blob: 3963d995648a551e0dbe355833ff3b204d6980e7 [file] [log] [blame]
Benjamin Herrenschmidtab814b92011-04-14 22:31:58 +00001/*
2 * Copyright 2010 Benjamin Herrenschmidt, IBM Corp
3 * <benh@kernel.crashing.org>
4 * and David Gibson, IBM Corporation.
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 as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14 * the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include <linux/kernel.h>
22#include <linux/debugfs.h>
23#include <linux/slab.h>
Paul Gortmaker66b15db2011-05-27 10:46:24 -040024#include <linux/export.h>
Paul Gortmaker1bc68a92012-04-16 00:52:51 -040025#include <asm/debug.h>
Benjamin Herrenschmidtab814b92011-04-14 22:31:58 +000026#include <asm/prom.h>
27#include <asm/scom.h>
28
29const struct scom_controller *scom_controller;
30EXPORT_SYMBOL_GPL(scom_controller);
31
32struct device_node *scom_find_parent(struct device_node *node)
33{
34 struct device_node *par, *tmp;
35 const u32 *p;
36
37 for (par = of_node_get(node); par;) {
38 if (of_get_property(par, "scom-controller", NULL))
39 break;
40 p = of_get_property(par, "scom-parent", NULL);
41 tmp = par;
42 if (p == NULL)
43 par = of_get_parent(par);
44 else
45 par = of_find_node_by_phandle(*p);
46 of_node_put(tmp);
47 }
48 return par;
49}
50EXPORT_SYMBOL_GPL(scom_find_parent);
51
52scom_map_t scom_map_device(struct device_node *dev, int index)
53{
54 struct device_node *parent;
55 unsigned int cells, size;
Benjamin Herrenschmidt5f33af42013-08-29 16:56:16 +100056 const __be32 *prop, *sprop;
Benjamin Herrenschmidtab814b92011-04-14 22:31:58 +000057 u64 reg, cnt;
58 scom_map_t ret;
59
60 parent = scom_find_parent(dev);
61
62 if (parent == NULL)
63 return 0;
64
Benjamin Herrenschmidt5f33af42013-08-29 16:56:16 +100065 /*
66 * We support "scom-reg" properties for adding scom registers
67 * to a random device-tree node with an explicit scom-parent
68 *
69 * We also support the simple "reg" property if the device is
70 * a direct child of a scom controller.
71 *
72 * In case both exist, "scom-reg" takes precedence.
73 */
Benjamin Herrenschmidtab814b92011-04-14 22:31:58 +000074 prop = of_get_property(dev, "scom-reg", &size);
Benjamin Herrenschmidt5f33af42013-08-29 16:56:16 +100075 sprop = of_get_property(parent, "#scom-cells", NULL);
76 if (!prop && parent == dev->parent) {
77 prop = of_get_property(dev, "reg", &size);
78 sprop = of_get_property(parent, "#address-cells", NULL);
79 }
Benjamin Herrenschmidtab814b92011-04-14 22:31:58 +000080 if (!prop)
Benjamin Herrenschmidt5f33af42013-08-29 16:56:16 +100081 return NULL;
82 cells = sprop ? be32_to_cpup(sprop) : 1;
Benjamin Herrenschmidtab814b92011-04-14 22:31:58 +000083 size >>= 2;
84
85 if (index >= (size / (2*cells)))
86 return 0;
87
88 reg = of_read_number(&prop[index * cells * 2], cells);
89 cnt = of_read_number(&prop[index * cells * 2 + cells], cells);
90
91 ret = scom_map(parent, reg, cnt);
92 of_node_put(parent);
93
94 return ret;
95}
96EXPORT_SYMBOL_GPL(scom_map_device);
97
98#ifdef CONFIG_SCOM_DEBUGFS
99struct scom_debug_entry {
100 struct device_node *dn;
101 unsigned long addr;
102 scom_map_t map;
103 spinlock_t lock;
104 char name[8];
105 struct debugfs_blob_wrapper blob;
106};
107
108static int scom_addr_set(void *data, u64 val)
109{
110 struct scom_debug_entry *ent = data;
111
112 ent->addr = 0;
113 scom_unmap(ent->map);
114
115 ent->map = scom_map(ent->dn, val, 1);
116 if (scom_map_ok(ent->map))
117 ent->addr = val;
118 else
119 return -EFAULT;
120
121 return 0;
122}
123
124static int scom_addr_get(void *data, u64 *val)
125{
126 struct scom_debug_entry *ent = data;
127 *val = ent->addr;
128 return 0;
129}
130DEFINE_SIMPLE_ATTRIBUTE(scom_addr_fops, scom_addr_get, scom_addr_set,
131 "0x%llx\n");
132
133static int scom_val_set(void *data, u64 val)
134{
135 struct scom_debug_entry *ent = data;
136
137 if (!scom_map_ok(ent->map))
138 return -EFAULT;
139
140 scom_write(ent->map, 0, val);
141
142 return 0;
143}
144
145static int scom_val_get(void *data, u64 *val)
146{
147 struct scom_debug_entry *ent = data;
148
149 if (!scom_map_ok(ent->map))
150 return -EFAULT;
151
Benjamin Herrenschmidtaaa63092013-08-29 16:55:45 +1000152 return scom_read(ent->map, 0, val);
Benjamin Herrenschmidtab814b92011-04-14 22:31:58 +0000153}
154DEFINE_SIMPLE_ATTRIBUTE(scom_val_fops, scom_val_get, scom_val_set,
155 "0x%llx\n");
156
157static int scom_debug_init_one(struct dentry *root, struct device_node *dn,
158 int i)
159{
160 struct scom_debug_entry *ent;
161 struct dentry *dir;
162
163 ent = kzalloc(sizeof(*ent), GFP_KERNEL);
164 if (!ent)
165 return -ENOMEM;
166
167 ent->dn = of_node_get(dn);
168 ent->map = SCOM_MAP_INVALID;
169 spin_lock_init(&ent->lock);
170 snprintf(ent->name, 8, "scom%d", i);
Grant Likely499b42c2012-11-29 16:35:41 +0000171 ent->blob.data = (void*) dn->full_name;
Benjamin Herrenschmidtab814b92011-04-14 22:31:58 +0000172 ent->blob.size = strlen(dn->full_name);
173
174 dir = debugfs_create_dir(ent->name, root);
175 if (!dir) {
176 of_node_put(dn);
177 kfree(ent);
178 return -1;
179 }
180
181 debugfs_create_file("addr", 0600, dir, ent, &scom_addr_fops);
182 debugfs_create_file("value", 0600, dir, ent, &scom_val_fops);
Benjamin Herrenschmidt81fafea2013-08-29 17:25:35 +1000183 debugfs_create_blob("devspec", 0400, dir, &ent->blob);
Benjamin Herrenschmidtab814b92011-04-14 22:31:58 +0000184
185 return 0;
186}
187
188static int scom_debug_init(void)
189{
190 struct device_node *dn;
191 struct dentry *root;
192 int i, rc;
193
194 root = debugfs_create_dir("scom", powerpc_debugfs_root);
195 if (!root)
196 return -1;
197
198 i = rc = 0;
Benjamin Herrenschmidt762fd3a2013-08-29 16:56:59 +1000199 for_each_node_with_property(dn, "scom-controller") {
200 int id = of_get_ibm_chip_id(dn);
201 if (id == -1)
202 id = i;
203 rc |= scom_debug_init_one(root, dn, id);
204 i++;
205 }
Benjamin Herrenschmidtab814b92011-04-14 22:31:58 +0000206
207 return rc;
208}
209device_initcall(scom_debug_init);
210#endif /* CONFIG_SCOM_DEBUGFS */