sst.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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_byte_write(struct spi_flash *flash, u32 offset, const void *buf)
  89. {
  90. int ret;
  91. u8 cmd[4] = {
  92. CMD_SST_BP,
  93. offset >> 16,
  94. offset >> 8,
  95. offset,
  96. };
  97. debug("BP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
  98. spi_w8r8(flash->spi, CMD_READ_STATUS), buf, cmd[0], offset);
  99. ret = spi_flash_cmd_write_enable(flash);
  100. if (ret)
  101. return ret;
  102. ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), buf, 1);
  103. if (ret)
  104. return ret;
  105. return spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
  106. }
  107. static int
  108. sst_write_wp(struct spi_flash *flash, u32 offset, size_t len, const void *buf)
  109. {
  110. size_t actual, cmd_len;
  111. int ret;
  112. u8 cmd[4];
  113. ret = spi_claim_bus(flash->spi);
  114. if (ret) {
  115. debug("SF: Unable to claim SPI bus\n");
  116. return ret;
  117. }
  118. /* If the data is not word aligned, write out leading single byte */
  119. actual = offset % 2;
  120. if (actual) {
  121. ret = sst_byte_write(flash, offset, buf);
  122. if (ret)
  123. goto done;
  124. }
  125. offset += actual;
  126. ret = spi_flash_cmd_write_enable(flash);
  127. if (ret)
  128. goto done;
  129. cmd_len = 4;
  130. cmd[0] = CMD_SST_AAI_WP;
  131. cmd[1] = offset >> 16;
  132. cmd[2] = offset >> 8;
  133. cmd[3] = offset;
  134. for (; actual < len - 1; actual += 2) {
  135. debug("WP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
  136. spi_w8r8(flash->spi, CMD_READ_STATUS), buf + actual, cmd[0],
  137. offset);
  138. ret = spi_flash_cmd_write(flash->spi, cmd, cmd_len,
  139. buf + actual, 2);
  140. if (ret) {
  141. debug("SF: sst word program failed\n");
  142. break;
  143. }
  144. ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
  145. if (ret)
  146. break;
  147. cmd_len = 1;
  148. offset += 2;
  149. }
  150. if (!ret)
  151. ret = spi_flash_cmd_write_disable(flash);
  152. /* If there is a single trailing byte, write it out */
  153. if (!ret && actual != len)
  154. ret = sst_byte_write(flash, offset, buf + actual);
  155. done:
  156. debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
  157. ret ? "failure" : "success", len, offset - actual);
  158. spi_release_bus(flash->spi);
  159. return ret;
  160. }
  161. struct spi_flash *
  162. spi_flash_probe_sst(struct spi_slave *spi, u8 *idcode)
  163. {
  164. const struct sst_spi_flash_params *params;
  165. struct sst_spi_flash *stm;
  166. size_t i;
  167. for (i = 0; i < ARRAY_SIZE(sst_spi_flash_table); ++i) {
  168. params = &sst_spi_flash_table[i];
  169. if (params->idcode1 == idcode[2])
  170. break;
  171. }
  172. if (i == ARRAY_SIZE(sst_spi_flash_table)) {
  173. debug("SF: Unsupported SST ID %02x\n", idcode[1]);
  174. return NULL;
  175. }
  176. stm = spi_flash_alloc(struct sst_spi_flash, spi, params->name);
  177. if (!stm) {
  178. debug("SF: Failed to allocate memory\n");
  179. return NULL;
  180. }
  181. stm->params = params;
  182. if (stm->params->flags & SST_FEAT_WP)
  183. stm->flash.write = sst_write_wp;
  184. stm->flash.page_size = 256;
  185. stm->flash.sector_size = 4096;
  186. stm->flash.size = stm->flash.sector_size * params->nr_sectors;
  187. /* Flash powers up read-only, so clear BP# bits */
  188. spi_flash_cmd_write_status(&stm->flash, 0);
  189. return &stm->flash;
  190. }