0001
0002
0003
0004
0005
0006 #include <dt-bindings/firmware/imx/rsrc.h>
0007 #include <linux/firmware/imx/sci.h>
0008 #include <linux/slab.h>
0009 #include <linux/sys_soc.h>
0010 #include <linux/platform_device.h>
0011 #include <linux/of.h>
0012
0013 static struct imx_sc_ipc *imx_sc_soc_ipc_handle;
0014
0015 struct imx_sc_msg_misc_get_soc_id {
0016 struct imx_sc_rpc_msg hdr;
0017 union {
0018 struct {
0019 u32 control;
0020 u16 resource;
0021 } __packed req;
0022 struct {
0023 u32 id;
0024 } resp;
0025 } data;
0026 } __packed __aligned(4);
0027
0028 struct imx_sc_msg_misc_get_soc_uid {
0029 struct imx_sc_rpc_msg hdr;
0030 u32 uid_low;
0031 u32 uid_high;
0032 } __packed;
0033
0034 static int imx_scu_soc_uid(u64 *soc_uid)
0035 {
0036 struct imx_sc_msg_misc_get_soc_uid msg;
0037 struct imx_sc_rpc_msg *hdr = &msg.hdr;
0038 int ret;
0039
0040 hdr->ver = IMX_SC_RPC_VERSION;
0041 hdr->svc = IMX_SC_RPC_SVC_MISC;
0042 hdr->func = IMX_SC_MISC_FUNC_UNIQUE_ID;
0043 hdr->size = 1;
0044
0045 ret = imx_scu_call_rpc(imx_sc_soc_ipc_handle, &msg, true);
0046 if (ret) {
0047 pr_err("%s: get soc uid failed, ret %d\n", __func__, ret);
0048 return ret;
0049 }
0050
0051 *soc_uid = msg.uid_high;
0052 *soc_uid <<= 32;
0053 *soc_uid |= msg.uid_low;
0054
0055 return 0;
0056 }
0057
0058 static int imx_scu_soc_id(void)
0059 {
0060 struct imx_sc_msg_misc_get_soc_id msg;
0061 struct imx_sc_rpc_msg *hdr = &msg.hdr;
0062 int ret;
0063
0064 hdr->ver = IMX_SC_RPC_VERSION;
0065 hdr->svc = IMX_SC_RPC_SVC_MISC;
0066 hdr->func = IMX_SC_MISC_FUNC_GET_CONTROL;
0067 hdr->size = 3;
0068
0069 msg.data.req.control = IMX_SC_C_ID;
0070 msg.data.req.resource = IMX_SC_R_SYSTEM;
0071
0072 ret = imx_scu_call_rpc(imx_sc_soc_ipc_handle, &msg, true);
0073 if (ret) {
0074 pr_err("%s: get soc info failed, ret %d\n", __func__, ret);
0075 return ret;
0076 }
0077
0078 return msg.data.resp.id;
0079 }
0080
0081 int imx_scu_soc_init(struct device *dev)
0082 {
0083 struct soc_device_attribute *soc_dev_attr;
0084 struct soc_device *soc_dev;
0085 int id, ret;
0086 u64 uid = 0;
0087 u32 val;
0088
0089 ret = imx_scu_get_handle(&imx_sc_soc_ipc_handle);
0090 if (ret)
0091 return ret;
0092
0093 soc_dev_attr = devm_kzalloc(dev, sizeof(*soc_dev_attr),
0094 GFP_KERNEL);
0095 if (!soc_dev_attr)
0096 return -ENOMEM;
0097
0098 soc_dev_attr->family = "Freescale i.MX";
0099
0100 ret = of_property_read_string(of_root,
0101 "model",
0102 &soc_dev_attr->machine);
0103 if (ret)
0104 return ret;
0105
0106 id = imx_scu_soc_id();
0107 if (id < 0)
0108 return -EINVAL;
0109
0110 ret = imx_scu_soc_uid(&uid);
0111 if (ret < 0)
0112 return -EINVAL;
0113
0114
0115 val = id & 0x1f;
0116 soc_dev_attr->soc_id = devm_kasprintf(dev, GFP_KERNEL, "0x%x", val);
0117 if (!soc_dev_attr->soc_id)
0118 return -ENOMEM;
0119
0120
0121 val = (id >> 5) & 0xf;
0122 val = (((val >> 2) + 1) << 4) | (val & 0x3);
0123 soc_dev_attr->revision = devm_kasprintf(dev, GFP_KERNEL, "%d.%d",
0124 (val >> 4) & 0xf, val & 0xf);
0125 if (!soc_dev_attr->revision)
0126 return -ENOMEM;
0127
0128 soc_dev_attr->serial_number = devm_kasprintf(dev, GFP_KERNEL,
0129 "%016llX", uid);
0130 if (!soc_dev_attr->serial_number)
0131 return -ENOMEM;
0132
0133 soc_dev = soc_device_register(soc_dev_attr);
0134 if (IS_ERR(soc_dev))
0135 return PTR_ERR(soc_dev);
0136
0137 return 0;
0138 }