2 * Driver for the NXP ISP1760 chip
4 * However, the code might contain some bugs. What doesn't work for sure is:
7 e The interrupt line is configured as active low, level.
9 * (c) 2007 Sebastian Siewior <bigeasy@linutronix.de>
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>
22 #include <asm/unaligned.h>
23 #include <asm/cacheflush.h>
25 #include "isp1760-hcd.h"
27 static struct kmem_cache *qtd_cachep;
28 static struct kmem_cache *qh_cachep;
33 struct inter_packet_info atl_ints[32];
34 struct inter_packet_info int_ints[32];
35 struct memory_chunk memory_pool[BLOCKS];
38 /* periodic schedule support */
39 #define DEFAULT_I_TDPS 1024
40 unsigned periodic_size;
42 unsigned long reset_done;
43 unsigned long next_statechange;
44 unsigned int devflags;
47 static inline struct isp1760_hcd *hcd_to_priv(struct usb_hcd *hcd)
49 return (struct isp1760_hcd *) (hcd->hcd_priv);
51 static inline struct usb_hcd *priv_to_hcd(struct isp1760_hcd *priv)
53 return container_of((void *) priv, struct usb_hcd, hcd_priv);
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 */
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" */
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)
84 struct isp1760_qtd *hw_next;
89 /* the rest is HCD-private */
90 struct list_head qtd_list;
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)
103 /* first part defined by EHCI spec */
104 struct list_head qtd_list;
105 struct isp1760_hcd *priv;
107 /* periodic schedule info */
108 unsigned short period; /* polling interval */
109 struct usb_device *dev;
115 #define ehci_port_speed(priv, portsc) USB_PORT_STAT_HIGH_SPEED
117 static unsigned int isp1760_readl(__u32 __iomem *regs)
122 static void isp1760_writel(const unsigned int val, __u32 __iomem *regs)
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
131 static void priv_read_copy(struct isp1760_hcd *priv, u32 *src,
132 __u32 __iomem *dst, u32 len)
138 printk(KERN_ERR "ERROR: buffer: %p len: %d\n", src, len);
143 *src = __raw_readl(dst);
152 /* in case we have 3, 2 or 1 by left. The dst buffer may not be fully
155 val = isp1760_readl(dst);
167 static void priv_write_copy(const struct isp1760_hcd *priv, const u32 *src,
168 __u32 __iomem *dst, u32 len)
171 __raw_writel(*src, dst);
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
183 __raw_writel(*src, dst);
186 /* memory management of the 60kb on the chip from 0x1000 to 0xffff */
187 static void init_memory(struct isp1760_hcd *priv)
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;
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;
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;
216 BUG_ON(payload - priv->memory_pool[i - 1].size > PAYLOAD_SIZE);
219 static u32 alloc_mem(struct isp1760_hcd *priv, u32 size)
224 return ISP1760_NULL_POINTER;
226 for (i = 0; i < BLOCKS; i++) {
227 if (priv->memory_pool[i].size >= size &&
228 priv->memory_pool[i].free) {
230 priv->memory_pool[i].free = 0;
231 return priv->memory_pool[i].start;
235 printk(KERN_ERR "ISP1760 MEM: can not allocate %d bytes of memory\n",
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);
243 /* XXX maybe -ENOMEM could be possible */
248 static void free_mem(struct isp1760_hcd *priv, u32 mem)
252 if (mem == ISP1760_NULL_POINTER)
255 for (i = 0; i < BLOCKS; i++) {
256 if (priv->memory_pool[i].start == mem) {
258 BUG_ON(priv->memory_pool[i].free);
260 priv->memory_pool[i].free = 1;
265 printk(KERN_ERR "Trying to free not-here-allocated memory :%08x\n",
270 static void isp1760_init_regs(struct usb_hcd *hcd)
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);
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);
288 static int handshake(struct isp1760_hcd *priv, void __iomem *ptr,
289 u32 mask, u32 done, int usec)
294 result = isp1760_readl(ptr);
306 /* reset a non-running (STS_HALT == 1) controller */
307 static int ehci_reset(struct isp1760_hcd *priv)
310 struct usb_hcd *hcd = priv_to_hcd(priv);
311 u32 command = isp1760_readl(hcd->regs + HC_USBCMD);
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);
322 static void qh_destroy(struct isp1760_qh *qh)
324 BUG_ON(!list_empty(&qh->qtd_list));
325 kmem_cache_free(qh_cachep, qh);
328 static struct isp1760_qh *isp1760_qh_alloc(struct isp1760_hcd *priv,
331 struct isp1760_qh *qh;
333 qh = kmem_cache_zalloc(qh_cachep, flags);
337 INIT_LIST_HEAD(&qh->qtd_list);
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 */
350 /* one-time init, only for memory state */
351 static int priv_init(struct usb_hcd *hcd)
353 struct isp1760_hcd *priv = hcd_to_priv(hcd);
356 spin_lock_init(&priv->lock);
359 * hw default: 1K periodic list heads, one per frame.
360 * periodic_size can shrink by USBCMD update if hcc_params allows.
362 priv->periodic_size = DEFAULT_I_TDPS;
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))
369 else /* N microframes cached */
370 priv->i_thresh = 2 + HCC_ISOC_THRES(hcc_params);
375 static int isp1760_hc_setup(struct usb_hcd *hcd)
377 struct isp1760_hcd *priv = hcd_to_priv(hcd);
381 /* Setup HW Mode Control: This assumes a level active-low interrupt */
382 hwmode = HW_DATA_BUS_32BIT;
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;
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
402 isp1760_writel(hwmode, hcd->regs + HC_HW_MODE_CTRL);
403 isp1760_writel(hwmode, hcd->regs + HC_HW_MODE_CTRL);
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");
415 isp1760_init_regs(hcd);
418 isp1760_writel(SW_RESET_RESET_ALL, hcd->regs + HC_RESET_REG);
421 isp1760_writel(SW_RESET_RESET_HC, hcd->regs + HC_RESET_REG);
424 result = ehci_reset(priv);
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");
436 isp1760_writel(hwmode | ALL_ATX_RESET, hcd->regs + HC_HW_MODE_CTRL);
438 isp1760_writel(hwmode, hcd->regs + HC_HW_MODE_CTRL);
440 isp1760_writel(INTERRUPT_ENABLE_MASK, hcd->regs + HC_INTERRUPT_REG);
441 isp1760_writel(INTERRUPT_ENABLE_MASK, hcd->regs + HC_INTERRUPT_ENABLE);
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
449 isp1760_writel(PORT1_POWER | PORT1_INIT2,
450 hcd->regs + HC_PORT1_CTRL);
453 priv->hcs_params = isp1760_readl(hcd->regs + HC_HCSPARAMS);
455 return priv_init(hcd);
458 static void isp1760_init_maps(struct usb_hcd *hcd)
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);
466 static void isp1760_enable_interrupts(struct usb_hcd *hcd)
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);
477 static int isp1760_run(struct usb_hcd *hcd)
479 struct isp1760_hcd *priv = hcd_to_priv(hcd);
485 hcd->uses_new_polling = 1;
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);
492 command = isp1760_readl(hcd->regs + HC_USBCMD);
493 command &= ~(CMD_LRESET|CMD_RESET);
495 isp1760_writel(command, hcd->regs + HC_USBCMD);
497 retval = handshake(priv, hcd->regs + HC_USBCMD, CMD_RUN, CMD_RUN,
504 * Spec says to write FLAG_CF as last config action, priv code grabs
505 * the semaphore while doing so.
507 down_write(&ehci_cf_port_reset_rwsem);
508 isp1760_writel(FLAG_CF, hcd->regs + HC_CONFIGFLAG);
510 retval = handshake(priv, hcd->regs + HC_CONFIGFLAG, FLAG_CF, FLAG_CF,
512 up_write(&ehci_cf_port_reset_rwsem);
516 chipid = isp1760_readl(hcd->regs + HC_CHIP_ID_REG);
517 isp1760_info(priv, "USB ISP %04x HW rev. %d started\n", chipid & 0xffff,
520 /* PTD Register Init Part 2, Step 28 */
522 isp1760_init_maps(hcd);
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.
531 static u32 base_to_chip(u32 base)
533 return ((base - 0x400) >> 3);
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)
548 u32 nak = NAK_COUNTER;
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);
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;
563 dw1 |= PTD_DEVICE_ADDR(usb_pipedevice(urb->pipe));
565 pid_code = qtd->packet_type;
566 dw1 |= PTD_PID_TOKEN(pid_code);
568 if (usb_pipebulk(urb->pipe))
569 dw1 |= PTD_TRANS_BULK;
570 else if (usb_pipeint(urb->pipe))
571 dw1 |= PTD_TRANS_INT;
573 if (urb->dev->speed != USB_SPEED_HIGH) {
574 /* split transaction */
576 dw1 |= PTD_TRANS_SPLIT;
577 if (urb->dev->speed == USB_SPEED_LOW)
578 dw1 |= PTD_SE_USB_LOSPEED;
580 dw1 |= PTD_PORT_NUM(urb->dev->ttport);
581 dw1 |= PTD_HUB_NUM(urb->dev->tt->hub->devnum);
583 /* SE bit for Split INT transfers */
584 if (usb_pipeint(urb->pipe) &&
585 (urb->dev->speed == USB_SPEED_LOW))
592 dw0 |= PTD_MULTI(multi);
593 if (usb_pipecontrol(urb->pipe) || usb_pipebulk(urb->pipe))
600 dw2 |= PTD_DATA_START_ADDR(base_to_chip(payload));
601 dw2 |= PTD_RL_CNT(rl);
602 dw3 |= PTD_NAC_CNT(nak);
605 if (usb_pipecontrol(urb->pipe))
606 dw3 |= PTD_DATA_TOGGLE(qtd->toggle);
613 dw3 |= PTD_CERR(ERR_COUNTER);
615 memset(ptd, 0, sizeof(*ptd));
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);
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)
634 maxpacket = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
635 multi = 1 + ((maxpacket >> 11) & 0x3);
637 /* length of the data per uframe */
638 maxpacket = multi * maxpacket;
640 numberofusofs = urb->transfer_buffer_length / maxpacket;
641 if (urb->transfer_buffer_length % maxpacket)
646 for (i = 0; i < numberofusofs; i++) {
651 if (urb->dev->speed != USB_SPEED_HIGH) {
653 ptd->dw5 = cpu_to_le32(0x1c);
655 if (qh->period >= 32)
656 period = qh->period / 2;
663 period = qh->period/8;
670 if (qh->period >= 8) {
671 /* millisecond period */
672 period = (period << 3);
674 /* usof based tranmsfers */
675 /* minimum 4 usofs */
680 ptd->dw2 |= cpu_to_le32(period);
681 ptd->dw4 = cpu_to_le32(usof);
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)
688 transform_into_atl(priv, qh, qtd, urb, payload, ptd);
689 transform_add_int(priv, qh, qtd, urb, payload, ptd);
692 static int qtd_fill(struct isp1760_qtd *qtd, void *databuffer, size_t len,
697 qtd->data_buffer = databuffer;
698 qtd->packet_type = GET_QTD_TOKEN_TYPE(token);
699 qtd->toggle = GET_DATA_TOGGLE(token);
701 if (len > HC_ATL_PL_SIZE)
702 count = HC_ATL_PL_SIZE;
710 static int check_error(struct ptd *ptd)
715 dw3 = le32_to_cpu(ptd->dw3);
716 if (dw3 & DW3_HALT_BIT) {
719 if (dw3 & DW3_ERROR_BIT)
720 pr_err("error bit is set in DW3\n");
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);
732 static void check_int_err_status(u32 dw4)
738 for (i = 0; i < 8; i++) {
741 printk(KERN_ERR "ERROR: under run , %d\n", i);
745 printk(KERN_ERR "ERROR: transaction error, %d\n", i);
749 printk(KERN_ERR "ERROR: babble error, %d\n", i);
756 static void enqueue_one_qtd(struct isp1760_qtd *qtd, struct isp1760_hcd *priv,
760 struct usb_hcd *hcd = priv_to_hcd(priv);
762 token = qtd->packet_type;
764 if (qtd->length && (qtd->length <= HC_ATL_PL_SIZE)) {
770 priv_write_copy(priv, qtd->data_buffer,
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)
782 struct usb_hcd *hcd = priv_to_hcd(priv);
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);
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;
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)
802 struct usb_hcd *hcd = priv_to_hcd(priv);
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);
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;
817 static void enqueue_an_ATL_packet(struct usb_hcd *hcd, struct isp1760_qh *qh,
818 struct isp1760_qtd *qtd)
820 struct isp1760_hcd *priv = hcd_to_priv(hcd);
821 u32 skip_map, or_map;
824 u32 atl_regs, payload;
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).
835 skip_map = isp1760_readl(hcd->regs + HC_ATL_PTD_SKIPMAP_REG);
838 slot = __ffs(skip_map);
839 queue_entry = 1 << slot;
841 atl_regs = ATL_REGS_OFFSET + slot * sizeof(struct ptd);
843 payload = alloc_mem(priv, qtd->length);
845 enqueue_one_atl_qtd(atl_regs, payload, priv, qh, qtd->urb, slot, qtd);
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);
851 skip_map &= ~queue_entry;
852 isp1760_writel(skip_map, hcd->regs + HC_ATL_PTD_SKIPMAP_REG);
855 if (priv->atl_queued == 2)
856 isp1760_writel(INTERRUPT_ENABLE_SOT_MASK,
857 hcd->regs + HC_INTERRUPT_ENABLE);
859 buffstatus = isp1760_readl(hcd->regs + HC_BUFFER_STATUS_REG);
860 buffstatus |= ATL_BUFFER;
861 isp1760_writel(buffstatus, hcd->regs + HC_BUFFER_STATUS_REG);
864 static void enqueue_an_INT_packet(struct usb_hcd *hcd, struct isp1760_qh *qh,
865 struct isp1760_qtd *qtd)
867 struct isp1760_hcd *priv = hcd_to_priv(hcd);
868 u32 skip_map, or_map;
871 u32 int_regs, payload;
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).
882 skip_map = isp1760_readl(hcd->regs + HC_INT_PTD_SKIPMAP_REG);
885 slot = __ffs(skip_map);
886 queue_entry = 1 << slot;
888 int_regs = INT_REGS_OFFSET + slot * sizeof(struct ptd);
890 payload = alloc_mem(priv, qtd->length);
892 enqueue_one_int_qtd(int_regs, payload, priv, qh, qtd->urb, slot, qtd);
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);
898 skip_map &= ~queue_entry;
899 isp1760_writel(skip_map, hcd->regs + HC_INT_PTD_SKIPMAP_REG);
901 buffstatus = isp1760_readl(hcd->regs + HC_BUFFER_STATUS_REG);
902 buffstatus |= INT_BUFFER;
903 isp1760_writel(buffstatus, hcd->regs + HC_BUFFER_STATUS_REG);
906 static void isp1760_urb_done(struct isp1760_hcd *priv, struct urb *urb, int status)
907 __releases(priv->lock)
908 __acquires(priv->lock)
910 if (!urb->unlinked) {
911 if (status == -EINPROGRESS)
915 if (usb_pipein(urb->pipe) && usb_pipetype(urb->pipe) != PIPE_CONTROL) {
917 for (ptr = urb->transfer_buffer;
918 ptr < urb->transfer_buffer + urb->transfer_buffer_length;
920 flush_dcache_page(virt_to_page(ptr));
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);
930 static void isp1760_qtd_free(struct isp1760_qtd *qtd)
932 kmem_cache_free(qtd_cachep, qtd);
935 static struct isp1760_qtd *clean_this_qtd(struct isp1760_qtd *qtd)
937 struct isp1760_qtd *tmp_qtd;
939 tmp_qtd = qtd->hw_next;
940 list_del(&qtd->qtd_list);
941 isp1760_qtd_free(qtd);
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.
950 static struct isp1760_qtd *clean_up_qtdlist(struct isp1760_qtd *qtd)
952 struct isp1760_qtd *tmp_qtd;
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);
961 } while (!last_one && qtd);
966 static void do_atl_int(struct usb_hcd *usb_hcd)
968 struct isp1760_hcd *priv = hcd_to_priv(usb_hcd);
969 u32 done_map, skip_map;
971 struct urb *urb = NULL;
978 u32 status = -EINVAL;
980 struct isp1760_qtd *qtd;
981 struct isp1760_qh *qh;
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);
990 or_map = isp1760_readl(usb_hcd->regs + HC_ATL_IRQ_MASK_OR_REG);
992 isp1760_writel(or_map, usb_hcd->regs + HC_ATL_IRQ_MASK_OR_REG);
994 atl_regs_base = ATL_REGS_OFFSET;
1003 queue_entry = __ffs(done_map);
1004 done_map &= ~(1 << queue_entry);
1005 skip_map |= 1 << queue_entry;
1007 atl_regs = atl_regs_base + queue_entry * sizeof(struct ptd);
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;
1015 printk(KERN_ERR "qh is 0\n");
1018 isp1760_writel(atl_regs + ISP_BANK(0), usb_hcd->regs +
1020 isp1760_writel(payload + ISP_BANK(1), usb_hcd->regs +
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)
1027 isp1760_writel(payload + ISP_BANK(1), usb_hcd->regs +
1030 priv_read_copy(priv, (u32 *)&ptd, usb_hcd->regs + atl_regs +
1031 ISP_BANK(0), sizeof(ptd));
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;
1039 /* Transfer Error, *but* active and no HALT -> reload */
1040 if ((dw3 & DW3_ERROR_BIT) && (dw3 & DW3_QTD_ACTIVE) &&
1041 !(dw3 & DW3_HALT_BIT)) {
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
1050 length = PTD_XFERRED_LENGTH(dw3);
1051 printk(KERN_ERR "Should reload now.... transfered %d "
1052 "of %zu\n", length, qtd->length);
1056 if (!nakcount && (dw3 & DW3_QTD_ACTIVE)) {
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.
1065 /* RL counter = ERR counter */
1066 dw3 &= ~(0xf << 19);
1068 dw3 &= ~(3 << (55 - 32));
1069 dw3 |= ERR_COUNTER << (55 - 32);
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.
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);
1083 ptd.dw3 = cpu_to_le32(dw3);
1084 priv_write_copy(priv, (u32 *)&ptd, usb_hcd->regs +
1085 atl_regs, sizeof(ptd));
1087 ptd.dw0 |= cpu_to_le32(PTD_VALID);
1088 priv_write_copy(priv, (u32 *)&ptd, usb_hcd->regs +
1089 atl_regs, sizeof(ptd));
1092 if (priv->atl_queued == 2)
1093 isp1760_writel(INTERRUPT_ENABLE_SOT_MASK,
1094 usb_hcd->regs + HC_INTERRUPT_ENABLE);
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);
1104 error = check_error(&ptd);
1107 priv->atl_ints[queue_entry].qh->toggle = 0;
1108 priv->atl_ints[queue_entry].qh->ping = 0;
1109 urb->status = -EPIPE;
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: "
1116 ptd.dw0, ptd.dw1, ptd.dw2, ptd.dw3,
1117 ptd.dw4, ptd.dw5, ptd.dw6, ptd.dw7);
1120 if (usb_pipetype(urb->pipe) == PIPE_BULK) {
1121 priv->atl_ints[queue_entry].qh->toggle = dw3 &
1123 priv->atl_ints[queue_entry].qh->ping = dw3 &
1128 length = PTD_XFERRED_LENGTH(dw3);
1130 switch (DW1_GET_PID(dw1)) {
1132 priv_read_copy(priv,
1133 priv->atl_ints[queue_entry].data_buffer,
1134 usb_hcd->regs + payload + ISP_BANK(1),
1139 urb->actual_length += length;
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;
1151 free_mem(priv, payload);
1153 isp1760_writel(skip_map, usb_hcd->regs +
1154 HC_ATL_PTD_SKIPMAP_REG);
1156 if (urb->status == -EPIPE) {
1157 /* HALT was received */
1159 qtd = clean_up_qtdlist(qtd);
1160 isp1760_urb_done(priv, urb, urb->status);
1162 } else if (usb_pipebulk(urb->pipe) && (length < qtd->length)) {
1163 /* short BULK received */
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);
1172 if (urb->status == -EINPROGRESS)
1175 qtd = clean_up_qtdlist(qtd);
1177 isp1760_urb_done(priv, urb, urb->status);
1179 } else if (qtd->status & URB_COMPLETE_NOTIFY) {
1180 /* that was the last qtd of that URB */
1182 if (urb->status == -EINPROGRESS)
1185 qtd = clean_this_qtd(qtd);
1186 isp1760_urb_done(priv, urb, urb->status);
1189 /* next QTD of this URB */
1191 qtd = clean_this_qtd(qtd);
1196 enqueue_an_ATL_packet(usb_hcd, qh, qtd);
1198 skip_map = isp1760_readl(usb_hcd->regs +
1199 HC_ATL_PTD_SKIPMAP_REG);
1201 if (priv->atl_queued <= 1)
1202 isp1760_writel(INTERRUPT_ENABLE_MASK,
1203 usb_hcd->regs + HC_INTERRUPT_ENABLE);
1206 static void do_intl_int(struct usb_hcd *usb_hcd)
1208 struct isp1760_hcd *priv = hcd_to_priv(usb_hcd);
1209 u32 done_map, skip_map;
1211 struct urb *urb = NULL;
1219 struct isp1760_qtd *qtd;
1220 struct isp1760_qh *qh;
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);
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);
1231 int_regs_base = INT_REGS_OFFSET;
1237 queue_entry = __ffs(done_map);
1238 done_map &= ~(1 << queue_entry);
1239 skip_map |= 1 << queue_entry;
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;
1248 printk(KERN_ERR "(INT) qh is 0\n");
1252 isp1760_writel(int_regs + ISP_BANK(0), usb_hcd->regs +
1254 isp1760_writel(payload + ISP_BANK(1), usb_hcd->regs +
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)
1261 isp1760_writel(payload + ISP_BANK(1), usb_hcd->regs +
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));
1270 error = check_error(&ptd);
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: "
1277 ptd.dw0, ptd.dw1, ptd.dw2, ptd.dw3,
1278 ptd.dw4, ptd.dw5, ptd.dw6, ptd.dw7);
1280 urb->status = -EPIPE;
1281 priv->int_ints[queue_entry].qh->toggle = 0;
1282 priv->int_ints[queue_entry].qh->ping = 0;
1285 priv->int_ints[queue_entry].qh->toggle =
1287 priv->int_ints[queue_entry].qh->ping = dw3 & (1 << 26);
1290 if (urb->dev->speed != USB_SPEED_HIGH)
1291 length = PTD_XFERRED_LENGTH_LO(dw3);
1293 length = PTD_XFERRED_LENGTH(dw3);
1296 switch (DW1_GET_PID(dw1)) {
1298 priv_read_copy(priv,
1299 priv->int_ints[queue_entry].data_buffer,
1300 usb_hcd->regs + payload + ISP_BANK(1),
1304 urb->actual_length += length;
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;
1316 isp1760_writel(skip_map, usb_hcd->regs +
1317 HC_INT_PTD_SKIPMAP_REG);
1318 free_mem(priv, payload);
1320 if (urb->status == -EPIPE) {
1323 qtd = clean_up_qtdlist(qtd);
1324 isp1760_urb_done(priv, urb, urb->status);
1326 } else if (qtd->status & URB_COMPLETE_NOTIFY) {
1328 if (urb->status == -EINPROGRESS)
1331 qtd = clean_this_qtd(qtd);
1332 isp1760_urb_done(priv, urb, urb->status);
1335 /* next QTD of this URB */
1337 qtd = clean_this_qtd(qtd);
1342 enqueue_an_INT_packet(usb_hcd, qh, qtd);
1344 skip_map = isp1760_readl(usb_hcd->regs +
1345 HC_INT_PTD_SKIPMAP_REG);
1349 #define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
1350 static struct isp1760_qh *qh_make(struct isp1760_hcd *priv, struct urb *urb,
1353 struct isp1760_qh *qh;
1356 qh = isp1760_qh_alloc(priv, flags);
1361 * init endpoint/device data for this QH
1363 is_input = usb_pipein(urb->pipe);
1364 type = usb_pipetype(urb->pipe);
1366 if (type == PIPE_INTERRUPT) {
1368 if (urb->dev->speed == USB_SPEED_HIGH) {
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.
1376 printk(KERN_ERR "intr period %d uframes, NYET!",
1382 qh->period = urb->interval;
1386 /* support for tt scheduling, and access to toggles */
1389 if (!usb_pipecontrol(urb->pipe))
1390 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), !is_input,
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.
1401 static struct isp1760_qh *qh_append_tds(struct isp1760_hcd *priv,
1402 struct urb *urb, struct list_head *qtd_list, int epnum,
1405 struct isp1760_qh *qh;
1406 struct isp1760_qtd *qtd;
1407 struct isp1760_qtd *prev_qtd;
1409 qh = (struct isp1760_qh *)*ptr;
1411 /* can't sleep here, we have priv->lock... */
1412 qh = qh_make(priv, urb, GFP_ATOMIC);
1418 qtd = list_entry(qtd_list->next, struct isp1760_qtd,
1420 if (!list_empty(&qh->qtd_list))
1421 prev_qtd = list_entry(qh->qtd_list.prev,
1422 struct isp1760_qtd, qtd_list);
1426 list_splice(qtd_list, qh->qtd_list.prev);
1428 BUG_ON(prev_qtd->hw_next);
1429 prev_qtd->hw_next = qtd;
1436 static void qtd_list_free(struct isp1760_hcd *priv, struct urb *urb,
1437 struct list_head *qtd_list)
1439 struct list_head *entry, *temp;
1441 list_for_each_safe(entry, temp, qtd_list) {
1442 struct isp1760_qtd *qtd;
1444 qtd = list_entry(entry, struct isp1760_qtd, qtd_list);
1445 list_del(&qtd->qtd_list);
1446 isp1760_qtd_free(qtd);
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)
1453 struct isp1760_qtd *qtd;
1455 unsigned long flags;
1456 struct isp1760_qh *qh = NULL;
1460 qtd = list_entry(qtd_list->next, struct isp1760_qtd, qtd_list);
1461 epnum = urb->ep->desc.bEndpointAddress;
1463 spin_lock_irqsave(&priv->lock, flags);
1464 if (!HCD_HW_ACCESSIBLE(priv_to_hcd(priv))) {
1468 rc = usb_hcd_link_urb_to_ep(priv_to_hcd(priv), urb);
1472 qh = urb->ep->hcpriv;
1474 qh_busy = !list_empty(&qh->qtd_list);
1478 qh = qh_append_tds(priv, urb, qtd_list, epnum, &urb->ep->hcpriv);
1480 usb_hcd_unlink_urb_from_ep(priv_to_hcd(priv), urb);
1486 p(priv_to_hcd(priv), qh, qtd);
1489 spin_unlock_irqrestore(&priv->lock, flags);
1491 qtd_list_free(priv, urb, qtd_list);
1495 static struct isp1760_qtd *isp1760_qtd_alloc(struct isp1760_hcd *priv,
1498 struct isp1760_qtd *qtd;
1500 qtd = kmem_cache_zalloc(qtd_cachep, flags);
1502 INIT_LIST_HEAD(&qtd->qtd_list);
1508 * create a list of filled qtds for this URB; won't link into qh.
1510 static struct list_head *qh_urb_transaction(struct isp1760_hcd *priv,
1511 struct urb *urb, struct list_head *head, gfp_t flags)
1513 struct isp1760_qtd *qtd, *qtd_prev;
1520 * URBs map to sequences of QTDs: one logical transaction
1522 qtd = isp1760_qtd_alloc(priv, flags);
1526 list_add_tail(&qtd->qtd_list, head);
1528 urb->status = -EINPROGRESS;
1531 /* for split transactions, SplitXState initialized to zero */
1533 len = urb->transfer_buffer_length;
1534 is_input = usb_pipein(urb->pipe);
1535 if (usb_pipecontrol(urb->pipe)) {
1537 qtd_fill(qtd, urb->setup_packet,
1538 sizeof(struct usb_ctrlrequest),
1541 /* ... and always at least one more pid */
1542 token ^= DATA_TOGGLE;
1544 qtd = isp1760_qtd_alloc(priv, flags);
1548 qtd_prev->hw_next = qtd;
1549 list_add_tail(&qtd->qtd_list, head);
1551 /* for zero length DATA stages, STATUS is always IN */
1557 * data transfer stage: buffer setup
1559 buf = urb->transfer_buffer;
1566 maxpacket = max_packet(usb_maxpacket(urb->dev, urb->pipe, !is_input));
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
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);
1583 this_qtd_len = qtd_fill(qtd, buf, len, token);
1584 len -= this_qtd_len;
1585 buf += this_qtd_len;
1587 /* qh makes control packets use qtd toggle; maybe switch it */
1588 if ((maxpacket & (this_qtd_len + (maxpacket - 1))) == 0)
1589 token ^= DATA_TOGGLE;
1595 qtd = isp1760_qtd_alloc(priv, flags);
1599 qtd_prev->hw_next = qtd;
1600 list_add_tail(&qtd->qtd_list, head);
1604 * control requests may need a terminating data "status" ack;
1605 * bulk ones may need a terminating short packet (zero length).
1607 if (urb->transfer_buffer_length != 0) {
1610 if (usb_pipecontrol(urb->pipe)) {
1612 /* "in" <--> "out" */
1615 token |= DATA_TOGGLE;
1616 } else if (usb_pipebulk(urb->pipe)
1617 && (urb->transfer_flags & URB_ZERO_PACKET)
1618 && !(urb->transfer_buffer_length % maxpacket)) {
1623 qtd = isp1760_qtd_alloc(priv, flags);
1627 qtd_prev->hw_next = qtd;
1628 list_add_tail(&qtd->qtd_list, head);
1630 /* never any data in such packets */
1631 qtd_fill(qtd, NULL, 0, token);
1635 qtd->status = URB_COMPLETE_NOTIFY;
1639 qtd_list_free(priv, urb, head);
1643 static int isp1760_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
1646 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1647 struct list_head qtd_list;
1650 INIT_LIST_HEAD(&qtd_list);
1652 switch (usb_pipetype(urb->pipe)) {
1656 if (!qh_urb_transaction(priv, urb, &qtd_list, mem_flags))
1658 pe = enqueue_an_ATL_packet;
1661 case PIPE_INTERRUPT:
1662 if (!qh_urb_transaction(priv, urb, &qtd_list, mem_flags))
1664 pe = enqueue_an_INT_packet;
1667 case PIPE_ISOCHRONOUS:
1668 printk(KERN_ERR "PIPE_ISOCHRONOUS ain't supported\n");
1673 return isp1760_prepare_enqueue(priv, urb, &qtd_list, mem_flags, pe);
1676 static int isp1760_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
1679 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1680 struct inter_packet_info *ints;
1682 u32 reg_base, or_reg, skip_reg;
1683 unsigned long flags;
1687 switch (usb_pipetype(urb->pipe)) {
1688 case PIPE_ISOCHRONOUS:
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;
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;
1709 memset(&ptd, 0, sizeof(ptd));
1710 spin_lock_irqsave(&priv->lock, flags);
1712 for (i = 0; i < 32; i++) {
1713 if (ints->urb == urb) {
1716 struct isp1760_qtd *qtd;
1717 struct isp1760_qh *qh = ints->qh;
1719 skip_map = isp1760_readl(hcd->regs + skip_reg);
1721 isp1760_writel(skip_map, hcd->regs + skip_reg);
1723 or_map = isp1760_readl(hcd->regs + or_reg);
1724 or_map &= ~(1 << i);
1725 isp1760_writel(or_map, hcd->regs + or_reg);
1727 priv_write_copy(priv, (u32 *)&ptd, hcd->regs + reg_base
1728 + i * sizeof(ptd), sizeof(ptd));
1730 qtd = clean_up_qtdlist(qtd);
1732 free_mem(priv, ints->payload);
1737 ints->data_buffer = NULL;
1740 isp1760_urb_done(priv, urb, status);
1745 } else if (ints->qtd) {
1746 struct isp1760_qtd *qtd, *prev_qtd = ints->qtd;
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);
1756 /* we found the urb before the end of the list */
1763 spin_unlock_irqrestore(&priv->lock, flags);
1767 static irqreturn_t isp1760_irq(struct usb_hcd *usb_hcd)
1769 struct isp1760_hcd *priv = hcd_to_priv(usb_hcd);
1771 irqreturn_t irqret = IRQ_NONE;
1773 spin_lock(&priv->lock);
1775 if (!(usb_hcd->state & HC_STATE_RUNNING))
1778 imask = isp1760_readl(usb_hcd->regs + HC_INTERRUPT_REG);
1779 if (unlikely(!imask))
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);
1786 if (imask & HC_INTL_INT)
1787 do_intl_int(usb_hcd);
1789 irqret = IRQ_HANDLED;
1791 spin_unlock(&priv->lock);
1795 static int isp1760_hub_status_data(struct usb_hcd *hcd, char *buf)
1797 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1798 u32 temp, status = 0;
1801 unsigned long flags;
1803 /* if !USB_SUSPEND, root hub timers won't get shut down ... */
1804 if (!HC_IS_RUNNING(hcd->state))
1807 /* init status to no-changes */
1811 spin_lock_irqsave(&priv->lock, flags);
1812 temp = isp1760_readl(hcd->regs + HC_PORTSC1);
1814 if (temp & PORT_OWNER) {
1815 if (temp & PORT_CSC) {
1817 isp1760_writel(temp, hcd->regs + HC_PORTSC1);
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.
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);
1836 /* FIXME autosuspend idle root hubs */
1838 spin_unlock_irqrestore(&priv->lock, flags);
1839 return status ? retval : 0;
1842 static void isp1760_hub_descriptor(struct isp1760_hcd *priv,
1843 struct usb_hub_descriptor *desc)
1845 int ports = HCS_N_PORTS(priv->hcs_params);
1848 desc->bDescriptorType = 0x29;
1849 /* priv 1.0, 2.3.9 says 20ms max */
1850 desc->bPwrOn2PwrGood = 10;
1851 desc->bHubContrCurrent = 0;
1853 desc->bNbrPorts = ports;
1854 temp = 1 + (ports / 8);
1855 desc->bDescLength = 7 + 2 * temp;
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);
1861 /* per-port overcurrent reporting */
1863 if (HCS_PPC(priv->hcs_params))
1864 /* per-port power control */
1867 /* no power switching */
1869 desc->wHubCharacteristics = cpu_to_le16(temp);
1872 #define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
1874 static int check_reset_complete(struct isp1760_hcd *priv, int index,
1875 u32 __iomem *status_reg, int port_status)
1877 if (!(port_status & PORT_CONNECT))
1880 /* if reset finished and it's still not enabled -- handoff */
1881 if (!(port_status & PORT_PE)) {
1883 printk(KERN_ERR "port %d full speed --> companion\n",
1886 port_status |= PORT_OWNER;
1887 port_status &= ~PORT_RWC_BITS;
1888 isp1760_writel(port_status, status_reg);
1891 printk(KERN_ERR "port %d high speed\n", index + 1);
1896 static int isp1760_hub_control(struct usb_hcd *hcd, u16 typeReq,
1897 u16 wValue, u16 wIndex, char *buf, u16 wLength)
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;
1903 unsigned long flags;
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.
1914 spin_lock_irqsave(&priv->lock, flags);
1916 case ClearHubFeature:
1918 case C_HUB_LOCAL_POWER:
1919 case C_HUB_OVER_CURRENT:
1920 /* no hub-wide feature/status flags */
1926 case ClearPortFeature:
1927 if (!wIndex || wIndex > ports)
1930 temp = isp1760_readl(status_reg);
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).
1940 case USB_PORT_FEAT_ENABLE:
1941 isp1760_writel(temp & ~PORT_PE, status_reg);
1943 case USB_PORT_FEAT_C_ENABLE:
1946 case USB_PORT_FEAT_SUSPEND:
1947 if (temp & PORT_RESET)
1950 if (temp & PORT_SUSPEND) {
1951 if ((temp & PORT_PE) == 0)
1953 /* resume signaling for 20 msec */
1954 temp &= ~(PORT_RWC_BITS);
1955 isp1760_writel(temp | PORT_RESUME,
1957 priv->reset_done = jiffies +
1958 msecs_to_jiffies(20);
1961 case USB_PORT_FEAT_C_SUSPEND:
1962 /* we auto-clear this feature */
1964 case USB_PORT_FEAT_POWER:
1965 if (HCS_PPC(priv->hcs_params))
1966 isp1760_writel(temp & ~PORT_POWER, status_reg);
1968 case USB_PORT_FEAT_C_CONNECTION:
1969 isp1760_writel(temp | PORT_CSC,
1972 case USB_PORT_FEAT_C_OVER_CURRENT:
1975 case USB_PORT_FEAT_C_RESET:
1976 /* GetPortStatus clears reset */
1981 isp1760_readl(hcd->regs + HC_USBCMD);
1983 case GetHubDescriptor:
1984 isp1760_hub_descriptor(priv, (struct usb_hub_descriptor *)
1988 /* no hub-wide feature/status flags */
1992 if (!wIndex || wIndex > ports)
1996 temp = isp1760_readl(status_reg);
1998 /* wPortChange bits */
1999 if (temp & PORT_CSC)
2000 status |= USB_PORT_STAT_C_CONNECTION << 16;
2003 /* whoever resumes must GetPortStatus to complete it!! */
2004 if (temp & PORT_RESUME) {
2005 printk(KERN_ERR "Port resume should be skipped.\n");
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,
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;
2023 /* stop resume signaling */
2024 temp = isp1760_readl(status_reg);
2026 temp & ~(PORT_RWC_BITS | PORT_RESUME),
2028 retval = handshake(priv, status_reg,
2029 PORT_RESUME, 0, 2000 /* 2msec */);
2032 "port %d resume error %d\n",
2033 wIndex + 1, retval);
2036 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
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;
2047 /* force reset to complete */
2048 isp1760_writel(temp & ~PORT_RESET,
2050 /* REVISIT: some hardware needs 550+ usec to clear
2051 * this bit; seems too long to spin routinely...
2053 retval = handshake(priv, status_reg,
2054 PORT_RESET, 0, 750);
2056 isp1760_err(priv, "port %d reset error %d\n",
2057 wIndex + 1, retval);
2061 /* see what we found out */
2062 temp = check_reset_complete(priv, wIndex, status_reg,
2063 isp1760_readl(status_reg));
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).
2071 if (temp & PORT_OWNER)
2072 printk(KERN_ERR "Warning: PORT_OWNER is set\n");
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);
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;
2088 put_unaligned(cpu_to_le32(status), (__le32 *) buf);
2092 case C_HUB_LOCAL_POWER:
2093 case C_HUB_OVER_CURRENT:
2094 /* no hub-wide feature/status flags */
2100 case SetPortFeature:
2101 selector = wIndex >> 8;
2103 if (!wIndex || wIndex > ports)
2106 temp = isp1760_readl(status_reg);
2107 if (temp & PORT_OWNER)
2110 /* temp &= ~PORT_RWC_BITS; */
2112 case USB_PORT_FEAT_ENABLE:
2113 isp1760_writel(temp | PORT_PE, status_reg);
2116 case USB_PORT_FEAT_SUSPEND:
2117 if ((temp & PORT_PE) == 0
2118 || (temp & PORT_RESET) != 0)
2121 isp1760_writel(temp | PORT_SUSPEND, status_reg);
2123 case USB_PORT_FEAT_POWER:
2124 if (HCS_PPC(priv->hcs_params))
2125 isp1760_writel(temp | PORT_POWER,
2128 case USB_PORT_FEAT_RESET:
2129 if (temp & PORT_RESUME)
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.
2135 if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
2136 && PORT_USB11(temp)) {
2143 * caller must wait, then call GetPortStatus
2144 * usb 2.0 spec says 50 ms resets on root
2146 priv->reset_done = jiffies +
2147 msecs_to_jiffies(50);
2149 isp1760_writel(temp, status_reg);
2154 isp1760_readl(hcd->regs + HC_USBCMD);
2159 /* "stall" on error */
2162 spin_unlock_irqrestore(&priv->lock, flags);
2166 static void isp1760_endpoint_disable(struct usb_hcd *usb_hcd,
2167 struct usb_host_endpoint *ep)
2169 struct isp1760_hcd *priv = hcd_to_priv(usb_hcd);
2170 struct isp1760_qh *qh;
2171 struct isp1760_qtd *qtd;
2172 unsigned long flags;
2174 spin_lock_irqsave(&priv->lock, flags);
2181 /* more than entry might get removed */
2182 if (list_empty(&qh->qtd_list))
2185 qtd = list_first_entry(&qh->qtd_list, struct isp1760_qtd,
2188 if (qtd->status & URB_ENQUEUED) {
2190 spin_unlock_irqrestore(&priv->lock, flags);
2191 isp1760_urb_dequeue(usb_hcd, qtd->urb, -ECONNRESET);
2192 spin_lock_irqsave(&priv->lock, flags);
2197 clean_up_qtdlist(qtd);
2198 isp1760_urb_done(priv, urb, -ECONNRESET);
2203 /* remove requests and leak them.
2204 * ATL are pretty fast done, INT could take a while...
2205 * The latter shoule be removed
2208 spin_unlock_irqrestore(&priv->lock, flags);
2211 static int isp1760_get_frame(struct usb_hcd *hcd)
2213 struct isp1760_hcd *priv = hcd_to_priv(hcd);
2216 fr = isp1760_readl(hcd->regs + HC_FRINDEX);
2217 return (fr >> 3) % priv->periodic_size;
2220 static void isp1760_stop(struct usb_hcd *hcd)
2222 struct isp1760_hcd *priv = hcd_to_priv(hcd);
2225 isp1760_hub_control(hcd, ClearPortFeature, USB_PORT_FEAT_POWER, 1,
2229 spin_lock_irq(&priv->lock);
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);
2236 isp1760_writel(0, hcd->regs + HC_CONFIGFLAG);
2239 static void isp1760_shutdown(struct usb_hcd *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);
2247 command = isp1760_readl(hcd->regs + HC_USBCMD);
2248 command &= ~CMD_RUN;
2249 isp1760_writel(command, hcd->regs + HC_USBCMD);
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),
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,
2270 int __init init_kmem_once(void)
2272 qtd_cachep = kmem_cache_create("isp1760_qtd",
2273 sizeof(struct isp1760_qtd), 0, SLAB_TEMPORARY |
2274 SLAB_MEM_SPREAD, NULL);
2279 qh_cachep = kmem_cache_create("isp1760_qh", sizeof(struct isp1760_qh),
2280 0, SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
2283 kmem_cache_destroy(qtd_cachep);
2290 void deinit_kmem_cache(void)
2292 kmem_cache_destroy(qtd_cachep);
2293 kmem_cache_destroy(qh_cachep);
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)
2301 struct usb_hcd *hcd;
2302 struct isp1760_hcd *priv;
2306 return ERR_PTR(-ENODEV);
2308 /* prevent usb-core allocating DMA pages */
2309 dev->dma_mask = NULL;
2311 hcd = usb_create_hcd(&isp1760_hc_driver, dev, dev_name(dev));
2313 return ERR_PTR(-ENOMEM);
2315 priv = hcd_to_priv(hcd);
2316 priv->devflags = devflags;
2318 hcd->regs = ioremap(res_start, res_len);
2325 hcd->rsrc_start = res_start;
2326 hcd->rsrc_len = res_len;
2328 ret = usb_add_hcd(hcd, irq, irqflags);
2340 return ERR_PTR(ret);
2343 MODULE_DESCRIPTION("Driver for the ISP1760 USB-controller from NXP");
2344 MODULE_AUTHOR("Sebastian Siewior <bigeasy@linuxtronix.de>");
2345 MODULE_LICENSE("GPL v2");