CLK: ARC: HSDK: add separate clock map for HSDK-4xD
[oweals/u-boot.git] / drivers / spi / tegra210_qspi.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * NVIDIA Tegra210 QSPI controller driver
4  *
5  * (C) Copyright 2015-2020 NVIDIA Corporation <www.nvidia.com>
6  *
7  */
8
9 #include <common.h>
10 #include <dm.h>
11 #include <time.h>
12 #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
19 DECLARE_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_SHIFT        10
46 #define QSPI_CMD2_TX_CLK_TAP_DELAY_MASK GENMASK(14,10)
47 #define QSPI_CMD2_RX_CLK_TAP_DELAY_SHIFT        0
48 #define QSPI_CMD2_RX_CLK_TAP_DELAY_MASK GENMASK(7,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
70 struct 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
88 struct 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
97 static int tegra210_qspi_ofdata_to_platdata(struct udevice *bus)
98 {
99         struct tegra_spi_platdata *plat = bus->platdata;
100
101         plat->base = dev_read_addr(bus);
102         plat->periph_id = clock_decode_periph_id(bus);
103
104         if (plat->periph_id == PERIPH_ID_NONE) {
105                 debug("%s: could not decode periph id %d\n", __func__,
106                       plat->periph_id);
107                 return -FDT_ERR_NOTFOUND;
108         }
109
110         /* Use 500KHz as a suitable default */
111         plat->frequency = dev_read_u32_default(bus, "spi-max-frequency",
112                                                500000);
113         plat->deactivate_delay_us = dev_read_u32_default(bus,
114                                                          "spi-deactivate-delay",
115                                                          0);
116         debug("%s: base=%#08lx, periph_id=%d, max-frequency=%d, deactivate_delay=%d\n",
117               __func__, plat->base, plat->periph_id, plat->frequency,
118               plat->deactivate_delay_us);
119
120         return 0;
121 }
122
123 static int tegra210_qspi_probe(struct udevice *bus)
124 {
125         struct tegra_spi_platdata *plat = dev_get_platdata(bus);
126         struct tegra210_qspi_priv *priv = dev_get_priv(bus);
127
128         priv->regs = (struct qspi_regs *)plat->base;
129         struct qspi_regs *regs = priv->regs;
130
131         priv->last_transaction_us = timer_get_us();
132         priv->freq = plat->frequency;
133         priv->periph_id = plat->periph_id;
134
135         debug("%s: Freq = %u, id = %d\n", __func__, priv->freq,
136               priv->periph_id);
137         /* Change SPI clock to correct frequency, PLLP_OUT0 source */
138         clock_start_periph_pll(priv->periph_id, CLOCK_ID_PERIPH, priv->freq);
139
140         /* Set tap delays here, clock change above resets QSPI controller */
141         u32 reg = (0x09 << QSPI_CMD2_TX_CLK_TAP_DELAY_SHIFT) |
142                    (0x0C << QSPI_CMD2_RX_CLK_TAP_DELAY_SHIFT);
143         writel(reg, &regs->command2);
144         debug("%s: COMMAND2 = %08x\n", __func__, readl(&regs->command2));
145
146         return 0;
147 }
148
149 static int tegra210_qspi_claim_bus(struct udevice *dev)
150 {
151         struct udevice *bus = dev->parent;
152         struct tegra210_qspi_priv *priv = dev_get_priv(bus);
153         struct qspi_regs *regs = priv->regs;
154
155         debug("%s: FIFO STATUS = %08x\n", __func__, readl(&regs->fifo_status));
156
157         /* Set master mode and sw controlled CS */
158         setbits_le32(&regs->command1, QSPI_CMD1_M_S | QSPI_CMD1_CS_SW_HW |
159                      (priv->mode << QSPI_CMD1_MODE_SHIFT));
160         debug("%s: COMMAND1 = %08x\n", __func__, readl(&regs->command1));
161
162         return 0;
163 }
164
165 /**
166  * Activate the CS by driving it LOW
167  *
168  * @param slave Pointer to spi_slave to which controller has to
169  *              communicate with
170  */
171 static void spi_cs_activate(struct udevice *dev)
172 {
173         struct udevice *bus = dev->parent;
174         struct tegra_spi_platdata *pdata = dev_get_platdata(bus);
175         struct tegra210_qspi_priv *priv = dev_get_priv(bus);
176
177         /* If it's too soon to do another transaction, wait */
178         if (pdata->deactivate_delay_us &&
179             priv->last_transaction_us) {
180                 ulong delay_us;         /* The delay completed so far */
181                 delay_us = timer_get_us() - priv->last_transaction_us;
182                 if (delay_us < pdata->deactivate_delay_us)
183                         udelay(pdata->deactivate_delay_us - delay_us);
184         }
185
186         clrbits_le32(&priv->regs->command1, QSPI_CMD1_CS_SW_VAL);
187 }
188
189 /**
190  * Deactivate the CS by driving it HIGH
191  *
192  * @param slave Pointer to spi_slave to which controller has to
193  *              communicate with
194  */
195 static void spi_cs_deactivate(struct udevice *dev)
196 {
197         struct udevice *bus = dev->parent;
198         struct tegra_spi_platdata *pdata = dev_get_platdata(bus);
199         struct tegra210_qspi_priv *priv = dev_get_priv(bus);
200
201         setbits_le32(&priv->regs->command1, QSPI_CMD1_CS_SW_VAL);
202
203         /* Remember time of this transaction so we can honour the bus delay */
204         if (pdata->deactivate_delay_us)
205                 priv->last_transaction_us = timer_get_us();
206
207         debug("Deactivate CS, bus '%s'\n", bus->name);
208 }
209
210 static int tegra210_qspi_xfer(struct udevice *dev, unsigned int bitlen,
211                              const void *data_out, void *data_in,
212                              unsigned long flags)
213 {
214         struct udevice *bus = dev->parent;
215         struct tegra210_qspi_priv *priv = dev_get_priv(bus);
216         struct qspi_regs *regs = priv->regs;
217         u32 reg, tmpdout, tmpdin = 0;
218         const u8 *dout = data_out;
219         u8 *din = data_in;
220         int num_bytes, tm, ret;
221
222         debug("%s: slave %u:%u dout %p din %p bitlen %u\n",
223               __func__, bus->seq, spi_chip_select(dev), dout, din, bitlen);
224         if (bitlen % 8)
225                 return -1;
226         num_bytes = bitlen / 8;
227
228         ret = 0;
229
230         /* clear all error status bits */
231         reg = readl(&regs->fifo_status);
232         writel(reg, &regs->fifo_status);
233
234         /* flush RX/TX FIFOs */
235         setbits_le32(&regs->fifo_status,
236                      (QSPI_FIFO_STS_RX_FIFO_FLUSH |
237                       QSPI_FIFO_STS_TX_FIFO_FLUSH));
238
239         tm = QSPI_TIMEOUT;
240         while ((tm && readl(&regs->fifo_status) &
241                       (QSPI_FIFO_STS_RX_FIFO_FLUSH |
242                        QSPI_FIFO_STS_TX_FIFO_FLUSH))) {
243                 tm--;
244                 udelay(1);
245         }
246
247         if (!tm) {
248                 printf("%s: timeout during QSPI FIFO flush!\n",
249                        __func__);
250                 return -1;
251         }
252
253         /*
254          * Notes:
255          *   1. don't set LSBY_FE, so no need to swap bytes from/to TX/RX FIFOs;
256          *   2. don't set RX_EN and TX_EN yet.
257          *      (SW needs to make sure that while programming the blk_size,
258          *       tx_en and rx_en bits must be zero)
259          *      [TODO] I (Yen Lin) have problems when both RX/TX EN bits are set
260          *             i.e., both dout and din are not NULL.
261          */
262         clrsetbits_le32(&regs->command1,
263                         (QSPI_CMD1_LSBI_FE | QSPI_CMD1_LSBY_FE |
264                          QSPI_CMD1_RX_EN | QSPI_CMD1_TX_EN),
265                         (spi_chip_select(dev) << QSPI_CMD1_CS_SEL_SHIFT));
266
267         /* set xfer size to 1 block (32 bits) */
268         writel(0, &regs->dma_blk);
269
270         if (flags & SPI_XFER_BEGIN)
271                 spi_cs_activate(dev);
272
273         /* handle data in 32-bit chunks */
274         while (num_bytes > 0) {
275                 int bytes;
276
277                 tmpdout = 0;
278                 bytes = (num_bytes > 4) ?  4 : num_bytes;
279
280                 if (dout != NULL) {
281                         memcpy((void *)&tmpdout, (void *)dout, bytes);
282                         dout += bytes;
283                         num_bytes -= bytes;
284                         writel(tmpdout, &regs->tx_fifo);
285                         setbits_le32(&regs->command1, QSPI_CMD1_TX_EN);
286                 }
287
288                 if (din != NULL)
289                         setbits_le32(&regs->command1, QSPI_CMD1_RX_EN);
290
291                 /* clear ready bit */
292                 setbits_le32(&regs->xfer_status, QSPI_XFER_STS_RDY);
293
294                 clrsetbits_le32(&regs->command1,
295                                 QSPI_CMD1_BITLEN_MASK << QSPI_CMD1_BITLEN_SHIFT,
296                                 (bytes * 8 - 1) << QSPI_CMD1_BITLEN_SHIFT);
297
298                 /* Need to stabilize other reg bits before GO bit set.
299                  * As per the TRM:
300                  * "For successful operation at various freq combinations,
301                  * a minimum of 4-5 spi_clk cycle delay might be required
302                  * before enabling the PIO or DMA bits. The worst case delay
303                  * calculation can be done considering slowest qspi_clk as
304                  * 1MHz. Based on that 1us delay should be enough before
305                  * enabling PIO or DMA." Padded another 1us for safety.
306                  */
307                 udelay(2);
308                 setbits_le32(&regs->command1, QSPI_CMD1_GO);
309                 udelay(1);
310
311                 /*
312                  * Wait for SPI transmit FIFO to empty, or to time out.
313                  * The RX FIFO status will be read and cleared last
314                  */
315                 for (tm = 0; tm < QSPI_TIMEOUT; ++tm) {
316                         u32 fifo_status, xfer_status;
317
318                         xfer_status = readl(&regs->xfer_status);
319                         if (!(xfer_status & QSPI_XFER_STS_RDY))
320                                 continue;
321
322                         fifo_status = readl(&regs->fifo_status);
323                         if (fifo_status & QSPI_FIFO_STS_ERR) {
324                                 debug("%s: got a fifo error: ", __func__);
325                                 if (fifo_status & QSPI_FIFO_STS_TX_FIFO_OVF)
326                                         debug("tx FIFO overflow ");
327                                 if (fifo_status & QSPI_FIFO_STS_TX_FIFO_UNR)
328                                         debug("tx FIFO underrun ");
329                                 if (fifo_status & QSPI_FIFO_STS_RX_FIFO_OVF)
330                                         debug("rx FIFO overflow ");
331                                 if (fifo_status & QSPI_FIFO_STS_RX_FIFO_UNR)
332                                         debug("rx FIFO underrun ");
333                                 if (fifo_status & QSPI_FIFO_STS_TX_FIFO_FULL)
334                                         debug("tx FIFO full ");
335                                 if (fifo_status & QSPI_FIFO_STS_TX_FIFO_EMPTY)
336                                         debug("tx FIFO empty ");
337                                 if (fifo_status & QSPI_FIFO_STS_RX_FIFO_FULL)
338                                         debug("rx FIFO full ");
339                                 if (fifo_status & QSPI_FIFO_STS_RX_FIFO_EMPTY)
340                                         debug("rx FIFO empty ");
341                                 debug("\n");
342                                 break;
343                         }
344
345                         if (!(fifo_status & QSPI_FIFO_STS_RX_FIFO_EMPTY)) {
346                                 tmpdin = readl(&regs->rx_fifo);
347                                 if (din != NULL) {
348                                         memcpy(din, &tmpdin, bytes);
349                                         din += bytes;
350                                         num_bytes -= bytes;
351                                 }
352                         }
353                         break;
354                 }
355
356                 if (tm >= QSPI_TIMEOUT)
357                         ret = tm;
358
359                 /* clear ACK RDY, etc. bits */
360                 writel(readl(&regs->fifo_status), &regs->fifo_status);
361         }
362
363         if (flags & SPI_XFER_END)
364                 spi_cs_deactivate(dev);
365
366         debug("%s: transfer ended. Value=%08x, fifo_status = %08x\n",
367               __func__, tmpdin, readl(&regs->fifo_status));
368
369         if (ret) {
370                 printf("%s: timeout during SPI transfer, tm %d\n",
371                        __func__, ret);
372                 return -1;
373         }
374
375         return ret;
376 }
377
378 static int tegra210_qspi_set_speed(struct udevice *bus, uint speed)
379 {
380         struct tegra_spi_platdata *plat = bus->platdata;
381         struct tegra210_qspi_priv *priv = dev_get_priv(bus);
382
383         if (speed > plat->frequency)
384                 speed = plat->frequency;
385         priv->freq = speed;
386         debug("%s: regs=%p, speed=%d\n", __func__, priv->regs, priv->freq);
387
388         return 0;
389 }
390
391 static int tegra210_qspi_set_mode(struct udevice *bus, uint mode)
392 {
393         struct tegra210_qspi_priv *priv = dev_get_priv(bus);
394
395         priv->mode = mode;
396         debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode);
397
398         return 0;
399 }
400
401 static const struct dm_spi_ops tegra210_qspi_ops = {
402         .claim_bus      = tegra210_qspi_claim_bus,
403         .xfer           = tegra210_qspi_xfer,
404         .set_speed      = tegra210_qspi_set_speed,
405         .set_mode       = tegra210_qspi_set_mode,
406         /*
407          * cs_info is not needed, since we require all chip selects to be
408          * in the device tree explicitly
409          */
410 };
411
412 static const struct udevice_id tegra210_qspi_ids[] = {
413         { .compatible = "nvidia,tegra210-qspi" },
414         { }
415 };
416
417 U_BOOT_DRIVER(tegra210_qspi) = {
418         .name = "tegra210-qspi",
419         .id = UCLASS_SPI,
420         .of_match = tegra210_qspi_ids,
421         .ops = &tegra210_qspi_ops,
422         .ofdata_to_platdata = tegra210_qspi_ofdata_to_platdata,
423         .platdata_auto_alloc_size = sizeof(struct tegra_spi_platdata),
424         .priv_auto_alloc_size = sizeof(struct tegra210_qspi_priv),
425         .per_child_auto_alloc_size = sizeof(struct spi_slave),
426         .probe = tegra210_qspi_probe,
427 };