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 | |
T Karthik Reddy | ea836be | 2020-02-04 05:47:45 -0700 | [diff] [blame] | 9 | #include <clk.h> |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 10 | #include <common.h> |
| 11 | #include <dm.h> |
T Karthik Reddy | ea836be | 2020-02-04 05:47:45 -0700 | [diff] [blame] | 12 | #include <dm/device_compat.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 14 | #include <malloc.h> |
| 15 | #include <spi.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 16 | #include <asm/global_data.h> |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 17 | #include <asm/io.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 18 | #include <linux/bitops.h> |
T Karthik Reddy | bab0c02 | 2022-01-30 22:22:40 -0700 | [diff] [blame] | 19 | #include <spi-mem.h> |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 20 | |
| 21 | DECLARE_GLOBAL_DATA_PTR; |
| 22 | |
| 23 | /* zynq qspi register bit masks ZYNQ_QSPI_<REG>_<BIT>_MASK */ |
Jagan Teki | 736b4df | 2015-10-22 20:40:16 +0530 | [diff] [blame] | 24 | #define ZYNQ_QSPI_CR_IFMODE_MASK BIT(31) /* Flash intrface mode*/ |
| 25 | #define ZYNQ_QSPI_CR_MSA_MASK BIT(15) /* Manual start enb */ |
| 26 | #define ZYNQ_QSPI_CR_MCS_MASK BIT(14) /* Manual chip select */ |
| 27 | #define ZYNQ_QSPI_CR_PCS_MASK BIT(10) /* Peri chip select */ |
Jagan Teki | 9cf2ffb | 2015-10-22 21:06:37 +0530 | [diff] [blame] | 28 | #define ZYNQ_QSPI_CR_FW_MASK GENMASK(7, 6) /* FIFO width */ |
| 29 | #define ZYNQ_QSPI_CR_SS_MASK GENMASK(13, 10) /* Slave Select */ |
| 30 | #define ZYNQ_QSPI_CR_BAUD_MASK GENMASK(5, 3) /* Baud rate div */ |
Jagan Teki | 736b4df | 2015-10-22 20:40:16 +0530 | [diff] [blame] | 31 | #define ZYNQ_QSPI_CR_CPHA_MASK BIT(2) /* Clock phase */ |
| 32 | #define ZYNQ_QSPI_CR_CPOL_MASK BIT(1) /* Clock polarity */ |
| 33 | #define ZYNQ_QSPI_CR_MSTREN_MASK BIT(0) /* Mode select */ |
| 34 | #define ZYNQ_QSPI_IXR_RXNEMPTY_MASK BIT(4) /* RX_FIFO_not_empty */ |
| 35 | #define ZYNQ_QSPI_IXR_TXOW_MASK BIT(2) /* TX_FIFO_not_full */ |
Jagan Teki | 9cf2ffb | 2015-10-22 21:06:37 +0530 | [diff] [blame] | 36 | #define ZYNQ_QSPI_IXR_ALL_MASK GENMASK(6, 0) /* All IXR bits */ |
Jagan Teki | 736b4df | 2015-10-22 20:40:16 +0530 | [diff] [blame] | 37 | #define ZYNQ_QSPI_ENR_SPI_EN_MASK BIT(0) /* SPI Enable */ |
Nathan Rossi | 04a44d3 | 2015-12-09 00:44:40 +1000 | [diff] [blame] | 38 | #define ZYNQ_QSPI_LQSPICFG_LQMODE_MASK BIT(31) /* Linear QSPI Mode */ |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 39 | |
| 40 | /* zynq qspi Transmit Data Register */ |
| 41 | #define ZYNQ_QSPI_TXD_00_00_OFFSET 0x1C /* Transmit 4-byte inst */ |
| 42 | #define ZYNQ_QSPI_TXD_00_01_OFFSET 0x80 /* Transmit 1-byte inst */ |
| 43 | #define ZYNQ_QSPI_TXD_00_10_OFFSET 0x84 /* Transmit 2-byte inst */ |
| 44 | #define ZYNQ_QSPI_TXD_00_11_OFFSET 0x88 /* Transmit 3-byte inst */ |
| 45 | |
| 46 | #define ZYNQ_QSPI_TXFIFO_THRESHOLD 1 /* Tx FIFO threshold level*/ |
| 47 | #define ZYNQ_QSPI_RXFIFO_THRESHOLD 32 /* Rx FIFO threshold level */ |
| 48 | |
| 49 | #define ZYNQ_QSPI_CR_BAUD_MAX 8 /* Baud rate divisor max val */ |
| 50 | #define ZYNQ_QSPI_CR_BAUD_SHIFT 3 /* Baud rate divisor shift */ |
| 51 | #define ZYNQ_QSPI_CR_SS_SHIFT 10 /* Slave select shift */ |
| 52 | |
Siva Durga Prasad Paladugu | 3067186 | 2022-01-30 22:22:39 -0700 | [diff] [blame] | 53 | #define ZYNQ_QSPI_MAX_BAUD_RATE 0x7 |
| 54 | #define ZYNQ_QSPI_DEFAULT_BAUD_RATE 0x2 |
| 55 | |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 56 | #define ZYNQ_QSPI_FIFO_DEPTH 63 |
Ashok Reddy Soma | f44bd3b | 2020-05-18 01:11:00 -0600 | [diff] [blame] | 57 | #define ZYNQ_QSPI_WAIT (CONFIG_SYS_HZ / 100) /* 10 ms */ |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 58 | |
| 59 | /* zynq qspi register set */ |
| 60 | struct zynq_qspi_regs { |
| 61 | u32 cr; /* 0x00 */ |
| 62 | u32 isr; /* 0x04 */ |
| 63 | u32 ier; /* 0x08 */ |
| 64 | u32 idr; /* 0x0C */ |
| 65 | u32 imr; /* 0x10 */ |
| 66 | u32 enr; /* 0x14 */ |
| 67 | u32 dr; /* 0x18 */ |
| 68 | u32 txd0r; /* 0x1C */ |
| 69 | u32 drxr; /* 0x20 */ |
| 70 | u32 sicr; /* 0x24 */ |
| 71 | u32 txftr; /* 0x28 */ |
| 72 | u32 rxftr; /* 0x2C */ |
| 73 | u32 gpior; /* 0x30 */ |
| 74 | u32 reserved0[19]; |
| 75 | u32 txd1r; /* 0x80 */ |
| 76 | u32 txd2r; /* 0x84 */ |
| 77 | u32 txd3r; /* 0x88 */ |
Nathan Rossi | 04a44d3 | 2015-12-09 00:44:40 +1000 | [diff] [blame] | 78 | u32 reserved1[5]; |
| 79 | u32 lqspicfg; /* 0xA0 */ |
| 80 | u32 lqspists; /* 0xA4 */ |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | /* zynq qspi platform data */ |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 84 | struct zynq_qspi_plat { |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 85 | struct zynq_qspi_regs *regs; |
| 86 | u32 frequency; /* input frequency */ |
| 87 | u32 speed_hz; |
| 88 | }; |
| 89 | |
| 90 | /* zynq qspi priv */ |
| 91 | struct zynq_qspi_priv { |
| 92 | struct zynq_qspi_regs *regs; |
| 93 | u8 cs; |
| 94 | u8 mode; |
| 95 | u8 fifo_depth; |
| 96 | u32 freq; /* required frequency */ |
Siva Durga Prasad Paladugu | 1acb703 | 2022-07-15 19:31:16 +0530 | [diff] [blame^] | 97 | u32 max_hz; |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 98 | const void *tx_buf; |
| 99 | void *rx_buf; |
| 100 | unsigned len; |
| 101 | int bytes_to_transfer; |
| 102 | int bytes_to_receive; |
| 103 | unsigned int is_inst; |
| 104 | unsigned cs_change:1; |
| 105 | }; |
| 106 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 107 | static int zynq_qspi_of_to_plat(struct udevice *bus) |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 108 | { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 109 | struct zynq_qspi_plat *plat = dev_get_plat(bus); |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 110 | const void *blob = gd->fdt_blob; |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 111 | int node = dev_of_offset(bus); |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 112 | |
| 113 | plat->regs = (struct zynq_qspi_regs *)fdtdec_get_addr(blob, |
| 114 | node, "reg"); |
| 115 | |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 116 | return 0; |
| 117 | } |
| 118 | |
Ashok Reddy Soma | 17fbb59 | 2020-01-28 07:39:04 -0700 | [diff] [blame] | 119 | /** |
| 120 | * zynq_qspi_init_hw - Initialize the hardware |
| 121 | * @priv: Pointer to the zynq_qspi_priv structure |
| 122 | * |
| 123 | * The default settings of the QSPI controller's configurable parameters on |
| 124 | * reset are |
| 125 | * - Master mode |
| 126 | * - Baud rate divisor is set to 2 |
| 127 | * - Threshold value for TX FIFO not full interrupt is set to 1 |
| 128 | * - Flash memory interface mode enabled |
| 129 | * - Size of the word to be transferred as 8 bit |
| 130 | * This function performs the following actions |
| 131 | * - Disable and clear all the interrupts |
| 132 | * - Enable manual slave select |
| 133 | * - Enable auto start |
| 134 | * - Deselect all the chip select lines |
| 135 | * - Set the size of the word to be transferred as 32 bit |
| 136 | * - Set the little endian mode of TX FIFO and |
| 137 | * - Enable the QSPI controller |
| 138 | */ |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 139 | static void zynq_qspi_init_hw(struct zynq_qspi_priv *priv) |
| 140 | { |
| 141 | struct zynq_qspi_regs *regs = priv->regs; |
| 142 | u32 confr; |
| 143 | |
| 144 | /* Disable QSPI */ |
| 145 | writel(~ZYNQ_QSPI_ENR_SPI_EN_MASK, ®s->enr); |
| 146 | |
| 147 | /* Disable Interrupts */ |
| 148 | writel(ZYNQ_QSPI_IXR_ALL_MASK, ®s->idr); |
| 149 | |
| 150 | /* Clear the TX and RX threshold reg */ |
| 151 | writel(ZYNQ_QSPI_TXFIFO_THRESHOLD, ®s->txftr); |
| 152 | writel(ZYNQ_QSPI_RXFIFO_THRESHOLD, ®s->rxftr); |
| 153 | |
| 154 | /* Clear the RX FIFO */ |
| 155 | while (readl(®s->isr) & ZYNQ_QSPI_IXR_RXNEMPTY_MASK) |
| 156 | readl(®s->drxr); |
| 157 | |
| 158 | /* Clear Interrupts */ |
| 159 | writel(ZYNQ_QSPI_IXR_ALL_MASK, ®s->isr); |
| 160 | |
| 161 | /* Manual slave select and Auto start */ |
| 162 | confr = readl(®s->cr); |
| 163 | confr &= ~ZYNQ_QSPI_CR_MSA_MASK; |
| 164 | confr |= ZYNQ_QSPI_CR_IFMODE_MASK | ZYNQ_QSPI_CR_MCS_MASK | |
| 165 | ZYNQ_QSPI_CR_PCS_MASK | ZYNQ_QSPI_CR_FW_MASK | |
| 166 | ZYNQ_QSPI_CR_MSTREN_MASK; |
| 167 | writel(confr, ®s->cr); |
| 168 | |
Nathan Rossi | 04a44d3 | 2015-12-09 00:44:40 +1000 | [diff] [blame] | 169 | /* Disable the LQSPI feature */ |
| 170 | confr = readl(®s->lqspicfg); |
| 171 | confr &= ~ZYNQ_QSPI_LQSPICFG_LQMODE_MASK; |
| 172 | writel(confr, ®s->lqspicfg); |
| 173 | |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 174 | /* Enable SPI */ |
| 175 | writel(ZYNQ_QSPI_ENR_SPI_EN_MASK, ®s->enr); |
| 176 | } |
| 177 | |
Siva Durga Prasad Paladugu | 1acb703 | 2022-07-15 19:31:16 +0530 | [diff] [blame^] | 178 | static int zynq_qspi_child_pre_probe(struct udevice *bus) |
| 179 | { |
| 180 | struct spi_slave *slave = dev_get_parent_priv(bus); |
| 181 | struct zynq_qspi_priv *priv = dev_get_priv(bus->parent); |
| 182 | |
| 183 | priv->max_hz = slave->max_hz; |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 188 | static int zynq_qspi_probe(struct udevice *bus) |
| 189 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 190 | struct zynq_qspi_plat *plat = dev_get_plat(bus); |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 191 | struct zynq_qspi_priv *priv = dev_get_priv(bus); |
T Karthik Reddy | ea836be | 2020-02-04 05:47:45 -0700 | [diff] [blame] | 192 | struct clk clk; |
| 193 | unsigned long clock; |
| 194 | int ret; |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 195 | |
| 196 | priv->regs = plat->regs; |
| 197 | priv->fifo_depth = ZYNQ_QSPI_FIFO_DEPTH; |
| 198 | |
T Karthik Reddy | ea836be | 2020-02-04 05:47:45 -0700 | [diff] [blame] | 199 | ret = clk_get_by_name(bus, "ref_clk", &clk); |
| 200 | if (ret < 0) { |
| 201 | dev_err(bus, "failed to get clock\n"); |
| 202 | return ret; |
| 203 | } |
| 204 | |
| 205 | clock = clk_get_rate(&clk); |
| 206 | if (IS_ERR_VALUE(clock)) { |
| 207 | dev_err(bus, "failed to get rate\n"); |
| 208 | return clock; |
| 209 | } |
| 210 | |
| 211 | ret = clk_enable(&clk); |
Michal Simek | 9b7aac7 | 2021-02-09 15:28:15 +0100 | [diff] [blame] | 212 | if (ret) { |
T Karthik Reddy | ea836be | 2020-02-04 05:47:45 -0700 | [diff] [blame] | 213 | dev_err(bus, "failed to enable clock\n"); |
| 214 | return ret; |
| 215 | } |
| 216 | |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 217 | /* init the zynq spi hw */ |
| 218 | zynq_qspi_init_hw(priv); |
| 219 | |
T Karthik Reddy | ea836be | 2020-02-04 05:47:45 -0700 | [diff] [blame] | 220 | plat->frequency = clock; |
| 221 | plat->speed_hz = plat->frequency / 2; |
| 222 | |
| 223 | debug("%s: max-frequency=%d\n", __func__, plat->speed_hz); |
| 224 | |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 225 | return 0; |
| 226 | } |
| 227 | |
Ashok Reddy Soma | 17fbb59 | 2020-01-28 07:39:04 -0700 | [diff] [blame] | 228 | /** |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 229 | * zynq_qspi_read_data - Copy data to RX buffer |
Ashok Reddy Soma | 17fbb59 | 2020-01-28 07:39:04 -0700 | [diff] [blame] | 230 | * @priv: Pointer to the zynq_qspi_priv structure |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 231 | * @data: The 32 bit variable where data is stored |
| 232 | * @size: Number of bytes to be copied from data to RX buffer |
| 233 | */ |
| 234 | static void zynq_qspi_read_data(struct zynq_qspi_priv *priv, u32 data, u8 size) |
| 235 | { |
| 236 | u8 byte3; |
| 237 | |
| 238 | debug("%s: data 0x%04x rx_buf addr: 0x%08x size %d\n", __func__ , |
| 239 | data, (unsigned)(priv->rx_buf), size); |
| 240 | |
| 241 | if (priv->rx_buf) { |
| 242 | switch (size) { |
| 243 | case 1: |
| 244 | *((u8 *)priv->rx_buf) = data; |
| 245 | priv->rx_buf += 1; |
| 246 | break; |
| 247 | case 2: |
Siva Durga Prasad Paladugu | 255537b | 2022-01-30 22:22:37 -0700 | [diff] [blame] | 248 | *((u8 *)priv->rx_buf) = data; |
| 249 | priv->rx_buf += 1; |
| 250 | *((u8 *)priv->rx_buf) = (u8)(data >> 8); |
| 251 | priv->rx_buf += 1; |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 252 | break; |
| 253 | case 3: |
Siva Durga Prasad Paladugu | 255537b | 2022-01-30 22:22:37 -0700 | [diff] [blame] | 254 | *((u8 *)priv->rx_buf) = data; |
| 255 | priv->rx_buf += 1; |
| 256 | *((u8 *)priv->rx_buf) = (u8)(data >> 8); |
| 257 | priv->rx_buf += 1; |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 258 | byte3 = (u8)(data >> 16); |
| 259 | *((u8 *)priv->rx_buf) = byte3; |
| 260 | priv->rx_buf += 1; |
| 261 | break; |
| 262 | case 4: |
| 263 | /* Can not assume word aligned buffer */ |
| 264 | memcpy(priv->rx_buf, &data, size); |
| 265 | priv->rx_buf += 4; |
| 266 | break; |
| 267 | default: |
| 268 | /* This will never execute */ |
| 269 | break; |
| 270 | } |
| 271 | } |
| 272 | priv->bytes_to_receive -= size; |
| 273 | if (priv->bytes_to_receive < 0) |
| 274 | priv->bytes_to_receive = 0; |
| 275 | } |
| 276 | |
Ashok Reddy Soma | 17fbb59 | 2020-01-28 07:39:04 -0700 | [diff] [blame] | 277 | /** |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 278 | * zynq_qspi_write_data - Copy data from TX buffer |
Ashok Reddy Soma | 17fbb59 | 2020-01-28 07:39:04 -0700 | [diff] [blame] | 279 | * @priv: Pointer to the zynq_qspi_priv structure |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 280 | * @data: Pointer to the 32 bit variable where data is to be copied |
| 281 | * @size: Number of bytes to be copied from TX buffer to data |
| 282 | */ |
| 283 | static void zynq_qspi_write_data(struct zynq_qspi_priv *priv, |
| 284 | u32 *data, u8 size) |
| 285 | { |
| 286 | if (priv->tx_buf) { |
| 287 | switch (size) { |
| 288 | case 1: |
| 289 | *data = *((u8 *)priv->tx_buf); |
| 290 | priv->tx_buf += 1; |
| 291 | *data |= 0xFFFFFF00; |
| 292 | break; |
| 293 | case 2: |
Siva Durga Prasad Paladugu | a5a387a | 2022-01-30 22:22:38 -0700 | [diff] [blame] | 294 | *data = *((u8 *)priv->tx_buf); |
| 295 | priv->tx_buf += 1; |
| 296 | *data |= (*((u8 *)priv->tx_buf) << 8); |
| 297 | priv->tx_buf += 1; |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 298 | *data |= 0xFFFF0000; |
| 299 | break; |
| 300 | case 3: |
Siva Durga Prasad Paladugu | a5a387a | 2022-01-30 22:22:38 -0700 | [diff] [blame] | 301 | *data = *((u8 *)priv->tx_buf); |
| 302 | priv->tx_buf += 1; |
| 303 | *data |= (*((u8 *)priv->tx_buf) << 8); |
| 304 | priv->tx_buf += 1; |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 305 | *data |= (*((u8 *)priv->tx_buf) << 16); |
| 306 | priv->tx_buf += 1; |
| 307 | *data |= 0xFF000000; |
| 308 | break; |
| 309 | case 4: |
| 310 | /* Can not assume word aligned buffer */ |
| 311 | memcpy(data, priv->tx_buf, size); |
| 312 | priv->tx_buf += 4; |
| 313 | break; |
| 314 | default: |
| 315 | /* This will never execute */ |
| 316 | break; |
| 317 | } |
| 318 | } else { |
| 319 | *data = 0; |
| 320 | } |
| 321 | |
| 322 | debug("%s: data 0x%08x tx_buf addr: 0x%08x size %d\n", __func__, |
| 323 | *data, (u32)priv->tx_buf, size); |
| 324 | |
| 325 | priv->bytes_to_transfer -= size; |
| 326 | if (priv->bytes_to_transfer < 0) |
| 327 | priv->bytes_to_transfer = 0; |
| 328 | } |
| 329 | |
Ashok Reddy Soma | 17fbb59 | 2020-01-28 07:39:04 -0700 | [diff] [blame] | 330 | /** |
| 331 | * zynq_qspi_chipselect - Select or deselect the chip select line |
| 332 | * @priv: Pointer to the zynq_qspi_priv structure |
| 333 | * @is_on: Select(1) or deselect (0) the chip select line |
| 334 | */ |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 335 | static void zynq_qspi_chipselect(struct zynq_qspi_priv *priv, int is_on) |
| 336 | { |
| 337 | u32 confr; |
| 338 | struct zynq_qspi_regs *regs = priv->regs; |
| 339 | |
| 340 | confr = readl(®s->cr); |
| 341 | |
| 342 | if (is_on) { |
| 343 | /* Select the slave */ |
| 344 | confr &= ~ZYNQ_QSPI_CR_SS_MASK; |
| 345 | confr |= (~(1 << priv->cs) << ZYNQ_QSPI_CR_SS_SHIFT) & |
| 346 | ZYNQ_QSPI_CR_SS_MASK; |
| 347 | } else |
| 348 | /* Deselect the slave */ |
| 349 | confr |= ZYNQ_QSPI_CR_SS_MASK; |
| 350 | |
| 351 | writel(confr, ®s->cr); |
| 352 | } |
| 353 | |
Ashok Reddy Soma | 17fbb59 | 2020-01-28 07:39:04 -0700 | [diff] [blame] | 354 | /** |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 355 | * zynq_qspi_fill_tx_fifo - Fills the TX FIFO with as many bytes as possible |
Ashok Reddy Soma | 17fbb59 | 2020-01-28 07:39:04 -0700 | [diff] [blame] | 356 | * @priv: Pointer to the zynq_qspi_priv structure |
| 357 | * @size: Number of bytes to be copied to fifo |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 358 | */ |
| 359 | static void zynq_qspi_fill_tx_fifo(struct zynq_qspi_priv *priv, u32 size) |
| 360 | { |
| 361 | u32 data = 0; |
| 362 | u32 fifocount = 0; |
| 363 | unsigned len, offset; |
| 364 | struct zynq_qspi_regs *regs = priv->regs; |
| 365 | static const unsigned offsets[4] = { |
| 366 | ZYNQ_QSPI_TXD_00_00_OFFSET, ZYNQ_QSPI_TXD_00_01_OFFSET, |
| 367 | ZYNQ_QSPI_TXD_00_10_OFFSET, ZYNQ_QSPI_TXD_00_11_OFFSET }; |
| 368 | |
| 369 | while ((fifocount < size) && |
| 370 | (priv->bytes_to_transfer > 0)) { |
| 371 | if (priv->bytes_to_transfer >= 4) { |
| 372 | if (priv->tx_buf) { |
| 373 | memcpy(&data, priv->tx_buf, 4); |
| 374 | priv->tx_buf += 4; |
| 375 | } else { |
| 376 | data = 0; |
| 377 | } |
| 378 | writel(data, ®s->txd0r); |
| 379 | priv->bytes_to_transfer -= 4; |
| 380 | fifocount++; |
| 381 | } else { |
| 382 | /* Write TXD1, TXD2, TXD3 only if TxFIFO is empty. */ |
| 383 | if (!(readl(®s->isr) |
| 384 | & ZYNQ_QSPI_IXR_TXOW_MASK) && |
| 385 | !priv->rx_buf) |
| 386 | return; |
| 387 | len = priv->bytes_to_transfer; |
| 388 | zynq_qspi_write_data(priv, &data, len); |
| 389 | offset = (priv->rx_buf) ? offsets[0] : offsets[len]; |
| 390 | writel(data, ®s->cr + (offset / 4)); |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | |
Ashok Reddy Soma | 17fbb59 | 2020-01-28 07:39:04 -0700 | [diff] [blame] | 395 | /** |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 396 | * zynq_qspi_irq_poll - Interrupt service routine of the QSPI controller |
Ashok Reddy Soma | 17fbb59 | 2020-01-28 07:39:04 -0700 | [diff] [blame] | 397 | * @priv: Pointer to the zynq_qspi structure |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 398 | * |
| 399 | * This function handles TX empty and Mode Fault interrupts only. |
| 400 | * On TX empty interrupt this function reads the received data from RX FIFO and |
| 401 | * fills the TX FIFO if there is any data remaining to be transferred. |
| 402 | * On Mode Fault interrupt this function indicates that transfer is completed, |
| 403 | * the SPI subsystem will identify the error as the remaining bytes to be |
| 404 | * transferred is non-zero. |
| 405 | * |
| 406 | * returns: 0 for poll timeout |
| 407 | * 1 transfer operation complete |
| 408 | */ |
| 409 | static int zynq_qspi_irq_poll(struct zynq_qspi_priv *priv) |
| 410 | { |
| 411 | struct zynq_qspi_regs *regs = priv->regs; |
| 412 | u32 rxindex = 0; |
| 413 | u32 rxcount; |
| 414 | u32 status, timeout; |
| 415 | |
| 416 | /* Poll until any of the interrupt status bits are set */ |
| 417 | timeout = get_timer(0); |
| 418 | do { |
| 419 | status = readl(®s->isr); |
| 420 | } while ((status == 0) && |
Ashok Reddy Soma | f44bd3b | 2020-05-18 01:11:00 -0600 | [diff] [blame] | 421 | (get_timer(timeout) < ZYNQ_QSPI_WAIT)); |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 422 | |
| 423 | if (status == 0) { |
| 424 | printf("zynq_qspi_irq_poll: Timeout!\n"); |
| 425 | return -ETIMEDOUT; |
| 426 | } |
| 427 | |
| 428 | writel(status, ®s->isr); |
| 429 | |
| 430 | /* Disable all interrupts */ |
| 431 | writel(ZYNQ_QSPI_IXR_ALL_MASK, ®s->idr); |
| 432 | if ((status & ZYNQ_QSPI_IXR_TXOW_MASK) || |
| 433 | (status & ZYNQ_QSPI_IXR_RXNEMPTY_MASK)) { |
| 434 | /* |
| 435 | * This bit is set when Tx FIFO has < THRESHOLD entries. We have |
| 436 | * the THRESHOLD value set to 1, so this bit indicates Tx FIFO |
| 437 | * is empty |
| 438 | */ |
| 439 | rxcount = priv->bytes_to_receive - priv->bytes_to_transfer; |
| 440 | rxcount = (rxcount % 4) ? ((rxcount/4)+1) : (rxcount/4); |
| 441 | while ((rxindex < rxcount) && |
| 442 | (rxindex < ZYNQ_QSPI_RXFIFO_THRESHOLD)) { |
| 443 | /* Read out the data from the RX FIFO */ |
| 444 | u32 data; |
| 445 | data = readl(®s->drxr); |
| 446 | |
| 447 | if (priv->bytes_to_receive >= 4) { |
| 448 | if (priv->rx_buf) { |
| 449 | memcpy(priv->rx_buf, &data, 4); |
| 450 | priv->rx_buf += 4; |
| 451 | } |
| 452 | priv->bytes_to_receive -= 4; |
| 453 | } else { |
| 454 | zynq_qspi_read_data(priv, data, |
| 455 | priv->bytes_to_receive); |
| 456 | } |
| 457 | rxindex++; |
| 458 | } |
| 459 | |
| 460 | if (priv->bytes_to_transfer) { |
| 461 | /* There is more data to send */ |
| 462 | zynq_qspi_fill_tx_fifo(priv, |
| 463 | ZYNQ_QSPI_RXFIFO_THRESHOLD); |
| 464 | |
| 465 | writel(ZYNQ_QSPI_IXR_ALL_MASK, ®s->ier); |
| 466 | } else { |
| 467 | /* |
| 468 | * If transfer and receive is completed then only send |
| 469 | * complete signal |
| 470 | */ |
| 471 | if (!priv->bytes_to_receive) { |
| 472 | /* return operation complete */ |
| 473 | writel(ZYNQ_QSPI_IXR_ALL_MASK, |
| 474 | ®s->idr); |
| 475 | return 1; |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | return 0; |
| 481 | } |
| 482 | |
Ashok Reddy Soma | 17fbb59 | 2020-01-28 07:39:04 -0700 | [diff] [blame] | 483 | /** |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 484 | * zynq_qspi_start_transfer - Initiates the QSPI transfer |
Ashok Reddy Soma | 17fbb59 | 2020-01-28 07:39:04 -0700 | [diff] [blame] | 485 | * @priv: Pointer to the zynq_qspi_priv structure |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 486 | * |
| 487 | * This function fills the TX FIFO, starts the QSPI transfer, and waits for the |
| 488 | * transfer to be completed. |
| 489 | * |
| 490 | * returns: Number of bytes transferred in the last transfer |
| 491 | */ |
| 492 | static int zynq_qspi_start_transfer(struct zynq_qspi_priv *priv) |
| 493 | { |
| 494 | u32 data = 0; |
| 495 | struct zynq_qspi_regs *regs = priv->regs; |
| 496 | |
| 497 | debug("%s: qspi: 0x%08x transfer: 0x%08x len: %d\n", __func__, |
| 498 | (u32)priv, (u32)priv, priv->len); |
| 499 | |
| 500 | priv->bytes_to_transfer = priv->len; |
| 501 | priv->bytes_to_receive = priv->len; |
| 502 | |
| 503 | if (priv->len < 4) |
| 504 | zynq_qspi_fill_tx_fifo(priv, priv->len); |
| 505 | else |
| 506 | zynq_qspi_fill_tx_fifo(priv, priv->fifo_depth); |
| 507 | |
| 508 | writel(ZYNQ_QSPI_IXR_ALL_MASK, ®s->ier); |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 509 | |
| 510 | /* wait for completion */ |
| 511 | do { |
| 512 | data = zynq_qspi_irq_poll(priv); |
| 513 | } while (data == 0); |
| 514 | |
| 515 | return (priv->len) - (priv->bytes_to_transfer); |
| 516 | } |
| 517 | |
| 518 | static int zynq_qspi_transfer(struct zynq_qspi_priv *priv) |
| 519 | { |
| 520 | unsigned cs_change = 1; |
| 521 | int status = 0; |
| 522 | |
| 523 | while (1) { |
| 524 | /* Select the chip if required */ |
| 525 | if (cs_change) |
| 526 | zynq_qspi_chipselect(priv, 1); |
| 527 | |
| 528 | cs_change = priv->cs_change; |
| 529 | |
| 530 | if (!priv->tx_buf && !priv->rx_buf && priv->len) { |
| 531 | status = -1; |
| 532 | break; |
| 533 | } |
| 534 | |
| 535 | /* Request the transfer */ |
| 536 | if (priv->len) { |
| 537 | status = zynq_qspi_start_transfer(priv); |
| 538 | priv->is_inst = 0; |
| 539 | } |
| 540 | |
| 541 | if (status != priv->len) { |
| 542 | if (status > 0) |
| 543 | status = -EMSGSIZE; |
| 544 | debug("zynq_qspi_transfer:%d len:%d\n", |
| 545 | status, priv->len); |
| 546 | break; |
| 547 | } |
| 548 | status = 0; |
| 549 | |
| 550 | if (cs_change) |
| 551 | /* Deselect the chip */ |
| 552 | zynq_qspi_chipselect(priv, 0); |
| 553 | |
| 554 | break; |
| 555 | } |
| 556 | |
Vipul Kumar | 240cd75 | 2018-06-25 14:13:57 +0530 | [diff] [blame] | 557 | return status; |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | static int zynq_qspi_claim_bus(struct udevice *dev) |
| 561 | { |
| 562 | struct udevice *bus = dev->parent; |
| 563 | struct zynq_qspi_priv *priv = dev_get_priv(bus); |
| 564 | struct zynq_qspi_regs *regs = priv->regs; |
| 565 | |
| 566 | writel(ZYNQ_QSPI_ENR_SPI_EN_MASK, ®s->enr); |
| 567 | |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | static int zynq_qspi_release_bus(struct udevice *dev) |
| 572 | { |
| 573 | struct udevice *bus = dev->parent; |
| 574 | struct zynq_qspi_priv *priv = dev_get_priv(bus); |
| 575 | struct zynq_qspi_regs *regs = priv->regs; |
| 576 | |
| 577 | writel(~ZYNQ_QSPI_ENR_SPI_EN_MASK, ®s->enr); |
| 578 | |
| 579 | return 0; |
| 580 | } |
| 581 | |
| 582 | static int zynq_qspi_xfer(struct udevice *dev, unsigned int bitlen, |
| 583 | const void *dout, void *din, unsigned long flags) |
| 584 | { |
| 585 | struct udevice *bus = dev->parent; |
| 586 | struct zynq_qspi_priv *priv = dev_get_priv(bus); |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 587 | struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev); |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 588 | |
| 589 | priv->cs = slave_plat->cs; |
| 590 | priv->tx_buf = dout; |
| 591 | priv->rx_buf = din; |
| 592 | priv->len = bitlen / 8; |
| 593 | |
Jagan Teki | e5e0d68 | 2015-10-25 09:31:54 +0530 | [diff] [blame] | 594 | debug("zynq_qspi_xfer: bus:%i cs:%i bitlen:%i len:%i flags:%lx\n", |
Simon Glass | 8b85dfc | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 595 | dev_seq(bus), slave_plat->cs, bitlen, priv->len, flags); |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 596 | |
| 597 | /* |
| 598 | * Festering sore. |
| 599 | * Assume that the beginning of a transfer with bits to |
| 600 | * transmit must contain a device command. |
| 601 | */ |
| 602 | if (dout && flags & SPI_XFER_BEGIN) |
| 603 | priv->is_inst = 1; |
| 604 | else |
| 605 | priv->is_inst = 0; |
| 606 | |
| 607 | if (flags & SPI_XFER_END) |
| 608 | priv->cs_change = 1; |
| 609 | else |
| 610 | priv->cs_change = 0; |
| 611 | |
| 612 | zynq_qspi_transfer(priv); |
| 613 | |
| 614 | return 0; |
| 615 | } |
| 616 | |
| 617 | static int zynq_qspi_set_speed(struct udevice *bus, uint speed) |
| 618 | { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 619 | struct zynq_qspi_plat *plat = dev_get_plat(bus); |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 620 | struct zynq_qspi_priv *priv = dev_get_priv(bus); |
| 621 | struct zynq_qspi_regs *regs = priv->regs; |
| 622 | uint32_t confr; |
| 623 | u8 baud_rate_val = 0; |
| 624 | |
| 625 | if (speed > plat->frequency) |
| 626 | speed = plat->frequency; |
| 627 | |
| 628 | /* Set the clock frequency */ |
| 629 | confr = readl(®s->cr); |
| 630 | if (speed == 0) { |
| 631 | /* Set baudrate x8, if the freq is 0 */ |
| 632 | baud_rate_val = 0x2; |
| 633 | } else if (plat->speed_hz != speed) { |
| 634 | while ((baud_rate_val < ZYNQ_QSPI_CR_BAUD_MAX) && |
| 635 | ((plat->frequency / |
| 636 | (2 << baud_rate_val)) > speed)) |
| 637 | baud_rate_val++; |
| 638 | |
Siva Durga Prasad Paladugu | 3067186 | 2022-01-30 22:22:39 -0700 | [diff] [blame] | 639 | if (baud_rate_val > ZYNQ_QSPI_MAX_BAUD_RATE) |
| 640 | baud_rate_val = ZYNQ_QSPI_DEFAULT_BAUD_RATE; |
| 641 | |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 642 | plat->speed_hz = speed / (2 << baud_rate_val); |
| 643 | } |
| 644 | confr &= ~ZYNQ_QSPI_CR_BAUD_MASK; |
| 645 | confr |= (baud_rate_val << ZYNQ_QSPI_CR_BAUD_SHIFT); |
| 646 | |
| 647 | writel(confr, ®s->cr); |
| 648 | priv->freq = speed; |
| 649 | |
Jagan Teki | e5e0d68 | 2015-10-25 09:31:54 +0530 | [diff] [blame] | 650 | debug("%s: regs=%p, speed=%d\n", __func__, priv->regs, priv->freq); |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 651 | |
| 652 | return 0; |
| 653 | } |
| 654 | |
| 655 | static int zynq_qspi_set_mode(struct udevice *bus, uint mode) |
| 656 | { |
| 657 | struct zynq_qspi_priv *priv = dev_get_priv(bus); |
| 658 | struct zynq_qspi_regs *regs = priv->regs; |
| 659 | uint32_t confr; |
| 660 | |
| 661 | /* Set the SPI Clock phase and polarities */ |
| 662 | confr = readl(®s->cr); |
| 663 | confr &= ~(ZYNQ_QSPI_CR_CPHA_MASK | ZYNQ_QSPI_CR_CPOL_MASK); |
| 664 | |
Jagan Teki | 2775e91 | 2015-09-08 01:39:44 +0530 | [diff] [blame] | 665 | if (mode & SPI_CPHA) |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 666 | confr |= ZYNQ_QSPI_CR_CPHA_MASK; |
Jagan Teki | 2775e91 | 2015-09-08 01:39:44 +0530 | [diff] [blame] | 667 | if (mode & SPI_CPOL) |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 668 | confr |= ZYNQ_QSPI_CR_CPOL_MASK; |
| 669 | |
| 670 | writel(confr, ®s->cr); |
| 671 | priv->mode = mode; |
| 672 | |
Jagan Teki | e5e0d68 | 2015-10-25 09:31:54 +0530 | [diff] [blame] | 673 | debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode); |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 674 | |
| 675 | return 0; |
| 676 | } |
| 677 | |
T Karthik Reddy | bab0c02 | 2022-01-30 22:22:40 -0700 | [diff] [blame] | 678 | static int zynq_qspi_exec_op(struct spi_slave *slave, |
| 679 | const struct spi_mem_op *op) |
| 680 | { |
| 681 | int op_len, pos = 0, ret, i; |
| 682 | unsigned int flag = 0; |
| 683 | const u8 *tx_buf = NULL; |
| 684 | u8 *rx_buf = NULL; |
| 685 | |
| 686 | if (op->data.nbytes) { |
| 687 | if (op->data.dir == SPI_MEM_DATA_IN) |
| 688 | rx_buf = op->data.buf.in; |
| 689 | else |
| 690 | tx_buf = op->data.buf.out; |
| 691 | } |
| 692 | |
| 693 | op_len = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes; |
| 694 | |
| 695 | u8 op_buf[op_len]; |
| 696 | |
| 697 | op_buf[pos++] = op->cmd.opcode; |
| 698 | |
| 699 | if (op->addr.nbytes) { |
| 700 | for (i = 0; i < op->addr.nbytes; i++) |
| 701 | op_buf[pos + i] = op->addr.val >> |
| 702 | (8 * (op->addr.nbytes - i - 1)); |
| 703 | |
| 704 | pos += op->addr.nbytes; |
| 705 | } |
| 706 | |
| 707 | if (op->dummy.nbytes) |
| 708 | memset(op_buf + pos, 0xff, op->dummy.nbytes); |
| 709 | |
| 710 | /* 1st transfer: opcode + address + dummy cycles */ |
| 711 | /* Make sure to set END bit if no tx or rx data messages follow */ |
| 712 | if (!tx_buf && !rx_buf) |
| 713 | flag |= SPI_XFER_END; |
| 714 | |
| 715 | ret = zynq_qspi_xfer(slave->dev, op_len * 8, op_buf, NULL, |
| 716 | flag | SPI_XFER_BEGIN); |
| 717 | if (ret) |
| 718 | return ret; |
| 719 | |
| 720 | /* 2nd transfer: rx or tx data path */ |
| 721 | if (tx_buf || rx_buf) { |
| 722 | ret = zynq_qspi_xfer(slave->dev, op->data.nbytes * 8, tx_buf, |
| 723 | rx_buf, flag | SPI_XFER_END); |
| 724 | if (ret) |
| 725 | return ret; |
| 726 | } |
| 727 | |
| 728 | spi_release_bus(slave); |
| 729 | |
| 730 | return 0; |
| 731 | } |
| 732 | |
| 733 | static const struct spi_controller_mem_ops zynq_qspi_mem_ops = { |
| 734 | .exec_op = zynq_qspi_exec_op, |
| 735 | }; |
| 736 | |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 737 | static const struct dm_spi_ops zynq_qspi_ops = { |
| 738 | .claim_bus = zynq_qspi_claim_bus, |
| 739 | .release_bus = zynq_qspi_release_bus, |
| 740 | .xfer = zynq_qspi_xfer, |
| 741 | .set_speed = zynq_qspi_set_speed, |
| 742 | .set_mode = zynq_qspi_set_mode, |
T Karthik Reddy | bab0c02 | 2022-01-30 22:22:40 -0700 | [diff] [blame] | 743 | .mem_ops = &zynq_qspi_mem_ops, |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 744 | }; |
| 745 | |
| 746 | static const struct udevice_id zynq_qspi_ids[] = { |
| 747 | { .compatible = "xlnx,zynq-qspi-1.0" }, |
| 748 | { } |
| 749 | }; |
| 750 | |
| 751 | U_BOOT_DRIVER(zynq_qspi) = { |
| 752 | .name = "zynq_qspi", |
| 753 | .id = UCLASS_SPI, |
| 754 | .of_match = zynq_qspi_ids, |
| 755 | .ops = &zynq_qspi_ops, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 756 | .of_to_plat = zynq_qspi_of_to_plat, |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 757 | .plat_auto = sizeof(struct zynq_qspi_plat), |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 758 | .priv_auto = sizeof(struct zynq_qspi_priv), |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 759 | .probe = zynq_qspi_probe, |
Siva Durga Prasad Paladugu | 1acb703 | 2022-07-15 19:31:16 +0530 | [diff] [blame^] | 760 | .child_pre_probe = zynq_qspi_child_pre_probe, |
Jagan Teki | 46d0a99 | 2015-08-17 18:38:06 +0530 | [diff] [blame] | 761 | }; |