1 // SPDX-License-Identifier: GPL-2.0
3 * NVIDIA Tegra SPI-SLINK controller
5 * Copyright (c) 2010-2013 NVIDIA Corporation
13 #include <asm/arch/clock.h>
14 #include <asm/arch-tegra/clk_rst.h>
17 #include "tegra_spi.h"
19 DECLARE_GLOBAL_DATA_PTR;
22 #define SLINK_CMD_ENB BIT(31)
23 #define SLINK_CMD_GO BIT(30)
24 #define SLINK_CMD_M_S BIT(28)
25 #define SLINK_CMD_IDLE_SCLK_DRIVE_LOW (0 << 24)
26 #define SLINK_CMD_IDLE_SCLK_DRIVE_HIGH BIT(24)
27 #define SLINK_CMD_IDLE_SCLK_PULL_LOW (2 << 24)
28 #define SLINK_CMD_IDLE_SCLK_PULL_HIGH (3 << 24)
29 #define SLINK_CMD_IDLE_SCLK_MASK (3 << 24)
30 #define SLINK_CMD_CK_SDA BIT(21)
31 #define SLINK_CMD_CS_POL BIT(13)
32 #define SLINK_CMD_CS_VAL BIT(12)
33 #define SLINK_CMD_CS_SOFT BIT(11)
34 #define SLINK_CMD_BIT_LENGTH BIT(4)
35 #define SLINK_CMD_BIT_LENGTH_MASK GENMASK(4, 0)
37 #define SLINK_CMD2_TXEN BIT(30)
38 #define SLINK_CMD2_RXEN BIT(31)
39 #define SLINK_CMD2_SS_EN BIT(18)
40 #define SLINK_CMD2_SS_EN_SHIFT 18
41 #define SLINK_CMD2_SS_EN_MASK GENMASK(19, 18)
42 #define SLINK_CMD2_CS_ACTIVE_BETWEEN BIT(17)
44 #define SLINK_STAT_BSY BIT(31)
45 #define SLINK_STAT_RDY BIT(30)
46 #define SLINK_STAT_ERR BIT(29)
47 #define SLINK_STAT_RXF_FLUSH BIT(27)
48 #define SLINK_STAT_TXF_FLUSH BIT(26)
49 #define SLINK_STAT_RXF_OVF BIT(25)
50 #define SLINK_STAT_TXF_UNR BIT(24)
51 #define SLINK_STAT_RXF_EMPTY BIT(23)
52 #define SLINK_STAT_RXF_FULL BIT(22)
53 #define SLINK_STAT_TXF_EMPTY BIT(21)
54 #define SLINK_STAT_TXF_FULL BIT(20)
55 #define SLINK_STAT_TXF_OVF BIT(19)
56 #define SLINK_STAT_RXF_UNR BIT(18)
57 #define SLINK_STAT_CUR_BLKCNT BIT(15)
59 #define SLINK_STAT2_RXF_FULL_CNT BIT(16)
60 #define SLINK_STAT2_TXF_FULL_CNT BIT(0)
62 #define SPI_TIMEOUT 1000
63 #define TEGRA_SPI_MAX_FREQ 52000000
66 u32 command; /* SLINK_COMMAND_0 register */
67 u32 command2; /* SLINK_COMMAND2_0 reg */
68 u32 status; /* SLINK_STATUS_0 register */
69 u32 reserved; /* Reserved offset 0C */
70 u32 mas_data; /* SLINK_MAS_DATA_0 reg */
71 u32 slav_data; /* SLINK_SLAVE_DATA_0 reg */
72 u32 dma_ctl; /* SLINK_DMA_CTL_0 register */
73 u32 status2; /* SLINK_STATUS2_0 reg */
74 u32 rsvd[56]; /* 0x20 to 0xFF reserved */
75 u32 tx_fifo; /* SLINK_TX_FIFO_0 reg off 100h */
76 u32 rsvd2[31]; /* 0x104 to 0x17F reserved */
77 u32 rx_fifo; /* SLINK_RX_FIFO_0 reg off 180h */
80 struct tegra30_spi_priv {
81 struct spi_regs *regs;
86 int last_transaction_us;
89 struct tegra_spi_slave {
90 struct spi_slave slave;
91 struct tegra30_spi_priv *ctrl;
94 static int tegra30_spi_ofdata_to_platdata(struct udevice *bus)
96 struct tegra_spi_platdata *plat = bus->platdata;
97 const void *blob = gd->fdt_blob;
98 int node = dev_of_offset(bus);
100 plat->base = devfdt_get_addr(bus);
101 plat->periph_id = clock_decode_periph_id(bus);
103 if (plat->periph_id == PERIPH_ID_NONE) {
104 debug("%s: could not decode periph id %d\n", __func__,
106 return -FDT_ERR_NOTFOUND;
109 /* Use 500KHz as a suitable default */
110 plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
112 plat->deactivate_delay_us = fdtdec_get_int(blob, node,
113 "spi-deactivate-delay", 0);
114 debug("%s: base=%#08lx, periph_id=%d, max-frequency=%d, deactivate_delay=%d\n",
115 __func__, plat->base, plat->periph_id, plat->frequency,
116 plat->deactivate_delay_us);
121 static int tegra30_spi_probe(struct udevice *bus)
123 struct tegra_spi_platdata *plat = dev_get_platdata(bus);
124 struct tegra30_spi_priv *priv = dev_get_priv(bus);
126 priv->regs = (struct spi_regs *)plat->base;
128 priv->last_transaction_us = timer_get_us();
129 priv->freq = plat->frequency;
130 priv->periph_id = plat->periph_id;
132 /* Change SPI clock to correct frequency, PLLP_OUT0 source */
133 clock_start_periph_pll(priv->periph_id, CLOCK_ID_PERIPH,
139 static int tegra30_spi_claim_bus(struct udevice *dev)
141 struct udevice *bus = dev->parent;
142 struct tegra30_spi_priv *priv = dev_get_priv(bus);
143 struct spi_regs *regs = priv->regs;
146 /* Change SPI clock to correct frequency, PLLP_OUT0 source */
147 clock_start_periph_pll(priv->periph_id, CLOCK_ID_PERIPH,
150 /* Clear stale status here */
151 reg = SLINK_STAT_RDY | SLINK_STAT_RXF_FLUSH | SLINK_STAT_TXF_FLUSH | \
152 SLINK_STAT_RXF_UNR | SLINK_STAT_TXF_OVF;
153 writel(reg, ®s->status);
154 debug("%s: STATUS = %08x\n", __func__, readl(®s->status));
156 /* Set master mode and sw controlled CS */
157 reg = readl(®s->command);
158 reg |= SLINK_CMD_M_S | SLINK_CMD_CS_SOFT;
159 writel(reg, ®s->command);
160 debug("%s: COMMAND = %08x\n", __func__, readl(®s->command));
165 static void spi_cs_activate(struct udevice *dev)
167 struct udevice *bus = dev->parent;
168 struct tegra_spi_platdata *pdata = dev_get_platdata(bus);
169 struct tegra30_spi_priv *priv = dev_get_priv(bus);
171 /* If it's too soon to do another transaction, wait */
172 if (pdata->deactivate_delay_us &&
173 priv->last_transaction_us) {
174 ulong delay_us; /* The delay completed so far */
175 delay_us = timer_get_us() - priv->last_transaction_us;
176 if (delay_us < pdata->deactivate_delay_us)
177 udelay(pdata->deactivate_delay_us - delay_us);
180 /* CS is negated on Tegra, so drive a 1 to get a 0 */
181 setbits_le32(&priv->regs->command, SLINK_CMD_CS_VAL);
184 static void spi_cs_deactivate(struct udevice *dev)
186 struct udevice *bus = dev->parent;
187 struct tegra_spi_platdata *pdata = dev_get_platdata(bus);
188 struct tegra30_spi_priv *priv = dev_get_priv(bus);
190 /* CS is negated on Tegra, so drive a 0 to get a 1 */
191 clrbits_le32(&priv->regs->command, SLINK_CMD_CS_VAL);
193 /* Remember time of this transaction so we can honour the bus delay */
194 if (pdata->deactivate_delay_us)
195 priv->last_transaction_us = timer_get_us();
198 static int tegra30_spi_xfer(struct udevice *dev, unsigned int bitlen,
199 const void *data_out, void *data_in,
202 struct udevice *bus = dev->parent;
203 struct tegra30_spi_priv *priv = dev_get_priv(bus);
204 struct spi_regs *regs = priv->regs;
205 u32 reg, tmpdout, tmpdin = 0;
206 const u8 *dout = data_out;
211 debug("%s: slave %u:%u dout %p din %p bitlen %u\n",
212 __func__, bus->seq, spi_chip_select(dev), dout, din, bitlen);
215 num_bytes = bitlen / 8;
219 reg = readl(®s->status);
220 writel(reg, ®s->status); /* Clear all SPI events via R/W */
221 debug("%s entry: STATUS = %08x\n", __func__, reg);
223 reg = readl(®s->status2);
224 writel(reg, ®s->status2); /* Clear all STATUS2 events via R/W */
225 debug("%s entry: STATUS2 = %08x\n", __func__, reg);
227 debug("%s entry: COMMAND = %08x\n", __func__, readl(®s->command));
229 clrsetbits_le32(®s->command2, SLINK_CMD2_SS_EN_MASK,
230 SLINK_CMD2_TXEN | SLINK_CMD2_RXEN |
231 (spi_chip_select(dev) << SLINK_CMD2_SS_EN_SHIFT));
232 debug("%s entry: COMMAND2 = %08x\n", __func__, readl(®s->command2));
234 if (flags & SPI_XFER_BEGIN)
235 spi_cs_activate(dev);
237 /* handle data in 32-bit chunks */
238 while (num_bytes > 0) {
244 bytes = (num_bytes > 4) ? 4 : num_bytes;
247 for (i = 0; i < bytes; ++i)
248 tmpdout = (tmpdout << 8) | dout[i];
254 clrsetbits_le32(®s->command, SLINK_CMD_BIT_LENGTH_MASK,
256 writel(tmpdout, ®s->tx_fifo);
257 setbits_le32(®s->command, SLINK_CMD_GO);
260 * Wait for SPI transmit FIFO to empty, or to time out.
261 * The RX FIFO status will be read and cleared last
263 for (tm = 0, is_read = 0; tm < SPI_TIMEOUT; ++tm) {
266 status = readl(®s->status);
268 /* We can exit when we've had both RX and TX activity */
269 if (is_read && (status & SLINK_STAT_TXF_EMPTY))
272 if ((status & (SLINK_STAT_BSY | SLINK_STAT_RDY)) !=
276 else if (!(status & SLINK_STAT_RXF_EMPTY)) {
277 tmpdin = readl(®s->rx_fifo);
280 /* swap bytes read in */
282 for (i = bytes - 1; i >= 0; --i) {
283 din[i] = tmpdin & 0xff;
291 if (tm >= SPI_TIMEOUT)
294 /* clear ACK RDY, etc. bits */
295 writel(readl(®s->status), ®s->status);
298 if (flags & SPI_XFER_END)
299 spi_cs_deactivate(dev);
301 debug("%s: transfer ended. Value=%08x, status = %08x\n",
302 __func__, tmpdin, readl(®s->status));
305 printf("%s: timeout during SPI transfer, tm %d\n",
313 static int tegra30_spi_set_speed(struct udevice *bus, uint speed)
315 struct tegra_spi_platdata *plat = bus->platdata;
316 struct tegra30_spi_priv *priv = dev_get_priv(bus);
318 if (speed > plat->frequency)
319 speed = plat->frequency;
321 debug("%s: regs=%p, speed=%d\n", __func__, priv->regs, priv->freq);
326 static int tegra30_spi_set_mode(struct udevice *bus, uint mode)
328 struct tegra30_spi_priv *priv = dev_get_priv(bus);
329 struct spi_regs *regs = priv->regs;
332 reg = readl(®s->command);
334 /* Set CPOL and CPHA */
335 reg &= ~(SLINK_CMD_IDLE_SCLK_MASK | SLINK_CMD_CK_SDA);
337 reg |= SLINK_CMD_CK_SDA;
340 reg |= SLINK_CMD_IDLE_SCLK_DRIVE_HIGH;
342 reg |= SLINK_CMD_IDLE_SCLK_DRIVE_LOW;
344 writel(reg, ®s->command);
347 debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode);
352 static const struct dm_spi_ops tegra30_spi_ops = {
353 .claim_bus = tegra30_spi_claim_bus,
354 .xfer = tegra30_spi_xfer,
355 .set_speed = tegra30_spi_set_speed,
356 .set_mode = tegra30_spi_set_mode,
358 * cs_info is not needed, since we require all chip selects to be
359 * in the device tree explicitly
363 static const struct udevice_id tegra30_spi_ids[] = {
364 { .compatible = "nvidia,tegra20-slink" },
368 U_BOOT_DRIVER(tegra30_spi) = {
369 .name = "tegra20_slink",
371 .of_match = tegra30_spi_ids,
372 .ops = &tegra30_spi_ops,
373 .ofdata_to_platdata = tegra30_spi_ofdata_to_platdata,
374 .platdata_auto_alloc_size = sizeof(struct tegra_spi_platdata),
375 .priv_auto_alloc_size = sizeof(struct tegra30_spi_priv),
376 .probe = tegra30_spi_probe,