Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Cadence SPI controller driver (master mode only) |
| 3 | * |
| 4 | * Copyright (C) 2008 - 2014 Xilinx, Inc. |
| 5 | * |
| 6 | * based on Blackfin On-Chip SPI Driver (spi_bfin5xx.c) |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it under |
| 9 | * the terms of the GNU General Public License version 2 as published by the |
| 10 | * Free Software Foundation; either version 2 of the License, or (at your |
| 11 | * option) any later version. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/clk.h> |
| 15 | #include <linux/delay.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/io.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/of_irq.h> |
| 20 | #include <linux/of_address.h> |
| 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/spi/spi.h> |
| 23 | |
| 24 | /* Name of this driver */ |
| 25 | #define CDNS_SPI_NAME "cdns-spi" |
| 26 | |
| 27 | /* Register offset definitions */ |
Shubhrajyoti Datta | 2474667 | 2016-04-05 23:37:49 +0530 | [diff] [blame^] | 28 | #define CDNS_SPI_CR 0x00 /* Configuration Register, RW */ |
| 29 | #define CDNS_SPI_ISR 0x04 /* Interrupt Status Register, RO */ |
| 30 | #define CDNS_SPI_IER 0x08 /* Interrupt Enable Register, WO */ |
| 31 | #define CDNS_SPI_IDR 0x0c /* Interrupt Disable Register, WO */ |
| 32 | #define CDNS_SPI_IMR 0x10 /* Interrupt Enabled Mask Register, RO */ |
| 33 | #define CDNS_SPI_ER 0x14 /* Enable/Disable Register, RW */ |
| 34 | #define CDNS_SPI_DR 0x18 /* Delay Register, RW */ |
| 35 | #define CDNS_SPI_TXD 0x1C /* Data Transmit Register, WO */ |
| 36 | #define CDNS_SPI_RXD 0x20 /* Data Receive Register, RO */ |
| 37 | #define CDNS_SPI_SICR 0x24 /* Slave Idle Count Register, RW */ |
| 38 | #define CDNS_SPI_THLD 0x28 /* Transmit FIFO Watermark Register,RW */ |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 39 | |
| 40 | /* |
| 41 | * SPI Configuration Register bit Masks |
| 42 | * |
| 43 | * This register contains various control bits that affect the operation |
| 44 | * of the SPI controller |
| 45 | */ |
Shubhrajyoti Datta | 2474667 | 2016-04-05 23:37:49 +0530 | [diff] [blame^] | 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) |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 60 | |
| 61 | /* |
| 62 | * SPI Configuration Register - Baud rate and slave select |
| 63 | * |
| 64 | * These are the values used in the calculation of baud rate divisor and |
| 65 | * setting the slave select. |
| 66 | */ |
| 67 | |
| 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 | |
| 74 | /* |
| 75 | * SPI Interrupt Registers bit Masks |
| 76 | * |
| 77 | * All the four interrupt registers (Status/Mask/Enable/Disable) have the same |
| 78 | * bit definitions. |
| 79 | */ |
Shubhrajyoti Datta | 2474667 | 2016-04-05 23:37:49 +0530 | [diff] [blame^] | 80 | #define CDNS_SPI_IXR_TXOW 0x00000004 /* SPI TX FIFO Overwater */ |
| 81 | #define CDNS_SPI_IXR_MODF 0x00000002 /* SPI Mode Fault */ |
| 82 | #define CDNS_SPI_IXR_RXNEMTY 0x00000010 /* SPI RX FIFO Not Empty */ |
| 83 | #define CDNS_SPI_IXR_DEFAULT (CDNS_SPI_IXR_TXOW | \ |
| 84 | CDNS_SPI_IXR_MODF) |
| 85 | #define CDNS_SPI_IXR_TXFULL 0x00000008 /* SPI TX Full */ |
| 86 | #define CDNS_SPI_IXR_ALL 0x0000007F /* SPI all interrupts */ |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 87 | |
| 88 | /* |
| 89 | * SPI Enable Register bit Masks |
| 90 | * |
| 91 | * This register is used to enable or disable the SPI controller |
| 92 | */ |
Shubhrajyoti Datta | 2474667 | 2016-04-05 23:37:49 +0530 | [diff] [blame^] | 93 | #define CDNS_SPI_ER_ENABLE 0x00000001 /* SPI Enable Bit Mask */ |
| 94 | #define CDNS_SPI_ER_DISABLE 0x0 /* SPI Disable Bit Mask */ |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 95 | |
| 96 | /* SPI FIFO depth in bytes */ |
| 97 | #define CDNS_SPI_FIFO_DEPTH 128 |
| 98 | |
| 99 | /* Default number of chip select lines */ |
| 100 | #define CDNS_SPI_DEFAULT_NUM_CS 4 |
| 101 | |
| 102 | /** |
| 103 | * struct cdns_spi - This definition defines spi driver instance |
| 104 | * @regs: Virtual address of the SPI controller registers |
| 105 | * @ref_clk: Pointer to the peripheral clock |
| 106 | * @pclk: Pointer to the APB clock |
| 107 | * @speed_hz: Current SPI bus clock speed in Hz |
| 108 | * @txbuf: Pointer to the TX buffer |
| 109 | * @rxbuf: Pointer to the RX buffer |
| 110 | * @tx_bytes: Number of bytes left to transfer |
| 111 | * @rx_bytes: Number of bytes requested |
| 112 | * @dev_busy: Device busy flag |
| 113 | * @is_decoded_cs: Flag for decoder property set or not |
| 114 | */ |
| 115 | struct cdns_spi { |
| 116 | void __iomem *regs; |
| 117 | struct clk *ref_clk; |
| 118 | struct clk *pclk; |
| 119 | u32 speed_hz; |
| 120 | const u8 *txbuf; |
| 121 | u8 *rxbuf; |
| 122 | int tx_bytes; |
| 123 | int rx_bytes; |
| 124 | u8 dev_busy; |
| 125 | u32 is_decoded_cs; |
| 126 | }; |
| 127 | |
| 128 | /* Macros for the SPI controller read/write */ |
| 129 | static inline u32 cdns_spi_read(struct cdns_spi *xspi, u32 offset) |
| 130 | { |
| 131 | return readl_relaxed(xspi->regs + offset); |
| 132 | } |
| 133 | |
| 134 | static inline void cdns_spi_write(struct cdns_spi *xspi, u32 offset, u32 val) |
| 135 | { |
| 136 | writel_relaxed(val, xspi->regs + offset); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * cdns_spi_init_hw - Initialize the hardware and configure the SPI controller |
| 141 | * @xspi: Pointer to the cdns_spi structure |
| 142 | * |
| 143 | * On reset the SPI controller is configured to be in master mode, baud rate |
| 144 | * divisor is set to 4, threshold value for TX FIFO not full interrupt is set |
| 145 | * to 1 and size of the word to be transferred as 8 bit. |
| 146 | * This function initializes the SPI controller to disable and clear all the |
| 147 | * interrupts, enable manual slave select and manual start, deselect all the |
| 148 | * chip select lines, and enable the SPI controller. |
| 149 | */ |
| 150 | static void cdns_spi_init_hw(struct cdns_spi *xspi) |
| 151 | { |
Shubhrajyoti Datta | 2474667 | 2016-04-05 23:37:49 +0530 | [diff] [blame^] | 152 | u32 ctrl_reg = CDNS_SPI_CR_DEFAULT; |
Lars-Peter Clausen | ee0ebe81 | 2014-11-27 16:12:18 +0100 | [diff] [blame] | 153 | |
| 154 | if (xspi->is_decoded_cs) |
Shubhrajyoti Datta | 2474667 | 2016-04-05 23:37:49 +0530 | [diff] [blame^] | 155 | ctrl_reg |= CDNS_SPI_CR_PERI_SEL; |
Lars-Peter Clausen | ee0ebe81 | 2014-11-27 16:12:18 +0100 | [diff] [blame] | 156 | |
Shubhrajyoti Datta | 2474667 | 2016-04-05 23:37:49 +0530 | [diff] [blame^] | 157 | cdns_spi_write(xspi, CDNS_SPI_ER, CDNS_SPI_ER_DISABLE); |
| 158 | cdns_spi_write(xspi, CDNS_SPI_IDR, CDNS_SPI_IXR_ALL); |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 159 | |
| 160 | /* Clear the RX FIFO */ |
Shubhrajyoti Datta | 2474667 | 2016-04-05 23:37:49 +0530 | [diff] [blame^] | 161 | while (cdns_spi_read(xspi, CDNS_SPI_ISR) & CDNS_SPI_IXR_RXNEMTY) |
| 162 | cdns_spi_read(xspi, CDNS_SPI_RXD); |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 163 | |
Shubhrajyoti Datta | 2474667 | 2016-04-05 23:37:49 +0530 | [diff] [blame^] | 164 | cdns_spi_write(xspi, CDNS_SPI_ISR, CDNS_SPI_IXR_ALL); |
| 165 | cdns_spi_write(xspi, CDNS_SPI_CR, ctrl_reg); |
| 166 | cdns_spi_write(xspi, CDNS_SPI_ER, CDNS_SPI_ER_ENABLE); |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /** |
| 170 | * cdns_spi_chipselect - Select or deselect the chip select line |
| 171 | * @spi: Pointer to the spi_device structure |
| 172 | * @is_on: Select(0) or deselect (1) the chip select line |
| 173 | */ |
| 174 | static void cdns_spi_chipselect(struct spi_device *spi, bool is_high) |
| 175 | { |
| 176 | struct cdns_spi *xspi = spi_master_get_devdata(spi->master); |
| 177 | u32 ctrl_reg; |
| 178 | |
Shubhrajyoti Datta | 2474667 | 2016-04-05 23:37:49 +0530 | [diff] [blame^] | 179 | ctrl_reg = cdns_spi_read(xspi, CDNS_SPI_CR); |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 180 | |
| 181 | if (is_high) { |
| 182 | /* Deselect the slave */ |
Shubhrajyoti Datta | 2474667 | 2016-04-05 23:37:49 +0530 | [diff] [blame^] | 183 | ctrl_reg |= CDNS_SPI_CR_SSCTRL; |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 184 | } else { |
| 185 | /* Select the slave */ |
Shubhrajyoti Datta | 2474667 | 2016-04-05 23:37:49 +0530 | [diff] [blame^] | 186 | ctrl_reg &= ~CDNS_SPI_CR_SSCTRL; |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 187 | if (!(xspi->is_decoded_cs)) |
| 188 | ctrl_reg |= ((~(CDNS_SPI_SS0 << spi->chip_select)) << |
| 189 | CDNS_SPI_SS_SHIFT) & |
Shubhrajyoti Datta | 2474667 | 2016-04-05 23:37:49 +0530 | [diff] [blame^] | 190 | CDNS_SPI_CR_SSCTRL; |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 191 | else |
| 192 | ctrl_reg |= (spi->chip_select << CDNS_SPI_SS_SHIFT) & |
Shubhrajyoti Datta | 2474667 | 2016-04-05 23:37:49 +0530 | [diff] [blame^] | 193 | CDNS_SPI_CR_SSCTRL; |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 194 | } |
| 195 | |
Shubhrajyoti Datta | 2474667 | 2016-04-05 23:37:49 +0530 | [diff] [blame^] | 196 | cdns_spi_write(xspi, CDNS_SPI_CR, ctrl_reg); |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /** |
| 200 | * cdns_spi_config_clock_mode - Sets clock polarity and phase |
| 201 | * @spi: Pointer to the spi_device structure |
| 202 | * |
| 203 | * Sets the requested clock polarity and phase. |
| 204 | */ |
| 205 | static void cdns_spi_config_clock_mode(struct spi_device *spi) |
| 206 | { |
| 207 | struct cdns_spi *xspi = spi_master_get_devdata(spi->master); |
Lars-Peter Clausen | a39e65e | 2014-07-10 11:26:28 +0200 | [diff] [blame] | 208 | u32 ctrl_reg, new_ctrl_reg; |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 209 | |
Shubhrajyoti Datta | 2474667 | 2016-04-05 23:37:49 +0530 | [diff] [blame^] | 210 | new_ctrl_reg = ctrl_reg = cdns_spi_read(xspi, CDNS_SPI_CR); |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 211 | |
| 212 | /* Set the SPI clock phase and clock polarity */ |
Shubhrajyoti Datta | 2474667 | 2016-04-05 23:37:49 +0530 | [diff] [blame^] | 213 | new_ctrl_reg &= ~(CDNS_SPI_CR_CPHA | CDNS_SPI_CR_CPOL); |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 214 | if (spi->mode & SPI_CPHA) |
Shubhrajyoti Datta | 2474667 | 2016-04-05 23:37:49 +0530 | [diff] [blame^] | 215 | new_ctrl_reg |= CDNS_SPI_CR_CPHA; |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 216 | if (spi->mode & SPI_CPOL) |
Shubhrajyoti Datta | 2474667 | 2016-04-05 23:37:49 +0530 | [diff] [blame^] | 217 | new_ctrl_reg |= CDNS_SPI_CR_CPOL; |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 218 | |
Lars-Peter Clausen | a39e65e | 2014-07-10 11:26:28 +0200 | [diff] [blame] | 219 | if (new_ctrl_reg != ctrl_reg) { |
| 220 | /* |
| 221 | * Just writing the CR register does not seem to apply the clock |
| 222 | * setting changes. This is problematic when changing the clock |
| 223 | * polarity as it will cause the SPI slave to see spurious clock |
| 224 | * transitions. To workaround the issue toggle the ER register. |
| 225 | */ |
Shubhrajyoti Datta | 2474667 | 2016-04-05 23:37:49 +0530 | [diff] [blame^] | 226 | cdns_spi_write(xspi, CDNS_SPI_ER, CDNS_SPI_ER_DISABLE); |
| 227 | cdns_spi_write(xspi, CDNS_SPI_CR, new_ctrl_reg); |
| 228 | cdns_spi_write(xspi, CDNS_SPI_ER, CDNS_SPI_ER_ENABLE); |
Lars-Peter Clausen | a39e65e | 2014-07-10 11:26:28 +0200 | [diff] [blame] | 229 | } |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /** |
| 233 | * cdns_spi_config_clock_freq - Sets clock frequency |
| 234 | * @spi: Pointer to the spi_device structure |
| 235 | * @transfer: Pointer to the spi_transfer structure which provides |
| 236 | * information about next transfer setup parameters |
| 237 | * |
| 238 | * Sets the requested clock frequency. |
| 239 | * Note: If the requested frequency is not an exact match with what can be |
| 240 | * obtained using the prescalar value the driver sets the clock frequency which |
| 241 | * is lower than the requested frequency (maximum lower) for the transfer. If |
| 242 | * the requested frequency is higher or lower than that is supported by the SPI |
| 243 | * controller the driver will set the highest or lowest frequency supported by |
| 244 | * controller. |
| 245 | */ |
| 246 | static void cdns_spi_config_clock_freq(struct spi_device *spi, |
| 247 | struct spi_transfer *transfer) |
| 248 | { |
| 249 | struct cdns_spi *xspi = spi_master_get_devdata(spi->master); |
| 250 | u32 ctrl_reg, baud_rate_val; |
| 251 | unsigned long frequency; |
| 252 | |
| 253 | frequency = clk_get_rate(xspi->ref_clk); |
| 254 | |
Shubhrajyoti Datta | 2474667 | 2016-04-05 23:37:49 +0530 | [diff] [blame^] | 255 | ctrl_reg = cdns_spi_read(xspi, CDNS_SPI_CR); |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 256 | |
| 257 | /* Set the clock frequency */ |
| 258 | if (xspi->speed_hz != transfer->speed_hz) { |
| 259 | /* first valid value is 1 */ |
| 260 | baud_rate_val = CDNS_SPI_BAUD_DIV_MIN; |
| 261 | while ((baud_rate_val < CDNS_SPI_BAUD_DIV_MAX) && |
| 262 | (frequency / (2 << baud_rate_val)) > transfer->speed_hz) |
| 263 | baud_rate_val++; |
| 264 | |
Shubhrajyoti Datta | 2474667 | 2016-04-05 23:37:49 +0530 | [diff] [blame^] | 265 | ctrl_reg &= ~CDNS_SPI_CR_BAUD_DIV; |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 266 | ctrl_reg |= baud_rate_val << CDNS_SPI_BAUD_DIV_SHIFT; |
| 267 | |
| 268 | xspi->speed_hz = frequency / (2 << baud_rate_val); |
| 269 | } |
Shubhrajyoti Datta | 2474667 | 2016-04-05 23:37:49 +0530 | [diff] [blame^] | 270 | cdns_spi_write(xspi, CDNS_SPI_CR, ctrl_reg); |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /** |
| 274 | * cdns_spi_setup_transfer - Configure SPI controller for specified transfer |
| 275 | * @spi: Pointer to the spi_device structure |
| 276 | * @transfer: Pointer to the spi_transfer structure which provides |
| 277 | * information about next transfer setup parameters |
| 278 | * |
| 279 | * Sets the operational mode of SPI controller for the next SPI transfer and |
| 280 | * sets the requested clock frequency. |
| 281 | * |
| 282 | * Return: Always 0 |
| 283 | */ |
| 284 | static int cdns_spi_setup_transfer(struct spi_device *spi, |
| 285 | struct spi_transfer *transfer) |
| 286 | { |
| 287 | struct cdns_spi *xspi = spi_master_get_devdata(spi->master); |
| 288 | |
| 289 | cdns_spi_config_clock_freq(spi, transfer); |
| 290 | |
| 291 | dev_dbg(&spi->dev, "%s, mode %d, %u bits/w, %u clock speed\n", |
| 292 | __func__, spi->mode, spi->bits_per_word, |
| 293 | xspi->speed_hz); |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * cdns_spi_fill_tx_fifo - Fills the TX FIFO with as many bytes as possible |
| 300 | * @xspi: Pointer to the cdns_spi structure |
| 301 | */ |
| 302 | static void cdns_spi_fill_tx_fifo(struct cdns_spi *xspi) |
| 303 | { |
| 304 | unsigned long trans_cnt = 0; |
| 305 | |
| 306 | while ((trans_cnt < CDNS_SPI_FIFO_DEPTH) && |
| 307 | (xspi->tx_bytes > 0)) { |
| 308 | if (xspi->txbuf) |
Shubhrajyoti Datta | 2474667 | 2016-04-05 23:37:49 +0530 | [diff] [blame^] | 309 | cdns_spi_write(xspi, CDNS_SPI_TXD, *xspi->txbuf++); |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 310 | else |
Shubhrajyoti Datta | 2474667 | 2016-04-05 23:37:49 +0530 | [diff] [blame^] | 311 | cdns_spi_write(xspi, CDNS_SPI_TXD, 0); |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 312 | |
| 313 | xspi->tx_bytes--; |
| 314 | trans_cnt++; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * cdns_spi_irq - Interrupt service routine of the SPI controller |
| 320 | * @irq: IRQ number |
| 321 | * @dev_id: Pointer to the xspi structure |
| 322 | * |
| 323 | * This function handles TX empty and Mode Fault interrupts only. |
| 324 | * On TX empty interrupt this function reads the received data from RX FIFO and |
| 325 | * fills the TX FIFO if there is any data remaining to be transferred. |
| 326 | * On Mode Fault interrupt this function indicates that transfer is completed, |
| 327 | * the SPI subsystem will identify the error as the remaining bytes to be |
| 328 | * transferred is non-zero. |
| 329 | * |
| 330 | * Return: IRQ_HANDLED when handled; IRQ_NONE otherwise. |
| 331 | */ |
| 332 | static irqreturn_t cdns_spi_irq(int irq, void *dev_id) |
| 333 | { |
| 334 | struct spi_master *master = dev_id; |
| 335 | struct cdns_spi *xspi = spi_master_get_devdata(master); |
| 336 | u32 intr_status, status; |
| 337 | |
| 338 | status = IRQ_NONE; |
Shubhrajyoti Datta | 2474667 | 2016-04-05 23:37:49 +0530 | [diff] [blame^] | 339 | intr_status = cdns_spi_read(xspi, CDNS_SPI_ISR); |
| 340 | cdns_spi_write(xspi, CDNS_SPI_ISR, intr_status); |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 341 | |
Shubhrajyoti Datta | 2474667 | 2016-04-05 23:37:49 +0530 | [diff] [blame^] | 342 | if (intr_status & CDNS_SPI_IXR_MODF) { |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 343 | /* Indicate that transfer is completed, the SPI subsystem will |
| 344 | * identify the error as the remaining bytes to be |
| 345 | * transferred is non-zero |
| 346 | */ |
Shubhrajyoti Datta | 2474667 | 2016-04-05 23:37:49 +0530 | [diff] [blame^] | 347 | cdns_spi_write(xspi, CDNS_SPI_IDR, CDNS_SPI_IXR_DEFAULT); |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 348 | spi_finalize_current_transfer(master); |
| 349 | status = IRQ_HANDLED; |
Shubhrajyoti Datta | 2474667 | 2016-04-05 23:37:49 +0530 | [diff] [blame^] | 350 | } else if (intr_status & CDNS_SPI_IXR_TXOW) { |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 351 | unsigned long trans_cnt; |
| 352 | |
| 353 | trans_cnt = xspi->rx_bytes - xspi->tx_bytes; |
| 354 | |
| 355 | /* Read out the data from the RX FIFO */ |
| 356 | while (trans_cnt) { |
| 357 | u8 data; |
| 358 | |
Shubhrajyoti Datta | 2474667 | 2016-04-05 23:37:49 +0530 | [diff] [blame^] | 359 | data = cdns_spi_read(xspi, CDNS_SPI_RXD); |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 360 | if (xspi->rxbuf) |
| 361 | *xspi->rxbuf++ = data; |
| 362 | |
| 363 | xspi->rx_bytes--; |
| 364 | trans_cnt--; |
| 365 | } |
| 366 | |
| 367 | if (xspi->tx_bytes) { |
| 368 | /* There is more data to send */ |
| 369 | cdns_spi_fill_tx_fifo(xspi); |
| 370 | } else { |
| 371 | /* Transfer is completed */ |
Shubhrajyoti Datta | 2474667 | 2016-04-05 23:37:49 +0530 | [diff] [blame^] | 372 | cdns_spi_write(xspi, CDNS_SPI_IDR, |
| 373 | CDNS_SPI_IXR_DEFAULT); |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 374 | spi_finalize_current_transfer(master); |
| 375 | } |
| 376 | status = IRQ_HANDLED; |
| 377 | } |
| 378 | |
| 379 | return status; |
| 380 | } |
Lars-Peter Clausen | b48b948 | 2014-07-10 11:26:29 +0200 | [diff] [blame] | 381 | static int cdns_prepare_message(struct spi_master *master, |
| 382 | struct spi_message *msg) |
| 383 | { |
| 384 | cdns_spi_config_clock_mode(msg->spi); |
| 385 | return 0; |
| 386 | } |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 387 | |
| 388 | /** |
| 389 | * cdns_transfer_one - Initiates the SPI transfer |
| 390 | * @master: Pointer to spi_master structure |
| 391 | * @spi: Pointer to the spi_device structure |
| 392 | * @transfer: Pointer to the spi_transfer structure which provides |
| 393 | * information about next transfer parameters |
| 394 | * |
| 395 | * This function fills the TX FIFO, starts the SPI transfer and |
| 396 | * returns a positive transfer count so that core will wait for completion. |
| 397 | * |
| 398 | * Return: Number of bytes transferred in the last transfer |
| 399 | */ |
| 400 | static int cdns_transfer_one(struct spi_master *master, |
| 401 | struct spi_device *spi, |
| 402 | struct spi_transfer *transfer) |
| 403 | { |
| 404 | struct cdns_spi *xspi = spi_master_get_devdata(master); |
| 405 | |
| 406 | xspi->txbuf = transfer->tx_buf; |
| 407 | xspi->rxbuf = transfer->rx_buf; |
| 408 | xspi->tx_bytes = transfer->len; |
| 409 | xspi->rx_bytes = transfer->len; |
| 410 | |
| 411 | cdns_spi_setup_transfer(spi, transfer); |
| 412 | |
| 413 | cdns_spi_fill_tx_fifo(xspi); |
| 414 | |
Shubhrajyoti Datta | 2474667 | 2016-04-05 23:37:49 +0530 | [diff] [blame^] | 415 | cdns_spi_write(xspi, CDNS_SPI_IER, CDNS_SPI_IXR_DEFAULT); |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 416 | return transfer->len; |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * cdns_prepare_transfer_hardware - Prepares hardware for transfer. |
| 421 | * @master: Pointer to the spi_master structure which provides |
| 422 | * information about the controller. |
| 423 | * |
| 424 | * This function enables SPI master controller. |
| 425 | * |
| 426 | * Return: 0 always |
| 427 | */ |
| 428 | static int cdns_prepare_transfer_hardware(struct spi_master *master) |
| 429 | { |
| 430 | struct cdns_spi *xspi = spi_master_get_devdata(master); |
| 431 | |
Shubhrajyoti Datta | 2474667 | 2016-04-05 23:37:49 +0530 | [diff] [blame^] | 432 | cdns_spi_write(xspi, CDNS_SPI_ER, CDNS_SPI_ER_ENABLE); |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 433 | |
| 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * cdns_unprepare_transfer_hardware - Relaxes hardware after transfer |
| 439 | * @master: Pointer to the spi_master structure which provides |
| 440 | * information about the controller. |
| 441 | * |
| 442 | * This function disables the SPI master controller. |
| 443 | * |
| 444 | * Return: 0 always |
| 445 | */ |
| 446 | static int cdns_unprepare_transfer_hardware(struct spi_master *master) |
| 447 | { |
| 448 | struct cdns_spi *xspi = spi_master_get_devdata(master); |
| 449 | |
Shubhrajyoti Datta | 2474667 | 2016-04-05 23:37:49 +0530 | [diff] [blame^] | 450 | cdns_spi_write(xspi, CDNS_SPI_ER, CDNS_SPI_ER_DISABLE); |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 451 | |
| 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | /** |
| 456 | * cdns_spi_probe - Probe method for the SPI driver |
| 457 | * @pdev: Pointer to the platform_device structure |
| 458 | * |
| 459 | * This function initializes the driver data structures and the hardware. |
| 460 | * |
| 461 | * Return: 0 on success and error value on error |
| 462 | */ |
| 463 | static int cdns_spi_probe(struct platform_device *pdev) |
| 464 | { |
| 465 | int ret = 0, irq; |
| 466 | struct spi_master *master; |
| 467 | struct cdns_spi *xspi; |
| 468 | struct resource *res; |
| 469 | u32 num_cs; |
| 470 | |
| 471 | master = spi_alloc_master(&pdev->dev, sizeof(*xspi)); |
Shubhrajyoti Datta | 15a1c50 | 2016-04-05 23:37:48 +0530 | [diff] [blame] | 472 | if (!master) |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 473 | return -ENOMEM; |
| 474 | |
| 475 | xspi = spi_master_get_devdata(master); |
| 476 | master->dev.of_node = pdev->dev.of_node; |
| 477 | platform_set_drvdata(pdev, master); |
| 478 | |
| 479 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 480 | xspi->regs = devm_ioremap_resource(&pdev->dev, res); |
| 481 | if (IS_ERR(xspi->regs)) { |
| 482 | ret = PTR_ERR(xspi->regs); |
| 483 | goto remove_master; |
| 484 | } |
| 485 | |
| 486 | xspi->pclk = devm_clk_get(&pdev->dev, "pclk"); |
| 487 | if (IS_ERR(xspi->pclk)) { |
| 488 | dev_err(&pdev->dev, "pclk clock not found.\n"); |
| 489 | ret = PTR_ERR(xspi->pclk); |
| 490 | goto remove_master; |
| 491 | } |
| 492 | |
| 493 | xspi->ref_clk = devm_clk_get(&pdev->dev, "ref_clk"); |
| 494 | if (IS_ERR(xspi->ref_clk)) { |
| 495 | dev_err(&pdev->dev, "ref_clk clock not found.\n"); |
| 496 | ret = PTR_ERR(xspi->ref_clk); |
| 497 | goto remove_master; |
| 498 | } |
| 499 | |
| 500 | ret = clk_prepare_enable(xspi->pclk); |
| 501 | if (ret) { |
| 502 | dev_err(&pdev->dev, "Unable to enable APB clock.\n"); |
| 503 | goto remove_master; |
| 504 | } |
| 505 | |
| 506 | ret = clk_prepare_enable(xspi->ref_clk); |
| 507 | if (ret) { |
| 508 | dev_err(&pdev->dev, "Unable to enable device clock.\n"); |
| 509 | goto clk_dis_apb; |
| 510 | } |
| 511 | |
Paul Cercueil | 3cc2910 | 2014-11-27 16:12:17 +0100 | [diff] [blame] | 512 | ret = of_property_read_u32(pdev->dev.of_node, "num-cs", &num_cs); |
| 513 | if (ret < 0) |
| 514 | master->num_chipselect = CDNS_SPI_DEFAULT_NUM_CS; |
| 515 | else |
| 516 | master->num_chipselect = num_cs; |
| 517 | |
| 518 | ret = of_property_read_u32(pdev->dev.of_node, "is-decoded-cs", |
| 519 | &xspi->is_decoded_cs); |
| 520 | if (ret < 0) |
| 521 | xspi->is_decoded_cs = 0; |
| 522 | |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 523 | /* SPI controller initializations */ |
| 524 | cdns_spi_init_hw(xspi); |
| 525 | |
| 526 | irq = platform_get_irq(pdev, 0); |
| 527 | if (irq <= 0) { |
| 528 | ret = -ENXIO; |
| 529 | dev_err(&pdev->dev, "irq number is invalid\n"); |
| 530 | goto remove_master; |
| 531 | } |
| 532 | |
| 533 | ret = devm_request_irq(&pdev->dev, irq, cdns_spi_irq, |
| 534 | 0, pdev->name, master); |
| 535 | if (ret != 0) { |
| 536 | ret = -ENXIO; |
| 537 | dev_err(&pdev->dev, "request_irq failed\n"); |
| 538 | goto remove_master; |
| 539 | } |
| 540 | |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 541 | master->prepare_transfer_hardware = cdns_prepare_transfer_hardware; |
Lars-Peter Clausen | b48b948 | 2014-07-10 11:26:29 +0200 | [diff] [blame] | 542 | master->prepare_message = cdns_prepare_message; |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 543 | master->transfer_one = cdns_transfer_one; |
| 544 | master->unprepare_transfer_hardware = cdns_unprepare_transfer_hardware; |
| 545 | master->set_cs = cdns_spi_chipselect; |
| 546 | master->mode_bits = SPI_CPOL | SPI_CPHA; |
| 547 | |
| 548 | /* Set to default valid value */ |
| 549 | master->max_speed_hz = clk_get_rate(xspi->ref_clk) / 4; |
| 550 | xspi->speed_hz = master->max_speed_hz; |
| 551 | |
| 552 | master->bits_per_word_mask = SPI_BPW_MASK(8); |
| 553 | |
| 554 | ret = spi_register_master(master); |
| 555 | if (ret) { |
| 556 | dev_err(&pdev->dev, "spi_register_master failed\n"); |
| 557 | goto clk_dis_all; |
| 558 | } |
| 559 | |
| 560 | return ret; |
| 561 | |
| 562 | clk_dis_all: |
| 563 | clk_disable_unprepare(xspi->ref_clk); |
| 564 | clk_dis_apb: |
| 565 | clk_disable_unprepare(xspi->pclk); |
| 566 | remove_master: |
| 567 | spi_master_put(master); |
| 568 | return ret; |
| 569 | } |
| 570 | |
| 571 | /** |
| 572 | * cdns_spi_remove - Remove method for the SPI driver |
| 573 | * @pdev: Pointer to the platform_device structure |
| 574 | * |
| 575 | * This function is called if a device is physically removed from the system or |
| 576 | * if the driver module is being unloaded. It frees all resources allocated to |
| 577 | * the device. |
| 578 | * |
| 579 | * Return: 0 on success and error value on error |
| 580 | */ |
| 581 | static int cdns_spi_remove(struct platform_device *pdev) |
| 582 | { |
| 583 | struct spi_master *master = platform_get_drvdata(pdev); |
| 584 | struct cdns_spi *xspi = spi_master_get_devdata(master); |
| 585 | |
Shubhrajyoti Datta | 2474667 | 2016-04-05 23:37:49 +0530 | [diff] [blame^] | 586 | cdns_spi_write(xspi, CDNS_SPI_ER, CDNS_SPI_ER_DISABLE); |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 587 | |
| 588 | clk_disable_unprepare(xspi->ref_clk); |
| 589 | clk_disable_unprepare(xspi->pclk); |
| 590 | |
| 591 | spi_unregister_master(master); |
| 592 | |
| 593 | return 0; |
| 594 | } |
| 595 | |
| 596 | /** |
| 597 | * cdns_spi_suspend - Suspend method for the SPI driver |
| 598 | * @dev: Address of the platform_device structure |
| 599 | * |
| 600 | * This function disables the SPI controller and |
| 601 | * changes the driver state to "suspend" |
| 602 | * |
| 603 | * Return: Always 0 |
| 604 | */ |
| 605 | static int __maybe_unused cdns_spi_suspend(struct device *dev) |
| 606 | { |
Geliang Tang | 9033a5f | 2016-01-01 20:28:59 +0800 | [diff] [blame] | 607 | struct platform_device *pdev = to_platform_device(dev); |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 608 | struct spi_master *master = platform_get_drvdata(pdev); |
| 609 | struct cdns_spi *xspi = spi_master_get_devdata(master); |
| 610 | |
| 611 | spi_master_suspend(master); |
| 612 | |
| 613 | clk_disable_unprepare(xspi->ref_clk); |
| 614 | |
| 615 | clk_disable_unprepare(xspi->pclk); |
| 616 | |
| 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | /** |
| 621 | * cdns_spi_resume - Resume method for the SPI driver |
| 622 | * @dev: Address of the platform_device structure |
| 623 | * |
| 624 | * This function changes the driver state to "ready" |
| 625 | * |
| 626 | * Return: 0 on success and error value on error |
| 627 | */ |
| 628 | static int __maybe_unused cdns_spi_resume(struct device *dev) |
| 629 | { |
Geliang Tang | 9033a5f | 2016-01-01 20:28:59 +0800 | [diff] [blame] | 630 | struct platform_device *pdev = to_platform_device(dev); |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 631 | struct spi_master *master = platform_get_drvdata(pdev); |
| 632 | struct cdns_spi *xspi = spi_master_get_devdata(master); |
| 633 | int ret = 0; |
| 634 | |
| 635 | ret = clk_prepare_enable(xspi->pclk); |
| 636 | if (ret) { |
| 637 | dev_err(dev, "Cannot enable APB clock.\n"); |
| 638 | return ret; |
| 639 | } |
| 640 | |
| 641 | ret = clk_prepare_enable(xspi->ref_clk); |
| 642 | if (ret) { |
| 643 | dev_err(dev, "Cannot enable device clock.\n"); |
| 644 | clk_disable(xspi->pclk); |
| 645 | return ret; |
| 646 | } |
| 647 | spi_master_resume(master); |
| 648 | |
| 649 | return 0; |
| 650 | } |
| 651 | |
| 652 | static SIMPLE_DEV_PM_OPS(cdns_spi_dev_pm_ops, cdns_spi_suspend, |
| 653 | cdns_spi_resume); |
| 654 | |
Jingoo Han | f7f994a | 2014-06-03 21:01:40 +0900 | [diff] [blame] | 655 | static const struct of_device_id cdns_spi_of_match[] = { |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 656 | { .compatible = "xlnx,zynq-spi-r1p6" }, |
| 657 | { .compatible = "cdns,spi-r1p6" }, |
| 658 | { /* end of table */ } |
| 659 | }; |
| 660 | MODULE_DEVICE_TABLE(of, cdns_spi_of_match); |
| 661 | |
| 662 | /* cdns_spi_driver - This structure defines the SPI subsystem platform driver */ |
| 663 | static struct platform_driver cdns_spi_driver = { |
| 664 | .probe = cdns_spi_probe, |
| 665 | .remove = cdns_spi_remove, |
| 666 | .driver = { |
| 667 | .name = CDNS_SPI_NAME, |
Harini Katakam | c474b38 | 2014-04-14 14:36:53 +0530 | [diff] [blame] | 668 | .of_match_table = cdns_spi_of_match, |
| 669 | .pm = &cdns_spi_dev_pm_ops, |
| 670 | }, |
| 671 | }; |
| 672 | |
| 673 | module_platform_driver(cdns_spi_driver); |
| 674 | |
| 675 | MODULE_AUTHOR("Xilinx, Inc."); |
| 676 | MODULE_DESCRIPTION("Cadence SPI driver"); |
| 677 | MODULE_LICENSE("GPL"); |