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>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.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
|
|
|
|
2020-05-12 18:41:27 +00:00
|
|
|
if (CONFIG_IS_ENABLED(SPI_FLASH_MTD))
|
|
|
|
ret = spi_flash_mtd_register(flash);
|
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;
|
|
|
|
}
|
|
|
|
|
2020-06-04 15:11:53 +00:00
|
|
|
#if !CONFIG_IS_ENABLED(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)
|
|
|
|
{
|
2020-05-12 18:41:27 +00:00
|
|
|
if (CONFIG_IS_ENABLED(SPI_FLASH_MTD))
|
|
|
|
spi_flash_mtd_unregister();
|
|
|
|
|
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) {
|
2020-07-19 16:15:32 +00:00
|
|
|
debug("SF: Erase offset/length not multiple of erase size\n");
|
2019-02-05 05:59:23 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-12-07 04:42:50 +00:00
|
|
|
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);
|
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;
|
|
|
|
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)
|
|
|
|
{
|
2020-05-12 18:41:27 +00:00
|
|
|
if (CONFIG_IS_ENABLED(SPI_FLASH_MTD))
|
|
|
|
spi_flash_mtd_unregister();
|
|
|
|
|
2018-12-02 09:54:31 +00:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct udevice_id spi_flash_std_ids[] = {
|
2019-02-05 05:59:22 +00:00
|
|
|
{ .compatible = "jedec,spi-nor" },
|
2014-10-14 05:42:07 +00:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
2020-06-25 04:10:04 +00:00
|
|
|
U_BOOT_DRIVER(jedec_spi_nor) = {
|
|
|
|
.name = "jedec_spi_nor",
|
2014-10-14 05:42:07 +00:00
|
|
|
.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,
|
2020-12-03 23:55:17 +00:00
|
|
|
.priv_auto = sizeof(struct spi_flash),
|
2014-10-14 05:42:07 +00:00
|
|
|
.ops = &spi_flash_std_ops,
|
|
|
|
};
|
|
|
|
|
2020-06-25 04:10:06 +00:00
|
|
|
U_BOOT_DRIVER_ALIAS(jedec_spi_nor, spansion_m25p16)
|
|
|
|
|
2014-10-14 05:42:07 +00:00
|
|
|
#endif /* CONFIG_DM_SPI_FLASH */
|