blob: c6af30f0d66b6df1e39c4912e62b6a1ccd0f0316 [file] [log] [blame]
Tom Warren9112ef82011-11-05 09:48:11 +00001/*
Tom Warrenedffa632012-05-22 07:33:47 +00002 * Copyright (c) 2010-2012 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 *
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 Warren9112ef82011-11-05 09:48:11 +000026#include <malloc.h>
Tom Warren9112ef82011-11-05 09:48:11 +000027#include <asm/io.h>
28#include <asm/gpio.h>
Tom Warren9112ef82011-11-05 09:48:11 +000029#include <asm/arch/clock.h>
30#include <asm/arch/pinmux.h>
Tom Warren150c2492012-09-19 15:50:56 -070031#include <asm/arch-tegra/clk_rst.h>
Allen Martinff1da6f2013-03-16 18:58:03 +000032#include <asm/arch-tegra20/tegra20_sflash.h>
Tom Warren150c2492012-09-19 15:50:56 -070033#include <spi.h>
Allen Martin8f1b46b2013-01-29 13:51:24 +000034#include <fdtdec.h>
35
36DECLARE_GLOBAL_DATA_PTR;
Tom Warren9112ef82011-11-05 09:48:11 +000037
38struct tegra_spi_slave {
39 struct spi_slave slave;
40 struct spi_tegra *regs;
41 unsigned int freq;
42 unsigned int mode;
Allen Martin8f1b46b2013-01-29 13:51:24 +000043 int periph_id;
Tom Warren9112ef82011-11-05 09:48:11 +000044};
45
46static inline struct tegra_spi_slave *to_tegra_spi(struct spi_slave *slave)
47{
48 return container_of(slave, struct tegra_spi_slave, slave);
49}
50
51int spi_cs_is_valid(unsigned int bus, unsigned int cs)
52{
Allen Martin00a27492012-08-31 08:30:00 +000053 /* Tegra20 SPI-Flash - only 1 device ('bus/cs') */
Tom Warren9112ef82011-11-05 09:48:11 +000054 if (bus != 0 || cs != 0)
55 return 0;
56 else
57 return 1;
58}
59
60struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
61 unsigned int max_hz, unsigned int mode)
62{
63 struct tegra_spi_slave *spi;
64
65 if (!spi_cs_is_valid(bus, cs)) {
66 printf("SPI error: unsupported bus %d / chip select %d\n",
67 bus, cs);
68 return NULL;
69 }
70
Tom Warren29f3e3f2012-09-04 17:00:24 -070071 if (max_hz > TEGRA_SPI_MAX_FREQ) {
Tom Warren9112ef82011-11-05 09:48:11 +000072 printf("SPI error: unsupported frequency %d Hz. Max frequency"
Tom Warren29f3e3f2012-09-04 17:00:24 -070073 " is %d Hz\n", max_hz, TEGRA_SPI_MAX_FREQ);
Tom Warren9112ef82011-11-05 09:48:11 +000074 return NULL;
75 }
76
77 spi = malloc(sizeof(struct tegra_spi_slave));
78 if (!spi) {
79 printf("SPI error: malloc of SPI structure failed\n");
80 return NULL;
81 }
82 spi->slave.bus = bus;
83 spi->slave.cs = cs;
Allen Martin8f1b46b2013-01-29 13:51:24 +000084#ifdef CONFIG_OF_CONTROL
85 int node = fdtdec_next_compatible(gd->fdt_blob, 0,
86 COMPAT_NVIDIA_TEGRA20_SFLASH);
87 if (node < 0) {
88 debug("%s: cannot locate sflash node\n", __func__);
89 return NULL;
90 }
91 if (!fdtdec_get_is_enabled(gd->fdt_blob, node)) {
92 debug("%s: sflash is disabled\n", __func__);
93 return NULL;
94 }
95 spi->regs = (struct spi_tegra *)fdtdec_get_addr(gd->fdt_blob,
96 node, "reg");
97 if ((fdt_addr_t)spi->regs == FDT_ADDR_T_NONE) {
98 debug("%s: no sflash register found\n", __func__);
99 return NULL;
100 }
101 spi->freq = fdtdec_get_int(gd->fdt_blob, node, "spi-max-frequency", 0);
102 if (!spi->freq) {
103 debug("%s: no sflash max frequency found\n", __func__);
104 return NULL;
105 }
106 spi->periph_id = clock_decode_periph_id(gd->fdt_blob, node);
107 if (spi->periph_id == PERIPH_ID_NONE) {
108 debug("%s: could not decode periph id\n", __func__);
109 return NULL;
110 }
111#else
Tom Warren29f3e3f2012-09-04 17:00:24 -0700112 spi->regs = (struct spi_tegra *)NV_PA_SPI_BASE;
Allen Martin8f1b46b2013-01-29 13:51:24 +0000113 spi->freq = TEGRA_SPI_MAX_FREQ;
114 spi->periph_id = PERIPH_ID_SPI1;
115#endif
116 if (max_hz < spi->freq) {
117 debug("%s: limiting frequency from %u to %u\n", __func__,
118 spi->freq, max_hz);
119 spi->freq = max_hz;
120 }
121 debug("%s: controller initialized at %p, freq = %u, periph_id = %d\n",
122 __func__, spi->regs, spi->freq, spi->periph_id);
Tom Warren9112ef82011-11-05 09:48:11 +0000123 spi->mode = mode;
124
125 return &spi->slave;
126}
127
128void spi_free_slave(struct spi_slave *slave)
129{
130 struct tegra_spi_slave *spi = to_tegra_spi(slave);
131
132 free(spi);
133}
134
135void spi_init(void)
136{
137 /* do nothing */
138}
139
140int spi_claim_bus(struct spi_slave *slave)
141{
142 struct tegra_spi_slave *spi = to_tegra_spi(slave);
143 struct spi_tegra *regs = spi->regs;
144 u32 reg;
145
146 /* Change SPI clock to correct frequency, PLLP_OUT0 source */
Allen Martin8f1b46b2013-01-29 13:51:24 +0000147 clock_start_periph_pll(spi->periph_id, CLOCK_ID_PERIPH, spi->freq);
Tom Warren9112ef82011-11-05 09:48:11 +0000148
149 /* Clear stale status here */
150 reg = SPI_STAT_RDY | SPI_STAT_RXF_FLUSH | SPI_STAT_TXF_FLUSH | \
151 SPI_STAT_RXF_UNR | SPI_STAT_TXF_OVF;
152 writel(reg, &regs->status);
153 debug("spi_init: STATUS = %08x\n", readl(&regs->status));
154
155 /*
156 * Use sw-controlled CS, so we can clock in data after ReadID, etc.
157 */
158 reg = (spi->mode & 1) << SPI_CMD_ACTIVE_SDA_SHIFT;
159 if (spi->mode & 2)
160 reg |= 1 << SPI_CMD_ACTIVE_SCLK_SHIFT;
161 clrsetbits_le32(&regs->command, SPI_CMD_ACTIVE_SCLK_MASK |
162 SPI_CMD_ACTIVE_SDA_MASK, SPI_CMD_CS_SOFT | reg);
163 debug("spi_init: COMMAND = %08x\n", readl(&regs->command));
164
165 /*
Allen Martin00a27492012-08-31 08:30:00 +0000166 * SPI pins on Tegra20 are muxed - change pinmux later due to UART
Tom Warren9112ef82011-11-05 09:48:11 +0000167 * issue.
168 */
169 pinmux_set_func(PINGRP_GMD, PMUX_FUNC_SFLASH);
170 pinmux_tristate_disable(PINGRP_LSPI);
Allen Martin90006522013-03-16 18:58:02 +0000171 pinmux_set_func(PINGRP_GMC, PMUX_FUNC_SFLASH);
Simon Glass4560c7d2011-11-05 04:46:50 +0000172
Tom Warren9112ef82011-11-05 09:48:11 +0000173 return 0;
174}
175
176void spi_release_bus(struct spi_slave *slave)
177{
178 /*
179 * We can't release UART_DISABLE and set pinmux to UART4 here since
180 * some code (e,g, spi_flash_probe) uses printf() while the SPI
181 * bus is held. That is arguably bad, but it has the advantage of
182 * already being in the source tree.
183 */
184}
185
186void spi_cs_activate(struct spi_slave *slave)
187{
188 struct tegra_spi_slave *spi = to_tegra_spi(slave);
189
Tom Warren9112ef82011-11-05 09:48:11 +0000190 /* CS is negated on Tegra, so drive a 1 to get a 0 */
191 setbits_le32(&spi->regs->command, SPI_CMD_CS_VAL);
192}
193
194void spi_cs_deactivate(struct spi_slave *slave)
195{
196 struct tegra_spi_slave *spi = to_tegra_spi(slave);
197
198 /* CS is negated on Tegra, so drive a 0 to get a 1 */
199 clrbits_le32(&spi->regs->command, SPI_CMD_CS_VAL);
Tom Warren9112ef82011-11-05 09:48:11 +0000200}
201
202int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
203 const void *data_out, void *data_in, unsigned long flags)
204{
205 struct tegra_spi_slave *spi = to_tegra_spi(slave);
206 struct spi_tegra *regs = spi->regs;
207 u32 reg, tmpdout, tmpdin = 0;
208 const u8 *dout = data_out;
209 u8 *din = data_in;
210 int num_bytes;
211 int ret;
212
213 debug("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n",
214 slave->bus, slave->cs, *(u8 *)dout, *(u8 *)din, bitlen);
215 if (bitlen % 8)
216 return -1;
217 num_bytes = bitlen / 8;
218
219 ret = 0;
220
221 reg = readl(&regs->status);
222 writel(reg, &regs->status); /* Clear all SPI events via R/W */
223 debug("spi_xfer entry: STATUS = %08x\n", reg);
224
225 reg = readl(&regs->command);
226 reg |= SPI_CMD_TXEN | SPI_CMD_RXEN;
227 writel(reg, &regs->command);
228 debug("spi_xfer: COMMAND = %08x\n", readl(&regs->command));
229
230 if (flags & SPI_XFER_BEGIN)
231 spi_cs_activate(slave);
232
233 /* handle data in 32-bit chunks */
234 while (num_bytes > 0) {
235 int bytes;
236 int is_read = 0;
237 int tm, i;
238
239 tmpdout = 0;
240 bytes = (num_bytes > 4) ? 4 : num_bytes;
241
242 if (dout != NULL) {
243 for (i = 0; i < bytes; ++i)
244 tmpdout = (tmpdout << 8) | dout[i];
245 }
246
247 num_bytes -= bytes;
248 if (dout)
249 dout += bytes;
250
251 clrsetbits_le32(&regs->command, SPI_CMD_BIT_LENGTH_MASK,
252 bytes * 8 - 1);
253 writel(tmpdout, &regs->tx_fifo);
254 setbits_le32(&regs->command, SPI_CMD_GO);
255
256 /*
257 * Wait for SPI transmit FIFO to empty, or to time out.
258 * The RX FIFO status will be read and cleared last
259 */
260 for (tm = 0, is_read = 0; tm < SPI_TIMEOUT; ++tm) {
261 u32 status;
262
263 status = readl(&regs->status);
264
265 /* We can exit when we've had both RX and TX activity */
266 if (is_read && (status & SPI_STAT_TXF_EMPTY))
267 break;
268
269 if ((status & (SPI_STAT_BSY | SPI_STAT_RDY)) !=
270 SPI_STAT_RDY)
271 tm++;
272
273 else if (!(status & SPI_STAT_RXF_EMPTY)) {
274 tmpdin = readl(&regs->rx_fifo);
275 is_read = 1;
276
277 /* swap bytes read in */
278 if (din != NULL) {
279 for (i = bytes - 1; i >= 0; --i) {
280 din[i] = tmpdin & 0xff;
281 tmpdin >>= 8;
282 }
283 din += bytes;
284 }
285 }
286 }
287
288 if (tm >= SPI_TIMEOUT)
289 ret = tm;
290
291 /* clear ACK RDY, etc. bits */
292 writel(readl(&regs->status), &regs->status);
293 }
294
295 if (flags & SPI_XFER_END)
296 spi_cs_deactivate(slave);
297
298 debug("spi_xfer: transfer ended. Value=%08x, status = %08x\n",
299 tmpdin, readl(&regs->status));
300
301 if (ret) {
302 printf("spi_xfer: timeout during SPI transfer, tm %d\n", ret);
303 return -1;
304 }
305
306 return 0;
307}