4 * Copyright (c) 2011-2013 The Chromium OS Authors.
5 * See file CREDITS for list of people who contributed to this
8 * Licensed under the GPL-2 or later.
11 #define LOG_CATEGORY UCLASS_SPI
17 #include <spi_flash.h>
20 #include <linux/errno.h>
22 #include <asm/state.h>
24 #include <dm/device-internal.h>
27 * struct sandbox_spi_priv - Sandbox SPI private data
29 * Helper struct to keep track of the sandbox SPI bus internal state. It is
30 * used in unit tests to verify that dm spi functions update the bus
31 * speed/mode properly (for instance, when jumping back and forth between spi
32 * slaves claiming the bus, we need to make sure that the bus speed is updated
33 * accordingly for each slave).
35 * @speed: Current bus speed.
36 * @mode: Current bus mode.
38 struct sandbox_spi_priv {
43 __weak int sandbox_spi_get_emul(struct sandbox_state *state,
44 struct udevice *bus, struct udevice *slave,
45 struct udevice **emulp)
50 uint sandbox_spi_get_speed(struct udevice *dev)
52 struct sandbox_spi_priv *priv = dev_get_priv(dev);
57 uint sandbox_spi_get_mode(struct udevice *dev)
59 struct sandbox_spi_priv *priv = dev_get_priv(dev);
64 static int sandbox_spi_xfer(struct udevice *slave, unsigned int bitlen,
65 const void *dout, void *din, unsigned long flags)
67 struct udevice *bus = slave->parent;
68 struct sandbox_state *state = state_get_current();
69 struct dm_spi_emul_ops *ops;
71 uint bytes = bitlen / 8, i;
78 /* we can only do 8 bit transfers */
80 printf("sandbox_spi: xfer: invalid bitlen size %u; needs to be 8bit\n",
85 busnum = dev_seq(bus);
86 cs = spi_chip_select(slave);
87 if (busnum >= CONFIG_SANDBOX_SPI_MAX_BUS ||
88 cs >= CONFIG_SANDBOX_SPI_MAX_CS) {
89 printf("%s: busnum=%u, cs=%u: out of range\n", __func__,
93 ret = sandbox_spi_get_emul(state, bus, slave, &emul);
95 printf("%s: busnum=%u, cs=%u: no emulation available (err=%d)\n",
96 __func__, busnum, cs, ret);
99 ret = device_probe(emul);
103 ops = spi_emul_get_ops(emul);
104 ret = ops->xfer(emul, bitlen, dout, din, flags);
106 log_content("sandbox_spi: xfer: got back %i (that's %s)\n rx:",
107 ret, ret ? "bad" : "good");
109 for (i = 0; i < bytes; ++i)
110 log_content(" %u:%02x", i, ((u8 *)din)[i]);
117 static int sandbox_spi_set_speed(struct udevice *bus, uint speed)
119 struct sandbox_spi_priv *priv = dev_get_priv(bus);
126 static int sandbox_spi_set_mode(struct udevice *bus, uint mode)
128 struct sandbox_spi_priv *priv = dev_get_priv(bus);
135 static int sandbox_cs_info(struct udevice *bus, uint cs,
136 struct spi_cs_info *info)
138 /* Always allow activity on CS 0, CS 1 */
145 static int sandbox_spi_get_mmap(struct udevice *dev, ulong *map_basep,
146 uint *map_sizep, uint *offsetp)
155 static const struct dm_spi_ops sandbox_spi_ops = {
156 .xfer = sandbox_spi_xfer,
157 .set_speed = sandbox_spi_set_speed,
158 .set_mode = sandbox_spi_set_mode,
159 .cs_info = sandbox_cs_info,
160 .get_mmap = sandbox_spi_get_mmap,
163 static const struct udevice_id sandbox_spi_ids[] = {
164 { .compatible = "sandbox,spi" },
168 U_BOOT_DRIVER(sandbox_spi) = {
169 .name = "sandbox_spi",
171 .of_match = sandbox_spi_ids,
172 .ops = &sandbox_spi_ops,
173 .priv_auto = sizeof(struct sandbox_spi_priv),