blob: 105765168e5276c544d9dc24e252f286f13489d4 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marek Vasut6ca967b2017-11-29 06:29:46 +01002/*
3 * Renesas RCar Gen3 RPC QSPI driver
4 *
5 * Copyright (C) 2018 Marek Vasut <marek.vasut@gmail.com>
Marek Vasut6ca967b2017-11-29 06:29:46 +01006 */
7
8#include <common.h>
9#include <asm/io.h>
10#include <clk.h>
11#include <dm.h>
12#include <dm/of_access.h>
13#include <dt-structs.h>
14#include <errno.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060015#include <linux/bitops.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060016#include <linux/bug.h>
Marek Vasut6ca967b2017-11-29 06:29:46 +010017#include <linux/errno.h>
18#include <spi.h>
19#include <wait_bit.h>
20
21#define RPC_CMNCR 0x0000 /* R/W */
22#define RPC_CMNCR_MD BIT(31)
23#define RPC_CMNCR_SFDE BIT(24)
24#define RPC_CMNCR_MOIIO3(val) (((val) & 0x3) << 22)
25#define RPC_CMNCR_MOIIO2(val) (((val) & 0x3) << 20)
26#define RPC_CMNCR_MOIIO1(val) (((val) & 0x3) << 18)
27#define RPC_CMNCR_MOIIO0(val) (((val) & 0x3) << 16)
28#define RPC_CMNCR_MOIIO_HIZ (RPC_CMNCR_MOIIO0(3) | RPC_CMNCR_MOIIO1(3) | \
29 RPC_CMNCR_MOIIO2(3) | RPC_CMNCR_MOIIO3(3))
30#define RPC_CMNCR_IO3FV(val) (((val) & 0x3) << 14)
31#define RPC_CMNCR_IO2FV(val) (((val) & 0x3) << 12)
32#define RPC_CMNCR_IO0FV(val) (((val) & 0x3) << 8)
33#define RPC_CMNCR_IOFV_HIZ (RPC_CMNCR_IO0FV(3) | RPC_CMNCR_IO2FV(3) | \
34 RPC_CMNCR_IO3FV(3))
35#define RPC_CMNCR_CPHAT BIT(6)
36#define RPC_CMNCR_CPHAR BIT(5)
37#define RPC_CMNCR_SSLP BIT(4)
38#define RPC_CMNCR_CPOL BIT(3)
39#define RPC_CMNCR_BSZ(val) (((val) & 0x3) << 0)
40
41#define RPC_SSLDR 0x0004 /* R/W */
42#define RPC_SSLDR_SPNDL(d) (((d) & 0x7) << 16)
43#define RPC_SSLDR_SLNDL(d) (((d) & 0x7) << 8)
44#define RPC_SSLDR_SCKDL(d) (((d) & 0x7) << 0)
45
46#define RPC_DRCR 0x000C /* R/W */
47#define RPC_DRCR_SSLN BIT(24)
48#define RPC_DRCR_RBURST(v) (((v) & 0x1F) << 16)
49#define RPC_DRCR_RCF BIT(9)
50#define RPC_DRCR_RBE BIT(8)
51#define RPC_DRCR_SSLE BIT(0)
52
53#define RPC_DRCMR 0x0010 /* R/W */
54#define RPC_DRCMR_CMD(c) (((c) & 0xFF) << 16)
55#define RPC_DRCMR_OCMD(c) (((c) & 0xFF) << 0)
56
57#define RPC_DREAR 0x0014 /* R/W */
58#define RPC_DREAR_EAV(v) (((v) & 0xFF) << 16)
59#define RPC_DREAR_EAC(v) (((v) & 0x7) << 0)
60
61#define RPC_DROPR 0x0018 /* R/W */
62#define RPC_DROPR_OPD3(o) (((o) & 0xFF) << 24)
63#define RPC_DROPR_OPD2(o) (((o) & 0xFF) << 16)
64#define RPC_DROPR_OPD1(o) (((o) & 0xFF) << 8)
65#define RPC_DROPR_OPD0(o) (((o) & 0xFF) << 0)
66
67#define RPC_DRENR 0x001C /* R/W */
68#define RPC_DRENR_CDB(o) (u32)((((o) & 0x3) << 30))
69#define RPC_DRENR_OCDB(o) (((o) & 0x3) << 28)
70#define RPC_DRENR_ADB(o) (((o) & 0x3) << 24)
71#define RPC_DRENR_OPDB(o) (((o) & 0x3) << 20)
72#define RPC_DRENR_SPIDB(o) (((o) & 0x3) << 16)
73#define RPC_DRENR_DME BIT(15)
74#define RPC_DRENR_CDE BIT(14)
75#define RPC_DRENR_OCDE BIT(12)
76#define RPC_DRENR_ADE(v) (((v) & 0xF) << 8)
77#define RPC_DRENR_OPDE(v) (((v) & 0xF) << 4)
78
79#define RPC_SMCR 0x0020 /* R/W */
80#define RPC_SMCR_SSLKP BIT(8)
81#define RPC_SMCR_SPIRE BIT(2)
82#define RPC_SMCR_SPIWE BIT(1)
83#define RPC_SMCR_SPIE BIT(0)
84
85#define RPC_SMCMR 0x0024 /* R/W */
86#define RPC_SMCMR_CMD(c) (((c) & 0xFF) << 16)
87#define RPC_SMCMR_OCMD(c) (((c) & 0xFF) << 0)
88
89#define RPC_SMADR 0x0028 /* R/W */
90#define RPC_SMOPR 0x002C /* R/W */
91#define RPC_SMOPR_OPD0(o) (((o) & 0xFF) << 0)
92#define RPC_SMOPR_OPD1(o) (((o) & 0xFF) << 8)
93#define RPC_SMOPR_OPD2(o) (((o) & 0xFF) << 16)
94#define RPC_SMOPR_OPD3(o) (((o) & 0xFF) << 24)
95
96#define RPC_SMENR 0x0030 /* R/W */
97#define RPC_SMENR_CDB(o) (((o) & 0x3) << 30)
98#define RPC_SMENR_OCDB(o) (((o) & 0x3) << 28)
99#define RPC_SMENR_ADB(o) (((o) & 0x3) << 24)
100#define RPC_SMENR_OPDB(o) (((o) & 0x3) << 20)
101#define RPC_SMENR_SPIDB(o) (((o) & 0x3) << 16)
102#define RPC_SMENR_DME BIT(15)
103#define RPC_SMENR_CDE BIT(14)
104#define RPC_SMENR_OCDE BIT(12)
105#define RPC_SMENR_ADE(v) (((v) & 0xF) << 8)
106#define RPC_SMENR_OPDE(v) (((v) & 0xF) << 4)
107#define RPC_SMENR_SPIDE(v) (((v) & 0xF) << 0)
108
109#define RPC_SMRDR0 0x0038 /* R */
110#define RPC_SMRDR1 0x003C /* R */
111#define RPC_SMWDR0 0x0040 /* R/W */
112#define RPC_SMWDR1 0x0044 /* R/W */
113#define RPC_CMNSR 0x0048 /* R */
114#define RPC_CMNSR_SSLF BIT(1)
115#define RPC_CMNSR_TEND BIT(0)
116
117#define RPC_DRDMCR 0x0058 /* R/W */
118#define RPC_DRDMCR_DMCYC(v) (((v) & 0xF) << 0)
119
120#define RPC_DRDRENR 0x005C /* R/W */
121#define RPC_DRDRENR_HYPE (0x5 << 12)
122#define RPC_DRDRENR_ADDRE BIT(8)
123#define RPC_DRDRENR_OPDRE BIT(4)
124#define RPC_DRDRENR_DRDRE BIT(0)
125
126#define RPC_SMDMCR 0x0060 /* R/W */
127#define RPC_SMDMCR_DMCYC(v) (((v) & 0xF) << 0)
128
129#define RPC_SMDRENR 0x0064 /* R/W */
130#define RPC_SMDRENR_HYPE (0x5 << 12)
131#define RPC_SMDRENR_ADDRE BIT(8)
132#define RPC_SMDRENR_OPDRE BIT(4)
133#define RPC_SMDRENR_SPIDRE BIT(0)
134
135#define RPC_PHYCNT 0x007C /* R/W */
136#define RPC_PHYCNT_CAL BIT(31)
137#define PRC_PHYCNT_OCTA_AA BIT(22)
138#define PRC_PHYCNT_OCTA_SA BIT(23)
139#define PRC_PHYCNT_EXDS BIT(21)
140#define RPC_PHYCNT_OCT BIT(20)
141#define RPC_PHYCNT_STRTIM(v) (((v) & 0x7) << 15)
142#define RPC_PHYCNT_WBUF2 BIT(4)
143#define RPC_PHYCNT_WBUF BIT(2)
144#define RPC_PHYCNT_MEM(v) (((v) & 0x3) << 0)
145
146#define RPC_PHYINT 0x0088 /* R/W */
147#define RPC_PHYINT_RSTEN BIT(18)
148#define RPC_PHYINT_WPEN BIT(17)
149#define RPC_PHYINT_INTEN BIT(16)
150#define RPC_PHYINT_RST BIT(2)
151#define RPC_PHYINT_WP BIT(1)
152#define RPC_PHYINT_INT BIT(0)
153
154#define RPC_WBUF 0x8000 /* R/W size=4/8/16/32/64Bytes */
155#define RPC_WBUF_SIZE 0x100
156
157DECLARE_GLOBAL_DATA_PTR;
158
Simon Glassb75b15b2020-12-03 16:55:23 -0700159struct rpc_spi_plat {
Marek Vasut6ca967b2017-11-29 06:29:46 +0100160 fdt_addr_t regs;
161 fdt_addr_t extr;
162 s32 freq; /* Default clock freq, -1 for none */
163};
164
165struct rpc_spi_priv {
166 fdt_addr_t regs;
167 fdt_addr_t extr;
168 struct clk clk;
169
170 u8 cmdcopy[8];
171 u32 cmdlen;
172 bool cmdstarted;
173};
174
175static int rpc_spi_wait_sslf(struct udevice *dev)
176{
177 struct rpc_spi_priv *priv = dev_get_priv(dev->parent);
178
179 return wait_for_bit_le32((void *)priv->regs + RPC_CMNSR, RPC_CMNSR_SSLF,
180 false, 1000, false);
181}
182
183static int rpc_spi_wait_tend(struct udevice *dev)
184{
185 struct rpc_spi_priv *priv = dev_get_priv(dev->parent);
186
187 return wait_for_bit_le32((void *)priv->regs + RPC_CMNSR, RPC_CMNSR_TEND,
188 true, 1000, false);
189}
190
191static void rpc_spi_flush_read_cache(struct udevice *dev)
192{
193 struct udevice *bus = dev->parent;
194 struct rpc_spi_priv *priv = dev_get_priv(bus);
195
196 /* Flush read cache */
197 writel(RPC_DRCR_SSLN | RPC_DRCR_RBURST(0x1f) |
198 RPC_DRCR_RCF | RPC_DRCR_RBE | RPC_DRCR_SSLE,
199 priv->regs + RPC_DRCR);
200 readl(priv->regs + RPC_DRCR);
201
202}
203
204static int rpc_spi_claim_bus(struct udevice *dev, bool manual)
205{
206 struct udevice *bus = dev->parent;
207 struct rpc_spi_priv *priv = dev_get_priv(bus);
208
209 /*
210 * NOTE: The 0x260 are undocumented bits, but they must be set.
211 * NOTE: On H3 ES1.x (not supported in mainline U-Boot), the
212 * RPC_PHYCNT_STRTIM shall be 0, while on newer parts, the
213 * RPC_PHYCNT_STRTIM shall be 6.
214 */
215 writel(RPC_PHYCNT_CAL | RPC_PHYCNT_STRTIM(6) | 0x260,
216 priv->regs + RPC_PHYCNT);
217 writel((manual ? RPC_CMNCR_MD : 0) | RPC_CMNCR_SFDE |
218 RPC_CMNCR_MOIIO_HIZ | RPC_CMNCR_IOFV_HIZ | RPC_CMNCR_BSZ(0),
219 priv->regs + RPC_CMNCR);
220
221 writel(RPC_SSLDR_SPNDL(7) | RPC_SSLDR_SLNDL(7) |
222 RPC_SSLDR_SCKDL(7), priv->regs + RPC_SSLDR);
223
224 rpc_spi_flush_read_cache(dev);
225
226 return 0;
227}
228
229static int rpc_spi_release_bus(struct udevice *dev)
230{
231 struct udevice *bus = dev->parent;
232 struct rpc_spi_priv *priv = dev_get_priv(bus);
233
234 /* NOTE: The 0x260 are undocumented bits, but they must be set. */
235 writel(RPC_PHYCNT_STRTIM(6) | 0x260, priv->regs + RPC_PHYCNT);
236
237 rpc_spi_flush_read_cache(dev);
238
239 return 0;
240}
241
242static int rpc_spi_xfer(struct udevice *dev, unsigned int bitlen,
243 const void *dout, void *din, unsigned long flags)
244{
245 struct udevice *bus = dev->parent;
246 struct rpc_spi_priv *priv = dev_get_priv(bus);
247 u32 wlen = dout ? (bitlen / 8) : 0;
248 u32 rlen = din ? (bitlen / 8) : 0;
249 u32 wloop = DIV_ROUND_UP(wlen, 4);
250 u32 smenr, smcr, offset;
251 int ret = 0;
252
253 if (!priv->cmdstarted) {
254 if (!wlen || rlen)
255 BUG();
256
257 memcpy(priv->cmdcopy, dout, wlen);
258 priv->cmdlen = wlen;
259
260 /* Command transfer start */
261 priv->cmdstarted = true;
262 if (!(flags & SPI_XFER_END))
263 return 0;
264 }
265
266 offset = (priv->cmdcopy[1] << 16) | (priv->cmdcopy[2] << 8) |
267 (priv->cmdcopy[3] << 0);
268
269 smenr = 0;
270
271 if (wlen || (!rlen && !wlen) || flags == SPI_XFER_ONCE) {
272 if (wlen && flags == SPI_XFER_END)
273 smenr = RPC_SMENR_SPIDE(0xf);
274
275 rpc_spi_claim_bus(dev, true);
276
277 writel(0, priv->regs + RPC_SMCR);
278
279 if (priv->cmdlen >= 1) { /* Command(1) */
280 writel(RPC_SMCMR_CMD(priv->cmdcopy[0]),
281 priv->regs + RPC_SMCMR);
282 smenr |= RPC_SMENR_CDE;
283 } else {
284 writel(0, priv->regs + RPC_SMCMR);
285 }
286
287 if (priv->cmdlen >= 4) { /* Address(3) */
288 writel(offset, priv->regs + RPC_SMADR);
289 smenr |= RPC_SMENR_ADE(7);
290 } else {
291 writel(0, priv->regs + RPC_SMADR);
292 }
293
294 if (priv->cmdlen >= 5) { /* Dummy(n) */
295 writel(8 * (priv->cmdlen - 4) - 1,
296 priv->regs + RPC_SMDMCR);
297 smenr |= RPC_SMENR_DME;
298 } else {
299 writel(0, priv->regs + RPC_SMDMCR);
300 }
301
302 writel(0, priv->regs + RPC_SMOPR);
303
304 writel(0, priv->regs + RPC_SMDRENR);
305
306 if (wlen && flags == SPI_XFER_END) {
307 u32 *datout = (u32 *)dout;
308
309 while (wloop--) {
310 smcr = RPC_SMCR_SPIWE | RPC_SMCR_SPIE;
311 if (wloop >= 1)
312 smcr |= RPC_SMCR_SSLKP;
313 writel(smenr, priv->regs + RPC_SMENR);
314 writel(*datout, priv->regs + RPC_SMWDR0);
315 writel(smcr, priv->regs + RPC_SMCR);
316 ret = rpc_spi_wait_tend(dev);
317 if (ret)
318 goto err;
319 datout++;
320 smenr = RPC_SMENR_SPIDE(0xf);
321 }
322
323 ret = rpc_spi_wait_sslf(dev);
324
325 } else {
326 writel(smenr, priv->regs + RPC_SMENR);
327 writel(RPC_SMCR_SPIE, priv->regs + RPC_SMCR);
328 ret = rpc_spi_wait_tend(dev);
329 }
330 } else { /* Read data only, using DRx ext access */
331 rpc_spi_claim_bus(dev, false);
332
333 if (priv->cmdlen >= 1) { /* Command(1) */
334 writel(RPC_DRCMR_CMD(priv->cmdcopy[0]),
335 priv->regs + RPC_DRCMR);
336 smenr |= RPC_DRENR_CDE;
337 } else {
338 writel(0, priv->regs + RPC_DRCMR);
339 }
340
341 if (priv->cmdlen >= 4) /* Address(3) */
342 smenr |= RPC_DRENR_ADE(7);
343
344 if (priv->cmdlen >= 5) { /* Dummy(n) */
345 writel(8 * (priv->cmdlen - 4) - 1,
346 priv->regs + RPC_DRDMCR);
347 smenr |= RPC_DRENR_DME;
348 } else {
349 writel(0, priv->regs + RPC_DRDMCR);
350 }
351
352 writel(0, priv->regs + RPC_DROPR);
353
354 writel(smenr, priv->regs + RPC_DRENR);
355
356 if (rlen)
357 memcpy_fromio(din, (void *)(priv->extr + offset), rlen);
358 else
359 readl(priv->extr); /* Dummy read */
360 }
361
362err:
363 priv->cmdstarted = false;
364
365 rpc_spi_release_bus(dev);
366
367 return ret;
368}
369
370static int rpc_spi_set_speed(struct udevice *bus, uint speed)
371{
372 /* This is a SPI NOR controller, do nothing. */
373 return 0;
374}
375
376static int rpc_spi_set_mode(struct udevice *bus, uint mode)
377{
378 /* This is a SPI NOR controller, do nothing. */
379 return 0;
380}
381
382static int rpc_spi_bind(struct udevice *parent)
383{
384 const void *fdt = gd->fdt_blob;
385 ofnode node;
386 int ret, off;
387
388 /*
389 * Check if there are any SPI NOR child nodes, if so, bind as
390 * this controller will be operated in SPI mode.
391 */
392 dev_for_each_subnode(node, parent) {
393 off = ofnode_to_offset(node);
394
395 ret = fdt_node_check_compatible(fdt, off, "spi-flash");
396 if (!ret)
397 return 0;
398
399 ret = fdt_node_check_compatible(fdt, off, "jedec,spi-nor");
400 if (!ret)
401 return 0;
402 }
403
404 return -ENODEV;
405}
406
407static int rpc_spi_probe(struct udevice *dev)
408{
Simon Glassb75b15b2020-12-03 16:55:23 -0700409 struct rpc_spi_plat *plat = dev_get_plat(dev);
Marek Vasut6ca967b2017-11-29 06:29:46 +0100410 struct rpc_spi_priv *priv = dev_get_priv(dev);
411
412 priv->regs = plat->regs;
413 priv->extr = plat->extr;
Marek Vasutf9db3b32019-05-04 18:52:33 +0200414#if CONFIG_IS_ENABLED(CLK)
Marek Vasut6ca967b2017-11-29 06:29:46 +0100415 clk_enable(&priv->clk);
Marek Vasutf9db3b32019-05-04 18:52:33 +0200416#endif
Marek Vasut6ca967b2017-11-29 06:29:46 +0100417 return 0;
418}
419
Simon Glassaad29ae2020-12-03 16:55:21 -0700420static int rpc_spi_of_to_plat(struct udevice *bus)
Marek Vasut6ca967b2017-11-29 06:29:46 +0100421{
Simon Glassb75b15b2020-12-03 16:55:23 -0700422 struct rpc_spi_plat *plat = dev_get_plat(bus);
Marek Vasut6ca967b2017-11-29 06:29:46 +0100423
424 plat->regs = dev_read_addr_index(bus, 0);
425 plat->extr = dev_read_addr_index(bus, 1);
426
Marek Vasutf9db3b32019-05-04 18:52:33 +0200427#if CONFIG_IS_ENABLED(CLK)
428 struct rpc_spi_priv *priv = dev_get_priv(bus);
429 int ret;
430
Marek Vasut6ca967b2017-11-29 06:29:46 +0100431 ret = clk_get_by_index(bus, 0, &priv->clk);
432 if (ret < 0) {
433 printf("%s: Could not get clock for %s: %d\n",
434 __func__, bus->name, ret);
435 return ret;
436 }
Marek Vasutf9db3b32019-05-04 18:52:33 +0200437#endif
Marek Vasut6ca967b2017-11-29 06:29:46 +0100438
439 plat->freq = dev_read_u32_default(bus, "spi-max-freq", 50000000);
440
441 return 0;
442}
443
444static const struct dm_spi_ops rpc_spi_ops = {
445 .xfer = rpc_spi_xfer,
446 .set_speed = rpc_spi_set_speed,
447 .set_mode = rpc_spi_set_mode,
448};
449
450static const struct udevice_id rpc_spi_ids[] = {
Biju Das5e459202020-09-30 13:19:34 +0100451 { .compatible = "renesas,rpc-r7s72100" },
Marek Vasut6ca967b2017-11-29 06:29:46 +0100452 { .compatible = "renesas,rpc-r8a7795" },
453 { .compatible = "renesas,rpc-r8a7796" },
454 { .compatible = "renesas,rpc-r8a77965" },
455 { .compatible = "renesas,rpc-r8a77970" },
456 { .compatible = "renesas,rpc-r8a77995" },
Biju Das5e459202020-09-30 13:19:34 +0100457 { .compatible = "renesas,rcar-gen3-rpc" },
Marek Vasut6ca967b2017-11-29 06:29:46 +0100458 { }
459};
460
461U_BOOT_DRIVER(rpc_spi) = {
462 .name = "rpc_spi",
463 .id = UCLASS_SPI,
464 .of_match = rpc_spi_ids,
465 .ops = &rpc_spi_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700466 .of_to_plat = rpc_spi_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700467 .plat_auto = sizeof(struct rpc_spi_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700468 .priv_auto = sizeof(struct rpc_spi_priv),
Marek Vasut6ca967b2017-11-29 06:29:46 +0100469 .bind = rpc_spi_bind,
470 .probe = rpc_spi_probe,
471};