Allen Martin | b19f574 | 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> |
Allen Martin | ff1da6f | 2013-03-16 18:58:03 +0000 | [diff] [blame] | 30 | #include <asm/arch-tegra20/tegra20_slink.h> |
Allen Martin | b19f574 | 2013-01-29 13:51:28 +0000 | [diff] [blame] | 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 | |
| 84 | spi = malloc(sizeof(struct tegra_spi_slave)); |
| 85 | if (!spi) { |
| 86 | printf("SPI error: malloc of SPI structure failed\n"); |
| 87 | return NULL; |
| 88 | } |
| 89 | spi->slave.bus = bus; |
| 90 | spi->slave.cs = cs; |
| 91 | spi->ctrl = &spi_ctrls[bus]; |
| 92 | if (!spi->ctrl) { |
| 93 | printf("SPI error: could not find controller for bus %d\n", |
| 94 | bus); |
| 95 | return NULL; |
| 96 | } |
| 97 | |
| 98 | if (max_hz < spi->ctrl->freq) { |
| 99 | debug("%s: limiting frequency from %u to %u\n", __func__, |
| 100 | spi->ctrl->freq, max_hz); |
| 101 | spi->ctrl->freq = max_hz; |
| 102 | } |
| 103 | spi->ctrl->mode = mode; |
| 104 | |
| 105 | return &spi->slave; |
| 106 | } |
| 107 | |
| 108 | void spi_free_slave(struct spi_slave *slave) |
| 109 | { |
| 110 | struct tegra_spi_slave *spi = to_tegra_spi(slave); |
| 111 | |
| 112 | free(spi); |
| 113 | } |
| 114 | |
| 115 | void spi_init(void) |
| 116 | { |
| 117 | struct tegra_spi_ctrl *ctrl; |
| 118 | int i; |
Allen Martin | b19f574 | 2013-01-29 13:51:28 +0000 | [diff] [blame] | 119 | int node = 0; |
| 120 | int count; |
| 121 | int node_list[CONFIG_TEGRA_SLINK_CTRLS]; |
| 122 | |
| 123 | count = fdtdec_find_aliases_for_id(gd->fdt_blob, "spi", |
| 124 | COMPAT_NVIDIA_TEGRA20_SLINK, |
| 125 | node_list, |
| 126 | CONFIG_TEGRA_SLINK_CTRLS); |
| 127 | for (i = 0; i < count; i++) { |
| 128 | ctrl = &spi_ctrls[i]; |
| 129 | node = node_list[i]; |
| 130 | |
| 131 | ctrl->regs = (struct slink_tegra *)fdtdec_get_addr(gd->fdt_blob, |
| 132 | node, "reg"); |
| 133 | if ((fdt_addr_t)ctrl->regs == FDT_ADDR_T_NONE) { |
| 134 | debug("%s: no slink register found\n", __func__); |
| 135 | continue; |
| 136 | } |
| 137 | ctrl->freq = fdtdec_get_int(gd->fdt_blob, node, |
| 138 | "spi-max-frequency", 0); |
| 139 | if (!ctrl->freq) { |
| 140 | debug("%s: no slink max frequency found\n", __func__); |
| 141 | continue; |
| 142 | } |
| 143 | |
| 144 | ctrl->periph_id = clock_decode_periph_id(gd->fdt_blob, node); |
| 145 | if (ctrl->periph_id == PERIPH_ID_NONE) { |
| 146 | debug("%s: could not decode periph id\n", __func__); |
| 147 | continue; |
| 148 | } |
| 149 | ctrl->valid = 1; |
| 150 | |
| 151 | debug("%s: found controller at %p, freq = %u, periph_id = %d\n", |
| 152 | __func__, ctrl->regs, ctrl->freq, ctrl->periph_id); |
| 153 | } |
Allen Martin | b19f574 | 2013-01-29 13:51:28 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | int spi_claim_bus(struct spi_slave *slave) |
| 157 | { |
| 158 | struct tegra_spi_slave *spi = to_tegra_spi(slave); |
| 159 | struct slink_tegra *regs = spi->ctrl->regs; |
| 160 | u32 reg; |
| 161 | |
| 162 | /* Change SPI clock to correct frequency, PLLP_OUT0 source */ |
| 163 | clock_start_periph_pll(spi->ctrl->periph_id, CLOCK_ID_PERIPH, |
| 164 | spi->ctrl->freq); |
| 165 | |
| 166 | /* Clear stale status here */ |
| 167 | reg = SLINK_STAT_RDY | SLINK_STAT_RXF_FLUSH | SLINK_STAT_TXF_FLUSH | \ |
| 168 | SLINK_STAT_RXF_UNR | SLINK_STAT_TXF_OVF; |
| 169 | writel(reg, ®s->status); |
| 170 | debug("%s: STATUS = %08x\n", __func__, readl(®s->status)); |
| 171 | |
| 172 | /* Set master mode and sw controlled CS */ |
| 173 | reg = readl(®s->command); |
| 174 | reg |= SLINK_CMD_M_S | SLINK_CMD_CS_SOFT; |
| 175 | writel(reg, ®s->command); |
| 176 | debug("%s: COMMAND = %08x\n", __func__, readl(®s->command)); |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | void spi_release_bus(struct spi_slave *slave) |
| 182 | { |
| 183 | } |
| 184 | |
| 185 | void spi_cs_activate(struct spi_slave *slave) |
| 186 | { |
| 187 | struct tegra_spi_slave *spi = to_tegra_spi(slave); |
| 188 | struct slink_tegra *regs = spi->ctrl->regs; |
| 189 | |
| 190 | /* CS is negated on Tegra, so drive a 1 to get a 0 */ |
| 191 | setbits_le32(®s->command, SLINK_CMD_CS_VAL); |
| 192 | } |
| 193 | |
| 194 | void spi_cs_deactivate(struct spi_slave *slave) |
| 195 | { |
| 196 | struct tegra_spi_slave *spi = to_tegra_spi(slave); |
| 197 | struct slink_tegra *regs = spi->ctrl->regs; |
| 198 | |
| 199 | /* CS is negated on Tegra, so drive a 0 to get a 1 */ |
| 200 | clrbits_le32(®s->command, SLINK_CMD_CS_VAL); |
| 201 | } |
| 202 | |
| 203 | int spi_xfer(struct spi_slave *slave, unsigned int bitlen, |
| 204 | const void *data_out, void *data_in, unsigned long flags) |
| 205 | { |
| 206 | struct tegra_spi_slave *spi = to_tegra_spi(slave); |
| 207 | struct slink_tegra *regs = spi->ctrl->regs; |
| 208 | u32 reg, tmpdout, tmpdin = 0; |
| 209 | const u8 *dout = data_out; |
| 210 | u8 *din = data_in; |
| 211 | int num_bytes; |
| 212 | int ret; |
| 213 | |
| 214 | debug("%s: slave %u:%u dout %p din %p bitlen %u\n", |
| 215 | __func__, slave->bus, slave->cs, dout, din, bitlen); |
| 216 | if (bitlen % 8) |
| 217 | return -1; |
| 218 | num_bytes = bitlen / 8; |
| 219 | |
| 220 | ret = 0; |
| 221 | |
| 222 | reg = readl(®s->status); |
| 223 | writel(reg, ®s->status); /* Clear all SPI events via R/W */ |
| 224 | debug("%s entry: STATUS = %08x\n", __func__, reg); |
| 225 | |
| 226 | reg = readl(®s->status2); |
| 227 | writel(reg, ®s->status2); /* Clear all STATUS2 events via R/W */ |
| 228 | debug("%s entry: STATUS2 = %08x\n", __func__, reg); |
| 229 | |
| 230 | debug("%s entry: COMMAND = %08x\n", __func__, readl(®s->command)); |
| 231 | |
| 232 | clrsetbits_le32(®s->command2, SLINK_CMD2_SS_EN_MASK, |
| 233 | SLINK_CMD2_TXEN | SLINK_CMD2_RXEN | |
| 234 | (slave->cs << SLINK_CMD2_SS_EN_SHIFT)); |
| 235 | debug("%s entry: COMMAND2 = %08x\n", __func__, readl(®s->command2)); |
| 236 | |
| 237 | if (flags & SPI_XFER_BEGIN) |
| 238 | spi_cs_activate(slave); |
| 239 | |
| 240 | /* handle data in 32-bit chunks */ |
| 241 | while (num_bytes > 0) { |
| 242 | int bytes; |
| 243 | int is_read = 0; |
| 244 | int tm, i; |
| 245 | |
| 246 | tmpdout = 0; |
| 247 | bytes = (num_bytes > 4) ? 4 : num_bytes; |
| 248 | |
| 249 | if (dout != NULL) { |
| 250 | for (i = 0; i < bytes; ++i) |
| 251 | tmpdout = (tmpdout << 8) | dout[i]; |
| 252 | dout += bytes; |
| 253 | } |
| 254 | |
| 255 | num_bytes -= bytes; |
| 256 | |
| 257 | clrsetbits_le32(®s->command, SLINK_CMD_BIT_LENGTH_MASK, |
| 258 | bytes * 8 - 1); |
| 259 | writel(tmpdout, ®s->tx_fifo); |
| 260 | setbits_le32(®s->command, SLINK_CMD_GO); |
| 261 | |
| 262 | /* |
| 263 | * Wait for SPI transmit FIFO to empty, or to time out. |
| 264 | * The RX FIFO status will be read and cleared last |
| 265 | */ |
| 266 | for (tm = 0, is_read = 0; tm < SPI_TIMEOUT; ++tm) { |
| 267 | u32 status; |
| 268 | |
| 269 | status = readl(®s->status); |
| 270 | |
| 271 | /* We can exit when we've had both RX and TX activity */ |
| 272 | if (is_read && (status & SLINK_STAT_TXF_EMPTY)) |
| 273 | break; |
| 274 | |
| 275 | if ((status & (SLINK_STAT_BSY | SLINK_STAT_RDY)) != |
| 276 | SLINK_STAT_RDY) |
| 277 | tm++; |
| 278 | |
| 279 | else if (!(status & SLINK_STAT_RXF_EMPTY)) { |
| 280 | tmpdin = readl(®s->rx_fifo); |
| 281 | is_read = 1; |
| 282 | |
| 283 | /* swap bytes read in */ |
| 284 | if (din != NULL) { |
| 285 | for (i = bytes - 1; i >= 0; --i) { |
| 286 | din[i] = tmpdin & 0xff; |
| 287 | tmpdin >>= 8; |
| 288 | } |
| 289 | din += bytes; |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | if (tm >= SPI_TIMEOUT) |
| 295 | ret = tm; |
| 296 | |
| 297 | /* clear ACK RDY, etc. bits */ |
| 298 | writel(readl(®s->status), ®s->status); |
| 299 | } |
| 300 | |
| 301 | if (flags & SPI_XFER_END) |
| 302 | spi_cs_deactivate(slave); |
| 303 | |
| 304 | debug("%s: transfer ended. Value=%08x, status = %08x\n", |
| 305 | __func__, tmpdin, readl(®s->status)); |
| 306 | |
| 307 | if (ret) { |
| 308 | printf("%s: timeout during SPI transfer, tm %d\n", |
| 309 | __func__, ret); |
| 310 | return -1; |
| 311 | } |
| 312 | |
| 313 | return 0; |
| 314 | } |