sst.c 7.3 KB

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