GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / usb / early / ehci-dbgp.c
blob89bc2f35cb4912565ef71046ce9cfa5aee1fb5e2
1 /*
2 * Standalone EHCI usb debug driver
4 * Originally written by:
5 * Eric W. Biederman" <ebiederm@xmission.com> and
6 * Yinghai Lu <yhlu.kernel@gmail.com>
8 * Changes for early/late printk and HW errata:
9 * Jason Wessel <jason.wessel@windriver.com>
10 * Copyright (C) 2009 Wind River Systems, Inc.
14 #include <linux/console.h>
15 #include <linux/errno.h>
16 #include <linux/module.h>
17 #include <linux/pci_regs.h>
18 #include <linux/pci_ids.h>
19 #include <linux/usb/ch9.h>
20 #include <linux/usb/ehci_def.h>
21 #include <linux/delay.h>
22 #include <linux/serial_core.h>
23 #include <linux/kgdb.h>
24 #include <linux/kthread.h>
25 #include <asm/io.h>
26 #include <asm/pci-direct.h>
27 #include <asm/fixmap.h>
29 /* The code here is intended to talk directly to the EHCI debug port
30 * and does not require that you have any kind of USB host controller
31 * drivers or USB device drivers compiled into the kernel.
33 * If you make a change to anything in here, the following test cases
34 * need to pass where a USB debug device works in the following
35 * configurations.
37 * 1. boot args: earlyprintk=dbgp
38 * o kernel compiled with # CONFIG_USB_EHCI_HCD is not set
39 * o kernel compiled with CONFIG_USB_EHCI_HCD=y
40 * 2. boot args: earlyprintk=dbgp,keep
41 * o kernel compiled with # CONFIG_USB_EHCI_HCD is not set
42 * o kernel compiled with CONFIG_USB_EHCI_HCD=y
43 * 3. boot args: earlyprintk=dbgp console=ttyUSB0
44 * o kernel has CONFIG_USB_EHCI_HCD=y and
45 * CONFIG_USB_SERIAL_DEBUG=y
46 * 4. boot args: earlyprintk=vga,dbgp
47 * o kernel compiled with # CONFIG_USB_EHCI_HCD is not set
48 * o kernel compiled with CONFIG_USB_EHCI_HCD=y
50 * For the 4th configuration you can turn on or off the DBGP_DEBUG
51 * such that you can debug the dbgp device's driver code.
54 static int dbgp_phys_port = 1;
56 static struct ehci_caps __iomem *ehci_caps;
57 static struct ehci_regs __iomem *ehci_regs;
58 static struct ehci_dbg_port __iomem *ehci_debug;
59 static int dbgp_not_safe; /* Cannot use debug device during ehci reset */
60 static unsigned int dbgp_endpoint_out;
61 static unsigned int dbgp_endpoint_in;
63 struct ehci_dev {
64 u32 bus;
65 u32 slot;
66 u32 func;
69 static struct ehci_dev ehci_dev;
71 #define USB_DEBUG_DEVNUM 127
73 #ifdef DBGP_DEBUG
74 #define dbgp_printk printk
75 static void dbgp_ehci_status(char *str)
77 if (!ehci_debug)
78 return;
79 dbgp_printk("dbgp: %s\n", str);
80 dbgp_printk(" Debug control: %08x", readl(&ehci_debug->control));
81 dbgp_printk(" ehci cmd : %08x", readl(&ehci_regs->command));
82 dbgp_printk(" ehci conf flg: %08x\n",
83 readl(&ehci_regs->configured_flag));
84 dbgp_printk(" ehci status : %08x", readl(&ehci_regs->status));
85 dbgp_printk(" ehci portsc : %08x\n",
86 readl(&ehci_regs->port_status[dbgp_phys_port - 1]));
88 #else
89 static inline void dbgp_ehci_status(char *str) { }
90 static inline void dbgp_printk(const char *fmt, ...) { }
91 #endif
93 static inline u32 dbgp_len_update(u32 x, u32 len)
95 return (x & ~0x0f) | (len & 0x0f);
98 #ifdef CONFIG_KGDB
99 static struct kgdb_io kgdbdbgp_io_ops;
100 #define dbgp_kgdb_mode (dbg_io_ops == &kgdbdbgp_io_ops)
101 #else
102 #define dbgp_kgdb_mode (0)
103 #endif
106 * USB Packet IDs (PIDs)
109 /* token */
110 #define USB_PID_OUT 0xe1
111 #define USB_PID_IN 0x69
112 #define USB_PID_SOF 0xa5
113 #define USB_PID_SETUP 0x2d
114 /* handshake */
115 #define USB_PID_ACK 0xd2
116 #define USB_PID_NAK 0x5a
117 #define USB_PID_STALL 0x1e
118 #define USB_PID_NYET 0x96
119 /* data */
120 #define USB_PID_DATA0 0xc3
121 #define USB_PID_DATA1 0x4b
122 #define USB_PID_DATA2 0x87
123 #define USB_PID_MDATA 0x0f
124 /* Special */
125 #define USB_PID_PREAMBLE 0x3c
126 #define USB_PID_ERR 0x3c
127 #define USB_PID_SPLIT 0x78
128 #define USB_PID_PING 0xb4
129 #define USB_PID_UNDEF_0 0xf0
131 #define USB_PID_DATA_TOGGLE 0x88
132 #define DBGP_CLAIM (DBGP_OWNER | DBGP_ENABLED | DBGP_INUSE)
134 #define PCI_CAP_ID_EHCI_DEBUG 0xa
136 #define HUB_ROOT_RESET_TIME 50 /* times are in msec */
137 #define HUB_SHORT_RESET_TIME 10
138 #define HUB_LONG_RESET_TIME 200
139 #define HUB_RESET_TIMEOUT 500
141 #define DBGP_MAX_PACKET 8
142 #define DBGP_TIMEOUT (250 * 1000)
143 #define DBGP_LOOPS 1000
145 static inline u32 dbgp_pid_write_update(u32 x, u32 tok)
147 static int data0 = USB_PID_DATA1;
148 data0 ^= USB_PID_DATA_TOGGLE;
149 return (x & 0xffff0000) | (data0 << 8) | (tok & 0xff);
152 static inline u32 dbgp_pid_read_update(u32 x, u32 tok)
154 return (x & 0xffff0000) | (USB_PID_DATA0 << 8) | (tok & 0xff);
157 static int dbgp_wait_until_complete(void)
159 u32 ctrl;
160 int loop = DBGP_TIMEOUT;
162 do {
163 ctrl = readl(&ehci_debug->control);
164 /* Stop when the transaction is finished */
165 if (ctrl & DBGP_DONE)
166 break;
167 udelay(1);
168 } while (--loop > 0);
170 if (!loop)
171 return -DBGP_TIMEOUT;
174 * Now that we have observed the completed transaction,
175 * clear the done bit.
177 writel(ctrl | DBGP_DONE, &ehci_debug->control);
178 return (ctrl & DBGP_ERROR) ? -DBGP_ERRCODE(ctrl) : DBGP_LEN(ctrl);
181 static inline void dbgp_mdelay(int ms)
183 int i;
185 while (ms--) {
186 for (i = 0; i < 1000; i++)
187 outb(0x1, 0x80);
191 static void dbgp_breath(void)
193 /* Sleep to give the debug port a chance to breathe */
196 static int dbgp_wait_until_done(unsigned ctrl, int loop)
198 u32 pids, lpid;
199 int ret;
201 retry:
202 writel(ctrl | DBGP_GO, &ehci_debug->control);
203 ret = dbgp_wait_until_complete();
204 pids = readl(&ehci_debug->pids);
205 lpid = DBGP_PID_GET(pids);
207 if (ret < 0) {
208 /* A -DBGP_TIMEOUT failure here means the device has
209 * failed, perhaps because it was unplugged, in which
210 * case we do not want to hang the system so the dbgp
211 * will be marked as unsafe to use. EHCI reset is the
212 * only way to recover if you unplug the dbgp device.
214 if (ret == -DBGP_TIMEOUT && !dbgp_not_safe)
215 dbgp_not_safe = 1;
216 if (ret == -DBGP_ERR_BAD && --loop > 0)
217 goto retry;
218 return ret;
222 * If the port is getting full or it has dropped data
223 * start pacing ourselves, not necessary but it's friendly.
225 if ((lpid == USB_PID_NAK) || (lpid == USB_PID_NYET))
226 dbgp_breath();
228 /* If I get a NACK reissue the transmission */
229 if (lpid == USB_PID_NAK) {
230 if (--loop > 0)
231 goto retry;
234 return ret;
237 static inline void dbgp_set_data(const void *buf, int size)
239 const unsigned char *bytes = buf;
240 u32 lo, hi;
241 int i;
243 lo = hi = 0;
244 for (i = 0; i < 4 && i < size; i++)
245 lo |= bytes[i] << (8*i);
246 for (; i < 8 && i < size; i++)
247 hi |= bytes[i] << (8*(i - 4));
248 writel(lo, &ehci_debug->data03);
249 writel(hi, &ehci_debug->data47);
252 static inline void dbgp_get_data(void *buf, int size)
254 unsigned char *bytes = buf;
255 u32 lo, hi;
256 int i;
258 lo = readl(&ehci_debug->data03);
259 hi = readl(&ehci_debug->data47);
260 for (i = 0; i < 4 && i < size; i++)
261 bytes[i] = (lo >> (8*i)) & 0xff;
262 for (; i < 8 && i < size; i++)
263 bytes[i] = (hi >> (8*(i - 4))) & 0xff;
266 static int dbgp_bulk_write(unsigned devnum, unsigned endpoint,
267 const char *bytes, int size)
269 int ret;
270 u32 addr;
271 u32 pids, ctrl;
273 if (size > DBGP_MAX_PACKET)
274 return -1;
276 addr = DBGP_EPADDR(devnum, endpoint);
278 pids = readl(&ehci_debug->pids);
279 pids = dbgp_pid_write_update(pids, USB_PID_OUT);
281 ctrl = readl(&ehci_debug->control);
282 ctrl = dbgp_len_update(ctrl, size);
283 ctrl |= DBGP_OUT;
284 ctrl |= DBGP_GO;
286 dbgp_set_data(bytes, size);
287 writel(addr, &ehci_debug->address);
288 writel(pids, &ehci_debug->pids);
289 ret = dbgp_wait_until_done(ctrl, DBGP_LOOPS);
291 return ret;
294 static int dbgp_bulk_read(unsigned devnum, unsigned endpoint, void *data,
295 int size, int loops)
297 u32 pids, addr, ctrl;
298 int ret;
300 if (size > DBGP_MAX_PACKET)
301 return -1;
303 addr = DBGP_EPADDR(devnum, endpoint);
305 pids = readl(&ehci_debug->pids);
306 pids = dbgp_pid_read_update(pids, USB_PID_IN);
308 ctrl = readl(&ehci_debug->control);
309 ctrl = dbgp_len_update(ctrl, size);
310 ctrl &= ~DBGP_OUT;
311 ctrl |= DBGP_GO;
313 writel(addr, &ehci_debug->address);
314 writel(pids, &ehci_debug->pids);
315 ret = dbgp_wait_until_done(ctrl, loops);
316 if (ret < 0)
317 return ret;
319 if (size > ret)
320 size = ret;
321 dbgp_get_data(data, size);
322 return ret;
325 static int dbgp_control_msg(unsigned devnum, int requesttype,
326 int request, int value, int index, void *data, int size)
328 u32 pids, addr, ctrl;
329 struct usb_ctrlrequest req;
330 int read;
331 int ret;
333 read = (requesttype & USB_DIR_IN) != 0;
334 if (size > (read ? DBGP_MAX_PACKET:0))
335 return -1;
337 /* Compute the control message */
338 req.bRequestType = requesttype;
339 req.bRequest = request;
340 req.wValue = cpu_to_le16(value);
341 req.wIndex = cpu_to_le16(index);
342 req.wLength = cpu_to_le16(size);
344 pids = DBGP_PID_SET(USB_PID_DATA0, USB_PID_SETUP);
345 addr = DBGP_EPADDR(devnum, 0);
347 ctrl = readl(&ehci_debug->control);
348 ctrl = dbgp_len_update(ctrl, sizeof(req));
349 ctrl |= DBGP_OUT;
350 ctrl |= DBGP_GO;
352 /* Send the setup message */
353 dbgp_set_data(&req, sizeof(req));
354 writel(addr, &ehci_debug->address);
355 writel(pids, &ehci_debug->pids);
356 ret = dbgp_wait_until_done(ctrl, DBGP_LOOPS);
357 if (ret < 0)
358 return ret;
360 /* Read the result */
361 return dbgp_bulk_read(devnum, 0, data, size, DBGP_LOOPS);
364 /* Find a PCI capability */
365 static u32 __init find_cap(u32 num, u32 slot, u32 func, int cap)
367 u8 pos;
368 int bytes;
370 if (!(read_pci_config_16(num, slot, func, PCI_STATUS) &
371 PCI_STATUS_CAP_LIST))
372 return 0;
374 pos = read_pci_config_byte(num, slot, func, PCI_CAPABILITY_LIST);
375 for (bytes = 0; bytes < 48 && pos >= 0x40; bytes++) {
376 u8 id;
378 pos &= ~3;
379 id = read_pci_config_byte(num, slot, func, pos+PCI_CAP_LIST_ID);
380 if (id == 0xff)
381 break;
382 if (id == cap)
383 return pos;
385 pos = read_pci_config_byte(num, slot, func,
386 pos+PCI_CAP_LIST_NEXT);
388 return 0;
391 static u32 __init __find_dbgp(u32 bus, u32 slot, u32 func)
393 u32 class;
395 class = read_pci_config(bus, slot, func, PCI_CLASS_REVISION);
396 if ((class >> 8) != PCI_CLASS_SERIAL_USB_EHCI)
397 return 0;
399 return find_cap(bus, slot, func, PCI_CAP_ID_EHCI_DEBUG);
402 static u32 __init find_dbgp(int ehci_num, u32 *rbus, u32 *rslot, u32 *rfunc)
404 u32 bus, slot, func;
406 for (bus = 0; bus < 256; bus++) {
407 for (slot = 0; slot < 32; slot++) {
408 for (func = 0; func < 8; func++) {
409 unsigned cap;
411 cap = __find_dbgp(bus, slot, func);
413 if (!cap)
414 continue;
415 if (ehci_num-- != 0)
416 continue;
417 *rbus = bus;
418 *rslot = slot;
419 *rfunc = func;
420 return cap;
424 return 0;
427 static int dbgp_ehci_startup(void)
429 u32 ctrl, cmd, status;
430 int loop;
432 /* Claim ownership, but do not enable yet */
433 ctrl = readl(&ehci_debug->control);
434 ctrl |= DBGP_OWNER;
435 ctrl &= ~(DBGP_ENABLED | DBGP_INUSE);
436 writel(ctrl, &ehci_debug->control);
437 udelay(1);
439 dbgp_ehci_status("EHCI startup");
440 /* Start the ehci running */
441 cmd = readl(&ehci_regs->command);
442 cmd &= ~(CMD_LRESET | CMD_IAAD | CMD_PSE | CMD_ASE | CMD_RESET);
443 cmd |= CMD_RUN;
444 writel(cmd, &ehci_regs->command);
446 /* Ensure everything is routed to the EHCI */
447 writel(FLAG_CF, &ehci_regs->configured_flag);
449 /* Wait until the controller is no longer halted */
450 loop = 10;
451 do {
452 status = readl(&ehci_regs->status);
453 if (!(status & STS_HALT))
454 break;
455 udelay(1);
456 } while (--loop > 0);
458 if (!loop) {
459 dbgp_printk("ehci can not be started\n");
460 return -ENODEV;
462 dbgp_printk("ehci started\n");
463 return 0;
466 static int dbgp_ehci_controller_reset(void)
468 int loop = 250 * 1000;
469 u32 cmd;
471 /* Reset the EHCI controller */
472 cmd = readl(&ehci_regs->command);
473 cmd |= CMD_RESET;
474 writel(cmd, &ehci_regs->command);
475 do {
476 cmd = readl(&ehci_regs->command);
477 } while ((cmd & CMD_RESET) && (--loop > 0));
479 if (!loop) {
480 dbgp_printk("can not reset ehci\n");
481 return -1;
483 dbgp_ehci_status("ehci reset done");
484 return 0;
486 static int ehci_wait_for_port(int port);
487 /* Return 0 on success
488 * Return -ENODEV for any general failure
489 * Return -EIO if wait for port fails
491 int dbgp_external_startup(void)
493 int devnum;
494 struct usb_debug_descriptor dbgp_desc;
495 int ret;
496 u32 ctrl, portsc, cmd;
497 int dbg_port = dbgp_phys_port;
498 int tries = 3;
499 int reset_port_tries = 1;
500 int try_hard_once = 1;
502 try_port_reset_again:
503 ret = dbgp_ehci_startup();
504 if (ret)
505 return ret;
507 /* Wait for a device to show up in the debug port */
508 ret = ehci_wait_for_port(dbg_port);
509 if (ret < 0) {
510 portsc = readl(&ehci_regs->port_status[dbg_port - 1]);
511 if (!(portsc & PORT_CONNECT) && try_hard_once) {
512 /* Last ditch effort to try to force enable
513 * the debug device by using the packet test
514 * ehci command to try and wake it up. */
515 try_hard_once = 0;
516 cmd = readl(&ehci_regs->command);
517 cmd &= ~CMD_RUN;
518 writel(cmd, &ehci_regs->command);
519 portsc = readl(&ehci_regs->port_status[dbg_port - 1]);
520 portsc |= PORT_TEST_PKT;
521 writel(portsc, &ehci_regs->port_status[dbg_port - 1]);
522 dbgp_ehci_status("Trying to force debug port online");
523 mdelay(50);
524 dbgp_ehci_controller_reset();
525 goto try_port_reset_again;
526 } else if (reset_port_tries--) {
527 goto try_port_reset_again;
529 dbgp_printk("No device found in debug port\n");
530 return -EIO;
532 dbgp_ehci_status("wait for port done");
534 /* Enable the debug port */
535 ctrl = readl(&ehci_debug->control);
536 ctrl |= DBGP_CLAIM;
537 writel(ctrl, &ehci_debug->control);
538 ctrl = readl(&ehci_debug->control);
539 if ((ctrl & DBGP_CLAIM) != DBGP_CLAIM) {
540 dbgp_printk("No device in debug port\n");
541 writel(ctrl & ~DBGP_CLAIM, &ehci_debug->control);
542 return -ENODEV;
544 dbgp_ehci_status("debug ported enabled");
546 /* Completely transfer the debug device to the debug controller */
547 portsc = readl(&ehci_regs->port_status[dbg_port - 1]);
548 portsc &= ~PORT_PE;
549 writel(portsc, &ehci_regs->port_status[dbg_port - 1]);
551 dbgp_mdelay(100);
553 try_again:
554 /* Find the debug device and make it device number 127 */
555 for (devnum = 0; devnum <= 127; devnum++) {
556 ret = dbgp_control_msg(devnum,
557 USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
558 USB_REQ_GET_DESCRIPTOR, (USB_DT_DEBUG << 8), 0,
559 &dbgp_desc, sizeof(dbgp_desc));
560 if (ret > 0)
561 break;
563 if (devnum > 127) {
564 dbgp_printk("Could not find attached debug device\n");
565 goto err;
567 if (ret < 0) {
568 dbgp_printk("Attached device is not a debug device\n");
569 goto err;
571 dbgp_endpoint_out = dbgp_desc.bDebugOutEndpoint;
572 dbgp_endpoint_in = dbgp_desc.bDebugInEndpoint;
574 /* Move the device to 127 if it isn't already there */
575 if (devnum != USB_DEBUG_DEVNUM) {
576 ret = dbgp_control_msg(devnum,
577 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
578 USB_REQ_SET_ADDRESS, USB_DEBUG_DEVNUM, 0, NULL, 0);
579 if (ret < 0) {
580 dbgp_printk("Could not move attached device to %d\n",
581 USB_DEBUG_DEVNUM);
582 goto err;
584 devnum = USB_DEBUG_DEVNUM;
585 dbgp_printk("debug device renamed to 127\n");
588 /* Enable the debug interface */
589 ret = dbgp_control_msg(USB_DEBUG_DEVNUM,
590 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
591 USB_REQ_SET_FEATURE, USB_DEVICE_DEBUG_MODE, 0, NULL, 0);
592 if (ret < 0) {
593 dbgp_printk(" Could not enable the debug device\n");
594 goto err;
596 dbgp_printk("debug interface enabled\n");
597 /* Perform a small write to get the even/odd data state in sync
599 ret = dbgp_bulk_write(USB_DEBUG_DEVNUM, dbgp_endpoint_out, " ", 1);
600 if (ret < 0) {
601 dbgp_printk("dbgp_bulk_write failed: %d\n", ret);
602 goto err;
604 dbgp_printk("small write doned\n");
605 dbgp_not_safe = 0;
607 return 0;
608 err:
609 if (tries--)
610 goto try_again;
611 return -ENODEV;
613 EXPORT_SYMBOL_GPL(dbgp_external_startup);
615 static int ehci_reset_port(int port)
617 u32 portsc;
618 u32 delay_time, delay;
619 int loop;
621 dbgp_ehci_status("reset port");
622 /* Reset the usb debug port */
623 portsc = readl(&ehci_regs->port_status[port - 1]);
624 portsc &= ~PORT_PE;
625 portsc |= PORT_RESET;
626 writel(portsc, &ehci_regs->port_status[port - 1]);
628 delay = HUB_ROOT_RESET_TIME;
629 for (delay_time = 0; delay_time < HUB_RESET_TIMEOUT;
630 delay_time += delay) {
631 dbgp_mdelay(delay);
632 portsc = readl(&ehci_regs->port_status[port - 1]);
633 if (!(portsc & PORT_RESET))
634 break;
636 if (portsc & PORT_RESET) {
637 /* force reset to complete */
638 loop = 100 * 1000;
639 writel(portsc & ~(PORT_RWC_BITS | PORT_RESET),
640 &ehci_regs->port_status[port - 1]);
641 do {
642 udelay(1);
643 portsc = readl(&ehci_regs->port_status[port-1]);
644 } while ((portsc & PORT_RESET) && (--loop > 0));
647 /* Device went away? */
648 if (!(portsc & PORT_CONNECT))
649 return -ENOTCONN;
651 /* bomb out completely if something weird happend */
652 if ((portsc & PORT_CSC))
653 return -EINVAL;
655 /* If we've finished resetting, then break out of the loop */
656 if (!(portsc & PORT_RESET) && (portsc & PORT_PE))
657 return 0;
658 return -EBUSY;
661 static int ehci_wait_for_port(int port)
663 u32 status;
664 int ret, reps;
666 for (reps = 0; reps < 300; reps++) {
667 status = readl(&ehci_regs->status);
668 if (status & STS_PCD)
669 break;
670 dbgp_mdelay(1);
672 ret = ehci_reset_port(port);
673 if (ret == 0)
674 return 0;
675 return -ENOTCONN;
678 typedef void (*set_debug_port_t)(int port);
680 static void __init default_set_debug_port(int port)
684 static set_debug_port_t __initdata set_debug_port = default_set_debug_port;
686 static void __init nvidia_set_debug_port(int port)
688 u32 dword;
689 dword = read_pci_config(ehci_dev.bus, ehci_dev.slot, ehci_dev.func,
690 0x74);
691 dword &= ~(0x0f<<12);
692 dword |= ((port & 0x0f)<<12);
693 write_pci_config(ehci_dev.bus, ehci_dev.slot, ehci_dev.func, 0x74,
694 dword);
695 dbgp_printk("set debug port to %d\n", port);
698 static void __init detect_set_debug_port(void)
700 u32 vendorid;
702 vendorid = read_pci_config(ehci_dev.bus, ehci_dev.slot, ehci_dev.func,
703 0x00);
705 if ((vendorid & 0xffff) == 0x10de) {
706 dbgp_printk("using nvidia set_debug_port\n");
707 set_debug_port = nvidia_set_debug_port;
711 /* The code in early_ehci_bios_handoff() is derived from the usb pci
712 * quirk initialization, but altered so as to use the early PCI
713 * routines. */
714 #define EHCI_USBLEGSUP_BIOS (1 << 16) /* BIOS semaphore */
715 #define EHCI_USBLEGCTLSTS 4 /* legacy control/status */
716 static void __init early_ehci_bios_handoff(void)
718 u32 hcc_params = readl(&ehci_caps->hcc_params);
719 int offset = (hcc_params >> 8) & 0xff;
720 u32 cap;
721 int msec;
723 if (!offset)
724 return;
726 cap = read_pci_config(ehci_dev.bus, ehci_dev.slot,
727 ehci_dev.func, offset);
728 dbgp_printk("dbgp: ehci BIOS state %08x\n", cap);
730 if ((cap & 0xff) == 1 && (cap & EHCI_USBLEGSUP_BIOS)) {
731 dbgp_printk("dbgp: BIOS handoff\n");
732 write_pci_config_byte(ehci_dev.bus, ehci_dev.slot,
733 ehci_dev.func, offset + 3, 1);
736 /* if boot firmware now owns EHCI, spin till it hands it over. */
737 msec = 1000;
738 while ((cap & EHCI_USBLEGSUP_BIOS) && (msec > 0)) {
739 mdelay(10);
740 msec -= 10;
741 cap = read_pci_config(ehci_dev.bus, ehci_dev.slot,
742 ehci_dev.func, offset);
745 if (cap & EHCI_USBLEGSUP_BIOS) {
746 /* well, possibly buggy BIOS... try to shut it down,
747 * and hope nothing goes too wrong */
748 dbgp_printk("dbgp: BIOS handoff failed: %08x\n", cap);
749 write_pci_config_byte(ehci_dev.bus, ehci_dev.slot,
750 ehci_dev.func, offset + 2, 0);
753 /* just in case, always disable EHCI SMIs */
754 write_pci_config_byte(ehci_dev.bus, ehci_dev.slot, ehci_dev.func,
755 offset + EHCI_USBLEGCTLSTS, 0);
758 static int __init ehci_setup(void)
760 u32 ctrl, portsc, hcs_params;
761 u32 debug_port, new_debug_port = 0, n_ports;
762 int ret, i;
763 int port_map_tried;
764 int playtimes = 3;
766 early_ehci_bios_handoff();
768 try_next_time:
769 port_map_tried = 0;
771 try_next_port:
773 hcs_params = readl(&ehci_caps->hcs_params);
774 debug_port = HCS_DEBUG_PORT(hcs_params);
775 dbgp_phys_port = debug_port;
776 n_ports = HCS_N_PORTS(hcs_params);
778 dbgp_printk("debug_port: %d\n", debug_port);
779 dbgp_printk("n_ports: %d\n", n_ports);
780 dbgp_ehci_status("");
782 for (i = 1; i <= n_ports; i++) {
783 portsc = readl(&ehci_regs->port_status[i-1]);
784 dbgp_printk("portstatus%d: %08x\n", i, portsc);
787 if (port_map_tried && (new_debug_port != debug_port)) {
788 if (--playtimes) {
789 set_debug_port(new_debug_port);
790 goto try_next_time;
792 return -1;
795 /* Only reset the controller if it is not already in the
796 * configured state */
797 if (!(readl(&ehci_regs->configured_flag) & FLAG_CF)) {
798 if (dbgp_ehci_controller_reset() != 0)
799 return -1;
800 } else {
801 dbgp_ehci_status("ehci skip - already configured");
804 ret = dbgp_external_startup();
805 if (ret == -EIO)
806 goto next_debug_port;
808 if (ret < 0) {
809 /* Things didn't work so remove my claim */
810 ctrl = readl(&ehci_debug->control);
811 ctrl &= ~(DBGP_CLAIM | DBGP_OUT);
812 writel(ctrl, &ehci_debug->control);
813 return -1;
815 return 0;
817 next_debug_port:
818 port_map_tried |= (1<<(debug_port - 1));
819 new_debug_port = ((debug_port-1+1)%n_ports) + 1;
820 if (port_map_tried != ((1<<n_ports) - 1)) {
821 set_debug_port(new_debug_port);
822 goto try_next_port;
824 if (--playtimes) {
825 set_debug_port(new_debug_port);
826 goto try_next_time;
829 return -1;
832 int __init early_dbgp_init(char *s)
834 u32 debug_port, bar, offset;
835 u32 bus, slot, func, cap;
836 void __iomem *ehci_bar;
837 u32 dbgp_num;
838 u32 bar_val;
839 char *e;
840 int ret;
841 u8 byte;
843 if (!early_pci_allowed())
844 return -1;
846 dbgp_num = 0;
847 if (*s)
848 dbgp_num = simple_strtoul(s, &e, 10);
849 dbgp_printk("dbgp_num: %d\n", dbgp_num);
851 cap = find_dbgp(dbgp_num, &bus, &slot, &func);
852 if (!cap)
853 return -1;
855 dbgp_printk("Found EHCI debug port on %02x:%02x.%1x\n", bus, slot,
856 func);
858 debug_port = read_pci_config(bus, slot, func, cap);
859 bar = (debug_port >> 29) & 0x7;
860 bar = (bar * 4) + 0xc;
861 offset = (debug_port >> 16) & 0xfff;
862 dbgp_printk("bar: %02x offset: %03x\n", bar, offset);
863 if (bar != PCI_BASE_ADDRESS_0) {
864 dbgp_printk("only debug ports on bar 1 handled.\n");
866 return -1;
869 bar_val = read_pci_config(bus, slot, func, PCI_BASE_ADDRESS_0);
870 dbgp_printk("bar_val: %02x offset: %03x\n", bar_val, offset);
871 if (bar_val & ~PCI_BASE_ADDRESS_MEM_MASK) {
872 dbgp_printk("only simple 32bit mmio bars supported\n");
874 return -1;
877 /* double check if the mem space is enabled */
878 byte = read_pci_config_byte(bus, slot, func, 0x04);
879 if (!(byte & 0x2)) {
880 byte |= 0x02;
881 write_pci_config_byte(bus, slot, func, 0x04, byte);
882 dbgp_printk("mmio for ehci enabled\n");
885 set_fixmap_nocache(FIX_DBGP_BASE, bar_val & PAGE_MASK);
886 ehci_bar = (void __iomem *)__fix_to_virt(FIX_DBGP_BASE);
887 ehci_bar += bar_val & ~PAGE_MASK;
888 dbgp_printk("ehci_bar: %p\n", ehci_bar);
890 ehci_caps = ehci_bar;
891 ehci_regs = ehci_bar + HC_LENGTH(readl(&ehci_caps->hc_capbase));
892 ehci_debug = ehci_bar + offset;
893 ehci_dev.bus = bus;
894 ehci_dev.slot = slot;
895 ehci_dev.func = func;
897 detect_set_debug_port();
899 ret = ehci_setup();
900 if (ret < 0) {
901 dbgp_printk("ehci_setup failed\n");
902 ehci_debug = NULL;
904 return -1;
906 dbgp_ehci_status("early_init_complete");
908 return 0;
911 static void early_dbgp_write(struct console *con, const char *str, u32 n)
913 int chunk, ret;
914 char buf[DBGP_MAX_PACKET];
915 int use_cr = 0;
916 u32 cmd, ctrl;
917 int reset_run = 0;
919 if (!ehci_debug || dbgp_not_safe)
920 return;
922 cmd = readl(&ehci_regs->command);
923 if (unlikely(!(cmd & CMD_RUN))) {
924 /* If the ehci controller is not in the run state do extended
925 * checks to see if the acpi or some other initialization also
926 * reset the ehci debug port */
927 ctrl = readl(&ehci_debug->control);
928 if (!(ctrl & DBGP_ENABLED)) {
929 dbgp_not_safe = 1;
930 dbgp_external_startup();
931 } else {
932 cmd |= CMD_RUN;
933 writel(cmd, &ehci_regs->command);
934 reset_run = 1;
937 while (n > 0) {
938 for (chunk = 0; chunk < DBGP_MAX_PACKET && n > 0;
939 str++, chunk++, n--) {
940 if (!use_cr && *str == '\n') {
941 use_cr = 1;
942 buf[chunk] = '\r';
943 str--;
944 n++;
945 continue;
947 if (use_cr)
948 use_cr = 0;
949 buf[chunk] = *str;
951 if (chunk > 0) {
952 ret = dbgp_bulk_write(USB_DEBUG_DEVNUM,
953 dbgp_endpoint_out, buf, chunk);
956 if (unlikely(reset_run)) {
957 cmd = readl(&ehci_regs->command);
958 cmd &= ~CMD_RUN;
959 writel(cmd, &ehci_regs->command);
963 struct console early_dbgp_console = {
964 .name = "earlydbg",
965 .write = early_dbgp_write,
966 .flags = CON_PRINTBUFFER,
967 .index = -1,
970 int dbgp_reset_prep(void)
972 u32 ctrl;
974 dbgp_not_safe = 1;
975 if (!ehci_debug)
976 return 0;
978 if ((early_dbgp_console.index != -1 &&
979 !(early_dbgp_console.flags & CON_BOOT)) ||
980 dbgp_kgdb_mode)
981 return 1;
982 /* This means the console is not initialized, or should get
983 * shutdown so as to allow for reuse of the usb device, which
984 * means it is time to shutdown the usb debug port. */
985 ctrl = readl(&ehci_debug->control);
986 if (ctrl & DBGP_ENABLED) {
987 ctrl &= ~(DBGP_CLAIM);
988 writel(ctrl, &ehci_debug->control);
990 return 0;
992 EXPORT_SYMBOL_GPL(dbgp_reset_prep);
994 #ifdef CONFIG_KGDB
996 static char kgdbdbgp_buf[DBGP_MAX_PACKET];
997 static int kgdbdbgp_buf_sz;
998 static int kgdbdbgp_buf_idx;
999 static int kgdbdbgp_loop_cnt = DBGP_LOOPS;
1001 static int kgdbdbgp_read_char(void)
1003 int ret;
1005 if (kgdbdbgp_buf_idx < kgdbdbgp_buf_sz) {
1006 char ch = kgdbdbgp_buf[kgdbdbgp_buf_idx++];
1007 return ch;
1010 ret = dbgp_bulk_read(USB_DEBUG_DEVNUM, dbgp_endpoint_in,
1011 &kgdbdbgp_buf, DBGP_MAX_PACKET,
1012 kgdbdbgp_loop_cnt);
1013 if (ret <= 0)
1014 return NO_POLL_CHAR;
1015 kgdbdbgp_buf_sz = ret;
1016 kgdbdbgp_buf_idx = 1;
1017 return kgdbdbgp_buf[0];
1020 static void kgdbdbgp_write_char(u8 chr)
1022 early_dbgp_write(NULL, &chr, 1);
1025 static struct kgdb_io kgdbdbgp_io_ops = {
1026 .name = "kgdbdbgp",
1027 .read_char = kgdbdbgp_read_char,
1028 .write_char = kgdbdbgp_write_char,
1031 static int kgdbdbgp_wait_time;
1033 static int __init kgdbdbgp_parse_config(char *str)
1035 char *ptr;
1037 if (!ehci_debug) {
1038 if (early_dbgp_init(str))
1039 return -1;
1041 ptr = strchr(str, ',');
1042 if (ptr) {
1043 ptr++;
1044 kgdbdbgp_wait_time = simple_strtoul(ptr, &ptr, 10);
1046 kgdb_register_io_module(&kgdbdbgp_io_ops);
1047 kgdbdbgp_io_ops.is_console = early_dbgp_console.index != -1;
1049 return 0;
1051 early_param("kgdbdbgp", kgdbdbgp_parse_config);
1053 static int kgdbdbgp_reader_thread(void *ptr)
1055 int ret;
1057 while (readl(&ehci_debug->control) & DBGP_ENABLED) {
1058 kgdbdbgp_loop_cnt = 1;
1059 ret = kgdbdbgp_read_char();
1060 kgdbdbgp_loop_cnt = DBGP_LOOPS;
1061 if (ret != NO_POLL_CHAR) {
1062 if (ret == 0x3 || ret == '$') {
1063 if (ret == '$')
1064 kgdbdbgp_buf_idx--;
1065 kgdb_breakpoint();
1067 continue;
1069 schedule_timeout_interruptible(kgdbdbgp_wait_time * HZ);
1071 return 0;
1074 static int __init kgdbdbgp_start_thread(void)
1076 if (dbgp_kgdb_mode && kgdbdbgp_wait_time)
1077 kthread_run(kgdbdbgp_reader_thread, NULL, "%s", "dbgp");
1079 return 0;
1081 module_init(kgdbdbgp_start_thread);
1082 #endif /* CONFIG_KGDB */