blob: 291fd17de9fc8b65665c2da28a7ba39ef31f6c9a [file] [log] [blame]
Prafulla Wadaskar7ce60312009-04-06 21:24:43 +05301/*
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
32#include <common.h>
33#include <malloc.h>
34#include <spi_flash.h>
35
36#include "spi_flash_internal.h"
37
38/* MX25xx-specific commands */
39#define CMD_MX25XX_WREN 0x06 /* Write Enable */
40#define CMD_MX25XX_WRDI 0x04 /* Write Disable */
41#define CMD_MX25XX_RDSR 0x05 /* Read Status Register */
42#define CMD_MX25XX_WRSR 0x01 /* Write Status Register */
43#define CMD_MX25XX_READ 0x03 /* Read Data Bytes */
44#define CMD_MX25XX_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */
45#define CMD_MX25XX_PP 0x02 /* Page Program */
46#define CMD_MX25XX_SE 0x20 /* Sector Erase */
47#define CMD_MX25XX_BE 0xD8 /* Block Erase */
48#define CMD_MX25XX_CE 0xc7 /* Chip Erase */
49#define CMD_MX25XX_DP 0xb9 /* Deep Power-down */
50#define CMD_MX25XX_RES 0xab /* Release from DP, and Read Signature */
51
Prafulla Wadaskar7ce60312009-04-06 21:24:43 +053052struct macronix_spi_flash_params {
Prafulla Wadaskar2efee522009-07-06 20:29:15 +053053 u16 idcode;
Prafulla Wadaskar7ce60312009-04-06 21:24:43 +053054 u16 page_size;
55 u16 pages_per_sector;
56 u16 sectors_per_block;
57 u16 nr_blocks;
58 const char *name;
59};
60
61struct macronix_spi_flash {
62 struct spi_flash flash;
63 const struct macronix_spi_flash_params *params;
64};
65
66static inline struct macronix_spi_flash *to_macronix_spi_flash(struct spi_flash
67 *flash)
68{
69 return container_of(flash, struct macronix_spi_flash, flash);
70}
71
72static const struct macronix_spi_flash_params macronix_spi_flash_table[] = {
73 {
Prafulla Wadaskar2efee522009-07-06 20:29:15 +053074 .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,
Prafulla Wadaskar7ce60312009-04-06 21:24:43 +053099 .page_size = 256,
100 .pages_per_sector = 16,
101 .sectors_per_block = 16,
102 .nr_blocks = 256,
103 .name = "MX25L12805D",
104 },
Prafulla Wadaskar2efee522009-07-06 20:29:15 +0530105 {
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 },
Prafulla Wadaskar7ce60312009-04-06 21:24:43 +0530113};
114
Prafulla Wadaskar7ce60312009-04-06 21:24:43 +0530115static int macronix_read_fast(struct spi_flash *flash,
116 u32 offset, size_t len, void *buf)
117{
118 struct macronix_spi_flash *mcx = to_macronix_spi_flash(flash);
119 unsigned long page_addr;
120 unsigned long page_size;
121 u8 cmd[5];
122
123 page_size = mcx->params->page_size;
124 page_addr = offset / page_size;
125
126 cmd[0] = CMD_READ_ARRAY_FAST;
127 cmd[1] = page_addr >> 8;
128 cmd[2] = page_addr;
129 cmd[3] = offset % page_size;
130 cmd[4] = 0x00;
131
132 return spi_flash_read_common(flash, cmd, sizeof(cmd), buf, len);
133}
134
135static int macronix_write(struct spi_flash *flash,
136 u32 offset, size_t len, const void *buf)
137{
138 struct macronix_spi_flash *mcx = to_macronix_spi_flash(flash);
139 unsigned long page_addr;
140 unsigned long byte_addr;
141 unsigned long page_size;
142 size_t chunk_len;
143 size_t actual;
144 int ret;
145 u8 cmd[4];
146
147 page_size = mcx->params->page_size;
148 page_addr = offset / page_size;
149 byte_addr = offset % page_size;
150
151 ret = spi_claim_bus(flash->spi);
152 if (ret) {
153 debug("SF: Unable to claim SPI bus\n");
154 return ret;
155 }
156
157 ret = 0;
158 for (actual = 0; actual < len; actual += chunk_len) {
159 chunk_len = min(len - actual, page_size - byte_addr);
160
161 cmd[0] = CMD_MX25XX_PP;
162 cmd[1] = page_addr >> 8;
163 cmd[2] = page_addr;
164 cmd[3] = byte_addr;
165
166 debug
167 ("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %d\n",
168 buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
169
170 ret = spi_flash_cmd(flash->spi, CMD_MX25XX_WREN, NULL, 0);
171 if (ret < 0) {
172 debug("SF: Enabling Write failed\n");
173 break;
174 }
175
176 ret = spi_flash_cmd_write(flash->spi, cmd, 4,
177 buf + actual, chunk_len);
178 if (ret < 0) {
179 debug("SF: Macronix Page Program failed\n");
180 break;
181 }
182
Mike Frysinger61630452011-01-10 02:20:12 -0500183 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
184 if (ret)
Prafulla Wadaskar7ce60312009-04-06 21:24:43 +0530185 break;
Prafulla Wadaskar7ce60312009-04-06 21:24:43 +0530186
187 page_addr++;
188 byte_addr = 0;
189 }
190
191 debug("SF: Macronix: Successfully programmed %u bytes @ 0x%x\n",
192 len, offset);
193
194 spi_release_bus(flash->spi);
195 return ret;
196}
197
198int macronix_erase(struct spi_flash *flash, u32 offset, size_t len)
199{
200 struct macronix_spi_flash *mcx = to_macronix_spi_flash(flash);
201 unsigned long sector_size;
202 size_t actual;
203 int ret;
204 u8 cmd[4];
205
206 /*
207 * This function currently uses sector erase only.
208 * probably speed things up by using bulk erase
209 * when possible.
210 */
211
212 sector_size = mcx->params->page_size * mcx->params->pages_per_sector
213 * mcx->params->sectors_per_block;
214
215 if (offset % sector_size || len % sector_size) {
216 debug("SF: Erase offset/length not multiple of sector size\n");
217 return -1;
218 }
219
220 len /= sector_size;
221 cmd[0] = CMD_MX25XX_BE;
222 cmd[2] = 0x00;
223 cmd[3] = 0x00;
224
225 ret = spi_claim_bus(flash->spi);
226 if (ret) {
227 debug("SF: Unable to claim SPI bus\n");
228 return ret;
229 }
230
231 ret = 0;
232 for (actual = 0; actual < len; actual++) {
233 cmd[1] = (offset / sector_size) + actual;
234
235 ret = spi_flash_cmd(flash->spi, CMD_MX25XX_WREN, NULL, 0);
236 if (ret < 0) {
237 debug("SF: Enabling Write failed\n");
238 break;
239 }
240
241 ret = spi_flash_cmd_write(flash->spi, cmd, 4, NULL, 0);
242 if (ret < 0) {
243 debug("SF: Macronix page erase failed\n");
244 break;
245 }
246
Mike Frysinger61630452011-01-10 02:20:12 -0500247 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
248 if (ret)
Prafulla Wadaskar7ce60312009-04-06 21:24:43 +0530249 break;
Prafulla Wadaskar7ce60312009-04-06 21:24:43 +0530250 }
251
252 debug("SF: Macronix: Successfully erased %u bytes @ 0x%x\n",
253 len * sector_size, offset);
254
255 spi_release_bus(flash->spi);
256 return ret;
257}
258
259struct spi_flash *spi_flash_probe_macronix(struct spi_slave *spi, u8 *idcode)
260{
261 const struct macronix_spi_flash_params *params;
262 struct macronix_spi_flash *mcx;
263 unsigned int i;
Prafulla Wadaskar2efee522009-07-06 20:29:15 +0530264 u16 id = idcode[2] | idcode[1] << 8;
Prafulla Wadaskar7ce60312009-04-06 21:24:43 +0530265
266 for (i = 0; i < ARRAY_SIZE(macronix_spi_flash_table); i++) {
267 params = &macronix_spi_flash_table[i];
Prafulla Wadaskar2efee522009-07-06 20:29:15 +0530268 if (params->idcode == id)
Prafulla Wadaskar7ce60312009-04-06 21:24:43 +0530269 break;
270 }
271
272 if (i == ARRAY_SIZE(macronix_spi_flash_table)) {
Prafulla Wadaskar2efee522009-07-06 20:29:15 +0530273 debug("SF: Unsupported Macronix ID %04x\n", id);
Prafulla Wadaskar7ce60312009-04-06 21:24:43 +0530274 return NULL;
275 }
276
277 mcx = malloc(sizeof(*mcx));
278 if (!mcx) {
279 debug("SF: Failed to allocate memory\n");
280 return NULL;
281 }
282
283 mcx->params = params;
284 mcx->flash.spi = spi;
285 mcx->flash.name = params->name;
286
287 mcx->flash.write = macronix_write;
288 mcx->flash.erase = macronix_erase;
289 mcx->flash.read = macronix_read_fast;
290 mcx->flash.size = params->page_size * params->pages_per_sector
291 * params->sectors_per_block * params->nr_blocks;
292
Mike Frysingerb376bbb2010-04-29 00:35:12 -0400293 printf("SF: Detected %s with page size %u, total ",
294 params->name, params->page_size);
295 print_size(mcx->flash.size, "\n");
Prafulla Wadaskar7ce60312009-04-06 21:24:43 +0530296
297 return &mcx->flash;
298}