winbond.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. #define WINBOND_ID_W25X16 0x3015
  24. #define WINBOND_ID_W25X32 0x3016
  25. #define WINBOND_ID_W25X64 0x3017
  26. #define WINBOND_SR_WIP (1 << 0) /* Write-in-Progress */
  27. struct winbond_spi_flash_params {
  28. uint16_t id;
  29. /* Log2 of page size in power-of-two mode */
  30. uint8_t l2_page_size;
  31. uint16_t pages_per_sector;
  32. uint16_t sectors_per_block;
  33. uint8_t nr_blocks;
  34. const char *name;
  35. };
  36. /* spi_flash needs to be first so upper layers can free() it */
  37. struct winbond_spi_flash {
  38. struct spi_flash flash;
  39. const struct winbond_spi_flash_params *params;
  40. };
  41. static inline struct winbond_spi_flash *
  42. to_winbond_spi_flash(struct spi_flash *flash)
  43. {
  44. return container_of(flash, struct winbond_spi_flash, flash);
  45. }
  46. static const struct winbond_spi_flash_params winbond_spi_flash_table[] = {
  47. {
  48. .id = WINBOND_ID_W25X16,
  49. .l2_page_size = 8,
  50. .pages_per_sector = 16,
  51. .sectors_per_block = 16,
  52. .nr_blocks = 32,
  53. .name = "W25X16",
  54. },
  55. {
  56. .id = WINBOND_ID_W25X32,
  57. .l2_page_size = 8,
  58. .pages_per_sector = 16,
  59. .sectors_per_block = 16,
  60. .nr_blocks = 64,
  61. .name = "W25X32",
  62. },
  63. {
  64. .id = WINBOND_ID_W25X64,
  65. .l2_page_size = 8,
  66. .pages_per_sector = 16,
  67. .sectors_per_block = 16,
  68. .nr_blocks = 128,
  69. .name = "W25X64",
  70. },
  71. };
  72. static int winbond_wait_ready(struct spi_flash *flash, unsigned long timeout)
  73. {
  74. struct spi_slave *spi = flash->spi;
  75. unsigned long timebase;
  76. int ret;
  77. u8 status;
  78. u8 cmd[4] = { CMD_W25_RDSR, 0xff, 0xff, 0xff };
  79. ret = spi_xfer(spi, 32, &cmd[0], NULL, SPI_XFER_BEGIN);
  80. if (ret) {
  81. debug("SF: Failed to send command %02x: %d\n", cmd, ret);
  82. return ret;
  83. }
  84. timebase = get_timer(0);
  85. do {
  86. ret = spi_xfer(spi, 8, NULL, &status, 0);
  87. if (ret) {
  88. debug("SF: Failed to get status for cmd %02x: %d\n", cmd, ret);
  89. return -1;
  90. }
  91. if ((status & WINBOND_SR_WIP) == 0)
  92. break;
  93. } while (get_timer(timebase) < timeout);
  94. spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END);
  95. if ((status & WINBOND_SR_WIP) == 0)
  96. return 0;
  97. debug("SF: Timed out on command %02x: %d\n", cmd, ret);
  98. /* Timed out */
  99. return -1;
  100. }
  101. /*
  102. * Assemble the address part of a command for Winbond devices in
  103. * non-power-of-two page size mode.
  104. */
  105. static void winbond_build_address(struct winbond_spi_flash *stm, u8 *cmd, u32 offset)
  106. {
  107. unsigned long page_addr;
  108. unsigned long byte_addr;
  109. unsigned long page_size;
  110. unsigned int page_shift;
  111. /*
  112. * The "extra" space per page is the power-of-two page size
  113. * divided by 32.
  114. */
  115. page_shift = stm->params->l2_page_size;
  116. page_size = (1 << page_shift);
  117. page_addr = offset / page_size;
  118. byte_addr = offset % page_size;
  119. cmd[0] = page_addr >> (16 - page_shift);
  120. cmd[1] = page_addr << (page_shift - 8) | (byte_addr >> 8);
  121. cmd[2] = byte_addr;
  122. }
  123. static int winbond_read_fast(struct spi_flash *flash,
  124. u32 offset, size_t len, void *buf)
  125. {
  126. struct winbond_spi_flash *stm = to_winbond_spi_flash(flash);
  127. u8 cmd[5];
  128. cmd[0] = CMD_READ_ARRAY_FAST;
  129. winbond_build_address(stm, cmd + 1, offset);
  130. cmd[4] = 0x00;
  131. return spi_flash_read_common(flash, cmd, sizeof(cmd), buf, len);
  132. }
  133. static int winbond_write(struct spi_flash *flash,
  134. u32 offset, size_t len, const void *buf)
  135. {
  136. struct winbond_spi_flash *stm = to_winbond_spi_flash(flash);
  137. unsigned long page_addr;
  138. unsigned long byte_addr;
  139. unsigned long page_size;
  140. unsigned int page_shift;
  141. size_t chunk_len;
  142. size_t actual;
  143. int ret;
  144. u8 cmd[4];
  145. page_shift = stm->params->l2_page_size;
  146. page_size = (1 << page_shift);
  147. page_addr = offset / page_size;
  148. byte_addr = offset % page_size;
  149. ret = spi_claim_bus(flash->spi);
  150. if (ret) {
  151. debug("SF: Unable to claim SPI bus\n");
  152. return ret;
  153. }
  154. for (actual = 0; actual < len; actual += chunk_len) {
  155. chunk_len = min(len - actual, page_size - byte_addr);
  156. cmd[0] = CMD_W25_PP;
  157. cmd[1] = page_addr >> (16 - page_shift);
  158. cmd[2] = page_addr << (page_shift - 8) | (byte_addr >> 8);
  159. cmd[3] = byte_addr;
  160. debug("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %d\n",
  161. buf + actual,
  162. cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
  163. ret = spi_flash_cmd(flash->spi, CMD_W25_WREN, NULL, 0);
  164. if (ret < 0) {
  165. debug("SF: Enabling Write failed\n");
  166. goto out;
  167. }
  168. ret = spi_flash_cmd_write(flash->spi, cmd, 4,
  169. buf + actual, chunk_len);
  170. if (ret < 0) {
  171. debug("SF: Winbond Page Program failed\n");
  172. goto out;
  173. }
  174. ret = winbond_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
  175. if (ret < 0) {
  176. debug("SF: Winbond page programming timed out\n");
  177. goto out;
  178. }
  179. page_addr++;
  180. byte_addr = 0;
  181. }
  182. debug("SF: Winbond: Successfully programmed %u bytes @ 0x%x\n",
  183. len, offset);
  184. ret = 0;
  185. out:
  186. spi_release_bus(flash->spi);
  187. return ret;
  188. }
  189. int winbond_erase(struct spi_flash *flash, u32 offset, size_t len)
  190. {
  191. struct winbond_spi_flash *stm = to_winbond_spi_flash(flash);
  192. unsigned long sector_size;
  193. unsigned int page_shift;
  194. size_t actual;
  195. int ret;
  196. u8 cmd[4];
  197. /*
  198. * This function currently uses sector erase only.
  199. * probably speed things up by using bulk erase
  200. * when possible.
  201. */
  202. page_shift = stm->params->l2_page_size;
  203. sector_size = (1 << page_shift) * stm->params->pages_per_sector;
  204. if (offset % sector_size || len % sector_size) {
  205. debug("SF: Erase offset/length not multiple of sector size\n");
  206. return -1;
  207. }
  208. len /= sector_size;
  209. cmd[0] = CMD_W25_SE;
  210. ret = spi_claim_bus(flash->spi);
  211. if (ret) {
  212. debug("SF: Unable to claim SPI bus\n");
  213. return ret;
  214. }
  215. for (actual = 0; actual < len; actual++) {
  216. winbond_build_address(stm, &cmd[1], offset + actual * sector_size);
  217. printf("Erase: %02x %02x %02x %02x\n",
  218. cmd[0], cmd[1], cmd[2], cmd[3]);
  219. ret = spi_flash_cmd(flash->spi, CMD_W25_WREN, NULL, 0);
  220. if (ret < 0) {
  221. debug("SF: Enabling Write failed\n");
  222. goto out;
  223. }
  224. ret = spi_flash_cmd_write(flash->spi, cmd, 4, NULL, 0);
  225. if (ret < 0) {
  226. debug("SF: Winbond sector erase failed\n");
  227. goto out;
  228. }
  229. ret = winbond_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
  230. if (ret < 0) {
  231. debug("SF: Winbond sector erase timed out\n");
  232. goto out;
  233. }
  234. }
  235. debug("SF: Winbond: Successfully erased %u bytes @ 0x%x\n",
  236. len * sector_size, offset);
  237. ret = 0;
  238. out:
  239. spi_release_bus(flash->spi);
  240. return ret;
  241. }
  242. struct spi_flash *spi_flash_probe_winbond(struct spi_slave *spi, u8 *idcode)
  243. {
  244. const struct winbond_spi_flash_params *params;
  245. unsigned page_size;
  246. struct winbond_spi_flash *stm;
  247. unsigned int i;
  248. for (i = 0; i < ARRAY_SIZE(winbond_spi_flash_table); i++) {
  249. params = &winbond_spi_flash_table[i];
  250. if (params->id == ((idcode[1] << 8) | idcode[2]))
  251. break;
  252. }
  253. if (i == ARRAY_SIZE(winbond_spi_flash_table)) {
  254. debug("SF: Unsupported Winbond ID %02x%02x\n",
  255. idcode[1], idcode[2]);
  256. return NULL;
  257. }
  258. stm = malloc(sizeof(struct winbond_spi_flash));
  259. if (!stm) {
  260. debug("SF: Failed to allocate memory\n");
  261. return NULL;
  262. }
  263. stm->params = params;
  264. stm->flash.spi = spi;
  265. stm->flash.name = params->name;
  266. /* Assuming power-of-two page size initially. */
  267. page_size = 1 << params->l2_page_size;
  268. stm->flash.write = winbond_write;
  269. stm->flash.erase = winbond_erase;
  270. stm->flash.read = winbond_read_fast;
  271. stm->flash.size = page_size * params->pages_per_sector
  272. * params->sectors_per_block
  273. * params->nr_blocks;
  274. printf("SF: Detected %s with page size %u, total ",
  275. params->name, page_size);
  276. print_size(stm->flash.size, "\n");
  277. return &stm->flash;
  278. }