stmicro.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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_M25P16 0x15
  46. #define STM_ID_M25P20 0x12
  47. #define STM_ID_M25P32 0x16
  48. #define STM_ID_M25P40 0x13
  49. #define STM_ID_M25P64 0x17
  50. #define STM_ID_M25P80 0x14
  51. #define STM_ID_M25P128 0x18
  52. #define STMICRO_SR_WIP (1 << 0) /* Write-in-Progress */
  53. struct stmicro_spi_flash_params {
  54. u8 idcode1;
  55. u16 page_size;
  56. u16 pages_per_sector;
  57. u16 nr_sectors;
  58. const char *name;
  59. };
  60. struct stmicro_spi_flash {
  61. const struct stmicro_spi_flash_params *params;
  62. struct spi_flash flash;
  63. };
  64. static inline struct stmicro_spi_flash *to_stmicro_spi_flash(struct spi_flash
  65. *flash)
  66. {
  67. return container_of(flash, struct stmicro_spi_flash, flash);
  68. }
  69. static const struct stmicro_spi_flash_params stmicro_spi_flash_table[] = {
  70. {
  71. .idcode1 = STM_ID_M25P16,
  72. .page_size = 256,
  73. .pages_per_sector = 256,
  74. .nr_sectors = 32,
  75. .name = "M25P16",
  76. },
  77. {
  78. .idcode1 = STM_ID_M25P20,
  79. .page_size = 256,
  80. .pages_per_sector = 256,
  81. .nr_sectors = 4,
  82. .name = "M25P20",
  83. },
  84. {
  85. .idcode1 = STM_ID_M25P32,
  86. .page_size = 256,
  87. .pages_per_sector = 256,
  88. .nr_sectors = 64,
  89. .name = "M25P32",
  90. },
  91. {
  92. .idcode1 = STM_ID_M25P40,
  93. .page_size = 256,
  94. .pages_per_sector = 256,
  95. .nr_sectors = 8,
  96. .name = "M25P40",
  97. },
  98. {
  99. .idcode1 = STM_ID_M25P64,
  100. .page_size = 256,
  101. .pages_per_sector = 256,
  102. .nr_sectors = 128,
  103. .name = "M25P64",
  104. },
  105. {
  106. .idcode1 = STM_ID_M25P80,
  107. .page_size = 256,
  108. .pages_per_sector = 256,
  109. .nr_sectors = 16,
  110. .name = "M25P80",
  111. },
  112. {
  113. .idcode1 = STM_ID_M25P128,
  114. .page_size = 256,
  115. .pages_per_sector = 1024,
  116. .nr_sectors = 64,
  117. .name = "M25P128",
  118. },
  119. };
  120. static int stmicro_wait_ready(struct spi_flash *flash, unsigned long timeout)
  121. {
  122. struct spi_slave *spi = flash->spi;
  123. unsigned long timebase;
  124. int ret;
  125. u8 status;
  126. u8 cmd[4] = { CMD_M25PXX_RDSR, 0xff, 0xff, 0xff };
  127. ret = spi_xfer(spi, 32, &cmd[0], NULL, SPI_XFER_BEGIN);
  128. if (ret) {
  129. debug("SF: Failed to send command %02x: %d\n", cmd[0], ret);
  130. return ret;
  131. }
  132. timebase = get_timer(0);
  133. do {
  134. ret = spi_xfer(spi, 8, NULL, &status, 0);
  135. if (ret)
  136. return -1;
  137. if ((status & STMICRO_SR_WIP) == 0)
  138. break;
  139. } while (get_timer(timebase) < timeout);
  140. spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END);
  141. if ((status & STMICRO_SR_WIP) == 0)
  142. return 0;
  143. /* Timed out */
  144. return -1;
  145. }
  146. static int stmicro_read_fast(struct spi_flash *flash,
  147. u32 offset, size_t len, void *buf)
  148. {
  149. struct stmicro_spi_flash *stm = to_stmicro_spi_flash(flash);
  150. unsigned long page_addr;
  151. unsigned long page_size;
  152. u8 cmd[5];
  153. page_size = stm->params->page_size;
  154. page_addr = offset / page_size;
  155. cmd[0] = CMD_READ_ARRAY_FAST;
  156. cmd[1] = page_addr >> 8;
  157. cmd[2] = page_addr;
  158. cmd[3] = offset % page_size;
  159. cmd[4] = 0x00;
  160. return spi_flash_read_common(flash, cmd, sizeof(cmd), buf, len);
  161. }
  162. static int stmicro_write(struct spi_flash *flash,
  163. u32 offset, size_t len, const void *buf)
  164. {
  165. struct stmicro_spi_flash *stm = to_stmicro_spi_flash(flash);
  166. unsigned long page_addr;
  167. unsigned long byte_addr;
  168. unsigned long page_size;
  169. size_t chunk_len;
  170. size_t actual;
  171. int ret;
  172. u8 cmd[4];
  173. page_size = stm->params->page_size;
  174. page_addr = offset / page_size;
  175. byte_addr = offset % page_size;
  176. ret = spi_claim_bus(flash->spi);
  177. if (ret) {
  178. debug("SF: Unable to claim SPI bus\n");
  179. return ret;
  180. }
  181. ret = 0;
  182. for (actual = 0; actual < len; actual += chunk_len) {
  183. chunk_len = min(len - actual, page_size - byte_addr);
  184. cmd[0] = CMD_M25PXX_PP;
  185. cmd[1] = page_addr >> 8;
  186. cmd[2] = page_addr;
  187. cmd[3] = byte_addr;
  188. debug
  189. ("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %d\n",
  190. buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
  191. ret = spi_flash_cmd(flash->spi, CMD_M25PXX_WREN, NULL, 0);
  192. if (ret < 0) {
  193. debug("SF: Enabling Write failed\n");
  194. break;
  195. }
  196. ret = spi_flash_cmd_write(flash->spi, cmd, 4,
  197. buf + actual, chunk_len);
  198. if (ret < 0) {
  199. debug("SF: STMicro Page Program failed\n");
  200. break;
  201. }
  202. ret = stmicro_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
  203. if (ret < 0) {
  204. debug("SF: STMicro page programming timed out\n");
  205. break;
  206. }
  207. page_addr++;
  208. byte_addr = 0;
  209. }
  210. debug("SF: STMicro: Successfully programmed %u bytes @ 0x%x\n",
  211. len, offset);
  212. spi_release_bus(flash->spi);
  213. return ret;
  214. }
  215. int stmicro_erase(struct spi_flash *flash, u32 offset, size_t len)
  216. {
  217. struct stmicro_spi_flash *stm = to_stmicro_spi_flash(flash);
  218. unsigned long sector_size;
  219. size_t actual;
  220. int ret;
  221. u8 cmd[4];
  222. /*
  223. * This function currently uses sector erase only.
  224. * probably speed things up by using bulk erase
  225. * when possible.
  226. */
  227. sector_size = stm->params->page_size * stm->params->pages_per_sector;
  228. if (offset % sector_size || len % sector_size) {
  229. debug("SF: Erase offset/length not multiple of sector size\n");
  230. return -1;
  231. }
  232. len /= sector_size;
  233. cmd[0] = CMD_M25PXX_SE;
  234. cmd[2] = 0x00;
  235. cmd[3] = 0x00;
  236. ret = spi_claim_bus(flash->spi);
  237. if (ret) {
  238. debug("SF: Unable to claim SPI bus\n");
  239. return ret;
  240. }
  241. ret = 0;
  242. for (actual = 0; actual < len; actual++) {
  243. cmd[1] = (offset / sector_size) + actual;
  244. ret = spi_flash_cmd(flash->spi, CMD_M25PXX_WREN, NULL, 0);
  245. if (ret < 0) {
  246. debug("SF: Enabling Write failed\n");
  247. break;
  248. }
  249. ret = spi_flash_cmd_write(flash->spi, cmd, 4, NULL, 0);
  250. if (ret < 0) {
  251. debug("SF: STMicro page erase failed\n");
  252. break;
  253. }
  254. /* Up to 2 seconds */
  255. ret = stmicro_wait_ready(flash, 2 * CONFIG_SYS_HZ);
  256. if (ret < 0) {
  257. debug("SF: STMicro page erase timed out\n");
  258. break;
  259. }
  260. }
  261. debug("SF: STMicro: Successfully erased %u bytes @ 0x%x\n",
  262. len * sector_size, offset);
  263. spi_release_bus(flash->spi);
  264. return ret;
  265. }
  266. struct spi_flash *spi_flash_probe_stmicro(struct spi_slave *spi, u8 * idcode)
  267. {
  268. const struct stmicro_spi_flash_params *params;
  269. struct stmicro_spi_flash *stm;
  270. unsigned int i;
  271. int ret;
  272. u8 id[3];
  273. ret = spi_flash_cmd(spi, CMD_READ_ID, id, sizeof(id));
  274. if (ret)
  275. return NULL;
  276. for (i = 0; i < ARRAY_SIZE(stmicro_spi_flash_table); i++) {
  277. params = &stmicro_spi_flash_table[i];
  278. if (params->idcode1 == idcode[2]) {
  279. break;
  280. }
  281. }
  282. if (i == ARRAY_SIZE(stmicro_spi_flash_table)) {
  283. debug("SF: Unsupported STMicro ID %02x\n", id[1]);
  284. return NULL;
  285. }
  286. stm = malloc(sizeof(struct stmicro_spi_flash));
  287. if (!stm) {
  288. debug("SF: Failed to allocate memory\n");
  289. return NULL;
  290. }
  291. stm->params = params;
  292. stm->flash.spi = spi;
  293. stm->flash.name = params->name;
  294. stm->flash.write = stmicro_write;
  295. stm->flash.erase = stmicro_erase;
  296. stm->flash.read = stmicro_read_fast;
  297. stm->flash.size = params->page_size * params->pages_per_sector
  298. * params->nr_sectors;
  299. debug("SF: Detected %s with page size %u, total %u bytes\n",
  300. params->name, params->page_size, stm->flash.size);
  301. return &stm->flash;
  302. }