blob: d77eac7b239d20ea91eccd36f26c8bac156823e1 [file] [log] [blame]
Felipe Balbi72246da2011-08-19 18:10:58 +03001/**
2 * debugfs.c - DesignWare USB3 DRD Controller DebugFS file
3 *
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
Felipe Balbi72246da2011-08-19 18:10:58 +03005 *
6 * Authors: Felipe Balbi <balbi@ti.com>,
7 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
8 *
Felipe Balbi5945f782013-06-30 14:15:11 +03009 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 of
11 * the License as published by the Free Software Foundation.
Felipe Balbi72246da2011-08-19 18:10:58 +030012 *
Felipe Balbi5945f782013-06-30 14:15:11 +030013 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
Felipe Balbi72246da2011-08-19 18:10:58 +030017 */
18
19#include <linux/kernel.h>
20#include <linux/slab.h>
21#include <linux/ptrace.h>
22#include <linux/types.h>
23#include <linux/spinlock.h>
24#include <linux/debugfs.h>
25#include <linux/seq_file.h>
26#include <linux/delay.h>
Felipe Balbi25b8ff62011-11-04 12:32:47 +020027#include <linux/uaccess.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030028
Felipe Balbi080d9212012-01-02 18:38:30 +020029#include <linux/usb/ch9.h>
30
Felipe Balbi72246da2011-08-19 18:10:58 +030031#include "core.h"
32#include "gadget.h"
33#include "io.h"
Felipe Balbi8becf272011-11-04 12:40:05 +020034#include "debug.h"
Felipe Balbi72246da2011-08-19 18:10:58 +030035
Felipe Balbi72246da2011-08-19 18:10:58 +030036#define dump_register(nm) \
37{ \
38 .name = __stringify(nm), \
Felipe Balbi2eb88012016-04-12 16:53:39 +030039 .offset = DWC3_ ##nm, \
Felipe Balbi72246da2011-08-19 18:10:58 +030040}
41
Felipe Balbi2eb88012016-04-12 16:53:39 +030042#define dump_ep_register_set(n) \
43 { \
44 .name = "DEPCMDPAR2("__stringify(n)")", \
45 .offset = DWC3_DEP_BASE(n) + \
46 DWC3_DEPCMDPAR2, \
47 }, \
48 { \
49 .name = "DEPCMDPAR1("__stringify(n)")", \
50 .offset = DWC3_DEP_BASE(n) + \
51 DWC3_DEPCMDPAR1, \
52 }, \
53 { \
54 .name = "DEPCMDPAR0("__stringify(n)")", \
55 .offset = DWC3_DEP_BASE(n) + \
56 DWC3_DEPCMDPAR0, \
57 }, \
58 { \
59 .name = "DEPCMD("__stringify(n)")", \
60 .offset = DWC3_DEP_BASE(n) + \
61 DWC3_DEPCMD, \
62 }
63
64
Felipe Balbi4ec0ddb2013-02-22 16:17:31 +020065static const struct debugfs_reg32 dwc3_regs[] = {
Felipe Balbi72246da2011-08-19 18:10:58 +030066 dump_register(GSBUSCFG0),
67 dump_register(GSBUSCFG1),
68 dump_register(GTXTHRCFG),
69 dump_register(GRXTHRCFG),
70 dump_register(GCTL),
71 dump_register(GEVTEN),
72 dump_register(GSTS),
William Wu475c8be2016-05-13 18:13:46 +080073 dump_register(GUCTL1),
Felipe Balbi72246da2011-08-19 18:10:58 +030074 dump_register(GSNPSID),
75 dump_register(GGPIO),
76 dump_register(GUID),
77 dump_register(GUCTL),
78 dump_register(GBUSERRADDR0),
79 dump_register(GBUSERRADDR1),
80 dump_register(GPRTBIMAP0),
81 dump_register(GPRTBIMAP1),
82 dump_register(GHWPARAMS0),
83 dump_register(GHWPARAMS1),
84 dump_register(GHWPARAMS2),
85 dump_register(GHWPARAMS3),
86 dump_register(GHWPARAMS4),
87 dump_register(GHWPARAMS5),
88 dump_register(GHWPARAMS6),
89 dump_register(GHWPARAMS7),
90 dump_register(GDBGFIFOSPACE),
91 dump_register(GDBGLTSSM),
92 dump_register(GPRTBIMAP_HS0),
93 dump_register(GPRTBIMAP_HS1),
94 dump_register(GPRTBIMAP_FS0),
95 dump_register(GPRTBIMAP_FS1),
96
97 dump_register(GUSB2PHYCFG(0)),
98 dump_register(GUSB2PHYCFG(1)),
99 dump_register(GUSB2PHYCFG(2)),
100 dump_register(GUSB2PHYCFG(3)),
101 dump_register(GUSB2PHYCFG(4)),
102 dump_register(GUSB2PHYCFG(5)),
103 dump_register(GUSB2PHYCFG(6)),
104 dump_register(GUSB2PHYCFG(7)),
105 dump_register(GUSB2PHYCFG(8)),
106 dump_register(GUSB2PHYCFG(9)),
107 dump_register(GUSB2PHYCFG(10)),
108 dump_register(GUSB2PHYCFG(11)),
109 dump_register(GUSB2PHYCFG(12)),
110 dump_register(GUSB2PHYCFG(13)),
111 dump_register(GUSB2PHYCFG(14)),
112 dump_register(GUSB2PHYCFG(15)),
113
114 dump_register(GUSB2I2CCTL(0)),
115 dump_register(GUSB2I2CCTL(1)),
116 dump_register(GUSB2I2CCTL(2)),
117 dump_register(GUSB2I2CCTL(3)),
118 dump_register(GUSB2I2CCTL(4)),
119 dump_register(GUSB2I2CCTL(5)),
120 dump_register(GUSB2I2CCTL(6)),
121 dump_register(GUSB2I2CCTL(7)),
122 dump_register(GUSB2I2CCTL(8)),
123 dump_register(GUSB2I2CCTL(9)),
124 dump_register(GUSB2I2CCTL(10)),
125 dump_register(GUSB2I2CCTL(11)),
126 dump_register(GUSB2I2CCTL(12)),
127 dump_register(GUSB2I2CCTL(13)),
128 dump_register(GUSB2I2CCTL(14)),
129 dump_register(GUSB2I2CCTL(15)),
130
131 dump_register(GUSB2PHYACC(0)),
132 dump_register(GUSB2PHYACC(1)),
133 dump_register(GUSB2PHYACC(2)),
134 dump_register(GUSB2PHYACC(3)),
135 dump_register(GUSB2PHYACC(4)),
136 dump_register(GUSB2PHYACC(5)),
137 dump_register(GUSB2PHYACC(6)),
138 dump_register(GUSB2PHYACC(7)),
139 dump_register(GUSB2PHYACC(8)),
140 dump_register(GUSB2PHYACC(9)),
141 dump_register(GUSB2PHYACC(10)),
142 dump_register(GUSB2PHYACC(11)),
143 dump_register(GUSB2PHYACC(12)),
144 dump_register(GUSB2PHYACC(13)),
145 dump_register(GUSB2PHYACC(14)),
146 dump_register(GUSB2PHYACC(15)),
147
148 dump_register(GUSB3PIPECTL(0)),
149 dump_register(GUSB3PIPECTL(1)),
150 dump_register(GUSB3PIPECTL(2)),
151 dump_register(GUSB3PIPECTL(3)),
152 dump_register(GUSB3PIPECTL(4)),
153 dump_register(GUSB3PIPECTL(5)),
154 dump_register(GUSB3PIPECTL(6)),
155 dump_register(GUSB3PIPECTL(7)),
156 dump_register(GUSB3PIPECTL(8)),
157 dump_register(GUSB3PIPECTL(9)),
158 dump_register(GUSB3PIPECTL(10)),
159 dump_register(GUSB3PIPECTL(11)),
160 dump_register(GUSB3PIPECTL(12)),
161 dump_register(GUSB3PIPECTL(13)),
162 dump_register(GUSB3PIPECTL(14)),
163 dump_register(GUSB3PIPECTL(15)),
164
165 dump_register(GTXFIFOSIZ(0)),
166 dump_register(GTXFIFOSIZ(1)),
167 dump_register(GTXFIFOSIZ(2)),
168 dump_register(GTXFIFOSIZ(3)),
169 dump_register(GTXFIFOSIZ(4)),
170 dump_register(GTXFIFOSIZ(5)),
171 dump_register(GTXFIFOSIZ(6)),
172 dump_register(GTXFIFOSIZ(7)),
173 dump_register(GTXFIFOSIZ(8)),
174 dump_register(GTXFIFOSIZ(9)),
175 dump_register(GTXFIFOSIZ(10)),
176 dump_register(GTXFIFOSIZ(11)),
177 dump_register(GTXFIFOSIZ(12)),
178 dump_register(GTXFIFOSIZ(13)),
179 dump_register(GTXFIFOSIZ(14)),
180 dump_register(GTXFIFOSIZ(15)),
181 dump_register(GTXFIFOSIZ(16)),
182 dump_register(GTXFIFOSIZ(17)),
183 dump_register(GTXFIFOSIZ(18)),
184 dump_register(GTXFIFOSIZ(19)),
185 dump_register(GTXFIFOSIZ(20)),
186 dump_register(GTXFIFOSIZ(21)),
187 dump_register(GTXFIFOSIZ(22)),
188 dump_register(GTXFIFOSIZ(23)),
189 dump_register(GTXFIFOSIZ(24)),
190 dump_register(GTXFIFOSIZ(25)),
191 dump_register(GTXFIFOSIZ(26)),
192 dump_register(GTXFIFOSIZ(27)),
193 dump_register(GTXFIFOSIZ(28)),
194 dump_register(GTXFIFOSIZ(29)),
195 dump_register(GTXFIFOSIZ(30)),
196 dump_register(GTXFIFOSIZ(31)),
197
198 dump_register(GRXFIFOSIZ(0)),
199 dump_register(GRXFIFOSIZ(1)),
200 dump_register(GRXFIFOSIZ(2)),
201 dump_register(GRXFIFOSIZ(3)),
202 dump_register(GRXFIFOSIZ(4)),
203 dump_register(GRXFIFOSIZ(5)),
204 dump_register(GRXFIFOSIZ(6)),
205 dump_register(GRXFIFOSIZ(7)),
206 dump_register(GRXFIFOSIZ(8)),
207 dump_register(GRXFIFOSIZ(9)),
208 dump_register(GRXFIFOSIZ(10)),
209 dump_register(GRXFIFOSIZ(11)),
210 dump_register(GRXFIFOSIZ(12)),
211 dump_register(GRXFIFOSIZ(13)),
212 dump_register(GRXFIFOSIZ(14)),
213 dump_register(GRXFIFOSIZ(15)),
214 dump_register(GRXFIFOSIZ(16)),
215 dump_register(GRXFIFOSIZ(17)),
216 dump_register(GRXFIFOSIZ(18)),
217 dump_register(GRXFIFOSIZ(19)),
218 dump_register(GRXFIFOSIZ(20)),
219 dump_register(GRXFIFOSIZ(21)),
220 dump_register(GRXFIFOSIZ(22)),
221 dump_register(GRXFIFOSIZ(23)),
222 dump_register(GRXFIFOSIZ(24)),
223 dump_register(GRXFIFOSIZ(25)),
224 dump_register(GRXFIFOSIZ(26)),
225 dump_register(GRXFIFOSIZ(27)),
226 dump_register(GRXFIFOSIZ(28)),
227 dump_register(GRXFIFOSIZ(29)),
228 dump_register(GRXFIFOSIZ(30)),
229 dump_register(GRXFIFOSIZ(31)),
230
231 dump_register(GEVNTADRLO(0)),
232 dump_register(GEVNTADRHI(0)),
233 dump_register(GEVNTSIZ(0)),
234 dump_register(GEVNTCOUNT(0)),
235
236 dump_register(GHWPARAMS8),
237 dump_register(DCFG),
238 dump_register(DCTL),
239 dump_register(DEVTEN),
240 dump_register(DSTS),
241 dump_register(DGCMDPAR),
242 dump_register(DGCMD),
243 dump_register(DALEPENA),
244
Felipe Balbi2eb88012016-04-12 16:53:39 +0300245 dump_ep_register_set(0),
246 dump_ep_register_set(1),
247 dump_ep_register_set(2),
248 dump_ep_register_set(3),
249 dump_ep_register_set(4),
250 dump_ep_register_set(5),
251 dump_ep_register_set(6),
252 dump_ep_register_set(7),
253 dump_ep_register_set(8),
254 dump_ep_register_set(9),
255 dump_ep_register_set(10),
256 dump_ep_register_set(11),
257 dump_ep_register_set(12),
258 dump_ep_register_set(13),
259 dump_ep_register_set(14),
260 dump_ep_register_set(15),
261 dump_ep_register_set(16),
262 dump_ep_register_set(17),
263 dump_ep_register_set(18),
264 dump_ep_register_set(19),
265 dump_ep_register_set(20),
266 dump_ep_register_set(21),
267 dump_ep_register_set(22),
268 dump_ep_register_set(23),
269 dump_ep_register_set(24),
270 dump_ep_register_set(25),
271 dump_ep_register_set(26),
272 dump_ep_register_set(27),
273 dump_ep_register_set(28),
274 dump_ep_register_set(29),
275 dump_ep_register_set(30),
276 dump_ep_register_set(31),
Felipe Balbi72246da2011-08-19 18:10:58 +0300277
278 dump_register(OCFG),
279 dump_register(OCTL),
George Cheriand4436c32013-03-14 16:05:24 +0530280 dump_register(OEVT),
Felipe Balbi72246da2011-08-19 18:10:58 +0300281 dump_register(OEVTEN),
282 dump_register(OSTS),
283};
284
Felipe Balbi0b9fe322011-10-17 08:50:39 +0300285static int dwc3_mode_show(struct seq_file *s, void *unused)
286{
287 struct dwc3 *dwc = s->private;
288 unsigned long flags;
289 u32 reg;
290
291 spin_lock_irqsave(&dwc->lock, flags);
292 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
293 spin_unlock_irqrestore(&dwc->lock, flags);
294
295 switch (DWC3_GCTL_PRTCAP(reg)) {
296 case DWC3_GCTL_PRTCAP_HOST:
297 seq_printf(s, "host\n");
298 break;
299 case DWC3_GCTL_PRTCAP_DEVICE:
300 seq_printf(s, "device\n");
301 break;
302 case DWC3_GCTL_PRTCAP_OTG:
Felipe Balbi2df301c2017-03-31 14:10:40 +0300303 seq_printf(s, "otg\n");
Felipe Balbi0b9fe322011-10-17 08:50:39 +0300304 break;
305 default:
306 seq_printf(s, "UNKNOWN %08x\n", DWC3_GCTL_PRTCAP(reg));
307 }
308
309 return 0;
310}
311
312static int dwc3_mode_open(struct inode *inode, struct file *file)
313{
314 return single_open(file, dwc3_mode_show, inode->i_private);
315}
316
317static ssize_t dwc3_mode_write(struct file *file,
318 const char __user *ubuf, size_t count, loff_t *ppos)
319{
320 struct seq_file *s = file->private_data;
321 struct dwc3 *dwc = s->private;
322 unsigned long flags;
Sebastian Andrzej Siewior3140e8cb2011-10-31 22:25:40 +0100323 u32 mode = 0;
Felipe Balbi0b9fe322011-10-17 08:50:39 +0300324 char buf[32];
325
326 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
327 return -EFAULT;
328
Felipe Balbi0b9fe322011-10-17 08:50:39 +0300329 if (!strncmp(buf, "host", 4))
Sebastian Andrzej Siewior3140e8cb2011-10-31 22:25:40 +0100330 mode |= DWC3_GCTL_PRTCAP_HOST;
Felipe Balbi0b9fe322011-10-17 08:50:39 +0300331
332 if (!strncmp(buf, "device", 6))
Sebastian Andrzej Siewior3140e8cb2011-10-31 22:25:40 +0100333 mode |= DWC3_GCTL_PRTCAP_DEVICE;
Felipe Balbi0b9fe322011-10-17 08:50:39 +0300334
335 if (!strncmp(buf, "otg", 3))
Sebastian Andrzej Siewior3140e8cb2011-10-31 22:25:40 +0100336 mode |= DWC3_GCTL_PRTCAP_OTG;
Felipe Balbi0b9fe322011-10-17 08:50:39 +0300337
Sebastian Andrzej Siewior3140e8cb2011-10-31 22:25:40 +0100338 if (mode) {
339 spin_lock_irqsave(&dwc->lock, flags);
340 dwc3_set_mode(dwc, mode);
341 spin_unlock_irqrestore(&dwc->lock, flags);
342 }
Felipe Balbi0b9fe322011-10-17 08:50:39 +0300343 return count;
344}
345
346static const struct file_operations dwc3_mode_fops = {
347 .open = dwc3_mode_open,
348 .write = dwc3_mode_write,
349 .read = seq_read,
350 .llseek = seq_lseek,
351 .release = single_release,
352};
353
Felipe Balbi080d9212012-01-02 18:38:30 +0200354static int dwc3_testmode_show(struct seq_file *s, void *unused)
355{
356 struct dwc3 *dwc = s->private;
357 unsigned long flags;
358 u32 reg;
359
360 spin_lock_irqsave(&dwc->lock, flags);
361 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
362 reg &= DWC3_DCTL_TSTCTRL_MASK;
363 reg >>= 1;
364 spin_unlock_irqrestore(&dwc->lock, flags);
365
366 switch (reg) {
367 case 0:
368 seq_printf(s, "no test\n");
369 break;
370 case TEST_J:
371 seq_printf(s, "test_j\n");
372 break;
373 case TEST_K:
374 seq_printf(s, "test_k\n");
375 break;
376 case TEST_SE0_NAK:
377 seq_printf(s, "test_se0_nak\n");
378 break;
379 case TEST_PACKET:
380 seq_printf(s, "test_packet\n");
381 break;
382 case TEST_FORCE_EN:
383 seq_printf(s, "test_force_enable\n");
384 break;
385 default:
386 seq_printf(s, "UNKNOWN %d\n", reg);
387 }
388
389 return 0;
390}
391
392static int dwc3_testmode_open(struct inode *inode, struct file *file)
393{
394 return single_open(file, dwc3_testmode_show, inode->i_private);
395}
396
397static ssize_t dwc3_testmode_write(struct file *file,
398 const char __user *ubuf, size_t count, loff_t *ppos)
399{
400 struct seq_file *s = file->private_data;
401 struct dwc3 *dwc = s->private;
402 unsigned long flags;
403 u32 testmode = 0;
404 char buf[32];
405
406 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
407 return -EFAULT;
408
409 if (!strncmp(buf, "test_j", 6))
410 testmode = TEST_J;
411 else if (!strncmp(buf, "test_k", 6))
412 testmode = TEST_K;
Gerard Cauvy09072542012-02-10 12:14:53 +0200413 else if (!strncmp(buf, "test_se0_nak", 12))
Felipe Balbi080d9212012-01-02 18:38:30 +0200414 testmode = TEST_SE0_NAK;
Gerard Cauvy09072542012-02-10 12:14:53 +0200415 else if (!strncmp(buf, "test_packet", 11))
Felipe Balbi080d9212012-01-02 18:38:30 +0200416 testmode = TEST_PACKET;
Gerard Cauvy09072542012-02-10 12:14:53 +0200417 else if (!strncmp(buf, "test_force_enable", 17))
Felipe Balbi080d9212012-01-02 18:38:30 +0200418 testmode = TEST_FORCE_EN;
419 else
420 testmode = 0;
421
422 spin_lock_irqsave(&dwc->lock, flags);
423 dwc3_gadget_set_test_mode(dwc, testmode);
424 spin_unlock_irqrestore(&dwc->lock, flags);
425
426 return count;
427}
428
429static const struct file_operations dwc3_testmode_fops = {
430 .open = dwc3_testmode_open,
431 .write = dwc3_testmode_write,
432 .read = seq_read,
433 .llseek = seq_lseek,
434 .release = single_release,
435};
436
Felipe Balbi138801a2012-01-02 19:25:16 +0200437static int dwc3_link_state_show(struct seq_file *s, void *unused)
438{
439 struct dwc3 *dwc = s->private;
440 unsigned long flags;
441 enum dwc3_link_state state;
442 u32 reg;
443
444 spin_lock_irqsave(&dwc->lock, flags);
445 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
446 state = DWC3_DSTS_USBLNKST(reg);
447 spin_unlock_irqrestore(&dwc->lock, flags);
448
Felipe Balbi26c9f3e2017-03-31 14:28:33 +0300449 seq_printf(s, "%s\n", dwc3_gadget_link_string(state));
Felipe Balbi138801a2012-01-02 19:25:16 +0200450
451 return 0;
452}
453
454static int dwc3_link_state_open(struct inode *inode, struct file *file)
455{
456 return single_open(file, dwc3_link_state_show, inode->i_private);
457}
458
459static ssize_t dwc3_link_state_write(struct file *file,
460 const char __user *ubuf, size_t count, loff_t *ppos)
461{
462 struct seq_file *s = file->private_data;
463 struct dwc3 *dwc = s->private;
464 unsigned long flags;
465 enum dwc3_link_state state = 0;
466 char buf[32];
467
468 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
469 return -EFAULT;
470
471 if (!strncmp(buf, "SS.Disabled", 11))
472 state = DWC3_LINK_STATE_SS_DIS;
473 else if (!strncmp(buf, "Rx.Detect", 9))
474 state = DWC3_LINK_STATE_RX_DET;
475 else if (!strncmp(buf, "SS.Inactive", 11))
476 state = DWC3_LINK_STATE_SS_INACT;
477 else if (!strncmp(buf, "Recovery", 8))
478 state = DWC3_LINK_STATE_RECOV;
479 else if (!strncmp(buf, "Compliance", 10))
480 state = DWC3_LINK_STATE_CMPLY;
481 else if (!strncmp(buf, "Loopback", 8))
482 state = DWC3_LINK_STATE_LPBK;
483 else
484 return -EINVAL;
485
486 spin_lock_irqsave(&dwc->lock, flags);
487 dwc3_gadget_set_link_state(dwc, state);
488 spin_unlock_irqrestore(&dwc->lock, flags);
489
490 return count;
491}
492
493static const struct file_operations dwc3_link_state_fops = {
494 .open = dwc3_link_state_open,
495 .write = dwc3_link_state_write,
496 .read = seq_read,
497 .llseek = seq_lseek,
498 .release = single_release,
499};
500
Felipe Balbi818ec3a2016-04-14 14:53:44 +0300501struct dwc3_ep_file_map {
502 char name[25];
503 int (*show)(struct seq_file *s, void *unused);
504};
Felipe Balbi72246da2011-08-19 18:10:58 +0300505
Felipe Balbi818ec3a2016-04-14 14:53:44 +0300506static int dwc3_tx_fifo_queue_show(struct seq_file *s, void *unused)
507{
508 struct dwc3_ep *dep = s->private;
509 struct dwc3 *dwc = dep->dwc;
510 unsigned long flags;
511 u32 val;
512
513 spin_lock_irqsave(&dwc->lock, flags);
514 val = dwc3_core_fifo_space(dep, DWC3_TXFIFOQ);
515 seq_printf(s, "%u\n", val);
516 spin_unlock_irqrestore(&dwc->lock, flags);
517
518 return 0;
519}
520
521static int dwc3_rx_fifo_queue_show(struct seq_file *s, void *unused)
522{
523 struct dwc3_ep *dep = s->private;
524 struct dwc3 *dwc = dep->dwc;
525 unsigned long flags;
526 u32 val;
527
528 spin_lock_irqsave(&dwc->lock, flags);
529 val = dwc3_core_fifo_space(dep, DWC3_RXFIFOQ);
530 seq_printf(s, "%u\n", val);
531 spin_unlock_irqrestore(&dwc->lock, flags);
532
533 return 0;
534}
535
536static int dwc3_tx_request_queue_show(struct seq_file *s, void *unused)
537{
538 struct dwc3_ep *dep = s->private;
539 struct dwc3 *dwc = dep->dwc;
540 unsigned long flags;
541 u32 val;
542
543 spin_lock_irqsave(&dwc->lock, flags);
544 val = dwc3_core_fifo_space(dep, DWC3_TXREQQ);
545 seq_printf(s, "%u\n", val);
546 spin_unlock_irqrestore(&dwc->lock, flags);
547
548 return 0;
549}
550
551static int dwc3_rx_request_queue_show(struct seq_file *s, void *unused)
552{
553 struct dwc3_ep *dep = s->private;
554 struct dwc3 *dwc = dep->dwc;
555 unsigned long flags;
556 u32 val;
557
558 spin_lock_irqsave(&dwc->lock, flags);
559 val = dwc3_core_fifo_space(dep, DWC3_RXREQQ);
560 seq_printf(s, "%u\n", val);
561 spin_unlock_irqrestore(&dwc->lock, flags);
562
563 return 0;
564}
565
566static int dwc3_rx_info_queue_show(struct seq_file *s, void *unused)
567{
568 struct dwc3_ep *dep = s->private;
569 struct dwc3 *dwc = dep->dwc;
570 unsigned long flags;
571 u32 val;
572
573 spin_lock_irqsave(&dwc->lock, flags);
574 val = dwc3_core_fifo_space(dep, DWC3_RXINFOQ);
575 seq_printf(s, "%u\n", val);
576 spin_unlock_irqrestore(&dwc->lock, flags);
577
578 return 0;
579}
580
581static int dwc3_descriptor_fetch_queue_show(struct seq_file *s, void *unused)
582{
583 struct dwc3_ep *dep = s->private;
584 struct dwc3 *dwc = dep->dwc;
585 unsigned long flags;
586 u32 val;
587
588 spin_lock_irqsave(&dwc->lock, flags);
589 val = dwc3_core_fifo_space(dep, DWC3_DESCFETCHQ);
590 seq_printf(s, "%u\n", val);
591 spin_unlock_irqrestore(&dwc->lock, flags);
592
593 return 0;
594}
595
596static int dwc3_event_queue_show(struct seq_file *s, void *unused)
597{
598 struct dwc3_ep *dep = s->private;
599 struct dwc3 *dwc = dep->dwc;
600 unsigned long flags;
601 u32 val;
602
603 spin_lock_irqsave(&dwc->lock, flags);
604 val = dwc3_core_fifo_space(dep, DWC3_EVENTQ);
605 seq_printf(s, "%u\n", val);
606 spin_unlock_irqrestore(&dwc->lock, flags);
607
608 return 0;
609}
610
611static int dwc3_ep_transfer_type_show(struct seq_file *s, void *unused)
612{
613 struct dwc3_ep *dep = s->private;
614 struct dwc3 *dwc = dep->dwc;
615 unsigned long flags;
616
617 spin_lock_irqsave(&dwc->lock, flags);
618 if (!(dep->flags & DWC3_EP_ENABLED) ||
619 !dep->endpoint.desc) {
620 seq_printf(s, "--\n");
621 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300622 }
623
Felipe Balbi818ec3a2016-04-14 14:53:44 +0300624 switch (usb_endpoint_type(dep->endpoint.desc)) {
625 case USB_ENDPOINT_XFER_CONTROL:
626 seq_printf(s, "control\n");
627 break;
628 case USB_ENDPOINT_XFER_ISOC:
629 seq_printf(s, "isochronous\n");
630 break;
631 case USB_ENDPOINT_XFER_BULK:
632 seq_printf(s, "bulk\n");
633 break;
634 case USB_ENDPOINT_XFER_INT:
635 seq_printf(s, "interrupt\n");
636 break;
637 default:
638 seq_printf(s, "--\n");
639 }
640
641out:
642 spin_unlock_irqrestore(&dwc->lock, flags);
643
644 return 0;
645}
646
647static inline const char *dwc3_trb_type_string(struct dwc3_trb *trb)
648{
649 switch (DWC3_TRBCTL_TYPE(trb->ctrl)) {
650 case DWC3_TRBCTL_NORMAL:
651 return "normal";
652 case DWC3_TRBCTL_CONTROL_SETUP:
653 return "control-setup";
654 case DWC3_TRBCTL_CONTROL_STATUS2:
655 return "control-status2";
656 case DWC3_TRBCTL_CONTROL_STATUS3:
657 return "control-status3";
658 case DWC3_TRBCTL_CONTROL_DATA:
659 return "control-data";
660 case DWC3_TRBCTL_ISOCHRONOUS_FIRST:
661 return "isoc-first";
662 case DWC3_TRBCTL_ISOCHRONOUS:
663 return "isoc";
664 case DWC3_TRBCTL_LINK_TRB:
665 return "link";
666 default:
667 return "UNKNOWN";
668 }
669}
670
671static int dwc3_ep_trb_ring_show(struct seq_file *s, void *unused)
672{
673 struct dwc3_ep *dep = s->private;
674 struct dwc3 *dwc = dep->dwc;
675 unsigned long flags;
676 int i;
677
678 spin_lock_irqsave(&dwc->lock, flags);
679 if (dep->number <= 1) {
680 seq_printf(s, "--\n");
681 goto out;
682 }
683
684 seq_printf(s, "enqueue pointer %d\n", dep->trb_enqueue);
685 seq_printf(s, "dequeue pointer %d\n", dep->trb_dequeue);
686 seq_printf(s, "\n--------------------------------------------------\n\n");
687 seq_printf(s, "buffer_addr,size,type,ioc,isp_imi,csp,chn,lst,hwo\n");
688
689 for (i = 0; i < DWC3_TRB_NUM; i++) {
690 struct dwc3_trb *trb = &dep->trb_pool[i];
691
692 seq_printf(s, "%08x%08x,%d,%s,%d,%d,%d,%d,%d,%d\n",
693 trb->bph, trb->bpl, trb->size,
694 dwc3_trb_type_string(trb),
695 !!(trb->ctrl & DWC3_TRB_CTRL_IOC),
696 !!(trb->ctrl & DWC3_TRB_CTRL_ISP_IMI),
697 !!(trb->ctrl & DWC3_TRB_CTRL_CSP),
698 !!(trb->ctrl & DWC3_TRB_CTRL_CHN),
699 !!(trb->ctrl & DWC3_TRB_CTRL_LST),
700 !!(trb->ctrl & DWC3_TRB_CTRL_HWO));
701 }
702
703out:
704 spin_unlock_irqrestore(&dwc->lock, flags);
705
706 return 0;
707}
708
709static struct dwc3_ep_file_map map[] = {
710 { "tx_fifo_queue", dwc3_tx_fifo_queue_show, },
711 { "rx_fifo_queue", dwc3_rx_fifo_queue_show, },
712 { "tx_request_queue", dwc3_tx_request_queue_show, },
713 { "rx_request_queue", dwc3_rx_request_queue_show, },
714 { "rx_info_queue", dwc3_rx_info_queue_show, },
715 { "descriptor_fetch_queue", dwc3_descriptor_fetch_queue_show, },
716 { "event_queue", dwc3_event_queue_show, },
717 { "transfer_type", dwc3_ep_transfer_type_show, },
718 { "trb_ring", dwc3_ep_trb_ring_show, },
719};
720
721static int dwc3_endpoint_open(struct inode *inode, struct file *file)
722{
723 const char *file_name = file_dentry(file)->d_iname;
724 struct dwc3_ep_file_map *f_map;
725 int i;
726
727 for (i = 0; i < ARRAY_SIZE(map); i++) {
728 f_map = &map[i];
729
730 if (strcmp(f_map->name, file_name) == 0)
731 break;
732 }
733
734 return single_open(file, f_map->show, inode->i_private);
735}
736
737static const struct file_operations dwc3_endpoint_fops = {
738 .open = dwc3_endpoint_open,
739 .read = seq_read,
740 .llseek = seq_lseek,
741 .release = single_release,
742};
743
744static void dwc3_debugfs_create_endpoint_file(struct dwc3_ep *dep,
745 struct dentry *parent, int type)
746{
747 struct dentry *file;
748 struct dwc3_ep_file_map *ep_file = &map[type];
749
750 file = debugfs_create_file(ep_file->name, S_IRUGO, parent, dep,
751 &dwc3_endpoint_fops);
752}
753
754static void dwc3_debugfs_create_endpoint_files(struct dwc3_ep *dep,
755 struct dentry *parent)
756{
757 int i;
758
759 for (i = 0; i < ARRAY_SIZE(map); i++)
760 dwc3_debugfs_create_endpoint_file(dep, parent, i);
761}
762
763static void dwc3_debugfs_create_endpoint_dir(struct dwc3_ep *dep,
764 struct dentry *parent)
765{
766 struct dentry *dir;
767
768 dir = debugfs_create_dir(dep->name, parent);
769 if (IS_ERR_OR_NULL(dir))
770 return;
771
772 dwc3_debugfs_create_endpoint_files(dep, dir);
773}
774
775static void dwc3_debugfs_create_endpoint_dirs(struct dwc3 *dwc,
776 struct dentry *parent)
777{
778 int i;
779
Bryan O'Donoghue47d39462017-01-31 20:58:10 +0000780 for (i = 0; i < dwc->num_eps; i++) {
781 struct dwc3_ep *dep = dwc->eps[i];
Felipe Balbi818ec3a2016-04-14 14:53:44 +0300782
783 if (!dep)
784 continue;
785
786 dwc3_debugfs_create_endpoint_dir(dep, parent);
787 }
788}
789
Du, Changbin4e9f3112016-04-12 19:10:18 +0800790void dwc3_debugfs_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300791{
792 struct dentry *root;
Du, Changbin4e9f3112016-04-12 19:10:18 +0800793 struct dentry *file;
Felipe Balbi72246da2011-08-19 18:10:58 +0300794
795 root = debugfs_create_dir(dev_name(dwc->dev), NULL);
Du, Changbin4e9f3112016-04-12 19:10:18 +0800796 if (IS_ERR_OR_NULL(root)) {
797 if (!root)
798 dev_err(dwc->dev, "Can't create debugfs root\n");
799 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300800 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300801 dwc->root = root;
802
Felipe Balbid76680242013-01-18 10:21:34 +0200803 dwc->regset = kzalloc(sizeof(*dwc->regset), GFP_KERNEL);
804 if (!dwc->regset) {
Du, Changbin4e9f3112016-04-12 19:10:18 +0800805 debugfs_remove_recursive(root);
806 return;
Felipe Balbid76680242013-01-18 10:21:34 +0200807 }
808
809 dwc->regset->regs = dwc3_regs;
810 dwc->regset->nregs = ARRAY_SIZE(dwc3_regs);
Felipe Balbi2eb88012016-04-12 16:53:39 +0300811 dwc->regset->base = dwc->regs - DWC3_GLOBALS_REGS_START;
Felipe Balbid76680242013-01-18 10:21:34 +0200812
813 file = debugfs_create_regset32("regdump", S_IRUGO, root, dwc->regset);
Du, Changbin4e9f3112016-04-12 19:10:18 +0800814 if (!file)
815 dev_dbg(dwc->dev, "Can't create debugfs regdump\n");
Felipe Balbi0b9fe322011-10-17 08:50:39 +0300816
Felipe Balbidbfff052013-02-22 16:24:49 +0200817 if (IS_ENABLED(CONFIG_USB_DWC3_DUAL_ROLE)) {
818 file = debugfs_create_file("mode", S_IRUGO | S_IWUSR, root,
819 dwc, &dwc3_mode_fops);
Du, Changbin4e9f3112016-04-12 19:10:18 +0800820 if (!file)
821 dev_dbg(dwc->dev, "Can't create debugfs mode\n");
Felipe Balbi0b9fe322011-10-17 08:50:39 +0300822 }
823
Felipe Balbidbfff052013-02-22 16:24:49 +0200824 if (IS_ENABLED(CONFIG_USB_DWC3_DUAL_ROLE) ||
825 IS_ENABLED(CONFIG_USB_DWC3_GADGET)) {
826 file = debugfs_create_file("testmode", S_IRUGO | S_IWUSR, root,
827 dwc, &dwc3_testmode_fops);
Du, Changbin4e9f3112016-04-12 19:10:18 +0800828 if (!file)
829 dev_dbg(dwc->dev, "Can't create debugfs testmode\n");
Felipe Balbi080d9212012-01-02 18:38:30 +0200830
Du, Changbin4e9f3112016-04-12 19:10:18 +0800831 file = debugfs_create_file("link_state", S_IRUGO | S_IWUSR,
832 root, dwc, &dwc3_link_state_fops);
833 if (!file)
834 dev_dbg(dwc->dev, "Can't create debugfs link_state\n");
Felipe Balbi818ec3a2016-04-14 14:53:44 +0300835
836 dwc3_debugfs_create_endpoint_dirs(dwc, root);
Felipe Balbi138801a2012-01-02 19:25:16 +0200837 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300838}
839
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500840void dwc3_debugfs_exit(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300841{
842 debugfs_remove_recursive(dwc->root);
Du, Changbine6bdf812016-04-12 16:24:34 +0800843 kfree(dwc->regset);
Felipe Balbi72246da2011-08-19 18:10:58 +0300844}