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