Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2011-2015 Daniel Schwierzeck <daniel.schwierzeck@gmail.com>
0004  * Copyright (C) 2016 Hauke Mehrtens <hauke@hauke-m.de>
0005  */
0006 
0007 #include <linux/kernel.h>
0008 #include <linux/module.h>
0009 #include <linux/of_device.h>
0010 #include <linux/clk.h>
0011 #include <linux/io.h>
0012 #include <linux/delay.h>
0013 #include <linux/interrupt.h>
0014 #include <linux/sched.h>
0015 #include <linux/completion.h>
0016 #include <linux/spinlock.h>
0017 #include <linux/err.h>
0018 #include <linux/pm_runtime.h>
0019 #include <linux/spi/spi.h>
0020 
0021 #ifdef CONFIG_LANTIQ
0022 #include <lantiq_soc.h>
0023 #endif
0024 
0025 #define LTQ_SPI_RX_IRQ_NAME "spi_rx"
0026 #define LTQ_SPI_TX_IRQ_NAME "spi_tx"
0027 #define LTQ_SPI_ERR_IRQ_NAME    "spi_err"
0028 #define LTQ_SPI_FRM_IRQ_NAME    "spi_frm"
0029 
0030 #define LTQ_SPI_CLC     0x00
0031 #define LTQ_SPI_PISEL       0x04
0032 #define LTQ_SPI_ID      0x08
0033 #define LTQ_SPI_CON     0x10
0034 #define LTQ_SPI_STAT        0x14
0035 #define LTQ_SPI_WHBSTATE    0x18
0036 #define LTQ_SPI_TB      0x20
0037 #define LTQ_SPI_RB      0x24
0038 #define LTQ_SPI_RXFCON      0x30
0039 #define LTQ_SPI_TXFCON      0x34
0040 #define LTQ_SPI_FSTAT       0x38
0041 #define LTQ_SPI_BRT     0x40
0042 #define LTQ_SPI_BRSTAT      0x44
0043 #define LTQ_SPI_SFCON       0x60
0044 #define LTQ_SPI_SFSTAT      0x64
0045 #define LTQ_SPI_GPOCON      0x70
0046 #define LTQ_SPI_GPOSTAT     0x74
0047 #define LTQ_SPI_FPGO        0x78
0048 #define LTQ_SPI_RXREQ       0x80
0049 #define LTQ_SPI_RXCNT       0x84
0050 #define LTQ_SPI_DMACON      0xec
0051 #define LTQ_SPI_IRNEN       0xf4
0052 
0053 #define LTQ_SPI_CLC_SMC_S   16  /* Clock divider for sleep mode */
0054 #define LTQ_SPI_CLC_SMC_M   (0xFF << LTQ_SPI_CLC_SMC_S)
0055 #define LTQ_SPI_CLC_RMC_S   8   /* Clock divider for normal run mode */
0056 #define LTQ_SPI_CLC_RMC_M   (0xFF << LTQ_SPI_CLC_RMC_S)
0057 #define LTQ_SPI_CLC_DISS    BIT(1)  /* Disable status bit */
0058 #define LTQ_SPI_CLC_DISR    BIT(0)  /* Disable request bit */
0059 
0060 #define LTQ_SPI_ID_TXFS_S   24  /* Implemented TX FIFO size */
0061 #define LTQ_SPI_ID_RXFS_S   16  /* Implemented RX FIFO size */
0062 #define LTQ_SPI_ID_MOD_S    8   /* Module ID */
0063 #define LTQ_SPI_ID_MOD_M    (0xff << LTQ_SPI_ID_MOD_S)
0064 #define LTQ_SPI_ID_CFG_S    5   /* DMA interface support */
0065 #define LTQ_SPI_ID_CFG_M    (1 << LTQ_SPI_ID_CFG_S)
0066 #define LTQ_SPI_ID_REV_M    0x1F    /* Hardware revision number */
0067 
0068 #define LTQ_SPI_CON_BM_S    16  /* Data width selection */
0069 #define LTQ_SPI_CON_BM_M    (0x1F << LTQ_SPI_CON_BM_S)
0070 #define LTQ_SPI_CON_EM      BIT(24) /* Echo mode */
0071 #define LTQ_SPI_CON_IDLE    BIT(23) /* Idle bit value */
0072 #define LTQ_SPI_CON_ENBV    BIT(22) /* Enable byte valid control */
0073 #define LTQ_SPI_CON_RUEN    BIT(12) /* Receive underflow error enable */
0074 #define LTQ_SPI_CON_TUEN    BIT(11) /* Transmit underflow error enable */
0075 #define LTQ_SPI_CON_AEN     BIT(10) /* Abort error enable */
0076 #define LTQ_SPI_CON_REN     BIT(9)  /* Receive overflow error enable */
0077 #define LTQ_SPI_CON_TEN     BIT(8)  /* Transmit overflow error enable */
0078 #define LTQ_SPI_CON_LB      BIT(7)  /* Loopback control */
0079 #define LTQ_SPI_CON_PO      BIT(6)  /* Clock polarity control */
0080 #define LTQ_SPI_CON_PH      BIT(5)  /* Clock phase control */
0081 #define LTQ_SPI_CON_HB      BIT(4)  /* Heading control */
0082 #define LTQ_SPI_CON_RXOFF   BIT(1)  /* Switch receiver off */
0083 #define LTQ_SPI_CON_TXOFF   BIT(0)  /* Switch transmitter off */
0084 
0085 #define LTQ_SPI_STAT_RXBV_S 28
0086 #define LTQ_SPI_STAT_RXBV_M (0x7 << LTQ_SPI_STAT_RXBV_S)
0087 #define LTQ_SPI_STAT_BSY    BIT(13) /* Busy flag */
0088 #define LTQ_SPI_STAT_RUE    BIT(12) /* Receive underflow error flag */
0089 #define LTQ_SPI_STAT_TUE    BIT(11) /* Transmit underflow error flag */
0090 #define LTQ_SPI_STAT_AE     BIT(10) /* Abort error flag */
0091 #define LTQ_SPI_STAT_RE     BIT(9)  /* Receive error flag */
0092 #define LTQ_SPI_STAT_TE     BIT(8)  /* Transmit error flag */
0093 #define LTQ_SPI_STAT_ME     BIT(7)  /* Mode error flag */
0094 #define LTQ_SPI_STAT_MS     BIT(1)  /* Master/slave select bit */
0095 #define LTQ_SPI_STAT_EN     BIT(0)  /* Enable bit */
0096 #define LTQ_SPI_STAT_ERRORS (LTQ_SPI_STAT_ME | LTQ_SPI_STAT_TE | \
0097                  LTQ_SPI_STAT_RE | LTQ_SPI_STAT_AE | \
0098                  LTQ_SPI_STAT_TUE | LTQ_SPI_STAT_RUE)
0099 
0100 #define LTQ_SPI_WHBSTATE_SETTUE BIT(15) /* Set transmit underflow error flag */
0101 #define LTQ_SPI_WHBSTATE_SETAE  BIT(14) /* Set abort error flag */
0102 #define LTQ_SPI_WHBSTATE_SETRE  BIT(13) /* Set receive error flag */
0103 #define LTQ_SPI_WHBSTATE_SETTE  BIT(12) /* Set transmit error flag */
0104 #define LTQ_SPI_WHBSTATE_CLRTUE BIT(11) /* Clear transmit underflow error flag */
0105 #define LTQ_SPI_WHBSTATE_CLRAE  BIT(10) /* Clear abort error flag */
0106 #define LTQ_SPI_WHBSTATE_CLRRE  BIT(9)  /* Clear receive error flag */
0107 #define LTQ_SPI_WHBSTATE_CLRTE  BIT(8)  /* Clear transmit error flag */
0108 #define LTQ_SPI_WHBSTATE_SETME  BIT(7)  /* Set mode error flag */
0109 #define LTQ_SPI_WHBSTATE_CLRME  BIT(6)  /* Clear mode error flag */
0110 #define LTQ_SPI_WHBSTATE_SETRUE BIT(5)  /* Set receive underflow error flag */
0111 #define LTQ_SPI_WHBSTATE_CLRRUE BIT(4)  /* Clear receive underflow error flag */
0112 #define LTQ_SPI_WHBSTATE_SETMS  BIT(3)  /* Set master select bit */
0113 #define LTQ_SPI_WHBSTATE_CLRMS  BIT(2)  /* Clear master select bit */
0114 #define LTQ_SPI_WHBSTATE_SETEN  BIT(1)  /* Set enable bit (operational mode) */
0115 #define LTQ_SPI_WHBSTATE_CLREN  BIT(0)  /* Clear enable bit (config mode */
0116 #define LTQ_SPI_WHBSTATE_CLR_ERRORS (LTQ_SPI_WHBSTATE_CLRRUE | \
0117                      LTQ_SPI_WHBSTATE_CLRME | \
0118                      LTQ_SPI_WHBSTATE_CLRTE | \
0119                      LTQ_SPI_WHBSTATE_CLRRE | \
0120                      LTQ_SPI_WHBSTATE_CLRAE | \
0121                      LTQ_SPI_WHBSTATE_CLRTUE)
0122 
0123 #define LTQ_SPI_RXFCON_RXFITL_S 8   /* FIFO interrupt trigger level */
0124 #define LTQ_SPI_RXFCON_RXFLU    BIT(1)  /* FIFO flush */
0125 #define LTQ_SPI_RXFCON_RXFEN    BIT(0)  /* FIFO enable */
0126 
0127 #define LTQ_SPI_TXFCON_TXFITL_S 8   /* FIFO interrupt trigger level */
0128 #define LTQ_SPI_TXFCON_TXFLU    BIT(1)  /* FIFO flush */
0129 #define LTQ_SPI_TXFCON_TXFEN    BIT(0)  /* FIFO enable */
0130 
0131 #define LTQ_SPI_FSTAT_RXFFL_S   0
0132 #define LTQ_SPI_FSTAT_TXFFL_S   8
0133 
0134 #define LTQ_SPI_GPOCON_ISCSBN_S 8
0135 #define LTQ_SPI_GPOCON_INVOUTN_S    0
0136 
0137 #define LTQ_SPI_FGPO_SETOUTN_S  8
0138 #define LTQ_SPI_FGPO_CLROUTN_S  0
0139 
0140 #define LTQ_SPI_RXREQ_RXCNT_M   0xFFFF  /* Receive count value */
0141 #define LTQ_SPI_RXCNT_TODO_M    0xFFFF  /* Recevie to-do value */
0142 
0143 #define LTQ_SPI_IRNEN_TFI   BIT(4)  /* TX finished interrupt */
0144 #define LTQ_SPI_IRNEN_F     BIT(3)  /* Frame end interrupt request */
0145 #define LTQ_SPI_IRNEN_E     BIT(2)  /* Error end interrupt request */
0146 #define LTQ_SPI_IRNEN_T_XWAY    BIT(1)  /* Transmit end interrupt request */
0147 #define LTQ_SPI_IRNEN_R_XWAY    BIT(0)  /* Receive end interrupt request */
0148 #define LTQ_SPI_IRNEN_R_XRX BIT(1)  /* Transmit end interrupt request */
0149 #define LTQ_SPI_IRNEN_T_XRX BIT(0)  /* Receive end interrupt request */
0150 #define LTQ_SPI_IRNEN_ALL   0x1F
0151 
0152 struct lantiq_ssc_spi;
0153 
0154 struct lantiq_ssc_hwcfg {
0155     int (*cfg_irq)(struct platform_device *pdev, struct lantiq_ssc_spi *spi);
0156     unsigned int    irnen_r;
0157     unsigned int    irnen_t;
0158     unsigned int    irncr;
0159     unsigned int    irnicr;
0160     bool        irq_ack;
0161     u32     fifo_size_mask;
0162 };
0163 
0164 struct lantiq_ssc_spi {
0165     struct spi_master       *master;
0166     struct device           *dev;
0167     void __iomem            *regbase;
0168     struct clk          *spi_clk;
0169     struct clk          *fpi_clk;
0170     const struct lantiq_ssc_hwcfg   *hwcfg;
0171 
0172     spinlock_t          lock;
0173     struct workqueue_struct     *wq;
0174     struct work_struct      work;
0175 
0176     const u8            *tx;
0177     u8              *rx;
0178     unsigned int            tx_todo;
0179     unsigned int            rx_todo;
0180     unsigned int            bits_per_word;
0181     unsigned int            speed_hz;
0182     unsigned int            tx_fifo_size;
0183     unsigned int            rx_fifo_size;
0184     unsigned int            base_cs;
0185     unsigned int            fdx_tx_level;
0186 };
0187 
0188 static u32 lantiq_ssc_readl(const struct lantiq_ssc_spi *spi, u32 reg)
0189 {
0190     return __raw_readl(spi->regbase + reg);
0191 }
0192 
0193 static void lantiq_ssc_writel(const struct lantiq_ssc_spi *spi, u32 val,
0194                   u32 reg)
0195 {
0196     __raw_writel(val, spi->regbase + reg);
0197 }
0198 
0199 static void lantiq_ssc_maskl(const struct lantiq_ssc_spi *spi, u32 clr,
0200                  u32 set, u32 reg)
0201 {
0202     u32 val = __raw_readl(spi->regbase + reg);
0203 
0204     val &= ~clr;
0205     val |= set;
0206     __raw_writel(val, spi->regbase + reg);
0207 }
0208 
0209 static unsigned int tx_fifo_level(const struct lantiq_ssc_spi *spi)
0210 {
0211     const struct lantiq_ssc_hwcfg *hwcfg = spi->hwcfg;
0212     u32 fstat = lantiq_ssc_readl(spi, LTQ_SPI_FSTAT);
0213 
0214     return (fstat >> LTQ_SPI_FSTAT_TXFFL_S) & hwcfg->fifo_size_mask;
0215 }
0216 
0217 static unsigned int rx_fifo_level(const struct lantiq_ssc_spi *spi)
0218 {
0219     const struct lantiq_ssc_hwcfg *hwcfg = spi->hwcfg;
0220     u32 fstat = lantiq_ssc_readl(spi, LTQ_SPI_FSTAT);
0221 
0222     return (fstat >> LTQ_SPI_FSTAT_RXFFL_S) & hwcfg->fifo_size_mask;
0223 }
0224 
0225 static unsigned int tx_fifo_free(const struct lantiq_ssc_spi *spi)
0226 {
0227     return spi->tx_fifo_size - tx_fifo_level(spi);
0228 }
0229 
0230 static void rx_fifo_reset(const struct lantiq_ssc_spi *spi)
0231 {
0232     u32 val = spi->rx_fifo_size << LTQ_SPI_RXFCON_RXFITL_S;
0233 
0234     val |= LTQ_SPI_RXFCON_RXFEN | LTQ_SPI_RXFCON_RXFLU;
0235     lantiq_ssc_writel(spi, val, LTQ_SPI_RXFCON);
0236 }
0237 
0238 static void tx_fifo_reset(const struct lantiq_ssc_spi *spi)
0239 {
0240     u32 val = 1 << LTQ_SPI_TXFCON_TXFITL_S;
0241 
0242     val |= LTQ_SPI_TXFCON_TXFEN | LTQ_SPI_TXFCON_TXFLU;
0243     lantiq_ssc_writel(spi, val, LTQ_SPI_TXFCON);
0244 }
0245 
0246 static void rx_fifo_flush(const struct lantiq_ssc_spi *spi)
0247 {
0248     lantiq_ssc_maskl(spi, 0, LTQ_SPI_RXFCON_RXFLU, LTQ_SPI_RXFCON);
0249 }
0250 
0251 static void tx_fifo_flush(const struct lantiq_ssc_spi *spi)
0252 {
0253     lantiq_ssc_maskl(spi, 0, LTQ_SPI_TXFCON_TXFLU, LTQ_SPI_TXFCON);
0254 }
0255 
0256 static void hw_enter_config_mode(const struct lantiq_ssc_spi *spi)
0257 {
0258     lantiq_ssc_writel(spi, LTQ_SPI_WHBSTATE_CLREN, LTQ_SPI_WHBSTATE);
0259 }
0260 
0261 static void hw_enter_active_mode(const struct lantiq_ssc_spi *spi)
0262 {
0263     lantiq_ssc_writel(spi, LTQ_SPI_WHBSTATE_SETEN, LTQ_SPI_WHBSTATE);
0264 }
0265 
0266 static void hw_setup_speed_hz(const struct lantiq_ssc_spi *spi,
0267                   unsigned int max_speed_hz)
0268 {
0269     u32 spi_clk, brt;
0270 
0271     /*
0272      * SPI module clock is derived from FPI bus clock dependent on
0273      * divider value in CLC.RMS which is always set to 1.
0274      *
0275      *                 f_SPI
0276      * baudrate = --------------
0277      *             2 * (BR + 1)
0278      */
0279     spi_clk = clk_get_rate(spi->fpi_clk) / 2;
0280 
0281     if (max_speed_hz > spi_clk)
0282         brt = 0;
0283     else
0284         brt = spi_clk / max_speed_hz - 1;
0285 
0286     if (brt > 0xFFFF)
0287         brt = 0xFFFF;
0288 
0289     dev_dbg(spi->dev, "spi_clk %u, max_speed_hz %u, brt %u\n",
0290         spi_clk, max_speed_hz, brt);
0291 
0292     lantiq_ssc_writel(spi, brt, LTQ_SPI_BRT);
0293 }
0294 
0295 static void hw_setup_bits_per_word(const struct lantiq_ssc_spi *spi,
0296                    unsigned int bits_per_word)
0297 {
0298     u32 bm;
0299 
0300     /* CON.BM value = bits_per_word - 1 */
0301     bm = (bits_per_word - 1) << LTQ_SPI_CON_BM_S;
0302 
0303     lantiq_ssc_maskl(spi, LTQ_SPI_CON_BM_M, bm, LTQ_SPI_CON);
0304 }
0305 
0306 static void hw_setup_clock_mode(const struct lantiq_ssc_spi *spi,
0307                 unsigned int mode)
0308 {
0309     u32 con_set = 0, con_clr = 0;
0310 
0311     /*
0312      * SPI mode mapping in CON register:
0313      * Mode CPOL CPHA CON.PO CON.PH
0314      *  0    0    0      0      1
0315      *  1    0    1      0      0
0316      *  2    1    0      1      1
0317      *  3    1    1      1      0
0318      */
0319     if (mode & SPI_CPHA)
0320         con_clr |= LTQ_SPI_CON_PH;
0321     else
0322         con_set |= LTQ_SPI_CON_PH;
0323 
0324     if (mode & SPI_CPOL)
0325         con_set |= LTQ_SPI_CON_PO | LTQ_SPI_CON_IDLE;
0326     else
0327         con_clr |= LTQ_SPI_CON_PO | LTQ_SPI_CON_IDLE;
0328 
0329     /* Set heading control */
0330     if (mode & SPI_LSB_FIRST)
0331         con_clr |= LTQ_SPI_CON_HB;
0332     else
0333         con_set |= LTQ_SPI_CON_HB;
0334 
0335     /* Set loopback mode */
0336     if (mode & SPI_LOOP)
0337         con_set |= LTQ_SPI_CON_LB;
0338     else
0339         con_clr |= LTQ_SPI_CON_LB;
0340 
0341     lantiq_ssc_maskl(spi, con_clr, con_set, LTQ_SPI_CON);
0342 }
0343 
0344 static void lantiq_ssc_hw_init(const struct lantiq_ssc_spi *spi)
0345 {
0346     const struct lantiq_ssc_hwcfg *hwcfg = spi->hwcfg;
0347 
0348     /*
0349      * Set clock divider for run mode to 1 to
0350      * run at same frequency as FPI bus
0351      */
0352     lantiq_ssc_writel(spi, 1 << LTQ_SPI_CLC_RMC_S, LTQ_SPI_CLC);
0353 
0354     /* Put controller into config mode */
0355     hw_enter_config_mode(spi);
0356 
0357     /* Clear error flags */
0358     lantiq_ssc_maskl(spi, 0, LTQ_SPI_WHBSTATE_CLR_ERRORS, LTQ_SPI_WHBSTATE);
0359 
0360     /* Enable error checking, disable TX/RX */
0361     lantiq_ssc_writel(spi, LTQ_SPI_CON_RUEN | LTQ_SPI_CON_AEN |
0362         LTQ_SPI_CON_TEN | LTQ_SPI_CON_REN | LTQ_SPI_CON_TXOFF |
0363         LTQ_SPI_CON_RXOFF, LTQ_SPI_CON);
0364 
0365     /* Setup default SPI mode */
0366     hw_setup_bits_per_word(spi, spi->bits_per_word);
0367     hw_setup_clock_mode(spi, SPI_MODE_0);
0368 
0369     /* Enable master mode and clear error flags */
0370     lantiq_ssc_writel(spi, LTQ_SPI_WHBSTATE_SETMS |
0371                    LTQ_SPI_WHBSTATE_CLR_ERRORS,
0372                    LTQ_SPI_WHBSTATE);
0373 
0374     /* Reset GPIO/CS registers */
0375     lantiq_ssc_writel(spi, 0, LTQ_SPI_GPOCON);
0376     lantiq_ssc_writel(spi, 0xFF00, LTQ_SPI_FPGO);
0377 
0378     /* Enable and flush FIFOs */
0379     rx_fifo_reset(spi);
0380     tx_fifo_reset(spi);
0381 
0382     /* Enable interrupts */
0383     lantiq_ssc_writel(spi, hwcfg->irnen_t | hwcfg->irnen_r |
0384               LTQ_SPI_IRNEN_E, LTQ_SPI_IRNEN);
0385 }
0386 
0387 static int lantiq_ssc_setup(struct spi_device *spidev)
0388 {
0389     struct spi_master *master = spidev->master;
0390     struct lantiq_ssc_spi *spi = spi_master_get_devdata(master);
0391     unsigned int cs = spidev->chip_select;
0392     u32 gpocon;
0393 
0394     /* GPIOs are used for CS */
0395     if (spidev->cs_gpiod)
0396         return 0;
0397 
0398     dev_dbg(spi->dev, "using internal chipselect %u\n", cs);
0399 
0400     if (cs < spi->base_cs) {
0401         dev_err(spi->dev,
0402             "chipselect %i too small (min %i)\n", cs, spi->base_cs);
0403         return -EINVAL;
0404     }
0405 
0406     /* set GPO pin to CS mode */
0407     gpocon = 1 << ((cs - spi->base_cs) + LTQ_SPI_GPOCON_ISCSBN_S);
0408 
0409     /* invert GPO pin */
0410     if (spidev->mode & SPI_CS_HIGH)
0411         gpocon |= 1 << (cs - spi->base_cs);
0412 
0413     lantiq_ssc_maskl(spi, 0, gpocon, LTQ_SPI_GPOCON);
0414 
0415     return 0;
0416 }
0417 
0418 static int lantiq_ssc_prepare_message(struct spi_master *master,
0419                       struct spi_message *message)
0420 {
0421     struct lantiq_ssc_spi *spi = spi_master_get_devdata(master);
0422 
0423     hw_enter_config_mode(spi);
0424     hw_setup_clock_mode(spi, message->spi->mode);
0425     hw_enter_active_mode(spi);
0426 
0427     return 0;
0428 }
0429 
0430 static void hw_setup_transfer(struct lantiq_ssc_spi *spi,
0431                   struct spi_device *spidev, struct spi_transfer *t)
0432 {
0433     unsigned int speed_hz = t->speed_hz;
0434     unsigned int bits_per_word = t->bits_per_word;
0435     u32 con;
0436 
0437     if (bits_per_word != spi->bits_per_word ||
0438         speed_hz != spi->speed_hz) {
0439         hw_enter_config_mode(spi);
0440         hw_setup_speed_hz(spi, speed_hz);
0441         hw_setup_bits_per_word(spi, bits_per_word);
0442         hw_enter_active_mode(spi);
0443 
0444         spi->speed_hz = speed_hz;
0445         spi->bits_per_word = bits_per_word;
0446     }
0447 
0448     /* Configure transmitter and receiver */
0449     con = lantiq_ssc_readl(spi, LTQ_SPI_CON);
0450     if (t->tx_buf)
0451         con &= ~LTQ_SPI_CON_TXOFF;
0452     else
0453         con |= LTQ_SPI_CON_TXOFF;
0454 
0455     if (t->rx_buf)
0456         con &= ~LTQ_SPI_CON_RXOFF;
0457     else
0458         con |= LTQ_SPI_CON_RXOFF;
0459 
0460     lantiq_ssc_writel(spi, con, LTQ_SPI_CON);
0461 }
0462 
0463 static int lantiq_ssc_unprepare_message(struct spi_master *master,
0464                     struct spi_message *message)
0465 {
0466     struct lantiq_ssc_spi *spi = spi_master_get_devdata(master);
0467 
0468     flush_workqueue(spi->wq);
0469 
0470     /* Disable transmitter and receiver while idle */
0471     lantiq_ssc_maskl(spi, 0, LTQ_SPI_CON_TXOFF | LTQ_SPI_CON_RXOFF,
0472              LTQ_SPI_CON);
0473 
0474     return 0;
0475 }
0476 
0477 static void tx_fifo_write(struct lantiq_ssc_spi *spi)
0478 {
0479     const u8 *tx8;
0480     const u16 *tx16;
0481     const u32 *tx32;
0482     u32 data;
0483     unsigned int tx_free = tx_fifo_free(spi);
0484 
0485     spi->fdx_tx_level = 0;
0486     while (spi->tx_todo && tx_free) {
0487         switch (spi->bits_per_word) {
0488         case 2 ... 8:
0489             tx8 = spi->tx;
0490             data = *tx8;
0491             spi->tx_todo--;
0492             spi->tx++;
0493             break;
0494         case 16:
0495             tx16 = (u16 *) spi->tx;
0496             data = *tx16;
0497             spi->tx_todo -= 2;
0498             spi->tx += 2;
0499             break;
0500         case 32:
0501             tx32 = (u32 *) spi->tx;
0502             data = *tx32;
0503             spi->tx_todo -= 4;
0504             spi->tx += 4;
0505             break;
0506         default:
0507             WARN_ON(1);
0508             data = 0;
0509             break;
0510         }
0511 
0512         lantiq_ssc_writel(spi, data, LTQ_SPI_TB);
0513         tx_free--;
0514         spi->fdx_tx_level++;
0515     }
0516 }
0517 
0518 static void rx_fifo_read_full_duplex(struct lantiq_ssc_spi *spi)
0519 {
0520     u8 *rx8;
0521     u16 *rx16;
0522     u32 *rx32;
0523     u32 data;
0524     unsigned int rx_fill = rx_fifo_level(spi);
0525 
0526     /*
0527      * Wait until all expected data to be shifted in.
0528      * Otherwise, rx overrun may occur.
0529      */
0530     while (rx_fill != spi->fdx_tx_level)
0531         rx_fill = rx_fifo_level(spi);
0532 
0533     while (rx_fill) {
0534         data = lantiq_ssc_readl(spi, LTQ_SPI_RB);
0535 
0536         switch (spi->bits_per_word) {
0537         case 2 ... 8:
0538             rx8 = spi->rx;
0539             *rx8 = data;
0540             spi->rx_todo--;
0541             spi->rx++;
0542             break;
0543         case 16:
0544             rx16 = (u16 *) spi->rx;
0545             *rx16 = data;
0546             spi->rx_todo -= 2;
0547             spi->rx += 2;
0548             break;
0549         case 32:
0550             rx32 = (u32 *) spi->rx;
0551             *rx32 = data;
0552             spi->rx_todo -= 4;
0553             spi->rx += 4;
0554             break;
0555         default:
0556             WARN_ON(1);
0557             break;
0558         }
0559 
0560         rx_fill--;
0561     }
0562 }
0563 
0564 static void rx_fifo_read_half_duplex(struct lantiq_ssc_spi *spi)
0565 {
0566     u32 data, *rx32;
0567     u8 *rx8;
0568     unsigned int rxbv, shift;
0569     unsigned int rx_fill = rx_fifo_level(spi);
0570 
0571     /*
0572      * In RX-only mode the bits per word value is ignored by HW. A value
0573      * of 32 is used instead. Thus all 4 bytes per FIFO must be read.
0574      * If remaining RX bytes are less than 4, the FIFO must be read
0575      * differently. The amount of received and valid bytes is indicated
0576      * by STAT.RXBV register value.
0577      */
0578     while (rx_fill) {
0579         if (spi->rx_todo < 4)  {
0580             rxbv = (lantiq_ssc_readl(spi, LTQ_SPI_STAT) &
0581                 LTQ_SPI_STAT_RXBV_M) >> LTQ_SPI_STAT_RXBV_S;
0582             data = lantiq_ssc_readl(spi, LTQ_SPI_RB);
0583 
0584             shift = (rxbv - 1) * 8;
0585             rx8 = spi->rx;
0586 
0587             while (rxbv) {
0588                 *rx8++ = (data >> shift) & 0xFF;
0589                 rxbv--;
0590                 shift -= 8;
0591                 spi->rx_todo--;
0592                 spi->rx++;
0593             }
0594         } else {
0595             data = lantiq_ssc_readl(spi, LTQ_SPI_RB);
0596             rx32 = (u32 *) spi->rx;
0597 
0598             *rx32++ = data;
0599             spi->rx_todo -= 4;
0600             spi->rx += 4;
0601         }
0602         rx_fill--;
0603     }
0604 }
0605 
0606 static void rx_request(struct lantiq_ssc_spi *spi)
0607 {
0608     unsigned int rxreq, rxreq_max;
0609 
0610     /*
0611      * To avoid receive overflows at high clocks it is better to request
0612      * only the amount of bytes that fits into all FIFOs. This value
0613      * depends on the FIFO size implemented in hardware.
0614      */
0615     rxreq = spi->rx_todo;
0616     rxreq_max = spi->rx_fifo_size * 4;
0617     if (rxreq > rxreq_max)
0618         rxreq = rxreq_max;
0619 
0620     lantiq_ssc_writel(spi, rxreq, LTQ_SPI_RXREQ);
0621 }
0622 
0623 static irqreturn_t lantiq_ssc_xmit_interrupt(int irq, void *data)
0624 {
0625     struct lantiq_ssc_spi *spi = data;
0626     const struct lantiq_ssc_hwcfg *hwcfg = spi->hwcfg;
0627     u32 val = lantiq_ssc_readl(spi, hwcfg->irncr);
0628 
0629     spin_lock(&spi->lock);
0630     if (hwcfg->irq_ack)
0631         lantiq_ssc_writel(spi, val, hwcfg->irncr);
0632 
0633     if (spi->tx) {
0634         if (spi->rx && spi->rx_todo)
0635             rx_fifo_read_full_duplex(spi);
0636 
0637         if (spi->tx_todo)
0638             tx_fifo_write(spi);
0639         else if (!tx_fifo_level(spi))
0640             goto completed;
0641     } else if (spi->rx) {
0642         if (spi->rx_todo) {
0643             rx_fifo_read_half_duplex(spi);
0644 
0645             if (spi->rx_todo)
0646                 rx_request(spi);
0647             else
0648                 goto completed;
0649         } else {
0650             goto completed;
0651         }
0652     }
0653 
0654     spin_unlock(&spi->lock);
0655     return IRQ_HANDLED;
0656 
0657 completed:
0658     queue_work(spi->wq, &spi->work);
0659     spin_unlock(&spi->lock);
0660 
0661     return IRQ_HANDLED;
0662 }
0663 
0664 static irqreturn_t lantiq_ssc_err_interrupt(int irq, void *data)
0665 {
0666     struct lantiq_ssc_spi *spi = data;
0667     const struct lantiq_ssc_hwcfg *hwcfg = spi->hwcfg;
0668     u32 stat = lantiq_ssc_readl(spi, LTQ_SPI_STAT);
0669     u32 val = lantiq_ssc_readl(spi, hwcfg->irncr);
0670 
0671     if (!(stat & LTQ_SPI_STAT_ERRORS))
0672         return IRQ_NONE;
0673 
0674     spin_lock(&spi->lock);
0675     if (hwcfg->irq_ack)
0676         lantiq_ssc_writel(spi, val, hwcfg->irncr);
0677 
0678     if (stat & LTQ_SPI_STAT_RUE)
0679         dev_err(spi->dev, "receive underflow error\n");
0680     if (stat & LTQ_SPI_STAT_TUE)
0681         dev_err(spi->dev, "transmit underflow error\n");
0682     if (stat & LTQ_SPI_STAT_AE)
0683         dev_err(spi->dev, "abort error\n");
0684     if (stat & LTQ_SPI_STAT_RE)
0685         dev_err(spi->dev, "receive overflow error\n");
0686     if (stat & LTQ_SPI_STAT_TE)
0687         dev_err(spi->dev, "transmit overflow error\n");
0688     if (stat & LTQ_SPI_STAT_ME)
0689         dev_err(spi->dev, "mode error\n");
0690 
0691     /* Clear error flags */
0692     lantiq_ssc_maskl(spi, 0, LTQ_SPI_WHBSTATE_CLR_ERRORS, LTQ_SPI_WHBSTATE);
0693 
0694     /* set bad status so it can be retried */
0695     if (spi->master->cur_msg)
0696         spi->master->cur_msg->status = -EIO;
0697     queue_work(spi->wq, &spi->work);
0698     spin_unlock(&spi->lock);
0699 
0700     return IRQ_HANDLED;
0701 }
0702 
0703 static irqreturn_t intel_lgm_ssc_isr(int irq, void *data)
0704 {
0705     struct lantiq_ssc_spi *spi = data;
0706     const struct lantiq_ssc_hwcfg *hwcfg = spi->hwcfg;
0707     u32 val = lantiq_ssc_readl(spi, hwcfg->irncr);
0708 
0709     if (!(val & LTQ_SPI_IRNEN_ALL))
0710         return IRQ_NONE;
0711 
0712     if (val & LTQ_SPI_IRNEN_E)
0713         return lantiq_ssc_err_interrupt(irq, data);
0714 
0715     if ((val & hwcfg->irnen_t) || (val & hwcfg->irnen_r))
0716         return lantiq_ssc_xmit_interrupt(irq, data);
0717 
0718     return IRQ_HANDLED;
0719 }
0720 
0721 static int transfer_start(struct lantiq_ssc_spi *spi, struct spi_device *spidev,
0722               struct spi_transfer *t)
0723 {
0724     unsigned long flags;
0725 
0726     spin_lock_irqsave(&spi->lock, flags);
0727 
0728     spi->tx = t->tx_buf;
0729     spi->rx = t->rx_buf;
0730 
0731     if (t->tx_buf) {
0732         spi->tx_todo = t->len;
0733 
0734         /* initially fill TX FIFO */
0735         tx_fifo_write(spi);
0736     }
0737 
0738     if (spi->rx) {
0739         spi->rx_todo = t->len;
0740 
0741         /* start shift clock in RX-only mode */
0742         if (!spi->tx)
0743             rx_request(spi);
0744     }
0745 
0746     spin_unlock_irqrestore(&spi->lock, flags);
0747 
0748     return t->len;
0749 }
0750 
0751 /*
0752  * The driver only gets an interrupt when the FIFO is empty, but there
0753  * is an additional shift register from which the data is written to
0754  * the wire. We get the last interrupt when the controller starts to
0755  * write the last word to the wire, not when it is finished. Do busy
0756  * waiting till it finishes.
0757  */
0758 static void lantiq_ssc_bussy_work(struct work_struct *work)
0759 {
0760     struct lantiq_ssc_spi *spi;
0761     unsigned long long timeout = 8LL * 1000LL;
0762     unsigned long end;
0763 
0764     spi = container_of(work, typeof(*spi), work);
0765 
0766     do_div(timeout, spi->speed_hz);
0767     timeout += timeout + 100; /* some tolerance */
0768 
0769     end = jiffies + msecs_to_jiffies(timeout);
0770     do {
0771         u32 stat = lantiq_ssc_readl(spi, LTQ_SPI_STAT);
0772 
0773         if (!(stat & LTQ_SPI_STAT_BSY)) {
0774             spi_finalize_current_transfer(spi->master);
0775             return;
0776         }
0777 
0778         cond_resched();
0779     } while (!time_after_eq(jiffies, end));
0780 
0781     if (spi->master->cur_msg)
0782         spi->master->cur_msg->status = -EIO;
0783     spi_finalize_current_transfer(spi->master);
0784 }
0785 
0786 static void lantiq_ssc_handle_err(struct spi_master *master,
0787                   struct spi_message *message)
0788 {
0789     struct lantiq_ssc_spi *spi = spi_master_get_devdata(master);
0790 
0791     /* flush FIFOs on timeout */
0792     rx_fifo_flush(spi);
0793     tx_fifo_flush(spi);
0794 }
0795 
0796 static void lantiq_ssc_set_cs(struct spi_device *spidev, bool enable)
0797 {
0798     struct lantiq_ssc_spi *spi = spi_master_get_devdata(spidev->master);
0799     unsigned int cs = spidev->chip_select;
0800     u32 fgpo;
0801 
0802     if (!!(spidev->mode & SPI_CS_HIGH) == enable)
0803         fgpo = (1 << (cs - spi->base_cs));
0804     else
0805         fgpo = (1 << (cs - spi->base_cs + LTQ_SPI_FGPO_SETOUTN_S));
0806 
0807     lantiq_ssc_writel(spi, fgpo, LTQ_SPI_FPGO);
0808 }
0809 
0810 static int lantiq_ssc_transfer_one(struct spi_master *master,
0811                    struct spi_device *spidev,
0812                    struct spi_transfer *t)
0813 {
0814     struct lantiq_ssc_spi *spi = spi_master_get_devdata(master);
0815 
0816     hw_setup_transfer(spi, spidev, t);
0817 
0818     return transfer_start(spi, spidev, t);
0819 }
0820 
0821 static int intel_lgm_cfg_irq(struct platform_device *pdev, struct lantiq_ssc_spi *spi)
0822 {
0823     int irq;
0824 
0825     irq = platform_get_irq(pdev, 0);
0826     if (irq < 0)
0827         return irq;
0828 
0829     return devm_request_irq(&pdev->dev, irq, intel_lgm_ssc_isr, 0, "spi", spi);
0830 }
0831 
0832 static int lantiq_cfg_irq(struct platform_device *pdev, struct lantiq_ssc_spi *spi)
0833 {
0834     int irq, err;
0835 
0836     irq = platform_get_irq_byname(pdev, LTQ_SPI_RX_IRQ_NAME);
0837     if (irq < 0)
0838         return irq;
0839 
0840     err = devm_request_irq(&pdev->dev, irq, lantiq_ssc_xmit_interrupt,
0841                    0, LTQ_SPI_RX_IRQ_NAME, spi);
0842     if (err)
0843         return err;
0844 
0845     irq = platform_get_irq_byname(pdev, LTQ_SPI_TX_IRQ_NAME);
0846     if (irq < 0)
0847         return irq;
0848 
0849     err = devm_request_irq(&pdev->dev, irq, lantiq_ssc_xmit_interrupt,
0850                    0, LTQ_SPI_TX_IRQ_NAME, spi);
0851 
0852     if (err)
0853         return err;
0854 
0855     irq = platform_get_irq_byname(pdev, LTQ_SPI_ERR_IRQ_NAME);
0856     if (irq < 0)
0857         return irq;
0858 
0859     err = devm_request_irq(&pdev->dev, irq, lantiq_ssc_err_interrupt,
0860                    0, LTQ_SPI_ERR_IRQ_NAME, spi);
0861     return err;
0862 }
0863 
0864 static const struct lantiq_ssc_hwcfg lantiq_ssc_xway = {
0865     .cfg_irq    = lantiq_cfg_irq,
0866     .irnen_r    = LTQ_SPI_IRNEN_R_XWAY,
0867     .irnen_t    = LTQ_SPI_IRNEN_T_XWAY,
0868     .irnicr     = 0xF8,
0869     .irncr      = 0xFC,
0870     .fifo_size_mask = GENMASK(5, 0),
0871     .irq_ack    = false,
0872 };
0873 
0874 static const struct lantiq_ssc_hwcfg lantiq_ssc_xrx = {
0875     .cfg_irq    = lantiq_cfg_irq,
0876     .irnen_r    = LTQ_SPI_IRNEN_R_XRX,
0877     .irnen_t    = LTQ_SPI_IRNEN_T_XRX,
0878     .irnicr     = 0xF8,
0879     .irncr      = 0xFC,
0880     .fifo_size_mask = GENMASK(5, 0),
0881     .irq_ack    = false,
0882 };
0883 
0884 static const struct lantiq_ssc_hwcfg intel_ssc_lgm = {
0885     .cfg_irq    = intel_lgm_cfg_irq,
0886     .irnen_r    = LTQ_SPI_IRNEN_R_XRX,
0887     .irnen_t    = LTQ_SPI_IRNEN_T_XRX,
0888     .irnicr     = 0xFC,
0889     .irncr      = 0xF8,
0890     .fifo_size_mask = GENMASK(7, 0),
0891     .irq_ack    = true,
0892 };
0893 
0894 static const struct of_device_id lantiq_ssc_match[] = {
0895     { .compatible = "lantiq,ase-spi", .data = &lantiq_ssc_xway, },
0896     { .compatible = "lantiq,falcon-spi", .data = &lantiq_ssc_xrx, },
0897     { .compatible = "lantiq,xrx100-spi", .data = &lantiq_ssc_xrx, },
0898     { .compatible = "intel,lgm-spi", .data = &intel_ssc_lgm, },
0899     {},
0900 };
0901 MODULE_DEVICE_TABLE(of, lantiq_ssc_match);
0902 
0903 static int lantiq_ssc_probe(struct platform_device *pdev)
0904 {
0905     struct device *dev = &pdev->dev;
0906     struct spi_master *master;
0907     struct lantiq_ssc_spi *spi;
0908     const struct lantiq_ssc_hwcfg *hwcfg;
0909     u32 id, supports_dma, revision;
0910     unsigned int num_cs;
0911     int err;
0912 
0913     hwcfg = of_device_get_match_data(dev);
0914 
0915     master = spi_alloc_master(dev, sizeof(struct lantiq_ssc_spi));
0916     if (!master)
0917         return -ENOMEM;
0918 
0919     spi = spi_master_get_devdata(master);
0920     spi->master = master;
0921     spi->dev = dev;
0922     spi->hwcfg = hwcfg;
0923     platform_set_drvdata(pdev, spi);
0924     spi->regbase = devm_platform_ioremap_resource(pdev, 0);
0925     if (IS_ERR(spi->regbase)) {
0926         err = PTR_ERR(spi->regbase);
0927         goto err_master_put;
0928     }
0929 
0930     err = hwcfg->cfg_irq(pdev, spi);
0931     if (err)
0932         goto err_master_put;
0933 
0934     spi->spi_clk = devm_clk_get(dev, "gate");
0935     if (IS_ERR(spi->spi_clk)) {
0936         err = PTR_ERR(spi->spi_clk);
0937         goto err_master_put;
0938     }
0939     err = clk_prepare_enable(spi->spi_clk);
0940     if (err)
0941         goto err_master_put;
0942 
0943     /*
0944      * Use the old clk_get_fpi() function on Lantiq platform, till it
0945      * supports common clk.
0946      */
0947 #if defined(CONFIG_LANTIQ) && !defined(CONFIG_COMMON_CLK)
0948     spi->fpi_clk = clk_get_fpi();
0949 #else
0950     spi->fpi_clk = clk_get(dev, "freq");
0951 #endif
0952     if (IS_ERR(spi->fpi_clk)) {
0953         err = PTR_ERR(spi->fpi_clk);
0954         goto err_clk_disable;
0955     }
0956 
0957     num_cs = 8;
0958     of_property_read_u32(pdev->dev.of_node, "num-cs", &num_cs);
0959 
0960     spi->base_cs = 1;
0961     of_property_read_u32(pdev->dev.of_node, "base-cs", &spi->base_cs);
0962 
0963     spin_lock_init(&spi->lock);
0964     spi->bits_per_word = 8;
0965     spi->speed_hz = 0;
0966 
0967     master->dev.of_node = pdev->dev.of_node;
0968     master->num_chipselect = num_cs;
0969     master->use_gpio_descriptors = true;
0970     master->setup = lantiq_ssc_setup;
0971     master->set_cs = lantiq_ssc_set_cs;
0972     master->handle_err = lantiq_ssc_handle_err;
0973     master->prepare_message = lantiq_ssc_prepare_message;
0974     master->unprepare_message = lantiq_ssc_unprepare_message;
0975     master->transfer_one = lantiq_ssc_transfer_one;
0976     master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST | SPI_CS_HIGH |
0977                 SPI_LOOP;
0978     master->bits_per_word_mask = SPI_BPW_RANGE_MASK(2, 8) |
0979                      SPI_BPW_MASK(16) | SPI_BPW_MASK(32);
0980 
0981     spi->wq = alloc_ordered_workqueue(dev_name(dev), WQ_MEM_RECLAIM);
0982     if (!spi->wq) {
0983         err = -ENOMEM;
0984         goto err_clk_put;
0985     }
0986     INIT_WORK(&spi->work, lantiq_ssc_bussy_work);
0987 
0988     id = lantiq_ssc_readl(spi, LTQ_SPI_ID);
0989     spi->tx_fifo_size = (id >> LTQ_SPI_ID_TXFS_S) & hwcfg->fifo_size_mask;
0990     spi->rx_fifo_size = (id >> LTQ_SPI_ID_RXFS_S) & hwcfg->fifo_size_mask;
0991     supports_dma = (id & LTQ_SPI_ID_CFG_M) >> LTQ_SPI_ID_CFG_S;
0992     revision = id & LTQ_SPI_ID_REV_M;
0993 
0994     lantiq_ssc_hw_init(spi);
0995 
0996     dev_info(dev,
0997         "Lantiq SSC SPI controller (Rev %i, TXFS %u, RXFS %u, DMA %u)\n",
0998         revision, spi->tx_fifo_size, spi->rx_fifo_size, supports_dma);
0999 
1000     err = devm_spi_register_master(dev, master);
1001     if (err) {
1002         dev_err(dev, "failed to register spi_master\n");
1003         goto err_wq_destroy;
1004     }
1005 
1006     return 0;
1007 
1008 err_wq_destroy:
1009     destroy_workqueue(spi->wq);
1010 err_clk_put:
1011     clk_put(spi->fpi_clk);
1012 err_clk_disable:
1013     clk_disable_unprepare(spi->spi_clk);
1014 err_master_put:
1015     spi_master_put(master);
1016 
1017     return err;
1018 }
1019 
1020 static int lantiq_ssc_remove(struct platform_device *pdev)
1021 {
1022     struct lantiq_ssc_spi *spi = platform_get_drvdata(pdev);
1023 
1024     lantiq_ssc_writel(spi, 0, LTQ_SPI_IRNEN);
1025     lantiq_ssc_writel(spi, 0, LTQ_SPI_CLC);
1026     rx_fifo_flush(spi);
1027     tx_fifo_flush(spi);
1028     hw_enter_config_mode(spi);
1029 
1030     destroy_workqueue(spi->wq);
1031     clk_disable_unprepare(spi->spi_clk);
1032     clk_put(spi->fpi_clk);
1033 
1034     return 0;
1035 }
1036 
1037 static struct platform_driver lantiq_ssc_driver = {
1038     .probe = lantiq_ssc_probe,
1039     .remove = lantiq_ssc_remove,
1040     .driver = {
1041         .name = "spi-lantiq-ssc",
1042         .of_match_table = lantiq_ssc_match,
1043     },
1044 };
1045 module_platform_driver(lantiq_ssc_driver);
1046 
1047 MODULE_DESCRIPTION("Lantiq SSC SPI controller driver");
1048 MODULE_AUTHOR("Daniel Schwierzeck <daniel.schwierzeck@gmail.com>");
1049 MODULE_AUTHOR("Hauke Mehrtens <hauke@hauke-m.de>");
1050 MODULE_LICENSE("GPL");
1051 MODULE_ALIAS("platform:spi-lantiq-ssc");