Rename almost all occurences of LinuxBIOS to coreboot.
[coreboot.git] / src / lib / usbdebug_direct.c
1 /*
2  * Copyright (C) 2006 Eric Biederman (ebiederm@xmission.com)
3  *
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.
7  *
8  *      2006.12.10 yhlu moved it to corbeoot and use struct instead
9  */
10 #ifndef __ROMCC__
11 #include <console/console.h>
12 #else
13 #if CONFIG_USE_PRINTK_IN_CAR==0
14 #define printk_debug(fmt, arg...)   do {} while(0)
15 #endif
16 #endif
17
18
19 #include <arch/io.h>
20
21 #include <usb_ch9.h>
22 #include <ehci.h>
23 #include <usbdebug_direct.h>
24
25 #define USB_DEBUG_DEVNUM 127
26
27 #define DBGP_DATA_TOGGLE        0x8800
28 #define DBGP_PID_UPDATE(x, tok) \
29         ((((x) ^ DBGP_DATA_TOGGLE) & 0xffff00) | ((tok) & 0xff))
30
31 #define DBGP_LEN_UPDATE(x, len) (((x) & ~0x0f) | ((len) & 0x0f))
32 /*
33  * USB Packet IDs (PIDs)
34  */
35
36 /* token */
37 #define USB_PID_OUT             0xe1
38 #define USB_PID_IN              0x69
39 #define USB_PID_SOF             0xa5
40 #define USB_PID_SETUP           0x2d
41 /* handshake */
42 #define USB_PID_ACK             0xd2
43 #define USB_PID_NAK             0x5a
44 #define USB_PID_STALL           0x1e
45 #define USB_PID_NYET            0x96
46 /* data */
47 #define USB_PID_DATA0           0xc3
48 #define USB_PID_DATA1           0x4b
49 #define USB_PID_DATA2           0x87
50 #define USB_PID_MDATA           0x0f
51 /* Special */
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
57
58 #define USB_PID_DATA_TOGGLE     0x88
59 #define DBGP_CLAIM (DBGP_OWNER | DBGP_ENABLED | DBGP_INUSE)
60
61 #define PCI_CAP_ID_EHCI_DEBUG   0xa
62
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
67
68 #define DBGP_MAX_PACKET         8
69
70 static int dbgp_wait_until_complete(struct ehci_dbg_port *ehci_debug)
71 {
72         unsigned ctrl;
73         int loop = 0x100000;
74         do {
75                 ctrl = readl(&ehci_debug->control);
76                 /* Stop when the transaction is finished */
77                 if (ctrl & DBGP_DONE)
78                         break;
79         } while(--loop>0); 
80
81         if (!loop) return -1000;
82
83         /* Now that we have observed the completed transaction,
84          * clear the done bit.
85          */
86         writel(ctrl | DBGP_DONE, &ehci_debug->control);
87         return (ctrl & DBGP_ERROR) ? -DBGP_ERRCODE(ctrl) : DBGP_LEN(ctrl);
88 }
89
90 static void dbgp_mdelay(int ms)
91 {
92         int i;
93         while (ms--) {
94                 for (i = 0; i < 1000; i++)
95                         outb(0x1, 0x80);
96         }
97 }
98
99 static void dbgp_breath(void)
100 {
101         /* Sleep to give the debug port a chance to breathe */
102 }
103
104 static int dbgp_wait_until_done(struct ehci_dbg_port *ehci_debug, unsigned ctrl)
105 {
106         unsigned pids, lpid;
107         int ret;
108
109         int loop = 3;
110 retry:
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);
115
116         if (ret < 0)
117                 return ret;
118
119         /* If the port is getting full or it has dropped data
120          * start pacing ourselves, not necessary but it's friendly.
121          */
122         if ((lpid == USB_PID_NAK) || (lpid == USB_PID_NYET))
123                 dbgp_breath();
124         
125         /* If I get a NACK reissue the transmission */
126         if (lpid == USB_PID_NAK) {
127                 if (--loop > 0) goto retry;
128         }
129
130         return ret;
131 }
132
133 static void dbgp_set_data(struct ehci_dbg_port *ehci_debug, const void *buf, int size)
134 {
135         const unsigned char *bytes = buf;
136         unsigned lo, hi;
137         int i;
138         lo = hi = 0;
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);
145 }
146
147 static void dbgp_get_data(struct ehci_dbg_port *ehci_debug, void *buf, int size)
148 {
149         unsigned char *bytes = buf;
150         unsigned lo, hi;
151         int i;
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;
158 }
159
160 static int dbgp_bulk_write(struct ehci_dbg_port *ehci_debug, unsigned devnum, unsigned endpoint, const char *bytes, int size)
161 {
162         unsigned pids, addr, ctrl;
163         int ret;
164         if (size > DBGP_MAX_PACKET)
165                 return -1;
166
167         addr = DBGP_EPADDR(devnum, endpoint);
168
169         pids = readl(&ehci_debug->pids);
170         pids = DBGP_PID_UPDATE(pids, USB_PID_OUT);
171         
172         ctrl = readl(&ehci_debug->control);
173         ctrl = DBGP_LEN_UPDATE(ctrl, size);
174         ctrl |= DBGP_OUT;
175         ctrl |= DBGP_GO;
176
177         dbgp_set_data(ehci_debug, bytes, size);
178         writel(addr, &ehci_debug->address);
179         writel(pids, &ehci_debug->pids);
180
181         ret = dbgp_wait_until_done(ehci_debug, ctrl);
182         if (ret < 0) {
183                 return ret;
184         }
185         return ret;
186 }
187
188 int dbgp_bulk_write_x(struct ehci_debug_info *dbg_info, const char *bytes, int size)
189 {
190         return dbgp_bulk_write(dbg_info->ehci_debug, dbg_info->devnum, dbg_info->endpoint_out, bytes, size);
191 }
192
193 static int dbgp_bulk_read(struct ehci_dbg_port *ehci_debug, unsigned devnum, unsigned endpoint, void *data, int size)
194 {
195         unsigned pids, addr, ctrl;
196         int ret;
197
198         if (size > DBGP_MAX_PACKET)
199                 return -1;
200
201         addr = DBGP_EPADDR(devnum, endpoint);
202
203         pids = readl(&ehci_debug->pids);
204         pids = DBGP_PID_UPDATE(pids, USB_PID_IN);
205                 
206         ctrl = readl(&ehci_debug->control);
207         ctrl = DBGP_LEN_UPDATE(ctrl, size);
208         ctrl &= ~DBGP_OUT;
209         ctrl |= DBGP_GO;
210                 
211         writel(addr, &ehci_debug->address);
212         writel(pids, &ehci_debug->pids);
213         ret = dbgp_wait_until_done(ehci_debug, ctrl);
214         if (ret < 0)
215                 return ret;
216         if (size > ret)
217                 size = ret;
218         dbgp_get_data(ehci_debug, data, size);
219         return ret;
220 }
221 int dbgp_bulk_read_x(struct ehci_debug_info *dbg_info, void *data, int size)
222 {
223         return dbgp_bulk_read(dbg_info->ehci_debug, dbg_info->devnum, dbg_info->endpoint_in, data, size);
224 }
225
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)
228 {
229         unsigned pids, addr, ctrl;
230         struct usb_ctrlrequest req;
231         int read;
232         int ret;
233
234         read = (requesttype & USB_DIR_IN) != 0;
235         if (size > (read?DBGP_MAX_PACKET:0))
236                 return -1;
237         
238         /* Compute the control message */
239         req.bRequestType = requesttype;
240         req.bRequest = request;
241         req.wValue = value;
242         req.wIndex = index;
243         req.wLength = size;
244
245         pids = DBGP_PID_SET(USB_PID_DATA0, USB_PID_SETUP);
246         addr = DBGP_EPADDR(devnum, 0);
247
248         ctrl = readl(&ehci_debug->control);
249         ctrl = DBGP_LEN_UPDATE(ctrl, sizeof(req));
250         ctrl |= DBGP_OUT;
251         ctrl |= DBGP_GO;
252
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);
258         if (ret < 0)
259                 return ret;
260
261
262         /* Read the result */
263         ret = dbgp_bulk_read(ehci_debug, devnum, 0, data, size);
264         return ret;
265 }
266
267 static int ehci_reset_port(struct ehci_regs *ehci_regs, int port)
268 {
269         unsigned portsc;
270         unsigned delay_time, delay;
271         int loop;
272
273         /* Reset the usb debug port */
274         portsc = readl(&ehci_regs->port_status[port - 1]);
275         portsc &= ~PORT_PE;
276         portsc |= PORT_RESET;
277         writel(portsc, &ehci_regs->port_status[port - 1]);
278
279         delay = HUB_ROOT_RESET_TIME;
280         for (delay_time = 0; delay_time < HUB_RESET_TIMEOUT;
281              delay_time += delay) {
282                 dbgp_mdelay(delay);
283
284                 portsc = readl(&ehci_regs->port_status[port - 1]);
285                 if (portsc & PORT_RESET) {
286                         /* force reset to complete */
287                         loop = 2;
288                         writel(portsc & ~(PORT_RWC_BITS | PORT_RESET), 
289                                 &ehci_regs->port_status[port - 1]);
290                         do {    
291                                 dbgp_mdelay(delay);
292                                 portsc = readl(&ehci_regs->port_status[port - 1]);
293                                 delay_time += delay;
294                         } while ((portsc & PORT_RESET) && (--loop > 0));
295                         if (!loop) {
296                                 printk_debug("ehci_reset_port forced done");
297                         }
298                 }
299
300                 /* Device went away? */
301                 if (!(portsc & PORT_CONNECT))
302                         return -107;//-ENOTCONN;
303
304                 /* bomb out completely if something weird happend */
305                 if ((portsc & PORT_CSC))
306                         return -22;//-EINVAL;
307
308                 /* If we've finished resetting, then break out of the loop */
309                 if (!(portsc & PORT_RESET) && (portsc & PORT_PE))
310                         return 0;
311         }
312         return -16;//-EBUSY;
313 }
314
315 static int ehci_wait_for_port(struct ehci_regs *ehci_regs, int port)
316 {
317         unsigned status;
318         int ret, reps;
319         for (reps = 0; reps < 3; reps++) {
320                 dbgp_mdelay(100);
321                 status = readl(&ehci_regs->status);
322                 if (status & STS_PCD) {
323                         ret = ehci_reset_port(ehci_regs, port);
324                         if (ret == 0)
325                                 return 0;
326                 }
327         }
328         return -107; //-ENOTCONN;
329 }
330
331
332 #define DBGP_DEBUG 1
333 #if DBGP_DEBUG
334 # define dbgp_printk printk_debug
335 #else
336 #define dbgp_printk(fmt, arg...)   do {} while(0)
337 #endif
338 static void usbdebug_direct_init(unsigned ehci_bar, unsigned offset, struct ehci_debug_info *info)
339 {
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;
347         int ret;
348         unsigned delay_time, delay;
349         int loop;
350
351         unsigned cmd,  status, portsc, hcs_params, debug_port, n_ports, new_debug_port;
352         int i;
353         unsigned port_map_tried;
354
355         unsigned playtimes = 3;
356
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);
360
361         info->ehci_debug = (void *)0;
362
363 try_next_time:
364         port_map_tried = 0;
365
366 try_next_port:
367         hcs_params = readl(&ehci_caps->hcs_params);
368         debug_port = HCS_DEBUG_PORT(hcs_params);
369         n_ports    = HCS_N_PORTS(hcs_params);
370
371         dbgp_printk("debug_port: %d\n", debug_port);
372         dbgp_printk("n_ports:    %d\n", n_ports);
373
374 #if 0
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);
378         }
379 #endif
380
381         if(port_map_tried && (new_debug_port!=debug_port)) {
382                 if(--playtimes) {
383                         set_debug_port(debug_port);
384                         goto try_next_time;
385                 }
386                 return; 
387         }
388
389         /* Reset the EHCI controller */
390         loop = 10;
391         cmd = readl(&ehci_regs->command);
392         cmd |= CMD_RESET;
393         writel(cmd, &ehci_regs->command);
394         do {
395                 cmd = readl(&ehci_regs->command);
396         } while ((cmd & CMD_RESET) && (--loop > 0));
397
398         if(!loop) {
399                 dbgp_printk("can not reset ehci\n");
400                 return;
401         }
402         dbgp_printk("ehci reset done\n");
403
404         /* Claim ownership, but do not enable yet */
405         ctrl = readl(&ehci_debug->control);
406         ctrl |= DBGP_OWNER;
407         ctrl &= ~(DBGP_ENABLED | DBGP_INUSE);
408         writel(ctrl, &ehci_debug->control);
409
410         /* Start the ehci running */
411         cmd = readl(&ehci_regs->command);
412         cmd &= ~(CMD_LRESET | CMD_IAAD | CMD_PSE | CMD_ASE | CMD_RESET);
413         cmd |= CMD_RUN;
414         writel(cmd, &ehci_regs->command);
415
416         /* Ensure everything is routed to the EHCI */
417         writel(FLAG_CF, &ehci_regs->configured_flag);
418
419         /* Wait until the controller is no longer halted */
420         loop = 10;
421         do {
422                 status = readl(&ehci_regs->status);
423         } while ((status & STS_HALT) && (--loop>0));
424
425         if(!loop) {
426                 dbgp_printk("ehci can be started\n");
427                 return;
428         }
429         dbgp_printk("ehci started\n");
430
431         /* Wait for a device to show up in the debug port */
432         ret = ehci_wait_for_port(ehci_regs, debug_port);
433         if (ret < 0) {
434                 dbgp_printk("No device found in debug port %d\n", debug_port);
435                 goto next_debug_port;
436         }
437         dbgp_printk("ehci wait for port done\n");
438
439         /* Enable the debug port */
440         ctrl = readl(&ehci_debug->control);
441         ctrl |= DBGP_CLAIM;
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);
447                 goto err;
448         }
449         dbgp_printk("debug ported enabled\n");
450
451         /* Completely transfer the debug device to the debug controller */
452         portsc = readl(&ehci_regs->port_status[debug_port - 1]);
453         portsc &= ~PORT_PE;
454         writel(portsc, &ehci_regs->port_status[debug_port - 1]);
455
456         dbgp_mdelay(100);
457
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));
464                 if (ret > 0)
465                         break;
466         }
467         if (devnum > 127) {
468                 dbgp_printk("Could not find attached debug device\n");
469                 goto err;
470         }
471         if (ret < 0) {
472                 dbgp_printk("Attached device is not a debug device\n");
473                 goto err;
474         }
475         dbgp_endpoint_out = dbgp_desc.bDebugOutEndpoint;
476         dbgp_endpoint_in = dbgp_desc.bDebugInEndpoint;
477
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);
483                 if (ret < 0) {
484                         dbgp_printk("Could not move attached device to %d\n", 
485                                 USB_DEBUG_DEVNUM);
486                         goto err;
487                 }
488                 devnum = USB_DEBUG_DEVNUM;
489                 dbgp_printk("debug device renamed to 127\n");
490         }
491
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);
496         if (ret < 0) {
497                 dbgp_printk(" Could not enable the debug device\n");
498                 goto err;
499         }
500         dbgp_printk("debug interface enabled\n");
501
502         /* Perform a small write to get the even/odd data state in sync
503          */
504         ret = dbgp_bulk_write(ehci_debug, USB_DEBUG_DEVNUM, dbgp_endpoint_out, " ",1);
505         if (ret < 0) {
506                 dbgp_printk("dbgp_bulk_write failed: %d\n", ret);
507                 goto err;
508         }
509         dbgp_printk("small write doned\n");
510
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;
517         
518         return;
519 err:
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);
524
525 next_debug_port:
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);
530                 goto try_next_port;
531         }
532         if(--playtimes) {
533                 set_debug_port(debug_port);
534                 goto try_next_time;
535         }
536
537 }
538
539