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 struct macronix_spi_flash_params {
56 u16 sectors_per_block;
61 struct macronix_spi_flash {
62 struct spi_flash flash;
63 const struct macronix_spi_flash_params *params;
66 static inline struct macronix_spi_flash *to_macronix_spi_flash(struct spi_flash
69 return container_of(flash, struct macronix_spi_flash, flash);
72 static const struct macronix_spi_flash_params macronix_spi_flash_table[] = {
76 .pages_per_sector = 16,
77 .sectors_per_block = 16,
84 .pages_per_sector = 16,
85 .sectors_per_block = 16,
92 .pages_per_sector = 16,
93 .sectors_per_block = 16,
100 .pages_per_sector = 16,
101 .sectors_per_block = 16,
103 .name = "MX25L12805D",
108 .pages_per_sector = 16,
109 .sectors_per_block = 16,
111 .name = "MX25L12855E",
115 static int macronix_read_fast(struct spi_flash *flash,
116 u32 offset, size_t len, void *buf)
118 struct macronix_spi_flash *mcx = to_macronix_spi_flash(flash);
119 unsigned long page_addr;
120 unsigned long page_size;
123 page_size = mcx->params->page_size;
124 page_addr = offset / page_size;
126 cmd[0] = CMD_READ_ARRAY_FAST;
127 cmd[1] = page_addr >> 8;
129 cmd[3] = offset % page_size;
132 return spi_flash_read_common(flash, cmd, sizeof(cmd), buf, len);
135 static int macronix_write(struct spi_flash *flash,
136 u32 offset, size_t len, const void *buf)
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;
147 page_size = mcx->params->page_size;
148 page_addr = offset / page_size;
149 byte_addr = offset % page_size;
151 ret = spi_claim_bus(flash->spi);
153 debug("SF: Unable to claim SPI bus\n");
158 for (actual = 0; actual < len; actual += chunk_len) {
159 chunk_len = min(len - actual, page_size - byte_addr);
161 cmd[0] = CMD_MX25XX_PP;
162 cmd[1] = page_addr >> 8;
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);
170 ret = spi_flash_cmd(flash->spi, CMD_MX25XX_WREN, NULL, 0);
172 debug("SF: Enabling Write failed\n");
176 ret = spi_flash_cmd_write(flash->spi, cmd, 4,
177 buf + actual, chunk_len);
179 debug("SF: Macronix Page Program failed\n");
183 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
191 debug("SF: Macronix: Successfully programmed %u bytes @ 0x%x\n",
194 spi_release_bus(flash->spi);
198 int macronix_erase(struct spi_flash *flash, u32 offset, size_t len)
200 struct macronix_spi_flash *mcx = to_macronix_spi_flash(flash);
201 return spi_flash_cmd_erase(flash, CMD_MX25XX_BE,
202 mcx->params->page_size * mcx->params->pages_per_sector *
203 mcx->params->sectors_per_block,
207 struct spi_flash *spi_flash_probe_macronix(struct spi_slave *spi, u8 *idcode)
209 const struct macronix_spi_flash_params *params;
210 struct macronix_spi_flash *mcx;
212 u16 id = idcode[2] | idcode[1] << 8;
214 for (i = 0; i < ARRAY_SIZE(macronix_spi_flash_table); i++) {
215 params = ¯onix_spi_flash_table[i];
216 if (params->idcode == id)
220 if (i == ARRAY_SIZE(macronix_spi_flash_table)) {
221 debug("SF: Unsupported Macronix ID %04x\n", id);
225 mcx = malloc(sizeof(*mcx));
227 debug("SF: Failed to allocate memory\n");
231 mcx->params = params;
232 mcx->flash.spi = spi;
233 mcx->flash.name = params->name;
235 mcx->flash.write = macronix_write;
236 mcx->flash.erase = macronix_erase;
237 mcx->flash.read = macronix_read_fast;
238 mcx->flash.size = params->page_size * params->pages_per_sector
239 * params->sectors_per_block * params->nr_blocks;
241 printf("SF: Detected %s with page size %u, total ",
242 params->name, params->page_size);
243 print_size(mcx->flash.size, "\n");