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