1
2
3
4
5
6
7
8
9
10
11
12#include <asm/global_data.h>
13#include <linux/math64.h>
14#include <common.h>
15#include <dm.h>
16#include <errno.h>
17#include <common.h>
18#include <log.h>
19#include <spi.h>
20#include <malloc.h>
21#include <asm/io.h>
22#include <fdtdec.h>
23#ifndef CONFIG_M68K
24#include <asm/arch/clock.h>
25#endif
26#include <fsl_dspi.h>
27#include <linux/bitops.h>
28#include <linux/delay.h>
29
30
31#define NSEC_PER_SEC 1000000000L
32
33DECLARE_GLOBAL_DATA_PTR;
34
35
36#define DSPI_FLAG_REGMAP_ENDIAN_BIG BIT(0)
37
38
39#define DSPI_IDLE_VAL 0x0
40
41
42#define FSL_DSPI_MAX_CHIPSELECT 6
43
44
45#define FSL_DSPI_DEFAULT_SCK_FREQ 10000000
46
47
48#define DSPI_TXRX_WAIT_TIMEOUT 1000000
49
50
51#define DSPI_CTAR_DEFAULT_VALUE (DSPI_CTAR_TRSZ(7) | \
52 DSPI_CTAR_PCSSCK_1CLK | \
53 DSPI_CTAR_PASC(0) | \
54 DSPI_CTAR_PDT(0) | \
55 DSPI_CTAR_CSSCK(0) | \
56 DSPI_CTAR_ASC(0) | \
57 DSPI_CTAR_DT(0))
58
59
60#define DSPI_CTAR_SET_MODE_MASK (DSPI_CTAR_TRSZ(15) | \
61 DSPI_CTAR_PCSSCK(3) | \
62 DSPI_CTAR_PASC(3) | \
63 DSPI_CTAR_PDT(3) | \
64 DSPI_CTAR_CSSCK(15) | \
65 DSPI_CTAR_ASC(15) | \
66 DSPI_CTAR_DT(15))
67
68
69
70
71
72
73
74
75
76struct fsl_dspi_plat {
77 uint flags;
78 uint speed_hz;
79 uint num_chipselect;
80 fdt_addr_t regs_addr;
81};
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96struct fsl_dspi_priv {
97 uint flags;
98 uint mode;
99 uint mcr_val;
100 uint bus_clk;
101 uint speed_hz;
102 uint charbit;
103 uint num_chipselect;
104 uint ctar_val[FSL_DSPI_MAX_CHIPSELECT];
105 struct dspi *regs;
106};
107
108__weak void cpu_dspi_port_conf(void)
109{
110}
111
112__weak int cpu_dspi_claim_bus(uint bus, uint cs)
113{
114 return 0;
115}
116
117__weak void cpu_dspi_release_bus(uint bus, uint cs)
118{
119}
120
121static uint dspi_read32(uint flags, uint *addr)
122{
123 return flags & DSPI_FLAG_REGMAP_ENDIAN_BIG ?
124 in_be32(addr) : in_le32(addr);
125}
126
127static void dspi_write32(uint flags, uint *addr, uint val)
128{
129 flags & DSPI_FLAG_REGMAP_ENDIAN_BIG ?
130 out_be32(addr, val) : out_le32(addr, val);
131}
132
133static void dspi_halt(struct fsl_dspi_priv *priv, u8 halt)
134{
135 uint mcr_val;
136
137 mcr_val = dspi_read32(priv->flags, &priv->regs->mcr);
138
139 if (halt)
140 mcr_val |= DSPI_MCR_HALT;
141 else
142 mcr_val &= ~DSPI_MCR_HALT;
143
144 dspi_write32(priv->flags, &priv->regs->mcr, mcr_val);
145}
146
147static void fsl_dspi_init_mcr(struct fsl_dspi_priv *priv, uint cfg_val)
148{
149
150 dspi_halt(priv, 1);
151
152 dspi_write32(priv->flags, &priv->regs->mcr, cfg_val);
153
154
155 dspi_halt(priv, 0);
156
157 priv->mcr_val = cfg_val;
158}
159
160static void fsl_dspi_cfg_cs_active_state(struct fsl_dspi_priv *priv,
161 uint cs, uint state)
162{
163 uint mcr_val;
164
165 dspi_halt(priv, 1);
166
167 mcr_val = dspi_read32(priv->flags, &priv->regs->mcr);
168 if (state & SPI_CS_HIGH)
169
170 mcr_val &= ~DSPI_MCR_PCSIS(cs);
171 else
172
173 mcr_val |= DSPI_MCR_PCSIS(cs);
174 dspi_write32(priv->flags, &priv->regs->mcr, mcr_val);
175
176 dspi_halt(priv, 0);
177}
178
179static int fsl_dspi_cfg_ctar_mode(struct fsl_dspi_priv *priv,
180 uint cs, uint mode)
181{
182 uint bus_setup;
183
184 bus_setup = dspi_read32(priv->flags, &priv->regs->ctar[0]);
185
186 bus_setup &= ~DSPI_CTAR_SET_MODE_MASK;
187 bus_setup |= priv->ctar_val[cs];
188 bus_setup &= ~(DSPI_CTAR_CPOL | DSPI_CTAR_CPHA | DSPI_CTAR_LSBFE);
189
190 if (mode & SPI_CPOL)
191 bus_setup |= DSPI_CTAR_CPOL;
192 if (mode & SPI_CPHA)
193 bus_setup |= DSPI_CTAR_CPHA;
194 if (mode & SPI_LSB_FIRST)
195 bus_setup |= DSPI_CTAR_LSBFE;
196
197 dspi_write32(priv->flags, &priv->regs->ctar[0], bus_setup);
198
199 priv->charbit =
200 ((dspi_read32(priv->flags, &priv->regs->ctar[0]) &
201 DSPI_CTAR_TRSZ(15)) == DSPI_CTAR_TRSZ(15)) ? 16 : 8;
202
203 return 0;
204}
205
206static void fsl_dspi_clr_fifo(struct fsl_dspi_priv *priv)
207{
208 uint mcr_val;
209
210 dspi_halt(priv, 1);
211 mcr_val = dspi_read32(priv->flags, &priv->regs->mcr);
212
213 mcr_val |= (DSPI_MCR_CTXF | DSPI_MCR_CRXF);
214 dspi_write32(priv->flags, &priv->regs->mcr, mcr_val);
215 dspi_halt(priv, 0);
216}
217
218static void dspi_tx(struct fsl_dspi_priv *priv, u32 ctrl, u16 data)
219{
220 int timeout = DSPI_TXRX_WAIT_TIMEOUT;
221
222
223 while (DSPI_SR_TXCTR(dspi_read32(priv->flags, &priv->regs->sr)) >= 4 &&
224 timeout--)
225 udelay(1);
226
227 if (timeout >= 0)
228 dspi_write32(priv->flags, &priv->regs->tfr, (ctrl | data));
229 else
230 debug("dspi_tx: waiting timeout!\n");
231}
232
233static u16 dspi_rx(struct fsl_dspi_priv *priv)
234{
235 int timeout = DSPI_TXRX_WAIT_TIMEOUT;
236
237
238 while (DSPI_SR_RXCTR(dspi_read32(priv->flags, &priv->regs->sr)) == 0 &&
239 timeout--)
240 udelay(1);
241
242 if (timeout >= 0)
243 return (u16)DSPI_RFR_RXDATA(
244 dspi_read32(priv->flags, &priv->regs->rfr));
245 else {
246 debug("dspi_rx: waiting timeout!\n");
247 return (u16)(~0);
248 }
249}
250
251static int dspi_xfer(struct fsl_dspi_priv *priv, uint cs, unsigned int bitlen,
252 const void *dout, void *din, unsigned long flags)
253{
254 u16 *spi_rd16 = NULL, *spi_wr16 = NULL;
255 u8 *spi_rd = NULL, *spi_wr = NULL;
256 static u32 ctrl;
257 uint len = bitlen >> 3;
258
259 if (priv->charbit == 16) {
260 bitlen >>= 1;
261 spi_wr16 = (u16 *)dout;
262 spi_rd16 = (u16 *)din;
263 } else {
264 spi_wr = (u8 *)dout;
265 spi_rd = (u8 *)din;
266 }
267
268 if ((flags & SPI_XFER_BEGIN) == SPI_XFER_BEGIN)
269 ctrl |= DSPI_TFR_CONT;
270
271 ctrl = ctrl & DSPI_TFR_CONT;
272 ctrl = ctrl | DSPI_TFR_CTAS(0) | DSPI_TFR_PCS(cs);
273
274 if (len > 1) {
275 int tmp_len = len - 1;
276 while (tmp_len--) {
277 if ((dout != NULL) && (din != NULL)) {
278 if (priv->charbit == 16) {
279 dspi_tx(priv, ctrl, *spi_wr16++);
280 *spi_rd16++ = dspi_rx(priv);
281 }
282 else {
283 dspi_tx(priv, ctrl, *spi_wr++);
284 *spi_rd++ = dspi_rx(priv);
285 }
286 }
287
288 else if (dout != NULL) {
289 if (priv->charbit == 16)
290 dspi_tx(priv, ctrl, *spi_wr16++);
291 else
292 dspi_tx(priv, ctrl, *spi_wr++);
293 dspi_rx(priv);
294 }
295
296 else if (din != NULL) {
297 dspi_tx(priv, ctrl, DSPI_IDLE_VAL);
298 if (priv->charbit == 16)
299 *spi_rd16++ = dspi_rx(priv);
300 else
301 *spi_rd++ = dspi_rx(priv);
302 }
303 }
304
305 len = 1;
306 }
307
308 if ((flags & SPI_XFER_END) == SPI_XFER_END)
309 ctrl &= ~DSPI_TFR_CONT;
310
311 if (len) {
312 if ((dout != NULL) && (din != NULL)) {
313 if (priv->charbit == 16) {
314 dspi_tx(priv, ctrl, *spi_wr16++);
315 *spi_rd16++ = dspi_rx(priv);
316 }
317 else {
318 dspi_tx(priv, ctrl, *spi_wr++);
319 *spi_rd++ = dspi_rx(priv);
320 }
321 }
322
323 else if (dout != NULL) {
324 if (priv->charbit == 16)
325 dspi_tx(priv, ctrl, *spi_wr16);
326 else
327 dspi_tx(priv, ctrl, *spi_wr);
328 dspi_rx(priv);
329 }
330
331 else if (din != NULL) {
332 dspi_tx(priv, ctrl, DSPI_IDLE_VAL);
333 if (priv->charbit == 16)
334 *spi_rd16 = dspi_rx(priv);
335 else
336 *spi_rd = dspi_rx(priv);
337 }
338 } else {
339
340 dspi_tx(priv, ctrl, DSPI_IDLE_VAL);
341 dspi_rx(priv);
342 }
343
344 return 0;
345}
346
347
348
349
350
351
352
353
354
355
356
357static int fsl_dspi_hz_to_spi_baud(int *pbr, int *br,
358 int speed_hz, uint clkrate)
359{
360
361 int pbr_tbl[4] = {2, 3, 5, 7};
362 int brs[16] = {2, 4, 6, 8,
363 16, 32, 64, 128,
364 256, 512, 1024, 2048,
365 4096, 8192, 16384, 32768};
366 int temp, i = 0, j = 0;
367
368 temp = clkrate / speed_hz;
369
370 for (i = 0; i < ARRAY_SIZE(pbr_tbl); i++)
371 for (j = 0; j < ARRAY_SIZE(brs); j++) {
372 if (pbr_tbl[i] * brs[j] >= temp) {
373 *pbr = i;
374 *br = j;
375 return 0;
376 }
377 }
378
379 debug("Can not find valid baud rate,speed_hz is %d, ", speed_hz);
380 debug("clkrate is %d, we use the max prescaler value.\n", clkrate);
381
382 *pbr = ARRAY_SIZE(pbr_tbl) - 1;
383 *br = ARRAY_SIZE(brs) - 1;
384 return -EINVAL;
385}
386
387static void ns_delay_scale(unsigned char *psc, unsigned char *sc, int delay_ns,
388 unsigned long clkrate)
389{
390 int scale_needed, scale, minscale = INT_MAX;
391 int pscale_tbl[4] = {1, 3, 5, 7};
392 u32 remainder;
393 int i, j;
394
395 scale_needed = div_u64_rem((u64)delay_ns * clkrate, NSEC_PER_SEC,
396 &remainder);
397 if (remainder)
398 scale_needed++;
399
400 for (i = 0; i < ARRAY_SIZE(pscale_tbl); i++)
401 for (j = 0; j <= DSPI_CTAR_SCALE_BITS; j++) {
402 scale = pscale_tbl[i] * (2 << j);
403 if (scale >= scale_needed) {
404 if (scale < minscale) {
405 minscale = scale;
406 *psc = i;
407 *sc = j;
408 }
409 break;
410 }
411 }
412
413 if (minscale == INT_MAX) {
414 pr_warn("Cannot find correct scale values for %dns delay at clkrate %ld, using max prescaler value",
415 delay_ns, clkrate);
416 *psc = ARRAY_SIZE(pscale_tbl) - 1;
417 *sc = DSPI_CTAR_SCALE_BITS;
418 }
419}
420
421static int fsl_dspi_cfg_speed(struct fsl_dspi_priv *priv, uint speed)
422{
423 int ret;
424 uint bus_setup;
425 int best_i, best_j, bus_clk;
426
427 bus_clk = priv->bus_clk;
428
429 debug("DSPI set_speed: expected SCK speed %u, bus_clk %u.\n",
430 speed, bus_clk);
431
432 bus_setup = dspi_read32(priv->flags, &priv->regs->ctar[0]);
433 bus_setup &= ~(DSPI_CTAR_DBR | DSPI_CTAR_PBR(0x3) | DSPI_CTAR_BR(0xf));
434
435 ret = fsl_dspi_hz_to_spi_baud(&best_i, &best_j, speed, bus_clk);
436 if (ret) {
437 speed = priv->speed_hz;
438 debug("DSPI set_speed use default SCK rate %u.\n", speed);
439 fsl_dspi_hz_to_spi_baud(&best_i, &best_j, speed, bus_clk);
440 }
441
442 bus_setup |= (DSPI_CTAR_PBR(best_i) | DSPI_CTAR_BR(best_j));
443 dspi_write32(priv->flags, &priv->regs->ctar[0], bus_setup);
444
445 priv->speed_hz = speed;
446
447 return 0;
448}
449
450static int fsl_dspi_child_pre_probe(struct udevice *dev)
451{
452 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
453 struct fsl_dspi_priv *priv = dev_get_priv(dev->parent);
454 u32 cs_sck_delay = 0, sck_cs_delay = 0;
455 unsigned char pcssck = 0, cssck = 0;
456 unsigned char pasc = 0, asc = 0;
457
458 if (slave_plat->cs >= priv->num_chipselect) {
459 debug("DSPI invalid chipselect number %d(max %d)!\n",
460 slave_plat->cs, priv->num_chipselect - 1);
461 return -EINVAL;
462 }
463
464 ofnode_read_u32(dev_ofnode(dev), "fsl,spi-cs-sck-delay",
465 &cs_sck_delay);
466 ofnode_read_u32(dev_ofnode(dev), "fsl,spi-sck-cs-delay",
467 &sck_cs_delay);
468
469
470 ns_delay_scale(&pcssck, &cssck, cs_sck_delay, priv->bus_clk);
471
472
473 ns_delay_scale(&pasc, &asc, sck_cs_delay, priv->bus_clk);
474
475 priv->ctar_val[slave_plat->cs] = DSPI_CTAR_DEFAULT_VALUE |
476 DSPI_CTAR_PCSSCK(pcssck) |
477 DSPI_CTAR_PASC(pasc);
478
479 debug("DSPI pre_probe slave device on CS %u, max_hz %u, mode 0x%x.\n",
480 slave_plat->cs, slave_plat->max_hz, slave_plat->mode);
481
482 return 0;
483}
484
485static int fsl_dspi_probe(struct udevice *bus)
486{
487 struct fsl_dspi_plat *plat = dev_get_plat(bus);
488 struct fsl_dspi_priv *priv = dev_get_priv(bus);
489 struct dm_spi_bus *dm_spi_bus;
490 uint mcr_cfg_val;
491
492 dm_spi_bus = dev_get_uclass_priv(bus);
493
494
495 cpu_dspi_port_conf();
496
497
498 priv->regs = (struct dspi *)plat->regs_addr;
499 priv->flags = plat->flags;
500#ifdef CONFIG_M68K
501 priv->bus_clk = gd->bus_clk;
502#else
503 priv->bus_clk = mxc_get_clock(MXC_DSPI_CLK);
504#endif
505 priv->num_chipselect = plat->num_chipselect;
506 priv->speed_hz = plat->speed_hz;
507
508 priv->charbit = 8;
509
510 dm_spi_bus->max_hz = plat->speed_hz;
511
512
513 mcr_cfg_val = DSPI_MCR_MSTR | DSPI_MCR_PCSIS_MASK |
514 DSPI_MCR_CRXF | DSPI_MCR_CTXF;
515 fsl_dspi_init_mcr(priv, mcr_cfg_val);
516
517 debug("%s probe done, bus-num %d.\n", bus->name, dev_seq(bus));
518
519 return 0;
520}
521
522static int fsl_dspi_claim_bus(struct udevice *dev)
523{
524 uint sr_val;
525 struct fsl_dspi_priv *priv;
526 struct udevice *bus = dev->parent;
527 struct dm_spi_slave_plat *slave_plat =
528 dev_get_parent_plat(dev);
529
530 priv = dev_get_priv(bus);
531
532
533 cpu_dspi_claim_bus(dev_seq(bus), slave_plat->cs);
534
535
536 fsl_dspi_cfg_ctar_mode(priv, slave_plat->cs, priv->mode);
537
538
539 fsl_dspi_cfg_cs_active_state(priv, slave_plat->cs,
540 priv->mode);
541
542 fsl_dspi_clr_fifo(priv);
543
544
545 sr_val = dspi_read32(priv->flags, &priv->regs->sr);
546 if ((sr_val & DSPI_SR_TXRXS) != DSPI_SR_TXRXS) {
547 debug("DSPI RX/TX not ready!\n");
548 return -EIO;
549 }
550
551 return 0;
552}
553
554static int fsl_dspi_release_bus(struct udevice *dev)
555{
556 struct udevice *bus = dev->parent;
557 struct fsl_dspi_priv *priv = dev_get_priv(bus);
558 struct dm_spi_slave_plat *slave_plat =
559 dev_get_parent_plat(dev);
560
561
562 dspi_halt(priv, 1);
563
564
565 cpu_dspi_release_bus(dev_seq(bus), slave_plat->cs);
566
567 return 0;
568}
569
570
571
572
573static int fsl_dspi_bind(struct udevice *bus)
574{
575 debug("%s assigned seq %d.\n", bus->name, dev_seq(bus));
576 return 0;
577}
578
579static int fsl_dspi_of_to_plat(struct udevice *bus)
580{
581 fdt_addr_t addr;
582 struct fsl_dspi_plat *plat = dev_get_plat(bus);
583 const void *blob = gd->fdt_blob;
584 int node = dev_of_offset(bus);
585
586 if (fdtdec_get_bool(blob, node, "big-endian"))
587 plat->flags |= DSPI_FLAG_REGMAP_ENDIAN_BIG;
588
589 plat->num_chipselect = fdtdec_get_int(blob, node,
590 "spi-num-chipselects",
591 FSL_DSPI_MAX_CHIPSELECT);
592
593 addr = dev_read_addr(bus);
594 if (addr == FDT_ADDR_T_NONE) {
595 debug("DSPI: Can't get base address or size\n");
596 return -ENOMEM;
597 }
598 plat->regs_addr = addr;
599
600 plat->speed_hz = fdtdec_get_int(blob,
601 node, "spi-max-frequency", FSL_DSPI_DEFAULT_SCK_FREQ);
602
603 debug("DSPI: regs=%pa, max-frequency=%d, endianess=%s, num-cs=%d\n",
604 &plat->regs_addr, plat->speed_hz,
605 plat->flags & DSPI_FLAG_REGMAP_ENDIAN_BIG ? "be" : "le",
606 plat->num_chipselect);
607
608 return 0;
609}
610
611static int fsl_dspi_xfer(struct udevice *dev, unsigned int bitlen,
612 const void *dout, void *din, unsigned long flags)
613{
614 struct fsl_dspi_priv *priv;
615 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
616 struct udevice *bus;
617
618 bus = dev->parent;
619 priv = dev_get_priv(bus);
620
621 return dspi_xfer(priv, slave_plat->cs, bitlen, dout, din, flags);
622}
623
624static int fsl_dspi_set_speed(struct udevice *bus, uint speed)
625{
626 struct fsl_dspi_priv *priv = dev_get_priv(bus);
627
628 return fsl_dspi_cfg_speed(priv, speed);
629}
630
631static int fsl_dspi_set_mode(struct udevice *bus, uint mode)
632{
633 struct fsl_dspi_priv *priv = dev_get_priv(bus);
634
635 debug("DSPI set_mode: mode 0x%x.\n", mode);
636
637
638
639
640
641
642
643 priv->mode = mode;
644
645 return 0;
646}
647
648static const struct dm_spi_ops fsl_dspi_ops = {
649 .claim_bus = fsl_dspi_claim_bus,
650 .release_bus = fsl_dspi_release_bus,
651 .xfer = fsl_dspi_xfer,
652 .set_speed = fsl_dspi_set_speed,
653 .set_mode = fsl_dspi_set_mode,
654};
655
656static const struct udevice_id fsl_dspi_ids[] = {
657 { .compatible = "fsl,vf610-dspi" },
658 { .compatible = "fsl,ls1021a-v1.0-dspi" },
659 { }
660};
661
662U_BOOT_DRIVER(fsl_dspi) = {
663 .name = "fsl_dspi",
664 .id = UCLASS_SPI,
665 .of_match = fsl_dspi_ids,
666 .ops = &fsl_dspi_ops,
667 .of_to_plat = fsl_dspi_of_to_plat,
668 .plat_auto = sizeof(struct fsl_dspi_plat),
669 .priv_auto = sizeof(struct fsl_dspi_priv),
670 .probe = fsl_dspi_probe,
671 .child_pre_probe = fsl_dspi_child_pre_probe,
672 .bind = fsl_dspi_bind,
673};
674