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 struct macronix_spi_flash_params {
44 static const struct macronix_spi_flash_params macronix_spi_flash_table[] = {
73 .name = "MX25L12805D",
78 .name = "MX25L12855E",
82 static int macronix_write_status(struct spi_flash *flash, u8 sr)
87 ret = spi_flash_cmd_write_enable(flash);
89 debug("SF: enabling write failed\n");
93 cmd = CMD_WRITE_STATUS;
94 ret = spi_flash_cmd_write(flash->spi, &cmd, 1, &sr, 1);
96 debug("SF: fail to write status register\n");
100 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
102 debug("SF: write status register timed out\n");
109 static int macronix_unlock(struct spi_flash *flash)
113 /* Enable status register writing and clear BP# bits */
114 ret = macronix_write_status(flash, 0);
116 debug("SF: fail to disable write protection\n");
121 struct spi_flash *spi_flash_probe_macronix(struct spi_slave *spi, u8 *idcode)
123 const struct macronix_spi_flash_params *params;
124 struct spi_flash *flash;
126 u16 id = idcode[2] | idcode[1] << 8;
128 for (i = 0; i < ARRAY_SIZE(macronix_spi_flash_table); i++) {
129 params = ¯onix_spi_flash_table[i];
130 if (params->idcode == id)
134 if (i == ARRAY_SIZE(macronix_spi_flash_table)) {
135 debug("SF: Unsupported Macronix ID %04x\n", id);
139 flash = malloc(sizeof(*flash));
141 debug("SF: Failed to allocate memory\n");
146 flash->name = params->name;
148 flash->write = spi_flash_cmd_write_multi;
149 flash->erase = spi_flash_cmd_erase;
150 flash->read = spi_flash_cmd_read_fast;
151 flash->page_size = 256;
152 flash->sector_size = 256 * 16 * 16;
153 flash->size = flash->sector_size * params->nr_blocks;
155 /* Clear BP# bits for read-only flash */
156 macronix_unlock(flash);