blob: 56cb22963db635b9a5e5b01c5cf3793f1c7ba980 [file] [log] [blame]
Tom Warrenee554f82011-11-05 09:48:11 +00001/*
2 * Copyright (c) 2010-2011 NVIDIA Corporation
3 * 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>
26
27#include <malloc.h>
28#include <spi.h>
29#include <asm/io.h>
30#include <asm/gpio.h>
Simon Glass34bad072011-11-05 04:46:50 +000031#include <ns16550.h>
Tom Warrenee554f82011-11-05 09:48:11 +000032#include <asm/arch/clk_rst.h>
33#include <asm/arch/clock.h>
34#include <asm/arch/pinmux.h>
Simon Glass34bad072011-11-05 04:46:50 +000035#include <asm/arch/uart-spi-switch.h>
Tom Warrenee554f82011-11-05 09:48:11 +000036#include <asm/arch/tegra2_spi.h>
37
38struct tegra_spi_slave {
39 struct spi_slave slave;
40 struct spi_tegra *regs;
41 unsigned int freq;
42 unsigned int mode;
43};
44
45static inline struct tegra_spi_slave *to_tegra_spi(struct spi_slave *slave)
46{
47 return container_of(slave, struct tegra_spi_slave, slave);
48}
49
50int spi_cs_is_valid(unsigned int bus, unsigned int cs)
51{
52 /* Tegra2 SPI-Flash - only 1 device ('bus/cs') */
53 if (bus != 0 || cs != 0)
54 return 0;
55 else
56 return 1;
57}
58
59struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
60 unsigned int max_hz, unsigned int mode)
61{
62 struct tegra_spi_slave *spi;
63
64 if (!spi_cs_is_valid(bus, cs)) {
65 printf("SPI error: unsupported bus %d / chip select %d\n",
66 bus, cs);
67 return NULL;
68 }
69
70 if (max_hz > TEGRA2_SPI_MAX_FREQ) {
71 printf("SPI error: unsupported frequency %d Hz. Max frequency"
72 " is %d Hz\n", max_hz, TEGRA2_SPI_MAX_FREQ);
73 return NULL;
74 }
75
76 spi = malloc(sizeof(struct tegra_spi_slave));
77 if (!spi) {
78 printf("SPI error: malloc of SPI structure failed\n");
79 return NULL;
80 }
81 spi->slave.bus = bus;
82 spi->slave.cs = cs;
83 spi->freq = max_hz;
84 spi->regs = (struct spi_tegra *)TEGRA2_SPI_BASE;
85 spi->mode = mode;
86
87 return &spi->slave;
88}
89
90void spi_free_slave(struct spi_slave *slave)
91{
92 struct tegra_spi_slave *spi = to_tegra_spi(slave);
93
94 free(spi);
95}
96
97void spi_init(void)
98{
99 /* do nothing */
100}
101
102int spi_claim_bus(struct spi_slave *slave)
103{
104 struct tegra_spi_slave *spi = to_tegra_spi(slave);
105 struct spi_tegra *regs = spi->regs;
106 u32 reg;
107
108 /* Change SPI clock to correct frequency, PLLP_OUT0 source */
109 clock_start_periph_pll(PERIPH_ID_SPI1, CLOCK_ID_PERIPH, spi->freq);
110
111 /* Clear stale status here */
112 reg = SPI_STAT_RDY | SPI_STAT_RXF_FLUSH | SPI_STAT_TXF_FLUSH | \
113 SPI_STAT_RXF_UNR | SPI_STAT_TXF_OVF;
114 writel(reg, &regs->status);
115 debug("spi_init: STATUS = %08x\n", readl(&regs->status));
116
117 /*
118 * Use sw-controlled CS, so we can clock in data after ReadID, etc.
119 */
120 reg = (spi->mode & 1) << SPI_CMD_ACTIVE_SDA_SHIFT;
121 if (spi->mode & 2)
122 reg |= 1 << SPI_CMD_ACTIVE_SCLK_SHIFT;
123 clrsetbits_le32(&regs->command, SPI_CMD_ACTIVE_SCLK_MASK |
124 SPI_CMD_ACTIVE_SDA_MASK, SPI_CMD_CS_SOFT | reg);
125 debug("spi_init: COMMAND = %08x\n", readl(&regs->command));
126
127 /*
128 * SPI pins on Tegra2 are muxed - change pinmux later due to UART
129 * issue.
130 */
131 pinmux_set_func(PINGRP_GMD, PMUX_FUNC_SFLASH);
132 pinmux_tristate_disable(PINGRP_LSPI);
Simon Glass34bad072011-11-05 04:46:50 +0000133
134#ifndef CONFIG_SPI_UART_SWITCH
135 /*
136 * NOTE:
137 * Only set PinMux bits 3:2 to SPI here on boards that don't have the
138 * SPI UART switch or subsequent UART data won't go out! See
139 * spi_uart_switch().
140 */
141 /* TODO: pinmux_set_func(PINGRP_GMC, PMUX_FUNC_SFLASH); */
142#endif
Tom Warrenee554f82011-11-05 09:48:11 +0000143 return 0;
144}
145
146void spi_release_bus(struct spi_slave *slave)
147{
148 /*
149 * We can't release UART_DISABLE and set pinmux to UART4 here since
150 * some code (e,g, spi_flash_probe) uses printf() while the SPI
151 * bus is held. That is arguably bad, but it has the advantage of
152 * already being in the source tree.
153 */
154}
155
156void spi_cs_activate(struct spi_slave *slave)
157{
158 struct tegra_spi_slave *spi = to_tegra_spi(slave);
159
Simon Glass34bad072011-11-05 04:46:50 +0000160 pinmux_select_spi();
161
Tom Warrenee554f82011-11-05 09:48:11 +0000162 /* CS is negated on Tegra, so drive a 1 to get a 0 */
163 setbits_le32(&spi->regs->command, SPI_CMD_CS_VAL);
164}
165
166void spi_cs_deactivate(struct spi_slave *slave)
167{
168 struct tegra_spi_slave *spi = to_tegra_spi(slave);
169
170 /* CS is negated on Tegra, so drive a 0 to get a 1 */
171 clrbits_le32(&spi->regs->command, SPI_CMD_CS_VAL);
172}
173
174int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
175 const void *data_out, void *data_in, unsigned long flags)
176{
177 struct tegra_spi_slave *spi = to_tegra_spi(slave);
178 struct spi_tegra *regs = spi->regs;
179 u32 reg, tmpdout, tmpdin = 0;
180 const u8 *dout = data_out;
181 u8 *din = data_in;
182 int num_bytes;
183 int ret;
184
185 debug("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n",
186 slave->bus, slave->cs, *(u8 *)dout, *(u8 *)din, bitlen);
187 if (bitlen % 8)
188 return -1;
189 num_bytes = bitlen / 8;
190
191 ret = 0;
192
193 reg = readl(&regs->status);
194 writel(reg, &regs->status); /* Clear all SPI events via R/W */
195 debug("spi_xfer entry: STATUS = %08x\n", reg);
196
197 reg = readl(&regs->command);
198 reg |= SPI_CMD_TXEN | SPI_CMD_RXEN;
199 writel(reg, &regs->command);
200 debug("spi_xfer: COMMAND = %08x\n", readl(&regs->command));
201
202 if (flags & SPI_XFER_BEGIN)
203 spi_cs_activate(slave);
204
205 /* handle data in 32-bit chunks */
206 while (num_bytes > 0) {
207 int bytes;
208 int is_read = 0;
209 int tm, i;
210
211 tmpdout = 0;
212 bytes = (num_bytes > 4) ? 4 : num_bytes;
213
214 if (dout != NULL) {
215 for (i = 0; i < bytes; ++i)
216 tmpdout = (tmpdout << 8) | dout[i];
217 }
218
219 num_bytes -= bytes;
220 if (dout)
221 dout += bytes;
222
223 clrsetbits_le32(&regs->command, SPI_CMD_BIT_LENGTH_MASK,
224 bytes * 8 - 1);
225 writel(tmpdout, &regs->tx_fifo);
226 setbits_le32(&regs->command, SPI_CMD_GO);
227
228 /*
229 * Wait for SPI transmit FIFO to empty, or to time out.
230 * The RX FIFO status will be read and cleared last
231 */
232 for (tm = 0, is_read = 0; tm < SPI_TIMEOUT; ++tm) {
233 u32 status;
234
235 status = readl(&regs->status);
236
237 /* We can exit when we've had both RX and TX activity */
238 if (is_read && (status & SPI_STAT_TXF_EMPTY))
239 break;
240
241 if ((status & (SPI_STAT_BSY | SPI_STAT_RDY)) !=
242 SPI_STAT_RDY)
243 tm++;
244
245 else if (!(status & SPI_STAT_RXF_EMPTY)) {
246 tmpdin = readl(&regs->rx_fifo);
247 is_read = 1;
248
249 /* swap bytes read in */
250 if (din != NULL) {
251 for (i = bytes - 1; i >= 0; --i) {
252 din[i] = tmpdin & 0xff;
253 tmpdin >>= 8;
254 }
255 din += bytes;
256 }
257 }
258 }
259
260 if (tm >= SPI_TIMEOUT)
261 ret = tm;
262
263 /* clear ACK RDY, etc. bits */
264 writel(readl(&regs->status), &regs->status);
265 }
266
267 if (flags & SPI_XFER_END)
268 spi_cs_deactivate(slave);
269
270 debug("spi_xfer: transfer ended. Value=%08x, status = %08x\n",
271 tmpdin, readl(&regs->status));
272
273 if (ret) {
274 printf("spi_xfer: timeout during SPI transfer, tm %d\n", ret);
275 return -1;
276 }
277
278 return 0;
279}