2 * Copyright 2009(C) Marvell International Ltd. and its affiliates
3 * Prafulla Wadaskar <prafulla@marvell.com>
5 * Based on drivers/mtd/spi/stmicro.c
7 * Copyright 2008, Network Appliance Inc.
8 * Jason McMullan <mcmullan@netapp.com>
10 * Copyright (C) 2004-2007 Freescale Semiconductor, Inc.
11 * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
13 * See file CREDITS for list of people who contributed to this
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.
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.
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,
34 #include <spi_flash.h>
36 #include "spi_flash_internal.h"
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 */
52 #define MXIC_ID_MX2516 0x15
53 #define MXIC_ID_MX2520 0x12
54 #define MXIC_ID_MX2532 0x16
55 #define MXIC_ID_MX2540 0x13
56 #define MXIC_ID_MX2564 0x17
57 #define MXIC_ID_MX2580 0x14
58 #define MXIC_ID_MX25128 0x18
60 #define MACRONIX_SR_WIP (1 << 0) /* Write-in-Progress */
62 struct macronix_spi_flash_params {
66 u16 sectors_per_block;
71 struct macronix_spi_flash {
72 struct spi_flash flash;
73 const struct macronix_spi_flash_params *params;
76 static inline struct macronix_spi_flash *to_macronix_spi_flash(struct spi_flash
79 return container_of(flash, struct macronix_spi_flash, flash);
82 static const struct macronix_spi_flash_params macronix_spi_flash_table[] = {
84 .idcode1 = MXIC_ID_MX25128,
86 .pages_per_sector = 16,
87 .sectors_per_block = 16,
89 .name = "MX25L12805D",
93 static int macronix_wait_ready(struct spi_flash *flash, unsigned long timeout)
95 struct spi_slave *spi = flash->spi;
96 unsigned long timebase;
99 u8 cmd = CMD_MX25XX_RDSR;
101 ret = spi_xfer(spi, 8, &cmd, NULL, SPI_XFER_BEGIN);
103 debug("SF: Failed to send command %02x: %d\n", cmd, ret);
107 timebase = get_timer(0);
109 ret = spi_xfer(spi, 8, NULL, &status, 0);
113 if ((status & MACRONIX_SR_WIP) == 0)
116 } while (get_timer(timebase) < timeout);
118 spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END);
120 if ((status & MACRONIX_SR_WIP) == 0)
127 static int macronix_read_fast(struct spi_flash *flash,
128 u32 offset, size_t len, void *buf)
130 struct macronix_spi_flash *mcx = to_macronix_spi_flash(flash);
131 unsigned long page_addr;
132 unsigned long page_size;
135 page_size = mcx->params->page_size;
136 page_addr = offset / page_size;
138 cmd[0] = CMD_READ_ARRAY_FAST;
139 cmd[1] = page_addr >> 8;
141 cmd[3] = offset % page_size;
144 return spi_flash_read_common(flash, cmd, sizeof(cmd), buf, len);
147 static int macronix_write(struct spi_flash *flash,
148 u32 offset, size_t len, const void *buf)
150 struct macronix_spi_flash *mcx = to_macronix_spi_flash(flash);
151 unsigned long page_addr;
152 unsigned long byte_addr;
153 unsigned long page_size;
159 page_size = mcx->params->page_size;
160 page_addr = offset / page_size;
161 byte_addr = offset % page_size;
163 ret = spi_claim_bus(flash->spi);
165 debug("SF: Unable to claim SPI bus\n");
170 for (actual = 0; actual < len; actual += chunk_len) {
171 chunk_len = min(len - actual, page_size - byte_addr);
173 cmd[0] = CMD_MX25XX_PP;
174 cmd[1] = page_addr >> 8;
179 ("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %d\n",
180 buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
182 ret = spi_flash_cmd(flash->spi, CMD_MX25XX_WREN, NULL, 0);
184 debug("SF: Enabling Write failed\n");
188 ret = spi_flash_cmd_write(flash->spi, cmd, 4,
189 buf + actual, chunk_len);
191 debug("SF: Macronix Page Program failed\n");
195 ret = macronix_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
197 debug("SF: Macronix page programming timed out\n");
205 debug("SF: Macronix: Successfully programmed %u bytes @ 0x%x\n",
208 spi_release_bus(flash->spi);
212 int macronix_erase(struct spi_flash *flash, u32 offset, size_t len)
214 struct macronix_spi_flash *mcx = to_macronix_spi_flash(flash);
215 unsigned long sector_size;
221 * This function currently uses sector erase only.
222 * probably speed things up by using bulk erase
226 sector_size = mcx->params->page_size * mcx->params->pages_per_sector
227 * mcx->params->sectors_per_block;
229 if (offset % sector_size || len % sector_size) {
230 debug("SF: Erase offset/length not multiple of sector size\n");
235 cmd[0] = CMD_MX25XX_BE;
239 ret = spi_claim_bus(flash->spi);
241 debug("SF: Unable to claim SPI bus\n");
246 for (actual = 0; actual < len; actual++) {
247 cmd[1] = (offset / sector_size) + actual;
249 ret = spi_flash_cmd(flash->spi, CMD_MX25XX_WREN, NULL, 0);
251 debug("SF: Enabling Write failed\n");
255 ret = spi_flash_cmd_write(flash->spi, cmd, 4, NULL, 0);
257 debug("SF: Macronix page erase failed\n");
261 ret = macronix_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
263 debug("SF: Macronix page erase timed out\n");
268 debug("SF: Macronix: Successfully erased %u bytes @ 0x%x\n",
269 len * sector_size, offset);
271 spi_release_bus(flash->spi);
275 struct spi_flash *spi_flash_probe_macronix(struct spi_slave *spi, u8 *idcode)
277 const struct macronix_spi_flash_params *params;
278 struct macronix_spi_flash *mcx;
281 for (i = 0; i < ARRAY_SIZE(macronix_spi_flash_table); i++) {
282 params = ¯onix_spi_flash_table[i];
283 if (params->idcode1 == idcode[2])
287 if (i == ARRAY_SIZE(macronix_spi_flash_table)) {
288 debug("SF: Unsupported Macronix ID %02x\n", idcode[1]);
292 mcx = malloc(sizeof(*mcx));
294 debug("SF: Failed to allocate memory\n");
298 mcx->params = params;
299 mcx->flash.spi = spi;
300 mcx->flash.name = params->name;
302 mcx->flash.write = macronix_write;
303 mcx->flash.erase = macronix_erase;
304 mcx->flash.read = macronix_read_fast;
305 mcx->flash.size = params->page_size * params->pages_per_sector
306 * params->sectors_per_block * params->nr_blocks;
308 printf("SF: Detected %s with page size %u, total %u bytes\n",
309 params->name, params->page_size, mcx->flash.size);