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