1 #include <linux/console.h>
2 #include <linux/kernel.h>
3 #include <linux/init.h>
4 #include <linux/string.h>
5 #include <linux/screen_info.h>
6 #include <linux/usb/ch9.h>
7 #include <linux/pci_regs.h>
8 #include <linux/pci_ids.h>
9 #include <linux/errno.h>
11 #include <asm/processor.h>
12 #include <asm/fcntl.h>
13 #include <asm/setup.h>
14 #include <xen/hvc-console.h>
15 #include <asm/pci-direct.h>
16 #include <asm/fixmap.h>
17 #include <asm/pgtable.h>
18 #include <linux/usb/ehci_def.h>
20 /* Simple VGA output */
21 #define VGABASE (__ISA_IO_base + 0xb8000)
23 static int max_ypos = 25, max_xpos = 80;
24 static int current_ypos = 25, current_xpos;
26 static void early_vga_write(struct console *con, const char *str, unsigned n)
31 while ((c = *str++) != '\0' && n-- > 0) {
32 if (current_ypos >= max_ypos) {
33 /* scroll 1 line up */
34 for (k = 1, j = 0; k < max_ypos; k++, j++) {
35 for (i = 0; i < max_xpos; i++) {
36 writew(readw(VGABASE+2*(max_xpos*k+i)),
37 VGABASE + 2*(max_xpos*j + i));
40 for (i = 0; i < max_xpos; i++)
41 writew(0x720, VGABASE + 2*(max_xpos*j + i));
42 current_ypos = max_ypos-1;
47 } else if (c != '\r') {
48 writew(((0x7 << 8) | (unsigned short) c),
49 VGABASE + 2*(max_xpos*current_ypos +
51 if (current_xpos >= max_xpos) {
59 static struct console early_vga_console = {
61 .write = early_vga_write,
62 .flags = CON_PRINTBUFFER,
66 /* Serial functions loosely based on a similar package from Klaus P. Gerlicher */
68 static int early_serial_base = 0x3f8; /* ttyS0 */
74 #define TXR 0 /* Transmit register (WRITE) */
75 #define RXR 0 /* Receive register (READ) */
76 #define IER 1 /* Interrupt Enable */
77 #define IIR 2 /* Interrupt ID */
78 #define FCR 2 /* FIFO control */
79 #define LCR 3 /* Line control */
80 #define MCR 4 /* Modem control */
81 #define LSR 5 /* Line Status */
82 #define MSR 6 /* Modem Status */
83 #define DLL 0 /* Divisor Latch Low */
84 #define DLH 1 /* Divisor latch High */
86 static int early_serial_putc(unsigned char ch)
88 unsigned timeout = 0xffff;
90 while ((inb(early_serial_base + LSR) & XMTRDY) == 0 && --timeout)
92 outb(ch, early_serial_base + TXR);
93 return timeout ? 0 : -1;
96 static void early_serial_write(struct console *con, const char *s, unsigned n)
98 while (*s && n-- > 0) {
100 early_serial_putc('\r');
101 early_serial_putc(*s);
106 #define DEFAULT_BAUD 9600
108 static __init void early_serial_init(char *s)
112 unsigned baud = DEFAULT_BAUD;
120 if (!strncmp(s, "0x", 2)) {
121 early_serial_base = simple_strtoul(s, &e, 16);
123 static const int __initconst bases[] = { 0x3f8, 0x2f8 };
125 if (!strncmp(s, "ttyS", 4))
127 port = simple_strtoul(s, &e, 10);
128 if (port > 1 || s == e)
130 early_serial_base = bases[port];
132 s += strcspn(s, ",");
137 outb(0x3, early_serial_base + LCR); /* 8n1 */
138 outb(0, early_serial_base + IER); /* no interrupt */
139 outb(0, early_serial_base + FCR); /* no fifo */
140 outb(0x3, early_serial_base + MCR); /* DTR + RTS */
143 baud = simple_strtoul(s, &e, 0);
144 if (baud == 0 || s == e)
148 divisor = 115200 / baud;
149 c = inb(early_serial_base + LCR);
150 outb(c | DLAB, early_serial_base + LCR);
151 outb(divisor & 0xff, early_serial_base + DLL);
152 outb((divisor >> 8) & 0xff, early_serial_base + DLH);
153 outb(c & ~DLAB, early_serial_base + LCR);
156 static struct console early_serial_console = {
158 .write = early_serial_write,
159 .flags = CON_PRINTBUFFER,
163 #ifdef CONFIG_EARLY_PRINTK_DBGP
165 static struct ehci_caps __iomem *ehci_caps;
166 static struct ehci_regs __iomem *ehci_regs;
167 static struct ehci_dbg_port __iomem *ehci_debug;
168 static unsigned int dbgp_endpoint_out;
176 static struct ehci_dev ehci_dev;
178 #define USB_DEBUG_DEVNUM 127
180 #define DBGP_DATA_TOGGLE 0x8800
182 static inline u32 dbgp_pid_update(u32 x, u32 tok)
184 return ((x ^ DBGP_DATA_TOGGLE) & 0xffff00) | (tok & 0xff);
187 static inline u32 dbgp_len_update(u32 x, u32 len)
189 return (x & ~0x0f) | (len & 0x0f);
193 * USB Packet IDs (PIDs)
197 #define USB_PID_OUT 0xe1
198 #define USB_PID_IN 0x69
199 #define USB_PID_SOF 0xa5
200 #define USB_PID_SETUP 0x2d
202 #define USB_PID_ACK 0xd2
203 #define USB_PID_NAK 0x5a
204 #define USB_PID_STALL 0x1e
205 #define USB_PID_NYET 0x96
207 #define USB_PID_DATA0 0xc3
208 #define USB_PID_DATA1 0x4b
209 #define USB_PID_DATA2 0x87
210 #define USB_PID_MDATA 0x0f
212 #define USB_PID_PREAMBLE 0x3c
213 #define USB_PID_ERR 0x3c
214 #define USB_PID_SPLIT 0x78
215 #define USB_PID_PING 0xb4
216 #define USB_PID_UNDEF_0 0xf0
218 #define USB_PID_DATA_TOGGLE 0x88
219 #define DBGP_CLAIM (DBGP_OWNER | DBGP_ENABLED | DBGP_INUSE)
221 #define PCI_CAP_ID_EHCI_DEBUG 0xa
223 #define HUB_ROOT_RESET_TIME 50 /* times are in msec */
224 #define HUB_SHORT_RESET_TIME 10
225 #define HUB_LONG_RESET_TIME 200
226 #define HUB_RESET_TIMEOUT 500
228 #define DBGP_MAX_PACKET 8
230 static int dbgp_wait_until_complete(void)
236 ctrl = readl(&ehci_debug->control);
237 /* Stop when the transaction is finished */
238 if (ctrl & DBGP_DONE)
240 } while (--loop > 0);
246 * Now that we have observed the completed transaction,
247 * clear the done bit.
249 writel(ctrl | DBGP_DONE, &ehci_debug->control);
250 return (ctrl & DBGP_ERROR) ? -DBGP_ERRCODE(ctrl) : DBGP_LEN(ctrl);
253 static void dbgp_mdelay(int ms)
258 for (i = 0; i < 1000; i++)
263 static void dbgp_breath(void)
265 /* Sleep to give the debug port a chance to breathe */
268 static int dbgp_wait_until_done(unsigned ctrl)
275 writel(ctrl | DBGP_GO, &ehci_debug->control);
276 ret = dbgp_wait_until_complete();
277 pids = readl(&ehci_debug->pids);
278 lpid = DBGP_PID_GET(pids);
284 * If the port is getting full or it has dropped data
285 * start pacing ourselves, not necessary but it's friendly.
287 if ((lpid == USB_PID_NAK) || (lpid == USB_PID_NYET))
290 /* If I get a NACK reissue the transmission */
291 if (lpid == USB_PID_NAK) {
299 static void dbgp_set_data(const void *buf, int size)
301 const unsigned char *bytes = buf;
306 for (i = 0; i < 4 && i < size; i++)
307 lo |= bytes[i] << (8*i);
308 for (; i < 8 && i < size; i++)
309 hi |= bytes[i] << (8*(i - 4));
310 writel(lo, &ehci_debug->data03);
311 writel(hi, &ehci_debug->data47);
314 static void dbgp_get_data(void *buf, int size)
316 unsigned char *bytes = buf;
320 lo = readl(&ehci_debug->data03);
321 hi = readl(&ehci_debug->data47);
322 for (i = 0; i < 4 && i < size; i++)
323 bytes[i] = (lo >> (8*i)) & 0xff;
324 for (; i < 8 && i < size; i++)
325 bytes[i] = (hi >> (8*(i - 4))) & 0xff;
328 static int dbgp_bulk_write(unsigned devnum, unsigned endpoint,
329 const char *bytes, int size)
331 u32 pids, addr, ctrl;
334 if (size > DBGP_MAX_PACKET)
337 addr = DBGP_EPADDR(devnum, endpoint);
339 pids = readl(&ehci_debug->pids);
340 pids = dbgp_pid_update(pids, USB_PID_OUT);
342 ctrl = readl(&ehci_debug->control);
343 ctrl = dbgp_len_update(ctrl, size);
347 dbgp_set_data(bytes, size);
348 writel(addr, &ehci_debug->address);
349 writel(pids, &ehci_debug->pids);
351 ret = dbgp_wait_until_done(ctrl);
358 static int dbgp_bulk_read(unsigned devnum, unsigned endpoint, void *data,
361 u32 pids, addr, ctrl;
364 if (size > DBGP_MAX_PACKET)
367 addr = DBGP_EPADDR(devnum, endpoint);
369 pids = readl(&ehci_debug->pids);
370 pids = dbgp_pid_update(pids, USB_PID_IN);
372 ctrl = readl(&ehci_debug->control);
373 ctrl = dbgp_len_update(ctrl, size);
377 writel(addr, &ehci_debug->address);
378 writel(pids, &ehci_debug->pids);
379 ret = dbgp_wait_until_done(ctrl);
385 dbgp_get_data(data, size);
389 static int dbgp_control_msg(unsigned devnum, int requesttype, int request,
390 int value, int index, void *data, int size)
392 u32 pids, addr, ctrl;
393 struct usb_ctrlrequest req;
397 read = (requesttype & USB_DIR_IN) != 0;
398 if (size > (read ? DBGP_MAX_PACKET:0))
401 /* Compute the control message */
402 req.bRequestType = requesttype;
403 req.bRequest = request;
404 req.wValue = cpu_to_le16(value);
405 req.wIndex = cpu_to_le16(index);
406 req.wLength = cpu_to_le16(size);
408 pids = DBGP_PID_SET(USB_PID_DATA0, USB_PID_SETUP);
409 addr = DBGP_EPADDR(devnum, 0);
411 ctrl = readl(&ehci_debug->control);
412 ctrl = dbgp_len_update(ctrl, sizeof(req));
416 /* Send the setup message */
417 dbgp_set_data(&req, sizeof(req));
418 writel(addr, &ehci_debug->address);
419 writel(pids, &ehci_debug->pids);
420 ret = dbgp_wait_until_done(ctrl);
424 /* Read the result */
425 return dbgp_bulk_read(devnum, 0, data, size);
429 /* Find a PCI capability */
430 static u32 __init find_cap(u32 num, u32 slot, u32 func, int cap)
435 if (!(read_pci_config_16(num, slot, func, PCI_STATUS) &
436 PCI_STATUS_CAP_LIST))
439 pos = read_pci_config_byte(num, slot, func, PCI_CAPABILITY_LIST);
440 for (bytes = 0; bytes < 48 && pos >= 0x40; bytes++) {
444 id = read_pci_config_byte(num, slot, func, pos+PCI_CAP_LIST_ID);
450 pos = read_pci_config_byte(num, slot, func,
451 pos+PCI_CAP_LIST_NEXT);
456 static u32 __init __find_dbgp(u32 bus, u32 slot, u32 func)
460 class = read_pci_config(bus, slot, func, PCI_CLASS_REVISION);
461 if ((class >> 8) != PCI_CLASS_SERIAL_USB_EHCI)
464 return find_cap(bus, slot, func, PCI_CAP_ID_EHCI_DEBUG);
467 static u32 __init find_dbgp(int ehci_num, u32 *rbus, u32 *rslot, u32 *rfunc)
471 for (bus = 0; bus < 256; bus++) {
472 for (slot = 0; slot < 32; slot++) {
473 for (func = 0; func < 8; func++) {
476 cap = __find_dbgp(bus, slot, func);
492 static int ehci_reset_port(int port)
495 u32 delay_time, delay;
498 /* Reset the usb debug port */
499 portsc = readl(&ehci_regs->port_status[port - 1]);
501 portsc |= PORT_RESET;
502 writel(portsc, &ehci_regs->port_status[port - 1]);
504 delay = HUB_ROOT_RESET_TIME;
505 for (delay_time = 0; delay_time < HUB_RESET_TIMEOUT;
506 delay_time += delay) {
509 portsc = readl(&ehci_regs->port_status[port - 1]);
510 if (portsc & PORT_RESET) {
511 /* force reset to complete */
513 writel(portsc & ~(PORT_RWC_BITS | PORT_RESET),
514 &ehci_regs->port_status[port - 1]);
516 portsc = readl(&ehci_regs->port_status[port-1]);
517 } while ((portsc & PORT_RESET) && (--loop > 0));
520 /* Device went away? */
521 if (!(portsc & PORT_CONNECT))
524 /* bomb out completely if something weird happend */
525 if ((portsc & PORT_CSC))
528 /* If we've finished resetting, then break out of the loop */
529 if (!(portsc & PORT_RESET) && (portsc & PORT_PE))
535 static int ehci_wait_for_port(int port)
540 for (reps = 0; reps < 3; reps++) {
542 status = readl(&ehci_regs->status);
543 if (status & STS_PCD) {
544 ret = ehci_reset_port(port);
553 # define dbgp_printk early_printk
555 static inline void dbgp_printk(const char *fmt, ...) { }
558 typedef void (*set_debug_port_t)(int port);
560 static void default_set_debug_port(int port)
564 static set_debug_port_t set_debug_port = default_set_debug_port;
566 static void nvidia_set_debug_port(int port)
569 dword = read_pci_config(ehci_dev.bus, ehci_dev.slot, ehci_dev.func,
571 dword &= ~(0x0f<<12);
572 dword |= ((port & 0x0f)<<12);
573 write_pci_config(ehci_dev.bus, ehci_dev.slot, ehci_dev.func, 0x74,
575 dbgp_printk("set debug port to %d\n", port);
578 static void __init detect_set_debug_port(void)
582 vendorid = read_pci_config(ehci_dev.bus, ehci_dev.slot, ehci_dev.func,
585 if ((vendorid & 0xffff) == 0x10de) {
586 dbgp_printk("using nvidia set_debug_port\n");
587 set_debug_port = nvidia_set_debug_port;
591 static int __init ehci_setup(void)
593 struct usb_debug_descriptor dbgp_desc;
594 u32 cmd, ctrl, status, portsc, hcs_params;
595 u32 debug_port, new_debug_port = 0, n_ports;
607 hcs_params = readl(&ehci_caps->hcs_params);
608 debug_port = HCS_DEBUG_PORT(hcs_params);
609 n_ports = HCS_N_PORTS(hcs_params);
611 dbgp_printk("debug_port: %d\n", debug_port);
612 dbgp_printk("n_ports: %d\n", n_ports);
614 for (i = 1; i <= n_ports; i++) {
615 portsc = readl(&ehci_regs->port_status[i-1]);
616 dbgp_printk("portstatus%d: %08x\n", i, portsc);
619 if (port_map_tried && (new_debug_port != debug_port)) {
621 set_debug_port(new_debug_port);
628 /* Reset the EHCI controller */
629 cmd = readl(&ehci_regs->command);
631 writel(cmd, &ehci_regs->command);
633 cmd = readl(&ehci_regs->command);
634 } while ((cmd & CMD_RESET) && (--loop > 0));
637 dbgp_printk("can not reset ehci\n");
640 dbgp_printk("ehci reset done\n");
642 /* Claim ownership, but do not enable yet */
643 ctrl = readl(&ehci_debug->control);
645 ctrl &= ~(DBGP_ENABLED | DBGP_INUSE);
646 writel(ctrl, &ehci_debug->control);
648 /* Start the ehci running */
649 cmd = readl(&ehci_regs->command);
650 cmd &= ~(CMD_LRESET | CMD_IAAD | CMD_PSE | CMD_ASE | CMD_RESET);
652 writel(cmd, &ehci_regs->command);
654 /* Ensure everything is routed to the EHCI */
655 writel(FLAG_CF, &ehci_regs->configured_flag);
657 /* Wait until the controller is no longer halted */
660 status = readl(&ehci_regs->status);
661 } while ((status & STS_HALT) && (--loop > 0));
664 dbgp_printk("ehci can be started\n");
667 dbgp_printk("ehci started\n");
669 /* Wait for a device to show up in the debug port */
670 ret = ehci_wait_for_port(debug_port);
672 dbgp_printk("No device found in debug port\n");
673 goto next_debug_port;
675 dbgp_printk("ehci wait for port done\n");
677 /* Enable the debug port */
678 ctrl = readl(&ehci_debug->control);
680 writel(ctrl, &ehci_debug->control);
681 ctrl = readl(&ehci_debug->control);
682 if ((ctrl & DBGP_CLAIM) != DBGP_CLAIM) {
683 dbgp_printk("No device in debug port\n");
684 writel(ctrl & ~DBGP_CLAIM, &ehci_debug->control);
687 dbgp_printk("debug ported enabled\n");
689 /* Completely transfer the debug device to the debug controller */
690 portsc = readl(&ehci_regs->port_status[debug_port - 1]);
692 writel(portsc, &ehci_regs->port_status[debug_port - 1]);
696 /* Find the debug device and make it device number 127 */
697 for (devnum = 0; devnum <= 127; devnum++) {
698 ret = dbgp_control_msg(devnum,
699 USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
700 USB_REQ_GET_DESCRIPTOR, (USB_DT_DEBUG << 8), 0,
701 &dbgp_desc, sizeof(dbgp_desc));
706 dbgp_printk("Could not find attached debug device\n");
710 dbgp_printk("Attached device is not a debug device\n");
713 dbgp_endpoint_out = dbgp_desc.bDebugOutEndpoint;
715 /* Move the device to 127 if it isn't already there */
716 if (devnum != USB_DEBUG_DEVNUM) {
717 ret = dbgp_control_msg(devnum,
718 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
719 USB_REQ_SET_ADDRESS, USB_DEBUG_DEVNUM, 0, NULL, 0);
721 dbgp_printk("Could not move attached device to %d\n",
725 devnum = USB_DEBUG_DEVNUM;
726 dbgp_printk("debug device renamed to 127\n");
729 /* Enable the debug interface */
730 ret = dbgp_control_msg(USB_DEBUG_DEVNUM,
731 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
732 USB_REQ_SET_FEATURE, USB_DEVICE_DEBUG_MODE, 0, NULL, 0);
734 dbgp_printk(" Could not enable the debug device\n");
737 dbgp_printk("debug interface enabled\n");
739 /* Perform a small write to get the even/odd data state in sync
741 ret = dbgp_bulk_write(USB_DEBUG_DEVNUM, dbgp_endpoint_out, " ", 1);
743 dbgp_printk("dbgp_bulk_write failed: %d\n", ret);
746 dbgp_printk("small write doned\n");
750 /* Things didn't work so remove my claim */
751 ctrl = readl(&ehci_debug->control);
752 ctrl &= ~(DBGP_CLAIM | DBGP_OUT);
753 writel(ctrl, &ehci_debug->control);
757 port_map_tried |= (1<<(debug_port - 1));
758 new_debug_port = ((debug_port-1+1)%n_ports) + 1;
759 if (port_map_tried != ((1<<n_ports) - 1)) {
760 set_debug_port(new_debug_port);
764 set_debug_port(new_debug_port);
771 static int __init early_dbgp_init(char *s)
773 u32 debug_port, bar, offset;
774 u32 bus, slot, func, cap;
775 void __iomem *ehci_bar;
782 if (!early_pci_allowed())
787 dbgp_num = simple_strtoul(s, &e, 10);
788 dbgp_printk("dbgp_num: %d\n", dbgp_num);
790 cap = find_dbgp(dbgp_num, &bus, &slot, &func);
794 dbgp_printk("Found EHCI debug port on %02x:%02x.%1x\n", bus, slot,
797 debug_port = read_pci_config(bus, slot, func, cap);
798 bar = (debug_port >> 29) & 0x7;
799 bar = (bar * 4) + 0xc;
800 offset = (debug_port >> 16) & 0xfff;
801 dbgp_printk("bar: %02x offset: %03x\n", bar, offset);
802 if (bar != PCI_BASE_ADDRESS_0) {
803 dbgp_printk("only debug ports on bar 1 handled.\n");
808 bar_val = read_pci_config(bus, slot, func, PCI_BASE_ADDRESS_0);
809 dbgp_printk("bar_val: %02x offset: %03x\n", bar_val, offset);
810 if (bar_val & ~PCI_BASE_ADDRESS_MEM_MASK) {
811 dbgp_printk("only simple 32bit mmio bars supported\n");
816 /* double check if the mem space is enabled */
817 byte = read_pci_config_byte(bus, slot, func, 0x04);
820 write_pci_config_byte(bus, slot, func, 0x04, byte);
821 dbgp_printk("mmio for ehci enabled\n");
825 * FIXME I don't have the bar size so just guess PAGE_SIZE is more
826 * than enough. 1K is the biggest I have seen.
828 set_fixmap_nocache(FIX_DBGP_BASE, bar_val & PAGE_MASK);
829 ehci_bar = (void __iomem *)__fix_to_virt(FIX_DBGP_BASE);
830 ehci_bar += bar_val & ~PAGE_MASK;
831 dbgp_printk("ehci_bar: %p\n", ehci_bar);
833 ehci_caps = ehci_bar;
834 ehci_regs = ehci_bar + HC_LENGTH(readl(&ehci_caps->hc_capbase));
835 ehci_debug = ehci_bar + offset;
837 ehci_dev.slot = slot;
838 ehci_dev.func = func;
840 detect_set_debug_port();
844 dbgp_printk("ehci_setup failed\n");
853 static void early_dbgp_write(struct console *con, const char *str, u32 n)
861 if (chunk > DBGP_MAX_PACKET)
862 chunk = DBGP_MAX_PACKET;
863 ret = dbgp_bulk_write(USB_DEBUG_DEVNUM,
864 dbgp_endpoint_out, str, chunk);
870 static struct console early_dbgp_console = {
872 .write = early_dbgp_write,
873 .flags = CON_PRINTBUFFER,
878 /* Direct interface for emergencies */
879 static struct console *early_console = &early_vga_console;
880 static int __initdata early_console_initialized;
882 asmlinkage void early_printk(const char *fmt, ...)
889 n = vscnprintf(buf, sizeof(buf), fmt, ap);
890 early_console->write(early_console, buf, n);
895 static int __init setup_early_printk(char *buf)
902 if (early_console_initialized)
904 early_console_initialized = 1;
906 keep_early = (strstr(buf, "keep") != NULL);
908 if (!strncmp(buf, "serial", 6)) {
909 early_serial_init(buf + 6);
910 early_console = &early_serial_console;
911 } else if (!strncmp(buf, "ttyS", 4)) {
912 early_serial_init(buf);
913 early_console = &early_serial_console;
914 } else if (!strncmp(buf, "vga", 3)
915 && boot_params.screen_info.orig_video_isVGA == 1) {
916 max_xpos = boot_params.screen_info.orig_video_cols;
917 max_ypos = boot_params.screen_info.orig_video_lines;
918 current_ypos = boot_params.screen_info.orig_y;
919 early_console = &early_vga_console;
920 #ifdef CONFIG_EARLY_PRINTK_DBGP
921 } else if (!strncmp(buf, "dbgp", 4)) {
922 if (early_dbgp_init(buf+4) < 0)
924 early_console = &early_dbgp_console;
926 * usb subsys will reset ehci controller, so don't keep
931 #ifdef CONFIG_HVC_XEN
932 } else if (!strncmp(buf, "xen", 3)) {
933 early_console = &xenboot_console;
938 early_console->flags &= ~CON_BOOT;
940 early_console->flags |= CON_BOOT;
941 register_console(early_console);
945 early_param("earlyprintk", setup_early_printk);