1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Renesas SoC Identification
4  *
5  * Copyright (C) 2014-2016 Glider bvba
6  */
7 
8 #include <linux/io.h>
9 #include <linux/of.h>
10 #include <linux/of_address.h>
11 #include <linux/slab.h>
12 #include <linux/string.h>
13 #include <linux/sys_soc.h>
14 
15 
16 struct renesas_family {
17 	const char name[16];
18 	u32 reg;			/* CCCR or PRR, if not in DT */
19 };
20 
21 static const struct renesas_family fam_rcar_gen1 __initconst __maybe_unused = {
22 	.name	= "R-Car Gen1",
23 	.reg	= 0xff000044,		/* PRR (Product Register) */
24 };
25 
26 static const struct renesas_family fam_rcar_gen2 __initconst __maybe_unused = {
27 	.name	= "R-Car Gen2",
28 	.reg	= 0xff000044,		/* PRR (Product Register) */
29 };
30 
31 static const struct renesas_family fam_rcar_gen3 __initconst __maybe_unused = {
32 	.name	= "R-Car Gen3",
33 	.reg	= 0xfff00044,		/* PRR (Product Register) */
34 };
35 
36 static const struct renesas_family fam_rcar_gen4 __initconst __maybe_unused = {
37 	.name	= "R-Car Gen4",
38 };
39 
40 static const struct renesas_family fam_rmobile __initconst __maybe_unused = {
41 	.name	= "R-Mobile",
42 	.reg	= 0xe600101c,		/* CCCR (Common Chip Code Register) */
43 };
44 
45 static const struct renesas_family fam_rza1 __initconst __maybe_unused = {
46 	.name	= "RZ/A1",
47 };
48 
49 static const struct renesas_family fam_rza2 __initconst __maybe_unused = {
50 	.name	= "RZ/A2",
51 };
52 
53 static const struct renesas_family fam_rzfive __initconst __maybe_unused = {
54 	.name	= "RZ/Five",
55 };
56 
57 static const struct renesas_family fam_rzg1 __initconst __maybe_unused = {
58 	.name	= "RZ/G1",
59 	.reg	= 0xff000044,		/* PRR (Product Register) */
60 };
61 
62 static const struct renesas_family fam_rzg2 __initconst __maybe_unused = {
63 	.name	= "RZ/G2",
64 	.reg	= 0xfff00044,		/* PRR (Product Register) */
65 };
66 
67 static const struct renesas_family fam_rzg2l __initconst __maybe_unused = {
68 	.name	= "RZ/G2L",
69 };
70 
71 static const struct renesas_family fam_rzg2ul __initconst __maybe_unused = {
72 	.name	= "RZ/G2UL",
73 };
74 
75 static const struct renesas_family fam_rzv2l __initconst __maybe_unused = {
76 	.name	= "RZ/V2L",
77 };
78 
79 static const struct renesas_family fam_rzv2m __initconst __maybe_unused = {
80 	.name	= "RZ/V2M",
81 };
82 
83 static const struct renesas_family fam_shmobile __initconst __maybe_unused = {
84 	.name	= "SH-Mobile",
85 	.reg	= 0xe600101c,		/* CCCR (Common Chip Code Register) */
86 };
87 
88 
89 struct renesas_soc {
90 	const struct renesas_family *family;
91 	u32 id;
92 };
93 
94 static const struct renesas_soc soc_rz_a1h __initconst __maybe_unused = {
95 	.family	= &fam_rza1,
96 };
97 
98 static const struct renesas_soc soc_rz_a2m __initconst __maybe_unused = {
99 	.family	= &fam_rza2,
100 	.id	= 0x3b,
101 };
102 
103 static const struct renesas_soc soc_rmobile_ape6 __initconst __maybe_unused = {
104 	.family	= &fam_rmobile,
105 	.id	= 0x3f,
106 };
107 
108 static const struct renesas_soc soc_rmobile_a1 __initconst __maybe_unused = {
109 	.family	= &fam_rmobile,
110 	.id	= 0x40,
111 };
112 
113 static const struct renesas_soc soc_rz_five __initconst __maybe_unused = {
114 	.family = &fam_rzfive,
115 	.id     = 0x847c447,
116 };
117 
118 static const struct renesas_soc soc_rz_g1h __initconst __maybe_unused = {
119 	.family	= &fam_rzg1,
120 	.id	= 0x45,
121 };
122 
123 static const struct renesas_soc soc_rz_g1m __initconst __maybe_unused = {
124 	.family	= &fam_rzg1,
125 	.id	= 0x47,
126 };
127 
128 static const struct renesas_soc soc_rz_g1n __initconst __maybe_unused = {
129 	.family	= &fam_rzg1,
130 	.id	= 0x4b,
131 };
132 
133 static const struct renesas_soc soc_rz_g1e __initconst __maybe_unused = {
134 	.family	= &fam_rzg1,
135 	.id	= 0x4c,
136 };
137 
138 static const struct renesas_soc soc_rz_g1c __initconst __maybe_unused = {
139 	.family	= &fam_rzg1,
140 	.id	= 0x53,
141 };
142 
143 static const struct renesas_soc soc_rz_g2m __initconst __maybe_unused = {
144 	.family	= &fam_rzg2,
145 	.id	= 0x52,
146 };
147 
148 static const struct renesas_soc soc_rz_g2n __initconst __maybe_unused = {
149 	.family = &fam_rzg2,
150 	.id     = 0x55,
151 };
152 
153 static const struct renesas_soc soc_rz_g2e __initconst __maybe_unused = {
154 	.family	= &fam_rzg2,
155 	.id	= 0x57,
156 };
157 
158 static const struct renesas_soc soc_rz_g2h __initconst __maybe_unused = {
159 	.family	= &fam_rzg2,
160 	.id	= 0x4f,
161 };
162 
163 static const struct renesas_soc soc_rz_g2l __initconst __maybe_unused = {
164 	.family = &fam_rzg2l,
165 	.id     = 0x841c447,
166 };
167 
168 static const struct renesas_soc soc_rz_g2ul __initconst __maybe_unused = {
169 	.family = &fam_rzg2ul,
170 	.id     = 0x8450447,
171 };
172 
173 static const struct renesas_soc soc_rz_v2l __initconst __maybe_unused = {
174 	.family = &fam_rzv2l,
175 	.id     = 0x8447447,
176 };
177 
178 static const struct renesas_soc soc_rz_v2m __initconst __maybe_unused = {
179 	.family = &fam_rzv2m,
180 };
181 
182 static const struct renesas_soc soc_rcar_m1a __initconst __maybe_unused = {
183 	.family	= &fam_rcar_gen1,
184 };
185 
186 static const struct renesas_soc soc_rcar_h1 __initconst __maybe_unused = {
187 	.family	= &fam_rcar_gen1,
188 	.id	= 0x3b,
189 };
190 
191 static const struct renesas_soc soc_rcar_h2 __initconst __maybe_unused = {
192 	.family	= &fam_rcar_gen2,
193 	.id	= 0x45,
194 };
195 
196 static const struct renesas_soc soc_rcar_m2_w __initconst __maybe_unused = {
197 	.family	= &fam_rcar_gen2,
198 	.id	= 0x47,
199 };
200 
201 static const struct renesas_soc soc_rcar_v2h __initconst __maybe_unused = {
202 	.family	= &fam_rcar_gen2,
203 	.id	= 0x4a,
204 };
205 
206 static const struct renesas_soc soc_rcar_m2_n __initconst __maybe_unused = {
207 	.family	= &fam_rcar_gen2,
208 	.id	= 0x4b,
209 };
210 
211 static const struct renesas_soc soc_rcar_e2 __initconst __maybe_unused = {
212 	.family	= &fam_rcar_gen2,
213 	.id	= 0x4c,
214 };
215 
216 static const struct renesas_soc soc_rcar_h3 __initconst __maybe_unused = {
217 	.family	= &fam_rcar_gen3,
218 	.id	= 0x4f,
219 };
220 
221 static const struct renesas_soc soc_rcar_m3_w __initconst __maybe_unused = {
222 	.family	= &fam_rcar_gen3,
223 	.id	= 0x52,
224 };
225 
226 static const struct renesas_soc soc_rcar_m3_n __initconst __maybe_unused = {
227 	.family = &fam_rcar_gen3,
228 	.id     = 0x55,
229 };
230 
231 static const struct renesas_soc soc_rcar_v3m __initconst __maybe_unused = {
232 	.family	= &fam_rcar_gen3,
233 	.id	= 0x54,
234 };
235 
236 static const struct renesas_soc soc_rcar_v3h __initconst __maybe_unused = {
237 	.family	= &fam_rcar_gen3,
238 	.id	= 0x56,
239 };
240 
241 static const struct renesas_soc soc_rcar_e3 __initconst __maybe_unused = {
242 	.family	= &fam_rcar_gen3,
243 	.id	= 0x57,
244 };
245 
246 static const struct renesas_soc soc_rcar_d3 __initconst __maybe_unused = {
247 	.family	= &fam_rcar_gen3,
248 	.id	= 0x58,
249 };
250 
251 static const struct renesas_soc soc_rcar_v3u __initconst __maybe_unused = {
252 	.family	= &fam_rcar_gen4,
253 	.id	= 0x59,
254 };
255 
256 static const struct renesas_soc soc_rcar_s4 __initconst __maybe_unused = {
257 	.family	= &fam_rcar_gen4,
258 	.id	= 0x5a,
259 };
260 
261 static const struct renesas_soc soc_rcar_v4h __initconst __maybe_unused = {
262 	.family	= &fam_rcar_gen4,
263 	.id	= 0x5c,
264 };
265 
266 static const struct renesas_soc soc_shmobile_ag5 __initconst __maybe_unused = {
267 	.family	= &fam_shmobile,
268 	.id	= 0x37,
269 };
270 
271 
272 static const struct of_device_id renesas_socs[] __initconst __maybe_unused = {
273 #ifdef CONFIG_ARCH_R7S72100
274 	{ .compatible = "renesas,r7s72100",	.data = &soc_rz_a1h },
275 #endif
276 #ifdef CONFIG_ARCH_R7S9210
277 	{ .compatible = "renesas,r7s9210",	.data = &soc_rz_a2m },
278 #endif
279 #ifdef CONFIG_ARCH_R8A73A4
280 	{ .compatible = "renesas,r8a73a4",	.data = &soc_rmobile_ape6 },
281 #endif
282 #ifdef CONFIG_ARCH_R8A7740
283 	{ .compatible = "renesas,r8a7740",	.data = &soc_rmobile_a1 },
284 #endif
285 #ifdef CONFIG_ARCH_R8A7742
286 	{ .compatible = "renesas,r8a7742",	.data = &soc_rz_g1h },
287 #endif
288 #ifdef CONFIG_ARCH_R8A7743
289 	{ .compatible = "renesas,r8a7743",	.data = &soc_rz_g1m },
290 #endif
291 #ifdef CONFIG_ARCH_R8A7744
292 	{ .compatible = "renesas,r8a7744",	.data = &soc_rz_g1n },
293 #endif
294 #ifdef CONFIG_ARCH_R8A7745
295 	{ .compatible = "renesas,r8a7745",	.data = &soc_rz_g1e },
296 #endif
297 #ifdef CONFIG_ARCH_R8A77470
298 	{ .compatible = "renesas,r8a77470",	.data = &soc_rz_g1c },
299 #endif
300 #ifdef CONFIG_ARCH_R8A774A1
301 	{ .compatible = "renesas,r8a774a1",	.data = &soc_rz_g2m },
302 #endif
303 #ifdef CONFIG_ARCH_R8A774B1
304 	{ .compatible = "renesas,r8a774b1",	.data = &soc_rz_g2n },
305 #endif
306 #ifdef CONFIG_ARCH_R8A774C0
307 	{ .compatible = "renesas,r8a774c0",	.data = &soc_rz_g2e },
308 #endif
309 #ifdef CONFIG_ARCH_R8A774E1
310 	{ .compatible = "renesas,r8a774e1",	.data = &soc_rz_g2h },
311 #endif
312 #ifdef CONFIG_ARCH_R8A7778
313 	{ .compatible = "renesas,r8a7778",	.data = &soc_rcar_m1a },
314 #endif
315 #ifdef CONFIG_ARCH_R8A7779
316 	{ .compatible = "renesas,r8a7779",	.data = &soc_rcar_h1 },
317 #endif
318 #ifdef CONFIG_ARCH_R8A7790
319 	{ .compatible = "renesas,r8a7790",	.data = &soc_rcar_h2 },
320 #endif
321 #ifdef CONFIG_ARCH_R8A7791
322 	{ .compatible = "renesas,r8a7791",	.data = &soc_rcar_m2_w },
323 #endif
324 #ifdef CONFIG_ARCH_R8A7792
325 	{ .compatible = "renesas,r8a7792",	.data = &soc_rcar_v2h },
326 #endif
327 #ifdef CONFIG_ARCH_R8A7793
328 	{ .compatible = "renesas,r8a7793",	.data = &soc_rcar_m2_n },
329 #endif
330 #ifdef CONFIG_ARCH_R8A7794
331 	{ .compatible = "renesas,r8a7794",	.data = &soc_rcar_e2 },
332 #endif
333 #ifdef CONFIG_ARCH_R8A77951
334 	{ .compatible = "renesas,r8a7795",	.data = &soc_rcar_h3 },
335 	{ .compatible = "renesas,r8a779m0",	.data = &soc_rcar_h3 },
336 	{ .compatible = "renesas,r8a779m1",	.data = &soc_rcar_h3 },
337 	{ .compatible = "renesas,r8a779m8",	.data = &soc_rcar_h3 },
338 	{ .compatible = "renesas,r8a779mb",	.data = &soc_rcar_h3 },
339 #endif
340 #ifdef CONFIG_ARCH_R8A77960
341 	{ .compatible = "renesas,r8a7796",	.data = &soc_rcar_m3_w },
342 #endif
343 #ifdef CONFIG_ARCH_R8A77961
344 	{ .compatible = "renesas,r8a77961",	.data = &soc_rcar_m3_w },
345 	{ .compatible = "renesas,r8a779m2",	.data = &soc_rcar_m3_w },
346 	{ .compatible = "renesas,r8a779m3",	.data = &soc_rcar_m3_w },
347 #endif
348 #ifdef CONFIG_ARCH_R8A77965
349 	{ .compatible = "renesas,r8a77965",	.data = &soc_rcar_m3_n },
350 	{ .compatible = "renesas,r8a779m4",	.data = &soc_rcar_m3_n },
351 	{ .compatible = "renesas,r8a779m5",	.data = &soc_rcar_m3_n },
352 #endif
353 #ifdef CONFIG_ARCH_R8A77970
354 	{ .compatible = "renesas,r8a77970",	.data = &soc_rcar_v3m },
355 #endif
356 #ifdef CONFIG_ARCH_R8A77980
357 	{ .compatible = "renesas,r8a77980",	.data = &soc_rcar_v3h },
358 #endif
359 #ifdef CONFIG_ARCH_R8A77990
360 	{ .compatible = "renesas,r8a77990",	.data = &soc_rcar_e3 },
361 	{ .compatible = "renesas,r8a779m6",	.data = &soc_rcar_e3 },
362 #endif
363 #ifdef CONFIG_ARCH_R8A77995
364 	{ .compatible = "renesas,r8a77995",	.data = &soc_rcar_d3 },
365 	{ .compatible = "renesas,r8a779m7",	.data = &soc_rcar_d3 },
366 #endif
367 #ifdef CONFIG_ARCH_R8A779A0
368 	{ .compatible = "renesas,r8a779a0",	.data = &soc_rcar_v3u },
369 #endif
370 #ifdef CONFIG_ARCH_R8A779F0
371 	{ .compatible = "renesas,r8a779f0",	.data = &soc_rcar_s4 },
372 #endif
373 #ifdef CONFIG_ARCH_R8A779G0
374 	{ .compatible = "renesas,r8a779g0",	.data = &soc_rcar_v4h },
375 #endif
376 #ifdef CONFIG_ARCH_R9A07G043
377 #ifdef CONFIG_RISCV
378 	{ .compatible = "renesas,r9a07g043",	.data = &soc_rz_five },
379 #else
380 	{ .compatible = "renesas,r9a07g043",	.data = &soc_rz_g2ul },
381 #endif
382 #endif
383 #ifdef CONFIG_ARCH_R9A07G044
384 	{ .compatible = "renesas,r9a07g044",	.data = &soc_rz_g2l },
385 #endif
386 #ifdef CONFIG_ARCH_R9A07G054
387 	{ .compatible = "renesas,r9a07g054",	.data = &soc_rz_v2l },
388 #endif
389 #ifdef CONFIG_ARCH_R9A09G011
390 	{ .compatible = "renesas,r9a09g011",	.data = &soc_rz_v2m },
391 #endif
392 #ifdef CONFIG_ARCH_SH73A0
393 	{ .compatible = "renesas,sh73a0",	.data = &soc_shmobile_ag5 },
394 #endif
395 	{ /* sentinel */ }
396 };
397 
398 struct renesas_id {
399 	unsigned int offset;
400 	u32 mask;
401 };
402 
403 static const struct renesas_id id_bsid __initconst = {
404 	.offset = 0,
405 	.mask = 0xff0000,
406 	/*
407 	 * TODO: Upper 4 bits of BSID are for chip version, but the format is
408 	 * not known at this time so we don't know how to specify eshi and eslo
409 	 */
410 };
411 
412 static const struct renesas_id id_rzg2l __initconst = {
413 	.offset = 0xa04,
414 	.mask = 0xfffffff,
415 };
416 
417 static const struct renesas_id id_rzv2m __initconst = {
418 	.offset = 0x104,
419 	.mask = 0xff,
420 };
421 
422 static const struct renesas_id id_prr __initconst = {
423 	.offset = 0,
424 	.mask = 0xff00,
425 };
426 
427 static const struct of_device_id renesas_ids[] __initconst = {
428 	{ .compatible = "renesas,bsid",			.data = &id_bsid },
429 	{ .compatible = "renesas,r9a07g043-sysc",	.data = &id_rzg2l },
430 	{ .compatible = "renesas,r9a07g044-sysc",	.data = &id_rzg2l },
431 	{ .compatible = "renesas,r9a07g054-sysc",	.data = &id_rzg2l },
432 	{ .compatible = "renesas,r9a09g011-sys",	.data = &id_rzv2m },
433 	{ .compatible = "renesas,prr",			.data = &id_prr },
434 	{ /* sentinel */ }
435 };
436 
renesas_soc_init(void)437 static int __init renesas_soc_init(void)
438 {
439 	struct soc_device_attribute *soc_dev_attr;
440 	unsigned int product, eshi = 0, eslo;
441 	const struct renesas_family *family;
442 	const struct of_device_id *match;
443 	const struct renesas_soc *soc;
444 	const struct renesas_id *id;
445 	void __iomem *chipid = NULL;
446 	const char *rev_prefix = "";
447 	struct soc_device *soc_dev;
448 	struct device_node *np;
449 	const char *soc_id;
450 	int ret;
451 
452 	match = of_match_node(renesas_socs, of_root);
453 	if (!match)
454 		return -ENODEV;
455 
456 	soc_id = strchr(match->compatible, ',') + 1;
457 	soc = match->data;
458 	family = soc->family;
459 
460 	np = of_find_matching_node_and_match(NULL, renesas_ids, &match);
461 	if (np) {
462 		id = match->data;
463 		chipid = of_iomap(np, 0);
464 		of_node_put(np);
465 	} else if (soc->id && family->reg) {
466 		/* Try hardcoded CCCR/PRR fallback */
467 		id = &id_prr;
468 		chipid = ioremap(family->reg, 4);
469 	}
470 
471 	soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
472 	if (!soc_dev_attr) {
473 		if (chipid)
474 			iounmap(chipid);
475 		return -ENOMEM;
476 	}
477 
478 	np = of_find_node_by_path("/");
479 	of_property_read_string(np, "model", &soc_dev_attr->machine);
480 	of_node_put(np);
481 
482 	soc_dev_attr->family = kstrdup_const(family->name, GFP_KERNEL);
483 	soc_dev_attr->soc_id = kstrdup_const(soc_id, GFP_KERNEL);
484 
485 	if (chipid) {
486 		product = readl(chipid + id->offset);
487 		iounmap(chipid);
488 
489 		if (id == &id_prr) {
490 			/* R-Car M3-W ES1.1 incorrectly identifies as ES2.0 */
491 			if ((product & 0x7fff) == 0x5210)
492 				product ^= 0x11;
493 			/* R-Car M3-W ES1.3 incorrectly identifies as ES2.1 */
494 			if ((product & 0x7fff) == 0x5211)
495 				product ^= 0x12;
496 
497 			eshi = ((product >> 4) & 0x0f) + 1;
498 			eslo = product & 0xf;
499 			soc_dev_attr->revision = kasprintf(GFP_KERNEL, "ES%u.%u",
500 							   eshi, eslo);
501 		}  else if (id == &id_rzg2l) {
502 			eshi =  ((product >> 28) & 0x0f);
503 			soc_dev_attr->revision = kasprintf(GFP_KERNEL, "%u",
504 							   eshi);
505 			rev_prefix = "Rev ";
506 		} else if (id == &id_rzv2m) {
507 			eshi = ((product >> 4) & 0x0f);
508 			eslo = product & 0xf;
509 			soc_dev_attr->revision = kasprintf(GFP_KERNEL, "%u.%u",
510 							   eshi, eslo);
511 		}
512 
513 		if (soc->id &&
514 		    ((product & id->mask) >> __ffs(id->mask)) != soc->id) {
515 			pr_warn("SoC mismatch (product = 0x%x)\n", product);
516 			ret = -ENODEV;
517 			goto free_soc_dev_attr;
518 		}
519 	}
520 
521 	pr_info("Detected Renesas %s %s %s%s\n", soc_dev_attr->family,
522 		soc_dev_attr->soc_id, rev_prefix, soc_dev_attr->revision ?: "");
523 
524 	soc_dev = soc_device_register(soc_dev_attr);
525 	if (IS_ERR(soc_dev)) {
526 		ret = PTR_ERR(soc_dev);
527 		goto free_soc_dev_attr;
528 	}
529 
530 	return 0;
531 
532 free_soc_dev_attr:
533 	kfree(soc_dev_attr->revision);
534 	kfree_const(soc_dev_attr->soc_id);
535 	kfree_const(soc_dev_attr->family);
536 	kfree(soc_dev_attr);
537 	return ret;
538 }
539 early_initcall(renesas_soc_init);
540