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