1 // SPDX-License-Identifier: GPL-2.0+
5 * Supports 8 bit SPI transfers only, with or w/o FIFO
7 * Based on bfin_spi.c, by way of altera_spi.c
12 * Copyright (c) 2005-2008 Analog Devices Inc.
24 #include <linux/bitops.h>
27 * [0]: http://www.xilinx.com/support/documentation
29 * Xilinx SPI Register Definitions
30 * [1]: [0]/ip_documentation/xps_spi.pdf
31 * page 8, Register Descriptions
32 * [2]: [0]/ip_documentation/axi_spi_ds742.pdf
33 * page 7, Register Overview Table
36 /* SPI Control Register (spicr), [1] p9, [2] p8 */
37 #define SPICR_LSB_FIRST BIT(9)
38 #define SPICR_MASTER_INHIBIT BIT(8)
39 #define SPICR_MANUAL_SS BIT(7)
40 #define SPICR_RXFIFO_RESEST BIT(6)
41 #define SPICR_TXFIFO_RESEST BIT(5)
42 #define SPICR_CPHA BIT(4)
43 #define SPICR_CPOL BIT(3)
44 #define SPICR_MASTER_MODE BIT(2)
45 #define SPICR_SPE BIT(1)
46 #define SPICR_LOOP BIT(0)
48 /* SPI Status Register (spisr), [1] p11, [2] p10 */
49 #define SPISR_SLAVE_MODE_SELECT BIT(5)
50 #define SPISR_MODF BIT(4)
51 #define SPISR_TX_FULL BIT(3)
52 #define SPISR_TX_EMPTY BIT(2)
53 #define SPISR_RX_FULL BIT(1)
54 #define SPISR_RX_EMPTY BIT(0)
56 /* SPI Data Transmit Register (spidtr), [1] p12, [2] p12 */
57 #define SPIDTR_8BIT_MASK GENMASK(7, 0)
58 #define SPIDTR_16BIT_MASK GENMASK(15, 0)
59 #define SPIDTR_32BIT_MASK GENMASK(31, 0)
61 /* SPI Data Receive Register (spidrr), [1] p12, [2] p12 */
62 #define SPIDRR_8BIT_MASK GENMASK(7, 0)
63 #define SPIDRR_16BIT_MASK GENMASK(15, 0)
64 #define SPIDRR_32BIT_MASK GENMASK(31, 0)
66 /* SPI Slave Select Register (spissr), [1] p13, [2] p13 */
67 #define SPISSR_MASK(cs) (1 << (cs))
68 #define SPISSR_ACT(cs) ~SPISSR_MASK(cs)
69 #define SPISSR_OFF (~0U)
71 /* SPI Software Reset Register (ssr) */
72 #define SPISSR_RESET_VALUE 0x0a
74 #define XILSPI_MAX_XFER_BITS 8
75 #define XILSPI_SPICR_DFLT_ON (SPICR_MANUAL_SS | SPICR_MASTER_MODE | \
76 SPICR_SPE | SPICR_MASTER_INHIBIT)
77 #define XILSPI_SPICR_DFLT_OFF (SPICR_MASTER_INHIBIT | SPICR_MANUAL_SS)
79 #define XILINX_SPI_IDLE_VAL GENMASK(7, 0)
81 #define XILINX_SPISR_TIMEOUT 10000 /* in milliseconds */
83 /* xilinx spi register set */
84 struct xilinx_spi_regs {
86 u32 dgier; /* Device Global Interrupt Enable Register (DGIER) */
87 u32 ipisr; /* IP Interrupt Status Register (IPISR) */
89 u32 ipier; /* IP Interrupt Enable Register (IPIER) */
91 u32 srr; /* Softare Reset Register (SRR) */
93 u32 spicr; /* SPI Control Register (SPICR) */
94 u32 spisr; /* SPI Status Register (SPISR) */
95 u32 spidtr; /* SPI Data Transmit Register (SPIDTR) */
96 u32 spidrr; /* SPI Data Receive Register (SPIDRR) */
97 u32 spissr; /* SPI Slave Select Register (SPISSR) */
98 u32 spitfor; /* SPI Transmit FIFO Occupancy Register (SPITFOR) */
99 u32 spirfor; /* SPI Receive FIFO Occupancy Register (SPIRFOR) */
102 /* xilinx spi priv */
103 struct xilinx_spi_priv {
104 struct xilinx_spi_regs *regs;
107 unsigned int fifo_depth;
111 static int xilinx_spi_find_buffer_size(struct xilinx_spi_regs *regs)
117 * Before the buffer_size detection reset the core
118 * to make sure to start with a clean state.
120 writel(SPISSR_RESET_VALUE, ®s->srr);
122 /* Fill the Tx FIFO with as many words as possible */
124 writel(0, ®s->spidtr);
125 sr = readl(®s->spisr);
127 } while (!(sr & SPISR_TX_FULL));
132 static int xilinx_spi_probe(struct udevice *bus)
134 struct xilinx_spi_priv *priv = dev_get_priv(bus);
135 struct xilinx_spi_regs *regs;
137 regs = priv->regs = dev_read_addr_ptr(bus);
138 priv->fifo_depth = dev_read_u32_default(bus, "fifo-size", 0);
139 if (!priv->fifo_depth)
140 priv->fifo_depth = xilinx_spi_find_buffer_size(regs);
142 writel(SPISSR_RESET_VALUE, ®s->srr);
146 * Enable Manual Slave Select Assertion,
147 * Set SPI controller into master mode, and enable it
149 writel(SPICR_RXFIFO_RESEST | SPICR_TXFIFO_RESEST |
150 SPICR_MANUAL_SS | SPICR_MASTER_MODE | SPICR_SPE,
156 static void spi_cs_activate(struct udevice *dev, uint cs)
158 struct udevice *bus = dev_get_parent(dev);
159 struct xilinx_spi_priv *priv = dev_get_priv(bus);
160 struct xilinx_spi_regs *regs = priv->regs;
162 writel(SPISSR_ACT(cs), ®s->spissr);
165 static void spi_cs_deactivate(struct udevice *dev)
167 struct udevice *bus = dev_get_parent(dev);
168 struct xilinx_spi_priv *priv = dev_get_priv(bus);
169 struct xilinx_spi_regs *regs = priv->regs;
172 reg = readl(®s->spicr) | SPICR_RXFIFO_RESEST | SPICR_TXFIFO_RESEST;
173 writel(reg, ®s->spicr);
174 writel(SPISSR_OFF, ®s->spissr);
177 static int xilinx_spi_claim_bus(struct udevice *dev)
179 struct udevice *bus = dev_get_parent(dev);
180 struct xilinx_spi_priv *priv = dev_get_priv(bus);
181 struct xilinx_spi_regs *regs = priv->regs;
183 writel(SPISSR_OFF, ®s->spissr);
184 writel(XILSPI_SPICR_DFLT_ON, ®s->spicr);
189 static int xilinx_spi_release_bus(struct udevice *dev)
191 struct udevice *bus = dev_get_parent(dev);
192 struct xilinx_spi_priv *priv = dev_get_priv(bus);
193 struct xilinx_spi_regs *regs = priv->regs;
195 writel(SPISSR_OFF, ®s->spissr);
196 writel(XILSPI_SPICR_DFLT_OFF, ®s->spicr);
201 static u32 xilinx_spi_fill_txfifo(struct udevice *bus, const u8 *txp,
204 struct xilinx_spi_priv *priv = dev_get_priv(bus);
205 struct xilinx_spi_regs *regs = priv->regs;
209 while (txbytes && !(readl(®s->spisr) & SPISR_TX_FULL) &&
210 i < priv->fifo_depth) {
211 d = txp ? *txp++ : XILINX_SPI_IDLE_VAL;
212 debug("spi_xfer: tx:%x ", d);
213 /* write out and wait for processing (receive data) */
214 writel(d & SPIDTR_8BIT_MASK, ®s->spidtr);
222 static u32 xilinx_spi_read_rxfifo(struct udevice *bus, u8 *rxp, u32 rxbytes)
224 struct xilinx_spi_priv *priv = dev_get_priv(bus);
225 struct xilinx_spi_regs *regs = priv->regs;
229 while (rxbytes && !(readl(®s->spisr) & SPISR_RX_EMPTY)) {
230 d = readl(®s->spidrr) & SPIDRR_8BIT_MASK;
233 debug("spi_xfer: rx:%x\n", d);
242 static int start_transfer(struct udevice *dev, const void *dout, void *din, u32 len)
244 struct udevice *bus = dev->parent;
245 struct xilinx_spi_priv *priv = dev_get_priv(bus);
246 struct xilinx_spi_regs *regs = priv->regs;
247 u32 count, txbytes, rxbytes;
249 const unsigned char *txp = (const unsigned char *)dout;
250 unsigned char *rxp = (unsigned char *)din;
254 while (txbytes || rxbytes) {
255 /* Disable master transaction */
256 reg = readl(®s->spicr) | SPICR_MASTER_INHIBIT;
257 writel(reg, ®s->spicr);
258 count = xilinx_spi_fill_txfifo(bus, txp, txbytes);
259 /* Enable master transaction */
260 reg = readl(®s->spicr) & ~SPICR_MASTER_INHIBIT;
261 writel(reg, ®s->spicr);
266 ret = wait_for_bit_le32(®s->spisr, SPISR_TX_EMPTY, true,
267 XILINX_SPISR_TIMEOUT, false);
269 printf("XILSPI error: Xfer timeout\n");
273 reg = readl(®s->spicr) | SPICR_MASTER_INHIBIT;
274 writel(reg, ®s->spicr);
275 count = xilinx_spi_read_rxfifo(bus, rxp, rxbytes);
284 static void xilinx_spi_startup_block(struct udevice *dev)
286 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
288 unsigned char rxp[8];
291 * Perform a dummy read as a work around for
292 * the startup block issue.
294 spi_cs_activate(dev, slave_plat->cs);
296 start_transfer(dev, (void *)&txp, NULL, 1);
298 start_transfer(dev, NULL, (void *)rxp, 6);
300 spi_cs_deactivate(dev);
303 static int xilinx_spi_xfer(struct udevice *dev, unsigned int bitlen,
304 const void *dout, void *din, unsigned long flags)
306 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
309 spi_cs_activate(dev, slave_plat->cs);
310 ret = start_transfer(dev, dout, din, bitlen / 8);
311 spi_cs_deactivate(dev);
315 static int xilinx_spi_mem_exec_op(struct spi_slave *spi,
316 const struct spi_mem_op *op)
318 struct dm_spi_slave_plat *slave_plat =
319 dev_get_parent_plat(spi->dev);
324 * This is the work around for the startup block issue in
325 * the spi controller. SPI clock is passing through STARTUP
326 * block to FLASH. STARTUP block don't provide clock as soon
327 * as QSPI provides command. So first command fails.
330 xilinx_spi_startup_block(spi->dev);
334 spi_cs_activate(spi->dev, slave_plat->cs);
336 if (op->cmd.opcode) {
337 ret = start_transfer(spi->dev, (void *)&op->cmd.opcode,
342 if (op->addr.nbytes) {
346 for (i = 0; i < op->addr.nbytes; i++)
347 addr_buf[i] = op->addr.val >>
348 (8 * (op->addr.nbytes - i - 1));
350 ret = start_transfer(spi->dev, (void *)addr_buf, NULL,
355 if (op->dummy.nbytes) {
356 dummy_len = (op->dummy.nbytes * op->data.buswidth) /
359 ret = start_transfer(spi->dev, NULL, NULL, dummy_len);
363 if (op->data.nbytes) {
364 if (op->data.dir == SPI_MEM_DATA_IN) {
365 ret = start_transfer(spi->dev, NULL,
366 op->data.buf.in, op->data.nbytes);
368 ret = start_transfer(spi->dev, op->data.buf.out,
369 NULL, op->data.nbytes);
375 spi_cs_deactivate(spi->dev);
380 static int xilinx_qspi_check_buswidth(struct spi_slave *slave, u8 width)
382 u32 mode = slave->mode;
388 if (mode & SPI_RX_DUAL)
392 if (mode & SPI_RX_QUAD)
400 static bool xilinx_qspi_mem_exec_op(struct spi_slave *slave,
401 const struct spi_mem_op *op)
403 if (xilinx_qspi_check_buswidth(slave, op->cmd.buswidth))
406 if (op->addr.nbytes &&
407 xilinx_qspi_check_buswidth(slave, op->addr.buswidth))
410 if (op->dummy.nbytes &&
411 xilinx_qspi_check_buswidth(slave, op->dummy.buswidth))
414 if (op->data.dir != SPI_MEM_NO_DATA &&
415 xilinx_qspi_check_buswidth(slave, op->data.buswidth))
421 static int xilinx_spi_set_speed(struct udevice *bus, uint speed)
423 struct xilinx_spi_priv *priv = dev_get_priv(bus);
427 debug("%s: regs=%p, speed=%d\n", __func__, priv->regs, priv->freq);
432 static int xilinx_spi_set_mode(struct udevice *bus, uint mode)
434 struct xilinx_spi_priv *priv = dev_get_priv(bus);
435 struct xilinx_spi_regs *regs = priv->regs;
438 spicr = readl(®s->spicr);
439 if (mode & SPI_LSB_FIRST)
440 spicr |= SPICR_LSB_FIRST;
448 writel(spicr, ®s->spicr);
451 debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode);
456 static const struct spi_controller_mem_ops xilinx_spi_mem_ops = {
457 .exec_op = xilinx_spi_mem_exec_op,
458 .supports_op = xilinx_qspi_mem_exec_op,
461 static const struct dm_spi_ops xilinx_spi_ops = {
462 .claim_bus = xilinx_spi_claim_bus,
463 .release_bus = xilinx_spi_release_bus,
464 .xfer = xilinx_spi_xfer,
465 .set_speed = xilinx_spi_set_speed,
466 .set_mode = xilinx_spi_set_mode,
467 .mem_ops = &xilinx_spi_mem_ops,
470 static const struct udevice_id xilinx_spi_ids[] = {
471 { .compatible = "xlnx,xps-spi-2.00.a" },
472 { .compatible = "xlnx,xps-spi-2.00.b" },
476 U_BOOT_DRIVER(xilinx_spi) = {
477 .name = "xilinx_spi",
479 .of_match = xilinx_spi_ids,
480 .ops = &xilinx_spi_ops,
481 .priv_auto = sizeof(struct xilinx_spi_priv),
482 .probe = xilinx_spi_probe,