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
18 #include <spi_flash.h>
21 #include <linux/errno.h>
23 #include <asm/state.h>
25 #include <dm/device-internal.h>
27 #ifndef CONFIG_SPI_IDLE_VAL
28 # define CONFIG_SPI_IDLE_VAL 0xFF
32 * struct sandbox_spi_priv - Sandbox SPI private data
34 * Helper struct to keep track of the sandbox SPI bus internal state. It is
35 * used in unit tests to verify that dm spi functions update the bus
36 * speed/mode properly (for instance, when jumping back and forth between spi
37 * slaves claiming the bus, we need to make sure that the bus speed is updated
38 * accordingly for each slave).
40 * @speed: Current bus speed.
41 * @mode: Current bus mode.
43 struct sandbox_spi_priv {
48 __weak int sandbox_spi_get_emul(struct sandbox_state *state,
49 struct udevice *bus, struct udevice *slave,
50 struct udevice **emulp)
55 uint sandbox_spi_get_speed(struct udevice *dev)
57 struct sandbox_spi_priv *priv = dev_get_priv(dev);
62 uint sandbox_spi_get_mode(struct udevice *dev)
64 struct sandbox_spi_priv *priv = dev_get_priv(dev);
69 static int sandbox_spi_xfer(struct udevice *slave, unsigned int bitlen,
70 const void *dout, void *din, unsigned long flags)
72 struct udevice *bus = slave->parent;
73 struct sandbox_state *state = state_get_current();
74 struct dm_spi_emul_ops *ops;
76 uint bytes = bitlen / 8, i;
83 /* we can only do 8 bit transfers */
85 printf("sandbox_spi: xfer: invalid bitlen size %u; needs to be 8bit\n",
90 busnum = dev_seq(bus);
91 cs = spi_chip_select(slave);
92 if (busnum >= CONFIG_SANDBOX_SPI_MAX_BUS ||
93 cs >= CONFIG_SANDBOX_SPI_MAX_CS) {
94 printf("%s: busnum=%u, cs=%u: out of range\n", __func__,
98 ret = sandbox_spi_get_emul(state, bus, slave, &emul);
100 printf("%s: busnum=%u, cs=%u: no emulation available (err=%d)\n",
101 __func__, busnum, cs, ret);
104 ret = device_probe(emul);
108 ops = spi_emul_get_ops(emul);
109 ret = ops->xfer(emul, bitlen, dout, din, flags);
111 log_content("sandbox_spi: xfer: got back %i (that's %s)\n rx:",
112 ret, ret ? "bad" : "good");
114 for (i = 0; i < bytes; ++i)
115 log_content(" %u:%02x", i, ((u8 *)din)[i]);
122 static int sandbox_spi_set_speed(struct udevice *bus, uint speed)
124 struct sandbox_spi_priv *priv = dev_get_priv(bus);
131 static int sandbox_spi_set_mode(struct udevice *bus, uint mode)
133 struct sandbox_spi_priv *priv = dev_get_priv(bus);
140 static int sandbox_cs_info(struct udevice *bus, uint cs,
141 struct spi_cs_info *info)
143 /* Always allow activity on CS 0, CS 1 */
150 static int sandbox_spi_get_mmap(struct udevice *dev, ulong *map_basep,
151 uint *map_sizep, uint *offsetp)
160 static const struct dm_spi_ops sandbox_spi_ops = {
161 .xfer = sandbox_spi_xfer,
162 .set_speed = sandbox_spi_set_speed,
163 .set_mode = sandbox_spi_set_mode,
164 .cs_info = sandbox_cs_info,
165 .get_mmap = sandbox_spi_get_mmap,
168 static const struct udevice_id sandbox_spi_ids[] = {
169 { .compatible = "sandbox,spi" },
173 U_BOOT_DRIVER(sandbox_spi) = {
174 .name = "sandbox_spi",
176 .of_match = sandbox_spi_ids,
177 .ops = &sandbox_spi_ops,
178 .priv_auto = sizeof(struct sandbox_spi_priv),