2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2013-08-29 13:31:56 +00:00
|
|
|
/*
|
|
|
|
* SPI flash probing
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 Atmel Corporation
|
|
|
|
* Copyright (C) 2010 Reinhard Meyer, EMK Elektronik
|
|
|
|
* Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2014-10-14 05:42:07 +00:00
|
|
|
#include <dm.h>
|
2014-10-14 05:42:05 +00:00
|
|
|
#include <errno.h>
|
2013-08-29 13:31:56 +00:00
|
|
|
#include <malloc.h>
|
|
|
|
#include <spi.h>
|
|
|
|
#include <spi_flash.h>
|
|
|
|
|
2013-09-26 10:30:15 +00:00
|
|
|
#include "sf_internal.h"
|
2013-08-29 13:31:56 +00:00
|
|
|
|
2014-10-14 05:42:05 +00:00
|
|
|
/**
|
|
|
|
* spi_flash_probe_slave() - Probe for a SPI flash device on a bus
|
|
|
|
*
|
|
|
|
* @flashp: Pointer to place to put flash info, which may be NULL if the
|
|
|
|
* space should be allocated
|
|
|
|
*/
|
2015-12-06 16:14:12 +00:00
|
|
|
static int spi_flash_probe_slave(struct spi_flash *flash)
|
2013-08-29 13:31:56 +00:00
|
|
|
{
|
2015-12-06 16:03:32 +00:00
|
|
|
struct spi_slave *spi = flash->spi;
|
2013-09-24 10:31:23 +00:00
|
|
|
int ret;
|
2013-08-29 13:31:56 +00:00
|
|
|
|
2013-09-24 10:31:23 +00:00
|
|
|
/* Setup spi_slave */
|
2013-08-29 13:31:56 +00:00
|
|
|
if (!spi) {
|
|
|
|
printf("SF: Failed to set up slave\n");
|
2014-10-14 05:42:05 +00:00
|
|
|
return -ENODEV;
|
2013-08-29 13:31:56 +00:00
|
|
|
}
|
|
|
|
|
2013-09-24 10:31:23 +00:00
|
|
|
/* Claim spi bus */
|
2013-08-29 13:31:56 +00:00
|
|
|
ret = spi_claim_bus(spi);
|
|
|
|
if (ret) {
|
|
|
|
debug("SF: Failed to claim SPI bus: %d\n", ret);
|
2014-10-14 05:42:05 +00:00
|
|
|
return ret;
|
2013-08-29 13:31:56 +00:00
|
|
|
}
|
|
|
|
|
2019-02-05 05:59:23 +00:00
|
|
|
ret = spi_nor_scan(flash);
|
2016-02-24 16:14:56 +00:00
|
|
|
if (ret)
|
2013-09-24 10:31:23 +00:00
|
|
|
goto err_read_id;
|
2013-08-29 13:31:56 +00:00
|
|
|
|
2015-04-27 05:42:04 +00:00
|
|
|
#ifdef CONFIG_SPI_FLASH_MTD
|
|
|
|
ret = spi_flash_mtd_register(flash);
|
|
|
|
#endif
|
2013-08-29 13:31:56 +00:00
|
|
|
|
|
|
|
err_read_id:
|
|
|
|
spi_release_bus(spi);
|
2014-10-14 05:42:05 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-10-14 05:42:07 +00:00
|
|
|
#ifndef CONFIG_DM_SPI_FLASH
|
2018-01-15 10:08:42 +00:00
|
|
|
struct spi_flash *spi_flash_probe(unsigned int busnum, unsigned int cs,
|
|
|
|
unsigned int max_hz, unsigned int spi_mode)
|
2014-10-14 05:42:05 +00:00
|
|
|
{
|
2018-01-15 10:08:42 +00:00
|
|
|
struct spi_slave *bus;
|
2014-10-14 05:42:05 +00:00
|
|
|
struct spi_flash *flash;
|
|
|
|
|
2018-01-15 10:08:42 +00:00
|
|
|
bus = spi_setup_slave(busnum, cs, max_hz, spi_mode);
|
|
|
|
if (!bus)
|
|
|
|
return NULL;
|
|
|
|
|
2014-10-14 05:42:05 +00:00
|
|
|
/* Allocate space if needed (not used by sf-uclass */
|
|
|
|
flash = calloc(1, sizeof(*flash));
|
|
|
|
if (!flash) {
|
|
|
|
debug("SF: Failed to allocate spi_flash\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-12-06 16:03:32 +00:00
|
|
|
flash->spi = bus;
|
|
|
|
if (spi_flash_probe_slave(flash)) {
|
2014-10-14 05:42:05 +00:00
|
|
|
spi_free_slave(bus);
|
|
|
|
free(flash);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return flash;
|
2013-08-29 13:31:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void spi_flash_free(struct spi_flash *flash)
|
|
|
|
{
|
2015-04-27 05:42:04 +00:00
|
|
|
#ifdef CONFIG_SPI_FLASH_MTD
|
|
|
|
spi_flash_mtd_unregister();
|
|
|
|
#endif
|
2013-08-29 13:31:56 +00:00
|
|
|
spi_free_slave(flash->spi);
|
|
|
|
free(flash);
|
|
|
|
}
|
2014-10-14 05:42:07 +00:00
|
|
|
|
|
|
|
#else /* defined CONFIG_DM_SPI_FLASH */
|
|
|
|
|
|
|
|
static int spi_flash_std_read(struct udevice *dev, u32 offset, size_t len,
|
|
|
|
void *buf)
|
|
|
|
{
|
2015-03-05 19:25:20 +00:00
|
|
|
struct spi_flash *flash = dev_get_uclass_priv(dev);
|
2019-02-05 05:59:23 +00:00
|
|
|
struct mtd_info *mtd = &flash->mtd;
|
|
|
|
size_t retlen;
|
2014-10-14 05:42:07 +00:00
|
|
|
|
2019-02-05 05:59:23 +00:00
|
|
|
return log_ret(mtd->_read(mtd, offset, len, &retlen, buf));
|
2014-10-14 05:42:07 +00:00
|
|
|
}
|
|
|
|
|
2015-12-06 16:14:12 +00:00
|
|
|
static int spi_flash_std_write(struct udevice *dev, u32 offset, size_t len,
|
2018-01-15 10:08:40 +00:00
|
|
|
const void *buf)
|
2014-10-14 05:42:07 +00:00
|
|
|
{
|
2015-03-05 19:25:20 +00:00
|
|
|
struct spi_flash *flash = dev_get_uclass_priv(dev);
|
2019-02-05 05:59:23 +00:00
|
|
|
struct mtd_info *mtd = &flash->mtd;
|
|
|
|
size_t retlen;
|
2014-10-14 05:42:07 +00:00
|
|
|
|
2019-02-05 05:59:23 +00:00
|
|
|
return mtd->_write(mtd, offset, len, &retlen, buf);
|
2014-10-14 05:42:07 +00:00
|
|
|
}
|
|
|
|
|
2015-12-06 16:14:12 +00:00
|
|
|
static int spi_flash_std_erase(struct udevice *dev, u32 offset, size_t len)
|
2014-10-14 05:42:07 +00:00
|
|
|
{
|
2015-03-05 19:25:20 +00:00
|
|
|
struct spi_flash *flash = dev_get_uclass_priv(dev);
|
2019-02-05 05:59:23 +00:00
|
|
|
struct mtd_info *mtd = &flash->mtd;
|
|
|
|
struct erase_info instr;
|
|
|
|
|
|
|
|
if (offset % mtd->erasesize || len % mtd->erasesize) {
|
|
|
|
printf("SF: Erase offset/length not multiple of erase size\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&instr, 0, sizeof(instr));
|
|
|
|
instr.addr = offset;
|
|
|
|
instr.len = len;
|
2014-10-14 05:42:07 +00:00
|
|
|
|
2019-02-05 05:59:23 +00:00
|
|
|
return mtd->_erase(mtd, &instr);
|
2014-10-14 05:42:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-06 22:21:41 +00:00
|
|
|
static int spi_flash_std_get_sw_write_prot(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct spi_flash *flash = dev_get_uclass_priv(dev);
|
|
|
|
|
|
|
|
return spi_flash_cmd_get_sw_write_prot(flash);
|
|
|
|
}
|
|
|
|
|
2015-12-06 16:14:12 +00:00
|
|
|
static int spi_flash_std_probe(struct udevice *dev)
|
2014-10-14 05:42:07 +00:00
|
|
|
{
|
2015-09-29 05:32:01 +00:00
|
|
|
struct spi_slave *slave = dev_get_parent_priv(dev);
|
2015-01-25 15:27:12 +00:00
|
|
|
struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
|
2014-10-14 05:42:07 +00:00
|
|
|
struct spi_flash *flash;
|
|
|
|
|
2015-03-05 19:25:20 +00:00
|
|
|
flash = dev_get_uclass_priv(dev);
|
2014-10-14 05:42:07 +00:00
|
|
|
flash->dev = dev;
|
2015-12-06 16:03:32 +00:00
|
|
|
flash->spi = slave;
|
2015-01-25 15:27:12 +00:00
|
|
|
debug("%s: slave=%p, cs=%d\n", __func__, slave, plat->cs);
|
2015-12-06 16:03:32 +00:00
|
|
|
return spi_flash_probe_slave(flash);
|
2014-10-14 05:42:07 +00:00
|
|
|
}
|
|
|
|
|
2018-12-02 09:54:31 +00:00
|
|
|
static int spi_flash_std_remove(struct udevice *dev)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_SPI_FLASH_MTD
|
|
|
|
spi_flash_mtd_unregister();
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-14 05:42:07 +00:00
|
|
|
static const struct dm_spi_flash_ops spi_flash_std_ops = {
|
|
|
|
.read = spi_flash_std_read,
|
|
|
|
.write = spi_flash_std_write,
|
|
|
|
.erase = spi_flash_std_erase,
|
2018-11-06 22:21:41 +00:00
|
|
|
.get_sw_write_prot = spi_flash_std_get_sw_write_prot,
|
2014-10-14 05:42:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct udevice_id spi_flash_std_ids[] = {
|
|
|
|
{ .compatible = "spi-flash" },
|
2019-02-05 05:59:22 +00:00
|
|
|
{ .compatible = "jedec,spi-nor" },
|
2014-10-14 05:42:07 +00:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(spi_flash_std) = {
|
|
|
|
.name = "spi_flash_std",
|
|
|
|
.id = UCLASS_SPI_FLASH,
|
|
|
|
.of_match = spi_flash_std_ids,
|
|
|
|
.probe = spi_flash_std_probe,
|
2018-12-02 09:54:31 +00:00
|
|
|
.remove = spi_flash_std_remove,
|
2014-10-14 05:42:07 +00:00
|
|
|
.priv_auto_alloc_size = sizeof(struct spi_flash),
|
|
|
|
.ops = &spi_flash_std_ops,
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* CONFIG_DM_SPI_FLASH */
|