1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2010 Benjamin Herrenschmidt, IBM Corp
4 * <benh@kernel.crashing.org>
5 * and David Gibson, IBM Corporation.
8 #include <linux/kernel.h>
9 #include <linux/slab.h>
10 #include <linux/export.h>
11 #include <asm/debugfs.h>
14 #include <linux/uaccess.h>
16 const struct scom_controller *scom_controller;
17 EXPORT_SYMBOL_GPL(scom_controller);
19 struct device_node *scom_find_parent(struct device_node *node)
21 struct device_node *par, *tmp;
24 for (par = of_node_get(node); par;) {
25 if (of_get_property(par, "scom-controller", NULL))
27 p = of_get_property(par, "scom-parent", NULL);
30 par = of_get_parent(par);
32 par = of_find_node_by_phandle(*p);
37 EXPORT_SYMBOL_GPL(scom_find_parent);
39 scom_map_t scom_map_device(struct device_node *dev, int index)
41 struct device_node *parent;
42 unsigned int cells, size;
43 const __be32 *prop, *sprop;
47 parent = scom_find_parent(dev);
53 * We support "scom-reg" properties for adding scom registers
54 * to a random device-tree node with an explicit scom-parent
56 * We also support the simple "reg" property if the device is
57 * a direct child of a scom controller.
59 * In case both exist, "scom-reg" takes precedence.
61 prop = of_get_property(dev, "scom-reg", &size);
62 sprop = of_get_property(parent, "#scom-cells", NULL);
63 if (!prop && parent == dev->parent) {
64 prop = of_get_property(dev, "reg", &size);
65 sprop = of_get_property(parent, "#address-cells", NULL);
69 cells = sprop ? be32_to_cpup(sprop) : 1;
72 if (index >= (size / (2*cells)))
75 reg = of_read_number(&prop[index * cells * 2], cells);
76 cnt = of_read_number(&prop[index * cells * 2 + cells], cells);
78 ret = scom_map(parent, reg, cnt);
83 EXPORT_SYMBOL_GPL(scom_map_device);
85 #ifdef CONFIG_SCOM_DEBUGFS
86 struct scom_debug_entry {
87 struct device_node *dn;
88 struct debugfs_blob_wrapper path;
92 static ssize_t scom_debug_read(struct file *filp, char __user *ubuf,
93 size_t count, loff_t *ppos)
95 struct scom_debug_entry *ent = filp->private_data;
96 u64 __user *ubuf64 = (u64 __user *)ubuf;
99 u64 reg, reg_cnt, val;
103 if (off < 0 || (off & 7) || (count & 7))
106 reg_cnt = count >> 3;
108 map = scom_map(ent->dn, reg, reg_cnt);
109 if (!scom_map_ok(map))
112 for (reg = 0; reg < reg_cnt; reg++) {
113 rc = scom_read(map, reg, &val);
115 rc = put_user(val, ubuf64);
129 static ssize_t scom_debug_write(struct file* filp, const char __user *ubuf,
130 size_t count, loff_t *ppos)
132 struct scom_debug_entry *ent = filp->private_data;
133 u64 __user *ubuf64 = (u64 __user *)ubuf;
136 u64 reg, reg_cnt, val;
140 if (off < 0 || (off & 7) || (count & 7))
143 reg_cnt = count >> 3;
145 map = scom_map(ent->dn, reg, reg_cnt);
146 if (!scom_map_ok(map))
149 for (reg = 0; reg < reg_cnt; reg++) {
150 rc = get_user(val, ubuf64);
152 rc = scom_write(map, reg, val);
165 static const struct file_operations scom_debug_fops = {
166 .read = scom_debug_read,
167 .write = scom_debug_write,
169 .llseek = default_llseek,
172 static int scom_debug_init_one(struct dentry *root, struct device_node *dn,
175 struct scom_debug_entry *ent;
178 ent = kzalloc(sizeof(*ent), GFP_KERNEL);
182 ent->dn = of_node_get(dn);
183 snprintf(ent->name, 16, "%08x", i);
184 ent->path.data = (void*)kasprintf(GFP_KERNEL, "%pOF", dn);
185 ent->path.size = strlen((char *)ent->path.data);
187 dir = debugfs_create_dir(ent->name, root);
190 kfree(ent->path.data);
195 debugfs_create_blob("devspec", 0400, dir, &ent->path);
196 debugfs_create_file("access", 0600, dir, ent, &scom_debug_fops);
201 static int scom_debug_init(void)
203 struct device_node *dn;
207 root = debugfs_create_dir("scom", powerpc_debugfs_root);
212 for_each_node_with_property(dn, "scom-controller") {
213 int id = of_get_ibm_chip_id(dn);
216 rc |= scom_debug_init_one(root, dn, id);
222 device_initcall(scom_debug_init);
223 #endif /* CONFIG_SCOM_DEBUGFS */