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