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