winbond.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Copyright 2008, Network Appliance Inc.
  3. * Author: Jason McMullan <mcmullan <at> netapp.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. /* M25Pxx-specific commands */
  11. #define CMD_W25_WREN 0x06 /* Write Enable */
  12. #define CMD_W25_WRDI 0x04 /* Write Disable */
  13. #define CMD_W25_RDSR 0x05 /* Read Status Register */
  14. #define CMD_W25_WRSR 0x01 /* Write Status Register */
  15. #define CMD_W25_READ 0x03 /* Read Data Bytes */
  16. #define CMD_W25_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */
  17. #define CMD_W25_PP 0x02 /* Page Program */
  18. #define CMD_W25_SE 0x20 /* Sector (4K) Erase */
  19. #define CMD_W25_BE 0xd8 /* Block (64K) Erase */
  20. #define CMD_W25_CE 0xc7 /* Chip Erase */
  21. #define CMD_W25_DP 0xb9 /* Deep Power-down */
  22. #define CMD_W25_RES 0xab /* Release from DP, and Read Signature */
  23. struct winbond_spi_flash_params {
  24. uint16_t id;
  25. /* Log2 of page size in power-of-two mode */
  26. uint8_t l2_page_size;
  27. uint16_t pages_per_sector;
  28. uint16_t sectors_per_block;
  29. uint16_t nr_blocks;
  30. const char *name;
  31. };
  32. /* spi_flash needs to be first so upper layers can free() it */
  33. struct winbond_spi_flash {
  34. struct spi_flash flash;
  35. const struct winbond_spi_flash_params *params;
  36. };
  37. static inline struct winbond_spi_flash *
  38. to_winbond_spi_flash(struct spi_flash *flash)
  39. {
  40. return container_of(flash, struct winbond_spi_flash, flash);
  41. }
  42. static const struct winbond_spi_flash_params winbond_spi_flash_table[] = {
  43. {
  44. .id = 0x3015,
  45. .l2_page_size = 8,
  46. .pages_per_sector = 16,
  47. .sectors_per_block = 16,
  48. .nr_blocks = 32,
  49. .name = "W25X16",
  50. },
  51. {
  52. .id = 0x3016,
  53. .l2_page_size = 8,
  54. .pages_per_sector = 16,
  55. .sectors_per_block = 16,
  56. .nr_blocks = 64,
  57. .name = "W25X32",
  58. },
  59. {
  60. .id = 0x3017,
  61. .l2_page_size = 8,
  62. .pages_per_sector = 16,
  63. .sectors_per_block = 16,
  64. .nr_blocks = 128,
  65. .name = "W25X64",
  66. },
  67. {
  68. .id = 0x4015,
  69. .l2_page_size = 8,
  70. .pages_per_sector = 16,
  71. .sectors_per_block = 16,
  72. .nr_blocks = 32,
  73. .name = "W25Q16",
  74. },
  75. {
  76. .id = 0x4016,
  77. .l2_page_size = 8,
  78. .pages_per_sector = 16,
  79. .sectors_per_block = 16,
  80. .nr_blocks = 64,
  81. .name = "W25Q32",
  82. },
  83. {
  84. .id = 0x4017,
  85. .l2_page_size = 8,
  86. .pages_per_sector = 16,
  87. .sectors_per_block = 16,
  88. .nr_blocks = 128,
  89. .name = "W25Q64",
  90. },
  91. {
  92. .id = 0x4018,
  93. .l2_page_size = 8,
  94. .pages_per_sector = 16,
  95. .sectors_per_block = 16,
  96. .nr_blocks = 256,
  97. .name = "W25Q128",
  98. },
  99. };
  100. /*
  101. * Assemble the address part of a command for Winbond devices in
  102. * non-power-of-two page size mode.
  103. */
  104. static void winbond_build_address(struct winbond_spi_flash *stm, u8 *cmd, u32 offset)
  105. {
  106. unsigned long page_addr;
  107. unsigned long byte_addr;
  108. unsigned long page_size;
  109. unsigned int page_shift;
  110. /*
  111. * The "extra" space per page is the power-of-two page size
  112. * divided by 32.
  113. */
  114. page_shift = stm->params->l2_page_size;
  115. page_size = (1 << page_shift);
  116. page_addr = offset / page_size;
  117. byte_addr = offset % page_size;
  118. cmd[0] = page_addr >> (16 - page_shift);
  119. cmd[1] = page_addr << (page_shift - 8) | (byte_addr >> 8);
  120. cmd[2] = byte_addr;
  121. }
  122. static int winbond_read_fast(struct spi_flash *flash,
  123. u32 offset, size_t len, void *buf)
  124. {
  125. struct winbond_spi_flash *stm = to_winbond_spi_flash(flash);
  126. u8 cmd[5];
  127. cmd[0] = CMD_READ_ARRAY_FAST;
  128. winbond_build_address(stm, cmd + 1, offset);
  129. cmd[4] = 0x00;
  130. return spi_flash_read_common(flash, cmd, sizeof(cmd), buf, len);
  131. }
  132. static int winbond_write(struct spi_flash *flash,
  133. u32 offset, size_t len, const void *buf)
  134. {
  135. struct winbond_spi_flash *stm = to_winbond_spi_flash(flash);
  136. unsigned long page_addr;
  137. unsigned long byte_addr;
  138. unsigned long page_size;
  139. unsigned int page_shift;
  140. size_t chunk_len;
  141. size_t actual;
  142. int ret;
  143. u8 cmd[4];
  144. page_shift = stm->params->l2_page_size;
  145. page_size = (1 << page_shift);
  146. page_addr = offset / page_size;
  147. byte_addr = offset % page_size;
  148. ret = spi_claim_bus(flash->spi);
  149. if (ret) {
  150. debug("SF: Unable to claim SPI bus\n");
  151. return ret;
  152. }
  153. for (actual = 0; actual < len; actual += chunk_len) {
  154. chunk_len = min(len - actual, page_size - byte_addr);
  155. cmd[0] = CMD_W25_PP;
  156. cmd[1] = page_addr >> (16 - page_shift);
  157. cmd[2] = page_addr << (page_shift - 8) | (byte_addr >> 8);
  158. cmd[3] = byte_addr;
  159. debug("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %d\n",
  160. buf + actual,
  161. cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
  162. ret = spi_flash_cmd(flash->spi, CMD_W25_WREN, NULL, 0);
  163. if (ret < 0) {
  164. debug("SF: Enabling Write failed\n");
  165. goto out;
  166. }
  167. ret = spi_flash_cmd_write(flash->spi, cmd, 4,
  168. buf + actual, chunk_len);
  169. if (ret < 0) {
  170. debug("SF: Winbond Page Program failed\n");
  171. goto out;
  172. }
  173. ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
  174. if (ret)
  175. goto out;
  176. page_addr++;
  177. byte_addr = 0;
  178. }
  179. debug("SF: Winbond: Successfully programmed %u bytes @ 0x%x\n",
  180. len, offset);
  181. ret = 0;
  182. out:
  183. spi_release_bus(flash->spi);
  184. return ret;
  185. }
  186. int winbond_erase(struct spi_flash *flash, u32 offset, size_t len)
  187. {
  188. struct winbond_spi_flash *stm = to_winbond_spi_flash(flash);
  189. return spi_flash_cmd_erase(flash, CMD_W25_SE,
  190. (1 << stm->params->l2_page_size) * stm->params->pages_per_sector,
  191. offset, len);
  192. }
  193. struct spi_flash *spi_flash_probe_winbond(struct spi_slave *spi, u8 *idcode)
  194. {
  195. const struct winbond_spi_flash_params *params;
  196. unsigned page_size;
  197. struct winbond_spi_flash *stm;
  198. unsigned int i;
  199. for (i = 0; i < ARRAY_SIZE(winbond_spi_flash_table); i++) {
  200. params = &winbond_spi_flash_table[i];
  201. if (params->id == ((idcode[1] << 8) | idcode[2]))
  202. break;
  203. }
  204. if (i == ARRAY_SIZE(winbond_spi_flash_table)) {
  205. debug("SF: Unsupported Winbond ID %02x%02x\n",
  206. idcode[1], idcode[2]);
  207. return NULL;
  208. }
  209. stm = malloc(sizeof(struct winbond_spi_flash));
  210. if (!stm) {
  211. debug("SF: Failed to allocate memory\n");
  212. return NULL;
  213. }
  214. stm->params = params;
  215. stm->flash.spi = spi;
  216. stm->flash.name = params->name;
  217. /* Assuming power-of-two page size initially. */
  218. page_size = 1 << params->l2_page_size;
  219. stm->flash.write = winbond_write;
  220. stm->flash.erase = winbond_erase;
  221. stm->flash.read = winbond_read_fast;
  222. stm->flash.size = page_size * params->pages_per_sector
  223. * params->sectors_per_block
  224. * params->nr_blocks;
  225. printf("SF: Detected %s with page size %u, total ",
  226. params->name, page_size);
  227. print_size(stm->flash.size, "\n");
  228. return &stm->flash;
  229. }