1*1802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
234642650SFlorian Fainelli /*
334642650SFlorian Fainelli * Copyright © 2014 NVIDIA Corporation
434642650SFlorian Fainelli * Copyright © 2015 Broadcom Corporation
534642650SFlorian Fainelli */
634642650SFlorian Fainelli
734642650SFlorian Fainelli #include <linux/io.h>
834642650SFlorian Fainelli #include <linux/of.h>
934642650SFlorian Fainelli #include <linux/of_address.h>
1034642650SFlorian Fainelli #include <linux/slab.h>
1134642650SFlorian Fainelli #include <linux/soc/brcmstb/brcmstb.h>
1234642650SFlorian Fainelli #include <linux/sys_soc.h>
1334642650SFlorian Fainelli
1434642650SFlorian Fainelli static u32 family_id;
1534642650SFlorian Fainelli static u32 product_id;
1634642650SFlorian Fainelli
brcmstb_get_family_id(void)177f20333fSAl Cooper u32 brcmstb_get_family_id(void)
187f20333fSAl Cooper {
197f20333fSAl Cooper return family_id;
207f20333fSAl Cooper }
217f20333fSAl Cooper EXPORT_SYMBOL(brcmstb_get_family_id);
227f20333fSAl Cooper
brcmstb_get_product_id(void)237f20333fSAl Cooper u32 brcmstb_get_product_id(void)
247f20333fSAl Cooper {
257f20333fSAl Cooper return product_id;
267f20333fSAl Cooper }
277f20333fSAl Cooper EXPORT_SYMBOL(brcmstb_get_product_id);
287f20333fSAl Cooper
2934642650SFlorian Fainelli static const struct of_device_id sun_top_ctrl_match[] = {
30bd0faf08SFlorian Fainelli { .compatible = "brcm,bcm7125-sun-top-ctrl", },
31bd0faf08SFlorian Fainelli { .compatible = "brcm,bcm7346-sun-top-ctrl", },
32bd0faf08SFlorian Fainelli { .compatible = "brcm,bcm7358-sun-top-ctrl", },
33bd0faf08SFlorian Fainelli { .compatible = "brcm,bcm7360-sun-top-ctrl", },
34bd0faf08SFlorian Fainelli { .compatible = "brcm,bcm7362-sun-top-ctrl", },
35bd0faf08SFlorian Fainelli { .compatible = "brcm,bcm7420-sun-top-ctrl", },
36bd0faf08SFlorian Fainelli { .compatible = "brcm,bcm7425-sun-top-ctrl", },
37bd0faf08SFlorian Fainelli { .compatible = "brcm,bcm7429-sun-top-ctrl", },
3831d848aaSFlorian Fainelli { .compatible = "brcm,bcm7435-sun-top-ctrl", },
3934642650SFlorian Fainelli { .compatible = "brcm,brcmstb-sun-top-ctrl", },
4034642650SFlorian Fainelli { }
4134642650SFlorian Fainelli };
4234642650SFlorian Fainelli
brcmstb_soc_device_early_init(void)435d4567ecSFlorian Fainelli static int __init brcmstb_soc_device_early_init(void)
4434642650SFlorian Fainelli {
4534642650SFlorian Fainelli struct device_node *sun_top_ctrl;
4634642650SFlorian Fainelli void __iomem *sun_top_ctrl_base;
47c5b40c31SThierry Reding int ret = 0;
4834642650SFlorian Fainelli
49c5b40c31SThierry Reding /* We could be on a multi-platform kernel, don't make this fatal but
50c5b40c31SThierry Reding * bail out early
51c5b40c31SThierry Reding */
5234642650SFlorian Fainelli sun_top_ctrl = of_find_matching_node(NULL, sun_top_ctrl_match);
5334642650SFlorian Fainelli if (!sun_top_ctrl)
54c5b40c31SThierry Reding return ret;
5534642650SFlorian Fainelli
5634642650SFlorian Fainelli sun_top_ctrl_base = of_iomap(sun_top_ctrl, 0);
57c5b40c31SThierry Reding if (!sun_top_ctrl_base) {
58c5b40c31SThierry Reding ret = -ENODEV;
59c5b40c31SThierry Reding goto out;
60c5b40c31SThierry Reding }
6134642650SFlorian Fainelli
6234642650SFlorian Fainelli family_id = readl(sun_top_ctrl_base);
6334642650SFlorian Fainelli product_id = readl(sun_top_ctrl_base + 0x4);
645d4567ecSFlorian Fainelli iounmap(sun_top_ctrl_base);
65c5b40c31SThierry Reding out:
66c5b40c31SThierry Reding of_node_put(sun_top_ctrl);
67c5b40c31SThierry Reding return ret;
685d4567ecSFlorian Fainelli }
695d4567ecSFlorian Fainelli early_initcall(brcmstb_soc_device_early_init);
705d4567ecSFlorian Fainelli
brcmstb_soc_device_init(void)715d4567ecSFlorian Fainelli static int __init brcmstb_soc_device_init(void)
725d4567ecSFlorian Fainelli {
735d4567ecSFlorian Fainelli struct soc_device_attribute *soc_dev_attr;
74c5b40c31SThierry Reding struct device_node *sun_top_ctrl;
755d4567ecSFlorian Fainelli struct soc_device *soc_dev;
76c5b40c31SThierry Reding int ret = 0;
77c5b40c31SThierry Reding
78c5b40c31SThierry Reding /* We could be on a multi-platform kernel, don't make this fatal but
79c5b40c31SThierry Reding * bail out early
80c5b40c31SThierry Reding */
81c5b40c31SThierry Reding sun_top_ctrl = of_find_matching_node(NULL, sun_top_ctrl_match);
82c5b40c31SThierry Reding if (!sun_top_ctrl)
83c5b40c31SThierry Reding return ret;
8434642650SFlorian Fainelli
8534642650SFlorian Fainelli soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
86c5b40c31SThierry Reding if (!soc_dev_attr) {
87c5b40c31SThierry Reding ret = -ENOMEM;
88c5b40c31SThierry Reding goto out;
89c5b40c31SThierry Reding }
9034642650SFlorian Fainelli
9134642650SFlorian Fainelli soc_dev_attr->family = kasprintf(GFP_KERNEL, "%x",
9234642650SFlorian Fainelli family_id >> 28 ?
9334642650SFlorian Fainelli family_id >> 16 : family_id >> 8);
9434642650SFlorian Fainelli soc_dev_attr->soc_id = kasprintf(GFP_KERNEL, "%x",
9534642650SFlorian Fainelli product_id >> 28 ?
9634642650SFlorian Fainelli product_id >> 16 : product_id >> 8);
9734642650SFlorian Fainelli soc_dev_attr->revision = kasprintf(GFP_KERNEL, "%c%d",
9834642650SFlorian Fainelli ((product_id & 0xf0) >> 4) + 'A',
9934642650SFlorian Fainelli product_id & 0xf);
10034642650SFlorian Fainelli
10134642650SFlorian Fainelli soc_dev = soc_device_register(soc_dev_attr);
10234642650SFlorian Fainelli if (IS_ERR(soc_dev)) {
10334642650SFlorian Fainelli kfree(soc_dev_attr->family);
10434642650SFlorian Fainelli kfree(soc_dev_attr->soc_id);
10534642650SFlorian Fainelli kfree(soc_dev_attr->revision);
10634642650SFlorian Fainelli kfree(soc_dev_attr);
107c5b40c31SThierry Reding ret = -ENOMEM;
10834642650SFlorian Fainelli }
109c5b40c31SThierry Reding out:
110c5b40c31SThierry Reding of_node_put(sun_top_ctrl);
111c5b40c31SThierry Reding return ret;
11234642650SFlorian Fainelli }
11334642650SFlorian Fainelli arch_initcall(brcmstb_soc_device_init);
114