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.
10 * (c) 2007 Sebastian Siewior <bigeasy@linutronix.de>
12 * (c) 2011 Arvid Brodin <arvid.brodin@enea.com>
14 * Copyright 2021 Linaro, Rui Miguel Silva <rui.silva@linaro.org>
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 <asm/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 isp1763_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 isp1763, 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 = isp1763_hc_portsc1_fields[field];
215 u32 port_status = readl(priv->base + ISP1763_HC_PORTSC1);
218 writel(port_status | bit, priv->base + ISP1763_HC_PORTSC1);
220 writel(port_status & ~bit, priv->base + ISP1763_HC_PORTSC1);
223 static void isp1760_hcd_write(struct usb_hcd *hcd, u32 field, u32 val)
225 struct isp1760_hcd *priv = hcd_to_priv(hcd);
227 if (unlikely(priv->is_isp1763 &&
228 (field >= PORT_OWNER && field <= PORT_CONNECT)))
229 return isp1760_hcd_portsc1_set_clear(priv, field, val);
231 isp1760_field_write(priv->fields, field, val);
234 static void isp1760_hcd_set(struct usb_hcd *hcd, u32 field)
236 isp1760_hcd_write(hcd, field, 0xFFFFFFFF);
239 static void isp1760_hcd_clear(struct usb_hcd *hcd, u32 field)
241 isp1760_hcd_write(hcd, field, 0);
244 static int isp1760_hcd_set_and_wait(struct usb_hcd *hcd, u32 field,
247 struct isp1760_hcd *priv = hcd_to_priv(hcd);
250 isp1760_hcd_set(hcd, field);
252 return regmap_field_read_poll_timeout(priv->fields[field], val,
253 val, 10, timeout_us);
256 static int isp1760_hcd_set_and_wait_swap(struct usb_hcd *hcd, u32 field,
259 struct isp1760_hcd *priv = hcd_to_priv(hcd);
262 isp1760_hcd_set(hcd, field);
264 return regmap_field_read_poll_timeout(priv->fields[field], val,
265 !val, 10, timeout_us);
268 static int isp1760_hcd_clear_and_wait(struct usb_hcd *hcd, u32 field,
271 struct isp1760_hcd *priv = hcd_to_priv(hcd);
274 isp1760_hcd_clear(hcd, field);
276 return regmap_field_read_poll_timeout(priv->fields[field], val,
277 !val, 10, timeout_us);
280 static bool isp1760_hcd_is_set(struct usb_hcd *hcd, u32 field)
282 return !!isp1760_hcd_read(hcd, field);
285 static bool isp1760_hcd_ppc_is_set(struct usb_hcd *hcd)
287 struct isp1760_hcd *priv = hcd_to_priv(hcd);
289 if (priv->is_isp1763)
292 return isp1760_hcd_is_set(hcd, HCS_PPC);
295 static u32 isp1760_hcd_n_ports(struct usb_hcd *hcd)
297 struct isp1760_hcd *priv = hcd_to_priv(hcd);
299 if (priv->is_isp1763)
302 return isp1760_hcd_read(hcd, HCS_N_PORTS);
306 * Access functions for isp176x memory (offset >= 0x0400).
308 * bank_reads8() reads memory locations prefetched by an earlier write to
309 * HC_MEMORY_REG (see isp176x datasheet). Unless you want to do fancy multi-
310 * bank optimizations, you should use the more generic mem_read() below.
312 * For access to ptd memory, use the specialized ptd_read() and ptd_write()
315 * These functions copy via MMIO data to/from the device. memcpy_{to|from}io()
316 * doesn't quite work because some people have to enforce 32-bit access
318 static void bank_reads8(void __iomem *src_base, u32 src_offset, u32 bank_addr,
319 __u32 *dst, u32 bytes)
326 src = src_base + (bank_addr | src_offset);
328 if (src_offset < PAYLOAD_OFFSET) {
330 *dst = readl_relaxed(src);
337 *dst = __raw_readl(src);
347 /* in case we have 3, 2 or 1 by left. The dst buffer may not be fully
350 if (src_offset < PAYLOAD_OFFSET)
351 val = readl_relaxed(src);
353 val = __raw_readl(src);
355 dst_byteptr = (void *) dst;
356 src_byteptr = (void *) &val;
358 *dst_byteptr = *src_byteptr;
365 static void isp1760_mem_read(struct usb_hcd *hcd, u32 src_offset, void *dst,
368 struct isp1760_hcd *priv = hcd_to_priv(hcd);
370 isp1760_hcd_write(hcd, MEM_BANK_SEL, ISP_BANK_0);
371 isp1760_hcd_write(hcd, MEM_START_ADDR, 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_hcd_write(hcd, MEM_BANK_SEL, ISP_BANK_0);
500 isp1760_hcd_write(hcd, MEM_START_ADDR, src_offset);
503 bank_reads8(priv->base, src_offset, ISP_BANK_0, (void *)ptd,
507 static void isp1763_ptd_read(struct usb_hcd *hcd, u32 ptd_offset, u32 slot,
510 u16 src_offset = ptd_offset + slot * sizeof(*ptd);
511 struct ptd_le32 le32_ptd;
513 isp1763_mem_read(hcd, src_offset, (u16 *)&le32_ptd, sizeof(le32_ptd));
514 /* Normalize the data obtained */
515 ptd->dw0 = le32_to_dw(le32_ptd.dw0);
516 ptd->dw1 = le32_to_dw(le32_ptd.dw1);
517 ptd->dw2 = le32_to_dw(le32_ptd.dw2);
518 ptd->dw3 = le32_to_dw(le32_ptd.dw3);
519 ptd->dw4 = le32_to_dw(le32_ptd.dw4);
520 ptd->dw5 = le32_to_dw(le32_ptd.dw5);
521 ptd->dw6 = le32_to_dw(le32_ptd.dw6);
522 ptd->dw7 = le32_to_dw(le32_ptd.dw7);
525 static void ptd_read(struct usb_hcd *hcd, u32 ptd_offset, u32 slot,
528 struct isp1760_hcd *priv = hcd_to_priv(hcd);
530 if (!priv->is_isp1763)
531 return isp1760_ptd_read(hcd, ptd_offset, slot, ptd);
533 isp1763_ptd_read(hcd, ptd_offset, slot, ptd);
536 static void isp1763_ptd_write(struct usb_hcd *hcd, u32 ptd_offset, u32 slot,
539 u16 dst_offset = ptd_offset + slot * sizeof(*cpu_ptd);
542 ptd.dw0 = dw_to_le32(cpu_ptd->dw0);
543 ptd.dw1 = dw_to_le32(cpu_ptd->dw1);
544 ptd.dw2 = dw_to_le32(cpu_ptd->dw2);
545 ptd.dw3 = dw_to_le32(cpu_ptd->dw3);
546 ptd.dw4 = dw_to_le32(cpu_ptd->dw4);
547 ptd.dw5 = dw_to_le32(cpu_ptd->dw5);
548 ptd.dw6 = dw_to_le32(cpu_ptd->dw6);
549 ptd.dw7 = dw_to_le32(cpu_ptd->dw7);
551 isp1763_mem_write(hcd, dst_offset, (u16 *)&ptd.dw0,
552 8 * sizeof(ptd.dw0));
555 static void isp1760_ptd_write(void __iomem *base, u32 ptd_offset, u32 slot,
558 u32 dst_offset = ptd_offset + slot * sizeof(*ptd);
561 * Make sure dw0 gets written last (after other dw's and after payload)
562 * since it contains the enable bit
564 isp1760_mem_write(base, dst_offset + sizeof(ptd->dw0),
565 (__force u32 *)&ptd->dw1, 7 * sizeof(ptd->dw1));
567 isp1760_mem_write(base, dst_offset, (__force u32 *)&ptd->dw0,
571 static void ptd_write(struct usb_hcd *hcd, u32 ptd_offset, u32 slot,
574 struct isp1760_hcd *priv = hcd_to_priv(hcd);
576 if (!priv->is_isp1763)
577 return isp1760_ptd_write(priv->base, ptd_offset, slot, ptd);
579 isp1763_ptd_write(hcd, ptd_offset, slot, ptd);
582 /* memory management of the 60kb on the chip from 0x1000 to 0xffff */
583 static void init_memory(struct isp1760_hcd *priv)
585 const struct isp1760_memory_layout *mem = priv->memory_layout;
589 payload_addr = PAYLOAD_OFFSET;
591 for (i = 0, curr = 0; i < ARRAY_SIZE(mem->blocks); i++) {
592 for (j = 0; j < mem->blocks[i]; j++, curr++) {
593 priv->memory_pool[curr + j].start = payload_addr;
594 priv->memory_pool[curr + j].size = mem->blocks_size[i];
595 priv->memory_pool[curr + j].free = 1;
596 payload_addr += priv->memory_pool[curr + j].size;
600 WARN_ON(payload_addr - priv->memory_pool[0].start >
601 mem->payload_area_size);
604 static void alloc_mem(struct usb_hcd *hcd, struct isp1760_qtd *qtd)
606 struct isp1760_hcd *priv = hcd_to_priv(hcd);
607 const struct isp1760_memory_layout *mem = priv->memory_layout;
610 WARN_ON(qtd->payload_addr);
615 for (i = 0; i < mem->payload_blocks; i++) {
616 if (priv->memory_pool[i].size >= qtd->length &&
617 priv->memory_pool[i].free) {
618 priv->memory_pool[i].free = 0;
619 qtd->payload_addr = priv->memory_pool[i].start;
625 static void free_mem(struct usb_hcd *hcd, struct isp1760_qtd *qtd)
627 struct isp1760_hcd *priv = hcd_to_priv(hcd);
628 const struct isp1760_memory_layout *mem = priv->memory_layout;
631 if (!qtd->payload_addr)
634 for (i = 0; i < mem->payload_blocks; i++) {
635 if (priv->memory_pool[i].start == qtd->payload_addr) {
636 WARN_ON(priv->memory_pool[i].free);
637 priv->memory_pool[i].free = 1;
638 qtd->payload_addr = 0;
643 dev_err(hcd->self.controller, "%s: Invalid pointer: %08x\n",
644 __func__, qtd->payload_addr);
646 qtd->payload_addr = 0;
649 /* reset a non-running (STS_HALT == 1) controller */
650 static int ehci_reset(struct usb_hcd *hcd)
652 struct isp1760_hcd *priv = hcd_to_priv(hcd);
654 hcd->state = HC_STATE_HALT;
655 priv->next_statechange = jiffies;
657 return isp1760_hcd_set_and_wait_swap(hcd, CMD_RESET, 250 * 1000);
660 static struct isp1760_qh *qh_alloc(gfp_t flags)
662 struct isp1760_qh *qh;
664 qh = kmem_cache_zalloc(qh_cachep, flags);
668 INIT_LIST_HEAD(&qh->qh_list);
669 INIT_LIST_HEAD(&qh->qtd_list);
675 static void qh_free(struct isp1760_qh *qh)
677 WARN_ON(!list_empty(&qh->qtd_list));
678 WARN_ON(qh->slot > -1);
679 kmem_cache_free(qh_cachep, qh);
682 /* one-time init, only for memory state */
683 static int priv_init(struct usb_hcd *hcd)
685 struct isp1760_hcd *priv = hcd_to_priv(hcd);
690 spin_lock_init(&priv->lock);
692 for (i = 0; i < QH_END; i++)
693 INIT_LIST_HEAD(&priv->qh_list[i]);
696 * hw default: 1K periodic list heads, one per frame.
697 * periodic_size can shrink by USBCMD update if hcc_params allows.
699 priv->periodic_size = DEFAULT_I_TDPS;
701 if (priv->is_isp1763) {
706 /* controllers may cache some of the periodic schedule ... */
707 isoc_cache = isp1760_hcd_read(hcd, HCC_ISOC_CACHE);
708 isoc_thres = isp1760_hcd_read(hcd, HCC_ISOC_THRES);
710 /* full frame cache */
713 else /* N microframes cached */
714 priv->i_thresh = 2 + isoc_thres;
719 static int isp1760_hc_setup(struct usb_hcd *hcd)
721 struct isp1760_hcd *priv = hcd_to_priv(hcd);
727 if (priv->is_isp1763)
730 pattern = 0xdeadcafe;
732 isp1760_hcd_write(hcd, HC_SCRATCH, pattern);
734 /* Change bus pattern */
735 scratch = isp1760_hcd_read(hcd, HC_CHIP_ID_HIGH);
736 dev_err(hcd->self.controller, "Scratch test 0x%08x\n", scratch);
737 scratch = isp1760_hcd_read(hcd, HC_SCRATCH);
738 if (scratch != pattern) {
739 dev_err(hcd->self.controller, "Scratch test failed. 0x%08x\n", scratch);
744 * The RESET_HC bit in the SW_RESET register is supposed to reset the
745 * host controller without touching the CPU interface registers, but at
746 * least on the ISP1761 it seems to behave as the RESET_ALL bit and
747 * reset the whole device. We thus can't use it here, so let's reset
748 * the host controller through the EHCI USB Command register. The device
749 * has been reset in core code anyway, so this shouldn't matter.
751 isp1760_hcd_clear(hcd, ISO_BUF_FILL);
752 isp1760_hcd_clear(hcd, INT_BUF_FILL);
753 isp1760_hcd_clear(hcd, ATL_BUF_FILL);
755 isp1760_hcd_set(hcd, HC_ATL_PTD_SKIPMAP);
756 isp1760_hcd_set(hcd, HC_INT_PTD_SKIPMAP);
757 isp1760_hcd_set(hcd, HC_ISO_PTD_SKIPMAP);
759 result = ehci_reset(hcd);
766 if (priv->is_isp1763)
767 atx_reset = SW_RESET_RESET_ATX;
769 atx_reset = ALL_ATX_RESET;
771 isp1760_hcd_set(hcd, atx_reset);
773 isp1760_hcd_clear(hcd, atx_reset);
775 if (priv->is_isp1763) {
776 isp1760_hcd_set(hcd, HW_OTG_DISABLE);
777 isp1760_hcd_set(hcd, HW_SW_SEL_HC_DC_CLEAR);
778 isp1760_hcd_set(hcd, HW_HC_2_DIS_CLEAR);
781 isp1760_hcd_set(hcd, HW_INTF_LOCK);
784 isp1760_hcd_set(hcd, HC_INT_IRQ_ENABLE);
785 isp1760_hcd_set(hcd, HC_ATL_IRQ_ENABLE);
787 return priv_init(hcd);
790 static u32 base_to_chip(u32 base)
792 return ((base - 0x400) >> 3);
795 static int last_qtd_of_urb(struct isp1760_qtd *qtd, struct isp1760_qh *qh)
799 if (list_is_last(&qtd->qtd_list, &qh->qtd_list))
803 qtd = list_entry(qtd->qtd_list.next, typeof(*qtd), qtd_list);
804 return (qtd->urb != urb);
807 /* magic numbers that can affect system performance */
808 #define EHCI_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
809 #define EHCI_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
810 #define EHCI_TUNE_RL_TT 0
811 #define EHCI_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
812 #define EHCI_TUNE_MULT_TT 1
813 #define EHCI_TUNE_FLS 2 /* (small) 256 frame schedule */
815 static void create_ptd_atl(struct isp1760_qh *qh,
816 struct isp1760_qtd *qtd, struct ptd *ptd)
821 u32 nak = NAK_COUNTER;
823 memset(ptd, 0, sizeof(*ptd));
825 /* according to 3.6.2, max packet len can not be > 0x400 */
826 maxpacket = usb_maxpacket(qtd->urb->dev, qtd->urb->pipe,
827 usb_pipeout(qtd->urb->pipe));
828 multi = 1 + ((maxpacket >> 11) & 0x3);
832 ptd->dw0 = DW0_VALID_BIT;
833 ptd->dw0 |= TO_DW0_LENGTH(qtd->length);
834 ptd->dw0 |= TO_DW0_MAXPACKET(maxpacket);
835 ptd->dw0 |= TO_DW0_ENDPOINT(usb_pipeendpoint(qtd->urb->pipe));
838 ptd->dw1 = TO_DW((usb_pipeendpoint(qtd->urb->pipe) >> 1));
839 ptd->dw1 |= TO_DW1_DEVICE_ADDR(usb_pipedevice(qtd->urb->pipe));
840 ptd->dw1 |= TO_DW1_PID_TOKEN(qtd->packet_type);
842 if (usb_pipebulk(qtd->urb->pipe))
843 ptd->dw1 |= DW1_TRANS_BULK;
844 else if (usb_pipeint(qtd->urb->pipe))
845 ptd->dw1 |= DW1_TRANS_INT;
847 if (qtd->urb->dev->speed != USB_SPEED_HIGH) {
848 /* split transaction */
850 ptd->dw1 |= DW1_TRANS_SPLIT;
851 if (qtd->urb->dev->speed == USB_SPEED_LOW)
852 ptd->dw1 |= DW1_SE_USB_LOSPEED;
854 ptd->dw1 |= TO_DW1_PORT_NUM(qtd->urb->dev->ttport);
855 ptd->dw1 |= TO_DW1_HUB_NUM(qtd->urb->dev->tt->hub->devnum);
857 /* SE bit for Split INT transfers */
858 if (usb_pipeint(qtd->urb->pipe) &&
859 (qtd->urb->dev->speed == USB_SPEED_LOW))
860 ptd->dw1 |= DW1_SE_USB_LOSPEED;
865 ptd->dw0 |= TO_DW0_MULTI(multi);
866 if (usb_pipecontrol(qtd->urb->pipe) ||
867 usb_pipebulk(qtd->urb->pipe))
868 ptd->dw3 |= TO_DW3_PING(qh->ping);
872 ptd->dw2 |= TO_DW2_DATA_START_ADDR(base_to_chip(qtd->payload_addr));
873 ptd->dw2 |= TO_DW2_RL(rl);
876 ptd->dw3 |= TO_DW3_NAKCOUNT(nak);
877 ptd->dw3 |= TO_DW3_DATA_TOGGLE(qh->toggle);
878 if (usb_pipecontrol(qtd->urb->pipe)) {
879 if (qtd->data_buffer == qtd->urb->setup_packet)
880 ptd->dw3 &= ~TO_DW3_DATA_TOGGLE(1);
881 else if (last_qtd_of_urb(qtd, qh))
882 ptd->dw3 |= TO_DW3_DATA_TOGGLE(1);
885 ptd->dw3 |= DW3_ACTIVE_BIT;
887 ptd->dw3 |= TO_DW3_CERR(ERR_COUNTER);
890 static void transform_add_int(struct isp1760_qh *qh,
891 struct isp1760_qtd *qtd, struct ptd *ptd)
897 * Most of this is guessing. ISP1761 datasheet is quite unclear, and
898 * the algorithm from the original Philips driver code, which was
899 * pretty much used in this driver before as well, is quite horrendous
900 * and, i believe, incorrect. The code below follows the datasheet and
901 * USB2.0 spec as far as I can tell, and plug/unplug seems to be much
902 * more reliable this way (fingers crossed...).
905 if (qtd->urb->dev->speed == USB_SPEED_HIGH) {
906 /* urb->interval is in units of microframes (1/8 ms) */
907 period = qtd->urb->interval >> 3;
909 if (qtd->urb->interval > 4)
910 usof = 0x01; /* One bit set =>
911 interval 1 ms * uFrame-match */
912 else if (qtd->urb->interval > 2)
913 usof = 0x22; /* Two bits set => interval 1/2 ms */
914 else if (qtd->urb->interval > 1)
915 usof = 0x55; /* Four bits set => interval 1/4 ms */
917 usof = 0xff; /* All bits set => interval 1/8 ms */
919 /* urb->interval is in units of frames (1 ms) */
920 period = qtd->urb->interval;
921 usof = 0x0f; /* Execute Start Split on any of the
922 four first uFrames */
925 * First 8 bits in dw5 is uSCS and "specifies which uSOF the
926 * complete split needs to be sent. Valid only for IN." Also,
927 * "All bits can be set to one for every transfer." (p 82,
928 * ISP1761 data sheet.) 0x1c is from Philips driver. Where did
929 * that number come from? 0xff seems to work fine...
931 /* ptd->dw5 = 0x1c; */
932 ptd->dw5 = TO_DW(0xff); /* Execute Complete Split on any uFrame */
935 period = period >> 1;/* Ensure equal or shorter period than requested */
936 period &= 0xf8; /* Mask off too large values and lowest unused 3 bits */
938 ptd->dw2 |= TO_DW(period);
939 ptd->dw4 = TO_DW(usof);
942 static void create_ptd_int(struct isp1760_qh *qh,
943 struct isp1760_qtd *qtd, struct ptd *ptd)
945 create_ptd_atl(qh, qtd, ptd);
946 transform_add_int(qh, qtd, ptd);
949 static void isp1760_urb_done(struct usb_hcd *hcd, struct urb *urb)
950 __releases(priv->lock)
951 __acquires(priv->lock)
953 struct isp1760_hcd *priv = hcd_to_priv(hcd);
955 if (!urb->unlinked) {
956 if (urb->status == -EINPROGRESS)
960 if (usb_pipein(urb->pipe) && usb_pipetype(urb->pipe) != PIPE_CONTROL) {
962 for (ptr = urb->transfer_buffer;
963 ptr < urb->transfer_buffer + urb->transfer_buffer_length;
965 flush_dcache_page(virt_to_page(ptr));
968 /* complete() can reenter this HCD */
969 usb_hcd_unlink_urb_from_ep(hcd, urb);
970 spin_unlock(&priv->lock);
971 usb_hcd_giveback_urb(hcd, urb, urb->status);
972 spin_lock(&priv->lock);
975 static struct isp1760_qtd *qtd_alloc(gfp_t flags, struct urb *urb,
978 struct isp1760_qtd *qtd;
980 qtd = kmem_cache_zalloc(qtd_cachep, flags);
984 INIT_LIST_HEAD(&qtd->qtd_list);
986 qtd->packet_type = packet_type;
987 qtd->status = QTD_ENQUEUED;
988 qtd->actual_length = 0;
993 static void qtd_free(struct isp1760_qtd *qtd)
995 WARN_ON(qtd->payload_addr);
996 kmem_cache_free(qtd_cachep, qtd);
999 static void start_bus_transfer(struct usb_hcd *hcd, u32 ptd_offset, int slot,
1000 struct isp1760_slotinfo *slots,
1001 struct isp1760_qtd *qtd, struct isp1760_qh *qh,
1004 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1005 const struct isp1760_memory_layout *mem = priv->memory_layout;
1008 WARN_ON((slot < 0) || (slot > mem->slot_num - 1));
1009 WARN_ON(qtd->length && !qtd->payload_addr);
1010 WARN_ON(slots[slot].qtd);
1011 WARN_ON(slots[slot].qh);
1012 WARN_ON(qtd->status != QTD_PAYLOAD_ALLOC);
1014 if (priv->is_isp1763)
1017 /* Make sure done map has not triggered from some unlinked transfer */
1018 if (ptd_offset == ATL_PTD_OFFSET) {
1019 skip_map = isp1760_hcd_read(hcd, HC_ATL_PTD_SKIPMAP);
1020 isp1760_hcd_write(hcd, HC_ATL_PTD_SKIPMAP,
1021 skip_map | (1 << slot));
1022 priv->atl_done_map |= isp1760_hcd_read(hcd, HC_ATL_PTD_DONEMAP);
1023 priv->atl_done_map &= ~(1 << slot);
1025 skip_map = isp1760_hcd_read(hcd, HC_INT_PTD_SKIPMAP);
1026 isp1760_hcd_write(hcd, HC_INT_PTD_SKIPMAP,
1027 skip_map | (1 << slot));
1028 priv->int_done_map |= isp1760_hcd_read(hcd, HC_INT_PTD_DONEMAP);
1029 priv->int_done_map &= ~(1 << slot);
1032 skip_map &= ~(1 << slot);
1034 qtd->status = QTD_XFER_STARTED;
1035 slots[slot].timestamp = jiffies;
1036 slots[slot].qtd = qtd;
1037 slots[slot].qh = qh;
1038 ptd_write(hcd, ptd_offset, slot, ptd);
1040 if (ptd_offset == ATL_PTD_OFFSET)
1041 isp1760_hcd_write(hcd, HC_ATL_PTD_SKIPMAP, skip_map);
1043 isp1760_hcd_write(hcd, HC_INT_PTD_SKIPMAP, skip_map);
1046 static int is_short_bulk(struct isp1760_qtd *qtd)
1048 return (usb_pipebulk(qtd->urb->pipe) &&
1049 (qtd->actual_length < qtd->length));
1052 static void collect_qtds(struct usb_hcd *hcd, struct isp1760_qh *qh,
1053 struct list_head *urb_list)
1055 struct isp1760_qtd *qtd, *qtd_next;
1056 struct urb_listitem *urb_listitem;
1059 list_for_each_entry_safe(qtd, qtd_next, &qh->qtd_list, qtd_list) {
1060 if (qtd->status < QTD_XFER_COMPLETE)
1063 last_qtd = last_qtd_of_urb(qtd, qh);
1065 if ((!last_qtd) && (qtd->status == QTD_RETIRE))
1066 qtd_next->status = QTD_RETIRE;
1068 if (qtd->status == QTD_XFER_COMPLETE) {
1069 if (qtd->actual_length) {
1070 switch (qtd->packet_type) {
1072 mem_read(hcd, qtd->payload_addr,
1074 qtd->actual_length);
1077 qtd->urb->actual_length +=
1085 if (is_short_bulk(qtd)) {
1086 if (qtd->urb->transfer_flags & URB_SHORT_NOT_OK)
1087 qtd->urb->status = -EREMOTEIO;
1089 qtd_next->status = QTD_RETIRE;
1093 if (qtd->payload_addr)
1097 if ((qtd->status == QTD_RETIRE) &&
1098 (qtd->urb->status == -EINPROGRESS))
1099 qtd->urb->status = -EPIPE;
1100 /* Defer calling of urb_done() since it releases lock */
1101 urb_listitem = kmem_cache_zalloc(urb_listitem_cachep,
1103 if (unlikely(!urb_listitem))
1104 break; /* Try again on next call */
1105 urb_listitem->urb = qtd->urb;
1106 list_add_tail(&urb_listitem->urb_list, urb_list);
1109 list_del(&qtd->qtd_list);
1114 #define ENQUEUE_DEPTH 2
1115 static void enqueue_qtds(struct usb_hcd *hcd, struct isp1760_qh *qh)
1117 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1118 const struct isp1760_memory_layout *mem = priv->memory_layout;
1119 int slot_num = mem->slot_num;
1121 struct isp1760_slotinfo *slots;
1122 int curr_slot, free_slot;
1125 struct isp1760_qtd *qtd;
1127 if (unlikely(list_empty(&qh->qtd_list))) {
1132 /* Make sure this endpoint's TT buffer is clean before queueing ptds */
1133 if (qh->tt_buffer_dirty)
1136 if (usb_pipeint(list_entry(qh->qtd_list.next, struct isp1760_qtd,
1137 qtd_list)->urb->pipe)) {
1138 ptd_offset = INT_PTD_OFFSET;
1139 slots = priv->int_slots;
1141 ptd_offset = ATL_PTD_OFFSET;
1142 slots = priv->atl_slots;
1146 for (curr_slot = 0; curr_slot < slot_num; curr_slot++) {
1147 if ((free_slot == -1) && (slots[curr_slot].qtd == NULL))
1148 free_slot = curr_slot;
1149 if (slots[curr_slot].qh == qh)
1154 list_for_each_entry(qtd, &qh->qtd_list, qtd_list) {
1155 if (qtd->status == QTD_ENQUEUED) {
1156 WARN_ON(qtd->payload_addr);
1157 alloc_mem(hcd, qtd);
1158 if ((qtd->length) && (!qtd->payload_addr))
1161 if (qtd->length && (qtd->packet_type == SETUP_PID ||
1162 qtd->packet_type == OUT_PID)) {
1163 mem_write(hcd, qtd->payload_addr,
1164 qtd->data_buffer, qtd->length);
1167 qtd->status = QTD_PAYLOAD_ALLOC;
1170 if (qtd->status == QTD_PAYLOAD_ALLOC) {
1172 if ((curr_slot > 31) && (free_slot == -1))
1173 dev_dbg(hcd->self.controller, "%s: No slot "
1174 "available for transfer\n", __func__);
1176 /* Start xfer for this endpoint if not already done */
1177 if ((curr_slot > slot_num - 1) && (free_slot > -1)) {
1178 if (usb_pipeint(qtd->urb->pipe))
1179 create_ptd_int(qh, qtd, &ptd);
1181 create_ptd_atl(qh, qtd, &ptd);
1183 start_bus_transfer(hcd, ptd_offset, free_slot,
1184 slots, qtd, qh, &ptd);
1185 curr_slot = free_slot;
1189 if (n >= ENQUEUE_DEPTH)
1195 static void schedule_ptds(struct usb_hcd *hcd)
1197 struct isp1760_hcd *priv;
1198 struct isp1760_qh *qh, *qh_next;
1199 struct list_head *ep_queue;
1200 LIST_HEAD(urb_list);
1201 struct urb_listitem *urb_listitem, *urb_listitem_next;
1209 priv = hcd_to_priv(hcd);
1212 * check finished/retired xfers, transfer payloads, call urb_done()
1214 for (i = 0; i < QH_END; i++) {
1215 ep_queue = &priv->qh_list[i];
1216 list_for_each_entry_safe(qh, qh_next, ep_queue, qh_list) {
1217 collect_qtds(hcd, qh, &urb_list);
1218 if (list_empty(&qh->qtd_list))
1219 list_del(&qh->qh_list);
1223 list_for_each_entry_safe(urb_listitem, urb_listitem_next, &urb_list,
1225 isp1760_urb_done(hcd, urb_listitem->urb);
1226 kmem_cache_free(urb_listitem_cachep, urb_listitem);
1230 * Schedule packets for transfer.
1232 * According to USB2.0 specification:
1234 * 1st prio: interrupt xfers, up to 80 % of bandwidth
1235 * 2nd prio: control xfers
1236 * 3rd prio: bulk xfers
1238 * ... but let's use a simpler scheme here (mostly because ISP1761 doc
1239 * is very unclear on how to prioritize traffic):
1241 * 1) Enqueue any queued control transfers, as long as payload chip mem
1242 * and PTD ATL slots are available.
1243 * 2) Enqueue any queued INT transfers, as long as payload chip mem
1244 * and PTD INT slots are available.
1245 * 3) Enqueue any queued bulk transfers, as long as payload chip mem
1246 * and PTD ATL slots are available.
1248 * Use double buffering (ENQUEUE_DEPTH==2) as a compromise between
1249 * conservation of chip mem and performance.
1251 * I'm sure this scheme could be improved upon!
1253 for (i = 0; i < QH_END; i++) {
1254 ep_queue = &priv->qh_list[i];
1255 list_for_each_entry_safe(qh, qh_next, ep_queue, qh_list)
1256 enqueue_qtds(hcd, qh);
1260 #define PTD_STATE_QTD_DONE 1
1261 #define PTD_STATE_QTD_RELOAD 2
1262 #define PTD_STATE_URB_RETIRE 3
1264 static int check_int_transfer(struct usb_hcd *hcd, struct ptd *ptd,
1270 dw4 = TO_U32(ptd->dw4);
1273 /* FIXME: ISP1761 datasheet does not say what to do with these. Do we
1274 need to handle these errors? Is it done in hardware? */
1276 if (ptd->dw3 & DW3_HALT_BIT) {
1278 urb->status = -EPROTO; /* Default unknown error */
1280 for (i = 0; i < 8; i++) {
1281 switch (dw4 & 0x7) {
1283 dev_dbg(hcd->self.controller, "%s: underrun "
1284 "during uFrame %d\n",
1286 urb->status = -ECOMM; /* Could not write data */
1289 dev_dbg(hcd->self.controller, "%s: transaction "
1290 "error during uFrame %d\n",
1292 urb->status = -EPROTO; /* timeout, bad CRC, PID
1296 dev_dbg(hcd->self.controller, "%s: babble "
1297 "error during uFrame %d\n",
1299 urb->status = -EOVERFLOW;
1305 return PTD_STATE_URB_RETIRE;
1308 return PTD_STATE_QTD_DONE;
1311 static int check_atl_transfer(struct usb_hcd *hcd, struct ptd *ptd,
1315 if (ptd->dw3 & DW3_HALT_BIT) {
1316 if (ptd->dw3 & DW3_BABBLE_BIT)
1317 urb->status = -EOVERFLOW;
1318 else if (FROM_DW3_CERR(ptd->dw3))
1319 urb->status = -EPIPE; /* Stall */
1321 urb->status = -EPROTO; /* Unknown */
1323 dev_dbg(hcd->self.controller, "%s: ptd error:\n"
1324 " dw0: %08x dw1: %08x dw2: %08x dw3: %08x\n"
1325 " dw4: %08x dw5: %08x dw6: %08x dw7: %08x\n",
1327 ptd->dw0, ptd->dw1, ptd->dw2, ptd->dw3,
1328 ptd->dw4, ptd->dw5, ptd->dw6, ptd->dw7);
1330 return PTD_STATE_URB_RETIRE;
1333 if ((ptd->dw3 & DW3_ERROR_BIT) && (ptd->dw3 & DW3_ACTIVE_BIT)) {
1334 /* Transfer Error, *but* active and no HALT -> reload */
1335 dev_dbg(hcd->self.controller, "PID error; reloading ptd\n");
1336 return PTD_STATE_QTD_RELOAD;
1339 if (!FROM_DW3_NAKCOUNT(ptd->dw3) && (ptd->dw3 & DW3_ACTIVE_BIT)) {
1341 * NAKs are handled in HW by the chip. Usually if the
1342 * device is not able to send data fast enough.
1343 * This happens mostly on slower hardware.
1345 return PTD_STATE_QTD_RELOAD;
1348 return PTD_STATE_QTD_DONE;
1351 static void handle_done_ptds(struct usb_hcd *hcd)
1353 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1355 struct isp1760_qh *qh;
1358 struct isp1760_slotinfo *slots;
1360 struct isp1760_qtd *qtd;
1364 skip_map = isp1760_hcd_read(hcd, HC_INT_PTD_SKIPMAP);
1365 priv->int_done_map &= ~skip_map;
1366 skip_map = isp1760_hcd_read(hcd, HC_ATL_PTD_SKIPMAP);
1367 priv->atl_done_map &= ~skip_map;
1369 modified = priv->int_done_map || priv->atl_done_map;
1371 while (priv->int_done_map || priv->atl_done_map) {
1372 if (priv->int_done_map) {
1374 slot = __ffs(priv->int_done_map);
1375 priv->int_done_map &= ~(1 << slot);
1376 slots = priv->int_slots;
1377 /* This should not trigger, and could be removed if
1378 noone have any problems with it triggering: */
1379 if (!slots[slot].qh) {
1383 ptd_offset = INT_PTD_OFFSET;
1384 ptd_read(hcd, INT_PTD_OFFSET, slot, &ptd);
1385 state = check_int_transfer(hcd, &ptd,
1386 slots[slot].qtd->urb);
1389 slot = __ffs(priv->atl_done_map);
1390 priv->atl_done_map &= ~(1 << slot);
1391 slots = priv->atl_slots;
1392 /* This should not trigger, and could be removed if
1393 noone have any problems with it triggering: */
1394 if (!slots[slot].qh) {
1398 ptd_offset = ATL_PTD_OFFSET;
1399 ptd_read(hcd, ATL_PTD_OFFSET, slot, &ptd);
1400 state = check_atl_transfer(hcd, &ptd,
1401 slots[slot].qtd->urb);
1404 qtd = slots[slot].qtd;
1405 slots[slot].qtd = NULL;
1406 qh = slots[slot].qh;
1407 slots[slot].qh = NULL;
1410 WARN_ON(qtd->status != QTD_XFER_STARTED);
1413 case PTD_STATE_QTD_DONE:
1414 if ((usb_pipeint(qtd->urb->pipe)) &&
1415 (qtd->urb->dev->speed != USB_SPEED_HIGH))
1416 qtd->actual_length =
1417 FROM_DW3_SCS_NRBYTESTRANSFERRED(ptd.dw3);
1419 qtd->actual_length =
1420 FROM_DW3_NRBYTESTRANSFERRED(ptd.dw3);
1422 qtd->status = QTD_XFER_COMPLETE;
1423 if (list_is_last(&qtd->qtd_list, &qh->qtd_list) ||
1427 qtd = list_entry(qtd->qtd_list.next,
1428 typeof(*qtd), qtd_list);
1430 qh->toggle = FROM_DW3_DATA_TOGGLE(ptd.dw3);
1431 qh->ping = FROM_DW3_PING(ptd.dw3);
1434 case PTD_STATE_QTD_RELOAD: /* QTD_RETRY, for atls only */
1435 qtd->status = QTD_PAYLOAD_ALLOC;
1436 ptd.dw0 |= DW0_VALID_BIT;
1437 /* RL counter = ERR counter */
1438 ptd.dw3 &= ~TO_DW3_NAKCOUNT(0xf);
1439 ptd.dw3 |= TO_DW3_NAKCOUNT(FROM_DW2_RL(ptd.dw2));
1440 ptd.dw3 &= ~TO_DW3_CERR(3);
1441 ptd.dw3 |= TO_DW3_CERR(ERR_COUNTER);
1442 qh->toggle = FROM_DW3_DATA_TOGGLE(ptd.dw3);
1443 qh->ping = FROM_DW3_PING(ptd.dw3);
1446 case PTD_STATE_URB_RETIRE:
1447 qtd->status = QTD_RETIRE;
1448 if ((qtd->urb->dev->speed != USB_SPEED_HIGH) &&
1449 (qtd->urb->status != -EPIPE) &&
1450 (qtd->urb->status != -EREMOTEIO)) {
1451 qh->tt_buffer_dirty = 1;
1452 if (usb_hub_clear_tt_buffer(qtd->urb))
1453 /* Clear failed; let's hope things work
1455 qh->tt_buffer_dirty = 0;
1467 if (qtd && (qtd->status == QTD_PAYLOAD_ALLOC)) {
1468 if (slots == priv->int_slots) {
1469 if (state == PTD_STATE_QTD_RELOAD)
1470 dev_err(hcd->self.controller,
1471 "%s: PTD_STATE_QTD_RELOAD on "
1472 "interrupt packet\n", __func__);
1473 if (state != PTD_STATE_QTD_RELOAD)
1474 create_ptd_int(qh, qtd, &ptd);
1476 if (state != PTD_STATE_QTD_RELOAD)
1477 create_ptd_atl(qh, qtd, &ptd);
1480 start_bus_transfer(hcd, ptd_offset, slot, slots, qtd,
1489 static irqreturn_t isp1760_irq(struct usb_hcd *hcd)
1491 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1492 irqreturn_t irqret = IRQ_NONE;
1496 spin_lock(&priv->lock);
1498 if (!(hcd->state & HC_STATE_RUNNING))
1501 imask = isp1760_hcd_read(hcd, HC_INTERRUPT);
1502 if (unlikely(!imask))
1505 int_reg = priv->is_isp1763 ? ISP1763_HC_INTERRUPT :
1506 ISP176x_HC_INTERRUPT;
1507 isp1760_reg_write(priv->regs, int_reg, imask);
1509 priv->int_done_map |= isp1760_hcd_read(hcd, HC_INT_PTD_DONEMAP);
1510 priv->atl_done_map |= isp1760_hcd_read(hcd, HC_ATL_PTD_DONEMAP);
1512 handle_done_ptds(hcd);
1514 irqret = IRQ_HANDLED;
1517 spin_unlock(&priv->lock);
1523 * Workaround for problem described in chip errata 2:
1525 * Sometimes interrupts are not generated when ATL (not INT?) completion occurs.
1526 * One solution suggested in the errata is to use SOF interrupts _instead_of_
1527 * ATL done interrupts (the "instead of" might be important since it seems
1528 * enabling ATL interrupts also causes the chip to sometimes - rarely - "forget"
1529 * to set the PTD's done bit in addition to not generating an interrupt!).
1531 * So if we use SOF + ATL interrupts, we sometimes get stale PTDs since their
1532 * done bit is not being set. This is bad - it blocks the endpoint until reboot.
1534 * If we use SOF interrupts only, we get latency between ptd completion and the
1535 * actual handling. This is very noticeable in testusb runs which takes several
1536 * minutes longer without ATL interrupts.
1538 * A better solution is to run the code below every SLOT_CHECK_PERIOD ms. If it
1539 * finds active ATL slots which are older than SLOT_TIMEOUT ms, it checks the
1540 * slot's ACTIVE and VALID bits. If these are not set, the ptd is considered
1541 * completed and its done map bit is set.
1543 * The values of SLOT_TIMEOUT and SLOT_CHECK_PERIOD have been arbitrarily chosen
1544 * not to cause too much lag when this HW bug occurs, while still hopefully
1545 * ensuring that the check does not falsely trigger.
1547 #define SLOT_TIMEOUT 300
1548 #define SLOT_CHECK_PERIOD 200
1549 static struct timer_list errata2_timer;
1550 static struct usb_hcd *errata2_timer_hcd;
1552 static void errata2_function(struct timer_list *unused)
1554 struct usb_hcd *hcd = errata2_timer_hcd;
1555 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1556 const struct isp1760_memory_layout *mem = priv->memory_layout;
1559 unsigned long spinflags;
1561 spin_lock_irqsave(&priv->lock, spinflags);
1563 for (slot = 0; slot < mem->slot_num; slot++)
1564 if (priv->atl_slots[slot].qh && time_after(jiffies,
1565 priv->atl_slots[slot].timestamp +
1566 msecs_to_jiffies(SLOT_TIMEOUT))) {
1567 ptd_read(hcd, ATL_PTD_OFFSET, slot, &ptd);
1568 if (!FROM_DW0_VALID(ptd.dw0) &&
1569 !FROM_DW3_ACTIVE(ptd.dw3))
1570 priv->atl_done_map |= 1 << slot;
1573 if (priv->atl_done_map)
1574 handle_done_ptds(hcd);
1576 spin_unlock_irqrestore(&priv->lock, spinflags);
1578 errata2_timer.expires = jiffies + msecs_to_jiffies(SLOT_CHECK_PERIOD);
1579 add_timer(&errata2_timer);
1582 static int isp1763_run(struct usb_hcd *hcd)
1584 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1592 hcd->uses_new_polling = 1;
1593 hcd->state = HC_STATE_RUNNING;
1595 chipid_h = isp1760_hcd_read(hcd, HC_CHIP_ID_HIGH);
1596 chipid_l = isp1760_hcd_read(hcd, HC_CHIP_ID_LOW);
1597 chip_rev = isp1760_hcd_read(hcd, HC_CHIP_REV);
1598 dev_info(hcd->self.controller, "USB ISP %02x%02x HW rev. %d started\n",
1599 chipid_h, chipid_l, chip_rev);
1601 isp1760_hcd_clear(hcd, ISO_BUF_FILL);
1602 isp1760_hcd_clear(hcd, INT_BUF_FILL);
1603 isp1760_hcd_clear(hcd, ATL_BUF_FILL);
1605 isp1760_hcd_set(hcd, HC_ATL_PTD_SKIPMAP);
1606 isp1760_hcd_set(hcd, HC_INT_PTD_SKIPMAP);
1607 isp1760_hcd_set(hcd, HC_ISO_PTD_SKIPMAP);
1609 isp1760_hcd_clear(hcd, HC_ATL_PTD_DONEMAP);
1610 isp1760_hcd_clear(hcd, HC_INT_PTD_DONEMAP);
1611 isp1760_hcd_clear(hcd, HC_ISO_PTD_DONEMAP);
1613 isp1760_hcd_set(hcd, HW_OTG_DISABLE);
1614 isp1760_reg_write(priv->regs, ISP1763_HC_OTG_CTRL_CLEAR, BIT(7));
1615 isp1760_reg_write(priv->regs, ISP1763_HC_OTG_CTRL_CLEAR, BIT(15));
1618 isp1760_hcd_set(hcd, HC_INT_IRQ_ENABLE);
1619 isp1760_hcd_set(hcd, HC_ATL_IRQ_ENABLE);
1621 isp1760_hcd_set(hcd, HW_GLOBAL_INTR_EN);
1623 isp1760_hcd_clear(hcd, HC_ATL_IRQ_MASK_AND);
1624 isp1760_hcd_clear(hcd, HC_INT_IRQ_MASK_AND);
1625 isp1760_hcd_clear(hcd, HC_ISO_IRQ_MASK_AND);
1627 isp1760_hcd_set(hcd, HC_ATL_IRQ_MASK_OR);
1628 isp1760_hcd_set(hcd, HC_INT_IRQ_MASK_OR);
1629 isp1760_hcd_set(hcd, HC_ISO_IRQ_MASK_OR);
1631 ptd_atl_int = 0x8000;
1634 isp1760_hcd_write(hcd, HC_ATL_PTD_LASTPTD, ptd_atl_int);
1635 isp1760_hcd_write(hcd, HC_INT_PTD_LASTPTD, ptd_atl_int);
1636 isp1760_hcd_write(hcd, HC_ISO_PTD_LASTPTD, ptd_iso);
1638 isp1760_hcd_set(hcd, ATL_BUF_FILL);
1639 isp1760_hcd_set(hcd, INT_BUF_FILL);
1641 isp1760_hcd_clear(hcd, CMD_LRESET);
1642 isp1760_hcd_clear(hcd, CMD_RESET);
1644 retval = isp1760_hcd_set_and_wait(hcd, CMD_RUN, 250 * 1000);
1648 down_write(&ehci_cf_port_reset_rwsem);
1649 retval = isp1760_hcd_set_and_wait(hcd, FLAG_CF, 250 * 1000);
1650 up_write(&ehci_cf_port_reset_rwsem);
1657 static int isp1760_run(struct usb_hcd *hcd)
1659 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1668 * ISP1763 have some differences in the setup and order to enable
1669 * the ports, disable otg, setup buffers, and ATL, INT, ISO status.
1670 * So, just handle it a separate sequence.
1672 if (priv->is_isp1763)
1673 return isp1763_run(hcd);
1675 hcd->uses_new_polling = 1;
1677 hcd->state = HC_STATE_RUNNING;
1679 /* Set PTD interrupt AND & OR maps */
1680 isp1760_hcd_clear(hcd, HC_ATL_IRQ_MASK_AND);
1681 isp1760_hcd_clear(hcd, HC_INT_IRQ_MASK_AND);
1682 isp1760_hcd_clear(hcd, HC_ISO_IRQ_MASK_AND);
1684 isp1760_hcd_set(hcd, HC_ATL_IRQ_MASK_OR);
1685 isp1760_hcd_set(hcd, HC_INT_IRQ_MASK_OR);
1686 isp1760_hcd_set(hcd, HC_ISO_IRQ_MASK_OR);
1688 /* step 23 passed */
1690 isp1760_hcd_set(hcd, HW_GLOBAL_INTR_EN);
1692 isp1760_hcd_clear(hcd, CMD_LRESET);
1693 isp1760_hcd_clear(hcd, CMD_RESET);
1695 retval = isp1760_hcd_set_and_wait(hcd, CMD_RUN, 250 * 1000);
1701 * Spec says to write FLAG_CF as last config action, priv code grabs
1702 * the semaphore while doing so.
1704 down_write(&ehci_cf_port_reset_rwsem);
1706 retval = isp1760_hcd_set_and_wait(hcd, FLAG_CF, 250 * 1000);
1707 up_write(&ehci_cf_port_reset_rwsem);
1711 errata2_timer_hcd = hcd;
1712 timer_setup(&errata2_timer, errata2_function, 0);
1713 errata2_timer.expires = jiffies + msecs_to_jiffies(SLOT_CHECK_PERIOD);
1714 add_timer(&errata2_timer);
1716 chipid_h = isp1760_hcd_read(hcd, HC_CHIP_ID_HIGH);
1717 chipid_l = isp1760_hcd_read(hcd, HC_CHIP_ID_LOW);
1718 chip_rev = isp1760_hcd_read(hcd, HC_CHIP_REV);
1719 dev_info(hcd->self.controller, "USB ISP %02x%02x HW rev. %d started\n",
1720 chipid_h, chipid_l, chip_rev);
1722 /* PTD Register Init Part 2, Step 28 */
1724 /* Setup registers controlling PTD checking */
1725 ptd_atl_int = 0x80000000;
1726 ptd_iso = 0x00000001;
1728 isp1760_hcd_write(hcd, HC_ATL_PTD_LASTPTD, ptd_atl_int);
1729 isp1760_hcd_write(hcd, HC_INT_PTD_LASTPTD, ptd_atl_int);
1730 isp1760_hcd_write(hcd, HC_ISO_PTD_LASTPTD, ptd_iso);
1732 isp1760_hcd_set(hcd, HC_ATL_PTD_SKIPMAP);
1733 isp1760_hcd_set(hcd, HC_INT_PTD_SKIPMAP);
1734 isp1760_hcd_set(hcd, HC_ISO_PTD_SKIPMAP);
1736 isp1760_hcd_set(hcd, ATL_BUF_FILL);
1737 isp1760_hcd_set(hcd, INT_BUF_FILL);
1739 /* GRR this is run-once init(), being done every time the HC starts.
1740 * So long as they're part of class devices, we can't do it init()
1741 * since the class device isn't created that early.
1746 static int qtd_fill(struct isp1760_qtd *qtd, void *databuffer, size_t len)
1748 qtd->data_buffer = databuffer;
1755 static void qtd_list_free(struct list_head *qtd_list)
1757 struct isp1760_qtd *qtd, *qtd_next;
1759 list_for_each_entry_safe(qtd, qtd_next, qtd_list, qtd_list) {
1760 list_del(&qtd->qtd_list);
1766 * Packetize urb->transfer_buffer into list of packets of size wMaxPacketSize.
1767 * Also calculate the PID type (SETUP/IN/OUT) for each packet.
1769 #define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
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 = max_packet(usb_maxpacket(urb->dev, urb->pipe,
1811 usb_pipeout(urb->pipe)));
1814 * buffer gets wrapped in one or more qtds;
1815 * last one may be "short" (including zero len)
1816 * and may serve as a control status ack
1818 buf = urb->transfer_buffer;
1819 len = urb->transfer_buffer_length;
1824 qtd = qtd_alloc(flags, urb, packet_type);
1828 if (len > mem->blocks_size[ISP176x_BLOCK_NUM - 1])
1829 len = mem->blocks_size[ISP176x_BLOCK_NUM - 1];
1831 this_qtd_len = qtd_fill(qtd, buf, len);
1832 list_add_tail(&qtd->qtd_list, head);
1834 len -= this_qtd_len;
1835 buf += this_qtd_len;
1842 * control requests may need a terminating data "status" ack;
1843 * bulk ones may need a terminating short packet (zero length).
1845 if (urb->transfer_buffer_length != 0) {
1848 if (usb_pipecontrol(urb->pipe)) {
1850 if (packet_type == IN_PID)
1851 packet_type = OUT_PID;
1853 packet_type = IN_PID;
1854 } else if (usb_pipebulk(urb->pipe)
1855 && (urb->transfer_flags & URB_ZERO_PACKET)
1856 && !(urb->transfer_buffer_length %
1861 qtd = qtd_alloc(flags, urb, packet_type);
1865 /* never any data in such packets */
1866 qtd_fill(qtd, NULL, 0);
1867 list_add_tail(&qtd->qtd_list, head);
1874 qtd_list_free(head);
1877 static int isp1760_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
1880 struct isp1760_hcd *priv = hcd_to_priv(hcd);
1881 struct list_head *ep_queue;
1882 struct isp1760_qh *qh, *qhit;
1883 unsigned long spinflags;
1884 LIST_HEAD(new_qtds);
1888 switch (usb_pipetype(urb->pipe)) {
1890 ep_queue = &priv->qh_list[QH_CONTROL];
1893 ep_queue = &priv->qh_list[QH_BULK];
1895 case PIPE_INTERRUPT:
1896 if (urb->interval < 0)
1898 /* FIXME: Check bandwidth */
1899 ep_queue = &priv->qh_list[QH_INTERRUPT];
1901 case PIPE_ISOCHRONOUS:
1902 dev_err(hcd->self.controller, "%s: isochronous USB packets "
1903 "not yet supported\n",
1907 dev_err(hcd->self.controller, "%s: unknown pipe type\n",
1912 if (usb_pipein(urb->pipe))
1913 urb->actual_length = 0;
1915 packetize_urb(hcd, urb, &new_qtds, mem_flags);
1916 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 skip_map = isp1760_hcd_read(hcd, HC_ATL_PTD_SKIPMAP);
1976 skip_map |= (1 << qh->slot);
1977 isp1760_hcd_write(hcd, HC_ATL_PTD_SKIPMAP, skip_map);
1979 priv->atl_slots[qh->slot].qh = NULL;
1980 priv->atl_slots[qh->slot].qtd = NULL;
1982 skip_map = isp1760_hcd_read(hcd, HC_INT_PTD_SKIPMAP);
1983 skip_map |= (1 << qh->slot);
1984 isp1760_hcd_write(hcd, HC_INT_PTD_SKIPMAP, skip_map);
1985 priv->int_slots[qh->slot].qh = NULL;
1986 priv->int_slots[qh->slot].qtd = NULL;
1993 * Retire the qtds beginning at 'qtd' and belonging all to the same urb, killing
1994 * any active transfer belonging to the urb in the process.
1996 static void dequeue_urb_from_qtd(struct usb_hcd *hcd, struct isp1760_qh *qh,
1997 struct isp1760_qtd *qtd)
2000 int urb_was_running;
2003 urb_was_running = 0;
2004 list_for_each_entry_from(qtd, &qh->qtd_list, qtd_list) {
2005 if (qtd->urb != urb)
2008 if (qtd->status >= QTD_XFER_STARTED)
2009 urb_was_running = 1;
2010 if (last_qtd_of_urb(qtd, qh) &&
2011 (qtd->status >= QTD_XFER_COMPLETE))
2012 urb_was_running = 0;
2014 if (qtd->status == QTD_XFER_STARTED)
2015 kill_transfer(hcd, urb, qh);
2016 qtd->status = QTD_RETIRE;
2019 if ((urb->dev->speed != USB_SPEED_HIGH) && urb_was_running) {
2020 qh->tt_buffer_dirty = 1;
2021 if (usb_hub_clear_tt_buffer(urb))
2022 /* Clear failed; let's hope things work anyway */
2023 qh->tt_buffer_dirty = 0;
2027 static int isp1760_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
2030 struct isp1760_hcd *priv = hcd_to_priv(hcd);
2031 unsigned long spinflags;
2032 struct isp1760_qh *qh;
2033 struct isp1760_qtd *qtd;
2036 spin_lock_irqsave(&priv->lock, spinflags);
2037 retval = usb_hcd_check_unlink_urb(hcd, urb, status);
2041 qh = urb->ep->hcpriv;
2047 list_for_each_entry(qtd, &qh->qtd_list, qtd_list)
2048 if (qtd->urb == urb) {
2049 dequeue_urb_from_qtd(hcd, qh, qtd);
2050 list_move(&qtd->qtd_list, &qh->qtd_list);
2054 urb->status = status;
2058 spin_unlock_irqrestore(&priv->lock, spinflags);
2062 static void isp1760_endpoint_disable(struct usb_hcd *hcd,
2063 struct usb_host_endpoint *ep)
2065 struct isp1760_hcd *priv = hcd_to_priv(hcd);
2066 unsigned long spinflags;
2067 struct isp1760_qh *qh, *qh_iter;
2070 spin_lock_irqsave(&priv->lock, spinflags);
2076 WARN_ON(!list_empty(&qh->qtd_list));
2078 for (i = 0; i < QH_END; i++)
2079 list_for_each_entry(qh_iter, &priv->qh_list[i], qh_list)
2080 if (qh_iter == qh) {
2081 list_del(&qh_iter->qh_list);
2091 spin_unlock_irqrestore(&priv->lock, spinflags);
2094 static int isp1760_hub_status_data(struct usb_hcd *hcd, char *buf)
2096 struct isp1760_hcd *priv = hcd_to_priv(hcd);
2099 unsigned long flags;
2101 /* if !PM, root hub timers won't get shut down ... */
2102 if (!HC_IS_RUNNING(hcd->state))
2105 /* init status to no-changes */
2108 spin_lock_irqsave(&priv->lock, flags);
2110 if (isp1760_hcd_is_set(hcd, PORT_OWNER) &&
2111 isp1760_hcd_is_set(hcd, PORT_CSC)) {
2112 isp1760_hcd_clear(hcd, PORT_CSC);
2117 * Return status information even for ports with OWNER set.
2118 * Otherwise hub_wq wouldn't see the disconnect event when a
2119 * high-speed device is switched over to the companion
2120 * controller by the user.
2122 if (isp1760_hcd_is_set(hcd, PORT_CSC) ||
2123 (isp1760_hcd_is_set(hcd, PORT_RESUME) &&
2124 time_after_eq(jiffies, priv->reset_done))) {
2125 buf [0] |= 1 << (0 + 1);
2128 /* FIXME autosuspend idle root hubs */
2130 spin_unlock_irqrestore(&priv->lock, flags);
2131 return status ? retval : 0;
2134 static void isp1760_hub_descriptor(struct isp1760_hcd *priv,
2135 struct usb_hub_descriptor *desc)
2140 ports = isp1760_hcd_n_ports(priv->hcd);
2142 desc->bDescriptorType = USB_DT_HUB;
2143 /* priv 1.0, 2.3.9 says 20ms max */
2144 desc->bPwrOn2PwrGood = 10;
2145 desc->bHubContrCurrent = 0;
2147 desc->bNbrPorts = ports;
2148 temp = 1 + (ports / 8);
2149 desc->bDescLength = 7 + 2 * temp;
2151 /* ports removable, and usb 1.0 legacy PortPwrCtrlMask */
2152 memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
2153 memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
2155 /* per-port overcurrent reporting */
2156 temp = HUB_CHAR_INDV_PORT_OCPM;
2157 if (isp1760_hcd_ppc_is_set(priv->hcd))
2158 /* per-port power control */
2159 temp |= HUB_CHAR_INDV_PORT_LPSM;
2161 /* no power switching */
2162 temp |= HUB_CHAR_NO_LPSM;
2163 desc->wHubCharacteristics = cpu_to_le16(temp);
2166 #define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
2168 static void check_reset_complete(struct usb_hcd *hcd, int index)
2170 if (!(isp1760_hcd_is_set(hcd, PORT_CONNECT)))
2173 /* if reset finished and it's still not enabled -- handoff */
2174 if (!isp1760_hcd_is_set(hcd, PORT_PE)) {
2175 dev_info(hcd->self.controller,
2176 "port %d full speed --> companion\n", index + 1);
2178 isp1760_hcd_set(hcd, PORT_OWNER);
2180 isp1760_hcd_clear(hcd, PORT_CSC);
2182 dev_info(hcd->self.controller, "port %d high speed\n",
2189 static int isp1760_hub_control(struct usb_hcd *hcd, u16 typeReq,
2190 u16 wValue, u16 wIndex, char *buf, u16 wLength)
2192 struct isp1760_hcd *priv = hcd_to_priv(hcd);
2194 unsigned long flags;
2198 ports = isp1760_hcd_n_ports(hcd);
2201 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
2202 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
2203 * (track current state ourselves) ... blink for diagnostics,
2204 * power, "this is the one", etc. EHCI spec supports this.
2207 spin_lock_irqsave(&priv->lock, flags);
2209 case ClearHubFeature:
2211 case C_HUB_LOCAL_POWER:
2212 case C_HUB_OVER_CURRENT:
2213 /* no hub-wide feature/status flags */
2219 case ClearPortFeature:
2220 if (!wIndex || wIndex > ports)
2225 * Even if OWNER is set, so the port is owned by the
2226 * companion controller, hub_wq needs to be able to clear
2227 * the port-change status bits (especially
2228 * USB_PORT_STAT_C_CONNECTION).
2232 case USB_PORT_FEAT_ENABLE:
2233 isp1760_hcd_clear(hcd, PORT_PE);
2235 case USB_PORT_FEAT_C_ENABLE:
2238 case USB_PORT_FEAT_SUSPEND:
2239 if (isp1760_hcd_is_set(hcd, PORT_RESET))
2242 if (isp1760_hcd_is_set(hcd, PORT_SUSPEND)) {
2243 if (!isp1760_hcd_is_set(hcd, PORT_PE))
2245 /* resume signaling for 20 msec */
2246 isp1760_hcd_clear(hcd, PORT_CSC);
2247 isp1760_hcd_set(hcd, PORT_RESUME);
2249 priv->reset_done = jiffies +
2250 msecs_to_jiffies(USB_RESUME_TIMEOUT);
2253 case USB_PORT_FEAT_C_SUSPEND:
2254 /* we auto-clear this feature */
2256 case USB_PORT_FEAT_POWER:
2257 if (isp1760_hcd_ppc_is_set(hcd))
2258 isp1760_hcd_clear(hcd, PORT_POWER);
2260 case USB_PORT_FEAT_C_CONNECTION:
2261 isp1760_hcd_set(hcd, PORT_CSC);
2263 case USB_PORT_FEAT_C_OVER_CURRENT:
2266 case USB_PORT_FEAT_C_RESET:
2267 /* GetPortStatus clears reset */
2272 isp1760_hcd_read(hcd, CMD_RUN);
2274 case GetHubDescriptor:
2275 isp1760_hub_descriptor(priv, (struct usb_hub_descriptor *)
2279 /* no hub-wide feature/status flags */
2283 if (!wIndex || wIndex > ports)
2288 /* wPortChange bits */
2289 if (isp1760_hcd_is_set(hcd, PORT_CSC))
2290 status |= USB_PORT_STAT_C_CONNECTION << 16;
2292 /* whoever resumes must GetPortStatus to complete it!! */
2293 if (isp1760_hcd_is_set(hcd, PORT_RESUME)) {
2294 dev_err(hcd->self.controller, "Port resume should be skipped.\n");
2296 /* Remote Wakeup received? */
2297 if (!priv->reset_done) {
2298 /* resume signaling for 20 msec */
2299 priv->reset_done = jiffies
2300 + msecs_to_jiffies(20);
2301 /* check the port again */
2302 mod_timer(&hcd->rh_timer, priv->reset_done);
2305 /* resume completed? */
2306 else if (time_after_eq(jiffies,
2307 priv->reset_done)) {
2308 status |= USB_PORT_STAT_C_SUSPEND << 16;
2309 priv->reset_done = 0;
2311 /* stop resume signaling */
2312 isp1760_hcd_clear(hcd, PORT_CSC);
2314 retval = isp1760_hcd_clear_and_wait(hcd,
2317 dev_err(hcd->self.controller,
2318 "port %d resume error %d\n",
2319 wIndex + 1, retval);
2325 /* whoever resets must GetPortStatus to complete it!! */
2326 if (isp1760_hcd_is_set(hcd, PORT_RESET) &&
2327 time_after_eq(jiffies, priv->reset_done)) {
2328 status |= USB_PORT_STAT_C_RESET << 16;
2329 priv->reset_done = 0;
2331 /* force reset to complete */
2332 /* REVISIT: some hardware needs 550+ usec to clear
2333 * this bit; seems too long to spin routinely...
2335 retval = isp1760_hcd_clear_and_wait(hcd, PORT_RESET,
2338 dev_err(hcd->self.controller, "port %d reset error %d\n",
2339 wIndex + 1, retval);
2343 /* see what we found out */
2344 check_reset_complete(hcd, wIndex);
2347 * Even if OWNER is set, there's no harm letting hub_wq
2348 * see the wPortStatus values (they should all be 0 except
2349 * for PORT_POWER anyway).
2352 if (isp1760_hcd_is_set(hcd, PORT_OWNER))
2353 dev_err(hcd->self.controller, "PORT_OWNER is set\n");
2355 if (isp1760_hcd_is_set(hcd, PORT_CONNECT)) {
2356 status |= USB_PORT_STAT_CONNECTION;
2357 /* status may be from integrated TT */
2358 status |= USB_PORT_STAT_HIGH_SPEED;
2360 if (isp1760_hcd_is_set(hcd, PORT_PE))
2361 status |= USB_PORT_STAT_ENABLE;
2362 if (isp1760_hcd_is_set(hcd, PORT_SUSPEND) &&
2363 isp1760_hcd_is_set(hcd, PORT_RESUME))
2364 status |= USB_PORT_STAT_SUSPEND;
2365 if (isp1760_hcd_is_set(hcd, PORT_RESET))
2366 status |= USB_PORT_STAT_RESET;
2367 if (isp1760_hcd_is_set(hcd, PORT_POWER))
2368 status |= USB_PORT_STAT_POWER;
2370 put_unaligned(cpu_to_le32(status), (__le32 *) buf);
2374 case C_HUB_LOCAL_POWER:
2375 case C_HUB_OVER_CURRENT:
2376 /* no hub-wide feature/status flags */
2382 case SetPortFeature:
2384 if (!wIndex || wIndex > ports)
2388 if (isp1760_hcd_is_set(hcd, PORT_OWNER))
2392 case USB_PORT_FEAT_ENABLE:
2393 isp1760_hcd_set(hcd, PORT_PE);
2396 case USB_PORT_FEAT_SUSPEND:
2397 if (!isp1760_hcd_is_set(hcd, PORT_PE) ||
2398 isp1760_hcd_is_set(hcd, PORT_RESET))
2401 isp1760_hcd_set(hcd, PORT_SUSPEND);
2403 case USB_PORT_FEAT_POWER:
2404 if (isp1760_hcd_ppc_is_set(hcd))
2405 isp1760_hcd_set(hcd, PORT_POWER);
2407 case USB_PORT_FEAT_RESET:
2408 if (isp1760_hcd_is_set(hcd, PORT_RESUME))
2410 /* line status bits may report this as low speed,
2411 * which can be fine if this root hub has a
2412 * transaction translator built in.
2414 if ((isp1760_hcd_is_set(hcd, PORT_CONNECT) &&
2415 !isp1760_hcd_is_set(hcd, PORT_PE)) &&
2416 (isp1760_hcd_read(hcd, PORT_LSTATUS) == 1)) {
2417 isp1760_hcd_set(hcd, PORT_OWNER);
2419 isp1760_hcd_set(hcd, PORT_RESET);
2420 isp1760_hcd_clear(hcd, PORT_PE);
2423 * caller must wait, then call GetPortStatus
2424 * usb 2.0 spec says 50 ms resets on root
2426 priv->reset_done = jiffies +
2427 msecs_to_jiffies(50);
2437 /* "stall" on error */
2440 spin_unlock_irqrestore(&priv->lock, flags);
2444 static int isp1760_get_frame(struct usb_hcd *hcd)
2446 struct isp1760_hcd *priv = hcd_to_priv(hcd);
2449 fr = isp1760_hcd_read(hcd, HC_FRINDEX);
2450 return (fr >> 3) % priv->periodic_size;
2453 static void isp1760_stop(struct usb_hcd *hcd)
2455 struct isp1760_hcd *priv = hcd_to_priv(hcd);
2457 del_timer(&errata2_timer);
2459 isp1760_hub_control(hcd, ClearPortFeature, USB_PORT_FEAT_POWER, 1,
2463 spin_lock_irq(&priv->lock);
2466 isp1760_hcd_clear(hcd, HW_GLOBAL_INTR_EN);
2467 spin_unlock_irq(&priv->lock);
2469 isp1760_hcd_clear(hcd, FLAG_CF);
2472 static void isp1760_shutdown(struct usb_hcd *hcd)
2476 isp1760_hcd_clear(hcd, HW_GLOBAL_INTR_EN);
2478 isp1760_hcd_clear(hcd, CMD_RUN);
2481 static void isp1760_clear_tt_buffer_complete(struct usb_hcd *hcd,
2482 struct usb_host_endpoint *ep)
2484 struct isp1760_hcd *priv = hcd_to_priv(hcd);
2485 struct isp1760_qh *qh = ep->hcpriv;
2486 unsigned long spinflags;
2491 spin_lock_irqsave(&priv->lock, spinflags);
2492 qh->tt_buffer_dirty = 0;
2494 spin_unlock_irqrestore(&priv->lock, spinflags);
2498 static const struct hc_driver isp1760_hc_driver = {
2499 .description = "isp1760-hcd",
2500 .product_desc = "NXP ISP1760 USB Host Controller",
2501 .hcd_priv_size = sizeof(struct isp1760_hcd *),
2503 .flags = HCD_MEMORY | HCD_USB2,
2504 .reset = isp1760_hc_setup,
2505 .start = isp1760_run,
2506 .stop = isp1760_stop,
2507 .shutdown = isp1760_shutdown,
2508 .urb_enqueue = isp1760_urb_enqueue,
2509 .urb_dequeue = isp1760_urb_dequeue,
2510 .endpoint_disable = isp1760_endpoint_disable,
2511 .get_frame_number = isp1760_get_frame,
2512 .hub_status_data = isp1760_hub_status_data,
2513 .hub_control = isp1760_hub_control,
2514 .clear_tt_buffer_complete = isp1760_clear_tt_buffer_complete,
2517 int __init isp1760_init_kmem_once(void)
2519 urb_listitem_cachep = kmem_cache_create("isp1760_urb_listitem",
2520 sizeof(struct urb_listitem), 0, SLAB_TEMPORARY |
2521 SLAB_MEM_SPREAD, NULL);
2523 if (!urb_listitem_cachep)
2526 qtd_cachep = kmem_cache_create("isp1760_qtd",
2527 sizeof(struct isp1760_qtd), 0, SLAB_TEMPORARY |
2528 SLAB_MEM_SPREAD, NULL);
2533 qh_cachep = kmem_cache_create("isp1760_qh", sizeof(struct isp1760_qh),
2534 0, SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
2537 kmem_cache_destroy(qtd_cachep);
2544 void isp1760_deinit_kmem_cache(void)
2546 kmem_cache_destroy(qtd_cachep);
2547 kmem_cache_destroy(qh_cachep);
2548 kmem_cache_destroy(urb_listitem_cachep);
2551 int isp1760_hcd_register(struct isp1760_hcd *priv, struct resource *mem,
2552 int irq, unsigned long irqflags,
2555 const struct isp1760_memory_layout *mem_layout = priv->memory_layout;
2556 struct usb_hcd *hcd;
2559 hcd = usb_create_hcd(&isp1760_hc_driver, dev, dev_name(dev));
2563 *(struct isp1760_hcd **)hcd->hcd_priv = priv;
2567 priv->atl_slots = kcalloc(mem_layout->slot_num,
2568 sizeof(struct isp1760_slotinfo), GFP_KERNEL);
2569 if (!priv->atl_slots) {
2574 priv->int_slots = kcalloc(mem_layout->slot_num,
2575 sizeof(struct isp1760_slotinfo), GFP_KERNEL);
2576 if (!priv->int_slots) {
2578 goto free_atl_slots;
2584 hcd->rsrc_start = mem->start;
2585 hcd->rsrc_len = resource_size(mem);
2587 /* This driver doesn't support wakeup requests */
2588 hcd->cant_recv_wakeups = 1;
2590 ret = usb_add_hcd(hcd, irq, irqflags);
2592 goto free_int_slots;
2594 device_wakeup_enable(hcd->self.controller);
2599 kfree(priv->int_slots);
2601 kfree(priv->atl_slots);
2607 void isp1760_hcd_unregister(struct isp1760_hcd *priv)
2612 usb_remove_hcd(priv->hcd);
2613 usb_put_hcd(priv->hcd);
2614 kfree(priv->atl_slots);
2615 kfree(priv->int_slots);