stmicro.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * (C) Copyright 2000-2002
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * Copyright 2008, Network Appliance Inc.
  6. * Jason McMullan <mcmullan@netapp.com>
  7. *
  8. * Copyright (C) 2004-2007 Freescale Semiconductor, Inc.
  9. * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
  10. *
  11. * See file CREDITS for list of people who contributed to this
  12. * project.
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License as
  16. * published by the Free Software Foundation; either version 2 of
  17. * the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  27. * MA 02111-1307 USA
  28. */
  29. #include <common.h>
  30. #include <malloc.h>
  31. #include <spi_flash.h>
  32. #include "spi_flash_internal.h"
  33. /* M25Pxx-specific commands */
  34. #define CMD_M25PXX_WREN 0x06 /* Write Enable */
  35. #define CMD_M25PXX_WRDI 0x04 /* Write Disable */
  36. #define CMD_M25PXX_RDSR 0x05 /* Read Status Register */
  37. #define CMD_M25PXX_WRSR 0x01 /* Write Status Register */
  38. #define CMD_M25PXX_READ 0x03 /* Read Data Bytes */
  39. #define CMD_M25PXX_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */
  40. #define CMD_M25PXX_PP 0x02 /* Page Program */
  41. #define CMD_M25PXX_SE 0xd8 /* Sector Erase */
  42. #define CMD_M25PXX_BE 0xc7 /* Bulk Erase */
  43. #define CMD_M25PXX_DP 0xb9 /* Deep Power-down */
  44. #define CMD_M25PXX_RES 0xab /* Release from DP, and Read Signature */
  45. #define STM_ID_M25P10 0x11
  46. #define STM_ID_M25P16 0x15
  47. #define STM_ID_M25P20 0x12
  48. #define STM_ID_M25P32 0x16
  49. #define STM_ID_M25P40 0x13
  50. #define STM_ID_M25P64 0x17
  51. #define STM_ID_M25P80 0x14
  52. #define STM_ID_M25P128 0x18
  53. #define STMICRO_SR_WIP (1 << 0) /* Write-in-Progress */
  54. struct stmicro_spi_flash_params {
  55. u8 idcode1;
  56. u16 page_size;
  57. u16 pages_per_sector;
  58. u16 nr_sectors;
  59. const char *name;
  60. };
  61. /* spi_flash needs to be first so upper layers can free() it */
  62. struct stmicro_spi_flash {
  63. struct spi_flash flash;
  64. const struct stmicro_spi_flash_params *params;
  65. };
  66. static inline struct stmicro_spi_flash *to_stmicro_spi_flash(struct spi_flash
  67. *flash)
  68. {
  69. return container_of(flash, struct stmicro_spi_flash, flash);
  70. }
  71. static const struct stmicro_spi_flash_params stmicro_spi_flash_table[] = {
  72. {
  73. .idcode1 = STM_ID_M25P10,
  74. .page_size = 256,
  75. .pages_per_sector = 128,
  76. .nr_sectors = 4,
  77. .name = "M25P10",
  78. },
  79. {
  80. .idcode1 = STM_ID_M25P16,
  81. .page_size = 256,
  82. .pages_per_sector = 256,
  83. .nr_sectors = 32,
  84. .name = "M25P16",
  85. },
  86. {
  87. .idcode1 = STM_ID_M25P20,
  88. .page_size = 256,
  89. .pages_per_sector = 256,
  90. .nr_sectors = 4,
  91. .name = "M25P20",
  92. },
  93. {
  94. .idcode1 = STM_ID_M25P32,
  95. .page_size = 256,
  96. .pages_per_sector = 256,
  97. .nr_sectors = 64,
  98. .name = "M25P32",
  99. },
  100. {
  101. .idcode1 = STM_ID_M25P40,
  102. .page_size = 256,
  103. .pages_per_sector = 256,
  104. .nr_sectors = 8,
  105. .name = "M25P40",
  106. },
  107. {
  108. .idcode1 = STM_ID_M25P64,
  109. .page_size = 256,
  110. .pages_per_sector = 256,
  111. .nr_sectors = 128,
  112. .name = "M25P64",
  113. },
  114. {
  115. .idcode1 = STM_ID_M25P80,
  116. .page_size = 256,
  117. .pages_per_sector = 256,
  118. .nr_sectors = 16,
  119. .name = "M25P80",
  120. },
  121. {
  122. .idcode1 = STM_ID_M25P128,
  123. .page_size = 256,
  124. .pages_per_sector = 1024,
  125. .nr_sectors = 64,
  126. .name = "M25P128",
  127. },
  128. };
  129. static int stmicro_wait_ready(struct spi_flash *flash, unsigned long timeout)
  130. {
  131. struct spi_slave *spi = flash->spi;
  132. unsigned long timebase;
  133. int ret;
  134. u8 cmd = CMD_M25PXX_RDSR;
  135. u8 status;
  136. ret = spi_xfer(spi, 8, &cmd, NULL, SPI_XFER_BEGIN);
  137. if (ret) {
  138. debug("SF: Failed to send command %02x: %d\n", cmd, ret);
  139. return ret;
  140. }
  141. timebase = get_timer(0);
  142. do {
  143. ret = spi_xfer(spi, 8, NULL, &status, 0);
  144. if (ret)
  145. return -1;
  146. if ((status & STMICRO_SR_WIP) == 0)
  147. break;
  148. } while (get_timer(timebase) < timeout);
  149. spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END);
  150. if ((status & STMICRO_SR_WIP) == 0)
  151. return 0;
  152. /* Timed out */
  153. return -1;
  154. }
  155. static int stmicro_read_fast(struct spi_flash *flash,
  156. u32 offset, size_t len, void *buf)
  157. {
  158. struct stmicro_spi_flash *stm = to_stmicro_spi_flash(flash);
  159. unsigned long page_addr;
  160. unsigned long page_size;
  161. u8 cmd[5];
  162. page_size = stm->params->page_size;
  163. page_addr = offset / page_size;
  164. cmd[0] = CMD_READ_ARRAY_FAST;
  165. cmd[1] = page_addr >> 8;
  166. cmd[2] = page_addr;
  167. cmd[3] = offset % page_size;
  168. cmd[4] = 0x00;
  169. return spi_flash_read_common(flash, cmd, sizeof(cmd), buf, len);
  170. }
  171. static int stmicro_write(struct spi_flash *flash,
  172. u32 offset, size_t len, const void *buf)
  173. {
  174. struct stmicro_spi_flash *stm = to_stmicro_spi_flash(flash);
  175. unsigned long page_addr;
  176. unsigned long byte_addr;
  177. unsigned long page_size;
  178. size_t chunk_len;
  179. size_t actual;
  180. int ret;
  181. u8 cmd[4];
  182. page_size = stm->params->page_size;
  183. page_addr = offset / page_size;
  184. byte_addr = offset % page_size;
  185. ret = spi_claim_bus(flash->spi);
  186. if (ret) {
  187. debug("SF: Unable to claim SPI bus\n");
  188. return ret;
  189. }
  190. ret = 0;
  191. for (actual = 0; actual < len; actual += chunk_len) {
  192. chunk_len = min(len - actual, page_size - byte_addr);
  193. cmd[0] = CMD_M25PXX_PP;
  194. cmd[1] = page_addr >> 8;
  195. cmd[2] = page_addr;
  196. cmd[3] = byte_addr;
  197. debug
  198. ("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %d\n",
  199. buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
  200. ret = spi_flash_cmd(flash->spi, CMD_M25PXX_WREN, NULL, 0);
  201. if (ret < 0) {
  202. debug("SF: Enabling Write failed\n");
  203. break;
  204. }
  205. ret = spi_flash_cmd_write(flash->spi, cmd, 4,
  206. buf + actual, chunk_len);
  207. if (ret < 0) {
  208. debug("SF: STMicro Page Program failed\n");
  209. break;
  210. }
  211. ret = stmicro_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
  212. if (ret < 0) {
  213. debug("SF: STMicro page programming timed out\n");
  214. break;
  215. }
  216. page_addr++;
  217. byte_addr = 0;
  218. }
  219. debug("SF: STMicro: Successfully programmed %u bytes @ 0x%x\n",
  220. len, offset);
  221. spi_release_bus(flash->spi);
  222. return ret;
  223. }
  224. int stmicro_erase(struct spi_flash *flash, u32 offset, size_t len)
  225. {
  226. struct stmicro_spi_flash *stm = to_stmicro_spi_flash(flash);
  227. unsigned long sector_size;
  228. size_t actual;
  229. int ret;
  230. u8 cmd[4];
  231. /*
  232. * This function currently uses sector erase only.
  233. * probably speed things up by using bulk erase
  234. * when possible.
  235. */
  236. sector_size = stm->params->page_size * stm->params->pages_per_sector;
  237. if (offset % sector_size || len % sector_size) {
  238. debug("SF: Erase offset/length not multiple of sector size\n");
  239. return -1;
  240. }
  241. len /= sector_size;
  242. cmd[0] = CMD_M25PXX_SE;
  243. cmd[2] = 0x00;
  244. cmd[3] = 0x00;
  245. ret = spi_claim_bus(flash->spi);
  246. if (ret) {
  247. debug("SF: Unable to claim SPI bus\n");
  248. return ret;
  249. }
  250. ret = 0;
  251. for (actual = 0; actual < len; actual++) {
  252. cmd[1] = offset >> 16;
  253. offset += sector_size;
  254. ret = spi_flash_cmd(flash->spi, CMD_M25PXX_WREN, NULL, 0);
  255. if (ret < 0) {
  256. debug("SF: Enabling Write failed\n");
  257. break;
  258. }
  259. ret = spi_flash_cmd_write(flash->spi, cmd, 4, NULL, 0);
  260. if (ret < 0) {
  261. debug("SF: STMicro page erase failed\n");
  262. break;
  263. }
  264. ret = stmicro_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
  265. if (ret < 0) {
  266. debug("SF: STMicro page erase timed out\n");
  267. break;
  268. }
  269. }
  270. debug("SF: STMicro: Successfully erased %u bytes @ 0x%x\n",
  271. len * sector_size, offset);
  272. spi_release_bus(flash->spi);
  273. return ret;
  274. }
  275. struct spi_flash *spi_flash_probe_stmicro(struct spi_slave *spi, u8 * idcode)
  276. {
  277. const struct stmicro_spi_flash_params *params;
  278. struct stmicro_spi_flash *stm;
  279. unsigned int i;
  280. if (idcode[0] == 0xff) {
  281. i = spi_flash_cmd(spi, CMD_M25PXX_RES,
  282. idcode, 4);
  283. if (i)
  284. return NULL;
  285. if ((idcode[3] & 0xf0) == 0x10) {
  286. idcode[0] = 0x20;
  287. idcode[1] = 0x20;
  288. idcode[2] = idcode[3] + 1;
  289. } else
  290. return NULL;
  291. }
  292. for (i = 0; i < ARRAY_SIZE(stmicro_spi_flash_table); i++) {
  293. params = &stmicro_spi_flash_table[i];
  294. if (params->idcode1 == idcode[2]) {
  295. break;
  296. }
  297. }
  298. if (i == ARRAY_SIZE(stmicro_spi_flash_table)) {
  299. debug("SF: Unsupported STMicro ID %02x\n", idcode[1]);
  300. return NULL;
  301. }
  302. stm = malloc(sizeof(struct stmicro_spi_flash));
  303. if (!stm) {
  304. debug("SF: Failed to allocate memory\n");
  305. return NULL;
  306. }
  307. stm->params = params;
  308. stm->flash.spi = spi;
  309. stm->flash.name = params->name;
  310. stm->flash.write = stmicro_write;
  311. stm->flash.erase = stmicro_erase;
  312. stm->flash.read = stmicro_read_fast;
  313. stm->flash.size = params->page_size * params->pages_per_sector
  314. * params->nr_sectors;
  315. printf("SF: Detected %s with page size %u, total ",
  316. params->name, params->page_size);
  317. print_size(stm->flash.size, "\n");
  318. return &stm->flash;
  319. }