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