2013-08-29 22:31:56 +09:00
|
|
|
/*
|
|
|
|
* SPI flash probing
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 Atmel Corporation
|
|
|
|
* Copyright (C) 2010 Reinhard Meyer, EMK Elektronik
|
|
|
|
* Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc.
|
|
|
|
*
|
2013-10-11 02:02:55 +09:00
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
2013-08-29 22:31:56 +09:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2014-10-14 14:42:07 +09:00
|
|
|
#include <dm.h>
|
2014-10-14 14:42:05 +09:00
|
|
|
#include <errno.h>
|
2013-08-29 22:31:56 +09:00
|
|
|
#include <malloc.h>
|
|
|
|
#include <spi.h>
|
|
|
|
#include <spi_flash.h>
|
|
|
|
|
2013-09-26 19:30:15 +09:00
|
|
|
#include "sf_internal.h"
|
2013-08-29 22:31:56 +09:00
|
|
|
|
2014-10-14 14:42:05 +09: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-07 01:14:12 +09:00
|
|
|
static int spi_flash_probe_slave(struct spi_flash *flash)
|
2013-08-29 22:31:56 +09:00
|
|
|
{
|
2015-12-07 01:03:32 +09:00
|
|
|
struct spi_slave *spi = flash->spi;
|
2013-09-24 19:31:23 +09:00
|
|
|
int ret;
|
2013-08-29 22:31:56 +09:00
|
|
|
|
2013-09-24 19:31:23 +09:00
|
|
|
/* Setup spi_slave */
|
2013-08-29 22:31:56 +09:00
|
|
|
if (!spi) {
|
|
|
|
printf("SF: Failed to set up slave\n");
|
2014-10-14 14:42:05 +09:00
|
|
|
return -ENODEV;
|
2013-08-29 22:31:56 +09:00
|
|
|
}
|
|
|
|
|
2013-09-24 19:31:23 +09:00
|
|
|
/* Claim spi bus */
|
2013-08-29 22:31:56 +09:00
|
|
|
ret = spi_claim_bus(spi);
|
|
|
|
if (ret) {
|
|
|
|
debug("SF: Failed to claim SPI bus: %d\n", ret);
|
2014-10-14 14:42:05 +09:00
|
|
|
return ret;
|
2013-08-29 22:31:56 +09:00
|
|
|
}
|
|
|
|
|
2015-12-07 01:03:32 +09:00
|
|
|
ret = spi_flash_scan(flash);
|
2015-09-29 20:58:20 +09:00
|
|
|
if (ret) {
|
2014-10-14 14:42:05 +09:00
|
|
|
ret = -EINVAL;
|
2013-09-24 19:31:23 +09:00
|
|
|
goto err_read_id;
|
2014-10-14 14:42:05 +09:00
|
|
|
}
|
2013-08-29 22:31:56 +09:00
|
|
|
|
2015-04-27 14:42:04 +09:00
|
|
|
#ifdef CONFIG_SPI_FLASH_MTD
|
|
|
|
ret = spi_flash_mtd_register(flash);
|
|
|
|
#endif
|
2013-08-29 22:31:56 +09:00
|
|
|
|
|
|
|
err_read_id:
|
|
|
|
spi_release_bus(spi);
|
2014-10-14 14:42:05 +09:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-10-14 14:42:07 +09:00
|
|
|
#ifndef CONFIG_DM_SPI_FLASH
|
2015-12-07 01:14:12 +09:00
|
|
|
static struct spi_flash *spi_flash_probe_tail(struct spi_slave *bus)
|
2014-10-14 14:42:05 +09:00
|
|
|
{
|
|
|
|
struct spi_flash *flash;
|
|
|
|
|
|
|
|
/* 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-07 01:03:32 +09:00
|
|
|
flash->spi = bus;
|
|
|
|
if (spi_flash_probe_slave(flash)) {
|
2014-10-14 14:42:05 +09:00
|
|
|
spi_free_slave(bus);
|
|
|
|
free(flash);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return flash;
|
2013-08-29 22:31:56 +09:00
|
|
|
}
|
|
|
|
|
2014-10-14 14:42:05 +09:00
|
|
|
struct spi_flash *spi_flash_probe(unsigned int busnum, unsigned int cs,
|
2013-12-04 08:43:24 +09:00
|
|
|
unsigned int max_hz, unsigned int spi_mode)
|
|
|
|
{
|
2014-10-14 14:42:05 +09:00
|
|
|
struct spi_slave *bus;
|
2013-12-04 08:43:24 +09:00
|
|
|
|
2014-10-14 14:42:05 +09:00
|
|
|
bus = spi_setup_slave(busnum, cs, max_hz, spi_mode);
|
2015-03-20 14:19:16 +09:00
|
|
|
if (!bus)
|
|
|
|
return NULL;
|
2014-10-14 14:42:05 +09:00
|
|
|
return spi_flash_probe_tail(bus);
|
2013-12-04 08:43:24 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_OF_SPI_FLASH
|
|
|
|
struct spi_flash *spi_flash_probe_fdt(const void *blob, int slave_node,
|
|
|
|
int spi_node)
|
|
|
|
{
|
2014-10-14 14:42:05 +09:00
|
|
|
struct spi_slave *bus;
|
2013-12-04 08:43:24 +09:00
|
|
|
|
2014-10-14 14:42:05 +09:00
|
|
|
bus = spi_setup_slave_fdt(blob, slave_node, spi_node);
|
2015-03-20 14:19:16 +09:00
|
|
|
if (!bus)
|
|
|
|
return NULL;
|
2014-10-14 14:42:05 +09:00
|
|
|
return spi_flash_probe_tail(bus);
|
2013-12-04 08:43:24 +09:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-08-29 22:31:56 +09:00
|
|
|
void spi_flash_free(struct spi_flash *flash)
|
|
|
|
{
|
2015-04-27 14:42:04 +09:00
|
|
|
#ifdef CONFIG_SPI_FLASH_MTD
|
|
|
|
spi_flash_mtd_unregister();
|
|
|
|
#endif
|
2013-08-29 22:31:56 +09:00
|
|
|
spi_free_slave(flash->spi);
|
|
|
|
free(flash);
|
|
|
|
}
|
2014-10-14 14:42:07 +09: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-06 04:25:20 +09:00
|
|
|
struct spi_flash *flash = dev_get_uclass_priv(dev);
|
2014-10-14 14:42:07 +09:00
|
|
|
|
|
|
|
return spi_flash_cmd_read_ops(flash, offset, len, buf);
|
|
|
|
}
|
|
|
|
|
2015-12-07 01:14:12 +09:00
|
|
|
static int spi_flash_std_write(struct udevice *dev, u32 offset, size_t len,
|
2014-10-14 14:42:07 +09:00
|
|
|
const void *buf)
|
|
|
|
{
|
2015-03-06 04:25:20 +09:00
|
|
|
struct spi_flash *flash = dev_get_uclass_priv(dev);
|
2014-10-14 14:42:07 +09:00
|
|
|
|
2015-04-24 20:51:10 +09:00
|
|
|
#if defined(CONFIG_SPI_FLASH_SST)
|
2015-09-29 14:47:02 +09:00
|
|
|
if (flash->flags & SNOR_F_SST_WR) {
|
2015-04-24 20:51:10 +09:00
|
|
|
if (flash->spi->op_mode_tx & SPI_OPM_TX_BP)
|
|
|
|
return sst_write_bp(flash, offset, len, buf);
|
|
|
|
else
|
|
|
|
return sst_write_wp(flash, offset, len, buf);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-10-14 14:42:07 +09:00
|
|
|
return spi_flash_cmd_write_ops(flash, offset, len, buf);
|
|
|
|
}
|
|
|
|
|
2015-12-07 01:14:12 +09:00
|
|
|
static int spi_flash_std_erase(struct udevice *dev, u32 offset, size_t len)
|
2014-10-14 14:42:07 +09:00
|
|
|
{
|
2015-03-06 04:25:20 +09:00
|
|
|
struct spi_flash *flash = dev_get_uclass_priv(dev);
|
2014-10-14 14:42:07 +09:00
|
|
|
|
|
|
|
return spi_flash_cmd_erase_ops(flash, offset, len);
|
|
|
|
}
|
|
|
|
|
2015-12-07 01:14:12 +09:00
|
|
|
static int spi_flash_std_probe(struct udevice *dev)
|
2014-10-14 14:42:07 +09:00
|
|
|
{
|
2015-09-29 14:32:01 +09:00
|
|
|
struct spi_slave *slave = dev_get_parent_priv(dev);
|
2015-01-26 00:27:12 +09:00
|
|
|
struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
|
2014-10-14 14:42:07 +09:00
|
|
|
struct spi_flash *flash;
|
|
|
|
|
2015-03-06 04:25:20 +09:00
|
|
|
flash = dev_get_uclass_priv(dev);
|
2014-10-14 14:42:07 +09:00
|
|
|
flash->dev = dev;
|
2015-12-07 01:03:32 +09:00
|
|
|
flash->spi = slave;
|
2015-01-26 00:27:12 +09:00
|
|
|
debug("%s: slave=%p, cs=%d\n", __func__, slave, plat->cs);
|
2015-12-07 01:03:32 +09:00
|
|
|
return spi_flash_probe_slave(flash);
|
2014-10-14 14:42:07 +09: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[] = {
|
|
|
|
{ .compatible = "spi-flash" },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
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,
|
|
|
|
.priv_auto_alloc_size = sizeof(struct spi_flash),
|
|
|
|
.ops = &spi_flash_std_ops,
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* CONFIG_DM_SPI_FLASH */
|