eon.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * (C) Copyright 2010, ucRobotics Inc.
  3. * Author: Chong Huang <chuang@ucrobotics.com>
  4. * Licensed under the GPL-2 or later.
  5. */
  6. #include <common.h>
  7. #include <malloc.h>
  8. #include <spi_flash.h>
  9. #include "spi_flash_internal.h"
  10. /* EN25Q128-specific commands */
  11. #define CMD_EN25Q128_WREN 0x06 /* Write Enable */
  12. #define CMD_EN25Q128_WRDI 0x04 /* Write Disable */
  13. #define CMD_EN25Q128_RDSR 0x05 /* Read Status Register */
  14. #define CMD_EN25Q128_WRSR 0x01 /* Write Status Register */
  15. #define CMD_EN25Q128_READ 0x03 /* Read Data Bytes */
  16. #define CMD_EN25Q128_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */
  17. #define CMD_EN25Q128_PP 0x02 /* Page Program */
  18. #define CMD_EN25Q128_SE 0x20 /* Sector Erase */
  19. #define CMD_EN25Q128_BE 0xd8 /* Block Erase */
  20. #define CMD_EN25Q128_DP 0xb9 /* Deep Power-down */
  21. #define CMD_EN25Q128_RES 0xab /* Release from DP, and Read Signature */
  22. #define EON_ID_EN25Q128 0x18
  23. struct eon_spi_flash_params {
  24. u8 idcode1;
  25. u16 page_size;
  26. u16 pages_per_sector;
  27. u16 sectors_per_block;
  28. u16 nr_sectors;
  29. const char *name;
  30. };
  31. /* spi_flash needs to be first so upper layers can free() it */
  32. struct eon_spi_flash {
  33. struct spi_flash flash;
  34. const struct eon_spi_flash_params *params;
  35. };
  36. static inline struct eon_spi_flash *to_eon_spi_flash(struct spi_flash *flash)
  37. {
  38. return container_of(flash, struct eon_spi_flash, flash);
  39. }
  40. static const struct eon_spi_flash_params eon_spi_flash_table[] = {
  41. {
  42. .idcode1 = EON_ID_EN25Q128,
  43. .page_size = 256,
  44. .pages_per_sector = 16,
  45. .sectors_per_block = 16,
  46. .nr_sectors = 4096,
  47. .name = "EN25Q128",
  48. },
  49. };
  50. static int eon_read_fast(struct spi_flash *flash,
  51. u32 offset, size_t len, void *buf)
  52. {
  53. struct eon_spi_flash *eon = to_eon_spi_flash(flash);
  54. unsigned long page_addr;
  55. unsigned long page_size;
  56. u8 cmd[5];
  57. page_size = eon->params->page_size;
  58. page_addr = offset / page_size;
  59. cmd[0] = CMD_READ_ARRAY_FAST;
  60. cmd[1] = page_addr >> 8;
  61. cmd[2] = page_addr;
  62. cmd[3] = offset % page_size;
  63. cmd[4] = 0x00;
  64. return spi_flash_read_common(flash, cmd, sizeof(cmd), buf, len);
  65. }
  66. static int eon_write(struct spi_flash *flash,
  67. u32 offset, size_t len, const void *buf)
  68. {
  69. struct eon_spi_flash *eon = to_eon_spi_flash(flash);
  70. unsigned long page_addr;
  71. unsigned long byte_addr;
  72. unsigned long page_size;
  73. size_t chunk_len;
  74. size_t actual;
  75. int ret;
  76. u8 cmd[4];
  77. page_size = eon->params->page_size;
  78. page_addr = offset / page_size;
  79. byte_addr = offset % page_size;
  80. ret = spi_claim_bus(flash->spi);
  81. if (ret) {
  82. debug("SF: Unable to claim SPI bus\n");
  83. return ret;
  84. }
  85. ret = 0;
  86. for (actual = 0; actual < len; actual += chunk_len) {
  87. chunk_len = min(len - actual, page_size - byte_addr);
  88. cmd[0] = CMD_EN25Q128_PP;
  89. cmd[1] = page_addr >> 8;
  90. cmd[2] = page_addr;
  91. cmd[3] = byte_addr;
  92. debug
  93. ("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %d\n",
  94. buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
  95. ret = spi_flash_cmd(flash->spi, CMD_EN25Q128_WREN, NULL, 0);
  96. if (ret < 0) {
  97. debug("SF: Enabling Write failed\n");
  98. break;
  99. }
  100. ret = spi_flash_cmd_write(flash->spi, cmd, 4,
  101. buf + actual, chunk_len);
  102. if (ret < 0) {
  103. debug("SF: EON Page Program failed\n");
  104. break;
  105. }
  106. ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
  107. if (ret)
  108. break;
  109. page_addr++;
  110. byte_addr = 0;
  111. }
  112. debug("SF: EON: Successfully programmed %u bytes @ 0x%x\n",
  113. len, offset);
  114. spi_release_bus(flash->spi);
  115. return ret;
  116. }
  117. int eon_erase(struct spi_flash *flash, u32 offset, size_t len)
  118. {
  119. /* block erase */
  120. struct eon_spi_flash *eon = to_eon_spi_flash(flash);
  121. unsigned long block_size;
  122. size_t actual;
  123. int ret;
  124. u8 cmd[4];
  125. block_size = eon->params->page_size * eon->params->pages_per_sector
  126. * eon->params->sectors_per_block;
  127. if (offset % block_size || len % block_size) {
  128. debug("SF: Erase offset/length not multiple of block size\n");
  129. return -1;
  130. }
  131. len /= block_size;
  132. cmd[0] = CMD_EN25Q128_BE;
  133. cmd[2] = 0x00;
  134. cmd[3] = 0x00;
  135. ret = spi_claim_bus(flash->spi);
  136. if (ret) {
  137. debug("SF: Unable to claim SPI bus\n");
  138. return ret;
  139. }
  140. ret = 0;
  141. for (actual = 0; actual < len; actual++) {
  142. cmd[1] = (offset / block_size) + actual;
  143. ret = spi_flash_cmd(flash->spi, CMD_EN25Q128_WREN, NULL, 0);
  144. if (ret < 0) {
  145. debug("SF: Enabling Write failed\n");
  146. break;
  147. }
  148. ret = spi_flash_cmd_write(flash->spi, cmd, 4, NULL, 0);
  149. if (ret < 0) {
  150. debug("SF: EON page erase failed\n");
  151. break;
  152. }
  153. ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
  154. if (ret)
  155. break;
  156. }
  157. debug("SF: EON: Successfully erased %u bytes @ 0x%x\n",
  158. len * block_size, offset);
  159. spi_release_bus(flash->spi);
  160. return ret;
  161. }
  162. struct spi_flash *spi_flash_probe_eon(struct spi_slave *spi, u8 *idcode)
  163. {
  164. const struct eon_spi_flash_params *params;
  165. struct eon_spi_flash *eon;
  166. unsigned int i;
  167. for (i = 0; i < ARRAY_SIZE(eon_spi_flash_table); ++i) {
  168. params = &eon_spi_flash_table[i];
  169. if (params->idcode1 == idcode[2])
  170. break;
  171. }
  172. if (i == ARRAY_SIZE(eon_spi_flash_table)) {
  173. debug("SF: Unsupported EON ID %02x\n", idcode[1]);
  174. return NULL;
  175. }
  176. eon = malloc(sizeof(*eon));
  177. if (!eon) {
  178. debug("SF: Failed to allocate memory\n");
  179. return NULL;
  180. }
  181. eon->params = params;
  182. eon->flash.spi = spi;
  183. eon->flash.name = params->name;
  184. eon->flash.write = eon_write;
  185. eon->flash.erase = eon_erase;
  186. eon->flash.read = eon_read_fast;
  187. eon->flash.size = params->page_size * params->pages_per_sector
  188. * params->nr_sectors;
  189. debug("SF: Detected %s with page size %u, total %u bytes\n",
  190. params->name, params->page_size, eon->flash.size);
  191. return &eon->flash;
  192. }