You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
196 lines
3.9 KiB
196 lines
3.9 KiB
8 years ago
|
#include <stdint.h>
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
8 years ago
|
#include <string.h>
|
||
8 years ago
|
|
||
8 years ago
|
#include <bitops.h>
|
||
8 years ago
|
#include <flash.h>
|
||
|
#include <macros.h>
|
||
|
#include <spi.h>
|
||
8 years ago
|
#include <spi_flash.h>
|
||
8 years ago
|
|
||
8 years ago
|
static size_t spi_flash_get_size(struct flash_dev *dev);
|
||
8 years ago
|
static size_t spi_flash_read(struct flash_dev *dev, uint32_t addr, void *data,
|
||
8 years ago
|
size_t len);
|
||
8 years ago
|
static size_t spi_flash_write(struct flash_dev *dev, uint32_t addr,
|
||
8 years ago
|
const void *data, size_t len);
|
||
8 years ago
|
static int spi_flash_erase(struct flash_dev *dev, uint32_t addr);
|
||
|
static void spi_flash_release(struct flash_dev *dev);
|
||
8 years ago
|
|
||
8 years ago
|
static struct flash_ops spi_flash_ops = {
|
||
|
.release = spi_flash_release,
|
||
8 years ago
|
.get_size = spi_flash_get_size,
|
||
8 years ago
|
.get_capacity = spi_flash_get_size,
|
||
8 years ago
|
.read = spi_flash_read,
|
||
|
.write = spi_flash_write,
|
||
8 years ago
|
.copy = default_flash_copy,
|
||
8 years ago
|
.is_erased = default_flash_is_one,
|
||
8 years ago
|
.erase = spi_flash_erase,
|
||
|
};
|
||
|
|
||
|
static void spi_flash_addr(char *cmd, uint32_t addr)
|
||
|
{
|
||
|
cmd[1] = addr >> 16;
|
||
|
cmd[2] = addr >> 8;
|
||
|
cmd[3] = addr;
|
||
|
}
|
||
|
|
||
8 years ago
|
static char spi_flash_get_status1(struct flash_dev *dev)
|
||
8 years ago
|
{
|
||
|
struct spi_dev *spi_dev = dev->priv;
|
||
8 years ago
|
char cmd = SPI_FLASH_READ_SR1;
|
||
|
char ret;
|
||
8 years ago
|
|
||
|
spi_tx_rx(spi_dev, &ret, sizeof ret, &cmd, sizeof cmd);
|
||
|
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
static int spi_flash_wait(struct flash_dev *dev)
|
||
|
{
|
||
|
while (spi_flash_get_status1(dev) & SPI_FLASH_SR1_BUSY);
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
8 years ago
|
static void spi_flash_write_enable(struct flash_dev *dev)
|
||
|
{
|
||
8 years ago
|
char cmd[1] = { SPI_FLASH_WRITE_ENABLE };
|
||
8 years ago
|
struct spi_dev *spi_dev = dev->priv;
|
||
|
|
||
8 years ago
|
spi_tx_rx(spi_dev, NULL, 0, cmd, sizeof cmd);
|
||
8 years ago
|
}
|
||
|
|
||
|
static void spi_flash_write_disable(struct flash_dev *dev)
|
||
|
{
|
||
8 years ago
|
char cmd[1] = { SPI_FLASH_WRITE_DISABLE };
|
||
8 years ago
|
struct spi_dev *spi_dev = dev->priv;
|
||
|
|
||
8 years ago
|
spi_tx_rx(spi_dev, NULL, 0, cmd, sizeof cmd);
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
static size_t spi_flash_get_jedec_id(struct flash_dev *dev, char *jedec_id,
|
||
8 years ago
|
size_t len)
|
||
|
{
|
||
8 years ago
|
char cmd[1] = { SPI_FLASH_JEDEC_ID };
|
||
|
char buf[3];
|
||
8 years ago
|
struct spi_dev *spi_dev = dev->priv;
|
||
|
|
||
8 years ago
|
spi_tx_rx(spi_dev, buf, sizeof buf, cmd, sizeof cmd);
|
||
8 years ago
|
|
||
8 years ago
|
memcpy(jedec_id, buf, min(sizeof buf, len));
|
||
8 years ago
|
|
||
8 years ago
|
return len;
|
||
8 years ago
|
}
|
||
|
|
||
|
static size_t spi_flash_get_size(struct flash_dev *dev)
|
||
|
{
|
||
8 years ago
|
char jedec_id[4];
|
||
8 years ago
|
|
||
|
spi_flash_get_jedec_id(dev, jedec_id, sizeof jedec_id);
|
||
|
|
||
|
return SPI_FLASH_SIZE(jedec_id[2]);
|
||
|
}
|
||
|
|
||
8 years ago
|
static size_t spi_flash_read(struct flash_dev *dev, uint32_t addr, void *data,
|
||
8 years ago
|
size_t len)
|
||
|
{
|
||
8 years ago
|
char cmd[4];
|
||
8 years ago
|
char *buf = data;
|
||
8 years ago
|
struct spi_dev *spi_dev = dev->priv;
|
||
8 years ago
|
size_t nbytes, ret = 0;
|
||
8 years ago
|
|
||
8 years ago
|
while (len) {
|
||
|
cmd[0] = SPI_FLASH_READ;
|
||
|
spi_flash_addr(cmd, addr);
|
||
8 years ago
|
|
||
8 years ago
|
nbytes = min(len, 32);
|
||
8 years ago
|
|
||
8 years ago
|
spi_tx_rx(spi_dev, buf, len, cmd, sizeof cmd);
|
||
8 years ago
|
|
||
|
addr += nbytes;
|
||
8 years ago
|
buf += nbytes;
|
||
8 years ago
|
len -= nbytes;
|
||
|
ret += nbytes;
|
||
|
}
|
||
|
|
||
|
return ret;
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
static size_t spi_flash_write(struct flash_dev *dev, uint32_t addr,
|
||
8 years ago
|
const void *data, size_t len)
|
||
|
{
|
||
8 years ago
|
char cmd[4 + 32];
|
||
8 years ago
|
const char *buf = data;
|
||
8 years ago
|
struct spi_dev *spi_dev = dev->priv;
|
||
8 years ago
|
size_t nbytes, ret = 0;
|
||
8 years ago
|
|
||
8 years ago
|
while (len) {
|
||
8 years ago
|
spi_flash_write_enable(dev);
|
||
|
|
||
8 years ago
|
cmd[0] = SPI_FLASH_PAGE_PROGRAM;
|
||
|
spi_flash_addr(cmd, addr);
|
||
8 years ago
|
|
||
8 years ago
|
nbytes = min(len, 32);
|
||
8 years ago
|
nbytes = min(nbytes, align_up(addr + 1, ilog2(256)) - addr);
|
||
8 years ago
|
|
||
8 years ago
|
memmove(cmd + 4, buf, nbytes);
|
||
8 years ago
|
spi_tx_rx(spi_dev, NULL, 0, cmd, 4 + nbytes);
|
||
|
spi_flash_wait(dev);
|
||
8 years ago
|
|
||
|
addr += nbytes;
|
||
8 years ago
|
buf += nbytes;
|
||
8 years ago
|
len -= nbytes;
|
||
|
ret += nbytes;
|
||
|
}
|
||
|
|
||
|
return ret;
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
static int spi_flash_erase(struct flash_dev *dev, uint32_t addr)
|
||
8 years ago
|
{
|
||
|
char cmd[4];
|
||
|
struct spi_dev *spi_dev = dev->priv;
|
||
|
|
||
|
spi_flash_write_enable(dev);
|
||
8 years ago
|
|
||
|
cmd[0] = SPI_FLASH_ERASE_4K;
|
||
|
spi_flash_addr(cmd, addr << dev->log2_block_size);
|
||
|
|
||
8 years ago
|
spi_tx_rx(spi_dev, NULL, 0, cmd, sizeof cmd);
|
||
8 years ago
|
spi_flash_wait(dev);
|
||
8 years ago
|
|
||
8 years ago
|
spi_flash_write_disable(dev);
|
||
8 years ago
|
|
||
8 years ago
|
return 0;
|
||
|
}
|
||
|
|
||
|
struct flash_dev *flash_probe(void)
|
||
|
{
|
||
|
struct flash_dev *dev;
|
||
|
|
||
|
if (!(dev = malloc(sizeof *dev)))
|
||
|
return NULL;
|
||
|
|
||
|
if (!(dev->priv = spi_probe()))
|
||
|
goto err_free_dev;
|
||
|
|
||
|
dev->ops = &spi_flash_ops;
|
||
8 years ago
|
dev->log2_block_size = ilog2(4 * KIB);
|
||
8 years ago
|
|
||
|
return dev;
|
||
|
|
||
|
err_free_dev:
|
||
|
free(dev);
|
||
|
return NULL;
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
static void spi_flash_release(struct flash_dev *dev)
|
||
8 years ago
|
{
|
||
8 years ago
|
if (!dev)
|
||
|
return;
|
||
|
|
||
8 years ago
|
spi_release(dev->priv);
|
||
|
free(dev);
|
||
|
}
|