blob: 153fbb2889a1e2261b9be794fd62ab7fb7acc4f3 [file] [log] [blame]
Kunihiko Hayashi9424ecd2019-07-05 10:03:18 +09001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * uniphier_spi.c - Socionext UniPhier SPI driver
4 * Copyright 2019 Socionext, Inc.
5 */
6
7#include <clk.h>
8#include <common.h>
9#include <dm.h>
Simon Glass10453152019-11-14 12:57:30 -070010#include <time.h>
Simon Glass336d4612020-02-03 07:36:16 -070011#include <dm/device_compat.h>
Kunihiko Hayashi9424ecd2019-07-05 10:03:18 +090012#include <linux/bitfield.h>
13#include <linux/io.h>
14#include <spi.h>
15#include <wait_bit.h>
16
17DECLARE_GLOBAL_DATA_PTR;
18
19#define SSI_CTL 0x00
20#define SSI_CTL_EN BIT(0)
21
22#define SSI_CKS 0x04
23#define SSI_CKS_CKRAT_MASK GENMASK(7, 0)
24#define SSI_CKS_CKPHS BIT(14)
25#define SSI_CKS_CKINIT BIT(13)
26#define SSI_CKS_CKDLY BIT(12)
27
28#define SSI_TXWDS 0x08
29#define SSI_TXWDS_WDLEN_MASK GENMASK(13, 8)
30#define SSI_TXWDS_TDTF_MASK GENMASK(7, 6)
31#define SSI_TXWDS_DTLEN_MASK GENMASK(5, 0)
32
33#define SSI_RXWDS 0x0c
34#define SSI_RXWDS_RDTF_MASK GENMASK(7, 6)
35#define SSI_RXWDS_DTLEN_MASK GENMASK(5, 0)
36
37#define SSI_FPS 0x10
38#define SSI_FPS_FSPOL BIT(15)
39#define SSI_FPS_FSTRT BIT(14)
40
41#define SSI_SR 0x14
42#define SSI_SR_BUSY BIT(7)
43#define SSI_SR_TNF BIT(5)
44#define SSI_SR_RNE BIT(0)
45
46#define SSI_IE 0x18
47
48#define SSI_IC 0x1c
49#define SSI_IC_TCIC BIT(4)
50#define SSI_IC_RCIC BIT(3)
51#define SSI_IC_RORIC BIT(0)
52
53#define SSI_FC 0x20
54#define SSI_FC_TXFFL BIT(12)
55#define SSI_FC_TXFTH_MASK GENMASK(11, 8)
56#define SSI_FC_RXFFL BIT(4)
57#define SSI_FC_RXFTH_MASK GENMASK(3, 0)
58
59#define SSI_XDR 0x24 /* TXDR for write, RXDR for read */
60
61#define SSI_FIFO_DEPTH 8U
62
63#define SSI_REG_TIMEOUT (CONFIG_SYS_HZ / 100) /* 10 ms */
64#define SSI_XFER_TIMEOUT (CONFIG_SYS_HZ) /* 1 sec */
65
66#define SSI_CLK 50000000 /* internal I/O clock: 50MHz */
67
68struct uniphier_spi_platdata {
69 void __iomem *base;
70 u32 frequency; /* input frequency */
71 u32 speed_hz;
72 uint deactivate_delay_us; /* Delay to wait after deactivate */
73 uint activate_delay_us; /* Delay to wait after activate */
74};
75
76struct uniphier_spi_priv {
77 void __iomem *base;
78 u8 mode;
79 u8 fifo_depth;
80 u8 bits_per_word;
81 ulong last_transaction_us; /* Time of last transaction end */
82};
83
84static void uniphier_spi_enable(struct uniphier_spi_priv *priv, int enable)
85{
86 u32 val;
87
88 val = readl(priv->base + SSI_CTL);
89 if (enable)
90 val |= SSI_CTL_EN;
91 else
92 val &= ~SSI_CTL_EN;
93 writel(val, priv->base + SSI_CTL);
94}
95
96static void uniphier_spi_regdump(struct uniphier_spi_priv *priv)
97{
98 pr_debug("CTL %08x\n", readl(priv->base + SSI_CTL));
99 pr_debug("CKS %08x\n", readl(priv->base + SSI_CKS));
100 pr_debug("TXWDS %08x\n", readl(priv->base + SSI_TXWDS));
101 pr_debug("RXWDS %08x\n", readl(priv->base + SSI_RXWDS));
102 pr_debug("FPS %08x\n", readl(priv->base + SSI_FPS));
103 pr_debug("SR %08x\n", readl(priv->base + SSI_SR));
104 pr_debug("IE %08x\n", readl(priv->base + SSI_IE));
105 pr_debug("IC %08x\n", readl(priv->base + SSI_IC));
106 pr_debug("FC %08x\n", readl(priv->base + SSI_FC));
107 pr_debug("XDR %08x\n", readl(priv->base + SSI_XDR));
108}
109
110static void spi_cs_activate(struct udevice *dev)
111{
112 struct udevice *bus = dev->parent;
113 struct uniphier_spi_platdata *plat = bus->platdata;
114 struct uniphier_spi_priv *priv = dev_get_priv(bus);
115 ulong delay_us; /* The delay completed so far */
116 u32 val;
117
118 /* If it's too soon to do another transaction, wait */
119 if (plat->deactivate_delay_us && priv->last_transaction_us) {
120 delay_us = timer_get_us() - priv->last_transaction_us;
121 if (delay_us < plat->deactivate_delay_us)
122 udelay(plat->deactivate_delay_us - delay_us);
123 }
124
125 val = readl(priv->base + SSI_FPS);
126 if (priv->mode & SPI_CS_HIGH)
127 val |= SSI_FPS_FSPOL;
128 else
129 val &= ~SSI_FPS_FSPOL;
130 writel(val, priv->base + SSI_FPS);
131
132 if (plat->activate_delay_us)
133 udelay(plat->activate_delay_us);
134}
135
136static void spi_cs_deactivate(struct udevice *dev)
137{
138 struct udevice *bus = dev->parent;
139 struct uniphier_spi_platdata *plat = bus->platdata;
140 struct uniphier_spi_priv *priv = dev_get_priv(bus);
141 u32 val;
142
143 val = readl(priv->base + SSI_FPS);
144 if (priv->mode & SPI_CS_HIGH)
145 val &= ~SSI_FPS_FSPOL;
146 else
147 val |= SSI_FPS_FSPOL;
148 writel(val, priv->base + SSI_FPS);
149
150 /* Remember time of this transaction so we can honour the bus delay */
151 if (plat->deactivate_delay_us)
152 priv->last_transaction_us = timer_get_us();
153}
154
155static int uniphier_spi_claim_bus(struct udevice *dev)
156{
157 struct udevice *bus = dev->parent;
158 struct uniphier_spi_priv *priv = dev_get_priv(bus);
159 u32 val, size;
160
161 uniphier_spi_enable(priv, false);
162
163 /* disable interrupts */
164 writel(0, priv->base + SSI_IE);
165
166 /* bits_per_word */
167 size = priv->bits_per_word;
168 val = readl(priv->base + SSI_TXWDS);
169 val &= ~(SSI_TXWDS_WDLEN_MASK | SSI_TXWDS_DTLEN_MASK);
170 val |= FIELD_PREP(SSI_TXWDS_WDLEN_MASK, size);
171 val |= FIELD_PREP(SSI_TXWDS_DTLEN_MASK, size);
172 writel(val, priv->base + SSI_TXWDS);
173
174 val = readl(priv->base + SSI_RXWDS);
175 val &= ~SSI_RXWDS_DTLEN_MASK;
176 val |= FIELD_PREP(SSI_RXWDS_DTLEN_MASK, size);
177 writel(val, priv->base + SSI_RXWDS);
178
179 /* reset FIFOs */
180 val = SSI_FC_TXFFL | SSI_FC_RXFFL;
181 writel(val, priv->base + SSI_FC);
182
183 /* FIFO threthold */
184 val = readl(priv->base + SSI_FC);
185 val &= ~(SSI_FC_TXFTH_MASK | SSI_FC_RXFTH_MASK);
186 val |= FIELD_PREP(SSI_FC_TXFTH_MASK, priv->fifo_depth);
187 val |= FIELD_PREP(SSI_FC_RXFTH_MASK, priv->fifo_depth);
188 writel(val, priv->base + SSI_FC);
189
190 /* clear interrupts */
191 writel(SSI_IC_TCIC | SSI_IC_RCIC | SSI_IC_RORIC,
192 priv->base + SSI_IC);
193
194 uniphier_spi_enable(priv, true);
195
196 return 0;
197}
198
199static int uniphier_spi_release_bus(struct udevice *dev)
200{
201 struct udevice *bus = dev->parent;
202 struct uniphier_spi_priv *priv = dev_get_priv(bus);
203
204 uniphier_spi_enable(priv, false);
205
206 return 0;
207}
208
209static int uniphier_spi_xfer(struct udevice *dev, unsigned int bitlen,
210 const void *dout, void *din, unsigned long flags)
211{
212 struct udevice *bus = dev->parent;
213 struct uniphier_spi_priv *priv = dev_get_priv(bus);
214 const u8 *tx_buf = dout;
215 u8 *rx_buf = din, buf;
216 u32 len = bitlen / 8;
217 u32 tx_len, rx_len;
218 u32 ts, status;
219 int ret = 0;
220
221 if (bitlen % 8) {
222 dev_err(dev, "Non byte aligned SPI transfer\n");
223 return -EINVAL;
224 }
225
226 if (flags & SPI_XFER_BEGIN)
227 spi_cs_activate(dev);
228
229 uniphier_spi_enable(priv, true);
230
231 ts = get_timer(0);
232 tx_len = len;
233 rx_len = len;
234
235 uniphier_spi_regdump(priv);
236
237 while (tx_len || rx_len) {
238 ret = wait_for_bit_le32(priv->base + SSI_SR, SSI_SR_BUSY, false,
239 SSI_REG_TIMEOUT * 1000, false);
240 if (ret) {
241 if (ret == -ETIMEDOUT)
242 dev_err(dev, "access timeout\n");
243 break;
244 }
245
246 status = readl(priv->base + SSI_SR);
247 /* write the data into TX */
248 if (tx_len && (status & SSI_SR_TNF)) {
249 buf = tx_buf ? *tx_buf++ : 0;
250 writel(buf, priv->base + SSI_XDR);
251 tx_len--;
252 }
253
254 /* read the data from RX */
255 if (rx_len && (status & SSI_SR_RNE)) {
256 buf = readl(priv->base + SSI_XDR);
257 if (rx_buf)
258 *rx_buf++ = buf;
259 rx_len--;
260 }
261
262 if (get_timer(ts) >= SSI_XFER_TIMEOUT) {
263 dev_err(dev, "transfer timeout\n");
264 ret = -ETIMEDOUT;
265 break;
266 }
267 }
268
269 if (flags & SPI_XFER_END)
270 spi_cs_deactivate(dev);
271
272 uniphier_spi_enable(priv, false);
273
274 return ret;
275}
276
277static int uniphier_spi_set_speed(struct udevice *bus, uint speed)
278{
279 struct uniphier_spi_platdata *plat = bus->platdata;
280 struct uniphier_spi_priv *priv = dev_get_priv(bus);
281 u32 val, ckdiv;
282
283 if (speed > plat->frequency)
284 speed = plat->frequency;
285
286 /* baudrate */
287 ckdiv = DIV_ROUND_UP(SSI_CLK, speed);
288 ckdiv = round_up(ckdiv, 2);
289
290 val = readl(priv->base + SSI_CKS);
291 val &= ~SSI_CKS_CKRAT_MASK;
292 val |= ckdiv & SSI_CKS_CKRAT_MASK;
293 writel(val, priv->base + SSI_CKS);
294
295 return 0;
296}
297
298static int uniphier_spi_set_mode(struct udevice *bus, uint mode)
299{
300 struct uniphier_spi_priv *priv = dev_get_priv(bus);
301 u32 val1, val2;
302
303 /*
304 * clock setting
305 * CKPHS capture timing. 0:rising edge, 1:falling edge
306 * CKINIT clock initial level. 0:low, 1:high
307 * CKDLY clock delay. 0:no delay, 1:delay depending on FSTRT
308 * (FSTRT=0: 1 clock, FSTRT=1: 0.5 clock)
309 *
310 * frame setting
311 * FSPOL frame signal porarity. 0: low, 1: high
312 * FSTRT start frame timing
313 * 0: rising edge of clock, 1: falling edge of clock
314 */
315 val1 = readl(priv->base + SSI_CKS);
316 val2 = readl(priv->base + SSI_FPS);
317
318 switch (mode & (SPI_CPOL | SPI_CPHA)) {
319 case SPI_MODE_0:
320 /* CKPHS=1, CKINIT=0, CKDLY=1, FSTRT=0 */
321 val1 |= SSI_CKS_CKPHS | SSI_CKS_CKDLY;
322 val1 &= ~SSI_CKS_CKINIT;
323 val2 &= ~SSI_FPS_FSTRT;
324 break;
325 case SPI_MODE_1:
326 /* CKPHS=0, CKINIT=0, CKDLY=0, FSTRT=1 */
327 val1 &= ~(SSI_CKS_CKPHS | SSI_CKS_CKINIT | SSI_CKS_CKDLY);
328 val2 |= SSI_FPS_FSTRT;
329 break;
330 case SPI_MODE_2:
331 /* CKPHS=0, CKINIT=1, CKDLY=1, FSTRT=1 */
332 val1 |= SSI_CKS_CKINIT | SSI_CKS_CKDLY;
333 val1 &= ~SSI_CKS_CKPHS;
334 val2 |= SSI_FPS_FSTRT;
335 break;
336 case SPI_MODE_3:
337 /* CKPHS=1, CKINIT=1, CKDLY=0, FSTRT=0 */
338 val1 |= SSI_CKS_CKPHS | SSI_CKS_CKINIT;
339 val1 &= ~SSI_CKS_CKDLY;
340 val2 &= ~SSI_FPS_FSTRT;
341 break;
342 }
343
344 writel(val1, priv->base + SSI_CKS);
345 writel(val2, priv->base + SSI_FPS);
346
347 /* format */
348 val1 = readl(priv->base + SSI_TXWDS);
349 val2 = readl(priv->base + SSI_RXWDS);
350 if (mode & SPI_LSB_FIRST) {
351 val1 |= FIELD_PREP(SSI_TXWDS_TDTF_MASK, 1);
352 val2 |= FIELD_PREP(SSI_RXWDS_RDTF_MASK, 1);
353 }
354 writel(val1, priv->base + SSI_TXWDS);
355 writel(val2, priv->base + SSI_RXWDS);
356
357 priv->mode = mode;
358
359 return 0;
360}
361
362static int uniphier_spi_ofdata_to_platdata(struct udevice *bus)
363{
364 struct uniphier_spi_platdata *plat = bus->platdata;
365 const void *blob = gd->fdt_blob;
366 int node = dev_of_offset(bus);
367
368 plat->base = devfdt_get_addr_ptr(bus);
369
370 plat->frequency =
371 fdtdec_get_int(blob, node, "spi-max-frequency", 12500000);
372 plat->deactivate_delay_us =
373 fdtdec_get_int(blob, node, "spi-deactivate-delay", 0);
374 plat->activate_delay_us =
375 fdtdec_get_int(blob, node, "spi-activate-delay", 0);
376 plat->speed_hz = plat->frequency / 2;
377
378 return 0;
379}
380
381static int uniphier_spi_probe(struct udevice *bus)
382{
383 struct uniphier_spi_platdata *plat = dev_get_platdata(bus);
384 struct uniphier_spi_priv *priv = dev_get_priv(bus);
385
386 priv->base = plat->base;
387 priv->fifo_depth = SSI_FIFO_DEPTH;
388 priv->bits_per_word = 8;
389
390 return 0;
391}
392
393static const struct dm_spi_ops uniphier_spi_ops = {
394 .claim_bus = uniphier_spi_claim_bus,
395 .release_bus = uniphier_spi_release_bus,
396 .xfer = uniphier_spi_xfer,
397 .set_speed = uniphier_spi_set_speed,
398 .set_mode = uniphier_spi_set_mode,
399};
400
401static const struct udevice_id uniphier_spi_ids[] = {
402 { .compatible = "socionext,uniphier-scssi" },
403 { /* Sentinel */ }
404};
405
406U_BOOT_DRIVER(uniphier_spi) = {
407 .name = "uniphier_spi",
408 .id = UCLASS_SPI,
409 .of_match = uniphier_spi_ids,
410 .ops = &uniphier_spi_ops,
411 .ofdata_to_platdata = uniphier_spi_ofdata_to_platdata,
412 .platdata_auto_alloc_size = sizeof(struct uniphier_spi_platdata),
413 .priv_auto_alloc_size = sizeof(struct uniphier_spi_priv),
414 .probe = uniphier_spi_probe,
415};