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