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