1 // SPDX-License-Identifier: GPL-2.0
6 #include <linux/init.h>
8 #include <linux/of_address.h>
9 #include <linux/slab.h>
10 #include <linux/sys_soc.h>
11 #include <linux/platform_device.h>
12 #include <linux/arm-smccc.h>
14 #include <linux/clk.h>
18 #define IMX8MQ_SW_INFO_B1 0x40
19 #define IMX8MQ_SW_MAGIC_B1 0xff0055aa
21 #define IMX_SIP_GET_SOC_INFO 0xc2000006
23 #define OCOTP_UID_LOW 0x410
24 #define OCOTP_UID_HIGH 0x420
26 #define IMX8MP_OCOTP_UID_OFFSET 0x10
28 /* Same as ANADIG_DIGPROG_IMX7D */
29 #define ANADIG_DIGPROG_IMX8MM 0x800
31 struct imx8_soc_data
{
33 u32 (*soc_revision
)(void);
38 #ifdef CONFIG_HAVE_ARM_SMCCC
39 static u32
imx8mq_soc_revision_from_atf(void)
41 struct arm_smccc_res res
;
43 arm_smccc_smc(IMX_SIP_GET_SOC_INFO
, 0, 0, 0, 0, 0, 0, 0, &res
);
45 if (res
.a0
== SMCCC_RET_NOT_SUPPORTED
)
51 static inline u32
imx8mq_soc_revision_from_atf(void) { return 0; };
54 static u32 __init
imx8mq_soc_revision(void)
56 struct device_node
*np
;
57 void __iomem
*ocotp_base
;
62 np
= of_find_compatible_node(NULL
, NULL
, "fsl,imx8mq-ocotp");
66 ocotp_base
= of_iomap(np
, 0);
68 clk
= of_clk_get_by_name(np
, NULL
);
74 clk_prepare_enable(clk
);
77 * SOC revision on older imx8mq is not available in fuses so query
78 * the value from ATF instead.
80 rev
= imx8mq_soc_revision_from_atf();
82 magic
= readl_relaxed(ocotp_base
+ IMX8MQ_SW_INFO_B1
);
83 if (magic
== IMX8MQ_SW_MAGIC_B1
)
87 soc_uid
= readl_relaxed(ocotp_base
+ OCOTP_UID_HIGH
);
89 soc_uid
|= readl_relaxed(ocotp_base
+ OCOTP_UID_LOW
);
91 clk_disable_unprepare(clk
);
99 static void __init
imx8mm_soc_uid(void)
101 void __iomem
*ocotp_base
;
102 struct device_node
*np
;
104 u32 offset
= of_machine_is_compatible("fsl,imx8mp") ?
105 IMX8MP_OCOTP_UID_OFFSET
: 0;
107 np
= of_find_compatible_node(NULL
, NULL
, "fsl,imx8mm-ocotp");
111 ocotp_base
= of_iomap(np
, 0);
112 WARN_ON(!ocotp_base
);
113 clk
= of_clk_get_by_name(np
, NULL
);
115 WARN_ON(IS_ERR(clk
));
119 clk_prepare_enable(clk
);
121 soc_uid
= readl_relaxed(ocotp_base
+ OCOTP_UID_HIGH
+ offset
);
123 soc_uid
|= readl_relaxed(ocotp_base
+ OCOTP_UID_LOW
+ offset
);
125 clk_disable_unprepare(clk
);
131 static u32 __init
imx8mm_soc_revision(void)
133 struct device_node
*np
;
134 void __iomem
*anatop_base
;
137 np
= of_find_compatible_node(NULL
, NULL
, "fsl,imx8mm-anatop");
141 anatop_base
= of_iomap(np
, 0);
142 WARN_ON(!anatop_base
);
144 rev
= readl_relaxed(anatop_base
+ ANADIG_DIGPROG_IMX8MM
);
146 iounmap(anatop_base
);
154 static const struct imx8_soc_data imx8mq_soc_data
= {
156 .soc_revision
= imx8mq_soc_revision
,
159 static const struct imx8_soc_data imx8mm_soc_data
= {
161 .soc_revision
= imx8mm_soc_revision
,
164 static const struct imx8_soc_data imx8mn_soc_data
= {
166 .soc_revision
= imx8mm_soc_revision
,
169 static const struct imx8_soc_data imx8mp_soc_data
= {
171 .soc_revision
= imx8mm_soc_revision
,
174 static __maybe_unused
const struct of_device_id imx8_soc_match
[] = {
175 { .compatible
= "fsl,imx8mq", .data
= &imx8mq_soc_data
, },
176 { .compatible
= "fsl,imx8mm", .data
= &imx8mm_soc_data
, },
177 { .compatible
= "fsl,imx8mn", .data
= &imx8mn_soc_data
, },
178 { .compatible
= "fsl,imx8mp", .data
= &imx8mp_soc_data
, },
182 #define imx8_revision(soc_rev) \
184 kasprintf(GFP_KERNEL, "%d.%d", (soc_rev >> 4) & 0xf, soc_rev & 0xf) : \
187 static int __init
imx8_soc_init(void)
189 struct soc_device_attribute
*soc_dev_attr
;
190 struct soc_device
*soc_dev
;
191 const struct of_device_id
*id
;
193 const struct imx8_soc_data
*data
;
196 soc_dev_attr
= kzalloc(sizeof(*soc_dev_attr
), GFP_KERNEL
);
200 soc_dev_attr
->family
= "Freescale i.MX";
202 ret
= of_property_read_string(of_root
, "model", &soc_dev_attr
->machine
);
206 id
= of_match_node(imx8_soc_match
, of_root
);
214 soc_dev_attr
->soc_id
= data
->name
;
215 if (data
->soc_revision
)
216 soc_rev
= data
->soc_revision();
219 soc_dev_attr
->revision
= imx8_revision(soc_rev
);
220 if (!soc_dev_attr
->revision
) {
225 soc_dev_attr
->serial_number
= kasprintf(GFP_KERNEL
, "%016llX", soc_uid
);
226 if (!soc_dev_attr
->serial_number
) {
231 soc_dev
= soc_device_register(soc_dev_attr
);
232 if (IS_ERR(soc_dev
)) {
233 ret
= PTR_ERR(soc_dev
);
234 goto free_serial_number
;
237 pr_info("SoC: %s revision %s\n", soc_dev_attr
->soc_id
,
238 soc_dev_attr
->revision
);
240 if (IS_ENABLED(CONFIG_ARM_IMX_CPUFREQ_DT
))
241 platform_device_register_simple("imx-cpufreq-dt", -1, NULL
, 0);
246 kfree(soc_dev_attr
->serial_number
);
248 if (strcmp(soc_dev_attr
->revision
, "unknown"))
249 kfree(soc_dev_attr
->revision
);
254 device_initcall(imx8_soc_init
);
255 MODULE_LICENSE("GPL");