xref: /linux/drivers/soc/amlogic/meson-gx-socinfo.c (revision 11efc1cb7016e300047822fd60e0f4b4158bd56d)
1 /*
2  * Copyright (c) 2017 BayLibre, SAS
3  * Author: Neil Armstrong <narmstrong@baylibre.com>
4  *
5  * SPDX-License-Identifier: GPL-2.0+
6  */
7 
8 #include <linux/io.h>
9 #include <linux/of.h>
10 #include <linux/of_address.h>
11 #include <linux/of_platform.h>
12 #include <linux/platform_device.h>
13 #include <linux/slab.h>
14 #include <linux/sys_soc.h>
15 #include <linux/bitfield.h>
16 #include <linux/regmap.h>
17 #include <linux/mfd/syscon.h>
18 
19 #define AO_SEC_SD_CFG8		0xe0
20 #define AO_SEC_SOCINFO_OFFSET	AO_SEC_SD_CFG8
21 
22 #define SOCINFO_MAJOR	GENMASK(31, 24)
23 #define SOCINFO_PACK	GENMASK(23, 16)
24 #define SOCINFO_MINOR	GENMASK(15, 8)
25 #define SOCINFO_MISC	GENMASK(7, 0)
26 
27 static const struct meson_gx_soc_id {
28 	const char *name;
29 	unsigned int id;
30 } soc_ids[] = {
31 	{ "GXBB", 0x1f },
32 	{ "GXTVBB", 0x20 },
33 	{ "GXL", 0x21 },
34 	{ "GXM", 0x22 },
35 	{ "TXL", 0x23 },
36 	{ "TXLX", 0x24 },
37 	{ "AXG", 0x25 },
38 	{ "GXLX", 0x26 },
39 	{ "TXHD", 0x27 },
40 	{ "G12A", 0x28 },
41 	{ "G12B", 0x29 },
42 	{ "SM1", 0x2b },
43 	{ "A1", 0x2c },
44 	{ "T7", 0x36 },
45 	{ "S4", 0x37 },
46 	{ "A5", 0x3c },
47 	{ "C3", 0x3d },
48 	{ "A4", 0x40 },
49 	{ "S7", 0x46 },
50 	{ "S7D", 0x47 },
51 	{ "S6", 0x48 },
52 };
53 
54 static const struct meson_gx_package_id {
55 	const char *name;
56 	unsigned int major_id;
57 	unsigned int pack_id;
58 	unsigned int pack_mask;
59 } soc_packages[] = {
60 	{ "S905", 0x1f, 0, 0x20 }, /* pack_id != 0x20 */
61 	{ "S905H", 0x1f, 0x3, 0xf }, /* pack_id & 0xf == 0x3 */
62 	{ "S905M", 0x1f, 0x20, 0xf0 }, /* pack_id == 0x20 */
63 	{ "S905D", 0x21, 0, 0xf0 },
64 	{ "S905X", 0x21, 0x80, 0xf0 },
65 	{ "S905W", 0x21, 0xa0, 0xf0 },
66 	{ "S905L", 0x21, 0xc0, 0xf0 },
67 	{ "S905M2", 0x21, 0xe0, 0xf0 },
68 	{ "S805X", 0x21, 0x30, 0xf0 },
69 	{ "S805Y", 0x21, 0xb0, 0xf0 },
70 	{ "S912", 0x22, 0, 0x0 }, /* Only S912 is known for GXM */
71 	{ "962X", 0x24, 0x10, 0xf0 },
72 	{ "962E", 0x24, 0x20, 0xf0 },
73 	{ "A113X", 0x25, 0x37, 0xff },
74 	{ "A113X", 0x25, 0x43, 0xff },
75 	{ "A113D", 0x25, 0x22, 0xff },
76 	{ "S905L", 0x26, 0, 0x0 },
77 	{ "S905D2", 0x28, 0x10, 0xf0 },
78 	{ "S905Y2", 0x28, 0x30, 0xf0 },
79 	{ "S905X2", 0x28, 0x40, 0xf0 },
80 	{ "A311D", 0x29, 0x10, 0xf0 },
81 	{ "S922X", 0x29, 0x40, 0xf0 },
82 	{ "S905D3", 0x2b, 0x4, 0xf5 },
83 	{ "S905X3", 0x2b, 0x5, 0xf5 },
84 	{ "S905X3", 0x2b, 0x10, 0x3f },
85 	{ "S905D3", 0x2b, 0x30, 0x3f },
86 	{ "A113L", 0x2c, 0x0, 0xf8 },
87 	{ "S805X2", 0x37, 0x2, 0xf },
88 	{ "C308L", 0x3d, 0x1, 0xf },
89 	{ "A311D2", 0x36, 0x1, 0xf },
90 	{ "A113X2", 0x3c, 0x1, 0xf },
91 	{ "A113L2", 0x40, 0x1, 0xf },
92 	{ "S805X3", 0x46, 0x3, 0xf },
93 	{ "S905X5M", 0x47, 0x1, 0xf },
94 	{ "S905X5", 0x48, 0x1, 0xf },
95 };
96 
socinfo_to_major(u32 socinfo)97 static inline unsigned int socinfo_to_major(u32 socinfo)
98 {
99 	return FIELD_GET(SOCINFO_MAJOR, socinfo);
100 }
101 
socinfo_to_minor(u32 socinfo)102 static inline unsigned int socinfo_to_minor(u32 socinfo)
103 {
104 	return FIELD_GET(SOCINFO_MINOR, socinfo);
105 }
106 
socinfo_to_pack(u32 socinfo)107 static inline unsigned int socinfo_to_pack(u32 socinfo)
108 {
109 	return FIELD_GET(SOCINFO_PACK, socinfo);
110 }
111 
socinfo_to_misc(u32 socinfo)112 static inline unsigned int socinfo_to_misc(u32 socinfo)
113 {
114 	return FIELD_GET(SOCINFO_MISC, socinfo);
115 }
116 
socinfo_to_package_id(u32 socinfo)117 static const char *socinfo_to_package_id(u32 socinfo)
118 {
119 	unsigned int pack = socinfo_to_pack(socinfo);
120 	unsigned int major = socinfo_to_major(socinfo);
121 	int i;
122 
123 	for (i = 0 ; i < ARRAY_SIZE(soc_packages) ; ++i) {
124 		if (soc_packages[i].major_id == major &&
125 		    soc_packages[i].pack_id ==
126 				(pack & soc_packages[i].pack_mask))
127 			return soc_packages[i].name;
128 	}
129 
130 	return "Unknown";
131 }
132 
socinfo_to_soc_id(u32 socinfo)133 static const char *socinfo_to_soc_id(u32 socinfo)
134 {
135 	unsigned int id = socinfo_to_major(socinfo);
136 	int i;
137 
138 	for (i = 0 ; i < ARRAY_SIZE(soc_ids) ; ++i) {
139 		if (soc_ids[i].id == id)
140 			return soc_ids[i].name;
141 	}
142 
143 	return "Unknown";
144 }
145 
meson_gx_socinfo_init(void)146 static int __init meson_gx_socinfo_init(void)
147 {
148 	struct soc_device_attribute *soc_dev_attr;
149 	struct soc_device *soc_dev;
150 	struct device_node *np;
151 	struct regmap *regmap;
152 	unsigned int socinfo;
153 	struct device *dev;
154 	int ret;
155 
156 	/* look up for chipid node */
157 	np = of_find_compatible_node(NULL, NULL, "amlogic,meson-gx-ao-secure");
158 	if (!np)
159 		return -ENODEV;
160 
161 	/* check if interface is enabled */
162 	if (!of_device_is_available(np)) {
163 		of_node_put(np);
164 		return -ENODEV;
165 	}
166 
167 	/* check if chip-id is available */
168 	if (!of_property_read_bool(np, "amlogic,has-chip-id")) {
169 		of_node_put(np);
170 		return -ENODEV;
171 	}
172 
173 	/* node should be a syscon */
174 	regmap = syscon_node_to_regmap(np);
175 	of_node_put(np);
176 	if (IS_ERR(regmap)) {
177 		pr_err("%s: failed to get regmap\n", __func__);
178 		return -ENODEV;
179 	}
180 
181 	ret = regmap_read(regmap, AO_SEC_SOCINFO_OFFSET, &socinfo);
182 	if (ret < 0)
183 		return ret;
184 
185 	if (!socinfo) {
186 		pr_err("%s: invalid chipid value\n", __func__);
187 		return -EINVAL;
188 	}
189 
190 	soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
191 	if (!soc_dev_attr)
192 		return -ENODEV;
193 
194 	soc_dev_attr->family = "Amlogic Meson";
195 	soc_dev_attr->revision = kasprintf(GFP_KERNEL, "%x:%x - %x:%x",
196 					   socinfo_to_major(socinfo),
197 					   socinfo_to_minor(socinfo),
198 					   socinfo_to_pack(socinfo),
199 					   socinfo_to_misc(socinfo));
200 	soc_dev_attr->soc_id = kasprintf(GFP_KERNEL, "%s (%s)",
201 					 socinfo_to_soc_id(socinfo),
202 					 socinfo_to_package_id(socinfo));
203 
204 	soc_dev = soc_device_register(soc_dev_attr);
205 	if (IS_ERR(soc_dev)) {
206 		kfree(soc_dev_attr->revision);
207 		kfree_const(soc_dev_attr->soc_id);
208 		kfree(soc_dev_attr);
209 		return PTR_ERR(soc_dev);
210 	}
211 	dev = soc_device_to_device(soc_dev);
212 
213 	dev_info(dev, "Amlogic Meson %s Revision %x:%x (%x:%x) Detected\n",
214 			soc_dev_attr->soc_id,
215 			socinfo_to_major(socinfo),
216 			socinfo_to_minor(socinfo),
217 			socinfo_to_pack(socinfo),
218 			socinfo_to_misc(socinfo));
219 
220 	return 0;
221 }
222 device_initcall(meson_gx_socinfo_init);
223