0001
0002
0003
0004
0005
0006
0007 #include <linux/interrupt.h>
0008 #include <linux/io.h>
0009 #include <linux/gpio/consumer.h>
0010 #include <linux/module.h>
0011 #include <linux/delay.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/err.h>
0014 #include <linux/clk.h>
0015 #include <linux/dmaengine.h>
0016 #include <linux/dma-mapping.h>
0017 #include <linux/of.h>
0018 #include <linux/of_device.h>
0019 #include <linux/spi/spi.h>
0020 #include <linux/spi/spi_bitbang.h>
0021 #include <linux/slab.h>
0022
0023 #include <linux/platform_data/spi-davinci.h>
0024
0025 #define CS_DEFAULT 0xFF
0026
0027 #define SPIFMT_PHASE_MASK BIT(16)
0028 #define SPIFMT_POLARITY_MASK BIT(17)
0029 #define SPIFMT_DISTIMER_MASK BIT(18)
0030 #define SPIFMT_SHIFTDIR_MASK BIT(20)
0031 #define SPIFMT_WAITENA_MASK BIT(21)
0032 #define SPIFMT_PARITYENA_MASK BIT(22)
0033 #define SPIFMT_ODD_PARITY_MASK BIT(23)
0034 #define SPIFMT_WDELAY_MASK 0x3f000000u
0035 #define SPIFMT_WDELAY_SHIFT 24
0036 #define SPIFMT_PRESCALE_SHIFT 8
0037
0038
0039 #define SPIPC0_DIFUN_MASK BIT(11)
0040 #define SPIPC0_DOFUN_MASK BIT(10)
0041 #define SPIPC0_CLKFUN_MASK BIT(9)
0042 #define SPIPC0_SPIENA_MASK BIT(8)
0043
0044 #define SPIINT_MASKALL 0x0101035F
0045 #define SPIINT_MASKINT 0x0000015F
0046 #define SPI_INTLVL_1 0x000001FF
0047 #define SPI_INTLVL_0 0x00000000
0048
0049
0050 #define SPIDAT1_CSHOLD_MASK BIT(12)
0051 #define SPIDAT1_WDEL BIT(10)
0052
0053
0054 #define SPIGCR1_CLKMOD_MASK BIT(1)
0055 #define SPIGCR1_MASTER_MASK BIT(0)
0056 #define SPIGCR1_POWERDOWN_MASK BIT(8)
0057 #define SPIGCR1_LOOPBACK_MASK BIT(16)
0058 #define SPIGCR1_SPIENA_MASK BIT(24)
0059
0060
0061 #define SPIBUF_TXFULL_MASK BIT(29)
0062 #define SPIBUF_RXEMPTY_MASK BIT(31)
0063
0064
0065 #define SPIDELAY_C2TDELAY_SHIFT 24
0066 #define SPIDELAY_C2TDELAY_MASK (0xFF << SPIDELAY_C2TDELAY_SHIFT)
0067 #define SPIDELAY_T2CDELAY_SHIFT 16
0068 #define SPIDELAY_T2CDELAY_MASK (0xFF << SPIDELAY_T2CDELAY_SHIFT)
0069 #define SPIDELAY_T2EDELAY_SHIFT 8
0070 #define SPIDELAY_T2EDELAY_MASK (0xFF << SPIDELAY_T2EDELAY_SHIFT)
0071 #define SPIDELAY_C2EDELAY_SHIFT 0
0072 #define SPIDELAY_C2EDELAY_MASK 0xFF
0073
0074
0075 #define SPIFLG_DLEN_ERR_MASK BIT(0)
0076 #define SPIFLG_TIMEOUT_MASK BIT(1)
0077 #define SPIFLG_PARERR_MASK BIT(2)
0078 #define SPIFLG_DESYNC_MASK BIT(3)
0079 #define SPIFLG_BITERR_MASK BIT(4)
0080 #define SPIFLG_OVRRUN_MASK BIT(6)
0081 #define SPIFLG_BUF_INIT_ACTIVE_MASK BIT(24)
0082 #define SPIFLG_ERROR_MASK (SPIFLG_DLEN_ERR_MASK \
0083 | SPIFLG_TIMEOUT_MASK | SPIFLG_PARERR_MASK \
0084 | SPIFLG_DESYNC_MASK | SPIFLG_BITERR_MASK \
0085 | SPIFLG_OVRRUN_MASK)
0086
0087 #define SPIINT_DMA_REQ_EN BIT(16)
0088
0089
0090 #define SPIGCR0 0x00
0091 #define SPIGCR1 0x04
0092 #define SPIINT 0x08
0093 #define SPILVL 0x0c
0094 #define SPIFLG 0x10
0095 #define SPIPC0 0x14
0096 #define SPIDAT1 0x3c
0097 #define SPIBUF 0x40
0098 #define SPIDELAY 0x48
0099 #define SPIDEF 0x4c
0100 #define SPIFMT0 0x50
0101
0102 #define DMA_MIN_BYTES 16
0103
0104
0105 struct davinci_spi {
0106 struct spi_bitbang bitbang;
0107 struct clk *clk;
0108
0109 u8 version;
0110 resource_size_t pbase;
0111 void __iomem *base;
0112 u32 irq;
0113 struct completion done;
0114
0115 const void *tx;
0116 void *rx;
0117 int rcount;
0118 int wcount;
0119
0120 struct dma_chan *dma_rx;
0121 struct dma_chan *dma_tx;
0122
0123 struct davinci_spi_platform_data pdata;
0124
0125 void (*get_rx)(u32 rx_data, struct davinci_spi *);
0126 u32 (*get_tx)(struct davinci_spi *);
0127
0128 u8 *bytes_per_word;
0129
0130 u8 prescaler_limit;
0131 };
0132
0133 static struct davinci_spi_config davinci_spi_default_cfg;
0134
0135 static void davinci_spi_rx_buf_u8(u32 data, struct davinci_spi *dspi)
0136 {
0137 if (dspi->rx) {
0138 u8 *rx = dspi->rx;
0139 *rx++ = (u8)data;
0140 dspi->rx = rx;
0141 }
0142 }
0143
0144 static void davinci_spi_rx_buf_u16(u32 data, struct davinci_spi *dspi)
0145 {
0146 if (dspi->rx) {
0147 u16 *rx = dspi->rx;
0148 *rx++ = (u16)data;
0149 dspi->rx = rx;
0150 }
0151 }
0152
0153 static u32 davinci_spi_tx_buf_u8(struct davinci_spi *dspi)
0154 {
0155 u32 data = 0;
0156
0157 if (dspi->tx) {
0158 const u8 *tx = dspi->tx;
0159
0160 data = *tx++;
0161 dspi->tx = tx;
0162 }
0163 return data;
0164 }
0165
0166 static u32 davinci_spi_tx_buf_u16(struct davinci_spi *dspi)
0167 {
0168 u32 data = 0;
0169
0170 if (dspi->tx) {
0171 const u16 *tx = dspi->tx;
0172
0173 data = *tx++;
0174 dspi->tx = tx;
0175 }
0176 return data;
0177 }
0178
0179 static inline void set_io_bits(void __iomem *addr, u32 bits)
0180 {
0181 u32 v = ioread32(addr);
0182
0183 v |= bits;
0184 iowrite32(v, addr);
0185 }
0186
0187 static inline void clear_io_bits(void __iomem *addr, u32 bits)
0188 {
0189 u32 v = ioread32(addr);
0190
0191 v &= ~bits;
0192 iowrite32(v, addr);
0193 }
0194
0195
0196
0197
0198 static void davinci_spi_chipselect(struct spi_device *spi, int value)
0199 {
0200 struct davinci_spi *dspi;
0201 struct davinci_spi_config *spicfg = spi->controller_data;
0202 u8 chip_sel = spi->chip_select;
0203 u16 spidat1 = CS_DEFAULT;
0204
0205 dspi = spi_master_get_devdata(spi->master);
0206
0207
0208 if (spicfg && spicfg->wdelay)
0209 spidat1 |= SPIDAT1_WDEL;
0210
0211
0212
0213
0214
0215 if (spi->cs_gpiod) {
0216 if (value == BITBANG_CS_ACTIVE)
0217 gpiod_set_value(spi->cs_gpiod, 1);
0218 else
0219 gpiod_set_value(spi->cs_gpiod, 0);
0220 } else {
0221 if (value == BITBANG_CS_ACTIVE) {
0222 if (!(spi->mode & SPI_CS_WORD))
0223 spidat1 |= SPIDAT1_CSHOLD_MASK;
0224 spidat1 &= ~(0x1 << chip_sel);
0225 }
0226 }
0227
0228 iowrite16(spidat1, dspi->base + SPIDAT1 + 2);
0229 }
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242 static inline int davinci_spi_get_prescale(struct davinci_spi *dspi,
0243 u32 max_speed_hz)
0244 {
0245 int ret;
0246
0247
0248 ret = DIV_ROUND_UP(clk_get_rate(dspi->clk), max_speed_hz) - 1;
0249
0250 if (ret < dspi->prescaler_limit || ret > 255)
0251 return -EINVAL;
0252
0253 return ret;
0254 }
0255
0256
0257
0258
0259
0260
0261
0262
0263
0264
0265 static int davinci_spi_setup_transfer(struct spi_device *spi,
0266 struct spi_transfer *t)
0267 {
0268
0269 struct davinci_spi *dspi;
0270 struct davinci_spi_config *spicfg;
0271 u8 bits_per_word = 0;
0272 u32 hz = 0, spifmt = 0;
0273 int prescale;
0274
0275 dspi = spi_master_get_devdata(spi->master);
0276 spicfg = spi->controller_data;
0277 if (!spicfg)
0278 spicfg = &davinci_spi_default_cfg;
0279
0280 if (t) {
0281 bits_per_word = t->bits_per_word;
0282 hz = t->speed_hz;
0283 }
0284
0285
0286 if (!bits_per_word)
0287 bits_per_word = spi->bits_per_word;
0288
0289
0290
0291
0292
0293 if (bits_per_word <= 8) {
0294 dspi->get_rx = davinci_spi_rx_buf_u8;
0295 dspi->get_tx = davinci_spi_tx_buf_u8;
0296 dspi->bytes_per_word[spi->chip_select] = 1;
0297 } else {
0298 dspi->get_rx = davinci_spi_rx_buf_u16;
0299 dspi->get_tx = davinci_spi_tx_buf_u16;
0300 dspi->bytes_per_word[spi->chip_select] = 2;
0301 }
0302
0303 if (!hz)
0304 hz = spi->max_speed_hz;
0305
0306
0307
0308 prescale = davinci_spi_get_prescale(dspi, hz);
0309 if (prescale < 0)
0310 return prescale;
0311
0312 spifmt = (prescale << SPIFMT_PRESCALE_SHIFT) | (bits_per_word & 0x1f);
0313
0314 if (spi->mode & SPI_LSB_FIRST)
0315 spifmt |= SPIFMT_SHIFTDIR_MASK;
0316
0317 if (spi->mode & SPI_CPOL)
0318 spifmt |= SPIFMT_POLARITY_MASK;
0319
0320 if (!(spi->mode & SPI_CPHA))
0321 spifmt |= SPIFMT_PHASE_MASK;
0322
0323
0324
0325
0326
0327 if (spicfg->wdelay)
0328 spifmt |= ((spicfg->wdelay << SPIFMT_WDELAY_SHIFT)
0329 & SPIFMT_WDELAY_MASK);
0330
0331
0332
0333
0334
0335
0336
0337
0338
0339
0340
0341
0342
0343
0344 if (dspi->version == SPI_VERSION_2) {
0345
0346 u32 delay = 0;
0347
0348 if (spicfg->odd_parity)
0349 spifmt |= SPIFMT_ODD_PARITY_MASK;
0350
0351 if (spicfg->parity_enable)
0352 spifmt |= SPIFMT_PARITYENA_MASK;
0353
0354 if (spicfg->timer_disable) {
0355 spifmt |= SPIFMT_DISTIMER_MASK;
0356 } else {
0357 delay |= (spicfg->c2tdelay << SPIDELAY_C2TDELAY_SHIFT)
0358 & SPIDELAY_C2TDELAY_MASK;
0359 delay |= (spicfg->t2cdelay << SPIDELAY_T2CDELAY_SHIFT)
0360 & SPIDELAY_T2CDELAY_MASK;
0361 }
0362
0363 if (spi->mode & SPI_READY) {
0364 spifmt |= SPIFMT_WAITENA_MASK;
0365 delay |= (spicfg->t2edelay << SPIDELAY_T2EDELAY_SHIFT)
0366 & SPIDELAY_T2EDELAY_MASK;
0367 delay |= (spicfg->c2edelay << SPIDELAY_C2EDELAY_SHIFT)
0368 & SPIDELAY_C2EDELAY_MASK;
0369 }
0370
0371 iowrite32(delay, dspi->base + SPIDELAY);
0372 }
0373
0374 iowrite32(spifmt, dspi->base + SPIFMT0);
0375
0376 return 0;
0377 }
0378
0379 static int davinci_spi_of_setup(struct spi_device *spi)
0380 {
0381 struct davinci_spi_config *spicfg = spi->controller_data;
0382 struct device_node *np = spi->dev.of_node;
0383 struct davinci_spi *dspi = spi_master_get_devdata(spi->master);
0384 u32 prop;
0385
0386 if (spicfg == NULL && np) {
0387 spicfg = kzalloc(sizeof(*spicfg), GFP_KERNEL);
0388 if (!spicfg)
0389 return -ENOMEM;
0390 *spicfg = davinci_spi_default_cfg;
0391
0392 if (!of_property_read_u32(np, "ti,spi-wdelay", &prop))
0393 spicfg->wdelay = (u8)prop;
0394 spi->controller_data = spicfg;
0395
0396 if (dspi->dma_rx && dspi->dma_tx)
0397 spicfg->io_type = SPI_IO_TYPE_DMA;
0398 }
0399
0400 return 0;
0401 }
0402
0403
0404
0405
0406
0407
0408
0409 static int davinci_spi_setup(struct spi_device *spi)
0410 {
0411 struct davinci_spi *dspi;
0412 struct device_node *np = spi->dev.of_node;
0413 bool internal_cs = true;
0414
0415 dspi = spi_master_get_devdata(spi->master);
0416
0417 if (!(spi->mode & SPI_NO_CS)) {
0418 if (np && spi->cs_gpiod)
0419 internal_cs = false;
0420
0421 if (internal_cs)
0422 set_io_bits(dspi->base + SPIPC0, 1 << spi->chip_select);
0423 }
0424
0425 if (spi->mode & SPI_READY)
0426 set_io_bits(dspi->base + SPIPC0, SPIPC0_SPIENA_MASK);
0427
0428 if (spi->mode & SPI_LOOP)
0429 set_io_bits(dspi->base + SPIGCR1, SPIGCR1_LOOPBACK_MASK);
0430 else
0431 clear_io_bits(dspi->base + SPIGCR1, SPIGCR1_LOOPBACK_MASK);
0432
0433 return davinci_spi_of_setup(spi);
0434 }
0435
0436 static void davinci_spi_cleanup(struct spi_device *spi)
0437 {
0438 struct davinci_spi_config *spicfg = spi->controller_data;
0439
0440 spi->controller_data = NULL;
0441 if (spi->dev.of_node)
0442 kfree(spicfg);
0443 }
0444
0445 static bool davinci_spi_can_dma(struct spi_master *master,
0446 struct spi_device *spi,
0447 struct spi_transfer *xfer)
0448 {
0449 struct davinci_spi_config *spicfg = spi->controller_data;
0450 bool can_dma = false;
0451
0452 if (spicfg)
0453 can_dma = (spicfg->io_type == SPI_IO_TYPE_DMA) &&
0454 (xfer->len >= DMA_MIN_BYTES) &&
0455 !is_vmalloc_addr(xfer->rx_buf) &&
0456 !is_vmalloc_addr(xfer->tx_buf);
0457
0458 return can_dma;
0459 }
0460
0461 static int davinci_spi_check_error(struct davinci_spi *dspi, int int_status)
0462 {
0463 struct device *sdev = dspi->bitbang.master->dev.parent;
0464
0465 if (int_status & SPIFLG_TIMEOUT_MASK) {
0466 dev_err(sdev, "SPI Time-out Error\n");
0467 return -ETIMEDOUT;
0468 }
0469 if (int_status & SPIFLG_DESYNC_MASK) {
0470 dev_err(sdev, "SPI Desynchronization Error\n");
0471 return -EIO;
0472 }
0473 if (int_status & SPIFLG_BITERR_MASK) {
0474 dev_err(sdev, "SPI Bit error\n");
0475 return -EIO;
0476 }
0477
0478 if (dspi->version == SPI_VERSION_2) {
0479 if (int_status & SPIFLG_DLEN_ERR_MASK) {
0480 dev_err(sdev, "SPI Data Length Error\n");
0481 return -EIO;
0482 }
0483 if (int_status & SPIFLG_PARERR_MASK) {
0484 dev_err(sdev, "SPI Parity Error\n");
0485 return -EIO;
0486 }
0487 if (int_status & SPIFLG_OVRRUN_MASK) {
0488 dev_err(sdev, "SPI Data Overrun error\n");
0489 return -EIO;
0490 }
0491 if (int_status & SPIFLG_BUF_INIT_ACTIVE_MASK) {
0492 dev_err(sdev, "SPI Buffer Init Active\n");
0493 return -EBUSY;
0494 }
0495 }
0496
0497 return 0;
0498 }
0499
0500
0501
0502
0503
0504
0505
0506
0507 static int davinci_spi_process_events(struct davinci_spi *dspi)
0508 {
0509 u32 buf, status, errors = 0, spidat1;
0510
0511 buf = ioread32(dspi->base + SPIBUF);
0512
0513 if (dspi->rcount > 0 && !(buf & SPIBUF_RXEMPTY_MASK)) {
0514 dspi->get_rx(buf & 0xFFFF, dspi);
0515 dspi->rcount--;
0516 }
0517
0518 status = ioread32(dspi->base + SPIFLG);
0519
0520 if (unlikely(status & SPIFLG_ERROR_MASK)) {
0521 errors = status & SPIFLG_ERROR_MASK;
0522 goto out;
0523 }
0524
0525 if (dspi->wcount > 0 && !(buf & SPIBUF_TXFULL_MASK)) {
0526 spidat1 = ioread32(dspi->base + SPIDAT1);
0527 dspi->wcount--;
0528 spidat1 &= ~0xFFFF;
0529 spidat1 |= 0xFFFF & dspi->get_tx(dspi);
0530 iowrite32(spidat1, dspi->base + SPIDAT1);
0531 }
0532
0533 out:
0534 return errors;
0535 }
0536
0537 static void davinci_spi_dma_rx_callback(void *data)
0538 {
0539 struct davinci_spi *dspi = (struct davinci_spi *)data;
0540
0541 dspi->rcount = 0;
0542
0543 if (!dspi->wcount && !dspi->rcount)
0544 complete(&dspi->done);
0545 }
0546
0547 static void davinci_spi_dma_tx_callback(void *data)
0548 {
0549 struct davinci_spi *dspi = (struct davinci_spi *)data;
0550
0551 dspi->wcount = 0;
0552
0553 if (!dspi->wcount && !dspi->rcount)
0554 complete(&dspi->done);
0555 }
0556
0557
0558
0559
0560
0561
0562
0563
0564
0565
0566 static int davinci_spi_bufs(struct spi_device *spi, struct spi_transfer *t)
0567 {
0568 struct davinci_spi *dspi;
0569 int data_type, ret = -ENOMEM;
0570 u32 tx_data, spidat1;
0571 u32 errors = 0;
0572 struct davinci_spi_config *spicfg;
0573 struct davinci_spi_platform_data *pdata;
0574
0575 dspi = spi_master_get_devdata(spi->master);
0576 pdata = &dspi->pdata;
0577 spicfg = (struct davinci_spi_config *)spi->controller_data;
0578 if (!spicfg)
0579 spicfg = &davinci_spi_default_cfg;
0580
0581
0582 data_type = dspi->bytes_per_word[spi->chip_select];
0583
0584 dspi->tx = t->tx_buf;
0585 dspi->rx = t->rx_buf;
0586 dspi->wcount = t->len / data_type;
0587 dspi->rcount = dspi->wcount;
0588
0589 spidat1 = ioread32(dspi->base + SPIDAT1);
0590
0591 clear_io_bits(dspi->base + SPIGCR1, SPIGCR1_POWERDOWN_MASK);
0592 set_io_bits(dspi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
0593
0594 reinit_completion(&dspi->done);
0595
0596 if (!davinci_spi_can_dma(spi->master, spi, t)) {
0597 if (spicfg->io_type != SPI_IO_TYPE_POLL)
0598 set_io_bits(dspi->base + SPIINT, SPIINT_MASKINT);
0599
0600 dspi->wcount--;
0601 tx_data = dspi->get_tx(dspi);
0602 spidat1 &= 0xFFFF0000;
0603 spidat1 |= tx_data & 0xFFFF;
0604 iowrite32(spidat1, dspi->base + SPIDAT1);
0605 } else {
0606 struct dma_slave_config dma_rx_conf = {
0607 .direction = DMA_DEV_TO_MEM,
0608 .src_addr = (unsigned long)dspi->pbase + SPIBUF,
0609 .src_addr_width = data_type,
0610 .src_maxburst = 1,
0611 };
0612 struct dma_slave_config dma_tx_conf = {
0613 .direction = DMA_MEM_TO_DEV,
0614 .dst_addr = (unsigned long)dspi->pbase + SPIDAT1,
0615 .dst_addr_width = data_type,
0616 .dst_maxburst = 1,
0617 };
0618 struct dma_async_tx_descriptor *rxdesc;
0619 struct dma_async_tx_descriptor *txdesc;
0620
0621 dmaengine_slave_config(dspi->dma_rx, &dma_rx_conf);
0622 dmaengine_slave_config(dspi->dma_tx, &dma_tx_conf);
0623
0624 rxdesc = dmaengine_prep_slave_sg(dspi->dma_rx,
0625 t->rx_sg.sgl, t->rx_sg.nents, DMA_DEV_TO_MEM,
0626 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
0627 if (!rxdesc)
0628 goto err_desc;
0629
0630 if (!t->tx_buf) {
0631
0632
0633
0634
0635
0636 t->tx_sg.sgl = t->rx_sg.sgl;
0637 t->tx_sg.nents = t->rx_sg.nents;
0638 }
0639
0640 txdesc = dmaengine_prep_slave_sg(dspi->dma_tx,
0641 t->tx_sg.sgl, t->tx_sg.nents, DMA_MEM_TO_DEV,
0642 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
0643 if (!txdesc)
0644 goto err_desc;
0645
0646 rxdesc->callback = davinci_spi_dma_rx_callback;
0647 rxdesc->callback_param = (void *)dspi;
0648 txdesc->callback = davinci_spi_dma_tx_callback;
0649 txdesc->callback_param = (void *)dspi;
0650
0651 if (pdata->cshold_bug)
0652 iowrite16(spidat1 >> 16, dspi->base + SPIDAT1 + 2);
0653
0654 dmaengine_submit(rxdesc);
0655 dmaengine_submit(txdesc);
0656
0657 dma_async_issue_pending(dspi->dma_rx);
0658 dma_async_issue_pending(dspi->dma_tx);
0659
0660 set_io_bits(dspi->base + SPIINT, SPIINT_DMA_REQ_EN);
0661 }
0662
0663
0664 if (spicfg->io_type != SPI_IO_TYPE_POLL) {
0665 if (wait_for_completion_timeout(&dspi->done, HZ) == 0)
0666 errors = SPIFLG_TIMEOUT_MASK;
0667 } else {
0668 while (dspi->rcount > 0 || dspi->wcount > 0) {
0669 errors = davinci_spi_process_events(dspi);
0670 if (errors)
0671 break;
0672 cpu_relax();
0673 }
0674 }
0675
0676 clear_io_bits(dspi->base + SPIINT, SPIINT_MASKALL);
0677 if (davinci_spi_can_dma(spi->master, spi, t))
0678 clear_io_bits(dspi->base + SPIINT, SPIINT_DMA_REQ_EN);
0679
0680 clear_io_bits(dspi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
0681 set_io_bits(dspi->base + SPIGCR1, SPIGCR1_POWERDOWN_MASK);
0682
0683
0684
0685
0686
0687 if (errors) {
0688 ret = davinci_spi_check_error(dspi, errors);
0689 WARN(!ret, "%s: error reported but no error found!\n",
0690 dev_name(&spi->dev));
0691 return ret;
0692 }
0693
0694 if (dspi->rcount != 0 || dspi->wcount != 0) {
0695 dev_err(&spi->dev, "SPI data transfer error\n");
0696 return -EIO;
0697 }
0698
0699 return t->len;
0700
0701 err_desc:
0702 return ret;
0703 }
0704
0705
0706
0707
0708
0709
0710
0711
0712
0713 static irqreturn_t dummy_thread_fn(s32 irq, void *data)
0714 {
0715 return IRQ_HANDLED;
0716 }
0717
0718
0719
0720
0721
0722
0723
0724
0725
0726
0727
0728
0729 static irqreturn_t davinci_spi_irq(s32 irq, void *data)
0730 {
0731 struct davinci_spi *dspi = data;
0732 int status;
0733
0734 status = davinci_spi_process_events(dspi);
0735 if (unlikely(status != 0))
0736 clear_io_bits(dspi->base + SPIINT, SPIINT_MASKINT);
0737
0738 if ((!dspi->rcount && !dspi->wcount) || status)
0739 complete(&dspi->done);
0740
0741 return IRQ_HANDLED;
0742 }
0743
0744 static int davinci_spi_request_dma(struct davinci_spi *dspi)
0745 {
0746 struct device *sdev = dspi->bitbang.master->dev.parent;
0747
0748 dspi->dma_rx = dma_request_chan(sdev, "rx");
0749 if (IS_ERR(dspi->dma_rx))
0750 return PTR_ERR(dspi->dma_rx);
0751
0752 dspi->dma_tx = dma_request_chan(sdev, "tx");
0753 if (IS_ERR(dspi->dma_tx)) {
0754 dma_release_channel(dspi->dma_rx);
0755 return PTR_ERR(dspi->dma_tx);
0756 }
0757
0758 return 0;
0759 }
0760
0761 #if defined(CONFIG_OF)
0762
0763
0764 struct davinci_spi_of_data {
0765 u8 version;
0766 u8 prescaler_limit;
0767 };
0768
0769 static const struct davinci_spi_of_data dm6441_spi_data = {
0770 .version = SPI_VERSION_1,
0771 .prescaler_limit = 2,
0772 };
0773
0774 static const struct davinci_spi_of_data da830_spi_data = {
0775 .version = SPI_VERSION_2,
0776 .prescaler_limit = 2,
0777 };
0778
0779 static const struct davinci_spi_of_data keystone_spi_data = {
0780 .version = SPI_VERSION_1,
0781 .prescaler_limit = 0,
0782 };
0783
0784 static const struct of_device_id davinci_spi_of_match[] = {
0785 {
0786 .compatible = "ti,dm6441-spi",
0787 .data = &dm6441_spi_data,
0788 },
0789 {
0790 .compatible = "ti,da830-spi",
0791 .data = &da830_spi_data,
0792 },
0793 {
0794 .compatible = "ti,keystone-spi",
0795 .data = &keystone_spi_data,
0796 },
0797 { },
0798 };
0799 MODULE_DEVICE_TABLE(of, davinci_spi_of_match);
0800
0801
0802
0803
0804
0805
0806
0807
0808
0809
0810 static int spi_davinci_get_pdata(struct platform_device *pdev,
0811 struct davinci_spi *dspi)
0812 {
0813 struct device_node *node = pdev->dev.of_node;
0814 const struct davinci_spi_of_data *spi_data;
0815 struct davinci_spi_platform_data *pdata;
0816 unsigned int num_cs, intr_line = 0;
0817
0818 pdata = &dspi->pdata;
0819
0820 spi_data = device_get_match_data(&pdev->dev);
0821
0822 pdata->version = spi_data->version;
0823 pdata->prescaler_limit = spi_data->prescaler_limit;
0824
0825
0826
0827
0828
0829
0830
0831 num_cs = 1;
0832 of_property_read_u32(node, "num-cs", &num_cs);
0833 pdata->num_chipselect = num_cs;
0834 of_property_read_u32(node, "ti,davinci-spi-intr-line", &intr_line);
0835 pdata->intr_line = intr_line;
0836 return 0;
0837 }
0838 #else
0839 static int spi_davinci_get_pdata(struct platform_device *pdev,
0840 struct davinci_spi *dspi)
0841 {
0842 return -ENODEV;
0843 }
0844 #endif
0845
0846
0847
0848
0849
0850
0851
0852
0853
0854
0855
0856
0857 static int davinci_spi_probe(struct platform_device *pdev)
0858 {
0859 struct spi_master *master;
0860 struct davinci_spi *dspi;
0861 struct davinci_spi_platform_data *pdata;
0862 struct resource *r;
0863 int ret = 0;
0864 u32 spipc0;
0865
0866 master = spi_alloc_master(&pdev->dev, sizeof(struct davinci_spi));
0867 if (master == NULL) {
0868 ret = -ENOMEM;
0869 goto err;
0870 }
0871
0872 platform_set_drvdata(pdev, master);
0873
0874 dspi = spi_master_get_devdata(master);
0875
0876 if (dev_get_platdata(&pdev->dev)) {
0877 pdata = dev_get_platdata(&pdev->dev);
0878 dspi->pdata = *pdata;
0879 } else {
0880
0881 ret = spi_davinci_get_pdata(pdev, dspi);
0882 if (ret < 0)
0883 goto free_master;
0884 }
0885
0886
0887 pdata = &dspi->pdata;
0888
0889 dspi->bytes_per_word = devm_kcalloc(&pdev->dev,
0890 pdata->num_chipselect,
0891 sizeof(*dspi->bytes_per_word),
0892 GFP_KERNEL);
0893 if (dspi->bytes_per_word == NULL) {
0894 ret = -ENOMEM;
0895 goto free_master;
0896 }
0897
0898 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0899 if (r == NULL) {
0900 ret = -ENOENT;
0901 goto free_master;
0902 }
0903
0904 dspi->pbase = r->start;
0905
0906 dspi->base = devm_ioremap_resource(&pdev->dev, r);
0907 if (IS_ERR(dspi->base)) {
0908 ret = PTR_ERR(dspi->base);
0909 goto free_master;
0910 }
0911
0912 init_completion(&dspi->done);
0913
0914 ret = platform_get_irq(pdev, 0);
0915 if (ret == 0)
0916 ret = -EINVAL;
0917 if (ret < 0)
0918 goto free_master;
0919 dspi->irq = ret;
0920
0921 ret = devm_request_threaded_irq(&pdev->dev, dspi->irq, davinci_spi_irq,
0922 dummy_thread_fn, 0, dev_name(&pdev->dev), dspi);
0923 if (ret)
0924 goto free_master;
0925
0926 dspi->bitbang.master = master;
0927
0928 dspi->clk = devm_clk_get(&pdev->dev, NULL);
0929 if (IS_ERR(dspi->clk)) {
0930 ret = -ENODEV;
0931 goto free_master;
0932 }
0933 ret = clk_prepare_enable(dspi->clk);
0934 if (ret)
0935 goto free_master;
0936
0937 master->use_gpio_descriptors = true;
0938 master->dev.of_node = pdev->dev.of_node;
0939 master->bus_num = pdev->id;
0940 master->num_chipselect = pdata->num_chipselect;
0941 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(2, 16);
0942 master->flags = SPI_MASTER_MUST_RX | SPI_MASTER_GPIO_SS;
0943 master->setup = davinci_spi_setup;
0944 master->cleanup = davinci_spi_cleanup;
0945 master->can_dma = davinci_spi_can_dma;
0946
0947 dspi->bitbang.chipselect = davinci_spi_chipselect;
0948 dspi->bitbang.setup_transfer = davinci_spi_setup_transfer;
0949 dspi->prescaler_limit = pdata->prescaler_limit;
0950 dspi->version = pdata->version;
0951
0952 dspi->bitbang.flags = SPI_NO_CS | SPI_LSB_FIRST | SPI_LOOP | SPI_CS_WORD;
0953 if (dspi->version == SPI_VERSION_2)
0954 dspi->bitbang.flags |= SPI_READY;
0955
0956 dspi->bitbang.txrx_bufs = davinci_spi_bufs;
0957
0958 ret = davinci_spi_request_dma(dspi);
0959 if (ret == -EPROBE_DEFER) {
0960 goto free_clk;
0961 } else if (ret) {
0962 dev_info(&pdev->dev, "DMA is not supported (%d)\n", ret);
0963 dspi->dma_rx = NULL;
0964 dspi->dma_tx = NULL;
0965 }
0966
0967 dspi->get_rx = davinci_spi_rx_buf_u8;
0968 dspi->get_tx = davinci_spi_tx_buf_u8;
0969
0970
0971 iowrite32(0, dspi->base + SPIGCR0);
0972 udelay(100);
0973 iowrite32(1, dspi->base + SPIGCR0);
0974
0975
0976 spipc0 = SPIPC0_DIFUN_MASK | SPIPC0_DOFUN_MASK | SPIPC0_CLKFUN_MASK;
0977 iowrite32(spipc0, dspi->base + SPIPC0);
0978
0979 if (pdata->intr_line)
0980 iowrite32(SPI_INTLVL_1, dspi->base + SPILVL);
0981 else
0982 iowrite32(SPI_INTLVL_0, dspi->base + SPILVL);
0983
0984 iowrite32(CS_DEFAULT, dspi->base + SPIDEF);
0985
0986
0987 set_io_bits(dspi->base + SPIGCR1, SPIGCR1_CLKMOD_MASK);
0988 set_io_bits(dspi->base + SPIGCR1, SPIGCR1_MASTER_MASK);
0989 set_io_bits(dspi->base + SPIGCR1, SPIGCR1_POWERDOWN_MASK);
0990
0991 ret = spi_bitbang_start(&dspi->bitbang);
0992 if (ret)
0993 goto free_dma;
0994
0995 dev_info(&pdev->dev, "Controller at 0x%p\n", dspi->base);
0996
0997 return ret;
0998
0999 free_dma:
1000 if (dspi->dma_rx) {
1001 dma_release_channel(dspi->dma_rx);
1002 dma_release_channel(dspi->dma_tx);
1003 }
1004 free_clk:
1005 clk_disable_unprepare(dspi->clk);
1006 free_master:
1007 spi_master_put(master);
1008 err:
1009 return ret;
1010 }
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021 static int davinci_spi_remove(struct platform_device *pdev)
1022 {
1023 struct davinci_spi *dspi;
1024 struct spi_master *master;
1025
1026 master = platform_get_drvdata(pdev);
1027 dspi = spi_master_get_devdata(master);
1028
1029 spi_bitbang_stop(&dspi->bitbang);
1030
1031 clk_disable_unprepare(dspi->clk);
1032
1033 if (dspi->dma_rx) {
1034 dma_release_channel(dspi->dma_rx);
1035 dma_release_channel(dspi->dma_tx);
1036 }
1037
1038 spi_master_put(master);
1039 return 0;
1040 }
1041
1042 static struct platform_driver davinci_spi_driver = {
1043 .driver = {
1044 .name = "spi_davinci",
1045 .of_match_table = of_match_ptr(davinci_spi_of_match),
1046 },
1047 .probe = davinci_spi_probe,
1048 .remove = davinci_spi_remove,
1049 };
1050 module_platform_driver(davinci_spi_driver);
1051
1052 MODULE_DESCRIPTION("TI DaVinci SPI Master Controller Driver");
1053 MODULE_LICENSE("GPL");