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 BIT(31) /* Flash intrface mode*/
20 #define ZYNQ_QSPI_CR_MSA_MASK BIT(15) /* Manual start enb */
21 #define ZYNQ_QSPI_CR_MCS_MASK BIT(14) /* Manual chip select */
22 #define ZYNQ_QSPI_CR_PCS_MASK BIT(10) /* Peri chip select */
23 #define ZYNQ_QSPI_CR_FW_MASK GENMASK(7, 6) /* FIFO width */
24 #define ZYNQ_QSPI_CR_SS_MASK GENMASK(13, 10) /* Slave Select */
25 #define ZYNQ_QSPI_CR_BAUD_MASK GENMASK(5, 3) /* Baud rate div */
26 #define ZYNQ_QSPI_CR_CPHA_MASK BIT(2) /* Clock phase */
27 #define ZYNQ_QSPI_CR_CPOL_MASK BIT(1) /* Clock polarity */
28 #define ZYNQ_QSPI_CR_MSTREN_MASK BIT(0) /* Mode select */
29 #define ZYNQ_QSPI_IXR_RXNEMPTY_MASK BIT(4) /* RX_FIFO_not_empty */
30 #define ZYNQ_QSPI_IXR_TXOW_MASK BIT(2) /* TX_FIFO_not_full */
31 #define ZYNQ_QSPI_IXR_ALL_MASK GENMASK(6, 0) /* All IXR bits */
32 #define ZYNQ_QSPI_ENR_SPI_EN_MASK BIT(0) /* SPI Enable */
33 #define ZYNQ_QSPI_LQSPICFG_LQMODE_MASK BIT(31) /* Linear QSPI Mode */
35 /* zynq qspi Transmit Data Register */
36 #define ZYNQ_QSPI_TXD_00_00_OFFSET 0x1C /* Transmit 4-byte inst */
37 #define ZYNQ_QSPI_TXD_00_01_OFFSET 0x80 /* Transmit 1-byte inst */
38 #define ZYNQ_QSPI_TXD_00_10_OFFSET 0x84 /* Transmit 2-byte inst */
39 #define ZYNQ_QSPI_TXD_00_11_OFFSET 0x88 /* Transmit 3-byte inst */
41 #define ZYNQ_QSPI_TXFIFO_THRESHOLD 1 /* Tx FIFO threshold level*/
42 #define ZYNQ_QSPI_RXFIFO_THRESHOLD 32 /* Rx FIFO threshold level */
44 #define ZYNQ_QSPI_CR_BAUD_MAX 8 /* Baud rate divisor max val */
45 #define ZYNQ_QSPI_CR_BAUD_SHIFT 3 /* Baud rate divisor shift */
46 #define ZYNQ_QSPI_CR_SS_SHIFT 10 /* Slave select shift */
48 #define ZYNQ_QSPI_FIFO_DEPTH 63
49 #ifndef CONFIG_SYS_ZYNQ_QSPI_WAIT
50 #define CONFIG_SYS_ZYNQ_QSPI_WAIT CONFIG_SYS_HZ/100 /* 10 ms */
53 /* zynq qspi register set */
54 struct zynq_qspi_regs {
73 u32 lqspicfg; /* 0xA0 */
74 u32 lqspists; /* 0xA4 */
77 /* zynq qspi platform data */
78 struct zynq_qspi_platdata {
79 struct zynq_qspi_regs *regs;
80 u32 frequency; /* input frequency */
85 struct zynq_qspi_priv {
86 struct zynq_qspi_regs *regs;
90 u32 freq; /* required frequency */
94 int bytes_to_transfer;
100 static int zynq_qspi_ofdata_to_platdata(struct udevice *bus)
102 struct zynq_qspi_platdata *plat = bus->platdata;
103 const void *blob = gd->fdt_blob;
104 int node = dev_of_offset(bus);
106 plat->regs = (struct zynq_qspi_regs *)fdtdec_get_addr(blob,
109 /* FIXME: Use 166MHz as a suitable default */
110 plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
112 plat->speed_hz = plat->frequency / 2;
114 debug("%s: regs=%p max-frequency=%d\n", __func__,
115 plat->regs, plat->frequency);
120 static void zynq_qspi_init_hw(struct zynq_qspi_priv *priv)
122 struct zynq_qspi_regs *regs = priv->regs;
126 writel(~ZYNQ_QSPI_ENR_SPI_EN_MASK, ®s->enr);
128 /* Disable Interrupts */
129 writel(ZYNQ_QSPI_IXR_ALL_MASK, ®s->idr);
131 /* Clear the TX and RX threshold reg */
132 writel(ZYNQ_QSPI_TXFIFO_THRESHOLD, ®s->txftr);
133 writel(ZYNQ_QSPI_RXFIFO_THRESHOLD, ®s->rxftr);
135 /* Clear the RX FIFO */
136 while (readl(®s->isr) & ZYNQ_QSPI_IXR_RXNEMPTY_MASK)
139 /* Clear Interrupts */
140 writel(ZYNQ_QSPI_IXR_ALL_MASK, ®s->isr);
142 /* Manual slave select and Auto start */
143 confr = readl(®s->cr);
144 confr &= ~ZYNQ_QSPI_CR_MSA_MASK;
145 confr |= ZYNQ_QSPI_CR_IFMODE_MASK | ZYNQ_QSPI_CR_MCS_MASK |
146 ZYNQ_QSPI_CR_PCS_MASK | ZYNQ_QSPI_CR_FW_MASK |
147 ZYNQ_QSPI_CR_MSTREN_MASK;
148 writel(confr, ®s->cr);
150 /* Disable the LQSPI feature */
151 confr = readl(®s->lqspicfg);
152 confr &= ~ZYNQ_QSPI_LQSPICFG_LQMODE_MASK;
153 writel(confr, ®s->lqspicfg);
156 writel(ZYNQ_QSPI_ENR_SPI_EN_MASK, ®s->enr);
159 static int zynq_qspi_probe(struct udevice *bus)
161 struct zynq_qspi_platdata *plat = dev_get_platdata(bus);
162 struct zynq_qspi_priv *priv = dev_get_priv(bus);
164 priv->regs = plat->regs;
165 priv->fifo_depth = ZYNQ_QSPI_FIFO_DEPTH;
167 /* init the zynq spi hw */
168 zynq_qspi_init_hw(priv);
174 * zynq_qspi_read_data - Copy data to RX buffer
175 * @zqspi: Pointer to the zynq_qspi structure
176 * @data: The 32 bit variable where data is stored
177 * @size: Number of bytes to be copied from data to RX buffer
179 static void zynq_qspi_read_data(struct zynq_qspi_priv *priv, u32 data, u8 size)
183 debug("%s: data 0x%04x rx_buf addr: 0x%08x size %d\n", __func__ ,
184 data, (unsigned)(priv->rx_buf), size);
189 *((u8 *)priv->rx_buf) = data;
193 *((u16 *)priv->rx_buf) = data;
197 *((u16 *)priv->rx_buf) = data;
199 byte3 = (u8)(data >> 16);
200 *((u8 *)priv->rx_buf) = byte3;
204 /* Can not assume word aligned buffer */
205 memcpy(priv->rx_buf, &data, size);
209 /* This will never execute */
213 priv->bytes_to_receive -= size;
214 if (priv->bytes_to_receive < 0)
215 priv->bytes_to_receive = 0;
219 * zynq_qspi_write_data - Copy data from TX buffer
220 * @zqspi: Pointer to the zynq_qspi structure
221 * @data: Pointer to the 32 bit variable where data is to be copied
222 * @size: Number of bytes to be copied from TX buffer to data
224 static void zynq_qspi_write_data(struct zynq_qspi_priv *priv,
230 *data = *((u8 *)priv->tx_buf);
235 *data = *((u16 *)priv->tx_buf);
240 *data = *((u16 *)priv->tx_buf);
242 *data |= (*((u8 *)priv->tx_buf) << 16);
247 /* Can not assume word aligned buffer */
248 memcpy(data, priv->tx_buf, size);
252 /* This will never execute */
259 debug("%s: data 0x%08x tx_buf addr: 0x%08x size %d\n", __func__,
260 *data, (u32)priv->tx_buf, size);
262 priv->bytes_to_transfer -= size;
263 if (priv->bytes_to_transfer < 0)
264 priv->bytes_to_transfer = 0;
267 static void zynq_qspi_chipselect(struct zynq_qspi_priv *priv, int is_on)
270 struct zynq_qspi_regs *regs = priv->regs;
272 confr = readl(®s->cr);
275 /* Select the slave */
276 confr &= ~ZYNQ_QSPI_CR_SS_MASK;
277 confr |= (~(1 << priv->cs) << ZYNQ_QSPI_CR_SS_SHIFT) &
278 ZYNQ_QSPI_CR_SS_MASK;
280 /* Deselect the slave */
281 confr |= ZYNQ_QSPI_CR_SS_MASK;
283 writel(confr, ®s->cr);
287 * zynq_qspi_fill_tx_fifo - Fills the TX FIFO with as many bytes as possible
288 * @zqspi: Pointer to the zynq_qspi structure
290 static void zynq_qspi_fill_tx_fifo(struct zynq_qspi_priv *priv, u32 size)
294 unsigned len, offset;
295 struct zynq_qspi_regs *regs = priv->regs;
296 static const unsigned offsets[4] = {
297 ZYNQ_QSPI_TXD_00_00_OFFSET, ZYNQ_QSPI_TXD_00_01_OFFSET,
298 ZYNQ_QSPI_TXD_00_10_OFFSET, ZYNQ_QSPI_TXD_00_11_OFFSET };
300 while ((fifocount < size) &&
301 (priv->bytes_to_transfer > 0)) {
302 if (priv->bytes_to_transfer >= 4) {
304 memcpy(&data, priv->tx_buf, 4);
309 writel(data, ®s->txd0r);
310 priv->bytes_to_transfer -= 4;
313 /* Write TXD1, TXD2, TXD3 only if TxFIFO is empty. */
314 if (!(readl(®s->isr)
315 & ZYNQ_QSPI_IXR_TXOW_MASK) &&
318 len = priv->bytes_to_transfer;
319 zynq_qspi_write_data(priv, &data, len);
320 offset = (priv->rx_buf) ? offsets[0] : offsets[len];
321 writel(data, ®s->cr + (offset / 4));
327 * zynq_qspi_irq_poll - Interrupt service routine of the QSPI controller
328 * @zqspi: Pointer to the zynq_qspi structure
330 * This function handles TX empty and Mode Fault interrupts only.
331 * On TX empty interrupt this function reads the received data from RX FIFO and
332 * fills the TX FIFO if there is any data remaining to be transferred.
333 * On Mode Fault interrupt this function indicates that transfer is completed,
334 * the SPI subsystem will identify the error as the remaining bytes to be
335 * transferred is non-zero.
337 * returns: 0 for poll timeout
338 * 1 transfer operation complete
340 static int zynq_qspi_irq_poll(struct zynq_qspi_priv *priv)
342 struct zynq_qspi_regs *regs = priv->regs;
347 /* Poll until any of the interrupt status bits are set */
348 timeout = get_timer(0);
350 status = readl(®s->isr);
351 } while ((status == 0) &&
352 (get_timer(timeout) < CONFIG_SYS_ZYNQ_QSPI_WAIT));
355 printf("zynq_qspi_irq_poll: Timeout!\n");
359 writel(status, ®s->isr);
361 /* Disable all interrupts */
362 writel(ZYNQ_QSPI_IXR_ALL_MASK, ®s->idr);
363 if ((status & ZYNQ_QSPI_IXR_TXOW_MASK) ||
364 (status & ZYNQ_QSPI_IXR_RXNEMPTY_MASK)) {
366 * This bit is set when Tx FIFO has < THRESHOLD entries. We have
367 * the THRESHOLD value set to 1, so this bit indicates Tx FIFO
370 rxcount = priv->bytes_to_receive - priv->bytes_to_transfer;
371 rxcount = (rxcount % 4) ? ((rxcount/4)+1) : (rxcount/4);
372 while ((rxindex < rxcount) &&
373 (rxindex < ZYNQ_QSPI_RXFIFO_THRESHOLD)) {
374 /* Read out the data from the RX FIFO */
376 data = readl(®s->drxr);
378 if (priv->bytes_to_receive >= 4) {
380 memcpy(priv->rx_buf, &data, 4);
383 priv->bytes_to_receive -= 4;
385 zynq_qspi_read_data(priv, data,
386 priv->bytes_to_receive);
391 if (priv->bytes_to_transfer) {
392 /* There is more data to send */
393 zynq_qspi_fill_tx_fifo(priv,
394 ZYNQ_QSPI_RXFIFO_THRESHOLD);
396 writel(ZYNQ_QSPI_IXR_ALL_MASK, ®s->ier);
399 * If transfer and receive is completed then only send
402 if (!priv->bytes_to_receive) {
403 /* return operation complete */
404 writel(ZYNQ_QSPI_IXR_ALL_MASK,
415 * zynq_qspi_start_transfer - Initiates the QSPI transfer
416 * @qspi: Pointer to the spi_device structure
417 * @transfer: Pointer to the spi_transfer structure which provide information
418 * about next transfer parameters
420 * This function fills the TX FIFO, starts the QSPI transfer, and waits for the
421 * transfer to be completed.
423 * returns: Number of bytes transferred in the last transfer
425 static int zynq_qspi_start_transfer(struct zynq_qspi_priv *priv)
428 struct zynq_qspi_regs *regs = priv->regs;
430 debug("%s: qspi: 0x%08x transfer: 0x%08x len: %d\n", __func__,
431 (u32)priv, (u32)priv, priv->len);
433 priv->bytes_to_transfer = priv->len;
434 priv->bytes_to_receive = priv->len;
437 zynq_qspi_fill_tx_fifo(priv, priv->len);
439 zynq_qspi_fill_tx_fifo(priv, priv->fifo_depth);
441 writel(ZYNQ_QSPI_IXR_ALL_MASK, ®s->ier);
443 /* wait for completion */
445 data = zynq_qspi_irq_poll(priv);
448 return (priv->len) - (priv->bytes_to_transfer);
451 static int zynq_qspi_transfer(struct zynq_qspi_priv *priv)
453 unsigned cs_change = 1;
457 /* Select the chip if required */
459 zynq_qspi_chipselect(priv, 1);
461 cs_change = priv->cs_change;
463 if (!priv->tx_buf && !priv->rx_buf && priv->len) {
468 /* Request the transfer */
470 status = zynq_qspi_start_transfer(priv);
474 if (status != priv->len) {
477 debug("zynq_qspi_transfer:%d len:%d\n",
484 /* Deselect the chip */
485 zynq_qspi_chipselect(priv, 0);
493 static int zynq_qspi_claim_bus(struct udevice *dev)
495 struct udevice *bus = dev->parent;
496 struct zynq_qspi_priv *priv = dev_get_priv(bus);
497 struct zynq_qspi_regs *regs = priv->regs;
499 writel(ZYNQ_QSPI_ENR_SPI_EN_MASK, ®s->enr);
504 static int zynq_qspi_release_bus(struct udevice *dev)
506 struct udevice *bus = dev->parent;
507 struct zynq_qspi_priv *priv = dev_get_priv(bus);
508 struct zynq_qspi_regs *regs = priv->regs;
510 writel(~ZYNQ_QSPI_ENR_SPI_EN_MASK, ®s->enr);
515 static int zynq_qspi_xfer(struct udevice *dev, unsigned int bitlen,
516 const void *dout, void *din, unsigned long flags)
518 struct udevice *bus = dev->parent;
519 struct zynq_qspi_priv *priv = dev_get_priv(bus);
520 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
522 priv->cs = slave_plat->cs;
525 priv->len = bitlen / 8;
527 debug("zynq_qspi_xfer: bus:%i cs:%i bitlen:%i len:%i flags:%lx\n",
528 bus->seq, slave_plat->cs, bitlen, priv->len, flags);
532 * Assume that the beginning of a transfer with bits to
533 * transmit must contain a device command.
535 if (dout && flags & SPI_XFER_BEGIN)
540 if (flags & SPI_XFER_END)
545 zynq_qspi_transfer(priv);
550 static int zynq_qspi_set_speed(struct udevice *bus, uint speed)
552 struct zynq_qspi_platdata *plat = bus->platdata;
553 struct zynq_qspi_priv *priv = dev_get_priv(bus);
554 struct zynq_qspi_regs *regs = priv->regs;
556 u8 baud_rate_val = 0;
558 if (speed > plat->frequency)
559 speed = plat->frequency;
561 /* Set the clock frequency */
562 confr = readl(®s->cr);
564 /* Set baudrate x8, if the freq is 0 */
566 } else if (plat->speed_hz != speed) {
567 while ((baud_rate_val < ZYNQ_QSPI_CR_BAUD_MAX) &&
569 (2 << baud_rate_val)) > speed))
572 plat->speed_hz = speed / (2 << baud_rate_val);
574 confr &= ~ZYNQ_QSPI_CR_BAUD_MASK;
575 confr |= (baud_rate_val << ZYNQ_QSPI_CR_BAUD_SHIFT);
577 writel(confr, ®s->cr);
580 debug("%s: regs=%p, speed=%d\n", __func__, priv->regs, priv->freq);
585 static int zynq_qspi_set_mode(struct udevice *bus, uint mode)
587 struct zynq_qspi_priv *priv = dev_get_priv(bus);
588 struct zynq_qspi_regs *regs = priv->regs;
591 /* Set the SPI Clock phase and polarities */
592 confr = readl(®s->cr);
593 confr &= ~(ZYNQ_QSPI_CR_CPHA_MASK | ZYNQ_QSPI_CR_CPOL_MASK);
596 confr |= ZYNQ_QSPI_CR_CPHA_MASK;
598 confr |= ZYNQ_QSPI_CR_CPOL_MASK;
600 writel(confr, ®s->cr);
603 debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode);
608 static const struct dm_spi_ops zynq_qspi_ops = {
609 .claim_bus = zynq_qspi_claim_bus,
610 .release_bus = zynq_qspi_release_bus,
611 .xfer = zynq_qspi_xfer,
612 .set_speed = zynq_qspi_set_speed,
613 .set_mode = zynq_qspi_set_mode,
616 static const struct udevice_id zynq_qspi_ids[] = {
617 { .compatible = "xlnx,zynq-qspi-1.0" },
621 U_BOOT_DRIVER(zynq_qspi) = {
624 .of_match = zynq_qspi_ids,
625 .ops = &zynq_qspi_ops,
626 .ofdata_to_platdata = zynq_qspi_ofdata_to_platdata,
627 .platdata_auto_alloc_size = sizeof(struct zynq_qspi_platdata),
628 .priv_auto_alloc_size = sizeof(struct zynq_qspi_priv),
629 .probe = zynq_qspi_probe,