]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/usb/host/isp1760-hcd.c
c470cc83dbb0108ca19712a1ee82837d5dc1d5ee
[karo-tx-linux.git] / drivers / usb / host / isp1760-hcd.c
1 /*
2  * Driver for the NXP ISP1760 chip
3  *
4  * However, the code might contain some bugs. What doesn't work for sure is:
5  * - ISO
6  * - OTG
7  e The interrupt line is configured as active low, level.
8  *
9  * (c) 2007 Sebastian Siewior <bigeasy@linutronix.de>
10  *
11  */
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/list.h>
16 #include <linux/usb.h>
17 #include <linux/usb/hcd.h>
18 #include <linux/debugfs.h>
19 #include <linux/uaccess.h>
20 #include <linux/io.h>
21 #include <linux/mm.h>
22 #include <asm/unaligned.h>
23 #include <asm/cacheflush.h>
24
25 #include "isp1760-hcd.h"
26
27 static struct kmem_cache *qtd_cachep;
28 static struct kmem_cache *qh_cachep;
29
30 struct isp1760_hcd {
31         u32 hcs_params;
32         spinlock_t              lock;
33         struct inter_packet_info atl_ints[32];
34         struct inter_packet_info int_ints[32];
35         struct memory_chunk memory_pool[BLOCKS];
36         u32 atl_queued;
37
38         /* periodic schedule support */
39 #define DEFAULT_I_TDPS          1024
40         unsigned                periodic_size;
41         unsigned                i_thresh;
42         unsigned long           reset_done;
43         unsigned long           next_statechange;
44         unsigned int            devflags;
45 };
46
47 static inline struct isp1760_hcd *hcd_to_priv(struct usb_hcd *hcd)
48 {
49         return (struct isp1760_hcd *) (hcd->hcd_priv);
50 }
51 static inline struct usb_hcd *priv_to_hcd(struct isp1760_hcd *priv)
52 {
53         return container_of((void *) priv, struct usb_hcd, hcd_priv);
54 }
55
56 /* Section 2.2 Host Controller Capability Registers */
57 #define HC_LENGTH(p)            (((p)>>00)&0x00ff)      /* bits 7:0 */
58 #define HC_VERSION(p)           (((p)>>16)&0xffff)      /* bits 31:16 */
59 #define HCS_INDICATOR(p)        ((p)&(1 << 16)) /* true: has port indicators */
60 #define HCS_PPC(p)              ((p)&(1 << 4))  /* true: port power control */
61 #define HCS_N_PORTS(p)          (((p)>>0)&0xf)  /* bits 3:0, ports on HC */
62 #define HCC_ISOC_CACHE(p)       ((p)&(1 << 7))  /* true: can cache isoc frame */
63 #define HCC_ISOC_THRES(p)       (((p)>>4)&0x7)  /* bits 6:4, uframes cached */
64
65 /* Section 2.3 Host Controller Operational Registers */
66 #define CMD_LRESET      (1<<7)          /* partial reset (no ports, etc) */
67 #define CMD_RESET       (1<<1)          /* reset HC not bus */
68 #define CMD_RUN         (1<<0)          /* start/stop HC */
69 #define STS_PCD         (1<<2)          /* port change detect */
70 #define FLAG_CF         (1<<0)          /* true: we'll support "high speed" */
71
72 #define PORT_OWNER      (1<<13)         /* true: companion hc owns this port */
73 #define PORT_POWER      (1<<12)         /* true: has power (see PPC) */
74 #define PORT_USB11(x) (((x) & (3 << 10)) == (1 << 10))  /* USB 1.1 device */
75 #define PORT_RESET      (1<<8)          /* reset port */
76 #define PORT_SUSPEND    (1<<7)          /* suspend port */
77 #define PORT_RESUME     (1<<6)          /* resume it */
78 #define PORT_PE         (1<<2)          /* port enable */
79 #define PORT_CSC        (1<<1)          /* connect status change */
80 #define PORT_CONNECT    (1<<0)          /* device connected */
81 #define PORT_RWC_BITS   (PORT_CSC)
82
83 struct isp1760_qtd {
84         struct isp1760_qtd *hw_next;
85         u8 packet_type;
86         u8 toggle;
87
88         void *data_buffer;
89         /* the rest is HCD-private */
90         struct list_head qtd_list;
91         struct urb *urb;
92         size_t length;
93
94         /* isp special*/
95         u32 status;
96 #define URB_COMPLETE_NOTIFY     (1 << 0)
97 #define URB_ENQUEUED            (1 << 1)
98 #define URB_TYPE_ATL            (1 << 2)
99 #define URB_TYPE_INT            (1 << 3)
100 };
101
102 struct isp1760_qh {
103         /* first part defined by EHCI spec */
104         struct list_head qtd_list;
105         struct isp1760_hcd *priv;
106
107         /* periodic schedule info */
108         unsigned short period;          /* polling interval */
109         struct usb_device *dev;
110
111         u32 toggle;
112         u32 ping;
113 };
114
115 #define ehci_port_speed(priv, portsc) USB_PORT_STAT_HIGH_SPEED
116
117 static unsigned int isp1760_readl(__u32 __iomem *regs)
118 {
119         return readl(regs);
120 }
121
122 static void isp1760_writel(const unsigned int val, __u32 __iomem *regs)
123 {
124         writel(val, regs);
125 }
126
127 /*
128  * The next two copy via MMIO data to/from the device. memcpy_{to|from}io()
129  * doesn't quite work because some people have to enforce 32-bit access
130  */
131 static void priv_read_copy(struct isp1760_hcd *priv, u32 *src,
132                 __u32 __iomem *dst, u32 len)
133 {
134         u32 val;
135         u8 *buff8;
136
137         if (!src) {
138                 printk(KERN_ERR "ERROR: buffer: %p len: %d\n", src, len);
139                 return;
140         }
141
142         while (len >= 4) {
143                 *src = __raw_readl(dst);
144                 len -= 4;
145                 src++;
146                 dst++;
147         }
148
149         if (!len)
150                 return;
151
152         /* in case we have 3, 2 or 1 by left. The dst buffer may not be fully
153          * allocated.
154          */
155         val = isp1760_readl(dst);
156
157         buff8 = (u8 *)src;
158         while (len) {
159
160                 *buff8 = val;
161                 val >>= 8;
162                 len--;
163                 buff8++;
164         }
165 }
166
167 static void priv_write_copy(const struct isp1760_hcd *priv, const u32 *src,
168                 __u32 __iomem *dst, u32 len)
169 {
170         while (len >= 4) {
171                 __raw_writel(*src, dst);
172                 len -= 4;
173                 src++;
174                 dst++;
175         }
176
177         if (!len)
178                 return;
179         /* in case we have 3, 2 or 1 by left. The buffer is allocated and the
180          * extra bytes should not be read by the HW
181          */
182
183         __raw_writel(*src, dst);
184 }
185
186 /* memory management of the 60kb on the chip from 0x1000 to 0xffff */
187 static void init_memory(struct isp1760_hcd *priv)
188 {
189         int i;
190         u32 payload;
191
192         payload = 0x1000;
193         for (i = 0; i < BLOCK_1_NUM; i++) {
194                 priv->memory_pool[i].start = payload;
195                 priv->memory_pool[i].size = BLOCK_1_SIZE;
196                 priv->memory_pool[i].free = 1;
197                 payload += priv->memory_pool[i].size;
198         }
199
200
201         for (i = BLOCK_1_NUM; i < BLOCK_1_NUM + BLOCK_2_NUM; i++) {
202                 priv->memory_pool[i].start = payload;
203                 priv->memory_pool[i].size = BLOCK_2_SIZE;
204                 priv->memory_pool[i].free = 1;
205                 payload += priv->memory_pool[i].size;
206         }
207
208
209         for (i = BLOCK_1_NUM + BLOCK_2_NUM; i < BLOCKS; i++) {
210                 priv->memory_pool[i].start = payload;
211                 priv->memory_pool[i].size = BLOCK_3_SIZE;
212                 priv->memory_pool[i].free = 1;
213                 payload += priv->memory_pool[i].size;
214         }
215
216         BUG_ON(payload - priv->memory_pool[i - 1].size > PAYLOAD_SIZE);
217 }
218
219 static u32 alloc_mem(struct isp1760_hcd *priv, u32 size)
220 {
221         int i;
222
223         if (!size)
224                 return ISP1760_NULL_POINTER;
225
226         for (i = 0; i < BLOCKS; i++) {
227                 if (priv->memory_pool[i].size >= size &&
228                                 priv->memory_pool[i].free) {
229
230                         priv->memory_pool[i].free = 0;
231                         return priv->memory_pool[i].start;
232                 }
233         }
234
235         printk(KERN_ERR "ISP1760 MEM: can not allocate %d bytes of memory\n",
236                         size);
237         printk(KERN_ERR "Current memory map:\n");
238         for (i = 0; i < BLOCKS; i++) {
239                 printk(KERN_ERR "Pool %2d size %4d status: %d\n",
240                                 i, priv->memory_pool[i].size,
241                                 priv->memory_pool[i].free);
242         }
243         /* XXX maybe -ENOMEM could be possible */
244         BUG();
245         return 0;
246 }
247
248 static void free_mem(struct isp1760_hcd *priv, u32 mem)
249 {
250         int i;
251
252         if (mem == ISP1760_NULL_POINTER)
253                 return;
254
255         for (i = 0; i < BLOCKS; i++) {
256                 if (priv->memory_pool[i].start == mem) {
257
258                         BUG_ON(priv->memory_pool[i].free);
259
260                         priv->memory_pool[i].free = 1;
261                         return ;
262                 }
263         }
264
265         printk(KERN_ERR "Trying to free not-here-allocated memory :%08x\n",
266                         mem);
267         BUG();
268 }
269
270 static void isp1760_init_regs(struct usb_hcd *hcd)
271 {
272         isp1760_writel(0, hcd->regs + HC_BUFFER_STATUS_REG);
273         isp1760_writel(NO_TRANSFER_ACTIVE, hcd->regs +
274                         HC_ATL_PTD_SKIPMAP_REG);
275         isp1760_writel(NO_TRANSFER_ACTIVE, hcd->regs +
276                         HC_INT_PTD_SKIPMAP_REG);
277         isp1760_writel(NO_TRANSFER_ACTIVE, hcd->regs +
278                         HC_ISO_PTD_SKIPMAP_REG);
279
280         isp1760_writel(~NO_TRANSFER_ACTIVE, hcd->regs +
281                         HC_ATL_PTD_DONEMAP_REG);
282         isp1760_writel(~NO_TRANSFER_ACTIVE, hcd->regs +
283                         HC_INT_PTD_DONEMAP_REG);
284         isp1760_writel(~NO_TRANSFER_ACTIVE, hcd->regs +
285                         HC_ISO_PTD_DONEMAP_REG);
286 }
287
288 static int handshake(struct isp1760_hcd *priv, void __iomem *ptr,
289                       u32 mask, u32 done, int usec)
290 {
291         u32 result;
292
293         do {
294                 result = isp1760_readl(ptr);
295                 if (result == ~0)
296                         return -ENODEV;
297                 result &= mask;
298                 if (result == done)
299                         return 0;
300                 udelay(1);
301                 usec--;
302         } while (usec > 0);
303         return -ETIMEDOUT;
304 }
305
306 /* reset a non-running (STS_HALT == 1) controller */
307 static int ehci_reset(struct isp1760_hcd *priv)
308 {
309         int retval;
310         struct usb_hcd *hcd = priv_to_hcd(priv);
311         u32 command = isp1760_readl(hcd->regs + HC_USBCMD);
312
313         command |= CMD_RESET;
314         isp1760_writel(command, hcd->regs + HC_USBCMD);
315         hcd->state = HC_STATE_HALT;
316         priv->next_statechange = jiffies;
317         retval = handshake(priv, hcd->regs + HC_USBCMD,
318                             CMD_RESET, 0, 250 * 1000);
319         return retval;
320 }
321
322 static void qh_destroy(struct isp1760_qh *qh)
323 {
324         BUG_ON(!list_empty(&qh->qtd_list));
325         kmem_cache_free(qh_cachep, qh);
326 }
327
328 static struct isp1760_qh *isp1760_qh_alloc(struct isp1760_hcd *priv,
329                 gfp_t flags)
330 {
331         struct isp1760_qh *qh;
332
333         qh = kmem_cache_zalloc(qh_cachep, flags);
334         if (!qh)
335                 return qh;
336
337         INIT_LIST_HEAD(&qh->qtd_list);
338         qh->priv = priv;
339         return qh;
340 }
341
342 /* magic numbers that can affect system performance */
343 #define EHCI_TUNE_CERR          3       /* 0-3 qtd retries; 0 == don't stop */
344 #define EHCI_TUNE_RL_HS         4       /* nak throttle; see 4.9 */
345 #define EHCI_TUNE_RL_TT         0
346 #define EHCI_TUNE_MULT_HS       1       /* 1-3 transactions/uframe; 4.10.3 */
347 #define EHCI_TUNE_MULT_TT       1
348 #define EHCI_TUNE_FLS           2       /* (small) 256 frame schedule */
349
350 /* one-time init, only for memory state */
351 static int priv_init(struct usb_hcd *hcd)
352 {
353         struct isp1760_hcd              *priv = hcd_to_priv(hcd);
354         u32                     hcc_params;
355
356         spin_lock_init(&priv->lock);
357
358         /*
359          * hw default: 1K periodic list heads, one per frame.
360          * periodic_size can shrink by USBCMD update if hcc_params allows.
361          */
362         priv->periodic_size = DEFAULT_I_TDPS;
363
364         /* controllers may cache some of the periodic schedule ... */
365         hcc_params = isp1760_readl(hcd->regs + HC_HCCPARAMS);
366         /* full frame cache */
367         if (HCC_ISOC_CACHE(hcc_params))
368                 priv->i_thresh = 8;
369         else /* N microframes cached */
370                 priv->i_thresh = 2 + HCC_ISOC_THRES(hcc_params);
371
372         return 0;
373 }
374
375 static int isp1760_hc_setup(struct usb_hcd *hcd)
376 {
377         struct isp1760_hcd *priv = hcd_to_priv(hcd);
378         int result;
379         u32 scratch, hwmode;
380
381         /* Setup HW Mode Control: This assumes a level active-low interrupt */
382         hwmode = HW_DATA_BUS_32BIT;
383
384         if (priv->devflags & ISP1760_FLAG_BUS_WIDTH_16)
385                 hwmode &= ~HW_DATA_BUS_32BIT;
386         if (priv->devflags & ISP1760_FLAG_ANALOG_OC)
387                 hwmode |= HW_ANA_DIGI_OC;
388         if (priv->devflags & ISP1760_FLAG_DACK_POL_HIGH)
389                 hwmode |= HW_DACK_POL_HIGH;
390         if (priv->devflags & ISP1760_FLAG_DREQ_POL_HIGH)
391                 hwmode |= HW_DREQ_POL_HIGH;
392         if (priv->devflags & ISP1760_FLAG_INTR_POL_HIGH)
393                 hwmode |= HW_INTR_HIGH_ACT;
394         if (priv->devflags & ISP1760_FLAG_INTR_EDGE_TRIG)
395                 hwmode |= HW_INTR_EDGE_TRIG;
396
397         /*
398          * We have to set this first in case we're in 16-bit mode.
399          * Write it twice to ensure correct upper bits if switching
400          * to 16-bit mode.
401          */
402         isp1760_writel(hwmode, hcd->regs + HC_HW_MODE_CTRL);
403         isp1760_writel(hwmode, hcd->regs + HC_HW_MODE_CTRL);
404
405         isp1760_writel(0xdeadbabe, hcd->regs + HC_SCRATCH_REG);
406         /* Change bus pattern */
407         scratch = isp1760_readl(hcd->regs + HC_CHIP_ID_REG);
408         scratch = isp1760_readl(hcd->regs + HC_SCRATCH_REG);
409         if (scratch != 0xdeadbabe) {
410                 printk(KERN_ERR "ISP1760: Scratch test failed.\n");
411                 return -ENODEV;
412         }
413
414         /* pre reset */
415         isp1760_init_regs(hcd);
416
417         /* reset */
418         isp1760_writel(SW_RESET_RESET_ALL, hcd->regs + HC_RESET_REG);
419         mdelay(100);
420
421         isp1760_writel(SW_RESET_RESET_HC, hcd->regs + HC_RESET_REG);
422         mdelay(100);
423
424         result = ehci_reset(priv);
425         if (result)
426                 return result;
427
428         /* Step 11 passed */
429
430         isp1760_info(priv, "bus width: %d, oc: %s\n",
431                            (priv->devflags & ISP1760_FLAG_BUS_WIDTH_16) ?
432                            16 : 32, (priv->devflags & ISP1760_FLAG_ANALOG_OC) ?
433                            "analog" : "digital");
434
435         /* ATL reset */
436         isp1760_writel(hwmode | ALL_ATX_RESET, hcd->regs + HC_HW_MODE_CTRL);
437         mdelay(10);
438         isp1760_writel(hwmode, hcd->regs + HC_HW_MODE_CTRL);
439
440         isp1760_writel(INTERRUPT_ENABLE_MASK, hcd->regs + HC_INTERRUPT_REG);
441         isp1760_writel(INTERRUPT_ENABLE_MASK, hcd->regs + HC_INTERRUPT_ENABLE);
442
443         /*
444          * PORT 1 Control register of the ISP1760 is the OTG control
445          * register on ISP1761. Since there is no OTG or device controller
446          * support in this driver, we use port 1 as a "normal" USB host port on
447          * both chips.
448          */
449         isp1760_writel(PORT1_POWER | PORT1_INIT2,
450                        hcd->regs + HC_PORT1_CTRL);
451         mdelay(10);
452
453         priv->hcs_params = isp1760_readl(hcd->regs + HC_HCSPARAMS);
454
455         return priv_init(hcd);
456 }
457
458 static void isp1760_init_maps(struct usb_hcd *hcd)
459 {
460         /*set last maps, for iso its only 1, else 32 tds bitmap*/
461         isp1760_writel(0x80000000, hcd->regs + HC_ATL_PTD_LASTPTD_REG);
462         isp1760_writel(0x80000000, hcd->regs + HC_INT_PTD_LASTPTD_REG);
463         isp1760_writel(0x00000001, hcd->regs + HC_ISO_PTD_LASTPTD_REG);
464 }
465
466 static void isp1760_enable_interrupts(struct usb_hcd *hcd)
467 {
468         isp1760_writel(0, hcd->regs + HC_ATL_IRQ_MASK_AND_REG);
469         isp1760_writel(0, hcd->regs + HC_ATL_IRQ_MASK_OR_REG);
470         isp1760_writel(0, hcd->regs + HC_INT_IRQ_MASK_AND_REG);
471         isp1760_writel(0, hcd->regs + HC_INT_IRQ_MASK_OR_REG);
472         isp1760_writel(0, hcd->regs + HC_ISO_IRQ_MASK_AND_REG);
473         isp1760_writel(0xffffffff, hcd->regs + HC_ISO_IRQ_MASK_OR_REG);
474         /* step 23 passed */
475 }
476
477 static int isp1760_run(struct usb_hcd *hcd)
478 {
479         struct isp1760_hcd *priv = hcd_to_priv(hcd);
480         int retval;
481         u32 temp;
482         u32 command;
483         u32 chipid;
484
485         hcd->uses_new_polling = 1;
486
487         hcd->state = HC_STATE_RUNNING;
488         isp1760_enable_interrupts(hcd);
489         temp = isp1760_readl(hcd->regs + HC_HW_MODE_CTRL);
490         isp1760_writel(temp | HW_GLOBAL_INTR_EN, hcd->regs + HC_HW_MODE_CTRL);
491
492         command = isp1760_readl(hcd->regs + HC_USBCMD);
493         command &= ~(CMD_LRESET|CMD_RESET);
494         command |= CMD_RUN;
495         isp1760_writel(command, hcd->regs + HC_USBCMD);
496
497         retval = handshake(priv, hcd->regs + HC_USBCMD, CMD_RUN, CMD_RUN,
498                         250 * 1000);
499         if (retval)
500                 return retval;
501
502         /*
503          * XXX
504          * Spec says to write FLAG_CF as last config action, priv code grabs
505          * the semaphore while doing so.
506          */
507         down_write(&ehci_cf_port_reset_rwsem);
508         isp1760_writel(FLAG_CF, hcd->regs + HC_CONFIGFLAG);
509
510         retval = handshake(priv, hcd->regs + HC_CONFIGFLAG, FLAG_CF, FLAG_CF,
511                         250 * 1000);
512         up_write(&ehci_cf_port_reset_rwsem);
513         if (retval)
514                 return retval;
515
516         chipid = isp1760_readl(hcd->regs + HC_CHIP_ID_REG);
517         isp1760_info(priv, "USB ISP %04x HW rev. %d started\n", chipid & 0xffff,
518                         chipid >> 16);
519
520         /* PTD Register Init Part 2, Step 28 */
521         /* enable INTs */
522         isp1760_init_maps(hcd);
523
524         /* GRR this is run-once init(), being done every time the HC starts.
525          * So long as they're part of class devices, we can't do it init()
526          * since the class device isn't created that early.
527          */
528         return 0;
529 }
530
531 static u32 base_to_chip(u32 base)
532 {
533         return ((base - 0x400) >> 3);
534 }
535
536 static void transform_into_atl(struct isp1760_hcd *priv, struct isp1760_qh *qh,
537                         struct isp1760_qtd *qtd, struct urb *urb,
538                         u32 payload, struct ptd *ptd)
539 {
540         u32 dw0;
541         u32 dw1;
542         u32 dw2;
543         u32 dw3;
544         u32 maxpacket;
545         u32 multi;
546         u32 pid_code;
547         u32 rl = RL_COUNTER;
548         u32 nak = NAK_COUNTER;
549
550         /* according to 3.6.2, max packet len can not be > 0x400 */
551         maxpacket = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
552         multi =  1 + ((maxpacket >> 11) & 0x3);
553         maxpacket &= 0x7ff;
554
555         /* DW0 */
556         dw0 = PTD_VALID;
557         dw0 |= PTD_LENGTH(qtd->length);
558         dw0 |= PTD_MAXPACKET(maxpacket);
559         dw0 |= PTD_ENDPOINT(usb_pipeendpoint(urb->pipe));
560         dw1 = usb_pipeendpoint(urb->pipe) >> 1;
561
562         /* DW1 */
563         dw1 |= PTD_DEVICE_ADDR(usb_pipedevice(urb->pipe));
564
565         pid_code = qtd->packet_type;
566         dw1 |= PTD_PID_TOKEN(pid_code);
567
568         if (usb_pipebulk(urb->pipe))
569                 dw1 |= PTD_TRANS_BULK;
570         else if  (usb_pipeint(urb->pipe))
571                 dw1 |= PTD_TRANS_INT;
572
573         if (urb->dev->speed != USB_SPEED_HIGH) {
574                 /* split transaction */
575
576                 dw1 |= PTD_TRANS_SPLIT;
577                 if (urb->dev->speed == USB_SPEED_LOW)
578                         dw1 |= PTD_SE_USB_LOSPEED;
579
580                 dw1 |= PTD_PORT_NUM(urb->dev->ttport);
581                 dw1 |= PTD_HUB_NUM(urb->dev->tt->hub->devnum);
582
583                 /* SE bit for Split INT transfers */
584                 if (usb_pipeint(urb->pipe) &&
585                                 (urb->dev->speed == USB_SPEED_LOW))
586                         dw1 |= 2 << 16;
587
588                 dw3 = 0;
589                 rl = 0;
590                 nak = 0;
591         } else {
592                 dw0 |= PTD_MULTI(multi);
593                 if (usb_pipecontrol(urb->pipe) || usb_pipebulk(urb->pipe))
594                         dw3 = qh->ping;
595                 else
596                         dw3 = 0;
597         }
598         /* DW2 */
599         dw2 = 0;
600         dw2 |= PTD_DATA_START_ADDR(base_to_chip(payload));
601         dw2 |= PTD_RL_CNT(rl);
602         dw3 |= PTD_NAC_CNT(nak);
603
604         /* DW3 */
605         if (usb_pipecontrol(urb->pipe))
606                 dw3 |= PTD_DATA_TOGGLE(qtd->toggle);
607         else
608                 dw3 |= qh->toggle;
609
610
611         dw3 |= PTD_ACTIVE;
612         /* Cerr */
613         dw3 |= PTD_CERR(ERR_COUNTER);
614
615         memset(ptd, 0, sizeof(*ptd));
616
617         ptd->dw0 = cpu_to_le32(dw0);
618         ptd->dw1 = cpu_to_le32(dw1);
619         ptd->dw2 = cpu_to_le32(dw2);
620         ptd->dw3 = cpu_to_le32(dw3);
621 }
622
623 static void transform_add_int(struct isp1760_hcd *priv, struct isp1760_qh *qh,
624                         struct isp1760_qtd *qtd, struct urb *urb,
625                         u32 payload, struct ptd *ptd)
626 {
627         u32 maxpacket;
628         u32 multi;
629         u32 numberofusofs;
630         u32 i;
631         u32 usofmask, usof;
632         u32 period;
633
634         maxpacket = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
635         multi =  1 + ((maxpacket >> 11) & 0x3);
636         maxpacket &= 0x7ff;
637         /* length of the data per uframe */
638         maxpacket = multi * maxpacket;
639
640         numberofusofs = urb->transfer_buffer_length / maxpacket;
641         if (urb->transfer_buffer_length % maxpacket)
642                 numberofusofs += 1;
643
644         usofmask = 1;
645         usof = 0;
646         for (i = 0; i < numberofusofs; i++) {
647                 usof |= usofmask;
648                 usofmask <<= 1;
649         }
650
651         if (urb->dev->speed != USB_SPEED_HIGH) {
652                 /* split */
653                 ptd->dw5 = cpu_to_le32(0x1c);
654
655                 if (qh->period >= 32)
656                         period = qh->period / 2;
657                 else
658                         period = qh->period;
659
660         } else {
661
662                 if (qh->period >= 8)
663                         period = qh->period/8;
664                 else
665                         period = qh->period;
666
667                 if (period >= 32)
668                         period  = 16;
669
670                 if (qh->period >= 8) {
671                         /* millisecond period */
672                         period = (period << 3);
673                 } else {
674                         /* usof based tranmsfers */
675                         /* minimum 4 usofs */
676                         usof = 0x11;
677                 }
678         }
679
680         ptd->dw2 |= cpu_to_le32(period);
681         ptd->dw4 = cpu_to_le32(usof);
682 }
683
684 static void transform_into_int(struct isp1760_hcd *priv, struct isp1760_qh *qh,
685                         struct isp1760_qtd *qtd, struct urb *urb,
686                         u32 payload, struct ptd *ptd)
687 {
688         transform_into_atl(priv, qh, qtd, urb, payload, ptd);
689         transform_add_int(priv, qh, qtd, urb,  payload, ptd);
690 }
691
692 static int qtd_fill(struct isp1760_qtd *qtd, void *databuffer, size_t len,
693                 u32 token)
694 {
695         int count;
696
697         qtd->data_buffer = databuffer;
698         qtd->packet_type = GET_QTD_TOKEN_TYPE(token);
699         qtd->toggle = GET_DATA_TOGGLE(token);
700
701         if (len > HC_ATL_PL_SIZE)
702                 count = HC_ATL_PL_SIZE;
703         else
704                 count = len;
705
706         qtd->length = count;
707         return count;
708 }
709
710 static int check_error(struct ptd *ptd)
711 {
712         int error = 0;
713         u32 dw3;
714
715         dw3 = le32_to_cpu(ptd->dw3);
716         if (dw3 & DW3_HALT_BIT) {
717                 error = -EPIPE;
718
719                 if (dw3 & DW3_ERROR_BIT)
720                         pr_err("error bit is set in DW3\n");
721         }
722
723         if (dw3 & DW3_QTD_ACTIVE) {
724                 printk(KERN_ERR "transfer active bit is set DW3\n");
725                 printk(KERN_ERR "nak counter: %d, rl: %d\n", (dw3 >> 19) & 0xf,
726                                 (le32_to_cpu(ptd->dw2) >> 25) & 0xf);
727         }
728
729         return error;
730 }
731
732 static void check_int_err_status(u32 dw4)
733 {
734         u32 i;
735
736         dw4 >>= 8;
737
738         for (i = 0; i < 8; i++) {
739                 switch (dw4 & 0x7) {
740                 case INT_UNDERRUN:
741                         printk(KERN_ERR "ERROR: under run , %d\n", i);
742                         break;
743
744                 case INT_EXACT:
745                         printk(KERN_ERR "ERROR: transaction error, %d\n", i);
746                         break;
747
748                 case INT_BABBLE:
749                         printk(KERN_ERR "ERROR: babble error, %d\n", i);
750                         break;
751                 }
752                 dw4 >>= 3;
753         }
754 }
755
756 static void enqueue_one_qtd(struct isp1760_qtd *qtd, struct isp1760_hcd *priv,
757                 u32 payload)
758 {
759         u32 token;
760         struct usb_hcd *hcd = priv_to_hcd(priv);
761
762         token = qtd->packet_type;
763
764         if (qtd->length && (qtd->length <= HC_ATL_PL_SIZE)) {
765                 switch (token) {
766                 case IN_PID:
767                         break;
768                 case OUT_PID:
769                 case SETUP_PID:
770                         priv_write_copy(priv, qtd->data_buffer,
771                                         hcd->regs + payload,
772                                         qtd->length);
773                 }
774         }
775 }
776
777 static void enqueue_one_atl_qtd(u32 atl_regs, u32 payload,
778                 struct isp1760_hcd *priv, struct isp1760_qh *qh,
779                 struct urb *urb, u32 slot, struct isp1760_qtd *qtd)
780 {
781         struct ptd ptd;
782         struct usb_hcd *hcd = priv_to_hcd(priv);
783
784         transform_into_atl(priv, qh, qtd, urb, payload, &ptd);
785         priv_write_copy(priv, (u32 *)&ptd, hcd->regs + atl_regs, sizeof(ptd));
786         enqueue_one_qtd(qtd, priv, payload);
787
788         priv->atl_ints[slot].urb = urb;
789         priv->atl_ints[slot].qh = qh;
790         priv->atl_ints[slot].qtd = qtd;
791         priv->atl_ints[slot].data_buffer = qtd->data_buffer;
792         priv->atl_ints[slot].payload = payload;
793         qtd->status |= URB_ENQUEUED | URB_TYPE_ATL;
794         qtd->status |= slot << 16;
795 }
796
797 static void enqueue_one_int_qtd(u32 int_regs, u32 payload,
798                 struct isp1760_hcd *priv, struct isp1760_qh *qh,
799                 struct urb *urb, u32 slot,  struct isp1760_qtd *qtd)
800 {
801         struct ptd ptd;
802         struct usb_hcd *hcd = priv_to_hcd(priv);
803
804         transform_into_int(priv, qh, qtd, urb, payload, &ptd);
805         priv_write_copy(priv, (u32 *)&ptd, hcd->regs + int_regs, sizeof(ptd));
806         enqueue_one_qtd(qtd, priv, payload);
807
808         priv->int_ints[slot].urb = urb;
809         priv->int_ints[slot].qh = qh;
810         priv->int_ints[slot].qtd = qtd;
811         priv->int_ints[slot].data_buffer = qtd->data_buffer;
812         priv->int_ints[slot].payload = payload;
813         qtd->status |= URB_ENQUEUED | URB_TYPE_INT;
814         qtd->status |= slot << 16;
815 }
816
817 static void enqueue_an_ATL_packet(struct usb_hcd *hcd, struct isp1760_qh *qh,
818                                   struct isp1760_qtd *qtd)
819 {
820         struct isp1760_hcd *priv = hcd_to_priv(hcd);
821         u32 skip_map, or_map;
822         u32 queue_entry;
823         u32 slot;
824         u32 atl_regs, payload;
825         u32 buffstatus;
826
827         /*
828          * When this function is called from the interrupt handler to enqueue
829          * a follow-up packet, the SKIP register gets written and read back
830          * almost immediately. With ISP1761, this register requires a delay of
831          * 195ns between a write and subsequent read (see section 15.1.1.3).
832          */
833         mmiowb();
834         ndelay(195);
835         skip_map = isp1760_readl(hcd->regs + HC_ATL_PTD_SKIPMAP_REG);
836
837         BUG_ON(!skip_map);
838         slot = __ffs(skip_map);
839         queue_entry = 1 << slot;
840
841         atl_regs = ATL_REGS_OFFSET + slot * sizeof(struct ptd);
842
843         payload = alloc_mem(priv, qtd->length);
844
845         enqueue_one_atl_qtd(atl_regs, payload, priv, qh, qtd->urb, slot, qtd);
846
847         or_map = isp1760_readl(hcd->regs + HC_ATL_IRQ_MASK_OR_REG);
848         or_map |= queue_entry;
849         isp1760_writel(or_map, hcd->regs + HC_ATL_IRQ_MASK_OR_REG);
850
851         skip_map &= ~queue_entry;
852         isp1760_writel(skip_map, hcd->regs + HC_ATL_PTD_SKIPMAP_REG);
853
854         priv->atl_queued++;
855         if (priv->atl_queued == 2)
856                 isp1760_writel(INTERRUPT_ENABLE_SOT_MASK,
857                                 hcd->regs + HC_INTERRUPT_ENABLE);
858
859         buffstatus = isp1760_readl(hcd->regs + HC_BUFFER_STATUS_REG);
860         buffstatus |= ATL_BUFFER;
861         isp1760_writel(buffstatus, hcd->regs + HC_BUFFER_STATUS_REG);
862 }
863
864 static void enqueue_an_INT_packet(struct usb_hcd *hcd, struct isp1760_qh *qh,
865                                   struct isp1760_qtd *qtd)
866 {
867         struct isp1760_hcd *priv = hcd_to_priv(hcd);
868         u32 skip_map, or_map;
869         u32 queue_entry;
870         u32 slot;
871         u32 int_regs, payload;
872         u32 buffstatus;
873
874         /*
875          * When this function is called from the interrupt handler to enqueue
876          * a follow-up packet, the SKIP register gets written and read back
877          * almost immediately. With ISP1761, this register requires a delay of
878          * 195ns between a write and subsequent read (see section 15.1.1.3).
879          */
880         mmiowb();
881         ndelay(195);
882         skip_map = isp1760_readl(hcd->regs + HC_INT_PTD_SKIPMAP_REG);
883
884         BUG_ON(!skip_map);
885         slot = __ffs(skip_map);
886         queue_entry = 1 << slot;
887
888         int_regs = INT_REGS_OFFSET + slot * sizeof(struct ptd);
889
890         payload = alloc_mem(priv, qtd->length);
891
892         enqueue_one_int_qtd(int_regs, payload, priv, qh, qtd->urb, slot, qtd);
893
894         or_map = isp1760_readl(hcd->regs + HC_INT_IRQ_MASK_OR_REG);
895         or_map |= queue_entry;
896         isp1760_writel(or_map, hcd->regs + HC_INT_IRQ_MASK_OR_REG);
897
898         skip_map &= ~queue_entry;
899         isp1760_writel(skip_map, hcd->regs + HC_INT_PTD_SKIPMAP_REG);
900
901         buffstatus = isp1760_readl(hcd->regs + HC_BUFFER_STATUS_REG);
902         buffstatus |= INT_BUFFER;
903         isp1760_writel(buffstatus, hcd->regs + HC_BUFFER_STATUS_REG);
904 }
905
906 static void isp1760_urb_done(struct isp1760_hcd *priv, struct urb *urb, int status)
907 __releases(priv->lock)
908 __acquires(priv->lock)
909 {
910         if (!urb->unlinked) {
911                 if (status == -EINPROGRESS)
912                         status = 0;
913         }
914
915         if (usb_pipein(urb->pipe) && usb_pipetype(urb->pipe) != PIPE_CONTROL) {
916                 void *ptr;
917                 for (ptr = urb->transfer_buffer;
918                      ptr < urb->transfer_buffer + urb->transfer_buffer_length;
919                      ptr += PAGE_SIZE)
920                         flush_dcache_page(virt_to_page(ptr));
921         }
922
923         /* complete() can reenter this HCD */
924         usb_hcd_unlink_urb_from_ep(priv_to_hcd(priv), urb);
925         spin_unlock(&priv->lock);
926         usb_hcd_giveback_urb(priv_to_hcd(priv), urb, status);
927         spin_lock(&priv->lock);
928 }
929
930 static void isp1760_qtd_free(struct isp1760_qtd *qtd)
931 {
932         kmem_cache_free(qtd_cachep, qtd);
933 }
934
935 static struct isp1760_qtd *clean_this_qtd(struct isp1760_qtd *qtd)
936 {
937         struct isp1760_qtd *tmp_qtd;
938
939         tmp_qtd = qtd->hw_next;
940         list_del(&qtd->qtd_list);
941         isp1760_qtd_free(qtd);
942         return tmp_qtd;
943 }
944
945 /*
946  * Remove this QTD from the QH list and free its memory. If this QTD
947  * isn't the last one than remove also his successor(s).
948  * Returns the QTD which is part of an new URB and should be enqueued.
949  */
950 static struct isp1760_qtd *clean_up_qtdlist(struct isp1760_qtd *qtd)
951 {
952         struct isp1760_qtd *tmp_qtd;
953         int last_one;
954
955         do {
956                 tmp_qtd = qtd->hw_next;
957                 last_one = qtd->status & URB_COMPLETE_NOTIFY;
958                 list_del(&qtd->qtd_list);
959                 isp1760_qtd_free(qtd);
960                 qtd = tmp_qtd;
961         } while (!last_one && qtd);
962
963         return qtd;
964 }
965
966 static void do_atl_int(struct usb_hcd *usb_hcd)
967 {
968         struct isp1760_hcd *priv = hcd_to_priv(usb_hcd);
969         u32 done_map, skip_map;
970         struct ptd ptd;
971         struct urb *urb = NULL;
972         u32 atl_regs_base;
973         u32 atl_regs;
974         u32 queue_entry;
975         u32 payload;
976         u32 length;
977         u32 or_map;
978         u32 status = -EINVAL;
979         int error;
980         struct isp1760_qtd *qtd;
981         struct isp1760_qh *qh;
982         u32 rl;
983         u32 nakcount;
984
985         done_map = isp1760_readl(usb_hcd->regs +
986                         HC_ATL_PTD_DONEMAP_REG);
987         skip_map = isp1760_readl(usb_hcd->regs +
988                         HC_ATL_PTD_SKIPMAP_REG);
989
990         or_map = isp1760_readl(usb_hcd->regs + HC_ATL_IRQ_MASK_OR_REG);
991         or_map &= ~done_map;
992         isp1760_writel(or_map, usb_hcd->regs + HC_ATL_IRQ_MASK_OR_REG);
993
994         atl_regs_base = ATL_REGS_OFFSET;
995         while (done_map) {
996                 u32 dw1;
997                 u32 dw2;
998                 u32 dw3;
999
1000                 status = 0;
1001                 priv->atl_queued--;
1002
1003                 queue_entry = __ffs(done_map);
1004                 done_map &= ~(1 << queue_entry);
1005                 skip_map |= 1 << queue_entry;
1006
1007                 atl_regs = atl_regs_base + queue_entry * sizeof(struct ptd);
1008
1009                 urb = priv->atl_ints[queue_entry].urb;
1010                 qtd = priv->atl_ints[queue_entry].qtd;
1011                 qh = priv->atl_ints[queue_entry].qh;
1012                 payload = priv->atl_ints[queue_entry].payload;
1013
1014                 if (!qh) {
1015                         printk(KERN_ERR "qh is 0\n");
1016                         continue;
1017                 }
1018                 isp1760_writel(atl_regs + ISP_BANK(0), usb_hcd->regs +
1019                                 HC_MEMORY_REG);
1020                 isp1760_writel(payload  + ISP_BANK(1), usb_hcd->regs +
1021                                 HC_MEMORY_REG);
1022                 /*
1023                  * write bank1 address twice to ensure the 90ns delay (time
1024                  * between BANK0 write and the priv_read_copy() call is at
1025                  * least 3*t_WHWL + 2*t_w11 = 3*25ns + 2*17ns = 109ns)
1026                  */
1027                 isp1760_writel(payload  + ISP_BANK(1), usb_hcd->regs +
1028                                 HC_MEMORY_REG);
1029
1030                 priv_read_copy(priv, (u32 *)&ptd, usb_hcd->regs + atl_regs +
1031                                 ISP_BANK(0), sizeof(ptd));
1032
1033                 dw1 = le32_to_cpu(ptd.dw1);
1034                 dw2 = le32_to_cpu(ptd.dw2);
1035                 dw3 = le32_to_cpu(ptd.dw3);
1036                 rl = (dw2 >> 25) & 0x0f;
1037                 nakcount = (dw3 >> 19) & 0xf;
1038
1039                 /* Transfer Error, *but* active and no HALT -> reload */
1040                 if ((dw3 & DW3_ERROR_BIT) && (dw3 & DW3_QTD_ACTIVE) &&
1041                                 !(dw3 & DW3_HALT_BIT)) {
1042
1043                         /* according to ppriv code, we have to
1044                          * reload this one if trasfered bytes != requested bytes
1045                          * else act like everything went smooth..
1046                          * XXX This just doesn't feel right and hasn't
1047                          * triggered so far.
1048                          */
1049
1050                         length = PTD_XFERRED_LENGTH(dw3);
1051                         printk(KERN_ERR "Should reload now.... transfered %d "
1052                                         "of %zu\n", length, qtd->length);
1053                         BUG();
1054                 }
1055
1056                 if (!nakcount && (dw3 & DW3_QTD_ACTIVE)) {
1057                         u32 buffstatus;
1058
1059                         /*
1060                          * NAKs are handled in HW by the chip. Usually if the
1061                          * device is not able to send data fast enough.
1062                          * This happens mostly on slower hardware.
1063                          */
1064
1065                         /* RL counter = ERR counter */
1066                         dw3 &= ~(0xf << 19);
1067                         dw3 |= rl << 19;
1068                         dw3 &= ~(3 << (55 - 32));
1069                         dw3 |= ERR_COUNTER << (55 - 32);
1070
1071                         /*
1072                          * It is not needed to write skip map back because it
1073                          * is unchanged. Just make sure that this entry is
1074                          * unskipped once it gets written to the HW.
1075                          */
1076                         skip_map &= ~(1 << queue_entry);
1077                         or_map = isp1760_readl(usb_hcd->regs +
1078                                         HC_ATL_IRQ_MASK_OR_REG);
1079                         or_map |= 1 << queue_entry;
1080                         isp1760_writel(or_map, usb_hcd->regs +
1081                                         HC_ATL_IRQ_MASK_OR_REG);
1082
1083                         ptd.dw3 = cpu_to_le32(dw3);
1084                         priv_write_copy(priv, (u32 *)&ptd, usb_hcd->regs +
1085                                         atl_regs, sizeof(ptd));
1086
1087                         ptd.dw0 |= cpu_to_le32(PTD_VALID);
1088                         priv_write_copy(priv, (u32 *)&ptd, usb_hcd->regs +
1089                                         atl_regs, sizeof(ptd));
1090
1091                         priv->atl_queued++;
1092                         if (priv->atl_queued == 2)
1093                                 isp1760_writel(INTERRUPT_ENABLE_SOT_MASK,
1094                                     usb_hcd->regs + HC_INTERRUPT_ENABLE);
1095
1096                         buffstatus = isp1760_readl(usb_hcd->regs +
1097                                         HC_BUFFER_STATUS_REG);
1098                         buffstatus |= ATL_BUFFER;
1099                         isp1760_writel(buffstatus, usb_hcd->regs +
1100                                         HC_BUFFER_STATUS_REG);
1101                         continue;
1102                 }
1103
1104                 error = check_error(&ptd);
1105                 if (error) {
1106                         status = error;
1107                         priv->atl_ints[queue_entry].qh->toggle = 0;
1108                         priv->atl_ints[queue_entry].qh->ping = 0;
1109                         urb->status = -EPIPE;
1110
1111 #if 0
1112                         printk(KERN_ERR "Error in %s().\n", __func__);
1113                         printk(KERN_ERR "IN dw0: %08x dw1: %08x dw2: %08x "
1114                                         "dw3: %08x dw4: %08x dw5: %08x dw6: "
1115                                         "%08x dw7: %08x\n",
1116                                         ptd.dw0, ptd.dw1, ptd.dw2, ptd.dw3,
1117                                         ptd.dw4, ptd.dw5, ptd.dw6, ptd.dw7);
1118 #endif
1119                 } else {
1120                         if (usb_pipetype(urb->pipe) == PIPE_BULK) {
1121                                 priv->atl_ints[queue_entry].qh->toggle = dw3 &
1122                                         (1 << 25);
1123                                 priv->atl_ints[queue_entry].qh->ping = dw3 &
1124                                         (1 << 26);
1125                         }
1126                 }
1127
1128                 length = PTD_XFERRED_LENGTH(dw3);
1129                 if (length) {
1130                         switch (DW1_GET_PID(dw1)) {
1131                         case IN_PID:
1132                                 priv_read_copy(priv,
1133                                         priv->atl_ints[queue_entry].data_buffer,
1134                                         usb_hcd->regs + payload + ISP_BANK(1),
1135                                         length);
1136
1137                         case OUT_PID:
1138
1139                                 urb->actual_length += length;
1140
1141                         case SETUP_PID:
1142                                 break;
1143                         }
1144                 }
1145
1146                 priv->atl_ints[queue_entry].data_buffer = NULL;
1147                 priv->atl_ints[queue_entry].urb = NULL;
1148                 priv->atl_ints[queue_entry].qtd = NULL;
1149                 priv->atl_ints[queue_entry].qh = NULL;
1150
1151                 free_mem(priv, payload);
1152
1153                 isp1760_writel(skip_map, usb_hcd->regs +
1154                                 HC_ATL_PTD_SKIPMAP_REG);
1155
1156                 if (urb->status == -EPIPE) {
1157                         /* HALT was received */
1158
1159                         qtd = clean_up_qtdlist(qtd);
1160                         isp1760_urb_done(priv, urb, urb->status);
1161
1162                 } else if (usb_pipebulk(urb->pipe) && (length < qtd->length)) {
1163                         /* short BULK received */
1164
1165                         if (urb->transfer_flags & URB_SHORT_NOT_OK) {
1166                                 urb->status = -EREMOTEIO;
1167                                 isp1760_dbg(priv, "short bulk, %d instead %zu "
1168                                         "with URB_SHORT_NOT_OK flag.\n",
1169                                         length, qtd->length);
1170                         }
1171
1172                         if (urb->status == -EINPROGRESS)
1173                                 urb->status = 0;
1174
1175                         qtd = clean_up_qtdlist(qtd);
1176
1177                         isp1760_urb_done(priv, urb, urb->status);
1178
1179                 } else if (qtd->status & URB_COMPLETE_NOTIFY) {
1180                         /* that was the last qtd of that URB */
1181
1182                         if (urb->status == -EINPROGRESS)
1183                                 urb->status = 0;
1184
1185                         qtd = clean_this_qtd(qtd);
1186                         isp1760_urb_done(priv, urb, urb->status);
1187
1188                 } else {
1189                         /* next QTD of this URB */
1190
1191                         qtd = clean_this_qtd(qtd);
1192                         BUG_ON(!qtd);
1193                 }
1194
1195                 if (qtd)
1196                         enqueue_an_ATL_packet(usb_hcd, qh, qtd);
1197
1198                 skip_map = isp1760_readl(usb_hcd->regs +
1199                                 HC_ATL_PTD_SKIPMAP_REG);
1200         }
1201         if (priv->atl_queued <= 1)
1202                 isp1760_writel(INTERRUPT_ENABLE_MASK,
1203                                 usb_hcd->regs + HC_INTERRUPT_ENABLE);
1204 }
1205
1206 static void do_intl_int(struct usb_hcd *usb_hcd)
1207 {
1208         struct isp1760_hcd *priv = hcd_to_priv(usb_hcd);
1209         u32 done_map, skip_map;
1210         struct ptd ptd;
1211         struct urb *urb = NULL;
1212         u32 int_regs;
1213         u32 int_regs_base;
1214         u32 payload;
1215         u32 length;
1216         u32 or_map;
1217         int error;
1218         u32 queue_entry;
1219         struct isp1760_qtd *qtd;
1220         struct isp1760_qh *qh;
1221
1222         done_map = isp1760_readl(usb_hcd->regs +
1223                         HC_INT_PTD_DONEMAP_REG);
1224         skip_map = isp1760_readl(usb_hcd->regs +
1225                         HC_INT_PTD_SKIPMAP_REG);
1226
1227         or_map = isp1760_readl(usb_hcd->regs + HC_INT_IRQ_MASK_OR_REG);
1228         or_map &= ~done_map;
1229         isp1760_writel(or_map, usb_hcd->regs + HC_INT_IRQ_MASK_OR_REG);
1230
1231         int_regs_base = INT_REGS_OFFSET;
1232
1233         while (done_map) {
1234                 u32 dw1;
1235                 u32 dw3;
1236
1237                 queue_entry = __ffs(done_map);
1238                 done_map &= ~(1 << queue_entry);
1239                 skip_map |= 1 << queue_entry;
1240
1241                 int_regs = int_regs_base + queue_entry * sizeof(struct ptd);
1242                 urb = priv->int_ints[queue_entry].urb;
1243                 qtd = priv->int_ints[queue_entry].qtd;
1244                 qh = priv->int_ints[queue_entry].qh;
1245                 payload = priv->int_ints[queue_entry].payload;
1246
1247                 if (!qh) {
1248                         printk(KERN_ERR "(INT) qh is 0\n");
1249                         continue;
1250                 }
1251
1252                 isp1760_writel(int_regs + ISP_BANK(0), usb_hcd->regs +
1253                                 HC_MEMORY_REG);
1254                 isp1760_writel(payload  + ISP_BANK(1), usb_hcd->regs +
1255                                 HC_MEMORY_REG);
1256                 /*
1257                  * write bank1 address twice to ensure the 90ns delay (time
1258                  * between BANK0 write and the priv_read_copy() call is at
1259                  * least 3*t_WHWL + 2*t_w11 = 3*25ns + 2*17ns = 92ns)
1260                  */
1261                 isp1760_writel(payload  + ISP_BANK(1), usb_hcd->regs +
1262                                 HC_MEMORY_REG);
1263
1264                 priv_read_copy(priv, (u32 *)&ptd, usb_hcd->regs + int_regs +
1265                                 ISP_BANK(0), sizeof(ptd));
1266                 dw1 = le32_to_cpu(ptd.dw1);
1267                 dw3 = le32_to_cpu(ptd.dw3);
1268                 check_int_err_status(le32_to_cpu(ptd.dw4));
1269
1270                 error = check_error(&ptd);
1271                 if (error) {
1272 #if 0
1273                         printk(KERN_ERR "Error in %s().\n", __func__);
1274                         printk(KERN_ERR "IN dw0: %08x dw1: %08x dw2: %08x "
1275                                         "dw3: %08x dw4: %08x dw5: %08x dw6: "
1276                                         "%08x dw7: %08x\n",
1277                                         ptd.dw0, ptd.dw1, ptd.dw2, ptd.dw3,
1278                                         ptd.dw4, ptd.dw5, ptd.dw6, ptd.dw7);
1279 #endif
1280                         urb->status = -EPIPE;
1281                         priv->int_ints[queue_entry].qh->toggle = 0;
1282                         priv->int_ints[queue_entry].qh->ping = 0;
1283
1284                 } else {
1285                         priv->int_ints[queue_entry].qh->toggle =
1286                                 dw3 & (1 << 25);
1287                         priv->int_ints[queue_entry].qh->ping = dw3 & (1 << 26);
1288                 }
1289
1290                 if (urb->dev->speed != USB_SPEED_HIGH)
1291                         length = PTD_XFERRED_LENGTH_LO(dw3);
1292                 else
1293                         length = PTD_XFERRED_LENGTH(dw3);
1294
1295                 if (length) {
1296                         switch (DW1_GET_PID(dw1)) {
1297                         case IN_PID:
1298                                 priv_read_copy(priv,
1299                                         priv->int_ints[queue_entry].data_buffer,
1300                                         usb_hcd->regs + payload + ISP_BANK(1),
1301                                         length);
1302                         case OUT_PID:
1303
1304                                 urb->actual_length += length;
1305
1306                         case SETUP_PID:
1307                                 break;
1308                         }
1309                 }
1310
1311                 priv->int_ints[queue_entry].data_buffer = NULL;
1312                 priv->int_ints[queue_entry].urb = NULL;
1313                 priv->int_ints[queue_entry].qtd = NULL;
1314                 priv->int_ints[queue_entry].qh = NULL;
1315
1316                 isp1760_writel(skip_map, usb_hcd->regs +
1317                                 HC_INT_PTD_SKIPMAP_REG);
1318                 free_mem(priv, payload);
1319
1320                 if (urb->status == -EPIPE) {
1321                         /* HALT received */
1322
1323                          qtd = clean_up_qtdlist(qtd);
1324                          isp1760_urb_done(priv, urb, urb->status);
1325
1326                 } else if (qtd->status & URB_COMPLETE_NOTIFY) {
1327
1328                         if (urb->status == -EINPROGRESS)
1329                                 urb->status = 0;
1330
1331                         qtd = clean_this_qtd(qtd);
1332                         isp1760_urb_done(priv, urb, urb->status);
1333
1334                 } else {
1335                         /* next QTD of this URB */
1336
1337                         qtd = clean_this_qtd(qtd);
1338                         BUG_ON(!qtd);
1339                 }
1340
1341                 if (qtd)
1342                         enqueue_an_INT_packet(usb_hcd, qh, qtd);
1343
1344                 skip_map = isp1760_readl(usb_hcd->regs +
1345                                 HC_INT_PTD_SKIPMAP_REG);
1346         }
1347 }
1348
1349 #define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
1350 static struct isp1760_qh *qh_make(struct isp1760_hcd *priv, struct urb *urb,
1351                 gfp_t flags)
1352 {
1353         struct isp1760_qh *qh;
1354         int is_input, type;
1355
1356         qh = isp1760_qh_alloc(priv, flags);
1357         if (!qh)
1358                 return qh;
1359
1360         /*
1361          * init endpoint/device data for this QH
1362          */
1363         is_input = usb_pipein(urb->pipe);
1364         type = usb_pipetype(urb->pipe);
1365
1366         if (type == PIPE_INTERRUPT) {
1367
1368                 if (urb->dev->speed == USB_SPEED_HIGH) {
1369
1370                         qh->period = urb->interval >> 3;
1371                         if (qh->period == 0 && urb->interval != 1) {
1372                                 /* NOTE interval 2 or 4 uframes could work.
1373                                  * But interval 1 scheduling is simpler, and
1374                                  * includes high bandwidth.
1375                                  */
1376                                 printk(KERN_ERR "intr period %d uframes, NYET!",
1377                                                 urb->interval);
1378                                 qh_destroy(qh);
1379                                 return NULL;
1380                         }
1381                 } else {
1382                         qh->period = urb->interval;
1383                 }
1384         }
1385
1386         /* support for tt scheduling, and access to toggles */
1387         qh->dev = urb->dev;
1388
1389         if (!usb_pipecontrol(urb->pipe))
1390                 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), !is_input,
1391                                 1);
1392         return qh;
1393 }
1394
1395 /*
1396  * For control/bulk/interrupt, return QH with these TDs appended.
1397  * Allocates and initializes the QH if necessary.
1398  * Returns null if it can't allocate a QH it needs to.
1399  * If the QH has TDs (urbs) already, that's great.
1400  */
1401 static struct isp1760_qh *qh_append_tds(struct isp1760_hcd *priv,
1402                 struct urb *urb, struct list_head *qtd_list, int epnum,
1403                 void **ptr)
1404 {
1405         struct isp1760_qh *qh;
1406         struct isp1760_qtd *qtd;
1407         struct isp1760_qtd *prev_qtd;
1408
1409         qh = (struct isp1760_qh *)*ptr;
1410         if (!qh) {
1411                 /* can't sleep here, we have priv->lock... */
1412                 qh = qh_make(priv, urb, GFP_ATOMIC);
1413                 if (!qh)
1414                         return qh;
1415                 *ptr = qh;
1416         }
1417
1418         qtd = list_entry(qtd_list->next, struct isp1760_qtd,
1419                         qtd_list);
1420         if (!list_empty(&qh->qtd_list))
1421                 prev_qtd = list_entry(qh->qtd_list.prev,
1422                                 struct isp1760_qtd, qtd_list);
1423         else
1424                 prev_qtd = NULL;
1425
1426         list_splice(qtd_list, qh->qtd_list.prev);
1427         if (prev_qtd) {
1428                 BUG_ON(prev_qtd->hw_next);
1429                 prev_qtd->hw_next = qtd;
1430         }
1431
1432         urb->hcpriv = qh;
1433         return qh;
1434 }
1435
1436 static void qtd_list_free(struct isp1760_hcd *priv, struct urb *urb,
1437                 struct list_head *qtd_list)
1438 {
1439         struct list_head *entry, *temp;
1440
1441         list_for_each_safe(entry, temp, qtd_list) {
1442                 struct isp1760_qtd      *qtd;
1443
1444                 qtd = list_entry(entry, struct isp1760_qtd, qtd_list);
1445                 list_del(&qtd->qtd_list);
1446                 isp1760_qtd_free(qtd);
1447         }
1448 }
1449
1450 static int isp1760_prepare_enqueue(struct isp1760_hcd *priv, struct urb *urb,
1451                 struct list_head *qtd_list, gfp_t mem_flags, packet_enqueue *p)
1452 {
1453         struct isp1760_qtd         *qtd;
1454         int                     epnum;
1455         unsigned long           flags;
1456         struct isp1760_qh          *qh = NULL;
1457         int                     rc;
1458         int qh_busy;
1459
1460         qtd = list_entry(qtd_list->next, struct isp1760_qtd, qtd_list);
1461         epnum = urb->ep->desc.bEndpointAddress;
1462
1463         spin_lock_irqsave(&priv->lock, flags);
1464         if (!HCD_HW_ACCESSIBLE(priv_to_hcd(priv))) {
1465                 rc = -ESHUTDOWN;
1466                 goto done;
1467         }
1468         rc = usb_hcd_link_urb_to_ep(priv_to_hcd(priv), urb);
1469         if (rc)
1470                 goto done;
1471
1472         qh = urb->ep->hcpriv;
1473         if (qh)
1474                 qh_busy = !list_empty(&qh->qtd_list);
1475         else
1476                 qh_busy = 0;
1477
1478         qh = qh_append_tds(priv, urb, qtd_list, epnum, &urb->ep->hcpriv);
1479         if (!qh) {
1480                 usb_hcd_unlink_urb_from_ep(priv_to_hcd(priv), urb);
1481                 rc = -ENOMEM;
1482                 goto done;
1483         }
1484
1485         if (!qh_busy)
1486                 p(priv_to_hcd(priv), qh, qtd);
1487
1488 done:
1489         spin_unlock_irqrestore(&priv->lock, flags);
1490         if (!qh)
1491                 qtd_list_free(priv, urb, qtd_list);
1492         return rc;
1493 }
1494
1495 static struct isp1760_qtd *isp1760_qtd_alloc(struct isp1760_hcd *priv,
1496                 gfp_t flags)
1497 {
1498         struct isp1760_qtd *qtd;
1499
1500         qtd = kmem_cache_zalloc(qtd_cachep, flags);
1501         if (qtd)
1502                 INIT_LIST_HEAD(&qtd->qtd_list);
1503
1504         return qtd;
1505 }
1506
1507 /*
1508  * create a list of filled qtds for this URB; won't link into qh.
1509  */
1510 static struct list_head *qh_urb_transaction(struct isp1760_hcd *priv,
1511                 struct urb *urb, struct list_head *head, gfp_t flags)
1512 {
1513         struct isp1760_qtd *qtd, *qtd_prev;
1514         void *buf;
1515         int len, maxpacket;
1516         int is_input;
1517         u32 token;
1518
1519         /*
1520          * URBs map to sequences of QTDs:  one logical transaction
1521          */
1522         qtd = isp1760_qtd_alloc(priv, flags);
1523         if (!qtd)
1524                 return NULL;
1525
1526         list_add_tail(&qtd->qtd_list, head);
1527         qtd->urb = urb;
1528         urb->status = -EINPROGRESS;
1529
1530         token = 0;
1531         /* for split transactions, SplitXState initialized to zero */
1532
1533         len = urb->transfer_buffer_length;
1534         is_input = usb_pipein(urb->pipe);
1535         if (usb_pipecontrol(urb->pipe)) {
1536                 /* SETUP pid */
1537                 qtd_fill(qtd, urb->setup_packet,
1538                                 sizeof(struct usb_ctrlrequest),
1539                                 token | SETUP_PID);
1540
1541                 /* ... and always at least one more pid */
1542                 token ^= DATA_TOGGLE;
1543                 qtd_prev = qtd;
1544                 qtd = isp1760_qtd_alloc(priv, flags);
1545                 if (!qtd)
1546                         goto cleanup;
1547                 qtd->urb = urb;
1548                 qtd_prev->hw_next = qtd;
1549                 list_add_tail(&qtd->qtd_list, head);
1550
1551                 /* for zero length DATA stages, STATUS is always IN */
1552                 if (len == 0)
1553                         token |= IN_PID;
1554         }
1555
1556         /*
1557          * data transfer stage:  buffer setup
1558          */
1559         buf = urb->transfer_buffer;
1560
1561         if (is_input)
1562                 token |= IN_PID;
1563         else
1564                 token |= OUT_PID;
1565
1566         maxpacket = max_packet(usb_maxpacket(urb->dev, urb->pipe, !is_input));
1567
1568         /*
1569          * buffer gets wrapped in one or more qtds;
1570          * last one may be "short" (including zero len)
1571          * and may serve as a control status ack
1572          */
1573         for (;;) {
1574                 int this_qtd_len;
1575
1576                 if (!buf && len) {
1577                         /* XXX This looks like usb storage / SCSI bug */
1578                         printk(KERN_ERR "buf is null, dma is %08lx len is %d\n",
1579                                         (long unsigned)urb->transfer_dma, len);
1580                         WARN_ON(1);
1581                 }
1582
1583                 this_qtd_len = qtd_fill(qtd, buf, len, token);
1584                 len -= this_qtd_len;
1585                 buf += this_qtd_len;
1586
1587                 /* qh makes control packets use qtd toggle; maybe switch it */
1588                 if ((maxpacket & (this_qtd_len + (maxpacket - 1))) == 0)
1589                         token ^= DATA_TOGGLE;
1590
1591                 if (len <= 0)
1592                         break;
1593
1594                 qtd_prev = qtd;
1595                 qtd = isp1760_qtd_alloc(priv, flags);
1596                 if (!qtd)
1597                         goto cleanup;
1598                 qtd->urb = urb;
1599                 qtd_prev->hw_next = qtd;
1600                 list_add_tail(&qtd->qtd_list, head);
1601         }
1602
1603         /*
1604          * control requests may need a terminating data "status" ack;
1605          * bulk ones may need a terminating short packet (zero length).
1606          */
1607         if (urb->transfer_buffer_length != 0) {
1608                 int one_more = 0;
1609
1610                 if (usb_pipecontrol(urb->pipe)) {
1611                         one_more = 1;
1612                         /* "in" <--> "out"  */
1613                         token ^= IN_PID;
1614                         /* force DATA1 */
1615                         token |= DATA_TOGGLE;
1616                 } else if (usb_pipebulk(urb->pipe)
1617                                 && (urb->transfer_flags & URB_ZERO_PACKET)
1618                                 && !(urb->transfer_buffer_length % maxpacket)) {
1619                         one_more = 1;
1620                 }
1621                 if (one_more) {
1622                         qtd_prev = qtd;
1623                         qtd = isp1760_qtd_alloc(priv, flags);
1624                         if (!qtd)
1625                                 goto cleanup;
1626                         qtd->urb = urb;
1627                         qtd_prev->hw_next = qtd;
1628                         list_add_tail(&qtd->qtd_list, head);
1629
1630                         /* never any data in such packets */
1631                         qtd_fill(qtd, NULL, 0, token);
1632                 }
1633         }
1634
1635         qtd->status = URB_COMPLETE_NOTIFY;
1636         return head;
1637
1638 cleanup:
1639         qtd_list_free(priv, urb, head);
1640         return NULL;
1641 }
1642
1643 static int isp1760_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
1644                 gfp_t mem_flags)
1645 {
1646         struct isp1760_hcd *priv = hcd_to_priv(hcd);
1647         struct list_head qtd_list;
1648         packet_enqueue *pe;
1649
1650         INIT_LIST_HEAD(&qtd_list);
1651
1652         switch (usb_pipetype(urb->pipe)) {
1653         case PIPE_CONTROL:
1654         case PIPE_BULK:
1655
1656                 if (!qh_urb_transaction(priv, urb, &qtd_list, mem_flags))
1657                         return -ENOMEM;
1658                 pe =  enqueue_an_ATL_packet;
1659                 break;
1660
1661         case PIPE_INTERRUPT:
1662                 if (!qh_urb_transaction(priv, urb, &qtd_list, mem_flags))
1663                         return -ENOMEM;
1664                 pe = enqueue_an_INT_packet;
1665                 break;
1666
1667         case PIPE_ISOCHRONOUS:
1668                 printk(KERN_ERR "PIPE_ISOCHRONOUS ain't supported\n");
1669         default:
1670                 return -EPIPE;
1671         }
1672
1673         return isp1760_prepare_enqueue(priv, urb, &qtd_list, mem_flags, pe);
1674 }
1675
1676 static int isp1760_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
1677                 int status)
1678 {
1679         struct isp1760_hcd *priv = hcd_to_priv(hcd);
1680         struct inter_packet_info *ints;
1681         u32 i;
1682         u32 reg_base, or_reg, skip_reg;
1683         unsigned long flags;
1684         struct ptd ptd;
1685         packet_enqueue *pe;
1686
1687         switch (usb_pipetype(urb->pipe)) {
1688         case PIPE_ISOCHRONOUS:
1689                 return -EPIPE;
1690                 break;
1691
1692         case PIPE_INTERRUPT:
1693                 ints = priv->int_ints;
1694                 reg_base = INT_REGS_OFFSET;
1695                 or_reg = HC_INT_IRQ_MASK_OR_REG;
1696                 skip_reg = HC_INT_PTD_SKIPMAP_REG;
1697                 pe = enqueue_an_INT_packet;
1698                 break;
1699
1700         default:
1701                 ints = priv->atl_ints;
1702                 reg_base = ATL_REGS_OFFSET;
1703                 or_reg = HC_ATL_IRQ_MASK_OR_REG;
1704                 skip_reg = HC_ATL_PTD_SKIPMAP_REG;
1705                 pe =  enqueue_an_ATL_packet;
1706                 break;
1707         }
1708
1709         memset(&ptd, 0, sizeof(ptd));
1710         spin_lock_irqsave(&priv->lock, flags);
1711
1712         for (i = 0; i < 32; i++) {
1713                 if (ints->urb == urb) {
1714                         u32 skip_map;
1715                         u32 or_map;
1716                         struct isp1760_qtd *qtd;
1717                         struct isp1760_qh *qh = ints->qh;
1718
1719                         skip_map = isp1760_readl(hcd->regs + skip_reg);
1720                         skip_map |= 1 << i;
1721                         isp1760_writel(skip_map, hcd->regs + skip_reg);
1722
1723                         or_map = isp1760_readl(hcd->regs + or_reg);
1724                         or_map &= ~(1 << i);
1725                         isp1760_writel(or_map, hcd->regs + or_reg);
1726
1727                         priv_write_copy(priv, (u32 *)&ptd, hcd->regs + reg_base
1728                                         + i * sizeof(ptd), sizeof(ptd));
1729                         qtd = ints->qtd;
1730                         qtd = clean_up_qtdlist(qtd);
1731
1732                         free_mem(priv, ints->payload);
1733
1734                         ints->urb = NULL;
1735                         ints->qh = NULL;
1736                         ints->qtd = NULL;
1737                         ints->data_buffer = NULL;
1738                         ints->payload = 0;
1739
1740                         isp1760_urb_done(priv, urb, status);
1741                         if (qtd)
1742                                 pe(hcd, qh, qtd);
1743                         break;
1744
1745                 } else if (ints->qtd) {
1746                         struct isp1760_qtd *qtd, *prev_qtd = ints->qtd;
1747
1748                         for (qtd = ints->qtd->hw_next; qtd; qtd = qtd->hw_next) {
1749                                 if (qtd->urb == urb) {
1750                                         prev_qtd->hw_next = clean_up_qtdlist(qtd);
1751                                         isp1760_urb_done(priv, urb, status);
1752                                         break;
1753                                 }
1754                                 prev_qtd = qtd;
1755                         }
1756                         /* we found the urb before the end of the list */
1757                         if (qtd)
1758                                 break;
1759                 }
1760                 ints++;
1761         }
1762
1763         spin_unlock_irqrestore(&priv->lock, flags);
1764         return 0;
1765 }
1766
1767 static irqreturn_t isp1760_irq(struct usb_hcd *usb_hcd)
1768 {
1769         struct isp1760_hcd *priv = hcd_to_priv(usb_hcd);
1770         u32 imask;
1771         irqreturn_t irqret = IRQ_NONE;
1772
1773         spin_lock(&priv->lock);
1774
1775         if (!(usb_hcd->state & HC_STATE_RUNNING))
1776                 goto leave;
1777
1778         imask = isp1760_readl(usb_hcd->regs + HC_INTERRUPT_REG);
1779         if (unlikely(!imask))
1780                 goto leave;
1781
1782         isp1760_writel(imask, usb_hcd->regs + HC_INTERRUPT_REG);
1783         if (imask & (HC_ATL_INT | HC_SOT_INT))
1784                 do_atl_int(usb_hcd);
1785
1786         if (imask & HC_INTL_INT)
1787                 do_intl_int(usb_hcd);
1788
1789         irqret = IRQ_HANDLED;
1790 leave:
1791         spin_unlock(&priv->lock);
1792         return irqret;
1793 }
1794
1795 static int isp1760_hub_status_data(struct usb_hcd *hcd, char *buf)
1796 {
1797         struct isp1760_hcd *priv = hcd_to_priv(hcd);
1798         u32 temp, status = 0;
1799         u32 mask;
1800         int retval = 1;
1801         unsigned long flags;
1802
1803         /* if !USB_SUSPEND, root hub timers won't get shut down ... */
1804         if (!HC_IS_RUNNING(hcd->state))
1805                 return 0;
1806
1807         /* init status to no-changes */
1808         buf[0] = 0;
1809         mask = PORT_CSC;
1810
1811         spin_lock_irqsave(&priv->lock, flags);
1812         temp = isp1760_readl(hcd->regs + HC_PORTSC1);
1813
1814         if (temp & PORT_OWNER) {
1815                 if (temp & PORT_CSC) {
1816                         temp &= ~PORT_CSC;
1817                         isp1760_writel(temp, hcd->regs + HC_PORTSC1);
1818                         goto done;
1819                 }
1820         }
1821
1822         /*
1823          * Return status information even for ports with OWNER set.
1824          * Otherwise khubd wouldn't see the disconnect event when a
1825          * high-speed device is switched over to the companion
1826          * controller by the user.
1827          */
1828
1829         if ((temp & mask) != 0
1830                         || ((temp & PORT_RESUME) != 0
1831                                 && time_after_eq(jiffies,
1832                                         priv->reset_done))) {
1833                 buf [0] |= 1 << (0 + 1);
1834                 status = STS_PCD;
1835         }
1836         /* FIXME autosuspend idle root hubs */
1837 done:
1838         spin_unlock_irqrestore(&priv->lock, flags);
1839         return status ? retval : 0;
1840 }
1841
1842 static void isp1760_hub_descriptor(struct isp1760_hcd *priv,
1843                 struct usb_hub_descriptor *desc)
1844 {
1845         int ports = HCS_N_PORTS(priv->hcs_params);
1846         u16 temp;
1847
1848         desc->bDescriptorType = 0x29;
1849         /* priv 1.0, 2.3.9 says 20ms max */
1850         desc->bPwrOn2PwrGood = 10;
1851         desc->bHubContrCurrent = 0;
1852
1853         desc->bNbrPorts = ports;
1854         temp = 1 + (ports / 8);
1855         desc->bDescLength = 7 + 2 * temp;
1856
1857         /* two bitmaps:  ports removable, and usb 1.0 legacy PortPwrCtrlMask */
1858         memset(&desc->bitmap[0], 0, temp);
1859         memset(&desc->bitmap[temp], 0xff, temp);
1860
1861         /* per-port overcurrent reporting */
1862         temp = 0x0008;
1863         if (HCS_PPC(priv->hcs_params))
1864                 /* per-port power control */
1865                 temp |= 0x0001;
1866         else
1867                 /* no power switching */
1868                 temp |= 0x0002;
1869         desc->wHubCharacteristics = cpu_to_le16(temp);
1870 }
1871
1872 #define PORT_WAKE_BITS  (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
1873
1874 static int check_reset_complete(struct isp1760_hcd *priv, int index,
1875                 u32 __iomem *status_reg, int port_status)
1876 {
1877         if (!(port_status & PORT_CONNECT))
1878                 return port_status;
1879
1880         /* if reset finished and it's still not enabled -- handoff */
1881         if (!(port_status & PORT_PE)) {
1882
1883                 printk(KERN_ERR "port %d full speed --> companion\n",
1884                         index + 1);
1885
1886                 port_status |= PORT_OWNER;
1887                 port_status &= ~PORT_RWC_BITS;
1888                 isp1760_writel(port_status, status_reg);
1889
1890         } else
1891                 printk(KERN_ERR "port %d high speed\n", index + 1);
1892
1893         return port_status;
1894 }
1895
1896 static int isp1760_hub_control(struct usb_hcd *hcd, u16 typeReq,
1897                 u16 wValue, u16 wIndex, char *buf, u16 wLength)
1898 {
1899         struct isp1760_hcd *priv = hcd_to_priv(hcd);
1900         int ports = HCS_N_PORTS(priv->hcs_params);
1901         u32 __iomem *status_reg = hcd->regs + HC_PORTSC1;
1902         u32 temp, status;
1903         unsigned long flags;
1904         int retval = 0;
1905         unsigned selector;
1906
1907         /*
1908          * FIXME:  support SetPortFeatures USB_PORT_FEAT_INDICATOR.
1909          * HCS_INDICATOR may say we can change LEDs to off/amber/green.
1910          * (track current state ourselves) ... blink for diagnostics,
1911          * power, "this is the one", etc.  EHCI spec supports this.
1912          */
1913
1914         spin_lock_irqsave(&priv->lock, flags);
1915         switch (typeReq) {
1916         case ClearHubFeature:
1917                 switch (wValue) {
1918                 case C_HUB_LOCAL_POWER:
1919                 case C_HUB_OVER_CURRENT:
1920                         /* no hub-wide feature/status flags */
1921                         break;
1922                 default:
1923                         goto error;
1924                 }
1925                 break;
1926         case ClearPortFeature:
1927                 if (!wIndex || wIndex > ports)
1928                         goto error;
1929                 wIndex--;
1930                 temp = isp1760_readl(status_reg);
1931
1932                 /*
1933                  * Even if OWNER is set, so the port is owned by the
1934                  * companion controller, khubd needs to be able to clear
1935                  * the port-change status bits (especially
1936                  * USB_PORT_STAT_C_CONNECTION).
1937                  */
1938
1939                 switch (wValue) {
1940                 case USB_PORT_FEAT_ENABLE:
1941                         isp1760_writel(temp & ~PORT_PE, status_reg);
1942                         break;
1943                 case USB_PORT_FEAT_C_ENABLE:
1944                         /* XXX error? */
1945                         break;
1946                 case USB_PORT_FEAT_SUSPEND:
1947                         if (temp & PORT_RESET)
1948                                 goto error;
1949
1950                         if (temp & PORT_SUSPEND) {
1951                                 if ((temp & PORT_PE) == 0)
1952                                         goto error;
1953                                 /* resume signaling for 20 msec */
1954                                 temp &= ~(PORT_RWC_BITS);
1955                                 isp1760_writel(temp | PORT_RESUME,
1956                                                 status_reg);
1957                                 priv->reset_done = jiffies +
1958                                         msecs_to_jiffies(20);
1959                         }
1960                         break;
1961                 case USB_PORT_FEAT_C_SUSPEND:
1962                         /* we auto-clear this feature */
1963                         break;
1964                 case USB_PORT_FEAT_POWER:
1965                         if (HCS_PPC(priv->hcs_params))
1966                                 isp1760_writel(temp & ~PORT_POWER, status_reg);
1967                         break;
1968                 case USB_PORT_FEAT_C_CONNECTION:
1969                         isp1760_writel(temp | PORT_CSC,
1970                                         status_reg);
1971                         break;
1972                 case USB_PORT_FEAT_C_OVER_CURRENT:
1973                         /* XXX error ?*/
1974                         break;
1975                 case USB_PORT_FEAT_C_RESET:
1976                         /* GetPortStatus clears reset */
1977                         break;
1978                 default:
1979                         goto error;
1980                 }
1981                 isp1760_readl(hcd->regs + HC_USBCMD);
1982                 break;
1983         case GetHubDescriptor:
1984                 isp1760_hub_descriptor(priv, (struct usb_hub_descriptor *)
1985                         buf);
1986                 break;
1987         case GetHubStatus:
1988                 /* no hub-wide feature/status flags */
1989                 memset(buf, 0, 4);
1990                 break;
1991         case GetPortStatus:
1992                 if (!wIndex || wIndex > ports)
1993                         goto error;
1994                 wIndex--;
1995                 status = 0;
1996                 temp = isp1760_readl(status_reg);
1997
1998                 /* wPortChange bits */
1999                 if (temp & PORT_CSC)
2000                         status |= USB_PORT_STAT_C_CONNECTION << 16;
2001
2002
2003                 /* whoever resumes must GetPortStatus to complete it!! */
2004                 if (temp & PORT_RESUME) {
2005                         printk(KERN_ERR "Port resume should be skipped.\n");
2006
2007                         /* Remote Wakeup received? */
2008                         if (!priv->reset_done) {
2009                                 /* resume signaling for 20 msec */
2010                                 priv->reset_done = jiffies
2011                                                 + msecs_to_jiffies(20);
2012                                 /* check the port again */
2013                                 mod_timer(&priv_to_hcd(priv)->rh_timer,
2014                                                 priv->reset_done);
2015                         }
2016
2017                         /* resume completed? */
2018                         else if (time_after_eq(jiffies,
2019                                         priv->reset_done)) {
2020                                 status |= USB_PORT_STAT_C_SUSPEND << 16;
2021                                 priv->reset_done = 0;
2022
2023                                 /* stop resume signaling */
2024                                 temp = isp1760_readl(status_reg);
2025                                 isp1760_writel(
2026                                         temp & ~(PORT_RWC_BITS | PORT_RESUME),
2027                                         status_reg);
2028                                 retval = handshake(priv, status_reg,
2029                                            PORT_RESUME, 0, 2000 /* 2msec */);
2030                                 if (retval != 0) {
2031                                         isp1760_err(priv,
2032                                                 "port %d resume error %d\n",
2033                                                 wIndex + 1, retval);
2034                                         goto error;
2035                                 }
2036                                 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
2037                         }
2038                 }
2039
2040                 /* whoever resets must GetPortStatus to complete it!! */
2041                 if ((temp & PORT_RESET)
2042                                 && time_after_eq(jiffies,
2043                                         priv->reset_done)) {
2044                         status |= USB_PORT_STAT_C_RESET << 16;
2045                         priv->reset_done = 0;
2046
2047                         /* force reset to complete */
2048                         isp1760_writel(temp & ~PORT_RESET,
2049                                         status_reg);
2050                         /* REVISIT:  some hardware needs 550+ usec to clear
2051                          * this bit; seems too long to spin routinely...
2052                          */
2053                         retval = handshake(priv, status_reg,
2054                                         PORT_RESET, 0, 750);
2055                         if (retval != 0) {
2056                                 isp1760_err(priv, "port %d reset error %d\n",
2057                                                 wIndex + 1, retval);
2058                                 goto error;
2059                         }
2060
2061                         /* see what we found out */
2062                         temp = check_reset_complete(priv, wIndex, status_reg,
2063                                         isp1760_readl(status_reg));
2064                 }
2065                 /*
2066                  * Even if OWNER is set, there's no harm letting khubd
2067                  * see the wPortStatus values (they should all be 0 except
2068                  * for PORT_POWER anyway).
2069                  */
2070
2071                 if (temp & PORT_OWNER)
2072                         printk(KERN_ERR "Warning: PORT_OWNER is set\n");
2073
2074                 if (temp & PORT_CONNECT) {
2075                         status |= USB_PORT_STAT_CONNECTION;
2076                         /* status may be from integrated TT */
2077                         status |= ehci_port_speed(priv, temp);
2078                 }
2079                 if (temp & PORT_PE)
2080                         status |= USB_PORT_STAT_ENABLE;
2081                 if (temp & (PORT_SUSPEND|PORT_RESUME))
2082                         status |= USB_PORT_STAT_SUSPEND;
2083                 if (temp & PORT_RESET)
2084                         status |= USB_PORT_STAT_RESET;
2085                 if (temp & PORT_POWER)
2086                         status |= USB_PORT_STAT_POWER;
2087
2088                 put_unaligned(cpu_to_le32(status), (__le32 *) buf);
2089                 break;
2090         case SetHubFeature:
2091                 switch (wValue) {
2092                 case C_HUB_LOCAL_POWER:
2093                 case C_HUB_OVER_CURRENT:
2094                         /* no hub-wide feature/status flags */
2095                         break;
2096                 default:
2097                         goto error;
2098                 }
2099                 break;
2100         case SetPortFeature:
2101                 selector = wIndex >> 8;
2102                 wIndex &= 0xff;
2103                 if (!wIndex || wIndex > ports)
2104                         goto error;
2105                 wIndex--;
2106                 temp = isp1760_readl(status_reg);
2107                 if (temp & PORT_OWNER)
2108                         break;
2109
2110 /*              temp &= ~PORT_RWC_BITS; */
2111                 switch (wValue) {
2112                 case USB_PORT_FEAT_ENABLE:
2113                         isp1760_writel(temp | PORT_PE, status_reg);
2114                         break;
2115
2116                 case USB_PORT_FEAT_SUSPEND:
2117                         if ((temp & PORT_PE) == 0
2118                                         || (temp & PORT_RESET) != 0)
2119                                 goto error;
2120
2121                         isp1760_writel(temp | PORT_SUSPEND, status_reg);
2122                         break;
2123                 case USB_PORT_FEAT_POWER:
2124                         if (HCS_PPC(priv->hcs_params))
2125                                 isp1760_writel(temp | PORT_POWER,
2126                                                 status_reg);
2127                         break;
2128                 case USB_PORT_FEAT_RESET:
2129                         if (temp & PORT_RESUME)
2130                                 goto error;
2131                         /* line status bits may report this as low speed,
2132                          * which can be fine if this root hub has a
2133                          * transaction translator built in.
2134                          */
2135                         if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
2136                                         && PORT_USB11(temp)) {
2137                                 temp |= PORT_OWNER;
2138                         } else {
2139                                 temp |= PORT_RESET;
2140                                 temp &= ~PORT_PE;
2141
2142                                 /*
2143                                  * caller must wait, then call GetPortStatus
2144                                  * usb 2.0 spec says 50 ms resets on root
2145                                  */
2146                                 priv->reset_done = jiffies +
2147                                         msecs_to_jiffies(50);
2148                         }
2149                         isp1760_writel(temp, status_reg);
2150                         break;
2151                 default:
2152                         goto error;
2153                 }
2154                 isp1760_readl(hcd->regs + HC_USBCMD);
2155                 break;
2156
2157         default:
2158 error:
2159                 /* "stall" on error */
2160                 retval = -EPIPE;
2161         }
2162         spin_unlock_irqrestore(&priv->lock, flags);
2163         return retval;
2164 }
2165
2166 static void isp1760_endpoint_disable(struct usb_hcd *usb_hcd,
2167                 struct usb_host_endpoint *ep)
2168 {
2169         struct isp1760_hcd *priv = hcd_to_priv(usb_hcd);
2170         struct isp1760_qh *qh;
2171         struct isp1760_qtd *qtd;
2172         unsigned long flags;
2173
2174         spin_lock_irqsave(&priv->lock, flags);
2175         qh = ep->hcpriv;
2176         if (!qh)
2177                 goto out;
2178
2179         ep->hcpriv = NULL;
2180         do {
2181                 /* more than entry might get removed */
2182                 if (list_empty(&qh->qtd_list))
2183                         break;
2184
2185                 qtd = list_first_entry(&qh->qtd_list, struct isp1760_qtd,
2186                                 qtd_list);
2187
2188                 if (qtd->status & URB_ENQUEUED) {
2189
2190                         spin_unlock_irqrestore(&priv->lock, flags);
2191                         isp1760_urb_dequeue(usb_hcd, qtd->urb, -ECONNRESET);
2192                         spin_lock_irqsave(&priv->lock, flags);
2193                 } else {
2194                         struct urb *urb;
2195
2196                         urb = qtd->urb;
2197                         clean_up_qtdlist(qtd);
2198                         isp1760_urb_done(priv, urb, -ECONNRESET);
2199                 }
2200         } while (1);
2201
2202         qh_destroy(qh);
2203         /* remove requests and leak them.
2204          * ATL are pretty fast done, INT could take a while...
2205          * The latter shoule be removed
2206          */
2207 out:
2208         spin_unlock_irqrestore(&priv->lock, flags);
2209 }
2210
2211 static int isp1760_get_frame(struct usb_hcd *hcd)
2212 {
2213         struct isp1760_hcd *priv = hcd_to_priv(hcd);
2214         u32 fr;
2215
2216         fr = isp1760_readl(hcd->regs + HC_FRINDEX);
2217         return (fr >> 3) % priv->periodic_size;
2218 }
2219
2220 static void isp1760_stop(struct usb_hcd *hcd)
2221 {
2222         struct isp1760_hcd *priv = hcd_to_priv(hcd);
2223         u32 temp;
2224
2225         isp1760_hub_control(hcd, ClearPortFeature, USB_PORT_FEAT_POWER, 1,
2226                         NULL, 0);
2227         mdelay(20);
2228
2229         spin_lock_irq(&priv->lock);
2230         ehci_reset(priv);
2231         /* Disable IRQ */
2232         temp = isp1760_readl(hcd->regs + HC_HW_MODE_CTRL);
2233         isp1760_writel(temp &= ~HW_GLOBAL_INTR_EN, hcd->regs + HC_HW_MODE_CTRL);
2234         spin_unlock_irq(&priv->lock);
2235
2236         isp1760_writel(0, hcd->regs + HC_CONFIGFLAG);
2237 }
2238
2239 static void isp1760_shutdown(struct usb_hcd *hcd)
2240 {
2241         u32 command, temp;
2242
2243         isp1760_stop(hcd);
2244         temp = isp1760_readl(hcd->regs + HC_HW_MODE_CTRL);
2245         isp1760_writel(temp &= ~HW_GLOBAL_INTR_EN, hcd->regs + HC_HW_MODE_CTRL);
2246
2247         command = isp1760_readl(hcd->regs + HC_USBCMD);
2248         command &= ~CMD_RUN;
2249         isp1760_writel(command, hcd->regs + HC_USBCMD);
2250 }
2251
2252 static const struct hc_driver isp1760_hc_driver = {
2253         .description            = "isp1760-hcd",
2254         .product_desc           = "NXP ISP1760 USB Host Controller",
2255         .hcd_priv_size          = sizeof(struct isp1760_hcd),
2256         .irq                    = isp1760_irq,
2257         .flags                  = HCD_MEMORY | HCD_USB2,
2258         .reset                  = isp1760_hc_setup,
2259         .start                  = isp1760_run,
2260         .stop                   = isp1760_stop,
2261         .shutdown               = isp1760_shutdown,
2262         .urb_enqueue            = isp1760_urb_enqueue,
2263         .urb_dequeue            = isp1760_urb_dequeue,
2264         .endpoint_disable       = isp1760_endpoint_disable,
2265         .get_frame_number       = isp1760_get_frame,
2266         .hub_status_data        = isp1760_hub_status_data,
2267         .hub_control            = isp1760_hub_control,
2268 };
2269
2270 int __init init_kmem_once(void)
2271 {
2272         qtd_cachep = kmem_cache_create("isp1760_qtd",
2273                         sizeof(struct isp1760_qtd), 0, SLAB_TEMPORARY |
2274                         SLAB_MEM_SPREAD, NULL);
2275
2276         if (!qtd_cachep)
2277                 return -ENOMEM;
2278
2279         qh_cachep = kmem_cache_create("isp1760_qh", sizeof(struct isp1760_qh),
2280                         0, SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
2281
2282         if (!qh_cachep) {
2283                 kmem_cache_destroy(qtd_cachep);
2284                 return -ENOMEM;
2285         }
2286
2287         return 0;
2288 }
2289
2290 void deinit_kmem_cache(void)
2291 {
2292         kmem_cache_destroy(qtd_cachep);
2293         kmem_cache_destroy(qh_cachep);
2294 }
2295
2296 struct usb_hcd *isp1760_register(phys_addr_t res_start, resource_size_t res_len,
2297                                  int irq, unsigned long irqflags,
2298                                  struct device *dev, const char *busname,
2299                                  unsigned int devflags)
2300 {
2301         struct usb_hcd *hcd;
2302         struct isp1760_hcd *priv;
2303         int ret;
2304
2305         if (usb_disabled())
2306                 return ERR_PTR(-ENODEV);
2307
2308         /* prevent usb-core allocating DMA pages */
2309         dev->dma_mask = NULL;
2310
2311         hcd = usb_create_hcd(&isp1760_hc_driver, dev, dev_name(dev));
2312         if (!hcd)
2313                 return ERR_PTR(-ENOMEM);
2314
2315         priv = hcd_to_priv(hcd);
2316         priv->devflags = devflags;
2317         init_memory(priv);
2318         hcd->regs = ioremap(res_start, res_len);
2319         if (!hcd->regs) {
2320                 ret = -EIO;
2321                 goto err_put;
2322         }
2323
2324         hcd->irq = irq;
2325         hcd->rsrc_start = res_start;
2326         hcd->rsrc_len = res_len;
2327
2328         ret = usb_add_hcd(hcd, irq, irqflags);
2329         if (ret)
2330                 goto err_unmap;
2331
2332         return hcd;
2333
2334 err_unmap:
2335          iounmap(hcd->regs);
2336
2337 err_put:
2338          usb_put_hcd(hcd);
2339
2340          return ERR_PTR(ret);
2341 }
2342
2343 MODULE_DESCRIPTION("Driver for the ISP1760 USB-controller from NXP");
2344 MODULE_AUTHOR("Sebastian Siewior <bigeasy@linuxtronix.de>");
2345 MODULE_LICENSE("GPL v2");