spansion.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright (C) 2009 Freescale Semiconductor, Inc.
  3. *
  4. * Author: Mingkai Hu (Mingkai.hu@freescale.com)
  5. * Based on stmicro.c by Wolfgang Denk (wd@denx.de),
  6. * TsiChung Liew (Tsi-Chung.Liew@freescale.com),
  7. * and Jason McMullan (mcmullan@netapp.com)
  8. *
  9. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. */
  27. #include <common.h>
  28. #include <malloc.h>
  29. #include <spi_flash.h>
  30. #include "spi_flash_internal.h"
  31. /* S25FLxx-specific commands */
  32. #define CMD_S25FLXX_READ 0x03 /* Read Data Bytes */
  33. #define CMD_S25FLXX_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */
  34. #define CMD_S25FLXX_READID 0x90 /* Read Manufacture ID and Device ID */
  35. #define CMD_S25FLXX_WREN 0x06 /* Write Enable */
  36. #define CMD_S25FLXX_WRDI 0x04 /* Write Disable */
  37. #define CMD_S25FLXX_RDSR 0x05 /* Read Status Register */
  38. #define CMD_S25FLXX_WRSR 0x01 /* Write Status Register */
  39. #define CMD_S25FLXX_PP 0x02 /* Page Program */
  40. #define CMD_S25FLXX_SE 0xd8 /* Sector Erase */
  41. #define CMD_S25FLXX_BE 0xc7 /* Bulk Erase */
  42. #define CMD_S25FLXX_DP 0xb9 /* Deep Power-down */
  43. #define CMD_S25FLXX_RES 0xab /* Release from DP, and Read Signature */
  44. #define SPSN_ID_S25FL008A 0x0213
  45. #define SPSN_ID_S25FL016A 0x0214
  46. #define SPSN_ID_S25FL032A 0x0215
  47. #define SPSN_ID_S25FL064A 0x0216
  48. #define SPSN_ID_S25FL128P 0x2018
  49. #define SPSN_EXT_ID_S25FL128P_256KB 0x0300
  50. #define SPSN_EXT_ID_S25FL128P_64KB 0x0301
  51. #define SPSN_EXT_ID_S25FL032P 0x4d00
  52. struct spansion_spi_flash_params {
  53. u16 idcode1;
  54. u16 idcode2;
  55. u16 page_size;
  56. u16 pages_per_sector;
  57. u16 nr_sectors;
  58. const char *name;
  59. };
  60. struct spansion_spi_flash {
  61. struct spi_flash flash;
  62. const struct spansion_spi_flash_params *params;
  63. };
  64. static inline struct spansion_spi_flash *to_spansion_spi_flash(struct spi_flash
  65. *flash)
  66. {
  67. return container_of(flash, struct spansion_spi_flash, flash);
  68. }
  69. static const struct spansion_spi_flash_params spansion_spi_flash_table[] = {
  70. {
  71. .idcode1 = SPSN_ID_S25FL008A,
  72. .idcode2 = 0,
  73. .page_size = 256,
  74. .pages_per_sector = 256,
  75. .nr_sectors = 16,
  76. .name = "S25FL008A",
  77. },
  78. {
  79. .idcode1 = SPSN_ID_S25FL016A,
  80. .idcode2 = 0,
  81. .page_size = 256,
  82. .pages_per_sector = 256,
  83. .nr_sectors = 32,
  84. .name = "S25FL016A",
  85. },
  86. {
  87. .idcode1 = SPSN_ID_S25FL032A,
  88. .idcode2 = 0,
  89. .page_size = 256,
  90. .pages_per_sector = 256,
  91. .nr_sectors = 64,
  92. .name = "S25FL032A",
  93. },
  94. {
  95. .idcode1 = SPSN_ID_S25FL064A,
  96. .idcode2 = 0,
  97. .page_size = 256,
  98. .pages_per_sector = 256,
  99. .nr_sectors = 128,
  100. .name = "S25FL064A",
  101. },
  102. {
  103. .idcode1 = SPSN_ID_S25FL128P,
  104. .idcode2 = SPSN_EXT_ID_S25FL128P_64KB,
  105. .page_size = 256,
  106. .pages_per_sector = 256,
  107. .nr_sectors = 256,
  108. .name = "S25FL128P_64K",
  109. },
  110. {
  111. .idcode1 = SPSN_ID_S25FL128P,
  112. .idcode2 = SPSN_EXT_ID_S25FL128P_256KB,
  113. .page_size = 256,
  114. .pages_per_sector = 1024,
  115. .nr_sectors = 64,
  116. .name = "S25FL128P_256K",
  117. },
  118. {
  119. .idcode1 = SPSN_ID_S25FL032A,
  120. .idcode2 = SPSN_EXT_ID_S25FL032P,
  121. .page_size = 256,
  122. .pages_per_sector = 256,
  123. .nr_sectors = 64,
  124. .name = "S25FL032P",
  125. },
  126. };
  127. static int spansion_write(struct spi_flash *flash,
  128. u32 offset, size_t len, const void *buf)
  129. {
  130. struct spansion_spi_flash *spsn = to_spansion_spi_flash(flash);
  131. unsigned long page_addr;
  132. unsigned long byte_addr;
  133. unsigned long page_size;
  134. size_t chunk_len;
  135. size_t actual;
  136. int ret;
  137. u8 cmd[4];
  138. page_size = spsn->params->page_size;
  139. page_addr = offset / page_size;
  140. byte_addr = offset % page_size;
  141. ret = spi_claim_bus(flash->spi);
  142. if (ret) {
  143. debug("SF: Unable to claim SPI bus\n");
  144. return ret;
  145. }
  146. ret = 0;
  147. for (actual = 0; actual < len; actual += chunk_len) {
  148. chunk_len = min(len - actual, page_size - byte_addr);
  149. cmd[0] = CMD_S25FLXX_PP;
  150. cmd[1] = page_addr >> 8;
  151. cmd[2] = page_addr;
  152. cmd[3] = byte_addr;
  153. debug
  154. ("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %d\n",
  155. buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
  156. ret = spi_flash_cmd(flash->spi, CMD_S25FLXX_WREN, NULL, 0);
  157. if (ret < 0) {
  158. debug("SF: Enabling Write failed\n");
  159. break;
  160. }
  161. ret = spi_flash_cmd_write(flash->spi, cmd, 4,
  162. buf + actual, chunk_len);
  163. if (ret < 0) {
  164. debug("SF: SPANSION Page Program failed\n");
  165. break;
  166. }
  167. ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
  168. if (ret)
  169. break;
  170. page_addr++;
  171. byte_addr = 0;
  172. }
  173. debug("SF: SPANSION: Successfully programmed %u bytes @ 0x%x\n",
  174. len, offset);
  175. spi_release_bus(flash->spi);
  176. return ret;
  177. }
  178. static int spansion_erase(struct spi_flash *flash, u32 offset, size_t len)
  179. {
  180. return spi_flash_cmd_erase(flash, CMD_S25FLXX_SE, offset, len);
  181. }
  182. struct spi_flash *spi_flash_probe_spansion(struct spi_slave *spi, u8 *idcode)
  183. {
  184. const struct spansion_spi_flash_params *params;
  185. struct spansion_spi_flash *spsn;
  186. unsigned int i;
  187. unsigned short jedec, ext_jedec;
  188. jedec = idcode[1] << 8 | idcode[2];
  189. ext_jedec = idcode[3] << 8 | idcode[4];
  190. for (i = 0; i < ARRAY_SIZE(spansion_spi_flash_table); i++) {
  191. params = &spansion_spi_flash_table[i];
  192. if (params->idcode1 == jedec) {
  193. if (params->idcode2 == ext_jedec)
  194. break;
  195. }
  196. }
  197. if (i == ARRAY_SIZE(spansion_spi_flash_table)) {
  198. debug("SF: Unsupported SPANSION ID %04x %04x\n", jedec, ext_jedec);
  199. return NULL;
  200. }
  201. spsn = malloc(sizeof(struct spansion_spi_flash));
  202. if (!spsn) {
  203. debug("SF: Failed to allocate memory\n");
  204. return NULL;
  205. }
  206. spsn->params = params;
  207. spsn->flash.spi = spi;
  208. spsn->flash.name = params->name;
  209. spsn->flash.write = spansion_write;
  210. spsn->flash.erase = spansion_erase;
  211. spsn->flash.read = spi_flash_cmd_read_fast;
  212. spsn->flash.sector_size = params->page_size * params->pages_per_sector;
  213. spsn->flash.size = spsn->flash.sector_size * params->nr_sectors;
  214. return &spsn->flash;
  215. }