spansion.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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_read_fast(struct spi_flash *flash,
  128. u32 offset, size_t len, void *buf)
  129. {
  130. struct spansion_spi_flash *spsn = to_spansion_spi_flash(flash);
  131. unsigned long page_addr;
  132. unsigned long page_size;
  133. u8 cmd[5];
  134. page_size = spsn->params->page_size;
  135. page_addr = offset / page_size;
  136. cmd[0] = CMD_READ_ARRAY_FAST;
  137. cmd[1] = page_addr >> 8;
  138. cmd[2] = page_addr;
  139. cmd[3] = offset % page_size;
  140. cmd[4] = 0x00;
  141. debug
  142. ("READ: 0x%x => cmd = { 0x%02x 0x%02x%02x%02x%02x } len = 0x%x\n",
  143. offset, cmd[0], cmd[1], cmd[2], cmd[3], cmd[4], len);
  144. return spi_flash_read_common(flash, cmd, sizeof(cmd), buf, len);
  145. }
  146. static int spansion_write(struct spi_flash *flash,
  147. u32 offset, size_t len, const void *buf)
  148. {
  149. struct spansion_spi_flash *spsn = to_spansion_spi_flash(flash);
  150. unsigned long page_addr;
  151. unsigned long byte_addr;
  152. unsigned long page_size;
  153. size_t chunk_len;
  154. size_t actual;
  155. int ret;
  156. u8 cmd[4];
  157. page_size = spsn->params->page_size;
  158. page_addr = offset / page_size;
  159. byte_addr = offset % page_size;
  160. ret = spi_claim_bus(flash->spi);
  161. if (ret) {
  162. debug("SF: Unable to claim SPI bus\n");
  163. return ret;
  164. }
  165. ret = 0;
  166. for (actual = 0; actual < len; actual += chunk_len) {
  167. chunk_len = min(len - actual, page_size - byte_addr);
  168. cmd[0] = CMD_S25FLXX_PP;
  169. cmd[1] = page_addr >> 8;
  170. cmd[2] = page_addr;
  171. cmd[3] = byte_addr;
  172. debug
  173. ("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %d\n",
  174. buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
  175. ret = spi_flash_cmd(flash->spi, CMD_S25FLXX_WREN, NULL, 0);
  176. if (ret < 0) {
  177. debug("SF: Enabling Write failed\n");
  178. break;
  179. }
  180. ret = spi_flash_cmd_write(flash->spi, cmd, 4,
  181. buf + actual, chunk_len);
  182. if (ret < 0) {
  183. debug("SF: SPANSION Page Program failed\n");
  184. break;
  185. }
  186. ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
  187. if (ret)
  188. break;
  189. page_addr++;
  190. byte_addr = 0;
  191. }
  192. debug("SF: SPANSION: Successfully programmed %u bytes @ 0x%x\n",
  193. len, offset);
  194. spi_release_bus(flash->spi);
  195. return ret;
  196. }
  197. int spansion_erase(struct spi_flash *flash, u32 offset, size_t len)
  198. {
  199. struct spansion_spi_flash *spsn = to_spansion_spi_flash(flash);
  200. unsigned long sector_size;
  201. size_t actual;
  202. int ret;
  203. u8 cmd[4];
  204. /*
  205. * This function currently uses sector erase only.
  206. * probably speed things up by using bulk erase
  207. * when possible.
  208. */
  209. sector_size = spsn->params->page_size * spsn->params->pages_per_sector;
  210. if (offset % sector_size || len % sector_size) {
  211. debug("SF: Erase offset/length not multiple of sector size\n");
  212. return -1;
  213. }
  214. cmd[0] = CMD_S25FLXX_SE;
  215. cmd[2] = 0x00;
  216. cmd[3] = 0x00;
  217. ret = spi_claim_bus(flash->spi);
  218. if (ret) {
  219. debug("SF: Unable to claim SPI bus\n");
  220. return ret;
  221. }
  222. ret = 0;
  223. for (actual = 0; actual < len; actual += sector_size) {
  224. cmd[1] = (offset + actual) >> 16;
  225. ret = spi_flash_cmd(flash->spi, CMD_S25FLXX_WREN, NULL, 0);
  226. if (ret < 0) {
  227. debug("SF: Enabling Write failed\n");
  228. break;
  229. }
  230. ret = spi_flash_cmd_write(flash->spi, cmd, 4, NULL, 0);
  231. if (ret < 0) {
  232. debug("SF: SPANSION page erase failed\n");
  233. break;
  234. }
  235. ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
  236. if (ret)
  237. break;
  238. }
  239. debug("SF: SPANSION: Successfully erased %u bytes @ 0x%x\n",
  240. len, offset);
  241. spi_release_bus(flash->spi);
  242. return ret;
  243. }
  244. struct spi_flash *spi_flash_probe_spansion(struct spi_slave *spi, u8 *idcode)
  245. {
  246. const struct spansion_spi_flash_params *params;
  247. struct spansion_spi_flash *spsn;
  248. unsigned int i;
  249. unsigned short jedec, ext_jedec;
  250. jedec = idcode[1] << 8 | idcode[2];
  251. ext_jedec = idcode[3] << 8 | idcode[4];
  252. for (i = 0; i < ARRAY_SIZE(spansion_spi_flash_table); i++) {
  253. params = &spansion_spi_flash_table[i];
  254. if (params->idcode1 == jedec) {
  255. if (params->idcode2 == ext_jedec)
  256. break;
  257. }
  258. }
  259. if (i == ARRAY_SIZE(spansion_spi_flash_table)) {
  260. debug("SF: Unsupported SPANSION ID %04x %04x\n", jedec, ext_jedec);
  261. return NULL;
  262. }
  263. spsn = malloc(sizeof(struct spansion_spi_flash));
  264. if (!spsn) {
  265. debug("SF: Failed to allocate memory\n");
  266. return NULL;
  267. }
  268. spsn->params = params;
  269. spsn->flash.spi = spi;
  270. spsn->flash.name = params->name;
  271. spsn->flash.write = spansion_write;
  272. spsn->flash.erase = spansion_erase;
  273. spsn->flash.read = spansion_read_fast;
  274. spsn->flash.size = params->page_size * params->pages_per_sector
  275. * params->nr_sectors;
  276. printf("SF: Detected %s with page size %u, total ",
  277. params->name, params->page_size);
  278. print_size(spsn->flash.size, "\n");
  279. return &spsn->flash;
  280. }