2 * Driver for the NXP ISP1760 chip
4 * However, the code might contain some bugs. What doesn't work for sure is:
7 e The interrupt line is configured as active low, level.
9 * (c) 2007 Sebastian Siewior <bigeasy@linutronix.de>
11 * (c) 2011 Arvid Brodin <arvid.brodin@enea.com>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
17 #include <linux/list.h>
18 #include <linux/usb.h>
19 #include <linux/usb/hcd.h>
20 #include <linux/debugfs.h>
21 #include <linux/uaccess.h>
24 #include <asm/unaligned.h>
25 #include <asm/cacheflush.h>
27 #include "isp1760-hcd.h"
29 static struct kmem_cache
*qtd_cachep
;
30 static struct kmem_cache
*qh_cachep
;
31 static struct kmem_cache
*urb_listitem_cachep
;
36 struct slotinfo atl_slots
[32];
38 struct slotinfo int_slots
[32];
40 struct memory_chunk memory_pool
[BLOCKS
];
41 struct list_head controlqhs
, bulkqhs
, interruptqhs
;
44 /* periodic schedule support */
45 #define DEFAULT_I_TDPS 1024
46 unsigned periodic_size
;
48 unsigned long reset_done
;
49 unsigned long next_statechange
;
50 unsigned int devflags
;
53 static inline struct isp1760_hcd
*hcd_to_priv(struct usb_hcd
*hcd
)
55 return (struct isp1760_hcd
*) (hcd
->hcd_priv
);
58 /* Section 2.2 Host Controller Capability Registers */
59 #define HC_LENGTH(p) (((p)>>00)&0x00ff) /* bits 7:0 */
60 #define HC_VERSION(p) (((p)>>16)&0xffff) /* bits 31:16 */
61 #define HCS_INDICATOR(p) ((p)&(1 << 16)) /* true: has port indicators */
62 #define HCS_PPC(p) ((p)&(1 << 4)) /* true: port power control */
63 #define HCS_N_PORTS(p) (((p)>>0)&0xf) /* bits 3:0, ports on HC */
64 #define HCC_ISOC_CACHE(p) ((p)&(1 << 7)) /* true: can cache isoc frame */
65 #define HCC_ISOC_THRES(p) (((p)>>4)&0x7) /* bits 6:4, uframes cached */
67 /* Section 2.3 Host Controller Operational Registers */
68 #define CMD_LRESET (1<<7) /* partial reset (no ports, etc) */
69 #define CMD_RESET (1<<1) /* reset HC not bus */
70 #define CMD_RUN (1<<0) /* start/stop HC */
71 #define STS_PCD (1<<2) /* port change detect */
72 #define FLAG_CF (1<<0) /* true: we'll support "high speed" */
74 #define PORT_OWNER (1<<13) /* true: companion hc owns this port */
75 #define PORT_POWER (1<<12) /* true: has power (see PPC) */
76 #define PORT_USB11(x) (((x) & (3 << 10)) == (1 << 10)) /* USB 1.1 device */
77 #define PORT_RESET (1<<8) /* reset port */
78 #define PORT_SUSPEND (1<<7) /* suspend port */
79 #define PORT_RESUME (1<<6) /* resume it */
80 #define PORT_PE (1<<2) /* port enable */
81 #define PORT_CSC (1<<1) /* connect status change */
82 #define PORT_CONNECT (1<<0) /* device connected */
83 #define PORT_RWC_BITS (PORT_CSC)
90 /* the rest is HCD-private */
91 struct list_head qtd_list
;
96 /* QTD_ENQUEUED: waiting for transfer (inactive) */
97 /* QTD_PAYLOAD_ALLOC: chip mem has been allocated for payload */
98 /* QTD_XFER_STARTED: valid ptd has been written to isp176x - only
99 interrupt handler may touch this qtd! */
100 /* QTD_XFER_COMPLETE: payload has been transferred successfully */
101 /* QTD_RETIRE: transfer error/abort qtd */
102 #define QTD_ENQUEUED 0
103 #define QTD_PAYLOAD_ALLOC 1
104 #define QTD_XFER_STARTED 2
105 #define QTD_XFER_COMPLETE 3
110 /* Queue head, one for each active endpoint */
112 struct list_head qh_list
;
113 struct list_head qtd_list
;
119 struct urb_listitem
{
120 struct list_head urb_list
;
125 * Access functions for isp176x registers (addresses 0..0x03FF).
127 static u32
reg_read32(void __iomem
*base
, u32 reg
)
129 return readl(base
+ reg
);
132 static void reg_write32(void __iomem
*base
, u32 reg
, u32 val
)
134 writel(val
, base
+ reg
);
138 * Access functions for isp176x memory (offset >= 0x0400).
140 * bank_reads8() reads memory locations prefetched by an earlier write to
141 * HC_MEMORY_REG (see isp176x datasheet). Unless you want to do fancy multi-
142 * bank optimizations, you should use the more generic mem_reads8() below.
144 * For access to ptd memory, use the specialized ptd_read() and ptd_write()
147 * These functions copy via MMIO data to/from the device. memcpy_{to|from}io()
148 * doesn't quite work because some people have to enforce 32-bit access
150 static void bank_reads8(void __iomem
*src_base
, u32 src_offset
, u32 bank_addr
,
151 __u32
*dst
, u32 bytes
)
158 src
= src_base
+ (bank_addr
| src_offset
);
160 if (src_offset
< PAYLOAD_OFFSET
) {
162 *dst
= le32_to_cpu(__raw_readl(src
));
169 *dst
= __raw_readl(src
);
179 /* in case we have 3, 2 or 1 by left. The dst buffer may not be fully
182 if (src_offset
< PAYLOAD_OFFSET
)
183 val
= le32_to_cpu(__raw_readl(src
));
185 val
= __raw_readl(src
);
187 dst_byteptr
= (void *) dst
;
188 src_byteptr
= (void *) &val
;
190 *dst_byteptr
= *src_byteptr
;
197 static void mem_reads8(void __iomem
*src_base
, u32 src_offset
, void *dst
,
200 reg_write32(src_base
, HC_MEMORY_REG
, src_offset
+ ISP_BANK(0));
202 bank_reads8(src_base
, src_offset
, ISP_BANK(0), dst
, bytes
);
205 static void mem_writes8(void __iomem
*dst_base
, u32 dst_offset
,
206 __u32
const *src
, u32 bytes
)
210 dst
= dst_base
+ dst_offset
;
212 if (dst_offset
< PAYLOAD_OFFSET
) {
214 __raw_writel(cpu_to_le32(*src
), dst
);
221 __raw_writel(*src
, dst
);
230 /* in case we have 3, 2 or 1 bytes left. The buffer is allocated and the
231 * extra bytes should not be read by the HW.
234 if (dst_offset
< PAYLOAD_OFFSET
)
235 __raw_writel(cpu_to_le32(*src
), dst
);
237 __raw_writel(*src
, dst
);
241 * Read and write ptds. 'ptd_offset' should be one of ISO_PTD_OFFSET,
242 * INT_PTD_OFFSET, and ATL_PTD_OFFSET. 'slot' should be less than 32.
244 static void ptd_read(void __iomem
*base
, u32 ptd_offset
, u32 slot
,
247 reg_write32(base
, HC_MEMORY_REG
,
248 ISP_BANK(0) + ptd_offset
+ slot
*sizeof(*ptd
));
250 bank_reads8(base
, ptd_offset
+ slot
*sizeof(*ptd
), ISP_BANK(0),
251 (void *) ptd
, sizeof(*ptd
));
254 static void ptd_write(void __iomem
*base
, u32 ptd_offset
, u32 slot
,
257 mem_writes8(base
, ptd_offset
+ slot
*sizeof(*ptd
) + sizeof(ptd
->dw0
),
258 &ptd
->dw1
, 7*sizeof(ptd
->dw1
));
259 /* Make sure dw0 gets written last (after other dw's and after payload)
260 since it contains the enable bit */
262 mem_writes8(base
, ptd_offset
+ slot
*sizeof(*ptd
), &ptd
->dw0
,
267 /* memory management of the 60kb on the chip from 0x1000 to 0xffff */
268 static void init_memory(struct isp1760_hcd
*priv
)
273 payload_addr
= PAYLOAD_OFFSET
;
274 for (i
= 0; i
< BLOCK_1_NUM
; i
++) {
275 priv
->memory_pool
[i
].start
= payload_addr
;
276 priv
->memory_pool
[i
].size
= BLOCK_1_SIZE
;
277 priv
->memory_pool
[i
].free
= 1;
278 payload_addr
+= priv
->memory_pool
[i
].size
;
282 for (i
= 0; i
< BLOCK_2_NUM
; i
++) {
283 priv
->memory_pool
[curr
+ i
].start
= payload_addr
;
284 priv
->memory_pool
[curr
+ i
].size
= BLOCK_2_SIZE
;
285 priv
->memory_pool
[curr
+ i
].free
= 1;
286 payload_addr
+= priv
->memory_pool
[curr
+ i
].size
;
290 for (i
= 0; i
< BLOCK_3_NUM
; i
++) {
291 priv
->memory_pool
[curr
+ i
].start
= payload_addr
;
292 priv
->memory_pool
[curr
+ i
].size
= BLOCK_3_SIZE
;
293 priv
->memory_pool
[curr
+ i
].free
= 1;
294 payload_addr
+= priv
->memory_pool
[curr
+ i
].size
;
297 WARN_ON(payload_addr
- priv
->memory_pool
[0].start
> PAYLOAD_AREA_SIZE
);
300 static void alloc_mem(struct usb_hcd
*hcd
, struct isp1760_qtd
*qtd
)
302 struct isp1760_hcd
*priv
= hcd_to_priv(hcd
);
305 WARN_ON(qtd
->payload_addr
);
310 for (i
= 0; i
< BLOCKS
; i
++) {
311 if (priv
->memory_pool
[i
].size
>= qtd
->length
&&
312 priv
->memory_pool
[i
].free
) {
313 priv
->memory_pool
[i
].free
= 0;
314 qtd
->payload_addr
= priv
->memory_pool
[i
].start
;
320 static void free_mem(struct usb_hcd
*hcd
, struct isp1760_qtd
*qtd
)
322 struct isp1760_hcd
*priv
= hcd_to_priv(hcd
);
325 if (!qtd
->payload_addr
)
328 for (i
= 0; i
< BLOCKS
; i
++) {
329 if (priv
->memory_pool
[i
].start
== qtd
->payload_addr
) {
330 WARN_ON(priv
->memory_pool
[i
].free
);
331 priv
->memory_pool
[i
].free
= 1;
332 qtd
->payload_addr
= 0;
337 dev_err(hcd
->self
.controller
, "%s: Invalid pointer: %08x\n",
338 __func__
, qtd
->payload_addr
);
340 qtd
->payload_addr
= 0;
343 static int handshake(struct usb_hcd
*hcd
, u32 reg
,
344 u32 mask
, u32 done
, int usec
)
349 result
= reg_read32(hcd
->regs
, reg
);
361 /* reset a non-running (STS_HALT == 1) controller */
362 static int ehci_reset(struct usb_hcd
*hcd
)
365 struct isp1760_hcd
*priv
= hcd_to_priv(hcd
);
367 u32 command
= reg_read32(hcd
->regs
, HC_USBCMD
);
369 command
|= CMD_RESET
;
370 reg_write32(hcd
->regs
, HC_USBCMD
, command
);
371 hcd
->state
= HC_STATE_HALT
;
372 priv
->next_statechange
= jiffies
;
373 retval
= handshake(hcd
, HC_USBCMD
,
374 CMD_RESET
, 0, 250 * 1000);
378 static struct isp1760_qh
*qh_alloc(gfp_t flags
)
380 struct isp1760_qh
*qh
;
382 qh
= kmem_cache_zalloc(qh_cachep
, flags
);
386 INIT_LIST_HEAD(&qh
->qh_list
);
387 INIT_LIST_HEAD(&qh
->qtd_list
);
393 static void qh_free(struct isp1760_qh
*qh
)
395 WARN_ON(!list_empty(&qh
->qtd_list
));
396 WARN_ON(qh
->slot
> -1);
397 kmem_cache_free(qh_cachep
, qh
);
400 /* one-time init, only for memory state */
401 static int priv_init(struct usb_hcd
*hcd
)
403 struct isp1760_hcd
*priv
= hcd_to_priv(hcd
);
406 spin_lock_init(&priv
->lock
);
408 INIT_LIST_HEAD(&priv
->interruptqhs
);
409 INIT_LIST_HEAD(&priv
->controlqhs
);
410 INIT_LIST_HEAD(&priv
->bulkqhs
);
413 * hw default: 1K periodic list heads, one per frame.
414 * periodic_size can shrink by USBCMD update if hcc_params allows.
416 priv
->periodic_size
= DEFAULT_I_TDPS
;
418 /* controllers may cache some of the periodic schedule ... */
419 hcc_params
= reg_read32(hcd
->regs
, HC_HCCPARAMS
);
420 /* full frame cache */
421 if (HCC_ISOC_CACHE(hcc_params
))
423 else /* N microframes cached */
424 priv
->i_thresh
= 2 + HCC_ISOC_THRES(hcc_params
);
429 static int isp1760_hc_setup(struct usb_hcd
*hcd
)
431 struct isp1760_hcd
*priv
= hcd_to_priv(hcd
);
435 /* Setup HW Mode Control: This assumes a level active-low interrupt */
436 hwmode
= HW_DATA_BUS_32BIT
;
438 if (priv
->devflags
& ISP1760_FLAG_BUS_WIDTH_16
)
439 hwmode
&= ~HW_DATA_BUS_32BIT
;
440 if (priv
->devflags
& ISP1760_FLAG_ANALOG_OC
)
441 hwmode
|= HW_ANA_DIGI_OC
;
442 if (priv
->devflags
& ISP1760_FLAG_DACK_POL_HIGH
)
443 hwmode
|= HW_DACK_POL_HIGH
;
444 if (priv
->devflags
& ISP1760_FLAG_DREQ_POL_HIGH
)
445 hwmode
|= HW_DREQ_POL_HIGH
;
446 if (priv
->devflags
& ISP1760_FLAG_INTR_POL_HIGH
)
447 hwmode
|= HW_INTR_HIGH_ACT
;
448 if (priv
->devflags
& ISP1760_FLAG_INTR_EDGE_TRIG
)
449 hwmode
|= HW_INTR_EDGE_TRIG
;
452 * We have to set this first in case we're in 16-bit mode.
453 * Write it twice to ensure correct upper bits if switching
456 reg_write32(hcd
->regs
, HC_HW_MODE_CTRL
, hwmode
);
457 reg_write32(hcd
->regs
, HC_HW_MODE_CTRL
, hwmode
);
459 reg_write32(hcd
->regs
, HC_SCRATCH_REG
, 0xdeadbabe);
460 /* Change bus pattern */
461 scratch
= reg_read32(hcd
->regs
, HC_CHIP_ID_REG
);
462 scratch
= reg_read32(hcd
->regs
, HC_SCRATCH_REG
);
463 if (scratch
!= 0xdeadbabe) {
464 dev_err(hcd
->self
.controller
, "Scratch test failed.\n");
469 reg_write32(hcd
->regs
, HC_BUFFER_STATUS_REG
, 0);
470 reg_write32(hcd
->regs
, HC_ATL_PTD_SKIPMAP_REG
, NO_TRANSFER_ACTIVE
);
471 reg_write32(hcd
->regs
, HC_INT_PTD_SKIPMAP_REG
, NO_TRANSFER_ACTIVE
);
472 reg_write32(hcd
->regs
, HC_ISO_PTD_SKIPMAP_REG
, NO_TRANSFER_ACTIVE
);
475 reg_write32(hcd
->regs
, HC_RESET_REG
, SW_RESET_RESET_ALL
);
478 reg_write32(hcd
->regs
, HC_RESET_REG
, SW_RESET_RESET_HC
);
481 result
= ehci_reset(hcd
);
487 dev_info(hcd
->self
.controller
, "bus width: %d, oc: %s\n",
488 (priv
->devflags
& ISP1760_FLAG_BUS_WIDTH_16
) ?
489 16 : 32, (priv
->devflags
& ISP1760_FLAG_ANALOG_OC
) ?
490 "analog" : "digital");
492 /* This is weird: at the first plug-in of a device there seems to be
493 one packet queued that never gets returned? */
494 priv
->active_ptds
= -1;
497 reg_write32(hcd
->regs
, HC_HW_MODE_CTRL
, hwmode
| ALL_ATX_RESET
);
499 reg_write32(hcd
->regs
, HC_HW_MODE_CTRL
, hwmode
);
501 reg_write32(hcd
->regs
, HC_INTERRUPT_ENABLE
, INTERRUPT_ENABLE_MASK
);
504 * PORT 1 Control register of the ISP1760 is the OTG control
505 * register on ISP1761. Since there is no OTG or device controller
506 * support in this driver, we use port 1 as a "normal" USB host port on
509 reg_write32(hcd
->regs
, HC_PORT1_CTRL
, PORT1_POWER
| PORT1_INIT2
);
512 priv
->hcs_params
= reg_read32(hcd
->regs
, HC_HCSPARAMS
);
514 return priv_init(hcd
);
517 static void isp1760_init_maps(struct usb_hcd
*hcd
)
519 /*set last maps, for iso its only 1, else 32 tds bitmap*/
520 reg_write32(hcd
->regs
, HC_ATL_PTD_LASTPTD_REG
, 0x80000000);
521 reg_write32(hcd
->regs
, HC_INT_PTD_LASTPTD_REG
, 0x80000000);
522 reg_write32(hcd
->regs
, HC_ISO_PTD_LASTPTD_REG
, 0x00000001);
524 reg_write32(hcd
->regs
, HC_ATL_PTD_SKIPMAP_REG
, 0xffffffff);
525 reg_write32(hcd
->regs
, HC_INT_PTD_SKIPMAP_REG
, 0xffffffff);
526 reg_write32(hcd
->regs
, HC_ISO_PTD_SKIPMAP_REG
, 0xffffffff);
528 reg_write32(hcd
->regs
, HC_BUFFER_STATUS_REG
,
529 ATL_BUF_FILL
| INT_BUF_FILL
);
532 static void isp1760_enable_interrupts(struct usb_hcd
*hcd
)
534 reg_write32(hcd
->regs
, HC_ATL_IRQ_MASK_AND_REG
, 0);
535 reg_write32(hcd
->regs
, HC_ATL_IRQ_MASK_OR_REG
, 0xffffffff);
536 reg_write32(hcd
->regs
, HC_INT_IRQ_MASK_AND_REG
, 0);
537 reg_write32(hcd
->regs
, HC_INT_IRQ_MASK_OR_REG
, 0xffffffff);
538 reg_write32(hcd
->regs
, HC_ISO_IRQ_MASK_AND_REG
, 0);
539 reg_write32(hcd
->regs
, HC_ISO_IRQ_MASK_OR_REG
, 0xffffffff);
543 static int isp1760_run(struct usb_hcd
*hcd
)
550 hcd
->uses_new_polling
= 1;
552 hcd
->state
= HC_STATE_RUNNING
;
553 isp1760_enable_interrupts(hcd
);
554 temp
= reg_read32(hcd
->regs
, HC_HW_MODE_CTRL
);
555 reg_write32(hcd
->regs
, HC_HW_MODE_CTRL
, temp
| HW_GLOBAL_INTR_EN
);
557 command
= reg_read32(hcd
->regs
, HC_USBCMD
);
558 command
&= ~(CMD_LRESET
|CMD_RESET
);
560 reg_write32(hcd
->regs
, HC_USBCMD
, command
);
562 retval
= handshake(hcd
, HC_USBCMD
, CMD_RUN
, CMD_RUN
, 250 * 1000);
568 * Spec says to write FLAG_CF as last config action, priv code grabs
569 * the semaphore while doing so.
571 down_write(&ehci_cf_port_reset_rwsem
);
572 reg_write32(hcd
->regs
, HC_CONFIGFLAG
, FLAG_CF
);
574 retval
= handshake(hcd
, HC_CONFIGFLAG
, FLAG_CF
, FLAG_CF
, 250 * 1000);
575 up_write(&ehci_cf_port_reset_rwsem
);
579 chipid
= reg_read32(hcd
->regs
, HC_CHIP_ID_REG
);
580 dev_info(hcd
->self
.controller
, "USB ISP %04x HW rev. %d started\n",
581 chipid
& 0xffff, chipid
>> 16);
583 /* PTD Register Init Part 2, Step 28 */
585 isp1760_init_maps(hcd
);
587 /* GRR this is run-once init(), being done every time the HC starts.
588 * So long as they're part of class devices, we can't do it init()
589 * since the class device isn't created that early.
594 static u32
base_to_chip(u32 base
)
596 return ((base
- 0x400) >> 3);
599 static int last_qtd_of_urb(struct isp1760_qtd
*qtd
, struct isp1760_qh
*qh
)
603 if (list_is_last(&qtd
->qtd_list
, &qh
->qtd_list
))
607 qtd
= list_entry(qtd
->qtd_list
.next
, typeof(*qtd
), qtd_list
);
608 return (qtd
->urb
!= urb
);
611 /* magic numbers that can affect system performance */
612 #define EHCI_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
613 #define EHCI_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
614 #define EHCI_TUNE_RL_TT 0
615 #define EHCI_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
616 #define EHCI_TUNE_MULT_TT 1
617 #define EHCI_TUNE_FLS 2 /* (small) 256 frame schedule */
619 static void create_ptd_atl(struct isp1760_qh
*qh
,
620 struct isp1760_qtd
*qtd
, struct ptd
*ptd
)
625 u32 nak
= NAK_COUNTER
;
627 memset(ptd
, 0, sizeof(*ptd
));
629 /* according to 3.6.2, max packet len can not be > 0x400 */
630 maxpacket
= usb_maxpacket(qtd
->urb
->dev
, qtd
->urb
->pipe
,
631 usb_pipeout(qtd
->urb
->pipe
));
632 multi
= 1 + ((maxpacket
>> 11) & 0x3);
636 ptd
->dw0
= DW0_VALID_BIT
;
637 ptd
->dw0
|= TO_DW0_LENGTH(qtd
->length
);
638 ptd
->dw0
|= TO_DW0_MAXPACKET(maxpacket
);
639 ptd
->dw0
|= TO_DW0_ENDPOINT(usb_pipeendpoint(qtd
->urb
->pipe
));
642 ptd
->dw1
= usb_pipeendpoint(qtd
->urb
->pipe
) >> 1;
643 ptd
->dw1
|= TO_DW1_DEVICE_ADDR(usb_pipedevice(qtd
->urb
->pipe
));
644 ptd
->dw1
|= TO_DW1_PID_TOKEN(qtd
->packet_type
);
646 if (usb_pipebulk(qtd
->urb
->pipe
))
647 ptd
->dw1
|= DW1_TRANS_BULK
;
648 else if (usb_pipeint(qtd
->urb
->pipe
))
649 ptd
->dw1
|= DW1_TRANS_INT
;
651 if (qtd
->urb
->dev
->speed
!= USB_SPEED_HIGH
) {
652 /* split transaction */
654 ptd
->dw1
|= DW1_TRANS_SPLIT
;
655 if (qtd
->urb
->dev
->speed
== USB_SPEED_LOW
)
656 ptd
->dw1
|= DW1_SE_USB_LOSPEED
;
658 ptd
->dw1
|= TO_DW1_PORT_NUM(qtd
->urb
->dev
->ttport
);
659 ptd
->dw1
|= TO_DW1_HUB_NUM(qtd
->urb
->dev
->tt
->hub
->devnum
);
661 /* SE bit for Split INT transfers */
662 if (usb_pipeint(qtd
->urb
->pipe
) &&
663 (qtd
->urb
->dev
->speed
== USB_SPEED_LOW
))
669 ptd
->dw0
|= TO_DW0_MULTI(multi
);
670 if (usb_pipecontrol(qtd
->urb
->pipe
) ||
671 usb_pipebulk(qtd
->urb
->pipe
))
672 ptd
->dw3
|= TO_DW3_PING(qh
->ping
);
676 ptd
->dw2
|= TO_DW2_DATA_START_ADDR(base_to_chip(qtd
->payload_addr
));
677 ptd
->dw2
|= TO_DW2_RL(rl
);
680 ptd
->dw3
|= TO_DW3_NAKCOUNT(nak
);
681 ptd
->dw3
|= TO_DW3_DATA_TOGGLE(qh
->toggle
);
682 if (usb_pipecontrol(qtd
->urb
->pipe
)) {
683 if (qtd
->data_buffer
== qtd
->urb
->setup_packet
)
684 ptd
->dw3
&= ~TO_DW3_DATA_TOGGLE(1);
685 else if (last_qtd_of_urb(qtd
, qh
))
686 ptd
->dw3
|= TO_DW3_DATA_TOGGLE(1);
689 ptd
->dw3
|= DW3_ACTIVE_BIT
;
691 ptd
->dw3
|= TO_DW3_CERR(ERR_COUNTER
);
694 static void transform_add_int(struct isp1760_qh
*qh
,
695 struct isp1760_qtd
*qtd
, struct ptd
*ptd
)
701 * Most of this is guessing. ISP1761 datasheet is quite unclear, and
702 * the algorithm from the original Philips driver code, which was
703 * pretty much used in this driver before as well, is quite horrendous
704 * and, i believe, incorrect. The code below follows the datasheet and
705 * USB2.0 spec as far as I can tell, and plug/unplug seems to be much
706 * more reliable this way (fingers crossed...).
709 if (qtd
->urb
->dev
->speed
== USB_SPEED_HIGH
) {
710 /* urb->interval is in units of microframes (1/8 ms) */
711 period
= qtd
->urb
->interval
>> 3;
713 if (qtd
->urb
->interval
> 4)
714 usof
= 0x01; /* One bit set =>
715 interval 1 ms * uFrame-match */
716 else if (qtd
->urb
->interval
> 2)
717 usof
= 0x22; /* Two bits set => interval 1/2 ms */
718 else if (qtd
->urb
->interval
> 1)
719 usof
= 0x55; /* Four bits set => interval 1/4 ms */
721 usof
= 0xff; /* All bits set => interval 1/8 ms */
723 /* urb->interval is in units of frames (1 ms) */
724 period
= qtd
->urb
->interval
;
725 usof
= 0x0f; /* Execute Start Split on any of the
726 four first uFrames */
729 * First 8 bits in dw5 is uSCS and "specifies which uSOF the
730 * complete split needs to be sent. Valid only for IN." Also,
731 * "All bits can be set to one for every transfer." (p 82,
732 * ISP1761 data sheet.) 0x1c is from Philips driver. Where did
733 * that number come from? 0xff seems to work fine...
735 /* ptd->dw5 = 0x1c; */
736 ptd
->dw5
= 0xff; /* Execute Complete Split on any uFrame */
739 period
= period
>> 1;/* Ensure equal or shorter period than requested */
740 period
&= 0xf8; /* Mask off too large values and lowest unused 3 bits */
746 static void create_ptd_int(struct isp1760_qh
*qh
,
747 struct isp1760_qtd
*qtd
, struct ptd
*ptd
)
749 create_ptd_atl(qh
, qtd
, ptd
);
750 transform_add_int(qh
, qtd
, ptd
);
753 static void isp1760_urb_done(struct usb_hcd
*hcd
, struct urb
*urb
)
754 __releases(priv
->lock
)
755 __acquires(priv
->lock
)
757 struct isp1760_hcd
*priv
= hcd_to_priv(hcd
);
759 if (!urb
->unlinked
) {
760 if (urb
->status
== -EINPROGRESS
)
764 if (usb_pipein(urb
->pipe
) && usb_pipetype(urb
->pipe
) != PIPE_CONTROL
) {
766 for (ptr
= urb
->transfer_buffer
;
767 ptr
< urb
->transfer_buffer
+ urb
->transfer_buffer_length
;
769 flush_dcache_page(virt_to_page(ptr
));
772 /* complete() can reenter this HCD */
773 usb_hcd_unlink_urb_from_ep(hcd
, urb
);
774 spin_unlock(&priv
->lock
);
775 usb_hcd_giveback_urb(hcd
, urb
, urb
->status
);
776 spin_lock(&priv
->lock
);
779 static struct isp1760_qtd
*qtd_alloc(gfp_t flags
, struct urb
*urb
,
782 struct isp1760_qtd
*qtd
;
784 qtd
= kmem_cache_zalloc(qtd_cachep
, flags
);
788 INIT_LIST_HEAD(&qtd
->qtd_list
);
790 qtd
->packet_type
= packet_type
;
791 qtd
->status
= QTD_ENQUEUED
;
792 qtd
->actual_length
= 0;
797 static void qtd_free(struct isp1760_qtd
*qtd
)
799 WARN_ON(qtd
->payload_addr
);
800 kmem_cache_free(qtd_cachep
, qtd
);
803 static void start_bus_transfer(struct usb_hcd
*hcd
, u32 ptd_offset
, int slot
,
804 struct slotinfo
*slots
, struct isp1760_qtd
*qtd
,
805 struct isp1760_qh
*qh
, struct ptd
*ptd
)
807 struct isp1760_hcd
*priv
= hcd_to_priv(hcd
);
810 WARN_ON((slot
< 0) || (slot
> 31));
811 WARN_ON(qtd
->length
&& !qtd
->payload_addr
);
812 WARN_ON(slots
[slot
].qtd
);
813 WARN_ON(slots
[slot
].qh
);
814 WARN_ON(qtd
->status
!= QTD_PAYLOAD_ALLOC
);
816 slots
[slot
].qtd
= qtd
;
819 qtd
->status
= QTD_XFER_STARTED
; /* Set this before writing ptd, since
820 interrupt routine may preempt and expects this value. */
821 ptd_write(hcd
->regs
, ptd_offset
, slot
, ptd
);
824 /* Make sure done map has not triggered from some unlinked transfer */
825 if (ptd_offset
== ATL_PTD_OFFSET
) {
826 priv
->atl_done_map
|= reg_read32(hcd
->regs
,
827 HC_ATL_PTD_DONEMAP_REG
);
828 priv
->atl_done_map
&= ~(1 << qh
->slot
);
830 skip_map
= reg_read32(hcd
->regs
, HC_ATL_PTD_SKIPMAP_REG
);
831 skip_map
&= ~(1 << qh
->slot
);
832 reg_write32(hcd
->regs
, HC_ATL_PTD_SKIPMAP_REG
, skip_map
);
834 priv
->int_done_map
|= reg_read32(hcd
->regs
,
835 HC_INT_PTD_DONEMAP_REG
);
836 priv
->int_done_map
&= ~(1 << qh
->slot
);
838 skip_map
= reg_read32(hcd
->regs
, HC_INT_PTD_SKIPMAP_REG
);
839 skip_map
&= ~(1 << qh
->slot
);
840 reg_write32(hcd
->regs
, HC_INT_PTD_SKIPMAP_REG
, skip_map
);
844 static int is_short_bulk(struct isp1760_qtd
*qtd
)
846 return (usb_pipebulk(qtd
->urb
->pipe
) &&
847 (qtd
->actual_length
< qtd
->length
));
850 static void collect_qtds(struct usb_hcd
*hcd
, struct isp1760_qh
*qh
,
851 struct list_head
*urb_list
)
854 struct isp1760_qtd
*qtd
, *qtd_next
;
855 struct urb_listitem
*urb_listitem
;
857 list_for_each_entry_safe(qtd
, qtd_next
, &qh
->qtd_list
, qtd_list
) {
858 if (qtd
->status
< QTD_XFER_COMPLETE
)
861 if (list_is_last(&qtd
->qtd_list
, &qh
->qtd_list
))
864 last_qtd
= qtd
->urb
!= qtd_next
->urb
;
866 if ((!last_qtd
) && (qtd
->status
== QTD_RETIRE
))
867 qtd_next
->status
= QTD_RETIRE
;
869 if (qtd
->status
== QTD_XFER_COMPLETE
) {
870 if (qtd
->actual_length
) {
871 switch (qtd
->packet_type
) {
873 mem_reads8(hcd
->regs
, qtd
->payload_addr
,
876 /* Fall through (?) */
878 qtd
->urb
->actual_length
+=
880 /* Fall through ... */
886 if (is_short_bulk(qtd
)) {
887 if (qtd
->urb
->transfer_flags
& URB_SHORT_NOT_OK
)
888 qtd
->urb
->status
= -EREMOTEIO
;
890 qtd_next
->status
= QTD_RETIRE
;
894 if (qtd
->payload_addr
)
898 if ((qtd
->status
== QTD_RETIRE
) &&
899 (qtd
->urb
->status
== -EINPROGRESS
))
900 qtd
->urb
->status
= -EPIPE
;
901 /* Defer calling of urb_done() since it releases lock */
902 urb_listitem
= kmem_cache_zalloc(urb_listitem_cachep
,
904 if (unlikely(!urb_listitem
))
906 urb_listitem
->urb
= qtd
->urb
;
907 list_add_tail(&urb_listitem
->urb_list
, urb_list
);
910 list_del(&qtd
->qtd_list
);
915 #define ENQUEUE_DEPTH 2
916 static void enqueue_qtds(struct usb_hcd
*hcd
, struct isp1760_qh
*qh
)
918 struct isp1760_hcd
*priv
= hcd_to_priv(hcd
);
920 struct slotinfo
*slots
;
921 int curr_slot
, free_slot
;
924 struct isp1760_qtd
*qtd
;
926 if (unlikely(list_empty(&qh
->qtd_list
))) {
931 if (usb_pipeint(list_entry(qh
->qtd_list
.next
, struct isp1760_qtd
,
932 qtd_list
)->urb
->pipe
)) {
933 ptd_offset
= INT_PTD_OFFSET
;
934 slots
= priv
->int_slots
;
936 ptd_offset
= ATL_PTD_OFFSET
;
937 slots
= priv
->atl_slots
;
941 for (curr_slot
= 0; curr_slot
< 32; curr_slot
++) {
942 if ((free_slot
== -1) && (slots
[curr_slot
].qtd
== NULL
))
943 free_slot
= curr_slot
;
944 if (slots
[curr_slot
].qh
== qh
)
949 list_for_each_entry(qtd
, &qh
->qtd_list
, qtd_list
) {
950 if (qtd
->status
== QTD_ENQUEUED
) {
951 WARN_ON(qtd
->payload_addr
);
953 if ((qtd
->length
) && (!qtd
->payload_addr
))
957 ((qtd
->packet_type
== SETUP_PID
) ||
958 (qtd
->packet_type
== OUT_PID
))) {
959 mem_writes8(hcd
->regs
, qtd
->payload_addr
,
960 qtd
->data_buffer
, qtd
->length
);
963 qtd
->status
= QTD_PAYLOAD_ALLOC
;
966 if (qtd
->status
== QTD_PAYLOAD_ALLOC
) {
968 if ((curr_slot > 31) && (free_slot == -1))
969 dev_dbg(hcd->self.controller, "%s: No slot "
970 "available for transfer\n", __func__);
972 /* Start xfer for this endpoint if not already done */
973 if ((curr_slot
> 31) && (free_slot
> -1)) {
974 if (usb_pipeint(qtd
->urb
->pipe
))
975 create_ptd_int(qh
, qtd
, &ptd
);
977 create_ptd_atl(qh
, qtd
, &ptd
);
979 start_bus_transfer(hcd
, ptd_offset
, free_slot
,
980 slots
, qtd
, qh
, &ptd
);
981 curr_slot
= free_slot
;
985 if (n
>= ENQUEUE_DEPTH
)
991 void schedule_ptds(struct usb_hcd
*hcd
)
993 struct isp1760_hcd
*priv
;
994 struct isp1760_qh
*qh
, *qh_next
;
995 struct list_head
*ep_queue
;
996 struct usb_host_endpoint
*ep
;
998 struct urb_listitem
*urb_listitem
, *urb_listitem_next
;
1005 priv
= hcd_to_priv(hcd
);
1008 * check finished/retired xfers, transfer payloads, call urb_done()
1010 ep_queue
= &priv
->interruptqhs
;
1012 list_for_each_entry_safe(qh
, qh_next
, ep_queue
, qh_list
) {
1013 ep
= list_entry(qh
->qtd_list
.next
, struct isp1760_qtd
,
1015 collect_qtds(hcd
, qh
, &urb_list
);
1016 if (list_empty(&qh
->qtd_list
)) {
1017 list_del(&qh
->qh_list
);
1018 if (ep
->hcpriv
== NULL
) {
1019 /* Endpoint has been disabled, so we
1020 can free the associated queue head. */
1026 if (ep_queue
== &priv
->interruptqhs
)
1027 ep_queue
= &priv
->controlqhs
;
1028 else if (ep_queue
== &priv
->controlqhs
)
1029 ep_queue
= &priv
->bulkqhs
;
1034 list_for_each_entry_safe(urb_listitem
, urb_listitem_next
, &urb_list
,
1036 isp1760_urb_done(hcd
, urb_listitem
->urb
);
1037 kmem_cache_free(urb_listitem_cachep
, urb_listitem
);
1041 * Schedule packets for transfer.
1043 * According to USB2.0 specification:
1045 * 1st prio: interrupt xfers, up to 80 % of bandwidth
1046 * 2nd prio: control xfers
1047 * 3rd prio: bulk xfers
1049 * ... but let's use a simpler scheme here (mostly because ISP1761 doc
1050 * is very unclear on how to prioritize traffic):
1052 * 1) Enqueue any queued control transfers, as long as payload chip mem
1053 * and PTD ATL slots are available.
1054 * 2) Enqueue any queued INT transfers, as long as payload chip mem
1055 * and PTD INT slots are available.
1056 * 3) Enqueue any queued bulk transfers, as long as payload chip mem
1057 * and PTD ATL slots are available.
1059 * Use double buffering (ENQUEUE_DEPTH==2) as a compromise between
1060 * conservation of chip mem and performance.
1062 * I'm sure this scheme could be improved upon!
1064 ep_queue
= &priv
->controlqhs
;
1066 list_for_each_entry_safe(qh
, qh_next
, ep_queue
, qh_list
)
1067 enqueue_qtds(hcd
, qh
);
1069 if (ep_queue
== &priv
->controlqhs
)
1070 ep_queue
= &priv
->interruptqhs
;
1071 else if (ep_queue
== &priv
->interruptqhs
)
1072 ep_queue
= &priv
->bulkqhs
;
1078 #define PTD_STATE_QTD_DONE 1
1079 #define PTD_STATE_QTD_RELOAD 2
1080 #define PTD_STATE_URB_RETIRE 3
1082 static int check_int_transfer(struct usb_hcd
*hcd
, struct ptd
*ptd
,
1091 /* FIXME: ISP1761 datasheet does not say what to do with these. Do we
1092 need to handle these errors? Is it done in hardware? */
1094 if (ptd
->dw3
& DW3_HALT_BIT
) {
1096 urb
->status
= -EPROTO
; /* Default unknown error */
1098 for (i
= 0; i
< 8; i
++) {
1099 switch (dw4
& 0x7) {
1101 dev_dbg(hcd
->self
.controller
, "%s: underrun "
1102 "during uFrame %d\n",
1104 urb
->status
= -ECOMM
; /* Could not write data */
1107 dev_dbg(hcd
->self
.controller
, "%s: transaction "
1108 "error during uFrame %d\n",
1110 urb
->status
= -EPROTO
; /* timeout, bad CRC, PID
1114 dev_dbg(hcd
->self
.controller
, "%s: babble "
1115 "error during uFrame %d\n",
1117 urb
->status
= -EOVERFLOW
;
1123 return PTD_STATE_URB_RETIRE
;
1126 return PTD_STATE_QTD_DONE
;
1129 static int check_atl_transfer(struct usb_hcd
*hcd
, struct ptd
*ptd
,
1133 if (ptd
->dw3
& DW3_HALT_BIT
) {
1134 if (ptd
->dw3
& DW3_BABBLE_BIT
)
1135 urb
->status
= -EOVERFLOW
;
1136 else if (FROM_DW3_CERR(ptd
->dw3
))
1137 urb
->status
= -EPIPE
; /* Stall */
1138 else if (ptd
->dw3
& DW3_ERROR_BIT
)
1139 urb
->status
= -EPROTO
; /* XactErr */
1141 urb
->status
= -EPROTO
; /* Unknown */
1143 dev_dbg(hcd->self.controller, "%s: ptd error:\n"
1144 " dw0: %08x dw1: %08x dw2: %08x dw3: %08x\n"
1145 " dw4: %08x dw5: %08x dw6: %08x dw7: %08x\n",
1147 ptd->dw0, ptd->dw1, ptd->dw2, ptd->dw3,
1148 ptd->dw4, ptd->dw5, ptd->dw6, ptd->dw7);
1150 return PTD_STATE_URB_RETIRE
;
1153 if ((ptd
->dw3
& DW3_ERROR_BIT
) && (ptd
->dw3
& DW3_ACTIVE_BIT
)) {
1154 /* Transfer Error, *but* active and no HALT -> reload */
1155 dev_dbg(hcd
->self
.controller
, "PID error; reloading ptd\n");
1156 return PTD_STATE_QTD_RELOAD
;
1159 if (!FROM_DW3_NAKCOUNT(ptd
->dw3
) && (ptd
->dw3
& DW3_ACTIVE_BIT
)) {
1161 * NAKs are handled in HW by the chip. Usually if the
1162 * device is not able to send data fast enough.
1163 * This happens mostly on slower hardware.
1165 return PTD_STATE_QTD_RELOAD
;
1168 return PTD_STATE_QTD_DONE
;
1171 static irqreturn_t
isp1760_irq(struct usb_hcd
*hcd
)
1173 struct isp1760_hcd
*priv
= hcd_to_priv(hcd
);
1175 irqreturn_t irqret
= IRQ_NONE
;
1177 struct isp1760_qh
*qh
;
1180 struct slotinfo
*slots
;
1182 struct isp1760_qtd
*qtd
;
1184 static int last_active_ptds
;
1185 int int_skip_map
, atl_skip_map
;
1187 spin_lock(&priv
->lock
);
1189 if (!(hcd
->state
& HC_STATE_RUNNING
))
1192 imask
= reg_read32(hcd
->regs
, HC_INTERRUPT_REG
);
1193 if (unlikely(!imask
))
1195 reg_write32(hcd
->regs
, HC_INTERRUPT_REG
, imask
); /* Clear */
1197 int_skip_map
= reg_read32(hcd
->regs
, HC_INT_PTD_SKIPMAP_REG
);
1198 atl_skip_map
= reg_read32(hcd
->regs
, HC_ATL_PTD_SKIPMAP_REG
);
1199 priv
->int_done_map
|= reg_read32(hcd
->regs
, HC_INT_PTD_DONEMAP_REG
);
1200 priv
->atl_done_map
|= reg_read32(hcd
->regs
, HC_ATL_PTD_DONEMAP_REG
);
1201 priv
->int_done_map
&= ~int_skip_map
;
1202 priv
->atl_done_map
&= ~atl_skip_map
;
1204 modified
= priv
->int_done_map
| priv
->atl_done_map
;
1206 while (priv
->int_done_map
|| priv
->atl_done_map
) {
1207 if (priv
->int_done_map
) {
1209 slot
= __ffs(priv
->int_done_map
);
1210 priv
->int_done_map
&= ~(1 << slot
);
1211 slots
= priv
->int_slots
;
1212 /* This should not trigger, and could be removed if
1213 noone have any problems with it triggering: */
1214 if (!slots
[slot
].qh
) {
1218 ptd_offset
= INT_PTD_OFFSET
;
1219 ptd_read(hcd
->regs
, INT_PTD_OFFSET
, slot
, &ptd
);
1220 state
= check_int_transfer(hcd
, &ptd
,
1221 slots
[slot
].qtd
->urb
);
1224 slot
= __ffs(priv
->atl_done_map
);
1225 priv
->atl_done_map
&= ~(1 << slot
);
1226 slots
= priv
->atl_slots
;
1227 /* This should not trigger, and could be removed if
1228 noone have any problems with it triggering: */
1229 if (!slots
[slot
].qh
) {
1233 ptd_offset
= ATL_PTD_OFFSET
;
1234 ptd_read(hcd
->regs
, ATL_PTD_OFFSET
, slot
, &ptd
);
1235 state
= check_atl_transfer(hcd
, &ptd
,
1236 slots
[slot
].qtd
->urb
);
1239 qtd
= slots
[slot
].qtd
;
1240 slots
[slot
].qtd
= NULL
;
1241 qh
= slots
[slot
].qh
;
1242 slots
[slot
].qh
= NULL
;
1243 priv
->active_ptds
--;
1246 WARN_ON(qtd
->status
!= QTD_XFER_STARTED
);
1249 case PTD_STATE_QTD_DONE
:
1250 if ((usb_pipeint(qtd
->urb
->pipe
)) &&
1251 (qtd
->urb
->dev
->speed
!= USB_SPEED_HIGH
))
1252 qtd
->actual_length
=
1253 FROM_DW3_SCS_NRBYTESTRANSFERRED(ptd
.dw3
);
1255 qtd
->actual_length
=
1256 FROM_DW3_NRBYTESTRANSFERRED(ptd
.dw3
);
1258 qtd
->status
= QTD_XFER_COMPLETE
;
1259 if (list_is_last(&qtd
->qtd_list
, &qh
->qtd_list
) ||
1263 qtd
= list_entry(qtd
->qtd_list
.next
,
1264 typeof(*qtd
), qtd_list
);
1266 qh
->toggle
= FROM_DW3_DATA_TOGGLE(ptd
.dw3
);
1267 qh
->ping
= FROM_DW3_PING(ptd
.dw3
);
1270 case PTD_STATE_QTD_RELOAD
: /* QTD_RETRY, for atls only */
1271 qtd
->status
= QTD_PAYLOAD_ALLOC
;
1272 ptd
.dw0
|= DW0_VALID_BIT
;
1273 /* RL counter = ERR counter */
1274 ptd
.dw3
&= ~TO_DW3_NAKCOUNT(0xf);
1275 ptd
.dw3
|= TO_DW3_NAKCOUNT(FROM_DW2_RL(ptd
.dw2
));
1276 ptd
.dw3
&= ~TO_DW3_CERR(3);
1277 ptd
.dw3
|= TO_DW3_CERR(ERR_COUNTER
);
1278 qh
->toggle
= FROM_DW3_DATA_TOGGLE(ptd
.dw3
);
1279 qh
->ping
= FROM_DW3_PING(ptd
.dw3
);
1282 case PTD_STATE_URB_RETIRE
:
1283 qtd
->status
= QTD_RETIRE
;
1294 if (qtd
&& (qtd
->status
== QTD_PAYLOAD_ALLOC
)) {
1295 if (slots
== priv
->int_slots
) {
1296 if (state
== PTD_STATE_QTD_RELOAD
)
1297 dev_err(hcd
->self
.controller
,
1298 "%s: PTD_STATE_QTD_RELOAD on "
1299 "interrupt packet\n", __func__
);
1300 if (state
!= PTD_STATE_QTD_RELOAD
)
1301 create_ptd_int(qh
, qtd
, &ptd
);
1303 if (state
!= PTD_STATE_QTD_RELOAD
)
1304 create_ptd_atl(qh
, qtd
, &ptd
);
1307 start_bus_transfer(hcd
, ptd_offset
, slot
, slots
, qtd
,
1315 /* ISP1760 Errata 2 explains that interrupts may be missed (or not
1316 happen?) if two USB devices are running simultaneously. Perhaps
1317 this happens when a PTD is finished during interrupt handling;
1318 enable SOF interrupts if PTDs are still scheduled when exiting this
1319 interrupt handler, just to be safe. */
1321 if (priv
->active_ptds
!= last_active_ptds
) {
1322 if (priv
->active_ptds
> 0)
1323 reg_write32(hcd
->regs
, HC_INTERRUPT_ENABLE
,
1324 INTERRUPT_ENABLE_SOT_MASK
);
1326 reg_write32(hcd
->regs
, HC_INTERRUPT_ENABLE
,
1327 INTERRUPT_ENABLE_MASK
);
1328 last_active_ptds
= priv
->active_ptds
;
1331 irqret
= IRQ_HANDLED
;
1333 spin_unlock(&priv
->lock
);
1338 static int qtd_fill(struct isp1760_qtd
*qtd
, void *databuffer
, size_t len
)
1340 qtd
->data_buffer
= databuffer
;
1342 if (len
> MAX_PAYLOAD_SIZE
)
1343 len
= MAX_PAYLOAD_SIZE
;
1349 static void qtd_list_free(struct list_head
*qtd_list
)
1351 struct isp1760_qtd
*qtd
, *qtd_next
;
1353 list_for_each_entry_safe(qtd
, qtd_next
, qtd_list
, qtd_list
) {
1354 list_del(&qtd
->qtd_list
);
1360 * Packetize urb->transfer_buffer into list of packets of size wMaxPacketSize.
1361 * Also calculate the PID type (SETUP/IN/OUT) for each packet.
1363 #define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
1364 static void packetize_urb(struct usb_hcd
*hcd
,
1365 struct urb
*urb
, struct list_head
*head
, gfp_t flags
)
1367 struct isp1760_qtd
*qtd
;
1369 int len
, maxpacketsize
;
1373 * URBs map to sequences of QTDs: one logical transaction
1376 if (!urb
->transfer_buffer
&& urb
->transfer_buffer_length
) {
1377 /* XXX This looks like usb storage / SCSI bug */
1378 dev_err(hcd
->self
.controller
,
1379 "buf is null, dma is %08lx len is %d\n",
1380 (long unsigned)urb
->transfer_dma
,
1381 urb
->transfer_buffer_length
);
1385 if (usb_pipein(urb
->pipe
))
1386 packet_type
= IN_PID
;
1388 packet_type
= OUT_PID
;
1390 if (usb_pipecontrol(urb
->pipe
)) {
1391 qtd
= qtd_alloc(flags
, urb
, SETUP_PID
);
1394 qtd_fill(qtd
, urb
->setup_packet
, sizeof(struct usb_ctrlrequest
));
1395 list_add_tail(&qtd
->qtd_list
, head
);
1397 /* for zero length DATA stages, STATUS is always IN */
1398 if (urb
->transfer_buffer_length
== 0)
1399 packet_type
= IN_PID
;
1402 maxpacketsize
= max_packet(usb_maxpacket(urb
->dev
, urb
->pipe
,
1403 usb_pipeout(urb
->pipe
)));
1406 * buffer gets wrapped in one or more qtds;
1407 * last one may be "short" (including zero len)
1408 * and may serve as a control status ack
1410 buf
= urb
->transfer_buffer
;
1411 len
= urb
->transfer_buffer_length
;
1416 qtd
= qtd_alloc(flags
, urb
, packet_type
);
1419 this_qtd_len
= qtd_fill(qtd
, buf
, len
);
1420 list_add_tail(&qtd
->qtd_list
, head
);
1422 len
-= this_qtd_len
;
1423 buf
+= this_qtd_len
;
1430 * control requests may need a terminating data "status" ack;
1431 * bulk ones may need a terminating short packet (zero length).
1433 if (urb
->transfer_buffer_length
!= 0) {
1436 if (usb_pipecontrol(urb
->pipe
)) {
1438 if (packet_type
== IN_PID
)
1439 packet_type
= OUT_PID
;
1441 packet_type
= IN_PID
;
1442 } else if (usb_pipebulk(urb
->pipe
)
1443 && (urb
->transfer_flags
& URB_ZERO_PACKET
)
1444 && !(urb
->transfer_buffer_length
%
1449 qtd
= qtd_alloc(flags
, urb
, packet_type
);
1453 /* never any data in such packets */
1454 qtd_fill(qtd
, NULL
, 0);
1455 list_add_tail(&qtd
->qtd_list
, head
);
1462 qtd_list_free(head
);
1465 static int isp1760_urb_enqueue(struct usb_hcd
*hcd
, struct urb
*urb
,
1468 struct isp1760_hcd
*priv
= hcd_to_priv(hcd
);
1469 struct list_head
*ep_queue
;
1470 struct isp1760_qh
*qh
, *qhit
;
1471 unsigned long spinflags
;
1472 LIST_HEAD(new_qtds
);
1476 switch (usb_pipetype(urb
->pipe
)) {
1478 ep_queue
= &priv
->controlqhs
;
1481 ep_queue
= &priv
->bulkqhs
;
1483 case PIPE_INTERRUPT
:
1484 if (urb
->interval
< 0)
1486 /* FIXME: Check bandwidth */
1487 ep_queue
= &priv
->interruptqhs
;
1489 case PIPE_ISOCHRONOUS
:
1490 dev_err(hcd
->self
.controller
, "%s: isochronous USB packets "
1491 "not yet supported\n",
1495 dev_err(hcd
->self
.controller
, "%s: unknown pipe type\n",
1500 if (usb_pipein(urb
->pipe
))
1501 urb
->actual_length
= 0;
1503 packetize_urb(hcd
, urb
, &new_qtds
, mem_flags
);
1504 if (list_empty(&new_qtds
))
1506 urb
->hcpriv
= NULL
; /* Used to signal unlink to interrupt handler */
1509 spin_lock_irqsave(&priv
->lock
, spinflags
);
1511 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE
, &hcd
->flags
)) {
1512 retval
= -ESHUTDOWN
;
1515 retval
= usb_hcd_link_urb_to_ep(hcd
, urb
);
1519 qh
= urb
->ep
->hcpriv
;
1522 list_for_each_entry(qhit
, ep_queue
, qh_list
) {
1529 list_add_tail(&qh
->qh_list
, ep_queue
);
1531 qh
= qh_alloc(GFP_ATOMIC
);
1536 list_add_tail(&qh
->qh_list
, ep_queue
);
1537 urb
->ep
->hcpriv
= qh
;
1540 list_splice_tail(&new_qtds
, &qh
->qtd_list
);
1544 spin_unlock_irqrestore(&priv
->lock
, spinflags
);
1548 static void kill_transfer(struct usb_hcd
*hcd
, struct urb
*urb
,
1549 struct isp1760_qh
*qh
)
1551 struct isp1760_hcd
*priv
= hcd_to_priv(hcd
);
1554 WARN_ON(qh
->slot
== -1);
1556 /* We need to forcefully reclaim the slot since some transfers never
1557 return, e.g. interrupt transfers and NAKed bulk transfers. */
1558 if (usb_pipecontrol(urb
->pipe
) || usb_pipebulk(urb
->pipe
)) {
1559 skip_map
= reg_read32(hcd
->regs
, HC_ATL_PTD_SKIPMAP_REG
);
1560 skip_map
|= (1 << qh
->slot
);
1561 reg_write32(hcd
->regs
, HC_ATL_PTD_SKIPMAP_REG
, skip_map
);
1562 priv
->atl_slots
[qh
->slot
].qh
= NULL
;
1563 priv
->atl_slots
[qh
->slot
].qtd
= NULL
;
1565 skip_map
= reg_read32(hcd
->regs
, HC_INT_PTD_SKIPMAP_REG
);
1566 skip_map
|= (1 << qh
->slot
);
1567 reg_write32(hcd
->regs
, HC_INT_PTD_SKIPMAP_REG
, skip_map
);
1568 priv
->int_slots
[qh
->slot
].qh
= NULL
;
1569 priv
->int_slots
[qh
->slot
].qtd
= NULL
;
1573 priv
->active_ptds
--;
1576 static int isp1760_urb_dequeue(struct usb_hcd
*hcd
, struct urb
*urb
,
1579 struct isp1760_hcd
*priv
= hcd_to_priv(hcd
);
1580 unsigned long spinflags
;
1581 struct isp1760_qh
*qh
;
1582 struct isp1760_qtd
*qtd
;
1585 spin_lock_irqsave(&priv
->lock
, spinflags
);
1586 retval
= usb_hcd_check_unlink_urb(hcd
, urb
, status
);
1590 qh
= urb
->ep
->hcpriv
;
1596 list_for_each_entry(qtd
, &qh
->qtd_list
, qtd_list
)
1597 if (qtd
->urb
== urb
) {
1598 if (qtd
->status
== QTD_XFER_STARTED
)
1599 kill_transfer(hcd
, urb
, qh
);
1600 qtd
->status
= QTD_RETIRE
;
1603 urb
->status
= status
;
1607 spin_unlock_irqrestore(&priv
->lock
, spinflags
);
1611 static void isp1760_endpoint_disable(struct usb_hcd
*hcd
,
1612 struct usb_host_endpoint
*ep
)
1614 struct isp1760_hcd
*priv
= hcd_to_priv(hcd
);
1615 unsigned long spinflags
;
1616 struct isp1760_qh
*qh
;
1617 struct isp1760_qtd
*qtd
;
1619 spin_lock_irqsave(&priv
->lock
, spinflags
);
1625 list_for_each_entry(qtd
, &qh
->qtd_list
, qtd_list
) {
1626 if (qtd
->status
== QTD_XFER_STARTED
)
1627 kill_transfer(hcd
, qtd
->urb
, qh
);
1628 qtd
->status
= QTD_RETIRE
;
1629 qtd
->urb
->status
= -ECONNRESET
;
1633 /* Cannot free qh here since it will be parsed by schedule_ptds() */
1638 spin_unlock_irqrestore(&priv
->lock
, spinflags
);
1641 static int isp1760_hub_status_data(struct usb_hcd
*hcd
, char *buf
)
1643 struct isp1760_hcd
*priv
= hcd_to_priv(hcd
);
1644 u32 temp
, status
= 0;
1647 unsigned long flags
;
1649 /* if !USB_SUSPEND, root hub timers won't get shut down ... */
1650 if (!HC_IS_RUNNING(hcd
->state
))
1653 /* init status to no-changes */
1657 spin_lock_irqsave(&priv
->lock
, flags
);
1658 temp
= reg_read32(hcd
->regs
, HC_PORTSC1
);
1660 if (temp
& PORT_OWNER
) {
1661 if (temp
& PORT_CSC
) {
1663 reg_write32(hcd
->regs
, HC_PORTSC1
, temp
);
1669 * Return status information even for ports with OWNER set.
1670 * Otherwise khubd wouldn't see the disconnect event when a
1671 * high-speed device is switched over to the companion
1672 * controller by the user.
1675 if ((temp
& mask
) != 0
1676 || ((temp
& PORT_RESUME
) != 0
1677 && time_after_eq(jiffies
,
1678 priv
->reset_done
))) {
1679 buf
[0] |= 1 << (0 + 1);
1682 /* FIXME autosuspend idle root hubs */
1684 spin_unlock_irqrestore(&priv
->lock
, flags
);
1685 return status
? retval
: 0;
1688 static void isp1760_hub_descriptor(struct isp1760_hcd
*priv
,
1689 struct usb_hub_descriptor
*desc
)
1691 int ports
= HCS_N_PORTS(priv
->hcs_params
);
1694 desc
->bDescriptorType
= 0x29;
1695 /* priv 1.0, 2.3.9 says 20ms max */
1696 desc
->bPwrOn2PwrGood
= 10;
1697 desc
->bHubContrCurrent
= 0;
1699 desc
->bNbrPorts
= ports
;
1700 temp
= 1 + (ports
/ 8);
1701 desc
->bDescLength
= 7 + 2 * temp
;
1703 /* ports removable, and usb 1.0 legacy PortPwrCtrlMask */
1704 memset(&desc
->u
.hs
.DeviceRemovable
[0], 0, temp
);
1705 memset(&desc
->u
.hs
.DeviceRemovable
[temp
], 0xff, temp
);
1707 /* per-port overcurrent reporting */
1709 if (HCS_PPC(priv
->hcs_params
))
1710 /* per-port power control */
1713 /* no power switching */
1715 desc
->wHubCharacteristics
= cpu_to_le16(temp
);
1718 #define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
1720 static int check_reset_complete(struct usb_hcd
*hcd
, int index
,
1723 if (!(port_status
& PORT_CONNECT
))
1726 /* if reset finished and it's still not enabled -- handoff */
1727 if (!(port_status
& PORT_PE
)) {
1729 dev_info(hcd
->self
.controller
,
1730 "port %d full speed --> companion\n",
1733 port_status
|= PORT_OWNER
;
1734 port_status
&= ~PORT_RWC_BITS
;
1735 reg_write32(hcd
->regs
, HC_PORTSC1
, port_status
);
1738 dev_info(hcd
->self
.controller
, "port %d high speed\n",
1744 static int isp1760_hub_control(struct usb_hcd
*hcd
, u16 typeReq
,
1745 u16 wValue
, u16 wIndex
, char *buf
, u16 wLength
)
1747 struct isp1760_hcd
*priv
= hcd_to_priv(hcd
);
1748 int ports
= HCS_N_PORTS(priv
->hcs_params
);
1750 unsigned long flags
;
1755 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
1756 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
1757 * (track current state ourselves) ... blink for diagnostics,
1758 * power, "this is the one", etc. EHCI spec supports this.
1761 spin_lock_irqsave(&priv
->lock
, flags
);
1763 case ClearHubFeature
:
1765 case C_HUB_LOCAL_POWER
:
1766 case C_HUB_OVER_CURRENT
:
1767 /* no hub-wide feature/status flags */
1773 case ClearPortFeature
:
1774 if (!wIndex
|| wIndex
> ports
)
1777 temp
= reg_read32(hcd
->regs
, HC_PORTSC1
);
1780 * Even if OWNER is set, so the port is owned by the
1781 * companion controller, khubd needs to be able to clear
1782 * the port-change status bits (especially
1783 * USB_PORT_STAT_C_CONNECTION).
1787 case USB_PORT_FEAT_ENABLE
:
1788 reg_write32(hcd
->regs
, HC_PORTSC1
, temp
& ~PORT_PE
);
1790 case USB_PORT_FEAT_C_ENABLE
:
1793 case USB_PORT_FEAT_SUSPEND
:
1794 if (temp
& PORT_RESET
)
1797 if (temp
& PORT_SUSPEND
) {
1798 if ((temp
& PORT_PE
) == 0)
1800 /* resume signaling for 20 msec */
1801 temp
&= ~(PORT_RWC_BITS
);
1802 reg_write32(hcd
->regs
, HC_PORTSC1
,
1803 temp
| PORT_RESUME
);
1804 priv
->reset_done
= jiffies
+
1805 msecs_to_jiffies(20);
1808 case USB_PORT_FEAT_C_SUSPEND
:
1809 /* we auto-clear this feature */
1811 case USB_PORT_FEAT_POWER
:
1812 if (HCS_PPC(priv
->hcs_params
))
1813 reg_write32(hcd
->regs
, HC_PORTSC1
,
1814 temp
& ~PORT_POWER
);
1816 case USB_PORT_FEAT_C_CONNECTION
:
1817 reg_write32(hcd
->regs
, HC_PORTSC1
, temp
| PORT_CSC
);
1819 case USB_PORT_FEAT_C_OVER_CURRENT
:
1822 case USB_PORT_FEAT_C_RESET
:
1823 /* GetPortStatus clears reset */
1828 reg_read32(hcd
->regs
, HC_USBCMD
);
1830 case GetHubDescriptor
:
1831 isp1760_hub_descriptor(priv
, (struct usb_hub_descriptor
*)
1835 /* no hub-wide feature/status flags */
1839 if (!wIndex
|| wIndex
> ports
)
1843 temp
= reg_read32(hcd
->regs
, HC_PORTSC1
);
1845 /* wPortChange bits */
1846 if (temp
& PORT_CSC
)
1847 status
|= USB_PORT_STAT_C_CONNECTION
<< 16;
1850 /* whoever resumes must GetPortStatus to complete it!! */
1851 if (temp
& PORT_RESUME
) {
1852 dev_err(hcd
->self
.controller
, "Port resume should be skipped.\n");
1854 /* Remote Wakeup received? */
1855 if (!priv
->reset_done
) {
1856 /* resume signaling for 20 msec */
1857 priv
->reset_done
= jiffies
1858 + msecs_to_jiffies(20);
1859 /* check the port again */
1860 mod_timer(&hcd
->rh_timer
, priv
->reset_done
);
1863 /* resume completed? */
1864 else if (time_after_eq(jiffies
,
1865 priv
->reset_done
)) {
1866 status
|= USB_PORT_STAT_C_SUSPEND
<< 16;
1867 priv
->reset_done
= 0;
1869 /* stop resume signaling */
1870 temp
= reg_read32(hcd
->regs
, HC_PORTSC1
);
1871 reg_write32(hcd
->regs
, HC_PORTSC1
,
1872 temp
& ~(PORT_RWC_BITS
| PORT_RESUME
));
1873 retval
= handshake(hcd
, HC_PORTSC1
,
1874 PORT_RESUME
, 0, 2000 /* 2msec */);
1876 dev_err(hcd
->self
.controller
,
1877 "port %d resume error %d\n",
1878 wIndex
+ 1, retval
);
1881 temp
&= ~(PORT_SUSPEND
|PORT_RESUME
|(3<<10));
1885 /* whoever resets must GetPortStatus to complete it!! */
1886 if ((temp
& PORT_RESET
)
1887 && time_after_eq(jiffies
,
1888 priv
->reset_done
)) {
1889 status
|= USB_PORT_STAT_C_RESET
<< 16;
1890 priv
->reset_done
= 0;
1892 /* force reset to complete */
1893 reg_write32(hcd
->regs
, HC_PORTSC1
, temp
& ~PORT_RESET
);
1894 /* REVISIT: some hardware needs 550+ usec to clear
1895 * this bit; seems too long to spin routinely...
1897 retval
= handshake(hcd
, HC_PORTSC1
,
1898 PORT_RESET
, 0, 750);
1900 dev_err(hcd
->self
.controller
, "port %d reset error %d\n",
1901 wIndex
+ 1, retval
);
1905 /* see what we found out */
1906 temp
= check_reset_complete(hcd
, wIndex
,
1907 reg_read32(hcd
->regs
, HC_PORTSC1
));
1910 * Even if OWNER is set, there's no harm letting khubd
1911 * see the wPortStatus values (they should all be 0 except
1912 * for PORT_POWER anyway).
1915 if (temp
& PORT_OWNER
)
1916 dev_err(hcd
->self
.controller
, "PORT_OWNER is set\n");
1918 if (temp
& PORT_CONNECT
) {
1919 status
|= USB_PORT_STAT_CONNECTION
;
1920 /* status may be from integrated TT */
1921 status
|= USB_PORT_STAT_HIGH_SPEED
;
1924 status
|= USB_PORT_STAT_ENABLE
;
1925 if (temp
& (PORT_SUSPEND
|PORT_RESUME
))
1926 status
|= USB_PORT_STAT_SUSPEND
;
1927 if (temp
& PORT_RESET
)
1928 status
|= USB_PORT_STAT_RESET
;
1929 if (temp
& PORT_POWER
)
1930 status
|= USB_PORT_STAT_POWER
;
1932 put_unaligned(cpu_to_le32(status
), (__le32
*) buf
);
1936 case C_HUB_LOCAL_POWER
:
1937 case C_HUB_OVER_CURRENT
:
1938 /* no hub-wide feature/status flags */
1944 case SetPortFeature
:
1945 selector
= wIndex
>> 8;
1947 if (!wIndex
|| wIndex
> ports
)
1950 temp
= reg_read32(hcd
->regs
, HC_PORTSC1
);
1951 if (temp
& PORT_OWNER
)
1954 /* temp &= ~PORT_RWC_BITS; */
1956 case USB_PORT_FEAT_ENABLE
:
1957 reg_write32(hcd
->regs
, HC_PORTSC1
, temp
| PORT_PE
);
1960 case USB_PORT_FEAT_SUSPEND
:
1961 if ((temp
& PORT_PE
) == 0
1962 || (temp
& PORT_RESET
) != 0)
1965 reg_write32(hcd
->regs
, HC_PORTSC1
, temp
| PORT_SUSPEND
);
1967 case USB_PORT_FEAT_POWER
:
1968 if (HCS_PPC(priv
->hcs_params
))
1969 reg_write32(hcd
->regs
, HC_PORTSC1
,
1972 case USB_PORT_FEAT_RESET
:
1973 if (temp
& PORT_RESUME
)
1975 /* line status bits may report this as low speed,
1976 * which can be fine if this root hub has a
1977 * transaction translator built in.
1979 if ((temp
& (PORT_PE
|PORT_CONNECT
)) == PORT_CONNECT
1980 && PORT_USB11(temp
)) {
1987 * caller must wait, then call GetPortStatus
1988 * usb 2.0 spec says 50 ms resets on root
1990 priv
->reset_done
= jiffies
+
1991 msecs_to_jiffies(50);
1993 reg_write32(hcd
->regs
, HC_PORTSC1
, temp
);
1998 reg_read32(hcd
->regs
, HC_USBCMD
);
2003 /* "stall" on error */
2006 spin_unlock_irqrestore(&priv
->lock
, flags
);
2010 static int isp1760_get_frame(struct usb_hcd
*hcd
)
2012 struct isp1760_hcd
*priv
= hcd_to_priv(hcd
);
2015 fr
= reg_read32(hcd
->regs
, HC_FRINDEX
);
2016 return (fr
>> 3) % priv
->periodic_size
;
2019 static void isp1760_stop(struct usb_hcd
*hcd
)
2021 struct isp1760_hcd
*priv
= hcd_to_priv(hcd
);
2024 isp1760_hub_control(hcd
, ClearPortFeature
, USB_PORT_FEAT_POWER
, 1,
2028 spin_lock_irq(&priv
->lock
);
2031 temp
= reg_read32(hcd
->regs
, HC_HW_MODE_CTRL
);
2032 reg_write32(hcd
->regs
, HC_HW_MODE_CTRL
, temp
&= ~HW_GLOBAL_INTR_EN
);
2033 spin_unlock_irq(&priv
->lock
);
2035 reg_write32(hcd
->regs
, HC_CONFIGFLAG
, 0);
2038 static void isp1760_shutdown(struct usb_hcd
*hcd
)
2043 temp
= reg_read32(hcd
->regs
, HC_HW_MODE_CTRL
);
2044 reg_write32(hcd
->regs
, HC_HW_MODE_CTRL
, temp
&= ~HW_GLOBAL_INTR_EN
);
2046 command
= reg_read32(hcd
->regs
, HC_USBCMD
);
2047 command
&= ~CMD_RUN
;
2048 reg_write32(hcd
->regs
, HC_USBCMD
, command
);
2051 static const struct hc_driver isp1760_hc_driver
= {
2052 .description
= "isp1760-hcd",
2053 .product_desc
= "NXP ISP1760 USB Host Controller",
2054 .hcd_priv_size
= sizeof(struct isp1760_hcd
),
2056 .flags
= HCD_MEMORY
| HCD_USB2
,
2057 .reset
= isp1760_hc_setup
,
2058 .start
= isp1760_run
,
2059 .stop
= isp1760_stop
,
2060 .shutdown
= isp1760_shutdown
,
2061 .urb_enqueue
= isp1760_urb_enqueue
,
2062 .urb_dequeue
= isp1760_urb_dequeue
,
2063 .endpoint_disable
= isp1760_endpoint_disable
,
2064 .get_frame_number
= isp1760_get_frame
,
2065 .hub_status_data
= isp1760_hub_status_data
,
2066 .hub_control
= isp1760_hub_control
,
2069 int __init
init_kmem_once(void)
2071 urb_listitem_cachep
= kmem_cache_create("isp1760 urb_listitem",
2072 sizeof(struct urb_listitem
), 0, SLAB_TEMPORARY
|
2073 SLAB_MEM_SPREAD
, NULL
);
2075 if (!urb_listitem_cachep
)
2078 qtd_cachep
= kmem_cache_create("isp1760_qtd",
2079 sizeof(struct isp1760_qtd
), 0, SLAB_TEMPORARY
|
2080 SLAB_MEM_SPREAD
, NULL
);
2085 qh_cachep
= kmem_cache_create("isp1760_qh", sizeof(struct isp1760_qh
),
2086 0, SLAB_TEMPORARY
| SLAB_MEM_SPREAD
, NULL
);
2089 kmem_cache_destroy(qtd_cachep
);
2096 void deinit_kmem_cache(void)
2098 kmem_cache_destroy(qtd_cachep
);
2099 kmem_cache_destroy(qh_cachep
);
2100 kmem_cache_destroy(urb_listitem_cachep
);
2103 struct usb_hcd
*isp1760_register(phys_addr_t res_start
, resource_size_t res_len
,
2104 int irq
, unsigned long irqflags
,
2105 struct device
*dev
, const char *busname
,
2106 unsigned int devflags
)
2108 struct usb_hcd
*hcd
;
2109 struct isp1760_hcd
*priv
;
2113 return ERR_PTR(-ENODEV
);
2115 /* prevent usb-core allocating DMA pages */
2116 dev
->dma_mask
= NULL
;
2118 hcd
= usb_create_hcd(&isp1760_hc_driver
, dev
, dev_name(dev
));
2120 return ERR_PTR(-ENOMEM
);
2122 priv
= hcd_to_priv(hcd
);
2123 priv
->devflags
= devflags
;
2125 hcd
->regs
= ioremap(res_start
, res_len
);
2132 hcd
->rsrc_start
= res_start
;
2133 hcd
->rsrc_len
= res_len
;
2135 ret
= usb_add_hcd(hcd
, irq
, irqflags
);
2147 return ERR_PTR(ret
);
2150 MODULE_DESCRIPTION("Driver for the ISP1760 USB-controller from NXP");
2151 MODULE_AUTHOR("Sebastian Siewior <bigeasy@linuxtronix.de>");
2152 MODULE_LICENSE("GPL v2");