Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2013 Xilinx, Inc. |
| 4 | * (C) Copyright 2015 Jagan Teki <jteki@openedev.com> |
| 5 | * |
| 6 | * Xilinx Zynq Quad-SPI(QSPI) controller driver (master mode only) |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <dm.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 12 | #include <malloc.h> |
| 13 | #include <spi.h> |
| 14 | #include <asm/io.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 15 | #include <linux/bitops.h> |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 16 | |
| 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
| 19 | /* zynq qspi register bit masks ZYNQ_QSPI_<REG>_<BIT>_MASK */ |
Jagan Teki | 736b4df | 2015-10-22 20:40:16 +0530 | [diff] [blame] | 20 | #define ZYNQ_QSPI_CR_IFMODE_MASK BIT(31) /* Flash intrface mode*/ |
| 21 | #define ZYNQ_QSPI_CR_MSA_MASK BIT(15) /* Manual start enb */ |
| 22 | #define ZYNQ_QSPI_CR_MCS_MASK BIT(14) /* Manual chip select */ |
| 23 | #define ZYNQ_QSPI_CR_PCS_MASK BIT(10) /* Peri chip select */ |
Jagan Teki | 9cf2ffb | 2015-10-22 21:06:37 +0530 | [diff] [blame] | 24 | #define ZYNQ_QSPI_CR_FW_MASK GENMASK(7, 6) /* FIFO width */ |
| 25 | #define ZYNQ_QSPI_CR_SS_MASK GENMASK(13, 10) /* Slave Select */ |
| 26 | #define ZYNQ_QSPI_CR_BAUD_MASK GENMASK(5, 3) /* Baud rate div */ |
Jagan Teki | 736b4df | 2015-10-22 20:40:16 +0530 | [diff] [blame] | 27 | #define ZYNQ_QSPI_CR_CPHA_MASK BIT(2) /* Clock phase */ |
| 28 | #define ZYNQ_QSPI_CR_CPOL_MASK BIT(1) /* Clock polarity */ |
| 29 | #define ZYNQ_QSPI_CR_MSTREN_MASK BIT(0) /* Mode select */ |
| 30 | #define ZYNQ_QSPI_IXR_RXNEMPTY_MASK BIT(4) /* RX_FIFO_not_empty */ |
| 31 | #define ZYNQ_QSPI_IXR_TXOW_MASK BIT(2) /* TX_FIFO_not_full */ |
Jagan Teki | 9cf2ffb | 2015-10-22 21:06:37 +0530 | [diff] [blame] | 32 | #define ZYNQ_QSPI_IXR_ALL_MASK GENMASK(6, 0) /* All IXR bits */ |
Jagan Teki | 736b4df | 2015-10-22 20:40:16 +0530 | [diff] [blame] | 33 | #define ZYNQ_QSPI_ENR_SPI_EN_MASK BIT(0) /* SPI Enable */ |
Nathan Rossi | 04a44d3 | 2015-12-09 00:44:40 +1000 | [diff] [blame] | 34 | #define ZYNQ_QSPI_LQSPICFG_LQMODE_MASK BIT(31) /* Linear QSPI Mode */ |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 35 | |
| 36 | /* zynq qspi Transmit Data Register */ |
| 37 | #define ZYNQ_QSPI_TXD_00_00_OFFSET 0x1C /* Transmit 4-byte inst */ |
| 38 | #define ZYNQ_QSPI_TXD_00_01_OFFSET 0x80 /* Transmit 1-byte inst */ |
| 39 | #define ZYNQ_QSPI_TXD_00_10_OFFSET 0x84 /* Transmit 2-byte inst */ |
| 40 | #define ZYNQ_QSPI_TXD_00_11_OFFSET 0x88 /* Transmit 3-byte inst */ |
| 41 | |
| 42 | #define ZYNQ_QSPI_TXFIFO_THRESHOLD 1 /* Tx FIFO threshold level*/ |
| 43 | #define ZYNQ_QSPI_RXFIFO_THRESHOLD 32 /* Rx FIFO threshold level */ |
| 44 | |
| 45 | #define ZYNQ_QSPI_CR_BAUD_MAX 8 /* Baud rate divisor max val */ |
| 46 | #define ZYNQ_QSPI_CR_BAUD_SHIFT 3 /* Baud rate divisor shift */ |
| 47 | #define ZYNQ_QSPI_CR_SS_SHIFT 10 /* Slave select shift */ |
| 48 | |
| 49 | #define ZYNQ_QSPI_FIFO_DEPTH 63 |
| 50 | #ifndef CONFIG_SYS_ZYNQ_QSPI_WAIT |
| 51 | #define CONFIG_SYS_ZYNQ_QSPI_WAIT CONFIG_SYS_HZ/100 /* 10 ms */ |
| 52 | #endif |
| 53 | |
| 54 | /* zynq qspi register set */ |
| 55 | struct zynq_qspi_regs { |
| 56 | u32 cr; /* 0x00 */ |
| 57 | u32 isr; /* 0x04 */ |
| 58 | u32 ier; /* 0x08 */ |
| 59 | u32 idr; /* 0x0C */ |
| 60 | u32 imr; /* 0x10 */ |
| 61 | u32 enr; /* 0x14 */ |
| 62 | u32 dr; /* 0x18 */ |
| 63 | u32 txd0r; /* 0x1C */ |
| 64 | u32 drxr; /* 0x20 */ |
| 65 | u32 sicr; /* 0x24 */ |
| 66 | u32 txftr; /* 0x28 */ |
| 67 | u32 rxftr; /* 0x2C */ |
| 68 | u32 gpior; /* 0x30 */ |
| 69 | u32 reserved0[19]; |
| 70 | u32 txd1r; /* 0x80 */ |
| 71 | u32 txd2r; /* 0x84 */ |
| 72 | u32 txd3r; /* 0x88 */ |
Nathan Rossi | 04a44d3 | 2015-12-09 00:44:40 +1000 | [diff] [blame] | 73 | u32 reserved1[5]; |
| 74 | u32 lqspicfg; /* 0xA0 */ |
| 75 | u32 lqspists; /* 0xA4 */ |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | /* zynq qspi platform data */ |
| 79 | struct zynq_qspi_platdata { |
| 80 | struct zynq_qspi_regs *regs; |
| 81 | u32 frequency; /* input frequency */ |
| 82 | u32 speed_hz; |
| 83 | }; |
| 84 | |
| 85 | /* zynq qspi priv */ |
| 86 | struct zynq_qspi_priv { |
| 87 | struct zynq_qspi_regs *regs; |
| 88 | u8 cs; |
| 89 | u8 mode; |
| 90 | u8 fifo_depth; |
| 91 | u32 freq; /* required frequency */ |
| 92 | const void *tx_buf; |
| 93 | void *rx_buf; |
| 94 | unsigned len; |
| 95 | int bytes_to_transfer; |
| 96 | int bytes_to_receive; |
| 97 | unsigned int is_inst; |
| 98 | unsigned cs_change:1; |
| 99 | }; |
| 100 | |
| 101 | static int zynq_qspi_ofdata_to_platdata(struct udevice *bus) |
| 102 | { |
| 103 | struct zynq_qspi_platdata *plat = bus->platdata; |
| 104 | const void *blob = gd->fdt_blob; |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 105 | int node = dev_of_offset(bus); |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 106 | |
| 107 | plat->regs = (struct zynq_qspi_regs *)fdtdec_get_addr(blob, |
| 108 | node, "reg"); |
| 109 | |
| 110 | /* FIXME: Use 166MHz as a suitable default */ |
| 111 | plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency", |
| 112 | 166666666); |
| 113 | plat->speed_hz = plat->frequency / 2; |
| 114 | |
| 115 | debug("%s: regs=%p max-frequency=%d\n", __func__, |
| 116 | plat->regs, plat->frequency); |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | static void zynq_qspi_init_hw(struct zynq_qspi_priv *priv) |
| 122 | { |
| 123 | struct zynq_qspi_regs *regs = priv->regs; |
| 124 | u32 confr; |
| 125 | |
| 126 | /* Disable QSPI */ |
| 127 | writel(~ZYNQ_QSPI_ENR_SPI_EN_MASK, ®s->enr); |
| 128 | |
| 129 | /* Disable Interrupts */ |
| 130 | writel(ZYNQ_QSPI_IXR_ALL_MASK, ®s->idr); |
| 131 | |
| 132 | /* Clear the TX and RX threshold reg */ |
| 133 | writel(ZYNQ_QSPI_TXFIFO_THRESHOLD, ®s->txftr); |
| 134 | writel(ZYNQ_QSPI_RXFIFO_THRESHOLD, ®s->rxftr); |
| 135 | |
| 136 | /* Clear the RX FIFO */ |
| 137 | while (readl(®s->isr) & ZYNQ_QSPI_IXR_RXNEMPTY_MASK) |
| 138 | readl(®s->drxr); |
| 139 | |
| 140 | /* Clear Interrupts */ |
| 141 | writel(ZYNQ_QSPI_IXR_ALL_MASK, ®s->isr); |
| 142 | |
| 143 | /* Manual slave select and Auto start */ |
| 144 | confr = readl(®s->cr); |
| 145 | confr &= ~ZYNQ_QSPI_CR_MSA_MASK; |
| 146 | confr |= ZYNQ_QSPI_CR_IFMODE_MASK | ZYNQ_QSPI_CR_MCS_MASK | |
| 147 | ZYNQ_QSPI_CR_PCS_MASK | ZYNQ_QSPI_CR_FW_MASK | |
| 148 | ZYNQ_QSPI_CR_MSTREN_MASK; |
| 149 | writel(confr, ®s->cr); |
| 150 | |
Nathan Rossi | 04a44d3 | 2015-12-09 00:44:40 +1000 | [diff] [blame] | 151 | /* Disable the LQSPI feature */ |
| 152 | confr = readl(®s->lqspicfg); |
| 153 | confr &= ~ZYNQ_QSPI_LQSPICFG_LQMODE_MASK; |
| 154 | writel(confr, ®s->lqspicfg); |
| 155 | |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 156 | /* Enable SPI */ |
| 157 | writel(ZYNQ_QSPI_ENR_SPI_EN_MASK, ®s->enr); |
| 158 | } |
| 159 | |
| 160 | static int zynq_qspi_probe(struct udevice *bus) |
| 161 | { |
| 162 | struct zynq_qspi_platdata *plat = dev_get_platdata(bus); |
| 163 | struct zynq_qspi_priv *priv = dev_get_priv(bus); |
| 164 | |
| 165 | priv->regs = plat->regs; |
| 166 | priv->fifo_depth = ZYNQ_QSPI_FIFO_DEPTH; |
| 167 | |
| 168 | /* init the zynq spi hw */ |
| 169 | zynq_qspi_init_hw(priv); |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | /* |
| 175 | * zynq_qspi_read_data - Copy data to RX buffer |
| 176 | * @zqspi: Pointer to the zynq_qspi structure |
| 177 | * @data: The 32 bit variable where data is stored |
| 178 | * @size: Number of bytes to be copied from data to RX buffer |
| 179 | */ |
| 180 | static void zynq_qspi_read_data(struct zynq_qspi_priv *priv, u32 data, u8 size) |
| 181 | { |
| 182 | u8 byte3; |
| 183 | |
| 184 | debug("%s: data 0x%04x rx_buf addr: 0x%08x size %d\n", __func__ , |
| 185 | data, (unsigned)(priv->rx_buf), size); |
| 186 | |
| 187 | if (priv->rx_buf) { |
| 188 | switch (size) { |
| 189 | case 1: |
| 190 | *((u8 *)priv->rx_buf) = data; |
| 191 | priv->rx_buf += 1; |
| 192 | break; |
| 193 | case 2: |
| 194 | *((u16 *)priv->rx_buf) = data; |
| 195 | priv->rx_buf += 2; |
| 196 | break; |
| 197 | case 3: |
| 198 | *((u16 *)priv->rx_buf) = data; |
| 199 | priv->rx_buf += 2; |
| 200 | byte3 = (u8)(data >> 16); |
| 201 | *((u8 *)priv->rx_buf) = byte3; |
| 202 | priv->rx_buf += 1; |
| 203 | break; |
| 204 | case 4: |
| 205 | /* Can not assume word aligned buffer */ |
| 206 | memcpy(priv->rx_buf, &data, size); |
| 207 | priv->rx_buf += 4; |
| 208 | break; |
| 209 | default: |
| 210 | /* This will never execute */ |
| 211 | break; |
| 212 | } |
| 213 | } |
| 214 | priv->bytes_to_receive -= size; |
| 215 | if (priv->bytes_to_receive < 0) |
| 216 | priv->bytes_to_receive = 0; |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * zynq_qspi_write_data - Copy data from TX buffer |
| 221 | * @zqspi: Pointer to the zynq_qspi structure |
| 222 | * @data: Pointer to the 32 bit variable where data is to be copied |
| 223 | * @size: Number of bytes to be copied from TX buffer to data |
| 224 | */ |
| 225 | static void zynq_qspi_write_data(struct zynq_qspi_priv *priv, |
| 226 | u32 *data, u8 size) |
| 227 | { |
| 228 | if (priv->tx_buf) { |
| 229 | switch (size) { |
| 230 | case 1: |
| 231 | *data = *((u8 *)priv->tx_buf); |
| 232 | priv->tx_buf += 1; |
| 233 | *data |= 0xFFFFFF00; |
| 234 | break; |
| 235 | case 2: |
| 236 | *data = *((u16 *)priv->tx_buf); |
| 237 | priv->tx_buf += 2; |
| 238 | *data |= 0xFFFF0000; |
| 239 | break; |
| 240 | case 3: |
| 241 | *data = *((u16 *)priv->tx_buf); |
| 242 | priv->tx_buf += 2; |
| 243 | *data |= (*((u8 *)priv->tx_buf) << 16); |
| 244 | priv->tx_buf += 1; |
| 245 | *data |= 0xFF000000; |
| 246 | break; |
| 247 | case 4: |
| 248 | /* Can not assume word aligned buffer */ |
| 249 | memcpy(data, priv->tx_buf, size); |
| 250 | priv->tx_buf += 4; |
| 251 | break; |
| 252 | default: |
| 253 | /* This will never execute */ |
| 254 | break; |
| 255 | } |
| 256 | } else { |
| 257 | *data = 0; |
| 258 | } |
| 259 | |
| 260 | debug("%s: data 0x%08x tx_buf addr: 0x%08x size %d\n", __func__, |
| 261 | *data, (u32)priv->tx_buf, size); |
| 262 | |
| 263 | priv->bytes_to_transfer -= size; |
| 264 | if (priv->bytes_to_transfer < 0) |
| 265 | priv->bytes_to_transfer = 0; |
| 266 | } |
| 267 | |
| 268 | static void zynq_qspi_chipselect(struct zynq_qspi_priv *priv, int is_on) |
| 269 | { |
| 270 | u32 confr; |
| 271 | struct zynq_qspi_regs *regs = priv->regs; |
| 272 | |
| 273 | confr = readl(®s->cr); |
| 274 | |
| 275 | if (is_on) { |
| 276 | /* Select the slave */ |
| 277 | confr &= ~ZYNQ_QSPI_CR_SS_MASK; |
| 278 | confr |= (~(1 << priv->cs) << ZYNQ_QSPI_CR_SS_SHIFT) & |
| 279 | ZYNQ_QSPI_CR_SS_MASK; |
| 280 | } else |
| 281 | /* Deselect the slave */ |
| 282 | confr |= ZYNQ_QSPI_CR_SS_MASK; |
| 283 | |
| 284 | writel(confr, ®s->cr); |
| 285 | } |
| 286 | |
| 287 | /* |
| 288 | * zynq_qspi_fill_tx_fifo - Fills the TX FIFO with as many bytes as possible |
| 289 | * @zqspi: Pointer to the zynq_qspi structure |
| 290 | */ |
| 291 | static void zynq_qspi_fill_tx_fifo(struct zynq_qspi_priv *priv, u32 size) |
| 292 | { |
| 293 | u32 data = 0; |
| 294 | u32 fifocount = 0; |
| 295 | unsigned len, offset; |
| 296 | struct zynq_qspi_regs *regs = priv->regs; |
| 297 | static const unsigned offsets[4] = { |
| 298 | ZYNQ_QSPI_TXD_00_00_OFFSET, ZYNQ_QSPI_TXD_00_01_OFFSET, |
| 299 | ZYNQ_QSPI_TXD_00_10_OFFSET, ZYNQ_QSPI_TXD_00_11_OFFSET }; |
| 300 | |
| 301 | while ((fifocount < size) && |
| 302 | (priv->bytes_to_transfer > 0)) { |
| 303 | if (priv->bytes_to_transfer >= 4) { |
| 304 | if (priv->tx_buf) { |
| 305 | memcpy(&data, priv->tx_buf, 4); |
| 306 | priv->tx_buf += 4; |
| 307 | } else { |
| 308 | data = 0; |
| 309 | } |
| 310 | writel(data, ®s->txd0r); |
| 311 | priv->bytes_to_transfer -= 4; |
| 312 | fifocount++; |
| 313 | } else { |
| 314 | /* Write TXD1, TXD2, TXD3 only if TxFIFO is empty. */ |
| 315 | if (!(readl(®s->isr) |
| 316 | & ZYNQ_QSPI_IXR_TXOW_MASK) && |
| 317 | !priv->rx_buf) |
| 318 | return; |
| 319 | len = priv->bytes_to_transfer; |
| 320 | zynq_qspi_write_data(priv, &data, len); |
| 321 | offset = (priv->rx_buf) ? offsets[0] : offsets[len]; |
| 322 | writel(data, ®s->cr + (offset / 4)); |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | * zynq_qspi_irq_poll - Interrupt service routine of the QSPI controller |
| 329 | * @zqspi: Pointer to the zynq_qspi structure |
| 330 | * |
| 331 | * This function handles TX empty and Mode Fault interrupts only. |
| 332 | * On TX empty interrupt this function reads the received data from RX FIFO and |
| 333 | * fills the TX FIFO if there is any data remaining to be transferred. |
| 334 | * On Mode Fault interrupt this function indicates that transfer is completed, |
| 335 | * the SPI subsystem will identify the error as the remaining bytes to be |
| 336 | * transferred is non-zero. |
| 337 | * |
| 338 | * returns: 0 for poll timeout |
| 339 | * 1 transfer operation complete |
| 340 | */ |
| 341 | static int zynq_qspi_irq_poll(struct zynq_qspi_priv *priv) |
| 342 | { |
| 343 | struct zynq_qspi_regs *regs = priv->regs; |
| 344 | u32 rxindex = 0; |
| 345 | u32 rxcount; |
| 346 | u32 status, timeout; |
| 347 | |
| 348 | /* Poll until any of the interrupt status bits are set */ |
| 349 | timeout = get_timer(0); |
| 350 | do { |
| 351 | status = readl(®s->isr); |
| 352 | } while ((status == 0) && |
| 353 | (get_timer(timeout) < CONFIG_SYS_ZYNQ_QSPI_WAIT)); |
| 354 | |
| 355 | if (status == 0) { |
| 356 | printf("zynq_qspi_irq_poll: Timeout!\n"); |
| 357 | return -ETIMEDOUT; |
| 358 | } |
| 359 | |
| 360 | writel(status, ®s->isr); |
| 361 | |
| 362 | /* Disable all interrupts */ |
| 363 | writel(ZYNQ_QSPI_IXR_ALL_MASK, ®s->idr); |
| 364 | if ((status & ZYNQ_QSPI_IXR_TXOW_MASK) || |
| 365 | (status & ZYNQ_QSPI_IXR_RXNEMPTY_MASK)) { |
| 366 | /* |
| 367 | * This bit is set when Tx FIFO has < THRESHOLD entries. We have |
| 368 | * the THRESHOLD value set to 1, so this bit indicates Tx FIFO |
| 369 | * is empty |
| 370 | */ |
| 371 | rxcount = priv->bytes_to_receive - priv->bytes_to_transfer; |
| 372 | rxcount = (rxcount % 4) ? ((rxcount/4)+1) : (rxcount/4); |
| 373 | while ((rxindex < rxcount) && |
| 374 | (rxindex < ZYNQ_QSPI_RXFIFO_THRESHOLD)) { |
| 375 | /* Read out the data from the RX FIFO */ |
| 376 | u32 data; |
| 377 | data = readl(®s->drxr); |
| 378 | |
| 379 | if (priv->bytes_to_receive >= 4) { |
| 380 | if (priv->rx_buf) { |
| 381 | memcpy(priv->rx_buf, &data, 4); |
| 382 | priv->rx_buf += 4; |
| 383 | } |
| 384 | priv->bytes_to_receive -= 4; |
| 385 | } else { |
| 386 | zynq_qspi_read_data(priv, data, |
| 387 | priv->bytes_to_receive); |
| 388 | } |
| 389 | rxindex++; |
| 390 | } |
| 391 | |
| 392 | if (priv->bytes_to_transfer) { |
| 393 | /* There is more data to send */ |
| 394 | zynq_qspi_fill_tx_fifo(priv, |
| 395 | ZYNQ_QSPI_RXFIFO_THRESHOLD); |
| 396 | |
| 397 | writel(ZYNQ_QSPI_IXR_ALL_MASK, ®s->ier); |
| 398 | } else { |
| 399 | /* |
| 400 | * If transfer and receive is completed then only send |
| 401 | * complete signal |
| 402 | */ |
| 403 | if (!priv->bytes_to_receive) { |
| 404 | /* return operation complete */ |
| 405 | writel(ZYNQ_QSPI_IXR_ALL_MASK, |
| 406 | ®s->idr); |
| 407 | return 1; |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | /* |
| 416 | * zynq_qspi_start_transfer - Initiates the QSPI transfer |
| 417 | * @qspi: Pointer to the spi_device structure |
| 418 | * @transfer: Pointer to the spi_transfer structure which provide information |
| 419 | * about next transfer parameters |
| 420 | * |
| 421 | * This function fills the TX FIFO, starts the QSPI transfer, and waits for the |
| 422 | * transfer to be completed. |
| 423 | * |
| 424 | * returns: Number of bytes transferred in the last transfer |
| 425 | */ |
| 426 | static int zynq_qspi_start_transfer(struct zynq_qspi_priv *priv) |
| 427 | { |
| 428 | u32 data = 0; |
| 429 | struct zynq_qspi_regs *regs = priv->regs; |
| 430 | |
| 431 | debug("%s: qspi: 0x%08x transfer: 0x%08x len: %d\n", __func__, |
| 432 | (u32)priv, (u32)priv, priv->len); |
| 433 | |
| 434 | priv->bytes_to_transfer = priv->len; |
| 435 | priv->bytes_to_receive = priv->len; |
| 436 | |
| 437 | if (priv->len < 4) |
| 438 | zynq_qspi_fill_tx_fifo(priv, priv->len); |
| 439 | else |
| 440 | zynq_qspi_fill_tx_fifo(priv, priv->fifo_depth); |
| 441 | |
| 442 | writel(ZYNQ_QSPI_IXR_ALL_MASK, ®s->ier); |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 443 | |
| 444 | /* wait for completion */ |
| 445 | do { |
| 446 | data = zynq_qspi_irq_poll(priv); |
| 447 | } while (data == 0); |
| 448 | |
| 449 | return (priv->len) - (priv->bytes_to_transfer); |
| 450 | } |
| 451 | |
| 452 | static int zynq_qspi_transfer(struct zynq_qspi_priv *priv) |
| 453 | { |
| 454 | unsigned cs_change = 1; |
| 455 | int status = 0; |
| 456 | |
| 457 | while (1) { |
| 458 | /* Select the chip if required */ |
| 459 | if (cs_change) |
| 460 | zynq_qspi_chipselect(priv, 1); |
| 461 | |
| 462 | cs_change = priv->cs_change; |
| 463 | |
| 464 | if (!priv->tx_buf && !priv->rx_buf && priv->len) { |
| 465 | status = -1; |
| 466 | break; |
| 467 | } |
| 468 | |
| 469 | /* Request the transfer */ |
| 470 | if (priv->len) { |
| 471 | status = zynq_qspi_start_transfer(priv); |
| 472 | priv->is_inst = 0; |
| 473 | } |
| 474 | |
| 475 | if (status != priv->len) { |
| 476 | if (status > 0) |
| 477 | status = -EMSGSIZE; |
| 478 | debug("zynq_qspi_transfer:%d len:%d\n", |
| 479 | status, priv->len); |
| 480 | break; |
| 481 | } |
| 482 | status = 0; |
| 483 | |
| 484 | if (cs_change) |
| 485 | /* Deselect the chip */ |
| 486 | zynq_qspi_chipselect(priv, 0); |
| 487 | |
| 488 | break; |
| 489 | } |
| 490 | |
Vipul Kumar | 240cd75 | 2018-06-25 14:13:57 +0530 | [diff] [blame] | 491 | return status; |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | static int zynq_qspi_claim_bus(struct udevice *dev) |
| 495 | { |
| 496 | struct udevice *bus = dev->parent; |
| 497 | struct zynq_qspi_priv *priv = dev_get_priv(bus); |
| 498 | struct zynq_qspi_regs *regs = priv->regs; |
| 499 | |
| 500 | writel(ZYNQ_QSPI_ENR_SPI_EN_MASK, ®s->enr); |
| 501 | |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | static int zynq_qspi_release_bus(struct udevice *dev) |
| 506 | { |
| 507 | struct udevice *bus = dev->parent; |
| 508 | struct zynq_qspi_priv *priv = dev_get_priv(bus); |
| 509 | struct zynq_qspi_regs *regs = priv->regs; |
| 510 | |
| 511 | writel(~ZYNQ_QSPI_ENR_SPI_EN_MASK, ®s->enr); |
| 512 | |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | static int zynq_qspi_xfer(struct udevice *dev, unsigned int bitlen, |
| 517 | const void *dout, void *din, unsigned long flags) |
| 518 | { |
| 519 | struct udevice *bus = dev->parent; |
| 520 | struct zynq_qspi_priv *priv = dev_get_priv(bus); |
| 521 | struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev); |
| 522 | |
| 523 | priv->cs = slave_plat->cs; |
| 524 | priv->tx_buf = dout; |
| 525 | priv->rx_buf = din; |
| 526 | priv->len = bitlen / 8; |
| 527 | |
Jagan Teki | e5e0d68 | 2015-10-25 09:31:54 +0530 | [diff] [blame] | 528 | debug("zynq_qspi_xfer: bus:%i cs:%i bitlen:%i len:%i flags:%lx\n", |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 529 | bus->seq, slave_plat->cs, bitlen, priv->len, flags); |
| 530 | |
| 531 | /* |
| 532 | * Festering sore. |
| 533 | * Assume that the beginning of a transfer with bits to |
| 534 | * transmit must contain a device command. |
| 535 | */ |
| 536 | if (dout && flags & SPI_XFER_BEGIN) |
| 537 | priv->is_inst = 1; |
| 538 | else |
| 539 | priv->is_inst = 0; |
| 540 | |
| 541 | if (flags & SPI_XFER_END) |
| 542 | priv->cs_change = 1; |
| 543 | else |
| 544 | priv->cs_change = 0; |
| 545 | |
| 546 | zynq_qspi_transfer(priv); |
| 547 | |
| 548 | return 0; |
| 549 | } |
| 550 | |
| 551 | static int zynq_qspi_set_speed(struct udevice *bus, uint speed) |
| 552 | { |
| 553 | struct zynq_qspi_platdata *plat = bus->platdata; |
| 554 | struct zynq_qspi_priv *priv = dev_get_priv(bus); |
| 555 | struct zynq_qspi_regs *regs = priv->regs; |
| 556 | uint32_t confr; |
| 557 | u8 baud_rate_val = 0; |
| 558 | |
| 559 | if (speed > plat->frequency) |
| 560 | speed = plat->frequency; |
| 561 | |
| 562 | /* Set the clock frequency */ |
| 563 | confr = readl(®s->cr); |
| 564 | if (speed == 0) { |
| 565 | /* Set baudrate x8, if the freq is 0 */ |
| 566 | baud_rate_val = 0x2; |
| 567 | } else if (plat->speed_hz != speed) { |
| 568 | while ((baud_rate_val < ZYNQ_QSPI_CR_BAUD_MAX) && |
| 569 | ((plat->frequency / |
| 570 | (2 << baud_rate_val)) > speed)) |
| 571 | baud_rate_val++; |
| 572 | |
| 573 | plat->speed_hz = speed / (2 << baud_rate_val); |
| 574 | } |
| 575 | confr &= ~ZYNQ_QSPI_CR_BAUD_MASK; |
| 576 | confr |= (baud_rate_val << ZYNQ_QSPI_CR_BAUD_SHIFT); |
| 577 | |
| 578 | writel(confr, ®s->cr); |
| 579 | priv->freq = speed; |
| 580 | |
Jagan Teki | e5e0d68 | 2015-10-25 09:31:54 +0530 | [diff] [blame] | 581 | debug("%s: regs=%p, speed=%d\n", __func__, priv->regs, priv->freq); |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 582 | |
| 583 | return 0; |
| 584 | } |
| 585 | |
| 586 | static int zynq_qspi_set_mode(struct udevice *bus, uint mode) |
| 587 | { |
| 588 | struct zynq_qspi_priv *priv = dev_get_priv(bus); |
| 589 | struct zynq_qspi_regs *regs = priv->regs; |
| 590 | uint32_t confr; |
| 591 | |
| 592 | /* Set the SPI Clock phase and polarities */ |
| 593 | confr = readl(®s->cr); |
| 594 | confr &= ~(ZYNQ_QSPI_CR_CPHA_MASK | ZYNQ_QSPI_CR_CPOL_MASK); |
| 595 | |
Jagan Teki | 2775e91 | 2015-09-08 01:39:44 +0530 | [diff] [blame] | 596 | if (mode & SPI_CPHA) |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 597 | confr |= ZYNQ_QSPI_CR_CPHA_MASK; |
Jagan Teki | 2775e91 | 2015-09-08 01:39:44 +0530 | [diff] [blame] | 598 | if (mode & SPI_CPOL) |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 599 | confr |= ZYNQ_QSPI_CR_CPOL_MASK; |
| 600 | |
| 601 | writel(confr, ®s->cr); |
| 602 | priv->mode = mode; |
| 603 | |
Jagan Teki | e5e0d68 | 2015-10-25 09:31:54 +0530 | [diff] [blame] | 604 | debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode); |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 605 | |
| 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | static const struct dm_spi_ops zynq_qspi_ops = { |
| 610 | .claim_bus = zynq_qspi_claim_bus, |
| 611 | .release_bus = zynq_qspi_release_bus, |
| 612 | .xfer = zynq_qspi_xfer, |
| 613 | .set_speed = zynq_qspi_set_speed, |
| 614 | .set_mode = zynq_qspi_set_mode, |
| 615 | }; |
| 616 | |
| 617 | static const struct udevice_id zynq_qspi_ids[] = { |
| 618 | { .compatible = "xlnx,zynq-qspi-1.0" }, |
| 619 | { } |
| 620 | }; |
| 621 | |
| 622 | U_BOOT_DRIVER(zynq_qspi) = { |
| 623 | .name = "zynq_qspi", |
| 624 | .id = UCLASS_SPI, |
| 625 | .of_match = zynq_qspi_ids, |
| 626 | .ops = &zynq_qspi_ops, |
| 627 | .ofdata_to_platdata = zynq_qspi_ofdata_to_platdata, |
| 628 | .platdata_auto_alloc_size = sizeof(struct zynq_qspi_platdata), |
| 629 | .priv_auto_alloc_size = sizeof(struct zynq_qspi_priv), |
| 630 | .probe = zynq_qspi_probe, |
| 631 | }; |