winbond.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. static int winbond_write(struct spi_flash *flash,
  101. u32 offset, size_t len, const void *buf)
  102. {
  103. struct winbond_spi_flash *stm = to_winbond_spi_flash(flash);
  104. unsigned long page_addr;
  105. unsigned long byte_addr;
  106. unsigned long page_size;
  107. unsigned int page_shift;
  108. size_t chunk_len;
  109. size_t actual;
  110. int ret;
  111. u8 cmd[4];
  112. page_shift = stm->params->l2_page_size;
  113. page_size = (1 << page_shift);
  114. page_addr = offset / page_size;
  115. byte_addr = offset % page_size;
  116. ret = spi_claim_bus(flash->spi);
  117. if (ret) {
  118. debug("SF: Unable to claim SPI bus\n");
  119. return ret;
  120. }
  121. for (actual = 0; actual < len; actual += chunk_len) {
  122. chunk_len = min(len - actual, page_size - byte_addr);
  123. cmd[0] = CMD_W25_PP;
  124. cmd[1] = page_addr >> (16 - page_shift);
  125. cmd[2] = page_addr << (page_shift - 8) | (byte_addr >> 8);
  126. cmd[3] = byte_addr;
  127. debug("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %d\n",
  128. buf + actual,
  129. cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
  130. ret = spi_flash_cmd(flash->spi, CMD_W25_WREN, NULL, 0);
  131. if (ret < 0) {
  132. debug("SF: Enabling Write failed\n");
  133. goto out;
  134. }
  135. ret = spi_flash_cmd_write(flash->spi, cmd, 4,
  136. buf + actual, chunk_len);
  137. if (ret < 0) {
  138. debug("SF: Winbond Page Program failed\n");
  139. goto out;
  140. }
  141. ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
  142. if (ret)
  143. goto out;
  144. page_addr++;
  145. byte_addr = 0;
  146. }
  147. debug("SF: Winbond: Successfully programmed %u bytes @ 0x%x\n",
  148. len, offset);
  149. ret = 0;
  150. out:
  151. spi_release_bus(flash->spi);
  152. return ret;
  153. }
  154. static int winbond_erase(struct spi_flash *flash, u32 offset, size_t len)
  155. {
  156. return spi_flash_cmd_erase(flash, CMD_W25_SE, offset, len);
  157. }
  158. struct spi_flash *spi_flash_probe_winbond(struct spi_slave *spi, u8 *idcode)
  159. {
  160. const struct winbond_spi_flash_params *params;
  161. unsigned page_size;
  162. struct winbond_spi_flash *stm;
  163. unsigned int i;
  164. for (i = 0; i < ARRAY_SIZE(winbond_spi_flash_table); i++) {
  165. params = &winbond_spi_flash_table[i];
  166. if (params->id == ((idcode[1] << 8) | idcode[2]))
  167. break;
  168. }
  169. if (i == ARRAY_SIZE(winbond_spi_flash_table)) {
  170. debug("SF: Unsupported Winbond ID %02x%02x\n",
  171. idcode[1], idcode[2]);
  172. return NULL;
  173. }
  174. stm = malloc(sizeof(struct winbond_spi_flash));
  175. if (!stm) {
  176. debug("SF: Failed to allocate memory\n");
  177. return NULL;
  178. }
  179. stm->params = params;
  180. stm->flash.spi = spi;
  181. stm->flash.name = params->name;
  182. /* Assuming power-of-two page size initially. */
  183. page_size = 1 << params->l2_page_size;
  184. stm->flash.write = winbond_write;
  185. stm->flash.erase = winbond_erase;
  186. stm->flash.read = spi_flash_cmd_read_fast;
  187. stm->flash.sector_size = (1 << stm->params->l2_page_size) *
  188. stm->params->pages_per_sector;
  189. stm->flash.size = page_size * params->pages_per_sector
  190. * params->sectors_per_block
  191. * params->nr_blocks;
  192. return &stm->flash;
  193. }