macronix.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. static const struct macronix_spi_flash_params macronix_spi_flash_table[] = {
  57. {
  58. .idcode = 0x2013,
  59. .page_size = 256,
  60. .pages_per_sector = 16,
  61. .sectors_per_block = 16,
  62. .nr_blocks = 8,
  63. .name = "MX25L4005",
  64. },
  65. {
  66. .idcode = 0x2014,
  67. .page_size = 256,
  68. .pages_per_sector = 16,
  69. .sectors_per_block = 16,
  70. .nr_blocks = 16,
  71. .name = "MX25L8005",
  72. },
  73. {
  74. .idcode = 0x2015,
  75. .page_size = 256,
  76. .pages_per_sector = 16,
  77. .sectors_per_block = 16,
  78. .nr_blocks = 32,
  79. .name = "MX25L1605D",
  80. },
  81. {
  82. .idcode = 0x2016,
  83. .page_size = 256,
  84. .pages_per_sector = 16,
  85. .sectors_per_block = 16,
  86. .nr_blocks = 64,
  87. .name = "MX25L3205D",
  88. },
  89. {
  90. .idcode = 0x2017,
  91. .page_size = 256,
  92. .pages_per_sector = 16,
  93. .sectors_per_block = 16,
  94. .nr_blocks = 128,
  95. .name = "MX25L6405D",
  96. },
  97. {
  98. .idcode = 0x2018,
  99. .page_size = 256,
  100. .pages_per_sector = 16,
  101. .sectors_per_block = 16,
  102. .nr_blocks = 256,
  103. .name = "MX25L12805D",
  104. },
  105. {
  106. .idcode = 0x2618,
  107. .page_size = 256,
  108. .pages_per_sector = 16,
  109. .sectors_per_block = 16,
  110. .nr_blocks = 256,
  111. .name = "MX25L12855E",
  112. },
  113. };
  114. static int macronix_erase(struct spi_flash *flash, u32 offset, size_t len)
  115. {
  116. return spi_flash_cmd_erase(flash, CMD_MX25XX_BE, offset, len);
  117. }
  118. struct spi_flash *spi_flash_probe_macronix(struct spi_slave *spi, u8 *idcode)
  119. {
  120. const struct macronix_spi_flash_params *params;
  121. struct spi_flash *flash;
  122. unsigned int i;
  123. u16 id = idcode[2] | idcode[1] << 8;
  124. for (i = 0; i < ARRAY_SIZE(macronix_spi_flash_table); i++) {
  125. params = &macronix_spi_flash_table[i];
  126. if (params->idcode == id)
  127. break;
  128. }
  129. if (i == ARRAY_SIZE(macronix_spi_flash_table)) {
  130. debug("SF: Unsupported Macronix ID %04x\n", id);
  131. return NULL;
  132. }
  133. flash = malloc(sizeof(*flash));
  134. if (!flash) {
  135. debug("SF: Failed to allocate memory\n");
  136. return NULL;
  137. }
  138. flash->spi = spi;
  139. flash->name = params->name;
  140. flash->write = spi_flash_cmd_write_multi;
  141. flash->erase = macronix_erase;
  142. flash->read = spi_flash_cmd_read_fast;
  143. flash->page_size = params->page_size;
  144. flash->sector_size = params->page_size * params->pages_per_sector
  145. * params->sectors_per_block;
  146. flash->size = flash->sector_size * params->nr_blocks;
  147. return flash;
  148. }