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>
11 * (c) 2011 Arvid Brodin <arvid.brodin@enea.com>
14 #include <linux/gpio/consumer.h>
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/slab.h>
18 #include <linux/list.h>
19 #include <linux/usb.h>
20 #include <linux/usb/hcd.h>
21 #include <linux/debugfs.h>
22 #include <linux/uaccess.h>
25 #include <linux/timer.h>
26 #include <asm/unaligned.h>
27 #include <asm/cacheflush.h>
29 #include "isp1760-core.h"
30 #include "isp1760-hcd.h"
31 #include "isp1760-regs.h"
33 static struct kmem_cache *qtd_cachep;
34 static struct kmem_cache *qh_cachep;
35 static struct kmem_cache *urb_listitem_cachep;
37 typedef void (packet_enqueue)(struct usb_hcd *hcd, struct isp1760_qh *qh,
38 struct isp1760_qtd *qtd);
40 static inline struct isp1760_hcd *hcd_to_priv(struct usb_hcd *hcd)
42 return *(struct isp1760_hcd **)hcd->hcd_priv;
46 #define DELETE_URB (0x0008)
47 #define NO_TRANSFER_ACTIVE (0xffffffff)
49 /* Philips Proprietary Transfer Descriptor (PTD) */
50 typedef __u32 __bitwise __dw;
61 #define PTD_OFFSET 0x0400
62 #define ISO_PTD_OFFSET 0x0400
63 #define INT_PTD_OFFSET 0x0800
64 #define ATL_PTD_OFFSET 0x0c00
65 #define PAYLOAD_OFFSET 0x1000
70 #define DW0_VALID_BIT 1
71 #define FROM_DW0_VALID(x) ((x) & 0x01)
72 #define TO_DW0_LENGTH(x) (((u32) x) << 3)
73 #define TO_DW0_MAXPACKET(x) (((u32) x) << 18)
74 #define TO_DW0_MULTI(x) (((u32) x) << 29)
75 #define TO_DW0_ENDPOINT(x) (((u32) x) << 31)
77 #define TO_DW1_DEVICE_ADDR(x) (((u32) x) << 3)
78 #define TO_DW1_PID_TOKEN(x) (((u32) x) << 10)
79 #define DW1_TRANS_BULK ((u32) 2 << 12)
80 #define DW1_TRANS_INT ((u32) 3 << 12)
81 #define DW1_TRANS_SPLIT ((u32) 1 << 14)
82 #define DW1_SE_USB_LOSPEED ((u32) 2 << 16)
83 #define TO_DW1_PORT_NUM(x) (((u32) x) << 18)
84 #define TO_DW1_HUB_NUM(x) (((u32) x) << 25)
86 #define TO_DW2_DATA_START_ADDR(x) (((u32) x) << 8)
87 #define TO_DW2_RL(x) ((x) << 25)
88 #define FROM_DW2_RL(x) (((x) >> 25) & 0xf)
90 #define FROM_DW3_NRBYTESTRANSFERRED(x) ((x) & 0x7fff)
91 #define FROM_DW3_SCS_NRBYTESTRANSFERRED(x) ((x) & 0x07ff)
92 #define TO_DW3_NAKCOUNT(x) ((x) << 19)
93 #define FROM_DW3_NAKCOUNT(x) (((x) >> 19) & 0xf)
94 #define TO_DW3_CERR(x) ((x) << 23)
95 #define FROM_DW3_CERR(x) (((x) >> 23) & 0x3)
96 #define TO_DW3_DATA_TOGGLE(x) ((x) << 25)
97 #define FROM_DW3_DATA_TOGGLE(x) (((x) >> 25) & 0x1)
98 #define TO_DW3_PING(x) ((x) << 26)
99 #define FROM_DW3_PING(x) (((x) >> 26) & 0x1)
100 #define DW3_ERROR_BIT (1 << 28)
101 #define DW3_BABBLE_BIT (1 << 29)
102 #define DW3_HALT_BIT (1 << 30)
103 #define DW3_ACTIVE_BIT (1 << 31)
104 #define FROM_DW3_ACTIVE(x) (((x) >> 31) & 0x01)
106 #define INT_UNDERRUN (1 << 2)
107 #define INT_BABBLE (1 << 1)
108 #define INT_EXACT (1 << 0)
110 #define SETUP_PID (2)
115 #define RL_COUNTER (0)
116 #define NAK_COUNTER (0)
117 #define ERR_COUNTER (2)
124 /* the rest is HCD-private */
125 struct list_head qtd_list;
128 size_t actual_length;
130 /* QTD_ENQUEUED: waiting for transfer (inactive) */
131 /* QTD_PAYLOAD_ALLOC: chip mem has been allocated for payload */
132 /* QTD_XFER_STARTED: valid ptd has been written to isp176x - only
133 interrupt handler may touch this qtd! */
134 /* QTD_XFER_COMPLETE: payload has been transferred successfully */
135 /* QTD_RETIRE: transfer error/abort qtd */
136 #define QTD_ENQUEUED 0
137 #define QTD_PAYLOAD_ALLOC 1
138 #define QTD_XFER_STARTED 2
139 #define QTD_XFER_COMPLETE 3
144 /* Queue head, one for each active endpoint */
146 struct list_head qh_list;
147 struct list_head qtd_list;
151 int tt_buffer_dirty; /* See USB2.0 spec section 11.17.5 */
154 struct urb_listitem {
155 struct list_head urb_list;
160 * Access functions for isp176x registers (addresses 0..0x03FF).
162 static u32 reg_read32(void __iomem *base, u32 reg)
164 return isp1760_read32(base, reg);
167 static void reg_write32(void __iomem *base, u32 reg, u32 val)
169 isp1760_write32(base, reg, val);
173 * Access functions for isp176x memory (offset >= 0x0400).
175 * bank_reads8() reads memory locations prefetched by an earlier write to
176 * HC_MEMORY_REG (see isp176x datasheet). Unless you want to do fancy multi-
177 * bank optimizations, you should use the more generic mem_reads8() below.
179 * For access to ptd memory, use the specialized ptd_read() and ptd_write()
182 * These functions copy via MMIO data to/from the device. memcpy_{to|from}io()
183 * doesn't quite work because some people have to enforce 32-bit access
185 static void bank_reads8(void __iomem *src_base, u32 src_offset, u32 bank_addr,
186 __u32 *dst, u32 bytes)
193 src = src_base + (bank_addr | src_offset);
195 if (src_offset < PAYLOAD_OFFSET) {
197 *dst = le32_to_cpu(__raw_readl(src));
204 *dst = __raw_readl(src);
214 /* in case we have 3, 2 or 1 by left. The dst buffer may not be fully
217 if (src_offset < PAYLOAD_OFFSET)
218 val = le32_to_cpu(__raw_readl(src));
220 val = __raw_readl(src);
222 dst_byteptr = (void *) dst;
223 src_byteptr = (void *) &val;
225 *dst_byteptr = *src_byteptr;
232 static void mem_reads8(void __iomem *src_base, u32 src_offset, void *dst,
235 reg_write32(src_base, HC_MEMORY_REG, src_offset + ISP_BANK(0));
237 bank_reads8(src_base, src_offset, ISP_BANK(0), dst, bytes);
240 static void mem_writes8(void __iomem *dst_base, u32 dst_offset,
241 __u32 const *src, u32 bytes)
245 dst = dst_base + dst_offset;
247 if (dst_offset < PAYLOAD_OFFSET) {
249 __raw_writel(cpu_to_le32(*src), dst);
256 __raw_writel(*src, dst);
265 /* in case we have 3, 2 or 1 bytes left. The buffer is allocated and the
266 * extra bytes should not be read by the HW.
269 if (dst_offset < PAYLOAD_OFFSET)
270 __raw_writel(cpu_to_le32(*src), dst);
272 __raw_writel(*src, dst);
276 * Read and write ptds. 'ptd_offset' should be one of ISO_PTD_OFFSET,
277 * INT_PTD_OFFSET, and ATL_PTD_OFFSET. 'slot' should be less than 32.
279 static void ptd_read(void __iomem *base, u32 ptd_offset, u32 slot,
282 reg_write32(base, HC_MEMORY_REG,
283 ISP_BANK(0) + ptd_offset + slot*sizeof(*ptd));
285 bank_reads8(base, ptd_offset + slot*sizeof(*ptd), ISP_BANK(0),
286 (void *) ptd, sizeof(*ptd));
289 static void ptd_write(void __iomem *base, u32 ptd_offset, u32 slot,
292 mem_writes8(base, ptd_offset + slot*sizeof(*ptd) + sizeof(ptd->dw0),
293 &ptd->dw1, 7*sizeof(ptd->dw1));
294 /* Make sure dw0 gets written last (after other dw's and after payload)
295 since it contains the enable bit */
297 mem_writes8(base, ptd_offset + slot*sizeof(*ptd), &ptd->dw0,
302 /* memory management of the 60kb on the chip from 0x1000 to 0xffff */
303 static void init_memory(struct isp1760_hcd *priv)
308 payload_addr = PAYLOAD_OFFSET;
309 for (i = 0; i < BLOCK_1_NUM; i++) {
310 priv->memory_pool[i].start = payload_addr;
311 priv->memory_pool[i].size = BLOCK_1_SIZE;
312 priv->memory_pool[i].free = 1;
313 payload_addr += priv->memory_pool[i].size;
317 for (i = 0; i < BLOCK_2_NUM; i++) {
318 priv->memory_pool[curr + i].start = payload_addr;
319 priv->memory_pool[curr + i].size = BLOCK_2_SIZE;
320 priv->memory_pool[curr + i].free = 1;
321 payload_addr += priv->memory_pool[curr + i].size;
325 for (i = 0; i < BLOCK_3_NUM; i++) {
326 priv->memory_pool[curr + i].start = payload_addr;
327 priv->memory_pool[curr + i].size = BLOCK_3_SIZE;
328 priv->memory_pool[curr + i].free = 1;
329 payload_addr += priv->memory_pool[curr + i].size;
332 WARN_ON(payload_addr - priv->memory_pool[0].start > PAYLOAD_AREA_SIZE);
335 static void alloc_mem(struct usb_hcd *hcd, struct isp1760_qtd *qtd)
337 struct isp1760_hcd *priv = hcd_to_priv(hcd);
340 WARN_ON(qtd->payload_addr);
345 for (i = 0; i < BLOCKS; i++) {
346 if (priv->memory_pool[i].size >= qtd->length &&
347 priv->memory_pool[i].free) {
348 priv->memory_pool[i].free = 0;
349 qtd->payload_addr = priv->memory_pool[i].start;
355 static void free_mem(struct usb_hcd *hcd, struct isp1760_qtd *qtd)
357 struct isp1760_hcd *priv = hcd_to_priv(hcd);
360 if (!qtd->payload_addr)
363 for (i = 0; i < BLOCKS; i++) {
364 if (priv->memory_pool[i].start == qtd->payload_addr) {
365 WARN_ON(priv->memory_pool[i].free);
366 priv->memory_pool[i].free = 1;
367 qtd->payload_addr = 0;
372 dev_err(hcd->self.controller, "%s: Invalid pointer: %08x\n",
373 __func__, qtd->payload_addr);
375 qtd->payload_addr = 0;
378 static int handshake(struct usb_hcd *hcd, u32 reg,
379 u32 mask, u32 done, int usec)
384 result = reg_read32(hcd->regs, reg);
396 /* reset a non-running (STS_HALT == 1) controller */
397 static int ehci_reset(struct usb_hcd *hcd)
400 struct isp1760_hcd *priv = hcd_to_priv(hcd);
402 u32 command = reg_read32(hcd->regs, HC_USBCMD);
404 command |= CMD_RESET;
405 reg_write32(hcd->regs, HC_USBCMD, command);
406 hcd->state = HC_STATE_HALT;
407 priv->next_statechange = jiffies;
408 retval = handshake(hcd, HC_USBCMD,
409 CMD_RESET, 0, 250 * 1000);
413 static struct isp1760_qh *qh_alloc(gfp_t flags)
415 struct isp1760_qh *qh;
417 qh = kmem_cache_zalloc(qh_cachep, flags);
421 INIT_LIST_HEAD(&qh->qh_list);
422 INIT_LIST_HEAD(&qh->qtd_list);
428 static void qh_free(struct isp1760_qh *qh)
430 WARN_ON(!list_empty(&qh->qtd_list));
431 WARN_ON(qh->slot > -1);
432 kmem_cache_free(qh_cachep, qh);
435 /* one-time init, only for memory state */
436 static int priv_init(struct usb_hcd *hcd)
438 struct isp1760_hcd *priv = hcd_to_priv(hcd);
442 spin_lock_init(&priv->lock);
444 for (i = 0; i < QH_END; i++)
445 INIT_LIST_HEAD(&priv->qh_list[i]);
448 * hw default: 1K periodic list heads, one per frame.
449 * periodic_size can shrink by USBCMD update if hcc_params allows.
451 priv->periodic_size = DEFAULT_I_TDPS;
453 /* controllers may cache some of the periodic schedule ... */
454 hcc_params = reg_read32(hcd->regs, HC_HCCPARAMS);
455 /* full frame cache */
456 if (HCC_ISOC_CACHE(hcc_params))
458 else /* N microframes cached */
459 priv->i_thresh = 2 + HCC_ISOC_THRES(hcc_params);
464 static int isp1760_hc_setup(struct usb_hcd *hcd)
466 struct isp1760_hcd *priv = hcd_to_priv(hcd);
470 reg_write32(hcd->regs, HC_SCRATCH_REG, 0xdeadbabe);
471 /* Change bus pattern */
472 scratch = reg_read32(hcd->regs, HC_CHIP_ID_REG);
473 scratch = reg_read32(hcd->regs, HC_SCRATCH_REG);
474 if (scratch != 0xdeadbabe) {
475 dev_err(hcd->self.controller, "Scratch test failed.\n");
480 * The RESET_HC bit in the SW_RESET register is supposed to reset the
481 * host controller without touching the CPU interface registers, but at
482 * least on the ISP1761 it seems to behave as the RESET_ALL bit and
483 * reset the whole device. We thus can't use it here, so let's reset
484 * the host controller through the EHCI USB Command register. The device
485 * has been reset in core code anyway, so this shouldn't matter.
487 reg_write32(hcd->regs, HC_BUFFER_STATUS_REG, 0);
488 reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, NO_TRANSFER_ACTIVE);
489 reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, NO_TRANSFER_ACTIVE);
490 reg_write32(hcd->regs, HC_ISO_PTD_SKIPMAP_REG, NO_TRANSFER_ACTIVE);
492 result = ehci_reset(hcd);
499 hwmode = reg_read32(hcd->regs, HC_HW_MODE_CTRL) & ~ALL_ATX_RESET;
500 reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode | ALL_ATX_RESET);
502 reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode);
504 reg_write32(hcd->regs, HC_INTERRUPT_ENABLE, INTERRUPT_ENABLE_MASK);
507 * PORT 1 Control register of the ISP1760 is the OTG control
508 * register on ISP1761. Since there is no OTG or device controller
509 * support in this driver, we use port 1 as a "normal" USB host port on
512 reg_write32(hcd->regs, HC_PORT1_CTRL, PORT1_POWER | PORT1_INIT2);
515 priv->hcs_params = reg_read32(hcd->regs, HC_HCSPARAMS);
517 return priv_init(hcd);
520 static u32 base_to_chip(u32 base)
522 return ((base - 0x400) >> 3);
525 static int last_qtd_of_urb(struct isp1760_qtd *qtd, struct isp1760_qh *qh)
529 if (list_is_last(&qtd->qtd_list, &qh->qtd_list))
533 qtd = list_entry(qtd->qtd_list.next, typeof(*qtd), qtd_list);
534 return (qtd->urb != urb);
537 /* magic numbers that can affect system performance */
538 #define EHCI_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
539 #define EHCI_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
540 #define EHCI_TUNE_RL_TT 0
541 #define EHCI_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
542 #define EHCI_TUNE_MULT_TT 1
543 #define EHCI_TUNE_FLS 2 /* (small) 256 frame schedule */
545 static void create_ptd_atl(struct isp1760_qh *qh,
546 struct isp1760_qtd *qtd, struct ptd *ptd)
551 u32 nak = NAK_COUNTER;
553 memset(ptd, 0, sizeof(*ptd));
555 /* according to 3.6.2, max packet len can not be > 0x400 */
556 maxpacket = usb_maxpacket(qtd->urb->dev, qtd->urb->pipe,
557 usb_pipeout(qtd->urb->pipe));
558 multi = 1 + ((maxpacket >> 11) & 0x3);
562 ptd->dw0 = DW0_VALID_BIT;
563 ptd->dw0 |= TO_DW0_LENGTH(qtd->length);
564 ptd->dw0 |= TO_DW0_MAXPACKET(maxpacket);
565 ptd->dw0 |= TO_DW0_ENDPOINT(usb_pipeendpoint(qtd->urb->pipe));
568 ptd->dw1 = usb_pipeendpoint(qtd->urb->pipe) >> 1;
569 ptd->dw1 |= TO_DW1_DEVICE_ADDR(usb_pipedevice(qtd->urb->pipe));
570 ptd->dw1 |= TO_DW1_PID_TOKEN(qtd->packet_type);
572 if (usb_pipebulk(qtd->urb->pipe))
573 ptd->dw1 |= DW1_TRANS_BULK;
574 else if (usb_pipeint(qtd->urb->pipe))
575 ptd->dw1 |= DW1_TRANS_INT;
577 if (qtd->urb->dev->speed != USB_SPEED_HIGH) {
578 /* split transaction */
580 ptd->dw1 |= DW1_TRANS_SPLIT;
581 if (qtd->urb->dev->speed == USB_SPEED_LOW)
582 ptd->dw1 |= DW1_SE_USB_LOSPEED;
584 ptd->dw1 |= TO_DW1_PORT_NUM(qtd->urb->dev->ttport);
585 ptd->dw1 |= TO_DW1_HUB_NUM(qtd->urb->dev->tt->hub->devnum);
587 /* SE bit for Split INT transfers */
588 if (usb_pipeint(qtd->urb->pipe) &&
589 (qtd->urb->dev->speed == USB_SPEED_LOW))
595 ptd->dw0 |= TO_DW0_MULTI(multi);
596 if (usb_pipecontrol(qtd->urb->pipe) ||
597 usb_pipebulk(qtd->urb->pipe))
598 ptd->dw3 |= TO_DW3_PING(qh->ping);
602 ptd->dw2 |= TO_DW2_DATA_START_ADDR(base_to_chip(qtd->payload_addr));
603 ptd->dw2 |= TO_DW2_RL(rl);
606 ptd->dw3 |= TO_DW3_NAKCOUNT(nak);
607 ptd->dw3 |= TO_DW3_DATA_TOGGLE(qh->toggle);
608 if (usb_pipecontrol(qtd->urb->pipe)) {
609 if (qtd->data_buffer == qtd->urb->setup_packet)
610 ptd->dw3 &= ~TO_DW3_DATA_TOGGLE(1);
611 else if (last_qtd_of_urb(qtd, qh))
612 ptd->dw3 |= TO_DW3_DATA_TOGGLE(1);
615 ptd->dw3 |= DW3_ACTIVE_BIT;
617 ptd->dw3 |= TO_DW3_CERR(ERR_COUNTER);
620 static void transform_add_int(struct isp1760_qh *qh,
621 struct isp1760_qtd *qtd, struct ptd *ptd)
627 * Most of this is guessing. ISP1761 datasheet is quite unclear, and
628 * the algorithm from the original Philips driver code, which was
629 * pretty much used in this driver before as well, is quite horrendous
630 * and, i believe, incorrect. The code below follows the datasheet and
631 * USB2.0 spec as far as I can tell, and plug/unplug seems to be much
632 * more reliable this way (fingers crossed...).
635 if (qtd->urb->dev->speed == USB_SPEED_HIGH) {
636 /* urb->interval is in units of microframes (1/8 ms) */
637 period = qtd->urb->interval >> 3;
639 if (qtd->urb->interval > 4)
640 usof = 0x01; /* One bit set =>
641 interval 1 ms * uFrame-match */
642 else if (qtd->urb->interval > 2)
643 usof = 0x22; /* Two bits set => interval 1/2 ms */
644 else if (qtd->urb->interval > 1)
645 usof = 0x55; /* Four bits set => interval 1/4 ms */
647 usof = 0xff; /* All bits set => interval 1/8 ms */
649 /* urb->interval is in units of frames (1 ms) */
650 period = qtd->urb->interval;
651 usof = 0x0f; /* Execute Start Split on any of the
652 four first uFrames */
655 * First 8 bits in dw5 is uSCS and "specifies which uSOF the
656 * complete split needs to be sent. Valid only for IN." Also,
657 * "All bits can be set to one for every transfer." (p 82,
658 * ISP1761 data sheet.) 0x1c is from Philips driver. Where did
659 * that number come from? 0xff seems to work fine...
661 /* ptd->dw5 = 0x1c; */
662 ptd->dw5 = 0xff; /* Execute Complete Split on any uFrame */
665 period = period >> 1;/* Ensure equal or shorter period than requested */
666 period &= 0xf8; /* Mask off too large values and lowest unused 3 bits */
672 static void create_ptd_int(struct isp1760_qh *qh,
673 struct isp1760_qtd *qtd, struct ptd *ptd)
675 create_ptd_atl(qh, qtd, ptd);
676 transform_add_int(qh, qtd, ptd);
679 static void isp1760_urb_done(struct usb_hcd *hcd, struct urb *urb)
680 __releases(priv->lock)
681 __acquires(priv->lock)
683 struct isp1760_hcd *priv = hcd_to_priv(hcd);
685 if (!urb->unlinked) {
686 if (urb->status == -EINPROGRESS)
690 if (usb_pipein(urb->pipe) && usb_pipetype(urb->pipe) != PIPE_CONTROL) {
692 for (ptr = urb->transfer_buffer;
693 ptr < urb->transfer_buffer + urb->transfer_buffer_length;
695 flush_dcache_page(virt_to_page(ptr));
698 /* complete() can reenter this HCD */
699 usb_hcd_unlink_urb_from_ep(hcd, urb);
700 spin_unlock(&priv->lock);
701 usb_hcd_giveback_urb(hcd, urb, urb->status);
702 spin_lock(&priv->lock);
705 static struct isp1760_qtd *qtd_alloc(gfp_t flags, struct urb *urb,
708 struct isp1760_qtd *qtd;
710 qtd = kmem_cache_zalloc(qtd_cachep, flags);
714 INIT_LIST_HEAD(&qtd->qtd_list);
716 qtd->packet_type = packet_type;
717 qtd->status = QTD_ENQUEUED;
718 qtd->actual_length = 0;
723 static void qtd_free(struct isp1760_qtd *qtd)
725 WARN_ON(qtd->payload_addr);
726 kmem_cache_free(qtd_cachep, qtd);
729 static void start_bus_transfer(struct usb_hcd *hcd, u32 ptd_offset, int slot,
730 struct isp1760_slotinfo *slots,
731 struct isp1760_qtd *qtd, struct isp1760_qh *qh,
734 struct isp1760_hcd *priv = hcd_to_priv(hcd);
737 WARN_ON((slot < 0) || (slot > 31));
738 WARN_ON(qtd->length && !qtd->payload_addr);
739 WARN_ON(slots[slot].qtd);
740 WARN_ON(slots[slot].qh);
741 WARN_ON(qtd->status != QTD_PAYLOAD_ALLOC);
743 /* Make sure done map has not triggered from some unlinked transfer */
744 if (ptd_offset == ATL_PTD_OFFSET) {
745 priv->atl_done_map |= reg_read32(hcd->regs,
746 HC_ATL_PTD_DONEMAP_REG);
747 priv->atl_done_map &= ~(1 << slot);
749 priv->int_done_map |= reg_read32(hcd->regs,
750 HC_INT_PTD_DONEMAP_REG);
751 priv->int_done_map &= ~(1 << slot);
755 qtd->status = QTD_XFER_STARTED;
756 slots[slot].timestamp = jiffies;
757 slots[slot].qtd = qtd;
759 ptd_write(hcd->regs, ptd_offset, slot, ptd);
761 if (ptd_offset == ATL_PTD_OFFSET) {
762 skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
763 skip_map &= ~(1 << qh->slot);
764 reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, skip_map);
766 skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
767 skip_map &= ~(1 << qh->slot);
768 reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, skip_map);
772 static int is_short_bulk(struct isp1760_qtd *qtd)
774 return (usb_pipebulk(qtd->urb->pipe) &&
775 (qtd->actual_length < qtd->length));
778 static void collect_qtds(struct usb_hcd *hcd, struct isp1760_qh *qh,
779 struct list_head *urb_list)
782 struct isp1760_qtd *qtd, *qtd_next;
783 struct urb_listitem *urb_listitem;
785 list_for_each_entry_safe(qtd, qtd_next, &qh->qtd_list, qtd_list) {
786 if (qtd->status < QTD_XFER_COMPLETE)
789 last_qtd = last_qtd_of_urb(qtd, qh);
791 if ((!last_qtd) && (qtd->status == QTD_RETIRE))
792 qtd_next->status = QTD_RETIRE;
794 if (qtd->status == QTD_XFER_COMPLETE) {
795 if (qtd->actual_length) {
796 switch (qtd->packet_type) {
798 mem_reads8(hcd->regs, qtd->payload_addr,
801 /* Fall through (?) */
803 qtd->urb->actual_length +=
805 /* Fall through ... */
811 if (is_short_bulk(qtd)) {
812 if (qtd->urb->transfer_flags & URB_SHORT_NOT_OK)
813 qtd->urb->status = -EREMOTEIO;
815 qtd_next->status = QTD_RETIRE;
819 if (qtd->payload_addr)
823 if ((qtd->status == QTD_RETIRE) &&
824 (qtd->urb->status == -EINPROGRESS))
825 qtd->urb->status = -EPIPE;
826 /* Defer calling of urb_done() since it releases lock */
827 urb_listitem = kmem_cache_zalloc(urb_listitem_cachep,
829 if (unlikely(!urb_listitem))
830 break; /* Try again on next call */
831 urb_listitem->urb = qtd->urb;
832 list_add_tail(&urb_listitem->urb_list, urb_list);
835 list_del(&qtd->qtd_list);
840 #define ENQUEUE_DEPTH 2
841 static void enqueue_qtds(struct usb_hcd *hcd, struct isp1760_qh *qh)
843 struct isp1760_hcd *priv = hcd_to_priv(hcd);
845 struct isp1760_slotinfo *slots;
846 int curr_slot, free_slot;
849 struct isp1760_qtd *qtd;
851 if (unlikely(list_empty(&qh->qtd_list))) {
856 /* Make sure this endpoint's TT buffer is clean before queueing ptds */
857 if (qh->tt_buffer_dirty)
860 if (usb_pipeint(list_entry(qh->qtd_list.next, struct isp1760_qtd,
861 qtd_list)->urb->pipe)) {
862 ptd_offset = INT_PTD_OFFSET;
863 slots = priv->int_slots;
865 ptd_offset = ATL_PTD_OFFSET;
866 slots = priv->atl_slots;
870 for (curr_slot = 0; curr_slot < 32; curr_slot++) {
871 if ((free_slot == -1) && (slots[curr_slot].qtd == NULL))
872 free_slot = curr_slot;
873 if (slots[curr_slot].qh == qh)
878 list_for_each_entry(qtd, &qh->qtd_list, qtd_list) {
879 if (qtd->status == QTD_ENQUEUED) {
880 WARN_ON(qtd->payload_addr);
882 if ((qtd->length) && (!qtd->payload_addr))
886 ((qtd->packet_type == SETUP_PID) ||
887 (qtd->packet_type == OUT_PID))) {
888 mem_writes8(hcd->regs, qtd->payload_addr,
889 qtd->data_buffer, qtd->length);
892 qtd->status = QTD_PAYLOAD_ALLOC;
895 if (qtd->status == QTD_PAYLOAD_ALLOC) {
897 if ((curr_slot > 31) && (free_slot == -1))
898 dev_dbg(hcd->self.controller, "%s: No slot "
899 "available for transfer\n", __func__);
901 /* Start xfer for this endpoint if not already done */
902 if ((curr_slot > 31) && (free_slot > -1)) {
903 if (usb_pipeint(qtd->urb->pipe))
904 create_ptd_int(qh, qtd, &ptd);
906 create_ptd_atl(qh, qtd, &ptd);
908 start_bus_transfer(hcd, ptd_offset, free_slot,
909 slots, qtd, qh, &ptd);
910 curr_slot = free_slot;
914 if (n >= ENQUEUE_DEPTH)
920 static void schedule_ptds(struct usb_hcd *hcd)
922 struct isp1760_hcd *priv;
923 struct isp1760_qh *qh, *qh_next;
924 struct list_head *ep_queue;
926 struct urb_listitem *urb_listitem, *urb_listitem_next;
934 priv = hcd_to_priv(hcd);
937 * check finished/retired xfers, transfer payloads, call urb_done()
939 for (i = 0; i < QH_END; i++) {
940 ep_queue = &priv->qh_list[i];
941 list_for_each_entry_safe(qh, qh_next, ep_queue, qh_list) {
942 collect_qtds(hcd, qh, &urb_list);
943 if (list_empty(&qh->qtd_list))
944 list_del(&qh->qh_list);
948 list_for_each_entry_safe(urb_listitem, urb_listitem_next, &urb_list,
950 isp1760_urb_done(hcd, urb_listitem->urb);
951 kmem_cache_free(urb_listitem_cachep, urb_listitem);
955 * Schedule packets for transfer.
957 * According to USB2.0 specification:
959 * 1st prio: interrupt xfers, up to 80 % of bandwidth
960 * 2nd prio: control xfers
961 * 3rd prio: bulk xfers
963 * ... but let's use a simpler scheme here (mostly because ISP1761 doc
964 * is very unclear on how to prioritize traffic):
966 * 1) Enqueue any queued control transfers, as long as payload chip mem
967 * and PTD ATL slots are available.
968 * 2) Enqueue any queued INT transfers, as long as payload chip mem
969 * and PTD INT slots are available.
970 * 3) Enqueue any queued bulk transfers, as long as payload chip mem
971 * and PTD ATL slots are available.
973 * Use double buffering (ENQUEUE_DEPTH==2) as a compromise between
974 * conservation of chip mem and performance.
976 * I'm sure this scheme could be improved upon!
978 for (i = 0; i < QH_END; i++) {
979 ep_queue = &priv->qh_list[i];
980 list_for_each_entry_safe(qh, qh_next, ep_queue, qh_list)
981 enqueue_qtds(hcd, qh);
985 #define PTD_STATE_QTD_DONE 1
986 #define PTD_STATE_QTD_RELOAD 2
987 #define PTD_STATE_URB_RETIRE 3
989 static int check_int_transfer(struct usb_hcd *hcd, struct ptd *ptd,
998 /* FIXME: ISP1761 datasheet does not say what to do with these. Do we
999 need to handle these errors? Is it done in hardware? */
1001 if (ptd->dw3 & DW3_HALT_BIT) {
1003 urb->status = -EPROTO; /* Default unknown error */
1005 for (i = 0; i < 8; i++) {
1006 switch (dw4 & 0x7) {
1008 dev_dbg(hcd->self.controller, "%s: underrun "
1009 "during uFrame %d\n",
1011 urb->status = -ECOMM; /* Could not write data */
1014 dev_dbg(hcd->self.controller, "%s: transaction "
1015 "error during uFrame %d\n",
1017 urb->status = -EPROTO; /* timeout, bad CRC, PID
1021 dev_dbg(hcd->self.controller, "%s: babble "
1022 "error during uFrame %d\n",
1024 urb->status = -EOVERFLOW;
1030 return PTD_STATE_URB_RETIRE;
1033 return PTD_STATE_QTD_DONE;
1036 static int check_atl_transfer(struct usb_hcd *hcd, struct ptd *ptd,
1040 if (ptd->dw3 & DW3_HALT_BIT) {
1041 if (ptd->dw3 & DW3_BABBLE_BIT)
1042 urb->status = -EOVERFLOW;
1043 else if (FROM_DW3_CERR(ptd->dw3))
1044 urb->status = -EPIPE; /* Stall */
1045 else if (ptd->dw3 & DW3_ERROR_BIT)
1046 urb->status = -EPROTO; /* XactErr */
1048 urb->status = -EPROTO; /* Unknown */
1050 dev_dbg(hcd->self.controller, "%s: ptd error:\n"
1051 " dw0: %08x dw1: %08x dw2: %08x dw3: %08x\n"
1052 " dw4: %08x dw5: %08x dw6: %08x dw7: %08x\n",
1054 ptd->dw0, ptd->dw1, ptd->dw2, ptd->dw3,
1055 ptd->dw4, ptd->dw5, ptd->dw6, ptd->dw7);
1057 return PTD_STATE_URB_RETIRE;
1060 if ((ptd->dw3 & DW3_ERROR_BIT) && (ptd->dw3 & DW3_ACTIVE_BIT)) {
1061 /* Transfer Error, *but* active and no HALT -> reload */
1062 dev_dbg(hcd->self.controller, "PID error; reloading ptd\n");
1063 return PTD_STATE_QTD_RELOAD;
1066 if (!FROM_DW3_NAKCOUNT(ptd->dw3) && (ptd->dw3 & DW3_ACTIVE_BIT)) {
1068 * NAKs are handled in HW by the chip. Usually if the
1069 * device is not able to send data fast enough.
1070 * This happens mostly on slower hardware.
1072 return PTD_STATE_QTD_RELOAD;
1075 return PTD_STATE_QTD_DONE;
1078 static void handle_done_ptds(struct usb_hcd *hcd)
1080 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1082 struct isp1760_qh *qh;
1085 struct isp1760_slotinfo *slots;
1087 struct isp1760_qtd *qtd;
1091 skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
1092 priv->int_done_map &= ~skip_map;
1093 skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
1094 priv->atl_done_map &= ~skip_map;
1096 modified = priv->int_done_map || priv->atl_done_map;
1098 while (priv->int_done_map || priv->atl_done_map) {
1099 if (priv->int_done_map) {
1101 slot = __ffs(priv->int_done_map);
1102 priv->int_done_map &= ~(1 << slot);
1103 slots = priv->int_slots;
1104 /* This should not trigger, and could be removed if
1105 noone have any problems with it triggering: */
1106 if (!slots[slot].qh) {
1110 ptd_offset = INT_PTD_OFFSET;
1111 ptd_read(hcd->regs, INT_PTD_OFFSET, slot, &ptd);
1112 state = check_int_transfer(hcd, &ptd,
1113 slots[slot].qtd->urb);
1116 slot = __ffs(priv->atl_done_map);
1117 priv->atl_done_map &= ~(1 << slot);
1118 slots = priv->atl_slots;
1119 /* This should not trigger, and could be removed if
1120 noone have any problems with it triggering: */
1121 if (!slots[slot].qh) {
1125 ptd_offset = ATL_PTD_OFFSET;
1126 ptd_read(hcd->regs, ATL_PTD_OFFSET, slot, &ptd);
1127 state = check_atl_transfer(hcd, &ptd,
1128 slots[slot].qtd->urb);
1131 qtd = slots[slot].qtd;
1132 slots[slot].qtd = NULL;
1133 qh = slots[slot].qh;
1134 slots[slot].qh = NULL;
1137 WARN_ON(qtd->status != QTD_XFER_STARTED);
1140 case PTD_STATE_QTD_DONE:
1141 if ((usb_pipeint(qtd->urb->pipe)) &&
1142 (qtd->urb->dev->speed != USB_SPEED_HIGH))
1143 qtd->actual_length =
1144 FROM_DW3_SCS_NRBYTESTRANSFERRED(ptd.dw3);
1146 qtd->actual_length =
1147 FROM_DW3_NRBYTESTRANSFERRED(ptd.dw3);
1149 qtd->status = QTD_XFER_COMPLETE;
1150 if (list_is_last(&qtd->qtd_list, &qh->qtd_list) ||
1154 qtd = list_entry(qtd->qtd_list.next,
1155 typeof(*qtd), qtd_list);
1157 qh->toggle = FROM_DW3_DATA_TOGGLE(ptd.dw3);
1158 qh->ping = FROM_DW3_PING(ptd.dw3);
1161 case PTD_STATE_QTD_RELOAD: /* QTD_RETRY, for atls only */
1162 qtd->status = QTD_PAYLOAD_ALLOC;
1163 ptd.dw0 |= DW0_VALID_BIT;
1164 /* RL counter = ERR counter */
1165 ptd.dw3 &= ~TO_DW3_NAKCOUNT(0xf);
1166 ptd.dw3 |= TO_DW3_NAKCOUNT(FROM_DW2_RL(ptd.dw2));
1167 ptd.dw3 &= ~TO_DW3_CERR(3);
1168 ptd.dw3 |= TO_DW3_CERR(ERR_COUNTER);
1169 qh->toggle = FROM_DW3_DATA_TOGGLE(ptd.dw3);
1170 qh->ping = FROM_DW3_PING(ptd.dw3);
1173 case PTD_STATE_URB_RETIRE:
1174 qtd->status = QTD_RETIRE;
1175 if ((qtd->urb->dev->speed != USB_SPEED_HIGH) &&
1176 (qtd->urb->status != -EPIPE) &&
1177 (qtd->urb->status != -EREMOTEIO)) {
1178 qh->tt_buffer_dirty = 1;
1179 if (usb_hub_clear_tt_buffer(qtd->urb))
1180 /* Clear failed; let's hope things work
1182 qh->tt_buffer_dirty = 0;
1194 if (qtd && (qtd->status == QTD_PAYLOAD_ALLOC)) {
1195 if (slots == priv->int_slots) {
1196 if (state == PTD_STATE_QTD_RELOAD)
1197 dev_err(hcd->self.controller,
1198 "%s: PTD_STATE_QTD_RELOAD on "
1199 "interrupt packet\n", __func__);
1200 if (state != PTD_STATE_QTD_RELOAD)
1201 create_ptd_int(qh, qtd, &ptd);
1203 if (state != PTD_STATE_QTD_RELOAD)
1204 create_ptd_atl(qh, qtd, &ptd);
1207 start_bus_transfer(hcd, ptd_offset, slot, slots, qtd,
1216 static irqreturn_t isp1760_irq(struct usb_hcd *hcd)
1218 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1220 irqreturn_t irqret = IRQ_NONE;
1222 spin_lock(&priv->lock);
1224 if (!(hcd->state & HC_STATE_RUNNING))
1227 imask = reg_read32(hcd->regs, HC_INTERRUPT_REG);
1228 if (unlikely(!imask))
1230 reg_write32(hcd->regs, HC_INTERRUPT_REG, imask); /* Clear */
1232 priv->int_done_map |= reg_read32(hcd->regs, HC_INT_PTD_DONEMAP_REG);
1233 priv->atl_done_map |= reg_read32(hcd->regs, HC_ATL_PTD_DONEMAP_REG);
1235 handle_done_ptds(hcd);
1237 irqret = IRQ_HANDLED;
1239 spin_unlock(&priv->lock);
1245 * Workaround for problem described in chip errata 2:
1247 * Sometimes interrupts are not generated when ATL (not INT?) completion occurs.
1248 * One solution suggested in the errata is to use SOF interrupts _instead_of_
1249 * ATL done interrupts (the "instead of" might be important since it seems
1250 * enabling ATL interrupts also causes the chip to sometimes - rarely - "forget"
1251 * to set the PTD's done bit in addition to not generating an interrupt!).
1253 * So if we use SOF + ATL interrupts, we sometimes get stale PTDs since their
1254 * done bit is not being set. This is bad - it blocks the endpoint until reboot.
1256 * If we use SOF interrupts only, we get latency between ptd completion and the
1257 * actual handling. This is very noticeable in testusb runs which takes several
1258 * minutes longer without ATL interrupts.
1260 * A better solution is to run the code below every SLOT_CHECK_PERIOD ms. If it
1261 * finds active ATL slots which are older than SLOT_TIMEOUT ms, it checks the
1262 * slot's ACTIVE and VALID bits. If these are not set, the ptd is considered
1263 * completed and its done map bit is set.
1265 * The values of SLOT_TIMEOUT and SLOT_CHECK_PERIOD have been arbitrarily chosen
1266 * not to cause too much lag when this HW bug occurs, while still hopefully
1267 * ensuring that the check does not falsely trigger.
1269 #define SLOT_TIMEOUT 300
1270 #define SLOT_CHECK_PERIOD 200
1271 static struct timer_list errata2_timer;
1273 static void errata2_function(unsigned long data)
1275 struct usb_hcd *hcd = (struct usb_hcd *) data;
1276 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1279 unsigned long spinflags;
1281 spin_lock_irqsave(&priv->lock, spinflags);
1283 for (slot = 0; slot < 32; slot++)
1284 if (priv->atl_slots[slot].qh && time_after(jiffies,
1285 priv->atl_slots[slot].timestamp +
1286 SLOT_TIMEOUT * HZ / 1000)) {
1287 ptd_read(hcd->regs, ATL_PTD_OFFSET, slot, &ptd);
1288 if (!FROM_DW0_VALID(ptd.dw0) &&
1289 !FROM_DW3_ACTIVE(ptd.dw3))
1290 priv->atl_done_map |= 1 << slot;
1293 if (priv->atl_done_map)
1294 handle_done_ptds(hcd);
1296 spin_unlock_irqrestore(&priv->lock, spinflags);
1298 errata2_timer.expires = jiffies + SLOT_CHECK_PERIOD * HZ / 1000;
1299 add_timer(&errata2_timer);
1302 static int isp1760_run(struct usb_hcd *hcd)
1309 hcd->uses_new_polling = 1;
1311 hcd->state = HC_STATE_RUNNING;
1313 /* Set PTD interrupt AND & OR maps */
1314 reg_write32(hcd->regs, HC_ATL_IRQ_MASK_AND_REG, 0);
1315 reg_write32(hcd->regs, HC_ATL_IRQ_MASK_OR_REG, 0xffffffff);
1316 reg_write32(hcd->regs, HC_INT_IRQ_MASK_AND_REG, 0);
1317 reg_write32(hcd->regs, HC_INT_IRQ_MASK_OR_REG, 0xffffffff);
1318 reg_write32(hcd->regs, HC_ISO_IRQ_MASK_AND_REG, 0);
1319 reg_write32(hcd->regs, HC_ISO_IRQ_MASK_OR_REG, 0xffffffff);
1320 /* step 23 passed */
1322 temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
1323 reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp | HW_GLOBAL_INTR_EN);
1325 command = reg_read32(hcd->regs, HC_USBCMD);
1326 command &= ~(CMD_LRESET|CMD_RESET);
1328 reg_write32(hcd->regs, HC_USBCMD, command);
1330 retval = handshake(hcd, HC_USBCMD, CMD_RUN, CMD_RUN, 250 * 1000);
1336 * Spec says to write FLAG_CF as last config action, priv code grabs
1337 * the semaphore while doing so.
1339 down_write(&ehci_cf_port_reset_rwsem);
1340 reg_write32(hcd->regs, HC_CONFIGFLAG, FLAG_CF);
1342 retval = handshake(hcd, HC_CONFIGFLAG, FLAG_CF, FLAG_CF, 250 * 1000);
1343 up_write(&ehci_cf_port_reset_rwsem);
1347 init_timer(&errata2_timer);
1348 errata2_timer.function = errata2_function;
1349 errata2_timer.data = (unsigned long) hcd;
1350 errata2_timer.expires = jiffies + SLOT_CHECK_PERIOD * HZ / 1000;
1351 add_timer(&errata2_timer);
1353 chipid = reg_read32(hcd->regs, HC_CHIP_ID_REG);
1354 dev_info(hcd->self.controller, "USB ISP %04x HW rev. %d started\n",
1355 chipid & 0xffff, chipid >> 16);
1357 /* PTD Register Init Part 2, Step 28 */
1359 /* Setup registers controlling PTD checking */
1360 reg_write32(hcd->regs, HC_ATL_PTD_LASTPTD_REG, 0x80000000);
1361 reg_write32(hcd->regs, HC_INT_PTD_LASTPTD_REG, 0x80000000);
1362 reg_write32(hcd->regs, HC_ISO_PTD_LASTPTD_REG, 0x00000001);
1363 reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, 0xffffffff);
1364 reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, 0xffffffff);
1365 reg_write32(hcd->regs, HC_ISO_PTD_SKIPMAP_REG, 0xffffffff);
1366 reg_write32(hcd->regs, HC_BUFFER_STATUS_REG,
1367 ATL_BUF_FILL | INT_BUF_FILL);
1369 /* GRR this is run-once init(), being done every time the HC starts.
1370 * So long as they're part of class devices, we can't do it init()
1371 * since the class device isn't created that early.
1376 static int qtd_fill(struct isp1760_qtd *qtd, void *databuffer, size_t len)
1378 qtd->data_buffer = databuffer;
1380 if (len > MAX_PAYLOAD_SIZE)
1381 len = MAX_PAYLOAD_SIZE;
1387 static void qtd_list_free(struct list_head *qtd_list)
1389 struct isp1760_qtd *qtd, *qtd_next;
1391 list_for_each_entry_safe(qtd, qtd_next, qtd_list, qtd_list) {
1392 list_del(&qtd->qtd_list);
1398 * Packetize urb->transfer_buffer into list of packets of size wMaxPacketSize.
1399 * Also calculate the PID type (SETUP/IN/OUT) for each packet.
1401 #define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
1402 static void packetize_urb(struct usb_hcd *hcd,
1403 struct urb *urb, struct list_head *head, gfp_t flags)
1405 struct isp1760_qtd *qtd;
1407 int len, maxpacketsize;
1411 * URBs map to sequences of QTDs: one logical transaction
1414 if (!urb->transfer_buffer && urb->transfer_buffer_length) {
1415 /* XXX This looks like usb storage / SCSI bug */
1416 dev_err(hcd->self.controller,
1417 "buf is null, dma is %08lx len is %d\n",
1418 (long unsigned)urb->transfer_dma,
1419 urb->transfer_buffer_length);
1423 if (usb_pipein(urb->pipe))
1424 packet_type = IN_PID;
1426 packet_type = OUT_PID;
1428 if (usb_pipecontrol(urb->pipe)) {
1429 qtd = qtd_alloc(flags, urb, SETUP_PID);
1432 qtd_fill(qtd, urb->setup_packet, sizeof(struct usb_ctrlrequest));
1433 list_add_tail(&qtd->qtd_list, head);
1435 /* for zero length DATA stages, STATUS is always IN */
1436 if (urb->transfer_buffer_length == 0)
1437 packet_type = IN_PID;
1440 maxpacketsize = max_packet(usb_maxpacket(urb->dev, urb->pipe,
1441 usb_pipeout(urb->pipe)));
1444 * buffer gets wrapped in one or more qtds;
1445 * last one may be "short" (including zero len)
1446 * and may serve as a control status ack
1448 buf = urb->transfer_buffer;
1449 len = urb->transfer_buffer_length;
1454 qtd = qtd_alloc(flags, urb, packet_type);
1457 this_qtd_len = qtd_fill(qtd, buf, len);
1458 list_add_tail(&qtd->qtd_list, head);
1460 len -= this_qtd_len;
1461 buf += this_qtd_len;
1468 * control requests may need a terminating data "status" ack;
1469 * bulk ones may need a terminating short packet (zero length).
1471 if (urb->transfer_buffer_length != 0) {
1474 if (usb_pipecontrol(urb->pipe)) {
1476 if (packet_type == IN_PID)
1477 packet_type = OUT_PID;
1479 packet_type = IN_PID;
1480 } else if (usb_pipebulk(urb->pipe)
1481 && (urb->transfer_flags & URB_ZERO_PACKET)
1482 && !(urb->transfer_buffer_length %
1487 qtd = qtd_alloc(flags, urb, packet_type);
1491 /* never any data in such packets */
1492 qtd_fill(qtd, NULL, 0);
1493 list_add_tail(&qtd->qtd_list, head);
1500 qtd_list_free(head);
1503 static int isp1760_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
1506 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1507 struct list_head *ep_queue;
1508 struct isp1760_qh *qh, *qhit;
1509 unsigned long spinflags;
1510 LIST_HEAD(new_qtds);
1514 switch (usb_pipetype(urb->pipe)) {
1516 ep_queue = &priv->qh_list[QH_CONTROL];
1519 ep_queue = &priv->qh_list[QH_BULK];
1521 case PIPE_INTERRUPT:
1522 if (urb->interval < 0)
1524 /* FIXME: Check bandwidth */
1525 ep_queue = &priv->qh_list[QH_INTERRUPT];
1527 case PIPE_ISOCHRONOUS:
1528 dev_err(hcd->self.controller, "%s: isochronous USB packets "
1529 "not yet supported\n",
1533 dev_err(hcd->self.controller, "%s: unknown pipe type\n",
1538 if (usb_pipein(urb->pipe))
1539 urb->actual_length = 0;
1541 packetize_urb(hcd, urb, &new_qtds, mem_flags);
1542 if (list_empty(&new_qtds))
1546 spin_lock_irqsave(&priv->lock, spinflags);
1548 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
1549 retval = -ESHUTDOWN;
1550 qtd_list_free(&new_qtds);
1553 retval = usb_hcd_link_urb_to_ep(hcd, urb);
1555 qtd_list_free(&new_qtds);
1559 qh = urb->ep->hcpriv;
1562 list_for_each_entry(qhit, ep_queue, qh_list) {
1569 list_add_tail(&qh->qh_list, ep_queue);
1571 qh = qh_alloc(GFP_ATOMIC);
1574 usb_hcd_unlink_urb_from_ep(hcd, urb);
1575 qtd_list_free(&new_qtds);
1578 list_add_tail(&qh->qh_list, ep_queue);
1579 urb->ep->hcpriv = qh;
1582 list_splice_tail(&new_qtds, &qh->qtd_list);
1586 spin_unlock_irqrestore(&priv->lock, spinflags);
1590 static void kill_transfer(struct usb_hcd *hcd, struct urb *urb,
1591 struct isp1760_qh *qh)
1593 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1596 WARN_ON(qh->slot == -1);
1598 /* We need to forcefully reclaim the slot since some transfers never
1599 return, e.g. interrupt transfers and NAKed bulk transfers. */
1600 if (usb_pipecontrol(urb->pipe) || usb_pipebulk(urb->pipe)) {
1601 skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
1602 skip_map |= (1 << qh->slot);
1603 reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, skip_map);
1604 priv->atl_slots[qh->slot].qh = NULL;
1605 priv->atl_slots[qh->slot].qtd = NULL;
1607 skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
1608 skip_map |= (1 << qh->slot);
1609 reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, skip_map);
1610 priv->int_slots[qh->slot].qh = NULL;
1611 priv->int_slots[qh->slot].qtd = NULL;
1618 * Retire the qtds beginning at 'qtd' and belonging all to the same urb, killing
1619 * any active transfer belonging to the urb in the process.
1621 static void dequeue_urb_from_qtd(struct usb_hcd *hcd, struct isp1760_qh *qh,
1622 struct isp1760_qtd *qtd)
1625 int urb_was_running;
1628 urb_was_running = 0;
1629 list_for_each_entry_from(qtd, &qh->qtd_list, qtd_list) {
1630 if (qtd->urb != urb)
1633 if (qtd->status >= QTD_XFER_STARTED)
1634 urb_was_running = 1;
1635 if (last_qtd_of_urb(qtd, qh) &&
1636 (qtd->status >= QTD_XFER_COMPLETE))
1637 urb_was_running = 0;
1639 if (qtd->status == QTD_XFER_STARTED)
1640 kill_transfer(hcd, urb, qh);
1641 qtd->status = QTD_RETIRE;
1644 if ((urb->dev->speed != USB_SPEED_HIGH) && urb_was_running) {
1645 qh->tt_buffer_dirty = 1;
1646 if (usb_hub_clear_tt_buffer(urb))
1647 /* Clear failed; let's hope things work anyway */
1648 qh->tt_buffer_dirty = 0;
1652 static int isp1760_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
1655 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1656 unsigned long spinflags;
1657 struct isp1760_qh *qh;
1658 struct isp1760_qtd *qtd;
1661 spin_lock_irqsave(&priv->lock, spinflags);
1662 retval = usb_hcd_check_unlink_urb(hcd, urb, status);
1666 qh = urb->ep->hcpriv;
1672 list_for_each_entry(qtd, &qh->qtd_list, qtd_list)
1673 if (qtd->urb == urb) {
1674 dequeue_urb_from_qtd(hcd, qh, qtd);
1675 list_move(&qtd->qtd_list, &qh->qtd_list);
1679 urb->status = status;
1683 spin_unlock_irqrestore(&priv->lock, spinflags);
1687 static void isp1760_endpoint_disable(struct usb_hcd *hcd,
1688 struct usb_host_endpoint *ep)
1690 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1691 unsigned long spinflags;
1692 struct isp1760_qh *qh, *qh_iter;
1695 spin_lock_irqsave(&priv->lock, spinflags);
1701 WARN_ON(!list_empty(&qh->qtd_list));
1703 for (i = 0; i < QH_END; i++)
1704 list_for_each_entry(qh_iter, &priv->qh_list[i], qh_list)
1705 if (qh_iter == qh) {
1706 list_del(&qh_iter->qh_list);
1716 spin_unlock_irqrestore(&priv->lock, spinflags);
1719 static int isp1760_hub_status_data(struct usb_hcd *hcd, char *buf)
1721 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1722 u32 temp, status = 0;
1725 unsigned long flags;
1727 /* if !PM, root hub timers won't get shut down ... */
1728 if (!HC_IS_RUNNING(hcd->state))
1731 /* init status to no-changes */
1735 spin_lock_irqsave(&priv->lock, flags);
1736 temp = reg_read32(hcd->regs, HC_PORTSC1);
1738 if (temp & PORT_OWNER) {
1739 if (temp & PORT_CSC) {
1741 reg_write32(hcd->regs, HC_PORTSC1, temp);
1747 * Return status information even for ports with OWNER set.
1748 * Otherwise hub_wq wouldn't see the disconnect event when a
1749 * high-speed device is switched over to the companion
1750 * controller by the user.
1753 if ((temp & mask) != 0
1754 || ((temp & PORT_RESUME) != 0
1755 && time_after_eq(jiffies,
1756 priv->reset_done))) {
1757 buf [0] |= 1 << (0 + 1);
1760 /* FIXME autosuspend idle root hubs */
1762 spin_unlock_irqrestore(&priv->lock, flags);
1763 return status ? retval : 0;
1766 static void isp1760_hub_descriptor(struct isp1760_hcd *priv,
1767 struct usb_hub_descriptor *desc)
1769 int ports = HCS_N_PORTS(priv->hcs_params);
1772 desc->bDescriptorType = 0x29;
1773 /* priv 1.0, 2.3.9 says 20ms max */
1774 desc->bPwrOn2PwrGood = 10;
1775 desc->bHubContrCurrent = 0;
1777 desc->bNbrPorts = ports;
1778 temp = 1 + (ports / 8);
1779 desc->bDescLength = 7 + 2 * temp;
1781 /* ports removable, and usb 1.0 legacy PortPwrCtrlMask */
1782 memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
1783 memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
1785 /* per-port overcurrent reporting */
1787 if (HCS_PPC(priv->hcs_params))
1788 /* per-port power control */
1791 /* no power switching */
1793 desc->wHubCharacteristics = cpu_to_le16(temp);
1796 #define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
1798 static int check_reset_complete(struct usb_hcd *hcd, int index,
1801 if (!(port_status & PORT_CONNECT))
1804 /* if reset finished and it's still not enabled -- handoff */
1805 if (!(port_status & PORT_PE)) {
1807 dev_info(hcd->self.controller,
1808 "port %d full speed --> companion\n",
1811 port_status |= PORT_OWNER;
1812 port_status &= ~PORT_RWC_BITS;
1813 reg_write32(hcd->regs, HC_PORTSC1, port_status);
1816 dev_info(hcd->self.controller, "port %d high speed\n",
1822 static int isp1760_hub_control(struct usb_hcd *hcd, u16 typeReq,
1823 u16 wValue, u16 wIndex, char *buf, u16 wLength)
1825 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1826 int ports = HCS_N_PORTS(priv->hcs_params);
1828 unsigned long flags;
1833 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
1834 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
1835 * (track current state ourselves) ... blink for diagnostics,
1836 * power, "this is the one", etc. EHCI spec supports this.
1839 spin_lock_irqsave(&priv->lock, flags);
1841 case ClearHubFeature:
1843 case C_HUB_LOCAL_POWER:
1844 case C_HUB_OVER_CURRENT:
1845 /* no hub-wide feature/status flags */
1851 case ClearPortFeature:
1852 if (!wIndex || wIndex > ports)
1855 temp = reg_read32(hcd->regs, HC_PORTSC1);
1858 * Even if OWNER is set, so the port is owned by the
1859 * companion controller, hub_wq needs to be able to clear
1860 * the port-change status bits (especially
1861 * USB_PORT_STAT_C_CONNECTION).
1865 case USB_PORT_FEAT_ENABLE:
1866 reg_write32(hcd->regs, HC_PORTSC1, temp & ~PORT_PE);
1868 case USB_PORT_FEAT_C_ENABLE:
1871 case USB_PORT_FEAT_SUSPEND:
1872 if (temp & PORT_RESET)
1875 if (temp & PORT_SUSPEND) {
1876 if ((temp & PORT_PE) == 0)
1878 /* resume signaling for 20 msec */
1879 temp &= ~(PORT_RWC_BITS);
1880 reg_write32(hcd->regs, HC_PORTSC1,
1881 temp | PORT_RESUME);
1882 priv->reset_done = jiffies +
1883 msecs_to_jiffies(20);
1886 case USB_PORT_FEAT_C_SUSPEND:
1887 /* we auto-clear this feature */
1889 case USB_PORT_FEAT_POWER:
1890 if (HCS_PPC(priv->hcs_params))
1891 reg_write32(hcd->regs, HC_PORTSC1,
1892 temp & ~PORT_POWER);
1894 case USB_PORT_FEAT_C_CONNECTION:
1895 reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_CSC);
1897 case USB_PORT_FEAT_C_OVER_CURRENT:
1900 case USB_PORT_FEAT_C_RESET:
1901 /* GetPortStatus clears reset */
1906 reg_read32(hcd->regs, HC_USBCMD);
1908 case GetHubDescriptor:
1909 isp1760_hub_descriptor(priv, (struct usb_hub_descriptor *)
1913 /* no hub-wide feature/status flags */
1917 if (!wIndex || wIndex > ports)
1921 temp = reg_read32(hcd->regs, HC_PORTSC1);
1923 /* wPortChange bits */
1924 if (temp & PORT_CSC)
1925 status |= USB_PORT_STAT_C_CONNECTION << 16;
1928 /* whoever resumes must GetPortStatus to complete it!! */
1929 if (temp & PORT_RESUME) {
1930 dev_err(hcd->self.controller, "Port resume should be skipped.\n");
1932 /* Remote Wakeup received? */
1933 if (!priv->reset_done) {
1934 /* resume signaling for 20 msec */
1935 priv->reset_done = jiffies
1936 + msecs_to_jiffies(20);
1937 /* check the port again */
1938 mod_timer(&hcd->rh_timer, priv->reset_done);
1941 /* resume completed? */
1942 else if (time_after_eq(jiffies,
1943 priv->reset_done)) {
1944 status |= USB_PORT_STAT_C_SUSPEND << 16;
1945 priv->reset_done = 0;
1947 /* stop resume signaling */
1948 temp = reg_read32(hcd->regs, HC_PORTSC1);
1949 reg_write32(hcd->regs, HC_PORTSC1,
1950 temp & ~(PORT_RWC_BITS | PORT_RESUME));
1951 retval = handshake(hcd, HC_PORTSC1,
1952 PORT_RESUME, 0, 2000 /* 2msec */);
1954 dev_err(hcd->self.controller,
1955 "port %d resume error %d\n",
1956 wIndex + 1, retval);
1959 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
1963 /* whoever resets must GetPortStatus to complete it!! */
1964 if ((temp & PORT_RESET)
1965 && time_after_eq(jiffies,
1966 priv->reset_done)) {
1967 status |= USB_PORT_STAT_C_RESET << 16;
1968 priv->reset_done = 0;
1970 /* force reset to complete */
1971 reg_write32(hcd->regs, HC_PORTSC1, temp & ~PORT_RESET);
1972 /* REVISIT: some hardware needs 550+ usec to clear
1973 * this bit; seems too long to spin routinely...
1975 retval = handshake(hcd, HC_PORTSC1,
1976 PORT_RESET, 0, 750);
1978 dev_err(hcd->self.controller, "port %d reset error %d\n",
1979 wIndex + 1, retval);
1983 /* see what we found out */
1984 temp = check_reset_complete(hcd, wIndex,
1985 reg_read32(hcd->regs, HC_PORTSC1));
1988 * Even if OWNER is set, there's no harm letting hub_wq
1989 * see the wPortStatus values (they should all be 0 except
1990 * for PORT_POWER anyway).
1993 if (temp & PORT_OWNER)
1994 dev_err(hcd->self.controller, "PORT_OWNER is set\n");
1996 if (temp & PORT_CONNECT) {
1997 status |= USB_PORT_STAT_CONNECTION;
1998 /* status may be from integrated TT */
1999 status |= USB_PORT_STAT_HIGH_SPEED;
2002 status |= USB_PORT_STAT_ENABLE;
2003 if (temp & (PORT_SUSPEND|PORT_RESUME))
2004 status |= USB_PORT_STAT_SUSPEND;
2005 if (temp & PORT_RESET)
2006 status |= USB_PORT_STAT_RESET;
2007 if (temp & PORT_POWER)
2008 status |= USB_PORT_STAT_POWER;
2010 put_unaligned(cpu_to_le32(status), (__le32 *) buf);
2014 case C_HUB_LOCAL_POWER:
2015 case C_HUB_OVER_CURRENT:
2016 /* no hub-wide feature/status flags */
2022 case SetPortFeature:
2023 selector = wIndex >> 8;
2025 if (!wIndex || wIndex > ports)
2028 temp = reg_read32(hcd->regs, HC_PORTSC1);
2029 if (temp & PORT_OWNER)
2032 /* temp &= ~PORT_RWC_BITS; */
2034 case USB_PORT_FEAT_ENABLE:
2035 reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_PE);
2038 case USB_PORT_FEAT_SUSPEND:
2039 if ((temp & PORT_PE) == 0
2040 || (temp & PORT_RESET) != 0)
2043 reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_SUSPEND);
2045 case USB_PORT_FEAT_POWER:
2046 if (HCS_PPC(priv->hcs_params))
2047 reg_write32(hcd->regs, HC_PORTSC1,
2050 case USB_PORT_FEAT_RESET:
2051 if (temp & PORT_RESUME)
2053 /* line status bits may report this as low speed,
2054 * which can be fine if this root hub has a
2055 * transaction translator built in.
2057 if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
2058 && PORT_USB11(temp)) {
2065 * caller must wait, then call GetPortStatus
2066 * usb 2.0 spec says 50 ms resets on root
2068 priv->reset_done = jiffies +
2069 msecs_to_jiffies(50);
2071 reg_write32(hcd->regs, HC_PORTSC1, temp);
2076 reg_read32(hcd->regs, HC_USBCMD);
2081 /* "stall" on error */
2084 spin_unlock_irqrestore(&priv->lock, flags);
2088 static int isp1760_get_frame(struct usb_hcd *hcd)
2090 struct isp1760_hcd *priv = hcd_to_priv(hcd);
2093 fr = reg_read32(hcd->regs, HC_FRINDEX);
2094 return (fr >> 3) % priv->periodic_size;
2097 static void isp1760_stop(struct usb_hcd *hcd)
2099 struct isp1760_hcd *priv = hcd_to_priv(hcd);
2102 del_timer(&errata2_timer);
2104 isp1760_hub_control(hcd, ClearPortFeature, USB_PORT_FEAT_POWER, 1,
2108 spin_lock_irq(&priv->lock);
2111 temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
2112 reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp &= ~HW_GLOBAL_INTR_EN);
2113 spin_unlock_irq(&priv->lock);
2115 reg_write32(hcd->regs, HC_CONFIGFLAG, 0);
2118 static void isp1760_shutdown(struct usb_hcd *hcd)
2123 temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
2124 reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp &= ~HW_GLOBAL_INTR_EN);
2126 command = reg_read32(hcd->regs, HC_USBCMD);
2127 command &= ~CMD_RUN;
2128 reg_write32(hcd->regs, HC_USBCMD, command);
2131 static void isp1760_clear_tt_buffer_complete(struct usb_hcd *hcd,
2132 struct usb_host_endpoint *ep)
2134 struct isp1760_hcd *priv = hcd_to_priv(hcd);
2135 struct isp1760_qh *qh = ep->hcpriv;
2136 unsigned long spinflags;
2141 spin_lock_irqsave(&priv->lock, spinflags);
2142 qh->tt_buffer_dirty = 0;
2144 spin_unlock_irqrestore(&priv->lock, spinflags);
2148 static const struct hc_driver isp1760_hc_driver = {
2149 .description = "isp1760-hcd",
2150 .product_desc = "NXP ISP1760 USB Host Controller",
2151 .hcd_priv_size = sizeof(struct isp1760_hcd *),
2153 .flags = HCD_MEMORY | HCD_USB2,
2154 .reset = isp1760_hc_setup,
2155 .start = isp1760_run,
2156 .stop = isp1760_stop,
2157 .shutdown = isp1760_shutdown,
2158 .urb_enqueue = isp1760_urb_enqueue,
2159 .urb_dequeue = isp1760_urb_dequeue,
2160 .endpoint_disable = isp1760_endpoint_disable,
2161 .get_frame_number = isp1760_get_frame,
2162 .hub_status_data = isp1760_hub_status_data,
2163 .hub_control = isp1760_hub_control,
2164 .clear_tt_buffer_complete = isp1760_clear_tt_buffer_complete,
2167 int __init isp1760_init_kmem_once(void)
2169 urb_listitem_cachep = kmem_cache_create("isp1760_urb_listitem",
2170 sizeof(struct urb_listitem), 0, SLAB_TEMPORARY |
2171 SLAB_MEM_SPREAD, NULL);
2173 if (!urb_listitem_cachep)
2176 qtd_cachep = kmem_cache_create("isp1760_qtd",
2177 sizeof(struct isp1760_qtd), 0, SLAB_TEMPORARY |
2178 SLAB_MEM_SPREAD, NULL);
2183 qh_cachep = kmem_cache_create("isp1760_qh", sizeof(struct isp1760_qh),
2184 0, SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
2187 kmem_cache_destroy(qtd_cachep);
2194 void isp1760_deinit_kmem_cache(void)
2196 kmem_cache_destroy(qtd_cachep);
2197 kmem_cache_destroy(qh_cachep);
2198 kmem_cache_destroy(urb_listitem_cachep);
2201 int isp1760_hcd_register(struct isp1760_hcd *priv, void __iomem *regs,
2202 struct resource *mem, int irq, unsigned long irqflags,
2205 struct usb_hcd *hcd;
2208 hcd = usb_create_hcd(&isp1760_hc_driver, dev, dev_name(dev));
2212 *(struct isp1760_hcd **)hcd->hcd_priv = priv;
2220 hcd->rsrc_start = mem->start;
2221 hcd->rsrc_len = resource_size(mem);
2223 ret = usb_add_hcd(hcd, irq, irqflags);
2227 device_wakeup_enable(hcd->self.controller);
2236 void isp1760_hcd_unregister(struct isp1760_hcd *priv)
2238 usb_remove_hcd(priv->hcd);
2239 usb_put_hcd(priv->hcd);