18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright © 2014 NVIDIA Corporation
48c2ecf20Sopenharmony_ci * Copyright © 2015 Broadcom Corporation
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/io.h>
88c2ecf20Sopenharmony_ci#include <linux/of.h>
98c2ecf20Sopenharmony_ci#include <linux/of_address.h>
108c2ecf20Sopenharmony_ci#include <linux/slab.h>
118c2ecf20Sopenharmony_ci#include <linux/soc/brcmstb/brcmstb.h>
128c2ecf20Sopenharmony_ci#include <linux/sys_soc.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <soc/brcmstb/common.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_cistatic u32 family_id;
178c2ecf20Sopenharmony_cistatic u32 product_id;
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistatic const struct of_device_id brcmstb_machine_match[] = {
208c2ecf20Sopenharmony_ci	{ .compatible = "brcm,brcmstb", },
218c2ecf20Sopenharmony_ci	{ }
228c2ecf20Sopenharmony_ci};
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_cibool soc_is_brcmstb(void)
258c2ecf20Sopenharmony_ci{
268c2ecf20Sopenharmony_ci	const struct of_device_id *match;
278c2ecf20Sopenharmony_ci	struct device_node *root;
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci	root = of_find_node_by_path("/");
308c2ecf20Sopenharmony_ci	if (!root)
318c2ecf20Sopenharmony_ci		return false;
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci	match = of_match_node(brcmstb_machine_match, root);
348c2ecf20Sopenharmony_ci	of_node_put(root);
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci	return match != NULL;
378c2ecf20Sopenharmony_ci}
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ciu32 brcmstb_get_family_id(void)
408c2ecf20Sopenharmony_ci{
418c2ecf20Sopenharmony_ci	return family_id;
428c2ecf20Sopenharmony_ci}
438c2ecf20Sopenharmony_ciEXPORT_SYMBOL(brcmstb_get_family_id);
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ciu32 brcmstb_get_product_id(void)
468c2ecf20Sopenharmony_ci{
478c2ecf20Sopenharmony_ci	return product_id;
488c2ecf20Sopenharmony_ci}
498c2ecf20Sopenharmony_ciEXPORT_SYMBOL(brcmstb_get_product_id);
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistatic const struct of_device_id sun_top_ctrl_match[] = {
528c2ecf20Sopenharmony_ci	{ .compatible = "brcm,bcm7125-sun-top-ctrl", },
538c2ecf20Sopenharmony_ci	{ .compatible = "brcm,bcm7346-sun-top-ctrl", },
548c2ecf20Sopenharmony_ci	{ .compatible = "brcm,bcm7358-sun-top-ctrl", },
558c2ecf20Sopenharmony_ci	{ .compatible = "brcm,bcm7360-sun-top-ctrl", },
568c2ecf20Sopenharmony_ci	{ .compatible = "brcm,bcm7362-sun-top-ctrl", },
578c2ecf20Sopenharmony_ci	{ .compatible = "brcm,bcm7420-sun-top-ctrl", },
588c2ecf20Sopenharmony_ci	{ .compatible = "brcm,bcm7425-sun-top-ctrl", },
598c2ecf20Sopenharmony_ci	{ .compatible = "brcm,bcm7429-sun-top-ctrl", },
608c2ecf20Sopenharmony_ci	{ .compatible = "brcm,bcm7435-sun-top-ctrl", },
618c2ecf20Sopenharmony_ci	{ .compatible = "brcm,brcmstb-sun-top-ctrl", },
628c2ecf20Sopenharmony_ci	{ }
638c2ecf20Sopenharmony_ci};
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cistatic int __init brcmstb_soc_device_early_init(void)
668c2ecf20Sopenharmony_ci{
678c2ecf20Sopenharmony_ci	struct device_node *sun_top_ctrl;
688c2ecf20Sopenharmony_ci	void __iomem *sun_top_ctrl_base;
698c2ecf20Sopenharmony_ci	int ret = 0;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	/* We could be on a multi-platform kernel, don't make this fatal but
728c2ecf20Sopenharmony_ci	 * bail out early
738c2ecf20Sopenharmony_ci	 */
748c2ecf20Sopenharmony_ci	sun_top_ctrl = of_find_matching_node(NULL, sun_top_ctrl_match);
758c2ecf20Sopenharmony_ci	if (!sun_top_ctrl)
768c2ecf20Sopenharmony_ci		return ret;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	sun_top_ctrl_base = of_iomap(sun_top_ctrl, 0);
798c2ecf20Sopenharmony_ci	if (!sun_top_ctrl_base) {
808c2ecf20Sopenharmony_ci		ret = -ENODEV;
818c2ecf20Sopenharmony_ci		goto out;
828c2ecf20Sopenharmony_ci	}
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	family_id = readl(sun_top_ctrl_base);
858c2ecf20Sopenharmony_ci	product_id = readl(sun_top_ctrl_base + 0x4);
868c2ecf20Sopenharmony_ci	iounmap(sun_top_ctrl_base);
878c2ecf20Sopenharmony_ciout:
888c2ecf20Sopenharmony_ci	of_node_put(sun_top_ctrl);
898c2ecf20Sopenharmony_ci	return ret;
908c2ecf20Sopenharmony_ci}
918c2ecf20Sopenharmony_ciearly_initcall(brcmstb_soc_device_early_init);
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistatic int __init brcmstb_soc_device_init(void)
948c2ecf20Sopenharmony_ci{
958c2ecf20Sopenharmony_ci	struct soc_device_attribute *soc_dev_attr;
968c2ecf20Sopenharmony_ci	struct device_node *sun_top_ctrl;
978c2ecf20Sopenharmony_ci	struct soc_device *soc_dev;
988c2ecf20Sopenharmony_ci	int ret = 0;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	/* We could be on a multi-platform kernel, don't make this fatal but
1018c2ecf20Sopenharmony_ci	 * bail out early
1028c2ecf20Sopenharmony_ci	 */
1038c2ecf20Sopenharmony_ci	sun_top_ctrl = of_find_matching_node(NULL, sun_top_ctrl_match);
1048c2ecf20Sopenharmony_ci	if (!sun_top_ctrl)
1058c2ecf20Sopenharmony_ci		return ret;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
1088c2ecf20Sopenharmony_ci	if (!soc_dev_attr) {
1098c2ecf20Sopenharmony_ci		ret = -ENOMEM;
1108c2ecf20Sopenharmony_ci		goto out;
1118c2ecf20Sopenharmony_ci	}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	soc_dev_attr->family = kasprintf(GFP_KERNEL, "%x",
1148c2ecf20Sopenharmony_ci					 family_id >> 28 ?
1158c2ecf20Sopenharmony_ci					 family_id >> 16 : family_id >> 8);
1168c2ecf20Sopenharmony_ci	soc_dev_attr->soc_id = kasprintf(GFP_KERNEL, "%x",
1178c2ecf20Sopenharmony_ci					 product_id >> 28 ?
1188c2ecf20Sopenharmony_ci					 product_id >> 16 : product_id >> 8);
1198c2ecf20Sopenharmony_ci	soc_dev_attr->revision = kasprintf(GFP_KERNEL, "%c%d",
1208c2ecf20Sopenharmony_ci					 ((product_id & 0xf0) >> 4) + 'A',
1218c2ecf20Sopenharmony_ci					   product_id & 0xf);
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	soc_dev = soc_device_register(soc_dev_attr);
1248c2ecf20Sopenharmony_ci	if (IS_ERR(soc_dev)) {
1258c2ecf20Sopenharmony_ci		kfree(soc_dev_attr->family);
1268c2ecf20Sopenharmony_ci		kfree(soc_dev_attr->soc_id);
1278c2ecf20Sopenharmony_ci		kfree(soc_dev_attr->revision);
1288c2ecf20Sopenharmony_ci		kfree(soc_dev_attr);
1298c2ecf20Sopenharmony_ci		ret = -ENOMEM;
1308c2ecf20Sopenharmony_ci	}
1318c2ecf20Sopenharmony_ciout:
1328c2ecf20Sopenharmony_ci	of_node_put(sun_top_ctrl);
1338c2ecf20Sopenharmony_ci	return ret;
1348c2ecf20Sopenharmony_ci}
1358c2ecf20Sopenharmony_ciarch_initcall(brcmstb_soc_device_init);
136