Skip to contentSkip to conversation

SiFive SPI MMC Support

PATCH Superseded Anup Patel 3 participants 17 messages Jul 17, 2019 – Jul 18, 2019

Subsystems (3)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Anup Patel

author
#0 lore raw

v5 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_v5 branch of:
https//github.com/avpatel/u-boot.git

Changes since v4:
- Renamed kconfig option from SIFIVE_SPI to SPI_SIFIVE for consistency
- Added dummy claim_bus() and release_bus() callbacks

Changes since v3:
- Removed PATCH2, PATCH3, and PATCH4 because these are already merged
- Added separate patch to use SPI_XFER_xyz flags in MMC_SPI driver
- Use readl/writel directly instead of sifive_spi_read/sifi_spi_write
- Use SPI_XFER_xyz flags to enable/disable chipselect
- Remove unused callback sifive_spi_cs_info()

Changes since v2:
- Minor fixes in PATCH1 which adds SiFive SPI driver
- Removed CONFIG_MMC_SPI_xyz from scripts/config_whitelist.txt
- Removed cmd/mmc_spi and all its refrences as separate patch
- Removed DM_SPI and DM_MMC from SiFive FU540 Kconfig

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: mmc_spi: Use SPI_XFER_BEGIN and SPI_XFER_END flags
doc: sifive-fu540: Update README for SiFive SPI and MMC SPI drivers

Bhargav Shah (2):
spi: Add SiFive SPI driver
riscv: sifive: fu540: Enable SiFive SPI and MMC SPI drivers

 board/sifive/fu540/Kconfig |   6 +
 doc/README.sifive-fu540    |   4 +-
 drivers/mmc/mmc_spi.c      |   4 +-
 drivers/spi/Kconfig        |   8 +
 drivers/spi/Makefile       |   1 +
 drivers/spi/spi-sifive.c   | 375 +++++++++++++++++++++++++++++++++++++
 6 files changed, 394 insertions(+), 4 deletions(-)
 create mode 100644 drivers/spi/spi-sifive.c

--
2.17.1

author
#1 lore raw

PATCH v5 1/4 mmc: mmc_spi: Use SPI_XFER_BEGIN and SPI_XFER_END flags

Most DM based SPI host controller drivers use SPI_XFER_BEGIN and SPI_XFER_END flags to enable/disable slave chip select.

This patch extends MMC SPI driver to pass SPI_XFER_BEGIN flag when MMC command is send at start and pass SPI_XFER_END flag using a dummy transfer (of bitlen = 0) at the end of MMC command.

---
 drivers/mmc/mmc_spi.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/mmc/mmc_spi.c +3 -1
index f3d687ae80..350812a04b 100644
@@ -84,7 +84,7 @@ static int mmc_spi_sendcmd(struct udevice *dev,
 	cmdo[4] = cmdarg >> 8;
 	cmdo[5] = cmdarg;
 	cmdo[6] = (crc7(0, &cmdo[1], 5) << 1) | 0x01;
-	ret = dm_spi_xfer(dev, sizeof(cmdo) * 8, cmdo, NULL, 0);
+	ret = dm_spi_xfer(dev, sizeof(cmdo) * 8, cmdo, NULL, SPI_XFER_BEGIN);
 	if (ret)
 		return ret;
 
@@ -360,6 +360,8 @@ static int dm_mmc_spi_request(struct udevice *dev, struct mmc_cmd *cmd,
 	}
 
 done:
+	dm_spi_xfer(dev, 0, NULL, NULL, SPI_XFER_END);
+
 	dm_spi_release_bus(dev);
 
 	return ret;
Signature · 1 line
2.17.1
#2 lore raw

PATCH v5 1/4 mmc: mmc_spi: Use SPI_XFER_BEGIN and SPI_XFER_END flags

Quoted 10 lines · Anup Patel

Most DM based SPI host controller drivers use SPI_XFER_BEGIN and SPI_XFER_END flags to enable/disable slave chip select.

This patch extends MMC SPI driver to pass SPI_XFER_BEGIN flag when MMC command is send at start and pass SPI_XFER_END flag using a dummy transfer (of bitlen = 0) at the end of MMC command.

#3 lore raw

PATCH v5 1/4 mmc: mmc_spi: Use SPI_XFER_BEGIN and SPI_XFER_END flags

Hi Peng,

Quoted 6 lines · Jagan Teki
Quoted 4 lines · Anup Patel

Most DM based SPI host controller drivers use SPI_XFER_BEGIN and SPI_XFER_END flags to enable/disable slave chip select.

Quoted 8 lines
Quoted 6 lines

This patch extends MMC SPI driver to pass SPI_XFER_BEGIN flag when MMC command is send at start and pass SPI_XFER_END flag using a dummy transfer (of bitlen = 0) at the end of MMC command.

Would like to pick this via spi tree, let me know if it fine for you?

#4 lore raw

PATCH v5 1/4 mmc: mmc_spi: Use SPI_XFER_BEGIN and SPI_XFER_END flags

Quoted 14 lines · Jagan Teki

Hi Peng,

Quoted 10 lines · Jagan Teki
Quoted 8 lines · Anup Patel

Most DM based SPI host controller drivers use SPI_XFER_BEGIN and SPI_XFER_END flags to enable/disable slave chip select.

This patch extends MMC SPI driver to pass SPI_XFER_BEGIN flag when MMC command is send at start and pass SPI_XFER_END flag using a dummy transfer (of bitlen = 0) at the end of MMC command.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Would like to pick this via spi tree, let me know if it fine for you?

Look like it has dependency on your tree, please take this from mmc.

#5 lore raw

PATCH v5 1/4 mmc: mmc_spi: Use SPI_XFER_BEGIN and SPI_XFER_END flags

Quoted 16 lines · Jagan Teki
Quoted 14 lines · Jagan Teki

Hi Peng,

Quoted 10 lines · Jagan Teki
Quoted 8 lines · Anup Patel

Most DM based SPI host controller drivers use SPI_XFER_BEGIN and SPI_XFER_END flags to enable/disable slave chip select.

This patch extends MMC SPI driver to pass SPI_XFER_BEGIN flag when MMC command is send at start and pass SPI_XFER_END flag using a dummy transfer (of bitlen = 0) at the end of MMC command.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Would like to pick this via spi tree, let me know if it fine for you?

Look like it has dependency on your tree, please take this from mmc.

Sorry for the noise, the dependent patch is already in master. So

Applied to u-boot-spi/master

#6 lore raw

PATCH v5 1/4 mmc: mmc_spi: Use SPI_XFER_BEGIN and SPI_XFER_END flags

Quoted 23 lines · Jagan Teki
Quoted 21 lines · Jagan Teki
Quoted 19 lines · Jagan Teki

Hi Peng,

Quoted 14 lines · Jagan Teki
Quoted 10 lines · Anup Patel

Most DM based SPI host controller drivers use SPI_XFER_BEGIN and SPI_XFER_END flags to enable/disable slave chip select.

This patch extends MMC SPI driver to pass SPI_XFER_BEGIN flag when MMC command is send at start and pass SPI_XFER_END flag using a dummy transfer (of bitlen = 0) at the end of MMC command.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Would like to pick this via spi tree, let me know if it fine for you?

Look like it has dependency on your tree, please take this from mmc.

Sorry for the noise, the dependent patch is already in master. So

Applied to u-boot-spi/master

Cool, thanks.

Regards,
Anup

author
#7 lore raw

PATCH v5 2/4 spi: Add SiFive SPI driver

In reply to 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.

---
 drivers/spi/Kconfig      |   8 +
 drivers/spi/Makefile     |   1 +
 drivers/spi/spi-sifive.c | 375 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 384 insertions(+)
 create mode 100644 drivers/spi/spi-sifive.c
drivers/spi/Kconfig +8 -0
index eb32f082fe..030f3377d4 100644
@@ -224,6 +224,14 @@ config SANDBOX_SPI
 		};
 	  };
 
+config SPI_SIFIVE
+	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
index 8be9a4baa2..a53b9f4217 100644
@@ -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_SPI_SIFIVE) += 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 +375 -0

375 lines not on this page · raw message

Signature · 1 line
2.17.1
#8 lore raw

PATCH v5 2/4 spi: Add SiFive SPI driver

Quoted 167 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.

---
 drivers/spi/Kconfig      |   8 +
 drivers/spi/Makefile     |   1 +
 drivers/spi/spi-sifive.c | 375 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 384 insertions(+)
 create mode 100644 drivers/spi/spi-sifive.c
drivers/spi/Kconfig +8 -0
index eb32f082fe..030f3377d4 100644
@@ -224,6 +224,14 @@ config SANDBOX_SPI
                };
          };
 
+config SPI_SIFIVE
+       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
index 8be9a4baa2..a53b9f4217 100644
@@ -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_SPI_SIFIVE) += 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 +107 -0
new file mode 100644
index 0000000000..08f30f3f21
@@ -0,0 +1,375 @@
+// 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>
+#include <spi.h>
+#include <asm/io.h>
+#include <linux/log2.h>
+#include <clk.h>
+
+#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_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);
+       writel(spi->cs_inactive, spi->regs + SIFIVE_SPI_REG_CSDEF);
+
+       /* Select the correct device */

drivers/spi/spi-sifive.c line 117

Quoted 10 lines
drivers/spi/spi-sifive.c new file +10 -0
+       writel(slave->cs, spi->regs + SIFIVE_SPI_REG_CSID);
+}
+
+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;

Move this to cs_info.

Quoted 75 lines
drivers/spi/spi-sifive.c new file +75 -0
+
+       if (slave->mode & SPI_CS_HIGH)
+               cs_mode = SIFIVE_SPI_CSMODE_MODE_AUTO;
+
+       writel(cs_mode, spi->regs + SIFIVE_SPI_REG_CSMODE);
+
+       return 0;
+}
+
+static void sifive_spi_clear_cs(struct sifive_spi *spi)
+{
+       writel(SIFIVE_SPI_CSMODE_MODE_AUTO, spi->regs + SIFIVE_SPI_REG_CSMODE);
+}
+
+static void sifive_spi_prep_transfer(struct sifive_spi *spi,
+                                    bool is_rx_xfer,
+                                    struct dm_spi_slave_platdata *slave)
+{
+       u32 cr;
+
+       /* Modify the SPI protocol mode */
+       cr = readl(spi->regs + SIFIVE_SPI_REG_FMT);
+
+       /* Bits per word ? */
+       cr &= ~SIFIVE_SPI_FMT_LEN_MASK;
+       cr |= SIFIVE_SPI_FMT_LEN(spi->bits_per_word);
+
+       /* 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;
+
+       writel(cr, spi->regs + SIFIVE_SPI_REG_FMT);
+}
+
+static void sifive_spi_rx(struct sifive_spi *spi, u8 *rx_ptr)
+{
+       u32 data;
+
+       do {
+               data = readl(spi->regs + 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 = readl(spi->regs + SIFIVE_SPI_REG_TXDATA);
+       } while (data & SIFIVE_SPI_TXDATA_FULL);
+
+       writel(tx_data, spi->regs + SIFIVE_SPI_REG_TXDATA);
+}
+
+static int sifive_spi_claim_bus(struct udevice *dev)

drivers/spi/spi-sifive.c line 202

Quoted 10 lines
drivers/spi/spi-sifive.c new file +10 -0
+{
+       /* Nothing to do here. */
+       return 0;
+}
+
+static int sifive_spi_release_bus(struct udevice *dev)
+{
+       /* Nothing to do here. */
+       return 0;
+}

dummy won't need as spi-uclass.c return 0 if none hooked.

#9 lore raw

PATCH v5 2/4 spi: Add SiFive SPI driver

Quoted 173 lines · Jagan Teki
Quoted 171 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.

---
 drivers/spi/Kconfig      |   8 +
 drivers/spi/Makefile     |   1 +
 drivers/spi/spi-sifive.c | 375 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 384 insertions(+)
 create mode 100644 drivers/spi/spi-sifive.c
drivers/spi/Kconfig +8 -0
index eb32f082fe..030f3377d4 100644
@@ -224,6 +224,14 @@ config SANDBOX_SPI
                };
          };
 
+config SPI_SIFIVE
+       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
index 8be9a4baa2..a53b9f4217 100644
@@ -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_SPI_SIFIVE) += 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 +111 -0
new file mode 100644
index 0000000000..08f30f3f21
@@ -0,0 +1,375 @@
+// 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>
+#include <spi.h>
+#include <asm/io.h>
+#include <linux/log2.h>
+#include <clk.h>
+
+#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_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);
+       writel(spi->cs_inactive, spi->regs + SIFIVE_SPI_REG_CSDEF);
+
+       /* Select the correct device */
+       writel(slave->cs, spi->regs + SIFIVE_SPI_REG_CSID);
+}
+
+static int sifive_spi_set_cs(struct sifive_spi *spi,
Quoted 8 lines

drivers/spi/spi-sifive.c line 117

Quoted 6 lines
drivers/spi/spi-sifive.c new file +6 -0
+                            struct dm_spi_slave_platdata *slave)
+{
+       u32 cs_mode = SIFIVE_SPI_CSMODE_MODE_HOLD;
+
+       if (slave->cs > spi->num_cs)
+               return -EINVAL;

Move this to cs_info.

Sure, will do.

Quoted 80 lines
Quoted 80 lines
drivers/spi/spi-sifive.c new file +79 -0
+
+       if (slave->mode & SPI_CS_HIGH)
+               cs_mode = SIFIVE_SPI_CSMODE_MODE_AUTO;
+
+       writel(cs_mode, spi->regs + SIFIVE_SPI_REG_CSMODE);
+
+       return 0;
+}
+
+static void sifive_spi_clear_cs(struct sifive_spi *spi)
+{
+       writel(SIFIVE_SPI_CSMODE_MODE_AUTO, spi->regs + SIFIVE_SPI_REG_CSMODE);
+}
+
+static void sifive_spi_prep_transfer(struct sifive_spi *spi,
+                                    bool is_rx_xfer,
+                                    struct dm_spi_slave_platdata *slave)
+{
+       u32 cr;
+
+       /* Modify the SPI protocol mode */
+       cr = readl(spi->regs + SIFIVE_SPI_REG_FMT);
+
+       /* Bits per word ? */
+       cr &= ~SIFIVE_SPI_FMT_LEN_MASK;
+       cr |= SIFIVE_SPI_FMT_LEN(spi->bits_per_word);
+
+       /* 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;
+
+       writel(cr, spi->regs + SIFIVE_SPI_REG_FMT);
+}
+
+static void sifive_spi_rx(struct sifive_spi *spi, u8 *rx_ptr)
+{
+       u32 data;
+
+       do {
+               data = readl(spi->regs + 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 = readl(spi->regs + SIFIVE_SPI_REG_TXDATA);
+       } while (data & SIFIVE_SPI_TXDATA_FULL);
+
+       writel(tx_data, spi->regs + SIFIVE_SPI_REG_TXDATA);
+}
+
+static int sifive_spi_claim_bus(struct udevice *dev)
+{
+       /* Nothing to do here. */
+       return 0;
+}
Quoted 8 lines

drivers/spi/spi-sifive.c line 202

Quoted 6 lines
drivers/spi/spi-sifive.c new file +6 -0
+
+static int sifive_spi_release_bus(struct udevice *dev)
+{
+       /* Nothing to do here. */
+       return 0;
+}

dummy won't need as spi-uclass.c return 0 if none hooked.

Okay, I will update.

Regards,
Anup

author
#10 lore raw

PATCH v5 3/4 riscv: sifive: fu540: Enable SiFive SPI and MMC SPI drivers

In reply to Anup Patel

From: Bhargav Shah <[email protected]>

This patch enables SiFive SPI and MMC SPI drivers for the SiFive Unleashed board.

---
 board/sifive/fu540/Kconfig | 6 ++++++
 1 file changed, 6 insertions(+)
board/sifive/fu540/Kconfig +6 -0
index f46437901d..5d65080429 100644
@@ -38,6 +38,12 @@ config BOARD_SPECIFIC_OPTIONS # dummy
 	imply PHY_LIB
 	imply PHY_MSCC
 	imply SIFIVE_SERIAL
+	imply SPI
+	imply SPI_SIFIVE
+	imply MMC
+	imply MMC_SPI
+	imply MMC_BROKEN_CD
+	imply CMD_MMC
 	imply SMP
 
 endif
Signature · 1 line
2.17.1
author
#12 lore raw

PATCH v5 4/4 doc: sifive-fu540: Update README for SiFive SPI and MMC SPI drivers

In reply to Anup Patel

This 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
index 33e03dc861..944ba1c8a0 100644
@@ -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
#13 lore raw

v5 cover SiFive SPI MMC Support

In reply to Anup Patel
Quoted 43 lines · Anup Patel

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_v5 branch of:
https//github.com/avpatel/u-boot.git

Changes since v4:
- Renamed kconfig option from SIFIVE_SPI to SPI_SIFIVE for consistency
- Added dummy claim_bus() and release_bus() callbacks

Changes since v3:
- Removed PATCH2, PATCH3, and PATCH4 because these are already merged
- Added separate patch to use SPI_XFER_xyz flags in MMC_SPI driver
- Use readl/writel directly instead of sifive_spi_read/sifi_spi_write
- Use SPI_XFER_xyz flags to enable/disable chipselect
- Remove unused callback sifive_spi_cs_info()

Changes since v2:
- Minor fixes in PATCH1 which adds SiFive SPI driver
- Removed CONFIG_MMC_SPI_xyz from scripts/config_whitelist.txt
- Removed cmd/mmc_spi and all its refrences as separate patch
- Removed DM_SPI and DM_MMC from SiFive FU540 Kconfig

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: mmc_spi: Use SPI_XFER_BEGIN and SPI_XFER_END flags
doc: sifive-fu540: Update README for SiFive SPI and MMC SPI drivers

Bhargav Shah (2):
spi: Add SiFive SPI driver
riscv: sifive: fu540: Enable SiFive SPI and MMC SPI drivers

Quoted 8 lines
 board/sifive/fu540/Kconfig |   6 +
 doc/README.sifive-fu540    |   4 +-
 drivers/mmc/mmc_spi.c      |   4 +-
 drivers/spi/Kconfig        |   8 +
 drivers/spi/Makefile       |   1 +
 drivers/spi/spi-sifive.c   | 375 +++++++++++++++++++++++++++++++++++++
 6 files changed, 394 insertions(+), 4 deletions(-)
 create mode 100644 drivers/spi/spi-sifive.c

I would like to pick entire series on spi tree, but it has some boards changes so I would like to ask the board maintainers to give a permissions to do so, if not possible I can take only spi changes.

Jagan.

#14 lore raw

v5 cover SiFive SPI MMC Support

Quoted 54 lines · Jagan Teki
Quoted 43 lines · Anup Patel

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_v5 branch of:
https//github.com/avpatel/u-boot.git

Changes since v4:
- Renamed kconfig option from SIFIVE_SPI to SPI_SIFIVE for consistency
- Added dummy claim_bus() and release_bus() callbacks

Changes since v3:
- Removed PATCH2, PATCH3, and PATCH4 because these are already merged
- Added separate patch to use SPI_XFER_xyz flags in MMC_SPI driver
- Use readl/writel directly instead of sifive_spi_read/sifi_spi_write
- Use SPI_XFER_xyz flags to enable/disable chipselect
- Remove unused callback sifive_spi_cs_info()

Changes since v2:
- Minor fixes in PATCH1 which adds SiFive SPI driver
- Removed CONFIG_MMC_SPI_xyz from scripts/config_whitelist.txt
- Removed cmd/mmc_spi and all its refrences as separate patch
- Removed DM_SPI and DM_MMC from SiFive FU540 Kconfig

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: mmc_spi: Use SPI_XFER_BEGIN and SPI_XFER_END flags
doc: sifive-fu540: Update README for SiFive SPI and MMC SPI drivers

Bhargav Shah (2):
spi: Add SiFive SPI driver
riscv: sifive: fu540: Enable SiFive SPI and MMC SPI drivers

Quoted 8 lines
 board/sifive/fu540/Kconfig |   6 +
 doc/README.sifive-fu540    |   4 +-
 drivers/mmc/mmc_spi.c      |   4 +-
 drivers/spi/Kconfig        |   8 +
 drivers/spi/Makefile       |   1 +
 drivers/spi/spi-sifive.c   | 375 +++++++++++++++++++++++++++++++++++++
 6 files changed, 394 insertions(+), 4 deletions(-)
 create mode 100644 drivers/spi/spi-sifive.c
Quoted 3 lines

I would like to pick entire series on spi tree, but it has some boards changes so I would like to ask the board maintainers to give a permissions to do so, if not possible I can take only spi changes.

Last 2 patches depend on below mentioned series so won't apply for you. I guess you can pickup SPI driver and MMC_SPI change.

Refer, https://www.mail-archive.com/u-boot at lists.denx.de/msg331405.html

Regards,
Anup

#15 lore raw

v5 cover SiFive SPI MMC Support

Quoted 56 lines · Anup Patel
Quoted 54 lines · Jagan Teki
Quoted 51 lines · Anup Patel

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_v5 branch of:
https//github.com/avpatel/u-boot.git

Changes since v4:
- Renamed kconfig option from SIFIVE_SPI to SPI_SIFIVE for consistency
- Added dummy claim_bus() and release_bus() callbacks

Changes since v3:
- Removed PATCH2, PATCH3, and PATCH4 because these are already merged
- Added separate patch to use SPI_XFER_xyz flags in MMC_SPI driver
- Use readl/writel directly instead of sifive_spi_read/sifi_spi_write
- Use SPI_XFER_xyz flags to enable/disable chipselect
- Remove unused callback sifive_spi_cs_info()

Changes since v2:
- Minor fixes in PATCH1 which adds SiFive SPI driver
- Removed CONFIG_MMC_SPI_xyz from scripts/config_whitelist.txt
- Removed cmd/mmc_spi and all its refrences as separate patch
- Removed DM_SPI and DM_MMC from SiFive FU540 Kconfig

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: mmc_spi: Use SPI_XFER_BEGIN and SPI_XFER_END flags
doc: sifive-fu540: Update README for SiFive SPI and MMC SPI drivers

Bhargav Shah (2):
spi: Add SiFive SPI driver
riscv: sifive: fu540: Enable SiFive SPI and MMC SPI drivers

 board/sifive/fu540/Kconfig |   6 +
 doc/README.sifive-fu540    |   4 +-
 drivers/mmc/mmc_spi.c      |   4 +-
 drivers/spi/Kconfig        |   8 +
 drivers/spi/Makefile       |   1 +
 drivers/spi/spi-sifive.c   | 375 +++++++++++++++++++++++++++++++++++++
 6 files changed, 394 insertions(+), 4 deletions(-)
 create mode 100644 drivers/spi/spi-sifive.c
Quoted 6 lines
Quoted 3 lines

I would like to pick entire series on spi tree, but it has some boards changes so I would like to ask the board maintainers to give a permissions to do so, if not possible I can take only spi changes.

Last 2 patches depend on below mentioned series so won't apply for you. I guess you can pickup SPI driver and MMC_SPI change.

Haa., only SPI driver rest has dependencies on respective trees so I picked spi driver only.

#16 lore raw

v5 cover SiFive SPI MMC Support

Quoted 62 lines · Jagan Teki
Quoted 60 lines · Anup Patel
Quoted 57 lines · Jagan Teki
Quoted 43 lines · Anup Patel

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_v5 branch of:
https//github.com/avpatel/u-boot.git

Changes since v4:
- Renamed kconfig option from SIFIVE_SPI to SPI_SIFIVE for consistency
- Added dummy claim_bus() and release_bus() callbacks

Changes since v3:
- Removed PATCH2, PATCH3, and PATCH4 because these are already merged
- Added separate patch to use SPI_XFER_xyz flags in MMC_SPI driver
- Use readl/writel directly instead of sifive_spi_read/sifi_spi_write
- Use SPI_XFER_xyz flags to enable/disable chipselect
- Remove unused callback sifive_spi_cs_info()

Changes since v2:
- Minor fixes in PATCH1 which adds SiFive SPI driver
- Removed CONFIG_MMC_SPI_xyz from scripts/config_whitelist.txt
- Removed cmd/mmc_spi and all its refrences as separate patch
- Removed DM_SPI and DM_MMC from SiFive FU540 Kconfig

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: mmc_spi: Use SPI_XFER_BEGIN and SPI_XFER_END flags
doc: sifive-fu540: Update README for SiFive SPI and MMC SPI drivers

Bhargav Shah (2):
spi: Add SiFive SPI driver
riscv: sifive: fu540: Enable SiFive SPI and MMC SPI drivers

Quoted 8 lines
 board/sifive/fu540/Kconfig |   6 +
 doc/README.sifive-fu540    |   4 +-
 drivers/mmc/mmc_spi.c      |   4 +-
 drivers/spi/Kconfig        |   8 +
 drivers/spi/Makefile       |   1 +
 drivers/spi/spi-sifive.c   | 375 +++++++++++++++++++++++++++++++++++++
 6 files changed, 394 insertions(+), 4 deletions(-)
 create mode 100644 drivers/spi/spi-sifive.c

I would like to pick entire series on spi tree, but it has some boards changes so I would like to ask the board maintainers to give a permissions to do so, if not possible I can take only spi changes.

Quoted 5 lines
Quoted 2 lines

Last 2 patches depend on below mentioned series so won't apply for you. I guess you can pickup SPI driver and MMC_SPI change.

Haa., only SPI driver rest has dependencies on respective trees so I picked spi driver only.

Thanks Jagan.

Regards,
Anup