2 * Copyright (C) 2006 Eric Biederman (ebiederm@xmission.com)
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License version
6 * 2 as published by the Free Software Foundation.
8 * 2006.12.10 yhlu moved it to corbeoot and use struct instead
11 #include <console/console.h>
13 #if CONFIG_USE_PRINTK_IN_CAR==0
14 #define printk_debug(fmt, arg...) do {} while(0)
23 #include <usbdebug_direct.h>
25 #define USB_DEBUG_DEVNUM 127
27 #define DBGP_DATA_TOGGLE 0x8800
28 #define DBGP_PID_UPDATE(x, tok) \
29 ((((x) ^ DBGP_DATA_TOGGLE) & 0xffff00) | ((tok) & 0xff))
31 #define DBGP_LEN_UPDATE(x, len) (((x) & ~0x0f) | ((len) & 0x0f))
33 * USB Packet IDs (PIDs)
37 #define USB_PID_OUT 0xe1
38 #define USB_PID_IN 0x69
39 #define USB_PID_SOF 0xa5
40 #define USB_PID_SETUP 0x2d
42 #define USB_PID_ACK 0xd2
43 #define USB_PID_NAK 0x5a
44 #define USB_PID_STALL 0x1e
45 #define USB_PID_NYET 0x96
47 #define USB_PID_DATA0 0xc3
48 #define USB_PID_DATA1 0x4b
49 #define USB_PID_DATA2 0x87
50 #define USB_PID_MDATA 0x0f
52 #define USB_PID_PREAMBLE 0x3c
53 #define USB_PID_ERR 0x3c
54 #define USB_PID_SPLIT 0x78
55 #define USB_PID_PING 0xb4
56 #define USB_PID_UNDEF_0 0xf0
58 #define USB_PID_DATA_TOGGLE 0x88
59 #define DBGP_CLAIM (DBGP_OWNER | DBGP_ENABLED | DBGP_INUSE)
61 #define PCI_CAP_ID_EHCI_DEBUG 0xa
63 #define HUB_ROOT_RESET_TIME 50 /* times are in msec */
64 #define HUB_SHORT_RESET_TIME 10
65 #define HUB_LONG_RESET_TIME 200
66 #define HUB_RESET_TIMEOUT 500
68 #define DBGP_MAX_PACKET 8
70 static int dbgp_wait_until_complete(struct ehci_dbg_port *ehci_debug)
75 ctrl = readl(&ehci_debug->control);
76 /* Stop when the transaction is finished */
81 if (!loop) return -1000;
83 /* Now that we have observed the completed transaction,
86 writel(ctrl | DBGP_DONE, &ehci_debug->control);
87 return (ctrl & DBGP_ERROR) ? -DBGP_ERRCODE(ctrl) : DBGP_LEN(ctrl);
90 static void dbgp_mdelay(int ms)
94 for (i = 0; i < 1000; i++)
99 static void dbgp_breath(void)
101 /* Sleep to give the debug port a chance to breathe */
104 static int dbgp_wait_until_done(struct ehci_dbg_port *ehci_debug, unsigned ctrl)
111 writel(ctrl | DBGP_GO, &ehci_debug->control);
112 ret = dbgp_wait_until_complete(ehci_debug);
113 pids = readl(&ehci_debug->pids);
114 lpid = DBGP_PID_GET(pids);
119 /* If the port is getting full or it has dropped data
120 * start pacing ourselves, not necessary but it's friendly.
122 if ((lpid == USB_PID_NAK) || (lpid == USB_PID_NYET))
125 /* If I get a NACK reissue the transmission */
126 if (lpid == USB_PID_NAK) {
127 if (--loop > 0) goto retry;
133 static void dbgp_set_data(struct ehci_dbg_port *ehci_debug, const void *buf, int size)
135 const unsigned char *bytes = buf;
139 for (i = 0; i < 4 && i < size; i++)
140 lo |= bytes[i] << (8*i);
141 for (; i < 8 && i < size; i++)
142 hi |= bytes[i] << (8*(i - 4));
143 writel(lo, &ehci_debug->data03);
144 writel(hi, &ehci_debug->data47);
147 static void dbgp_get_data(struct ehci_dbg_port *ehci_debug, void *buf, int size)
149 unsigned char *bytes = buf;
152 lo = readl(&ehci_debug->data03);
153 hi = readl(&ehci_debug->data47);
154 for (i = 0; i < 4 && i < size; i++)
155 bytes[i] = (lo >> (8*i)) & 0xff;
156 for (; i < 8 && i < size; i++)
157 bytes[i] = (hi >> (8*(i - 4))) & 0xff;
160 static int dbgp_bulk_write(struct ehci_dbg_port *ehci_debug, unsigned devnum, unsigned endpoint, const char *bytes, int size)
162 unsigned pids, addr, ctrl;
164 if (size > DBGP_MAX_PACKET)
167 addr = DBGP_EPADDR(devnum, endpoint);
169 pids = readl(&ehci_debug->pids);
170 pids = DBGP_PID_UPDATE(pids, USB_PID_OUT);
172 ctrl = readl(&ehci_debug->control);
173 ctrl = DBGP_LEN_UPDATE(ctrl, size);
177 dbgp_set_data(ehci_debug, bytes, size);
178 writel(addr, &ehci_debug->address);
179 writel(pids, &ehci_debug->pids);
181 ret = dbgp_wait_until_done(ehci_debug, ctrl);
188 int dbgp_bulk_write_x(struct ehci_debug_info *dbg_info, const char *bytes, int size)
190 return dbgp_bulk_write(dbg_info->ehci_debug, dbg_info->devnum, dbg_info->endpoint_out, bytes, size);
193 static int dbgp_bulk_read(struct ehci_dbg_port *ehci_debug, unsigned devnum, unsigned endpoint, void *data, int size)
195 unsigned pids, addr, ctrl;
198 if (size > DBGP_MAX_PACKET)
201 addr = DBGP_EPADDR(devnum, endpoint);
203 pids = readl(&ehci_debug->pids);
204 pids = DBGP_PID_UPDATE(pids, USB_PID_IN);
206 ctrl = readl(&ehci_debug->control);
207 ctrl = DBGP_LEN_UPDATE(ctrl, size);
211 writel(addr, &ehci_debug->address);
212 writel(pids, &ehci_debug->pids);
213 ret = dbgp_wait_until_done(ehci_debug, ctrl);
218 dbgp_get_data(ehci_debug, data, size);
221 int dbgp_bulk_read_x(struct ehci_debug_info *dbg_info, void *data, int size)
223 return dbgp_bulk_read(dbg_info->ehci_debug, dbg_info->devnum, dbg_info->endpoint_in, data, size);
226 static int dbgp_control_msg(struct ehci_dbg_port *ehci_debug, unsigned devnum, int requesttype, int request,
227 int value, int index, void *data, int size)
229 unsigned pids, addr, ctrl;
230 struct usb_ctrlrequest req;
234 read = (requesttype & USB_DIR_IN) != 0;
235 if (size > (read?DBGP_MAX_PACKET:0))
238 /* Compute the control message */
239 req.bRequestType = requesttype;
240 req.bRequest = request;
245 pids = DBGP_PID_SET(USB_PID_DATA0, USB_PID_SETUP);
246 addr = DBGP_EPADDR(devnum, 0);
248 ctrl = readl(&ehci_debug->control);
249 ctrl = DBGP_LEN_UPDATE(ctrl, sizeof(req));
253 /* Send the setup message */
254 dbgp_set_data(ehci_debug, &req, sizeof(req));
255 writel(addr, &ehci_debug->address);
256 writel(pids, &ehci_debug->pids);
257 ret = dbgp_wait_until_done(ehci_debug, ctrl);
262 /* Read the result */
263 ret = dbgp_bulk_read(ehci_debug, devnum, 0, data, size);
267 static int ehci_reset_port(struct ehci_regs *ehci_regs, int port)
270 unsigned delay_time, delay;
273 /* Reset the usb debug port */
274 portsc = readl(&ehci_regs->port_status[port - 1]);
276 portsc |= PORT_RESET;
277 writel(portsc, &ehci_regs->port_status[port - 1]);
279 delay = HUB_ROOT_RESET_TIME;
280 for (delay_time = 0; delay_time < HUB_RESET_TIMEOUT;
281 delay_time += delay) {
284 portsc = readl(&ehci_regs->port_status[port - 1]);
285 if (portsc & PORT_RESET) {
286 /* force reset to complete */
288 writel(portsc & ~(PORT_RWC_BITS | PORT_RESET),
289 &ehci_regs->port_status[port - 1]);
292 portsc = readl(&ehci_regs->port_status[port - 1]);
294 } while ((portsc & PORT_RESET) && (--loop > 0));
296 printk_debug("ehci_reset_port forced done");
300 /* Device went away? */
301 if (!(portsc & PORT_CONNECT))
302 return -107;//-ENOTCONN;
304 /* bomb out completely if something weird happend */
305 if ((portsc & PORT_CSC))
306 return -22;//-EINVAL;
308 /* If we've finished resetting, then break out of the loop */
309 if (!(portsc & PORT_RESET) && (portsc & PORT_PE))
315 static int ehci_wait_for_port(struct ehci_regs *ehci_regs, int port)
319 for (reps = 0; reps < 3; reps++) {
321 status = readl(&ehci_regs->status);
322 if (status & STS_PCD) {
323 ret = ehci_reset_port(ehci_regs, port);
328 return -107; //-ENOTCONN;
334 # define dbgp_printk printk_debug
336 #define dbgp_printk(fmt, arg...) do {} while(0)
338 static void usbdebug_direct_init(unsigned ehci_bar, unsigned offset, struct ehci_debug_info *info)
340 struct ehci_caps *ehci_caps;
341 struct ehci_regs *ehci_regs;
342 struct ehci_dbg_port *ehci_debug;
343 unsigned dbgp_endpoint_out;
344 unsigned dbgp_endpoint_in;
345 struct usb_debug_descriptor dbgp_desc;
346 unsigned ctrl, devnum;
348 unsigned delay_time, delay;
351 unsigned cmd, status, portsc, hcs_params, debug_port, n_ports, new_debug_port;
353 unsigned port_map_tried;
355 unsigned playtimes = 3;
357 ehci_caps = (struct ehci_caps *)ehci_bar;
358 ehci_regs = (struct ehci_regs *)(ehci_bar + HC_LENGTH(readl(&ehci_caps->hc_capbase)));
359 ehci_debug = (struct ehci_dbg_port *)(ehci_bar + offset);
361 info->ehci_debug = (void *)0;
367 hcs_params = readl(&ehci_caps->hcs_params);
368 debug_port = HCS_DEBUG_PORT(hcs_params);
369 n_ports = HCS_N_PORTS(hcs_params);
371 dbgp_printk("debug_port: %d\n", debug_port);
372 dbgp_printk("n_ports: %d\n", n_ports);
375 for (i = 1; i <= n_ports; i++) {
376 portsc = readl(&ehci_regs->port_status[i-1]);
377 dbgp_printk("portstatus%d: %08x\n", i, portsc);
381 if(port_map_tried && (new_debug_port!=debug_port)) {
383 set_debug_port(debug_port);
389 /* Reset the EHCI controller */
391 cmd = readl(&ehci_regs->command);
393 writel(cmd, &ehci_regs->command);
395 cmd = readl(&ehci_regs->command);
396 } while ((cmd & CMD_RESET) && (--loop > 0));
399 dbgp_printk("can not reset ehci\n");
402 dbgp_printk("ehci reset done\n");
404 /* Claim ownership, but do not enable yet */
405 ctrl = readl(&ehci_debug->control);
407 ctrl &= ~(DBGP_ENABLED | DBGP_INUSE);
408 writel(ctrl, &ehci_debug->control);
410 /* Start the ehci running */
411 cmd = readl(&ehci_regs->command);
412 cmd &= ~(CMD_LRESET | CMD_IAAD | CMD_PSE | CMD_ASE | CMD_RESET);
414 writel(cmd, &ehci_regs->command);
416 /* Ensure everything is routed to the EHCI */
417 writel(FLAG_CF, &ehci_regs->configured_flag);
419 /* Wait until the controller is no longer halted */
422 status = readl(&ehci_regs->status);
423 } while ((status & STS_HALT) && (--loop>0));
426 dbgp_printk("ehci can be started\n");
429 dbgp_printk("ehci started\n");
431 /* Wait for a device to show up in the debug port */
432 ret = ehci_wait_for_port(ehci_regs, debug_port);
434 dbgp_printk("No device found in debug port %d\n", debug_port);
435 goto next_debug_port;
437 dbgp_printk("ehci wait for port done\n");
439 /* Enable the debug port */
440 ctrl = readl(&ehci_debug->control);
442 writel(ctrl, &ehci_debug->control);
443 ctrl = readl(&ehci_debug->control);
444 if ((ctrl & DBGP_CLAIM) != DBGP_CLAIM) {
445 dbgp_printk("No device in debug port\n");
446 writel(ctrl & ~DBGP_CLAIM, &ehci_debug->control);
449 dbgp_printk("debug ported enabled\n");
451 /* Completely transfer the debug device to the debug controller */
452 portsc = readl(&ehci_regs->port_status[debug_port - 1]);
454 writel(portsc, &ehci_regs->port_status[debug_port - 1]);
458 /* Find the debug device and make it device number 127 */
459 for (devnum = 0; devnum <= 127; devnum++) {
460 ret = dbgp_control_msg(ehci_debug, devnum,
461 USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
462 USB_REQ_GET_DESCRIPTOR, (USB_DT_DEBUG << 8), 0,
463 &dbgp_desc, sizeof(dbgp_desc));
468 dbgp_printk("Could not find attached debug device\n");
472 dbgp_printk("Attached device is not a debug device\n");
475 dbgp_endpoint_out = dbgp_desc.bDebugOutEndpoint;
476 dbgp_endpoint_in = dbgp_desc.bDebugInEndpoint;
478 /* Move the device to 127 if it isn't already there */
479 if (devnum != USB_DEBUG_DEVNUM) {
480 ret = dbgp_control_msg(ehci_debug, devnum,
481 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
482 USB_REQ_SET_ADDRESS, USB_DEBUG_DEVNUM, 0, (void *)0, 0);
484 dbgp_printk("Could not move attached device to %d\n",
488 devnum = USB_DEBUG_DEVNUM;
489 dbgp_printk("debug device renamed to 127\n");
492 /* Enable the debug interface */
493 ret = dbgp_control_msg(ehci_debug, USB_DEBUG_DEVNUM,
494 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
495 USB_REQ_SET_FEATURE, USB_DEVICE_DEBUG_MODE, 0, (void *)0, 0);
497 dbgp_printk(" Could not enable the debug device\n");
500 dbgp_printk("debug interface enabled\n");
502 /* Perform a small write to get the even/odd data state in sync
504 ret = dbgp_bulk_write(ehci_debug, USB_DEBUG_DEVNUM, dbgp_endpoint_out, " ",1);
506 dbgp_printk("dbgp_bulk_write failed: %d\n", ret);
509 dbgp_printk("small write doned\n");
511 info->ehci_caps = ehci_caps;
512 info->ehci_regs = ehci_regs;
513 info->ehci_debug = ehci_debug;
514 info->devnum = devnum;
515 info->endpoint_out = dbgp_endpoint_out;
516 info->endpoint_in = dbgp_endpoint_in;
520 /* Things didn't work so remove my claim */
521 ctrl = readl(&ehci_debug->control);
522 ctrl &= ~(DBGP_CLAIM | DBGP_OUT);
523 writel(ctrl, &ehci_debug->control);
526 port_map_tried |= (1<<(debug_port-1));
527 if(port_map_tried != ((1<<n_ports) -1)) {
528 new_debug_port = ((debug_port-1+1)%n_ports) + 1;
529 set_debug_port(new_debug_port);
533 set_debug_port(debug_port);