1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
3 * Microsemi SoCs spi driver
5 * Copyright (c) 2018 Microsemi Corporation
16 #include <linux/bitops.h>
17 #include <linux/delay.h>
21 u32 deactivate_delay_us;
22 bool cs_active; /* State flag as to whether CS is asserted */
24 u32 svalue; /* Value to start transfer with */
25 u32 clk1; /* Clock value start */
26 u32 clk2; /* Clock value 2nd phase */
29 /* Delay 24 instructions for this particular application */
30 #define hold_time_delay() mscc_vcoreiii_nop_delay(3)
32 static int mscc_bb_spi_cs_activate(struct mscc_bb_priv *priv, int mode, int cs)
34 if (!priv->cs_active) {
35 int cpha = mode & SPI_CPHA;
41 /* Initial clock starts SCK=1 */
42 priv->clk1 = ICPU_SW_MODE_SW_SPI_SCK;
45 /* Initial clock starts SCK=0 */
47 priv->clk2 = ICPU_SW_MODE_SW_SPI_SCK;
50 /* Enable bitbang, SCK_OE, SDO_OE */
51 priv->svalue = (ICPU_SW_MODE_SW_PIN_CTRL_MODE | /* Bitbang */
52 ICPU_SW_MODE_SW_SPI_SCK_OE | /* SCK_OE */
53 ICPU_SW_MODE_SW_SPI_SDO_OE); /* SDO OE */
58 ICPU_SW_MODE_SW_SPI_CS_OE(BIT(cs)) |
59 ICPU_SW_MODE_SW_SPI_CS(BIT(cs));
64 priv->svalue |= cs_value;
66 /* Enable the CS in HW, Initial clock value */
67 writel(priv->svalue | priv->clk2, priv->regs);
69 priv->cs_active = true;
70 debug("Activated CS%d\n", priv->cs_num);
76 static int mscc_bb_spi_cs_deactivate(struct mscc_bb_priv *priv, int deact_delay)
78 if (priv->cs_active) {
79 /* Keep driving the CLK to its current value while
80 * actively deselecting CS.
82 u32 value = readl(priv->regs);
84 value &= ~ICPU_SW_MODE_SW_SPI_CS_M;
85 writel(value, priv->regs);
88 /* Stop driving the clock, but keep CS with nCS == 1 */
89 value &= ~ICPU_SW_MODE_SW_SPI_SCK_OE;
90 writel(value, priv->regs);
92 /* Deselect hold time delay */
97 writel(0, priv->regs);
99 priv->cs_active = false;
100 debug("Deactivated CS%d\n", priv->cs_num);
106 int mscc_bb_spi_claim_bus(struct udevice *dev)
111 int mscc_bb_spi_release_bus(struct udevice *dev)
116 int mscc_bb_spi_xfer(struct udevice *dev, unsigned int bitlen,
117 const void *dout, void *din, unsigned long flags)
119 struct udevice *bus = dev_get_parent(dev);
120 struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
121 struct mscc_bb_priv *priv = dev_get_priv(bus);
123 const u8 *txd = dout;
126 debug("spi_xfer: slave %s:%s cs%d mode %d, dout %p din %p bitlen %u\n",
127 dev->parent->name, dev->name, plat->cs, plat->mode, dout,
130 if (flags & SPI_XFER_BEGIN)
131 mscc_bb_spi_cs_activate(priv, plat->mode, plat->cs);
134 for (i = 0; i < count; i++) {
135 u32 rx = 0, mask = 0x80, value;
138 /* Initial condition: CLK is low. */
139 value = priv->svalue;
140 if (txd && txd[i] & mask)
141 value |= ICPU_SW_MODE_SW_SPI_SDO;
143 /* Drive data while taking CLK low. The device
144 * we're accessing will sample on the
145 * following rising edge and will output data
146 * on this edge for us to be sampled at the
149 writel(value | priv->clk1, priv->regs);
151 /* Wait for t_setup. All devices do have a
152 * setup-time, so we always insert some delay
153 * here. Some devices have a very long
154 * setup-time, which can be adjusted by the
155 * user through vcoreiii_device->delay.
159 /* Drive the clock high. */
160 writel(value | priv->clk2, priv->regs);
162 /* Wait for t_hold. See comment about t_setup
167 /* We sample as close to the next falling edge
170 value = readl(priv->regs);
171 if (value & ICPU_SW_MODE_SW_SPI_SDI)
176 debug("Read 0x%02x\n", rx);
179 debug("spi_xfer: byte %d/%d\n", i + 1, count);
182 debug("spi_xfer: done\n");
184 if (flags & SPI_XFER_END)
185 mscc_bb_spi_cs_deactivate(priv, priv->deactivate_delay_us);
190 int mscc_bb_spi_set_speed(struct udevice *dev, unsigned int speed)
192 /* Accept any speed */
196 int mscc_bb_spi_set_mode(struct udevice *dev, unsigned int mode)
201 static const struct dm_spi_ops mscc_bb_ops = {
202 .claim_bus = mscc_bb_spi_claim_bus,
203 .release_bus = mscc_bb_spi_release_bus,
204 .xfer = mscc_bb_spi_xfer,
205 .set_speed = mscc_bb_spi_set_speed,
206 .set_mode = mscc_bb_spi_set_mode,
209 static const struct udevice_id mscc_bb_ids[] = {
210 { .compatible = "mscc,luton-bb-spi" },
214 static int mscc_bb_spi_probe(struct udevice *bus)
216 struct mscc_bb_priv *priv = dev_get_priv(bus);
218 debug("%s: loaded, priv %p\n", __func__, priv);
220 priv->regs = (void __iomem *)dev_read_addr(bus);
222 priv->deactivate_delay_us =
223 dev_read_u32_default(bus, "spi-deactivate-delay", 0);
225 priv->cs_active = false;
230 U_BOOT_DRIVER(mscc_bb) = {
233 .of_match = mscc_bb_ids,
235 .priv_auto_alloc_size = sizeof(struct mscc_bb_priv),
236 .probe = mscc_bb_spi_probe,