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>
29 const struct scom_controller
*scom_controller
;
30 EXPORT_SYMBOL_GPL(scom_controller
);
32 struct device_node
*scom_find_parent(struct device_node
*node
)
34 struct device_node
*par
, *tmp
;
37 for (par
= of_node_get(node
); par
;) {
38 if (of_get_property(par
, "scom-controller", NULL
))
40 p
= of_get_property(par
, "scom-parent", NULL
);
43 par
= of_get_parent(par
);
45 par
= of_find_node_by_phandle(*p
);
50 EXPORT_SYMBOL_GPL(scom_find_parent
);
52 scom_map_t
scom_map_device(struct device_node
*dev
, int index
)
54 struct device_node
*parent
;
55 unsigned int cells
, size
;
60 parent
= scom_find_parent(dev
);
65 prop
= of_get_property(parent
, "#scom-cells", NULL
);
66 cells
= prop
? *prop
: 1;
68 prop
= of_get_property(dev
, "scom-reg", &size
);
73 if (index
>= (size
/ (2*cells
)))
76 reg
= of_read_number(&prop
[index
* cells
* 2], cells
);
77 cnt
= of_read_number(&prop
[index
* cells
* 2 + cells
], cells
);
79 ret
= scom_map(parent
, reg
, cnt
);
84 EXPORT_SYMBOL_GPL(scom_map_device
);
86 #ifdef CONFIG_SCOM_DEBUGFS
87 struct scom_debug_entry
{
88 struct device_node
*dn
;
93 struct debugfs_blob_wrapper blob
;
96 static int scom_addr_set(void *data
, u64 val
)
98 struct scom_debug_entry
*ent
= data
;
101 scom_unmap(ent
->map
);
103 ent
->map
= scom_map(ent
->dn
, val
, 1);
104 if (scom_map_ok(ent
->map
))
112 static int scom_addr_get(void *data
, u64
*val
)
114 struct scom_debug_entry
*ent
= data
;
118 DEFINE_SIMPLE_ATTRIBUTE(scom_addr_fops
, scom_addr_get
, scom_addr_set
,
121 static int scom_val_set(void *data
, u64 val
)
123 struct scom_debug_entry
*ent
= data
;
125 if (!scom_map_ok(ent
->map
))
128 scom_write(ent
->map
, 0, val
);
133 static int scom_val_get(void *data
, u64
*val
)
135 struct scom_debug_entry
*ent
= data
;
137 if (!scom_map_ok(ent
->map
))
140 *val
= scom_read(ent
->map
, 0);
143 DEFINE_SIMPLE_ATTRIBUTE(scom_val_fops
, scom_val_get
, scom_val_set
,
146 static int scom_debug_init_one(struct dentry
*root
, struct device_node
*dn
,
149 struct scom_debug_entry
*ent
;
152 ent
= kzalloc(sizeof(*ent
), GFP_KERNEL
);
156 ent
->dn
= of_node_get(dn
);
157 ent
->map
= SCOM_MAP_INVALID
;
158 spin_lock_init(&ent
->lock
);
159 snprintf(ent
->name
, 8, "scom%d", i
);
160 ent
->blob
.data
= (void*) dn
->full_name
;
161 ent
->blob
.size
= strlen(dn
->full_name
);
163 dir
= debugfs_create_dir(ent
->name
, root
);
170 debugfs_create_file("addr", 0600, dir
, ent
, &scom_addr_fops
);
171 debugfs_create_file("value", 0600, dir
, ent
, &scom_val_fops
);
172 debugfs_create_blob("path", 0400, dir
, &ent
->blob
);
177 static int scom_debug_init(void)
179 struct device_node
*dn
;
183 root
= debugfs_create_dir("scom", powerpc_debugfs_root
);
188 for_each_node_with_property(dn
, "scom-controller")
189 rc
|= scom_debug_init_one(root
, dn
, i
++);
193 device_initcall(scom_debug_init
);
194 #endif /* CONFIG_SCOM_DEBUGFS */