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>
23 #include <dm/device-internal.h>
25 #ifndef CONFIG_SPI_IDLE_VAL
26 # define CONFIG_SPI_IDLE_VAL 0xFF
29 const char *sandbox_spi_parse_spec(const char *arg, unsigned long *bus,
34 *bus = simple_strtoul(arg, &endp, 0);
35 if (*endp != ':' || *bus >= CONFIG_SANDBOX_SPI_MAX_BUS)
38 *cs = simple_strtoul(endp + 1, &endp, 0);
39 if (*endp != ':' || *cs >= CONFIG_SANDBOX_SPI_MAX_CS)
45 __weak int sandbox_spi_get_emul(struct sandbox_state *state,
46 struct udevice *bus, struct udevice *slave,
47 struct udevice **emulp)
52 static int sandbox_spi_xfer(struct udevice *slave, unsigned int bitlen,
53 const void *dout, void *din, unsigned long flags)
55 struct udevice *bus = slave->parent;
56 struct sandbox_state *state = state_get_current();
57 struct dm_spi_emul_ops *ops;
59 uint bytes = bitlen / 8, i;
66 /* we can only do 8 bit transfers */
68 printf("sandbox_spi: xfer: invalid bitlen size %u; needs to be 8bit\n",
74 cs = spi_chip_select(slave);
75 if (busnum >= CONFIG_SANDBOX_SPI_MAX_BUS ||
76 cs >= CONFIG_SANDBOX_SPI_MAX_CS) {
77 printf("%s: busnum=%u, cs=%u: out of range\n", __func__,
81 ret = sandbox_spi_get_emul(state, bus, slave, &emul);
83 printf("%s: busnum=%u, cs=%u: no emulation available (err=%d)\n",
84 __func__, busnum, cs, ret);
87 ret = device_probe(emul);
91 ops = spi_emul_get_ops(emul);
92 ret = ops->xfer(emul, bitlen, dout, din, flags);
94 log_content("sandbox_spi: xfer: got back %i (that's %s)\n rx:",
95 ret, ret ? "bad" : "good");
97 for (i = 0; i < bytes; ++i)
98 log_content(" %u:%02x", i, ((u8 *)din)[i]);
105 static int sandbox_spi_set_speed(struct udevice *bus, uint speed)
110 static int sandbox_spi_set_mode(struct udevice *bus, uint mode)
115 static int sandbox_cs_info(struct udevice *bus, uint cs,
116 struct spi_cs_info *info)
118 /* Always allow activity on CS 0 */
125 static const struct dm_spi_ops sandbox_spi_ops = {
126 .xfer = sandbox_spi_xfer,
127 .set_speed = sandbox_spi_set_speed,
128 .set_mode = sandbox_spi_set_mode,
129 .cs_info = sandbox_cs_info,
132 static const struct udevice_id sandbox_spi_ids[] = {
133 { .compatible = "sandbox,spi" },
137 U_BOOT_DRIVER(spi_sandbox) = {
138 .name = "spi_sandbox",
140 .of_match = sandbox_spi_ids,
141 .ops = &sandbox_spi_ops,