blob: 2a02942d413ec751db5cb14367fd7c155fc5cf87 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +05302/*
Jagan Teki86e99b92015-09-02 11:39:45 +05303 * (C) Copyright 2013 Xilinx, Inc.
Jagan Tekib1c82da2015-06-27 00:51:31 +05304 * (C) Copyright 2015 Jagan Teki <jteki@openedev.com>
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +05305 *
6 * Xilinx Zynq PS SPI controller driver (master mode only)
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +05307 */
8
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +05309#include <common.h>
Jagan Tekib1c82da2015-06-27 00:51:31 +053010#include <dm.h>
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +053011#include <malloc.h>
12#include <spi.h>
13#include <asm/io.h>
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +053014
Jagan Tekicdc9dd02015-06-27 00:51:34 +053015DECLARE_GLOBAL_DATA_PTR;
16
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +053017/* zynq spi register bit masks ZYNQ_SPI_<REG>_<BIT>_MASK */
Jagan Teki736b4df2015-10-22 20:40:16 +053018#define ZYNQ_SPI_CR_MSA_MASK BIT(15) /* Manual start enb */
19#define ZYNQ_SPI_CR_MCS_MASK BIT(14) /* Manual chip select */
Jagan Teki9cf2ffb2015-10-22 21:06:37 +053020#define ZYNQ_SPI_CR_CS_MASK GENMASK(13, 10) /* Chip select */
21#define ZYNQ_SPI_CR_BAUD_MASK GENMASK(5, 3) /* Baud rate div */
Jagan Teki736b4df2015-10-22 20:40:16 +053022#define ZYNQ_SPI_CR_CPHA_MASK BIT(2) /* Clock phase */
23#define ZYNQ_SPI_CR_CPOL_MASK BIT(1) /* Clock polarity */
24#define ZYNQ_SPI_CR_MSTREN_MASK BIT(0) /* Mode select */
25#define ZYNQ_SPI_IXR_RXNEMPTY_MASK BIT(4) /* RX_FIFO_not_empty */
26#define ZYNQ_SPI_IXR_TXOW_MASK BIT(2) /* TX_FIFO_not_full */
Jagan Teki9cf2ffb2015-10-22 21:06:37 +053027#define ZYNQ_SPI_IXR_ALL_MASK GENMASK(6, 0) /* All IXR bits */
Jagan Teki736b4df2015-10-22 20:40:16 +053028#define ZYNQ_SPI_ENR_SPI_EN_MASK BIT(0) /* SPI Enable */
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +053029
Jagan Teki46ab8a62015-08-17 18:25:03 +053030#define ZYNQ_SPI_CR_BAUD_MAX 8 /* Baud rate divisor max val */
31#define ZYNQ_SPI_CR_BAUD_SHIFT 3 /* Baud rate divisor shift */
32#define ZYNQ_SPI_CR_SS_SHIFT 10 /* Slave select shift */
33
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +053034#define ZYNQ_SPI_FIFO_DEPTH 128
35#ifndef CONFIG_SYS_ZYNQ_SPI_WAIT
36#define CONFIG_SYS_ZYNQ_SPI_WAIT (CONFIG_SYS_HZ/100) /* 10 ms */
37#endif
38
39/* zynq spi register set */
40struct zynq_spi_regs {
41 u32 cr; /* 0x00 */
42 u32 isr; /* 0x04 */
43 u32 ier; /* 0x08 */
44 u32 idr; /* 0x0C */
45 u32 imr; /* 0x10 */
46 u32 enr; /* 0x14 */
47 u32 dr; /* 0x18 */
48 u32 txdr; /* 0x1C */
49 u32 rxdr; /* 0x20 */
50};
51
Jagan Tekib1c82da2015-06-27 00:51:31 +053052
53/* zynq spi platform data */
54struct zynq_spi_platdata {
55 struct zynq_spi_regs *regs;
56 u32 frequency; /* input frequency */
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +053057 u32 speed_hz;
Moritz Fischerac6991f2016-12-08 12:11:09 -080058 uint deactivate_delay_us; /* Delay to wait after deactivate */
59 uint activate_delay_us; /* Delay to wait after activate */
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +053060};
61
Jagan Tekib1c82da2015-06-27 00:51:31 +053062/* zynq spi priv */
63struct zynq_spi_priv {
64 struct zynq_spi_regs *regs;
Jagan Teki19126992015-08-17 18:31:39 +053065 u8 cs;
Jagan Tekib1c82da2015-06-27 00:51:31 +053066 u8 mode;
Moritz Fischerac6991f2016-12-08 12:11:09 -080067 ulong last_transaction_us; /* Time of last transaction end */
Jagan Tekib1c82da2015-06-27 00:51:31 +053068 u8 fifo_depth;
69 u32 freq; /* required frequency */
70};
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +053071
Jagan Tekib1c82da2015-06-27 00:51:31 +053072static int zynq_spi_ofdata_to_platdata(struct udevice *bus)
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +053073{
Jagan Tekib1c82da2015-06-27 00:51:31 +053074 struct zynq_spi_platdata *plat = bus->platdata;
Jagan Tekicdc9dd02015-06-27 00:51:34 +053075 const void *blob = gd->fdt_blob;
Simon Glasse160f7d2017-01-17 16:52:55 -070076 int node = dev_of_offset(bus);
Jagan Tekib1c82da2015-06-27 00:51:31 +053077
Simon Glassa821c4a2017-05-17 17:18:05 -060078 plat->regs = (struct zynq_spi_regs *)devfdt_get_addr(bus);
Jagan Tekicdc9dd02015-06-27 00:51:34 +053079
80 /* FIXME: Use 250MHz as a suitable default */
81 plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
82 250000000);
Moritz Fischerac6991f2016-12-08 12:11:09 -080083 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);
Jagan Tekib1c82da2015-06-27 00:51:31 +053087 plat->speed_hz = plat->frequency / 2;
88
Michal Simek80fd9792015-07-21 07:54:11 +020089 debug("%s: regs=%p max-frequency=%d\n", __func__,
Jagan Tekicdc9dd02015-06-27 00:51:34 +053090 plat->regs, plat->frequency);
91
Jagan Tekib1c82da2015-06-27 00:51:31 +053092 return 0;
93}
94
95static void zynq_spi_init_hw(struct zynq_spi_priv *priv)
96{
97 struct zynq_spi_regs *regs = priv->regs;
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +053098 u32 confr;
99
100 /* Disable SPI */
Michal Simek5f647c22016-09-01 12:51:27 +0200101 confr = ZYNQ_SPI_ENR_SPI_EN_MASK;
102 writel(~confr, &regs->enr);
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +0530103
104 /* Disable Interrupts */
Jagan Tekib1c82da2015-06-27 00:51:31 +0530105 writel(ZYNQ_SPI_IXR_ALL_MASK, &regs->idr);
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +0530106
107 /* Clear RX FIFO */
Jagan Tekib1c82da2015-06-27 00:51:31 +0530108 while (readl(&regs->isr) &
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +0530109 ZYNQ_SPI_IXR_RXNEMPTY_MASK)
Jagan Tekib1c82da2015-06-27 00:51:31 +0530110 readl(&regs->rxdr);
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +0530111
112 /* Clear Interrupts */
Jagan Tekib1c82da2015-06-27 00:51:31 +0530113 writel(ZYNQ_SPI_IXR_ALL_MASK, &regs->isr);
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +0530114
115 /* Manual slave select and Auto start */
116 confr = ZYNQ_SPI_CR_MCS_MASK | ZYNQ_SPI_CR_CS_MASK |
117 ZYNQ_SPI_CR_MSTREN_MASK;
118 confr &= ~ZYNQ_SPI_CR_MSA_MASK;
Jagan Tekib1c82da2015-06-27 00:51:31 +0530119 writel(confr, &regs->cr);
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +0530120
121 /* Enable SPI */
Jagan Tekib1c82da2015-06-27 00:51:31 +0530122 writel(ZYNQ_SPI_ENR_SPI_EN_MASK, &regs->enr);
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +0530123}
124
Jagan Tekib1c82da2015-06-27 00:51:31 +0530125static int zynq_spi_probe(struct udevice *bus)
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +0530126{
Jagan Tekib1c82da2015-06-27 00:51:31 +0530127 struct zynq_spi_platdata *plat = dev_get_platdata(bus);
128 struct zynq_spi_priv *priv = dev_get_priv(bus);
129
130 priv->regs = plat->regs;
131 priv->fifo_depth = ZYNQ_SPI_FIFO_DEPTH;
132
133 /* init the zynq spi hw */
134 zynq_spi_init_hw(priv);
135
136 return 0;
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +0530137}
138
Jagan Teki19126992015-08-17 18:31:39 +0530139static void spi_cs_activate(struct udevice *dev)
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +0530140{
Jagan Tekib1c82da2015-06-27 00:51:31 +0530141 struct udevice *bus = dev->parent;
Moritz Fischerac6991f2016-12-08 12:11:09 -0800142 struct zynq_spi_platdata *plat = bus->platdata;
Jagan Tekib1c82da2015-06-27 00:51:31 +0530143 struct zynq_spi_priv *priv = dev_get_priv(bus);
144 struct zynq_spi_regs *regs = priv->regs;
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +0530145 u32 cr;
146
Moritz Fischerac6991f2016-12-08 12:11:09 -0800147 /* If it's too soon to do another transaction, wait */
148 if (plat->deactivate_delay_us && priv->last_transaction_us) {
149 ulong delay_us; /* The delay completed so far */
150 delay_us = timer_get_us() - priv->last_transaction_us;
151 if (delay_us < plat->deactivate_delay_us)
152 udelay(plat->deactivate_delay_us - delay_us);
153 }
154
Jagan Tekib1c82da2015-06-27 00:51:31 +0530155 clrbits_le32(&regs->cr, ZYNQ_SPI_CR_CS_MASK);
156 cr = readl(&regs->cr);
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +0530157 /*
158 * CS cal logic: CS[13:10]
159 * xxx0 - cs0
160 * xx01 - cs1
161 * x011 - cs2
162 */
Jagan Teki19126992015-08-17 18:31:39 +0530163 cr |= (~(1 << priv->cs) << ZYNQ_SPI_CR_SS_SHIFT) & ZYNQ_SPI_CR_CS_MASK;
Jagan Tekib1c82da2015-06-27 00:51:31 +0530164 writel(cr, &regs->cr);
Moritz Fischerac6991f2016-12-08 12:11:09 -0800165
166 if (plat->activate_delay_us)
167 udelay(plat->activate_delay_us);
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +0530168}
169
Jagan Tekib1c82da2015-06-27 00:51:31 +0530170static void spi_cs_deactivate(struct udevice *dev)
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +0530171{
Jagan Tekib1c82da2015-06-27 00:51:31 +0530172 struct udevice *bus = dev->parent;
Moritz Fischerac6991f2016-12-08 12:11:09 -0800173 struct zynq_spi_platdata *plat = bus->platdata;
Jagan Tekib1c82da2015-06-27 00:51:31 +0530174 struct zynq_spi_priv *priv = dev_get_priv(bus);
175 struct zynq_spi_regs *regs = priv->regs;
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +0530176
Jagan Tekib1c82da2015-06-27 00:51:31 +0530177 setbits_le32(&regs->cr, ZYNQ_SPI_CR_CS_MASK);
Moritz Fischerac6991f2016-12-08 12:11:09 -0800178
179 /* Remember time of this transaction so we can honour the bus delay */
180 if (plat->deactivate_delay_us)
181 priv->last_transaction_us = timer_get_us();
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +0530182}
183
Jagan Tekib1c82da2015-06-27 00:51:31 +0530184static int zynq_spi_claim_bus(struct udevice *dev)
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +0530185{
Jagan Tekib1c82da2015-06-27 00:51:31 +0530186 struct udevice *bus = dev->parent;
187 struct zynq_spi_priv *priv = dev_get_priv(bus);
188 struct zynq_spi_regs *regs = priv->regs;
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +0530189
Jagan Tekib1c82da2015-06-27 00:51:31 +0530190 writel(ZYNQ_SPI_ENR_SPI_EN_MASK, &regs->enr);
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +0530191
192 return 0;
193}
194
Jagan Tekib1c82da2015-06-27 00:51:31 +0530195static int zynq_spi_release_bus(struct udevice *dev)
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +0530196{
Jagan Tekib1c82da2015-06-27 00:51:31 +0530197 struct udevice *bus = dev->parent;
198 struct zynq_spi_priv *priv = dev_get_priv(bus);
199 struct zynq_spi_regs *regs = priv->regs;
Michal Simek5f647c22016-09-01 12:51:27 +0200200 u32 confr;
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +0530201
Michal Simek5f647c22016-09-01 12:51:27 +0200202 confr = ZYNQ_SPI_ENR_SPI_EN_MASK;
203 writel(~confr, &regs->enr);
Jagan Tekib1c82da2015-06-27 00:51:31 +0530204
205 return 0;
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +0530206}
207
Jagan Tekib1c82da2015-06-27 00:51:31 +0530208static int zynq_spi_xfer(struct udevice *dev, unsigned int bitlen,
209 const void *dout, void *din, unsigned long flags)
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +0530210{
Jagan Tekib1c82da2015-06-27 00:51:31 +0530211 struct udevice *bus = dev->parent;
212 struct zynq_spi_priv *priv = dev_get_priv(bus);
213 struct zynq_spi_regs *regs = priv->regs;
214 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +0530215 u32 len = bitlen / 8;
216 u32 tx_len = len, rx_len = len, tx_tvl;
217 const u8 *tx_buf = dout;
218 u8 *rx_buf = din, buf;
219 u32 ts, status;
220
221 debug("spi_xfer: bus:%i cs:%i bitlen:%i len:%i flags:%lx\n",
Jagan Tekib1c82da2015-06-27 00:51:31 +0530222 bus->seq, slave_plat->cs, bitlen, len, flags);
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +0530223
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +0530224 if (bitlen % 8) {
225 debug("spi_xfer: Non byte aligned SPI transfer\n");
226 return -1;
227 }
228
Jagan Teki19126992015-08-17 18:31:39 +0530229 priv->cs = slave_plat->cs;
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +0530230 if (flags & SPI_XFER_BEGIN)
Jagan Teki19126992015-08-17 18:31:39 +0530231 spi_cs_activate(dev);
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +0530232
233 while (rx_len > 0) {
234 /* Write the data into TX FIFO - tx threshold is fifo_depth */
235 tx_tvl = 0;
Jagan Tekib1c82da2015-06-27 00:51:31 +0530236 while ((tx_tvl < priv->fifo_depth) && tx_len) {
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +0530237 if (tx_buf)
238 buf = *tx_buf++;
239 else
240 buf = 0;
Jagan Tekib1c82da2015-06-27 00:51:31 +0530241 writel(buf, &regs->txdr);
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +0530242 tx_len--;
243 tx_tvl++;
244 }
245
246 /* Check TX FIFO completion */
247 ts = get_timer(0);
Jagan Tekib1c82da2015-06-27 00:51:31 +0530248 status = readl(&regs->isr);
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +0530249 while (!(status & ZYNQ_SPI_IXR_TXOW_MASK)) {
250 if (get_timer(ts) > CONFIG_SYS_ZYNQ_SPI_WAIT) {
251 printf("spi_xfer: Timeout! TX FIFO not full\n");
252 return -1;
253 }
Jagan Tekib1c82da2015-06-27 00:51:31 +0530254 status = readl(&regs->isr);
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +0530255 }
256
257 /* Read the data from RX FIFO */
Jagan Tekib1c82da2015-06-27 00:51:31 +0530258 status = readl(&regs->isr);
Lad, Prabhakard2998282016-07-30 22:28:24 +0100259 while ((status & ZYNQ_SPI_IXR_RXNEMPTY_MASK) && rx_len) {
Jagan Tekib1c82da2015-06-27 00:51:31 +0530260 buf = readl(&regs->rxdr);
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +0530261 if (rx_buf)
262 *rx_buf++ = buf;
Jagan Tekib1c82da2015-06-27 00:51:31 +0530263 status = readl(&regs->isr);
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +0530264 rx_len--;
265 }
266 }
267
268 if (flags & SPI_XFER_END)
Jagan Tekib1c82da2015-06-27 00:51:31 +0530269 spi_cs_deactivate(dev);
Jagannadha Sutradharudu Teki1465d052013-07-29 23:45:16 +0530270
271 return 0;
272}
Jagan Tekib1c82da2015-06-27 00:51:31 +0530273
274static int zynq_spi_set_speed(struct udevice *bus, uint speed)
275{
276 struct zynq_spi_platdata *plat = bus->platdata;
277 struct zynq_spi_priv *priv = dev_get_priv(bus);
278 struct zynq_spi_regs *regs = priv->regs;
279 uint32_t confr;
280 u8 baud_rate_val = 0;
281
282 if (speed > plat->frequency)
283 speed = plat->frequency;
284
285 /* Set the clock frequency */
286 confr = readl(&regs->cr);
287 if (speed == 0) {
288 /* Set baudrate x8, if the freq is 0 */
289 baud_rate_val = 0x2;
290 } else if (plat->speed_hz != speed) {
Jagan Teki46ab8a62015-08-17 18:25:03 +0530291 while ((baud_rate_val < ZYNQ_SPI_CR_BAUD_MAX) &&
Jagan Tekib1c82da2015-06-27 00:51:31 +0530292 ((plat->frequency /
293 (2 << baud_rate_val)) > speed))
294 baud_rate_val++;
295 plat->speed_hz = speed / (2 << baud_rate_val);
296 }
Jagan Tekidda62412015-08-17 18:27:47 +0530297 confr &= ~ZYNQ_SPI_CR_BAUD_MASK;
Jagan Teki46ab8a62015-08-17 18:25:03 +0530298 confr |= (baud_rate_val << ZYNQ_SPI_CR_BAUD_SHIFT);
Jagan Tekib1c82da2015-06-27 00:51:31 +0530299
300 writel(confr, &regs->cr);
301 priv->freq = speed;
302
Jagan Tekia22bba82015-09-08 01:38:50 +0530303 debug("zynq_spi_set_speed: regs=%p, speed=%d\n",
304 priv->regs, priv->freq);
Jagan Tekib1c82da2015-06-27 00:51:31 +0530305
306 return 0;
307}
308
309static int zynq_spi_set_mode(struct udevice *bus, uint mode)
310{
311 struct zynq_spi_priv *priv = dev_get_priv(bus);
312 struct zynq_spi_regs *regs = priv->regs;
313 uint32_t confr;
314
315 /* Set the SPI Clock phase and polarities */
316 confr = readl(&regs->cr);
317 confr &= ~(ZYNQ_SPI_CR_CPHA_MASK | ZYNQ_SPI_CR_CPOL_MASK);
318
Jagan Tekia22bba82015-09-08 01:38:50 +0530319 if (mode & SPI_CPHA)
Jagan Tekib1c82da2015-06-27 00:51:31 +0530320 confr |= ZYNQ_SPI_CR_CPHA_MASK;
Jagan Tekia22bba82015-09-08 01:38:50 +0530321 if (mode & SPI_CPOL)
Jagan Tekib1c82da2015-06-27 00:51:31 +0530322 confr |= ZYNQ_SPI_CR_CPOL_MASK;
323
324 writel(confr, &regs->cr);
325 priv->mode = mode;
326
327 debug("zynq_spi_set_mode: regs=%p, mode=%d\n", priv->regs, priv->mode);
328
329 return 0;
330}
331
332static const struct dm_spi_ops zynq_spi_ops = {
333 .claim_bus = zynq_spi_claim_bus,
334 .release_bus = zynq_spi_release_bus,
335 .xfer = zynq_spi_xfer,
336 .set_speed = zynq_spi_set_speed,
337 .set_mode = zynq_spi_set_mode,
338};
339
340static const struct udevice_id zynq_spi_ids[] = {
Michal Simek40b383f2015-07-22 10:47:33 +0200341 { .compatible = "xlnx,zynq-spi-r1p6" },
Michal Simek23ef5ae2015-12-07 13:06:54 +0100342 { .compatible = "cdns,spi-r1p6" },
Jagan Tekib1c82da2015-06-27 00:51:31 +0530343 { }
344};
345
346U_BOOT_DRIVER(zynq_spi) = {
347 .name = "zynq_spi",
348 .id = UCLASS_SPI,
349 .of_match = zynq_spi_ids,
350 .ops = &zynq_spi_ops,
351 .ofdata_to_platdata = zynq_spi_ofdata_to_platdata,
352 .platdata_auto_alloc_size = sizeof(struct zynq_spi_platdata),
353 .priv_auto_alloc_size = sizeof(struct zynq_spi_priv),
354 .probe = zynq_spi_probe,
355};