2 * Driver for the NXP ISP1760 chip
4 * However, the code might contain some bugs. What doesn't work for sure is:
7 e The interrupt line is configured as active low, level.
9 * (c) 2007 Sebastian Siewior <bigeasy@linutronix.de>
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/list.h>
16 #include <linux/usb.h>
17 #include <linux/usb/hcd.h>
18 #include <linux/debugfs.h>
19 #include <linux/uaccess.h>
22 #include <asm/unaligned.h>
23 #include <asm/cacheflush.h>
25 #include "isp1760-hcd.h"
27 static struct kmem_cache
*qtd_cachep
;
28 static struct kmem_cache
*qh_cachep
;
33 struct inter_packet_info atl_ints
[32];
34 struct inter_packet_info int_ints
[32];
35 struct memory_chunk memory_pool
[BLOCKS
];
37 /* periodic schedule support */
38 #define DEFAULT_I_TDPS 1024
39 unsigned periodic_size
;
41 unsigned long reset_done
;
42 unsigned long next_statechange
;
43 unsigned int devflags
;
46 static inline struct isp1760_hcd
*hcd_to_priv(struct usb_hcd
*hcd
)
48 return (struct isp1760_hcd
*) (hcd
->hcd_priv
);
50 static inline struct usb_hcd
*priv_to_hcd(struct isp1760_hcd
*priv
)
52 return container_of((void *) priv
, struct usb_hcd
, hcd_priv
);
55 /* Section 2.2 Host Controller Capability Registers */
56 #define HC_LENGTH(p) (((p)>>00)&0x00ff) /* bits 7:0 */
57 #define HC_VERSION(p) (((p)>>16)&0xffff) /* bits 31:16 */
58 #define HCS_INDICATOR(p) ((p)&(1 << 16)) /* true: has port indicators */
59 #define HCS_PPC(p) ((p)&(1 << 4)) /* true: port power control */
60 #define HCS_N_PORTS(p) (((p)>>0)&0xf) /* bits 3:0, ports on HC */
61 #define HCC_ISOC_CACHE(p) ((p)&(1 << 7)) /* true: can cache isoc frame */
62 #define HCC_ISOC_THRES(p) (((p)>>4)&0x7) /* bits 6:4, uframes cached */
64 /* Section 2.3 Host Controller Operational Registers */
65 #define CMD_LRESET (1<<7) /* partial reset (no ports, etc) */
66 #define CMD_RESET (1<<1) /* reset HC not bus */
67 #define CMD_RUN (1<<0) /* start/stop HC */
68 #define STS_PCD (1<<2) /* port change detect */
69 #define FLAG_CF (1<<0) /* true: we'll support "high speed" */
71 #define PORT_OWNER (1<<13) /* true: companion hc owns this port */
72 #define PORT_POWER (1<<12) /* true: has power (see PPC) */
73 #define PORT_USB11(x) (((x) & (3 << 10)) == (1 << 10)) /* USB 1.1 device */
74 #define PORT_RESET (1<<8) /* reset port */
75 #define PORT_SUSPEND (1<<7) /* suspend port */
76 #define PORT_RESUME (1<<6) /* resume it */
77 #define PORT_PE (1<<2) /* port enable */
78 #define PORT_CSC (1<<1) /* connect status change */
79 #define PORT_CONNECT (1<<0) /* device connected */
80 #define PORT_RWC_BITS (PORT_CSC)
83 struct isp1760_qtd
*hw_next
;
88 /* the rest is HCD-private */
89 struct list_head qtd_list
;
95 #define URB_COMPLETE_NOTIFY (1 << 0)
96 #define URB_ENQUEUED (1 << 1)
97 #define URB_TYPE_ATL (1 << 2)
98 #define URB_TYPE_INT (1 << 3)
102 /* first part defined by EHCI spec */
103 struct list_head qtd_list
;
104 struct isp1760_hcd
*priv
;
106 /* periodic schedule info */
107 unsigned short period
; /* polling interval */
108 struct usb_device
*dev
;
114 #define ehci_port_speed(priv, portsc) USB_PORT_STAT_HIGH_SPEED
116 static unsigned int isp1760_readl(__u32 __iomem
*regs
)
121 static void isp1760_writel(const unsigned int val
, __u32 __iomem
*regs
)
127 * The next two copy via MMIO data to/from the device. memcpy_{to|from}io()
128 * doesn't quite work because some people have to enforce 32-bit access
130 static void priv_read_copy(struct isp1760_hcd
*priv
, u32
*src
,
131 __u32 __iomem
*dst
, u32 len
)
137 printk(KERN_ERR
"ERROR: buffer: %p len: %d\n", src
, len
);
142 *src
= __raw_readl(dst
);
151 /* in case we have 3, 2 or 1 by left. The dst buffer may not be fully
154 val
= isp1760_readl(dst
);
166 static void priv_write_copy(const struct isp1760_hcd
*priv
, const u32
*src
,
167 __u32 __iomem
*dst
, u32 len
)
170 __raw_writel(*src
, dst
);
178 /* in case we have 3, 2 or 1 by left. The buffer is allocated and the
179 * extra bytes should not be read by the HW
182 __raw_writel(*src
, dst
);
185 /* memory management of the 60kb on the chip from 0x1000 to 0xffff */
186 static void init_memory(struct isp1760_hcd
*priv
)
192 for (i
= 0; i
< BLOCK_1_NUM
; i
++) {
193 priv
->memory_pool
[i
].start
= payload
;
194 priv
->memory_pool
[i
].size
= BLOCK_1_SIZE
;
195 priv
->memory_pool
[i
].free
= 1;
196 payload
+= priv
->memory_pool
[i
].size
;
200 for (i
= BLOCK_1_NUM
; i
< BLOCK_1_NUM
+ BLOCK_2_NUM
; i
++) {
201 priv
->memory_pool
[i
].start
= payload
;
202 priv
->memory_pool
[i
].size
= BLOCK_2_SIZE
;
203 priv
->memory_pool
[i
].free
= 1;
204 payload
+= priv
->memory_pool
[i
].size
;
208 for (i
= BLOCK_1_NUM
+ BLOCK_2_NUM
; i
< BLOCKS
; i
++) {
209 priv
->memory_pool
[i
].start
= payload
;
210 priv
->memory_pool
[i
].size
= BLOCK_3_SIZE
;
211 priv
->memory_pool
[i
].free
= 1;
212 payload
+= priv
->memory_pool
[i
].size
;
215 BUG_ON(payload
- priv
->memory_pool
[i
- 1].size
> PAYLOAD_SIZE
);
218 static u32
alloc_mem(struct isp1760_hcd
*priv
, u32 size
)
223 return ISP1760_NULL_POINTER
;
225 for (i
= 0; i
< BLOCKS
; i
++) {
226 if (priv
->memory_pool
[i
].size
>= size
&&
227 priv
->memory_pool
[i
].free
) {
229 priv
->memory_pool
[i
].free
= 0;
230 return priv
->memory_pool
[i
].start
;
234 printk(KERN_ERR
"ISP1760 MEM: can not allocate %d bytes of memory\n",
236 printk(KERN_ERR
"Current memory map:\n");
237 for (i
= 0; i
< BLOCKS
; i
++) {
238 printk(KERN_ERR
"Pool %2d size %4d status: %d\n",
239 i
, priv
->memory_pool
[i
].size
,
240 priv
->memory_pool
[i
].free
);
246 static void free_mem(struct isp1760_hcd
*priv
, u32 mem
)
250 if (mem
== ISP1760_NULL_POINTER
)
253 for (i
= 0; i
< BLOCKS
; i
++) {
254 if (priv
->memory_pool
[i
].start
== mem
) {
256 BUG_ON(priv
->memory_pool
[i
].free
);
258 priv
->memory_pool
[i
].free
= 1;
263 printk(KERN_ERR
"Trying to free not-here-allocated memory :%08x\n",
268 static void isp1760_init_regs(struct usb_hcd
*hcd
)
270 isp1760_writel(0, hcd
->regs
+ HC_BUFFER_STATUS_REG
);
271 isp1760_writel(NO_TRANSFER_ACTIVE
, hcd
->regs
+
272 HC_ATL_PTD_SKIPMAP_REG
);
273 isp1760_writel(NO_TRANSFER_ACTIVE
, hcd
->regs
+
274 HC_INT_PTD_SKIPMAP_REG
);
275 isp1760_writel(NO_TRANSFER_ACTIVE
, hcd
->regs
+
276 HC_ISO_PTD_SKIPMAP_REG
);
278 isp1760_writel(~NO_TRANSFER_ACTIVE
, hcd
->regs
+
279 HC_ATL_PTD_DONEMAP_REG
);
280 isp1760_writel(~NO_TRANSFER_ACTIVE
, hcd
->regs
+
281 HC_INT_PTD_DONEMAP_REG
);
282 isp1760_writel(~NO_TRANSFER_ACTIVE
, hcd
->regs
+
283 HC_ISO_PTD_DONEMAP_REG
);
286 static int handshake(struct isp1760_hcd
*priv
, void __iomem
*ptr
,
287 u32 mask
, u32 done
, int usec
)
292 result
= isp1760_readl(ptr
);
304 /* reset a non-running (STS_HALT == 1) controller */
305 static int ehci_reset(struct isp1760_hcd
*priv
)
308 struct usb_hcd
*hcd
= priv_to_hcd(priv
);
309 u32 command
= isp1760_readl(hcd
->regs
+ HC_USBCMD
);
311 command
|= CMD_RESET
;
312 isp1760_writel(command
, hcd
->regs
+ HC_USBCMD
);
313 hcd
->state
= HC_STATE_HALT
;
314 priv
->next_statechange
= jiffies
;
315 retval
= handshake(priv
, hcd
->regs
+ HC_USBCMD
,
316 CMD_RESET
, 0, 250 * 1000);
320 static void qh_destroy(struct isp1760_qh
*qh
)
322 BUG_ON(!list_empty(&qh
->qtd_list
));
323 kmem_cache_free(qh_cachep
, qh
);
326 static struct isp1760_qh
*isp1760_qh_alloc(struct isp1760_hcd
*priv
,
329 struct isp1760_qh
*qh
;
331 qh
= kmem_cache_zalloc(qh_cachep
, flags
);
335 INIT_LIST_HEAD(&qh
->qtd_list
);
340 /* magic numbers that can affect system performance */
341 #define EHCI_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
342 #define EHCI_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
343 #define EHCI_TUNE_RL_TT 0
344 #define EHCI_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
345 #define EHCI_TUNE_MULT_TT 1
346 #define EHCI_TUNE_FLS 2 /* (small) 256 frame schedule */
348 /* one-time init, only for memory state */
349 static int priv_init(struct usb_hcd
*hcd
)
351 struct isp1760_hcd
*priv
= hcd_to_priv(hcd
);
354 spin_lock_init(&priv
->lock
);
357 * hw default: 1K periodic list heads, one per frame.
358 * periodic_size can shrink by USBCMD update if hcc_params allows.
360 priv
->periodic_size
= DEFAULT_I_TDPS
;
362 /* controllers may cache some of the periodic schedule ... */
363 hcc_params
= isp1760_readl(hcd
->regs
+ HC_HCCPARAMS
);
364 /* full frame cache */
365 if (HCC_ISOC_CACHE(hcc_params
))
367 else /* N microframes cached */
368 priv
->i_thresh
= 2 + HCC_ISOC_THRES(hcc_params
);
373 static int isp1760_hc_setup(struct usb_hcd
*hcd
)
375 struct isp1760_hcd
*priv
= hcd_to_priv(hcd
);
379 /* Setup HW Mode Control: This assumes a level active-low interrupt */
380 hwmode
= HW_DATA_BUS_32BIT
;
382 if (priv
->devflags
& ISP1760_FLAG_BUS_WIDTH_16
)
383 hwmode
&= ~HW_DATA_BUS_32BIT
;
384 if (priv
->devflags
& ISP1760_FLAG_ANALOG_OC
)
385 hwmode
|= HW_ANA_DIGI_OC
;
386 if (priv
->devflags
& ISP1760_FLAG_DACK_POL_HIGH
)
387 hwmode
|= HW_DACK_POL_HIGH
;
388 if (priv
->devflags
& ISP1760_FLAG_DREQ_POL_HIGH
)
389 hwmode
|= HW_DREQ_POL_HIGH
;
390 if (priv
->devflags
& ISP1760_FLAG_INTR_POL_HIGH
)
391 hwmode
|= HW_INTR_HIGH_ACT
;
392 if (priv
->devflags
& ISP1760_FLAG_INTR_EDGE_TRIG
)
393 hwmode
|= HW_INTR_EDGE_TRIG
;
396 * We have to set this first in case we're in 16-bit mode.
397 * Write it twice to ensure correct upper bits if switching
400 isp1760_writel(hwmode
, hcd
->regs
+ HC_HW_MODE_CTRL
);
401 isp1760_writel(hwmode
, hcd
->regs
+ HC_HW_MODE_CTRL
);
403 isp1760_writel(0xdeadbabe, hcd
->regs
+ HC_SCRATCH_REG
);
404 /* Change bus pattern */
405 scratch
= isp1760_readl(hcd
->regs
+ HC_CHIP_ID_REG
);
406 scratch
= isp1760_readl(hcd
->regs
+ HC_SCRATCH_REG
);
407 if (scratch
!= 0xdeadbabe) {
408 printk(KERN_ERR
"ISP1760: Scratch test failed.\n");
413 isp1760_init_regs(hcd
);
416 isp1760_writel(SW_RESET_RESET_ALL
, hcd
->regs
+ HC_RESET_REG
);
419 isp1760_writel(SW_RESET_RESET_HC
, hcd
->regs
+ HC_RESET_REG
);
422 result
= ehci_reset(priv
);
428 isp1760_info(priv
, "bus width: %d, oc: %s\n",
429 (priv
->devflags
& ISP1760_FLAG_BUS_WIDTH_16
) ?
430 16 : 32, (priv
->devflags
& ISP1760_FLAG_ANALOG_OC
) ?
431 "analog" : "digital");
434 isp1760_writel(hwmode
| ALL_ATX_RESET
, hcd
->regs
+ HC_HW_MODE_CTRL
);
436 isp1760_writel(hwmode
, hcd
->regs
+ HC_HW_MODE_CTRL
);
438 isp1760_writel(INTERRUPT_ENABLE_MASK
, hcd
->regs
+ HC_INTERRUPT_REG
);
439 isp1760_writel(INTERRUPT_ENABLE_MASK
, hcd
->regs
+ HC_INTERRUPT_ENABLE
);
442 * PORT 1 Control register of the ISP1760 is the OTG control
443 * register on ISP1761. Since there is no OTG or device controller
444 * support in this driver, we use port 1 as a "normal" USB host port on
447 isp1760_writel(PORT1_POWER
| PORT1_INIT2
,
448 hcd
->regs
+ HC_PORT1_CTRL
);
451 priv
->hcs_params
= isp1760_readl(hcd
->regs
+ HC_HCSPARAMS
);
453 return priv_init(hcd
);
456 static void isp1760_init_maps(struct usb_hcd
*hcd
)
458 /*set last maps, for iso its only 1, else 32 tds bitmap*/
459 isp1760_writel(0x80000000, hcd
->regs
+ HC_ATL_PTD_LASTPTD_REG
);
460 isp1760_writel(0x80000000, hcd
->regs
+ HC_INT_PTD_LASTPTD_REG
);
461 isp1760_writel(0x00000001, hcd
->regs
+ HC_ISO_PTD_LASTPTD_REG
);
464 static void isp1760_enable_interrupts(struct usb_hcd
*hcd
)
466 isp1760_writel(0, hcd
->regs
+ HC_ATL_IRQ_MASK_AND_REG
);
467 isp1760_writel(0, hcd
->regs
+ HC_ATL_IRQ_MASK_OR_REG
);
468 isp1760_writel(0, hcd
->regs
+ HC_INT_IRQ_MASK_AND_REG
);
469 isp1760_writel(0, hcd
->regs
+ HC_INT_IRQ_MASK_OR_REG
);
470 isp1760_writel(0, hcd
->regs
+ HC_ISO_IRQ_MASK_AND_REG
);
471 isp1760_writel(0xffffffff, hcd
->regs
+ HC_ISO_IRQ_MASK_OR_REG
);
475 static int isp1760_run(struct usb_hcd
*hcd
)
477 struct isp1760_hcd
*priv
= hcd_to_priv(hcd
);
483 hcd
->uses_new_polling
= 1;
485 hcd
->state
= HC_STATE_RUNNING
;
486 isp1760_enable_interrupts(hcd
);
487 temp
= isp1760_readl(hcd
->regs
+ HC_HW_MODE_CTRL
);
488 isp1760_writel(temp
| HW_GLOBAL_INTR_EN
, hcd
->regs
+ HC_HW_MODE_CTRL
);
490 command
= isp1760_readl(hcd
->regs
+ HC_USBCMD
);
491 command
&= ~(CMD_LRESET
|CMD_RESET
);
493 isp1760_writel(command
, hcd
->regs
+ HC_USBCMD
);
495 retval
= handshake(priv
, hcd
->regs
+ HC_USBCMD
, CMD_RUN
, CMD_RUN
,
500 down_write(&ehci_cf_port_reset_rwsem
);
501 isp1760_writel(FLAG_CF
, hcd
->regs
+ HC_CONFIGFLAG
);
503 retval
= handshake(priv
, hcd
->regs
+ HC_CONFIGFLAG
, FLAG_CF
, FLAG_CF
,
505 up_write(&ehci_cf_port_reset_rwsem
);
509 chipid
= isp1760_readl(hcd
->regs
+ HC_CHIP_ID_REG
);
510 isp1760_info(priv
, "USB ISP %04x HW rev. %d started\n", chipid
& 0xffff,
513 /* PTD Register Init Part 2, Step 28 */
515 isp1760_init_maps(hcd
);
517 /* GRR this is run-once init(), being done every time the HC starts.
518 * So long as they're part of class devices, we can't do it init()
519 * since the class device isn't created that early.
524 static u32
base_to_chip(u32 base
)
526 return ((base
- 0x400) >> 3);
529 static void transform_into_atl(struct isp1760_hcd
*priv
, struct isp1760_qh
*qh
,
530 struct isp1760_qtd
*qtd
, struct urb
*urb
,
531 u32 payload
, struct ptd
*ptd
)
541 u32 nak
= NAK_COUNTER
;
543 /* according to 3.6.2, max packet len can not be > 0x400 */
544 maxpacket
= usb_maxpacket(urb
->dev
, urb
->pipe
, usb_pipeout(urb
->pipe
));
545 multi
= 1 + ((maxpacket
>> 11) & 0x3);
550 dw0
|= PTD_LENGTH(qtd
->length
);
551 dw0
|= PTD_MAXPACKET(maxpacket
);
552 dw0
|= PTD_ENDPOINT(usb_pipeendpoint(urb
->pipe
));
553 dw1
= usb_pipeendpoint(urb
->pipe
) >> 1;
556 dw1
|= PTD_DEVICE_ADDR(usb_pipedevice(urb
->pipe
));
558 pid_code
= qtd
->packet_type
;
559 dw1
|= PTD_PID_TOKEN(pid_code
);
561 if (usb_pipebulk(urb
->pipe
))
562 dw1
|= PTD_TRANS_BULK
;
563 else if (usb_pipeint(urb
->pipe
))
564 dw1
|= PTD_TRANS_INT
;
566 if (urb
->dev
->speed
!= USB_SPEED_HIGH
) {
567 /* split transaction */
569 dw1
|= PTD_TRANS_SPLIT
;
570 if (urb
->dev
->speed
== USB_SPEED_LOW
)
571 dw1
|= PTD_SE_USB_LOSPEED
;
573 dw1
|= PTD_PORT_NUM(urb
->dev
->ttport
);
574 dw1
|= PTD_HUB_NUM(urb
->dev
->tt
->hub
->devnum
);
576 /* SE bit for Split INT transfers */
577 if (usb_pipeint(urb
->pipe
) &&
578 (urb
->dev
->speed
== USB_SPEED_LOW
))
585 dw0
|= PTD_MULTI(multi
);
586 if (usb_pipecontrol(urb
->pipe
) || usb_pipebulk(urb
->pipe
))
593 dw2
|= PTD_DATA_START_ADDR(base_to_chip(payload
));
594 dw2
|= PTD_RL_CNT(rl
);
595 dw3
|= PTD_NAC_CNT(nak
);
598 if (usb_pipecontrol(urb
->pipe
))
599 dw3
|= PTD_DATA_TOGGLE(qtd
->toggle
);
606 dw3
|= PTD_CERR(ERR_COUNTER
);
608 memset(ptd
, 0, sizeof(*ptd
));
610 ptd
->dw0
= cpu_to_le32(dw0
);
611 ptd
->dw1
= cpu_to_le32(dw1
);
612 ptd
->dw2
= cpu_to_le32(dw2
);
613 ptd
->dw3
= cpu_to_le32(dw3
);
616 static void transform_add_int(struct isp1760_hcd
*priv
, struct isp1760_qh
*qh
,
617 struct isp1760_qtd
*qtd
, struct urb
*urb
,
618 u32 payload
, struct ptd
*ptd
)
627 maxpacket
= usb_maxpacket(urb
->dev
, urb
->pipe
, usb_pipeout(urb
->pipe
));
628 multi
= 1 + ((maxpacket
>> 11) & 0x3);
630 /* length of the data per uframe */
631 maxpacket
= multi
* maxpacket
;
633 numberofusofs
= urb
->transfer_buffer_length
/ maxpacket
;
634 if (urb
->transfer_buffer_length
% maxpacket
)
639 for (i
= 0; i
< numberofusofs
; i
++) {
644 if (urb
->dev
->speed
!= USB_SPEED_HIGH
) {
646 ptd
->dw5
= cpu_to_le32(0x1c);
648 if (qh
->period
>= 32)
649 period
= qh
->period
/ 2;
656 period
= qh
->period
/8;
663 if (qh
->period
>= 8) {
664 /* millisecond period */
665 period
= (period
<< 3);
667 /* usof based tranmsfers */
668 /* minimum 4 usofs */
673 ptd
->dw2
|= cpu_to_le32(period
);
674 ptd
->dw4
= cpu_to_le32(usof
);
677 static void transform_into_int(struct isp1760_hcd
*priv
, struct isp1760_qh
*qh
,
678 struct isp1760_qtd
*qtd
, struct urb
*urb
,
679 u32 payload
, struct ptd
*ptd
)
681 transform_into_atl(priv
, qh
, qtd
, urb
, payload
, ptd
);
682 transform_add_int(priv
, qh
, qtd
, urb
, payload
, ptd
);
685 static int qtd_fill(struct isp1760_qtd
*qtd
, void *databuffer
, size_t len
,
690 qtd
->data_buffer
= databuffer
;
691 qtd
->packet_type
= GET_QTD_TOKEN_TYPE(token
);
692 qtd
->toggle
= GET_DATA_TOGGLE(token
);
694 if (len
> HC_ATL_PL_SIZE
)
695 count
= HC_ATL_PL_SIZE
;
703 static int check_error(struct ptd
*ptd
)
708 dw3
= le32_to_cpu(ptd
->dw3
);
709 if (dw3
& DW3_HALT_BIT
) {
712 if (dw3
& DW3_ERROR_BIT
)
713 pr_err("error bit is set in DW3\n");
716 if (dw3
& DW3_QTD_ACTIVE
) {
717 printk(KERN_ERR
"transfer active bit is set DW3\n");
718 printk(KERN_ERR
"nak counter: %d, rl: %d\n", (dw3
>> 19) & 0xf,
719 (le32_to_cpu(ptd
->dw2
) >> 25) & 0xf);
725 static void check_int_err_status(u32 dw4
)
731 for (i
= 0; i
< 8; i
++) {
734 printk(KERN_ERR
"ERROR: under run , %d\n", i
);
738 printk(KERN_ERR
"ERROR: transaction error, %d\n", i
);
742 printk(KERN_ERR
"ERROR: babble error, %d\n", i
);
749 static void enqueue_one_qtd(struct isp1760_qtd
*qtd
, struct isp1760_hcd
*priv
,
753 struct usb_hcd
*hcd
= priv_to_hcd(priv
);
755 token
= qtd
->packet_type
;
757 if (qtd
->length
&& (qtd
->length
<= HC_ATL_PL_SIZE
)) {
763 priv_write_copy(priv
, qtd
->data_buffer
,
770 static void enqueue_one_atl_qtd(u32 atl_regs
, u32 payload
,
771 struct isp1760_hcd
*priv
, struct isp1760_qh
*qh
,
772 struct urb
*urb
, u32 slot
, struct isp1760_qtd
*qtd
)
775 struct usb_hcd
*hcd
= priv_to_hcd(priv
);
777 transform_into_atl(priv
, qh
, qtd
, urb
, payload
, &ptd
);
778 priv_write_copy(priv
, (u32
*)&ptd
, hcd
->regs
+ atl_regs
, sizeof(ptd
));
779 enqueue_one_qtd(qtd
, priv
, payload
);
781 priv
->atl_ints
[slot
].urb
= urb
;
782 priv
->atl_ints
[slot
].qh
= qh
;
783 priv
->atl_ints
[slot
].qtd
= qtd
;
784 priv
->atl_ints
[slot
].data_buffer
= qtd
->data_buffer
;
785 priv
->atl_ints
[slot
].payload
= payload
;
786 qtd
->status
|= URB_ENQUEUED
| URB_TYPE_ATL
;
787 qtd
->status
|= slot
<< 16;
790 static void enqueue_one_int_qtd(u32 int_regs
, u32 payload
,
791 struct isp1760_hcd
*priv
, struct isp1760_qh
*qh
,
792 struct urb
*urb
, u32 slot
, struct isp1760_qtd
*qtd
)
795 struct usb_hcd
*hcd
= priv_to_hcd(priv
);
797 transform_into_int(priv
, qh
, qtd
, urb
, payload
, &ptd
);
798 priv_write_copy(priv
, (u32
*)&ptd
, hcd
->regs
+ int_regs
, sizeof(ptd
));
799 enqueue_one_qtd(qtd
, priv
, payload
);
801 priv
->int_ints
[slot
].urb
= urb
;
802 priv
->int_ints
[slot
].qh
= qh
;
803 priv
->int_ints
[slot
].qtd
= qtd
;
804 priv
->int_ints
[slot
].data_buffer
= qtd
->data_buffer
;
805 priv
->int_ints
[slot
].payload
= payload
;
806 qtd
->status
|= URB_ENQUEUED
| URB_TYPE_INT
;
807 qtd
->status
|= slot
<< 16;
810 static void enqueue_an_ATL_packet(struct usb_hcd
*hcd
, struct isp1760_qh
*qh
,
811 struct isp1760_qtd
*qtd
)
813 struct isp1760_hcd
*priv
= hcd_to_priv(hcd
);
814 u32 skip_map
, or_map
;
817 u32 atl_regs
, payload
;
821 * When this function is called from the interrupt handler to enqueue
822 * a follow-up packet, the SKIP register gets written and read back
823 * almost immediately. With ISP1761, this register requires a delay of
824 * 195ns between a write and subsequent read (see section 15.1.1.3).
828 skip_map
= isp1760_readl(hcd
->regs
+ HC_ATL_PTD_SKIPMAP_REG
);
831 slot
= __ffs(skip_map
);
832 queue_entry
= 1 << slot
;
834 atl_regs
= ATL_REGS_OFFSET
+ slot
* sizeof(struct ptd
);
836 payload
= alloc_mem(priv
, qtd
->length
);
838 enqueue_one_atl_qtd(atl_regs
, payload
, priv
, qh
, qtd
->urb
, slot
, qtd
);
840 or_map
= isp1760_readl(hcd
->regs
+ HC_ATL_IRQ_MASK_OR_REG
);
841 or_map
|= queue_entry
;
842 isp1760_writel(or_map
, hcd
->regs
+ HC_ATL_IRQ_MASK_OR_REG
);
844 skip_map
&= ~queue_entry
;
845 isp1760_writel(skip_map
, hcd
->regs
+ HC_ATL_PTD_SKIPMAP_REG
);
847 buffstatus
= isp1760_readl(hcd
->regs
+ HC_BUFFER_STATUS_REG
);
848 buffstatus
|= ATL_BUFFER
;
849 isp1760_writel(buffstatus
, hcd
->regs
+ HC_BUFFER_STATUS_REG
);
852 static void enqueue_an_INT_packet(struct usb_hcd
*hcd
, struct isp1760_qh
*qh
,
853 struct isp1760_qtd
*qtd
)
855 struct isp1760_hcd
*priv
= hcd_to_priv(hcd
);
856 u32 skip_map
, or_map
;
859 u32 int_regs
, payload
;
863 * When this function is called from the interrupt handler to enqueue
864 * a follow-up packet, the SKIP register gets written and read back
865 * almost immediately. With ISP1761, this register requires a delay of
866 * 195ns between a write and subsequent read (see section 15.1.1.3).
870 skip_map
= isp1760_readl(hcd
->regs
+ HC_INT_PTD_SKIPMAP_REG
);
873 slot
= __ffs(skip_map
);
874 queue_entry
= 1 << slot
;
876 int_regs
= INT_REGS_OFFSET
+ slot
* sizeof(struct ptd
);
878 payload
= alloc_mem(priv
, qtd
->length
);
880 enqueue_one_int_qtd(int_regs
, payload
, priv
, qh
, qtd
->urb
, slot
, qtd
);
882 or_map
= isp1760_readl(hcd
->regs
+ HC_INT_IRQ_MASK_OR_REG
);
883 or_map
|= queue_entry
;
884 isp1760_writel(or_map
, hcd
->regs
+ HC_INT_IRQ_MASK_OR_REG
);
886 skip_map
&= ~queue_entry
;
887 isp1760_writel(skip_map
, hcd
->regs
+ HC_INT_PTD_SKIPMAP_REG
);
889 buffstatus
= isp1760_readl(hcd
->regs
+ HC_BUFFER_STATUS_REG
);
890 buffstatus
|= INT_BUFFER
;
891 isp1760_writel(buffstatus
, hcd
->regs
+ HC_BUFFER_STATUS_REG
);
894 static void isp1760_urb_done(struct isp1760_hcd
*priv
, struct urb
*urb
, int status
)
895 __releases(priv
->lock
)
896 __acquires(priv
->lock
)
898 if (!urb
->unlinked
) {
899 if (status
== -EINPROGRESS
)
903 if (usb_pipein(urb
->pipe
) && usb_pipetype(urb
->pipe
) != PIPE_CONTROL
) {
905 for (ptr
= urb
->transfer_buffer
;
906 ptr
< urb
->transfer_buffer
+ urb
->transfer_buffer_length
;
908 flush_dcache_page(virt_to_page(ptr
));
911 /* complete() can reenter this HCD */
912 usb_hcd_unlink_urb_from_ep(priv_to_hcd(priv
), urb
);
913 spin_unlock(&priv
->lock
);
914 usb_hcd_giveback_urb(priv_to_hcd(priv
), urb
, status
);
915 spin_lock(&priv
->lock
);
918 static void isp1760_qtd_free(struct isp1760_qtd
*qtd
)
920 kmem_cache_free(qtd_cachep
, qtd
);
923 static struct isp1760_qtd
*clean_this_qtd(struct isp1760_qtd
*qtd
)
925 struct isp1760_qtd
*tmp_qtd
;
927 tmp_qtd
= qtd
->hw_next
;
928 list_del(&qtd
->qtd_list
);
929 isp1760_qtd_free(qtd
);
934 * Remove this QTD from the QH list and free its memory. If this QTD
935 * isn't the last one than remove also his successor(s).
936 * Returns the QTD which is part of an new URB and should be enqueued.
938 static struct isp1760_qtd
*clean_up_qtdlist(struct isp1760_qtd
*qtd
)
940 struct isp1760_qtd
*tmp_qtd
;
944 tmp_qtd
= qtd
->hw_next
;
945 last_one
= qtd
->status
& URB_COMPLETE_NOTIFY
;
946 list_del(&qtd
->qtd_list
);
947 isp1760_qtd_free(qtd
);
949 } while (!last_one
&& qtd
);
954 static void do_atl_int(struct usb_hcd
*usb_hcd
)
956 struct isp1760_hcd
*priv
= hcd_to_priv(usb_hcd
);
957 u32 done_map
, skip_map
;
959 struct urb
*urb
= NULL
;
966 u32 status
= -EINVAL
;
968 struct isp1760_qtd
*qtd
;
969 struct isp1760_qh
*qh
;
973 done_map
= isp1760_readl(usb_hcd
->regs
+
974 HC_ATL_PTD_DONEMAP_REG
);
975 skip_map
= isp1760_readl(usb_hcd
->regs
+
976 HC_ATL_PTD_SKIPMAP_REG
);
978 or_map
= isp1760_readl(usb_hcd
->regs
+ HC_ATL_IRQ_MASK_OR_REG
);
980 isp1760_writel(or_map
, usb_hcd
->regs
+ HC_ATL_IRQ_MASK_OR_REG
);
982 atl_regs_base
= ATL_REGS_OFFSET
;
990 queue_entry
= __ffs(done_map
);
991 done_map
&= ~(1 << queue_entry
);
992 skip_map
|= 1 << queue_entry
;
994 atl_regs
= atl_regs_base
+ queue_entry
* sizeof(struct ptd
);
996 urb
= priv
->atl_ints
[queue_entry
].urb
;
997 qtd
= priv
->atl_ints
[queue_entry
].qtd
;
998 qh
= priv
->atl_ints
[queue_entry
].qh
;
999 payload
= priv
->atl_ints
[queue_entry
].payload
;
1002 printk(KERN_ERR
"qh is 0\n");
1005 isp1760_writel(atl_regs
+ ISP_BANK(0), usb_hcd
->regs
+
1007 isp1760_writel(payload
+ ISP_BANK(1), usb_hcd
->regs
+
1010 * write bank1 address twice to ensure the 90ns delay (time
1011 * between BANK0 write and the priv_read_copy() call is at
1012 * least 3*t_WHWL + 2*t_w11 = 3*25ns + 2*17ns = 109ns)
1014 isp1760_writel(payload
+ ISP_BANK(1), usb_hcd
->regs
+
1017 priv_read_copy(priv
, (u32
*)&ptd
, usb_hcd
->regs
+ atl_regs
+
1018 ISP_BANK(0), sizeof(ptd
));
1020 dw1
= le32_to_cpu(ptd
.dw1
);
1021 dw2
= le32_to_cpu(ptd
.dw2
);
1022 dw3
= le32_to_cpu(ptd
.dw3
);
1023 rl
= (dw2
>> 25) & 0x0f;
1024 nakcount
= (dw3
>> 19) & 0xf;
1026 /* Transfer Error, *but* active and no HALT -> reload */
1027 if ((dw3
& DW3_ERROR_BIT
) && (dw3
& DW3_QTD_ACTIVE
) &&
1028 !(dw3
& DW3_HALT_BIT
)) {
1031 length
= PTD_XFERRED_LENGTH(dw3
);
1032 printk(KERN_ERR
"Should reload now.... transfered %d "
1033 "of %zu\n", length
, qtd
->length
);
1037 if (!nakcount
&& (dw3
& DW3_QTD_ACTIVE
)) {
1041 * NAKs are handled in HW by the chip. Usually if the
1042 * device is not able to send data fast enough.
1043 * This happens mostly on slower hardware.
1045 printk(KERN_NOTICE
"Reloading ptd %p/%p... qh %p read: "
1046 "%d of %zu done: %08x cur: %08x\n", qtd
,
1047 urb
, qh
, PTD_XFERRED_LENGTH(dw3
),
1048 qtd
->length
, done_map
,
1049 (1 << queue_entry
));
1051 /* RL counter = ERR counter */
1052 dw3
&= ~(0xf << 19);
1054 dw3
&= ~(3 << (55 - 32));
1055 dw3
|= ERR_COUNTER
<< (55 - 32);
1058 * It is not needed to write skip map back because it
1059 * is unchanged. Just make sure that this entry is
1060 * unskipped once it gets written to the HW.
1062 skip_map
&= ~(1 << queue_entry
);
1063 or_map
= isp1760_readl(usb_hcd
->regs
+
1064 HC_ATL_IRQ_MASK_OR_REG
);
1065 or_map
|= 1 << queue_entry
;
1066 isp1760_writel(or_map
, usb_hcd
->regs
+
1067 HC_ATL_IRQ_MASK_OR_REG
);
1069 ptd
.dw3
= cpu_to_le32(dw3
);
1070 priv_write_copy(priv
, (u32
*)&ptd
, usb_hcd
->regs
+
1071 atl_regs
, sizeof(ptd
));
1073 ptd
.dw0
|= cpu_to_le32(PTD_VALID
);
1074 priv_write_copy(priv
, (u32
*)&ptd
, usb_hcd
->regs
+
1075 atl_regs
, sizeof(ptd
));
1077 buffstatus
= isp1760_readl(usb_hcd
->regs
+
1078 HC_BUFFER_STATUS_REG
);
1079 buffstatus
|= ATL_BUFFER
;
1080 isp1760_writel(buffstatus
, usb_hcd
->regs
+
1081 HC_BUFFER_STATUS_REG
);
1085 error
= check_error(&ptd
);
1088 priv
->atl_ints
[queue_entry
].qh
->toggle
= 0;
1089 priv
->atl_ints
[queue_entry
].qh
->ping
= 0;
1090 urb
->status
= -EPIPE
;
1093 if (usb_pipetype(urb
->pipe
) == PIPE_BULK
) {
1094 priv
->atl_ints
[queue_entry
].qh
->toggle
= dw3
&
1096 priv
->atl_ints
[queue_entry
].qh
->ping
= dw3
&
1101 length
= PTD_XFERRED_LENGTH(dw3
);
1103 switch (DW1_GET_PID(dw1
)) {
1105 priv_read_copy(priv
,
1106 priv
->atl_ints
[queue_entry
].data_buffer
,
1107 usb_hcd
->regs
+ payload
+ ISP_BANK(1),
1112 urb
->actual_length
+= length
;
1119 priv
->atl_ints
[queue_entry
].data_buffer
= NULL
;
1120 priv
->atl_ints
[queue_entry
].urb
= NULL
;
1121 priv
->atl_ints
[queue_entry
].qtd
= NULL
;
1122 priv
->atl_ints
[queue_entry
].qh
= NULL
;
1124 free_mem(priv
, payload
);
1126 isp1760_writel(skip_map
, usb_hcd
->regs
+
1127 HC_ATL_PTD_SKIPMAP_REG
);
1129 if (urb
->status
== -EPIPE
) {
1130 /* HALT was received */
1132 qtd
= clean_up_qtdlist(qtd
);
1133 isp1760_urb_done(priv
, urb
, urb
->status
);
1135 } else if (usb_pipebulk(urb
->pipe
) && (length
< qtd
->length
)) {
1136 /* short BULK received */
1138 if (urb
->transfer_flags
& URB_SHORT_NOT_OK
) {
1139 urb
->status
= -EREMOTEIO
;
1140 isp1760_dbg(priv
, "short bulk, %d instead %zu "
1141 "with URB_SHORT_NOT_OK flag.\n",
1142 length
, qtd
->length
);
1145 if (urb
->status
== -EINPROGRESS
)
1148 qtd
= clean_up_qtdlist(qtd
);
1150 isp1760_urb_done(priv
, urb
, urb
->status
);
1152 } else if (qtd
->status
& URB_COMPLETE_NOTIFY
) {
1153 /* that was the last qtd of that URB */
1155 if (urb
->status
== -EINPROGRESS
)
1158 qtd
= clean_this_qtd(qtd
);
1159 isp1760_urb_done(priv
, urb
, urb
->status
);
1162 /* next QTD of this URB */
1164 qtd
= clean_this_qtd(qtd
);
1169 enqueue_an_ATL_packet(usb_hcd
, qh
, qtd
);
1171 skip_map
= isp1760_readl(usb_hcd
->regs
+
1172 HC_ATL_PTD_SKIPMAP_REG
);
1176 static void do_intl_int(struct usb_hcd
*usb_hcd
)
1178 struct isp1760_hcd
*priv
= hcd_to_priv(usb_hcd
);
1179 u32 done_map
, skip_map
;
1181 struct urb
*urb
= NULL
;
1189 struct isp1760_qtd
*qtd
;
1190 struct isp1760_qh
*qh
;
1192 done_map
= isp1760_readl(usb_hcd
->regs
+
1193 HC_INT_PTD_DONEMAP_REG
);
1194 skip_map
= isp1760_readl(usb_hcd
->regs
+
1195 HC_INT_PTD_SKIPMAP_REG
);
1197 or_map
= isp1760_readl(usb_hcd
->regs
+ HC_INT_IRQ_MASK_OR_REG
);
1198 or_map
&= ~done_map
;
1199 isp1760_writel(or_map
, usb_hcd
->regs
+ HC_INT_IRQ_MASK_OR_REG
);
1201 int_regs_base
= INT_REGS_OFFSET
;
1207 queue_entry
= __ffs(done_map
);
1208 done_map
&= ~(1 << queue_entry
);
1209 skip_map
|= 1 << queue_entry
;
1211 int_regs
= int_regs_base
+ queue_entry
* sizeof(struct ptd
);
1212 urb
= priv
->int_ints
[queue_entry
].urb
;
1213 qtd
= priv
->int_ints
[queue_entry
].qtd
;
1214 qh
= priv
->int_ints
[queue_entry
].qh
;
1215 payload
= priv
->int_ints
[queue_entry
].payload
;
1218 printk(KERN_ERR
"(INT) qh is 0\n");
1222 isp1760_writel(int_regs
+ ISP_BANK(0), usb_hcd
->regs
+
1224 isp1760_writel(payload
+ ISP_BANK(1), usb_hcd
->regs
+
1227 * write bank1 address twice to ensure the 90ns delay (time
1228 * between BANK0 write and the priv_read_copy() call is at
1229 * least 3*t_WHWL + 2*t_w11 = 3*25ns + 2*17ns = 92ns)
1231 isp1760_writel(payload
+ ISP_BANK(1), usb_hcd
->regs
+
1234 priv_read_copy(priv
, (u32
*)&ptd
, usb_hcd
->regs
+ int_regs
+
1235 ISP_BANK(0), sizeof(ptd
));
1236 dw1
= le32_to_cpu(ptd
.dw1
);
1237 dw3
= le32_to_cpu(ptd
.dw3
);
1238 check_int_err_status(le32_to_cpu(ptd
.dw4
));
1240 error
= check_error(&ptd
);
1242 urb
->status
= -EPIPE
;
1243 priv
->int_ints
[queue_entry
].qh
->toggle
= 0;
1244 priv
->int_ints
[queue_entry
].qh
->ping
= 0;
1247 priv
->int_ints
[queue_entry
].qh
->toggle
=
1249 priv
->int_ints
[queue_entry
].qh
->ping
= dw3
& (1 << 26);
1252 if (urb
->dev
->speed
!= USB_SPEED_HIGH
)
1253 length
= PTD_XFERRED_LENGTH_LO(dw3
);
1255 length
= PTD_XFERRED_LENGTH(dw3
);
1258 switch (DW1_GET_PID(dw1
)) {
1260 priv_read_copy(priv
,
1261 priv
->int_ints
[queue_entry
].data_buffer
,
1262 usb_hcd
->regs
+ payload
+ ISP_BANK(1),
1266 urb
->actual_length
+= length
;
1273 priv
->int_ints
[queue_entry
].data_buffer
= NULL
;
1274 priv
->int_ints
[queue_entry
].urb
= NULL
;
1275 priv
->int_ints
[queue_entry
].qtd
= NULL
;
1276 priv
->int_ints
[queue_entry
].qh
= NULL
;
1278 isp1760_writel(skip_map
, usb_hcd
->regs
+
1279 HC_INT_PTD_SKIPMAP_REG
);
1280 free_mem(priv
, payload
);
1282 if (urb
->status
== -EPIPE
) {
1285 qtd
= clean_up_qtdlist(qtd
);
1286 isp1760_urb_done(priv
, urb
, urb
->status
);
1288 } else if (qtd
->status
& URB_COMPLETE_NOTIFY
) {
1290 if (urb
->status
== -EINPROGRESS
)
1293 qtd
= clean_this_qtd(qtd
);
1294 isp1760_urb_done(priv
, urb
, urb
->status
);
1297 /* next QTD of this URB */
1299 qtd
= clean_this_qtd(qtd
);
1304 enqueue_an_INT_packet(usb_hcd
, qh
, qtd
);
1306 skip_map
= isp1760_readl(usb_hcd
->regs
+
1307 HC_INT_PTD_SKIPMAP_REG
);
1311 #define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
1312 static struct isp1760_qh
*qh_make(struct isp1760_hcd
*priv
, struct urb
*urb
,
1315 struct isp1760_qh
*qh
;
1318 qh
= isp1760_qh_alloc(priv
, flags
);
1323 * init endpoint/device data for this QH
1325 is_input
= usb_pipein(urb
->pipe
);
1326 type
= usb_pipetype(urb
->pipe
);
1328 if (type
== PIPE_INTERRUPT
) {
1330 if (urb
->dev
->speed
== USB_SPEED_HIGH
) {
1332 qh
->period
= urb
->interval
>> 3;
1333 if (qh
->period
== 0 && urb
->interval
!= 1) {
1334 /* NOTE interval 2 or 4 uframes could work.
1335 * But interval 1 scheduling is simpler, and
1336 * includes high bandwidth.
1338 printk(KERN_ERR
"intr period %d uframes, NYET!",
1344 qh
->period
= urb
->interval
;
1348 /* support for tt scheduling, and access to toggles */
1351 if (!usb_pipecontrol(urb
->pipe
))
1352 usb_settoggle(urb
->dev
, usb_pipeendpoint(urb
->pipe
), !is_input
,
1358 * For control/bulk/interrupt, return QH with these TDs appended.
1359 * Allocates and initializes the QH if necessary.
1360 * Returns null if it can't allocate a QH it needs to.
1361 * If the QH has TDs (urbs) already, that's great.
1363 static struct isp1760_qh
*qh_append_tds(struct isp1760_hcd
*priv
,
1364 struct urb
*urb
, struct list_head
*qtd_list
, int epnum
,
1367 struct isp1760_qh
*qh
;
1368 struct isp1760_qtd
*qtd
;
1369 struct isp1760_qtd
*prev_qtd
;
1371 qh
= (struct isp1760_qh
*)*ptr
;
1373 /* can't sleep here, we have priv->lock... */
1374 qh
= qh_make(priv
, urb
, GFP_ATOMIC
);
1380 qtd
= list_entry(qtd_list
->next
, struct isp1760_qtd
,
1382 if (!list_empty(&qh
->qtd_list
))
1383 prev_qtd
= list_entry(qh
->qtd_list
.prev
,
1384 struct isp1760_qtd
, qtd_list
);
1388 list_splice(qtd_list
, qh
->qtd_list
.prev
);
1390 BUG_ON(prev_qtd
->hw_next
);
1391 prev_qtd
->hw_next
= qtd
;
1398 static void qtd_list_free(struct isp1760_hcd
*priv
, struct urb
*urb
,
1399 struct list_head
*qtd_list
)
1401 struct list_head
*entry
, *temp
;
1403 list_for_each_safe(entry
, temp
, qtd_list
) {
1404 struct isp1760_qtd
*qtd
;
1406 qtd
= list_entry(entry
, struct isp1760_qtd
, qtd_list
);
1407 list_del(&qtd
->qtd_list
);
1408 isp1760_qtd_free(qtd
);
1412 static int isp1760_prepare_enqueue(struct isp1760_hcd
*priv
, struct urb
*urb
,
1413 struct list_head
*qtd_list
, gfp_t mem_flags
, packet_enqueue
*p
)
1415 struct isp1760_qtd
*qtd
;
1417 unsigned long flags
;
1418 struct isp1760_qh
*qh
= NULL
;
1422 qtd
= list_entry(qtd_list
->next
, struct isp1760_qtd
, qtd_list
);
1423 epnum
= urb
->ep
->desc
.bEndpointAddress
;
1425 spin_lock_irqsave(&priv
->lock
, flags
);
1426 if (!HCD_HW_ACCESSIBLE(priv_to_hcd(priv
))) {
1430 rc
= usb_hcd_link_urb_to_ep(priv_to_hcd(priv
), urb
);
1434 qh
= urb
->ep
->hcpriv
;
1436 qh_busy
= !list_empty(&qh
->qtd_list
);
1440 qh
= qh_append_tds(priv
, urb
, qtd_list
, epnum
, &urb
->ep
->hcpriv
);
1442 usb_hcd_unlink_urb_from_ep(priv_to_hcd(priv
), urb
);
1448 p(priv_to_hcd(priv
), qh
, qtd
);
1451 spin_unlock_irqrestore(&priv
->lock
, flags
);
1453 qtd_list_free(priv
, urb
, qtd_list
);
1457 static struct isp1760_qtd
*isp1760_qtd_alloc(struct isp1760_hcd
*priv
,
1460 struct isp1760_qtd
*qtd
;
1462 qtd
= kmem_cache_zalloc(qtd_cachep
, flags
);
1464 INIT_LIST_HEAD(&qtd
->qtd_list
);
1470 * create a list of filled qtds for this URB; won't link into qh.
1472 static struct list_head
*qh_urb_transaction(struct isp1760_hcd
*priv
,
1473 struct urb
*urb
, struct list_head
*head
, gfp_t flags
)
1475 struct isp1760_qtd
*qtd
, *qtd_prev
;
1482 * URBs map to sequences of QTDs: one logical transaction
1484 qtd
= isp1760_qtd_alloc(priv
, flags
);
1488 list_add_tail(&qtd
->qtd_list
, head
);
1490 urb
->status
= -EINPROGRESS
;
1493 /* for split transactions, SplitXState initialized to zero */
1495 len
= urb
->transfer_buffer_length
;
1496 is_input
= usb_pipein(urb
->pipe
);
1497 if (usb_pipecontrol(urb
->pipe
)) {
1499 qtd_fill(qtd
, urb
->setup_packet
,
1500 sizeof(struct usb_ctrlrequest
),
1503 /* ... and always at least one more pid */
1504 token
^= DATA_TOGGLE
;
1506 qtd
= isp1760_qtd_alloc(priv
, flags
);
1510 qtd_prev
->hw_next
= qtd
;
1511 list_add_tail(&qtd
->qtd_list
, head
);
1513 /* for zero length DATA stages, STATUS is always IN */
1519 * data transfer stage: buffer setup
1521 buf
= urb
->transfer_buffer
;
1528 maxpacket
= max_packet(usb_maxpacket(urb
->dev
, urb
->pipe
, !is_input
));
1531 * buffer gets wrapped in one or more qtds;
1532 * last one may be "short" (including zero len)
1533 * and may serve as a control status ack
1539 printk(KERN_ERR
"buf is null, dma is %08lx len is %d\n",
1540 (long unsigned)urb
->transfer_dma
, len
);
1544 this_qtd_len
= qtd_fill(qtd
, buf
, len
, token
);
1545 len
-= this_qtd_len
;
1546 buf
+= this_qtd_len
;
1548 /* qh makes control packets use qtd toggle; maybe switch it */
1549 if ((maxpacket
& (this_qtd_len
+ (maxpacket
- 1))) == 0)
1550 token
^= DATA_TOGGLE
;
1556 qtd
= isp1760_qtd_alloc(priv
, flags
);
1560 qtd_prev
->hw_next
= qtd
;
1561 list_add_tail(&qtd
->qtd_list
, head
);
1565 * control requests may need a terminating data "status" ack;
1566 * bulk ones may need a terminating short packet (zero length).
1568 if (urb
->transfer_buffer_length
!= 0) {
1571 if (usb_pipecontrol(urb
->pipe
)) {
1573 /* "in" <--> "out" */
1576 token
|= DATA_TOGGLE
;
1577 } else if (usb_pipebulk(urb
->pipe
)
1578 && (urb
->transfer_flags
& URB_ZERO_PACKET
)
1579 && !(urb
->transfer_buffer_length
% maxpacket
)) {
1584 qtd
= isp1760_qtd_alloc(priv
, flags
);
1588 qtd_prev
->hw_next
= qtd
;
1589 list_add_tail(&qtd
->qtd_list
, head
);
1591 /* never any data in such packets */
1592 qtd_fill(qtd
, NULL
, 0, token
);
1596 qtd
->status
= URB_COMPLETE_NOTIFY
;
1600 qtd_list_free(priv
, urb
, head
);
1604 static int isp1760_urb_enqueue(struct usb_hcd
*hcd
, struct urb
*urb
,
1607 struct isp1760_hcd
*priv
= hcd_to_priv(hcd
);
1608 struct list_head qtd_list
;
1611 INIT_LIST_HEAD(&qtd_list
);
1613 switch (usb_pipetype(urb
->pipe
)) {
1617 if (!qh_urb_transaction(priv
, urb
, &qtd_list
, mem_flags
))
1619 pe
= enqueue_an_ATL_packet
;
1622 case PIPE_INTERRUPT
:
1623 if (!qh_urb_transaction(priv
, urb
, &qtd_list
, mem_flags
))
1625 pe
= enqueue_an_INT_packet
;
1628 case PIPE_ISOCHRONOUS
:
1629 printk(KERN_ERR
"PIPE_ISOCHRONOUS ain't supported\n");
1634 return isp1760_prepare_enqueue(priv
, urb
, &qtd_list
, mem_flags
, pe
);
1637 static int isp1760_urb_dequeue(struct usb_hcd
*hcd
, struct urb
*urb
,
1640 struct isp1760_hcd
*priv
= hcd_to_priv(hcd
);
1641 struct inter_packet_info
*ints
;
1643 u32 reg_base
, or_reg
, skip_reg
;
1644 unsigned long flags
;
1648 switch (usb_pipetype(urb
->pipe
)) {
1649 case PIPE_ISOCHRONOUS
:
1653 case PIPE_INTERRUPT
:
1654 ints
= priv
->int_ints
;
1655 reg_base
= INT_REGS_OFFSET
;
1656 or_reg
= HC_INT_IRQ_MASK_OR_REG
;
1657 skip_reg
= HC_INT_PTD_SKIPMAP_REG
;
1658 pe
= enqueue_an_INT_packet
;
1662 ints
= priv
->atl_ints
;
1663 reg_base
= ATL_REGS_OFFSET
;
1664 or_reg
= HC_ATL_IRQ_MASK_OR_REG
;
1665 skip_reg
= HC_ATL_PTD_SKIPMAP_REG
;
1666 pe
= enqueue_an_ATL_packet
;
1670 memset(&ptd
, 0, sizeof(ptd
));
1671 spin_lock_irqsave(&priv
->lock
, flags
);
1673 for (i
= 0; i
< 32; i
++) {
1674 if (ints
->urb
== urb
) {
1677 struct isp1760_qtd
*qtd
;
1678 struct isp1760_qh
*qh
= ints
->qh
;
1680 skip_map
= isp1760_readl(hcd
->regs
+ skip_reg
);
1682 isp1760_writel(skip_map
, hcd
->regs
+ skip_reg
);
1684 or_map
= isp1760_readl(hcd
->regs
+ or_reg
);
1685 or_map
&= ~(1 << i
);
1686 isp1760_writel(or_map
, hcd
->regs
+ or_reg
);
1688 priv_write_copy(priv
, (u32
*)&ptd
, hcd
->regs
+ reg_base
1689 + i
* sizeof(ptd
), sizeof(ptd
));
1691 qtd
= clean_up_qtdlist(qtd
);
1693 free_mem(priv
, ints
->payload
);
1698 ints
->data_buffer
= NULL
;
1701 isp1760_urb_done(priv
, urb
, status
);
1706 } else if (ints
->qtd
) {
1707 struct isp1760_qtd
*qtd
, *prev_qtd
= ints
->qtd
;
1709 for (qtd
= ints
->qtd
->hw_next
; qtd
; qtd
= qtd
->hw_next
) {
1710 if (qtd
->urb
== urb
) {
1711 prev_qtd
->hw_next
= clean_up_qtdlist(qtd
);
1712 isp1760_urb_done(priv
, urb
, status
);
1717 /* we found the urb before the end of the list */
1724 spin_unlock_irqrestore(&priv
->lock
, flags
);
1728 static irqreturn_t
isp1760_irq(struct usb_hcd
*usb_hcd
)
1730 struct isp1760_hcd
*priv
= hcd_to_priv(usb_hcd
);
1732 irqreturn_t irqret
= IRQ_NONE
;
1734 spin_lock(&priv
->lock
);
1736 if (!(usb_hcd
->state
& HC_STATE_RUNNING
))
1739 imask
= isp1760_readl(usb_hcd
->regs
+ HC_INTERRUPT_REG
);
1740 if (unlikely(!imask
))
1743 isp1760_writel(imask
, usb_hcd
->regs
+ HC_INTERRUPT_REG
);
1744 if (imask
& HC_ATL_INT
)
1745 do_atl_int(usb_hcd
);
1747 if (imask
& HC_INTL_INT
)
1748 do_intl_int(usb_hcd
);
1750 irqret
= IRQ_HANDLED
;
1752 spin_unlock(&priv
->lock
);
1756 static int isp1760_hub_status_data(struct usb_hcd
*hcd
, char *buf
)
1758 struct isp1760_hcd
*priv
= hcd_to_priv(hcd
);
1759 u32 temp
, status
= 0;
1762 unsigned long flags
;
1764 /* if !USB_SUSPEND, root hub timers won't get shut down ... */
1765 if (!HC_IS_RUNNING(hcd
->state
))
1768 /* init status to no-changes */
1772 spin_lock_irqsave(&priv
->lock
, flags
);
1773 temp
= isp1760_readl(hcd
->regs
+ HC_PORTSC1
);
1775 if (temp
& PORT_OWNER
) {
1776 if (temp
& PORT_CSC
) {
1778 isp1760_writel(temp
, hcd
->regs
+ HC_PORTSC1
);
1784 * Return status information even for ports with OWNER set.
1785 * Otherwise khubd wouldn't see the disconnect event when a
1786 * high-speed device is switched over to the companion
1787 * controller by the user.
1790 if ((temp
& mask
) != 0
1791 || ((temp
& PORT_RESUME
) != 0
1792 && time_after_eq(jiffies
,
1793 priv
->reset_done
))) {
1794 buf
[0] |= 1 << (0 + 1);
1798 spin_unlock_irqrestore(&priv
->lock
, flags
);
1799 return status
? retval
: 0;
1802 static void isp1760_hub_descriptor(struct isp1760_hcd
*priv
,
1803 struct usb_hub_descriptor
*desc
)
1805 int ports
= HCS_N_PORTS(priv
->hcs_params
);
1808 desc
->bDescriptorType
= 0x29;
1809 /* priv 1.0, 2.3.9 says 20ms max */
1810 desc
->bPwrOn2PwrGood
= 10;
1811 desc
->bHubContrCurrent
= 0;
1813 desc
->bNbrPorts
= ports
;
1814 temp
= 1 + (ports
/ 8);
1815 desc
->bDescLength
= 7 + 2 * temp
;
1817 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
1818 memset(&desc
->bitmap
[0], 0, temp
);
1819 memset(&desc
->bitmap
[temp
], 0xff, temp
);
1821 /* per-port overcurrent reporting */
1823 if (HCS_PPC(priv
->hcs_params
))
1824 /* per-port power control */
1827 /* no power switching */
1829 desc
->wHubCharacteristics
= cpu_to_le16(temp
);
1832 #define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
1834 static int check_reset_complete(struct isp1760_hcd
*priv
, int index
,
1835 u32 __iomem
*status_reg
, int port_status
)
1837 if (!(port_status
& PORT_CONNECT
))
1840 /* if reset finished and it's still not enabled -- handoff */
1841 if (!(port_status
& PORT_PE
)) {
1843 printk(KERN_ERR
"port %d full speed --> companion\n",
1846 port_status
|= PORT_OWNER
;
1847 port_status
&= ~PORT_RWC_BITS
;
1848 isp1760_writel(port_status
, status_reg
);
1851 printk(KERN_ERR
"port %d high speed\n", index
+ 1);
1856 static int isp1760_hub_control(struct usb_hcd
*hcd
, u16 typeReq
,
1857 u16 wValue
, u16 wIndex
, char *buf
, u16 wLength
)
1859 struct isp1760_hcd
*priv
= hcd_to_priv(hcd
);
1860 int ports
= HCS_N_PORTS(priv
->hcs_params
);
1861 u32 __iomem
*status_reg
= hcd
->regs
+ HC_PORTSC1
;
1863 unsigned long flags
;
1868 spin_lock_irqsave(&priv
->lock
, flags
);
1870 case ClearHubFeature
:
1872 case C_HUB_LOCAL_POWER
:
1873 case C_HUB_OVER_CURRENT
:
1874 /* no hub-wide feature/status flags */
1880 case ClearPortFeature
:
1881 if (!wIndex
|| wIndex
> ports
)
1884 temp
= isp1760_readl(status_reg
);
1887 * Even if OWNER is set, so the port is owned by the
1888 * companion controller, khubd needs to be able to clear
1889 * the port-change status bits (especially
1890 * USB_PORT_STAT_C_CONNECTION).
1894 case USB_PORT_FEAT_ENABLE
:
1895 isp1760_writel(temp
& ~PORT_PE
, status_reg
);
1897 case USB_PORT_FEAT_C_ENABLE
:
1899 case USB_PORT_FEAT_SUSPEND
:
1900 if (temp
& PORT_RESET
)
1903 if (temp
& PORT_SUSPEND
) {
1904 if ((temp
& PORT_PE
) == 0)
1906 /* resume signaling for 20 msec */
1907 temp
&= ~(PORT_RWC_BITS
);
1908 isp1760_writel(temp
| PORT_RESUME
,
1910 priv
->reset_done
= jiffies
+
1911 msecs_to_jiffies(20);
1914 case USB_PORT_FEAT_C_SUSPEND
:
1915 /* we auto-clear this feature */
1917 case USB_PORT_FEAT_POWER
:
1918 if (HCS_PPC(priv
->hcs_params
))
1919 isp1760_writel(temp
& ~PORT_POWER
, status_reg
);
1921 case USB_PORT_FEAT_C_CONNECTION
:
1922 isp1760_writel(temp
| PORT_CSC
,
1925 case USB_PORT_FEAT_C_OVER_CURRENT
:
1927 case USB_PORT_FEAT_C_RESET
:
1928 /* GetPortStatus clears reset */
1933 isp1760_readl(hcd
->regs
+ HC_USBCMD
);
1935 case GetHubDescriptor
:
1936 isp1760_hub_descriptor(priv
, (struct usb_hub_descriptor
*)
1940 /* no hub-wide feature/status flags */
1944 if (!wIndex
|| wIndex
> ports
)
1948 temp
= isp1760_readl(status_reg
);
1950 /* wPortChange bits */
1951 if (temp
& PORT_CSC
)
1952 status
|= USB_PORT_STAT_C_CONNECTION
<< 16;
1955 /* whoever resumes must GetPortStatus to complete it!! */
1956 if (temp
& PORT_RESUME
) {
1957 printk(KERN_ERR
"Port resume should be skipped.\n");
1959 /* Remote Wakeup received? */
1960 if (!priv
->reset_done
) {
1961 /* resume signaling for 20 msec */
1962 priv
->reset_done
= jiffies
1963 + msecs_to_jiffies(20);
1964 /* check the port again */
1965 mod_timer(&priv_to_hcd(priv
)->rh_timer
,
1969 /* resume completed? */
1970 else if (time_after_eq(jiffies
,
1971 priv
->reset_done
)) {
1972 status
|= USB_PORT_STAT_C_SUSPEND
<< 16;
1973 priv
->reset_done
= 0;
1975 /* stop resume signaling */
1976 temp
= isp1760_readl(status_reg
);
1978 temp
& ~(PORT_RWC_BITS
| PORT_RESUME
),
1980 retval
= handshake(priv
, status_reg
,
1981 PORT_RESUME
, 0, 2000 /* 2msec */);
1984 "port %d resume error %d\n",
1985 wIndex
+ 1, retval
);
1988 temp
&= ~(PORT_SUSPEND
|PORT_RESUME
|(3<<10));
1992 /* whoever resets must GetPortStatus to complete it!! */
1993 if ((temp
& PORT_RESET
)
1994 && time_after_eq(jiffies
,
1995 priv
->reset_done
)) {
1996 status
|= USB_PORT_STAT_C_RESET
<< 16;
1997 priv
->reset_done
= 0;
1999 /* force reset to complete */
2000 isp1760_writel(temp
& ~PORT_RESET
,
2002 /* REVISIT: some hardware needs 550+ usec to clear
2003 * this bit; seems too long to spin routinely...
2005 retval
= handshake(priv
, status_reg
,
2006 PORT_RESET
, 0, 750);
2008 isp1760_err(priv
, "port %d reset error %d\n",
2009 wIndex
+ 1, retval
);
2013 /* see what we found out */
2014 temp
= check_reset_complete(priv
, wIndex
, status_reg
,
2015 isp1760_readl(status_reg
));
2018 * Even if OWNER is set, there's no harm letting khubd
2019 * see the wPortStatus values (they should all be 0 except
2020 * for PORT_POWER anyway).
2023 if (temp
& PORT_OWNER
)
2024 printk(KERN_ERR
"Warning: PORT_OWNER is set\n");
2026 if (temp
& PORT_CONNECT
) {
2027 status
|= USB_PORT_STAT_CONNECTION
;
2028 /* status may be from integrated TT */
2029 status
|= ehci_port_speed(priv
, temp
);
2032 status
|= USB_PORT_STAT_ENABLE
;
2033 if (temp
& (PORT_SUSPEND
|PORT_RESUME
))
2034 status
|= USB_PORT_STAT_SUSPEND
;
2035 if (temp
& PORT_RESET
)
2036 status
|= USB_PORT_STAT_RESET
;
2037 if (temp
& PORT_POWER
)
2038 status
|= USB_PORT_STAT_POWER
;
2040 put_unaligned(cpu_to_le32(status
), (__le32
*) buf
);
2044 case C_HUB_LOCAL_POWER
:
2045 case C_HUB_OVER_CURRENT
:
2046 /* no hub-wide feature/status flags */
2052 case SetPortFeature
:
2053 selector
= wIndex
>> 8;
2055 if (!wIndex
|| wIndex
> ports
)
2058 temp
= isp1760_readl(status_reg
);
2059 if (temp
& PORT_OWNER
)
2062 /* temp &= ~PORT_RWC_BITS; */
2064 case USB_PORT_FEAT_ENABLE
:
2065 isp1760_writel(temp
| PORT_PE
, status_reg
);
2068 case USB_PORT_FEAT_SUSPEND
:
2069 if ((temp
& PORT_PE
) == 0
2070 || (temp
& PORT_RESET
) != 0)
2073 isp1760_writel(temp
| PORT_SUSPEND
, status_reg
);
2075 case USB_PORT_FEAT_POWER
:
2076 if (HCS_PPC(priv
->hcs_params
))
2077 isp1760_writel(temp
| PORT_POWER
,
2080 case USB_PORT_FEAT_RESET
:
2081 if (temp
& PORT_RESUME
)
2083 /* line status bits may report this as low speed,
2084 * which can be fine if this root hub has a
2085 * transaction translator built in.
2087 if ((temp
& (PORT_PE
|PORT_CONNECT
)) == PORT_CONNECT
2088 && PORT_USB11(temp
)) {
2095 * caller must wait, then call GetPortStatus
2096 * usb 2.0 spec says 50 ms resets on root
2098 priv
->reset_done
= jiffies
+
2099 msecs_to_jiffies(50);
2101 isp1760_writel(temp
, status_reg
);
2106 isp1760_readl(hcd
->regs
+ HC_USBCMD
);
2111 /* "stall" on error */
2114 spin_unlock_irqrestore(&priv
->lock
, flags
);
2118 static void isp1760_endpoint_disable(struct usb_hcd
*usb_hcd
,
2119 struct usb_host_endpoint
*ep
)
2121 struct isp1760_hcd
*priv
= hcd_to_priv(usb_hcd
);
2122 struct isp1760_qh
*qh
;
2123 struct isp1760_qtd
*qtd
;
2124 unsigned long flags
;
2126 spin_lock_irqsave(&priv
->lock
, flags
);
2133 /* more than entry might get removed */
2134 if (list_empty(&qh
->qtd_list
))
2137 qtd
= list_first_entry(&qh
->qtd_list
, struct isp1760_qtd
,
2140 if (qtd
->status
& URB_ENQUEUED
) {
2142 spin_unlock_irqrestore(&priv
->lock
, flags
);
2143 isp1760_urb_dequeue(usb_hcd
, qtd
->urb
, -ECONNRESET
);
2144 spin_lock_irqsave(&priv
->lock
, flags
);
2149 clean_up_qtdlist(qtd
);
2150 isp1760_urb_done(priv
, urb
, -ECONNRESET
);
2155 /* remove requests and leak them.
2156 * ATL are pretty fast done, INT could take a while...
2157 * The latter shoule be removed
2160 spin_unlock_irqrestore(&priv
->lock
, flags
);
2163 static int isp1760_get_frame(struct usb_hcd
*hcd
)
2165 struct isp1760_hcd
*priv
= hcd_to_priv(hcd
);
2168 fr
= isp1760_readl(hcd
->regs
+ HC_FRINDEX
);
2169 return (fr
>> 3) % priv
->periodic_size
;
2172 static void isp1760_stop(struct usb_hcd
*hcd
)
2174 struct isp1760_hcd
*priv
= hcd_to_priv(hcd
);
2177 isp1760_hub_control(hcd
, ClearPortFeature
, USB_PORT_FEAT_POWER
, 1,
2181 spin_lock_irq(&priv
->lock
);
2184 temp
= isp1760_readl(hcd
->regs
+ HC_HW_MODE_CTRL
);
2185 isp1760_writel(temp
&= ~HW_GLOBAL_INTR_EN
, hcd
->regs
+ HC_HW_MODE_CTRL
);
2186 spin_unlock_irq(&priv
->lock
);
2188 isp1760_writel(0, hcd
->regs
+ HC_CONFIGFLAG
);
2191 static void isp1760_shutdown(struct usb_hcd
*hcd
)
2196 temp
= isp1760_readl(hcd
->regs
+ HC_HW_MODE_CTRL
);
2197 isp1760_writel(temp
&= ~HW_GLOBAL_INTR_EN
, hcd
->regs
+ HC_HW_MODE_CTRL
);
2199 command
= isp1760_readl(hcd
->regs
+ HC_USBCMD
);
2200 command
&= ~CMD_RUN
;
2201 isp1760_writel(command
, hcd
->regs
+ HC_USBCMD
);
2204 static const struct hc_driver isp1760_hc_driver
= {
2205 .description
= "isp1760-hcd",
2206 .product_desc
= "NXP ISP1760 USB Host Controller",
2207 .hcd_priv_size
= sizeof(struct isp1760_hcd
),
2209 .flags
= HCD_MEMORY
| HCD_USB2
,
2210 .reset
= isp1760_hc_setup
,
2211 .start
= isp1760_run
,
2212 .stop
= isp1760_stop
,
2213 .shutdown
= isp1760_shutdown
,
2214 .urb_enqueue
= isp1760_urb_enqueue
,
2215 .urb_dequeue
= isp1760_urb_dequeue
,
2216 .endpoint_disable
= isp1760_endpoint_disable
,
2217 .get_frame_number
= isp1760_get_frame
,
2218 .hub_status_data
= isp1760_hub_status_data
,
2219 .hub_control
= isp1760_hub_control
,
2222 int __init
init_kmem_once(void)
2224 qtd_cachep
= kmem_cache_create("isp1760_qtd",
2225 sizeof(struct isp1760_qtd
), 0, SLAB_TEMPORARY
|
2226 SLAB_MEM_SPREAD
, NULL
);
2231 qh_cachep
= kmem_cache_create("isp1760_qh", sizeof(struct isp1760_qh
),
2232 0, SLAB_TEMPORARY
| SLAB_MEM_SPREAD
, NULL
);
2235 kmem_cache_destroy(qtd_cachep
);
2242 void deinit_kmem_cache(void)
2244 kmem_cache_destroy(qtd_cachep
);
2245 kmem_cache_destroy(qh_cachep
);
2248 struct usb_hcd
*isp1760_register(phys_addr_t res_start
, resource_size_t res_len
,
2249 int irq
, unsigned long irqflags
,
2250 struct device
*dev
, const char *busname
,
2251 unsigned int devflags
)
2253 struct usb_hcd
*hcd
;
2254 struct isp1760_hcd
*priv
;
2258 return ERR_PTR(-ENODEV
);
2260 /* prevent usb-core allocating DMA pages */
2261 dev
->dma_mask
= NULL
;
2263 hcd
= usb_create_hcd(&isp1760_hc_driver
, dev
, dev_name(dev
));
2265 return ERR_PTR(-ENOMEM
);
2267 priv
= hcd_to_priv(hcd
);
2268 priv
->devflags
= devflags
;
2270 hcd
->regs
= ioremap(res_start
, res_len
);
2277 hcd
->rsrc_start
= res_start
;
2278 hcd
->rsrc_len
= res_len
;
2280 ret
= usb_add_hcd(hcd
, irq
, irqflags
);
2292 return ERR_PTR(ret
);
2295 MODULE_DESCRIPTION("Driver for the ISP1760 USB-controller from NXP");
2296 MODULE_AUTHOR("Sebastian Siewior <bigeasy@linuxtronix.de>");
2297 MODULE_LICENSE("GPL v2");