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>
24 #include <dm/device-internal.h>
26 #ifndef CONFIG_SPI_IDLE_VAL
27 # define CONFIG_SPI_IDLE_VAL 0xFF
30 const char *sandbox_spi_parse_spec(const char *arg, unsigned long *bus,
35 *bus = simple_strtoul(arg, &endp, 0);
36 if (*endp != ':' || *bus >= CONFIG_SANDBOX_SPI_MAX_BUS)
39 *cs = simple_strtoul(endp + 1, &endp, 0);
40 if (*endp != ':' || *cs >= CONFIG_SANDBOX_SPI_MAX_CS)
46 __weak int sandbox_spi_get_emul(struct sandbox_state *state,
47 struct udevice *bus, struct udevice *slave,
48 struct udevice **emulp)
53 static int sandbox_spi_xfer(struct udevice *slave, unsigned int bitlen,
54 const void *dout, void *din, unsigned long flags)
56 struct udevice *bus = slave->parent;
57 struct sandbox_state *state = state_get_current();
58 struct dm_spi_emul_ops *ops;
60 uint bytes = bitlen / 8, i;
67 /* we can only do 8 bit transfers */
69 printf("sandbox_spi: xfer: invalid bitlen size %u; needs to be 8bit\n",
75 cs = spi_chip_select(slave);
76 if (busnum >= CONFIG_SANDBOX_SPI_MAX_BUS ||
77 cs >= CONFIG_SANDBOX_SPI_MAX_CS) {
78 printf("%s: busnum=%u, cs=%u: out of range\n", __func__,
82 ret = sandbox_spi_get_emul(state, bus, slave, &emul);
84 printf("%s: busnum=%u, cs=%u: no emulation available (err=%d)\n",
85 __func__, busnum, cs, ret);
88 ret = device_probe(emul);
92 ops = spi_emul_get_ops(emul);
93 ret = ops->xfer(emul, bitlen, dout, din, flags);
95 log_content("sandbox_spi: xfer: got back %i (that's %s)\n rx:",
96 ret, ret ? "bad" : "good");
98 for (i = 0; i < bytes; ++i)
99 log_content(" %u:%02x", i, ((u8 *)din)[i]);
106 static int sandbox_spi_set_speed(struct udevice *bus, uint speed)
111 static int sandbox_spi_set_mode(struct udevice *bus, uint mode)
116 static int sandbox_cs_info(struct udevice *bus, uint cs,
117 struct spi_cs_info *info)
119 /* Always allow activity on CS 0 */
126 static int sandbox_spi_get_mmap(struct udevice *dev, ulong *map_basep,
127 uint *map_sizep, uint *offsetp)
136 static const struct dm_spi_ops sandbox_spi_ops = {
137 .xfer = sandbox_spi_xfer,
138 .set_speed = sandbox_spi_set_speed,
139 .set_mode = sandbox_spi_set_mode,
140 .cs_info = sandbox_cs_info,
141 .get_mmap = sandbox_spi_get_mmap,
144 static const struct udevice_id sandbox_spi_ids[] = {
145 { .compatible = "sandbox,spi" },
149 U_BOOT_DRIVER(spi_sandbox) = {
150 .name = "spi_sandbox",
152 .of_match = sandbox_spi_ids,
153 .ops = &sandbox_spi_ops,