sf: unify erase functions
[platform/kernel/u-boot.git] / drivers / mtd / spi / stmicro.c
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
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
49 #define STM_ID_M25P10           0x11
50 #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
58 struct 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
66 /* spi_flash needs to be first so upper layers can free() it */
67 struct stmicro_spi_flash {
68         struct spi_flash flash;
69         const struct stmicro_spi_flash_params *params;
70 };
71
72 static inline struct stmicro_spi_flash *to_stmicro_spi_flash(struct spi_flash
73                                                              *flash)
74 {
75         return container_of(flash, struct stmicro_spi_flash, flash);
76 }
77
78 static const struct stmicro_spi_flash_params stmicro_spi_flash_table[] = {
79         {
80                 .idcode1 = STM_ID_M25P10,
81                 .page_size = 256,
82                 .pages_per_sector = 128,
83                 .nr_sectors = 4,
84                 .name = "M25P10",
85         },
86         {
87                 .idcode1 = STM_ID_M25P16,
88                 .page_size = 256,
89                 .pages_per_sector = 256,
90                 .nr_sectors = 32,
91                 .name = "M25P16",
92         },
93         {
94                 .idcode1 = STM_ID_M25P20,
95                 .page_size = 256,
96                 .pages_per_sector = 256,
97                 .nr_sectors = 4,
98                 .name = "M25P20",
99         },
100         {
101                 .idcode1 = STM_ID_M25P32,
102                 .page_size = 256,
103                 .pages_per_sector = 256,
104                 .nr_sectors = 64,
105                 .name = "M25P32",
106         },
107         {
108                 .idcode1 = STM_ID_M25P40,
109                 .page_size = 256,
110                 .pages_per_sector = 256,
111                 .nr_sectors = 8,
112                 .name = "M25P40",
113         },
114         {
115                 .idcode1 = STM_ID_M25P64,
116                 .page_size = 256,
117                 .pages_per_sector = 256,
118                 .nr_sectors = 128,
119                 .name = "M25P64",
120         },
121         {
122                 .idcode1 = STM_ID_M25P80,
123                 .page_size = 256,
124                 .pages_per_sector = 256,
125                 .nr_sectors = 16,
126                 .name = "M25P80",
127         },
128         {
129                 .idcode1 = STM_ID_M25P128,
130                 .page_size = 256,
131                 .pages_per_sector = 1024,
132                 .nr_sectors = 64,
133                 .name = "M25P128",
134         },
135 };
136
137 static int stmicro_read_fast(struct spi_flash *flash,
138                              u32 offset, size_t len, void *buf)
139 {
140         struct stmicro_spi_flash *stm = to_stmicro_spi_flash(flash);
141         unsigned long page_addr;
142         unsigned long page_size;
143         u8 cmd[5];
144
145         page_size = stm->params->page_size;
146         page_addr = offset / page_size;
147
148         cmd[0] = CMD_READ_ARRAY_FAST;
149         cmd[1] = page_addr >> 8;
150         cmd[2] = page_addr;
151         cmd[3] = offset % page_size;
152         cmd[4] = 0x00;
153
154         return spi_flash_read_common(flash, cmd, sizeof(cmd), buf, len);
155 }
156
157 static int stmicro_write(struct spi_flash *flash,
158                          u32 offset, size_t len, const void *buf)
159 {
160         struct stmicro_spi_flash *stm = to_stmicro_spi_flash(flash);
161         unsigned long page_addr;
162         unsigned long byte_addr;
163         unsigned long page_size;
164         size_t chunk_len;
165         size_t actual;
166         int ret;
167         u8 cmd[4];
168
169         page_size = stm->params->page_size;
170         page_addr = offset / page_size;
171         byte_addr = offset % page_size;
172
173         ret = spi_claim_bus(flash->spi);
174         if (ret) {
175                 debug("SF: Unable to claim SPI bus\n");
176                 return ret;
177         }
178
179         ret = 0;
180         for (actual = 0; actual < len; actual += chunk_len) {
181                 chunk_len = min(len - actual, page_size - byte_addr);
182
183                 cmd[0] = CMD_M25PXX_PP;
184                 cmd[1] = page_addr >> 8;
185                 cmd[2] = page_addr;
186                 cmd[3] = byte_addr;
187
188                 debug
189                     ("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %d\n",
190                      buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
191
192                 ret = spi_flash_cmd(flash->spi, CMD_M25PXX_WREN, NULL, 0);
193                 if (ret < 0) {
194                         debug("SF: Enabling Write failed\n");
195                         break;
196                 }
197
198                 ret = spi_flash_cmd_write(flash->spi, cmd, 4,
199                                           buf + actual, chunk_len);
200                 if (ret < 0) {
201                         debug("SF: STMicro Page Program failed\n");
202                         break;
203                 }
204
205                 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
206                 if (ret)
207                         break;
208
209                 page_addr++;
210                 byte_addr = 0;
211         }
212
213         debug("SF: STMicro: Successfully programmed %u bytes @ 0x%x\n",
214               len, offset);
215
216         spi_release_bus(flash->spi);
217         return ret;
218 }
219
220 int stmicro_erase(struct spi_flash *flash, u32 offset, size_t len)
221 {
222         struct stmicro_spi_flash *stm = to_stmicro_spi_flash(flash);
223         return spi_flash_cmd_erase(flash, CMD_M25PXX_SE,
224                 stm->params->page_size * stm->params->pages_per_sector,
225                 offset, len);
226 }
227
228 struct spi_flash *spi_flash_probe_stmicro(struct spi_slave *spi, u8 * idcode)
229 {
230         const struct stmicro_spi_flash_params *params;
231         struct stmicro_spi_flash *stm;
232         unsigned int i;
233
234         if (idcode[0] == 0xff) {
235                 i = spi_flash_cmd(spi, CMD_M25PXX_RES,
236                                   idcode, 4);
237                 if (i)
238                         return NULL;
239                 if ((idcode[3] & 0xf0) == 0x10) {
240                         idcode[0] = 0x20;
241                         idcode[1] = 0x20;
242                         idcode[2] = idcode[3] + 1;
243                 } else
244                         return NULL;
245         }
246
247         for (i = 0; i < ARRAY_SIZE(stmicro_spi_flash_table); i++) {
248                 params = &stmicro_spi_flash_table[i];
249                 if (params->idcode1 == idcode[2]) {
250                         break;
251                 }
252         }
253
254         if (i == ARRAY_SIZE(stmicro_spi_flash_table)) {
255                 debug("SF: Unsupported STMicro ID %02x\n", idcode[1]);
256                 return NULL;
257         }
258
259         stm = malloc(sizeof(struct stmicro_spi_flash));
260         if (!stm) {
261                 debug("SF: Failed to allocate memory\n");
262                 return NULL;
263         }
264
265         stm->params = params;
266         stm->flash.spi = spi;
267         stm->flash.name = params->name;
268
269         stm->flash.write = stmicro_write;
270         stm->flash.erase = stmicro_erase;
271         stm->flash.read = stmicro_read_fast;
272         stm->flash.size = params->page_size * params->pages_per_sector
273             * params->nr_sectors;
274
275         printf("SF: Detected %s with page size %u, total ",
276                params->name, params->page_size);
277         print_size(stm->flash.size, "\n");
278
279         return &stm->flash;
280 }