xref: /openbmc/linux/drivers/usb/musb/musb_debugfs.c (revision 4da722ca)
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 <linux/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 	{ "IntrRxE",	MUSB_INTRRXE,	16 },
63 	{ "IntrTxE",	MUSB_INTRTXE,	16 },
64 	{ "IntrUsbE",	MUSB_INTRUSBE,	8 },
65 	{ "DevCtl",	MUSB_DEVCTL,	8 },
66 	{ "VControl",	0x68,		32 },
67 	{ "HWVers",	0x69,		16 },
68 	{ "LinkInfo",	MUSB_LINKINFO,	8 },
69 	{ "VPLen",	MUSB_VPLEN,	8 },
70 	{ "HS_EOF1",	MUSB_HS_EOF1,	8 },
71 	{ "FS_EOF1",	MUSB_FS_EOF1,	8 },
72 	{ "LS_EOF1",	MUSB_LS_EOF1,	8 },
73 	{ "SOFT_RST",	0x7F,		8 },
74 	{ "DMA_CNTLch0",	0x204,	16 },
75 	{ "DMA_ADDRch0",	0x208,	32 },
76 	{ "DMA_COUNTch0",	0x20C,	32 },
77 	{ "DMA_CNTLch1",	0x214,	16 },
78 	{ "DMA_ADDRch1",	0x218,	32 },
79 	{ "DMA_COUNTch1",	0x21C,	32 },
80 	{ "DMA_CNTLch2",	0x224,	16 },
81 	{ "DMA_ADDRch2",	0x228,	32 },
82 	{ "DMA_COUNTch2",	0x22C,	32 },
83 	{ "DMA_CNTLch3",	0x234,	16 },
84 	{ "DMA_ADDRch3",	0x238,	32 },
85 	{ "DMA_COUNTch3",	0x23C,	32 },
86 	{ "DMA_CNTLch4",	0x244,	16 },
87 	{ "DMA_ADDRch4",	0x248,	32 },
88 	{ "DMA_COUNTch4",	0x24C,	32 },
89 	{ "DMA_CNTLch5",	0x254,	16 },
90 	{ "DMA_ADDRch5",	0x258,	32 },
91 	{ "DMA_COUNTch5",	0x25C,	32 },
92 	{ "DMA_CNTLch6",	0x264,	16 },
93 	{ "DMA_ADDRch6",	0x268,	32 },
94 	{ "DMA_COUNTch6",	0x26C,	32 },
95 	{ "DMA_CNTLch7",	0x274,	16 },
96 	{ "DMA_ADDRch7",	0x278,	32 },
97 	{ "DMA_COUNTch7",	0x27C,	32 },
98 #ifndef CONFIG_BLACKFIN
99 	{ "ConfigData",	MUSB_CONFIGDATA,8 },
100 	{ "BabbleCtl",	MUSB_BABBLE_CTL,8 },
101 	{ "TxFIFOsz",	MUSB_TXFIFOSZ,	8 },
102 	{ "RxFIFOsz",	MUSB_RXFIFOSZ,	8 },
103 	{ "TxFIFOadd",	MUSB_TXFIFOADD,	16 },
104 	{ "RxFIFOadd",	MUSB_RXFIFOADD,	16 },
105 	{ "EPInfo",	MUSB_EPINFO,	8 },
106 	{ "RAMInfo",	MUSB_RAMINFO,	8 },
107 #endif
108 	{  }	/* Terminating Entry */
109 };
110 
111 static int musb_regdump_show(struct seq_file *s, void *unused)
112 {
113 	struct musb		*musb = s->private;
114 	unsigned		i;
115 
116 	seq_printf(s, "MUSB (M)HDRC Register Dump\n");
117 	pm_runtime_get_sync(musb->controller);
118 
119 	for (i = 0; i < ARRAY_SIZE(musb_regmap); i++) {
120 		switch (musb_regmap[i].size) {
121 		case 8:
122 			seq_printf(s, "%-12s: %02x\n", musb_regmap[i].name,
123 					musb_readb(musb->mregs, musb_regmap[i].offset));
124 			break;
125 		case 16:
126 			seq_printf(s, "%-12s: %04x\n", musb_regmap[i].name,
127 					musb_readw(musb->mregs, musb_regmap[i].offset));
128 			break;
129 		case 32:
130 			seq_printf(s, "%-12s: %08x\n", musb_regmap[i].name,
131 					musb_readl(musb->mregs, musb_regmap[i].offset));
132 			break;
133 		}
134 	}
135 
136 	pm_runtime_mark_last_busy(musb->controller);
137 	pm_runtime_put_autosuspend(musb->controller);
138 	return 0;
139 }
140 
141 static int musb_regdump_open(struct inode *inode, struct file *file)
142 {
143 	return single_open(file, musb_regdump_show, inode->i_private);
144 }
145 
146 static int musb_test_mode_show(struct seq_file *s, void *unused)
147 {
148 	struct musb		*musb = s->private;
149 	unsigned		test;
150 
151 	pm_runtime_get_sync(musb->controller);
152 	test = musb_readb(musb->mregs, MUSB_TESTMODE);
153 	pm_runtime_mark_last_busy(musb->controller);
154 	pm_runtime_put_autosuspend(musb->controller);
155 
156 	if (test == (MUSB_TEST_FORCE_HOST | MUSB_TEST_FORCE_FS))
157 		seq_printf(s, "force host full-speed\n");
158 
159 	else if (test == (MUSB_TEST_FORCE_HOST | MUSB_TEST_FORCE_HS))
160 		seq_printf(s, "force host high-speed\n");
161 
162 	else if (test == MUSB_TEST_FORCE_HOST)
163 		seq_printf(s, "force host\n");
164 
165 	else if (test == MUSB_TEST_FIFO_ACCESS)
166 		seq_printf(s, "fifo access\n");
167 
168 	else if (test == MUSB_TEST_FORCE_FS)
169 		seq_printf(s, "force full-speed\n");
170 
171 	else if (test == MUSB_TEST_FORCE_HS)
172 		seq_printf(s, "force high-speed\n");
173 
174 	else if (test == MUSB_TEST_PACKET)
175 		seq_printf(s, "test packet\n");
176 
177 	else if (test == MUSB_TEST_K)
178 		seq_printf(s, "test K\n");
179 
180 	else if (test == MUSB_TEST_J)
181 		seq_printf(s, "test J\n");
182 
183 	else if (test == MUSB_TEST_SE0_NAK)
184 		seq_printf(s, "test SE0 NAK\n");
185 
186 	return 0;
187 }
188 
189 static const struct file_operations musb_regdump_fops = {
190 	.open			= musb_regdump_open,
191 	.read			= seq_read,
192 	.llseek			= seq_lseek,
193 	.release		= single_release,
194 };
195 
196 static int musb_test_mode_open(struct inode *inode, struct file *file)
197 {
198 	return single_open(file, musb_test_mode_show, inode->i_private);
199 }
200 
201 static ssize_t musb_test_mode_write(struct file *file,
202 		const char __user *ubuf, size_t count, loff_t *ppos)
203 {
204 	struct seq_file		*s = file->private_data;
205 	struct musb		*musb = s->private;
206 	u8			test;
207 	char			buf[24];
208 
209 	pm_runtime_get_sync(musb->controller);
210 	test = musb_readb(musb->mregs, MUSB_TESTMODE);
211 	if (test) {
212 		dev_err(musb->controller, "Error: test mode is already set. "
213 			"Please do USB Bus Reset to start a new test.\n");
214 		goto ret;
215 	}
216 
217 	memset(buf, 0x00, sizeof(buf));
218 
219 	if (copy_from_user(buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
220 		return -EFAULT;
221 
222 	if (strstarts(buf, "force host full-speed"))
223 		test = MUSB_TEST_FORCE_HOST | MUSB_TEST_FORCE_FS;
224 
225 	else if (strstarts(buf, "force host high-speed"))
226 		test = MUSB_TEST_FORCE_HOST | MUSB_TEST_FORCE_HS;
227 
228 	else if (strstarts(buf, "force host"))
229 		test = MUSB_TEST_FORCE_HOST;
230 
231 	else if (strstarts(buf, "fifo access"))
232 		test = MUSB_TEST_FIFO_ACCESS;
233 
234 	else if (strstarts(buf, "force full-speed"))
235 		test = MUSB_TEST_FORCE_FS;
236 
237 	else if (strstarts(buf, "force high-speed"))
238 		test = MUSB_TEST_FORCE_HS;
239 
240 	else if (strstarts(buf, "test packet")) {
241 		test = MUSB_TEST_PACKET;
242 		musb_load_testpacket(musb);
243 	}
244 
245 	else if (strstarts(buf, "test K"))
246 		test = MUSB_TEST_K;
247 
248 	else if (strstarts(buf, "test J"))
249 		test = MUSB_TEST_J;
250 
251 	else if (strstarts(buf, "test SE0 NAK"))
252 		test = MUSB_TEST_SE0_NAK;
253 
254 	musb_writeb(musb->mregs, MUSB_TESTMODE, test);
255 
256 ret:
257 	pm_runtime_mark_last_busy(musb->controller);
258 	pm_runtime_put_autosuspend(musb->controller);
259 	return count;
260 }
261 
262 static const struct file_operations musb_test_mode_fops = {
263 	.open			= musb_test_mode_open,
264 	.write			= musb_test_mode_write,
265 	.read			= seq_read,
266 	.llseek			= seq_lseek,
267 	.release		= single_release,
268 };
269 
270 static int musb_softconnect_show(struct seq_file *s, void *unused)
271 {
272 	struct musb	*musb = s->private;
273 	u8		reg;
274 	int		connect;
275 
276 	switch (musb->xceiv->otg->state) {
277 	case OTG_STATE_A_HOST:
278 	case OTG_STATE_A_WAIT_BCON:
279 		pm_runtime_get_sync(musb->controller);
280 
281 		reg = musb_readb(musb->mregs, MUSB_DEVCTL);
282 		connect = reg & MUSB_DEVCTL_SESSION ? 1 : 0;
283 
284 		pm_runtime_mark_last_busy(musb->controller);
285 		pm_runtime_put_autosuspend(musb->controller);
286 		break;
287 	default:
288 		connect = -1;
289 	}
290 
291 	seq_printf(s, "%d\n", connect);
292 
293 	return 0;
294 }
295 
296 static int musb_softconnect_open(struct inode *inode, struct file *file)
297 {
298 	return single_open(file, musb_softconnect_show, inode->i_private);
299 }
300 
301 static ssize_t musb_softconnect_write(struct file *file,
302 		const char __user *ubuf, size_t count, loff_t *ppos)
303 {
304 	struct seq_file		*s = file->private_data;
305 	struct musb		*musb = s->private;
306 	char			buf[2];
307 	u8			reg;
308 
309 	memset(buf, 0x00, sizeof(buf));
310 
311 	if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
312 		return -EFAULT;
313 
314 	pm_runtime_get_sync(musb->controller);
315 	if (!strncmp(buf, "0", 1)) {
316 		switch (musb->xceiv->otg->state) {
317 		case OTG_STATE_A_HOST:
318 			musb_root_disconnect(musb);
319 			reg = musb_readb(musb->mregs, MUSB_DEVCTL);
320 			reg &= ~MUSB_DEVCTL_SESSION;
321 			musb_writeb(musb->mregs, MUSB_DEVCTL, reg);
322 			break;
323 		default:
324 			break;
325 		}
326 	} else if (!strncmp(buf, "1", 1)) {
327 		switch (musb->xceiv->otg->state) {
328 		case OTG_STATE_A_WAIT_BCON:
329 			/*
330 			 * musb_save_context() called in musb_runtime_suspend()
331 			 * might cache devctl with SESSION bit cleared during
332 			 * soft-disconnect, so specifically set SESSION bit
333 			 * here to preserve it for musb_runtime_resume().
334 			 */
335 			musb->context.devctl |= MUSB_DEVCTL_SESSION;
336 			reg = musb_readb(musb->mregs, MUSB_DEVCTL);
337 			reg |= MUSB_DEVCTL_SESSION;
338 			musb_writeb(musb->mregs, MUSB_DEVCTL, reg);
339 			break;
340 		default:
341 			break;
342 		}
343 	}
344 
345 	pm_runtime_mark_last_busy(musb->controller);
346 	pm_runtime_put_autosuspend(musb->controller);
347 	return count;
348 }
349 
350 /*
351  * In host mode, connect/disconnect the bus without physically
352  * remove the devices.
353  */
354 static const struct file_operations musb_softconnect_fops = {
355 	.open			= musb_softconnect_open,
356 	.write			= musb_softconnect_write,
357 	.read			= seq_read,
358 	.llseek			= seq_lseek,
359 	.release		= single_release,
360 };
361 
362 int musb_init_debugfs(struct musb *musb)
363 {
364 	struct dentry		*root;
365 	struct dentry		*file;
366 	int			ret;
367 
368 	root = debugfs_create_dir(dev_name(musb->controller), NULL);
369 	if (!root) {
370 		ret = -ENOMEM;
371 		goto err0;
372 	}
373 
374 	file = debugfs_create_file("regdump", S_IRUGO, root, musb,
375 			&musb_regdump_fops);
376 	if (!file) {
377 		ret = -ENOMEM;
378 		goto err1;
379 	}
380 
381 	file = debugfs_create_file("testmode", S_IRUGO | S_IWUSR,
382 			root, musb, &musb_test_mode_fops);
383 	if (!file) {
384 		ret = -ENOMEM;
385 		goto err1;
386 	}
387 
388 	file = debugfs_create_file("softconnect", S_IRUGO | S_IWUSR,
389 			root, musb, &musb_softconnect_fops);
390 	if (!file) {
391 		ret = -ENOMEM;
392 		goto err1;
393 	}
394 
395 	musb->debugfs_root = root;
396 
397 	return 0;
398 
399 err1:
400 	debugfs_remove_recursive(root);
401 
402 err0:
403 	return ret;
404 }
405 
406 void /* __init_or_exit */ musb_exit_debugfs(struct musb *musb)
407 {
408 	debugfs_remove_recursive(musb->debugfs_root);
409 }
410