f0a41c647bef467ca005366b0ef429c843ac4824
[safe/jmp/linux-2.6] / drivers / usb / early / ehci-dbgp.c
1 /*
2  * Standalone EHCI usb debug driver
3  *
4  * Originally written by:
5  *  Eric W. Biederman" <ebiederm@xmission.com> and
6  *  Yinghai Lu <yhlu.kernel@gmail.com>
7  *
8  * Changes for early/late printk and HW errata:
9  *  Jason Wessel <jason.wessel@windriver.com>
10  *  Copyright (C) 2009 Wind River Systems, Inc.
11  *
12  */
13
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 <asm/io.h>
23 #include <asm/pci-direct.h>
24 #include <asm/fixmap.h>
25
26 /* The code here is intended to talk directly to the EHCI debug port
27  * and does not require that you have any kind of USB host controller
28  * drivers or USB device drivers compiled into the kernel.
29  *
30  * If you make a change to anything in here, the following test cases
31  * need to pass where a USB debug device works in the following
32  * configurations.
33  *
34  * 1. boot args:  earlyprintk=dbgp
35  *     o kernel compiled with # CONFIG_USB_EHCI_HCD is not set
36  *     o kernel compiled with CONFIG_USB_EHCI_HCD=y
37  * 2. boot args: earlyprintk=dbgp,keep
38  *     o kernel compiled with # CONFIG_USB_EHCI_HCD is not set
39  *     o kernel compiled with CONFIG_USB_EHCI_HCD=y
40  * 3. boot args: earlyprintk=dbgp console=ttyUSB0
41  *     o kernel has CONFIG_USB_EHCI_HCD=y and
42  *       CONFIG_USB_SERIAL_DEBUG=y
43  * 4. boot args: earlyprintk=vga,dbgp
44  *     o kernel compiled with # CONFIG_USB_EHCI_HCD is not set
45  *     o kernel compiled with CONFIG_USB_EHCI_HCD=y
46  *
47  * For the 4th configuration you can turn on or off the DBGP_DEBUG
48  * such that you can debug the dbgp device's driver code.
49  */
50
51 static int dbgp_phys_port = 1;
52
53 static struct ehci_caps __iomem *ehci_caps;
54 static struct ehci_regs __iomem *ehci_regs;
55 static struct ehci_dbg_port __iomem *ehci_debug;
56 static int dbgp_not_safe; /* Cannot use debug device during ehci reset */
57 static unsigned int dbgp_endpoint_out;
58
59 struct ehci_dev {
60         u32 bus;
61         u32 slot;
62         u32 func;
63 };
64
65 static struct ehci_dev ehci_dev;
66
67 #define USB_DEBUG_DEVNUM 127
68
69 #define DBGP_DATA_TOGGLE        0x8800
70
71 #ifdef DBGP_DEBUG
72 #define dbgp_printk printk
73 static void dbgp_ehci_status(char *str)
74 {
75         if (!ehci_debug)
76                 return;
77         dbgp_printk("dbgp: %s\n", str);
78         dbgp_printk("  Debug control: %08x", readl(&ehci_debug->control));
79         dbgp_printk("  ehci cmd     : %08x", readl(&ehci_regs->command));
80         dbgp_printk("  ehci conf flg: %08x\n",
81                     readl(&ehci_regs->configured_flag));
82         dbgp_printk("  ehci status  : %08x", readl(&ehci_regs->status));
83         dbgp_printk("  ehci portsc  : %08x\n",
84                     readl(&ehci_regs->port_status[dbgp_phys_port - 1]));
85 }
86 #else
87 static inline void dbgp_ehci_status(char *str) { }
88 static inline void dbgp_printk(const char *fmt, ...) { }
89 #endif
90
91 static inline u32 dbgp_pid_update(u32 x, u32 tok)
92 {
93         return ((x ^ DBGP_DATA_TOGGLE) & 0xffff00) | (tok & 0xff);
94 }
95
96 static inline u32 dbgp_len_update(u32 x, u32 len)
97 {
98         return (x & ~0x0f) | (len & 0x0f);
99 }
100
101 /*
102  * USB Packet IDs (PIDs)
103  */
104
105 /* token */
106 #define USB_PID_OUT             0xe1
107 #define USB_PID_IN              0x69
108 #define USB_PID_SOF             0xa5
109 #define USB_PID_SETUP           0x2d
110 /* handshake */
111 #define USB_PID_ACK             0xd2
112 #define USB_PID_NAK             0x5a
113 #define USB_PID_STALL           0x1e
114 #define USB_PID_NYET            0x96
115 /* data */
116 #define USB_PID_DATA0           0xc3
117 #define USB_PID_DATA1           0x4b
118 #define USB_PID_DATA2           0x87
119 #define USB_PID_MDATA           0x0f
120 /* Special */
121 #define USB_PID_PREAMBLE        0x3c
122 #define USB_PID_ERR             0x3c
123 #define USB_PID_SPLIT           0x78
124 #define USB_PID_PING            0xb4
125 #define USB_PID_UNDEF_0         0xf0
126
127 #define USB_PID_DATA_TOGGLE     0x88
128 #define DBGP_CLAIM (DBGP_OWNER | DBGP_ENABLED | DBGP_INUSE)
129
130 #define PCI_CAP_ID_EHCI_DEBUG   0xa
131
132 #define HUB_ROOT_RESET_TIME     50      /* times are in msec */
133 #define HUB_SHORT_RESET_TIME    10
134 #define HUB_LONG_RESET_TIME     200
135 #define HUB_RESET_TIMEOUT       500
136
137 #define DBGP_MAX_PACKET         8
138 #define DBGP_TIMEOUT            (250 * 1000)
139
140 static int dbgp_wait_until_complete(void)
141 {
142         u32 ctrl;
143         int loop = DBGP_TIMEOUT;
144
145         do {
146                 ctrl = readl(&ehci_debug->control);
147                 /* Stop when the transaction is finished */
148                 if (ctrl & DBGP_DONE)
149                         break;
150                 udelay(1);
151         } while (--loop > 0);
152
153         if (!loop)
154                 return -DBGP_TIMEOUT;
155
156         /*
157          * Now that we have observed the completed transaction,
158          * clear the done bit.
159          */
160         writel(ctrl | DBGP_DONE, &ehci_debug->control);
161         return (ctrl & DBGP_ERROR) ? -DBGP_ERRCODE(ctrl) : DBGP_LEN(ctrl);
162 }
163
164 static inline void dbgp_mdelay(int ms)
165 {
166         int i;
167
168         while (ms--) {
169                 for (i = 0; i < 1000; i++)
170                         outb(0x1, 0x80);
171         }
172 }
173
174 static void dbgp_breath(void)
175 {
176         /* Sleep to give the debug port a chance to breathe */
177 }
178
179 static int dbgp_wait_until_done(unsigned ctrl)
180 {
181         u32 pids, lpid;
182         int ret;
183         int loop = 3;
184
185 retry:
186         writel(ctrl | DBGP_GO, &ehci_debug->control);
187         ret = dbgp_wait_until_complete();
188         pids = readl(&ehci_debug->pids);
189         lpid = DBGP_PID_GET(pids);
190
191         if (ret < 0) {
192                 /* A -DBGP_TIMEOUT failure here means the device has
193                  * failed, perhaps because it was unplugged, in which
194                  * case we do not want to hang the system so the dbgp
195                  * will be marked as unsafe to use.  EHCI reset is the
196                  * only way to recover if you unplug the dbgp device.
197                  */
198                 if (ret == -DBGP_TIMEOUT && !dbgp_not_safe)
199                         dbgp_not_safe = 1;
200                 return ret;
201         }
202
203         /*
204          * If the port is getting full or it has dropped data
205          * start pacing ourselves, not necessary but it's friendly.
206          */
207         if ((lpid == USB_PID_NAK) || (lpid == USB_PID_NYET))
208                 dbgp_breath();
209
210         /* If I get a NACK reissue the transmission */
211         if (lpid == USB_PID_NAK) {
212                 if (--loop > 0)
213                         goto retry;
214         }
215
216         return ret;
217 }
218
219 static inline void dbgp_set_data(const void *buf, int size)
220 {
221         const unsigned char *bytes = buf;
222         u32 lo, hi;
223         int i;
224
225         lo = hi = 0;
226         for (i = 0; i < 4 && i < size; i++)
227                 lo |= bytes[i] << (8*i);
228         for (; i < 8 && i < size; i++)
229                 hi |= bytes[i] << (8*(i - 4));
230         writel(lo, &ehci_debug->data03);
231         writel(hi, &ehci_debug->data47);
232 }
233
234 static inline void dbgp_get_data(void *buf, int size)
235 {
236         unsigned char *bytes = buf;
237         u32 lo, hi;
238         int i;
239
240         lo = readl(&ehci_debug->data03);
241         hi = readl(&ehci_debug->data47);
242         for (i = 0; i < 4 && i < size; i++)
243                 bytes[i] = (lo >> (8*i)) & 0xff;
244         for (; i < 8 && i < size; i++)
245                 bytes[i] = (hi >> (8*(i - 4))) & 0xff;
246 }
247
248 static int dbgp_bulk_write(unsigned devnum, unsigned endpoint,
249                          const char *bytes, int size)
250 {
251         u32 pids, addr, ctrl;
252         int ret;
253
254         if (size > DBGP_MAX_PACKET)
255                 return -1;
256
257         addr = DBGP_EPADDR(devnum, endpoint);
258
259         pids = readl(&ehci_debug->pids);
260         pids = dbgp_pid_update(pids, USB_PID_OUT);
261
262         ctrl = readl(&ehci_debug->control);
263         ctrl = dbgp_len_update(ctrl, size);
264         ctrl |= DBGP_OUT;
265         ctrl |= DBGP_GO;
266
267         dbgp_set_data(bytes, size);
268         writel(addr, &ehci_debug->address);
269         writel(pids, &ehci_debug->pids);
270
271         ret = dbgp_wait_until_done(ctrl);
272         if (ret < 0)
273                 return ret;
274
275         return ret;
276 }
277
278 static int dbgp_bulk_read(unsigned devnum, unsigned endpoint, void *data,
279                                  int size)
280 {
281         u32 pids, addr, ctrl;
282         int ret;
283
284         if (size > DBGP_MAX_PACKET)
285                 return -1;
286
287         addr = DBGP_EPADDR(devnum, endpoint);
288
289         pids = readl(&ehci_debug->pids);
290         pids = dbgp_pid_update(pids, USB_PID_IN);
291
292         ctrl = readl(&ehci_debug->control);
293         ctrl = dbgp_len_update(ctrl, size);
294         ctrl &= ~DBGP_OUT;
295         ctrl |= DBGP_GO;
296
297         writel(addr, &ehci_debug->address);
298         writel(pids, &ehci_debug->pids);
299         ret = dbgp_wait_until_done(ctrl);
300         if (ret < 0)
301                 return ret;
302
303         if (size > ret)
304                 size = ret;
305         dbgp_get_data(data, size);
306         return ret;
307 }
308
309 static int dbgp_control_msg(unsigned devnum, int requesttype,
310         int request, int value, int index, void *data, int size)
311 {
312         u32 pids, addr, ctrl;
313         struct usb_ctrlrequest req;
314         int read;
315         int ret;
316
317         read = (requesttype & USB_DIR_IN) != 0;
318         if (size > (read ? DBGP_MAX_PACKET:0))
319                 return -1;
320
321         /* Compute the control message */
322         req.bRequestType = requesttype;
323         req.bRequest = request;
324         req.wValue = cpu_to_le16(value);
325         req.wIndex = cpu_to_le16(index);
326         req.wLength = cpu_to_le16(size);
327
328         pids = DBGP_PID_SET(USB_PID_DATA0, USB_PID_SETUP);
329         addr = DBGP_EPADDR(devnum, 0);
330
331         ctrl = readl(&ehci_debug->control);
332         ctrl = dbgp_len_update(ctrl, sizeof(req));
333         ctrl |= DBGP_OUT;
334         ctrl |= DBGP_GO;
335
336         /* Send the setup message */
337         dbgp_set_data(&req, sizeof(req));
338         writel(addr, &ehci_debug->address);
339         writel(pids, &ehci_debug->pids);
340         ret = dbgp_wait_until_done(ctrl);
341         if (ret < 0)
342                 return ret;
343
344         /* Read the result */
345         return dbgp_bulk_read(devnum, 0, data, size);
346 }
347
348
349 /* Find a PCI capability */
350 static u32 __init find_cap(u32 num, u32 slot, u32 func, int cap)
351 {
352         u8 pos;
353         int bytes;
354
355         if (!(read_pci_config_16(num, slot, func, PCI_STATUS) &
356                 PCI_STATUS_CAP_LIST))
357                 return 0;
358
359         pos = read_pci_config_byte(num, slot, func, PCI_CAPABILITY_LIST);
360         for (bytes = 0; bytes < 48 && pos >= 0x40; bytes++) {
361                 u8 id;
362
363                 pos &= ~3;
364                 id = read_pci_config_byte(num, slot, func, pos+PCI_CAP_LIST_ID);
365                 if (id == 0xff)
366                         break;
367                 if (id == cap)
368                         return pos;
369
370                 pos = read_pci_config_byte(num, slot, func,
371                                                  pos+PCI_CAP_LIST_NEXT);
372         }
373         return 0;
374 }
375
376 static u32 __init __find_dbgp(u32 bus, u32 slot, u32 func)
377 {
378         u32 class;
379
380         class = read_pci_config(bus, slot, func, PCI_CLASS_REVISION);
381         if ((class >> 8) != PCI_CLASS_SERIAL_USB_EHCI)
382                 return 0;
383
384         return find_cap(bus, slot, func, PCI_CAP_ID_EHCI_DEBUG);
385 }
386
387 static u32 __init find_dbgp(int ehci_num, u32 *rbus, u32 *rslot, u32 *rfunc)
388 {
389         u32 bus, slot, func;
390
391         for (bus = 0; bus < 256; bus++) {
392                 for (slot = 0; slot < 32; slot++) {
393                         for (func = 0; func < 8; func++) {
394                                 unsigned cap;
395
396                                 cap = __find_dbgp(bus, slot, func);
397
398                                 if (!cap)
399                                         continue;
400                                 if (ehci_num-- != 0)
401                                         continue;
402                                 *rbus = bus;
403                                 *rslot = slot;
404                                 *rfunc = func;
405                                 return cap;
406                         }
407                 }
408         }
409         return 0;
410 }
411
412 static int dbgp_ehci_startup(void)
413 {
414         u32 ctrl, cmd, status;
415         int loop;
416
417         /* Claim ownership, but do not enable yet */
418         ctrl = readl(&ehci_debug->control);
419         ctrl |= DBGP_OWNER;
420         ctrl &= ~(DBGP_ENABLED | DBGP_INUSE);
421         writel(ctrl, &ehci_debug->control);
422         udelay(1);
423
424         dbgp_ehci_status("EHCI startup");
425         /* Start the ehci running */
426         cmd = readl(&ehci_regs->command);
427         cmd &= ~(CMD_LRESET | CMD_IAAD | CMD_PSE | CMD_ASE | CMD_RESET);
428         cmd |= CMD_RUN;
429         writel(cmd, &ehci_regs->command);
430
431         /* Ensure everything is routed to the EHCI */
432         writel(FLAG_CF, &ehci_regs->configured_flag);
433
434         /* Wait until the controller is no longer halted */
435         loop = 10;
436         do {
437                 status = readl(&ehci_regs->status);
438                 if (!(status & STS_HALT))
439                         break;
440                 udelay(1);
441         } while (--loop > 0);
442
443         if (!loop) {
444                 dbgp_printk("ehci can not be started\n");
445                 return -ENODEV;
446         }
447         dbgp_printk("ehci started\n");
448         return 0;
449 }
450
451 static int dbgp_ehci_controller_reset(void)
452 {
453         int loop = 250 * 1000;
454         u32 cmd;
455
456         /* Reset the EHCI controller */
457         cmd = readl(&ehci_regs->command);
458         cmd |= CMD_RESET;
459         writel(cmd, &ehci_regs->command);
460         do {
461                 cmd = readl(&ehci_regs->command);
462         } while ((cmd & CMD_RESET) && (--loop > 0));
463
464         if (!loop) {
465                 dbgp_printk("can not reset ehci\n");
466                 return -1;
467         }
468         dbgp_ehci_status("ehci reset done");
469         return 0;
470 }
471 static int ehci_wait_for_port(int port);
472 /* Return 0 on success
473  * Return -ENODEV for any general failure
474  * Return -EIO if wait for port fails
475  */
476 int dbgp_external_startup(void)
477 {
478         int devnum;
479         struct usb_debug_descriptor dbgp_desc;
480         int ret;
481         u32 ctrl, portsc, cmd;
482         int dbg_port = dbgp_phys_port;
483         int tries = 3;
484         int reset_port_tries = 1;
485         int try_hard_once = 1;
486
487 try_port_reset_again:
488         ret = dbgp_ehci_startup();
489         if (ret)
490                 return ret;
491
492         /* Wait for a device to show up in the debug port */
493         ret = ehci_wait_for_port(dbg_port);
494         if (ret < 0) {
495                 portsc = readl(&ehci_regs->port_status[dbg_port - 1]);
496                 if (!(portsc & PORT_CONNECT) && try_hard_once) {
497                         /* Last ditch effort to try to force enable
498                          * the debug device by using the packet test
499                          * ehci command to try and wake it up. */
500                         try_hard_once = 0;
501                         cmd = readl(&ehci_regs->command);
502                         cmd &= ~CMD_RUN;
503                         writel(cmd, &ehci_regs->command);
504                         portsc = readl(&ehci_regs->port_status[dbg_port - 1]);
505                         portsc |= PORT_TEST_PKT;
506                         writel(portsc, &ehci_regs->port_status[dbg_port - 1]);
507                         dbgp_ehci_status("Trying to force debug port online");
508                         mdelay(50);
509                         dbgp_ehci_controller_reset();
510                         goto try_port_reset_again;
511                 } else if (reset_port_tries--) {
512                         goto try_port_reset_again;
513                 }
514                 dbgp_printk("No device found in debug port\n");
515                 return -EIO;
516         }
517         dbgp_ehci_status("wait for port done");
518
519         /* Enable the debug port */
520         ctrl = readl(&ehci_debug->control);
521         ctrl |= DBGP_CLAIM;
522         writel(ctrl, &ehci_debug->control);
523         ctrl = readl(&ehci_debug->control);
524         if ((ctrl & DBGP_CLAIM) != DBGP_CLAIM) {
525                 dbgp_printk("No device in debug port\n");
526                 writel(ctrl & ~DBGP_CLAIM, &ehci_debug->control);
527                 return -ENODEV;
528         }
529         dbgp_ehci_status("debug ported enabled");
530
531         /* Completely transfer the debug device to the debug controller */
532         portsc = readl(&ehci_regs->port_status[dbg_port - 1]);
533         portsc &= ~PORT_PE;
534         writel(portsc, &ehci_regs->port_status[dbg_port - 1]);
535
536         dbgp_mdelay(100);
537
538 try_again:
539         /* Find the debug device and make it device number 127 */
540         for (devnum = 0; devnum <= 127; devnum++) {
541                 ret = dbgp_control_msg(devnum,
542                         USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
543                         USB_REQ_GET_DESCRIPTOR, (USB_DT_DEBUG << 8), 0,
544                         &dbgp_desc, sizeof(dbgp_desc));
545                 if (ret > 0)
546                         break;
547         }
548         if (devnum > 127) {
549                 dbgp_printk("Could not find attached debug device\n");
550                 goto err;
551         }
552         if (ret < 0) {
553                 dbgp_printk("Attached device is not a debug device\n");
554                 goto err;
555         }
556         dbgp_endpoint_out = dbgp_desc.bDebugOutEndpoint;
557
558         /* Move the device to 127 if it isn't already there */
559         if (devnum != USB_DEBUG_DEVNUM) {
560                 ret = dbgp_control_msg(devnum,
561                         USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
562                         USB_REQ_SET_ADDRESS, USB_DEBUG_DEVNUM, 0, NULL, 0);
563                 if (ret < 0) {
564                         dbgp_printk("Could not move attached device to %d\n",
565                                 USB_DEBUG_DEVNUM);
566                         goto err;
567                 }
568                 devnum = USB_DEBUG_DEVNUM;
569                 dbgp_printk("debug device renamed to 127\n");
570         }
571
572         /* Enable the debug interface */
573         ret = dbgp_control_msg(USB_DEBUG_DEVNUM,
574                 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
575                 USB_REQ_SET_FEATURE, USB_DEVICE_DEBUG_MODE, 0, NULL, 0);
576         if (ret < 0) {
577                 dbgp_printk(" Could not enable the debug device\n");
578                 goto err;
579         }
580         dbgp_printk("debug interface enabled\n");
581         /* Perform a small write to get the even/odd data state in sync
582          */
583         ret = dbgp_bulk_write(USB_DEBUG_DEVNUM, dbgp_endpoint_out, " ", 1);
584         if (ret < 0) {
585                 dbgp_printk("dbgp_bulk_write failed: %d\n", ret);
586                 goto err;
587         }
588         dbgp_printk("small write doned\n");
589         dbgp_not_safe = 0;
590
591         return 0;
592 err:
593         if (tries--)
594                 goto try_again;
595         return -ENODEV;
596 }
597 EXPORT_SYMBOL_GPL(dbgp_external_startup);
598
599 static int __init ehci_reset_port(int port)
600 {
601         u32 portsc;
602         u32 delay_time, delay;
603         int loop;
604
605         dbgp_ehci_status("reset port");
606         /* Reset the usb debug port */
607         portsc = readl(&ehci_regs->port_status[port - 1]);
608         portsc &= ~PORT_PE;
609         portsc |= PORT_RESET;
610         writel(portsc, &ehci_regs->port_status[port - 1]);
611
612         delay = HUB_ROOT_RESET_TIME;
613         for (delay_time = 0; delay_time < HUB_RESET_TIMEOUT;
614              delay_time += delay) {
615                 dbgp_mdelay(delay);
616                 portsc = readl(&ehci_regs->port_status[port - 1]);
617                 if (!(portsc & PORT_RESET))
618                         break;
619         }
620                 if (portsc & PORT_RESET) {
621                         /* force reset to complete */
622                         loop = 100 * 1000;
623                         writel(portsc & ~(PORT_RWC_BITS | PORT_RESET),
624                                 &ehci_regs->port_status[port - 1]);
625                         do {
626                                 udelay(1);
627                                 portsc = readl(&ehci_regs->port_status[port-1]);
628                         } while ((portsc & PORT_RESET) && (--loop > 0));
629                 }
630
631                 /* Device went away? */
632                 if (!(portsc & PORT_CONNECT))
633                         return -ENOTCONN;
634
635                 /* bomb out completely if something weird happend */
636                 if ((portsc & PORT_CSC))
637                         return -EINVAL;
638
639                 /* If we've finished resetting, then break out of the loop */
640                 if (!(portsc & PORT_RESET) && (portsc & PORT_PE))
641                         return 0;
642         return -EBUSY;
643 }
644
645 static int ehci_wait_for_port(int port)
646 {
647         u32 status;
648         int ret, reps;
649
650         for (reps = 0; reps < 300; reps++) {
651                 status = readl(&ehci_regs->status);
652                 if (status & STS_PCD)
653                         break;
654                 dbgp_mdelay(1);
655         }
656         ret = ehci_reset_port(port);
657         if (ret == 0)
658                 return 0;
659         return -ENOTCONN;
660 }
661
662 typedef void (*set_debug_port_t)(int port);
663
664 static void __init default_set_debug_port(int port)
665 {
666 }
667
668 static set_debug_port_t __initdata set_debug_port = default_set_debug_port;
669
670 static void __init nvidia_set_debug_port(int port)
671 {
672         u32 dword;
673         dword = read_pci_config(ehci_dev.bus, ehci_dev.slot, ehci_dev.func,
674                                  0x74);
675         dword &= ~(0x0f<<12);
676         dword |= ((port & 0x0f)<<12);
677         write_pci_config(ehci_dev.bus, ehci_dev.slot, ehci_dev.func, 0x74,
678                                  dword);
679         dbgp_printk("set debug port to %d\n", port);
680 }
681
682 static void __init detect_set_debug_port(void)
683 {
684         u32 vendorid;
685
686         vendorid = read_pci_config(ehci_dev.bus, ehci_dev.slot, ehci_dev.func,
687                  0x00);
688
689         if ((vendorid & 0xffff) == 0x10de) {
690                 dbgp_printk("using nvidia set_debug_port\n");
691                 set_debug_port = nvidia_set_debug_port;
692         }
693 }
694
695 /* The code in early_ehci_bios_handoff() is derived from the usb pci
696  * quirk initialization, but altered so as to use the early PCI
697  * routines. */
698 #define EHCI_USBLEGSUP_BIOS     (1 << 16)       /* BIOS semaphore */
699 #define EHCI_USBLEGCTLSTS       4               /* legacy control/status */
700 static void __init early_ehci_bios_handoff(void)
701 {
702         u32 hcc_params = readl(&ehci_caps->hcc_params);
703         int offset = (hcc_params >> 8) & 0xff;
704         u32 cap;
705         int msec;
706
707         if (!offset)
708                 return;
709
710         cap = read_pci_config(ehci_dev.bus, ehci_dev.slot,
711                               ehci_dev.func, offset);
712         dbgp_printk("dbgp: ehci BIOS state %08x\n", cap);
713
714         if ((cap & 0xff) == 1 && (cap & EHCI_USBLEGSUP_BIOS)) {
715                 dbgp_printk("dbgp: BIOS handoff\n");
716                 write_pci_config_byte(ehci_dev.bus, ehci_dev.slot,
717                                       ehci_dev.func, offset + 3, 1);
718         }
719
720         /* if boot firmware now owns EHCI, spin till it hands it over. */
721         msec = 1000;
722         while ((cap & EHCI_USBLEGSUP_BIOS) && (msec > 0)) {
723                 mdelay(10);
724                 msec -= 10;
725                 cap = read_pci_config(ehci_dev.bus, ehci_dev.slot,
726                                       ehci_dev.func, offset);
727         }
728
729         if (cap & EHCI_USBLEGSUP_BIOS) {
730                 /* well, possibly buggy BIOS... try to shut it down,
731                  * and hope nothing goes too wrong */
732                 dbgp_printk("dbgp: BIOS handoff failed: %08x\n", cap);
733                 write_pci_config_byte(ehci_dev.bus, ehci_dev.slot,
734                                       ehci_dev.func, offset + 2, 0);
735         }
736
737         /* just in case, always disable EHCI SMIs */
738         write_pci_config_byte(ehci_dev.bus, ehci_dev.slot, ehci_dev.func,
739                               offset + EHCI_USBLEGCTLSTS, 0);
740 }
741
742 static int __init ehci_setup(void)
743 {
744         u32 ctrl, portsc, hcs_params;
745         u32 debug_port, new_debug_port = 0, n_ports;
746         int ret, i;
747         int port_map_tried;
748         int playtimes = 3;
749
750         early_ehci_bios_handoff();
751
752 try_next_time:
753         port_map_tried = 0;
754
755 try_next_port:
756
757         hcs_params = readl(&ehci_caps->hcs_params);
758         debug_port = HCS_DEBUG_PORT(hcs_params);
759         dbgp_phys_port = debug_port;
760         n_ports    = HCS_N_PORTS(hcs_params);
761
762         dbgp_printk("debug_port: %d\n", debug_port);
763         dbgp_printk("n_ports:    %d\n", n_ports);
764         dbgp_ehci_status("");
765
766         for (i = 1; i <= n_ports; i++) {
767                 portsc = readl(&ehci_regs->port_status[i-1]);
768                 dbgp_printk("portstatus%d: %08x\n", i, portsc);
769         }
770
771         if (port_map_tried && (new_debug_port != debug_port)) {
772                 if (--playtimes) {
773                         set_debug_port(new_debug_port);
774                         goto try_next_time;
775                 }
776                 return -1;
777         }
778
779         /* Only reset the controller if it is not already in the
780          * configured state */
781         if (!(readl(&ehci_regs->configured_flag) & FLAG_CF)) {
782                 if (dbgp_ehci_controller_reset() != 0)
783                         return -1;
784         } else {
785                 dbgp_ehci_status("ehci skip - already configured");
786         }
787
788         ret = dbgp_external_startup();
789         if (ret == -EIO)
790                 goto next_debug_port;
791
792         if (ret < 0) {
793                 /* Things didn't work so remove my claim */
794                 ctrl = readl(&ehci_debug->control);
795                 ctrl &= ~(DBGP_CLAIM | DBGP_OUT);
796                 writel(ctrl, &ehci_debug->control);
797                 return -1;
798         }
799         return 0;
800
801 next_debug_port:
802         port_map_tried |= (1<<(debug_port - 1));
803         new_debug_port = ((debug_port-1+1)%n_ports) + 1;
804         if (port_map_tried != ((1<<n_ports) - 1)) {
805                 set_debug_port(new_debug_port);
806                 goto try_next_port;
807         }
808         if (--playtimes) {
809                 set_debug_port(new_debug_port);
810                 goto try_next_time;
811         }
812
813         return -1;
814 }
815
816 int __init early_dbgp_init(char *s)
817 {
818         u32 debug_port, bar, offset;
819         u32 bus, slot, func, cap;
820         void __iomem *ehci_bar;
821         u32 dbgp_num;
822         u32 bar_val;
823         char *e;
824         int ret;
825         u8 byte;
826
827         if (!early_pci_allowed())
828                 return -1;
829
830         dbgp_num = 0;
831         if (*s)
832                 dbgp_num = simple_strtoul(s, &e, 10);
833         dbgp_printk("dbgp_num: %d\n", dbgp_num);
834
835         cap = find_dbgp(dbgp_num, &bus, &slot, &func);
836         if (!cap)
837                 return -1;
838
839         dbgp_printk("Found EHCI debug port on %02x:%02x.%1x\n", bus, slot,
840                          func);
841
842         debug_port = read_pci_config(bus, slot, func, cap);
843         bar = (debug_port >> 29) & 0x7;
844         bar = (bar * 4) + 0xc;
845         offset = (debug_port >> 16) & 0xfff;
846         dbgp_printk("bar: %02x offset: %03x\n", bar, offset);
847         if (bar != PCI_BASE_ADDRESS_0) {
848                 dbgp_printk("only debug ports on bar 1 handled.\n");
849
850                 return -1;
851         }
852
853         bar_val = read_pci_config(bus, slot, func, PCI_BASE_ADDRESS_0);
854         dbgp_printk("bar_val: %02x offset: %03x\n", bar_val, offset);
855         if (bar_val & ~PCI_BASE_ADDRESS_MEM_MASK) {
856                 dbgp_printk("only simple 32bit mmio bars supported\n");
857
858                 return -1;
859         }
860
861         /* double check if the mem space is enabled */
862         byte = read_pci_config_byte(bus, slot, func, 0x04);
863         if (!(byte & 0x2)) {
864                 byte  |= 0x02;
865                 write_pci_config_byte(bus, slot, func, 0x04, byte);
866                 dbgp_printk("mmio for ehci enabled\n");
867         }
868
869         /*
870          * FIXME I don't have the bar size so just guess PAGE_SIZE is more
871          * than enough.  1K is the biggest I have seen.
872          */
873         set_fixmap_nocache(FIX_DBGP_BASE, bar_val & PAGE_MASK);
874         ehci_bar = (void __iomem *)__fix_to_virt(FIX_DBGP_BASE);
875         ehci_bar += bar_val & ~PAGE_MASK;
876         dbgp_printk("ehci_bar: %p\n", ehci_bar);
877
878         ehci_caps  = ehci_bar;
879         ehci_regs  = ehci_bar + HC_LENGTH(readl(&ehci_caps->hc_capbase));
880         ehci_debug = ehci_bar + offset;
881         ehci_dev.bus = bus;
882         ehci_dev.slot = slot;
883         ehci_dev.func = func;
884
885         detect_set_debug_port();
886
887         ret = ehci_setup();
888         if (ret < 0) {
889                 dbgp_printk("ehci_setup failed\n");
890                 ehci_debug = NULL;
891
892                 return -1;
893         }
894         dbgp_ehci_status("early_init_complete");
895
896         return 0;
897 }
898
899 static void early_dbgp_write(struct console *con, const char *str, u32 n)
900 {
901         int chunk, ret;
902         char buf[DBGP_MAX_PACKET];
903         int use_cr = 0;
904         u32 cmd, ctrl;
905         int reset_run = 0;
906
907         if (!ehci_debug || dbgp_not_safe)
908                 return;
909
910         cmd = readl(&ehci_regs->command);
911         if (unlikely(!(cmd & CMD_RUN))) {
912                 /* If the ehci controller is not in the run state do extended
913                  * checks to see if the acpi or some other initialization also
914                  * reset the ehci debug port */
915                 ctrl = readl(&ehci_debug->control);
916                 if (!(ctrl & DBGP_ENABLED)) {
917                         dbgp_not_safe = 1;
918                         dbgp_external_startup();
919                 } else {
920                         cmd |= CMD_RUN;
921                         writel(cmd, &ehci_regs->command);
922                         reset_run = 1;
923                 }
924         }
925         while (n > 0) {
926                 for (chunk = 0; chunk < DBGP_MAX_PACKET && n > 0;
927                      str++, chunk++, n--) {
928                         if (!use_cr && *str == '\n') {
929                                 use_cr = 1;
930                                 buf[chunk] = '\r';
931                                 str--;
932                                 n++;
933                                 continue;
934                         }
935                         if (use_cr)
936                                 use_cr = 0;
937                         buf[chunk] = *str;
938                 }
939                 if (chunk > 0) {
940                         ret = dbgp_bulk_write(USB_DEBUG_DEVNUM,
941                                       dbgp_endpoint_out, buf, chunk);
942                 }
943         }
944         if (unlikely(reset_run)) {
945                 cmd = readl(&ehci_regs->command);
946                 cmd &= ~CMD_RUN;
947                 writel(cmd, &ehci_regs->command);
948         }
949 }
950
951 struct console early_dbgp_console = {
952         .name =         "earlydbg",
953         .write =        early_dbgp_write,
954         .flags =        CON_PRINTBUFFER,
955         .index =        -1,
956 };
957
958 int dbgp_reset_prep(void)
959 {
960         u32 ctrl;
961
962         dbgp_not_safe = 1;
963         if (!ehci_debug)
964                 return 0;
965
966         if (early_dbgp_console.index != -1 &&
967                 !(early_dbgp_console.flags & CON_BOOT))
968                 return 1;
969         /* This means the console is not initialized, or should get
970          * shutdown so as to allow for reuse of the usb device, which
971          * means it is time to shutdown the usb debug port. */
972         ctrl = readl(&ehci_debug->control);
973         if (ctrl & DBGP_ENABLED) {
974                 ctrl &= ~(DBGP_CLAIM);
975                 writel(ctrl, &ehci_debug->control);
976         }
977         return 0;
978 }
979 EXPORT_SYMBOL_GPL(dbgp_reset_prep);