macronix.c 3.5 KB

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