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