blob: 603c024bcc1613ac08f90eeb4d49e282ee0ec7ea [file] [log] [blame]
Tom Warren9112ef82011-11-05 09:48:11 +00001/*
Allen Martin6b3a03e2013-03-16 18:58:06 +00002 * Copyright (c) 2010-2013 NVIDIA Corporation
Tom Warren9112ef82011-11-05 09:48:11 +00003 * With help from the mpc8xxx SPI driver
4 * With more help from omap3_spi SPI driver
5 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Tom Warren9112ef82011-11-05 09:48:11 +00007 */
8
9#include <common.h>
Tom Warren9112ef82011-11-05 09:48:11 +000010#include <malloc.h>
Tom Warren9112ef82011-11-05 09:48:11 +000011#include <asm/io.h>
12#include <asm/gpio.h>
Tom Warren9112ef82011-11-05 09:48:11 +000013#include <asm/arch/clock.h>
14#include <asm/arch/pinmux.h>
Tom Warren150c2492012-09-19 15:50:56 -070015#include <asm/arch-tegra/clk_rst.h>
Allen Martinff1da6f2013-03-16 18:58:03 +000016#include <asm/arch-tegra20/tegra20_sflash.h>
Tom Warren150c2492012-09-19 15:50:56 -070017#include <spi.h>
Allen Martin8f1b46b2013-01-29 13:51:24 +000018#include <fdtdec.h>
19
20DECLARE_GLOBAL_DATA_PTR;
Tom Warren9112ef82011-11-05 09:48:11 +000021
Allen Martin7a49ba62013-03-16 18:58:05 +000022#define SPI_CMD_GO (1 << 30)
23#define SPI_CMD_ACTIVE_SCLK_SHIFT 26
24#define SPI_CMD_ACTIVE_SCLK_MASK (3 << SPI_CMD_ACTIVE_SCLK_SHIFT)
25#define SPI_CMD_CK_SDA (1 << 21)
26#define SPI_CMD_ACTIVE_SDA_SHIFT 18
27#define SPI_CMD_ACTIVE_SDA_MASK (3 << SPI_CMD_ACTIVE_SDA_SHIFT)
28#define SPI_CMD_CS_POL (1 << 16)
29#define SPI_CMD_TXEN (1 << 15)
30#define SPI_CMD_RXEN (1 << 14)
31#define SPI_CMD_CS_VAL (1 << 13)
32#define SPI_CMD_CS_SOFT (1 << 12)
33#define SPI_CMD_CS_DELAY (1 << 9)
34#define SPI_CMD_CS3_EN (1 << 8)
35#define SPI_CMD_CS2_EN (1 << 7)
36#define SPI_CMD_CS1_EN (1 << 6)
37#define SPI_CMD_CS0_EN (1 << 5)
38#define SPI_CMD_BIT_LENGTH (1 << 4)
39#define SPI_CMD_BIT_LENGTH_MASK 0x0000001F
40
41#define SPI_STAT_BSY (1 << 31)
42#define SPI_STAT_RDY (1 << 30)
43#define SPI_STAT_RXF_FLUSH (1 << 29)
44#define SPI_STAT_TXF_FLUSH (1 << 28)
45#define SPI_STAT_RXF_UNR (1 << 27)
46#define SPI_STAT_TXF_OVF (1 << 26)
47#define SPI_STAT_RXF_EMPTY (1 << 25)
48#define SPI_STAT_RXF_FULL (1 << 24)
49#define SPI_STAT_TXF_EMPTY (1 << 23)
50#define SPI_STAT_TXF_FULL (1 << 22)
51#define SPI_STAT_SEL_TXRX_N (1 << 16)
52#define SPI_STAT_CUR_BLKCNT (1 << 15)
53
54#define SPI_TIMEOUT 1000
55#define TEGRA_SPI_MAX_FREQ 52000000
56
57struct spi_regs {
58 u32 command; /* SPI_COMMAND_0 register */
59 u32 status; /* SPI_STATUS_0 register */
60 u32 rx_cmp; /* SPI_RX_CMP_0 register */
61 u32 dma_ctl; /* SPI_DMA_CTL_0 register */
62 u32 tx_fifo; /* SPI_TX_FIFO_0 register */
63 u32 rsvd[3]; /* offsets 0x14 to 0x1F reserved */
64 u32 rx_fifo; /* SPI_RX_FIFO_0 register */
65};
66
Allen Martin6b3a03e2013-03-16 18:58:06 +000067struct tegra_spi_ctrl {
Allen Martin7a49ba62013-03-16 18:58:05 +000068 struct spi_regs *regs;
Tom Warren9112ef82011-11-05 09:48:11 +000069 unsigned int freq;
70 unsigned int mode;
Allen Martin8f1b46b2013-01-29 13:51:24 +000071 int periph_id;
Allen Martin6b3a03e2013-03-16 18:58:06 +000072 int valid;
Tom Warren9112ef82011-11-05 09:48:11 +000073};
74
Allen Martin6b3a03e2013-03-16 18:58:06 +000075struct tegra_spi_slave {
76 struct spi_slave slave;
77 struct tegra_spi_ctrl *ctrl;
78};
79
80/* tegra20 only supports one SFLASH controller */
81static struct tegra_spi_ctrl spi_ctrls[1];
82
Tom Warren9112ef82011-11-05 09:48:11 +000083static inline struct tegra_spi_slave *to_tegra_spi(struct spi_slave *slave)
84{
85 return container_of(slave, struct tegra_spi_slave, slave);
86}
87
Allen Martin78f47b72013-03-16 18:58:07 +000088int tegra20_spi_cs_is_valid(unsigned int bus, unsigned int cs)
Tom Warren9112ef82011-11-05 09:48:11 +000089{
Allen Martin00a27492012-08-31 08:30:00 +000090 /* Tegra20 SPI-Flash - only 1 device ('bus/cs') */
Tom Warren9112ef82011-11-05 09:48:11 +000091 if (bus != 0 || cs != 0)
92 return 0;
93 else
94 return 1;
95}
96
Allen Martin78f47b72013-03-16 18:58:07 +000097struct spi_slave *tegra20_spi_setup_slave(unsigned int bus, unsigned int cs,
98 unsigned int max_hz, unsigned int mode)
Tom Warren9112ef82011-11-05 09:48:11 +000099{
100 struct tegra_spi_slave *spi;
101
102 if (!spi_cs_is_valid(bus, cs)) {
103 printf("SPI error: unsupported bus %d / chip select %d\n",
104 bus, cs);
105 return NULL;
106 }
107
Tom Warren29f3e3f2012-09-04 17:00:24 -0700108 if (max_hz > TEGRA_SPI_MAX_FREQ) {
Tom Warren9112ef82011-11-05 09:48:11 +0000109 printf("SPI error: unsupported frequency %d Hz. Max frequency"
Tom Warren29f3e3f2012-09-04 17:00:24 -0700110 " is %d Hz\n", max_hz, TEGRA_SPI_MAX_FREQ);
Tom Warren9112ef82011-11-05 09:48:11 +0000111 return NULL;
112 }
113
Albert ARIBAUD009d75c2013-03-28 18:50:01 +0100114 spi = spi_alloc_slave(struct tegra_spi_slave, bus, cs);
Tom Warren9112ef82011-11-05 09:48:11 +0000115 if (!spi) {
116 printf("SPI error: malloc of SPI structure failed\n");
117 return NULL;
118 }
Allen Martin6b3a03e2013-03-16 18:58:06 +0000119 spi->ctrl = &spi_ctrls[bus];
120 if (!spi->ctrl) {
121 printf("SPI error: could not find controller for bus %d\n",
122 bus);
123 return NULL;
124 }
Allen Martin2a3c5bc2013-03-16 18:58:04 +0000125
Allen Martin6b3a03e2013-03-16 18:58:06 +0000126 if (max_hz < spi->ctrl->freq) {
Allen Martin8f1b46b2013-01-29 13:51:24 +0000127 debug("%s: limiting frequency from %u to %u\n", __func__,
Allen Martin6b3a03e2013-03-16 18:58:06 +0000128 spi->ctrl->freq, max_hz);
129 spi->ctrl->freq = max_hz;
Allen Martin8f1b46b2013-01-29 13:51:24 +0000130 }
Allen Martin6b3a03e2013-03-16 18:58:06 +0000131 spi->ctrl->mode = mode;
Tom Warren9112ef82011-11-05 09:48:11 +0000132
133 return &spi->slave;
134}
135
Allen Martin78f47b72013-03-16 18:58:07 +0000136void tegra20_spi_free_slave(struct spi_slave *slave)
Tom Warren9112ef82011-11-05 09:48:11 +0000137{
138 struct tegra_spi_slave *spi = to_tegra_spi(slave);
139
140 free(spi);
141}
142
Allen Martin78f47b72013-03-16 18:58:07 +0000143int tegra20_spi_init(int *node_list, int count)
Tom Warren9112ef82011-11-05 09:48:11 +0000144{
Allen Martin6b3a03e2013-03-16 18:58:06 +0000145 struct tegra_spi_ctrl *ctrl;
146 int i;
147 int node = 0;
Allen Martin78f47b72013-03-16 18:58:07 +0000148 int found = 0;
Allen Martin6b3a03e2013-03-16 18:58:06 +0000149
Allen Martin6b3a03e2013-03-16 18:58:06 +0000150 for (i = 0; i < count; i++) {
151 ctrl = &spi_ctrls[i];
152 node = node_list[i];
153
154 ctrl->regs = (struct spi_regs *)fdtdec_get_addr(gd->fdt_blob,
155 node, "reg");
156 if ((fdt_addr_t)ctrl->regs == FDT_ADDR_T_NONE) {
157 debug("%s: no slink register found\n", __func__);
158 continue;
159 }
160 ctrl->freq = fdtdec_get_int(gd->fdt_blob, node,
161 "spi-max-frequency", 0);
162 if (!ctrl->freq) {
163 debug("%s: no slink max frequency found\n", __func__);
164 continue;
165 }
166
167 ctrl->periph_id = clock_decode_periph_id(gd->fdt_blob, node);
168 if (ctrl->periph_id == PERIPH_ID_NONE) {
169 debug("%s: could not decode periph id\n", __func__);
170 continue;
171 }
172 ctrl->valid = 1;
Allen Martin78f47b72013-03-16 18:58:07 +0000173 found = 1;
Allen Martin6b3a03e2013-03-16 18:58:06 +0000174
175 debug("%s: found controller at %p, freq = %u, periph_id = %d\n",
176 __func__, ctrl->regs, ctrl->freq, ctrl->periph_id);
177 }
Allen Martin78f47b72013-03-16 18:58:07 +0000178 return !found;
Tom Warren9112ef82011-11-05 09:48:11 +0000179}
180
Allen Martin78f47b72013-03-16 18:58:07 +0000181int tegra20_spi_claim_bus(struct spi_slave *slave)
Tom Warren9112ef82011-11-05 09:48:11 +0000182{
183 struct tegra_spi_slave *spi = to_tegra_spi(slave);
Allen Martin6b3a03e2013-03-16 18:58:06 +0000184 struct spi_regs *regs = spi->ctrl->regs;
Tom Warren9112ef82011-11-05 09:48:11 +0000185 u32 reg;
186
187 /* Change SPI clock to correct frequency, PLLP_OUT0 source */
Allen Martin6b3a03e2013-03-16 18:58:06 +0000188 clock_start_periph_pll(spi->ctrl->periph_id, CLOCK_ID_PERIPH,
189 spi->ctrl->freq);
Tom Warren9112ef82011-11-05 09:48:11 +0000190
191 /* Clear stale status here */
192 reg = SPI_STAT_RDY | SPI_STAT_RXF_FLUSH | SPI_STAT_TXF_FLUSH | \
193 SPI_STAT_RXF_UNR | SPI_STAT_TXF_OVF;
194 writel(reg, &regs->status);
Allen Martin78f47b72013-03-16 18:58:07 +0000195 debug("%s: STATUS = %08x\n", __func__, readl(&regs->status));
Tom Warren9112ef82011-11-05 09:48:11 +0000196
197 /*
198 * Use sw-controlled CS, so we can clock in data after ReadID, etc.
199 */
Allen Martin6b3a03e2013-03-16 18:58:06 +0000200 reg = (spi->ctrl->mode & 1) << SPI_CMD_ACTIVE_SDA_SHIFT;
201 if (spi->ctrl->mode & 2)
Tom Warren9112ef82011-11-05 09:48:11 +0000202 reg |= 1 << SPI_CMD_ACTIVE_SCLK_SHIFT;
203 clrsetbits_le32(&regs->command, SPI_CMD_ACTIVE_SCLK_MASK |
204 SPI_CMD_ACTIVE_SDA_MASK, SPI_CMD_CS_SOFT | reg);
Allen Martin78f47b72013-03-16 18:58:07 +0000205 debug("%s: COMMAND = %08x\n", __func__, readl(&regs->command));
Tom Warren9112ef82011-11-05 09:48:11 +0000206
207 /*
Allen Martin00a27492012-08-31 08:30:00 +0000208 * SPI pins on Tegra20 are muxed - change pinmux later due to UART
Tom Warren9112ef82011-11-05 09:48:11 +0000209 * issue.
210 */
211 pinmux_set_func(PINGRP_GMD, PMUX_FUNC_SFLASH);
212 pinmux_tristate_disable(PINGRP_LSPI);
Allen Martin90006522013-03-16 18:58:02 +0000213 pinmux_set_func(PINGRP_GMC, PMUX_FUNC_SFLASH);
Simon Glass4560c7d2011-11-05 04:46:50 +0000214
Tom Warren9112ef82011-11-05 09:48:11 +0000215 return 0;
216}
217
Allen Martin78f47b72013-03-16 18:58:07 +0000218void tegra20_spi_cs_activate(struct spi_slave *slave)
Tom Warren9112ef82011-11-05 09:48:11 +0000219{
220 struct tegra_spi_slave *spi = to_tegra_spi(slave);
Allen Martin6b3a03e2013-03-16 18:58:06 +0000221 struct spi_regs *regs = spi->ctrl->regs;
Tom Warren9112ef82011-11-05 09:48:11 +0000222
Tom Warren9112ef82011-11-05 09:48:11 +0000223 /* CS is negated on Tegra, so drive a 1 to get a 0 */
Allen Martin6b3a03e2013-03-16 18:58:06 +0000224 setbits_le32(&regs->command, SPI_CMD_CS_VAL);
Tom Warren9112ef82011-11-05 09:48:11 +0000225}
226
Allen Martin78f47b72013-03-16 18:58:07 +0000227void tegra20_spi_cs_deactivate(struct spi_slave *slave)
Tom Warren9112ef82011-11-05 09:48:11 +0000228{
229 struct tegra_spi_slave *spi = to_tegra_spi(slave);
Allen Martin6b3a03e2013-03-16 18:58:06 +0000230 struct spi_regs *regs = spi->ctrl->regs;
Tom Warren9112ef82011-11-05 09:48:11 +0000231
232 /* CS is negated on Tegra, so drive a 0 to get a 1 */
Allen Martin6b3a03e2013-03-16 18:58:06 +0000233 clrbits_le32(&regs->command, SPI_CMD_CS_VAL);
Tom Warren9112ef82011-11-05 09:48:11 +0000234}
235
Allen Martin78f47b72013-03-16 18:58:07 +0000236int tegra20_spi_xfer(struct spi_slave *slave, unsigned int bitlen,
Tom Warren9112ef82011-11-05 09:48:11 +0000237 const void *data_out, void *data_in, unsigned long flags)
238{
239 struct tegra_spi_slave *spi = to_tegra_spi(slave);
Allen Martin6b3a03e2013-03-16 18:58:06 +0000240 struct spi_regs *regs = spi->ctrl->regs;
Tom Warren9112ef82011-11-05 09:48:11 +0000241 u32 reg, tmpdout, tmpdin = 0;
242 const u8 *dout = data_out;
243 u8 *din = data_in;
244 int num_bytes;
245 int ret;
246
247 debug("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n",
248 slave->bus, slave->cs, *(u8 *)dout, *(u8 *)din, bitlen);
249 if (bitlen % 8)
250 return -1;
251 num_bytes = bitlen / 8;
252
253 ret = 0;
254
255 reg = readl(&regs->status);
256 writel(reg, &regs->status); /* Clear all SPI events via R/W */
257 debug("spi_xfer entry: STATUS = %08x\n", reg);
258
259 reg = readl(&regs->command);
260 reg |= SPI_CMD_TXEN | SPI_CMD_RXEN;
261 writel(reg, &regs->command);
262 debug("spi_xfer: COMMAND = %08x\n", readl(&regs->command));
263
264 if (flags & SPI_XFER_BEGIN)
265 spi_cs_activate(slave);
266
267 /* handle data in 32-bit chunks */
268 while (num_bytes > 0) {
269 int bytes;
270 int is_read = 0;
271 int tm, i;
272
273 tmpdout = 0;
274 bytes = (num_bytes > 4) ? 4 : num_bytes;
275
276 if (dout != NULL) {
277 for (i = 0; i < bytes; ++i)
278 tmpdout = (tmpdout << 8) | dout[i];
279 }
280
281 num_bytes -= bytes;
282 if (dout)
283 dout += bytes;
284
285 clrsetbits_le32(&regs->command, SPI_CMD_BIT_LENGTH_MASK,
286 bytes * 8 - 1);
287 writel(tmpdout, &regs->tx_fifo);
288 setbits_le32(&regs->command, SPI_CMD_GO);
289
290 /*
291 * Wait for SPI transmit FIFO to empty, or to time out.
292 * The RX FIFO status will be read and cleared last
293 */
294 for (tm = 0, is_read = 0; tm < SPI_TIMEOUT; ++tm) {
295 u32 status;
296
297 status = readl(&regs->status);
298
299 /* We can exit when we've had both RX and TX activity */
300 if (is_read && (status & SPI_STAT_TXF_EMPTY))
301 break;
302
303 if ((status & (SPI_STAT_BSY | SPI_STAT_RDY)) !=
304 SPI_STAT_RDY)
305 tm++;
306
307 else if (!(status & SPI_STAT_RXF_EMPTY)) {
308 tmpdin = readl(&regs->rx_fifo);
309 is_read = 1;
310
311 /* swap bytes read in */
312 if (din != NULL) {
313 for (i = bytes - 1; i >= 0; --i) {
314 din[i] = tmpdin & 0xff;
315 tmpdin >>= 8;
316 }
317 din += bytes;
318 }
319 }
320 }
321
322 if (tm >= SPI_TIMEOUT)
323 ret = tm;
324
325 /* clear ACK RDY, etc. bits */
326 writel(readl(&regs->status), &regs->status);
327 }
328
329 if (flags & SPI_XFER_END)
330 spi_cs_deactivate(slave);
331
332 debug("spi_xfer: transfer ended. Value=%08x, status = %08x\n",
333 tmpdin, readl(&regs->status));
334
335 if (ret) {
336 printf("spi_xfer: timeout during SPI transfer, tm %d\n", ret);
337 return -1;
338 }
339
340 return 0;
341}