blob: 2a77126d04fb3af10a209a06ae7533d0764caac5 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Tom Warren4e675ff2015-10-12 14:50:54 -07002/*
3 * NVIDIA Tegra210 QSPI controller driver
4 *
Tom Warren3c8cf242019-11-12 13:17:37 -07005 * (C) Copyright 2015-2019 NVIDIA Corporation <www.nvidia.com>
6 *
Tom Warren4e675ff2015-10-12 14:50:54 -07007 */
8
9#include <common.h>
10#include <dm.h>
Simon Glass10453152019-11-14 12:57:30 -070011#include <time.h>
Tom Warren4e675ff2015-10-12 14:50:54 -070012#include <asm/io.h>
13#include <asm/arch/clock.h>
14#include <asm/arch-tegra/clk_rst.h>
15#include <spi.h>
16#include <fdtdec.h>
17#include "tegra_spi.h"
18
19DECLARE_GLOBAL_DATA_PTR;
20
21/* COMMAND1 */
22#define QSPI_CMD1_GO BIT(31)
23#define QSPI_CMD1_M_S BIT(30)
24#define QSPI_CMD1_MODE_MASK GENMASK(1,0)
25#define QSPI_CMD1_MODE_SHIFT 28
26#define QSPI_CMD1_CS_SEL_MASK GENMASK(1,0)
27#define QSPI_CMD1_CS_SEL_SHIFT 26
28#define QSPI_CMD1_CS_POL_INACTIVE0 BIT(22)
29#define QSPI_CMD1_CS_SW_HW BIT(21)
30#define QSPI_CMD1_CS_SW_VAL BIT(20)
31#define QSPI_CMD1_IDLE_SDA_MASK GENMASK(1,0)
32#define QSPI_CMD1_IDLE_SDA_SHIFT 18
33#define QSPI_CMD1_BIDIR BIT(17)
34#define QSPI_CMD1_LSBI_FE BIT(16)
35#define QSPI_CMD1_LSBY_FE BIT(15)
36#define QSPI_CMD1_BOTH_EN_BIT BIT(14)
37#define QSPI_CMD1_BOTH_EN_BYTE BIT(13)
38#define QSPI_CMD1_RX_EN BIT(12)
39#define QSPI_CMD1_TX_EN BIT(11)
40#define QSPI_CMD1_PACKED BIT(5)
41#define QSPI_CMD1_BITLEN_MASK GENMASK(4,0)
42#define QSPI_CMD1_BITLEN_SHIFT 0
43
44/* COMMAND2 */
45#define QSPI_CMD2_TX_CLK_TAP_DELAY BIT(6)
46#define QSPI_CMD2_TX_CLK_TAP_DELAY_MASK GENMASK(11,6)
47#define QSPI_CMD2_RX_CLK_TAP_DELAY BIT(0)
48#define QSPI_CMD2_RX_CLK_TAP_DELAY_MASK GENMASK(5,0)
49
50/* TRANSFER STATUS */
51#define QSPI_XFER_STS_RDY BIT(30)
52
53/* FIFO STATUS */
54#define QSPI_FIFO_STS_CS_INACTIVE BIT(31)
55#define QSPI_FIFO_STS_FRAME_END BIT(30)
56#define QSPI_FIFO_STS_RX_FIFO_FLUSH BIT(15)
57#define QSPI_FIFO_STS_TX_FIFO_FLUSH BIT(14)
58#define QSPI_FIFO_STS_ERR BIT(8)
59#define QSPI_FIFO_STS_TX_FIFO_OVF BIT(7)
60#define QSPI_FIFO_STS_TX_FIFO_UNR BIT(6)
61#define QSPI_FIFO_STS_RX_FIFO_OVF BIT(5)
62#define QSPI_FIFO_STS_RX_FIFO_UNR BIT(4)
63#define QSPI_FIFO_STS_TX_FIFO_FULL BIT(3)
64#define QSPI_FIFO_STS_TX_FIFO_EMPTY BIT(2)
65#define QSPI_FIFO_STS_RX_FIFO_FULL BIT(1)
66#define QSPI_FIFO_STS_RX_FIFO_EMPTY BIT(0)
67
68#define QSPI_TIMEOUT 1000
69
70struct qspi_regs {
71 u32 command1; /* 000:QSPI_COMMAND1 register */
72 u32 command2; /* 004:QSPI_COMMAND2 register */
73 u32 timing1; /* 008:QSPI_CS_TIM1 register */
74 u32 timing2; /* 00c:QSPI_CS_TIM2 register */
75 u32 xfer_status;/* 010:QSPI_TRANS_STATUS register */
76 u32 fifo_status;/* 014:QSPI_FIFO_STATUS register */
77 u32 tx_data; /* 018:QSPI_TX_DATA register */
78 u32 rx_data; /* 01c:QSPI_RX_DATA register */
79 u32 dma_ctl; /* 020:QSPI_DMA_CTL register */
80 u32 dma_blk; /* 024:QSPI_DMA_BLK register */
81 u32 rsvd[56]; /* 028-107 reserved */
82 u32 tx_fifo; /* 108:QSPI_FIFO1 register */
83 u32 rsvd2[31]; /* 10c-187 reserved */
84 u32 rx_fifo; /* 188:QSPI_FIFO2 register */
85 u32 spare_ctl; /* 18c:QSPI_SPARE_CTRL register */
86};
87
88struct tegra210_qspi_priv {
89 struct qspi_regs *regs;
90 unsigned int freq;
91 unsigned int mode;
92 int periph_id;
93 int valid;
94 int last_transaction_us;
95};
96
97static int tegra210_qspi_ofdata_to_platdata(struct udevice *bus)
98{
99 struct tegra_spi_platdata *plat = bus->platdata;
100 const void *blob = gd->fdt_blob;
Simon Glasse160f7d2017-01-17 16:52:55 -0700101 int node = dev_of_offset(bus);
Tom Warren4e675ff2015-10-12 14:50:54 -0700102
Simon Glassa821c4a2017-05-17 17:18:05 -0600103 plat->base = devfdt_get_addr(bus);
Simon Glass000f15f2017-07-25 08:30:00 -0600104 plat->periph_id = clock_decode_periph_id(bus);
Tom Warren4e675ff2015-10-12 14:50:54 -0700105
106 if (plat->periph_id == PERIPH_ID_NONE) {
107 debug("%s: could not decode periph id %d\n", __func__,
108 plat->periph_id);
109 return -FDT_ERR_NOTFOUND;
110 }
111
112 /* Use 500KHz as a suitable default */
113 plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
114 500000);
115 plat->deactivate_delay_us = fdtdec_get_int(blob, node,
116 "spi-deactivate-delay", 0);
117 debug("%s: base=%#08lx, periph_id=%d, max-frequency=%d, deactivate_delay=%d\n",
118 __func__, plat->base, plat->periph_id, plat->frequency,
119 plat->deactivate_delay_us);
120
121 return 0;
122}
123
124static int tegra210_qspi_probe(struct udevice *bus)
125{
126 struct tegra_spi_platdata *plat = dev_get_platdata(bus);
127 struct tegra210_qspi_priv *priv = dev_get_priv(bus);
128
129 priv->regs = (struct qspi_regs *)plat->base;
130
131 priv->last_transaction_us = timer_get_us();
132 priv->freq = plat->frequency;
133 priv->periph_id = plat->periph_id;
134
Stephen Warren4832c7f2016-08-18 10:53:33 -0600135 /* Change SPI clock to correct frequency, PLLP_OUT0 source */
136 clock_start_periph_pll(priv->periph_id, CLOCK_ID_PERIPH, priv->freq);
137
Tom Warren4e675ff2015-10-12 14:50:54 -0700138 return 0;
139}
140
Tom Warren3c8cf242019-11-12 13:17:37 -0700141static int tegra210_qspi_claim_bus(struct udevice *dev)
Tom Warren4e675ff2015-10-12 14:50:54 -0700142{
Tom Warren3c8cf242019-11-12 13:17:37 -0700143 struct udevice *bus = dev->parent;
Tom Warren4e675ff2015-10-12 14:50:54 -0700144 struct tegra210_qspi_priv *priv = dev_get_priv(bus);
145 struct qspi_regs *regs = priv->regs;
146
147 /* Change SPI clock to correct frequency, PLLP_OUT0 source */
148 clock_start_periph_pll(priv->periph_id, CLOCK_ID_PERIPH, priv->freq);
149
150 debug("%s: FIFO STATUS = %08x\n", __func__, readl(&regs->fifo_status));
151
152 /* Set master mode and sw controlled CS */
153 setbits_le32(&regs->command1, QSPI_CMD1_M_S | QSPI_CMD1_CS_SW_HW |
154 (priv->mode << QSPI_CMD1_MODE_SHIFT));
155 debug("%s: COMMAND1 = %08x\n", __func__, readl(&regs->command1));
156
157 return 0;
158}
159
160/**
161 * Activate the CS by driving it LOW
162 *
163 * @param slave Pointer to spi_slave to which controller has to
164 * communicate with
165 */
166static void spi_cs_activate(struct udevice *dev)
167{
168 struct udevice *bus = dev->parent;
169 struct tegra_spi_platdata *pdata = dev_get_platdata(bus);
170 struct tegra210_qspi_priv *priv = dev_get_priv(bus);
171
172 /* If it's too soon to do another transaction, wait */
173 if (pdata->deactivate_delay_us &&
174 priv->last_transaction_us) {
175 ulong delay_us; /* The delay completed so far */
176 delay_us = timer_get_us() - priv->last_transaction_us;
177 if (delay_us < pdata->deactivate_delay_us)
178 udelay(pdata->deactivate_delay_us - delay_us);
179 }
180
181 clrbits_le32(&priv->regs->command1, QSPI_CMD1_CS_SW_VAL);
182}
183
184/**
185 * Deactivate the CS by driving it HIGH
186 *
187 * @param slave Pointer to spi_slave to which controller has to
188 * communicate with
189 */
190static void spi_cs_deactivate(struct udevice *dev)
191{
192 struct udevice *bus = dev->parent;
193 struct tegra_spi_platdata *pdata = dev_get_platdata(bus);
194 struct tegra210_qspi_priv *priv = dev_get_priv(bus);
195
196 setbits_le32(&priv->regs->command1, QSPI_CMD1_CS_SW_VAL);
197
198 /* Remember time of this transaction so we can honour the bus delay */
199 if (pdata->deactivate_delay_us)
200 priv->last_transaction_us = timer_get_us();
201
202 debug("Deactivate CS, bus '%s'\n", bus->name);
203}
204
205static int tegra210_qspi_xfer(struct udevice *dev, unsigned int bitlen,
206 const void *data_out, void *data_in,
207 unsigned long flags)
208{
209 struct udevice *bus = dev->parent;
210 struct tegra210_qspi_priv *priv = dev_get_priv(bus);
211 struct qspi_regs *regs = priv->regs;
212 u32 reg, tmpdout, tmpdin = 0;
213 const u8 *dout = data_out;
214 u8 *din = data_in;
215 int num_bytes, tm, ret;
216
217 debug("%s: slave %u:%u dout %p din %p bitlen %u\n",
218 __func__, bus->seq, spi_chip_select(dev), dout, din, bitlen);
219 if (bitlen % 8)
220 return -1;
221 num_bytes = bitlen / 8;
222
223 ret = 0;
224
225 /* clear all error status bits */
226 reg = readl(&regs->fifo_status);
227 writel(reg, &regs->fifo_status);
228
229 /* flush RX/TX FIFOs */
230 setbits_le32(&regs->fifo_status,
231 (QSPI_FIFO_STS_RX_FIFO_FLUSH |
232 QSPI_FIFO_STS_TX_FIFO_FLUSH));
233
234 tm = QSPI_TIMEOUT;
235 while ((tm && readl(&regs->fifo_status) &
236 (QSPI_FIFO_STS_RX_FIFO_FLUSH |
237 QSPI_FIFO_STS_TX_FIFO_FLUSH))) {
238 tm--;
239 udelay(1);
240 }
241
242 if (!tm) {
243 printf("%s: timeout during QSPI FIFO flush!\n",
244 __func__);
245 return -1;
246 }
247
248 /*
249 * Notes:
250 * 1. don't set LSBY_FE, so no need to swap bytes from/to TX/RX FIFOs;
251 * 2. don't set RX_EN and TX_EN yet.
252 * (SW needs to make sure that while programming the blk_size,
253 * tx_en and rx_en bits must be zero)
254 * [TODO] I (Yen Lin) have problems when both RX/TX EN bits are set
255 * i.e., both dout and din are not NULL.
256 */
257 clrsetbits_le32(&regs->command1,
258 (QSPI_CMD1_LSBI_FE | QSPI_CMD1_LSBY_FE |
259 QSPI_CMD1_RX_EN | QSPI_CMD1_TX_EN),
260 (spi_chip_select(dev) << QSPI_CMD1_CS_SEL_SHIFT));
261
262 /* set xfer size to 1 block (32 bits) */
263 writel(0, &regs->dma_blk);
264
265 if (flags & SPI_XFER_BEGIN)
266 spi_cs_activate(dev);
267
268 /* handle data in 32-bit chunks */
269 while (num_bytes > 0) {
270 int bytes;
271
272 tmpdout = 0;
273 bytes = (num_bytes > 4) ? 4 : num_bytes;
274
275 if (dout != NULL) {
276 memcpy((void *)&tmpdout, (void *)dout, bytes);
277 dout += bytes;
278 num_bytes -= bytes;
279 writel(tmpdout, &regs->tx_fifo);
280 setbits_le32(&regs->command1, QSPI_CMD1_TX_EN);
281 }
282
283 if (din != NULL)
284 setbits_le32(&regs->command1, QSPI_CMD1_RX_EN);
285
286 /* clear ready bit */
287 setbits_le32(&regs->xfer_status, QSPI_XFER_STS_RDY);
288
289 clrsetbits_le32(&regs->command1,
290 QSPI_CMD1_BITLEN_MASK << QSPI_CMD1_BITLEN_SHIFT,
291 (bytes * 8 - 1) << QSPI_CMD1_BITLEN_SHIFT);
292
293 /* Need to stabilize other reg bits before GO bit set.
294 * As per the TRM:
295 * "For successful operation at various freq combinations,
296 * a minimum of 4-5 spi_clk cycle delay might be required
297 * before enabling the PIO or DMA bits. The worst case delay
298 * calculation can be done considering slowest qspi_clk as
299 * 1MHz. Based on that 1us delay should be enough before
300 * enabling PIO or DMA." Padded another 1us for safety.
301 */
302 udelay(2);
303 setbits_le32(&regs->command1, QSPI_CMD1_GO);
304 udelay(1);
305
306 /*
307 * Wait for SPI transmit FIFO to empty, or to time out.
308 * The RX FIFO status will be read and cleared last
309 */
310 for (tm = 0; tm < QSPI_TIMEOUT; ++tm) {
311 u32 fifo_status, xfer_status;
312
313 xfer_status = readl(&regs->xfer_status);
314 if (!(xfer_status & QSPI_XFER_STS_RDY))
315 continue;
316
317 fifo_status = readl(&regs->fifo_status);
318 if (fifo_status & QSPI_FIFO_STS_ERR) {
319 debug("%s: got a fifo error: ", __func__);
320 if (fifo_status & QSPI_FIFO_STS_TX_FIFO_OVF)
321 debug("tx FIFO overflow ");
322 if (fifo_status & QSPI_FIFO_STS_TX_FIFO_UNR)
323 debug("tx FIFO underrun ");
324 if (fifo_status & QSPI_FIFO_STS_RX_FIFO_OVF)
325 debug("rx FIFO overflow ");
326 if (fifo_status & QSPI_FIFO_STS_RX_FIFO_UNR)
327 debug("rx FIFO underrun ");
328 if (fifo_status & QSPI_FIFO_STS_TX_FIFO_FULL)
329 debug("tx FIFO full ");
330 if (fifo_status & QSPI_FIFO_STS_TX_FIFO_EMPTY)
331 debug("tx FIFO empty ");
332 if (fifo_status & QSPI_FIFO_STS_RX_FIFO_FULL)
333 debug("rx FIFO full ");
334 if (fifo_status & QSPI_FIFO_STS_RX_FIFO_EMPTY)
335 debug("rx FIFO empty ");
336 debug("\n");
337 break;
338 }
339
340 if (!(fifo_status & QSPI_FIFO_STS_RX_FIFO_EMPTY)) {
341 tmpdin = readl(&regs->rx_fifo);
342 if (din != NULL) {
343 memcpy(din, &tmpdin, bytes);
344 din += bytes;
345 num_bytes -= bytes;
346 }
347 }
348 break;
349 }
350
351 if (tm >= QSPI_TIMEOUT)
352 ret = tm;
353
354 /* clear ACK RDY, etc. bits */
355 writel(readl(&regs->fifo_status), &regs->fifo_status);
356 }
357
358 if (flags & SPI_XFER_END)
359 spi_cs_deactivate(dev);
360
361 debug("%s: transfer ended. Value=%08x, fifo_status = %08x\n",
362 __func__, tmpdin, readl(&regs->fifo_status));
363
364 if (ret) {
365 printf("%s: timeout during SPI transfer, tm %d\n",
366 __func__, ret);
367 return -1;
368 }
369
370 return ret;
371}
372
373static int tegra210_qspi_set_speed(struct udevice *bus, uint speed)
374{
375 struct tegra_spi_platdata *plat = bus->platdata;
376 struct tegra210_qspi_priv *priv = dev_get_priv(bus);
377
378 if (speed > plat->frequency)
379 speed = plat->frequency;
380 priv->freq = speed;
381 debug("%s: regs=%p, speed=%d\n", __func__, priv->regs, priv->freq);
382
383 return 0;
384}
385
386static int tegra210_qspi_set_mode(struct udevice *bus, uint mode)
387{
388 struct tegra210_qspi_priv *priv = dev_get_priv(bus);
389
390 priv->mode = mode;
391 debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode);
392
393 return 0;
394}
395
396static const struct dm_spi_ops tegra210_qspi_ops = {
397 .claim_bus = tegra210_qspi_claim_bus,
398 .xfer = tegra210_qspi_xfer,
399 .set_speed = tegra210_qspi_set_speed,
400 .set_mode = tegra210_qspi_set_mode,
401 /*
402 * cs_info is not needed, since we require all chip selects to be
403 * in the device tree explicitly
404 */
405};
406
407static const struct udevice_id tegra210_qspi_ids[] = {
408 { .compatible = "nvidia,tegra210-qspi" },
409 { }
410};
411
412U_BOOT_DRIVER(tegra210_qspi) = {
413 .name = "tegra210-qspi",
414 .id = UCLASS_SPI,
415 .of_match = tegra210_qspi_ids,
416 .ops = &tegra210_qspi_ops,
417 .ofdata_to_platdata = tegra210_qspi_ofdata_to_platdata,
418 .platdata_auto_alloc_size = sizeof(struct tegra_spi_platdata),
419 .priv_auto_alloc_size = sizeof(struct tegra210_qspi_priv),
420 .per_child_auto_alloc_size = sizeof(struct spi_slave),
421 .probe = tegra210_qspi_probe,
422};