152102a3bSPeng Fan // SPDX-License-Identifier: GPL-2.0
252102a3bSPeng Fan /*
352102a3bSPeng Fan * Copyright 2020 NXP
452102a3bSPeng Fan */
552102a3bSPeng Fan
652102a3bSPeng Fan #include <linux/mfd/syscon.h>
752102a3bSPeng Fan #include <linux/of.h>
852102a3bSPeng Fan #include <linux/of_address.h>
952102a3bSPeng Fan #include <linux/regmap.h>
1052102a3bSPeng Fan #include <linux/slab.h>
1152102a3bSPeng Fan #include <linux/sys_soc.h>
1252102a3bSPeng Fan
1352102a3bSPeng Fan #include <soc/imx/cpu.h>
1452102a3bSPeng Fan #include <soc/imx/revision.h>
1552102a3bSPeng Fan
161168935bSSebastian Reichel #define IIM_UID 0x820
171168935bSSebastian Reichel
1852102a3bSPeng Fan #define OCOTP_UID_H 0x420
1952102a3bSPeng Fan #define OCOTP_UID_L 0x410
2052102a3bSPeng Fan
2152102a3bSPeng Fan #define OCOTP_ULP_UID_1 0x4b0
2252102a3bSPeng Fan #define OCOTP_ULP_UID_2 0x4c0
2352102a3bSPeng Fan #define OCOTP_ULP_UID_3 0x4d0
2452102a3bSPeng Fan #define OCOTP_ULP_UID_4 0x4e0
2552102a3bSPeng Fan
imx_soc_device_init(void)2652102a3bSPeng Fan static int __init imx_soc_device_init(void)
2752102a3bSPeng Fan {
2852102a3bSPeng Fan struct soc_device_attribute *soc_dev_attr;
2952102a3bSPeng Fan const char *ocotp_compat = NULL;
3052102a3bSPeng Fan struct soc_device *soc_dev;
3152102a3bSPeng Fan struct device_node *root;
3252102a3bSPeng Fan struct regmap *ocotp = NULL;
3352102a3bSPeng Fan const char *soc_id;
3452102a3bSPeng Fan u64 soc_uid = 0;
3552102a3bSPeng Fan u32 val;
3652102a3bSPeng Fan int ret;
371168935bSSebastian Reichel int i;
3852102a3bSPeng Fan
39*4ebd29f9SStephan Gerhold /* Return early if this is running on devices with different SoCs */
40*4ebd29f9SStephan Gerhold if (!__mxc_cpu_type)
41*4ebd29f9SStephan Gerhold return 0;
42*4ebd29f9SStephan Gerhold
4352102a3bSPeng Fan soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
4452102a3bSPeng Fan if (!soc_dev_attr)
4552102a3bSPeng Fan return -ENOMEM;
4652102a3bSPeng Fan
4752102a3bSPeng Fan soc_dev_attr->family = "Freescale i.MX";
4852102a3bSPeng Fan
4952102a3bSPeng Fan root = of_find_node_by_path("/");
5052102a3bSPeng Fan ret = of_property_read_string(root, "model", &soc_dev_attr->machine);
5152102a3bSPeng Fan of_node_put(root);
5252102a3bSPeng Fan if (ret)
5352102a3bSPeng Fan goto free_soc;
5452102a3bSPeng Fan
5552102a3bSPeng Fan switch (__mxc_cpu_type) {
5652102a3bSPeng Fan case MXC_CPU_MX1:
5752102a3bSPeng Fan soc_id = "i.MX1";
5852102a3bSPeng Fan break;
5952102a3bSPeng Fan case MXC_CPU_MX21:
6052102a3bSPeng Fan soc_id = "i.MX21";
6152102a3bSPeng Fan break;
6252102a3bSPeng Fan case MXC_CPU_MX25:
6352102a3bSPeng Fan soc_id = "i.MX25";
6452102a3bSPeng Fan break;
6552102a3bSPeng Fan case MXC_CPU_MX27:
6652102a3bSPeng Fan soc_id = "i.MX27";
6752102a3bSPeng Fan break;
6852102a3bSPeng Fan case MXC_CPU_MX31:
6952102a3bSPeng Fan soc_id = "i.MX31";
7052102a3bSPeng Fan break;
7152102a3bSPeng Fan case MXC_CPU_MX35:
7252102a3bSPeng Fan soc_id = "i.MX35";
7352102a3bSPeng Fan break;
7422b5059bSJonathan Neuschäfer case MXC_CPU_MX50:
7522b5059bSJonathan Neuschäfer soc_id = "i.MX50";
7622b5059bSJonathan Neuschäfer break;
7752102a3bSPeng Fan case MXC_CPU_MX51:
781168935bSSebastian Reichel ocotp_compat = "fsl,imx51-iim";
7952102a3bSPeng Fan soc_id = "i.MX51";
8052102a3bSPeng Fan break;
8152102a3bSPeng Fan case MXC_CPU_MX53:
821168935bSSebastian Reichel ocotp_compat = "fsl,imx53-iim";
8352102a3bSPeng Fan soc_id = "i.MX53";
8452102a3bSPeng Fan break;
8552102a3bSPeng Fan case MXC_CPU_IMX6SL:
8652102a3bSPeng Fan ocotp_compat = "fsl,imx6sl-ocotp";
8752102a3bSPeng Fan soc_id = "i.MX6SL";
8852102a3bSPeng Fan break;
8952102a3bSPeng Fan case MXC_CPU_IMX6DL:
9052102a3bSPeng Fan ocotp_compat = "fsl,imx6q-ocotp";
9152102a3bSPeng Fan soc_id = "i.MX6DL";
9252102a3bSPeng Fan break;
9352102a3bSPeng Fan case MXC_CPU_IMX6SX:
9452102a3bSPeng Fan ocotp_compat = "fsl,imx6sx-ocotp";
9552102a3bSPeng Fan soc_id = "i.MX6SX";
9652102a3bSPeng Fan break;
9752102a3bSPeng Fan case MXC_CPU_IMX6Q:
9852102a3bSPeng Fan ocotp_compat = "fsl,imx6q-ocotp";
9952102a3bSPeng Fan soc_id = "i.MX6Q";
10052102a3bSPeng Fan break;
10152102a3bSPeng Fan case MXC_CPU_IMX6UL:
10252102a3bSPeng Fan ocotp_compat = "fsl,imx6ul-ocotp";
10352102a3bSPeng Fan soc_id = "i.MX6UL";
10452102a3bSPeng Fan break;
10552102a3bSPeng Fan case MXC_CPU_IMX6ULL:
10652102a3bSPeng Fan ocotp_compat = "fsl,imx6ull-ocotp";
10752102a3bSPeng Fan soc_id = "i.MX6ULL";
10852102a3bSPeng Fan break;
10952102a3bSPeng Fan case MXC_CPU_IMX6ULZ:
11052102a3bSPeng Fan ocotp_compat = "fsl,imx6ull-ocotp";
11152102a3bSPeng Fan soc_id = "i.MX6ULZ";
11252102a3bSPeng Fan break;
11352102a3bSPeng Fan case MXC_CPU_IMX6SLL:
11452102a3bSPeng Fan ocotp_compat = "fsl,imx6sll-ocotp";
11552102a3bSPeng Fan soc_id = "i.MX6SLL";
11652102a3bSPeng Fan break;
11752102a3bSPeng Fan case MXC_CPU_IMX7D:
11852102a3bSPeng Fan ocotp_compat = "fsl,imx7d-ocotp";
11952102a3bSPeng Fan soc_id = "i.MX7D";
12052102a3bSPeng Fan break;
12152102a3bSPeng Fan case MXC_CPU_IMX7ULP:
12252102a3bSPeng Fan ocotp_compat = "fsl,imx7ulp-ocotp";
12352102a3bSPeng Fan soc_id = "i.MX7ULP";
12452102a3bSPeng Fan break;
12552102a3bSPeng Fan case MXC_CPU_VF500:
12652102a3bSPeng Fan ocotp_compat = "fsl,vf610-ocotp";
12752102a3bSPeng Fan soc_id = "VF500";
12852102a3bSPeng Fan break;
12952102a3bSPeng Fan case MXC_CPU_VF510:
13052102a3bSPeng Fan ocotp_compat = "fsl,vf610-ocotp";
13152102a3bSPeng Fan soc_id = "VF510";
13252102a3bSPeng Fan break;
13352102a3bSPeng Fan case MXC_CPU_VF600:
13452102a3bSPeng Fan ocotp_compat = "fsl,vf610-ocotp";
13552102a3bSPeng Fan soc_id = "VF600";
13652102a3bSPeng Fan break;
13752102a3bSPeng Fan case MXC_CPU_VF610:
13852102a3bSPeng Fan ocotp_compat = "fsl,vf610-ocotp";
13952102a3bSPeng Fan soc_id = "VF610";
14052102a3bSPeng Fan break;
14152102a3bSPeng Fan default:
14252102a3bSPeng Fan soc_id = "Unknown";
14352102a3bSPeng Fan }
14452102a3bSPeng Fan soc_dev_attr->soc_id = soc_id;
14552102a3bSPeng Fan
14652102a3bSPeng Fan if (ocotp_compat) {
14752102a3bSPeng Fan ocotp = syscon_regmap_lookup_by_compatible(ocotp_compat);
14852102a3bSPeng Fan if (IS_ERR(ocotp))
14952102a3bSPeng Fan pr_err("%s: failed to find %s regmap!\n", __func__, ocotp_compat);
15052102a3bSPeng Fan }
15152102a3bSPeng Fan
15252102a3bSPeng Fan if (!IS_ERR_OR_NULL(ocotp)) {
15352102a3bSPeng Fan if (__mxc_cpu_type == MXC_CPU_IMX7ULP) {
15452102a3bSPeng Fan regmap_read(ocotp, OCOTP_ULP_UID_4, &val);
15552102a3bSPeng Fan soc_uid = val & 0xffff;
15652102a3bSPeng Fan regmap_read(ocotp, OCOTP_ULP_UID_3, &val);
15752102a3bSPeng Fan soc_uid <<= 16;
15852102a3bSPeng Fan soc_uid |= val & 0xffff;
15952102a3bSPeng Fan regmap_read(ocotp, OCOTP_ULP_UID_2, &val);
16052102a3bSPeng Fan soc_uid <<= 16;
16152102a3bSPeng Fan soc_uid |= val & 0xffff;
16252102a3bSPeng Fan regmap_read(ocotp, OCOTP_ULP_UID_1, &val);
16352102a3bSPeng Fan soc_uid <<= 16;
16452102a3bSPeng Fan soc_uid |= val & 0xffff;
1651168935bSSebastian Reichel } else if (__mxc_cpu_type == MXC_CPU_MX51 ||
1661168935bSSebastian Reichel __mxc_cpu_type == MXC_CPU_MX53) {
1671168935bSSebastian Reichel for (i=0; i < 8; i++) {
1681168935bSSebastian Reichel regmap_read(ocotp, IIM_UID + i*4, &val);
1691168935bSSebastian Reichel soc_uid <<= 8;
1701168935bSSebastian Reichel soc_uid |= (val & 0xff);
1711168935bSSebastian Reichel }
17252102a3bSPeng Fan } else {
17352102a3bSPeng Fan regmap_read(ocotp, OCOTP_UID_H, &val);
17452102a3bSPeng Fan soc_uid = val;
17552102a3bSPeng Fan regmap_read(ocotp, OCOTP_UID_L, &val);
17652102a3bSPeng Fan soc_uid <<= 32;
17752102a3bSPeng Fan soc_uid |= val;
17852102a3bSPeng Fan }
17952102a3bSPeng Fan }
18052102a3bSPeng Fan
18152102a3bSPeng Fan soc_dev_attr->revision = kasprintf(GFP_KERNEL, "%d.%d",
18252102a3bSPeng Fan (imx_get_soc_revision() >> 4) & 0xf,
18352102a3bSPeng Fan imx_get_soc_revision() & 0xf);
18452102a3bSPeng Fan if (!soc_dev_attr->revision) {
18552102a3bSPeng Fan ret = -ENOMEM;
18652102a3bSPeng Fan goto free_soc;
18752102a3bSPeng Fan }
18852102a3bSPeng Fan
18952102a3bSPeng Fan soc_dev_attr->serial_number = kasprintf(GFP_KERNEL, "%016llX", soc_uid);
19052102a3bSPeng Fan if (!soc_dev_attr->serial_number) {
19152102a3bSPeng Fan ret = -ENOMEM;
19252102a3bSPeng Fan goto free_rev;
19352102a3bSPeng Fan }
19452102a3bSPeng Fan
19552102a3bSPeng Fan soc_dev = soc_device_register(soc_dev_attr);
19652102a3bSPeng Fan if (IS_ERR(soc_dev)) {
19752102a3bSPeng Fan ret = PTR_ERR(soc_dev);
19852102a3bSPeng Fan goto free_serial_number;
19952102a3bSPeng Fan }
20052102a3bSPeng Fan
20152102a3bSPeng Fan return 0;
20252102a3bSPeng Fan
20352102a3bSPeng Fan free_serial_number:
20452102a3bSPeng Fan kfree(soc_dev_attr->serial_number);
20552102a3bSPeng Fan free_rev:
20652102a3bSPeng Fan kfree(soc_dev_attr->revision);
20752102a3bSPeng Fan free_soc:
20852102a3bSPeng Fan kfree(soc_dev_attr);
20952102a3bSPeng Fan return ret;
21052102a3bSPeng Fan }
21152102a3bSPeng Fan device_initcall(imx_soc_device_init);
212