1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for the NXP ISP1760 chip
5 * However, the code might contain some bugs. What doesn't work for sure is:
8 e The interrupt line is configured as active low, level.
17 #include <linux/gpio/consumer.h>
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/slab.h>
21 #include <linux/list.h>
22 #include <linux/usb.h>
23 #include <linux/usb/hcd.h>
24 #include <linux/debugfs.h>
25 #include <linux/uaccess.h>
27 #include <linux/iopoll.h>
29 #include <linux/timer.h>
30 #include <linux/unaligned.h>
31 #include <asm/cacheflush.h>
33 #include "isp1760-core.h"
34 #include "isp1760-hcd.h"
35 #include "isp1760-regs.h"
37 static struct kmem_cache *qtd_cachep;
38 static struct kmem_cache *qh_cachep;
39 static struct kmem_cache *urb_listitem_cachep;
41 typedef void (packet_enqueue)(struct usb_hcd *hcd, struct isp1760_qh *qh,
42 struct isp1760_qtd *qtd);
44 static inline struct isp1760_hcd *hcd_to_priv(struct usb_hcd *hcd)
46 return *(struct isp1760_hcd **)hcd->hcd_priv;
49 #define dw_to_le32(x) (cpu_to_le32((__force u32)x))
50 #define le32_to_dw(x) ((__force __dw)(le32_to_cpu(x)))
53 #define DELETE_URB (0x0008)
54 #define NO_TRANSFER_ACTIVE (0xffffffff)
56 /* Philips Proprietary Transfer Descriptor (PTD) */
57 typedef __u32 __bitwise __dw;
80 #define PTD_OFFSET 0x0400
81 #define ISO_PTD_OFFSET 0x0400
82 #define INT_PTD_OFFSET 0x0800
83 #define ATL_PTD_OFFSET 0x0c00
84 #define PAYLOAD_OFFSET 0x1000
86 #define ISP_BANK_0 0x00
87 #define ISP_BANK_1 0x01
88 #define ISP_BANK_2 0x02
89 #define ISP_BANK_3 0x03
91 #define TO_DW(x) ((__force __dw)x)
92 #define TO_U32(x) ((__force u32)x)
96 #define DW0_VALID_BIT TO_DW(1)
97 #define FROM_DW0_VALID(x) (TO_U32(x) & 0x01)
98 #define TO_DW0_LENGTH(x) TO_DW((((u32)x) << 3))
99 #define TO_DW0_MAXPACKET(x) TO_DW((((u32)x) << 18))
100 #define TO_DW0_MULTI(x) TO_DW((((u32)x) << 29))
101 #define TO_DW0_ENDPOINT(x) TO_DW((((u32)x) << 31))
103 #define TO_DW1_DEVICE_ADDR(x) TO_DW((((u32)x) << 3))
104 #define TO_DW1_PID_TOKEN(x) TO_DW((((u32)x) << 10))
105 #define DW1_TRANS_BULK TO_DW(((u32)2 << 12))
106 #define DW1_TRANS_INT TO_DW(((u32)3 << 12))
107 #define DW1_TRANS_SPLIT TO_DW(((u32)1 << 14))
108 #define DW1_SE_USB_LOSPEED TO_DW(((u32)2 << 16))
109 #define TO_DW1_PORT_NUM(x) TO_DW((((u32)x) << 18))
110 #define TO_DW1_HUB_NUM(x) TO_DW((((u32)x) << 25))
112 #define TO_DW2_DATA_START_ADDR(x) TO_DW((((u32)x) << 8))
113 #define TO_DW2_RL(x) TO_DW(((x) << 25))
114 #define FROM_DW2_RL(x) ((TO_U32(x) >> 25) & 0xf)
116 #define FROM_DW3_NRBYTESTRANSFERRED(x) TO_U32((x) & 0x3fff)
117 #define FROM_DW3_SCS_NRBYTESTRANSFERRED(x) TO_U32((x) & 0x07ff)
118 #define TO_DW3_NAKCOUNT(x) TO_DW(((x) << 19))
119 #define FROM_DW3_NAKCOUNT(x) ((TO_U32(x) >> 19) & 0xf)
120 #define TO_DW3_CERR(x) TO_DW(((x) << 23))
121 #define FROM_DW3_CERR(x) ((TO_U32(x) >> 23) & 0x3)
122 #define TO_DW3_DATA_TOGGLE(x) TO_DW(((x) << 25))
123 #define FROM_DW3_DATA_TOGGLE(x) ((TO_U32(x) >> 25) & 0x1)
124 #define TO_DW3_PING(x) TO_DW(((x) << 26))
125 #define FROM_DW3_PING(x) ((TO_U32(x) >> 26) & 0x1)
126 #define DW3_ERROR_BIT TO_DW((1 << 28))
127 #define DW3_BABBLE_BIT TO_DW((1 << 29))
128 #define DW3_HALT_BIT TO_DW((1 << 30))
129 #define DW3_ACTIVE_BIT TO_DW((1 << 31))
130 #define FROM_DW3_ACTIVE(x) ((TO_U32(x) >> 31) & 0x01)
132 #define INT_UNDERRUN (1 << 2)
133 #define INT_BABBLE (1 << 1)
134 #define INT_EXACT (1 << 0)
136 #define SETUP_PID (2)
141 #define RL_COUNTER (0)
142 #define NAK_COUNTER (0)
143 #define ERR_COUNTER (3)
150 /* the rest is HCD-private */
151 struct list_head qtd_list;
154 size_t actual_length;
156 /* QTD_ENQUEUED: waiting for transfer (inactive) */
157 /* QTD_PAYLOAD_ALLOC: chip mem has been allocated for payload */
158 /* QTD_XFER_STARTED: valid ptd has been written to isp176x - only
159 interrupt handler may touch this qtd! */
160 /* QTD_XFER_COMPLETE: payload has been transferred successfully */
161 /* QTD_RETIRE: transfer error/abort qtd */
162 #define QTD_ENQUEUED 0
163 #define QTD_PAYLOAD_ALLOC 1
164 #define QTD_XFER_STARTED 2
165 #define QTD_XFER_COMPLETE 3
170 /* Queue head, one for each active endpoint */
172 struct list_head qh_list;
173 struct list_head qtd_list;
177 int tt_buffer_dirty; /* See USB2.0 spec section 11.17.5 */
180 struct urb_listitem {
181 struct list_head urb_list;
185 static const u32 isp176x_hc_portsc1_fields[] = {
186 [PORT_OWNER] = BIT(13),
187 [PORT_POWER] = BIT(12),
188 [PORT_LSTATUS] = BIT(10),
189 [PORT_RESET] = BIT(8),
190 [PORT_SUSPEND] = BIT(7),
191 [PORT_RESUME] = BIT(6),
194 [PORT_CONNECT] = BIT(0),
198 * Access functions for isp176x registers regmap fields
200 static u32 isp1760_hcd_read(struct usb_hcd *hcd, u32 field)
202 struct isp1760_hcd *priv = hcd_to_priv(hcd);
204 return isp1760_field_read(priv->fields, field);
208 * We need, in isp176x, to write directly the values to the portsc1
209 * register so it will make the other values to trigger.
211 static void isp1760_hcd_portsc1_set_clear(struct isp1760_hcd *priv, u32 field,
214 u32 bit = isp176x_hc_portsc1_fields[field];
215 u16 portsc1_reg = priv->is_isp1763 ? ISP1763_HC_PORTSC1 :
217 u32 port_status = readl(priv->base + portsc1_reg);
220 writel(port_status | bit, priv->base + portsc1_reg);
222 writel(port_status & ~bit, priv->base + portsc1_reg);
225 static void isp1760_hcd_write(struct usb_hcd *hcd, u32 field, u32 val)
227 struct isp1760_hcd *priv = hcd_to_priv(hcd);
229 if (unlikely((field >= PORT_OWNER && field <= PORT_CONNECT)))
230 return isp1760_hcd_portsc1_set_clear(priv, field, val);
232 isp1760_field_write(priv->fields, field, val);
235 static void isp1760_hcd_set(struct usb_hcd *hcd, u32 field)
237 isp1760_hcd_write(hcd, field, 0xFFFFFFFF);
240 static void isp1760_hcd_clear(struct usb_hcd *hcd, u32 field)
242 isp1760_hcd_write(hcd, field, 0);
245 static int isp1760_hcd_set_and_wait(struct usb_hcd *hcd, u32 field,
248 struct isp1760_hcd *priv = hcd_to_priv(hcd);
251 isp1760_hcd_set(hcd, field);
253 return regmap_field_read_poll_timeout(priv->fields[field], val,
257 static int isp1760_hcd_set_and_wait_swap(struct usb_hcd *hcd, u32 field,
260 struct isp1760_hcd *priv = hcd_to_priv(hcd);
263 isp1760_hcd_set(hcd, field);
265 return regmap_field_read_poll_timeout(priv->fields[field], val,
266 !val, 0, timeout_us);
269 static int isp1760_hcd_clear_and_wait(struct usb_hcd *hcd, u32 field,
272 struct isp1760_hcd *priv = hcd_to_priv(hcd);
275 isp1760_hcd_clear(hcd, field);
277 return regmap_field_read_poll_timeout(priv->fields[field], val,
278 !val, 0, timeout_us);
281 static bool isp1760_hcd_is_set(struct usb_hcd *hcd, u32 field)
283 return !!isp1760_hcd_read(hcd, field);
286 static bool isp1760_hcd_ppc_is_set(struct usb_hcd *hcd)
288 struct isp1760_hcd *priv = hcd_to_priv(hcd);
290 if (priv->is_isp1763)
293 return isp1760_hcd_is_set(hcd, HCS_PPC);
296 static u32 isp1760_hcd_n_ports(struct usb_hcd *hcd)
298 struct isp1760_hcd *priv = hcd_to_priv(hcd);
300 if (priv->is_isp1763)
303 return isp1760_hcd_read(hcd, HCS_N_PORTS);
307 * Access functions for isp176x memory (offset >= 0x0400).
309 * bank_reads8() reads memory locations prefetched by an earlier write to
310 * HC_MEMORY_REG (see isp176x datasheet). Unless you want to do fancy multi-
311 * bank optimizations, you should use the more generic mem_read() below.
313 * For access to ptd memory, use the specialized ptd_read() and ptd_write()
316 * These functions copy via MMIO data to/from the device. memcpy_{to|from}io()
317 * doesn't quite work because some people have to enforce 32-bit access
319 static void bank_reads8(void __iomem *src_base, u32 src_offset, u32 bank_addr,
320 __u32 *dst, u32 bytes)
327 src = src_base + (bank_addr | src_offset);
329 if (src_offset < PAYLOAD_OFFSET) {
331 *dst = readl_relaxed(src);
338 *dst = __raw_readl(src);
348 /* in case we have 3, 2 or 1 by left. The dst buffer may not be fully
351 if (src_offset < PAYLOAD_OFFSET)
352 val = readl_relaxed(src);
354 val = __raw_readl(src);
356 dst_byteptr = (void *) dst;
357 src_byteptr = (void *) &val;
359 *dst_byteptr = *src_byteptr;
366 static void isp1760_mem_read(struct usb_hcd *hcd, u32 src_offset, void *dst,
369 struct isp1760_hcd *priv = hcd_to_priv(hcd);
371 isp1760_reg_write(priv->regs, ISP176x_HC_MEMORY, src_offset);
374 bank_reads8(priv->base, src_offset, ISP_BANK_0, dst, bytes);
378 * ISP1763 does not have the banks direct host controller memory access,
379 * needs to use the HC_DATA register. Add data read/write according to this,
380 * and also adjust 16bit access.
382 static void isp1763_mem_read(struct usb_hcd *hcd, u16 srcaddr,
383 u16 *dstptr, u32 bytes)
385 struct isp1760_hcd *priv = hcd_to_priv(hcd);
387 /* Write the starting device address to the hcd memory register */
388 isp1760_reg_write(priv->regs, ISP1763_HC_MEMORY, srcaddr);
389 ndelay(100); /* Delay between consecutive access */
391 /* As long there are at least 16-bit to read ... */
393 *dstptr = __raw_readw(priv->base + ISP1763_HC_DATA);
398 /* If there are no more bytes to read, return */
402 *((u8 *)dstptr) = (u8)(readw(priv->base + ISP1763_HC_DATA) & 0xFF);
405 static void mem_read(struct usb_hcd *hcd, u32 src_offset, __u32 *dst,
408 struct isp1760_hcd *priv = hcd_to_priv(hcd);
410 if (!priv->is_isp1763)
411 return isp1760_mem_read(hcd, src_offset, (u16 *)dst, bytes);
413 isp1763_mem_read(hcd, (u16)src_offset, (u16 *)dst, bytes);
416 static void isp1760_mem_write(void __iomem *dst_base, u32 dst_offset,
417 __u32 const *src, u32 bytes)
421 dst = dst_base + dst_offset;
423 if (dst_offset < PAYLOAD_OFFSET) {
425 writel_relaxed(*src, dst);
432 __raw_writel(*src, dst);
441 /* in case we have 3, 2 or 1 bytes left. The buffer is allocated and the
442 * extra bytes should not be read by the HW.
445 if (dst_offset < PAYLOAD_OFFSET)
446 writel_relaxed(*src, dst);
448 __raw_writel(*src, dst);
451 static void isp1763_mem_write(struct usb_hcd *hcd, u16 dstaddr, u16 *src,
454 struct isp1760_hcd *priv = hcd_to_priv(hcd);
456 /* Write the starting device address to the hcd memory register */
457 isp1760_reg_write(priv->regs, ISP1763_HC_MEMORY, dstaddr);
458 ndelay(100); /* Delay between consecutive access */
461 /* Get and write the data; then adjust the data ptr and len */
462 __raw_writew(*src, priv->base + ISP1763_HC_DATA);
467 /* If there are no more bytes to process, return */
472 * The only way to get here is if there is a single byte left,
473 * get it and write it to the data reg;
475 writew(*((u8 *)src), priv->base + ISP1763_HC_DATA);
478 static void mem_write(struct usb_hcd *hcd, u32 dst_offset, __u32 *src,
481 struct isp1760_hcd *priv = hcd_to_priv(hcd);
483 if (!priv->is_isp1763)
484 return isp1760_mem_write(priv->base, dst_offset, src, bytes);
486 isp1763_mem_write(hcd, dst_offset, (u16 *)src, bytes);
490 * Read and write ptds. 'ptd_offset' should be one of ISO_PTD_OFFSET,
491 * INT_PTD_OFFSET, and ATL_PTD_OFFSET. 'slot' should be less than 32.
493 static void isp1760_ptd_read(struct usb_hcd *hcd, u32 ptd_offset, u32 slot,
496 u16 src_offset = ptd_offset + slot * sizeof(*ptd);
497 struct isp1760_hcd *priv = hcd_to_priv(hcd);
499 isp1760_reg_write(priv->regs, ISP176x_HC_MEMORY, src_offset);
502 bank_reads8(priv->base, src_offset, ISP_BANK_0, (void *)ptd,
506 static void isp1763_ptd_read(struct usb_hcd *hcd, u32 ptd_offset, u32 slot,
509 u16 src_offset = ptd_offset + slot * sizeof(*ptd);
510 struct ptd_le32 le32_ptd;
512 isp1763_mem_read(hcd, src_offset, (u16 *)&le32_ptd, sizeof(le32_ptd));
513 /* Normalize the data obtained */
514 ptd->dw0 = le32_to_dw(le32_ptd.dw0);
515 ptd->dw1 = le32_to_dw(le32_ptd.dw1);
516 ptd->dw2 = le32_to_dw(le32_ptd.dw2);
517 ptd->dw3 = le32_to_dw(le32_ptd.dw3);
518 ptd->dw4 = le32_to_dw(le32_ptd.dw4);
519 ptd->dw5 = le32_to_dw(le32_ptd.dw5);
520 ptd->dw6 = le32_to_dw(le32_ptd.dw6);
521 ptd->dw7 = le32_to_dw(le32_ptd.dw7);
524 static void ptd_read(struct usb_hcd *hcd, u32 ptd_offset, u32 slot,
527 struct isp1760_hcd *priv = hcd_to_priv(hcd);
529 if (!priv->is_isp1763)
530 return isp1760_ptd_read(hcd, ptd_offset, slot, ptd);
532 isp1763_ptd_read(hcd, ptd_offset, slot, ptd);
535 static void isp1763_ptd_write(struct usb_hcd *hcd, u32 ptd_offset, u32 slot,
538 u16 dst_offset = ptd_offset + slot * sizeof(*cpu_ptd);
541 ptd.dw0 = dw_to_le32(cpu_ptd->dw0);
542 ptd.dw1 = dw_to_le32(cpu_ptd->dw1);
543 ptd.dw2 = dw_to_le32(cpu_ptd->dw2);
544 ptd.dw3 = dw_to_le32(cpu_ptd->dw3);
545 ptd.dw4 = dw_to_le32(cpu_ptd->dw4);
546 ptd.dw5 = dw_to_le32(cpu_ptd->dw5);
547 ptd.dw6 = dw_to_le32(cpu_ptd->dw6);
548 ptd.dw7 = dw_to_le32(cpu_ptd->dw7);
550 isp1763_mem_write(hcd, dst_offset, (u16 *)&ptd.dw0,
551 8 * sizeof(ptd.dw0));
554 static void isp1760_ptd_write(void __iomem *base, u32 ptd_offset, u32 slot,
557 u32 dst_offset = ptd_offset + slot * sizeof(*ptd);
560 * Make sure dw0 gets written last (after other dw's and after payload)
561 * since it contains the enable bit
563 isp1760_mem_write(base, dst_offset + sizeof(ptd->dw0),
564 (__force u32 *)&ptd->dw1, 7 * sizeof(ptd->dw1));
566 isp1760_mem_write(base, dst_offset, (__force u32 *)&ptd->dw0,
570 static void ptd_write(struct usb_hcd *hcd, u32 ptd_offset, u32 slot,
573 struct isp1760_hcd *priv = hcd_to_priv(hcd);
575 if (!priv->is_isp1763)
576 return isp1760_ptd_write(priv->base, ptd_offset, slot, ptd);
578 isp1763_ptd_write(hcd, ptd_offset, slot, ptd);
581 /* memory management of the 60kb on the chip from 0x1000 to 0xffff */
582 static void init_memory(struct isp1760_hcd *priv)
584 const struct isp1760_memory_layout *mem = priv->memory_layout;
588 payload_addr = PAYLOAD_OFFSET;
590 for (i = 0, curr = 0; i < ARRAY_SIZE(mem->blocks); i++, curr += j) {
591 for (j = 0; j < mem->blocks[i]; j++) {
592 priv->memory_pool[curr + j].start = payload_addr;
593 priv->memory_pool[curr + j].size = mem->blocks_size[i];
594 priv->memory_pool[curr + j].free = 1;
595 payload_addr += priv->memory_pool[curr + j].size;
599 WARN_ON(payload_addr - priv->memory_pool[0].start >
600 mem->payload_area_size);
603 static void alloc_mem(struct usb_hcd *hcd, struct isp1760_qtd *qtd)
605 struct isp1760_hcd *priv = hcd_to_priv(hcd);
606 const struct isp1760_memory_layout *mem = priv->memory_layout;
609 WARN_ON(qtd->payload_addr);
614 for (i = 0; i < mem->payload_blocks; i++) {
615 if (priv->memory_pool[i].size >= qtd->length &&
616 priv->memory_pool[i].free) {
617 priv->memory_pool[i].free = 0;
618 qtd->payload_addr = priv->memory_pool[i].start;
624 static void free_mem(struct usb_hcd *hcd, struct isp1760_qtd *qtd)
626 struct isp1760_hcd *priv = hcd_to_priv(hcd);
627 const struct isp1760_memory_layout *mem = priv->memory_layout;
630 if (!qtd->payload_addr)
633 for (i = 0; i < mem->payload_blocks; i++) {
634 if (priv->memory_pool[i].start == qtd->payload_addr) {
635 WARN_ON(priv->memory_pool[i].free);
636 priv->memory_pool[i].free = 1;
637 qtd->payload_addr = 0;
642 dev_err(hcd->self.controller, "%s: Invalid pointer: %08x\n",
643 __func__, qtd->payload_addr);
645 qtd->payload_addr = 0;
648 /* reset a non-running (STS_HALT == 1) controller */
649 static int ehci_reset(struct usb_hcd *hcd)
651 struct isp1760_hcd *priv = hcd_to_priv(hcd);
653 hcd->state = HC_STATE_HALT;
654 priv->next_statechange = jiffies;
656 return isp1760_hcd_set_and_wait_swap(hcd, CMD_RESET, 250 * 1000);
659 static struct isp1760_qh *qh_alloc(gfp_t flags)
661 struct isp1760_qh *qh;
663 qh = kmem_cache_zalloc(qh_cachep, flags);
667 INIT_LIST_HEAD(&qh->qh_list);
668 INIT_LIST_HEAD(&qh->qtd_list);
674 static void qh_free(struct isp1760_qh *qh)
676 WARN_ON(!list_empty(&qh->qtd_list));
677 WARN_ON(qh->slot > -1);
678 kmem_cache_free(qh_cachep, qh);
681 /* one-time init, only for memory state */
682 static int priv_init(struct usb_hcd *hcd)
684 struct isp1760_hcd *priv = hcd_to_priv(hcd);
689 spin_lock_init(&priv->lock);
691 for (i = 0; i < QH_END; i++)
692 INIT_LIST_HEAD(&priv->qh_list[i]);
695 * hw default: 1K periodic list heads, one per frame.
696 * periodic_size can shrink by USBCMD update if hcc_params allows.
698 priv->periodic_size = DEFAULT_I_TDPS;
700 if (priv->is_isp1763) {
705 /* controllers may cache some of the periodic schedule ... */
706 isoc_cache = isp1760_hcd_read(hcd, HCC_ISOC_CACHE);
707 isoc_thres = isp1760_hcd_read(hcd, HCC_ISOC_THRES);
709 /* full frame cache */
712 else /* N microframes cached */
713 priv->i_thresh = 2 + isoc_thres;
718 static int isp1760_hc_setup(struct usb_hcd *hcd)
720 struct isp1760_hcd *priv = hcd_to_priv(hcd);
726 if (priv->is_isp1763)
729 pattern = 0xdeadcafe;
731 isp1760_hcd_write(hcd, HC_SCRATCH, pattern);
734 * we do not care about the read value here we just want to
735 * change bus pattern.
737 isp1760_hcd_read(hcd, HC_CHIP_ID_HIGH);
738 scratch = isp1760_hcd_read(hcd, HC_SCRATCH);
739 if (scratch != pattern) {
740 dev_err(hcd->self.controller, "Scratch test failed. 0x%08x\n",
746 * The RESET_HC bit in the SW_RESET register is supposed to reset the
747 * host controller without touching the CPU interface registers, but at
748 * least on the ISP1761 it seems to behave as the RESET_ALL bit and
749 * reset the whole device. We thus can't use it here, so let's reset
750 * the host controller through the EHCI USB Command register. The device
751 * has been reset in core code anyway, so this shouldn't matter.
753 isp1760_hcd_clear(hcd, ISO_BUF_FILL);
754 isp1760_hcd_clear(hcd, INT_BUF_FILL);
755 isp1760_hcd_clear(hcd, ATL_BUF_FILL);
757 isp1760_hcd_set(hcd, HC_ATL_PTD_SKIPMAP);
758 isp1760_hcd_set(hcd, HC_INT_PTD_SKIPMAP);
759 isp1760_hcd_set(hcd, HC_ISO_PTD_SKIPMAP);
761 result = ehci_reset(hcd);
768 if (priv->is_isp1763)
769 atx_reset = SW_RESET_RESET_ATX;
771 atx_reset = ALL_ATX_RESET;
773 isp1760_hcd_set(hcd, atx_reset);
775 isp1760_hcd_clear(hcd, atx_reset);
777 if (priv->is_isp1763) {
778 isp1760_hcd_set(hcd, HW_OTG_DISABLE);
779 isp1760_hcd_set(hcd, HW_SW_SEL_HC_DC_CLEAR);
780 isp1760_hcd_set(hcd, HW_HC_2_DIS_CLEAR);
783 isp1760_hcd_set(hcd, HW_INTF_LOCK);
786 isp1760_hcd_set(hcd, HC_INT_IRQ_ENABLE);
787 isp1760_hcd_set(hcd, HC_ATL_IRQ_ENABLE);
789 return priv_init(hcd);
792 static u32 base_to_chip(u32 base)
794 return ((base - 0x400) >> 3);
797 static int last_qtd_of_urb(struct isp1760_qtd *qtd, struct isp1760_qh *qh)
801 if (list_is_last(&qtd->qtd_list, &qh->qtd_list))
805 qtd = list_entry(qtd->qtd_list.next, typeof(*qtd), qtd_list);
806 return (qtd->urb != urb);
809 /* magic numbers that can affect system performance */
810 #define EHCI_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
811 #define EHCI_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
812 #define EHCI_TUNE_RL_TT 0
813 #define EHCI_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
814 #define EHCI_TUNE_MULT_TT 1
815 #define EHCI_TUNE_FLS 2 /* (small) 256 frame schedule */
817 static void create_ptd_atl(struct isp1760_qh *qh,
818 struct isp1760_qtd *qtd, struct ptd *ptd)
823 u32 nak = NAK_COUNTER;
825 memset(ptd, 0, sizeof(*ptd));
827 /* according to 3.6.2, max packet len can not be > 0x400 */
828 maxpacket = usb_maxpacket(qtd->urb->dev, qtd->urb->pipe);
829 multi = 1 + ((maxpacket >> 11) & 0x3);
833 ptd->dw0 = DW0_VALID_BIT;
834 ptd->dw0 |= TO_DW0_LENGTH(qtd->length);
835 ptd->dw0 |= TO_DW0_MAXPACKET(maxpacket);
836 ptd->dw0 |= TO_DW0_ENDPOINT(usb_pipeendpoint(qtd->urb->pipe));
839 ptd->dw1 = TO_DW((usb_pipeendpoint(qtd->urb->pipe) >> 1));
840 ptd->dw1 |= TO_DW1_DEVICE_ADDR(usb_pipedevice(qtd->urb->pipe));
841 ptd->dw1 |= TO_DW1_PID_TOKEN(qtd->packet_type);
843 if (usb_pipebulk(qtd->urb->pipe))
844 ptd->dw1 |= DW1_TRANS_BULK;
845 else if (usb_pipeint(qtd->urb->pipe))
846 ptd->dw1 |= DW1_TRANS_INT;
848 if (qtd->urb->dev->speed != USB_SPEED_HIGH) {
849 /* split transaction */
851 ptd->dw1 |= DW1_TRANS_SPLIT;
852 if (qtd->urb->dev->speed == USB_SPEED_LOW)
853 ptd->dw1 |= DW1_SE_USB_LOSPEED;
855 ptd->dw1 |= TO_DW1_PORT_NUM(qtd->urb->dev->ttport);
856 ptd->dw1 |= TO_DW1_HUB_NUM(qtd->urb->dev->tt->hub->devnum);
858 /* SE bit for Split INT transfers */
859 if (usb_pipeint(qtd->urb->pipe) &&
860 (qtd->urb->dev->speed == USB_SPEED_LOW))
861 ptd->dw1 |= DW1_SE_USB_LOSPEED;
866 ptd->dw0 |= TO_DW0_MULTI(multi);
867 if (usb_pipecontrol(qtd->urb->pipe) ||
868 usb_pipebulk(qtd->urb->pipe))
869 ptd->dw3 |= TO_DW3_PING(qh->ping);
873 ptd->dw2 |= TO_DW2_DATA_START_ADDR(base_to_chip(qtd->payload_addr));
874 ptd->dw2 |= TO_DW2_RL(rl);
877 ptd->dw3 |= TO_DW3_NAKCOUNT(nak);
878 ptd->dw3 |= TO_DW3_DATA_TOGGLE(qh->toggle);
879 if (usb_pipecontrol(qtd->urb->pipe)) {
880 if (qtd->data_buffer == qtd->urb->setup_packet)
881 ptd->dw3 &= ~TO_DW3_DATA_TOGGLE(1);
882 else if (last_qtd_of_urb(qtd, qh))
883 ptd->dw3 |= TO_DW3_DATA_TOGGLE(1);
886 ptd->dw3 |= DW3_ACTIVE_BIT;
888 ptd->dw3 |= TO_DW3_CERR(ERR_COUNTER);
891 static void transform_add_int(struct isp1760_qh *qh,
892 struct isp1760_qtd *qtd, struct ptd *ptd)
898 * Most of this is guessing. ISP1761 datasheet is quite unclear, and
899 * the algorithm from the original Philips driver code, which was
900 * pretty much used in this driver before as well, is quite horrendous
901 * and, i believe, incorrect. The code below follows the datasheet and
902 * USB2.0 spec as far as I can tell, and plug/unplug seems to be much
903 * more reliable this way (fingers crossed...).
906 if (qtd->urb->dev->speed == USB_SPEED_HIGH) {
907 /* urb->interval is in units of microframes (1/8 ms) */
908 period = qtd->urb->interval >> 3;
910 if (qtd->urb->interval > 4)
911 usof = 0x01; /* One bit set =>
912 interval 1 ms * uFrame-match */
913 else if (qtd->urb->interval > 2)
914 usof = 0x22; /* Two bits set => interval 1/2 ms */
915 else if (qtd->urb->interval > 1)
916 usof = 0x55; /* Four bits set => interval 1/4 ms */
918 usof = 0xff; /* All bits set => interval 1/8 ms */
920 /* urb->interval is in units of frames (1 ms) */
921 period = qtd->urb->interval;
922 usof = 0x0f; /* Execute Start Split on any of the
923 four first uFrames */
926 * First 8 bits in dw5 is uSCS and "specifies which uSOF the
927 * complete split needs to be sent. Valid only for IN." Also,
928 * "All bits can be set to one for every transfer." (p 82,
929 * ISP1761 data sheet.) 0x1c is from Philips driver. Where did
930 * that number come from? 0xff seems to work fine...
932 /* ptd->dw5 = 0x1c; */
933 ptd->dw5 = TO_DW(0xff); /* Execute Complete Split on any uFrame */
936 period = period >> 1;/* Ensure equal or shorter period than requested */
937 period &= 0xf8; /* Mask off too large values and lowest unused 3 bits */
939 ptd->dw2 |= TO_DW(period);
940 ptd->dw4 = TO_DW(usof);
943 static void create_ptd_int(struct isp1760_qh *qh,
944 struct isp1760_qtd *qtd, struct ptd *ptd)
946 create_ptd_atl(qh, qtd, ptd);
947 transform_add_int(qh, qtd, ptd);
950 static void isp1760_urb_done(struct usb_hcd *hcd, struct urb *urb)
951 __releases(priv->lock)
952 __acquires(priv->lock)
954 struct isp1760_hcd *priv = hcd_to_priv(hcd);
956 if (!urb->unlinked) {
957 if (urb->status == -EINPROGRESS)
961 if (usb_pipein(urb->pipe) && usb_pipetype(urb->pipe) != PIPE_CONTROL) {
963 for (ptr = urb->transfer_buffer;
964 ptr < urb->transfer_buffer + urb->transfer_buffer_length;
966 flush_dcache_page(virt_to_page(ptr));
969 /* complete() can reenter this HCD */
970 usb_hcd_unlink_urb_from_ep(hcd, urb);
971 spin_unlock(&priv->lock);
972 usb_hcd_giveback_urb(hcd, urb, urb->status);
973 spin_lock(&priv->lock);
976 static struct isp1760_qtd *qtd_alloc(gfp_t flags, struct urb *urb,
979 struct isp1760_qtd *qtd;
981 qtd = kmem_cache_zalloc(qtd_cachep, flags);
985 INIT_LIST_HEAD(&qtd->qtd_list);
987 qtd->packet_type = packet_type;
988 qtd->status = QTD_ENQUEUED;
989 qtd->actual_length = 0;
994 static void qtd_free(struct isp1760_qtd *qtd)
996 WARN_ON(qtd->payload_addr);
997 kmem_cache_free(qtd_cachep, qtd);
1000 static void start_bus_transfer(struct usb_hcd *hcd, u32 ptd_offset, int slot,
1001 struct isp1760_slotinfo *slots,
1002 struct isp1760_qtd *qtd, struct isp1760_qh *qh,
1005 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1006 const struct isp1760_memory_layout *mem = priv->memory_layout;
1009 WARN_ON((slot < 0) || (slot > mem->slot_num - 1));
1010 WARN_ON(qtd->length && !qtd->payload_addr);
1011 WARN_ON(slots[slot].qtd);
1012 WARN_ON(slots[slot].qh);
1013 WARN_ON(qtd->status != QTD_PAYLOAD_ALLOC);
1015 if (priv->is_isp1763)
1018 /* Make sure done map has not triggered from some unlinked transfer */
1019 if (ptd_offset == ATL_PTD_OFFSET) {
1020 skip_map = isp1760_hcd_read(hcd, HC_ATL_PTD_SKIPMAP);
1021 isp1760_hcd_write(hcd, HC_ATL_PTD_SKIPMAP,
1022 skip_map | (1 << slot));
1023 priv->atl_done_map |= isp1760_hcd_read(hcd, HC_ATL_PTD_DONEMAP);
1024 priv->atl_done_map &= ~(1 << slot);
1026 skip_map = isp1760_hcd_read(hcd, HC_INT_PTD_SKIPMAP);
1027 isp1760_hcd_write(hcd, HC_INT_PTD_SKIPMAP,
1028 skip_map | (1 << slot));
1029 priv->int_done_map |= isp1760_hcd_read(hcd, HC_INT_PTD_DONEMAP);
1030 priv->int_done_map &= ~(1 << slot);
1033 skip_map &= ~(1 << slot);
1035 qtd->status = QTD_XFER_STARTED;
1036 slots[slot].timestamp = jiffies;
1037 slots[slot].qtd = qtd;
1038 slots[slot].qh = qh;
1039 ptd_write(hcd, ptd_offset, slot, ptd);
1041 if (ptd_offset == ATL_PTD_OFFSET)
1042 isp1760_hcd_write(hcd, HC_ATL_PTD_SKIPMAP, skip_map);
1044 isp1760_hcd_write(hcd, HC_INT_PTD_SKIPMAP, skip_map);
1047 static int is_short_bulk(struct isp1760_qtd *qtd)
1049 return (usb_pipebulk(qtd->urb->pipe) &&
1050 (qtd->actual_length < qtd->length));
1053 static void collect_qtds(struct usb_hcd *hcd, struct isp1760_qh *qh,
1054 struct list_head *urb_list)
1056 struct isp1760_qtd *qtd, *qtd_next;
1057 struct urb_listitem *urb_listitem;
1060 list_for_each_entry_safe(qtd, qtd_next, &qh->qtd_list, qtd_list) {
1061 if (qtd->status < QTD_XFER_COMPLETE)
1064 last_qtd = last_qtd_of_urb(qtd, qh);
1066 if ((!last_qtd) && (qtd->status == QTD_RETIRE))
1067 qtd_next->status = QTD_RETIRE;
1069 if (qtd->status == QTD_XFER_COMPLETE) {
1070 if (qtd->actual_length) {
1071 switch (qtd->packet_type) {
1073 mem_read(hcd, qtd->payload_addr,
1075 qtd->actual_length);
1078 qtd->urb->actual_length +=
1086 if (is_short_bulk(qtd)) {
1087 if (qtd->urb->transfer_flags & URB_SHORT_NOT_OK)
1088 qtd->urb->status = -EREMOTEIO;
1090 qtd_next->status = QTD_RETIRE;
1094 if (qtd->payload_addr)
1098 if ((qtd->status == QTD_RETIRE) &&
1099 (qtd->urb->status == -EINPROGRESS))
1100 qtd->urb->status = -EPIPE;
1101 /* Defer calling of urb_done() since it releases lock */
1102 urb_listitem = kmem_cache_zalloc(urb_listitem_cachep,
1104 if (unlikely(!urb_listitem))
1105 break; /* Try again on next call */
1106 urb_listitem->urb = qtd->urb;
1107 list_add_tail(&urb_listitem->urb_list, urb_list);
1110 list_del(&qtd->qtd_list);
1115 #define ENQUEUE_DEPTH 2
1116 static void enqueue_qtds(struct usb_hcd *hcd, struct isp1760_qh *qh)
1118 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1119 const struct isp1760_memory_layout *mem = priv->memory_layout;
1120 int slot_num = mem->slot_num;
1122 struct isp1760_slotinfo *slots;
1123 int curr_slot, free_slot;
1126 struct isp1760_qtd *qtd;
1128 if (unlikely(list_empty(&qh->qtd_list))) {
1133 /* Make sure this endpoint's TT buffer is clean before queueing ptds */
1134 if (qh->tt_buffer_dirty)
1137 if (usb_pipeint(list_entry(qh->qtd_list.next, struct isp1760_qtd,
1138 qtd_list)->urb->pipe)) {
1139 ptd_offset = INT_PTD_OFFSET;
1140 slots = priv->int_slots;
1142 ptd_offset = ATL_PTD_OFFSET;
1143 slots = priv->atl_slots;
1147 for (curr_slot = 0; curr_slot < slot_num; curr_slot++) {
1148 if ((free_slot == -1) && (slots[curr_slot].qtd == NULL))
1149 free_slot = curr_slot;
1150 if (slots[curr_slot].qh == qh)
1155 list_for_each_entry(qtd, &qh->qtd_list, qtd_list) {
1156 if (qtd->status == QTD_ENQUEUED) {
1157 WARN_ON(qtd->payload_addr);
1158 alloc_mem(hcd, qtd);
1159 if ((qtd->length) && (!qtd->payload_addr))
1162 if (qtd->length && (qtd->packet_type == SETUP_PID ||
1163 qtd->packet_type == OUT_PID)) {
1164 mem_write(hcd, qtd->payload_addr,
1165 qtd->data_buffer, qtd->length);
1168 qtd->status = QTD_PAYLOAD_ALLOC;
1171 if (qtd->status == QTD_PAYLOAD_ALLOC) {
1173 if ((curr_slot > 31) && (free_slot == -1))
1174 dev_dbg(hcd->self.controller, "%s: No slot "
1175 "available for transfer\n", __func__);
1177 /* Start xfer for this endpoint if not already done */
1178 if ((curr_slot > slot_num - 1) && (free_slot > -1)) {
1179 if (usb_pipeint(qtd->urb->pipe))
1180 create_ptd_int(qh, qtd, &ptd);
1182 create_ptd_atl(qh, qtd, &ptd);
1184 start_bus_transfer(hcd, ptd_offset, free_slot,
1185 slots, qtd, qh, &ptd);
1186 curr_slot = free_slot;
1190 if (n >= ENQUEUE_DEPTH)
1196 static void schedule_ptds(struct usb_hcd *hcd)
1198 struct isp1760_hcd *priv;
1199 struct isp1760_qh *qh, *qh_next;
1200 struct list_head *ep_queue;
1201 LIST_HEAD(urb_list);
1202 struct urb_listitem *urb_listitem, *urb_listitem_next;
1210 priv = hcd_to_priv(hcd);
1213 * check finished/retired xfers, transfer payloads, call urb_done()
1215 for (i = 0; i < QH_END; i++) {
1216 ep_queue = &priv->qh_list[i];
1217 list_for_each_entry_safe(qh, qh_next, ep_queue, qh_list) {
1218 collect_qtds(hcd, qh, &urb_list);
1219 if (list_empty(&qh->qtd_list))
1220 list_del(&qh->qh_list);
1224 list_for_each_entry_safe(urb_listitem, urb_listitem_next, &urb_list,
1226 isp1760_urb_done(hcd, urb_listitem->urb);
1227 kmem_cache_free(urb_listitem_cachep, urb_listitem);
1231 * Schedule packets for transfer.
1233 * According to USB2.0 specification:
1235 * 1st prio: interrupt xfers, up to 80 % of bandwidth
1236 * 2nd prio: control xfers
1237 * 3rd prio: bulk xfers
1239 * ... but let's use a simpler scheme here (mostly because ISP1761 doc
1240 * is very unclear on how to prioritize traffic):
1242 * 1) Enqueue any queued control transfers, as long as payload chip mem
1243 * and PTD ATL slots are available.
1244 * 2) Enqueue any queued INT transfers, as long as payload chip mem
1245 * and PTD INT slots are available.
1246 * 3) Enqueue any queued bulk transfers, as long as payload chip mem
1247 * and PTD ATL slots are available.
1249 * Use double buffering (ENQUEUE_DEPTH==2) as a compromise between
1250 * conservation of chip mem and performance.
1252 * I'm sure this scheme could be improved upon!
1254 for (i = 0; i < QH_END; i++) {
1255 ep_queue = &priv->qh_list[i];
1256 list_for_each_entry_safe(qh, qh_next, ep_queue, qh_list)
1257 enqueue_qtds(hcd, qh);
1261 #define PTD_STATE_QTD_DONE 1
1262 #define PTD_STATE_QTD_RELOAD 2
1263 #define PTD_STATE_URB_RETIRE 3
1265 static int check_int_transfer(struct usb_hcd *hcd, struct ptd *ptd,
1271 dw4 = TO_U32(ptd->dw4);
1274 /* FIXME: ISP1761 datasheet does not say what to do with these. Do we
1275 need to handle these errors? Is it done in hardware? */
1277 if (ptd->dw3 & DW3_HALT_BIT) {
1279 urb->status = -EPROTO; /* Default unknown error */
1281 for (i = 0; i < 8; i++) {
1282 switch (dw4 & 0x7) {
1284 dev_dbg(hcd->self.controller, "%s: underrun "
1285 "during uFrame %d\n",
1287 urb->status = -ECOMM; /* Could not write data */
1290 dev_dbg(hcd->self.controller, "%s: transaction "
1291 "error during uFrame %d\n",
1293 urb->status = -EPROTO; /* timeout, bad CRC, PID
1297 dev_dbg(hcd->self.controller, "%s: babble "
1298 "error during uFrame %d\n",
1300 urb->status = -EOVERFLOW;
1306 return PTD_STATE_URB_RETIRE;
1309 return PTD_STATE_QTD_DONE;
1312 static int check_atl_transfer(struct usb_hcd *hcd, struct ptd *ptd,
1316 if (ptd->dw3 & DW3_HALT_BIT) {
1317 if (ptd->dw3 & DW3_BABBLE_BIT)
1318 urb->status = -EOVERFLOW;
1319 else if (FROM_DW3_CERR(ptd->dw3))
1320 urb->status = -EPIPE; /* Stall */
1322 urb->status = -EPROTO; /* Unknown */
1324 dev_dbg(hcd->self.controller, "%s: ptd error:\n"
1325 " dw0: %08x dw1: %08x dw2: %08x dw3: %08x\n"
1326 " dw4: %08x dw5: %08x dw6: %08x dw7: %08x\n",
1328 ptd->dw0, ptd->dw1, ptd->dw2, ptd->dw3,
1329 ptd->dw4, ptd->dw5, ptd->dw6, ptd->dw7);
1331 return PTD_STATE_URB_RETIRE;
1334 if ((ptd->dw3 & DW3_ERROR_BIT) && (ptd->dw3 & DW3_ACTIVE_BIT)) {
1335 /* Transfer Error, *but* active and no HALT -> reload */
1336 dev_dbg(hcd->self.controller, "PID error; reloading ptd\n");
1337 return PTD_STATE_QTD_RELOAD;
1340 if (!FROM_DW3_NAKCOUNT(ptd->dw3) && (ptd->dw3 & DW3_ACTIVE_BIT)) {
1342 * NAKs are handled in HW by the chip. Usually if the
1343 * device is not able to send data fast enough.
1344 * This happens mostly on slower hardware.
1346 return PTD_STATE_QTD_RELOAD;
1349 return PTD_STATE_QTD_DONE;
1352 static void handle_done_ptds(struct usb_hcd *hcd)
1354 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1356 struct isp1760_qh *qh;
1359 struct isp1760_slotinfo *slots;
1361 struct isp1760_qtd *qtd;
1365 skip_map = isp1760_hcd_read(hcd, HC_INT_PTD_SKIPMAP);
1366 priv->int_done_map &= ~skip_map;
1367 skip_map = isp1760_hcd_read(hcd, HC_ATL_PTD_SKIPMAP);
1368 priv->atl_done_map &= ~skip_map;
1370 modified = priv->int_done_map || priv->atl_done_map;
1372 while (priv->int_done_map || priv->atl_done_map) {
1373 if (priv->int_done_map) {
1375 slot = __ffs(priv->int_done_map);
1376 priv->int_done_map &= ~(1 << slot);
1377 slots = priv->int_slots;
1378 /* This should not trigger, and could be removed if
1379 noone have any problems with it triggering: */
1380 if (!slots[slot].qh) {
1384 ptd_offset = INT_PTD_OFFSET;
1385 ptd_read(hcd, INT_PTD_OFFSET, slot, &ptd);
1386 state = check_int_transfer(hcd, &ptd,
1387 slots[slot].qtd->urb);
1390 slot = __ffs(priv->atl_done_map);
1391 priv->atl_done_map &= ~(1 << slot);
1392 slots = priv->atl_slots;
1393 /* This should not trigger, and could be removed if
1394 noone have any problems with it triggering: */
1395 if (!slots[slot].qh) {
1399 ptd_offset = ATL_PTD_OFFSET;
1400 ptd_read(hcd, ATL_PTD_OFFSET, slot, &ptd);
1401 state = check_atl_transfer(hcd, &ptd,
1402 slots[slot].qtd->urb);
1405 qtd = slots[slot].qtd;
1406 slots[slot].qtd = NULL;
1407 qh = slots[slot].qh;
1408 slots[slot].qh = NULL;
1411 WARN_ON(qtd->status != QTD_XFER_STARTED);
1414 case PTD_STATE_QTD_DONE:
1415 if ((usb_pipeint(qtd->urb->pipe)) &&
1416 (qtd->urb->dev->speed != USB_SPEED_HIGH))
1417 qtd->actual_length =
1418 FROM_DW3_SCS_NRBYTESTRANSFERRED(ptd.dw3);
1420 qtd->actual_length =
1421 FROM_DW3_NRBYTESTRANSFERRED(ptd.dw3);
1423 qtd->status = QTD_XFER_COMPLETE;
1424 if (list_is_last(&qtd->qtd_list, &qh->qtd_list) ||
1428 qtd = list_entry(qtd->qtd_list.next,
1429 typeof(*qtd), qtd_list);
1431 qh->toggle = FROM_DW3_DATA_TOGGLE(ptd.dw3);
1432 qh->ping = FROM_DW3_PING(ptd.dw3);
1435 case PTD_STATE_QTD_RELOAD: /* QTD_RETRY, for atls only */
1436 qtd->status = QTD_PAYLOAD_ALLOC;
1437 ptd.dw0 |= DW0_VALID_BIT;
1438 /* RL counter = ERR counter */
1439 ptd.dw3 &= ~TO_DW3_NAKCOUNT(0xf);
1440 ptd.dw3 |= TO_DW3_NAKCOUNT(FROM_DW2_RL(ptd.dw2));
1441 ptd.dw3 &= ~TO_DW3_CERR(3);
1442 ptd.dw3 |= TO_DW3_CERR(ERR_COUNTER);
1443 qh->toggle = FROM_DW3_DATA_TOGGLE(ptd.dw3);
1444 qh->ping = FROM_DW3_PING(ptd.dw3);
1447 case PTD_STATE_URB_RETIRE:
1448 qtd->status = QTD_RETIRE;
1449 if ((qtd->urb->dev->speed != USB_SPEED_HIGH) &&
1450 (qtd->urb->status != -EPIPE) &&
1451 (qtd->urb->status != -EREMOTEIO)) {
1452 qh->tt_buffer_dirty = 1;
1453 if (usb_hub_clear_tt_buffer(qtd->urb))
1454 /* Clear failed; let's hope things work
1456 qh->tt_buffer_dirty = 0;
1468 if (qtd && (qtd->status == QTD_PAYLOAD_ALLOC)) {
1469 if (slots == priv->int_slots) {
1470 if (state == PTD_STATE_QTD_RELOAD)
1471 dev_err(hcd->self.controller,
1472 "%s: PTD_STATE_QTD_RELOAD on "
1473 "interrupt packet\n", __func__);
1474 if (state != PTD_STATE_QTD_RELOAD)
1475 create_ptd_int(qh, qtd, &ptd);
1477 if (state != PTD_STATE_QTD_RELOAD)
1478 create_ptd_atl(qh, qtd, &ptd);
1481 start_bus_transfer(hcd, ptd_offset, slot, slots, qtd,
1490 static irqreturn_t isp1760_irq(struct usb_hcd *hcd)
1492 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1493 irqreturn_t irqret = IRQ_NONE;
1497 spin_lock(&priv->lock);
1499 if (!(hcd->state & HC_STATE_RUNNING))
1502 imask = isp1760_hcd_read(hcd, HC_INTERRUPT);
1503 if (unlikely(!imask))
1506 int_reg = priv->is_isp1763 ? ISP1763_HC_INTERRUPT :
1507 ISP176x_HC_INTERRUPT;
1508 isp1760_reg_write(priv->regs, int_reg, imask);
1510 priv->int_done_map |= isp1760_hcd_read(hcd, HC_INT_PTD_DONEMAP);
1511 priv->atl_done_map |= isp1760_hcd_read(hcd, HC_ATL_PTD_DONEMAP);
1513 handle_done_ptds(hcd);
1515 irqret = IRQ_HANDLED;
1518 spin_unlock(&priv->lock);
1524 * Workaround for problem described in chip errata 2:
1526 * Sometimes interrupts are not generated when ATL (not INT?) completion occurs.
1527 * One solution suggested in the errata is to use SOF interrupts _instead_of_
1528 * ATL done interrupts (the "instead of" might be important since it seems
1529 * enabling ATL interrupts also causes the chip to sometimes - rarely - "forget"
1530 * to set the PTD's done bit in addition to not generating an interrupt!).
1532 * So if we use SOF + ATL interrupts, we sometimes get stale PTDs since their
1533 * done bit is not being set. This is bad - it blocks the endpoint until reboot.
1535 * If we use SOF interrupts only, we get latency between ptd completion and the
1536 * actual handling. This is very noticeable in testusb runs which takes several
1537 * minutes longer without ATL interrupts.
1539 * A better solution is to run the code below every SLOT_CHECK_PERIOD ms. If it
1540 * finds active ATL slots which are older than SLOT_TIMEOUT ms, it checks the
1541 * slot's ACTIVE and VALID bits. If these are not set, the ptd is considered
1542 * completed and its done map bit is set.
1544 * The values of SLOT_TIMEOUT and SLOT_CHECK_PERIOD have been arbitrarily chosen
1545 * not to cause too much lag when this HW bug occurs, while still hopefully
1546 * ensuring that the check does not falsely trigger.
1548 #define SLOT_TIMEOUT 300
1549 #define SLOT_CHECK_PERIOD 200
1550 static struct timer_list errata2_timer;
1551 static struct usb_hcd *errata2_timer_hcd;
1553 static void errata2_function(struct timer_list *unused)
1555 struct usb_hcd *hcd = errata2_timer_hcd;
1556 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1557 const struct isp1760_memory_layout *mem = priv->memory_layout;
1560 unsigned long spinflags;
1562 spin_lock_irqsave(&priv->lock, spinflags);
1564 for (slot = 0; slot < mem->slot_num; slot++)
1565 if (priv->atl_slots[slot].qh && time_after(jiffies,
1566 priv->atl_slots[slot].timestamp +
1567 msecs_to_jiffies(SLOT_TIMEOUT))) {
1568 ptd_read(hcd, ATL_PTD_OFFSET, slot, &ptd);
1569 if (!FROM_DW0_VALID(ptd.dw0) &&
1570 !FROM_DW3_ACTIVE(ptd.dw3))
1571 priv->atl_done_map |= 1 << slot;
1574 if (priv->atl_done_map)
1575 handle_done_ptds(hcd);
1577 spin_unlock_irqrestore(&priv->lock, spinflags);
1579 errata2_timer.expires = jiffies + msecs_to_jiffies(SLOT_CHECK_PERIOD);
1580 add_timer(&errata2_timer);
1583 static int isp1763_run(struct usb_hcd *hcd)
1585 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1593 hcd->uses_new_polling = 1;
1594 hcd->state = HC_STATE_RUNNING;
1596 chipid_h = isp1760_hcd_read(hcd, HC_CHIP_ID_HIGH);
1597 chipid_l = isp1760_hcd_read(hcd, HC_CHIP_ID_LOW);
1598 chip_rev = isp1760_hcd_read(hcd, HC_CHIP_REV);
1599 dev_info(hcd->self.controller, "USB ISP %02x%02x HW rev. %d started\n",
1600 chipid_h, chipid_l, chip_rev);
1602 isp1760_hcd_clear(hcd, ISO_BUF_FILL);
1603 isp1760_hcd_clear(hcd, INT_BUF_FILL);
1604 isp1760_hcd_clear(hcd, ATL_BUF_FILL);
1606 isp1760_hcd_set(hcd, HC_ATL_PTD_SKIPMAP);
1607 isp1760_hcd_set(hcd, HC_INT_PTD_SKIPMAP);
1608 isp1760_hcd_set(hcd, HC_ISO_PTD_SKIPMAP);
1610 isp1760_hcd_clear(hcd, HC_ATL_PTD_DONEMAP);
1611 isp1760_hcd_clear(hcd, HC_INT_PTD_DONEMAP);
1612 isp1760_hcd_clear(hcd, HC_ISO_PTD_DONEMAP);
1614 isp1760_hcd_set(hcd, HW_OTG_DISABLE);
1615 isp1760_reg_write(priv->regs, ISP1763_HC_OTG_CTRL_CLEAR, BIT(7));
1616 isp1760_reg_write(priv->regs, ISP1763_HC_OTG_CTRL_CLEAR, BIT(15));
1619 isp1760_hcd_set(hcd, HC_INT_IRQ_ENABLE);
1620 isp1760_hcd_set(hcd, HC_ATL_IRQ_ENABLE);
1622 isp1760_hcd_set(hcd, HW_GLOBAL_INTR_EN);
1624 isp1760_hcd_clear(hcd, HC_ATL_IRQ_MASK_AND);
1625 isp1760_hcd_clear(hcd, HC_INT_IRQ_MASK_AND);
1626 isp1760_hcd_clear(hcd, HC_ISO_IRQ_MASK_AND);
1628 isp1760_hcd_set(hcd, HC_ATL_IRQ_MASK_OR);
1629 isp1760_hcd_set(hcd, HC_INT_IRQ_MASK_OR);
1630 isp1760_hcd_set(hcd, HC_ISO_IRQ_MASK_OR);
1632 ptd_atl_int = 0x8000;
1635 isp1760_hcd_write(hcd, HC_ATL_PTD_LASTPTD, ptd_atl_int);
1636 isp1760_hcd_write(hcd, HC_INT_PTD_LASTPTD, ptd_atl_int);
1637 isp1760_hcd_write(hcd, HC_ISO_PTD_LASTPTD, ptd_iso);
1639 isp1760_hcd_set(hcd, ATL_BUF_FILL);
1640 isp1760_hcd_set(hcd, INT_BUF_FILL);
1642 isp1760_hcd_clear(hcd, CMD_LRESET);
1643 isp1760_hcd_clear(hcd, CMD_RESET);
1645 retval = isp1760_hcd_set_and_wait(hcd, CMD_RUN, 250 * 1000);
1649 down_write(&ehci_cf_port_reset_rwsem);
1650 retval = isp1760_hcd_set_and_wait(hcd, FLAG_CF, 250 * 1000);
1651 up_write(&ehci_cf_port_reset_rwsem);
1658 static int isp1760_run(struct usb_hcd *hcd)
1660 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1669 * ISP1763 have some differences in the setup and order to enable
1670 * the ports, disable otg, setup buffers, and ATL, INT, ISO status.
1671 * So, just handle it a separate sequence.
1673 if (priv->is_isp1763)
1674 return isp1763_run(hcd);
1676 hcd->uses_new_polling = 1;
1678 hcd->state = HC_STATE_RUNNING;
1680 /* Set PTD interrupt AND & OR maps */
1681 isp1760_hcd_clear(hcd, HC_ATL_IRQ_MASK_AND);
1682 isp1760_hcd_clear(hcd, HC_INT_IRQ_MASK_AND);
1683 isp1760_hcd_clear(hcd, HC_ISO_IRQ_MASK_AND);
1685 isp1760_hcd_set(hcd, HC_ATL_IRQ_MASK_OR);
1686 isp1760_hcd_set(hcd, HC_INT_IRQ_MASK_OR);
1687 isp1760_hcd_set(hcd, HC_ISO_IRQ_MASK_OR);
1689 /* step 23 passed */
1691 isp1760_hcd_set(hcd, HW_GLOBAL_INTR_EN);
1693 isp1760_hcd_clear(hcd, CMD_LRESET);
1694 isp1760_hcd_clear(hcd, CMD_RESET);
1696 retval = isp1760_hcd_set_and_wait(hcd, CMD_RUN, 250 * 1000);
1702 * Spec says to write FLAG_CF as last config action, priv code grabs
1703 * the semaphore while doing so.
1705 down_write(&ehci_cf_port_reset_rwsem);
1707 retval = isp1760_hcd_set_and_wait(hcd, FLAG_CF, 250 * 1000);
1708 up_write(&ehci_cf_port_reset_rwsem);
1712 errata2_timer_hcd = hcd;
1713 timer_setup(&errata2_timer, errata2_function, 0);
1714 errata2_timer.expires = jiffies + msecs_to_jiffies(SLOT_CHECK_PERIOD);
1715 add_timer(&errata2_timer);
1717 chipid_h = isp1760_hcd_read(hcd, HC_CHIP_ID_HIGH);
1718 chipid_l = isp1760_hcd_read(hcd, HC_CHIP_ID_LOW);
1719 chip_rev = isp1760_hcd_read(hcd, HC_CHIP_REV);
1720 dev_info(hcd->self.controller, "USB ISP %02x%02x HW rev. %d started\n",
1721 chipid_h, chipid_l, chip_rev);
1723 /* PTD Register Init Part 2, Step 28 */
1725 /* Setup registers controlling PTD checking */
1726 ptd_atl_int = 0x80000000;
1727 ptd_iso = 0x00000001;
1729 isp1760_hcd_write(hcd, HC_ATL_PTD_LASTPTD, ptd_atl_int);
1730 isp1760_hcd_write(hcd, HC_INT_PTD_LASTPTD, ptd_atl_int);
1731 isp1760_hcd_write(hcd, HC_ISO_PTD_LASTPTD, ptd_iso);
1733 isp1760_hcd_set(hcd, HC_ATL_PTD_SKIPMAP);
1734 isp1760_hcd_set(hcd, HC_INT_PTD_SKIPMAP);
1735 isp1760_hcd_set(hcd, HC_ISO_PTD_SKIPMAP);
1737 isp1760_hcd_set(hcd, ATL_BUF_FILL);
1738 isp1760_hcd_set(hcd, INT_BUF_FILL);
1740 /* GRR this is run-once init(), being done every time the HC starts.
1741 * So long as they're part of class devices, we can't do it init()
1742 * since the class device isn't created that early.
1747 static int qtd_fill(struct isp1760_qtd *qtd, void *databuffer, size_t len)
1749 qtd->data_buffer = databuffer;
1756 static void qtd_list_free(struct list_head *qtd_list)
1758 struct isp1760_qtd *qtd, *qtd_next;
1760 list_for_each_entry_safe(qtd, qtd_next, qtd_list, qtd_list) {
1761 list_del(&qtd->qtd_list);
1767 * Packetize urb->transfer_buffer into list of packets of size wMaxPacketSize.
1768 * Also calculate the PID type (SETUP/IN/OUT) for each packet.
1770 static void packetize_urb(struct usb_hcd *hcd,
1771 struct urb *urb, struct list_head *head, gfp_t flags)
1773 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1774 const struct isp1760_memory_layout *mem = priv->memory_layout;
1775 struct isp1760_qtd *qtd;
1777 int len, maxpacketsize;
1781 * URBs map to sequences of QTDs: one logical transaction
1784 if (!urb->transfer_buffer && urb->transfer_buffer_length) {
1785 /* XXX This looks like usb storage / SCSI bug */
1786 dev_err(hcd->self.controller,
1787 "buf is null, dma is %08lx len is %d\n",
1788 (long unsigned)urb->transfer_dma,
1789 urb->transfer_buffer_length);
1793 if (usb_pipein(urb->pipe))
1794 packet_type = IN_PID;
1796 packet_type = OUT_PID;
1798 if (usb_pipecontrol(urb->pipe)) {
1799 qtd = qtd_alloc(flags, urb, SETUP_PID);
1802 qtd_fill(qtd, urb->setup_packet, sizeof(struct usb_ctrlrequest));
1803 list_add_tail(&qtd->qtd_list, head);
1805 /* for zero length DATA stages, STATUS is always IN */
1806 if (urb->transfer_buffer_length == 0)
1807 packet_type = IN_PID;
1810 maxpacketsize = usb_maxpacket(urb->dev, urb->pipe);
1813 * buffer gets wrapped in one or more qtds;
1814 * last one may be "short" (including zero len)
1815 * and may serve as a control status ack
1817 buf = urb->transfer_buffer;
1818 len = urb->transfer_buffer_length;
1823 qtd = qtd_alloc(flags, urb, packet_type);
1827 if (len > mem->blocks_size[ISP176x_BLOCK_NUM - 1])
1828 this_qtd_len = mem->blocks_size[ISP176x_BLOCK_NUM - 1];
1832 this_qtd_len = qtd_fill(qtd, buf, this_qtd_len);
1833 list_add_tail(&qtd->qtd_list, head);
1835 len -= this_qtd_len;
1836 buf += this_qtd_len;
1843 * control requests may need a terminating data "status" ack;
1844 * bulk ones may need a terminating short packet (zero length).
1846 if (urb->transfer_buffer_length != 0) {
1849 if (usb_pipecontrol(urb->pipe)) {
1851 if (packet_type == IN_PID)
1852 packet_type = OUT_PID;
1854 packet_type = IN_PID;
1855 } else if (usb_pipebulk(urb->pipe) && maxpacketsize
1856 && (urb->transfer_flags & URB_ZERO_PACKET)
1857 && !(urb->transfer_buffer_length %
1862 qtd = qtd_alloc(flags, urb, packet_type);
1866 /* never any data in such packets */
1867 qtd_fill(qtd, NULL, 0);
1868 list_add_tail(&qtd->qtd_list, head);
1875 qtd_list_free(head);
1878 static int isp1760_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
1881 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1882 struct list_head *ep_queue;
1883 struct isp1760_qh *qh, *qhit;
1884 unsigned long spinflags;
1885 LIST_HEAD(new_qtds);
1889 switch (usb_pipetype(urb->pipe)) {
1891 ep_queue = &priv->qh_list[QH_CONTROL];
1894 ep_queue = &priv->qh_list[QH_BULK];
1896 case PIPE_INTERRUPT:
1897 if (urb->interval < 0)
1899 /* FIXME: Check bandwidth */
1900 ep_queue = &priv->qh_list[QH_INTERRUPT];
1902 case PIPE_ISOCHRONOUS:
1903 dev_err(hcd->self.controller, "%s: isochronous USB packets "
1904 "not yet supported\n",
1908 dev_err(hcd->self.controller, "%s: unknown pipe type\n",
1913 if (usb_pipein(urb->pipe))
1914 urb->actual_length = 0;
1916 packetize_urb(hcd, urb, &new_qtds, mem_flags);
1917 if (list_empty(&new_qtds))
1920 spin_lock_irqsave(&priv->lock, spinflags);
1922 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
1923 retval = -ESHUTDOWN;
1924 qtd_list_free(&new_qtds);
1927 retval = usb_hcd_link_urb_to_ep(hcd, urb);
1929 qtd_list_free(&new_qtds);
1933 qh = urb->ep->hcpriv;
1936 list_for_each_entry(qhit, ep_queue, qh_list) {
1943 list_add_tail(&qh->qh_list, ep_queue);
1945 qh = qh_alloc(GFP_ATOMIC);
1948 usb_hcd_unlink_urb_from_ep(hcd, urb);
1949 qtd_list_free(&new_qtds);
1952 list_add_tail(&qh->qh_list, ep_queue);
1953 urb->ep->hcpriv = qh;
1956 list_splice_tail(&new_qtds, &qh->qtd_list);
1960 spin_unlock_irqrestore(&priv->lock, spinflags);
1964 static void kill_transfer(struct usb_hcd *hcd, struct urb *urb,
1965 struct isp1760_qh *qh)
1967 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1970 WARN_ON(qh->slot == -1);
1972 /* We need to forcefully reclaim the slot since some transfers never
1973 return, e.g. interrupt transfers and NAKed bulk transfers. */
1974 if (usb_pipecontrol(urb->pipe) || usb_pipebulk(urb->pipe)) {
1975 if (qh->slot != -1) {
1976 skip_map = isp1760_hcd_read(hcd, HC_ATL_PTD_SKIPMAP);
1977 skip_map |= (1 << qh->slot);
1978 isp1760_hcd_write(hcd, HC_ATL_PTD_SKIPMAP, skip_map);
1981 priv->atl_slots[qh->slot].qh = NULL;
1982 priv->atl_slots[qh->slot].qtd = NULL;
1984 if (qh->slot != -1) {
1985 skip_map = isp1760_hcd_read(hcd, HC_INT_PTD_SKIPMAP);
1986 skip_map |= (1 << qh->slot);
1987 isp1760_hcd_write(hcd, HC_INT_PTD_SKIPMAP, skip_map);
1989 priv->int_slots[qh->slot].qh = NULL;
1990 priv->int_slots[qh->slot].qtd = NULL;
1997 * Retire the qtds beginning at 'qtd' and belonging all to the same urb, killing
1998 * any active transfer belonging to the urb in the process.
2000 static void dequeue_urb_from_qtd(struct usb_hcd *hcd, struct isp1760_qh *qh,
2001 struct isp1760_qtd *qtd)
2004 int urb_was_running;
2007 urb_was_running = 0;
2008 list_for_each_entry_from(qtd, &qh->qtd_list, qtd_list) {
2009 if (qtd->urb != urb)
2012 if (qtd->status >= QTD_XFER_STARTED)
2013 urb_was_running = 1;
2014 if (last_qtd_of_urb(qtd, qh) &&
2015 (qtd->status >= QTD_XFER_COMPLETE))
2016 urb_was_running = 0;
2018 if (qtd->status == QTD_XFER_STARTED)
2019 kill_transfer(hcd, urb, qh);
2020 qtd->status = QTD_RETIRE;
2023 if ((urb->dev->speed != USB_SPEED_HIGH) && urb_was_running) {
2024 qh->tt_buffer_dirty = 1;
2025 if (usb_hub_clear_tt_buffer(urb))
2026 /* Clear failed; let's hope things work anyway */
2027 qh->tt_buffer_dirty = 0;
2031 static int isp1760_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
2034 struct isp1760_hcd *priv = hcd_to_priv(hcd);
2035 unsigned long spinflags;
2036 struct isp1760_qh *qh;
2037 struct isp1760_qtd *qtd;
2040 spin_lock_irqsave(&priv->lock, spinflags);
2041 retval = usb_hcd_check_unlink_urb(hcd, urb, status);
2045 qh = urb->ep->hcpriv;
2051 list_for_each_entry(qtd, &qh->qtd_list, qtd_list)
2052 if (qtd->urb == urb) {
2053 dequeue_urb_from_qtd(hcd, qh, qtd);
2054 list_move(&qtd->qtd_list, &qh->qtd_list);
2058 urb->status = status;
2062 spin_unlock_irqrestore(&priv->lock, spinflags);
2066 static void isp1760_endpoint_disable(struct usb_hcd *hcd,
2067 struct usb_host_endpoint *ep)
2069 struct isp1760_hcd *priv = hcd_to_priv(hcd);
2070 unsigned long spinflags;
2071 struct isp1760_qh *qh, *qh_iter;
2074 spin_lock_irqsave(&priv->lock, spinflags);
2080 WARN_ON(!list_empty(&qh->qtd_list));
2082 for (i = 0; i < QH_END; i++)
2083 list_for_each_entry(qh_iter, &priv->qh_list[i], qh_list)
2084 if (qh_iter == qh) {
2085 list_del(&qh_iter->qh_list);
2095 spin_unlock_irqrestore(&priv->lock, spinflags);
2098 static int isp1760_hub_status_data(struct usb_hcd *hcd, char *buf)
2100 struct isp1760_hcd *priv = hcd_to_priv(hcd);
2103 unsigned long flags;
2105 /* if !PM, root hub timers won't get shut down ... */
2106 if (!HC_IS_RUNNING(hcd->state))
2109 /* init status to no-changes */
2112 spin_lock_irqsave(&priv->lock, flags);
2114 if (isp1760_hcd_is_set(hcd, PORT_OWNER) &&
2115 isp1760_hcd_is_set(hcd, PORT_CSC)) {
2116 isp1760_hcd_clear(hcd, PORT_CSC);
2121 * Return status information even for ports with OWNER set.
2122 * Otherwise hub_wq wouldn't see the disconnect event when a
2123 * high-speed device is switched over to the companion
2124 * controller by the user.
2126 if (isp1760_hcd_is_set(hcd, PORT_CSC) ||
2127 (isp1760_hcd_is_set(hcd, PORT_RESUME) &&
2128 time_after_eq(jiffies, priv->reset_done))) {
2129 buf [0] |= 1 << (0 + 1);
2132 /* FIXME autosuspend idle root hubs */
2134 spin_unlock_irqrestore(&priv->lock, flags);
2135 return status ? retval : 0;
2138 static void isp1760_hub_descriptor(struct isp1760_hcd *priv,
2139 struct usb_hub_descriptor *desc)
2144 ports = isp1760_hcd_n_ports(priv->hcd);
2146 desc->bDescriptorType = USB_DT_HUB;
2147 /* priv 1.0, 2.3.9 says 20ms max */
2148 desc->bPwrOn2PwrGood = 10;
2149 desc->bHubContrCurrent = 0;
2151 desc->bNbrPorts = ports;
2152 temp = 1 + (ports / 8);
2153 desc->bDescLength = 7 + 2 * temp;
2155 /* ports removable, and usb 1.0 legacy PortPwrCtrlMask */
2156 memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
2157 memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
2159 /* per-port overcurrent reporting */
2160 temp = HUB_CHAR_INDV_PORT_OCPM;
2161 if (isp1760_hcd_ppc_is_set(priv->hcd))
2162 /* per-port power control */
2163 temp |= HUB_CHAR_INDV_PORT_LPSM;
2165 /* no power switching */
2166 temp |= HUB_CHAR_NO_LPSM;
2167 desc->wHubCharacteristics = cpu_to_le16(temp);
2170 #define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
2172 static void check_reset_complete(struct usb_hcd *hcd, int index)
2174 if (!(isp1760_hcd_is_set(hcd, PORT_CONNECT)))
2177 /* if reset finished and it's still not enabled -- handoff */
2178 if (!isp1760_hcd_is_set(hcd, PORT_PE)) {
2179 dev_info(hcd->self.controller,
2180 "port %d full speed --> companion\n", index + 1);
2182 isp1760_hcd_set(hcd, PORT_OWNER);
2184 isp1760_hcd_clear(hcd, PORT_CSC);
2186 dev_info(hcd->self.controller, "port %d high speed\n",
2193 static int isp1760_hub_control(struct usb_hcd *hcd, u16 typeReq,
2194 u16 wValue, u16 wIndex, char *buf, u16 wLength)
2196 struct isp1760_hcd *priv = hcd_to_priv(hcd);
2198 unsigned long flags;
2202 ports = isp1760_hcd_n_ports(hcd);
2205 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
2206 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
2207 * (track current state ourselves) ... blink for diagnostics,
2208 * power, "this is the one", etc. EHCI spec supports this.
2211 spin_lock_irqsave(&priv->lock, flags);
2213 case ClearHubFeature:
2215 case C_HUB_LOCAL_POWER:
2216 case C_HUB_OVER_CURRENT:
2217 /* no hub-wide feature/status flags */
2223 case ClearPortFeature:
2224 if (!wIndex || wIndex > ports)
2229 * Even if OWNER is set, so the port is owned by the
2230 * companion controller, hub_wq needs to be able to clear
2231 * the port-change status bits (especially
2232 * USB_PORT_STAT_C_CONNECTION).
2236 case USB_PORT_FEAT_ENABLE:
2237 isp1760_hcd_clear(hcd, PORT_PE);
2239 case USB_PORT_FEAT_C_ENABLE:
2242 case USB_PORT_FEAT_SUSPEND:
2243 if (isp1760_hcd_is_set(hcd, PORT_RESET))
2246 if (isp1760_hcd_is_set(hcd, PORT_SUSPEND)) {
2247 if (!isp1760_hcd_is_set(hcd, PORT_PE))
2249 /* resume signaling for 20 msec */
2250 isp1760_hcd_clear(hcd, PORT_CSC);
2251 isp1760_hcd_set(hcd, PORT_RESUME);
2253 priv->reset_done = jiffies +
2254 msecs_to_jiffies(USB_RESUME_TIMEOUT);
2257 case USB_PORT_FEAT_C_SUSPEND:
2258 /* we auto-clear this feature */
2260 case USB_PORT_FEAT_POWER:
2261 if (isp1760_hcd_ppc_is_set(hcd))
2262 isp1760_hcd_clear(hcd, PORT_POWER);
2264 case USB_PORT_FEAT_C_CONNECTION:
2265 isp1760_hcd_set(hcd, PORT_CSC);
2267 case USB_PORT_FEAT_C_OVER_CURRENT:
2270 case USB_PORT_FEAT_C_RESET:
2271 /* GetPortStatus clears reset */
2276 isp1760_hcd_read(hcd, CMD_RUN);
2278 case GetHubDescriptor:
2279 isp1760_hub_descriptor(priv, (struct usb_hub_descriptor *)
2283 /* no hub-wide feature/status flags */
2287 if (!wIndex || wIndex > ports)
2292 /* wPortChange bits */
2293 if (isp1760_hcd_is_set(hcd, PORT_CSC))
2294 status |= USB_PORT_STAT_C_CONNECTION << 16;
2296 /* whoever resumes must GetPortStatus to complete it!! */
2297 if (isp1760_hcd_is_set(hcd, PORT_RESUME)) {
2298 dev_err(hcd->self.controller, "Port resume should be skipped.\n");
2300 /* Remote Wakeup received? */
2301 if (!priv->reset_done) {
2302 /* resume signaling for 20 msec */
2303 priv->reset_done = jiffies
2304 + msecs_to_jiffies(20);
2305 /* check the port again */
2306 mod_timer(&hcd->rh_timer, priv->reset_done);
2309 /* resume completed? */
2310 else if (time_after_eq(jiffies,
2311 priv->reset_done)) {
2312 status |= USB_PORT_STAT_C_SUSPEND << 16;
2313 priv->reset_done = 0;
2315 /* stop resume signaling */
2316 isp1760_hcd_clear(hcd, PORT_CSC);
2318 retval = isp1760_hcd_clear_and_wait(hcd,
2321 dev_err(hcd->self.controller,
2322 "port %d resume error %d\n",
2323 wIndex + 1, retval);
2329 /* whoever resets must GetPortStatus to complete it!! */
2330 if (isp1760_hcd_is_set(hcd, PORT_RESET) &&
2331 time_after_eq(jiffies, priv->reset_done)) {
2332 status |= USB_PORT_STAT_C_RESET << 16;
2333 priv->reset_done = 0;
2335 /* force reset to complete */
2336 /* REVISIT: some hardware needs 550+ usec to clear
2337 * this bit; seems too long to spin routinely...
2339 retval = isp1760_hcd_clear_and_wait(hcd, PORT_RESET,
2342 dev_err(hcd->self.controller, "port %d reset error %d\n",
2343 wIndex + 1, retval);
2347 /* see what we found out */
2348 check_reset_complete(hcd, wIndex);
2351 * Even if OWNER is set, there's no harm letting hub_wq
2352 * see the wPortStatus values (they should all be 0 except
2353 * for PORT_POWER anyway).
2356 if (isp1760_hcd_is_set(hcd, PORT_OWNER))
2357 dev_err(hcd->self.controller, "PORT_OWNER is set\n");
2359 if (isp1760_hcd_is_set(hcd, PORT_CONNECT)) {
2360 status |= USB_PORT_STAT_CONNECTION;
2361 /* status may be from integrated TT */
2362 status |= USB_PORT_STAT_HIGH_SPEED;
2364 if (isp1760_hcd_is_set(hcd, PORT_PE))
2365 status |= USB_PORT_STAT_ENABLE;
2366 if (isp1760_hcd_is_set(hcd, PORT_SUSPEND) &&
2367 isp1760_hcd_is_set(hcd, PORT_RESUME))
2368 status |= USB_PORT_STAT_SUSPEND;
2369 if (isp1760_hcd_is_set(hcd, PORT_RESET))
2370 status |= USB_PORT_STAT_RESET;
2371 if (isp1760_hcd_is_set(hcd, PORT_POWER))
2372 status |= USB_PORT_STAT_POWER;
2374 put_unaligned(cpu_to_le32(status), (__le32 *) buf);
2378 case C_HUB_LOCAL_POWER:
2379 case C_HUB_OVER_CURRENT:
2380 /* no hub-wide feature/status flags */
2386 case SetPortFeature:
2388 if (!wIndex || wIndex > ports)
2392 if (isp1760_hcd_is_set(hcd, PORT_OWNER))
2396 case USB_PORT_FEAT_ENABLE:
2397 isp1760_hcd_set(hcd, PORT_PE);
2400 case USB_PORT_FEAT_SUSPEND:
2401 if (!isp1760_hcd_is_set(hcd, PORT_PE) ||
2402 isp1760_hcd_is_set(hcd, PORT_RESET))
2405 isp1760_hcd_set(hcd, PORT_SUSPEND);
2407 case USB_PORT_FEAT_POWER:
2408 if (isp1760_hcd_ppc_is_set(hcd))
2409 isp1760_hcd_set(hcd, PORT_POWER);
2411 case USB_PORT_FEAT_RESET:
2412 if (isp1760_hcd_is_set(hcd, PORT_RESUME))
2414 /* line status bits may report this as low speed,
2415 * which can be fine if this root hub has a
2416 * transaction translator built in.
2418 if ((isp1760_hcd_is_set(hcd, PORT_CONNECT) &&
2419 !isp1760_hcd_is_set(hcd, PORT_PE)) &&
2420 (isp1760_hcd_read(hcd, PORT_LSTATUS) == 1)) {
2421 isp1760_hcd_set(hcd, PORT_OWNER);
2423 isp1760_hcd_set(hcd, PORT_RESET);
2424 isp1760_hcd_clear(hcd, PORT_PE);
2427 * caller must wait, then call GetPortStatus
2428 * usb 2.0 spec says 50 ms resets on root
2430 priv->reset_done = jiffies +
2431 msecs_to_jiffies(50);
2441 /* "stall" on error */
2444 spin_unlock_irqrestore(&priv->lock, flags);
2448 static int isp1760_get_frame(struct usb_hcd *hcd)
2450 struct isp1760_hcd *priv = hcd_to_priv(hcd);
2453 fr = isp1760_hcd_read(hcd, HC_FRINDEX);
2454 return (fr >> 3) % priv->periodic_size;
2457 static void isp1760_stop(struct usb_hcd *hcd)
2459 struct isp1760_hcd *priv = hcd_to_priv(hcd);
2461 del_timer(&errata2_timer);
2463 isp1760_hub_control(hcd, ClearPortFeature, USB_PORT_FEAT_POWER, 1,
2467 spin_lock_irq(&priv->lock);
2470 isp1760_hcd_clear(hcd, HW_GLOBAL_INTR_EN);
2471 spin_unlock_irq(&priv->lock);
2473 isp1760_hcd_clear(hcd, FLAG_CF);
2476 static void isp1760_shutdown(struct usb_hcd *hcd)
2480 isp1760_hcd_clear(hcd, HW_GLOBAL_INTR_EN);
2482 isp1760_hcd_clear(hcd, CMD_RUN);
2485 static void isp1760_clear_tt_buffer_complete(struct usb_hcd *hcd,
2486 struct usb_host_endpoint *ep)
2488 struct isp1760_hcd *priv = hcd_to_priv(hcd);
2489 struct isp1760_qh *qh = ep->hcpriv;
2490 unsigned long spinflags;
2495 spin_lock_irqsave(&priv->lock, spinflags);
2496 qh->tt_buffer_dirty = 0;
2498 spin_unlock_irqrestore(&priv->lock, spinflags);
2502 static const struct hc_driver isp1760_hc_driver = {
2503 .description = "isp1760-hcd",
2504 .product_desc = "NXP ISP1760 USB Host Controller",
2505 .hcd_priv_size = sizeof(struct isp1760_hcd *),
2507 .flags = HCD_MEMORY | HCD_USB2,
2508 .reset = isp1760_hc_setup,
2509 .start = isp1760_run,
2510 .stop = isp1760_stop,
2511 .shutdown = isp1760_shutdown,
2512 .urb_enqueue = isp1760_urb_enqueue,
2513 .urb_dequeue = isp1760_urb_dequeue,
2514 .endpoint_disable = isp1760_endpoint_disable,
2515 .get_frame_number = isp1760_get_frame,
2516 .hub_status_data = isp1760_hub_status_data,
2517 .hub_control = isp1760_hub_control,
2518 .clear_tt_buffer_complete = isp1760_clear_tt_buffer_complete,
2521 int __init isp1760_init_kmem_once(void)
2523 urb_listitem_cachep = kmem_cache_create("isp1760_urb_listitem",
2524 sizeof(struct urb_listitem), 0, SLAB_TEMPORARY, NULL);
2526 if (!urb_listitem_cachep)
2529 qtd_cachep = kmem_cache_create("isp1760_qtd",
2530 sizeof(struct isp1760_qtd), 0, SLAB_TEMPORARY, NULL);
2533 goto destroy_urb_listitem;
2535 qh_cachep = kmem_cache_create("isp1760_qh", sizeof(struct isp1760_qh),
2536 0, SLAB_TEMPORARY, NULL);
2544 kmem_cache_destroy(qtd_cachep);
2546 destroy_urb_listitem:
2547 kmem_cache_destroy(urb_listitem_cachep);
2552 void isp1760_deinit_kmem_cache(void)
2554 kmem_cache_destroy(qtd_cachep);
2555 kmem_cache_destroy(qh_cachep);
2556 kmem_cache_destroy(urb_listitem_cachep);
2559 int isp1760_hcd_register(struct isp1760_hcd *priv, struct resource *mem,
2560 int irq, unsigned long irqflags,
2563 const struct isp1760_memory_layout *mem_layout = priv->memory_layout;
2564 struct usb_hcd *hcd;
2567 hcd = usb_create_hcd(&isp1760_hc_driver, dev, dev_name(dev));
2571 *(struct isp1760_hcd **)hcd->hcd_priv = priv;
2575 priv->atl_slots = kcalloc(mem_layout->slot_num,
2576 sizeof(struct isp1760_slotinfo), GFP_KERNEL);
2577 if (!priv->atl_slots) {
2582 priv->int_slots = kcalloc(mem_layout->slot_num,
2583 sizeof(struct isp1760_slotinfo), GFP_KERNEL);
2584 if (!priv->int_slots) {
2586 goto free_atl_slots;
2592 hcd->rsrc_start = mem->start;
2593 hcd->rsrc_len = resource_size(mem);
2595 /* This driver doesn't support wakeup requests */
2596 hcd->cant_recv_wakeups = 1;
2598 ret = usb_add_hcd(hcd, irq, irqflags);
2600 goto free_int_slots;
2602 device_wakeup_enable(hcd->self.controller);
2607 kfree(priv->int_slots);
2609 kfree(priv->atl_slots);
2615 void isp1760_hcd_unregister(struct isp1760_hcd *priv)
2620 usb_remove_hcd(priv->hcd);
2621 usb_put_hcd(priv->hcd);
2622 kfree(priv->atl_slots);
2623 kfree(priv->int_slots);