Linux Audio

Check our new training course

Loading...
v3.5.6
  1/*
  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>
 24#include <linux/export.h>
 25#include <asm/debug.h>
 26#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;
 56	const u32 *prop;
 57	u64 reg, cnt;
 58	scom_map_t ret;
 59
 60	parent = scom_find_parent(dev);
 61
 62	if (parent == NULL)
 63		return 0;
 64
 65	prop = of_get_property(parent, "#scom-cells", NULL);
 66	cells = prop ? *prop : 1;
 67
 
 
 
 
 
 
 68	prop = of_get_property(dev, "scom-reg", &size);
 
 
 
 
 
 69	if (!prop)
 70		return 0;
 
 71	size >>= 2;
 72
 73	if (index >= (size / (2*cells)))
 74		return 0;
 75
 76	reg = of_read_number(&prop[index * cells * 2], cells);
 77	cnt = of_read_number(&prop[index * cells * 2 + cells], cells);
 78
 79	ret = scom_map(parent, reg, cnt);
 80	of_node_put(parent);
 81
 82	return ret;
 83}
 84EXPORT_SYMBOL_GPL(scom_map_device);
 85
 86#ifdef CONFIG_SCOM_DEBUGFS
 87struct scom_debug_entry {
 88	struct device_node *dn;
 89	unsigned long addr;
 90	scom_map_t map;
 91	spinlock_t lock;
 92	char name[8];
 93	struct debugfs_blob_wrapper blob;
 94};
 95
 96static int scom_addr_set(void *data, u64 val)
 
 97{
 98	struct scom_debug_entry *ent = data;
 99
100	ent->addr = 0;
101	scom_unmap(ent->map);
102
103	ent->map = scom_map(ent->dn, val, 1);
104	if (scom_map_ok(ent->map))
105		ent->addr = val;
106	else
107		return -EFAULT;
108
109	return 0;
110}
111
112static int scom_addr_get(void *data, u64 *val)
113{
114	struct scom_debug_entry *ent = data;
115	*val = ent->addr;
116	return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117}
118DEFINE_SIMPLE_ATTRIBUTE(scom_addr_fops, scom_addr_get, scom_addr_set,
119			"0x%llx\n");
120
121static int scom_val_set(void *data, u64 val)
 
122{
123	struct scom_debug_entry *ent = data;
124
125	if (!scom_map_ok(ent->map))
126		return -EFAULT;
127
128	scom_write(ent->map, 0, val);
 
129
130	return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131}
132
133static int scom_val_get(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	*val = scom_read(ent->map, 0);
141	return 0;
142}
143DEFINE_SIMPLE_ATTRIBUTE(scom_val_fops, scom_val_get, scom_val_set,
144			"0x%llx\n");
145
146static int scom_debug_init_one(struct dentry *root, struct device_node *dn,
147			       int i)
148{
149	struct scom_debug_entry *ent;
150	struct dentry *dir;
151
152	ent = kzalloc(sizeof(*ent), GFP_KERNEL);
153	if (!ent)
154		return -ENOMEM;
155
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 = dn->full_name;
161	ent->blob.size = strlen(dn->full_name);
162
163	dir = debugfs_create_dir(ent->name, root);
164	if (!dir) {
165		of_node_put(dn);
166		kfree(ent);
167		return -1;
168	}
169
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);
173
174	return 0;
175}
176
177static int scom_debug_init(void)
178{
179	struct device_node *dn;
180	struct dentry *root;
181	int i, rc;
182
183	root = debugfs_create_dir("scom", powerpc_debugfs_root);
184	if (!root)
185		return -1;
186
187	i = rc = 0;
188	for_each_node_with_property(dn, "scom-controller")
189		rc |= scom_debug_init_one(root, dn, i++);
 
 
 
 
 
190
191	return rc;
192}
193device_initcall(scom_debug_init);
194#endif /* CONFIG_SCOM_DEBUGFS */
v4.10.11
  1/*
  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>
 24#include <linux/export.h>
 25#include <asm/debug.h>
 26#include <asm/prom.h>
 27#include <asm/scom.h>
 28#include <linux/uaccess.h>
 29
 30const struct scom_controller *scom_controller;
 31EXPORT_SYMBOL_GPL(scom_controller);
 32
 33struct device_node *scom_find_parent(struct device_node *node)
 34{
 35	struct device_node *par, *tmp;
 36	const u32 *p;
 37
 38	for (par = of_node_get(node); par;) {
 39		if (of_get_property(par, "scom-controller", NULL))
 40			break;
 41		p = of_get_property(par, "scom-parent", NULL);
 42		tmp = par;
 43		if (p == NULL)
 44			par = of_get_parent(par);
 45		else
 46			par = of_find_node_by_phandle(*p);
 47		of_node_put(tmp);
 48	}
 49	return par;
 50}
 51EXPORT_SYMBOL_GPL(scom_find_parent);
 52
 53scom_map_t scom_map_device(struct device_node *dev, int index)
 54{
 55	struct device_node *parent;
 56	unsigned int cells, size;
 57	const __be32 *prop, *sprop;
 58	u64 reg, cnt;
 59	scom_map_t ret;
 60
 61	parent = scom_find_parent(dev);
 62
 63	if (parent == NULL)
 64		return 0;
 65
 66	/*
 67	 * We support "scom-reg" properties for adding scom registers
 68	 * to a random device-tree node with an explicit scom-parent
 69	 *
 70	 * We also support the simple "reg" property if the device is
 71	 * a direct child of a scom controller.
 72	 *
 73	 * In case both exist, "scom-reg" takes precedence.
 74	 */
 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);
 80	}
 81	if (!prop)
 82		return NULL;
 83	cells = sprop ? be32_to_cpup(sprop) : 1;
 84	size >>= 2;
 85
 86	if (index >= (size / (2*cells)))
 87		return 0;
 88
 89	reg = of_read_number(&prop[index * cells * 2], cells);
 90	cnt = of_read_number(&prop[index * cells * 2 + cells], cells);
 91
 92	ret = scom_map(parent, reg, cnt);
 93	of_node_put(parent);
 94
 95	return ret;
 96}
 97EXPORT_SYMBOL_GPL(scom_map_device);
 98
 99#ifdef CONFIG_SCOM_DEBUGFS
100struct scom_debug_entry {
101	struct device_node *dn;
102	struct debugfs_blob_wrapper path;
103	char name[16];
 
 
 
104};
105
106static ssize_t scom_debug_read(struct file *filp, char __user *ubuf,
107			       size_t count, loff_t *ppos)
108{
109	struct scom_debug_entry *ent = filp->private_data;
110	u64 __user *ubuf64 = (u64 __user *)ubuf;
111	loff_t off = *ppos;
112	ssize_t done = 0; 
113	u64 reg, reg_cnt, val;
114	scom_map_t map;
115	int rc;
 
 
 
 
 
 
116
117	if (off < 0 || (off & 7) || (count & 7))
118		return -EINVAL;
119	reg = off >> 3;
120	reg_cnt = count >> 3;
121
122	map = scom_map(ent->dn, reg, reg_cnt);
123	if (!scom_map_ok(map))
124		return -ENXIO;
125
126	for (reg = 0; reg < reg_cnt; reg++) {
127		rc = scom_read(map, reg, &val);
128		if (!rc)
129			rc = put_user(val, ubuf64);
130		if (rc) {
131			if (!done)
132				done = rc;
133			break;
134		}
135		ubuf64++;
136		*ppos += 8;
137		done += 8;
138	}
139	scom_unmap(map);
140	return done;
141}
 
 
142
143static ssize_t scom_debug_write(struct file* filp, const char __user *ubuf,
144				size_t count, loff_t *ppos)
145{
146	struct scom_debug_entry *ent = filp->private_data;
147	u64 __user *ubuf64 = (u64 __user *)ubuf;
148	loff_t off = *ppos;
149	ssize_t done = 0; 
150	u64 reg, reg_cnt, val;
151	scom_map_t map;
152	int rc;
153
154	if (off < 0 || (off & 7) || (count & 7))
155		return -EINVAL;
156	reg = off >> 3;
157	reg_cnt = count >> 3;
158
159	map = scom_map(ent->dn, reg, reg_cnt);
160	if (!scom_map_ok(map))
161		return -ENXIO;
162
163	for (reg = 0; reg < reg_cnt; reg++) {
164		rc = get_user(val, ubuf64);
165		if (!rc)
166			rc = scom_write(map, reg,  val);
167		if (rc) {
168			if (!done)
169				done = rc;
170			break;
171		}
172		ubuf64++;
173		done += 8;
174	}
175	scom_unmap(map);
176	return done;
177}
178
179static const struct file_operations scom_debug_fops = {
180	.read =		scom_debug_read,
181	.write =	scom_debug_write,
182	.open =		simple_open,
183	.llseek =	default_llseek,
184};
 
 
 
 
 
 
185
186static int scom_debug_init_one(struct dentry *root, struct device_node *dn,
187			       int i)
188{
189	struct scom_debug_entry *ent;
190	struct dentry *dir;
191
192	ent = kzalloc(sizeof(*ent), GFP_KERNEL);
193	if (!ent)
194		return -ENOMEM;
195
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);
 
 
200
201	dir = debugfs_create_dir(ent->name, root);
202	if (!dir) {
203		of_node_put(dn);
204		kfree(ent);
205		return -1;
206	}
207
208	debugfs_create_blob("devspec", 0400, dir, &ent->path);
209	debugfs_create_file("access", 0600, dir, ent, &scom_debug_fops);
 
210
211	return 0;
212}
213
214static int scom_debug_init(void)
215{
216	struct device_node *dn;
217	struct dentry *root;
218	int i, rc;
219
220	root = debugfs_create_dir("scom", powerpc_debugfs_root);
221	if (!root)
222		return -1;
223
224	i = rc = 0;
225	for_each_node_with_property(dn, "scom-controller") {
226		int id = of_get_ibm_chip_id(dn);
227		if (id == -1)
228			id = i;
229		rc |= scom_debug_init_one(root, dn, id);
230		i++;
231	}
232
233	return rc;
234}
235device_initcall(scom_debug_init);
236#endif /* CONFIG_SCOM_DEBUGFS */