uboot/drivers/spi/zynq_qspi.c
<<
>>
Prefs
   1/*
   2 * (C) Copyright 2011 - 2013 Xilinx
   3 *
   4 * Xilinx Zynq Quad-SPI(QSPI) controller driver (master mode only)
   5 *
   6 * SPDX-License-Identifier:     GPL-2.0+
   7 */
   8
   9#include <common.h>
  10#include <malloc.h>
  11#include <dm.h>
  12#include <ubi_uboot.h>
  13#include <spi.h>
  14#include <spi_flash.h>
  15#include <asm/io.h>
  16#include <asm/arch/hardware.h>
  17#include <asm/arch/sys_proto.h>
  18#include <asm/arch/clk.h>
  19#include "../mtd/spi/sf_internal.h"
  20
  21DECLARE_GLOBAL_DATA_PTR;
  22
  23/* QSPI Transmit Data Register */
  24#define ZYNQ_QSPI_TXD_00_00_OFFSET      0x1C /* Transmit 4-byte inst, WO */
  25#define ZYNQ_QSPI_TXD_00_01_OFFSET      0x80 /* Transmit 1-byte inst, WO */
  26#define ZYNQ_QSPI_TXD_00_10_OFFSET      0x84 /* Transmit 2-byte inst, WO */
  27#define ZYNQ_QSPI_TXD_00_11_OFFSET      0x88 /* Transmit 3-byte inst, WO */
  28
  29/*
  30 * QSPI Configuration Register bit Masks
  31 *
  32 * This register contains various control bits that effect the operation
  33 * of the QSPI controller
  34 */
  35#define ZYNQ_QSPI_CONFIG_IFMODE_MASK    (1 << 31)  /* Flash intrface mode*/
  36#define ZYNQ_QSPI_CONFIG_MSA_MASK       (1 << 15)  /* Manual start enb */
  37#define ZYNQ_QSPI_CONFIG_MCS_MASK       (1 << 14)  /* Manual chip select */
  38#define ZYNQ_QSPI_CONFIG_PCS_MASK       (1 << 10)  /* Peri chip select */
  39#define ZYNQ_QSPI_CONFIG_FW_MASK        (0x3 << 6) /* FIFO width */
  40#define ZYNQ_QSPI_CONFIG_BAUD_DIV_MASK  (0x7 << 3) /* Baud rate div */
  41#define ZYNQ_QSPI_CONFIG_MSTREN_MASK    (1 << 0)   /* Mode select */
  42#define ZYNQ_QSPI_CONFIG_MANSRT_MASK    0x00010000 /* Manual TX Start */
  43#define ZYNQ_QSPI_CONFIG_CPHA_MASK      0x00000004 /* Clock Phase Control */
  44#define ZYNQ_QSPI_CONFIG_CPOL_MASK      0x00000002 /* Clock Polarity Control */
  45#define ZYNQ_QSPI_CONFIG_SSCTRL_MASK    0x00003C00 /* Slave Select Mask */
  46/*
  47 * QSPI Interrupt Registers bit Masks
  48 *
  49 * All the four interrupt registers (Status/Mask/Enable/Disable) have the same
  50 * bit definitions.
  51 */
  52#define ZYNQ_QSPI_IXR_TXNFULL_MASK      0x00000004 /* QSPI TX FIFO Overflow */
  53#define ZYNQ_QSPI_IXR_TXFULL_MASK       0x00000008 /* QSPI TX FIFO is full */
  54#define ZYNQ_QSPI_IXR_RXNEMTY_MASK      0x00000010 /* QSPI RX FIFO Not Empty */
  55#define ZYNQ_QSPI_IXR_ALL_MASK          (ZYNQ_QSPI_IXR_TXNFULL_MASK | \
  56                                        ZYNQ_QSPI_IXR_RXNEMTY_MASK)
  57
  58/*
  59 * QSPI Enable Register bit Masks
  60 *
  61 * This register is used to enable or disable the QSPI controller
  62 */
  63#define ZYNQ_QSPI_ENABLE_ENABLE_MASK    0x00000001 /* QSPI Enable Bit Mask */
  64
  65/*
  66 * QSPI Linear Configuration Register
  67 *
  68 * It is named Linear Configuration but it controls other modes when not in
  69 * linear mode also.
  70 */
  71#define ZYNQ_QSPI_LCFG_TWO_MEM_MASK     0x40000000 /* QSPI Enable Bit Mask */
  72#define ZYNQ_QSPI_LCFG_SEP_BUS_MASK     0x20000000 /* QSPI Enable Bit Mask */
  73#define ZYNQ_QSPI_LCFG_U_PAGE           0x10000000 /* QSPI Upper memory set */
  74
  75#define ZYNQ_QSPI_LCFG_DUMMY_SHIFT      8
  76
  77#define ZYNQ_QSPI_FR_QOUT_CODE  0x6B    /* read instruction code */
  78#define ZYNQ_QSPI_FR_DUALIO_CODE        0xBB
  79
  80/*
  81 * The modebits configurable by the driver to make the SPI support different
  82 * data formats
  83 */
  84#define MODEBITS                        (SPI_CPOL | SPI_CPHA)
  85
  86/* Definitions for the status of queue */
  87#define ZYNQ_QSPI_QUEUE_STOPPED         0
  88#define ZYNQ_QSPI_QUEUE_RUNNING         1
  89#define ZYNQ_QSPI_RXFIFO_THRESHOLD      32
  90#define ZYNQ_QSPI_FIFO_DEPTH            63
  91
  92/* QSPI MIO's count for different connection topologies */
  93#define ZYNQ_QSPI_MIO_NUM_QSPI0         6
  94#define ZYNQ_QSPI_MIO_NUM_QSPI1         5
  95#define ZYNQ_QSPI_MIO_NUM_QSPI1_CS      1
  96
  97/* QSPI MIO's count for different connection topologies */
  98#define ZYNQ_QSPI_MIO_NUM_QSPI0_DIO     4
  99#define ZYNQ_QSPI_MIO_NUM_QSPI1_DIO     3
 100#define ZYNQ_QSPI_MIO_NUM_QSPI1_CS_DIO  1
 101
 102#define ZYNQ_QSPI_MAX_BAUD_RATE         0x7
 103#define ZYNQ_QSPI_DEFAULT_BAUD_RATE     0x2
 104
 105/* QSPI register offsets */
 106struct zynq_qspi_regs {
 107        u32 confr;      /* 0x00 */
 108        u32 isr;        /* 0x04 */
 109        u32 ier;        /* 0x08 */
 110        u32 idisr;      /* 0x0C */
 111        u32 imaskr;     /* 0x10 */
 112        u32 enbr;       /* 0x14 */
 113        u32 dr;         /* 0x18 */
 114        u32 txd0r;      /* 0x1C */
 115        u32 drxr;       /* 0x20 */
 116        u32 sicr;       /* 0x24 */
 117        u32 txftr;      /* 0x28 */
 118        u32 rxftr;      /* 0x2C */
 119        u32 gpior;      /* 0x30 */
 120        u32 reserved0[19];
 121        u32 txd1r;      /* 0x80 */
 122        u32 txd2r;      /* 0x84 */
 123        u32 txd3r;      /* 0x88 */
 124        u32 reserved1[5];
 125        u32 lcr;        /* 0xA0 */
 126        u32 reserved2[22];
 127        u32 midr;       /* 0xFC */
 128};
 129
 130
 131struct zynq_qspi_platdata {
 132        struct zynq_qspi_regs *regs;
 133        u32 frequency;          /* input frequency */
 134        u32 speed_hz;
 135        u32 is_dual;
 136        u32 tx_rx_mode;
 137};
 138
 139struct zynq_qspi_priv {
 140        struct zynq_qspi_regs *regs;
 141        u8 mode;
 142        u32 freq;
 143        const void *txbuf;
 144        void *rxbuf;
 145        unsigned len;
 146        int bytes_to_transfer;
 147        int bytes_to_receive;
 148        unsigned int is_inst;
 149        unsigned int is_dual;
 150        unsigned int is_dio;
 151        unsigned int u_page;
 152        unsigned cs_change:1;
 153};
 154
 155static int zynq_qspi_ofdata_to_platdata(struct udevice *bus)
 156{
 157        struct zynq_qspi_platdata *plat = bus->platdata;
 158        int is_dual;
 159        u32 mode = 0;
 160        int offset;
 161        u32 value;
 162
 163        debug("%s\n", __func__);
 164        plat->regs = (struct zynq_qspi_regs *)ZYNQ_QSPI_BASEADDR;
 165
 166        is_dual = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus), "is-dual", -1);
 167        if (is_dual < 0)
 168                plat->is_dual = SF_SINGLE_FLASH;
 169        else if (is_dual == 1)
 170                plat->is_dual = SF_DUAL_PARALLEL_FLASH;
 171        else
 172                if (fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus),
 173                                   "is-stacked", -1) < 0)
 174                        plat->is_dual = SF_SINGLE_FLASH;
 175                else
 176                        plat->is_dual = SF_DUAL_STACKED_FLASH;
 177
 178        offset = fdt_first_subnode(gd->fdt_blob, dev_of_offset(bus));
 179
 180        value = fdtdec_get_uint(gd->fdt_blob, offset, "spi-rx-bus-width", 1);
 181        switch (value) {
 182        case 1:
 183                break;
 184        case 2:
 185                mode |= SPI_RX_DUAL;
 186                break;
 187        case 4:
 188                mode |= SPI_RX_QUAD;
 189                break;
 190        default:
 191                printf("Invalid spi-rx-bus-width %d\n", value);
 192                break;
 193        }
 194
 195        value = fdtdec_get_uint(gd->fdt_blob, offset, "spi-tx-bus-width", 1);
 196        switch (value) {
 197        case 1:
 198                break;
 199        case 2:
 200                mode |= SPI_TX_DUAL;
 201                break;
 202        case 4:
 203                mode |= SPI_TX_QUAD;
 204                break;
 205        default:
 206                printf("Invalid spi-tx-bus-width %d\n", value);
 207                break;
 208        }
 209
 210        plat->tx_rx_mode = mode;
 211
 212        plat->frequency = 166666666;
 213        plat->speed_hz = plat->frequency / 2;
 214
 215        return 0;
 216}
 217
 218/*
 219 * zynq_qspi_init_hw - Initialize the hardware
 220 * @is_dual:            Indicates whether dual memories are used
 221 * @cs:                 Indicates which chip select is used in dual stacked
 222 *
 223 * The default settings of the QSPI controller's configurable parameters on
 224 * reset are
 225 *      - Master mode
 226 *      - Baud rate divisor is set to 2
 227 *      - Threshold value for TX FIFO not full interrupt is set to 1
 228 *      - Flash memory interface mode enabled
 229 *      - Size of the word to be transferred as 8 bit
 230 * This function performs the following actions
 231 *      - Disable and clear all the interrupts
 232 *      - Enable manual slave select
 233 *      - Enable auto start
 234 *      - Deselect all the chip select lines
 235 *      - Set the size of the word to be transferred as 32 bit
 236 *      - Set the little endian mode of TX FIFO and
 237 *      - Enable the QSPI controller
 238 */
 239static void zynq_qspi_init_hw(struct zynq_qspi_priv *priv)
 240{
 241        u32 config_reg;
 242        struct zynq_qspi_regs *regs = priv->regs;
 243
 244        writel(~ZYNQ_QSPI_ENABLE_ENABLE_MASK, &regs->enbr);
 245        writel(0x7F, &regs->idisr);
 246
 247        /* Disable linear mode as the boot loader may have used it */
 248        writel(0x0, &regs->lcr);
 249
 250        /* Clear the TX and RX threshold reg */
 251        writel(0x1, &regs->txftr);
 252        writel(ZYNQ_QSPI_RXFIFO_THRESHOLD, &regs->rxftr);
 253
 254        /* Clear the RX FIFO */
 255        while (readl(&regs->isr) & ZYNQ_QSPI_IXR_RXNEMTY_MASK)
 256                readl(&regs->drxr);
 257
 258        debug("%s is_dual:0x%x, is_dio:0x%x\n", __func__, priv->is_dual, priv->is_dio);
 259
 260        writel(0x7F, &regs->isr);
 261        config_reg = readl(&regs->confr);
 262        config_reg &= ~ZYNQ_QSPI_CONFIG_MSA_MASK;
 263        config_reg |= ZYNQ_QSPI_CONFIG_IFMODE_MASK |
 264                ZYNQ_QSPI_CONFIG_MCS_MASK | ZYNQ_QSPI_CONFIG_PCS_MASK |
 265                ZYNQ_QSPI_CONFIG_FW_MASK | ZYNQ_QSPI_CONFIG_MSTREN_MASK;
 266        if (priv->is_dual == SF_DUAL_STACKED_FLASH)
 267                config_reg |= 0x10;
 268        writel(config_reg, &regs->confr);
 269
 270        if (priv->is_dual == SF_DUAL_PARALLEL_FLASH) {
 271                if (priv->is_dio == SF_DUALIO_FLASH)
 272                        /* Enable two memories on seperate buses */
 273                        writel((ZYNQ_QSPI_LCFG_TWO_MEM_MASK |
 274                                ZYNQ_QSPI_LCFG_SEP_BUS_MASK |
 275                                (1 << ZYNQ_QSPI_LCFG_DUMMY_SHIFT) |
 276                                ZYNQ_QSPI_FR_DUALIO_CODE),
 277                                &regs->lcr);
 278                else
 279                        /* Enable two memories on seperate buses */
 280                        writel((ZYNQ_QSPI_LCFG_TWO_MEM_MASK |
 281                                ZYNQ_QSPI_LCFG_SEP_BUS_MASK |
 282                                (1 << ZYNQ_QSPI_LCFG_DUMMY_SHIFT) |
 283                                ZYNQ_QSPI_FR_QOUT_CODE),
 284                                &regs->lcr);
 285        } else if (priv->is_dual == SF_DUAL_STACKED_FLASH) {
 286                if (priv->is_dio == SF_DUALIO_FLASH)
 287                        /* Configure two memories on shared bus
 288                         * by enabling lower mem */
 289                        writel((ZYNQ_QSPI_LCFG_TWO_MEM_MASK |
 290                                (1 << ZYNQ_QSPI_LCFG_DUMMY_SHIFT) |
 291                                ZYNQ_QSPI_FR_DUALIO_CODE),
 292                                &regs->lcr);
 293                else
 294                        /* Configure two memories on shared bus
 295                         * by enabling lower mem */
 296                        writel((ZYNQ_QSPI_LCFG_TWO_MEM_MASK |
 297                                (1 << ZYNQ_QSPI_LCFG_DUMMY_SHIFT) |
 298                                ZYNQ_QSPI_FR_QOUT_CODE),
 299                                &regs->lcr);
 300        }
 301        writel(ZYNQ_QSPI_ENABLE_ENABLE_MASK, &regs->enbr);
 302}
 303
 304static int zynq_qspi_child_pre_probe(struct udevice *bus)
 305{
 306        struct spi_slave *slave = dev_get_parent_priv(bus);
 307        struct zynq_qspi_priv *priv = dev_get_priv(bus->parent);
 308        struct zynq_qspi_platdata *plat = dev_get_platdata(bus->parent);
 309
 310        slave->option = priv->is_dual;
 311        slave->dio = priv->is_dio;
 312        slave->mode = plat->tx_rx_mode;
 313
 314        return 0;
 315}
 316
 317static int zynq_qspi_probe(struct udevice *bus)
 318{
 319        struct zynq_qspi_platdata *plat = dev_get_platdata(bus);
 320        struct zynq_qspi_priv *priv = dev_get_priv(bus);
 321
 322        debug("zynq_qspi_probe:  bus:%p, priv:%p \n", bus, priv);
 323
 324        priv->regs = plat->regs;
 325        priv->is_dual = plat->is_dual;
 326
 327        if (priv->is_dual == -1) {
 328                debug("%s: No QSPI device detected based on MIO settings\n",
 329                      __func__);
 330                return -1;
 331        }
 332
 333        /* init the zynq spi hw */
 334        zynq_qspi_init_hw(priv);
 335
 336        return 0;
 337}
 338
 339static int zynq_qspi_set_speed(struct udevice *bus, uint speed)
 340{
 341        struct zynq_qspi_platdata *plat = bus->platdata;
 342        struct zynq_qspi_priv *priv = dev_get_priv(bus);
 343        struct zynq_qspi_regs *regs = priv->regs;
 344        uint32_t confr;
 345        u8 baud_rate_val = 0;
 346
 347        debug("%s\n", __func__);
 348        if (speed > plat->frequency)
 349                speed = plat->frequency;
 350
 351        /* Set the clock frequency */
 352        confr = readl(&regs->confr);
 353        if (speed == 0) {
 354                /* Set baudrate x8, if the freq is 0 */
 355                baud_rate_val = 0x2;
 356        } else if (plat->speed_hz != speed) {
 357                while ((baud_rate_val < 8) &&
 358                       ((plat->frequency /
 359                       (2 << baud_rate_val)) > speed))
 360                        baud_rate_val++;
 361
 362                if (baud_rate_val > ZYNQ_QSPI_MAX_BAUD_RATE)
 363                        baud_rate_val = ZYNQ_QSPI_DEFAULT_BAUD_RATE;
 364
 365                plat->speed_hz = speed / (2 << baud_rate_val);
 366        }
 367        confr &= ~ZYNQ_QSPI_CONFIG_BAUD_DIV_MASK;
 368        confr |= (baud_rate_val << 3);
 369
 370        writel(confr, &regs->confr);
 371        priv->freq = speed;
 372
 373        debug("zynq_spi_set_speed: regs=%p, mode=%d\n", priv->regs, priv->freq);
 374
 375        return 0;
 376}
 377
 378static int zynq_qspi_set_mode(struct udevice *bus, uint mode)
 379{
 380        struct zynq_qspi_priv *priv = dev_get_priv(bus);
 381        struct zynq_qspi_regs *regs = priv->regs;
 382        uint32_t confr;
 383
 384        debug("%s\n", __func__);
 385        /* Set the SPI Clock phase and polarities */
 386        confr = readl(&regs->confr);
 387        confr &= ~(ZYNQ_QSPI_CONFIG_CPHA_MASK | ZYNQ_QSPI_CONFIG_CPOL_MASK);
 388
 389        if (priv->mode & SPI_CPHA)
 390                confr |= ZYNQ_QSPI_CONFIG_CPHA_MASK;
 391        if (priv->mode & SPI_CPOL)
 392                confr |= ZYNQ_QSPI_CONFIG_CPOL_MASK;
 393
 394        writel(confr, &regs->confr);
 395        priv->mode = mode;
 396
 397        debug("zynq_spi_set_mode: regs=%p, mode=%d\n", priv->regs, priv->mode);
 398
 399        return 0;
 400}
 401
 402/*
 403 * zynq_qspi_copy_read_data - Copy data to RX buffer
 404 * @zqspi:      Pointer to the zynq_qspi structure
 405 * @data:       The 32 bit variable where data is stored
 406 * @size:       Number of bytes to be copied from data to RX buffer
 407 */
 408static void zynq_qspi_copy_read_data(struct zynq_qspi_priv *priv, u32 data, u8 size)
 409{
 410        u8 byte3;
 411
 412        debug("%s: data 0x%04x rxbuf addr: 0x%08x size %d\n", __func__ ,
 413              data, (unsigned)(priv->rxbuf), size);
 414
 415        if (priv->rxbuf) {
 416                switch (size) {
 417                case 1:
 418                        *((u8 *)priv->rxbuf) = data;
 419                        priv->rxbuf += 1;
 420                        break;
 421                case 2:
 422                        *((u8 *)priv->rxbuf) = data;
 423                        priv->rxbuf += 1;
 424                        *((u8 *)priv->rxbuf) = (u8)(data >> 8);
 425                        priv->rxbuf += 1;
 426                        break;
 427                case 3:
 428                        *((u8 *)priv->rxbuf) = data;
 429                        priv->rxbuf += 1;
 430                        *((u8 *)priv->rxbuf) = (u8)(data >> 8);
 431                        priv->rxbuf += 1;
 432                        byte3 = (u8)(data >> 16);
 433                        *((u8 *)priv->rxbuf) = byte3;
 434                        priv->rxbuf += 1;
 435                        break;
 436                case 4:
 437                        /* Can not assume word aligned buffer */
 438                        memcpy(priv->rxbuf, &data, size);
 439                        priv->rxbuf += 4;
 440                        break;
 441                default:
 442                        /* This will never execute */
 443                        break;
 444                }
 445        }
 446        priv->bytes_to_receive -= size;
 447        if (priv->bytes_to_receive < 0)
 448                priv->bytes_to_receive = 0;
 449}
 450
 451/*
 452 * zynq_qspi_copy_write_data - Copy data from TX buffer
 453 * @zqspi:      Pointer to the zynq_qspi structure
 454 * @data:       Pointer to the 32 bit variable where data is to be copied
 455 * @size:       Number of bytes to be copied from TX buffer to data
 456 */
 457static void zynq_qspi_copy_write_data(struct  zynq_qspi_priv *priv,
 458                u32 *data, u8 size)
 459{
 460        if (priv->txbuf) {
 461                switch (size) {
 462                case 1:
 463                        *data = *((u8 *)priv->txbuf);
 464                        priv->txbuf += 1;
 465                        *data |= 0xFFFFFF00;
 466                        break;
 467                case 2:
 468                        *data = *((u8 *)priv->txbuf);
 469                        priv->txbuf += 1;
 470                        *data |= (*((u8 *)priv->txbuf) << 8);
 471                        priv->txbuf += 1;
 472                        *data |= 0xFFFF0000;
 473                        break;
 474                case 3:
 475                        *data = *((u8 *)priv->txbuf);
 476                        priv->txbuf += 1;
 477                        *data |= (*((u8 *)priv->txbuf) << 8);
 478                        priv->txbuf += 1;
 479                        *data |= (*((u8 *)priv->txbuf) << 16);
 480                        priv->txbuf += 1;
 481                        *data |= 0xFF000000;
 482                        break;
 483                case 4:
 484                        /* Can not assume word aligned buffer */
 485                        memcpy(data, priv->txbuf, size);
 486                        priv->txbuf += 4;
 487                        break;
 488                default:
 489                        /* This will never execute */
 490                        break;
 491                }
 492        } else {
 493                *data = 0;
 494        }
 495
 496        debug("%s: data 0x%08x txbuf addr: 0x%08x size %d\n", __func__,
 497              *data, (u32)priv->txbuf, size);
 498
 499        priv->bytes_to_transfer -= size;
 500        if (priv->bytes_to_transfer < 0)
 501                priv->bytes_to_transfer = 0;
 502}
 503
 504/*
 505 * zynq_qspi_chipselect - Select or deselect the chip select line
 506 * @qspi:       Pointer to the spi_device structure
 507 * @is_on:      Select(1) or deselect (0) the chip select line
 508 */
 509static void zynq_qspi_chipselect(struct  zynq_qspi_priv *priv, int is_on)
 510{
 511        u32 config_reg;
 512        struct zynq_qspi_regs *regs = priv->regs;
 513
 514        debug("%s: is_on: %d\n", __func__, is_on);
 515
 516        config_reg = readl(&regs->confr);
 517
 518        if (is_on) {
 519                /* Select the slave */
 520                config_reg &= ~ZYNQ_QSPI_CONFIG_SSCTRL_MASK;
 521                config_reg |= (((~(0x0001 << 0)) << 10) &
 522                                ZYNQ_QSPI_CONFIG_SSCTRL_MASK);
 523        } else
 524                /* Deselect the slave */
 525                config_reg |= ZYNQ_QSPI_CONFIG_SSCTRL_MASK;
 526
 527        writel(config_reg, &regs->confr);
 528}
 529
 530/*
 531 * zynq_qspi_fill_tx_fifo - Fills the TX FIFO with as many bytes as possible
 532 * @zqspi:      Pointer to the zynq_qspi structure
 533 */
 534static void zynq_qspi_fill_tx_fifo(struct zynq_qspi_priv *priv, u32 size)
 535{
 536        u32 data = 0;
 537        u32 fifocount = 0;
 538        unsigned len, offset;
 539        struct zynq_qspi_regs *regs = priv->regs;
 540        static const unsigned offsets[4] = {
 541                ZYNQ_QSPI_TXD_00_00_OFFSET, ZYNQ_QSPI_TXD_00_01_OFFSET,
 542                ZYNQ_QSPI_TXD_00_10_OFFSET, ZYNQ_QSPI_TXD_00_11_OFFSET };
 543
 544        while ((fifocount < size) &&
 545                        (priv->bytes_to_transfer > 0)) {
 546                if (priv->bytes_to_transfer >= 4) {
 547                        if (priv->txbuf) {
 548                                memcpy(&data, priv->txbuf, 4);
 549                                priv->txbuf += 4;
 550                        } else {
 551                                data = 0;
 552                        }
 553                        writel(data, &regs->txd0r);
 554                        priv->bytes_to_transfer -= 4;
 555                        fifocount++;
 556                } else {
 557                        /* Write TXD1, TXD2, TXD3 only if TxFIFO is empty. */
 558                        if (!(readl(&regs->isr)
 559                                        & ZYNQ_QSPI_IXR_TXNFULL_MASK) &&
 560                                        !priv->rxbuf)
 561                                return;
 562                        len = priv->bytes_to_transfer;
 563                        zynq_qspi_copy_write_data(priv, &data, len);
 564                        offset = (priv->rxbuf) ? offsets[0] : offsets[len];
 565                        writel(data, &regs->confr + (offset / 4));
 566                }
 567        }
 568}
 569
 570/*
 571 * zynq_qspi_irq_poll - Interrupt service routine of the QSPI controller
 572 * @zqspi:      Pointer to the zynq_qspi structure
 573 *
 574 * This function handles TX empty and Mode Fault interrupts only.
 575 * On TX empty interrupt this function reads the received data from RX FIFO and
 576 * fills the TX FIFO if there is any data remaining to be transferred.
 577 * On Mode Fault interrupt this function indicates that transfer is completed,
 578 * the SPI subsystem will identify the error as the remaining bytes to be
 579 * transferred is non-zero.
 580 *
 581 * returns:     0 for poll timeout
 582 *              1 transfer operation complete
 583 */
 584static int zynq_qspi_irq_poll(struct zynq_qspi_priv *priv)
 585{
 586        int max_loop;
 587        u32 intr_status;
 588        u32 rxindex = 0;
 589        u32 rxcount;
 590        struct zynq_qspi_regs *regs = priv->regs;
 591
 592        debug("%s: zqspi: 0x%08x\n", __func__, (u32)priv);
 593
 594        /* Poll until any of the interrupt status bits are set */
 595        max_loop = 0;
 596        do {
 597                intr_status = readl(&regs->isr);
 598                max_loop++;
 599        } while ((intr_status == 0) && (max_loop < 100000));
 600
 601        if (intr_status == 0) {
 602                debug("%s: Timeout\n", __func__);
 603                return 0;
 604        }
 605
 606        writel(intr_status, &regs->isr);
 607
 608        /* Disable all interrupts */
 609        writel(ZYNQ_QSPI_IXR_ALL_MASK, &regs->idisr);
 610        if ((intr_status & ZYNQ_QSPI_IXR_TXNFULL_MASK) ||
 611            (intr_status & ZYNQ_QSPI_IXR_RXNEMTY_MASK)) {
 612                /*
 613                 * This bit is set when Tx FIFO has < THRESHOLD entries. We have
 614                 * the THRESHOLD value set to 1, so this bit indicates Tx FIFO
 615                 * is empty
 616                 */
 617                rxcount = priv->bytes_to_receive - priv->bytes_to_transfer;
 618                rxcount = (rxcount % 4) ? ((rxcount/4)+1) : (rxcount/4);
 619                while ((rxindex < rxcount) &&
 620                                (rxindex < ZYNQ_QSPI_RXFIFO_THRESHOLD)) {
 621                        /* Read out the data from the RX FIFO */
 622                        u32 data;
 623                        data = readl(&regs->drxr);
 624
 625                        if (priv->bytes_to_receive >= 4) {
 626                                if (priv->rxbuf) {
 627                                        memcpy(priv->rxbuf, &data, 4);
 628                                        priv->rxbuf += 4;
 629                                }
 630                                priv->bytes_to_receive -= 4;
 631                        } else {
 632                                zynq_qspi_copy_read_data(priv, data,
 633                                        priv->bytes_to_receive);
 634                        }
 635                        rxindex++;
 636                }
 637
 638                if (priv->bytes_to_transfer) {
 639                        /* There is more data to send */
 640                        zynq_qspi_fill_tx_fifo(priv,
 641                                               ZYNQ_QSPI_RXFIFO_THRESHOLD);
 642
 643                        writel(ZYNQ_QSPI_IXR_ALL_MASK, &regs->ier);
 644                } else {
 645                        /*
 646                         * If transfer and receive is completed then only send
 647                         * complete signal
 648                         */
 649                        if (!priv->bytes_to_receive) {
 650                                /* return operation complete */
 651                                writel(ZYNQ_QSPI_IXR_ALL_MASK,
 652                                       &regs->idisr);
 653                                return 1;
 654                        }
 655                }
 656        }
 657
 658        return 0;
 659}
 660
 661/*
 662 * zynq_qspi_start_transfer - Initiates the QSPI transfer
 663 * @qspi:       Pointer to the spi_device structure
 664 * @transfer:   Pointer to the spi_transfer structure which provide information
 665 *              about next transfer parameters
 666 *
 667 * This function fills the TX FIFO, starts the QSPI transfer, and waits for the
 668 * transfer to be completed.
 669 *
 670 * returns:     Number of bytes transferred in the last transfer
 671 */
 672static int zynq_qspi_start_transfer(struct zynq_qspi_priv *priv)
 673{
 674        static u8 current_u_page;
 675        u32 data = 0;
 676        struct zynq_qspi_regs *regs = priv->regs;
 677
 678        debug("%s: qspi: 0x%08x transfer: 0x%08x len: %d\n", __func__,
 679              (u32)priv, (u32)priv, priv->len);
 680
 681        priv->bytes_to_transfer = priv->len;
 682        priv->bytes_to_receive = priv->len;
 683
 684        if (priv->is_inst && (priv->is_dual == SF_DUAL_STACKED_FLASH) &&
 685            (current_u_page != priv->u_page)) {
 686                if (priv->u_page) {
 687                        if (priv->is_dio == SF_DUALIO_FLASH)
 688                                writel((ZYNQ_QSPI_LCFG_TWO_MEM_MASK |
 689                                        ZYNQ_QSPI_LCFG_U_PAGE |
 690                                        (1 << ZYNQ_QSPI_LCFG_DUMMY_SHIFT) |
 691                                        ZYNQ_QSPI_FR_DUALIO_CODE),
 692                                        &regs->lcr);
 693                        else
 694                                /* Configure two memories on shared bus
 695                                 * by enabling upper mem
 696                                 */
 697                                writel((ZYNQ_QSPI_LCFG_TWO_MEM_MASK |
 698                                        ZYNQ_QSPI_LCFG_U_PAGE |
 699                                        (1 << ZYNQ_QSPI_LCFG_DUMMY_SHIFT) |
 700                                        ZYNQ_QSPI_FR_QOUT_CODE),
 701                                        &regs->lcr);
 702                } else {
 703                        if (priv->is_dio == SF_DUALIO_FLASH)
 704                                writel((ZYNQ_QSPI_LCFG_TWO_MEM_MASK |
 705                                        (1 << ZYNQ_QSPI_LCFG_DUMMY_SHIFT) |
 706                                        ZYNQ_QSPI_FR_DUALIO_CODE),
 707                                        &regs->lcr);
 708                        else
 709                                /* Configure two memories on shared bus
 710                                 * by enabling lower mem
 711                                 */
 712                                writel((ZYNQ_QSPI_LCFG_TWO_MEM_MASK |
 713                                        (1 << ZYNQ_QSPI_LCFG_DUMMY_SHIFT) |
 714                                        ZYNQ_QSPI_FR_QOUT_CODE),
 715                                        &regs->lcr);
 716                }
 717                current_u_page = priv->u_page;
 718        }
 719
 720        if (priv->len < 4)
 721                zynq_qspi_fill_tx_fifo(priv, priv->len);
 722        else
 723                zynq_qspi_fill_tx_fifo(priv, ZYNQ_QSPI_FIFO_DEPTH);
 724
 725        writel(ZYNQ_QSPI_IXR_ALL_MASK, &regs->ier);
 726        /* Start the transfer by enabling manual start bit */
 727
 728        /* wait for completion */
 729        do {
 730                data = zynq_qspi_irq_poll(priv);
 731        } while (data == 0);
 732
 733        return (priv->len) - (priv->bytes_to_transfer);
 734}
 735
 736static int zynq_qspi_transfer(struct zynq_qspi_priv *priv)
 737{
 738        unsigned cs_change = 1;
 739        int status = 0;
 740
 741        debug("%s\n", __func__);
 742
 743        while (1) {
 744
 745                /* Select the chip if required */
 746                if (cs_change)
 747                        zynq_qspi_chipselect(priv, 1);
 748
 749                cs_change = priv->cs_change;
 750
 751                if (!priv->txbuf && !priv->rxbuf && priv->len) {
 752                        status = -1;
 753                        break;
 754                }
 755
 756                /* Request the transfer */
 757                if (priv->len) {
 758                        status = zynq_qspi_start_transfer(priv);
 759                        priv->is_inst = 0;
 760                }
 761
 762                if (status != priv->len) {
 763                        if (status > 0)
 764                                status = -EMSGSIZE;
 765                        debug("zynq_qspi_transfer:%d len:%d\n", status, priv->len);
 766                        break;
 767                }
 768                status = 0;
 769
 770                if (cs_change)
 771                        /* Deselect the chip */
 772                        zynq_qspi_chipselect(priv, 0);
 773
 774                break;
 775        }
 776
 777        return 0;
 778}
 779
 780static int zynq_qspi_claim_bus(struct udevice *dev)
 781{
 782        struct udevice *bus = dev->parent;
 783        struct zynq_qspi_priv *priv = dev_get_priv(bus);
 784        struct zynq_qspi_regs *regs = priv->regs;
 785
 786        debug("%s\n", __func__);
 787        writel(ZYNQ_QSPI_ENABLE_ENABLE_MASK, &regs->enbr);
 788
 789        return 0;
 790}
 791
 792static int zynq_qspi_release_bus(struct udevice *dev)
 793{
 794        struct udevice *bus = dev->parent;
 795        struct zynq_qspi_priv *priv = dev_get_priv(bus);
 796        struct zynq_qspi_regs *regs = priv->regs;
 797
 798        debug("%s\n", __func__);
 799        writel(~ZYNQ_QSPI_ENABLE_ENABLE_MASK, &regs->enbr);
 800
 801        return 0;
 802}
 803
 804static int zynq_qspi_xfer(struct udevice *dev, unsigned int bitlen, const void *dout,
 805                void *din, unsigned long flags)
 806{
 807        struct udevice *bus = dev->parent;
 808        struct zynq_qspi_priv *priv = dev_get_priv(bus);
 809
 810        debug("%s", __func__);
 811        debug("%s: slave: 0x%08x bitlen: %d dout: 0x%08x ", __func__,
 812              (u32)priv, bitlen, (u32)dout);
 813        debug("din: 0x%08x flags: 0x%lx\n", (u32)din, flags);
 814
 815        priv->txbuf = dout;
 816        priv->rxbuf = din;
 817        priv->len = bitlen / 8;
 818
 819        /*
 820         * Festering sore.
 821         * Assume that the beginning of a transfer with bits to
 822         * transmit must contain a device command.
 823         */
 824        if (dout && flags & SPI_XFER_BEGIN)
 825                priv->is_inst = 1;
 826        else
 827                priv->is_inst = 0;
 828
 829        if (flags & SPI_XFER_END)
 830                priv->cs_change = 1;
 831        else
 832                priv->cs_change = 0;
 833
 834        if (flags & SPI_XFER_U_PAGE)
 835                priv->u_page = 1;
 836        else
 837                priv->u_page = 0;
 838
 839        zynq_qspi_transfer(priv);
 840
 841        return 0;
 842}
 843
 844static const struct dm_spi_ops zynq_qspi_ops = {
 845        .claim_bus      = zynq_qspi_claim_bus,
 846        .release_bus    = zynq_qspi_release_bus,
 847        .xfer           = zynq_qspi_xfer,
 848        .set_speed      = zynq_qspi_set_speed,
 849        .set_mode       = zynq_qspi_set_mode,
 850};
 851
 852static const struct udevice_id zynq_qspi_ids[] = {
 853        { .compatible = "xlnx,zynq-qspi-1.0" },
 854        { }
 855};
 856
 857U_BOOT_DRIVER(zynq_qspi) = {
 858        .name   = "zynq_qspi",
 859        .id     = UCLASS_SPI,
 860        .of_match = zynq_qspi_ids,
 861        .ops    = &zynq_qspi_ops,
 862        .ofdata_to_platdata = zynq_qspi_ofdata_to_platdata,
 863        .platdata_auto_alloc_size = sizeof(struct zynq_qspi_platdata),
 864        .priv_auto_alloc_size = sizeof(struct zynq_qspi_priv),
 865        .probe  = zynq_qspi_probe,
 866        .child_pre_probe = zynq_qspi_child_pre_probe,
 867};
 868