1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2013 Xilinx, Inc.
6 * Xilinx Zynq PS SPI controller driver (master mode only)
10 #include <dm/device_compat.h>
16 #include <asm/global_data.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 {
56 /* zynq spi platform data */
57 struct zynq_spi_plat {
58 struct zynq_spi_regs *regs;
59 u32 frequency; /* input frequency */
61 uint deactivate_delay_us; /* Delay to wait after deactivate */
62 uint activate_delay_us; /* Delay to wait after activate */
66 struct zynq_spi_priv {
67 struct zynq_spi_regs *regs;
70 ulong last_transaction_us; /* Time of last transaction end */
72 u32 freq; /* required frequency */
75 static int zynq_spi_of_to_plat(struct udevice *bus)
77 struct zynq_spi_plat *plat = dev_get_plat(bus);
78 const void *blob = gd->fdt_blob;
79 int node = dev_of_offset(bus);
81 plat->regs = dev_read_addr_ptr(bus);
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);
91 static void zynq_spi_init_hw(struct zynq_spi_priv *priv)
93 struct zynq_spi_regs *regs = priv->regs;
97 confr = ZYNQ_SPI_ENR_SPI_EN_MASK;
98 writel(~confr, ®s->enr);
100 /* Disable Interrupts */
101 writel(ZYNQ_SPI_IXR_ALL_MASK, ®s->idr);
104 while (readl(®s->isr) &
105 ZYNQ_SPI_IXR_RXNEMPTY_MASK)
108 /* Clear Interrupts */
109 writel(ZYNQ_SPI_IXR_ALL_MASK, ®s->isr);
111 /* Manual slave select and Auto start */
112 confr = ZYNQ_SPI_CR_MCS_MASK | ZYNQ_SPI_CR_CS_MASK |
113 ZYNQ_SPI_CR_MSTREN_MASK;
114 confr &= ~ZYNQ_SPI_CR_MSA_MASK;
115 writel(confr, ®s->cr);
118 writel(ZYNQ_SPI_ENR_SPI_EN_MASK, ®s->enr);
121 static int zynq_spi_probe(struct udevice *bus)
123 struct zynq_spi_plat *plat = dev_get_plat(bus);
124 struct zynq_spi_priv *priv = dev_get_priv(bus);
129 priv->regs = plat->regs;
130 priv->fifo_depth = ZYNQ_SPI_FIFO_DEPTH;
132 ret = clk_get_by_name(bus, "ref_clk", &clk);
134 dev_err(bus, "failed to get clock\n");
138 clock = clk_get_rate(&clk);
139 if (IS_ERR_VALUE(clock)) {
140 dev_err(bus, "failed to get rate\n");
144 ret = clk_enable(&clk);
146 dev_err(bus, "failed to enable clock\n");
150 /* init the zynq spi hw */
151 zynq_spi_init_hw(priv);
153 plat->frequency = clock;
154 plat->speed_hz = plat->frequency / 2;
156 debug("%s: max-frequency=%d\n", __func__, plat->speed_hz);
161 static void spi_cs_activate(struct udevice *dev)
163 struct udevice *bus = dev->parent;
164 struct zynq_spi_plat *plat = dev_get_plat(bus);
165 struct zynq_spi_priv *priv = dev_get_priv(bus);
166 struct zynq_spi_regs *regs = priv->regs;
169 /* If it's too soon to do another transaction, wait */
170 if (plat->deactivate_delay_us && priv->last_transaction_us) {
171 ulong delay_us; /* The delay completed so far */
172 delay_us = timer_get_us() - priv->last_transaction_us;
173 if (delay_us < plat->deactivate_delay_us)
174 udelay(plat->deactivate_delay_us - delay_us);
177 clrbits_le32(®s->cr, ZYNQ_SPI_CR_CS_MASK);
178 cr = readl(®s->cr);
180 * CS cal logic: CS[13:10]
185 cr |= (~(1 << priv->cs) << ZYNQ_SPI_CR_SS_SHIFT) & ZYNQ_SPI_CR_CS_MASK;
186 writel(cr, ®s->cr);
188 if (plat->activate_delay_us)
189 udelay(plat->activate_delay_us);
192 static void spi_cs_deactivate(struct udevice *dev)
194 struct udevice *bus = dev->parent;
195 struct zynq_spi_plat *plat = dev_get_plat(bus);
196 struct zynq_spi_priv *priv = dev_get_priv(bus);
197 struct zynq_spi_regs *regs = priv->regs;
199 setbits_le32(®s->cr, ZYNQ_SPI_CR_CS_MASK);
201 /* Remember time of this transaction so we can honour the bus delay */
202 if (plat->deactivate_delay_us)
203 priv->last_transaction_us = timer_get_us();
206 static int zynq_spi_claim_bus(struct udevice *dev)
208 struct udevice *bus = dev->parent;
209 struct zynq_spi_priv *priv = dev_get_priv(bus);
210 struct zynq_spi_regs *regs = priv->regs;
212 writel(ZYNQ_SPI_ENR_SPI_EN_MASK, ®s->enr);
217 static int zynq_spi_release_bus(struct udevice *dev)
219 struct udevice *bus = dev->parent;
220 struct zynq_spi_priv *priv = dev_get_priv(bus);
221 struct zynq_spi_regs *regs = priv->regs;
224 confr = ZYNQ_SPI_ENR_SPI_EN_MASK;
225 writel(~confr, ®s->enr);
230 static int zynq_spi_xfer(struct udevice *dev, unsigned int bitlen,
231 const void *dout, void *din, unsigned long flags)
233 struct udevice *bus = dev->parent;
234 struct zynq_spi_priv *priv = dev_get_priv(bus);
235 struct zynq_spi_regs *regs = priv->regs;
236 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
237 u32 len = bitlen / 8;
238 u32 tx_len = len, rx_len = len, tx_tvl;
239 const u8 *tx_buf = dout;
240 u8 *rx_buf = din, buf;
243 debug("spi_xfer: bus:%i cs[0]:%i bitlen:%i len:%i flags:%lx\n",
244 dev_seq(bus), slave_plat->cs[0], bitlen, len, flags);
247 debug("spi_xfer: Non byte aligned SPI transfer\n");
251 priv->cs = slave_plat->cs[0];
252 if (flags & SPI_XFER_BEGIN)
253 spi_cs_activate(dev);
256 /* Write the data into TX FIFO - tx threshold is fifo_depth */
258 while ((tx_tvl < priv->fifo_depth) && tx_len) {
263 writel(buf, ®s->txdr);
268 /* Check TX FIFO completion */
270 status = readl(®s->isr);
271 while (!(status & ZYNQ_SPI_IXR_TXOW_MASK)) {
272 if (get_timer(ts) > ZYNQ_SPI_WAIT) {
273 printf("spi_xfer: Timeout! TX FIFO not full\n");
276 status = readl(®s->isr);
279 /* Read the data from RX FIFO */
280 status = readl(®s->isr);
281 while ((status & ZYNQ_SPI_IXR_RXNEMPTY_MASK) && rx_len) {
282 buf = readl(®s->rxdr);
285 status = readl(®s->isr);
290 if (flags & SPI_XFER_END)
291 spi_cs_deactivate(dev);
296 static int zynq_spi_set_speed(struct udevice *bus, uint speed)
298 struct zynq_spi_plat *plat = dev_get_plat(bus);
299 struct zynq_spi_priv *priv = dev_get_priv(bus);
300 struct zynq_spi_regs *regs = priv->regs;
302 u8 baud_rate_val = 0;
304 if (speed > plat->frequency)
305 speed = plat->frequency;
307 /* Set the clock frequency */
308 confr = readl(®s->cr);
310 /* Set baudrate x8, if the freq is 0 */
312 } else if (plat->speed_hz != speed) {
313 while ((baud_rate_val < ZYNQ_SPI_CR_BAUD_MAX) &&
315 (2 << baud_rate_val)) > speed))
317 plat->speed_hz = speed / (2 << baud_rate_val);
319 confr &= ~ZYNQ_SPI_CR_BAUD_MASK;
320 confr |= (baud_rate_val << ZYNQ_SPI_CR_BAUD_SHIFT);
322 writel(confr, ®s->cr);
325 debug("zynq_spi_set_speed: regs=%p, speed=%d\n",
326 priv->regs, priv->freq);
331 static int zynq_spi_set_mode(struct udevice *bus, uint mode)
333 struct zynq_spi_priv *priv = dev_get_priv(bus);
334 struct zynq_spi_regs *regs = priv->regs;
337 /* Set the SPI Clock phase and polarities */
338 confr = readl(®s->cr);
339 confr &= ~(ZYNQ_SPI_CR_CPHA_MASK | ZYNQ_SPI_CR_CPOL_MASK);
342 confr |= ZYNQ_SPI_CR_CPHA_MASK;
344 confr |= ZYNQ_SPI_CR_CPOL_MASK;
346 writel(confr, ®s->cr);
349 debug("zynq_spi_set_mode: regs=%p, mode=%d\n", priv->regs, priv->mode);
354 static const struct dm_spi_ops zynq_spi_ops = {
355 .claim_bus = zynq_spi_claim_bus,
356 .release_bus = zynq_spi_release_bus,
357 .xfer = zynq_spi_xfer,
358 .set_speed = zynq_spi_set_speed,
359 .set_mode = zynq_spi_set_mode,
362 static const struct udevice_id zynq_spi_ids[] = {
363 { .compatible = "xlnx,zynq-spi-r1p6" },
364 { .compatible = "cdns,spi-r1p6" },
368 U_BOOT_DRIVER(zynq_spi) = {
371 .of_match = zynq_spi_ids,
372 .ops = &zynq_spi_ops,
373 .of_to_plat = zynq_spi_of_to_plat,
374 .plat_auto = sizeof(struct zynq_spi_plat),
375 .priv_auto = sizeof(struct zynq_spi_priv),
376 .probe = zynq_spi_probe,