blob: d54a5049205b76f3d4e021b61624247ccc10fde6 [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
Simon Glass1121b1b2014-10-13 23:42:13 -06008#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070010#include <time.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060011#include <asm/global_data.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 Glass4dcacfc2020-05-10 11:40:13 -060017#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060018#include <linux/delay.h>
Simon Glass1121b1b2014-10-13 23:42:13 -060019#include "tegra_spi.h"
Allen Martinba4fb9b2013-01-29 13:51:28 +000020
21DECLARE_GLOBAL_DATA_PTR;
22
Allen Martin8db241b2013-03-16 18:58:05 +000023/* COMMAND */
Jagan Teki7f7ccf72015-10-23 01:39:06 +053024#define SLINK_CMD_ENB BIT(31)
25#define SLINK_CMD_GO BIT(30)
26#define SLINK_CMD_M_S BIT(28)
Mirza Krak960dad92015-09-08 10:30:49 +020027#define SLINK_CMD_IDLE_SCLK_DRIVE_LOW (0 << 24)
Jagan Teki7f7ccf72015-10-23 01:39:06 +053028#define SLINK_CMD_IDLE_SCLK_DRIVE_HIGH BIT(24)
Mirza Krak960dad92015-09-08 10:30:49 +020029#define SLINK_CMD_IDLE_SCLK_PULL_LOW (2 << 24)
30#define SLINK_CMD_IDLE_SCLK_PULL_HIGH (3 << 24)
31#define SLINK_CMD_IDLE_SCLK_MASK (3 << 24)
Jagan Teki7f7ccf72015-10-23 01:39:06 +053032#define SLINK_CMD_CK_SDA BIT(21)
33#define SLINK_CMD_CS_POL BIT(13)
34#define SLINK_CMD_CS_VAL BIT(12)
35#define SLINK_CMD_CS_SOFT BIT(11)
36#define SLINK_CMD_BIT_LENGTH BIT(4)
Jagan Teki54badcb2015-10-23 01:03:10 +053037#define SLINK_CMD_BIT_LENGTH_MASK GENMASK(4, 0)
Allen Martin8db241b2013-03-16 18:58:05 +000038/* COMMAND2 */
Jagan Teki7f7ccf72015-10-23 01:39:06 +053039#define SLINK_CMD2_TXEN BIT(30)
40#define SLINK_CMD2_RXEN BIT(31)
41#define SLINK_CMD2_SS_EN BIT(18)
Allen Martin8db241b2013-03-16 18:58:05 +000042#define SLINK_CMD2_SS_EN_SHIFT 18
Jagan Teki54badcb2015-10-23 01:03:10 +053043#define SLINK_CMD2_SS_EN_MASK GENMASK(19, 18)
Jagan Teki7f7ccf72015-10-23 01:39:06 +053044#define SLINK_CMD2_CS_ACTIVE_BETWEEN BIT(17)
Allen Martin8db241b2013-03-16 18:58:05 +000045/* STATUS */
Jagan Teki7f7ccf72015-10-23 01:39:06 +053046#define SLINK_STAT_BSY BIT(31)
47#define SLINK_STAT_RDY BIT(30)
48#define SLINK_STAT_ERR BIT(29)
49#define SLINK_STAT_RXF_FLUSH BIT(27)
50#define SLINK_STAT_TXF_FLUSH BIT(26)
51#define SLINK_STAT_RXF_OVF BIT(25)
52#define SLINK_STAT_TXF_UNR BIT(24)
53#define SLINK_STAT_RXF_EMPTY BIT(23)
54#define SLINK_STAT_RXF_FULL BIT(22)
55#define SLINK_STAT_TXF_EMPTY BIT(21)
56#define SLINK_STAT_TXF_FULL BIT(20)
57#define SLINK_STAT_TXF_OVF BIT(19)
58#define SLINK_STAT_RXF_UNR BIT(18)
59#define SLINK_STAT_CUR_BLKCNT BIT(15)
Allen Martin8db241b2013-03-16 18:58:05 +000060/* STATUS2 */
Jagan Teki7f7ccf72015-10-23 01:39:06 +053061#define SLINK_STAT2_RXF_FULL_CNT BIT(16)
62#define SLINK_STAT2_TXF_FULL_CNT BIT(0)
Allen Martin8db241b2013-03-16 18:58:05 +000063
64#define SPI_TIMEOUT 1000
65#define TEGRA_SPI_MAX_FREQ 52000000
66
67struct spi_regs {
68 u32 command; /* SLINK_COMMAND_0 register */
69 u32 command2; /* SLINK_COMMAND2_0 reg */
70 u32 status; /* SLINK_STATUS_0 register */
71 u32 reserved; /* Reserved offset 0C */
72 u32 mas_data; /* SLINK_MAS_DATA_0 reg */
73 u32 slav_data; /* SLINK_SLAVE_DATA_0 reg */
74 u32 dma_ctl; /* SLINK_DMA_CTL_0 register */
75 u32 status2; /* SLINK_STATUS2_0 reg */
76 u32 rsvd[56]; /* 0x20 to 0xFF reserved */
77 u32 tx_fifo; /* SLINK_TX_FIFO_0 reg off 100h */
78 u32 rsvd2[31]; /* 0x104 to 0x17F reserved */
79 u32 rx_fifo; /* SLINK_RX_FIFO_0 reg off 180h */
80};
81
Simon Glass1121b1b2014-10-13 23:42:13 -060082struct tegra30_spi_priv {
Allen Martin8db241b2013-03-16 18:58:05 +000083 struct spi_regs *regs;
Allen Martinba4fb9b2013-01-29 13:51:28 +000084 unsigned int freq;
85 unsigned int mode;
86 int periph_id;
87 int valid;
Simon Glass1121b1b2014-10-13 23:42:13 -060088 int last_transaction_us;
Allen Martinba4fb9b2013-01-29 13:51:28 +000089};
90
91struct tegra_spi_slave {
92 struct spi_slave slave;
Simon Glass1121b1b2014-10-13 23:42:13 -060093 struct tegra30_spi_priv *ctrl;
Allen Martinba4fb9b2013-01-29 13:51:28 +000094};
95
Simon Glassaad29ae2020-12-03 16:55:21 -070096static int tegra30_spi_of_to_plat(struct udevice *bus)
Allen Martinba4fb9b2013-01-29 13:51:28 +000097{
Simon Glass95588622020-12-22 19:30:28 -070098 struct tegra_spi_plat *plat = dev_get_plat(bus);
Simon Glass1121b1b2014-10-13 23:42:13 -060099 const void *blob = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700100 int node = dev_of_offset(bus);
Allen Martinba4fb9b2013-01-29 13:51:28 +0000101
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900102 plat->base = dev_read_addr(bus);
Simon Glassc3f26502017-07-25 08:30:00 -0600103 plat->periph_id = clock_decode_periph_id(bus);
Allen Martinba4fb9b2013-01-29 13:51:28 +0000104
Simon Glass1121b1b2014-10-13 23:42:13 -0600105 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;
Allen Martinba4fb9b2013-01-29 13:51:28 +0000109 }
110
Simon Glass1121b1b2014-10-13 23:42:13 -0600111 /* Use 500KHz as a suitable default */
112 plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
113 500000);
114 plat->deactivate_delay_us = fdtdec_get_int(blob, node,
115 "spi-deactivate-delay", 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);
Allen Martinba4fb9b2013-01-29 13:51:28 +0000119
Simon Glass1121b1b2014-10-13 23:42:13 -0600120 return 0;
Allen Martinba4fb9b2013-01-29 13:51:28 +0000121}
122
Simon Glass1121b1b2014-10-13 23:42:13 -0600123static int tegra30_spi_probe(struct udevice *bus)
Allen Martinba4fb9b2013-01-29 13:51:28 +0000124{
Simon Glassb75b15b2020-12-03 16:55:23 -0700125 struct tegra_spi_plat *plat = dev_get_plat(bus);
Simon Glass1121b1b2014-10-13 23:42:13 -0600126 struct tegra30_spi_priv *priv = dev_get_priv(bus);
Allen Martinba4fb9b2013-01-29 13:51:28 +0000127
Simon Glass1121b1b2014-10-13 23:42:13 -0600128 priv->regs = (struct spi_regs *)plat->base;
Allen Martinba4fb9b2013-01-29 13:51:28 +0000129
Simon Glass1121b1b2014-10-13 23:42:13 -0600130 priv->last_transaction_us = timer_get_us();
131 priv->freq = plat->frequency;
132 priv->periph_id = plat->periph_id;
Allen Martinba4fb9b2013-01-29 13:51:28 +0000133
Stephen Warrenb68a9942016-08-18 10:53:33 -0600134 /* Change SPI clock to correct frequency, PLLP_OUT0 source */
135 clock_start_periph_pll(priv->periph_id, CLOCK_ID_PERIPH,
136 priv->freq);
137
Simon Glass1121b1b2014-10-13 23:42:13 -0600138 return 0;
Allen Martinba4fb9b2013-01-29 13:51:28 +0000139}
140
Simon Glass5c74fba2015-04-19 09:05:40 -0600141static int tegra30_spi_claim_bus(struct udevice *dev)
Allen Martinba4fb9b2013-01-29 13:51:28 +0000142{
Simon Glass5c74fba2015-04-19 09:05:40 -0600143 struct udevice *bus = dev->parent;
Simon Glass1121b1b2014-10-13 23:42:13 -0600144 struct tegra30_spi_priv *priv = dev_get_priv(bus);
145 struct spi_regs *regs = priv->regs;
Allen Martinba4fb9b2013-01-29 13:51:28 +0000146 u32 reg;
147
148 /* Change SPI clock to correct frequency, PLLP_OUT0 source */
Simon Glass1121b1b2014-10-13 23:42:13 -0600149 clock_start_periph_pll(priv->periph_id, CLOCK_ID_PERIPH,
150 priv->freq);
Allen Martinba4fb9b2013-01-29 13:51:28 +0000151
152 /* Clear stale status here */
153 reg = SLINK_STAT_RDY | SLINK_STAT_RXF_FLUSH | SLINK_STAT_TXF_FLUSH | \
154 SLINK_STAT_RXF_UNR | SLINK_STAT_TXF_OVF;
155 writel(reg, &regs->status);
156 debug("%s: STATUS = %08x\n", __func__, readl(&regs->status));
157
158 /* Set master mode and sw controlled CS */
159 reg = readl(&regs->command);
160 reg |= SLINK_CMD_M_S | SLINK_CMD_CS_SOFT;
161 writel(reg, &regs->command);
162 debug("%s: COMMAND = %08x\n", __func__, readl(&regs->command));
163
164 return 0;
165}
166
Simon Glass1121b1b2014-10-13 23:42:13 -0600167static void spi_cs_activate(struct udevice *dev)
Allen Martinba4fb9b2013-01-29 13:51:28 +0000168{
Simon Glass1121b1b2014-10-13 23:42:13 -0600169 struct udevice *bus = dev->parent;
Simon Glassb75b15b2020-12-03 16:55:23 -0700170 struct tegra_spi_plat *pdata = dev_get_plat(bus);
Simon Glass1121b1b2014-10-13 23:42:13 -0600171 struct tegra30_spi_priv *priv = dev_get_priv(bus);
172
173 /* If it's too soon to do another transaction, wait */
174 if (pdata->deactivate_delay_us &&
175 priv->last_transaction_us) {
176 ulong delay_us; /* The delay completed so far */
177 delay_us = timer_get_us() - priv->last_transaction_us;
178 if (delay_us < pdata->deactivate_delay_us)
179 udelay(pdata->deactivate_delay_us - delay_us);
180 }
Allen Martinba4fb9b2013-01-29 13:51:28 +0000181
182 /* CS is negated on Tegra, so drive a 1 to get a 0 */
Simon Glass1121b1b2014-10-13 23:42:13 -0600183 setbits_le32(&priv->regs->command, SLINK_CMD_CS_VAL);
Allen Martinba4fb9b2013-01-29 13:51:28 +0000184}
185
Simon Glass1121b1b2014-10-13 23:42:13 -0600186static void spi_cs_deactivate(struct udevice *dev)
Allen Martinba4fb9b2013-01-29 13:51:28 +0000187{
Simon Glass1121b1b2014-10-13 23:42:13 -0600188 struct udevice *bus = dev->parent;
Simon Glassb75b15b2020-12-03 16:55:23 -0700189 struct tegra_spi_plat *pdata = dev_get_plat(bus);
Simon Glass1121b1b2014-10-13 23:42:13 -0600190 struct tegra30_spi_priv *priv = dev_get_priv(bus);
Allen Martinba4fb9b2013-01-29 13:51:28 +0000191
192 /* CS is negated on Tegra, so drive a 0 to get a 1 */
Simon Glass1121b1b2014-10-13 23:42:13 -0600193 clrbits_le32(&priv->regs->command, SLINK_CMD_CS_VAL);
194
195 /* Remember time of this transaction so we can honour the bus delay */
196 if (pdata->deactivate_delay_us)
197 priv->last_transaction_us = timer_get_us();
Allen Martinba4fb9b2013-01-29 13:51:28 +0000198}
199
Simon Glass1121b1b2014-10-13 23:42:13 -0600200static int tegra30_spi_xfer(struct udevice *dev, unsigned int bitlen,
201 const void *data_out, void *data_in,
202 unsigned long flags)
Allen Martinba4fb9b2013-01-29 13:51:28 +0000203{
Simon Glass1121b1b2014-10-13 23:42:13 -0600204 struct udevice *bus = dev->parent;
205 struct tegra30_spi_priv *priv = dev_get_priv(bus);
206 struct spi_regs *regs = priv->regs;
Allen Martinba4fb9b2013-01-29 13:51:28 +0000207 u32 reg, tmpdout, tmpdin = 0;
208 const u8 *dout = data_out;
209 u8 *din = data_in;
Svyatoslav Ryhel74916e82023-02-14 19:35:29 +0200210 int num_bytes, overflow;
211 int ret = 0;
Allen Martinba4fb9b2013-01-29 13:51:28 +0000212
213 debug("%s: slave %u:%u dout %p din %p bitlen %u\n",
Simon Glass75e534b2020-12-16 21:20:07 -0700214 __func__, dev_seq(bus), spi_chip_select(dev), dout, din, bitlen);
Allen Martinba4fb9b2013-01-29 13:51:28 +0000215
Svyatoslav Ryhel74916e82023-02-14 19:35:29 +0200216 num_bytes = DIV_ROUND_UP(bitlen, 8);
217 overflow = bitlen % 8;
Allen Martinba4fb9b2013-01-29 13:51:28 +0000218
219 reg = readl(&regs->status);
220 writel(reg, &regs->status); /* Clear all SPI events via R/W */
221 debug("%s entry: STATUS = %08x\n", __func__, reg);
222
223 reg = readl(&regs->status2);
224 writel(reg, &regs->status2); /* Clear all STATUS2 events via R/W */
225 debug("%s entry: STATUS2 = %08x\n", __func__, reg);
226
227 debug("%s entry: COMMAND = %08x\n", __func__, readl(&regs->command));
228
229 clrsetbits_le32(&regs->command2, SLINK_CMD2_SS_EN_MASK,
230 SLINK_CMD2_TXEN | SLINK_CMD2_RXEN |
Simon Glass1121b1b2014-10-13 23:42:13 -0600231 (spi_chip_select(dev) << SLINK_CMD2_SS_EN_SHIFT));
Allen Martinba4fb9b2013-01-29 13:51:28 +0000232 debug("%s entry: COMMAND2 = %08x\n", __func__, readl(&regs->command2));
233
234 if (flags & SPI_XFER_BEGIN)
Simon Glass1121b1b2014-10-13 23:42:13 -0600235 spi_cs_activate(dev);
Allen Martinba4fb9b2013-01-29 13:51:28 +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 dout += bytes;
250 }
251
252 num_bytes -= bytes;
253
Svyatoslav Ryhel74916e82023-02-14 19:35:29 +0200254 if (overflow && !num_bytes)
255 clrsetbits_le32(&regs->command, SLINK_CMD_BIT_LENGTH_MASK,
256 (bytes - 1) * 8 + overflow - 1);
257 else
258 clrsetbits_le32(&regs->command, SLINK_CMD_BIT_LENGTH_MASK,
259 bytes * 8 - 1);
260
Allen Martinba4fb9b2013-01-29 13:51:28 +0000261 writel(tmpdout, &regs->tx_fifo);
262 setbits_le32(&regs->command, SLINK_CMD_GO);
263
264 /*
265 * Wait for SPI transmit FIFO to empty, or to time out.
266 * The RX FIFO status will be read and cleared last
267 */
268 for (tm = 0, is_read = 0; tm < SPI_TIMEOUT; ++tm) {
269 u32 status;
270
271 status = readl(&regs->status);
272
273 /* We can exit when we've had both RX and TX activity */
274 if (is_read && (status & SLINK_STAT_TXF_EMPTY))
275 break;
276
277 if ((status & (SLINK_STAT_BSY | SLINK_STAT_RDY)) !=
278 SLINK_STAT_RDY)
279 tm++;
280
281 else if (!(status & SLINK_STAT_RXF_EMPTY)) {
282 tmpdin = readl(&regs->rx_fifo);
283 is_read = 1;
284
285 /* swap bytes read in */
286 if (din != NULL) {
287 for (i = bytes - 1; i >= 0; --i) {
288 din[i] = tmpdin & 0xff;
289 tmpdin >>= 8;
290 }
291 din += bytes;
292 }
293 }
294 }
295
296 if (tm >= SPI_TIMEOUT)
297 ret = tm;
298
299 /* clear ACK RDY, etc. bits */
300 writel(readl(&regs->status), &regs->status);
301 }
302
303 if (flags & SPI_XFER_END)
Simon Glass1121b1b2014-10-13 23:42:13 -0600304 spi_cs_deactivate(dev);
Allen Martinba4fb9b2013-01-29 13:51:28 +0000305
306 debug("%s: transfer ended. Value=%08x, status = %08x\n",
307 __func__, tmpdin, readl(&regs->status));
308
309 if (ret) {
310 printf("%s: timeout during SPI transfer, tm %d\n",
311 __func__, ret);
312 return -1;
313 }
314
315 return 0;
316}
Simon Glass1121b1b2014-10-13 23:42:13 -0600317
318static int tegra30_spi_set_speed(struct udevice *bus, uint speed)
319{
Simon Glass95588622020-12-22 19:30:28 -0700320 struct tegra_spi_plat *plat = dev_get_plat(bus);
Simon Glass1121b1b2014-10-13 23:42:13 -0600321 struct tegra30_spi_priv *priv = dev_get_priv(bus);
322
323 if (speed > plat->frequency)
324 speed = plat->frequency;
325 priv->freq = speed;
326 debug("%s: regs=%p, speed=%d\n", __func__, priv->regs, priv->freq);
327
328 return 0;
329}
330
331static int tegra30_spi_set_mode(struct udevice *bus, uint mode)
332{
333 struct tegra30_spi_priv *priv = dev_get_priv(bus);
Mirza Krak960dad92015-09-08 10:30:49 +0200334 struct spi_regs *regs = priv->regs;
335 u32 reg;
336
337 reg = readl(&regs->command);
338
339 /* Set CPOL and CPHA */
340 reg &= ~(SLINK_CMD_IDLE_SCLK_MASK | SLINK_CMD_CK_SDA);
341 if (mode & SPI_CPHA)
342 reg |= SLINK_CMD_CK_SDA;
343
344 if (mode & SPI_CPOL)
345 reg |= SLINK_CMD_IDLE_SCLK_DRIVE_HIGH;
346 else
347 reg |= SLINK_CMD_IDLE_SCLK_DRIVE_LOW;
348
349 writel(reg, &regs->command);
Simon Glass1121b1b2014-10-13 23:42:13 -0600350
351 priv->mode = mode;
352 debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode);
353
354 return 0;
355}
356
357static const struct dm_spi_ops tegra30_spi_ops = {
358 .claim_bus = tegra30_spi_claim_bus,
359 .xfer = tegra30_spi_xfer,
360 .set_speed = tegra30_spi_set_speed,
361 .set_mode = tegra30_spi_set_mode,
362 /*
363 * cs_info is not needed, since we require all chip selects to be
364 * in the device tree explicitly
365 */
366};
367
368static const struct udevice_id tegra30_spi_ids[] = {
369 { .compatible = "nvidia,tegra20-slink" },
370 { }
371};
372
373U_BOOT_DRIVER(tegra30_spi) = {
374 .name = "tegra20_slink",
375 .id = UCLASS_SPI,
376 .of_match = tegra30_spi_ids,
377 .ops = &tegra30_spi_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700378 .of_to_plat = tegra30_spi_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700379 .plat_auto = sizeof(struct tegra_spi_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700380 .priv_auto = sizeof(struct tegra30_spi_priv),
Simon Glass1121b1b2014-10-13 23:42:13 -0600381 .probe = tegra30_spi_probe,
382};