blob: 0e7fa3a4525d1092812ea7ea82f0c98160757ff4 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stephan Linzfc77d512012-07-29 00:25:35 +02002/*
3 * Xilinx SPI driver
4 *
Jagan Teki48a0dbd2015-06-27 00:51:27 +05305 * Supports 8 bit SPI transfers only, with or w/o FIFO
Stephan Linzfc77d512012-07-29 00:25:35 +02006 *
Jagan Teki48a0dbd2015-06-27 00:51:27 +05307 * Based on bfin_spi.c, by way of altera_spi.c
Jagan Tekifdc2b3d2015-06-29 13:15:18 +05308 * Copyright (c) 2015 Jagan Teki <jteki@openedev.com>
Stephan Linzfc77d512012-07-29 00:25:35 +02009 * Copyright (c) 2012 Stephan Linz <linz@li-pro.net>
Jagan Teki48a0dbd2015-06-27 00:51:27 +053010 * Copyright (c) 2010 Graeme Smecher <graeme.smecher@mail.mcgill.ca>
11 * Copyright (c) 2010 Thomas Chou <thomas@wytron.com.tw>
12 * Copyright (c) 2005-2008 Analog Devices Inc.
Stephan Linzfc77d512012-07-29 00:25:35 +020013 */
Jagan Teki48a0dbd2015-06-27 00:51:27 +053014
Stephan Linzfc77d512012-07-29 00:25:35 +020015#include <config.h>
Jagan Tekifdc2b3d2015-06-29 13:15:18 +053016#include <dm.h>
17#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060018#include <log.h>
Stephan Linzfc77d512012-07-29 00:25:35 +020019#include <malloc.h>
20#include <spi.h>
T Karthik Reddy50963802022-07-16 12:28:46 +053021#include <spi-mem.h>
Jagan Teki41fcbba2015-06-27 00:51:37 +053022#include <asm/io.h>
Vipul Kumar90098ba2018-06-30 08:15:18 +053023#include <wait_bit.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060024#include <linux/bitops.h>
Stephan Linzfc77d512012-07-29 00:25:35 +020025
Jagan Teki23e281d2015-06-27 00:51:26 +053026/*
Jagan Teki48a0dbd2015-06-27 00:51:27 +053027 * [0]: http://www.xilinx.com/support/documentation
Jagan Teki23e281d2015-06-27 00:51:26 +053028 *
Jagan Teki48a0dbd2015-06-27 00:51:27 +053029 * Xilinx SPI Register Definitions
Jagan Teki23e281d2015-06-27 00:51:26 +053030 * [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
34 */
Jagan Teki23e281d2015-06-27 00:51:26 +053035
36/* SPI Control Register (spicr), [1] p9, [2] p8 */
Jagan Tekif0a01412015-10-23 01:39:31 +053037#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)
Jagan Teki23e281d2015-06-27 00:51:26 +053047
48/* SPI Status Register (spisr), [1] p11, [2] p10 */
Jagan Tekif0a01412015-10-23 01:39:31 +053049#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)
Jagan Teki23e281d2015-06-27 00:51:26 +053055
56/* SPI Data Transmit Register (spidtr), [1] p12, [2] p12 */
Jagan Tekia2888d02015-10-23 01:03:44 +053057#define SPIDTR_8BIT_MASK GENMASK(7, 0)
58#define SPIDTR_16BIT_MASK GENMASK(15, 0)
59#define SPIDTR_32BIT_MASK GENMASK(31, 0)
Jagan Teki23e281d2015-06-27 00:51:26 +053060
61/* SPI Data Receive Register (spidrr), [1] p12, [2] p12 */
Jagan Tekia2888d02015-10-23 01:03:44 +053062#define SPIDRR_8BIT_MASK GENMASK(7, 0)
63#define SPIDRR_16BIT_MASK GENMASK(15, 0)
64#define SPIDRR_32BIT_MASK GENMASK(31, 0)
Jagan Teki23e281d2015-06-27 00:51:26 +053065
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)
Mayuresh Chitale22c97242023-11-16 22:13:34 +053069#define SPISSR_OFF (~0U)
Jagan Teki23e281d2015-06-27 00:51:26 +053070
Jagan Teki23e281d2015-06-27 00:51:26 +053071/* SPI Software Reset Register (ssr) */
72#define SPISSR_RESET_VALUE 0x0a
73
Jagan Teki48a0dbd2015-06-27 00:51:27 +053074#define XILSPI_MAX_XFER_BITS 8
75#define XILSPI_SPICR_DFLT_ON (SPICR_MANUAL_SS | SPICR_MASTER_MODE | \
T Karthik Reddy50963802022-07-16 12:28:46 +053076 SPICR_SPE | SPICR_MASTER_INHIBIT)
Jagan Teki48a0dbd2015-06-27 00:51:27 +053077#define XILSPI_SPICR_DFLT_OFF (SPICR_MASTER_INHIBIT | SPICR_MANUAL_SS)
78
Ashok Reddy Somacaecfe62020-05-18 01:11:00 -060079#define XILINX_SPI_IDLE_VAL GENMASK(7, 0)
Jagan Teki48a0dbd2015-06-27 00:51:27 +053080
Vipul Kumar90098ba2018-06-30 08:15:18 +053081#define XILINX_SPISR_TIMEOUT 10000 /* in milliseconds */
82
Jagan Teki48a0dbd2015-06-27 00:51:27 +053083/* xilinx spi register set */
Jagan Tekifdc2b3d2015-06-29 13:15:18 +053084struct xilinx_spi_regs {
Jagan Teki48a0dbd2015-06-27 00:51:27 +053085 u32 __space0__[7];
86 u32 dgier; /* Device Global Interrupt Enable Register (DGIER) */
87 u32 ipisr; /* IP Interrupt Status Register (IPISR) */
88 u32 __space1__;
89 u32 ipier; /* IP Interrupt Enable Register (IPIER) */
90 u32 __space2__[5];
91 u32 srr; /* Softare Reset Register (SRR) */
92 u32 __space3__[7];
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) */
100};
101
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530102/* xilinx spi priv */
103struct xilinx_spi_priv {
104 struct xilinx_spi_regs *regs;
Jagan Teki23e281d2015-06-27 00:51:26 +0530105 unsigned int freq;
106 unsigned int mode;
Vipul Kumar90098ba2018-06-30 08:15:18 +0530107 unsigned int fifo_depth;
Vipul Kumarc7e2aae2018-06-30 08:15:19 +0530108 u8 startup;
Jagan Teki23e281d2015-06-27 00:51:26 +0530109};
110
Mayuresh Chitale732225a2023-11-16 22:13:36 +0530111static int xilinx_spi_find_buffer_size(struct xilinx_spi_regs *regs)
112{
113 u8 sr;
114 int n_words = 0;
115
116 /*
117 * Before the buffer_size detection reset the core
118 * to make sure to start with a clean state.
119 */
120 writel(SPISSR_RESET_VALUE, &regs->srr);
121
122 /* Fill the Tx FIFO with as many words as possible */
123 do {
124 writel(0, &regs->spidtr);
125 sr = readl(&regs->spisr);
126 n_words++;
127 } while (!(sr & SPISR_TX_FULL));
128
129 return n_words;
130}
131
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530132static int xilinx_spi_probe(struct udevice *bus)
Stephan Linzfc77d512012-07-29 00:25:35 +0200133{
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530134 struct xilinx_spi_priv *priv = dev_get_priv(bus);
Jiajie Chencf191e42023-02-27 23:09:39 +0800135 struct xilinx_spi_regs *regs;
Stephan Linzfc77d512012-07-29 00:25:35 +0200136
Johan Jonker8d5d8e02023-03-13 01:32:04 +0100137 regs = priv->regs = dev_read_addr_ptr(bus);
Vipul Kumar646b4602018-06-30 08:15:20 +0530138 priv->fifo_depth = dev_read_u32_default(bus, "fifo-size", 0);
Mayuresh Chitale732225a2023-11-16 22:13:36 +0530139 if (!priv->fifo_depth)
140 priv->fifo_depth = xilinx_spi_find_buffer_size(regs);
Vipul Kumar90098ba2018-06-30 08:15:18 +0530141
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530142 writel(SPISSR_RESET_VALUE, &regs->srr);
Stephan Linzfc77d512012-07-29 00:25:35 +0200143
T Karthik Reddy50963802022-07-16 12:28:46 +0530144 /*
145 * Reset RX & TX FIFO
146 * Enable Manual Slave Select Assertion,
147 * Set SPI controller into master mode, and enable it
148 */
149 writel(SPICR_RXFIFO_RESEST | SPICR_TXFIFO_RESEST |
150 SPICR_MANUAL_SS | SPICR_MASTER_MODE | SPICR_SPE,
151 &regs->spicr);
152
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530153 return 0;
Stephan Linzfc77d512012-07-29 00:25:35 +0200154}
155
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530156static void spi_cs_activate(struct udevice *dev, uint cs)
Stephan Linzfc77d512012-07-29 00:25:35 +0200157{
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530158 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;
Stephan Linzfc77d512012-07-29 00:25:35 +0200161
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530162 writel(SPISSR_ACT(cs), &regs->spissr);
Stephan Linzfc77d512012-07-29 00:25:35 +0200163}
164
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530165static void spi_cs_deactivate(struct udevice *dev)
Stephan Linzfc77d512012-07-29 00:25:35 +0200166{
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530167 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;
T Karthik Reddy50963802022-07-16 12:28:46 +0530170 u32 reg;
Stephan Linzfc77d512012-07-29 00:25:35 +0200171
T Karthik Reddy50963802022-07-16 12:28:46 +0530172 reg = readl(&regs->spicr) | SPICR_RXFIFO_RESEST | SPICR_TXFIFO_RESEST;
173 writel(reg, &regs->spicr);
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530174 writel(SPISSR_OFF, &regs->spissr);
Stephan Linzfc77d512012-07-29 00:25:35 +0200175}
176
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530177static int xilinx_spi_claim_bus(struct udevice *dev)
Stephan Linzfc77d512012-07-29 00:25:35 +0200178{
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530179 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;
Stephan Linzfc77d512012-07-29 00:25:35 +0200182
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530183 writel(SPISSR_OFF, &regs->spissr);
184 writel(XILSPI_SPICR_DFLT_ON, &regs->spicr);
Stephan Linzfc77d512012-07-29 00:25:35 +0200185
Stephan Linzfc77d512012-07-29 00:25:35 +0200186 return 0;
187}
188
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530189static int xilinx_spi_release_bus(struct udevice *dev)
Stephan Linzfc77d512012-07-29 00:25:35 +0200190{
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530191 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;
Stephan Linzfc77d512012-07-29 00:25:35 +0200194
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530195 writel(SPISSR_OFF, &regs->spissr);
196 writel(XILSPI_SPICR_DFLT_OFF, &regs->spicr);
197
198 return 0;
Stephan Linzfc77d512012-07-29 00:25:35 +0200199}
200
Vipul Kumar90098ba2018-06-30 08:15:18 +0530201static u32 xilinx_spi_fill_txfifo(struct udevice *bus, const u8 *txp,
202 u32 txbytes)
203{
204 struct xilinx_spi_priv *priv = dev_get_priv(bus);
205 struct xilinx_spi_regs *regs = priv->regs;
206 unsigned char d;
207 u32 i = 0;
208
209 while (txbytes && !(readl(&regs->spisr) & SPISR_TX_FULL) &&
210 i < priv->fifo_depth) {
Ashok Reddy Somacaecfe62020-05-18 01:11:00 -0600211 d = txp ? *txp++ : XILINX_SPI_IDLE_VAL;
Vipul Kumar90098ba2018-06-30 08:15:18 +0530212 debug("spi_xfer: tx:%x ", d);
213 /* write out and wait for processing (receive data) */
214 writel(d & SPIDTR_8BIT_MASK, &regs->spidtr);
215 txbytes--;
216 i++;
217 }
218
219 return i;
220}
221
222static u32 xilinx_spi_read_rxfifo(struct udevice *bus, u8 *rxp, u32 rxbytes)
223{
224 struct xilinx_spi_priv *priv = dev_get_priv(bus);
225 struct xilinx_spi_regs *regs = priv->regs;
226 unsigned char d;
227 unsigned int i = 0;
228
229 while (rxbytes && !(readl(&regs->spisr) & SPISR_RX_EMPTY)) {
230 d = readl(&regs->spidrr) & SPIDRR_8BIT_MASK;
231 if (rxp)
232 *rxp++ = d;
233 debug("spi_xfer: rx:%x\n", d);
234 rxbytes--;
235 i++;
236 }
237 debug("Rx_done\n");
238
239 return i;
240}
241
Mayuresh Chitale22c97242023-11-16 22:13:34 +0530242static int start_transfer(struct udevice *dev, const void *dout, void *din, u32 len)
Vipul Kumarc7e2aae2018-06-30 08:15:19 +0530243{
Mayuresh Chitale22c97242023-11-16 22:13:34 +0530244 struct udevice *bus = dev->parent;
Vipul Kumarc7e2aae2018-06-30 08:15:19 +0530245 struct xilinx_spi_priv *priv = dev_get_priv(bus);
246 struct xilinx_spi_regs *regs = priv->regs;
T Karthik Reddy50963802022-07-16 12:28:46 +0530247 u32 count, txbytes, rxbytes;
248 int reg, ret;
249 const unsigned char *txp = (const unsigned char *)dout;
250 unsigned char *rxp = (unsigned char *)din;
Vipul Kumarc7e2aae2018-06-30 08:15:19 +0530251
T Karthik Reddy50963802022-07-16 12:28:46 +0530252 txbytes = len;
253 rxbytes = len;
254 while (txbytes || rxbytes) {
255 /* Disable master transaction */
256 reg = readl(&regs->spicr) | SPICR_MASTER_INHIBIT;
257 writel(reg, &regs->spicr);
258 count = xilinx_spi_fill_txfifo(bus, txp, txbytes);
259 /* Enable master transaction */
Vipul Kumarc7e2aae2018-06-30 08:15:19 +0530260 reg = readl(&regs->spicr) & ~SPICR_MASTER_INHIBIT;
261 writel(reg, &regs->spicr);
T Karthik Reddy50963802022-07-16 12:28:46 +0530262 txbytes -= count;
263 if (txp)
264 txp += count;
Vipul Kumarc7e2aae2018-06-30 08:15:19 +0530265
T Karthik Reddy50963802022-07-16 12:28:46 +0530266 ret = wait_for_bit_le32(&regs->spisr, SPISR_TX_EMPTY, true,
267 XILINX_SPISR_TIMEOUT, false);
268 if (ret < 0) {
269 printf("XILSPI error: Xfer timeout\n");
270 return ret;
Vipul Kumarc7e2aae2018-06-30 08:15:19 +0530271 }
T Karthik Reddy50963802022-07-16 12:28:46 +0530272
273 reg = readl(&regs->spicr) | SPICR_MASTER_INHIBIT;
274 writel(reg, &regs->spicr);
275 count = xilinx_spi_read_rxfifo(bus, rxp, rxbytes);
276 rxbytes -= count;
277 if (rxp)
278 rxp += count;
Vipul Kumarc7e2aae2018-06-30 08:15:19 +0530279 }
T Karthik Reddy50963802022-07-16 12:28:46 +0530280
281 return 0;
Vipul Kumarc7e2aae2018-06-30 08:15:19 +0530282}
283
Mayuresh Chitale22c97242023-11-16 22:13:34 +0530284static void xilinx_spi_startup_block(struct udevice *dev)
Stephan Linzfc77d512012-07-29 00:25:35 +0200285{
Mayuresh Chitale22c97242023-11-16 22:13:34 +0530286 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
T Karthik Reddy50963802022-07-16 12:28:46 +0530287 unsigned char txp;
288 unsigned char rxp[8];
Stephan Linzfc77d512012-07-29 00:25:35 +0200289
T Karthik Reddy50963802022-07-16 12:28:46 +0530290 /*
291 * Perform a dummy read as a work around for
292 * the startup block issue.
293 */
Mayuresh Chitale22c97242023-11-16 22:13:34 +0530294 spi_cs_activate(dev, slave_plat->cs);
T Karthik Reddy50963802022-07-16 12:28:46 +0530295 txp = 0x9f;
Mayuresh Chitale22c97242023-11-16 22:13:34 +0530296 start_transfer(dev, (void *)&txp, NULL, 1);
Jagan Teki48a0dbd2015-06-27 00:51:27 +0530297
Mayuresh Chitale22c97242023-11-16 22:13:34 +0530298 start_transfer(dev, NULL, (void *)rxp, 6);
Stephan Linzfc77d512012-07-29 00:25:35 +0200299
Mayuresh Chitale22c97242023-11-16 22:13:34 +0530300 spi_cs_deactivate(dev);
T Karthik Reddy50963802022-07-16 12:28:46 +0530301}
Stephan Linzfc77d512012-07-29 00:25:35 +0200302
Mayuresh Chitale73f425f2023-11-16 22:13:35 +0530303static int xilinx_spi_xfer(struct udevice *dev, unsigned int bitlen,
304 const void *dout, void *din, unsigned long flags)
305{
306 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
307 int ret;
308
309 spi_cs_activate(dev, slave_plat->cs);
310 ret = start_transfer(dev, dout, din, bitlen / 8);
311 spi_cs_deactivate(dev);
312 return ret;
313}
314
T Karthik Reddy50963802022-07-16 12:28:46 +0530315static int xilinx_spi_mem_exec_op(struct spi_slave *spi,
316 const struct spi_mem_op *op)
317{
318 struct dm_spi_slave_plat *slave_plat =
319 dev_get_parent_plat(spi->dev);
320 static u32 startup;
321 u32 dummy_len, ret;
Stephan Linzfc77d512012-07-29 00:25:35 +0200322
Vipul Kumarc7e2aae2018-06-30 08:15:19 +0530323 /*
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.
328 */
T Karthik Reddy50963802022-07-16 12:28:46 +0530329 if (!startup) {
Mayuresh Chitale22c97242023-11-16 22:13:34 +0530330 xilinx_spi_startup_block(spi->dev);
T Karthik Reddy50963802022-07-16 12:28:46 +0530331 startup++;
332 }
Stephan Linzfc77d512012-07-29 00:25:35 +0200333
T Karthik Reddy50963802022-07-16 12:28:46 +0530334 spi_cs_activate(spi->dev, slave_plat->cs);
Stephan Linzfc77d512012-07-29 00:25:35 +0200335
T Karthik Reddy50963802022-07-16 12:28:46 +0530336 if (op->cmd.opcode) {
Mayuresh Chitale22c97242023-11-16 22:13:34 +0530337 ret = start_transfer(spi->dev, (void *)&op->cmd.opcode,
338 NULL, 1);
T Karthik Reddy50963802022-07-16 12:28:46 +0530339 if (ret)
340 goto done;
Stephan Linzfc77d512012-07-29 00:25:35 +0200341 }
T Karthik Reddy50963802022-07-16 12:28:46 +0530342 if (op->addr.nbytes) {
343 int i;
344 u8 addr_buf[4];
Stephan Linzfc77d512012-07-29 00:25:35 +0200345
T Karthik Reddy50963802022-07-16 12:28:46 +0530346 for (i = 0; i < op->addr.nbytes; i++)
347 addr_buf[i] = op->addr.val >>
348 (8 * (op->addr.nbytes - i - 1));
Stephan Linzfc77d512012-07-29 00:25:35 +0200349
Mayuresh Chitale22c97242023-11-16 22:13:34 +0530350 ret = start_transfer(spi->dev, (void *)addr_buf, NULL,
T Karthik Reddy50963802022-07-16 12:28:46 +0530351 op->addr.nbytes);
352 if (ret)
353 goto done;
354 }
355 if (op->dummy.nbytes) {
T Karthik Reddyea0bd082022-07-16 12:28:47 +0530356 dummy_len = (op->dummy.nbytes * op->data.buswidth) /
357 op->dummy.buswidth;
358
Mayuresh Chitale22c97242023-11-16 22:13:34 +0530359 ret = start_transfer(spi->dev, NULL, NULL, dummy_len);
T Karthik Reddy50963802022-07-16 12:28:46 +0530360 if (ret)
361 goto done;
362 }
363 if (op->data.nbytes) {
364 if (op->data.dir == SPI_MEM_DATA_IN) {
Mayuresh Chitale22c97242023-11-16 22:13:34 +0530365 ret = start_transfer(spi->dev, NULL,
T Karthik Reddy50963802022-07-16 12:28:46 +0530366 op->data.buf.in, op->data.nbytes);
367 } else {
Mayuresh Chitale22c97242023-11-16 22:13:34 +0530368 ret = start_transfer(spi->dev, op->data.buf.out,
T Karthik Reddy50963802022-07-16 12:28:46 +0530369 NULL, op->data.nbytes);
370 }
371 if (ret)
372 goto done;
373 }
374done:
375 spi_cs_deactivate(spi->dev);
376
377 return ret;
Stephan Linzfc77d512012-07-29 00:25:35 +0200378}
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530379
T Karthik Reddyea0bd082022-07-16 12:28:47 +0530380static int xilinx_qspi_check_buswidth(struct spi_slave *slave, u8 width)
381{
382 u32 mode = slave->mode;
383
384 switch (width) {
385 case 1:
386 return 0;
387 case 2:
388 if (mode & SPI_RX_DUAL)
389 return 0;
390 break;
391 case 4:
392 if (mode & SPI_RX_QUAD)
393 return 0;
394 break;
395 }
396
397 return -EOPNOTSUPP;
398}
399
Algapally Santosh Sagarc1357a12023-06-14 03:03:54 -0600400static bool xilinx_qspi_mem_exec_op(struct spi_slave *slave,
401 const struct spi_mem_op *op)
T Karthik Reddyea0bd082022-07-16 12:28:47 +0530402{
403 if (xilinx_qspi_check_buswidth(slave, op->cmd.buswidth))
404 return false;
405
406 if (op->addr.nbytes &&
407 xilinx_qspi_check_buswidth(slave, op->addr.buswidth))
408 return false;
409
410 if (op->dummy.nbytes &&
411 xilinx_qspi_check_buswidth(slave, op->dummy.buswidth))
412 return false;
413
414 if (op->data.dir != SPI_MEM_NO_DATA &&
415 xilinx_qspi_check_buswidth(slave, op->data.buswidth))
416 return false;
417
418 return true;
419}
420
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530421static int xilinx_spi_set_speed(struct udevice *bus, uint speed)
422{
423 struct xilinx_spi_priv *priv = dev_get_priv(bus);
424
425 priv->freq = speed;
426
T Karthik Reddyc17b3072021-03-17 01:01:50 -0600427 debug("%s: regs=%p, speed=%d\n", __func__, priv->regs, priv->freq);
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530428
429 return 0;
430}
431
432static int xilinx_spi_set_mode(struct udevice *bus, uint mode)
433{
434 struct xilinx_spi_priv *priv = dev_get_priv(bus);
435 struct xilinx_spi_regs *regs = priv->regs;
T Karthik Reddyc17b3072021-03-17 01:01:50 -0600436 u32 spicr;
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530437
438 spicr = readl(&regs->spicr);
Jagan Teki0dc543f2015-09-08 01:26:29 +0530439 if (mode & SPI_LSB_FIRST)
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530440 spicr |= SPICR_LSB_FIRST;
Jagan Teki0dc543f2015-09-08 01:26:29 +0530441 if (mode & SPI_CPHA)
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530442 spicr |= SPICR_CPHA;
Jagan Teki0dc543f2015-09-08 01:26:29 +0530443 if (mode & SPI_CPOL)
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530444 spicr |= SPICR_CPOL;
Jagan Teki0dc543f2015-09-08 01:26:29 +0530445 if (mode & SPI_LOOP)
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530446 spicr |= SPICR_LOOP;
447
448 writel(spicr, &regs->spicr);
449 priv->mode = mode;
450
T Karthik Reddyc17b3072021-03-17 01:01:50 -0600451 debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode);
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530452
453 return 0;
454}
455
T Karthik Reddy50963802022-07-16 12:28:46 +0530456static const struct spi_controller_mem_ops xilinx_spi_mem_ops = {
457 .exec_op = xilinx_spi_mem_exec_op,
T Karthik Reddyea0bd082022-07-16 12:28:47 +0530458 .supports_op = xilinx_qspi_mem_exec_op,
T Karthik Reddy50963802022-07-16 12:28:46 +0530459};
460
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530461static const struct dm_spi_ops xilinx_spi_ops = {
462 .claim_bus = xilinx_spi_claim_bus,
463 .release_bus = xilinx_spi_release_bus,
Mayuresh Chitale73f425f2023-11-16 22:13:35 +0530464 .xfer = xilinx_spi_xfer,
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530465 .set_speed = xilinx_spi_set_speed,
466 .set_mode = xilinx_spi_set_mode,
T Karthik Reddy50963802022-07-16 12:28:46 +0530467 .mem_ops = &xilinx_spi_mem_ops,
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530468};
469
470static const struct udevice_id xilinx_spi_ids[] = {
Michal Simek7465f312015-12-11 12:41:14 +0100471 { .compatible = "xlnx,xps-spi-2.00.a" },
472 { .compatible = "xlnx,xps-spi-2.00.b" },
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530473 { }
474};
475
476U_BOOT_DRIVER(xilinx_spi) = {
477 .name = "xilinx_spi",
478 .id = UCLASS_SPI,
479 .of_match = xilinx_spi_ids,
480 .ops = &xilinx_spi_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700481 .priv_auto = sizeof(struct xilinx_spi_priv),
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530482 .probe = xilinx_spi_probe,
483};