2007-10-15 23:45:29 +02:00
|
|
|
/*
|
|
|
|
* This file is part of the flashrom project.
|
|
|
|
*
|
2008-01-19 01:04:46 +01:00
|
|
|
* Copyright (C) 2007, 2008 Carl-Daniel Hailfinger
|
2008-06-27 18:28:34 +02:00
|
|
|
* Copyright (C) 2008 coresystems GmbH
|
2007-10-15 23:45:29 +02:00
|
|
|
*
|
|
|
|
* 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; version 2 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Contains the generic SPI framework
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <pci/pci.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "flash.h"
|
2008-05-13 16:58:23 +02:00
|
|
|
#include "spi.h"
|
2007-10-15 23:45:29 +02:00
|
|
|
|
2008-01-19 01:04:46 +01:00
|
|
|
void spi_prettyprint_status_register(struct flashchip *flash);
|
2007-10-15 23:45:29 +02:00
|
|
|
|
2008-10-18 23:14:13 +02:00
|
|
|
int spi_command(unsigned int writecnt, unsigned int readcnt,
|
|
|
|
const unsigned char *writearr, unsigned char *readarr)
|
2007-10-16 23:09:06 +02:00
|
|
|
{
|
2008-07-01 01:45:22 +02:00
|
|
|
switch (flashbus) {
|
|
|
|
case BUS_TYPE_IT87XX_SPI:
|
2008-10-18 23:14:13 +02:00
|
|
|
return it8716f_spi_command(writecnt, readcnt, writearr,
|
|
|
|
readarr);
|
2008-07-01 01:45:22 +02:00
|
|
|
case BUS_TYPE_ICH7_SPI:
|
|
|
|
case BUS_TYPE_ICH9_SPI:
|
|
|
|
case BUS_TYPE_VIA_SPI:
|
2008-10-18 23:14:13 +02:00
|
|
|
return ich_spi_command(writecnt, readcnt, writearr, readarr);
|
2008-07-01 01:45:22 +02:00
|
|
|
default:
|
2008-10-18 23:14:13 +02:00
|
|
|
printf_debug
|
|
|
|
("%s called, but no SPI chipset/strapping detected\n",
|
|
|
|
__FUNCTION__);
|
2008-07-01 01:45:22 +02:00
|
|
|
}
|
2007-10-16 23:09:06 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-06-30 23:45:17 +02:00
|
|
|
static int spi_rdid(unsigned char *readarr, int bytes)
|
2007-10-15 23:45:29 +02:00
|
|
|
{
|
2008-10-18 23:14:13 +02:00
|
|
|
const unsigned char cmd[JEDEC_RDID_OUTSIZE] = { JEDEC_RDID };
|
2007-10-15 23:45:29 +02:00
|
|
|
|
2008-07-07 08:38:51 +02:00
|
|
|
if (spi_command(sizeof(cmd), bytes, cmd, readarr))
|
2007-10-15 23:45:29 +02:00
|
|
|
return 1;
|
2008-10-18 23:14:13 +02:00
|
|
|
printf_debug("RDID returned %02x %02x %02x.\n", readarr[0], readarr[1],
|
|
|
|
readarr[2]);
|
2007-10-15 23:45:29 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-05-15 05:19:49 +02:00
|
|
|
static int spi_res(unsigned char *readarr)
|
|
|
|
{
|
2008-10-18 23:14:13 +02:00
|
|
|
const unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, 0, 0, 0 };
|
2008-05-15 05:19:49 +02:00
|
|
|
|
2008-07-07 08:38:51 +02:00
|
|
|
if (spi_command(sizeof(cmd), JEDEC_RES_INSIZE, cmd, readarr))
|
2008-05-15 05:19:49 +02:00
|
|
|
return 1;
|
|
|
|
printf_debug("RES returned %02x.\n", readarr[0]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-05-11 01:07:52 +02:00
|
|
|
void spi_write_enable()
|
2007-10-18 02:24:07 +02:00
|
|
|
{
|
2008-10-18 23:14:13 +02:00
|
|
|
const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN };
|
2007-10-18 02:24:07 +02:00
|
|
|
|
|
|
|
/* Send WREN (Write Enable) */
|
2008-07-07 08:38:51 +02:00
|
|
|
spi_command(sizeof(cmd), 0, cmd, NULL);
|
2007-10-18 02:24:07 +02:00
|
|
|
}
|
|
|
|
|
2008-05-11 01:07:52 +02:00
|
|
|
void spi_write_disable()
|
2007-10-18 02:24:07 +02:00
|
|
|
{
|
2008-10-18 23:14:13 +02:00
|
|
|
const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI };
|
2007-10-18 02:24:07 +02:00
|
|
|
|
|
|
|
/* Send WRDI (Write Disable) */
|
2008-07-07 08:38:51 +02:00
|
|
|
spi_command(sizeof(cmd), 0, cmd, NULL);
|
2007-10-18 02:24:07 +02:00
|
|
|
}
|
|
|
|
|
2008-06-30 23:45:17 +02:00
|
|
|
static int probe_spi_rdid_generic(struct flashchip *flash, int bytes)
|
2007-10-15 23:45:29 +02:00
|
|
|
{
|
2008-06-30 23:45:17 +02:00
|
|
|
unsigned char readarr[4];
|
2008-02-06 23:07:58 +01:00
|
|
|
uint32_t manuf_id;
|
|
|
|
uint32_t model_id;
|
2008-06-24 03:22:03 +02:00
|
|
|
|
2008-06-30 23:45:17 +02:00
|
|
|
if (spi_rdid(readarr, bytes))
|
2008-06-24 03:22:03 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!oddparity(readarr[0]))
|
|
|
|
printf_debug("RDID byte 0 parity violation.\n");
|
|
|
|
|
|
|
|
/* Check if this is a continuation vendor ID */
|
|
|
|
if (readarr[0] == 0x7f) {
|
|
|
|
if (!oddparity(readarr[1]))
|
|
|
|
printf_debug("RDID byte 1 parity violation.\n");
|
|
|
|
manuf_id = (readarr[0] << 8) | readarr[1];
|
|
|
|
model_id = readarr[2];
|
2008-06-30 23:45:17 +02:00
|
|
|
if (bytes > 3) {
|
|
|
|
model_id <<= 8;
|
|
|
|
model_id |= readarr[3];
|
|
|
|
}
|
2008-06-24 03:22:03 +02:00
|
|
|
} else {
|
|
|
|
manuf_id = readarr[0];
|
|
|
|
model_id = (readarr[1] << 8) | readarr[2];
|
|
|
|
}
|
|
|
|
|
2008-10-18 23:14:13 +02:00
|
|
|
printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, manuf_id,
|
|
|
|
model_id);
|
2008-06-24 03:22:03 +02:00
|
|
|
|
2008-10-18 23:14:13 +02:00
|
|
|
if (manuf_id == flash->manufacture_id && model_id == flash->model_id) {
|
2008-06-24 03:22:03 +02:00
|
|
|
/* Print the status register to tell the
|
|
|
|
* user about possible write protection.
|
|
|
|
*/
|
|
|
|
spi_prettyprint_status_register(flash);
|
|
|
|
|
|
|
|
return 1;
|
2007-10-15 23:45:29 +02:00
|
|
|
}
|
|
|
|
|
2008-06-24 03:22:03 +02:00
|
|
|
/* Test if this is a pure vendor match. */
|
|
|
|
if (manuf_id == flash->manufacture_id &&
|
|
|
|
GENERIC_DEVICE_ID == flash->model_id)
|
|
|
|
return 1;
|
|
|
|
|
2007-10-15 23:45:29 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-18 23:14:13 +02:00
|
|
|
int probe_spi_rdid(struct flashchip *flash)
|
|
|
|
{
|
2008-06-30 23:45:17 +02:00
|
|
|
return probe_spi_rdid_generic(flash, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* support 4 bytes flash ID */
|
2008-10-18 23:14:13 +02:00
|
|
|
int probe_spi_rdid4(struct flashchip *flash)
|
|
|
|
{
|
2008-06-30 23:45:17 +02:00
|
|
|
/* only some SPI chipsets support 4 bytes commands */
|
2008-07-01 01:45:22 +02:00
|
|
|
switch (flashbus) {
|
|
|
|
case BUS_TYPE_ICH7_SPI:
|
|
|
|
case BUS_TYPE_ICH9_SPI:
|
|
|
|
case BUS_TYPE_VIA_SPI:
|
|
|
|
return probe_spi_rdid_generic(flash, 4);
|
|
|
|
default:
|
|
|
|
printf_debug("4b ID not supported on this SPI controller\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2008-06-30 23:45:17 +02:00
|
|
|
}
|
|
|
|
|
2008-05-15 05:19:49 +02:00
|
|
|
int probe_spi_res(struct flashchip *flash)
|
|
|
|
{
|
|
|
|
unsigned char readarr[3];
|
|
|
|
uint32_t model_id;
|
2008-06-24 03:22:03 +02:00
|
|
|
|
2008-06-30 23:45:17 +02:00
|
|
|
if (spi_rdid(readarr, 3))
|
2008-05-15 05:19:49 +02:00
|
|
|
/* We couldn't issue RDID, it's pointless to try RES. */
|
|
|
|
return 0;
|
|
|
|
|
2008-06-24 03:22:03 +02:00
|
|
|
/* Check if RDID returns 0xff 0xff 0xff, then we use RES. */
|
|
|
|
if ((readarr[0] != 0xff) || (readarr[1] != 0xff) ||
|
|
|
|
(readarr[2] != 0xff))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (spi_res(readarr))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
model_id = readarr[0];
|
|
|
|
printf_debug("%s: id 0x%x\n", __FUNCTION__, model_id);
|
|
|
|
if (model_id != flash->model_id)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Print the status register to tell the
|
|
|
|
* user about possible write protection.
|
|
|
|
*/
|
|
|
|
spi_prettyprint_status_register(flash);
|
|
|
|
return 1;
|
2008-05-15 05:19:49 +02:00
|
|
|
}
|
|
|
|
|
2008-05-11 01:07:52 +02:00
|
|
|
uint8_t spi_read_status_register()
|
2007-10-18 02:24:07 +02:00
|
|
|
{
|
2008-10-18 23:14:13 +02:00
|
|
|
const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { JEDEC_RDSR };
|
2008-07-07 08:38:51 +02:00
|
|
|
unsigned char readarr[JEDEC_RDSR_INSIZE];
|
2007-10-18 02:24:07 +02:00
|
|
|
|
|
|
|
/* Read Status Register */
|
2008-07-07 08:38:51 +02:00
|
|
|
spi_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
|
2007-10-18 02:24:07 +02:00
|
|
|
return readarr[0];
|
|
|
|
}
|
|
|
|
|
2008-01-19 01:04:46 +01:00
|
|
|
/* Prettyprint the status register. Common definitions.
|
2007-12-29 11:15:58 +01:00
|
|
|
*/
|
2008-01-19 01:04:46 +01:00
|
|
|
void spi_prettyprint_status_register_common(uint8_t status)
|
2007-12-29 11:15:58 +01:00
|
|
|
{
|
2008-01-19 01:04:46 +01:00
|
|
|
printf_debug("Chip status register: Bit 5 / Block Protect 3 (BP3) is "
|
2008-10-18 23:14:13 +02:00
|
|
|
"%sset\n", (status & (1 << 5)) ? "" : "not ");
|
2008-01-19 01:04:46 +01:00
|
|
|
printf_debug("Chip status register: Bit 4 / Block Protect 2 (BP2) is "
|
2008-10-18 23:14:13 +02:00
|
|
|
"%sset\n", (status & (1 << 4)) ? "" : "not ");
|
2008-01-19 01:04:46 +01:00
|
|
|
printf_debug("Chip status register: Bit 3 / Block Protect 1 (BP1) is "
|
2008-10-18 23:14:13 +02:00
|
|
|
"%sset\n", (status & (1 << 3)) ? "" : "not ");
|
2008-01-19 01:04:46 +01:00
|
|
|
printf_debug("Chip status register: Bit 2 / Block Protect 0 (BP0) is "
|
2008-10-18 23:14:13 +02:00
|
|
|
"%sset\n", (status & (1 << 2)) ? "" : "not ");
|
2007-12-29 11:15:58 +01:00
|
|
|
printf_debug("Chip status register: Write Enable Latch (WEL) is "
|
2008-10-18 23:14:13 +02:00
|
|
|
"%sset\n", (status & (1 << 1)) ? "" : "not ");
|
2008-01-19 01:04:46 +01:00
|
|
|
printf_debug("Chip status register: Write In Progress (WIP/BUSY) is "
|
2008-10-18 23:14:13 +02:00
|
|
|
"%sset\n", (status & (1 << 0)) ? "" : "not ");
|
2007-12-29 11:15:58 +01:00
|
|
|
}
|
|
|
|
|
2008-01-19 01:04:46 +01:00
|
|
|
/* Prettyprint the status register. Works for
|
|
|
|
* ST M25P series
|
|
|
|
* MX MX25L series
|
|
|
|
*/
|
|
|
|
void spi_prettyprint_status_register_st_m25p(uint8_t status)
|
|
|
|
{
|
|
|
|
printf_debug("Chip status register: Status Register Write Disable "
|
2008-10-18 23:14:13 +02:00
|
|
|
"(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not ");
|
2008-01-19 01:04:46 +01:00
|
|
|
printf_debug("Chip status register: Bit 6 is "
|
2008-10-18 23:14:13 +02:00
|
|
|
"%sset\n", (status & (1 << 6)) ? "" : "not ");
|
2008-01-19 01:04:46 +01:00
|
|
|
spi_prettyprint_status_register_common(status);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Prettyprint the status register. Works for
|
|
|
|
* SST 25VF016
|
|
|
|
*/
|
|
|
|
void spi_prettyprint_status_register_sst25vf016(uint8_t status)
|
|
|
|
{
|
2008-01-22 15:37:31 +01:00
|
|
|
const char *bpt[] = {
|
2008-01-19 01:04:46 +01:00
|
|
|
"none",
|
|
|
|
"1F0000H-1FFFFFH",
|
|
|
|
"1E0000H-1FFFFFH",
|
|
|
|
"1C0000H-1FFFFFH",
|
|
|
|
"180000H-1FFFFFH",
|
|
|
|
"100000H-1FFFFFH",
|
2008-01-22 15:37:31 +01:00
|
|
|
"all", "all"
|
2008-01-19 01:04:46 +01:00
|
|
|
};
|
|
|
|
printf_debug("Chip status register: Block Protect Write Disable "
|
2008-10-18 23:14:13 +02:00
|
|
|
"(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not ");
|
2008-01-19 01:04:46 +01:00
|
|
|
printf_debug("Chip status register: Auto Address Increment Programming "
|
2008-10-18 23:14:13 +02:00
|
|
|
"(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not ");
|
2008-01-19 01:04:46 +01:00
|
|
|
spi_prettyprint_status_register_common(status);
|
|
|
|
printf_debug("Resulting block protection : %s\n",
|
2008-10-18 23:14:13 +02:00
|
|
|
bpt[(status & 0x1c) >> 2]);
|
2008-01-19 01:04:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void spi_prettyprint_status_register(struct flashchip *flash)
|
2007-12-29 11:15:58 +01:00
|
|
|
{
|
|
|
|
uint8_t status;
|
|
|
|
|
2008-05-11 01:07:52 +02:00
|
|
|
status = spi_read_status_register();
|
2007-12-29 11:15:58 +01:00
|
|
|
printf_debug("Chip status register is %02x\n", status);
|
|
|
|
switch (flash->manufacture_id) {
|
|
|
|
case ST_ID:
|
2008-05-16 00:32:08 +02:00
|
|
|
if (((flash->model_id & 0xff00) == 0x2000) ||
|
|
|
|
((flash->model_id & 0xff00) == 0x2500))
|
|
|
|
spi_prettyprint_status_register_st_m25p(status);
|
|
|
|
break;
|
2007-12-29 11:15:58 +01:00
|
|
|
case MX_ID:
|
|
|
|
if ((flash->model_id & 0xff00) == 0x2000)
|
2008-01-19 01:04:46 +01:00
|
|
|
spi_prettyprint_status_register_st_m25p(status);
|
|
|
|
break;
|
|
|
|
case SST_ID:
|
|
|
|
if (flash->model_id == SST_25VF016B)
|
|
|
|
spi_prettyprint_status_register_sst25vf016(status);
|
2007-12-29 11:15:58 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-10-18 23:14:13 +02:00
|
|
|
|
2008-11-03 01:02:11 +01:00
|
|
|
int spi_chip_erase_60(struct flashchip *flash)
|
|
|
|
{
|
|
|
|
const unsigned char cmd[JEDEC_CE_60_OUTSIZE] = {JEDEC_CE_60};
|
|
|
|
|
|
|
|
spi_disable_blockprotect();
|
|
|
|
spi_write_enable();
|
|
|
|
/* Send CE (Chip Erase) */
|
|
|
|
spi_command(sizeof(cmd), 0, cmd, NULL);
|
|
|
|
/* Wait until the Write-In-Progress bit is cleared.
|
|
|
|
* This usually takes 1-85 s, so wait in 1 s steps.
|
|
|
|
*/
|
|
|
|
while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
|
|
|
|
sleep(1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-05-11 01:07:52 +02:00
|
|
|
int spi_chip_erase_c7(struct flashchip *flash)
|
2007-10-18 02:24:07 +02:00
|
|
|
{
|
2008-10-18 23:14:13 +02:00
|
|
|
const unsigned char cmd[JEDEC_CE_C7_OUTSIZE] = { JEDEC_CE_C7 };
|
|
|
|
|
2008-01-19 01:04:46 +01:00
|
|
|
spi_disable_blockprotect();
|
2008-05-11 01:07:52 +02:00
|
|
|
spi_write_enable();
|
2007-10-18 02:24:07 +02:00
|
|
|
/* Send CE (Chip Erase) */
|
2008-07-07 08:38:51 +02:00
|
|
|
spi_command(sizeof(cmd), 0, cmd, NULL);
|
2007-10-22 18:15:28 +02:00
|
|
|
/* Wait until the Write-In-Progress bit is cleared.
|
|
|
|
* This usually takes 1-85 s, so wait in 1 s steps.
|
|
|
|
*/
|
2008-05-11 01:07:52 +02:00
|
|
|
while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
|
2007-10-18 02:24:07 +02:00
|
|
|
sleep(1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-11-03 01:02:11 +01:00
|
|
|
int spi_block_erase_52(const struct flashchip *flash, unsigned long addr)
|
|
|
|
{
|
|
|
|
unsigned char cmd[JEDEC_BE_52_OUTSIZE] = {JEDEC_BE_52};
|
|
|
|
|
|
|
|
cmd[1] = (addr & 0x00ff0000) >> 16;
|
|
|
|
cmd[2] = (addr & 0x0000ff00) >> 8;
|
|
|
|
cmd[3] = (addr & 0x000000ff);
|
|
|
|
spi_write_enable();
|
|
|
|
/* Send BE (Block Erase) */
|
|
|
|
spi_command(sizeof(cmd), 0, cmd, NULL);
|
|
|
|
/* Wait until the Write-In-Progress bit is cleared.
|
|
|
|
* This usually takes 100-4000 ms, so wait in 100 ms steps.
|
|
|
|
*/
|
|
|
|
while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
|
|
|
|
usleep(100 * 1000);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-10-22 18:15:28 +02:00
|
|
|
/* Block size is usually
|
|
|
|
* 64k for Macronix
|
|
|
|
* 32k for SST
|
|
|
|
* 4-32k non-uniform for EON
|
|
|
|
*/
|
2008-05-11 01:07:52 +02:00
|
|
|
int spi_block_erase_d8(const struct flashchip *flash, unsigned long addr)
|
2007-10-22 18:15:28 +02:00
|
|
|
{
|
2008-10-18 23:14:13 +02:00
|
|
|
unsigned char cmd[JEDEC_BE_D8_OUTSIZE] = { JEDEC_BE_D8 };
|
2007-10-22 18:15:28 +02:00
|
|
|
|
|
|
|
cmd[1] = (addr & 0x00ff0000) >> 16;
|
|
|
|
cmd[2] = (addr & 0x0000ff00) >> 8;
|
|
|
|
cmd[3] = (addr & 0x000000ff);
|
2008-05-11 01:07:52 +02:00
|
|
|
spi_write_enable();
|
2007-10-22 18:15:28 +02:00
|
|
|
/* Send BE (Block Erase) */
|
2008-07-07 08:38:51 +02:00
|
|
|
spi_command(sizeof(cmd), 0, cmd, NULL);
|
2007-10-22 18:15:28 +02:00
|
|
|
/* Wait until the Write-In-Progress bit is cleared.
|
|
|
|
* This usually takes 100-4000 ms, so wait in 100 ms steps.
|
|
|
|
*/
|
2008-05-11 01:07:52 +02:00
|
|
|
while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
|
2007-10-22 18:15:28 +02:00
|
|
|
usleep(100 * 1000);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-29 23:13:20 +01:00
|
|
|
int spi_chip_erase_d8(struct flashchip *flash)
|
|
|
|
{
|
|
|
|
int i, rc = 0;
|
|
|
|
int total_size = flash->total_size * 1024;
|
|
|
|
int erase_size = 64 * 1024;
|
|
|
|
|
|
|
|
spi_disable_blockprotect();
|
|
|
|
|
|
|
|
printf("Erasing chip: \n");
|
|
|
|
|
|
|
|
for (i = 0; i < total_size / erase_size; i++) {
|
|
|
|
rc = spi_block_erase_d8(flash, i * erase_size);
|
|
|
|
if (rc) {
|
|
|
|
printf("Error erasing block at 0x%x\n", i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2007-10-22 18:15:28 +02:00
|
|
|
/* Sector size is usually 4k, though Macronix eliteflash has 64k */
|
2008-05-11 01:07:52 +02:00
|
|
|
int spi_sector_erase(const struct flashchip *flash, unsigned long addr)
|
2007-10-22 18:15:28 +02:00
|
|
|
{
|
2008-10-18 23:14:13 +02:00
|
|
|
unsigned char cmd[JEDEC_SE_OUTSIZE] = { JEDEC_SE };
|
2007-10-22 18:15:28 +02:00
|
|
|
cmd[1] = (addr & 0x00ff0000) >> 16;
|
|
|
|
cmd[2] = (addr & 0x0000ff00) >> 8;
|
|
|
|
cmd[3] = (addr & 0x000000ff);
|
|
|
|
|
2008-05-11 01:07:52 +02:00
|
|
|
spi_write_enable();
|
2007-10-22 18:15:28 +02:00
|
|
|
/* Send SE (Sector Erase) */
|
2008-07-07 08:38:51 +02:00
|
|
|
spi_command(sizeof(cmd), 0, cmd, NULL);
|
2007-10-22 18:15:28 +02:00
|
|
|
/* Wait until the Write-In-Progress bit is cleared.
|
|
|
|
* This usually takes 15-800 ms, so wait in 10 ms steps.
|
|
|
|
*/
|
2008-05-11 01:07:52 +02:00
|
|
|
while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
|
2007-10-22 18:15:28 +02:00
|
|
|
usleep(10 * 1000);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-01-19 01:04:46 +01:00
|
|
|
/*
|
|
|
|
* This is according the SST25VF016 datasheet, who knows it is more
|
|
|
|
* generic that this...
|
|
|
|
*/
|
|
|
|
void spi_write_status_register(int status)
|
|
|
|
{
|
2008-10-18 23:14:13 +02:00
|
|
|
const unsigned char cmd[JEDEC_WRSR_OUTSIZE] =
|
|
|
|
{ JEDEC_WRSR, (unsigned char)status };
|
2008-01-19 01:04:46 +01:00
|
|
|
|
|
|
|
/* Send WRSR (Write Status Register) */
|
2008-07-07 08:38:51 +02:00
|
|
|
spi_command(sizeof(cmd), 0, cmd, NULL);
|
2008-01-19 01:04:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void spi_byte_program(int address, uint8_t byte)
|
|
|
|
{
|
2008-10-18 23:14:13 +02:00
|
|
|
const unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE] = {
|
|
|
|
JEDEC_BYTE_PROGRAM,
|
|
|
|
(address >> 16) & 0xff,
|
|
|
|
(address >> 8) & 0xff,
|
|
|
|
(address >> 0) & 0xff,
|
2008-01-19 01:04:46 +01:00
|
|
|
byte
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Send Byte-Program */
|
2008-07-07 08:38:51 +02:00
|
|
|
spi_command(sizeof(cmd), 0, cmd, NULL);
|
2008-01-19 01:04:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void spi_disable_blockprotect(void)
|
|
|
|
{
|
|
|
|
uint8_t status;
|
|
|
|
|
2008-05-11 01:07:52 +02:00
|
|
|
status = spi_read_status_register();
|
2008-01-19 01:04:46 +01:00
|
|
|
/* If there is block protection in effect, unprotect it first. */
|
|
|
|
if ((status & 0x3c) != 0) {
|
|
|
|
printf_debug("Some block protection in effect, disabling\n");
|
2008-05-11 01:07:52 +02:00
|
|
|
spi_write_enable();
|
2008-01-19 01:04:46 +01:00
|
|
|
spi_write_status_register(status & ~0x3c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-14 01:03:12 +02:00
|
|
|
void spi_nbyte_read(int address, uint8_t *bytes, int len)
|
2008-01-19 01:04:46 +01:00
|
|
|
{
|
2008-10-18 23:14:13 +02:00
|
|
|
const unsigned char cmd[JEDEC_READ_OUTSIZE] = {
|
|
|
|
JEDEC_READ,
|
2008-01-22 15:37:31 +01:00
|
|
|
(address >> 16) & 0xff,
|
|
|
|
(address >> 8) & 0xff,
|
|
|
|
(address >> 0) & 0xff,
|
2008-01-19 01:04:46 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Send Read */
|
2008-07-07 08:38:51 +02:00
|
|
|
spi_command(sizeof(cmd), len, cmd, bytes);
|
2008-01-19 01:04:46 +01:00
|
|
|
}
|
|
|
|
|
2008-05-11 01:07:52 +02:00
|
|
|
int spi_chip_read(struct flashchip *flash, uint8_t *buf)
|
2008-01-19 01:04:46 +01:00
|
|
|
{
|
2008-07-01 01:45:22 +02:00
|
|
|
switch (flashbus) {
|
|
|
|
case BUS_TYPE_IT87XX_SPI:
|
2008-05-14 01:03:12 +02:00
|
|
|
return it8716f_spi_chip_read(flash, buf);
|
2008-07-01 01:45:22 +02:00
|
|
|
case BUS_TYPE_ICH7_SPI:
|
|
|
|
case BUS_TYPE_ICH9_SPI:
|
|
|
|
case BUS_TYPE_VIA_SPI:
|
2008-05-16 14:55:55 +02:00
|
|
|
return ich_spi_read(flash, buf);
|
2008-07-01 01:45:22 +02:00
|
|
|
default:
|
2008-10-18 23:14:13 +02:00
|
|
|
printf_debug
|
|
|
|
("%s called, but no SPI chipset/strapping detected\n",
|
|
|
|
__FUNCTION__);
|
2008-07-01 01:45:22 +02:00
|
|
|
}
|
|
|
|
|
2008-05-14 01:03:12 +02:00
|
|
|
return 1;
|
2008-01-19 01:04:46 +01:00
|
|
|
}
|
|
|
|
|
2008-05-14 01:03:12 +02:00
|
|
|
int spi_chip_write(struct flashchip *flash, uint8_t *buf)
|
|
|
|
{
|
2008-07-01 01:45:22 +02:00
|
|
|
switch (flashbus) {
|
|
|
|
case BUS_TYPE_IT87XX_SPI:
|
2008-05-14 01:03:12 +02:00
|
|
|
return it8716f_spi_chip_write(flash, buf);
|
2008-07-01 01:45:22 +02:00
|
|
|
case BUS_TYPE_ICH7_SPI:
|
|
|
|
case BUS_TYPE_ICH9_SPI:
|
|
|
|
case BUS_TYPE_VIA_SPI:
|
2008-06-30 23:38:30 +02:00
|
|
|
return ich_spi_write(flash, buf);
|
2008-07-01 01:45:22 +02:00
|
|
|
default:
|
2008-10-18 23:14:13 +02:00
|
|
|
printf_debug
|
|
|
|
("%s called, but no SPI chipset/strapping detected\n",
|
|
|
|
__FUNCTION__);
|
2008-07-01 01:45:22 +02:00
|
|
|
}
|
|
|
|
|
2008-05-14 01:03:12 +02:00
|
|
|
return 1;
|
2007-10-18 02:24:07 +02:00
|
|
|
}
|