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 DECLARE_GLOBAL_DATA_PTR
;
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
;
61 u8
*tx
= (void *)dout
, *rx
= din
;
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 /* make sure rx/tx buffers are full so clients can assume */
94 debug("sandbox_spi: xfer: auto-allocating tx scratch buffer\n");
97 debug("sandbox_spi: Out of memory\n");
102 debug("sandbox_spi: xfer: auto-allocating rx scratch buffer\n");
105 debug("sandbox_spi: Out of memory\n");
110 ops
= spi_emul_get_ops(emul
);
111 ret
= ops
->xfer(emul
, bitlen
, dout
, din
, flags
);
113 debug("sandbox_spi: xfer: got back %i (that's %s)\n rx:",
114 ret
, ret
? "bad" : "good");
115 for (i
= 0; i
< bytes
; ++i
)
116 debug(" %u:%02x", i
, rx
[i
]);
127 static int sandbox_spi_set_speed(struct udevice
*bus
, uint speed
)
132 static int sandbox_spi_set_mode(struct udevice
*bus
, uint mode
)
137 static int sandbox_cs_info(struct udevice
*bus
, uint cs
,
138 struct spi_cs_info
*info
)
140 /* Always allow activity on CS 0 */
147 static const struct dm_spi_ops sandbox_spi_ops
= {
148 .xfer
= sandbox_spi_xfer
,
149 .set_speed
= sandbox_spi_set_speed
,
150 .set_mode
= sandbox_spi_set_mode
,
151 .cs_info
= sandbox_cs_info
,
154 static const struct udevice_id sandbox_spi_ids
[] = {
155 { .compatible
= "sandbox,spi" },
159 U_BOOT_DRIVER(spi_sandbox
) = {
160 .name
= "spi_sandbox",
162 .of_match
= sandbox_spi_ids
,
163 .ops
= &sandbox_spi_ops
,