1
2
3
4
5
6
7
8
9#include <common.h>
10#include <malloc.h>
11#include <dm.h>
12#include <ubi_uboot.h>
13#include <spi.h>
14#include <spi_flash.h>
15#include <asm/io.h>
16#include <asm/arch/hardware.h>
17#include <asm/arch/sys_proto.h>
18#include <asm/arch/clk.h>
19#include "../mtd/spi/sf_internal.h"
20
21DECLARE_GLOBAL_DATA_PTR;
22
23
24#define ZYNQ_QSPI_TXD_00_00_OFFSET 0x1C
25#define ZYNQ_QSPI_TXD_00_01_OFFSET 0x80
26#define ZYNQ_QSPI_TXD_00_10_OFFSET 0x84
27#define ZYNQ_QSPI_TXD_00_11_OFFSET 0x88
28
29
30
31
32
33
34
35#define ZYNQ_QSPI_CONFIG_IFMODE_MASK (1 << 31)
36#define ZYNQ_QSPI_CONFIG_MSA_MASK (1 << 15)
37#define ZYNQ_QSPI_CONFIG_MCS_MASK (1 << 14)
38#define ZYNQ_QSPI_CONFIG_PCS_MASK (1 << 10)
39#define ZYNQ_QSPI_CONFIG_FW_MASK (0x3 << 6)
40#define ZYNQ_QSPI_CONFIG_BAUD_DIV_MASK (0x7 << 3)
41#define ZYNQ_QSPI_CONFIG_MSTREN_MASK (1 << 0)
42#define ZYNQ_QSPI_CONFIG_MANSRT_MASK 0x00010000
43#define ZYNQ_QSPI_CONFIG_CPHA_MASK 0x00000004
44#define ZYNQ_QSPI_CONFIG_CPOL_MASK 0x00000002
45#define ZYNQ_QSPI_CONFIG_SSCTRL_MASK 0x00003C00
46
47
48
49
50
51
52#define ZYNQ_QSPI_IXR_TXNFULL_MASK 0x00000004
53#define ZYNQ_QSPI_IXR_TXFULL_MASK 0x00000008
54#define ZYNQ_QSPI_IXR_RXNEMTY_MASK 0x00000010
55#define ZYNQ_QSPI_IXR_ALL_MASK (ZYNQ_QSPI_IXR_TXNFULL_MASK | \
56 ZYNQ_QSPI_IXR_RXNEMTY_MASK)
57
58
59
60
61
62
63#define ZYNQ_QSPI_ENABLE_ENABLE_MASK 0x00000001
64
65
66
67
68
69
70
71#define ZYNQ_QSPI_LCFG_TWO_MEM_MASK 0x40000000
72#define ZYNQ_QSPI_LCFG_SEP_BUS_MASK 0x20000000
73#define ZYNQ_QSPI_LCFG_U_PAGE 0x10000000
74
75#define ZYNQ_QSPI_LCFG_DUMMY_SHIFT 8
76
77#define ZYNQ_QSPI_FR_QOUT_CODE 0x6B
78#define ZYNQ_QSPI_FR_DUALIO_CODE 0xBB
79
80
81
82
83
84#define MODEBITS (SPI_CPOL | SPI_CPHA)
85
86
87#define ZYNQ_QSPI_QUEUE_STOPPED 0
88#define ZYNQ_QSPI_QUEUE_RUNNING 1
89#define ZYNQ_QSPI_RXFIFO_THRESHOLD 32
90#define ZYNQ_QSPI_FIFO_DEPTH 63
91
92
93#define ZYNQ_QSPI_MIO_NUM_QSPI0 6
94#define ZYNQ_QSPI_MIO_NUM_QSPI1 5
95#define ZYNQ_QSPI_MIO_NUM_QSPI1_CS 1
96
97
98#define ZYNQ_QSPI_MIO_NUM_QSPI0_DIO 4
99#define ZYNQ_QSPI_MIO_NUM_QSPI1_DIO 3
100#define ZYNQ_QSPI_MIO_NUM_QSPI1_CS_DIO 1
101
102#define ZYNQ_QSPI_MAX_BAUD_RATE 0x7
103#define ZYNQ_QSPI_DEFAULT_BAUD_RATE 0x2
104
105
106struct zynq_qspi_regs {
107 u32 confr;
108 u32 isr;
109 u32 ier;
110 u32 idisr;
111 u32 imaskr;
112 u32 enbr;
113 u32 dr;
114 u32 txd0r;
115 u32 drxr;
116 u32 sicr;
117 u32 txftr;
118 u32 rxftr;
119 u32 gpior;
120 u32 reserved0[19];
121 u32 txd1r;
122 u32 txd2r;
123 u32 txd3r;
124 u32 reserved1[5];
125 u32 lcr;
126 u32 reserved2[22];
127 u32 midr;
128};
129
130
131struct zynq_qspi_platdata {
132 struct zynq_qspi_regs *regs;
133 u32 frequency;
134 u32 speed_hz;
135 u32 is_dual;
136 u32 tx_rx_mode;
137};
138
139struct zynq_qspi_priv {
140 struct zynq_qspi_regs *regs;
141 u8 mode;
142 u32 freq;
143 const void *txbuf;
144 void *rxbuf;
145 unsigned len;
146 int bytes_to_transfer;
147 int bytes_to_receive;
148 unsigned int is_inst;
149 unsigned int is_dual;
150 unsigned int is_dio;
151 unsigned int u_page;
152 unsigned cs_change:1;
153};
154
155static int zynq_qspi_ofdata_to_platdata(struct udevice *bus)
156{
157 struct zynq_qspi_platdata *plat = bus->platdata;
158 int is_dual;
159 u32 mode = 0;
160 int offset;
161 u32 value;
162
163 debug("%s\n", __func__);
164 plat->regs = (struct zynq_qspi_regs *)ZYNQ_QSPI_BASEADDR;
165
166 is_dual = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus), "is-dual", -1);
167 if (is_dual < 0)
168 plat->is_dual = SF_SINGLE_FLASH;
169 else if (is_dual == 1)
170 plat->is_dual = SF_DUAL_PARALLEL_FLASH;
171 else
172 if (fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus),
173 "is-stacked", -1) < 0)
174 plat->is_dual = SF_SINGLE_FLASH;
175 else
176 plat->is_dual = SF_DUAL_STACKED_FLASH;
177
178 offset = fdt_first_subnode(gd->fdt_blob, dev_of_offset(bus));
179
180 value = fdtdec_get_uint(gd->fdt_blob, offset, "spi-rx-bus-width", 1);
181 switch (value) {
182 case 1:
183 break;
184 case 2:
185 mode |= SPI_RX_DUAL;
186 break;
187 case 4:
188 mode |= SPI_RX_QUAD;
189 break;
190 default:
191 printf("Invalid spi-rx-bus-width %d\n", value);
192 break;
193 }
194
195 value = fdtdec_get_uint(gd->fdt_blob, offset, "spi-tx-bus-width", 1);
196 switch (value) {
197 case 1:
198 break;
199 case 2:
200 mode |= SPI_TX_DUAL;
201 break;
202 case 4:
203 mode |= SPI_TX_QUAD;
204 break;
205 default:
206 printf("Invalid spi-tx-bus-width %d\n", value);
207 break;
208 }
209
210 plat->tx_rx_mode = mode;
211
212 plat->frequency = 166666666;
213 plat->speed_hz = plat->frequency / 2;
214
215 return 0;
216}
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239static void zynq_qspi_init_hw(struct zynq_qspi_priv *priv)
240{
241 u32 config_reg;
242 struct zynq_qspi_regs *regs = priv->regs;
243
244 writel(~ZYNQ_QSPI_ENABLE_ENABLE_MASK, ®s->enbr);
245 writel(0x7F, ®s->idisr);
246
247
248 writel(0x0, ®s->lcr);
249
250
251 writel(0x1, ®s->txftr);
252 writel(ZYNQ_QSPI_RXFIFO_THRESHOLD, ®s->rxftr);
253
254
255 while (readl(®s->isr) & ZYNQ_QSPI_IXR_RXNEMTY_MASK)
256 readl(®s->drxr);
257
258 debug("%s is_dual:0x%x, is_dio:0x%x\n", __func__, priv->is_dual, priv->is_dio);
259
260 writel(0x7F, ®s->isr);
261 config_reg = readl(®s->confr);
262 config_reg &= ~ZYNQ_QSPI_CONFIG_MSA_MASK;
263 config_reg |= ZYNQ_QSPI_CONFIG_IFMODE_MASK |
264 ZYNQ_QSPI_CONFIG_MCS_MASK | ZYNQ_QSPI_CONFIG_PCS_MASK |
265 ZYNQ_QSPI_CONFIG_FW_MASK | ZYNQ_QSPI_CONFIG_MSTREN_MASK;
266 if (priv->is_dual == SF_DUAL_STACKED_FLASH)
267 config_reg |= 0x10;
268 writel(config_reg, ®s->confr);
269
270 if (priv->is_dual == SF_DUAL_PARALLEL_FLASH) {
271 if (priv->is_dio == SF_DUALIO_FLASH)
272
273 writel((ZYNQ_QSPI_LCFG_TWO_MEM_MASK |
274 ZYNQ_QSPI_LCFG_SEP_BUS_MASK |
275 (1 << ZYNQ_QSPI_LCFG_DUMMY_SHIFT) |
276 ZYNQ_QSPI_FR_DUALIO_CODE),
277 ®s->lcr);
278 else
279
280 writel((ZYNQ_QSPI_LCFG_TWO_MEM_MASK |
281 ZYNQ_QSPI_LCFG_SEP_BUS_MASK |
282 (1 << ZYNQ_QSPI_LCFG_DUMMY_SHIFT) |
283 ZYNQ_QSPI_FR_QOUT_CODE),
284 ®s->lcr);
285 } else if (priv->is_dual == SF_DUAL_STACKED_FLASH) {
286 if (priv->is_dio == SF_DUALIO_FLASH)
287
288
289 writel((ZYNQ_QSPI_LCFG_TWO_MEM_MASK |
290 (1 << ZYNQ_QSPI_LCFG_DUMMY_SHIFT) |
291 ZYNQ_QSPI_FR_DUALIO_CODE),
292 ®s->lcr);
293 else
294
295
296 writel((ZYNQ_QSPI_LCFG_TWO_MEM_MASK |
297 (1 << ZYNQ_QSPI_LCFG_DUMMY_SHIFT) |
298 ZYNQ_QSPI_FR_QOUT_CODE),
299 ®s->lcr);
300 }
301 writel(ZYNQ_QSPI_ENABLE_ENABLE_MASK, ®s->enbr);
302}
303
304static int zynq_qspi_child_pre_probe(struct udevice *bus)
305{
306 struct spi_slave *slave = dev_get_parent_priv(bus);
307 struct zynq_qspi_priv *priv = dev_get_priv(bus->parent);
308 struct zynq_qspi_platdata *plat = dev_get_platdata(bus->parent);
309
310 slave->option = priv->is_dual;
311 slave->dio = priv->is_dio;
312 slave->mode = plat->tx_rx_mode;
313
314 return 0;
315}
316
317static int zynq_qspi_probe(struct udevice *bus)
318{
319 struct zynq_qspi_platdata *plat = dev_get_platdata(bus);
320 struct zynq_qspi_priv *priv = dev_get_priv(bus);
321
322 debug("zynq_qspi_probe: bus:%p, priv:%p \n", bus, priv);
323
324 priv->regs = plat->regs;
325 priv->is_dual = plat->is_dual;
326
327 if (priv->is_dual == -1) {
328 debug("%s: No QSPI device detected based on MIO settings\n",
329 __func__);
330 return -1;
331 }
332
333
334 zynq_qspi_init_hw(priv);
335
336 return 0;
337}
338
339static int zynq_qspi_set_speed(struct udevice *bus, uint speed)
340{
341 struct zynq_qspi_platdata *plat = bus->platdata;
342 struct zynq_qspi_priv *priv = dev_get_priv(bus);
343 struct zynq_qspi_regs *regs = priv->regs;
344 uint32_t confr;
345 u8 baud_rate_val = 0;
346
347 debug("%s\n", __func__);
348 if (speed > plat->frequency)
349 speed = plat->frequency;
350
351
352 confr = readl(®s->confr);
353 if (speed == 0) {
354
355 baud_rate_val = 0x2;
356 } else if (plat->speed_hz != speed) {
357 while ((baud_rate_val < 8) &&
358 ((plat->frequency /
359 (2 << baud_rate_val)) > speed))
360 baud_rate_val++;
361
362 if (baud_rate_val > ZYNQ_QSPI_MAX_BAUD_RATE)
363 baud_rate_val = ZYNQ_QSPI_DEFAULT_BAUD_RATE;
364
365 plat->speed_hz = speed / (2 << baud_rate_val);
366 }
367 confr &= ~ZYNQ_QSPI_CONFIG_BAUD_DIV_MASK;
368 confr |= (baud_rate_val << 3);
369
370 writel(confr, ®s->confr);
371 priv->freq = speed;
372
373 debug("zynq_spi_set_speed: regs=%p, mode=%d\n", priv->regs, priv->freq);
374
375 return 0;
376}
377
378static int zynq_qspi_set_mode(struct udevice *bus, uint mode)
379{
380 struct zynq_qspi_priv *priv = dev_get_priv(bus);
381 struct zynq_qspi_regs *regs = priv->regs;
382 uint32_t confr;
383
384 debug("%s\n", __func__);
385
386 confr = readl(®s->confr);
387 confr &= ~(ZYNQ_QSPI_CONFIG_CPHA_MASK | ZYNQ_QSPI_CONFIG_CPOL_MASK);
388
389 if (priv->mode & SPI_CPHA)
390 confr |= ZYNQ_QSPI_CONFIG_CPHA_MASK;
391 if (priv->mode & SPI_CPOL)
392 confr |= ZYNQ_QSPI_CONFIG_CPOL_MASK;
393
394 writel(confr, ®s->confr);
395 priv->mode = mode;
396
397 debug("zynq_spi_set_mode: regs=%p, mode=%d\n", priv->regs, priv->mode);
398
399 return 0;
400}
401
402
403
404
405
406
407
408static void zynq_qspi_copy_read_data(struct zynq_qspi_priv *priv, u32 data, u8 size)
409{
410 u8 byte3;
411
412 debug("%s: data 0x%04x rxbuf addr: 0x%08x size %d\n", __func__ ,
413 data, (unsigned)(priv->rxbuf), size);
414
415 if (priv->rxbuf) {
416 switch (size) {
417 case 1:
418 *((u8 *)priv->rxbuf) = data;
419 priv->rxbuf += 1;
420 break;
421 case 2:
422 *((u8 *)priv->rxbuf) = data;
423 priv->rxbuf += 1;
424 *((u8 *)priv->rxbuf) = (u8)(data >> 8);
425 priv->rxbuf += 1;
426 break;
427 case 3:
428 *((u8 *)priv->rxbuf) = data;
429 priv->rxbuf += 1;
430 *((u8 *)priv->rxbuf) = (u8)(data >> 8);
431 priv->rxbuf += 1;
432 byte3 = (u8)(data >> 16);
433 *((u8 *)priv->rxbuf) = byte3;
434 priv->rxbuf += 1;
435 break;
436 case 4:
437
438 memcpy(priv->rxbuf, &data, size);
439 priv->rxbuf += 4;
440 break;
441 default:
442
443 break;
444 }
445 }
446 priv->bytes_to_receive -= size;
447 if (priv->bytes_to_receive < 0)
448 priv->bytes_to_receive = 0;
449}
450
451
452
453
454
455
456
457static void zynq_qspi_copy_write_data(struct zynq_qspi_priv *priv,
458 u32 *data, u8 size)
459{
460 if (priv->txbuf) {
461 switch (size) {
462 case 1:
463 *data = *((u8 *)priv->txbuf);
464 priv->txbuf += 1;
465 *data |= 0xFFFFFF00;
466 break;
467 case 2:
468 *data = *((u8 *)priv->txbuf);
469 priv->txbuf += 1;
470 *data |= (*((u8 *)priv->txbuf) << 8);
471 priv->txbuf += 1;
472 *data |= 0xFFFF0000;
473 break;
474 case 3:
475 *data = *((u8 *)priv->txbuf);
476 priv->txbuf += 1;
477 *data |= (*((u8 *)priv->txbuf) << 8);
478 priv->txbuf += 1;
479 *data |= (*((u8 *)priv->txbuf) << 16);
480 priv->txbuf += 1;
481 *data |= 0xFF000000;
482 break;
483 case 4:
484
485 memcpy(data, priv->txbuf, size);
486 priv->txbuf += 4;
487 break;
488 default:
489
490 break;
491 }
492 } else {
493 *data = 0;
494 }
495
496 debug("%s: data 0x%08x txbuf addr: 0x%08x size %d\n", __func__,
497 *data, (u32)priv->txbuf, size);
498
499 priv->bytes_to_transfer -= size;
500 if (priv->bytes_to_transfer < 0)
501 priv->bytes_to_transfer = 0;
502}
503
504
505
506
507
508
509static void zynq_qspi_chipselect(struct zynq_qspi_priv *priv, int is_on)
510{
511 u32 config_reg;
512 struct zynq_qspi_regs *regs = priv->regs;
513
514 debug("%s: is_on: %d\n", __func__, is_on);
515
516 config_reg = readl(®s->confr);
517
518 if (is_on) {
519
520 config_reg &= ~ZYNQ_QSPI_CONFIG_SSCTRL_MASK;
521 config_reg |= (((~(0x0001 << 0)) << 10) &
522 ZYNQ_QSPI_CONFIG_SSCTRL_MASK);
523 } else
524
525 config_reg |= ZYNQ_QSPI_CONFIG_SSCTRL_MASK;
526
527 writel(config_reg, ®s->confr);
528}
529
530
531
532
533
534static void zynq_qspi_fill_tx_fifo(struct zynq_qspi_priv *priv, u32 size)
535{
536 u32 data = 0;
537 u32 fifocount = 0;
538 unsigned len, offset;
539 struct zynq_qspi_regs *regs = priv->regs;
540 static const unsigned offsets[4] = {
541 ZYNQ_QSPI_TXD_00_00_OFFSET, ZYNQ_QSPI_TXD_00_01_OFFSET,
542 ZYNQ_QSPI_TXD_00_10_OFFSET, ZYNQ_QSPI_TXD_00_11_OFFSET };
543
544 while ((fifocount < size) &&
545 (priv->bytes_to_transfer > 0)) {
546 if (priv->bytes_to_transfer >= 4) {
547 if (priv->txbuf) {
548 memcpy(&data, priv->txbuf, 4);
549 priv->txbuf += 4;
550 } else {
551 data = 0;
552 }
553 writel(data, ®s->txd0r);
554 priv->bytes_to_transfer -= 4;
555 fifocount++;
556 } else {
557
558 if (!(readl(®s->isr)
559 & ZYNQ_QSPI_IXR_TXNFULL_MASK) &&
560 !priv->rxbuf)
561 return;
562 len = priv->bytes_to_transfer;
563 zynq_qspi_copy_write_data(priv, &data, len);
564 offset = (priv->rxbuf) ? offsets[0] : offsets[len];
565 writel(data, ®s->confr + (offset / 4));
566 }
567 }
568}
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584static int zynq_qspi_irq_poll(struct zynq_qspi_priv *priv)
585{
586 int max_loop;
587 u32 intr_status;
588 u32 rxindex = 0;
589 u32 rxcount;
590 struct zynq_qspi_regs *regs = priv->regs;
591
592 debug("%s: zqspi: 0x%08x\n", __func__, (u32)priv);
593
594
595 max_loop = 0;
596 do {
597 intr_status = readl(®s->isr);
598 max_loop++;
599 } while ((intr_status == 0) && (max_loop < 100000));
600
601 if (intr_status == 0) {
602 debug("%s: Timeout\n", __func__);
603 return 0;
604 }
605
606 writel(intr_status, ®s->isr);
607
608
609 writel(ZYNQ_QSPI_IXR_ALL_MASK, ®s->idisr);
610 if ((intr_status & ZYNQ_QSPI_IXR_TXNFULL_MASK) ||
611 (intr_status & ZYNQ_QSPI_IXR_RXNEMTY_MASK)) {
612
613
614
615
616
617 rxcount = priv->bytes_to_receive - priv->bytes_to_transfer;
618 rxcount = (rxcount % 4) ? ((rxcount/4)+1) : (rxcount/4);
619 while ((rxindex < rxcount) &&
620 (rxindex < ZYNQ_QSPI_RXFIFO_THRESHOLD)) {
621
622 u32 data;
623 data = readl(®s->drxr);
624
625 if (priv->bytes_to_receive >= 4) {
626 if (priv->rxbuf) {
627 memcpy(priv->rxbuf, &data, 4);
628 priv->rxbuf += 4;
629 }
630 priv->bytes_to_receive -= 4;
631 } else {
632 zynq_qspi_copy_read_data(priv, data,
633 priv->bytes_to_receive);
634 }
635 rxindex++;
636 }
637
638 if (priv->bytes_to_transfer) {
639
640 zynq_qspi_fill_tx_fifo(priv,
641 ZYNQ_QSPI_RXFIFO_THRESHOLD);
642
643 writel(ZYNQ_QSPI_IXR_ALL_MASK, ®s->ier);
644 } else {
645
646
647
648
649 if (!priv->bytes_to_receive) {
650
651 writel(ZYNQ_QSPI_IXR_ALL_MASK,
652 ®s->idisr);
653 return 1;
654 }
655 }
656 }
657
658 return 0;
659}
660
661
662
663
664
665
666
667
668
669
670
671
672static int zynq_qspi_start_transfer(struct zynq_qspi_priv *priv)
673{
674 static u8 current_u_page;
675 u32 data = 0;
676 struct zynq_qspi_regs *regs = priv->regs;
677
678 debug("%s: qspi: 0x%08x transfer: 0x%08x len: %d\n", __func__,
679 (u32)priv, (u32)priv, priv->len);
680
681 priv->bytes_to_transfer = priv->len;
682 priv->bytes_to_receive = priv->len;
683
684 if (priv->is_inst && (priv->is_dual == SF_DUAL_STACKED_FLASH) &&
685 (current_u_page != priv->u_page)) {
686 if (priv->u_page) {
687 if (priv->is_dio == SF_DUALIO_FLASH)
688 writel((ZYNQ_QSPI_LCFG_TWO_MEM_MASK |
689 ZYNQ_QSPI_LCFG_U_PAGE |
690 (1 << ZYNQ_QSPI_LCFG_DUMMY_SHIFT) |
691 ZYNQ_QSPI_FR_DUALIO_CODE),
692 ®s->lcr);
693 else
694
695
696
697 writel((ZYNQ_QSPI_LCFG_TWO_MEM_MASK |
698 ZYNQ_QSPI_LCFG_U_PAGE |
699 (1 << ZYNQ_QSPI_LCFG_DUMMY_SHIFT) |
700 ZYNQ_QSPI_FR_QOUT_CODE),
701 ®s->lcr);
702 } else {
703 if (priv->is_dio == SF_DUALIO_FLASH)
704 writel((ZYNQ_QSPI_LCFG_TWO_MEM_MASK |
705 (1 << ZYNQ_QSPI_LCFG_DUMMY_SHIFT) |
706 ZYNQ_QSPI_FR_DUALIO_CODE),
707 ®s->lcr);
708 else
709
710
711
712 writel((ZYNQ_QSPI_LCFG_TWO_MEM_MASK |
713 (1 << ZYNQ_QSPI_LCFG_DUMMY_SHIFT) |
714 ZYNQ_QSPI_FR_QOUT_CODE),
715 ®s->lcr);
716 }
717 current_u_page = priv->u_page;
718 }
719
720 if (priv->len < 4)
721 zynq_qspi_fill_tx_fifo(priv, priv->len);
722 else
723 zynq_qspi_fill_tx_fifo(priv, ZYNQ_QSPI_FIFO_DEPTH);
724
725 writel(ZYNQ_QSPI_IXR_ALL_MASK, ®s->ier);
726
727
728
729 do {
730 data = zynq_qspi_irq_poll(priv);
731 } while (data == 0);
732
733 return (priv->len) - (priv->bytes_to_transfer);
734}
735
736static int zynq_qspi_transfer(struct zynq_qspi_priv *priv)
737{
738 unsigned cs_change = 1;
739 int status = 0;
740
741 debug("%s\n", __func__);
742
743 while (1) {
744
745
746 if (cs_change)
747 zynq_qspi_chipselect(priv, 1);
748
749 cs_change = priv->cs_change;
750
751 if (!priv->txbuf && !priv->rxbuf && priv->len) {
752 status = -1;
753 break;
754 }
755
756
757 if (priv->len) {
758 status = zynq_qspi_start_transfer(priv);
759 priv->is_inst = 0;
760 }
761
762 if (status != priv->len) {
763 if (status > 0)
764 status = -EMSGSIZE;
765 debug("zynq_qspi_transfer:%d len:%d\n", status, priv->len);
766 break;
767 }
768 status = 0;
769
770 if (cs_change)
771
772 zynq_qspi_chipselect(priv, 0);
773
774 break;
775 }
776
777 return 0;
778}
779
780static int zynq_qspi_claim_bus(struct udevice *dev)
781{
782 struct udevice *bus = dev->parent;
783 struct zynq_qspi_priv *priv = dev_get_priv(bus);
784 struct zynq_qspi_regs *regs = priv->regs;
785
786 debug("%s\n", __func__);
787 writel(ZYNQ_QSPI_ENABLE_ENABLE_MASK, ®s->enbr);
788
789 return 0;
790}
791
792static int zynq_qspi_release_bus(struct udevice *dev)
793{
794 struct udevice *bus = dev->parent;
795 struct zynq_qspi_priv *priv = dev_get_priv(bus);
796 struct zynq_qspi_regs *regs = priv->regs;
797
798 debug("%s\n", __func__);
799 writel(~ZYNQ_QSPI_ENABLE_ENABLE_MASK, ®s->enbr);
800
801 return 0;
802}
803
804static int zynq_qspi_xfer(struct udevice *dev, unsigned int bitlen, const void *dout,
805 void *din, unsigned long flags)
806{
807 struct udevice *bus = dev->parent;
808 struct zynq_qspi_priv *priv = dev_get_priv(bus);
809
810 debug("%s", __func__);
811 debug("%s: slave: 0x%08x bitlen: %d dout: 0x%08x ", __func__,
812 (u32)priv, bitlen, (u32)dout);
813 debug("din: 0x%08x flags: 0x%lx\n", (u32)din, flags);
814
815 priv->txbuf = dout;
816 priv->rxbuf = din;
817 priv->len = bitlen / 8;
818
819
820
821
822
823
824 if (dout && flags & SPI_XFER_BEGIN)
825 priv->is_inst = 1;
826 else
827 priv->is_inst = 0;
828
829 if (flags & SPI_XFER_END)
830 priv->cs_change = 1;
831 else
832 priv->cs_change = 0;
833
834 if (flags & SPI_XFER_U_PAGE)
835 priv->u_page = 1;
836 else
837 priv->u_page = 0;
838
839 zynq_qspi_transfer(priv);
840
841 return 0;
842}
843
844static const struct dm_spi_ops zynq_qspi_ops = {
845 .claim_bus = zynq_qspi_claim_bus,
846 .release_bus = zynq_qspi_release_bus,
847 .xfer = zynq_qspi_xfer,
848 .set_speed = zynq_qspi_set_speed,
849 .set_mode = zynq_qspi_set_mode,
850};
851
852static const struct udevice_id zynq_qspi_ids[] = {
853 { .compatible = "xlnx,zynq-qspi-1.0" },
854 { }
855};
856
857U_BOOT_DRIVER(zynq_qspi) = {
858 .name = "zynq_qspi",
859 .id = UCLASS_SPI,
860 .of_match = zynq_qspi_ids,
861 .ops = &zynq_qspi_ops,
862 .ofdata_to_platdata = zynq_qspi_ofdata_to_platdata,
863 .platdata_auto_alloc_size = sizeof(struct zynq_qspi_platdata),
864 .priv_auto_alloc_size = sizeof(struct zynq_qspi_priv),
865 .probe = zynq_qspi_probe,
866 .child_pre_probe = zynq_qspi_child_pre_probe,
867};
868