blob: e426ad626d7498bf5e6537ac100c7a508979066a [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>
Jason Wessel4fe1da42010-05-20 21:04:31 -050022#include <linux/serial_core.h>
23#include <linux/kgdb.h>
24#include <linux/kthread.h>
Jason Wesseldf6c5162009-08-20 15:39:48 -050025#include <asm/io.h>
26#include <asm/pci-direct.h>
27#include <asm/fixmap.h>
28
Jason Wessel91777822009-08-20 15:39:53 -050029/* 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.
32 *
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.
36 *
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
49 *
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.
52 */
53
54static int dbgp_phys_port = 1;
Jason Wessel56faf0f2009-08-20 15:39:51 -050055
Jason Wesseldf6c5162009-08-20 15:39:48 -050056static struct ehci_caps __iomem *ehci_caps;
57static struct ehci_regs __iomem *ehci_regs;
58static struct ehci_dbg_port __iomem *ehci_debug;
Jason Wessel91777822009-08-20 15:39:53 -050059static int dbgp_not_safe; /* Cannot use debug device during ehci reset */
Jason Wesseldf6c5162009-08-20 15:39:48 -050060static unsigned int dbgp_endpoint_out;
Jason Wessel4fe1da42010-05-20 21:04:31 -050061static unsigned int dbgp_endpoint_in;
Jason Wesseldf6c5162009-08-20 15:39:48 -050062
63struct ehci_dev {
64 u32 bus;
65 u32 slot;
66 u32 func;
67};
68
69static struct ehci_dev ehci_dev;
70
71#define USB_DEBUG_DEVNUM 127
72
Jason Wessel91777822009-08-20 15:39:53 -050073#ifdef DBGP_DEBUG
74#define dbgp_printk printk
75static void dbgp_ehci_status(char *str)
76{
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]));
87}
88#else
89static inline void dbgp_ehci_status(char *str) { }
90static inline void dbgp_printk(const char *fmt, ...) { }
91#endif
92
Jason Wesseldf6c5162009-08-20 15:39:48 -050093static inline u32 dbgp_len_update(u32 x, u32 len)
94{
95 return (x & ~0x0f) | (len & 0x0f);
96}
97
Jason Wessel4fe1da42010-05-20 21:04:31 -050098#ifdef CONFIG_KGDB
99static 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
104
Jan Anderssonc4301312011-05-03 20:11:57 +0200105/* Local version of HC_LENGTH macro as ehci struct is not available here */
106#define EARLY_HC_LENGTH(p) (0x00ff & (p)) /* bits 7 : 0 */
107
Jason Wesseldf6c5162009-08-20 15:39:48 -0500108/*
109 * USB Packet IDs (PIDs)
110 */
111
112/* token */
113#define USB_PID_OUT 0xe1
114#define USB_PID_IN 0x69
115#define USB_PID_SOF 0xa5
116#define USB_PID_SETUP 0x2d
117/* handshake */
118#define USB_PID_ACK 0xd2
119#define USB_PID_NAK 0x5a
120#define USB_PID_STALL 0x1e
121#define USB_PID_NYET 0x96
122/* data */
123#define USB_PID_DATA0 0xc3
124#define USB_PID_DATA1 0x4b
125#define USB_PID_DATA2 0x87
126#define USB_PID_MDATA 0x0f
127/* Special */
128#define USB_PID_PREAMBLE 0x3c
129#define USB_PID_ERR 0x3c
130#define USB_PID_SPLIT 0x78
131#define USB_PID_PING 0xb4
132#define USB_PID_UNDEF_0 0xf0
133
134#define USB_PID_DATA_TOGGLE 0x88
135#define DBGP_CLAIM (DBGP_OWNER | DBGP_ENABLED | DBGP_INUSE)
136
137#define PCI_CAP_ID_EHCI_DEBUG 0xa
138
139#define HUB_ROOT_RESET_TIME 50 /* times are in msec */
140#define HUB_SHORT_RESET_TIME 10
141#define HUB_LONG_RESET_TIME 200
142#define HUB_RESET_TIMEOUT 500
143
144#define DBGP_MAX_PACKET 8
Jason Wessel91777822009-08-20 15:39:53 -0500145#define DBGP_TIMEOUT (250 * 1000)
Jason Wessel815e1732010-02-05 11:49:05 -0600146#define DBGP_LOOPS 1000
147
148static inline u32 dbgp_pid_write_update(u32 x, u32 tok)
149{
150 static int data0 = USB_PID_DATA1;
151 data0 ^= USB_PID_DATA_TOGGLE;
152 return (x & 0xffff0000) | (data0 << 8) | (tok & 0xff);
153}
154
155static inline u32 dbgp_pid_read_update(u32 x, u32 tok)
156{
157 return (x & 0xffff0000) | (USB_PID_DATA0 << 8) | (tok & 0xff);
158}
Jason Wesseldf6c5162009-08-20 15:39:48 -0500159
160static int dbgp_wait_until_complete(void)
161{
162 u32 ctrl;
Jason Wessel91777822009-08-20 15:39:53 -0500163 int loop = DBGP_TIMEOUT;
Jason Wesseldf6c5162009-08-20 15:39:48 -0500164
165 do {
166 ctrl = readl(&ehci_debug->control);
167 /* Stop when the transaction is finished */
168 if (ctrl & DBGP_DONE)
169 break;
Jason Wessel91777822009-08-20 15:39:53 -0500170 udelay(1);
Jason Wesseldf6c5162009-08-20 15:39:48 -0500171 } while (--loop > 0);
172
173 if (!loop)
Jason Wessel91777822009-08-20 15:39:53 -0500174 return -DBGP_TIMEOUT;
Jason Wesseldf6c5162009-08-20 15:39:48 -0500175
176 /*
177 * Now that we have observed the completed transaction,
178 * clear the done bit.
179 */
180 writel(ctrl | DBGP_DONE, &ehci_debug->control);
181 return (ctrl & DBGP_ERROR) ? -DBGP_ERRCODE(ctrl) : DBGP_LEN(ctrl);
182}
183
Jason Wessel91777822009-08-20 15:39:53 -0500184static inline void dbgp_mdelay(int ms)
Jason Wesseldf6c5162009-08-20 15:39:48 -0500185{
186 int i;
187
188 while (ms--) {
189 for (i = 0; i < 1000; i++)
190 outb(0x1, 0x80);
191 }
192}
193
194static void dbgp_breath(void)
195{
196 /* Sleep to give the debug port a chance to breathe */
197}
198
Jason Wessel4fe1da42010-05-20 21:04:31 -0500199static int dbgp_wait_until_done(unsigned ctrl, int loop)
Jason Wesseldf6c5162009-08-20 15:39:48 -0500200{
201 u32 pids, lpid;
202 int ret;
Jason Wesseldf6c5162009-08-20 15:39:48 -0500203
204retry:
205 writel(ctrl | DBGP_GO, &ehci_debug->control);
206 ret = dbgp_wait_until_complete();
207 pids = readl(&ehci_debug->pids);
208 lpid = DBGP_PID_GET(pids);
209
Jason Wessel91777822009-08-20 15:39:53 -0500210 if (ret < 0) {
211 /* A -DBGP_TIMEOUT failure here means the device has
212 * failed, perhaps because it was unplugged, in which
213 * case we do not want to hang the system so the dbgp
214 * will be marked as unsafe to use. EHCI reset is the
215 * only way to recover if you unplug the dbgp device.
216 */
217 if (ret == -DBGP_TIMEOUT && !dbgp_not_safe)
218 dbgp_not_safe = 1;
Jason Wessel815e1732010-02-05 11:49:05 -0600219 if (ret == -DBGP_ERR_BAD && --loop > 0)
220 goto retry;
Jason Wesseldf6c5162009-08-20 15:39:48 -0500221 return ret;
Jason Wessel91777822009-08-20 15:39:53 -0500222 }
Jason Wesseldf6c5162009-08-20 15:39:48 -0500223
224 /*
225 * If the port is getting full or it has dropped data
226 * start pacing ourselves, not necessary but it's friendly.
227 */
228 if ((lpid == USB_PID_NAK) || (lpid == USB_PID_NYET))
229 dbgp_breath();
230
231 /* If I get a NACK reissue the transmission */
232 if (lpid == USB_PID_NAK) {
233 if (--loop > 0)
234 goto retry;
235 }
236
237 return ret;
238}
239
Jason Wessel91777822009-08-20 15:39:53 -0500240static inline void dbgp_set_data(const void *buf, int size)
Jason Wesseldf6c5162009-08-20 15:39:48 -0500241{
242 const unsigned char *bytes = buf;
243 u32 lo, hi;
244 int i;
245
246 lo = hi = 0;
247 for (i = 0; i < 4 && i < size; i++)
248 lo |= bytes[i] << (8*i);
249 for (; i < 8 && i < size; i++)
250 hi |= bytes[i] << (8*(i - 4));
251 writel(lo, &ehci_debug->data03);
252 writel(hi, &ehci_debug->data47);
253}
254
Jason Wessel91777822009-08-20 15:39:53 -0500255static inline void dbgp_get_data(void *buf, int size)
Jason Wesseldf6c5162009-08-20 15:39:48 -0500256{
257 unsigned char *bytes = buf;
258 u32 lo, hi;
259 int i;
260
261 lo = readl(&ehci_debug->data03);
262 hi = readl(&ehci_debug->data47);
263 for (i = 0; i < 4 && i < size; i++)
264 bytes[i] = (lo >> (8*i)) & 0xff;
265 for (; i < 8 && i < size; i++)
266 bytes[i] = (hi >> (8*(i - 4))) & 0xff;
267}
268
Jason Wessel815e1732010-02-05 11:49:05 -0600269static int dbgp_bulk_write(unsigned devnum, unsigned endpoint,
270 const char *bytes, int size)
Jason Wesseldf6c5162009-08-20 15:39:48 -0500271{
Jason Wessel815e1732010-02-05 11:49:05 -0600272 int ret;
273 u32 addr;
Jason Wessel68d29562009-08-20 15:39:56 -0500274 u32 pids, ctrl;
Jason Wesseldf6c5162009-08-20 15:39:48 -0500275
Jason Wessel815e1732010-02-05 11:49:05 -0600276 if (size > DBGP_MAX_PACKET)
277 return -1;
278
279 addr = DBGP_EPADDR(devnum, endpoint);
280
Jason Wesseldf6c5162009-08-20 15:39:48 -0500281 pids = readl(&ehci_debug->pids);
Jason Wessel815e1732010-02-05 11:49:05 -0600282 pids = dbgp_pid_write_update(pids, USB_PID_OUT);
Jason Wesseldf6c5162009-08-20 15:39:48 -0500283
284 ctrl = readl(&ehci_debug->control);
285 ctrl = dbgp_len_update(ctrl, size);
286 ctrl |= DBGP_OUT;
287 ctrl |= DBGP_GO;
288
289 dbgp_set_data(bytes, size);
290 writel(addr, &ehci_debug->address);
291 writel(pids, &ehci_debug->pids);
Jason Wessel4fe1da42010-05-20 21:04:31 -0500292 ret = dbgp_wait_until_done(ctrl, DBGP_LOOPS);
Jason Wesseldf6c5162009-08-20 15:39:48 -0500293
294 return ret;
295}
296
Jason Wessel91777822009-08-20 15:39:53 -0500297static int dbgp_bulk_read(unsigned devnum, unsigned endpoint, void *data,
Jason Wessel4fe1da42010-05-20 21:04:31 -0500298 int size, int loops)
Jason Wesseldf6c5162009-08-20 15:39:48 -0500299{
300 u32 pids, addr, ctrl;
301 int ret;
302
303 if (size > DBGP_MAX_PACKET)
304 return -1;
305
306 addr = DBGP_EPADDR(devnum, endpoint);
307
308 pids = readl(&ehci_debug->pids);
Jason Wessel815e1732010-02-05 11:49:05 -0600309 pids = dbgp_pid_read_update(pids, USB_PID_IN);
Jason Wesseldf6c5162009-08-20 15:39:48 -0500310
311 ctrl = readl(&ehci_debug->control);
312 ctrl = dbgp_len_update(ctrl, size);
313 ctrl &= ~DBGP_OUT;
314 ctrl |= DBGP_GO;
315
316 writel(addr, &ehci_debug->address);
317 writel(pids, &ehci_debug->pids);
Jason Wessel4fe1da42010-05-20 21:04:31 -0500318 ret = dbgp_wait_until_done(ctrl, loops);
Jason Wesseldf6c5162009-08-20 15:39:48 -0500319 if (ret < 0)
320 return ret;
321
322 if (size > ret)
323 size = ret;
324 dbgp_get_data(data, size);
325 return ret;
326}
327
Jason Wessel91777822009-08-20 15:39:53 -0500328static int dbgp_control_msg(unsigned devnum, int requesttype,
Jason Wesseldf6c5162009-08-20 15:39:48 -0500329 int request, int value, int index, void *data, int size)
330{
331 u32 pids, addr, ctrl;
332 struct usb_ctrlrequest req;
333 int read;
334 int ret;
335
336 read = (requesttype & USB_DIR_IN) != 0;
Jeffrin Jose4d4847b2012-05-20 00:56:58 +0530337 if (size > (read ? DBGP_MAX_PACKET : 0))
Jason Wesseldf6c5162009-08-20 15:39:48 -0500338 return -1;
339
340 /* Compute the control message */
341 req.bRequestType = requesttype;
342 req.bRequest = request;
343 req.wValue = cpu_to_le16(value);
344 req.wIndex = cpu_to_le16(index);
345 req.wLength = cpu_to_le16(size);
346
347 pids = DBGP_PID_SET(USB_PID_DATA0, USB_PID_SETUP);
348 addr = DBGP_EPADDR(devnum, 0);
349
350 ctrl = readl(&ehci_debug->control);
351 ctrl = dbgp_len_update(ctrl, sizeof(req));
352 ctrl |= DBGP_OUT;
353 ctrl |= DBGP_GO;
354
355 /* Send the setup message */
356 dbgp_set_data(&req, sizeof(req));
357 writel(addr, &ehci_debug->address);
358 writel(pids, &ehci_debug->pids);
Jason Wessel4fe1da42010-05-20 21:04:31 -0500359 ret = dbgp_wait_until_done(ctrl, DBGP_LOOPS);
Jason Wesseldf6c5162009-08-20 15:39:48 -0500360 if (ret < 0)
361 return ret;
362
363 /* Read the result */
Jason Wessel4fe1da42010-05-20 21:04:31 -0500364 return dbgp_bulk_read(devnum, 0, data, size, DBGP_LOOPS);
Jason Wesseldf6c5162009-08-20 15:39:48 -0500365}
366
Jason Wesseldf6c5162009-08-20 15:39:48 -0500367/* Find a PCI capability */
368static u32 __init find_cap(u32 num, u32 slot, u32 func, int cap)
369{
370 u8 pos;
371 int bytes;
372
373 if (!(read_pci_config_16(num, slot, func, PCI_STATUS) &
374 PCI_STATUS_CAP_LIST))
375 return 0;
376
377 pos = read_pci_config_byte(num, slot, func, PCI_CAPABILITY_LIST);
378 for (bytes = 0; bytes < 48 && pos >= 0x40; bytes++) {
379 u8 id;
380
381 pos &= ~3;
382 id = read_pci_config_byte(num, slot, func, pos+PCI_CAP_LIST_ID);
383 if (id == 0xff)
384 break;
385 if (id == cap)
386 return pos;
387
388 pos = read_pci_config_byte(num, slot, func,
389 pos+PCI_CAP_LIST_NEXT);
390 }
391 return 0;
392}
393
394static u32 __init __find_dbgp(u32 bus, u32 slot, u32 func)
395{
396 u32 class;
397
398 class = read_pci_config(bus, slot, func, PCI_CLASS_REVISION);
399 if ((class >> 8) != PCI_CLASS_SERIAL_USB_EHCI)
400 return 0;
401
402 return find_cap(bus, slot, func, PCI_CAP_ID_EHCI_DEBUG);
403}
404
405static u32 __init find_dbgp(int ehci_num, u32 *rbus, u32 *rslot, u32 *rfunc)
406{
407 u32 bus, slot, func;
408
409 for (bus = 0; bus < 256; bus++) {
410 for (slot = 0; slot < 32; slot++) {
411 for (func = 0; func < 8; func++) {
412 unsigned cap;
413
414 cap = __find_dbgp(bus, slot, func);
415
416 if (!cap)
417 continue;
418 if (ehci_num-- != 0)
419 continue;
420 *rbus = bus;
421 *rslot = slot;
422 *rfunc = func;
423 return cap;
424 }
425 }
426 }
427 return 0;
428}
429
Jason Wessel91777822009-08-20 15:39:53 -0500430static int dbgp_ehci_startup(void)
431{
432 u32 ctrl, cmd, status;
433 int loop;
434
435 /* Claim ownership, but do not enable yet */
436 ctrl = readl(&ehci_debug->control);
437 ctrl |= DBGP_OWNER;
438 ctrl &= ~(DBGP_ENABLED | DBGP_INUSE);
439 writel(ctrl, &ehci_debug->control);
440 udelay(1);
441
442 dbgp_ehci_status("EHCI startup");
443 /* Start the ehci running */
444 cmd = readl(&ehci_regs->command);
445 cmd &= ~(CMD_LRESET | CMD_IAAD | CMD_PSE | CMD_ASE | CMD_RESET);
446 cmd |= CMD_RUN;
447 writel(cmd, &ehci_regs->command);
448
449 /* Ensure everything is routed to the EHCI */
450 writel(FLAG_CF, &ehci_regs->configured_flag);
451
452 /* Wait until the controller is no longer halted */
Colin Ian Kingf96a4212012-07-30 16:06:42 +0100453 loop = 1000;
Jason Wessel91777822009-08-20 15:39:53 -0500454 do {
455 status = readl(&ehci_regs->status);
456 if (!(status & STS_HALT))
457 break;
458 udelay(1);
459 } while (--loop > 0);
460
461 if (!loop) {
462 dbgp_printk("ehci can not be started\n");
463 return -ENODEV;
464 }
465 dbgp_printk("ehci started\n");
466 return 0;
467}
468
469static int dbgp_ehci_controller_reset(void)
470{
471 int loop = 250 * 1000;
472 u32 cmd;
473
474 /* Reset the EHCI controller */
475 cmd = readl(&ehci_regs->command);
476 cmd |= CMD_RESET;
477 writel(cmd, &ehci_regs->command);
478 do {
479 cmd = readl(&ehci_regs->command);
480 } while ((cmd & CMD_RESET) && (--loop > 0));
481
482 if (!loop) {
483 dbgp_printk("can not reset ehci\n");
484 return -1;
485 }
486 dbgp_ehci_status("ehci reset done");
487 return 0;
488}
489static int ehci_wait_for_port(int port);
490/* Return 0 on success
491 * Return -ENODEV for any general failure
492 * Return -EIO if wait for port fails
493 */
Jan Beulich9fa57802012-09-18 12:23:02 +0100494static int _dbgp_external_startup(void)
Jason Wessel91777822009-08-20 15:39:53 -0500495{
496 int devnum;
497 struct usb_debug_descriptor dbgp_desc;
498 int ret;
Jason Wesselaab2d402009-08-20 15:39:55 -0500499 u32 ctrl, portsc, cmd;
Jason Wessel91777822009-08-20 15:39:53 -0500500 int dbg_port = dbgp_phys_port;
501 int tries = 3;
Jason Wesselaab2d402009-08-20 15:39:55 -0500502 int reset_port_tries = 1;
503 int try_hard_once = 1;
Jason Wessel91777822009-08-20 15:39:53 -0500504
Jason Wesselaab2d402009-08-20 15:39:55 -0500505try_port_reset_again:
Jason Wessel91777822009-08-20 15:39:53 -0500506 ret = dbgp_ehci_startup();
507 if (ret)
508 return ret;
509
510 /* Wait for a device to show up in the debug port */
511 ret = ehci_wait_for_port(dbg_port);
512 if (ret < 0) {
513 portsc = readl(&ehci_regs->port_status[dbg_port - 1]);
Jason Wesselaab2d402009-08-20 15:39:55 -0500514 if (!(portsc & PORT_CONNECT) && try_hard_once) {
515 /* Last ditch effort to try to force enable
516 * the debug device by using the packet test
517 * ehci command to try and wake it up. */
518 try_hard_once = 0;
519 cmd = readl(&ehci_regs->command);
520 cmd &= ~CMD_RUN;
521 writel(cmd, &ehci_regs->command);
522 portsc = readl(&ehci_regs->port_status[dbg_port - 1]);
523 portsc |= PORT_TEST_PKT;
524 writel(portsc, &ehci_regs->port_status[dbg_port - 1]);
525 dbgp_ehci_status("Trying to force debug port online");
526 mdelay(50);
527 dbgp_ehci_controller_reset();
528 goto try_port_reset_again;
529 } else if (reset_port_tries--) {
530 goto try_port_reset_again;
531 }
Jason Wessel91777822009-08-20 15:39:53 -0500532 dbgp_printk("No device found in debug port\n");
533 return -EIO;
534 }
535 dbgp_ehci_status("wait for port done");
536
537 /* Enable the debug port */
538 ctrl = readl(&ehci_debug->control);
539 ctrl |= DBGP_CLAIM;
540 writel(ctrl, &ehci_debug->control);
541 ctrl = readl(&ehci_debug->control);
542 if ((ctrl & DBGP_CLAIM) != DBGP_CLAIM) {
543 dbgp_printk("No device in debug port\n");
544 writel(ctrl & ~DBGP_CLAIM, &ehci_debug->control);
545 return -ENODEV;
546 }
547 dbgp_ehci_status("debug ported enabled");
548
549 /* Completely transfer the debug device to the debug controller */
550 portsc = readl(&ehci_regs->port_status[dbg_port - 1]);
551 portsc &= ~PORT_PE;
552 writel(portsc, &ehci_regs->port_status[dbg_port - 1]);
553
554 dbgp_mdelay(100);
555
556try_again:
557 /* Find the debug device and make it device number 127 */
558 for (devnum = 0; devnum <= 127; devnum++) {
559 ret = dbgp_control_msg(devnum,
560 USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
561 USB_REQ_GET_DESCRIPTOR, (USB_DT_DEBUG << 8), 0,
562 &dbgp_desc, sizeof(dbgp_desc));
563 if (ret > 0)
564 break;
565 }
566 if (devnum > 127) {
567 dbgp_printk("Could not find attached debug device\n");
568 goto err;
569 }
570 if (ret < 0) {
571 dbgp_printk("Attached device is not a debug device\n");
572 goto err;
573 }
574 dbgp_endpoint_out = dbgp_desc.bDebugOutEndpoint;
Jason Wessel4fe1da42010-05-20 21:04:31 -0500575 dbgp_endpoint_in = dbgp_desc.bDebugInEndpoint;
Jason Wessel91777822009-08-20 15:39:53 -0500576
577 /* Move the device to 127 if it isn't already there */
578 if (devnum != USB_DEBUG_DEVNUM) {
579 ret = dbgp_control_msg(devnum,
580 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
581 USB_REQ_SET_ADDRESS, USB_DEBUG_DEVNUM, 0, NULL, 0);
582 if (ret < 0) {
583 dbgp_printk("Could not move attached device to %d\n",
584 USB_DEBUG_DEVNUM);
585 goto err;
586 }
587 devnum = USB_DEBUG_DEVNUM;
588 dbgp_printk("debug device renamed to 127\n");
589 }
590
591 /* Enable the debug interface */
592 ret = dbgp_control_msg(USB_DEBUG_DEVNUM,
593 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
594 USB_REQ_SET_FEATURE, USB_DEVICE_DEBUG_MODE, 0, NULL, 0);
595 if (ret < 0) {
596 dbgp_printk(" Could not enable the debug device\n");
597 goto err;
598 }
599 dbgp_printk("debug interface enabled\n");
600 /* Perform a small write to get the even/odd data state in sync
601 */
602 ret = dbgp_bulk_write(USB_DEBUG_DEVNUM, dbgp_endpoint_out, " ", 1);
603 if (ret < 0) {
604 dbgp_printk("dbgp_bulk_write failed: %d\n", ret);
605 goto err;
606 }
Ferenc Wagneref58d972011-01-10 19:00:35 +0100607 dbgp_printk("small write done\n");
Jason Wessel91777822009-08-20 15:39:53 -0500608 dbgp_not_safe = 0;
609
610 return 0;
611err:
612 if (tries--)
613 goto try_again;
614 return -ENODEV;
615}
Jan Beulich9fa57802012-09-18 12:23:02 +0100616
617int dbgp_external_startup(struct usb_hcd *hcd)
618{
619 return xen_dbgp_external_startup(hcd) ?: _dbgp_external_startup();
620}
Jason Wessel91777822009-08-20 15:39:53 -0500621EXPORT_SYMBOL_GPL(dbgp_external_startup);
622
Jan Beulich40b52372009-11-20 13:50:53 +0000623static int ehci_reset_port(int port)
Jason Wesseldf6c5162009-08-20 15:39:48 -0500624{
625 u32 portsc;
626 u32 delay_time, delay;
627 int loop;
628
Jason Wessel91777822009-08-20 15:39:53 -0500629 dbgp_ehci_status("reset port");
Jason Wesseldf6c5162009-08-20 15:39:48 -0500630 /* Reset the usb debug port */
631 portsc = readl(&ehci_regs->port_status[port - 1]);
632 portsc &= ~PORT_PE;
633 portsc |= PORT_RESET;
634 writel(portsc, &ehci_regs->port_status[port - 1]);
635
636 delay = HUB_ROOT_RESET_TIME;
637 for (delay_time = 0; delay_time < HUB_RESET_TIMEOUT;
638 delay_time += delay) {
639 dbgp_mdelay(delay);
Jason Wesseldf6c5162009-08-20 15:39:48 -0500640 portsc = readl(&ehci_regs->port_status[port - 1]);
Jason Wessel56faf0f2009-08-20 15:39:51 -0500641 if (!(portsc & PORT_RESET))
642 break;
643 }
Jason Wesseldf6c5162009-08-20 15:39:48 -0500644 if (portsc & PORT_RESET) {
645 /* force reset to complete */
Jason Wessel56faf0f2009-08-20 15:39:51 -0500646 loop = 100 * 1000;
Jason Wesseldf6c5162009-08-20 15:39:48 -0500647 writel(portsc & ~(PORT_RWC_BITS | PORT_RESET),
648 &ehci_regs->port_status[port - 1]);
649 do {
Jason Wessel56faf0f2009-08-20 15:39:51 -0500650 udelay(1);
Jason Wesseldf6c5162009-08-20 15:39:48 -0500651 portsc = readl(&ehci_regs->port_status[port-1]);
652 } while ((portsc & PORT_RESET) && (--loop > 0));
653 }
654
655 /* Device went away? */
656 if (!(portsc & PORT_CONNECT))
657 return -ENOTCONN;
658
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300659 /* bomb out completely if something weird happened */
Jason Wesseldf6c5162009-08-20 15:39:48 -0500660 if ((portsc & PORT_CSC))
661 return -EINVAL;
662
663 /* If we've finished resetting, then break out of the loop */
664 if (!(portsc & PORT_RESET) && (portsc & PORT_PE))
665 return 0;
Jason Wesseldf6c5162009-08-20 15:39:48 -0500666 return -EBUSY;
667}
668
Jason Wessel91777822009-08-20 15:39:53 -0500669static int ehci_wait_for_port(int port)
Jason Wesseldf6c5162009-08-20 15:39:48 -0500670{
671 u32 status;
672 int ret, reps;
673
Jason Wessel56faf0f2009-08-20 15:39:51 -0500674 for (reps = 0; reps < 300; reps++) {
Jason Wesseldf6c5162009-08-20 15:39:48 -0500675 status = readl(&ehci_regs->status);
Jason Wessel56faf0f2009-08-20 15:39:51 -0500676 if (status & STS_PCD)
677 break;
678 dbgp_mdelay(1);
Jason Wesseldf6c5162009-08-20 15:39:48 -0500679 }
Jason Wessel56faf0f2009-08-20 15:39:51 -0500680 ret = ehci_reset_port(port);
681 if (ret == 0)
682 return 0;
Jason Wesseldf6c5162009-08-20 15:39:48 -0500683 return -ENOTCONN;
684}
685
Jason Wesseldf6c5162009-08-20 15:39:48 -0500686typedef void (*set_debug_port_t)(int port);
687
688static void __init default_set_debug_port(int port)
689{
690}
691
692static set_debug_port_t __initdata set_debug_port = default_set_debug_port;
693
694static void __init nvidia_set_debug_port(int port)
695{
696 u32 dword;
697 dword = read_pci_config(ehci_dev.bus, ehci_dev.slot, ehci_dev.func,
698 0x74);
699 dword &= ~(0x0f<<12);
700 dword |= ((port & 0x0f)<<12);
701 write_pci_config(ehci_dev.bus, ehci_dev.slot, ehci_dev.func, 0x74,
702 dword);
703 dbgp_printk("set debug port to %d\n", port);
704}
705
706static void __init detect_set_debug_port(void)
707{
708 u32 vendorid;
709
710 vendorid = read_pci_config(ehci_dev.bus, ehci_dev.slot, ehci_dev.func,
711 0x00);
712
713 if ((vendorid & 0xffff) == 0x10de) {
714 dbgp_printk("using nvidia set_debug_port\n");
715 set_debug_port = nvidia_set_debug_port;
716 }
717}
718
Jason Wessel093344e2009-08-20 15:39:50 -0500719/* The code in early_ehci_bios_handoff() is derived from the usb pci
720 * quirk initialization, but altered so as to use the early PCI
721 * routines. */
722#define EHCI_USBLEGSUP_BIOS (1 << 16) /* BIOS semaphore */
723#define EHCI_USBLEGCTLSTS 4 /* legacy control/status */
724static void __init early_ehci_bios_handoff(void)
725{
726 u32 hcc_params = readl(&ehci_caps->hcc_params);
727 int offset = (hcc_params >> 8) & 0xff;
728 u32 cap;
729 int msec;
730
731 if (!offset)
732 return;
733
734 cap = read_pci_config(ehci_dev.bus, ehci_dev.slot,
735 ehci_dev.func, offset);
736 dbgp_printk("dbgp: ehci BIOS state %08x\n", cap);
737
738 if ((cap & 0xff) == 1 && (cap & EHCI_USBLEGSUP_BIOS)) {
739 dbgp_printk("dbgp: BIOS handoff\n");
740 write_pci_config_byte(ehci_dev.bus, ehci_dev.slot,
741 ehci_dev.func, offset + 3, 1);
742 }
743
744 /* if boot firmware now owns EHCI, spin till it hands it over. */
745 msec = 1000;
746 while ((cap & EHCI_USBLEGSUP_BIOS) && (msec > 0)) {
747 mdelay(10);
748 msec -= 10;
749 cap = read_pci_config(ehci_dev.bus, ehci_dev.slot,
750 ehci_dev.func, offset);
751 }
752
753 if (cap & EHCI_USBLEGSUP_BIOS) {
754 /* well, possibly buggy BIOS... try to shut it down,
755 * and hope nothing goes too wrong */
756 dbgp_printk("dbgp: BIOS handoff failed: %08x\n", cap);
757 write_pci_config_byte(ehci_dev.bus, ehci_dev.slot,
758 ehci_dev.func, offset + 2, 0);
759 }
760
761 /* just in case, always disable EHCI SMIs */
762 write_pci_config_byte(ehci_dev.bus, ehci_dev.slot, ehci_dev.func,
763 offset + EHCI_USBLEGCTLSTS, 0);
764}
765
Jason Wesseldf6c5162009-08-20 15:39:48 -0500766static int __init ehci_setup(void)
767{
Jason Wessel91777822009-08-20 15:39:53 -0500768 u32 ctrl, portsc, hcs_params;
Jason Wesseldf6c5162009-08-20 15:39:48 -0500769 u32 debug_port, new_debug_port = 0, n_ports;
Jason Wesseldf6c5162009-08-20 15:39:48 -0500770 int ret, i;
Jason Wesseldf6c5162009-08-20 15:39:48 -0500771 int port_map_tried;
772 int playtimes = 3;
773
Jason Wessel093344e2009-08-20 15:39:50 -0500774 early_ehci_bios_handoff();
775
Jason Wesseldf6c5162009-08-20 15:39:48 -0500776try_next_time:
777 port_map_tried = 0;
778
779try_next_port:
780
781 hcs_params = readl(&ehci_caps->hcs_params);
782 debug_port = HCS_DEBUG_PORT(hcs_params);
Jason Wessel91777822009-08-20 15:39:53 -0500783 dbgp_phys_port = debug_port;
Jason Wesseldf6c5162009-08-20 15:39:48 -0500784 n_ports = HCS_N_PORTS(hcs_params);
785
786 dbgp_printk("debug_port: %d\n", debug_port);
787 dbgp_printk("n_ports: %d\n", n_ports);
Jason Wessel91777822009-08-20 15:39:53 -0500788 dbgp_ehci_status("");
Jason Wesseldf6c5162009-08-20 15:39:48 -0500789
790 for (i = 1; i <= n_ports; i++) {
791 portsc = readl(&ehci_regs->port_status[i-1]);
792 dbgp_printk("portstatus%d: %08x\n", i, portsc);
793 }
794
795 if (port_map_tried && (new_debug_port != debug_port)) {
796 if (--playtimes) {
797 set_debug_port(new_debug_port);
798 goto try_next_time;
799 }
800 return -1;
801 }
802
Jason Wessel91777822009-08-20 15:39:53 -0500803 /* Only reset the controller if it is not already in the
804 * configured state */
805 if (!(readl(&ehci_regs->configured_flag) & FLAG_CF)) {
806 if (dbgp_ehci_controller_reset() != 0)
807 return -1;
808 } else {
809 dbgp_ehci_status("ehci skip - already configured");
Jason Wesseldf6c5162009-08-20 15:39:48 -0500810 }
Jason Wesseldf6c5162009-08-20 15:39:48 -0500811
Jan Beulich9fa57802012-09-18 12:23:02 +0100812 ret = _dbgp_external_startup();
Jason Wessel91777822009-08-20 15:39:53 -0500813 if (ret == -EIO)
Jason Wesseldf6c5162009-08-20 15:39:48 -0500814 goto next_debug_port;
Jason Wesseldf6c5162009-08-20 15:39:48 -0500815
Jason Wesseldf6c5162009-08-20 15:39:48 -0500816 if (ret < 0) {
Jason Wessel91777822009-08-20 15:39:53 -0500817 /* Things didn't work so remove my claim */
818 ctrl = readl(&ehci_debug->control);
819 ctrl &= ~(DBGP_CLAIM | DBGP_OUT);
820 writel(ctrl, &ehci_debug->control);
821 return -1;
Jason Wesseldf6c5162009-08-20 15:39:48 -0500822 }
Jason Wesseldf6c5162009-08-20 15:39:48 -0500823 return 0;
Jason Wesseldf6c5162009-08-20 15:39:48 -0500824
825next_debug_port:
826 port_map_tried |= (1<<(debug_port - 1));
827 new_debug_port = ((debug_port-1+1)%n_ports) + 1;
828 if (port_map_tried != ((1<<n_ports) - 1)) {
829 set_debug_port(new_debug_port);
830 goto try_next_port;
831 }
832 if (--playtimes) {
833 set_debug_port(new_debug_port);
834 goto try_next_time;
835 }
836
837 return -1;
838}
839
840int __init early_dbgp_init(char *s)
841{
842 u32 debug_port, bar, offset;
843 u32 bus, slot, func, cap;
844 void __iomem *ehci_bar;
845 u32 dbgp_num;
846 u32 bar_val;
847 char *e;
848 int ret;
849 u8 byte;
850
851 if (!early_pci_allowed())
852 return -1;
853
854 dbgp_num = 0;
855 if (*s)
856 dbgp_num = simple_strtoul(s, &e, 10);
857 dbgp_printk("dbgp_num: %d\n", dbgp_num);
858
859 cap = find_dbgp(dbgp_num, &bus, &slot, &func);
860 if (!cap)
861 return -1;
862
863 dbgp_printk("Found EHCI debug port on %02x:%02x.%1x\n", bus, slot,
864 func);
865
866 debug_port = read_pci_config(bus, slot, func, cap);
867 bar = (debug_port >> 29) & 0x7;
868 bar = (bar * 4) + 0xc;
869 offset = (debug_port >> 16) & 0xfff;
870 dbgp_printk("bar: %02x offset: %03x\n", bar, offset);
871 if (bar != PCI_BASE_ADDRESS_0) {
872 dbgp_printk("only debug ports on bar 1 handled.\n");
873
874 return -1;
875 }
876
877 bar_val = read_pci_config(bus, slot, func, PCI_BASE_ADDRESS_0);
878 dbgp_printk("bar_val: %02x offset: %03x\n", bar_val, offset);
879 if (bar_val & ~PCI_BASE_ADDRESS_MEM_MASK) {
880 dbgp_printk("only simple 32bit mmio bars supported\n");
881
882 return -1;
883 }
884
885 /* double check if the mem space is enabled */
886 byte = read_pci_config_byte(bus, slot, func, 0x04);
887 if (!(byte & 0x2)) {
888 byte |= 0x02;
889 write_pci_config_byte(bus, slot, func, 0x04, byte);
890 dbgp_printk("mmio for ehci enabled\n");
891 }
892
893 /*
894 * FIXME I don't have the bar size so just guess PAGE_SIZE is more
895 * than enough. 1K is the biggest I have seen.
896 */
897 set_fixmap_nocache(FIX_DBGP_BASE, bar_val & PAGE_MASK);
898 ehci_bar = (void __iomem *)__fix_to_virt(FIX_DBGP_BASE);
899 ehci_bar += bar_val & ~PAGE_MASK;
900 dbgp_printk("ehci_bar: %p\n", ehci_bar);
901
902 ehci_caps = ehci_bar;
Jan Anderssonc4301312011-05-03 20:11:57 +0200903 ehci_regs = ehci_bar + EARLY_HC_LENGTH(readl(&ehci_caps->hc_capbase));
Jason Wesseldf6c5162009-08-20 15:39:48 -0500904 ehci_debug = ehci_bar + offset;
905 ehci_dev.bus = bus;
906 ehci_dev.slot = slot;
907 ehci_dev.func = func;
908
909 detect_set_debug_port();
910
911 ret = ehci_setup();
912 if (ret < 0) {
913 dbgp_printk("ehci_setup failed\n");
914 ehci_debug = NULL;
915
916 return -1;
917 }
Jason Wessel91777822009-08-20 15:39:53 -0500918 dbgp_ehci_status("early_init_complete");
Jason Wesseldf6c5162009-08-20 15:39:48 -0500919
920 return 0;
921}
922
923static void early_dbgp_write(struct console *con, const char *str, u32 n)
924{
925 int chunk, ret;
Jason Wessel87a5d152009-08-20 15:39:49 -0500926 char buf[DBGP_MAX_PACKET];
927 int use_cr = 0;
Jason Wessel91777822009-08-20 15:39:53 -0500928 u32 cmd, ctrl;
929 int reset_run = 0;
Jason Wesseldf6c5162009-08-20 15:39:48 -0500930
Jason Wessel91777822009-08-20 15:39:53 -0500931 if (!ehci_debug || dbgp_not_safe)
Jason Wesseldf6c5162009-08-20 15:39:48 -0500932 return;
Jason Wessel91777822009-08-20 15:39:53 -0500933
934 cmd = readl(&ehci_regs->command);
935 if (unlikely(!(cmd & CMD_RUN))) {
936 /* If the ehci controller is not in the run state do extended
937 * checks to see if the acpi or some other initialization also
938 * reset the ehci debug port */
939 ctrl = readl(&ehci_debug->control);
940 if (!(ctrl & DBGP_ENABLED)) {
941 dbgp_not_safe = 1;
Jan Beulich9fa57802012-09-18 12:23:02 +0100942 _dbgp_external_startup();
Jason Wessel91777822009-08-20 15:39:53 -0500943 } else {
944 cmd |= CMD_RUN;
945 writel(cmd, &ehci_regs->command);
946 reset_run = 1;
947 }
948 }
Jason Wesseldf6c5162009-08-20 15:39:48 -0500949 while (n > 0) {
Jason Wessel87a5d152009-08-20 15:39:49 -0500950 for (chunk = 0; chunk < DBGP_MAX_PACKET && n > 0;
951 str++, chunk++, n--) {
952 if (!use_cr && *str == '\n') {
953 use_cr = 1;
954 buf[chunk] = '\r';
955 str--;
956 n++;
957 continue;
958 }
959 if (use_cr)
960 use_cr = 0;
961 buf[chunk] = *str;
962 }
Jason Wessel91777822009-08-20 15:39:53 -0500963 if (chunk > 0) {
964 ret = dbgp_bulk_write(USB_DEBUG_DEVNUM,
965 dbgp_endpoint_out, buf, chunk);
966 }
967 }
968 if (unlikely(reset_run)) {
969 cmd = readl(&ehci_regs->command);
970 cmd &= ~CMD_RUN;
971 writel(cmd, &ehci_regs->command);
Jason Wesseldf6c5162009-08-20 15:39:48 -0500972 }
973}
974
975struct console early_dbgp_console = {
976 .name = "earlydbg",
977 .write = early_dbgp_write,
978 .flags = CON_PRINTBUFFER,
979 .index = -1,
980};
Jason Wessel8d053c72009-08-20 15:39:54 -0500981
Jan Beulich9fa57802012-09-18 12:23:02 +0100982int dbgp_reset_prep(struct usb_hcd *hcd)
Jason Wessel8d053c72009-08-20 15:39:54 -0500983{
Jan Beulich9fa57802012-09-18 12:23:02 +0100984 int ret = xen_dbgp_reset_prep(hcd);
Jason Wessel8d053c72009-08-20 15:39:54 -0500985 u32 ctrl;
986
Jan Beulich9fa57802012-09-18 12:23:02 +0100987 if (ret)
988 return ret;
989
Jason Wessel8d053c72009-08-20 15:39:54 -0500990 dbgp_not_safe = 1;
991 if (!ehci_debug)
992 return 0;
993
Jason Wessel4fe1da42010-05-20 21:04:31 -0500994 if ((early_dbgp_console.index != -1 &&
995 !(early_dbgp_console.flags & CON_BOOT)) ||
996 dbgp_kgdb_mode)
Jason Wessel8d053c72009-08-20 15:39:54 -0500997 return 1;
998 /* This means the console is not initialized, or should get
999 * shutdown so as to allow for reuse of the usb device, which
1000 * means it is time to shutdown the usb debug port. */
1001 ctrl = readl(&ehci_debug->control);
1002 if (ctrl & DBGP_ENABLED) {
1003 ctrl &= ~(DBGP_CLAIM);
1004 writel(ctrl, &ehci_debug->control);
1005 }
1006 return 0;
1007}
1008EXPORT_SYMBOL_GPL(dbgp_reset_prep);
Jason Wessel4fe1da42010-05-20 21:04:31 -05001009
1010#ifdef CONFIG_KGDB
1011
1012static char kgdbdbgp_buf[DBGP_MAX_PACKET];
1013static int kgdbdbgp_buf_sz;
1014static int kgdbdbgp_buf_idx;
1015static int kgdbdbgp_loop_cnt = DBGP_LOOPS;
1016
1017static int kgdbdbgp_read_char(void)
1018{
1019 int ret;
1020
1021 if (kgdbdbgp_buf_idx < kgdbdbgp_buf_sz) {
1022 char ch = kgdbdbgp_buf[kgdbdbgp_buf_idx++];
1023 return ch;
1024 }
1025
1026 ret = dbgp_bulk_read(USB_DEBUG_DEVNUM, dbgp_endpoint_in,
1027 &kgdbdbgp_buf, DBGP_MAX_PACKET,
1028 kgdbdbgp_loop_cnt);
1029 if (ret <= 0)
1030 return NO_POLL_CHAR;
1031 kgdbdbgp_buf_sz = ret;
1032 kgdbdbgp_buf_idx = 1;
1033 return kgdbdbgp_buf[0];
1034}
1035
1036static void kgdbdbgp_write_char(u8 chr)
1037{
1038 early_dbgp_write(NULL, &chr, 1);
1039}
1040
1041static struct kgdb_io kgdbdbgp_io_ops = {
1042 .name = "kgdbdbgp",
1043 .read_char = kgdbdbgp_read_char,
1044 .write_char = kgdbdbgp_write_char,
1045};
1046
1047static int kgdbdbgp_wait_time;
1048
1049static int __init kgdbdbgp_parse_config(char *str)
1050{
1051 char *ptr;
1052
1053 if (!ehci_debug) {
1054 if (early_dbgp_init(str))
1055 return -1;
1056 }
1057 ptr = strchr(str, ',');
1058 if (ptr) {
1059 ptr++;
1060 kgdbdbgp_wait_time = simple_strtoul(ptr, &ptr, 10);
1061 }
1062 kgdb_register_io_module(&kgdbdbgp_io_ops);
1063 kgdbdbgp_io_ops.is_console = early_dbgp_console.index != -1;
1064
1065 return 0;
1066}
1067early_param("kgdbdbgp", kgdbdbgp_parse_config);
1068
1069static int kgdbdbgp_reader_thread(void *ptr)
1070{
1071 int ret;
1072
1073 while (readl(&ehci_debug->control) & DBGP_ENABLED) {
1074 kgdbdbgp_loop_cnt = 1;
1075 ret = kgdbdbgp_read_char();
1076 kgdbdbgp_loop_cnt = DBGP_LOOPS;
1077 if (ret != NO_POLL_CHAR) {
1078 if (ret == 0x3 || ret == '$') {
1079 if (ret == '$')
1080 kgdbdbgp_buf_idx--;
1081 kgdb_breakpoint();
1082 }
1083 continue;
1084 }
1085 schedule_timeout_interruptible(kgdbdbgp_wait_time * HZ);
1086 }
1087 return 0;
1088}
1089
1090static int __init kgdbdbgp_start_thread(void)
1091{
1092 if (dbgp_kgdb_mode && kgdbdbgp_wait_time)
1093 kthread_run(kgdbdbgp_reader_thread, NULL, "%s", "dbgp");
1094
1095 return 0;
1096}
1097module_init(kgdbdbgp_start_thread);
1098#endif /* CONFIG_KGDB */