stmicro.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * (C) Copyright 2000-2002
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * Copyright 2008, Network Appliance Inc.
  6. * Jason McMullan <mcmullan@netapp.com>
  7. *
  8. * Copyright (C) 2004-2007 Freescale Semiconductor, Inc.
  9. * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
  10. *
  11. * See file CREDITS for list of people who contributed to this
  12. * project.
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License as
  16. * published by the Free Software Foundation; either version 2 of
  17. * the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  27. * MA 02111-1307 USA
  28. */
  29. #include <common.h>
  30. #include <malloc.h>
  31. #include <spi_flash.h>
  32. #include "spi_flash_internal.h"
  33. /* M25Pxx-specific commands */
  34. #define CMD_M25PXX_WREN 0x06 /* Write Enable */
  35. #define CMD_M25PXX_WRDI 0x04 /* Write Disable */
  36. #define CMD_M25PXX_RDSR 0x05 /* Read Status Register */
  37. #define CMD_M25PXX_WRSR 0x01 /* Write Status Register */
  38. #define CMD_M25PXX_READ 0x03 /* Read Data Bytes */
  39. #define CMD_M25PXX_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */
  40. #define CMD_M25PXX_PP 0x02 /* Page Program */
  41. #define CMD_M25PXX_SE 0xd8 /* Sector Erase */
  42. #define CMD_M25PXX_BE 0xc7 /* Bulk Erase */
  43. #define CMD_M25PXX_DP 0xb9 /* Deep Power-down */
  44. #define CMD_M25PXX_RES 0xab /* Release from DP, and Read Signature */
  45. #define STM_ID_M25P10 0x11
  46. #define STM_ID_M25P16 0x15
  47. #define STM_ID_M25P20 0x12
  48. #define STM_ID_M25P32 0x16
  49. #define STM_ID_M25P40 0x13
  50. #define STM_ID_M25P64 0x17
  51. #define STM_ID_M25P80 0x14
  52. #define STM_ID_M25P128 0x18
  53. struct stmicro_spi_flash_params {
  54. u8 idcode1;
  55. u16 page_size;
  56. u16 pages_per_sector;
  57. u16 nr_sectors;
  58. const char *name;
  59. };
  60. static const struct stmicro_spi_flash_params stmicro_spi_flash_table[] = {
  61. {
  62. .idcode1 = STM_ID_M25P10,
  63. .page_size = 256,
  64. .pages_per_sector = 128,
  65. .nr_sectors = 4,
  66. .name = "M25P10",
  67. },
  68. {
  69. .idcode1 = STM_ID_M25P16,
  70. .page_size = 256,
  71. .pages_per_sector = 256,
  72. .nr_sectors = 32,
  73. .name = "M25P16",
  74. },
  75. {
  76. .idcode1 = STM_ID_M25P20,
  77. .page_size = 256,
  78. .pages_per_sector = 256,
  79. .nr_sectors = 4,
  80. .name = "M25P20",
  81. },
  82. {
  83. .idcode1 = STM_ID_M25P32,
  84. .page_size = 256,
  85. .pages_per_sector = 256,
  86. .nr_sectors = 64,
  87. .name = "M25P32",
  88. },
  89. {
  90. .idcode1 = STM_ID_M25P40,
  91. .page_size = 256,
  92. .pages_per_sector = 256,
  93. .nr_sectors = 8,
  94. .name = "M25P40",
  95. },
  96. {
  97. .idcode1 = STM_ID_M25P64,
  98. .page_size = 256,
  99. .pages_per_sector = 256,
  100. .nr_sectors = 128,
  101. .name = "M25P64",
  102. },
  103. {
  104. .idcode1 = STM_ID_M25P80,
  105. .page_size = 256,
  106. .pages_per_sector = 256,
  107. .nr_sectors = 16,
  108. .name = "M25P80",
  109. },
  110. {
  111. .idcode1 = STM_ID_M25P128,
  112. .page_size = 256,
  113. .pages_per_sector = 1024,
  114. .nr_sectors = 64,
  115. .name = "M25P128",
  116. },
  117. };
  118. static int stmicro_erase(struct spi_flash *flash, u32 offset, size_t len)
  119. {
  120. return spi_flash_cmd_erase(flash, CMD_M25PXX_SE, offset, len);
  121. }
  122. struct spi_flash *spi_flash_probe_stmicro(struct spi_slave *spi, u8 * idcode)
  123. {
  124. const struct stmicro_spi_flash_params *params;
  125. struct spi_flash *flash;
  126. unsigned int i;
  127. if (idcode[0] == 0xff) {
  128. i = spi_flash_cmd(spi, CMD_M25PXX_RES,
  129. idcode, 4);
  130. if (i)
  131. return NULL;
  132. if ((idcode[3] & 0xf0) == 0x10) {
  133. idcode[0] = 0x20;
  134. idcode[1] = 0x20;
  135. idcode[2] = idcode[3] + 1;
  136. } else
  137. return NULL;
  138. }
  139. for (i = 0; i < ARRAY_SIZE(stmicro_spi_flash_table); i++) {
  140. params = &stmicro_spi_flash_table[i];
  141. if (params->idcode1 == idcode[2]) {
  142. break;
  143. }
  144. }
  145. if (i == ARRAY_SIZE(stmicro_spi_flash_table)) {
  146. debug("SF: Unsupported STMicro ID %02x\n", idcode[1]);
  147. return NULL;
  148. }
  149. flash = malloc(sizeof(*flash));
  150. if (!flash) {
  151. debug("SF: Failed to allocate memory\n");
  152. return NULL;
  153. }
  154. flash->spi = spi;
  155. flash->name = params->name;
  156. flash->write = spi_flash_cmd_write_multi;
  157. flash->erase = stmicro_erase;
  158. flash->read = spi_flash_cmd_read_fast;
  159. flash->page_size = params->page_size;
  160. flash->sector_size = params->page_size * params->pages_per_sector;
  161. flash->size = flash->sector_size * params->nr_sectors;
  162. return flash;
  163. }