More cleanup for the delta board and the generic usb_ohci driver. Added
[platform/kernel/u-boot.git] / drivers / usb_ohci.c
1 /*
2  * URB OHCI HCD (Host Controller Driver) for USB on the AT91RM9200.
3  *
4  * (C) Copyright 2003
5  * Gary Jennejohn, DENX Software Engineering <gj@denx.de>
6  *
7  * Note: Much of this code has been derived from Linux 2.4
8  * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
9  * (C) Copyright 2000-2002 David Brownell
10  *
11  * Modified for the MP2USB by (C) Copyright 2005 Eric Benard
12  * ebenard@eukrea.com - based on s3c24x0's driver
13  *
14  * See file CREDITS for list of people who contributed to this
15  * project.
16  *
17  * This program is free software; you can redistribute it and/or
18  * modify it under the terms of the GNU General Public License as
19  * published by the Free Software Foundation; either version 2 of
20  * the License, or (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30  * MA 02111-1307 USA
31  *
32  */
33 /*
34  * IMPORTANT NOTES
35  * 1 - you MUST define LITTLEENDIAN in the configuration file for the
36  *     board or this driver will NOT work!
37  * 2 - this driver is intended for use with USB Mass Storage Devices
38  *     (BBB) ONLY. There is NO support for Interrupt or Isochronous pipes!
39  * 3 - when running on a PQFP208 AT91RM9200, define CONFIG_AT91C_PQFP_UHPBUG
40  *     to activate workaround for bug #41 or this driver will NOT work!
41  */
42
43 #include <common.h>
44 /* #include <pci.h> no PCI on the S3C24X0 */
45
46 #ifdef CONFIG_USB_OHCI
47
48 #include <asm/arch/hardware.h>
49
50 #include <malloc.h>
51 #include <usb.h>
52 #include "usb_ohci.h"
53
54 #ifdef CONFIG_ARM920T
55 # define OHCI_USE_NPS           /* force NoPowerSwitching mode */
56 #endif
57
58 #undef OHCI_VERBOSE_DEBUG       /* not always helpful */
59
60 /* For initializing controller (mask in an HCFS mode too) */
61 #define OHCI_CONTROL_INIT \
62         (OHCI_CTRL_CBSR & 0x3) | OHCI_CTRL_IE | OHCI_CTRL_PLE
63
64 #define readl(a) (*((vu_long *)(a)))
65 #define writel(a, b) (*((vu_long *)(b)) = ((vu_long)a))
66
67 #define min_t(type,x,y) ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
68
69 #undef DEBUG
70 #ifdef DEBUG
71 #define dbg(format, arg...) printf("DEBUG: " format "\n", ## arg)
72 #else
73 #define dbg(format, arg...) do {} while(0)
74 #endif /* DEBUG */
75 #define err(format, arg...) printf("ERROR: " format "\n", ## arg)
76 #undef SHOW_INFO
77 #ifdef SHOW_INFO
78 #define info(format, arg...) printf("INFO: " format "\n", ## arg)
79 #else
80 #define info(format, arg...) do {} while(0)
81 #endif
82
83 #define m16_swap(x) swap_16(x)
84 #define m32_swap(x) swap_32(x)
85
86 /* global ohci_t */
87 static ohci_t gohci;
88 /* this must be aligned to a 256 byte boundary */
89 struct ohci_hcca ghcca[1];
90 /* a pointer to the aligned storage */
91 struct ohci_hcca *phcca;
92 /* this allocates EDs for all possible endpoints */
93 struct ohci_device ohci_dev;
94 /* urb_priv */
95 urb_priv_t urb_priv;
96 /* RHSC flag */
97 int got_rhsc;
98 /* device which was disconnected */
99 struct usb_device *devgone;
100
101 /*-------------------------------------------------------------------------*/
102
103 /* AMD-756 (D2 rev) reports corrupt register contents in some cases.
104  * The erratum (#4) description is incorrect.  AMD's workaround waits
105  * till some bits (mostly reserved) are clear; ok for all revs.
106  */
107 #define OHCI_QUIRK_AMD756 0xabcd
108 #define read_roothub(hc, register, mask) ({ \
109         u32 temp = readl (&hc->regs->roothub.register); \
110         if (hc->flags & OHCI_QUIRK_AMD756) \
111                 while (temp & mask) \
112                         temp = readl (&hc->regs->roothub.register); \
113         temp; })
114
115 static u32 roothub_a (struct ohci *hc)
116         { return read_roothub (hc, a, 0xfc0fe000); }
117 static inline u32 roothub_b (struct ohci *hc)
118         { return readl (&hc->regs->roothub.b); }
119 static inline u32 roothub_status (struct ohci *hc)
120         { return readl (&hc->regs->roothub.status); }
121 static u32 roothub_portstatus (struct ohci *hc, int i)
122         { return read_roothub (hc, portstatus [i], 0xffe0fce0); }
123
124
125 /* forward declaration */
126 static int hc_interrupt (void);
127 static void
128 td_submit_job (struct usb_device * dev, unsigned long pipe, void * buffer,
129         int transfer_len, struct devrequest * setup, urb_priv_t * urb, int interval);
130
131 /*-------------------------------------------------------------------------*
132  * URB support functions
133  *-------------------------------------------------------------------------*/
134
135 /* free HCD-private data associated with this URB */
136
137 static void urb_free_priv (urb_priv_t * urb)
138 {
139         int             i;
140         int             last;
141         struct td       * td;
142
143         last = urb->length - 1;
144         if (last >= 0) {
145                 for (i = 0; i <= last; i++) {
146                         td = urb->td[i];
147                         if (td) {
148                                 td->usb_dev = NULL;
149                                 urb->td[i] = NULL;
150                         }
151                 }
152         }
153 }
154
155 /*-------------------------------------------------------------------------*/
156
157 #ifdef DEBUG
158 static int sohci_get_current_frame_number (struct usb_device * dev);
159
160 /* debug| print the main components of an URB
161  * small: 0) header + data packets 1) just header */
162
163 static void pkt_print (struct usb_device * dev, unsigned long pipe, void * buffer,
164         int transfer_len, struct devrequest * setup, char * str, int small)
165 {
166         urb_priv_t * purb = &urb_priv;
167
168         dbg("%s URB:[%4x] dev:%2d,ep:%2d-%c,type:%s,len:%d/%d stat:%#lx",
169                         str,
170                         sohci_get_current_frame_number (dev),
171                         usb_pipedevice (pipe),
172                         usb_pipeendpoint (pipe),
173                         usb_pipeout (pipe)? 'O': 'I',
174                         usb_pipetype (pipe) < 2? (usb_pipeint (pipe)? "INTR": "ISOC"):
175                                 (usb_pipecontrol (pipe)? "CTRL": "BULK"),
176                         purb->actual_length,
177                         transfer_len, dev->status);
178 #ifdef  OHCI_VERBOSE_DEBUG
179         if (!small) {
180                 int i, len;
181
182                 if (usb_pipecontrol (pipe)) {
183                         printf (__FILE__ ": cmd(8):");
184                         for (i = 0; i < 8 ; i++)
185                                 printf (" %02x", ((__u8 *) setup) [i]);
186                         printf ("\n");
187                 }
188                 if (transfer_len > 0 && buffer) {
189                         printf (__FILE__ ": data(%d/%d):",
190                                 purb->actual_length,
191                                 transfer_len);
192                         len = usb_pipeout (pipe)?
193                                         transfer_len: purb->actual_length;
194                         for (i = 0; i < 16 && i < len; i++)
195                                 printf (" %02x", ((__u8 *) buffer) [i]);
196                         printf ("%s\n", i < len? "...": "");
197                 }
198         }
199 #endif
200 }
201
202 /* just for debugging; prints non-empty branches of the int ed tree inclusive iso eds*/
203 void ep_print_int_eds (ohci_t *ohci, char * str) {
204         int i, j;
205          __u32 * ed_p;
206         for (i= 0; i < 32; i++) {
207                 j = 5;
208                 ed_p = &(ohci->hcca->int_table [i]);
209                 if (*ed_p == 0)
210                     continue;
211                 printf (__FILE__ ": %s branch int %2d(%2x):", str, i, i);
212                 while (*ed_p != 0 && j--) {
213                         ed_t *ed = (ed_t *)m32_swap(ed_p);
214                         printf (" ed: %4x;", ed->hwINFO);
215                         ed_p = &ed->hwNextED;
216                 }
217                 printf ("\n");
218         }
219 }
220
221 static void ohci_dump_intr_mask (char *label, __u32 mask)
222 {
223         dbg ("%s: 0x%08x%s%s%s%s%s%s%s%s%s",
224                 label,
225                 mask,
226                 (mask & OHCI_INTR_MIE) ? " MIE" : "",
227                 (mask & OHCI_INTR_OC) ? " OC" : "",
228                 (mask & OHCI_INTR_RHSC) ? " RHSC" : "",
229                 (mask & OHCI_INTR_FNO) ? " FNO" : "",
230                 (mask & OHCI_INTR_UE) ? " UE" : "",
231                 (mask & OHCI_INTR_RD) ? " RD" : "",
232                 (mask & OHCI_INTR_SF) ? " SF" : "",
233                 (mask & OHCI_INTR_WDH) ? " WDH" : "",
234                 (mask & OHCI_INTR_SO) ? " SO" : ""
235                 );
236 }
237
238 static void maybe_print_eds (char *label, __u32 value)
239 {
240         ed_t *edp = (ed_t *)value;
241
242         if (value) {
243                 dbg ("%s %08x", label, value);
244                 dbg ("%08x", edp->hwINFO);
245                 dbg ("%08x", edp->hwTailP);
246                 dbg ("%08x", edp->hwHeadP);
247                 dbg ("%08x", edp->hwNextED);
248         }
249 }
250
251 static char * hcfs2string (int state)
252 {
253         switch (state) {
254                 case OHCI_USB_RESET:    return "reset";
255                 case OHCI_USB_RESUME:   return "resume";
256                 case OHCI_USB_OPER:     return "operational";
257                 case OHCI_USB_SUSPEND:  return "suspend";
258         }
259         return "?";
260 }
261
262 /* dump control and status registers */
263 static void ohci_dump_status (ohci_t *controller)
264 {
265         struct ohci_regs        *regs = controller->regs;
266         __u32                   temp;
267
268         temp = readl (&regs->revision) & 0xff;
269         if (temp != 0x10)
270                 dbg ("spec %d.%d", (temp >> 4), (temp & 0x0f));
271
272         temp = readl (&regs->control);
273         dbg ("control: 0x%08x%s%s%s HCFS=%s%s%s%s%s CBSR=%d", temp,
274                 (temp & OHCI_CTRL_RWE) ? " RWE" : "",
275                 (temp & OHCI_CTRL_RWC) ? " RWC" : "",
276                 (temp & OHCI_CTRL_IR) ? " IR" : "",
277                 hcfs2string (temp & OHCI_CTRL_HCFS),
278                 (temp & OHCI_CTRL_BLE) ? " BLE" : "",
279                 (temp & OHCI_CTRL_CLE) ? " CLE" : "",
280                 (temp & OHCI_CTRL_IE) ? " IE" : "",
281                 (temp & OHCI_CTRL_PLE) ? " PLE" : "",
282                 temp & OHCI_CTRL_CBSR
283                 );
284
285         temp = readl (&regs->cmdstatus);
286         dbg ("cmdstatus: 0x%08x SOC=%d%s%s%s%s", temp,
287                 (temp & OHCI_SOC) >> 16,
288                 (temp & OHCI_OCR) ? " OCR" : "",
289                 (temp & OHCI_BLF) ? " BLF" : "",
290                 (temp & OHCI_CLF) ? " CLF" : "",
291                 (temp & OHCI_HCR) ? " HCR" : ""
292                 );
293
294         ohci_dump_intr_mask ("intrstatus", readl (&regs->intrstatus));
295         ohci_dump_intr_mask ("intrenable", readl (&regs->intrenable));
296
297         maybe_print_eds ("ed_periodcurrent", readl (&regs->ed_periodcurrent));
298
299         maybe_print_eds ("ed_controlhead", readl (&regs->ed_controlhead));
300         maybe_print_eds ("ed_controlcurrent", readl (&regs->ed_controlcurrent));
301
302         maybe_print_eds ("ed_bulkhead", readl (&regs->ed_bulkhead));
303         maybe_print_eds ("ed_bulkcurrent", readl (&regs->ed_bulkcurrent));
304
305         maybe_print_eds ("donehead", readl (&regs->donehead));
306 }
307
308 static void ohci_dump_roothub (ohci_t *controller, int verbose)
309 {
310         __u32                   temp, ndp, i;
311
312         temp = roothub_a (controller);
313         ndp = (temp & RH_A_NDP);
314 #ifdef CONFIG_AT91C_PQFP_UHPBUG
315         ndp = (ndp == 2) ? 1:0;
316 #endif
317         if (verbose) {
318                 dbg ("roothub.a: %08x POTPGT=%d%s%s%s%s%s NDP=%d", temp,
319                         ((temp & RH_A_POTPGT) >> 24) & 0xff,
320                         (temp & RH_A_NOCP) ? " NOCP" : "",
321                         (temp & RH_A_OCPM) ? " OCPM" : "",
322                         (temp & RH_A_DT) ? " DT" : "",
323                         (temp & RH_A_NPS) ? " NPS" : "",
324                         (temp & RH_A_PSM) ? " PSM" : "",
325                         ndp
326                         );
327                 temp = roothub_b (controller);
328                 dbg ("roothub.b: %08x PPCM=%04x DR=%04x",
329                         temp,
330                         (temp & RH_B_PPCM) >> 16,
331                         (temp & RH_B_DR)
332                         );
333                 temp = roothub_status (controller);
334                 dbg ("roothub.status: %08x%s%s%s%s%s%s",
335                         temp,
336                         (temp & RH_HS_CRWE) ? " CRWE" : "",
337                         (temp & RH_HS_OCIC) ? " OCIC" : "",
338                         (temp & RH_HS_LPSC) ? " LPSC" : "",
339                         (temp & RH_HS_DRWE) ? " DRWE" : "",
340                         (temp & RH_HS_OCI) ? " OCI" : "",
341                         (temp & RH_HS_LPS) ? " LPS" : ""
342                         );
343         }
344
345         for (i = 0; i < ndp; i++) {
346                 temp = roothub_portstatus (controller, i);
347                 dbg ("roothub.portstatus [%d] = 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s",
348                         i,
349                         temp,
350                         (temp & RH_PS_PRSC) ? " PRSC" : "",
351                         (temp & RH_PS_OCIC) ? " OCIC" : "",
352                         (temp & RH_PS_PSSC) ? " PSSC" : "",
353                         (temp & RH_PS_PESC) ? " PESC" : "",
354                         (temp & RH_PS_CSC) ? " CSC" : "",
355
356                         (temp & RH_PS_LSDA) ? " LSDA" : "",
357                         (temp & RH_PS_PPS) ? " PPS" : "",
358                         (temp & RH_PS_PRS) ? " PRS" : "",
359                         (temp & RH_PS_POCI) ? " POCI" : "",
360                         (temp & RH_PS_PSS) ? " PSS" : "",
361
362                         (temp & RH_PS_PES) ? " PES" : "",
363                         (temp & RH_PS_CCS) ? " CCS" : ""
364                         );
365         }
366 }
367
368 static void ohci_dump (ohci_t *controller, int verbose)
369 {
370         dbg ("OHCI controller usb-%s state", controller->slot_name);
371
372         /* dumps some of the state we know about */
373         ohci_dump_status (controller);
374         if (verbose)
375                 ep_print_int_eds (controller, "hcca");
376         dbg ("hcca frame #%04x", controller->hcca->frame_no);
377         ohci_dump_roothub (controller, 1);
378 }
379
380
381 #endif /* DEBUG */
382
383 /*-------------------------------------------------------------------------*
384  * Interface functions (URB)
385  *-------------------------------------------------------------------------*/
386
387 /* get a transfer request */
388
389 int sohci_submit_job(struct usb_device *dev, unsigned long pipe, void *buffer,
390                 int transfer_len, struct devrequest *setup, int interval)
391 {
392         ohci_t *ohci;
393         ed_t * ed;
394         urb_priv_t *purb_priv;
395         int i, size = 0;
396
397         ohci = &gohci;
398
399         /* when controller's hung, permit only roothub cleanup attempts
400          * such as powering down ports */
401         if (ohci->disabled) {
402                 err("sohci_submit_job: EPIPE");
403                 return -1;
404         }
405
406         /* every endpoint has a ed, locate and fill it */
407         if (!(ed = ep_add_ed (dev, pipe))) {
408                 err("sohci_submit_job: ENOMEM");
409                 return -1;
410         }
411
412         /* for the private part of the URB we need the number of TDs (size) */
413         switch (usb_pipetype (pipe)) {
414                 case PIPE_BULK: /* one TD for every 4096 Byte */
415                         size = (transfer_len - 1) / 4096 + 1;
416                         break;
417                 case PIPE_CONTROL: /* 1 TD for setup, 1 for ACK and 1 for every 4096 B */
418                         size = (transfer_len == 0)? 2:
419                                                 (transfer_len - 1) / 4096 + 3;
420                         break;
421         }
422
423         if (size >= (N_URB_TD - 1)) {
424                 err("need %d TDs, only have %d", size, N_URB_TD);
425                 return -1;
426         }
427         purb_priv = &urb_priv;
428         purb_priv->pipe = pipe;
429
430         /* fill the private part of the URB */
431         purb_priv->length = size;
432         purb_priv->ed = ed;
433         purb_priv->actual_length = 0;
434
435         /* allocate the TDs */
436         /* note that td[0] was allocated in ep_add_ed */
437         for (i = 0; i < size; i++) {
438                 purb_priv->td[i] = td_alloc (dev);
439                 if (!purb_priv->td[i]) {
440                         purb_priv->length = i;
441                         urb_free_priv (purb_priv);
442                         err("sohci_submit_job: ENOMEM");
443                         return -1;
444                 }
445         }
446
447         if (ed->state == ED_NEW || (ed->state & ED_DEL)) {
448                 urb_free_priv (purb_priv);
449                 err("sohci_submit_job: EINVAL");
450                 return -1;
451         }
452
453         /* link the ed into a chain if is not already */
454         if (ed->state != ED_OPER)
455                 ep_link (ohci, ed);
456
457         /* fill the TDs and link it to the ed */
458         td_submit_job(dev, pipe, buffer, transfer_len, setup, purb_priv, interval);
459
460         return 0;
461 }
462
463 /*-------------------------------------------------------------------------*/
464
465 #ifdef DEBUG
466 /* tell us the current USB frame number */
467
468 static int sohci_get_current_frame_number (struct usb_device *usb_dev)
469 {
470         ohci_t *ohci = &gohci;
471
472         return m16_swap (ohci->hcca->frame_no);
473 }
474 #endif
475
476 /*-------------------------------------------------------------------------*
477  * ED handling functions
478  *-------------------------------------------------------------------------*/
479
480 /* link an ed into one of the HC chains */
481
482 static int ep_link (ohci_t *ohci, ed_t *edi)
483 {
484         volatile ed_t *ed = edi;
485
486         ed->state = ED_OPER;
487
488         switch (ed->type) {
489         case PIPE_CONTROL:
490                 ed->hwNextED = 0;
491                 if (ohci->ed_controltail == NULL) {
492                         writel (ed, &ohci->regs->ed_controlhead);
493                 } else {
494                         ohci->ed_controltail->hwNextED = m32_swap (ed);
495                 }
496                 ed->ed_prev = ohci->ed_controltail;
497                 if (!ohci->ed_controltail && !ohci->ed_rm_list[0] &&
498                         !ohci->ed_rm_list[1] && !ohci->sleeping) {
499                         ohci->hc_control |= OHCI_CTRL_CLE;
500                         writel (ohci->hc_control, &ohci->regs->control);
501                 }
502                 ohci->ed_controltail = edi;
503                 break;
504
505         case PIPE_BULK:
506                 ed->hwNextED = 0;
507                 if (ohci->ed_bulktail == NULL) {
508                         writel (ed, &ohci->regs->ed_bulkhead);
509                 } else {
510                         ohci->ed_bulktail->hwNextED = m32_swap (ed);
511                 }
512                 ed->ed_prev = ohci->ed_bulktail;
513                 if (!ohci->ed_bulktail && !ohci->ed_rm_list[0] &&
514                         !ohci->ed_rm_list[1] && !ohci->sleeping) {
515                         ohci->hc_control |= OHCI_CTRL_BLE;
516                         writel (ohci->hc_control, &ohci->regs->control);
517                 }
518                 ohci->ed_bulktail = edi;
519                 break;
520         }
521         return 0;
522 }
523
524 /*-------------------------------------------------------------------------*/
525
526 /* unlink an ed from one of the HC chains.
527  * just the link to the ed is unlinked.
528  * the link from the ed still points to another operational ed or 0
529  * so the HC can eventually finish the processing of the unlinked ed */
530
531 static int ep_unlink (ohci_t *ohci, ed_t *ed)
532 {
533         ed->hwINFO |= m32_swap (OHCI_ED_SKIP);
534
535         switch (ed->type) {
536         case PIPE_CONTROL:
537                 if (ed->ed_prev == NULL) {
538                         if (!ed->hwNextED) {
539                                 ohci->hc_control &= ~OHCI_CTRL_CLE;
540                                 writel (ohci->hc_control, &ohci->regs->control);
541                         }
542                         writel (m32_swap (*((__u32 *)&ed->hwNextED)), &ohci->regs->ed_controlhead);
543                 } else {
544                         ed->ed_prev->hwNextED = ed->hwNextED;
545                 }
546                 if (ohci->ed_controltail == ed) {
547                         ohci->ed_controltail = ed->ed_prev;
548                 } else {
549                         ((ed_t *)m32_swap (*((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
550                 }
551                 break;
552
553         case PIPE_BULK:
554                 if (ed->ed_prev == NULL) {
555                         if (!ed->hwNextED) {
556                                 ohci->hc_control &= ~OHCI_CTRL_BLE;
557                                 writel (ohci->hc_control, &ohci->regs->control);
558                         }
559                         writel (m32_swap (*((__u32 *)&ed->hwNextED)), &ohci->regs->ed_bulkhead);
560                 } else {
561                         ed->ed_prev->hwNextED = ed->hwNextED;
562                 }
563                 if (ohci->ed_bulktail == ed) {
564                         ohci->ed_bulktail = ed->ed_prev;
565                 } else {
566                         ((ed_t *)m32_swap (*((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
567                 }
568                 break;
569         }
570         ed->state = ED_UNLINK;
571         return 0;
572 }
573
574
575 /*-------------------------------------------------------------------------*/
576
577 /* add/reinit an endpoint; this should be done once at the usb_set_configuration command,
578  * but the USB stack is a little bit stateless  so we do it at every transaction
579  * if the state of the ed is ED_NEW then a dummy td is added and the state is changed to ED_UNLINK
580  * in all other cases the state is left unchanged
581  * the ed info fields are setted anyway even though most of them should not change */
582
583 static ed_t * ep_add_ed (struct usb_device *usb_dev, unsigned long pipe)
584 {
585         td_t *td;
586         ed_t *ed_ret;
587         volatile ed_t *ed;
588
589         ed = ed_ret = &ohci_dev.ed[(usb_pipeendpoint (pipe) << 1) |
590                         (usb_pipecontrol (pipe)? 0: usb_pipeout (pipe))];
591
592         if ((ed->state & ED_DEL) || (ed->state & ED_URB_DEL)) {
593                 err("ep_add_ed: pending delete");
594                 /* pending delete request */
595                 return NULL;
596         }
597
598         if (ed->state == ED_NEW) {
599                 ed->hwINFO = m32_swap (OHCI_ED_SKIP); /* skip ed */
600                 /* dummy td; end of td list for ed */
601                 td = td_alloc (usb_dev);
602                 ed->hwTailP = m32_swap (td);
603                 ed->hwHeadP = ed->hwTailP;
604                 ed->state = ED_UNLINK;
605                 ed->type = usb_pipetype (pipe);
606                 ohci_dev.ed_cnt++;
607         }
608
609         ed->hwINFO = m32_swap (usb_pipedevice (pipe)
610                         | usb_pipeendpoint (pipe) << 7
611                         | (usb_pipeisoc (pipe)? 0x8000: 0)
612                         | (usb_pipecontrol (pipe)? 0: (usb_pipeout (pipe)? 0x800: 0x1000))
613                         | usb_pipeslow (pipe) << 13
614                         | usb_maxpacket (usb_dev, pipe) << 16);
615
616         return ed_ret;
617 }
618
619 /*-------------------------------------------------------------------------*
620  * TD handling functions
621  *-------------------------------------------------------------------------*/
622
623 /* enqueue next TD for this URB (OHCI spec 5.2.8.2) */
624
625 static void td_fill (ohci_t *ohci, unsigned int info,
626         void *data, int len,
627         struct usb_device *dev, int index, urb_priv_t *urb_priv)
628 {
629         volatile td_t  *td, *td_pt;
630 #ifdef OHCI_FILL_TRACE
631         int i;
632 #endif
633
634         if (index > urb_priv->length) {
635                 err("index > length");
636                 return;
637         }
638         /* use this td as the next dummy */
639         td_pt = urb_priv->td [index];
640         td_pt->hwNextTD = 0;
641
642         /* fill the old dummy TD */
643         td = urb_priv->td [index] = (td_t *)(m32_swap (urb_priv->ed->hwTailP) & ~0xf);
644
645         td->ed = urb_priv->ed;
646         td->next_dl_td = NULL;
647         td->index = index;
648         td->data = (__u32)data;
649 #ifdef OHCI_FILL_TRACE
650         if ((usb_pipetype(urb_priv->pipe) == PIPE_BULK) && usb_pipeout(urb_priv->pipe)) {
651                 for (i = 0; i < len; i++)
652                 printf("td->data[%d] %#2x ",i, ((unsigned char *)td->data)[i]);
653                 printf("\n");
654         }
655 #endif
656         if (!len)
657                 data = 0;
658
659         td->hwINFO = m32_swap (info);
660         td->hwCBP = m32_swap (data);
661         if (data)
662                 td->hwBE = m32_swap (data + len - 1);
663         else
664                 td->hwBE = 0;
665         td->hwNextTD = m32_swap (td_pt);
666         td->hwPSW [0] = m16_swap (((__u32)data & 0x0FFF) | 0xE000);
667
668         /* append to queue */
669         td->ed->hwTailP = td->hwNextTD;
670 }
671
672 /*-------------------------------------------------------------------------*/
673
674 /* prepare all TDs of a transfer */
675
676 static void td_submit_job (struct usb_device *dev, unsigned long pipe, void *buffer,
677         int transfer_len, struct devrequest *setup, urb_priv_t *urb, int interval)
678 {
679         ohci_t *ohci = &gohci;
680         int data_len = transfer_len;
681         void *data;
682         int cnt = 0;
683         __u32 info = 0;
684         unsigned int toggle = 0;
685
686         /* OHCI handles the DATA-toggles itself, we just use the USB-toggle bits for reseting */
687         if(usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe))) {
688                 toggle = TD_T_TOGGLE;
689         } else {
690                 toggle = TD_T_DATA0;
691                 usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), 1);
692         }
693         urb->td_cnt = 0;
694         if (data_len)
695                 data = buffer;
696         else
697                 data = 0;
698
699         switch (usb_pipetype (pipe)) {
700         case PIPE_BULK:
701                 info = usb_pipeout (pipe)?
702                         TD_CC | TD_DP_OUT : TD_CC | TD_DP_IN ;
703                 while(data_len > 4096) {
704                         td_fill (ohci, info | (cnt? TD_T_TOGGLE:toggle), data, 4096, dev, cnt, urb);
705                         data += 4096; data_len -= 4096; cnt++;
706                 }
707                 info = usb_pipeout (pipe)?
708                         TD_CC | TD_DP_OUT : TD_CC | TD_R | TD_DP_IN ;
709                 td_fill (ohci, info | (cnt? TD_T_TOGGLE:toggle), data, data_len, dev, cnt, urb);
710                 cnt++;
711
712                 if (!ohci->sleeping)
713                         writel (OHCI_BLF, &ohci->regs->cmdstatus); /* start bulk list */
714                 break;
715
716         case PIPE_CONTROL:
717                 info = TD_CC | TD_DP_SETUP | TD_T_DATA0;
718                 td_fill (ohci, info, setup, 8, dev, cnt++, urb);
719                 if (data_len > 0) {
720                         info = usb_pipeout (pipe)?
721                                 TD_CC | TD_R | TD_DP_OUT | TD_T_DATA1 : TD_CC | TD_R | TD_DP_IN | TD_T_DATA1;
722                         /* NOTE:  mishandles transfers >8K, some >4K */
723                         td_fill (ohci, info, data, data_len, dev, cnt++, urb);
724                 }
725                 info = usb_pipeout (pipe)?
726                         TD_CC | TD_DP_IN | TD_T_DATA1: TD_CC | TD_DP_OUT | TD_T_DATA1;
727                 td_fill (ohci, info, data, 0, dev, cnt++, urb);
728                 if (!ohci->sleeping)
729                         writel (OHCI_CLF, &ohci->regs->cmdstatus); /* start Control list */
730                 break;
731         }
732         if (urb->length != cnt)
733                 dbg("TD LENGTH %d != CNT %d", urb->length, cnt);
734 }
735
736 /*-------------------------------------------------------------------------*
737  * Done List handling functions
738  *-------------------------------------------------------------------------*/
739
740
741 /* calculate the transfer length and update the urb */
742
743 static void dl_transfer_length(td_t * td)
744 {
745         __u32 tdINFO, tdBE, tdCBP;
746         urb_priv_t *lurb_priv = &urb_priv;
747
748         tdINFO = m32_swap (td->hwINFO);
749         tdBE   = m32_swap (td->hwBE);
750         tdCBP  = m32_swap (td->hwCBP);
751
752
753         if (!(usb_pipetype (lurb_priv->pipe) == PIPE_CONTROL &&
754             ((td->index == 0) || (td->index == lurb_priv->length - 1)))) {
755                 if (tdBE != 0) {
756                         if (td->hwCBP == 0)
757                                 lurb_priv->actual_length += tdBE - td->data + 1;
758                         else
759                                 lurb_priv->actual_length += tdCBP - td->data;
760                 }
761         }
762 }
763
764 /*-------------------------------------------------------------------------*/
765
766 /* replies to the request have to be on a FIFO basis so
767  * we reverse the reversed done-list */
768
769 static td_t * dl_reverse_done_list (ohci_t *ohci)
770 {
771         __u32 td_list_hc;
772         td_t *td_rev = NULL;
773         td_t *td_list = NULL;
774         urb_priv_t *lurb_priv = NULL;
775
776         td_list_hc = m32_swap (ohci->hcca->done_head) & 0xfffffff0;
777         ohci->hcca->done_head = 0;
778
779         while (td_list_hc) {
780                 td_list = (td_t *)td_list_hc;
781
782                 if (TD_CC_GET (m32_swap (td_list->hwINFO))) {
783                         lurb_priv = &urb_priv;
784                         dbg(" USB-error/status: %x : %p",
785                                         TD_CC_GET (m32_swap (td_list->hwINFO)), td_list);
786                         if (td_list->ed->hwHeadP & m32_swap (0x1)) {
787                                 if (lurb_priv && ((td_list->index + 1) < lurb_priv->length)) {
788                                         td_list->ed->hwHeadP =
789                                                 (lurb_priv->td[lurb_priv->length - 1]->hwNextTD & m32_swap (0xfffffff0)) |
790                                                                         (td_list->ed->hwHeadP & m32_swap (0x2));
791                                         lurb_priv->td_cnt += lurb_priv->length - td_list->index - 1;
792                                 } else
793                                         td_list->ed->hwHeadP &= m32_swap (0xfffffff2);
794                         }
795                 }
796
797                 td_list->next_dl_td = td_rev;
798                 td_rev = td_list;
799                 td_list_hc = m32_swap (td_list->hwNextTD) & 0xfffffff0;
800         }
801         return td_list;
802 }
803
804 /*-------------------------------------------------------------------------*/
805
806 /* td done list */
807 static int dl_done_list (ohci_t *ohci, td_t *td_list)
808 {
809         td_t *td_list_next = NULL;
810         ed_t *ed;
811         int cc = 0;
812         int stat = 0;
813         /* urb_t *urb; */
814         urb_priv_t *lurb_priv;
815         __u32 tdINFO, edHeadP, edTailP;
816
817         while (td_list) {
818                 td_list_next = td_list->next_dl_td;
819
820                 lurb_priv = &urb_priv;
821                 tdINFO = m32_swap (td_list->hwINFO);
822
823                 ed = td_list->ed;
824
825                 dl_transfer_length(td_list);
826
827                 /* error code of transfer */
828                 cc = TD_CC_GET (tdINFO);
829                 if (cc != 0) {
830                         dbg("ConditionCode %#x", cc);
831                         stat = cc_to_error[cc];
832                 }
833
834                 if (ed->state != ED_NEW) {
835                         edHeadP = m32_swap (ed->hwHeadP) & 0xfffffff0;
836                         edTailP = m32_swap (ed->hwTailP);
837
838                         /* unlink eds if they are not busy */
839                         if ((edHeadP == edTailP) && (ed->state == ED_OPER))
840                                 ep_unlink (ohci, ed);
841                 }
842
843                 td_list = td_list_next;
844         }
845         return stat;
846 }
847
848 /*-------------------------------------------------------------------------*
849  * Virtual Root Hub
850  *-------------------------------------------------------------------------*/
851
852 /* Device descriptor */
853 static __u8 root_hub_dev_des[] =
854 {
855         0x12,       /*  __u8  bLength; */
856         0x01,       /*  __u8  bDescriptorType; Device */
857         0x10,       /*  __u16 bcdUSB; v1.1 */
858         0x01,
859         0x09,       /*  __u8  bDeviceClass; HUB_CLASSCODE */
860         0x00,       /*  __u8  bDeviceSubClass; */
861         0x00,       /*  __u8  bDeviceProtocol; */
862         0x08,       /*  __u8  bMaxPacketSize0; 8 Bytes */
863         0x00,       /*  __u16 idVendor; */
864         0x00,
865         0x00,       /*  __u16 idProduct; */
866         0x00,
867         0x00,       /*  __u16 bcdDevice; */
868         0x00,
869         0x00,       /*  __u8  iManufacturer; */
870         0x01,       /*  __u8  iProduct; */
871         0x00,       /*  __u8  iSerialNumber; */
872         0x01        /*  __u8  bNumConfigurations; */
873 };
874
875
876 /* Configuration descriptor */
877 static __u8 root_hub_config_des[] =
878 {
879         0x09,       /*  __u8  bLength; */
880         0x02,       /*  __u8  bDescriptorType; Configuration */
881         0x19,       /*  __u16 wTotalLength; */
882         0x00,
883         0x01,       /*  __u8  bNumInterfaces; */
884         0x01,       /*  __u8  bConfigurationValue; */
885         0x00,       /*  __u8  iConfiguration; */
886         0x40,       /*  __u8  bmAttributes;
887                  Bit 7: Bus-powered, 6: Self-powered, 5 Remote-wakwup, 4..0: resvd */
888         0x00,       /*  __u8  MaxPower; */
889
890         /* interface */
891         0x09,       /*  __u8  if_bLength; */
892         0x04,       /*  __u8  if_bDescriptorType; Interface */
893         0x00,       /*  __u8  if_bInterfaceNumber; */
894         0x00,       /*  __u8  if_bAlternateSetting; */
895         0x01,       /*  __u8  if_bNumEndpoints; */
896         0x09,       /*  __u8  if_bInterfaceClass; HUB_CLASSCODE */
897         0x00,       /*  __u8  if_bInterfaceSubClass; */
898         0x00,       /*  __u8  if_bInterfaceProtocol; */
899         0x00,       /*  __u8  if_iInterface; */
900
901         /* endpoint */
902         0x07,       /*  __u8  ep_bLength; */
903         0x05,       /*  __u8  ep_bDescriptorType; Endpoint */
904         0x81,       /*  __u8  ep_bEndpointAddress; IN Endpoint 1 */
905         0x03,       /*  __u8  ep_bmAttributes; Interrupt */
906         0x02,       /*  __u16 ep_wMaxPacketSize; ((MAX_ROOT_PORTS + 1) / 8 */
907         0x00,
908         0xff        /*  __u8  ep_bInterval; 255 ms */
909 };
910
911 static unsigned char root_hub_str_index0[] =
912 {
913         0x04,                   /*  __u8  bLength; */
914         0x03,                   /*  __u8  bDescriptorType; String-descriptor */
915         0x09,                   /*  __u8  lang ID */
916         0x04,                   /*  __u8  lang ID */
917 };
918
919 static unsigned char root_hub_str_index1[] =
920 {
921         28,                     /*  __u8  bLength; */
922         0x03,                   /*  __u8  bDescriptorType; String-descriptor */
923         'O',                    /*  __u8  Unicode */
924         0,                              /*  __u8  Unicode */
925         'H',                    /*  __u8  Unicode */
926         0,                              /*  __u8  Unicode */
927         'C',                    /*  __u8  Unicode */
928         0,                              /*  __u8  Unicode */
929         'I',                    /*  __u8  Unicode */
930         0,                              /*  __u8  Unicode */
931         ' ',                    /*  __u8  Unicode */
932         0,                              /*  __u8  Unicode */
933         'R',                    /*  __u8  Unicode */
934         0,                              /*  __u8  Unicode */
935         'o',                    /*  __u8  Unicode */
936         0,                              /*  __u8  Unicode */
937         'o',                    /*  __u8  Unicode */
938         0,                              /*  __u8  Unicode */
939         't',                    /*  __u8  Unicode */
940         0,                              /*  __u8  Unicode */
941         ' ',                    /*  __u8  Unicode */
942         0,                              /*  __u8  Unicode */
943         'H',                    /*  __u8  Unicode */
944         0,                              /*  __u8  Unicode */
945         'u',                    /*  __u8  Unicode */
946         0,                              /*  __u8  Unicode */
947         'b',                    /*  __u8  Unicode */
948         0,                              /*  __u8  Unicode */
949 };
950
951 /* Hub class-specific descriptor is constructed dynamically */
952
953
954 /*-------------------------------------------------------------------------*/
955
956 #define OK(x)                   len = (x); break
957 #ifdef DEBUG
958 #define WR_RH_STAT(x)           {info("WR:status %#8x", (x));writel((x), &gohci.regs->roothub.status);}
959 #define WR_RH_PORTSTAT(x)       {info("WR:portstatus[%d] %#8x", wIndex-1, (x));writel((x), &gohci.regs->roothub.portstatus[wIndex-1]);}
960 #else
961 #define WR_RH_STAT(x)           writel((x), &gohci.regs->roothub.status)
962 #define WR_RH_PORTSTAT(x)       writel((x), &gohci.regs->roothub.portstatus[wIndex-1])
963 #endif
964 #define RD_RH_STAT              roothub_status(&gohci)
965 #define RD_RH_PORTSTAT          roothub_portstatus(&gohci,wIndex-1)
966
967 /* request to virtual root hub */
968
969 int rh_check_port_status(ohci_t *controller)
970 {
971         __u32 temp, ndp, i;
972         int res;
973
974         res = -1;
975         temp = roothub_a (controller);
976         ndp = (temp & RH_A_NDP);
977 #ifdef CONFIG_AT91C_PQFP_UHPBUG
978         ndp = (ndp == 2) ? 1:0;
979 #endif
980         for (i = 0; i < ndp; i++) {
981                 temp = roothub_portstatus (controller, i);
982                 /* check for a device disconnect */
983                 if (((temp & (RH_PS_PESC | RH_PS_CSC)) ==
984                         (RH_PS_PESC | RH_PS_CSC)) &&
985                         ((temp & RH_PS_CCS) == 0)) {
986                         res = i;
987                         break;
988                 }
989         }
990         return res;
991 }
992
993 static int ohci_submit_rh_msg(struct usb_device *dev, unsigned long pipe,
994                 void *buffer, int transfer_len, struct devrequest *cmd)
995 {
996         void * data = buffer;
997         int leni = transfer_len;
998         int len = 0;
999         int stat = 0;
1000         __u32 datab[4];
1001         __u8 *data_buf = (__u8 *)datab;
1002         __u16 bmRType_bReq;
1003         __u16 wValue;
1004         __u16 wIndex;
1005         __u16 wLength;
1006
1007 #ifdef DEBUG
1008 urb_priv.actual_length = 0;
1009 pkt_print(dev, pipe, buffer, transfer_len, cmd, "SUB(rh)", usb_pipein(pipe));
1010 #else
1011         wait_ms(1);
1012 #endif
1013         if ((pipe & PIPE_INTERRUPT) == PIPE_INTERRUPT) {
1014                 info("Root-Hub submit IRQ: NOT implemented");
1015                 return 0;
1016         }
1017
1018         bmRType_bReq  = cmd->requesttype | (cmd->request << 8);
1019         wValue        = m16_swap (cmd->value);
1020         wIndex        = m16_swap (cmd->index);
1021         wLength       = m16_swap (cmd->length);
1022
1023         info("Root-Hub: adr: %2x cmd(%1x): %08x %04x %04x %04x",
1024                 dev->devnum, 8, bmRType_bReq, wValue, wIndex, wLength);
1025
1026         switch (bmRType_bReq) {
1027         /* Request Destination:
1028            without flags: Device,
1029            RH_INTERFACE: interface,
1030            RH_ENDPOINT: endpoint,
1031            RH_CLASS means HUB here,
1032            RH_OTHER | RH_CLASS  almost ever means HUB_PORT here
1033         */
1034
1035         case RH_GET_STATUS:
1036                         *(__u16 *) data_buf = m16_swap (1); OK (2);
1037         case RH_GET_STATUS | RH_INTERFACE:
1038                         *(__u16 *) data_buf = m16_swap (0); OK (2);
1039         case RH_GET_STATUS | RH_ENDPOINT:
1040                         *(__u16 *) data_buf = m16_swap (0); OK (2);
1041         case RH_GET_STATUS | RH_CLASS:
1042                         *(__u32 *) data_buf = m32_swap (
1043                                 RD_RH_STAT & ~(RH_HS_CRWE | RH_HS_DRWE));
1044                         OK (4);
1045         case RH_GET_STATUS | RH_OTHER | RH_CLASS:
1046                         *(__u32 *) data_buf = m32_swap (RD_RH_PORTSTAT); OK (4);
1047
1048         case RH_CLEAR_FEATURE | RH_ENDPOINT:
1049                 switch (wValue) {
1050                         case (RH_ENDPOINT_STALL): OK (0);
1051                 }
1052                 break;
1053
1054         case RH_CLEAR_FEATURE | RH_CLASS:
1055                 switch (wValue) {
1056                         case RH_C_HUB_LOCAL_POWER:
1057                                 OK(0);
1058                         case (RH_C_HUB_OVER_CURRENT):
1059                                         WR_RH_STAT(RH_HS_OCIC); OK (0);
1060                 }
1061                 break;
1062
1063         case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
1064                 switch (wValue) {
1065                         case (RH_PORT_ENABLE):
1066                                         WR_RH_PORTSTAT (RH_PS_CCS ); OK (0);
1067                         case (RH_PORT_SUSPEND):
1068                                         WR_RH_PORTSTAT (RH_PS_POCI); OK (0);
1069                         case (RH_PORT_POWER):
1070                                         WR_RH_PORTSTAT (RH_PS_LSDA); OK (0);
1071                         case (RH_C_PORT_CONNECTION):
1072                                         WR_RH_PORTSTAT (RH_PS_CSC ); OK (0);
1073                         case (RH_C_PORT_ENABLE):
1074                                         WR_RH_PORTSTAT (RH_PS_PESC); OK (0);
1075                         case (RH_C_PORT_SUSPEND):
1076                                         WR_RH_PORTSTAT (RH_PS_PSSC); OK (0);
1077                         case (RH_C_PORT_OVER_CURRENT):
1078                                         WR_RH_PORTSTAT (RH_PS_OCIC); OK (0);
1079                         case (RH_C_PORT_RESET):
1080                                         WR_RH_PORTSTAT (RH_PS_PRSC); OK (0);
1081                 }
1082                 break;
1083
1084         case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
1085                 switch (wValue) {
1086                         case (RH_PORT_SUSPEND):
1087                                         WR_RH_PORTSTAT (RH_PS_PSS ); OK (0);
1088                         case (RH_PORT_RESET): /* BUG IN HUP CODE *********/
1089                                         if (RD_RH_PORTSTAT & RH_PS_CCS)
1090                                             WR_RH_PORTSTAT (RH_PS_PRS);
1091                                         OK (0);
1092                         case (RH_PORT_POWER):
1093                                         WR_RH_PORTSTAT (RH_PS_PPS ); OK (0);
1094                         case (RH_PORT_ENABLE): /* BUG IN HUP CODE *********/
1095                                         if (RD_RH_PORTSTAT & RH_PS_CCS)
1096                                             WR_RH_PORTSTAT (RH_PS_PES );
1097                                         OK (0);
1098                 }
1099                 break;
1100
1101         case RH_SET_ADDRESS: gohci.rh.devnum = wValue; OK(0);
1102
1103         case RH_GET_DESCRIPTOR:
1104                 switch ((wValue & 0xff00) >> 8) {
1105                         case (0x01): /* device descriptor */
1106                                 len = min_t(unsigned int,
1107                                           leni,
1108                                           min_t(unsigned int,
1109                                               sizeof (root_hub_dev_des),
1110                                               wLength));
1111                                 data_buf = root_hub_dev_des; OK(len);
1112                         case (0x02): /* configuration descriptor */
1113                                 len = min_t(unsigned int,
1114                                           leni,
1115                                           min_t(unsigned int,
1116                                               sizeof (root_hub_config_des),
1117                                               wLength));
1118                                 data_buf = root_hub_config_des; OK(len);
1119                         case (0x03): /* string descriptors */
1120                                 if(wValue==0x0300) {
1121                                         len = min_t(unsigned int,
1122                                                   leni,
1123                                                   min_t(unsigned int,
1124                                                       sizeof (root_hub_str_index0),
1125                                                       wLength));
1126                                         data_buf = root_hub_str_index0;
1127                                         OK(len);
1128                                 }
1129                                 if(wValue==0x0301) {
1130                                         len = min_t(unsigned int,
1131                                                   leni,
1132                                                   min_t(unsigned int,
1133                                                       sizeof (root_hub_str_index1),
1134                                                       wLength));
1135                                         data_buf = root_hub_str_index1;
1136                                         OK(len);
1137                         }
1138                         default:
1139                                 stat = USB_ST_STALLED;
1140                 }
1141                 break;
1142
1143         case RH_GET_DESCRIPTOR | RH_CLASS:
1144         {
1145                 __u32 temp = roothub_a (&gohci);
1146
1147                 data_buf [0] = 9;               /* min length; */
1148                 data_buf [1] = 0x29;
1149                 data_buf [2] = temp & RH_A_NDP;
1150 #ifdef CONFIG_AT91C_PQFP_UHPBUG
1151                 data_buf [2] = (data_buf [2] == 2) ? 1:0;
1152 #endif
1153                 data_buf [3] = 0;
1154                 if (temp & RH_A_PSM)    /* per-port power switching? */
1155                         data_buf [3] |= 0x1;
1156                 if (temp & RH_A_NOCP)   /* no overcurrent reporting? */
1157                         data_buf [3] |= 0x10;
1158                 else if (temp & RH_A_OCPM)      /* per-port overcurrent reporting? */
1159                         data_buf [3] |= 0x8;
1160
1161                 /* corresponds to data_buf[4-7] */
1162                 datab [1] = 0;
1163                 data_buf [5] = (temp & RH_A_POTPGT) >> 24;
1164                 temp = roothub_b (&gohci);
1165                 data_buf [7] = temp & RH_B_DR;
1166                 if (data_buf [2] < 7) {
1167                         data_buf [8] = 0xff;
1168                 } else {
1169                         data_buf [0] += 2;
1170                         data_buf [8] = (temp & RH_B_DR) >> 8;
1171                         data_buf [10] = data_buf [9] = 0xff;
1172                 }
1173
1174                 len = min_t(unsigned int, leni,
1175                 min_t(unsigned int, data_buf [0], wLength));
1176                 OK (len);
1177         }
1178
1179         case RH_GET_CONFIGURATION:      *(__u8 *) data_buf = 0x01; OK (1);
1180
1181         case RH_SET_CONFIGURATION:      WR_RH_STAT (0x10000); OK (0);
1182
1183         default:
1184                 dbg ("unsupported root hub command");
1185                 stat = USB_ST_STALLED;
1186         }
1187
1188 #ifdef  DEBUG
1189         ohci_dump_roothub (&gohci, 1);
1190 #else
1191         wait_ms(1);
1192 #endif
1193
1194         len = min_t(int, len, leni);
1195         if (data != data_buf)
1196             memcpy (data, data_buf, len);
1197         dev->act_len = len;
1198         dev->status = stat;
1199
1200 #ifdef DEBUG
1201         if (transfer_len)
1202                 urb_priv.actual_length = transfer_len;
1203         pkt_print(dev, pipe, buffer, transfer_len, cmd, "RET(rh)", 0/*usb_pipein(pipe)*/);
1204 #else
1205         wait_ms(1);
1206 #endif
1207
1208         return stat;
1209 }
1210
1211 /*-------------------------------------------------------------------------*/
1212
1213 /* common code for handling submit messages - used for all but root hub */
1214 /* accesses. */
1215 int submit_common_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1216                 int transfer_len, struct devrequest *setup, int interval)
1217 {
1218         int stat = 0;
1219         int maxsize = usb_maxpacket(dev, pipe);
1220         int timeout;
1221
1222         /* device pulled? Shortcut the action. */
1223         if (devgone == dev) {
1224                 dev->status = USB_ST_CRC_ERR;
1225                 return 0;
1226         }
1227
1228 #ifdef DEBUG
1229         urb_priv.actual_length = 0;
1230         pkt_print(dev, pipe, buffer, transfer_len, setup, "SUB", usb_pipein(pipe));
1231 #else
1232         wait_ms(1);
1233 #endif
1234         if (!maxsize) {
1235                 err("submit_common_message: pipesize for pipe %lx is zero",
1236                         pipe);
1237                 return -1;
1238         }
1239
1240         if (sohci_submit_job(dev, pipe, buffer, transfer_len, setup, interval) < 0) {
1241                 err("sohci_submit_job failed");
1242                 return -1;
1243         }
1244
1245         wait_ms(10);
1246         /* ohci_dump_status(&gohci); */
1247
1248         /* allow more time for a BULK device to react - some are slow */
1249 #define BULK_TO  5000   /* timeout in milliseconds */
1250         if (usb_pipetype (pipe) == PIPE_BULK)
1251                 timeout = BULK_TO;
1252         else
1253                 timeout = 100;
1254
1255         /* wait for it to complete */
1256         for (;;) {
1257                 /* check whether the controller is done */
1258                 stat = hc_interrupt();
1259                 if (stat < 0) {
1260                         stat = USB_ST_CRC_ERR;
1261                         break;
1262                 }
1263                 if (stat >= 0 && stat != 0xff) {
1264                         /* 0xff is returned for an SF-interrupt */
1265                         break;
1266                 }
1267                 if (--timeout) {
1268                         wait_ms(1);
1269                 } else {
1270                         err("CTL:TIMEOUT ");
1271                         stat = USB_ST_CRC_ERR;
1272                         break;
1273                 }
1274         }
1275         /* we got an Root Hub Status Change interrupt */
1276         if (got_rhsc) {
1277 #ifdef DEBUG
1278                 ohci_dump_roothub (&gohci, 1);
1279 #endif
1280                 got_rhsc = 0;
1281                 /* abuse timeout */
1282                 timeout = rh_check_port_status(&gohci);
1283                 if (timeout >= 0) {
1284 #if 0 /* this does nothing useful, but leave it here in case that changes */
1285                         /* the called routine adds 1 to the passed value */
1286                         usb_hub_port_connect_change(gohci.rh.dev, timeout - 1);
1287 #endif
1288                         /*
1289                          * XXX
1290                          * This is potentially dangerous because it assumes
1291                          * that only one device is ever plugged in!
1292                          */
1293                         devgone = dev;
1294                 }
1295         }
1296
1297         dev->status = stat;
1298         dev->act_len = transfer_len;
1299
1300 #ifdef DEBUG
1301         pkt_print(dev, pipe, buffer, transfer_len, setup, "RET(ctlr)", usb_pipein(pipe));
1302 #else
1303         wait_ms(1);
1304 #endif
1305
1306         /* free TDs in urb_priv */
1307         urb_free_priv (&urb_priv);
1308         return 0;
1309 }
1310
1311 /* submit routines called from usb.c */
1312 int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1313                 int transfer_len)
1314 {
1315         info("submit_bulk_msg");
1316         return submit_common_msg(dev, pipe, buffer, transfer_len, NULL, 0);
1317 }
1318
1319 int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1320                 int transfer_len, struct devrequest *setup)
1321 {
1322         int maxsize = usb_maxpacket(dev, pipe);
1323
1324         info("submit_control_msg");
1325 #ifdef DEBUG
1326         urb_priv.actual_length = 0;
1327         pkt_print(dev, pipe, buffer, transfer_len, setup, "SUB", usb_pipein(pipe));
1328 #else
1329         wait_ms(1);
1330 #endif
1331         if (!maxsize) {
1332                 err("submit_control_message: pipesize for pipe %lx is zero",
1333                         pipe);
1334                 return -1;
1335         }
1336         if (((pipe >> 8) & 0x7f) == gohci.rh.devnum) {
1337                 gohci.rh.dev = dev;
1338                 /* root hub - redirect */
1339                 return ohci_submit_rh_msg(dev, pipe, buffer, transfer_len,
1340                         setup);
1341         }
1342
1343         return submit_common_msg(dev, pipe, buffer, transfer_len, setup, 0);
1344 }
1345
1346 int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1347                 int transfer_len, int interval)
1348 {
1349         info("submit_int_msg");
1350         return -1;
1351 }
1352
1353 /*-------------------------------------------------------------------------*
1354  * HC functions
1355  *-------------------------------------------------------------------------*/
1356
1357 /* reset the HC and BUS */
1358
1359 static int hc_reset (ohci_t *ohci)
1360 {
1361         int timeout = 30;
1362         int smm_timeout = 50; /* 0,5 sec */
1363
1364         dbg("%s\n", __FUNCTION__);
1365
1366         if (readl (&ohci->regs->control) & OHCI_CTRL_IR) { /* SMM owns the HC */
1367                 writel (OHCI_OCR, &ohci->regs->cmdstatus); /* request ownership */
1368                 info("USB HC TakeOver from SMM");
1369                 while (readl (&ohci->regs->control) & OHCI_CTRL_IR) {
1370                         wait_ms (10);
1371                         if (--smm_timeout == 0) {
1372                                 err("USB HC TakeOver failed!");
1373                                 return -1;
1374                         }
1375                 }
1376         }
1377
1378         /* Disable HC interrupts */
1379         writel (OHCI_INTR_MIE, &ohci->regs->intrdisable);
1380
1381         dbg("USB HC reset_hc usb-%s: ctrl = 0x%X ;\n",
1382                 ohci->slot_name,
1383                 readl(&ohci->regs->control));
1384
1385         /* Reset USB (needed by some controllers) */
1386         writel (0, &ohci->regs->control);
1387
1388         /* HC Reset requires max 10 us delay */
1389         writel (OHCI_HCR,  &ohci->regs->cmdstatus);
1390         while ((readl (&ohci->regs->cmdstatus) & OHCI_HCR) != 0) {
1391                 if (--timeout == 0) {
1392                         err("USB HC reset timed out!");
1393                         return -1;
1394                 }
1395                 udelay (1);
1396         }
1397         return 0;
1398 }
1399
1400 /*-------------------------------------------------------------------------*/
1401
1402 /* Start an OHCI controller, set the BUS operational
1403  * enable interrupts
1404  * connect the virtual root hub */
1405
1406 static int hc_start (ohci_t * ohci)
1407 {
1408         __u32 mask;
1409         unsigned int fminterval;
1410
1411         ohci->disabled = 1;
1412
1413         /* Tell the controller where the control and bulk lists are
1414          * The lists are empty now. */
1415
1416         writel (0, &ohci->regs->ed_controlhead);
1417         writel (0, &ohci->regs->ed_bulkhead);
1418
1419         writel ((__u32)ohci->hcca, &ohci->regs->hcca); /* a reset clears this */
1420
1421         fminterval = 0x2edf;
1422         writel ((fminterval * 9) / 10, &ohci->regs->periodicstart);
1423         fminterval |= ((((fminterval - 210) * 6) / 7) << 16);
1424         writel (fminterval, &ohci->regs->fminterval);
1425         writel (0x628, &ohci->regs->lsthresh);
1426
1427         /* start controller operations */
1428         ohci->hc_control = OHCI_CONTROL_INIT | OHCI_USB_OPER;
1429         ohci->disabled = 0;
1430         writel (ohci->hc_control, &ohci->regs->control);
1431
1432         /* disable all interrupts */
1433         mask = (OHCI_INTR_SO | OHCI_INTR_WDH | OHCI_INTR_SF | OHCI_INTR_RD |
1434                         OHCI_INTR_UE | OHCI_INTR_FNO | OHCI_INTR_RHSC |
1435                         OHCI_INTR_OC | OHCI_INTR_MIE);
1436         writel (mask, &ohci->regs->intrdisable);
1437         /* clear all interrupts */
1438         mask &= ~OHCI_INTR_MIE;
1439         writel (mask, &ohci->regs->intrstatus);
1440         /* Choose the interrupts we care about now  - but w/o MIE */
1441         mask = OHCI_INTR_RHSC | OHCI_INTR_UE | OHCI_INTR_WDH | OHCI_INTR_SO;
1442         writel (mask, &ohci->regs->intrenable);
1443
1444 #ifdef  OHCI_USE_NPS
1445         /* required for AMD-756 and some Mac platforms */
1446         writel ((roothub_a (ohci) | RH_A_NPS) & ~RH_A_PSM,
1447                 &ohci->regs->roothub.a);
1448         writel (RH_HS_LPSC, &ohci->regs->roothub.status);
1449 #endif  /* OHCI_USE_NPS */
1450
1451 #define mdelay(n) ({unsigned long msec=(n); while (msec--) udelay(1000);})
1452         /* POTPGT delay is bits 24-31, in 2 ms units. */
1453         mdelay ((roothub_a (ohci) >> 23) & 0x1fe);
1454
1455         /* connect the virtual root hub */
1456         ohci->rh.devnum = 0;
1457
1458         return 0;
1459 }
1460
1461 /*-------------------------------------------------------------------------*/
1462
1463 /* an interrupt happens */
1464
1465 static int
1466 hc_interrupt (void)
1467 {
1468         ohci_t *ohci = &gohci;
1469         struct ohci_regs *regs = ohci->regs;
1470         int ints;
1471         int stat = -1;
1472
1473         if ((ohci->hcca->done_head != 0) && !(m32_swap (ohci->hcca->done_head) & 0x01)) {
1474                 ints =  OHCI_INTR_WDH;
1475         } else {
1476                 ints = readl (&regs->intrstatus);
1477         }
1478
1479         /* dbg("Interrupt: %x frame: %x", ints, le16_to_cpu (ohci->hcca->frame_no)); */
1480
1481         if (ints & OHCI_INTR_RHSC) {
1482                 got_rhsc = 1;
1483         }
1484
1485         if (ints & OHCI_INTR_UE) {
1486                 ohci->disabled++;
1487                 err ("OHCI Unrecoverable Error, controller usb-%s disabled",
1488                         ohci->slot_name);
1489                 /* e.g. due to PCI Master/Target Abort */
1490
1491 #ifdef  DEBUG
1492                 ohci_dump (ohci, 1);
1493 #else
1494         wait_ms(1);
1495 #endif
1496                 /* FIXME: be optimistic, hope that bug won't repeat often. */
1497                 /* Make some non-interrupt context restart the controller. */
1498                 /* Count and limit the retries though; either hardware or */
1499                 /* software errors can go forever... */
1500                 hc_reset (ohci);
1501                 return -1;
1502         }
1503
1504         if (ints & OHCI_INTR_WDH) {
1505                 wait_ms(1);
1506                 writel (OHCI_INTR_WDH, &regs->intrdisable);
1507                 stat = dl_done_list (&gohci, dl_reverse_done_list (&gohci));
1508                 writel (OHCI_INTR_WDH, &regs->intrenable);
1509         }
1510
1511         if (ints & OHCI_INTR_SO) {
1512                 dbg("USB Schedule overrun\n");
1513                 writel (OHCI_INTR_SO, &regs->intrenable);
1514                 stat = -1;
1515         }
1516
1517         /* FIXME:  this assumes SOF (1/ms) interrupts don't get lost... */
1518         if (ints & OHCI_INTR_SF) {
1519                 unsigned int frame = m16_swap (ohci->hcca->frame_no) & 1;
1520                 wait_ms(1);
1521                 writel (OHCI_INTR_SF, &regs->intrdisable);
1522                 if (ohci->ed_rm_list[frame] != NULL)
1523                         writel (OHCI_INTR_SF, &regs->intrenable);
1524                 stat = 0xff;
1525         }
1526
1527         writel (ints, &regs->intrstatus);
1528         return stat;
1529 }
1530
1531 /*-------------------------------------------------------------------------*/
1532
1533 /*-------------------------------------------------------------------------*/
1534
1535 /* De-allocate all resources.. */
1536
1537 static void hc_release_ohci (ohci_t *ohci)
1538 {
1539         dbg ("USB HC release ohci usb-%s", ohci->slot_name);
1540
1541         if (!ohci->disabled)
1542                 hc_reset (ohci);
1543 }
1544
1545 /*-------------------------------------------------------------------------*/
1546
1547 /*
1548  * low level initalisation routine, called from usb.c
1549  */
1550 static char ohci_inited = 0;
1551
1552 int usb_lowlevel_init(void)
1553 {
1554
1555 #if CFG_USB_CPU_INIT
1556         /* cpu dependant init */
1557         if(usb_cpu_init())
1558                 return -1;
1559 #endif
1560
1561 #if CFG_USB_BOARD_INIT
1562         /*  board dependant init */
1563         if(usb_board_init())
1564                 return -1;
1565 #endif
1566         memset (&gohci, 0, sizeof (ohci_t));
1567         memset (&urb_priv, 0, sizeof (urb_priv_t));
1568
1569         /* align the storage */
1570         if ((__u32)&ghcca[0] & 0xff) {
1571                 err("HCCA not aligned!!");
1572                 return -1;
1573         }
1574         phcca = &ghcca[0];
1575         info("aligned ghcca %p", phcca);
1576         memset(&ohci_dev, 0, sizeof(struct ohci_device));
1577         if ((__u32)&ohci_dev.ed[0] & 0x7) {
1578                 err("EDs not aligned!!");
1579                 return -1;
1580         }
1581         memset(gtd, 0, sizeof(td_t) * (NUM_TD + 1));
1582         if ((__u32)gtd & 0x7) {
1583                 err("TDs not aligned!!");
1584                 return -1;
1585         }
1586         ptd = gtd;
1587         gohci.hcca = phcca;
1588         memset (phcca, 0, sizeof (struct ohci_hcca));
1589
1590         gohci.disabled = 1;
1591         gohci.sleeping = 0;
1592         gohci.irq = -1;
1593         gohci.regs = (struct ohci_regs *)CFG_USB_OHCI_REGS_BASE;
1594
1595         gohci.flags = 0;
1596         gohci.slot_name = CFG_USB_SLOT_NAME;
1597
1598         if (hc_reset (&gohci) < 0) {
1599                 hc_release_ohci (&gohci);
1600                 err ("can't reset usb-%s", gohci.slot_name);
1601                 /* Initialization failed disable clocks */
1602 #if CFG_USB_BOARD_INIT
1603                 /* board dependant cleanup */
1604                 usb_board_stop();
1605 #endif
1606
1607 #if CFG_USB_CPU_INIT
1608                 /* cpu dependant cleanup */
1609                 usb_cpu_stop();
1610 #endif
1611                 return -1;
1612         }
1613
1614         /* FIXME this is a second HC reset; why?? */
1615         /* writel(gohci.hc_control = OHCI_USB_RESET, &gohci.regs->control);
1616            wait_ms(10); */
1617         if (hc_start (&gohci) < 0) {
1618                 err ("can't start usb-%s", gohci.slot_name);
1619                 hc_release_ohci (&gohci);
1620                 /* Initialization failed */
1621 #if CFG_USB_BOARD_INIT
1622                 /* board dependant cleanup */
1623                 usb_board_stop();
1624 #endif
1625
1626 #if CFG_USB_CPU_INIT
1627                 /* cpu dependant cleanup */
1628                 usb_cpu_stop();
1629 #endif
1630                 return -1;
1631         }
1632
1633 #ifdef  DEBUG
1634         ohci_dump (&gohci, 1);
1635 #else
1636         wait_ms(1);
1637 #endif
1638         ohci_inited = 1;
1639         return 0;
1640 }
1641
1642 int usb_lowlevel_stop(void)
1643 {
1644         /* this gets called really early - before the controller has */
1645         /* even been initialized! */
1646         if (!ohci_inited)
1647                 return 0;
1648         /* TODO release any interrupts, etc. */
1649         /* call hc_release_ohci() here ? */
1650         hc_reset (&gohci);
1651
1652 #if CFG_USB_BOARD_INIT
1653         /* board dependant cleanup */
1654         if(usb_board_stop())
1655                 return -1;
1656 #endif
1657
1658 #if CFG_USB_CPU_INIT
1659         /* cpu dependant cleanup */
1660         if(usb_cpu_stop())
1661                 return -1;
1662 #endif
1663
1664         return 0;
1665 }
1666
1667 #endif /* CONFIG_USB_OHCI */