blob: 3a1ef8c093522e49047f91909d027dc15a30f4f0 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Tom Warrenee554f82011-11-05 09:48:11 +00002/*
Allen Martin73e0f1b2013-03-16 18:58:06 +00003 * Copyright (c) 2010-2013 NVIDIA Corporation
Tom Warrenee554f82011-11-05 09:48:11 +00004 * With help from the mpc8xxx SPI driver
5 * With more help from omap3_spi SPI driver
Tom Warrenee554f82011-11-05 09:48:11 +00006 */
7
8#include <common.h>
Simon Glass1121b1b2014-10-13 23:42:13 -06009#include <dm.h>
10#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070012#include <time.h>
Tom Warrenee554f82011-11-05 09:48:11 +000013#include <asm/io.h>
14#include <asm/gpio.h>
Tom Warrenee554f82011-11-05 09:48:11 +000015#include <asm/arch/clock.h>
16#include <asm/arch/pinmux.h>
Tom Warrenab371962012-09-19 15:50:56 -070017#include <asm/arch-tegra/clk_rst.h>
Tom Warrenab371962012-09-19 15:50:56 -070018#include <spi.h>
Allen Martine7659522013-01-29 13:51:24 +000019#include <fdtdec.h>
Simon Glass1121b1b2014-10-13 23:42:13 -060020#include "tegra_spi.h"
Allen Martine7659522013-01-29 13:51:24 +000021
22DECLARE_GLOBAL_DATA_PTR;
Tom Warrenee554f82011-11-05 09:48:11 +000023
Jagan Teki7f7ccf72015-10-23 01:39:06 +053024#define SPI_CMD_GO BIT(30)
Allen Martin8db241b2013-03-16 18:58:05 +000025#define SPI_CMD_ACTIVE_SCLK_SHIFT 26
26#define SPI_CMD_ACTIVE_SCLK_MASK (3 << SPI_CMD_ACTIVE_SCLK_SHIFT)
Jagan Teki7f7ccf72015-10-23 01:39:06 +053027#define SPI_CMD_CK_SDA BIT(21)
Allen Martin8db241b2013-03-16 18:58:05 +000028#define SPI_CMD_ACTIVE_SDA_SHIFT 18
29#define SPI_CMD_ACTIVE_SDA_MASK (3 << SPI_CMD_ACTIVE_SDA_SHIFT)
Jagan Teki7f7ccf72015-10-23 01:39:06 +053030#define SPI_CMD_CS_POL BIT(16)
31#define SPI_CMD_TXEN BIT(15)
32#define SPI_CMD_RXEN BIT(14)
33#define SPI_CMD_CS_VAL BIT(13)
34#define SPI_CMD_CS_SOFT BIT(12)
35#define SPI_CMD_CS_DELAY BIT(9)
36#define SPI_CMD_CS3_EN BIT(8)
37#define SPI_CMD_CS2_EN BIT(7)
38#define SPI_CMD_CS1_EN BIT(6)
39#define SPI_CMD_CS0_EN BIT(5)
40#define SPI_CMD_BIT_LENGTH BIT(4)
Jagan Teki54badcb2015-10-23 01:03:10 +053041#define SPI_CMD_BIT_LENGTH_MASK GENMASK(4, 0)
Allen Martin8db241b2013-03-16 18:58:05 +000042
Jagan Teki7f7ccf72015-10-23 01:39:06 +053043#define SPI_STAT_BSY BIT(31)
44#define SPI_STAT_RDY BIT(30)
45#define SPI_STAT_RXF_FLUSH BIT(29)
46#define SPI_STAT_TXF_FLUSH BIT(28)
47#define SPI_STAT_RXF_UNR BIT(27)
48#define SPI_STAT_TXF_OVF BIT(26)
49#define SPI_STAT_RXF_EMPTY BIT(25)
50#define SPI_STAT_RXF_FULL BIT(24)
51#define SPI_STAT_TXF_EMPTY BIT(23)
52#define SPI_STAT_TXF_FULL BIT(22)
53#define SPI_STAT_SEL_TXRX_N BIT(16)
54#define SPI_STAT_CUR_BLKCNT BIT(15)
Allen Martin8db241b2013-03-16 18:58:05 +000055
56#define SPI_TIMEOUT 1000
57#define TEGRA_SPI_MAX_FREQ 52000000
58
59struct spi_regs {
60 u32 command; /* SPI_COMMAND_0 register */
61 u32 status; /* SPI_STATUS_0 register */
62 u32 rx_cmp; /* SPI_RX_CMP_0 register */
63 u32 dma_ctl; /* SPI_DMA_CTL_0 register */
64 u32 tx_fifo; /* SPI_TX_FIFO_0 register */
65 u32 rsvd[3]; /* offsets 0x14 to 0x1F reserved */
66 u32 rx_fifo; /* SPI_RX_FIFO_0 register */
67};
68
Simon Glass1121b1b2014-10-13 23:42:13 -060069struct tegra20_sflash_priv {
Allen Martin8db241b2013-03-16 18:58:05 +000070 struct spi_regs *regs;
Tom Warrenee554f82011-11-05 09:48:11 +000071 unsigned int freq;
72 unsigned int mode;
Allen Martine7659522013-01-29 13:51:24 +000073 int periph_id;
Allen Martin73e0f1b2013-03-16 18:58:06 +000074 int valid;
Simon Glass1121b1b2014-10-13 23:42:13 -060075 int last_transaction_us;
Allen Martin73e0f1b2013-03-16 18:58:06 +000076};
77
Simon Glass1121b1b2014-10-13 23:42:13 -060078int tegra20_sflash_cs_info(struct udevice *bus, unsigned int cs,
79 struct spi_cs_info *info)
Tom Warrenee554f82011-11-05 09:48:11 +000080{
Allen Martin55d98a12012-08-31 08:30:00 +000081 /* Tegra20 SPI-Flash - only 1 device ('bus/cs') */
Simon Glass1121b1b2014-10-13 23:42:13 -060082 if (cs != 0)
Bin Mengf8586f62019-09-09 06:00:01 -070083 return -EINVAL;
Tom Warrenee554f82011-11-05 09:48:11 +000084 else
Simon Glass1121b1b2014-10-13 23:42:13 -060085 return 0;
Tom Warrenee554f82011-11-05 09:48:11 +000086}
87
Simon Glass1121b1b2014-10-13 23:42:13 -060088static int tegra20_sflash_ofdata_to_platdata(struct udevice *bus)
Tom Warrenee554f82011-11-05 09:48:11 +000089{
Simon Glass1121b1b2014-10-13 23:42:13 -060090 struct tegra_spi_platdata *plat = bus->platdata;
91 const void *blob = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -070092 int node = dev_of_offset(bus);
Tom Warrenee554f82011-11-05 09:48:11 +000093
Simon Glassba1dea42017-05-17 17:18:05 -060094 plat->base = devfdt_get_addr(bus);
Simon Glassc3f26502017-07-25 08:30:00 -060095 plat->periph_id = clock_decode_periph_id(bus);
Tom Warrenee554f82011-11-05 09:48:11 +000096
Simon Glass1121b1b2014-10-13 23:42:13 -060097 if (plat->periph_id == PERIPH_ID_NONE) {
98 debug("%s: could not decode periph id %d\n", __func__,
99 plat->periph_id);
100 return -FDT_ERR_NOTFOUND;
Tom Warrenee554f82011-11-05 09:48:11 +0000101 }
102
Simon Glass1121b1b2014-10-13 23:42:13 -0600103 /* Use 500KHz as a suitable default */
104 plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
105 500000);
106 plat->deactivate_delay_us = fdtdec_get_int(blob, node,
107 "spi-deactivate-delay", 0);
108 debug("%s: base=%#08lx, periph_id=%d, max-frequency=%d, deactivate_delay=%d\n",
109 __func__, plat->base, plat->periph_id, plat->frequency,
110 plat->deactivate_delay_us);
Allen Martin73e0f1b2013-03-16 18:58:06 +0000111
Simon Glass1121b1b2014-10-13 23:42:13 -0600112 return 0;
Tom Warrenee554f82011-11-05 09:48:11 +0000113}
114
Simon Glass1121b1b2014-10-13 23:42:13 -0600115static int tegra20_sflash_probe(struct udevice *bus)
Tom Warrenee554f82011-11-05 09:48:11 +0000116{
Simon Glass1121b1b2014-10-13 23:42:13 -0600117 struct tegra_spi_platdata *plat = dev_get_platdata(bus);
118 struct tegra20_sflash_priv *priv = dev_get_priv(bus);
Tom Warrenee554f82011-11-05 09:48:11 +0000119
Simon Glass1121b1b2014-10-13 23:42:13 -0600120 priv->regs = (struct spi_regs *)plat->base;
Allen Martin73e0f1b2013-03-16 18:58:06 +0000121
Simon Glass1121b1b2014-10-13 23:42:13 -0600122 priv->last_transaction_us = timer_get_us();
123 priv->freq = plat->frequency;
124 priv->periph_id = plat->periph_id;
Allen Martin73e0f1b2013-03-16 18:58:06 +0000125
Stephen Warrenb68a9942016-08-18 10:53:33 -0600126 /* Change SPI clock to correct frequency, PLLP_OUT0 source */
127 clock_start_periph_pll(priv->periph_id, CLOCK_ID_PERIPH,
128 priv->freq);
129
Simon Glass1121b1b2014-10-13 23:42:13 -0600130 return 0;
Tom Warrenee554f82011-11-05 09:48:11 +0000131}
132
Simon Glass5c74fba2015-04-19 09:05:40 -0600133static int tegra20_sflash_claim_bus(struct udevice *dev)
Tom Warrenee554f82011-11-05 09:48:11 +0000134{
Simon Glass5c74fba2015-04-19 09:05:40 -0600135 struct udevice *bus = dev->parent;
Simon Glass1121b1b2014-10-13 23:42:13 -0600136 struct tegra20_sflash_priv *priv = dev_get_priv(bus);
137 struct spi_regs *regs = priv->regs;
Tom Warrenee554f82011-11-05 09:48:11 +0000138 u32 reg;
139
140 /* Change SPI clock to correct frequency, PLLP_OUT0 source */
Simon Glass1121b1b2014-10-13 23:42:13 -0600141 clock_start_periph_pll(priv->periph_id, CLOCK_ID_PERIPH,
142 priv->freq);
Tom Warrenee554f82011-11-05 09:48:11 +0000143
144 /* Clear stale status here */
145 reg = SPI_STAT_RDY | SPI_STAT_RXF_FLUSH | SPI_STAT_TXF_FLUSH | \
146 SPI_STAT_RXF_UNR | SPI_STAT_TXF_OVF;
147 writel(reg, &regs->status);
Allen Martinb98691c2013-03-16 18:58:07 +0000148 debug("%s: STATUS = %08x\n", __func__, readl(&regs->status));
Tom Warrenee554f82011-11-05 09:48:11 +0000149
150 /*
151 * Use sw-controlled CS, so we can clock in data after ReadID, etc.
152 */
Simon Glass1121b1b2014-10-13 23:42:13 -0600153 reg = (priv->mode & 1) << SPI_CMD_ACTIVE_SDA_SHIFT;
154 if (priv->mode & 2)
Tom Warrenee554f82011-11-05 09:48:11 +0000155 reg |= 1 << SPI_CMD_ACTIVE_SCLK_SHIFT;
156 clrsetbits_le32(&regs->command, SPI_CMD_ACTIVE_SCLK_MASK |
157 SPI_CMD_ACTIVE_SDA_MASK, SPI_CMD_CS_SOFT | reg);
Allen Martinb98691c2013-03-16 18:58:07 +0000158 debug("%s: COMMAND = %08x\n", __func__, readl(&regs->command));
Tom Warrenee554f82011-11-05 09:48:11 +0000159
160 /*
Allen Martin55d98a12012-08-31 08:30:00 +0000161 * SPI pins on Tegra20 are muxed - change pinmux later due to UART
Tom Warrenee554f82011-11-05 09:48:11 +0000162 * issue.
163 */
Stephen Warrenf27f4e82014-03-21 12:28:58 -0600164 pinmux_set_func(PMUX_PINGRP_GMD, PMUX_FUNC_SFLASH);
165 pinmux_tristate_disable(PMUX_PINGRP_LSPI);
166 pinmux_set_func(PMUX_PINGRP_GMC, PMUX_FUNC_SFLASH);
Simon Glass34bad072011-11-05 04:46:50 +0000167
Tom Warrenee554f82011-11-05 09:48:11 +0000168 return 0;
169}
170
Simon Glass1121b1b2014-10-13 23:42:13 -0600171static void spi_cs_activate(struct udevice *dev)
Tom Warrenee554f82011-11-05 09:48:11 +0000172{
Simon Glass1121b1b2014-10-13 23:42:13 -0600173 struct udevice *bus = dev->parent;
174 struct tegra_spi_platdata *pdata = dev_get_platdata(bus);
175 struct tegra20_sflash_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 }
Tom Warrenee554f82011-11-05 09:48:11 +0000185
Tom Warrenee554f82011-11-05 09:48:11 +0000186 /* CS is negated on Tegra, so drive a 1 to get a 0 */
Simon Glass1121b1b2014-10-13 23:42:13 -0600187 setbits_le32(&priv->regs->command, SPI_CMD_CS_VAL);
Tom Warrenee554f82011-11-05 09:48:11 +0000188}
189
Simon Glass1121b1b2014-10-13 23:42:13 -0600190static void spi_cs_deactivate(struct udevice *dev)
Tom Warrenee554f82011-11-05 09:48:11 +0000191{
Simon Glass1121b1b2014-10-13 23:42:13 -0600192 struct udevice *bus = dev->parent;
193 struct tegra_spi_platdata *pdata = dev_get_platdata(bus);
194 struct tegra20_sflash_priv *priv = dev_get_priv(bus);
Tom Warrenee554f82011-11-05 09:48:11 +0000195
196 /* CS is negated on Tegra, so drive a 0 to get a 1 */
Simon Glass1121b1b2014-10-13 23:42:13 -0600197 clrbits_le32(&priv->regs->command, SPI_CMD_CS_VAL);
198
199 /* Remember time of this transaction so we can honour the bus delay */
200 if (pdata->deactivate_delay_us)
201 priv->last_transaction_us = timer_get_us();
Tom Warrenee554f82011-11-05 09:48:11 +0000202}
203
Simon Glass1121b1b2014-10-13 23:42:13 -0600204static int tegra20_sflash_xfer(struct udevice *dev, unsigned int bitlen,
205 const void *data_out, void *data_in,
206 unsigned long flags)
Tom Warrenee554f82011-11-05 09:48:11 +0000207{
Simon Glass1121b1b2014-10-13 23:42:13 -0600208 struct udevice *bus = dev->parent;
209 struct tegra20_sflash_priv *priv = dev_get_priv(bus);
210 struct spi_regs *regs = priv->regs;
Tom Warrenee554f82011-11-05 09:48:11 +0000211 u32 reg, tmpdout, tmpdin = 0;
212 const u8 *dout = data_out;
213 u8 *din = data_in;
214 int num_bytes;
215 int ret;
216
Simon Glass1121b1b2014-10-13 23:42:13 -0600217 debug("%s: slave %u:%u dout %p din %p bitlen %u\n",
218 __func__, bus->seq, spi_chip_select(dev), dout, din, bitlen);
Tom Warrenee554f82011-11-05 09:48:11 +0000219 if (bitlen % 8)
220 return -1;
221 num_bytes = bitlen / 8;
222
223 ret = 0;
224
225 reg = readl(&regs->status);
226 writel(reg, &regs->status); /* Clear all SPI events via R/W */
227 debug("spi_xfer entry: STATUS = %08x\n", reg);
228
229 reg = readl(&regs->command);
230 reg |= SPI_CMD_TXEN | SPI_CMD_RXEN;
231 writel(reg, &regs->command);
232 debug("spi_xfer: COMMAND = %08x\n", readl(&regs->command));
233
234 if (flags & SPI_XFER_BEGIN)
Simon Glass1121b1b2014-10-13 23:42:13 -0600235 spi_cs_activate(dev);
Tom Warrenee554f82011-11-05 09:48:11 +0000236
237 /* handle data in 32-bit chunks */
238 while (num_bytes > 0) {
239 int bytes;
240 int is_read = 0;
241 int tm, i;
242
243 tmpdout = 0;
244 bytes = (num_bytes > 4) ? 4 : num_bytes;
245
246 if (dout != NULL) {
247 for (i = 0; i < bytes; ++i)
248 tmpdout = (tmpdout << 8) | dout[i];
249 }
250
251 num_bytes -= bytes;
252 if (dout)
253 dout += bytes;
254
255 clrsetbits_le32(&regs->command, SPI_CMD_BIT_LENGTH_MASK,
256 bytes * 8 - 1);
257 writel(tmpdout, &regs->tx_fifo);
258 setbits_le32(&regs->command, SPI_CMD_GO);
259
260 /*
261 * Wait for SPI transmit FIFO to empty, or to time out.
262 * The RX FIFO status will be read and cleared last
263 */
264 for (tm = 0, is_read = 0; tm < SPI_TIMEOUT; ++tm) {
265 u32 status;
266
267 status = readl(&regs->status);
268
269 /* We can exit when we've had both RX and TX activity */
270 if (is_read && (status & SPI_STAT_TXF_EMPTY))
271 break;
272
273 if ((status & (SPI_STAT_BSY | SPI_STAT_RDY)) !=
274 SPI_STAT_RDY)
275 tm++;
276
277 else if (!(status & SPI_STAT_RXF_EMPTY)) {
278 tmpdin = readl(&regs->rx_fifo);
279 is_read = 1;
280
281 /* swap bytes read in */
282 if (din != NULL) {
283 for (i = bytes - 1; i >= 0; --i) {
284 din[i] = tmpdin & 0xff;
285 tmpdin >>= 8;
286 }
287 din += bytes;
288 }
289 }
290 }
291
292 if (tm >= SPI_TIMEOUT)
293 ret = tm;
294
295 /* clear ACK RDY, etc. bits */
296 writel(readl(&regs->status), &regs->status);
297 }
298
299 if (flags & SPI_XFER_END)
Simon Glass1121b1b2014-10-13 23:42:13 -0600300 spi_cs_deactivate(dev);
Tom Warrenee554f82011-11-05 09:48:11 +0000301
302 debug("spi_xfer: transfer ended. Value=%08x, status = %08x\n",
303 tmpdin, readl(&regs->status));
304
305 if (ret) {
306 printf("spi_xfer: timeout during SPI transfer, tm %d\n", ret);
307 return -1;
308 }
309
310 return 0;
311}
Simon Glass1121b1b2014-10-13 23:42:13 -0600312
313static int tegra20_sflash_set_speed(struct udevice *bus, uint speed)
314{
315 struct tegra_spi_platdata *plat = bus->platdata;
316 struct tegra20_sflash_priv *priv = dev_get_priv(bus);
317
318 if (speed > plat->frequency)
319 speed = plat->frequency;
320 priv->freq = speed;
321 debug("%s: regs=%p, speed=%d\n", __func__, priv->regs, priv->freq);
322
323 return 0;
324}
325
326static int tegra20_sflash_set_mode(struct udevice *bus, uint mode)
327{
328 struct tegra20_sflash_priv *priv = dev_get_priv(bus);
329
330 priv->mode = mode;
331 debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode);
332
333 return 0;
334}
335
336static const struct dm_spi_ops tegra20_sflash_ops = {
337 .claim_bus = tegra20_sflash_claim_bus,
338 .xfer = tegra20_sflash_xfer,
339 .set_speed = tegra20_sflash_set_speed,
340 .set_mode = tegra20_sflash_set_mode,
341 .cs_info = tegra20_sflash_cs_info,
342};
343
344static const struct udevice_id tegra20_sflash_ids[] = {
345 { .compatible = "nvidia,tegra20-sflash" },
346 { }
347};
348
349U_BOOT_DRIVER(tegra20_sflash) = {
350 .name = "tegra20_sflash",
351 .id = UCLASS_SPI,
352 .of_match = tegra20_sflash_ids,
353 .ops = &tegra20_sflash_ops,
354 .ofdata_to_platdata = tegra20_sflash_ofdata_to_platdata,
355 .platdata_auto_alloc_size = sizeof(struct tegra_spi_platdata),
356 .priv_auto_alloc_size = sizeof(struct tegra20_sflash_priv),
Simon Glass1121b1b2014-10-13 23:42:13 -0600357 .probe = tegra20_sflash_probe,
358};