2012-05-10 20:27:32 +02:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2000-2002
|
|
|
|
* Wolfgang Denk, DENX Software Engineering, wd@denx.de.
|
|
|
|
*
|
|
|
|
* Copyright 2008, Network Appliance Inc.
|
|
|
|
* Jason McMullan <mcmullan@netapp.com>
|
|
|
|
*
|
|
|
|
* Copyright (C) 2004-2007 Freescale Semiconductor, Inc.
|
|
|
|
* TsiChung Liew (Tsi-Chung.Liew@freescale.com)
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of
|
|
|
|
* the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2013-02-20 13:21:20 +01:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2012-05-10 20:27:32 +02:00
|
|
|
* GNU General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <spi_flash.h>
|
2014-06-26 07:02:40 +02:00
|
|
|
|
2012-05-10 20:27:32 +02:00
|
|
|
#include "spi_flash_internal.h"
|
|
|
|
|
|
|
|
/* M25Pxx-specific commands */
|
|
|
|
#define CMD_M25PXX_WREN 0x06 /* Write Enable */
|
|
|
|
#define CMD_M25PXX_WRDI 0x04 /* Write Disable */
|
|
|
|
#define CMD_M25PXX_RDSR 0x05 /* Read Status Register */
|
|
|
|
#define CMD_M25PXX_WRSR 0x01 /* Write Status Register */
|
|
|
|
#define CMD_M25PXX_READ 0x03 /* Read Data Bytes */
|
|
|
|
#define CMD_M25PXX_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */
|
|
|
|
#define CMD_M25PXX_PP 0x02 /* Page Program */
|
2014-10-14 21:40:59 +02:00
|
|
|
#define CMD_M25PXX_SSE 0x20 /* Subsector Erase */
|
2012-05-10 20:27:32 +02:00
|
|
|
#define CMD_M25PXX_SE 0xd8 /* Sector Erase */
|
|
|
|
#define CMD_M25PXX_BE 0xc7 /* Bulk Erase */
|
|
|
|
#define CMD_M25PXX_DP 0xb9 /* Deep Power-down */
|
|
|
|
#define CMD_M25PXX_RES 0xab /* Release from DP, and Read Signature */
|
|
|
|
|
2015-02-17 22:35:20 +01:00
|
|
|
/*
|
|
|
|
* Device ID = (memory_type << 8) + memory_capacity
|
|
|
|
*/
|
|
|
|
#define STM_ID_M25P10 0x2011
|
|
|
|
#define STM_ID_M25P16 0x2015
|
|
|
|
#define STM_ID_M25P20 0x2012
|
|
|
|
#define STM_ID_M25P32 0x2016
|
|
|
|
#define STM_ID_M25P40 0x2013
|
|
|
|
#define STM_ID_M25P64 0x2017
|
|
|
|
#define STM_ID_M25P80 0x2014
|
|
|
|
#define STM_ID_M25P128 0x2018
|
2015-05-03 16:57:38 +02:00
|
|
|
#define STM_ID_N25Q256 0xba19
|
2015-05-03 16:02:56 +02:00
|
|
|
#define STM_ID_N25Q064 0xbb17
|
2015-02-17 22:35:20 +01:00
|
|
|
#define STM_ID_N25Q128 0xbb18
|
2012-05-10 20:27:32 +02:00
|
|
|
|
|
|
|
struct stmicro_spi_flash_params {
|
2015-02-17 22:35:20 +01:00
|
|
|
u16 device_id;
|
2014-10-14 21:40:59 +02:00
|
|
|
u8 op_erase;
|
2012-05-10 20:27:32 +02:00
|
|
|
u16 page_size;
|
|
|
|
u16 pages_per_sector;
|
|
|
|
u16 nr_sectors;
|
|
|
|
const char *name;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* spi_flash needs to be first so upper layers can free() it */
|
|
|
|
struct stmicro_spi_flash {
|
|
|
|
struct spi_flash flash;
|
|
|
|
const struct stmicro_spi_flash_params *params;
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline struct stmicro_spi_flash *to_stmicro_spi_flash(struct spi_flash
|
|
|
|
*flash)
|
|
|
|
{
|
|
|
|
return container_of(flash, struct stmicro_spi_flash, flash);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct stmicro_spi_flash_params stmicro_spi_flash_table[] = {
|
|
|
|
{
|
2015-02-17 22:35:20 +01:00
|
|
|
.device_id = STM_ID_M25P10,
|
2014-10-14 21:40:59 +02:00
|
|
|
.op_erase = CMD_M25PXX_SE,
|
2012-05-10 20:27:32 +02:00
|
|
|
.page_size = 256,
|
|
|
|
.pages_per_sector = 128,
|
|
|
|
.nr_sectors = 4,
|
|
|
|
.name = "M25P10",
|
|
|
|
},
|
|
|
|
{
|
2015-02-17 22:35:20 +01:00
|
|
|
.device_id = STM_ID_M25P16,
|
2014-10-14 21:40:59 +02:00
|
|
|
.op_erase = CMD_M25PXX_SE,
|
2012-05-10 20:27:32 +02:00
|
|
|
.page_size = 256,
|
|
|
|
.pages_per_sector = 256,
|
|
|
|
.nr_sectors = 32,
|
|
|
|
.name = "M25P16",
|
|
|
|
},
|
|
|
|
{
|
2015-02-17 22:35:20 +01:00
|
|
|
.device_id = STM_ID_M25P20,
|
2014-10-14 21:40:59 +02:00
|
|
|
.op_erase = CMD_M25PXX_SE,
|
2012-05-10 20:27:32 +02:00
|
|
|
.page_size = 256,
|
|
|
|
.pages_per_sector = 256,
|
|
|
|
.nr_sectors = 4,
|
|
|
|
.name = "M25P20",
|
|
|
|
},
|
|
|
|
{
|
2015-02-17 22:35:20 +01:00
|
|
|
.device_id = STM_ID_M25P32,
|
2014-10-14 21:40:59 +02:00
|
|
|
.op_erase = CMD_M25PXX_SE,
|
2012-05-10 20:27:32 +02:00
|
|
|
.page_size = 256,
|
|
|
|
.pages_per_sector = 256,
|
|
|
|
.nr_sectors = 64,
|
|
|
|
.name = "M25P32",
|
|
|
|
},
|
|
|
|
{
|
2015-02-17 22:35:20 +01:00
|
|
|
.device_id = STM_ID_M25P40,
|
2014-10-14 21:40:59 +02:00
|
|
|
.op_erase = CMD_M25PXX_SE,
|
2012-05-10 20:27:32 +02:00
|
|
|
.page_size = 256,
|
|
|
|
.pages_per_sector = 256,
|
|
|
|
.nr_sectors = 8,
|
|
|
|
.name = "M25P40",
|
|
|
|
},
|
|
|
|
{
|
2015-02-17 22:35:20 +01:00
|
|
|
.device_id = STM_ID_M25P64,
|
2014-10-14 21:40:59 +02:00
|
|
|
.op_erase = CMD_M25PXX_SE,
|
2012-05-10 20:27:32 +02:00
|
|
|
.page_size = 256,
|
|
|
|
.pages_per_sector = 256,
|
|
|
|
.nr_sectors = 128,
|
|
|
|
.name = "M25P64",
|
|
|
|
},
|
|
|
|
{
|
2015-02-17 22:35:20 +01:00
|
|
|
.device_id = STM_ID_M25P80,
|
2014-10-14 21:40:59 +02:00
|
|
|
.op_erase = CMD_M25PXX_SE,
|
2012-05-10 20:27:32 +02:00
|
|
|
.page_size = 256,
|
|
|
|
.pages_per_sector = 256,
|
|
|
|
.nr_sectors = 16,
|
|
|
|
.name = "M25P80",
|
|
|
|
},
|
|
|
|
{
|
2015-02-17 22:35:20 +01:00
|
|
|
.device_id = STM_ID_M25P128,
|
2014-10-14 21:40:59 +02:00
|
|
|
.op_erase = CMD_M25PXX_SE,
|
2012-05-10 20:27:32 +02:00
|
|
|
.page_size = 256,
|
|
|
|
.pages_per_sector = 1024,
|
|
|
|
.nr_sectors = 64,
|
|
|
|
.name = "M25P128",
|
|
|
|
},
|
2015-05-03 16:02:56 +02:00
|
|
|
{
|
|
|
|
.device_id = STM_ID_N25Q064,
|
|
|
|
.op_erase = CMD_M25PXX_SSE,
|
|
|
|
.page_size = 256,
|
|
|
|
.pages_per_sector = 16,
|
|
|
|
.nr_sectors = 2048,
|
|
|
|
.name = "N25Q064",
|
|
|
|
},
|
2015-05-03 14:06:21 +02:00
|
|
|
{
|
|
|
|
.device_id = STM_ID_N25Q128,
|
|
|
|
.op_erase = CMD_M25PXX_SSE,
|
|
|
|
.page_size = 256,
|
|
|
|
.pages_per_sector = 16,
|
|
|
|
.nr_sectors = 4096,
|
|
|
|
.name = "N25Q128",
|
|
|
|
},
|
2015-02-17 22:35:20 +01:00
|
|
|
{
|
2015-05-03 16:57:38 +02:00
|
|
|
.device_id = STM_ID_N25Q256,
|
2015-05-03 16:05:41 +02:00
|
|
|
.op_erase = CMD_M25PXX_SSE,
|
2015-02-17 22:35:20 +01:00
|
|
|
.page_size = 256,
|
2015-05-03 16:05:41 +02:00
|
|
|
.pages_per_sector = 16,
|
|
|
|
.nr_sectors = 8192,
|
2015-05-03 16:57:38 +02:00
|
|
|
.name = "N25Q256",
|
2015-02-17 22:35:20 +01:00
|
|
|
},
|
2012-05-10 20:27:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static int stmicro_write(struct spi_flash *flash,
|
|
|
|
u32 offset, size_t len, const void *buf)
|
|
|
|
{
|
|
|
|
struct stmicro_spi_flash *stm = to_stmicro_spi_flash(flash);
|
|
|
|
unsigned long byte_addr;
|
|
|
|
unsigned long page_size;
|
|
|
|
size_t chunk_len;
|
|
|
|
size_t actual;
|
2014-04-12 04:48:55 +02:00
|
|
|
int ret = 0;
|
2012-05-10 20:27:32 +02:00
|
|
|
u8 cmd[4];
|
|
|
|
|
2014-06-29 15:15:39 +02:00
|
|
|
page_size = stm->params->page_size;
|
2012-05-10 20:27:32 +02:00
|
|
|
byte_addr = offset % page_size;
|
|
|
|
|
2012-12-06 00:22:54 +01:00
|
|
|
flash->spi->rw = SPI_WRITE_FLAG;
|
2012-05-10 20:27:32 +02:00
|
|
|
|
|
|
|
for (actual = 0; actual < len; actual += chunk_len) {
|
|
|
|
chunk_len = min(len - actual, page_size - byte_addr);
|
2014-06-29 15:17:33 +02:00
|
|
|
chunk_len = spi_crop_chunk(sizeof(cmd), chunk_len);
|
2012-05-10 20:27:32 +02:00
|
|
|
|
|
|
|
cmd[0] = CMD_M25PXX_PP;
|
2013-08-18 11:44:24 +02:00
|
|
|
cmd[1] = (offset >> 16) & 0xff;
|
|
|
|
cmd[2] = (offset >> 8) & 0xff;
|
|
|
|
cmd[3] = offset & 0xff;
|
2012-05-23 20:03:29 +02:00
|
|
|
#if CONFIG_DEBUG_SPI_FLASH
|
2012-05-14 22:52:32 +02:00
|
|
|
printk(BIOS_SPEW, "PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x }"
|
|
|
|
" chunk_len = %zu\n",
|
2012-05-10 20:27:32 +02:00
|
|
|
buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
|
2012-05-23 20:03:29 +02:00
|
|
|
#endif
|
2012-05-10 20:27:32 +02:00
|
|
|
|
|
|
|
ret = spi_flash_cmd(flash->spi, CMD_M25PXX_WREN, NULL, 0);
|
|
|
|
if (ret < 0) {
|
|
|
|
printk(BIOS_WARNING, "SF: Enabling Write failed\n");
|
2013-08-18 11:44:24 +02:00
|
|
|
goto out;
|
2012-05-10 20:27:32 +02:00
|
|
|
}
|
|
|
|
|
2014-06-29 15:17:33 +02:00
|
|
|
ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd),
|
2012-05-10 20:27:32 +02:00
|
|
|
buf + actual, chunk_len);
|
|
|
|
if (ret < 0) {
|
|
|
|
printk(BIOS_WARNING, "SF: STMicro Page Program failed\n");
|
2013-08-18 11:44:24 +02:00
|
|
|
goto out;
|
2012-05-10 20:27:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
|
|
|
|
if (ret)
|
2013-08-18 11:44:24 +02:00
|
|
|
goto out;
|
2012-05-10 20:27:32 +02:00
|
|
|
|
2013-08-18 11:44:24 +02:00
|
|
|
offset += chunk_len;
|
2012-05-10 20:27:32 +02:00
|
|
|
byte_addr = 0;
|
|
|
|
}
|
|
|
|
|
2012-12-04 06:16:29 +01:00
|
|
|
#if CONFIG_DEBUG_SPI_FLASH
|
2013-08-18 11:44:24 +02:00
|
|
|
printk(BIOS_SPEW, "SF: STMicro: Successfully programmed %zu bytes @"
|
|
|
|
" 0x%lx\n", len, (unsigned long)(offset - len));
|
2012-12-04 06:16:29 +01:00
|
|
|
#endif
|
2013-08-18 11:44:24 +02:00
|
|
|
ret = 0;
|
2012-05-10 20:27:32 +02:00
|
|
|
|
2013-08-18 11:44:24 +02:00
|
|
|
out:
|
2012-05-10 20:27:32 +02:00
|
|
|
spi_release_bus(flash->spi);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-02-12 04:13:22 +01:00
|
|
|
static struct stmicro_spi_flash stm;
|
|
|
|
|
2012-05-10 20:27:32 +02:00
|
|
|
struct spi_flash *spi_flash_probe_stmicro(struct spi_slave *spi, u8 * idcode)
|
|
|
|
{
|
|
|
|
const struct stmicro_spi_flash_params *params;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (idcode[0] == 0xff) {
|
2015-02-17 22:35:20 +01:00
|
|
|
i = spi_flash_cmd(spi, CMD_M25PXX_RES, idcode, 4);
|
2012-05-10 20:27:32 +02:00
|
|
|
if (i)
|
|
|
|
return NULL;
|
|
|
|
if ((idcode[3] & 0xf0) == 0x10) {
|
|
|
|
idcode[0] = 0x20;
|
|
|
|
idcode[1] = 0x20;
|
|
|
|
idcode[2] = idcode[3] + 1;
|
|
|
|
} else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(stmicro_spi_flash_table); i++) {
|
|
|
|
params = &stmicro_spi_flash_table[i];
|
2015-02-17 22:35:20 +01:00
|
|
|
if (params->device_id == (idcode[1] << 8 | idcode[2])) {
|
2012-05-10 20:27:32 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == ARRAY_SIZE(stmicro_spi_flash_table)) {
|
2015-02-17 22:35:20 +01:00
|
|
|
printk(BIOS_WARNING, "SF: Unsupported STMicro ID %02x%02x\n",
|
|
|
|
idcode[1], idcode[2]);
|
2012-05-10 20:27:32 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-02-12 04:13:22 +01:00
|
|
|
stm.params = params;
|
|
|
|
stm.flash.spi = spi;
|
|
|
|
stm.flash.name = params->name;
|
2012-05-10 20:27:32 +02:00
|
|
|
|
2015-02-12 04:13:22 +01:00
|
|
|
stm.flash.write = stmicro_write;
|
|
|
|
stm.flash.erase = spi_flash_cmd_erase;
|
|
|
|
stm.flash.read = spi_flash_cmd_read_fast;
|
|
|
|
stm.flash.sector_size = params->page_size * params->pages_per_sector;
|
|
|
|
stm.flash.size = stm.flash.sector_size * params->nr_sectors;
|
|
|
|
stm.flash.erase_cmd = params->op_erase;
|
2012-05-10 20:27:32 +02:00
|
|
|
|
2015-02-12 04:13:22 +01:00
|
|
|
return &stm.flash;
|
2012-05-10 20:27:32 +02:00
|
|
|
}
|