Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Copyright (C) 2009 Texas Instruments.
0004  * Copyright (C) 2010 EF Johnson Technologies
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 /* SPIPC0 */
0039 #define SPIPC0_DIFUN_MASK   BIT(11)     /* MISO */
0040 #define SPIPC0_DOFUN_MASK   BIT(10)     /* MOSI */
0041 #define SPIPC0_CLKFUN_MASK  BIT(9)      /* CLK */
0042 #define SPIPC0_SPIENA_MASK  BIT(8)      /* nREADY */
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 /* SPIDAT1 (upper 16 bit defines) */
0050 #define SPIDAT1_CSHOLD_MASK BIT(12)
0051 #define SPIDAT1_WDEL        BIT(10)
0052 
0053 /* SPIGCR1 */
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 /* SPIBUF */
0061 #define SPIBUF_TXFULL_MASK  BIT(29)
0062 #define SPIBUF_RXEMPTY_MASK BIT(31)
0063 
0064 /* SPIDELAY */
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 /* Error Masks */
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 /* SPI Controller registers */
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 /* SPI Controller driver's private data. */
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  * Interface to control the chip select signal
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     /* program delay transfers if tx_delay is non zero */
0208     if (spicfg && spicfg->wdelay)
0209         spidat1 |= SPIDAT1_WDEL;
0210 
0211     /*
0212      * Board specific chip select logic decides the polarity and cs
0213      * line for the controller
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  * davinci_spi_get_prescale - Calculates the correct prescale value
0233  * @dspi: the controller data
0234  * @max_speed_hz: the maximum rate the SPI clock can run at
0235  *
0236  * This function calculates the prescale value that generates a clock rate
0237  * less than or equal to the specified maximum.
0238  *
0239  * Returns: calculated prescale value for easy programming into SPI registers
0240  * or negative error number if valid prescalar cannot be updated.
0241  */
0242 static inline int davinci_spi_get_prescale(struct davinci_spi *dspi,
0243                             u32 max_speed_hz)
0244 {
0245     int ret;
0246 
0247     /* Subtract 1 to match what will be programmed into SPI register. */
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  * davinci_spi_setup_transfer - This functions will determine transfer method
0258  * @spi: spi device on which data transfer to be done
0259  * @t: spi transfer in which transfer info is filled
0260  *
0261  * This function determines data transfer method (8/16/32 bit transfer).
0262  * It will also set the SPI Clock Control register according to
0263  * SPI slave device freq.
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     /* if bits_per_word is not set then set it default */
0286     if (!bits_per_word)
0287         bits_per_word = spi->bits_per_word;
0288 
0289     /*
0290      * Assign function pointer to appropriate transfer method
0291      * 8bit, 16bit or 32bit transfer
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     /* Set up SPIFMTn register, unique to this chipselect. */
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     * Assume wdelay is used only on SPI peripherals that has this field
0325     * in SPIFMTn register and when it's configured from board file or DT.
0326     */
0327     if (spicfg->wdelay)
0328         spifmt |= ((spicfg->wdelay << SPIFMT_WDELAY_SHIFT)
0329                 & SPIFMT_WDELAY_MASK);
0330 
0331     /*
0332      * Version 1 hardware supports two basic SPI modes:
0333      *  - Standard SPI mode uses 4 pins, with chipselect
0334      *  - 3 pin SPI is a 4 pin variant without CS (SPI_NO_CS)
0335      *  (distinct from SPI_3WIRE, with just one data wire;
0336      *  or similar variants without MOSI or without MISO)
0337      *
0338      * Version 2 hardware supports an optional handshaking signal,
0339      * so it can support two more modes:
0340      *  - 5 pin SPI variant is standard SPI plus SPI_READY
0341      *  - 4 pin with enable is (SPI_READY | SPI_NO_CS)
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         /* override with dt configured values */
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  * davinci_spi_setup - This functions will set default transfer method
0405  * @spi: spi device on which data transfer to be done
0406  *
0407  * This functions sets the default transfer method.
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  * davinci_spi_process_events - check for and handle any SPI controller events
0502  * @dspi: the controller data
0503  *
0504  * This function will check the SPIFLG register and handle any events that are
0505  * detected there
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  * davinci_spi_bufs - functions which will handle transfer data
0559  * @spi: spi device on which data transfer to be done
0560  * @t: spi transfer in which transfer info is filled
0561  *
0562  * This function will put data to be transferred into data register
0563  * of SPI controller and then wait until the completion will be marked
0564  * by the IRQ Handler.
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     /* convert len to words based on bits_per_word */
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         /* start the transfer */
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             /* To avoid errors when doing rx-only transfers with
0632              * many SG entries (> 20), use the rx buffer as the
0633              * dummy tx buffer so that dma reloads are done at the
0634              * same time for rx and tx.
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     /* Wait for the transfer to complete */
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      * Check for bit error, desync error,parity error,timeout error and
0685      * receive overflow errors
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  * dummy_thread_fn - dummy thread function
0707  * @irq: IRQ number for this SPI Master
0708  * @data: structure for SPI Master controller davinci_spi
0709  *
0710  * This is to satisfy the request_threaded_irq() API so that the irq
0711  * handler is called in interrupt context.
0712  */
0713 static irqreturn_t dummy_thread_fn(s32 irq, void *data)
0714 {
0715     return IRQ_HANDLED;
0716 }
0717 
0718 /**
0719  * davinci_spi_irq - Interrupt handler for SPI Master Controller
0720  * @irq: IRQ number for this SPI Master
0721  * @data: structure for SPI Master controller davinci_spi
0722  *
0723  * ISR will determine that interrupt arrives either for READ or WRITE command.
0724  * According to command it will do the appropriate action. It will check
0725  * transfer length and if it is not zero then dispatch transfer command again.
0726  * If transfer length is zero then it will indicate the COMPLETION so that
0727  * davinci_spi_bufs function can go ahead.
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 /* OF SPI data structure */
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  * spi_davinci_get_pdata - Get platform data from DTS binding
0803  * @pdev: ptr to platform data
0804  * @dspi: ptr to driver data
0805  *
0806  * Parses and populates pdata in dspi from device tree bindings.
0807  *
0808  * NOTE: Not all platform data params are supported currently.
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      * default num_cs is 1 and all chipsel are internal to the chip
0826      * indicated by chip_sel being NULL or cs_gpios being NULL or
0827      * set to -ENOENT. num-cs includes internal as well as gpios.
0828      * indicated by chip_sel being NULL. GPIO based CS is not
0829      * supported yet in DT bindings.
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  * davinci_spi_probe - probe function for SPI Master Controller
0848  * @pdev: platform_device structure which contains plateform specific data
0849  *
0850  * According to Linux Device Model this function will be invoked by Linux
0851  * with platform_device struct which contains the device specific info.
0852  * This function will map the SPI controller's memory, register IRQ,
0853  * Reset SPI controller and setting its registers to default value.
0854  * It will invoke spi_bitbang_start to create work queue so that client driver
0855  * can register transfer method to work queue.
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         /* update dspi pdata with that from the DT */
0881         ret = spi_davinci_get_pdata(pdev, dspi);
0882         if (ret < 0)
0883             goto free_master;
0884     }
0885 
0886     /* pdata in dspi is now updated and point pdata to that */
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     /* Reset In/OUT SPI module */
0971     iowrite32(0, dspi->base + SPIGCR0);
0972     udelay(100);
0973     iowrite32(1, dspi->base + SPIGCR0);
0974 
0975     /* Set up SPIPC0.  CS and ENA init is done in davinci_spi_setup */
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     /* master mode default */
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  * davinci_spi_remove - remove function for SPI Master Controller
1014  * @pdev: platform_device structure which contains plateform specific data
1015  *
1016  * This function will do the reverse action of davinci_spi_probe function
1017  * It will free the IRQ and SPI controller's memory region.
1018  * It will also call spi_bitbang_stop to destroy the work queue which was
1019  * created by spi_bitbang_start.
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");