1 /* 2 * NVIDIA Tegra SPI controller (T114 and later) 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 <dm.h> 26 #include <asm/io.h> 27 #include <asm/arch/clock.h> 28 #include <asm/arch-tegra/clk_rst.h> 29 #include <spi.h> 30 #include <fdtdec.h> 31 #include "tegra_spi.h" 32 33 DECLARE_GLOBAL_DATA_PTR; 34 35 /* COMMAND1 */ 36 #define SPI_CMD1_GO (1 << 31) 37 #define SPI_CMD1_M_S (1 << 30) 38 #define SPI_CMD1_MODE_MASK 0x3 39 #define SPI_CMD1_MODE_SHIFT 28 40 #define SPI_CMD1_CS_SEL_MASK 0x3 41 #define SPI_CMD1_CS_SEL_SHIFT 26 42 #define SPI_CMD1_CS_POL_INACTIVE3 (1 << 25) 43 #define SPI_CMD1_CS_POL_INACTIVE2 (1 << 24) 44 #define SPI_CMD1_CS_POL_INACTIVE1 (1 << 23) 45 #define SPI_CMD1_CS_POL_INACTIVE0 (1 << 22) 46 #define SPI_CMD1_CS_SW_HW (1 << 21) 47 #define SPI_CMD1_CS_SW_VAL (1 << 20) 48 #define SPI_CMD1_IDLE_SDA_MASK 0x3 49 #define SPI_CMD1_IDLE_SDA_SHIFT 18 50 #define SPI_CMD1_BIDIR (1 << 17) 51 #define SPI_CMD1_LSBI_FE (1 << 16) 52 #define SPI_CMD1_LSBY_FE (1 << 15) 53 #define SPI_CMD1_BOTH_EN_BIT (1 << 14) 54 #define SPI_CMD1_BOTH_EN_BYTE (1 << 13) 55 #define SPI_CMD1_RX_EN (1 << 12) 56 #define SPI_CMD1_TX_EN (1 << 11) 57 #define SPI_CMD1_PACKED (1 << 5) 58 #define SPI_CMD1_BIT_LEN_MASK 0x1F 59 #define SPI_CMD1_BIT_LEN_SHIFT 0 60 61 /* COMMAND2 */ 62 #define SPI_CMD2_TX_CLK_TAP_DELAY (1 << 6) 63 #define SPI_CMD2_TX_CLK_TAP_DELAY_MASK (0x3F << 6) 64 #define SPI_CMD2_RX_CLK_TAP_DELAY (1 << 0) 65 #define SPI_CMD2_RX_CLK_TAP_DELAY_MASK (0x3F << 0) 66 67 /* TRANSFER STATUS */ 68 #define SPI_XFER_STS_RDY (1 << 30) 69 70 /* FIFO STATUS */ 71 #define SPI_FIFO_STS_CS_INACTIVE (1 << 31) 72 #define SPI_FIFO_STS_FRAME_END (1 << 30) 73 #define SPI_FIFO_STS_RX_FIFO_FLUSH (1 << 15) 74 #define SPI_FIFO_STS_TX_FIFO_FLUSH (1 << 14) 75 #define SPI_FIFO_STS_ERR (1 << 8) 76 #define SPI_FIFO_STS_TX_FIFO_OVF (1 << 7) 77 #define SPI_FIFO_STS_TX_FIFO_UNR (1 << 6) 78 #define SPI_FIFO_STS_RX_FIFO_OVF (1 << 5) 79 #define SPI_FIFO_STS_RX_FIFO_UNR (1 << 4) 80 #define SPI_FIFO_STS_TX_FIFO_FULL (1 << 3) 81 #define SPI_FIFO_STS_TX_FIFO_EMPTY (1 << 2) 82 #define SPI_FIFO_STS_RX_FIFO_FULL (1 << 1) 83 #define SPI_FIFO_STS_RX_FIFO_EMPTY (1 << 0) 84 85 #define SPI_TIMEOUT 1000 86 #define TEGRA_SPI_MAX_FREQ 52000000 87 88 struct spi_regs { 89 u32 command1; /* 000:SPI_COMMAND1 register */ 90 u32 command2; /* 004:SPI_COMMAND2 register */ 91 u32 timing1; /* 008:SPI_CS_TIM1 register */ 92 u32 timing2; /* 00c:SPI_CS_TIM2 register */ 93 u32 xfer_status;/* 010:SPI_TRANS_STATUS register */ 94 u32 fifo_status;/* 014:SPI_FIFO_STATUS register */ 95 u32 tx_data; /* 018:SPI_TX_DATA register */ 96 u32 rx_data; /* 01c:SPI_RX_DATA register */ 97 u32 dma_ctl; /* 020:SPI_DMA_CTL register */ 98 u32 dma_blk; /* 024:SPI_DMA_BLK register */ 99 u32 rsvd[56]; /* 028-107 reserved */ 100 u32 tx_fifo; /* 108:SPI_FIFO1 register */ 101 u32 rsvd2[31]; /* 10c-187 reserved */ 102 u32 rx_fifo; /* 188:SPI_FIFO2 register */ 103 u32 spare_ctl; /* 18c:SPI_SPARE_CTRL register */ 104 }; 105 106 struct tegra114_spi_priv { 107 struct spi_regs *regs; 108 unsigned int freq; 109 unsigned int mode; 110 int periph_id; 111 int valid; 112 int last_transaction_us; 113 }; 114 115 static int tegra114_spi_ofdata_to_platdata(struct udevice *bus) 116 { 117 struct tegra_spi_platdata *plat = bus->platdata; 118 const void *blob = gd->fdt_blob; 119 int node = bus->of_offset; 120 121 plat->base = fdtdec_get_addr(blob, node, "reg"); 122 plat->periph_id = clock_decode_periph_id(blob, node); 123 124 if (plat->periph_id == PERIPH_ID_NONE) { 125 debug("%s: could not decode periph id %d\n", __func__, 126 plat->periph_id); 127 return -FDT_ERR_NOTFOUND; 128 } 129 130 /* Use 500KHz as a suitable default */ 131 plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency", 132 500000); 133 plat->deactivate_delay_us = fdtdec_get_int(blob, node, 134 "spi-deactivate-delay", 0); 135 debug("%s: base=%#08lx, periph_id=%d, max-frequency=%d, deactivate_delay=%d\n", 136 __func__, plat->base, plat->periph_id, plat->frequency, 137 plat->deactivate_delay_us); 138 139 return 0; 140 } 141 142 static int tegra114_spi_probe(struct udevice *bus) 143 { 144 struct tegra_spi_platdata *plat = dev_get_platdata(bus); 145 struct tegra114_spi_priv *priv = dev_get_priv(bus); 146 147 priv->regs = (struct spi_regs *)plat->base; 148 149 priv->last_transaction_us = timer_get_us(); 150 priv->freq = plat->frequency; 151 priv->periph_id = plat->periph_id; 152 153 return 0; 154 } 155 156 static int tegra114_spi_claim_bus(struct udevice *dev) 157 { 158 struct udevice *bus = dev->parent; 159 struct tegra114_spi_priv *priv = dev_get_priv(bus); 160 struct spi_regs *regs = priv->regs; 161 162 /* Change SPI clock to correct frequency, PLLP_OUT0 source */ 163 clock_start_periph_pll(priv->periph_id, CLOCK_ID_PERIPH, priv->freq); 164 165 /* Clear stale status here */ 166 setbits_le32(®s->fifo_status, 167 SPI_FIFO_STS_ERR | 168 SPI_FIFO_STS_TX_FIFO_OVF | 169 SPI_FIFO_STS_TX_FIFO_UNR | 170 SPI_FIFO_STS_RX_FIFO_OVF | 171 SPI_FIFO_STS_RX_FIFO_UNR | 172 SPI_FIFO_STS_TX_FIFO_FULL | 173 SPI_FIFO_STS_TX_FIFO_EMPTY | 174 SPI_FIFO_STS_RX_FIFO_FULL | 175 SPI_FIFO_STS_RX_FIFO_EMPTY); 176 debug("%s: FIFO STATUS = %08x\n", __func__, readl(®s->fifo_status)); 177 178 /* Set master mode and sw controlled CS */ 179 setbits_le32(®s->command1, SPI_CMD1_M_S | SPI_CMD1_CS_SW_HW | 180 (priv->mode << SPI_CMD1_MODE_SHIFT)); 181 debug("%s: COMMAND1 = %08x\n", __func__, readl(®s->command1)); 182 183 return 0; 184 } 185 186 /** 187 * Activate the CS by driving it LOW 188 * 189 * @param slave Pointer to spi_slave to which controller has to 190 * communicate with 191 */ 192 static void spi_cs_activate(struct udevice *dev) 193 { 194 struct udevice *bus = dev->parent; 195 struct tegra_spi_platdata *pdata = dev_get_platdata(bus); 196 struct tegra114_spi_priv *priv = dev_get_priv(bus); 197 198 /* If it's too soon to do another transaction, wait */ 199 if (pdata->deactivate_delay_us && 200 priv->last_transaction_us) { 201 ulong delay_us; /* The delay completed so far */ 202 delay_us = timer_get_us() - priv->last_transaction_us; 203 if (delay_us < pdata->deactivate_delay_us) 204 udelay(pdata->deactivate_delay_us - delay_us); 205 } 206 207 clrbits_le32(&priv->regs->command1, SPI_CMD1_CS_SW_VAL); 208 } 209 210 /** 211 * Deactivate the CS by driving it HIGH 212 * 213 * @param slave Pointer to spi_slave to which controller has to 214 * communicate with 215 */ 216 static void spi_cs_deactivate(struct udevice *dev) 217 { 218 struct udevice *bus = dev->parent; 219 struct tegra_spi_platdata *pdata = dev_get_platdata(bus); 220 struct tegra114_spi_priv *priv = dev_get_priv(bus); 221 222 setbits_le32(&priv->regs->command1, SPI_CMD1_CS_SW_VAL); 223 224 /* Remember time of this transaction so we can honour the bus delay */ 225 if (pdata->deactivate_delay_us) 226 priv->last_transaction_us = timer_get_us(); 227 228 debug("Deactivate CS, bus '%s'\n", bus->name); 229 } 230 231 static int tegra114_spi_xfer(struct udevice *dev, unsigned int bitlen, 232 const void *data_out, void *data_in, 233 unsigned long flags) 234 { 235 struct udevice *bus = dev->parent; 236 struct tegra114_spi_priv *priv = dev_get_priv(bus); 237 struct spi_regs *regs = priv->regs; 238 u32 reg, tmpdout, tmpdin = 0; 239 const u8 *dout = data_out; 240 u8 *din = data_in; 241 int num_bytes; 242 int ret; 243 244 debug("%s: slave %u:%u dout %p din %p bitlen %u\n", 245 __func__, bus->seq, spi_chip_select(dev), dout, din, bitlen); 246 if (bitlen % 8) 247 return -1; 248 num_bytes = bitlen / 8; 249 250 ret = 0; 251 252 /* clear all error status bits */ 253 reg = readl(®s->fifo_status); 254 writel(reg, ®s->fifo_status); 255 256 clrsetbits_le32(®s->command1, SPI_CMD1_CS_SW_VAL, 257 SPI_CMD1_RX_EN | SPI_CMD1_TX_EN | SPI_CMD1_LSBY_FE | 258 (spi_chip_select(dev) << SPI_CMD1_CS_SEL_SHIFT)); 259 260 /* set xfer size to 1 block (32 bits) */ 261 writel(0, ®s->dma_blk); 262 263 if (flags & SPI_XFER_BEGIN) 264 spi_cs_activate(dev); 265 266 /* handle data in 32-bit chunks */ 267 while (num_bytes > 0) { 268 int bytes; 269 int tm, i; 270 271 tmpdout = 0; 272 bytes = (num_bytes > 4) ? 4 : num_bytes; 273 274 if (dout != NULL) { 275 for (i = 0; i < bytes; ++i) 276 tmpdout = (tmpdout << 8) | dout[i]; 277 dout += bytes; 278 } 279 280 num_bytes -= bytes; 281 282 /* clear ready bit */ 283 setbits_le32(®s->xfer_status, SPI_XFER_STS_RDY); 284 285 clrsetbits_le32(®s->command1, 286 SPI_CMD1_BIT_LEN_MASK << SPI_CMD1_BIT_LEN_SHIFT, 287 (bytes * 8 - 1) << SPI_CMD1_BIT_LEN_SHIFT); 288 writel(tmpdout, ®s->tx_fifo); 289 setbits_le32(®s->command1, SPI_CMD1_GO); 290 291 /* 292 * Wait for SPI transmit FIFO to empty, or to time out. 293 * The RX FIFO status will be read and cleared last 294 */ 295 for (tm = 0; tm < SPI_TIMEOUT; ++tm) { 296 u32 fifo_status, xfer_status; 297 298 xfer_status = readl(®s->xfer_status); 299 if (!(xfer_status & SPI_XFER_STS_RDY)) 300 continue; 301 302 fifo_status = readl(®s->fifo_status); 303 if (fifo_status & SPI_FIFO_STS_ERR) { 304 debug("%s: got a fifo error: ", __func__); 305 if (fifo_status & SPI_FIFO_STS_TX_FIFO_OVF) 306 debug("tx FIFO overflow "); 307 if (fifo_status & SPI_FIFO_STS_TX_FIFO_UNR) 308 debug("tx FIFO underrun "); 309 if (fifo_status & SPI_FIFO_STS_RX_FIFO_OVF) 310 debug("rx FIFO overflow "); 311 if (fifo_status & SPI_FIFO_STS_RX_FIFO_UNR) 312 debug("rx FIFO underrun "); 313 if (fifo_status & SPI_FIFO_STS_TX_FIFO_FULL) 314 debug("tx FIFO full "); 315 if (fifo_status & SPI_FIFO_STS_TX_FIFO_EMPTY) 316 debug("tx FIFO empty "); 317 if (fifo_status & SPI_FIFO_STS_RX_FIFO_FULL) 318 debug("rx FIFO full "); 319 if (fifo_status & SPI_FIFO_STS_RX_FIFO_EMPTY) 320 debug("rx FIFO empty "); 321 debug("\n"); 322 break; 323 } 324 325 if (!(fifo_status & SPI_FIFO_STS_RX_FIFO_EMPTY)) { 326 tmpdin = readl(®s->rx_fifo); 327 328 /* swap bytes read in */ 329 if (din != NULL) { 330 for (i = bytes - 1; i >= 0; --i) { 331 din[i] = tmpdin & 0xff; 332 tmpdin >>= 8; 333 } 334 din += bytes; 335 } 336 337 /* We can exit when we've had both RX and TX */ 338 break; 339 } 340 } 341 342 if (tm >= SPI_TIMEOUT) 343 ret = tm; 344 345 /* clear ACK RDY, etc. bits */ 346 writel(readl(®s->fifo_status), ®s->fifo_status); 347 } 348 349 if (flags & SPI_XFER_END) 350 spi_cs_deactivate(dev); 351 352 debug("%s: transfer ended. Value=%08x, fifo_status = %08x\n", 353 __func__, tmpdin, readl(®s->fifo_status)); 354 355 if (ret) { 356 printf("%s: timeout during SPI transfer, tm %d\n", 357 __func__, ret); 358 return -1; 359 } 360 361 return ret; 362 } 363 364 static int tegra114_spi_set_speed(struct udevice *bus, uint speed) 365 { 366 struct tegra_spi_platdata *plat = bus->platdata; 367 struct tegra114_spi_priv *priv = dev_get_priv(bus); 368 369 if (speed > plat->frequency) 370 speed = plat->frequency; 371 priv->freq = speed; 372 debug("%s: regs=%p, speed=%d\n", __func__, priv->regs, priv->freq); 373 374 return 0; 375 } 376 377 static int tegra114_spi_set_mode(struct udevice *bus, uint mode) 378 { 379 struct tegra114_spi_priv *priv = dev_get_priv(bus); 380 381 priv->mode = mode; 382 debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode); 383 384 return 0; 385 } 386 387 static const struct dm_spi_ops tegra114_spi_ops = { 388 .claim_bus = tegra114_spi_claim_bus, 389 .xfer = tegra114_spi_xfer, 390 .set_speed = tegra114_spi_set_speed, 391 .set_mode = tegra114_spi_set_mode, 392 /* 393 * cs_info is not needed, since we require all chip selects to be 394 * in the device tree explicitly 395 */ 396 }; 397 398 static const struct udevice_id tegra114_spi_ids[] = { 399 { .compatible = "nvidia,tegra114-spi" }, 400 { } 401 }; 402 403 U_BOOT_DRIVER(tegra114_spi) = { 404 .name = "tegra114_spi", 405 .id = UCLASS_SPI, 406 .of_match = tegra114_spi_ids, 407 .ops = &tegra114_spi_ops, 408 .ofdata_to_platdata = tegra114_spi_ofdata_to_platdata, 409 .platdata_auto_alloc_size = sizeof(struct tegra_spi_platdata), 410 .priv_auto_alloc_size = sizeof(struct tegra114_spi_priv), 411 .probe = tegra114_spi_probe, 412 }; 413