1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/mfd/syscon.h>
4 #include <linux/module.h>
7 #include <linux/of_address.h>
8 #include <linux/regmap.h>
9 #include <linux/slab.h>
10 #include <linux/sys_soc.h>
15 #define OCOTP_UID_H 0x420
16 #define OCOTP_UID_L 0x410
18 #define OCOTP_ULP_UID_1 0x4b0
19 #define OCOTP_ULP_UID_2 0x4c0
20 #define OCOTP_ULP_UID_3 0x4d0
21 #define OCOTP_ULP_UID_4 0x4e0
23 unsigned int __mxc_cpu_type;
24 static unsigned int imx_soc_revision;
26 void mxc_set_cpu_type(unsigned int type)
28 __mxc_cpu_type = type;
31 void imx_set_soc_revision(unsigned int rev)
33 imx_soc_revision = rev;
36 unsigned int imx_get_soc_revision(void)
38 return imx_soc_revision;
41 void imx_print_silicon_rev(const char *cpu, int srev)
43 if (srev == IMX_CHIP_REVISION_UNKNOWN)
44 pr_info("CPU identified as %s, unknown revision\n", cpu);
46 pr_info("CPU identified as %s, silicon rev %d.%d\n",
47 cpu, (srev >> 4) & 0xf, srev & 0xf);
50 void __init imx_set_aips(void __iomem *base)
54 * Set all MPROTx to be non-bufferable, trusted for R/W,
55 * not forced to user-mode.
57 imx_writel(0x77777777, base + 0x0);
58 imx_writel(0x77777777, base + 0x4);
61 * Set all OPACRx to be non-bufferable, to not require
62 * supervisor privilege level for access, allow for
63 * write access and untrusted master access.
65 imx_writel(0x0, base + 0x40);
66 imx_writel(0x0, base + 0x44);
67 imx_writel(0x0, base + 0x48);
68 imx_writel(0x0, base + 0x4C);
69 reg = imx_readl(base + 0x50) & 0x00FFFFFF;
70 imx_writel(reg, base + 0x50);
73 void __init imx_aips_allow_unprivileged_access(
76 void __iomem *aips_base_addr;
77 struct device_node *np;
79 for_each_compatible_node(np, NULL, compat) {
80 aips_base_addr = of_iomap(np, 0);
81 WARN_ON(!aips_base_addr);
82 imx_set_aips(aips_base_addr);
86 struct device * __init imx_soc_device_init(void)
88 struct soc_device_attribute *soc_dev_attr;
89 const char *ocotp_compat = NULL;
90 struct soc_device *soc_dev;
91 struct device_node *root;
92 struct regmap *ocotp = NULL;
98 soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
102 soc_dev_attr->family = "Freescale i.MX";
104 root = of_find_node_by_path("/");
105 ret = of_property_read_string(root, "model", &soc_dev_attr->machine);
110 switch (__mxc_cpu_type) {
136 ocotp_compat = "fsl,imx6sl-ocotp";
140 ocotp_compat = "fsl,imx6q-ocotp";
144 ocotp_compat = "fsl,imx6sx-ocotp";
148 ocotp_compat = "fsl,imx6q-ocotp";
152 ocotp_compat = "fsl,imx6ul-ocotp";
155 case MXC_CPU_IMX6ULL:
156 ocotp_compat = "fsl,imx6ull-ocotp";
159 case MXC_CPU_IMX6ULZ:
160 ocotp_compat = "fsl,imx6ull-ocotp";
163 case MXC_CPU_IMX6SLL:
164 ocotp_compat = "fsl,imx6sll-ocotp";
168 ocotp_compat = "fsl,imx7d-ocotp";
171 case MXC_CPU_IMX7ULP:
172 ocotp_compat = "fsl,imx7ulp-ocotp";
178 soc_dev_attr->soc_id = soc_id;
181 ocotp = syscon_regmap_lookup_by_compatible(ocotp_compat);
183 pr_err("%s: failed to find %s regmap!\n", __func__, ocotp_compat);
186 if (!IS_ERR_OR_NULL(ocotp)) {
187 if (__mxc_cpu_type == MXC_CPU_IMX7ULP) {
188 regmap_read(ocotp, OCOTP_ULP_UID_4, &val);
189 soc_uid = val & 0xffff;
190 regmap_read(ocotp, OCOTP_ULP_UID_3, &val);
192 soc_uid |= val & 0xffff;
193 regmap_read(ocotp, OCOTP_ULP_UID_2, &val);
195 soc_uid |= val & 0xffff;
196 regmap_read(ocotp, OCOTP_ULP_UID_1, &val);
198 soc_uid |= val & 0xffff;
200 regmap_read(ocotp, OCOTP_UID_H, &val);
202 regmap_read(ocotp, OCOTP_UID_L, &val);
208 soc_dev_attr->revision = kasprintf(GFP_KERNEL, "%d.%d",
209 (imx_soc_revision >> 4) & 0xf,
210 imx_soc_revision & 0xf);
211 if (!soc_dev_attr->revision)
214 soc_dev_attr->serial_number = kasprintf(GFP_KERNEL, "%016llX", soc_uid);
215 if (!soc_dev_attr->serial_number)
218 soc_dev = soc_device_register(soc_dev_attr);
220 goto free_serial_number;
222 return soc_device_to_device(soc_dev);
225 kfree(soc_dev_attr->serial_number);
227 kfree(soc_dev_attr->revision);