macronix.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. struct macronix_spi_flash_params {
  49. u16 idcode;
  50. u16 page_size;
  51. u16 pages_per_sector;
  52. u16 sectors_per_block;
  53. u16 nr_blocks;
  54. const char *name;
  55. };
  56. struct macronix_spi_flash {
  57. struct spi_flash flash;
  58. const struct macronix_spi_flash_params *params;
  59. };
  60. static inline struct macronix_spi_flash *to_macronix_spi_flash(struct spi_flash
  61. *flash)
  62. {
  63. return container_of(flash, struct macronix_spi_flash, flash);
  64. }
  65. static const struct macronix_spi_flash_params macronix_spi_flash_table[] = {
  66. {
  67. .idcode = 0x2015,
  68. .page_size = 256,
  69. .pages_per_sector = 16,
  70. .sectors_per_block = 16,
  71. .nr_blocks = 32,
  72. .name = "MX25L1605D",
  73. },
  74. {
  75. .idcode = 0x2016,
  76. .page_size = 256,
  77. .pages_per_sector = 16,
  78. .sectors_per_block = 16,
  79. .nr_blocks = 64,
  80. .name = "MX25L3205D",
  81. },
  82. {
  83. .idcode = 0x2017,
  84. .page_size = 256,
  85. .pages_per_sector = 16,
  86. .sectors_per_block = 16,
  87. .nr_blocks = 128,
  88. .name = "MX25L6405D",
  89. },
  90. {
  91. .idcode = 0x2018,
  92. .page_size = 256,
  93. .pages_per_sector = 16,
  94. .sectors_per_block = 16,
  95. .nr_blocks = 256,
  96. .name = "MX25L12805D",
  97. },
  98. {
  99. .idcode = 0x2618,
  100. .page_size = 256,
  101. .pages_per_sector = 16,
  102. .sectors_per_block = 16,
  103. .nr_blocks = 256,
  104. .name = "MX25L12855E",
  105. },
  106. };
  107. static int macronix_write(struct spi_flash *flash,
  108. u32 offset, size_t len, const void *buf)
  109. {
  110. struct macronix_spi_flash *mcx = to_macronix_spi_flash(flash);
  111. unsigned long page_addr;
  112. unsigned long byte_addr;
  113. unsigned long page_size;
  114. size_t chunk_len;
  115. size_t actual;
  116. int ret;
  117. u8 cmd[4];
  118. page_size = mcx->params->page_size;
  119. page_addr = offset / page_size;
  120. byte_addr = offset % page_size;
  121. ret = spi_claim_bus(flash->spi);
  122. if (ret) {
  123. debug("SF: Unable to claim SPI bus\n");
  124. return ret;
  125. }
  126. ret = 0;
  127. for (actual = 0; actual < len; actual += chunk_len) {
  128. chunk_len = min(len - actual, page_size - byte_addr);
  129. cmd[0] = CMD_MX25XX_PP;
  130. cmd[1] = page_addr >> 8;
  131. cmd[2] = page_addr;
  132. cmd[3] = byte_addr;
  133. debug
  134. ("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %d\n",
  135. buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
  136. ret = spi_flash_cmd(flash->spi, CMD_MX25XX_WREN, NULL, 0);
  137. if (ret < 0) {
  138. debug("SF: Enabling Write failed\n");
  139. break;
  140. }
  141. ret = spi_flash_cmd_write(flash->spi, cmd, 4,
  142. buf + actual, chunk_len);
  143. if (ret < 0) {
  144. debug("SF: Macronix Page Program failed\n");
  145. break;
  146. }
  147. ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
  148. if (ret)
  149. break;
  150. page_addr++;
  151. byte_addr = 0;
  152. }
  153. debug("SF: Macronix: Successfully programmed %u bytes @ 0x%x\n",
  154. len, offset);
  155. spi_release_bus(flash->spi);
  156. return ret;
  157. }
  158. int macronix_erase(struct spi_flash *flash, u32 offset, size_t len)
  159. {
  160. struct macronix_spi_flash *mcx = to_macronix_spi_flash(flash);
  161. return spi_flash_cmd_erase(flash, CMD_MX25XX_BE,
  162. mcx->params->page_size * mcx->params->pages_per_sector *
  163. mcx->params->sectors_per_block,
  164. offset, len);
  165. }
  166. struct spi_flash *spi_flash_probe_macronix(struct spi_slave *spi, u8 *idcode)
  167. {
  168. const struct macronix_spi_flash_params *params;
  169. struct macronix_spi_flash *mcx;
  170. unsigned int i;
  171. u16 id = idcode[2] | idcode[1] << 8;
  172. for (i = 0; i < ARRAY_SIZE(macronix_spi_flash_table); i++) {
  173. params = &macronix_spi_flash_table[i];
  174. if (params->idcode == id)
  175. break;
  176. }
  177. if (i == ARRAY_SIZE(macronix_spi_flash_table)) {
  178. debug("SF: Unsupported Macronix ID %04x\n", id);
  179. return NULL;
  180. }
  181. mcx = malloc(sizeof(*mcx));
  182. if (!mcx) {
  183. debug("SF: Failed to allocate memory\n");
  184. return NULL;
  185. }
  186. mcx->params = params;
  187. mcx->flash.spi = spi;
  188. mcx->flash.name = params->name;
  189. mcx->flash.write = macronix_write;
  190. mcx->flash.erase = macronix_erase;
  191. mcx->flash.read = spi_flash_cmd_read_fast;
  192. mcx->flash.size = params->page_size * params->pages_per_sector
  193. * params->sectors_per_block * params->nr_blocks;
  194. printf("SF: Detected %s with page size %u, total ",
  195. params->name, params->page_size);
  196. print_size(mcx->flash.size, "\n");
  197. return &mcx->flash;
  198. }