1 // SPDX-License-Identifier: GPL-2.0+
3 * Renesas RCar Gen3 RPC QSPI driver
5 * Copyright (C) 2018 Marek Vasut <marek.vasut@gmail.com>
12 #include <dm/of_access.h>
13 #include <dt-structs.h>
15 #include <linux/errno.h>
19 #define RPC_CMNCR 0x0000 /* R/W */
20 #define RPC_CMNCR_MD BIT(31)
21 #define RPC_CMNCR_SFDE BIT(24)
22 #define RPC_CMNCR_MOIIO3(val) (((val) & 0x3) << 22)
23 #define RPC_CMNCR_MOIIO2(val) (((val) & 0x3) << 20)
24 #define RPC_CMNCR_MOIIO1(val) (((val) & 0x3) << 18)
25 #define RPC_CMNCR_MOIIO0(val) (((val) & 0x3) << 16)
26 #define RPC_CMNCR_MOIIO_HIZ (RPC_CMNCR_MOIIO0(3) | RPC_CMNCR_MOIIO1(3) | \
27 RPC_CMNCR_MOIIO2(3) | RPC_CMNCR_MOIIO3(3))
28 #define RPC_CMNCR_IO3FV(val) (((val) & 0x3) << 14)
29 #define RPC_CMNCR_IO2FV(val) (((val) & 0x3) << 12)
30 #define RPC_CMNCR_IO0FV(val) (((val) & 0x3) << 8)
31 #define RPC_CMNCR_IOFV_HIZ (RPC_CMNCR_IO0FV(3) | RPC_CMNCR_IO2FV(3) | \
33 #define RPC_CMNCR_CPHAT BIT(6)
34 #define RPC_CMNCR_CPHAR BIT(5)
35 #define RPC_CMNCR_SSLP BIT(4)
36 #define RPC_CMNCR_CPOL BIT(3)
37 #define RPC_CMNCR_BSZ(val) (((val) & 0x3) << 0)
39 #define RPC_SSLDR 0x0004 /* R/W */
40 #define RPC_SSLDR_SPNDL(d) (((d) & 0x7) << 16)
41 #define RPC_SSLDR_SLNDL(d) (((d) & 0x7) << 8)
42 #define RPC_SSLDR_SCKDL(d) (((d) & 0x7) << 0)
44 #define RPC_DRCR 0x000C /* R/W */
45 #define RPC_DRCR_SSLN BIT(24)
46 #define RPC_DRCR_RBURST(v) (((v) & 0x1F) << 16)
47 #define RPC_DRCR_RCF BIT(9)
48 #define RPC_DRCR_RBE BIT(8)
49 #define RPC_DRCR_SSLE BIT(0)
51 #define RPC_DRCMR 0x0010 /* R/W */
52 #define RPC_DRCMR_CMD(c) (((c) & 0xFF) << 16)
53 #define RPC_DRCMR_OCMD(c) (((c) & 0xFF) << 0)
55 #define RPC_DREAR 0x0014 /* R/W */
56 #define RPC_DREAR_EAV(v) (((v) & 0xFF) << 16)
57 #define RPC_DREAR_EAC(v) (((v) & 0x7) << 0)
59 #define RPC_DROPR 0x0018 /* R/W */
60 #define RPC_DROPR_OPD3(o) (((o) & 0xFF) << 24)
61 #define RPC_DROPR_OPD2(o) (((o) & 0xFF) << 16)
62 #define RPC_DROPR_OPD1(o) (((o) & 0xFF) << 8)
63 #define RPC_DROPR_OPD0(o) (((o) & 0xFF) << 0)
65 #define RPC_DRENR 0x001C /* R/W */
66 #define RPC_DRENR_CDB(o) (u32)((((o) & 0x3) << 30))
67 #define RPC_DRENR_OCDB(o) (((o) & 0x3) << 28)
68 #define RPC_DRENR_ADB(o) (((o) & 0x3) << 24)
69 #define RPC_DRENR_OPDB(o) (((o) & 0x3) << 20)
70 #define RPC_DRENR_SPIDB(o) (((o) & 0x3) << 16)
71 #define RPC_DRENR_DME BIT(15)
72 #define RPC_DRENR_CDE BIT(14)
73 #define RPC_DRENR_OCDE BIT(12)
74 #define RPC_DRENR_ADE(v) (((v) & 0xF) << 8)
75 #define RPC_DRENR_OPDE(v) (((v) & 0xF) << 4)
77 #define RPC_SMCR 0x0020 /* R/W */
78 #define RPC_SMCR_SSLKP BIT(8)
79 #define RPC_SMCR_SPIRE BIT(2)
80 #define RPC_SMCR_SPIWE BIT(1)
81 #define RPC_SMCR_SPIE BIT(0)
83 #define RPC_SMCMR 0x0024 /* R/W */
84 #define RPC_SMCMR_CMD(c) (((c) & 0xFF) << 16)
85 #define RPC_SMCMR_OCMD(c) (((c) & 0xFF) << 0)
87 #define RPC_SMADR 0x0028 /* R/W */
88 #define RPC_SMOPR 0x002C /* R/W */
89 #define RPC_SMOPR_OPD0(o) (((o) & 0xFF) << 0)
90 #define RPC_SMOPR_OPD1(o) (((o) & 0xFF) << 8)
91 #define RPC_SMOPR_OPD2(o) (((o) & 0xFF) << 16)
92 #define RPC_SMOPR_OPD3(o) (((o) & 0xFF) << 24)
94 #define RPC_SMENR 0x0030 /* R/W */
95 #define RPC_SMENR_CDB(o) (((o) & 0x3) << 30)
96 #define RPC_SMENR_OCDB(o) (((o) & 0x3) << 28)
97 #define RPC_SMENR_ADB(o) (((o) & 0x3) << 24)
98 #define RPC_SMENR_OPDB(o) (((o) & 0x3) << 20)
99 #define RPC_SMENR_SPIDB(o) (((o) & 0x3) << 16)
100 #define RPC_SMENR_DME BIT(15)
101 #define RPC_SMENR_CDE BIT(14)
102 #define RPC_SMENR_OCDE BIT(12)
103 #define RPC_SMENR_ADE(v) (((v) & 0xF) << 8)
104 #define RPC_SMENR_OPDE(v) (((v) & 0xF) << 4)
105 #define RPC_SMENR_SPIDE(v) (((v) & 0xF) << 0)
107 #define RPC_SMRDR0 0x0038 /* R */
108 #define RPC_SMRDR1 0x003C /* R */
109 #define RPC_SMWDR0 0x0040 /* R/W */
110 #define RPC_SMWDR1 0x0044 /* R/W */
111 #define RPC_CMNSR 0x0048 /* R */
112 #define RPC_CMNSR_SSLF BIT(1)
113 #define RPC_CMNSR_TEND BIT(0)
115 #define RPC_DRDMCR 0x0058 /* R/W */
116 #define RPC_DRDMCR_DMCYC(v) (((v) & 0xF) << 0)
118 #define RPC_DRDRENR 0x005C /* R/W */
119 #define RPC_DRDRENR_HYPE (0x5 << 12)
120 #define RPC_DRDRENR_ADDRE BIT(8)
121 #define RPC_DRDRENR_OPDRE BIT(4)
122 #define RPC_DRDRENR_DRDRE BIT(0)
124 #define RPC_SMDMCR 0x0060 /* R/W */
125 #define RPC_SMDMCR_DMCYC(v) (((v) & 0xF) << 0)
127 #define RPC_SMDRENR 0x0064 /* R/W */
128 #define RPC_SMDRENR_HYPE (0x5 << 12)
129 #define RPC_SMDRENR_ADDRE BIT(8)
130 #define RPC_SMDRENR_OPDRE BIT(4)
131 #define RPC_SMDRENR_SPIDRE BIT(0)
133 #define RPC_PHYCNT 0x007C /* R/W */
134 #define RPC_PHYCNT_CAL BIT(31)
135 #define PRC_PHYCNT_OCTA_AA BIT(22)
136 #define PRC_PHYCNT_OCTA_SA BIT(23)
137 #define PRC_PHYCNT_EXDS BIT(21)
138 #define RPC_PHYCNT_OCT BIT(20)
139 #define RPC_PHYCNT_STRTIM(v) (((v) & 0x7) << 15)
140 #define RPC_PHYCNT_WBUF2 BIT(4)
141 #define RPC_PHYCNT_WBUF BIT(2)
142 #define RPC_PHYCNT_MEM(v) (((v) & 0x3) << 0)
144 #define RPC_PHYINT 0x0088 /* R/W */
145 #define RPC_PHYINT_RSTEN BIT(18)
146 #define RPC_PHYINT_WPEN BIT(17)
147 #define RPC_PHYINT_INTEN BIT(16)
148 #define RPC_PHYINT_RST BIT(2)
149 #define RPC_PHYINT_WP BIT(1)
150 #define RPC_PHYINT_INT BIT(0)
152 #define RPC_WBUF 0x8000 /* R/W size=4/8/16/32/64Bytes */
153 #define RPC_WBUF_SIZE 0x100
155 DECLARE_GLOBAL_DATA_PTR;
157 struct rpc_spi_platdata {
160 s32 freq; /* Default clock freq, -1 for none */
163 struct rpc_spi_priv {
173 static int rpc_spi_wait_sslf(struct udevice *dev)
175 struct rpc_spi_priv *priv = dev_get_priv(dev->parent);
177 return wait_for_bit_le32((void *)priv->regs + RPC_CMNSR, RPC_CMNSR_SSLF,
181 static int rpc_spi_wait_tend(struct udevice *dev)
183 struct rpc_spi_priv *priv = dev_get_priv(dev->parent);
185 return wait_for_bit_le32((void *)priv->regs + RPC_CMNSR, RPC_CMNSR_TEND,
189 static void rpc_spi_flush_read_cache(struct udevice *dev)
191 struct udevice *bus = dev->parent;
192 struct rpc_spi_priv *priv = dev_get_priv(bus);
194 /* Flush read cache */
195 writel(RPC_DRCR_SSLN | RPC_DRCR_RBURST(0x1f) |
196 RPC_DRCR_RCF | RPC_DRCR_RBE | RPC_DRCR_SSLE,
197 priv->regs + RPC_DRCR);
198 readl(priv->regs + RPC_DRCR);
202 static int rpc_spi_claim_bus(struct udevice *dev, bool manual)
204 struct udevice *bus = dev->parent;
205 struct rpc_spi_priv *priv = dev_get_priv(bus);
208 * NOTE: The 0x260 are undocumented bits, but they must be set.
209 * NOTE: On H3 ES1.x (not supported in mainline U-Boot), the
210 * RPC_PHYCNT_STRTIM shall be 0, while on newer parts, the
211 * RPC_PHYCNT_STRTIM shall be 6.
213 writel(RPC_PHYCNT_CAL | RPC_PHYCNT_STRTIM(6) | 0x260,
214 priv->regs + RPC_PHYCNT);
215 writel((manual ? RPC_CMNCR_MD : 0) | RPC_CMNCR_SFDE |
216 RPC_CMNCR_MOIIO_HIZ | RPC_CMNCR_IOFV_HIZ | RPC_CMNCR_BSZ(0),
217 priv->regs + RPC_CMNCR);
219 writel(RPC_SSLDR_SPNDL(7) | RPC_SSLDR_SLNDL(7) |
220 RPC_SSLDR_SCKDL(7), priv->regs + RPC_SSLDR);
222 rpc_spi_flush_read_cache(dev);
227 static int rpc_spi_release_bus(struct udevice *dev)
229 struct udevice *bus = dev->parent;
230 struct rpc_spi_priv *priv = dev_get_priv(bus);
232 /* NOTE: The 0x260 are undocumented bits, but they must be set. */
233 writel(RPC_PHYCNT_STRTIM(6) | 0x260, priv->regs + RPC_PHYCNT);
235 rpc_spi_flush_read_cache(dev);
240 static int rpc_spi_xfer(struct udevice *dev, unsigned int bitlen,
241 const void *dout, void *din, unsigned long flags)
243 struct udevice *bus = dev->parent;
244 struct rpc_spi_priv *priv = dev_get_priv(bus);
245 u32 wlen = dout ? (bitlen / 8) : 0;
246 u32 rlen = din ? (bitlen / 8) : 0;
247 u32 wloop = DIV_ROUND_UP(wlen, 4);
248 u32 smenr, smcr, offset;
251 if (!priv->cmdstarted) {
255 memcpy(priv->cmdcopy, dout, wlen);
258 /* Command transfer start */
259 priv->cmdstarted = true;
260 if (!(flags & SPI_XFER_END))
264 offset = (priv->cmdcopy[1] << 16) | (priv->cmdcopy[2] << 8) |
265 (priv->cmdcopy[3] << 0);
269 if (wlen || (!rlen && !wlen) || flags == SPI_XFER_ONCE) {
270 if (wlen && flags == SPI_XFER_END)
271 smenr = RPC_SMENR_SPIDE(0xf);
273 rpc_spi_claim_bus(dev, true);
275 writel(0, priv->regs + RPC_SMCR);
277 if (priv->cmdlen >= 1) { /* Command(1) */
278 writel(RPC_SMCMR_CMD(priv->cmdcopy[0]),
279 priv->regs + RPC_SMCMR);
280 smenr |= RPC_SMENR_CDE;
282 writel(0, priv->regs + RPC_SMCMR);
285 if (priv->cmdlen >= 4) { /* Address(3) */
286 writel(offset, priv->regs + RPC_SMADR);
287 smenr |= RPC_SMENR_ADE(7);
289 writel(0, priv->regs + RPC_SMADR);
292 if (priv->cmdlen >= 5) { /* Dummy(n) */
293 writel(8 * (priv->cmdlen - 4) - 1,
294 priv->regs + RPC_SMDMCR);
295 smenr |= RPC_SMENR_DME;
297 writel(0, priv->regs + RPC_SMDMCR);
300 writel(0, priv->regs + RPC_SMOPR);
302 writel(0, priv->regs + RPC_SMDRENR);
304 if (wlen && flags == SPI_XFER_END) {
305 u32 *datout = (u32 *)dout;
308 smcr = RPC_SMCR_SPIWE | RPC_SMCR_SPIE;
310 smcr |= RPC_SMCR_SSLKP;
311 writel(smenr, priv->regs + RPC_SMENR);
312 writel(*datout, priv->regs + RPC_SMWDR0);
313 writel(smcr, priv->regs + RPC_SMCR);
314 ret = rpc_spi_wait_tend(dev);
318 smenr = RPC_SMENR_SPIDE(0xf);
321 ret = rpc_spi_wait_sslf(dev);
324 writel(smenr, priv->regs + RPC_SMENR);
325 writel(RPC_SMCR_SPIE, priv->regs + RPC_SMCR);
326 ret = rpc_spi_wait_tend(dev);
328 } else { /* Read data only, using DRx ext access */
329 rpc_spi_claim_bus(dev, false);
331 if (priv->cmdlen >= 1) { /* Command(1) */
332 writel(RPC_DRCMR_CMD(priv->cmdcopy[0]),
333 priv->regs + RPC_DRCMR);
334 smenr |= RPC_DRENR_CDE;
336 writel(0, priv->regs + RPC_DRCMR);
339 if (priv->cmdlen >= 4) /* Address(3) */
340 smenr |= RPC_DRENR_ADE(7);
342 if (priv->cmdlen >= 5) { /* Dummy(n) */
343 writel(8 * (priv->cmdlen - 4) - 1,
344 priv->regs + RPC_DRDMCR);
345 smenr |= RPC_DRENR_DME;
347 writel(0, priv->regs + RPC_DRDMCR);
350 writel(0, priv->regs + RPC_DROPR);
352 writel(smenr, priv->regs + RPC_DRENR);
355 memcpy_fromio(din, (void *)(priv->extr + offset), rlen);
357 readl(priv->extr); /* Dummy read */
361 priv->cmdstarted = false;
363 rpc_spi_release_bus(dev);
368 static int rpc_spi_set_speed(struct udevice *bus, uint speed)
370 /* This is a SPI NOR controller, do nothing. */
374 static int rpc_spi_set_mode(struct udevice *bus, uint mode)
376 /* This is a SPI NOR controller, do nothing. */
380 static int rpc_spi_bind(struct udevice *parent)
382 const void *fdt = gd->fdt_blob;
387 * Check if there are any SPI NOR child nodes, if so, bind as
388 * this controller will be operated in SPI mode.
390 dev_for_each_subnode(node, parent) {
391 off = ofnode_to_offset(node);
393 ret = fdt_node_check_compatible(fdt, off, "spi-flash");
397 ret = fdt_node_check_compatible(fdt, off, "jedec,spi-nor");
405 static int rpc_spi_probe(struct udevice *dev)
407 struct rpc_spi_platdata *plat = dev_get_platdata(dev);
408 struct rpc_spi_priv *priv = dev_get_priv(dev);
410 priv->regs = plat->regs;
411 priv->extr = plat->extr;
413 clk_enable(&priv->clk);
418 static int rpc_spi_ofdata_to_platdata(struct udevice *bus)
420 struct rpc_spi_platdata *plat = dev_get_platdata(bus);
421 struct rpc_spi_priv *priv = dev_get_priv(bus);
424 plat->regs = dev_read_addr_index(bus, 0);
425 plat->extr = dev_read_addr_index(bus, 1);
427 ret = clk_get_by_index(bus, 0, &priv->clk);
429 printf("%s: Could not get clock for %s: %d\n",
430 __func__, bus->name, ret);
434 plat->freq = dev_read_u32_default(bus, "spi-max-freq", 50000000);
439 static const struct dm_spi_ops rpc_spi_ops = {
440 .xfer = rpc_spi_xfer,
441 .set_speed = rpc_spi_set_speed,
442 .set_mode = rpc_spi_set_mode,
445 static const struct udevice_id rpc_spi_ids[] = {
446 { .compatible = "renesas,rpc-r8a7795" },
447 { .compatible = "renesas,rpc-r8a7796" },
448 { .compatible = "renesas,rpc-r8a77965" },
449 { .compatible = "renesas,rpc-r8a77970" },
450 { .compatible = "renesas,rpc-r8a77995" },
454 U_BOOT_DRIVER(rpc_spi) = {
457 .of_match = rpc_spi_ids,
459 .ofdata_to_platdata = rpc_spi_ofdata_to_platdata,
460 .platdata_auto_alloc_size = sizeof(struct rpc_spi_platdata),
461 .priv_auto_alloc_size = sizeof(struct rpc_spi_priv),
462 .bind = rpc_spi_bind,
463 .probe = rpc_spi_probe,