blob: dcd09508946c1b3b6e5cf3bacfcf2bca6aab8678 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Allen Martinba4fb9b2013-01-29 13:51:28 +00002/*
3 * NVIDIA Tegra SPI-SLINK controller
4 *
5 * Copyright (c) 2010-2013 NVIDIA Corporation
Allen Martinba4fb9b2013-01-29 13:51:28 +00006 */
7
8#include <common.h>
Simon Glass1121b1b2014-10-13 23:42:13 -06009#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070011#include <time.h>
Allen Martinba4fb9b2013-01-29 13:51:28 +000012#include <asm/io.h>
Allen Martinba4fb9b2013-01-29 13:51:28 +000013#include <asm/arch/clock.h>
14#include <asm/arch-tegra/clk_rst.h>
Allen Martinba4fb9b2013-01-29 13:51:28 +000015#include <spi.h>
16#include <fdtdec.h>
Simon Glassdbd79542020-05-10 11:40:11 -060017#include <linux/delay.h>
Simon Glass1121b1b2014-10-13 23:42:13 -060018#include "tegra_spi.h"
Allen Martinba4fb9b2013-01-29 13:51:28 +000019
20DECLARE_GLOBAL_DATA_PTR;
21
Allen Martin8db241b2013-03-16 18:58:05 +000022/* COMMAND */
Jagan Teki7f7ccf72015-10-23 01:39:06 +053023#define SLINK_CMD_ENB BIT(31)
24#define SLINK_CMD_GO BIT(30)
25#define SLINK_CMD_M_S BIT(28)
Mirza Krak960dad92015-09-08 10:30:49 +020026#define SLINK_CMD_IDLE_SCLK_DRIVE_LOW (0 << 24)
Jagan Teki7f7ccf72015-10-23 01:39:06 +053027#define SLINK_CMD_IDLE_SCLK_DRIVE_HIGH BIT(24)
Mirza Krak960dad92015-09-08 10:30:49 +020028#define SLINK_CMD_IDLE_SCLK_PULL_LOW (2 << 24)
29#define SLINK_CMD_IDLE_SCLK_PULL_HIGH (3 << 24)
30#define SLINK_CMD_IDLE_SCLK_MASK (3 << 24)
Jagan Teki7f7ccf72015-10-23 01:39:06 +053031#define SLINK_CMD_CK_SDA BIT(21)
32#define SLINK_CMD_CS_POL BIT(13)
33#define SLINK_CMD_CS_VAL BIT(12)
34#define SLINK_CMD_CS_SOFT BIT(11)
35#define SLINK_CMD_BIT_LENGTH BIT(4)
Jagan Teki54badcb2015-10-23 01:03:10 +053036#define SLINK_CMD_BIT_LENGTH_MASK GENMASK(4, 0)
Allen Martin8db241b2013-03-16 18:58:05 +000037/* COMMAND2 */
Jagan Teki7f7ccf72015-10-23 01:39:06 +053038#define SLINK_CMD2_TXEN BIT(30)
39#define SLINK_CMD2_RXEN BIT(31)
40#define SLINK_CMD2_SS_EN BIT(18)
Allen Martin8db241b2013-03-16 18:58:05 +000041#define SLINK_CMD2_SS_EN_SHIFT 18
Jagan Teki54badcb2015-10-23 01:03:10 +053042#define SLINK_CMD2_SS_EN_MASK GENMASK(19, 18)
Jagan Teki7f7ccf72015-10-23 01:39:06 +053043#define SLINK_CMD2_CS_ACTIVE_BETWEEN BIT(17)
Allen Martin8db241b2013-03-16 18:58:05 +000044/* STATUS */
Jagan Teki7f7ccf72015-10-23 01:39:06 +053045#define SLINK_STAT_BSY BIT(31)
46#define SLINK_STAT_RDY BIT(30)
47#define SLINK_STAT_ERR BIT(29)
48#define SLINK_STAT_RXF_FLUSH BIT(27)
49#define SLINK_STAT_TXF_FLUSH BIT(26)
50#define SLINK_STAT_RXF_OVF BIT(25)
51#define SLINK_STAT_TXF_UNR BIT(24)
52#define SLINK_STAT_RXF_EMPTY BIT(23)
53#define SLINK_STAT_RXF_FULL BIT(22)
54#define SLINK_STAT_TXF_EMPTY BIT(21)
55#define SLINK_STAT_TXF_FULL BIT(20)
56#define SLINK_STAT_TXF_OVF BIT(19)
57#define SLINK_STAT_RXF_UNR BIT(18)
58#define SLINK_STAT_CUR_BLKCNT BIT(15)
Allen Martin8db241b2013-03-16 18:58:05 +000059/* STATUS2 */
Jagan Teki7f7ccf72015-10-23 01:39:06 +053060#define SLINK_STAT2_RXF_FULL_CNT BIT(16)
61#define SLINK_STAT2_TXF_FULL_CNT BIT(0)
Allen Martin8db241b2013-03-16 18:58:05 +000062
63#define SPI_TIMEOUT 1000
64#define TEGRA_SPI_MAX_FREQ 52000000
65
66struct spi_regs {
67 u32 command; /* SLINK_COMMAND_0 register */
68 u32 command2; /* SLINK_COMMAND2_0 reg */
69 u32 status; /* SLINK_STATUS_0 register */
70 u32 reserved; /* Reserved offset 0C */
71 u32 mas_data; /* SLINK_MAS_DATA_0 reg */
72 u32 slav_data; /* SLINK_SLAVE_DATA_0 reg */
73 u32 dma_ctl; /* SLINK_DMA_CTL_0 register */
74 u32 status2; /* SLINK_STATUS2_0 reg */
75 u32 rsvd[56]; /* 0x20 to 0xFF reserved */
76 u32 tx_fifo; /* SLINK_TX_FIFO_0 reg off 100h */
77 u32 rsvd2[31]; /* 0x104 to 0x17F reserved */
78 u32 rx_fifo; /* SLINK_RX_FIFO_0 reg off 180h */
79};
80
Simon Glass1121b1b2014-10-13 23:42:13 -060081struct tegra30_spi_priv {
Allen Martin8db241b2013-03-16 18:58:05 +000082 struct spi_regs *regs;
Allen Martinba4fb9b2013-01-29 13:51:28 +000083 unsigned int freq;
84 unsigned int mode;
85 int periph_id;
86 int valid;
Simon Glass1121b1b2014-10-13 23:42:13 -060087 int last_transaction_us;
Allen Martinba4fb9b2013-01-29 13:51:28 +000088};
89
90struct tegra_spi_slave {
91 struct spi_slave slave;
Simon Glass1121b1b2014-10-13 23:42:13 -060092 struct tegra30_spi_priv *ctrl;
Allen Martinba4fb9b2013-01-29 13:51:28 +000093};
94
Simon Glass1121b1b2014-10-13 23:42:13 -060095static int tegra30_spi_ofdata_to_platdata(struct udevice *bus)
Allen Martinba4fb9b2013-01-29 13:51:28 +000096{
Simon Glass1121b1b2014-10-13 23:42:13 -060097 struct tegra_spi_platdata *plat = bus->platdata;
98 const void *blob = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -070099 int node = dev_of_offset(bus);
Allen Martinba4fb9b2013-01-29 13:51:28 +0000100
Simon Glassba1dea42017-05-17 17:18:05 -0600101 plat->base = devfdt_get_addr(bus);
Simon Glassc3f26502017-07-25 08:30:00 -0600102 plat->periph_id = clock_decode_periph_id(bus);
Allen Martinba4fb9b2013-01-29 13:51:28 +0000103
Simon Glass1121b1b2014-10-13 23:42:13 -0600104 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;
Allen Martinba4fb9b2013-01-29 13:51:28 +0000108 }
109
Simon Glass1121b1b2014-10-13 23:42:13 -0600110 /* Use 500KHz as a suitable default */
111 plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
112 500000);
113 plat->deactivate_delay_us = fdtdec_get_int(blob, node,
114 "spi-deactivate-delay", 0);
115 debug("%s: base=%#08lx, periph_id=%d, max-frequency=%d, deactivate_delay=%d\n",
116 __func__, plat->base, plat->periph_id, plat->frequency,
117 plat->deactivate_delay_us);
Allen Martinba4fb9b2013-01-29 13:51:28 +0000118
Simon Glass1121b1b2014-10-13 23:42:13 -0600119 return 0;
Allen Martinba4fb9b2013-01-29 13:51:28 +0000120}
121
Simon Glass1121b1b2014-10-13 23:42:13 -0600122static int tegra30_spi_probe(struct udevice *bus)
Allen Martinba4fb9b2013-01-29 13:51:28 +0000123{
Simon Glass1121b1b2014-10-13 23:42:13 -0600124 struct tegra_spi_platdata *plat = dev_get_platdata(bus);
125 struct tegra30_spi_priv *priv = dev_get_priv(bus);
Allen Martinba4fb9b2013-01-29 13:51:28 +0000126
Simon Glass1121b1b2014-10-13 23:42:13 -0600127 priv->regs = (struct spi_regs *)plat->base;
Allen Martinba4fb9b2013-01-29 13:51:28 +0000128
Simon Glass1121b1b2014-10-13 23:42:13 -0600129 priv->last_transaction_us = timer_get_us();
130 priv->freq = plat->frequency;
131 priv->periph_id = plat->periph_id;
Allen Martinba4fb9b2013-01-29 13:51:28 +0000132
Stephen Warrenb68a9942016-08-18 10:53:33 -0600133 /* Change SPI clock to correct frequency, PLLP_OUT0 source */
134 clock_start_periph_pll(priv->periph_id, CLOCK_ID_PERIPH,
135 priv->freq);
136
Simon Glass1121b1b2014-10-13 23:42:13 -0600137 return 0;
Allen Martinba4fb9b2013-01-29 13:51:28 +0000138}
139
Simon Glass5c74fba2015-04-19 09:05:40 -0600140static int tegra30_spi_claim_bus(struct udevice *dev)
Allen Martinba4fb9b2013-01-29 13:51:28 +0000141{
Simon Glass5c74fba2015-04-19 09:05:40 -0600142 struct udevice *bus = dev->parent;
Simon Glass1121b1b2014-10-13 23:42:13 -0600143 struct tegra30_spi_priv *priv = dev_get_priv(bus);
144 struct spi_regs *regs = priv->regs;
Allen Martinba4fb9b2013-01-29 13:51:28 +0000145 u32 reg;
146
147 /* Change SPI clock to correct frequency, PLLP_OUT0 source */
Simon Glass1121b1b2014-10-13 23:42:13 -0600148 clock_start_periph_pll(priv->periph_id, CLOCK_ID_PERIPH,
149 priv->freq);
Allen Martinba4fb9b2013-01-29 13:51:28 +0000150
151 /* Clear stale status here */
152 reg = SLINK_STAT_RDY | SLINK_STAT_RXF_FLUSH | SLINK_STAT_TXF_FLUSH | \
153 SLINK_STAT_RXF_UNR | SLINK_STAT_TXF_OVF;
154 writel(reg, &regs->status);
155 debug("%s: STATUS = %08x\n", __func__, readl(&regs->status));
156
157 /* Set master mode and sw controlled CS */
158 reg = readl(&regs->command);
159 reg |= SLINK_CMD_M_S | SLINK_CMD_CS_SOFT;
160 writel(reg, &regs->command);
161 debug("%s: COMMAND = %08x\n", __func__, readl(&regs->command));
162
163 return 0;
164}
165
Simon Glass1121b1b2014-10-13 23:42:13 -0600166static void spi_cs_activate(struct udevice *dev)
Allen Martinba4fb9b2013-01-29 13:51:28 +0000167{
Simon Glass1121b1b2014-10-13 23:42:13 -0600168 struct udevice *bus = dev->parent;
169 struct tegra_spi_platdata *pdata = dev_get_platdata(bus);
170 struct tegra30_spi_priv *priv = dev_get_priv(bus);
171
172 /* If it's too soon to do another transaction, wait */
173 if (pdata->deactivate_delay_us &&
174 priv->last_transaction_us) {
175 ulong delay_us; /* The delay completed so far */
176 delay_us = timer_get_us() - priv->last_transaction_us;
177 if (delay_us < pdata->deactivate_delay_us)
178 udelay(pdata->deactivate_delay_us - delay_us);
179 }
Allen Martinba4fb9b2013-01-29 13:51:28 +0000180
181 /* CS is negated on Tegra, so drive a 1 to get a 0 */
Simon Glass1121b1b2014-10-13 23:42:13 -0600182 setbits_le32(&priv->regs->command, SLINK_CMD_CS_VAL);
Allen Martinba4fb9b2013-01-29 13:51:28 +0000183}
184
Simon Glass1121b1b2014-10-13 23:42:13 -0600185static void spi_cs_deactivate(struct udevice *dev)
Allen Martinba4fb9b2013-01-29 13:51:28 +0000186{
Simon Glass1121b1b2014-10-13 23:42:13 -0600187 struct udevice *bus = dev->parent;
188 struct tegra_spi_platdata *pdata = dev_get_platdata(bus);
189 struct tegra30_spi_priv *priv = dev_get_priv(bus);
Allen Martinba4fb9b2013-01-29 13:51:28 +0000190
191 /* CS is negated on Tegra, so drive a 0 to get a 1 */
Simon Glass1121b1b2014-10-13 23:42:13 -0600192 clrbits_le32(&priv->regs->command, SLINK_CMD_CS_VAL);
193
194 /* Remember time of this transaction so we can honour the bus delay */
195 if (pdata->deactivate_delay_us)
196 priv->last_transaction_us = timer_get_us();
Allen Martinba4fb9b2013-01-29 13:51:28 +0000197}
198
Simon Glass1121b1b2014-10-13 23:42:13 -0600199static int tegra30_spi_xfer(struct udevice *dev, unsigned int bitlen,
200 const void *data_out, void *data_in,
201 unsigned long flags)
Allen Martinba4fb9b2013-01-29 13:51:28 +0000202{
Simon Glass1121b1b2014-10-13 23:42:13 -0600203 struct udevice *bus = dev->parent;
204 struct tegra30_spi_priv *priv = dev_get_priv(bus);
205 struct spi_regs *regs = priv->regs;
Allen Martinba4fb9b2013-01-29 13:51:28 +0000206 u32 reg, tmpdout, tmpdin = 0;
207 const u8 *dout = data_out;
208 u8 *din = data_in;
209 int num_bytes;
210 int ret;
211
212 debug("%s: slave %u:%u dout %p din %p bitlen %u\n",
Simon Glass1121b1b2014-10-13 23:42:13 -0600213 __func__, bus->seq, spi_chip_select(dev), dout, din, bitlen);
Allen Martinba4fb9b2013-01-29 13:51:28 +0000214 if (bitlen % 8)
215 return -1;
216 num_bytes = bitlen / 8;
217
218 ret = 0;
219
220 reg = readl(&regs->status);
221 writel(reg, &regs->status); /* Clear all SPI events via R/W */
222 debug("%s entry: STATUS = %08x\n", __func__, reg);
223
224 reg = readl(&regs->status2);
225 writel(reg, &regs->status2); /* Clear all STATUS2 events via R/W */
226 debug("%s entry: STATUS2 = %08x\n", __func__, reg);
227
228 debug("%s entry: COMMAND = %08x\n", __func__, readl(&regs->command));
229
230 clrsetbits_le32(&regs->command2, SLINK_CMD2_SS_EN_MASK,
231 SLINK_CMD2_TXEN | SLINK_CMD2_RXEN |
Simon Glass1121b1b2014-10-13 23:42:13 -0600232 (spi_chip_select(dev) << SLINK_CMD2_SS_EN_SHIFT));
Allen Martinba4fb9b2013-01-29 13:51:28 +0000233 debug("%s entry: COMMAND2 = %08x\n", __func__, readl(&regs->command2));
234
235 if (flags & SPI_XFER_BEGIN)
Simon Glass1121b1b2014-10-13 23:42:13 -0600236 spi_cs_activate(dev);
Allen Martinba4fb9b2013-01-29 13:51:28 +0000237
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 dout += bytes;
251 }
252
253 num_bytes -= bytes;
254
255 clrsetbits_le32(&regs->command, SLINK_CMD_BIT_LENGTH_MASK,
256 bytes * 8 - 1);
257 writel(tmpdout, &regs->tx_fifo);
258 setbits_le32(&regs->command, SLINK_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 & SLINK_STAT_TXF_EMPTY))
271 break;
272
273 if ((status & (SLINK_STAT_BSY | SLINK_STAT_RDY)) !=
274 SLINK_STAT_RDY)
275 tm++;
276
277 else if (!(status & SLINK_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);
Allen Martinba4fb9b2013-01-29 13:51:28 +0000301
302 debug("%s: transfer ended. Value=%08x, status = %08x\n",
303 __func__, tmpdin, readl(&regs->status));
304
305 if (ret) {
306 printf("%s: timeout during SPI transfer, tm %d\n",
307 __func__, ret);
308 return -1;
309 }
310
311 return 0;
312}
Simon Glass1121b1b2014-10-13 23:42:13 -0600313
314static int tegra30_spi_set_speed(struct udevice *bus, uint speed)
315{
316 struct tegra_spi_platdata *plat = bus->platdata;
317 struct tegra30_spi_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
327static int tegra30_spi_set_mode(struct udevice *bus, uint mode)
328{
329 struct tegra30_spi_priv *priv = dev_get_priv(bus);
Mirza Krak960dad92015-09-08 10:30:49 +0200330 struct spi_regs *regs = priv->regs;
331 u32 reg;
332
333 reg = readl(&regs->command);
334
335 /* Set CPOL and CPHA */
336 reg &= ~(SLINK_CMD_IDLE_SCLK_MASK | SLINK_CMD_CK_SDA);
337 if (mode & SPI_CPHA)
338 reg |= SLINK_CMD_CK_SDA;
339
340 if (mode & SPI_CPOL)
341 reg |= SLINK_CMD_IDLE_SCLK_DRIVE_HIGH;
342 else
343 reg |= SLINK_CMD_IDLE_SCLK_DRIVE_LOW;
344
345 writel(reg, &regs->command);
Simon Glass1121b1b2014-10-13 23:42:13 -0600346
347 priv->mode = mode;
348 debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode);
349
350 return 0;
351}
352
353static const struct dm_spi_ops tegra30_spi_ops = {
354 .claim_bus = tegra30_spi_claim_bus,
355 .xfer = tegra30_spi_xfer,
356 .set_speed = tegra30_spi_set_speed,
357 .set_mode = tegra30_spi_set_mode,
358 /*
359 * cs_info is not needed, since we require all chip selects to be
360 * in the device tree explicitly
361 */
362};
363
364static const struct udevice_id tegra30_spi_ids[] = {
365 { .compatible = "nvidia,tegra20-slink" },
366 { }
367};
368
369U_BOOT_DRIVER(tegra30_spi) = {
370 .name = "tegra20_slink",
371 .id = UCLASS_SPI,
372 .of_match = tegra30_spi_ids,
373 .ops = &tegra30_spi_ops,
374 .ofdata_to_platdata = tegra30_spi_ofdata_to_platdata,
375 .platdata_auto_alloc_size = sizeof(struct tegra_spi_platdata),
376 .priv_auto_alloc_size = sizeof(struct tegra30_spi_priv),
Simon Glass1121b1b2014-10-13 23:42:13 -0600377 .probe = tegra30_spi_probe,
378};