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