sst.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 = 0x01,
  51. .nr_sectors = 128,
  52. .name = "SST25WF512",
  53. },{
  54. .idcode1 = 0x02,
  55. .nr_sectors = 256,
  56. .name = "SST25WF010",
  57. },{
  58. .idcode1 = 0x03,
  59. .nr_sectors = 512,
  60. .name = "SST25WF020",
  61. },{
  62. .idcode1 = 0x04,
  63. .nr_sectors = 1024,
  64. .name = "SST25WF040",
  65. },
  66. };
  67. static int
  68. sst_wait_ready(struct spi_flash *flash, unsigned long timeout)
  69. {
  70. struct spi_slave *spi = flash->spi;
  71. unsigned long timebase;
  72. int ret;
  73. u8 byte = CMD_SST_RDSR;
  74. ret = spi_xfer(spi, sizeof(byte) * 8, &byte, NULL, SPI_XFER_BEGIN);
  75. if (ret) {
  76. debug("SF: Failed to send command %02x: %d\n", byte, ret);
  77. return ret;
  78. }
  79. timebase = get_timer(0);
  80. do {
  81. ret = spi_xfer(spi, sizeof(byte) * 8, NULL, &byte, 0);
  82. if (ret)
  83. break;
  84. if ((byte & SST_SR_WIP) == 0)
  85. break;
  86. } while (get_timer(timebase) < timeout);
  87. spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END);
  88. if (!ret && (byte & SST_SR_WIP) != 0)
  89. ret = -1;
  90. if (ret)
  91. debug("SF: sst wait for ready timed out\n");
  92. return ret;
  93. }
  94. static int
  95. sst_enable_writing(struct spi_flash *flash)
  96. {
  97. int ret = spi_flash_cmd(flash->spi, CMD_SST_WREN, NULL, 0);
  98. if (ret)
  99. debug("SF: Enabling Write failed\n");
  100. return ret;
  101. }
  102. static int
  103. sst_disable_writing(struct spi_flash *flash)
  104. {
  105. int ret = spi_flash_cmd(flash->spi, CMD_SST_WRDI, NULL, 0);
  106. if (ret)
  107. debug("SF: Disabling Write failed\n");
  108. return ret;
  109. }
  110. static int
  111. sst_read_fast(struct spi_flash *flash, u32 offset, size_t len, void *buf)
  112. {
  113. u8 cmd[5] = {
  114. CMD_READ_ARRAY_FAST,
  115. offset >> 16,
  116. offset >> 8,
  117. offset,
  118. 0x00,
  119. };
  120. return spi_flash_read_common(flash, cmd, sizeof(cmd), buf, len);
  121. }
  122. static int
  123. sst_byte_write(struct spi_flash *flash, u32 offset, const void *buf)
  124. {
  125. int ret;
  126. u8 cmd[4] = {
  127. CMD_SST_BP,
  128. offset >> 16,
  129. offset >> 8,
  130. offset,
  131. };
  132. debug("BP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
  133. spi_w8r8(flash->spi, CMD_SST_RDSR), buf, cmd[0], offset);
  134. ret = sst_enable_writing(flash);
  135. if (ret)
  136. return ret;
  137. ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), buf, 1);
  138. if (ret)
  139. return ret;
  140. return sst_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
  141. }
  142. static int
  143. sst_write(struct spi_flash *flash, u32 offset, size_t len, const void *buf)
  144. {
  145. size_t actual, cmd_len;
  146. int ret;
  147. u8 cmd[4];
  148. ret = spi_claim_bus(flash->spi);
  149. if (ret) {
  150. debug("SF: Unable to claim SPI bus\n");
  151. return ret;
  152. }
  153. /* If the data is not word aligned, write out leading single byte */
  154. actual = offset % 2;
  155. if (actual) {
  156. ret = sst_byte_write(flash, offset, buf);
  157. if (ret)
  158. goto done;
  159. }
  160. offset += actual;
  161. ret = sst_enable_writing(flash);
  162. if (ret)
  163. goto done;
  164. cmd_len = 4;
  165. cmd[0] = CMD_SST_AAI_WP;
  166. cmd[1] = offset >> 16;
  167. cmd[2] = offset >> 8;
  168. cmd[3] = offset;
  169. for (; actual < len - 1; actual += 2) {
  170. debug("WP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
  171. spi_w8r8(flash->spi, CMD_SST_RDSR), buf + actual, cmd[0],
  172. offset);
  173. ret = spi_flash_cmd_write(flash->spi, cmd, cmd_len,
  174. buf + actual, 2);
  175. if (ret) {
  176. debug("SF: sst word program failed\n");
  177. break;
  178. }
  179. ret = sst_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
  180. if (ret)
  181. break;
  182. cmd_len = 1;
  183. offset += 2;
  184. }
  185. if (!ret)
  186. ret = sst_disable_writing(flash);
  187. /* If there is a single trailing byte, write it out */
  188. if (!ret && actual != len)
  189. ret = sst_byte_write(flash, offset, buf + actual);
  190. done:
  191. debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
  192. ret ? "failure" : "success", len, offset - actual);
  193. spi_release_bus(flash->spi);
  194. return ret;
  195. }
  196. int
  197. sst_erase(struct spi_flash *flash, u32 offset, size_t len)
  198. {
  199. unsigned long sector_size;
  200. u32 start, end;
  201. int ret;
  202. u8 cmd[4];
  203. /*
  204. * This function currently uses sector erase only.
  205. * Probably speed things up by using bulk erase
  206. * when possible.
  207. */
  208. sector_size = SST_SECTOR_SIZE;
  209. if (offset % sector_size) {
  210. debug("SF: Erase offset not multiple of sector size\n");
  211. return -1;
  212. }
  213. ret = spi_claim_bus(flash->spi);
  214. if (ret) {
  215. debug("SF: Unable to claim SPI bus\n");
  216. return ret;
  217. }
  218. cmd[0] = CMD_SST_SE;
  219. cmd[3] = 0;
  220. start = offset;
  221. end = start + len;
  222. ret = 0;
  223. while (offset < end) {
  224. cmd[1] = offset >> 16;
  225. cmd[2] = offset >> 8;
  226. offset += sector_size;
  227. debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
  228. cmd[2], cmd[3], offset);
  229. ret = sst_enable_writing(flash);
  230. if (ret)
  231. break;
  232. ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), NULL, 0);
  233. if (ret) {
  234. debug("SF: sst page erase failed\n");
  235. break;
  236. }
  237. ret = sst_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
  238. if (ret)
  239. break;
  240. }
  241. debug("SF: sst: Successfully erased %lu bytes @ 0x%x\n",
  242. len * sector_size, start);
  243. spi_release_bus(flash->spi);
  244. return ret;
  245. }
  246. static int
  247. sst_unlock(struct spi_flash *flash)
  248. {
  249. int ret;
  250. u8 cmd, status;
  251. ret = sst_enable_writing(flash);
  252. if (ret)
  253. return ret;
  254. cmd = CMD_SST_WRSR;
  255. status = 0;
  256. ret = spi_flash_cmd_write(flash->spi, &cmd, 1, &status, 1);
  257. if (ret)
  258. debug("SF: Unable to set status byte\n");
  259. debug("SF: sst: status = %x\n", spi_w8r8(flash->spi, CMD_SST_RDSR));
  260. return ret;
  261. }
  262. struct spi_flash *
  263. spi_flash_probe_sst(struct spi_slave *spi, u8 *idcode)
  264. {
  265. const struct sst_spi_flash_params *params;
  266. struct sst_spi_flash *stm;
  267. size_t i;
  268. for (i = 0; i < ARRAY_SIZE(sst_spi_flash_table); ++i) {
  269. params = &sst_spi_flash_table[i];
  270. if (params->idcode1 == idcode[2])
  271. break;
  272. }
  273. if (i == ARRAY_SIZE(sst_spi_flash_table)) {
  274. debug("SF: Unsupported SST ID %02x\n", idcode[1]);
  275. return NULL;
  276. }
  277. stm = malloc(sizeof(*stm));
  278. if (!stm) {
  279. debug("SF: Failed to allocate memory\n");
  280. return NULL;
  281. }
  282. stm->params = params;
  283. stm->flash.spi = spi;
  284. stm->flash.name = params->name;
  285. stm->flash.write = sst_write;
  286. stm->flash.erase = sst_erase;
  287. stm->flash.read = sst_read_fast;
  288. stm->flash.size = SST_SECTOR_SIZE * params->nr_sectors;
  289. debug("SF: Detected %s with page size %u, total %u bytes\n",
  290. params->name, SST_SECTOR_SIZE, stm->flash.size);
  291. /* Flash powers up read-only, so clear BP# bits */
  292. sst_unlock(&stm->flash);
  293. return &stm->flash;
  294. }