blob: 7cf5a1bbdaff9323971879252d225975072e7ac4 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Felipe Balbi7f7f9e22010-03-12 10:29:11 +02002/*
3 * MUSB OTG driver debugfs support
4 *
5 * Copyright 2010 Nokia Corporation
6 * Contact: Felipe Balbi <felipe.balbi@nokia.com>
Felipe Balbi7f7f9e22010-03-12 10:29:11 +02007 */
8
9#include <linux/module.h>
10#include <linux/kernel.h>
Felipe Balbi7f7f9e22010-03-12 10:29:11 +020011#include <linux/init.h>
Felipe Balbi7f7f9e22010-03-12 10:29:11 +020012#include <linux/debugfs.h>
13#include <linux/seq_file.h>
14
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080015#include <linux/uaccess.h>
Felipe Balbi7f7f9e22010-03-12 10:29:11 +020016
17#include "musb_core.h"
18#include "musb_debug.h"
19
Felipe Balbi7f7f9e22010-03-12 10:29:11 +020020struct musb_register_map {
21 char *name;
22 unsigned offset;
23 unsigned size;
24};
25
26static const struct musb_register_map musb_regmap[] = {
Roman Byshkoc2365ce2014-11-10 21:53:32 +010027 { "FAddr", MUSB_FADDR, 8 },
28 { "Power", MUSB_POWER, 8 },
29 { "Frame", MUSB_FRAME, 16 },
30 { "Index", MUSB_INDEX, 8 },
31 { "Testmode", MUSB_TESTMODE, 8 },
32 { "TxMaxPp", MUSB_TXMAXP, 16 },
33 { "TxCSRp", MUSB_TXCSR, 16 },
34 { "RxMaxPp", MUSB_RXMAXP, 16 },
35 { "RxCSR", MUSB_RXCSR, 16 },
36 { "RxCount", MUSB_RXCOUNT, 16 },
Roman Byshkoc2365ce2014-11-10 21:53:32 +010037 { "IntrRxE", MUSB_INTRRXE, 16 },
38 { "IntrTxE", MUSB_INTRTXE, 16 },
39 { "IntrUsbE", MUSB_INTRUSBE, 8 },
40 { "DevCtl", MUSB_DEVCTL, 8 },
Roman Byshkoc2365ce2014-11-10 21:53:32 +010041 { "VControl", 0x68, 32 },
42 { "HWVers", 0x69, 16 },
Roman Byshkoc2365ce2014-11-10 21:53:32 +010043 { "LinkInfo", MUSB_LINKINFO, 8 },
44 { "VPLen", MUSB_VPLEN, 8 },
45 { "HS_EOF1", MUSB_HS_EOF1, 8 },
46 { "FS_EOF1", MUSB_FS_EOF1, 8 },
47 { "LS_EOF1", MUSB_LS_EOF1, 8 },
48 { "SOFT_RST", 0x7F, 8 },
Felipe Balbi7f7f9e22010-03-12 10:29:11 +020049 { "DMA_CNTLch0", 0x204, 16 },
Anand Gadiyar0936fb52010-07-08 14:02:59 +053050 { "DMA_ADDRch0", 0x208, 32 },
51 { "DMA_COUNTch0", 0x20C, 32 },
Felipe Balbi7f7f9e22010-03-12 10:29:11 +020052 { "DMA_CNTLch1", 0x214, 16 },
Anand Gadiyar0936fb52010-07-08 14:02:59 +053053 { "DMA_ADDRch1", 0x218, 32 },
54 { "DMA_COUNTch1", 0x21C, 32 },
Felipe Balbi7f7f9e22010-03-12 10:29:11 +020055 { "DMA_CNTLch2", 0x224, 16 },
Anand Gadiyar0936fb52010-07-08 14:02:59 +053056 { "DMA_ADDRch2", 0x228, 32 },
57 { "DMA_COUNTch2", 0x22C, 32 },
Felipe Balbi7f7f9e22010-03-12 10:29:11 +020058 { "DMA_CNTLch3", 0x234, 16 },
Anand Gadiyar0936fb52010-07-08 14:02:59 +053059 { "DMA_ADDRch3", 0x238, 32 },
60 { "DMA_COUNTch3", 0x23C, 32 },
Felipe Balbi7f7f9e22010-03-12 10:29:11 +020061 { "DMA_CNTLch4", 0x244, 16 },
Anand Gadiyar0936fb52010-07-08 14:02:59 +053062 { "DMA_ADDRch4", 0x248, 32 },
63 { "DMA_COUNTch4", 0x24C, 32 },
Felipe Balbi7f7f9e22010-03-12 10:29:11 +020064 { "DMA_CNTLch5", 0x254, 16 },
Anand Gadiyar0936fb52010-07-08 14:02:59 +053065 { "DMA_ADDRch5", 0x258, 32 },
66 { "DMA_COUNTch5", 0x25C, 32 },
Felipe Balbi7f7f9e22010-03-12 10:29:11 +020067 { "DMA_CNTLch6", 0x264, 16 },
Anand Gadiyar0936fb52010-07-08 14:02:59 +053068 { "DMA_ADDRch6", 0x268, 32 },
69 { "DMA_COUNTch6", 0x26C, 32 },
Felipe Balbi7f7f9e22010-03-12 10:29:11 +020070 { "DMA_CNTLch7", 0x274, 16 },
Anand Gadiyar0936fb52010-07-08 14:02:59 +053071 { "DMA_ADDRch7", 0x278, 32 },
72 { "DMA_COUNTch7", 0x27C, 32 },
Felipe Balbi449a7e92014-11-26 08:01:36 -060073#ifndef CONFIG_BLACKFIN
74 { "ConfigData", MUSB_CONFIGDATA,8 },
75 { "BabbleCtl", MUSB_BABBLE_CTL,8 },
76 { "TxFIFOsz", MUSB_TXFIFOSZ, 8 },
77 { "RxFIFOsz", MUSB_RXFIFOSZ, 8 },
78 { "TxFIFOadd", MUSB_TXFIFOADD, 16 },
79 { "RxFIFOadd", MUSB_RXFIFOADD, 16 },
80 { "EPInfo", MUSB_EPINFO, 8 },
81 { "RAMInfo", MUSB_RAMINFO, 8 },
82#endif
Felipe Balbi7f7f9e22010-03-12 10:29:11 +020083 { } /* Terminating Entry */
84};
85
Felipe Balbi7f7f9e22010-03-12 10:29:11 +020086static int musb_regdump_show(struct seq_file *s, void *unused)
87{
88 struct musb *musb = s->private;
89 unsigned i;
90
91 seq_printf(s, "MUSB (M)HDRC Register Dump\n");
Bin Liu7b6c1b42017-01-10 10:46:00 -060092 pm_runtime_get_sync(musb->controller);
Felipe Balbi7f7f9e22010-03-12 10:29:11 +020093
94 for (i = 0; i < ARRAY_SIZE(musb_regmap); i++) {
95 switch (musb_regmap[i].size) {
96 case 8:
97 seq_printf(s, "%-12s: %02x\n", musb_regmap[i].name,
98 musb_readb(musb->mregs, musb_regmap[i].offset));
99 break;
100 case 16:
101 seq_printf(s, "%-12s: %04x\n", musb_regmap[i].name,
102 musb_readw(musb->mregs, musb_regmap[i].offset));
103 break;
104 case 32:
105 seq_printf(s, "%-12s: %08x\n", musb_regmap[i].name,
106 musb_readl(musb->mregs, musb_regmap[i].offset));
107 break;
108 }
109 }
110
Bin Liu7b6c1b42017-01-10 10:46:00 -0600111 pm_runtime_mark_last_busy(musb->controller);
112 pm_runtime_put_autosuspend(musb->controller);
Felipe Balbi7f7f9e22010-03-12 10:29:11 +0200113 return 0;
114}
115
116static int musb_regdump_open(struct inode *inode, struct file *file)
117{
118 return single_open(file, musb_regdump_show, inode->i_private);
119}
120
121static int musb_test_mode_show(struct seq_file *s, void *unused)
122{
123 struct musb *musb = s->private;
124 unsigned test;
125
Bin Liu7b6c1b42017-01-10 10:46:00 -0600126 pm_runtime_get_sync(musb->controller);
Felipe Balbi7f7f9e22010-03-12 10:29:11 +0200127 test = musb_readb(musb->mregs, MUSB_TESTMODE);
Bin Liu7b6c1b42017-01-10 10:46:00 -0600128 pm_runtime_mark_last_busy(musb->controller);
129 pm_runtime_put_autosuspend(musb->controller);
Felipe Balbi7f7f9e22010-03-12 10:29:11 +0200130
Pali Rohár7eebe4e2017-02-01 21:30:22 -0600131 if (test == (MUSB_TEST_FORCE_HOST | MUSB_TEST_FORCE_FS))
132 seq_printf(s, "force host full-speed\n");
133
134 else if (test == (MUSB_TEST_FORCE_HOST | MUSB_TEST_FORCE_HS))
135 seq_printf(s, "force host high-speed\n");
136
137 else if (test == MUSB_TEST_FORCE_HOST)
Felipe Balbi7f7f9e22010-03-12 10:29:11 +0200138 seq_printf(s, "force host\n");
139
Pali Rohár7eebe4e2017-02-01 21:30:22 -0600140 else if (test == MUSB_TEST_FIFO_ACCESS)
Felipe Balbi7f7f9e22010-03-12 10:29:11 +0200141 seq_printf(s, "fifo access\n");
142
Pali Rohár7eebe4e2017-02-01 21:30:22 -0600143 else if (test == MUSB_TEST_FORCE_FS)
Felipe Balbi7f7f9e22010-03-12 10:29:11 +0200144 seq_printf(s, "force full-speed\n");
145
Pali Rohár7eebe4e2017-02-01 21:30:22 -0600146 else if (test == MUSB_TEST_FORCE_HS)
Felipe Balbi7f7f9e22010-03-12 10:29:11 +0200147 seq_printf(s, "force high-speed\n");
148
Pali Rohár7eebe4e2017-02-01 21:30:22 -0600149 else if (test == MUSB_TEST_PACKET)
Felipe Balbi7f7f9e22010-03-12 10:29:11 +0200150 seq_printf(s, "test packet\n");
151
Pali Rohár7eebe4e2017-02-01 21:30:22 -0600152 else if (test == MUSB_TEST_K)
Felipe Balbi7f7f9e22010-03-12 10:29:11 +0200153 seq_printf(s, "test K\n");
154
Pali Rohár7eebe4e2017-02-01 21:30:22 -0600155 else if (test == MUSB_TEST_J)
Felipe Balbi7f7f9e22010-03-12 10:29:11 +0200156 seq_printf(s, "test J\n");
157
Pali Rohár7eebe4e2017-02-01 21:30:22 -0600158 else if (test == MUSB_TEST_SE0_NAK)
Felipe Balbi7f7f9e22010-03-12 10:29:11 +0200159 seq_printf(s, "test SE0 NAK\n");
160
161 return 0;
162}
163
164static const struct file_operations musb_regdump_fops = {
165 .open = musb_regdump_open,
166 .read = seq_read,
167 .llseek = seq_lseek,
168 .release = single_release,
169};
170
171static int musb_test_mode_open(struct inode *inode, struct file *file)
172{
Felipe Balbi7f7f9e22010-03-12 10:29:11 +0200173 return single_open(file, musb_test_mode_show, inode->i_private);
174}
175
176static ssize_t musb_test_mode_write(struct file *file,
177 const char __user *ubuf, size_t count, loff_t *ppos)
178{
Mathias Nyman024cfa52010-09-06 13:52:01 +0300179 struct seq_file *s = file->private_data;
180 struct musb *musb = s->private;
Bin Liu205845e2015-03-24 15:09:25 -0500181 u8 test;
Pali Rohár7eebe4e2017-02-01 21:30:22 -0600182 char buf[24];
Felipe Balbi7f7f9e22010-03-12 10:29:11 +0200183
Bin Liu7b6c1b42017-01-10 10:46:00 -0600184 pm_runtime_get_sync(musb->controller);
Bin Liu205845e2015-03-24 15:09:25 -0500185 test = musb_readb(musb->mregs, MUSB_TESTMODE);
186 if (test) {
187 dev_err(musb->controller, "Error: test mode is already set. "
188 "Please do USB Bus Reset to start a new test.\n");
Bin Liu7b6c1b42017-01-10 10:46:00 -0600189 goto ret;
Bin Liu205845e2015-03-24 15:09:25 -0500190 }
191
Felipe Balbi7f7f9e22010-03-12 10:29:11 +0200192 memset(buf, 0x00, sizeof(buf));
193
Markus Pargmannfe198e32015-01-14 16:08:38 +0100194 if (copy_from_user(buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
Felipe Balbi7f7f9e22010-03-12 10:29:11 +0200195 return -EFAULT;
196
Pali Rohár7eebe4e2017-02-01 21:30:22 -0600197 if (strstarts(buf, "force host full-speed"))
198 test = MUSB_TEST_FORCE_HOST | MUSB_TEST_FORCE_FS;
199
200 else if (strstarts(buf, "force host high-speed"))
201 test = MUSB_TEST_FORCE_HOST | MUSB_TEST_FORCE_HS;
202
203 else if (strstarts(buf, "force host"))
Felipe Balbi7f7f9e22010-03-12 10:29:11 +0200204 test = MUSB_TEST_FORCE_HOST;
205
Pali Rohár7eebe4e2017-02-01 21:30:22 -0600206 else if (strstarts(buf, "fifo access"))
Felipe Balbi7f7f9e22010-03-12 10:29:11 +0200207 test = MUSB_TEST_FIFO_ACCESS;
208
Pali Rohár7eebe4e2017-02-01 21:30:22 -0600209 else if (strstarts(buf, "force full-speed"))
Felipe Balbi7f7f9e22010-03-12 10:29:11 +0200210 test = MUSB_TEST_FORCE_FS;
211
Pali Rohár7eebe4e2017-02-01 21:30:22 -0600212 else if (strstarts(buf, "force high-speed"))
Felipe Balbi7f7f9e22010-03-12 10:29:11 +0200213 test = MUSB_TEST_FORCE_HS;
214
Pali Rohár7eebe4e2017-02-01 21:30:22 -0600215 else if (strstarts(buf, "test packet")) {
Felipe Balbi7f7f9e22010-03-12 10:29:11 +0200216 test = MUSB_TEST_PACKET;
217 musb_load_testpacket(musb);
218 }
219
Pali Rohár7eebe4e2017-02-01 21:30:22 -0600220 else if (strstarts(buf, "test K"))
Felipe Balbi7f7f9e22010-03-12 10:29:11 +0200221 test = MUSB_TEST_K;
222
Pali Rohár7eebe4e2017-02-01 21:30:22 -0600223 else if (strstarts(buf, "test J"))
Felipe Balbi7f7f9e22010-03-12 10:29:11 +0200224 test = MUSB_TEST_J;
225
Pali Rohár7eebe4e2017-02-01 21:30:22 -0600226 else if (strstarts(buf, "test SE0 NAK"))
Felipe Balbi7f7f9e22010-03-12 10:29:11 +0200227 test = MUSB_TEST_SE0_NAK;
228
229 musb_writeb(musb->mregs, MUSB_TESTMODE, test);
230
Bin Liu7b6c1b42017-01-10 10:46:00 -0600231ret:
232 pm_runtime_mark_last_busy(musb->controller);
233 pm_runtime_put_autosuspend(musb->controller);
Felipe Balbi7f7f9e22010-03-12 10:29:11 +0200234 return count;
235}
236
237static const struct file_operations musb_test_mode_fops = {
238 .open = musb_test_mode_open,
239 .write = musb_test_mode_write,
240 .read = seq_read,
241 .llseek = seq_lseek,
242 .release = single_release,
243};
244
Bin Liuffc1d2992015-03-24 15:19:56 -0500245static int musb_softconnect_show(struct seq_file *s, void *unused)
246{
247 struct musb *musb = s->private;
248 u8 reg;
249 int connect;
250
251 switch (musb->xceiv->otg->state) {
252 case OTG_STATE_A_HOST:
253 case OTG_STATE_A_WAIT_BCON:
Bin Liu7b6c1b42017-01-10 10:46:00 -0600254 pm_runtime_get_sync(musb->controller);
255
Bin Liuffc1d2992015-03-24 15:19:56 -0500256 reg = musb_readb(musb->mregs, MUSB_DEVCTL);
257 connect = reg & MUSB_DEVCTL_SESSION ? 1 : 0;
Bin Liu7b6c1b42017-01-10 10:46:00 -0600258
259 pm_runtime_mark_last_busy(musb->controller);
260 pm_runtime_put_autosuspend(musb->controller);
Bin Liuffc1d2992015-03-24 15:19:56 -0500261 break;
262 default:
263 connect = -1;
264 }
265
266 seq_printf(s, "%d\n", connect);
267
268 return 0;
269}
270
271static int musb_softconnect_open(struct inode *inode, struct file *file)
272{
273 return single_open(file, musb_softconnect_show, inode->i_private);
274}
275
276static ssize_t musb_softconnect_write(struct file *file,
277 const char __user *ubuf, size_t count, loff_t *ppos)
278{
279 struct seq_file *s = file->private_data;
280 struct musb *musb = s->private;
281 char buf[2];
282 u8 reg;
283
284 memset(buf, 0x00, sizeof(buf));
285
286 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
287 return -EFAULT;
288
Bin Liu7b6c1b42017-01-10 10:46:00 -0600289 pm_runtime_get_sync(musb->controller);
Bin Liuffc1d2992015-03-24 15:19:56 -0500290 if (!strncmp(buf, "0", 1)) {
291 switch (musb->xceiv->otg->state) {
292 case OTG_STATE_A_HOST:
293 musb_root_disconnect(musb);
294 reg = musb_readb(musb->mregs, MUSB_DEVCTL);
295 reg &= ~MUSB_DEVCTL_SESSION;
296 musb_writeb(musb->mregs, MUSB_DEVCTL, reg);
297 break;
298 default:
299 break;
300 }
301 } else if (!strncmp(buf, "1", 1)) {
302 switch (musb->xceiv->otg->state) {
303 case OTG_STATE_A_WAIT_BCON:
304 /*
305 * musb_save_context() called in musb_runtime_suspend()
306 * might cache devctl with SESSION bit cleared during
307 * soft-disconnect, so specifically set SESSION bit
308 * here to preserve it for musb_runtime_resume().
309 */
310 musb->context.devctl |= MUSB_DEVCTL_SESSION;
311 reg = musb_readb(musb->mregs, MUSB_DEVCTL);
312 reg |= MUSB_DEVCTL_SESSION;
313 musb_writeb(musb->mregs, MUSB_DEVCTL, reg);
314 break;
315 default:
316 break;
317 }
318 }
319
Bin Liu7b6c1b42017-01-10 10:46:00 -0600320 pm_runtime_mark_last_busy(musb->controller);
321 pm_runtime_put_autosuspend(musb->controller);
Bin Liuffc1d2992015-03-24 15:19:56 -0500322 return count;
323}
324
325/*
326 * In host mode, connect/disconnect the bus without physically
327 * remove the devices.
328 */
329static const struct file_operations musb_softconnect_fops = {
330 .open = musb_softconnect_open,
331 .write = musb_softconnect_write,
332 .read = seq_read,
333 .llseek = seq_lseek,
334 .release = single_release,
335};
336
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500337int musb_init_debugfs(struct musb *musb)
Felipe Balbi7f7f9e22010-03-12 10:29:11 +0200338{
339 struct dentry *root;
340 struct dentry *file;
341 int ret;
342
Ajay Kumar Gupta8d2421e2012-08-31 11:09:50 +0000343 root = debugfs_create_dir(dev_name(musb->controller), NULL);
Felipe Balbi6c2abcd2012-01-31 14:19:00 +0200344 if (!root) {
345 ret = -ENOMEM;
Felipe Balbi7f7f9e22010-03-12 10:29:11 +0200346 goto err0;
347 }
348
349 file = debugfs_create_file("regdump", S_IRUGO, root, musb,
350 &musb_regdump_fops);
Felipe Balbi6c2abcd2012-01-31 14:19:00 +0200351 if (!file) {
352 ret = -ENOMEM;
Felipe Balbi7f7f9e22010-03-12 10:29:11 +0200353 goto err1;
354 }
355
356 file = debugfs_create_file("testmode", S_IRUGO | S_IWUSR,
357 root, musb, &musb_test_mode_fops);
Felipe Balbi6c2abcd2012-01-31 14:19:00 +0200358 if (!file) {
359 ret = -ENOMEM;
Felipe Balbi7f7f9e22010-03-12 10:29:11 +0200360 goto err1;
361 }
362
Bin Liuffc1d2992015-03-24 15:19:56 -0500363 file = debugfs_create_file("softconnect", S_IRUGO | S_IWUSR,
364 root, musb, &musb_softconnect_fops);
365 if (!file) {
366 ret = -ENOMEM;
367 goto err1;
368 }
369
Ajay Kumar Gupta8d2421e2012-08-31 11:09:50 +0000370 musb->debugfs_root = root;
Felipe Balbi7f7f9e22010-03-12 10:29:11 +0200371
372 return 0;
373
374err1:
375 debugfs_remove_recursive(root);
376
377err0:
378 return ret;
379}
380
Felipe Balbib0f9da72010-03-25 13:25:18 +0200381void /* __init_or_exit */ musb_exit_debugfs(struct musb *musb)
Felipe Balbi7f7f9e22010-03-12 10:29:11 +0200382{
Ajay Kumar Gupta8d2421e2012-08-31 11:09:50 +0000383 debugfs_remove_recursive(musb->debugfs_root);
Felipe Balbi7f7f9e22010-03-12 10:29:11 +0200384}