blob: f1422cf893e7a6d394510175fc04ee56176da7f3 [file] [log] [blame]
Jassi Brar971a3442021-06-04 18:44:27 +09001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * spi-synquacer.c - Socionext Synquacer SPI driver
4 * Copyright 2021 Linaro Ltd.
5 * Copyright 2021 Socionext, Inc.
6 */
7
8#include <clk.h>
9#include <common.h>
10#include <dm.h>
11#include <log.h>
12#include <time.h>
13#include <dm/device_compat.h>
14#include <linux/bitfield.h>
15#include <linux/bitops.h>
16#include <linux/delay.h>
17#include <linux/io.h>
18#include <spi.h>
19#include <wait_bit.h>
20
21#define MCTRL 0x0
22#define MEN 0
23#define CSEN 1
24#define IPCLK 3
25#define MES 4
26#define SYNCON 5
27
28#define PCC0 0x4
29#define PCC(n) (PCC0 + (n) * 4)
30#define RTM 3
31#define ACES 2
32#define SAFESYNC 16
33#define CPHA 0
34#define CPOL 1
35#define SSPOL 4
36#define SDIR 7
37#define SS2CD 5
38#define SENDIAN 8
39#define CDRS_SHIFT 9
40#define CDRS_MASK 0x7f
41
42#define TXF 0x14
43#define TXE 0x18
44#define TXC 0x1c
45#define RXF 0x20
46#define RXE 0x24
47#define RXC 0x28
48#define TFLETE 4
Masahisa Kojima88d50ed2022-05-17 17:41:37 +090049#define TSSRS 6
Jassi Brar971a3442021-06-04 18:44:27 +090050#define RFMTE 5
Masahisa Kojima88d50ed2022-05-17 17:41:37 +090051#define RSSRS 6
Jassi Brar971a3442021-06-04 18:44:27 +090052
53#define FAULTF 0x2c
54#define FAULTC 0x30
55
56#define DMCFG 0x34
57#define SSDC 1
58#define MSTARTEN 2
59
60#define DMSTART 0x38
61#define TRIGGER 0
62#define DMSTOP 8
63#define CS_MASK 3
64#define CS_SHIFT 16
65#define DATA_TXRX 0
66#define DATA_RX 1
67#define DATA_TX 2
68#define DATA_MASK 3
69#define DATA_SHIFT 26
70#define BUS_WIDTH 24
71
72#define DMBCC 0x3c
73#define DMSTATUS 0x40
74#define RX_DATA_MASK 0x1f
75#define RX_DATA_SHIFT 8
76#define TX_DATA_MASK 0x1f
77#define TX_DATA_SHIFT 16
78
79#define TXBITCNT 0x44
80
81#define FIFOCFG 0x4c
82#define BPW_MASK 0x3
83#define BPW_SHIFT 8
84#define RX_FLUSH 11
85#define TX_FLUSH 12
86#define RX_TRSHLD_MASK 0xf
87#define RX_TRSHLD_SHIFT 0
88#define TX_TRSHLD_MASK 0xf
89#define TX_TRSHLD_SHIFT 4
90
91#define TXFIFO 0x50
92#define RXFIFO 0x90
93#define MID 0xfc
94
95#define FIFO_DEPTH 16
96#define TX_TRSHLD 4
97#define RX_TRSHLD (FIFO_DEPTH - TX_TRSHLD)
98
99#define TXBIT 1
100#define RXBIT 2
101
102DECLARE_GLOBAL_DATA_PTR;
103
104struct synquacer_spi_plat {
105 void __iomem *base;
106 bool aces, rtm;
107};
108
109struct synquacer_spi_priv {
110 void __iomem *base;
111 bool aces, rtm;
112 int speed, cs, mode, rwflag;
113 void *rx_buf;
114 const void *tx_buf;
115 unsigned int tx_words, rx_words;
116};
117
118static void read_fifo(struct synquacer_spi_priv *priv)
119{
120 u32 len = readl(priv->base + DMSTATUS);
121 u8 *buf = priv->rx_buf;
122 int i;
123
124 len = (len >> RX_DATA_SHIFT) & RX_DATA_MASK;
125 len = min_t(unsigned int, len, priv->rx_words);
126
127 for (i = 0; i < len; i++)
128 *buf++ = readb(priv->base + RXFIFO);
129
130 priv->rx_buf = buf;
131 priv->rx_words -= len;
132}
133
134static void write_fifo(struct synquacer_spi_priv *priv)
135{
136 u32 len = readl(priv->base + DMSTATUS);
137 const u8 *buf = priv->tx_buf;
138 int i;
139
140 len = (len >> TX_DATA_SHIFT) & TX_DATA_MASK;
141 len = min_t(unsigned int, FIFO_DEPTH - len, priv->tx_words);
142
143 for (i = 0; i < len; i++)
144 writeb(*buf++, priv->base + TXFIFO);
145
146 priv->tx_buf = buf;
147 priv->tx_words -= len;
148}
149
150static void synquacer_cs_set(struct synquacer_spi_priv *priv, bool active)
151{
152 u32 val;
153
154 val = readl(priv->base + DMSTART);
155 val &= ~(CS_MASK << CS_SHIFT);
156 val |= priv->cs << CS_SHIFT;
157
158 if (active) {
159 writel(val, priv->base + DMSTART);
160
161 val = readl(priv->base + DMSTART);
162 val &= ~BIT(DMSTOP);
163 writel(val, priv->base + DMSTART);
164 } else {
165 val |= BIT(DMSTOP);
166 writel(val, priv->base + DMSTART);
167
168 if (priv->rx_buf) {
169 u32 buf[16];
170
171 priv->rx_buf = buf;
172 priv->rx_words = 16;
173 read_fifo(priv);
174 }
Masahisa Kojima88d50ed2022-05-17 17:41:37 +0900175
176 /* wait until slave is deselected */
177 while (!(readl(priv->base + TXF) & BIT(TSSRS)) ||
178 !(readl(priv->base + RXF) & BIT(RSSRS)))
179 ;
Jassi Brar971a3442021-06-04 18:44:27 +0900180 }
181}
182
183static void synquacer_spi_config(struct udevice *dev, void *rx, const void *tx)
184{
185 struct udevice *bus = dev->parent;
186 struct synquacer_spi_priv *priv = dev_get_priv(bus);
187 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
188 u32 val, div, bus_width;
189 int rwflag;
190
191 rwflag = (rx ? 1 : 0) | (tx ? 2 : 0);
192
193 /* if nothing to do */
194 if (slave_plat->mode == priv->mode &&
195 rwflag == priv->rwflag &&
196 slave_plat->cs == priv->cs &&
197 slave_plat->max_hz == priv->speed)
198 return;
199
200 priv->rwflag = rwflag;
201 priv->cs = slave_plat->cs;
202 priv->mode = slave_plat->mode;
203 priv->speed = slave_plat->max_hz;
204
205 if (priv->mode & SPI_TX_BYTE)
206 bus_width = 1;
207 else if (priv->mode & SPI_TX_DUAL)
208 bus_width = 2;
209 else if (priv->mode & SPI_TX_QUAD)
210 bus_width = 4;
211 else if (priv->mode & SPI_TX_OCTAL)
212 bus_width = 8;
213
214 div = DIV_ROUND_UP(125000000, priv->speed);
215
216 val = readl(priv->base + PCC(priv->cs));
217 val &= ~BIT(RTM);
218 val &= ~BIT(ACES);
219 val &= ~BIT(SAFESYNC);
220 if ((priv->mode & (SPI_TX_DUAL | SPI_RX_DUAL)) && div < 3)
221 val |= BIT(SAFESYNC);
222 if ((priv->mode & (SPI_TX_QUAD | SPI_RX_QUAD)) && div < 6)
223 val |= BIT(SAFESYNC);
224
225 if (priv->mode & SPI_CPHA)
226 val |= BIT(CPHA);
227 else
228 val &= ~BIT(CPHA);
229
230 if (priv->mode & SPI_CPOL)
231 val |= BIT(CPOL);
232 else
233 val &= ~BIT(CPOL);
234
235 if (priv->mode & SPI_CS_HIGH)
236 val |= BIT(SSPOL);
237 else
238 val &= ~BIT(SSPOL);
239
240 if (priv->mode & SPI_LSB_FIRST)
241 val |= BIT(SDIR);
242 else
243 val &= ~BIT(SDIR);
244
245 if (priv->aces)
246 val |= BIT(ACES);
247
248 if (priv->rtm)
249 val |= BIT(RTM);
250
251 val |= (3 << SS2CD);
252 val |= BIT(SENDIAN);
253
254 val &= ~(CDRS_MASK << CDRS_SHIFT);
255 val |= ((div >> 1) << CDRS_SHIFT);
256
257 writel(val, priv->base + PCC(priv->cs));
258
259 val = readl(priv->base + FIFOCFG);
260 val &= ~(BPW_MASK << BPW_SHIFT);
261 val |= (0 << BPW_SHIFT);
262 writel(val, priv->base + FIFOCFG);
263
264 val = readl(priv->base + DMSTART);
265 val &= ~(DATA_MASK << DATA_SHIFT);
266
267 if (tx && rx)
268 val |= (DATA_TXRX << DATA_SHIFT);
269 else if (rx)
270 val |= (DATA_RX << DATA_SHIFT);
271 else
272 val |= (DATA_TX << DATA_SHIFT);
273
274 val &= ~(3 << BUS_WIDTH);
275 val |= ((bus_width >> 1) << BUS_WIDTH);
276 writel(val, priv->base + DMSTART);
277}
278
279static int synquacer_spi_xfer(struct udevice *dev, unsigned int bitlen,
280 const void *tx_buf, void *rx_buf,
281 unsigned long flags)
282{
283 struct udevice *bus = dev->parent;
284 struct synquacer_spi_priv *priv = dev_get_priv(bus);
Masahisa Kojima29d382b2022-05-17 17:41:36 +0900285 u32 val, words, busy = 0;
Jassi Brar971a3442021-06-04 18:44:27 +0900286
287 val = readl(priv->base + FIFOCFG);
288 val |= (1 << RX_FLUSH);
289 val |= (1 << TX_FLUSH);
290 writel(val, priv->base + FIFOCFG);
291
292 synquacer_spi_config(dev, rx_buf, tx_buf);
293
294 priv->tx_buf = tx_buf;
295 priv->rx_buf = rx_buf;
296
297 words = bitlen / 8;
298
299 if (tx_buf) {
300 busy |= BIT(TXBIT);
301 priv->tx_words = words;
302 } else {
303 busy &= ~BIT(TXBIT);
304 priv->tx_words = 0;
305 }
306
307 if (rx_buf) {
308 busy |= BIT(RXBIT);
309 priv->rx_words = words;
310 } else {
311 busy &= ~BIT(RXBIT);
312 priv->rx_words = 0;
313 }
314
315 if (flags & SPI_XFER_BEGIN)
316 synquacer_cs_set(priv, true);
317
318 if (tx_buf)
319 write_fifo(priv);
320
321 if (rx_buf) {
322 val = readl(priv->base + FIFOCFG);
323 val &= ~(RX_TRSHLD_MASK << RX_TRSHLD_SHIFT);
324 val |= ((priv->rx_words > FIFO_DEPTH ?
325 RX_TRSHLD : priv->rx_words) << RX_TRSHLD_SHIFT);
326 writel(val, priv->base + FIFOCFG);
327 }
328
329 writel(~0, priv->base + TXC);
330 writel(~0, priv->base + RXC);
331
332 /* Trigger */
333 val = readl(priv->base + DMSTART);
334 val |= BIT(TRIGGER);
335 writel(val, priv->base + DMSTART);
336
337 while (busy & (BIT(RXBIT) | BIT(TXBIT))) {
338 if (priv->rx_words)
339 read_fifo(priv);
340 else
341 busy &= ~BIT(RXBIT);
342
343 if (priv->tx_words) {
344 write_fifo(priv);
345 } else {
346 u32 len;
347
348 do { /* wait for shifter to empty out */
349 cpu_relax();
350 len = readl(priv->base + DMSTATUS);
351 len = (len >> TX_DATA_SHIFT) & TX_DATA_MASK;
352 } while (tx_buf && len);
353 busy &= ~BIT(TXBIT);
354 }
355 }
356
357 if (flags & SPI_XFER_END)
358 synquacer_cs_set(priv, false);
359
360 return 0;
361}
362
363static int synquacer_spi_set_speed(struct udevice *bus, uint speed)
364{
365 return 0;
366}
367
368static int synquacer_spi_set_mode(struct udevice *bus, uint mode)
369{
370 return 0;
371}
372
373static int synquacer_spi_claim_bus(struct udevice *dev)
374{
375 return 0;
376}
377
378static int synquacer_spi_release_bus(struct udevice *dev)
379{
380 return 0;
381}
382
383static void synquacer_spi_disable_module(struct synquacer_spi_priv *priv)
384{
385 writel(0, priv->base + MCTRL);
386 while (readl(priv->base + MCTRL) & BIT(MES))
387 cpu_relax();
388}
389
390static void synquacer_spi_init(struct synquacer_spi_priv *priv)
391{
392 u32 val;
393
394 synquacer_spi_disable_module(priv);
395
396 writel(0, priv->base + TXE);
397 writel(0, priv->base + RXE);
398 val = readl(priv->base + TXF);
399 writel(val, priv->base + TXC);
400 val = readl(priv->base + RXF);
401 writel(val, priv->base + RXC);
402 val = readl(priv->base + FAULTF);
403 writel(val, priv->base + FAULTC);
404
405 val = readl(priv->base + DMCFG);
406 val &= ~BIT(SSDC);
407 val &= ~BIT(MSTARTEN);
408 writel(val, priv->base + DMCFG);
409
410 /* Enable module with direct mode */
411 val = readl(priv->base + MCTRL);
412 val &= ~BIT(IPCLK);
413 val &= ~BIT(CSEN);
414 val |= BIT(MEN);
415 val |= BIT(SYNCON);
416 writel(val, priv->base + MCTRL);
417}
418
419static void synquacer_spi_exit(struct synquacer_spi_priv *priv)
420{
421 u32 val;
422
423 synquacer_spi_disable_module(priv);
424
425 /* Enable module with command sequence mode */
426 val = readl(priv->base + MCTRL);
427 val &= ~BIT(IPCLK);
428 val |= BIT(CSEN);
429 val |= BIT(MEN);
430 val |= BIT(SYNCON);
431 writel(val, priv->base + MCTRL);
432
433 while (!(readl(priv->base + MCTRL) & BIT(MES)))
434 cpu_relax();
435}
436
437static int synquacer_spi_probe(struct udevice *bus)
438{
439 struct synquacer_spi_plat *plat = dev_get_plat(bus);
440 struct synquacer_spi_priv *priv = dev_get_priv(bus);
441
442 priv->base = plat->base;
443 priv->aces = plat->aces;
444 priv->rtm = plat->rtm;
445
446 synquacer_spi_init(priv);
447 return 0;
448}
449
450static int synquacer_spi_remove(struct udevice *bus)
451{
452 struct synquacer_spi_priv *priv = dev_get_priv(bus);
453
454 synquacer_spi_exit(priv);
455 return 0;
456}
457
458static int synquacer_spi_of_to_plat(struct udevice *bus)
459{
460 struct synquacer_spi_plat *plat = dev_get_plat(bus);
461 struct clk clk;
462
463 plat->base = dev_read_addr_ptr(bus);
464
465 plat->aces = dev_read_bool(bus, "socionext,set-aces");
466 plat->rtm = dev_read_bool(bus, "socionext,use-rtm");
467
468 clk_get_by_name(bus, "iHCLK", &clk);
469 clk_enable(&clk);
470
471 return 0;
472}
473
474static const struct dm_spi_ops synquacer_spi_ops = {
475 .claim_bus = synquacer_spi_claim_bus,
476 .release_bus = synquacer_spi_release_bus,
477 .xfer = synquacer_spi_xfer,
478 .set_speed = synquacer_spi_set_speed,
479 .set_mode = synquacer_spi_set_mode,
480};
481
482static const struct udevice_id synquacer_spi_ids[] = {
483 { .compatible = "socionext,synquacer-spi" },
484 { /* Sentinel */ }
485};
486
487U_BOOT_DRIVER(synquacer_spi) = {
488 .name = "synquacer_spi",
489 .id = UCLASS_SPI,
490 .of_match = synquacer_spi_ids,
491 .ops = &synquacer_spi_ops,
492 .of_to_plat = synquacer_spi_of_to_plat,
493 .plat_auto = sizeof(struct synquacer_spi_plat),
494 .priv_auto = sizeof(struct synquacer_spi_priv),
495 .probe = synquacer_spi_probe,
496 .flags = DM_FLAG_OS_PREPARE,
497 .remove = synquacer_spi_remove,
498};