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