1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <console/console.h>
5 #include <spi_bitbang.h>
8 /* Set to 1 to dump all SPI transfers to the UART. */
11 * In theory, this should work fine with 0 delay since the bus is fully clocked
12 * by the master and the slave just needs to follow clock transitions whenever
13 * they happen. We're not going to be "too fast" by bit-banging anyway. However,
14 * if something doesn't work right, try increasing this value to slow it down.
16 #define HALF_CLOCK_US 0
18 int spi_bitbang_claim_bus(const struct spi_bitbang_ops
*ops
)
24 void spi_bitbang_release_bus(const struct spi_bitbang_ops
*ops
)
29 /* Implements a CPOL=0, CPH=0, MSB first 8-bit controller. */
30 int spi_bitbang_xfer(const struct spi_bitbang_ops
*ops
, const void *dout
,
31 size_t bytes_out
, void *din
, size_t bytes_in
)
34 if (bytes_in
&& bytes_out
)
35 printk(BIOS_SPEW
, "!");
37 printk(BIOS_SPEW
, "<");
39 printk(BIOS_SPEW
, ">");
42 while (bytes_out
|| bytes_in
) {
44 uint8_t in_byte
= 0, out_byte
= 0;
46 out_byte
= *(const uint8_t *)dout
++;
49 printk(BIOS_SPEW
, "%02x", out_byte
);
51 for (i
= 7; i
>= 0; i
--) {
52 ops
->set_mosi(ops
, !!(out_byte
& (1 << i
)));
54 udelay(HALF_CLOCK_US
);
56 in_byte
|= !!ops
->get_miso(ops
) << i
;
58 udelay(HALF_CLOCK_US
);
62 *(uint8_t *)din
++ = in_byte
;
65 printk(BIOS_SPEW
, "%02x", in_byte
);
70 printk(BIOS_SPEW
, "\n");