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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2014-10-14 05:41:57 +00:00
|
|
|
#include <dm.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>
|
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
|
|
|
|
|
|
|
|
const char *sandbox_spi_parse_spec(const char *arg, unsigned long *bus,
|
|
|
|
unsigned long *cs)
|
|
|
|
{
|
|
|
|
char *endp;
|
|
|
|
|
|
|
|
*bus = simple_strtoul(arg, &endp, 0);
|
|
|
|
if (*endp != ':' || *bus >= CONFIG_SANDBOX_SPI_MAX_BUS)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
*cs = simple_strtoul(endp + 1, &endp, 0);
|
|
|
|
if (*endp != ':' || *cs >= CONFIG_SANDBOX_SPI_MAX_CS)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return endp + 1;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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;
|
2013-12-03 23:43:26 +00:00
|
|
|
u8 *tx = (void *)dout, *rx = din;
|
2014-10-14 05:41:57 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-10-14 05:41:57 +00:00
|
|
|
busnum = bus->seq;
|
|
|
|
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
|
|
|
|
|
|
|
/* make sure rx/tx buffers are full so clients can assume */
|
|
|
|
if (!tx) {
|
|
|
|
debug("sandbox_spi: xfer: auto-allocating tx scratch buffer\n");
|
|
|
|
tx = malloc(bytes);
|
|
|
|
if (!tx) {
|
|
|
|
debug("sandbox_spi: Out of memory\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!rx) {
|
|
|
|
debug("sandbox_spi: xfer: auto-allocating rx scratch buffer\n");
|
|
|
|
rx = malloc(bytes);
|
|
|
|
if (!rx) {
|
|
|
|
debug("sandbox_spi: Out of memory\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
debug("sandbox_spi: xfer: got back %i (that's %s)\n rx:",
|
|
|
|
ret, ret ? "bad" : "good");
|
|
|
|
for (i = 0; i < bytes; ++i)
|
|
|
|
debug(" %u:%02x", i, rx[i]);
|
|
|
|
debug("\n");
|
|
|
|
|
|
|
|
if (tx != dout)
|
|
|
|
free(tx);
|
|
|
|
if (rx != din)
|
|
|
|
free(rx);
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sandbox_spi_set_mode(struct udevice *bus, uint mode)
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2014-10-14 05:41:57 +00:00
|
|
|
/* Always allow activity on CS 0 */
|
|
|
|
if (cs >= 1)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
return 0;
|
2014-02-27 20:26:24 +00:00
|
|
|
}
|
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,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct udevice_id sandbox_spi_ids[] = {
|
|
|
|
{ .compatible = "sandbox,spi" },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(spi_sandbox) = {
|
|
|
|
.name = "spi_sandbox",
|
|
|
|
.id = UCLASS_SPI,
|
|
|
|
.of_match = sandbox_spi_ids,
|
|
|
|
.ops = &sandbox_spi_ops,
|
|
|
|
};
|