1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Cadence SPI controller driver (master and slave mode)
5 * Copyright (C) 2008 - 2014 Xilinx, Inc.
7 * based on Blackfin On-Chip SPI Driver (spi_bfin5xx.c)
10 #include <linux/clk.h>
11 #include <linux/delay.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/interrupt.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/of_irq.h>
18 #include <linux/of_address.h>
19 #include <linux/platform_device.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/spi/spi.h>
23 /* Name of this driver */
24 #define CDNS_SPI_NAME "cdns-spi"
26 /* Register offset definitions */
27 #define CDNS_SPI_CR 0x00 /* Configuration Register, RW */
28 #define CDNS_SPI_ISR 0x04 /* Interrupt Status Register, RO */
29 #define CDNS_SPI_IER 0x08 /* Interrupt Enable Register, WO */
30 #define CDNS_SPI_IDR 0x0c /* Interrupt Disable Register, WO */
31 #define CDNS_SPI_IMR 0x10 /* Interrupt Enabled Mask Register, RO */
32 #define CDNS_SPI_ER 0x14 /* Enable/Disable Register, RW */
33 #define CDNS_SPI_DR 0x18 /* Delay Register, RW */
34 #define CDNS_SPI_TXD 0x1C /* Data Transmit Register, WO */
35 #define CDNS_SPI_RXD 0x20 /* Data Receive Register, RO */
36 #define CDNS_SPI_SICR 0x24 /* Slave Idle Count Register, RW */
37 #define CDNS_SPI_THLD 0x28 /* Transmit FIFO Watermark Register,RW */
39 #define SPI_AUTOSUSPEND_TIMEOUT 3000
41 * SPI Configuration Register bit Masks
43 * This register contains various control bits that affect the operation
44 * of the SPI controller
46 #define CDNS_SPI_CR_MANSTRT 0x00010000 /* Manual TX Start */
47 #define CDNS_SPI_CR_CPHA 0x00000004 /* Clock Phase Control */
48 #define CDNS_SPI_CR_CPOL 0x00000002 /* Clock Polarity Control */
49 #define CDNS_SPI_CR_SSCTRL 0x00003C00 /* Slave Select Mask */
50 #define CDNS_SPI_CR_PERI_SEL 0x00000200 /* Peripheral Select Decode */
51 #define CDNS_SPI_CR_BAUD_DIV 0x00000038 /* Baud Rate Divisor Mask */
52 #define CDNS_SPI_CR_MSTREN 0x00000001 /* Master Enable Mask */
53 #define CDNS_SPI_CR_MANSTRTEN 0x00008000 /* Manual TX Enable Mask */
54 #define CDNS_SPI_CR_SSFORCE 0x00004000 /* Manual SS Enable Mask */
55 #define CDNS_SPI_CR_BAUD_DIV_4 0x00000008 /* Default Baud Div Mask */
56 #define CDNS_SPI_CR_DEFAULT (CDNS_SPI_CR_MSTREN | \
57 CDNS_SPI_CR_SSCTRL | \
58 CDNS_SPI_CR_SSFORCE | \
59 CDNS_SPI_CR_BAUD_DIV_4)
62 * SPI Configuration Register - Baud rate and slave select
64 * These are the values used in the calculation of baud rate divisor and
65 * setting the slave select.
68 #define CDNS_SPI_BAUD_DIV_MAX 7 /* Baud rate divisor maximum */
69 #define CDNS_SPI_BAUD_DIV_MIN 1 /* Baud rate divisor minimum */
70 #define CDNS_SPI_BAUD_DIV_SHIFT 3 /* Baud rate divisor shift in CR */
71 #define CDNS_SPI_SS_SHIFT 10 /* Slave Select field shift in CR */
72 #define CDNS_SPI_SS0 0x1 /* Slave Select zero */
73 #define CDNS_SPI_NOSS 0xF /* No Slave select */
76 * SPI Interrupt Registers bit Masks
78 * All the four interrupt registers (Status/Mask/Enable/Disable) have the same
81 #define CDNS_SPI_IXR_TXOW 0x00000004 /* SPI TX FIFO Overwater */
82 #define CDNS_SPI_IXR_MODF 0x00000002 /* SPI Mode Fault */
83 #define CDNS_SPI_IXR_RXNEMTY 0x00000010 /* SPI RX FIFO Not Empty */
84 #define CDNS_SPI_IXR_DEFAULT (CDNS_SPI_IXR_TXOW | \
86 #define CDNS_SPI_IXR_TXFULL 0x00000008 /* SPI TX Full */
87 #define CDNS_SPI_IXR_ALL 0x0000007F /* SPI all interrupts */
90 * SPI Enable Register bit Masks
92 * This register is used to enable or disable the SPI controller
94 #define CDNS_SPI_ER_ENABLE 0x00000001 /* SPI Enable Bit Mask */
95 #define CDNS_SPI_ER_DISABLE 0x0 /* SPI Disable Bit Mask */
97 /* Default number of chip select lines */
98 #define CDNS_SPI_DEFAULT_NUM_CS 4
101 * struct cdns_spi - This definition defines spi driver instance
102 * @regs: Virtual address of the SPI controller registers
103 * @ref_clk: Pointer to the peripheral clock
104 * @pclk: Pointer to the APB clock
105 * @speed_hz: Current SPI bus clock speed in Hz
106 * @txbuf: Pointer to the TX buffer
107 * @rxbuf: Pointer to the RX buffer
108 * @tx_bytes: Number of bytes left to transfer
109 * @rx_bytes: Number of bytes requested
110 * @dev_busy: Device busy flag
111 * @is_decoded_cs: Flag for decoder property set or not
112 * @tx_fifo_depth: Depth of the TX FIFO
118 unsigned int clk_rate;
126 unsigned int tx_fifo_depth;
129 /* Macros for the SPI controller read/write */
130 static inline u32 cdns_spi_read(struct cdns_spi *xspi, u32 offset)
132 return readl_relaxed(xspi->regs + offset);
135 static inline void cdns_spi_write(struct cdns_spi *xspi, u32 offset, u32 val)
137 writel_relaxed(val, xspi->regs + offset);
141 * cdns_spi_init_hw - Initialize the hardware and configure the SPI controller
142 * @xspi: Pointer to the cdns_spi structure
143 * @is_slave: Flag to indicate slave or master mode
144 * * On reset the SPI controller is configured to slave or master mode.
145 * In master mode baud rate divisor is set to 4, threshold value for TX FIFO
146 * not full interrupt is set to 1 and size of the word to be transferred as 8 bit.
148 * This function initializes the SPI controller to disable and clear all the
149 * interrupts, enable manual slave select and manual start, deselect all the
150 * chip select lines, and enable the SPI controller.
152 static void cdns_spi_init_hw(struct cdns_spi *xspi, bool is_slave)
157 ctrl_reg |= CDNS_SPI_CR_DEFAULT;
159 if (xspi->is_decoded_cs)
160 ctrl_reg |= CDNS_SPI_CR_PERI_SEL;
162 cdns_spi_write(xspi, CDNS_SPI_ER, CDNS_SPI_ER_DISABLE);
163 cdns_spi_write(xspi, CDNS_SPI_IDR, CDNS_SPI_IXR_ALL);
165 /* Clear the RX FIFO */
166 while (cdns_spi_read(xspi, CDNS_SPI_ISR) & CDNS_SPI_IXR_RXNEMTY)
167 cdns_spi_read(xspi, CDNS_SPI_RXD);
169 cdns_spi_write(xspi, CDNS_SPI_ISR, CDNS_SPI_IXR_ALL);
170 cdns_spi_write(xspi, CDNS_SPI_CR, ctrl_reg);
171 cdns_spi_write(xspi, CDNS_SPI_ER, CDNS_SPI_ER_ENABLE);
175 * cdns_spi_chipselect - Select or deselect the chip select line
176 * @spi: Pointer to the spi_device structure
177 * @is_high: Select(0) or deselect (1) the chip select line
179 static void cdns_spi_chipselect(struct spi_device *spi, bool is_high)
181 struct cdns_spi *xspi = spi_controller_get_devdata(spi->controller);
184 ctrl_reg = cdns_spi_read(xspi, CDNS_SPI_CR);
187 /* Deselect the slave */
188 ctrl_reg |= CDNS_SPI_CR_SSCTRL;
190 /* Select the slave */
191 ctrl_reg &= ~CDNS_SPI_CR_SSCTRL;
192 if (!(xspi->is_decoded_cs))
193 ctrl_reg |= ((~(CDNS_SPI_SS0 << spi_get_chipselect(spi, 0))) <<
197 ctrl_reg |= (spi_get_chipselect(spi, 0) << CDNS_SPI_SS_SHIFT) &
201 cdns_spi_write(xspi, CDNS_SPI_CR, ctrl_reg);
205 * cdns_spi_config_clock_mode - Sets clock polarity and phase
206 * @spi: Pointer to the spi_device structure
208 * Sets the requested clock polarity and phase.
210 static void cdns_spi_config_clock_mode(struct spi_device *spi)
212 struct cdns_spi *xspi = spi_controller_get_devdata(spi->controller);
213 u32 ctrl_reg, new_ctrl_reg;
215 new_ctrl_reg = cdns_spi_read(xspi, CDNS_SPI_CR);
216 ctrl_reg = new_ctrl_reg;
218 /* Set the SPI clock phase and clock polarity */
219 new_ctrl_reg &= ~(CDNS_SPI_CR_CPHA | CDNS_SPI_CR_CPOL);
220 if (spi->mode & SPI_CPHA)
221 new_ctrl_reg |= CDNS_SPI_CR_CPHA;
222 if (spi->mode & SPI_CPOL)
223 new_ctrl_reg |= CDNS_SPI_CR_CPOL;
225 if (new_ctrl_reg != ctrl_reg) {
227 * Just writing the CR register does not seem to apply the clock
228 * setting changes. This is problematic when changing the clock
229 * polarity as it will cause the SPI slave to see spurious clock
230 * transitions. To workaround the issue toggle the ER register.
232 cdns_spi_write(xspi, CDNS_SPI_ER, CDNS_SPI_ER_DISABLE);
233 cdns_spi_write(xspi, CDNS_SPI_CR, new_ctrl_reg);
234 cdns_spi_write(xspi, CDNS_SPI_ER, CDNS_SPI_ER_ENABLE);
239 * cdns_spi_config_clock_freq - Sets clock frequency
240 * @spi: Pointer to the spi_device structure
241 * @transfer: Pointer to the spi_transfer structure which provides
242 * information about next transfer setup parameters
244 * Sets the requested clock frequency.
245 * Note: If the requested frequency is not an exact match with what can be
246 * obtained using the prescalar value the driver sets the clock frequency which
247 * is lower than the requested frequency (maximum lower) for the transfer. If
248 * the requested frequency is higher or lower than that is supported by the SPI
249 * controller the driver will set the highest or lowest frequency supported by
252 static void cdns_spi_config_clock_freq(struct spi_device *spi,
253 struct spi_transfer *transfer)
255 struct cdns_spi *xspi = spi_controller_get_devdata(spi->controller);
256 u32 ctrl_reg, baud_rate_val;
257 unsigned long frequency;
259 frequency = xspi->clk_rate;
261 ctrl_reg = cdns_spi_read(xspi, CDNS_SPI_CR);
263 /* Set the clock frequency */
264 if (xspi->speed_hz != transfer->speed_hz) {
265 /* first valid value is 1 */
266 baud_rate_val = CDNS_SPI_BAUD_DIV_MIN;
267 while ((baud_rate_val < CDNS_SPI_BAUD_DIV_MAX) &&
268 (frequency / (2 << baud_rate_val)) > transfer->speed_hz)
271 ctrl_reg &= ~CDNS_SPI_CR_BAUD_DIV;
272 ctrl_reg |= baud_rate_val << CDNS_SPI_BAUD_DIV_SHIFT;
274 xspi->speed_hz = frequency / (2 << baud_rate_val);
276 cdns_spi_write(xspi, CDNS_SPI_CR, ctrl_reg);
280 * cdns_spi_setup_transfer - Configure SPI controller for specified transfer
281 * @spi: Pointer to the spi_device structure
282 * @transfer: Pointer to the spi_transfer structure which provides
283 * information about next transfer setup parameters
285 * Sets the operational mode of SPI controller for the next SPI transfer and
286 * sets the requested clock frequency.
290 static int cdns_spi_setup_transfer(struct spi_device *spi,
291 struct spi_transfer *transfer)
293 struct cdns_spi *xspi = spi_controller_get_devdata(spi->controller);
295 cdns_spi_config_clock_freq(spi, transfer);
297 dev_dbg(&spi->dev, "%s, mode %d, %u bits/w, %u clock speed\n",
298 __func__, spi->mode, spi->bits_per_word,
305 * cdns_spi_process_fifo - Fills the TX FIFO, and drain the RX FIFO
306 * @xspi: Pointer to the cdns_spi structure
307 * @ntx: Number of bytes to pack into the TX FIFO
308 * @nrx: Number of bytes to drain from the RX FIFO
310 static void cdns_spi_process_fifo(struct cdns_spi *xspi, int ntx, int nrx)
312 ntx = clamp(ntx, 0, xspi->tx_bytes);
313 nrx = clamp(nrx, 0, xspi->rx_bytes);
315 xspi->tx_bytes -= ntx;
316 xspi->rx_bytes -= nrx;
319 /* When xspi in busy condition, bytes may send failed,
320 * then spi control did't work thoroughly, add one byte delay
322 if (cdns_spi_read(xspi, CDNS_SPI_ISR) & CDNS_SPI_IXR_TXFULL)
327 cdns_spi_write(xspi, CDNS_SPI_TXD, *xspi->txbuf++);
329 cdns_spi_write(xspi, CDNS_SPI_TXD, 0);
335 u8 data = cdns_spi_read(xspi, CDNS_SPI_RXD);
338 *xspi->rxbuf++ = data;
346 * cdns_spi_irq - Interrupt service routine of the SPI controller
348 * @dev_id: Pointer to the xspi structure
350 * This function handles TX empty and Mode Fault interrupts only.
351 * On TX empty interrupt this function reads the received data from RX FIFO and
352 * fills the TX FIFO if there is any data remaining to be transferred.
353 * On Mode Fault interrupt this function indicates that transfer is completed,
354 * the SPI subsystem will identify the error as the remaining bytes to be
355 * transferred is non-zero.
357 * Return: IRQ_HANDLED when handled; IRQ_NONE otherwise.
359 static irqreturn_t cdns_spi_irq(int irq, void *dev_id)
361 struct spi_controller *ctlr = dev_id;
362 struct cdns_spi *xspi = spi_controller_get_devdata(ctlr);
367 intr_status = cdns_spi_read(xspi, CDNS_SPI_ISR);
368 cdns_spi_write(xspi, CDNS_SPI_ISR, intr_status);
370 if (intr_status & CDNS_SPI_IXR_MODF) {
371 /* Indicate that transfer is completed, the SPI subsystem will
372 * identify the error as the remaining bytes to be
373 * transferred is non-zero
375 cdns_spi_write(xspi, CDNS_SPI_IDR, CDNS_SPI_IXR_DEFAULT);
376 spi_finalize_current_transfer(ctlr);
377 status = IRQ_HANDLED;
378 } else if (intr_status & CDNS_SPI_IXR_TXOW) {
379 int threshold = cdns_spi_read(xspi, CDNS_SPI_THLD);
380 int trans_cnt = xspi->rx_bytes - xspi->tx_bytes;
383 trans_cnt -= threshold;
385 /* Set threshold to one if number of pending are
386 * less than half fifo
388 if (xspi->tx_bytes < xspi->tx_fifo_depth >> 1)
389 cdns_spi_write(xspi, CDNS_SPI_THLD, 1);
391 if (xspi->tx_bytes) {
392 cdns_spi_process_fifo(xspi, trans_cnt, trans_cnt);
394 cdns_spi_process_fifo(xspi, 0, trans_cnt);
395 cdns_spi_write(xspi, CDNS_SPI_IDR,
396 CDNS_SPI_IXR_DEFAULT);
397 spi_finalize_current_transfer(ctlr);
399 status = IRQ_HANDLED;
405 static int cdns_prepare_message(struct spi_controller *ctlr,
406 struct spi_message *msg)
408 if (!spi_controller_is_slave(ctlr))
409 cdns_spi_config_clock_mode(msg->spi);
414 * cdns_transfer_one - Initiates the SPI transfer
415 * @ctlr: Pointer to spi_controller structure
416 * @spi: Pointer to the spi_device structure
417 * @transfer: Pointer to the spi_transfer structure which provides
418 * information about next transfer parameters
420 * This function in master mode fills the TX FIFO, starts the SPI transfer and
421 * returns a positive transfer count so that core will wait for completion.
422 * This function in slave mode fills the TX FIFO and wait for transfer trigger.
424 * Return: Number of bytes transferred in the last transfer
426 static int cdns_transfer_one(struct spi_controller *ctlr,
427 struct spi_device *spi,
428 struct spi_transfer *transfer)
430 struct cdns_spi *xspi = spi_controller_get_devdata(ctlr);
432 xspi->txbuf = transfer->tx_buf;
433 xspi->rxbuf = transfer->rx_buf;
434 xspi->tx_bytes = transfer->len;
435 xspi->rx_bytes = transfer->len;
437 if (!spi_controller_is_slave(ctlr)) {
438 cdns_spi_setup_transfer(spi, transfer);
440 /* Set TX empty threshold to half of FIFO depth
441 * only if TX bytes are more than half FIFO depth.
443 if (xspi->tx_bytes > xspi->tx_fifo_depth)
444 cdns_spi_write(xspi, CDNS_SPI_THLD, xspi->tx_fifo_depth >> 1);
447 cdns_spi_process_fifo(xspi, xspi->tx_fifo_depth, 0);
448 spi_transfer_delay_exec(transfer);
450 cdns_spi_write(xspi, CDNS_SPI_IER, CDNS_SPI_IXR_DEFAULT);
451 return transfer->len;
455 * cdns_prepare_transfer_hardware - Prepares hardware for transfer.
456 * @ctlr: Pointer to the spi_controller structure which provides
457 * information about the controller.
459 * This function enables SPI master controller.
463 static int cdns_prepare_transfer_hardware(struct spi_controller *ctlr)
465 struct cdns_spi *xspi = spi_controller_get_devdata(ctlr);
467 cdns_spi_write(xspi, CDNS_SPI_ER, CDNS_SPI_ER_ENABLE);
473 * cdns_unprepare_transfer_hardware - Relaxes hardware after transfer
474 * @ctlr: Pointer to the spi_controller structure which provides
475 * information about the controller.
477 * This function disables the SPI master controller when no slave selected.
478 * This function flush out if any pending data in FIFO.
482 static int cdns_unprepare_transfer_hardware(struct spi_controller *ctlr)
484 struct cdns_spi *xspi = spi_controller_get_devdata(ctlr);
486 unsigned int cnt = xspi->tx_fifo_depth;
488 if (spi_controller_is_slave(ctlr)) {
490 cdns_spi_read(xspi, CDNS_SPI_RXD);
493 /* Disable the SPI if slave is deselected */
494 ctrl_reg = cdns_spi_read(xspi, CDNS_SPI_CR);
495 ctrl_reg = (ctrl_reg & CDNS_SPI_CR_SSCTRL) >> CDNS_SPI_SS_SHIFT;
496 if (ctrl_reg == CDNS_SPI_NOSS || spi_controller_is_slave(ctlr))
497 cdns_spi_write(xspi, CDNS_SPI_ER, CDNS_SPI_ER_DISABLE);
499 /* Reset to default */
500 cdns_spi_write(xspi, CDNS_SPI_THLD, 0x1);
505 * cdns_spi_detect_fifo_depth - Detect the FIFO depth of the hardware
506 * @xspi: Pointer to the cdns_spi structure
508 * The depth of the TX FIFO is a synthesis configuration parameter of the SPI
509 * IP. The FIFO threshold register is sized so that its maximum value can be the
510 * FIFO size - 1. This is used to detect the size of the FIFO.
512 static void cdns_spi_detect_fifo_depth(struct cdns_spi *xspi)
514 /* The MSBs will get truncated giving us the size of the FIFO */
515 cdns_spi_write(xspi, CDNS_SPI_THLD, 0xffff);
516 xspi->tx_fifo_depth = cdns_spi_read(xspi, CDNS_SPI_THLD) + 1;
518 /* Reset to default */
519 cdns_spi_write(xspi, CDNS_SPI_THLD, 0x1);
523 * cdns_slave_abort - Abort slave transfer
524 * @ctlr: Pointer to the spi_controller structure
526 * This function abort slave transfer if there any transfer timeout.
530 static int cdns_slave_abort(struct spi_controller *ctlr)
532 struct cdns_spi *xspi = spi_controller_get_devdata(ctlr);
535 intr_status = cdns_spi_read(xspi, CDNS_SPI_ISR);
536 cdns_spi_write(xspi, CDNS_SPI_ISR, intr_status);
537 cdns_spi_write(xspi, CDNS_SPI_IDR, (CDNS_SPI_IXR_MODF | CDNS_SPI_IXR_RXNEMTY));
538 spi_finalize_current_transfer(ctlr);
544 * cdns_spi_probe - Probe method for the SPI driver
545 * @pdev: Pointer to the platform_device structure
547 * This function initializes the driver data structures and the hardware.
549 * Return: 0 on success and error value on error
551 static int cdns_spi_probe(struct platform_device *pdev)
554 struct spi_controller *ctlr;
555 struct cdns_spi *xspi;
559 slave = of_property_read_bool(pdev->dev.of_node, "spi-slave");
561 ctlr = spi_alloc_slave(&pdev->dev, sizeof(*xspi));
563 ctlr = spi_alloc_master(&pdev->dev, sizeof(*xspi));
568 xspi = spi_controller_get_devdata(ctlr);
569 ctlr->dev.of_node = pdev->dev.of_node;
570 platform_set_drvdata(pdev, ctlr);
572 xspi->regs = devm_platform_ioremap_resource(pdev, 0);
573 if (IS_ERR(xspi->regs)) {
574 ret = PTR_ERR(xspi->regs);
578 xspi->pclk = devm_clk_get(&pdev->dev, "pclk");
579 if (IS_ERR(xspi->pclk)) {
580 dev_err(&pdev->dev, "pclk clock not found.\n");
581 ret = PTR_ERR(xspi->pclk);
585 ret = clk_prepare_enable(xspi->pclk);
587 dev_err(&pdev->dev, "Unable to enable APB clock.\n");
591 if (!spi_controller_is_slave(ctlr)) {
592 xspi->ref_clk = devm_clk_get(&pdev->dev, "ref_clk");
593 if (IS_ERR(xspi->ref_clk)) {
594 dev_err(&pdev->dev, "ref_clk clock not found.\n");
595 ret = PTR_ERR(xspi->ref_clk);
599 ret = clk_prepare_enable(xspi->ref_clk);
601 dev_err(&pdev->dev, "Unable to enable device clock.\n");
605 pm_runtime_use_autosuspend(&pdev->dev);
606 pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
607 pm_runtime_get_noresume(&pdev->dev);
608 pm_runtime_set_active(&pdev->dev);
609 pm_runtime_enable(&pdev->dev);
611 ret = of_property_read_u32(pdev->dev.of_node, "num-cs", &num_cs);
613 ctlr->num_chipselect = CDNS_SPI_DEFAULT_NUM_CS;
615 ctlr->num_chipselect = num_cs;
617 ret = of_property_read_u32(pdev->dev.of_node, "is-decoded-cs",
618 &xspi->is_decoded_cs);
620 xspi->is_decoded_cs = 0;
623 cdns_spi_detect_fifo_depth(xspi);
625 /* SPI controller initializations */
626 cdns_spi_init_hw(xspi, spi_controller_is_slave(ctlr));
628 irq = platform_get_irq(pdev, 0);
634 ret = devm_request_irq(&pdev->dev, irq, cdns_spi_irq,
635 0, pdev->name, ctlr);
638 dev_err(&pdev->dev, "request_irq failed\n");
642 ctlr->use_gpio_descriptors = true;
643 ctlr->prepare_transfer_hardware = cdns_prepare_transfer_hardware;
644 ctlr->prepare_message = cdns_prepare_message;
645 ctlr->transfer_one = cdns_transfer_one;
646 ctlr->unprepare_transfer_hardware = cdns_unprepare_transfer_hardware;
647 ctlr->mode_bits = SPI_CPOL | SPI_CPHA;
648 ctlr->bits_per_word_mask = SPI_BPW_MASK(8);
650 if (!spi_controller_is_slave(ctlr)) {
651 ctlr->mode_bits |= SPI_CS_HIGH;
652 ctlr->set_cs = cdns_spi_chipselect;
653 ctlr->auto_runtime_pm = true;
654 xspi->clk_rate = clk_get_rate(xspi->ref_clk);
655 /* Set to default valid value */
656 ctlr->max_speed_hz = xspi->clk_rate / 4;
657 xspi->speed_hz = ctlr->max_speed_hz;
658 pm_runtime_mark_last_busy(&pdev->dev);
659 pm_runtime_put_autosuspend(&pdev->dev);
661 ctlr->mode_bits |= SPI_NO_CS;
662 ctlr->slave_abort = cdns_slave_abort;
664 ret = spi_register_controller(ctlr);
666 dev_err(&pdev->dev, "spi_register_controller failed\n");
673 if (!spi_controller_is_slave(ctlr)) {
674 pm_runtime_set_suspended(&pdev->dev);
675 pm_runtime_disable(&pdev->dev);
676 clk_disable_unprepare(xspi->ref_clk);
679 clk_disable_unprepare(xspi->pclk);
681 spi_controller_put(ctlr);
686 * cdns_spi_remove - Remove method for the SPI driver
687 * @pdev: Pointer to the platform_device structure
689 * This function is called if a device is physically removed from the system or
690 * if the driver module is being unloaded. It frees all resources allocated to
693 * Return: 0 on success and error value on error
695 static void cdns_spi_remove(struct platform_device *pdev)
697 struct spi_controller *ctlr = platform_get_drvdata(pdev);
698 struct cdns_spi *xspi = spi_controller_get_devdata(ctlr);
700 cdns_spi_write(xspi, CDNS_SPI_ER, CDNS_SPI_ER_DISABLE);
702 clk_disable_unprepare(xspi->ref_clk);
703 clk_disable_unprepare(xspi->pclk);
704 pm_runtime_set_suspended(&pdev->dev);
705 pm_runtime_disable(&pdev->dev);
707 spi_unregister_controller(ctlr);
711 * cdns_spi_suspend - Suspend method for the SPI driver
712 * @dev: Address of the platform_device structure
714 * This function disables the SPI controller and
715 * changes the driver state to "suspend"
717 * Return: 0 on success and error value on error
719 static int __maybe_unused cdns_spi_suspend(struct device *dev)
721 struct spi_controller *ctlr = dev_get_drvdata(dev);
723 return spi_controller_suspend(ctlr);
727 * cdns_spi_resume - Resume method for the SPI driver
728 * @dev: Address of the platform_device structure
730 * This function changes the driver state to "ready"
732 * Return: 0 on success and error value on error
734 static int __maybe_unused cdns_spi_resume(struct device *dev)
736 struct spi_controller *ctlr = dev_get_drvdata(dev);
737 struct cdns_spi *xspi = spi_controller_get_devdata(ctlr);
739 cdns_spi_init_hw(xspi, spi_controller_is_slave(ctlr));
740 return spi_controller_resume(ctlr);
744 * cdns_spi_runtime_resume - Runtime resume method for the SPI driver
745 * @dev: Address of the platform_device structure
747 * This function enables the clocks
749 * Return: 0 on success and error value on error
751 static int __maybe_unused cdns_spi_runtime_resume(struct device *dev)
753 struct spi_controller *ctlr = dev_get_drvdata(dev);
754 struct cdns_spi *xspi = spi_controller_get_devdata(ctlr);
757 ret = clk_prepare_enable(xspi->pclk);
759 dev_err(dev, "Cannot enable APB clock.\n");
763 ret = clk_prepare_enable(xspi->ref_clk);
765 dev_err(dev, "Cannot enable device clock.\n");
766 clk_disable_unprepare(xspi->pclk);
773 * cdns_spi_runtime_suspend - Runtime suspend method for the SPI driver
774 * @dev: Address of the platform_device structure
776 * This function disables the clocks
780 static int __maybe_unused cdns_spi_runtime_suspend(struct device *dev)
782 struct spi_controller *ctlr = dev_get_drvdata(dev);
783 struct cdns_spi *xspi = spi_controller_get_devdata(ctlr);
785 clk_disable_unprepare(xspi->ref_clk);
786 clk_disable_unprepare(xspi->pclk);
791 static const struct dev_pm_ops cdns_spi_dev_pm_ops = {
792 SET_RUNTIME_PM_OPS(cdns_spi_runtime_suspend,
793 cdns_spi_runtime_resume, NULL)
794 SET_SYSTEM_SLEEP_PM_OPS(cdns_spi_suspend, cdns_spi_resume)
797 static const struct of_device_id cdns_spi_of_match[] = {
798 { .compatible = "xlnx,zynq-spi-r1p6" },
799 { .compatible = "cdns,spi-r1p6" },
800 { /* end of table */ }
802 MODULE_DEVICE_TABLE(of, cdns_spi_of_match);
804 /* cdns_spi_driver - This structure defines the SPI subsystem platform driver */
805 static struct platform_driver cdns_spi_driver = {
806 .probe = cdns_spi_probe,
807 .remove_new = cdns_spi_remove,
809 .name = CDNS_SPI_NAME,
810 .of_match_table = cdns_spi_of_match,
811 .pm = &cdns_spi_dev_pm_ops,
815 module_platform_driver(cdns_spi_driver);
817 MODULE_AUTHOR("Xilinx, Inc.");
818 MODULE_DESCRIPTION("Cadence SPI driver");
819 MODULE_LICENSE("GPL");