15 #include <linux/errno.h>
16 #include <linux/module.h>
19 #include <linux/usb/ch9.h>
22 #include <linux/serial_core.h>
28 #include <asm/fixmap.h>
55 static int dbgp_phys_port = 1;
60 static int dbgp_not_safe;
61 static unsigned int dbgp_endpoint_out;
62 static unsigned int dbgp_endpoint_in;
72 #define USB_DEBUG_DEVNUM 127
75 #define dbgp_printk printk
76 static void dbgp_ehci_status(
char *
str)
80 dbgp_printk(
"dbgp: %s\n", str);
81 dbgp_printk(
" Debug control: %08x",
readl(&ehci_debug->
control));
82 dbgp_printk(
" ehci cmd : %08x",
readl(&ehci_regs->
command));
83 dbgp_printk(
" ehci conf flg: %08x\n",
85 dbgp_printk(
" ehci status : %08x",
readl(&ehci_regs->
status));
86 dbgp_printk(
" ehci portsc : %08x\n",
90 static inline void dbgp_ehci_status(
char *
str) { }
91 static inline void dbgp_printk(
const char *
fmt, ...) { }
94 static inline u32 dbgp_len_update(
u32 x,
u32 len)
96 return (x & ~0x0f) | (len & 0x0f);
100 static struct kgdb_io kgdbdbgp_io_ops;
101 #define dbgp_kgdb_mode (dbg_io_ops == &kgdbdbgp_io_ops)
103 #define dbgp_kgdb_mode (0)
107 #define EARLY_HC_LENGTH(p) (0x00ff & (p))
114 #define USB_PID_OUT 0xe1
115 #define USB_PID_IN 0x69
116 #define USB_PID_SOF 0xa5
117 #define USB_PID_SETUP 0x2d
119 #define USB_PID_ACK 0xd2
120 #define USB_PID_NAK 0x5a
121 #define USB_PID_STALL 0x1e
122 #define USB_PID_NYET 0x96
124 #define USB_PID_DATA0 0xc3
125 #define USB_PID_DATA1 0x4b
126 #define USB_PID_DATA2 0x87
127 #define USB_PID_MDATA 0x0f
129 #define USB_PID_PREAMBLE 0x3c
130 #define USB_PID_ERR 0x3c
131 #define USB_PID_SPLIT 0x78
132 #define USB_PID_PING 0xb4
133 #define USB_PID_UNDEF_0 0xf0
135 #define USB_PID_DATA_TOGGLE 0x88
136 #define DBGP_CLAIM (DBGP_OWNER | DBGP_ENABLED | DBGP_INUSE)
138 #define PCI_CAP_ID_EHCI_DEBUG 0xa
140 #define HUB_ROOT_RESET_TIME 50
141 #define HUB_SHORT_RESET_TIME 10
142 #define HUB_LONG_RESET_TIME 200
143 #define HUB_RESET_TIMEOUT 500
145 #define DBGP_MAX_PACKET 8
146 #define DBGP_TIMEOUT (250 * 1000)
147 #define DBGP_LOOPS 1000
149 static inline u32 dbgp_pid_write_update(
u32 x,
u32 tok)
153 return (x & 0xffff0000) | (data0 << 8) | (tok & 0xff);
156 static inline u32 dbgp_pid_read_update(
u32 x,
u32 tok)
158 return (x & 0xffff0000) | (
USB_PID_DATA0 << 8) | (tok & 0xff);
161 static int dbgp_wait_until_complete(
void)
172 }
while (--loop > 0);
185 static inline void dbgp_mdelay(
int ms)
190 for (i = 0; i < 1000; i++)
195 static void dbgp_breath(
void)
200 static int dbgp_wait_until_done(
unsigned ctrl,
int loop)
207 ret = dbgp_wait_until_complete();
241 static inline void dbgp_set_data(
const void *
buf,
int size)
248 for (i = 0; i < 4 && i <
size; i++)
249 lo |= bytes[i] << (8*i);
250 for (; i < 8 && i <
size; i++)
251 hi |= bytes[i] << (8*(i - 4));
256 static inline void dbgp_get_data(
void *buf,
int size)
258 unsigned char *bytes =
buf;
264 for (i = 0; i < 4 && i <
size; i++)
265 bytes[i] = (lo >> (8*i)) & 0xff;
266 for (; i < 8 && i <
size; i++)
267 bytes[i] = (hi >> (8*(i - 4))) & 0xff;
271 const char *bytes,
int size)
286 ctrl = dbgp_len_update(ctrl, size);
290 dbgp_set_data(bytes, size);
298 static int dbgp_bulk_read(
unsigned devnum,
unsigned endpoint,
void *
data,
310 pids = dbgp_pid_read_update(pids,
USB_PID_IN);
313 ctrl = dbgp_len_update(ctrl, size);
319 ret = dbgp_wait_until_done(ctrl, loops);
325 dbgp_get_data(data, size);
329 static int dbgp_control_msg(
unsigned devnum,
int requesttype,
342 req.bRequestType = requesttype;
352 ctrl = dbgp_len_update(ctrl,
sizeof(
req));
357 dbgp_set_data(&
req,
sizeof(
req));
365 return dbgp_bulk_read(devnum, 0, data, size,
DBGP_LOOPS);
379 for (bytes = 0; bytes < 48 && pos >= 0x40; bytes++) {
410 for (bus = 0; bus < 256; bus++) {
411 for (slot = 0; slot < 32; slot++) {
412 for (func = 0; func < 8; func++) {
415 cap = __find_dbgp(bus, slot, func);
431 static int dbgp_ehci_startup(
void)
443 dbgp_ehci_status(
"EHCI startup");
460 }
while (--loop > 0);
463 dbgp_printk(
"ehci can not be started\n");
466 dbgp_printk(
"ehci started\n");
470 static int dbgp_ehci_controller_reset(
void)
472 int loop = 250 * 1000;
481 }
while ((cmd &
CMD_RESET) && (--loop > 0));
484 dbgp_printk(
"can not reset ehci\n");
487 dbgp_ehci_status(
"ehci reset done");
490 static int ehci_wait_for_port(
int port);
495 static int _dbgp_external_startup(
void)
503 int reset_port_tries = 1;
504 int try_hard_once = 1;
506 try_port_reset_again:
507 ret = dbgp_ehci_startup();
512 ret = ehci_wait_for_port(dbg_port);
526 dbgp_ehci_status(
"Trying to force debug port online");
528 dbgp_ehci_controller_reset();
529 goto try_port_reset_again;
530 }
else if (reset_port_tries--) {
531 goto try_port_reset_again;
533 dbgp_printk(
"No device found in debug port\n");
536 dbgp_ehci_status(
"wait for port done");
544 dbgp_printk(
"No device in debug port\n");
548 dbgp_ehci_status(
"debug ported enabled");
559 for (devnum = 0; devnum <= 127; devnum++) {
560 ret = dbgp_control_msg(devnum,
563 &dbgp_desc,
sizeof(dbgp_desc));
568 dbgp_printk(
"Could not find attached debug device\n");
572 dbgp_printk(
"Attached device is not a debug device\n");
575 dbgp_endpoint_out = dbgp_desc.bDebugOutEndpoint;
576 dbgp_endpoint_in = dbgp_desc.bDebugInEndpoint;
580 ret = dbgp_control_msg(devnum,
584 dbgp_printk(
"Could not move attached device to %d\n",
589 dbgp_printk(
"debug device renamed to 127\n");
597 dbgp_printk(
" Could not enable the debug device\n");
600 dbgp_printk(
"debug interface enabled\n");
605 dbgp_printk(
"dbgp_bulk_write failed: %d\n", ret);
608 dbgp_printk(
"small write done\n");
618 static int ehci_reset_port(
int port)
624 dbgp_ehci_status(
"reset port");
633 delay_time +=
delay) {
647 }
while ((portsc & PORT_RESET) && (--loop > 0));
651 if (!(portsc & PORT_CONNECT))
659 if (!(portsc & PORT_RESET) && (portsc &
PORT_PE))
664 static int ehci_wait_for_port(
int port)
669 for (reps = 0; reps < 300; reps++) {
675 ret = ehci_reset_port(port);
683 static void __init default_set_debug_port(
int port)
689 static void __init nvidia_set_debug_port(
int port)
694 dword &= ~(0x0f<<12);
695 dword |= ((port & 0x0f)<<12);
698 dbgp_printk(
"set debug port to %d\n", port);
701 static void __init detect_set_debug_port(
void)
708 if ((vendorid & 0xffff) == 0x10de) {
709 dbgp_printk(
"using nvidia set_debug_port\n");
710 set_debug_port = nvidia_set_debug_port;
717 #define EHCI_USBLEGSUP_BIOS (1 << 16)
718 #define EHCI_USBLEGCTLSTS 4
719 static void __init early_ehci_bios_handoff(
void)
722 int offset = (hcc_params >> 8) & 0xff;
731 dbgp_printk(
"dbgp: ehci BIOS state %08x\n", cap);
734 dbgp_printk(
"dbgp: BIOS handoff\n");
741 while ((cap & EHCI_USBLEGSUP_BIOS) && (msec > 0)) {
748 if (cap & EHCI_USBLEGSUP_BIOS) {
751 dbgp_printk(
"dbgp: BIOS handoff failed: %08x\n", cap);
761 static int __init ehci_setup(
void)
764 u32 debug_port, new_debug_port = 0, n_ports;
769 early_ehci_bios_handoff();
778 dbgp_phys_port = debug_port;
781 dbgp_printk(
"debug_port: %d\n", debug_port);
782 dbgp_printk(
"n_ports: %d\n", n_ports);
783 dbgp_ehci_status(
"");
785 for (i = 1; i <= n_ports; i++) {
787 dbgp_printk(
"portstatus%d: %08x\n", i, portsc);
790 if (port_map_tried && (new_debug_port != debug_port)) {
792 set_debug_port(new_debug_port);
801 if (dbgp_ehci_controller_reset() != 0)
804 dbgp_ehci_status(
"ehci skip - already configured");
807 ret = _dbgp_external_startup();
809 goto next_debug_port;
821 port_map_tried |= (1<<(debug_port - 1));
822 new_debug_port = ((debug_port-1+1)%n_ports) + 1;
823 if (port_map_tried != ((1<<n_ports) - 1)) {
824 set_debug_port(new_debug_port);
828 set_debug_port(new_debug_port);
852 dbgp_printk(
"dbgp_num: %d\n", dbgp_num);
854 cap = find_dbgp(dbgp_num, &bus, &slot, &func);
858 dbgp_printk(
"Found EHCI debug port on %02x:%02x.%1x\n", bus, slot,
862 bar = (debug_port >> 29) & 0x7;
863 bar = (bar * 4) + 0
xc;
864 offset = (debug_port >> 16) & 0xfff;
865 dbgp_printk(
"bar: %02x offset: %03x\n", bar, offset);
867 dbgp_printk(
"only debug ports on bar 1 handled.\n");
873 dbgp_printk(
"bar_val: %02x offset: %03x\n", bar_val, offset);
875 dbgp_printk(
"only simple 32bit mmio bars supported\n");
885 dbgp_printk(
"mmio for ehci enabled\n");
894 ehci_bar += bar_val & ~PAGE_MASK;
895 dbgp_printk(
"ehci_bar: %p\n", ehci_bar);
897 ehci_caps = ehci_bar;
899 ehci_debug = ehci_bar +
offset;
904 detect_set_debug_port();
908 dbgp_printk(
"ehci_setup failed\n");
913 dbgp_ehci_status(
"early_init_complete");
926 if (!ehci_debug || dbgp_not_safe)
937 _dbgp_external_startup();
945 for (chunk = 0; chunk < DBGP_MAX_PACKET && n > 0;
946 str++, chunk++, n--) {
947 if (!use_cr && *str ==
'\n') {
960 dbgp_endpoint_out, buf, chunk);
972 .write = early_dbgp_write,
977 #if IS_ENABLED(CONFIG_USB_EHCI_HCD)
978 int dbgp_reset_prep(
struct usb_hcd *hcd)
990 if ((early_dbgp_console.
index != -1 &&
1006 int dbgp_external_startup(
struct usb_hcd *hcd)
1016 static int kgdbdbgp_buf_sz;
1017 static int kgdbdbgp_buf_idx;
1020 static int kgdbdbgp_read_char(
void)
1024 if (kgdbdbgp_buf_idx < kgdbdbgp_buf_sz) {
1025 char ch = kgdbdbgp_buf[kgdbdbgp_buf_idx++];
1030 &kgdbdbgp_buf, DBGP_MAX_PACKET,
1034 kgdbdbgp_buf_sz =
ret;
1035 kgdbdbgp_buf_idx = 1;
1036 return kgdbdbgp_buf[0];
1039 static void kgdbdbgp_write_char(
u8 chr)
1041 early_dbgp_write(
NULL, &chr, 1);
1044 static struct kgdb_io kgdbdbgp_io_ops = {
1046 .read_char = kgdbdbgp_read_char,
1047 .write_char = kgdbdbgp_write_char,
1050 static int kgdbdbgp_wait_time;
1052 static int __init kgdbdbgp_parse_config(
char *str)
1066 kgdbdbgp_io_ops.is_console = early_dbgp_console.
index != -1;
1072 static int kgdbdbgp_reader_thread(
void *ptr)
1077 kgdbdbgp_loop_cnt = 1;
1078 ret = kgdbdbgp_read_char();
1081 if (ret == 0x3 || ret ==
'$') {
1093 static int __init kgdbdbgp_start_thread(
void)