Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Cadence SPI controller driver (master mode only)
0004  *
0005  * Copyright (C) 2008 - 2014 Xilinx, Inc.
0006  *
0007  * based on Blackfin On-Chip SPI Driver (spi_bfin5xx.c)
0008  */
0009 
0010 #include <linux/clk.h>
0011 #include <linux/delay.h>
0012 #include <linux/gpio/consumer.h>
0013 #include <linux/interrupt.h>
0014 #include <linux/io.h>
0015 #include <linux/module.h>
0016 #include <linux/of_irq.h>
0017 #include <linux/of_address.h>
0018 #include <linux/platform_device.h>
0019 #include <linux/pm_runtime.h>
0020 #include <linux/spi/spi.h>
0021 
0022 /* Name of this driver */
0023 #define CDNS_SPI_NAME       "cdns-spi"
0024 
0025 /* Register offset definitions */
0026 #define CDNS_SPI_CR 0x00 /* Configuration  Register, RW */
0027 #define CDNS_SPI_ISR    0x04 /* Interrupt Status Register, RO */
0028 #define CDNS_SPI_IER    0x08 /* Interrupt Enable Register, WO */
0029 #define CDNS_SPI_IDR    0x0c /* Interrupt Disable Register, WO */
0030 #define CDNS_SPI_IMR    0x10 /* Interrupt Enabled Mask Register, RO */
0031 #define CDNS_SPI_ER 0x14 /* Enable/Disable Register, RW */
0032 #define CDNS_SPI_DR 0x18 /* Delay Register, RW */
0033 #define CDNS_SPI_TXD    0x1C /* Data Transmit Register, WO */
0034 #define CDNS_SPI_RXD    0x20 /* Data Receive Register, RO */
0035 #define CDNS_SPI_SICR   0x24 /* Slave Idle Count Register, RW */
0036 #define CDNS_SPI_THLD   0x28 /* Transmit FIFO Watermark Register,RW */
0037 
0038 #define SPI_AUTOSUSPEND_TIMEOUT     3000
0039 /*
0040  * SPI Configuration Register bit Masks
0041  *
0042  * This register contains various control bits that affect the operation
0043  * of the SPI controller
0044  */
0045 #define CDNS_SPI_CR_MANSTRT 0x00010000 /* Manual TX Start */
0046 #define CDNS_SPI_CR_CPHA        0x00000004 /* Clock Phase Control */
0047 #define CDNS_SPI_CR_CPOL        0x00000002 /* Clock Polarity Control */
0048 #define CDNS_SPI_CR_SSCTRL      0x00003C00 /* Slave Select Mask */
0049 #define CDNS_SPI_CR_PERI_SEL    0x00000200 /* Peripheral Select Decode */
0050 #define CDNS_SPI_CR_BAUD_DIV    0x00000038 /* Baud Rate Divisor Mask */
0051 #define CDNS_SPI_CR_MSTREN      0x00000001 /* Master Enable Mask */
0052 #define CDNS_SPI_CR_MANSTRTEN   0x00008000 /* Manual TX Enable Mask */
0053 #define CDNS_SPI_CR_SSFORCE 0x00004000 /* Manual SS Enable Mask */
0054 #define CDNS_SPI_CR_BAUD_DIV_4  0x00000008 /* Default Baud Div Mask */
0055 #define CDNS_SPI_CR_DEFAULT (CDNS_SPI_CR_MSTREN | \
0056                     CDNS_SPI_CR_SSCTRL | \
0057                     CDNS_SPI_CR_SSFORCE | \
0058                     CDNS_SPI_CR_BAUD_DIV_4)
0059 
0060 /*
0061  * SPI Configuration Register - Baud rate and slave select
0062  *
0063  * These are the values used in the calculation of baud rate divisor and
0064  * setting the slave select.
0065  */
0066 
0067 #define CDNS_SPI_BAUD_DIV_MAX       7 /* Baud rate divisor maximum */
0068 #define CDNS_SPI_BAUD_DIV_MIN       1 /* Baud rate divisor minimum */
0069 #define CDNS_SPI_BAUD_DIV_SHIFT     3 /* Baud rate divisor shift in CR */
0070 #define CDNS_SPI_SS_SHIFT       10 /* Slave Select field shift in CR */
0071 #define CDNS_SPI_SS0            0x1 /* Slave Select zero */
0072 #define CDNS_SPI_NOSS           0xF /* No Slave select */
0073 
0074 /*
0075  * SPI Interrupt Registers bit Masks
0076  *
0077  * All the four interrupt registers (Status/Mask/Enable/Disable) have the same
0078  * bit definitions.
0079  */
0080 #define CDNS_SPI_IXR_TXOW   0x00000004 /* SPI TX FIFO Overwater */
0081 #define CDNS_SPI_IXR_MODF   0x00000002 /* SPI Mode Fault */
0082 #define CDNS_SPI_IXR_RXNEMTY 0x00000010 /* SPI RX FIFO Not Empty */
0083 #define CDNS_SPI_IXR_DEFAULT    (CDNS_SPI_IXR_TXOW | \
0084                     CDNS_SPI_IXR_MODF)
0085 #define CDNS_SPI_IXR_TXFULL 0x00000008 /* SPI TX Full */
0086 #define CDNS_SPI_IXR_ALL    0x0000007F /* SPI all interrupts */
0087 
0088 /*
0089  * SPI Enable Register bit Masks
0090  *
0091  * This register is used to enable or disable the SPI controller
0092  */
0093 #define CDNS_SPI_ER_ENABLE  0x00000001 /* SPI Enable Bit Mask */
0094 #define CDNS_SPI_ER_DISABLE 0x0 /* SPI Disable Bit Mask */
0095 
0096 /* Default number of chip select lines */
0097 #define CDNS_SPI_DEFAULT_NUM_CS     4
0098 
0099 /**
0100  * struct cdns_spi - This definition defines spi driver instance
0101  * @regs:       Virtual address of the SPI controller registers
0102  * @ref_clk:        Pointer to the peripheral clock
0103  * @pclk:       Pointer to the APB clock
0104  * @speed_hz:       Current SPI bus clock speed in Hz
0105  * @txbuf:      Pointer to the TX buffer
0106  * @rxbuf:      Pointer to the RX buffer
0107  * @tx_bytes:       Number of bytes left to transfer
0108  * @rx_bytes:       Number of bytes requested
0109  * @dev_busy:       Device busy flag
0110  * @is_decoded_cs:  Flag for decoder property set or not
0111  * @tx_fifo_depth:  Depth of the TX FIFO
0112  */
0113 struct cdns_spi {
0114     void __iomem *regs;
0115     struct clk *ref_clk;
0116     struct clk *pclk;
0117     unsigned int clk_rate;
0118     u32 speed_hz;
0119     const u8 *txbuf;
0120     u8 *rxbuf;
0121     int tx_bytes;
0122     int rx_bytes;
0123     u8 dev_busy;
0124     u32 is_decoded_cs;
0125     unsigned int tx_fifo_depth;
0126 };
0127 
0128 /* Macros for the SPI controller read/write */
0129 static inline u32 cdns_spi_read(struct cdns_spi *xspi, u32 offset)
0130 {
0131     return readl_relaxed(xspi->regs + offset);
0132 }
0133 
0134 static inline void cdns_spi_write(struct cdns_spi *xspi, u32 offset, u32 val)
0135 {
0136     writel_relaxed(val, xspi->regs + offset);
0137 }
0138 
0139 /**
0140  * cdns_spi_init_hw - Initialize the hardware and configure the SPI controller
0141  * @xspi:   Pointer to the cdns_spi structure
0142  *
0143  * On reset the SPI controller is configured to be in master mode, baud rate
0144  * divisor is set to 4, threshold value for TX FIFO not full interrupt is set
0145  * to 1 and size of the word to be transferred as 8 bit.
0146  * This function initializes the SPI controller to disable and clear all the
0147  * interrupts, enable manual slave select and manual start, deselect all the
0148  * chip select lines, and enable the SPI controller.
0149  */
0150 static void cdns_spi_init_hw(struct cdns_spi *xspi)
0151 {
0152     u32 ctrl_reg = CDNS_SPI_CR_DEFAULT;
0153 
0154     if (xspi->is_decoded_cs)
0155         ctrl_reg |= CDNS_SPI_CR_PERI_SEL;
0156 
0157     cdns_spi_write(xspi, CDNS_SPI_ER, CDNS_SPI_ER_DISABLE);
0158     cdns_spi_write(xspi, CDNS_SPI_IDR, CDNS_SPI_IXR_ALL);
0159 
0160     /* Clear the RX FIFO */
0161     while (cdns_spi_read(xspi, CDNS_SPI_ISR) & CDNS_SPI_IXR_RXNEMTY)
0162         cdns_spi_read(xspi, CDNS_SPI_RXD);
0163 
0164     cdns_spi_write(xspi, CDNS_SPI_ISR, CDNS_SPI_IXR_ALL);
0165     cdns_spi_write(xspi, CDNS_SPI_CR, ctrl_reg);
0166     cdns_spi_write(xspi, CDNS_SPI_ER, CDNS_SPI_ER_ENABLE);
0167 }
0168 
0169 /**
0170  * cdns_spi_chipselect - Select or deselect the chip select line
0171  * @spi:    Pointer to the spi_device structure
0172  * @is_high:    Select(0) or deselect (1) the chip select line
0173  */
0174 static void cdns_spi_chipselect(struct spi_device *spi, bool is_high)
0175 {
0176     struct cdns_spi *xspi = spi_master_get_devdata(spi->master);
0177     u32 ctrl_reg;
0178 
0179     ctrl_reg = cdns_spi_read(xspi, CDNS_SPI_CR);
0180 
0181     if (is_high) {
0182         /* Deselect the slave */
0183         ctrl_reg |= CDNS_SPI_CR_SSCTRL;
0184     } else {
0185         /* Select the slave */
0186         ctrl_reg &= ~CDNS_SPI_CR_SSCTRL;
0187         if (!(xspi->is_decoded_cs))
0188             ctrl_reg |= ((~(CDNS_SPI_SS0 << spi->chip_select)) <<
0189                      CDNS_SPI_SS_SHIFT) &
0190                      CDNS_SPI_CR_SSCTRL;
0191         else
0192             ctrl_reg |= (spi->chip_select << CDNS_SPI_SS_SHIFT) &
0193                      CDNS_SPI_CR_SSCTRL;
0194     }
0195 
0196     cdns_spi_write(xspi, CDNS_SPI_CR, ctrl_reg);
0197 }
0198 
0199 /**
0200  * cdns_spi_config_clock_mode - Sets clock polarity and phase
0201  * @spi:    Pointer to the spi_device structure
0202  *
0203  * Sets the requested clock polarity and phase.
0204  */
0205 static void cdns_spi_config_clock_mode(struct spi_device *spi)
0206 {
0207     struct cdns_spi *xspi = spi_master_get_devdata(spi->master);
0208     u32 ctrl_reg, new_ctrl_reg;
0209 
0210     new_ctrl_reg = cdns_spi_read(xspi, CDNS_SPI_CR);
0211     ctrl_reg = new_ctrl_reg;
0212 
0213     /* Set the SPI clock phase and clock polarity */
0214     new_ctrl_reg &= ~(CDNS_SPI_CR_CPHA | CDNS_SPI_CR_CPOL);
0215     if (spi->mode & SPI_CPHA)
0216         new_ctrl_reg |= CDNS_SPI_CR_CPHA;
0217     if (spi->mode & SPI_CPOL)
0218         new_ctrl_reg |= CDNS_SPI_CR_CPOL;
0219 
0220     if (new_ctrl_reg != ctrl_reg) {
0221         /*
0222          * Just writing the CR register does not seem to apply the clock
0223          * setting changes. This is problematic when changing the clock
0224          * polarity as it will cause the SPI slave to see spurious clock
0225          * transitions. To workaround the issue toggle the ER register.
0226          */
0227         cdns_spi_write(xspi, CDNS_SPI_ER, CDNS_SPI_ER_DISABLE);
0228         cdns_spi_write(xspi, CDNS_SPI_CR, new_ctrl_reg);
0229         cdns_spi_write(xspi, CDNS_SPI_ER, CDNS_SPI_ER_ENABLE);
0230     }
0231 }
0232 
0233 /**
0234  * cdns_spi_config_clock_freq - Sets clock frequency
0235  * @spi:    Pointer to the spi_device structure
0236  * @transfer:   Pointer to the spi_transfer structure which provides
0237  *      information about next transfer setup parameters
0238  *
0239  * Sets the requested clock frequency.
0240  * Note: If the requested frequency is not an exact match with what can be
0241  * obtained using the prescalar value the driver sets the clock frequency which
0242  * is lower than the requested frequency (maximum lower) for the transfer. If
0243  * the requested frequency is higher or lower than that is supported by the SPI
0244  * controller the driver will set the highest or lowest frequency supported by
0245  * controller.
0246  */
0247 static void cdns_spi_config_clock_freq(struct spi_device *spi,
0248                        struct spi_transfer *transfer)
0249 {
0250     struct cdns_spi *xspi = spi_master_get_devdata(spi->master);
0251     u32 ctrl_reg, baud_rate_val;
0252     unsigned long frequency;
0253 
0254     frequency = xspi->clk_rate;
0255 
0256     ctrl_reg = cdns_spi_read(xspi, CDNS_SPI_CR);
0257 
0258     /* Set the clock frequency */
0259     if (xspi->speed_hz != transfer->speed_hz) {
0260         /* first valid value is 1 */
0261         baud_rate_val = CDNS_SPI_BAUD_DIV_MIN;
0262         while ((baud_rate_val < CDNS_SPI_BAUD_DIV_MAX) &&
0263                (frequency / (2 << baud_rate_val)) > transfer->speed_hz)
0264             baud_rate_val++;
0265 
0266         ctrl_reg &= ~CDNS_SPI_CR_BAUD_DIV;
0267         ctrl_reg |= baud_rate_val << CDNS_SPI_BAUD_DIV_SHIFT;
0268 
0269         xspi->speed_hz = frequency / (2 << baud_rate_val);
0270     }
0271     cdns_spi_write(xspi, CDNS_SPI_CR, ctrl_reg);
0272 }
0273 
0274 /**
0275  * cdns_spi_setup_transfer - Configure SPI controller for specified transfer
0276  * @spi:    Pointer to the spi_device structure
0277  * @transfer:   Pointer to the spi_transfer structure which provides
0278  *      information about next transfer setup parameters
0279  *
0280  * Sets the operational mode of SPI controller for the next SPI transfer and
0281  * sets the requested clock frequency.
0282  *
0283  * Return:  Always 0
0284  */
0285 static int cdns_spi_setup_transfer(struct spi_device *spi,
0286                    struct spi_transfer *transfer)
0287 {
0288     struct cdns_spi *xspi = spi_master_get_devdata(spi->master);
0289 
0290     cdns_spi_config_clock_freq(spi, transfer);
0291 
0292     dev_dbg(&spi->dev, "%s, mode %d, %u bits/w, %u clock speed\n",
0293         __func__, spi->mode, spi->bits_per_word,
0294         xspi->speed_hz);
0295 
0296     return 0;
0297 }
0298 
0299 /**
0300  * cdns_spi_fill_tx_fifo - Fills the TX FIFO with as many bytes as possible
0301  * @xspi:   Pointer to the cdns_spi structure
0302  */
0303 static void cdns_spi_fill_tx_fifo(struct cdns_spi *xspi)
0304 {
0305     unsigned long trans_cnt = 0;
0306 
0307     while ((trans_cnt < xspi->tx_fifo_depth) &&
0308            (xspi->tx_bytes > 0)) {
0309 
0310         /* When xspi in busy condition, bytes may send failed,
0311          * then spi control did't work thoroughly, add one byte delay
0312          */
0313         if (cdns_spi_read(xspi, CDNS_SPI_ISR) &
0314             CDNS_SPI_IXR_TXFULL)
0315             udelay(10);
0316 
0317         if (xspi->txbuf)
0318             cdns_spi_write(xspi, CDNS_SPI_TXD, *xspi->txbuf++);
0319         else
0320             cdns_spi_write(xspi, CDNS_SPI_TXD, 0);
0321 
0322         xspi->tx_bytes--;
0323         trans_cnt++;
0324     }
0325 }
0326 
0327 /**
0328  * cdns_spi_irq - Interrupt service routine of the SPI controller
0329  * @irq:    IRQ number
0330  * @dev_id: Pointer to the xspi structure
0331  *
0332  * This function handles TX empty and Mode Fault interrupts only.
0333  * On TX empty interrupt this function reads the received data from RX FIFO and
0334  * fills the TX FIFO if there is any data remaining to be transferred.
0335  * On Mode Fault interrupt this function indicates that transfer is completed,
0336  * the SPI subsystem will identify the error as the remaining bytes to be
0337  * transferred is non-zero.
0338  *
0339  * Return:  IRQ_HANDLED when handled; IRQ_NONE otherwise.
0340  */
0341 static irqreturn_t cdns_spi_irq(int irq, void *dev_id)
0342 {
0343     struct spi_master *master = dev_id;
0344     struct cdns_spi *xspi = spi_master_get_devdata(master);
0345     irqreturn_t status;
0346     u32 intr_status;
0347 
0348     status = IRQ_NONE;
0349     intr_status = cdns_spi_read(xspi, CDNS_SPI_ISR);
0350     cdns_spi_write(xspi, CDNS_SPI_ISR, intr_status);
0351 
0352     if (intr_status & CDNS_SPI_IXR_MODF) {
0353         /* Indicate that transfer is completed, the SPI subsystem will
0354          * identify the error as the remaining bytes to be
0355          * transferred is non-zero
0356          */
0357         cdns_spi_write(xspi, CDNS_SPI_IDR, CDNS_SPI_IXR_DEFAULT);
0358         spi_finalize_current_transfer(master);
0359         status = IRQ_HANDLED;
0360     } else if (intr_status & CDNS_SPI_IXR_TXOW) {
0361         unsigned long trans_cnt;
0362 
0363         trans_cnt = xspi->rx_bytes - xspi->tx_bytes;
0364 
0365         /* Read out the data from the RX FIFO */
0366         while (trans_cnt) {
0367             u8 data;
0368 
0369             data = cdns_spi_read(xspi, CDNS_SPI_RXD);
0370             if (xspi->rxbuf)
0371                 *xspi->rxbuf++ = data;
0372 
0373             xspi->rx_bytes--;
0374             trans_cnt--;
0375         }
0376 
0377         if (xspi->tx_bytes) {
0378             /* There is more data to send */
0379             cdns_spi_fill_tx_fifo(xspi);
0380         } else {
0381             /* Transfer is completed */
0382             cdns_spi_write(xspi, CDNS_SPI_IDR,
0383                        CDNS_SPI_IXR_DEFAULT);
0384             spi_finalize_current_transfer(master);
0385         }
0386         status = IRQ_HANDLED;
0387     }
0388 
0389     return status;
0390 }
0391 
0392 static int cdns_prepare_message(struct spi_master *master,
0393                 struct spi_message *msg)
0394 {
0395     cdns_spi_config_clock_mode(msg->spi);
0396     return 0;
0397 }
0398 
0399 /**
0400  * cdns_transfer_one - Initiates the SPI transfer
0401  * @master: Pointer to spi_master structure
0402  * @spi:    Pointer to the spi_device structure
0403  * @transfer:   Pointer to the spi_transfer structure which provides
0404  *      information about next transfer parameters
0405  *
0406  * This function fills the TX FIFO, starts the SPI transfer and
0407  * returns a positive transfer count so that core will wait for completion.
0408  *
0409  * Return:  Number of bytes transferred in the last transfer
0410  */
0411 static int cdns_transfer_one(struct spi_master *master,
0412                  struct spi_device *spi,
0413                  struct spi_transfer *transfer)
0414 {
0415     struct cdns_spi *xspi = spi_master_get_devdata(master);
0416 
0417     xspi->txbuf = transfer->tx_buf;
0418     xspi->rxbuf = transfer->rx_buf;
0419     xspi->tx_bytes = transfer->len;
0420     xspi->rx_bytes = transfer->len;
0421 
0422     cdns_spi_setup_transfer(spi, transfer);
0423     cdns_spi_fill_tx_fifo(xspi);
0424     spi_transfer_delay_exec(transfer);
0425 
0426     cdns_spi_write(xspi, CDNS_SPI_IER, CDNS_SPI_IXR_DEFAULT);
0427     return transfer->len;
0428 }
0429 
0430 /**
0431  * cdns_prepare_transfer_hardware - Prepares hardware for transfer.
0432  * @master: Pointer to the spi_master structure which provides
0433  *      information about the controller.
0434  *
0435  * This function enables SPI master controller.
0436  *
0437  * Return:  0 always
0438  */
0439 static int cdns_prepare_transfer_hardware(struct spi_master *master)
0440 {
0441     struct cdns_spi *xspi = spi_master_get_devdata(master);
0442 
0443     cdns_spi_write(xspi, CDNS_SPI_ER, CDNS_SPI_ER_ENABLE);
0444 
0445     return 0;
0446 }
0447 
0448 /**
0449  * cdns_unprepare_transfer_hardware - Relaxes hardware after transfer
0450  * @master: Pointer to the spi_master structure which provides
0451  *      information about the controller.
0452  *
0453  * This function disables the SPI master controller when no slave selected.
0454  *
0455  * Return:  0 always
0456  */
0457 static int cdns_unprepare_transfer_hardware(struct spi_master *master)
0458 {
0459     struct cdns_spi *xspi = spi_master_get_devdata(master);
0460     u32 ctrl_reg;
0461 
0462     /* Disable the SPI if slave is deselected */
0463     ctrl_reg = cdns_spi_read(xspi, CDNS_SPI_CR);
0464     ctrl_reg = (ctrl_reg & CDNS_SPI_CR_SSCTRL) >>  CDNS_SPI_SS_SHIFT;
0465     if (ctrl_reg == CDNS_SPI_NOSS)
0466         cdns_spi_write(xspi, CDNS_SPI_ER, CDNS_SPI_ER_DISABLE);
0467 
0468     return 0;
0469 }
0470 
0471 /**
0472  * cdns_spi_detect_fifo_depth - Detect the FIFO depth of the hardware
0473  * @xspi:   Pointer to the cdns_spi structure
0474  *
0475  * The depth of the TX FIFO is a synthesis configuration parameter of the SPI
0476  * IP. The FIFO threshold register is sized so that its maximum value can be the
0477  * FIFO size - 1. This is used to detect the size of the FIFO.
0478  */
0479 static void cdns_spi_detect_fifo_depth(struct cdns_spi *xspi)
0480 {
0481     /* The MSBs will get truncated giving us the size of the FIFO */
0482     cdns_spi_write(xspi, CDNS_SPI_THLD, 0xffff);
0483     xspi->tx_fifo_depth = cdns_spi_read(xspi, CDNS_SPI_THLD) + 1;
0484 
0485     /* Reset to default */
0486     cdns_spi_write(xspi, CDNS_SPI_THLD, 0x1);
0487 }
0488 
0489 /**
0490  * cdns_spi_probe - Probe method for the SPI driver
0491  * @pdev:   Pointer to the platform_device structure
0492  *
0493  * This function initializes the driver data structures and the hardware.
0494  *
0495  * Return:  0 on success and error value on error
0496  */
0497 static int cdns_spi_probe(struct platform_device *pdev)
0498 {
0499     int ret = 0, irq;
0500     struct spi_master *master;
0501     struct cdns_spi *xspi;
0502     u32 num_cs;
0503 
0504     master = spi_alloc_master(&pdev->dev, sizeof(*xspi));
0505     if (!master)
0506         return -ENOMEM;
0507 
0508     xspi = spi_master_get_devdata(master);
0509     master->dev.of_node = pdev->dev.of_node;
0510     platform_set_drvdata(pdev, master);
0511 
0512     xspi->regs = devm_platform_ioremap_resource(pdev, 0);
0513     if (IS_ERR(xspi->regs)) {
0514         ret = PTR_ERR(xspi->regs);
0515         goto remove_master;
0516     }
0517 
0518     xspi->pclk = devm_clk_get(&pdev->dev, "pclk");
0519     if (IS_ERR(xspi->pclk)) {
0520         dev_err(&pdev->dev, "pclk clock not found.\n");
0521         ret = PTR_ERR(xspi->pclk);
0522         goto remove_master;
0523     }
0524 
0525     xspi->ref_clk = devm_clk_get(&pdev->dev, "ref_clk");
0526     if (IS_ERR(xspi->ref_clk)) {
0527         dev_err(&pdev->dev, "ref_clk clock not found.\n");
0528         ret = PTR_ERR(xspi->ref_clk);
0529         goto remove_master;
0530     }
0531 
0532     ret = clk_prepare_enable(xspi->pclk);
0533     if (ret) {
0534         dev_err(&pdev->dev, "Unable to enable APB clock.\n");
0535         goto remove_master;
0536     }
0537 
0538     ret = clk_prepare_enable(xspi->ref_clk);
0539     if (ret) {
0540         dev_err(&pdev->dev, "Unable to enable device clock.\n");
0541         goto clk_dis_apb;
0542     }
0543 
0544     pm_runtime_use_autosuspend(&pdev->dev);
0545     pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
0546     pm_runtime_get_noresume(&pdev->dev);
0547     pm_runtime_set_active(&pdev->dev);
0548     pm_runtime_enable(&pdev->dev);
0549 
0550     ret = of_property_read_u32(pdev->dev.of_node, "num-cs", &num_cs);
0551     if (ret < 0)
0552         master->num_chipselect = CDNS_SPI_DEFAULT_NUM_CS;
0553     else
0554         master->num_chipselect = num_cs;
0555 
0556     ret = of_property_read_u32(pdev->dev.of_node, "is-decoded-cs",
0557                    &xspi->is_decoded_cs);
0558     if (ret < 0)
0559         xspi->is_decoded_cs = 0;
0560 
0561     cdns_spi_detect_fifo_depth(xspi);
0562 
0563     /* SPI controller initializations */
0564     cdns_spi_init_hw(xspi);
0565 
0566     irq = platform_get_irq(pdev, 0);
0567     if (irq <= 0) {
0568         ret = -ENXIO;
0569         goto clk_dis_all;
0570     }
0571 
0572     ret = devm_request_irq(&pdev->dev, irq, cdns_spi_irq,
0573                    0, pdev->name, master);
0574     if (ret != 0) {
0575         ret = -ENXIO;
0576         dev_err(&pdev->dev, "request_irq failed\n");
0577         goto clk_dis_all;
0578     }
0579 
0580     master->use_gpio_descriptors = true;
0581     master->prepare_transfer_hardware = cdns_prepare_transfer_hardware;
0582     master->prepare_message = cdns_prepare_message;
0583     master->transfer_one = cdns_transfer_one;
0584     master->unprepare_transfer_hardware = cdns_unprepare_transfer_hardware;
0585     master->set_cs = cdns_spi_chipselect;
0586     master->auto_runtime_pm = true;
0587     master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
0588 
0589     xspi->clk_rate = clk_get_rate(xspi->ref_clk);
0590     /* Set to default valid value */
0591     master->max_speed_hz = xspi->clk_rate / 4;
0592     xspi->speed_hz = master->max_speed_hz;
0593 
0594     master->bits_per_word_mask = SPI_BPW_MASK(8);
0595 
0596     pm_runtime_mark_last_busy(&pdev->dev);
0597     pm_runtime_put_autosuspend(&pdev->dev);
0598 
0599     ret = spi_register_master(master);
0600     if (ret) {
0601         dev_err(&pdev->dev, "spi_register_master failed\n");
0602         goto clk_dis_all;
0603     }
0604 
0605     return ret;
0606 
0607 clk_dis_all:
0608     pm_runtime_set_suspended(&pdev->dev);
0609     pm_runtime_disable(&pdev->dev);
0610     clk_disable_unprepare(xspi->ref_clk);
0611 clk_dis_apb:
0612     clk_disable_unprepare(xspi->pclk);
0613 remove_master:
0614     spi_master_put(master);
0615     return ret;
0616 }
0617 
0618 /**
0619  * cdns_spi_remove - Remove method for the SPI driver
0620  * @pdev:   Pointer to the platform_device structure
0621  *
0622  * This function is called if a device is physically removed from the system or
0623  * if the driver module is being unloaded. It frees all resources allocated to
0624  * the device.
0625  *
0626  * Return:  0 on success and error value on error
0627  */
0628 static int cdns_spi_remove(struct platform_device *pdev)
0629 {
0630     struct spi_master *master = platform_get_drvdata(pdev);
0631     struct cdns_spi *xspi = spi_master_get_devdata(master);
0632 
0633     cdns_spi_write(xspi, CDNS_SPI_ER, CDNS_SPI_ER_DISABLE);
0634 
0635     clk_disable_unprepare(xspi->ref_clk);
0636     clk_disable_unprepare(xspi->pclk);
0637     pm_runtime_set_suspended(&pdev->dev);
0638     pm_runtime_disable(&pdev->dev);
0639 
0640     spi_unregister_master(master);
0641 
0642     return 0;
0643 }
0644 
0645 /**
0646  * cdns_spi_suspend - Suspend method for the SPI driver
0647  * @dev:    Address of the platform_device structure
0648  *
0649  * This function disables the SPI controller and
0650  * changes the driver state to "suspend"
0651  *
0652  * Return:  0 on success and error value on error
0653  */
0654 static int __maybe_unused cdns_spi_suspend(struct device *dev)
0655 {
0656     struct spi_master *master = dev_get_drvdata(dev);
0657 
0658     return spi_master_suspend(master);
0659 }
0660 
0661 /**
0662  * cdns_spi_resume - Resume method for the SPI driver
0663  * @dev:    Address of the platform_device structure
0664  *
0665  * This function changes the driver state to "ready"
0666  *
0667  * Return:  0 on success and error value on error
0668  */
0669 static int __maybe_unused cdns_spi_resume(struct device *dev)
0670 {
0671     struct spi_master *master = dev_get_drvdata(dev);
0672     struct cdns_spi *xspi = spi_master_get_devdata(master);
0673 
0674     cdns_spi_init_hw(xspi);
0675     return spi_master_resume(master);
0676 }
0677 
0678 /**
0679  * cdns_spi_runtime_resume - Runtime resume method for the SPI driver
0680  * @dev:    Address of the platform_device structure
0681  *
0682  * This function enables the clocks
0683  *
0684  * Return:  0 on success and error value on error
0685  */
0686 static int __maybe_unused cdns_spi_runtime_resume(struct device *dev)
0687 {
0688     struct spi_master *master = dev_get_drvdata(dev);
0689     struct cdns_spi *xspi = spi_master_get_devdata(master);
0690     int ret;
0691 
0692     ret = clk_prepare_enable(xspi->pclk);
0693     if (ret) {
0694         dev_err(dev, "Cannot enable APB clock.\n");
0695         return ret;
0696     }
0697 
0698     ret = clk_prepare_enable(xspi->ref_clk);
0699     if (ret) {
0700         dev_err(dev, "Cannot enable device clock.\n");
0701         clk_disable_unprepare(xspi->pclk);
0702         return ret;
0703     }
0704     return 0;
0705 }
0706 
0707 /**
0708  * cdns_spi_runtime_suspend - Runtime suspend method for the SPI driver
0709  * @dev:    Address of the platform_device structure
0710  *
0711  * This function disables the clocks
0712  *
0713  * Return:  Always 0
0714  */
0715 static int __maybe_unused cdns_spi_runtime_suspend(struct device *dev)
0716 {
0717     struct spi_master *master = dev_get_drvdata(dev);
0718     struct cdns_spi *xspi = spi_master_get_devdata(master);
0719 
0720     clk_disable_unprepare(xspi->ref_clk);
0721     clk_disable_unprepare(xspi->pclk);
0722 
0723     return 0;
0724 }
0725 
0726 static const struct dev_pm_ops cdns_spi_dev_pm_ops = {
0727     SET_RUNTIME_PM_OPS(cdns_spi_runtime_suspend,
0728                cdns_spi_runtime_resume, NULL)
0729     SET_SYSTEM_SLEEP_PM_OPS(cdns_spi_suspend, cdns_spi_resume)
0730 };
0731 
0732 static const struct of_device_id cdns_spi_of_match[] = {
0733     { .compatible = "xlnx,zynq-spi-r1p6" },
0734     { .compatible = "cdns,spi-r1p6" },
0735     { /* end of table */ }
0736 };
0737 MODULE_DEVICE_TABLE(of, cdns_spi_of_match);
0738 
0739 /* cdns_spi_driver - This structure defines the SPI subsystem platform driver */
0740 static struct platform_driver cdns_spi_driver = {
0741     .probe  = cdns_spi_probe,
0742     .remove = cdns_spi_remove,
0743     .driver = {
0744         .name = CDNS_SPI_NAME,
0745         .of_match_table = cdns_spi_of_match,
0746         .pm = &cdns_spi_dev_pm_ops,
0747     },
0748 };
0749 
0750 module_platform_driver(cdns_spi_driver);
0751 
0752 MODULE_AUTHOR("Xilinx, Inc.");
0753 MODULE_DESCRIPTION("Cadence SPI driver");
0754 MODULE_LICENSE("GPL");