xref: /openbmc/linux/drivers/soc/ti/k3-socinfo.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1907a2b7eSGrygorii Strashko // SPDX-License-Identifier: GPL-2.0
2907a2b7eSGrygorii Strashko /*
3907a2b7eSGrygorii Strashko  * TI K3 SoC info driver
4907a2b7eSGrygorii Strashko  *
5907a2b7eSGrygorii Strashko  * Copyright (C) 2020 Texas Instruments Incorporated - http://www.ti.com
6907a2b7eSGrygorii Strashko  */
7907a2b7eSGrygorii Strashko 
8907a2b7eSGrygorii Strashko #include <linux/mfd/syscon.h>
9907a2b7eSGrygorii Strashko #include <linux/of.h>
10907a2b7eSGrygorii Strashko #include <linux/of_address.h>
11907a2b7eSGrygorii Strashko #include <linux/regmap.h>
12907a2b7eSGrygorii Strashko #include <linux/platform_device.h>
13907a2b7eSGrygorii Strashko #include <linux/slab.h>
14907a2b7eSGrygorii Strashko #include <linux/string.h>
15907a2b7eSGrygorii Strashko #include <linux/sys_soc.h>
16907a2b7eSGrygorii Strashko 
17907a2b7eSGrygorii Strashko #define CTRLMMR_WKUP_JTAGID_REG		0
18907a2b7eSGrygorii Strashko /*
19907a2b7eSGrygorii Strashko  * Bits:
20907a2b7eSGrygorii Strashko  *  31-28 VARIANT	Device variant
21907a2b7eSGrygorii Strashko  *  27-12 PARTNO	Part number
22907a2b7eSGrygorii Strashko  *  11-1  MFG		Indicates TI as manufacturer (0x17)
23907a2b7eSGrygorii Strashko  *  1			Always 1
24907a2b7eSGrygorii Strashko  */
25907a2b7eSGrygorii Strashko #define CTRLMMR_WKUP_JTAGID_VARIANT_SHIFT	(28)
26907a2b7eSGrygorii Strashko #define CTRLMMR_WKUP_JTAGID_VARIANT_MASK	GENMASK(31, 28)
27907a2b7eSGrygorii Strashko 
28907a2b7eSGrygorii Strashko #define CTRLMMR_WKUP_JTAGID_PARTNO_SHIFT	(12)
29907a2b7eSGrygorii Strashko #define CTRLMMR_WKUP_JTAGID_PARTNO_MASK		GENMASK(27, 12)
30907a2b7eSGrygorii Strashko 
31907a2b7eSGrygorii Strashko #define CTRLMMR_WKUP_JTAGID_MFG_SHIFT		(1)
32907a2b7eSGrygorii Strashko #define CTRLMMR_WKUP_JTAGID_MFG_MASK		GENMASK(11, 1)
33907a2b7eSGrygorii Strashko 
34907a2b7eSGrygorii Strashko #define CTRLMMR_WKUP_JTAGID_MFG_TI		0x17
35907a2b7eSGrygorii Strashko 
36907a2b7eSGrygorii Strashko static const struct k3_soc_id {
37907a2b7eSGrygorii Strashko 	unsigned int id;
38907a2b7eSGrygorii Strashko 	const char *family_name;
39907a2b7eSGrygorii Strashko } k3_soc_ids[] = {
40907a2b7eSGrygorii Strashko 	{ 0xBB5A, "AM65X" },
41907a2b7eSGrygorii Strashko 	{ 0xBB64, "J721E" },
424f020441SPeter Ujfalusi 	{ 0xBB6D, "J7200" },
43a34ff76aSAswath Govindraju 	{ 0xBB38, "AM64X" },
44a34ff76aSAswath Govindraju 	{ 0xBB75, "J721S2"},
45c65d68e7SVignesh Raghavendra 	{ 0xBB7E, "AM62X" },
4600e34c94SSiddharth Vadapalli 	{ 0xBB80, "J784S4" },
47c11b537eSVignesh Raghavendra 	{ 0xBB8D, "AM62AX" },
48*443012ddSVignesh Raghavendra 	{ 0xBB9D, "AM62PX" },
49907a2b7eSGrygorii Strashko };
50907a2b7eSGrygorii Strashko 
51907a2b7eSGrygorii Strashko static int
k3_chipinfo_partno_to_names(unsigned int partno,struct soc_device_attribute * soc_dev_attr)52907a2b7eSGrygorii Strashko k3_chipinfo_partno_to_names(unsigned int partno,
53907a2b7eSGrygorii Strashko 			    struct soc_device_attribute *soc_dev_attr)
54907a2b7eSGrygorii Strashko {
55907a2b7eSGrygorii Strashko 	int i;
56907a2b7eSGrygorii Strashko 
57907a2b7eSGrygorii Strashko 	for (i = 0; i < ARRAY_SIZE(k3_soc_ids); i++)
58907a2b7eSGrygorii Strashko 		if (partno == k3_soc_ids[i].id) {
59907a2b7eSGrygorii Strashko 			soc_dev_attr->family = k3_soc_ids[i].family_name;
60907a2b7eSGrygorii Strashko 			return 0;
61907a2b7eSGrygorii Strashko 		}
62907a2b7eSGrygorii Strashko 
63907a2b7eSGrygorii Strashko 	return -EINVAL;
64907a2b7eSGrygorii Strashko }
65907a2b7eSGrygorii Strashko 
k3_chipinfo_probe(struct platform_device * pdev)66907a2b7eSGrygorii Strashko static int k3_chipinfo_probe(struct platform_device *pdev)
67907a2b7eSGrygorii Strashko {
68907a2b7eSGrygorii Strashko 	struct device_node *node = pdev->dev.of_node;
69907a2b7eSGrygorii Strashko 	struct soc_device_attribute *soc_dev_attr;
70907a2b7eSGrygorii Strashko 	struct device *dev = &pdev->dev;
71907a2b7eSGrygorii Strashko 	struct soc_device *soc_dev;
72907a2b7eSGrygorii Strashko 	struct regmap *regmap;
73907a2b7eSGrygorii Strashko 	u32 partno_id;
74907a2b7eSGrygorii Strashko 	u32 variant;
75907a2b7eSGrygorii Strashko 	u32 jtag_id;
76907a2b7eSGrygorii Strashko 	u32 mfg;
77907a2b7eSGrygorii Strashko 	int ret;
78907a2b7eSGrygorii Strashko 
79907a2b7eSGrygorii Strashko 	regmap = device_node_to_regmap(node);
80907a2b7eSGrygorii Strashko 	if (IS_ERR(regmap))
81907a2b7eSGrygorii Strashko 		return PTR_ERR(regmap);
82907a2b7eSGrygorii Strashko 
83907a2b7eSGrygorii Strashko 	ret = regmap_read(regmap, CTRLMMR_WKUP_JTAGID_REG, &jtag_id);
84907a2b7eSGrygorii Strashko 	if (ret < 0)
85907a2b7eSGrygorii Strashko 		return ret;
86907a2b7eSGrygorii Strashko 
87907a2b7eSGrygorii Strashko 	mfg = (jtag_id & CTRLMMR_WKUP_JTAGID_MFG_MASK) >>
88907a2b7eSGrygorii Strashko 	       CTRLMMR_WKUP_JTAGID_MFG_SHIFT;
89907a2b7eSGrygorii Strashko 
90907a2b7eSGrygorii Strashko 	if (mfg != CTRLMMR_WKUP_JTAGID_MFG_TI) {
91907a2b7eSGrygorii Strashko 		dev_err(dev, "Invalid MFG SoC\n");
92907a2b7eSGrygorii Strashko 		return -ENODEV;
93907a2b7eSGrygorii Strashko 	}
94907a2b7eSGrygorii Strashko 
95907a2b7eSGrygorii Strashko 	variant = (jtag_id & CTRLMMR_WKUP_JTAGID_VARIANT_MASK) >>
96907a2b7eSGrygorii Strashko 		  CTRLMMR_WKUP_JTAGID_VARIANT_SHIFT;
97907a2b7eSGrygorii Strashko 	variant++;
98907a2b7eSGrygorii Strashko 
99907a2b7eSGrygorii Strashko 	partno_id = (jtag_id & CTRLMMR_WKUP_JTAGID_PARTNO_MASK) >>
100907a2b7eSGrygorii Strashko 		 CTRLMMR_WKUP_JTAGID_PARTNO_SHIFT;
101907a2b7eSGrygorii Strashko 
102907a2b7eSGrygorii Strashko 	soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
103907a2b7eSGrygorii Strashko 	if (!soc_dev_attr)
104907a2b7eSGrygorii Strashko 		return -ENOMEM;
105907a2b7eSGrygorii Strashko 
106907a2b7eSGrygorii Strashko 	soc_dev_attr->revision = kasprintf(GFP_KERNEL, "SR%x.0", variant);
107907a2b7eSGrygorii Strashko 	if (!soc_dev_attr->revision) {
108907a2b7eSGrygorii Strashko 		ret = -ENOMEM;
109907a2b7eSGrygorii Strashko 		goto err;
110907a2b7eSGrygorii Strashko 	}
111907a2b7eSGrygorii Strashko 
112907a2b7eSGrygorii Strashko 	ret = k3_chipinfo_partno_to_names(partno_id, soc_dev_attr);
113907a2b7eSGrygorii Strashko 	if (ret) {
114907a2b7eSGrygorii Strashko 		dev_err(dev, "Unknown SoC JTAGID[0x%08X]\n", jtag_id);
115907a2b7eSGrygorii Strashko 		ret = -ENODEV;
116907a2b7eSGrygorii Strashko 		goto err_free_rev;
117907a2b7eSGrygorii Strashko 	}
118907a2b7eSGrygorii Strashko 
119907a2b7eSGrygorii Strashko 	node = of_find_node_by_path("/");
120907a2b7eSGrygorii Strashko 	of_property_read_string(node, "model", &soc_dev_attr->machine);
121907a2b7eSGrygorii Strashko 	of_node_put(node);
122907a2b7eSGrygorii Strashko 
123907a2b7eSGrygorii Strashko 	soc_dev = soc_device_register(soc_dev_attr);
124907a2b7eSGrygorii Strashko 	if (IS_ERR(soc_dev)) {
125907a2b7eSGrygorii Strashko 		ret = PTR_ERR(soc_dev);
126907a2b7eSGrygorii Strashko 		goto err_free_rev;
127907a2b7eSGrygorii Strashko 	}
128907a2b7eSGrygorii Strashko 
129907a2b7eSGrygorii Strashko 	dev_info(dev, "Family:%s rev:%s JTAGID[0x%08x] Detected\n",
130907a2b7eSGrygorii Strashko 		 soc_dev_attr->family,
131907a2b7eSGrygorii Strashko 		 soc_dev_attr->revision, jtag_id);
132907a2b7eSGrygorii Strashko 
133907a2b7eSGrygorii Strashko 	return 0;
134907a2b7eSGrygorii Strashko 
135907a2b7eSGrygorii Strashko err_free_rev:
136907a2b7eSGrygorii Strashko 	kfree(soc_dev_attr->revision);
137907a2b7eSGrygorii Strashko err:
138907a2b7eSGrygorii Strashko 	kfree(soc_dev_attr);
139907a2b7eSGrygorii Strashko 	return ret;
140907a2b7eSGrygorii Strashko }
141907a2b7eSGrygorii Strashko 
142907a2b7eSGrygorii Strashko static const struct of_device_id k3_chipinfo_of_match[] = {
143907a2b7eSGrygorii Strashko 	{ .compatible = "ti,am654-chipid", },
144907a2b7eSGrygorii Strashko 	{ /* sentinel */ },
145907a2b7eSGrygorii Strashko };
146907a2b7eSGrygorii Strashko 
147907a2b7eSGrygorii Strashko static struct platform_driver k3_chipinfo_driver = {
148907a2b7eSGrygorii Strashko 	.driver = {
149907a2b7eSGrygorii Strashko 		.name = "k3-chipinfo",
150907a2b7eSGrygorii Strashko 		.of_match_table = k3_chipinfo_of_match,
151907a2b7eSGrygorii Strashko 	},
152907a2b7eSGrygorii Strashko 	.probe = k3_chipinfo_probe,
153907a2b7eSGrygorii Strashko };
154907a2b7eSGrygorii Strashko 
k3_chipinfo_init(void)155907a2b7eSGrygorii Strashko static int __init k3_chipinfo_init(void)
156907a2b7eSGrygorii Strashko {
157907a2b7eSGrygorii Strashko 	return platform_driver_register(&k3_chipinfo_driver);
158907a2b7eSGrygorii Strashko }
159907a2b7eSGrygorii Strashko subsys_initcall(k3_chipinfo_init);
160