blob: 9a9d3d401394518e5cc65c89d83c93663729b6dd [file] [log] [blame]
TsiChung Liew7b7a8692008-08-06 16:08:41 -05001/*
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
30#include <common.h>
31#include <malloc.h>
32#include <spi_flash.h>
33
34#include "spi_flash_internal.h"
35
36/* M25Pxx-specific commands */
37#define CMD_M25PXX_WREN 0x06 /* Write Enable */
38#define CMD_M25PXX_WRDI 0x04 /* Write Disable */
39#define CMD_M25PXX_RDSR 0x05 /* Read Status Register */
40#define CMD_M25PXX_WRSR 0x01 /* Write Status Register */
41#define CMD_M25PXX_READ 0x03 /* Read Data Bytes */
42#define CMD_M25PXX_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */
43#define CMD_M25PXX_PP 0x02 /* Page Program */
44#define CMD_M25PXX_SE 0xd8 /* Sector Erase */
45#define CMD_M25PXX_BE 0xc7 /* Bulk Erase */
46#define CMD_M25PXX_DP 0xb9 /* Deep Power-down */
47#define CMD_M25PXX_RES 0xab /* Release from DP, and Read Signature */
48
Thomas Chou12c2e3b2010-04-30 21:11:20 +080049#define STM_ID_M25P10 0x11
TsiChung Liew7b7a8692008-08-06 16:08:41 -050050#define STM_ID_M25P16 0x15
51#define STM_ID_M25P20 0x12
52#define STM_ID_M25P32 0x16
53#define STM_ID_M25P40 0x13
54#define STM_ID_M25P64 0x17
55#define STM_ID_M25P80 0x14
56#define STM_ID_M25P128 0x18
57
TsiChung Liew7b7a8692008-08-06 16:08:41 -050058struct stmicro_spi_flash_params {
59 u8 idcode1;
60 u16 page_size;
61 u16 pages_per_sector;
62 u16 nr_sectors;
63 const char *name;
64};
65
TsiChung Liew7b7a8692008-08-06 16:08:41 -050066static const struct stmicro_spi_flash_params stmicro_spi_flash_table[] = {
67 {
Thomas Chou12c2e3b2010-04-30 21:11:20 +080068 .idcode1 = STM_ID_M25P10,
69 .page_size = 256,
70 .pages_per_sector = 128,
71 .nr_sectors = 4,
72 .name = "M25P10",
73 },
74 {
TsiChung Liew7b7a8692008-08-06 16:08:41 -050075 .idcode1 = STM_ID_M25P16,
76 .page_size = 256,
77 .pages_per_sector = 256,
78 .nr_sectors = 32,
79 .name = "M25P16",
80 },
81 {
82 .idcode1 = STM_ID_M25P20,
83 .page_size = 256,
84 .pages_per_sector = 256,
85 .nr_sectors = 4,
86 .name = "M25P20",
87 },
88 {
89 .idcode1 = STM_ID_M25P32,
90 .page_size = 256,
91 .pages_per_sector = 256,
92 .nr_sectors = 64,
93 .name = "M25P32",
94 },
95 {
96 .idcode1 = STM_ID_M25P40,
97 .page_size = 256,
98 .pages_per_sector = 256,
99 .nr_sectors = 8,
100 .name = "M25P40",
101 },
102 {
103 .idcode1 = STM_ID_M25P64,
104 .page_size = 256,
105 .pages_per_sector = 256,
106 .nr_sectors = 128,
107 .name = "M25P64",
108 },
109 {
110 .idcode1 = STM_ID_M25P80,
111 .page_size = 256,
112 .pages_per_sector = 256,
113 .nr_sectors = 16,
114 .name = "M25P80",
115 },
116 {
117 .idcode1 = STM_ID_M25P128,
118 .page_size = 256,
119 .pages_per_sector = 1024,
120 .nr_sectors = 64,
121 .name = "M25P128",
122 },
123};
124
Mike Frysingerf8f07572011-04-12 01:51:29 -0400125static int stmicro_erase(struct spi_flash *flash, u32 offset, size_t len)
TsiChung Liew7b7a8692008-08-06 16:08:41 -0500126{
Richard Retanubun4e6a5152011-02-16 16:37:22 -0500127 return spi_flash_cmd_erase(flash, CMD_M25PXX_SE, offset, len);
TsiChung Liew7b7a8692008-08-06 16:08:41 -0500128}
129
130struct spi_flash *spi_flash_probe_stmicro(struct spi_slave *spi, u8 * idcode)
131{
132 const struct stmicro_spi_flash_params *params;
Mike Frysingerb06afa72011-06-28 07:38:10 +0000133 struct spi_flash *flash;
TsiChung Liew7b7a8692008-08-06 16:08:41 -0500134 unsigned int i;
TsiChung Liew7b7a8692008-08-06 16:08:41 -0500135
Thomas Chou12c2e3b2010-04-30 21:11:20 +0800136 if (idcode[0] == 0xff) {
137 i = spi_flash_cmd(spi, CMD_M25PXX_RES,
138 idcode, 4);
139 if (i)
140 return NULL;
141 if ((idcode[3] & 0xf0) == 0x10) {
142 idcode[0] = 0x20;
143 idcode[1] = 0x20;
144 idcode[2] = idcode[3] + 1;
145 } else
146 return NULL;
147 }
148
TsiChung Liew7b7a8692008-08-06 16:08:41 -0500149 for (i = 0; i < ARRAY_SIZE(stmicro_spi_flash_table); i++) {
150 params = &stmicro_spi_flash_table[i];
151 if (params->idcode1 == idcode[2]) {
152 break;
153 }
154 }
155
156 if (i == ARRAY_SIZE(stmicro_spi_flash_table)) {
Mike Frysinger9726ba42009-03-27 16:34:21 -0400157 debug("SF: Unsupported STMicro ID %02x\n", idcode[1]);
TsiChung Liew7b7a8692008-08-06 16:08:41 -0500158 return NULL;
159 }
160
Mike Frysingerb06afa72011-06-28 07:38:10 +0000161 flash = malloc(sizeof(*flash));
162 if (!flash) {
TsiChung Liew7b7a8692008-08-06 16:08:41 -0500163 debug("SF: Failed to allocate memory\n");
164 return NULL;
165 }
166
Mike Frysingerb06afa72011-06-28 07:38:10 +0000167 flash->spi = spi;
168 flash->name = params->name;
TsiChung Liew7b7a8692008-08-06 16:08:41 -0500169
Mike Frysingerb06afa72011-06-28 07:38:10 +0000170 flash->write = spi_flash_cmd_write_multi;
171 flash->erase = stmicro_erase;
172 flash->read = spi_flash_cmd_read_fast;
173 flash->page_size = params->page_size;
174 flash->sector_size = params->page_size * params->pages_per_sector;
175 flash->size = flash->sector_size * params->nr_sectors;
TsiChung Liew7b7a8692008-08-06 16:08:41 -0500176
Mike Frysingerb06afa72011-06-28 07:38:10 +0000177 return flash;
TsiChung Liew7b7a8692008-08-06 16:08:41 -0500178}