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/debugfs.h>
18 #include <linux/uaccess.h>
20 #include <asm/unaligned.h>
22 #include "../core/hcd.h"
23 #include "isp1760-hcd.h"
25 static struct kmem_cache
*qtd_cachep
;
26 static struct kmem_cache
*qh_cachep
;
31 struct inter_packet_info atl_ints
[32];
32 struct inter_packet_info int_ints
[32];
33 struct memory_chunk memory_pool
[BLOCKS
];
35 /* periodic schedule support */
36 #define DEFAULT_I_TDPS 1024
37 unsigned periodic_size
;
39 unsigned long reset_done
;
40 unsigned long next_statechange
;
43 static inline struct isp1760_hcd
*hcd_to_priv(struct usb_hcd
*hcd
)
45 return (struct isp1760_hcd
*) (hcd
->hcd_priv
);
47 static inline struct usb_hcd
*priv_to_hcd(struct isp1760_hcd
*priv
)
49 return container_of((void *) priv
, struct usb_hcd
, hcd_priv
);
52 /* Section 2.2 Host Controller Capability Registers */
53 #define HC_LENGTH(p) (((p)>>00)&0x00ff) /* bits 7:0 */
54 #define HC_VERSION(p) (((p)>>16)&0xffff) /* bits 31:16 */
55 #define HCS_INDICATOR(p) ((p)&(1 << 16)) /* true: has port indicators */
56 #define HCS_PPC(p) ((p)&(1 << 4)) /* true: port power control */
57 #define HCS_N_PORTS(p) (((p)>>0)&0xf) /* bits 3:0, ports on HC */
58 #define HCC_ISOC_CACHE(p) ((p)&(1 << 7)) /* true: can cache isoc frame */
59 #define HCC_ISOC_THRES(p) (((p)>>4)&0x7) /* bits 6:4, uframes cached */
61 /* Section 2.3 Host Controller Operational Registers */
62 #define CMD_LRESET (1<<7) /* partial reset (no ports, etc) */
63 #define CMD_RESET (1<<1) /* reset HC not bus */
64 #define CMD_RUN (1<<0) /* start/stop HC */
65 #define STS_PCD (1<<2) /* port change detect */
66 #define FLAG_CF (1<<0) /* true: we'll support "high speed" */
68 #define PORT_OWNER (1<<13) /* true: companion hc owns this port */
69 #define PORT_POWER (1<<12) /* true: has power (see PPC) */
70 #define PORT_USB11(x) (((x) & (3 << 10)) == (1 << 10)) /* USB 1.1 device */
71 #define PORT_RESET (1<<8) /* reset port */
72 #define PORT_SUSPEND (1<<7) /* suspend port */
73 #define PORT_RESUME (1<<6) /* resume it */
74 #define PORT_PE (1<<2) /* port enable */
75 #define PORT_CSC (1<<1) /* connect status change */
76 #define PORT_CONNECT (1<<0) /* device connected */
77 #define PORT_RWC_BITS (PORT_CSC)
80 struct isp1760_qtd
*hw_next
;
85 /* the rest is HCD-private */
86 struct list_head qtd_list
;
92 #define URB_COMPLETE_NOTIFY (1 << 0)
93 #define URB_ENQUEUED (1 << 1)
94 #define URB_TYPE_ATL (1 << 2)
95 #define URB_TYPE_INT (1 << 3)
99 /* first part defined by EHCI spec */
100 struct list_head qtd_list
;
101 struct isp1760_hcd
*priv
;
103 /* periodic schedule info */
104 unsigned short period
; /* polling interval */
105 struct usb_device
*dev
;
111 #define ehci_port_speed(priv, portsc) (1 << USB_PORT_FEAT_HIGHSPEED)
113 static unsigned int isp1760_readl(__u32 __iomem
*regs
)
118 static void isp1760_writel(const unsigned int val
, __u32 __iomem
*regs
)
124 * The next two copy via MMIO data to/from the device. memcpy_{to|from}io()
125 * doesn't quite work because some people have to enforce 32-bit access
127 static void priv_read_copy(struct isp1760_hcd
*priv
, u32
*src
,
128 __u32 __iomem
*dst
, u32 offset
, u32 len
)
130 struct usb_hcd
*hcd
= priv_to_hcd(priv
);
135 printk(KERN_ERR
"ERROR: buffer: %p len: %d\n", src
, len
);
138 isp1760_writel(offset
, hcd
->regs
+ HC_MEMORY_REG
);
140 * 90nsec delay, the spec says something how this could be avoided.
145 *src
= __raw_readl(dst
);
154 /* in case we have 3, 2 or 1 by left. The dst buffer may not be fully
157 val
= isp1760_readl(dst
);
169 static void priv_write_copy(const struct isp1760_hcd
*priv
, const u32
*src
,
170 __u32 __iomem
*dst
, u32 len
)
173 __raw_writel(*src
, dst
);
181 /* in case we have 3, 2 or 1 by left. The buffer is allocated and the
182 * extra bytes should not be read by the HW
185 __raw_writel(*src
, dst
);
188 /* memory management of the 60kb on the chip from 0x1000 to 0xffff */
189 static void init_memory(struct isp1760_hcd
*priv
)
195 for (i
= 0; i
< BLOCK_1_NUM
; i
++) {
196 priv
->memory_pool
[i
].start
= payload
;
197 priv
->memory_pool
[i
].size
= BLOCK_1_SIZE
;
198 priv
->memory_pool
[i
].free
= 1;
199 payload
+= priv
->memory_pool
[i
].size
;
203 for (i
= BLOCK_1_NUM
; i
< BLOCK_1_NUM
+ BLOCK_2_NUM
; i
++) {
204 priv
->memory_pool
[i
].start
= payload
;
205 priv
->memory_pool
[i
].size
= BLOCK_2_SIZE
;
206 priv
->memory_pool
[i
].free
= 1;
207 payload
+= priv
->memory_pool
[i
].size
;
211 for (i
= BLOCK_1_NUM
+ BLOCK_2_NUM
; i
< BLOCKS
; i
++) {
212 priv
->memory_pool
[i
].start
= payload
;
213 priv
->memory_pool
[i
].size
= BLOCK_3_SIZE
;
214 priv
->memory_pool
[i
].free
= 1;
215 payload
+= priv
->memory_pool
[i
].size
;
218 BUG_ON(payload
- priv
->memory_pool
[i
- 1].size
> PAYLOAD_SIZE
);
221 static u32
alloc_mem(struct isp1760_hcd
*priv
, u32 size
)
226 return ISP1760_NULL_POINTER
;
228 for (i
= 0; i
< BLOCKS
; i
++) {
229 if (priv
->memory_pool
[i
].size
>= size
&&
230 priv
->memory_pool
[i
].free
) {
232 priv
->memory_pool
[i
].free
= 0;
233 return priv
->memory_pool
[i
].start
;
237 printk(KERN_ERR
"ISP1760 MEM: can not allocate %d bytes of memory\n",
239 printk(KERN_ERR
"Current memory map:\n");
240 for (i
= 0; i
< BLOCKS
; i
++) {
241 printk(KERN_ERR
"Pool %2d size %4d status: %d\n",
242 i
, priv
->memory_pool
[i
].size
,
243 priv
->memory_pool
[i
].free
);
245 /* XXX maybe -ENOMEM could be possible */
250 static void free_mem(struct isp1760_hcd
*priv
, u32 mem
)
254 if (mem
== ISP1760_NULL_POINTER
)
257 for (i
= 0; i
< BLOCKS
; i
++) {
258 if (priv
->memory_pool
[i
].start
== mem
) {
260 BUG_ON(priv
->memory_pool
[i
].free
);
262 priv
->memory_pool
[i
].free
= 1;
267 printk(KERN_ERR
"Trying to free not-here-allocated memory :%08x\n",
272 static void isp1760_init_regs(struct usb_hcd
*hcd
)
274 isp1760_writel(0, hcd
->regs
+ HC_BUFFER_STATUS_REG
);
275 isp1760_writel(NO_TRANSFER_ACTIVE
, hcd
->regs
+
276 HC_ATL_PTD_SKIPMAP_REG
);
277 isp1760_writel(NO_TRANSFER_ACTIVE
, hcd
->regs
+
278 HC_INT_PTD_SKIPMAP_REG
);
279 isp1760_writel(NO_TRANSFER_ACTIVE
, hcd
->regs
+
280 HC_ISO_PTD_SKIPMAP_REG
);
282 isp1760_writel(~NO_TRANSFER_ACTIVE
, hcd
->regs
+
283 HC_ATL_PTD_DONEMAP_REG
);
284 isp1760_writel(~NO_TRANSFER_ACTIVE
, hcd
->regs
+
285 HC_INT_PTD_DONEMAP_REG
);
286 isp1760_writel(~NO_TRANSFER_ACTIVE
, hcd
->regs
+
287 HC_ISO_PTD_DONEMAP_REG
);
290 static int handshake(struct isp1760_hcd
*priv
, void __iomem
*ptr
,
291 u32 mask
, u32 done
, int usec
)
296 result
= isp1760_readl(ptr
);
308 /* reset a non-running (STS_HALT == 1) controller */
309 static int ehci_reset(struct isp1760_hcd
*priv
)
312 struct usb_hcd
*hcd
= priv_to_hcd(priv
);
313 u32 command
= isp1760_readl(hcd
->regs
+ HC_USBCMD
);
315 command
|= CMD_RESET
;
316 isp1760_writel(command
, hcd
->regs
+ HC_USBCMD
);
317 hcd
->state
= HC_STATE_HALT
;
318 priv
->next_statechange
= jiffies
;
319 retval
= handshake(priv
, hcd
->regs
+ HC_USBCMD
,
320 CMD_RESET
, 0, 250 * 1000);
324 static void qh_destroy(struct isp1760_qh
*qh
)
326 BUG_ON(!list_empty(&qh
->qtd_list
));
327 kmem_cache_free(qh_cachep
, qh
);
330 static struct isp1760_qh
*isp1760_qh_alloc(struct isp1760_hcd
*priv
,
333 struct isp1760_qh
*qh
;
335 qh
= kmem_cache_zalloc(qh_cachep
, flags
);
339 INIT_LIST_HEAD(&qh
->qtd_list
);
344 /* magic numbers that can affect system performance */
345 #define EHCI_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
346 #define EHCI_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
347 #define EHCI_TUNE_RL_TT 0
348 #define EHCI_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
349 #define EHCI_TUNE_MULT_TT 1
350 #define EHCI_TUNE_FLS 2 /* (small) 256 frame schedule */
352 /* one-time init, only for memory state */
353 static int priv_init(struct usb_hcd
*hcd
)
355 struct isp1760_hcd
*priv
= hcd_to_priv(hcd
);
358 spin_lock_init(&priv
->lock
);
361 * hw default: 1K periodic list heads, one per frame.
362 * periodic_size can shrink by USBCMD update if hcc_params allows.
364 priv
->periodic_size
= DEFAULT_I_TDPS
;
366 /* controllers may cache some of the periodic schedule ... */
367 hcc_params
= isp1760_readl(hcd
->regs
+ HC_HCCPARAMS
);
368 /* full frame cache */
369 if (HCC_ISOC_CACHE(hcc_params
))
371 else /* N microframes cached */
372 priv
->i_thresh
= 2 + HCC_ISOC_THRES(hcc_params
);
377 static int isp1760_hc_setup(struct usb_hcd
*hcd
)
379 struct isp1760_hcd
*priv
= hcd_to_priv(hcd
);
383 isp1760_writel(0xdeadbabe, hcd
->regs
+ HC_SCRATCH_REG
);
384 scratch
= isp1760_readl(hcd
->regs
+ HC_SCRATCH_REG
);
385 if (scratch
!= 0xdeadbabe) {
386 printk(KERN_ERR
"ISP1760: Scratch test failed.\n");
391 isp1760_init_regs(hcd
);
394 isp1760_writel(SW_RESET_RESET_ALL
, hcd
->regs
+ HC_RESET_REG
);
397 isp1760_writel(SW_RESET_RESET_HC
, hcd
->regs
+ HC_RESET_REG
);
400 result
= ehci_reset(priv
);
406 isp1760_writel(INTERRUPT_ENABLE_MASK
, hcd
->regs
+ HC_INTERRUPT_REG
);
407 isp1760_writel(INTERRUPT_ENABLE_MASK
, hcd
->regs
+ HC_INTERRUPT_ENABLE
);
410 scratch
= isp1760_readl(hcd
->regs
+ HC_HW_MODE_CTRL
);
411 isp1760_writel(scratch
| ALL_ATX_RESET
, hcd
->regs
+ HC_HW_MODE_CTRL
);
413 isp1760_writel(scratch
, hcd
->regs
+ HC_HW_MODE_CTRL
);
415 isp1760_writel(PORT1_POWER
| PORT1_INIT2
, hcd
->regs
+ HC_PORT1_CTRL
);
418 priv
->hcs_params
= isp1760_readl(hcd
->regs
+ HC_HCSPARAMS
);
420 return priv_init(hcd
);
423 static void isp1760_init_maps(struct usb_hcd
*hcd
)
425 /*set last maps, for iso its only 1, else 32 tds bitmap*/
426 isp1760_writel(0x80000000, hcd
->regs
+ HC_ATL_PTD_LASTPTD_REG
);
427 isp1760_writel(0x80000000, hcd
->regs
+ HC_INT_PTD_LASTPTD_REG
);
428 isp1760_writel(0x00000001, hcd
->regs
+ HC_ISO_PTD_LASTPTD_REG
);
431 static void isp1760_enable_interrupts(struct usb_hcd
*hcd
)
433 isp1760_writel(0, hcd
->regs
+ HC_ATL_IRQ_MASK_AND_REG
);
434 isp1760_writel(0, hcd
->regs
+ HC_ATL_IRQ_MASK_OR_REG
);
435 isp1760_writel(0, hcd
->regs
+ HC_INT_IRQ_MASK_AND_REG
);
436 isp1760_writel(0, hcd
->regs
+ HC_INT_IRQ_MASK_OR_REG
);
437 isp1760_writel(0, hcd
->regs
+ HC_ISO_IRQ_MASK_AND_REG
);
438 isp1760_writel(0xffffffff, hcd
->regs
+ HC_ISO_IRQ_MASK_OR_REG
);
442 static int isp1760_run(struct usb_hcd
*hcd
)
444 struct isp1760_hcd
*priv
= hcd_to_priv(hcd
);
450 hcd
->uses_new_polling
= 1;
453 hcd
->state
= HC_STATE_RUNNING
;
454 isp1760_enable_interrupts(hcd
);
455 temp
= isp1760_readl(hcd
->regs
+ HC_HW_MODE_CTRL
);
456 temp
|= FINAL_HW_CONFIG
;
457 isp1760_writel(temp
, hcd
->regs
+ HC_HW_MODE_CTRL
);
459 command
= isp1760_readl(hcd
->regs
+ HC_USBCMD
);
460 command
&= ~(CMD_LRESET
|CMD_RESET
);
462 isp1760_writel(command
, hcd
->regs
+ HC_USBCMD
);
464 retval
= handshake(priv
, hcd
->regs
+ HC_USBCMD
, CMD_RUN
, CMD_RUN
,
471 * Spec says to write FLAG_CF as last config action, priv code grabs
472 * the semaphore while doing so.
474 down_write(&ehci_cf_port_reset_rwsem
);
475 isp1760_writel(FLAG_CF
, hcd
->regs
+ HC_CONFIGFLAG
);
477 retval
= handshake(priv
, hcd
->regs
+ HC_CONFIGFLAG
, FLAG_CF
, FLAG_CF
,
479 up_write(&ehci_cf_port_reset_rwsem
);
483 chipid
= isp1760_readl(hcd
->regs
+ HC_CHIP_ID_REG
);
484 isp1760_info(priv
, "USB ISP %04x HW rev. %d started\n", chipid
& 0xffff,
487 /* PTD Register Init Part 2, Step 28 */
489 isp1760_init_maps(hcd
);
491 /* GRR this is run-once init(), being done every time the HC starts.
492 * So long as they're part of class devices, we can't do it init()
493 * since the class device isn't created that early.
498 static u32
base_to_chip(u32 base
)
500 return ((base
- 0x400) >> 3);
503 static void transform_into_atl(struct isp1760_hcd
*priv
, struct isp1760_qh
*qh
,
504 struct isp1760_qtd
*qtd
, struct urb
*urb
,
505 u32 payload
, struct ptd
*ptd
)
515 u32 nak
= NAK_COUNTER
;
517 /* according to 3.6.2, max packet len can not be > 0x400 */
518 maxpacket
= usb_maxpacket(urb
->dev
, urb
->pipe
, usb_pipeout(urb
->pipe
));
519 multi
= 1 + ((maxpacket
>> 11) & 0x3);
524 dw0
|= PTD_LENGTH(qtd
->length
);
525 dw0
|= PTD_MAXPACKET(maxpacket
);
526 dw0
|= PTD_ENDPOINT(usb_pipeendpoint(urb
->pipe
));
527 dw1
= usb_pipeendpoint(urb
->pipe
) >> 1;
530 dw1
|= PTD_DEVICE_ADDR(usb_pipedevice(urb
->pipe
));
532 pid_code
= qtd
->packet_type
;
533 dw1
|= PTD_PID_TOKEN(pid_code
);
535 if (usb_pipebulk(urb
->pipe
))
536 dw1
|= PTD_TRANS_BULK
;
537 else if (usb_pipeint(urb
->pipe
))
538 dw1
|= PTD_TRANS_INT
;
540 if (urb
->dev
->speed
!= USB_SPEED_HIGH
) {
541 /* split transaction */
543 dw1
|= PTD_TRANS_SPLIT
;
544 if (urb
->dev
->speed
== USB_SPEED_LOW
)
545 dw1
|= PTD_SE_USB_LOSPEED
;
547 dw1
|= PTD_PORT_NUM(urb
->dev
->ttport
);
548 dw1
|= PTD_HUB_NUM(urb
->dev
->tt
->hub
->devnum
);
550 /* SE bit for Split INT transfers */
551 if (usb_pipeint(urb
->pipe
) &&
552 (urb
->dev
->speed
== USB_SPEED_LOW
))
559 dw0
|= PTD_MULTI(multi
);
560 if (usb_pipecontrol(urb
->pipe
) || usb_pipebulk(urb
->pipe
))
567 dw2
|= PTD_DATA_START_ADDR(base_to_chip(payload
));
568 dw2
|= PTD_RL_CNT(rl
);
569 dw3
|= PTD_NAC_CNT(nak
);
572 if (usb_pipecontrol(urb
->pipe
))
573 dw3
|= PTD_DATA_TOGGLE(qtd
->toggle
);
580 dw3
|= PTD_CERR(ERR_COUNTER
);
582 memset(ptd
, 0, sizeof(*ptd
));
584 ptd
->dw0
= cpu_to_le32(dw0
);
585 ptd
->dw1
= cpu_to_le32(dw1
);
586 ptd
->dw2
= cpu_to_le32(dw2
);
587 ptd
->dw3
= cpu_to_le32(dw3
);
590 static void transform_add_int(struct isp1760_hcd
*priv
, struct isp1760_qh
*qh
,
591 struct isp1760_qtd
*qtd
, struct urb
*urb
,
592 u32 payload
, struct ptd
*ptd
)
601 maxpacket
= usb_maxpacket(urb
->dev
, urb
->pipe
, usb_pipeout(urb
->pipe
));
602 multi
= 1 + ((maxpacket
>> 11) & 0x3);
604 /* length of the data per uframe */
605 maxpacket
= multi
* maxpacket
;
607 numberofusofs
= urb
->transfer_buffer_length
/ maxpacket
;
608 if (urb
->transfer_buffer_length
% maxpacket
)
613 for (i
= 0; i
< numberofusofs
; i
++) {
618 if (urb
->dev
->speed
!= USB_SPEED_HIGH
) {
620 ptd
->dw5
= __constant_cpu_to_le32(0x1c);
622 if (qh
->period
>= 32)
623 period
= qh
->period
/ 2;
630 period
= qh
->period
/8;
637 if (qh
->period
>= 8) {
638 /* millisecond period */
639 period
= (period
<< 3);
641 /* usof based tranmsfers */
642 /* minimum 4 usofs */
647 ptd
->dw2
|= cpu_to_le32(period
);
648 ptd
->dw4
= cpu_to_le32(usof
);
651 static void transform_into_int(struct isp1760_hcd
*priv
, struct isp1760_qh
*qh
,
652 struct isp1760_qtd
*qtd
, struct urb
*urb
,
653 u32 payload
, struct ptd
*ptd
)
655 transform_into_atl(priv
, qh
, qtd
, urb
, payload
, ptd
);
656 transform_add_int(priv
, qh
, qtd
, urb
, payload
, ptd
);
659 static int qtd_fill(struct isp1760_qtd
*qtd
, void *databuffer
, size_t len
,
664 qtd
->data_buffer
= databuffer
;
665 qtd
->packet_type
= GET_QTD_TOKEN_TYPE(token
);
666 qtd
->toggle
= GET_DATA_TOGGLE(token
);
668 if (len
> HC_ATL_PL_SIZE
)
669 count
= HC_ATL_PL_SIZE
;
677 static int check_error(struct ptd
*ptd
)
682 dw3
= le32_to_cpu(ptd
->dw3
);
683 if (dw3
& DW3_HALT_BIT
)
686 if (dw3
& DW3_ERROR_BIT
) {
687 printk(KERN_ERR
"error bit is set in DW3\n");
691 if (dw3
& DW3_QTD_ACTIVE
) {
692 printk(KERN_ERR
"transfer active bit is set DW3\n");
693 printk(KERN_ERR
"nak counter: %d, rl: %d\n", (dw3
>> 19) & 0xf,
694 (le32_to_cpu(ptd
->dw2
) >> 25) & 0xf);
700 static void check_int_err_status(u32 dw4
)
706 for (i
= 0; i
< 8; i
++) {
709 printk(KERN_ERR
"ERROR: under run , %d\n", i
);
713 printk(KERN_ERR
"ERROR: transaction error, %d\n", i
);
717 printk(KERN_ERR
"ERROR: babble error, %d\n", i
);
724 static void enqueue_one_qtd(struct isp1760_qtd
*qtd
, struct isp1760_hcd
*priv
,
728 struct usb_hcd
*hcd
= priv_to_hcd(priv
);
730 token
= qtd
->packet_type
;
732 if (qtd
->length
&& (qtd
->length
<= HC_ATL_PL_SIZE
)) {
738 priv_write_copy(priv
, qtd
->data_buffer
,
745 static void enqueue_one_atl_qtd(u32 atl_regs
, u32 payload
,
746 struct isp1760_hcd
*priv
, struct isp1760_qh
*qh
,
747 struct urb
*urb
, u32 slot
, struct isp1760_qtd
*qtd
)
750 struct usb_hcd
*hcd
= priv_to_hcd(priv
);
752 transform_into_atl(priv
, qh
, qtd
, urb
, payload
, &ptd
);
753 priv_write_copy(priv
, (u32
*)&ptd
, hcd
->regs
+ atl_regs
, sizeof(ptd
));
754 enqueue_one_qtd(qtd
, priv
, payload
);
756 priv
->atl_ints
[slot
].urb
= urb
;
757 priv
->atl_ints
[slot
].qh
= qh
;
758 priv
->atl_ints
[slot
].qtd
= qtd
;
759 priv
->atl_ints
[slot
].data_buffer
= qtd
->data_buffer
;
760 priv
->atl_ints
[slot
].payload
= payload
;
761 qtd
->status
|= URB_ENQUEUED
| URB_TYPE_ATL
;
762 qtd
->status
|= slot
<< 16;
765 static void enqueue_one_int_qtd(u32 int_regs
, u32 payload
,
766 struct isp1760_hcd
*priv
, struct isp1760_qh
*qh
,
767 struct urb
*urb
, u32 slot
, struct isp1760_qtd
*qtd
)
770 struct usb_hcd
*hcd
= priv_to_hcd(priv
);
772 transform_into_int(priv
, qh
, qtd
, urb
, payload
, &ptd
);
773 priv_write_copy(priv
, (u32
*)&ptd
, hcd
->regs
+ int_regs
, sizeof(ptd
));
774 enqueue_one_qtd(qtd
, priv
, payload
);
776 priv
->int_ints
[slot
].urb
= urb
;
777 priv
->int_ints
[slot
].qh
= qh
;
778 priv
->int_ints
[slot
].qtd
= qtd
;
779 priv
->int_ints
[slot
].data_buffer
= qtd
->data_buffer
;
780 priv
->int_ints
[slot
].payload
= payload
;
781 qtd
->status
|= URB_ENQUEUED
| URB_TYPE_INT
;
782 qtd
->status
|= slot
<< 16;
785 void enqueue_an_ATL_packet(struct usb_hcd
*hcd
, struct isp1760_qh
*qh
,
786 struct isp1760_qtd
*qtd
)
788 struct isp1760_hcd
*priv
= hcd_to_priv(hcd
);
789 u32 skip_map
, or_map
;
792 u32 atl_regs
, payload
;
795 skip_map
= isp1760_readl(hcd
->regs
+ HC_ATL_PTD_SKIPMAP_REG
);
798 slot
= __ffs(skip_map
);
799 queue_entry
= 1 << slot
;
801 atl_regs
= ATL_REGS_OFFSET
+ slot
* sizeof(struct ptd
);
803 payload
= alloc_mem(priv
, qtd
->length
);
805 enqueue_one_atl_qtd(atl_regs
, payload
, priv
, qh
, qtd
->urb
, slot
, qtd
);
807 or_map
= isp1760_readl(hcd
->regs
+ HC_ATL_IRQ_MASK_OR_REG
);
808 or_map
|= queue_entry
;
809 isp1760_writel(or_map
, hcd
->regs
+ HC_ATL_IRQ_MASK_OR_REG
);
811 skip_map
&= ~queue_entry
;
812 isp1760_writel(skip_map
, hcd
->regs
+ HC_ATL_PTD_SKIPMAP_REG
);
814 buffstatus
= isp1760_readl(hcd
->regs
+ HC_BUFFER_STATUS_REG
);
815 buffstatus
|= ATL_BUFFER
;
816 isp1760_writel(buffstatus
, hcd
->regs
+ HC_BUFFER_STATUS_REG
);
819 void enqueue_an_INT_packet(struct usb_hcd
*hcd
, struct isp1760_qh
*qh
,
820 struct isp1760_qtd
*qtd
)
822 struct isp1760_hcd
*priv
= hcd_to_priv(hcd
);
823 u32 skip_map
, or_map
;
826 u32 int_regs
, payload
;
829 skip_map
= isp1760_readl(hcd
->regs
+ HC_INT_PTD_SKIPMAP_REG
);
832 slot
= __ffs(skip_map
);
833 queue_entry
= 1 << slot
;
835 int_regs
= INT_REGS_OFFSET
+ slot
* sizeof(struct ptd
);
837 payload
= alloc_mem(priv
, qtd
->length
);
839 enqueue_one_int_qtd(int_regs
, payload
, priv
, qh
, qtd
->urb
, slot
, qtd
);
841 or_map
= isp1760_readl(hcd
->regs
+ HC_INT_IRQ_MASK_OR_REG
);
842 or_map
|= queue_entry
;
843 isp1760_writel(or_map
, hcd
->regs
+ HC_INT_IRQ_MASK_OR_REG
);
845 skip_map
&= ~queue_entry
;
846 isp1760_writel(skip_map
, hcd
->regs
+ HC_INT_PTD_SKIPMAP_REG
);
848 buffstatus
= isp1760_readl(hcd
->regs
+ HC_BUFFER_STATUS_REG
);
849 buffstatus
|= INT_BUFFER
;
850 isp1760_writel(buffstatus
, hcd
->regs
+ HC_BUFFER_STATUS_REG
);
853 static void isp1760_urb_done(struct isp1760_hcd
*priv
, struct urb
*urb
, int status
)
854 __releases(priv
->lock
)
855 __acquires(priv
->lock
)
857 if (!urb
->unlinked
) {
858 if (status
== -EINPROGRESS
)
862 /* complete() can reenter this HCD */
863 usb_hcd_unlink_urb_from_ep(priv_to_hcd(priv
), urb
);
864 spin_unlock(&priv
->lock
);
865 usb_hcd_giveback_urb(priv_to_hcd(priv
), urb
, status
);
866 spin_lock(&priv
->lock
);
869 static void isp1760_qtd_free(struct isp1760_qtd
*qtd
)
871 kmem_cache_free(qtd_cachep
, qtd
);
874 static struct isp1760_qtd
*clean_this_qtd(struct isp1760_qtd
*qtd
)
876 struct isp1760_qtd
*tmp_qtd
;
878 tmp_qtd
= qtd
->hw_next
;
879 list_del(&qtd
->qtd_list
);
880 isp1760_qtd_free(qtd
);
885 * Remove this QTD from the QH list and free its memory. If this QTD
886 * isn't the last one than remove also his successor(s).
887 * Returns the QTD which is part of an new URB and should be enqueued.
889 static struct isp1760_qtd
*clean_up_qtdlist(struct isp1760_qtd
*qtd
)
891 struct isp1760_qtd
*tmp_qtd
;
895 tmp_qtd
= qtd
->hw_next
;
896 last_one
= qtd
->status
& URB_COMPLETE_NOTIFY
;
897 list_del(&qtd
->qtd_list
);
898 isp1760_qtd_free(qtd
);
900 } while (!last_one
&& qtd
);
905 static void do_atl_int(struct usb_hcd
*usb_hcd
)
907 struct isp1760_hcd
*priv
= hcd_to_priv(usb_hcd
);
908 u32 done_map
, skip_map
;
910 struct urb
*urb
= NULL
;
917 u32 status
= -EINVAL
;
919 struct isp1760_qtd
*qtd
;
920 struct isp1760_qh
*qh
;
924 done_map
= isp1760_readl(usb_hcd
->regs
+
925 HC_ATL_PTD_DONEMAP_REG
);
926 skip_map
= isp1760_readl(usb_hcd
->regs
+
927 HC_ATL_PTD_SKIPMAP_REG
);
929 or_map
= isp1760_readl(usb_hcd
->regs
+ HC_ATL_IRQ_MASK_OR_REG
);
931 isp1760_writel(or_map
, usb_hcd
->regs
+ HC_ATL_IRQ_MASK_OR_REG
);
933 atl_regs_base
= ATL_REGS_OFFSET
;
941 queue_entry
= __ffs(done_map
);
942 done_map
&= ~(1 << queue_entry
);
943 skip_map
|= 1 << queue_entry
;
945 atl_regs
= atl_regs_base
+ queue_entry
* sizeof(struct ptd
);
947 urb
= priv
->atl_ints
[queue_entry
].urb
;
948 qtd
= priv
->atl_ints
[queue_entry
].qtd
;
949 qh
= priv
->atl_ints
[queue_entry
].qh
;
950 payload
= priv
->atl_ints
[queue_entry
].payload
;
953 printk(KERN_ERR
"qh is 0\n");
956 priv_read_copy(priv
, (u32
*)&ptd
, usb_hcd
->regs
+ atl_regs
,
957 atl_regs
, sizeof(ptd
));
959 dw1
= le32_to_cpu(ptd
.dw1
);
960 dw2
= le32_to_cpu(ptd
.dw2
);
961 dw3
= le32_to_cpu(ptd
.dw3
);
962 rl
= (dw2
>> 25) & 0x0f;
963 nakcount
= (dw3
>> 19) & 0xf;
965 /* Transfer Error, *but* active and no HALT -> reload */
966 if ((dw3
& DW3_ERROR_BIT
) && (dw3
& DW3_QTD_ACTIVE
) &&
967 !(dw3
& DW3_HALT_BIT
)) {
969 /* according to ppriv code, we have to
970 * reload this one if trasfered bytes != requested bytes
971 * else act like everything went smooth..
972 * XXX This just doesn't feel right and hasn't
976 length
= PTD_XFERRED_LENGTH(dw3
);
977 printk(KERN_ERR
"Should reload now.... transfered %d "
978 "of %zu\n", length
, qtd
->length
);
982 if (!nakcount
&& (dw3
& DW3_QTD_ACTIVE
)) {
986 * NAKs are handled in HW by the chip. Usually if the
987 * device is not able to send data fast enough.
988 * This did not trigger for a long time now.
990 printk(KERN_ERR
"Reloading ptd %p/%p... qh %p readed: "
991 "%d of %zu done: %08x cur: %08x\n", qtd
,
992 urb
, qh
, PTD_XFERRED_LENGTH(dw3
),
993 qtd
->length
, done_map
,
996 /* RL counter = ERR counter */
999 dw3
&= ~(3 << (55 - 32));
1000 dw3
|= ERR_COUNTER
<< (55 - 32);
1003 * It is not needed to write skip map back because it
1004 * is unchanged. Just make sure that this entry is
1005 * unskipped once it gets written to the HW.
1007 skip_map
&= ~(1 << queue_entry
);
1008 or_map
= isp1760_readl(usb_hcd
->regs
+
1009 HC_ATL_IRQ_MASK_OR_REG
);
1010 or_map
|= 1 << queue_entry
;
1011 isp1760_writel(or_map
, usb_hcd
->regs
+
1012 HC_ATL_IRQ_MASK_OR_REG
);
1014 ptd
.dw3
= cpu_to_le32(dw3
);
1015 priv_write_copy(priv
, (u32
*)&ptd
, usb_hcd
->regs
+
1016 atl_regs
, sizeof(ptd
));
1018 ptd
.dw0
|= __constant_cpu_to_le32(PTD_VALID
);
1019 priv_write_copy(priv
, (u32
*)&ptd
, usb_hcd
->regs
+
1020 atl_regs
, sizeof(ptd
));
1022 buffstatus
= isp1760_readl(usb_hcd
->regs
+
1023 HC_BUFFER_STATUS_REG
);
1024 buffstatus
|= ATL_BUFFER
;
1025 isp1760_writel(buffstatus
, usb_hcd
->regs
+
1026 HC_BUFFER_STATUS_REG
);
1030 error
= check_error(&ptd
);
1033 priv
->atl_ints
[queue_entry
].qh
->toggle
= 0;
1034 priv
->atl_ints
[queue_entry
].qh
->ping
= 0;
1035 urb
->status
= -EPIPE
;
1038 printk(KERN_ERR
"Error in %s().\n", __func__
);
1039 printk(KERN_ERR
"IN dw0: %08x dw1: %08x dw2: %08x "
1040 "dw3: %08x dw4: %08x dw5: %08x dw6: "
1042 ptd
.dw0
, ptd
.dw1
, ptd
.dw2
, ptd
.dw3
,
1043 ptd
.dw4
, ptd
.dw5
, ptd
.dw6
, ptd
.dw7
);
1046 if (usb_pipetype(urb
->pipe
) == PIPE_BULK
) {
1047 priv
->atl_ints
[queue_entry
].qh
->toggle
= dw3
&
1049 priv
->atl_ints
[queue_entry
].qh
->ping
= dw3
&
1054 length
= PTD_XFERRED_LENGTH(dw3
);
1056 switch (DW1_GET_PID(dw1
)) {
1058 priv_read_copy(priv
,
1059 priv
->atl_ints
[queue_entry
].data_buffer
,
1060 usb_hcd
->regs
+ payload
, payload
,
1065 urb
->actual_length
+= length
;
1072 priv
->atl_ints
[queue_entry
].data_buffer
= NULL
;
1073 priv
->atl_ints
[queue_entry
].urb
= NULL
;
1074 priv
->atl_ints
[queue_entry
].qtd
= NULL
;
1075 priv
->atl_ints
[queue_entry
].qh
= NULL
;
1077 free_mem(priv
, payload
);
1079 isp1760_writel(skip_map
, usb_hcd
->regs
+
1080 HC_ATL_PTD_SKIPMAP_REG
);
1082 if (urb
->status
== -EPIPE
) {
1083 /* HALT was received */
1085 qtd
= clean_up_qtdlist(qtd
);
1086 isp1760_urb_done(priv
, urb
, urb
->status
);
1088 } else if (usb_pipebulk(urb
->pipe
) && (length
< qtd
->length
)) {
1089 /* short BULK received */
1091 printk(KERN_ERR
"short bulk, %d instead %zu\n", length
,
1093 if (urb
->transfer_flags
& URB_SHORT_NOT_OK
) {
1094 urb
->status
= -EREMOTEIO
;
1095 printk(KERN_ERR
"not okey\n");
1098 if (urb
->status
== -EINPROGRESS
)
1101 qtd
= clean_up_qtdlist(qtd
);
1103 isp1760_urb_done(priv
, urb
, urb
->status
);
1105 } else if (qtd
->status
& URB_COMPLETE_NOTIFY
) {
1106 /* that was the last qtd of that URB */
1108 if (urb
->status
== -EINPROGRESS
)
1111 qtd
= clean_this_qtd(qtd
);
1112 isp1760_urb_done(priv
, urb
, urb
->status
);
1115 /* next QTD of this URB */
1117 qtd
= clean_this_qtd(qtd
);
1122 enqueue_an_ATL_packet(usb_hcd
, qh
, qtd
);
1124 skip_map
= isp1760_readl(usb_hcd
->regs
+
1125 HC_ATL_PTD_SKIPMAP_REG
);
1129 static void do_intl_int(struct usb_hcd
*usb_hcd
)
1131 struct isp1760_hcd
*priv
= hcd_to_priv(usb_hcd
);
1132 u32 done_map
, skip_map
;
1134 struct urb
*urb
= NULL
;
1142 struct isp1760_qtd
*qtd
;
1143 struct isp1760_qh
*qh
;
1145 done_map
= isp1760_readl(usb_hcd
->regs
+
1146 HC_INT_PTD_DONEMAP_REG
);
1147 skip_map
= isp1760_readl(usb_hcd
->regs
+
1148 HC_INT_PTD_SKIPMAP_REG
);
1150 or_map
= isp1760_readl(usb_hcd
->regs
+ HC_INT_IRQ_MASK_OR_REG
);
1151 or_map
&= ~done_map
;
1152 isp1760_writel(or_map
, usb_hcd
->regs
+ HC_INT_IRQ_MASK_OR_REG
);
1154 int_regs_base
= INT_REGS_OFFSET
;
1160 queue_entry
= __ffs(done_map
);
1161 done_map
&= ~(1 << queue_entry
);
1162 skip_map
|= 1 << queue_entry
;
1164 int_regs
= int_regs_base
+ queue_entry
* sizeof(struct ptd
);
1165 urb
= priv
->int_ints
[queue_entry
].urb
;
1166 qtd
= priv
->int_ints
[queue_entry
].qtd
;
1167 qh
= priv
->int_ints
[queue_entry
].qh
;
1168 payload
= priv
->int_ints
[queue_entry
].payload
;
1171 printk(KERN_ERR
"(INT) qh is 0\n");
1175 priv_read_copy(priv
, (u32
*)&ptd
, usb_hcd
->regs
+ int_regs
,
1176 int_regs
, sizeof(ptd
));
1177 dw1
= le32_to_cpu(ptd
.dw1
);
1178 dw3
= le32_to_cpu(ptd
.dw3
);
1179 check_int_err_status(le32_to_cpu(ptd
.dw4
));
1181 error
= check_error(&ptd
);
1184 printk(KERN_ERR
"Error in %s().\n", __func__
);
1185 printk(KERN_ERR
"IN dw0: %08x dw1: %08x dw2: %08x "
1186 "dw3: %08x dw4: %08x dw5: %08x dw6: "
1188 ptd
.dw0
, ptd
.dw1
, ptd
.dw2
, ptd
.dw3
,
1189 ptd
.dw4
, ptd
.dw5
, ptd
.dw6
, ptd
.dw7
);
1191 urb
->status
= -EPIPE
;
1192 priv
->int_ints
[queue_entry
].qh
->toggle
= 0;
1193 priv
->int_ints
[queue_entry
].qh
->ping
= 0;
1196 priv
->int_ints
[queue_entry
].qh
->toggle
=
1198 priv
->int_ints
[queue_entry
].qh
->ping
= dw3
& (1 << 26);
1201 if (urb
->dev
->speed
!= USB_SPEED_HIGH
)
1202 length
= PTD_XFERRED_LENGTH_LO(dw3
);
1204 length
= PTD_XFERRED_LENGTH(dw3
);
1207 switch (DW1_GET_PID(dw1
)) {
1209 priv_read_copy(priv
,
1210 priv
->int_ints
[queue_entry
].data_buffer
,
1211 usb_hcd
->regs
+ payload
, payload
,
1215 urb
->actual_length
+= length
;
1222 priv
->int_ints
[queue_entry
].data_buffer
= NULL
;
1223 priv
->int_ints
[queue_entry
].urb
= NULL
;
1224 priv
->int_ints
[queue_entry
].qtd
= NULL
;
1225 priv
->int_ints
[queue_entry
].qh
= NULL
;
1227 isp1760_writel(skip_map
, usb_hcd
->regs
+
1228 HC_INT_PTD_SKIPMAP_REG
);
1229 free_mem(priv
, payload
);
1231 if (urb
->status
== -EPIPE
) {
1234 qtd
= clean_up_qtdlist(qtd
);
1235 isp1760_urb_done(priv
, urb
, urb
->status
);
1237 } else if (qtd
->status
& URB_COMPLETE_NOTIFY
) {
1239 if (urb
->status
== -EINPROGRESS
)
1242 qtd
= clean_this_qtd(qtd
);
1243 isp1760_urb_done(priv
, urb
, urb
->status
);
1246 /* next QTD of this URB */
1248 qtd
= clean_this_qtd(qtd
);
1253 enqueue_an_INT_packet(usb_hcd
, qh
, qtd
);
1255 skip_map
= isp1760_readl(usb_hcd
->regs
+
1256 HC_INT_PTD_SKIPMAP_REG
);
1260 #define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
1261 static struct isp1760_qh
*qh_make(struct isp1760_hcd
*priv
, struct urb
*urb
,
1264 struct isp1760_qh
*qh
;
1267 qh
= isp1760_qh_alloc(priv
, flags
);
1272 * init endpoint/device data for this QH
1274 is_input
= usb_pipein(urb
->pipe
);
1275 type
= usb_pipetype(urb
->pipe
);
1277 if (type
== PIPE_INTERRUPT
) {
1279 if (urb
->dev
->speed
== USB_SPEED_HIGH
) {
1281 qh
->period
= urb
->interval
>> 3;
1282 if (qh
->period
== 0 && urb
->interval
!= 1) {
1283 /* NOTE interval 2 or 4 uframes could work.
1284 * But interval 1 scheduling is simpler, and
1285 * includes high bandwidth.
1287 printk(KERN_ERR
"intr period %d uframes, NYET!",
1293 qh
->period
= urb
->interval
;
1297 /* support for tt scheduling, and access to toggles */
1300 if (!usb_pipecontrol(urb
->pipe
))
1301 usb_settoggle(urb
->dev
, usb_pipeendpoint(urb
->pipe
), !is_input
,
1307 * For control/bulk/interrupt, return QH with these TDs appended.
1308 * Allocates and initializes the QH if necessary.
1309 * Returns null if it can't allocate a QH it needs to.
1310 * If the QH has TDs (urbs) already, that's great.
1312 static struct isp1760_qh
*qh_append_tds(struct isp1760_hcd
*priv
,
1313 struct urb
*urb
, struct list_head
*qtd_list
, int epnum
,
1316 struct isp1760_qh
*qh
;
1317 struct isp1760_qtd
*qtd
;
1318 struct isp1760_qtd
*prev_qtd
;
1320 qh
= (struct isp1760_qh
*)*ptr
;
1322 /* can't sleep here, we have priv->lock... */
1323 qh
= qh_make(priv
, urb
, GFP_ATOMIC
);
1329 qtd
= list_entry(qtd_list
->next
, struct isp1760_qtd
,
1331 if (!list_empty(&qh
->qtd_list
))
1332 prev_qtd
= list_entry(qh
->qtd_list
.prev
,
1333 struct isp1760_qtd
, qtd_list
);
1337 list_splice(qtd_list
, qh
->qtd_list
.prev
);
1339 BUG_ON(prev_qtd
->hw_next
);
1340 prev_qtd
->hw_next
= qtd
;
1347 static void qtd_list_free(struct isp1760_hcd
*priv
, struct urb
*urb
,
1348 struct list_head
*qtd_list
)
1350 struct list_head
*entry
, *temp
;
1352 list_for_each_safe(entry
, temp
, qtd_list
) {
1353 struct isp1760_qtd
*qtd
;
1355 qtd
= list_entry(entry
, struct isp1760_qtd
, qtd_list
);
1356 list_del(&qtd
->qtd_list
);
1357 isp1760_qtd_free(qtd
);
1361 static int isp1760_prepare_enqueue(struct isp1760_hcd
*priv
, struct urb
*urb
,
1362 struct list_head
*qtd_list
, gfp_t mem_flags
, packet_enqueue
*p
)
1364 struct isp1760_qtd
*qtd
;
1366 unsigned long flags
;
1367 struct isp1760_qh
*qh
= NULL
;
1371 qtd
= list_entry(qtd_list
->next
, struct isp1760_qtd
, qtd_list
);
1372 epnum
= urb
->ep
->desc
.bEndpointAddress
;
1374 spin_lock_irqsave(&priv
->lock
, flags
);
1375 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE
, &priv_to_hcd(priv
)->flags
)) {
1379 rc
= usb_hcd_link_urb_to_ep(priv_to_hcd(priv
), urb
);
1383 qh
= urb
->ep
->hcpriv
;
1385 qh_busy
= !list_empty(&qh
->qtd_list
);
1389 qh
= qh_append_tds(priv
, urb
, qtd_list
, epnum
, &urb
->ep
->hcpriv
);
1391 usb_hcd_unlink_urb_from_ep(priv_to_hcd(priv
), urb
);
1397 p(priv_to_hcd(priv
), qh
, qtd
);
1400 spin_unlock_irqrestore(&priv
->lock
, flags
);
1402 qtd_list_free(priv
, urb
, qtd_list
);
1406 static struct isp1760_qtd
*isp1760_qtd_alloc(struct isp1760_hcd
*priv
,
1409 struct isp1760_qtd
*qtd
;
1411 qtd
= kmem_cache_zalloc(qtd_cachep
, flags
);
1413 INIT_LIST_HEAD(&qtd
->qtd_list
);
1419 * create a list of filled qtds for this URB; won't link into qh.
1421 static struct list_head
*qh_urb_transaction(struct isp1760_hcd
*priv
,
1422 struct urb
*urb
, struct list_head
*head
, gfp_t flags
)
1424 struct isp1760_qtd
*qtd
, *qtd_prev
;
1431 * URBs map to sequences of QTDs: one logical transaction
1433 qtd
= isp1760_qtd_alloc(priv
, flags
);
1437 list_add_tail(&qtd
->qtd_list
, head
);
1439 urb
->status
= -EINPROGRESS
;
1442 /* for split transactions, SplitXState initialized to zero */
1444 len
= urb
->transfer_buffer_length
;
1445 is_input
= usb_pipein(urb
->pipe
);
1446 if (usb_pipecontrol(urb
->pipe
)) {
1448 qtd_fill(qtd
, urb
->setup_packet
,
1449 sizeof(struct usb_ctrlrequest
),
1452 /* ... and always at least one more pid */
1453 token
^= DATA_TOGGLE
;
1455 qtd
= isp1760_qtd_alloc(priv
, flags
);
1459 qtd_prev
->hw_next
= qtd
;
1460 list_add_tail(&qtd
->qtd_list
, head
);
1462 /* for zero length DATA stages, STATUS is always IN */
1468 * data transfer stage: buffer setup
1470 buf
= urb
->transfer_buffer
;
1477 maxpacket
= max_packet(usb_maxpacket(urb
->dev
, urb
->pipe
, !is_input
));
1480 * buffer gets wrapped in one or more qtds;
1481 * last one may be "short" (including zero len)
1482 * and may serve as a control status ack
1488 /* XXX This looks like usb storage / SCSI bug */
1489 printk(KERN_ERR
"buf is null, dma is %08lx len is %d\n",
1490 (long unsigned)urb
->transfer_dma
, len
);
1494 this_qtd_len
= qtd_fill(qtd
, buf
, len
, token
);
1495 len
-= this_qtd_len
;
1496 buf
+= this_qtd_len
;
1498 /* qh makes control packets use qtd toggle; maybe switch it */
1499 if ((maxpacket
& (this_qtd_len
+ (maxpacket
- 1))) == 0)
1500 token
^= DATA_TOGGLE
;
1506 qtd
= isp1760_qtd_alloc(priv
, flags
);
1510 qtd_prev
->hw_next
= qtd
;
1511 list_add_tail(&qtd
->qtd_list
, head
);
1515 * control requests may need a terminating data "status" ack;
1516 * bulk ones may need a terminating short packet (zero length).
1518 if (urb
->transfer_buffer_length
!= 0) {
1521 if (usb_pipecontrol(urb
->pipe
)) {
1523 /* "in" <--> "out" */
1526 token
|= DATA_TOGGLE
;
1527 } else if (usb_pipebulk(urb
->pipe
)
1528 && (urb
->transfer_flags
& URB_ZERO_PACKET
)
1529 && !(urb
->transfer_buffer_length
% maxpacket
)) {
1534 qtd
= isp1760_qtd_alloc(priv
, flags
);
1538 qtd_prev
->hw_next
= qtd
;
1539 list_add_tail(&qtd
->qtd_list
, head
);
1541 /* never any data in such packets */
1542 qtd_fill(qtd
, NULL
, 0, token
);
1546 qtd
->status
= URB_COMPLETE_NOTIFY
;
1550 qtd_list_free(priv
, urb
, head
);
1554 static int isp1760_urb_enqueue(struct usb_hcd
*hcd
, struct urb
*urb
,
1557 struct isp1760_hcd
*priv
= hcd_to_priv(hcd
);
1558 struct list_head qtd_list
;
1561 INIT_LIST_HEAD(&qtd_list
);
1563 switch (usb_pipetype(urb
->pipe
)) {
1567 if (!qh_urb_transaction(priv
, urb
, &qtd_list
, mem_flags
))
1569 pe
= enqueue_an_ATL_packet
;
1572 case PIPE_INTERRUPT
:
1573 if (!qh_urb_transaction(priv
, urb
, &qtd_list
, mem_flags
))
1575 pe
= enqueue_an_INT_packet
;
1578 case PIPE_ISOCHRONOUS
:
1579 printk(KERN_ERR
"PIPE_ISOCHRONOUS ain't supported\n");
1584 isp1760_prepare_enqueue(priv
, urb
, &qtd_list
, mem_flags
, pe
);
1588 static int isp1760_urb_dequeue(struct usb_hcd
*hcd
, struct urb
*urb
,
1591 struct isp1760_hcd
*priv
= hcd_to_priv(hcd
);
1592 struct inter_packet_info
*ints
;
1594 u32 reg_base
, or_reg
, skip_reg
;
1598 switch (usb_pipetype(urb
->pipe
)) {
1599 case PIPE_ISOCHRONOUS
:
1603 case PIPE_INTERRUPT
:
1604 ints
= priv
->int_ints
;
1605 reg_base
= INT_REGS_OFFSET
;
1606 or_reg
= HC_INT_IRQ_MASK_OR_REG
;
1607 skip_reg
= HC_INT_PTD_SKIPMAP_REG
;
1611 ints
= priv
->atl_ints
;
1612 reg_base
= ATL_REGS_OFFSET
;
1613 or_reg
= HC_ATL_IRQ_MASK_OR_REG
;
1614 skip_reg
= HC_ATL_PTD_SKIPMAP_REG
;
1618 memset(&ptd
, 0, sizeof(ptd
));
1619 spin_lock_irqsave(&priv
->lock
, flags
);
1621 for (i
= 0; i
< 32; i
++) {
1622 if (ints
->urb
== urb
) {
1625 struct isp1760_qtd
*qtd
;
1627 skip_map
= isp1760_readl(hcd
->regs
+ skip_reg
);
1629 isp1760_writel(skip_map
, hcd
->regs
+ skip_reg
);
1631 or_map
= isp1760_readl(hcd
->regs
+ or_reg
);
1632 or_map
&= ~(1 << i
);
1633 isp1760_writel(or_map
, hcd
->regs
+ or_reg
);
1635 priv_write_copy(priv
, (u32
*)&ptd
, hcd
->regs
+ reg_base
1636 + i
* sizeof(ptd
), sizeof(ptd
));
1639 clean_up_qtdlist(qtd
);
1641 free_mem(priv
, ints
->payload
);
1646 ints
->data_buffer
= NULL
;
1649 isp1760_urb_done(priv
, urb
, status
);
1655 spin_unlock_irqrestore(&priv
->lock
, flags
);
1659 static irqreturn_t
isp1760_irq(struct usb_hcd
*usb_hcd
)
1661 struct isp1760_hcd
*priv
= hcd_to_priv(usb_hcd
);
1663 irqreturn_t irqret
= IRQ_NONE
;
1665 spin_lock(&priv
->lock
);
1667 if (!(usb_hcd
->state
& HC_STATE_RUNNING
))
1670 imask
= isp1760_readl(usb_hcd
->regs
+ HC_INTERRUPT_REG
);
1671 if (unlikely(!imask
))
1674 isp1760_writel(imask
, usb_hcd
->regs
+ HC_INTERRUPT_REG
);
1675 if (imask
& HC_ATL_INT
)
1676 do_atl_int(usb_hcd
);
1678 if (imask
& HC_INTL_INT
)
1679 do_intl_int(usb_hcd
);
1681 irqret
= IRQ_HANDLED
;
1683 spin_unlock(&priv
->lock
);
1687 static int isp1760_hub_status_data(struct usb_hcd
*hcd
, char *buf
)
1689 struct isp1760_hcd
*priv
= hcd_to_priv(hcd
);
1690 u32 temp
, status
= 0;
1693 unsigned long flags
;
1695 /* if !USB_SUSPEND, root hub timers won't get shut down ... */
1696 if (!HC_IS_RUNNING(hcd
->state
))
1699 /* init status to no-changes */
1703 spin_lock_irqsave(&priv
->lock
, flags
);
1704 temp
= isp1760_readl(hcd
->regs
+ HC_PORTSC1
);
1706 if (temp
& PORT_OWNER
) {
1707 if (temp
& PORT_CSC
) {
1709 isp1760_writel(temp
, hcd
->regs
+ HC_PORTSC1
);
1715 * Return status information even for ports with OWNER set.
1716 * Otherwise khubd wouldn't see the disconnect event when a
1717 * high-speed device is switched over to the companion
1718 * controller by the user.
1721 if ((temp
& mask
) != 0
1722 || ((temp
& PORT_RESUME
) != 0
1723 && time_after_eq(jiffies
,
1724 priv
->reset_done
))) {
1725 buf
[0] |= 1 << (0 + 1);
1728 /* FIXME autosuspend idle root hubs */
1730 spin_unlock_irqrestore(&priv
->lock
, flags
);
1731 return status
? retval
: 0;
1734 static void isp1760_hub_descriptor(struct isp1760_hcd
*priv
,
1735 struct usb_hub_descriptor
*desc
)
1737 int ports
= HCS_N_PORTS(priv
->hcs_params
);
1740 desc
->bDescriptorType
= 0x29;
1741 /* priv 1.0, 2.3.9 says 20ms max */
1742 desc
->bPwrOn2PwrGood
= 10;
1743 desc
->bHubContrCurrent
= 0;
1745 desc
->bNbrPorts
= ports
;
1746 temp
= 1 + (ports
/ 8);
1747 desc
->bDescLength
= 7 + 2 * temp
;
1749 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
1750 memset(&desc
->bitmap
[0], 0, temp
);
1751 memset(&desc
->bitmap
[temp
], 0xff, temp
);
1753 /* per-port overcurrent reporting */
1755 if (HCS_PPC(priv
->hcs_params
))
1756 /* per-port power control */
1759 /* no power switching */
1761 desc
->wHubCharacteristics
= cpu_to_le16(temp
);
1764 #define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
1766 static int check_reset_complete(struct isp1760_hcd
*priv
, int index
,
1767 u32 __iomem
*status_reg
, int port_status
)
1769 if (!(port_status
& PORT_CONNECT
))
1772 /* if reset finished and it's still not enabled -- handoff */
1773 if (!(port_status
& PORT_PE
)) {
1775 printk(KERN_ERR
"port %d full speed --> companion\n",
1778 port_status
|= PORT_OWNER
;
1779 port_status
&= ~PORT_RWC_BITS
;
1780 isp1760_writel(port_status
, status_reg
);
1783 printk(KERN_ERR
"port %d high speed\n", index
+ 1);
1788 static int isp1760_hub_control(struct usb_hcd
*hcd
, u16 typeReq
,
1789 u16 wValue
, u16 wIndex
, char *buf
, u16 wLength
)
1791 struct isp1760_hcd
*priv
= hcd_to_priv(hcd
);
1792 int ports
= HCS_N_PORTS(priv
->hcs_params
);
1793 u32 __iomem
*status_reg
= hcd
->regs
+ HC_PORTSC1
;
1795 unsigned long flags
;
1800 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
1801 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
1802 * (track current state ourselves) ... blink for diagnostics,
1803 * power, "this is the one", etc. EHCI spec supports this.
1806 spin_lock_irqsave(&priv
->lock
, flags
);
1808 case ClearHubFeature
:
1810 case C_HUB_LOCAL_POWER
:
1811 case C_HUB_OVER_CURRENT
:
1812 /* no hub-wide feature/status flags */
1818 case ClearPortFeature
:
1819 if (!wIndex
|| wIndex
> ports
)
1822 temp
= isp1760_readl(status_reg
);
1825 * Even if OWNER is set, so the port is owned by the
1826 * companion controller, khubd needs to be able to clear
1827 * the port-change status bits (especially
1828 * USB_PORT_FEAT_C_CONNECTION).
1832 case USB_PORT_FEAT_ENABLE
:
1833 isp1760_writel(temp
& ~PORT_PE
, status_reg
);
1835 case USB_PORT_FEAT_C_ENABLE
:
1838 case USB_PORT_FEAT_SUSPEND
:
1839 if (temp
& PORT_RESET
)
1842 if (temp
& PORT_SUSPEND
) {
1843 if ((temp
& PORT_PE
) == 0)
1845 /* resume signaling for 20 msec */
1846 temp
&= ~(PORT_RWC_BITS
);
1847 isp1760_writel(temp
| PORT_RESUME
,
1849 priv
->reset_done
= jiffies
+
1850 msecs_to_jiffies(20);
1853 case USB_PORT_FEAT_C_SUSPEND
:
1854 /* we auto-clear this feature */
1856 case USB_PORT_FEAT_POWER
:
1857 if (HCS_PPC(priv
->hcs_params
))
1858 isp1760_writel(temp
& ~PORT_POWER
, status_reg
);
1860 case USB_PORT_FEAT_C_CONNECTION
:
1861 isp1760_writel(temp
| PORT_CSC
,
1864 case USB_PORT_FEAT_C_OVER_CURRENT
:
1867 case USB_PORT_FEAT_C_RESET
:
1868 /* GetPortStatus clears reset */
1873 isp1760_readl(hcd
->regs
+ HC_USBCMD
);
1875 case GetHubDescriptor
:
1876 isp1760_hub_descriptor(priv
, (struct usb_hub_descriptor
*)
1880 /* no hub-wide feature/status flags */
1884 if (!wIndex
|| wIndex
> ports
)
1888 temp
= isp1760_readl(status_reg
);
1890 /* wPortChange bits */
1891 if (temp
& PORT_CSC
)
1892 status
|= 1 << USB_PORT_FEAT_C_CONNECTION
;
1895 /* whoever resumes must GetPortStatus to complete it!! */
1896 if (temp
& PORT_RESUME
) {
1897 printk(KERN_ERR
"Port resume should be skipped.\n");
1899 /* Remote Wakeup received? */
1900 if (!priv
->reset_done
) {
1901 /* resume signaling for 20 msec */
1902 priv
->reset_done
= jiffies
1903 + msecs_to_jiffies(20);
1904 /* check the port again */
1905 mod_timer(&priv_to_hcd(priv
)->rh_timer
,
1909 /* resume completed? */
1910 else if (time_after_eq(jiffies
,
1911 priv
->reset_done
)) {
1912 status
|= 1 << USB_PORT_FEAT_C_SUSPEND
;
1913 priv
->reset_done
= 0;
1915 /* stop resume signaling */
1916 temp
= isp1760_readl(status_reg
);
1918 temp
& ~(PORT_RWC_BITS
| PORT_RESUME
),
1920 retval
= handshake(priv
, status_reg
,
1921 PORT_RESUME
, 0, 2000 /* 2msec */);
1924 "port %d resume error %d\n",
1925 wIndex
+ 1, retval
);
1928 temp
&= ~(PORT_SUSPEND
|PORT_RESUME
|(3<<10));
1932 /* whoever resets must GetPortStatus to complete it!! */
1933 if ((temp
& PORT_RESET
)
1934 && time_after_eq(jiffies
,
1935 priv
->reset_done
)) {
1936 status
|= 1 << USB_PORT_FEAT_C_RESET
;
1937 priv
->reset_done
= 0;
1939 /* force reset to complete */
1940 isp1760_writel(temp
& ~PORT_RESET
,
1942 /* REVISIT: some hardware needs 550+ usec to clear
1943 * this bit; seems too long to spin routinely...
1945 retval
= handshake(priv
, status_reg
,
1946 PORT_RESET
, 0, 750);
1948 isp1760_err(priv
, "port %d reset error %d\n",
1949 wIndex
+ 1, retval
);
1953 /* see what we found out */
1954 temp
= check_reset_complete(priv
, wIndex
, status_reg
,
1955 isp1760_readl(status_reg
));
1958 * Even if OWNER is set, there's no harm letting khubd
1959 * see the wPortStatus values (they should all be 0 except
1960 * for PORT_POWER anyway).
1963 if (temp
& PORT_OWNER
)
1964 printk(KERN_ERR
"Warning: PORT_OWNER is set\n");
1966 if (temp
& PORT_CONNECT
) {
1967 status
|= 1 << USB_PORT_FEAT_CONNECTION
;
1968 /* status may be from integrated TT */
1969 status
|= ehci_port_speed(priv
, temp
);
1972 status
|= 1 << USB_PORT_FEAT_ENABLE
;
1973 if (temp
& (PORT_SUSPEND
|PORT_RESUME
))
1974 status
|= 1 << USB_PORT_FEAT_SUSPEND
;
1975 if (temp
& PORT_RESET
)
1976 status
|= 1 << USB_PORT_FEAT_RESET
;
1977 if (temp
& PORT_POWER
)
1978 status
|= 1 << USB_PORT_FEAT_POWER
;
1980 put_unaligned(cpu_to_le32(status
), (__le32
*) buf
);
1984 case C_HUB_LOCAL_POWER
:
1985 case C_HUB_OVER_CURRENT
:
1986 /* no hub-wide feature/status flags */
1992 case SetPortFeature
:
1993 selector
= wIndex
>> 8;
1995 if (!wIndex
|| wIndex
> ports
)
1998 temp
= isp1760_readl(status_reg
);
1999 if (temp
& PORT_OWNER
)
2002 /* temp &= ~PORT_RWC_BITS; */
2004 case USB_PORT_FEAT_ENABLE
:
2005 isp1760_writel(temp
| PORT_PE
, status_reg
);
2008 case USB_PORT_FEAT_SUSPEND
:
2009 if ((temp
& PORT_PE
) == 0
2010 || (temp
& PORT_RESET
) != 0)
2013 isp1760_writel(temp
| PORT_SUSPEND
, status_reg
);
2015 case USB_PORT_FEAT_POWER
:
2016 if (HCS_PPC(priv
->hcs_params
))
2017 isp1760_writel(temp
| PORT_POWER
,
2020 case USB_PORT_FEAT_RESET
:
2021 if (temp
& PORT_RESUME
)
2023 /* line status bits may report this as low speed,
2024 * which can be fine if this root hub has a
2025 * transaction translator built in.
2027 if ((temp
& (PORT_PE
|PORT_CONNECT
)) == PORT_CONNECT
2028 && PORT_USB11(temp
)) {
2035 * caller must wait, then call GetPortStatus
2036 * usb 2.0 spec says 50 ms resets on root
2038 priv
->reset_done
= jiffies
+
2039 msecs_to_jiffies(50);
2041 isp1760_writel(temp
, status_reg
);
2046 isp1760_readl(hcd
->regs
+ HC_USBCMD
);
2051 /* "stall" on error */
2054 spin_unlock_irqrestore(&priv
->lock
, flags
);
2058 static void isp1760_endpoint_disable(struct usb_hcd
*usb_hcd
,
2059 struct usb_host_endpoint
*ep
)
2061 struct isp1760_hcd
*priv
= hcd_to_priv(usb_hcd
);
2062 struct isp1760_qh
*qh
;
2063 struct isp1760_qtd
*qtd
;
2066 spin_lock_irqsave(&priv
->lock
, flags
);
2073 /* more than entry might get removed */
2074 if (list_empty(&qh
->qtd_list
))
2077 qtd
= list_first_entry(&qh
->qtd_list
, struct isp1760_qtd
,
2080 if (qtd
->status
& URB_ENQUEUED
) {
2082 spin_unlock_irqrestore(&priv
->lock
, flags
);
2083 isp1760_urb_dequeue(usb_hcd
, qtd
->urb
, -ECONNRESET
);
2084 spin_lock_irqsave(&priv
->lock
, flags
);
2089 clean_up_qtdlist(qtd
);
2090 isp1760_urb_done(priv
, urb
, -ECONNRESET
);
2095 /* remove requests and leak them.
2096 * ATL are pretty fast done, INT could take a while...
2097 * The latter shoule be removed
2100 spin_unlock_irqrestore(&priv
->lock
, flags
);
2103 static int isp1760_get_frame(struct usb_hcd
*hcd
)
2105 struct isp1760_hcd
*priv
= hcd_to_priv(hcd
);
2108 fr
= isp1760_readl(hcd
->regs
+ HC_FRINDEX
);
2109 return (fr
>> 3) % priv
->periodic_size
;
2112 static void isp1760_stop(struct usb_hcd
*hcd
)
2114 struct isp1760_hcd
*priv
= hcd_to_priv(hcd
);
2116 isp1760_hub_control(hcd
, ClearPortFeature
, USB_PORT_FEAT_POWER
, 1,
2120 spin_lock_irq(&priv
->lock
);
2123 isp1760_writel(HW_DATA_BUS_32BIT
, hcd
->regs
+ HC_HW_MODE_CTRL
);
2124 spin_unlock_irq(&priv
->lock
);
2126 isp1760_writel(0, hcd
->regs
+ HC_CONFIGFLAG
);
2129 static void isp1760_shutdown(struct usb_hcd
*hcd
)
2134 isp1760_writel(HW_DATA_BUS_32BIT
, hcd
->regs
+ HC_HW_MODE_CTRL
);
2136 command
= isp1760_readl(hcd
->regs
+ HC_USBCMD
);
2137 command
&= ~CMD_RUN
;
2138 isp1760_writel(command
, hcd
->regs
+ HC_USBCMD
);
2141 static const struct hc_driver isp1760_hc_driver
= {
2142 .description
= "isp1760-hcd",
2143 .product_desc
= "NXP ISP1760 USB Host Controller",
2144 .hcd_priv_size
= sizeof(struct isp1760_hcd
),
2146 .flags
= HCD_MEMORY
| HCD_USB2
,
2147 .reset
= isp1760_hc_setup
,
2148 .start
= isp1760_run
,
2149 .stop
= isp1760_stop
,
2150 .shutdown
= isp1760_shutdown
,
2151 .urb_enqueue
= isp1760_urb_enqueue
,
2152 .urb_dequeue
= isp1760_urb_dequeue
,
2153 .endpoint_disable
= isp1760_endpoint_disable
,
2154 .get_frame_number
= isp1760_get_frame
,
2155 .hub_status_data
= isp1760_hub_status_data
,
2156 .hub_control
= isp1760_hub_control
,
2159 int __init
init_kmem_once(void)
2161 qtd_cachep
= kmem_cache_create("isp1760_qtd",
2162 sizeof(struct isp1760_qtd
), 0, SLAB_TEMPORARY
|
2163 SLAB_MEM_SPREAD
, NULL
);
2168 qh_cachep
= kmem_cache_create("isp1760_qh", sizeof(struct isp1760_qh
),
2169 0, SLAB_TEMPORARY
| SLAB_MEM_SPREAD
, NULL
);
2172 kmem_cache_destroy(qtd_cachep
);
2179 void deinit_kmem_cache(void)
2181 kmem_cache_destroy(qtd_cachep
);
2182 kmem_cache_destroy(qh_cachep
);
2185 struct usb_hcd
*isp1760_register(u64 res_start
, u64 res_len
, int irq
,
2186 u64 irqflags
, struct device
*dev
, const char *busname
)
2188 struct usb_hcd
*hcd
;
2189 struct isp1760_hcd
*priv
;
2193 return ERR_PTR(-ENODEV
);
2195 /* prevent usb-core allocating DMA pages */
2196 dev
->dma_mask
= NULL
;
2198 hcd
= usb_create_hcd(&isp1760_hc_driver
, dev
, dev
->bus_id
);
2200 return ERR_PTR(-ENOMEM
);
2202 priv
= hcd_to_priv(hcd
);
2204 hcd
->regs
= ioremap(res_start
, res_len
);
2210 ret
= usb_add_hcd(hcd
, irq
, irqflags
);
2215 hcd
->rsrc_start
= res_start
;
2216 hcd
->rsrc_len
= res_len
;
2226 return ERR_PTR(ret
);
2229 MODULE_DESCRIPTION("Driver for the ISP1760 USB-controller from NXP");
2230 MODULE_AUTHOR("Sebastian Siewior <bigeasy@linuxtronix.de>");
2231 MODULE_LICENSE("GPL v2");