From 4783db2cf18c31ab48b219dabe1e04bf7f311d52 Mon Sep 17 00:00:00 2001 From: Julius Werner Date: Tue, 27 Mar 2018 16:28:52 -0700 Subject: spi: Add helper functions for bit-banging Sometimes when bringing up a new board it can take a while until you have all the peripheral drivers ready. For those cases it is nice to be able to bitbang certain protocols so that you can already get further in the boot flow while those drivers are still being worked on. We already have this support for I2C, but it would be nice to have something for SPI as well, since without SPI you're not going to boot very far. This patch adds a couple of helper functions that platforms can use to implement bit-banging SPI with minimal effort. It also adds a proof of concept implementation using the RK3399. Change-Id: Ie3551f51cc9a9f8bf3a47fd5cea6d9c064da8a62 Signed-off-by: Julius Werner Reviewed-on: https://review.coreboot.org/25394 Tested-by: build bot (Jenkins) Reviewed-by: Aaron Durbin --- src/drivers/spi/bitbang.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src/drivers/spi/bitbang.c (limited to 'src/drivers/spi/bitbang.c') diff --git a/src/drivers/spi/bitbang.c b/src/drivers/spi/bitbang.c new file mode 100644 index 0000000000..d858345859 --- /dev/null +++ b/src/drivers/spi/bitbang.c @@ -0,0 +1,84 @@ +/* + * This file is part of the coreboot project. + * + * Copyright 2018 Google LLC + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include +#include +#include + +/* Set to 1 to dump all SPI transfers to the UART. */ +#define TRACE 0 +/* + * In theory, this should work fine with 0 delay since the bus is fully clocked + * by the master and the slave just needs to follow clock transitions whenever + * they happen. We're not going to be "too fast" by bit-banging anyway. However, + * if something doesn't work right, try increasing this value to slow it down. + */ +#define HALF_CLOCK_US 0 + +int spi_bitbang_claim_bus(const struct spi_bitbang_ops *ops) +{ + ops->set_cs(ops, 0); + return 0; +} + +void spi_bitbang_release_bus(const struct spi_bitbang_ops *ops) +{ + ops->set_cs(ops, 1); +} + +/* Implements a CPOL=0, CPH=0, MSB first 8-bit controller. */ +int spi_bitbang_xfer(const struct spi_bitbang_ops *ops, const void *dout, + size_t bytes_out, void *din, size_t bytes_in) +{ + if (TRACE) { + if (bytes_in && bytes_out) + printk(BIOS_SPEW, "!"); + else if (bytes_in) + printk(BIOS_SPEW, "<"); + else if (bytes_out) + printk(BIOS_SPEW, ">"); + } + + while (bytes_out || bytes_in) { + int i; + uint8_t in_byte = 0, out_byte = 0; + if (bytes_out) { + out_byte = *(const uint8_t *)dout++; + bytes_out--; + if (TRACE) + printk(BIOS_SPEW, "%02x", out_byte); + } + for (i = 7; i >= 0; i--) { + ops->set_mosi(ops, !!(out_byte & (1 << i))); + if (HALF_CLOCK_US) + udelay(HALF_CLOCK_US); + ops->set_clk(ops, 1); + in_byte |= !!ops->get_miso(ops) << i; + if (HALF_CLOCK_US) + udelay(HALF_CLOCK_US); + ops->set_clk(ops, 0); + } + if (bytes_in) { + *(uint8_t *)din++ = in_byte; + bytes_in--; + if (TRACE) + printk(BIOS_SPEW, "%02x", in_byte); + } + } + + if (TRACE) + printk(BIOS_SPEW, "\n"); + return 0; +} -- cgit v1.2.3