2 * Copyright (c) 2010-2012 NVIDIA Corporation
3 * With help from the mpc8xxx SPI driver
4 * With more help from omap3_spi SPI driver
6 * See file CREDITS for list of people who contributed to this
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.
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.
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,
31 #include <asm/arch/clk_rst.h>
32 #include <asm/arch/clock.h>
33 #include <asm/arch/pinmux.h>
34 #include <asm/arch/uart-spi-switch.h>
35 #include <asm/arch/tegra_spi.h>
37 #if defined(CONFIG_SPI_CORRUPTS_UART)
38 #define corrupt_delay() udelay(CONFIG_SPI_CORRUPTS_UART_DLY);
40 #define corrupt_delay()
43 struct tegra_spi_slave {
44 struct spi_slave slave;
45 struct spi_tegra *regs;
50 static inline struct tegra_spi_slave *to_tegra_spi(struct spi_slave *slave)
52 return container_of(slave, struct tegra_spi_slave, slave);
55 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
57 /* Tegra20 SPI-Flash - only 1 device ('bus/cs') */
58 if (bus != 0 || cs != 0)
64 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
65 unsigned int max_hz, unsigned int mode)
67 struct tegra_spi_slave *spi;
69 if (!spi_cs_is_valid(bus, cs)) {
70 printf("SPI error: unsupported bus %d / chip select %d\n",
75 if (max_hz > TEGRA20_SPI_MAX_FREQ) {
76 printf("SPI error: unsupported frequency %d Hz. Max frequency"
77 " is %d Hz\n", max_hz, TEGRA20_SPI_MAX_FREQ);
81 spi = malloc(sizeof(struct tegra_spi_slave));
83 printf("SPI error: malloc of SPI structure failed\n");
89 spi->regs = (struct spi_tegra *)TEGRA20_SPI_BASE;
95 void spi_free_slave(struct spi_slave *slave)
97 struct tegra_spi_slave *spi = to_tegra_spi(slave);
107 int spi_claim_bus(struct spi_slave *slave)
109 struct tegra_spi_slave *spi = to_tegra_spi(slave);
110 struct spi_tegra *regs = spi->regs;
113 /* Change SPI clock to correct frequency, PLLP_OUT0 source */
114 clock_start_periph_pll(PERIPH_ID_SPI1, CLOCK_ID_PERIPH, spi->freq);
116 /* Clear stale status here */
117 reg = SPI_STAT_RDY | SPI_STAT_RXF_FLUSH | SPI_STAT_TXF_FLUSH | \
118 SPI_STAT_RXF_UNR | SPI_STAT_TXF_OVF;
119 writel(reg, ®s->status);
120 debug("spi_init: STATUS = %08x\n", readl(®s->status));
123 * Use sw-controlled CS, so we can clock in data after ReadID, etc.
125 reg = (spi->mode & 1) << SPI_CMD_ACTIVE_SDA_SHIFT;
127 reg |= 1 << SPI_CMD_ACTIVE_SCLK_SHIFT;
128 clrsetbits_le32(®s->command, SPI_CMD_ACTIVE_SCLK_MASK |
129 SPI_CMD_ACTIVE_SDA_MASK, SPI_CMD_CS_SOFT | reg);
130 debug("spi_init: COMMAND = %08x\n", readl(®s->command));
133 * SPI pins on Tegra20 are muxed - change pinmux later due to UART
136 pinmux_set_func(PINGRP_GMD, PMUX_FUNC_SFLASH);
137 pinmux_tristate_disable(PINGRP_LSPI);
139 #ifndef CONFIG_SPI_UART_SWITCH
142 * Only set PinMux bits 3:2 to SPI here on boards that don't have the
143 * SPI UART switch or subsequent UART data won't go out! See
146 /* TODO: pinmux_set_func(PINGRP_GMC, PMUX_FUNC_SFLASH); */
151 void spi_release_bus(struct spi_slave *slave)
154 * We can't release UART_DISABLE and set pinmux to UART4 here since
155 * some code (e,g, spi_flash_probe) uses printf() while the SPI
156 * bus is held. That is arguably bad, but it has the advantage of
157 * already being in the source tree.
161 void spi_cs_activate(struct spi_slave *slave)
163 struct tegra_spi_slave *spi = to_tegra_spi(slave);
167 /* CS is negated on Tegra, so drive a 1 to get a 0 */
168 setbits_le32(&spi->regs->command, SPI_CMD_CS_VAL);
170 corrupt_delay(); /* Let UART settle */
173 void spi_cs_deactivate(struct spi_slave *slave)
175 struct tegra_spi_slave *spi = to_tegra_spi(slave);
177 pinmux_select_uart();
179 /* CS is negated on Tegra, so drive a 0 to get a 1 */
180 clrbits_le32(&spi->regs->command, SPI_CMD_CS_VAL);
182 corrupt_delay(); /* Let SPI settle */
185 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
186 const void *data_out, void *data_in, unsigned long flags)
188 struct tegra_spi_slave *spi = to_tegra_spi(slave);
189 struct spi_tegra *regs = spi->regs;
190 u32 reg, tmpdout, tmpdin = 0;
191 const u8 *dout = data_out;
196 debug("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n",
197 slave->bus, slave->cs, *(u8 *)dout, *(u8 *)din, bitlen);
200 num_bytes = bitlen / 8;
204 reg = readl(®s->status);
205 writel(reg, ®s->status); /* Clear all SPI events via R/W */
206 debug("spi_xfer entry: STATUS = %08x\n", reg);
208 reg = readl(®s->command);
209 reg |= SPI_CMD_TXEN | SPI_CMD_RXEN;
210 writel(reg, ®s->command);
211 debug("spi_xfer: COMMAND = %08x\n", readl(®s->command));
213 if (flags & SPI_XFER_BEGIN)
214 spi_cs_activate(slave);
216 /* handle data in 32-bit chunks */
217 while (num_bytes > 0) {
223 bytes = (num_bytes > 4) ? 4 : num_bytes;
226 for (i = 0; i < bytes; ++i)
227 tmpdout = (tmpdout << 8) | dout[i];
234 clrsetbits_le32(®s->command, SPI_CMD_BIT_LENGTH_MASK,
236 writel(tmpdout, ®s->tx_fifo);
237 setbits_le32(®s->command, SPI_CMD_GO);
240 * Wait for SPI transmit FIFO to empty, or to time out.
241 * The RX FIFO status will be read and cleared last
243 for (tm = 0, is_read = 0; tm < SPI_TIMEOUT; ++tm) {
246 status = readl(®s->status);
248 /* We can exit when we've had both RX and TX activity */
249 if (is_read && (status & SPI_STAT_TXF_EMPTY))
252 if ((status & (SPI_STAT_BSY | SPI_STAT_RDY)) !=
256 else if (!(status & SPI_STAT_RXF_EMPTY)) {
257 tmpdin = readl(®s->rx_fifo);
260 /* swap bytes read in */
262 for (i = bytes - 1; i >= 0; --i) {
263 din[i] = tmpdin & 0xff;
271 if (tm >= SPI_TIMEOUT)
274 /* clear ACK RDY, etc. bits */
275 writel(readl(®s->status), ®s->status);
278 if (flags & SPI_XFER_END)
279 spi_cs_deactivate(slave);
281 debug("spi_xfer: transfer ended. Value=%08x, status = %08x\n",
282 tmpdin, readl(®s->status));
285 printf("spi_xfer: timeout during SPI transfer, tm %d\n", ret);