macronix.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * Copyright 2009(C) Marvell International Ltd. and its affiliates
  3. * Prafulla Wadaskar <prafulla@marvell.com>
  4. *
  5. * Based on drivers/mtd/spi/stmicro.c
  6. *
  7. * Copyright 2008, Network Appliance Inc.
  8. * Jason McMullan <mcmullan@netapp.com>
  9. *
  10. * Copyright (C) 2004-2007 Freescale Semiconductor, Inc.
  11. * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
  12. *
  13. * See file CREDITS for list of people who contributed to this
  14. * project.
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License as
  18. * published by the Free Software Foundation; either version 2 of
  19. * the License, or (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  29. * MA 02110-1301 USA
  30. */
  31. #include <common.h>
  32. #include <malloc.h>
  33. #include <spi_flash.h>
  34. #include "spi_flash_internal.h"
  35. /* MX25xx-specific commands */
  36. #define CMD_MX25XX_WREN 0x06 /* Write Enable */
  37. #define CMD_MX25XX_WRDI 0x04 /* Write Disable */
  38. #define CMD_MX25XX_RDSR 0x05 /* Read Status Register */
  39. #define CMD_MX25XX_WRSR 0x01 /* Write Status Register */
  40. #define CMD_MX25XX_READ 0x03 /* Read Data Bytes */
  41. #define CMD_MX25XX_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */
  42. #define CMD_MX25XX_PP 0x02 /* Page Program */
  43. #define CMD_MX25XX_SE 0x20 /* Sector Erase */
  44. #define CMD_MX25XX_BE 0xD8 /* Block Erase */
  45. #define CMD_MX25XX_CE 0xc7 /* Chip Erase */
  46. #define CMD_MX25XX_DP 0xb9 /* Deep Power-down */
  47. #define CMD_MX25XX_RES 0xab /* Release from DP, and Read Signature */
  48. #define MACRONIX_SR_WIP (1 << 0) /* Write-in-Progress */
  49. struct macronix_spi_flash_params {
  50. u16 idcode;
  51. u16 page_size;
  52. u16 pages_per_sector;
  53. u16 sectors_per_block;
  54. u16 nr_blocks;
  55. const char *name;
  56. };
  57. struct macronix_spi_flash {
  58. struct spi_flash flash;
  59. const struct macronix_spi_flash_params *params;
  60. };
  61. static inline struct macronix_spi_flash *to_macronix_spi_flash(struct spi_flash
  62. *flash)
  63. {
  64. return container_of(flash, struct macronix_spi_flash, flash);
  65. }
  66. static const struct macronix_spi_flash_params macronix_spi_flash_table[] = {
  67. {
  68. .idcode = 0x2015,
  69. .page_size = 256,
  70. .pages_per_sector = 16,
  71. .sectors_per_block = 16,
  72. .nr_blocks = 32,
  73. .name = "MX25L1605D",
  74. },
  75. {
  76. .idcode = 0x2016,
  77. .page_size = 256,
  78. .pages_per_sector = 16,
  79. .sectors_per_block = 16,
  80. .nr_blocks = 64,
  81. .name = "MX25L3205D",
  82. },
  83. {
  84. .idcode = 0x2017,
  85. .page_size = 256,
  86. .pages_per_sector = 16,
  87. .sectors_per_block = 16,
  88. .nr_blocks = 128,
  89. .name = "MX25L6405D",
  90. },
  91. {
  92. .idcode = 0x2018,
  93. .page_size = 256,
  94. .pages_per_sector = 16,
  95. .sectors_per_block = 16,
  96. .nr_blocks = 256,
  97. .name = "MX25L12805D",
  98. },
  99. {
  100. .idcode = 0x2618,
  101. .page_size = 256,
  102. .pages_per_sector = 16,
  103. .sectors_per_block = 16,
  104. .nr_blocks = 256,
  105. .name = "MX25L12855E",
  106. },
  107. };
  108. static int macronix_wait_ready(struct spi_flash *flash, unsigned long timeout)
  109. {
  110. struct spi_slave *spi = flash->spi;
  111. unsigned long timebase;
  112. int ret;
  113. u8 status;
  114. u8 cmd = CMD_MX25XX_RDSR;
  115. ret = spi_xfer(spi, 8, &cmd, NULL, SPI_XFER_BEGIN);
  116. if (ret) {
  117. debug("SF: Failed to send command %02x: %d\n", cmd, ret);
  118. return ret;
  119. }
  120. timebase = get_timer(0);
  121. do {
  122. ret = spi_xfer(spi, 8, NULL, &status, 0);
  123. if (ret)
  124. return -1;
  125. if ((status & MACRONIX_SR_WIP) == 0)
  126. break;
  127. } while (get_timer(timebase) < timeout);
  128. spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END);
  129. if ((status & MACRONIX_SR_WIP) == 0)
  130. return 0;
  131. /* Timed out */
  132. return -1;
  133. }
  134. static int macronix_read_fast(struct spi_flash *flash,
  135. u32 offset, size_t len, void *buf)
  136. {
  137. struct macronix_spi_flash *mcx = to_macronix_spi_flash(flash);
  138. unsigned long page_addr;
  139. unsigned long page_size;
  140. u8 cmd[5];
  141. page_size = mcx->params->page_size;
  142. page_addr = offset / page_size;
  143. cmd[0] = CMD_READ_ARRAY_FAST;
  144. cmd[1] = page_addr >> 8;
  145. cmd[2] = page_addr;
  146. cmd[3] = offset % page_size;
  147. cmd[4] = 0x00;
  148. return spi_flash_read_common(flash, cmd, sizeof(cmd), buf, len);
  149. }
  150. static int macronix_write(struct spi_flash *flash,
  151. u32 offset, size_t len, const void *buf)
  152. {
  153. struct macronix_spi_flash *mcx = to_macronix_spi_flash(flash);
  154. unsigned long page_addr;
  155. unsigned long byte_addr;
  156. unsigned long page_size;
  157. size_t chunk_len;
  158. size_t actual;
  159. int ret;
  160. u8 cmd[4];
  161. page_size = mcx->params->page_size;
  162. page_addr = offset / page_size;
  163. byte_addr = offset % page_size;
  164. ret = spi_claim_bus(flash->spi);
  165. if (ret) {
  166. debug("SF: Unable to claim SPI bus\n");
  167. return ret;
  168. }
  169. ret = 0;
  170. for (actual = 0; actual < len; actual += chunk_len) {
  171. chunk_len = min(len - actual, page_size - byte_addr);
  172. cmd[0] = CMD_MX25XX_PP;
  173. cmd[1] = page_addr >> 8;
  174. cmd[2] = page_addr;
  175. cmd[3] = byte_addr;
  176. debug
  177. ("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %d\n",
  178. buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
  179. ret = spi_flash_cmd(flash->spi, CMD_MX25XX_WREN, NULL, 0);
  180. if (ret < 0) {
  181. debug("SF: Enabling Write failed\n");
  182. break;
  183. }
  184. ret = spi_flash_cmd_write(flash->spi, cmd, 4,
  185. buf + actual, chunk_len);
  186. if (ret < 0) {
  187. debug("SF: Macronix Page Program failed\n");
  188. break;
  189. }
  190. ret = macronix_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
  191. if (ret < 0) {
  192. debug("SF: Macronix page programming timed out\n");
  193. break;
  194. }
  195. page_addr++;
  196. byte_addr = 0;
  197. }
  198. debug("SF: Macronix: Successfully programmed %u bytes @ 0x%x\n",
  199. len, offset);
  200. spi_release_bus(flash->spi);
  201. return ret;
  202. }
  203. int macronix_erase(struct spi_flash *flash, u32 offset, size_t len)
  204. {
  205. struct macronix_spi_flash *mcx = to_macronix_spi_flash(flash);
  206. unsigned long sector_size;
  207. size_t actual;
  208. int ret;
  209. u8 cmd[4];
  210. /*
  211. * This function currently uses sector erase only.
  212. * probably speed things up by using bulk erase
  213. * when possible.
  214. */
  215. sector_size = mcx->params->page_size * mcx->params->pages_per_sector
  216. * mcx->params->sectors_per_block;
  217. if (offset % sector_size || len % sector_size) {
  218. debug("SF: Erase offset/length not multiple of sector size\n");
  219. return -1;
  220. }
  221. len /= sector_size;
  222. cmd[0] = CMD_MX25XX_BE;
  223. cmd[2] = 0x00;
  224. cmd[3] = 0x00;
  225. ret = spi_claim_bus(flash->spi);
  226. if (ret) {
  227. debug("SF: Unable to claim SPI bus\n");
  228. return ret;
  229. }
  230. ret = 0;
  231. for (actual = 0; actual < len; actual++) {
  232. cmd[1] = (offset / sector_size) + actual;
  233. ret = spi_flash_cmd(flash->spi, CMD_MX25XX_WREN, NULL, 0);
  234. if (ret < 0) {
  235. debug("SF: Enabling Write failed\n");
  236. break;
  237. }
  238. ret = spi_flash_cmd_write(flash->spi, cmd, 4, NULL, 0);
  239. if (ret < 0) {
  240. debug("SF: Macronix page erase failed\n");
  241. break;
  242. }
  243. ret = macronix_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
  244. if (ret < 0) {
  245. debug("SF: Macronix page erase timed out\n");
  246. break;
  247. }
  248. }
  249. debug("SF: Macronix: Successfully erased %u bytes @ 0x%x\n",
  250. len * sector_size, offset);
  251. spi_release_bus(flash->spi);
  252. return ret;
  253. }
  254. struct spi_flash *spi_flash_probe_macronix(struct spi_slave *spi, u8 *idcode)
  255. {
  256. const struct macronix_spi_flash_params *params;
  257. struct macronix_spi_flash *mcx;
  258. unsigned int i;
  259. u16 id = idcode[2] | idcode[1] << 8;
  260. for (i = 0; i < ARRAY_SIZE(macronix_spi_flash_table); i++) {
  261. params = &macronix_spi_flash_table[i];
  262. if (params->idcode == id)
  263. break;
  264. }
  265. if (i == ARRAY_SIZE(macronix_spi_flash_table)) {
  266. debug("SF: Unsupported Macronix ID %04x\n", id);
  267. return NULL;
  268. }
  269. mcx = malloc(sizeof(*mcx));
  270. if (!mcx) {
  271. debug("SF: Failed to allocate memory\n");
  272. return NULL;
  273. }
  274. mcx->params = params;
  275. mcx->flash.spi = spi;
  276. mcx->flash.name = params->name;
  277. mcx->flash.write = macronix_write;
  278. mcx->flash.erase = macronix_erase;
  279. mcx->flash.read = macronix_read_fast;
  280. mcx->flash.size = params->page_size * params->pages_per_sector
  281. * params->sectors_per_block * params->nr_blocks;
  282. printf("SF: Detected %s with page size %u, total %u bytes\n",
  283. params->name, params->page_size, mcx->flash.size);
  284. return &mcx->flash;
  285. }