winbond.c 8.2 KB

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