xref: /openbmc/linux/drivers/firmware/smccc/soc_id.c (revision 35727af2)
1821b67faSSudeep Holla // SPDX-License-Identifier: GPL-2.0
2821b67faSSudeep Holla /*
3821b67faSSudeep Holla  * Copyright 2020 Arm Limited
4821b67faSSudeep Holla  */
5821b67faSSudeep Holla 
6821b67faSSudeep Holla #define pr_fmt(fmt) "SMCCC: SOC_ID: " fmt
7821b67faSSudeep Holla 
8821b67faSSudeep Holla #include <linux/arm-smccc.h>
9821b67faSSudeep Holla #include <linux/bitfield.h>
10821b67faSSudeep Holla #include <linux/device.h>
11821b67faSSudeep Holla #include <linux/module.h>
12821b67faSSudeep Holla #include <linux/kernel.h>
13821b67faSSudeep Holla #include <linux/slab.h>
14821b67faSSudeep Holla #include <linux/sys_soc.h>
15821b67faSSudeep Holla 
16821b67faSSudeep Holla #define SMCCC_SOC_ID_JEP106_BANK_IDX_MASK	GENMASK(30, 24)
17821b67faSSudeep Holla /*
18821b67faSSudeep Holla  * As per the SMC Calling Convention specification v1.2 (ARM DEN 0028C)
19821b67faSSudeep Holla  * Section 7.4 SMCCC_ARCH_SOC_ID bits[23:16] are JEP-106 identification
20821b67faSSudeep Holla  * code with parity bit for the SiP. We can drop the parity bit.
21821b67faSSudeep Holla  */
22821b67faSSudeep Holla #define SMCCC_SOC_ID_JEP106_ID_CODE_MASK	GENMASK(22, 16)
23821b67faSSudeep Holla #define SMCCC_SOC_ID_IMP_DEF_SOC_ID_MASK	GENMASK(15, 0)
24821b67faSSudeep Holla 
25821b67faSSudeep Holla #define JEP106_BANK_CONT_CODE(x)	\
26821b67faSSudeep Holla 	(u8)(FIELD_GET(SMCCC_SOC_ID_JEP106_BANK_IDX_MASK, (x)))
27821b67faSSudeep Holla #define JEP106_ID_CODE(x)	\
28821b67faSSudeep Holla 	(u8)(FIELD_GET(SMCCC_SOC_ID_JEP106_ID_CODE_MASK, (x)))
29821b67faSSudeep Holla #define IMP_DEF_SOC_ID(x)	\
30821b67faSSudeep Holla 	(u16)(FIELD_GET(SMCCC_SOC_ID_IMP_DEF_SOC_ID_MASK, (x)))
31821b67faSSudeep Holla 
32821b67faSSudeep Holla static struct soc_device *soc_dev;
33821b67faSSudeep Holla static struct soc_device_attribute *soc_dev_attr;
34821b67faSSudeep Holla 
35821b67faSSudeep Holla static int __init smccc_soc_init(void)
36821b67faSSudeep Holla {
37821b67faSSudeep Holla 	struct arm_smccc_res res;
38821b67faSSudeep Holla 	int soc_id_rev, soc_id_version;
39821b67faSSudeep Holla 	static char soc_id_str[20], soc_id_rev_str[12];
40821b67faSSudeep Holla 	static char soc_id_jep106_id_str[12];
41821b67faSSudeep Holla 
42821b67faSSudeep Holla 	if (arm_smccc_get_version() < ARM_SMCCC_VERSION_1_2)
43821b67faSSudeep Holla 		return 0;
44821b67faSSudeep Holla 
45*35727af2SShanker Donthineni 	soc_id_version = arm_smccc_get_soc_id_version();
46*35727af2SShanker Donthineni 	if (soc_id_version == SMCCC_RET_NOT_SUPPORTED) {
47821b67faSSudeep Holla 		pr_info("ARCH_SOC_ID not implemented, skipping ....\n");
48821b67faSSudeep Holla 		return 0;
49821b67faSSudeep Holla 	}
50821b67faSSudeep Holla 
51*35727af2SShanker Donthineni 	if (soc_id_version < 0) {
52821b67faSSudeep Holla 		pr_err("ARCH_SOC_ID(0) returned error: %lx\n", res.a0);
53821b67faSSudeep Holla 		return -EINVAL;
54821b67faSSudeep Holla 	}
55821b67faSSudeep Holla 
56*35727af2SShanker Donthineni 	soc_id_rev = arm_smccc_get_soc_id_revision();
57*35727af2SShanker Donthineni 	if (soc_id_rev < 0) {
58821b67faSSudeep Holla 		pr_err("ARCH_SOC_ID(1) returned error: %lx\n", res.a0);
59821b67faSSudeep Holla 		return -EINVAL;
60821b67faSSudeep Holla 	}
61821b67faSSudeep Holla 
62821b67faSSudeep Holla 	soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
63821b67faSSudeep Holla 	if (!soc_dev_attr)
64821b67faSSudeep Holla 		return -ENOMEM;
65821b67faSSudeep Holla 
66821b67faSSudeep Holla 	sprintf(soc_id_rev_str, "0x%08x", soc_id_rev);
67821b67faSSudeep Holla 	sprintf(soc_id_jep106_id_str, "jep106:%02x%02x",
68821b67faSSudeep Holla 		JEP106_BANK_CONT_CODE(soc_id_version),
69821b67faSSudeep Holla 		JEP106_ID_CODE(soc_id_version));
70821b67faSSudeep Holla 	sprintf(soc_id_str, "%s:%04x", soc_id_jep106_id_str,
71821b67faSSudeep Holla 		IMP_DEF_SOC_ID(soc_id_version));
72821b67faSSudeep Holla 
73821b67faSSudeep Holla 	soc_dev_attr->soc_id = soc_id_str;
74821b67faSSudeep Holla 	soc_dev_attr->revision = soc_id_rev_str;
75821b67faSSudeep Holla 	soc_dev_attr->family = soc_id_jep106_id_str;
76821b67faSSudeep Holla 
77821b67faSSudeep Holla 	soc_dev = soc_device_register(soc_dev_attr);
78821b67faSSudeep Holla 	if (IS_ERR(soc_dev)) {
79821b67faSSudeep Holla 		kfree(soc_dev_attr);
80821b67faSSudeep Holla 		return PTR_ERR(soc_dev);
81821b67faSSudeep Holla 	}
82821b67faSSudeep Holla 
83821b67faSSudeep Holla 	pr_info("ID = %s Revision = %s\n", soc_dev_attr->soc_id,
84821b67faSSudeep Holla 		soc_dev_attr->revision);
85821b67faSSudeep Holla 
86821b67faSSudeep Holla 	return 0;
87821b67faSSudeep Holla }
88821b67faSSudeep Holla module_init(smccc_soc_init);
89821b67faSSudeep Holla 
90821b67faSSudeep Holla static void __exit smccc_soc_exit(void)
91821b67faSSudeep Holla {
92821b67faSSudeep Holla 	if (soc_dev)
93821b67faSSudeep Holla 		soc_device_unregister(soc_dev);
94821b67faSSudeep Holla 	kfree(soc_dev_attr);
95821b67faSSudeep Holla }
96821b67faSSudeep Holla module_exit(smccc_soc_exit);
97