2012-05-10 20:27:32 +02:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2010, ucRobotics Inc.
|
2014-05-01 22:53:24 +02:00
|
|
|
* Copyright (c) 2014, Sage Electronic Engineering, LLC
|
|
|
|
* .
|
2012-05-10 20:27:32 +02:00
|
|
|
* Author: Chong Huang <chuang@ucrobotics.com>
|
|
|
|
* Licensed under the GPL-2 or later.
|
|
|
|
*/
|
|
|
|
|
2016-11-21 06:04:00 +01:00
|
|
|
#include <console/console.h>
|
2012-05-10 20:27:32 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <spi_flash.h>
|
2016-11-21 06:04:00 +01:00
|
|
|
#include <spi-generic.h>
|
2016-12-06 05:32:24 +01:00
|
|
|
#include <string.h>
|
2014-06-26 07:02:40 +02:00
|
|
|
|
2012-05-10 20:27:32 +02:00
|
|
|
#include "spi_flash_internal.h"
|
|
|
|
|
2014-05-01 22:53:24 +02:00
|
|
|
/* EN25*-specific commands */
|
|
|
|
#define CMD_EN25_WREN 0x06 /* Write Enable */
|
|
|
|
#define CMD_EN25_WRDI 0x04 /* Write Disable */
|
|
|
|
#define CMD_EN25_RDSR 0x05 /* Read Status Register */
|
|
|
|
#define CMD_EN25_WRSR 0x01 /* Write Status Register */
|
|
|
|
#define CMD_EN25_READ 0x03 /* Read Data Bytes */
|
|
|
|
#define CMD_EN25_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */
|
|
|
|
#define CMD_EN25_PP 0x02 /* Page Program */
|
|
|
|
#define CMD_EN25_SE 0x20 /* Sector Erase */
|
|
|
|
#define CMD_EN25_BE 0xd8 /* Block Erase */
|
|
|
|
#define CMD_EN25_DP 0xb9 /* Deep Power-down */
|
|
|
|
#define CMD_EN25_RES 0xab /* Release from DP, and Read Signature */
|
|
|
|
|
|
|
|
#define EON_ID_EN25Q128 0x3018
|
|
|
|
#define EON_ID_EN25Q64 0x3017
|
2014-05-01 22:54:11 +02:00
|
|
|
#define EON_ID_EN25S64 0x3817
|
2012-05-10 20:27:32 +02:00
|
|
|
|
|
|
|
struct eon_spi_flash_params {
|
2014-05-01 22:53:24 +02:00
|
|
|
u16 id;
|
2012-05-10 20:27:32 +02:00
|
|
|
u16 page_size;
|
|
|
|
u16 pages_per_sector;
|
|
|
|
u16 sectors_per_block;
|
|
|
|
u16 nr_sectors;
|
|
|
|
const char *name;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* spi_flash needs to be first so upper layers can free() it */
|
|
|
|
struct eon_spi_flash {
|
|
|
|
struct spi_flash flash;
|
|
|
|
const struct eon_spi_flash_params *params;
|
|
|
|
};
|
|
|
|
|
2016-11-21 06:04:00 +01:00
|
|
|
static inline
|
|
|
|
struct eon_spi_flash *to_eon_spi_flash(const struct spi_flash *flash)
|
2012-05-10 20:27:32 +02:00
|
|
|
{
|
|
|
|
return container_of(flash, struct eon_spi_flash, flash);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct eon_spi_flash_params eon_spi_flash_table[] = {
|
|
|
|
{
|
2014-05-01 22:53:24 +02:00
|
|
|
.id = EON_ID_EN25Q128,
|
2012-05-10 20:27:32 +02:00
|
|
|
.page_size = 256,
|
|
|
|
.pages_per_sector = 16,
|
|
|
|
.sectors_per_block = 16,
|
|
|
|
.nr_sectors = 4096,
|
|
|
|
.name = "EN25Q128",
|
|
|
|
},
|
2014-01-05 06:46:19 +01:00
|
|
|
{
|
2014-05-01 22:53:24 +02:00
|
|
|
.id = EON_ID_EN25Q64,
|
2014-01-05 06:46:19 +01:00
|
|
|
.page_size = 256,
|
|
|
|
.pages_per_sector = 16,
|
|
|
|
.sectors_per_block = 16,
|
|
|
|
.nr_sectors = 2048,
|
|
|
|
.name = "EN25Q64",
|
|
|
|
},
|
2014-05-01 22:54:11 +02:00
|
|
|
{
|
|
|
|
.id = EON_ID_EN25S64,
|
|
|
|
.page_size = 256,
|
|
|
|
.pages_per_sector = 16,
|
|
|
|
.sectors_per_block = 16,
|
|
|
|
.nr_sectors = 2048,
|
|
|
|
.name = "EN25S64",
|
|
|
|
},
|
2012-05-10 20:27:32 +02:00
|
|
|
};
|
|
|
|
|
2016-11-21 06:04:00 +01:00
|
|
|
static int eon_write(const struct spi_flash *flash,
|
2012-05-10 20:27:32 +02:00
|
|
|
u32 offset, size_t len, const void *buf)
|
|
|
|
{
|
|
|
|
struct eon_spi_flash *eon = to_eon_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-05-01 22:53:24 +02:00
|
|
|
page_size = 1 << eon->params->page_size;
|
2012-05-10 20:27:32 +02:00
|
|
|
|
|
|
|
for (actual = 0; actual < len; actual += chunk_len) {
|
2016-12-17 20:16:07 +01:00
|
|
|
byte_addr = offset % page_size;
|
2012-05-10 20:27:32 +02:00
|
|
|
chunk_len = min(len - actual, page_size - byte_addr);
|
2017-04-20 04:27:28 +02:00
|
|
|
chunk_len = spi_crop_chunk(&flash->spi, sizeof(cmd), chunk_len);
|
2012-05-10 20:27:32 +02:00
|
|
|
|
2016-12-06 05:32:24 +01:00
|
|
|
ret = spi_flash_cmd(&flash->spi, CMD_EN25_WREN, NULL, 0);
|
2014-05-01 22:53:24 +02:00
|
|
|
if (ret < 0) {
|
|
|
|
printk(BIOS_WARNING, "SF: Enabling Write failed\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd[0] = CMD_EN25_PP;
|
|
|
|
cmd[1] = (offset >> 16) & 0xff;
|
|
|
|
cmd[2] = (offset >> 8) & 0xff;
|
|
|
|
cmd[3] = offset & 0xff;
|
2012-05-10 20:27:32 +02:00
|
|
|
|
2012-05-23 20:03:29 +02:00
|
|
|
#if CONFIG_DEBUG_SPI_FLASH
|
2012-05-10 20:27:32 +02:00
|
|
|
printk(BIOS_SPEW,
|
2012-05-14 22:52:32 +02:00
|
|
|
"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
|
|
|
|
2016-12-06 05:32:24 +01: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: EON Page Program failed\n");
|
2014-05-01 22:53:24 +02:00
|
|
|
goto out;
|
2012-05-10 20:27:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
|
2014-05-01 22:53:24 +02:00
|
|
|
if (ret) {
|
|
|
|
printk(BIOS_WARNING, "SF: EON Page Program timeout\n");
|
|
|
|
goto out;
|
|
|
|
}
|
2012-05-10 20:27:32 +02:00
|
|
|
|
2014-05-01 22:53:24 +02:00
|
|
|
offset += chunk_len;
|
2012-05-10 20:27:32 +02:00
|
|
|
}
|
|
|
|
|
2012-12-04 06:16:29 +01:00
|
|
|
#if CONFIG_DEBUG_SPI_FLASH
|
2014-05-01 22:53:24 +02:00
|
|
|
printk(BIOS_SPEW, "SF: EON: Successfully programmed %zu bytes @ %#x\n",
|
|
|
|
len, (unsigned int)(offset - len));
|
2012-12-04 06:16:29 +01:00
|
|
|
#endif
|
2012-05-10 20:27:32 +02:00
|
|
|
|
2014-05-01 22:53:24 +02:00
|
|
|
out:
|
2012-05-10 20:27:32 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct spi_flash *spi_flash_probe_eon(struct spi_slave *spi, u8 *idcode)
|
|
|
|
{
|
|
|
|
const struct eon_spi_flash_params *params;
|
|
|
|
struct eon_spi_flash *eon;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(eon_spi_flash_table); ++i) {
|
|
|
|
params = &eon_spi_flash_table[i];
|
2014-05-01 22:53:24 +02:00
|
|
|
if (params->id == ((idcode[1] << 8) | idcode[2]))
|
2012-05-10 20:27:32 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == ARRAY_SIZE(eon_spi_flash_table)) {
|
2014-05-01 22:53:24 +02:00
|
|
|
printk(BIOS_WARNING, "SF: Unsupported EON ID %#02x%02x\n",
|
|
|
|
idcode[1], idcode[2]);
|
2012-05-10 20:27:32 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
eon = malloc(sizeof(*eon));
|
|
|
|
if (!eon) {
|
|
|
|
printk(BIOS_WARNING, "SF: Failed to allocate memory\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
eon->params = params;
|
2016-12-06 05:32:24 +01:00
|
|
|
memcpy(&eon->flash.spi, spi, sizeof(*spi));
|
2012-05-10 20:27:32 +02:00
|
|
|
eon->flash.name = params->name;
|
|
|
|
|
2016-11-21 06:04:00 +01:00
|
|
|
eon->flash.internal_write = eon_write;
|
|
|
|
eon->flash.internal_erase = spi_flash_cmd_erase;
|
|
|
|
eon->flash.internal_status = spi_flash_cmd_status;
|
|
|
|
eon->flash.internal_read = spi_flash_cmd_read_fast;
|
2014-05-01 22:53:24 +02:00
|
|
|
eon->flash.sector_size = params->page_size * params->pages_per_sector;
|
2012-05-10 20:27:32 +02:00
|
|
|
eon->flash.size = params->page_size * params->pages_per_sector
|
|
|
|
* params->nr_sectors;
|
2015-01-08 19:29:19 +01:00
|
|
|
eon->flash.erase_cmd = CMD_EN25_SE;
|
2015-01-16 00:28:46 +01:00
|
|
|
eon->flash.status_cmd = CMD_EN25_RDSR;
|
2012-05-10 20:27:32 +02:00
|
|
|
|
|
|
|
return &eon->flash;
|
|
|
|
}
|