Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Marvell Armada-3700 SPI controller driver
0004  *
0005  * Copyright (C) 2016 Marvell Ltd.
0006  *
0007  * Author: Wilson Ding <dingwei@marvell.com>
0008  * Author: Romain Perier <romain.perier@free-electrons.com>
0009  */
0010 
0011 #include <linux/clk.h>
0012 #include <linux/completion.h>
0013 #include <linux/delay.h>
0014 #include <linux/err.h>
0015 #include <linux/interrupt.h>
0016 #include <linux/io.h>
0017 #include <linux/kernel.h>
0018 #include <linux/module.h>
0019 #include <linux/of.h>
0020 #include <linux/of_irq.h>
0021 #include <linux/of_device.h>
0022 #include <linux/pinctrl/consumer.h>
0023 #include <linux/spi/spi.h>
0024 
0025 #define DRIVER_NAME         "armada_3700_spi"
0026 
0027 #define A3700_SPI_MAX_SPEED_HZ      100000000
0028 #define A3700_SPI_MAX_PRESCALE      30
0029 #define A3700_SPI_TIMEOUT       10
0030 
0031 /* SPI Register Offest */
0032 #define A3700_SPI_IF_CTRL_REG       0x00
0033 #define A3700_SPI_IF_CFG_REG        0x04
0034 #define A3700_SPI_DATA_OUT_REG      0x08
0035 #define A3700_SPI_DATA_IN_REG       0x0C
0036 #define A3700_SPI_IF_INST_REG       0x10
0037 #define A3700_SPI_IF_ADDR_REG       0x14
0038 #define A3700_SPI_IF_RMODE_REG      0x18
0039 #define A3700_SPI_IF_HDR_CNT_REG    0x1C
0040 #define A3700_SPI_IF_DIN_CNT_REG    0x20
0041 #define A3700_SPI_IF_TIME_REG       0x24
0042 #define A3700_SPI_INT_STAT_REG      0x28
0043 #define A3700_SPI_INT_MASK_REG      0x2C
0044 
0045 /* A3700_SPI_IF_CTRL_REG */
0046 #define A3700_SPI_EN            BIT(16)
0047 #define A3700_SPI_ADDR_NOT_CONFIG   BIT(12)
0048 #define A3700_SPI_WFIFO_OVERFLOW    BIT(11)
0049 #define A3700_SPI_WFIFO_UNDERFLOW   BIT(10)
0050 #define A3700_SPI_RFIFO_OVERFLOW    BIT(9)
0051 #define A3700_SPI_RFIFO_UNDERFLOW   BIT(8)
0052 #define A3700_SPI_WFIFO_FULL        BIT(7)
0053 #define A3700_SPI_WFIFO_EMPTY       BIT(6)
0054 #define A3700_SPI_RFIFO_FULL        BIT(5)
0055 #define A3700_SPI_RFIFO_EMPTY       BIT(4)
0056 #define A3700_SPI_WFIFO_RDY     BIT(3)
0057 #define A3700_SPI_RFIFO_RDY     BIT(2)
0058 #define A3700_SPI_XFER_RDY      BIT(1)
0059 #define A3700_SPI_XFER_DONE     BIT(0)
0060 
0061 /* A3700_SPI_IF_CFG_REG */
0062 #define A3700_SPI_WFIFO_THRS        BIT(28)
0063 #define A3700_SPI_RFIFO_THRS        BIT(24)
0064 #define A3700_SPI_AUTO_CS       BIT(20)
0065 #define A3700_SPI_DMA_RD_EN     BIT(18)
0066 #define A3700_SPI_FIFO_MODE     BIT(17)
0067 #define A3700_SPI_SRST          BIT(16)
0068 #define A3700_SPI_XFER_START        BIT(15)
0069 #define A3700_SPI_XFER_STOP     BIT(14)
0070 #define A3700_SPI_INST_PIN      BIT(13)
0071 #define A3700_SPI_ADDR_PIN      BIT(12)
0072 #define A3700_SPI_DATA_PIN1     BIT(11)
0073 #define A3700_SPI_DATA_PIN0     BIT(10)
0074 #define A3700_SPI_FIFO_FLUSH        BIT(9)
0075 #define A3700_SPI_RW_EN         BIT(8)
0076 #define A3700_SPI_CLK_POL       BIT(7)
0077 #define A3700_SPI_CLK_PHA       BIT(6)
0078 #define A3700_SPI_BYTE_LEN      BIT(5)
0079 #define A3700_SPI_CLK_PRESCALE      BIT(0)
0080 #define A3700_SPI_CLK_PRESCALE_MASK (0x1f)
0081 #define A3700_SPI_CLK_EVEN_OFFS     (0x10)
0082 
0083 #define A3700_SPI_WFIFO_THRS_BIT    28
0084 #define A3700_SPI_RFIFO_THRS_BIT    24
0085 #define A3700_SPI_FIFO_THRS_MASK    0x7
0086 
0087 #define A3700_SPI_DATA_PIN_MASK     0x3
0088 
0089 /* A3700_SPI_IF_HDR_CNT_REG */
0090 #define A3700_SPI_DUMMY_CNT_BIT     12
0091 #define A3700_SPI_DUMMY_CNT_MASK    0x7
0092 #define A3700_SPI_RMODE_CNT_BIT     8
0093 #define A3700_SPI_RMODE_CNT_MASK    0x3
0094 #define A3700_SPI_ADDR_CNT_BIT      4
0095 #define A3700_SPI_ADDR_CNT_MASK     0x7
0096 #define A3700_SPI_INSTR_CNT_BIT     0
0097 #define A3700_SPI_INSTR_CNT_MASK    0x3
0098 
0099 /* A3700_SPI_IF_TIME_REG */
0100 #define A3700_SPI_CLK_CAPT_EDGE     BIT(7)
0101 
0102 struct a3700_spi {
0103     struct spi_master *master;
0104     void __iomem *base;
0105     struct clk *clk;
0106     unsigned int irq;
0107     unsigned int flags;
0108     bool xmit_data;
0109     const u8 *tx_buf;
0110     u8 *rx_buf;
0111     size_t buf_len;
0112     u8 byte_len;
0113     u32 wait_mask;
0114     struct completion done;
0115 };
0116 
0117 static u32 spireg_read(struct a3700_spi *a3700_spi, u32 offset)
0118 {
0119     return readl(a3700_spi->base + offset);
0120 }
0121 
0122 static void spireg_write(struct a3700_spi *a3700_spi, u32 offset, u32 data)
0123 {
0124     writel(data, a3700_spi->base + offset);
0125 }
0126 
0127 static void a3700_spi_auto_cs_unset(struct a3700_spi *a3700_spi)
0128 {
0129     u32 val;
0130 
0131     val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
0132     val &= ~A3700_SPI_AUTO_CS;
0133     spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
0134 }
0135 
0136 static void a3700_spi_activate_cs(struct a3700_spi *a3700_spi, unsigned int cs)
0137 {
0138     u32 val;
0139 
0140     val = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
0141     val |= (A3700_SPI_EN << cs);
0142     spireg_write(a3700_spi, A3700_SPI_IF_CTRL_REG, val);
0143 }
0144 
0145 static void a3700_spi_deactivate_cs(struct a3700_spi *a3700_spi,
0146                     unsigned int cs)
0147 {
0148     u32 val;
0149 
0150     val = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
0151     val &= ~(A3700_SPI_EN << cs);
0152     spireg_write(a3700_spi, A3700_SPI_IF_CTRL_REG, val);
0153 }
0154 
0155 static int a3700_spi_pin_mode_set(struct a3700_spi *a3700_spi,
0156                   unsigned int pin_mode, bool receiving)
0157 {
0158     u32 val;
0159 
0160     val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
0161     val &= ~(A3700_SPI_INST_PIN | A3700_SPI_ADDR_PIN);
0162     val &= ~(A3700_SPI_DATA_PIN0 | A3700_SPI_DATA_PIN1);
0163 
0164     switch (pin_mode) {
0165     case SPI_NBITS_SINGLE:
0166         break;
0167     case SPI_NBITS_DUAL:
0168         val |= A3700_SPI_DATA_PIN0;
0169         break;
0170     case SPI_NBITS_QUAD:
0171         val |= A3700_SPI_DATA_PIN1;
0172         /* RX during address reception uses 4-pin */
0173         if (receiving)
0174             val |= A3700_SPI_ADDR_PIN;
0175         break;
0176     default:
0177         dev_err(&a3700_spi->master->dev, "wrong pin mode %u", pin_mode);
0178         return -EINVAL;
0179     }
0180 
0181     spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
0182 
0183     return 0;
0184 }
0185 
0186 static void a3700_spi_fifo_mode_set(struct a3700_spi *a3700_spi, bool enable)
0187 {
0188     u32 val;
0189 
0190     val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
0191     if (enable)
0192         val |= A3700_SPI_FIFO_MODE;
0193     else
0194         val &= ~A3700_SPI_FIFO_MODE;
0195     spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
0196 }
0197 
0198 static void a3700_spi_mode_set(struct a3700_spi *a3700_spi,
0199                    unsigned int mode_bits)
0200 {
0201     u32 val;
0202 
0203     val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
0204 
0205     if (mode_bits & SPI_CPOL)
0206         val |= A3700_SPI_CLK_POL;
0207     else
0208         val &= ~A3700_SPI_CLK_POL;
0209 
0210     if (mode_bits & SPI_CPHA)
0211         val |= A3700_SPI_CLK_PHA;
0212     else
0213         val &= ~A3700_SPI_CLK_PHA;
0214 
0215     spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
0216 }
0217 
0218 static void a3700_spi_clock_set(struct a3700_spi *a3700_spi,
0219                 unsigned int speed_hz)
0220 {
0221     u32 val;
0222     u32 prescale;
0223 
0224     prescale = DIV_ROUND_UP(clk_get_rate(a3700_spi->clk), speed_hz);
0225 
0226     /* For prescaler values over 15, we can only set it by steps of 2.
0227      * Starting from A3700_SPI_CLK_EVEN_OFFS, we set values from 0 up to
0228      * 30. We only use this range from 16 to 30.
0229      */
0230     if (prescale > 15)
0231         prescale = A3700_SPI_CLK_EVEN_OFFS + DIV_ROUND_UP(prescale, 2);
0232 
0233     val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
0234     val = val & ~A3700_SPI_CLK_PRESCALE_MASK;
0235 
0236     val = val | (prescale & A3700_SPI_CLK_PRESCALE_MASK);
0237     spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
0238 
0239     if (prescale <= 2) {
0240         val = spireg_read(a3700_spi, A3700_SPI_IF_TIME_REG);
0241         val |= A3700_SPI_CLK_CAPT_EDGE;
0242         spireg_write(a3700_spi, A3700_SPI_IF_TIME_REG, val);
0243     }
0244 }
0245 
0246 static void a3700_spi_bytelen_set(struct a3700_spi *a3700_spi, unsigned int len)
0247 {
0248     u32 val;
0249 
0250     val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
0251     if (len == 4)
0252         val |= A3700_SPI_BYTE_LEN;
0253     else
0254         val &= ~A3700_SPI_BYTE_LEN;
0255     spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
0256 
0257     a3700_spi->byte_len = len;
0258 }
0259 
0260 static int a3700_spi_fifo_flush(struct a3700_spi *a3700_spi)
0261 {
0262     int timeout = A3700_SPI_TIMEOUT;
0263     u32 val;
0264 
0265     val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
0266     val |= A3700_SPI_FIFO_FLUSH;
0267     spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
0268 
0269     while (--timeout) {
0270         val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
0271         if (!(val & A3700_SPI_FIFO_FLUSH))
0272             return 0;
0273         udelay(1);
0274     }
0275 
0276     return -ETIMEDOUT;
0277 }
0278 
0279 static void a3700_spi_init(struct a3700_spi *a3700_spi)
0280 {
0281     struct spi_master *master = a3700_spi->master;
0282     u32 val;
0283     int i;
0284 
0285     /* Reset SPI unit */
0286     val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
0287     val |= A3700_SPI_SRST;
0288     spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
0289 
0290     udelay(A3700_SPI_TIMEOUT);
0291 
0292     val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
0293     val &= ~A3700_SPI_SRST;
0294     spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
0295 
0296     /* Disable AUTO_CS and deactivate all chip-selects */
0297     a3700_spi_auto_cs_unset(a3700_spi);
0298     for (i = 0; i < master->num_chipselect; i++)
0299         a3700_spi_deactivate_cs(a3700_spi, i);
0300 
0301     /* Enable FIFO mode */
0302     a3700_spi_fifo_mode_set(a3700_spi, true);
0303 
0304     /* Set SPI mode */
0305     a3700_spi_mode_set(a3700_spi, master->mode_bits);
0306 
0307     /* Reset counters */
0308     spireg_write(a3700_spi, A3700_SPI_IF_HDR_CNT_REG, 0);
0309     spireg_write(a3700_spi, A3700_SPI_IF_DIN_CNT_REG, 0);
0310 
0311     /* Mask the interrupts and clear cause bits */
0312     spireg_write(a3700_spi, A3700_SPI_INT_MASK_REG, 0);
0313     spireg_write(a3700_spi, A3700_SPI_INT_STAT_REG, ~0U);
0314 }
0315 
0316 static irqreturn_t a3700_spi_interrupt(int irq, void *dev_id)
0317 {
0318     struct spi_master *master = dev_id;
0319     struct a3700_spi *a3700_spi;
0320     u32 cause;
0321 
0322     a3700_spi = spi_master_get_devdata(master);
0323 
0324     /* Get interrupt causes */
0325     cause = spireg_read(a3700_spi, A3700_SPI_INT_STAT_REG);
0326 
0327     if (!cause || !(a3700_spi->wait_mask & cause))
0328         return IRQ_NONE;
0329 
0330     /* mask and acknowledge the SPI interrupts */
0331     spireg_write(a3700_spi, A3700_SPI_INT_MASK_REG, 0);
0332     spireg_write(a3700_spi, A3700_SPI_INT_STAT_REG, cause);
0333 
0334     /* Wake up the transfer */
0335     complete(&a3700_spi->done);
0336 
0337     return IRQ_HANDLED;
0338 }
0339 
0340 static bool a3700_spi_wait_completion(struct spi_device *spi)
0341 {
0342     struct a3700_spi *a3700_spi;
0343     unsigned int timeout;
0344     unsigned int ctrl_reg;
0345     unsigned long timeout_jiffies;
0346 
0347     a3700_spi = spi_master_get_devdata(spi->master);
0348 
0349     /* SPI interrupt is edge-triggered, which means an interrupt will
0350      * be generated only when detecting a specific status bit changed
0351      * from '0' to '1'. So when we start waiting for a interrupt, we
0352      * need to check status bit in control reg first, if it is already 1,
0353      * then we do not need to wait for interrupt
0354      */
0355     ctrl_reg = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
0356     if (a3700_spi->wait_mask & ctrl_reg)
0357         return true;
0358 
0359     reinit_completion(&a3700_spi->done);
0360 
0361     spireg_write(a3700_spi, A3700_SPI_INT_MASK_REG,
0362              a3700_spi->wait_mask);
0363 
0364     timeout_jiffies = msecs_to_jiffies(A3700_SPI_TIMEOUT);
0365     timeout = wait_for_completion_timeout(&a3700_spi->done,
0366                           timeout_jiffies);
0367 
0368     a3700_spi->wait_mask = 0;
0369 
0370     if (timeout)
0371         return true;
0372 
0373     /* there might be the case that right after we checked the
0374      * status bits in this routine and before start to wait for
0375      * interrupt by wait_for_completion_timeout, the interrupt
0376      * happens, to avoid missing it we need to double check
0377      * status bits in control reg, if it is already 1, then
0378      * consider that we have the interrupt successfully and
0379      * return true.
0380      */
0381     ctrl_reg = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
0382     if (a3700_spi->wait_mask & ctrl_reg)
0383         return true;
0384 
0385     spireg_write(a3700_spi, A3700_SPI_INT_MASK_REG, 0);
0386 
0387     /* Timeout was reached */
0388     return false;
0389 }
0390 
0391 static bool a3700_spi_transfer_wait(struct spi_device *spi,
0392                     unsigned int bit_mask)
0393 {
0394     struct a3700_spi *a3700_spi;
0395 
0396     a3700_spi = spi_master_get_devdata(spi->master);
0397     a3700_spi->wait_mask = bit_mask;
0398 
0399     return a3700_spi_wait_completion(spi);
0400 }
0401 
0402 static void a3700_spi_fifo_thres_set(struct a3700_spi *a3700_spi,
0403                      unsigned int bytes)
0404 {
0405     u32 val;
0406 
0407     val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
0408     val &= ~(A3700_SPI_FIFO_THRS_MASK << A3700_SPI_RFIFO_THRS_BIT);
0409     val |= (bytes - 1) << A3700_SPI_RFIFO_THRS_BIT;
0410     val &= ~(A3700_SPI_FIFO_THRS_MASK << A3700_SPI_WFIFO_THRS_BIT);
0411     val |= (7 - bytes) << A3700_SPI_WFIFO_THRS_BIT;
0412     spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
0413 }
0414 
0415 static void a3700_spi_transfer_setup(struct spi_device *spi,
0416                      struct spi_transfer *xfer)
0417 {
0418     struct a3700_spi *a3700_spi;
0419 
0420     a3700_spi = spi_master_get_devdata(spi->master);
0421 
0422     a3700_spi_clock_set(a3700_spi, xfer->speed_hz);
0423 
0424     /* Use 4 bytes long transfers. Each transfer method has its way to deal
0425      * with the remaining bytes for non 4-bytes aligned transfers.
0426      */
0427     a3700_spi_bytelen_set(a3700_spi, 4);
0428 
0429     /* Initialize the working buffers */
0430     a3700_spi->tx_buf  = xfer->tx_buf;
0431     a3700_spi->rx_buf  = xfer->rx_buf;
0432     a3700_spi->buf_len = xfer->len;
0433 }
0434 
0435 static void a3700_spi_set_cs(struct spi_device *spi, bool enable)
0436 {
0437     struct a3700_spi *a3700_spi = spi_master_get_devdata(spi->master);
0438 
0439     if (!enable)
0440         a3700_spi_activate_cs(a3700_spi, spi->chip_select);
0441     else
0442         a3700_spi_deactivate_cs(a3700_spi, spi->chip_select);
0443 }
0444 
0445 static void a3700_spi_header_set(struct a3700_spi *a3700_spi)
0446 {
0447     unsigned int addr_cnt;
0448     u32 val = 0;
0449 
0450     /* Clear the header registers */
0451     spireg_write(a3700_spi, A3700_SPI_IF_INST_REG, 0);
0452     spireg_write(a3700_spi, A3700_SPI_IF_ADDR_REG, 0);
0453     spireg_write(a3700_spi, A3700_SPI_IF_RMODE_REG, 0);
0454     spireg_write(a3700_spi, A3700_SPI_IF_HDR_CNT_REG, 0);
0455 
0456     /* Set header counters */
0457     if (a3700_spi->tx_buf) {
0458         /*
0459          * when tx data is not 4 bytes aligned, there will be unexpected
0460          * bytes out of SPI output register, since it always shifts out
0461          * as whole 4 bytes. This might cause incorrect transaction with
0462          * some devices. To avoid that, use SPI header count feature to
0463          * transfer up to 3 bytes of data first, and then make the rest
0464          * of data 4-byte aligned.
0465          */
0466         addr_cnt = a3700_spi->buf_len % 4;
0467         if (addr_cnt) {
0468             val = (addr_cnt & A3700_SPI_ADDR_CNT_MASK)
0469                 << A3700_SPI_ADDR_CNT_BIT;
0470             spireg_write(a3700_spi, A3700_SPI_IF_HDR_CNT_REG, val);
0471 
0472             /* Update the buffer length to be transferred */
0473             a3700_spi->buf_len -= addr_cnt;
0474 
0475             /* transfer 1~3 bytes through address count */
0476             val = 0;
0477             while (addr_cnt--) {
0478                 val = (val << 8) | a3700_spi->tx_buf[0];
0479                 a3700_spi->tx_buf++;
0480             }
0481             spireg_write(a3700_spi, A3700_SPI_IF_ADDR_REG, val);
0482         }
0483     }
0484 }
0485 
0486 static int a3700_is_wfifo_full(struct a3700_spi *a3700_spi)
0487 {
0488     u32 val;
0489 
0490     val = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
0491     return (val & A3700_SPI_WFIFO_FULL);
0492 }
0493 
0494 static int a3700_spi_fifo_write(struct a3700_spi *a3700_spi)
0495 {
0496     u32 val;
0497 
0498     while (!a3700_is_wfifo_full(a3700_spi) && a3700_spi->buf_len) {
0499         val = *(u32 *)a3700_spi->tx_buf;
0500         spireg_write(a3700_spi, A3700_SPI_DATA_OUT_REG, cpu_to_le32(val));
0501         a3700_spi->buf_len -= 4;
0502         a3700_spi->tx_buf += 4;
0503     }
0504 
0505     return 0;
0506 }
0507 
0508 static int a3700_is_rfifo_empty(struct a3700_spi *a3700_spi)
0509 {
0510     u32 val = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
0511 
0512     return (val & A3700_SPI_RFIFO_EMPTY);
0513 }
0514 
0515 static int a3700_spi_fifo_read(struct a3700_spi *a3700_spi)
0516 {
0517     u32 val;
0518 
0519     while (!a3700_is_rfifo_empty(a3700_spi) && a3700_spi->buf_len) {
0520         val = spireg_read(a3700_spi, A3700_SPI_DATA_IN_REG);
0521         if (a3700_spi->buf_len >= 4) {
0522             val = le32_to_cpu(val);
0523             memcpy(a3700_spi->rx_buf, &val, 4);
0524 
0525             a3700_spi->buf_len -= 4;
0526             a3700_spi->rx_buf += 4;
0527         } else {
0528             /*
0529              * When remain bytes is not larger than 4, we should
0530              * avoid memory overwriting and just write the left rx
0531              * buffer bytes.
0532              */
0533             while (a3700_spi->buf_len) {
0534                 *a3700_spi->rx_buf = val & 0xff;
0535                 val >>= 8;
0536 
0537                 a3700_spi->buf_len--;
0538                 a3700_spi->rx_buf++;
0539             }
0540         }
0541     }
0542 
0543     return 0;
0544 }
0545 
0546 static void a3700_spi_transfer_abort_fifo(struct a3700_spi *a3700_spi)
0547 {
0548     int timeout = A3700_SPI_TIMEOUT;
0549     u32 val;
0550 
0551     val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
0552     val |= A3700_SPI_XFER_STOP;
0553     spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
0554 
0555     while (--timeout) {
0556         val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
0557         if (!(val & A3700_SPI_XFER_START))
0558             break;
0559         udelay(1);
0560     }
0561 
0562     a3700_spi_fifo_flush(a3700_spi);
0563 
0564     val &= ~A3700_SPI_XFER_STOP;
0565     spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
0566 }
0567 
0568 static int a3700_spi_prepare_message(struct spi_master *master,
0569                      struct spi_message *message)
0570 {
0571     struct a3700_spi *a3700_spi = spi_master_get_devdata(master);
0572     struct spi_device *spi = message->spi;
0573     int ret;
0574 
0575     ret = clk_enable(a3700_spi->clk);
0576     if (ret) {
0577         dev_err(&spi->dev, "failed to enable clk with error %d\n", ret);
0578         return ret;
0579     }
0580 
0581     /* Flush the FIFOs */
0582     ret = a3700_spi_fifo_flush(a3700_spi);
0583     if (ret)
0584         return ret;
0585 
0586     a3700_spi_mode_set(a3700_spi, spi->mode);
0587 
0588     return 0;
0589 }
0590 
0591 static int a3700_spi_transfer_one_fifo(struct spi_master *master,
0592                   struct spi_device *spi,
0593                   struct spi_transfer *xfer)
0594 {
0595     struct a3700_spi *a3700_spi = spi_master_get_devdata(master);
0596     int ret = 0, timeout = A3700_SPI_TIMEOUT;
0597     unsigned int nbits = 0, byte_len;
0598     u32 val;
0599 
0600     /* Make sure we use FIFO mode */
0601     a3700_spi_fifo_mode_set(a3700_spi, true);
0602 
0603     /* Configure FIFO thresholds */
0604     byte_len = xfer->bits_per_word >> 3;
0605     a3700_spi_fifo_thres_set(a3700_spi, byte_len);
0606 
0607     if (xfer->tx_buf)
0608         nbits = xfer->tx_nbits;
0609     else if (xfer->rx_buf)
0610         nbits = xfer->rx_nbits;
0611 
0612     a3700_spi_pin_mode_set(a3700_spi, nbits, xfer->rx_buf ? true : false);
0613 
0614     /* Flush the FIFOs */
0615     a3700_spi_fifo_flush(a3700_spi);
0616 
0617     /* Transfer first bytes of data when buffer is not 4-byte aligned */
0618     a3700_spi_header_set(a3700_spi);
0619 
0620     if (xfer->rx_buf) {
0621         /* Clear WFIFO, since it's last 2 bytes are shifted out during
0622          * a read operation
0623          */
0624         spireg_write(a3700_spi, A3700_SPI_DATA_OUT_REG, 0);
0625 
0626         /* Set read data length */
0627         spireg_write(a3700_spi, A3700_SPI_IF_DIN_CNT_REG,
0628                  a3700_spi->buf_len);
0629         /* Start READ transfer */
0630         val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
0631         val &= ~A3700_SPI_RW_EN;
0632         val |= A3700_SPI_XFER_START;
0633         spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
0634     } else if (xfer->tx_buf) {
0635         /* Start Write transfer */
0636         val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
0637         val |= (A3700_SPI_XFER_START | A3700_SPI_RW_EN);
0638         spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
0639 
0640         /*
0641          * If there are data to be written to the SPI device, xmit_data
0642          * flag is set true; otherwise the instruction in SPI_INSTR does
0643          * not require data to be written to the SPI device, then
0644          * xmit_data flag is set false.
0645          */
0646         a3700_spi->xmit_data = (a3700_spi->buf_len != 0);
0647     }
0648 
0649     while (a3700_spi->buf_len) {
0650         if (a3700_spi->tx_buf) {
0651             /* Wait wfifo ready */
0652             if (!a3700_spi_transfer_wait(spi,
0653                              A3700_SPI_WFIFO_RDY)) {
0654                 dev_err(&spi->dev,
0655                     "wait wfifo ready timed out\n");
0656                 ret = -ETIMEDOUT;
0657                 goto error;
0658             }
0659             /* Fill up the wfifo */
0660             ret = a3700_spi_fifo_write(a3700_spi);
0661             if (ret)
0662                 goto error;
0663         } else if (a3700_spi->rx_buf) {
0664             /* Wait rfifo ready */
0665             if (!a3700_spi_transfer_wait(spi,
0666                              A3700_SPI_RFIFO_RDY)) {
0667                 dev_err(&spi->dev,
0668                     "wait rfifo ready timed out\n");
0669                 ret = -ETIMEDOUT;
0670                 goto error;
0671             }
0672             /* Drain out the rfifo */
0673             ret = a3700_spi_fifo_read(a3700_spi);
0674             if (ret)
0675                 goto error;
0676         }
0677     }
0678 
0679     /*
0680      * Stop a write transfer in fifo mode:
0681      *  - wait all the bytes in wfifo to be shifted out
0682      *   - set XFER_STOP bit
0683      *  - wait XFER_START bit clear
0684      *  - clear XFER_STOP bit
0685      * Stop a read transfer in fifo mode:
0686      *  - the hardware is to reset the XFER_START bit
0687      *     after the number of bytes indicated in DIN_CNT
0688      *     register
0689      *  - just wait XFER_START bit clear
0690      */
0691     if (a3700_spi->tx_buf) {
0692         if (a3700_spi->xmit_data) {
0693             /*
0694              * If there are data written to the SPI device, wait
0695              * until SPI_WFIFO_EMPTY is 1 to wait for all data to
0696              * transfer out of write FIFO.
0697              */
0698             if (!a3700_spi_transfer_wait(spi,
0699                              A3700_SPI_WFIFO_EMPTY)) {
0700                 dev_err(&spi->dev, "wait wfifo empty timed out\n");
0701                 return -ETIMEDOUT;
0702             }
0703         }
0704 
0705         if (!a3700_spi_transfer_wait(spi, A3700_SPI_XFER_RDY)) {
0706             dev_err(&spi->dev, "wait xfer ready timed out\n");
0707             return -ETIMEDOUT;
0708         }
0709 
0710         val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
0711         val |= A3700_SPI_XFER_STOP;
0712         spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
0713     }
0714 
0715     while (--timeout) {
0716         val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
0717         if (!(val & A3700_SPI_XFER_START))
0718             break;
0719         udelay(1);
0720     }
0721 
0722     if (timeout == 0) {
0723         dev_err(&spi->dev, "wait transfer start clear timed out\n");
0724         ret = -ETIMEDOUT;
0725         goto error;
0726     }
0727 
0728     val &= ~A3700_SPI_XFER_STOP;
0729     spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
0730     goto out;
0731 
0732 error:
0733     a3700_spi_transfer_abort_fifo(a3700_spi);
0734 out:
0735     spi_finalize_current_transfer(master);
0736 
0737     return ret;
0738 }
0739 
0740 static int a3700_spi_transfer_one_full_duplex(struct spi_master *master,
0741                   struct spi_device *spi,
0742                   struct spi_transfer *xfer)
0743 {
0744     struct a3700_spi *a3700_spi = spi_master_get_devdata(master);
0745     u32 val;
0746 
0747     /* Disable FIFO mode */
0748     a3700_spi_fifo_mode_set(a3700_spi, false);
0749 
0750     while (a3700_spi->buf_len) {
0751 
0752         /* When we have less than 4 bytes to transfer, switch to 1 byte
0753          * mode. This is reset after each transfer
0754          */
0755         if (a3700_spi->buf_len < 4)
0756             a3700_spi_bytelen_set(a3700_spi, 1);
0757 
0758         if (a3700_spi->byte_len == 1)
0759             val = *a3700_spi->tx_buf;
0760         else
0761             val = *(u32 *)a3700_spi->tx_buf;
0762 
0763         spireg_write(a3700_spi, A3700_SPI_DATA_OUT_REG, val);
0764 
0765         /* Wait for all the data to be shifted in / out */
0766         while (!(spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG) &
0767                 A3700_SPI_XFER_DONE))
0768             cpu_relax();
0769 
0770         val = spireg_read(a3700_spi, A3700_SPI_DATA_IN_REG);
0771 
0772         memcpy(a3700_spi->rx_buf, &val, a3700_spi->byte_len);
0773 
0774         a3700_spi->buf_len -= a3700_spi->byte_len;
0775         a3700_spi->tx_buf += a3700_spi->byte_len;
0776         a3700_spi->rx_buf += a3700_spi->byte_len;
0777 
0778     }
0779 
0780     spi_finalize_current_transfer(master);
0781 
0782     return 0;
0783 }
0784 
0785 static int a3700_spi_transfer_one(struct spi_master *master,
0786                   struct spi_device *spi,
0787                   struct spi_transfer *xfer)
0788 {
0789     a3700_spi_transfer_setup(spi, xfer);
0790 
0791     if (xfer->tx_buf && xfer->rx_buf)
0792         return a3700_spi_transfer_one_full_duplex(master, spi, xfer);
0793 
0794     return a3700_spi_transfer_one_fifo(master, spi, xfer);
0795 }
0796 
0797 static int a3700_spi_unprepare_message(struct spi_master *master,
0798                        struct spi_message *message)
0799 {
0800     struct a3700_spi *a3700_spi = spi_master_get_devdata(master);
0801 
0802     clk_disable(a3700_spi->clk);
0803 
0804     return 0;
0805 }
0806 
0807 static const struct of_device_id a3700_spi_dt_ids[] = {
0808     { .compatible = "marvell,armada-3700-spi", .data = NULL },
0809     {},
0810 };
0811 
0812 MODULE_DEVICE_TABLE(of, a3700_spi_dt_ids);
0813 
0814 static int a3700_spi_probe(struct platform_device *pdev)
0815 {
0816     struct device *dev = &pdev->dev;
0817     struct device_node *of_node = dev->of_node;
0818     struct spi_master *master;
0819     struct a3700_spi *spi;
0820     u32 num_cs = 0;
0821     int irq, ret = 0;
0822 
0823     master = spi_alloc_master(dev, sizeof(*spi));
0824     if (!master) {
0825         dev_err(dev, "master allocation failed\n");
0826         ret = -ENOMEM;
0827         goto out;
0828     }
0829 
0830     if (of_property_read_u32(of_node, "num-cs", &num_cs)) {
0831         dev_err(dev, "could not find num-cs\n");
0832         ret = -ENXIO;
0833         goto error;
0834     }
0835 
0836     master->bus_num = pdev->id;
0837     master->dev.of_node = of_node;
0838     master->mode_bits = SPI_MODE_3;
0839     master->num_chipselect = num_cs;
0840     master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(32);
0841     master->prepare_message =  a3700_spi_prepare_message;
0842     master->transfer_one = a3700_spi_transfer_one;
0843     master->unprepare_message = a3700_spi_unprepare_message;
0844     master->set_cs = a3700_spi_set_cs;
0845     master->mode_bits |= (SPI_RX_DUAL | SPI_TX_DUAL |
0846                   SPI_RX_QUAD | SPI_TX_QUAD);
0847 
0848     platform_set_drvdata(pdev, master);
0849 
0850     spi = spi_master_get_devdata(master);
0851 
0852     spi->master = master;
0853 
0854     spi->base = devm_platform_ioremap_resource(pdev, 0);
0855     if (IS_ERR(spi->base)) {
0856         ret = PTR_ERR(spi->base);
0857         goto error;
0858     }
0859 
0860     irq = platform_get_irq(pdev, 0);
0861     if (irq < 0) {
0862         ret = -ENXIO;
0863         goto error;
0864     }
0865     spi->irq = irq;
0866 
0867     init_completion(&spi->done);
0868 
0869     spi->clk = devm_clk_get(dev, NULL);
0870     if (IS_ERR(spi->clk)) {
0871         dev_err(dev, "could not find clk: %ld\n", PTR_ERR(spi->clk));
0872         goto error;
0873     }
0874 
0875     ret = clk_prepare(spi->clk);
0876     if (ret) {
0877         dev_err(dev, "could not prepare clk: %d\n", ret);
0878         goto error;
0879     }
0880 
0881     master->max_speed_hz = min_t(unsigned long, A3700_SPI_MAX_SPEED_HZ,
0882                     clk_get_rate(spi->clk));
0883     master->min_speed_hz = DIV_ROUND_UP(clk_get_rate(spi->clk),
0884                         A3700_SPI_MAX_PRESCALE);
0885 
0886     a3700_spi_init(spi);
0887 
0888     ret = devm_request_irq(dev, spi->irq, a3700_spi_interrupt, 0,
0889                    dev_name(dev), master);
0890     if (ret) {
0891         dev_err(dev, "could not request IRQ: %d\n", ret);
0892         goto error_clk;
0893     }
0894 
0895     ret = devm_spi_register_master(dev, master);
0896     if (ret) {
0897         dev_err(dev, "Failed to register master\n");
0898         goto error_clk;
0899     }
0900 
0901     return 0;
0902 
0903 error_clk:
0904     clk_unprepare(spi->clk);
0905 error:
0906     spi_master_put(master);
0907 out:
0908     return ret;
0909 }
0910 
0911 static int a3700_spi_remove(struct platform_device *pdev)
0912 {
0913     struct spi_master *master = platform_get_drvdata(pdev);
0914     struct a3700_spi *spi = spi_master_get_devdata(master);
0915 
0916     clk_unprepare(spi->clk);
0917 
0918     return 0;
0919 }
0920 
0921 static struct platform_driver a3700_spi_driver = {
0922     .driver = {
0923         .name   = DRIVER_NAME,
0924         .of_match_table = of_match_ptr(a3700_spi_dt_ids),
0925     },
0926     .probe      = a3700_spi_probe,
0927     .remove     = a3700_spi_remove,
0928 };
0929 
0930 module_platform_driver(a3700_spi_driver);
0931 
0932 MODULE_DESCRIPTION("Armada-3700 SPI driver");
0933 MODULE_AUTHOR("Wilson Ding <dingwei@marvell.com>");
0934 MODULE_LICENSE("GPL");
0935 MODULE_ALIAS("platform:" DRIVER_NAME);