xref: /linux/drivers/soc/amlogic/meson-gx-socinfo.c (revision bf4afc53b77aeaa48b5409da5c8da6bb4eff7f43)
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 	{ "S905Y4", 0x37, 0x3, 0xf },
89 	{ "C308L", 0x3d, 0x1, 0xf },
90 	{ "A311D2", 0x36, 0x1, 0xf },
91 	{ "A113X2", 0x3c, 0x1, 0xf },
92 	{ "A113L2", 0x40, 0x1, 0xf },
93 	{ "S805X3", 0x46, 0x3, 0xf },
94 	{ "S905X5M", 0x47, 0x1, 0xf },
95 	{ "S905X5", 0x48, 0x1, 0xf },
96 };
97 
socinfo_to_major(u32 socinfo)98 static inline unsigned int socinfo_to_major(u32 socinfo)
99 {
100 	return FIELD_GET(SOCINFO_MAJOR, socinfo);
101 }
102 
socinfo_to_minor(u32 socinfo)103 static inline unsigned int socinfo_to_minor(u32 socinfo)
104 {
105 	return FIELD_GET(SOCINFO_MINOR, socinfo);
106 }
107 
socinfo_to_pack(u32 socinfo)108 static inline unsigned int socinfo_to_pack(u32 socinfo)
109 {
110 	return FIELD_GET(SOCINFO_PACK, socinfo);
111 }
112 
socinfo_to_misc(u32 socinfo)113 static inline unsigned int socinfo_to_misc(u32 socinfo)
114 {
115 	return FIELD_GET(SOCINFO_MISC, socinfo);
116 }
117 
socinfo_to_package_id(u32 socinfo)118 static const char *socinfo_to_package_id(u32 socinfo)
119 {
120 	unsigned int pack = socinfo_to_pack(socinfo);
121 	unsigned int major = socinfo_to_major(socinfo);
122 	int i;
123 
124 	for (i = 0 ; i < ARRAY_SIZE(soc_packages) ; ++i) {
125 		if (soc_packages[i].major_id == major &&
126 		    soc_packages[i].pack_id ==
127 				(pack & soc_packages[i].pack_mask))
128 			return soc_packages[i].name;
129 	}
130 
131 	return "Unknown";
132 }
133 
socinfo_to_soc_id(u32 socinfo)134 static const char *socinfo_to_soc_id(u32 socinfo)
135 {
136 	unsigned int id = socinfo_to_major(socinfo);
137 	int i;
138 
139 	for (i = 0 ; i < ARRAY_SIZE(soc_ids) ; ++i) {
140 		if (soc_ids[i].id == id)
141 			return soc_ids[i].name;
142 	}
143 
144 	return "Unknown";
145 }
146 
meson_gx_socinfo_init(void)147 static int __init meson_gx_socinfo_init(void)
148 {
149 	struct soc_device_attribute *soc_dev_attr;
150 	struct soc_device *soc_dev;
151 	struct device_node *np;
152 	struct regmap *regmap;
153 	unsigned int socinfo;
154 	struct device *dev;
155 	int ret;
156 
157 	/* look up for chipid node */
158 	np = of_find_compatible_node(NULL, NULL, "amlogic,meson-gx-ao-secure");
159 	if (!np)
160 		return -ENODEV;
161 
162 	/* check if interface is enabled */
163 	if (!of_device_is_available(np)) {
164 		of_node_put(np);
165 		return -ENODEV;
166 	}
167 
168 	/* check if chip-id is available */
169 	if (!of_property_read_bool(np, "amlogic,has-chip-id")) {
170 		of_node_put(np);
171 		return -ENODEV;
172 	}
173 
174 	/* node should be a syscon */
175 	regmap = syscon_node_to_regmap(np);
176 	of_node_put(np);
177 	if (IS_ERR(regmap)) {
178 		pr_err("%s: failed to get regmap\n", __func__);
179 		return -ENODEV;
180 	}
181 
182 	ret = regmap_read(regmap, AO_SEC_SOCINFO_OFFSET, &socinfo);
183 	if (ret < 0)
184 		return ret;
185 
186 	if (!socinfo) {
187 		pr_err("%s: invalid chipid value\n", __func__);
188 		return -EINVAL;
189 	}
190 
191 	soc_dev_attr = kzalloc_obj(*soc_dev_attr);
192 	if (!soc_dev_attr)
193 		return -ENODEV;
194 
195 	soc_dev_attr->family = "Amlogic Meson";
196 	soc_dev_attr->revision = kasprintf(GFP_KERNEL, "%x:%x - %x:%x",
197 					   socinfo_to_major(socinfo),
198 					   socinfo_to_minor(socinfo),
199 					   socinfo_to_pack(socinfo),
200 					   socinfo_to_misc(socinfo));
201 	soc_dev_attr->soc_id = kasprintf(GFP_KERNEL, "%s (%s)",
202 					 socinfo_to_soc_id(socinfo),
203 					 socinfo_to_package_id(socinfo));
204 
205 	soc_dev = soc_device_register(soc_dev_attr);
206 	if (IS_ERR(soc_dev)) {
207 		kfree(soc_dev_attr->revision);
208 		kfree_const(soc_dev_attr->soc_id);
209 		kfree(soc_dev_attr);
210 		return PTR_ERR(soc_dev);
211 	}
212 	dev = soc_device_to_device(soc_dev);
213 
214 	dev_info(dev, "Amlogic Meson %s Revision %x:%x (%x:%x) Detected\n",
215 			soc_dev_attr->soc_id,
216 			socinfo_to_major(socinfo),
217 			socinfo_to_minor(socinfo),
218 			socinfo_to_pack(socinfo),
219 			socinfo_to_misc(socinfo));
220 
221 	return 0;
222 }
223 device_initcall(meson_gx_socinfo_init);
224