2 * (C) Copyright 2013 Xilinx, Inc.
3 * (C) Copyright 2015 Jagan Teki <jteki@openedev.com>
5 * Xilinx Zynq Quad-SPI(QSPI) controller driver (master mode only)
7 * SPDX-License-Identifier: GPL-2.0+
16 DECLARE_GLOBAL_DATA_PTR
;
18 /* zynq qspi register bit masks ZYNQ_QSPI_<REG>_<BIT>_MASK */
19 #define ZYNQ_QSPI_CR_IFMODE_MASK (1 << 31) /* Flash intrface mode*/
20 #define ZYNQ_QSPI_CR_MSA_MASK (1 << 15) /* Manual start enb */
21 #define ZYNQ_QSPI_CR_MCS_MASK (1 << 14) /* Manual chip select */
22 #define ZYNQ_QSPI_CR_PCS_MASK (1 << 10) /* Peri chip select */
23 #define ZYNQ_QSPI_CR_FW_MASK (0x3 << 6) /* FIFO width */
24 #define ZYNQ_QSPI_CR_SS_MASK (0xF << 10) /* Slave Select */
25 #define ZYNQ_QSPI_CR_BAUD_MASK (0x7 << 3) /* Baud rate div */
26 #define ZYNQ_QSPI_CR_CPHA_MASK (1 << 2) /* Clock phase */
27 #define ZYNQ_QSPI_CR_CPOL_MASK (1 << 1) /* Clock polarity */
28 #define ZYNQ_QSPI_CR_MSTREN_MASK (1 << 0) /* Mode select */
29 #define ZYNQ_QSPI_IXR_RXNEMPTY_MASK (1 << 4) /* RX_FIFO_not_empty */
30 #define ZYNQ_QSPI_IXR_TXOW_MASK (1 << 2) /* TX_FIFO_not_full */
31 #define ZYNQ_QSPI_IXR_ALL_MASK 0x7F /* All IXR bits */
32 #define ZYNQ_QSPI_ENR_SPI_EN_MASK (1 << 0) /* SPI Enable */
34 /* zynq qspi Transmit Data Register */
35 #define ZYNQ_QSPI_TXD_00_00_OFFSET 0x1C /* Transmit 4-byte inst */
36 #define ZYNQ_QSPI_TXD_00_01_OFFSET 0x80 /* Transmit 1-byte inst */
37 #define ZYNQ_QSPI_TXD_00_10_OFFSET 0x84 /* Transmit 2-byte inst */
38 #define ZYNQ_QSPI_TXD_00_11_OFFSET 0x88 /* Transmit 3-byte inst */
40 #define ZYNQ_QSPI_TXFIFO_THRESHOLD 1 /* Tx FIFO threshold level*/
41 #define ZYNQ_QSPI_RXFIFO_THRESHOLD 32 /* Rx FIFO threshold level */
43 #define ZYNQ_QSPI_CR_BAUD_MAX 8 /* Baud rate divisor max val */
44 #define ZYNQ_QSPI_CR_BAUD_SHIFT 3 /* Baud rate divisor shift */
45 #define ZYNQ_QSPI_CR_SS_SHIFT 10 /* Slave select shift */
47 #define ZYNQ_QSPI_FIFO_DEPTH 63
48 #ifndef CONFIG_SYS_ZYNQ_QSPI_WAIT
49 #define CONFIG_SYS_ZYNQ_QSPI_WAIT CONFIG_SYS_HZ/100 /* 10 ms */
52 /* zynq qspi register set */
53 struct zynq_qspi_regs
{
73 /* zynq qspi platform data */
74 struct zynq_qspi_platdata
{
75 struct zynq_qspi_regs
*regs
;
76 u32 frequency
; /* input frequency */
81 struct zynq_qspi_priv
{
82 struct zynq_qspi_regs
*regs
;
86 u32 freq
; /* required frequency */
90 int bytes_to_transfer
;
96 static int zynq_qspi_ofdata_to_platdata(struct udevice
*bus
)
98 struct zynq_qspi_platdata
*plat
= bus
->platdata
;
99 const void *blob
= gd
->fdt_blob
;
100 int node
= bus
->of_offset
;
102 plat
->regs
= (struct zynq_qspi_regs
*)fdtdec_get_addr(blob
,
105 /* FIXME: Use 166MHz as a suitable default */
106 plat
->frequency
= fdtdec_get_int(blob
, node
, "spi-max-frequency",
108 plat
->speed_hz
= plat
->frequency
/ 2;
110 debug("%s: regs=%p max-frequency=%d\n", __func__
,
111 plat
->regs
, plat
->frequency
);
116 static void zynq_qspi_init_hw(struct zynq_qspi_priv
*priv
)
118 struct zynq_qspi_regs
*regs
= priv
->regs
;
122 writel(~ZYNQ_QSPI_ENR_SPI_EN_MASK
, ®s
->enr
);
124 /* Disable Interrupts */
125 writel(ZYNQ_QSPI_IXR_ALL_MASK
, ®s
->idr
);
127 /* Clear the TX and RX threshold reg */
128 writel(ZYNQ_QSPI_TXFIFO_THRESHOLD
, ®s
->txftr
);
129 writel(ZYNQ_QSPI_RXFIFO_THRESHOLD
, ®s
->rxftr
);
131 /* Clear the RX FIFO */
132 while (readl(®s
->isr
) & ZYNQ_QSPI_IXR_RXNEMPTY_MASK
)
135 /* Clear Interrupts */
136 writel(ZYNQ_QSPI_IXR_ALL_MASK
, ®s
->isr
);
138 /* Manual slave select and Auto start */
139 confr
= readl(®s
->cr
);
140 confr
&= ~ZYNQ_QSPI_CR_MSA_MASK
;
141 confr
|= ZYNQ_QSPI_CR_IFMODE_MASK
| ZYNQ_QSPI_CR_MCS_MASK
|
142 ZYNQ_QSPI_CR_PCS_MASK
| ZYNQ_QSPI_CR_FW_MASK
|
143 ZYNQ_QSPI_CR_MSTREN_MASK
;
144 writel(confr
, ®s
->cr
);
147 writel(ZYNQ_QSPI_ENR_SPI_EN_MASK
, ®s
->enr
);
150 static int zynq_qspi_probe(struct udevice
*bus
)
152 struct zynq_qspi_platdata
*plat
= dev_get_platdata(bus
);
153 struct zynq_qspi_priv
*priv
= dev_get_priv(bus
);
155 priv
->regs
= plat
->regs
;
156 priv
->fifo_depth
= ZYNQ_QSPI_FIFO_DEPTH
;
158 /* init the zynq spi hw */
159 zynq_qspi_init_hw(priv
);
165 * zynq_qspi_read_data - Copy data to RX buffer
166 * @zqspi: Pointer to the zynq_qspi structure
167 * @data: The 32 bit variable where data is stored
168 * @size: Number of bytes to be copied from data to RX buffer
170 static void zynq_qspi_read_data(struct zynq_qspi_priv
*priv
, u32 data
, u8 size
)
174 debug("%s: data 0x%04x rx_buf addr: 0x%08x size %d\n", __func__
,
175 data
, (unsigned)(priv
->rx_buf
), size
);
180 *((u8
*)priv
->rx_buf
) = data
;
184 *((u16
*)priv
->rx_buf
) = data
;
188 *((u16
*)priv
->rx_buf
) = data
;
190 byte3
= (u8
)(data
>> 16);
191 *((u8
*)priv
->rx_buf
) = byte3
;
195 /* Can not assume word aligned buffer */
196 memcpy(priv
->rx_buf
, &data
, size
);
200 /* This will never execute */
204 priv
->bytes_to_receive
-= size
;
205 if (priv
->bytes_to_receive
< 0)
206 priv
->bytes_to_receive
= 0;
210 * zynq_qspi_write_data - Copy data from TX buffer
211 * @zqspi: Pointer to the zynq_qspi structure
212 * @data: Pointer to the 32 bit variable where data is to be copied
213 * @size: Number of bytes to be copied from TX buffer to data
215 static void zynq_qspi_write_data(struct zynq_qspi_priv
*priv
,
221 *data
= *((u8
*)priv
->tx_buf
);
226 *data
= *((u16
*)priv
->tx_buf
);
231 *data
= *((u16
*)priv
->tx_buf
);
233 *data
|= (*((u8
*)priv
->tx_buf
) << 16);
238 /* Can not assume word aligned buffer */
239 memcpy(data
, priv
->tx_buf
, size
);
243 /* This will never execute */
250 debug("%s: data 0x%08x tx_buf addr: 0x%08x size %d\n", __func__
,
251 *data
, (u32
)priv
->tx_buf
, size
);
253 priv
->bytes_to_transfer
-= size
;
254 if (priv
->bytes_to_transfer
< 0)
255 priv
->bytes_to_transfer
= 0;
258 static void zynq_qspi_chipselect(struct zynq_qspi_priv
*priv
, int is_on
)
261 struct zynq_qspi_regs
*regs
= priv
->regs
;
263 confr
= readl(®s
->cr
);
266 /* Select the slave */
267 confr
&= ~ZYNQ_QSPI_CR_SS_MASK
;
268 confr
|= (~(1 << priv
->cs
) << ZYNQ_QSPI_CR_SS_SHIFT
) &
269 ZYNQ_QSPI_CR_SS_MASK
;
271 /* Deselect the slave */
272 confr
|= ZYNQ_QSPI_CR_SS_MASK
;
274 writel(confr
, ®s
->cr
);
278 * zynq_qspi_fill_tx_fifo - Fills the TX FIFO with as many bytes as possible
279 * @zqspi: Pointer to the zynq_qspi structure
281 static void zynq_qspi_fill_tx_fifo(struct zynq_qspi_priv
*priv
, u32 size
)
285 unsigned len
, offset
;
286 struct zynq_qspi_regs
*regs
= priv
->regs
;
287 static const unsigned offsets
[4] = {
288 ZYNQ_QSPI_TXD_00_00_OFFSET
, ZYNQ_QSPI_TXD_00_01_OFFSET
,
289 ZYNQ_QSPI_TXD_00_10_OFFSET
, ZYNQ_QSPI_TXD_00_11_OFFSET
};
291 while ((fifocount
< size
) &&
292 (priv
->bytes_to_transfer
> 0)) {
293 if (priv
->bytes_to_transfer
>= 4) {
295 memcpy(&data
, priv
->tx_buf
, 4);
300 writel(data
, ®s
->txd0r
);
301 priv
->bytes_to_transfer
-= 4;
304 /* Write TXD1, TXD2, TXD3 only if TxFIFO is empty. */
305 if (!(readl(®s
->isr
)
306 & ZYNQ_QSPI_IXR_TXOW_MASK
) &&
309 len
= priv
->bytes_to_transfer
;
310 zynq_qspi_write_data(priv
, &data
, len
);
311 offset
= (priv
->rx_buf
) ? offsets
[0] : offsets
[len
];
312 writel(data
, ®s
->cr
+ (offset
/ 4));
318 * zynq_qspi_irq_poll - Interrupt service routine of the QSPI controller
319 * @zqspi: Pointer to the zynq_qspi structure
321 * This function handles TX empty and Mode Fault interrupts only.
322 * On TX empty interrupt this function reads the received data from RX FIFO and
323 * fills the TX FIFO if there is any data remaining to be transferred.
324 * On Mode Fault interrupt this function indicates that transfer is completed,
325 * the SPI subsystem will identify the error as the remaining bytes to be
326 * transferred is non-zero.
328 * returns: 0 for poll timeout
329 * 1 transfer operation complete
331 static int zynq_qspi_irq_poll(struct zynq_qspi_priv
*priv
)
333 struct zynq_qspi_regs
*regs
= priv
->regs
;
338 /* Poll until any of the interrupt status bits are set */
339 timeout
= get_timer(0);
341 status
= readl(®s
->isr
);
342 } while ((status
== 0) &&
343 (get_timer(timeout
) < CONFIG_SYS_ZYNQ_QSPI_WAIT
));
346 printf("zynq_qspi_irq_poll: Timeout!\n");
350 writel(status
, ®s
->isr
);
352 /* Disable all interrupts */
353 writel(ZYNQ_QSPI_IXR_ALL_MASK
, ®s
->idr
);
354 if ((status
& ZYNQ_QSPI_IXR_TXOW_MASK
) ||
355 (status
& ZYNQ_QSPI_IXR_RXNEMPTY_MASK
)) {
357 * This bit is set when Tx FIFO has < THRESHOLD entries. We have
358 * the THRESHOLD value set to 1, so this bit indicates Tx FIFO
361 rxcount
= priv
->bytes_to_receive
- priv
->bytes_to_transfer
;
362 rxcount
= (rxcount
% 4) ? ((rxcount
/4)+1) : (rxcount
/4);
363 while ((rxindex
< rxcount
) &&
364 (rxindex
< ZYNQ_QSPI_RXFIFO_THRESHOLD
)) {
365 /* Read out the data from the RX FIFO */
367 data
= readl(®s
->drxr
);
369 if (priv
->bytes_to_receive
>= 4) {
371 memcpy(priv
->rx_buf
, &data
, 4);
374 priv
->bytes_to_receive
-= 4;
376 zynq_qspi_read_data(priv
, data
,
377 priv
->bytes_to_receive
);
382 if (priv
->bytes_to_transfer
) {
383 /* There is more data to send */
384 zynq_qspi_fill_tx_fifo(priv
,
385 ZYNQ_QSPI_RXFIFO_THRESHOLD
);
387 writel(ZYNQ_QSPI_IXR_ALL_MASK
, ®s
->ier
);
390 * If transfer and receive is completed then only send
393 if (!priv
->bytes_to_receive
) {
394 /* return operation complete */
395 writel(ZYNQ_QSPI_IXR_ALL_MASK
,
406 * zynq_qspi_start_transfer - Initiates the QSPI transfer
407 * @qspi: Pointer to the spi_device structure
408 * @transfer: Pointer to the spi_transfer structure which provide information
409 * about next transfer parameters
411 * This function fills the TX FIFO, starts the QSPI transfer, and waits for the
412 * transfer to be completed.
414 * returns: Number of bytes transferred in the last transfer
416 static int zynq_qspi_start_transfer(struct zynq_qspi_priv
*priv
)
419 struct zynq_qspi_regs
*regs
= priv
->regs
;
421 debug("%s: qspi: 0x%08x transfer: 0x%08x len: %d\n", __func__
,
422 (u32
)priv
, (u32
)priv
, priv
->len
);
424 priv
->bytes_to_transfer
= priv
->len
;
425 priv
->bytes_to_receive
= priv
->len
;
428 zynq_qspi_fill_tx_fifo(priv
, priv
->len
);
430 zynq_qspi_fill_tx_fifo(priv
, priv
->fifo_depth
);
432 writel(ZYNQ_QSPI_IXR_ALL_MASK
, ®s
->ier
);
433 /* Start the transfer by enabling manual start bit */
435 /* wait for completion */
437 data
= zynq_qspi_irq_poll(priv
);
440 return (priv
->len
) - (priv
->bytes_to_transfer
);
443 static int zynq_qspi_transfer(struct zynq_qspi_priv
*priv
)
445 unsigned cs_change
= 1;
449 /* Select the chip if required */
451 zynq_qspi_chipselect(priv
, 1);
453 cs_change
= priv
->cs_change
;
455 if (!priv
->tx_buf
&& !priv
->rx_buf
&& priv
->len
) {
460 /* Request the transfer */
462 status
= zynq_qspi_start_transfer(priv
);
466 if (status
!= priv
->len
) {
469 debug("zynq_qspi_transfer:%d len:%d\n",
476 /* Deselect the chip */
477 zynq_qspi_chipselect(priv
, 0);
485 static int zynq_qspi_claim_bus(struct udevice
*dev
)
487 struct udevice
*bus
= dev
->parent
;
488 struct zynq_qspi_priv
*priv
= dev_get_priv(bus
);
489 struct zynq_qspi_regs
*regs
= priv
->regs
;
491 writel(ZYNQ_QSPI_ENR_SPI_EN_MASK
, ®s
->enr
);
496 static int zynq_qspi_release_bus(struct udevice
*dev
)
498 struct udevice
*bus
= dev
->parent
;
499 struct zynq_qspi_priv
*priv
= dev_get_priv(bus
);
500 struct zynq_qspi_regs
*regs
= priv
->regs
;
502 writel(~ZYNQ_QSPI_ENR_SPI_EN_MASK
, ®s
->enr
);
507 static int zynq_qspi_xfer(struct udevice
*dev
, unsigned int bitlen
,
508 const void *dout
, void *din
, unsigned long flags
)
510 struct udevice
*bus
= dev
->parent
;
511 struct zynq_qspi_priv
*priv
= dev_get_priv(bus
);
512 struct dm_spi_slave_platdata
*slave_plat
= dev_get_parent_platdata(dev
);
514 priv
->cs
= slave_plat
->cs
;
517 priv
->len
= bitlen
/ 8;
519 debug("spi_xfer: bus:%i cs:%i bitlen:%i len:%i flags:%lx\n",
520 bus
->seq
, slave_plat
->cs
, bitlen
, priv
->len
, flags
);
524 * Assume that the beginning of a transfer with bits to
525 * transmit must contain a device command.
527 if (dout
&& flags
& SPI_XFER_BEGIN
)
532 if (flags
& SPI_XFER_END
)
537 zynq_qspi_transfer(priv
);
542 static int zynq_qspi_set_speed(struct udevice
*bus
, uint speed
)
544 struct zynq_qspi_platdata
*plat
= bus
->platdata
;
545 struct zynq_qspi_priv
*priv
= dev_get_priv(bus
);
546 struct zynq_qspi_regs
*regs
= priv
->regs
;
548 u8 baud_rate_val
= 0;
550 if (speed
> plat
->frequency
)
551 speed
= plat
->frequency
;
553 /* Set the clock frequency */
554 confr
= readl(®s
->cr
);
556 /* Set baudrate x8, if the freq is 0 */
558 } else if (plat
->speed_hz
!= speed
) {
559 while ((baud_rate_val
< ZYNQ_QSPI_CR_BAUD_MAX
) &&
561 (2 << baud_rate_val
)) > speed
))
564 plat
->speed_hz
= speed
/ (2 << baud_rate_val
);
566 confr
&= ~ZYNQ_QSPI_CR_BAUD_MASK
;
567 confr
|= (baud_rate_val
<< ZYNQ_QSPI_CR_BAUD_SHIFT
);
569 writel(confr
, ®s
->cr
);
572 debug("zynq_spi_set_speed: regs=%p, mode=%d\n", priv
->regs
, priv
->freq
);
577 static int zynq_qspi_set_mode(struct udevice
*bus
, uint mode
)
579 struct zynq_qspi_priv
*priv
= dev_get_priv(bus
);
580 struct zynq_qspi_regs
*regs
= priv
->regs
;
583 /* Set the SPI Clock phase and polarities */
584 confr
= readl(®s
->cr
);
585 confr
&= ~(ZYNQ_QSPI_CR_CPHA_MASK
| ZYNQ_QSPI_CR_CPOL_MASK
);
587 if (priv
->mode
& SPI_CPHA
)
588 confr
|= ZYNQ_QSPI_CR_CPHA_MASK
;
589 if (priv
->mode
& SPI_CPOL
)
590 confr
|= ZYNQ_QSPI_CR_CPOL_MASK
;
592 writel(confr
, ®s
->cr
);
595 debug("zynq_spi_set_mode: regs=%p, mode=%d\n", priv
->regs
, priv
->mode
);
600 static const struct dm_spi_ops zynq_qspi_ops
= {
601 .claim_bus
= zynq_qspi_claim_bus
,
602 .release_bus
= zynq_qspi_release_bus
,
603 .xfer
= zynq_qspi_xfer
,
604 .set_speed
= zynq_qspi_set_speed
,
605 .set_mode
= zynq_qspi_set_mode
,
608 static const struct udevice_id zynq_qspi_ids
[] = {
609 { .compatible
= "xlnx,zynq-qspi-1.0" },
613 U_BOOT_DRIVER(zynq_qspi
) = {
616 .of_match
= zynq_qspi_ids
,
617 .ops
= &zynq_qspi_ops
,
618 .ofdata_to_platdata
= zynq_qspi_ofdata_to_platdata
,
619 .platdata_auto_alloc_size
= sizeof(struct zynq_qspi_platdata
),
620 .priv_auto_alloc_size
= sizeof(struct zynq_qspi_priv
),
621 .probe
= zynq_qspi_probe
,