coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
bitbang.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <console/console.h>
4 #include <delay.h>
5 #include <spi_bitbang.h>
6 
7 /* Set to 1 to dump all SPI transfers to the UART. */
8 #define TRACE 0
9 /*
10  * In theory, this should work fine with 0 delay since the bus is fully clocked
11  * by the master and the slave just needs to follow clock transitions whenever
12  * they happen. We're not going to be "too fast" by bit-banging anyway. However,
13  * if something doesn't work right, try increasing this value to slow it down.
14  */
15 #define HALF_CLOCK_US 0
16 
18 {
19  ops->set_cs(ops, 0);
20  return 0;
21 }
22 
24 {
25  ops->set_cs(ops, 1);
26 }
27 
28 /* Implements a CPOL=0, CPH=0, MSB first 8-bit controller. */
29 int spi_bitbang_xfer(const struct spi_bitbang_ops *ops, const void *dout,
30  size_t bytes_out, void *din, size_t bytes_in)
31 {
32  if (TRACE) {
33  if (bytes_in && bytes_out)
34  printk(BIOS_SPEW, "!");
35  else if (bytes_in)
36  printk(BIOS_SPEW, "<");
37  else if (bytes_out)
38  printk(BIOS_SPEW, ">");
39  }
40 
41  while (bytes_out || bytes_in) {
42  int i;
43  uint8_t in_byte = 0, out_byte = 0;
44  if (bytes_out) {
45  out_byte = *(const uint8_t *)dout++;
46  bytes_out--;
47  if (TRACE)
48  printk(BIOS_SPEW, "%02x", out_byte);
49  }
50  for (i = 7; i >= 0; i--) {
51  ops->set_mosi(ops, !!(out_byte & (1 << i)));
52  if (HALF_CLOCK_US)
54  ops->set_clk(ops, 1);
55  in_byte |= !!ops->get_miso(ops) << i;
56  if (HALF_CLOCK_US)
58  ops->set_clk(ops, 0);
59  }
60  if (bytes_in) {
61  *(uint8_t *)din++ = in_byte;
62  bytes_in--;
63  if (TRACE)
64  printk(BIOS_SPEW, "%02x", in_byte);
65  }
66  }
67 
68  if (TRACE)
69  printk(BIOS_SPEW, "\n");
70  return 0;
71 }
#define HALF_CLOCK_US
Definition: bitbang.c:15
int spi_bitbang_xfer(const struct spi_bitbang_ops *ops, const void *dout, size_t bytes_out, void *din, size_t bytes_in)
Definition: bitbang.c:29
#define TRACE
Definition: bitbang.c:8
int spi_bitbang_claim_bus(const struct spi_bitbang_ops *ops)
Definition: bitbang.c:17
void spi_bitbang_release_bus(const struct spi_bitbang_ops *ops)
Definition: bitbang.c:23
#define printk(level,...)
Definition: stdlib.h:16
static int out_byte(unsigned int bus, u8 byte)
Definition: software_i2c.c:194
static int in_byte(unsigned int bus, int ack)
Definition: software_i2c.c:212
static struct device_operations ops
Definition: ipmi_kcs_ops.c:416
#define BIOS_SPEW
BIOS_SPEW - Excessively verbose output.
Definition: loglevel.h:142
unsigned char uint8_t
Definition: stdint.h:8
void udelay(uint32_t us)
Definition: udelay.c:15