xref: /openbmc/linux/drivers/usb/musb/musb_debugfs.c (revision ff6defa6)
1 /*
2  * MUSB OTG driver debugfs support
3  *
4  * Copyright 2010 Nokia Corporation
5  * Contact: Felipe Balbi <felipe.balbi@nokia.com>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * 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., 51 Franklin St, Fifth Floor, Boston, MA
19  * 02110-1301 USA
20  *
21  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
22  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
24  * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
27  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
28  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 
34 #include <linux/module.h>
35 #include <linux/kernel.h>
36 #include <linux/init.h>
37 #include <linux/debugfs.h>
38 #include <linux/seq_file.h>
39 
40 #include <asm/uaccess.h>
41 
42 #include "musb_core.h"
43 #include "musb_debug.h"
44 
45 struct musb_register_map {
46 	char			*name;
47 	unsigned		offset;
48 	unsigned		size;
49 };
50 
51 static const struct musb_register_map musb_regmap[] = {
52 	{ "FAddr",	MUSB_FADDR,	8 },
53 	{ "Power",	MUSB_POWER,	8 },
54 	{ "Frame",	MUSB_FRAME,	16 },
55 	{ "Index",	MUSB_INDEX,	8 },
56 	{ "Testmode",	MUSB_TESTMODE,	8 },
57 	{ "TxMaxPp",	MUSB_TXMAXP,	16 },
58 	{ "TxCSRp",	MUSB_TXCSR,	16 },
59 	{ "RxMaxPp",	MUSB_RXMAXP,	16 },
60 	{ "RxCSR",	MUSB_RXCSR,	16 },
61 	{ "RxCount",	MUSB_RXCOUNT,	16 },
62 	{ "ConfigData",	MUSB_CONFIGDATA,8 },
63 	{ "IntrRxE",	MUSB_INTRRXE,	16 },
64 	{ "IntrTxE",	MUSB_INTRTXE,	16 },
65 	{ "IntrUsbE",	MUSB_INTRUSBE,	8 },
66 	{ "DevCtl",	MUSB_DEVCTL,	8 },
67 	{ "BabbleCtl",	MUSB_BABBLE_CTL,8 },
68 	{ "TxFIFOsz",	MUSB_TXFIFOSZ,	8 },
69 	{ "RxFIFOsz",	MUSB_RXFIFOSZ,	8 },
70 	{ "TxFIFOadd",	MUSB_TXFIFOADD,	16 },
71 	{ "RxFIFOadd",	MUSB_RXFIFOADD,	16 },
72 	{ "VControl",	0x68,		32 },
73 	{ "HWVers",	0x69,		16 },
74 	{ "EPInfo",	MUSB_EPINFO,	8 },
75 	{ "RAMInfo",	MUSB_RAMINFO,	8 },
76 	{ "LinkInfo",	MUSB_LINKINFO,	8 },
77 	{ "VPLen",	MUSB_VPLEN,	8 },
78 	{ "HS_EOF1",	MUSB_HS_EOF1,	8 },
79 	{ "FS_EOF1",	MUSB_FS_EOF1,	8 },
80 	{ "LS_EOF1",	MUSB_LS_EOF1,	8 },
81 	{ "SOFT_RST",	0x7F,		8 },
82 	{ "DMA_CNTLch0",	0x204,	16 },
83 	{ "DMA_ADDRch0",	0x208,	32 },
84 	{ "DMA_COUNTch0",	0x20C,	32 },
85 	{ "DMA_CNTLch1",	0x214,	16 },
86 	{ "DMA_ADDRch1",	0x218,	32 },
87 	{ "DMA_COUNTch1",	0x21C,	32 },
88 	{ "DMA_CNTLch2",	0x224,	16 },
89 	{ "DMA_ADDRch2",	0x228,	32 },
90 	{ "DMA_COUNTch2",	0x22C,	32 },
91 	{ "DMA_CNTLch3",	0x234,	16 },
92 	{ "DMA_ADDRch3",	0x238,	32 },
93 	{ "DMA_COUNTch3",	0x23C,	32 },
94 	{ "DMA_CNTLch4",	0x244,	16 },
95 	{ "DMA_ADDRch4",	0x248,	32 },
96 	{ "DMA_COUNTch4",	0x24C,	32 },
97 	{ "DMA_CNTLch5",	0x254,	16 },
98 	{ "DMA_ADDRch5",	0x258,	32 },
99 	{ "DMA_COUNTch5",	0x25C,	32 },
100 	{ "DMA_CNTLch6",	0x264,	16 },
101 	{ "DMA_ADDRch6",	0x268,	32 },
102 	{ "DMA_COUNTch6",	0x26C,	32 },
103 	{ "DMA_CNTLch7",	0x274,	16 },
104 	{ "DMA_ADDRch7",	0x278,	32 },
105 	{ "DMA_COUNTch7",	0x27C,	32 },
106 	{  }	/* Terminating Entry */
107 };
108 
109 static int musb_regdump_show(struct seq_file *s, void *unused)
110 {
111 	struct musb		*musb = s->private;
112 	unsigned		i;
113 
114 	seq_printf(s, "MUSB (M)HDRC Register Dump\n");
115 
116 	for (i = 0; i < ARRAY_SIZE(musb_regmap); i++) {
117 		switch (musb_regmap[i].size) {
118 		case 8:
119 			seq_printf(s, "%-12s: %02x\n", musb_regmap[i].name,
120 					musb_readb(musb->mregs, musb_regmap[i].offset));
121 			break;
122 		case 16:
123 			seq_printf(s, "%-12s: %04x\n", musb_regmap[i].name,
124 					musb_readw(musb->mregs, musb_regmap[i].offset));
125 			break;
126 		case 32:
127 			seq_printf(s, "%-12s: %08x\n", musb_regmap[i].name,
128 					musb_readl(musb->mregs, musb_regmap[i].offset));
129 			break;
130 		}
131 	}
132 
133 	return 0;
134 }
135 
136 static int musb_regdump_open(struct inode *inode, struct file *file)
137 {
138 	return single_open(file, musb_regdump_show, inode->i_private);
139 }
140 
141 static int musb_test_mode_show(struct seq_file *s, void *unused)
142 {
143 	struct musb		*musb = s->private;
144 	unsigned		test;
145 
146 	test = musb_readb(musb->mregs, MUSB_TESTMODE);
147 
148 	if (test & MUSB_TEST_FORCE_HOST)
149 		seq_printf(s, "force host\n");
150 
151 	if (test & MUSB_TEST_FIFO_ACCESS)
152 		seq_printf(s, "fifo access\n");
153 
154 	if (test & MUSB_TEST_FORCE_FS)
155 		seq_printf(s, "force full-speed\n");
156 
157 	if (test & MUSB_TEST_FORCE_HS)
158 		seq_printf(s, "force high-speed\n");
159 
160 	if (test & MUSB_TEST_PACKET)
161 		seq_printf(s, "test packet\n");
162 
163 	if (test & MUSB_TEST_K)
164 		seq_printf(s, "test K\n");
165 
166 	if (test & MUSB_TEST_J)
167 		seq_printf(s, "test J\n");
168 
169 	if (test & MUSB_TEST_SE0_NAK)
170 		seq_printf(s, "test SE0 NAK\n");
171 
172 	return 0;
173 }
174 
175 static const struct file_operations musb_regdump_fops = {
176 	.open			= musb_regdump_open,
177 	.read			= seq_read,
178 	.llseek			= seq_lseek,
179 	.release		= single_release,
180 };
181 
182 static int musb_test_mode_open(struct inode *inode, struct file *file)
183 {
184 	return single_open(file, musb_test_mode_show, inode->i_private);
185 }
186 
187 static ssize_t musb_test_mode_write(struct file *file,
188 		const char __user *ubuf, size_t count, loff_t *ppos)
189 {
190 	struct seq_file		*s = file->private_data;
191 	struct musb		*musb = s->private;
192 	u8			test = 0;
193 	char			buf[18];
194 
195 	memset(buf, 0x00, sizeof(buf));
196 
197 	if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
198 		return -EFAULT;
199 
200 	if (!strncmp(buf, "force host", 9))
201 		test = MUSB_TEST_FORCE_HOST;
202 
203 	if (!strncmp(buf, "fifo access", 11))
204 		test = MUSB_TEST_FIFO_ACCESS;
205 
206 	if (!strncmp(buf, "force full-speed", 15))
207 		test = MUSB_TEST_FORCE_FS;
208 
209 	if (!strncmp(buf, "force high-speed", 15))
210 		test = MUSB_TEST_FORCE_HS;
211 
212 	if (!strncmp(buf, "test packet", 10)) {
213 		test = MUSB_TEST_PACKET;
214 		musb_load_testpacket(musb);
215 	}
216 
217 	if (!strncmp(buf, "test K", 6))
218 		test = MUSB_TEST_K;
219 
220 	if (!strncmp(buf, "test J", 6))
221 		test = MUSB_TEST_J;
222 
223 	if (!strncmp(buf, "test SE0 NAK", 12))
224 		test = MUSB_TEST_SE0_NAK;
225 
226 	musb_writeb(musb->mregs, MUSB_TESTMODE, test);
227 
228 	return count;
229 }
230 
231 static const struct file_operations musb_test_mode_fops = {
232 	.open			= musb_test_mode_open,
233 	.write			= musb_test_mode_write,
234 	.read			= seq_read,
235 	.llseek			= seq_lseek,
236 	.release		= single_release,
237 };
238 
239 int musb_init_debugfs(struct musb *musb)
240 {
241 	struct dentry		*root;
242 	struct dentry		*file;
243 	int			ret;
244 
245 	root = debugfs_create_dir(dev_name(musb->controller), NULL);
246 	if (!root) {
247 		ret = -ENOMEM;
248 		goto err0;
249 	}
250 
251 	file = debugfs_create_file("regdump", S_IRUGO, root, musb,
252 			&musb_regdump_fops);
253 	if (!file) {
254 		ret = -ENOMEM;
255 		goto err1;
256 	}
257 
258 	file = debugfs_create_file("testmode", S_IRUGO | S_IWUSR,
259 			root, musb, &musb_test_mode_fops);
260 	if (!file) {
261 		ret = -ENOMEM;
262 		goto err1;
263 	}
264 
265 	musb->debugfs_root = root;
266 
267 	return 0;
268 
269 err1:
270 	debugfs_remove_recursive(root);
271 
272 err0:
273 	return ret;
274 }
275 
276 void /* __init_or_exit */ musb_exit_debugfs(struct musb *musb)
277 {
278 	debugfs_remove_recursive(musb->debugfs_root);
279 }
280