blob: 0454410ee9bb5150ac22d273da132c27ca4e98ef [file] [log] [blame]
Lars Povlsenf01b7322019-01-08 10:38:33 +01001// SPDX-License-Identifier: (GPL-2.0 OR MIT)
2/*
3 * Microsemi SoCs spi driver
4 *
5 * Copyright (c) 2018 Microsemi Corporation
6 */
7
8#include <common.h>
9#include <dm.h>
10#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Lars Povlsenf01b7322019-01-08 10:38:33 +010012#include <malloc.h>
13#include <spi.h>
14#include <dm.h>
15#include <asm/gpio.h>
16#include <asm/io.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060017#include <linux/bitops.h>
Lars Povlsenf01b7322019-01-08 10:38:33 +010018#include <linux/delay.h>
19
20struct mscc_bb_priv {
21 void __iomem *regs;
22 u32 deactivate_delay_us;
23 bool cs_active; /* State flag as to whether CS is asserted */
24 int cs_num;
25 u32 svalue; /* Value to start transfer with */
26 u32 clk1; /* Clock value start */
27 u32 clk2; /* Clock value 2nd phase */
28};
29
30/* Delay 24 instructions for this particular application */
31#define hold_time_delay() mscc_vcoreiii_nop_delay(3)
32
33static int mscc_bb_spi_cs_activate(struct mscc_bb_priv *priv, int mode, int cs)
34{
35 if (!priv->cs_active) {
36 int cpha = mode & SPI_CPHA;
37 u32 cs_value;
38
39 priv->cs_num = cs;
40
41 if (cpha) {
42 /* Initial clock starts SCK=1 */
43 priv->clk1 = ICPU_SW_MODE_SW_SPI_SCK;
44 priv->clk2 = 0;
45 } else {
46 /* Initial clock starts SCK=0 */
47 priv->clk1 = 0;
48 priv->clk2 = ICPU_SW_MODE_SW_SPI_SCK;
49 }
50
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 */
55
56 /* Add CS */
57 if (cs >= 0) {
58 cs_value =
59 ICPU_SW_MODE_SW_SPI_CS_OE(BIT(cs)) |
60 ICPU_SW_MODE_SW_SPI_CS(BIT(cs));
61 } else {
62 cs_value = 0;
63 }
64
65 priv->svalue |= cs_value;
66
67 /* Enable the CS in HW, Initial clock value */
68 writel(priv->svalue | priv->clk2, priv->regs);
69
70 priv->cs_active = true;
71 debug("Activated CS%d\n", priv->cs_num);
72 }
73
74 return 0;
75}
76
77static int mscc_bb_spi_cs_deactivate(struct mscc_bb_priv *priv, int deact_delay)
78{
79 if (priv->cs_active) {
80 /* Keep driving the CLK to its current value while
81 * actively deselecting CS.
82 */
83 u32 value = readl(priv->regs);
84
85 value &= ~ICPU_SW_MODE_SW_SPI_CS_M;
86 writel(value, priv->regs);
87 hold_time_delay();
88
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);
92
93 /* Deselect hold time delay */
94 if (deact_delay)
95 udelay(deact_delay);
96
97 /* Drop everything */
98 writel(0, priv->regs);
99
100 priv->cs_active = false;
101 debug("Deactivated CS%d\n", priv->cs_num);
102 }
103
104 return 0;
105}
106
107int mscc_bb_spi_claim_bus(struct udevice *dev)
108{
109 return 0;
110}
111
112int mscc_bb_spi_release_bus(struct udevice *dev)
113{
114 return 0;
115}
116
117int mscc_bb_spi_xfer(struct udevice *dev, unsigned int bitlen,
118 const void *dout, void *din, unsigned long flags)
119{
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);
123 u32 i, count;
124 const u8 *txd = dout;
125 u8 *rxd = din;
126
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,
129 din, bitlen);
130
131 if (flags & SPI_XFER_BEGIN)
132 mscc_bb_spi_cs_activate(priv, plat->mode, plat->cs);
133
134 count = bitlen / 8;
135 for (i = 0; i < count; i++) {
136 u32 rx = 0, mask = 0x80, value;
137
138 while (mask) {
139 /* Initial condition: CLK is low. */
140 value = priv->svalue;
141 if (txd && txd[i] & mask)
142 value |= ICPU_SW_MODE_SW_SPI_SDO;
143
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
148 * end of this loop.
149 */
150 writel(value | priv->clk1, priv->regs);
151
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.
157 */
158 hold_time_delay();
159
160 /* Drive the clock high. */
161 writel(value | priv->clk2, priv->regs);
162
163 /* Wait for t_hold. See comment about t_setup
164 * above.
165 */
166 hold_time_delay();
167
168 /* We sample as close to the next falling edge
169 * as possible.
170 */
171 value = readl(priv->regs);
172 if (value & ICPU_SW_MODE_SW_SPI_SDI)
173 rx |= mask;
174 mask >>= 1;
175 }
176 if (rxd) {
177 debug("Read 0x%02x\n", rx);
178 rxd[i] = (u8)rx;
179 }
180 debug("spi_xfer: byte %d/%d\n", i + 1, count);
181 }
182
183 debug("spi_xfer: done\n");
184
185 if (flags & SPI_XFER_END)
186 mscc_bb_spi_cs_deactivate(priv, priv->deactivate_delay_us);
187
188 return 0;
189}
190
191int mscc_bb_spi_set_speed(struct udevice *dev, unsigned int speed)
192{
193 /* Accept any speed */
194 return 0;
195}
196
197int mscc_bb_spi_set_mode(struct udevice *dev, unsigned int mode)
198{
199 return 0;
200}
201
202static 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,
208};
209
210static const struct udevice_id mscc_bb_ids[] = {
211 { .compatible = "mscc,luton-bb-spi" },
212 { }
213};
214
215static int mscc_bb_spi_probe(struct udevice *bus)
216{
217 struct mscc_bb_priv *priv = dev_get_priv(bus);
218
219 debug("%s: loaded, priv %p\n", __func__, priv);
220
221 priv->regs = (void __iomem *)dev_read_addr(bus);
222
223 priv->deactivate_delay_us =
224 dev_read_u32_default(bus, "spi-deactivate-delay", 0);
225
226 priv->cs_active = false;
227
228 return 0;
229}
230
231U_BOOT_DRIVER(mscc_bb) = {
232 .name = "mscc_bb",
233 .id = UCLASS_SPI,
234 .of_match = mscc_bb_ids,
235 .ops = &mscc_bb_ops,
236 .priv_auto_alloc_size = sizeof(struct mscc_bb_priv),
237 .probe = mscc_bb_spi_probe,
238};