blob: 6198ebded3a4c70e3f95998ceedcfee77e5ad12a [file] [log] [blame]
Jason Wesseldf6c5162009-08-20 15:39:48 -05001#include <linux/console.h>
2#include <linux/errno.h>
3#include <linux/pci_regs.h>
4#include <linux/pci_ids.h>
5#include <linux/usb/ch9.h>
6#include <linux/usb/ehci_def.h>
7#include <linux/delay.h>
8#include <asm/io.h>
9#include <asm/pci-direct.h>
10#include <asm/fixmap.h>
11
Jason Wessel56faf0f2009-08-20 15:39:51 -050012#ifdef DBGP_DEBUG
13# define dbgp_printk printk
14#else
15static inline void dbgp_printk(const char *fmt, ...) { }
16#endif
17
Jason Wesseldf6c5162009-08-20 15:39:48 -050018static struct ehci_caps __iomem *ehci_caps;
19static struct ehci_regs __iomem *ehci_regs;
20static struct ehci_dbg_port __iomem *ehci_debug;
21static unsigned int dbgp_endpoint_out;
22
23struct ehci_dev {
24 u32 bus;
25 u32 slot;
26 u32 func;
27};
28
29static struct ehci_dev ehci_dev;
30
31#define USB_DEBUG_DEVNUM 127
32
33#define DBGP_DATA_TOGGLE 0x8800
34
35static inline u32 dbgp_pid_update(u32 x, u32 tok)
36{
37 return ((x ^ DBGP_DATA_TOGGLE) & 0xffff00) | (tok & 0xff);
38}
39
40static inline u32 dbgp_len_update(u32 x, u32 len)
41{
42 return (x & ~0x0f) | (len & 0x0f);
43}
44
45/*
46 * USB Packet IDs (PIDs)
47 */
48
49/* token */
50#define USB_PID_OUT 0xe1
51#define USB_PID_IN 0x69
52#define USB_PID_SOF 0xa5
53#define USB_PID_SETUP 0x2d
54/* handshake */
55#define USB_PID_ACK 0xd2
56#define USB_PID_NAK 0x5a
57#define USB_PID_STALL 0x1e
58#define USB_PID_NYET 0x96
59/* data */
60#define USB_PID_DATA0 0xc3
61#define USB_PID_DATA1 0x4b
62#define USB_PID_DATA2 0x87
63#define USB_PID_MDATA 0x0f
64/* Special */
65#define USB_PID_PREAMBLE 0x3c
66#define USB_PID_ERR 0x3c
67#define USB_PID_SPLIT 0x78
68#define USB_PID_PING 0xb4
69#define USB_PID_UNDEF_0 0xf0
70
71#define USB_PID_DATA_TOGGLE 0x88
72#define DBGP_CLAIM (DBGP_OWNER | DBGP_ENABLED | DBGP_INUSE)
73
74#define PCI_CAP_ID_EHCI_DEBUG 0xa
75
76#define HUB_ROOT_RESET_TIME 50 /* times are in msec */
77#define HUB_SHORT_RESET_TIME 10
78#define HUB_LONG_RESET_TIME 200
79#define HUB_RESET_TIMEOUT 500
80
81#define DBGP_MAX_PACKET 8
82
83static int dbgp_wait_until_complete(void)
84{
85 u32 ctrl;
86 int loop = 0x100000;
87
88 do {
89 ctrl = readl(&ehci_debug->control);
90 /* Stop when the transaction is finished */
91 if (ctrl & DBGP_DONE)
92 break;
93 } while (--loop > 0);
94
95 if (!loop)
96 return -1;
97
98 /*
99 * Now that we have observed the completed transaction,
100 * clear the done bit.
101 */
102 writel(ctrl | DBGP_DONE, &ehci_debug->control);
103 return (ctrl & DBGP_ERROR) ? -DBGP_ERRCODE(ctrl) : DBGP_LEN(ctrl);
104}
105
106static void __init dbgp_mdelay(int ms)
107{
108 int i;
109
110 while (ms--) {
111 for (i = 0; i < 1000; i++)
112 outb(0x1, 0x80);
113 }
114}
115
116static void dbgp_breath(void)
117{
118 /* Sleep to give the debug port a chance to breathe */
119}
120
121static int dbgp_wait_until_done(unsigned ctrl)
122{
123 u32 pids, lpid;
124 int ret;
125 int loop = 3;
126
127retry:
128 writel(ctrl | DBGP_GO, &ehci_debug->control);
129 ret = dbgp_wait_until_complete();
130 pids = readl(&ehci_debug->pids);
131 lpid = DBGP_PID_GET(pids);
132
133 if (ret < 0)
134 return ret;
135
136 /*
137 * If the port is getting full or it has dropped data
138 * start pacing ourselves, not necessary but it's friendly.
139 */
140 if ((lpid == USB_PID_NAK) || (lpid == USB_PID_NYET))
141 dbgp_breath();
142
143 /* If I get a NACK reissue the transmission */
144 if (lpid == USB_PID_NAK) {
145 if (--loop > 0)
146 goto retry;
147 }
148
149 return ret;
150}
151
152static void dbgp_set_data(const void *buf, int size)
153{
154 const unsigned char *bytes = buf;
155 u32 lo, hi;
156 int i;
157
158 lo = hi = 0;
159 for (i = 0; i < 4 && i < size; i++)
160 lo |= bytes[i] << (8*i);
161 for (; i < 8 && i < size; i++)
162 hi |= bytes[i] << (8*(i - 4));
163 writel(lo, &ehci_debug->data03);
164 writel(hi, &ehci_debug->data47);
165}
166
167static void __init dbgp_get_data(void *buf, int size)
168{
169 unsigned char *bytes = buf;
170 u32 lo, hi;
171 int i;
172
173 lo = readl(&ehci_debug->data03);
174 hi = readl(&ehci_debug->data47);
175 for (i = 0; i < 4 && i < size; i++)
176 bytes[i] = (lo >> (8*i)) & 0xff;
177 for (; i < 8 && i < size; i++)
178 bytes[i] = (hi >> (8*(i - 4))) & 0xff;
179}
180
181static int dbgp_bulk_write(unsigned devnum, unsigned endpoint,
182 const char *bytes, int size)
183{
184 u32 pids, addr, ctrl;
185 int ret;
186
187 if (size > DBGP_MAX_PACKET)
188 return -1;
189
190 addr = DBGP_EPADDR(devnum, endpoint);
191
192 pids = readl(&ehci_debug->pids);
193 pids = dbgp_pid_update(pids, USB_PID_OUT);
194
195 ctrl = readl(&ehci_debug->control);
196 ctrl = dbgp_len_update(ctrl, size);
197 ctrl |= DBGP_OUT;
198 ctrl |= DBGP_GO;
199
200 dbgp_set_data(bytes, size);
201 writel(addr, &ehci_debug->address);
202 writel(pids, &ehci_debug->pids);
203
204 ret = dbgp_wait_until_done(ctrl);
205 if (ret < 0)
206 return ret;
207
208 return ret;
209}
210
211static int __init dbgp_bulk_read(unsigned devnum, unsigned endpoint, void *data,
212 int size)
213{
214 u32 pids, addr, ctrl;
215 int ret;
216
217 if (size > DBGP_MAX_PACKET)
218 return -1;
219
220 addr = DBGP_EPADDR(devnum, endpoint);
221
222 pids = readl(&ehci_debug->pids);
223 pids = dbgp_pid_update(pids, USB_PID_IN);
224
225 ctrl = readl(&ehci_debug->control);
226 ctrl = dbgp_len_update(ctrl, size);
227 ctrl &= ~DBGP_OUT;
228 ctrl |= DBGP_GO;
229
230 writel(addr, &ehci_debug->address);
231 writel(pids, &ehci_debug->pids);
232 ret = dbgp_wait_until_done(ctrl);
233 if (ret < 0)
234 return ret;
235
236 if (size > ret)
237 size = ret;
238 dbgp_get_data(data, size);
239 return ret;
240}
241
242static int __init dbgp_control_msg(unsigned devnum, int requesttype,
243 int request, int value, int index, void *data, int size)
244{
245 u32 pids, addr, ctrl;
246 struct usb_ctrlrequest req;
247 int read;
248 int ret;
249
250 read = (requesttype & USB_DIR_IN) != 0;
251 if (size > (read ? DBGP_MAX_PACKET:0))
252 return -1;
253
254 /* Compute the control message */
255 req.bRequestType = requesttype;
256 req.bRequest = request;
257 req.wValue = cpu_to_le16(value);
258 req.wIndex = cpu_to_le16(index);
259 req.wLength = cpu_to_le16(size);
260
261 pids = DBGP_PID_SET(USB_PID_DATA0, USB_PID_SETUP);
262 addr = DBGP_EPADDR(devnum, 0);
263
264 ctrl = readl(&ehci_debug->control);
265 ctrl = dbgp_len_update(ctrl, sizeof(req));
266 ctrl |= DBGP_OUT;
267 ctrl |= DBGP_GO;
268
269 /* Send the setup message */
270 dbgp_set_data(&req, sizeof(req));
271 writel(addr, &ehci_debug->address);
272 writel(pids, &ehci_debug->pids);
273 ret = dbgp_wait_until_done(ctrl);
274 if (ret < 0)
275 return ret;
276
277 /* Read the result */
278 return dbgp_bulk_read(devnum, 0, data, size);
279}
280
281
282/* Find a PCI capability */
283static u32 __init find_cap(u32 num, u32 slot, u32 func, int cap)
284{
285 u8 pos;
286 int bytes;
287
288 if (!(read_pci_config_16(num, slot, func, PCI_STATUS) &
289 PCI_STATUS_CAP_LIST))
290 return 0;
291
292 pos = read_pci_config_byte(num, slot, func, PCI_CAPABILITY_LIST);
293 for (bytes = 0; bytes < 48 && pos >= 0x40; bytes++) {
294 u8 id;
295
296 pos &= ~3;
297 id = read_pci_config_byte(num, slot, func, pos+PCI_CAP_LIST_ID);
298 if (id == 0xff)
299 break;
300 if (id == cap)
301 return pos;
302
303 pos = read_pci_config_byte(num, slot, func,
304 pos+PCI_CAP_LIST_NEXT);
305 }
306 return 0;
307}
308
309static u32 __init __find_dbgp(u32 bus, u32 slot, u32 func)
310{
311 u32 class;
312
313 class = read_pci_config(bus, slot, func, PCI_CLASS_REVISION);
314 if ((class >> 8) != PCI_CLASS_SERIAL_USB_EHCI)
315 return 0;
316
317 return find_cap(bus, slot, func, PCI_CAP_ID_EHCI_DEBUG);
318}
319
320static u32 __init find_dbgp(int ehci_num, u32 *rbus, u32 *rslot, u32 *rfunc)
321{
322 u32 bus, slot, func;
323
324 for (bus = 0; bus < 256; bus++) {
325 for (slot = 0; slot < 32; slot++) {
326 for (func = 0; func < 8; func++) {
327 unsigned cap;
328
329 cap = __find_dbgp(bus, slot, func);
330
331 if (!cap)
332 continue;
333 if (ehci_num-- != 0)
334 continue;
335 *rbus = bus;
336 *rslot = slot;
337 *rfunc = func;
338 return cap;
339 }
340 }
341 }
342 return 0;
343}
344
345static int __init ehci_reset_port(int port)
346{
347 u32 portsc;
348 u32 delay_time, delay;
349 int loop;
350
Jason Wessel56faf0f2009-08-20 15:39:51 -0500351 dbgp_printk("ehci_reset_port %i\n", port);
Jason Wesseldf6c5162009-08-20 15:39:48 -0500352 /* Reset the usb debug port */
353 portsc = readl(&ehci_regs->port_status[port - 1]);
354 portsc &= ~PORT_PE;
355 portsc |= PORT_RESET;
356 writel(portsc, &ehci_regs->port_status[port - 1]);
357
358 delay = HUB_ROOT_RESET_TIME;
359 for (delay_time = 0; delay_time < HUB_RESET_TIMEOUT;
360 delay_time += delay) {
361 dbgp_mdelay(delay);
Jason Wesseldf6c5162009-08-20 15:39:48 -0500362 portsc = readl(&ehci_regs->port_status[port - 1]);
Jason Wessel56faf0f2009-08-20 15:39:51 -0500363 if (!(portsc & PORT_RESET))
364 break;
365 }
Jason Wesseldf6c5162009-08-20 15:39:48 -0500366 if (portsc & PORT_RESET) {
367 /* force reset to complete */
Jason Wessel56faf0f2009-08-20 15:39:51 -0500368 loop = 100 * 1000;
Jason Wesseldf6c5162009-08-20 15:39:48 -0500369 writel(portsc & ~(PORT_RWC_BITS | PORT_RESET),
370 &ehci_regs->port_status[port - 1]);
371 do {
Jason Wessel56faf0f2009-08-20 15:39:51 -0500372 udelay(1);
Jason Wesseldf6c5162009-08-20 15:39:48 -0500373 portsc = readl(&ehci_regs->port_status[port-1]);
374 } while ((portsc & PORT_RESET) && (--loop > 0));
375 }
376
377 /* Device went away? */
378 if (!(portsc & PORT_CONNECT))
379 return -ENOTCONN;
380
381 /* bomb out completely if something weird happend */
382 if ((portsc & PORT_CSC))
383 return -EINVAL;
384
385 /* If we've finished resetting, then break out of the loop */
386 if (!(portsc & PORT_RESET) && (portsc & PORT_PE))
387 return 0;
Jason Wesseldf6c5162009-08-20 15:39:48 -0500388 return -EBUSY;
389}
390
391static int __init ehci_wait_for_port(int port)
392{
393 u32 status;
394 int ret, reps;
395
Jason Wessel56faf0f2009-08-20 15:39:51 -0500396 for (reps = 0; reps < 300; reps++) {
Jason Wesseldf6c5162009-08-20 15:39:48 -0500397 status = readl(&ehci_regs->status);
Jason Wessel56faf0f2009-08-20 15:39:51 -0500398 if (status & STS_PCD)
399 break;
400 dbgp_mdelay(1);
Jason Wesseldf6c5162009-08-20 15:39:48 -0500401 }
Jason Wessel56faf0f2009-08-20 15:39:51 -0500402 ret = ehci_reset_port(port);
403 if (ret == 0)
404 return 0;
Jason Wesseldf6c5162009-08-20 15:39:48 -0500405 return -ENOTCONN;
406}
407
Jason Wesseldf6c5162009-08-20 15:39:48 -0500408typedef void (*set_debug_port_t)(int port);
409
410static void __init default_set_debug_port(int port)
411{
412}
413
414static set_debug_port_t __initdata set_debug_port = default_set_debug_port;
415
416static void __init nvidia_set_debug_port(int port)
417{
418 u32 dword;
419 dword = read_pci_config(ehci_dev.bus, ehci_dev.slot, ehci_dev.func,
420 0x74);
421 dword &= ~(0x0f<<12);
422 dword |= ((port & 0x0f)<<12);
423 write_pci_config(ehci_dev.bus, ehci_dev.slot, ehci_dev.func, 0x74,
424 dword);
425 dbgp_printk("set debug port to %d\n", port);
426}
427
428static void __init detect_set_debug_port(void)
429{
430 u32 vendorid;
431
432 vendorid = read_pci_config(ehci_dev.bus, ehci_dev.slot, ehci_dev.func,
433 0x00);
434
435 if ((vendorid & 0xffff) == 0x10de) {
436 dbgp_printk("using nvidia set_debug_port\n");
437 set_debug_port = nvidia_set_debug_port;
438 }
439}
440
Jason Wessel093344e2009-08-20 15:39:50 -0500441/* The code in early_ehci_bios_handoff() is derived from the usb pci
442 * quirk initialization, but altered so as to use the early PCI
443 * routines. */
444#define EHCI_USBLEGSUP_BIOS (1 << 16) /* BIOS semaphore */
445#define EHCI_USBLEGCTLSTS 4 /* legacy control/status */
446static void __init early_ehci_bios_handoff(void)
447{
448 u32 hcc_params = readl(&ehci_caps->hcc_params);
449 int offset = (hcc_params >> 8) & 0xff;
450 u32 cap;
451 int msec;
452
453 if (!offset)
454 return;
455
456 cap = read_pci_config(ehci_dev.bus, ehci_dev.slot,
457 ehci_dev.func, offset);
458 dbgp_printk("dbgp: ehci BIOS state %08x\n", cap);
459
460 if ((cap & 0xff) == 1 && (cap & EHCI_USBLEGSUP_BIOS)) {
461 dbgp_printk("dbgp: BIOS handoff\n");
462 write_pci_config_byte(ehci_dev.bus, ehci_dev.slot,
463 ehci_dev.func, offset + 3, 1);
464 }
465
466 /* if boot firmware now owns EHCI, spin till it hands it over. */
467 msec = 1000;
468 while ((cap & EHCI_USBLEGSUP_BIOS) && (msec > 0)) {
469 mdelay(10);
470 msec -= 10;
471 cap = read_pci_config(ehci_dev.bus, ehci_dev.slot,
472 ehci_dev.func, offset);
473 }
474
475 if (cap & EHCI_USBLEGSUP_BIOS) {
476 /* well, possibly buggy BIOS... try to shut it down,
477 * and hope nothing goes too wrong */
478 dbgp_printk("dbgp: BIOS handoff failed: %08x\n", cap);
479 write_pci_config_byte(ehci_dev.bus, ehci_dev.slot,
480 ehci_dev.func, offset + 2, 0);
481 }
482
483 /* just in case, always disable EHCI SMIs */
484 write_pci_config_byte(ehci_dev.bus, ehci_dev.slot, ehci_dev.func,
485 offset + EHCI_USBLEGCTLSTS, 0);
486}
487
Jason Wesseldf6c5162009-08-20 15:39:48 -0500488static int __init ehci_setup(void)
489{
490 struct usb_debug_descriptor dbgp_desc;
491 u32 cmd, ctrl, status, portsc, hcs_params;
492 u32 debug_port, new_debug_port = 0, n_ports;
493 u32 devnum;
494 int ret, i;
495 int loop;
496 int port_map_tried;
497 int playtimes = 3;
498
Jason Wessel093344e2009-08-20 15:39:50 -0500499 early_ehci_bios_handoff();
500
Jason Wesseldf6c5162009-08-20 15:39:48 -0500501try_next_time:
502 port_map_tried = 0;
503
504try_next_port:
505
506 hcs_params = readl(&ehci_caps->hcs_params);
507 debug_port = HCS_DEBUG_PORT(hcs_params);
508 n_ports = HCS_N_PORTS(hcs_params);
509
510 dbgp_printk("debug_port: %d\n", debug_port);
511 dbgp_printk("n_ports: %d\n", n_ports);
512
513 for (i = 1; i <= n_ports; i++) {
514 portsc = readl(&ehci_regs->port_status[i-1]);
515 dbgp_printk("portstatus%d: %08x\n", i, portsc);
516 }
517
518 if (port_map_tried && (new_debug_port != debug_port)) {
519 if (--playtimes) {
520 set_debug_port(new_debug_port);
521 goto try_next_time;
522 }
523 return -1;
524 }
525
Jason Wessel56faf0f2009-08-20 15:39:51 -0500526 loop = 250 * 1000;
Jason Wesseldf6c5162009-08-20 15:39:48 -0500527 /* Reset the EHCI controller */
528 cmd = readl(&ehci_regs->command);
529 cmd |= CMD_RESET;
530 writel(cmd, &ehci_regs->command);
531 do {
532 cmd = readl(&ehci_regs->command);
533 } while ((cmd & CMD_RESET) && (--loop > 0));
534
535 if (!loop) {
536 dbgp_printk("can not reset ehci\n");
537 return -1;
538 }
539 dbgp_printk("ehci reset done\n");
540
541 /* Claim ownership, but do not enable yet */
542 ctrl = readl(&ehci_debug->control);
543 ctrl |= DBGP_OWNER;
544 ctrl &= ~(DBGP_ENABLED | DBGP_INUSE);
545 writel(ctrl, &ehci_debug->control);
Jason Wessel56faf0f2009-08-20 15:39:51 -0500546 udelay(1);
Jason Wesseldf6c5162009-08-20 15:39:48 -0500547
548 /* Start the ehci running */
549 cmd = readl(&ehci_regs->command);
550 cmd &= ~(CMD_LRESET | CMD_IAAD | CMD_PSE | CMD_ASE | CMD_RESET);
551 cmd |= CMD_RUN;
552 writel(cmd, &ehci_regs->command);
553
554 /* Ensure everything is routed to the EHCI */
555 writel(FLAG_CF, &ehci_regs->configured_flag);
556
557 /* Wait until the controller is no longer halted */
558 loop = 10;
559 do {
560 status = readl(&ehci_regs->status);
Jason Wessel56faf0f2009-08-20 15:39:51 -0500561 if (!(status & STS_HALT))
562 break;
563 udelay(1);
564 } while (--loop > 0);
Jason Wesseldf6c5162009-08-20 15:39:48 -0500565
566 if (!loop) {
Jason Wessel56faf0f2009-08-20 15:39:51 -0500567 dbgp_printk("ehci can not be started\n");
Jason Wesseldf6c5162009-08-20 15:39:48 -0500568 return -1;
569 }
570 dbgp_printk("ehci started\n");
571
572 /* Wait for a device to show up in the debug port */
573 ret = ehci_wait_for_port(debug_port);
574 if (ret < 0) {
575 dbgp_printk("No device found in debug port\n");
576 goto next_debug_port;
577 }
578 dbgp_printk("ehci wait for port done\n");
579
580 /* Enable the debug port */
581 ctrl = readl(&ehci_debug->control);
582 ctrl |= DBGP_CLAIM;
583 writel(ctrl, &ehci_debug->control);
584 ctrl = readl(&ehci_debug->control);
585 if ((ctrl & DBGP_CLAIM) != DBGP_CLAIM) {
586 dbgp_printk("No device in debug port\n");
587 writel(ctrl & ~DBGP_CLAIM, &ehci_debug->control);
588 goto err;
589 }
590 dbgp_printk("debug ported enabled\n");
591
592 /* Completely transfer the debug device to the debug controller */
593 portsc = readl(&ehci_regs->port_status[debug_port - 1]);
594 portsc &= ~PORT_PE;
595 writel(portsc, &ehci_regs->port_status[debug_port - 1]);
596
597 dbgp_mdelay(100);
598
599 /* Find the debug device and make it device number 127 */
600 for (devnum = 0; devnum <= 127; devnum++) {
601 ret = dbgp_control_msg(devnum,
602 USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
603 USB_REQ_GET_DESCRIPTOR, (USB_DT_DEBUG << 8), 0,
604 &dbgp_desc, sizeof(dbgp_desc));
605 if (ret > 0)
606 break;
607 }
608 if (devnum > 127) {
609 dbgp_printk("Could not find attached debug device\n");
610 goto err;
611 }
612 if (ret < 0) {
613 dbgp_printk("Attached device is not a debug device\n");
614 goto err;
615 }
616 dbgp_endpoint_out = dbgp_desc.bDebugOutEndpoint;
617
618 /* Move the device to 127 if it isn't already there */
619 if (devnum != USB_DEBUG_DEVNUM) {
620 ret = dbgp_control_msg(devnum,
621 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
622 USB_REQ_SET_ADDRESS, USB_DEBUG_DEVNUM, 0, NULL, 0);
623 if (ret < 0) {
624 dbgp_printk("Could not move attached device to %d\n",
625 USB_DEBUG_DEVNUM);
626 goto err;
627 }
628 devnum = USB_DEBUG_DEVNUM;
629 dbgp_printk("debug device renamed to 127\n");
630 }
631
632 /* Enable the debug interface */
633 ret = dbgp_control_msg(USB_DEBUG_DEVNUM,
634 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
635 USB_REQ_SET_FEATURE, USB_DEVICE_DEBUG_MODE, 0, NULL, 0);
636 if (ret < 0) {
637 dbgp_printk(" Could not enable the debug device\n");
638 goto err;
639 }
640 dbgp_printk("debug interface enabled\n");
641
642 /* Perform a small write to get the even/odd data state in sync
643 */
644 ret = dbgp_bulk_write(USB_DEBUG_DEVNUM, dbgp_endpoint_out, " ", 1);
645 if (ret < 0) {
646 dbgp_printk("dbgp_bulk_write failed: %d\n", ret);
647 goto err;
648 }
649 dbgp_printk("small write doned\n");
650
651 return 0;
652err:
653 /* Things didn't work so remove my claim */
654 ctrl = readl(&ehci_debug->control);
655 ctrl &= ~(DBGP_CLAIM | DBGP_OUT);
656 writel(ctrl, &ehci_debug->control);
657 return -1;
658
659next_debug_port:
660 port_map_tried |= (1<<(debug_port - 1));
661 new_debug_port = ((debug_port-1+1)%n_ports) + 1;
662 if (port_map_tried != ((1<<n_ports) - 1)) {
663 set_debug_port(new_debug_port);
664 goto try_next_port;
665 }
666 if (--playtimes) {
667 set_debug_port(new_debug_port);
668 goto try_next_time;
669 }
670
671 return -1;
672}
673
674int __init early_dbgp_init(char *s)
675{
676 u32 debug_port, bar, offset;
677 u32 bus, slot, func, cap;
678 void __iomem *ehci_bar;
679 u32 dbgp_num;
680 u32 bar_val;
681 char *e;
682 int ret;
683 u8 byte;
684
685 if (!early_pci_allowed())
686 return -1;
687
688 dbgp_num = 0;
689 if (*s)
690 dbgp_num = simple_strtoul(s, &e, 10);
691 dbgp_printk("dbgp_num: %d\n", dbgp_num);
692
693 cap = find_dbgp(dbgp_num, &bus, &slot, &func);
694 if (!cap)
695 return -1;
696
697 dbgp_printk("Found EHCI debug port on %02x:%02x.%1x\n", bus, slot,
698 func);
699
700 debug_port = read_pci_config(bus, slot, func, cap);
701 bar = (debug_port >> 29) & 0x7;
702 bar = (bar * 4) + 0xc;
703 offset = (debug_port >> 16) & 0xfff;
704 dbgp_printk("bar: %02x offset: %03x\n", bar, offset);
705 if (bar != PCI_BASE_ADDRESS_0) {
706 dbgp_printk("only debug ports on bar 1 handled.\n");
707
708 return -1;
709 }
710
711 bar_val = read_pci_config(bus, slot, func, PCI_BASE_ADDRESS_0);
712 dbgp_printk("bar_val: %02x offset: %03x\n", bar_val, offset);
713 if (bar_val & ~PCI_BASE_ADDRESS_MEM_MASK) {
714 dbgp_printk("only simple 32bit mmio bars supported\n");
715
716 return -1;
717 }
718
719 /* double check if the mem space is enabled */
720 byte = read_pci_config_byte(bus, slot, func, 0x04);
721 if (!(byte & 0x2)) {
722 byte |= 0x02;
723 write_pci_config_byte(bus, slot, func, 0x04, byte);
724 dbgp_printk("mmio for ehci enabled\n");
725 }
726
727 /*
728 * FIXME I don't have the bar size so just guess PAGE_SIZE is more
729 * than enough. 1K is the biggest I have seen.
730 */
731 set_fixmap_nocache(FIX_DBGP_BASE, bar_val & PAGE_MASK);
732 ehci_bar = (void __iomem *)__fix_to_virt(FIX_DBGP_BASE);
733 ehci_bar += bar_val & ~PAGE_MASK;
734 dbgp_printk("ehci_bar: %p\n", ehci_bar);
735
736 ehci_caps = ehci_bar;
737 ehci_regs = ehci_bar + HC_LENGTH(readl(&ehci_caps->hc_capbase));
738 ehci_debug = ehci_bar + offset;
739 ehci_dev.bus = bus;
740 ehci_dev.slot = slot;
741 ehci_dev.func = func;
742
743 detect_set_debug_port();
744
745 ret = ehci_setup();
746 if (ret < 0) {
747 dbgp_printk("ehci_setup failed\n");
748 ehci_debug = NULL;
749
750 return -1;
751 }
752
753 return 0;
754}
755
756static void early_dbgp_write(struct console *con, const char *str, u32 n)
757{
758 int chunk, ret;
Jason Wessel87a5d152009-08-20 15:39:49 -0500759 char buf[DBGP_MAX_PACKET];
760 int use_cr = 0;
Jason Wesseldf6c5162009-08-20 15:39:48 -0500761
762 if (!ehci_debug)
763 return;
764 while (n > 0) {
Jason Wessel87a5d152009-08-20 15:39:49 -0500765 for (chunk = 0; chunk < DBGP_MAX_PACKET && n > 0;
766 str++, chunk++, n--) {
767 if (!use_cr && *str == '\n') {
768 use_cr = 1;
769 buf[chunk] = '\r';
770 str--;
771 n++;
772 continue;
773 }
774 if (use_cr)
775 use_cr = 0;
776 buf[chunk] = *str;
777 }
Jason Wesseldf6c5162009-08-20 15:39:48 -0500778 ret = dbgp_bulk_write(USB_DEBUG_DEVNUM,
Jason Wessel87a5d152009-08-20 15:39:49 -0500779 dbgp_endpoint_out, buf, chunk);
Jason Wesseldf6c5162009-08-20 15:39:48 -0500780 }
781}
782
783struct console early_dbgp_console = {
784 .name = "earlydbg",
785 .write = early_dbgp_write,
786 .flags = CON_PRINTBUFFER,
787 .index = -1,
788};
789