common: Drop linux/delay.h from common header
[oweals/u-boot.git] / drivers / spi / tegra20_sflash.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2010-2013 NVIDIA Corporation
4  * With help from the mpc8xxx SPI driver
5  * With more help from omap3_spi SPI driver
6  */
7
8 #include <common.h>
9 #include <dm.h>
10 #include <errno.h>
11 #include <log.h>
12 #include <time.h>
13 #include <asm/io.h>
14 #include <asm/gpio.h>
15 #include <asm/arch/clock.h>
16 #include <asm/arch/pinmux.h>
17 #include <asm/arch-tegra/clk_rst.h>
18 #include <spi.h>
19 #include <fdtdec.h>
20 #include <linux/delay.h>
21 #include "tegra_spi.h"
22
23 DECLARE_GLOBAL_DATA_PTR;
24
25 #define SPI_CMD_GO                      BIT(30)
26 #define SPI_CMD_ACTIVE_SCLK_SHIFT       26
27 #define SPI_CMD_ACTIVE_SCLK_MASK        (3 << SPI_CMD_ACTIVE_SCLK_SHIFT)
28 #define SPI_CMD_CK_SDA                  BIT(21)
29 #define SPI_CMD_ACTIVE_SDA_SHIFT        18
30 #define SPI_CMD_ACTIVE_SDA_MASK         (3 << SPI_CMD_ACTIVE_SDA_SHIFT)
31 #define SPI_CMD_CS_POL                  BIT(16)
32 #define SPI_CMD_TXEN                    BIT(15)
33 #define SPI_CMD_RXEN                    BIT(14)
34 #define SPI_CMD_CS_VAL                  BIT(13)
35 #define SPI_CMD_CS_SOFT                 BIT(12)
36 #define SPI_CMD_CS_DELAY                BIT(9)
37 #define SPI_CMD_CS3_EN                  BIT(8)
38 #define SPI_CMD_CS2_EN                  BIT(7)
39 #define SPI_CMD_CS1_EN                  BIT(6)
40 #define SPI_CMD_CS0_EN                  BIT(5)
41 #define SPI_CMD_BIT_LENGTH              BIT(4)
42 #define SPI_CMD_BIT_LENGTH_MASK         GENMASK(4, 0)
43
44 #define SPI_STAT_BSY                    BIT(31)
45 #define SPI_STAT_RDY                    BIT(30)
46 #define SPI_STAT_RXF_FLUSH              BIT(29)
47 #define SPI_STAT_TXF_FLUSH              BIT(28)
48 #define SPI_STAT_RXF_UNR                BIT(27)
49 #define SPI_STAT_TXF_OVF                BIT(26)
50 #define SPI_STAT_RXF_EMPTY              BIT(25)
51 #define SPI_STAT_RXF_FULL               BIT(24)
52 #define SPI_STAT_TXF_EMPTY              BIT(23)
53 #define SPI_STAT_TXF_FULL               BIT(22)
54 #define SPI_STAT_SEL_TXRX_N             BIT(16)
55 #define SPI_STAT_CUR_BLKCNT             BIT(15)
56
57 #define SPI_TIMEOUT             1000
58 #define TEGRA_SPI_MAX_FREQ      52000000
59
60 struct spi_regs {
61         u32 command;    /* SPI_COMMAND_0 register  */
62         u32 status;     /* SPI_STATUS_0 register */
63         u32 rx_cmp;     /* SPI_RX_CMP_0 register  */
64         u32 dma_ctl;    /* SPI_DMA_CTL_0 register */
65         u32 tx_fifo;    /* SPI_TX_FIFO_0 register */
66         u32 rsvd[3];    /* offsets 0x14 to 0x1F reserved */
67         u32 rx_fifo;    /* SPI_RX_FIFO_0 register */
68 };
69
70 struct tegra20_sflash_priv {
71         struct spi_regs *regs;
72         unsigned int freq;
73         unsigned int mode;
74         int periph_id;
75         int valid;
76         int last_transaction_us;
77 };
78
79 int tegra20_sflash_cs_info(struct udevice *bus, unsigned int cs,
80                            struct spi_cs_info *info)
81 {
82         /* Tegra20 SPI-Flash - only 1 device ('bus/cs') */
83         if (cs != 0)
84                 return -EINVAL;
85         else
86                 return 0;
87 }
88
89 static int tegra20_sflash_ofdata_to_platdata(struct udevice *bus)
90 {
91         struct tegra_spi_platdata *plat = bus->platdata;
92         const void *blob = gd->fdt_blob;
93         int node = dev_of_offset(bus);
94
95         plat->base = devfdt_get_addr(bus);
96         plat->periph_id = clock_decode_periph_id(bus);
97
98         if (plat->periph_id == PERIPH_ID_NONE) {
99                 debug("%s: could not decode periph id %d\n", __func__,
100                       plat->periph_id);
101                 return -FDT_ERR_NOTFOUND;
102         }
103
104         /* Use 500KHz as a suitable default */
105         plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
106                                         500000);
107         plat->deactivate_delay_us = fdtdec_get_int(blob, node,
108                                         "spi-deactivate-delay", 0);
109         debug("%s: base=%#08lx, periph_id=%d, max-frequency=%d, deactivate_delay=%d\n",
110               __func__, plat->base, plat->periph_id, plat->frequency,
111               plat->deactivate_delay_us);
112
113         return 0;
114 }
115
116 static int tegra20_sflash_probe(struct udevice *bus)
117 {
118         struct tegra_spi_platdata *plat = dev_get_platdata(bus);
119         struct tegra20_sflash_priv *priv = dev_get_priv(bus);
120
121         priv->regs = (struct spi_regs *)plat->base;
122
123         priv->last_transaction_us = timer_get_us();
124         priv->freq = plat->frequency;
125         priv->periph_id = plat->periph_id;
126
127         /* Change SPI clock to correct frequency, PLLP_OUT0 source */
128         clock_start_periph_pll(priv->periph_id, CLOCK_ID_PERIPH,
129                                priv->freq);
130
131         return 0;
132 }
133
134 static int tegra20_sflash_claim_bus(struct udevice *dev)
135 {
136         struct udevice *bus = dev->parent;
137         struct tegra20_sflash_priv *priv = dev_get_priv(bus);
138         struct spi_regs *regs = priv->regs;
139         u32 reg;
140
141         /* Change SPI clock to correct frequency, PLLP_OUT0 source */
142         clock_start_periph_pll(priv->periph_id, CLOCK_ID_PERIPH,
143                                priv->freq);
144
145         /* Clear stale status here */
146         reg = SPI_STAT_RDY | SPI_STAT_RXF_FLUSH | SPI_STAT_TXF_FLUSH | \
147                 SPI_STAT_RXF_UNR | SPI_STAT_TXF_OVF;
148         writel(reg, &regs->status);
149         debug("%s: STATUS = %08x\n", __func__, readl(&regs->status));
150
151         /*
152          * Use sw-controlled CS, so we can clock in data after ReadID, etc.
153          */
154         reg = (priv->mode & 1) << SPI_CMD_ACTIVE_SDA_SHIFT;
155         if (priv->mode & 2)
156                 reg |= 1 << SPI_CMD_ACTIVE_SCLK_SHIFT;
157         clrsetbits_le32(&regs->command, SPI_CMD_ACTIVE_SCLK_MASK |
158                 SPI_CMD_ACTIVE_SDA_MASK, SPI_CMD_CS_SOFT | reg);
159         debug("%s: COMMAND = %08x\n", __func__, readl(&regs->command));
160
161         /*
162          * SPI pins on Tegra20 are muxed - change pinmux later due to UART
163          * issue.
164          */
165         pinmux_set_func(PMUX_PINGRP_GMD, PMUX_FUNC_SFLASH);
166         pinmux_tristate_disable(PMUX_PINGRP_LSPI);
167         pinmux_set_func(PMUX_PINGRP_GMC, PMUX_FUNC_SFLASH);
168
169         return 0;
170 }
171
172 static 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 tegra20_sflash_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         /* CS is negated on Tegra, so drive a 1 to get a 0 */
188         setbits_le32(&priv->regs->command, SPI_CMD_CS_VAL);
189 }
190
191 static void spi_cs_deactivate(struct udevice *dev)
192 {
193         struct udevice *bus = dev->parent;
194         struct tegra_spi_platdata *pdata = dev_get_platdata(bus);
195         struct tegra20_sflash_priv *priv = dev_get_priv(bus);
196
197         /* CS is negated on Tegra, so drive a 0 to get a 1 */
198         clrbits_le32(&priv->regs->command, SPI_CMD_CS_VAL);
199
200         /* Remember time of this transaction so we can honour the bus delay */
201         if (pdata->deactivate_delay_us)
202                 priv->last_transaction_us = timer_get_us();
203 }
204
205 static int tegra20_sflash_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 tegra20_sflash_priv *priv = dev_get_priv(bus);
211         struct spi_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;
216         int ret;
217
218         debug("%s: slave %u:%u dout %p din %p bitlen %u\n",
219               __func__, bus->seq, spi_chip_select(dev), dout, din, bitlen);
220         if (bitlen % 8)
221                 return -1;
222         num_bytes = bitlen / 8;
223
224         ret = 0;
225
226         reg = readl(&regs->status);
227         writel(reg, &regs->status);     /* Clear all SPI events via R/W */
228         debug("spi_xfer entry: STATUS = %08x\n", reg);
229
230         reg = readl(&regs->command);
231         reg |= SPI_CMD_TXEN | SPI_CMD_RXEN;
232         writel(reg, &regs->command);
233         debug("spi_xfer: COMMAND = %08x\n", readl(&regs->command));
234
235         if (flags & SPI_XFER_BEGIN)
236                 spi_cs_activate(dev);
237
238         /* handle data in 32-bit chunks */
239         while (num_bytes > 0) {
240                 int bytes;
241                 int is_read = 0;
242                 int tm, i;
243
244                 tmpdout = 0;
245                 bytes = (num_bytes > 4) ?  4 : num_bytes;
246
247                 if (dout != NULL) {
248                         for (i = 0; i < bytes; ++i)
249                                 tmpdout = (tmpdout << 8) | dout[i];
250                 }
251
252                 num_bytes -= bytes;
253                 if (dout)
254                         dout += bytes;
255
256                 clrsetbits_le32(&regs->command, SPI_CMD_BIT_LENGTH_MASK,
257                                 bytes * 8 - 1);
258                 writel(tmpdout, &regs->tx_fifo);
259                 setbits_le32(&regs->command, SPI_CMD_GO);
260
261                 /*
262                  * Wait for SPI transmit FIFO to empty, or to time out.
263                  * The RX FIFO status will be read and cleared last
264                  */
265                 for (tm = 0, is_read = 0; tm < SPI_TIMEOUT; ++tm) {
266                         u32 status;
267
268                         status = readl(&regs->status);
269
270                         /* We can exit when we've had both RX and TX activity */
271                         if (is_read && (status & SPI_STAT_TXF_EMPTY))
272                                 break;
273
274                         if ((status & (SPI_STAT_BSY | SPI_STAT_RDY)) !=
275                                         SPI_STAT_RDY)
276                                 tm++;
277
278                         else if (!(status & SPI_STAT_RXF_EMPTY)) {
279                                 tmpdin = readl(&regs->rx_fifo);
280                                 is_read = 1;
281
282                                 /* swap bytes read in */
283                                 if (din != NULL) {
284                                         for (i = bytes - 1; i >= 0; --i) {
285                                                 din[i] = tmpdin & 0xff;
286                                                 tmpdin >>= 8;
287                                         }
288                                         din += bytes;
289                                 }
290                         }
291                 }
292
293                 if (tm >= SPI_TIMEOUT)
294                         ret = tm;
295
296                 /* clear ACK RDY, etc. bits */
297                 writel(readl(&regs->status), &regs->status);
298         }
299
300         if (flags & SPI_XFER_END)
301                 spi_cs_deactivate(dev);
302
303         debug("spi_xfer: transfer ended. Value=%08x, status = %08x\n",
304                 tmpdin, readl(&regs->status));
305
306         if (ret) {
307                 printf("spi_xfer: timeout during SPI transfer, tm %d\n", ret);
308                 return -1;
309         }
310
311         return 0;
312 }
313
314 static int tegra20_sflash_set_speed(struct udevice *bus, uint speed)
315 {
316         struct tegra_spi_platdata *plat = bus->platdata;
317         struct tegra20_sflash_priv *priv = dev_get_priv(bus);
318
319         if (speed > plat->frequency)
320                 speed = plat->frequency;
321         priv->freq = speed;
322         debug("%s: regs=%p, speed=%d\n", __func__, priv->regs, priv->freq);
323
324         return 0;
325 }
326
327 static int tegra20_sflash_set_mode(struct udevice *bus, uint mode)
328 {
329         struct tegra20_sflash_priv *priv = dev_get_priv(bus);
330
331         priv->mode = mode;
332         debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode);
333
334         return 0;
335 }
336
337 static const struct dm_spi_ops tegra20_sflash_ops = {
338         .claim_bus      = tegra20_sflash_claim_bus,
339         .xfer           = tegra20_sflash_xfer,
340         .set_speed      = tegra20_sflash_set_speed,
341         .set_mode       = tegra20_sflash_set_mode,
342         .cs_info        = tegra20_sflash_cs_info,
343 };
344
345 static const struct udevice_id tegra20_sflash_ids[] = {
346         { .compatible = "nvidia,tegra20-sflash" },
347         { }
348 };
349
350 U_BOOT_DRIVER(tegra20_sflash) = {
351         .name   = "tegra20_sflash",
352         .id     = UCLASS_SPI,
353         .of_match = tegra20_sflash_ids,
354         .ops    = &tegra20_sflash_ops,
355         .ofdata_to_platdata = tegra20_sflash_ofdata_to_platdata,
356         .platdata_auto_alloc_size = sizeof(struct tegra_spi_platdata),
357         .priv_auto_alloc_size = sizeof(struct tegra20_sflash_priv),
358         .probe  = tegra20_sflash_probe,
359 };