blob: 91659349a3dbd90df2df72b748957dac2795c62c [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 Glassa821c4a2017-05-17 17:18:05 -0600106 plat->base = devfdt_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 }
Simon Glass4a7b9ee2017-05-31 17:57:18 -0600155 udelay(plat->deactivate_delay_us);
Allen Martin77c42e82013-03-16 18:58:13 +0000156
157 /* Clear stale status here */
158 setbits_le32(&regs->fifo_status,
159 SPI_FIFO_STS_ERR |
160 SPI_FIFO_STS_TX_FIFO_OVF |
161 SPI_FIFO_STS_TX_FIFO_UNR |
162 SPI_FIFO_STS_RX_FIFO_OVF |
163 SPI_FIFO_STS_RX_FIFO_UNR |
164 SPI_FIFO_STS_TX_FIFO_FULL |
165 SPI_FIFO_STS_TX_FIFO_EMPTY |
166 SPI_FIFO_STS_RX_FIFO_FULL |
167 SPI_FIFO_STS_RX_FIFO_EMPTY);
168 debug("%s: FIFO STATUS = %08x\n", __func__, readl(&regs->fifo_status));
169
Simon Glass635c2512015-06-05 14:39:33 -0600170 setbits_le32(&priv->regs->command1, SPI_CMD1_M_S | SPI_CMD1_CS_SW_HW |
171 (priv->mode << SPI_CMD1_MODE_SHIFT) | SPI_CMD1_CS_SW_VAL);
Allen Martin77c42e82013-03-16 18:58:13 +0000172 debug("%s: COMMAND1 = %08x\n", __func__, readl(&regs->command1));
173
174 return 0;
175}
176
Simon Glassfda6fac2014-10-13 23:42:13 -0600177/**
178 * Activate the CS by driving it LOW
179 *
180 * @param slave Pointer to spi_slave to which controller has to
181 * communicate with
182 */
183static void spi_cs_activate(struct udevice *dev)
Allen Martin77c42e82013-03-16 18:58:13 +0000184{
Simon Glassfda6fac2014-10-13 23:42:13 -0600185 struct udevice *bus = dev->parent;
186 struct tegra_spi_platdata *pdata = dev_get_platdata(bus);
187 struct tegra114_spi_priv *priv = dev_get_priv(bus);
Allen Martin77c42e82013-03-16 18:58:13 +0000188
Simon Glassfda6fac2014-10-13 23:42:13 -0600189 /* If it's too soon to do another transaction, wait */
190 if (pdata->deactivate_delay_us &&
191 priv->last_transaction_us) {
192 ulong delay_us; /* The delay completed so far */
193 delay_us = timer_get_us() - priv->last_transaction_us;
194 if (delay_us < pdata->deactivate_delay_us)
195 udelay(pdata->deactivate_delay_us - delay_us);
196 }
197
198 clrbits_le32(&priv->regs->command1, SPI_CMD1_CS_SW_VAL);
Allen Martin77c42e82013-03-16 18:58:13 +0000199}
200
Simon Glassfda6fac2014-10-13 23:42:13 -0600201/**
202 * Deactivate the CS by driving it HIGH
203 *
204 * @param slave Pointer to spi_slave to which controller has to
205 * communicate with
206 */
207static void spi_cs_deactivate(struct udevice *dev)
Allen Martin77c42e82013-03-16 18:58:13 +0000208{
Simon Glassfda6fac2014-10-13 23:42:13 -0600209 struct udevice *bus = dev->parent;
210 struct tegra_spi_platdata *pdata = dev_get_platdata(bus);
211 struct tegra114_spi_priv *priv = dev_get_priv(bus);
Allen Martin77c42e82013-03-16 18:58:13 +0000212
Simon Glassfda6fac2014-10-13 23:42:13 -0600213 setbits_le32(&priv->regs->command1, SPI_CMD1_CS_SW_VAL);
214
215 /* Remember time of this transaction so we can honour the bus delay */
216 if (pdata->deactivate_delay_us)
217 priv->last_transaction_us = timer_get_us();
218
219 debug("Deactivate CS, bus '%s'\n", bus->name);
Allen Martin77c42e82013-03-16 18:58:13 +0000220}
221
Simon Glassfda6fac2014-10-13 23:42:13 -0600222static int tegra114_spi_xfer(struct udevice *dev, unsigned int bitlen,
223 const void *data_out, void *data_in,
224 unsigned long flags)
Allen Martin77c42e82013-03-16 18:58:13 +0000225{
Simon Glassfda6fac2014-10-13 23:42:13 -0600226 struct udevice *bus = dev->parent;
227 struct tegra114_spi_priv *priv = dev_get_priv(bus);
228 struct spi_regs *regs = priv->regs;
Allen Martin77c42e82013-03-16 18:58:13 +0000229 u32 reg, tmpdout, tmpdin = 0;
230 const u8 *dout = data_out;
231 u8 *din = data_in;
232 int num_bytes;
233 int ret;
234
235 debug("%s: slave %u:%u dout %p din %p bitlen %u\n",
Simon Glassfda6fac2014-10-13 23:42:13 -0600236 __func__, bus->seq, spi_chip_select(dev), dout, din, bitlen);
Allen Martin77c42e82013-03-16 18:58:13 +0000237 if (bitlen % 8)
238 return -1;
239 num_bytes = bitlen / 8;
240
241 ret = 0;
242
Simon Glass635c2512015-06-05 14:39:33 -0600243 if (flags & SPI_XFER_BEGIN)
244 spi_cs_activate(dev);
245
Allen Martin77c42e82013-03-16 18:58:13 +0000246 /* clear all error status bits */
247 reg = readl(&regs->fifo_status);
248 writel(reg, &regs->fifo_status);
249
Allen Martin77c42e82013-03-16 18:58:13 +0000250 clrsetbits_le32(&regs->command1, SPI_CMD1_CS_SW_VAL,
251 SPI_CMD1_RX_EN | SPI_CMD1_TX_EN | SPI_CMD1_LSBY_FE |
Simon Glassfda6fac2014-10-13 23:42:13 -0600252 (spi_chip_select(dev) << SPI_CMD1_CS_SEL_SHIFT));
Allen Martin77c42e82013-03-16 18:58:13 +0000253
254 /* set xfer size to 1 block (32 bits) */
255 writel(0, &regs->dma_blk);
256
Allen Martin77c42e82013-03-16 18:58:13 +0000257 /* handle data in 32-bit chunks */
258 while (num_bytes > 0) {
259 int bytes;
Allen Martin77c42e82013-03-16 18:58:13 +0000260 int tm, i;
261
262 tmpdout = 0;
263 bytes = (num_bytes > 4) ? 4 : num_bytes;
264
265 if (dout != NULL) {
266 for (i = 0; i < bytes; ++i)
267 tmpdout = (tmpdout << 8) | dout[i];
268 dout += bytes;
269 }
270
271 num_bytes -= bytes;
272
Yen Lin60acde42013-12-18 11:18:46 -0700273 /* clear ready bit */
274 setbits_le32(&regs->xfer_status, SPI_XFER_STS_RDY);
275
Allen Martin77c42e82013-03-16 18:58:13 +0000276 clrsetbits_le32(&regs->command1,
277 SPI_CMD1_BIT_LEN_MASK << SPI_CMD1_BIT_LEN_SHIFT,
278 (bytes * 8 - 1) << SPI_CMD1_BIT_LEN_SHIFT);
279 writel(tmpdout, &regs->tx_fifo);
280 setbits_le32(&regs->command1, SPI_CMD1_GO);
281
282 /*
283 * Wait for SPI transmit FIFO to empty, or to time out.
284 * The RX FIFO status will be read and cleared last
285 */
Yen Lin60acde42013-12-18 11:18:46 -0700286 for (tm = 0; tm < SPI_TIMEOUT; ++tm) {
Allen Martin77c42e82013-03-16 18:58:13 +0000287 u32 fifo_status, xfer_status;
288
Allen Martin77c42e82013-03-16 18:58:13 +0000289 xfer_status = readl(&regs->xfer_status);
290 if (!(xfer_status & SPI_XFER_STS_RDY))
291 continue;
292
Yen Lin60acde42013-12-18 11:18:46 -0700293 fifo_status = readl(&regs->fifo_status);
Allen Martin77c42e82013-03-16 18:58:13 +0000294 if (fifo_status & SPI_FIFO_STS_ERR) {
295 debug("%s: got a fifo error: ", __func__);
296 if (fifo_status & SPI_FIFO_STS_TX_FIFO_OVF)
297 debug("tx FIFO overflow ");
298 if (fifo_status & SPI_FIFO_STS_TX_FIFO_UNR)
299 debug("tx FIFO underrun ");
300 if (fifo_status & SPI_FIFO_STS_RX_FIFO_OVF)
301 debug("rx FIFO overflow ");
302 if (fifo_status & SPI_FIFO_STS_RX_FIFO_UNR)
303 debug("rx FIFO underrun ");
304 if (fifo_status & SPI_FIFO_STS_TX_FIFO_FULL)
305 debug("tx FIFO full ");
306 if (fifo_status & SPI_FIFO_STS_TX_FIFO_EMPTY)
307 debug("tx FIFO empty ");
308 if (fifo_status & SPI_FIFO_STS_RX_FIFO_FULL)
309 debug("rx FIFO full ");
310 if (fifo_status & SPI_FIFO_STS_RX_FIFO_EMPTY)
311 debug("rx FIFO empty ");
312 debug("\n");
313 break;
314 }
315
316 if (!(fifo_status & SPI_FIFO_STS_RX_FIFO_EMPTY)) {
317 tmpdin = readl(&regs->rx_fifo);
Allen Martin77c42e82013-03-16 18:58:13 +0000318
319 /* swap bytes read in */
320 if (din != NULL) {
321 for (i = bytes - 1; i >= 0; --i) {
322 din[i] = tmpdin & 0xff;
323 tmpdin >>= 8;
324 }
325 din += bytes;
326 }
Yen Lin60acde42013-12-18 11:18:46 -0700327
328 /* We can exit when we've had both RX and TX */
329 break;
Allen Martin77c42e82013-03-16 18:58:13 +0000330 }
331 }
332
333 if (tm >= SPI_TIMEOUT)
334 ret = tm;
335
336 /* clear ACK RDY, etc. bits */
337 writel(readl(&regs->fifo_status), &regs->fifo_status);
338 }
339
340 if (flags & SPI_XFER_END)
Simon Glassfda6fac2014-10-13 23:42:13 -0600341 spi_cs_deactivate(dev);
Allen Martin77c42e82013-03-16 18:58:13 +0000342
343 debug("%s: transfer ended. Value=%08x, fifo_status = %08x\n",
344 __func__, tmpdin, readl(&regs->fifo_status));
345
346 if (ret) {
347 printf("%s: timeout during SPI transfer, tm %d\n",
348 __func__, ret);
349 return -1;
350 }
351
Simon Glassfda6fac2014-10-13 23:42:13 -0600352 return ret;
353}
354
355static int tegra114_spi_set_speed(struct udevice *bus, uint speed)
356{
357 struct tegra_spi_platdata *plat = bus->platdata;
358 struct tegra114_spi_priv *priv = dev_get_priv(bus);
359
360 if (speed > plat->frequency)
361 speed = plat->frequency;
362 priv->freq = speed;
363 debug("%s: regs=%p, speed=%d\n", __func__, priv->regs, priv->freq);
364
Allen Martin77c42e82013-03-16 18:58:13 +0000365 return 0;
366}
Simon Glassfda6fac2014-10-13 23:42:13 -0600367
368static int tegra114_spi_set_mode(struct udevice *bus, uint mode)
369{
370 struct tegra114_spi_priv *priv = dev_get_priv(bus);
371
372 priv->mode = mode;
373 debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode);
374
375 return 0;
376}
377
378static const struct dm_spi_ops tegra114_spi_ops = {
Simon Glassfda6fac2014-10-13 23:42:13 -0600379 .xfer = tegra114_spi_xfer,
380 .set_speed = tegra114_spi_set_speed,
381 .set_mode = tegra114_spi_set_mode,
382 /*
383 * cs_info is not needed, since we require all chip selects to be
384 * in the device tree explicitly
385 */
386};
387
388static const struct udevice_id tegra114_spi_ids[] = {
389 { .compatible = "nvidia,tegra114-spi" },
390 { }
391};
392
393U_BOOT_DRIVER(tegra114_spi) = {
394 .name = "tegra114_spi",
395 .id = UCLASS_SPI,
396 .of_match = tegra114_spi_ids,
397 .ops = &tegra114_spi_ops,
398 .ofdata_to_platdata = tegra114_spi_ofdata_to_platdata,
399 .platdata_auto_alloc_size = sizeof(struct tegra_spi_platdata),
400 .priv_auto_alloc_size = sizeof(struct tegra114_spi_priv),
Simon Glassfda6fac2014-10-13 23:42:13 -0600401 .probe = tegra114_spi_probe,
402};