[adm5120] USB driver fixes
[openwrt.git] / target / linux / adm5120-2.6 / files / drivers / usb / host / adm5120-hcd.c
1 /*
2  *      HCD driver for ADM5120 SoC
3  *
4  *      Copyright (C) 2005 Jeroen Vreeken (pe1rxq@amsat.org)
5  *
6  *      Based on the ADMtek 2.4 driver
7  *      (C) Copyright 2003 Junius Chen <juniusc@admtek.com.tw>
8  *      Which again was based on the ohci and uhci drivers.
9  */
10
11 #include <linux/module.h>
12 #include <linux/delay.h>
13 #include <linux/debugfs.h>
14 #include <linux/seq_file.h>
15 #include <linux/errno.h>
16 #include <linux/init.h>
17 #include <linux/list.h>
18 #include <linux/usb.h>
19 #include <linux/platform_device.h>
20
21 #include <asm/bootinfo.h>
22 #include <asm/io.h>
23 #include <asm/irq.h>
24 #include <asm/system.h>
25 #include <asm/byteorder.h>
26 #include <asm/mach-adm5120/adm5120_info.h>
27
28 #include "../core/hcd.h"
29
30 MODULE_DESCRIPTION("ADM5120 USB Host Controller Driver");
31 MODULE_LICENSE("GPL");
32 MODULE_AUTHOR("Jeroen Vreeken (pe1rxq@amsat.org)");
33
34 #define PFX     "adm5120-hcd: "
35
36 #define ADMHCD_REG_CONTROL              0x00
37 #define  ADMHCD_SW_RESET                0x00000008      /* Reset */
38 #define  ADMHCD_DMAA                    0x00000004      /* DMA arbitration control */
39 #define  ADMHCD_SW_INTREQ               0x00000002      /* request software int */
40 #define  ADMHCD_HOST_EN                 0x00000001      /* Host enable */
41 #define ADMHCD_REG_INTSTATUS            0x04
42 #define  ADMHCD_INT_ACT                 0x80000000      /* Interrupt active */
43 #define  ADMHCD_INT_FATAL               0x40000000      /* Fatal interrupt */
44 #define  ADMHCD_INT_SW                  0x20000000      /* software interrupt */
45 #define  ADMHCD_INT_TD                  0x00100000      /* TD completed */
46 #define  ADMHCD_FNO                     0x00000800      /* Frame number overaflow */
47 #define  ADMHCD_SO                      0x00000400      /* Scheduling overrun */
48 #define  ADMHCD_INSMI                   0x00000200      /* Root hub status change */
49 #define  ADMHCD_BABI                    0x00000100      /* Babble detected, host mode */
50 #define  ADMHCD_RESI                    0x00000020      /* Resume detected */
51 #define  ADMHCD_SOFI                    0x00000010      /* SOF transmitted/received, host mode */
52 #define ADMHCD_REG_INTENABLE            0x08
53 #define  ADMHCD_INT_EN                  0x80000000      /* Interrupt enable */
54 #define  ADMHCD_INTMASK                 0x00000001      /* Interrupt mask */
55 #define ADMHCD_REG_HOSTCONTROL          0x10
56 #define  ADMHCD_DMA_EN                  0x00000004      /* USB host DMA enable */
57 #define  ADMHCD_STATE_RST               0x00000000      /* bus state reset */
58 #define  ADMHCD_STATE_RES               0x00000001      /* bus state resume */
59 #define  ADMHCD_STATE_OP                0x00000002      /* bus state operational */
60 #define  ADMHCD_STATE_SUS               0x00000003      /* bus state suspended */
61 #define ADMHCD_REG_FMINTERVAL           0x18
62 #define ADMHCD_REG_FMNUMBER             0x1c
63 #define ADMHCD_REG_LSTHRESH             0x70
64 #define ADMHCD_REG_RHDESCR              0x74
65 #define  ADMHCD_CRWE                    0x20000000      /* Clear wakeup enable */
66 #define  ADMHCD_DRWE                    0x10000000      /* Device remote wakeup enable */
67 #define  ADMHCD_HW_OCIC                 0x08000000      /* Over current indication change */
68 #define  ADMHCD_LPSC                    0x04000000      /* Local power switch change */
69 #define  ADMHCD_OCI                     0x02000000      /* Over current indication */
70 #define  ADMHCD_LPS                     0x01000000      /* Local power switch/global power switch */
71 #define  ADMHCD_NOCP                    0x00000800      /* No over current protect mode */
72 #define  ADMHCD_OPCM                    0x00000400      /* Over current protect mode */
73 #define  ADMHCD_NPS                     0x00000200      /* No Power Switch */
74 #define  ADMHCD_PSM                     0x00000100      /* Power switch mode */
75 #define ADMHCD_REG_PORTSTATUS0          0x78
76 #define  ADMHCD_CCS                     0x00000001      /* current connect status */
77 #define  ADMHCD_PES                     0x00000002      /* port enable status */
78 #define  ADMHCD_PSS                     0x00000004      /* port suspend status */
79 #define  ADMHCD_POCI                    0x00000008      /* port overcurrent indicator */
80 #define  ADMHCD_PRS                     0x00000010      /* port reset status */
81 #define  ADMHCD_PPS                     0x00000100      /* port power status */
82 #define  ADMHCD_LSDA                    0x00000200      /* low speed device attached */
83 #define  ADMHCD_CSC                     0x00010000      /* connect status change */
84 #define  ADMHCD_PESC                    0x00020000      /* enable status change */
85 #define  ADMHCD_PSSC                    0x00040000      /* suspend status change */
86 #define  ADMHCD_OCIC                    0x00080000      /* overcurrent change*/
87 #define  ADMHCD_PRSC                    0x00100000      /* reset status change */
88 #define ADMHCD_REG_PORTSTATUS1          0x7c
89 #define ADMHCD_REG_HOSTHEAD             0x80
90
91
92 #define ADMHCD_NUMPORTS         2
93
94 struct admhcd_ed {
95         /* Don't change first four, they used for DMA */
96         u32                             control;
97         struct admhcd_td                *tail;
98         struct admhcd_td                *head;
99         struct admhcd_ed                *next;
100         /* the rest is for the driver only: */
101         struct admhcd_td                *cur;
102         struct usb_host_endpoint        *ep;
103         struct urb                      *urb;
104         struct admhcd_ed                *real;
105 } __attribute__ ((packed));
106
107 #define ADMHCD_ED_EPSHIFT       7               /* Shift for endpoint number */
108 #define ADMHCD_ED_INT           0x00000800      /* Is this an int endpoint */
109 #define ADMHCD_ED_SPEED         0x00002000      /* Is it a high speed dev? */
110 #define ADMHCD_ED_SKIP          0x00004000      /* Skip this ED */
111 #define ADMHCD_ED_FORMAT        0x00008000      /* Is this an isoc endpoint */
112 #define ADMHCD_ED_MAXSHIFT      16              /* Shift for max packet size */
113
114 struct admhcd_td {
115         /* Don't change first four, they are used for DMA */
116         u32                     control;
117         u32                     buffer;
118         u32                     buflen;
119         struct admhcd_td        *next;
120         /* the rest is for the driver only: */
121         struct urb              *urb;
122         struct admhcd_td        *real;
123 } __attribute__ ((packed));
124
125 #define ADMHCD_TD_OWN           0x80000000
126 #define ADMHCD_TD_TOGGLE        0x00000000
127 #define ADMHCD_TD_DATA0         0x01000000
128 #define ADMHCD_TD_DATA1         0x01800000
129 #define ADMHCD_TD_OUT           0x00200000
130 #define ADMHCD_TD_IN            0x00400000
131 #define ADMHCD_TD_SETUP         0x00000000
132 #define ADMHCD_TD_ISO           0x00010000
133 #define ADMHCD_TD_R             0x00040000
134 #define ADMHCD_TD_INTEN         0x00010000
135
136 static int admhcd_td_err[16] = {
137         0,              /* No */
138         -EREMOTEIO,             /* CRC */
139         -EREMOTEIO,     /* bit stuff */
140         -EREMOTEIO,             /* data toggle */
141         -EPIPE,         /* stall */
142         -ETIMEDOUT,     /* timeout */
143         -EPROTO,        /* pid err */
144         -EPROTO,        /* unexpected pid */
145         -EREMOTEIO,     /* data overrun */
146         -EREMOTEIO,     /* data underrun */
147         -ETIMEDOUT,     /* 1010 */
148         -ETIMEDOUT,     /* 1011 */
149         -EREMOTEIO,     /* buffer overrun */
150         -EREMOTEIO,     /* buffer underrun */
151         -ETIMEDOUT,     /* 1110 */
152         -ETIMEDOUT,     /* 1111 */
153 };
154
155 #define ADMHCD_TD_ERRMASK       0x38000000
156 #define ADMHCD_TD_ERRSHIFT      27
157
158 #define TD(td)  ((struct admhcd_td *)(((u32)(td)) & ~0xf))
159 #define ED(ed)  ((struct admhcd_ed *)(((u32)(ed)) & ~0xf))
160
161 struct admhcd {
162         spinlock_t      lock;
163
164         void __iomem *data_reg;
165         /* Root hub registers */
166         u32 rhdesca;
167         u32 rhdescb;
168         u32 rhstatus;
169         u32 rhport[2];
170
171         /* async schedule: control, bulk */
172         struct list_head async;
173         u32             base;
174         u32             dma_en;
175         unsigned long   flags;
176
177 };
178
179 static inline struct admhcd *hcd_to_admhcd(struct usb_hcd *hcd)
180 {
181         return (struct admhcd *)(hcd->hcd_priv);
182 }
183
184 static inline struct usb_hcd *admhcd_to_hcd(struct admhcd *admhcd)
185 {
186         return container_of((void *)admhcd, struct usb_hcd, hcd_priv);
187 }
188
189 static char hcd_name[] = "adm5120-hcd";
190
191 static u32 admhcd_reg_get(struct admhcd *ahcd, int reg)
192 {
193         return *(volatile u32 *)KSEG1ADDR(ahcd->base+reg);
194 }
195
196 static void admhcd_reg_set(struct admhcd *ahcd, int reg, u32 val)
197 {
198         *(volatile u32 *)KSEG1ADDR(ahcd->base+reg) = val;
199 }
200
201 static void admhcd_lock(struct admhcd *ahcd)
202 {
203         spin_lock_irqsave(&ahcd->lock, ahcd->flags);
204         ahcd->dma_en = admhcd_reg_get(ahcd, ADMHCD_REG_HOSTCONTROL) &
205             ADMHCD_DMA_EN;
206         admhcd_reg_set(ahcd, ADMHCD_REG_HOSTCONTROL, ADMHCD_STATE_OP);
207 }
208
209 static void admhcd_unlock(struct admhcd *ahcd)
210 {
211         admhcd_reg_set(ahcd, ADMHCD_REG_HOSTCONTROL,
212             ADMHCD_STATE_OP | ahcd->dma_en);
213         spin_unlock_irqrestore(&ahcd->lock, ahcd->flags);
214 }
215
216 static struct admhcd_td *admhcd_td_alloc(struct admhcd_ed *ed, struct urb *urb)
217 {
218         struct admhcd_td *tdn, *td;
219
220         tdn = kmalloc(sizeof(struct admhcd_td), GFP_ATOMIC);
221         if (!tdn)
222                 return NULL;
223         tdn->real = tdn;
224         tdn = (struct admhcd_td *)KSEG1ADDR(tdn);
225         memset(tdn, 0, sizeof(struct admhcd_td));
226         if (ed->cur == NULL) {
227                 ed->cur = tdn;
228                 ed->head = tdn;
229                 ed->tail = tdn;
230                 td = tdn;
231         } else {
232                 /* Supply back the old tail and link in new td as tail */
233                 td = TD(ed->tail);
234                 TD(ed->tail)->next = tdn;
235                 ed->tail = tdn;
236         }
237         td->urb = urb;
238
239         return td;
240 }
241
242 static void admhcd_td_free(struct admhcd_ed *ed, struct urb *urb)
243 {
244         struct admhcd_td *td, **tdp;
245
246         if (urb == NULL)
247                 ed->control |= ADMHCD_ED_SKIP;
248         tdp = &ed->cur;
249         td = ed->cur;
250         do {
251                 if (td->urb == urb)
252                         break;
253                 tdp = &td->next;
254                 td = TD(td->next);
255         } while (td);
256         while (td && td->urb == urb) {
257                 *tdp = TD(td->next);
258                 kfree(td->real);
259                 td = *tdp;
260         }
261 }
262
263 /* Find an endpoint's descriptor, if needed allocate a new one and link it
264    in the DMA chain
265  */
266 static struct admhcd_ed *admhcd_get_ed(struct admhcd *ahcd,
267     struct usb_host_endpoint *ep, struct urb *urb)
268 {
269         struct admhcd_ed *hosthead;
270         struct admhcd_ed *found = NULL, *ed = NULL;
271         unsigned int pipe = urb->pipe;
272
273         admhcd_lock(ahcd);
274         hosthead = (struct admhcd_ed *)admhcd_reg_get(ahcd, ADMHCD_REG_HOSTHEAD);
275         if (hosthead) {
276                 for (ed = hosthead;; ed = ED(ed->next)) {
277                         if (ed->ep == ep) {
278                                 found = ed;
279                                 break;
280                         }
281                         if (ED(ed->next) == hosthead)
282                                 break;
283                 }
284         }
285         if (!found) {
286                 found = kmalloc(sizeof(struct admhcd_ed), GFP_ATOMIC);
287                 if (!found)
288                         goto out;
289                 memset(found, 0, sizeof(struct admhcd_ed));
290                 found->real = found;
291                 found->ep = ep;
292                 found = (struct admhcd_ed *)KSEG1ADDR(found);
293                 found->control = usb_pipedevice(pipe) |
294                     (usb_pipeendpoint(pipe) << ADMHCD_ED_EPSHIFT) |
295                     (usb_pipeint(pipe) ? ADMHCD_ED_INT : 0) |
296                     (urb->dev->speed == USB_SPEED_FULL ? ADMHCD_ED_SPEED : 0) |
297                     (usb_pipeisoc(pipe) ? ADMHCD_ED_FORMAT : 0) |
298                     (usb_maxpacket(urb->dev, pipe, usb_pipeout(pipe)) << ADMHCD_ED_MAXSHIFT);
299                 /* Alloc first dummy td */
300                 admhcd_td_alloc(found, NULL);
301                 if (hosthead) {
302                         found->next = hosthead;
303                         ed->next = found;
304                 } else {
305                         found->next = found;
306                         admhcd_reg_set(ahcd, ADMHCD_REG_HOSTHEAD, (u32)found);
307                 }
308         }
309 out:
310         admhcd_unlock(ahcd);
311         return found;
312 }
313
314 static struct admhcd_td *admhcd_td_fill(u32 control, struct admhcd_td *td,
315     dma_addr_t data, int len)
316 {
317         td->buffer = data;
318         td->buflen = len;
319         td->control = control;
320         return TD(td->next);
321 }
322
323 static void admhcd_ed_start(struct admhcd *ahcd, struct admhcd_ed *ed)
324 {
325         struct admhcd_td *td = ed->cur;
326
327         if (ed->urb)
328                 return;
329         if (td->urb) {
330                 ed->urb = td->urb;
331                 while (1) {
332                         td->control |= ADMHCD_TD_OWN;
333                         if (TD(td->next)->urb != td->urb) {
334                                 td->buflen |= ADMHCD_TD_INTEN;
335                                 break;
336                         }
337                         td = TD(td->next);
338                 }
339         }
340         ed->head = TD(ed->head);
341         ahcd->dma_en |= ADMHCD_DMA_EN;
342 }
343
344 static irqreturn_t adm5120hcd_irq(struct usb_hcd *hcd)
345 {
346         struct admhcd *ahcd = hcd_to_admhcd(hcd);
347         u32 intstatus;
348
349         intstatus = admhcd_reg_get(ahcd, ADMHCD_REG_INTSTATUS);
350         if (intstatus & ADMHCD_INT_FATAL) {
351                 admhcd_reg_set(ahcd, ADMHCD_REG_INTSTATUS, ADMHCD_INT_FATAL);
352                 //
353         }
354         if (intstatus & ADMHCD_INT_SW) {
355                 admhcd_reg_set(ahcd, ADMHCD_REG_INTSTATUS, ADMHCD_INT_SW);
356                 //
357         }
358         if (intstatus & ADMHCD_INT_TD) {
359                 struct admhcd_ed *ed, *head;
360
361                 admhcd_reg_set(ahcd, ADMHCD_REG_INTSTATUS, ADMHCD_INT_TD);
362
363                 head = (struct admhcd_ed *)admhcd_reg_get(ahcd, ADMHCD_REG_HOSTHEAD);
364                 ed = head;
365                 if (ed) do {
366                         /* Is it a finished TD? */
367                         if (ed->urb && !(ed->cur->control & ADMHCD_TD_OWN)) {
368                                 struct admhcd_td *td;
369                                 int error;
370
371                                 td = ed->cur;
372                                 error = (td->control & ADMHCD_TD_ERRMASK) >>
373                                     ADMHCD_TD_ERRSHIFT;
374                                 ed->urb->status = admhcd_td_err[error];
375                                 admhcd_td_free(ed, ed->urb);
376                                 // Calculate real length!!!
377                                 ed->urb->actual_length = ed->urb->transfer_buffer_length;
378                                 ed->urb->hcpriv = NULL;
379                                 usb_hcd_giveback_urb(hcd, ed->urb);
380                                 ed->urb = NULL;
381                         }
382                         admhcd_ed_start(ahcd, ed);
383                         ed = ED(ed->next);
384                 } while (ed != head);
385         }
386
387         return IRQ_HANDLED;
388 }
389
390 static int admhcd_urb_enqueue(struct usb_hcd *hcd, struct usb_host_endpoint *ep,
391     struct urb *urb, gfp_t mem_flags)
392 {
393         struct admhcd *ahcd = hcd_to_admhcd(hcd);
394         struct admhcd_ed *ed;
395         struct admhcd_td *td;
396         int size = 0, i, zero = 0, ret = 0;
397         unsigned int pipe = urb->pipe, toggle = 0;
398         dma_addr_t data = (dma_addr_t)urb->transfer_buffer;
399         int data_len = urb->transfer_buffer_length;
400
401         ed = admhcd_get_ed(ahcd, ep, urb);
402         if (!ed)
403                 return -ENOMEM;
404
405         switch(usb_pipetype(pipe)) {
406                 case PIPE_CONTROL:
407                         size = 2;
408                 case PIPE_INTERRUPT:
409                 case PIPE_BULK:
410                 default:
411                         size += urb->transfer_buffer_length / 4096;
412                         if (urb->transfer_buffer_length % 4096)
413                                 size++;
414                         if (size == 0)
415                                 size++;
416                         else if (urb->transfer_flags & URB_ZERO_PACKET &&
417                             !(urb->transfer_buffer_length %
418                               usb_maxpacket(urb->dev, pipe, usb_pipeout(pipe)))) {
419                                 size++;
420                                 zero = 1;
421                         }
422                         break;
423                 case PIPE_ISOCHRONOUS:
424                         size = urb->number_of_packets;
425                         break;
426         }
427
428         admhcd_lock(ahcd);
429         /* Remember the first td */
430         td = admhcd_td_alloc(ed, urb);
431         if (!td) {
432                 ret = -ENOMEM;
433                 goto out;
434         }
435         /* Allocate additionall tds first */
436         for (i = 1; i < size; i++) {
437                 if (admhcd_td_alloc(ed, urb) == NULL) {
438                         admhcd_td_free(ed, urb);
439                         ret = -ENOMEM;
440                         goto out;
441                 }
442         }
443
444         if (usb_gettoggle(urb->dev, usb_pipeendpoint(pipe), usb_pipeout(pipe)))
445                 toggle = ADMHCD_TD_TOGGLE;
446         else {
447                 toggle = ADMHCD_TD_DATA0;
448                 usb_settoggle(urb->dev, usb_pipeendpoint(pipe),
449                     usb_pipeout(pipe), 1);
450         }
451
452         switch(usb_pipetype(pipe)) {
453                 case PIPE_CONTROL:
454                         td = admhcd_td_fill(ADMHCD_TD_SETUP | ADMHCD_TD_DATA0,
455                             td, (dma_addr_t)urb->setup_packet, 8);
456                         while (data_len > 0) {
457                                 td = admhcd_td_fill(ADMHCD_TD_DATA1
458                                     | ADMHCD_TD_R |
459                                     (usb_pipeout(pipe) ?
460                                     ADMHCD_TD_OUT : ADMHCD_TD_IN), td,
461                                     data, data_len % 4097);
462                                 data_len -= 4096;
463                         }
464                         admhcd_td_fill(ADMHCD_TD_DATA1 | (usb_pipeout(pipe) ?
465                             ADMHCD_TD_IN : ADMHCD_TD_OUT), td,
466                             data, 0);
467                         break;
468                 case PIPE_INTERRUPT:
469                 case PIPE_BULK:
470                         //info ok for interrupt?
471                         i = 0;
472                         while(data_len > 4096) {
473                                 td = admhcd_td_fill((usb_pipeout(pipe) ?
474                                     ADMHCD_TD_OUT :
475                                     ADMHCD_TD_IN | ADMHCD_TD_R) |
476                                     (i ? ADMHCD_TD_TOGGLE : toggle), td,
477                                     data, 4096);
478                                 data += 4096;
479                                 data_len -= 4096;
480                                 i++;
481                         }
482                         td = admhcd_td_fill((usb_pipeout(pipe) ?
483                             ADMHCD_TD_OUT : ADMHCD_TD_IN) |
484                             (i ? ADMHCD_TD_TOGGLE : toggle), td, data, data_len);
485                         i++;
486                         if (zero)
487                                 admhcd_td_fill((usb_pipeout(pipe) ?
488                                     ADMHCD_TD_OUT : ADMHCD_TD_IN) |
489                                     (i ? ADMHCD_TD_TOGGLE : toggle), td, 0, 0);
490                         break;
491                 case PIPE_ISOCHRONOUS:
492                         for (i = 0; i < urb->number_of_packets; i++) {
493                                 td = admhcd_td_fill(ADMHCD_TD_ISO |
494                                     ((urb->start_frame + i) & 0xffff), td,
495                                     data + urb->iso_frame_desc[i].offset,
496                                     urb->iso_frame_desc[i].length);
497                         }
498                         break;
499         }
500         urb->hcpriv = ed;
501         admhcd_ed_start(ahcd, ed);
502 out:
503         admhcd_unlock(ahcd);
504         return ret;
505 }
506
507 static int admhcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
508 {
509         struct admhcd *ahcd = hcd_to_admhcd(hcd);
510         struct admhcd_ed *ed;
511
512         admhcd_lock(ahcd);
513
514         ed = urb->hcpriv;
515         if (ed && ed->urb != urb)
516                 admhcd_td_free(ed, urb);
517
518         admhcd_unlock(ahcd);
519         return 0;
520 }
521
522 static void admhcd_endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
523 {
524         struct admhcd *ahcd = hcd_to_admhcd(hcd);
525         struct admhcd_ed *ed, *edt, *head;
526
527         admhcd_lock(ahcd);
528
529         head = (struct admhcd_ed *)admhcd_reg_get(ahcd, ADMHCD_REG_HOSTHEAD);
530         if (!head)
531                 goto out;
532         for (ed = head; ED(ed->next) != head; ed = ED(ed->next))
533                 if (ed->ep == ep)
534                         break;
535         if (ed->ep != ep)
536                 goto out;
537         while (ed->cur)
538                 admhcd_td_free(ed, ed->cur->urb);
539         if (head == ed) {
540                 if (ED(ed->next) == ed) {
541                         admhcd_reg_set(ahcd, ADMHCD_REG_HOSTHEAD, 0);
542                         ahcd->dma_en = 0;
543                         goto out_free;
544                 }
545                 head = ED(ed->next);
546                 for (edt = head; ED(edt->next) != head; edt = ED(edt->next));
547                 edt->next = ED(ed->next);
548                 admhcd_reg_set(ahcd, ADMHCD_REG_HOSTHEAD, (u32)ed->next);
549                 goto out_free;
550         }
551         for (edt = head; edt->next != ed; edt = edt->next);
552         edt->next = ed->next;
553 out_free:
554         kfree(ed->real);
555 out:
556         admhcd_unlock(ahcd);
557 }
558
559 static int admhcd_get_frame_number(struct usb_hcd *hcd)
560 {
561         struct admhcd *ahcd = hcd_to_admhcd(hcd);
562
563         return admhcd_reg_get(ahcd, ADMHCD_REG_FMNUMBER) & 0x0000ffff;
564 }
565
566 static int admhcd_hub_status_data(struct usb_hcd *hcd, char *buf)
567 {
568         struct admhcd *ahcd = hcd_to_admhcd(hcd);
569         int port;
570
571         *buf = 0;
572         for (port = 0; port < ADMHCD_NUMPORTS; port++) {
573                 if (admhcd_reg_get(ahcd, ADMHCD_REG_PORTSTATUS0 + port*4) &
574                     (ADMHCD_CSC | ADMHCD_PESC | ADMHCD_PSSC | ADMHCD_OCIC |
575                      ADMHCD_PRSC))
576                         *buf |= (1 << (port + 1));
577         }
578         return !!*buf;
579 }
580
581 static __u8 root_hub_hub_des[] = {
582         0x09,           /* __u8  bLength; */
583         0x29,           /* __u8  bDescriptorType; Hub-descriptor */
584         0x02,           /* __u8  bNbrPorts; */
585         0x0a, 0x00,     /* __u16 wHubCharacteristics; */
586         0x01,           /* __u8  bPwrOn2pwrGood; 2ms */
587         0x00,           /* __u8  bHubContrCurrent; 0mA */
588         0x00,           /* __u8  DeviceRemovable; */
589         0xff,           /* __u8  PortPwrCtrlMask; */
590 };
591
592 static int admhcd_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
593     u16 wIndex, char *buf, u16 wLength)
594 {
595         struct admhcd *ahcd = hcd_to_admhcd(hcd);
596         int retval = 0, len;
597         unsigned int port = wIndex -1;
598
599         switch (typeReq) {
600
601         case GetHubStatus:
602                 *(__le32 *)buf = cpu_to_le32(0);
603                 break;
604         case GetPortStatus:
605                 if (port >= ADMHCD_NUMPORTS)
606                         goto err;
607                 *(__le32 *)buf = cpu_to_le32(
608                     admhcd_reg_get(ahcd, ADMHCD_REG_PORTSTATUS0 + port*4));
609                 break;
610         case SetHubFeature:             /* We don't implement these */
611         case ClearHubFeature:
612                 switch (wValue) {
613                 case C_HUB_OVER_CURRENT:
614                 case C_HUB_LOCAL_POWER:
615                         break;
616                 default:
617                         goto err;
618                 }
619         case SetPortFeature:
620                 if (port >= ADMHCD_NUMPORTS)
621                         goto err;
622
623                 switch (wValue) {
624                 case USB_PORT_FEAT_SUSPEND:
625                         admhcd_reg_set(ahcd, ADMHCD_REG_PORTSTATUS0 + port*4,
626                             ADMHCD_PSS);
627                         break;
628                 case USB_PORT_FEAT_RESET:
629                         if (admhcd_reg_get(ahcd, ADMHCD_REG_PORTSTATUS0 + port*4)
630                             & ADMHCD_CCS) {
631                                 admhcd_reg_set(ahcd,
632                                     ADMHCD_REG_PORTSTATUS0 + port*4,
633                                     ADMHCD_PRS | ADMHCD_CSC);
634                                 mdelay(50);
635                                 admhcd_reg_set(ahcd,
636                                     ADMHCD_REG_PORTSTATUS0 + port*4,
637                                     ADMHCD_PES | ADMHCD_CSC);
638                         }
639                         break;
640                 case USB_PORT_FEAT_POWER:
641                         admhcd_reg_set(ahcd, ADMHCD_REG_PORTSTATUS0 + port*4,
642                             ADMHCD_PPS);
643                         break;
644                 default:
645                         goto err;
646                 }
647                 break;
648         case ClearPortFeature:
649                 if (port >= ADMHCD_NUMPORTS)
650                         goto err;
651
652                 switch (wValue) {
653                 case USB_PORT_FEAT_ENABLE:
654                         admhcd_reg_set(ahcd, ADMHCD_REG_PORTSTATUS0 + port*4,
655                             ADMHCD_CCS);
656                         break;
657                 case USB_PORT_FEAT_C_ENABLE:
658                         admhcd_reg_set(ahcd, ADMHCD_REG_PORTSTATUS0 + port*4,
659                             ADMHCD_PESC);
660                         break;
661                 case USB_PORT_FEAT_SUSPEND:
662                         admhcd_reg_set(ahcd, ADMHCD_REG_PORTSTATUS0 + port*4,
663                             ADMHCD_POCI);
664                         break;
665                 case USB_PORT_FEAT_C_SUSPEND:
666                         admhcd_reg_set(ahcd, ADMHCD_REG_PORTSTATUS0 + port*4,
667                             ADMHCD_PSSC);
668                 case USB_PORT_FEAT_POWER:
669                         admhcd_reg_set(ahcd, ADMHCD_REG_PORTSTATUS0 + port*4,
670                             ADMHCD_LSDA);
671                         break;
672                 case USB_PORT_FEAT_C_CONNECTION:
673                         admhcd_reg_set(ahcd, ADMHCD_REG_PORTSTATUS0 + port*4,
674                             ADMHCD_CSC);
675                         break;
676                 case USB_PORT_FEAT_C_OVER_CURRENT:
677                         admhcd_reg_set(ahcd, ADMHCD_REG_PORTSTATUS0 + port*4,
678                             ADMHCD_OCIC);
679                         break;
680                 case USB_PORT_FEAT_C_RESET:
681                         admhcd_reg_set(ahcd, ADMHCD_REG_PORTSTATUS0 + port*4,
682                             ADMHCD_PRSC);
683                         break;
684                 default:
685                         goto err;
686                 }
687                 break;
688         case GetHubDescriptor:
689                 len = min_t(unsigned int, sizeof(root_hub_hub_des), wLength);
690                 memcpy(buf, root_hub_hub_des, len);
691                 break;
692         default:
693 err:
694                 retval = -EPIPE;
695         }
696
697         return retval;
698 }
699
700 static int admhcd_start(struct usb_hcd *hcd)
701 {
702         struct admhcd *ahcd = hcd_to_admhcd(hcd);
703         unsigned long flags;
704
705         printk(KERN_DEBUG PFX "calling admhcd_start\n");
706
707         spin_lock_irqsave(&ahcd->lock, flags);
708
709         /* Initialise the HCD registers */
710         admhcd_reg_set(ahcd, ADMHCD_REG_INTENABLE, 0);
711         mdelay(10);
712
713         admhcd_reg_set(ahcd, ADMHCD_REG_CONTROL, ADMHCD_SW_RESET);
714
715         while (admhcd_reg_get(ahcd, ADMHCD_REG_CONTROL) & ADMHCD_SW_RESET) {
716                 printk(KERN_WARNING PFX "waiting for reset to complete\n");
717                 mdelay(1);
718         }
719
720         hcd->uses_new_polling = 1;
721
722         /* Enable USB host mode */
723         admhcd_reg_set(ahcd, ADMHCD_REG_CONTROL, ADMHCD_HOST_EN);
724
725         /* Set host specific settings */
726         admhcd_reg_set(ahcd, ADMHCD_REG_HOSTHEAD, 0x00000000);
727         admhcd_reg_set(ahcd, ADMHCD_REG_FMINTERVAL, 0x20002edf);
728         admhcd_reg_set(ahcd, ADMHCD_REG_LSTHRESH, 0x628);
729
730         /* Set interrupts */
731         admhcd_reg_set(ahcd, ADMHCD_REG_INTENABLE,
732             ADMHCD_INT_ACT | ADMHCD_INT_FATAL | ADMHCD_INT_SW | ADMHCD_INT_TD);
733         admhcd_reg_set(ahcd, ADMHCD_REG_INTSTATUS,
734             ADMHCD_INT_ACT | ADMHCD_INT_FATAL | ADMHCD_INT_SW | ADMHCD_INT_TD);
735
736         /* Power on all ports */
737         admhcd_reg_set(ahcd, ADMHCD_REG_RHDESCR, ADMHCD_NPS | ADMHCD_LPSC);
738
739         /* HCD is now operationnal */
740         admhcd_reg_set(ahcd, ADMHCD_REG_HOSTCONTROL, ADMHCD_STATE_OP);
741
742         hcd->state = HC_STATE_RUNNING;
743
744         spin_unlock_irqrestore(&ahcd->lock, flags);
745
746         printk(KERN_DEBUG PFX "returning 0 from admhcd_start\n");
747         return 0;
748 }
749
750 static int admhcd_sw_reset(struct admhcd *ahcd)
751 {
752         int retries = 15;
753         unsigned long flags;
754         int ret = 0;
755
756         spin_lock_irqsave(&ahcd->lock, flags);
757
758         admhcd_reg_set(ahcd, ADMHCD_REG_INTENABLE, 0);
759         mdelay(10);
760
761         admhcd_reg_set(ahcd, ADMHCD_REG_CONTROL, ADMHCD_SW_RESET);
762
763         while (--retries) {
764                 mdelay(1);
765                 if (!(admhcd_reg_get(ahcd, ADMHCD_REG_CONTROL) & ADMHCD_SW_RESET))
766                         break;
767         }
768         if (!retries) {
769                 printk(KERN_WARNING "%s: software reset timeout\n", hcd_name);
770                 ret = -ETIME;
771         }
772         spin_unlock_irqrestore(&ahcd->lock, flags);
773         return ret;
774 }
775
776 static int admhcd_reset(struct usb_hcd *hcd)
777 {
778         struct admhcd *ahcd = hcd_to_admhcd(hcd);
779         u32 val = 0;
780         int ret, timeout = 15; /* ms */
781         unsigned long t;
782
783         ret = admhcd_sw_reset(ahcd);
784         if (ret)
785                 return ret;
786
787         t = jiffies + msecs_to_jiffies(timeout);
788         while (time_before_eq(jiffies, t)) {
789                 msleep(4);
790                 spin_lock_irq(&ahcd->lock);
791                 val = admhcd_reg_get(ahcd, ADMHCD_REG_HOSTCONTROL) & ADMHCD_STATE_RST;
792                 spin_unlock_irq(&ahcd->lock);
793                 if (val)
794                         break;
795         }
796         if (!val) {
797                 printk(KERN_WARNING "%s: device not ready after %dms\n",
798                         hcd_name, timeout);
799                 ret = -ENODEV;
800         }
801         return ret;
802 }
803
804 static void admhcd_stop(struct usb_hcd *hcd)
805 {
806         struct admhcd *ahcd = hcd_to_admhcd(hcd);
807         unsigned long flags;
808         u32 val;
809
810         spin_lock_irqsave(&ahcd->lock, flags);
811         admhcd_reg_set(ahcd, ADMHCD_REG_INTENABLE, 0);
812
813         /* Set global control of power for ports */
814         val = admhcd_reg_get(ahcd, ADMHCD_REG_RHDESCR);
815         val &= (~ADMHCD_PSM | ADMHCD_LPS);
816         admhcd_reg_set(ahcd, ADMHCD_REG_RHDESCR, val);
817
818         spin_unlock_irqrestore(&ahcd->lock, flags);
819
820         /* Ask for software reset */
821         admhcd_sw_reset(ahcd);
822 }
823
824
825 static struct hc_driver adm5120_hc_driver = {
826         .description =          hcd_name,
827         .product_desc =         "ADM5120 HCD",
828         .hcd_priv_size =        sizeof(struct admhcd),
829         .irq =                  adm5120hcd_irq,
830         .flags =                HCD_USB11,
831         .urb_enqueue =          admhcd_urb_enqueue,
832         .urb_dequeue =          admhcd_urb_dequeue,
833         .endpoint_disable =     admhcd_endpoint_disable,
834         .get_frame_number =     admhcd_get_frame_number,
835         .hub_status_data =      admhcd_hub_status_data,
836         .hub_control =          admhcd_hub_control,
837         .start  =               admhcd_start,
838         .stop   =               admhcd_stop,
839         .reset =                admhcd_reset,
840 };
841
842 #define resource_len(r) (((r)->end - (r)->start) + 1)
843
844 static int __init adm5120hcd_probe(struct platform_device *pdev)
845 {
846         struct usb_hcd *hcd;
847         struct admhcd *ahcd;
848         struct resource *data;
849         void __iomem *data_reg;
850
851         int err = 0, irq;
852
853         if (pdev->num_resources < 2) {
854                 printk(KERN_WARNING PFX "not enough resources\n");
855                 err = -ENODEV;
856                 goto out;
857         }
858
859         irq = platform_get_irq(pdev, 0);
860         data = platform_get_resource(pdev, IORESOURCE_MEM, 0);
861
862         if (pdev->dev.dma_mask) {
863                 printk(KERN_DEBUG PFX "no we won't dma\n");
864                 return -EINVAL;
865         }
866
867         if (!data || irq < 0) {
868                 printk(KERN_DEBUG PFX "either IRQ or data resource is invalid\n");
869                 err = -ENODEV;
870                 goto out;
871         }
872
873         if (!request_mem_region(data->start, resource_len(data), hcd_name)) {
874                 printk(KERN_DEBUG PFX "cannot request memory regions for the data resource\n");
875                 err = -EBUSY;
876                 goto out;
877         }
878
879         data_reg = ioremap(data->start, resource_len(data));
880         if (data_reg == NULL) {
881                 printk(KERN_DEBUG PFX "unable to ioremap\n");
882                 err = -ENOMEM;
883                 goto out_mem;
884         }
885
886         hcd = usb_create_hcd(&adm5120_hc_driver, &pdev->dev, pdev->dev.bus_id);
887         if (!hcd) {
888                 printk(KERN_DEBUG PFX "unable to create the hcd\n");
889                 err = -ENOMEM;
890                 goto out_unmap;
891         }
892
893         hcd->rsrc_start = data->start;
894         hcd->rsrc_len = resource_len(data);
895         hcd->regs = data_reg;
896
897         ahcd = hcd_to_admhcd(hcd);
898         ahcd->data_reg = data_reg;
899         ahcd->base = (u32)data_reg;
900
901         spin_lock_init(&ahcd->lock);
902         INIT_LIST_HEAD(&ahcd->async);
903
904         hcd->product_desc = "ADM5120 HCD";
905
906         err = usb_add_hcd(hcd, irq, IRQF_DISABLED);
907         if (err) {
908                 printk(KERN_DEBUG PFX "unable to add hcd\n");
909                 goto out_dev;
910         }
911
912         return 0;
913
914 out_dev:
915         usb_put_hcd(hcd);
916 out_unmap:
917         iounmap(data_reg);
918 out_mem:
919         release_mem_region(pdev->resource[0].start, pdev->resource[0].end - pdev->resource[0].start +1);
920 out:
921         return err;
922 }
923
924 #ifdef CONFIG_PM
925 static int adm5120hcd_suspend(struct platform_device *pdev,  pm_message_t state)
926 {
927         pdev-dev.power.power_state = state;
928         mdelay(1);
929         return 0;
930 }
931
932 static int adm5120hcd_resume(struct platform_device *pdev, pm_message_t state)
933 {
934         pdev->dev.power.power_state = PMSG_ON;
935         mdelay(1);
936         return 0;
937 }
938 #else
939 #define adm5120hcd_suspend      NULL
940 #define adm5120hcd_resume       NULL
941 #endif
942
943 static int __init_or_module adm5120hcd_remove(struct platform_device *pdev)
944 {
945         struct usb_hcd *hcd = platform_get_drvdata(pdev);
946         struct admhcd *ahcd;
947
948         if (!hcd)
949                 return 0;
950         ahcd = hcd_to_admhcd(hcd);
951         usb_remove_hcd(hcd);
952
953         usb_put_hcd(hcd);
954         return 0;
955 }
956
957 static struct platform_driver adm5120hcd_driver = {
958         .probe =        adm5120hcd_probe,
959         .remove =       adm5120hcd_remove,
960         .suspend =      adm5120hcd_suspend,
961         .remove =       adm5120hcd_resume,
962         .driver =       {
963                 .name   = (char *)hcd_name,
964                 .owner  = THIS_MODULE,
965         },
966 };
967
968 static int __init adm5120hcd_init(void)
969 {
970         int ret;
971
972         if (usb_disabled()) {
973                 printk(KERN_DEBUG PFX "USB support is disabled\n");
974                 return -ENODEV;
975         }
976
977         if (mips_machgroup != MACH_GROUP_ADM5120) {
978                 printk(KERN_DEBUG PFX "unsupported machine group\n");
979                 return -ENODEV;
980         }
981
982         ret = platform_driver_register(&adm5120hcd_driver);
983         if (ret == 0)
984                 printk(KERN_INFO PFX "registered\n");
985
986         return ret;
987 }
988
989 static void __exit adm5120hcd_exit(void)
990 {
991         platform_driver_unregister(&adm5120hcd_driver);
992         printk(KERN_INFO PFX "driver unregistered\n");
993 }
994
995 module_init(adm5120hcd_init);
996 module_exit(adm5120hcd_exit);