linux/drivers/spi/spi-zynq-qspi.c
<<
>>
Prefs
   1/*
   2 * Xilinx Zynq Quad-SPI (QSPI) controller driver (master mode only)
   3 *
   4 * Copyright (C) 2009 - 2014 Xilinx, Inc.
   5 *
   6 * This program is free software; you can redistribute it and/or modify it under
   7 * the terms of the GNU General Public License version 2 as published by the
   8 * Free Software Foundation; either version 2 of the License, or (at your
   9 * option) any later version.
  10 */
  11
  12#include <linux/clk.h>
  13#include <linux/delay.h>
  14#include <linux/interrupt.h>
  15#include <linux/io.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/spi/spi.h>
  21#include <linux/workqueue.h>
  22
  23/* Name of this driver */
  24#define DRIVER_NAME                     "zynq-qspi"
  25
  26/* Register offset definitions */
  27#define ZYNQ_QSPI_CONFIG_OFFSET         0x00 /* Configuration  Register, RW */
  28#define ZYNQ_QSPI_STATUS_OFFSET         0x04 /* Interrupt Status Register, RO */
  29#define ZYNQ_QSPI_IEN_OFFSET            0x08 /* Interrupt Enable Register, WO */
  30#define ZYNQ_QSPI_IDIS_OFFSET           0x0C /* Interrupt Disable Reg, WO */
  31#define ZYNQ_QSPI_IMASK_OFFSET          0x10 /* Interrupt Enabled Mask Reg,RO */
  32#define ZYNQ_QSPI_ENABLE_OFFSET         0x14 /* Enable/Disable Register, RW */
  33#define ZYNQ_QSPI_DELAY_OFFSET          0x18 /* Delay Register, RW */
  34#define ZYNQ_QSPI_TXD_00_00_OFFSET      0x1C /* Transmit 4-byte inst, WO */
  35#define ZYNQ_QSPI_TXD_00_01_OFFSET      0x80 /* Transmit 1-byte inst, WO */
  36#define ZYNQ_QSPI_TXD_00_10_OFFSET      0x84 /* Transmit 2-byte inst, WO */
  37#define ZYNQ_QSPI_TXD_00_11_OFFSET      0x88 /* Transmit 3-byte inst, WO */
  38#define ZYNQ_QSPI_RXD_OFFSET            0x20 /* Data Receive Register, RO */
  39#define ZYNQ_QSPI_SIC_OFFSET            0x24 /* Slave Idle Count Register, RW */
  40#define ZYNQ_QSPI_TX_THRESH_OFFSET      0x28 /* TX FIFO Watermark Reg, RW */
  41#define ZYNQ_QSPI_RX_THRESH_OFFSET      0x2C /* RX FIFO Watermark Reg, RW */
  42#define ZYNQ_QSPI_GPIO_OFFSET           0x30 /* GPIO Register, RW */
  43#define ZYNQ_QSPI_LINEAR_CFG_OFFSET     0xA0 /* Linear Adapter Config Ref, RW */
  44#define ZYNQ_QSPI_MOD_ID_OFFSET         0xFC /* Module ID Register, RO */
  45
  46/*
  47 * QSPI Configuration Register bit Masks
  48 *
  49 * This register contains various control bits that effect the operation
  50 * of the QSPI controller
  51 */
  52#define ZYNQ_QSPI_CONFIG_IFMODE_MASK    0x80000000 /* Flash Memory Interface */
  53#define ZYNQ_QSPI_CONFIG_MANSRT_MASK    0x00010000 /* Manual TX Start */
  54#define ZYNQ_QSPI_CONFIG_MANSRTEN_MASK  0x00008000 /* Enable Manual TX Mode */
  55#define ZYNQ_QSPI_CONFIG_SSFORCE_MASK   0x00004000 /* Manual Chip Select */
  56#define ZYNQ_QSPI_CONFIG_BDRATE_MASK    0x00000038 /* Baud Rate Divisor Mask */
  57#define ZYNQ_QSPI_CONFIG_CPHA_MASK      0x00000004 /* Clock Phase Control */
  58#define ZYNQ_QSPI_CONFIG_CPOL_MASK      0x00000002 /* Clock Polarity Control */
  59#define ZYNQ_QSPI_CONFIG_SSCTRL_MASK    0x00003C00 /* Slave Select Mask */
  60#define ZYNQ_QSPI_CONFIG_FWIDTH_MASK    0x000000C0 /* FIFO width */
  61#define ZYNQ_QSPI_CONFIG_MSTREN_MASK    0x00000001 /* Master Mode */
  62
  63/*
  64 * QSPI Configuration Register - Baud rate and slave select
  65 *
  66 * These are the values used in the calculation of baud rate divisor and
  67 * setting the slave select.
  68 */
  69#define ZYNQ_QSPI_BAUD_DIV_MAX          7 /* Baud rate divisor maximum */
  70#define ZYNQ_QSPI_BAUD_DIV_SHIFT        3 /* Baud rate divisor shift in CR */
  71#define ZYNQ_QSPI_SS_SHIFT              10 /* Slave Select field shift in CR */
  72
  73/*
  74 * QSPI Interrupt Registers bit Masks
  75 *
  76 * All the four interrupt registers (Status/Mask/Enable/Disable) have the same
  77 * bit definitions.
  78 */
  79#define ZYNQ_QSPI_IXR_TXNFULL_MASK      0x00000004 /* QSPI TX FIFO Overflow */
  80#define ZYNQ_QSPI_IXR_TXFULL_MASK       0x00000008 /* QSPI TX FIFO is full */
  81#define ZYNQ_QSPI_IXR_RXNEMTY_MASK      0x00000010 /* QSPI RX FIFO Not Empty */
  82#define ZYNQ_QSPI_IXR_ALL_MASK          (ZYNQ_QSPI_IXR_TXNFULL_MASK | \
  83                                        ZYNQ_QSPI_IXR_RXNEMTY_MASK)
  84
  85/*
  86 * QSPI Enable Register bit Masks
  87 *
  88 * This register is used to enable or disable the QSPI controller
  89 */
  90#define ZYNQ_QSPI_ENABLE_ENABLE_MASK    0x00000001 /* QSPI Enable Bit Mask */
  91
  92/*
  93 * QSPI Linear Configuration Register
  94 *
  95 * It is named Linear Configuration but it controls other modes when not in
  96 * linear mode also.
  97 */
  98#define ZYNQ_QSPI_LCFG_TWO_MEM_MASK     0x40000000 /* LQSPI Two memories Mask */
  99#define ZYNQ_QSPI_LCFG_SEP_BUS_MASK     0x20000000 /* LQSPI Separate bus Mask */
 100#define ZYNQ_QSPI_LCFG_U_PAGE_MASK      0x10000000 /* LQSPI Upper Page Mask */
 101
 102#define ZYNQ_QSPI_LCFG_DUMMY_SHIFT      8
 103
 104#define ZYNQ_QSPI_FAST_READ_QOUT_CODE   0x6B /* read instruction code */
 105#define ZYNQ_QSPI_FIFO_DEPTH            63 /* FIFO depth in words */
 106#define ZYNQ_QSPI_RX_THRESHOLD          32 /* Rx FIFO threshold level */
 107#define ZYNQ_QSPI_TX_THRESHOLD          1 /* Tx FIFO threshold level */
 108
 109/*
 110 * The modebits configurable by the driver to make the SPI support different
 111 * data formats
 112 */
 113#define MODEBITS                        (SPI_CPOL | SPI_CPHA)
 114
 115/* Default number of chip selects */
 116#define ZYNQ_QSPI_DEFAULT_NUM_CS        1
 117
 118/**
 119 * struct zynq_qspi - Defines qspi driver instance
 120 * @regs:               Virtual address of the QSPI controller registers
 121 * @refclk:             Pointer to the peripheral clock
 122 * @pclk:               Pointer to the APB clock
 123 * @irq:                IRQ number
 124 * @txbuf:              Pointer to the TX buffer
 125 * @rxbuf:              Pointer to the RX buffer
 126 * @bytes_to_transfer:  Number of bytes left to transfer
 127 * @bytes_to_receive:   Number of bytes left to receive
 128 * @is_dual:            Flag to indicate whether dual flash memories are used
 129 * @is_instr:           Flag to indicate if transfer contains an instruction
 130 *                      (Used in dual parallel configuration)
 131 */
 132struct zynq_qspi {
 133        void __iomem *regs;
 134        struct clk *refclk;
 135        struct clk *pclk;
 136        int irq;
 137        const void *txbuf;
 138        void *rxbuf;
 139        int bytes_to_transfer;
 140        int bytes_to_receive;
 141        u32 is_dual;
 142        u8 is_instr;
 143};
 144
 145/*
 146 * Inline functions for the QSPI controller read/write
 147 */
 148static inline u32 zynq_qspi_read(struct zynq_qspi *xqspi, u32 offset)
 149{
 150        return readl_relaxed(xqspi->regs + offset);
 151}
 152
 153static 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 thresold 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 */
 179static 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, 0x7F);
 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, 0x7F);
 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        if (xqspi->is_dual)
 215                /* Enable two memories on separate buses */
 216                zynq_qspi_write(xqspi, ZYNQ_QSPI_LINEAR_CFG_OFFSET,
 217                                (ZYNQ_QSPI_LCFG_TWO_MEM_MASK |
 218                                ZYNQ_QSPI_LCFG_SEP_BUS_MASK |
 219                                (1 << ZYNQ_QSPI_LCFG_DUMMY_SHIFT) |
 220                                ZYNQ_QSPI_FAST_READ_QOUT_CODE));
 221#ifdef CONFIG_SPI_ZYNQ_QSPI_DUAL_STACKED
 222        /* Enable two memories on shared bus */
 223        zynq_qspi_write(xqspi, ZYNQ_QSPI_LINEAR_CFG_OFFSET,
 224                        (ZYNQ_QSPI_LCFG_TWO_MEM_MASK |
 225                        (1 << ZYNQ_QSPI_LCFG_DUMMY_SHIFT) |
 226                        ZYNQ_QSPI_FAST_READ_QOUT_CODE));
 227#endif
 228        zynq_qspi_write(xqspi, ZYNQ_QSPI_ENABLE_OFFSET,
 229                        ZYNQ_QSPI_ENABLE_ENABLE_MASK);
 230}
 231
 232/**
 233 * zynq_qspi_read_rx_fifo - Read 1..4 bytes from RxFIFO to RX buffer
 234 * @xqspi:      Pointer to the zynq_qspi structure
 235 * @size:       Number of bytes to be read (1..4)
 236 *
 237 * Note: In case of dual parallel connection, even number of bytes are read
 238 * when odd bytes are requested to avoid transfer of a nibble to each flash.
 239 * The receive buffer though, is populated with the number of bytes requested.
 240 */
 241static void zynq_qspi_read_rx_fifo(struct zynq_qspi *xqspi, unsigned int size)
 242{
 243        unsigned int xsize;
 244        u32 data;
 245
 246        data = zynq_qspi_read(xqspi, ZYNQ_QSPI_RXD_OFFSET);
 247
 248        if (xqspi->rxbuf) {
 249                xsize = size;
 250                if (xqspi->is_dual && !xqspi->is_instr && (size % 2))
 251                        xsize++;
 252                memcpy(xqspi->rxbuf, ((u8 *)&data) + 4 - xsize, size);
 253                xqspi->rxbuf += size;
 254        }
 255
 256        xqspi->bytes_to_receive -= size;
 257        if (xqspi->bytes_to_receive < 0)
 258                xqspi->bytes_to_receive = 0;
 259}
 260
 261/**
 262 * zynq_qspi_write_tx_fifo - Write 1..4 bytes from TX buffer to TxFIFO
 263 * @xqspi:      Pointer to the zynq_qspi structure
 264 * @size:       Number of bytes to be written (1..4)
 265 *
 266 * In dual parallel configuration, when read/write data operations
 267 * are performed, odd data bytes have to be converted to even to
 268 * avoid a nibble (of data when programming / dummy when reading)
 269 * going to individual flash devices, where a byte is expected.
 270 * This check is only for data and will not apply for commands.
 271 */
 272static void zynq_qspi_write_tx_fifo(struct zynq_qspi *xqspi, unsigned int size)
 273{
 274        static const unsigned int offset[4] = {
 275                ZYNQ_QSPI_TXD_00_01_OFFSET, ZYNQ_QSPI_TXD_00_10_OFFSET,
 276                ZYNQ_QSPI_TXD_00_11_OFFSET, ZYNQ_QSPI_TXD_00_00_OFFSET };
 277        unsigned int xsize;
 278        u32 data;
 279
 280        if (xqspi->txbuf) {
 281                data = 0xffffffff;
 282                memcpy(&data, xqspi->txbuf, size);
 283                xqspi->txbuf += size;
 284        } else {
 285                data = 0;
 286        }
 287
 288        xqspi->bytes_to_transfer -= size;
 289
 290        xsize = size;
 291        if (xqspi->is_dual && !xqspi->is_instr && (size % 2))
 292                xsize++;
 293        zynq_qspi_write(xqspi, offset[xsize - 1], data);
 294}
 295
 296/**
 297 * zynq_prepare_transfer_hardware - Prepares hardware for transfer.
 298 * @master:     Pointer to the spi_master structure which provides
 299 *              information about the controller.
 300 *
 301 * This function enables SPI master controller.
 302 *
 303 * Return:      Always 0
 304 */
 305static int zynq_prepare_transfer_hardware(struct spi_master *master)
 306{
 307        struct zynq_qspi *xqspi = spi_master_get_devdata(master);
 308
 309        clk_enable(xqspi->refclk);
 310        clk_enable(xqspi->pclk);
 311        zynq_qspi_write(xqspi, ZYNQ_QSPI_ENABLE_OFFSET,
 312                        ZYNQ_QSPI_ENABLE_ENABLE_MASK);
 313
 314        return 0;
 315}
 316
 317/**
 318 * zynq_unprepare_transfer_hardware - Relaxes hardware after transfer
 319 * @master:     Pointer to the spi_master structure which provides
 320 *              information about the controller.
 321 *
 322 * This function disables the SPI master controller.
 323 *
 324 * Return:      Always 0
 325 */
 326static int zynq_unprepare_transfer_hardware(struct spi_master *master)
 327{
 328        struct zynq_qspi *xqspi = spi_master_get_devdata(master);
 329
 330        zynq_qspi_write(xqspi, ZYNQ_QSPI_ENABLE_OFFSET, 0);
 331        clk_disable(xqspi->refclk);
 332        clk_disable(xqspi->pclk);
 333
 334        return 0;
 335}
 336
 337/**
 338 * zynq_qspi_chipselect - Select or deselect the chip select line
 339 * @qspi:       Pointer to the spi_device structure
 340 * @is_high:    Select(0) or deselect (1) the chip select line
 341 */
 342static void zynq_qspi_chipselect(struct spi_device *qspi, bool is_high)
 343{
 344        struct zynq_qspi *xqspi = spi_master_get_devdata(qspi->master);
 345        u32 config_reg;
 346#ifdef CONFIG_SPI_ZYNQ_QSPI_DUAL_STACKED
 347        u32 lqspi_cfg_reg;
 348#endif
 349
 350        config_reg = zynq_qspi_read(xqspi, ZYNQ_QSPI_CONFIG_OFFSET);
 351
 352        /* Select upper/lower page before asserting CS */
 353#ifdef CONFIG_SPI_ZYNQ_QSPI_DUAL_STACKED
 354                lqspi_cfg_reg = zynq_qspi_read(xqspi,
 355                                               ZYNQ_QSPI_LINEAR_CFG_OFFSET);
 356                if (qspi->master->flags & SPI_MASTER_U_PAGE)
 357                        lqspi_cfg_reg |= ZYNQ_QSPI_LCFG_U_PAGE_MASK;
 358                else
 359                        lqspi_cfg_reg &= ~ZYNQ_QSPI_LCFG_U_PAGE_MASK;
 360                zynq_qspi_write(xqspi, ZYNQ_QSPI_LINEAR_CFG_OFFSET,
 361                                lqspi_cfg_reg);
 362#endif
 363
 364        if (is_high) {
 365                /* Deselect the slave */
 366                config_reg |= ZYNQ_QSPI_CONFIG_SSCTRL_MASK;
 367        } else {
 368                /* Select the slave */
 369                config_reg &= ~ZYNQ_QSPI_CONFIG_SSCTRL_MASK;
 370                config_reg |= (((~(BIT(qspi->chip_select))) <<
 371                                 ZYNQ_QSPI_SS_SHIFT) &
 372                                 ZYNQ_QSPI_CONFIG_SSCTRL_MASK);
 373                xqspi->is_instr = 1;
 374        }
 375
 376        zynq_qspi_write(xqspi, ZYNQ_QSPI_CONFIG_OFFSET, config_reg);
 377}
 378
 379/**
 380 * zynq_qspi_setup_transfer - Configure QSPI controller for specified transfer
 381 * @qspi:       Pointer to the spi_device structure
 382 * @transfer:   Pointer to the spi_transfer structure which provides information
 383 *              about next transfer setup parameters
 384 *
 385 * Sets the operational mode of QSPI controller for the next QSPI transfer and
 386 * sets the requested clock frequency.
 387 *
 388 * Return:      0 on success and -EINVAL on invalid input parameter
 389 *
 390 * Note: If the requested frequency is not an exact match with what can be
 391 * obtained using the prescalar value, the driver sets the clock frequency which
 392 * is lower than the requested frequency (maximum lower) for the transfer. If
 393 * the requested frequency is higher or lower than that is supported by the QSPI
 394 * controller the driver will set the highest or lowest frequency supported by
 395 * controller.
 396 */
 397static int zynq_qspi_setup_transfer(struct spi_device *qspi,
 398                                    struct spi_transfer *transfer)
 399{
 400        struct zynq_qspi *xqspi = spi_master_get_devdata(qspi->master);
 401        u32 config_reg, req_hz, baud_rate_val = 0;
 402
 403        if (transfer)
 404                req_hz = transfer->speed_hz;
 405        else
 406                req_hz = qspi->max_speed_hz;
 407
 408        /* Set the clock frequency */
 409        /* If req_hz == 0, default to lowest speed */
 410        while ((baud_rate_val < ZYNQ_QSPI_BAUD_DIV_MAX)  &&
 411               (clk_get_rate(xqspi->refclk) / (2 << baud_rate_val)) > req_hz)
 412                baud_rate_val++;
 413
 414        config_reg = zynq_qspi_read(xqspi, ZYNQ_QSPI_CONFIG_OFFSET);
 415
 416        /* Set the QSPI clock phase and clock polarity */
 417        config_reg &= (~ZYNQ_QSPI_CONFIG_CPHA_MASK) &
 418                      (~ZYNQ_QSPI_CONFIG_CPOL_MASK);
 419        if (qspi->mode & SPI_CPHA)
 420                config_reg |= ZYNQ_QSPI_CONFIG_CPHA_MASK;
 421        if (qspi->mode & SPI_CPOL)
 422                config_reg |= ZYNQ_QSPI_CONFIG_CPOL_MASK;
 423
 424        config_reg &= ~ZYNQ_QSPI_CONFIG_BDRATE_MASK;
 425        config_reg |= (baud_rate_val << ZYNQ_QSPI_BAUD_DIV_SHIFT);
 426
 427        zynq_qspi_write(xqspi, ZYNQ_QSPI_CONFIG_OFFSET, config_reg);
 428
 429        return 0;
 430}
 431
 432/**
 433 * zynq_qspi_setup - Configure the QSPI controller
 434 * @qspi:       Pointer to the spi_device structure
 435 *
 436 * Sets the operational mode of QSPI controller for the next QSPI transfer, baud
 437 * rate and divisor value to setup the requested qspi clock.
 438 *
 439 * Return:      0 on success and error value on failure
 440 */
 441static int zynq_qspi_setup(struct spi_device *qspi)
 442{
 443        if (qspi->master->busy)
 444                return -EBUSY;
 445
 446        return zynq_qspi_setup_transfer(qspi, NULL);
 447}
 448
 449/**
 450 * zynq_qspi_fill_tx_fifo - Fills the TX FIFO with as many bytes as possible
 451 * @xqspi:      Pointer to the zynq_qspi structure
 452 * @txcount:    Maximum number of words to write
 453 * @txempty:    Indicates that TxFIFO is empty
 454 */
 455static void zynq_qspi_fill_tx_fifo(struct zynq_qspi *xqspi, int txcount,
 456                                   bool txempty)
 457{
 458        int count, len, k;
 459
 460        len = xqspi->bytes_to_transfer;
 461        if (len && len < 4) {
 462                /*
 463                 * We must empty the TxFIFO between accesses to TXD0,
 464                 * TXD1, TXD2, TXD3.
 465                 */
 466                if (txempty)
 467                        zynq_qspi_write_tx_fifo(xqspi, len);
 468                return;
 469        }
 470
 471        count = len / 4;
 472        if (count > txcount)
 473                count = txcount;
 474
 475        if (xqspi->txbuf) {
 476                writesl(xqspi->regs + ZYNQ_QSPI_TXD_00_00_OFFSET,
 477                        xqspi->txbuf, count);
 478                xqspi->txbuf += count * 4;
 479        } else {
 480                for (k = 0; k < count; k++)
 481                        writel_relaxed(0, xqspi->regs +
 482                                          ZYNQ_QSPI_TXD_00_00_OFFSET);
 483        }
 484        xqspi->bytes_to_transfer -= count * 4;
 485}
 486
 487/**
 488 * zynq_qspi_drain_rx_fifo - Drains the RX FIFO by as many bytes as possible
 489 * @xqspi:      Pointer to the zynq_qspi structure
 490 * @rxcount:    Maximum number of words to read
 491 */
 492static void zynq_qspi_drain_rx_fifo(struct zynq_qspi *xqspi, int rxcount)
 493{
 494        int count, len, k;
 495
 496        len = xqspi->bytes_to_receive - xqspi->bytes_to_transfer;
 497        count = len / 4;
 498        if (count > rxcount)
 499                count = rxcount;
 500
 501        if (xqspi->rxbuf) {
 502                readsl(xqspi->regs + ZYNQ_QSPI_RXD_OFFSET,
 503                       xqspi->rxbuf, count);
 504                xqspi->rxbuf += count * 4;
 505        } else {
 506                for (k = 0; k < count; k++)
 507                        readl_relaxed(xqspi->regs + ZYNQ_QSPI_RXD_OFFSET);
 508        }
 509        xqspi->bytes_to_receive -= count * 4;
 510        len -= count * 4;
 511
 512        if (len && len < 4 && count < rxcount)
 513                zynq_qspi_read_rx_fifo(xqspi, len);
 514}
 515
 516/**
 517 * zynq_qspi_irq - Interrupt service routine of the QSPI controller
 518 * @irq:        IRQ number
 519 * @dev_id:     Pointer to the xqspi structure
 520 *
 521 * This function handles TX empty only.
 522 * On TX empty interrupt this function reads the received data from RX FIFO and
 523 * fills the TX FIFO if there is any data remaining to be transferred.
 524 *
 525 * Return:      IRQ_HANDLED when interrupt is handled; IRQ_NONE otherwise.
 526 */
 527static irqreturn_t zynq_qspi_irq(int irq, void *dev_id)
 528{
 529        struct spi_master *master = dev_id;
 530        struct zynq_qspi *xqspi = spi_master_get_devdata(master);
 531        u32 intr_status;
 532        bool txempty;
 533
 534        intr_status = zynq_qspi_read(xqspi, ZYNQ_QSPI_STATUS_OFFSET);
 535        zynq_qspi_write(xqspi, ZYNQ_QSPI_STATUS_OFFSET, intr_status);
 536
 537        if ((intr_status & ZYNQ_QSPI_IXR_TXNFULL_MASK) ||
 538            (intr_status & ZYNQ_QSPI_IXR_RXNEMTY_MASK)) {
 539                /*
 540                 * This bit is set when Tx FIFO has < THRESHOLD entries.
 541                 * We have the THRESHOLD value set to 1,
 542                 * so this bit indicates Tx FIFO is empty.
 543                 */
 544                txempty = !!(intr_status & ZYNQ_QSPI_IXR_TXNFULL_MASK);
 545
 546                /* Read out the data from the RX FIFO */
 547                zynq_qspi_drain_rx_fifo(xqspi, ZYNQ_QSPI_RX_THRESHOLD);
 548
 549                if (xqspi->bytes_to_transfer) {
 550                        /* There is more data to send */
 551                        zynq_qspi_fill_tx_fifo(xqspi, ZYNQ_QSPI_RX_THRESHOLD,
 552                                               txempty);
 553                } else {
 554                        /*
 555                         * If transfer and receive is completed then only send
 556                         * complete signal.
 557                         */
 558                        if (!xqspi->bytes_to_receive) {
 559                                zynq_qspi_write(xqspi,
 560                                                ZYNQ_QSPI_IDIS_OFFSET,
 561                                                ZYNQ_QSPI_IXR_ALL_MASK);
 562                                spi_finalize_current_transfer(master);
 563                                xqspi->is_instr = 0;
 564                        }
 565                }
 566                return IRQ_HANDLED;
 567        }
 568
 569        return IRQ_NONE;
 570}
 571
 572/**
 573 * zynq_qspi_start_transfer - Initiates the QSPI transfer
 574 * @master:     Pointer to the spi_master structure which provides
 575 *              information about the controller.
 576 * @qspi:       Pointer to the spi_device structure
 577 * @transfer:   Pointer to the spi_transfer structure which provide information
 578 *              about next transfer parameters
 579 *
 580 * This function fills the TX FIFO, starts the QSPI transfer, and waits for the
 581 * transfer to be completed.
 582 *
 583 * Return:      Number of bytes transferred in the last transfer
 584 */
 585static int zynq_qspi_start_transfer(struct spi_master *master,
 586                                    struct spi_device *qspi,
 587                                    struct spi_transfer *transfer)
 588{
 589        struct zynq_qspi *xqspi = spi_master_get_devdata(master);
 590
 591        xqspi->txbuf = transfer->tx_buf;
 592        xqspi->rxbuf = transfer->rx_buf;
 593        xqspi->bytes_to_transfer = transfer->len;
 594        xqspi->bytes_to_receive = transfer->len;
 595
 596        zynq_qspi_setup_transfer(qspi, transfer);
 597
 598        zynq_qspi_fill_tx_fifo(xqspi, ZYNQ_QSPI_FIFO_DEPTH, true);
 599
 600        zynq_qspi_write(xqspi, ZYNQ_QSPI_IEN_OFFSET,
 601                        ZYNQ_QSPI_IXR_ALL_MASK);
 602
 603        return transfer->len;
 604}
 605
 606/**
 607 * zynq_qspi_suspend - Suspend method for the QSPI driver
 608 * @_dev:       Address of the platform_device structure
 609 *
 610 * This function stops the QSPI driver queue and disables the QSPI controller
 611 *
 612 * Return:      Always 0
 613 */
 614static int __maybe_unused zynq_qspi_suspend(struct device *_dev)
 615{
 616        struct platform_device *pdev = container_of(_dev,
 617                        struct platform_device, dev);
 618        struct spi_master *master = platform_get_drvdata(pdev);
 619
 620        spi_master_suspend(master);
 621
 622        zynq_unprepare_transfer_hardware(master);
 623
 624        return 0;
 625}
 626
 627/**
 628 * zynq_qspi_resume - Resume method for the QSPI driver
 629 * @dev:        Address of the platform_device structure
 630 *
 631 * The function starts the QSPI driver queue and initializes the QSPI controller
 632 *
 633 * Return:      0 on success and error value on error
 634 */
 635static int __maybe_unused zynq_qspi_resume(struct device *dev)
 636{
 637        struct platform_device *pdev = container_of(dev,
 638                        struct platform_device, dev);
 639        struct spi_master *master = platform_get_drvdata(pdev);
 640        struct zynq_qspi *xqspi = spi_master_get_devdata(master);
 641        int ret = 0;
 642
 643        ret = clk_enable(xqspi->pclk);
 644        if (ret) {
 645                dev_err(dev, "Cannot enable APB clock.\n");
 646                return ret;
 647        }
 648
 649        ret = clk_enable(xqspi->refclk);
 650        if (ret) {
 651                dev_err(dev, "Cannot enable device clock.\n");
 652                clk_disable(xqspi->pclk);
 653                return ret;
 654        }
 655
 656        spi_master_resume(master);
 657
 658        return 0;
 659}
 660
 661static SIMPLE_DEV_PM_OPS(zynq_qspi_dev_pm_ops, zynq_qspi_suspend,
 662                         zynq_qspi_resume);
 663
 664/**
 665 * zynq_qspi_probe - Probe method for the QSPI driver
 666 * @pdev:       Pointer to the platform_device structure
 667 *
 668 * This function initializes the driver data structures and the hardware.
 669 *
 670 * Return:      0 on success and error value on failure
 671 */
 672static int zynq_qspi_probe(struct platform_device *pdev)
 673{
 674        int ret = 0;
 675        struct spi_master *master;
 676        struct zynq_qspi *xqspi;
 677        struct resource *res;
 678        u32 num_cs;
 679
 680        master = spi_alloc_master(&pdev->dev, sizeof(*xqspi));
 681        if (!master)
 682                return -ENOMEM;
 683
 684        xqspi = spi_master_get_devdata(master);
 685        master->dev.of_node = pdev->dev.of_node;
 686        platform_set_drvdata(pdev, master);
 687
 688        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 689        xqspi->regs = devm_ioremap_resource(&pdev->dev, res);
 690        if (IS_ERR(xqspi->regs)) {
 691                ret = PTR_ERR(xqspi->regs);
 692                goto remove_master;
 693        }
 694
 695        if (of_property_read_u32(pdev->dev.of_node, "is-dual",
 696                                 &xqspi->is_dual)) {
 697                dev_warn(&pdev->dev, "couldn't determine configuration info");
 698                dev_warn(&pdev->dev, "about dual memories. defaulting to single memory\n");
 699        }
 700
 701        xqspi->pclk = devm_clk_get(&pdev->dev, "pclk");
 702        if (IS_ERR(xqspi->pclk)) {
 703                dev_err(&pdev->dev, "pclk clock not found.\n");
 704                ret = PTR_ERR(xqspi->pclk);
 705                goto remove_master;
 706        }
 707
 708        xqspi->refclk = devm_clk_get(&pdev->dev, "ref_clk");
 709        if (IS_ERR(xqspi->refclk)) {
 710                dev_err(&pdev->dev, "ref_clk clock not found.\n");
 711                ret = PTR_ERR(xqspi->refclk);
 712                goto remove_master;
 713        }
 714
 715        ret = clk_prepare_enable(xqspi->pclk);
 716        if (ret) {
 717                dev_err(&pdev->dev, "Unable to enable APB clock.\n");
 718                goto remove_master;
 719        }
 720
 721        ret = clk_prepare_enable(xqspi->refclk);
 722        if (ret) {
 723                dev_err(&pdev->dev, "Unable to enable device clock.\n");
 724                goto clk_dis_pclk;
 725        }
 726
 727        /* QSPI controller initializations */
 728        zynq_qspi_init_hw(xqspi);
 729
 730        xqspi->irq = platform_get_irq(pdev, 0);
 731        if (xqspi->irq <= 0) {
 732                ret = -ENXIO;
 733                dev_err(&pdev->dev, "irq resource not found\n");
 734                goto remove_master;
 735        }
 736        ret = devm_request_irq(&pdev->dev, xqspi->irq, zynq_qspi_irq,
 737                               0, pdev->name, master);
 738        if (ret != 0) {
 739                ret = -ENXIO;
 740                dev_err(&pdev->dev, "request_irq failed\n");
 741                goto remove_master;
 742        }
 743
 744        ret = of_property_read_u32(pdev->dev.of_node, "num-cs",
 745                                   &num_cs);
 746        if (ret < 0)
 747                master->num_chipselect = ZYNQ_QSPI_DEFAULT_NUM_CS;
 748        else
 749                master->num_chipselect = num_cs;
 750
 751        master->setup = zynq_qspi_setup;
 752        master->set_cs = zynq_qspi_chipselect;
 753        master->transfer_one = zynq_qspi_start_transfer;
 754        master->prepare_transfer_hardware = zynq_prepare_transfer_hardware;
 755        master->unprepare_transfer_hardware = zynq_unprepare_transfer_hardware;
 756        master->flags = SPI_MASTER_QUAD_MODE;
 757
 758        master->max_speed_hz = clk_get_rate(xqspi->refclk) / 2;
 759        master->bits_per_word_mask = SPI_BPW_MASK(8);
 760        master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_RX_DUAL | SPI_RX_QUAD |
 761                            SPI_TX_DUAL | SPI_TX_QUAD;
 762
 763        ret = spi_register_master(master);
 764        if (ret) {
 765                dev_err(&pdev->dev, "spi_register_master failed\n");
 766                goto clk_dis_all;
 767        }
 768
 769        return ret;
 770
 771clk_dis_all:
 772        clk_disable_unprepare(xqspi->refclk);
 773clk_dis_pclk:
 774        clk_disable_unprepare(xqspi->pclk);
 775remove_master:
 776        spi_master_put(master);
 777        return ret;
 778}
 779
 780/**
 781 * zynq_qspi_remove - Remove method for the QSPI driver
 782 * @pdev:       Pointer to the platform_device structure
 783 *
 784 * This function is called if a device is physically removed from the system or
 785 * if the driver module is being unloaded. It frees all resources allocated to
 786 * the device.
 787 *
 788 * Return:      0 on success and error value on failure
 789 */
 790static int zynq_qspi_remove(struct platform_device *pdev)
 791{
 792        struct spi_master *master = platform_get_drvdata(pdev);
 793        struct zynq_qspi *xqspi = spi_master_get_devdata(master);
 794
 795        zynq_qspi_write(xqspi, ZYNQ_QSPI_ENABLE_OFFSET, 0);
 796
 797        clk_disable_unprepare(xqspi->refclk);
 798        clk_disable_unprepare(xqspi->pclk);
 799
 800        spi_unregister_master(master);
 801
 802        return 0;
 803}
 804
 805static const struct of_device_id zynq_qspi_of_match[] = {
 806        { .compatible = "xlnx,zynq-qspi-1.0", },
 807        { /* end of table */ }
 808};
 809MODULE_DEVICE_TABLE(of, zynq_qspi_of_match);
 810
 811/*
 812 * zynq_qspi_driver - This structure defines the QSPI platform driver
 813 */
 814static struct platform_driver zynq_qspi_driver = {
 815        .probe = zynq_qspi_probe,
 816        .remove = zynq_qspi_remove,
 817        .driver = {
 818                .name = DRIVER_NAME,
 819                .of_match_table = zynq_qspi_of_match,
 820                .pm = &zynq_qspi_dev_pm_ops,
 821        },
 822};
 823
 824module_platform_driver(zynq_qspi_driver);
 825
 826MODULE_AUTHOR("Xilinx, Inc.");
 827MODULE_DESCRIPTION("Xilinx Zynq QSPI driver");
 828MODULE_LICENSE("GPL");
 829