David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright 2019 NXP. |
| 4 | */ |
| 5 | |
| 6 | #include <dt-bindings/firmware/imx/rsrc.h> |
| 7 | #include <linux/firmware/imx/sci.h> |
| 8 | #include <linux/slab.h> |
| 9 | #include <linux/sys_soc.h> |
| 10 | #include <linux/platform_device.h> |
| 11 | #include <linux/of.h> |
| 12 | |
| 13 | #define IMX_SCU_SOC_DRIVER_NAME "imx-scu-soc" |
| 14 | |
| 15 | static struct imx_sc_ipc *soc_ipc_handle; |
| 16 | |
| 17 | struct imx_sc_msg_misc_get_soc_id { |
| 18 | struct imx_sc_rpc_msg hdr; |
| 19 | union { |
| 20 | struct { |
| 21 | u32 control; |
| 22 | u16 resource; |
| 23 | } __packed req; |
| 24 | struct { |
| 25 | u32 id; |
| 26 | } resp; |
| 27 | } data; |
| 28 | } __packed; |
| 29 | |
| 30 | struct imx_sc_msg_misc_get_soc_uid { |
| 31 | struct imx_sc_rpc_msg hdr; |
| 32 | u32 uid_low; |
| 33 | u32 uid_high; |
| 34 | } __packed; |
| 35 | |
| 36 | static ssize_t soc_uid_show(struct device *dev, |
| 37 | struct device_attribute *attr, char *buf) |
| 38 | { |
| 39 | struct imx_sc_msg_misc_get_soc_uid msg; |
| 40 | struct imx_sc_rpc_msg *hdr = &msg.hdr; |
| 41 | u64 soc_uid; |
| 42 | int ret; |
| 43 | |
| 44 | hdr->ver = IMX_SC_RPC_VERSION; |
| 45 | hdr->svc = IMX_SC_RPC_SVC_MISC; |
| 46 | hdr->func = IMX_SC_MISC_FUNC_UNIQUE_ID; |
| 47 | hdr->size = 1; |
| 48 | |
| 49 | ret = imx_scu_call_rpc(soc_ipc_handle, &msg, true); |
| 50 | if (ret) { |
| 51 | pr_err("%s: get soc uid failed, ret %d\n", __func__, ret); |
| 52 | return ret; |
| 53 | } |
| 54 | |
| 55 | soc_uid = msg.uid_high; |
| 56 | soc_uid <<= 32; |
| 57 | soc_uid |= msg.uid_low; |
| 58 | |
| 59 | return sprintf(buf, "%016llX\n", soc_uid); |
| 60 | } |
| 61 | |
| 62 | static DEVICE_ATTR_RO(soc_uid); |
| 63 | |
| 64 | static int imx_scu_soc_id(void) |
| 65 | { |
| 66 | struct imx_sc_msg_misc_get_soc_id msg; |
| 67 | struct imx_sc_rpc_msg *hdr = &msg.hdr; |
| 68 | int ret; |
| 69 | |
| 70 | hdr->ver = IMX_SC_RPC_VERSION; |
| 71 | hdr->svc = IMX_SC_RPC_SVC_MISC; |
| 72 | hdr->func = IMX_SC_MISC_FUNC_GET_CONTROL; |
| 73 | hdr->size = 3; |
| 74 | |
| 75 | msg.data.req.control = IMX_SC_C_ID; |
| 76 | msg.data.req.resource = IMX_SC_R_SYSTEM; |
| 77 | |
| 78 | ret = imx_scu_call_rpc(soc_ipc_handle, &msg, true); |
| 79 | if (ret) { |
| 80 | pr_err("%s: get soc info failed, ret %d\n", __func__, ret); |
| 81 | return ret; |
| 82 | } |
| 83 | |
| 84 | return msg.data.resp.id; |
| 85 | } |
| 86 | |
| 87 | static int imx_scu_soc_probe(struct platform_device *pdev) |
| 88 | { |
| 89 | struct soc_device_attribute *soc_dev_attr; |
| 90 | struct soc_device *soc_dev; |
| 91 | int id, ret; |
| 92 | u32 val; |
| 93 | |
| 94 | ret = imx_scu_get_handle(&soc_ipc_handle); |
| 95 | if (ret) |
| 96 | return ret; |
| 97 | |
| 98 | soc_dev_attr = devm_kzalloc(&pdev->dev, sizeof(*soc_dev_attr), |
| 99 | GFP_KERNEL); |
| 100 | if (!soc_dev_attr) |
| 101 | return -ENOMEM; |
| 102 | |
| 103 | soc_dev_attr->family = "Freescale i.MX"; |
| 104 | |
| 105 | ret = of_property_read_string(of_root, |
| 106 | "model", |
| 107 | &soc_dev_attr->machine); |
| 108 | if (ret) |
| 109 | return ret; |
| 110 | |
| 111 | id = imx_scu_soc_id(); |
| 112 | if (id < 0) |
| 113 | return -EINVAL; |
| 114 | |
| 115 | /* format soc_id value passed from SCU firmware */ |
| 116 | val = id & 0x1f; |
| 117 | soc_dev_attr->soc_id = kasprintf(GFP_KERNEL, "0x%x", val); |
| 118 | if (!soc_dev_attr->soc_id) |
| 119 | return -ENOMEM; |
| 120 | |
| 121 | /* format revision value passed from SCU firmware */ |
| 122 | val = (id >> 5) & 0xf; |
| 123 | val = (((val >> 2) + 1) << 4) | (val & 0x3); |
| 124 | soc_dev_attr->revision = kasprintf(GFP_KERNEL, |
| 125 | "%d.%d", |
| 126 | (val >> 4) & 0xf, |
| 127 | val & 0xf); |
| 128 | if (!soc_dev_attr->revision) { |
| 129 | ret = -ENOMEM; |
| 130 | goto free_soc_id; |
| 131 | } |
| 132 | |
| 133 | soc_dev = soc_device_register(soc_dev_attr); |
| 134 | if (IS_ERR(soc_dev)) { |
| 135 | ret = PTR_ERR(soc_dev); |
| 136 | goto free_revision; |
| 137 | } |
| 138 | |
| 139 | ret = device_create_file(soc_device_to_device(soc_dev), |
| 140 | &dev_attr_soc_uid); |
| 141 | if (ret) |
| 142 | goto free_revision; |
| 143 | |
| 144 | return 0; |
| 145 | |
| 146 | free_revision: |
| 147 | kfree(soc_dev_attr->revision); |
| 148 | free_soc_id: |
| 149 | kfree(soc_dev_attr->soc_id); |
| 150 | return ret; |
| 151 | } |
| 152 | |
| 153 | static struct platform_driver imx_scu_soc_driver = { |
| 154 | .driver = { |
| 155 | .name = IMX_SCU_SOC_DRIVER_NAME, |
| 156 | }, |
| 157 | .probe = imx_scu_soc_probe, |
| 158 | }; |
| 159 | |
| 160 | static int __init imx_scu_soc_init(void) |
| 161 | { |
| 162 | struct platform_device *pdev; |
| 163 | struct device_node *np; |
| 164 | int ret; |
| 165 | |
| 166 | np = of_find_compatible_node(NULL, NULL, "fsl,imx-scu"); |
| 167 | if (!np) |
| 168 | return -ENODEV; |
| 169 | |
| 170 | of_node_put(np); |
| 171 | |
| 172 | ret = platform_driver_register(&imx_scu_soc_driver); |
| 173 | if (ret) |
| 174 | return ret; |
| 175 | |
| 176 | pdev = platform_device_register_simple(IMX_SCU_SOC_DRIVER_NAME, |
| 177 | -1, NULL, 0); |
| 178 | if (IS_ERR(pdev)) |
| 179 | platform_driver_unregister(&imx_scu_soc_driver); |
| 180 | |
| 181 | return PTR_ERR_OR_ZERO(pdev); |
| 182 | } |
| 183 | device_initcall(imx_scu_soc_init); |