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 int (*soc_revision
)(u32
*socrev
, u64
*socuid
);
36 #ifdef CONFIG_HAVE_ARM_SMCCC
37 static u32
imx8mq_soc_revision_from_atf(void)
39 struct arm_smccc_res res
;
41 arm_smccc_smc(IMX_SIP_GET_SOC_INFO
, 0, 0, 0, 0, 0, 0, 0, &res
);
43 if (res
.a0
== SMCCC_RET_NOT_SUPPORTED
)
49 static inline u32
imx8mq_soc_revision_from_atf(void) { return 0; };
52 static int imx8mq_soc_revision(u32
*socrev
, u64
*socuid
)
54 struct device_node
*np
__free(device_node
) =
55 of_find_compatible_node(NULL
, NULL
, "fsl,imx8mq-ocotp");
56 void __iomem
*ocotp_base
;
65 ocotp_base
= of_iomap(np
, 0);
69 clk
= of_clk_get_by_name(np
, NULL
);
75 clk_prepare_enable(clk
);
78 * SOC revision on older imx8mq is not available in fuses so query
79 * the value from ATF instead.
81 rev
= imx8mq_soc_revision_from_atf();
83 magic
= readl_relaxed(ocotp_base
+ IMX8MQ_SW_INFO_B1
);
84 if (magic
== IMX8MQ_SW_MAGIC_B1
)
88 *socuid
= readl_relaxed(ocotp_base
+ OCOTP_UID_HIGH
);
90 *socuid
|= readl_relaxed(ocotp_base
+ OCOTP_UID_LOW
);
94 clk_disable_unprepare(clk
);
105 static int imx8mm_soc_uid(u64
*socuid
)
107 struct device_node
*np
__free(device_node
) =
108 of_find_compatible_node(NULL
, NULL
, "fsl,imx8mm-ocotp");
109 void __iomem
*ocotp_base
;
112 u32 offset
= of_machine_is_compatible("fsl,imx8mp") ?
113 IMX8MP_OCOTP_UID_OFFSET
: 0;
118 ocotp_base
= of_iomap(np
, 0);
122 clk
= of_clk_get_by_name(np
, NULL
);
128 clk_prepare_enable(clk
);
130 *socuid
= readl_relaxed(ocotp_base
+ OCOTP_UID_HIGH
+ offset
);
132 *socuid
|= readl_relaxed(ocotp_base
+ OCOTP_UID_LOW
+ offset
);
134 clk_disable_unprepare(clk
);
142 static int imx8mm_soc_revision(u32
*socrev
, u64
*socuid
)
144 struct device_node
*np
__free(device_node
) =
145 of_find_compatible_node(NULL
, NULL
, "fsl,imx8mm-anatop");
146 void __iomem
*anatop_base
;
151 anatop_base
= of_iomap(np
, 0);
155 *socrev
= readl_relaxed(anatop_base
+ ANADIG_DIGPROG_IMX8MM
);
157 iounmap(anatop_base
);
159 return imx8mm_soc_uid(socuid
);
162 static const struct imx8_soc_data imx8mq_soc_data
= {
164 .soc_revision
= imx8mq_soc_revision
,
167 static const struct imx8_soc_data imx8mm_soc_data
= {
169 .soc_revision
= imx8mm_soc_revision
,
172 static const struct imx8_soc_data imx8mn_soc_data
= {
174 .soc_revision
= imx8mm_soc_revision
,
177 static const struct imx8_soc_data imx8mp_soc_data
= {
179 .soc_revision
= imx8mm_soc_revision
,
182 static __maybe_unused
const struct of_device_id imx8_soc_match
[] = {
183 { .compatible
= "fsl,imx8mq", .data
= &imx8mq_soc_data
, },
184 { .compatible
= "fsl,imx8mm", .data
= &imx8mm_soc_data
, },
185 { .compatible
= "fsl,imx8mn", .data
= &imx8mn_soc_data
, },
186 { .compatible
= "fsl,imx8mp", .data
= &imx8mp_soc_data
, },
190 #define imx8_revision(dev, soc_rev) \
192 devm_kasprintf((dev), GFP_KERNEL, "%d.%d", ((soc_rev) >> 4) & 0xf, (soc_rev) & 0xf) : \
195 static int imx8m_soc_probe(struct platform_device
*pdev
)
197 struct soc_device_attribute
*soc_dev_attr
;
198 const struct imx8_soc_data
*data
;
199 struct device
*dev
= &pdev
->dev
;
200 const struct of_device_id
*id
;
201 struct soc_device
*soc_dev
;
206 soc_dev_attr
= devm_kzalloc(dev
, sizeof(*soc_dev_attr
), GFP_KERNEL
);
210 soc_dev_attr
->family
= "Freescale i.MX";
212 ret
= of_property_read_string(of_root
, "model", &soc_dev_attr
->machine
);
216 id
= of_match_node(imx8_soc_match
, of_root
);
222 soc_dev_attr
->soc_id
= data
->name
;
223 if (data
->soc_revision
) {
224 ret
= data
->soc_revision(&soc_rev
, &soc_uid
);
230 soc_dev_attr
->revision
= imx8_revision(dev
, soc_rev
);
231 if (!soc_dev_attr
->revision
)
234 soc_dev_attr
->serial_number
= devm_kasprintf(dev
, GFP_KERNEL
, "%016llX", soc_uid
);
235 if (!soc_dev_attr
->serial_number
)
238 soc_dev
= soc_device_register(soc_dev_attr
);
240 return PTR_ERR(soc_dev
);
242 pr_info("SoC: %s revision %s\n", soc_dev_attr
->soc_id
,
243 soc_dev_attr
->revision
);
245 if (IS_ENABLED(CONFIG_ARM_IMX_CPUFREQ_DT
))
246 platform_device_register_simple("imx-cpufreq-dt", -1, NULL
, 0);
251 static struct platform_driver imx8m_soc_driver
= {
252 .probe
= imx8m_soc_probe
,
258 static int __init
imx8_soc_init(void)
260 struct platform_device
*pdev
;
263 /* No match means this is non-i.MX8M hardware, do nothing. */
264 if (!of_match_node(imx8_soc_match
, of_root
))
267 ret
= platform_driver_register(&imx8m_soc_driver
);
269 pr_err("Failed to register imx8m-soc platform driver: %d\n", ret
);
273 pdev
= platform_device_register_simple("imx8m-soc", -1, NULL
, 0);
275 pr_err("Failed to register imx8m-soc platform device: %ld\n", PTR_ERR(pdev
));
276 platform_driver_unregister(&imx8m_soc_driver
);
277 return PTR_ERR(pdev
);
282 device_initcall(imx8_soc_init
);
283 MODULE_DESCRIPTION("NXP i.MX8M SoC driver");
284 MODULE_LICENSE("GPL");