blob: ce19095af03d931f5db46094289a48d79f464ebf [file] [log] [blame]
Tom Warrenee554f82011-11-05 09:48:11 +00001/*
Tom Warrene8aebc22012-05-22 07:33:47 +00002 * Copyright (c) 2010-2012 NVIDIA Corporation
Tom Warrenee554f82011-11-05 09:48:11 +00003 * With help from the mpc8xxx SPI driver
4 * With more help from omap3_spi SPI driver
5 *
6 * See file CREDITS for list of people who contributed to this
7 * project.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22 * MA 02111-1307 USA
23 */
24
25#include <common.h>
Tom Warrenee554f82011-11-05 09:48:11 +000026#include <malloc.h>
Tom Warrenee554f82011-11-05 09:48:11 +000027#include <asm/io.h>
28#include <asm/gpio.h>
Tom Warrenee554f82011-11-05 09:48:11 +000029#include <asm/arch/clock.h>
30#include <asm/arch/pinmux.h>
Simon Glass34bad072011-11-05 04:46:50 +000031#include <asm/arch/uart-spi-switch.h>
Tom Warrenab371962012-09-19 15:50:56 -070032#include <asm/arch-tegra/clk_rst.h>
33#include <asm/arch-tegra/tegra_spi.h>
34#include <spi.h>
Allen Martine7659522013-01-29 13:51:24 +000035#include <fdtdec.h>
36
37DECLARE_GLOBAL_DATA_PTR;
Tom Warrenee554f82011-11-05 09:48:11 +000038
Tom Warrenc309b462012-05-15 14:32:40 -070039#if defined(CONFIG_SPI_CORRUPTS_UART)
40 #define corrupt_delay() udelay(CONFIG_SPI_CORRUPTS_UART_DLY);
41#else
42 #define corrupt_delay()
43#endif
44
Tom Warrenee554f82011-11-05 09:48:11 +000045struct tegra_spi_slave {
46 struct spi_slave slave;
47 struct spi_tegra *regs;
48 unsigned int freq;
49 unsigned int mode;
Allen Martine7659522013-01-29 13:51:24 +000050 int periph_id;
Tom Warrenee554f82011-11-05 09:48:11 +000051};
52
53static inline struct tegra_spi_slave *to_tegra_spi(struct spi_slave *slave)
54{
55 return container_of(slave, struct tegra_spi_slave, slave);
56}
57
58int spi_cs_is_valid(unsigned int bus, unsigned int cs)
59{
Allen Martin55d98a12012-08-31 08:30:00 +000060 /* Tegra20 SPI-Flash - only 1 device ('bus/cs') */
Tom Warrenee554f82011-11-05 09:48:11 +000061 if (bus != 0 || cs != 0)
62 return 0;
63 else
64 return 1;
65}
66
67struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
68 unsigned int max_hz, unsigned int mode)
69{
70 struct tegra_spi_slave *spi;
71
72 if (!spi_cs_is_valid(bus, cs)) {
73 printf("SPI error: unsupported bus %d / chip select %d\n",
74 bus, cs);
75 return NULL;
76 }
77
Tom Warren22562a42012-09-04 17:00:24 -070078 if (max_hz > TEGRA_SPI_MAX_FREQ) {
Tom Warrenee554f82011-11-05 09:48:11 +000079 printf("SPI error: unsupported frequency %d Hz. Max frequency"
Tom Warren22562a42012-09-04 17:00:24 -070080 " is %d Hz\n", max_hz, TEGRA_SPI_MAX_FREQ);
Tom Warrenee554f82011-11-05 09:48:11 +000081 return NULL;
82 }
83
84 spi = malloc(sizeof(struct tegra_spi_slave));
85 if (!spi) {
86 printf("SPI error: malloc of SPI structure failed\n");
87 return NULL;
88 }
89 spi->slave.bus = bus;
90 spi->slave.cs = cs;
Allen Martine7659522013-01-29 13:51:24 +000091#ifdef CONFIG_OF_CONTROL
92 int node = fdtdec_next_compatible(gd->fdt_blob, 0,
93 COMPAT_NVIDIA_TEGRA20_SFLASH);
94 if (node < 0) {
95 debug("%s: cannot locate sflash node\n", __func__);
96 return NULL;
97 }
98 if (!fdtdec_get_is_enabled(gd->fdt_blob, node)) {
99 debug("%s: sflash is disabled\n", __func__);
100 return NULL;
101 }
102 spi->regs = (struct spi_tegra *)fdtdec_get_addr(gd->fdt_blob,
103 node, "reg");
104 if ((fdt_addr_t)spi->regs == FDT_ADDR_T_NONE) {
105 debug("%s: no sflash register found\n", __func__);
106 return NULL;
107 }
108 spi->freq = fdtdec_get_int(gd->fdt_blob, node, "spi-max-frequency", 0);
109 if (!spi->freq) {
110 debug("%s: no sflash max frequency found\n", __func__);
111 return NULL;
112 }
113 spi->periph_id = clock_decode_periph_id(gd->fdt_blob, node);
114 if (spi->periph_id == PERIPH_ID_NONE) {
115 debug("%s: could not decode periph id\n", __func__);
116 return NULL;
117 }
118#else
Tom Warren22562a42012-09-04 17:00:24 -0700119 spi->regs = (struct spi_tegra *)NV_PA_SPI_BASE;
Allen Martine7659522013-01-29 13:51:24 +0000120 spi->freq = TEGRA_SPI_MAX_FREQ;
121 spi->periph_id = PERIPH_ID_SPI1;
122#endif
123 if (max_hz < spi->freq) {
124 debug("%s: limiting frequency from %u to %u\n", __func__,
125 spi->freq, max_hz);
126 spi->freq = max_hz;
127 }
128 debug("%s: controller initialized at %p, freq = %u, periph_id = %d\n",
129 __func__, spi->regs, spi->freq, spi->periph_id);
Tom Warrenee554f82011-11-05 09:48:11 +0000130 spi->mode = mode;
131
132 return &spi->slave;
133}
134
135void spi_free_slave(struct spi_slave *slave)
136{
137 struct tegra_spi_slave *spi = to_tegra_spi(slave);
138
139 free(spi);
140}
141
142void spi_init(void)
143{
144 /* do nothing */
145}
146
147int spi_claim_bus(struct spi_slave *slave)
148{
149 struct tegra_spi_slave *spi = to_tegra_spi(slave);
150 struct spi_tegra *regs = spi->regs;
151 u32 reg;
152
153 /* Change SPI clock to correct frequency, PLLP_OUT0 source */
Allen Martine7659522013-01-29 13:51:24 +0000154 clock_start_periph_pll(spi->periph_id, CLOCK_ID_PERIPH, spi->freq);
Tom Warrenee554f82011-11-05 09:48:11 +0000155
156 /* Clear stale status here */
157 reg = SPI_STAT_RDY | SPI_STAT_RXF_FLUSH | SPI_STAT_TXF_FLUSH | \
158 SPI_STAT_RXF_UNR | SPI_STAT_TXF_OVF;
159 writel(reg, &regs->status);
160 debug("spi_init: STATUS = %08x\n", readl(&regs->status));
161
162 /*
163 * Use sw-controlled CS, so we can clock in data after ReadID, etc.
164 */
165 reg = (spi->mode & 1) << SPI_CMD_ACTIVE_SDA_SHIFT;
166 if (spi->mode & 2)
167 reg |= 1 << SPI_CMD_ACTIVE_SCLK_SHIFT;
168 clrsetbits_le32(&regs->command, SPI_CMD_ACTIVE_SCLK_MASK |
169 SPI_CMD_ACTIVE_SDA_MASK, SPI_CMD_CS_SOFT | reg);
170 debug("spi_init: COMMAND = %08x\n", readl(&regs->command));
171
172 /*
Allen Martin55d98a12012-08-31 08:30:00 +0000173 * SPI pins on Tegra20 are muxed - change pinmux later due to UART
Tom Warrenee554f82011-11-05 09:48:11 +0000174 * issue.
175 */
176 pinmux_set_func(PINGRP_GMD, PMUX_FUNC_SFLASH);
177 pinmux_tristate_disable(PINGRP_LSPI);
Simon Glass34bad072011-11-05 04:46:50 +0000178
179#ifndef CONFIG_SPI_UART_SWITCH
180 /*
181 * NOTE:
182 * Only set PinMux bits 3:2 to SPI here on boards that don't have the
183 * SPI UART switch or subsequent UART data won't go out! See
184 * spi_uart_switch().
185 */
186 /* TODO: pinmux_set_func(PINGRP_GMC, PMUX_FUNC_SFLASH); */
187#endif
Tom Warrenee554f82011-11-05 09:48:11 +0000188 return 0;
189}
190
191void spi_release_bus(struct spi_slave *slave)
192{
193 /*
194 * We can't release UART_DISABLE and set pinmux to UART4 here since
195 * some code (e,g, spi_flash_probe) uses printf() while the SPI
196 * bus is held. That is arguably bad, but it has the advantage of
197 * already being in the source tree.
198 */
199}
200
201void spi_cs_activate(struct spi_slave *slave)
202{
203 struct tegra_spi_slave *spi = to_tegra_spi(slave);
204
Simon Glass34bad072011-11-05 04:46:50 +0000205 pinmux_select_spi();
206
Tom Warrenee554f82011-11-05 09:48:11 +0000207 /* CS is negated on Tegra, so drive a 1 to get a 0 */
208 setbits_le32(&spi->regs->command, SPI_CMD_CS_VAL);
Tom Warrenc309b462012-05-15 14:32:40 -0700209
210 corrupt_delay(); /* Let UART settle */
Tom Warrenee554f82011-11-05 09:48:11 +0000211}
212
213void spi_cs_deactivate(struct spi_slave *slave)
214{
215 struct tegra_spi_slave *spi = to_tegra_spi(slave);
216
Tom Warrenc309b462012-05-15 14:32:40 -0700217 pinmux_select_uart();
218
Tom Warrenee554f82011-11-05 09:48:11 +0000219 /* CS is negated on Tegra, so drive a 0 to get a 1 */
220 clrbits_le32(&spi->regs->command, SPI_CMD_CS_VAL);
Tom Warrenc309b462012-05-15 14:32:40 -0700221
222 corrupt_delay(); /* Let SPI settle */
Tom Warrenee554f82011-11-05 09:48:11 +0000223}
224
225int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
226 const void *data_out, void *data_in, unsigned long flags)
227{
228 struct tegra_spi_slave *spi = to_tegra_spi(slave);
229 struct spi_tegra *regs = spi->regs;
230 u32 reg, tmpdout, tmpdin = 0;
231 const u8 *dout = data_out;
232 u8 *din = data_in;
233 int num_bytes;
234 int ret;
235
236 debug("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n",
237 slave->bus, slave->cs, *(u8 *)dout, *(u8 *)din, bitlen);
238 if (bitlen % 8)
239 return -1;
240 num_bytes = bitlen / 8;
241
242 ret = 0;
243
244 reg = readl(&regs->status);
245 writel(reg, &regs->status); /* Clear all SPI events via R/W */
246 debug("spi_xfer entry: STATUS = %08x\n", reg);
247
248 reg = readl(&regs->command);
249 reg |= SPI_CMD_TXEN | SPI_CMD_RXEN;
250 writel(reg, &regs->command);
251 debug("spi_xfer: COMMAND = %08x\n", readl(&regs->command));
252
253 if (flags & SPI_XFER_BEGIN)
254 spi_cs_activate(slave);
255
256 /* handle data in 32-bit chunks */
257 while (num_bytes > 0) {
258 int bytes;
259 int is_read = 0;
260 int tm, i;
261
262 tmpdout = 0;
263 bytes = (num_bytes > 4) ? 4 : num_bytes;
264
265 if (dout != NULL) {
266 for (i = 0; i < bytes; ++i)
267 tmpdout = (tmpdout << 8) | dout[i];
268 }
269
270 num_bytes -= bytes;
271 if (dout)
272 dout += bytes;
273
274 clrsetbits_le32(&regs->command, SPI_CMD_BIT_LENGTH_MASK,
275 bytes * 8 - 1);
276 writel(tmpdout, &regs->tx_fifo);
277 setbits_le32(&regs->command, SPI_CMD_GO);
278
279 /*
280 * Wait for SPI transmit FIFO to empty, or to time out.
281 * The RX FIFO status will be read and cleared last
282 */
283 for (tm = 0, is_read = 0; tm < SPI_TIMEOUT; ++tm) {
284 u32 status;
285
286 status = readl(&regs->status);
287
288 /* We can exit when we've had both RX and TX activity */
289 if (is_read && (status & SPI_STAT_TXF_EMPTY))
290 break;
291
292 if ((status & (SPI_STAT_BSY | SPI_STAT_RDY)) !=
293 SPI_STAT_RDY)
294 tm++;
295
296 else if (!(status & SPI_STAT_RXF_EMPTY)) {
297 tmpdin = readl(&regs->rx_fifo);
298 is_read = 1;
299
300 /* swap bytes read in */
301 if (din != NULL) {
302 for (i = bytes - 1; i >= 0; --i) {
303 din[i] = tmpdin & 0xff;
304 tmpdin >>= 8;
305 }
306 din += bytes;
307 }
308 }
309 }
310
311 if (tm >= SPI_TIMEOUT)
312 ret = tm;
313
314 /* clear ACK RDY, etc. bits */
315 writel(readl(&regs->status), &regs->status);
316 }
317
318 if (flags & SPI_XFER_END)
319 spi_cs_deactivate(slave);
320
321 debug("spi_xfer: transfer ended. Value=%08x, status = %08x\n",
322 tmpdin, readl(&regs->status));
323
324 if (ret) {
325 printf("spi_xfer: timeout during SPI transfer, tm %d\n", ret);
326 return -1;
327 }
328
329 return 0;
330}