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.
16 #include <asm/errno.h>
18 #include <asm/state.h>
20 #ifndef CONFIG_SPI_IDLE_VAL
21 # define CONFIG_SPI_IDLE_VAL 0xFF
24 struct sandbox_spi_slave
{
25 struct spi_slave slave
;
26 const struct sandbox_spi_emu_ops
*ops
;
30 #define to_sandbox_spi_slave(s) container_of(s, struct sandbox_spi_slave, slave)
32 const char *sandbox_spi_parse_spec(const char *arg
, unsigned long *bus
,
37 *bus
= simple_strtoul(arg
, &endp
, 0);
38 if (*endp
!= ':' || *bus
>= CONFIG_SANDBOX_SPI_MAX_BUS
)
41 *cs
= simple_strtoul(endp
+ 1, &endp
, 0);
42 if (*endp
!= ':' || *cs
>= CONFIG_SANDBOX_SPI_MAX_CS
)
48 int spi_cs_is_valid(unsigned int bus
, unsigned int cs
)
50 return bus
< CONFIG_SANDBOX_SPI_MAX_BUS
&&
51 cs
< CONFIG_SANDBOX_SPI_MAX_CS
;
54 void spi_cs_activate(struct spi_slave
*slave
)
56 struct sandbox_spi_slave
*sss
= to_sandbox_spi_slave(slave
);
58 debug("sandbox_spi: activating CS\n");
59 if (sss
->ops
->cs_activate
)
60 sss
->ops
->cs_activate(sss
->priv
);
63 void spi_cs_deactivate(struct spi_slave
*slave
)
65 struct sandbox_spi_slave
*sss
= to_sandbox_spi_slave(slave
);
67 debug("sandbox_spi: deactivating CS\n");
68 if (sss
->ops
->cs_deactivate
)
69 sss
->ops
->cs_deactivate(sss
->priv
);
76 void spi_set_speed(struct spi_slave
*slave
, uint hz
)
80 struct spi_slave
*spi_setup_slave(unsigned int bus
, unsigned int cs
,
81 unsigned int max_hz
, unsigned int mode
)
83 struct sandbox_spi_slave
*sss
;
84 struct sandbox_state
*state
= state_get_current();
87 if (!spi_cs_is_valid(bus
, cs
)) {
88 debug("sandbox_spi: Invalid SPI bus/cs\n");
92 sss
= spi_alloc_slave(struct sandbox_spi_slave
, bus
, cs
);
94 debug("sandbox_spi: Out of memory\n");
98 spec
= state
->spi
[bus
][cs
].spec
;
99 sss
->ops
= state
->spi
[bus
][cs
].ops
;
100 if (!spec
|| !sss
->ops
|| sss
->ops
->setup(&sss
->priv
, spec
)) {
102 printf("sandbox_spi: unable to locate a slave client\n");
109 void spi_free_slave(struct spi_slave
*slave
)
111 struct sandbox_spi_slave
*sss
= to_sandbox_spi_slave(slave
);
113 debug("sandbox_spi: releasing slave\n");
116 sss
->ops
->free(sss
->priv
);
121 static int spi_bus_claim_cnt
[CONFIG_SANDBOX_SPI_MAX_BUS
];
123 int spi_claim_bus(struct spi_slave
*slave
)
125 if (spi_bus_claim_cnt
[slave
->bus
]++) {
126 printf("sandbox_spi: error: bus already claimed: %d!\n",
127 spi_bus_claim_cnt
[slave
->bus
]);
133 void spi_release_bus(struct spi_slave
*slave
)
135 if (--spi_bus_claim_cnt
[slave
->bus
]) {
136 printf("sandbox_spi: error: bus freed too often: %d!\n",
137 spi_bus_claim_cnt
[slave
->bus
]);
141 int spi_xfer(struct spi_slave
*slave
, unsigned int bitlen
, const void *dout
,
142 void *din
, unsigned long flags
)
144 struct sandbox_spi_slave
*sss
= to_sandbox_spi_slave(slave
);
145 uint bytes
= bitlen
/ 8, i
;
147 u8
*tx
= (void *)dout
, *rx
= din
;
152 /* we can only do 8 bit transfers */
154 printf("sandbox_spi: xfer: invalid bitlen size %u; needs to be 8bit\n",
156 flags
|= SPI_XFER_END
;
160 if (flags
& SPI_XFER_BEGIN
)
161 spi_cs_activate(slave
);
163 /* make sure rx/tx buffers are full so clients can assume */
165 debug("sandbox_spi: xfer: auto-allocating tx scratch buffer\n");
168 debug("sandbox_spi: Out of memory\n");
173 debug("sandbox_spi: xfer: auto-allocating rx scratch buffer\n");
176 debug("sandbox_spi: Out of memory\n");
181 debug("sandbox_spi: xfer: bytes = %u\n tx:", bytes
);
182 for (i
= 0; i
< bytes
; ++i
)
183 debug(" %u:%02x", i
, tx
[i
]);
186 ret
= sss
->ops
->xfer(sss
->priv
, tx
, rx
, bytes
);
188 debug("sandbox_spi: xfer: got back %i (that's %s)\n rx:",
189 ret
, ret
? "bad" : "good");
190 for (i
= 0; i
< bytes
; ++i
)
191 debug(" %u:%02x", i
, rx
[i
]);
200 if (flags
& SPI_XFER_END
)
201 spi_cs_deactivate(slave
);
207 * Set up a new SPI slave for an fdt node
209 * @param blob Device tree blob
210 * @param node SPI peripheral node to use
211 * @return 0 if ok, -1 on error
213 struct spi_slave
*spi_setup_slave_fdt(const void *blob
, int slave_node
,