stmicro.c 8.3 KB

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