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