sst.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Driver for SST serial flashes
  3. *
  4. * (C) Copyright 2000-2002
  5. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  6. * Copyright 2008, Network Appliance Inc.
  7. * Jason McMullan <mcmullan@netapp.com>
  8. * Copyright (C) 2004-2007 Freescale Semiconductor, Inc.
  9. * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
  10. * Copyright (c) 2008-2009 Analog Devices Inc.
  11. *
  12. * Licensed under the GPL-2 or later.
  13. */
  14. #include <common.h>
  15. #include <malloc.h>
  16. #include <spi_flash.h>
  17. #include "spi_flash_internal.h"
  18. #define CMD_SST_WREN 0x06 /* Write Enable */
  19. #define CMD_SST_WRDI 0x04 /* Write Disable */
  20. #define CMD_SST_RDSR 0x05 /* Read Status Register */
  21. #define CMD_SST_WRSR 0x01 /* Write Status Register */
  22. #define CMD_SST_READ 0x03 /* Read Data Bytes */
  23. #define CMD_SST_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */
  24. #define CMD_SST_BP 0x02 /* Byte Program */
  25. #define CMD_SST_AAI_WP 0xAD /* Auto Address Increment Word Program */
  26. #define CMD_SST_SE 0x20 /* Sector Erase */
  27. #define SST_SR_WIP (1 << 0) /* Write-in-Progress */
  28. #define SST_SR_WEL (1 << 1) /* Write enable */
  29. #define SST_SR_BP0 (1 << 2) /* Block Protection 0 */
  30. #define SST_SR_BP1 (1 << 3) /* Block Protection 1 */
  31. #define SST_SR_BP2 (1 << 4) /* Block Protection 2 */
  32. #define SST_SR_AAI (1 << 6) /* Addressing mode */
  33. #define SST_SR_BPL (1 << 7) /* BP bits lock */
  34. struct sst_spi_flash_params {
  35. u8 idcode1;
  36. u16 nr_sectors;
  37. const char *name;
  38. };
  39. struct sst_spi_flash {
  40. struct spi_flash flash;
  41. const struct sst_spi_flash_params *params;
  42. };
  43. static inline struct sst_spi_flash *to_sst_spi_flash(struct spi_flash *flash)
  44. {
  45. return container_of(flash, struct sst_spi_flash, flash);
  46. }
  47. #define SST_SECTOR_SIZE (4 * 1024)
  48. static const struct sst_spi_flash_params sst_spi_flash_table[] = {
  49. {
  50. .idcode1 = 0x8d,
  51. .nr_sectors = 128,
  52. .name = "SST25VF040B",
  53. },{
  54. .idcode1 = 0x8e,
  55. .nr_sectors = 256,
  56. .name = "SST25VF080B",
  57. },{
  58. .idcode1 = 0x41,
  59. .nr_sectors = 512,
  60. .name = "SST25VF016B",
  61. },{
  62. .idcode1 = 0x4a,
  63. .nr_sectors = 1024,
  64. .name = "SST25VF032B",
  65. },{
  66. .idcode1 = 0x4b,
  67. .nr_sectors = 2048,
  68. .name = "SST25VF064C",
  69. },{
  70. .idcode1 = 0x01,
  71. .nr_sectors = 16,
  72. .name = "SST25WF512",
  73. },{
  74. .idcode1 = 0x02,
  75. .nr_sectors = 32,
  76. .name = "SST25WF010",
  77. },{
  78. .idcode1 = 0x03,
  79. .nr_sectors = 64,
  80. .name = "SST25WF020",
  81. },{
  82. .idcode1 = 0x04,
  83. .nr_sectors = 128,
  84. .name = "SST25WF040",
  85. },
  86. };
  87. static int
  88. sst_enable_writing(struct spi_flash *flash)
  89. {
  90. int ret = spi_flash_cmd(flash->spi, CMD_SST_WREN, NULL, 0);
  91. if (ret)
  92. debug("SF: Enabling Write failed\n");
  93. return ret;
  94. }
  95. static int
  96. sst_disable_writing(struct spi_flash *flash)
  97. {
  98. int ret = spi_flash_cmd(flash->spi, CMD_SST_WRDI, NULL, 0);
  99. if (ret)
  100. debug("SF: Disabling Write failed\n");
  101. return ret;
  102. }
  103. static int
  104. sst_byte_write(struct spi_flash *flash, u32 offset, const void *buf)
  105. {
  106. int ret;
  107. u8 cmd[4] = {
  108. CMD_SST_BP,
  109. offset >> 16,
  110. offset >> 8,
  111. offset,
  112. };
  113. debug("BP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
  114. spi_w8r8(flash->spi, CMD_SST_RDSR), buf, cmd[0], offset);
  115. ret = sst_enable_writing(flash);
  116. if (ret)
  117. return ret;
  118. ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), buf, 1);
  119. if (ret)
  120. return ret;
  121. return spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
  122. }
  123. static int
  124. sst_write(struct spi_flash *flash, u32 offset, size_t len, const void *buf)
  125. {
  126. size_t actual, cmd_len;
  127. int ret;
  128. u8 cmd[4];
  129. ret = spi_claim_bus(flash->spi);
  130. if (ret) {
  131. debug("SF: Unable to claim SPI bus\n");
  132. return ret;
  133. }
  134. /* If the data is not word aligned, write out leading single byte */
  135. actual = offset % 2;
  136. if (actual) {
  137. ret = sst_byte_write(flash, offset, buf);
  138. if (ret)
  139. goto done;
  140. }
  141. offset += actual;
  142. ret = sst_enable_writing(flash);
  143. if (ret)
  144. goto done;
  145. cmd_len = 4;
  146. cmd[0] = CMD_SST_AAI_WP;
  147. cmd[1] = offset >> 16;
  148. cmd[2] = offset >> 8;
  149. cmd[3] = offset;
  150. for (; actual < len - 1; actual += 2) {
  151. debug("WP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
  152. spi_w8r8(flash->spi, CMD_SST_RDSR), buf + actual, cmd[0],
  153. offset);
  154. ret = spi_flash_cmd_write(flash->spi, cmd, cmd_len,
  155. buf + actual, 2);
  156. if (ret) {
  157. debug("SF: sst word program failed\n");
  158. break;
  159. }
  160. ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
  161. if (ret)
  162. break;
  163. cmd_len = 1;
  164. offset += 2;
  165. }
  166. if (!ret)
  167. ret = sst_disable_writing(flash);
  168. /* If there is a single trailing byte, write it out */
  169. if (!ret && actual != len)
  170. ret = sst_byte_write(flash, offset, buf + actual);
  171. done:
  172. debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
  173. ret ? "failure" : "success", len, offset - actual);
  174. spi_release_bus(flash->spi);
  175. return ret;
  176. }
  177. static int sst_erase(struct spi_flash *flash, u32 offset, size_t len)
  178. {
  179. return spi_flash_cmd_erase(flash, CMD_SST_SE, offset, len);
  180. }
  181. static int
  182. sst_unlock(struct spi_flash *flash)
  183. {
  184. int ret;
  185. u8 cmd, status;
  186. ret = sst_enable_writing(flash);
  187. if (ret)
  188. return ret;
  189. cmd = CMD_SST_WRSR;
  190. status = 0;
  191. ret = spi_flash_cmd_write(flash->spi, &cmd, 1, &status, 1);
  192. if (ret)
  193. debug("SF: Unable to set status byte\n");
  194. debug("SF: sst: status = %x\n", spi_w8r8(flash->spi, CMD_SST_RDSR));
  195. return ret;
  196. }
  197. struct spi_flash *
  198. spi_flash_probe_sst(struct spi_slave *spi, u8 *idcode)
  199. {
  200. const struct sst_spi_flash_params *params;
  201. struct sst_spi_flash *stm;
  202. size_t i;
  203. for (i = 0; i < ARRAY_SIZE(sst_spi_flash_table); ++i) {
  204. params = &sst_spi_flash_table[i];
  205. if (params->idcode1 == idcode[2])
  206. break;
  207. }
  208. if (i == ARRAY_SIZE(sst_spi_flash_table)) {
  209. debug("SF: Unsupported SST ID %02x\n", idcode[1]);
  210. return NULL;
  211. }
  212. stm = malloc(sizeof(*stm));
  213. if (!stm) {
  214. debug("SF: Failed to allocate memory\n");
  215. return NULL;
  216. }
  217. stm->params = params;
  218. stm->flash.spi = spi;
  219. stm->flash.name = params->name;
  220. stm->flash.write = sst_write;
  221. stm->flash.erase = sst_erase;
  222. stm->flash.read = spi_flash_cmd_read_fast;
  223. stm->flash.sector_size = SST_SECTOR_SIZE;
  224. stm->flash.size = stm->flash.sector_size * params->nr_sectors;
  225. /* Flash powers up read-only, so clear BP# bits */
  226. sst_unlock(&stm->flash);
  227. return &stm->flash;
  228. }