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> |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 25 | #include <dm.h> |
Allen Martin | b19f574 | 2013-01-29 13:51:28 +0000 | [diff] [blame] | 26 | #include <asm/io.h> |
Allen Martin | b19f574 | 2013-01-29 13:51:28 +0000 | [diff] [blame] | 27 | #include <asm/arch/clock.h> |
| 28 | #include <asm/arch-tegra/clk_rst.h> |
Allen Martin | b19f574 | 2013-01-29 13:51:28 +0000 | [diff] [blame] | 29 | #include <spi.h> |
| 30 | #include <fdtdec.h> |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 31 | #include "tegra_spi.h" |
Allen Martin | b19f574 | 2013-01-29 13:51:28 +0000 | [diff] [blame] | 32 | |
| 33 | DECLARE_GLOBAL_DATA_PTR; |
| 34 | |
Allen Martin | 7a49ba6 | 2013-03-16 18:58:05 +0000 | [diff] [blame] | 35 | /* COMMAND */ |
| 36 | #define SLINK_CMD_ENB (1 << 31) |
| 37 | #define SLINK_CMD_GO (1 << 30) |
| 38 | #define SLINK_CMD_M_S (1 << 28) |
| 39 | #define SLINK_CMD_CK_SDA (1 << 21) |
| 40 | #define SLINK_CMD_CS_POL (1 << 13) |
| 41 | #define SLINK_CMD_CS_VAL (1 << 12) |
| 42 | #define SLINK_CMD_CS_SOFT (1 << 11) |
| 43 | #define SLINK_CMD_BIT_LENGTH (1 << 4) |
| 44 | #define SLINK_CMD_BIT_LENGTH_MASK 0x0000001F |
| 45 | /* COMMAND2 */ |
| 46 | #define SLINK_CMD2_TXEN (1 << 30) |
| 47 | #define SLINK_CMD2_RXEN (1 << 31) |
| 48 | #define SLINK_CMD2_SS_EN (1 << 18) |
| 49 | #define SLINK_CMD2_SS_EN_SHIFT 18 |
| 50 | #define SLINK_CMD2_SS_EN_MASK 0x000C0000 |
| 51 | #define SLINK_CMD2_CS_ACTIVE_BETWEEN (1 << 17) |
| 52 | /* STATUS */ |
| 53 | #define SLINK_STAT_BSY (1 << 31) |
| 54 | #define SLINK_STAT_RDY (1 << 30) |
| 55 | #define SLINK_STAT_ERR (1 << 29) |
| 56 | #define SLINK_STAT_RXF_FLUSH (1 << 27) |
| 57 | #define SLINK_STAT_TXF_FLUSH (1 << 26) |
| 58 | #define SLINK_STAT_RXF_OVF (1 << 25) |
| 59 | #define SLINK_STAT_TXF_UNR (1 << 24) |
| 60 | #define SLINK_STAT_RXF_EMPTY (1 << 23) |
| 61 | #define SLINK_STAT_RXF_FULL (1 << 22) |
| 62 | #define SLINK_STAT_TXF_EMPTY (1 << 21) |
| 63 | #define SLINK_STAT_TXF_FULL (1 << 20) |
| 64 | #define SLINK_STAT_TXF_OVF (1 << 19) |
| 65 | #define SLINK_STAT_RXF_UNR (1 << 18) |
| 66 | #define SLINK_STAT_CUR_BLKCNT (1 << 15) |
| 67 | /* STATUS2 */ |
| 68 | #define SLINK_STAT2_RXF_FULL_CNT (1 << 16) |
| 69 | #define SLINK_STAT2_TXF_FULL_CNT (1 << 0) |
| 70 | |
| 71 | #define SPI_TIMEOUT 1000 |
| 72 | #define TEGRA_SPI_MAX_FREQ 52000000 |
| 73 | |
| 74 | struct spi_regs { |
| 75 | u32 command; /* SLINK_COMMAND_0 register */ |
| 76 | u32 command2; /* SLINK_COMMAND2_0 reg */ |
| 77 | u32 status; /* SLINK_STATUS_0 register */ |
| 78 | u32 reserved; /* Reserved offset 0C */ |
| 79 | u32 mas_data; /* SLINK_MAS_DATA_0 reg */ |
| 80 | u32 slav_data; /* SLINK_SLAVE_DATA_0 reg */ |
| 81 | u32 dma_ctl; /* SLINK_DMA_CTL_0 register */ |
| 82 | u32 status2; /* SLINK_STATUS2_0 reg */ |
| 83 | u32 rsvd[56]; /* 0x20 to 0xFF reserved */ |
| 84 | u32 tx_fifo; /* SLINK_TX_FIFO_0 reg off 100h */ |
| 85 | u32 rsvd2[31]; /* 0x104 to 0x17F reserved */ |
| 86 | u32 rx_fifo; /* SLINK_RX_FIFO_0 reg off 180h */ |
| 87 | }; |
| 88 | |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 89 | struct tegra30_spi_priv { |
Allen Martin | 7a49ba6 | 2013-03-16 18:58:05 +0000 | [diff] [blame] | 90 | struct spi_regs *regs; |
Allen Martin | b19f574 | 2013-01-29 13:51:28 +0000 | [diff] [blame] | 91 | unsigned int freq; |
| 92 | unsigned int mode; |
| 93 | int periph_id; |
| 94 | int valid; |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 95 | int last_transaction_us; |
Allen Martin | b19f574 | 2013-01-29 13:51:28 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | struct tegra_spi_slave { |
| 99 | struct spi_slave slave; |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 100 | struct tegra30_spi_priv *ctrl; |
Allen Martin | b19f574 | 2013-01-29 13:51:28 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 103 | static int tegra30_spi_ofdata_to_platdata(struct udevice *bus) |
Allen Martin | b19f574 | 2013-01-29 13:51:28 +0000 | [diff] [blame] | 104 | { |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 105 | struct tegra_spi_platdata *plat = bus->platdata; |
| 106 | const void *blob = gd->fdt_blob; |
| 107 | int node = bus->of_offset; |
| 108 | |
| 109 | plat->base = fdtdec_get_addr(blob, node, "reg"); |
| 110 | plat->periph_id = clock_decode_periph_id(blob, node); |
| 111 | |
| 112 | if (plat->periph_id == PERIPH_ID_NONE) { |
| 113 | debug("%s: could not decode periph id %d\n", __func__, |
| 114 | plat->periph_id); |
| 115 | return -FDT_ERR_NOTFOUND; |
| 116 | } |
| 117 | |
| 118 | /* Use 500KHz as a suitable default */ |
| 119 | plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency", |
| 120 | 500000); |
| 121 | plat->deactivate_delay_us = fdtdec_get_int(blob, node, |
| 122 | "spi-deactivate-delay", 0); |
| 123 | debug("%s: base=%#08lx, periph_id=%d, max-frequency=%d, deactivate_delay=%d\n", |
| 124 | __func__, plat->base, plat->periph_id, plat->frequency, |
| 125 | plat->deactivate_delay_us); |
| 126 | |
| 127 | return 0; |
Allen Martin | b19f574 | 2013-01-29 13:51:28 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 130 | static int tegra30_spi_probe(struct udevice *bus) |
Allen Martin | b19f574 | 2013-01-29 13:51:28 +0000 | [diff] [blame] | 131 | { |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 132 | struct tegra_spi_platdata *plat = dev_get_platdata(bus); |
| 133 | struct tegra30_spi_priv *priv = dev_get_priv(bus); |
| 134 | |
| 135 | priv->regs = (struct spi_regs *)plat->base; |
| 136 | |
| 137 | priv->last_transaction_us = timer_get_us(); |
| 138 | priv->freq = plat->frequency; |
| 139 | priv->periph_id = plat->periph_id; |
| 140 | |
| 141 | return 0; |
Allen Martin | b19f574 | 2013-01-29 13:51:28 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Simon Glass | 9694b72 | 2015-04-19 09:05:40 -0600 | [diff] [blame] | 144 | static int tegra30_spi_claim_bus(struct udevice *dev) |
Allen Martin | b19f574 | 2013-01-29 13:51:28 +0000 | [diff] [blame] | 145 | { |
Simon Glass | 9694b72 | 2015-04-19 09:05:40 -0600 | [diff] [blame] | 146 | struct udevice *bus = dev->parent; |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 147 | struct tegra30_spi_priv *priv = dev_get_priv(bus); |
| 148 | struct spi_regs *regs = priv->regs; |
Allen Martin | b19f574 | 2013-01-29 13:51:28 +0000 | [diff] [blame] | 149 | u32 reg; |
| 150 | |
| 151 | /* Change SPI clock to correct frequency, PLLP_OUT0 source */ |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 152 | clock_start_periph_pll(priv->periph_id, CLOCK_ID_PERIPH, |
| 153 | priv->freq); |
Allen Martin | b19f574 | 2013-01-29 13:51:28 +0000 | [diff] [blame] | 154 | |
| 155 | /* Clear stale status here */ |
| 156 | reg = SLINK_STAT_RDY | SLINK_STAT_RXF_FLUSH | SLINK_STAT_TXF_FLUSH | \ |
| 157 | SLINK_STAT_RXF_UNR | SLINK_STAT_TXF_OVF; |
| 158 | writel(reg, ®s->status); |
| 159 | debug("%s: STATUS = %08x\n", __func__, readl(®s->status)); |
| 160 | |
| 161 | /* Set master mode and sw controlled CS */ |
| 162 | reg = readl(®s->command); |
| 163 | reg |= SLINK_CMD_M_S | SLINK_CMD_CS_SOFT; |
| 164 | writel(reg, ®s->command); |
| 165 | debug("%s: COMMAND = %08x\n", __func__, readl(®s->command)); |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 170 | static void spi_cs_activate(struct udevice *dev) |
Allen Martin | b19f574 | 2013-01-29 13:51:28 +0000 | [diff] [blame] | 171 | { |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 172 | struct udevice *bus = dev->parent; |
| 173 | struct tegra_spi_platdata *pdata = dev_get_platdata(bus); |
| 174 | struct tegra30_spi_priv *priv = dev_get_priv(bus); |
| 175 | |
| 176 | /* If it's too soon to do another transaction, wait */ |
| 177 | if (pdata->deactivate_delay_us && |
| 178 | priv->last_transaction_us) { |
| 179 | ulong delay_us; /* The delay completed so far */ |
| 180 | delay_us = timer_get_us() - priv->last_transaction_us; |
| 181 | if (delay_us < pdata->deactivate_delay_us) |
| 182 | udelay(pdata->deactivate_delay_us - delay_us); |
| 183 | } |
Allen Martin | b19f574 | 2013-01-29 13:51:28 +0000 | [diff] [blame] | 184 | |
| 185 | /* CS is negated on Tegra, so drive a 1 to get a 0 */ |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 186 | setbits_le32(&priv->regs->command, SLINK_CMD_CS_VAL); |
Allen Martin | b19f574 | 2013-01-29 13:51:28 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 189 | static void spi_cs_deactivate(struct udevice *dev) |
Allen Martin | b19f574 | 2013-01-29 13:51:28 +0000 | [diff] [blame] | 190 | { |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 191 | struct udevice *bus = dev->parent; |
| 192 | struct tegra_spi_platdata *pdata = dev_get_platdata(bus); |
| 193 | struct tegra30_spi_priv *priv = dev_get_priv(bus); |
Allen Martin | b19f574 | 2013-01-29 13:51:28 +0000 | [diff] [blame] | 194 | |
| 195 | /* CS is negated on Tegra, so drive a 0 to get a 1 */ |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 196 | clrbits_le32(&priv->regs->command, SLINK_CMD_CS_VAL); |
| 197 | |
| 198 | /* Remember time of this transaction so we can honour the bus delay */ |
| 199 | if (pdata->deactivate_delay_us) |
| 200 | priv->last_transaction_us = timer_get_us(); |
Allen Martin | b19f574 | 2013-01-29 13:51:28 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 203 | static int tegra30_spi_xfer(struct udevice *dev, unsigned int bitlen, |
| 204 | const void *data_out, void *data_in, |
| 205 | unsigned long flags) |
Allen Martin | b19f574 | 2013-01-29 13:51:28 +0000 | [diff] [blame] | 206 | { |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 207 | struct udevice *bus = dev->parent; |
| 208 | struct tegra30_spi_priv *priv = dev_get_priv(bus); |
| 209 | struct spi_regs *regs = priv->regs; |
Allen Martin | b19f574 | 2013-01-29 13:51:28 +0000 | [diff] [blame] | 210 | u32 reg, tmpdout, tmpdin = 0; |
| 211 | const u8 *dout = data_out; |
| 212 | u8 *din = data_in; |
| 213 | int num_bytes; |
| 214 | int ret; |
| 215 | |
| 216 | debug("%s: slave %u:%u dout %p din %p bitlen %u\n", |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 217 | __func__, bus->seq, spi_chip_select(dev), dout, din, bitlen); |
Allen Martin | b19f574 | 2013-01-29 13:51:28 +0000 | [diff] [blame] | 218 | if (bitlen % 8) |
| 219 | return -1; |
| 220 | num_bytes = bitlen / 8; |
| 221 | |
| 222 | ret = 0; |
| 223 | |
| 224 | reg = readl(®s->status); |
| 225 | writel(reg, ®s->status); /* Clear all SPI events via R/W */ |
| 226 | debug("%s entry: STATUS = %08x\n", __func__, reg); |
| 227 | |
| 228 | reg = readl(®s->status2); |
| 229 | writel(reg, ®s->status2); /* Clear all STATUS2 events via R/W */ |
| 230 | debug("%s entry: STATUS2 = %08x\n", __func__, reg); |
| 231 | |
| 232 | debug("%s entry: COMMAND = %08x\n", __func__, readl(®s->command)); |
| 233 | |
| 234 | clrsetbits_le32(®s->command2, SLINK_CMD2_SS_EN_MASK, |
| 235 | SLINK_CMD2_TXEN | SLINK_CMD2_RXEN | |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 236 | (spi_chip_select(dev) << SLINK_CMD2_SS_EN_SHIFT)); |
Allen Martin | b19f574 | 2013-01-29 13:51:28 +0000 | [diff] [blame] | 237 | debug("%s entry: COMMAND2 = %08x\n", __func__, readl(®s->command2)); |
| 238 | |
| 239 | if (flags & SPI_XFER_BEGIN) |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 240 | spi_cs_activate(dev); |
Allen Martin | b19f574 | 2013-01-29 13:51:28 +0000 | [diff] [blame] | 241 | |
| 242 | /* handle data in 32-bit chunks */ |
| 243 | while (num_bytes > 0) { |
| 244 | int bytes; |
| 245 | int is_read = 0; |
| 246 | int tm, i; |
| 247 | |
| 248 | tmpdout = 0; |
| 249 | bytes = (num_bytes > 4) ? 4 : num_bytes; |
| 250 | |
| 251 | if (dout != NULL) { |
| 252 | for (i = 0; i < bytes; ++i) |
| 253 | tmpdout = (tmpdout << 8) | dout[i]; |
| 254 | dout += bytes; |
| 255 | } |
| 256 | |
| 257 | num_bytes -= bytes; |
| 258 | |
| 259 | clrsetbits_le32(®s->command, SLINK_CMD_BIT_LENGTH_MASK, |
| 260 | bytes * 8 - 1); |
| 261 | writel(tmpdout, ®s->tx_fifo); |
| 262 | setbits_le32(®s->command, SLINK_CMD_GO); |
| 263 | |
| 264 | /* |
| 265 | * Wait for SPI transmit FIFO to empty, or to time out. |
| 266 | * The RX FIFO status will be read and cleared last |
| 267 | */ |
| 268 | for (tm = 0, is_read = 0; tm < SPI_TIMEOUT; ++tm) { |
| 269 | u32 status; |
| 270 | |
| 271 | status = readl(®s->status); |
| 272 | |
| 273 | /* We can exit when we've had both RX and TX activity */ |
| 274 | if (is_read && (status & SLINK_STAT_TXF_EMPTY)) |
| 275 | break; |
| 276 | |
| 277 | if ((status & (SLINK_STAT_BSY | SLINK_STAT_RDY)) != |
| 278 | SLINK_STAT_RDY) |
| 279 | tm++; |
| 280 | |
| 281 | else if (!(status & SLINK_STAT_RXF_EMPTY)) { |
| 282 | tmpdin = readl(®s->rx_fifo); |
| 283 | is_read = 1; |
| 284 | |
| 285 | /* swap bytes read in */ |
| 286 | if (din != NULL) { |
| 287 | for (i = bytes - 1; i >= 0; --i) { |
| 288 | din[i] = tmpdin & 0xff; |
| 289 | tmpdin >>= 8; |
| 290 | } |
| 291 | din += bytes; |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | if (tm >= SPI_TIMEOUT) |
| 297 | ret = tm; |
| 298 | |
| 299 | /* clear ACK RDY, etc. bits */ |
| 300 | writel(readl(®s->status), ®s->status); |
| 301 | } |
| 302 | |
| 303 | if (flags & SPI_XFER_END) |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 304 | spi_cs_deactivate(dev); |
Allen Martin | b19f574 | 2013-01-29 13:51:28 +0000 | [diff] [blame] | 305 | |
| 306 | debug("%s: transfer ended. Value=%08x, status = %08x\n", |
| 307 | __func__, tmpdin, readl(®s->status)); |
| 308 | |
| 309 | if (ret) { |
| 310 | printf("%s: timeout during SPI transfer, tm %d\n", |
| 311 | __func__, ret); |
| 312 | return -1; |
| 313 | } |
| 314 | |
| 315 | return 0; |
| 316 | } |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 317 | |
| 318 | static int tegra30_spi_set_speed(struct udevice *bus, uint speed) |
| 319 | { |
| 320 | struct tegra_spi_platdata *plat = bus->platdata; |
| 321 | struct tegra30_spi_priv *priv = dev_get_priv(bus); |
| 322 | |
| 323 | if (speed > plat->frequency) |
| 324 | speed = plat->frequency; |
| 325 | priv->freq = speed; |
| 326 | debug("%s: regs=%p, speed=%d\n", __func__, priv->regs, priv->freq); |
| 327 | |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | static int tegra30_spi_set_mode(struct udevice *bus, uint mode) |
| 332 | { |
| 333 | struct tegra30_spi_priv *priv = dev_get_priv(bus); |
| 334 | |
| 335 | priv->mode = mode; |
| 336 | debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode); |
| 337 | |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | static const struct dm_spi_ops tegra30_spi_ops = { |
| 342 | .claim_bus = tegra30_spi_claim_bus, |
| 343 | .xfer = tegra30_spi_xfer, |
| 344 | .set_speed = tegra30_spi_set_speed, |
| 345 | .set_mode = tegra30_spi_set_mode, |
| 346 | /* |
| 347 | * cs_info is not needed, since we require all chip selects to be |
| 348 | * in the device tree explicitly |
| 349 | */ |
| 350 | }; |
| 351 | |
| 352 | static const struct udevice_id tegra30_spi_ids[] = { |
| 353 | { .compatible = "nvidia,tegra20-slink" }, |
| 354 | { } |
| 355 | }; |
| 356 | |
| 357 | U_BOOT_DRIVER(tegra30_spi) = { |
| 358 | .name = "tegra20_slink", |
| 359 | .id = UCLASS_SPI, |
| 360 | .of_match = tegra30_spi_ids, |
| 361 | .ops = &tegra30_spi_ops, |
| 362 | .ofdata_to_platdata = tegra30_spi_ofdata_to_platdata, |
| 363 | .platdata_auto_alloc_size = sizeof(struct tegra_spi_platdata), |
| 364 | .priv_auto_alloc_size = sizeof(struct tegra30_spi_priv), |
Simon Glass | fda6fac | 2014-10-13 23:42:13 -0600 | [diff] [blame] | 365 | .probe = tegra30_spi_probe, |
| 366 | }; |