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