winbond.c 8.4 KB

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