Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * (C) Copyright 2018 Xilinx |
| 4 | * |
| 5 | * Xilinx ZynqMP Generic Quad-SPI(QSPI) controller driver(master mode only) |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Simon Glass | 1eb69ae | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 9 | #include <cpu_func.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 11 | #include <asm/arch/sys_proto.h> |
Simon Glass | 90526e9 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 12 | #include <asm/cache.h> |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 13 | #include <asm/io.h> |
| 14 | #include <clk.h> |
| 15 | #include <dm.h> |
| 16 | #include <malloc.h> |
| 17 | #include <memalign.h> |
| 18 | #include <spi.h> |
Brandon Maier | f1fd79a | 2021-01-20 10:39:46 -0600 | [diff] [blame] | 19 | #include <spi-mem.h> |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 20 | #include <ubi_uboot.h> |
| 21 | #include <wait_bit.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 22 | #include <dm/device_compat.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 23 | #include <linux/bitops.h> |
Simon Glass | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 24 | #include <linux/err.h> |
Ashok Reddy Soma | a3d4bfb | 2022-08-25 06:59:04 -0600 | [diff] [blame^] | 25 | #include <linux/sizes.h> |
Ashok Reddy Soma | f4f1b65 | 2022-08-25 06:59:01 -0600 | [diff] [blame] | 26 | #include <zynqmp_firmware.h> |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 27 | |
| 28 | #define GQSPI_GFIFO_STRT_MODE_MASK BIT(29) |
| 29 | #define GQSPI_CONFIG_MODE_EN_MASK (3 << 30) |
| 30 | #define GQSPI_CONFIG_DMA_MODE (2 << 30) |
| 31 | #define GQSPI_CONFIG_CPHA_MASK BIT(2) |
| 32 | #define GQSPI_CONFIG_CPOL_MASK BIT(1) |
| 33 | |
| 34 | /* |
| 35 | * QSPI Interrupt Registers bit Masks |
| 36 | * |
| 37 | * All the four interrupt registers (Status/Mask/Enable/Disable) have the same |
| 38 | * bit definitions. |
| 39 | */ |
| 40 | #define GQSPI_IXR_TXNFULL_MASK 0x00000004 /* QSPI TX FIFO Overflow */ |
| 41 | #define GQSPI_IXR_TXFULL_MASK 0x00000008 /* QSPI TX FIFO is full */ |
Ashok Reddy Soma | 4f9d255 | 2021-10-19 19:43:00 +0530 | [diff] [blame] | 42 | #define GQSPI_IXR_TXFIFOEMPTY_MASK 0x00000100 /* QSPI TX FIFO is Empty */ |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 43 | #define GQSPI_IXR_RXNEMTY_MASK 0x00000010 /* QSPI RX FIFO Not Empty */ |
| 44 | #define GQSPI_IXR_GFEMTY_MASK 0x00000080 /* QSPI Generic FIFO Empty */ |
Ashok Reddy Soma | 2ffa653 | 2021-05-25 06:36:27 -0600 | [diff] [blame] | 45 | #define GQSPI_IXR_GFNFULL_MASK 0x00000200 /* QSPI GENFIFO not full */ |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 46 | #define GQSPI_IXR_ALL_MASK (GQSPI_IXR_TXNFULL_MASK | \ |
| 47 | GQSPI_IXR_RXNEMTY_MASK) |
| 48 | |
| 49 | /* |
| 50 | * QSPI Enable Register bit Masks |
| 51 | * |
| 52 | * This register is used to enable or disable the QSPI controller |
| 53 | */ |
| 54 | #define GQSPI_ENABLE_ENABLE_MASK 0x00000001 /* QSPI Enable Bit Mask */ |
| 55 | |
| 56 | #define GQSPI_GFIFO_LOW_BUS BIT(14) |
| 57 | #define GQSPI_GFIFO_CS_LOWER BIT(12) |
| 58 | #define GQSPI_GFIFO_UP_BUS BIT(15) |
| 59 | #define GQSPI_GFIFO_CS_UPPER BIT(13) |
| 60 | #define GQSPI_SPI_MODE_QSPI (3 << 10) |
| 61 | #define GQSPI_SPI_MODE_SPI BIT(10) |
| 62 | #define GQSPI_SPI_MODE_DUAL_SPI (2 << 10) |
| 63 | #define GQSPI_IMD_DATA_CS_ASSERT 5 |
| 64 | #define GQSPI_IMD_DATA_CS_DEASSERT 5 |
| 65 | #define GQSPI_GFIFO_TX BIT(16) |
| 66 | #define GQSPI_GFIFO_RX BIT(17) |
| 67 | #define GQSPI_GFIFO_STRIPE_MASK BIT(18) |
| 68 | #define GQSPI_GFIFO_IMD_MASK 0xFF |
| 69 | #define GQSPI_GFIFO_EXP_MASK BIT(9) |
| 70 | #define GQSPI_GFIFO_DATA_XFR_MASK BIT(8) |
| 71 | #define GQSPI_STRT_GEN_FIFO BIT(28) |
| 72 | #define GQSPI_GEN_FIFO_STRT_MOD BIT(29) |
| 73 | #define GQSPI_GFIFO_WP_HOLD BIT(19) |
| 74 | #define GQSPI_BAUD_DIV_MASK (7 << 3) |
| 75 | #define GQSPI_DFLT_BAUD_RATE_DIV BIT(3) |
| 76 | #define GQSPI_GFIFO_ALL_INT_MASK 0xFBE |
| 77 | #define GQSPI_DMA_DST_I_STS_DONE BIT(1) |
| 78 | #define GQSPI_DMA_DST_I_STS_MASK 0xFE |
| 79 | #define MODEBITS 0x6 |
| 80 | |
| 81 | #define GQSPI_GFIFO_SELECT BIT(0) |
| 82 | #define GQSPI_FIFO_THRESHOLD 1 |
Ashok Reddy Soma | 020b353 | 2021-08-20 07:43:17 -0600 | [diff] [blame] | 83 | #define GQSPI_GENFIFO_THRESHOLD 31 |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 84 | |
| 85 | #define SPI_XFER_ON_BOTH 0 |
| 86 | #define SPI_XFER_ON_LOWER 1 |
| 87 | #define SPI_XFER_ON_UPPER 2 |
| 88 | |
| 89 | #define GQSPI_DMA_ALIGN 0x4 |
| 90 | #define GQSPI_MAX_BAUD_RATE_VAL 7 |
| 91 | #define GQSPI_DFLT_BAUD_RATE_VAL 2 |
| 92 | |
| 93 | #define GQSPI_TIMEOUT 100000000 |
| 94 | |
| 95 | #define GQSPI_BAUD_DIV_SHIFT 2 |
| 96 | #define GQSPI_LPBK_DLY_ADJ_LPBK_SHIFT 5 |
| 97 | #define GQSPI_LPBK_DLY_ADJ_DLY_1 0x2 |
| 98 | #define GQSPI_LPBK_DLY_ADJ_DLY_1_SHIFT 3 |
| 99 | #define GQSPI_LPBK_DLY_ADJ_DLY_0 0x3 |
| 100 | #define GQSPI_USE_DATA_DLY 0x1 |
| 101 | #define GQSPI_USE_DATA_DLY_SHIFT 31 |
| 102 | #define GQSPI_DATA_DLY_ADJ_VALUE 0x2 |
| 103 | #define GQSPI_DATA_DLY_ADJ_SHIFT 28 |
| 104 | #define TAP_DLY_BYPASS_LQSPI_RX_VALUE 0x1 |
| 105 | #define TAP_DLY_BYPASS_LQSPI_RX_SHIFT 2 |
| 106 | #define GQSPI_DATA_DLY_ADJ_OFST 0x000001F8 |
Ashok Reddy Soma | f4f1b65 | 2022-08-25 06:59:01 -0600 | [diff] [blame] | 107 | #define IOU_TAPDLY_BYPASS_OFST !IS_ENABLED(CONFIG_ARCH_VERSAL) ? \ |
| 108 | 0xFF180390 : 0xF103003C |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 109 | #define GQSPI_LPBK_DLY_ADJ_LPBK_MASK 0x00000020 |
Ashok Reddy Soma | f4f1b65 | 2022-08-25 06:59:01 -0600 | [diff] [blame] | 110 | #define GQSPI_FREQ_37_5MHZ 37500000 |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 111 | #define GQSPI_FREQ_40MHZ 40000000 |
| 112 | #define GQSPI_FREQ_100MHZ 100000000 |
| 113 | #define GQSPI_FREQ_150MHZ 150000000 |
| 114 | #define IOU_TAPDLY_BYPASS_MASK 0x7 |
| 115 | |
| 116 | #define GQSPI_REG_OFFSET 0x100 |
| 117 | #define GQSPI_DMA_REG_OFFSET 0x800 |
| 118 | |
| 119 | /* QSPI register offsets */ |
| 120 | struct zynqmp_qspi_regs { |
| 121 | u32 confr; /* 0x00 */ |
| 122 | u32 isr; /* 0x04 */ |
| 123 | u32 ier; /* 0x08 */ |
| 124 | u32 idisr; /* 0x0C */ |
| 125 | u32 imaskr; /* 0x10 */ |
| 126 | u32 enbr; /* 0x14 */ |
| 127 | u32 dr; /* 0x18 */ |
| 128 | u32 txd0r; /* 0x1C */ |
| 129 | u32 drxr; /* 0x20 */ |
| 130 | u32 sicr; /* 0x24 */ |
| 131 | u32 txftr; /* 0x28 */ |
| 132 | u32 rxftr; /* 0x2C */ |
| 133 | u32 gpior; /* 0x30 */ |
| 134 | u32 reserved0; /* 0x34 */ |
| 135 | u32 lpbkdly; /* 0x38 */ |
| 136 | u32 reserved1; /* 0x3C */ |
| 137 | u32 genfifo; /* 0x40 */ |
| 138 | u32 gqspisel; /* 0x44 */ |
| 139 | u32 reserved2; /* 0x48 */ |
| 140 | u32 gqfifoctrl; /* 0x4C */ |
| 141 | u32 gqfthr; /* 0x50 */ |
| 142 | u32 gqpollcfg; /* 0x54 */ |
| 143 | u32 gqpollto; /* 0x58 */ |
| 144 | u32 gqxfersts; /* 0x5C */ |
| 145 | u32 gqfifosnap; /* 0x60 */ |
| 146 | u32 gqrxcpy; /* 0x64 */ |
| 147 | u32 reserved3[36]; /* 0x68 */ |
| 148 | u32 gqspidlyadj; /* 0xF8 */ |
| 149 | }; |
| 150 | |
| 151 | struct zynqmp_qspi_dma_regs { |
| 152 | u32 dmadst; /* 0x00 */ |
| 153 | u32 dmasize; /* 0x04 */ |
| 154 | u32 dmasts; /* 0x08 */ |
| 155 | u32 dmactrl; /* 0x0C */ |
| 156 | u32 reserved0; /* 0x10 */ |
| 157 | u32 dmaisr; /* 0x14 */ |
| 158 | u32 dmaier; /* 0x18 */ |
| 159 | u32 dmaidr; /* 0x1C */ |
| 160 | u32 dmaimr; /* 0x20 */ |
| 161 | u32 dmactrl2; /* 0x24 */ |
| 162 | u32 dmadstmsb; /* 0x28 */ |
| 163 | }; |
| 164 | |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 165 | struct zynqmp_qspi_plat { |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 166 | struct zynqmp_qspi_regs *regs; |
| 167 | struct zynqmp_qspi_dma_regs *dma_regs; |
| 168 | u32 frequency; |
| 169 | u32 speed_hz; |
Ashok Reddy Soma | d91b0f4 | 2022-08-25 06:59:03 -0600 | [diff] [blame] | 170 | unsigned int io_mode; |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 171 | }; |
| 172 | |
| 173 | struct zynqmp_qspi_priv { |
| 174 | struct zynqmp_qspi_regs *regs; |
| 175 | struct zynqmp_qspi_dma_regs *dma_regs; |
| 176 | const void *tx_buf; |
| 177 | void *rx_buf; |
| 178 | unsigned int len; |
Ashok Reddy Soma | d91b0f4 | 2022-08-25 06:59:03 -0600 | [diff] [blame] | 179 | unsigned int io_mode; |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 180 | int bytes_to_transfer; |
| 181 | int bytes_to_receive; |
Brandon Maier | f1fd79a | 2021-01-20 10:39:46 -0600 | [diff] [blame] | 182 | const struct spi_mem_op *op; |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 183 | }; |
| 184 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 185 | static int zynqmp_qspi_of_to_plat(struct udevice *bus) |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 186 | { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 187 | struct zynqmp_qspi_plat *plat = dev_get_plat(bus); |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 188 | |
| 189 | debug("%s\n", __func__); |
| 190 | |
Masahiro Yamada | 2548493 | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 191 | plat->regs = (struct zynqmp_qspi_regs *)(dev_read_addr(bus) + |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 192 | GQSPI_REG_OFFSET); |
| 193 | plat->dma_regs = (struct zynqmp_qspi_dma_regs *) |
Masahiro Yamada | 2548493 | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 194 | (dev_read_addr(bus) + GQSPI_DMA_REG_OFFSET); |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 195 | |
Ashok Reddy Soma | d91b0f4 | 2022-08-25 06:59:03 -0600 | [diff] [blame] | 196 | plat->io_mode = dev_read_bool(bus, "has-io-mode"); |
| 197 | |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | static void zynqmp_qspi_init_hw(struct zynqmp_qspi_priv *priv) |
| 202 | { |
| 203 | u32 config_reg; |
| 204 | struct zynqmp_qspi_regs *regs = priv->regs; |
| 205 | |
| 206 | writel(GQSPI_GFIFO_SELECT, ®s->gqspisel); |
| 207 | writel(GQSPI_GFIFO_ALL_INT_MASK, ®s->idisr); |
| 208 | writel(GQSPI_FIFO_THRESHOLD, ®s->txftr); |
| 209 | writel(GQSPI_FIFO_THRESHOLD, ®s->rxftr); |
Ashok Reddy Soma | 020b353 | 2021-08-20 07:43:17 -0600 | [diff] [blame] | 210 | writel(GQSPI_GENFIFO_THRESHOLD, ®s->gqfthr); |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 211 | writel(GQSPI_GFIFO_ALL_INT_MASK, ®s->isr); |
Ashok Reddy Soma | 020b353 | 2021-08-20 07:43:17 -0600 | [diff] [blame] | 212 | writel(~GQSPI_ENABLE_ENABLE_MASK, ®s->enbr); |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 213 | |
| 214 | config_reg = readl(®s->confr); |
| 215 | config_reg &= ~(GQSPI_GFIFO_STRT_MODE_MASK | |
| 216 | GQSPI_CONFIG_MODE_EN_MASK); |
Ashok Reddy Soma | d91b0f4 | 2022-08-25 06:59:03 -0600 | [diff] [blame] | 217 | config_reg |= GQSPI_GFIFO_WP_HOLD | GQSPI_DFLT_BAUD_RATE_DIV; |
| 218 | config_reg |= GQSPI_GFIFO_STRT_MODE_MASK; |
| 219 | if (!priv->io_mode) |
| 220 | config_reg |= GQSPI_CONFIG_DMA_MODE; |
| 221 | |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 222 | writel(config_reg, ®s->confr); |
| 223 | |
| 224 | writel(GQSPI_ENABLE_ENABLE_MASK, ®s->enbr); |
| 225 | } |
| 226 | |
| 227 | static u32 zynqmp_qspi_bus_select(struct zynqmp_qspi_priv *priv) |
| 228 | { |
| 229 | u32 gqspi_fifo_reg = 0; |
| 230 | |
| 231 | gqspi_fifo_reg = GQSPI_GFIFO_LOW_BUS | |
| 232 | GQSPI_GFIFO_CS_LOWER; |
| 233 | |
| 234 | return gqspi_fifo_reg; |
| 235 | } |
| 236 | |
Brandon Maier | f1fd79a | 2021-01-20 10:39:46 -0600 | [diff] [blame] | 237 | static u32 zynqmp_qspi_genfifo_mode(u8 buswidth) |
| 238 | { |
| 239 | switch (buswidth) { |
| 240 | case 1: |
| 241 | return GQSPI_SPI_MODE_SPI; |
| 242 | case 2: |
| 243 | return GQSPI_SPI_MODE_DUAL_SPI; |
| 244 | case 4: |
| 245 | return GQSPI_SPI_MODE_QSPI; |
| 246 | default: |
| 247 | debug("Unsupported bus width %u\n", buswidth); |
| 248 | return GQSPI_SPI_MODE_SPI; |
| 249 | } |
| 250 | } |
| 251 | |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 252 | static void zynqmp_qspi_fill_gen_fifo(struct zynqmp_qspi_priv *priv, |
| 253 | u32 gqspi_fifo_reg) |
| 254 | { |
| 255 | struct zynqmp_qspi_regs *regs = priv->regs; |
Ashok Reddy Soma | 2ffa653 | 2021-05-25 06:36:27 -0600 | [diff] [blame] | 256 | u32 config_reg, ier; |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 257 | int ret = 0; |
| 258 | |
Ashok Reddy Soma | 72022a5 | 2021-08-20 07:43:16 -0600 | [diff] [blame] | 259 | writel(gqspi_fifo_reg, ®s->genfifo); |
| 260 | |
Ashok Reddy Soma | 2ffa653 | 2021-05-25 06:36:27 -0600 | [diff] [blame] | 261 | config_reg = readl(®s->confr); |
| 262 | /* Manual start if needed */ |
| 263 | config_reg |= GQSPI_STRT_GEN_FIFO; |
| 264 | writel(config_reg, ®s->confr); |
| 265 | |
| 266 | /* Enable interrupts */ |
| 267 | ier = readl(®s->ier); |
Ashok Reddy Soma | 72022a5 | 2021-08-20 07:43:16 -0600 | [diff] [blame] | 268 | ier |= GQSPI_IXR_GFEMTY_MASK; |
Ashok Reddy Soma | 2ffa653 | 2021-05-25 06:36:27 -0600 | [diff] [blame] | 269 | writel(ier, ®s->ier); |
| 270 | |
Ashok Reddy Soma | 72022a5 | 2021-08-20 07:43:16 -0600 | [diff] [blame] | 271 | /* Wait until the gen fifo is empty to write the new command */ |
| 272 | ret = wait_for_bit_le32(®s->isr, GQSPI_IXR_GFEMTY_MASK, 1, |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 273 | GQSPI_TIMEOUT, 1); |
| 274 | if (ret) |
| 275 | printf("%s Timeout\n", __func__); |
| 276 | |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | static void zynqmp_qspi_chipselect(struct zynqmp_qspi_priv *priv, int is_on) |
| 280 | { |
| 281 | u32 gqspi_fifo_reg = 0; |
| 282 | |
| 283 | if (is_on) { |
| 284 | gqspi_fifo_reg = zynqmp_qspi_bus_select(priv); |
| 285 | gqspi_fifo_reg |= GQSPI_SPI_MODE_SPI | |
| 286 | GQSPI_IMD_DATA_CS_ASSERT; |
| 287 | } else { |
| 288 | gqspi_fifo_reg = GQSPI_GFIFO_LOW_BUS; |
| 289 | gqspi_fifo_reg |= GQSPI_IMD_DATA_CS_DEASSERT; |
| 290 | } |
| 291 | |
| 292 | debug("GFIFO_CMD_CS: 0x%x\n", gqspi_fifo_reg); |
| 293 | |
| 294 | zynqmp_qspi_fill_gen_fifo(priv, gqspi_fifo_reg); |
| 295 | } |
| 296 | |
| 297 | void zynqmp_qspi_set_tapdelay(struct udevice *bus, u32 baudrateval) |
| 298 | { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 299 | struct zynqmp_qspi_plat *plat = dev_get_plat(bus); |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 300 | struct zynqmp_qspi_priv *priv = dev_get_priv(bus); |
| 301 | struct zynqmp_qspi_regs *regs = priv->regs; |
| 302 | u32 tapdlybypass = 0, lpbkdlyadj = 0, datadlyadj = 0, clk_rate; |
| 303 | u32 reqhz = 0; |
| 304 | |
| 305 | clk_rate = plat->frequency; |
| 306 | reqhz = (clk_rate / (GQSPI_BAUD_DIV_SHIFT << baudrateval)); |
| 307 | |
| 308 | debug("%s, req_hz:%d, clk_rate:%d, baudrateval:%d\n", |
| 309 | __func__, reqhz, clk_rate, baudrateval); |
| 310 | |
Ashok Reddy Soma | f4f1b65 | 2022-08-25 06:59:01 -0600 | [diff] [blame] | 311 | if (!IS_ENABLED(CONFIG_ARCH_VERSAL)) { |
| 312 | if (reqhz <= GQSPI_FREQ_40MHZ) { |
| 313 | tapdlybypass = TAP_DLY_BYPASS_LQSPI_RX_VALUE << |
| 314 | TAP_DLY_BYPASS_LQSPI_RX_SHIFT; |
| 315 | } else if (reqhz <= GQSPI_FREQ_100MHZ) { |
| 316 | tapdlybypass = TAP_DLY_BYPASS_LQSPI_RX_VALUE << |
| 317 | TAP_DLY_BYPASS_LQSPI_RX_SHIFT; |
| 318 | lpbkdlyadj = GQSPI_LPBK_DLY_ADJ_LPBK_MASK; |
| 319 | datadlyadj = (GQSPI_USE_DATA_DLY << |
| 320 | GQSPI_USE_DATA_DLY_SHIFT) | |
| 321 | (GQSPI_DATA_DLY_ADJ_VALUE << |
| 322 | GQSPI_DATA_DLY_ADJ_SHIFT); |
| 323 | } else if (reqhz <= GQSPI_FREQ_150MHZ) { |
| 324 | lpbkdlyadj = GQSPI_LPBK_DLY_ADJ_LPBK_MASK | |
| 325 | GQSPI_LPBK_DLY_ADJ_DLY_0; |
| 326 | } |
| 327 | zynqmp_mmio_write(IOU_TAPDLY_BYPASS_OFST, |
| 328 | IOU_TAPDLY_BYPASS_MASK, tapdlybypass); |
| 329 | } else { |
| 330 | if (reqhz <= GQSPI_FREQ_37_5MHZ) { |
| 331 | tapdlybypass = TAP_DLY_BYPASS_LQSPI_RX_VALUE << |
| 332 | TAP_DLY_BYPASS_LQSPI_RX_SHIFT; |
| 333 | } else if (reqhz <= GQSPI_FREQ_100MHZ) { |
| 334 | tapdlybypass = TAP_DLY_BYPASS_LQSPI_RX_VALUE << |
| 335 | TAP_DLY_BYPASS_LQSPI_RX_SHIFT; |
| 336 | lpbkdlyadj = GQSPI_LPBK_DLY_ADJ_LPBK_MASK; |
| 337 | datadlyadj = GQSPI_USE_DATA_DLY << |
| 338 | GQSPI_USE_DATA_DLY_SHIFT; |
| 339 | } else if (reqhz <= GQSPI_FREQ_150MHZ) { |
| 340 | lpbkdlyadj = GQSPI_LPBK_DLY_ADJ_LPBK_MASK | |
| 341 | (GQSPI_LPBK_DLY_ADJ_DLY_1 << |
| 342 | GQSPI_LPBK_DLY_ADJ_DLY_1_SHIFT); |
| 343 | } |
| 344 | writel(tapdlybypass, IOU_TAPDLY_BYPASS_OFST); |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 345 | } |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 346 | writel(lpbkdlyadj, ®s->lpbkdly); |
| 347 | writel(datadlyadj, ®s->gqspidlyadj); |
| 348 | } |
| 349 | |
| 350 | static int zynqmp_qspi_set_speed(struct udevice *bus, uint speed) |
| 351 | { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 352 | struct zynqmp_qspi_plat *plat = dev_get_plat(bus); |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 353 | struct zynqmp_qspi_priv *priv = dev_get_priv(bus); |
| 354 | struct zynqmp_qspi_regs *regs = priv->regs; |
| 355 | u32 confr; |
| 356 | u8 baud_rate_val = 0; |
| 357 | |
| 358 | debug("%s\n", __func__); |
| 359 | if (speed > plat->frequency) |
| 360 | speed = plat->frequency; |
| 361 | |
Brandon Maier | d9aa19e | 2021-01-20 14:28:30 -0600 | [diff] [blame] | 362 | if (plat->speed_hz != speed) { |
| 363 | /* Set the clock frequency */ |
| 364 | /* If speed == 0, default to lowest speed */ |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 365 | while ((baud_rate_val < 8) && |
| 366 | ((plat->frequency / |
| 367 | (2 << baud_rate_val)) > speed)) |
| 368 | baud_rate_val++; |
| 369 | |
| 370 | if (baud_rate_val > GQSPI_MAX_BAUD_RATE_VAL) |
| 371 | baud_rate_val = GQSPI_DFLT_BAUD_RATE_VAL; |
| 372 | |
| 373 | plat->speed_hz = plat->frequency / (2 << baud_rate_val); |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 374 | |
Brandon Maier | d9aa19e | 2021-01-20 14:28:30 -0600 | [diff] [blame] | 375 | confr = readl(®s->confr); |
| 376 | confr &= ~GQSPI_BAUD_DIV_MASK; |
| 377 | confr |= (baud_rate_val << 3); |
| 378 | writel(confr, ®s->confr); |
| 379 | zynqmp_qspi_set_tapdelay(bus, baud_rate_val); |
| 380 | |
| 381 | debug("regs=%p, speed=%d\n", priv->regs, plat->speed_hz); |
| 382 | } |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 383 | |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | static int zynqmp_qspi_probe(struct udevice *bus) |
| 388 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 389 | struct zynqmp_qspi_plat *plat = dev_get_plat(bus); |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 390 | struct zynqmp_qspi_priv *priv = dev_get_priv(bus); |
| 391 | struct clk clk; |
| 392 | unsigned long clock; |
| 393 | int ret; |
| 394 | |
| 395 | debug("%s: bus:%p, priv:%p\n", __func__, bus, priv); |
| 396 | |
| 397 | priv->regs = plat->regs; |
| 398 | priv->dma_regs = plat->dma_regs; |
Ashok Reddy Soma | d91b0f4 | 2022-08-25 06:59:03 -0600 | [diff] [blame] | 399 | priv->io_mode = plat->io_mode; |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 400 | |
| 401 | ret = clk_get_by_index(bus, 0, &clk); |
| 402 | if (ret < 0) { |
Sean Anderson | 49dfbe9 | 2020-09-15 10:45:12 -0400 | [diff] [blame] | 403 | dev_err(bus, "failed to get clock\n"); |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 404 | return ret; |
| 405 | } |
| 406 | |
| 407 | clock = clk_get_rate(&clk); |
| 408 | if (IS_ERR_VALUE(clock)) { |
Sean Anderson | 49dfbe9 | 2020-09-15 10:45:12 -0400 | [diff] [blame] | 409 | dev_err(bus, "failed to get rate\n"); |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 410 | return clock; |
| 411 | } |
| 412 | debug("%s: CLK %ld\n", __func__, clock); |
| 413 | |
| 414 | ret = clk_enable(&clk); |
Michal Simek | 9b7aac7 | 2021-02-09 15:28:15 +0100 | [diff] [blame] | 415 | if (ret) { |
Sean Anderson | 49dfbe9 | 2020-09-15 10:45:12 -0400 | [diff] [blame] | 416 | dev_err(bus, "failed to enable clock\n"); |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 417 | return ret; |
| 418 | } |
| 419 | plat->frequency = clock; |
| 420 | plat->speed_hz = plat->frequency / 2; |
| 421 | |
| 422 | /* init the zynq spi hw */ |
| 423 | zynqmp_qspi_init_hw(priv); |
| 424 | |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | static int zynqmp_qspi_set_mode(struct udevice *bus, uint mode) |
| 429 | { |
| 430 | struct zynqmp_qspi_priv *priv = dev_get_priv(bus); |
| 431 | struct zynqmp_qspi_regs *regs = priv->regs; |
| 432 | u32 confr; |
| 433 | |
| 434 | debug("%s\n", __func__); |
| 435 | /* Set the SPI Clock phase and polarities */ |
| 436 | confr = readl(®s->confr); |
| 437 | confr &= ~(GQSPI_CONFIG_CPHA_MASK | |
| 438 | GQSPI_CONFIG_CPOL_MASK); |
| 439 | |
| 440 | if (mode & SPI_CPHA) |
| 441 | confr |= GQSPI_CONFIG_CPHA_MASK; |
| 442 | if (mode & SPI_CPOL) |
| 443 | confr |= GQSPI_CONFIG_CPOL_MASK; |
| 444 | |
| 445 | writel(confr, ®s->confr); |
| 446 | |
| 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | static int zynqmp_qspi_fill_tx_fifo(struct zynqmp_qspi_priv *priv, u32 size) |
| 451 | { |
| 452 | u32 data; |
| 453 | int ret = 0; |
| 454 | struct zynqmp_qspi_regs *regs = priv->regs; |
| 455 | u32 *buf = (u32 *)priv->tx_buf; |
| 456 | u32 len = size; |
| 457 | |
| 458 | debug("TxFIFO: 0x%x, size: 0x%x\n", readl(®s->isr), |
| 459 | size); |
| 460 | |
| 461 | while (size) { |
| 462 | ret = wait_for_bit_le32(®s->isr, GQSPI_IXR_TXNFULL_MASK, 1, |
| 463 | GQSPI_TIMEOUT, 1); |
| 464 | if (ret) { |
| 465 | printf("%s: Timeout\n", __func__); |
| 466 | return ret; |
| 467 | } |
| 468 | |
| 469 | if (size >= 4) { |
| 470 | writel(*buf, ®s->txd0r); |
| 471 | buf++; |
| 472 | size -= 4; |
| 473 | } else { |
| 474 | switch (size) { |
| 475 | case 1: |
| 476 | data = *((u8 *)buf); |
| 477 | buf += 1; |
| 478 | data |= GENMASK(31, 8); |
| 479 | break; |
| 480 | case 2: |
| 481 | data = *((u16 *)buf); |
| 482 | buf += 2; |
| 483 | data |= GENMASK(31, 16); |
| 484 | break; |
| 485 | case 3: |
T Karthik Reddy | 9021748 | 2020-11-19 05:00:36 -0700 | [diff] [blame] | 486 | data = *buf; |
| 487 | buf += 3; |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 488 | data |= GENMASK(31, 24); |
| 489 | break; |
| 490 | } |
| 491 | writel(data, ®s->txd0r); |
| 492 | size = 0; |
| 493 | } |
| 494 | } |
| 495 | |
Ashok Reddy Soma | 4f9d255 | 2021-10-19 19:43:00 +0530 | [diff] [blame] | 496 | ret = wait_for_bit_le32(®s->isr, GQSPI_IXR_TXFIFOEMPTY_MASK, 1, |
| 497 | GQSPI_TIMEOUT, 1); |
| 498 | if (ret) { |
| 499 | printf("%s: Timeout\n", __func__); |
| 500 | return ret; |
| 501 | } |
| 502 | |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 503 | priv->tx_buf += len; |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | static void zynqmp_qspi_genfifo_cmd(struct zynqmp_qspi_priv *priv) |
| 508 | { |
Brandon Maier | f1fd79a | 2021-01-20 10:39:46 -0600 | [diff] [blame] | 509 | const struct spi_mem_op *op = priv->op; |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 510 | u32 gen_fifo_cmd; |
Brandon Maier | f1fd79a | 2021-01-20 10:39:46 -0600 | [diff] [blame] | 511 | u8 i, dummy_cycles, addr; |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 512 | |
Brandon Maier | f1fd79a | 2021-01-20 10:39:46 -0600 | [diff] [blame] | 513 | /* Send opcode */ |
| 514 | gen_fifo_cmd = zynqmp_qspi_bus_select(priv); |
| 515 | gen_fifo_cmd |= zynqmp_qspi_genfifo_mode(op->cmd.buswidth); |
| 516 | gen_fifo_cmd |= GQSPI_GFIFO_TX; |
| 517 | gen_fifo_cmd |= op->cmd.opcode; |
| 518 | zynqmp_qspi_fill_gen_fifo(priv, gen_fifo_cmd); |
| 519 | |
| 520 | /* Send address */ |
| 521 | for (i = 0; i < op->addr.nbytes; i++) { |
| 522 | addr = op->addr.val >> (8 * (op->addr.nbytes - i - 1)); |
| 523 | |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 524 | gen_fifo_cmd = zynqmp_qspi_bus_select(priv); |
Brandon Maier | f1fd79a | 2021-01-20 10:39:46 -0600 | [diff] [blame] | 525 | gen_fifo_cmd |= zynqmp_qspi_genfifo_mode(op->addr.buswidth); |
| 526 | gen_fifo_cmd |= GQSPI_GFIFO_TX; |
| 527 | gen_fifo_cmd |= addr; |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 528 | |
| 529 | debug("GFIFO_CMD_Cmd = 0x%x\n", gen_fifo_cmd); |
| 530 | |
| 531 | zynqmp_qspi_fill_gen_fifo(priv, gen_fifo_cmd); |
| 532 | } |
Brandon Maier | f1fd79a | 2021-01-20 10:39:46 -0600 | [diff] [blame] | 533 | |
| 534 | /* Send dummy */ |
| 535 | if (op->dummy.nbytes) { |
| 536 | dummy_cycles = op->dummy.nbytes * 8 / op->dummy.buswidth; |
| 537 | |
| 538 | gen_fifo_cmd = zynqmp_qspi_bus_select(priv); |
| 539 | gen_fifo_cmd |= zynqmp_qspi_genfifo_mode(op->dummy.buswidth); |
| 540 | gen_fifo_cmd &= ~(GQSPI_GFIFO_TX | GQSPI_GFIFO_RX); |
| 541 | gen_fifo_cmd |= GQSPI_GFIFO_DATA_XFR_MASK; |
| 542 | gen_fifo_cmd |= dummy_cycles; |
| 543 | zynqmp_qspi_fill_gen_fifo(priv, gen_fifo_cmd); |
| 544 | } |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | static u32 zynqmp_qspi_calc_exp(struct zynqmp_qspi_priv *priv, |
| 548 | u32 *gen_fifo_cmd) |
| 549 | { |
| 550 | u32 expval = 8; |
| 551 | u32 len; |
| 552 | |
| 553 | while (1) { |
| 554 | if (priv->len > 255) { |
| 555 | if (priv->len & (1 << expval)) { |
| 556 | *gen_fifo_cmd &= ~GQSPI_GFIFO_IMD_MASK; |
| 557 | *gen_fifo_cmd |= GQSPI_GFIFO_EXP_MASK; |
| 558 | *gen_fifo_cmd |= expval; |
| 559 | priv->len -= (1 << expval); |
| 560 | return expval; |
| 561 | } |
| 562 | expval++; |
| 563 | } else { |
| 564 | *gen_fifo_cmd &= ~(GQSPI_GFIFO_IMD_MASK | |
| 565 | GQSPI_GFIFO_EXP_MASK); |
| 566 | *gen_fifo_cmd |= (u8)priv->len; |
| 567 | len = (u8)priv->len; |
| 568 | priv->len = 0; |
| 569 | return len; |
| 570 | } |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | static int zynqmp_qspi_genfifo_fill_tx(struct zynqmp_qspi_priv *priv) |
| 575 | { |
| 576 | u32 gen_fifo_cmd; |
| 577 | u32 len; |
| 578 | int ret = 0; |
| 579 | |
| 580 | gen_fifo_cmd = zynqmp_qspi_bus_select(priv); |
Brandon Maier | f1fd79a | 2021-01-20 10:39:46 -0600 | [diff] [blame] | 581 | gen_fifo_cmd |= zynqmp_qspi_genfifo_mode(priv->op->data.buswidth); |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 582 | gen_fifo_cmd |= GQSPI_GFIFO_TX | |
| 583 | GQSPI_GFIFO_DATA_XFR_MASK; |
| 584 | |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 585 | while (priv->len) { |
| 586 | len = zynqmp_qspi_calc_exp(priv, &gen_fifo_cmd); |
| 587 | zynqmp_qspi_fill_gen_fifo(priv, gen_fifo_cmd); |
| 588 | |
| 589 | debug("GFIFO_CMD_TX:0x%x\n", gen_fifo_cmd); |
| 590 | |
| 591 | if (gen_fifo_cmd & GQSPI_GFIFO_EXP_MASK) |
| 592 | ret = zynqmp_qspi_fill_tx_fifo(priv, |
| 593 | 1 << len); |
| 594 | else |
| 595 | ret = zynqmp_qspi_fill_tx_fifo(priv, |
| 596 | len); |
| 597 | |
| 598 | if (ret) |
| 599 | return ret; |
| 600 | } |
| 601 | return ret; |
| 602 | } |
| 603 | |
Ashok Reddy Soma | d91b0f4 | 2022-08-25 06:59:03 -0600 | [diff] [blame] | 604 | static int zynqmp_qspi_start_io(struct zynqmp_qspi_priv *priv, |
| 605 | u32 gen_fifo_cmd, u32 *buf) |
| 606 | { |
| 607 | u32 len; |
| 608 | u32 actuallen = priv->len; |
| 609 | u32 config_reg, ier, isr; |
| 610 | u32 timeout = GQSPI_TIMEOUT; |
| 611 | struct zynqmp_qspi_regs *regs = priv->regs; |
| 612 | u32 last_bits; |
| 613 | u32 *traverse = buf; |
| 614 | |
| 615 | while (priv->len) { |
| 616 | len = zynqmp_qspi_calc_exp(priv, &gen_fifo_cmd); |
| 617 | /* If exponent bit is set, reset immediate to be 2^len */ |
| 618 | if (gen_fifo_cmd & GQSPI_GFIFO_EXP_MASK) |
| 619 | priv->bytes_to_receive = (1 << len); |
| 620 | else |
| 621 | priv->bytes_to_receive = len; |
| 622 | zynqmp_qspi_fill_gen_fifo(priv, gen_fifo_cmd); |
| 623 | debug("GFIFO_CMD_RX:0x%x\n", gen_fifo_cmd); |
| 624 | /* Manual start */ |
| 625 | config_reg = readl(®s->confr); |
| 626 | config_reg |= GQSPI_STRT_GEN_FIFO; |
| 627 | writel(config_reg, ®s->confr); |
| 628 | /* Enable RX interrupts for IO mode */ |
| 629 | ier = readl(®s->ier); |
| 630 | ier |= GQSPI_IXR_ALL_MASK; |
| 631 | writel(ier, ®s->ier); |
| 632 | while (priv->bytes_to_receive && timeout) { |
| 633 | isr = readl(®s->isr); |
| 634 | if (isr & GQSPI_IXR_RXNEMTY_MASK) { |
| 635 | if (priv->bytes_to_receive >= 4) { |
| 636 | *traverse = readl(®s->drxr); |
| 637 | traverse++; |
| 638 | priv->bytes_to_receive -= 4; |
| 639 | } else { |
| 640 | last_bits = readl(®s->drxr); |
| 641 | memcpy(traverse, &last_bits, |
| 642 | priv->bytes_to_receive); |
| 643 | priv->bytes_to_receive = 0; |
| 644 | } |
| 645 | timeout = GQSPI_TIMEOUT; |
| 646 | } else { |
| 647 | udelay(1); |
| 648 | timeout--; |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | debug("buf:0x%lx, rxbuf:0x%lx, *buf:0x%x len: 0x%x\n", |
| 653 | (unsigned long)buf, (unsigned long)priv->rx_buf, |
| 654 | *buf, actuallen); |
| 655 | if (!timeout) { |
| 656 | printf("IO timeout: %d\n", readl(®s->isr)); |
| 657 | return -1; |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | return 0; |
| 662 | } |
| 663 | |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 664 | static int zynqmp_qspi_start_dma(struct zynqmp_qspi_priv *priv, |
| 665 | u32 gen_fifo_cmd, u32 *buf) |
| 666 | { |
| 667 | u32 addr; |
Ashok Reddy Soma | 020b353 | 2021-08-20 07:43:17 -0600 | [diff] [blame] | 668 | u32 size; |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 669 | u32 actuallen = priv->len; |
Ashok Reddy Soma | a3d4bfb | 2022-08-25 06:59:04 -0600 | [diff] [blame^] | 670 | u32 totallen = priv->len; |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 671 | int ret = 0; |
| 672 | struct zynqmp_qspi_dma_regs *dma_regs = priv->dma_regs; |
| 673 | |
Ashok Reddy Soma | a3d4bfb | 2022-08-25 06:59:04 -0600 | [diff] [blame^] | 674 | while (totallen) { |
| 675 | if (totallen >= SZ_512M) |
| 676 | priv->len = SZ_256M; |
| 677 | else |
| 678 | priv->len = totallen; |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 679 | |
Ashok Reddy Soma | a3d4bfb | 2022-08-25 06:59:04 -0600 | [diff] [blame^] | 680 | totallen -= priv->len; /* Save remaining bytes length to read */ |
| 681 | actuallen = priv->len; /* Actual number of bytes reading */ |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 682 | |
Ashok Reddy Soma | a3d4bfb | 2022-08-25 06:59:04 -0600 | [diff] [blame^] | 683 | writel((unsigned long)buf, &dma_regs->dmadst); |
| 684 | writel(roundup(priv->len, GQSPI_DMA_ALIGN), &dma_regs->dmasize); |
| 685 | writel(GQSPI_DMA_DST_I_STS_MASK, &dma_regs->dmaier); |
| 686 | addr = (unsigned long)buf; |
| 687 | size = roundup(priv->len, GQSPI_DMA_ALIGN); |
| 688 | flush_dcache_range(addr, addr + size); |
| 689 | |
| 690 | while (priv->len) { |
| 691 | zynqmp_qspi_calc_exp(priv, &gen_fifo_cmd); |
| 692 | zynqmp_qspi_fill_gen_fifo(priv, gen_fifo_cmd); |
| 693 | |
| 694 | debug("GFIFO_CMD_RX:0x%x\n", gen_fifo_cmd); |
| 695 | } |
| 696 | |
| 697 | ret = wait_for_bit_le32(&dma_regs->dmaisr, |
| 698 | GQSPI_DMA_DST_I_STS_DONE, 1, |
| 699 | GQSPI_TIMEOUT, 1); |
| 700 | if (ret) { |
| 701 | printf("DMA Timeout:0x%x\n", readl(&dma_regs->dmaisr)); |
| 702 | return -ETIMEDOUT; |
| 703 | } |
| 704 | |
| 705 | writel(GQSPI_DMA_DST_I_STS_DONE, &dma_regs->dmaisr); |
| 706 | |
| 707 | debug("buf:0x%lx, rxbuf:0x%lx, *buf:0x%x len: 0x%x\n", |
| 708 | (unsigned long)buf, (unsigned long)priv->rx_buf, *buf, |
| 709 | actuallen); |
| 710 | |
| 711 | if (buf != priv->rx_buf) |
| 712 | memcpy(priv->rx_buf, buf, actuallen); |
| 713 | |
| 714 | buf = (u32 *)((u8 *)buf + actuallen); |
| 715 | priv->rx_buf = (u8 *)priv->rx_buf + actuallen; |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 716 | } |
| 717 | |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 718 | return 0; |
| 719 | } |
| 720 | |
| 721 | static int zynqmp_qspi_genfifo_fill_rx(struct zynqmp_qspi_priv *priv) |
| 722 | { |
| 723 | u32 gen_fifo_cmd; |
| 724 | u32 *buf; |
| 725 | u32 actuallen = priv->len; |
| 726 | |
| 727 | gen_fifo_cmd = zynqmp_qspi_bus_select(priv); |
Brandon Maier | f1fd79a | 2021-01-20 10:39:46 -0600 | [diff] [blame] | 728 | gen_fifo_cmd |= zynqmp_qspi_genfifo_mode(priv->op->data.buswidth); |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 729 | gen_fifo_cmd |= GQSPI_GFIFO_RX | |
| 730 | GQSPI_GFIFO_DATA_XFR_MASK; |
| 731 | |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 732 | /* |
| 733 | * Check if receive buffer is aligned to 4 byte and length |
| 734 | * is multiples of four byte as we are using dma to receive. |
| 735 | */ |
Ashok Reddy Soma | d91b0f4 | 2022-08-25 06:59:03 -0600 | [diff] [blame] | 736 | if ((!((unsigned long)priv->rx_buf & (GQSPI_DMA_ALIGN - 1)) && |
| 737 | !(actuallen % GQSPI_DMA_ALIGN)) || priv->io_mode) { |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 738 | buf = (u32 *)priv->rx_buf; |
Ashok Reddy Soma | d91b0f4 | 2022-08-25 06:59:03 -0600 | [diff] [blame] | 739 | if (priv->io_mode) |
| 740 | return zynqmp_qspi_start_io(priv, gen_fifo_cmd, buf); |
| 741 | else |
| 742 | return zynqmp_qspi_start_dma(priv, gen_fifo_cmd, buf); |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | ALLOC_CACHE_ALIGN_BUFFER(u8, tmp, roundup(priv->len, |
| 746 | GQSPI_DMA_ALIGN)); |
| 747 | buf = (u32 *)tmp; |
| 748 | return zynqmp_qspi_start_dma(priv, gen_fifo_cmd, buf); |
| 749 | } |
| 750 | |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 751 | static int zynqmp_qspi_claim_bus(struct udevice *dev) |
| 752 | { |
| 753 | struct udevice *bus = dev->parent; |
| 754 | struct zynqmp_qspi_priv *priv = dev_get_priv(bus); |
| 755 | struct zynqmp_qspi_regs *regs = priv->regs; |
| 756 | |
| 757 | writel(GQSPI_ENABLE_ENABLE_MASK, ®s->enbr); |
| 758 | |
| 759 | return 0; |
| 760 | } |
| 761 | |
| 762 | static int zynqmp_qspi_release_bus(struct udevice *dev) |
| 763 | { |
| 764 | struct udevice *bus = dev->parent; |
| 765 | struct zynqmp_qspi_priv *priv = dev_get_priv(bus); |
| 766 | struct zynqmp_qspi_regs *regs = priv->regs; |
| 767 | |
| 768 | writel(~GQSPI_ENABLE_ENABLE_MASK, ®s->enbr); |
| 769 | |
| 770 | return 0; |
| 771 | } |
| 772 | |
Brandon Maier | f1fd79a | 2021-01-20 10:39:46 -0600 | [diff] [blame] | 773 | static int zynqmp_qspi_exec_op(struct spi_slave *slave, |
| 774 | const struct spi_mem_op *op) |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 775 | { |
Brandon Maier | f1fd79a | 2021-01-20 10:39:46 -0600 | [diff] [blame] | 776 | struct zynqmp_qspi_priv *priv = dev_get_priv(slave->dev->parent); |
| 777 | int ret = 0; |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 778 | |
Brandon Maier | f1fd79a | 2021-01-20 10:39:46 -0600 | [diff] [blame] | 779 | priv->op = op; |
| 780 | priv->tx_buf = op->data.buf.out; |
| 781 | priv->rx_buf = op->data.buf.in; |
| 782 | priv->len = op->data.nbytes; |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 783 | |
Brandon Maier | f1fd79a | 2021-01-20 10:39:46 -0600 | [diff] [blame] | 784 | zynqmp_qspi_chipselect(priv, 1); |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 785 | |
Brandon Maier | f1fd79a | 2021-01-20 10:39:46 -0600 | [diff] [blame] | 786 | /* Send opcode, addr, dummy */ |
| 787 | zynqmp_qspi_genfifo_cmd(priv); |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 788 | |
Brandon Maier | f1fd79a | 2021-01-20 10:39:46 -0600 | [diff] [blame] | 789 | /* Request the transfer */ |
| 790 | if (op->data.dir == SPI_MEM_DATA_IN) |
| 791 | ret = zynqmp_qspi_genfifo_fill_rx(priv); |
| 792 | else if (op->data.dir == SPI_MEM_DATA_OUT) |
| 793 | ret = zynqmp_qspi_genfifo_fill_tx(priv); |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 794 | |
Brandon Maier | f1fd79a | 2021-01-20 10:39:46 -0600 | [diff] [blame] | 795 | zynqmp_qspi_chipselect(priv, 0); |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 796 | |
Brandon Maier | f1fd79a | 2021-01-20 10:39:46 -0600 | [diff] [blame] | 797 | return ret; |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 798 | } |
| 799 | |
Brandon Maier | f1fd79a | 2021-01-20 10:39:46 -0600 | [diff] [blame] | 800 | static const struct spi_controller_mem_ops zynqmp_qspi_mem_ops = { |
| 801 | .exec_op = zynqmp_qspi_exec_op, |
| 802 | }; |
| 803 | |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 804 | static const struct dm_spi_ops zynqmp_qspi_ops = { |
| 805 | .claim_bus = zynqmp_qspi_claim_bus, |
| 806 | .release_bus = zynqmp_qspi_release_bus, |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 807 | .set_speed = zynqmp_qspi_set_speed, |
| 808 | .set_mode = zynqmp_qspi_set_mode, |
Brandon Maier | f1fd79a | 2021-01-20 10:39:46 -0600 | [diff] [blame] | 809 | .mem_ops = &zynqmp_qspi_mem_ops, |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 810 | }; |
| 811 | |
| 812 | static const struct udevice_id zynqmp_qspi_ids[] = { |
| 813 | { .compatible = "xlnx,zynqmp-qspi-1.0" }, |
Michal Simek | f3976cc | 2018-11-29 08:48:28 +0100 | [diff] [blame] | 814 | { .compatible = "xlnx,versal-qspi-1.0" }, |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 815 | { } |
| 816 | }; |
| 817 | |
| 818 | U_BOOT_DRIVER(zynqmp_qspi) = { |
| 819 | .name = "zynqmp_qspi", |
| 820 | .id = UCLASS_SPI, |
| 821 | .of_match = zynqmp_qspi_ids, |
| 822 | .ops = &zynqmp_qspi_ops, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 823 | .of_to_plat = zynqmp_qspi_of_to_plat, |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 824 | .plat_auto = sizeof(struct zynqmp_qspi_plat), |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 825 | .priv_auto = sizeof(struct zynqmp_qspi_priv), |
Siva Durga Prasad Paladugu | 22cca17 | 2018-07-04 17:31:23 +0530 | [diff] [blame] | 826 | .probe = zynqmp_qspi_probe, |
| 827 | }; |