xref: /openbmc/linux/drivers/soc/imx/soc-imx.c (revision aa74c44b)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright 2020 NXP
4  */
5 
6 #include <linux/mfd/syscon.h>
7 #include <linux/of.h>
8 #include <linux/of_address.h>
9 #include <linux/regmap.h>
10 #include <linux/slab.h>
11 #include <linux/sys_soc.h>
12 
13 #include <soc/imx/cpu.h>
14 #include <soc/imx/revision.h>
15 
16 #define IIM_UID		0x820
17 
18 #define OCOTP_UID_H	0x420
19 #define OCOTP_UID_L	0x410
20 
21 #define OCOTP_ULP_UID_1		0x4b0
22 #define OCOTP_ULP_UID_2		0x4c0
23 #define OCOTP_ULP_UID_3		0x4d0
24 #define OCOTP_ULP_UID_4		0x4e0
25 
26 static int __init imx_soc_device_init(void)
27 {
28 	struct soc_device_attribute *soc_dev_attr;
29 	const char *ocotp_compat = NULL;
30 	struct soc_device *soc_dev;
31 	struct device_node *root;
32 	struct regmap *ocotp = NULL;
33 	const char *soc_id;
34 	u64 soc_uid = 0;
35 	u32 val;
36 	int ret;
37 	int i;
38 
39 	/* Return early if this is running on devices with different SoCs */
40 	if (!__mxc_cpu_type)
41 		return 0;
42 
43 	if (of_machine_is_compatible("fsl,ls1021a"))
44 		return 0;
45 
46 	soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
47 	if (!soc_dev_attr)
48 		return -ENOMEM;
49 
50 	soc_dev_attr->family = "Freescale i.MX";
51 
52 	root = of_find_node_by_path("/");
53 	ret = of_property_read_string(root, "model", &soc_dev_attr->machine);
54 	of_node_put(root);
55 	if (ret)
56 		goto free_soc;
57 
58 	switch (__mxc_cpu_type) {
59 	case MXC_CPU_MX1:
60 		soc_id = "i.MX1";
61 		break;
62 	case MXC_CPU_MX21:
63 		soc_id = "i.MX21";
64 		break;
65 	case MXC_CPU_MX25:
66 		soc_id = "i.MX25";
67 		break;
68 	case MXC_CPU_MX27:
69 		soc_id = "i.MX27";
70 		break;
71 	case MXC_CPU_MX31:
72 		soc_id = "i.MX31";
73 		break;
74 	case MXC_CPU_MX35:
75 		soc_id = "i.MX35";
76 		break;
77 	case MXC_CPU_MX50:
78 		soc_id = "i.MX50";
79 		break;
80 	case MXC_CPU_MX51:
81 		ocotp_compat = "fsl,imx51-iim";
82 		soc_id = "i.MX51";
83 		break;
84 	case MXC_CPU_MX53:
85 		ocotp_compat = "fsl,imx53-iim";
86 		soc_id = "i.MX53";
87 		break;
88 	case MXC_CPU_IMX6SL:
89 		ocotp_compat = "fsl,imx6sl-ocotp";
90 		soc_id = "i.MX6SL";
91 		break;
92 	case MXC_CPU_IMX6DL:
93 		ocotp_compat = "fsl,imx6q-ocotp";
94 		soc_id = "i.MX6DL";
95 		break;
96 	case MXC_CPU_IMX6SX:
97 		ocotp_compat = "fsl,imx6sx-ocotp";
98 		soc_id = "i.MX6SX";
99 		break;
100 	case MXC_CPU_IMX6Q:
101 		ocotp_compat = "fsl,imx6q-ocotp";
102 		soc_id = "i.MX6Q";
103 		break;
104 	case MXC_CPU_IMX6UL:
105 		ocotp_compat = "fsl,imx6ul-ocotp";
106 		soc_id = "i.MX6UL";
107 		break;
108 	case MXC_CPU_IMX6ULL:
109 		ocotp_compat = "fsl,imx6ull-ocotp";
110 		soc_id = "i.MX6ULL";
111 		break;
112 	case MXC_CPU_IMX6ULZ:
113 		ocotp_compat = "fsl,imx6ull-ocotp";
114 		soc_id = "i.MX6ULZ";
115 		break;
116 	case MXC_CPU_IMX6SLL:
117 		ocotp_compat = "fsl,imx6sll-ocotp";
118 		soc_id = "i.MX6SLL";
119 		break;
120 	case MXC_CPU_IMX7D:
121 		ocotp_compat = "fsl,imx7d-ocotp";
122 		soc_id = "i.MX7D";
123 		break;
124 	case MXC_CPU_IMX7ULP:
125 		ocotp_compat = "fsl,imx7ulp-ocotp";
126 		soc_id = "i.MX7ULP";
127 		break;
128 	case MXC_CPU_VF500:
129 		ocotp_compat = "fsl,vf610-ocotp";
130 		soc_id = "VF500";
131 		break;
132 	case MXC_CPU_VF510:
133 		ocotp_compat = "fsl,vf610-ocotp";
134 		soc_id = "VF510";
135 		break;
136 	case MXC_CPU_VF600:
137 		ocotp_compat = "fsl,vf610-ocotp";
138 		soc_id = "VF600";
139 		break;
140 	case MXC_CPU_VF610:
141 		ocotp_compat = "fsl,vf610-ocotp";
142 		soc_id = "VF610";
143 		break;
144 	default:
145 		soc_id = "Unknown";
146 	}
147 	soc_dev_attr->soc_id = soc_id;
148 
149 	if (ocotp_compat) {
150 		ocotp = syscon_regmap_lookup_by_compatible(ocotp_compat);
151 		if (IS_ERR(ocotp))
152 			pr_err("%s: failed to find %s regmap!\n", __func__, ocotp_compat);
153 	}
154 
155 	if (!IS_ERR_OR_NULL(ocotp)) {
156 		if (__mxc_cpu_type == MXC_CPU_IMX7ULP) {
157 			regmap_read(ocotp, OCOTP_ULP_UID_4, &val);
158 			soc_uid = val & 0xffff;
159 			regmap_read(ocotp, OCOTP_ULP_UID_3, &val);
160 			soc_uid <<= 16;
161 			soc_uid |= val & 0xffff;
162 			regmap_read(ocotp, OCOTP_ULP_UID_2, &val);
163 			soc_uid <<= 16;
164 			soc_uid |= val & 0xffff;
165 			regmap_read(ocotp, OCOTP_ULP_UID_1, &val);
166 			soc_uid <<= 16;
167 			soc_uid |= val & 0xffff;
168 		} else if (__mxc_cpu_type == MXC_CPU_MX51 ||
169 			   __mxc_cpu_type == MXC_CPU_MX53) {
170 			for (i=0; i < 8; i++) {
171 				regmap_read(ocotp, IIM_UID + i*4, &val);
172 				soc_uid <<= 8;
173 				soc_uid |= (val & 0xff);
174 			}
175 		} else {
176 			regmap_read(ocotp, OCOTP_UID_H, &val);
177 			soc_uid = val;
178 			regmap_read(ocotp, OCOTP_UID_L, &val);
179 			soc_uid <<= 32;
180 			soc_uid |= val;
181 		}
182 	}
183 
184 	soc_dev_attr->revision = kasprintf(GFP_KERNEL, "%d.%d",
185 					   (imx_get_soc_revision() >> 4) & 0xf,
186 					   imx_get_soc_revision() & 0xf);
187 	if (!soc_dev_attr->revision) {
188 		ret = -ENOMEM;
189 		goto free_soc;
190 	}
191 
192 	soc_dev_attr->serial_number = kasprintf(GFP_KERNEL, "%016llX", soc_uid);
193 	if (!soc_dev_attr->serial_number) {
194 		ret = -ENOMEM;
195 		goto free_rev;
196 	}
197 
198 	soc_dev = soc_device_register(soc_dev_attr);
199 	if (IS_ERR(soc_dev)) {
200 		ret = PTR_ERR(soc_dev);
201 		goto free_serial_number;
202 	}
203 
204 	return 0;
205 
206 free_serial_number:
207 	kfree(soc_dev_attr->serial_number);
208 free_rev:
209 	kfree(soc_dev_attr->revision);
210 free_soc:
211 	kfree(soc_dev_attr);
212 	return ret;
213 }
214 device_initcall(imx_soc_device_init);
215