xref: /linux/drivers/soc/aspeed/aspeed-socinfo.c (revision 0f0c9c702241d839dbb1d355b77e5712a5a5793f)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Copyright 2019 IBM Corp. */
3 
4 #include <linux/io.h>
5 #include <linux/of.h>
6 #include <linux/of_address.h>
7 #include <linux/of_platform.h>
8 #include <linux/platform_device.h>
9 #include <linux/slab.h>
10 #include <linux/sys_soc.h>
11 
12 static struct {
13 	const char *name;
14 	const u32 id;
15 } const rev_table[] = {
16 	/* AST2400 */
17 	{ "AST2400", 0x02000303 },
18 	{ "AST1400", 0x02010103 },
19 	{ "AST1250", 0x02010303 },
20 	/* AST2500 */
21 	{ "AST2500", 0x04000303 },
22 	{ "AST2510", 0x04000103 },
23 	{ "AST2520", 0x04000203 },
24 	{ "AST2530", 0x04000403 },
25 	/* AST2600 */
26 	{ "AST2600", 0x05000303 },
27 	{ "AST2620", 0x05010203 },
28 };
29 
30 static const char *siliconid_to_name(u32 siliconid)
31 {
32 	unsigned int id = siliconid & 0xff00ffff;
33 	unsigned int i;
34 
35 	for (i = 0 ; i < ARRAY_SIZE(rev_table) ; ++i) {
36 		if (rev_table[i].id == id)
37 			return rev_table[i].name;
38 	}
39 
40 	return "Unknown";
41 }
42 
43 static const char *siliconid_to_rev(u32 siliconid)
44 {
45 	unsigned int rev = (siliconid >> 16) & 0xff;
46 
47 	switch (rev) {
48 	case 0:
49 		return "A0";
50 	case 1:
51 		return "A1";
52 	case 3:
53 		return "A2";
54 	}
55 
56 	return "??";
57 }
58 
59 static int __init aspeed_socinfo_init(void)
60 {
61 	struct soc_device_attribute *attrs;
62 	struct soc_device *soc_dev;
63 	struct device_node *np;
64 	void __iomem *reg;
65 	bool has_chipid = false;
66 	u32 siliconid;
67 	u32 chipid[2];
68 	const char *machine = NULL;
69 
70 	np = of_find_compatible_node(NULL, NULL, "aspeed,silicon-id");
71 	if (!of_device_is_available(np)) {
72 		of_node_put(np);
73 		return -ENODEV;
74 	}
75 
76 	reg = of_iomap(np, 0);
77 	if (!reg) {
78 		of_node_put(np);
79 		return -ENODEV;
80 	}
81 	siliconid = readl(reg);
82 	iounmap(reg);
83 
84 	/* This is optional, the ast2400 does not have it */
85 	reg = of_iomap(np, 1);
86 	if (reg) {
87 		has_chipid = true;
88 		chipid[0] = readl(reg);
89 		chipid[1] = readl(reg + 4);
90 		iounmap(reg);
91 	}
92 	of_node_put(np);
93 
94 	attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
95 	if (!attrs)
96 		return -ENODEV;
97 
98 	/*
99 	 * Machine: Romulus BMC
100 	 * Family: AST2500
101 	 * Revision: A1
102 	 * SoC ID: raw silicon revision id
103 	 * Serial Number: 64-bit chipid
104 	 */
105 
106 	np = of_find_node_by_path("/");
107 	of_property_read_string(np, "model", &machine);
108 	if (machine)
109 		attrs->machine = kstrdup(machine, GFP_KERNEL);
110 	of_node_put(np);
111 
112 	attrs->family = siliconid_to_name(siliconid);
113 	attrs->revision = siliconid_to_rev(siliconid);
114 	attrs->soc_id = kasprintf(GFP_KERNEL, "%08x", siliconid);
115 
116 	if (has_chipid)
117 		attrs->serial_number = kasprintf(GFP_KERNEL, "%08x%08x",
118 						 chipid[1], chipid[0]);
119 
120 	soc_dev = soc_device_register(attrs);
121 	if (IS_ERR(soc_dev)) {
122 		kfree(attrs->soc_id);
123 		kfree(attrs->serial_number);
124 		kfree(attrs);
125 		return PTR_ERR(soc_dev);
126 	}
127 
128 	pr_info("ASPEED %s rev %s (%s)\n",
129 			attrs->family,
130 			attrs->revision,
131 			attrs->soc_id);
132 
133 	return 0;
134 }
135 early_initcall(aspeed_socinfo_init);
136