sst.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 SST_SR_WIP (1 << 0) /* Write-in-Progress */
  21. #define SST_SR_WEL (1 << 1) /* Write enable */
  22. #define SST_SR_BP0 (1 << 2) /* Block Protection 0 */
  23. #define SST_SR_BP1 (1 << 3) /* Block Protection 1 */
  24. #define SST_SR_BP2 (1 << 4) /* Block Protection 2 */
  25. #define SST_SR_AAI (1 << 6) /* Addressing mode */
  26. #define SST_SR_BPL (1 << 7) /* BP bits lock */
  27. #define SST_FEAT_WP (1 << 0) /* Supports AAI word program */
  28. #define SST_FEAT_MBP (1 << 1) /* Supports multibyte program */
  29. struct sst_spi_flash_params {
  30. u8 idcode1;
  31. u8 flags;
  32. u16 nr_sectors;
  33. const char *name;
  34. };
  35. struct sst_spi_flash {
  36. struct spi_flash flash;
  37. const struct sst_spi_flash_params *params;
  38. };
  39. static const struct sst_spi_flash_params sst_spi_flash_table[] = {
  40. {
  41. .idcode1 = 0x8d,
  42. .flags = SST_FEAT_WP,
  43. .nr_sectors = 128,
  44. .name = "SST25VF040B",
  45. },{
  46. .idcode1 = 0x8e,
  47. .flags = SST_FEAT_WP,
  48. .nr_sectors = 256,
  49. .name = "SST25VF080B",
  50. },{
  51. .idcode1 = 0x41,
  52. .flags = SST_FEAT_WP,
  53. .nr_sectors = 512,
  54. .name = "SST25VF016B",
  55. },{
  56. .idcode1 = 0x4a,
  57. .flags = SST_FEAT_WP,
  58. .nr_sectors = 1024,
  59. .name = "SST25VF032B",
  60. },{
  61. .idcode1 = 0x4b,
  62. .flags = SST_FEAT_MBP,
  63. .nr_sectors = 2048,
  64. .name = "SST25VF064C",
  65. },{
  66. .idcode1 = 0x01,
  67. .flags = SST_FEAT_WP,
  68. .nr_sectors = 16,
  69. .name = "SST25WF512",
  70. },{
  71. .idcode1 = 0x02,
  72. .flags = SST_FEAT_WP,
  73. .nr_sectors = 32,
  74. .name = "SST25WF010",
  75. },{
  76. .idcode1 = 0x03,
  77. .flags = SST_FEAT_WP,
  78. .nr_sectors = 64,
  79. .name = "SST25WF020",
  80. },{
  81. .idcode1 = 0x04,
  82. .flags = SST_FEAT_WP,
  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_write_enable(flash);
  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_write_disable(flash);
  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_READ_STATUS), 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_wp(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_READ_STATUS), 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
  178. sst_unlock(struct spi_flash *flash)
  179. {
  180. int ret;
  181. u8 cmd, status;
  182. ret = sst_enable_writing(flash);
  183. if (ret)
  184. return ret;
  185. cmd = CMD_WRITE_STATUS;
  186. status = 0;
  187. ret = spi_flash_cmd_write(flash->spi, &cmd, 1, &status, 1);
  188. if (ret)
  189. debug("SF: Unable to set status byte\n");
  190. debug("SF: sst: status = %x\n", spi_w8r8(flash->spi, CMD_READ_STATUS));
  191. return ret;
  192. }
  193. struct spi_flash *
  194. spi_flash_probe_sst(struct spi_slave *spi, u8 *idcode)
  195. {
  196. const struct sst_spi_flash_params *params;
  197. struct sst_spi_flash *stm;
  198. size_t i;
  199. for (i = 0; i < ARRAY_SIZE(sst_spi_flash_table); ++i) {
  200. params = &sst_spi_flash_table[i];
  201. if (params->idcode1 == idcode[2])
  202. break;
  203. }
  204. if (i == ARRAY_SIZE(sst_spi_flash_table)) {
  205. debug("SF: Unsupported SST ID %02x\n", idcode[1]);
  206. return NULL;
  207. }
  208. stm = malloc(sizeof(*stm));
  209. if (!stm) {
  210. debug("SF: Failed to allocate memory\n");
  211. return NULL;
  212. }
  213. stm->params = params;
  214. stm->flash.spi = spi;
  215. stm->flash.name = params->name;
  216. if (stm->params->flags & SST_FEAT_WP)
  217. stm->flash.write = sst_write_wp;
  218. else
  219. stm->flash.write = spi_flash_cmd_write_multi;
  220. stm->flash.erase = spi_flash_cmd_erase;
  221. stm->flash.read = spi_flash_cmd_read_fast;
  222. stm->flash.page_size = 256;
  223. stm->flash.sector_size = 4096;
  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. }