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 if (of_machine_is_compatible("fsl,ls1021a")) 40 return 0; 41 42 soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL); 43 if (!soc_dev_attr) 44 return -ENOMEM; 45 46 soc_dev_attr->family = "Freescale i.MX"; 47 48 root = of_find_node_by_path("/"); 49 ret = of_property_read_string(root, "model", &soc_dev_attr->machine); 50 of_node_put(root); 51 if (ret) 52 goto free_soc; 53 54 switch (__mxc_cpu_type) { 55 case MXC_CPU_MX1: 56 soc_id = "i.MX1"; 57 break; 58 case MXC_CPU_MX21: 59 soc_id = "i.MX21"; 60 break; 61 case MXC_CPU_MX25: 62 soc_id = "i.MX25"; 63 break; 64 case MXC_CPU_MX27: 65 soc_id = "i.MX27"; 66 break; 67 case MXC_CPU_MX31: 68 soc_id = "i.MX31"; 69 break; 70 case MXC_CPU_MX35: 71 soc_id = "i.MX35"; 72 break; 73 case MXC_CPU_MX50: 74 soc_id = "i.MX50"; 75 break; 76 case MXC_CPU_MX51: 77 ocotp_compat = "fsl,imx51-iim"; 78 soc_id = "i.MX51"; 79 break; 80 case MXC_CPU_MX53: 81 ocotp_compat = "fsl,imx53-iim"; 82 soc_id = "i.MX53"; 83 break; 84 case MXC_CPU_IMX6SL: 85 ocotp_compat = "fsl,imx6sl-ocotp"; 86 soc_id = "i.MX6SL"; 87 break; 88 case MXC_CPU_IMX6DL: 89 ocotp_compat = "fsl,imx6q-ocotp"; 90 soc_id = "i.MX6DL"; 91 break; 92 case MXC_CPU_IMX6SX: 93 ocotp_compat = "fsl,imx6sx-ocotp"; 94 soc_id = "i.MX6SX"; 95 break; 96 case MXC_CPU_IMX6Q: 97 ocotp_compat = "fsl,imx6q-ocotp"; 98 soc_id = "i.MX6Q"; 99 break; 100 case MXC_CPU_IMX6UL: 101 ocotp_compat = "fsl,imx6ul-ocotp"; 102 soc_id = "i.MX6UL"; 103 break; 104 case MXC_CPU_IMX6ULL: 105 ocotp_compat = "fsl,imx6ull-ocotp"; 106 soc_id = "i.MX6ULL"; 107 break; 108 case MXC_CPU_IMX6ULZ: 109 ocotp_compat = "fsl,imx6ull-ocotp"; 110 soc_id = "i.MX6ULZ"; 111 break; 112 case MXC_CPU_IMX6SLL: 113 ocotp_compat = "fsl,imx6sll-ocotp"; 114 soc_id = "i.MX6SLL"; 115 break; 116 case MXC_CPU_IMX7D: 117 ocotp_compat = "fsl,imx7d-ocotp"; 118 soc_id = "i.MX7D"; 119 break; 120 case MXC_CPU_IMX7ULP: 121 ocotp_compat = "fsl,imx7ulp-ocotp"; 122 soc_id = "i.MX7ULP"; 123 break; 124 case MXC_CPU_VF500: 125 ocotp_compat = "fsl,vf610-ocotp"; 126 soc_id = "VF500"; 127 break; 128 case MXC_CPU_VF510: 129 ocotp_compat = "fsl,vf610-ocotp"; 130 soc_id = "VF510"; 131 break; 132 case MXC_CPU_VF600: 133 ocotp_compat = "fsl,vf610-ocotp"; 134 soc_id = "VF600"; 135 break; 136 case MXC_CPU_VF610: 137 ocotp_compat = "fsl,vf610-ocotp"; 138 soc_id = "VF610"; 139 break; 140 default: 141 soc_id = "Unknown"; 142 } 143 soc_dev_attr->soc_id = soc_id; 144 145 if (ocotp_compat) { 146 ocotp = syscon_regmap_lookup_by_compatible(ocotp_compat); 147 if (IS_ERR(ocotp)) 148 pr_err("%s: failed to find %s regmap!\n", __func__, ocotp_compat); 149 } 150 151 if (!IS_ERR_OR_NULL(ocotp)) { 152 if (__mxc_cpu_type == MXC_CPU_IMX7ULP) { 153 regmap_read(ocotp, OCOTP_ULP_UID_4, &val); 154 soc_uid = val & 0xffff; 155 regmap_read(ocotp, OCOTP_ULP_UID_3, &val); 156 soc_uid <<= 16; 157 soc_uid |= val & 0xffff; 158 regmap_read(ocotp, OCOTP_ULP_UID_2, &val); 159 soc_uid <<= 16; 160 soc_uid |= val & 0xffff; 161 regmap_read(ocotp, OCOTP_ULP_UID_1, &val); 162 soc_uid <<= 16; 163 soc_uid |= val & 0xffff; 164 } else if (__mxc_cpu_type == MXC_CPU_MX51 || 165 __mxc_cpu_type == MXC_CPU_MX53) { 166 for (i=0; i < 8; i++) { 167 regmap_read(ocotp, IIM_UID + i*4, &val); 168 soc_uid <<= 8; 169 soc_uid |= (val & 0xff); 170 } 171 } else { 172 regmap_read(ocotp, OCOTP_UID_H, &val); 173 soc_uid = val; 174 regmap_read(ocotp, OCOTP_UID_L, &val); 175 soc_uid <<= 32; 176 soc_uid |= val; 177 } 178 } 179 180 soc_dev_attr->revision = kasprintf(GFP_KERNEL, "%d.%d", 181 (imx_get_soc_revision() >> 4) & 0xf, 182 imx_get_soc_revision() & 0xf); 183 if (!soc_dev_attr->revision) { 184 ret = -ENOMEM; 185 goto free_soc; 186 } 187 188 soc_dev_attr->serial_number = kasprintf(GFP_KERNEL, "%016llX", soc_uid); 189 if (!soc_dev_attr->serial_number) { 190 ret = -ENOMEM; 191 goto free_rev; 192 } 193 194 soc_dev = soc_device_register(soc_dev_attr); 195 if (IS_ERR(soc_dev)) { 196 ret = PTR_ERR(soc_dev); 197 goto free_serial_number; 198 } 199 200 return 0; 201 202 free_serial_number: 203 kfree(soc_dev_attr->serial_number); 204 free_rev: 205 kfree(soc_dev_attr->revision); 206 free_soc: 207 kfree(soc_dev_attr); 208 return ret; 209 } 210 device_initcall(imx_soc_device_init); 211