blob: 0d69376e4e09d080b48506d954bd37fe606f9a61 [file] [log] [blame]
Allen Martin77c42e82013-03-16 18:58:13 +00001/*
2 * NVIDIA Tegra SPI controller (T114 and later)
3 *
4 * Copyright (c) 2010-2013 NVIDIA Corporation
5 *
6 * See file CREDITS for list of people who contributed to this
7 * project.
8 *
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.
12 *
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.
17 *
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,
21 * MA 02111-1307 USA
22 */
23
24#include <common.h>
Simon Glassfda6fac2014-10-13 23:42:13 -060025#include <dm.h>
Allen Martin77c42e82013-03-16 18:58:13 +000026#include <asm/io.h>
Allen Martin77c42e82013-03-16 18:58:13 +000027#include <asm/arch/clock.h>
28#include <asm/arch-tegra/clk_rst.h>
Allen Martin77c42e82013-03-16 18:58:13 +000029#include <spi.h>
30#include <fdtdec.h>
Simon Glassfda6fac2014-10-13 23:42:13 -060031#include "tegra_spi.h"
Allen Martin77c42e82013-03-16 18:58:13 +000032
33DECLARE_GLOBAL_DATA_PTR;
34
35/* COMMAND1 */
36#define SPI_CMD1_GO (1 << 31)
37#define SPI_CMD1_M_S (1 << 30)
38#define SPI_CMD1_MODE_MASK 0x3
39#define SPI_CMD1_MODE_SHIFT 28
40#define SPI_CMD1_CS_SEL_MASK 0x3
41#define SPI_CMD1_CS_SEL_SHIFT 26
42#define SPI_CMD1_CS_POL_INACTIVE3 (1 << 25)
43#define SPI_CMD1_CS_POL_INACTIVE2 (1 << 24)
44#define SPI_CMD1_CS_POL_INACTIVE1 (1 << 23)
45#define SPI_CMD1_CS_POL_INACTIVE0 (1 << 22)
46#define SPI_CMD1_CS_SW_HW (1 << 21)
47#define SPI_CMD1_CS_SW_VAL (1 << 20)
48#define SPI_CMD1_IDLE_SDA_MASK 0x3
49#define SPI_CMD1_IDLE_SDA_SHIFT 18
50#define SPI_CMD1_BIDIR (1 << 17)
51#define SPI_CMD1_LSBI_FE (1 << 16)
52#define SPI_CMD1_LSBY_FE (1 << 15)
53#define SPI_CMD1_BOTH_EN_BIT (1 << 14)
54#define SPI_CMD1_BOTH_EN_BYTE (1 << 13)
55#define SPI_CMD1_RX_EN (1 << 12)
56#define SPI_CMD1_TX_EN (1 << 11)
57#define SPI_CMD1_PACKED (1 << 5)
58#define SPI_CMD1_BIT_LEN_MASK 0x1F
59#define SPI_CMD1_BIT_LEN_SHIFT 0
60
61/* COMMAND2 */
62#define SPI_CMD2_TX_CLK_TAP_DELAY (1 << 6)
63#define SPI_CMD2_TX_CLK_TAP_DELAY_MASK (0x3F << 6)
64#define SPI_CMD2_RX_CLK_TAP_DELAY (1 << 0)
65#define SPI_CMD2_RX_CLK_TAP_DELAY_MASK (0x3F << 0)
66
67/* TRANSFER STATUS */
68#define SPI_XFER_STS_RDY (1 << 30)
69
70/* FIFO STATUS */
71#define SPI_FIFO_STS_CS_INACTIVE (1 << 31)
72#define SPI_FIFO_STS_FRAME_END (1 << 30)
73#define SPI_FIFO_STS_RX_FIFO_FLUSH (1 << 15)
74#define SPI_FIFO_STS_TX_FIFO_FLUSH (1 << 14)
75#define SPI_FIFO_STS_ERR (1 << 8)
76#define SPI_FIFO_STS_TX_FIFO_OVF (1 << 7)
77#define SPI_FIFO_STS_TX_FIFO_UNR (1 << 6)
78#define SPI_FIFO_STS_RX_FIFO_OVF (1 << 5)
79#define SPI_FIFO_STS_RX_FIFO_UNR (1 << 4)
80#define SPI_FIFO_STS_TX_FIFO_FULL (1 << 3)
81#define SPI_FIFO_STS_TX_FIFO_EMPTY (1 << 2)
82#define SPI_FIFO_STS_RX_FIFO_FULL (1 << 1)
83#define SPI_FIFO_STS_RX_FIFO_EMPTY (1 << 0)
84
85#define SPI_TIMEOUT 1000
86#define TEGRA_SPI_MAX_FREQ 52000000
87
88struct spi_regs {
89 u32 command1; /* 000:SPI_COMMAND1 register */
90 u32 command2; /* 004:SPI_COMMAND2 register */
91 u32 timing1; /* 008:SPI_CS_TIM1 register */
92 u32 timing2; /* 00c:SPI_CS_TIM2 register */
93 u32 xfer_status;/* 010:SPI_TRANS_STATUS register */
94 u32 fifo_status;/* 014:SPI_FIFO_STATUS register */
95 u32 tx_data; /* 018:SPI_TX_DATA register */
96 u32 rx_data; /* 01c:SPI_RX_DATA register */
97 u32 dma_ctl; /* 020:SPI_DMA_CTL register */
98 u32 dma_blk; /* 024:SPI_DMA_BLK register */
99 u32 rsvd[56]; /* 028-107 reserved */
100 u32 tx_fifo; /* 108:SPI_FIFO1 register */
101 u32 rsvd2[31]; /* 10c-187 reserved */
102 u32 rx_fifo; /* 188:SPI_FIFO2 register */
103 u32 spare_ctl; /* 18c:SPI_SPARE_CTRL register */
104};
105
Simon Glassfda6fac2014-10-13 23:42:13 -0600106struct tegra114_spi_priv {
Allen Martin77c42e82013-03-16 18:58:13 +0000107 struct spi_regs *regs;
108 unsigned int freq;
109 unsigned int mode;
110 int periph_id;
111 int valid;
Simon Glassfda6fac2014-10-13 23:42:13 -0600112 int last_transaction_us;
Allen Martin77c42e82013-03-16 18:58:13 +0000113};
114
Simon Glassfda6fac2014-10-13 23:42:13 -0600115static int tegra114_spi_ofdata_to_platdata(struct udevice *bus)
Allen Martin77c42e82013-03-16 18:58:13 +0000116{
Simon Glassfda6fac2014-10-13 23:42:13 -0600117 struct tegra_spi_platdata *plat = bus->platdata;
118 const void *blob = gd->fdt_blob;
119 int node = bus->of_offset;
120
121 plat->base = fdtdec_get_addr(blob, node, "reg");
122 plat->periph_id = clock_decode_periph_id(blob, node);
123
124 if (plat->periph_id == PERIPH_ID_NONE) {
125 debug("%s: could not decode periph id %d\n", __func__,
126 plat->periph_id);
127 return -FDT_ERR_NOTFOUND;
128 }
129
130 /* Use 500KHz as a suitable default */
131 plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
132 500000);
133 plat->deactivate_delay_us = fdtdec_get_int(blob, node,
134 "spi-deactivate-delay", 0);
135 debug("%s: base=%#08lx, periph_id=%d, max-frequency=%d, deactivate_delay=%d\n",
136 __func__, plat->base, plat->periph_id, plat->frequency,
137 plat->deactivate_delay_us);
138
139 return 0;
Allen Martin77c42e82013-03-16 18:58:13 +0000140}
141
Simon Glassfda6fac2014-10-13 23:42:13 -0600142static int tegra114_spi_probe(struct udevice *bus)
Allen Martin77c42e82013-03-16 18:58:13 +0000143{
Simon Glassfda6fac2014-10-13 23:42:13 -0600144 struct tegra_spi_platdata *plat = dev_get_platdata(bus);
145 struct tegra114_spi_priv *priv = dev_get_priv(bus);
Simon Glass635c2512015-06-05 14:39:33 -0600146 struct spi_regs *regs;
Simon Glassfda6fac2014-10-13 23:42:13 -0600147
148 priv->regs = (struct spi_regs *)plat->base;
Simon Glass635c2512015-06-05 14:39:33 -0600149 regs = priv->regs;
Simon Glassfda6fac2014-10-13 23:42:13 -0600150
151 priv->last_transaction_us = timer_get_us();
152 priv->freq = plat->frequency;
153 priv->periph_id = plat->periph_id;
154
Allen Martin77c42e82013-03-16 18:58:13 +0000155 /* Change SPI clock to correct frequency, PLLP_OUT0 source */
Simon Glass635c2512015-06-05 14:39:33 -0600156 clock_start_periph_pll(priv->periph_id, CLOCK_ID_PERIPH,
157 priv->freq);
Allen Martin77c42e82013-03-16 18:58:13 +0000158
159 /* Clear stale status here */
160 setbits_le32(&regs->fifo_status,
161 SPI_FIFO_STS_ERR |
162 SPI_FIFO_STS_TX_FIFO_OVF |
163 SPI_FIFO_STS_TX_FIFO_UNR |
164 SPI_FIFO_STS_RX_FIFO_OVF |
165 SPI_FIFO_STS_RX_FIFO_UNR |
166 SPI_FIFO_STS_TX_FIFO_FULL |
167 SPI_FIFO_STS_TX_FIFO_EMPTY |
168 SPI_FIFO_STS_RX_FIFO_FULL |
169 SPI_FIFO_STS_RX_FIFO_EMPTY);
170 debug("%s: FIFO STATUS = %08x\n", __func__, readl(&regs->fifo_status));
171
Simon Glass635c2512015-06-05 14:39:33 -0600172 setbits_le32(&priv->regs->command1, SPI_CMD1_M_S | SPI_CMD1_CS_SW_HW |
173 (priv->mode << SPI_CMD1_MODE_SHIFT) | SPI_CMD1_CS_SW_VAL);
Allen Martin77c42e82013-03-16 18:58:13 +0000174 debug("%s: COMMAND1 = %08x\n", __func__, readl(&regs->command1));
175
176 return 0;
177}
178
Simon Glassfda6fac2014-10-13 23:42:13 -0600179/**
180 * Activate the CS by driving it LOW
181 *
182 * @param slave Pointer to spi_slave to which controller has to
183 * communicate with
184 */
185static void spi_cs_activate(struct udevice *dev)
Allen Martin77c42e82013-03-16 18:58:13 +0000186{
Simon Glassfda6fac2014-10-13 23:42:13 -0600187 struct udevice *bus = dev->parent;
188 struct tegra_spi_platdata *pdata = dev_get_platdata(bus);
189 struct tegra114_spi_priv *priv = dev_get_priv(bus);
Allen Martin77c42e82013-03-16 18:58:13 +0000190
Simon Glassfda6fac2014-10-13 23:42:13 -0600191 /* If it's too soon to do another transaction, wait */
192 if (pdata->deactivate_delay_us &&
193 priv->last_transaction_us) {
194 ulong delay_us; /* The delay completed so far */
195 delay_us = timer_get_us() - priv->last_transaction_us;
196 if (delay_us < pdata->deactivate_delay_us)
197 udelay(pdata->deactivate_delay_us - delay_us);
198 }
199
200 clrbits_le32(&priv->regs->command1, SPI_CMD1_CS_SW_VAL);
Allen Martin77c42e82013-03-16 18:58:13 +0000201}
202
Simon Glassfda6fac2014-10-13 23:42:13 -0600203/**
204 * Deactivate the CS by driving it HIGH
205 *
206 * @param slave Pointer to spi_slave to which controller has to
207 * communicate with
208 */
209static void spi_cs_deactivate(struct udevice *dev)
Allen Martin77c42e82013-03-16 18:58:13 +0000210{
Simon Glassfda6fac2014-10-13 23:42:13 -0600211 struct udevice *bus = dev->parent;
212 struct tegra_spi_platdata *pdata = dev_get_platdata(bus);
213 struct tegra114_spi_priv *priv = dev_get_priv(bus);
Allen Martin77c42e82013-03-16 18:58:13 +0000214
Simon Glassfda6fac2014-10-13 23:42:13 -0600215 setbits_le32(&priv->regs->command1, SPI_CMD1_CS_SW_VAL);
216
217 /* Remember time of this transaction so we can honour the bus delay */
218 if (pdata->deactivate_delay_us)
219 priv->last_transaction_us = timer_get_us();
220
221 debug("Deactivate CS, bus '%s'\n", bus->name);
Allen Martin77c42e82013-03-16 18:58:13 +0000222}
223
Simon Glassfda6fac2014-10-13 23:42:13 -0600224static int tegra114_spi_xfer(struct udevice *dev, unsigned int bitlen,
225 const void *data_out, void *data_in,
226 unsigned long flags)
Allen Martin77c42e82013-03-16 18:58:13 +0000227{
Simon Glassfda6fac2014-10-13 23:42:13 -0600228 struct udevice *bus = dev->parent;
229 struct tegra114_spi_priv *priv = dev_get_priv(bus);
230 struct spi_regs *regs = priv->regs;
Allen Martin77c42e82013-03-16 18:58:13 +0000231 u32 reg, tmpdout, tmpdin = 0;
232 const u8 *dout = data_out;
233 u8 *din = data_in;
234 int num_bytes;
235 int ret;
236
237 debug("%s: slave %u:%u dout %p din %p bitlen %u\n",
Simon Glassfda6fac2014-10-13 23:42:13 -0600238 __func__, bus->seq, spi_chip_select(dev), dout, din, bitlen);
Allen Martin77c42e82013-03-16 18:58:13 +0000239 if (bitlen % 8)
240 return -1;
241 num_bytes = bitlen / 8;
242
243 ret = 0;
244
Simon Glass635c2512015-06-05 14:39:33 -0600245 if (flags & SPI_XFER_BEGIN)
246 spi_cs_activate(dev);
247
Allen Martin77c42e82013-03-16 18:58:13 +0000248 /* clear all error status bits */
249 reg = readl(&regs->fifo_status);
250 writel(reg, &regs->fifo_status);
251
Allen Martin77c42e82013-03-16 18:58:13 +0000252 clrsetbits_le32(&regs->command1, SPI_CMD1_CS_SW_VAL,
253 SPI_CMD1_RX_EN | SPI_CMD1_TX_EN | SPI_CMD1_LSBY_FE |
Simon Glassfda6fac2014-10-13 23:42:13 -0600254 (spi_chip_select(dev) << SPI_CMD1_CS_SEL_SHIFT));
Allen Martin77c42e82013-03-16 18:58:13 +0000255
256 /* set xfer size to 1 block (32 bits) */
257 writel(0, &regs->dma_blk);
258
Allen Martin77c42e82013-03-16 18:58:13 +0000259 /* handle data in 32-bit chunks */
260 while (num_bytes > 0) {
261 int bytes;
Allen Martin77c42e82013-03-16 18:58:13 +0000262 int tm, i;
263
264 tmpdout = 0;
265 bytes = (num_bytes > 4) ? 4 : num_bytes;
266
267 if (dout != NULL) {
268 for (i = 0; i < bytes; ++i)
269 tmpdout = (tmpdout << 8) | dout[i];
270 dout += bytes;
271 }
272
273 num_bytes -= bytes;
274
Yen Lin60acde42013-12-18 11:18:46 -0700275 /* clear ready bit */
276 setbits_le32(&regs->xfer_status, SPI_XFER_STS_RDY);
277
Allen Martin77c42e82013-03-16 18:58:13 +0000278 clrsetbits_le32(&regs->command1,
279 SPI_CMD1_BIT_LEN_MASK << SPI_CMD1_BIT_LEN_SHIFT,
280 (bytes * 8 - 1) << SPI_CMD1_BIT_LEN_SHIFT);
281 writel(tmpdout, &regs->tx_fifo);
282 setbits_le32(&regs->command1, SPI_CMD1_GO);
283
284 /*
285 * Wait for SPI transmit FIFO to empty, or to time out.
286 * The RX FIFO status will be read and cleared last
287 */
Yen Lin60acde42013-12-18 11:18:46 -0700288 for (tm = 0; tm < SPI_TIMEOUT; ++tm) {
Allen Martin77c42e82013-03-16 18:58:13 +0000289 u32 fifo_status, xfer_status;
290
Allen Martin77c42e82013-03-16 18:58:13 +0000291 xfer_status = readl(&regs->xfer_status);
292 if (!(xfer_status & SPI_XFER_STS_RDY))
293 continue;
294
Yen Lin60acde42013-12-18 11:18:46 -0700295 fifo_status = readl(&regs->fifo_status);
Allen Martin77c42e82013-03-16 18:58:13 +0000296 if (fifo_status & SPI_FIFO_STS_ERR) {
297 debug("%s: got a fifo error: ", __func__);
298 if (fifo_status & SPI_FIFO_STS_TX_FIFO_OVF)
299 debug("tx FIFO overflow ");
300 if (fifo_status & SPI_FIFO_STS_TX_FIFO_UNR)
301 debug("tx FIFO underrun ");
302 if (fifo_status & SPI_FIFO_STS_RX_FIFO_OVF)
303 debug("rx FIFO overflow ");
304 if (fifo_status & SPI_FIFO_STS_RX_FIFO_UNR)
305 debug("rx FIFO underrun ");
306 if (fifo_status & SPI_FIFO_STS_TX_FIFO_FULL)
307 debug("tx FIFO full ");
308 if (fifo_status & SPI_FIFO_STS_TX_FIFO_EMPTY)
309 debug("tx FIFO empty ");
310 if (fifo_status & SPI_FIFO_STS_RX_FIFO_FULL)
311 debug("rx FIFO full ");
312 if (fifo_status & SPI_FIFO_STS_RX_FIFO_EMPTY)
313 debug("rx FIFO empty ");
314 debug("\n");
315 break;
316 }
317
318 if (!(fifo_status & SPI_FIFO_STS_RX_FIFO_EMPTY)) {
319 tmpdin = readl(&regs->rx_fifo);
Allen Martin77c42e82013-03-16 18:58:13 +0000320
321 /* swap bytes read in */
322 if (din != NULL) {
323 for (i = bytes - 1; i >= 0; --i) {
324 din[i] = tmpdin & 0xff;
325 tmpdin >>= 8;
326 }
327 din += bytes;
328 }
Yen Lin60acde42013-12-18 11:18:46 -0700329
330 /* We can exit when we've had both RX and TX */
331 break;
Allen Martin77c42e82013-03-16 18:58:13 +0000332 }
333 }
334
335 if (tm >= SPI_TIMEOUT)
336 ret = tm;
337
338 /* clear ACK RDY, etc. bits */
339 writel(readl(&regs->fifo_status), &regs->fifo_status);
340 }
341
342 if (flags & SPI_XFER_END)
Simon Glassfda6fac2014-10-13 23:42:13 -0600343 spi_cs_deactivate(dev);
Allen Martin77c42e82013-03-16 18:58:13 +0000344
345 debug("%s: transfer ended. Value=%08x, fifo_status = %08x\n",
346 __func__, tmpdin, readl(&regs->fifo_status));
347
348 if (ret) {
349 printf("%s: timeout during SPI transfer, tm %d\n",
350 __func__, ret);
351 return -1;
352 }
353
Simon Glassfda6fac2014-10-13 23:42:13 -0600354 return ret;
355}
356
357static int tegra114_spi_set_speed(struct udevice *bus, uint speed)
358{
359 struct tegra_spi_platdata *plat = bus->platdata;
360 struct tegra114_spi_priv *priv = dev_get_priv(bus);
361
362 if (speed > plat->frequency)
363 speed = plat->frequency;
364 priv->freq = speed;
365 debug("%s: regs=%p, speed=%d\n", __func__, priv->regs, priv->freq);
366
Allen Martin77c42e82013-03-16 18:58:13 +0000367 return 0;
368}
Simon Glassfda6fac2014-10-13 23:42:13 -0600369
370static int tegra114_spi_set_mode(struct udevice *bus, uint mode)
371{
372 struct tegra114_spi_priv *priv = dev_get_priv(bus);
373
374 priv->mode = mode;
375 debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode);
376
377 return 0;
378}
379
380static const struct dm_spi_ops tegra114_spi_ops = {
Simon Glassfda6fac2014-10-13 23:42:13 -0600381 .xfer = tegra114_spi_xfer,
382 .set_speed = tegra114_spi_set_speed,
383 .set_mode = tegra114_spi_set_mode,
384 /*
385 * cs_info is not needed, since we require all chip selects to be
386 * in the device tree explicitly
387 */
388};
389
390static const struct udevice_id tegra114_spi_ids[] = {
391 { .compatible = "nvidia,tegra114-spi" },
392 { }
393};
394
395U_BOOT_DRIVER(tegra114_spi) = {
396 .name = "tegra114_spi",
397 .id = UCLASS_SPI,
398 .of_match = tegra114_spi_ids,
399 .ops = &tegra114_spi_ops,
400 .ofdata_to_platdata = tegra114_spi_ofdata_to_platdata,
401 .platdata_auto_alloc_size = sizeof(struct tegra_spi_platdata),
402 .priv_auto_alloc_size = sizeof(struct tegra114_spi_priv),
Simon Glassfda6fac2014-10-13 23:42:13 -0600403 .probe = tegra114_spi_probe,
404};