blob: 9cddbde3998609ca30d9af19afcf78a6d3bbb5e6 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) 2014 Linaro Ltd.
4 *
5 * Author: Linus Walleij <linus.walleij@linaro.org>
6 */
7#include <linux/device.h>
8#include <linux/init.h>
9#include <linux/io.h>
10#include <linux/slab.h>
11#include <linux/sys_soc.h>
12#include <linux/platform_device.h>
13#include <linux/mfd/syscon.h>
14#include <linux/regmap.h>
15#include <linux/of.h>
16
17/* System ID in syscon */
18#define REALVIEW_SYS_ID_OFFSET 0x00
19
20static const struct of_device_id realview_soc_of_match[] = {
21 { .compatible = "arm,realview-eb-soc", },
22 { .compatible = "arm,realview-pb1176-soc", },
23 { .compatible = "arm,realview-pb11mp-soc", },
24 { .compatible = "arm,realview-pba8-soc", },
25 { .compatible = "arm,realview-pbx-soc", },
26 { }
27};
28
29static u32 realview_coreid;
30
31static const char *realview_arch_str(u32 id)
32{
33 switch ((id >> 8) & 0xf) {
34 case 0x04:
35 return "AHB";
36 case 0x05:
37 return "Multi-layer AXI";
38 default:
39 return "Unknown";
40 }
41}
42
43static ssize_t realview_get_manf(struct device *dev,
44 struct device_attribute *attr,
45 char *buf)
46{
47 return sprintf(buf, "%02x\n", realview_coreid >> 24);
48}
49
50static struct device_attribute realview_manf_attr =
51 __ATTR(manufacturer, S_IRUGO, realview_get_manf, NULL);
52
53static ssize_t realview_get_board(struct device *dev,
54 struct device_attribute *attr,
55 char *buf)
56{
57 return sprintf(buf, "HBI-%03x\n", ((realview_coreid >> 16) & 0xfff));
58}
59
60static struct device_attribute realview_board_attr =
61 __ATTR(board, S_IRUGO, realview_get_board, NULL);
62
63static ssize_t realview_get_arch(struct device *dev,
64 struct device_attribute *attr,
65 char *buf)
66{
67 return sprintf(buf, "%s\n", realview_arch_str(realview_coreid));
68}
69
70static struct device_attribute realview_arch_attr =
71 __ATTR(fpga, S_IRUGO, realview_get_arch, NULL);
72
73static ssize_t realview_get_build(struct device *dev,
74 struct device_attribute *attr,
75 char *buf)
76{
77 return sprintf(buf, "%02x\n", (realview_coreid & 0xFF));
78}
79
80static struct device_attribute realview_build_attr =
81 __ATTR(build, S_IRUGO, realview_get_build, NULL);
82
83static void realview_soc_socdev_release(void *data)
84{
85 struct soc_device *soc_dev = data;
86
87 soc_device_unregister(soc_dev);
88}
89
90static int realview_soc_probe(struct platform_device *pdev)
91{
92 struct regmap *syscon_regmap;
93 struct soc_device *soc_dev;
94 struct soc_device_attribute *soc_dev_attr;
95 struct device_node *np = pdev->dev.of_node;
96 int ret;
97
98 syscon_regmap = syscon_regmap_lookup_by_phandle(np, "regmap");
99 if (IS_ERR(syscon_regmap))
100 return PTR_ERR(syscon_regmap);
101
102 soc_dev_attr = devm_kzalloc(&pdev->dev, sizeof(*soc_dev_attr), GFP_KERNEL);
103 if (!soc_dev_attr)
104 return -ENOMEM;
105
106 ret = of_property_read_string(np, "compatible",
107 &soc_dev_attr->soc_id);
108 if (ret)
109 return -EINVAL;
110
111 soc_dev_attr->machine = "RealView";
112 soc_dev_attr->family = "Versatile";
113 soc_dev = soc_device_register(soc_dev_attr);
114 if (IS_ERR(soc_dev))
115 return -ENODEV;
116
117 ret = devm_add_action_or_reset(&pdev->dev, realview_soc_socdev_release,
118 soc_dev);
119 if (ret)
120 return ret;
121
122 ret = regmap_read(syscon_regmap, REALVIEW_SYS_ID_OFFSET,
123 &realview_coreid);
124 if (ret)
125 return -ENODEV;
126
127 device_create_file(soc_device_to_device(soc_dev), &realview_manf_attr);
128 device_create_file(soc_device_to_device(soc_dev), &realview_board_attr);
129 device_create_file(soc_device_to_device(soc_dev), &realview_arch_attr);
130 device_create_file(soc_device_to_device(soc_dev), &realview_build_attr);
131
132 dev_info(&pdev->dev, "RealView Syscon Core ID: 0x%08x, HBI-%03x\n",
133 realview_coreid,
134 ((realview_coreid >> 16) & 0xfff));
135 /* FIXME: add attributes for SoC to sysfs */
136 return 0;
137}
138
139static struct platform_driver realview_soc_driver = {
140 .probe = realview_soc_probe,
141 .driver = {
142 .name = "realview-soc",
143 .of_match_table = realview_soc_of_match,
144 },
145};
146builtin_platform_driver(realview_soc_driver);