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 };
45 
46 static const struct meson_gx_package_id {
47 	const char *name;
48 	unsigned int major_id;
49 	unsigned int pack_id;
50 	unsigned int pack_mask;
51 } soc_packages[] = {
52 	{ "S905", 0x1f, 0, 0x20 }, /* pack_id != 0x20 */
53 	{ "S905H", 0x1f, 0x3, 0xf }, /* pack_id & 0xf == 0x3 */
54 	{ "S905M", 0x1f, 0x20, 0xf0 }, /* pack_id == 0x20 */
55 	{ "S905D", 0x21, 0, 0xf0 },
56 	{ "S905X", 0x21, 0x80, 0xf0 },
57 	{ "S905W", 0x21, 0xa0, 0xf0 },
58 	{ "S905L", 0x21, 0xc0, 0xf0 },
59 	{ "S905M2", 0x21, 0xe0, 0xf0 },
60 	{ "S805X", 0x21, 0x30, 0xf0 },
61 	{ "S805Y", 0x21, 0xb0, 0xf0 },
62 	{ "S912", 0x22, 0, 0x0 }, /* Only S912 is known for GXM */
63 	{ "962X", 0x24, 0x10, 0xf0 },
64 	{ "962E", 0x24, 0x20, 0xf0 },
65 	{ "A113X", 0x25, 0x37, 0xff },
66 	{ "A113D", 0x25, 0x22, 0xff },
67 	{ "S905D2", 0x28, 0x10, 0xf0 },
68 	{ "S905X2", 0x28, 0x40, 0xf0 },
69 	{ "S922X", 0x29, 0x40, 0xf0 },
70 	{ "A311D", 0x29, 0x10, 0xf0 },
71 	{ "S905X3", 0x2b, 0x5, 0xf },
72 	{ "S905D3", 0x2b, 0xb0, 0xf0 },
73 	{ "A113L", 0x2c, 0x0, 0xf8 },
74 };
75 
76 static inline unsigned int socinfo_to_major(u32 socinfo)
77 {
78 	return FIELD_GET(SOCINFO_MAJOR, socinfo);
79 }
80 
81 static inline unsigned int socinfo_to_minor(u32 socinfo)
82 {
83 	return FIELD_GET(SOCINFO_MINOR, socinfo);
84 }
85 
86 static inline unsigned int socinfo_to_pack(u32 socinfo)
87 {
88 	return FIELD_GET(SOCINFO_PACK, socinfo);
89 }
90 
91 static inline unsigned int socinfo_to_misc(u32 socinfo)
92 {
93 	return FIELD_GET(SOCINFO_MISC, socinfo);
94 }
95 
96 static const char *socinfo_to_package_id(u32 socinfo)
97 {
98 	unsigned int pack = socinfo_to_pack(socinfo);
99 	unsigned int major = socinfo_to_major(socinfo);
100 	int i;
101 
102 	for (i = 0 ; i < ARRAY_SIZE(soc_packages) ; ++i) {
103 		if (soc_packages[i].major_id == major &&
104 		    soc_packages[i].pack_id ==
105 				(pack & soc_packages[i].pack_mask))
106 			return soc_packages[i].name;
107 	}
108 
109 	return "Unknown";
110 }
111 
112 static const char *socinfo_to_soc_id(u32 socinfo)
113 {
114 	unsigned int id = socinfo_to_major(socinfo);
115 	int i;
116 
117 	for (i = 0 ; i < ARRAY_SIZE(soc_ids) ; ++i) {
118 		if (soc_ids[i].id == id)
119 			return soc_ids[i].name;
120 	}
121 
122 	return "Unknown";
123 }
124 
125 static int __init meson_gx_socinfo_init(void)
126 {
127 	struct soc_device_attribute *soc_dev_attr;
128 	struct soc_device *soc_dev;
129 	struct device_node *np;
130 	struct regmap *regmap;
131 	unsigned int socinfo;
132 	struct device *dev;
133 	int ret;
134 
135 	/* look up for chipid node */
136 	np = of_find_compatible_node(NULL, NULL, "amlogic,meson-gx-ao-secure");
137 	if (!np)
138 		return -ENODEV;
139 
140 	/* check if interface is enabled */
141 	if (!of_device_is_available(np)) {
142 		of_node_put(np);
143 		return -ENODEV;
144 	}
145 
146 	/* check if chip-id is available */
147 	if (!of_property_read_bool(np, "amlogic,has-chip-id")) {
148 		of_node_put(np);
149 		return -ENODEV;
150 	}
151 
152 	/* node should be a syscon */
153 	regmap = syscon_node_to_regmap(np);
154 	of_node_put(np);
155 	if (IS_ERR(regmap)) {
156 		pr_err("%s: failed to get regmap\n", __func__);
157 		return -ENODEV;
158 	}
159 
160 	ret = regmap_read(regmap, AO_SEC_SOCINFO_OFFSET, &socinfo);
161 	if (ret < 0)
162 		return ret;
163 
164 	if (!socinfo) {
165 		pr_err("%s: invalid chipid value\n", __func__);
166 		return -EINVAL;
167 	}
168 
169 	soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
170 	if (!soc_dev_attr)
171 		return -ENODEV;
172 
173 	soc_dev_attr->family = "Amlogic Meson";
174 
175 	np = of_find_node_by_path("/");
176 	of_property_read_string(np, "model", &soc_dev_attr->machine);
177 	of_node_put(np);
178 
179 	soc_dev_attr->revision = kasprintf(GFP_KERNEL, "%x:%x - %x:%x",
180 					   socinfo_to_major(socinfo),
181 					   socinfo_to_minor(socinfo),
182 					   socinfo_to_pack(socinfo),
183 					   socinfo_to_misc(socinfo));
184 	soc_dev_attr->soc_id = kasprintf(GFP_KERNEL, "%s (%s)",
185 					 socinfo_to_soc_id(socinfo),
186 					 socinfo_to_package_id(socinfo));
187 
188 	soc_dev = soc_device_register(soc_dev_attr);
189 	if (IS_ERR(soc_dev)) {
190 		kfree(soc_dev_attr->revision);
191 		kfree_const(soc_dev_attr->soc_id);
192 		kfree(soc_dev_attr);
193 		return PTR_ERR(soc_dev);
194 	}
195 	dev = soc_device_to_device(soc_dev);
196 
197 	dev_info(dev, "Amlogic Meson %s Revision %x:%x (%x:%x) Detected\n",
198 			soc_dev_attr->soc_id,
199 			socinfo_to_major(socinfo),
200 			socinfo_to_minor(socinfo),
201 			socinfo_to_pack(socinfo),
202 			socinfo_to_misc(socinfo));
203 
204 	return 0;
205 }
206 device_initcall(meson_gx_socinfo_init);
207