blob: cc5ac5183f5f98ea96a9e4641e3496afcd76fa17 [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>
Stephan Linzfc77d512012-07-29 00:25:35 +020019#include <malloc.h>
20#include <spi.h>
Jagan Teki41fcbba2015-06-27 00:51:37 +053021#include <asm/io.h>
Stephan Linzfc77d512012-07-29 00:25:35 +020022
Jagan Teki23e281d2015-06-27 00:51:26 +053023/*
Jagan Teki48a0dbd2015-06-27 00:51:27 +053024 * [0]: http://www.xilinx.com/support/documentation
Jagan Teki23e281d2015-06-27 00:51:26 +053025 *
Jagan Teki48a0dbd2015-06-27 00:51:27 +053026 * Xilinx SPI Register Definitions
Jagan Teki23e281d2015-06-27 00:51:26 +053027 * [1]: [0]/ip_documentation/xps_spi.pdf
28 * page 8, Register Descriptions
29 * [2]: [0]/ip_documentation/axi_spi_ds742.pdf
30 * page 7, Register Overview Table
31 */
Jagan Teki23e281d2015-06-27 00:51:26 +053032
33/* SPI Control Register (spicr), [1] p9, [2] p8 */
Jagan Tekif0a01412015-10-23 01:39:31 +053034#define SPICR_LSB_FIRST BIT(9)
35#define SPICR_MASTER_INHIBIT BIT(8)
36#define SPICR_MANUAL_SS BIT(7)
37#define SPICR_RXFIFO_RESEST BIT(6)
38#define SPICR_TXFIFO_RESEST BIT(5)
39#define SPICR_CPHA BIT(4)
40#define SPICR_CPOL BIT(3)
41#define SPICR_MASTER_MODE BIT(2)
42#define SPICR_SPE BIT(1)
43#define SPICR_LOOP BIT(0)
Jagan Teki23e281d2015-06-27 00:51:26 +053044
45/* SPI Status Register (spisr), [1] p11, [2] p10 */
Jagan Tekif0a01412015-10-23 01:39:31 +053046#define SPISR_SLAVE_MODE_SELECT BIT(5)
47#define SPISR_MODF BIT(4)
48#define SPISR_TX_FULL BIT(3)
49#define SPISR_TX_EMPTY BIT(2)
50#define SPISR_RX_FULL BIT(1)
51#define SPISR_RX_EMPTY BIT(0)
Jagan Teki23e281d2015-06-27 00:51:26 +053052
53/* SPI Data Transmit Register (spidtr), [1] p12, [2] p12 */
Jagan Tekia2888d02015-10-23 01:03:44 +053054#define SPIDTR_8BIT_MASK GENMASK(7, 0)
55#define SPIDTR_16BIT_MASK GENMASK(15, 0)
56#define SPIDTR_32BIT_MASK GENMASK(31, 0)
Jagan Teki23e281d2015-06-27 00:51:26 +053057
58/* SPI Data Receive Register (spidrr), [1] p12, [2] p12 */
Jagan Tekia2888d02015-10-23 01:03:44 +053059#define SPIDRR_8BIT_MASK GENMASK(7, 0)
60#define SPIDRR_16BIT_MASK GENMASK(15, 0)
61#define SPIDRR_32BIT_MASK GENMASK(31, 0)
Jagan Teki23e281d2015-06-27 00:51:26 +053062
63/* SPI Slave Select Register (spissr), [1] p13, [2] p13 */
64#define SPISSR_MASK(cs) (1 << (cs))
65#define SPISSR_ACT(cs) ~SPISSR_MASK(cs)
66#define SPISSR_OFF ~0UL
67
Jagan Teki23e281d2015-06-27 00:51:26 +053068/* SPI Software Reset Register (ssr) */
69#define SPISSR_RESET_VALUE 0x0a
70
Jagan Teki48a0dbd2015-06-27 00:51:27 +053071#define XILSPI_MAX_XFER_BITS 8
72#define XILSPI_SPICR_DFLT_ON (SPICR_MANUAL_SS | SPICR_MASTER_MODE | \
73 SPICR_SPE)
74#define XILSPI_SPICR_DFLT_OFF (SPICR_MASTER_INHIBIT | SPICR_MANUAL_SS)
75
76#ifndef CONFIG_XILINX_SPI_IDLE_VAL
Jagan Tekia2888d02015-10-23 01:03:44 +053077#define CONFIG_XILINX_SPI_IDLE_VAL GENMASK(7, 0)
Jagan Teki48a0dbd2015-06-27 00:51:27 +053078#endif
79
Jagan Teki48a0dbd2015-06-27 00:51:27 +053080/* xilinx spi register set */
Jagan Tekifdc2b3d2015-06-29 13:15:18 +053081struct xilinx_spi_regs {
Jagan Teki48a0dbd2015-06-27 00:51:27 +053082 u32 __space0__[7];
83 u32 dgier; /* Device Global Interrupt Enable Register (DGIER) */
84 u32 ipisr; /* IP Interrupt Status Register (IPISR) */
85 u32 __space1__;
86 u32 ipier; /* IP Interrupt Enable Register (IPIER) */
87 u32 __space2__[5];
88 u32 srr; /* Softare Reset Register (SRR) */
89 u32 __space3__[7];
90 u32 spicr; /* SPI Control Register (SPICR) */
91 u32 spisr; /* SPI Status Register (SPISR) */
92 u32 spidtr; /* SPI Data Transmit Register (SPIDTR) */
93 u32 spidrr; /* SPI Data Receive Register (SPIDRR) */
94 u32 spissr; /* SPI Slave Select Register (SPISSR) */
95 u32 spitfor; /* SPI Transmit FIFO Occupancy Register (SPITFOR) */
96 u32 spirfor; /* SPI Receive FIFO Occupancy Register (SPIRFOR) */
97};
98
Jagan Tekifdc2b3d2015-06-29 13:15:18 +053099/* xilinx spi priv */
100struct xilinx_spi_priv {
101 struct xilinx_spi_regs *regs;
Jagan Teki23e281d2015-06-27 00:51:26 +0530102 unsigned int freq;
103 unsigned int mode;
104};
105
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530106static int xilinx_spi_probe(struct udevice *bus)
Stephan Linzfc77d512012-07-29 00:25:35 +0200107{
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530108 struct xilinx_spi_priv *priv = dev_get_priv(bus);
109 struct xilinx_spi_regs *regs = priv->regs;
Stephan Linzfc77d512012-07-29 00:25:35 +0200110
Michal Simek1ba13862018-06-30 08:15:17 +0530111 priv->regs = (struct xilinx_spi_regs *)devfdt_get_addr(bus);
Stephan Linzfc77d512012-07-29 00:25:35 +0200112
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530113 writel(SPISSR_RESET_VALUE, &regs->srr);
Stephan Linzfc77d512012-07-29 00:25:35 +0200114
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530115 return 0;
Stephan Linzfc77d512012-07-29 00:25:35 +0200116}
117
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530118static void spi_cs_activate(struct udevice *dev, uint cs)
Stephan Linzfc77d512012-07-29 00:25:35 +0200119{
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530120 struct udevice *bus = dev_get_parent(dev);
121 struct xilinx_spi_priv *priv = dev_get_priv(bus);
122 struct xilinx_spi_regs *regs = priv->regs;
Stephan Linzfc77d512012-07-29 00:25:35 +0200123
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530124 writel(SPISSR_ACT(cs), &regs->spissr);
Stephan Linzfc77d512012-07-29 00:25:35 +0200125}
126
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530127static void spi_cs_deactivate(struct udevice *dev)
Stephan Linzfc77d512012-07-29 00:25:35 +0200128{
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530129 struct udevice *bus = dev_get_parent(dev);
130 struct xilinx_spi_priv *priv = dev_get_priv(bus);
131 struct xilinx_spi_regs *regs = priv->regs;
Stephan Linzfc77d512012-07-29 00:25:35 +0200132
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530133 writel(SPISSR_OFF, &regs->spissr);
Stephan Linzfc77d512012-07-29 00:25:35 +0200134}
135
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530136static int xilinx_spi_claim_bus(struct udevice *dev)
Stephan Linzfc77d512012-07-29 00:25:35 +0200137{
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530138 struct udevice *bus = dev_get_parent(dev);
139 struct xilinx_spi_priv *priv = dev_get_priv(bus);
140 struct xilinx_spi_regs *regs = priv->regs;
Stephan Linzfc77d512012-07-29 00:25:35 +0200141
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530142 writel(SPISSR_OFF, &regs->spissr);
143 writel(XILSPI_SPICR_DFLT_ON, &regs->spicr);
Stephan Linzfc77d512012-07-29 00:25:35 +0200144
Stephan Linzfc77d512012-07-29 00:25:35 +0200145 return 0;
146}
147
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530148static int xilinx_spi_release_bus(struct udevice *dev)
Stephan Linzfc77d512012-07-29 00:25:35 +0200149{
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530150 struct udevice *bus = dev_get_parent(dev);
151 struct xilinx_spi_priv *priv = dev_get_priv(bus);
152 struct xilinx_spi_regs *regs = priv->regs;
Stephan Linzfc77d512012-07-29 00:25:35 +0200153
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530154 writel(SPISSR_OFF, &regs->spissr);
155 writel(XILSPI_SPICR_DFLT_OFF, &regs->spicr);
156
157 return 0;
Stephan Linzfc77d512012-07-29 00:25:35 +0200158}
159
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530160static int xilinx_spi_xfer(struct udevice *dev, unsigned int bitlen,
161 const void *dout, void *din, unsigned long flags)
Stephan Linzfc77d512012-07-29 00:25:35 +0200162{
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530163 struct udevice *bus = dev_get_parent(dev);
164 struct xilinx_spi_priv *priv = dev_get_priv(bus);
165 struct xilinx_spi_regs *regs = priv->regs;
166 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
Stephan Linzfc77d512012-07-29 00:25:35 +0200167 /* assume spi core configured to do 8 bit transfers */
168 unsigned int bytes = bitlen / XILSPI_MAX_XFER_BITS;
169 const unsigned char *txp = dout;
170 unsigned char *rxp = din;
171 unsigned rxecount = 17; /* max. 16 elements in FIFO, leftover 1 */
Michal Simekf030d662014-01-22 09:48:55 +0100172 unsigned global_timeout;
Stephan Linzfc77d512012-07-29 00:25:35 +0200173
Jagan Teki48a0dbd2015-06-27 00:51:27 +0530174 debug("spi_xfer: bus:%i cs:%i bitlen:%i bytes:%i flags:%lx\n",
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530175 bus->seq, slave_plat->cs, bitlen, bytes, flags);
Jagan Teki48a0dbd2015-06-27 00:51:27 +0530176
Stephan Linzfc77d512012-07-29 00:25:35 +0200177 if (bitlen == 0)
178 goto done;
179
180 if (bitlen % XILSPI_MAX_XFER_BITS) {
Jagan Teki48a0dbd2015-06-27 00:51:27 +0530181 printf("XILSPI warning: Not a multiple of %d bits\n",
182 XILSPI_MAX_XFER_BITS);
Stephan Linzfc77d512012-07-29 00:25:35 +0200183 flags |= SPI_XFER_END;
184 goto done;
185 }
186
187 /* empty read buffer */
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530188 while (rxecount && !(readl(&regs->spisr) & SPISR_RX_EMPTY)) {
189 readl(&regs->spidrr);
Stephan Linzfc77d512012-07-29 00:25:35 +0200190 rxecount--;
191 }
192
193 if (!rxecount) {
Jagan Teki48a0dbd2015-06-27 00:51:27 +0530194 printf("XILSPI error: Rx buffer not empty\n");
Stephan Linzfc77d512012-07-29 00:25:35 +0200195 return -1;
196 }
197
198 if (flags & SPI_XFER_BEGIN)
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530199 spi_cs_activate(dev, slave_plat->cs);
Stephan Linzfc77d512012-07-29 00:25:35 +0200200
Michal Simekf030d662014-01-22 09:48:55 +0100201 /* at least 1usec or greater, leftover 1 */
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530202 global_timeout = priv->freq > XILSPI_MAX_XFER_BITS * 1000000 ? 2 :
203 (XILSPI_MAX_XFER_BITS * 1000000 / priv->freq) + 1;
Stephan Linzfc77d512012-07-29 00:25:35 +0200204
Michal Simekf030d662014-01-22 09:48:55 +0100205 while (bytes--) {
206 unsigned timeout = global_timeout;
Stephan Linzfc77d512012-07-29 00:25:35 +0200207 /* get Tx element from data out buffer and count up */
208 unsigned char d = txp ? *txp++ : CONFIG_XILINX_SPI_IDLE_VAL;
Jagan Teki48a0dbd2015-06-27 00:51:27 +0530209 debug("spi_xfer: tx:%x ", d);
Stephan Linzfc77d512012-07-29 00:25:35 +0200210
211 /* write out and wait for processing (receive data) */
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530212 writel(d & SPIDTR_8BIT_MASK, &regs->spidtr);
213 while (timeout && readl(&regs->spisr)
Stephan Linzfc77d512012-07-29 00:25:35 +0200214 & SPISR_RX_EMPTY) {
215 timeout--;
216 udelay(1);
217 }
218
219 if (!timeout) {
Jagan Teki48a0dbd2015-06-27 00:51:27 +0530220 printf("XILSPI error: Xfer timeout\n");
Stephan Linzfc77d512012-07-29 00:25:35 +0200221 return -1;
222 }
223
224 /* read Rx element and push into data in buffer */
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530225 d = readl(&regs->spidrr) & SPIDRR_8BIT_MASK;
Stephan Linzfc77d512012-07-29 00:25:35 +0200226 if (rxp)
227 *rxp++ = d;
Jagan Teki48a0dbd2015-06-27 00:51:27 +0530228 debug("spi_xfer: rx:%x\n", d);
Stephan Linzfc77d512012-07-29 00:25:35 +0200229 }
230
231 done:
232 if (flags & SPI_XFER_END)
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530233 spi_cs_deactivate(dev);
Stephan Linzfc77d512012-07-29 00:25:35 +0200234
235 return 0;
236}
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530237
238static int xilinx_spi_set_speed(struct udevice *bus, uint speed)
239{
240 struct xilinx_spi_priv *priv = dev_get_priv(bus);
241
242 priv->freq = speed;
243
Jagan Teki0dc543f2015-09-08 01:26:29 +0530244 debug("xilinx_spi_set_speed: regs=%p, speed=%d\n", priv->regs,
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530245 priv->freq);
246
247 return 0;
248}
249
250static int xilinx_spi_set_mode(struct udevice *bus, uint mode)
251{
252 struct xilinx_spi_priv *priv = dev_get_priv(bus);
253 struct xilinx_spi_regs *regs = priv->regs;
254 uint32_t spicr;
255
256 spicr = readl(&regs->spicr);
Jagan Teki0dc543f2015-09-08 01:26:29 +0530257 if (mode & SPI_LSB_FIRST)
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530258 spicr |= SPICR_LSB_FIRST;
Jagan Teki0dc543f2015-09-08 01:26:29 +0530259 if (mode & SPI_CPHA)
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530260 spicr |= SPICR_CPHA;
Jagan Teki0dc543f2015-09-08 01:26:29 +0530261 if (mode & SPI_CPOL)
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530262 spicr |= SPICR_CPOL;
Jagan Teki0dc543f2015-09-08 01:26:29 +0530263 if (mode & SPI_LOOP)
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530264 spicr |= SPICR_LOOP;
265
266 writel(spicr, &regs->spicr);
267 priv->mode = mode;
268
269 debug("xilinx_spi_set_mode: regs=%p, mode=%d\n", priv->regs,
270 priv->mode);
271
272 return 0;
273}
274
275static const struct dm_spi_ops xilinx_spi_ops = {
276 .claim_bus = xilinx_spi_claim_bus,
277 .release_bus = xilinx_spi_release_bus,
278 .xfer = xilinx_spi_xfer,
279 .set_speed = xilinx_spi_set_speed,
280 .set_mode = xilinx_spi_set_mode,
281};
282
283static const struct udevice_id xilinx_spi_ids[] = {
Michal Simek7465f312015-12-11 12:41:14 +0100284 { .compatible = "xlnx,xps-spi-2.00.a" },
285 { .compatible = "xlnx,xps-spi-2.00.b" },
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530286 { }
287};
288
289U_BOOT_DRIVER(xilinx_spi) = {
290 .name = "xilinx_spi",
291 .id = UCLASS_SPI,
292 .of_match = xilinx_spi_ids,
293 .ops = &xilinx_spi_ops,
294 .priv_auto_alloc_size = sizeof(struct xilinx_spi_priv),
295 .probe = xilinx_spi_probe,
296};