macronix.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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_SE 0x20 /* Sector Erase */
  37. #define CMD_MX25XX_BE 0xD8 /* Block Erase */
  38. #define CMD_MX25XX_CE 0xc7 /* Chip Erase */
  39. struct macronix_spi_flash_params {
  40. u16 idcode;
  41. u16 page_size;
  42. u16 pages_per_sector;
  43. u16 sectors_per_block;
  44. u16 nr_blocks;
  45. const char *name;
  46. };
  47. static const struct macronix_spi_flash_params macronix_spi_flash_table[] = {
  48. {
  49. .idcode = 0x2013,
  50. .page_size = 256,
  51. .pages_per_sector = 16,
  52. .sectors_per_block = 16,
  53. .nr_blocks = 8,
  54. .name = "MX25L4005",
  55. },
  56. {
  57. .idcode = 0x2014,
  58. .page_size = 256,
  59. .pages_per_sector = 16,
  60. .sectors_per_block = 16,
  61. .nr_blocks = 16,
  62. .name = "MX25L8005",
  63. },
  64. {
  65. .idcode = 0x2015,
  66. .page_size = 256,
  67. .pages_per_sector = 16,
  68. .sectors_per_block = 16,
  69. .nr_blocks = 32,
  70. .name = "MX25L1605D",
  71. },
  72. {
  73. .idcode = 0x2016,
  74. .page_size = 256,
  75. .pages_per_sector = 16,
  76. .sectors_per_block = 16,
  77. .nr_blocks = 64,
  78. .name = "MX25L3205D",
  79. },
  80. {
  81. .idcode = 0x2017,
  82. .page_size = 256,
  83. .pages_per_sector = 16,
  84. .sectors_per_block = 16,
  85. .nr_blocks = 128,
  86. .name = "MX25L6405D",
  87. },
  88. {
  89. .idcode = 0x2018,
  90. .page_size = 256,
  91. .pages_per_sector = 16,
  92. .sectors_per_block = 16,
  93. .nr_blocks = 256,
  94. .name = "MX25L12805D",
  95. },
  96. {
  97. .idcode = 0x2618,
  98. .page_size = 256,
  99. .pages_per_sector = 16,
  100. .sectors_per_block = 16,
  101. .nr_blocks = 256,
  102. .name = "MX25L12855E",
  103. },
  104. };
  105. static int macronix_write_status(struct spi_flash *flash, u8 sr)
  106. {
  107. u8 cmd;
  108. int ret;
  109. ret = spi_flash_cmd_write_enable(flash);
  110. if (ret < 0) {
  111. debug("SF: enabling write failed\n");
  112. return ret;
  113. }
  114. cmd = CMD_WRITE_STATUS;
  115. ret = spi_flash_cmd_write(flash->spi, &cmd, 1, &sr, 1);
  116. if (ret) {
  117. debug("SF: fail to write status register\n");
  118. return ret;
  119. }
  120. ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
  121. if (ret < 0) {
  122. debug("SF: write status register timed out\n");
  123. return ret;
  124. }
  125. return 0;
  126. }
  127. static int macronix_unlock(struct spi_flash *flash)
  128. {
  129. int ret;
  130. /* Enable status register writing and clear BP# bits */
  131. ret = macronix_write_status(flash, 0);
  132. if (ret)
  133. debug("SF: fail to disable write protection\n");
  134. return ret;
  135. }
  136. static int macronix_erase(struct spi_flash *flash, u32 offset, size_t len)
  137. {
  138. return spi_flash_cmd_erase(flash, CMD_MX25XX_BE, offset, len);
  139. }
  140. struct spi_flash *spi_flash_probe_macronix(struct spi_slave *spi, u8 *idcode)
  141. {
  142. const struct macronix_spi_flash_params *params;
  143. struct spi_flash *flash;
  144. unsigned int i;
  145. u16 id = idcode[2] | idcode[1] << 8;
  146. for (i = 0; i < ARRAY_SIZE(macronix_spi_flash_table); i++) {
  147. params = &macronix_spi_flash_table[i];
  148. if (params->idcode == id)
  149. break;
  150. }
  151. if (i == ARRAY_SIZE(macronix_spi_flash_table)) {
  152. debug("SF: Unsupported Macronix ID %04x\n", id);
  153. return NULL;
  154. }
  155. flash = malloc(sizeof(*flash));
  156. if (!flash) {
  157. debug("SF: Failed to allocate memory\n");
  158. return NULL;
  159. }
  160. flash->spi = spi;
  161. flash->name = params->name;
  162. flash->write = spi_flash_cmd_write_multi;
  163. flash->erase = macronix_erase;
  164. flash->read = spi_flash_cmd_read_fast;
  165. flash->page_size = params->page_size;
  166. flash->sector_size = params->page_size * params->pages_per_sector
  167. * params->sectors_per_block;
  168. flash->size = flash->sector_size * params->nr_blocks;
  169. /* Clear BP# bits for read-only flash */
  170. macronix_unlock(flash);
  171. return flash;
  172. }