Naga Sureshkumar Relli | 67dca5e | 2019-04-01 13:29:13 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2019 Xilinx, Inc. |
| 4 | * |
| 5 | * Author: Naga Sureshkumar Relli <nagasure@xilinx.com> |
| 6 | */ |
| 7 | |
| 8 | #include <linux/clk.h> |
| 9 | #include <linux/delay.h> |
Naga Sureshkumar Relli | 67dca5e | 2019-04-01 13:29:13 +0530 | [diff] [blame] | 10 | #include <linux/interrupt.h> |
| 11 | #include <linux/io.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/of_irq.h> |
| 14 | #include <linux/of_address.h> |
| 15 | #include <linux/platform_device.h> |
| 16 | #include <linux/spi/spi.h> |
| 17 | #include <linux/workqueue.h> |
| 18 | #include <linux/spi/spi-mem.h> |
| 19 | |
| 20 | /* Register offset definitions */ |
| 21 | #define ZYNQ_QSPI_CONFIG_OFFSET 0x00 /* Configuration Register, RW */ |
| 22 | #define ZYNQ_QSPI_STATUS_OFFSET 0x04 /* Interrupt Status Register, RO */ |
| 23 | #define ZYNQ_QSPI_IEN_OFFSET 0x08 /* Interrupt Enable Register, WO */ |
| 24 | #define ZYNQ_QSPI_IDIS_OFFSET 0x0C /* Interrupt Disable Reg, WO */ |
| 25 | #define ZYNQ_QSPI_IMASK_OFFSET 0x10 /* Interrupt Enabled Mask Reg,RO */ |
| 26 | #define ZYNQ_QSPI_ENABLE_OFFSET 0x14 /* Enable/Disable Register, RW */ |
| 27 | #define ZYNQ_QSPI_DELAY_OFFSET 0x18 /* Delay Register, RW */ |
| 28 | #define ZYNQ_QSPI_TXD_00_00_OFFSET 0x1C /* Transmit 4-byte inst, WO */ |
| 29 | #define ZYNQ_QSPI_TXD_00_01_OFFSET 0x80 /* Transmit 1-byte inst, WO */ |
| 30 | #define ZYNQ_QSPI_TXD_00_10_OFFSET 0x84 /* Transmit 2-byte inst, WO */ |
| 31 | #define ZYNQ_QSPI_TXD_00_11_OFFSET 0x88 /* Transmit 3-byte inst, WO */ |
| 32 | #define ZYNQ_QSPI_RXD_OFFSET 0x20 /* Data Receive Register, RO */ |
| 33 | #define ZYNQ_QSPI_SIC_OFFSET 0x24 /* Slave Idle Count Register, RW */ |
| 34 | #define ZYNQ_QSPI_TX_THRESH_OFFSET 0x28 /* TX FIFO Watermark Reg, RW */ |
| 35 | #define ZYNQ_QSPI_RX_THRESH_OFFSET 0x2C /* RX FIFO Watermark Reg, RW */ |
| 36 | #define ZYNQ_QSPI_GPIO_OFFSET 0x30 /* GPIO Register, RW */ |
| 37 | #define ZYNQ_QSPI_LINEAR_CFG_OFFSET 0xA0 /* Linear Adapter Config Ref, RW */ |
| 38 | #define ZYNQ_QSPI_MOD_ID_OFFSET 0xFC /* Module ID Register, RO */ |
| 39 | |
| 40 | /* |
| 41 | * QSPI Configuration Register bit Masks |
| 42 | * |
| 43 | * This register contains various control bits that effect the operation |
| 44 | * of the QSPI controller |
| 45 | */ |
| 46 | #define ZYNQ_QSPI_CONFIG_IFMODE_MASK BIT(31) /* Flash Memory Interface */ |
| 47 | #define ZYNQ_QSPI_CONFIG_MANSRT_MASK BIT(16) /* Manual TX Start */ |
| 48 | #define ZYNQ_QSPI_CONFIG_MANSRTEN_MASK BIT(15) /* Enable Manual TX Mode */ |
| 49 | #define ZYNQ_QSPI_CONFIG_SSFORCE_MASK BIT(14) /* Manual Chip Select */ |
| 50 | #define ZYNQ_QSPI_CONFIG_BDRATE_MASK GENMASK(5, 3) /* Baud Rate Mask */ |
| 51 | #define ZYNQ_QSPI_CONFIG_CPHA_MASK BIT(2) /* Clock Phase Control */ |
| 52 | #define ZYNQ_QSPI_CONFIG_CPOL_MASK BIT(1) /* Clock Polarity Control */ |
Naga Sureshkumar Relli | 67dca5e | 2019-04-01 13:29:13 +0530 | [diff] [blame] | 53 | #define ZYNQ_QSPI_CONFIG_FWIDTH_MASK GENMASK(7, 6) /* FIFO width */ |
| 54 | #define ZYNQ_QSPI_CONFIG_MSTREN_MASK BIT(0) /* Master Mode */ |
| 55 | |
| 56 | /* |
| 57 | * QSPI Configuration Register - Baud rate and slave select |
| 58 | * |
| 59 | * These are the values used in the calculation of baud rate divisor and |
| 60 | * setting the slave select. |
| 61 | */ |
Miquel Raynal | 941be72 | 2019-11-08 15:07:40 +0100 | [diff] [blame] | 62 | #define ZYNQ_QSPI_CONFIG_BAUD_DIV_MAX GENMASK(2, 0) /* Baud rate maximum */ |
| 63 | #define ZYNQ_QSPI_CONFIG_BAUD_DIV_SHIFT 3 /* Baud rate divisor shift */ |
Miquel Raynal | dffaf74 | 2019-11-08 15:07:42 +0100 | [diff] [blame] | 64 | #define ZYNQ_QSPI_CONFIG_PCS BIT(10) /* Peripheral Chip Select */ |
Naga Sureshkumar Relli | 67dca5e | 2019-04-01 13:29:13 +0530 | [diff] [blame] | 65 | |
| 66 | /* |
| 67 | * QSPI Interrupt Registers bit Masks |
| 68 | * |
| 69 | * All the four interrupt registers (Status/Mask/Enable/Disable) have the same |
| 70 | * bit definitions. |
| 71 | */ |
| 72 | #define ZYNQ_QSPI_IXR_RX_OVERFLOW_MASK BIT(0) /* QSPI RX FIFO Overflow */ |
| 73 | #define ZYNQ_QSPI_IXR_TXNFULL_MASK BIT(2) /* QSPI TX FIFO Overflow */ |
| 74 | #define ZYNQ_QSPI_IXR_TXFULL_MASK BIT(3) /* QSPI TX FIFO is full */ |
| 75 | #define ZYNQ_QSPI_IXR_RXNEMTY_MASK BIT(4) /* QSPI RX FIFO Not Empty */ |
| 76 | #define ZYNQ_QSPI_IXR_RXF_FULL_MASK BIT(5) /* QSPI RX FIFO is full */ |
| 77 | #define ZYNQ_QSPI_IXR_TXF_UNDRFLOW_MASK BIT(6) /* QSPI TX FIFO Underflow */ |
| 78 | #define ZYNQ_QSPI_IXR_ALL_MASK (ZYNQ_QSPI_IXR_RX_OVERFLOW_MASK | \ |
| 79 | ZYNQ_QSPI_IXR_TXNFULL_MASK | \ |
| 80 | ZYNQ_QSPI_IXR_TXFULL_MASK | \ |
| 81 | ZYNQ_QSPI_IXR_RXNEMTY_MASK | \ |
| 82 | ZYNQ_QSPI_IXR_RXF_FULL_MASK | \ |
| 83 | ZYNQ_QSPI_IXR_TXF_UNDRFLOW_MASK) |
| 84 | #define ZYNQ_QSPI_IXR_RXTX_MASK (ZYNQ_QSPI_IXR_TXNFULL_MASK | \ |
| 85 | ZYNQ_QSPI_IXR_RXNEMTY_MASK) |
| 86 | |
| 87 | /* |
| 88 | * QSPI Enable Register bit Masks |
| 89 | * |
| 90 | * This register is used to enable or disable the QSPI controller |
| 91 | */ |
| 92 | #define ZYNQ_QSPI_ENABLE_ENABLE_MASK BIT(0) /* QSPI Enable Bit Mask */ |
| 93 | |
| 94 | /* |
| 95 | * QSPI Linear Configuration Register |
| 96 | * |
| 97 | * It is named Linear Configuration but it controls other modes when not in |
| 98 | * linear mode also. |
| 99 | */ |
Miquel Raynal | 044ac82 | 2019-11-08 15:07:41 +0100 | [diff] [blame] | 100 | #define ZYNQ_QSPI_LCFG_TWO_MEM BIT(30) /* LQSPI Two memories */ |
| 101 | #define ZYNQ_QSPI_LCFG_SEP_BUS BIT(29) /* LQSPI Separate bus */ |
| 102 | #define ZYNQ_QSPI_LCFG_U_PAGE BIT(28) /* LQSPI Upper Page */ |
Naga Sureshkumar Relli | 67dca5e | 2019-04-01 13:29:13 +0530 | [diff] [blame] | 103 | |
| 104 | #define ZYNQ_QSPI_LCFG_DUMMY_SHIFT 8 |
| 105 | |
| 106 | #define ZYNQ_QSPI_FAST_READ_QOUT_CODE 0x6B /* read instruction code */ |
| 107 | #define ZYNQ_QSPI_FIFO_DEPTH 63 /* FIFO depth in words */ |
| 108 | #define ZYNQ_QSPI_RX_THRESHOLD 32 /* Rx FIFO threshold level */ |
| 109 | #define ZYNQ_QSPI_TX_THRESHOLD 1 /* Tx FIFO threshold level */ |
| 110 | |
| 111 | /* |
| 112 | * The modebits configurable by the driver to make the SPI support different |
| 113 | * data formats |
| 114 | */ |
| 115 | #define ZYNQ_QSPI_MODEBITS (SPI_CPOL | SPI_CPHA) |
| 116 | |
| 117 | /* Default number of chip selects */ |
| 118 | #define ZYNQ_QSPI_DEFAULT_NUM_CS 1 |
| 119 | |
| 120 | /** |
| 121 | * struct zynq_qspi - Defines qspi driver instance |
| 122 | * @regs: Virtual address of the QSPI controller registers |
| 123 | * @refclk: Pointer to the peripheral clock |
| 124 | * @pclk: Pointer to the APB clock |
| 125 | * @irq: IRQ number |
| 126 | * @txbuf: Pointer to the TX buffer |
| 127 | * @rxbuf: Pointer to the RX buffer |
| 128 | * @tx_bytes: Number of bytes left to transfer |
| 129 | * @rx_bytes: Number of bytes left to receive |
| 130 | * @data_completion: completion structure |
| 131 | */ |
| 132 | struct zynq_qspi { |
| 133 | struct device *dev; |
| 134 | void __iomem *regs; |
| 135 | struct clk *refclk; |
| 136 | struct clk *pclk; |
| 137 | int irq; |
| 138 | u8 *txbuf; |
| 139 | u8 *rxbuf; |
| 140 | int tx_bytes; |
| 141 | int rx_bytes; |
| 142 | struct completion data_completion; |
| 143 | }; |
| 144 | |
| 145 | /* |
| 146 | * Inline functions for the QSPI controller read/write |
| 147 | */ |
| 148 | static inline u32 zynq_qspi_read(struct zynq_qspi *xqspi, u32 offset) |
| 149 | { |
| 150 | return readl_relaxed(xqspi->regs + offset); |
| 151 | } |
| 152 | |
| 153 | static inline void zynq_qspi_write(struct zynq_qspi *xqspi, u32 offset, |
| 154 | u32 val) |
| 155 | { |
| 156 | writel_relaxed(val, xqspi->regs + offset); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * zynq_qspi_init_hw - Initialize the hardware |
| 161 | * @xqspi: Pointer to the zynq_qspi structure |
| 162 | * |
| 163 | * The default settings of the QSPI controller's configurable parameters on |
| 164 | * reset are |
| 165 | * - Master mode |
| 166 | * - Baud rate divisor is set to 2 |
| 167 | * - Tx threshold set to 1l Rx threshold set to 32 |
| 168 | * - Flash memory interface mode enabled |
| 169 | * - Size of the word to be transferred as 8 bit |
| 170 | * This function performs the following actions |
| 171 | * - Disable and clear all the interrupts |
| 172 | * - Enable manual slave select |
| 173 | * - Enable manual start |
| 174 | * - Deselect all the chip select lines |
| 175 | * - Set the size of the word to be transferred as 32 bit |
| 176 | * - Set the little endian mode of TX FIFO and |
| 177 | * - Enable the QSPI controller |
| 178 | */ |
| 179 | static void zynq_qspi_init_hw(struct zynq_qspi *xqspi) |
| 180 | { |
| 181 | u32 config_reg; |
| 182 | |
| 183 | zynq_qspi_write(xqspi, ZYNQ_QSPI_ENABLE_OFFSET, 0); |
| 184 | zynq_qspi_write(xqspi, ZYNQ_QSPI_IDIS_OFFSET, ZYNQ_QSPI_IXR_ALL_MASK); |
| 185 | |
| 186 | /* Disable linear mode as the boot loader may have used it */ |
| 187 | zynq_qspi_write(xqspi, ZYNQ_QSPI_LINEAR_CFG_OFFSET, 0); |
| 188 | |
| 189 | /* Clear the RX FIFO */ |
| 190 | while (zynq_qspi_read(xqspi, ZYNQ_QSPI_STATUS_OFFSET) & |
| 191 | ZYNQ_QSPI_IXR_RXNEMTY_MASK) |
| 192 | zynq_qspi_read(xqspi, ZYNQ_QSPI_RXD_OFFSET); |
| 193 | |
| 194 | zynq_qspi_write(xqspi, ZYNQ_QSPI_STATUS_OFFSET, ZYNQ_QSPI_IXR_ALL_MASK); |
| 195 | config_reg = zynq_qspi_read(xqspi, ZYNQ_QSPI_CONFIG_OFFSET); |
| 196 | config_reg &= ~(ZYNQ_QSPI_CONFIG_MSTREN_MASK | |
| 197 | ZYNQ_QSPI_CONFIG_CPOL_MASK | |
| 198 | ZYNQ_QSPI_CONFIG_CPHA_MASK | |
| 199 | ZYNQ_QSPI_CONFIG_BDRATE_MASK | |
| 200 | ZYNQ_QSPI_CONFIG_SSFORCE_MASK | |
| 201 | ZYNQ_QSPI_CONFIG_MANSRTEN_MASK | |
| 202 | ZYNQ_QSPI_CONFIG_MANSRT_MASK); |
| 203 | config_reg |= (ZYNQ_QSPI_CONFIG_MSTREN_MASK | |
| 204 | ZYNQ_QSPI_CONFIG_SSFORCE_MASK | |
| 205 | ZYNQ_QSPI_CONFIG_FWIDTH_MASK | |
| 206 | ZYNQ_QSPI_CONFIG_IFMODE_MASK); |
| 207 | zynq_qspi_write(xqspi, ZYNQ_QSPI_CONFIG_OFFSET, config_reg); |
| 208 | |
| 209 | zynq_qspi_write(xqspi, ZYNQ_QSPI_RX_THRESH_OFFSET, |
| 210 | ZYNQ_QSPI_RX_THRESHOLD); |
| 211 | zynq_qspi_write(xqspi, ZYNQ_QSPI_TX_THRESH_OFFSET, |
| 212 | ZYNQ_QSPI_TX_THRESHOLD); |
| 213 | |
| 214 | zynq_qspi_write(xqspi, ZYNQ_QSPI_ENABLE_OFFSET, |
| 215 | ZYNQ_QSPI_ENABLE_ENABLE_MASK); |
| 216 | } |
| 217 | |
| 218 | static bool zynq_qspi_supports_op(struct spi_mem *mem, |
| 219 | const struct spi_mem_op *op) |
| 220 | { |
| 221 | if (!spi_mem_default_supports_op(mem, op)) |
| 222 | return false; |
| 223 | |
| 224 | /* |
| 225 | * The number of address bytes should be equal to or less than 3 bytes. |
| 226 | */ |
| 227 | if (op->addr.nbytes > 3) |
| 228 | return false; |
| 229 | |
| 230 | return true; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * zynq_qspi_rxfifo_op - Read 1..4 bytes from RxFIFO to RX buffer |
| 235 | * @xqspi: Pointer to the zynq_qspi structure |
| 236 | * @size: Number of bytes to be read (1..4) |
| 237 | */ |
| 238 | static void zynq_qspi_rxfifo_op(struct zynq_qspi *xqspi, unsigned int size) |
| 239 | { |
| 240 | u32 data; |
| 241 | |
| 242 | data = zynq_qspi_read(xqspi, ZYNQ_QSPI_RXD_OFFSET); |
| 243 | |
| 244 | if (xqspi->rxbuf) { |
| 245 | memcpy(xqspi->rxbuf, ((u8 *)&data) + 4 - size, size); |
| 246 | xqspi->rxbuf += size; |
| 247 | } |
| 248 | |
| 249 | xqspi->rx_bytes -= size; |
| 250 | if (xqspi->rx_bytes < 0) |
| 251 | xqspi->rx_bytes = 0; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * zynq_qspi_txfifo_op - Write 1..4 bytes from TX buffer to TxFIFO |
| 256 | * @xqspi: Pointer to the zynq_qspi structure |
| 257 | * @size: Number of bytes to be written (1..4) |
| 258 | */ |
| 259 | static void zynq_qspi_txfifo_op(struct zynq_qspi *xqspi, unsigned int size) |
| 260 | { |
| 261 | static const unsigned int offset[4] = { |
| 262 | ZYNQ_QSPI_TXD_00_01_OFFSET, ZYNQ_QSPI_TXD_00_10_OFFSET, |
| 263 | ZYNQ_QSPI_TXD_00_11_OFFSET, ZYNQ_QSPI_TXD_00_00_OFFSET }; |
| 264 | u32 data; |
| 265 | |
| 266 | if (xqspi->txbuf) { |
| 267 | data = 0xffffffff; |
| 268 | memcpy(&data, xqspi->txbuf, size); |
| 269 | xqspi->txbuf += size; |
| 270 | } else { |
| 271 | data = 0; |
| 272 | } |
| 273 | |
| 274 | xqspi->tx_bytes -= size; |
| 275 | zynq_qspi_write(xqspi, offset[size - 1], data); |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * zynq_qspi_chipselect - Select or deselect the chip select line |
| 280 | * @spi: Pointer to the spi_device structure |
| 281 | * @assert: 1 for select or 0 for deselect the chip select line |
| 282 | */ |
| 283 | static void zynq_qspi_chipselect(struct spi_device *spi, bool assert) |
| 284 | { |
Miquel Raynal | 9b10fa3 | 2019-11-08 11:59:15 +0100 | [diff] [blame] | 285 | struct spi_controller *ctlr = spi->master; |
| 286 | struct zynq_qspi *xqspi = spi_controller_get_devdata(ctlr); |
Naga Sureshkumar Relli | 67dca5e | 2019-04-01 13:29:13 +0530 | [diff] [blame] | 287 | u32 config_reg; |
| 288 | |
Miquel Raynal | dffaf74 | 2019-11-08 15:07:42 +0100 | [diff] [blame] | 289 | /* Ground the line to assert the CS */ |
Naga Sureshkumar Relli | 67dca5e | 2019-04-01 13:29:13 +0530 | [diff] [blame] | 290 | config_reg = zynq_qspi_read(xqspi, ZYNQ_QSPI_CONFIG_OFFSET); |
Miquel Raynal | dffaf74 | 2019-11-08 15:07:42 +0100 | [diff] [blame] | 291 | if (assert) |
| 292 | config_reg &= ~ZYNQ_QSPI_CONFIG_PCS; |
| 293 | else |
| 294 | config_reg |= ZYNQ_QSPI_CONFIG_PCS; |
Naga Sureshkumar Relli | 67dca5e | 2019-04-01 13:29:13 +0530 | [diff] [blame] | 295 | |
| 296 | zynq_qspi_write(xqspi, ZYNQ_QSPI_CONFIG_OFFSET, config_reg); |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * zynq_qspi_config_op - Configure QSPI controller for specified transfer |
| 301 | * @xqspi: Pointer to the zynq_qspi structure |
| 302 | * @qspi: Pointer to the spi_device structure |
| 303 | * |
| 304 | * Sets the operational mode of QSPI controller for the next QSPI transfer and |
| 305 | * sets the requested clock frequency. |
| 306 | * |
| 307 | * Return: 0 on success and -EINVAL on invalid input parameter |
| 308 | * |
| 309 | * Note: If the requested frequency is not an exact match with what can be |
| 310 | * obtained using the prescalar value, the driver sets the clock frequency which |
| 311 | * is lower than the requested frequency (maximum lower) for the transfer. If |
| 312 | * the requested frequency is higher or lower than that is supported by the QSPI |
| 313 | * controller the driver will set the highest or lowest frequency supported by |
| 314 | * controller. |
| 315 | */ |
| 316 | static int zynq_qspi_config_op(struct zynq_qspi *xqspi, struct spi_device *spi) |
| 317 | { |
| 318 | u32 config_reg, baud_rate_val = 0; |
| 319 | |
| 320 | /* |
| 321 | * Set the clock frequency |
| 322 | * The baud rate divisor is not a direct mapping to the value written |
| 323 | * into the configuration register (config_reg[5:3]) |
| 324 | * i.e. 000 - divide by 2 |
| 325 | * 001 - divide by 4 |
| 326 | * ---------------- |
| 327 | * 111 - divide by 256 |
| 328 | */ |
Miquel Raynal | 941be72 | 2019-11-08 15:07:40 +0100 | [diff] [blame] | 329 | while ((baud_rate_val < ZYNQ_QSPI_CONFIG_BAUD_DIV_MAX) && |
Naga Sureshkumar Relli | 67dca5e | 2019-04-01 13:29:13 +0530 | [diff] [blame] | 330 | (clk_get_rate(xqspi->refclk) / (2 << baud_rate_val)) > |
| 331 | spi->max_speed_hz) |
| 332 | baud_rate_val++; |
| 333 | |
| 334 | config_reg = zynq_qspi_read(xqspi, ZYNQ_QSPI_CONFIG_OFFSET); |
| 335 | |
| 336 | /* Set the QSPI clock phase and clock polarity */ |
| 337 | config_reg &= (~ZYNQ_QSPI_CONFIG_CPHA_MASK) & |
| 338 | (~ZYNQ_QSPI_CONFIG_CPOL_MASK); |
| 339 | if (spi->mode & SPI_CPHA) |
| 340 | config_reg |= ZYNQ_QSPI_CONFIG_CPHA_MASK; |
| 341 | if (spi->mode & SPI_CPOL) |
| 342 | config_reg |= ZYNQ_QSPI_CONFIG_CPOL_MASK; |
| 343 | |
| 344 | config_reg &= ~ZYNQ_QSPI_CONFIG_BDRATE_MASK; |
Miquel Raynal | 941be72 | 2019-11-08 15:07:40 +0100 | [diff] [blame] | 345 | config_reg |= (baud_rate_val << ZYNQ_QSPI_CONFIG_BAUD_DIV_SHIFT); |
Naga Sureshkumar Relli | 67dca5e | 2019-04-01 13:29:13 +0530 | [diff] [blame] | 346 | zynq_qspi_write(xqspi, ZYNQ_QSPI_CONFIG_OFFSET, config_reg); |
| 347 | |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * zynq_qspi_setup - Configure the QSPI controller |
| 353 | * @spi: Pointer to the spi_device structure |
| 354 | * |
| 355 | * Sets the operational mode of QSPI controller for the next QSPI transfer, baud |
| 356 | * rate and divisor value to setup the requested qspi clock. |
| 357 | * |
| 358 | * Return: 0 on success and error value on failure |
| 359 | */ |
| 360 | static int zynq_qspi_setup_op(struct spi_device *spi) |
| 361 | { |
Miquel Raynal | 9b10fa3 | 2019-11-08 11:59:15 +0100 | [diff] [blame] | 362 | struct spi_controller *ctlr = spi->master; |
| 363 | struct zynq_qspi *qspi = spi_controller_get_devdata(ctlr); |
Naga Sureshkumar Relli | 67dca5e | 2019-04-01 13:29:13 +0530 | [diff] [blame] | 364 | |
Miquel Raynal | 9b10fa3 | 2019-11-08 11:59:15 +0100 | [diff] [blame] | 365 | if (ctlr->busy) |
Naga Sureshkumar Relli | 67dca5e | 2019-04-01 13:29:13 +0530 | [diff] [blame] | 366 | return -EBUSY; |
| 367 | |
| 368 | clk_enable(qspi->refclk); |
| 369 | clk_enable(qspi->pclk); |
| 370 | zynq_qspi_write(qspi, ZYNQ_QSPI_ENABLE_OFFSET, |
| 371 | ZYNQ_QSPI_ENABLE_ENABLE_MASK); |
| 372 | |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * zynq_qspi_write_op - Fills the TX FIFO with as many bytes as possible |
| 378 | * @xqspi: Pointer to the zynq_qspi structure |
| 379 | * @txcount: Maximum number of words to write |
| 380 | * @txempty: Indicates that TxFIFO is empty |
| 381 | */ |
| 382 | static void zynq_qspi_write_op(struct zynq_qspi *xqspi, int txcount, |
| 383 | bool txempty) |
| 384 | { |
| 385 | int count, len, k; |
| 386 | |
| 387 | len = xqspi->tx_bytes; |
| 388 | if (len && len < 4) { |
| 389 | /* |
| 390 | * We must empty the TxFIFO between accesses to TXD0, |
| 391 | * TXD1, TXD2, TXD3. |
| 392 | */ |
| 393 | if (txempty) |
| 394 | zynq_qspi_txfifo_op(xqspi, len); |
| 395 | |
| 396 | return; |
| 397 | } |
| 398 | |
| 399 | count = len / 4; |
| 400 | if (count > txcount) |
| 401 | count = txcount; |
| 402 | |
| 403 | if (xqspi->txbuf) { |
Naga Sureshkumar Relli | ba3ce8c | 2019-04-22 12:56:49 +0530 | [diff] [blame] | 404 | iowrite32_rep(xqspi->regs + ZYNQ_QSPI_TXD_00_00_OFFSET, |
| 405 | xqspi->txbuf, count); |
Naga Sureshkumar Relli | 67dca5e | 2019-04-01 13:29:13 +0530 | [diff] [blame] | 406 | xqspi->txbuf += count * 4; |
| 407 | } else { |
| 408 | for (k = 0; k < count; k++) |
| 409 | writel_relaxed(0, xqspi->regs + |
| 410 | ZYNQ_QSPI_TXD_00_00_OFFSET); |
| 411 | } |
| 412 | |
| 413 | xqspi->tx_bytes -= count * 4; |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * zynq_qspi_read_op - Drains the RX FIFO by as many bytes as possible |
| 418 | * @xqspi: Pointer to the zynq_qspi structure |
| 419 | * @rxcount: Maximum number of words to read |
| 420 | */ |
| 421 | static void zynq_qspi_read_op(struct zynq_qspi *xqspi, int rxcount) |
| 422 | { |
| 423 | int count, len, k; |
| 424 | |
| 425 | len = xqspi->rx_bytes - xqspi->tx_bytes; |
| 426 | count = len / 4; |
| 427 | if (count > rxcount) |
| 428 | count = rxcount; |
| 429 | if (xqspi->rxbuf) { |
Naga Sureshkumar Relli | ba3ce8c | 2019-04-22 12:56:49 +0530 | [diff] [blame] | 430 | ioread32_rep(xqspi->regs + ZYNQ_QSPI_RXD_OFFSET, |
| 431 | xqspi->rxbuf, count); |
Naga Sureshkumar Relli | 67dca5e | 2019-04-01 13:29:13 +0530 | [diff] [blame] | 432 | xqspi->rxbuf += count * 4; |
| 433 | } else { |
| 434 | for (k = 0; k < count; k++) |
| 435 | readl_relaxed(xqspi->regs + ZYNQ_QSPI_RXD_OFFSET); |
| 436 | } |
| 437 | xqspi->rx_bytes -= count * 4; |
| 438 | len -= count * 4; |
| 439 | |
| 440 | if (len && len < 4 && count < rxcount) |
| 441 | zynq_qspi_rxfifo_op(xqspi, len); |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * zynq_qspi_irq - Interrupt service routine of the QSPI controller |
| 446 | * @irq: IRQ number |
| 447 | * @dev_id: Pointer to the xqspi structure |
| 448 | * |
| 449 | * This function handles TX empty only. |
| 450 | * On TX empty interrupt this function reads the received data from RX FIFO and |
| 451 | * fills the TX FIFO if there is any data remaining to be transferred. |
| 452 | * |
| 453 | * Return: IRQ_HANDLED when interrupt is handled; IRQ_NONE otherwise. |
| 454 | */ |
| 455 | static irqreturn_t zynq_qspi_irq(int irq, void *dev_id) |
| 456 | { |
| 457 | u32 intr_status; |
| 458 | bool txempty; |
| 459 | struct zynq_qspi *xqspi = (struct zynq_qspi *)dev_id; |
| 460 | |
| 461 | intr_status = zynq_qspi_read(xqspi, ZYNQ_QSPI_STATUS_OFFSET); |
| 462 | zynq_qspi_write(xqspi, ZYNQ_QSPI_STATUS_OFFSET, intr_status); |
| 463 | |
| 464 | if ((intr_status & ZYNQ_QSPI_IXR_TXNFULL_MASK) || |
| 465 | (intr_status & ZYNQ_QSPI_IXR_RXNEMTY_MASK)) { |
| 466 | /* |
| 467 | * This bit is set when Tx FIFO has < THRESHOLD entries. |
| 468 | * We have the THRESHOLD value set to 1, |
| 469 | * so this bit indicates Tx FIFO is empty. |
| 470 | */ |
| 471 | txempty = !!(intr_status & ZYNQ_QSPI_IXR_TXNFULL_MASK); |
| 472 | /* Read out the data from the RX FIFO */ |
| 473 | zynq_qspi_read_op(xqspi, ZYNQ_QSPI_RX_THRESHOLD); |
| 474 | if (xqspi->tx_bytes) { |
| 475 | /* There is more data to send */ |
| 476 | zynq_qspi_write_op(xqspi, ZYNQ_QSPI_RX_THRESHOLD, |
| 477 | txempty); |
| 478 | } else { |
| 479 | /* |
| 480 | * If transfer and receive is completed then only send |
| 481 | * complete signal. |
| 482 | */ |
| 483 | if (!xqspi->rx_bytes) { |
| 484 | zynq_qspi_write(xqspi, |
| 485 | ZYNQ_QSPI_IDIS_OFFSET, |
| 486 | ZYNQ_QSPI_IXR_RXTX_MASK); |
| 487 | complete(&xqspi->data_completion); |
| 488 | } |
| 489 | } |
| 490 | return IRQ_HANDLED; |
| 491 | } |
| 492 | |
| 493 | return IRQ_NONE; |
| 494 | } |
| 495 | |
| 496 | /** |
| 497 | * zynq_qspi_exec_mem_op() - Initiates the QSPI transfer |
| 498 | * @mem: the SPI memory |
| 499 | * @op: the memory operation to execute |
| 500 | * |
| 501 | * Executes a memory operation. |
| 502 | * |
| 503 | * This function first selects the chip and starts the memory operation. |
| 504 | * |
| 505 | * Return: 0 in case of success, a negative error code otherwise. |
| 506 | */ |
| 507 | static int zynq_qspi_exec_mem_op(struct spi_mem *mem, |
| 508 | const struct spi_mem_op *op) |
| 509 | { |
| 510 | struct zynq_qspi *xqspi = spi_controller_get_devdata(mem->spi->master); |
| 511 | int err = 0, i; |
| 512 | u8 *tmpbuf; |
| 513 | |
| 514 | dev_dbg(xqspi->dev, "cmd:%#x mode:%d.%d.%d.%d\n", |
| 515 | op->cmd.opcode, op->cmd.buswidth, op->addr.buswidth, |
| 516 | op->dummy.buswidth, op->data.buswidth); |
| 517 | |
| 518 | zynq_qspi_chipselect(mem->spi, true); |
| 519 | zynq_qspi_config_op(xqspi, mem->spi); |
| 520 | |
| 521 | if (op->cmd.opcode) { |
| 522 | reinit_completion(&xqspi->data_completion); |
| 523 | xqspi->txbuf = (u8 *)&op->cmd.opcode; |
| 524 | xqspi->rxbuf = NULL; |
| 525 | xqspi->tx_bytes = sizeof(op->cmd.opcode); |
| 526 | xqspi->rx_bytes = sizeof(op->cmd.opcode); |
| 527 | zynq_qspi_write_op(xqspi, ZYNQ_QSPI_FIFO_DEPTH, true); |
| 528 | zynq_qspi_write(xqspi, ZYNQ_QSPI_IEN_OFFSET, |
| 529 | ZYNQ_QSPI_IXR_RXTX_MASK); |
| 530 | if (!wait_for_completion_interruptible_timeout(&xqspi->data_completion, |
| 531 | msecs_to_jiffies(1000))) |
| 532 | err = -ETIMEDOUT; |
| 533 | } |
| 534 | |
| 535 | if (op->addr.nbytes) { |
| 536 | for (i = 0; i < op->addr.nbytes; i++) { |
| 537 | xqspi->txbuf[i] = op->addr.val >> |
| 538 | (8 * (op->addr.nbytes - i - 1)); |
| 539 | } |
| 540 | |
| 541 | reinit_completion(&xqspi->data_completion); |
| 542 | xqspi->rxbuf = NULL; |
| 543 | xqspi->tx_bytes = op->addr.nbytes; |
| 544 | xqspi->rx_bytes = op->addr.nbytes; |
| 545 | zynq_qspi_write_op(xqspi, ZYNQ_QSPI_FIFO_DEPTH, true); |
| 546 | zynq_qspi_write(xqspi, ZYNQ_QSPI_IEN_OFFSET, |
| 547 | ZYNQ_QSPI_IXR_RXTX_MASK); |
| 548 | if (!wait_for_completion_interruptible_timeout(&xqspi->data_completion, |
| 549 | msecs_to_jiffies(1000))) |
| 550 | err = -ETIMEDOUT; |
| 551 | } |
| 552 | |
| 553 | if (op->dummy.nbytes) { |
| 554 | tmpbuf = kzalloc(op->dummy.nbytes, GFP_KERNEL); |
| 555 | memset(tmpbuf, 0xff, op->dummy.nbytes); |
| 556 | reinit_completion(&xqspi->data_completion); |
| 557 | xqspi->txbuf = tmpbuf; |
| 558 | xqspi->rxbuf = NULL; |
| 559 | xqspi->tx_bytes = op->dummy.nbytes; |
| 560 | xqspi->rx_bytes = op->dummy.nbytes; |
| 561 | zynq_qspi_write_op(xqspi, ZYNQ_QSPI_FIFO_DEPTH, true); |
| 562 | zynq_qspi_write(xqspi, ZYNQ_QSPI_IEN_OFFSET, |
| 563 | ZYNQ_QSPI_IXR_RXTX_MASK); |
| 564 | if (!wait_for_completion_interruptible_timeout(&xqspi->data_completion, |
| 565 | msecs_to_jiffies(1000))) |
| 566 | err = -ETIMEDOUT; |
| 567 | |
| 568 | kfree(tmpbuf); |
| 569 | } |
| 570 | |
| 571 | if (op->data.nbytes) { |
| 572 | reinit_completion(&xqspi->data_completion); |
| 573 | if (op->data.dir == SPI_MEM_DATA_OUT) { |
| 574 | xqspi->txbuf = (u8 *)op->data.buf.out; |
| 575 | xqspi->tx_bytes = op->data.nbytes; |
| 576 | xqspi->rxbuf = NULL; |
| 577 | xqspi->rx_bytes = op->data.nbytes; |
| 578 | } else { |
| 579 | xqspi->txbuf = NULL; |
| 580 | xqspi->rxbuf = (u8 *)op->data.buf.in; |
| 581 | xqspi->rx_bytes = op->data.nbytes; |
| 582 | xqspi->tx_bytes = op->data.nbytes; |
| 583 | } |
| 584 | |
| 585 | zynq_qspi_write_op(xqspi, ZYNQ_QSPI_FIFO_DEPTH, true); |
| 586 | zynq_qspi_write(xqspi, ZYNQ_QSPI_IEN_OFFSET, |
| 587 | ZYNQ_QSPI_IXR_RXTX_MASK); |
| 588 | if (!wait_for_completion_interruptible_timeout(&xqspi->data_completion, |
| 589 | msecs_to_jiffies(1000))) |
| 590 | err = -ETIMEDOUT; |
| 591 | } |
| 592 | zynq_qspi_chipselect(mem->spi, false); |
| 593 | |
| 594 | return err; |
| 595 | } |
| 596 | |
| 597 | static const struct spi_controller_mem_ops zynq_qspi_mem_ops = { |
| 598 | .supports_op = zynq_qspi_supports_op, |
| 599 | .exec_op = zynq_qspi_exec_mem_op, |
| 600 | }; |
| 601 | |
| 602 | /** |
| 603 | * zynq_qspi_probe - Probe method for the QSPI driver |
| 604 | * @pdev: Pointer to the platform_device structure |
| 605 | * |
| 606 | * This function initializes the driver data structures and the hardware. |
| 607 | * |
| 608 | * Return: 0 on success and error value on failure |
| 609 | */ |
| 610 | static int zynq_qspi_probe(struct platform_device *pdev) |
| 611 | { |
| 612 | int ret = 0; |
| 613 | struct spi_controller *ctlr; |
| 614 | struct device *dev = &pdev->dev; |
| 615 | struct device_node *np = dev->of_node; |
| 616 | struct zynq_qspi *xqspi; |
Naga Sureshkumar Relli | 67dca5e | 2019-04-01 13:29:13 +0530 | [diff] [blame] | 617 | u32 num_cs; |
| 618 | |
| 619 | ctlr = spi_alloc_master(&pdev->dev, sizeof(*xqspi)); |
| 620 | if (!ctlr) |
| 621 | return -ENOMEM; |
| 622 | |
| 623 | xqspi = spi_controller_get_devdata(ctlr); |
| 624 | xqspi->dev = dev; |
| 625 | platform_set_drvdata(pdev, xqspi); |
YueHaibing | ae91a43 | 2019-09-04 21:59:17 +0800 | [diff] [blame] | 626 | xqspi->regs = devm_platform_ioremap_resource(pdev, 0); |
Naga Sureshkumar Relli | 67dca5e | 2019-04-01 13:29:13 +0530 | [diff] [blame] | 627 | if (IS_ERR(xqspi->regs)) { |
| 628 | ret = PTR_ERR(xqspi->regs); |
| 629 | goto remove_master; |
| 630 | } |
| 631 | |
| 632 | xqspi->pclk = devm_clk_get(&pdev->dev, "pclk"); |
| 633 | if (IS_ERR(xqspi->pclk)) { |
| 634 | dev_err(&pdev->dev, "pclk clock not found.\n"); |
| 635 | ret = PTR_ERR(xqspi->pclk); |
| 636 | goto remove_master; |
| 637 | } |
| 638 | |
| 639 | init_completion(&xqspi->data_completion); |
| 640 | |
| 641 | xqspi->refclk = devm_clk_get(&pdev->dev, "ref_clk"); |
| 642 | if (IS_ERR(xqspi->refclk)) { |
| 643 | dev_err(&pdev->dev, "ref_clk clock not found.\n"); |
| 644 | ret = PTR_ERR(xqspi->refclk); |
| 645 | goto remove_master; |
| 646 | } |
| 647 | |
| 648 | ret = clk_prepare_enable(xqspi->pclk); |
| 649 | if (ret) { |
| 650 | dev_err(&pdev->dev, "Unable to enable APB clock.\n"); |
| 651 | goto remove_master; |
| 652 | } |
| 653 | |
| 654 | ret = clk_prepare_enable(xqspi->refclk); |
| 655 | if (ret) { |
| 656 | dev_err(&pdev->dev, "Unable to enable device clock.\n"); |
| 657 | goto clk_dis_pclk; |
| 658 | } |
| 659 | |
Naga Sureshkumar Relli | 67dca5e | 2019-04-01 13:29:13 +0530 | [diff] [blame] | 660 | xqspi->irq = platform_get_irq(pdev, 0); |
| 661 | if (xqspi->irq <= 0) { |
| 662 | ret = -ENXIO; |
Naga Sureshkumar Relli | 67dca5e | 2019-04-01 13:29:13 +0530 | [diff] [blame] | 663 | goto remove_master; |
| 664 | } |
| 665 | ret = devm_request_irq(&pdev->dev, xqspi->irq, zynq_qspi_irq, |
| 666 | 0, pdev->name, xqspi); |
| 667 | if (ret != 0) { |
| 668 | ret = -ENXIO; |
| 669 | dev_err(&pdev->dev, "request_irq failed\n"); |
| 670 | goto remove_master; |
| 671 | } |
| 672 | |
| 673 | ret = of_property_read_u32(np, "num-cs", |
| 674 | &num_cs); |
Miquel Raynal | 087622d | 2019-11-08 15:07:38 +0100 | [diff] [blame] | 675 | if (ret < 0) { |
Naga Sureshkumar Relli | 67dca5e | 2019-04-01 13:29:13 +0530 | [diff] [blame] | 676 | ctlr->num_chipselect = ZYNQ_QSPI_DEFAULT_NUM_CS; |
Miquel Raynal | 087622d | 2019-11-08 15:07:38 +0100 | [diff] [blame] | 677 | } else if (num_cs > ZYNQ_QSPI_DEFAULT_NUM_CS) { |
| 678 | dev_err(&pdev->dev, "anything but CS0 is not yet supported\n"); |
| 679 | goto remove_master; |
| 680 | } else { |
Naga Sureshkumar Relli | 67dca5e | 2019-04-01 13:29:13 +0530 | [diff] [blame] | 681 | ctlr->num_chipselect = num_cs; |
Miquel Raynal | 087622d | 2019-11-08 15:07:38 +0100 | [diff] [blame] | 682 | } |
Naga Sureshkumar Relli | 67dca5e | 2019-04-01 13:29:13 +0530 | [diff] [blame] | 683 | |
| 684 | ctlr->mode_bits = SPI_RX_DUAL | SPI_RX_QUAD | |
| 685 | SPI_TX_DUAL | SPI_TX_QUAD; |
| 686 | ctlr->mem_ops = &zynq_qspi_mem_ops; |
| 687 | ctlr->setup = zynq_qspi_setup_op; |
| 688 | ctlr->max_speed_hz = clk_get_rate(xqspi->refclk) / 2; |
| 689 | ctlr->dev.of_node = np; |
Miquel Raynal | 8f16292 | 2019-11-08 15:07:43 +0100 | [diff] [blame^] | 690 | |
| 691 | /* QSPI controller initializations */ |
| 692 | zynq_qspi_init_hw(xqspi); |
| 693 | |
Axel Lin | 8eb2fd00f | 2019-08-18 17:51:13 +0800 | [diff] [blame] | 694 | ret = devm_spi_register_controller(&pdev->dev, ctlr); |
Naga Sureshkumar Relli | 67dca5e | 2019-04-01 13:29:13 +0530 | [diff] [blame] | 695 | if (ret) { |
| 696 | dev_err(&pdev->dev, "spi_register_master failed\n"); |
| 697 | goto clk_dis_all; |
| 698 | } |
| 699 | |
| 700 | return ret; |
| 701 | |
| 702 | clk_dis_all: |
| 703 | clk_disable_unprepare(xqspi->refclk); |
| 704 | clk_dis_pclk: |
| 705 | clk_disable_unprepare(xqspi->pclk); |
| 706 | remove_master: |
| 707 | spi_controller_put(ctlr); |
| 708 | |
| 709 | return ret; |
| 710 | } |
| 711 | |
| 712 | /** |
| 713 | * zynq_qspi_remove - Remove method for the QSPI driver |
| 714 | * @pdev: Pointer to the platform_device structure |
| 715 | * |
| 716 | * This function is called if a device is physically removed from the system or |
| 717 | * if the driver module is being unloaded. It frees all resources allocated to |
| 718 | * the device. |
| 719 | * |
| 720 | * Return: 0 on success and error value on failure |
| 721 | */ |
| 722 | static int zynq_qspi_remove(struct platform_device *pdev) |
| 723 | { |
| 724 | struct zynq_qspi *xqspi = platform_get_drvdata(pdev); |
| 725 | |
| 726 | zynq_qspi_write(xqspi, ZYNQ_QSPI_ENABLE_OFFSET, 0); |
| 727 | |
| 728 | clk_disable_unprepare(xqspi->refclk); |
| 729 | clk_disable_unprepare(xqspi->pclk); |
| 730 | |
| 731 | return 0; |
| 732 | } |
| 733 | |
| 734 | static const struct of_device_id zynq_qspi_of_match[] = { |
| 735 | { .compatible = "xlnx,zynq-qspi-1.0", }, |
| 736 | { /* end of table */ } |
| 737 | }; |
| 738 | |
| 739 | MODULE_DEVICE_TABLE(of, zynq_qspi_of_match); |
| 740 | |
| 741 | /* |
| 742 | * zynq_qspi_driver - This structure defines the QSPI platform driver |
| 743 | */ |
| 744 | static struct platform_driver zynq_qspi_driver = { |
| 745 | .probe = zynq_qspi_probe, |
| 746 | .remove = zynq_qspi_remove, |
| 747 | .driver = { |
| 748 | .name = "zynq-qspi", |
| 749 | .of_match_table = zynq_qspi_of_match, |
| 750 | }, |
| 751 | }; |
| 752 | |
| 753 | module_platform_driver(zynq_qspi_driver); |
| 754 | |
| 755 | MODULE_AUTHOR("Xilinx, Inc."); |
| 756 | MODULE_DESCRIPTION("Xilinx Zynq QSPI driver"); |
| 757 | MODULE_LICENSE("GPL"); |