Allen Martin | ba4fb9b | 2013-01-29 13:51:28 +0000 | [diff] [blame] | 1 | /* |
| 2 | * NVIDIA Tegra SPI-SLINK controller |
| 3 | * |
| 4 | * Copyright (c) 2010-2013 NVIDIA Corporation |
| 5 | * |
| 6 | * See file CREDITS for list of people who contributed to this |
| 7 | * project. |
| 8 | * |
| 9 | * This software is licensed under the terms of the GNU General Public |
| 10 | * License version 2, as published by the Free Software Foundation, and |
| 11 | * may be copied, distributed, and modified under those terms. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include <common.h> |
| 25 | #include <malloc.h> |
| 26 | #include <asm/io.h> |
| 27 | #include <asm/gpio.h> |
| 28 | #include <asm/arch/clock.h> |
| 29 | #include <asm/arch-tegra/clk_rst.h> |
| 30 | #include <asm/arch-tegra/tegra_slink.h> |
| 31 | #include <spi.h> |
| 32 | #include <fdtdec.h> |
| 33 | |
| 34 | DECLARE_GLOBAL_DATA_PTR; |
| 35 | |
| 36 | struct tegra_spi_ctrl { |
| 37 | struct slink_tegra *regs; |
| 38 | unsigned int freq; |
| 39 | unsigned int mode; |
| 40 | int periph_id; |
| 41 | int valid; |
| 42 | }; |
| 43 | |
| 44 | struct tegra_spi_slave { |
| 45 | struct spi_slave slave; |
| 46 | struct tegra_spi_ctrl *ctrl; |
| 47 | }; |
| 48 | |
| 49 | static struct tegra_spi_ctrl spi_ctrls[CONFIG_TEGRA_SLINK_CTRLS]; |
| 50 | |
| 51 | static inline struct tegra_spi_slave *to_tegra_spi(struct spi_slave *slave) |
| 52 | { |
| 53 | return container_of(slave, struct tegra_spi_slave, slave); |
| 54 | } |
| 55 | |
| 56 | int spi_cs_is_valid(unsigned int bus, unsigned int cs) |
| 57 | { |
| 58 | if (bus >= CONFIG_TEGRA_SLINK_CTRLS || cs > 3 || !spi_ctrls[bus].valid) |
| 59 | return 0; |
| 60 | else |
| 61 | return 1; |
| 62 | } |
| 63 | |
| 64 | struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs, |
| 65 | unsigned int max_hz, unsigned int mode) |
| 66 | { |
| 67 | struct tegra_spi_slave *spi; |
| 68 | |
| 69 | debug("%s: bus: %u, cs: %u, max_hz: %u, mode: %u\n", __func__, |
| 70 | bus, cs, max_hz, mode); |
| 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 | |
| 78 | if (max_hz > TEGRA_SPI_MAX_FREQ) { |
| 79 | printf("SPI error: unsupported frequency %d Hz. Max frequency" |
| 80 | " is %d Hz\n", max_hz, TEGRA_SPI_MAX_FREQ); |
| 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); |
Allen Martin | ba4fb9b | 2013-01-29 13:51:28 +0000 | [diff] [blame] | 85 | if (!spi) { |
| 86 | printf("SPI error: malloc of SPI structure failed\n"); |
| 87 | return NULL; |
| 88 | } |
Allen Martin | ba4fb9b | 2013-01-29 13:51:28 +0000 | [diff] [blame] | 89 | spi->ctrl = &spi_ctrls[bus]; |
| 90 | if (!spi->ctrl) { |
| 91 | printf("SPI error: could not find controller for bus %d\n", |
| 92 | bus); |
| 93 | return NULL; |
| 94 | } |
| 95 | |
| 96 | if (max_hz < spi->ctrl->freq) { |
| 97 | debug("%s: limiting frequency from %u to %u\n", __func__, |
| 98 | spi->ctrl->freq, max_hz); |
| 99 | spi->ctrl->freq = max_hz; |
| 100 | } |
| 101 | spi->ctrl->mode = mode; |
| 102 | |
| 103 | return &spi->slave; |
| 104 | } |
| 105 | |
| 106 | void spi_free_slave(struct spi_slave *slave) |
| 107 | { |
| 108 | struct tegra_spi_slave *spi = to_tegra_spi(slave); |
| 109 | |
| 110 | free(spi); |
| 111 | } |
| 112 | |
| 113 | void spi_init(void) |
| 114 | { |
| 115 | struct tegra_spi_ctrl *ctrl; |
| 116 | int i; |
| 117 | #ifdef CONFIG_OF_CONTROL |
| 118 | int node = 0; |
| 119 | int count; |
| 120 | int node_list[CONFIG_TEGRA_SLINK_CTRLS]; |
| 121 | |
| 122 | count = fdtdec_find_aliases_for_id(gd->fdt_blob, "spi", |
| 123 | COMPAT_NVIDIA_TEGRA20_SLINK, |
| 124 | node_list, |
| 125 | CONFIG_TEGRA_SLINK_CTRLS); |
| 126 | for (i = 0; i < count; i++) { |
| 127 | ctrl = &spi_ctrls[i]; |
| 128 | node = node_list[i]; |
| 129 | |
| 130 | ctrl->regs = (struct slink_tegra *)fdtdec_get_addr(gd->fdt_blob, |
| 131 | node, "reg"); |
| 132 | if ((fdt_addr_t)ctrl->regs == FDT_ADDR_T_NONE) { |
| 133 | debug("%s: no slink register found\n", __func__); |
| 134 | continue; |
| 135 | } |
| 136 | ctrl->freq = fdtdec_get_int(gd->fdt_blob, node, |
| 137 | "spi-max-frequency", 0); |
| 138 | if (!ctrl->freq) { |
| 139 | debug("%s: no slink max frequency found\n", __func__); |
| 140 | continue; |
| 141 | } |
| 142 | |
| 143 | ctrl->periph_id = clock_decode_periph_id(gd->fdt_blob, node); |
| 144 | if (ctrl->periph_id == PERIPH_ID_NONE) { |
| 145 | debug("%s: could not decode periph id\n", __func__); |
| 146 | continue; |
| 147 | } |
| 148 | ctrl->valid = 1; |
| 149 | |
| 150 | debug("%s: found controller at %p, freq = %u, periph_id = %d\n", |
| 151 | __func__, ctrl->regs, ctrl->freq, ctrl->periph_id); |
| 152 | } |
| 153 | #else |
| 154 | for (i = 0; i < CONFIG_TEGRA_SLINK_CTRLS; i++) { |
| 155 | ctrl = &spi_ctrls[i]; |
| 156 | u32 base_regs[] = { |
| 157 | NV_PA_SLINK1_BASE, |
| 158 | NV_PA_SLINK2_BASE, |
| 159 | NV_PA_SLINK3_BASE, |
| 160 | NV_PA_SLINK4_BASE, |
| 161 | NV_PA_SLINK5_BASE, |
| 162 | NV_PA_SLINK6_BASE, |
| 163 | }; |
| 164 | int periph_ids[] = { |
| 165 | PERIPH_ID_SBC1, |
| 166 | PERIPH_ID_SBC2, |
| 167 | PERIPH_ID_SBC3, |
| 168 | PERIPH_ID_SBC4, |
| 169 | PERIPH_ID_SBC5, |
| 170 | PERIPH_ID_SBC6, |
| 171 | }; |
| 172 | ctrl->regs = (struct slink_tegra *)base_regs[i]; |
| 173 | ctrl->freq = TEGRA_SPI_MAX_FREQ; |
| 174 | ctrl->periph_id = periph_ids[i]; |
| 175 | ctrl->valid = 1; |
| 176 | |
| 177 | debug("%s: found controller at %p, freq = %u, periph_id = %d\n", |
| 178 | __func__, ctrl->regs, ctrl->freq, ctrl->periph_id); |
| 179 | } |
| 180 | #endif |
| 181 | } |
| 182 | |
| 183 | int spi_claim_bus(struct spi_slave *slave) |
| 184 | { |
| 185 | struct tegra_spi_slave *spi = to_tegra_spi(slave); |
| 186 | struct slink_tegra *regs = spi->ctrl->regs; |
| 187 | u32 reg; |
| 188 | |
| 189 | /* Change SPI clock to correct frequency, PLLP_OUT0 source */ |
| 190 | clock_start_periph_pll(spi->ctrl->periph_id, CLOCK_ID_PERIPH, |
| 191 | spi->ctrl->freq); |
| 192 | |
| 193 | /* Clear stale status here */ |
| 194 | reg = SLINK_STAT_RDY | SLINK_STAT_RXF_FLUSH | SLINK_STAT_TXF_FLUSH | \ |
| 195 | SLINK_STAT_RXF_UNR | SLINK_STAT_TXF_OVF; |
| 196 | writel(reg, ®s->status); |
| 197 | debug("%s: STATUS = %08x\n", __func__, readl(®s->status)); |
| 198 | |
| 199 | /* Set master mode and sw controlled CS */ |
| 200 | reg = readl(®s->command); |
| 201 | reg |= SLINK_CMD_M_S | SLINK_CMD_CS_SOFT; |
| 202 | writel(reg, ®s->command); |
| 203 | debug("%s: COMMAND = %08x\n", __func__, readl(®s->command)); |
| 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | void spi_release_bus(struct spi_slave *slave) |
| 209 | { |
| 210 | } |
| 211 | |
| 212 | void spi_cs_activate(struct spi_slave *slave) |
| 213 | { |
| 214 | struct tegra_spi_slave *spi = to_tegra_spi(slave); |
| 215 | struct slink_tegra *regs = spi->ctrl->regs; |
| 216 | |
| 217 | /* CS is negated on Tegra, so drive a 1 to get a 0 */ |
| 218 | setbits_le32(®s->command, SLINK_CMD_CS_VAL); |
| 219 | } |
| 220 | |
| 221 | void spi_cs_deactivate(struct spi_slave *slave) |
| 222 | { |
| 223 | struct tegra_spi_slave *spi = to_tegra_spi(slave); |
| 224 | struct slink_tegra *regs = spi->ctrl->regs; |
| 225 | |
| 226 | /* CS is negated on Tegra, so drive a 0 to get a 1 */ |
| 227 | clrbits_le32(®s->command, SLINK_CMD_CS_VAL); |
| 228 | } |
| 229 | |
| 230 | int spi_xfer(struct spi_slave *slave, unsigned int bitlen, |
| 231 | const void *data_out, void *data_in, unsigned long flags) |
| 232 | { |
| 233 | struct tegra_spi_slave *spi = to_tegra_spi(slave); |
| 234 | struct slink_tegra *regs = spi->ctrl->regs; |
| 235 | u32 reg, tmpdout, tmpdin = 0; |
| 236 | const u8 *dout = data_out; |
| 237 | u8 *din = data_in; |
| 238 | int num_bytes; |
| 239 | int ret; |
| 240 | |
| 241 | debug("%s: slave %u:%u dout %p din %p bitlen %u\n", |
| 242 | __func__, slave->bus, slave->cs, dout, din, bitlen); |
| 243 | if (bitlen % 8) |
| 244 | return -1; |
| 245 | num_bytes = bitlen / 8; |
| 246 | |
| 247 | ret = 0; |
| 248 | |
| 249 | reg = readl(®s->status); |
| 250 | writel(reg, ®s->status); /* Clear all SPI events via R/W */ |
| 251 | debug("%s entry: STATUS = %08x\n", __func__, reg); |
| 252 | |
| 253 | reg = readl(®s->status2); |
| 254 | writel(reg, ®s->status2); /* Clear all STATUS2 events via R/W */ |
| 255 | debug("%s entry: STATUS2 = %08x\n", __func__, reg); |
| 256 | |
| 257 | debug("%s entry: COMMAND = %08x\n", __func__, readl(®s->command)); |
| 258 | |
| 259 | clrsetbits_le32(®s->command2, SLINK_CMD2_SS_EN_MASK, |
| 260 | SLINK_CMD2_TXEN | SLINK_CMD2_RXEN | |
| 261 | (slave->cs << SLINK_CMD2_SS_EN_SHIFT)); |
| 262 | debug("%s entry: COMMAND2 = %08x\n", __func__, readl(®s->command2)); |
| 263 | |
| 264 | if (flags & SPI_XFER_BEGIN) |
| 265 | spi_cs_activate(slave); |
| 266 | |
| 267 | /* handle data in 32-bit chunks */ |
| 268 | while (num_bytes > 0) { |
| 269 | int bytes; |
| 270 | int is_read = 0; |
| 271 | int tm, i; |
| 272 | |
| 273 | tmpdout = 0; |
| 274 | bytes = (num_bytes > 4) ? 4 : num_bytes; |
| 275 | |
| 276 | if (dout != NULL) { |
| 277 | for (i = 0; i < bytes; ++i) |
| 278 | tmpdout = (tmpdout << 8) | dout[i]; |
| 279 | dout += bytes; |
| 280 | } |
| 281 | |
| 282 | num_bytes -= bytes; |
| 283 | |
| 284 | clrsetbits_le32(®s->command, SLINK_CMD_BIT_LENGTH_MASK, |
| 285 | bytes * 8 - 1); |
| 286 | writel(tmpdout, ®s->tx_fifo); |
| 287 | setbits_le32(®s->command, SLINK_CMD_GO); |
| 288 | |
| 289 | /* |
| 290 | * Wait for SPI transmit FIFO to empty, or to time out. |
| 291 | * The RX FIFO status will be read and cleared last |
| 292 | */ |
| 293 | for (tm = 0, is_read = 0; tm < SPI_TIMEOUT; ++tm) { |
| 294 | u32 status; |
| 295 | |
| 296 | status = readl(®s->status); |
| 297 | |
| 298 | /* We can exit when we've had both RX and TX activity */ |
| 299 | if (is_read && (status & SLINK_STAT_TXF_EMPTY)) |
| 300 | break; |
| 301 | |
| 302 | if ((status & (SLINK_STAT_BSY | SLINK_STAT_RDY)) != |
| 303 | SLINK_STAT_RDY) |
| 304 | tm++; |
| 305 | |
| 306 | else if (!(status & SLINK_STAT_RXF_EMPTY)) { |
| 307 | tmpdin = readl(®s->rx_fifo); |
| 308 | is_read = 1; |
| 309 | |
| 310 | /* swap bytes read in */ |
| 311 | if (din != NULL) { |
| 312 | for (i = bytes - 1; i >= 0; --i) { |
| 313 | din[i] = tmpdin & 0xff; |
| 314 | tmpdin >>= 8; |
| 315 | } |
| 316 | din += bytes; |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | if (tm >= SPI_TIMEOUT) |
| 322 | ret = tm; |
| 323 | |
| 324 | /* clear ACK RDY, etc. bits */ |
| 325 | writel(readl(®s->status), ®s->status); |
| 326 | } |
| 327 | |
| 328 | if (flags & SPI_XFER_END) |
| 329 | spi_cs_deactivate(slave); |
| 330 | |
| 331 | debug("%s: transfer ended. Value=%08x, status = %08x\n", |
| 332 | __func__, tmpdin, readl(®s->status)); |
| 333 | |
| 334 | if (ret) { |
| 335 | printf("%s: timeout during SPI transfer, tm %d\n", |
| 336 | __func__, ret); |
| 337 | return -1; |
| 338 | } |
| 339 | |
| 340 | return 0; |
| 341 | } |