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