blob: fb70b8a3f7c523964e1aec543341638bf7929e36 [file] [log] [blame]
Anson Huang15f52762019-05-24 01:52:36 +00001// 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
15static struct imx_sc_ipc *soc_ipc_handle;
16
17struct 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
Anson Huang73feb4d2019-07-02 15:45:45 +080030struct 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
Anson Huang7ae399b2019-10-25 14:56:23 +080036static int imx_scu_soc_uid(u64 *soc_uid)
Anson Huang73feb4d2019-07-02 15:45:45 +080037{
38 struct imx_sc_msg_misc_get_soc_uid msg;
39 struct imx_sc_rpc_msg *hdr = &msg.hdr;
Anson Huang73feb4d2019-07-02 15:45:45 +080040 int ret;
41
42 hdr->ver = IMX_SC_RPC_VERSION;
43 hdr->svc = IMX_SC_RPC_SVC_MISC;
44 hdr->func = IMX_SC_MISC_FUNC_UNIQUE_ID;
45 hdr->size = 1;
46
Anson Huang30ca9b02019-09-04 15:13:14 -040047 ret = imx_scu_call_rpc(soc_ipc_handle, &msg, true);
Anson Huang73feb4d2019-07-02 15:45:45 +080048 if (ret) {
49 pr_err("%s: get soc uid failed, ret %d\n", __func__, ret);
50 return ret;
51 }
52
Anson Huang7ae399b2019-10-25 14:56:23 +080053 *soc_uid = msg.uid_high;
54 *soc_uid <<= 32;
55 *soc_uid |= msg.uid_low;
Anson Huang73feb4d2019-07-02 15:45:45 +080056
Anson Huang7ae399b2019-10-25 14:56:23 +080057 return 0;
Anson Huang73feb4d2019-07-02 15:45:45 +080058}
59
Anson Huang15f52762019-05-24 01:52:36 +000060static int imx_scu_soc_id(void)
61{
62 struct imx_sc_msg_misc_get_soc_id msg;
63 struct imx_sc_rpc_msg *hdr = &msg.hdr;
64 int ret;
65
66 hdr->ver = IMX_SC_RPC_VERSION;
67 hdr->svc = IMX_SC_RPC_SVC_MISC;
68 hdr->func = IMX_SC_MISC_FUNC_GET_CONTROL;
69 hdr->size = 3;
70
71 msg.data.req.control = IMX_SC_C_ID;
72 msg.data.req.resource = IMX_SC_R_SYSTEM;
73
74 ret = imx_scu_call_rpc(soc_ipc_handle, &msg, true);
75 if (ret) {
76 pr_err("%s: get soc info failed, ret %d\n", __func__, ret);
77 return ret;
78 }
79
80 return msg.data.resp.id;
81}
82
83static int imx_scu_soc_probe(struct platform_device *pdev)
84{
85 struct soc_device_attribute *soc_dev_attr;
86 struct soc_device *soc_dev;
87 int id, ret;
Anson Huang7ae399b2019-10-25 14:56:23 +080088 u64 uid = 0;
Anson Huang15f52762019-05-24 01:52:36 +000089 u32 val;
90
91 ret = imx_scu_get_handle(&soc_ipc_handle);
92 if (ret)
93 return ret;
94
95 soc_dev_attr = devm_kzalloc(&pdev->dev, sizeof(*soc_dev_attr),
96 GFP_KERNEL);
97 if (!soc_dev_attr)
98 return -ENOMEM;
99
100 soc_dev_attr->family = "Freescale i.MX";
101
102 ret = of_property_read_string(of_root,
103 "model",
104 &soc_dev_attr->machine);
105 if (ret)
106 return ret;
107
108 id = imx_scu_soc_id();
109 if (id < 0)
110 return -EINVAL;
111
Anson Huang7ae399b2019-10-25 14:56:23 +0800112 ret = imx_scu_soc_uid(&uid);
113 if (ret < 0)
114 return -EINVAL;
115
Anson Huang15f52762019-05-24 01:52:36 +0000116 /* format soc_id value passed from SCU firmware */
117 val = id & 0x1f;
118 soc_dev_attr->soc_id = kasprintf(GFP_KERNEL, "0x%x", val);
119 if (!soc_dev_attr->soc_id)
120 return -ENOMEM;
121
122 /* format revision value passed from SCU firmware */
123 val = (id >> 5) & 0xf;
124 val = (((val >> 2) + 1) << 4) | (val & 0x3);
125 soc_dev_attr->revision = kasprintf(GFP_KERNEL,
126 "%d.%d",
127 (val >> 4) & 0xf,
128 val & 0xf);
129 if (!soc_dev_attr->revision) {
130 ret = -ENOMEM;
131 goto free_soc_id;
132 }
133
Anson Huang7ae399b2019-10-25 14:56:23 +0800134 soc_dev_attr->serial_number = kasprintf(GFP_KERNEL, "%016llX", uid);
135 if (!soc_dev_attr->serial_number) {
136 ret = -ENOMEM;
Anson Huang15f52762019-05-24 01:52:36 +0000137 goto free_revision;
138 }
139
Anson Huang7ae399b2019-10-25 14:56:23 +0800140 soc_dev = soc_device_register(soc_dev_attr);
141 if (IS_ERR(soc_dev)) {
142 ret = PTR_ERR(soc_dev);
143 goto free_serial_number;
144 }
Anson Huang73feb4d2019-07-02 15:45:45 +0800145
Anson Huang15f52762019-05-24 01:52:36 +0000146 return 0;
147
Anson Huang7ae399b2019-10-25 14:56:23 +0800148free_serial_number:
149 kfree(soc_dev_attr->serial_number);
Anson Huang15f52762019-05-24 01:52:36 +0000150free_revision:
151 kfree(soc_dev_attr->revision);
152free_soc_id:
153 kfree(soc_dev_attr->soc_id);
154 return ret;
155}
156
157static struct platform_driver imx_scu_soc_driver = {
158 .driver = {
159 .name = IMX_SCU_SOC_DRIVER_NAME,
160 },
161 .probe = imx_scu_soc_probe,
162};
163
164static int __init imx_scu_soc_init(void)
165{
166 struct platform_device *pdev;
167 struct device_node *np;
168 int ret;
169
170 np = of_find_compatible_node(NULL, NULL, "fsl,imx-scu");
171 if (!np)
172 return -ENODEV;
173
174 of_node_put(np);
175
176 ret = platform_driver_register(&imx_scu_soc_driver);
177 if (ret)
178 return ret;
179
180 pdev = platform_device_register_simple(IMX_SCU_SOC_DRIVER_NAME,
181 -1, NULL, 0);
182 if (IS_ERR(pdev))
183 platform_driver_unregister(&imx_scu_soc_driver);
184
185 return PTR_ERR_OR_ZERO(pdev);
186}
187device_initcall(imx_scu_soc_init);