blob: 94ddf4967eaf28c094733b6ccc053bfe5fab8cad [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>
16#include <common.h>
Jagan Tekifdc2b3d2015-06-29 13:15:18 +053017#include <dm.h>
18#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060019#include <log.h>
Stephan Linzfc77d512012-07-29 00:25:35 +020020#include <malloc.h>
21#include <spi.h>
T Karthik Reddy50963802022-07-16 12:28:46 +053022#include <spi-mem.h>
Jagan Teki41fcbba2015-06-27 00:51:37 +053023#include <asm/io.h>
Vipul Kumar90098ba2018-06-30 08:15:18 +053024#include <wait_bit.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060025#include <linux/bitops.h>
Stephan Linzfc77d512012-07-29 00:25:35 +020026
Jagan Teki23e281d2015-06-27 00:51:26 +053027/*
Jagan Teki48a0dbd2015-06-27 00:51:27 +053028 * [0]: http://www.xilinx.com/support/documentation
Jagan Teki23e281d2015-06-27 00:51:26 +053029 *
Jagan Teki48a0dbd2015-06-27 00:51:27 +053030 * Xilinx SPI Register Definitions
Jagan Teki23e281d2015-06-27 00:51:26 +053031 * [1]: [0]/ip_documentation/xps_spi.pdf
32 * page 8, Register Descriptions
33 * [2]: [0]/ip_documentation/axi_spi_ds742.pdf
34 * page 7, Register Overview Table
35 */
Jagan Teki23e281d2015-06-27 00:51:26 +053036
37/* SPI Control Register (spicr), [1] p9, [2] p8 */
Jagan Tekif0a01412015-10-23 01:39:31 +053038#define SPICR_LSB_FIRST BIT(9)
39#define SPICR_MASTER_INHIBIT BIT(8)
40#define SPICR_MANUAL_SS BIT(7)
41#define SPICR_RXFIFO_RESEST BIT(6)
42#define SPICR_TXFIFO_RESEST BIT(5)
43#define SPICR_CPHA BIT(4)
44#define SPICR_CPOL BIT(3)
45#define SPICR_MASTER_MODE BIT(2)
46#define SPICR_SPE BIT(1)
47#define SPICR_LOOP BIT(0)
Jagan Teki23e281d2015-06-27 00:51:26 +053048
49/* SPI Status Register (spisr), [1] p11, [2] p10 */
Jagan Tekif0a01412015-10-23 01:39:31 +053050#define SPISR_SLAVE_MODE_SELECT BIT(5)
51#define SPISR_MODF BIT(4)
52#define SPISR_TX_FULL BIT(3)
53#define SPISR_TX_EMPTY BIT(2)
54#define SPISR_RX_FULL BIT(1)
55#define SPISR_RX_EMPTY BIT(0)
Jagan Teki23e281d2015-06-27 00:51:26 +053056
57/* SPI Data Transmit Register (spidtr), [1] p12, [2] p12 */
Jagan Tekia2888d02015-10-23 01:03:44 +053058#define SPIDTR_8BIT_MASK GENMASK(7, 0)
59#define SPIDTR_16BIT_MASK GENMASK(15, 0)
60#define SPIDTR_32BIT_MASK GENMASK(31, 0)
Jagan Teki23e281d2015-06-27 00:51:26 +053061
62/* SPI Data Receive Register (spidrr), [1] p12, [2] p12 */
Jagan Tekia2888d02015-10-23 01:03:44 +053063#define SPIDRR_8BIT_MASK GENMASK(7, 0)
64#define SPIDRR_16BIT_MASK GENMASK(15, 0)
65#define SPIDRR_32BIT_MASK GENMASK(31, 0)
Jagan Teki23e281d2015-06-27 00:51:26 +053066
67/* SPI Slave Select Register (spissr), [1] p13, [2] p13 */
68#define SPISSR_MASK(cs) (1 << (cs))
69#define SPISSR_ACT(cs) ~SPISSR_MASK(cs)
Mayuresh Chitale22c97242023-11-16 22:13:34 +053070#define SPISSR_OFF (~0U)
Jagan Teki23e281d2015-06-27 00:51:26 +053071
Jagan Teki23e281d2015-06-27 00:51:26 +053072/* SPI Software Reset Register (ssr) */
73#define SPISSR_RESET_VALUE 0x0a
74
Jagan Teki48a0dbd2015-06-27 00:51:27 +053075#define XILSPI_MAX_XFER_BITS 8
76#define XILSPI_SPICR_DFLT_ON (SPICR_MANUAL_SS | SPICR_MASTER_MODE | \
T Karthik Reddy50963802022-07-16 12:28:46 +053077 SPICR_SPE | SPICR_MASTER_INHIBIT)
Jagan Teki48a0dbd2015-06-27 00:51:27 +053078#define XILSPI_SPICR_DFLT_OFF (SPICR_MASTER_INHIBIT | SPICR_MANUAL_SS)
79
Ashok Reddy Somacaecfe62020-05-18 01:11:00 -060080#define XILINX_SPI_IDLE_VAL GENMASK(7, 0)
Jagan Teki48a0dbd2015-06-27 00:51:27 +053081
Vipul Kumar90098ba2018-06-30 08:15:18 +053082#define XILINX_SPISR_TIMEOUT 10000 /* in milliseconds */
83
Jagan Teki48a0dbd2015-06-27 00:51:27 +053084/* xilinx spi register set */
Jagan Tekifdc2b3d2015-06-29 13:15:18 +053085struct xilinx_spi_regs {
Jagan Teki48a0dbd2015-06-27 00:51:27 +053086 u32 __space0__[7];
87 u32 dgier; /* Device Global Interrupt Enable Register (DGIER) */
88 u32 ipisr; /* IP Interrupt Status Register (IPISR) */
89 u32 __space1__;
90 u32 ipier; /* IP Interrupt Enable Register (IPIER) */
91 u32 __space2__[5];
92 u32 srr; /* Softare Reset Register (SRR) */
93 u32 __space3__[7];
94 u32 spicr; /* SPI Control Register (SPICR) */
95 u32 spisr; /* SPI Status Register (SPISR) */
96 u32 spidtr; /* SPI Data Transmit Register (SPIDTR) */
97 u32 spidrr; /* SPI Data Receive Register (SPIDRR) */
98 u32 spissr; /* SPI Slave Select Register (SPISSR) */
99 u32 spitfor; /* SPI Transmit FIFO Occupancy Register (SPITFOR) */
100 u32 spirfor; /* SPI Receive FIFO Occupancy Register (SPIRFOR) */
101};
102
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530103/* xilinx spi priv */
104struct xilinx_spi_priv {
105 struct xilinx_spi_regs *regs;
Jagan Teki23e281d2015-06-27 00:51:26 +0530106 unsigned int freq;
107 unsigned int mode;
Vipul Kumar90098ba2018-06-30 08:15:18 +0530108 unsigned int fifo_depth;
Vipul Kumarc7e2aae2018-06-30 08:15:19 +0530109 u8 startup;
Jagan Teki23e281d2015-06-27 00:51:26 +0530110};
111
Mayuresh Chitale732225a2023-11-16 22:13:36 +0530112static int xilinx_spi_find_buffer_size(struct xilinx_spi_regs *regs)
113{
114 u8 sr;
115 int n_words = 0;
116
117 /*
118 * Before the buffer_size detection reset the core
119 * to make sure to start with a clean state.
120 */
121 writel(SPISSR_RESET_VALUE, &regs->srr);
122
123 /* Fill the Tx FIFO with as many words as possible */
124 do {
125 writel(0, &regs->spidtr);
126 sr = readl(&regs->spisr);
127 n_words++;
128 } while (!(sr & SPISR_TX_FULL));
129
130 return n_words;
131}
132
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530133static int xilinx_spi_probe(struct udevice *bus)
Stephan Linzfc77d512012-07-29 00:25:35 +0200134{
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530135 struct xilinx_spi_priv *priv = dev_get_priv(bus);
Jiajie Chencf191e42023-02-27 23:09:39 +0800136 struct xilinx_spi_regs *regs;
Stephan Linzfc77d512012-07-29 00:25:35 +0200137
Johan Jonker8d5d8e02023-03-13 01:32:04 +0100138 regs = priv->regs = dev_read_addr_ptr(bus);
Vipul Kumar646b4602018-06-30 08:15:20 +0530139 priv->fifo_depth = dev_read_u32_default(bus, "fifo-size", 0);
Mayuresh Chitale732225a2023-11-16 22:13:36 +0530140 if (!priv->fifo_depth)
141 priv->fifo_depth = xilinx_spi_find_buffer_size(regs);
Vipul Kumar90098ba2018-06-30 08:15:18 +0530142
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530143 writel(SPISSR_RESET_VALUE, &regs->srr);
Stephan Linzfc77d512012-07-29 00:25:35 +0200144
T Karthik Reddy50963802022-07-16 12:28:46 +0530145 /*
146 * Reset RX & TX FIFO
147 * Enable Manual Slave Select Assertion,
148 * Set SPI controller into master mode, and enable it
149 */
150 writel(SPICR_RXFIFO_RESEST | SPICR_TXFIFO_RESEST |
151 SPICR_MANUAL_SS | SPICR_MASTER_MODE | SPICR_SPE,
152 &regs->spicr);
153
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530154 return 0;
Stephan Linzfc77d512012-07-29 00:25:35 +0200155}
156
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530157static void spi_cs_activate(struct udevice *dev, uint cs)
Stephan Linzfc77d512012-07-29 00:25:35 +0200158{
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530159 struct udevice *bus = dev_get_parent(dev);
160 struct xilinx_spi_priv *priv = dev_get_priv(bus);
161 struct xilinx_spi_regs *regs = priv->regs;
Stephan Linzfc77d512012-07-29 00:25:35 +0200162
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530163 writel(SPISSR_ACT(cs), &regs->spissr);
Stephan Linzfc77d512012-07-29 00:25:35 +0200164}
165
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530166static void spi_cs_deactivate(struct udevice *dev)
Stephan Linzfc77d512012-07-29 00:25:35 +0200167{
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530168 struct udevice *bus = dev_get_parent(dev);
169 struct xilinx_spi_priv *priv = dev_get_priv(bus);
170 struct xilinx_spi_regs *regs = priv->regs;
T Karthik Reddy50963802022-07-16 12:28:46 +0530171 u32 reg;
Stephan Linzfc77d512012-07-29 00:25:35 +0200172
T Karthik Reddy50963802022-07-16 12:28:46 +0530173 reg = readl(&regs->spicr) | SPICR_RXFIFO_RESEST | SPICR_TXFIFO_RESEST;
174 writel(reg, &regs->spicr);
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530175 writel(SPISSR_OFF, &regs->spissr);
Stephan Linzfc77d512012-07-29 00:25:35 +0200176}
177
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530178static int xilinx_spi_claim_bus(struct udevice *dev)
Stephan Linzfc77d512012-07-29 00:25:35 +0200179{
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530180 struct udevice *bus = dev_get_parent(dev);
181 struct xilinx_spi_priv *priv = dev_get_priv(bus);
182 struct xilinx_spi_regs *regs = priv->regs;
Stephan Linzfc77d512012-07-29 00:25:35 +0200183
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530184 writel(SPISSR_OFF, &regs->spissr);
185 writel(XILSPI_SPICR_DFLT_ON, &regs->spicr);
Stephan Linzfc77d512012-07-29 00:25:35 +0200186
Stephan Linzfc77d512012-07-29 00:25:35 +0200187 return 0;
188}
189
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530190static int xilinx_spi_release_bus(struct udevice *dev)
Stephan Linzfc77d512012-07-29 00:25:35 +0200191{
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530192 struct udevice *bus = dev_get_parent(dev);
193 struct xilinx_spi_priv *priv = dev_get_priv(bus);
194 struct xilinx_spi_regs *regs = priv->regs;
Stephan Linzfc77d512012-07-29 00:25:35 +0200195
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530196 writel(SPISSR_OFF, &regs->spissr);
197 writel(XILSPI_SPICR_DFLT_OFF, &regs->spicr);
198
199 return 0;
Stephan Linzfc77d512012-07-29 00:25:35 +0200200}
201
Vipul Kumar90098ba2018-06-30 08:15:18 +0530202static u32 xilinx_spi_fill_txfifo(struct udevice *bus, const u8 *txp,
203 u32 txbytes)
204{
205 struct xilinx_spi_priv *priv = dev_get_priv(bus);
206 struct xilinx_spi_regs *regs = priv->regs;
207 unsigned char d;
208 u32 i = 0;
209
210 while (txbytes && !(readl(&regs->spisr) & SPISR_TX_FULL) &&
211 i < priv->fifo_depth) {
Ashok Reddy Somacaecfe62020-05-18 01:11:00 -0600212 d = txp ? *txp++ : XILINX_SPI_IDLE_VAL;
Vipul Kumar90098ba2018-06-30 08:15:18 +0530213 debug("spi_xfer: tx:%x ", d);
214 /* write out and wait for processing (receive data) */
215 writel(d & SPIDTR_8BIT_MASK, &regs->spidtr);
216 txbytes--;
217 i++;
218 }
219
220 return i;
221}
222
223static u32 xilinx_spi_read_rxfifo(struct udevice *bus, u8 *rxp, u32 rxbytes)
224{
225 struct xilinx_spi_priv *priv = dev_get_priv(bus);
226 struct xilinx_spi_regs *regs = priv->regs;
227 unsigned char d;
228 unsigned int i = 0;
229
230 while (rxbytes && !(readl(&regs->spisr) & SPISR_RX_EMPTY)) {
231 d = readl(&regs->spidrr) & SPIDRR_8BIT_MASK;
232 if (rxp)
233 *rxp++ = d;
234 debug("spi_xfer: rx:%x\n", d);
235 rxbytes--;
236 i++;
237 }
238 debug("Rx_done\n");
239
240 return i;
241}
242
Mayuresh Chitale22c97242023-11-16 22:13:34 +0530243static int start_transfer(struct udevice *dev, const void *dout, void *din, u32 len)
Vipul Kumarc7e2aae2018-06-30 08:15:19 +0530244{
Mayuresh Chitale22c97242023-11-16 22:13:34 +0530245 struct udevice *bus = dev->parent;
Vipul Kumarc7e2aae2018-06-30 08:15:19 +0530246 struct xilinx_spi_priv *priv = dev_get_priv(bus);
247 struct xilinx_spi_regs *regs = priv->regs;
T Karthik Reddy50963802022-07-16 12:28:46 +0530248 u32 count, txbytes, rxbytes;
249 int reg, ret;
250 const unsigned char *txp = (const unsigned char *)dout;
251 unsigned char *rxp = (unsigned char *)din;
Vipul Kumarc7e2aae2018-06-30 08:15:19 +0530252
T Karthik Reddy50963802022-07-16 12:28:46 +0530253 txbytes = len;
254 rxbytes = len;
255 while (txbytes || rxbytes) {
256 /* Disable master transaction */
257 reg = readl(&regs->spicr) | SPICR_MASTER_INHIBIT;
258 writel(reg, &regs->spicr);
259 count = xilinx_spi_fill_txfifo(bus, txp, txbytes);
260 /* Enable master transaction */
Vipul Kumarc7e2aae2018-06-30 08:15:19 +0530261 reg = readl(&regs->spicr) & ~SPICR_MASTER_INHIBIT;
262 writel(reg, &regs->spicr);
T Karthik Reddy50963802022-07-16 12:28:46 +0530263 txbytes -= count;
264 if (txp)
265 txp += count;
Vipul Kumarc7e2aae2018-06-30 08:15:19 +0530266
T Karthik Reddy50963802022-07-16 12:28:46 +0530267 ret = wait_for_bit_le32(&regs->spisr, SPISR_TX_EMPTY, true,
268 XILINX_SPISR_TIMEOUT, false);
269 if (ret < 0) {
270 printf("XILSPI error: Xfer timeout\n");
271 return ret;
Vipul Kumarc7e2aae2018-06-30 08:15:19 +0530272 }
T Karthik Reddy50963802022-07-16 12:28:46 +0530273
274 reg = readl(&regs->spicr) | SPICR_MASTER_INHIBIT;
275 writel(reg, &regs->spicr);
276 count = xilinx_spi_read_rxfifo(bus, rxp, rxbytes);
277 rxbytes -= count;
278 if (rxp)
279 rxp += count;
Vipul Kumarc7e2aae2018-06-30 08:15:19 +0530280 }
T Karthik Reddy50963802022-07-16 12:28:46 +0530281
282 return 0;
Vipul Kumarc7e2aae2018-06-30 08:15:19 +0530283}
284
Mayuresh Chitale22c97242023-11-16 22:13:34 +0530285static void xilinx_spi_startup_block(struct udevice *dev)
Stephan Linzfc77d512012-07-29 00:25:35 +0200286{
Mayuresh Chitale22c97242023-11-16 22:13:34 +0530287 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
T Karthik Reddy50963802022-07-16 12:28:46 +0530288 unsigned char txp;
289 unsigned char rxp[8];
Stephan Linzfc77d512012-07-29 00:25:35 +0200290
T Karthik Reddy50963802022-07-16 12:28:46 +0530291 /*
292 * Perform a dummy read as a work around for
293 * the startup block issue.
294 */
Mayuresh Chitale22c97242023-11-16 22:13:34 +0530295 spi_cs_activate(dev, slave_plat->cs);
T Karthik Reddy50963802022-07-16 12:28:46 +0530296 txp = 0x9f;
Mayuresh Chitale22c97242023-11-16 22:13:34 +0530297 start_transfer(dev, (void *)&txp, NULL, 1);
Jagan Teki48a0dbd2015-06-27 00:51:27 +0530298
Mayuresh Chitale22c97242023-11-16 22:13:34 +0530299 start_transfer(dev, NULL, (void *)rxp, 6);
Stephan Linzfc77d512012-07-29 00:25:35 +0200300
Mayuresh Chitale22c97242023-11-16 22:13:34 +0530301 spi_cs_deactivate(dev);
T Karthik Reddy50963802022-07-16 12:28:46 +0530302}
Stephan Linzfc77d512012-07-29 00:25:35 +0200303
Mayuresh Chitale73f425f2023-11-16 22:13:35 +0530304static int xilinx_spi_xfer(struct udevice *dev, unsigned int bitlen,
305 const void *dout, void *din, unsigned long flags)
306{
307 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
308 int ret;
309
310 spi_cs_activate(dev, slave_plat->cs);
311 ret = start_transfer(dev, dout, din, bitlen / 8);
312 spi_cs_deactivate(dev);
313 return ret;
314}
315
T Karthik Reddy50963802022-07-16 12:28:46 +0530316static int xilinx_spi_mem_exec_op(struct spi_slave *spi,
317 const struct spi_mem_op *op)
318{
319 struct dm_spi_slave_plat *slave_plat =
320 dev_get_parent_plat(spi->dev);
321 static u32 startup;
322 u32 dummy_len, ret;
Stephan Linzfc77d512012-07-29 00:25:35 +0200323
Vipul Kumarc7e2aae2018-06-30 08:15:19 +0530324 /*
325 * This is the work around for the startup block issue in
326 * the spi controller. SPI clock is passing through STARTUP
327 * block to FLASH. STARTUP block don't provide clock as soon
328 * as QSPI provides command. So first command fails.
329 */
T Karthik Reddy50963802022-07-16 12:28:46 +0530330 if (!startup) {
Mayuresh Chitale22c97242023-11-16 22:13:34 +0530331 xilinx_spi_startup_block(spi->dev);
T Karthik Reddy50963802022-07-16 12:28:46 +0530332 startup++;
333 }
Stephan Linzfc77d512012-07-29 00:25:35 +0200334
T Karthik Reddy50963802022-07-16 12:28:46 +0530335 spi_cs_activate(spi->dev, slave_plat->cs);
Stephan Linzfc77d512012-07-29 00:25:35 +0200336
T Karthik Reddy50963802022-07-16 12:28:46 +0530337 if (op->cmd.opcode) {
Mayuresh Chitale22c97242023-11-16 22:13:34 +0530338 ret = start_transfer(spi->dev, (void *)&op->cmd.opcode,
339 NULL, 1);
T Karthik Reddy50963802022-07-16 12:28:46 +0530340 if (ret)
341 goto done;
Stephan Linzfc77d512012-07-29 00:25:35 +0200342 }
T Karthik Reddy50963802022-07-16 12:28:46 +0530343 if (op->addr.nbytes) {
344 int i;
345 u8 addr_buf[4];
Stephan Linzfc77d512012-07-29 00:25:35 +0200346
T Karthik Reddy50963802022-07-16 12:28:46 +0530347 for (i = 0; i < op->addr.nbytes; i++)
348 addr_buf[i] = op->addr.val >>
349 (8 * (op->addr.nbytes - i - 1));
Stephan Linzfc77d512012-07-29 00:25:35 +0200350
Mayuresh Chitale22c97242023-11-16 22:13:34 +0530351 ret = start_transfer(spi->dev, (void *)addr_buf, NULL,
T Karthik Reddy50963802022-07-16 12:28:46 +0530352 op->addr.nbytes);
353 if (ret)
354 goto done;
355 }
356 if (op->dummy.nbytes) {
T Karthik Reddyea0bd082022-07-16 12:28:47 +0530357 dummy_len = (op->dummy.nbytes * op->data.buswidth) /
358 op->dummy.buswidth;
359
Mayuresh Chitale22c97242023-11-16 22:13:34 +0530360 ret = start_transfer(spi->dev, NULL, NULL, dummy_len);
T Karthik Reddy50963802022-07-16 12:28:46 +0530361 if (ret)
362 goto done;
363 }
364 if (op->data.nbytes) {
365 if (op->data.dir == SPI_MEM_DATA_IN) {
Mayuresh Chitale22c97242023-11-16 22:13:34 +0530366 ret = start_transfer(spi->dev, NULL,
T Karthik Reddy50963802022-07-16 12:28:46 +0530367 op->data.buf.in, op->data.nbytes);
368 } else {
Mayuresh Chitale22c97242023-11-16 22:13:34 +0530369 ret = start_transfer(spi->dev, op->data.buf.out,
T Karthik Reddy50963802022-07-16 12:28:46 +0530370 NULL, op->data.nbytes);
371 }
372 if (ret)
373 goto done;
374 }
375done:
376 spi_cs_deactivate(spi->dev);
377
378 return ret;
Stephan Linzfc77d512012-07-29 00:25:35 +0200379}
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530380
T Karthik Reddyea0bd082022-07-16 12:28:47 +0530381static int xilinx_qspi_check_buswidth(struct spi_slave *slave, u8 width)
382{
383 u32 mode = slave->mode;
384
385 switch (width) {
386 case 1:
387 return 0;
388 case 2:
389 if (mode & SPI_RX_DUAL)
390 return 0;
391 break;
392 case 4:
393 if (mode & SPI_RX_QUAD)
394 return 0;
395 break;
396 }
397
398 return -EOPNOTSUPP;
399}
400
Algapally Santosh Sagarc1357a12023-06-14 03:03:54 -0600401static bool xilinx_qspi_mem_exec_op(struct spi_slave *slave,
402 const struct spi_mem_op *op)
T Karthik Reddyea0bd082022-07-16 12:28:47 +0530403{
404 if (xilinx_qspi_check_buswidth(slave, op->cmd.buswidth))
405 return false;
406
407 if (op->addr.nbytes &&
408 xilinx_qspi_check_buswidth(slave, op->addr.buswidth))
409 return false;
410
411 if (op->dummy.nbytes &&
412 xilinx_qspi_check_buswidth(slave, op->dummy.buswidth))
413 return false;
414
415 if (op->data.dir != SPI_MEM_NO_DATA &&
416 xilinx_qspi_check_buswidth(slave, op->data.buswidth))
417 return false;
418
419 return true;
420}
421
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530422static int xilinx_spi_set_speed(struct udevice *bus, uint speed)
423{
424 struct xilinx_spi_priv *priv = dev_get_priv(bus);
425
426 priv->freq = speed;
427
T Karthik Reddyc17b3072021-03-17 01:01:50 -0600428 debug("%s: regs=%p, speed=%d\n", __func__, priv->regs, priv->freq);
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530429
430 return 0;
431}
432
433static int xilinx_spi_set_mode(struct udevice *bus, uint mode)
434{
435 struct xilinx_spi_priv *priv = dev_get_priv(bus);
436 struct xilinx_spi_regs *regs = priv->regs;
T Karthik Reddyc17b3072021-03-17 01:01:50 -0600437 u32 spicr;
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530438
439 spicr = readl(&regs->spicr);
Jagan Teki0dc543f2015-09-08 01:26:29 +0530440 if (mode & SPI_LSB_FIRST)
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530441 spicr |= SPICR_LSB_FIRST;
Jagan Teki0dc543f2015-09-08 01:26:29 +0530442 if (mode & SPI_CPHA)
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530443 spicr |= SPICR_CPHA;
Jagan Teki0dc543f2015-09-08 01:26:29 +0530444 if (mode & SPI_CPOL)
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530445 spicr |= SPICR_CPOL;
Jagan Teki0dc543f2015-09-08 01:26:29 +0530446 if (mode & SPI_LOOP)
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530447 spicr |= SPICR_LOOP;
448
449 writel(spicr, &regs->spicr);
450 priv->mode = mode;
451
T Karthik Reddyc17b3072021-03-17 01:01:50 -0600452 debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode);
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530453
454 return 0;
455}
456
T Karthik Reddy50963802022-07-16 12:28:46 +0530457static const struct spi_controller_mem_ops xilinx_spi_mem_ops = {
458 .exec_op = xilinx_spi_mem_exec_op,
T Karthik Reddyea0bd082022-07-16 12:28:47 +0530459 .supports_op = xilinx_qspi_mem_exec_op,
T Karthik Reddy50963802022-07-16 12:28:46 +0530460};
461
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530462static const struct dm_spi_ops xilinx_spi_ops = {
463 .claim_bus = xilinx_spi_claim_bus,
464 .release_bus = xilinx_spi_release_bus,
Mayuresh Chitale73f425f2023-11-16 22:13:35 +0530465 .xfer = xilinx_spi_xfer,
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530466 .set_speed = xilinx_spi_set_speed,
467 .set_mode = xilinx_spi_set_mode,
T Karthik Reddy50963802022-07-16 12:28:46 +0530468 .mem_ops = &xilinx_spi_mem_ops,
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530469};
470
471static const struct udevice_id xilinx_spi_ids[] = {
Michal Simek7465f312015-12-11 12:41:14 +0100472 { .compatible = "xlnx,xps-spi-2.00.a" },
473 { .compatible = "xlnx,xps-spi-2.00.b" },
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530474 { }
475};
476
477U_BOOT_DRIVER(xilinx_spi) = {
478 .name = "xilinx_spi",
479 .id = UCLASS_SPI,
480 .of_match = xilinx_spi_ids,
481 .ops = &xilinx_spi_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700482 .priv_auto = sizeof(struct xilinx_spi_priv),
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530483 .probe = xilinx_spi_probe,
484};