blob: 27409186b0c2c722b01d7f5388c3801d4e1e3004 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Allen Martin77c42e82013-03-16 18:58:13 +00002/*
3 * NVIDIA Tegra SPI controller (T114 and later)
4 *
5 * Copyright (c) 2010-2013 NVIDIA Corporation
Allen Martin77c42e82013-03-16 18:58:13 +00006 */
7
8#include <common.h>
Simon Glassfda6fac2014-10-13 23:42:13 -06009#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Simon Glass10453152019-11-14 12:57:30 -070011#include <time.h>
Allen Martin77c42e82013-03-16 18:58:13 +000012#include <asm/io.h>
Allen Martin77c42e82013-03-16 18:58:13 +000013#include <asm/arch/clock.h>
14#include <asm/arch-tegra/clk_rst.h>
Allen Martin77c42e82013-03-16 18:58:13 +000015#include <spi.h>
Simon Glassfda6fac2014-10-13 23:42:13 -060016#include "tegra_spi.h"
Allen Martin77c42e82013-03-16 18:58:13 +000017
Allen Martin77c42e82013-03-16 18:58:13 +000018/* COMMAND1 */
Jagan Tekif6922482015-10-23 01:39:06 +053019#define SPI_CMD1_GO BIT(31)
20#define SPI_CMD1_M_S BIT(30)
Jagan Teki76538ec2015-10-23 01:03:10 +053021#define SPI_CMD1_MODE_MASK GENMASK(1, 0)
Allen Martin77c42e82013-03-16 18:58:13 +000022#define SPI_CMD1_MODE_SHIFT 28
Jagan Teki76538ec2015-10-23 01:03:10 +053023#define SPI_CMD1_CS_SEL_MASK GENMASK(1, 0)
Allen Martin77c42e82013-03-16 18:58:13 +000024#define SPI_CMD1_CS_SEL_SHIFT 26
Jagan Tekif6922482015-10-23 01:39:06 +053025#define SPI_CMD1_CS_POL_INACTIVE3 BIT(25)
26#define SPI_CMD1_CS_POL_INACTIVE2 BIT(24)
27#define SPI_CMD1_CS_POL_INACTIVE1 BIT(23)
28#define SPI_CMD1_CS_POL_INACTIVE0 BIT(22)
29#define SPI_CMD1_CS_SW_HW BIT(21)
30#define SPI_CMD1_CS_SW_VAL BIT(20)
Jagan Teki76538ec2015-10-23 01:03:10 +053031#define SPI_CMD1_IDLE_SDA_MASK GENMASK(1, 0)
Allen Martin77c42e82013-03-16 18:58:13 +000032#define SPI_CMD1_IDLE_SDA_SHIFT 18
Jagan Tekif6922482015-10-23 01:39:06 +053033#define SPI_CMD1_BIDIR BIT(17)
34#define SPI_CMD1_LSBI_FE BIT(16)
35#define SPI_CMD1_LSBY_FE BIT(15)
36#define SPI_CMD1_BOTH_EN_BIT BIT(14)
37#define SPI_CMD1_BOTH_EN_BYTE BIT(13)
38#define SPI_CMD1_RX_EN BIT(12)
39#define SPI_CMD1_TX_EN BIT(11)
40#define SPI_CMD1_PACKED BIT(5)
Jagan Teki76538ec2015-10-23 01:03:10 +053041#define SPI_CMD1_BIT_LEN_MASK GENMASK(4, 0)
Allen Martin77c42e82013-03-16 18:58:13 +000042#define SPI_CMD1_BIT_LEN_SHIFT 0
43
44/* COMMAND2 */
Jagan Tekif6922482015-10-23 01:39:06 +053045#define SPI_CMD2_TX_CLK_TAP_DELAY BIT(6)
Jagan Teki76538ec2015-10-23 01:03:10 +053046#define SPI_CMD2_TX_CLK_TAP_DELAY_MASK GENMASK(11, 6)
Jagan Tekif6922482015-10-23 01:39:06 +053047#define SPI_CMD2_RX_CLK_TAP_DELAY BIT(0)
Jagan Teki76538ec2015-10-23 01:03:10 +053048#define SPI_CMD2_RX_CLK_TAP_DELAY_MASK GENMASK(5, 0)
Allen Martin77c42e82013-03-16 18:58:13 +000049
50/* TRANSFER STATUS */
Jagan Tekif6922482015-10-23 01:39:06 +053051#define SPI_XFER_STS_RDY BIT(30)
Allen Martin77c42e82013-03-16 18:58:13 +000052
53/* FIFO STATUS */
Jagan Tekif6922482015-10-23 01:39:06 +053054#define SPI_FIFO_STS_CS_INACTIVE BIT(31)
55#define SPI_FIFO_STS_FRAME_END BIT(30)
56#define SPI_FIFO_STS_RX_FIFO_FLUSH BIT(15)
57#define SPI_FIFO_STS_TX_FIFO_FLUSH BIT(14)
58#define SPI_FIFO_STS_ERR BIT(8)
59#define SPI_FIFO_STS_TX_FIFO_OVF BIT(7)
60#define SPI_FIFO_STS_TX_FIFO_UNR BIT(6)
61#define SPI_FIFO_STS_RX_FIFO_OVF BIT(5)
62#define SPI_FIFO_STS_RX_FIFO_UNR BIT(4)
63#define SPI_FIFO_STS_TX_FIFO_FULL BIT(3)
64#define SPI_FIFO_STS_TX_FIFO_EMPTY BIT(2)
65#define SPI_FIFO_STS_RX_FIFO_FULL BIT(1)
66#define SPI_FIFO_STS_RX_FIFO_EMPTY BIT(0)
Allen Martin77c42e82013-03-16 18:58:13 +000067
68#define SPI_TIMEOUT 1000
69#define TEGRA_SPI_MAX_FREQ 52000000
70
71struct spi_regs {
72 u32 command1; /* 000:SPI_COMMAND1 register */
73 u32 command2; /* 004:SPI_COMMAND2 register */
74 u32 timing1; /* 008:SPI_CS_TIM1 register */
75 u32 timing2; /* 00c:SPI_CS_TIM2 register */
76 u32 xfer_status;/* 010:SPI_TRANS_STATUS register */
77 u32 fifo_status;/* 014:SPI_FIFO_STATUS register */
78 u32 tx_data; /* 018:SPI_TX_DATA register */
79 u32 rx_data; /* 01c:SPI_RX_DATA register */
80 u32 dma_ctl; /* 020:SPI_DMA_CTL register */
81 u32 dma_blk; /* 024:SPI_DMA_BLK register */
82 u32 rsvd[56]; /* 028-107 reserved */
83 u32 tx_fifo; /* 108:SPI_FIFO1 register */
84 u32 rsvd2[31]; /* 10c-187 reserved */
85 u32 rx_fifo; /* 188:SPI_FIFO2 register */
86 u32 spare_ctl; /* 18c:SPI_SPARE_CTRL register */
87};
88
Simon Glassfda6fac2014-10-13 23:42:13 -060089struct tegra114_spi_priv {
Allen Martin77c42e82013-03-16 18:58:13 +000090 struct spi_regs *regs;
91 unsigned int freq;
92 unsigned int mode;
93 int periph_id;
94 int valid;
Simon Glassfda6fac2014-10-13 23:42:13 -060095 int last_transaction_us;
Allen Martin77c42e82013-03-16 18:58:13 +000096};
97
Simon Glassfda6fac2014-10-13 23:42:13 -060098static int tegra114_spi_ofdata_to_platdata(struct udevice *bus)
Allen Martin77c42e82013-03-16 18:58:13 +000099{
Simon Glassfda6fac2014-10-13 23:42:13 -0600100 struct tegra_spi_platdata *plat = bus->platdata;
Simon Glassfda6fac2014-10-13 23:42:13 -0600101
Simon Glass28a3e5a2017-07-25 08:30:05 -0600102 plat->base = dev_read_addr(bus);
Simon Glass000f15f2017-07-25 08:30:00 -0600103 plat->periph_id = clock_decode_periph_id(bus);
Simon Glassfda6fac2014-10-13 23:42:13 -0600104
105 if (plat->periph_id == PERIPH_ID_NONE) {
106 debug("%s: could not decode periph id %d\n", __func__,
107 plat->periph_id);
108 return -FDT_ERR_NOTFOUND;
109 }
110
111 /* Use 500KHz as a suitable default */
Simon Glass28a3e5a2017-07-25 08:30:05 -0600112 plat->frequency = dev_read_u32_default(bus, "spi-max-frequency",
113 500000);
114 plat->deactivate_delay_us = dev_read_u32_default(bus,
115 "spi-deactivate-delay", 0);
Simon Glassfda6fac2014-10-13 23:42:13 -0600116 debug("%s: base=%#08lx, periph_id=%d, max-frequency=%d, deactivate_delay=%d\n",
117 __func__, plat->base, plat->periph_id, plat->frequency,
118 plat->deactivate_delay_us);
119
120 return 0;
Allen Martin77c42e82013-03-16 18:58:13 +0000121}
122
Simon Glassfda6fac2014-10-13 23:42:13 -0600123static int tegra114_spi_probe(struct udevice *bus)
Allen Martin77c42e82013-03-16 18:58:13 +0000124{
Simon Glassfda6fac2014-10-13 23:42:13 -0600125 struct tegra_spi_platdata *plat = dev_get_platdata(bus);
126 struct tegra114_spi_priv *priv = dev_get_priv(bus);
Simon Glass635c2512015-06-05 14:39:33 -0600127 struct spi_regs *regs;
Simon Glass20edd1a2015-06-05 14:39:35 -0600128 ulong rate;
Simon Glassfda6fac2014-10-13 23:42:13 -0600129
130 priv->regs = (struct spi_regs *)plat->base;
Simon Glass635c2512015-06-05 14:39:33 -0600131 regs = priv->regs;
Simon Glassfda6fac2014-10-13 23:42:13 -0600132
133 priv->last_transaction_us = timer_get_us();
134 priv->freq = plat->frequency;
135 priv->periph_id = plat->periph_id;
136
Simon Glass20edd1a2015-06-05 14:39:35 -0600137 /*
138 * Change SPI clock to correct frequency, PLLP_OUT0 source, falling
139 * back to the oscillator if that is too fast.
140 */
141 rate = clock_start_periph_pll(priv->periph_id, CLOCK_ID_PERIPH,
142 priv->freq);
143 if (rate > priv->freq + 100000) {
144 rate = clock_start_periph_pll(priv->periph_id, CLOCK_ID_OSC,
145 priv->freq);
146 if (rate != priv->freq) {
147 printf("Warning: SPI '%s' requested clock %u, actual clock %lu\n",
148 bus->name, priv->freq, rate);
149 }
150 }
Simon Glass4a7b9ee2017-05-31 17:57:18 -0600151 udelay(plat->deactivate_delay_us);
Allen Martin77c42e82013-03-16 18:58:13 +0000152
153 /* Clear stale status here */
154 setbits_le32(&regs->fifo_status,
155 SPI_FIFO_STS_ERR |
156 SPI_FIFO_STS_TX_FIFO_OVF |
157 SPI_FIFO_STS_TX_FIFO_UNR |
158 SPI_FIFO_STS_RX_FIFO_OVF |
159 SPI_FIFO_STS_RX_FIFO_UNR |
160 SPI_FIFO_STS_TX_FIFO_FULL |
161 SPI_FIFO_STS_TX_FIFO_EMPTY |
162 SPI_FIFO_STS_RX_FIFO_FULL |
163 SPI_FIFO_STS_RX_FIFO_EMPTY);
164 debug("%s: FIFO STATUS = %08x\n", __func__, readl(&regs->fifo_status));
165
Simon Glass635c2512015-06-05 14:39:33 -0600166 setbits_le32(&priv->regs->command1, SPI_CMD1_M_S | SPI_CMD1_CS_SW_HW |
167 (priv->mode << SPI_CMD1_MODE_SHIFT) | SPI_CMD1_CS_SW_VAL);
Allen Martin77c42e82013-03-16 18:58:13 +0000168 debug("%s: COMMAND1 = %08x\n", __func__, readl(&regs->command1));
169
170 return 0;
171}
172
Simon Glassfda6fac2014-10-13 23:42:13 -0600173/**
174 * Activate the CS by driving it LOW
175 *
176 * @param slave Pointer to spi_slave to which controller has to
177 * communicate with
178 */
179static void spi_cs_activate(struct udevice *dev)
Allen Martin77c42e82013-03-16 18:58:13 +0000180{
Simon Glassfda6fac2014-10-13 23:42:13 -0600181 struct udevice *bus = dev->parent;
182 struct tegra_spi_platdata *pdata = dev_get_platdata(bus);
183 struct tegra114_spi_priv *priv = dev_get_priv(bus);
Allen Martin77c42e82013-03-16 18:58:13 +0000184
Simon Glassfda6fac2014-10-13 23:42:13 -0600185 /* If it's too soon to do another transaction, wait */
186 if (pdata->deactivate_delay_us &&
187 priv->last_transaction_us) {
188 ulong delay_us; /* The delay completed so far */
189 delay_us = timer_get_us() - priv->last_transaction_us;
190 if (delay_us < pdata->deactivate_delay_us)
191 udelay(pdata->deactivate_delay_us - delay_us);
192 }
193
194 clrbits_le32(&priv->regs->command1, SPI_CMD1_CS_SW_VAL);
Allen Martin77c42e82013-03-16 18:58:13 +0000195}
196
Simon Glassfda6fac2014-10-13 23:42:13 -0600197/**
198 * Deactivate the CS by driving it HIGH
199 *
200 * @param slave Pointer to spi_slave to which controller has to
201 * communicate with
202 */
203static void spi_cs_deactivate(struct udevice *dev)
Allen Martin77c42e82013-03-16 18:58:13 +0000204{
Simon Glassfda6fac2014-10-13 23:42:13 -0600205 struct udevice *bus = dev->parent;
206 struct tegra_spi_platdata *pdata = dev_get_platdata(bus);
207 struct tegra114_spi_priv *priv = dev_get_priv(bus);
Allen Martin77c42e82013-03-16 18:58:13 +0000208
Simon Glassfda6fac2014-10-13 23:42:13 -0600209 setbits_le32(&priv->regs->command1, SPI_CMD1_CS_SW_VAL);
210
211 /* Remember time of this transaction so we can honour the bus delay */
212 if (pdata->deactivate_delay_us)
213 priv->last_transaction_us = timer_get_us();
214
215 debug("Deactivate CS, bus '%s'\n", bus->name);
Allen Martin77c42e82013-03-16 18:58:13 +0000216}
217
Simon Glassfda6fac2014-10-13 23:42:13 -0600218static int tegra114_spi_xfer(struct udevice *dev, unsigned int bitlen,
219 const void *data_out, void *data_in,
220 unsigned long flags)
Allen Martin77c42e82013-03-16 18:58:13 +0000221{
Simon Glassfda6fac2014-10-13 23:42:13 -0600222 struct udevice *bus = dev->parent;
223 struct tegra114_spi_priv *priv = dev_get_priv(bus);
224 struct spi_regs *regs = priv->regs;
Allen Martin77c42e82013-03-16 18:58:13 +0000225 u32 reg, tmpdout, tmpdin = 0;
226 const u8 *dout = data_out;
227 u8 *din = data_in;
228 int num_bytes;
229 int ret;
230
231 debug("%s: slave %u:%u dout %p din %p bitlen %u\n",
Simon Glassfda6fac2014-10-13 23:42:13 -0600232 __func__, bus->seq, spi_chip_select(dev), dout, din, bitlen);
Allen Martin77c42e82013-03-16 18:58:13 +0000233 if (bitlen % 8)
234 return -1;
235 num_bytes = bitlen / 8;
236
237 ret = 0;
238
Simon Glass635c2512015-06-05 14:39:33 -0600239 if (flags & SPI_XFER_BEGIN)
240 spi_cs_activate(dev);
241
Allen Martin77c42e82013-03-16 18:58:13 +0000242 /* clear all error status bits */
243 reg = readl(&regs->fifo_status);
244 writel(reg, &regs->fifo_status);
245
Allen Martin77c42e82013-03-16 18:58:13 +0000246 clrsetbits_le32(&regs->command1, SPI_CMD1_CS_SW_VAL,
247 SPI_CMD1_RX_EN | SPI_CMD1_TX_EN | SPI_CMD1_LSBY_FE |
Simon Glassfda6fac2014-10-13 23:42:13 -0600248 (spi_chip_select(dev) << SPI_CMD1_CS_SEL_SHIFT));
Allen Martin77c42e82013-03-16 18:58:13 +0000249
250 /* set xfer size to 1 block (32 bits) */
251 writel(0, &regs->dma_blk);
252
Allen Martin77c42e82013-03-16 18:58:13 +0000253 /* handle data in 32-bit chunks */
254 while (num_bytes > 0) {
255 int bytes;
Allen Martin77c42e82013-03-16 18:58:13 +0000256 int tm, i;
257
258 tmpdout = 0;
259 bytes = (num_bytes > 4) ? 4 : num_bytes;
260
261 if (dout != NULL) {
262 for (i = 0; i < bytes; ++i)
263 tmpdout = (tmpdout << 8) | dout[i];
264 dout += bytes;
265 }
266
267 num_bytes -= bytes;
268
Yen Lin60acde42013-12-18 11:18:46 -0700269 /* clear ready bit */
270 setbits_le32(&regs->xfer_status, SPI_XFER_STS_RDY);
271
Allen Martin77c42e82013-03-16 18:58:13 +0000272 clrsetbits_le32(&regs->command1,
273 SPI_CMD1_BIT_LEN_MASK << SPI_CMD1_BIT_LEN_SHIFT,
274 (bytes * 8 - 1) << SPI_CMD1_BIT_LEN_SHIFT);
275 writel(tmpdout, &regs->tx_fifo);
276 setbits_le32(&regs->command1, SPI_CMD1_GO);
277
278 /*
279 * Wait for SPI transmit FIFO to empty, or to time out.
280 * The RX FIFO status will be read and cleared last
281 */
Yen Lin60acde42013-12-18 11:18:46 -0700282 for (tm = 0; tm < SPI_TIMEOUT; ++tm) {
Allen Martin77c42e82013-03-16 18:58:13 +0000283 u32 fifo_status, xfer_status;
284
Allen Martin77c42e82013-03-16 18:58:13 +0000285 xfer_status = readl(&regs->xfer_status);
286 if (!(xfer_status & SPI_XFER_STS_RDY))
287 continue;
288
Yen Lin60acde42013-12-18 11:18:46 -0700289 fifo_status = readl(&regs->fifo_status);
Allen Martin77c42e82013-03-16 18:58:13 +0000290 if (fifo_status & SPI_FIFO_STS_ERR) {
291 debug("%s: got a fifo error: ", __func__);
292 if (fifo_status & SPI_FIFO_STS_TX_FIFO_OVF)
293 debug("tx FIFO overflow ");
294 if (fifo_status & SPI_FIFO_STS_TX_FIFO_UNR)
295 debug("tx FIFO underrun ");
296 if (fifo_status & SPI_FIFO_STS_RX_FIFO_OVF)
297 debug("rx FIFO overflow ");
298 if (fifo_status & SPI_FIFO_STS_RX_FIFO_UNR)
299 debug("rx FIFO underrun ");
300 if (fifo_status & SPI_FIFO_STS_TX_FIFO_FULL)
301 debug("tx FIFO full ");
302 if (fifo_status & SPI_FIFO_STS_TX_FIFO_EMPTY)
303 debug("tx FIFO empty ");
304 if (fifo_status & SPI_FIFO_STS_RX_FIFO_FULL)
305 debug("rx FIFO full ");
306 if (fifo_status & SPI_FIFO_STS_RX_FIFO_EMPTY)
307 debug("rx FIFO empty ");
308 debug("\n");
309 break;
310 }
311
312 if (!(fifo_status & SPI_FIFO_STS_RX_FIFO_EMPTY)) {
313 tmpdin = readl(&regs->rx_fifo);
Allen Martin77c42e82013-03-16 18:58:13 +0000314
315 /* swap bytes read in */
316 if (din != NULL) {
317 for (i = bytes - 1; i >= 0; --i) {
318 din[i] = tmpdin & 0xff;
319 tmpdin >>= 8;
320 }
321 din += bytes;
322 }
Yen Lin60acde42013-12-18 11:18:46 -0700323
324 /* We can exit when we've had both RX and TX */
325 break;
Allen Martin77c42e82013-03-16 18:58:13 +0000326 }
327 }
328
329 if (tm >= SPI_TIMEOUT)
330 ret = tm;
331
332 /* clear ACK RDY, etc. bits */
333 writel(readl(&regs->fifo_status), &regs->fifo_status);
334 }
335
336 if (flags & SPI_XFER_END)
Simon Glassfda6fac2014-10-13 23:42:13 -0600337 spi_cs_deactivate(dev);
Allen Martin77c42e82013-03-16 18:58:13 +0000338
339 debug("%s: transfer ended. Value=%08x, fifo_status = %08x\n",
340 __func__, tmpdin, readl(&regs->fifo_status));
341
342 if (ret) {
343 printf("%s: timeout during SPI transfer, tm %d\n",
344 __func__, ret);
345 return -1;
346 }
347
Simon Glassfda6fac2014-10-13 23:42:13 -0600348 return ret;
349}
350
351static int tegra114_spi_set_speed(struct udevice *bus, uint speed)
352{
353 struct tegra_spi_platdata *plat = bus->platdata;
354 struct tegra114_spi_priv *priv = dev_get_priv(bus);
355
356 if (speed > plat->frequency)
357 speed = plat->frequency;
358 priv->freq = speed;
359 debug("%s: regs=%p, speed=%d\n", __func__, priv->regs, priv->freq);
360
Allen Martin77c42e82013-03-16 18:58:13 +0000361 return 0;
362}
Simon Glassfda6fac2014-10-13 23:42:13 -0600363
364static int tegra114_spi_set_mode(struct udevice *bus, uint mode)
365{
366 struct tegra114_spi_priv *priv = dev_get_priv(bus);
367
368 priv->mode = mode;
369 debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode);
370
371 return 0;
372}
373
374static const struct dm_spi_ops tegra114_spi_ops = {
Simon Glassfda6fac2014-10-13 23:42:13 -0600375 .xfer = tegra114_spi_xfer,
376 .set_speed = tegra114_spi_set_speed,
377 .set_mode = tegra114_spi_set_mode,
378 /*
379 * cs_info is not needed, since we require all chip selects to be
380 * in the device tree explicitly
381 */
382};
383
384static const struct udevice_id tegra114_spi_ids[] = {
385 { .compatible = "nvidia,tegra114-spi" },
386 { }
387};
388
389U_BOOT_DRIVER(tegra114_spi) = {
390 .name = "tegra114_spi",
391 .id = UCLASS_SPI,
392 .of_match = tegra114_spi_ids,
393 .ops = &tegra114_spi_ops,
394 .ofdata_to_platdata = tegra114_spi_ofdata_to_platdata,
395 .platdata_auto_alloc_size = sizeof(struct tegra_spi_platdata),
396 .priv_auto_alloc_size = sizeof(struct tegra114_spi_priv),
Simon Glassfda6fac2014-10-13 23:42:13 -0600397 .probe = tegra114_spi_probe,
398};