sst.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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_BP 0x02 /* Byte Program */
  19. #define CMD_SST_AAI_WP 0xAD /* Auto Address Increment Word Program */
  20. #define CMD_SST_SE 0x20 /* Sector Erase */
  21. #define SST_SR_WIP (1 << 0) /* Write-in-Progress */
  22. #define SST_SR_WEL (1 << 1) /* Write enable */
  23. #define SST_SR_BP0 (1 << 2) /* Block Protection 0 */
  24. #define SST_SR_BP1 (1 << 3) /* Block Protection 1 */
  25. #define SST_SR_BP2 (1 << 4) /* Block Protection 2 */
  26. #define SST_SR_AAI (1 << 6) /* Addressing mode */
  27. #define SST_SR_BPL (1 << 7) /* BP bits lock */
  28. #define SST_FEAT_WP (1 << 0) /* Supports AAI word program */
  29. #define SST_FEAT_MBP (1 << 1) /* Supports multibyte program */
  30. struct sst_spi_flash_params {
  31. u8 idcode1;
  32. u8 flags;
  33. u16 nr_sectors;
  34. const char *name;
  35. };
  36. struct sst_spi_flash {
  37. struct spi_flash flash;
  38. const struct sst_spi_flash_params *params;
  39. };
  40. static inline struct sst_spi_flash *to_sst_spi_flash(struct spi_flash *flash)
  41. {
  42. return container_of(flash, struct sst_spi_flash, flash);
  43. }
  44. #define SST_SECTOR_SIZE (4 * 1024)
  45. #define SST_PAGE_SIZE 256
  46. static const struct sst_spi_flash_params sst_spi_flash_table[] = {
  47. {
  48. .idcode1 = 0x8d,
  49. .flags = SST_FEAT_WP,
  50. .nr_sectors = 128,
  51. .name = "SST25VF040B",
  52. },{
  53. .idcode1 = 0x8e,
  54. .flags = SST_FEAT_WP,
  55. .nr_sectors = 256,
  56. .name = "SST25VF080B",
  57. },{
  58. .idcode1 = 0x41,
  59. .flags = SST_FEAT_WP,
  60. .nr_sectors = 512,
  61. .name = "SST25VF016B",
  62. },{
  63. .idcode1 = 0x4a,
  64. .flags = SST_FEAT_WP,
  65. .nr_sectors = 1024,
  66. .name = "SST25VF032B",
  67. },{
  68. .idcode1 = 0x4b,
  69. .flags = SST_FEAT_MBP,
  70. .nr_sectors = 2048,
  71. .name = "SST25VF064C",
  72. },{
  73. .idcode1 = 0x01,
  74. .flags = SST_FEAT_WP,
  75. .nr_sectors = 16,
  76. .name = "SST25WF512",
  77. },{
  78. .idcode1 = 0x02,
  79. .flags = SST_FEAT_WP,
  80. .nr_sectors = 32,
  81. .name = "SST25WF010",
  82. },{
  83. .idcode1 = 0x03,
  84. .flags = SST_FEAT_WP,
  85. .nr_sectors = 64,
  86. .name = "SST25WF020",
  87. },{
  88. .idcode1 = 0x04,
  89. .flags = SST_FEAT_WP,
  90. .nr_sectors = 128,
  91. .name = "SST25WF040",
  92. },
  93. };
  94. static int
  95. sst_enable_writing(struct spi_flash *flash)
  96. {
  97. int ret = spi_flash_cmd_write_enable(flash);
  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_write_disable(flash);
  106. if (ret)
  107. debug("SF: Disabling Write failed\n");
  108. return ret;
  109. }
  110. static int
  111. sst_byte_write(struct spi_flash *flash, u32 offset, const void *buf)
  112. {
  113. int ret;
  114. u8 cmd[4] = {
  115. CMD_SST_BP,
  116. offset >> 16,
  117. offset >> 8,
  118. offset,
  119. };
  120. debug("BP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
  121. spi_w8r8(flash->spi, CMD_READ_STATUS), buf, cmd[0], offset);
  122. ret = sst_enable_writing(flash);
  123. if (ret)
  124. return ret;
  125. ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), buf, 1);
  126. if (ret)
  127. return ret;
  128. return spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
  129. }
  130. static int
  131. sst_write_wp(struct spi_flash *flash, u32 offset, size_t len, const void *buf)
  132. {
  133. size_t actual, cmd_len;
  134. int ret;
  135. u8 cmd[4];
  136. ret = spi_claim_bus(flash->spi);
  137. if (ret) {
  138. debug("SF: Unable to claim SPI bus\n");
  139. return ret;
  140. }
  141. /* If the data is not word aligned, write out leading single byte */
  142. actual = offset % 2;
  143. if (actual) {
  144. ret = sst_byte_write(flash, offset, buf);
  145. if (ret)
  146. goto done;
  147. }
  148. offset += actual;
  149. ret = sst_enable_writing(flash);
  150. if (ret)
  151. goto done;
  152. cmd_len = 4;
  153. cmd[0] = CMD_SST_AAI_WP;
  154. cmd[1] = offset >> 16;
  155. cmd[2] = offset >> 8;
  156. cmd[3] = offset;
  157. for (; actual < len - 1; actual += 2) {
  158. debug("WP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
  159. spi_w8r8(flash->spi, CMD_READ_STATUS), buf + actual, cmd[0],
  160. offset);
  161. ret = spi_flash_cmd_write(flash->spi, cmd, cmd_len,
  162. buf + actual, 2);
  163. if (ret) {
  164. debug("SF: sst word program failed\n");
  165. break;
  166. }
  167. ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
  168. if (ret)
  169. break;
  170. cmd_len = 1;
  171. offset += 2;
  172. }
  173. if (!ret)
  174. ret = sst_disable_writing(flash);
  175. /* If there is a single trailing byte, write it out */
  176. if (!ret && actual != len)
  177. ret = sst_byte_write(flash, offset, buf + actual);
  178. done:
  179. debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
  180. ret ? "failure" : "success", len, offset - actual);
  181. spi_release_bus(flash->spi);
  182. return ret;
  183. }
  184. static int sst_erase(struct spi_flash *flash, u32 offset, size_t len)
  185. {
  186. return spi_flash_cmd_erase(flash, CMD_SST_SE, offset, len);
  187. }
  188. static int
  189. sst_unlock(struct spi_flash *flash)
  190. {
  191. int ret;
  192. u8 cmd, status;
  193. ret = sst_enable_writing(flash);
  194. if (ret)
  195. return ret;
  196. cmd = CMD_WRITE_STATUS;
  197. status = 0;
  198. ret = spi_flash_cmd_write(flash->spi, &cmd, 1, &status, 1);
  199. if (ret)
  200. debug("SF: Unable to set status byte\n");
  201. debug("SF: sst: status = %x\n", spi_w8r8(flash->spi, CMD_READ_STATUS));
  202. return ret;
  203. }
  204. struct spi_flash *
  205. spi_flash_probe_sst(struct spi_slave *spi, u8 *idcode)
  206. {
  207. const struct sst_spi_flash_params *params;
  208. struct sst_spi_flash *stm;
  209. size_t i;
  210. for (i = 0; i < ARRAY_SIZE(sst_spi_flash_table); ++i) {
  211. params = &sst_spi_flash_table[i];
  212. if (params->idcode1 == idcode[2])
  213. break;
  214. }
  215. if (i == ARRAY_SIZE(sst_spi_flash_table)) {
  216. debug("SF: Unsupported SST ID %02x\n", idcode[1]);
  217. return NULL;
  218. }
  219. stm = malloc(sizeof(*stm));
  220. if (!stm) {
  221. debug("SF: Failed to allocate memory\n");
  222. return NULL;
  223. }
  224. stm->params = params;
  225. stm->flash.spi = spi;
  226. stm->flash.name = params->name;
  227. if (stm->params->flags & SST_FEAT_WP)
  228. stm->flash.write = sst_write_wp;
  229. else
  230. stm->flash.write = spi_flash_cmd_write_multi;
  231. stm->flash.erase = sst_erase;
  232. stm->flash.read = spi_flash_cmd_read_fast;
  233. stm->flash.page_size = SST_PAGE_SIZE;
  234. stm->flash.sector_size = SST_SECTOR_SIZE;
  235. stm->flash.size = stm->flash.sector_size * params->nr_sectors;
  236. /* Flash powers up read-only, so clear BP# bits */
  237. sst_unlock(&stm->flash);
  238. return &stm->flash;
  239. }