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