macronix.c 3.9 KB

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