Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Matt Porter | da3e4c6 | 2013-10-07 15:53:02 +0530 | [diff] [blame] | 2 | /* |
| 3 | * TI QSPI driver |
| 4 | * |
| 5 | * Copyright (C) 2013, Texas Instruments, Incorporated |
Matt Porter | da3e4c6 | 2013-10-07 15:53:02 +0530 | [diff] [blame] | 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> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 11 | #include <asm/cache.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 12 | #include <asm/global_data.h> |
Matt Porter | da3e4c6 | 2013-10-07 15:53:02 +0530 | [diff] [blame] | 13 | #include <asm/io.h> |
| 14 | #include <asm/arch/omap.h> |
| 15 | #include <malloc.h> |
| 16 | #include <spi.h> |
Vignesh Raghavendra | 6f9efcf | 2019-04-16 21:32:00 +0530 | [diff] [blame] | 17 | #include <spi-mem.h> |
Mugunthan V N | 540a115 | 2015-12-23 20:39:40 +0530 | [diff] [blame] | 18 | #include <dm.h> |
Sourav Poddar | 2145dff | 2013-12-21 12:50:09 +0530 | [diff] [blame] | 19 | #include <asm/gpio.h> |
| 20 | #include <asm/omap_gpio.h> |
Vignesh R | a5bba8d | 2015-08-17 15:20:13 +0530 | [diff] [blame] | 21 | #include <asm/omap_common.h> |
| 22 | #include <asm/ti-common/ti-edma3.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> |
Vignesh R | 44e4bac | 2016-11-05 16:05:16 +0530 | [diff] [blame] | 25 | #include <linux/kernel.h> |
Jean-Jacques Hiblot | 931b1f2 | 2017-02-13 16:17:49 +0100 | [diff] [blame] | 26 | #include <regmap.h> |
| 27 | #include <syscon.h> |
Matt Porter | da3e4c6 | 2013-10-07 15:53:02 +0530 | [diff] [blame] | 28 | |
Mugunthan V N | 540a115 | 2015-12-23 20:39:40 +0530 | [diff] [blame] | 29 | DECLARE_GLOBAL_DATA_PTR; |
| 30 | |
Matt Porter | da3e4c6 | 2013-10-07 15:53:02 +0530 | [diff] [blame] | 31 | /* ti qpsi register bit masks */ |
| 32 | #define QSPI_TIMEOUT 2000000 |
Stefan Mätje | 1560b76 | 2021-11-30 01:06:56 +0100 | [diff] [blame] | 33 | /* AM4372: QSPI gets SPI_GCLK from PRCM unit as PER_CLKOUTM2 divided by 4. */ |
| 34 | #define QSPI_FCLK (192000000 / 4) |
Vignesh R | 1535e5a | 2016-07-25 15:45:45 +0530 | [diff] [blame] | 35 | #define QSPI_DRA7XX_FCLK 76800000 |
Vignesh R | b007997 | 2016-09-07 15:18:22 +0530 | [diff] [blame] | 36 | #define QSPI_WLEN_MAX_BITS 128 |
| 37 | #define QSPI_WLEN_MAX_BYTES (QSPI_WLEN_MAX_BITS >> 3) |
| 38 | #define QSPI_WLEN_MASK QSPI_WLEN(QSPI_WLEN_MAX_BITS) |
Matt Porter | da3e4c6 | 2013-10-07 15:53:02 +0530 | [diff] [blame] | 39 | /* clock control */ |
Jagan Teki | f16e4db | 2015-10-23 01:39:20 +0530 | [diff] [blame] | 40 | #define QSPI_CLK_EN BIT(31) |
Matt Porter | da3e4c6 | 2013-10-07 15:53:02 +0530 | [diff] [blame] | 41 | #define QSPI_CLK_DIV_MAX 0xffff |
| 42 | /* command */ |
| 43 | #define QSPI_EN_CS(n) (n << 28) |
| 44 | #define QSPI_WLEN(n) ((n-1) << 19) |
Jagan Teki | f16e4db | 2015-10-23 01:39:20 +0530 | [diff] [blame] | 45 | #define QSPI_3_PIN BIT(18) |
| 46 | #define QSPI_RD_SNGL BIT(16) |
Matt Porter | da3e4c6 | 2013-10-07 15:53:02 +0530 | [diff] [blame] | 47 | #define QSPI_WR_SNGL (2 << 16) |
| 48 | #define QSPI_INVAL (4 << 16) |
| 49 | #define QSPI_RD_QUAD (7 << 16) |
| 50 | /* device control */ |
Matt Porter | da3e4c6 | 2013-10-07 15:53:02 +0530 | [diff] [blame] | 51 | #define QSPI_CKPHA(n) (1 << (2 + n*8)) |
| 52 | #define QSPI_CSPOL(n) (1 << (1 + n*8)) |
| 53 | #define QSPI_CKPOL(n) (1 << (n*8)) |
| 54 | /* status */ |
Jagan Teki | f16e4db | 2015-10-23 01:39:20 +0530 | [diff] [blame] | 55 | #define QSPI_WC BIT(1) |
| 56 | #define QSPI_BUSY BIT(0) |
Matt Porter | da3e4c6 | 2013-10-07 15:53:02 +0530 | [diff] [blame] | 57 | #define QSPI_WC_BUSY (QSPI_WC | QSPI_BUSY) |
| 58 | #define QSPI_XFER_DONE QSPI_WC |
| 59 | #define MM_SWITCH 0x01 |
Mugunthan V N | 132e007 | 2015-12-23 20:39:33 +0530 | [diff] [blame] | 60 | #define MEM_CS(cs) ((cs + 1) << 8) |
Praneeth Bajjuri | 5d21e6f | 2016-06-21 14:05:36 +0530 | [diff] [blame] | 61 | #define MEM_CS_UNSELECT 0xfffff8ff |
Matt Porter | da3e4c6 | 2013-10-07 15:53:02 +0530 | [diff] [blame] | 62 | |
Matt Porter | da3e4c6 | 2013-10-07 15:53:02 +0530 | [diff] [blame] | 63 | #define QSPI_SETUP0_READ_NORMAL (0x0 << 12) |
Mugunthan V N | 540a115 | 2015-12-23 20:39:40 +0530 | [diff] [blame] | 64 | #define QSPI_SETUP0_READ_DUAL (0x1 << 12) |
Matt Porter | da3e4c6 | 2013-10-07 15:53:02 +0530 | [diff] [blame] | 65 | #define QSPI_SETUP0_READ_QUAD (0x3 << 12) |
Vignesh Raghavendra | 6f9efcf | 2019-04-16 21:32:00 +0530 | [diff] [blame] | 66 | #define QSPI_SETUP0_ADDR_SHIFT (8) |
| 67 | #define QSPI_SETUP0_DBITS_SHIFT (10) |
Matt Porter | da3e4c6 | 2013-10-07 15:53:02 +0530 | [diff] [blame] | 68 | |
Vignesh Raghavendra | e995dbb | 2019-12-11 18:59:36 +0530 | [diff] [blame] | 69 | #define TI_QSPI_SETUP_REG(priv, cs) (&(priv)->base->setup0 + (cs)) |
| 70 | |
Matt Porter | da3e4c6 | 2013-10-07 15:53:02 +0530 | [diff] [blame] | 71 | /* ti qspi register set */ |
| 72 | struct ti_qspi_regs { |
| 73 | u32 pid; |
| 74 | u32 pad0[3]; |
| 75 | u32 sysconfig; |
| 76 | u32 pad1[3]; |
| 77 | u32 int_stat_raw; |
| 78 | u32 int_stat_en; |
| 79 | u32 int_en_set; |
| 80 | u32 int_en_ctlr; |
| 81 | u32 intc_eoi; |
| 82 | u32 pad2[3]; |
| 83 | u32 clk_ctrl; |
| 84 | u32 dc; |
| 85 | u32 cmd; |
| 86 | u32 status; |
| 87 | u32 data; |
| 88 | u32 setup0; |
| 89 | u32 setup1; |
| 90 | u32 setup2; |
| 91 | u32 setup3; |
| 92 | u32 memswitch; |
| 93 | u32 data1; |
| 94 | u32 data2; |
| 95 | u32 data3; |
| 96 | }; |
| 97 | |
Mugunthan V N | e206d30 | 2015-12-23 20:39:34 +0530 | [diff] [blame] | 98 | /* ti qspi priv */ |
| 99 | struct ti_qspi_priv { |
Mugunthan V N | 540a115 | 2015-12-23 20:39:40 +0530 | [diff] [blame] | 100 | void *memory_map; |
Vignesh Raghavendra | 6f9efcf | 2019-04-16 21:32:00 +0530 | [diff] [blame] | 101 | size_t mmap_size; |
Mugunthan V N | 540a115 | 2015-12-23 20:39:40 +0530 | [diff] [blame] | 102 | uint max_hz; |
| 103 | u32 num_cs; |
Matt Porter | da3e4c6 | 2013-10-07 15:53:02 +0530 | [diff] [blame] | 104 | struct ti_qspi_regs *base; |
Mugunthan V N | cd46773 | 2015-12-23 20:39:35 +0530 | [diff] [blame] | 105 | void *ctrl_mod_mmap; |
Vignesh R | 1535e5a | 2016-07-25 15:45:45 +0530 | [diff] [blame] | 106 | ulong fclk; |
Matt Porter | da3e4c6 | 2013-10-07 15:53:02 +0530 | [diff] [blame] | 107 | unsigned int mode; |
| 108 | u32 cmd; |
| 109 | u32 dc; |
| 110 | }; |
| 111 | |
Vignesh Raghavendra | f3603b8 | 2019-04-16 21:31:59 +0530 | [diff] [blame] | 112 | static int ti_qspi_set_speed(struct udevice *bus, uint hz) |
Matt Porter | da3e4c6 | 2013-10-07 15:53:02 +0530 | [diff] [blame] | 113 | { |
Vignesh Raghavendra | f3603b8 | 2019-04-16 21:31:59 +0530 | [diff] [blame] | 114 | struct ti_qspi_priv *priv = dev_get_priv(bus); |
Matt Porter | da3e4c6 | 2013-10-07 15:53:02 +0530 | [diff] [blame] | 115 | uint clk_div; |
| 116 | |
Matt Porter | da3e4c6 | 2013-10-07 15:53:02 +0530 | [diff] [blame] | 117 | if (!hz) |
| 118 | clk_div = 0; |
| 119 | else |
Vignesh R | 44e4bac | 2016-11-05 16:05:16 +0530 | [diff] [blame] | 120 | clk_div = DIV_ROUND_UP(priv->fclk, hz) - 1; |
| 121 | |
| 122 | /* truncate clk_div value to QSPI_CLK_DIV_MAX */ |
| 123 | if (clk_div > QSPI_CLK_DIV_MAX) |
| 124 | clk_div = QSPI_CLK_DIV_MAX; |
Matt Porter | da3e4c6 | 2013-10-07 15:53:02 +0530 | [diff] [blame] | 125 | |
Vignesh R | b970767 | 2016-07-22 10:55:49 +0530 | [diff] [blame] | 126 | debug("ti_spi_set_speed: hz: %d, clock divider %d\n", hz, clk_div); |
| 127 | |
Matt Porter | da3e4c6 | 2013-10-07 15:53:02 +0530 | [diff] [blame] | 128 | /* disable SCLK */ |
Mugunthan V N | e206d30 | 2015-12-23 20:39:34 +0530 | [diff] [blame] | 129 | writel(readl(&priv->base->clk_ctrl) & ~QSPI_CLK_EN, |
| 130 | &priv->base->clk_ctrl); |
Vignesh R | 44e4bac | 2016-11-05 16:05:16 +0530 | [diff] [blame] | 131 | /* enable SCLK and program the clk divider */ |
Mugunthan V N | e206d30 | 2015-12-23 20:39:34 +0530 | [diff] [blame] | 132 | writel(QSPI_CLK_EN | clk_div, &priv->base->clk_ctrl); |
Vignesh Raghavendra | f3603b8 | 2019-04-16 21:31:59 +0530 | [diff] [blame] | 133 | |
| 134 | return 0; |
Matt Porter | da3e4c6 | 2013-10-07 15:53:02 +0530 | [diff] [blame] | 135 | } |
| 136 | |
Mugunthan V N | cd46773 | 2015-12-23 20:39:35 +0530 | [diff] [blame] | 137 | static void ti_qspi_cs_deactivate(struct ti_qspi_priv *priv) |
Matt Porter | da3e4c6 | 2013-10-07 15:53:02 +0530 | [diff] [blame] | 138 | { |
Mugunthan V N | e206d30 | 2015-12-23 20:39:34 +0530 | [diff] [blame] | 139 | writel(priv->cmd | QSPI_INVAL, &priv->base->cmd); |
Vignesh R | 43ad9ce | 2015-11-10 11:52:10 +0530 | [diff] [blame] | 140 | /* dummy readl to ensure bus sync */ |
Mugunthan V N | cd46773 | 2015-12-23 20:39:35 +0530 | [diff] [blame] | 141 | readl(&priv->base->cmd); |
Matt Porter | da3e4c6 | 2013-10-07 15:53:02 +0530 | [diff] [blame] | 142 | } |
| 143 | |
Mugunthan V N | cd46773 | 2015-12-23 20:39:35 +0530 | [diff] [blame] | 144 | static void ti_qspi_ctrl_mode_mmap(void *ctrl_mod_mmap, int cs, bool enable) |
Matt Porter | da3e4c6 | 2013-10-07 15:53:02 +0530 | [diff] [blame] | 145 | { |
Mugunthan V N | cd46773 | 2015-12-23 20:39:35 +0530 | [diff] [blame] | 146 | u32 val; |
| 147 | |
| 148 | val = readl(ctrl_mod_mmap); |
| 149 | if (enable) |
| 150 | val |= MEM_CS(cs); |
| 151 | else |
| 152 | val &= MEM_CS_UNSELECT; |
| 153 | writel(val, ctrl_mod_mmap); |
| 154 | } |
| 155 | |
Vignesh Raghavendra | f3603b8 | 2019-04-16 21:31:59 +0530 | [diff] [blame] | 156 | static int ti_qspi_xfer(struct udevice *dev, unsigned int bitlen, |
| 157 | const void *dout, void *din, unsigned long flags) |
Mugunthan V N | cd46773 | 2015-12-23 20:39:35 +0530 | [diff] [blame] | 158 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 159 | struct dm_spi_slave_plat *slave = dev_get_parent_plat(dev); |
Vignesh Raghavendra | f3603b8 | 2019-04-16 21:31:59 +0530 | [diff] [blame] | 160 | struct ti_qspi_priv *priv; |
| 161 | struct udevice *bus; |
Matt Porter | da3e4c6 | 2013-10-07 15:53:02 +0530 | [diff] [blame] | 162 | uint words = bitlen >> 3; /* fixed 8-bit word length */ |
| 163 | const uchar *txp = dout; |
| 164 | uchar *rxp = din; |
| 165 | uint status; |
Sourav Poddar | 2145dff | 2013-12-21 12:50:09 +0530 | [diff] [blame] | 166 | int timeout; |
Vignesh Raghavendra | f3603b8 | 2019-04-16 21:31:59 +0530 | [diff] [blame] | 167 | unsigned int cs = slave->cs; |
| 168 | |
| 169 | bus = dev->parent; |
| 170 | priv = dev_get_priv(bus); |
| 171 | |
| 172 | if (cs > priv->num_cs) { |
| 173 | debug("invalid qspi chip select\n"); |
| 174 | return -EINVAL; |
| 175 | } |
Sourav Poddar | 2145dff | 2013-12-21 12:50:09 +0530 | [diff] [blame] | 176 | |
Matt Porter | da3e4c6 | 2013-10-07 15:53:02 +0530 | [diff] [blame] | 177 | if (bitlen == 0) |
| 178 | return -1; |
| 179 | |
| 180 | if (bitlen % 8) { |
| 181 | debug("spi_xfer: Non byte aligned SPI transfer\n"); |
| 182 | return -1; |
| 183 | } |
| 184 | |
| 185 | /* Setup command reg */ |
Mugunthan V N | e206d30 | 2015-12-23 20:39:34 +0530 | [diff] [blame] | 186 | priv->cmd = 0; |
| 187 | priv->cmd |= QSPI_WLEN(8); |
Mugunthan V N | cd46773 | 2015-12-23 20:39:35 +0530 | [diff] [blame] | 188 | priv->cmd |= QSPI_EN_CS(cs); |
Mugunthan V N | e206d30 | 2015-12-23 20:39:34 +0530 | [diff] [blame] | 189 | if (priv->mode & SPI_3WIRE) |
| 190 | priv->cmd |= QSPI_3_PIN; |
| 191 | priv->cmd |= 0xfff; |
Matt Porter | da3e4c6 | 2013-10-07 15:53:02 +0530 | [diff] [blame] | 192 | |
Vignesh R | b007997 | 2016-09-07 15:18:22 +0530 | [diff] [blame] | 193 | while (words) { |
| 194 | u8 xfer_len = 0; |
| 195 | |
Matt Porter | da3e4c6 | 2013-10-07 15:53:02 +0530 | [diff] [blame] | 196 | if (txp) { |
Vignesh R | b007997 | 2016-09-07 15:18:22 +0530 | [diff] [blame] | 197 | u32 cmd = priv->cmd; |
| 198 | |
| 199 | if (words >= QSPI_WLEN_MAX_BYTES) { |
| 200 | u32 *txbuf = (u32 *)txp; |
| 201 | u32 data; |
| 202 | |
| 203 | data = cpu_to_be32(*txbuf++); |
| 204 | writel(data, &priv->base->data3); |
| 205 | data = cpu_to_be32(*txbuf++); |
| 206 | writel(data, &priv->base->data2); |
| 207 | data = cpu_to_be32(*txbuf++); |
| 208 | writel(data, &priv->base->data1); |
| 209 | data = cpu_to_be32(*txbuf++); |
| 210 | writel(data, &priv->base->data); |
| 211 | cmd &= ~QSPI_WLEN_MASK; |
| 212 | cmd |= QSPI_WLEN(QSPI_WLEN_MAX_BITS); |
| 213 | xfer_len = QSPI_WLEN_MAX_BYTES; |
| 214 | } else { |
| 215 | writeb(*txp, &priv->base->data); |
| 216 | xfer_len = 1; |
| 217 | } |
| 218 | debug("tx cmd %08x dc %08x\n", |
| 219 | cmd | QSPI_WR_SNGL, priv->dc); |
| 220 | writel(cmd | QSPI_WR_SNGL, &priv->base->cmd); |
Mugunthan V N | e206d30 | 2015-12-23 20:39:34 +0530 | [diff] [blame] | 221 | status = readl(&priv->base->status); |
Matt Porter | da3e4c6 | 2013-10-07 15:53:02 +0530 | [diff] [blame] | 222 | timeout = QSPI_TIMEOUT; |
| 223 | while ((status & QSPI_WC_BUSY) != QSPI_XFER_DONE) { |
| 224 | if (--timeout < 0) { |
| 225 | printf("spi_xfer: TX timeout!\n"); |
| 226 | return -1; |
| 227 | } |
Mugunthan V N | e206d30 | 2015-12-23 20:39:34 +0530 | [diff] [blame] | 228 | status = readl(&priv->base->status); |
Matt Porter | da3e4c6 | 2013-10-07 15:53:02 +0530 | [diff] [blame] | 229 | } |
Vignesh R | b007997 | 2016-09-07 15:18:22 +0530 | [diff] [blame] | 230 | txp += xfer_len; |
Matt Porter | da3e4c6 | 2013-10-07 15:53:02 +0530 | [diff] [blame] | 231 | debug("tx done, status %08x\n", status); |
| 232 | } |
| 233 | if (rxp) { |
Matt Porter | da3e4c6 | 2013-10-07 15:53:02 +0530 | [diff] [blame] | 234 | debug("rx cmd %08x dc %08x\n", |
Vignesh R | aa29130 | 2016-07-22 10:55:48 +0530 | [diff] [blame] | 235 | ((u32)(priv->cmd | QSPI_RD_SNGL)), priv->dc); |
Vignesh R | aa29130 | 2016-07-22 10:55:48 +0530 | [diff] [blame] | 236 | writel(priv->cmd | QSPI_RD_SNGL, &priv->base->cmd); |
Mugunthan V N | e206d30 | 2015-12-23 20:39:34 +0530 | [diff] [blame] | 237 | status = readl(&priv->base->status); |
Matt Porter | da3e4c6 | 2013-10-07 15:53:02 +0530 | [diff] [blame] | 238 | timeout = QSPI_TIMEOUT; |
| 239 | while ((status & QSPI_WC_BUSY) != QSPI_XFER_DONE) { |
| 240 | if (--timeout < 0) { |
| 241 | printf("spi_xfer: RX timeout!\n"); |
| 242 | return -1; |
| 243 | } |
Mugunthan V N | e206d30 | 2015-12-23 20:39:34 +0530 | [diff] [blame] | 244 | status = readl(&priv->base->status); |
Matt Porter | da3e4c6 | 2013-10-07 15:53:02 +0530 | [diff] [blame] | 245 | } |
Mugunthan V N | e206d30 | 2015-12-23 20:39:34 +0530 | [diff] [blame] | 246 | *rxp++ = readl(&priv->base->data); |
Vignesh R | b007997 | 2016-09-07 15:18:22 +0530 | [diff] [blame] | 247 | xfer_len = 1; |
Matt Porter | da3e4c6 | 2013-10-07 15:53:02 +0530 | [diff] [blame] | 248 | debug("rx done, status %08x, read %02x\n", |
| 249 | status, *(rxp-1)); |
| 250 | } |
Vignesh R | b007997 | 2016-09-07 15:18:22 +0530 | [diff] [blame] | 251 | words -= xfer_len; |
Matt Porter | da3e4c6 | 2013-10-07 15:53:02 +0530 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /* Terminate frame */ |
| 255 | if (flags & SPI_XFER_END) |
Mugunthan V N | cd46773 | 2015-12-23 20:39:35 +0530 | [diff] [blame] | 256 | ti_qspi_cs_deactivate(priv); |
Matt Porter | da3e4c6 | 2013-10-07 15:53:02 +0530 | [diff] [blame] | 257 | |
| 258 | return 0; |
| 259 | } |
Vignesh R | a5bba8d | 2015-08-17 15:20:13 +0530 | [diff] [blame] | 260 | |
| 261 | /* TODO: control from sf layer to here through dm-spi */ |
Vignesh Raghavendra | 6f9efcf | 2019-04-16 21:32:00 +0530 | [diff] [blame] | 262 | static void ti_qspi_copy_mmap(void *data, void *offset, size_t len) |
Vignesh R | a5bba8d | 2015-08-17 15:20:13 +0530 | [diff] [blame] | 263 | { |
Vignesh Raghavendra | 6f9efcf | 2019-04-16 21:32:00 +0530 | [diff] [blame] | 264 | #if defined(CONFIG_TI_EDMA3) && !defined(CONFIG_DMA) |
Vignesh R | a5bba8d | 2015-08-17 15:20:13 +0530 | [diff] [blame] | 265 | unsigned int addr = (unsigned int) (data); |
| 266 | unsigned int edma_slot_num = 1; |
| 267 | |
| 268 | /* Invalidate the area, so no writeback into the RAM races with DMA */ |
| 269 | invalidate_dcache_range(addr, addr + roundup(len, ARCH_DMA_MINALIGN)); |
| 270 | |
| 271 | /* enable edma3 clocks */ |
| 272 | enable_edma3_clocks(); |
| 273 | |
| 274 | /* Call edma3 api to do actual DMA transfer */ |
| 275 | edma3_transfer(EDMA3_BASE, edma_slot_num, data, offset, len); |
| 276 | |
| 277 | /* disable edma3 clocks */ |
| 278 | disable_edma3_clocks(); |
Vignesh Raghavendra | 6f9efcf | 2019-04-16 21:32:00 +0530 | [diff] [blame] | 279 | #else |
| 280 | memcpy_fromio(data, offset, len); |
| 281 | #endif |
Vignesh R | a5bba8d | 2015-08-17 15:20:13 +0530 | [diff] [blame] | 282 | |
| 283 | *((unsigned int *)offset) += len; |
| 284 | } |
Mugunthan V N | cd46773 | 2015-12-23 20:39:35 +0530 | [diff] [blame] | 285 | |
Vignesh Raghavendra | e995dbb | 2019-12-11 18:59:36 +0530 | [diff] [blame] | 286 | static void ti_qspi_setup_mmap_read(struct ti_qspi_priv *priv, int cs, |
| 287 | u8 opcode, u8 data_nbits, u8 addr_width, |
Vignesh Raghavendra | 6f9efcf | 2019-04-16 21:32:00 +0530 | [diff] [blame] | 288 | u8 dummy_bytes) |
Mugunthan V N | 540a115 | 2015-12-23 20:39:40 +0530 | [diff] [blame] | 289 | { |
Vignesh Raghavendra | 6f9efcf | 2019-04-16 21:32:00 +0530 | [diff] [blame] | 290 | u32 memval = opcode; |
Mugunthan V N | 540a115 | 2015-12-23 20:39:40 +0530 | [diff] [blame] | 291 | |
Vignesh Raghavendra | 6f9efcf | 2019-04-16 21:32:00 +0530 | [diff] [blame] | 292 | switch (data_nbits) { |
| 293 | case 4: |
Mugunthan V N | 540a115 | 2015-12-23 20:39:40 +0530 | [diff] [blame] | 294 | memval |= QSPI_SETUP0_READ_QUAD; |
Mugunthan V N | 540a115 | 2015-12-23 20:39:40 +0530 | [diff] [blame] | 295 | break; |
Vignesh Raghavendra | 6f9efcf | 2019-04-16 21:32:00 +0530 | [diff] [blame] | 296 | case 2: |
Mugunthan V N | 540a115 | 2015-12-23 20:39:40 +0530 | [diff] [blame] | 297 | memval |= QSPI_SETUP0_READ_DUAL; |
| 298 | break; |
| 299 | default: |
Mugunthan V N | 540a115 | 2015-12-23 20:39:40 +0530 | [diff] [blame] | 300 | memval |= QSPI_SETUP0_READ_NORMAL; |
| 301 | break; |
| 302 | } |
| 303 | |
Vignesh Raghavendra | 6f9efcf | 2019-04-16 21:32:00 +0530 | [diff] [blame] | 304 | memval |= ((addr_width - 1) << QSPI_SETUP0_ADDR_SHIFT | |
| 305 | dummy_bytes << QSPI_SETUP0_DBITS_SHIFT); |
| 306 | |
Vignesh Raghavendra | e995dbb | 2019-12-11 18:59:36 +0530 | [diff] [blame] | 307 | writel(memval, TI_QSPI_SETUP_REG(priv, cs)); |
Mugunthan V N | 540a115 | 2015-12-23 20:39:40 +0530 | [diff] [blame] | 308 | } |
| 309 | |
Vignesh Raghavendra | f3603b8 | 2019-04-16 21:31:59 +0530 | [diff] [blame] | 310 | static int ti_qspi_set_mode(struct udevice *bus, uint mode) |
Mugunthan V N | 540a115 | 2015-12-23 20:39:40 +0530 | [diff] [blame] | 311 | { |
| 312 | struct ti_qspi_priv *priv = dev_get_priv(bus); |
| 313 | |
Vignesh Raghavendra | f3603b8 | 2019-04-16 21:31:59 +0530 | [diff] [blame] | 314 | priv->dc = 0; |
| 315 | if (mode & SPI_CPHA) |
| 316 | priv->dc |= QSPI_CKPHA(0); |
| 317 | if (mode & SPI_CPOL) |
| 318 | priv->dc |= QSPI_CKPOL(0); |
| 319 | if (mode & SPI_CS_HIGH) |
| 320 | priv->dc |= QSPI_CSPOL(0); |
Mugunthan V N | 540a115 | 2015-12-23 20:39:40 +0530 | [diff] [blame] | 321 | |
| 322 | return 0; |
| 323 | } |
| 324 | |
Vignesh Raghavendra | 6f9efcf | 2019-04-16 21:32:00 +0530 | [diff] [blame] | 325 | static int ti_qspi_exec_mem_op(struct spi_slave *slave, |
| 326 | const struct spi_mem_op *op) |
| 327 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 328 | struct dm_spi_slave_plat *slave_plat; |
Vignesh Raghavendra | 6f9efcf | 2019-04-16 21:32:00 +0530 | [diff] [blame] | 329 | struct ti_qspi_priv *priv; |
| 330 | struct udevice *bus; |
Vignesh Raghavendra | e995dbb | 2019-12-11 18:59:36 +0530 | [diff] [blame] | 331 | u32 from = 0; |
| 332 | int ret = 0; |
Vignesh Raghavendra | 6f9efcf | 2019-04-16 21:32:00 +0530 | [diff] [blame] | 333 | |
| 334 | bus = slave->dev->parent; |
| 335 | priv = dev_get_priv(bus); |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 336 | slave_plat = dev_get_parent_plat(slave->dev); |
Vignesh Raghavendra | 6f9efcf | 2019-04-16 21:32:00 +0530 | [diff] [blame] | 337 | |
| 338 | /* Only optimize read path. */ |
| 339 | if (!op->data.nbytes || op->data.dir != SPI_MEM_DATA_IN || |
| 340 | !op->addr.nbytes || op->addr.nbytes > 4) |
| 341 | return -ENOTSUPP; |
| 342 | |
| 343 | /* Address exceeds MMIO window size, fall back to regular mode. */ |
| 344 | from = op->addr.val; |
| 345 | if (from + op->data.nbytes > priv->mmap_size) |
| 346 | return -ENOTSUPP; |
| 347 | |
Vignesh Raghavendra | e995dbb | 2019-12-11 18:59:36 +0530 | [diff] [blame] | 348 | ti_qspi_setup_mmap_read(priv, slave_plat->cs, op->cmd.opcode, |
| 349 | op->data.buswidth, op->addr.nbytes, |
| 350 | op->dummy.nbytes); |
Vignesh Raghavendra | 6f9efcf | 2019-04-16 21:32:00 +0530 | [diff] [blame] | 351 | |
| 352 | ti_qspi_copy_mmap((void *)op->data.buf.in, |
| 353 | (void *)priv->memory_map + from, op->data.nbytes); |
| 354 | |
| 355 | return ret; |
| 356 | } |
| 357 | |
Mugunthan V N | 540a115 | 2015-12-23 20:39:40 +0530 | [diff] [blame] | 358 | static int ti_qspi_claim_bus(struct udevice *dev) |
| 359 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 360 | struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev); |
Mugunthan V N | 540a115 | 2015-12-23 20:39:40 +0530 | [diff] [blame] | 361 | struct ti_qspi_priv *priv; |
| 362 | struct udevice *bus; |
| 363 | |
| 364 | bus = dev->parent; |
| 365 | priv = dev_get_priv(bus); |
| 366 | |
| 367 | if (slave_plat->cs > priv->num_cs) { |
| 368 | debug("invalid qspi chip select\n"); |
| 369 | return -EINVAL; |
| 370 | } |
| 371 | |
Vignesh Raghavendra | 6f9efcf | 2019-04-16 21:32:00 +0530 | [diff] [blame] | 372 | writel(MM_SWITCH, &priv->base->memswitch); |
| 373 | if (priv->ctrl_mod_mmap) |
| 374 | ti_qspi_ctrl_mode_mmap(priv->ctrl_mod_mmap, |
| 375 | slave_plat->cs, true); |
Mugunthan V N | 540a115 | 2015-12-23 20:39:40 +0530 | [diff] [blame] | 376 | |
Vignesh Raghavendra | f3603b8 | 2019-04-16 21:31:59 +0530 | [diff] [blame] | 377 | writel(priv->dc, &priv->base->dc); |
| 378 | writel(0, &priv->base->cmd); |
| 379 | writel(0, &priv->base->data); |
| 380 | |
| 381 | priv->dc <<= slave_plat->cs * 8; |
| 382 | writel(priv->dc, &priv->base->dc); |
| 383 | |
| 384 | return 0; |
Mugunthan V N | 540a115 | 2015-12-23 20:39:40 +0530 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | static int ti_qspi_release_bus(struct udevice *dev) |
| 388 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 389 | struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev); |
Mugunthan V N | 540a115 | 2015-12-23 20:39:40 +0530 | [diff] [blame] | 390 | struct ti_qspi_priv *priv; |
| 391 | struct udevice *bus; |
| 392 | |
| 393 | bus = dev->parent; |
| 394 | priv = dev_get_priv(bus); |
| 395 | |
Vignesh Raghavendra | 6f9efcf | 2019-04-16 21:32:00 +0530 | [diff] [blame] | 396 | writel(~MM_SWITCH, &priv->base->memswitch); |
| 397 | if (priv->ctrl_mod_mmap) |
| 398 | ti_qspi_ctrl_mode_mmap(priv->ctrl_mod_mmap, |
| 399 | slave_plat->cs, false); |
Mugunthan V N | 540a115 | 2015-12-23 20:39:40 +0530 | [diff] [blame] | 400 | |
Vignesh Raghavendra | f3603b8 | 2019-04-16 21:31:59 +0530 | [diff] [blame] | 401 | writel(0, &priv->base->dc); |
| 402 | writel(0, &priv->base->cmd); |
| 403 | writel(0, &priv->base->data); |
Vignesh Raghavendra | e995dbb | 2019-12-11 18:59:36 +0530 | [diff] [blame] | 404 | writel(0, TI_QSPI_SETUP_REG(priv, slave_plat->cs)); |
Mugunthan V N | 540a115 | 2015-12-23 20:39:40 +0530 | [diff] [blame] | 405 | |
Vignesh Raghavendra | f3603b8 | 2019-04-16 21:31:59 +0530 | [diff] [blame] | 406 | return 0; |
Mugunthan V N | 540a115 | 2015-12-23 20:39:40 +0530 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | static int ti_qspi_probe(struct udevice *bus) |
| 410 | { |
Vignesh R | 1535e5a | 2016-07-25 15:45:45 +0530 | [diff] [blame] | 411 | struct ti_qspi_priv *priv = dev_get_priv(bus); |
| 412 | |
| 413 | priv->fclk = dev_get_driver_data(bus); |
| 414 | |
Mugunthan V N | 540a115 | 2015-12-23 20:39:40 +0530 | [diff] [blame] | 415 | return 0; |
| 416 | } |
| 417 | |
Jean-Jacques Hiblot | 931b1f2 | 2017-02-13 16:17:49 +0100 | [diff] [blame] | 418 | static void *map_syscon_chipselects(struct udevice *bus) |
| 419 | { |
| 420 | #if CONFIG_IS_ENABLED(SYSCON) |
| 421 | struct udevice *syscon; |
| 422 | struct regmap *regmap; |
| 423 | const fdt32_t *cell; |
| 424 | int len, err; |
| 425 | |
| 426 | err = uclass_get_device_by_phandle(UCLASS_SYSCON, bus, |
| 427 | "syscon-chipselects", &syscon); |
| 428 | if (err) { |
| 429 | debug("%s: unable to find syscon device (%d)\n", __func__, |
| 430 | err); |
| 431 | return NULL; |
| 432 | } |
| 433 | |
| 434 | regmap = syscon_get_regmap(syscon); |
| 435 | if (IS_ERR(regmap)) { |
| 436 | debug("%s: unable to find regmap (%ld)\n", __func__, |
| 437 | PTR_ERR(regmap)); |
| 438 | return NULL; |
| 439 | } |
| 440 | |
Simon Glass | 7a49443 | 2017-05-17 17:18:09 -0600 | [diff] [blame] | 441 | cell = fdt_getprop(gd->fdt_blob, dev_of_offset(bus), |
| 442 | "syscon-chipselects", &len); |
Jean-Jacques Hiblot | 931b1f2 | 2017-02-13 16:17:49 +0100 | [diff] [blame] | 443 | if (len < 2*sizeof(fdt32_t)) { |
| 444 | debug("%s: offset not available\n", __func__); |
| 445 | return NULL; |
| 446 | } |
| 447 | |
| 448 | return fdtdec_get_number(cell + 1, 1) + regmap_get_range(regmap, 0); |
| 449 | #else |
| 450 | fdt_addr_t addr; |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 451 | addr = devfdt_get_addr_index(bus, 2); |
Jean-Jacques Hiblot | 931b1f2 | 2017-02-13 16:17:49 +0100 | [diff] [blame] | 452 | return (addr == FDT_ADDR_T_NONE) ? NULL : |
| 453 | map_physmem(addr, 0, MAP_NOCACHE); |
| 454 | #endif |
| 455 | } |
| 456 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 457 | static int ti_qspi_of_to_plat(struct udevice *bus) |
Mugunthan V N | 540a115 | 2015-12-23 20:39:40 +0530 | [diff] [blame] | 458 | { |
| 459 | struct ti_qspi_priv *priv = dev_get_priv(bus); |
| 460 | const void *blob = gd->fdt_blob; |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 461 | int node = dev_of_offset(bus); |
Vignesh Raghavendra | 6f9efcf | 2019-04-16 21:32:00 +0530 | [diff] [blame] | 462 | fdt_addr_t mmap_addr; |
| 463 | fdt_addr_t mmap_size; |
Mugunthan V N | 540a115 | 2015-12-23 20:39:40 +0530 | [diff] [blame] | 464 | |
Jean-Jacques Hiblot | 931b1f2 | 2017-02-13 16:17:49 +0100 | [diff] [blame] | 465 | priv->ctrl_mod_mmap = map_syscon_chipselects(bus); |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 466 | priv->base = map_physmem(dev_read_addr(bus), |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 467 | sizeof(struct ti_qspi_regs), MAP_NOCACHE); |
Vignesh Raghavendra | 6f9efcf | 2019-04-16 21:32:00 +0530 | [diff] [blame] | 468 | mmap_addr = devfdt_get_addr_size_index(bus, 1, &mmap_size); |
| 469 | priv->memory_map = map_physmem(mmap_addr, mmap_size, MAP_NOCACHE); |
| 470 | priv->mmap_size = mmap_size; |
Mugunthan V N | 540a115 | 2015-12-23 20:39:40 +0530 | [diff] [blame] | 471 | |
Ovidiu Panait | 7cb5159 | 2020-11-28 10:11:28 +0200 | [diff] [blame] | 472 | priv->max_hz = dev_read_u32_default(bus, "spi-max-frequency", 0); |
| 473 | if (!priv->max_hz) { |
Mugunthan V N | 540a115 | 2015-12-23 20:39:40 +0530 | [diff] [blame] | 474 | debug("Error: Max frequency missing\n"); |
| 475 | return -ENODEV; |
| 476 | } |
| 477 | priv->num_cs = fdtdec_get_int(blob, node, "num-cs", 4); |
| 478 | |
| 479 | debug("%s: regs=<0x%x>, max-frequency=%d\n", __func__, |
| 480 | (int)priv->base, priv->max_hz); |
| 481 | |
| 482 | return 0; |
| 483 | } |
| 484 | |
Vignesh Raghavendra | 6f9efcf | 2019-04-16 21:32:00 +0530 | [diff] [blame] | 485 | static const struct spi_controller_mem_ops ti_qspi_mem_ops = { |
| 486 | .exec_op = ti_qspi_exec_mem_op, |
| 487 | }; |
Mugunthan V N | 540a115 | 2015-12-23 20:39:40 +0530 | [diff] [blame] | 488 | |
| 489 | static const struct dm_spi_ops ti_qspi_ops = { |
| 490 | .claim_bus = ti_qspi_claim_bus, |
| 491 | .release_bus = ti_qspi_release_bus, |
| 492 | .xfer = ti_qspi_xfer, |
| 493 | .set_speed = ti_qspi_set_speed, |
| 494 | .set_mode = ti_qspi_set_mode, |
Vignesh Raghavendra | 6f9efcf | 2019-04-16 21:32:00 +0530 | [diff] [blame] | 495 | .mem_ops = &ti_qspi_mem_ops, |
Mugunthan V N | 540a115 | 2015-12-23 20:39:40 +0530 | [diff] [blame] | 496 | }; |
| 497 | |
| 498 | static const struct udevice_id ti_qspi_ids[] = { |
Vignesh R | 1535e5a | 2016-07-25 15:45:45 +0530 | [diff] [blame] | 499 | { .compatible = "ti,dra7xxx-qspi", .data = QSPI_DRA7XX_FCLK}, |
| 500 | { .compatible = "ti,am4372-qspi", .data = QSPI_FCLK}, |
Mugunthan V N | 540a115 | 2015-12-23 20:39:40 +0530 | [diff] [blame] | 501 | { } |
| 502 | }; |
| 503 | |
| 504 | U_BOOT_DRIVER(ti_qspi) = { |
| 505 | .name = "ti_qspi", |
| 506 | .id = UCLASS_SPI, |
| 507 | .of_match = ti_qspi_ids, |
| 508 | .ops = &ti_qspi_ops, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 509 | .of_to_plat = ti_qspi_of_to_plat, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 510 | .priv_auto = sizeof(struct ti_qspi_priv), |
Mugunthan V N | 540a115 | 2015-12-23 20:39:40 +0530 | [diff] [blame] | 511 | .probe = ti_qspi_probe, |
Mugunthan V N | 540a115 | 2015-12-23 20:39:40 +0530 | [diff] [blame] | 512 | }; |