2013-12-03 23:43:26 +00:00
|
|
|
/*
|
|
|
|
* Simulate a SPI port
|
|
|
|
*
|
|
|
|
* Copyright (c) 2011-2013 The Chromium OS Authors.
|
|
|
|
* See file CREDITS for list of people who contributed to this
|
|
|
|
* project.
|
|
|
|
*
|
|
|
|
* Licensed under the GPL-2 or later.
|
|
|
|
*/
|
|
|
|
|
2018-10-01 17:55:13 +00:00
|
|
|
#define LOG_CATEGORY UCLASS_SPI
|
|
|
|
|
2013-12-03 23:43:26 +00:00
|
|
|
#include <common.h>
|
2014-10-14 05:41:57 +00:00
|
|
|
#include <dm.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2013-12-03 23:43:26 +00:00
|
|
|
#include <malloc.h>
|
|
|
|
#include <spi.h>
|
2014-10-14 05:41:57 +00:00
|
|
|
#include <spi_flash.h>
|
2013-12-03 23:43:26 +00:00
|
|
|
#include <os.h>
|
|
|
|
|
2016-09-21 02:28:55 +00:00
|
|
|
#include <linux/errno.h>
|
2013-12-03 23:43:26 +00:00
|
|
|
#include <asm/spi.h>
|
|
|
|
#include <asm/state.h>
|
2020-07-07 19:11:49 +00:00
|
|
|
#include <dm/acpi.h>
|
2014-10-14 05:41:57 +00:00
|
|
|
#include <dm/device-internal.h>
|
|
|
|
|
2013-12-03 23:43:26 +00:00
|
|
|
#ifndef CONFIG_SPI_IDLE_VAL
|
|
|
|
# define CONFIG_SPI_IDLE_VAL 0xFF
|
|
|
|
#endif
|
|
|
|
|
2020-12-14 17:06:48 +00:00
|
|
|
/**
|
|
|
|
* struct sandbox_spi_priv - Sandbox SPI private data
|
|
|
|
*
|
|
|
|
* Helper struct to keep track of the sandbox SPI bus internal state. It is
|
|
|
|
* used in unit tests to verify that dm spi functions update the bus
|
|
|
|
* speed/mode properly (for instance, when jumping back and forth between spi
|
|
|
|
* slaves claiming the bus, we need to make sure that the bus speed is updated
|
|
|
|
* accordingly for each slave).
|
|
|
|
*
|
|
|
|
* @speed: Current bus speed.
|
|
|
|
* @mode: Current bus mode.
|
|
|
|
*/
|
|
|
|
struct sandbox_spi_priv {
|
|
|
|
uint speed;
|
|
|
|
uint mode;
|
|
|
|
};
|
|
|
|
|
2014-10-14 05:41:57 +00:00
|
|
|
__weak int sandbox_spi_get_emul(struct sandbox_state *state,
|
|
|
|
struct udevice *bus, struct udevice *slave,
|
|
|
|
struct udevice **emulp)
|
2013-12-03 23:43:26 +00:00
|
|
|
{
|
2014-10-14 05:41:57 +00:00
|
|
|
return -ENOENT;
|
2013-12-03 23:43:26 +00:00
|
|
|
}
|
|
|
|
|
2020-12-14 17:06:49 +00:00
|
|
|
uint sandbox_spi_get_speed(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct sandbox_spi_priv *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
return priv->speed;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint sandbox_spi_get_mode(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct sandbox_spi_priv *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
return priv->mode;
|
|
|
|
}
|
|
|
|
|
2014-10-14 05:41:57 +00:00
|
|
|
static int sandbox_spi_xfer(struct udevice *slave, unsigned int bitlen,
|
|
|
|
const void *dout, void *din, unsigned long flags)
|
2013-12-03 23:43:26 +00:00
|
|
|
{
|
2014-10-14 05:41:57 +00:00
|
|
|
struct udevice *bus = slave->parent;
|
2013-12-03 23:43:26 +00:00
|
|
|
struct sandbox_state *state = state_get_current();
|
2014-10-14 05:41:57 +00:00
|
|
|
struct dm_spi_emul_ops *ops;
|
|
|
|
struct udevice *emul;
|
2013-12-03 23:43:26 +00:00
|
|
|
uint bytes = bitlen / 8, i;
|
2014-10-14 05:41:57 +00:00
|
|
|
int ret;
|
|
|
|
uint busnum, cs;
|
2013-12-03 23:43:26 +00:00
|
|
|
|
|
|
|
if (bitlen == 0)
|
2014-10-14 05:41:57 +00:00
|
|
|
return 0;
|
2013-12-03 23:43:26 +00:00
|
|
|
|
|
|
|
/* we can only do 8 bit transfers */
|
|
|
|
if (bitlen % 8) {
|
|
|
|
printf("sandbox_spi: xfer: invalid bitlen size %u; needs to be 8bit\n",
|
|
|
|
bitlen);
|
2014-10-14 05:41:57 +00:00
|
|
|
return -EINVAL;
|
2013-12-03 23:43:26 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 04:20:07 +00:00
|
|
|
busnum = dev_seq(bus);
|
2014-10-14 05:41:57 +00:00
|
|
|
cs = spi_chip_select(slave);
|
|
|
|
if (busnum >= CONFIG_SANDBOX_SPI_MAX_BUS ||
|
|
|
|
cs >= CONFIG_SANDBOX_SPI_MAX_CS) {
|
|
|
|
printf("%s: busnum=%u, cs=%u: out of range\n", __func__,
|
|
|
|
busnum, cs);
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
ret = sandbox_spi_get_emul(state, bus, slave, &emul);
|
|
|
|
if (ret) {
|
|
|
|
printf("%s: busnum=%u, cs=%u: no emulation available (err=%d)\n",
|
|
|
|
__func__, busnum, cs, ret);
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
ret = device_probe(emul);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2013-12-03 23:43:26 +00:00
|
|
|
|
2014-10-14 05:41:57 +00:00
|
|
|
ops = spi_emul_get_ops(emul);
|
|
|
|
ret = ops->xfer(emul, bitlen, dout, din, flags);
|
2013-12-03 23:43:26 +00:00
|
|
|
|
2018-10-01 17:55:13 +00:00
|
|
|
log_content("sandbox_spi: xfer: got back %i (that's %s)\n rx:",
|
|
|
|
ret, ret ? "bad" : "good");
|
|
|
|
if (din) {
|
|
|
|
for (i = 0; i < bytes; ++i)
|
|
|
|
log_content(" %u:%02x", i, ((u8 *)din)[i]);
|
|
|
|
}
|
|
|
|
log_content("\n");
|
2013-12-03 23:43:26 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2014-02-27 20:26:24 +00:00
|
|
|
|
2014-10-14 05:41:57 +00:00
|
|
|
static int sandbox_spi_set_speed(struct udevice *bus, uint speed)
|
|
|
|
{
|
2020-12-14 17:06:48 +00:00
|
|
|
struct sandbox_spi_priv *priv = dev_get_priv(bus);
|
|
|
|
|
|
|
|
priv->speed = speed;
|
|
|
|
|
2014-10-14 05:41:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sandbox_spi_set_mode(struct udevice *bus, uint mode)
|
|
|
|
{
|
2020-12-14 17:06:48 +00:00
|
|
|
struct sandbox_spi_priv *priv = dev_get_priv(bus);
|
|
|
|
|
|
|
|
priv->mode = mode;
|
|
|
|
|
2014-10-14 05:41:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sandbox_cs_info(struct udevice *bus, uint cs,
|
|
|
|
struct spi_cs_info *info)
|
2014-02-27 20:26:24 +00:00
|
|
|
{
|
2020-12-14 17:06:47 +00:00
|
|
|
/* Always allow activity on CS 0, CS 1 */
|
|
|
|
if (cs >= 2)
|
2019-09-09 13:00:01 +00:00
|
|
|
return -EINVAL;
|
2014-10-14 05:41:57 +00:00
|
|
|
|
|
|
|
return 0;
|
2014-02-27 20:26:24 +00:00
|
|
|
}
|
2014-10-14 05:41:57 +00:00
|
|
|
|
2019-10-21 03:31:47 +00:00
|
|
|
static int sandbox_spi_get_mmap(struct udevice *dev, ulong *map_basep,
|
|
|
|
uint *map_sizep, uint *offsetp)
|
|
|
|
{
|
|
|
|
*map_basep = 0x1000;
|
|
|
|
*map_sizep = 0x2000;
|
|
|
|
*offsetp = 0x100;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-14 05:41:57 +00:00
|
|
|
static const struct dm_spi_ops sandbox_spi_ops = {
|
|
|
|
.xfer = sandbox_spi_xfer,
|
|
|
|
.set_speed = sandbox_spi_set_speed,
|
|
|
|
.set_mode = sandbox_spi_set_mode,
|
|
|
|
.cs_info = sandbox_cs_info,
|
2019-10-21 03:31:47 +00:00
|
|
|
.get_mmap = sandbox_spi_get_mmap,
|
2014-10-14 05:41:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct udevice_id sandbox_spi_ids[] = {
|
|
|
|
{ .compatible = "sandbox,spi" },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
2020-06-25 04:10:04 +00:00
|
|
|
U_BOOT_DRIVER(sandbox_spi) = {
|
|
|
|
.name = "sandbox_spi",
|
2014-10-14 05:41:57 +00:00
|
|
|
.id = UCLASS_SPI,
|
|
|
|
.of_match = sandbox_spi_ids,
|
|
|
|
.ops = &sandbox_spi_ops,
|
2020-12-14 17:06:48 +00:00
|
|
|
.priv_auto = sizeof(struct sandbox_spi_priv),
|
2014-10-14 05:41:57 +00:00
|
|
|
};
|