spansion.c 8.6 KB

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