1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2a2974c9cSLinus Walleij /*
3a2974c9cSLinus Walleij * Copyright (C) 2014 Linaro Ltd.
4a2974c9cSLinus Walleij *
5a2974c9cSLinus Walleij * Author: Linus Walleij <linus.walleij@linaro.org>
6a2974c9cSLinus Walleij */
7*c774f256SKrzysztof Kozlowski #include <linux/device.h>
8a2974c9cSLinus Walleij #include <linux/init.h>
9a2974c9cSLinus Walleij #include <linux/io.h>
10a2974c9cSLinus Walleij #include <linux/slab.h>
11a2974c9cSLinus Walleij #include <linux/sys_soc.h>
12a2974c9cSLinus Walleij #include <linux/platform_device.h>
13a2974c9cSLinus Walleij #include <linux/mfd/syscon.h>
14a2974c9cSLinus Walleij #include <linux/regmap.h>
15a2974c9cSLinus Walleij #include <linux/of.h>
16a2974c9cSLinus Walleij
17a2974c9cSLinus Walleij /* System ID in syscon */
18a2974c9cSLinus Walleij #define REALVIEW_SYS_ID_OFFSET 0x00
19a2974c9cSLinus Walleij
20a2974c9cSLinus Walleij static const struct of_device_id realview_soc_of_match[] = {
21a2974c9cSLinus Walleij { .compatible = "arm,realview-eb-soc", },
22a2974c9cSLinus Walleij { .compatible = "arm,realview-pb1176-soc", },
23a2974c9cSLinus Walleij { .compatible = "arm,realview-pb11mp-soc", },
24a2974c9cSLinus Walleij { .compatible = "arm,realview-pba8-soc", },
25a2974c9cSLinus Walleij { .compatible = "arm,realview-pbx-soc", },
26157a1b17SAxel Lin { }
27a2974c9cSLinus Walleij };
28a2974c9cSLinus Walleij
29a2974c9cSLinus Walleij static u32 realview_coreid;
30a2974c9cSLinus Walleij
realview_arch_str(u32 id)31a2974c9cSLinus Walleij static const char *realview_arch_str(u32 id)
32a2974c9cSLinus Walleij {
33a2974c9cSLinus Walleij switch ((id >> 8) & 0xf) {
345d87f7a3SLinus Walleij case 0x04:
355d87f7a3SLinus Walleij return "AHB";
36a2974c9cSLinus Walleij case 0x05:
37a2974c9cSLinus Walleij return "Multi-layer AXI";
38a2974c9cSLinus Walleij default:
39a2974c9cSLinus Walleij return "Unknown";
40a2974c9cSLinus Walleij }
41a2974c9cSLinus Walleij }
42a2974c9cSLinus Walleij
435feebc65SSudeep Holla static ssize_t
manufacturer_show(struct device * dev,struct device_attribute * attr,char * buf)445feebc65SSudeep Holla manufacturer_show(struct device *dev, struct device_attribute *attr, char *buf)
45a2974c9cSLinus Walleij {
46a2974c9cSLinus Walleij return sprintf(buf, "%02x\n", realview_coreid >> 24);
47a2974c9cSLinus Walleij }
48a2974c9cSLinus Walleij
495feebc65SSudeep Holla static DEVICE_ATTR_RO(manufacturer);
50a2974c9cSLinus Walleij
515feebc65SSudeep Holla static ssize_t
board_show(struct device * dev,struct device_attribute * attr,char * buf)525feebc65SSudeep Holla board_show(struct device *dev, struct device_attribute *attr, char *buf)
53a2974c9cSLinus Walleij {
54fccc2b36SLinus Walleij return sprintf(buf, "HBI-%03x\n", ((realview_coreid >> 16) & 0xfff));
55a2974c9cSLinus Walleij }
56a2974c9cSLinus Walleij
575feebc65SSudeep Holla static DEVICE_ATTR_RO(board);
58a2974c9cSLinus Walleij
595feebc65SSudeep Holla static ssize_t
fpga_show(struct device * dev,struct device_attribute * attr,char * buf)605feebc65SSudeep Holla fpga_show(struct device *dev, struct device_attribute *attr, char *buf)
61a2974c9cSLinus Walleij {
62a2974c9cSLinus Walleij return sprintf(buf, "%s\n", realview_arch_str(realview_coreid));
63a2974c9cSLinus Walleij }
64a2974c9cSLinus Walleij
655feebc65SSudeep Holla static DEVICE_ATTR_RO(fpga);
66a2974c9cSLinus Walleij
675feebc65SSudeep Holla static ssize_t
build_show(struct device * dev,struct device_attribute * attr,char * buf)685feebc65SSudeep Holla build_show(struct device *dev, struct device_attribute *attr, char *buf)
69a2974c9cSLinus Walleij {
70a2974c9cSLinus Walleij return sprintf(buf, "%02x\n", (realview_coreid & 0xFF));
71a2974c9cSLinus Walleij }
72a2974c9cSLinus Walleij
735feebc65SSudeep Holla static DEVICE_ATTR_RO(build);
74a2974c9cSLinus Walleij
7599d50b9bSSudeep Holla static struct attribute *realview_attrs[] = {
7699d50b9bSSudeep Holla &dev_attr_manufacturer.attr,
7799d50b9bSSudeep Holla &dev_attr_board.attr,
7899d50b9bSSudeep Holla &dev_attr_fpga.attr,
7999d50b9bSSudeep Holla &dev_attr_build.attr,
8099d50b9bSSudeep Holla NULL
8199d50b9bSSudeep Holla };
8299d50b9bSSudeep Holla
8399d50b9bSSudeep Holla ATTRIBUTE_GROUPS(realview);
8499d50b9bSSudeep Holla
realview_soc_socdev_release(void * data)85*c774f256SKrzysztof Kozlowski static void realview_soc_socdev_release(void *data)
86*c774f256SKrzysztof Kozlowski {
87*c774f256SKrzysztof Kozlowski struct soc_device *soc_dev = data;
88*c774f256SKrzysztof Kozlowski
89*c774f256SKrzysztof Kozlowski soc_device_unregister(soc_dev);
90*c774f256SKrzysztof Kozlowski }
91*c774f256SKrzysztof Kozlowski
realview_soc_probe(struct platform_device * pdev)92a2974c9cSLinus Walleij static int realview_soc_probe(struct platform_device *pdev)
93a2974c9cSLinus Walleij {
94db86ab06SGustavo A. R. Silva struct regmap *syscon_regmap;
95a2974c9cSLinus Walleij struct soc_device *soc_dev;
96a2974c9cSLinus Walleij struct soc_device_attribute *soc_dev_attr;
97a2974c9cSLinus Walleij struct device_node *np = pdev->dev.of_node;
98a2974c9cSLinus Walleij int ret;
99a2974c9cSLinus Walleij
100a2974c9cSLinus Walleij syscon_regmap = syscon_regmap_lookup_by_phandle(np, "regmap");
101a2974c9cSLinus Walleij if (IS_ERR(syscon_regmap))
102a2974c9cSLinus Walleij return PTR_ERR(syscon_regmap);
103a2974c9cSLinus Walleij
1041c4f26a4SKrzysztof Kozlowski soc_dev_attr = devm_kzalloc(&pdev->dev, sizeof(*soc_dev_attr), GFP_KERNEL);
105a2974c9cSLinus Walleij if (!soc_dev_attr)
106a2974c9cSLinus Walleij return -ENOMEM;
107a2974c9cSLinus Walleij
108a2974c9cSLinus Walleij ret = of_property_read_string(np, "compatible",
109a2974c9cSLinus Walleij &soc_dev_attr->soc_id);
110a2974c9cSLinus Walleij if (ret)
111a2974c9cSLinus Walleij return -EINVAL;
112a2974c9cSLinus Walleij
113a2974c9cSLinus Walleij soc_dev_attr->machine = "RealView";
114a2974c9cSLinus Walleij soc_dev_attr->family = "Versatile";
11599d50b9bSSudeep Holla soc_dev_attr->custom_attr_group = realview_groups[0];
116a2974c9cSLinus Walleij soc_dev = soc_device_register(soc_dev_attr);
1171c4f26a4SKrzysztof Kozlowski if (IS_ERR(soc_dev))
118a2974c9cSLinus Walleij return -ENODEV;
1191c4f26a4SKrzysztof Kozlowski
120*c774f256SKrzysztof Kozlowski ret = devm_add_action_or_reset(&pdev->dev, realview_soc_socdev_release,
121*c774f256SKrzysztof Kozlowski soc_dev);
122*c774f256SKrzysztof Kozlowski if (ret)
123*c774f256SKrzysztof Kozlowski return ret;
124*c774f256SKrzysztof Kozlowski
125a2974c9cSLinus Walleij ret = regmap_read(syscon_regmap, REALVIEW_SYS_ID_OFFSET,
126a2974c9cSLinus Walleij &realview_coreid);
127a2974c9cSLinus Walleij if (ret)
128a2974c9cSLinus Walleij return -ENODEV;
129a2974c9cSLinus Walleij
130fccc2b36SLinus Walleij dev_info(&pdev->dev, "RealView Syscon Core ID: 0x%08x, HBI-%03x\n",
131fccc2b36SLinus Walleij realview_coreid,
132fccc2b36SLinus Walleij ((realview_coreid >> 16) & 0xfff));
133a2974c9cSLinus Walleij /* FIXME: add attributes for SoC to sysfs */
134a2974c9cSLinus Walleij return 0;
135a2974c9cSLinus Walleij }
136a2974c9cSLinus Walleij
137a2974c9cSLinus Walleij static struct platform_driver realview_soc_driver = {
138a2974c9cSLinus Walleij .probe = realview_soc_probe,
139a2974c9cSLinus Walleij .driver = {
140a2974c9cSLinus Walleij .name = "realview-soc",
141a2974c9cSLinus Walleij .of_match_table = realview_soc_of_match,
142a2974c9cSLinus Walleij },
143a2974c9cSLinus Walleij };
1440159ae95SPaul Gortmaker builtin_platform_driver(realview_soc_driver);
145