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