2 * USB Host Controller Driver for IMX21
4 * Copyright (C) 2006 Loping Dog Embedded Systems
5 * Copyright (C) 2009 Martin Fuzzey
6 * Originally written by Jay Monkman <jtm@lopingdog.com>
7 * Ported to 2.6.30, debugged and enhanced by Martin Fuzzey
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 * The i.MX21 USB hardware contains
27 * * 32 transfer descriptors (called ETDs)
28 * * 4Kb of Data memory
30 * The data memory is shared between the host and function controllers
31 * (but this driver only supports the host controller)
33 * So setting up a transfer involves:
35 * * Fill in ETD with appropriate information
36 * * Allocating data memory (and putting the offset in the ETD)
38 * * Get interrupt when done.
40 * An ETD is assigned to each active endpoint.
42 * Low resource (ETD and Data memory) situations are handled differently for
43 * isochronous and non insosynchronous transactions :
45 * Non ISOC transfers are queued if either ETDs or Data memory are unavailable
47 * ISOC transfers use 2 ETDs per endpoint to achieve double buffering.
48 * They allocate both ETDs and Data memory during URB submission
49 * (and fail if unavailable).
52 #include <linux/clk.h>
54 #include <linux/kernel.h>
55 #include <linux/list.h>
56 #include <linux/platform_device.h>
57 #include <linux/slab.h>
58 #include <linux/usb.h>
59 #include <linux/usb/hcd.h>
60 #include <linux/dma-mapping.h>
61 #include <linux/module.h>
63 #include "imx21-hcd.h"
65 #ifdef CONFIG_DYNAMIC_DEBUG
70 #define DEBUG_LOG_FRAME(imx21, etd, event) \
71 (etd)->event##_frame = readl((imx21)->regs + USBH_FRMNUB)
73 #define DEBUG_LOG_FRAME(imx21, etd, event) do { } while (0)
76 static const char hcd_name
[] = "imx21-hcd";
78 static inline struct imx21
*hcd_to_imx21(struct usb_hcd
*hcd
)
80 return (struct imx21
*)hcd
->hcd_priv
;
84 /* =========================================== */
85 /* Hardware access helpers */
86 /* =========================================== */
88 static inline void set_register_bits(struct imx21
*imx21
, u32 offset
, u32 mask
)
90 void __iomem
*reg
= imx21
->regs
+ offset
;
91 writel(readl(reg
) | mask
, reg
);
94 static inline void clear_register_bits(struct imx21
*imx21
,
97 void __iomem
*reg
= imx21
->regs
+ offset
;
98 writel(readl(reg
) & ~mask
, reg
);
101 static inline void clear_toggle_bit(struct imx21
*imx21
, u32 offset
, u32 mask
)
103 void __iomem
*reg
= imx21
->regs
+ offset
;
105 if (readl(reg
) & mask
)
109 static inline void set_toggle_bit(struct imx21
*imx21
, u32 offset
, u32 mask
)
111 void __iomem
*reg
= imx21
->regs
+ offset
;
113 if (!(readl(reg
) & mask
))
117 static void etd_writel(struct imx21
*imx21
, int etd_num
, int dword
, u32 value
)
119 writel(value
, imx21
->regs
+ USB_ETD_DWORD(etd_num
, dword
));
122 static u32
etd_readl(struct imx21
*imx21
, int etd_num
, int dword
)
124 return readl(imx21
->regs
+ USB_ETD_DWORD(etd_num
, dword
));
127 static inline int wrap_frame(int counter
)
129 return counter
& 0xFFFF;
132 static inline int frame_after(int frame
, int after
)
134 /* handle wrapping like jiffies time_afer */
135 return (s16
)((s16
)after
- (s16
)frame
) < 0;
138 static int imx21_hc_get_frame(struct usb_hcd
*hcd
)
140 struct imx21
*imx21
= hcd_to_imx21(hcd
);
142 return wrap_frame(readl(imx21
->regs
+ USBH_FRMNUB
));
145 static inline bool unsuitable_for_dma(dma_addr_t addr
)
147 return (addr
& 3) != 0;
150 #include "imx21-dbg.c"
152 static void nonisoc_urb_completed_for_etd(
153 struct imx21
*imx21
, struct etd_priv
*etd
, int status
);
154 static void schedule_nonisoc_etd(struct imx21
*imx21
, struct urb
*urb
);
155 static void free_dmem(struct imx21
*imx21
, struct etd_priv
*etd
);
157 /* =========================================== */
159 /* =========================================== */
161 static int alloc_etd(struct imx21
*imx21
)
164 struct etd_priv
*etd
= imx21
->etd
;
166 for (i
= 0; i
< USB_NUM_ETD
; i
++, etd
++) {
167 if (etd
->alloc
== 0) {
168 memset(etd
, 0, sizeof(imx21
->etd
[0]));
170 debug_etd_allocated(imx21
);
177 static void disactivate_etd(struct imx21
*imx21
, int num
)
179 int etd_mask
= (1 << num
);
180 struct etd_priv
*etd
= &imx21
->etd
[num
];
182 writel(etd_mask
, imx21
->regs
+ USBH_ETDENCLR
);
183 clear_register_bits(imx21
, USBH_ETDDONEEN
, etd_mask
);
184 writel(etd_mask
, imx21
->regs
+ USB_ETDDMACHANLCLR
);
185 clear_toggle_bit(imx21
, USBH_ETDDONESTAT
, etd_mask
);
187 etd
->active_count
= 0;
189 DEBUG_LOG_FRAME(imx21
, etd
, disactivated
);
192 static void reset_etd(struct imx21
*imx21
, int num
)
194 struct etd_priv
*etd
= imx21
->etd
+ num
;
197 disactivate_etd(imx21
, num
);
199 for (i
= 0; i
< 4; i
++)
200 etd_writel(imx21
, num
, i
, 0);
204 etd
->bounce_buffer
= NULL
;
207 static void free_etd(struct imx21
*imx21
, int num
)
212 if (num
>= USB_NUM_ETD
) {
213 dev_err(imx21
->dev
, "BAD etd=%d!\n", num
);
216 if (imx21
->etd
[num
].alloc
== 0) {
217 dev_err(imx21
->dev
, "ETD %d already free!\n", num
);
221 debug_etd_freed(imx21
);
222 reset_etd(imx21
, num
);
223 memset(&imx21
->etd
[num
], 0, sizeof(imx21
->etd
[0]));
227 static void setup_etd_dword0(struct imx21
*imx21
,
228 int etd_num
, struct urb
*urb
, u8 dir
, u16 maxpacket
)
230 etd_writel(imx21
, etd_num
, 0,
231 ((u32
) usb_pipedevice(urb
->pipe
)) << DW0_ADDRESS
|
232 ((u32
) usb_pipeendpoint(urb
->pipe
) << DW0_ENDPNT
) |
233 ((u32
) dir
<< DW0_DIRECT
) |
234 ((u32
) ((urb
->dev
->speed
== USB_SPEED_LOW
) ?
235 1 : 0) << DW0_SPEED
) |
236 ((u32
) fmt_urb_to_etd
[usb_pipetype(urb
->pipe
)] << DW0_FORMAT
) |
237 ((u32
) maxpacket
<< DW0_MAXPKTSIZ
));
241 * Copy buffer to data controller data memory.
242 * We cannot use memcpy_toio() because the hardware requires 32bit writes
244 static void copy_to_dmem(
245 struct imx21
*imx21
, int dmem_offset
, void *src
, int count
)
247 void __iomem
*dmem
= imx21
->regs
+ USBOTG_DMEM
+ dmem_offset
;
253 for (i
= 0; i
< count
; i
++) {
255 word
+= (*p
++ << (byte
* 8));
263 if (count
&& byte
!= 3)
267 static void activate_etd(struct imx21
*imx21
, int etd_num
, u8 dir
)
269 u32 etd_mask
= 1 << etd_num
;
270 struct etd_priv
*etd
= &imx21
->etd
[etd_num
];
272 if (etd
->dma_handle
&& unsuitable_for_dma(etd
->dma_handle
)) {
273 /* For non aligned isoc the condition below is always true */
274 if (etd
->len
<= etd
->dmem_size
) {
275 /* Fits into data memory, use PIO */
276 if (dir
!= TD_DIR_IN
) {
279 etd
->cpu_buffer
, etd
->len
);
284 /* Too big for data memory, use bounce buffer */
285 enum dma_data_direction dmadir
;
287 if (dir
== TD_DIR_IN
) {
288 dmadir
= DMA_FROM_DEVICE
;
289 etd
->bounce_buffer
= kmalloc(etd
->len
,
292 dmadir
= DMA_TO_DEVICE
;
293 etd
->bounce_buffer
= kmemdup(etd
->cpu_buffer
,
297 if (!etd
->bounce_buffer
) {
298 dev_err(imx21
->dev
, "failed bounce alloc\n");
299 goto err_bounce_alloc
;
303 dma_map_single(imx21
->dev
,
307 if (dma_mapping_error(imx21
->dev
, etd
->dma_handle
)) {
308 dev_err(imx21
->dev
, "failed bounce map\n");
314 clear_toggle_bit(imx21
, USBH_ETDDONESTAT
, etd_mask
);
315 set_register_bits(imx21
, USBH_ETDDONEEN
, etd_mask
);
316 clear_toggle_bit(imx21
, USBH_XFILLSTAT
, etd_mask
);
317 clear_toggle_bit(imx21
, USBH_YFILLSTAT
, etd_mask
);
319 if (etd
->dma_handle
) {
320 set_register_bits(imx21
, USB_ETDDMACHANLCLR
, etd_mask
);
321 clear_toggle_bit(imx21
, USBH_XBUFSTAT
, etd_mask
);
322 clear_toggle_bit(imx21
, USBH_YBUFSTAT
, etd_mask
);
323 writel(etd
->dma_handle
, imx21
->regs
+ USB_ETDSMSA(etd_num
));
324 set_register_bits(imx21
, USB_ETDDMAEN
, etd_mask
);
326 if (dir
!= TD_DIR_IN
) {
327 /* need to set for ZLP and PIO */
328 set_toggle_bit(imx21
, USBH_XFILLSTAT
, etd_mask
);
329 set_toggle_bit(imx21
, USBH_YFILLSTAT
, etd_mask
);
333 DEBUG_LOG_FRAME(imx21
, etd
, activated
);
336 if (!etd
->active_count
) {
338 etd
->activated_frame
= readl(imx21
->regs
+ USBH_FRMNUB
);
339 etd
->disactivated_frame
= -1;
340 etd
->last_int_frame
= -1;
341 etd
->last_req_frame
= -1;
343 for (i
= 0; i
< 4; i
++)
344 etd
->submitted_dwords
[i
] = etd_readl(imx21
, etd_num
, i
);
348 etd
->active_count
= 1;
349 writel(etd_mask
, imx21
->regs
+ USBH_ETDENSET
);
353 kfree(etd
->bounce_buffer
);
356 free_dmem(imx21
, etd
);
357 nonisoc_urb_completed_for_etd(imx21
, etd
, -ENOMEM
);
360 /* =========================================== */
361 /* Data memory management */
362 /* =========================================== */
364 static int alloc_dmem(struct imx21
*imx21
, unsigned int size
,
365 struct usb_host_endpoint
*ep
)
367 unsigned int offset
= 0;
368 struct imx21_dmem_area
*area
;
369 struct imx21_dmem_area
*tmp
;
371 size
+= (~size
+ 1) & 0x3; /* Round to 4 byte multiple */
373 if (size
> DMEM_SIZE
) {
374 dev_err(imx21
->dev
, "size=%d > DMEM_SIZE(%d)\n",
379 list_for_each_entry(tmp
, &imx21
->dmem_list
, list
) {
380 if ((size
+ offset
) < offset
)
382 if ((size
+ offset
) <= tmp
->offset
)
384 offset
= tmp
->size
+ tmp
->offset
;
385 if ((offset
+ size
) > DMEM_SIZE
)
389 area
= kmalloc(sizeof(struct imx21_dmem_area
), GFP_ATOMIC
);
394 area
->offset
= offset
;
396 list_add_tail(&area
->list
, &tmp
->list
);
397 debug_dmem_allocated(imx21
, size
);
404 /* Memory now available for a queued ETD - activate it */
405 static void activate_queued_etd(struct imx21
*imx21
,
406 struct etd_priv
*etd
, u32 dmem_offset
)
408 struct urb_priv
*urb_priv
= etd
->urb
->hcpriv
;
409 int etd_num
= etd
- &imx21
->etd
[0];
410 u32 maxpacket
= etd_readl(imx21
, etd_num
, 1) >> DW1_YBUFSRTAD
;
411 u8 dir
= (etd_readl(imx21
, etd_num
, 2) >> DW2_DIRPID
) & 0x03;
413 dev_dbg(imx21
->dev
, "activating queued ETD %d now DMEM available\n",
415 etd_writel(imx21
, etd_num
, 1,
416 ((dmem_offset
+ maxpacket
) << DW1_YBUFSRTAD
) | dmem_offset
);
418 etd
->dmem_offset
= dmem_offset
;
419 urb_priv
->active
= 1;
420 activate_etd(imx21
, etd_num
, dir
);
423 static void free_dmem(struct imx21
*imx21
, struct etd_priv
*etd
)
425 struct imx21_dmem_area
*area
;
426 struct etd_priv
*tmp
;
434 offset
= etd
->dmem_offset
;
435 list_for_each_entry(area
, &imx21
->dmem_list
, list
) {
436 if (area
->offset
== offset
) {
437 debug_dmem_freed(imx21
, area
->size
);
438 list_del(&area
->list
);
447 "Trying to free unallocated DMEM %d\n", offset
);
451 /* Try again to allocate memory for anything we've queued */
452 list_for_each_entry_safe(etd
, tmp
, &imx21
->queue_for_dmem
, queue
) {
453 offset
= alloc_dmem(imx21
, etd
->dmem_size
, etd
->ep
);
455 list_del(&etd
->queue
);
456 activate_queued_etd(imx21
, etd
, (u32
)offset
);
461 static void free_epdmem(struct imx21
*imx21
, struct usb_host_endpoint
*ep
)
463 struct imx21_dmem_area
*area
, *tmp
;
465 list_for_each_entry_safe(area
, tmp
, &imx21
->dmem_list
, list
) {
466 if (area
->ep
== ep
) {
468 "Active DMEM %d for disabled ep=%p\n",
470 list_del(&area
->list
);
477 /* =========================================== */
479 /* =========================================== */
481 /* Endpoint now idle - release its ETD(s) or assign to queued request */
482 static void ep_idle(struct imx21
*imx21
, struct ep_priv
*ep_priv
)
486 for (i
= 0; i
< NUM_ISO_ETDS
; i
++) {
487 int etd_num
= ep_priv
->etd
[i
];
488 struct etd_priv
*etd
;
492 etd
= &imx21
->etd
[etd_num
];
493 ep_priv
->etd
[i
] = -1;
495 free_dmem(imx21
, etd
); /* for isoc */
497 if (list_empty(&imx21
->queue_for_etd
)) {
498 free_etd(imx21
, etd_num
);
503 "assigning idle etd %d for queued request\n", etd_num
);
504 ep_priv
= list_first_entry(&imx21
->queue_for_etd
,
505 struct ep_priv
, queue
);
506 list_del(&ep_priv
->queue
);
507 reset_etd(imx21
, etd_num
);
508 ep_priv
->waiting_etd
= 0;
509 ep_priv
->etd
[i
] = etd_num
;
511 if (list_empty(&ep_priv
->ep
->urb_list
)) {
512 dev_err(imx21
->dev
, "No urb for queued ep!\n");
515 schedule_nonisoc_etd(imx21
, list_first_entry(
516 &ep_priv
->ep
->urb_list
, struct urb
, urb_list
));
520 static void urb_done(struct usb_hcd
*hcd
, struct urb
*urb
, int status
)
521 __releases(imx21
->lock
)
522 __acquires(imx21
->lock
)
524 struct imx21
*imx21
= hcd_to_imx21(hcd
);
525 struct ep_priv
*ep_priv
= urb
->ep
->hcpriv
;
526 struct urb_priv
*urb_priv
= urb
->hcpriv
;
528 debug_urb_completed(imx21
, urb
, status
);
529 dev_vdbg(imx21
->dev
, "urb %p done %d\n", urb
, status
);
531 kfree(urb_priv
->isoc_td
);
534 usb_hcd_unlink_urb_from_ep(hcd
, urb
);
535 spin_unlock(&imx21
->lock
);
536 usb_hcd_giveback_urb(hcd
, urb
, status
);
537 spin_lock(&imx21
->lock
);
538 if (list_empty(&ep_priv
->ep
->urb_list
))
539 ep_idle(imx21
, ep_priv
);
542 static void nonisoc_urb_completed_for_etd(
543 struct imx21
*imx21
, struct etd_priv
*etd
, int status
)
545 struct usb_host_endpoint
*ep
= etd
->ep
;
547 urb_done(imx21
->hcd
, etd
->urb
, status
);
550 if (!list_empty(&ep
->urb_list
)) {
551 struct urb
*urb
= list_first_entry(
552 &ep
->urb_list
, struct urb
, urb_list
);
554 dev_vdbg(imx21
->dev
, "next URB %p\n", urb
);
555 schedule_nonisoc_etd(imx21
, urb
);
560 /* =========================================== */
561 /* ISOC Handling ... */
562 /* =========================================== */
564 static void schedule_isoc_etds(struct usb_hcd
*hcd
,
565 struct usb_host_endpoint
*ep
)
567 struct imx21
*imx21
= hcd_to_imx21(hcd
);
568 struct ep_priv
*ep_priv
= ep
->hcpriv
;
569 struct etd_priv
*etd
;
570 struct urb_priv
*urb_priv
;
577 for (i
= 0; i
< NUM_ISO_ETDS
; i
++) {
579 if (list_empty(&ep_priv
->td_list
))
582 etd_num
= ep_priv
->etd
[i
];
586 etd
= &imx21
->etd
[etd_num
];
590 td
= list_entry(ep_priv
->td_list
.next
, struct td
, list
);
592 urb_priv
= td
->urb
->hcpriv
;
594 cur_frame
= imx21_hc_get_frame(hcd
);
595 if (frame_after(cur_frame
, td
->frame
)) {
596 dev_dbg(imx21
->dev
, "isoc too late frame %d > %d\n",
597 cur_frame
, td
->frame
);
598 urb_priv
->isoc_status
= -EXDEV
;
599 td
->urb
->iso_frame_desc
[
600 td
->isoc_index
].actual_length
= 0;
601 td
->urb
->iso_frame_desc
[td
->isoc_index
].status
= -EXDEV
;
602 if (--urb_priv
->isoc_remaining
== 0)
603 urb_done(hcd
, td
->urb
, urb_priv
->isoc_status
);
607 urb_priv
->active
= 1;
612 etd
->dma_handle
= td
->dma_handle
;
613 etd
->cpu_buffer
= td
->cpu_buffer
;
615 debug_isoc_submitted(imx21
, cur_frame
, td
);
617 dir
= usb_pipeout(td
->urb
->pipe
) ? TD_DIR_OUT
: TD_DIR_IN
;
618 setup_etd_dword0(imx21
, etd_num
, td
->urb
, dir
, etd
->dmem_size
);
619 etd_writel(imx21
, etd_num
, 1, etd
->dmem_offset
);
620 etd_writel(imx21
, etd_num
, 2,
621 (TD_NOTACCESSED
<< DW2_COMPCODE
) |
622 ((td
->frame
& 0xFFFF) << DW2_STARTFRM
));
623 etd_writel(imx21
, etd_num
, 3,
624 (TD_NOTACCESSED
<< DW3_COMPCODE0
) |
625 (td
->len
<< DW3_PKTLEN0
));
627 activate_etd(imx21
, etd_num
, dir
);
631 static void isoc_etd_done(struct usb_hcd
*hcd
, int etd_num
)
633 struct imx21
*imx21
= hcd_to_imx21(hcd
);
634 int etd_mask
= 1 << etd_num
;
635 struct etd_priv
*etd
= imx21
->etd
+ etd_num
;
636 struct urb
*urb
= etd
->urb
;
637 struct urb_priv
*urb_priv
= urb
->hcpriv
;
638 struct td
*td
= etd
->td
;
639 struct usb_host_endpoint
*ep
= etd
->ep
;
640 int isoc_index
= td
->isoc_index
;
641 unsigned int pipe
= urb
->pipe
;
642 int dir_in
= usb_pipein(pipe
);
646 disactivate_etd(imx21
, etd_num
);
648 cc
= (etd_readl(imx21
, etd_num
, 3) >> DW3_COMPCODE0
) & 0xf;
649 bytes_xfrd
= etd_readl(imx21
, etd_num
, 3) & 0x3ff;
651 /* Input doesn't always fill the buffer, don't generate an error
654 if (dir_in
&& (cc
== TD_DATAUNDERRUN
))
657 if (cc
== TD_NOTACCESSED
)
660 debug_isoc_completed(imx21
,
661 imx21_hc_get_frame(hcd
), td
, cc
, bytes_xfrd
);
663 urb_priv
->isoc_status
= -EXDEV
;
665 "bad iso cc=0x%X frame=%d sched frame=%d "
666 "cnt=%d len=%d urb=%p etd=%d index=%d\n",
667 cc
, imx21_hc_get_frame(hcd
), td
->frame
,
668 bytes_xfrd
, td
->len
, urb
, etd_num
, isoc_index
);
672 clear_toggle_bit(imx21
, USBH_XFILLSTAT
, etd_mask
);
673 if (!etd
->dma_handle
)
674 memcpy_fromio(etd
->cpu_buffer
,
675 imx21
->regs
+ USBOTG_DMEM
+ etd
->dmem_offset
,
679 urb
->actual_length
+= bytes_xfrd
;
680 urb
->iso_frame_desc
[isoc_index
].actual_length
= bytes_xfrd
;
681 urb
->iso_frame_desc
[isoc_index
].status
= cc_to_error
[cc
];
687 if (--urb_priv
->isoc_remaining
== 0)
688 urb_done(hcd
, urb
, urb_priv
->isoc_status
);
690 schedule_isoc_etds(hcd
, ep
);
693 static struct ep_priv
*alloc_isoc_ep(
694 struct imx21
*imx21
, struct usb_host_endpoint
*ep
)
696 struct ep_priv
*ep_priv
;
699 ep_priv
= kzalloc(sizeof(struct ep_priv
), GFP_ATOMIC
);
703 for (i
= 0; i
< NUM_ISO_ETDS
; i
++)
704 ep_priv
->etd
[i
] = -1;
706 INIT_LIST_HEAD(&ep_priv
->td_list
);
708 ep
->hcpriv
= ep_priv
;
712 static int alloc_isoc_etds(struct imx21
*imx21
, struct ep_priv
*ep_priv
)
717 /* Allocate the ETDs if required */
718 for (i
= 0; i
< NUM_ISO_ETDS
; i
++) {
719 if (ep_priv
->etd
[i
] < 0) {
720 etd_num
= alloc_etd(imx21
);
722 goto alloc_etd_failed
;
724 ep_priv
->etd
[i
] = etd_num
;
725 imx21
->etd
[etd_num
].ep
= ep_priv
->ep
;
731 dev_err(imx21
->dev
, "isoc: Couldn't allocate etd\n");
732 for (j
= 0; j
< i
; j
++) {
733 free_etd(imx21
, ep_priv
->etd
[j
]);
734 ep_priv
->etd
[j
] = -1;
739 static int imx21_hc_urb_enqueue_isoc(struct usb_hcd
*hcd
,
740 struct usb_host_endpoint
*ep
,
741 struct urb
*urb
, gfp_t mem_flags
)
743 struct imx21
*imx21
= hcd_to_imx21(hcd
);
744 struct urb_priv
*urb_priv
;
746 struct ep_priv
*ep_priv
;
747 struct td
*td
= NULL
;
753 urb_priv
= kzalloc(sizeof(struct urb_priv
), mem_flags
);
754 if (urb_priv
== NULL
)
757 urb_priv
->isoc_td
= kzalloc(
758 sizeof(struct td
) * urb
->number_of_packets
, mem_flags
);
759 if (urb_priv
->isoc_td
== NULL
) {
761 goto alloc_td_failed
;
764 spin_lock_irqsave(&imx21
->lock
, flags
);
766 if (ep
->hcpriv
== NULL
) {
767 ep_priv
= alloc_isoc_ep(imx21
, ep
);
768 if (ep_priv
== NULL
) {
770 goto alloc_ep_failed
;
773 ep_priv
= ep
->hcpriv
;
776 ret
= alloc_isoc_etds(imx21
, ep_priv
);
778 goto alloc_etd_failed
;
780 ret
= usb_hcd_link_urb_to_ep(hcd
, urb
);
784 urb
->status
= -EINPROGRESS
;
785 urb
->actual_length
= 0;
786 urb
->error_count
= 0;
787 urb
->hcpriv
= urb_priv
;
790 /* allocate data memory for largest packets if not already done */
791 maxpacket
= usb_maxpacket(urb
->dev
, urb
->pipe
, usb_pipeout(urb
->pipe
));
792 for (i
= 0; i
< NUM_ISO_ETDS
; i
++) {
793 struct etd_priv
*etd
= &imx21
->etd
[ep_priv
->etd
[i
]];
795 if (etd
->dmem_size
> 0 && etd
->dmem_size
< maxpacket
) {
796 /* not sure if this can really occur.... */
797 dev_err(imx21
->dev
, "increasing isoc buffer %d->%d\n",
798 etd
->dmem_size
, maxpacket
);
800 goto alloc_dmem_failed
;
803 if (etd
->dmem_size
== 0) {
804 etd
->dmem_offset
= alloc_dmem(imx21
, maxpacket
, ep
);
805 if (etd
->dmem_offset
< 0) {
806 dev_dbg(imx21
->dev
, "failed alloc isoc dmem\n");
808 goto alloc_dmem_failed
;
810 etd
->dmem_size
= maxpacket
;
814 /* calculate frame */
815 cur_frame
= imx21_hc_get_frame(hcd
);
817 if (list_empty(&ep_priv
->td_list
)) {
818 urb
->start_frame
= wrap_frame(cur_frame
+ 5);
820 urb
->start_frame
= wrap_frame(list_entry(ep_priv
->td_list
.prev
,
821 struct td
, list
)->frame
+ urb
->interval
);
823 if (frame_after(cur_frame
, urb
->start_frame
)) {
825 "enqueue: adjusting iso start %d (cur=%d) asap=%d\n",
826 urb
->start_frame
, cur_frame
,
827 (urb
->transfer_flags
& URB_ISO_ASAP
) != 0);
828 i
= DIV_ROUND_UP(wrap_frame(
829 cur_frame
- urb
->start_frame
),
832 /* Treat underruns as if URB_ISO_ASAP was set */
833 if ((urb
->transfer_flags
& URB_ISO_ASAP
) ||
834 i
>= urb
->number_of_packets
) {
835 urb
->start_frame
= wrap_frame(urb
->start_frame
836 + i
* urb
->interval
);
842 /* set up transfers */
843 urb_priv
->isoc_remaining
= urb
->number_of_packets
- i
;
844 td
= urb_priv
->isoc_td
;
845 for (; i
< urb
->number_of_packets
; i
++, td
++) {
846 unsigned int offset
= urb
->iso_frame_desc
[i
].offset
;
849 td
->len
= urb
->iso_frame_desc
[i
].length
;
851 td
->frame
= wrap_frame(urb
->start_frame
+ urb
->interval
* i
);
852 td
->dma_handle
= urb
->transfer_dma
+ offset
;
853 td
->cpu_buffer
= urb
->transfer_buffer
+ offset
;
854 list_add_tail(&td
->list
, &ep_priv
->td_list
);
857 dev_vdbg(imx21
->dev
, "setup %d packets for iso frame %d->%d\n",
858 urb
->number_of_packets
, urb
->start_frame
, td
->frame
);
860 debug_urb_submitted(imx21
, urb
);
861 schedule_isoc_etds(hcd
, ep
);
863 spin_unlock_irqrestore(&imx21
->lock
, flags
);
867 usb_hcd_unlink_urb_from_ep(hcd
, urb
);
872 spin_unlock_irqrestore(&imx21
->lock
, flags
);
873 kfree(urb_priv
->isoc_td
);
880 static void dequeue_isoc_urb(struct imx21
*imx21
,
881 struct urb
*urb
, struct ep_priv
*ep_priv
)
883 struct urb_priv
*urb_priv
= urb
->hcpriv
;
887 if (urb_priv
->active
) {
888 for (i
= 0; i
< NUM_ISO_ETDS
; i
++) {
889 int etd_num
= ep_priv
->etd
[i
];
890 if (etd_num
!= -1 && imx21
->etd
[etd_num
].urb
== urb
) {
891 struct etd_priv
*etd
= imx21
->etd
+ etd_num
;
893 reset_etd(imx21
, etd_num
);
894 free_dmem(imx21
, etd
);
899 list_for_each_entry_safe(td
, tmp
, &ep_priv
->td_list
, list
) {
900 if (td
->urb
== urb
) {
901 dev_vdbg(imx21
->dev
, "removing td %p\n", td
);
907 /* =========================================== */
908 /* NON ISOC Handling ... */
909 /* =========================================== */
911 static void schedule_nonisoc_etd(struct imx21
*imx21
, struct urb
*urb
)
913 unsigned int pipe
= urb
->pipe
;
914 struct urb_priv
*urb_priv
= urb
->hcpriv
;
915 struct ep_priv
*ep_priv
= urb_priv
->ep
->hcpriv
;
916 int state
= urb_priv
->state
;
917 int etd_num
= ep_priv
->etd
[0];
918 struct etd_priv
*etd
;
929 dev_err(imx21
->dev
, "No valid ETD\n");
932 if (readl(imx21
->regs
+ USBH_ETDENSET
) & (1 << etd_num
))
933 dev_err(imx21
->dev
, "submitting to active ETD %d\n", etd_num
);
935 etd
= &imx21
->etd
[etd_num
];
936 maxpacket
= usb_maxpacket(urb
->dev
, pipe
, usb_pipeout(pipe
));
940 if (usb_pipecontrol(pipe
) && (state
!= US_CTRL_DATA
)) {
941 if (state
== US_CTRL_SETUP
) {
943 if (unsuitable_for_dma(urb
->setup_dma
))
944 usb_hcd_unmap_urb_setup_for_dma(imx21
->hcd
,
946 etd
->dma_handle
= urb
->setup_dma
;
947 etd
->cpu_buffer
= urb
->setup_packet
;
950 datatoggle
= TD_TOGGLE_DATA0
;
951 } else { /* US_CTRL_ACK */
952 dir
= usb_pipeout(pipe
) ? TD_DIR_IN
: TD_DIR_OUT
;
955 datatoggle
= TD_TOGGLE_DATA1
;
958 dir
= usb_pipeout(pipe
) ? TD_DIR_OUT
: TD_DIR_IN
;
959 bufround
= (dir
== TD_DIR_IN
) ? 1 : 0;
960 if (unsuitable_for_dma(urb
->transfer_dma
))
961 usb_hcd_unmap_urb_for_dma(imx21
->hcd
, urb
);
963 etd
->dma_handle
= urb
->transfer_dma
;
964 etd
->cpu_buffer
= urb
->transfer_buffer
;
965 if (usb_pipebulk(pipe
) && (state
== US_BULK0
))
968 count
= urb
->transfer_buffer_length
;
970 if (usb_pipecontrol(pipe
)) {
971 datatoggle
= TD_TOGGLE_DATA1
;
975 usb_pipeendpoint(urb
->pipe
),
976 usb_pipeout(urb
->pipe
)))
977 datatoggle
= TD_TOGGLE_DATA1
;
979 datatoggle
= TD_TOGGLE_DATA0
;
984 etd
->ep
= urb_priv
->ep
;
987 if (usb_pipeint(pipe
)) {
988 interval
= urb
->interval
;
989 relpolpos
= (readl(imx21
->regs
+ USBH_FRMNUB
) + 1) & 0xff;
992 /* Write ETD to device memory */
993 setup_etd_dword0(imx21
, etd_num
, urb
, dir
, maxpacket
);
995 etd_writel(imx21
, etd_num
, 2,
996 (u32
) interval
<< DW2_POLINTERV
|
997 ((u32
) relpolpos
<< DW2_RELPOLPOS
) |
998 ((u32
) dir
<< DW2_DIRPID
) |
999 ((u32
) bufround
<< DW2_BUFROUND
) |
1000 ((u32
) datatoggle
<< DW2_DATATOG
) |
1001 ((u32
) TD_NOTACCESSED
<< DW2_COMPCODE
));
1003 /* DMA will always transfer buffer size even if TOBYCNT in DWORD3
1004 is smaller. Make sure we don't overrun the buffer!
1006 if (count
&& count
< maxpacket
)
1007 etd_buf_size
= count
;
1009 etd_buf_size
= maxpacket
;
1011 etd_writel(imx21
, etd_num
, 3,
1012 ((u32
) (etd_buf_size
- 1) << DW3_BUFSIZE
) | (u32
) count
);
1015 etd
->dma_handle
= 0;
1017 /* allocate x and y buffer space at once */
1018 etd
->dmem_size
= (count
> maxpacket
) ? maxpacket
* 2 : maxpacket
;
1019 etd
->dmem_offset
= alloc_dmem(imx21
, etd
->dmem_size
, urb_priv
->ep
);
1020 if (etd
->dmem_offset
< 0) {
1021 /* Setup everything we can in HW and update when we get DMEM */
1022 etd_writel(imx21
, etd_num
, 1, (u32
)maxpacket
<< 16);
1024 dev_dbg(imx21
->dev
, "Queuing etd %d for DMEM\n", etd_num
);
1025 debug_urb_queued_for_dmem(imx21
, urb
);
1026 list_add_tail(&etd
->queue
, &imx21
->queue_for_dmem
);
1030 etd_writel(imx21
, etd_num
, 1,
1031 (((u32
) etd
->dmem_offset
+ (u32
) maxpacket
) << DW1_YBUFSRTAD
) |
1032 (u32
) etd
->dmem_offset
);
1034 urb_priv
->active
= 1;
1036 /* enable the ETD to kick off transfer */
1037 dev_vdbg(imx21
->dev
, "Activating etd %d for %d bytes %s\n",
1038 etd_num
, count
, dir
!= TD_DIR_IN
? "out" : "in");
1039 activate_etd(imx21
, etd_num
, dir
);
1043 static void nonisoc_etd_done(struct usb_hcd
*hcd
, int etd_num
)
1045 struct imx21
*imx21
= hcd_to_imx21(hcd
);
1046 struct etd_priv
*etd
= &imx21
->etd
[etd_num
];
1047 struct urb
*urb
= etd
->urb
;
1048 u32 etd_mask
= 1 << etd_num
;
1049 struct urb_priv
*urb_priv
= urb
->hcpriv
;
1055 disactivate_etd(imx21
, etd_num
);
1057 dir
= (etd_readl(imx21
, etd_num
, 0) >> DW0_DIRECT
) & 0x3;
1058 cc
= (etd_readl(imx21
, etd_num
, 2) >> DW2_COMPCODE
) & 0xf;
1059 bytes_xfrd
= etd
->len
- (etd_readl(imx21
, etd_num
, 3) & 0x1fffff);
1061 /* save toggle carry */
1062 usb_settoggle(urb
->dev
, usb_pipeendpoint(urb
->pipe
),
1063 usb_pipeout(urb
->pipe
),
1064 (etd_readl(imx21
, etd_num
, 0) >> DW0_TOGCRY
) & 0x1);
1066 if (dir
== TD_DIR_IN
) {
1067 clear_toggle_bit(imx21
, USBH_XFILLSTAT
, etd_mask
);
1068 clear_toggle_bit(imx21
, USBH_YFILLSTAT
, etd_mask
);
1070 if (etd
->bounce_buffer
) {
1071 memcpy(etd
->cpu_buffer
, etd
->bounce_buffer
, bytes_xfrd
);
1072 dma_unmap_single(imx21
->dev
,
1073 etd
->dma_handle
, etd
->len
, DMA_FROM_DEVICE
);
1074 } else if (!etd
->dma_handle
&& bytes_xfrd
) {/* PIO */
1075 memcpy_fromio(etd
->cpu_buffer
,
1076 imx21
->regs
+ USBOTG_DMEM
+ etd
->dmem_offset
,
1081 kfree(etd
->bounce_buffer
);
1082 etd
->bounce_buffer
= NULL
;
1083 free_dmem(imx21
, etd
);
1085 urb
->error_count
= 0;
1086 if (!(urb
->transfer_flags
& URB_SHORT_NOT_OK
)
1087 && (cc
== TD_DATAUNDERRUN
))
1091 dev_vdbg(imx21
->dev
, "cc is 0x%x\n", cc
);
1093 etd_done
= (cc_to_error
[cc
] != 0); /* stop if error */
1095 switch (usb_pipetype(urb
->pipe
)) {
1097 switch (urb_priv
->state
) {
1099 if (urb
->transfer_buffer_length
> 0)
1100 urb_priv
->state
= US_CTRL_DATA
;
1102 urb_priv
->state
= US_CTRL_ACK
;
1105 urb
->actual_length
+= bytes_xfrd
;
1106 urb_priv
->state
= US_CTRL_ACK
;
1113 "Invalid pipe state %d\n", urb_priv
->state
);
1120 urb
->actual_length
+= bytes_xfrd
;
1121 if ((urb_priv
->state
== US_BULK
)
1122 && (urb
->transfer_flags
& URB_ZERO_PACKET
)
1123 && urb
->transfer_buffer_length
> 0
1124 && ((urb
->transfer_buffer_length
%
1125 usb_maxpacket(urb
->dev
, urb
->pipe
,
1126 usb_pipeout(urb
->pipe
))) == 0)) {
1127 /* need a 0-packet */
1128 urb_priv
->state
= US_BULK0
;
1134 case PIPE_INTERRUPT
:
1135 urb
->actual_length
+= bytes_xfrd
;
1141 nonisoc_urb_completed_for_etd(imx21
, etd
, cc_to_error
[cc
]);
1143 dev_vdbg(imx21
->dev
, "next state=%d\n", urb_priv
->state
);
1144 schedule_nonisoc_etd(imx21
, urb
);
1149 static struct ep_priv
*alloc_ep(void)
1152 struct ep_priv
*ep_priv
;
1154 ep_priv
= kzalloc(sizeof(struct ep_priv
), GFP_ATOMIC
);
1158 for (i
= 0; i
< NUM_ISO_ETDS
; ++i
)
1159 ep_priv
->etd
[i
] = -1;
1164 static int imx21_hc_urb_enqueue(struct usb_hcd
*hcd
,
1165 struct urb
*urb
, gfp_t mem_flags
)
1167 struct imx21
*imx21
= hcd_to_imx21(hcd
);
1168 struct usb_host_endpoint
*ep
= urb
->ep
;
1169 struct urb_priv
*urb_priv
;
1170 struct ep_priv
*ep_priv
;
1171 struct etd_priv
*etd
;
1173 unsigned long flags
;
1175 dev_vdbg(imx21
->dev
,
1176 "enqueue urb=%p ep=%p len=%d "
1177 "buffer=%p dma=%08X setupBuf=%p setupDma=%08X\n",
1179 urb
->transfer_buffer_length
,
1180 urb
->transfer_buffer
, urb
->transfer_dma
,
1181 urb
->setup_packet
, urb
->setup_dma
);
1183 if (usb_pipeisoc(urb
->pipe
))
1184 return imx21_hc_urb_enqueue_isoc(hcd
, ep
, urb
, mem_flags
);
1186 urb_priv
= kzalloc(sizeof(struct urb_priv
), mem_flags
);
1190 spin_lock_irqsave(&imx21
->lock
, flags
);
1192 ep_priv
= ep
->hcpriv
;
1193 if (ep_priv
== NULL
) {
1194 ep_priv
= alloc_ep();
1197 goto failed_alloc_ep
;
1199 ep
->hcpriv
= ep_priv
;
1203 ret
= usb_hcd_link_urb_to_ep(hcd
, urb
);
1207 urb
->status
= -EINPROGRESS
;
1208 urb
->actual_length
= 0;
1209 urb
->error_count
= 0;
1210 urb
->hcpriv
= urb_priv
;
1213 switch (usb_pipetype(urb
->pipe
)) {
1215 urb_priv
->state
= US_CTRL_SETUP
;
1218 urb_priv
->state
= US_BULK
;
1222 debug_urb_submitted(imx21
, urb
);
1223 if (ep_priv
->etd
[0] < 0) {
1224 if (ep_priv
->waiting_etd
) {
1226 "no ETD available already queued %p\n",
1228 debug_urb_queued_for_etd(imx21
, urb
);
1231 ep_priv
->etd
[0] = alloc_etd(imx21
);
1232 if (ep_priv
->etd
[0] < 0) {
1234 "no ETD available queueing %p\n", ep_priv
);
1235 debug_urb_queued_for_etd(imx21
, urb
);
1236 list_add_tail(&ep_priv
->queue
, &imx21
->queue_for_etd
);
1237 ep_priv
->waiting_etd
= 1;
1242 /* Schedule if no URB already active for this endpoint */
1243 etd
= &imx21
->etd
[ep_priv
->etd
[0]];
1244 if (etd
->urb
== NULL
) {
1245 DEBUG_LOG_FRAME(imx21
, etd
, last_req
);
1246 schedule_nonisoc_etd(imx21
, urb
);
1250 spin_unlock_irqrestore(&imx21
->lock
, flags
);
1255 spin_unlock_irqrestore(&imx21
->lock
, flags
);
1260 static int imx21_hc_urb_dequeue(struct usb_hcd
*hcd
, struct urb
*urb
,
1263 struct imx21
*imx21
= hcd_to_imx21(hcd
);
1264 unsigned long flags
;
1265 struct usb_host_endpoint
*ep
;
1266 struct ep_priv
*ep_priv
;
1267 struct urb_priv
*urb_priv
= urb
->hcpriv
;
1270 dev_vdbg(imx21
->dev
, "dequeue urb=%p iso=%d status=%d\n",
1271 urb
, usb_pipeisoc(urb
->pipe
), status
);
1273 spin_lock_irqsave(&imx21
->lock
, flags
);
1275 ret
= usb_hcd_check_unlink_urb(hcd
, urb
, status
);
1279 ep_priv
= ep
->hcpriv
;
1281 debug_urb_unlinked(imx21
, urb
);
1283 if (usb_pipeisoc(urb
->pipe
)) {
1284 dequeue_isoc_urb(imx21
, urb
, ep_priv
);
1285 schedule_isoc_etds(hcd
, ep
);
1286 } else if (urb_priv
->active
) {
1287 int etd_num
= ep_priv
->etd
[0];
1288 if (etd_num
!= -1) {
1289 struct etd_priv
*etd
= &imx21
->etd
[etd_num
];
1291 disactivate_etd(imx21
, etd_num
);
1292 free_dmem(imx21
, etd
);
1294 kfree(etd
->bounce_buffer
);
1295 etd
->bounce_buffer
= NULL
;
1299 urb_done(hcd
, urb
, status
);
1301 spin_unlock_irqrestore(&imx21
->lock
, flags
);
1305 spin_unlock_irqrestore(&imx21
->lock
, flags
);
1309 /* =========================================== */
1310 /* Interrupt dispatch */
1311 /* =========================================== */
1313 static void process_etds(struct usb_hcd
*hcd
, struct imx21
*imx21
, int sof
)
1316 int enable_sof_int
= 0;
1317 unsigned long flags
;
1319 spin_lock_irqsave(&imx21
->lock
, flags
);
1321 for (etd_num
= 0; etd_num
< USB_NUM_ETD
; etd_num
++) {
1322 u32 etd_mask
= 1 << etd_num
;
1323 u32 enabled
= readl(imx21
->regs
+ USBH_ETDENSET
) & etd_mask
;
1324 u32 done
= readl(imx21
->regs
+ USBH_ETDDONESTAT
) & etd_mask
;
1325 struct etd_priv
*etd
= &imx21
->etd
[etd_num
];
1329 DEBUG_LOG_FRAME(imx21
, etd
, last_int
);
1334 * When multiple transfers are using the bus we sometimes get into a state
1335 * where the transfer has completed (the CC field of the ETD is != 0x0F),
1336 * the ETD has self disabled but the ETDDONESTAT flag is not set
1337 * (and hence no interrupt occurs).
1338 * This causes the transfer in question to hang.
1339 * The kludge below checks for this condition at each SOF and processes any
1340 * blocked ETDs (after an arbitrary 10 frame wait)
1342 * With a single active transfer the usbtest test suite will run for days
1343 * without the kludge.
1344 * With other bus activity (eg mass storage) even just test1 will hang without
1350 if (etd
->active_count
&& !enabled
) /* suspicious... */
1353 if (!sof
|| enabled
|| !etd
->active_count
)
1356 cc
= etd_readl(imx21
, etd_num
, 2) >> DW2_COMPCODE
;
1357 if (cc
== TD_NOTACCESSED
)
1360 if (++etd
->active_count
< 10)
1363 dword0
= etd_readl(imx21
, etd_num
, 0);
1365 "unblock ETD %d dev=0x%X ep=0x%X cc=0x%02X!\n",
1366 etd_num
, dword0
& 0x7F,
1367 (dword0
>> DW0_ENDPNT
) & 0x0F,
1372 "frame: act=%d disact=%d"
1373 " int=%d req=%d cur=%d\n",
1374 etd
->activated_frame
,
1375 etd
->disactivated_frame
,
1376 etd
->last_int_frame
,
1377 etd
->last_req_frame
,
1378 readl(imx21
->regs
+ USBH_FRMNUB
));
1379 imx21
->debug_unblocks
++;
1381 etd
->active_count
= 0;
1385 if (etd
->ep
== NULL
|| etd
->urb
== NULL
) {
1387 "Interrupt for unexpected etd %d"
1389 etd_num
, etd
->ep
, etd
->urb
);
1390 disactivate_etd(imx21
, etd_num
);
1394 if (usb_pipeisoc(etd
->urb
->pipe
))
1395 isoc_etd_done(hcd
, etd_num
);
1397 nonisoc_etd_done(hcd
, etd_num
);
1400 /* only enable SOF interrupt if it may be needed for the kludge */
1402 set_register_bits(imx21
, USBH_SYSIEN
, USBH_SYSIEN_SOFINT
);
1404 clear_register_bits(imx21
, USBH_SYSIEN
, USBH_SYSIEN_SOFINT
);
1407 spin_unlock_irqrestore(&imx21
->lock
, flags
);
1410 static irqreturn_t
imx21_irq(struct usb_hcd
*hcd
)
1412 struct imx21
*imx21
= hcd_to_imx21(hcd
);
1413 u32 ints
= readl(imx21
->regs
+ USBH_SYSISR
);
1415 if (ints
& USBH_SYSIEN_HERRINT
)
1416 dev_dbg(imx21
->dev
, "Scheduling error\n");
1418 if (ints
& USBH_SYSIEN_SORINT
)
1419 dev_dbg(imx21
->dev
, "Scheduling overrun\n");
1421 if (ints
& (USBH_SYSISR_DONEINT
| USBH_SYSISR_SOFINT
))
1422 process_etds(hcd
, imx21
, ints
& USBH_SYSISR_SOFINT
);
1424 writel(ints
, imx21
->regs
+ USBH_SYSISR
);
1428 static void imx21_hc_endpoint_disable(struct usb_hcd
*hcd
,
1429 struct usb_host_endpoint
*ep
)
1431 struct imx21
*imx21
= hcd_to_imx21(hcd
);
1432 unsigned long flags
;
1433 struct ep_priv
*ep_priv
;
1439 spin_lock_irqsave(&imx21
->lock
, flags
);
1440 ep_priv
= ep
->hcpriv
;
1441 dev_vdbg(imx21
->dev
, "disable ep=%p, ep->hcpriv=%p\n", ep
, ep_priv
);
1443 if (!list_empty(&ep
->urb_list
))
1444 dev_dbg(imx21
->dev
, "ep's URB list is not empty\n");
1446 if (ep_priv
!= NULL
) {
1447 for (i
= 0; i
< NUM_ISO_ETDS
; i
++) {
1448 if (ep_priv
->etd
[i
] > -1)
1449 dev_dbg(imx21
->dev
, "free etd %d for disable\n",
1452 free_etd(imx21
, ep_priv
->etd
[i
]);
1458 for (i
= 0; i
< USB_NUM_ETD
; i
++) {
1459 if (imx21
->etd
[i
].alloc
&& imx21
->etd
[i
].ep
== ep
) {
1461 "Active etd %d for disabled ep=%p!\n", i
, ep
);
1465 free_epdmem(imx21
, ep
);
1466 spin_unlock_irqrestore(&imx21
->lock
, flags
);
1469 /* =========================================== */
1471 /* =========================================== */
1473 static int get_hub_descriptor(struct usb_hcd
*hcd
,
1474 struct usb_hub_descriptor
*desc
)
1476 struct imx21
*imx21
= hcd_to_imx21(hcd
);
1477 desc
->bDescriptorType
= 0x29; /* HUB descriptor */
1478 desc
->bHubContrCurrent
= 0;
1480 desc
->bNbrPorts
= readl(imx21
->regs
+ USBH_ROOTHUBA
)
1481 & USBH_ROOTHUBA_NDNSTMPRT_MASK
;
1482 desc
->bDescLength
= 9;
1483 desc
->bPwrOn2PwrGood
= 0;
1484 desc
->wHubCharacteristics
= (__force __u16
) cpu_to_le16(
1485 0x0002 | /* No power switching */
1486 0x0010 | /* No over current protection */
1489 desc
->u
.hs
.DeviceRemovable
[0] = 1 << 1;
1490 desc
->u
.hs
.DeviceRemovable
[1] = ~0;
1494 static int imx21_hc_hub_status_data(struct usb_hcd
*hcd
, char *buf
)
1496 struct imx21
*imx21
= hcd_to_imx21(hcd
);
1500 unsigned long flags
;
1502 spin_lock_irqsave(&imx21
->lock
, flags
);
1503 ports
= readl(imx21
->regs
+ USBH_ROOTHUBA
)
1504 & USBH_ROOTHUBA_NDNSTMPRT_MASK
;
1507 dev_err(imx21
->dev
, "ports %d > 7\n", ports
);
1509 for (i
= 0; i
< ports
; i
++) {
1510 if (readl(imx21
->regs
+ USBH_PORTSTAT(i
)) &
1511 (USBH_PORTSTAT_CONNECTSC
|
1512 USBH_PORTSTAT_PRTENBLSC
|
1513 USBH_PORTSTAT_PRTSTATSC
|
1514 USBH_PORTSTAT_OVRCURIC
|
1515 USBH_PORTSTAT_PRTRSTSC
)) {
1518 buf
[0] |= 1 << (i
+ 1);
1521 spin_unlock_irqrestore(&imx21
->lock
, flags
);
1524 dev_info(imx21
->dev
, "Hub status changed\n");
1528 static int imx21_hc_hub_control(struct usb_hcd
*hcd
,
1530 u16 wValue
, u16 wIndex
, char *buf
, u16 wLength
)
1532 struct imx21
*imx21
= hcd_to_imx21(hcd
);
1534 u32 status_write
= 0;
1537 case ClearHubFeature
:
1538 dev_dbg(imx21
->dev
, "ClearHubFeature\n");
1540 case C_HUB_OVER_CURRENT
:
1541 dev_dbg(imx21
->dev
, " OVER_CURRENT\n");
1543 case C_HUB_LOCAL_POWER
:
1544 dev_dbg(imx21
->dev
, " LOCAL_POWER\n");
1547 dev_dbg(imx21
->dev
, " unknown\n");
1553 case ClearPortFeature
:
1554 dev_dbg(imx21
->dev
, "ClearPortFeature\n");
1556 case USB_PORT_FEAT_ENABLE
:
1557 dev_dbg(imx21
->dev
, " ENABLE\n");
1558 status_write
= USBH_PORTSTAT_CURCONST
;
1560 case USB_PORT_FEAT_SUSPEND
:
1561 dev_dbg(imx21
->dev
, " SUSPEND\n");
1562 status_write
= USBH_PORTSTAT_PRTOVRCURI
;
1564 case USB_PORT_FEAT_POWER
:
1565 dev_dbg(imx21
->dev
, " POWER\n");
1566 status_write
= USBH_PORTSTAT_LSDEVCON
;
1568 case USB_PORT_FEAT_C_ENABLE
:
1569 dev_dbg(imx21
->dev
, " C_ENABLE\n");
1570 status_write
= USBH_PORTSTAT_PRTENBLSC
;
1572 case USB_PORT_FEAT_C_SUSPEND
:
1573 dev_dbg(imx21
->dev
, " C_SUSPEND\n");
1574 status_write
= USBH_PORTSTAT_PRTSTATSC
;
1576 case USB_PORT_FEAT_C_CONNECTION
:
1577 dev_dbg(imx21
->dev
, " C_CONNECTION\n");
1578 status_write
= USBH_PORTSTAT_CONNECTSC
;
1580 case USB_PORT_FEAT_C_OVER_CURRENT
:
1581 dev_dbg(imx21
->dev
, " C_OVER_CURRENT\n");
1582 status_write
= USBH_PORTSTAT_OVRCURIC
;
1584 case USB_PORT_FEAT_C_RESET
:
1585 dev_dbg(imx21
->dev
, " C_RESET\n");
1586 status_write
= USBH_PORTSTAT_PRTRSTSC
;
1589 dev_dbg(imx21
->dev
, " unknown\n");
1596 case GetHubDescriptor
:
1597 dev_dbg(imx21
->dev
, "GetHubDescriptor\n");
1598 rc
= get_hub_descriptor(hcd
, (void *)buf
);
1602 dev_dbg(imx21
->dev
, " GetHubStatus\n");
1603 *(__le32
*) buf
= 0;
1607 dev_dbg(imx21
->dev
, "GetPortStatus: port: %d, 0x%x\n",
1608 wIndex
, USBH_PORTSTAT(wIndex
- 1));
1609 *(__le32
*) buf
= readl(imx21
->regs
+
1610 USBH_PORTSTAT(wIndex
- 1));
1614 dev_dbg(imx21
->dev
, "SetHubFeature\n");
1616 case C_HUB_OVER_CURRENT
:
1617 dev_dbg(imx21
->dev
, " OVER_CURRENT\n");
1620 case C_HUB_LOCAL_POWER
:
1621 dev_dbg(imx21
->dev
, " LOCAL_POWER\n");
1624 dev_dbg(imx21
->dev
, " unknown\n");
1631 case SetPortFeature
:
1632 dev_dbg(imx21
->dev
, "SetPortFeature\n");
1634 case USB_PORT_FEAT_SUSPEND
:
1635 dev_dbg(imx21
->dev
, " SUSPEND\n");
1636 status_write
= USBH_PORTSTAT_PRTSUSPST
;
1638 case USB_PORT_FEAT_POWER
:
1639 dev_dbg(imx21
->dev
, " POWER\n");
1640 status_write
= USBH_PORTSTAT_PRTPWRST
;
1642 case USB_PORT_FEAT_RESET
:
1643 dev_dbg(imx21
->dev
, " RESET\n");
1644 status_write
= USBH_PORTSTAT_PRTRSTST
;
1647 dev_dbg(imx21
->dev
, " unknown\n");
1654 dev_dbg(imx21
->dev
, " unknown\n");
1660 writel(status_write
, imx21
->regs
+ USBH_PORTSTAT(wIndex
- 1));
1664 /* =========================================== */
1665 /* Host controller management */
1666 /* =========================================== */
1668 static int imx21_hc_reset(struct usb_hcd
*hcd
)
1670 struct imx21
*imx21
= hcd_to_imx21(hcd
);
1671 unsigned long timeout
;
1672 unsigned long flags
;
1674 spin_lock_irqsave(&imx21
->lock
, flags
);
1676 /* Reset the Host controller modules */
1677 writel(USBOTG_RST_RSTCTRL
| USBOTG_RST_RSTRH
|
1678 USBOTG_RST_RSTHSIE
| USBOTG_RST_RSTHC
,
1679 imx21
->regs
+ USBOTG_RST_CTRL
);
1681 /* Wait for reset to finish */
1682 timeout
= jiffies
+ HZ
;
1683 while (readl(imx21
->regs
+ USBOTG_RST_CTRL
) != 0) {
1684 if (time_after(jiffies
, timeout
)) {
1685 spin_unlock_irqrestore(&imx21
->lock
, flags
);
1686 dev_err(imx21
->dev
, "timeout waiting for reset\n");
1689 spin_unlock_irq(&imx21
->lock
);
1690 schedule_timeout_uninterruptible(1);
1691 spin_lock_irq(&imx21
->lock
);
1693 spin_unlock_irqrestore(&imx21
->lock
, flags
);
1697 static int imx21_hc_start(struct usb_hcd
*hcd
)
1699 struct imx21
*imx21
= hcd_to_imx21(hcd
);
1700 unsigned long flags
;
1702 u32 hw_mode
= USBOTG_HWMODE_CRECFG_HOST
;
1703 u32 usb_control
= 0;
1705 hw_mode
|= ((imx21
->pdata
->host_xcvr
<< USBOTG_HWMODE_HOSTXCVR_SHIFT
) &
1706 USBOTG_HWMODE_HOSTXCVR_MASK
);
1707 hw_mode
|= ((imx21
->pdata
->otg_xcvr
<< USBOTG_HWMODE_OTGXCVR_SHIFT
) &
1708 USBOTG_HWMODE_OTGXCVR_MASK
);
1710 if (imx21
->pdata
->host1_txenoe
)
1711 usb_control
|= USBCTRL_HOST1_TXEN_OE
;
1713 if (!imx21
->pdata
->host1_xcverless
)
1714 usb_control
|= USBCTRL_HOST1_BYP_TLL
;
1716 if (imx21
->pdata
->otg_ext_xcvr
)
1717 usb_control
|= USBCTRL_OTC_RCV_RXDP
;
1720 spin_lock_irqsave(&imx21
->lock
, flags
);
1722 writel((USBOTG_CLK_CTRL_HST
| USBOTG_CLK_CTRL_MAIN
),
1723 imx21
->regs
+ USBOTG_CLK_CTRL
);
1724 writel(hw_mode
, imx21
->regs
+ USBOTG_HWMODE
);
1725 writel(usb_control
, imx21
->regs
+ USBCTRL
);
1726 writel(USB_MISCCONTROL_SKPRTRY
| USB_MISCCONTROL_ARBMODE
,
1727 imx21
->regs
+ USB_MISCCONTROL
);
1729 /* Clear the ETDs */
1730 for (i
= 0; i
< USB_NUM_ETD
; i
++)
1731 for (j
= 0; j
< 4; j
++)
1732 etd_writel(imx21
, i
, j
, 0);
1734 /* Take the HC out of reset */
1735 writel(USBH_HOST_CTRL_HCUSBSTE_OPERATIONAL
| USBH_HOST_CTRL_CTLBLKSR_1
,
1736 imx21
->regs
+ USBH_HOST_CTRL
);
1739 if (imx21
->pdata
->enable_otg_host
)
1740 writel(USBH_PORTSTAT_PRTPWRST
| USBH_PORTSTAT_PRTENABST
,
1741 imx21
->regs
+ USBH_PORTSTAT(0));
1743 if (imx21
->pdata
->enable_host1
)
1744 writel(USBH_PORTSTAT_PRTPWRST
| USBH_PORTSTAT_PRTENABST
,
1745 imx21
->regs
+ USBH_PORTSTAT(1));
1747 if (imx21
->pdata
->enable_host2
)
1748 writel(USBH_PORTSTAT_PRTPWRST
| USBH_PORTSTAT_PRTENABST
,
1749 imx21
->regs
+ USBH_PORTSTAT(2));
1752 hcd
->state
= HC_STATE_RUNNING
;
1754 /* Enable host controller interrupts */
1755 set_register_bits(imx21
, USBH_SYSIEN
,
1756 USBH_SYSIEN_HERRINT
|
1757 USBH_SYSIEN_DONEINT
| USBH_SYSIEN_SORINT
);
1758 set_register_bits(imx21
, USBOTG_CINT_STEN
, USBOTG_HCINT
);
1760 spin_unlock_irqrestore(&imx21
->lock
, flags
);
1765 static void imx21_hc_stop(struct usb_hcd
*hcd
)
1767 struct imx21
*imx21
= hcd_to_imx21(hcd
);
1768 unsigned long flags
;
1770 spin_lock_irqsave(&imx21
->lock
, flags
);
1772 writel(0, imx21
->regs
+ USBH_SYSIEN
);
1773 clear_register_bits(imx21
, USBOTG_CINT_STEN
, USBOTG_HCINT
);
1774 clear_register_bits(imx21
, USBOTG_CLK_CTRL_HST
| USBOTG_CLK_CTRL_MAIN
,
1776 spin_unlock_irqrestore(&imx21
->lock
, flags
);
1779 /* =========================================== */
1781 /* =========================================== */
1783 static struct hc_driver imx21_hc_driver
= {
1784 .description
= hcd_name
,
1785 .product_desc
= "IMX21 USB Host Controller",
1786 .hcd_priv_size
= sizeof(struct imx21
),
1791 .reset
= imx21_hc_reset
,
1792 .start
= imx21_hc_start
,
1793 .stop
= imx21_hc_stop
,
1796 .urb_enqueue
= imx21_hc_urb_enqueue
,
1797 .urb_dequeue
= imx21_hc_urb_dequeue
,
1798 .endpoint_disable
= imx21_hc_endpoint_disable
,
1800 /* scheduling support */
1801 .get_frame_number
= imx21_hc_get_frame
,
1803 /* Root hub support */
1804 .hub_status_data
= imx21_hc_hub_status_data
,
1805 .hub_control
= imx21_hc_hub_control
,
1809 static struct mx21_usbh_platform_data default_pdata
= {
1810 .host_xcvr
= MX21_USBXCVR_TXDIF_RXDIF
,
1811 .otg_xcvr
= MX21_USBXCVR_TXDIF_RXDIF
,
1814 .enable_otg_host
= 1,
1818 static int imx21_remove(struct platform_device
*pdev
)
1820 struct usb_hcd
*hcd
= platform_get_drvdata(pdev
);
1821 struct imx21
*imx21
= hcd_to_imx21(hcd
);
1822 struct resource
*res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1824 remove_debug_files(imx21
);
1825 usb_remove_hcd(hcd
);
1828 clk_disable_unprepare(imx21
->clk
);
1829 clk_put(imx21
->clk
);
1830 iounmap(imx21
->regs
);
1831 release_mem_region(res
->start
, resource_size(res
));
1839 static int imx21_probe(struct platform_device
*pdev
)
1841 struct usb_hcd
*hcd
;
1842 struct imx21
*imx21
;
1843 struct resource
*res
;
1847 printk(KERN_INFO
"%s\n", imx21_hc_driver
.product_desc
);
1849 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1852 irq
= platform_get_irq(pdev
, 0);
1856 hcd
= usb_create_hcd(&imx21_hc_driver
,
1857 &pdev
->dev
, dev_name(&pdev
->dev
));
1859 dev_err(&pdev
->dev
, "Cannot create hcd (%s)\n",
1860 dev_name(&pdev
->dev
));
1864 imx21
= hcd_to_imx21(hcd
);
1866 imx21
->dev
= &pdev
->dev
;
1867 imx21
->pdata
= dev_get_platdata(&pdev
->dev
);
1869 imx21
->pdata
= &default_pdata
;
1871 spin_lock_init(&imx21
->lock
);
1872 INIT_LIST_HEAD(&imx21
->dmem_list
);
1873 INIT_LIST_HEAD(&imx21
->queue_for_etd
);
1874 INIT_LIST_HEAD(&imx21
->queue_for_dmem
);
1875 create_debug_files(imx21
);
1877 res
= request_mem_region(res
->start
, resource_size(res
), hcd_name
);
1880 goto failed_request_mem
;
1883 imx21
->regs
= ioremap(res
->start
, resource_size(res
));
1884 if (imx21
->regs
== NULL
) {
1885 dev_err(imx21
->dev
, "Cannot map registers\n");
1887 goto failed_ioremap
;
1890 /* Enable clocks source */
1891 imx21
->clk
= clk_get(imx21
->dev
, NULL
);
1892 if (IS_ERR(imx21
->clk
)) {
1893 dev_err(imx21
->dev
, "no clock found\n");
1894 ret
= PTR_ERR(imx21
->clk
);
1895 goto failed_clock_get
;
1898 ret
= clk_set_rate(imx21
->clk
, clk_round_rate(imx21
->clk
, 48000000));
1900 goto failed_clock_set
;
1901 ret
= clk_prepare_enable(imx21
->clk
);
1903 goto failed_clock_enable
;
1905 dev_info(imx21
->dev
, "Hardware HC revision: 0x%02X\n",
1906 (readl(imx21
->regs
+ USBOTG_HWMODE
) >> 16) & 0xFF);
1908 ret
= usb_add_hcd(hcd
, irq
, 0);
1910 dev_err(imx21
->dev
, "usb_add_hcd() returned %d\n", ret
);
1911 goto failed_add_hcd
;
1913 device_wakeup_enable(hcd
->self
.controller
);
1918 clk_disable_unprepare(imx21
->clk
);
1919 failed_clock_enable
:
1921 clk_put(imx21
->clk
);
1923 iounmap(imx21
->regs
);
1925 release_mem_region(res
->start
, resource_size(res
));
1927 remove_debug_files(imx21
);
1932 static struct platform_driver imx21_hcd_driver
= {
1936 .probe
= imx21_probe
,
1937 .remove
= imx21_remove
,
1942 module_platform_driver(imx21_hcd_driver
);
1944 MODULE_DESCRIPTION("i.MX21 USB Host controller");
1945 MODULE_AUTHOR("Martin Fuzzey");
1946 MODULE_LICENSE("GPL");
1947 MODULE_ALIAS("platform:imx21-hcd");