blob: 897409ca02faab242a8ec45c235b8d329c546e72 [file] [log] [blame]
Allen Martin77c42e82013-03-16 18:58:13 +00001/*
2 * NVIDIA Tegra SPI controller (T114 and later)
3 *
4 * Copyright (c) 2010-2013 NVIDIA Corporation
5 *
Tom Rini5b8031c2016-01-14 22:05:13 -05006 * SPDX-License-Identifier: GPL-2.0
Allen Martin77c42e82013-03-16 18:58:13 +00007 */
8
9#include <common.h>
Simon Glassfda6fac2014-10-13 23:42:13 -060010#include <dm.h>
Allen Martin77c42e82013-03-16 18:58:13 +000011#include <asm/io.h>
Allen Martin77c42e82013-03-16 18:58:13 +000012#include <asm/arch/clock.h>
13#include <asm/arch-tegra/clk_rst.h>
Allen Martin77c42e82013-03-16 18:58:13 +000014#include <spi.h>
15#include <fdtdec.h>
Simon Glassfda6fac2014-10-13 23:42:13 -060016#include "tegra_spi.h"
Allen Martin77c42e82013-03-16 18:58:13 +000017
18DECLARE_GLOBAL_DATA_PTR;
19
20/* COMMAND1 */
Jagan Tekif6922482015-10-23 01:39:06 +053021#define SPI_CMD1_GO BIT(31)
22#define SPI_CMD1_M_S BIT(30)
Jagan Teki76538ec2015-10-23 01:03:10 +053023#define SPI_CMD1_MODE_MASK GENMASK(1, 0)
Allen Martin77c42e82013-03-16 18:58:13 +000024#define SPI_CMD1_MODE_SHIFT 28
Jagan Teki76538ec2015-10-23 01:03:10 +053025#define SPI_CMD1_CS_SEL_MASK GENMASK(1, 0)
Allen Martin77c42e82013-03-16 18:58:13 +000026#define SPI_CMD1_CS_SEL_SHIFT 26
Jagan Tekif6922482015-10-23 01:39:06 +053027#define SPI_CMD1_CS_POL_INACTIVE3 BIT(25)
28#define SPI_CMD1_CS_POL_INACTIVE2 BIT(24)
29#define SPI_CMD1_CS_POL_INACTIVE1 BIT(23)
30#define SPI_CMD1_CS_POL_INACTIVE0 BIT(22)
31#define SPI_CMD1_CS_SW_HW BIT(21)
32#define SPI_CMD1_CS_SW_VAL BIT(20)
Jagan Teki76538ec2015-10-23 01:03:10 +053033#define SPI_CMD1_IDLE_SDA_MASK GENMASK(1, 0)
Allen Martin77c42e82013-03-16 18:58:13 +000034#define SPI_CMD1_IDLE_SDA_SHIFT 18
Jagan Tekif6922482015-10-23 01:39:06 +053035#define SPI_CMD1_BIDIR BIT(17)
36#define SPI_CMD1_LSBI_FE BIT(16)
37#define SPI_CMD1_LSBY_FE BIT(15)
38#define SPI_CMD1_BOTH_EN_BIT BIT(14)
39#define SPI_CMD1_BOTH_EN_BYTE BIT(13)
40#define SPI_CMD1_RX_EN BIT(12)
41#define SPI_CMD1_TX_EN BIT(11)
42#define SPI_CMD1_PACKED BIT(5)
Jagan Teki76538ec2015-10-23 01:03:10 +053043#define SPI_CMD1_BIT_LEN_MASK GENMASK(4, 0)
Allen Martin77c42e82013-03-16 18:58:13 +000044#define SPI_CMD1_BIT_LEN_SHIFT 0
45
46/* COMMAND2 */
Jagan Tekif6922482015-10-23 01:39:06 +053047#define SPI_CMD2_TX_CLK_TAP_DELAY BIT(6)
Jagan Teki76538ec2015-10-23 01:03:10 +053048#define SPI_CMD2_TX_CLK_TAP_DELAY_MASK GENMASK(11, 6)
Jagan Tekif6922482015-10-23 01:39:06 +053049#define SPI_CMD2_RX_CLK_TAP_DELAY BIT(0)
Jagan Teki76538ec2015-10-23 01:03:10 +053050#define SPI_CMD2_RX_CLK_TAP_DELAY_MASK GENMASK(5, 0)
Allen Martin77c42e82013-03-16 18:58:13 +000051
52/* TRANSFER STATUS */
Jagan Tekif6922482015-10-23 01:39:06 +053053#define SPI_XFER_STS_RDY BIT(30)
Allen Martin77c42e82013-03-16 18:58:13 +000054
55/* FIFO STATUS */
Jagan Tekif6922482015-10-23 01:39:06 +053056#define SPI_FIFO_STS_CS_INACTIVE BIT(31)
57#define SPI_FIFO_STS_FRAME_END BIT(30)
58#define SPI_FIFO_STS_RX_FIFO_FLUSH BIT(15)
59#define SPI_FIFO_STS_TX_FIFO_FLUSH BIT(14)
60#define SPI_FIFO_STS_ERR BIT(8)
61#define SPI_FIFO_STS_TX_FIFO_OVF BIT(7)
62#define SPI_FIFO_STS_TX_FIFO_UNR BIT(6)
63#define SPI_FIFO_STS_RX_FIFO_OVF BIT(5)
64#define SPI_FIFO_STS_RX_FIFO_UNR BIT(4)
65#define SPI_FIFO_STS_TX_FIFO_FULL BIT(3)
66#define SPI_FIFO_STS_TX_FIFO_EMPTY BIT(2)
67#define SPI_FIFO_STS_RX_FIFO_FULL BIT(1)
68#define SPI_FIFO_STS_RX_FIFO_EMPTY BIT(0)
Allen Martin77c42e82013-03-16 18:58:13 +000069
70#define SPI_TIMEOUT 1000
71#define TEGRA_SPI_MAX_FREQ 52000000
72
73struct spi_regs {
74 u32 command1; /* 000:SPI_COMMAND1 register */
75 u32 command2; /* 004:SPI_COMMAND2 register */
76 u32 timing1; /* 008:SPI_CS_TIM1 register */
77 u32 timing2; /* 00c:SPI_CS_TIM2 register */
78 u32 xfer_status;/* 010:SPI_TRANS_STATUS register */
79 u32 fifo_status;/* 014:SPI_FIFO_STATUS register */
80 u32 tx_data; /* 018:SPI_TX_DATA register */
81 u32 rx_data; /* 01c:SPI_RX_DATA register */
82 u32 dma_ctl; /* 020:SPI_DMA_CTL register */
83 u32 dma_blk; /* 024:SPI_DMA_BLK register */
84 u32 rsvd[56]; /* 028-107 reserved */
85 u32 tx_fifo; /* 108:SPI_FIFO1 register */
86 u32 rsvd2[31]; /* 10c-187 reserved */
87 u32 rx_fifo; /* 188:SPI_FIFO2 register */
88 u32 spare_ctl; /* 18c:SPI_SPARE_CTRL register */
89};
90
Simon Glassfda6fac2014-10-13 23:42:13 -060091struct tegra114_spi_priv {
Allen Martin77c42e82013-03-16 18:58:13 +000092 struct spi_regs *regs;
93 unsigned int freq;
94 unsigned int mode;
95 int periph_id;
96 int valid;
Simon Glassfda6fac2014-10-13 23:42:13 -060097 int last_transaction_us;
Allen Martin77c42e82013-03-16 18:58:13 +000098};
99
Simon Glassfda6fac2014-10-13 23:42:13 -0600100static int tegra114_spi_ofdata_to_platdata(struct udevice *bus)
Allen Martin77c42e82013-03-16 18:58:13 +0000101{
Simon Glassfda6fac2014-10-13 23:42:13 -0600102 struct tegra_spi_platdata *plat = bus->platdata;
103 const void *blob = gd->fdt_blob;
Simon Glasse160f7d2017-01-17 16:52:55 -0700104 int node = dev_of_offset(bus);
Simon Glassfda6fac2014-10-13 23:42:13 -0600105
Simon Glass4e9838c2015-08-11 08:33:29 -0600106 plat->base = dev_get_addr(bus);
Simon Glassfda6fac2014-10-13 23:42:13 -0600107 plat->periph_id = clock_decode_periph_id(blob, node);
108
109 if (plat->periph_id == PERIPH_ID_NONE) {
110 debug("%s: could not decode periph id %d\n", __func__,
111 plat->periph_id);
112 return -FDT_ERR_NOTFOUND;
113 }
114
115 /* Use 500KHz as a suitable default */
116 plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
117 500000);
118 plat->deactivate_delay_us = fdtdec_get_int(blob, node,
119 "spi-deactivate-delay", 0);
120 debug("%s: base=%#08lx, periph_id=%d, max-frequency=%d, deactivate_delay=%d\n",
121 __func__, plat->base, plat->periph_id, plat->frequency,
122 plat->deactivate_delay_us);
123
124 return 0;
Allen Martin77c42e82013-03-16 18:58:13 +0000125}
126
Simon Glassfda6fac2014-10-13 23:42:13 -0600127static int tegra114_spi_probe(struct udevice *bus)
Allen Martin77c42e82013-03-16 18:58:13 +0000128{
Simon Glassfda6fac2014-10-13 23:42:13 -0600129 struct tegra_spi_platdata *plat = dev_get_platdata(bus);
130 struct tegra114_spi_priv *priv = dev_get_priv(bus);
Simon Glass635c2512015-06-05 14:39:33 -0600131 struct spi_regs *regs;
Simon Glass20edd1a2015-06-05 14:39:35 -0600132 ulong rate;
Simon Glassfda6fac2014-10-13 23:42:13 -0600133
134 priv->regs = (struct spi_regs *)plat->base;
Simon Glass635c2512015-06-05 14:39:33 -0600135 regs = priv->regs;
Simon Glassfda6fac2014-10-13 23:42:13 -0600136
137 priv->last_transaction_us = timer_get_us();
138 priv->freq = plat->frequency;
139 priv->periph_id = plat->periph_id;
140
Simon Glass20edd1a2015-06-05 14:39:35 -0600141 /*
142 * Change SPI clock to correct frequency, PLLP_OUT0 source, falling
143 * back to the oscillator if that is too fast.
144 */
145 rate = clock_start_periph_pll(priv->periph_id, CLOCK_ID_PERIPH,
146 priv->freq);
147 if (rate > priv->freq + 100000) {
148 rate = clock_start_periph_pll(priv->periph_id, CLOCK_ID_OSC,
149 priv->freq);
150 if (rate != priv->freq) {
151 printf("Warning: SPI '%s' requested clock %u, actual clock %lu\n",
152 bus->name, priv->freq, rate);
153 }
154 }
Allen Martin77c42e82013-03-16 18:58:13 +0000155
156 /* Clear stale status here */
157 setbits_le32(&regs->fifo_status,
158 SPI_FIFO_STS_ERR |
159 SPI_FIFO_STS_TX_FIFO_OVF |
160 SPI_FIFO_STS_TX_FIFO_UNR |
161 SPI_FIFO_STS_RX_FIFO_OVF |
162 SPI_FIFO_STS_RX_FIFO_UNR |
163 SPI_FIFO_STS_TX_FIFO_FULL |
164 SPI_FIFO_STS_TX_FIFO_EMPTY |
165 SPI_FIFO_STS_RX_FIFO_FULL |
166 SPI_FIFO_STS_RX_FIFO_EMPTY);
167 debug("%s: FIFO STATUS = %08x\n", __func__, readl(&regs->fifo_status));
168
Simon Glass635c2512015-06-05 14:39:33 -0600169 setbits_le32(&priv->regs->command1, SPI_CMD1_M_S | SPI_CMD1_CS_SW_HW |
170 (priv->mode << SPI_CMD1_MODE_SHIFT) | SPI_CMD1_CS_SW_VAL);
Allen Martin77c42e82013-03-16 18:58:13 +0000171 debug("%s: COMMAND1 = %08x\n", __func__, readl(&regs->command1));
172
173 return 0;
174}
175
Simon Glassfda6fac2014-10-13 23:42:13 -0600176/**
177 * Activate the CS by driving it LOW
178 *
179 * @param slave Pointer to spi_slave to which controller has to
180 * communicate with
181 */
182static void spi_cs_activate(struct udevice *dev)
Allen Martin77c42e82013-03-16 18:58:13 +0000183{
Simon Glassfda6fac2014-10-13 23:42:13 -0600184 struct udevice *bus = dev->parent;
185 struct tegra_spi_platdata *pdata = dev_get_platdata(bus);
186 struct tegra114_spi_priv *priv = dev_get_priv(bus);
Allen Martin77c42e82013-03-16 18:58:13 +0000187
Simon Glassfda6fac2014-10-13 23:42:13 -0600188 /* If it's too soon to do another transaction, wait */
189 if (pdata->deactivate_delay_us &&
190 priv->last_transaction_us) {
191 ulong delay_us; /* The delay completed so far */
192 delay_us = timer_get_us() - priv->last_transaction_us;
193 if (delay_us < pdata->deactivate_delay_us)
194 udelay(pdata->deactivate_delay_us - delay_us);
195 }
196
197 clrbits_le32(&priv->regs->command1, SPI_CMD1_CS_SW_VAL);
Allen Martin77c42e82013-03-16 18:58:13 +0000198}
199
Simon Glassfda6fac2014-10-13 23:42:13 -0600200/**
201 * Deactivate the CS by driving it HIGH
202 *
203 * @param slave Pointer to spi_slave to which controller has to
204 * communicate with
205 */
206static void spi_cs_deactivate(struct udevice *dev)
Allen Martin77c42e82013-03-16 18:58:13 +0000207{
Simon Glassfda6fac2014-10-13 23:42:13 -0600208 struct udevice *bus = dev->parent;
209 struct tegra_spi_platdata *pdata = dev_get_platdata(bus);
210 struct tegra114_spi_priv *priv = dev_get_priv(bus);
Allen Martin77c42e82013-03-16 18:58:13 +0000211
Simon Glassfda6fac2014-10-13 23:42:13 -0600212 setbits_le32(&priv->regs->command1, SPI_CMD1_CS_SW_VAL);
213
214 /* Remember time of this transaction so we can honour the bus delay */
215 if (pdata->deactivate_delay_us)
216 priv->last_transaction_us = timer_get_us();
217
218 debug("Deactivate CS, bus '%s'\n", bus->name);
Allen Martin77c42e82013-03-16 18:58:13 +0000219}
220
Simon Glassfda6fac2014-10-13 23:42:13 -0600221static int tegra114_spi_xfer(struct udevice *dev, unsigned int bitlen,
222 const void *data_out, void *data_in,
223 unsigned long flags)
Allen Martin77c42e82013-03-16 18:58:13 +0000224{
Simon Glassfda6fac2014-10-13 23:42:13 -0600225 struct udevice *bus = dev->parent;
226 struct tegra114_spi_priv *priv = dev_get_priv(bus);
227 struct spi_regs *regs = priv->regs;
Allen Martin77c42e82013-03-16 18:58:13 +0000228 u32 reg, tmpdout, tmpdin = 0;
229 const u8 *dout = data_out;
230 u8 *din = data_in;
231 int num_bytes;
232 int ret;
233
234 debug("%s: slave %u:%u dout %p din %p bitlen %u\n",
Simon Glassfda6fac2014-10-13 23:42:13 -0600235 __func__, bus->seq, spi_chip_select(dev), dout, din, bitlen);
Allen Martin77c42e82013-03-16 18:58:13 +0000236 if (bitlen % 8)
237 return -1;
238 num_bytes = bitlen / 8;
239
240 ret = 0;
241
Simon Glass635c2512015-06-05 14:39:33 -0600242 if (flags & SPI_XFER_BEGIN)
243 spi_cs_activate(dev);
244
Allen Martin77c42e82013-03-16 18:58:13 +0000245 /* clear all error status bits */
246 reg = readl(&regs->fifo_status);
247 writel(reg, &regs->fifo_status);
248
Allen Martin77c42e82013-03-16 18:58:13 +0000249 clrsetbits_le32(&regs->command1, SPI_CMD1_CS_SW_VAL,
250 SPI_CMD1_RX_EN | SPI_CMD1_TX_EN | SPI_CMD1_LSBY_FE |
Simon Glassfda6fac2014-10-13 23:42:13 -0600251 (spi_chip_select(dev) << SPI_CMD1_CS_SEL_SHIFT));
Allen Martin77c42e82013-03-16 18:58:13 +0000252
253 /* set xfer size to 1 block (32 bits) */
254 writel(0, &regs->dma_blk);
255
Allen Martin77c42e82013-03-16 18:58:13 +0000256 /* handle data in 32-bit chunks */
257 while (num_bytes > 0) {
258 int bytes;
Allen Martin77c42e82013-03-16 18:58:13 +0000259 int tm, i;
260
261 tmpdout = 0;
262 bytes = (num_bytes > 4) ? 4 : num_bytes;
263
264 if (dout != NULL) {
265 for (i = 0; i < bytes; ++i)
266 tmpdout = (tmpdout << 8) | dout[i];
267 dout += bytes;
268 }
269
270 num_bytes -= bytes;
271
Yen Lin60acde42013-12-18 11:18:46 -0700272 /* clear ready bit */
273 setbits_le32(&regs->xfer_status, SPI_XFER_STS_RDY);
274
Allen Martin77c42e82013-03-16 18:58:13 +0000275 clrsetbits_le32(&regs->command1,
276 SPI_CMD1_BIT_LEN_MASK << SPI_CMD1_BIT_LEN_SHIFT,
277 (bytes * 8 - 1) << SPI_CMD1_BIT_LEN_SHIFT);
278 writel(tmpdout, &regs->tx_fifo);
279 setbits_le32(&regs->command1, SPI_CMD1_GO);
280
281 /*
282 * Wait for SPI transmit FIFO to empty, or to time out.
283 * The RX FIFO status will be read and cleared last
284 */
Yen Lin60acde42013-12-18 11:18:46 -0700285 for (tm = 0; tm < SPI_TIMEOUT; ++tm) {
Allen Martin77c42e82013-03-16 18:58:13 +0000286 u32 fifo_status, xfer_status;
287
Allen Martin77c42e82013-03-16 18:58:13 +0000288 xfer_status = readl(&regs->xfer_status);
289 if (!(xfer_status & SPI_XFER_STS_RDY))
290 continue;
291
Yen Lin60acde42013-12-18 11:18:46 -0700292 fifo_status = readl(&regs->fifo_status);
Allen Martin77c42e82013-03-16 18:58:13 +0000293 if (fifo_status & SPI_FIFO_STS_ERR) {
294 debug("%s: got a fifo error: ", __func__);
295 if (fifo_status & SPI_FIFO_STS_TX_FIFO_OVF)
296 debug("tx FIFO overflow ");
297 if (fifo_status & SPI_FIFO_STS_TX_FIFO_UNR)
298 debug("tx FIFO underrun ");
299 if (fifo_status & SPI_FIFO_STS_RX_FIFO_OVF)
300 debug("rx FIFO overflow ");
301 if (fifo_status & SPI_FIFO_STS_RX_FIFO_UNR)
302 debug("rx FIFO underrun ");
303 if (fifo_status & SPI_FIFO_STS_TX_FIFO_FULL)
304 debug("tx FIFO full ");
305 if (fifo_status & SPI_FIFO_STS_TX_FIFO_EMPTY)
306 debug("tx FIFO empty ");
307 if (fifo_status & SPI_FIFO_STS_RX_FIFO_FULL)
308 debug("rx FIFO full ");
309 if (fifo_status & SPI_FIFO_STS_RX_FIFO_EMPTY)
310 debug("rx FIFO empty ");
311 debug("\n");
312 break;
313 }
314
315 if (!(fifo_status & SPI_FIFO_STS_RX_FIFO_EMPTY)) {
316 tmpdin = readl(&regs->rx_fifo);
Allen Martin77c42e82013-03-16 18:58:13 +0000317
318 /* swap bytes read in */
319 if (din != NULL) {
320 for (i = bytes - 1; i >= 0; --i) {
321 din[i] = tmpdin & 0xff;
322 tmpdin >>= 8;
323 }
324 din += bytes;
325 }
Yen Lin60acde42013-12-18 11:18:46 -0700326
327 /* We can exit when we've had both RX and TX */
328 break;
Allen Martin77c42e82013-03-16 18:58:13 +0000329 }
330 }
331
332 if (tm >= SPI_TIMEOUT)
333 ret = tm;
334
335 /* clear ACK RDY, etc. bits */
336 writel(readl(&regs->fifo_status), &regs->fifo_status);
337 }
338
339 if (flags & SPI_XFER_END)
Simon Glassfda6fac2014-10-13 23:42:13 -0600340 spi_cs_deactivate(dev);
Allen Martin77c42e82013-03-16 18:58:13 +0000341
342 debug("%s: transfer ended. Value=%08x, fifo_status = %08x\n",
343 __func__, tmpdin, readl(&regs->fifo_status));
344
345 if (ret) {
346 printf("%s: timeout during SPI transfer, tm %d\n",
347 __func__, ret);
348 return -1;
349 }
350
Simon Glassfda6fac2014-10-13 23:42:13 -0600351 return ret;
352}
353
354static int tegra114_spi_set_speed(struct udevice *bus, uint speed)
355{
356 struct tegra_spi_platdata *plat = bus->platdata;
357 struct tegra114_spi_priv *priv = dev_get_priv(bus);
358
359 if (speed > plat->frequency)
360 speed = plat->frequency;
361 priv->freq = speed;
362 debug("%s: regs=%p, speed=%d\n", __func__, priv->regs, priv->freq);
363
Allen Martin77c42e82013-03-16 18:58:13 +0000364 return 0;
365}
Simon Glassfda6fac2014-10-13 23:42:13 -0600366
367static int tegra114_spi_set_mode(struct udevice *bus, uint mode)
368{
369 struct tegra114_spi_priv *priv = dev_get_priv(bus);
370
371 priv->mode = mode;
372 debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode);
373
374 return 0;
375}
376
377static const struct dm_spi_ops tegra114_spi_ops = {
Simon Glassfda6fac2014-10-13 23:42:13 -0600378 .xfer = tegra114_spi_xfer,
379 .set_speed = tegra114_spi_set_speed,
380 .set_mode = tegra114_spi_set_mode,
381 /*
382 * cs_info is not needed, since we require all chip selects to be
383 * in the device tree explicitly
384 */
385};
386
387static const struct udevice_id tegra114_spi_ids[] = {
388 { .compatible = "nvidia,tegra114-spi" },
389 { }
390};
391
392U_BOOT_DRIVER(tegra114_spi) = {
393 .name = "tegra114_spi",
394 .id = UCLASS_SPI,
395 .of_match = tegra114_spi_ids,
396 .ops = &tegra114_spi_ops,
397 .ofdata_to_platdata = tegra114_spi_ofdata_to_platdata,
398 .platdata_auto_alloc_size = sizeof(struct tegra_spi_platdata),
399 .priv_auto_alloc_size = sizeof(struct tegra114_spi_priv),
Simon Glassfda6fac2014-10-13 23:42:13 -0600400 .probe = tegra114_spi_probe,
401};