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.
15 #include <spi_flash.h>
18 #include <linux/errno.h>
20 #include <asm/state.h>
21 #include <dm/device-internal.h>
23 #ifndef CONFIG_SPI_IDLE_VAL
24 # define CONFIG_SPI_IDLE_VAL 0xFF
27 const char *sandbox_spi_parse_spec(const char *arg, unsigned long *bus,
32 *bus = simple_strtoul(arg, &endp, 0);
33 if (*endp != ':' || *bus >= CONFIG_SANDBOX_SPI_MAX_BUS)
36 *cs = simple_strtoul(endp + 1, &endp, 0);
37 if (*endp != ':' || *cs >= CONFIG_SANDBOX_SPI_MAX_CS)
43 __weak int sandbox_spi_get_emul(struct sandbox_state *state,
44 struct udevice *bus, struct udevice *slave,
45 struct udevice **emulp)
50 static int sandbox_spi_xfer(struct udevice *slave, unsigned int bitlen,
51 const void *dout, void *din, unsigned long flags)
53 struct udevice *bus = slave->parent;
54 struct sandbox_state *state = state_get_current();
55 struct dm_spi_emul_ops *ops;
57 uint bytes = bitlen / 8, i;
59 u8 *tx = (void *)dout, *rx = din;
65 /* we can only do 8 bit transfers */
67 printf("sandbox_spi: xfer: invalid bitlen size %u; needs to be 8bit\n",
73 cs = spi_chip_select(slave);
74 if (busnum >= CONFIG_SANDBOX_SPI_MAX_BUS ||
75 cs >= CONFIG_SANDBOX_SPI_MAX_CS) {
76 printf("%s: busnum=%u, cs=%u: out of range\n", __func__,
80 ret = sandbox_spi_get_emul(state, bus, slave, &emul);
82 printf("%s: busnum=%u, cs=%u: no emulation available (err=%d)\n",
83 __func__, busnum, cs, ret);
86 ret = device_probe(emul);
90 /* make sure rx/tx buffers are full so clients can assume */
92 debug("sandbox_spi: xfer: auto-allocating tx scratch buffer\n");
95 debug("sandbox_spi: Out of memory\n");
100 debug("sandbox_spi: xfer: auto-allocating rx scratch buffer\n");
103 debug("sandbox_spi: Out of memory\n");
108 ops = spi_emul_get_ops(emul);
109 ret = ops->xfer(emul, bitlen, dout, din, flags);
111 debug("sandbox_spi: xfer: got back %i (that's %s)\n rx:",
112 ret, ret ? "bad" : "good");
113 for (i = 0; i < bytes; ++i)
114 debug(" %u:%02x", i, rx[i]);
125 static int sandbox_spi_set_speed(struct udevice *bus, uint speed)
130 static int sandbox_spi_set_mode(struct udevice *bus, uint mode)
135 static int sandbox_cs_info(struct udevice *bus, uint cs,
136 struct spi_cs_info *info)
138 /* Always allow activity on CS 0 */
145 static const struct dm_spi_ops sandbox_spi_ops = {
146 .xfer = sandbox_spi_xfer,
147 .set_speed = sandbox_spi_set_speed,
148 .set_mode = sandbox_spi_set_mode,
149 .cs_info = sandbox_cs_info,
152 static const struct udevice_id sandbox_spi_ids[] = {
153 { .compatible = "sandbox,spi" },
157 U_BOOT_DRIVER(spi_sandbox) = {
158 .name = "spi_sandbox",
160 .of_match = sandbox_spi_ids,
161 .ops = &sandbox_spi_ops,