blob: c542f40c7df674ee8318431e5b6920ae94f18b17 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Matt Porter1d0933e2013-10-07 15:53:02 +05302/*
3 * TI QSPI driver
4 *
5 * Copyright (C) 2013, Texas Instruments, Incorporated
Matt Porter1d0933e2013-10-07 15:53:02 +05306 */
7
8#include <common.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -07009#include <cpu_func.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Simon Glass90526e92020-05-10 11:39:56 -060011#include <asm/cache.h>
Simon Glass401d1c42020-10-30 21:38:53 -060012#include <asm/global_data.h>
Matt Porter1d0933e2013-10-07 15:53:02 +053013#include <asm/io.h>
14#include <asm/arch/omap.h>
15#include <malloc.h>
16#include <spi.h>
Vignesh Raghavendra4c96c612019-04-16 21:32:00 +053017#include <spi-mem.h>
Mugunthan V N106f8132015-12-23 20:39:40 +053018#include <dm.h>
Sourav Poddar570533b2013-12-21 12:50:09 +053019#include <asm/gpio.h>
20#include <asm/omap_gpio.h>
Vignesh R8ddd9c42015-08-17 15:20:13 +053021#include <asm/omap_common.h>
22#include <asm/ti-common/ti-edma3.h>
Simon Glasscd93d622020-05-10 11:40:13 -060023#include <linux/bitops.h>
Simon Glass61b29b82020-02-03 07:36:15 -070024#include <linux/err.h>
Vignesh R948b8bb2016-11-05 16:05:16 +053025#include <linux/kernel.h>
Jean-Jacques Hiblotb06a3812017-02-13 16:17:49 +010026#include <regmap.h>
27#include <syscon.h>
Matt Porter1d0933e2013-10-07 15:53:02 +053028
Mugunthan V N106f8132015-12-23 20:39:40 +053029DECLARE_GLOBAL_DATA_PTR;
30
Matt Porter1d0933e2013-10-07 15:53:02 +053031/* ti qpsi register bit masks */
32#define QSPI_TIMEOUT 2000000
Vignesh Ra6f56ad2016-07-25 15:45:45 +053033#define QSPI_FCLK 192000000
34#define QSPI_DRA7XX_FCLK 76800000
Vignesh R26036852016-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 Porter1d0933e2013-10-07 15:53:02 +053038/* clock control */
Jagan Teki847720c2015-10-23 01:39:20 +053039#define QSPI_CLK_EN BIT(31)
Matt Porter1d0933e2013-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 Teki847720c2015-10-23 01:39:20 +053044#define QSPI_3_PIN BIT(18)
45#define QSPI_RD_SNGL BIT(16)
Matt Porter1d0933e2013-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 Porter1d0933e2013-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 Teki847720c2015-10-23 01:39:20 +053054#define QSPI_WC BIT(1)
55#define QSPI_BUSY BIT(0)
Matt Porter1d0933e2013-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 Nec712f42015-12-23 20:39:33 +053059#define MEM_CS(cs) ((cs + 1) << 8)
Praneeth Bajjuri8dfd6e22016-06-21 14:05:36 +053060#define MEM_CS_UNSELECT 0xfffff8ff
Matt Porter1d0933e2013-10-07 15:53:02 +053061
Matt Porter1d0933e2013-10-07 15:53:02 +053062#define QSPI_SETUP0_READ_NORMAL (0x0 << 12)
Mugunthan V N106f8132015-12-23 20:39:40 +053063#define QSPI_SETUP0_READ_DUAL (0x1 << 12)
Matt Porter1d0933e2013-10-07 15:53:02 +053064#define QSPI_SETUP0_READ_QUAD (0x3 << 12)
Vignesh Raghavendra4c96c612019-04-16 21:32:00 +053065#define QSPI_SETUP0_ADDR_SHIFT (8)
66#define QSPI_SETUP0_DBITS_SHIFT (10)
Matt Porter1d0933e2013-10-07 15:53:02 +053067
Vignesh Raghavendra5502c882019-12-11 18:59:36 +053068#define TI_QSPI_SETUP_REG(priv, cs) (&(priv)->base->setup0 + (cs))
69
Matt Porter1d0933e2013-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 N9c425582015-12-23 20:39:34 +053097/* ti qspi priv */
98struct ti_qspi_priv {
Mugunthan V N106f8132015-12-23 20:39:40 +053099 void *memory_map;
Vignesh Raghavendra4c96c612019-04-16 21:32:00 +0530100 size_t mmap_size;
Mugunthan V N106f8132015-12-23 20:39:40 +0530101 uint max_hz;
102 u32 num_cs;
Matt Porter1d0933e2013-10-07 15:53:02 +0530103 struct ti_qspi_regs *base;
Mugunthan V N22309142015-12-23 20:39:35 +0530104 void *ctrl_mod_mmap;
Vignesh Ra6f56ad2016-07-25 15:45:45 +0530105 ulong fclk;
Matt Porter1d0933e2013-10-07 15:53:02 +0530106 unsigned int mode;
107 u32 cmd;
108 u32 dc;
109};
110
Vignesh Raghavendra61ae9782019-04-16 21:31:59 +0530111static int ti_qspi_set_speed(struct udevice *bus, uint hz)
Matt Porter1d0933e2013-10-07 15:53:02 +0530112{
Vignesh Raghavendra61ae9782019-04-16 21:31:59 +0530113 struct ti_qspi_priv *priv = dev_get_priv(bus);
Matt Porter1d0933e2013-10-07 15:53:02 +0530114 uint clk_div;
115
Matt Porter1d0933e2013-10-07 15:53:02 +0530116 if (!hz)
117 clk_div = 0;
118 else
Vignesh R948b8bb2016-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 Porter1d0933e2013-10-07 15:53:02 +0530124
Vignesh Rc595a282016-07-22 10:55:49 +0530125 debug("ti_spi_set_speed: hz: %d, clock divider %d\n", hz, clk_div);
126
Matt Porter1d0933e2013-10-07 15:53:02 +0530127 /* disable SCLK */
Mugunthan V N9c425582015-12-23 20:39:34 +0530128 writel(readl(&priv->base->clk_ctrl) & ~QSPI_CLK_EN,
129 &priv->base->clk_ctrl);
Vignesh R948b8bb2016-11-05 16:05:16 +0530130 /* enable SCLK and program the clk divider */
Mugunthan V N9c425582015-12-23 20:39:34 +0530131 writel(QSPI_CLK_EN | clk_div, &priv->base->clk_ctrl);
Vignesh Raghavendra61ae9782019-04-16 21:31:59 +0530132
133 return 0;
Matt Porter1d0933e2013-10-07 15:53:02 +0530134}
135
Mugunthan V N22309142015-12-23 20:39:35 +0530136static void ti_qspi_cs_deactivate(struct ti_qspi_priv *priv)
Matt Porter1d0933e2013-10-07 15:53:02 +0530137{
Mugunthan V N9c425582015-12-23 20:39:34 +0530138 writel(priv->cmd | QSPI_INVAL, &priv->base->cmd);
Vignesh R857db482015-11-10 11:52:10 +0530139 /* dummy readl to ensure bus sync */
Mugunthan V N22309142015-12-23 20:39:35 +0530140 readl(&priv->base->cmd);
Matt Porter1d0933e2013-10-07 15:53:02 +0530141}
142
Mugunthan V N22309142015-12-23 20:39:35 +0530143static void ti_qspi_ctrl_mode_mmap(void *ctrl_mod_mmap, int cs, bool enable)
Matt Porter1d0933e2013-10-07 15:53:02 +0530144{
Mugunthan V N22309142015-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 Raghavendra61ae9782019-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 N22309142015-12-23 20:39:35 +0530157{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700158 struct dm_spi_slave_plat *slave = dev_get_parent_plat(dev);
Vignesh Raghavendra61ae9782019-04-16 21:31:59 +0530159 struct ti_qspi_priv *priv;
160 struct udevice *bus;
Matt Porter1d0933e2013-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 Poddar570533b2013-12-21 12:50:09 +0530165 int timeout;
Vignesh Raghavendra61ae9782019-04-16 21:31:59 +0530166 unsigned int cs = slave->cs;
167
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 Poddar570533b2013-12-21 12:50:09 +0530175
Matt Porter1d0933e2013-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 N9c425582015-12-23 20:39:34 +0530185 priv->cmd = 0;
186 priv->cmd |= QSPI_WLEN(8);
Mugunthan V N22309142015-12-23 20:39:35 +0530187 priv->cmd |= QSPI_EN_CS(cs);
Mugunthan V N9c425582015-12-23 20:39:34 +0530188 if (priv->mode & SPI_3WIRE)
189 priv->cmd |= QSPI_3_PIN;
190 priv->cmd |= 0xfff;
Matt Porter1d0933e2013-10-07 15:53:02 +0530191
Vignesh R26036852016-09-07 15:18:22 +0530192 while (words) {
193 u8 xfer_len = 0;
194
Matt Porter1d0933e2013-10-07 15:53:02 +0530195 if (txp) {
Vignesh R26036852016-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 N9c425582015-12-23 20:39:34 +0530220 status = readl(&priv->base->status);
Matt Porter1d0933e2013-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 N9c425582015-12-23 20:39:34 +0530227 status = readl(&priv->base->status);
Matt Porter1d0933e2013-10-07 15:53:02 +0530228 }
Vignesh R26036852016-09-07 15:18:22 +0530229 txp += xfer_len;
Matt Porter1d0933e2013-10-07 15:53:02 +0530230 debug("tx done, status %08x\n", status);
231 }
232 if (rxp) {
Matt Porter1d0933e2013-10-07 15:53:02 +0530233 debug("rx cmd %08x dc %08x\n",
Vignesh R69eeefa2016-07-22 10:55:48 +0530234 ((u32)(priv->cmd | QSPI_RD_SNGL)), priv->dc);
Vignesh R69eeefa2016-07-22 10:55:48 +0530235 writel(priv->cmd | QSPI_RD_SNGL, &priv->base->cmd);
Mugunthan V N9c425582015-12-23 20:39:34 +0530236 status = readl(&priv->base->status);
Matt Porter1d0933e2013-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 N9c425582015-12-23 20:39:34 +0530243 status = readl(&priv->base->status);
Matt Porter1d0933e2013-10-07 15:53:02 +0530244 }
Mugunthan V N9c425582015-12-23 20:39:34 +0530245 *rxp++ = readl(&priv->base->data);
Vignesh R26036852016-09-07 15:18:22 +0530246 xfer_len = 1;
Matt Porter1d0933e2013-10-07 15:53:02 +0530247 debug("rx done, status %08x, read %02x\n",
248 status, *(rxp-1));
249 }
Vignesh R26036852016-09-07 15:18:22 +0530250 words -= xfer_len;
Matt Porter1d0933e2013-10-07 15:53:02 +0530251 }
252
253 /* Terminate frame */
254 if (flags & SPI_XFER_END)
Mugunthan V N22309142015-12-23 20:39:35 +0530255 ti_qspi_cs_deactivate(priv);
Matt Porter1d0933e2013-10-07 15:53:02 +0530256
257 return 0;
258}
Vignesh R8ddd9c42015-08-17 15:20:13 +0530259
260/* TODO: control from sf layer to here through dm-spi */
Vignesh Raghavendra4c96c612019-04-16 21:32:00 +0530261static void ti_qspi_copy_mmap(void *data, void *offset, size_t len)
Vignesh R8ddd9c42015-08-17 15:20:13 +0530262{
Vignesh Raghavendra4c96c612019-04-16 21:32:00 +0530263#if defined(CONFIG_TI_EDMA3) && !defined(CONFIG_DMA)
Vignesh R8ddd9c42015-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 Raghavendra4c96c612019-04-16 21:32:00 +0530278#else
279 memcpy_fromio(data, offset, len);
280#endif
Vignesh R8ddd9c42015-08-17 15:20:13 +0530281
282 *((unsigned int *)offset) += len;
283}
Mugunthan V N22309142015-12-23 20:39:35 +0530284
Vignesh Raghavendra5502c882019-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 Raghavendra4c96c612019-04-16 21:32:00 +0530287 u8 dummy_bytes)
Mugunthan V N106f8132015-12-23 20:39:40 +0530288{
Vignesh Raghavendra4c96c612019-04-16 21:32:00 +0530289 u32 memval = opcode;
Mugunthan V N106f8132015-12-23 20:39:40 +0530290
Vignesh Raghavendra4c96c612019-04-16 21:32:00 +0530291 switch (data_nbits) {
292 case 4:
Mugunthan V N106f8132015-12-23 20:39:40 +0530293 memval |= QSPI_SETUP0_READ_QUAD;
Mugunthan V N106f8132015-12-23 20:39:40 +0530294 break;
Vignesh Raghavendra4c96c612019-04-16 21:32:00 +0530295 case 2:
Mugunthan V N106f8132015-12-23 20:39:40 +0530296 memval |= QSPI_SETUP0_READ_DUAL;
297 break;
298 default:
Mugunthan V N106f8132015-12-23 20:39:40 +0530299 memval |= QSPI_SETUP0_READ_NORMAL;
300 break;
301 }
302
Vignesh Raghavendra4c96c612019-04-16 21:32:00 +0530303 memval |= ((addr_width - 1) << QSPI_SETUP0_ADDR_SHIFT |
304 dummy_bytes << QSPI_SETUP0_DBITS_SHIFT);
305
Vignesh Raghavendra5502c882019-12-11 18:59:36 +0530306 writel(memval, TI_QSPI_SETUP_REG(priv, cs));
Mugunthan V N106f8132015-12-23 20:39:40 +0530307}
308
Mugunthan V N106f8132015-12-23 20:39:40 +0530309static int ti_qspi_set_mode(struct udevice *bus, uint mode)
310{
311 struct ti_qspi_priv *priv = dev_get_priv(bus);
Vignesh Raghavendra61ae9782019-04-16 21:31:59 +0530312
313 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);
320
321 return 0;
Mugunthan V N106f8132015-12-23 20:39:40 +0530322}
323
Vignesh Raghavendra4c96c612019-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 Glass8a8d24b2020-12-03 16:55:23 -0700327 struct dm_spi_slave_plat *slave_plat;
Vignesh Raghavendra4c96c612019-04-16 21:32:00 +0530328 struct ti_qspi_priv *priv;
329 struct udevice *bus;
Vignesh Raghavendra5502c882019-12-11 18:59:36 +0530330 u32 from = 0;
331 int ret = 0;
Vignesh Raghavendra4c96c612019-04-16 21:32:00 +0530332
333 bus = slave->dev->parent;
334 priv = dev_get_priv(bus);
Simon Glasscaa4daa2020-12-03 16:55:18 -0700335 slave_plat = dev_get_parent_plat(slave->dev);
Vignesh Raghavendra4c96c612019-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
Vignesh Raghavendra5502c882019-12-11 18:59:36 +0530347 ti_qspi_setup_mmap_read(priv, slave_plat->cs, op->cmd.opcode,
348 op->data.buswidth, op->addr.nbytes,
349 op->dummy.nbytes);
Vignesh Raghavendra4c96c612019-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 N106f8132015-12-23 20:39:40 +0530357static int ti_qspi_claim_bus(struct udevice *dev)
358{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700359 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
Mugunthan V N106f8132015-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
366 if (slave_plat->cs > priv->num_cs) {
367 debug("invalid qspi chip select\n");
368 return -EINVAL;
369 }
370
Vignesh Raghavendra4c96c612019-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,
374 slave_plat->cs, true);
Mugunthan V N106f8132015-12-23 20:39:40 +0530375
Vignesh Raghavendra61ae9782019-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
380 priv->dc <<= slave_plat->cs * 8;
381 writel(priv->dc, &priv->base->dc);
382
383 return 0;
Mugunthan V N106f8132015-12-23 20:39:40 +0530384}
385
386static int ti_qspi_release_bus(struct udevice *dev)
387{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700388 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
Mugunthan V N106f8132015-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 Raghavendra4c96c612019-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,
398 slave_plat->cs, false);
Vignesh Raghavendra61ae9782019-04-16 21:31:59 +0530399
400 writel(0, &priv->base->dc);
401 writel(0, &priv->base->cmd);
402 writel(0, &priv->base->data);
Vignesh Raghavendra5502c882019-12-11 18:59:36 +0530403 writel(0, TI_QSPI_SETUP_REG(priv, slave_plat->cs));
Mugunthan V N106f8132015-12-23 20:39:40 +0530404
405 return 0;
406}
407
Mugunthan V N106f8132015-12-23 20:39:40 +0530408static int ti_qspi_probe(struct udevice *bus)
409{
Vignesh Ra6f56ad2016-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 N106f8132015-12-23 20:39:40 +0530414 return 0;
415}
416
Jean-Jacques Hiblotb06a3812017-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 Glassda409cc2017-05-17 17:18:09 -0600440 cell = fdt_getprop(gd->fdt_blob, dev_of_offset(bus),
441 "syscon-chipselects", &len);
Jean-Jacques Hiblotb06a3812017-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 Glassa821c4a2017-05-17 17:18:05 -0600450 addr = devfdt_get_addr_index(bus, 2);
Jean-Jacques Hiblotb06a3812017-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 Glassd1998a92020-12-03 16:55:21 -0700456static int ti_qspi_of_to_plat(struct udevice *bus)
Mugunthan V N106f8132015-12-23 20:39:40 +0530457{
458 struct ti_qspi_priv *priv = dev_get_priv(bus);
459 const void *blob = gd->fdt_blob;
Simon Glasse160f7d2017-01-17 16:52:55 -0700460 int node = dev_of_offset(bus);
Vignesh Raghavendra4c96c612019-04-16 21:32:00 +0530461 fdt_addr_t mmap_addr;
462 fdt_addr_t mmap_size;
Mugunthan V N106f8132015-12-23 20:39:40 +0530463
Jean-Jacques Hiblotb06a3812017-02-13 16:17:49 +0100464 priv->ctrl_mod_mmap = map_syscon_chipselects(bus);
Masahiro Yamada25484932020-07-17 14:36:48 +0900465 priv->base = map_physmem(dev_read_addr(bus),
Simon Glassa821c4a2017-05-17 17:18:05 -0600466 sizeof(struct ti_qspi_regs), MAP_NOCACHE);
Vignesh Raghavendra4c96c612019-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 N106f8132015-12-23 20:39:40 +0530470
Ovidiu Panait705082d2020-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 N106f8132015-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 Raghavendra4c96c612019-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 N106f8132015-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 Raghavendra4c96c612019-04-16 21:32:00 +0530494 .mem_ops = &ti_qspi_mem_ops,
Mugunthan V N106f8132015-12-23 20:39:40 +0530495};
496
497static const struct udevice_id ti_qspi_ids[] = {
Vignesh Ra6f56ad2016-07-25 15:45:45 +0530498 { .compatible = "ti,dra7xxx-qspi", .data = QSPI_DRA7XX_FCLK},
499 { .compatible = "ti,am4372-qspi", .data = QSPI_FCLK},
Mugunthan V N106f8132015-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 Glassd1998a92020-12-03 16:55:21 -0700508 .of_to_plat = ti_qspi_of_to_plat,
Simon Glass41575d82020-12-03 16:55:17 -0700509 .priv_auto = sizeof(struct ti_qspi_priv),
Mugunthan V N106f8132015-12-23 20:39:40 +0530510 .probe = ti_qspi_probe,
Mugunthan V N106f8132015-12-23 20:39:40 +0530511};