2 * Copyright 2010 Benjamin Herrenschmidt, IBM Corp
3 * <benh@kernel.crashing.org>
4 * and David Gibson, IBM Corporation.
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.
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.
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
21 #include <linux/kernel.h>
22 #include <linux/debugfs.h>
23 #include <linux/slab.h>
24 #include <linux/export.h>
25 #include <asm/debug.h>
28 #include <asm/uaccess.h>
30 const struct scom_controller
*scom_controller
;
31 EXPORT_SYMBOL_GPL(scom_controller
);
33 struct device_node
*scom_find_parent(struct device_node
*node
)
35 struct device_node
*par
, *tmp
;
38 for (par
= of_node_get(node
); par
;) {
39 if (of_get_property(par
, "scom-controller", NULL
))
41 p
= of_get_property(par
, "scom-parent", NULL
);
44 par
= of_get_parent(par
);
46 par
= of_find_node_by_phandle(*p
);
51 EXPORT_SYMBOL_GPL(scom_find_parent
);
53 scom_map_t
scom_map_device(struct device_node
*dev
, int index
)
55 struct device_node
*parent
;
56 unsigned int cells
, size
;
57 const __be32
*prop
, *sprop
;
61 parent
= scom_find_parent(dev
);
67 * We support "scom-reg" properties for adding scom registers
68 * to a random device-tree node with an explicit scom-parent
70 * We also support the simple "reg" property if the device is
71 * a direct child of a scom controller.
73 * In case both exist, "scom-reg" takes precedence.
75 prop
= of_get_property(dev
, "scom-reg", &size
);
76 sprop
= of_get_property(parent
, "#scom-cells", NULL
);
77 if (!prop
&& parent
== dev
->parent
) {
78 prop
= of_get_property(dev
, "reg", &size
);
79 sprop
= of_get_property(parent
, "#address-cells", NULL
);
83 cells
= sprop
? be32_to_cpup(sprop
) : 1;
86 if (index
>= (size
/ (2*cells
)))
89 reg
= of_read_number(&prop
[index
* cells
* 2], cells
);
90 cnt
= of_read_number(&prop
[index
* cells
* 2 + cells
], cells
);
92 ret
= scom_map(parent
, reg
, cnt
);
97 EXPORT_SYMBOL_GPL(scom_map_device
);
99 #ifdef CONFIG_SCOM_DEBUGFS
100 struct scom_debug_entry
{
101 struct device_node
*dn
;
102 struct debugfs_blob_wrapper path
;
106 static ssize_t
scom_debug_read(struct file
*filp
, char __user
*ubuf
,
107 size_t count
, loff_t
*ppos
)
109 struct scom_debug_entry
*ent
= filp
->private_data
;
110 u64 __user
*ubuf64
= (u64 __user
*)ubuf
;
113 u64 reg
, reg_cnt
, val
;
117 if (off
< 0 || (off
& 7) || (count
& 7))
120 reg_cnt
= count
>> 3;
122 map
= scom_map(ent
->dn
, reg
, reg_cnt
);
123 if (!scom_map_ok(map
))
126 for (reg
= 0; reg
< reg_cnt
; reg
++) {
127 rc
= scom_read(map
, reg
, &val
);
129 rc
= put_user(val
, ubuf64
);
143 static ssize_t
scom_debug_write(struct file
* filp
, const char __user
*ubuf
,
144 size_t count
, loff_t
*ppos
)
146 struct scom_debug_entry
*ent
= filp
->private_data
;
147 u64 __user
*ubuf64
= (u64 __user
*)ubuf
;
150 u64 reg
, reg_cnt
, val
;
154 if (off
< 0 || (off
& 7) || (count
& 7))
157 reg_cnt
= count
>> 3;
159 map
= scom_map(ent
->dn
, reg
, reg_cnt
);
160 if (!scom_map_ok(map
))
163 for (reg
= 0; reg
< reg_cnt
; reg
++) {
164 rc
= get_user(val
, ubuf64
);
166 rc
= scom_write(map
, reg
, val
);
179 static const struct file_operations scom_debug_fops
= {
180 .read
= scom_debug_read
,
181 .write
= scom_debug_write
,
183 .llseek
= default_llseek
,
186 static int scom_debug_init_one(struct dentry
*root
, struct device_node
*dn
,
189 struct scom_debug_entry
*ent
;
192 ent
= kzalloc(sizeof(*ent
), GFP_KERNEL
);
196 ent
->dn
= of_node_get(dn
);
197 snprintf(ent
->name
, 16, "%08x", i
);
198 ent
->path
.data
= (void*) dn
->full_name
;
199 ent
->path
.size
= strlen(dn
->full_name
);
201 dir
= debugfs_create_dir(ent
->name
, root
);
208 debugfs_create_blob("devspec", 0400, dir
, &ent
->path
);
209 debugfs_create_file("access", 0600, dir
, ent
, &scom_debug_fops
);
214 static int scom_debug_init(void)
216 struct device_node
*dn
;
220 root
= debugfs_create_dir("scom", powerpc_debugfs_root
);
225 for_each_node_with_property(dn
, "scom-controller") {
226 int id
= of_get_ibm_chip_id(dn
);
229 rc
|= scom_debug_init_one(root
, dn
, id
);
235 device_initcall(scom_debug_init
);
236 #endif /* CONFIG_SCOM_DEBUGFS */