xref: /linux/drivers/soc/amlogic/meson-gx-socinfo.c (revision c073f31a969accd4d07345fe72e7a9e7b582f93b)
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 };
37 
38 static const struct meson_gx_package_id {
39 	const char *name;
40 	unsigned int major_id;
41 	unsigned int pack_id;
42 } soc_packages[] = {
43 	{ "S905", 0x1f, 0 },
44 	{ "S905H", 0x1f, 0x13 },
45 	{ "S905M", 0x1f, 0x20 },
46 	{ "S905D", 0x21, 0 },
47 	{ "S905X", 0x21, 0x80 },
48 	{ "S905L", 0x21, 0xc0 },
49 	{ "S905M2", 0x21, 0xe0 },
50 	{ "S912", 0x22, 0 },
51 };
52 
53 static inline unsigned int socinfo_to_major(u32 socinfo)
54 {
55 	return FIELD_GET(SOCINFO_MAJOR, socinfo);
56 }
57 
58 static inline unsigned int socinfo_to_minor(u32 socinfo)
59 {
60 	return FIELD_GET(SOCINFO_MINOR, socinfo);
61 }
62 
63 static inline unsigned int socinfo_to_pack(u32 socinfo)
64 {
65 	return FIELD_GET(SOCINFO_PACK, socinfo);
66 }
67 
68 static inline unsigned int socinfo_to_misc(u32 socinfo)
69 {
70 	return FIELD_GET(SOCINFO_MISC, socinfo);
71 }
72 
73 static const char *socinfo_to_package_id(u32 socinfo)
74 {
75 	unsigned int pack = socinfo_to_pack(socinfo) & 0xf0;
76 	unsigned int major = socinfo_to_major(socinfo);
77 	int i;
78 
79 	for (i = 0 ; i < ARRAY_SIZE(soc_packages) ; ++i) {
80 		if (soc_packages[i].major_id == major &&
81 		    soc_packages[i].pack_id == pack)
82 			return soc_packages[i].name;
83 	}
84 
85 	return "Unknown";
86 }
87 
88 static const char *socinfo_to_soc_id(u32 socinfo)
89 {
90 	unsigned int id = socinfo_to_major(socinfo);
91 	int i;
92 
93 	for (i = 0 ; i < ARRAY_SIZE(soc_ids) ; ++i) {
94 		if (soc_ids[i].id == id)
95 			return soc_ids[i].name;
96 	}
97 
98 	return "Unknown";
99 }
100 
101 int __init meson_gx_socinfo_init(void)
102 {
103 	struct soc_device_attribute *soc_dev_attr;
104 	struct soc_device *soc_dev;
105 	struct device_node *np;
106 	struct regmap *regmap;
107 	unsigned int socinfo;
108 	struct device *dev;
109 	int ret;
110 
111 	/* look up for chipid node */
112 	np = of_find_compatible_node(NULL, NULL, "amlogic,meson-gx-ao-secure");
113 	if (!np)
114 		return -ENODEV;
115 
116 	/* check if interface is enabled */
117 	if (!of_device_is_available(np))
118 		return -ENODEV;
119 
120 	/* check if chip-id is available */
121 	if (!of_property_read_bool(np, "amlogic,has-chip-id"))
122 		return -ENODEV;
123 
124 	/* node should be a syscon */
125 	regmap = syscon_node_to_regmap(np);
126 	of_node_put(np);
127 	if (IS_ERR(regmap)) {
128 		pr_err("%s: failed to get regmap\n", __func__);
129 		return -ENODEV;
130 	}
131 
132 	ret = regmap_read(regmap, AO_SEC_SOCINFO_OFFSET, &socinfo);
133 	if (ret < 0)
134 		return ret;
135 
136 	if (!socinfo) {
137 		pr_err("%s: invalid chipid value\n", __func__);
138 		return -EINVAL;
139 	}
140 
141 	soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
142 	if (!soc_dev_attr)
143 		return -ENODEV;
144 
145 	soc_dev_attr->family = "Amlogic Meson";
146 
147 	np = of_find_node_by_path("/");
148 	of_property_read_string(np, "model", &soc_dev_attr->machine);
149 	of_node_put(np);
150 
151 	soc_dev_attr->revision = kasprintf(GFP_KERNEL, "%x:%x - %x:%x",
152 					   socinfo_to_major(socinfo),
153 					   socinfo_to_minor(socinfo),
154 					   socinfo_to_pack(socinfo),
155 					   socinfo_to_misc(socinfo));
156 	soc_dev_attr->soc_id = kasprintf(GFP_KERNEL, "%s (%s)",
157 					 socinfo_to_soc_id(socinfo),
158 					 socinfo_to_package_id(socinfo));
159 
160 	soc_dev = soc_device_register(soc_dev_attr);
161 	if (IS_ERR(soc_dev)) {
162 		kfree(soc_dev_attr->revision);
163 		kfree_const(soc_dev_attr->soc_id);
164 		kfree(soc_dev_attr);
165 		return PTR_ERR(soc_dev);
166 	}
167 	dev = soc_device_to_device(soc_dev);
168 
169 	dev_info(dev, "Amlogic Meson %s Revision %x:%x (%x:%x) Detected\n",
170 			soc_dev_attr->soc_id,
171 			socinfo_to_major(socinfo),
172 			socinfo_to_minor(socinfo),
173 			socinfo_to_pack(socinfo),
174 			socinfo_to_misc(socinfo));
175 
176 	return 0;
177 }
178 device_initcall(meson_gx_socinfo_init);
179