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