2 * NVIDIA Tegra SPI-SLINK controller
4 * Copyright (c) 2010-2013 NVIDIA Corporation
6 * See file CREDITS for list of people who contributed to this
9 * This software is licensed under the terms of the GNU General Public
10 * License version 2, as published by the Free Software Foundation, and
11 * may be copied, distributed, and modified under those terms.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 #include <asm/arch/clock.h>
28 #include <asm/arch-tegra/clk_rst.h>
31 #include "tegra_spi.h"
33 DECLARE_GLOBAL_DATA_PTR;
36 #define SLINK_CMD_ENB (1 << 31)
37 #define SLINK_CMD_GO (1 << 30)
38 #define SLINK_CMD_M_S (1 << 28)
39 #define SLINK_CMD_CK_SDA (1 << 21)
40 #define SLINK_CMD_CS_POL (1 << 13)
41 #define SLINK_CMD_CS_VAL (1 << 12)
42 #define SLINK_CMD_CS_SOFT (1 << 11)
43 #define SLINK_CMD_BIT_LENGTH (1 << 4)
44 #define SLINK_CMD_BIT_LENGTH_MASK 0x0000001F
46 #define SLINK_CMD2_TXEN (1 << 30)
47 #define SLINK_CMD2_RXEN (1 << 31)
48 #define SLINK_CMD2_SS_EN (1 << 18)
49 #define SLINK_CMD2_SS_EN_SHIFT 18
50 #define SLINK_CMD2_SS_EN_MASK 0x000C0000
51 #define SLINK_CMD2_CS_ACTIVE_BETWEEN (1 << 17)
53 #define SLINK_STAT_BSY (1 << 31)
54 #define SLINK_STAT_RDY (1 << 30)
55 #define SLINK_STAT_ERR (1 << 29)
56 #define SLINK_STAT_RXF_FLUSH (1 << 27)
57 #define SLINK_STAT_TXF_FLUSH (1 << 26)
58 #define SLINK_STAT_RXF_OVF (1 << 25)
59 #define SLINK_STAT_TXF_UNR (1 << 24)
60 #define SLINK_STAT_RXF_EMPTY (1 << 23)
61 #define SLINK_STAT_RXF_FULL (1 << 22)
62 #define SLINK_STAT_TXF_EMPTY (1 << 21)
63 #define SLINK_STAT_TXF_FULL (1 << 20)
64 #define SLINK_STAT_TXF_OVF (1 << 19)
65 #define SLINK_STAT_RXF_UNR (1 << 18)
66 #define SLINK_STAT_CUR_BLKCNT (1 << 15)
68 #define SLINK_STAT2_RXF_FULL_CNT (1 << 16)
69 #define SLINK_STAT2_TXF_FULL_CNT (1 << 0)
71 #define SPI_TIMEOUT 1000
72 #define TEGRA_SPI_MAX_FREQ 52000000
75 u32 command; /* SLINK_COMMAND_0 register */
76 u32 command2; /* SLINK_COMMAND2_0 reg */
77 u32 status; /* SLINK_STATUS_0 register */
78 u32 reserved; /* Reserved offset 0C */
79 u32 mas_data; /* SLINK_MAS_DATA_0 reg */
80 u32 slav_data; /* SLINK_SLAVE_DATA_0 reg */
81 u32 dma_ctl; /* SLINK_DMA_CTL_0 register */
82 u32 status2; /* SLINK_STATUS2_0 reg */
83 u32 rsvd[56]; /* 0x20 to 0xFF reserved */
84 u32 tx_fifo; /* SLINK_TX_FIFO_0 reg off 100h */
85 u32 rsvd2[31]; /* 0x104 to 0x17F reserved */
86 u32 rx_fifo; /* SLINK_RX_FIFO_0 reg off 180h */
89 struct tegra30_spi_priv {
90 struct spi_regs *regs;
95 int last_transaction_us;
98 struct tegra_spi_slave {
99 struct spi_slave slave;
100 struct tegra30_spi_priv *ctrl;
103 static int tegra30_spi_ofdata_to_platdata(struct udevice *bus)
105 struct tegra_spi_platdata *plat = bus->platdata;
106 const void *blob = gd->fdt_blob;
107 int node = bus->of_offset;
109 plat->base = fdtdec_get_addr(blob, node, "reg");
110 plat->periph_id = clock_decode_periph_id(blob, node);
112 if (plat->periph_id == PERIPH_ID_NONE) {
113 debug("%s: could not decode periph id %d\n", __func__,
115 return -FDT_ERR_NOTFOUND;
118 /* Use 500KHz as a suitable default */
119 plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
121 plat->deactivate_delay_us = fdtdec_get_int(blob, node,
122 "spi-deactivate-delay", 0);
123 debug("%s: base=%#08lx, periph_id=%d, max-frequency=%d, deactivate_delay=%d\n",
124 __func__, plat->base, plat->periph_id, plat->frequency,
125 plat->deactivate_delay_us);
130 static int tegra30_spi_probe(struct udevice *bus)
132 struct tegra_spi_platdata *plat = dev_get_platdata(bus);
133 struct tegra30_spi_priv *priv = dev_get_priv(bus);
135 priv->regs = (struct spi_regs *)plat->base;
137 priv->last_transaction_us = timer_get_us();
138 priv->freq = plat->frequency;
139 priv->periph_id = plat->periph_id;
144 static int tegra30_spi_claim_bus(struct udevice *bus)
146 struct tegra30_spi_priv *priv = dev_get_priv(bus);
147 struct spi_regs *regs = priv->regs;
150 /* Change SPI clock to correct frequency, PLLP_OUT0 source */
151 clock_start_periph_pll(priv->periph_id, CLOCK_ID_PERIPH,
154 /* Clear stale status here */
155 reg = SLINK_STAT_RDY | SLINK_STAT_RXF_FLUSH | SLINK_STAT_TXF_FLUSH | \
156 SLINK_STAT_RXF_UNR | SLINK_STAT_TXF_OVF;
157 writel(reg, ®s->status);
158 debug("%s: STATUS = %08x\n", __func__, readl(®s->status));
160 /* Set master mode and sw controlled CS */
161 reg = readl(®s->command);
162 reg |= SLINK_CMD_M_S | SLINK_CMD_CS_SOFT;
163 writel(reg, ®s->command);
164 debug("%s: COMMAND = %08x\n", __func__, readl(®s->command));
169 static void spi_cs_activate(struct udevice *dev)
171 struct udevice *bus = dev->parent;
172 struct tegra_spi_platdata *pdata = dev_get_platdata(bus);
173 struct tegra30_spi_priv *priv = dev_get_priv(bus);
175 /* If it's too soon to do another transaction, wait */
176 if (pdata->deactivate_delay_us &&
177 priv->last_transaction_us) {
178 ulong delay_us; /* The delay completed so far */
179 delay_us = timer_get_us() - priv->last_transaction_us;
180 if (delay_us < pdata->deactivate_delay_us)
181 udelay(pdata->deactivate_delay_us - delay_us);
184 /* CS is negated on Tegra, so drive a 1 to get a 0 */
185 setbits_le32(&priv->regs->command, SLINK_CMD_CS_VAL);
188 static void spi_cs_deactivate(struct udevice *dev)
190 struct udevice *bus = dev->parent;
191 struct tegra_spi_platdata *pdata = dev_get_platdata(bus);
192 struct tegra30_spi_priv *priv = dev_get_priv(bus);
194 /* CS is negated on Tegra, so drive a 0 to get a 1 */
195 clrbits_le32(&priv->regs->command, SLINK_CMD_CS_VAL);
197 /* Remember time of this transaction so we can honour the bus delay */
198 if (pdata->deactivate_delay_us)
199 priv->last_transaction_us = timer_get_us();
202 static int tegra30_spi_xfer(struct udevice *dev, unsigned int bitlen,
203 const void *data_out, void *data_in,
206 struct udevice *bus = dev->parent;
207 struct tegra30_spi_priv *priv = dev_get_priv(bus);
208 struct spi_regs *regs = priv->regs;
209 u32 reg, tmpdout, tmpdin = 0;
210 const u8 *dout = data_out;
215 debug("%s: slave %u:%u dout %p din %p bitlen %u\n",
216 __func__, bus->seq, spi_chip_select(dev), dout, din, bitlen);
219 num_bytes = bitlen / 8;
223 reg = readl(®s->status);
224 writel(reg, ®s->status); /* Clear all SPI events via R/W */
225 debug("%s entry: STATUS = %08x\n", __func__, reg);
227 reg = readl(®s->status2);
228 writel(reg, ®s->status2); /* Clear all STATUS2 events via R/W */
229 debug("%s entry: STATUS2 = %08x\n", __func__, reg);
231 debug("%s entry: COMMAND = %08x\n", __func__, readl(®s->command));
233 clrsetbits_le32(®s->command2, SLINK_CMD2_SS_EN_MASK,
234 SLINK_CMD2_TXEN | SLINK_CMD2_RXEN |
235 (spi_chip_select(dev) << SLINK_CMD2_SS_EN_SHIFT));
236 debug("%s entry: COMMAND2 = %08x\n", __func__, readl(®s->command2));
238 if (flags & SPI_XFER_BEGIN)
239 spi_cs_activate(dev);
241 /* handle data in 32-bit chunks */
242 while (num_bytes > 0) {
248 bytes = (num_bytes > 4) ? 4 : num_bytes;
251 for (i = 0; i < bytes; ++i)
252 tmpdout = (tmpdout << 8) | dout[i];
258 clrsetbits_le32(®s->command, SLINK_CMD_BIT_LENGTH_MASK,
260 writel(tmpdout, ®s->tx_fifo);
261 setbits_le32(®s->command, SLINK_CMD_GO);
264 * Wait for SPI transmit FIFO to empty, or to time out.
265 * The RX FIFO status will be read and cleared last
267 for (tm = 0, is_read = 0; tm < SPI_TIMEOUT; ++tm) {
270 status = readl(®s->status);
272 /* We can exit when we've had both RX and TX activity */
273 if (is_read && (status & SLINK_STAT_TXF_EMPTY))
276 if ((status & (SLINK_STAT_BSY | SLINK_STAT_RDY)) !=
280 else if (!(status & SLINK_STAT_RXF_EMPTY)) {
281 tmpdin = readl(®s->rx_fifo);
284 /* swap bytes read in */
286 for (i = bytes - 1; i >= 0; --i) {
287 din[i] = tmpdin & 0xff;
295 if (tm >= SPI_TIMEOUT)
298 /* clear ACK RDY, etc. bits */
299 writel(readl(®s->status), ®s->status);
302 if (flags & SPI_XFER_END)
303 spi_cs_deactivate(dev);
305 debug("%s: transfer ended. Value=%08x, status = %08x\n",
306 __func__, tmpdin, readl(®s->status));
309 printf("%s: timeout during SPI transfer, tm %d\n",
317 static int tegra30_spi_set_speed(struct udevice *bus, uint speed)
319 struct tegra_spi_platdata *plat = bus->platdata;
320 struct tegra30_spi_priv *priv = dev_get_priv(bus);
322 if (speed > plat->frequency)
323 speed = plat->frequency;
325 debug("%s: regs=%p, speed=%d\n", __func__, priv->regs, priv->freq);
330 static int tegra30_spi_set_mode(struct udevice *bus, uint mode)
332 struct tegra30_spi_priv *priv = dev_get_priv(bus);
335 debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode);
340 static const struct dm_spi_ops tegra30_spi_ops = {
341 .claim_bus = tegra30_spi_claim_bus,
342 .xfer = tegra30_spi_xfer,
343 .set_speed = tegra30_spi_set_speed,
344 .set_mode = tegra30_spi_set_mode,
346 * cs_info is not needed, since we require all chip selects to be
347 * in the device tree explicitly
351 static const struct udevice_id tegra30_spi_ids[] = {
352 { .compatible = "nvidia,tegra20-slink" },
356 U_BOOT_DRIVER(tegra30_spi) = {
357 .name = "tegra20_slink",
359 .of_match = tegra30_spi_ids,
360 .ops = &tegra30_spi_ops,
361 .ofdata_to_platdata = tegra30_spi_ofdata_to_platdata,
362 .platdata_auto_alloc_size = sizeof(struct tegra_spi_platdata),
363 .priv_auto_alloc_size = sizeof(struct tegra30_spi_priv),
364 .probe = tegra30_spi_probe,