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