xref: /openbmc/linux/drivers/soc/ti/k3-socinfo.c (revision 4d75f5c664195b970e1cd2fd25b65b5eff257a0a)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * TI K3 SoC info driver
4  *
5  * Copyright (C) 2020 Texas Instruments Incorporated - http://www.ti.com
6  */
7 
8 #include <linux/mfd/syscon.h>
9 #include <linux/of.h>
10 #include <linux/of_address.h>
11 #include <linux/regmap.h>
12 #include <linux/platform_device.h>
13 #include <linux/slab.h>
14 #include <linux/string.h>
15 #include <linux/sys_soc.h>
16 
17 #define CTRLMMR_WKUP_JTAGID_REG		0
18 /*
19  * Bits:
20  *  31-28 VARIANT	Device variant
21  *  27-12 PARTNO	Part number
22  *  11-1  MFG		Indicates TI as manufacturer (0x17)
23  *  1			Always 1
24  */
25 #define CTRLMMR_WKUP_JTAGID_VARIANT_SHIFT	(28)
26 #define CTRLMMR_WKUP_JTAGID_VARIANT_MASK	GENMASK(31, 28)
27 
28 #define CTRLMMR_WKUP_JTAGID_PARTNO_SHIFT	(12)
29 #define CTRLMMR_WKUP_JTAGID_PARTNO_MASK		GENMASK(27, 12)
30 
31 #define CTRLMMR_WKUP_JTAGID_MFG_SHIFT		(1)
32 #define CTRLMMR_WKUP_JTAGID_MFG_MASK		GENMASK(11, 1)
33 
34 #define CTRLMMR_WKUP_JTAGID_MFG_TI		0x17
35 
36 static const struct k3_soc_id {
37 	unsigned int id;
38 	const char *family_name;
39 } k3_soc_ids[] = {
40 	{ 0xBB5A, "AM65X" },
41 	{ 0xBB64, "J721E" },
42 	{ 0xBB6D, "J7200" },
43 	{ 0xBB38, "AM64X" },
44 	{ 0xBB75, "J721S2"},
45 	{ 0xBB7E, "AM62X" },
46 	{ 0xBB80, "J784S4" },
47 	{ 0xBB8D, "AM62AX" },
48 	{ 0xBB9D, "AM62PX" },
49 };
50 
51 static int
k3_chipinfo_partno_to_names(unsigned int partno,struct soc_device_attribute * soc_dev_attr)52 k3_chipinfo_partno_to_names(unsigned int partno,
53 			    struct soc_device_attribute *soc_dev_attr)
54 {
55 	int i;
56 
57 	for (i = 0; i < ARRAY_SIZE(k3_soc_ids); i++)
58 		if (partno == k3_soc_ids[i].id) {
59 			soc_dev_attr->family = k3_soc_ids[i].family_name;
60 			return 0;
61 		}
62 
63 	return -EINVAL;
64 }
65 
66 static const struct regmap_config k3_chipinfo_regmap_cfg = {
67 	.reg_bits = 32,
68 	.val_bits = 32,
69 	.reg_stride = 4,
70 };
71 
k3_chipinfo_probe(struct platform_device * pdev)72 static int k3_chipinfo_probe(struct platform_device *pdev)
73 {
74 	struct device_node *node = pdev->dev.of_node;
75 	struct soc_device_attribute *soc_dev_attr;
76 	struct device *dev = &pdev->dev;
77 	struct soc_device *soc_dev;
78 	struct regmap *regmap;
79 	void __iomem *base;
80 	u32 partno_id;
81 	u32 variant;
82 	u32 jtag_id;
83 	u32 mfg;
84 	int ret;
85 
86 	base = devm_platform_ioremap_resource(pdev, 0);
87 	if (IS_ERR(base))
88 		return PTR_ERR(base);
89 
90 	regmap = regmap_init_mmio(dev, base, &k3_chipinfo_regmap_cfg);
91 	if (IS_ERR(regmap))
92 		return PTR_ERR(regmap);
93 
94 	ret = regmap_read(regmap, CTRLMMR_WKUP_JTAGID_REG, &jtag_id);
95 	if (ret < 0)
96 		return ret;
97 
98 	mfg = (jtag_id & CTRLMMR_WKUP_JTAGID_MFG_MASK) >>
99 	       CTRLMMR_WKUP_JTAGID_MFG_SHIFT;
100 
101 	if (mfg != CTRLMMR_WKUP_JTAGID_MFG_TI) {
102 		dev_err(dev, "Invalid MFG SoC\n");
103 		return -ENODEV;
104 	}
105 
106 	variant = (jtag_id & CTRLMMR_WKUP_JTAGID_VARIANT_MASK) >>
107 		  CTRLMMR_WKUP_JTAGID_VARIANT_SHIFT;
108 	variant++;
109 
110 	partno_id = (jtag_id & CTRLMMR_WKUP_JTAGID_PARTNO_MASK) >>
111 		 CTRLMMR_WKUP_JTAGID_PARTNO_SHIFT;
112 
113 	soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
114 	if (!soc_dev_attr)
115 		return -ENOMEM;
116 
117 	soc_dev_attr->revision = kasprintf(GFP_KERNEL, "SR%x.0", variant);
118 	if (!soc_dev_attr->revision) {
119 		ret = -ENOMEM;
120 		goto err;
121 	}
122 
123 	ret = k3_chipinfo_partno_to_names(partno_id, soc_dev_attr);
124 	if (ret) {
125 		dev_err(dev, "Unknown SoC JTAGID[0x%08X]\n", jtag_id);
126 		ret = -ENODEV;
127 		goto err_free_rev;
128 	}
129 
130 	node = of_find_node_by_path("/");
131 	of_property_read_string(node, "model", &soc_dev_attr->machine);
132 	of_node_put(node);
133 
134 	soc_dev = soc_device_register(soc_dev_attr);
135 	if (IS_ERR(soc_dev)) {
136 		ret = PTR_ERR(soc_dev);
137 		goto err_free_rev;
138 	}
139 
140 	dev_info(dev, "Family:%s rev:%s JTAGID[0x%08x] Detected\n",
141 		 soc_dev_attr->family,
142 		 soc_dev_attr->revision, jtag_id);
143 
144 	return 0;
145 
146 err_free_rev:
147 	kfree(soc_dev_attr->revision);
148 err:
149 	kfree(soc_dev_attr);
150 	return ret;
151 }
152 
153 static const struct of_device_id k3_chipinfo_of_match[] = {
154 	{ .compatible = "ti,am654-chipid", },
155 	{ /* sentinel */ },
156 };
157 
158 static struct platform_driver k3_chipinfo_driver = {
159 	.driver = {
160 		.name = "k3-chipinfo",
161 		.of_match_table = k3_chipinfo_of_match,
162 	},
163 	.probe = k3_chipinfo_probe,
164 };
165 
k3_chipinfo_init(void)166 static int __init k3_chipinfo_init(void)
167 {
168 	return platform_driver_register(&k3_chipinfo_driver);
169 }
170 subsys_initcall(k3_chipinfo_init);
171