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)
11 #include <dm/device_compat.h>
18 #include <linux/bitops.h>
19 #include <linux/delay.h>
21 DECLARE_GLOBAL_DATA_PTR;
23 /* zynq spi register bit masks ZYNQ_SPI_<REG>_<BIT>_MASK */
24 #define ZYNQ_SPI_CR_MSA_MASK BIT(15) /* Manual start enb */
25 #define ZYNQ_SPI_CR_MCS_MASK BIT(14) /* Manual chip select */
26 #define ZYNQ_SPI_CR_CS_MASK GENMASK(13, 10) /* Chip select */
27 #define ZYNQ_SPI_CR_BAUD_MASK GENMASK(5, 3) /* Baud rate div */
28 #define ZYNQ_SPI_CR_CPHA_MASK BIT(2) /* Clock phase */
29 #define ZYNQ_SPI_CR_CPOL_MASK BIT(1) /* Clock polarity */
30 #define ZYNQ_SPI_CR_MSTREN_MASK BIT(0) /* Mode select */
31 #define ZYNQ_SPI_IXR_RXNEMPTY_MASK BIT(4) /* RX_FIFO_not_empty */
32 #define ZYNQ_SPI_IXR_TXOW_MASK BIT(2) /* TX_FIFO_not_full */
33 #define ZYNQ_SPI_IXR_ALL_MASK GENMASK(6, 0) /* All IXR bits */
34 #define ZYNQ_SPI_ENR_SPI_EN_MASK BIT(0) /* SPI Enable */
36 #define ZYNQ_SPI_CR_BAUD_MAX 8 /* Baud rate divisor max val */
37 #define ZYNQ_SPI_CR_BAUD_SHIFT 3 /* Baud rate divisor shift */
38 #define ZYNQ_SPI_CR_SS_SHIFT 10 /* Slave select shift */
40 #define ZYNQ_SPI_FIFO_DEPTH 128
41 #define 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->plat;
79 const void *blob = gd->fdt_blob;
80 int node = dev_of_offset(bus);
82 plat->regs = dev_read_addr_ptr(bus);
84 plat->deactivate_delay_us = fdtdec_get_int(blob, node,
85 "spi-deactivate-delay", 0);
86 plat->activate_delay_us = fdtdec_get_int(blob, node,
87 "spi-activate-delay", 0);
92 static void zynq_spi_init_hw(struct zynq_spi_priv *priv)
94 struct zynq_spi_regs *regs = priv->regs;
98 confr = ZYNQ_SPI_ENR_SPI_EN_MASK;
99 writel(~confr, ®s->enr);
101 /* Disable Interrupts */
102 writel(ZYNQ_SPI_IXR_ALL_MASK, ®s->idr);
105 while (readl(®s->isr) &
106 ZYNQ_SPI_IXR_RXNEMPTY_MASK)
109 /* Clear Interrupts */
110 writel(ZYNQ_SPI_IXR_ALL_MASK, ®s->isr);
112 /* Manual slave select and Auto start */
113 confr = ZYNQ_SPI_CR_MCS_MASK | ZYNQ_SPI_CR_CS_MASK |
114 ZYNQ_SPI_CR_MSTREN_MASK;
115 confr &= ~ZYNQ_SPI_CR_MSA_MASK;
116 writel(confr, ®s->cr);
119 writel(ZYNQ_SPI_ENR_SPI_EN_MASK, ®s->enr);
122 static int zynq_spi_probe(struct udevice *bus)
124 struct zynq_spi_platdata *plat = dev_get_plat(bus);
125 struct zynq_spi_priv *priv = dev_get_priv(bus);
130 priv->regs = plat->regs;
131 priv->fifo_depth = ZYNQ_SPI_FIFO_DEPTH;
133 ret = clk_get_by_name(bus, "ref_clk", &clk);
135 dev_err(bus, "failed to get clock\n");
139 clock = clk_get_rate(&clk);
140 if (IS_ERR_VALUE(clock)) {
141 dev_err(bus, "failed to get rate\n");
145 ret = clk_enable(&clk);
146 if (ret && ret != -ENOSYS) {
147 dev_err(bus, "failed to enable clock\n");
151 /* init the zynq spi hw */
152 zynq_spi_init_hw(priv);
154 plat->frequency = clock;
155 plat->speed_hz = plat->frequency / 2;
157 debug("%s: max-frequency=%d\n", __func__, plat->speed_hz);
162 static void spi_cs_activate(struct udevice *dev)
164 struct udevice *bus = dev->parent;
165 struct zynq_spi_platdata *plat = bus->plat;
166 struct zynq_spi_priv *priv = dev_get_priv(bus);
167 struct zynq_spi_regs *regs = priv->regs;
170 /* If it's too soon to do another transaction, wait */
171 if (plat->deactivate_delay_us && priv->last_transaction_us) {
172 ulong delay_us; /* The delay completed so far */
173 delay_us = timer_get_us() - priv->last_transaction_us;
174 if (delay_us < plat->deactivate_delay_us)
175 udelay(plat->deactivate_delay_us - delay_us);
178 clrbits_le32(®s->cr, ZYNQ_SPI_CR_CS_MASK);
179 cr = readl(®s->cr);
181 * CS cal logic: CS[13:10]
186 cr |= (~(1 << priv->cs) << ZYNQ_SPI_CR_SS_SHIFT) & ZYNQ_SPI_CR_CS_MASK;
187 writel(cr, ®s->cr);
189 if (plat->activate_delay_us)
190 udelay(plat->activate_delay_us);
193 static void spi_cs_deactivate(struct udevice *dev)
195 struct udevice *bus = dev->parent;
196 struct zynq_spi_platdata *plat = bus->plat;
197 struct zynq_spi_priv *priv = dev_get_priv(bus);
198 struct zynq_spi_regs *regs = priv->regs;
200 setbits_le32(®s->cr, ZYNQ_SPI_CR_CS_MASK);
202 /* Remember time of this transaction so we can honour the bus delay */
203 if (plat->deactivate_delay_us)
204 priv->last_transaction_us = timer_get_us();
207 static int zynq_spi_claim_bus(struct udevice *dev)
209 struct udevice *bus = dev->parent;
210 struct zynq_spi_priv *priv = dev_get_priv(bus);
211 struct zynq_spi_regs *regs = priv->regs;
213 writel(ZYNQ_SPI_ENR_SPI_EN_MASK, ®s->enr);
218 static int zynq_spi_release_bus(struct udevice *dev)
220 struct udevice *bus = dev->parent;
221 struct zynq_spi_priv *priv = dev_get_priv(bus);
222 struct zynq_spi_regs *regs = priv->regs;
225 confr = ZYNQ_SPI_ENR_SPI_EN_MASK;
226 writel(~confr, ®s->enr);
231 static int zynq_spi_xfer(struct udevice *dev, unsigned int bitlen,
232 const void *dout, void *din, unsigned long flags)
234 struct udevice *bus = dev->parent;
235 struct zynq_spi_priv *priv = dev_get_priv(bus);
236 struct zynq_spi_regs *regs = priv->regs;
237 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_plat(dev);
238 u32 len = bitlen / 8;
239 u32 tx_len = len, rx_len = len, tx_tvl;
240 const u8 *tx_buf = dout;
241 u8 *rx_buf = din, buf;
244 debug("spi_xfer: bus:%i cs:%i bitlen:%i len:%i flags:%lx\n",
245 bus->seq, slave_plat->cs, bitlen, len, flags);
248 debug("spi_xfer: Non byte aligned SPI transfer\n");
252 priv->cs = slave_plat->cs;
253 if (flags & SPI_XFER_BEGIN)
254 spi_cs_activate(dev);
257 /* Write the data into TX FIFO - tx threshold is fifo_depth */
259 while ((tx_tvl < priv->fifo_depth) && tx_len) {
264 writel(buf, ®s->txdr);
269 /* Check TX FIFO completion */
271 status = readl(®s->isr);
272 while (!(status & ZYNQ_SPI_IXR_TXOW_MASK)) {
273 if (get_timer(ts) > ZYNQ_SPI_WAIT) {
274 printf("spi_xfer: Timeout! TX FIFO not full\n");
277 status = readl(®s->isr);
280 /* Read the data from RX FIFO */
281 status = readl(®s->isr);
282 while ((status & ZYNQ_SPI_IXR_RXNEMPTY_MASK) && rx_len) {
283 buf = readl(®s->rxdr);
286 status = readl(®s->isr);
291 if (flags & SPI_XFER_END)
292 spi_cs_deactivate(dev);
297 static int zynq_spi_set_speed(struct udevice *bus, uint speed)
299 struct zynq_spi_platdata *plat = bus->plat;
300 struct zynq_spi_priv *priv = dev_get_priv(bus);
301 struct zynq_spi_regs *regs = priv->regs;
303 u8 baud_rate_val = 0;
305 if (speed > plat->frequency)
306 speed = plat->frequency;
308 /* Set the clock frequency */
309 confr = readl(®s->cr);
311 /* Set baudrate x8, if the freq is 0 */
313 } else if (plat->speed_hz != speed) {
314 while ((baud_rate_val < ZYNQ_SPI_CR_BAUD_MAX) &&
316 (2 << baud_rate_val)) > speed))
318 plat->speed_hz = speed / (2 << baud_rate_val);
320 confr &= ~ZYNQ_SPI_CR_BAUD_MASK;
321 confr |= (baud_rate_val << ZYNQ_SPI_CR_BAUD_SHIFT);
323 writel(confr, ®s->cr);
326 debug("zynq_spi_set_speed: regs=%p, speed=%d\n",
327 priv->regs, priv->freq);
332 static int zynq_spi_set_mode(struct udevice *bus, uint mode)
334 struct zynq_spi_priv *priv = dev_get_priv(bus);
335 struct zynq_spi_regs *regs = priv->regs;
338 /* Set the SPI Clock phase and polarities */
339 confr = readl(®s->cr);
340 confr &= ~(ZYNQ_SPI_CR_CPHA_MASK | ZYNQ_SPI_CR_CPOL_MASK);
343 confr |= ZYNQ_SPI_CR_CPHA_MASK;
345 confr |= ZYNQ_SPI_CR_CPOL_MASK;
347 writel(confr, ®s->cr);
350 debug("zynq_spi_set_mode: regs=%p, mode=%d\n", priv->regs, priv->mode);
355 static const struct dm_spi_ops zynq_spi_ops = {
356 .claim_bus = zynq_spi_claim_bus,
357 .release_bus = zynq_spi_release_bus,
358 .xfer = zynq_spi_xfer,
359 .set_speed = zynq_spi_set_speed,
360 .set_mode = zynq_spi_set_mode,
363 static const struct udevice_id zynq_spi_ids[] = {
364 { .compatible = "xlnx,zynq-spi-r1p6" },
365 { .compatible = "cdns,spi-r1p6" },
369 U_BOOT_DRIVER(zynq_spi) = {
372 .of_match = zynq_spi_ids,
373 .ops = &zynq_spi_ops,
374 .ofdata_to_platdata = zynq_spi_ofdata_to_platdata,
375 .plat_auto = sizeof(struct zynq_spi_platdata),
376 .priv_auto = sizeof(struct zynq_spi_priv),
377 .probe = zynq_spi_probe,