1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2013 Xilinx, Inc.
4 * (C) Copyright 2015 Jagan Teki <jteki@openedev.com>
6 * Xilinx Zynq Quad-SPI(QSPI) controller driver (master mode only)
15 DECLARE_GLOBAL_DATA_PTR;
17 /* zynq qspi register bit masks ZYNQ_QSPI_<REG>_<BIT>_MASK */
18 #define ZYNQ_QSPI_CR_IFMODE_MASK BIT(31) /* Flash intrface mode*/
19 #define ZYNQ_QSPI_CR_MSA_MASK BIT(15) /* Manual start enb */
20 #define ZYNQ_QSPI_CR_MCS_MASK BIT(14) /* Manual chip select */
21 #define ZYNQ_QSPI_CR_PCS_MASK BIT(10) /* Peri chip select */
22 #define ZYNQ_QSPI_CR_FW_MASK GENMASK(7, 6) /* FIFO width */
23 #define ZYNQ_QSPI_CR_SS_MASK GENMASK(13, 10) /* Slave Select */
24 #define ZYNQ_QSPI_CR_BAUD_MASK GENMASK(5, 3) /* Baud rate div */
25 #define ZYNQ_QSPI_CR_CPHA_MASK BIT(2) /* Clock phase */
26 #define ZYNQ_QSPI_CR_CPOL_MASK BIT(1) /* Clock polarity */
27 #define ZYNQ_QSPI_CR_MSTREN_MASK BIT(0) /* Mode select */
28 #define ZYNQ_QSPI_IXR_RXNEMPTY_MASK BIT(4) /* RX_FIFO_not_empty */
29 #define ZYNQ_QSPI_IXR_TXOW_MASK BIT(2) /* TX_FIFO_not_full */
30 #define ZYNQ_QSPI_IXR_ALL_MASK GENMASK(6, 0) /* All IXR bits */
31 #define ZYNQ_QSPI_ENR_SPI_EN_MASK BIT(0) /* SPI Enable */
32 #define ZYNQ_QSPI_LQSPICFG_LQMODE_MASK BIT(31) /* Linear QSPI Mode */
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 {
72 u32 lqspicfg; /* 0xA0 */
73 u32 lqspists; /* 0xA4 */
76 /* zynq qspi platform data */
77 struct zynq_qspi_platdata {
78 struct zynq_qspi_regs *regs;
79 u32 frequency; /* input frequency */
84 struct zynq_qspi_priv {
85 struct zynq_qspi_regs *regs;
89 u32 freq; /* required frequency */
93 int bytes_to_transfer;
99 static int zynq_qspi_ofdata_to_platdata(struct udevice *bus)
101 struct zynq_qspi_platdata *plat = bus->platdata;
102 const void *blob = gd->fdt_blob;
103 int node = dev_of_offset(bus);
105 plat->regs = (struct zynq_qspi_regs *)fdtdec_get_addr(blob,
108 /* FIXME: Use 166MHz as a suitable default */
109 plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
111 plat->speed_hz = plat->frequency / 2;
113 debug("%s: regs=%p max-frequency=%d\n", __func__,
114 plat->regs, plat->frequency);
119 static void zynq_qspi_init_hw(struct zynq_qspi_priv *priv)
121 struct zynq_qspi_regs *regs = priv->regs;
125 writel(~ZYNQ_QSPI_ENR_SPI_EN_MASK, ®s->enr);
127 /* Disable Interrupts */
128 writel(ZYNQ_QSPI_IXR_ALL_MASK, ®s->idr);
130 /* Clear the TX and RX threshold reg */
131 writel(ZYNQ_QSPI_TXFIFO_THRESHOLD, ®s->txftr);
132 writel(ZYNQ_QSPI_RXFIFO_THRESHOLD, ®s->rxftr);
134 /* Clear the RX FIFO */
135 while (readl(®s->isr) & ZYNQ_QSPI_IXR_RXNEMPTY_MASK)
138 /* Clear Interrupts */
139 writel(ZYNQ_QSPI_IXR_ALL_MASK, ®s->isr);
141 /* Manual slave select and Auto start */
142 confr = readl(®s->cr);
143 confr &= ~ZYNQ_QSPI_CR_MSA_MASK;
144 confr |= ZYNQ_QSPI_CR_IFMODE_MASK | ZYNQ_QSPI_CR_MCS_MASK |
145 ZYNQ_QSPI_CR_PCS_MASK | ZYNQ_QSPI_CR_FW_MASK |
146 ZYNQ_QSPI_CR_MSTREN_MASK;
147 writel(confr, ®s->cr);
149 /* Disable the LQSPI feature */
150 confr = readl(®s->lqspicfg);
151 confr &= ~ZYNQ_QSPI_LQSPICFG_LQMODE_MASK;
152 writel(confr, ®s->lqspicfg);
155 writel(ZYNQ_QSPI_ENR_SPI_EN_MASK, ®s->enr);
158 static int zynq_qspi_probe(struct udevice *bus)
160 struct zynq_qspi_platdata *plat = dev_get_platdata(bus);
161 struct zynq_qspi_priv *priv = dev_get_priv(bus);
163 priv->regs = plat->regs;
164 priv->fifo_depth = ZYNQ_QSPI_FIFO_DEPTH;
166 /* init the zynq spi hw */
167 zynq_qspi_init_hw(priv);
173 * zynq_qspi_read_data - Copy data to RX buffer
174 * @zqspi: Pointer to the zynq_qspi structure
175 * @data: The 32 bit variable where data is stored
176 * @size: Number of bytes to be copied from data to RX buffer
178 static void zynq_qspi_read_data(struct zynq_qspi_priv *priv, u32 data, u8 size)
182 debug("%s: data 0x%04x rx_buf addr: 0x%08x size %d\n", __func__ ,
183 data, (unsigned)(priv->rx_buf), size);
188 *((u8 *)priv->rx_buf) = data;
192 *((u16 *)priv->rx_buf) = data;
196 *((u16 *)priv->rx_buf) = data;
198 byte3 = (u8)(data >> 16);
199 *((u8 *)priv->rx_buf) = byte3;
203 /* Can not assume word aligned buffer */
204 memcpy(priv->rx_buf, &data, size);
208 /* This will never execute */
212 priv->bytes_to_receive -= size;
213 if (priv->bytes_to_receive < 0)
214 priv->bytes_to_receive = 0;
218 * zynq_qspi_write_data - Copy data from TX buffer
219 * @zqspi: Pointer to the zynq_qspi structure
220 * @data: Pointer to the 32 bit variable where data is to be copied
221 * @size: Number of bytes to be copied from TX buffer to data
223 static void zynq_qspi_write_data(struct zynq_qspi_priv *priv,
229 *data = *((u8 *)priv->tx_buf);
234 *data = *((u16 *)priv->tx_buf);
239 *data = *((u16 *)priv->tx_buf);
241 *data |= (*((u8 *)priv->tx_buf) << 16);
246 /* Can not assume word aligned buffer */
247 memcpy(data, priv->tx_buf, size);
251 /* This will never execute */
258 debug("%s: data 0x%08x tx_buf addr: 0x%08x size %d\n", __func__,
259 *data, (u32)priv->tx_buf, size);
261 priv->bytes_to_transfer -= size;
262 if (priv->bytes_to_transfer < 0)
263 priv->bytes_to_transfer = 0;
266 static void zynq_qspi_chipselect(struct zynq_qspi_priv *priv, int is_on)
269 struct zynq_qspi_regs *regs = priv->regs;
271 confr = readl(®s->cr);
274 /* Select the slave */
275 confr &= ~ZYNQ_QSPI_CR_SS_MASK;
276 confr |= (~(1 << priv->cs) << ZYNQ_QSPI_CR_SS_SHIFT) &
277 ZYNQ_QSPI_CR_SS_MASK;
279 /* Deselect the slave */
280 confr |= ZYNQ_QSPI_CR_SS_MASK;
282 writel(confr, ®s->cr);
286 * zynq_qspi_fill_tx_fifo - Fills the TX FIFO with as many bytes as possible
287 * @zqspi: Pointer to the zynq_qspi structure
289 static void zynq_qspi_fill_tx_fifo(struct zynq_qspi_priv *priv, u32 size)
293 unsigned len, offset;
294 struct zynq_qspi_regs *regs = priv->regs;
295 static const unsigned offsets[4] = {
296 ZYNQ_QSPI_TXD_00_00_OFFSET, ZYNQ_QSPI_TXD_00_01_OFFSET,
297 ZYNQ_QSPI_TXD_00_10_OFFSET, ZYNQ_QSPI_TXD_00_11_OFFSET };
299 while ((fifocount < size) &&
300 (priv->bytes_to_transfer > 0)) {
301 if (priv->bytes_to_transfer >= 4) {
303 memcpy(&data, priv->tx_buf, 4);
308 writel(data, ®s->txd0r);
309 priv->bytes_to_transfer -= 4;
312 /* Write TXD1, TXD2, TXD3 only if TxFIFO is empty. */
313 if (!(readl(®s->isr)
314 & ZYNQ_QSPI_IXR_TXOW_MASK) &&
317 len = priv->bytes_to_transfer;
318 zynq_qspi_write_data(priv, &data, len);
319 offset = (priv->rx_buf) ? offsets[0] : offsets[len];
320 writel(data, ®s->cr + (offset / 4));
326 * zynq_qspi_irq_poll - Interrupt service routine of the QSPI controller
327 * @zqspi: Pointer to the zynq_qspi structure
329 * This function handles TX empty and Mode Fault interrupts only.
330 * On TX empty interrupt this function reads the received data from RX FIFO and
331 * fills the TX FIFO if there is any data remaining to be transferred.
332 * On Mode Fault interrupt this function indicates that transfer is completed,
333 * the SPI subsystem will identify the error as the remaining bytes to be
334 * transferred is non-zero.
336 * returns: 0 for poll timeout
337 * 1 transfer operation complete
339 static int zynq_qspi_irq_poll(struct zynq_qspi_priv *priv)
341 struct zynq_qspi_regs *regs = priv->regs;
346 /* Poll until any of the interrupt status bits are set */
347 timeout = get_timer(0);
349 status = readl(®s->isr);
350 } while ((status == 0) &&
351 (get_timer(timeout) < CONFIG_SYS_ZYNQ_QSPI_WAIT));
354 printf("zynq_qspi_irq_poll: Timeout!\n");
358 writel(status, ®s->isr);
360 /* Disable all interrupts */
361 writel(ZYNQ_QSPI_IXR_ALL_MASK, ®s->idr);
362 if ((status & ZYNQ_QSPI_IXR_TXOW_MASK) ||
363 (status & ZYNQ_QSPI_IXR_RXNEMPTY_MASK)) {
365 * This bit is set when Tx FIFO has < THRESHOLD entries. We have
366 * the THRESHOLD value set to 1, so this bit indicates Tx FIFO
369 rxcount = priv->bytes_to_receive - priv->bytes_to_transfer;
370 rxcount = (rxcount % 4) ? ((rxcount/4)+1) : (rxcount/4);
371 while ((rxindex < rxcount) &&
372 (rxindex < ZYNQ_QSPI_RXFIFO_THRESHOLD)) {
373 /* Read out the data from the RX FIFO */
375 data = readl(®s->drxr);
377 if (priv->bytes_to_receive >= 4) {
379 memcpy(priv->rx_buf, &data, 4);
382 priv->bytes_to_receive -= 4;
384 zynq_qspi_read_data(priv, data,
385 priv->bytes_to_receive);
390 if (priv->bytes_to_transfer) {
391 /* There is more data to send */
392 zynq_qspi_fill_tx_fifo(priv,
393 ZYNQ_QSPI_RXFIFO_THRESHOLD);
395 writel(ZYNQ_QSPI_IXR_ALL_MASK, ®s->ier);
398 * If transfer and receive is completed then only send
401 if (!priv->bytes_to_receive) {
402 /* return operation complete */
403 writel(ZYNQ_QSPI_IXR_ALL_MASK,
414 * zynq_qspi_start_transfer - Initiates the QSPI transfer
415 * @qspi: Pointer to the spi_device structure
416 * @transfer: Pointer to the spi_transfer structure which provide information
417 * about next transfer parameters
419 * This function fills the TX FIFO, starts the QSPI transfer, and waits for the
420 * transfer to be completed.
422 * returns: Number of bytes transferred in the last transfer
424 static int zynq_qspi_start_transfer(struct zynq_qspi_priv *priv)
427 struct zynq_qspi_regs *regs = priv->regs;
429 debug("%s: qspi: 0x%08x transfer: 0x%08x len: %d\n", __func__,
430 (u32)priv, (u32)priv, priv->len);
432 priv->bytes_to_transfer = priv->len;
433 priv->bytes_to_receive = priv->len;
436 zynq_qspi_fill_tx_fifo(priv, priv->len);
438 zynq_qspi_fill_tx_fifo(priv, priv->fifo_depth);
440 writel(ZYNQ_QSPI_IXR_ALL_MASK, ®s->ier);
442 /* wait for completion */
444 data = zynq_qspi_irq_poll(priv);
447 return (priv->len) - (priv->bytes_to_transfer);
450 static int zynq_qspi_transfer(struct zynq_qspi_priv *priv)
452 unsigned cs_change = 1;
456 /* Select the chip if required */
458 zynq_qspi_chipselect(priv, 1);
460 cs_change = priv->cs_change;
462 if (!priv->tx_buf && !priv->rx_buf && priv->len) {
467 /* Request the transfer */
469 status = zynq_qspi_start_transfer(priv);
473 if (status != priv->len) {
476 debug("zynq_qspi_transfer:%d len:%d\n",
483 /* Deselect the chip */
484 zynq_qspi_chipselect(priv, 0);
492 static int zynq_qspi_claim_bus(struct udevice *dev)
494 struct udevice *bus = dev->parent;
495 struct zynq_qspi_priv *priv = dev_get_priv(bus);
496 struct zynq_qspi_regs *regs = priv->regs;
498 writel(ZYNQ_QSPI_ENR_SPI_EN_MASK, ®s->enr);
503 static int zynq_qspi_release_bus(struct udevice *dev)
505 struct udevice *bus = dev->parent;
506 struct zynq_qspi_priv *priv = dev_get_priv(bus);
507 struct zynq_qspi_regs *regs = priv->regs;
509 writel(~ZYNQ_QSPI_ENR_SPI_EN_MASK, ®s->enr);
514 static int zynq_qspi_xfer(struct udevice *dev, unsigned int bitlen,
515 const void *dout, void *din, unsigned long flags)
517 struct udevice *bus = dev->parent;
518 struct zynq_qspi_priv *priv = dev_get_priv(bus);
519 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
521 priv->cs = slave_plat->cs;
524 priv->len = bitlen / 8;
526 debug("zynq_qspi_xfer: bus:%i cs:%i bitlen:%i len:%i flags:%lx\n",
527 bus->seq, slave_plat->cs, bitlen, priv->len, flags);
531 * Assume that the beginning of a transfer with bits to
532 * transmit must contain a device command.
534 if (dout && flags & SPI_XFER_BEGIN)
539 if (flags & SPI_XFER_END)
544 zynq_qspi_transfer(priv);
549 static int zynq_qspi_set_speed(struct udevice *bus, uint speed)
551 struct zynq_qspi_platdata *plat = bus->platdata;
552 struct zynq_qspi_priv *priv = dev_get_priv(bus);
553 struct zynq_qspi_regs *regs = priv->regs;
555 u8 baud_rate_val = 0;
557 if (speed > plat->frequency)
558 speed = plat->frequency;
560 /* Set the clock frequency */
561 confr = readl(®s->cr);
563 /* Set baudrate x8, if the freq is 0 */
565 } else if (plat->speed_hz != speed) {
566 while ((baud_rate_val < ZYNQ_QSPI_CR_BAUD_MAX) &&
568 (2 << baud_rate_val)) > speed))
571 plat->speed_hz = speed / (2 << baud_rate_val);
573 confr &= ~ZYNQ_QSPI_CR_BAUD_MASK;
574 confr |= (baud_rate_val << ZYNQ_QSPI_CR_BAUD_SHIFT);
576 writel(confr, ®s->cr);
579 debug("%s: regs=%p, speed=%d\n", __func__, priv->regs, priv->freq);
584 static int zynq_qspi_set_mode(struct udevice *bus, uint mode)
586 struct zynq_qspi_priv *priv = dev_get_priv(bus);
587 struct zynq_qspi_regs *regs = priv->regs;
590 /* Set the SPI Clock phase and polarities */
591 confr = readl(®s->cr);
592 confr &= ~(ZYNQ_QSPI_CR_CPHA_MASK | ZYNQ_QSPI_CR_CPOL_MASK);
595 confr |= ZYNQ_QSPI_CR_CPHA_MASK;
597 confr |= ZYNQ_QSPI_CR_CPOL_MASK;
599 writel(confr, ®s->cr);
602 debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode);
607 static const struct dm_spi_ops zynq_qspi_ops = {
608 .claim_bus = zynq_qspi_claim_bus,
609 .release_bus = zynq_qspi_release_bus,
610 .xfer = zynq_qspi_xfer,
611 .set_speed = zynq_qspi_set_speed,
612 .set_mode = zynq_qspi_set_mode,
615 static const struct udevice_id zynq_qspi_ids[] = {
616 { .compatible = "xlnx,zynq-qspi-1.0" },
620 U_BOOT_DRIVER(zynq_qspi) = {
623 .of_match = zynq_qspi_ids,
624 .ops = &zynq_qspi_ops,
625 .ofdata_to_platdata = zynq_qspi_ofdata_to_platdata,
626 .platdata_auto_alloc_size = sizeof(struct zynq_qspi_platdata),
627 .priv_auto_alloc_size = sizeof(struct zynq_qspi_priv),
628 .probe = zynq_qspi_probe,