blob: 7c3b1f7b88e1ebafc7861edc24442179e090c592 [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
8#include <common.h>
Simon Glass63334482019-11-14 12:57:39 -07009#include <cpu_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Simon Glass274e0b02020-05-10 11:39:56 -060011#include <asm/cache.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
Vignesh R1535e5a2016-07-25 15:45:45 +053032#define QSPI_FCLK 192000000
33#define QSPI_DRA7XX_FCLK 76800000
Vignesh Rb0079972016-09-07 15:18:22 +053034#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 Porterda3e4c62013-10-07 15:53:02 +053037/* clock control */
Jagan Tekif16e4db2015-10-23 01:39:20 +053038#define QSPI_CLK_EN BIT(31)
Matt Porterda3e4c62013-10-07 15:53:02 +053039#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 Tekif16e4db2015-10-23 01:39:20 +053043#define QSPI_3_PIN BIT(18)
44#define QSPI_RD_SNGL BIT(16)
Matt Porterda3e4c62013-10-07 15:53:02 +053045#define QSPI_WR_SNGL (2 << 16)
46#define QSPI_INVAL (4 << 16)
47#define QSPI_RD_QUAD (7 << 16)
48/* device control */
Matt Porterda3e4c62013-10-07 15:53:02 +053049#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 Tekif16e4db2015-10-23 01:39:20 +053053#define QSPI_WC BIT(1)
54#define QSPI_BUSY BIT(0)
Matt Porterda3e4c62013-10-07 15:53:02 +053055#define QSPI_WC_BUSY (QSPI_WC | QSPI_BUSY)
56#define QSPI_XFER_DONE QSPI_WC
57#define MM_SWITCH 0x01
Mugunthan V N132e0072015-12-23 20:39:33 +053058#define MEM_CS(cs) ((cs + 1) << 8)
Praneeth Bajjuri5d21e6f2016-06-21 14:05:36 +053059#define MEM_CS_UNSELECT 0xfffff8ff
Matt Porterda3e4c62013-10-07 15:53:02 +053060
Matt Porterda3e4c62013-10-07 15:53:02 +053061#define QSPI_SETUP0_READ_NORMAL (0x0 << 12)
Mugunthan V N540a1152015-12-23 20:39:40 +053062#define QSPI_SETUP0_READ_DUAL (0x1 << 12)
Matt Porterda3e4c62013-10-07 15:53:02 +053063#define QSPI_SETUP0_READ_QUAD (0x3 << 12)
Vignesh Raghavendra6f9efcf2019-04-16 21:32:00 +053064#define QSPI_SETUP0_ADDR_SHIFT (8)
65#define QSPI_SETUP0_DBITS_SHIFT (10)
Matt Porterda3e4c62013-10-07 15:53:02 +053066
Vignesh Raghavendrae995dbb2019-12-11 18:59:36 +053067#define TI_QSPI_SETUP_REG(priv, cs) (&(priv)->base->setup0 + (cs))
68
Matt Porterda3e4c62013-10-07 15:53:02 +053069/* ti qspi register set */
70struct 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 Ne206d302015-12-23 20:39:34 +053096/* ti qspi priv */
97struct ti_qspi_priv {
Mugunthan V N540a1152015-12-23 20:39:40 +053098 void *memory_map;
Vignesh Raghavendra6f9efcf2019-04-16 21:32:00 +053099 size_t mmap_size;
Mugunthan V N540a1152015-12-23 20:39:40 +0530100 uint max_hz;
101 u32 num_cs;
Matt Porterda3e4c62013-10-07 15:53:02 +0530102 struct ti_qspi_regs *base;
Mugunthan V Ncd467732015-12-23 20:39:35 +0530103 void *ctrl_mod_mmap;
Vignesh R1535e5a2016-07-25 15:45:45 +0530104 ulong fclk;
Matt Porterda3e4c62013-10-07 15:53:02 +0530105 unsigned int mode;
106 u32 cmd;
107 u32 dc;
108};
109
Vignesh Raghavendraf3603b82019-04-16 21:31:59 +0530110static int ti_qspi_set_speed(struct udevice *bus, uint hz)
Matt Porterda3e4c62013-10-07 15:53:02 +0530111{
Vignesh Raghavendraf3603b82019-04-16 21:31:59 +0530112 struct ti_qspi_priv *priv = dev_get_priv(bus);
Matt Porterda3e4c62013-10-07 15:53:02 +0530113 uint clk_div;
114
Matt Porterda3e4c62013-10-07 15:53:02 +0530115 if (!hz)
116 clk_div = 0;
117 else
Vignesh R44e4bac2016-11-05 16:05:16 +0530118 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 Porterda3e4c62013-10-07 15:53:02 +0530123
Vignesh Rb9707672016-07-22 10:55:49 +0530124 debug("ti_spi_set_speed: hz: %d, clock divider %d\n", hz, clk_div);
125
Matt Porterda3e4c62013-10-07 15:53:02 +0530126 /* disable SCLK */
Mugunthan V Ne206d302015-12-23 20:39:34 +0530127 writel(readl(&priv->base->clk_ctrl) & ~QSPI_CLK_EN,
128 &priv->base->clk_ctrl);
Vignesh R44e4bac2016-11-05 16:05:16 +0530129 /* enable SCLK and program the clk divider */
Mugunthan V Ne206d302015-12-23 20:39:34 +0530130 writel(QSPI_CLK_EN | clk_div, &priv->base->clk_ctrl);
Vignesh Raghavendraf3603b82019-04-16 21:31:59 +0530131
132 return 0;
Matt Porterda3e4c62013-10-07 15:53:02 +0530133}
134
Mugunthan V Ncd467732015-12-23 20:39:35 +0530135static void ti_qspi_cs_deactivate(struct ti_qspi_priv *priv)
Matt Porterda3e4c62013-10-07 15:53:02 +0530136{
Mugunthan V Ne206d302015-12-23 20:39:34 +0530137 writel(priv->cmd | QSPI_INVAL, &priv->base->cmd);
Vignesh R43ad9ce2015-11-10 11:52:10 +0530138 /* dummy readl to ensure bus sync */
Mugunthan V Ncd467732015-12-23 20:39:35 +0530139 readl(&priv->base->cmd);
Matt Porterda3e4c62013-10-07 15:53:02 +0530140}
141
Mugunthan V Ncd467732015-12-23 20:39:35 +0530142static void ti_qspi_ctrl_mode_mmap(void *ctrl_mod_mmap, int cs, bool enable)
Matt Porterda3e4c62013-10-07 15:53:02 +0530143{
Mugunthan V Ncd467732015-12-23 20:39:35 +0530144 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 Raghavendraf3603b82019-04-16 21:31:59 +0530154static int ti_qspi_xfer(struct udevice *dev, unsigned int bitlen,
155 const void *dout, void *din, unsigned long flags)
Mugunthan V Ncd467732015-12-23 20:39:35 +0530156{
Simon Glassb75b15b2020-12-03 16:55:23 -0700157 struct dm_spi_slave_plat *slave = dev_get_parent_plat(dev);
Vignesh Raghavendraf3603b82019-04-16 21:31:59 +0530158 struct ti_qspi_priv *priv;
159 struct udevice *bus;
Matt Porterda3e4c62013-10-07 15:53:02 +0530160 uint words = bitlen >> 3; /* fixed 8-bit word length */
161 const uchar *txp = dout;
162 uchar *rxp = din;
163 uint status;
Sourav Poddar2145dff2013-12-21 12:50:09 +0530164 int timeout;
Vignesh Raghavendraf3603b82019-04-16 21:31:59 +0530165 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 Poddar2145dff2013-12-21 12:50:09 +0530174
Matt Porterda3e4c62013-10-07 15:53:02 +0530175 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 Ne206d302015-12-23 20:39:34 +0530184 priv->cmd = 0;
185 priv->cmd |= QSPI_WLEN(8);
Mugunthan V Ncd467732015-12-23 20:39:35 +0530186 priv->cmd |= QSPI_EN_CS(cs);
Mugunthan V Ne206d302015-12-23 20:39:34 +0530187 if (priv->mode & SPI_3WIRE)
188 priv->cmd |= QSPI_3_PIN;
189 priv->cmd |= 0xfff;
Matt Porterda3e4c62013-10-07 15:53:02 +0530190
Vignesh Rb0079972016-09-07 15:18:22 +0530191 while (words) {
192 u8 xfer_len = 0;
193
Matt Porterda3e4c62013-10-07 15:53:02 +0530194 if (txp) {
Vignesh Rb0079972016-09-07 15:18:22 +0530195 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 Ne206d302015-12-23 20:39:34 +0530219 status = readl(&priv->base->status);
Matt Porterda3e4c62013-10-07 15:53:02 +0530220 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 Ne206d302015-12-23 20:39:34 +0530226 status = readl(&priv->base->status);
Matt Porterda3e4c62013-10-07 15:53:02 +0530227 }
Vignesh Rb0079972016-09-07 15:18:22 +0530228 txp += xfer_len;
Matt Porterda3e4c62013-10-07 15:53:02 +0530229 debug("tx done, status %08x\n", status);
230 }
231 if (rxp) {
Matt Porterda3e4c62013-10-07 15:53:02 +0530232 debug("rx cmd %08x dc %08x\n",
Vignesh Raa291302016-07-22 10:55:48 +0530233 ((u32)(priv->cmd | QSPI_RD_SNGL)), priv->dc);
Vignesh Raa291302016-07-22 10:55:48 +0530234 writel(priv->cmd | QSPI_RD_SNGL, &priv->base->cmd);
Mugunthan V Ne206d302015-12-23 20:39:34 +0530235 status = readl(&priv->base->status);
Matt Porterda3e4c62013-10-07 15:53:02 +0530236 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 Ne206d302015-12-23 20:39:34 +0530242 status = readl(&priv->base->status);
Matt Porterda3e4c62013-10-07 15:53:02 +0530243 }
Mugunthan V Ne206d302015-12-23 20:39:34 +0530244 *rxp++ = readl(&priv->base->data);
Vignesh Rb0079972016-09-07 15:18:22 +0530245 xfer_len = 1;
Matt Porterda3e4c62013-10-07 15:53:02 +0530246 debug("rx done, status %08x, read %02x\n",
247 status, *(rxp-1));
248 }
Vignesh Rb0079972016-09-07 15:18:22 +0530249 words -= xfer_len;
Matt Porterda3e4c62013-10-07 15:53:02 +0530250 }
251
252 /* Terminate frame */
253 if (flags & SPI_XFER_END)
Mugunthan V Ncd467732015-12-23 20:39:35 +0530254 ti_qspi_cs_deactivate(priv);
Matt Porterda3e4c62013-10-07 15:53:02 +0530255
256 return 0;
257}
Vignesh Ra5bba8d2015-08-17 15:20:13 +0530258
259/* TODO: control from sf layer to here through dm-spi */
Vignesh Raghavendra6f9efcf2019-04-16 21:32:00 +0530260static void ti_qspi_copy_mmap(void *data, void *offset, size_t len)
Vignesh Ra5bba8d2015-08-17 15:20:13 +0530261{
Vignesh Raghavendra6f9efcf2019-04-16 21:32:00 +0530262#if defined(CONFIG_TI_EDMA3) && !defined(CONFIG_DMA)
Vignesh Ra5bba8d2015-08-17 15:20:13 +0530263 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 Raghavendra6f9efcf2019-04-16 21:32:00 +0530277#else
278 memcpy_fromio(data, offset, len);
279#endif
Vignesh Ra5bba8d2015-08-17 15:20:13 +0530280
281 *((unsigned int *)offset) += len;
282}
Mugunthan V Ncd467732015-12-23 20:39:35 +0530283
Vignesh Raghavendrae995dbb2019-12-11 18:59:36 +0530284static void ti_qspi_setup_mmap_read(struct ti_qspi_priv *priv, int cs,
285 u8 opcode, u8 data_nbits, u8 addr_width,
Vignesh Raghavendra6f9efcf2019-04-16 21:32:00 +0530286 u8 dummy_bytes)
Mugunthan V N540a1152015-12-23 20:39:40 +0530287{
Vignesh Raghavendra6f9efcf2019-04-16 21:32:00 +0530288 u32 memval = opcode;
Mugunthan V N540a1152015-12-23 20:39:40 +0530289
Vignesh Raghavendra6f9efcf2019-04-16 21:32:00 +0530290 switch (data_nbits) {
291 case 4:
Mugunthan V N540a1152015-12-23 20:39:40 +0530292 memval |= QSPI_SETUP0_READ_QUAD;
Mugunthan V N540a1152015-12-23 20:39:40 +0530293 break;
Vignesh Raghavendra6f9efcf2019-04-16 21:32:00 +0530294 case 2:
Mugunthan V N540a1152015-12-23 20:39:40 +0530295 memval |= QSPI_SETUP0_READ_DUAL;
296 break;
297 default:
Mugunthan V N540a1152015-12-23 20:39:40 +0530298 memval |= QSPI_SETUP0_READ_NORMAL;
299 break;
300 }
301
Vignesh Raghavendra6f9efcf2019-04-16 21:32:00 +0530302 memval |= ((addr_width - 1) << QSPI_SETUP0_ADDR_SHIFT |
303 dummy_bytes << QSPI_SETUP0_DBITS_SHIFT);
304
Vignesh Raghavendrae995dbb2019-12-11 18:59:36 +0530305 writel(memval, TI_QSPI_SETUP_REG(priv, cs));
Mugunthan V N540a1152015-12-23 20:39:40 +0530306}
307
Vignesh Raghavendraf3603b82019-04-16 21:31:59 +0530308static int ti_qspi_set_mode(struct udevice *bus, uint mode)
Mugunthan V N540a1152015-12-23 20:39:40 +0530309{
310 struct ti_qspi_priv *priv = dev_get_priv(bus);
311
Vignesh Raghavendraf3603b82019-04-16 21:31:59 +0530312 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 N540a1152015-12-23 20:39:40 +0530319
320 return 0;
321}
322
Vignesh Raghavendra6f9efcf2019-04-16 21:32:00 +0530323static int ti_qspi_exec_mem_op(struct spi_slave *slave,
324 const struct spi_mem_op *op)
325{
Simon Glassb75b15b2020-12-03 16:55:23 -0700326 struct dm_spi_slave_plat *slave_plat;
Vignesh Raghavendra6f9efcf2019-04-16 21:32:00 +0530327 struct ti_qspi_priv *priv;
328 struct udevice *bus;
Vignesh Raghavendrae995dbb2019-12-11 18:59:36 +0530329 u32 from = 0;
330 int ret = 0;
Vignesh Raghavendra6f9efcf2019-04-16 21:32:00 +0530331
332 bus = slave->dev->parent;
333 priv = dev_get_priv(bus);
Simon Glass71fa5b42020-12-03 16:55:18 -0700334 slave_plat = dev_get_parent_plat(slave->dev);
Vignesh Raghavendra6f9efcf2019-04-16 21:32:00 +0530335
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 Raghavendrae995dbb2019-12-11 18:59:36 +0530346 ti_qspi_setup_mmap_read(priv, slave_plat->cs, op->cmd.opcode,
347 op->data.buswidth, op->addr.nbytes,
348 op->dummy.nbytes);
Vignesh Raghavendra6f9efcf2019-04-16 21:32:00 +0530349
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 N540a1152015-12-23 20:39:40 +0530356static int ti_qspi_claim_bus(struct udevice *dev)
357{
Simon Glassb75b15b2020-12-03 16:55:23 -0700358 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
Mugunthan V N540a1152015-12-23 20:39:40 +0530359 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 Raghavendra6f9efcf2019-04-16 21:32:00 +0530370 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 N540a1152015-12-23 20:39:40 +0530374
Vignesh Raghavendraf3603b82019-04-16 21:31:59 +0530375 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 N540a1152015-12-23 20:39:40 +0530383}
384
385static int ti_qspi_release_bus(struct udevice *dev)
386{
Simon Glassb75b15b2020-12-03 16:55:23 -0700387 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
Mugunthan V N540a1152015-12-23 20:39:40 +0530388 struct ti_qspi_priv *priv;
389 struct udevice *bus;
390
391 bus = dev->parent;
392 priv = dev_get_priv(bus);
393
Vignesh Raghavendra6f9efcf2019-04-16 21:32:00 +0530394 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 N540a1152015-12-23 20:39:40 +0530398
Vignesh Raghavendraf3603b82019-04-16 21:31:59 +0530399 writel(0, &priv->base->dc);
400 writel(0, &priv->base->cmd);
401 writel(0, &priv->base->data);
Vignesh Raghavendrae995dbb2019-12-11 18:59:36 +0530402 writel(0, TI_QSPI_SETUP_REG(priv, slave_plat->cs));
Mugunthan V N540a1152015-12-23 20:39:40 +0530403
Vignesh Raghavendraf3603b82019-04-16 21:31:59 +0530404 return 0;
Mugunthan V N540a1152015-12-23 20:39:40 +0530405}
406
407static int ti_qspi_probe(struct udevice *bus)
408{
Vignesh R1535e5a2016-07-25 15:45:45 +0530409 struct ti_qspi_priv *priv = dev_get_priv(bus);
410
411 priv->fclk = dev_get_driver_data(bus);
412
Mugunthan V N540a1152015-12-23 20:39:40 +0530413 return 0;
414}
415
Jean-Jacques Hiblot931b1f22017-02-13 16:17:49 +0100416static 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 Glass7a494432017-05-17 17:18:09 -0600439 cell = fdt_getprop(gd->fdt_blob, dev_of_offset(bus),
440 "syscon-chipselects", &len);
Jean-Jacques Hiblot931b1f22017-02-13 16:17:49 +0100441 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 Glassba1dea42017-05-17 17:18:05 -0600449 addr = devfdt_get_addr_index(bus, 2);
Jean-Jacques Hiblot931b1f22017-02-13 16:17:49 +0100450 return (addr == FDT_ADDR_T_NONE) ? NULL :
451 map_physmem(addr, 0, MAP_NOCACHE);
452#endif
453}
454
Simon Glassaad29ae2020-12-03 16:55:21 -0700455static int ti_qspi_of_to_plat(struct udevice *bus)
Mugunthan V N540a1152015-12-23 20:39:40 +0530456{
457 struct ti_qspi_priv *priv = dev_get_priv(bus);
458 const void *blob = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700459 int node = dev_of_offset(bus);
Vignesh Raghavendra6f9efcf2019-04-16 21:32:00 +0530460 fdt_addr_t mmap_addr;
461 fdt_addr_t mmap_size;
Mugunthan V N540a1152015-12-23 20:39:40 +0530462
Jean-Jacques Hiblot931b1f22017-02-13 16:17:49 +0100463 priv->ctrl_mod_mmap = map_syscon_chipselects(bus);
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900464 priv->base = map_physmem(dev_read_addr(bus),
Simon Glassba1dea42017-05-17 17:18:05 -0600465 sizeof(struct ti_qspi_regs), MAP_NOCACHE);
Vignesh Raghavendra6f9efcf2019-04-16 21:32:00 +0530466 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 N540a1152015-12-23 20:39:40 +0530469
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 Raghavendra6f9efcf2019-04-16 21:32:00 +0530483static const struct spi_controller_mem_ops ti_qspi_mem_ops = {
484 .exec_op = ti_qspi_exec_mem_op,
485};
Mugunthan V N540a1152015-12-23 20:39:40 +0530486
487static 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 Raghavendra6f9efcf2019-04-16 21:32:00 +0530493 .mem_ops = &ti_qspi_mem_ops,
Mugunthan V N540a1152015-12-23 20:39:40 +0530494};
495
496static const struct udevice_id ti_qspi_ids[] = {
Vignesh R1535e5a2016-07-25 15:45:45 +0530497 { .compatible = "ti,dra7xxx-qspi", .data = QSPI_DRA7XX_FCLK},
498 { .compatible = "ti,am4372-qspi", .data = QSPI_FCLK},
Mugunthan V N540a1152015-12-23 20:39:40 +0530499 { }
500};
501
502U_BOOT_DRIVER(ti_qspi) = {
503 .name = "ti_qspi",
504 .id = UCLASS_SPI,
505 .of_match = ti_qspi_ids,
506 .ops = &ti_qspi_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700507 .of_to_plat = ti_qspi_of_to_plat,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700508 .priv_auto = sizeof(struct ti_qspi_priv),
Mugunthan V N540a1152015-12-23 20:39:40 +0530509 .probe = ti_qspi_probe,
Mugunthan V N540a1152015-12-23 20:39:40 +0530510};