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