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