SiFive SPI MMC Support
Subsystems (3)
v2 cover SiFive SPI MMC Support
This patchset adds:
1. SiFive SPI driver
2. New MMC SPI driver based on DM_MMC and DM_SPI
3. Enables SiFive SPI driver and MMC SPI driver for SiFive Unleashed board
With this patch series, we can now load files from SD card on SiFive Unleashed board. Many thanks to Bhargav for porting SiFive SPI driver and updating MMC SPI driver for us.
These patches can be also found in riscv_unleashed_mmc_spi_v2 branch of:
https//github.com/avpatel/u-boot.git
Changes since v1:
- Make response matching part belongs to mmc_spi_sendcmd()
- Match response to zero for SEND_STATUS (CMD13)
- Add separate patch for updating SiFive FU540 Documentation
Anup Patel (2):
mmc: skip select_mode_and_width for MMC SPI host
doc: sifive-fu540: Update README for SiFive SPI and MMC SPI drivers
Bhargav Shah (3):
spi: Add SiFive SPI driver
mmc: mmc_spi: Re-write driver using DM framework
riscv: sifive: fu540: Enable SiFive SPI and MMC SPI drivers
board/sifive/fu540/Kconfig | 8 + doc/README.sifive-fu540 | 4 +- drivers/mmc/Kconfig | 18 ++ drivers/mmc/mmc.c | 14 ++ drivers/mmc/mmc_spi.c | 469 ++++++++++++++++++++++++------------- drivers/spi/Kconfig | 8 + drivers/spi/Makefile | 1 + drivers/spi/spi-sifive.c | 405 ++++++++++++++++++++++++++++++++ 8 files changed, 757 insertions(+), 170 deletions(-) create mode 100644 drivers/spi/spi-sifive.c
--
2.17.1
PATCH v2 1/5 spi: Add SiFive SPI driver
From: Bhargav Shah <[email protected]>
This patch adds SiFive SPI driver. The driver is 100% DM driver and it determines input clock using clk framework.
The SiFive SPI block is found on SiFive FU540 SOC and is used to access flash and MMC devices on SiFive Unleashed board.
This driver implementation is inspired from the Linux SiFive SPI driver available in Linux-5.2 or higher and SiFive FSBL sources.
--- drivers/spi/Kconfig | 8 + drivers/spi/Makefile | 1 + drivers/spi/spi-sifive.c | 405 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 414 insertions(+) create mode 100644 drivers/spi/spi-sifive.c
drivers/spi/Kconfig +8 -0
@@ -224,6 +224,14 @@ config SANDBOX_SPI }; }; +config SIFIVE_SPI + bool "SiFive SPI driver" + help + This driver supports the SiFive SPI IP. If unsure say N. + Enable the SiFive SPI controller driver. + + The SiFive SPI controller driver is found on various SiFive SoCs. + config SPI_SUNXI bool "Allwinner SoC SPI controllers" help
drivers/spi/Makefile +1 -0
@@ -49,6 +49,7 @@ obj-$(CONFIG_PL022_SPI) += pl022_spi.o obj-$(CONFIG_RENESAS_RPC_SPI) += renesas_rpc_spi.o obj-$(CONFIG_ROCKCHIP_SPI) += rk_spi.o obj-$(CONFIG_SANDBOX_SPI) += sandbox_spi.o +obj-$(CONFIG_SIFIVE_SPI) += spi-sifive.o obj-$(CONFIG_SPI_SUNXI) += spi-sunxi.o obj-$(CONFIG_SH_SPI) += sh_spi.o obj-$(CONFIG_SH_QSPI) += sh_qspi.o
drivers/spi/spi-sifive.c new file +405 -0
405 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v2 1/5 spi: Add SiFive SPI driver
Quoted 65 lines · Anup Patel
From: Bhargav Shah <[email protected]>
This patch adds SiFive SPI driver. The driver is 100% DM driver and it determines input clock using clk framework.
The SiFive SPI block is found on SiFive FU540 SOC and is used to access flash and MMC devices on SiFive Unleashed board.
This driver implementation is inspired from the Linux SiFive SPI driver available in Linux-5.2 or higher and SiFive FSBL sources.
Signed-off-by Bhargav Shah <[email protected]>Signed-off-by Anup Patel <[email protected]>--- drivers/spi/Kconfig | 8 + drivers/spi/Makefile | 1 + drivers/spi/spi-sifive.c | 405 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 414 insertions(+) create mode 100644 drivers/spi/spi-sifive.cdrivers/spi/Kconfig +8
-0@@ -224,6 +224,14 @@ config SANDBOX_SPI }; }; +config SIFIVE_SPI + bool "SiFive SPI driver" + help + This driver supports the SiFive SPI IP. If unsure say N. + Enable the SiFive SPI controller driver. + + The SiFive SPI controller driver is found on various SiFive SoCs. + config SPI_SUNXI bool "Allwinner SoC SPI controllers" helpdrivers/spi/Makefile +1
-0@@ -49,6 +49,7 @@ obj-$(CONFIG_PL022_SPI) += pl022_spi.o obj-$(CONFIG_RENESAS_RPC_SPI) += renesas_rpc_spi.o obj-$(CONFIG_ROCKCHIP_SPI) += rk_spi.o obj-$(CONFIG_SANDBOX_SPI) += sandbox_spi.o +obj-$(CONFIG_SIFIVE_SPI) += spi-sifive.o obj-$(CONFIG_SPI_SUNXI) += spi-sunxi.o obj-$(CONFIG_SH_SPI) += sh_spi.o obj-$(CONFIG_SH_QSPI) += sh_qspi.odrivers/spi/spi-sifive.c new file +7
-0@@ -0,0 +1,405 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* Copyright 2018 SiFive, Inc. +* Copyright 2019 Bhargav Shah <[email protected]> +* +* SiFive SPI controller driver (master mode only) +*/
drivers/spi/spi-sifive.c line 17
Quoted 10 lines
drivers/spi/spi-sifive.c new file +10
-0+ +#include <common.h> +#include <dm.h> +#include <malloc.h> +#include <spi.h> +#include <asm/io.h> +#include <linux/log2.h> +#include <clk.h> + +DECLARE_GLOBAL_DATA_PTR ;
This is not needed.
Quoted 129 lines
drivers/spi/spi-sifive.c new file +129
-0+ +#define SIFIVE_SPI_MAX_CS 32 + +#define SIFIVE_SPI_DEFAULT_DEPTH 8 +#define SIFIVE_SPI_DEFAULT_BITS 8 + +/* register offsets */ +#define SIFIVE_SPI_REG_SCKDIV 0x00/* Serial clock divisor */ +#define SIFIVE_SPI_REG_SCKMODE 0x04/* Serial clock mode */ +#define SIFIVE_SPI_REG_CSID 0x10/* Chip select ID */ +#define SIFIVE_SPI_REG_CSDEF 0x14/* Chip select default */ +#define SIFIVE_SPI_REG_CSMODE 0x18/* Chip select mode */ +#define SIFIVE_SPI_REG_DELAY0 0x28/* Delay control 0 */ +#define SIFIVE_SPI_REG_DELAY1 0x2c/* Delay control 1 */ +#define SIFIVE_SPI_REG_FMT 0x40/* Frame format */ +#define SIFIVE_SPI_REG_TXDATA 0x48/* Tx FIFO data */ +#define SIFIVE_SPI_REG_RXDATA 0x4c/* Rx FIFO data */ +#define SIFIVE_SPI_REG_TXMARK 0x50/* Tx FIFO watermark */ +#define SIFIVE_SPI_REG_RXMARK 0x54/* Rx FIFO watermark */ +#define SIFIVE_SPI_REG_FCTRL 0x60/* SPI flash interface control */ +#define SIFIVE_SPI_REG_FFMT 0x64/* SPI flash instruction format */ +#define SIFIVE_SPI_REG_IE 0x70/* Interrupt Enable Register */ +#define SIFIVE_SPI_REG_IP 0x74/* Interrupt Pendings Register */ + +/* sckdiv bits */ +#define SIFIVE_SPI_SCKDIV_DIV_MASK 0xfffU + +/* sckmode bits */ +#define SIFIVE_SPI_SCKMODE_PHA BIT(0) +#define SIFIVE_SPI_SCKMODE_POL BIT(1) +#define SIFIVE_SPI_SCKMODE_MODE_MASK (SIFIVE_SPI_SCKMODE_PHA | \ + SIFIVE_SPI_SCKMODE_POL) + +/* csmode bits */ +#define SIFIVE_SPI_CSMODE_MODE_AUTO 0U +#define SIFIVE_SPI_CSMODE_MODE_HOLD 2U +#define SIFIVE_SPI_CSMODE_MODE_OFF 3U + +/* delay0 bits */ +#define SIFIVE_SPI_DELAY0_CSSCK (x) ((u32)(x)) +#define SIFIVE_SPI_DELAY0_CSSCK_MASK 0xffU +#define SIFIVE_SPI_DELAY0_SCKCS (x) ((u32)(x) << 16) +#define SIFIVE_SPI_DELAY0_SCKCS_MASK (0xffU << 16) + +/* delay1 bits */ +#define SIFIVE_SPI_DELAY1_INTERCS (x) ((u32)(x)) +#define SIFIVE_SPI_DELAY1_INTERCS_MASK 0xffU +#define SIFIVE_SPI_DELAY1_INTERXFR (x) ((u32)(x) << 16) +#define SIFIVE_SPI_DELAY1_INTERXFR_MASK (0xffU << 16) + +/* fmt bits */ +#define SIFIVE_SPI_FMT_PROTO_SINGLE 0U +#define SIFIVE_SPI_FMT_PROTO_DUAL 1U +#define SIFIVE_SPI_FMT_PROTO_QUAD 2U +#define SIFIVE_SPI_FMT_PROTO_MASK 3U +#define SIFIVE_SPI_FMT_ENDIAN BIT(2) +#define SIFIVE_SPI_FMT_DIR BIT(3) +#define SIFIVE_SPI_FMT_LEN (x) ((u32)(x) << 16) +#define SIFIVE_SPI_FMT_LEN_MASK (0xfU << 16) + +/* txdata bits */ +#define SIFIVE_SPI_TXDATA_DATA_MASK 0xffU +#define SIFIVE_SPI_TXDATA_FULL BIT(31) + +/* rxdata bits */ +#define SIFIVE_SPI_RXDATA_DATA_MASK 0xffU +#define SIFIVE_SPI_RXDATA_EMPTY BIT(31) + +/* ie and ip bits */ +#define SIFIVE_SPI_IP_TXWM BIT(0) +#define SIFIVE_SPI_IP_RXWM BIT(1) + +struct sifive_spi { +void *regs;/* base address of the registers */ +u32 fifo_depth; +u32 bits_per_word; +u32 cs_inactive;/* Level of the CS pins when inactive*/ +u32 freq; +u32 num_cs; +}; + +static void sifive_spi_write (struct sifive_spi *spi,int offset,u32 value) +{ +writel (value, spi->regs + offset); +} + +static u32 sifive_spi_read (struct sifive_spi *spi,int offset) +{ +return readl (spi->regs + offset); +} + +static void sifive_spi_prep_device (struct sifive_spi *spi, +struct dm_spi_slave_platdata *slave) +{ +/* Update the chip select polarity */ +if (slave->mode &SPI_CS_HIGH ) + spi->cs_inactive &= ~BIT (slave->cs); +else + spi->cs_inactive |=BIT (slave->cs); +sifive_spi_write (spi,SIFIVE_SPI_REG_CSDEF , spi->cs_inactive); + +/* Select the correct device */ +sifive_spi_write (spi,SIFIVE_SPI_REG_CSID , slave->cs); +} + +static int sifive_spi_set_cs (struct sifive_spi *spi, +struct dm_spi_slave_platdata *slave) +{ +u32 cs_mode =SIFIVE_SPI_CSMODE_MODE_HOLD ; + +if (slave->cs > spi->num_cs) +return -EINVAL ; + +if (slave->mode &SPI_CS_HIGH ) + cs_mode =SIFIVE_SPI_CSMODE_MODE_AUTO ; + +sifive_spi_write (spi,SIFIVE_SPI_REG_CSMODE , cs_mode); + +return 0 ; +} + +static void sifive_spi_clear_cs (struct sifive_spi *spi) +{ +sifive_spi_write (spi,SIFIVE_SPI_REG_CSMODE , +SIFIVE_SPI_CSMODE_MODE_AUTO ); +} + +static void sifive_spi_prep_transfer(struct sifive_spi *spi, + bool is_rx_xfer,
drivers/spi/spi-sifive.c line 156
Quoted 10 lines
drivers/spi/spi-sifive.c new file +10
-0+ struct dm_spi_slave_platdata *slave) +{ +u32 cr; + +/* Modify the SPI protocol mode */ + cr =sifive_spi_read (spi,SIFIVE_SPI_REG_FMT ); + +/* Bits per word ? */ + cr &= ~SIFIVE_SPI_FMT_LEN_MASK ; + cr |=SIFIVE_SPI_FMT_LEN (SIFIVE_SPI_DEFAULT_BITS );
This should be: cr |= SIFIVE_SPI_FMT_LEN(spi->bits_per_word);
Quoted 148 lines
drivers/spi/spi-sifive.c new file +148
-0+ +/* LSB first? */ + cr &= ~SIFIVE_SPI_FMT_ENDIAN ; +if (slave->mode &SPI_LSB_FIRST ) + cr |=SIFIVE_SPI_FMT_ENDIAN ; + +/* Number of wires ? */ + cr &= ~SIFIVE_SPI_FMT_PROTO_MASK ; +if ((slave->mode &SPI_TX_QUAD ) || (slave->mode &SPI_RX_QUAD )) + cr |=SIFIVE_SPI_FMT_PROTO_QUAD ; +else if ((slave->mode &SPI_TX_DUAL ) || (slave->mode &SPI_RX_DUAL )) + cr |=SIFIVE_SPI_FMT_PROTO_DUAL ; +else + cr |=SIFIVE_SPI_FMT_PROTO_SINGLE ; + +/* SPI direction in/out ? */ + cr &= ~SIFIVE_SPI_FMT_DIR ; +if (!is_rx_xfer) + cr |=SIFIVE_SPI_FMT_DIR ; + +sifive_spi_write (spi,SIFIVE_SPI_REG_FMT , cr); +} + +static void sifive_spi_rx (struct sifive_spi *spi,u8 *rx_ptr) +{ +u32 data; + +do { + data =sifive_spi_read (spi,SIFIVE_SPI_REG_RXDATA ); + }while (data &SIFIVE_SPI_RXDATA_EMPTY ); + +if (rx_ptr) + *rx_ptr = data &SIFIVE_SPI_RXDATA_DATA_MASK ; +} + +static void sifive_spi_tx (struct sifive_spi *spi,const u8 *tx_ptr) +{ +u32 data; +u8 tx_data = (tx_ptr) ? *tx_ptr &SIFIVE_SPI_TXDATA_DATA_MASK : +SIFIVE_SPI_TXDATA_DATA_MASK ; + +do { + data =sifive_spi_read (spi,SIFIVE_SPI_REG_TXDATA ); + }while (data &SIFIVE_SPI_TXDATA_FULL ); + +sifive_spi_write (spi,SIFIVE_SPI_REG_TXDATA , tx_data); +} + +static u8 sifive_spi_txrx (struct sifive_spi *spi,const u8 *tx_ptr) +{ +u8 rx =0 ; + +sifive_spi_tx (spi, tx_ptr); +sifive_spi_rx (spi, &rx); + +return rx; +} + +static int sifive_spi_claim_bus (struct udevice *dev) +{ +int ret; +struct udevice *bus = dev->parent; +struct sifive_spi *spi =dev_get_priv (bus); +struct dm_spi_slave_platdata *slave =dev_get_parent_platdata (dev); + +sifive_spi_prep_device (spi, slave); + + ret =sifive_spi_set_cs (spi, slave); +if (ret) +return ret; + +return 0 ; +} + +static int sifive_spi_release_bus (struct udevice *dev) +{ +struct sifive_spi *spi =dev_get_priv (dev->parent); + +sifive_spi_clear_cs (spi); + +return 0 ; +} + +static int sifive_spi_xfer (struct udevice *dev,unsigned int bitlen, +const void *dout,void *din,unsigned long flags) +{ +struct udevice *bus = dev->parent; +struct sifive_spi *spi =dev_get_priv (bus); +struct dm_spi_slave_platdata *slave =dev_get_parent_platdata (dev); +const unsigned char *tx_ptr = dout; +unsigned char *rx_ptr = din; +u32 remaining_len; + +sifive_spi_prep_transfer (spi, true, slave); + + remaining_len = bitlen /8 ; + +while (remaining_len) { +int n_words, tx_words, rx_words; + + n_words =min (remaining_len, spi->fifo_depth); + +/* Enqueue n_words for transmission */ +if (tx_ptr) { +for (tx_words =0 ; tx_words < n_words; ++tx_words) { +sifive_spi_txrx (spi, tx_ptr); + tx_ptr++; + } + } + +/* Read out all the data from the RX FIFO */ +if (rx_ptr) { +for (rx_words =0 ; rx_words < n_words; ++rx_words) { + *rx_ptr =sifive_spi_txrx (spi,NULL ); + rx_ptr++; + } + } + + remaining_len -= n_words; + } + +return 0 ; +} + +static int sifive_spi_set_speed (struct udevice *bus,uint speed) +{ +struct sifive_spi *spi =dev_get_priv (bus); +u32 scale; + +if (speed > spi->freq) + speed = spi->freq; + +/* Cofigure max speed */ + scale = (DIV_ROUND_UP (spi->freq >>1 , speed) -1 ) + &SIFIVE_SPI_SCKDIV_DIV_MASK ; +sifive_spi_write (spi,SIFIVE_SPI_REG_SCKDIV , scale); +return 0 ; +} + +static int sifive_spi_set_mode (struct udevice *bus,uint mode) +{ +struct sifive_spi *spi =dev_get_priv (bus); +u32 cr; + +/* Switch clock mode bits */ + cr =sifive_spi_read (spi,SIFIVE_SPI_REG_SCKMODE ) & + ~SIFIVE_SPI_SCKMODE_MODE_MASK ; +if (mode &SPI_CPHA )
drivers/spi/spi-sifive.c line 314
Quoted 10 lines
drivers/spi/spi-sifive.c new file +10
-0+ cr |=SIFIVE_SPI_SCKMODE_PHA ; +if (mode &SPI_CPOL ) + cr |=SIFIVE_SPI_SCKMODE_POL ; + +sifive_spi_write (spi,SIFIVE_SPI_REG_SCKMODE , cr); + +return 0 ; +} + +static int sifive_cs_info(struct udevice *bus, uint cs,
For consistency, this function should be named as: sifive_spi_cs_info()
drivers/spi/spi-sifive.c line 319
Quoted 5 lines
drivers/spi/spi-sifive.c new file +5
-0+ struct spi_cs_info *info) +{ +return 0 ; +} +
Other than above, the driver looks pretty good.
Regards,
Bin
PATCH v2 1/5 spi: Add SiFive SPI driver
Quoted 71 lines · Bin Meng
Quoted 69 lines · Anup Patel
From: Bhargav Shah <[email protected]>
This patch adds SiFive SPI driver. The driver is 100% DM driver and it determines input clock using clk framework.
The SiFive SPI block is found on SiFive FU540 SOC and is used to access flash and MMC devices on SiFive Unleashed board.
This driver implementation is inspired from the Linux SiFive SPI driver available in Linux-5.2 or higher and SiFive FSBL sources.
Signed-off-by Bhargav Shah <[email protected]>Signed-off-by Anup Patel <[email protected]>--- drivers/spi/Kconfig | 8 + drivers/spi/Makefile | 1 + drivers/spi/spi-sifive.c | 405 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 414 insertions(+) create mode 100644 drivers/spi/spi-sifive.cdrivers/spi/Kconfig +8
-0@@ -224,6 +224,14 @@ config SANDBOX_SPI }; }; +config SIFIVE_SPI + bool "SiFive SPI driver" + help + This driver supports the SiFive SPI IP. If unsure say N. + Enable the SiFive SPI controller driver. + + The SiFive SPI controller driver is found on various SiFive SoCs. + config SPI_SUNXI bool "Allwinner SoC SPI controllers" helpdrivers/spi/Makefile +1
-0@@ -49,6 +49,7 @@ obj-$(CONFIG_PL022_SPI) += pl022_spi.o obj-$(CONFIG_RENESAS_RPC_SPI) += renesas_rpc_spi.o obj-$(CONFIG_ROCKCHIP_SPI) += rk_spi.o obj-$(CONFIG_SANDBOX_SPI) += sandbox_spi.o +obj-$(CONFIG_SIFIVE_SPI) += spi-sifive.o obj-$(CONFIG_SPI_SUNXI) += spi-sunxi.o obj-$(CONFIG_SH_SPI) += sh_spi.o obj-$(CONFIG_SH_QSPI) += sh_qspi.odrivers/spi/spi-sifive.c new file +11
-0@@ -0,0 +1,405 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* Copyright 2018 SiFive, Inc. +* Copyright 2019 Bhargav Shah <[email protected]> +* +* SiFive SPI controller driver (master mode only) +*/ + +#include <common.h> +#include <dm.h> +#include <malloc.h>
Quoted 8 lines
drivers/spi/spi-sifive.c line 17
Quoted 6 lines
drivers/spi/spi-sifive.c new file +6
-0+#include <spi.h> +#include <asm/io.h> +#include <linux/log2.h> +#include <clk.h> + +DECLARE_GLOBAL_DATA_PTR ;This is not needed.
Okay, I will drop it.
Quoted 134 lines
Quoted 134 lines
drivers/spi/spi-sifive.c new file +133
-0+ +#define SIFIVE_SPI_MAX_CS 32 + +#define SIFIVE_SPI_DEFAULT_DEPTH 8 +#define SIFIVE_SPI_DEFAULT_BITS 8 + +/* register offsets */ +#define SIFIVE_SPI_REG_SCKDIV 0x00/* Serial clock divisor */ +#define SIFIVE_SPI_REG_SCKMODE 0x04/* Serial clock mode */ +#define SIFIVE_SPI_REG_CSID 0x10/* Chip select ID */ +#define SIFIVE_SPI_REG_CSDEF 0x14/* Chip select default */ +#define SIFIVE_SPI_REG_CSMODE 0x18/* Chip select mode */ +#define SIFIVE_SPI_REG_DELAY0 0x28/* Delay control 0 */ +#define SIFIVE_SPI_REG_DELAY1 0x2c/* Delay control 1 */ +#define SIFIVE_SPI_REG_FMT 0x40/* Frame format */ +#define SIFIVE_SPI_REG_TXDATA 0x48/* Tx FIFO data */ +#define SIFIVE_SPI_REG_RXDATA 0x4c/* Rx FIFO data */ +#define SIFIVE_SPI_REG_TXMARK 0x50/* Tx FIFO watermark */ +#define SIFIVE_SPI_REG_RXMARK 0x54/* Rx FIFO watermark */ +#define SIFIVE_SPI_REG_FCTRL 0x60/* SPI flash interface control */ +#define SIFIVE_SPI_REG_FFMT 0x64/* SPI flash instruction format */ +#define SIFIVE_SPI_REG_IE 0x70/* Interrupt Enable Register */ +#define SIFIVE_SPI_REG_IP 0x74/* Interrupt Pendings Register */ + +/* sckdiv bits */ +#define SIFIVE_SPI_SCKDIV_DIV_MASK 0xfffU + +/* sckmode bits */ +#define SIFIVE_SPI_SCKMODE_PHA BIT(0) +#define SIFIVE_SPI_SCKMODE_POL BIT(1) +#define SIFIVE_SPI_SCKMODE_MODE_MASK (SIFIVE_SPI_SCKMODE_PHA | \ + SIFIVE_SPI_SCKMODE_POL) + +/* csmode bits */ +#define SIFIVE_SPI_CSMODE_MODE_AUTO 0U +#define SIFIVE_SPI_CSMODE_MODE_HOLD 2U +#define SIFIVE_SPI_CSMODE_MODE_OFF 3U + +/* delay0 bits */ +#define SIFIVE_SPI_DELAY0_CSSCK (x) ((u32)(x)) +#define SIFIVE_SPI_DELAY0_CSSCK_MASK 0xffU +#define SIFIVE_SPI_DELAY0_SCKCS (x) ((u32)(x) << 16) +#define SIFIVE_SPI_DELAY0_SCKCS_MASK (0xffU << 16) + +/* delay1 bits */ +#define SIFIVE_SPI_DELAY1_INTERCS (x) ((u32)(x)) +#define SIFIVE_SPI_DELAY1_INTERCS_MASK 0xffU +#define SIFIVE_SPI_DELAY1_INTERXFR (x) ((u32)(x) << 16) +#define SIFIVE_SPI_DELAY1_INTERXFR_MASK (0xffU << 16) + +/* fmt bits */ +#define SIFIVE_SPI_FMT_PROTO_SINGLE 0U +#define SIFIVE_SPI_FMT_PROTO_DUAL 1U +#define SIFIVE_SPI_FMT_PROTO_QUAD 2U +#define SIFIVE_SPI_FMT_PROTO_MASK 3U +#define SIFIVE_SPI_FMT_ENDIAN BIT(2) +#define SIFIVE_SPI_FMT_DIR BIT(3) +#define SIFIVE_SPI_FMT_LEN (x) ((u32)(x) << 16) +#define SIFIVE_SPI_FMT_LEN_MASK (0xfU << 16) + +/* txdata bits */ +#define SIFIVE_SPI_TXDATA_DATA_MASK 0xffU +#define SIFIVE_SPI_TXDATA_FULL BIT(31) + +/* rxdata bits */ +#define SIFIVE_SPI_RXDATA_DATA_MASK 0xffU +#define SIFIVE_SPI_RXDATA_EMPTY BIT(31) + +/* ie and ip bits */ +#define SIFIVE_SPI_IP_TXWM BIT(0) +#define SIFIVE_SPI_IP_RXWM BIT(1) + +struct sifive_spi { +void *regs;/* base address of the registers */ +u32 fifo_depth; +u32 bits_per_word; +u32 cs_inactive;/* Level of the CS pins when inactive*/ +u32 freq; +u32 num_cs; +}; + +static void sifive_spi_write (struct sifive_spi *spi,int offset,u32 value) +{ +writel (value, spi->regs + offset); +} + +static u32 sifive_spi_read (struct sifive_spi *spi,int offset) +{ +return readl (spi->regs + offset); +} + +static void sifive_spi_prep_device (struct sifive_spi *spi, +struct dm_spi_slave_platdata *slave) +{ +/* Update the chip select polarity */ +if (slave->mode &SPI_CS_HIGH ) + spi->cs_inactive &= ~BIT (slave->cs); +else + spi->cs_inactive |=BIT (slave->cs); +sifive_spi_write (spi,SIFIVE_SPI_REG_CSDEF , spi->cs_inactive); + +/* Select the correct device */ +sifive_spi_write (spi,SIFIVE_SPI_REG_CSID , slave->cs); +} + +static int sifive_spi_set_cs (struct sifive_spi *spi, +struct dm_spi_slave_platdata *slave) +{ +u32 cs_mode =SIFIVE_SPI_CSMODE_MODE_HOLD ; + +if (slave->cs > spi->num_cs) +return -EINVAL ; + +if (slave->mode &SPI_CS_HIGH ) + cs_mode =SIFIVE_SPI_CSMODE_MODE_AUTO ; + +sifive_spi_write (spi,SIFIVE_SPI_REG_CSMODE , cs_mode); + +return 0 ; +} + +static void sifive_spi_clear_cs (struct sifive_spi *spi) +{ +sifive_spi_write (spi,SIFIVE_SPI_REG_CSMODE , +SIFIVE_SPI_CSMODE_MODE_AUTO ); +} + +static void sifive_spi_prep_transfer (struct sifive_spi *spi, +bool is_rx_xfer, +struct dm_spi_slave_platdata *slave) +{ +u32 cr; +
Quoted 8 lines
drivers/spi/spi-sifive.c line 156
Quoted 6 lines
drivers/spi/spi-sifive.c new file +6
-0+/* Modify the SPI protocol mode */ + cr =sifive_spi_read (spi,SIFIVE_SPI_REG_FMT ); + +/* Bits per word ? */ + cr &= ~SIFIVE_SPI_FMT_LEN_MASK ; + cr |=SIFIVE_SPI_FMT_LEN (SIFIVE_SPI_DEFAULT_BITS );This should be: cr |=
SIFIVE_SPI_FMT_LEN(spi->bits_per_word);
Ahh, I missed this one. Thanks for catching. I will update.
Quoted 153 lines
Quoted 153 lines
drivers/spi/spi-sifive.c new file +152
-0+ +/* LSB first? */ + cr &= ~SIFIVE_SPI_FMT_ENDIAN ; +if (slave->mode &SPI_LSB_FIRST ) + cr |=SIFIVE_SPI_FMT_ENDIAN ; + +/* Number of wires ? */ + cr &= ~SIFIVE_SPI_FMT_PROTO_MASK ; +if ((slave->mode &SPI_TX_QUAD ) || (slave->mode &SPI_RX_QUAD )) + cr |=SIFIVE_SPI_FMT_PROTO_QUAD ; +else if ((slave->mode &SPI_TX_DUAL ) || (slave->mode &SPI_RX_DUAL )) + cr |=SIFIVE_SPI_FMT_PROTO_DUAL ; +else + cr |=SIFIVE_SPI_FMT_PROTO_SINGLE ; + +/* SPI direction in/out ? */ + cr &= ~SIFIVE_SPI_FMT_DIR ; +if (!is_rx_xfer) + cr |=SIFIVE_SPI_FMT_DIR ; + +sifive_spi_write (spi,SIFIVE_SPI_REG_FMT , cr); +} + +static void sifive_spi_rx (struct sifive_spi *spi,u8 *rx_ptr) +{ +u32 data; + +do { + data =sifive_spi_read (spi,SIFIVE_SPI_REG_RXDATA ); + }while (data &SIFIVE_SPI_RXDATA_EMPTY ); + +if (rx_ptr) + *rx_ptr = data &SIFIVE_SPI_RXDATA_DATA_MASK ; +} + +static void sifive_spi_tx (struct sifive_spi *spi,const u8 *tx_ptr) +{ +u32 data; +u8 tx_data = (tx_ptr) ? *tx_ptr &SIFIVE_SPI_TXDATA_DATA_MASK : +SIFIVE_SPI_TXDATA_DATA_MASK ; + +do { + data =sifive_spi_read (spi,SIFIVE_SPI_REG_TXDATA ); + }while (data &SIFIVE_SPI_TXDATA_FULL ); + +sifive_spi_write (spi,SIFIVE_SPI_REG_TXDATA , tx_data); +} + +static u8 sifive_spi_txrx (struct sifive_spi *spi,const u8 *tx_ptr) +{ +u8 rx =0 ; + +sifive_spi_tx (spi, tx_ptr); +sifive_spi_rx (spi, &rx); + +return rx; +} + +static int sifive_spi_claim_bus (struct udevice *dev) +{ +int ret; +struct udevice *bus = dev->parent; +struct sifive_spi *spi =dev_get_priv (bus); +struct dm_spi_slave_platdata *slave =dev_get_parent_platdata (dev); + +sifive_spi_prep_device (spi, slave); + + ret =sifive_spi_set_cs (spi, slave); +if (ret) +return ret; + +return 0 ; +} + +static int sifive_spi_release_bus (struct udevice *dev) +{ +struct sifive_spi *spi =dev_get_priv (dev->parent); + +sifive_spi_clear_cs (spi); + +return 0 ; +} + +static int sifive_spi_xfer (struct udevice *dev,unsigned int bitlen, +const void *dout,void *din,unsigned long flags) +{ +struct udevice *bus = dev->parent; +struct sifive_spi *spi =dev_get_priv (bus); +struct dm_spi_slave_platdata *slave =dev_get_parent_platdata (dev); +const unsigned char *tx_ptr = dout; +unsigned char *rx_ptr = din; +u32 remaining_len; + +sifive_spi_prep_transfer (spi, true, slave); + + remaining_len = bitlen /8 ; + +while (remaining_len) { +int n_words, tx_words, rx_words; + + n_words =min (remaining_len, spi->fifo_depth); + +/* Enqueue n_words for transmission */ +if (tx_ptr) { +for (tx_words =0 ; tx_words < n_words; ++tx_words) { +sifive_spi_txrx (spi, tx_ptr); + tx_ptr++; + } + } + +/* Read out all the data from the RX FIFO */ +if (rx_ptr) { +for (rx_words =0 ; rx_words < n_words; ++rx_words) { + *rx_ptr =sifive_spi_txrx (spi,NULL ); + rx_ptr++; + } + } + + remaining_len -= n_words; + } + +return 0 ; +} + +static int sifive_spi_set_speed (struct udevice *bus,uint speed) +{ +struct sifive_spi *spi =dev_get_priv (bus); +u32 scale; + +if (speed > spi->freq) + speed = spi->freq; + +/* Cofigure max speed */ + scale = (DIV_ROUND_UP (spi->freq >>1 , speed) -1 ) + &SIFIVE_SPI_SCKDIV_DIV_MASK ; +sifive_spi_write (spi,SIFIVE_SPI_REG_SCKDIV , scale); +return 0 ; +} + +static int sifive_spi_set_mode (struct udevice *bus,uint mode) +{ +struct sifive_spi *spi =dev_get_priv (bus); +u32 cr; + +/* Switch clock mode bits */ + cr =sifive_spi_read (spi,SIFIVE_SPI_REG_SCKMODE ) & + ~SIFIVE_SPI_SCKMODE_MODE_MASK ; +if (mode &SPI_CPHA ) + cr |=SIFIVE_SPI_SCKMODE_PHA ; +if (mode &SPI_CPOL ) + cr |=SIFIVE_SPI_SCKMODE_POL ; +
Quoted 8 lines
drivers/spi/spi-sifive.c line 314
Quoted 6 lines
drivers/spi/spi-sifive.c new file +6
-0+sifive_spi_write (spi,SIFIVE_SPI_REG_SCKMODE , cr); + +return 0 ; +} + +static int sifive_cs_info(struct udevice *bus, uint cs,For consistency, this function should be named as:
sifive_spi_cs_info()
Sure, I will rename it.
Quoted 9 lines
drivers/spi/spi-sifive.c line 319
Quoted 6 lines
drivers/spi/spi-sifive.c new file +5
-0+ struct spi_cs_info *info) +{ +return 0 ; +} +Other than above, the driver looks pretty good.
Quoted 5 lines
Reviewed-by Bin Meng <[email protected]>Tested-by Bin Meng <[email protected]>Regards,
Bin
Thanks,
Anup
PATCH v2 2/5 mmc: skip select_mode_and_width for MMC SPI host
In reply to Anup PatelThe MMC mode and width are fixed for MMC SPI host hence we skip sd_select_mode_and_width() and mmc_select_mode_and_width() for MMC SPI host.
--- drivers/mmc/mmc.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
drivers/mmc/mmc.c +14 -0
@@ -1672,6 +1672,13 @@ static int sd_select_mode_and_width(struct mmc *mmc, uint card_caps)mmc_dump_capabilities ("host" , mmc->host_caps);#endif +if (mmc_host_is_spi (mmc)) { +mmc_set_bus_width (mmc,1 ); +mmc_select_mode (mmc,SD_LEGACY ); +mmc_set_clock (mmc, mmc->tran_speed,MMC_CLK_ENABLE ); +return 0 ; + } +/* Restrict card's capabilities by what the host can do */ caps = card_caps & mmc->host_caps;
@@ -1934,6 +1941,13 @@ static int mmc_select_mode_and_width(struct mmc *mmc, uint card_caps)mmc_dump_capabilities ("host" , mmc->host_caps);#endif +if (mmc_host_is_spi (mmc)) { +mmc_set_bus_width (mmc,1 ); +mmc_select_mode (mmc,MMC_LEGACY ); +mmc_set_clock (mmc, mmc->tran_speed,MMC_CLK_ENABLE ); +return 0 ; + } +/* Restrict card's capabilities by what the host can do */ card_caps &= mmc->host_caps;
Signature · 1 line
2.17.1
PATCH v2 2/5 mmc: skip select_mode_and_width for MMC SPI host
Quoted 10 lines · Anup Patel
The MMC mode and width are fixed for MMC SPI host hence we skip
sd_select_mode_and_width()andmmc_select_mode_and_width()for MMC SPI host.Signed-off-by Anup Patel <[email protected]>--- drivers/mmc/mmc.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
PATCH v2 3/5 mmc: mmc_spi: Re-write driver using DM framework
In reply to Anup PatelFrom: Bhargav Shah <[email protected]>
This patch rewrites MMC SPI driver using U-Boot DM framework and get it's working on SiFive Unleashed board.
--- drivers/mmc/Kconfig | 18 ++ drivers/mmc/mmc_spi.c | 469 +++++++++++++++++++++++++++--------------- 2 files changed, 320 insertions(+), 167 deletions(-)
drivers/mmc/Kconfig +18 -0
@@ -46,6 +46,24 @@ config SPL_DM_MMC if MMC +config MMC_SPI + bool "Support for SPI-based MMC controller" + depends on DM_MMC && DM_SPI + help + This selects SPI-based MMC controllers. + If you have an MMC controller on a SPI bus, say Y here. + + If unsure, say N. + +config MMC_SPI_CRC_ON + bool "Support CRC for SPI-based MMC controller" + depends on MMC_SPI + default y + help + This enables CRC for SPI-based MMC controllers. + + If unsure, say N. + config ARM_PL180_MMCI bool "ARM AMBA Multimedia Card Interface and compatible support" depends on DM_MMC && OF_CONTROL
drivers/mmc/mmc_spi.c +302 -167
582 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v2 3/5 mmc: mmc_spi: Re-write driver using DM framework
Quoted 30 lines · Anup Patel
From: Bhargav Shah <[email protected]>
This patch rewrites MMC SPI driver using U-Boot DM framework and get it's working on SiFive Unleashed board.
Signed-off-by Bhargav Shah <[email protected]>Signed-off-by Anup Patel <[email protected]>--- drivers/mmc/Kconfig | 18 ++ drivers/mmc/mmc_spi.c | 469 +++++++++++++++++++++++++++--------------- 2 files changed, 320 insertions(+), 167 deletions(-)drivers/mmc/Kconfig +8
-0@@ -46,6 +46,24 @@ config SPL_DM_MMC if MMC +config MMC_SPI + bool "Support for SPI-based MMC controller" + depends on DM_MMC && DM_SPI + help + This selects SPI-based MMC controllers. + If you have an MMC controller on a SPI bus, say Y here. + + If unsure, say N.
drivers/mmc/Kconfig config SPL_DM_MMC · line 66
Quoted 10 lines
drivers/mmc/Kconfig +10
-0+ +config MMC_SPI_CRC_ON + bool "Support CRC for SPI-based MMC controller" + depends on MMC_SPI + default y + help + This enables CRC for SPI-based MMC controllers. + + If unsure, say N. +
scripts/config_whitelist.txt should be updated to remove the non-DM version macros: CONFIG_MMC_SPI_xxx
cmd/mmc_spi.c should be completely dropped as it is for non-DM version MMC SPI driver.
mmc_spi_init() declaration in mmc.h should be removed.
With commit: f51d7fc8ce455ef201f86e1fab4d380444b96789 ("board:
Arcturus: DM: Disable drivers without DM support"), UCP1020 board removes most legacy non-DM SPI MMC support, but still has CONFIG_MMC_SPI in UCP1020.h. That should be removed too. +Oleksandr Zhadan and Michael Durrant who maintains UCP1020 board for the awareness.
[snip]
Other than above,
Regards,
Bin
PATCH v2 3/5 mmc: mmc_spi: Re-write driver using DM framework
Quoted 37 lines · Bin Meng
Quoted 35 lines · Anup Patel
From: Bhargav Shah <[email protected]>
This patch rewrites MMC SPI driver using U-Boot DM framework and get it's working on SiFive Unleashed board.
Signed-off-by Bhargav Shah <[email protected]>Signed-off-by Anup Patel <[email protected]>--- drivers/mmc/Kconfig | 18 ++ drivers/mmc/mmc_spi.c | 469 +++++++++++++++++++++++++++--------------- 2 files changed, 320 insertions(+), 167 deletions(-)drivers/mmc/Kconfig +13
-0@@ -46,6 +46,24 @@ config SPL_DM_MMC if MMC +config MMC_SPI + bool "Support for SPI-based MMC controller" + depends on DM_MMC && DM_SPI + help + This selects SPI-based MMC controllers. + If you have an MMC controller on a SPI bus, say Y here. + + If unsure, say N. + +config MMC_SPI_CRC_ON + bool "Support CRC for SPI-based MMC controller" + depends on MMC_SPI + default y
Quoted 8 lines
drivers/mmc/Kconfig config SPL_DM_MMC · line 66
Quoted 5 lines
drivers/mmc/Kconfig +5
-0+ help + This enables CRC for SPI-based MMC controllers. + + If unsure, say N. +
scripts/config_whitelist.txtshould be updated to remove the non-DM version macros:CONFIG_MMC_SPI_xxx
Okay, I will update.
Quoted 5 lines
cmd/mmc_spi.cshould be completely dropped as it is for non-DM version MMC SPI driver.
mmc_spi_init()declaration inmmc.hshould be removed.
Okay, I will drop cmd/mmc_spi.c and also remove mmc_spi_init() from mmc.h as a separate patch.
Quoted 7 lines
With commit:
f51d7fc8ce455ef201f86e1fab4d380444b96789("board:
Arcturus: DM: Disable drivers without DM support"), UCP1020 board removes most legacy non-DM SPI MMC support, but still hasCONFIG_MMC_SPIinUCP1020.h. That should be removed too. +Oleksandr Zhadan and Michael Durrant who maintains UCP1020 board for the awareness.
Okay, I will remove CONFIG_MMC_SPI from UCP1020.h
Quoted 9 lines
[snip]
Other than above,
Reviewed-by Bin Meng <[email protected]>Tested-by Bin Meng <[email protected]>Regards,
Bin
Thanks,
Anup
PATCH v2 4/5 riscv: sifive: fu540: Enable SiFive SPI and MMC SPI drivers
In reply to Anup PatelFrom: Bhargav Shah <[email protected]>
This patch enables SiFive SPI and MMC SPI drivers for the SiFive Unleashed board.
--- board/sifive/fu540/Kconfig | 8 ++++++++ 1 file changed, 8 insertions(+)
board/sifive/fu540/Kconfig +8 -0
@@ -38,6 +38,14 @@ config BOARD_SPECIFIC_OPTIONS # dummy imply PHY_LIB imply PHY_MSCC imply SIFIVE_SERIAL + imply SPI + imply DM_SPI + imply SIFIVE_SPI + imply MMC + imply DM_MMC + imply MMC_SPI + imply MMC_BROKEN_CD + imply CMD_MMC imply SMP endif
Signature · 1 line
2.17.1
PATCH v2 4/5 riscv: sifive: fu540: Enable SiFive SPI and MMC SPI drivers
Quoted 12 lines · Anup Patel
From: Bhargav Shah <[email protected]>
This patch enables SiFive SPI and MMC SPI drivers for the SiFive Unleashed board.
Signed-off-by Bhargav Shah <[email protected]>Signed-off-by Anup Patel <[email protected]>--- board/sifive/fu540/Kconfig | 8 ++++++++ 1 file changed, 8 insertions(+)
PATCH v2 4/5 riscv: sifive: fu540: Enable SiFive SPI and MMC SPI drivers
Quoted 15 lines · Bin Meng
Quoted 12 lines · Anup Patel
From: Bhargav Shah <[email protected]>
This patch enables SiFive SPI and MMC SPI drivers for the SiFive Unleashed board.
Signed-off-by Bhargav Shah <[email protected]>Signed-off-by Anup Patel <[email protected]>--- board/sifive/fu540/Kconfig | 8 ++++++++ 1 file changed, 8 insertions(+)
Quoted 2 lines
Reviewed-by Bin Meng <[email protected]>Tested-by Bin Meng <[email protected]>
Ah, I just noticed that DM_SPI and DM_MMC is already turned on arch/Kconfig for RISC-V. Please remove these 2.
Regards,
Bin
PATCH v2 4/5 riscv: sifive: fu540: Enable SiFive SPI and MMC SPI drivers
Quoted 17 lines · Bin Meng
Quoted 15 lines · Bin Meng
Quoted 12 lines · Anup Patel
From: Bhargav Shah <[email protected]>
This patch enables SiFive SPI and MMC SPI drivers for the SiFive Unleashed board.
Signed-off-by Bhargav Shah <[email protected]>Signed-off-by Anup Patel <[email protected]>--- board/sifive/fu540/Kconfig | 8 ++++++++ 1 file changed, 8 insertions(+)
Quoted 5 lines
Quoted 2 lines
Reviewed-by Bin Meng <[email protected]>Tested-by Bin Meng <[email protected]>Ah, I just noticed that
DM_SPIandDM_MMCis already turned onarch/Kconfigfor RISC-V. Please remove these 2.
Good catch. I will update Kconfig accordingly.
Quoted 3 lines
Regards,
Bin
Thanks,
Anup
PATCH v2 5/5 doc: sifive-fu540: Update README for SiFive SPI and MMC SPI drivers
In reply to Anup PatelThis patch removes SiFive SPI driver and MMC SPI drive from the TODO list in SiFive FU540 README.
--- doc/README.sifive-fu540 | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
doc/README.sifive-fu540 +1 -3
@@ -13,9 +13,7 @@ The support for following drivers are already enabled: 3. Cadence MACB ethernet driver for networking support. TODO: -1. SPI host driver is still missing. -2. SPI MMC driver does not compile and needs a re-write using U-Boot DM. -2. U-Boot expects the serial console device entry to be present under /chosen +1. U-Boot expects the serial console device entry to be present under /chosen DT node. Example: chosen { stdout-path = "/soc/serial at 10010000:115200";
Signature · 1 line
2.17.1
PATCH v2 5/5 doc: sifive-fu540: Update README for SiFive SPI and MMC SPI drivers
Quoted 9 lines · Anup Patel
This patch removes SiFive SPI driver and MMC SPI drive from the TODO list in SiFive FU540 README.
Signed-off-by Anup Patel <[email protected]>--- doc/README.sifive-fu540 | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
v9 cover Update SiFive Unleashed Drivers
In reply to Anup PatelHi Anup,
Tested the entire V9 series with OpenSBI and mainline u-boot based on commit fc6c0e29a28f6 ("Prepare v2019.07-rc4") along with few Ramon Fried's patches present in your riscv_unleashed_clk_sync_v9 branch:
between commit fc6c0e29a28f6 ("Prepare v2019.07-rc4") and commit 1a417ef71ce1 ("net: macb: apply sane DMA configuration").
With tftp able to boot complete linux.
MDIO and mii utils in U-boot correctly display details of Microsemi PHY VSC8541-01 device as below:
=> mdio list
ethernet at 10090000:
0 - Microsemi VSC8541 <--> ethernet at 10090000
=> mii info 0
PHY 0x00: OUI = 0x01C1, Model = 0x37, Rev = 0x01, 100baseT, FDX
PHY address read from OTP:
Address in ROM is 70:b3:d5:92:f0:1f
BR,
Sagar Kadam