stmicro.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. 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_M25P10,
  73. .page_size = 256,
  74. .pages_per_sector = 128,
  75. .nr_sectors = 4,
  76. .name = "M25P10",
  77. },
  78. {
  79. .idcode1 = STM_ID_M25P16,
  80. .page_size = 256,
  81. .pages_per_sector = 256,
  82. .nr_sectors = 32,
  83. .name = "M25P16",
  84. },
  85. {
  86. .idcode1 = STM_ID_M25P20,
  87. .page_size = 256,
  88. .pages_per_sector = 256,
  89. .nr_sectors = 4,
  90. .name = "M25P20",
  91. },
  92. {
  93. .idcode1 = STM_ID_M25P32,
  94. .page_size = 256,
  95. .pages_per_sector = 256,
  96. .nr_sectors = 64,
  97. .name = "M25P32",
  98. },
  99. {
  100. .idcode1 = STM_ID_M25P40,
  101. .page_size = 256,
  102. .pages_per_sector = 256,
  103. .nr_sectors = 8,
  104. .name = "M25P40",
  105. },
  106. {
  107. .idcode1 = STM_ID_M25P64,
  108. .page_size = 256,
  109. .pages_per_sector = 256,
  110. .nr_sectors = 128,
  111. .name = "M25P64",
  112. },
  113. {
  114. .idcode1 = STM_ID_M25P80,
  115. .page_size = 256,
  116. .pages_per_sector = 256,
  117. .nr_sectors = 16,
  118. .name = "M25P80",
  119. },
  120. {
  121. .idcode1 = STM_ID_M25P128,
  122. .page_size = 256,
  123. .pages_per_sector = 1024,
  124. .nr_sectors = 64,
  125. .name = "M25P128",
  126. },
  127. };
  128. static int stmicro_write(struct spi_flash *flash,
  129. u32 offset, size_t len, const void *buf)
  130. {
  131. struct stmicro_spi_flash *stm = to_stmicro_spi_flash(flash);
  132. unsigned long page_addr;
  133. unsigned long byte_addr;
  134. unsigned long page_size;
  135. size_t chunk_len;
  136. size_t actual;
  137. int ret;
  138. u8 cmd[4];
  139. page_size = stm->params->page_size;
  140. page_addr = offset / page_size;
  141. byte_addr = offset % page_size;
  142. ret = spi_claim_bus(flash->spi);
  143. if (ret) {
  144. debug("SF: Unable to claim SPI bus\n");
  145. return ret;
  146. }
  147. ret = 0;
  148. for (actual = 0; actual < len; actual += chunk_len) {
  149. chunk_len = min(len - actual, page_size - byte_addr);
  150. cmd[0] = CMD_M25PXX_PP;
  151. cmd[1] = page_addr >> 8;
  152. cmd[2] = page_addr;
  153. cmd[3] = byte_addr;
  154. debug
  155. ("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %d\n",
  156. buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
  157. ret = spi_flash_cmd(flash->spi, CMD_M25PXX_WREN, NULL, 0);
  158. if (ret < 0) {
  159. debug("SF: Enabling Write failed\n");
  160. break;
  161. }
  162. ret = spi_flash_cmd_write(flash->spi, cmd, 4,
  163. buf + actual, chunk_len);
  164. if (ret < 0) {
  165. debug("SF: STMicro Page Program failed\n");
  166. break;
  167. }
  168. ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
  169. if (ret)
  170. break;
  171. page_addr++;
  172. byte_addr = 0;
  173. }
  174. debug("SF: STMicro: Successfully programmed %u bytes @ 0x%x\n",
  175. len, offset);
  176. spi_release_bus(flash->spi);
  177. return ret;
  178. }
  179. static int stmicro_erase(struct spi_flash *flash, u32 offset, size_t len)
  180. {
  181. return spi_flash_cmd_erase(flash, CMD_M25PXX_SE, offset, len);
  182. }
  183. struct spi_flash *spi_flash_probe_stmicro(struct spi_slave *spi, u8 * idcode)
  184. {
  185. const struct stmicro_spi_flash_params *params;
  186. struct stmicro_spi_flash *stm;
  187. unsigned int i;
  188. if (idcode[0] == 0xff) {
  189. i = spi_flash_cmd(spi, CMD_M25PXX_RES,
  190. idcode, 4);
  191. if (i)
  192. return NULL;
  193. if ((idcode[3] & 0xf0) == 0x10) {
  194. idcode[0] = 0x20;
  195. idcode[1] = 0x20;
  196. idcode[2] = idcode[3] + 1;
  197. } else
  198. return NULL;
  199. }
  200. for (i = 0; i < ARRAY_SIZE(stmicro_spi_flash_table); i++) {
  201. params = &stmicro_spi_flash_table[i];
  202. if (params->idcode1 == idcode[2]) {
  203. break;
  204. }
  205. }
  206. if (i == ARRAY_SIZE(stmicro_spi_flash_table)) {
  207. debug("SF: Unsupported STMicro ID %02x\n", idcode[1]);
  208. return NULL;
  209. }
  210. stm = malloc(sizeof(struct stmicro_spi_flash));
  211. if (!stm) {
  212. debug("SF: Failed to allocate memory\n");
  213. return NULL;
  214. }
  215. stm->params = params;
  216. stm->flash.spi = spi;
  217. stm->flash.name = params->name;
  218. stm->flash.write = stmicro_write;
  219. stm->flash.erase = stmicro_erase;
  220. stm->flash.read = spi_flash_cmd_read_fast;
  221. stm->flash.sector_size = params->page_size * params->pages_per_sector;
  222. stm->flash.size = stm->flash.sector_size * params->nr_sectors;
  223. return &stm->flash;
  224. }