1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
3 * Microsemi SoCs spi driver
5 * Copyright (c) 2018 Microsemi Corporation
17 #include <linux/bitops.h>
18 #include <linux/delay.h>
22 u32 deactivate_delay_us;
23 bool cs_active; /* State flag as to whether CS is asserted */
25 u32 svalue; /* Value to start transfer with */
26 u32 clk1; /* Clock value start */
27 u32 clk2; /* Clock value 2nd phase */
30 /* Delay 24 instructions for this particular application */
31 #define hold_time_delay() mscc_vcoreiii_nop_delay(3)
33 static int mscc_bb_spi_cs_activate(struct mscc_bb_priv *priv, int mode, int cs)
35 if (!priv->cs_active) {
36 int cpha = mode & SPI_CPHA;
42 /* Initial clock starts SCK=1 */
43 priv->clk1 = ICPU_SW_MODE_SW_SPI_SCK;
46 /* Initial clock starts SCK=0 */
48 priv->clk2 = ICPU_SW_MODE_SW_SPI_SCK;
51 /* Enable bitbang, SCK_OE, SDO_OE */
52 priv->svalue = (ICPU_SW_MODE_SW_PIN_CTRL_MODE | /* Bitbang */
53 ICPU_SW_MODE_SW_SPI_SCK_OE | /* SCK_OE */
54 ICPU_SW_MODE_SW_SPI_SDO_OE); /* SDO OE */
59 ICPU_SW_MODE_SW_SPI_CS_OE(BIT(cs)) |
60 ICPU_SW_MODE_SW_SPI_CS(BIT(cs));
65 priv->svalue |= cs_value;
67 /* Enable the CS in HW, Initial clock value */
68 writel(priv->svalue | priv->clk2, priv->regs);
70 priv->cs_active = true;
71 debug("Activated CS%d\n", priv->cs_num);
77 static int mscc_bb_spi_cs_deactivate(struct mscc_bb_priv *priv, int deact_delay)
79 if (priv->cs_active) {
80 /* Keep driving the CLK to its current value while
81 * actively deselecting CS.
83 u32 value = readl(priv->regs);
85 value &= ~ICPU_SW_MODE_SW_SPI_CS_M;
86 writel(value, priv->regs);
89 /* Stop driving the clock, but keep CS with nCS == 1 */
90 value &= ~ICPU_SW_MODE_SW_SPI_SCK_OE;
91 writel(value, priv->regs);
93 /* Deselect hold time delay */
98 writel(0, priv->regs);
100 priv->cs_active = false;
101 debug("Deactivated CS%d\n", priv->cs_num);
107 int mscc_bb_spi_claim_bus(struct udevice *dev)
112 int mscc_bb_spi_release_bus(struct udevice *dev)
117 int mscc_bb_spi_xfer(struct udevice *dev, unsigned int bitlen,
118 const void *dout, void *din, unsigned long flags)
120 struct udevice *bus = dev_get_parent(dev);
121 struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
122 struct mscc_bb_priv *priv = dev_get_priv(bus);
124 const u8 *txd = dout;
127 debug("spi_xfer: slave %s:%s cs%d mode %d, dout %p din %p bitlen %u\n",
128 dev->parent->name, dev->name, plat->cs, plat->mode, dout,
131 if (flags & SPI_XFER_BEGIN)
132 mscc_bb_spi_cs_activate(priv, plat->mode, plat->cs);
135 for (i = 0; i < count; i++) {
136 u32 rx = 0, mask = 0x80, value;
139 /* Initial condition: CLK is low. */
140 value = priv->svalue;
141 if (txd && txd[i] & mask)
142 value |= ICPU_SW_MODE_SW_SPI_SDO;
144 /* Drive data while taking CLK low. The device
145 * we're accessing will sample on the
146 * following rising edge and will output data
147 * on this edge for us to be sampled at the
150 writel(value | priv->clk1, priv->regs);
152 /* Wait for t_setup. All devices do have a
153 * setup-time, so we always insert some delay
154 * here. Some devices have a very long
155 * setup-time, which can be adjusted by the
156 * user through vcoreiii_device->delay.
160 /* Drive the clock high. */
161 writel(value | priv->clk2, priv->regs);
163 /* Wait for t_hold. See comment about t_setup
168 /* We sample as close to the next falling edge
171 value = readl(priv->regs);
172 if (value & ICPU_SW_MODE_SW_SPI_SDI)
177 debug("Read 0x%02x\n", rx);
180 debug("spi_xfer: byte %d/%d\n", i + 1, count);
183 debug("spi_xfer: done\n");
185 if (flags & SPI_XFER_END)
186 mscc_bb_spi_cs_deactivate(priv, priv->deactivate_delay_us);
191 int mscc_bb_spi_set_speed(struct udevice *dev, unsigned int speed)
193 /* Accept any speed */
197 int mscc_bb_spi_set_mode(struct udevice *dev, unsigned int mode)
202 static const struct dm_spi_ops mscc_bb_ops = {
203 .claim_bus = mscc_bb_spi_claim_bus,
204 .release_bus = mscc_bb_spi_release_bus,
205 .xfer = mscc_bb_spi_xfer,
206 .set_speed = mscc_bb_spi_set_speed,
207 .set_mode = mscc_bb_spi_set_mode,
210 static const struct udevice_id mscc_bb_ids[] = {
211 { .compatible = "mscc,luton-bb-spi" },
215 static int mscc_bb_spi_probe(struct udevice *bus)
217 struct mscc_bb_priv *priv = dev_get_priv(bus);
219 debug("%s: loaded, priv %p\n", __func__, priv);
221 priv->regs = (void __iomem *)dev_read_addr(bus);
223 priv->deactivate_delay_us =
224 dev_read_u32_default(bus, "spi-deactivate-delay", 0);
226 priv->cs_active = false;
231 U_BOOT_DRIVER(mscc_bb) = {
234 .of_match = mscc_bb_ids,
236 .priv_auto_alloc_size = sizeof(struct mscc_bb_priv),
237 .probe = mscc_bb_spi_probe,