1
2
3
4
5
6
7
8
9#include <clk.h>
10#include <common.h>
11#include <dm.h>
12#include <dm/device_compat.h>
13#include <log.h>
14#include <malloc.h>
15#include <spi.h>
16#include <asm/io.h>
17#include <linux/bitops.h>
18
19DECLARE_GLOBAL_DATA_PTR;
20
21
22#define ZYNQ_QSPI_CR_IFMODE_MASK BIT(31)
23#define ZYNQ_QSPI_CR_MSA_MASK BIT(15)
24#define ZYNQ_QSPI_CR_MCS_MASK BIT(14)
25#define ZYNQ_QSPI_CR_PCS_MASK BIT(10)
26#define ZYNQ_QSPI_CR_FW_MASK GENMASK(7, 6)
27#define ZYNQ_QSPI_CR_SS_MASK GENMASK(13, 10)
28#define ZYNQ_QSPI_CR_BAUD_MASK GENMASK(5, 3)
29#define ZYNQ_QSPI_CR_CPHA_MASK BIT(2)
30#define ZYNQ_QSPI_CR_CPOL_MASK BIT(1)
31#define ZYNQ_QSPI_CR_MSTREN_MASK BIT(0)
32#define ZYNQ_QSPI_IXR_RXNEMPTY_MASK BIT(4)
33#define ZYNQ_QSPI_IXR_TXOW_MASK BIT(2)
34#define ZYNQ_QSPI_IXR_ALL_MASK GENMASK(6, 0)
35#define ZYNQ_QSPI_ENR_SPI_EN_MASK BIT(0)
36#define ZYNQ_QSPI_LQSPICFG_LQMODE_MASK BIT(31)
37
38
39#define ZYNQ_QSPI_TXD_00_00_OFFSET 0x1C
40#define ZYNQ_QSPI_TXD_00_01_OFFSET 0x80
41#define ZYNQ_QSPI_TXD_00_10_OFFSET 0x84
42#define ZYNQ_QSPI_TXD_00_11_OFFSET 0x88
43
44#define ZYNQ_QSPI_TXFIFO_THRESHOLD 1
45#define ZYNQ_QSPI_RXFIFO_THRESHOLD 32
46
47#define ZYNQ_QSPI_CR_BAUD_MAX 8
48#define ZYNQ_QSPI_CR_BAUD_SHIFT 3
49#define ZYNQ_QSPI_CR_SS_SHIFT 10
50
51#define ZYNQ_QSPI_FIFO_DEPTH 63
52#define ZYNQ_QSPI_WAIT (CONFIG_SYS_HZ / 100)
53
54
55struct zynq_qspi_regs {
56 u32 cr;
57 u32 isr;
58 u32 ier;
59 u32 idr;
60 u32 imr;
61 u32 enr;
62 u32 dr;
63 u32 txd0r;
64 u32 drxr;
65 u32 sicr;
66 u32 txftr;
67 u32 rxftr;
68 u32 gpior;
69 u32 reserved0[19];
70 u32 txd1r;
71 u32 txd2r;
72 u32 txd3r;
73 u32 reserved1[5];
74 u32 lqspicfg;
75 u32 lqspists;
76};
77
78
79struct zynq_qspi_platdata {
80 struct zynq_qspi_regs *regs;
81 u32 frequency;
82 u32 speed_hz;
83};
84
85
86struct zynq_qspi_priv {
87 struct zynq_qspi_regs *regs;
88 u8 cs;
89 u8 mode;
90 u8 fifo_depth;
91 u32 freq;
92 const void *tx_buf;
93 void *rx_buf;
94 unsigned len;
95 int bytes_to_transfer;
96 int bytes_to_receive;
97 unsigned int is_inst;
98 unsigned cs_change:1;
99};
100
101static int zynq_qspi_ofdata_to_platdata(struct udevice *bus)
102{
103 struct zynq_qspi_platdata *plat = bus->platdata;
104 const void *blob = gd->fdt_blob;
105 int node = dev_of_offset(bus);
106
107 plat->regs = (struct zynq_qspi_regs *)fdtdec_get_addr(blob,
108 node, "reg");
109
110 return 0;
111}
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133static void zynq_qspi_init_hw(struct zynq_qspi_priv *priv)
134{
135 struct zynq_qspi_regs *regs = priv->regs;
136 u32 confr;
137
138
139 writel(~ZYNQ_QSPI_ENR_SPI_EN_MASK, ®s->enr);
140
141
142 writel(ZYNQ_QSPI_IXR_ALL_MASK, ®s->idr);
143
144
145 writel(ZYNQ_QSPI_TXFIFO_THRESHOLD, ®s->txftr);
146 writel(ZYNQ_QSPI_RXFIFO_THRESHOLD, ®s->rxftr);
147
148
149 while (readl(®s->isr) & ZYNQ_QSPI_IXR_RXNEMPTY_MASK)
150 readl(®s->drxr);
151
152
153 writel(ZYNQ_QSPI_IXR_ALL_MASK, ®s->isr);
154
155
156 confr = readl(®s->cr);
157 confr &= ~ZYNQ_QSPI_CR_MSA_MASK;
158 confr |= ZYNQ_QSPI_CR_IFMODE_MASK | ZYNQ_QSPI_CR_MCS_MASK |
159 ZYNQ_QSPI_CR_PCS_MASK | ZYNQ_QSPI_CR_FW_MASK |
160 ZYNQ_QSPI_CR_MSTREN_MASK;
161 writel(confr, ®s->cr);
162
163
164 confr = readl(®s->lqspicfg);
165 confr &= ~ZYNQ_QSPI_LQSPICFG_LQMODE_MASK;
166 writel(confr, ®s->lqspicfg);
167
168
169 writel(ZYNQ_QSPI_ENR_SPI_EN_MASK, ®s->enr);
170}
171
172static int zynq_qspi_probe(struct udevice *bus)
173{
174 struct zynq_qspi_platdata *plat = dev_get_platdata(bus);
175 struct zynq_qspi_priv *priv = dev_get_priv(bus);
176 struct clk clk;
177 unsigned long clock;
178 int ret;
179
180 priv->regs = plat->regs;
181 priv->fifo_depth = ZYNQ_QSPI_FIFO_DEPTH;
182
183 ret = clk_get_by_name(bus, "ref_clk", &clk);
184 if (ret < 0) {
185 dev_err(bus, "failed to get clock\n");
186 return ret;
187 }
188
189 clock = clk_get_rate(&clk);
190 if (IS_ERR_VALUE(clock)) {
191 dev_err(bus, "failed to get rate\n");
192 return clock;
193 }
194
195 ret = clk_enable(&clk);
196 if (ret && ret != -ENOSYS) {
197 dev_err(bus, "failed to enable clock\n");
198 return ret;
199 }
200
201
202 zynq_qspi_init_hw(priv);
203
204 plat->frequency = clock;
205 plat->speed_hz = plat->frequency / 2;
206
207 debug("%s: max-frequency=%d\n", __func__, plat->speed_hz);
208
209 return 0;
210}
211
212
213
214
215
216
217
218static void zynq_qspi_read_data(struct zynq_qspi_priv *priv, u32 data, u8 size)
219{
220 u8 byte3;
221
222 debug("%s: data 0x%04x rx_buf addr: 0x%08x size %d\n", __func__ ,
223 data, (unsigned)(priv->rx_buf), size);
224
225 if (priv->rx_buf) {
226 switch (size) {
227 case 1:
228 *((u8 *)priv->rx_buf) = data;
229 priv->rx_buf += 1;
230 break;
231 case 2:
232 *((u16 *)priv->rx_buf) = data;
233 priv->rx_buf += 2;
234 break;
235 case 3:
236 *((u16 *)priv->rx_buf) = data;
237 priv->rx_buf += 2;
238 byte3 = (u8)(data >> 16);
239 *((u8 *)priv->rx_buf) = byte3;
240 priv->rx_buf += 1;
241 break;
242 case 4:
243
244 memcpy(priv->rx_buf, &data, size);
245 priv->rx_buf += 4;
246 break;
247 default:
248
249 break;
250 }
251 }
252 priv->bytes_to_receive -= size;
253 if (priv->bytes_to_receive < 0)
254 priv->bytes_to_receive = 0;
255}
256
257
258
259
260
261
262
263static void zynq_qspi_write_data(struct zynq_qspi_priv *priv,
264 u32 *data, u8 size)
265{
266 if (priv->tx_buf) {
267 switch (size) {
268 case 1:
269 *data = *((u8 *)priv->tx_buf);
270 priv->tx_buf += 1;
271 *data |= 0xFFFFFF00;
272 break;
273 case 2:
274 *data = *((u16 *)priv->tx_buf);
275 priv->tx_buf += 2;
276 *data |= 0xFFFF0000;
277 break;
278 case 3:
279 *data = *((u16 *)priv->tx_buf);
280 priv->tx_buf += 2;
281 *data |= (*((u8 *)priv->tx_buf) << 16);
282 priv->tx_buf += 1;
283 *data |= 0xFF000000;
284 break;
285 case 4:
286
287 memcpy(data, priv->tx_buf, size);
288 priv->tx_buf += 4;
289 break;
290 default:
291
292 break;
293 }
294 } else {
295 *data = 0;
296 }
297
298 debug("%s: data 0x%08x tx_buf addr: 0x%08x size %d\n", __func__,
299 *data, (u32)priv->tx_buf, size);
300
301 priv->bytes_to_transfer -= size;
302 if (priv->bytes_to_transfer < 0)
303 priv->bytes_to_transfer = 0;
304}
305
306
307
308
309
310
311static void zynq_qspi_chipselect(struct zynq_qspi_priv *priv, int is_on)
312{
313 u32 confr;
314 struct zynq_qspi_regs *regs = priv->regs;
315
316 confr = readl(®s->cr);
317
318 if (is_on) {
319
320 confr &= ~ZYNQ_QSPI_CR_SS_MASK;
321 confr |= (~(1 << priv->cs) << ZYNQ_QSPI_CR_SS_SHIFT) &
322 ZYNQ_QSPI_CR_SS_MASK;
323 } else
324
325 confr |= ZYNQ_QSPI_CR_SS_MASK;
326
327 writel(confr, ®s->cr);
328}
329
330
331
332
333
334
335static void zynq_qspi_fill_tx_fifo(struct zynq_qspi_priv *priv, u32 size)
336{
337 u32 data = 0;
338 u32 fifocount = 0;
339 unsigned len, offset;
340 struct zynq_qspi_regs *regs = priv->regs;
341 static const unsigned offsets[4] = {
342 ZYNQ_QSPI_TXD_00_00_OFFSET, ZYNQ_QSPI_TXD_00_01_OFFSET,
343 ZYNQ_QSPI_TXD_00_10_OFFSET, ZYNQ_QSPI_TXD_00_11_OFFSET };
344
345 while ((fifocount < size) &&
346 (priv->bytes_to_transfer > 0)) {
347 if (priv->bytes_to_transfer >= 4) {
348 if (priv->tx_buf) {
349 memcpy(&data, priv->tx_buf, 4);
350 priv->tx_buf += 4;
351 } else {
352 data = 0;
353 }
354 writel(data, ®s->txd0r);
355 priv->bytes_to_transfer -= 4;
356 fifocount++;
357 } else {
358
359 if (!(readl(®s->isr)
360 & ZYNQ_QSPI_IXR_TXOW_MASK) &&
361 !priv->rx_buf)
362 return;
363 len = priv->bytes_to_transfer;
364 zynq_qspi_write_data(priv, &data, len);
365 offset = (priv->rx_buf) ? offsets[0] : offsets[len];
366 writel(data, ®s->cr + (offset / 4));
367 }
368 }
369}
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385static int zynq_qspi_irq_poll(struct zynq_qspi_priv *priv)
386{
387 struct zynq_qspi_regs *regs = priv->regs;
388 u32 rxindex = 0;
389 u32 rxcount;
390 u32 status, timeout;
391
392
393 timeout = get_timer(0);
394 do {
395 status = readl(®s->isr);
396 } while ((status == 0) &&
397 (get_timer(timeout) < ZYNQ_QSPI_WAIT));
398
399 if (status == 0) {
400 printf("zynq_qspi_irq_poll: Timeout!\n");
401 return -ETIMEDOUT;
402 }
403
404 writel(status, ®s->isr);
405
406
407 writel(ZYNQ_QSPI_IXR_ALL_MASK, ®s->idr);
408 if ((status & ZYNQ_QSPI_IXR_TXOW_MASK) ||
409 (status & ZYNQ_QSPI_IXR_RXNEMPTY_MASK)) {
410
411
412
413
414
415 rxcount = priv->bytes_to_receive - priv->bytes_to_transfer;
416 rxcount = (rxcount % 4) ? ((rxcount/4)+1) : (rxcount/4);
417 while ((rxindex < rxcount) &&
418 (rxindex < ZYNQ_QSPI_RXFIFO_THRESHOLD)) {
419
420 u32 data;
421 data = readl(®s->drxr);
422
423 if (priv->bytes_to_receive >= 4) {
424 if (priv->rx_buf) {
425 memcpy(priv->rx_buf, &data, 4);
426 priv->rx_buf += 4;
427 }
428 priv->bytes_to_receive -= 4;
429 } else {
430 zynq_qspi_read_data(priv, data,
431 priv->bytes_to_receive);
432 }
433 rxindex++;
434 }
435
436 if (priv->bytes_to_transfer) {
437
438 zynq_qspi_fill_tx_fifo(priv,
439 ZYNQ_QSPI_RXFIFO_THRESHOLD);
440
441 writel(ZYNQ_QSPI_IXR_ALL_MASK, ®s->ier);
442 } else {
443
444
445
446
447 if (!priv->bytes_to_receive) {
448
449 writel(ZYNQ_QSPI_IXR_ALL_MASK,
450 ®s->idr);
451 return 1;
452 }
453 }
454 }
455
456 return 0;
457}
458
459
460
461
462
463
464
465
466
467
468static int zynq_qspi_start_transfer(struct zynq_qspi_priv *priv)
469{
470 u32 data = 0;
471 struct zynq_qspi_regs *regs = priv->regs;
472
473 debug("%s: qspi: 0x%08x transfer: 0x%08x len: %d\n", __func__,
474 (u32)priv, (u32)priv, priv->len);
475
476 priv->bytes_to_transfer = priv->len;
477 priv->bytes_to_receive = priv->len;
478
479 if (priv->len < 4)
480 zynq_qspi_fill_tx_fifo(priv, priv->len);
481 else
482 zynq_qspi_fill_tx_fifo(priv, priv->fifo_depth);
483
484 writel(ZYNQ_QSPI_IXR_ALL_MASK, ®s->ier);
485
486
487 do {
488 data = zynq_qspi_irq_poll(priv);
489 } while (data == 0);
490
491 return (priv->len) - (priv->bytes_to_transfer);
492}
493
494static int zynq_qspi_transfer(struct zynq_qspi_priv *priv)
495{
496 unsigned cs_change = 1;
497 int status = 0;
498
499 while (1) {
500
501 if (cs_change)
502 zynq_qspi_chipselect(priv, 1);
503
504 cs_change = priv->cs_change;
505
506 if (!priv->tx_buf && !priv->rx_buf && priv->len) {
507 status = -1;
508 break;
509 }
510
511
512 if (priv->len) {
513 status = zynq_qspi_start_transfer(priv);
514 priv->is_inst = 0;
515 }
516
517 if (status != priv->len) {
518 if (status > 0)
519 status = -EMSGSIZE;
520 debug("zynq_qspi_transfer:%d len:%d\n",
521 status, priv->len);
522 break;
523 }
524 status = 0;
525
526 if (cs_change)
527
528 zynq_qspi_chipselect(priv, 0);
529
530 break;
531 }
532
533 return status;
534}
535
536static int zynq_qspi_claim_bus(struct udevice *dev)
537{
538 struct udevice *bus = dev->parent;
539 struct zynq_qspi_priv *priv = dev_get_priv(bus);
540 struct zynq_qspi_regs *regs = priv->regs;
541
542 writel(ZYNQ_QSPI_ENR_SPI_EN_MASK, ®s->enr);
543
544 return 0;
545}
546
547static int zynq_qspi_release_bus(struct udevice *dev)
548{
549 struct udevice *bus = dev->parent;
550 struct zynq_qspi_priv *priv = dev_get_priv(bus);
551 struct zynq_qspi_regs *regs = priv->regs;
552
553 writel(~ZYNQ_QSPI_ENR_SPI_EN_MASK, ®s->enr);
554
555 return 0;
556}
557
558static int zynq_qspi_xfer(struct udevice *dev, unsigned int bitlen,
559 const void *dout, void *din, unsigned long flags)
560{
561 struct udevice *bus = dev->parent;
562 struct zynq_qspi_priv *priv = dev_get_priv(bus);
563 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
564
565 priv->cs = slave_plat->cs;
566 priv->tx_buf = dout;
567 priv->rx_buf = din;
568 priv->len = bitlen / 8;
569
570 debug("zynq_qspi_xfer: bus:%i cs:%i bitlen:%i len:%i flags:%lx\n",
571 bus->seq, slave_plat->cs, bitlen, priv->len, flags);
572
573
574
575
576
577
578 if (dout && flags & SPI_XFER_BEGIN)
579 priv->is_inst = 1;
580 else
581 priv->is_inst = 0;
582
583 if (flags & SPI_XFER_END)
584 priv->cs_change = 1;
585 else
586 priv->cs_change = 0;
587
588 zynq_qspi_transfer(priv);
589
590 return 0;
591}
592
593static int zynq_qspi_set_speed(struct udevice *bus, uint speed)
594{
595 struct zynq_qspi_platdata *plat = bus->platdata;
596 struct zynq_qspi_priv *priv = dev_get_priv(bus);
597 struct zynq_qspi_regs *regs = priv->regs;
598 uint32_t confr;
599 u8 baud_rate_val = 0;
600
601 if (speed > plat->frequency)
602 speed = plat->frequency;
603
604
605 confr = readl(®s->cr);
606 if (speed == 0) {
607
608 baud_rate_val = 0x2;
609 } else if (plat->speed_hz != speed) {
610 while ((baud_rate_val < ZYNQ_QSPI_CR_BAUD_MAX) &&
611 ((plat->frequency /
612 (2 << baud_rate_val)) > speed))
613 baud_rate_val++;
614
615 plat->speed_hz = speed / (2 << baud_rate_val);
616 }
617 confr &= ~ZYNQ_QSPI_CR_BAUD_MASK;
618 confr |= (baud_rate_val << ZYNQ_QSPI_CR_BAUD_SHIFT);
619
620 writel(confr, ®s->cr);
621 priv->freq = speed;
622
623 debug("%s: regs=%p, speed=%d\n", __func__, priv->regs, priv->freq);
624
625 return 0;
626}
627
628static int zynq_qspi_set_mode(struct udevice *bus, uint mode)
629{
630 struct zynq_qspi_priv *priv = dev_get_priv(bus);
631 struct zynq_qspi_regs *regs = priv->regs;
632 uint32_t confr;
633
634
635 confr = readl(®s->cr);
636 confr &= ~(ZYNQ_QSPI_CR_CPHA_MASK | ZYNQ_QSPI_CR_CPOL_MASK);
637
638 if (mode & SPI_CPHA)
639 confr |= ZYNQ_QSPI_CR_CPHA_MASK;
640 if (mode & SPI_CPOL)
641 confr |= ZYNQ_QSPI_CR_CPOL_MASK;
642
643 writel(confr, ®s->cr);
644 priv->mode = mode;
645
646 debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode);
647
648 return 0;
649}
650
651static const struct dm_spi_ops zynq_qspi_ops = {
652 .claim_bus = zynq_qspi_claim_bus,
653 .release_bus = zynq_qspi_release_bus,
654 .xfer = zynq_qspi_xfer,
655 .set_speed = zynq_qspi_set_speed,
656 .set_mode = zynq_qspi_set_mode,
657};
658
659static const struct udevice_id zynq_qspi_ids[] = {
660 { .compatible = "xlnx,zynq-qspi-1.0" },
661 { }
662};
663
664U_BOOT_DRIVER(zynq_qspi) = {
665 .name = "zynq_qspi",
666 .id = UCLASS_SPI,
667 .of_match = zynq_qspi_ids,
668 .ops = &zynq_qspi_ops,
669 .ofdata_to_platdata = zynq_qspi_ofdata_to_platdata,
670 .platdata_auto_alloc_size = sizeof(struct zynq_qspi_platdata),
671 .priv_auto_alloc_size = sizeof(struct zynq_qspi_priv),
672 .probe = zynq_qspi_probe,
673};
674