summaryrefslogtreecommitdiff
path: root/arch/arm/mach-omap2/omap_hwmod_debug.c
blob: 603f05dd37aa3e5c25dab9edb3410a3b0bc334d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
 * omap_hwmod debugfs implementation
 *
 * Copyright (C) 2010 Texas Instruments, Inc.
 *
 * Benoit Cousson
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * Expose a debufs interface in order to check and modify hwmod state
 * The current directory structure is:
 *
 * hwmods
 *  +-hwmod_mpu
 *  +-hwmod_dsp
 *  .
 *  .
 *  +-hwmod_xxx   : hwmod node
 *     +-state    : internal state (RO)
 *     +-summary  : global view of hwmod definition
 *     +-resets   : reset lines
 *        +-rst1  : reset state / control (RW)
 *        +-rst2  :
 *
 * To do:
 * - Add irq / dma dump
 * - Add clock dump
 */
#undef DEBUG

#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/debugfs.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/seq_file.h>
#include <linux/slab.h>

#include <plat/clock.h>
#include <plat/common.h>
#include <plat/omap_hwmod.h>


/*
 * DEBUGFS helper macros
 *
 * This code is already used in several omap drivers, so eventually it will be
 * good to move that to plat-omap and share the same code.
 */

#define DEFINE_DEBUGFS_SHOW(__fops, __show)				\
static int __fops ## _open(struct inode *inode, struct file *file)	\
{									\
	return single_open(file, __show, inode->i_private);		\
}									\
static const struct file_operations __fops = {				\
	.owner	 	= THIS_MODULE,					\
	.open	 	= __fops ## _open,				\
	.read           = seq_read,					\
	.llseek         = seq_lseek,					\
	.release        = single_release,				\
};

/*
 * Aggregate reset information in a specific structure, because the reset
 * node does not contain any link to the parent hwmod structure
 */
struct reset_info {
	struct omap_hwmod 	*oh;
	const char		*name;
	u8			rst_shift;
};

/* internal hwmod states */
static const char* states[_HWMOD_STATE_LAST + 1] = {
	[_HWMOD_STATE_UNKNOWN] 		= "unknown",
	[_HWMOD_STATE_REGISTERED] 	= "registered",
	[_HWMOD_STATE_CLKS_INITED] 	= "clks_inited",
	[_HWMOD_STATE_INITIALIZED] 	= "initialized",
	[_HWMOD_STATE_ENABLED] 		= "enabled",
	[_HWMOD_STATE_IDLE] 		= "idle",
	[_HWMOD_STATE_DISABLED] 	= "disabled",
};

const char * _state_str(int state)
{
	if (state < 0 || state > _HWMOD_STATE_LAST)
		return "invalid_state";

	return states[state];
}

static int _set_state(void *data, u64 val)
{
	*(u8 *)data = val;
	return 0;
}
static int _get_state(void *data, u64 *val)
{
	*val = *(u8 *)data;
	return 0;
}
DEFINE_SIMPLE_ATTRIBUTE(state_fops, _get_state, _set_state, "%llu\n");

static int default_open(struct inode *inode, struct file *file)
{
	if (inode->i_private)
		file->private_data = inode->i_private;

	return 0;
}

#define MAX_BUFFER_SIZE 16

static ssize_t read_hwmod_state(struct file *file, char __user *user_buf,
			        size_t count, loff_t *ppos)
{
	char buf[MAX_BUFFER_SIZE];
	struct omap_hwmod *oh = file->private_data;
	const char *msg = _state_str(oh->_state);

	int len = snprintf(buf, MAX_BUFFER_SIZE, "%s\n", msg);

	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
}

static ssize_t write_hwmod_state(struct file *file, const char __user *user_buf,
			         size_t count, loff_t *ppos)
{
	char buf[32];
	int buf_size;
	char *p;
	int len;
	struct omap_hwmod *oh = file->private_data;

	if (!oh || !oh->name)
		return -EINVAL;

	buf_size = min(count, (sizeof(buf)-1));
	if (copy_from_user(buf, user_buf, buf_size))
		return -EFAULT;

	p = memchr(buf, '\n', buf_size);
	len = p ? p - buf : buf_size;
	buf[len] = '\0';

	if (!strncmp(buf, "enable", len))
		omap_hwmod_enable(oh);
	else if (!strncmp(buf, "idle", len))
		omap_hwmod_idle(oh);
	else if (!strncmp(buf, "disable", len))
		omap_hwmod_shutdown(oh);
	else
		pr_warning("write_hwmod_state: invalid state: %s\n", buf);

	return count;
}

static const struct file_operations hwmod_state_fops = {
	.read =		read_hwmod_state,
	.write =	write_hwmod_state,
	.open =		default_open,
};

/**
 * _reset_get - helper function for debugfs read to reset line
 * @data: pointer to data initialized during debugfs_create_file. In this
 * case this is a pointer to struct reset_info
 * @val: pointer to the value that will be read and propagate to the debufs
 * interface
 *
 * Returns: 0 if the reset is deasserted or asserted (0 or 1), any other states
 * are invalid, so return -EINVAL in that case.
 */
static int _reset_get(void *data, u64 *val)
{
	struct reset_info *info = data;
	int state;

	state = omap_hwmod_hardreset_state(info->oh, info->name);

	pr_debug("_reset_get: %s:%d %d\n", info->name, info->rst_shift, state);

	*val = (u64)state;

	if (state >= 0)
		return 0;

	return *val;
}

/**
 * _reset_set - helper function for debugfs write to reset line
 * @data: pointer to data initialized during debugfs_create_file. In this
 * case this is a pointer to struct reset_info
 * @val: value that should written from the debufs interface
 *
 * Assert or deassert the reset line depending of the value written
 * on the debugfs "rst" file entry
 * Returns -EINVAL if val is not 0 or 1.
 */
static int _reset_set(void *data, u64 val)
{
	struct reset_info *info = data;
	int ret = -EINVAL;

	pr_debug("_reset_set: %s[%d]: %llu\n", info->name, info->rst_shift,
					       val);

	if (val == 1)
		ret = omap_hwmod_hardreset_assert(info->oh, info->name);
	else if	(val == 0)
		ret = omap_hwmod_hardreset_deassert(info->oh, info->name);

	return ret;
}

DEFINE_SIMPLE_ATTRIBUTE(reset_fops, _reset_get, _reset_set, "%llu\n");

static struct omap_hwmod_addr_space* _print_addresses(struct seq_file *s,
						      struct omap_hwmod *oh)
{
	struct omap_hwmod_ocp_if *os = NULL;
	struct omap_hwmod_addr_space *mem;
	int j;

	if (!oh || oh->slaves_cnt == 0) {
		seq_printf(s, "  address:      N/A\n");
		return NULL;
	}

	for (j = 0; j < oh->slaves_cnt; j++) {
		int i;

		os = oh->slaves[j];
		if (!os->addr)
			continue;

		if (os->user & OCP_USER_MPU) {
			for (i = 0, mem = os->addr; i < os->addr_cnt; i++, mem++)
				seq_printf(s, "  address:      0x%08x-0x%08x (mpu)\n",
					   mem->pa_start, mem->pa_end);
		} else {
			for (i = 0, mem = os->addr; i < os->addr_cnt; i++, mem++)
				seq_printf(s, "  address:      0x%08x-0x%08x\n",
					   mem->pa_start, mem->pa_end);
		}
	}

	return NULL;
}

static int show_hwmod_summary(struct omap_hwmod *oh, void* param)
{
	struct seq_file *s = param;
	int i;

	if (!oh || !oh->name)
		return -EINVAL;

	seq_printf(s, "%s\n", oh->name);
	seq_printf(s, "  state:        %s:%d\n", _state_str(oh->_state),
		   oh->_state);
	seq_printf(s, "  class:        %s\n", oh->class->name);
	seq_printf(s, "  flags:        0x%04x\n", oh->flags);

	_print_addresses(s, oh);

	if (!IS_ERR_OR_NULL(oh->_clk))
		seq_printf(s, "  clock:        %s\n", oh->_clk->name);

	if (oh->rst_lines_cnt > 0)
		seq_printf(s, "  resets(%d):\n", oh->rst_lines_cnt);
	for (i = 0; i < oh->rst_lines_cnt; i++) {
		int state = omap_hwmod_hardreset_state(oh,
						       oh->rst_lines[i].name);
		seq_printf(s, "    %s:%d\n", oh->rst_lines[i].name,
			   state);
	}

	if (oh->mpu_irqs_cnt > 0)
		seq_printf(s, "  irqs(%d):\n", oh->mpu_irqs_cnt);
	for (i = 0; i < oh->mpu_irqs_cnt; i++)
		seq_printf(s, "    %s:%d\n", oh->mpu_irqs[i].name,
			   oh->mpu_irqs[i].irq);

	if (oh->sdma_reqs_cnt > 0)
		seq_printf(s, "  dmas(%d):\n", oh->sdma_reqs_cnt);
	for (i = 0; i < oh->sdma_reqs_cnt; i++)
		seq_printf(s, "    %s:%d\n", oh->sdma_reqs[i].name,
			   oh->sdma_reqs[i].dma_req);

	return 0;
}

static int show_hwmod(struct seq_file *s, void *unused)
{
	struct omap_hwmod *oh = s->private;

	show_hwmod_summary(oh, s);

	return 0;
}
DEFINE_DEBUGFS_SHOW(_hwmod_fops, show_hwmod);

/**
 * create_debugfs_entry - create a debugfs entry per omap_hwmod
 * @oh: struct omap_hwmod *
 * @parent: reference to the parent directory dentry needed for the creation
 * of the files inside the debugfs directory
 *
 * Create a directory entry for each hwmod.
 * Each directory will contain at least one state file and potentially
 * some files that will represent the HW reset lines of that IP.
 */
static int create_debugfs_entry(struct omap_hwmod *oh, void* parent)
{
	struct dentry *d, *fd;
	int i;

	if (!oh || !oh->name)
		return -EINVAL;

	pr_debug("creating debugfs entry: %s[%p]\n", oh->name, oh);

	d = debugfs_create_dir(oh->name, parent);
	if (IS_ERR(d))
		return PTR_ERR(d);

	fd = debugfs_create_file("summary", S_IRUGO, d, oh, &_hwmod_fops);
	if (IS_ERR(fd))
		return PTR_ERR(fd);

	fd = debugfs_create_file("state", S_IRUGO|S_IWUSR, d, oh,
				 &hwmod_state_fops);
	if (IS_ERR(fd))
		return PTR_ERR(fd);

	if (oh->rst_lines_cnt > 0) {
		struct dentry *drst = debugfs_create_dir("resets", d);
		if (IS_ERR(drst))
			return PTR_ERR(drst);

		/* Create one directory entry per reset line */
		for (i = 0; i < oh->rst_lines_cnt; i++) {
			struct reset_info *info;
			const char *name = oh->rst_lines[i].name;

			/* XXX need to find a way to free that memory */
			info = kmalloc(sizeof(struct reset_info), GFP_KERNEL);
			if (!info)
				return -ENOMEM;

			info->oh 	= oh;
			info->name 	= oh->rst_lines[i].name;
			info->rst_shift = oh->rst_lines[i].rst_shift;

			debugfs_create_file(name, S_IRUGO|S_IWUSR, drst, info,
					    &reset_fops);
		}
	}

	return 0;
}

/* hwmod_debugfs_init - Initialize the debugfs directory tree for hwmods */
static int __init hwmod_debugfs_init(void)
{
	struct dentry *d;

	pr_warning("omap_hwmod: Initialize debugfs support");

	d = debugfs_create_dir("hwmods", NULL);
	if (IS_ERR(d))
		return PTR_ERR(d);

	omap_hwmod_for_each(create_debugfs_entry, d);

	return 0;
}
late_initcall(hwmod_debugfs_init);