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 #define ZYNQ_SPI_WAIT (CONFIG_SYS_HZ / 100) /* 10 ms */
41 /* zynq spi register set */
42 struct zynq_spi_regs {
55 /* zynq spi platform data */
56 struct zynq_spi_platdata {
57 struct zynq_spi_regs *regs;
58 u32 frequency; /* input frequency */
60 uint deactivate_delay_us; /* Delay to wait after deactivate */
61 uint activate_delay_us; /* Delay to wait after activate */
65 struct zynq_spi_priv {
66 struct zynq_spi_regs *regs;
69 ulong last_transaction_us; /* Time of last transaction end */
71 u32 freq; /* required frequency */
74 static int zynq_spi_ofdata_to_platdata(struct udevice *bus)
76 struct zynq_spi_platdata *plat = bus->platdata;
77 const void *blob = gd->fdt_blob;
78 int node = dev_of_offset(bus);
80 plat->regs = (struct zynq_spi_regs *)devfdt_get_addr(bus);
82 /* FIXME: Use 250MHz as a suitable default */
83 plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
85 plat->deactivate_delay_us = fdtdec_get_int(blob, node,
86 "spi-deactivate-delay", 0);
87 plat->activate_delay_us = fdtdec_get_int(blob, node,
88 "spi-activate-delay", 0);
89 plat->speed_hz = plat->frequency / 2;
91 debug("%s: regs=%p max-frequency=%d\n", __func__,
92 plat->regs, plat->frequency);
97 static void zynq_spi_init_hw(struct zynq_spi_priv *priv)
99 struct zynq_spi_regs *regs = priv->regs;
103 confr = ZYNQ_SPI_ENR_SPI_EN_MASK;
104 writel(~confr, ®s->enr);
106 /* Disable Interrupts */
107 writel(ZYNQ_SPI_IXR_ALL_MASK, ®s->idr);
110 while (readl(®s->isr) &
111 ZYNQ_SPI_IXR_RXNEMPTY_MASK)
114 /* Clear Interrupts */
115 writel(ZYNQ_SPI_IXR_ALL_MASK, ®s->isr);
117 /* Manual slave select and Auto start */
118 confr = ZYNQ_SPI_CR_MCS_MASK | ZYNQ_SPI_CR_CS_MASK |
119 ZYNQ_SPI_CR_MSTREN_MASK;
120 confr &= ~ZYNQ_SPI_CR_MSA_MASK;
121 writel(confr, ®s->cr);
124 writel(ZYNQ_SPI_ENR_SPI_EN_MASK, ®s->enr);
127 static int zynq_spi_probe(struct udevice *bus)
129 struct zynq_spi_platdata *plat = dev_get_platdata(bus);
130 struct zynq_spi_priv *priv = dev_get_priv(bus);
132 priv->regs = plat->regs;
133 priv->fifo_depth = ZYNQ_SPI_FIFO_DEPTH;
135 /* init the zynq spi hw */
136 zynq_spi_init_hw(priv);
141 static void spi_cs_activate(struct udevice *dev)
143 struct udevice *bus = dev->parent;
144 struct zynq_spi_platdata *plat = bus->platdata;
145 struct zynq_spi_priv *priv = dev_get_priv(bus);
146 struct zynq_spi_regs *regs = priv->regs;
149 /* If it's too soon to do another transaction, wait */
150 if (plat->deactivate_delay_us && priv->last_transaction_us) {
151 ulong delay_us; /* The delay completed so far */
152 delay_us = timer_get_us() - priv->last_transaction_us;
153 if (delay_us < plat->deactivate_delay_us)
154 udelay(plat->deactivate_delay_us - delay_us);
157 clrbits_le32(®s->cr, ZYNQ_SPI_CR_CS_MASK);
158 cr = readl(®s->cr);
160 * CS cal logic: CS[13:10]
165 cr |= (~(1 << priv->cs) << ZYNQ_SPI_CR_SS_SHIFT) & ZYNQ_SPI_CR_CS_MASK;
166 writel(cr, ®s->cr);
168 if (plat->activate_delay_us)
169 udelay(plat->activate_delay_us);
172 static void spi_cs_deactivate(struct udevice *dev)
174 struct udevice *bus = dev->parent;
175 struct zynq_spi_platdata *plat = bus->platdata;
176 struct zynq_spi_priv *priv = dev_get_priv(bus);
177 struct zynq_spi_regs *regs = priv->regs;
179 setbits_le32(®s->cr, ZYNQ_SPI_CR_CS_MASK);
181 /* Remember time of this transaction so we can honour the bus delay */
182 if (plat->deactivate_delay_us)
183 priv->last_transaction_us = timer_get_us();
186 static int zynq_spi_claim_bus(struct udevice *dev)
188 struct udevice *bus = dev->parent;
189 struct zynq_spi_priv *priv = dev_get_priv(bus);
190 struct zynq_spi_regs *regs = priv->regs;
192 writel(ZYNQ_SPI_ENR_SPI_EN_MASK, ®s->enr);
197 static int zynq_spi_release_bus(struct udevice *dev)
199 struct udevice *bus = dev->parent;
200 struct zynq_spi_priv *priv = dev_get_priv(bus);
201 struct zynq_spi_regs *regs = priv->regs;
204 confr = ZYNQ_SPI_ENR_SPI_EN_MASK;
205 writel(~confr, ®s->enr);
210 static int zynq_spi_xfer(struct udevice *dev, unsigned int bitlen,
211 const void *dout, void *din, unsigned long flags)
213 struct udevice *bus = dev->parent;
214 struct zynq_spi_priv *priv = dev_get_priv(bus);
215 struct zynq_spi_regs *regs = priv->regs;
216 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
217 u32 len = bitlen / 8;
218 u32 tx_len = len, rx_len = len, tx_tvl;
219 const u8 *tx_buf = dout;
220 u8 *rx_buf = din, buf;
223 debug("spi_xfer: bus:%i cs:%i bitlen:%i len:%i flags:%lx\n",
224 bus->seq, slave_plat->cs, bitlen, len, flags);
227 debug("spi_xfer: Non byte aligned SPI transfer\n");
231 priv->cs = slave_plat->cs;
232 if (flags & SPI_XFER_BEGIN)
233 spi_cs_activate(dev);
236 /* Write the data into TX FIFO - tx threshold is fifo_depth */
238 while ((tx_tvl < priv->fifo_depth) && tx_len) {
243 writel(buf, ®s->txdr);
248 /* Check TX FIFO completion */
250 status = readl(®s->isr);
251 while (!(status & ZYNQ_SPI_IXR_TXOW_MASK)) {
252 if (get_timer(ts) > ZYNQ_SPI_WAIT) {
253 printf("spi_xfer: Timeout! TX FIFO not full\n");
256 status = readl(®s->isr);
259 /* Read the data from RX FIFO */
260 status = readl(®s->isr);
261 while ((status & ZYNQ_SPI_IXR_RXNEMPTY_MASK) && rx_len) {
262 buf = readl(®s->rxdr);
265 status = readl(®s->isr);
270 if (flags & SPI_XFER_END)
271 spi_cs_deactivate(dev);
276 static int zynq_spi_set_speed(struct udevice *bus, uint speed)
278 struct zynq_spi_platdata *plat = bus->platdata;
279 struct zynq_spi_priv *priv = dev_get_priv(bus);
280 struct zynq_spi_regs *regs = priv->regs;
282 u8 baud_rate_val = 0;
284 if (speed > plat->frequency)
285 speed = plat->frequency;
287 /* Set the clock frequency */
288 confr = readl(®s->cr);
290 /* Set baudrate x8, if the freq is 0 */
292 } else if (plat->speed_hz != speed) {
293 while ((baud_rate_val < ZYNQ_SPI_CR_BAUD_MAX) &&
295 (2 << baud_rate_val)) > speed))
297 plat->speed_hz = speed / (2 << baud_rate_val);
299 confr &= ~ZYNQ_SPI_CR_BAUD_MASK;
300 confr |= (baud_rate_val << ZYNQ_SPI_CR_BAUD_SHIFT);
302 writel(confr, ®s->cr);
305 debug("zynq_spi_set_speed: regs=%p, speed=%d\n",
306 priv->regs, priv->freq);
311 static int zynq_spi_set_mode(struct udevice *bus, uint mode)
313 struct zynq_spi_priv *priv = dev_get_priv(bus);
314 struct zynq_spi_regs *regs = priv->regs;
317 /* Set the SPI Clock phase and polarities */
318 confr = readl(®s->cr);
319 confr &= ~(ZYNQ_SPI_CR_CPHA_MASK | ZYNQ_SPI_CR_CPOL_MASK);
322 confr |= ZYNQ_SPI_CR_CPHA_MASK;
324 confr |= ZYNQ_SPI_CR_CPOL_MASK;
326 writel(confr, ®s->cr);
329 debug("zynq_spi_set_mode: regs=%p, mode=%d\n", priv->regs, priv->mode);
334 static const struct dm_spi_ops zynq_spi_ops = {
335 .claim_bus = zynq_spi_claim_bus,
336 .release_bus = zynq_spi_release_bus,
337 .xfer = zynq_spi_xfer,
338 .set_speed = zynq_spi_set_speed,
339 .set_mode = zynq_spi_set_mode,
342 static const struct udevice_id zynq_spi_ids[] = {
343 { .compatible = "xlnx,zynq-spi-r1p6" },
344 { .compatible = "cdns,spi-r1p6" },
348 U_BOOT_DRIVER(zynq_spi) = {
351 .of_match = zynq_spi_ids,
352 .ops = &zynq_spi_ops,
353 .ofdata_to_platdata = zynq_spi_ofdata_to_platdata,
354 .platdata_auto_alloc_size = sizeof(struct zynq_spi_platdata),
355 .priv_auto_alloc_size = sizeof(struct zynq_spi_priv),
356 .probe = zynq_spi_probe,