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 PS SPI controller driver (master mode only)
16 #include <linux/bitops.h>
17 #include <linux/delay.h>
19 DECLARE_GLOBAL_DATA_PTR;
21 /* zynq spi register bit masks ZYNQ_SPI_<REG>_<BIT>_MASK */
22 #define ZYNQ_SPI_CR_MSA_MASK BIT(15) /* Manual start enb */
23 #define ZYNQ_SPI_CR_MCS_MASK BIT(14) /* Manual chip select */
24 #define ZYNQ_SPI_CR_CS_MASK GENMASK(13, 10) /* Chip select */
25 #define ZYNQ_SPI_CR_BAUD_MASK GENMASK(5, 3) /* Baud rate div */
26 #define ZYNQ_SPI_CR_CPHA_MASK BIT(2) /* Clock phase */
27 #define ZYNQ_SPI_CR_CPOL_MASK BIT(1) /* Clock polarity */
28 #define ZYNQ_SPI_CR_MSTREN_MASK BIT(0) /* Mode select */
29 #define ZYNQ_SPI_IXR_RXNEMPTY_MASK BIT(4) /* RX_FIFO_not_empty */
30 #define ZYNQ_SPI_IXR_TXOW_MASK BIT(2) /* TX_FIFO_not_full */
31 #define ZYNQ_SPI_IXR_ALL_MASK GENMASK(6, 0) /* All IXR bits */
32 #define ZYNQ_SPI_ENR_SPI_EN_MASK BIT(0) /* SPI Enable */
34 #define ZYNQ_SPI_CR_BAUD_MAX 8 /* Baud rate divisor max val */
35 #define ZYNQ_SPI_CR_BAUD_SHIFT 3 /* Baud rate divisor shift */
36 #define ZYNQ_SPI_CR_SS_SHIFT 10 /* Slave select shift */
38 #define ZYNQ_SPI_FIFO_DEPTH 128
39 #ifndef CONFIG_SYS_ZYNQ_SPI_WAIT
40 #define CONFIG_SYS_ZYNQ_SPI_WAIT (CONFIG_SYS_HZ/100) /* 10 ms */
43 /* zynq spi register set */
44 struct zynq_spi_regs {
57 /* zynq spi platform data */
58 struct zynq_spi_platdata {
59 struct zynq_spi_regs *regs;
60 u32 frequency; /* input frequency */
62 uint deactivate_delay_us; /* Delay to wait after deactivate */
63 uint activate_delay_us; /* Delay to wait after activate */
67 struct zynq_spi_priv {
68 struct zynq_spi_regs *regs;
71 ulong last_transaction_us; /* Time of last transaction end */
73 u32 freq; /* required frequency */
76 static int zynq_spi_ofdata_to_platdata(struct udevice *bus)
78 struct zynq_spi_platdata *plat = bus->platdata;
79 const void *blob = gd->fdt_blob;
80 int node = dev_of_offset(bus);
82 plat->regs = (struct zynq_spi_regs *)devfdt_get_addr(bus);
84 /* FIXME: Use 250MHz as a suitable default */
85 plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
87 plat->deactivate_delay_us = fdtdec_get_int(blob, node,
88 "spi-deactivate-delay", 0);
89 plat->activate_delay_us = fdtdec_get_int(blob, node,
90 "spi-activate-delay", 0);
91 plat->speed_hz = plat->frequency / 2;
93 debug("%s: regs=%p max-frequency=%d\n", __func__,
94 plat->regs, plat->frequency);
99 static void zynq_spi_init_hw(struct zynq_spi_priv *priv)
101 struct zynq_spi_regs *regs = priv->regs;
105 confr = ZYNQ_SPI_ENR_SPI_EN_MASK;
106 writel(~confr, ®s->enr);
108 /* Disable Interrupts */
109 writel(ZYNQ_SPI_IXR_ALL_MASK, ®s->idr);
112 while (readl(®s->isr) &
113 ZYNQ_SPI_IXR_RXNEMPTY_MASK)
116 /* Clear Interrupts */
117 writel(ZYNQ_SPI_IXR_ALL_MASK, ®s->isr);
119 /* Manual slave select and Auto start */
120 confr = ZYNQ_SPI_CR_MCS_MASK | ZYNQ_SPI_CR_CS_MASK |
121 ZYNQ_SPI_CR_MSTREN_MASK;
122 confr &= ~ZYNQ_SPI_CR_MSA_MASK;
123 writel(confr, ®s->cr);
126 writel(ZYNQ_SPI_ENR_SPI_EN_MASK, ®s->enr);
129 static int zynq_spi_probe(struct udevice *bus)
131 struct zynq_spi_platdata *plat = dev_get_platdata(bus);
132 struct zynq_spi_priv *priv = dev_get_priv(bus);
134 priv->regs = plat->regs;
135 priv->fifo_depth = ZYNQ_SPI_FIFO_DEPTH;
137 /* init the zynq spi hw */
138 zynq_spi_init_hw(priv);
143 static void spi_cs_activate(struct udevice *dev)
145 struct udevice *bus = dev->parent;
146 struct zynq_spi_platdata *plat = bus->platdata;
147 struct zynq_spi_priv *priv = dev_get_priv(bus);
148 struct zynq_spi_regs *regs = priv->regs;
151 /* If it's too soon to do another transaction, wait */
152 if (plat->deactivate_delay_us && priv->last_transaction_us) {
153 ulong delay_us; /* The delay completed so far */
154 delay_us = timer_get_us() - priv->last_transaction_us;
155 if (delay_us < plat->deactivate_delay_us)
156 udelay(plat->deactivate_delay_us - delay_us);
159 clrbits_le32(®s->cr, ZYNQ_SPI_CR_CS_MASK);
160 cr = readl(®s->cr);
162 * CS cal logic: CS[13:10]
167 cr |= (~(1 << priv->cs) << ZYNQ_SPI_CR_SS_SHIFT) & ZYNQ_SPI_CR_CS_MASK;
168 writel(cr, ®s->cr);
170 if (plat->activate_delay_us)
171 udelay(plat->activate_delay_us);
174 static void spi_cs_deactivate(struct udevice *dev)
176 struct udevice *bus = dev->parent;
177 struct zynq_spi_platdata *plat = bus->platdata;
178 struct zynq_spi_priv *priv = dev_get_priv(bus);
179 struct zynq_spi_regs *regs = priv->regs;
181 setbits_le32(®s->cr, ZYNQ_SPI_CR_CS_MASK);
183 /* Remember time of this transaction so we can honour the bus delay */
184 if (plat->deactivate_delay_us)
185 priv->last_transaction_us = timer_get_us();
188 static int zynq_spi_claim_bus(struct udevice *dev)
190 struct udevice *bus = dev->parent;
191 struct zynq_spi_priv *priv = dev_get_priv(bus);
192 struct zynq_spi_regs *regs = priv->regs;
194 writel(ZYNQ_SPI_ENR_SPI_EN_MASK, ®s->enr);
199 static int zynq_spi_release_bus(struct udevice *dev)
201 struct udevice *bus = dev->parent;
202 struct zynq_spi_priv *priv = dev_get_priv(bus);
203 struct zynq_spi_regs *regs = priv->regs;
206 confr = ZYNQ_SPI_ENR_SPI_EN_MASK;
207 writel(~confr, ®s->enr);
212 static int zynq_spi_xfer(struct udevice *dev, unsigned int bitlen,
213 const void *dout, void *din, unsigned long flags)
215 struct udevice *bus = dev->parent;
216 struct zynq_spi_priv *priv = dev_get_priv(bus);
217 struct zynq_spi_regs *regs = priv->regs;
218 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
219 u32 len = bitlen / 8;
220 u32 tx_len = len, rx_len = len, tx_tvl;
221 const u8 *tx_buf = dout;
222 u8 *rx_buf = din, buf;
225 debug("spi_xfer: bus:%i cs:%i bitlen:%i len:%i flags:%lx\n",
226 bus->seq, slave_plat->cs, bitlen, len, flags);
229 debug("spi_xfer: Non byte aligned SPI transfer\n");
233 priv->cs = slave_plat->cs;
234 if (flags & SPI_XFER_BEGIN)
235 spi_cs_activate(dev);
238 /* Write the data into TX FIFO - tx threshold is fifo_depth */
240 while ((tx_tvl < priv->fifo_depth) && tx_len) {
245 writel(buf, ®s->txdr);
250 /* Check TX FIFO completion */
252 status = readl(®s->isr);
253 while (!(status & ZYNQ_SPI_IXR_TXOW_MASK)) {
254 if (get_timer(ts) > CONFIG_SYS_ZYNQ_SPI_WAIT) {
255 printf("spi_xfer: Timeout! TX FIFO not full\n");
258 status = readl(®s->isr);
261 /* Read the data from RX FIFO */
262 status = readl(®s->isr);
263 while ((status & ZYNQ_SPI_IXR_RXNEMPTY_MASK) && rx_len) {
264 buf = readl(®s->rxdr);
267 status = readl(®s->isr);
272 if (flags & SPI_XFER_END)
273 spi_cs_deactivate(dev);
278 static int zynq_spi_set_speed(struct udevice *bus, uint speed)
280 struct zynq_spi_platdata *plat = bus->platdata;
281 struct zynq_spi_priv *priv = dev_get_priv(bus);
282 struct zynq_spi_regs *regs = priv->regs;
284 u8 baud_rate_val = 0;
286 if (speed > plat->frequency)
287 speed = plat->frequency;
289 /* Set the clock frequency */
290 confr = readl(®s->cr);
292 /* Set baudrate x8, if the freq is 0 */
294 } else if (plat->speed_hz != speed) {
295 while ((baud_rate_val < ZYNQ_SPI_CR_BAUD_MAX) &&
297 (2 << baud_rate_val)) > speed))
299 plat->speed_hz = speed / (2 << baud_rate_val);
301 confr &= ~ZYNQ_SPI_CR_BAUD_MASK;
302 confr |= (baud_rate_val << ZYNQ_SPI_CR_BAUD_SHIFT);
304 writel(confr, ®s->cr);
307 debug("zynq_spi_set_speed: regs=%p, speed=%d\n",
308 priv->regs, priv->freq);
313 static int zynq_spi_set_mode(struct udevice *bus, uint mode)
315 struct zynq_spi_priv *priv = dev_get_priv(bus);
316 struct zynq_spi_regs *regs = priv->regs;
319 /* Set the SPI Clock phase and polarities */
320 confr = readl(®s->cr);
321 confr &= ~(ZYNQ_SPI_CR_CPHA_MASK | ZYNQ_SPI_CR_CPOL_MASK);
324 confr |= ZYNQ_SPI_CR_CPHA_MASK;
326 confr |= ZYNQ_SPI_CR_CPOL_MASK;
328 writel(confr, ®s->cr);
331 debug("zynq_spi_set_mode: regs=%p, mode=%d\n", priv->regs, priv->mode);
336 static const struct dm_spi_ops zynq_spi_ops = {
337 .claim_bus = zynq_spi_claim_bus,
338 .release_bus = zynq_spi_release_bus,
339 .xfer = zynq_spi_xfer,
340 .set_speed = zynq_spi_set_speed,
341 .set_mode = zynq_spi_set_mode,
344 static const struct udevice_id zynq_spi_ids[] = {
345 { .compatible = "xlnx,zynq-spi-r1p6" },
346 { .compatible = "cdns,spi-r1p6" },
350 U_BOOT_DRIVER(zynq_spi) = {
353 .of_match = zynq_spi_ids,
354 .ops = &zynq_spi_ops,
355 .ofdata_to_platdata = zynq_spi_ofdata_to_platdata,
356 .platdata_auto_alloc_size = sizeof(struct zynq_spi_platdata),
357 .priv_auto_alloc_size = sizeof(struct zynq_spi_priv),
358 .probe = zynq_spi_probe,