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