2 * Driver for the Atmel USBA high speed USB device controller
4 * Copyright (C) 2005-2007 Atmel Corporation
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 #include <linux/clk.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/interrupt.h>
15 #include <linux/slab.h>
16 #include <linux/device.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/list.h>
19 #include <linux/platform_device.h>
20 #include <linux/usb/ch9.h>
21 #include <linux/usb/gadget.h>
22 #include <linux/usb/atmel_usba_udc.h>
23 #include <linux/delay.h>
26 #include <mach/board.h>
28 #include "atmel_usba_udc.h"
31 static struct usba_udc the_udc
;
32 static struct usba_ep
*usba_ep
;
34 #ifdef CONFIG_USB_GADGET_DEBUG_FS
35 #include <linux/debugfs.h>
36 #include <linux/uaccess.h>
38 static int queue_dbg_open(struct inode
*inode
, struct file
*file
)
40 struct usba_ep
*ep
= inode
->i_private
;
41 struct usba_request
*req
, *req_copy
;
42 struct list_head
*queue_data
;
44 queue_data
= kmalloc(sizeof(*queue_data
), GFP_KERNEL
);
47 INIT_LIST_HEAD(queue_data
);
49 spin_lock_irq(&ep
->udc
->lock
);
50 list_for_each_entry(req
, &ep
->queue
, queue
) {
51 req_copy
= kmemdup(req
, sizeof(*req_copy
), GFP_ATOMIC
);
54 list_add_tail(&req_copy
->queue
, queue_data
);
56 spin_unlock_irq(&ep
->udc
->lock
);
58 file
->private_data
= queue_data
;
62 spin_unlock_irq(&ep
->udc
->lock
);
63 list_for_each_entry_safe(req
, req_copy
, queue_data
, queue
) {
64 list_del(&req
->queue
);
72 * bbbbbbbb llllllll IZS sssss nnnn FDL\n\0
76 * I/i: interrupt/no interrupt
78 * S/s: short ok/short not ok
81 * F/f: submitted/not submitted to FIFO
82 * D/d: using/not using DMA
83 * L/l: last transaction/not last transaction
85 static ssize_t
queue_dbg_read(struct file
*file
, char __user
*buf
,
86 size_t nbytes
, loff_t
*ppos
)
88 struct list_head
*queue
= file
->private_data
;
89 struct usba_request
*req
, *tmp_req
;
90 size_t len
, remaining
, actual
= 0;
93 if (!access_ok(VERIFY_WRITE
, buf
, nbytes
))
96 mutex_lock(&file
->f_dentry
->d_inode
->i_mutex
);
97 list_for_each_entry_safe(req
, tmp_req
, queue
, queue
) {
98 len
= snprintf(tmpbuf
, sizeof(tmpbuf
),
99 "%8p %08x %c%c%c %5d %c%c%c\n",
100 req
->req
.buf
, req
->req
.length
,
101 req
->req
.no_interrupt
? 'i' : 'I',
102 req
->req
.zero
? 'Z' : 'z',
103 req
->req
.short_not_ok
? 's' : 'S',
105 req
->submitted
? 'F' : 'f',
106 req
->using_dma
? 'D' : 'd',
107 req
->last_transaction
? 'L' : 'l');
108 len
= min(len
, sizeof(tmpbuf
));
112 list_del(&req
->queue
);
115 remaining
= __copy_to_user(buf
, tmpbuf
, len
);
116 actual
+= len
- remaining
;
123 mutex_unlock(&file
->f_dentry
->d_inode
->i_mutex
);
128 static int queue_dbg_release(struct inode
*inode
, struct file
*file
)
130 struct list_head
*queue_data
= file
->private_data
;
131 struct usba_request
*req
, *tmp_req
;
133 list_for_each_entry_safe(req
, tmp_req
, queue_data
, queue
) {
134 list_del(&req
->queue
);
141 static int regs_dbg_open(struct inode
*inode
, struct file
*file
)
143 struct usba_udc
*udc
;
148 mutex_lock(&inode
->i_mutex
);
149 udc
= inode
->i_private
;
150 data
= kmalloc(inode
->i_size
, GFP_KERNEL
);
154 spin_lock_irq(&udc
->lock
);
155 for (i
= 0; i
< inode
->i_size
/ 4; i
++)
156 data
[i
] = __raw_readl(udc
->regs
+ i
* 4);
157 spin_unlock_irq(&udc
->lock
);
159 file
->private_data
= data
;
163 mutex_unlock(&inode
->i_mutex
);
168 static ssize_t
regs_dbg_read(struct file
*file
, char __user
*buf
,
169 size_t nbytes
, loff_t
*ppos
)
171 struct inode
*inode
= file
->f_dentry
->d_inode
;
174 mutex_lock(&inode
->i_mutex
);
175 ret
= simple_read_from_buffer(buf
, nbytes
, ppos
,
177 file
->f_dentry
->d_inode
->i_size
);
178 mutex_unlock(&inode
->i_mutex
);
183 static int regs_dbg_release(struct inode
*inode
, struct file
*file
)
185 kfree(file
->private_data
);
189 const struct file_operations queue_dbg_fops
= {
190 .owner
= THIS_MODULE
,
191 .open
= queue_dbg_open
,
193 .read
= queue_dbg_read
,
194 .release
= queue_dbg_release
,
197 const struct file_operations regs_dbg_fops
= {
198 .owner
= THIS_MODULE
,
199 .open
= regs_dbg_open
,
200 .llseek
= generic_file_llseek
,
201 .read
= regs_dbg_read
,
202 .release
= regs_dbg_release
,
205 static void usba_ep_init_debugfs(struct usba_udc
*udc
,
208 struct dentry
*ep_root
;
210 ep_root
= debugfs_create_dir(ep
->ep
.name
, udc
->debugfs_root
);
213 ep
->debugfs_dir
= ep_root
;
215 ep
->debugfs_queue
= debugfs_create_file("queue", 0400, ep_root
,
216 ep
, &queue_dbg_fops
);
217 if (!ep
->debugfs_queue
)
221 ep
->debugfs_dma_status
222 = debugfs_create_u32("dma_status", 0400, ep_root
,
223 &ep
->last_dma_status
);
224 if (!ep
->debugfs_dma_status
)
227 if (ep_is_control(ep
)) {
229 = debugfs_create_u32("state", 0400, ep_root
,
231 if (!ep
->debugfs_state
)
239 debugfs_remove(ep
->debugfs_dma_status
);
241 debugfs_remove(ep
->debugfs_queue
);
243 debugfs_remove(ep_root
);
245 dev_err(&ep
->udc
->pdev
->dev
,
246 "failed to create debugfs directory for %s\n", ep
->ep
.name
);
249 static void usba_ep_cleanup_debugfs(struct usba_ep
*ep
)
251 debugfs_remove(ep
->debugfs_queue
);
252 debugfs_remove(ep
->debugfs_dma_status
);
253 debugfs_remove(ep
->debugfs_state
);
254 debugfs_remove(ep
->debugfs_dir
);
255 ep
->debugfs_dma_status
= NULL
;
256 ep
->debugfs_dir
= NULL
;
259 static void usba_init_debugfs(struct usba_udc
*udc
)
261 struct dentry
*root
, *regs
;
262 struct resource
*regs_resource
;
264 root
= debugfs_create_dir(udc
->gadget
.name
, NULL
);
265 if (IS_ERR(root
) || !root
)
267 udc
->debugfs_root
= root
;
269 regs
= debugfs_create_file("regs", 0400, root
, udc
, ®s_dbg_fops
);
273 regs_resource
= platform_get_resource(udc
->pdev
, IORESOURCE_MEM
,
275 regs
->d_inode
->i_size
= resource_size(regs_resource
);
276 udc
->debugfs_regs
= regs
;
278 usba_ep_init_debugfs(udc
, to_usba_ep(udc
->gadget
.ep0
));
283 debugfs_remove(root
);
285 udc
->debugfs_root
= NULL
;
286 dev_err(&udc
->pdev
->dev
, "debugfs is not available\n");
289 static void usba_cleanup_debugfs(struct usba_udc
*udc
)
291 usba_ep_cleanup_debugfs(to_usba_ep(udc
->gadget
.ep0
));
292 debugfs_remove(udc
->debugfs_regs
);
293 debugfs_remove(udc
->debugfs_root
);
294 udc
->debugfs_regs
= NULL
;
295 udc
->debugfs_root
= NULL
;
298 static inline void usba_ep_init_debugfs(struct usba_udc
*udc
,
304 static inline void usba_ep_cleanup_debugfs(struct usba_ep
*ep
)
309 static inline void usba_init_debugfs(struct usba_udc
*udc
)
314 static inline void usba_cleanup_debugfs(struct usba_udc
*udc
)
320 static int vbus_is_present(struct usba_udc
*udc
)
322 if (gpio_is_valid(udc
->vbus_pin
))
323 return gpio_get_value(udc
->vbus_pin
) ^ udc
->vbus_pin_inverted
;
325 /* No Vbus detection: Assume always present */
329 #if defined(CONFIG_ARCH_AT91SAM9RL)
331 #include <mach/at91_pmc.h>
333 static void toggle_bias(int is_on
)
335 unsigned int uckr
= at91_sys_read(AT91_CKGR_UCKR
);
338 at91_sys_write(AT91_CKGR_UCKR
, uckr
| AT91_PMC_BIASEN
);
340 at91_sys_write(AT91_CKGR_UCKR
, uckr
& ~(AT91_PMC_BIASEN
));
345 static void toggle_bias(int is_on
)
349 #endif /* CONFIG_ARCH_AT91SAM9RL */
351 static void next_fifo_transaction(struct usba_ep
*ep
, struct usba_request
*req
)
353 unsigned int transaction_len
;
355 transaction_len
= req
->req
.length
- req
->req
.actual
;
356 req
->last_transaction
= 1;
357 if (transaction_len
> ep
->ep
.maxpacket
) {
358 transaction_len
= ep
->ep
.maxpacket
;
359 req
->last_transaction
= 0;
360 } else if (transaction_len
== ep
->ep
.maxpacket
&& req
->req
.zero
)
361 req
->last_transaction
= 0;
363 DBG(DBG_QUEUE
, "%s: submit_transaction, req %p (length %d)%s\n",
364 ep
->ep
.name
, req
, transaction_len
,
365 req
->last_transaction
? ", done" : "");
367 memcpy_toio(ep
->fifo
, req
->req
.buf
+ req
->req
.actual
, transaction_len
);
368 usba_ep_writel(ep
, SET_STA
, USBA_TX_PK_RDY
);
369 req
->req
.actual
+= transaction_len
;
372 static void submit_request(struct usba_ep
*ep
, struct usba_request
*req
)
374 DBG(DBG_QUEUE
, "%s: submit_request: req %p (length %d)\n",
375 ep
->ep
.name
, req
, req
->req
.length
);
380 if (req
->using_dma
) {
381 if (req
->req
.length
== 0) {
382 usba_ep_writel(ep
, CTL_ENB
, USBA_TX_PK_RDY
);
387 usba_ep_writel(ep
, CTL_ENB
, USBA_SHORT_PACKET
);
389 usba_ep_writel(ep
, CTL_DIS
, USBA_SHORT_PACKET
);
391 usba_dma_writel(ep
, ADDRESS
, req
->req
.dma
);
392 usba_dma_writel(ep
, CONTROL
, req
->ctrl
);
394 next_fifo_transaction(ep
, req
);
395 if (req
->last_transaction
) {
396 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_PK_RDY
);
397 usba_ep_writel(ep
, CTL_ENB
, USBA_TX_COMPLETE
);
399 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_COMPLETE
);
400 usba_ep_writel(ep
, CTL_ENB
, USBA_TX_PK_RDY
);
405 static void submit_next_request(struct usba_ep
*ep
)
407 struct usba_request
*req
;
409 if (list_empty(&ep
->queue
)) {
410 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_PK_RDY
| USBA_RX_BK_RDY
);
414 req
= list_entry(ep
->queue
.next
, struct usba_request
, queue
);
416 submit_request(ep
, req
);
419 static void send_status(struct usba_udc
*udc
, struct usba_ep
*ep
)
421 ep
->state
= STATUS_STAGE_IN
;
422 usba_ep_writel(ep
, SET_STA
, USBA_TX_PK_RDY
);
423 usba_ep_writel(ep
, CTL_ENB
, USBA_TX_COMPLETE
);
426 static void receive_data(struct usba_ep
*ep
)
428 struct usba_udc
*udc
= ep
->udc
;
429 struct usba_request
*req
;
430 unsigned long status
;
431 unsigned int bytecount
, nr_busy
;
434 status
= usba_ep_readl(ep
, STA
);
435 nr_busy
= USBA_BFEXT(BUSY_BANKS
, status
);
437 DBG(DBG_QUEUE
, "receive data: nr_busy=%u\n", nr_busy
);
439 while (nr_busy
> 0) {
440 if (list_empty(&ep
->queue
)) {
441 usba_ep_writel(ep
, CTL_DIS
, USBA_RX_BK_RDY
);
444 req
= list_entry(ep
->queue
.next
,
445 struct usba_request
, queue
);
447 bytecount
= USBA_BFEXT(BYTE_COUNT
, status
);
449 if (status
& (1 << 31))
451 if (req
->req
.actual
+ bytecount
>= req
->req
.length
) {
453 bytecount
= req
->req
.length
- req
->req
.actual
;
456 memcpy_fromio(req
->req
.buf
+ req
->req
.actual
,
457 ep
->fifo
, bytecount
);
458 req
->req
.actual
+= bytecount
;
460 usba_ep_writel(ep
, CLR_STA
, USBA_RX_BK_RDY
);
463 DBG(DBG_QUEUE
, "%s: request done\n", ep
->ep
.name
);
465 list_del_init(&req
->queue
);
466 usba_ep_writel(ep
, CTL_DIS
, USBA_RX_BK_RDY
);
467 spin_unlock(&udc
->lock
);
468 req
->req
.complete(&ep
->ep
, &req
->req
);
469 spin_lock(&udc
->lock
);
472 status
= usba_ep_readl(ep
, STA
);
473 nr_busy
= USBA_BFEXT(BUSY_BANKS
, status
);
475 if (is_complete
&& ep_is_control(ep
)) {
476 send_status(udc
, ep
);
483 request_complete(struct usba_ep
*ep
, struct usba_request
*req
, int status
)
485 struct usba_udc
*udc
= ep
->udc
;
487 WARN_ON(!list_empty(&req
->queue
));
489 if (req
->req
.status
== -EINPROGRESS
)
490 req
->req
.status
= status
;
494 &udc
->pdev
->dev
, req
->req
.dma
, req
->req
.length
,
495 ep
->is_in
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
496 req
->req
.dma
= DMA_ADDR_INVALID
;
500 DBG(DBG_GADGET
| DBG_REQ
,
501 "%s: req %p complete: status %d, actual %u\n",
502 ep
->ep
.name
, req
, req
->req
.status
, req
->req
.actual
);
504 spin_unlock(&udc
->lock
);
505 req
->req
.complete(&ep
->ep
, &req
->req
);
506 spin_lock(&udc
->lock
);
510 request_complete_list(struct usba_ep
*ep
, struct list_head
*list
, int status
)
512 struct usba_request
*req
, *tmp_req
;
514 list_for_each_entry_safe(req
, tmp_req
, list
, queue
) {
515 list_del_init(&req
->queue
);
516 request_complete(ep
, req
, status
);
521 usba_ep_enable(struct usb_ep
*_ep
, const struct usb_endpoint_descriptor
*desc
)
523 struct usba_ep
*ep
= to_usba_ep(_ep
);
524 struct usba_udc
*udc
= ep
->udc
;
525 unsigned long flags
, ept_cfg
, maxpacket
;
526 unsigned int nr_trans
;
528 DBG(DBG_GADGET
, "%s: ep_enable: desc=%p\n", ep
->ep
.name
, desc
);
530 maxpacket
= usb_endpoint_maxp(desc
) & 0x7ff;
532 if (((desc
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
) != ep
->index
)
534 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
536 || maxpacket
> ep
->fifo_size
) {
537 DBG(DBG_ERR
, "ep_enable: Invalid argument");
545 ept_cfg
= USBA_BF(EPT_SIZE
, USBA_EPT_SIZE_8
);
547 /* LSB is bit 1, not 0 */
548 ept_cfg
= USBA_BF(EPT_SIZE
, fls(maxpacket
- 1) - 3);
550 DBG(DBG_HW
, "%s: EPT_SIZE = %lu (maxpacket = %lu)\n",
551 ep
->ep
.name
, ept_cfg
, maxpacket
);
553 if (usb_endpoint_dir_in(desc
)) {
555 ept_cfg
|= USBA_EPT_DIR_IN
;
558 switch (usb_endpoint_type(desc
)) {
559 case USB_ENDPOINT_XFER_CONTROL
:
560 ept_cfg
|= USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_CONTROL
);
561 ept_cfg
|= USBA_BF(BK_NUMBER
, USBA_BK_NUMBER_ONE
);
563 case USB_ENDPOINT_XFER_ISOC
:
565 DBG(DBG_ERR
, "ep_enable: %s is not isoc capable\n",
571 * Bits 11:12 specify number of _additional_
572 * transactions per microframe.
574 nr_trans
= ((usb_endpoint_maxp(desc
) >> 11) & 3) + 1;
579 ept_cfg
|= USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_ISO
);
582 * Do triple-buffering on high-bandwidth iso endpoints.
584 if (nr_trans
> 1 && ep
->nr_banks
== 3)
585 ept_cfg
|= USBA_BF(BK_NUMBER
, USBA_BK_NUMBER_TRIPLE
);
587 ept_cfg
|= USBA_BF(BK_NUMBER
, USBA_BK_NUMBER_DOUBLE
);
588 ept_cfg
|= USBA_BF(NB_TRANS
, nr_trans
);
590 case USB_ENDPOINT_XFER_BULK
:
591 ept_cfg
|= USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_BULK
);
592 ept_cfg
|= USBA_BF(BK_NUMBER
, USBA_BK_NUMBER_DOUBLE
);
594 case USB_ENDPOINT_XFER_INT
:
595 ept_cfg
|= USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_INT
);
596 ept_cfg
|= USBA_BF(BK_NUMBER
, USBA_BK_NUMBER_DOUBLE
);
600 spin_lock_irqsave(&ep
->udc
->lock
, flags
);
603 spin_unlock_irqrestore(&ep
->udc
->lock
, flags
);
604 DBG(DBG_ERR
, "ep%d already enabled\n", ep
->index
);
609 ep
->ep
.maxpacket
= maxpacket
;
611 usba_ep_writel(ep
, CFG
, ept_cfg
);
612 usba_ep_writel(ep
, CTL_ENB
, USBA_EPT_ENABLE
);
617 usba_writel(udc
, INT_ENB
,
618 (usba_readl(udc
, INT_ENB
)
619 | USBA_BF(EPT_INT
, 1 << ep
->index
)
620 | USBA_BF(DMA_INT
, 1 << ep
->index
)));
621 ctrl
= USBA_AUTO_VALID
| USBA_INTDIS_DMA
;
622 usba_ep_writel(ep
, CTL_ENB
, ctrl
);
624 usba_writel(udc
, INT_ENB
,
625 (usba_readl(udc
, INT_ENB
)
626 | USBA_BF(EPT_INT
, 1 << ep
->index
)));
629 spin_unlock_irqrestore(&udc
->lock
, flags
);
631 DBG(DBG_HW
, "EPT_CFG%d after init: %#08lx\n", ep
->index
,
632 (unsigned long)usba_ep_readl(ep
, CFG
));
633 DBG(DBG_HW
, "INT_ENB after init: %#08lx\n",
634 (unsigned long)usba_readl(udc
, INT_ENB
));
639 static int usba_ep_disable(struct usb_ep
*_ep
)
641 struct usba_ep
*ep
= to_usba_ep(_ep
);
642 struct usba_udc
*udc
= ep
->udc
;
646 DBG(DBG_GADGET
, "ep_disable: %s\n", ep
->ep
.name
);
648 spin_lock_irqsave(&udc
->lock
, flags
);
651 spin_unlock_irqrestore(&udc
->lock
, flags
);
652 /* REVISIT because this driver disables endpoints in
653 * reset_all_endpoints() before calling disconnect(),
654 * most gadget drivers would trigger this non-error ...
656 if (udc
->gadget
.speed
!= USB_SPEED_UNKNOWN
)
657 DBG(DBG_ERR
, "ep_disable: %s not enabled\n",
663 list_splice_init(&ep
->queue
, &req_list
);
665 usba_dma_writel(ep
, CONTROL
, 0);
666 usba_dma_writel(ep
, ADDRESS
, 0);
667 usba_dma_readl(ep
, STATUS
);
669 usba_ep_writel(ep
, CTL_DIS
, USBA_EPT_ENABLE
);
670 usba_writel(udc
, INT_ENB
,
671 usba_readl(udc
, INT_ENB
)
672 & ~USBA_BF(EPT_INT
, 1 << ep
->index
));
674 request_complete_list(ep
, &req_list
, -ESHUTDOWN
);
676 spin_unlock_irqrestore(&udc
->lock
, flags
);
681 static struct usb_request
*
682 usba_ep_alloc_request(struct usb_ep
*_ep
, gfp_t gfp_flags
)
684 struct usba_request
*req
;
686 DBG(DBG_GADGET
, "ep_alloc_request: %p, 0x%x\n", _ep
, gfp_flags
);
688 req
= kzalloc(sizeof(*req
), gfp_flags
);
692 INIT_LIST_HEAD(&req
->queue
);
693 req
->req
.dma
= DMA_ADDR_INVALID
;
699 usba_ep_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
701 struct usba_request
*req
= to_usba_req(_req
);
703 DBG(DBG_GADGET
, "ep_free_request: %p, %p\n", _ep
, _req
);
708 static int queue_dma(struct usba_udc
*udc
, struct usba_ep
*ep
,
709 struct usba_request
*req
, gfp_t gfp_flags
)
714 DBG(DBG_DMA
, "%s: req l/%u d/%08x %c%c%c\n",
715 ep
->ep
.name
, req
->req
.length
, req
->req
.dma
,
716 req
->req
.zero
? 'Z' : 'z',
717 req
->req
.short_not_ok
? 'S' : 's',
718 req
->req
.no_interrupt
? 'I' : 'i');
720 if (req
->req
.length
> 0x10000) {
721 /* Lengths from 0 to 65536 (inclusive) are supported */
722 DBG(DBG_ERR
, "invalid request length %u\n", req
->req
.length
);
728 if (req
->req
.dma
== DMA_ADDR_INVALID
) {
729 req
->req
.dma
= dma_map_single(
730 &udc
->pdev
->dev
, req
->req
.buf
, req
->req
.length
,
731 ep
->is_in
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
734 dma_sync_single_for_device(
735 &udc
->pdev
->dev
, req
->req
.dma
, req
->req
.length
,
736 ep
->is_in
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
740 req
->ctrl
= USBA_BF(DMA_BUF_LEN
, req
->req
.length
)
741 | USBA_DMA_CH_EN
| USBA_DMA_END_BUF_IE
742 | USBA_DMA_END_TR_EN
| USBA_DMA_END_TR_IE
;
745 req
->ctrl
|= USBA_DMA_END_BUF_EN
;
748 * Add this request to the queue and submit for DMA if
749 * possible. Check if we're still alive first -- we may have
750 * received a reset since last time we checked.
753 spin_lock_irqsave(&udc
->lock
, flags
);
755 if (list_empty(&ep
->queue
))
756 submit_request(ep
, req
);
758 list_add_tail(&req
->queue
, &ep
->queue
);
761 spin_unlock_irqrestore(&udc
->lock
, flags
);
767 usba_ep_queue(struct usb_ep
*_ep
, struct usb_request
*_req
, gfp_t gfp_flags
)
769 struct usba_request
*req
= to_usba_req(_req
);
770 struct usba_ep
*ep
= to_usba_ep(_ep
);
771 struct usba_udc
*udc
= ep
->udc
;
775 DBG(DBG_GADGET
| DBG_QUEUE
| DBG_REQ
, "%s: queue req %p, len %u\n",
776 ep
->ep
.name
, req
, _req
->length
);
778 if (!udc
->driver
|| udc
->gadget
.speed
== USB_SPEED_UNKNOWN
|| !ep
->desc
)
783 req
->last_transaction
= 0;
785 _req
->status
= -EINPROGRESS
;
789 return queue_dma(udc
, ep
, req
, gfp_flags
);
791 /* May have received a reset since last time we checked */
793 spin_lock_irqsave(&udc
->lock
, flags
);
795 list_add_tail(&req
->queue
, &ep
->queue
);
797 if ((!ep_is_control(ep
) && ep
->is_in
) ||
799 && (ep
->state
== DATA_STAGE_IN
800 || ep
->state
== STATUS_STAGE_IN
)))
801 usba_ep_writel(ep
, CTL_ENB
, USBA_TX_PK_RDY
);
803 usba_ep_writel(ep
, CTL_ENB
, USBA_RX_BK_RDY
);
806 spin_unlock_irqrestore(&udc
->lock
, flags
);
812 usba_update_req(struct usba_ep
*ep
, struct usba_request
*req
, u32 status
)
814 req
->req
.actual
= req
->req
.length
- USBA_BFEXT(DMA_BUF_LEN
, status
);
817 static int stop_dma(struct usba_ep
*ep
, u32
*pstatus
)
819 unsigned int timeout
;
823 * Stop the DMA controller. When writing both CH_EN
824 * and LINK to 0, the other bits are not affected.
826 usba_dma_writel(ep
, CONTROL
, 0);
828 /* Wait for the FIFO to empty */
829 for (timeout
= 40; timeout
; --timeout
) {
830 status
= usba_dma_readl(ep
, STATUS
);
831 if (!(status
& USBA_DMA_CH_EN
))
840 dev_err(&ep
->udc
->pdev
->dev
,
841 "%s: timed out waiting for DMA FIFO to empty\n",
849 static int usba_ep_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
851 struct usba_ep
*ep
= to_usba_ep(_ep
);
852 struct usba_udc
*udc
= ep
->udc
;
853 struct usba_request
*req
= to_usba_req(_req
);
857 DBG(DBG_GADGET
| DBG_QUEUE
, "ep_dequeue: %s, req %p\n",
860 spin_lock_irqsave(&udc
->lock
, flags
);
862 if (req
->using_dma
) {
864 * If this request is currently being transferred,
865 * stop the DMA controller and reset the FIFO.
867 if (ep
->queue
.next
== &req
->queue
) {
868 status
= usba_dma_readl(ep
, STATUS
);
869 if (status
& USBA_DMA_CH_EN
)
870 stop_dma(ep
, &status
);
872 #ifdef CONFIG_USB_GADGET_DEBUG_FS
873 ep
->last_dma_status
= status
;
876 usba_writel(udc
, EPT_RST
, 1 << ep
->index
);
878 usba_update_req(ep
, req
, status
);
883 * Errors should stop the queue from advancing until the
884 * completion function returns.
886 list_del_init(&req
->queue
);
888 request_complete(ep
, req
, -ECONNRESET
);
890 /* Process the next request if any */
891 submit_next_request(ep
);
892 spin_unlock_irqrestore(&udc
->lock
, flags
);
897 static int usba_ep_set_halt(struct usb_ep
*_ep
, int value
)
899 struct usba_ep
*ep
= to_usba_ep(_ep
);
900 struct usba_udc
*udc
= ep
->udc
;
904 DBG(DBG_GADGET
, "endpoint %s: %s HALT\n", ep
->ep
.name
,
905 value
? "set" : "clear");
908 DBG(DBG_ERR
, "Attempted to halt uninitialized ep %s\n",
913 DBG(DBG_ERR
, "Attempted to halt isochronous ep %s\n",
918 spin_lock_irqsave(&udc
->lock
, flags
);
921 * We can't halt IN endpoints while there are still data to be
924 if (!list_empty(&ep
->queue
)
925 || ((value
&& ep
->is_in
&& (usba_ep_readl(ep
, STA
)
926 & USBA_BF(BUSY_BANKS
, -1L))))) {
930 usba_ep_writel(ep
, SET_STA
, USBA_FORCE_STALL
);
932 usba_ep_writel(ep
, CLR_STA
,
933 USBA_FORCE_STALL
| USBA_TOGGLE_CLR
);
934 usba_ep_readl(ep
, STA
);
937 spin_unlock_irqrestore(&udc
->lock
, flags
);
942 static int usba_ep_fifo_status(struct usb_ep
*_ep
)
944 struct usba_ep
*ep
= to_usba_ep(_ep
);
946 return USBA_BFEXT(BYTE_COUNT
, usba_ep_readl(ep
, STA
));
949 static void usba_ep_fifo_flush(struct usb_ep
*_ep
)
951 struct usba_ep
*ep
= to_usba_ep(_ep
);
952 struct usba_udc
*udc
= ep
->udc
;
954 usba_writel(udc
, EPT_RST
, 1 << ep
->index
);
957 static const struct usb_ep_ops usba_ep_ops
= {
958 .enable
= usba_ep_enable
,
959 .disable
= usba_ep_disable
,
960 .alloc_request
= usba_ep_alloc_request
,
961 .free_request
= usba_ep_free_request
,
962 .queue
= usba_ep_queue
,
963 .dequeue
= usba_ep_dequeue
,
964 .set_halt
= usba_ep_set_halt
,
965 .fifo_status
= usba_ep_fifo_status
,
966 .fifo_flush
= usba_ep_fifo_flush
,
969 static int usba_udc_get_frame(struct usb_gadget
*gadget
)
971 struct usba_udc
*udc
= to_usba_udc(gadget
);
973 return USBA_BFEXT(FRAME_NUMBER
, usba_readl(udc
, FNUM
));
976 static int usba_udc_wakeup(struct usb_gadget
*gadget
)
978 struct usba_udc
*udc
= to_usba_udc(gadget
);
983 spin_lock_irqsave(&udc
->lock
, flags
);
984 if (udc
->devstatus
& (1 << USB_DEVICE_REMOTE_WAKEUP
)) {
985 ctrl
= usba_readl(udc
, CTRL
);
986 usba_writel(udc
, CTRL
, ctrl
| USBA_REMOTE_WAKE_UP
);
989 spin_unlock_irqrestore(&udc
->lock
, flags
);
995 usba_udc_set_selfpowered(struct usb_gadget
*gadget
, int is_selfpowered
)
997 struct usba_udc
*udc
= to_usba_udc(gadget
);
1000 spin_lock_irqsave(&udc
->lock
, flags
);
1002 udc
->devstatus
|= 1 << USB_DEVICE_SELF_POWERED
;
1004 udc
->devstatus
&= ~(1 << USB_DEVICE_SELF_POWERED
);
1005 spin_unlock_irqrestore(&udc
->lock
, flags
);
1010 static int atmel_usba_start(struct usb_gadget_driver
*driver
,
1011 int (*bind
)(struct usb_gadget
*));
1012 static int atmel_usba_stop(struct usb_gadget_driver
*driver
);
1014 static const struct usb_gadget_ops usba_udc_ops
= {
1015 .get_frame
= usba_udc_get_frame
,
1016 .wakeup
= usba_udc_wakeup
,
1017 .set_selfpowered
= usba_udc_set_selfpowered
,
1018 .start
= atmel_usba_start
,
1019 .stop
= atmel_usba_stop
,
1022 static struct usb_endpoint_descriptor usba_ep0_desc
= {
1023 .bLength
= USB_DT_ENDPOINT_SIZE
,
1024 .bDescriptorType
= USB_DT_ENDPOINT
,
1025 .bEndpointAddress
= 0,
1026 .bmAttributes
= USB_ENDPOINT_XFER_CONTROL
,
1027 .wMaxPacketSize
= cpu_to_le16(64),
1028 /* FIXME: I have no idea what to put here */
1032 static void nop_release(struct device
*dev
)
1037 static struct usba_udc the_udc
= {
1039 .ops
= &usba_udc_ops
,
1040 .ep_list
= LIST_HEAD_INIT(the_udc
.gadget
.ep_list
),
1041 .max_speed
= USB_SPEED_HIGH
,
1042 .name
= "atmel_usba_udc",
1044 .init_name
= "gadget",
1045 .release
= nop_release
,
1051 * Called with interrupts disabled and udc->lock held.
1053 static void reset_all_endpoints(struct usba_udc
*udc
)
1056 struct usba_request
*req
, *tmp_req
;
1058 usba_writel(udc
, EPT_RST
, ~0UL);
1060 ep
= to_usba_ep(udc
->gadget
.ep0
);
1061 list_for_each_entry_safe(req
, tmp_req
, &ep
->queue
, queue
) {
1062 list_del_init(&req
->queue
);
1063 request_complete(ep
, req
, -ECONNRESET
);
1066 /* NOTE: normally, the next call to the gadget driver is in
1067 * charge of disabling endpoints... usually disconnect().
1068 * The exception would be entering a high speed test mode.
1070 * FIXME remove this code ... and retest thoroughly.
1072 list_for_each_entry(ep
, &udc
->gadget
.ep_list
, ep
.ep_list
) {
1074 spin_unlock(&udc
->lock
);
1075 usba_ep_disable(&ep
->ep
);
1076 spin_lock(&udc
->lock
);
1081 static struct usba_ep
*get_ep_by_addr(struct usba_udc
*udc
, u16 wIndex
)
1085 if ((wIndex
& USB_ENDPOINT_NUMBER_MASK
) == 0)
1086 return to_usba_ep(udc
->gadget
.ep0
);
1088 list_for_each_entry (ep
, &udc
->gadget
.ep_list
, ep
.ep_list
) {
1089 u8 bEndpointAddress
;
1093 bEndpointAddress
= ep
->desc
->bEndpointAddress
;
1094 if ((wIndex
^ bEndpointAddress
) & USB_DIR_IN
)
1096 if ((bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
)
1097 == (wIndex
& USB_ENDPOINT_NUMBER_MASK
))
1104 /* Called with interrupts disabled and udc->lock held */
1105 static inline void set_protocol_stall(struct usba_udc
*udc
, struct usba_ep
*ep
)
1107 usba_ep_writel(ep
, SET_STA
, USBA_FORCE_STALL
);
1108 ep
->state
= WAIT_FOR_SETUP
;
1111 static inline int is_stalled(struct usba_udc
*udc
, struct usba_ep
*ep
)
1113 if (usba_ep_readl(ep
, STA
) & USBA_FORCE_STALL
)
1118 static inline void set_address(struct usba_udc
*udc
, unsigned int addr
)
1122 DBG(DBG_BUS
, "setting address %u...\n", addr
);
1123 regval
= usba_readl(udc
, CTRL
);
1124 regval
= USBA_BFINS(DEV_ADDR
, addr
, regval
);
1125 usba_writel(udc
, CTRL
, regval
);
1128 static int do_test_mode(struct usba_udc
*udc
)
1130 static const char test_packet_buffer
[] = {
1132 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1134 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
1136 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
1137 /* JJJJJJJKKKKKKK * 8 */
1138 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1139 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1141 0x7F, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD,
1142 /* {JKKKKKKK * 10}, JK */
1143 0xFC, 0x7E, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD, 0x7E
1146 struct device
*dev
= &udc
->pdev
->dev
;
1149 test_mode
= udc
->test_mode
;
1151 /* Start from a clean slate */
1152 reset_all_endpoints(udc
);
1154 switch (test_mode
) {
1157 usba_writel(udc
, TST
, USBA_TST_J_MODE
);
1158 dev_info(dev
, "Entering Test_J mode...\n");
1162 usba_writel(udc
, TST
, USBA_TST_K_MODE
);
1163 dev_info(dev
, "Entering Test_K mode...\n");
1167 * Test_SE0_NAK: Force high-speed mode and set up ep0
1168 * for Bulk IN transfers
1171 usba_writel(udc
, TST
,
1172 USBA_BF(SPEED_CFG
, USBA_SPEED_CFG_FORCE_HIGH
));
1173 usba_ep_writel(ep
, CFG
,
1174 USBA_BF(EPT_SIZE
, USBA_EPT_SIZE_64
)
1176 | USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_BULK
)
1177 | USBA_BF(BK_NUMBER
, 1));
1178 if (!(usba_ep_readl(ep
, CFG
) & USBA_EPT_MAPPED
)) {
1179 set_protocol_stall(udc
, ep
);
1180 dev_err(dev
, "Test_SE0_NAK: ep0 not mapped\n");
1182 usba_ep_writel(ep
, CTL_ENB
, USBA_EPT_ENABLE
);
1183 dev_info(dev
, "Entering Test_SE0_NAK mode...\n");
1189 usba_ep_writel(ep
, CFG
,
1190 USBA_BF(EPT_SIZE
, USBA_EPT_SIZE_64
)
1192 | USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_BULK
)
1193 | USBA_BF(BK_NUMBER
, 1));
1194 if (!(usba_ep_readl(ep
, CFG
) & USBA_EPT_MAPPED
)) {
1195 set_protocol_stall(udc
, ep
);
1196 dev_err(dev
, "Test_Packet: ep0 not mapped\n");
1198 usba_ep_writel(ep
, CTL_ENB
, USBA_EPT_ENABLE
);
1199 usba_writel(udc
, TST
, USBA_TST_PKT_MODE
);
1200 memcpy_toio(ep
->fifo
, test_packet_buffer
,
1201 sizeof(test_packet_buffer
));
1202 usba_ep_writel(ep
, SET_STA
, USBA_TX_PK_RDY
);
1203 dev_info(dev
, "Entering Test_Packet mode...\n");
1207 dev_err(dev
, "Invalid test mode: 0x%04x\n", test_mode
);
1214 /* Avoid overly long expressions */
1215 static inline bool feature_is_dev_remote_wakeup(struct usb_ctrlrequest
*crq
)
1217 if (crq
->wValue
== cpu_to_le16(USB_DEVICE_REMOTE_WAKEUP
))
1222 static inline bool feature_is_dev_test_mode(struct usb_ctrlrequest
*crq
)
1224 if (crq
->wValue
== cpu_to_le16(USB_DEVICE_TEST_MODE
))
1229 static inline bool feature_is_ep_halt(struct usb_ctrlrequest
*crq
)
1231 if (crq
->wValue
== cpu_to_le16(USB_ENDPOINT_HALT
))
1236 static int handle_ep0_setup(struct usba_udc
*udc
, struct usba_ep
*ep
,
1237 struct usb_ctrlrequest
*crq
)
1241 switch (crq
->bRequest
) {
1242 case USB_REQ_GET_STATUS
: {
1245 if (crq
->bRequestType
== (USB_DIR_IN
| USB_RECIP_DEVICE
)) {
1246 status
= cpu_to_le16(udc
->devstatus
);
1247 } else if (crq
->bRequestType
1248 == (USB_DIR_IN
| USB_RECIP_INTERFACE
)) {
1249 status
= cpu_to_le16(0);
1250 } else if (crq
->bRequestType
1251 == (USB_DIR_IN
| USB_RECIP_ENDPOINT
)) {
1252 struct usba_ep
*target
;
1254 target
= get_ep_by_addr(udc
, le16_to_cpu(crq
->wIndex
));
1259 if (is_stalled(udc
, target
))
1260 status
|= cpu_to_le16(1);
1264 /* Write directly to the FIFO. No queueing is done. */
1265 if (crq
->wLength
!= cpu_to_le16(sizeof(status
)))
1267 ep
->state
= DATA_STAGE_IN
;
1268 __raw_writew(status
, ep
->fifo
);
1269 usba_ep_writel(ep
, SET_STA
, USBA_TX_PK_RDY
);
1273 case USB_REQ_CLEAR_FEATURE
: {
1274 if (crq
->bRequestType
== USB_RECIP_DEVICE
) {
1275 if (feature_is_dev_remote_wakeup(crq
))
1277 &= ~(1 << USB_DEVICE_REMOTE_WAKEUP
);
1279 /* Can't CLEAR_FEATURE TEST_MODE */
1281 } else if (crq
->bRequestType
== USB_RECIP_ENDPOINT
) {
1282 struct usba_ep
*target
;
1284 if (crq
->wLength
!= cpu_to_le16(0)
1285 || !feature_is_ep_halt(crq
))
1287 target
= get_ep_by_addr(udc
, le16_to_cpu(crq
->wIndex
));
1291 usba_ep_writel(target
, CLR_STA
, USBA_FORCE_STALL
);
1292 if (target
->index
!= 0)
1293 usba_ep_writel(target
, CLR_STA
,
1299 send_status(udc
, ep
);
1303 case USB_REQ_SET_FEATURE
: {
1304 if (crq
->bRequestType
== USB_RECIP_DEVICE
) {
1305 if (feature_is_dev_test_mode(crq
)) {
1306 send_status(udc
, ep
);
1307 ep
->state
= STATUS_STAGE_TEST
;
1308 udc
->test_mode
= le16_to_cpu(crq
->wIndex
);
1310 } else if (feature_is_dev_remote_wakeup(crq
)) {
1311 udc
->devstatus
|= 1 << USB_DEVICE_REMOTE_WAKEUP
;
1315 } else if (crq
->bRequestType
== USB_RECIP_ENDPOINT
) {
1316 struct usba_ep
*target
;
1318 if (crq
->wLength
!= cpu_to_le16(0)
1319 || !feature_is_ep_halt(crq
))
1322 target
= get_ep_by_addr(udc
, le16_to_cpu(crq
->wIndex
));
1326 usba_ep_writel(target
, SET_STA
, USBA_FORCE_STALL
);
1330 send_status(udc
, ep
);
1334 case USB_REQ_SET_ADDRESS
:
1335 if (crq
->bRequestType
!= (USB_DIR_OUT
| USB_RECIP_DEVICE
))
1338 set_address(udc
, le16_to_cpu(crq
->wValue
));
1339 send_status(udc
, ep
);
1340 ep
->state
= STATUS_STAGE_ADDR
;
1345 spin_unlock(&udc
->lock
);
1346 retval
= udc
->driver
->setup(&udc
->gadget
, crq
);
1347 spin_lock(&udc
->lock
);
1353 pr_err("udc: %s: Invalid setup request: %02x.%02x v%04x i%04x l%d, "
1354 "halting endpoint...\n",
1355 ep
->ep
.name
, crq
->bRequestType
, crq
->bRequest
,
1356 le16_to_cpu(crq
->wValue
), le16_to_cpu(crq
->wIndex
),
1357 le16_to_cpu(crq
->wLength
));
1358 set_protocol_stall(udc
, ep
);
1362 static void usba_control_irq(struct usba_udc
*udc
, struct usba_ep
*ep
)
1364 struct usba_request
*req
;
1369 epstatus
= usba_ep_readl(ep
, STA
);
1370 epctrl
= usba_ep_readl(ep
, CTL
);
1372 DBG(DBG_INT
, "%s [%d]: s/%08x c/%08x\n",
1373 ep
->ep
.name
, ep
->state
, epstatus
, epctrl
);
1376 if (!list_empty(&ep
->queue
))
1377 req
= list_entry(ep
->queue
.next
,
1378 struct usba_request
, queue
);
1380 if ((epctrl
& USBA_TX_PK_RDY
) && !(epstatus
& USBA_TX_PK_RDY
)) {
1382 next_fifo_transaction(ep
, req
);
1384 submit_request(ep
, req
);
1386 if (req
->last_transaction
) {
1387 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_PK_RDY
);
1388 usba_ep_writel(ep
, CTL_ENB
, USBA_TX_COMPLETE
);
1392 if ((epstatus
& epctrl
) & USBA_TX_COMPLETE
) {
1393 usba_ep_writel(ep
, CLR_STA
, USBA_TX_COMPLETE
);
1395 switch (ep
->state
) {
1397 usba_ep_writel(ep
, CTL_ENB
, USBA_RX_BK_RDY
);
1398 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_COMPLETE
);
1399 ep
->state
= STATUS_STAGE_OUT
;
1401 case STATUS_STAGE_ADDR
:
1402 /* Activate our new address */
1403 usba_writel(udc
, CTRL
, (usba_readl(udc
, CTRL
)
1405 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_COMPLETE
);
1406 ep
->state
= WAIT_FOR_SETUP
;
1408 case STATUS_STAGE_IN
:
1410 list_del_init(&req
->queue
);
1411 request_complete(ep
, req
, 0);
1412 submit_next_request(ep
);
1414 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_COMPLETE
);
1415 ep
->state
= WAIT_FOR_SETUP
;
1417 case STATUS_STAGE_TEST
:
1418 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_COMPLETE
);
1419 ep
->state
= WAIT_FOR_SETUP
;
1420 if (do_test_mode(udc
))
1421 set_protocol_stall(udc
, ep
);
1424 pr_err("udc: %s: TXCOMP: Invalid endpoint state %d, "
1425 "halting endpoint...\n",
1426 ep
->ep
.name
, ep
->state
);
1427 set_protocol_stall(udc
, ep
);
1433 if ((epstatus
& epctrl
) & USBA_RX_BK_RDY
) {
1434 switch (ep
->state
) {
1435 case STATUS_STAGE_OUT
:
1436 usba_ep_writel(ep
, CLR_STA
, USBA_RX_BK_RDY
);
1437 usba_ep_writel(ep
, CTL_DIS
, USBA_RX_BK_RDY
);
1440 list_del_init(&req
->queue
);
1441 request_complete(ep
, req
, 0);
1443 ep
->state
= WAIT_FOR_SETUP
;
1446 case DATA_STAGE_OUT
:
1451 usba_ep_writel(ep
, CLR_STA
, USBA_RX_BK_RDY
);
1452 usba_ep_writel(ep
, CTL_DIS
, USBA_RX_BK_RDY
);
1453 pr_err("udc: %s: RXRDY: Invalid endpoint state %d, "
1454 "halting endpoint...\n",
1455 ep
->ep
.name
, ep
->state
);
1456 set_protocol_stall(udc
, ep
);
1462 if (epstatus
& USBA_RX_SETUP
) {
1464 struct usb_ctrlrequest crq
;
1465 unsigned long data
[2];
1467 unsigned int pkt_len
;
1470 if (ep
->state
!= WAIT_FOR_SETUP
) {
1472 * Didn't expect a SETUP packet at this
1473 * point. Clean up any pending requests (which
1474 * may be successful).
1476 int status
= -EPROTO
;
1479 * RXRDY and TXCOMP are dropped when SETUP
1480 * packets arrive. Just pretend we received
1481 * the status packet.
1483 if (ep
->state
== STATUS_STAGE_OUT
1484 || ep
->state
== STATUS_STAGE_IN
) {
1485 usba_ep_writel(ep
, CTL_DIS
, USBA_RX_BK_RDY
);
1490 list_del_init(&req
->queue
);
1491 request_complete(ep
, req
, status
);
1495 pkt_len
= USBA_BFEXT(BYTE_COUNT
, usba_ep_readl(ep
, STA
));
1496 DBG(DBG_HW
, "Packet length: %u\n", pkt_len
);
1497 if (pkt_len
!= sizeof(crq
)) {
1498 pr_warning("udc: Invalid packet length %u "
1499 "(expected %zu)\n", pkt_len
, sizeof(crq
));
1500 set_protocol_stall(udc
, ep
);
1504 DBG(DBG_FIFO
, "Copying ctrl request from 0x%p:\n", ep
->fifo
);
1505 memcpy_fromio(crq
.data
, ep
->fifo
, sizeof(crq
));
1507 /* Free up one bank in the FIFO so that we can
1508 * generate or receive a reply right away. */
1509 usba_ep_writel(ep
, CLR_STA
, USBA_RX_SETUP
);
1511 /* printk(KERN_DEBUG "setup: %d: %02x.%02x\n",
1512 ep->state, crq.crq.bRequestType,
1513 crq.crq.bRequest); */
1515 if (crq
.crq
.bRequestType
& USB_DIR_IN
) {
1517 * The USB 2.0 spec states that "if wLength is
1518 * zero, there is no data transfer phase."
1519 * However, testusb #14 seems to actually
1520 * expect a data phase even if wLength = 0...
1522 ep
->state
= DATA_STAGE_IN
;
1524 if (crq
.crq
.wLength
!= cpu_to_le16(0))
1525 ep
->state
= DATA_STAGE_OUT
;
1527 ep
->state
= STATUS_STAGE_IN
;
1532 ret
= handle_ep0_setup(udc
, ep
, &crq
.crq
);
1534 spin_unlock(&udc
->lock
);
1535 ret
= udc
->driver
->setup(&udc
->gadget
, &crq
.crq
);
1536 spin_lock(&udc
->lock
);
1539 DBG(DBG_BUS
, "req %02x.%02x, length %d, state %d, ret %d\n",
1540 crq
.crq
.bRequestType
, crq
.crq
.bRequest
,
1541 le16_to_cpu(crq
.crq
.wLength
), ep
->state
, ret
);
1544 /* Let the host know that we failed */
1545 set_protocol_stall(udc
, ep
);
1550 static void usba_ep_irq(struct usba_udc
*udc
, struct usba_ep
*ep
)
1552 struct usba_request
*req
;
1556 epstatus
= usba_ep_readl(ep
, STA
);
1557 epctrl
= usba_ep_readl(ep
, CTL
);
1559 DBG(DBG_INT
, "%s: interrupt, status: 0x%08x\n", ep
->ep
.name
, epstatus
);
1561 while ((epctrl
& USBA_TX_PK_RDY
) && !(epstatus
& USBA_TX_PK_RDY
)) {
1562 DBG(DBG_BUS
, "%s: TX PK ready\n", ep
->ep
.name
);
1564 if (list_empty(&ep
->queue
)) {
1565 dev_warn(&udc
->pdev
->dev
, "ep_irq: queue empty\n");
1566 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_PK_RDY
);
1570 req
= list_entry(ep
->queue
.next
, struct usba_request
, queue
);
1572 if (req
->using_dma
) {
1573 /* Send a zero-length packet */
1574 usba_ep_writel(ep
, SET_STA
,
1576 usba_ep_writel(ep
, CTL_DIS
,
1578 list_del_init(&req
->queue
);
1579 submit_next_request(ep
);
1580 request_complete(ep
, req
, 0);
1583 next_fifo_transaction(ep
, req
);
1585 submit_request(ep
, req
);
1587 if (req
->last_transaction
) {
1588 list_del_init(&req
->queue
);
1589 submit_next_request(ep
);
1590 request_complete(ep
, req
, 0);
1594 epstatus
= usba_ep_readl(ep
, STA
);
1595 epctrl
= usba_ep_readl(ep
, CTL
);
1597 if ((epstatus
& epctrl
) & USBA_RX_BK_RDY
) {
1598 DBG(DBG_BUS
, "%s: RX data ready\n", ep
->ep
.name
);
1600 usba_ep_writel(ep
, CLR_STA
, USBA_RX_BK_RDY
);
1604 static void usba_dma_irq(struct usba_udc
*udc
, struct usba_ep
*ep
)
1606 struct usba_request
*req
;
1607 u32 status
, control
, pending
;
1609 status
= usba_dma_readl(ep
, STATUS
);
1610 control
= usba_dma_readl(ep
, CONTROL
);
1611 #ifdef CONFIG_USB_GADGET_DEBUG_FS
1612 ep
->last_dma_status
= status
;
1614 pending
= status
& control
;
1615 DBG(DBG_INT
| DBG_DMA
, "dma irq, s/%#08x, c/%#08x\n", status
, control
);
1617 if (status
& USBA_DMA_CH_EN
) {
1618 dev_err(&udc
->pdev
->dev
,
1619 "DMA_CH_EN is set after transfer is finished!\n");
1620 dev_err(&udc
->pdev
->dev
,
1621 "status=%#08x, pending=%#08x, control=%#08x\n",
1622 status
, pending
, control
);
1625 * try to pretend nothing happened. We might have to
1626 * do something here...
1630 if (list_empty(&ep
->queue
))
1631 /* Might happen if a reset comes along at the right moment */
1634 if (pending
& (USBA_DMA_END_TR_ST
| USBA_DMA_END_BUF_ST
)) {
1635 req
= list_entry(ep
->queue
.next
, struct usba_request
, queue
);
1636 usba_update_req(ep
, req
, status
);
1638 list_del_init(&req
->queue
);
1639 submit_next_request(ep
);
1640 request_complete(ep
, req
, 0);
1644 static irqreturn_t
usba_udc_irq(int irq
, void *devid
)
1646 struct usba_udc
*udc
= devid
;
1651 spin_lock(&udc
->lock
);
1653 status
= usba_readl(udc
, INT_STA
);
1654 DBG(DBG_INT
, "irq, status=%#08x\n", status
);
1656 if (status
& USBA_DET_SUSPEND
) {
1658 usba_writel(udc
, INT_CLR
, USBA_DET_SUSPEND
);
1659 DBG(DBG_BUS
, "Suspend detected\n");
1660 if (udc
->gadget
.speed
!= USB_SPEED_UNKNOWN
1661 && udc
->driver
&& udc
->driver
->suspend
) {
1662 spin_unlock(&udc
->lock
);
1663 udc
->driver
->suspend(&udc
->gadget
);
1664 spin_lock(&udc
->lock
);
1668 if (status
& USBA_WAKE_UP
) {
1670 usba_writel(udc
, INT_CLR
, USBA_WAKE_UP
);
1671 DBG(DBG_BUS
, "Wake Up CPU detected\n");
1674 if (status
& USBA_END_OF_RESUME
) {
1675 usba_writel(udc
, INT_CLR
, USBA_END_OF_RESUME
);
1676 DBG(DBG_BUS
, "Resume detected\n");
1677 if (udc
->gadget
.speed
!= USB_SPEED_UNKNOWN
1678 && udc
->driver
&& udc
->driver
->resume
) {
1679 spin_unlock(&udc
->lock
);
1680 udc
->driver
->resume(&udc
->gadget
);
1681 spin_lock(&udc
->lock
);
1685 dma_status
= USBA_BFEXT(DMA_INT
, status
);
1689 for (i
= 1; i
< USBA_NR_ENDPOINTS
; i
++)
1690 if (dma_status
& (1 << i
))
1691 usba_dma_irq(udc
, &usba_ep
[i
]);
1694 ep_status
= USBA_BFEXT(EPT_INT
, status
);
1698 for (i
= 0; i
< USBA_NR_ENDPOINTS
; i
++)
1699 if (ep_status
& (1 << i
)) {
1700 if (ep_is_control(&usba_ep
[i
]))
1701 usba_control_irq(udc
, &usba_ep
[i
]);
1703 usba_ep_irq(udc
, &usba_ep
[i
]);
1707 if (status
& USBA_END_OF_RESET
) {
1708 struct usba_ep
*ep0
;
1710 usba_writel(udc
, INT_CLR
, USBA_END_OF_RESET
);
1711 reset_all_endpoints(udc
);
1713 if (udc
->gadget
.speed
!= USB_SPEED_UNKNOWN
1714 && udc
->driver
->disconnect
) {
1715 udc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1716 spin_unlock(&udc
->lock
);
1717 udc
->driver
->disconnect(&udc
->gadget
);
1718 spin_lock(&udc
->lock
);
1721 if (status
& USBA_HIGH_SPEED
)
1722 udc
->gadget
.speed
= USB_SPEED_HIGH
;
1724 udc
->gadget
.speed
= USB_SPEED_FULL
;
1725 DBG(DBG_BUS
, "%s bus reset detected\n",
1726 usb_speed_string(udc
->gadget
.speed
));
1729 ep0
->desc
= &usba_ep0_desc
;
1730 ep0
->state
= WAIT_FOR_SETUP
;
1731 usba_ep_writel(ep0
, CFG
,
1732 (USBA_BF(EPT_SIZE
, EP0_EPT_SIZE
)
1733 | USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_CONTROL
)
1734 | USBA_BF(BK_NUMBER
, USBA_BK_NUMBER_ONE
)));
1735 usba_ep_writel(ep0
, CTL_ENB
,
1736 USBA_EPT_ENABLE
| USBA_RX_SETUP
);
1737 usba_writel(udc
, INT_ENB
,
1738 (usba_readl(udc
, INT_ENB
)
1739 | USBA_BF(EPT_INT
, 1)
1741 | USBA_END_OF_RESUME
));
1744 * Unclear why we hit this irregularly, e.g. in usbtest,
1745 * but it's clearly harmless...
1747 if (!(usba_ep_readl(ep0
, CFG
) & USBA_EPT_MAPPED
))
1748 dev_dbg(&udc
->pdev
->dev
,
1749 "ODD: EP0 configuration is invalid!\n");
1752 spin_unlock(&udc
->lock
);
1757 static irqreturn_t
usba_vbus_irq(int irq
, void *devid
)
1759 struct usba_udc
*udc
= devid
;
1765 spin_lock(&udc
->lock
);
1767 /* May happen if Vbus pin toggles during probe() */
1771 vbus
= vbus_is_present(udc
);
1772 if (vbus
!= udc
->vbus_prev
) {
1775 usba_writel(udc
, CTRL
, USBA_ENABLE_MASK
);
1776 usba_writel(udc
, INT_ENB
, USBA_END_OF_RESET
);
1778 udc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1779 reset_all_endpoints(udc
);
1781 usba_writel(udc
, CTRL
, USBA_DISABLE_MASK
);
1782 if (udc
->driver
->disconnect
) {
1783 spin_unlock(&udc
->lock
);
1784 udc
->driver
->disconnect(&udc
->gadget
);
1785 spin_lock(&udc
->lock
);
1788 udc
->vbus_prev
= vbus
;
1792 spin_unlock(&udc
->lock
);
1797 static int atmel_usba_start(struct usb_gadget_driver
*driver
,
1798 int (*bind
)(struct usb_gadget
*))
1800 struct usba_udc
*udc
= &the_udc
;
1801 unsigned long flags
;
1807 spin_lock_irqsave(&udc
->lock
, flags
);
1809 spin_unlock_irqrestore(&udc
->lock
, flags
);
1813 udc
->devstatus
= 1 << USB_DEVICE_SELF_POWERED
;
1814 udc
->driver
= driver
;
1815 udc
->gadget
.dev
.driver
= &driver
->driver
;
1816 spin_unlock_irqrestore(&udc
->lock
, flags
);
1818 clk_enable(udc
->pclk
);
1819 clk_enable(udc
->hclk
);
1821 ret
= bind(&udc
->gadget
);
1823 DBG(DBG_ERR
, "Could not bind to driver %s: error %d\n",
1824 driver
->driver
.name
, ret
);
1825 goto err_driver_bind
;
1828 DBG(DBG_GADGET
, "registered driver `%s'\n", driver
->driver
.name
);
1831 if (gpio_is_valid(udc
->vbus_pin
))
1832 enable_irq(gpio_to_irq(udc
->vbus_pin
));
1834 /* If Vbus is present, enable the controller and wait for reset */
1835 spin_lock_irqsave(&udc
->lock
, flags
);
1836 if (vbus_is_present(udc
) && udc
->vbus_prev
== 0) {
1838 usba_writel(udc
, CTRL
, USBA_ENABLE_MASK
);
1839 usba_writel(udc
, INT_ENB
, USBA_END_OF_RESET
);
1841 spin_unlock_irqrestore(&udc
->lock
, flags
);
1847 udc
->gadget
.dev
.driver
= NULL
;
1851 static int atmel_usba_stop(struct usb_gadget_driver
*driver
)
1853 struct usba_udc
*udc
= &the_udc
;
1854 unsigned long flags
;
1858 if (driver
!= udc
->driver
|| !driver
->unbind
)
1861 if (gpio_is_valid(udc
->vbus_pin
))
1862 disable_irq(gpio_to_irq(udc
->vbus_pin
));
1864 spin_lock_irqsave(&udc
->lock
, flags
);
1865 udc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1866 reset_all_endpoints(udc
);
1867 spin_unlock_irqrestore(&udc
->lock
, flags
);
1869 /* This will also disable the DP pullup */
1871 usba_writel(udc
, CTRL
, USBA_DISABLE_MASK
);
1873 if (udc
->driver
->disconnect
)
1874 udc
->driver
->disconnect(&udc
->gadget
);
1876 driver
->unbind(&udc
->gadget
);
1877 udc
->gadget
.dev
.driver
= NULL
;
1880 clk_disable(udc
->hclk
);
1881 clk_disable(udc
->pclk
);
1883 DBG(DBG_GADGET
, "unregistered driver `%s'\n", driver
->driver
.name
);
1888 static int __init
usba_udc_probe(struct platform_device
*pdev
)
1890 struct usba_platform_data
*pdata
= pdev
->dev
.platform_data
;
1891 struct resource
*regs
, *fifo
;
1892 struct clk
*pclk
, *hclk
;
1893 struct usba_udc
*udc
= &the_udc
;
1896 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, CTRL_IOMEM_ID
);
1897 fifo
= platform_get_resource(pdev
, IORESOURCE_MEM
, FIFO_IOMEM_ID
);
1898 if (!regs
|| !fifo
|| !pdata
)
1901 irq
= platform_get_irq(pdev
, 0);
1905 pclk
= clk_get(&pdev
->dev
, "pclk");
1907 return PTR_ERR(pclk
);
1908 hclk
= clk_get(&pdev
->dev
, "hclk");
1910 ret
= PTR_ERR(hclk
);
1914 spin_lock_init(&udc
->lock
);
1918 udc
->vbus_pin
= -ENODEV
;
1921 udc
->regs
= ioremap(regs
->start
, resource_size(regs
));
1923 dev_err(&pdev
->dev
, "Unable to map I/O memory, aborting.\n");
1926 dev_info(&pdev
->dev
, "MMIO registers at 0x%08lx mapped at %p\n",
1927 (unsigned long)regs
->start
, udc
->regs
);
1928 udc
->fifo
= ioremap(fifo
->start
, resource_size(fifo
));
1930 dev_err(&pdev
->dev
, "Unable to map FIFO, aborting.\n");
1933 dev_info(&pdev
->dev
, "FIFO at 0x%08lx mapped at %p\n",
1934 (unsigned long)fifo
->start
, udc
->fifo
);
1936 device_initialize(&udc
->gadget
.dev
);
1937 udc
->gadget
.dev
.parent
= &pdev
->dev
;
1938 udc
->gadget
.dev
.dma_mask
= pdev
->dev
.dma_mask
;
1940 platform_set_drvdata(pdev
, udc
);
1942 /* Make sure we start from a clean slate */
1945 usba_writel(udc
, CTRL
, USBA_DISABLE_MASK
);
1948 usba_ep
= kzalloc(sizeof(struct usba_ep
) * pdata
->num_ep
,
1953 the_udc
.gadget
.ep0
= &usba_ep
[0].ep
;
1955 INIT_LIST_HEAD(&usba_ep
[0].ep
.ep_list
);
1956 usba_ep
[0].ep_regs
= udc
->regs
+ USBA_EPT_BASE(0);
1957 usba_ep
[0].dma_regs
= udc
->regs
+ USBA_DMA_BASE(0);
1958 usba_ep
[0].fifo
= udc
->fifo
+ USBA_FIFO_BASE(0);
1959 usba_ep
[0].ep
.ops
= &usba_ep_ops
;
1960 usba_ep
[0].ep
.name
= pdata
->ep
[0].name
;
1961 usba_ep
[0].ep
.maxpacket
= pdata
->ep
[0].fifo_size
;
1962 usba_ep
[0].udc
= &the_udc
;
1963 INIT_LIST_HEAD(&usba_ep
[0].queue
);
1964 usba_ep
[0].fifo_size
= pdata
->ep
[0].fifo_size
;
1965 usba_ep
[0].nr_banks
= pdata
->ep
[0].nr_banks
;
1966 usba_ep
[0].index
= pdata
->ep
[0].index
;
1967 usba_ep
[0].can_dma
= pdata
->ep
[0].can_dma
;
1968 usba_ep
[0].can_isoc
= pdata
->ep
[0].can_isoc
;
1970 for (i
= 1; i
< pdata
->num_ep
; i
++) {
1971 struct usba_ep
*ep
= &usba_ep
[i
];
1973 ep
->ep_regs
= udc
->regs
+ USBA_EPT_BASE(i
);
1974 ep
->dma_regs
= udc
->regs
+ USBA_DMA_BASE(i
);
1975 ep
->fifo
= udc
->fifo
+ USBA_FIFO_BASE(i
);
1976 ep
->ep
.ops
= &usba_ep_ops
;
1977 ep
->ep
.name
= pdata
->ep
[i
].name
;
1978 ep
->ep
.maxpacket
= pdata
->ep
[i
].fifo_size
;
1980 INIT_LIST_HEAD(&ep
->queue
);
1981 ep
->fifo_size
= pdata
->ep
[i
].fifo_size
;
1982 ep
->nr_banks
= pdata
->ep
[i
].nr_banks
;
1983 ep
->index
= pdata
->ep
[i
].index
;
1984 ep
->can_dma
= pdata
->ep
[i
].can_dma
;
1985 ep
->can_isoc
= pdata
->ep
[i
].can_isoc
;
1987 list_add_tail(&ep
->ep
.ep_list
, &udc
->gadget
.ep_list
);
1990 ret
= request_irq(irq
, usba_udc_irq
, 0, "atmel_usba_udc", udc
);
1992 dev_err(&pdev
->dev
, "Cannot request irq %d (error %d)\n",
1994 goto err_request_irq
;
1998 ret
= device_add(&udc
->gadget
.dev
);
2000 dev_dbg(&pdev
->dev
, "Could not add gadget: %d\n", ret
);
2001 goto err_device_add
;
2004 if (gpio_is_valid(pdata
->vbus_pin
)) {
2005 if (!gpio_request(pdata
->vbus_pin
, "atmel_usba_udc")) {
2006 udc
->vbus_pin
= pdata
->vbus_pin
;
2007 udc
->vbus_pin_inverted
= pdata
->vbus_pin_inverted
;
2009 ret
= request_irq(gpio_to_irq(udc
->vbus_pin
),
2011 "atmel_usba_udc", udc
);
2013 gpio_free(udc
->vbus_pin
);
2014 udc
->vbus_pin
= -ENODEV
;
2015 dev_warn(&udc
->pdev
->dev
,
2016 "failed to request vbus irq; "
2017 "assuming always on\n");
2019 disable_irq(gpio_to_irq(udc
->vbus_pin
));
2022 /* gpio_request fail so use -EINVAL for gpio_is_valid */
2023 udc
->vbus_pin
= -EINVAL
;
2027 ret
= usb_add_gadget_udc(&pdev
->dev
, &udc
->gadget
);
2031 usba_init_debugfs(udc
);
2032 for (i
= 1; i
< pdata
->num_ep
; i
++)
2033 usba_ep_init_debugfs(udc
, &usba_ep
[i
]);
2038 if (gpio_is_valid(pdata
->vbus_pin
)) {
2039 free_irq(gpio_to_irq(udc
->vbus_pin
), udc
);
2040 gpio_free(udc
->vbus_pin
);
2043 device_unregister(&udc
->gadget
.dev
);
2058 platform_set_drvdata(pdev
, NULL
);
2063 static int __exit
usba_udc_remove(struct platform_device
*pdev
)
2065 struct usba_udc
*udc
;
2067 struct usba_platform_data
*pdata
= pdev
->dev
.platform_data
;
2069 udc
= platform_get_drvdata(pdev
);
2071 usb_del_gadget_udc(&udc
->gadget
);
2073 for (i
= 1; i
< pdata
->num_ep
; i
++)
2074 usba_ep_cleanup_debugfs(&usba_ep
[i
]);
2075 usba_cleanup_debugfs(udc
);
2077 if (gpio_is_valid(udc
->vbus_pin
)) {
2078 free_irq(gpio_to_irq(udc
->vbus_pin
), udc
);
2079 gpio_free(udc
->vbus_pin
);
2082 free_irq(udc
->irq
, udc
);
2089 device_unregister(&udc
->gadget
.dev
);
2094 static struct platform_driver udc_driver
= {
2095 .remove
= __exit_p(usba_udc_remove
),
2097 .name
= "atmel_usba_udc",
2098 .owner
= THIS_MODULE
,
2102 static int __init
udc_init(void)
2104 return platform_driver_probe(&udc_driver
, usba_udc_probe
);
2106 module_init(udc_init
);
2108 static void __exit
udc_exit(void)
2110 platform_driver_unregister(&udc_driver
);
2112 module_exit(udc_exit
);
2114 MODULE_DESCRIPTION("Atmel USBA UDC driver");
2115 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
2116 MODULE_LICENSE("GPL");
2117 MODULE_ALIAS("platform:atmel_usba_udc");