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)
15 DECLARE_GLOBAL_DATA_PTR;
17 /* zynq spi register bit masks ZYNQ_SPI_<REG>_<BIT>_MASK */
18 #define ZYNQ_SPI_CR_MSA_MASK BIT(15) /* Manual start enb */
19 #define ZYNQ_SPI_CR_MCS_MASK BIT(14) /* Manual chip select */
20 #define ZYNQ_SPI_CR_CS_MASK GENMASK(13, 10) /* Chip select */
21 #define ZYNQ_SPI_CR_BAUD_MASK GENMASK(5, 3) /* Baud rate div */
22 #define ZYNQ_SPI_CR_CPHA_MASK BIT(2) /* Clock phase */
23 #define ZYNQ_SPI_CR_CPOL_MASK BIT(1) /* Clock polarity */
24 #define ZYNQ_SPI_CR_MSTREN_MASK BIT(0) /* Mode select */
25 #define ZYNQ_SPI_IXR_RXNEMPTY_MASK BIT(4) /* RX_FIFO_not_empty */
26 #define ZYNQ_SPI_IXR_TXOW_MASK BIT(2) /* TX_FIFO_not_full */
27 #define ZYNQ_SPI_IXR_ALL_MASK GENMASK(6, 0) /* All IXR bits */
28 #define ZYNQ_SPI_ENR_SPI_EN_MASK BIT(0) /* SPI Enable */
30 #define ZYNQ_SPI_CR_BAUD_MAX 8 /* Baud rate divisor max val */
31 #define ZYNQ_SPI_CR_BAUD_SHIFT 3 /* Baud rate divisor shift */
32 #define ZYNQ_SPI_CR_SS_SHIFT 10 /* Slave select shift */
34 #define ZYNQ_SPI_FIFO_DEPTH 128
35 #ifndef CONFIG_SYS_ZYNQ_SPI_WAIT
36 #define CONFIG_SYS_ZYNQ_SPI_WAIT (CONFIG_SYS_HZ/100) /* 10 ms */
39 /* zynq spi register set */
40 struct zynq_spi_regs {
53 /* zynq spi platform data */
54 struct zynq_spi_platdata {
55 struct zynq_spi_regs *regs;
56 u32 frequency; /* input frequency */
58 uint deactivate_delay_us; /* Delay to wait after deactivate */
59 uint activate_delay_us; /* Delay to wait after activate */
63 struct zynq_spi_priv {
64 struct zynq_spi_regs *regs;
67 ulong last_transaction_us; /* Time of last transaction end */
69 u32 freq; /* required frequency */
72 static int zynq_spi_ofdata_to_platdata(struct udevice *bus)
74 struct zynq_spi_platdata *plat = bus->platdata;
75 const void *blob = gd->fdt_blob;
76 int node = dev_of_offset(bus);
78 plat->regs = (struct zynq_spi_regs *)devfdt_get_addr(bus);
80 /* FIXME: Use 250MHz as a suitable default */
81 plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
83 plat->deactivate_delay_us = fdtdec_get_int(blob, node,
84 "spi-deactivate-delay", 0);
85 plat->activate_delay_us = fdtdec_get_int(blob, node,
86 "spi-activate-delay", 0);
87 plat->speed_hz = plat->frequency / 2;
89 debug("%s: regs=%p max-frequency=%d\n", __func__,
90 plat->regs, plat->frequency);
95 static void zynq_spi_init_hw(struct zynq_spi_priv *priv)
97 struct zynq_spi_regs *regs = priv->regs;
101 confr = ZYNQ_SPI_ENR_SPI_EN_MASK;
102 writel(~confr, ®s->enr);
104 /* Disable Interrupts */
105 writel(ZYNQ_SPI_IXR_ALL_MASK, ®s->idr);
108 while (readl(®s->isr) &
109 ZYNQ_SPI_IXR_RXNEMPTY_MASK)
112 /* Clear Interrupts */
113 writel(ZYNQ_SPI_IXR_ALL_MASK, ®s->isr);
115 /* Manual slave select and Auto start */
116 confr = ZYNQ_SPI_CR_MCS_MASK | ZYNQ_SPI_CR_CS_MASK |
117 ZYNQ_SPI_CR_MSTREN_MASK;
118 confr &= ~ZYNQ_SPI_CR_MSA_MASK;
119 writel(confr, ®s->cr);
122 writel(ZYNQ_SPI_ENR_SPI_EN_MASK, ®s->enr);
125 static int zynq_spi_probe(struct udevice *bus)
127 struct zynq_spi_platdata *plat = dev_get_platdata(bus);
128 struct zynq_spi_priv *priv = dev_get_priv(bus);
130 priv->regs = plat->regs;
131 priv->fifo_depth = ZYNQ_SPI_FIFO_DEPTH;
133 /* init the zynq spi hw */
134 zynq_spi_init_hw(priv);
139 static void spi_cs_activate(struct udevice *dev)
141 struct udevice *bus = dev->parent;
142 struct zynq_spi_platdata *plat = bus->platdata;
143 struct zynq_spi_priv *priv = dev_get_priv(bus);
144 struct zynq_spi_regs *regs = priv->regs;
147 /* If it's too soon to do another transaction, wait */
148 if (plat->deactivate_delay_us && priv->last_transaction_us) {
149 ulong delay_us; /* The delay completed so far */
150 delay_us = timer_get_us() - priv->last_transaction_us;
151 if (delay_us < plat->deactivate_delay_us)
152 udelay(plat->deactivate_delay_us - delay_us);
155 clrbits_le32(®s->cr, ZYNQ_SPI_CR_CS_MASK);
156 cr = readl(®s->cr);
158 * CS cal logic: CS[13:10]
163 cr |= (~(1 << priv->cs) << ZYNQ_SPI_CR_SS_SHIFT) & ZYNQ_SPI_CR_CS_MASK;
164 writel(cr, ®s->cr);
166 if (plat->activate_delay_us)
167 udelay(plat->activate_delay_us);
170 static void spi_cs_deactivate(struct udevice *dev)
172 struct udevice *bus = dev->parent;
173 struct zynq_spi_platdata *plat = bus->platdata;
174 struct zynq_spi_priv *priv = dev_get_priv(bus);
175 struct zynq_spi_regs *regs = priv->regs;
177 setbits_le32(®s->cr, ZYNQ_SPI_CR_CS_MASK);
179 /* Remember time of this transaction so we can honour the bus delay */
180 if (plat->deactivate_delay_us)
181 priv->last_transaction_us = timer_get_us();
184 static int zynq_spi_claim_bus(struct udevice *dev)
186 struct udevice *bus = dev->parent;
187 struct zynq_spi_priv *priv = dev_get_priv(bus);
188 struct zynq_spi_regs *regs = priv->regs;
190 writel(ZYNQ_SPI_ENR_SPI_EN_MASK, ®s->enr);
195 static int zynq_spi_release_bus(struct udevice *dev)
197 struct udevice *bus = dev->parent;
198 struct zynq_spi_priv *priv = dev_get_priv(bus);
199 struct zynq_spi_regs *regs = priv->regs;
202 confr = ZYNQ_SPI_ENR_SPI_EN_MASK;
203 writel(~confr, ®s->enr);
208 static int zynq_spi_xfer(struct udevice *dev, unsigned int bitlen,
209 const void *dout, void *din, unsigned long flags)
211 struct udevice *bus = dev->parent;
212 struct zynq_spi_priv *priv = dev_get_priv(bus);
213 struct zynq_spi_regs *regs = priv->regs;
214 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
215 u32 len = bitlen / 8;
216 u32 tx_len = len, rx_len = len, tx_tvl;
217 const u8 *tx_buf = dout;
218 u8 *rx_buf = din, buf;
221 debug("spi_xfer: bus:%i cs:%i bitlen:%i len:%i flags:%lx\n",
222 bus->seq, slave_plat->cs, bitlen, len, flags);
225 debug("spi_xfer: Non byte aligned SPI transfer\n");
229 priv->cs = slave_plat->cs;
230 if (flags & SPI_XFER_BEGIN)
231 spi_cs_activate(dev);
234 /* Write the data into TX FIFO - tx threshold is fifo_depth */
236 while ((tx_tvl < priv->fifo_depth) && tx_len) {
241 writel(buf, ®s->txdr);
246 /* Check TX FIFO completion */
248 status = readl(®s->isr);
249 while (!(status & ZYNQ_SPI_IXR_TXOW_MASK)) {
250 if (get_timer(ts) > CONFIG_SYS_ZYNQ_SPI_WAIT) {
251 printf("spi_xfer: Timeout! TX FIFO not full\n");
254 status = readl(®s->isr);
257 /* Read the data from RX FIFO */
258 status = readl(®s->isr);
259 while ((status & ZYNQ_SPI_IXR_RXNEMPTY_MASK) && rx_len) {
260 buf = readl(®s->rxdr);
263 status = readl(®s->isr);
268 if (flags & SPI_XFER_END)
269 spi_cs_deactivate(dev);
274 static int zynq_spi_set_speed(struct udevice *bus, uint speed)
276 struct zynq_spi_platdata *plat = bus->platdata;
277 struct zynq_spi_priv *priv = dev_get_priv(bus);
278 struct zynq_spi_regs *regs = priv->regs;
280 u8 baud_rate_val = 0;
282 if (speed > plat->frequency)
283 speed = plat->frequency;
285 /* Set the clock frequency */
286 confr = readl(®s->cr);
288 /* Set baudrate x8, if the freq is 0 */
290 } else if (plat->speed_hz != speed) {
291 while ((baud_rate_val < ZYNQ_SPI_CR_BAUD_MAX) &&
293 (2 << baud_rate_val)) > speed))
295 plat->speed_hz = speed / (2 << baud_rate_val);
297 confr &= ~ZYNQ_SPI_CR_BAUD_MASK;
298 confr |= (baud_rate_val << ZYNQ_SPI_CR_BAUD_SHIFT);
300 writel(confr, ®s->cr);
303 debug("zynq_spi_set_speed: regs=%p, speed=%d\n",
304 priv->regs, priv->freq);
309 static int zynq_spi_set_mode(struct udevice *bus, uint mode)
311 struct zynq_spi_priv *priv = dev_get_priv(bus);
312 struct zynq_spi_regs *regs = priv->regs;
315 /* Set the SPI Clock phase and polarities */
316 confr = readl(®s->cr);
317 confr &= ~(ZYNQ_SPI_CR_CPHA_MASK | ZYNQ_SPI_CR_CPOL_MASK);
320 confr |= ZYNQ_SPI_CR_CPHA_MASK;
322 confr |= ZYNQ_SPI_CR_CPOL_MASK;
324 writel(confr, ®s->cr);
327 debug("zynq_spi_set_mode: regs=%p, mode=%d\n", priv->regs, priv->mode);
332 static const struct dm_spi_ops zynq_spi_ops = {
333 .claim_bus = zynq_spi_claim_bus,
334 .release_bus = zynq_spi_release_bus,
335 .xfer = zynq_spi_xfer,
336 .set_speed = zynq_spi_set_speed,
337 .set_mode = zynq_spi_set_mode,
340 static const struct udevice_id zynq_spi_ids[] = {
341 { .compatible = "xlnx,zynq-spi-r1p6" },
342 { .compatible = "cdns,spi-r1p6" },
346 U_BOOT_DRIVER(zynq_spi) = {
349 .of_match = zynq_spi_ids,
350 .ops = &zynq_spi_ops,
351 .ofdata_to_platdata = zynq_spi_ofdata_to_platdata,
352 .platdata_auto_alloc_size = sizeof(struct zynq_spi_platdata),
353 .priv_auto_alloc_size = sizeof(struct zynq_spi_priv),
354 .probe = zynq_spi_probe,