blob: 1f2494e592c74aa8218a35913a91d27291e89baa [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Matt Porterda3e4c62013-10-07 15:53:02 +05302/*
3 * TI QSPI driver
4 *
5 * Copyright (C) 2013, Texas Instruments, Incorporated
Matt Porterda3e4c62013-10-07 15:53:02 +05306 */
7
Simon Glass63334482019-11-14 12:57:39 -07008#include <cpu_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Simon Glass274e0b02020-05-10 11:39:56 -060010#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060011#include <asm/global_data.h>
Matt Porterda3e4c62013-10-07 15:53:02 +053012#include <asm/io.h>
13#include <asm/arch/omap.h>
14#include <malloc.h>
15#include <spi.h>
Vignesh Raghavendra6f9efcf2019-04-16 21:32:00 +053016#include <spi-mem.h>
Mugunthan V N540a1152015-12-23 20:39:40 +053017#include <dm.h>
Sourav Poddar2145dff2013-12-21 12:50:09 +053018#include <asm/gpio.h>
19#include <asm/omap_gpio.h>
Vignesh Ra5bba8d2015-08-17 15:20:13 +053020#include <asm/omap_common.h>
21#include <asm/ti-common/ti-edma3.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060022#include <linux/bitops.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070023#include <linux/err.h>
Vignesh R44e4bac2016-11-05 16:05:16 +053024#include <linux/kernel.h>
Jean-Jacques Hiblot931b1f22017-02-13 16:17:49 +010025#include <regmap.h>
26#include <syscon.h>
Matt Porterda3e4c62013-10-07 15:53:02 +053027
Mugunthan V N540a1152015-12-23 20:39:40 +053028DECLARE_GLOBAL_DATA_PTR;
29
Matt Porterda3e4c62013-10-07 15:53:02 +053030/* ti qpsi register bit masks */
31#define QSPI_TIMEOUT 2000000
Stefan Mätje1560b762021-11-30 01:06:56 +010032/* AM4372: QSPI gets SPI_GCLK from PRCM unit as PER_CLKOUTM2 divided by 4. */
33#define QSPI_FCLK (192000000 / 4)
Vignesh R1535e5a2016-07-25 15:45:45 +053034#define QSPI_DRA7XX_FCLK 76800000
Vignesh Rb0079972016-09-07 15:18:22 +053035#define QSPI_WLEN_MAX_BITS 128
36#define QSPI_WLEN_MAX_BYTES (QSPI_WLEN_MAX_BITS >> 3)
37#define QSPI_WLEN_MASK QSPI_WLEN(QSPI_WLEN_MAX_BITS)
Matt Porterda3e4c62013-10-07 15:53:02 +053038/* clock control */
Jagan Tekif16e4db2015-10-23 01:39:20 +053039#define QSPI_CLK_EN BIT(31)
Matt Porterda3e4c62013-10-07 15:53:02 +053040#define QSPI_CLK_DIV_MAX 0xffff
41/* command */
42#define QSPI_EN_CS(n) (n << 28)
43#define QSPI_WLEN(n) ((n-1) << 19)
Jagan Tekif16e4db2015-10-23 01:39:20 +053044#define QSPI_3_PIN BIT(18)
45#define QSPI_RD_SNGL BIT(16)
Matt Porterda3e4c62013-10-07 15:53:02 +053046#define QSPI_WR_SNGL (2 << 16)
47#define QSPI_INVAL (4 << 16)
48#define QSPI_RD_QUAD (7 << 16)
49/* device control */
Matt Porterda3e4c62013-10-07 15:53:02 +053050#define QSPI_CKPHA(n) (1 << (2 + n*8))
51#define QSPI_CSPOL(n) (1 << (1 + n*8))
52#define QSPI_CKPOL(n) (1 << (n*8))
53/* status */
Jagan Tekif16e4db2015-10-23 01:39:20 +053054#define QSPI_WC BIT(1)
55#define QSPI_BUSY BIT(0)
Matt Porterda3e4c62013-10-07 15:53:02 +053056#define QSPI_WC_BUSY (QSPI_WC | QSPI_BUSY)
57#define QSPI_XFER_DONE QSPI_WC
58#define MM_SWITCH 0x01
Mugunthan V N132e0072015-12-23 20:39:33 +053059#define MEM_CS(cs) ((cs + 1) << 8)
Praneeth Bajjuri5d21e6f2016-06-21 14:05:36 +053060#define MEM_CS_UNSELECT 0xfffff8ff
Matt Porterda3e4c62013-10-07 15:53:02 +053061
Matt Porterda3e4c62013-10-07 15:53:02 +053062#define QSPI_SETUP0_READ_NORMAL (0x0 << 12)
Mugunthan V N540a1152015-12-23 20:39:40 +053063#define QSPI_SETUP0_READ_DUAL (0x1 << 12)
Matt Porterda3e4c62013-10-07 15:53:02 +053064#define QSPI_SETUP0_READ_QUAD (0x3 << 12)
Vignesh Raghavendra6f9efcf2019-04-16 21:32:00 +053065#define QSPI_SETUP0_ADDR_SHIFT (8)
66#define QSPI_SETUP0_DBITS_SHIFT (10)
Matt Porterda3e4c62013-10-07 15:53:02 +053067
Vignesh Raghavendrae995dbb2019-12-11 18:59:36 +053068#define TI_QSPI_SETUP_REG(priv, cs) (&(priv)->base->setup0 + (cs))
69
Matt Porterda3e4c62013-10-07 15:53:02 +053070/* ti qspi register set */
71struct ti_qspi_regs {
72 u32 pid;
73 u32 pad0[3];
74 u32 sysconfig;
75 u32 pad1[3];
76 u32 int_stat_raw;
77 u32 int_stat_en;
78 u32 int_en_set;
79 u32 int_en_ctlr;
80 u32 intc_eoi;
81 u32 pad2[3];
82 u32 clk_ctrl;
83 u32 dc;
84 u32 cmd;
85 u32 status;
86 u32 data;
87 u32 setup0;
88 u32 setup1;
89 u32 setup2;
90 u32 setup3;
91 u32 memswitch;
92 u32 data1;
93 u32 data2;
94 u32 data3;
95};
96
Mugunthan V Ne206d302015-12-23 20:39:34 +053097/* ti qspi priv */
98struct ti_qspi_priv {
Mugunthan V N540a1152015-12-23 20:39:40 +053099 void *memory_map;
Vignesh Raghavendra6f9efcf2019-04-16 21:32:00 +0530100 size_t mmap_size;
Mugunthan V N540a1152015-12-23 20:39:40 +0530101 uint max_hz;
102 u32 num_cs;
Matt Porterda3e4c62013-10-07 15:53:02 +0530103 struct ti_qspi_regs *base;
Mugunthan V Ncd467732015-12-23 20:39:35 +0530104 void *ctrl_mod_mmap;
Vignesh R1535e5a2016-07-25 15:45:45 +0530105 ulong fclk;
Matt Porterda3e4c62013-10-07 15:53:02 +0530106 unsigned int mode;
107 u32 cmd;
108 u32 dc;
109};
110
Vignesh Raghavendraf3603b82019-04-16 21:31:59 +0530111static int ti_qspi_set_speed(struct udevice *bus, uint hz)
Matt Porterda3e4c62013-10-07 15:53:02 +0530112{
Vignesh Raghavendraf3603b82019-04-16 21:31:59 +0530113 struct ti_qspi_priv *priv = dev_get_priv(bus);
Matt Porterda3e4c62013-10-07 15:53:02 +0530114 uint clk_div;
115
Matt Porterda3e4c62013-10-07 15:53:02 +0530116 if (!hz)
117 clk_div = 0;
118 else
Vignesh R44e4bac2016-11-05 16:05:16 +0530119 clk_div = DIV_ROUND_UP(priv->fclk, hz) - 1;
120
121 /* truncate clk_div value to QSPI_CLK_DIV_MAX */
122 if (clk_div > QSPI_CLK_DIV_MAX)
123 clk_div = QSPI_CLK_DIV_MAX;
Matt Porterda3e4c62013-10-07 15:53:02 +0530124
Vignesh Rb9707672016-07-22 10:55:49 +0530125 debug("ti_spi_set_speed: hz: %d, clock divider %d\n", hz, clk_div);
126
Matt Porterda3e4c62013-10-07 15:53:02 +0530127 /* disable SCLK */
Mugunthan V Ne206d302015-12-23 20:39:34 +0530128 writel(readl(&priv->base->clk_ctrl) & ~QSPI_CLK_EN,
129 &priv->base->clk_ctrl);
Vignesh R44e4bac2016-11-05 16:05:16 +0530130 /* enable SCLK and program the clk divider */
Mugunthan V Ne206d302015-12-23 20:39:34 +0530131 writel(QSPI_CLK_EN | clk_div, &priv->base->clk_ctrl);
Vignesh Raghavendraf3603b82019-04-16 21:31:59 +0530132
133 return 0;
Matt Porterda3e4c62013-10-07 15:53:02 +0530134}
135
Mugunthan V Ncd467732015-12-23 20:39:35 +0530136static void ti_qspi_cs_deactivate(struct ti_qspi_priv *priv)
Matt Porterda3e4c62013-10-07 15:53:02 +0530137{
Mugunthan V Ne206d302015-12-23 20:39:34 +0530138 writel(priv->cmd | QSPI_INVAL, &priv->base->cmd);
Vignesh R43ad9ce2015-11-10 11:52:10 +0530139 /* dummy readl to ensure bus sync */
Mugunthan V Ncd467732015-12-23 20:39:35 +0530140 readl(&priv->base->cmd);
Matt Porterda3e4c62013-10-07 15:53:02 +0530141}
142
Mugunthan V Ncd467732015-12-23 20:39:35 +0530143static void ti_qspi_ctrl_mode_mmap(void *ctrl_mod_mmap, int cs, bool enable)
Matt Porterda3e4c62013-10-07 15:53:02 +0530144{
Mugunthan V Ncd467732015-12-23 20:39:35 +0530145 u32 val;
146
147 val = readl(ctrl_mod_mmap);
148 if (enable)
149 val |= MEM_CS(cs);
150 else
151 val &= MEM_CS_UNSELECT;
152 writel(val, ctrl_mod_mmap);
153}
154
Vignesh Raghavendraf3603b82019-04-16 21:31:59 +0530155static int ti_qspi_xfer(struct udevice *dev, unsigned int bitlen,
156 const void *dout, void *din, unsigned long flags)
Mugunthan V Ncd467732015-12-23 20:39:35 +0530157{
Simon Glassb75b15b2020-12-03 16:55:23 -0700158 struct dm_spi_slave_plat *slave = dev_get_parent_plat(dev);
Vignesh Raghavendraf3603b82019-04-16 21:31:59 +0530159 struct ti_qspi_priv *priv;
160 struct udevice *bus;
Matt Porterda3e4c62013-10-07 15:53:02 +0530161 uint words = bitlen >> 3; /* fixed 8-bit word length */
162 const uchar *txp = dout;
163 uchar *rxp = din;
164 uint status;
Sourav Poddar2145dff2013-12-21 12:50:09 +0530165 int timeout;
Venkatesh Yadav Abbarapu91b9e372024-09-26 10:25:05 +0530166 unsigned int cs = slave->cs[0];
Vignesh Raghavendraf3603b82019-04-16 21:31:59 +0530167
168 bus = dev->parent;
169 priv = dev_get_priv(bus);
170
171 if (cs > priv->num_cs) {
172 debug("invalid qspi chip select\n");
173 return -EINVAL;
174 }
Sourav Poddar2145dff2013-12-21 12:50:09 +0530175
Matt Porterda3e4c62013-10-07 15:53:02 +0530176 if (bitlen == 0)
177 return -1;
178
179 if (bitlen % 8) {
180 debug("spi_xfer: Non byte aligned SPI transfer\n");
181 return -1;
182 }
183
184 /* Setup command reg */
Mugunthan V Ne206d302015-12-23 20:39:34 +0530185 priv->cmd = 0;
186 priv->cmd |= QSPI_WLEN(8);
Mugunthan V Ncd467732015-12-23 20:39:35 +0530187 priv->cmd |= QSPI_EN_CS(cs);
Mugunthan V Ne206d302015-12-23 20:39:34 +0530188 if (priv->mode & SPI_3WIRE)
189 priv->cmd |= QSPI_3_PIN;
190 priv->cmd |= 0xfff;
Matt Porterda3e4c62013-10-07 15:53:02 +0530191
Vignesh Rb0079972016-09-07 15:18:22 +0530192 while (words) {
193 u8 xfer_len = 0;
194
Matt Porterda3e4c62013-10-07 15:53:02 +0530195 if (txp) {
Vignesh Rb0079972016-09-07 15:18:22 +0530196 u32 cmd = priv->cmd;
197
198 if (words >= QSPI_WLEN_MAX_BYTES) {
199 u32 *txbuf = (u32 *)txp;
200 u32 data;
201
202 data = cpu_to_be32(*txbuf++);
203 writel(data, &priv->base->data3);
204 data = cpu_to_be32(*txbuf++);
205 writel(data, &priv->base->data2);
206 data = cpu_to_be32(*txbuf++);
207 writel(data, &priv->base->data1);
208 data = cpu_to_be32(*txbuf++);
209 writel(data, &priv->base->data);
210 cmd &= ~QSPI_WLEN_MASK;
211 cmd |= QSPI_WLEN(QSPI_WLEN_MAX_BITS);
212 xfer_len = QSPI_WLEN_MAX_BYTES;
213 } else {
214 writeb(*txp, &priv->base->data);
215 xfer_len = 1;
216 }
217 debug("tx cmd %08x dc %08x\n",
218 cmd | QSPI_WR_SNGL, priv->dc);
219 writel(cmd | QSPI_WR_SNGL, &priv->base->cmd);
Mugunthan V Ne206d302015-12-23 20:39:34 +0530220 status = readl(&priv->base->status);
Matt Porterda3e4c62013-10-07 15:53:02 +0530221 timeout = QSPI_TIMEOUT;
222 while ((status & QSPI_WC_BUSY) != QSPI_XFER_DONE) {
223 if (--timeout < 0) {
224 printf("spi_xfer: TX timeout!\n");
225 return -1;
226 }
Mugunthan V Ne206d302015-12-23 20:39:34 +0530227 status = readl(&priv->base->status);
Matt Porterda3e4c62013-10-07 15:53:02 +0530228 }
Vignesh Rb0079972016-09-07 15:18:22 +0530229 txp += xfer_len;
Matt Porterda3e4c62013-10-07 15:53:02 +0530230 debug("tx done, status %08x\n", status);
231 }
232 if (rxp) {
Matt Porterda3e4c62013-10-07 15:53:02 +0530233 debug("rx cmd %08x dc %08x\n",
Vignesh Raa291302016-07-22 10:55:48 +0530234 ((u32)(priv->cmd | QSPI_RD_SNGL)), priv->dc);
Vignesh Raa291302016-07-22 10:55:48 +0530235 writel(priv->cmd | QSPI_RD_SNGL, &priv->base->cmd);
Mugunthan V Ne206d302015-12-23 20:39:34 +0530236 status = readl(&priv->base->status);
Matt Porterda3e4c62013-10-07 15:53:02 +0530237 timeout = QSPI_TIMEOUT;
238 while ((status & QSPI_WC_BUSY) != QSPI_XFER_DONE) {
239 if (--timeout < 0) {
240 printf("spi_xfer: RX timeout!\n");
241 return -1;
242 }
Mugunthan V Ne206d302015-12-23 20:39:34 +0530243 status = readl(&priv->base->status);
Matt Porterda3e4c62013-10-07 15:53:02 +0530244 }
Mugunthan V Ne206d302015-12-23 20:39:34 +0530245 *rxp++ = readl(&priv->base->data);
Vignesh Rb0079972016-09-07 15:18:22 +0530246 xfer_len = 1;
Matt Porterda3e4c62013-10-07 15:53:02 +0530247 debug("rx done, status %08x, read %02x\n",
248 status, *(rxp-1));
249 }
Vignesh Rb0079972016-09-07 15:18:22 +0530250 words -= xfer_len;
Matt Porterda3e4c62013-10-07 15:53:02 +0530251 }
252
253 /* Terminate frame */
254 if (flags & SPI_XFER_END)
Mugunthan V Ncd467732015-12-23 20:39:35 +0530255 ti_qspi_cs_deactivate(priv);
Matt Porterda3e4c62013-10-07 15:53:02 +0530256
257 return 0;
258}
Vignesh Ra5bba8d2015-08-17 15:20:13 +0530259
260/* TODO: control from sf layer to here through dm-spi */
Vignesh Raghavendra6f9efcf2019-04-16 21:32:00 +0530261static void ti_qspi_copy_mmap(void *data, void *offset, size_t len)
Vignesh Ra5bba8d2015-08-17 15:20:13 +0530262{
Vignesh Raghavendra6f9efcf2019-04-16 21:32:00 +0530263#if defined(CONFIG_TI_EDMA3) && !defined(CONFIG_DMA)
Vignesh Ra5bba8d2015-08-17 15:20:13 +0530264 unsigned int addr = (unsigned int) (data);
265 unsigned int edma_slot_num = 1;
266
267 /* Invalidate the area, so no writeback into the RAM races with DMA */
268 invalidate_dcache_range(addr, addr + roundup(len, ARCH_DMA_MINALIGN));
269
270 /* enable edma3 clocks */
271 enable_edma3_clocks();
272
273 /* Call edma3 api to do actual DMA transfer */
274 edma3_transfer(EDMA3_BASE, edma_slot_num, data, offset, len);
275
276 /* disable edma3 clocks */
277 disable_edma3_clocks();
Vignesh Raghavendra6f9efcf2019-04-16 21:32:00 +0530278#else
279 memcpy_fromio(data, offset, len);
280#endif
Vignesh Ra5bba8d2015-08-17 15:20:13 +0530281
282 *((unsigned int *)offset) += len;
283}
Mugunthan V Ncd467732015-12-23 20:39:35 +0530284
Vignesh Raghavendrae995dbb2019-12-11 18:59:36 +0530285static void ti_qspi_setup_mmap_read(struct ti_qspi_priv *priv, int cs,
286 u8 opcode, u8 data_nbits, u8 addr_width,
Vignesh Raghavendra6f9efcf2019-04-16 21:32:00 +0530287 u8 dummy_bytes)
Mugunthan V N540a1152015-12-23 20:39:40 +0530288{
Vignesh Raghavendra6f9efcf2019-04-16 21:32:00 +0530289 u32 memval = opcode;
Mugunthan V N540a1152015-12-23 20:39:40 +0530290
Vignesh Raghavendra6f9efcf2019-04-16 21:32:00 +0530291 switch (data_nbits) {
292 case 4:
Mugunthan V N540a1152015-12-23 20:39:40 +0530293 memval |= QSPI_SETUP0_READ_QUAD;
Mugunthan V N540a1152015-12-23 20:39:40 +0530294 break;
Vignesh Raghavendra6f9efcf2019-04-16 21:32:00 +0530295 case 2:
Mugunthan V N540a1152015-12-23 20:39:40 +0530296 memval |= QSPI_SETUP0_READ_DUAL;
297 break;
298 default:
Mugunthan V N540a1152015-12-23 20:39:40 +0530299 memval |= QSPI_SETUP0_READ_NORMAL;
300 break;
301 }
302
Vignesh Raghavendra6f9efcf2019-04-16 21:32:00 +0530303 memval |= ((addr_width - 1) << QSPI_SETUP0_ADDR_SHIFT |
304 dummy_bytes << QSPI_SETUP0_DBITS_SHIFT);
305
Vignesh Raghavendrae995dbb2019-12-11 18:59:36 +0530306 writel(memval, TI_QSPI_SETUP_REG(priv, cs));
Mugunthan V N540a1152015-12-23 20:39:40 +0530307}
308
Vignesh Raghavendraf3603b82019-04-16 21:31:59 +0530309static int ti_qspi_set_mode(struct udevice *bus, uint mode)
Mugunthan V N540a1152015-12-23 20:39:40 +0530310{
311 struct ti_qspi_priv *priv = dev_get_priv(bus);
312
Vignesh Raghavendraf3603b82019-04-16 21:31:59 +0530313 priv->dc = 0;
314 if (mode & SPI_CPHA)
315 priv->dc |= QSPI_CKPHA(0);
316 if (mode & SPI_CPOL)
317 priv->dc |= QSPI_CKPOL(0);
318 if (mode & SPI_CS_HIGH)
319 priv->dc |= QSPI_CSPOL(0);
Mugunthan V N540a1152015-12-23 20:39:40 +0530320
321 return 0;
322}
323
Vignesh Raghavendra6f9efcf2019-04-16 21:32:00 +0530324static int ti_qspi_exec_mem_op(struct spi_slave *slave,
325 const struct spi_mem_op *op)
326{
Simon Glassb75b15b2020-12-03 16:55:23 -0700327 struct dm_spi_slave_plat *slave_plat;
Vignesh Raghavendra6f9efcf2019-04-16 21:32:00 +0530328 struct ti_qspi_priv *priv;
329 struct udevice *bus;
Vignesh Raghavendrae995dbb2019-12-11 18:59:36 +0530330 u32 from = 0;
331 int ret = 0;
Vignesh Raghavendra6f9efcf2019-04-16 21:32:00 +0530332
333 bus = slave->dev->parent;
334 priv = dev_get_priv(bus);
Simon Glass71fa5b42020-12-03 16:55:18 -0700335 slave_plat = dev_get_parent_plat(slave->dev);
Vignesh Raghavendra6f9efcf2019-04-16 21:32:00 +0530336
337 /* Only optimize read path. */
338 if (!op->data.nbytes || op->data.dir != SPI_MEM_DATA_IN ||
339 !op->addr.nbytes || op->addr.nbytes > 4)
340 return -ENOTSUPP;
341
342 /* Address exceeds MMIO window size, fall back to regular mode. */
343 from = op->addr.val;
344 if (from + op->data.nbytes > priv->mmap_size)
345 return -ENOTSUPP;
346
Venkatesh Yadav Abbarapu91b9e372024-09-26 10:25:05 +0530347 ti_qspi_setup_mmap_read(priv, slave_plat->cs[0], op->cmd.opcode,
Vignesh Raghavendrae995dbb2019-12-11 18:59:36 +0530348 op->data.buswidth, op->addr.nbytes,
349 op->dummy.nbytes);
Vignesh Raghavendra6f9efcf2019-04-16 21:32:00 +0530350
351 ti_qspi_copy_mmap((void *)op->data.buf.in,
352 (void *)priv->memory_map + from, op->data.nbytes);
353
354 return ret;
355}
356
Mugunthan V N540a1152015-12-23 20:39:40 +0530357static int ti_qspi_claim_bus(struct udevice *dev)
358{
Simon Glassb75b15b2020-12-03 16:55:23 -0700359 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
Mugunthan V N540a1152015-12-23 20:39:40 +0530360 struct ti_qspi_priv *priv;
361 struct udevice *bus;
362
363 bus = dev->parent;
364 priv = dev_get_priv(bus);
365
Venkatesh Yadav Abbarapu91b9e372024-09-26 10:25:05 +0530366 if (slave_plat->cs[0] > priv->num_cs) {
Mugunthan V N540a1152015-12-23 20:39:40 +0530367 debug("invalid qspi chip select\n");
368 return -EINVAL;
369 }
370
Vignesh Raghavendra6f9efcf2019-04-16 21:32:00 +0530371 writel(MM_SWITCH, &priv->base->memswitch);
372 if (priv->ctrl_mod_mmap)
373 ti_qspi_ctrl_mode_mmap(priv->ctrl_mod_mmap,
Venkatesh Yadav Abbarapu91b9e372024-09-26 10:25:05 +0530374 slave_plat->cs[0], true);
Mugunthan V N540a1152015-12-23 20:39:40 +0530375
Vignesh Raghavendraf3603b82019-04-16 21:31:59 +0530376 writel(priv->dc, &priv->base->dc);
377 writel(0, &priv->base->cmd);
378 writel(0, &priv->base->data);
379
Venkatesh Yadav Abbarapu91b9e372024-09-26 10:25:05 +0530380 priv->dc <<= slave_plat->cs[0] * 8;
Vignesh Raghavendraf3603b82019-04-16 21:31:59 +0530381 writel(priv->dc, &priv->base->dc);
382
383 return 0;
Mugunthan V N540a1152015-12-23 20:39:40 +0530384}
385
386static int ti_qspi_release_bus(struct udevice *dev)
387{
Simon Glassb75b15b2020-12-03 16:55:23 -0700388 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
Mugunthan V N540a1152015-12-23 20:39:40 +0530389 struct ti_qspi_priv *priv;
390 struct udevice *bus;
391
392 bus = dev->parent;
393 priv = dev_get_priv(bus);
394
Vignesh Raghavendra6f9efcf2019-04-16 21:32:00 +0530395 writel(~MM_SWITCH, &priv->base->memswitch);
396 if (priv->ctrl_mod_mmap)
397 ti_qspi_ctrl_mode_mmap(priv->ctrl_mod_mmap,
Venkatesh Yadav Abbarapu91b9e372024-09-26 10:25:05 +0530398 slave_plat->cs[0], false);
Mugunthan V N540a1152015-12-23 20:39:40 +0530399
Vignesh Raghavendraf3603b82019-04-16 21:31:59 +0530400 writel(0, &priv->base->dc);
401 writel(0, &priv->base->cmd);
402 writel(0, &priv->base->data);
Venkatesh Yadav Abbarapu91b9e372024-09-26 10:25:05 +0530403 writel(0, TI_QSPI_SETUP_REG(priv, slave_plat->cs[0]));
Mugunthan V N540a1152015-12-23 20:39:40 +0530404
Vignesh Raghavendraf3603b82019-04-16 21:31:59 +0530405 return 0;
Mugunthan V N540a1152015-12-23 20:39:40 +0530406}
407
408static int ti_qspi_probe(struct udevice *bus)
409{
Vignesh R1535e5a2016-07-25 15:45:45 +0530410 struct ti_qspi_priv *priv = dev_get_priv(bus);
411
412 priv->fclk = dev_get_driver_data(bus);
413
Mugunthan V N540a1152015-12-23 20:39:40 +0530414 return 0;
415}
416
Jean-Jacques Hiblot931b1f22017-02-13 16:17:49 +0100417static void *map_syscon_chipselects(struct udevice *bus)
418{
419#if CONFIG_IS_ENABLED(SYSCON)
420 struct udevice *syscon;
421 struct regmap *regmap;
422 const fdt32_t *cell;
423 int len, err;
424
425 err = uclass_get_device_by_phandle(UCLASS_SYSCON, bus,
426 "syscon-chipselects", &syscon);
427 if (err) {
428 debug("%s: unable to find syscon device (%d)\n", __func__,
429 err);
430 return NULL;
431 }
432
433 regmap = syscon_get_regmap(syscon);
434 if (IS_ERR(regmap)) {
435 debug("%s: unable to find regmap (%ld)\n", __func__,
436 PTR_ERR(regmap));
437 return NULL;
438 }
439
Simon Glass7a494432017-05-17 17:18:09 -0600440 cell = fdt_getprop(gd->fdt_blob, dev_of_offset(bus),
441 "syscon-chipselects", &len);
Jean-Jacques Hiblot931b1f22017-02-13 16:17:49 +0100442 if (len < 2*sizeof(fdt32_t)) {
443 debug("%s: offset not available\n", __func__);
444 return NULL;
445 }
446
447 return fdtdec_get_number(cell + 1, 1) + regmap_get_range(regmap, 0);
448#else
449 fdt_addr_t addr;
Simon Glassba1dea42017-05-17 17:18:05 -0600450 addr = devfdt_get_addr_index(bus, 2);
Jean-Jacques Hiblot931b1f22017-02-13 16:17:49 +0100451 return (addr == FDT_ADDR_T_NONE) ? NULL :
452 map_physmem(addr, 0, MAP_NOCACHE);
453#endif
454}
455
Simon Glassaad29ae2020-12-03 16:55:21 -0700456static int ti_qspi_of_to_plat(struct udevice *bus)
Mugunthan V N540a1152015-12-23 20:39:40 +0530457{
458 struct ti_qspi_priv *priv = dev_get_priv(bus);
459 const void *blob = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700460 int node = dev_of_offset(bus);
Vignesh Raghavendra6f9efcf2019-04-16 21:32:00 +0530461 fdt_addr_t mmap_addr;
462 fdt_addr_t mmap_size;
Mugunthan V N540a1152015-12-23 20:39:40 +0530463
Jean-Jacques Hiblot931b1f22017-02-13 16:17:49 +0100464 priv->ctrl_mod_mmap = map_syscon_chipselects(bus);
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900465 priv->base = map_physmem(dev_read_addr(bus),
Simon Glassba1dea42017-05-17 17:18:05 -0600466 sizeof(struct ti_qspi_regs), MAP_NOCACHE);
Vignesh Raghavendra6f9efcf2019-04-16 21:32:00 +0530467 mmap_addr = devfdt_get_addr_size_index(bus, 1, &mmap_size);
468 priv->memory_map = map_physmem(mmap_addr, mmap_size, MAP_NOCACHE);
469 priv->mmap_size = mmap_size;
Mugunthan V N540a1152015-12-23 20:39:40 +0530470
Ovidiu Panait7cb51592020-11-28 10:11:28 +0200471 priv->max_hz = dev_read_u32_default(bus, "spi-max-frequency", 0);
472 if (!priv->max_hz) {
Mugunthan V N540a1152015-12-23 20:39:40 +0530473 debug("Error: Max frequency missing\n");
474 return -ENODEV;
475 }
476 priv->num_cs = fdtdec_get_int(blob, node, "num-cs", 4);
477
478 debug("%s: regs=<0x%x>, max-frequency=%d\n", __func__,
479 (int)priv->base, priv->max_hz);
480
481 return 0;
482}
483
Vignesh Raghavendra6f9efcf2019-04-16 21:32:00 +0530484static const struct spi_controller_mem_ops ti_qspi_mem_ops = {
485 .exec_op = ti_qspi_exec_mem_op,
486};
Mugunthan V N540a1152015-12-23 20:39:40 +0530487
488static const struct dm_spi_ops ti_qspi_ops = {
489 .claim_bus = ti_qspi_claim_bus,
490 .release_bus = ti_qspi_release_bus,
491 .xfer = ti_qspi_xfer,
492 .set_speed = ti_qspi_set_speed,
493 .set_mode = ti_qspi_set_mode,
Vignesh Raghavendra6f9efcf2019-04-16 21:32:00 +0530494 .mem_ops = &ti_qspi_mem_ops,
Mugunthan V N540a1152015-12-23 20:39:40 +0530495};
496
497static const struct udevice_id ti_qspi_ids[] = {
Vignesh R1535e5a2016-07-25 15:45:45 +0530498 { .compatible = "ti,dra7xxx-qspi", .data = QSPI_DRA7XX_FCLK},
499 { .compatible = "ti,am4372-qspi", .data = QSPI_FCLK},
Mugunthan V N540a1152015-12-23 20:39:40 +0530500 { }
501};
502
503U_BOOT_DRIVER(ti_qspi) = {
504 .name = "ti_qspi",
505 .id = UCLASS_SPI,
506 .of_match = ti_qspi_ids,
507 .ops = &ti_qspi_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700508 .of_to_plat = ti_qspi_of_to_plat,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700509 .priv_auto = sizeof(struct ti_qspi_priv),
Mugunthan V N540a1152015-12-23 20:39:40 +0530510 .probe = ti_qspi_probe,
Mugunthan V N540a1152015-12-23 20:39:40 +0530511};