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>
24 #include <linux/platform_data/atmel.h>
26 #include <linux/of_gpio.h>
30 #include "atmel_usba_udc.h"
32 #ifdef CONFIG_USB_GADGET_DEBUG_FS
33 #include <linux/debugfs.h>
34 #include <linux/uaccess.h>
36 static int queue_dbg_open(struct inode
*inode
, struct file
*file
)
38 struct usba_ep
*ep
= inode
->i_private
;
39 struct usba_request
*req
, *req_copy
;
40 struct list_head
*queue_data
;
42 queue_data
= kmalloc(sizeof(*queue_data
), GFP_KERNEL
);
45 INIT_LIST_HEAD(queue_data
);
47 spin_lock_irq(&ep
->udc
->lock
);
48 list_for_each_entry(req
, &ep
->queue
, queue
) {
49 req_copy
= kmemdup(req
, sizeof(*req_copy
), GFP_ATOMIC
);
52 list_add_tail(&req_copy
->queue
, queue_data
);
54 spin_unlock_irq(&ep
->udc
->lock
);
56 file
->private_data
= queue_data
;
60 spin_unlock_irq(&ep
->udc
->lock
);
61 list_for_each_entry_safe(req
, req_copy
, queue_data
, queue
) {
62 list_del(&req
->queue
);
70 * bbbbbbbb llllllll IZS sssss nnnn FDL\n\0
74 * I/i: interrupt/no interrupt
76 * S/s: short ok/short not ok
79 * F/f: submitted/not submitted to FIFO
80 * D/d: using/not using DMA
81 * L/l: last transaction/not last transaction
83 static ssize_t
queue_dbg_read(struct file
*file
, char __user
*buf
,
84 size_t nbytes
, loff_t
*ppos
)
86 struct list_head
*queue
= file
->private_data
;
87 struct usba_request
*req
, *tmp_req
;
88 size_t len
, remaining
, actual
= 0;
91 if (!access_ok(VERIFY_WRITE
, buf
, nbytes
))
94 mutex_lock(&file_inode(file
)->i_mutex
);
95 list_for_each_entry_safe(req
, tmp_req
, queue
, queue
) {
96 len
= snprintf(tmpbuf
, sizeof(tmpbuf
),
97 "%8p %08x %c%c%c %5d %c%c%c\n",
98 req
->req
.buf
, req
->req
.length
,
99 req
->req
.no_interrupt
? 'i' : 'I',
100 req
->req
.zero
? 'Z' : 'z',
101 req
->req
.short_not_ok
? 's' : 'S',
103 req
->submitted
? 'F' : 'f',
104 req
->using_dma
? 'D' : 'd',
105 req
->last_transaction
? 'L' : 'l');
106 len
= min(len
, sizeof(tmpbuf
));
110 list_del(&req
->queue
);
113 remaining
= __copy_to_user(buf
, tmpbuf
, len
);
114 actual
+= len
- remaining
;
121 mutex_unlock(&file_inode(file
)->i_mutex
);
126 static int queue_dbg_release(struct inode
*inode
, struct file
*file
)
128 struct list_head
*queue_data
= file
->private_data
;
129 struct usba_request
*req
, *tmp_req
;
131 list_for_each_entry_safe(req
, tmp_req
, queue_data
, queue
) {
132 list_del(&req
->queue
);
139 static int regs_dbg_open(struct inode
*inode
, struct file
*file
)
141 struct usba_udc
*udc
;
146 mutex_lock(&inode
->i_mutex
);
147 udc
= inode
->i_private
;
148 data
= kmalloc(inode
->i_size
, GFP_KERNEL
);
152 spin_lock_irq(&udc
->lock
);
153 for (i
= 0; i
< inode
->i_size
/ 4; i
++)
154 data
[i
] = __raw_readl(udc
->regs
+ i
* 4);
155 spin_unlock_irq(&udc
->lock
);
157 file
->private_data
= data
;
161 mutex_unlock(&inode
->i_mutex
);
166 static ssize_t
regs_dbg_read(struct file
*file
, char __user
*buf
,
167 size_t nbytes
, loff_t
*ppos
)
169 struct inode
*inode
= file_inode(file
);
172 mutex_lock(&inode
->i_mutex
);
173 ret
= simple_read_from_buffer(buf
, nbytes
, ppos
,
175 file_inode(file
)->i_size
);
176 mutex_unlock(&inode
->i_mutex
);
181 static int regs_dbg_release(struct inode
*inode
, struct file
*file
)
183 kfree(file
->private_data
);
187 const struct file_operations queue_dbg_fops
= {
188 .owner
= THIS_MODULE
,
189 .open
= queue_dbg_open
,
191 .read
= queue_dbg_read
,
192 .release
= queue_dbg_release
,
195 const struct file_operations regs_dbg_fops
= {
196 .owner
= THIS_MODULE
,
197 .open
= regs_dbg_open
,
198 .llseek
= generic_file_llseek
,
199 .read
= regs_dbg_read
,
200 .release
= regs_dbg_release
,
203 static void usba_ep_init_debugfs(struct usba_udc
*udc
,
206 struct dentry
*ep_root
;
208 ep_root
= debugfs_create_dir(ep
->ep
.name
, udc
->debugfs_root
);
211 ep
->debugfs_dir
= ep_root
;
213 ep
->debugfs_queue
= debugfs_create_file("queue", 0400, ep_root
,
214 ep
, &queue_dbg_fops
);
215 if (!ep
->debugfs_queue
)
219 ep
->debugfs_dma_status
220 = debugfs_create_u32("dma_status", 0400, ep_root
,
221 &ep
->last_dma_status
);
222 if (!ep
->debugfs_dma_status
)
225 if (ep_is_control(ep
)) {
227 = debugfs_create_u32("state", 0400, ep_root
,
229 if (!ep
->debugfs_state
)
237 debugfs_remove(ep
->debugfs_dma_status
);
239 debugfs_remove(ep
->debugfs_queue
);
241 debugfs_remove(ep_root
);
243 dev_err(&ep
->udc
->pdev
->dev
,
244 "failed to create debugfs directory for %s\n", ep
->ep
.name
);
247 static void usba_ep_cleanup_debugfs(struct usba_ep
*ep
)
249 debugfs_remove(ep
->debugfs_queue
);
250 debugfs_remove(ep
->debugfs_dma_status
);
251 debugfs_remove(ep
->debugfs_state
);
252 debugfs_remove(ep
->debugfs_dir
);
253 ep
->debugfs_dma_status
= NULL
;
254 ep
->debugfs_dir
= NULL
;
257 static void usba_init_debugfs(struct usba_udc
*udc
)
259 struct dentry
*root
, *regs
;
260 struct resource
*regs_resource
;
262 root
= debugfs_create_dir(udc
->gadget
.name
, NULL
);
263 if (IS_ERR(root
) || !root
)
265 udc
->debugfs_root
= root
;
267 regs
= debugfs_create_file("regs", 0400, root
, udc
, ®s_dbg_fops
);
271 regs_resource
= platform_get_resource(udc
->pdev
, IORESOURCE_MEM
,
273 regs
->d_inode
->i_size
= resource_size(regs_resource
);
274 udc
->debugfs_regs
= regs
;
276 usba_ep_init_debugfs(udc
, to_usba_ep(udc
->gadget
.ep0
));
281 debugfs_remove(root
);
283 udc
->debugfs_root
= NULL
;
284 dev_err(&udc
->pdev
->dev
, "debugfs is not available\n");
287 static void usba_cleanup_debugfs(struct usba_udc
*udc
)
289 usba_ep_cleanup_debugfs(to_usba_ep(udc
->gadget
.ep0
));
290 debugfs_remove(udc
->debugfs_regs
);
291 debugfs_remove(udc
->debugfs_root
);
292 udc
->debugfs_regs
= NULL
;
293 udc
->debugfs_root
= NULL
;
296 static inline void usba_ep_init_debugfs(struct usba_udc
*udc
,
302 static inline void usba_ep_cleanup_debugfs(struct usba_ep
*ep
)
307 static inline void usba_init_debugfs(struct usba_udc
*udc
)
312 static inline void usba_cleanup_debugfs(struct usba_udc
*udc
)
318 static int vbus_is_present(struct usba_udc
*udc
)
320 if (gpio_is_valid(udc
->vbus_pin
))
321 return gpio_get_value(udc
->vbus_pin
) ^ udc
->vbus_pin_inverted
;
323 /* No Vbus detection: Assume always present */
327 #if defined(CONFIG_ARCH_AT91SAM9RL)
329 #include <linux/clk/at91_pmc.h>
331 static void toggle_bias(int is_on
)
333 unsigned int uckr
= at91_pmc_read(AT91_CKGR_UCKR
);
336 at91_pmc_write(AT91_CKGR_UCKR
, uckr
| AT91_PMC_BIASEN
);
338 at91_pmc_write(AT91_CKGR_UCKR
, uckr
& ~(AT91_PMC_BIASEN
));
343 static void toggle_bias(int is_on
)
347 #endif /* CONFIG_ARCH_AT91SAM9RL */
349 static void next_fifo_transaction(struct usba_ep
*ep
, struct usba_request
*req
)
351 unsigned int transaction_len
;
353 transaction_len
= req
->req
.length
- req
->req
.actual
;
354 req
->last_transaction
= 1;
355 if (transaction_len
> ep
->ep
.maxpacket
) {
356 transaction_len
= ep
->ep
.maxpacket
;
357 req
->last_transaction
= 0;
358 } else if (transaction_len
== ep
->ep
.maxpacket
&& req
->req
.zero
)
359 req
->last_transaction
= 0;
361 DBG(DBG_QUEUE
, "%s: submit_transaction, req %p (length %d)%s\n",
362 ep
->ep
.name
, req
, transaction_len
,
363 req
->last_transaction
? ", done" : "");
365 memcpy_toio(ep
->fifo
, req
->req
.buf
+ req
->req
.actual
, transaction_len
);
366 usba_ep_writel(ep
, SET_STA
, USBA_TX_PK_RDY
);
367 req
->req
.actual
+= transaction_len
;
370 static void submit_request(struct usba_ep
*ep
, struct usba_request
*req
)
372 DBG(DBG_QUEUE
, "%s: submit_request: req %p (length %d)\n",
373 ep
->ep
.name
, req
, req
->req
.length
);
378 if (req
->using_dma
) {
379 if (req
->req
.length
== 0) {
380 usba_ep_writel(ep
, CTL_ENB
, USBA_TX_PK_RDY
);
385 usba_ep_writel(ep
, CTL_ENB
, USBA_SHORT_PACKET
);
387 usba_ep_writel(ep
, CTL_DIS
, USBA_SHORT_PACKET
);
389 usba_dma_writel(ep
, ADDRESS
, req
->req
.dma
);
390 usba_dma_writel(ep
, CONTROL
, req
->ctrl
);
392 next_fifo_transaction(ep
, req
);
393 if (req
->last_transaction
) {
394 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_PK_RDY
);
395 usba_ep_writel(ep
, CTL_ENB
, USBA_TX_COMPLETE
);
397 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_COMPLETE
);
398 usba_ep_writel(ep
, CTL_ENB
, USBA_TX_PK_RDY
);
403 static void submit_next_request(struct usba_ep
*ep
)
405 struct usba_request
*req
;
407 if (list_empty(&ep
->queue
)) {
408 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_PK_RDY
| USBA_RX_BK_RDY
);
412 req
= list_entry(ep
->queue
.next
, struct usba_request
, queue
);
414 submit_request(ep
, req
);
417 static void send_status(struct usba_udc
*udc
, struct usba_ep
*ep
)
419 ep
->state
= STATUS_STAGE_IN
;
420 usba_ep_writel(ep
, SET_STA
, USBA_TX_PK_RDY
);
421 usba_ep_writel(ep
, CTL_ENB
, USBA_TX_COMPLETE
);
424 static void receive_data(struct usba_ep
*ep
)
426 struct usba_udc
*udc
= ep
->udc
;
427 struct usba_request
*req
;
428 unsigned long status
;
429 unsigned int bytecount
, nr_busy
;
432 status
= usba_ep_readl(ep
, STA
);
433 nr_busy
= USBA_BFEXT(BUSY_BANKS
, status
);
435 DBG(DBG_QUEUE
, "receive data: nr_busy=%u\n", nr_busy
);
437 while (nr_busy
> 0) {
438 if (list_empty(&ep
->queue
)) {
439 usba_ep_writel(ep
, CTL_DIS
, USBA_RX_BK_RDY
);
442 req
= list_entry(ep
->queue
.next
,
443 struct usba_request
, queue
);
445 bytecount
= USBA_BFEXT(BYTE_COUNT
, status
);
447 if (status
& (1 << 31))
449 if (req
->req
.actual
+ bytecount
>= req
->req
.length
) {
451 bytecount
= req
->req
.length
- req
->req
.actual
;
454 memcpy_fromio(req
->req
.buf
+ req
->req
.actual
,
455 ep
->fifo
, bytecount
);
456 req
->req
.actual
+= bytecount
;
458 usba_ep_writel(ep
, CLR_STA
, USBA_RX_BK_RDY
);
461 DBG(DBG_QUEUE
, "%s: request done\n", ep
->ep
.name
);
463 list_del_init(&req
->queue
);
464 usba_ep_writel(ep
, CTL_DIS
, USBA_RX_BK_RDY
);
465 spin_unlock(&udc
->lock
);
466 req
->req
.complete(&ep
->ep
, &req
->req
);
467 spin_lock(&udc
->lock
);
470 status
= usba_ep_readl(ep
, STA
);
471 nr_busy
= USBA_BFEXT(BUSY_BANKS
, status
);
473 if (is_complete
&& ep_is_control(ep
)) {
474 send_status(udc
, ep
);
481 request_complete(struct usba_ep
*ep
, struct usba_request
*req
, int status
)
483 struct usba_udc
*udc
= ep
->udc
;
485 WARN_ON(!list_empty(&req
->queue
));
487 if (req
->req
.status
== -EINPROGRESS
)
488 req
->req
.status
= status
;
491 usb_gadget_unmap_request(&udc
->gadget
, &req
->req
, ep
->is_in
);
493 DBG(DBG_GADGET
| DBG_REQ
,
494 "%s: req %p complete: status %d, actual %u\n",
495 ep
->ep
.name
, req
, req
->req
.status
, req
->req
.actual
);
497 spin_unlock(&udc
->lock
);
498 req
->req
.complete(&ep
->ep
, &req
->req
);
499 spin_lock(&udc
->lock
);
503 request_complete_list(struct usba_ep
*ep
, struct list_head
*list
, int status
)
505 struct usba_request
*req
, *tmp_req
;
507 list_for_each_entry_safe(req
, tmp_req
, list
, queue
) {
508 list_del_init(&req
->queue
);
509 request_complete(ep
, req
, status
);
514 usba_ep_enable(struct usb_ep
*_ep
, const struct usb_endpoint_descriptor
*desc
)
516 struct usba_ep
*ep
= to_usba_ep(_ep
);
517 struct usba_udc
*udc
= ep
->udc
;
518 unsigned long flags
, ept_cfg
, maxpacket
;
519 unsigned int nr_trans
;
521 DBG(DBG_GADGET
, "%s: ep_enable: desc=%p\n", ep
->ep
.name
, desc
);
523 maxpacket
= usb_endpoint_maxp(desc
) & 0x7ff;
525 if (((desc
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
) != ep
->index
)
527 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
529 || maxpacket
> ep
->fifo_size
) {
530 DBG(DBG_ERR
, "ep_enable: Invalid argument");
538 ept_cfg
= USBA_BF(EPT_SIZE
, USBA_EPT_SIZE_8
);
540 /* LSB is bit 1, not 0 */
541 ept_cfg
= USBA_BF(EPT_SIZE
, fls(maxpacket
- 1) - 3);
543 DBG(DBG_HW
, "%s: EPT_SIZE = %lu (maxpacket = %lu)\n",
544 ep
->ep
.name
, ept_cfg
, maxpacket
);
546 if (usb_endpoint_dir_in(desc
)) {
548 ept_cfg
|= USBA_EPT_DIR_IN
;
551 switch (usb_endpoint_type(desc
)) {
552 case USB_ENDPOINT_XFER_CONTROL
:
553 ept_cfg
|= USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_CONTROL
);
554 ept_cfg
|= USBA_BF(BK_NUMBER
, USBA_BK_NUMBER_ONE
);
556 case USB_ENDPOINT_XFER_ISOC
:
558 DBG(DBG_ERR
, "ep_enable: %s is not isoc capable\n",
564 * Bits 11:12 specify number of _additional_
565 * transactions per microframe.
567 nr_trans
= ((usb_endpoint_maxp(desc
) >> 11) & 3) + 1;
572 ept_cfg
|= USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_ISO
);
575 * Do triple-buffering on high-bandwidth iso endpoints.
577 if (nr_trans
> 1 && ep
->nr_banks
== 3)
578 ept_cfg
|= USBA_BF(BK_NUMBER
, USBA_BK_NUMBER_TRIPLE
);
580 ept_cfg
|= USBA_BF(BK_NUMBER
, USBA_BK_NUMBER_DOUBLE
);
581 ept_cfg
|= USBA_BF(NB_TRANS
, nr_trans
);
583 case USB_ENDPOINT_XFER_BULK
:
584 ept_cfg
|= USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_BULK
);
585 ept_cfg
|= USBA_BF(BK_NUMBER
, USBA_BK_NUMBER_DOUBLE
);
587 case USB_ENDPOINT_XFER_INT
:
588 ept_cfg
|= USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_INT
);
589 ept_cfg
|= USBA_BF(BK_NUMBER
, USBA_BK_NUMBER_DOUBLE
);
593 spin_lock_irqsave(&ep
->udc
->lock
, flags
);
596 ep
->ep
.maxpacket
= maxpacket
;
598 usba_ep_writel(ep
, CFG
, ept_cfg
);
599 usba_ep_writel(ep
, CTL_ENB
, USBA_EPT_ENABLE
);
604 usba_writel(udc
, INT_ENB
,
605 (usba_readl(udc
, INT_ENB
)
606 | USBA_BF(EPT_INT
, 1 << ep
->index
)
607 | USBA_BF(DMA_INT
, 1 << ep
->index
)));
608 ctrl
= USBA_AUTO_VALID
| USBA_INTDIS_DMA
;
609 usba_ep_writel(ep
, CTL_ENB
, ctrl
);
611 usba_writel(udc
, INT_ENB
,
612 (usba_readl(udc
, INT_ENB
)
613 | USBA_BF(EPT_INT
, 1 << ep
->index
)));
616 spin_unlock_irqrestore(&udc
->lock
, flags
);
618 DBG(DBG_HW
, "EPT_CFG%d after init: %#08lx\n", ep
->index
,
619 (unsigned long)usba_ep_readl(ep
, CFG
));
620 DBG(DBG_HW
, "INT_ENB after init: %#08lx\n",
621 (unsigned long)usba_readl(udc
, INT_ENB
));
626 static int usba_ep_disable(struct usb_ep
*_ep
)
628 struct usba_ep
*ep
= to_usba_ep(_ep
);
629 struct usba_udc
*udc
= ep
->udc
;
633 DBG(DBG_GADGET
, "ep_disable: %s\n", ep
->ep
.name
);
635 spin_lock_irqsave(&udc
->lock
, flags
);
638 spin_unlock_irqrestore(&udc
->lock
, flags
);
639 /* REVISIT because this driver disables endpoints in
640 * reset_all_endpoints() before calling disconnect(),
641 * most gadget drivers would trigger this non-error ...
643 if (udc
->gadget
.speed
!= USB_SPEED_UNKNOWN
)
644 DBG(DBG_ERR
, "ep_disable: %s not enabled\n",
650 list_splice_init(&ep
->queue
, &req_list
);
652 usba_dma_writel(ep
, CONTROL
, 0);
653 usba_dma_writel(ep
, ADDRESS
, 0);
654 usba_dma_readl(ep
, STATUS
);
656 usba_ep_writel(ep
, CTL_DIS
, USBA_EPT_ENABLE
);
657 usba_writel(udc
, INT_ENB
,
658 usba_readl(udc
, INT_ENB
)
659 & ~USBA_BF(EPT_INT
, 1 << ep
->index
));
661 request_complete_list(ep
, &req_list
, -ESHUTDOWN
);
663 spin_unlock_irqrestore(&udc
->lock
, flags
);
668 static struct usb_request
*
669 usba_ep_alloc_request(struct usb_ep
*_ep
, gfp_t gfp_flags
)
671 struct usba_request
*req
;
673 DBG(DBG_GADGET
, "ep_alloc_request: %p, 0x%x\n", _ep
, gfp_flags
);
675 req
= kzalloc(sizeof(*req
), gfp_flags
);
679 INIT_LIST_HEAD(&req
->queue
);
685 usba_ep_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
687 struct usba_request
*req
= to_usba_req(_req
);
689 DBG(DBG_GADGET
, "ep_free_request: %p, %p\n", _ep
, _req
);
694 static int queue_dma(struct usba_udc
*udc
, struct usba_ep
*ep
,
695 struct usba_request
*req
, gfp_t gfp_flags
)
700 DBG(DBG_DMA
, "%s: req l/%u d/%08x %c%c%c\n",
701 ep
->ep
.name
, req
->req
.length
, req
->req
.dma
,
702 req
->req
.zero
? 'Z' : 'z',
703 req
->req
.short_not_ok
? 'S' : 's',
704 req
->req
.no_interrupt
? 'I' : 'i');
706 if (req
->req
.length
> 0x10000) {
707 /* Lengths from 0 to 65536 (inclusive) are supported */
708 DBG(DBG_ERR
, "invalid request length %u\n", req
->req
.length
);
712 ret
= usb_gadget_map_request(&udc
->gadget
, &req
->req
, ep
->is_in
);
717 req
->ctrl
= USBA_BF(DMA_BUF_LEN
, req
->req
.length
)
718 | USBA_DMA_CH_EN
| USBA_DMA_END_BUF_IE
719 | USBA_DMA_END_TR_EN
| USBA_DMA_END_TR_IE
;
722 req
->ctrl
|= USBA_DMA_END_BUF_EN
;
725 * Add this request to the queue and submit for DMA if
726 * possible. Check if we're still alive first -- we may have
727 * received a reset since last time we checked.
730 spin_lock_irqsave(&udc
->lock
, flags
);
732 if (list_empty(&ep
->queue
))
733 submit_request(ep
, req
);
735 list_add_tail(&req
->queue
, &ep
->queue
);
738 spin_unlock_irqrestore(&udc
->lock
, flags
);
744 usba_ep_queue(struct usb_ep
*_ep
, struct usb_request
*_req
, gfp_t gfp_flags
)
746 struct usba_request
*req
= to_usba_req(_req
);
747 struct usba_ep
*ep
= to_usba_ep(_ep
);
748 struct usba_udc
*udc
= ep
->udc
;
752 DBG(DBG_GADGET
| DBG_QUEUE
| DBG_REQ
, "%s: queue req %p, len %u\n",
753 ep
->ep
.name
, req
, _req
->length
);
755 if (!udc
->driver
|| udc
->gadget
.speed
== USB_SPEED_UNKNOWN
||
761 req
->last_transaction
= 0;
763 _req
->status
= -EINPROGRESS
;
767 return queue_dma(udc
, ep
, req
, gfp_flags
);
769 /* May have received a reset since last time we checked */
771 spin_lock_irqsave(&udc
->lock
, flags
);
773 list_add_tail(&req
->queue
, &ep
->queue
);
775 if ((!ep_is_control(ep
) && ep
->is_in
) ||
777 && (ep
->state
== DATA_STAGE_IN
778 || ep
->state
== STATUS_STAGE_IN
)))
779 usba_ep_writel(ep
, CTL_ENB
, USBA_TX_PK_RDY
);
781 usba_ep_writel(ep
, CTL_ENB
, USBA_RX_BK_RDY
);
784 spin_unlock_irqrestore(&udc
->lock
, flags
);
790 usba_update_req(struct usba_ep
*ep
, struct usba_request
*req
, u32 status
)
792 req
->req
.actual
= req
->req
.length
- USBA_BFEXT(DMA_BUF_LEN
, status
);
795 static int stop_dma(struct usba_ep
*ep
, u32
*pstatus
)
797 unsigned int timeout
;
801 * Stop the DMA controller. When writing both CH_EN
802 * and LINK to 0, the other bits are not affected.
804 usba_dma_writel(ep
, CONTROL
, 0);
806 /* Wait for the FIFO to empty */
807 for (timeout
= 40; timeout
; --timeout
) {
808 status
= usba_dma_readl(ep
, STATUS
);
809 if (!(status
& USBA_DMA_CH_EN
))
818 dev_err(&ep
->udc
->pdev
->dev
,
819 "%s: timed out waiting for DMA FIFO to empty\n",
827 static int usba_ep_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
829 struct usba_ep
*ep
= to_usba_ep(_ep
);
830 struct usba_udc
*udc
= ep
->udc
;
831 struct usba_request
*req
= to_usba_req(_req
);
835 DBG(DBG_GADGET
| DBG_QUEUE
, "ep_dequeue: %s, req %p\n",
838 spin_lock_irqsave(&udc
->lock
, flags
);
840 if (req
->using_dma
) {
842 * If this request is currently being transferred,
843 * stop the DMA controller and reset the FIFO.
845 if (ep
->queue
.next
== &req
->queue
) {
846 status
= usba_dma_readl(ep
, STATUS
);
847 if (status
& USBA_DMA_CH_EN
)
848 stop_dma(ep
, &status
);
850 #ifdef CONFIG_USB_GADGET_DEBUG_FS
851 ep
->last_dma_status
= status
;
854 usba_writel(udc
, EPT_RST
, 1 << ep
->index
);
856 usba_update_req(ep
, req
, status
);
861 * Errors should stop the queue from advancing until the
862 * completion function returns.
864 list_del_init(&req
->queue
);
866 request_complete(ep
, req
, -ECONNRESET
);
868 /* Process the next request if any */
869 submit_next_request(ep
);
870 spin_unlock_irqrestore(&udc
->lock
, flags
);
875 static int usba_ep_set_halt(struct usb_ep
*_ep
, int value
)
877 struct usba_ep
*ep
= to_usba_ep(_ep
);
878 struct usba_udc
*udc
= ep
->udc
;
882 DBG(DBG_GADGET
, "endpoint %s: %s HALT\n", ep
->ep
.name
,
883 value
? "set" : "clear");
886 DBG(DBG_ERR
, "Attempted to halt uninitialized ep %s\n",
891 DBG(DBG_ERR
, "Attempted to halt isochronous ep %s\n",
896 spin_lock_irqsave(&udc
->lock
, flags
);
899 * We can't halt IN endpoints while there are still data to be
902 if (!list_empty(&ep
->queue
)
903 || ((value
&& ep
->is_in
&& (usba_ep_readl(ep
, STA
)
904 & USBA_BF(BUSY_BANKS
, -1L))))) {
908 usba_ep_writel(ep
, SET_STA
, USBA_FORCE_STALL
);
910 usba_ep_writel(ep
, CLR_STA
,
911 USBA_FORCE_STALL
| USBA_TOGGLE_CLR
);
912 usba_ep_readl(ep
, STA
);
915 spin_unlock_irqrestore(&udc
->lock
, flags
);
920 static int usba_ep_fifo_status(struct usb_ep
*_ep
)
922 struct usba_ep
*ep
= to_usba_ep(_ep
);
924 return USBA_BFEXT(BYTE_COUNT
, usba_ep_readl(ep
, STA
));
927 static void usba_ep_fifo_flush(struct usb_ep
*_ep
)
929 struct usba_ep
*ep
= to_usba_ep(_ep
);
930 struct usba_udc
*udc
= ep
->udc
;
932 usba_writel(udc
, EPT_RST
, 1 << ep
->index
);
935 static const struct usb_ep_ops usba_ep_ops
= {
936 .enable
= usba_ep_enable
,
937 .disable
= usba_ep_disable
,
938 .alloc_request
= usba_ep_alloc_request
,
939 .free_request
= usba_ep_free_request
,
940 .queue
= usba_ep_queue
,
941 .dequeue
= usba_ep_dequeue
,
942 .set_halt
= usba_ep_set_halt
,
943 .fifo_status
= usba_ep_fifo_status
,
944 .fifo_flush
= usba_ep_fifo_flush
,
947 static int usba_udc_get_frame(struct usb_gadget
*gadget
)
949 struct usba_udc
*udc
= to_usba_udc(gadget
);
951 return USBA_BFEXT(FRAME_NUMBER
, usba_readl(udc
, FNUM
));
954 static int usba_udc_wakeup(struct usb_gadget
*gadget
)
956 struct usba_udc
*udc
= to_usba_udc(gadget
);
961 spin_lock_irqsave(&udc
->lock
, flags
);
962 if (udc
->devstatus
& (1 << USB_DEVICE_REMOTE_WAKEUP
)) {
963 ctrl
= usba_readl(udc
, CTRL
);
964 usba_writel(udc
, CTRL
, ctrl
| USBA_REMOTE_WAKE_UP
);
967 spin_unlock_irqrestore(&udc
->lock
, flags
);
973 usba_udc_set_selfpowered(struct usb_gadget
*gadget
, int is_selfpowered
)
975 struct usba_udc
*udc
= to_usba_udc(gadget
);
978 spin_lock_irqsave(&udc
->lock
, flags
);
980 udc
->devstatus
|= 1 << USB_DEVICE_SELF_POWERED
;
982 udc
->devstatus
&= ~(1 << USB_DEVICE_SELF_POWERED
);
983 spin_unlock_irqrestore(&udc
->lock
, flags
);
988 static int atmel_usba_start(struct usb_gadget
*gadget
,
989 struct usb_gadget_driver
*driver
);
990 static int atmel_usba_stop(struct usb_gadget
*gadget
,
991 struct usb_gadget_driver
*driver
);
992 static const struct usb_gadget_ops usba_udc_ops
= {
993 .get_frame
= usba_udc_get_frame
,
994 .wakeup
= usba_udc_wakeup
,
995 .set_selfpowered
= usba_udc_set_selfpowered
,
996 .udc_start
= atmel_usba_start
,
997 .udc_stop
= atmel_usba_stop
,
1000 static struct usb_endpoint_descriptor usba_ep0_desc
= {
1001 .bLength
= USB_DT_ENDPOINT_SIZE
,
1002 .bDescriptorType
= USB_DT_ENDPOINT
,
1003 .bEndpointAddress
= 0,
1004 .bmAttributes
= USB_ENDPOINT_XFER_CONTROL
,
1005 .wMaxPacketSize
= cpu_to_le16(64),
1006 /* FIXME: I have no idea what to put here */
1010 static void nop_release(struct device
*dev
)
1015 static struct usb_gadget usba_gadget_template
= {
1016 .ops
= &usba_udc_ops
,
1017 .max_speed
= USB_SPEED_HIGH
,
1018 .name
= "atmel_usba_udc",
1020 .init_name
= "gadget",
1021 .release
= nop_release
,
1026 * Called with interrupts disabled and udc->lock held.
1028 static void reset_all_endpoints(struct usba_udc
*udc
)
1031 struct usba_request
*req
, *tmp_req
;
1033 usba_writel(udc
, EPT_RST
, ~0UL);
1035 ep
= to_usba_ep(udc
->gadget
.ep0
);
1036 list_for_each_entry_safe(req
, tmp_req
, &ep
->queue
, queue
) {
1037 list_del_init(&req
->queue
);
1038 request_complete(ep
, req
, -ECONNRESET
);
1041 /* NOTE: normally, the next call to the gadget driver is in
1042 * charge of disabling endpoints... usually disconnect().
1043 * The exception would be entering a high speed test mode.
1045 * FIXME remove this code ... and retest thoroughly.
1047 list_for_each_entry(ep
, &udc
->gadget
.ep_list
, ep
.ep_list
) {
1049 spin_unlock(&udc
->lock
);
1050 usba_ep_disable(&ep
->ep
);
1051 spin_lock(&udc
->lock
);
1056 static struct usba_ep
*get_ep_by_addr(struct usba_udc
*udc
, u16 wIndex
)
1060 if ((wIndex
& USB_ENDPOINT_NUMBER_MASK
) == 0)
1061 return to_usba_ep(udc
->gadget
.ep0
);
1063 list_for_each_entry (ep
, &udc
->gadget
.ep_list
, ep
.ep_list
) {
1064 u8 bEndpointAddress
;
1068 bEndpointAddress
= ep
->ep
.desc
->bEndpointAddress
;
1069 if ((wIndex
^ bEndpointAddress
) & USB_DIR_IN
)
1071 if ((bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
)
1072 == (wIndex
& USB_ENDPOINT_NUMBER_MASK
))
1079 /* Called with interrupts disabled and udc->lock held */
1080 static inline void set_protocol_stall(struct usba_udc
*udc
, struct usba_ep
*ep
)
1082 usba_ep_writel(ep
, SET_STA
, USBA_FORCE_STALL
);
1083 ep
->state
= WAIT_FOR_SETUP
;
1086 static inline int is_stalled(struct usba_udc
*udc
, struct usba_ep
*ep
)
1088 if (usba_ep_readl(ep
, STA
) & USBA_FORCE_STALL
)
1093 static inline void set_address(struct usba_udc
*udc
, unsigned int addr
)
1097 DBG(DBG_BUS
, "setting address %u...\n", addr
);
1098 regval
= usba_readl(udc
, CTRL
);
1099 regval
= USBA_BFINS(DEV_ADDR
, addr
, regval
);
1100 usba_writel(udc
, CTRL
, regval
);
1103 static int do_test_mode(struct usba_udc
*udc
)
1105 static const char test_packet_buffer
[] = {
1107 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1109 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
1111 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
1112 /* JJJJJJJKKKKKKK * 8 */
1113 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1114 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1116 0x7F, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD,
1117 /* {JKKKKKKK * 10}, JK */
1118 0xFC, 0x7E, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD, 0x7E
1121 struct device
*dev
= &udc
->pdev
->dev
;
1124 test_mode
= udc
->test_mode
;
1126 /* Start from a clean slate */
1127 reset_all_endpoints(udc
);
1129 switch (test_mode
) {
1132 usba_writel(udc
, TST
, USBA_TST_J_MODE
);
1133 dev_info(dev
, "Entering Test_J mode...\n");
1137 usba_writel(udc
, TST
, USBA_TST_K_MODE
);
1138 dev_info(dev
, "Entering Test_K mode...\n");
1142 * Test_SE0_NAK: Force high-speed mode and set up ep0
1143 * for Bulk IN transfers
1145 ep
= &udc
->usba_ep
[0];
1146 usba_writel(udc
, TST
,
1147 USBA_BF(SPEED_CFG
, USBA_SPEED_CFG_FORCE_HIGH
));
1148 usba_ep_writel(ep
, CFG
,
1149 USBA_BF(EPT_SIZE
, USBA_EPT_SIZE_64
)
1151 | USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_BULK
)
1152 | USBA_BF(BK_NUMBER
, 1));
1153 if (!(usba_ep_readl(ep
, CFG
) & USBA_EPT_MAPPED
)) {
1154 set_protocol_stall(udc
, ep
);
1155 dev_err(dev
, "Test_SE0_NAK: ep0 not mapped\n");
1157 usba_ep_writel(ep
, CTL_ENB
, USBA_EPT_ENABLE
);
1158 dev_info(dev
, "Entering Test_SE0_NAK mode...\n");
1163 ep
= &udc
->usba_ep
[0];
1164 usba_ep_writel(ep
, CFG
,
1165 USBA_BF(EPT_SIZE
, USBA_EPT_SIZE_64
)
1167 | USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_BULK
)
1168 | USBA_BF(BK_NUMBER
, 1));
1169 if (!(usba_ep_readl(ep
, CFG
) & USBA_EPT_MAPPED
)) {
1170 set_protocol_stall(udc
, ep
);
1171 dev_err(dev
, "Test_Packet: ep0 not mapped\n");
1173 usba_ep_writel(ep
, CTL_ENB
, USBA_EPT_ENABLE
);
1174 usba_writel(udc
, TST
, USBA_TST_PKT_MODE
);
1175 memcpy_toio(ep
->fifo
, test_packet_buffer
,
1176 sizeof(test_packet_buffer
));
1177 usba_ep_writel(ep
, SET_STA
, USBA_TX_PK_RDY
);
1178 dev_info(dev
, "Entering Test_Packet mode...\n");
1182 dev_err(dev
, "Invalid test mode: 0x%04x\n", test_mode
);
1189 /* Avoid overly long expressions */
1190 static inline bool feature_is_dev_remote_wakeup(struct usb_ctrlrequest
*crq
)
1192 if (crq
->wValue
== cpu_to_le16(USB_DEVICE_REMOTE_WAKEUP
))
1197 static inline bool feature_is_dev_test_mode(struct usb_ctrlrequest
*crq
)
1199 if (crq
->wValue
== cpu_to_le16(USB_DEVICE_TEST_MODE
))
1204 static inline bool feature_is_ep_halt(struct usb_ctrlrequest
*crq
)
1206 if (crq
->wValue
== cpu_to_le16(USB_ENDPOINT_HALT
))
1211 static int handle_ep0_setup(struct usba_udc
*udc
, struct usba_ep
*ep
,
1212 struct usb_ctrlrequest
*crq
)
1216 switch (crq
->bRequest
) {
1217 case USB_REQ_GET_STATUS
: {
1220 if (crq
->bRequestType
== (USB_DIR_IN
| USB_RECIP_DEVICE
)) {
1221 status
= cpu_to_le16(udc
->devstatus
);
1222 } else if (crq
->bRequestType
1223 == (USB_DIR_IN
| USB_RECIP_INTERFACE
)) {
1224 status
= cpu_to_le16(0);
1225 } else if (crq
->bRequestType
1226 == (USB_DIR_IN
| USB_RECIP_ENDPOINT
)) {
1227 struct usba_ep
*target
;
1229 target
= get_ep_by_addr(udc
, le16_to_cpu(crq
->wIndex
));
1234 if (is_stalled(udc
, target
))
1235 status
|= cpu_to_le16(1);
1239 /* Write directly to the FIFO. No queueing is done. */
1240 if (crq
->wLength
!= cpu_to_le16(sizeof(status
)))
1242 ep
->state
= DATA_STAGE_IN
;
1243 __raw_writew(status
, ep
->fifo
);
1244 usba_ep_writel(ep
, SET_STA
, USBA_TX_PK_RDY
);
1248 case USB_REQ_CLEAR_FEATURE
: {
1249 if (crq
->bRequestType
== USB_RECIP_DEVICE
) {
1250 if (feature_is_dev_remote_wakeup(crq
))
1252 &= ~(1 << USB_DEVICE_REMOTE_WAKEUP
);
1254 /* Can't CLEAR_FEATURE TEST_MODE */
1256 } else if (crq
->bRequestType
== USB_RECIP_ENDPOINT
) {
1257 struct usba_ep
*target
;
1259 if (crq
->wLength
!= cpu_to_le16(0)
1260 || !feature_is_ep_halt(crq
))
1262 target
= get_ep_by_addr(udc
, le16_to_cpu(crq
->wIndex
));
1266 usba_ep_writel(target
, CLR_STA
, USBA_FORCE_STALL
);
1267 if (target
->index
!= 0)
1268 usba_ep_writel(target
, CLR_STA
,
1274 send_status(udc
, ep
);
1278 case USB_REQ_SET_FEATURE
: {
1279 if (crq
->bRequestType
== USB_RECIP_DEVICE
) {
1280 if (feature_is_dev_test_mode(crq
)) {
1281 send_status(udc
, ep
);
1282 ep
->state
= STATUS_STAGE_TEST
;
1283 udc
->test_mode
= le16_to_cpu(crq
->wIndex
);
1285 } else if (feature_is_dev_remote_wakeup(crq
)) {
1286 udc
->devstatus
|= 1 << USB_DEVICE_REMOTE_WAKEUP
;
1290 } else if (crq
->bRequestType
== USB_RECIP_ENDPOINT
) {
1291 struct usba_ep
*target
;
1293 if (crq
->wLength
!= cpu_to_le16(0)
1294 || !feature_is_ep_halt(crq
))
1297 target
= get_ep_by_addr(udc
, le16_to_cpu(crq
->wIndex
));
1301 usba_ep_writel(target
, SET_STA
, USBA_FORCE_STALL
);
1305 send_status(udc
, ep
);
1309 case USB_REQ_SET_ADDRESS
:
1310 if (crq
->bRequestType
!= (USB_DIR_OUT
| USB_RECIP_DEVICE
))
1313 set_address(udc
, le16_to_cpu(crq
->wValue
));
1314 send_status(udc
, ep
);
1315 ep
->state
= STATUS_STAGE_ADDR
;
1320 spin_unlock(&udc
->lock
);
1321 retval
= udc
->driver
->setup(&udc
->gadget
, crq
);
1322 spin_lock(&udc
->lock
);
1328 pr_err("udc: %s: Invalid setup request: %02x.%02x v%04x i%04x l%d, "
1329 "halting endpoint...\n",
1330 ep
->ep
.name
, crq
->bRequestType
, crq
->bRequest
,
1331 le16_to_cpu(crq
->wValue
), le16_to_cpu(crq
->wIndex
),
1332 le16_to_cpu(crq
->wLength
));
1333 set_protocol_stall(udc
, ep
);
1337 static void usba_control_irq(struct usba_udc
*udc
, struct usba_ep
*ep
)
1339 struct usba_request
*req
;
1344 epstatus
= usba_ep_readl(ep
, STA
);
1345 epctrl
= usba_ep_readl(ep
, CTL
);
1347 DBG(DBG_INT
, "%s [%d]: s/%08x c/%08x\n",
1348 ep
->ep
.name
, ep
->state
, epstatus
, epctrl
);
1351 if (!list_empty(&ep
->queue
))
1352 req
= list_entry(ep
->queue
.next
,
1353 struct usba_request
, queue
);
1355 if ((epctrl
& USBA_TX_PK_RDY
) && !(epstatus
& USBA_TX_PK_RDY
)) {
1357 next_fifo_transaction(ep
, req
);
1359 submit_request(ep
, req
);
1361 if (req
->last_transaction
) {
1362 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_PK_RDY
);
1363 usba_ep_writel(ep
, CTL_ENB
, USBA_TX_COMPLETE
);
1367 if ((epstatus
& epctrl
) & USBA_TX_COMPLETE
) {
1368 usba_ep_writel(ep
, CLR_STA
, USBA_TX_COMPLETE
);
1370 switch (ep
->state
) {
1372 usba_ep_writel(ep
, CTL_ENB
, USBA_RX_BK_RDY
);
1373 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_COMPLETE
);
1374 ep
->state
= STATUS_STAGE_OUT
;
1376 case STATUS_STAGE_ADDR
:
1377 /* Activate our new address */
1378 usba_writel(udc
, CTRL
, (usba_readl(udc
, CTRL
)
1380 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_COMPLETE
);
1381 ep
->state
= WAIT_FOR_SETUP
;
1383 case STATUS_STAGE_IN
:
1385 list_del_init(&req
->queue
);
1386 request_complete(ep
, req
, 0);
1387 submit_next_request(ep
);
1389 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_COMPLETE
);
1390 ep
->state
= WAIT_FOR_SETUP
;
1392 case STATUS_STAGE_TEST
:
1393 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_COMPLETE
);
1394 ep
->state
= WAIT_FOR_SETUP
;
1395 if (do_test_mode(udc
))
1396 set_protocol_stall(udc
, ep
);
1399 pr_err("udc: %s: TXCOMP: Invalid endpoint state %d, "
1400 "halting endpoint...\n",
1401 ep
->ep
.name
, ep
->state
);
1402 set_protocol_stall(udc
, ep
);
1408 if ((epstatus
& epctrl
) & USBA_RX_BK_RDY
) {
1409 switch (ep
->state
) {
1410 case STATUS_STAGE_OUT
:
1411 usba_ep_writel(ep
, CLR_STA
, USBA_RX_BK_RDY
);
1412 usba_ep_writel(ep
, CTL_DIS
, USBA_RX_BK_RDY
);
1415 list_del_init(&req
->queue
);
1416 request_complete(ep
, req
, 0);
1418 ep
->state
= WAIT_FOR_SETUP
;
1421 case DATA_STAGE_OUT
:
1426 usba_ep_writel(ep
, CLR_STA
, USBA_RX_BK_RDY
);
1427 usba_ep_writel(ep
, CTL_DIS
, USBA_RX_BK_RDY
);
1428 pr_err("udc: %s: RXRDY: Invalid endpoint state %d, "
1429 "halting endpoint...\n",
1430 ep
->ep
.name
, ep
->state
);
1431 set_protocol_stall(udc
, ep
);
1437 if (epstatus
& USBA_RX_SETUP
) {
1439 struct usb_ctrlrequest crq
;
1440 unsigned long data
[2];
1442 unsigned int pkt_len
;
1445 if (ep
->state
!= WAIT_FOR_SETUP
) {
1447 * Didn't expect a SETUP packet at this
1448 * point. Clean up any pending requests (which
1449 * may be successful).
1451 int status
= -EPROTO
;
1454 * RXRDY and TXCOMP are dropped when SETUP
1455 * packets arrive. Just pretend we received
1456 * the status packet.
1458 if (ep
->state
== STATUS_STAGE_OUT
1459 || ep
->state
== STATUS_STAGE_IN
) {
1460 usba_ep_writel(ep
, CTL_DIS
, USBA_RX_BK_RDY
);
1465 list_del_init(&req
->queue
);
1466 request_complete(ep
, req
, status
);
1470 pkt_len
= USBA_BFEXT(BYTE_COUNT
, usba_ep_readl(ep
, STA
));
1471 DBG(DBG_HW
, "Packet length: %u\n", pkt_len
);
1472 if (pkt_len
!= sizeof(crq
)) {
1473 pr_warning("udc: Invalid packet length %u "
1474 "(expected %zu)\n", pkt_len
, sizeof(crq
));
1475 set_protocol_stall(udc
, ep
);
1479 DBG(DBG_FIFO
, "Copying ctrl request from 0x%p:\n", ep
->fifo
);
1480 memcpy_fromio(crq
.data
, ep
->fifo
, sizeof(crq
));
1482 /* Free up one bank in the FIFO so that we can
1483 * generate or receive a reply right away. */
1484 usba_ep_writel(ep
, CLR_STA
, USBA_RX_SETUP
);
1486 /* printk(KERN_DEBUG "setup: %d: %02x.%02x\n",
1487 ep->state, crq.crq.bRequestType,
1488 crq.crq.bRequest); */
1490 if (crq
.crq
.bRequestType
& USB_DIR_IN
) {
1492 * The USB 2.0 spec states that "if wLength is
1493 * zero, there is no data transfer phase."
1494 * However, testusb #14 seems to actually
1495 * expect a data phase even if wLength = 0...
1497 ep
->state
= DATA_STAGE_IN
;
1499 if (crq
.crq
.wLength
!= cpu_to_le16(0))
1500 ep
->state
= DATA_STAGE_OUT
;
1502 ep
->state
= STATUS_STAGE_IN
;
1507 ret
= handle_ep0_setup(udc
, ep
, &crq
.crq
);
1509 spin_unlock(&udc
->lock
);
1510 ret
= udc
->driver
->setup(&udc
->gadget
, &crq
.crq
);
1511 spin_lock(&udc
->lock
);
1514 DBG(DBG_BUS
, "req %02x.%02x, length %d, state %d, ret %d\n",
1515 crq
.crq
.bRequestType
, crq
.crq
.bRequest
,
1516 le16_to_cpu(crq
.crq
.wLength
), ep
->state
, ret
);
1519 /* Let the host know that we failed */
1520 set_protocol_stall(udc
, ep
);
1525 static void usba_ep_irq(struct usba_udc
*udc
, struct usba_ep
*ep
)
1527 struct usba_request
*req
;
1531 epstatus
= usba_ep_readl(ep
, STA
);
1532 epctrl
= usba_ep_readl(ep
, CTL
);
1534 DBG(DBG_INT
, "%s: interrupt, status: 0x%08x\n", ep
->ep
.name
, epstatus
);
1536 while ((epctrl
& USBA_TX_PK_RDY
) && !(epstatus
& USBA_TX_PK_RDY
)) {
1537 DBG(DBG_BUS
, "%s: TX PK ready\n", ep
->ep
.name
);
1539 if (list_empty(&ep
->queue
)) {
1540 dev_warn(&udc
->pdev
->dev
, "ep_irq: queue empty\n");
1541 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_PK_RDY
);
1545 req
= list_entry(ep
->queue
.next
, struct usba_request
, queue
);
1547 if (req
->using_dma
) {
1548 /* Send a zero-length packet */
1549 usba_ep_writel(ep
, SET_STA
,
1551 usba_ep_writel(ep
, CTL_DIS
,
1553 list_del_init(&req
->queue
);
1554 submit_next_request(ep
);
1555 request_complete(ep
, req
, 0);
1558 next_fifo_transaction(ep
, req
);
1560 submit_request(ep
, req
);
1562 if (req
->last_transaction
) {
1563 list_del_init(&req
->queue
);
1564 submit_next_request(ep
);
1565 request_complete(ep
, req
, 0);
1569 epstatus
= usba_ep_readl(ep
, STA
);
1570 epctrl
= usba_ep_readl(ep
, CTL
);
1572 if ((epstatus
& epctrl
) & USBA_RX_BK_RDY
) {
1573 DBG(DBG_BUS
, "%s: RX data ready\n", ep
->ep
.name
);
1575 usba_ep_writel(ep
, CLR_STA
, USBA_RX_BK_RDY
);
1579 static void usba_dma_irq(struct usba_udc
*udc
, struct usba_ep
*ep
)
1581 struct usba_request
*req
;
1582 u32 status
, control
, pending
;
1584 status
= usba_dma_readl(ep
, STATUS
);
1585 control
= usba_dma_readl(ep
, CONTROL
);
1586 #ifdef CONFIG_USB_GADGET_DEBUG_FS
1587 ep
->last_dma_status
= status
;
1589 pending
= status
& control
;
1590 DBG(DBG_INT
| DBG_DMA
, "dma irq, s/%#08x, c/%#08x\n", status
, control
);
1592 if (status
& USBA_DMA_CH_EN
) {
1593 dev_err(&udc
->pdev
->dev
,
1594 "DMA_CH_EN is set after transfer is finished!\n");
1595 dev_err(&udc
->pdev
->dev
,
1596 "status=%#08x, pending=%#08x, control=%#08x\n",
1597 status
, pending
, control
);
1600 * try to pretend nothing happened. We might have to
1601 * do something here...
1605 if (list_empty(&ep
->queue
))
1606 /* Might happen if a reset comes along at the right moment */
1609 if (pending
& (USBA_DMA_END_TR_ST
| USBA_DMA_END_BUF_ST
)) {
1610 req
= list_entry(ep
->queue
.next
, struct usba_request
, queue
);
1611 usba_update_req(ep
, req
, status
);
1613 list_del_init(&req
->queue
);
1614 submit_next_request(ep
);
1615 request_complete(ep
, req
, 0);
1619 static irqreturn_t
usba_udc_irq(int irq
, void *devid
)
1621 struct usba_udc
*udc
= devid
;
1626 spin_lock(&udc
->lock
);
1628 status
= usba_readl(udc
, INT_STA
);
1629 DBG(DBG_INT
, "irq, status=%#08x\n", status
);
1631 if (status
& USBA_DET_SUSPEND
) {
1633 usba_writel(udc
, INT_CLR
, USBA_DET_SUSPEND
);
1634 DBG(DBG_BUS
, "Suspend detected\n");
1635 if (udc
->gadget
.speed
!= USB_SPEED_UNKNOWN
1636 && udc
->driver
&& udc
->driver
->suspend
) {
1637 spin_unlock(&udc
->lock
);
1638 udc
->driver
->suspend(&udc
->gadget
);
1639 spin_lock(&udc
->lock
);
1643 if (status
& USBA_WAKE_UP
) {
1645 usba_writel(udc
, INT_CLR
, USBA_WAKE_UP
);
1646 DBG(DBG_BUS
, "Wake Up CPU detected\n");
1649 if (status
& USBA_END_OF_RESUME
) {
1650 usba_writel(udc
, INT_CLR
, USBA_END_OF_RESUME
);
1651 DBG(DBG_BUS
, "Resume detected\n");
1652 if (udc
->gadget
.speed
!= USB_SPEED_UNKNOWN
1653 && udc
->driver
&& udc
->driver
->resume
) {
1654 spin_unlock(&udc
->lock
);
1655 udc
->driver
->resume(&udc
->gadget
);
1656 spin_lock(&udc
->lock
);
1660 dma_status
= USBA_BFEXT(DMA_INT
, status
);
1664 for (i
= 1; i
< USBA_NR_ENDPOINTS
; i
++)
1665 if (dma_status
& (1 << i
))
1666 usba_dma_irq(udc
, &udc
->usba_ep
[i
]);
1669 ep_status
= USBA_BFEXT(EPT_INT
, status
);
1673 for (i
= 0; i
< USBA_NR_ENDPOINTS
; i
++)
1674 if (ep_status
& (1 << i
)) {
1675 if (ep_is_control(&udc
->usba_ep
[i
]))
1676 usba_control_irq(udc
, &udc
->usba_ep
[i
]);
1678 usba_ep_irq(udc
, &udc
->usba_ep
[i
]);
1682 if (status
& USBA_END_OF_RESET
) {
1683 struct usba_ep
*ep0
;
1685 usba_writel(udc
, INT_CLR
, USBA_END_OF_RESET
);
1686 reset_all_endpoints(udc
);
1688 if (udc
->gadget
.speed
!= USB_SPEED_UNKNOWN
1689 && udc
->driver
->disconnect
) {
1690 udc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1691 spin_unlock(&udc
->lock
);
1692 udc
->driver
->disconnect(&udc
->gadget
);
1693 spin_lock(&udc
->lock
);
1696 if (status
& USBA_HIGH_SPEED
)
1697 udc
->gadget
.speed
= USB_SPEED_HIGH
;
1699 udc
->gadget
.speed
= USB_SPEED_FULL
;
1700 DBG(DBG_BUS
, "%s bus reset detected\n",
1701 usb_speed_string(udc
->gadget
.speed
));
1703 ep0
= &udc
->usba_ep
[0];
1704 ep0
->ep
.desc
= &usba_ep0_desc
;
1705 ep0
->state
= WAIT_FOR_SETUP
;
1706 usba_ep_writel(ep0
, CFG
,
1707 (USBA_BF(EPT_SIZE
, EP0_EPT_SIZE
)
1708 | USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_CONTROL
)
1709 | USBA_BF(BK_NUMBER
, USBA_BK_NUMBER_ONE
)));
1710 usba_ep_writel(ep0
, CTL_ENB
,
1711 USBA_EPT_ENABLE
| USBA_RX_SETUP
);
1712 usba_writel(udc
, INT_ENB
,
1713 (usba_readl(udc
, INT_ENB
)
1714 | USBA_BF(EPT_INT
, 1)
1716 | USBA_END_OF_RESUME
));
1719 * Unclear why we hit this irregularly, e.g. in usbtest,
1720 * but it's clearly harmless...
1722 if (!(usba_ep_readl(ep0
, CFG
) & USBA_EPT_MAPPED
))
1723 dev_dbg(&udc
->pdev
->dev
,
1724 "ODD: EP0 configuration is invalid!\n");
1727 spin_unlock(&udc
->lock
);
1732 static irqreturn_t
usba_vbus_irq(int irq
, void *devid
)
1734 struct usba_udc
*udc
= devid
;
1740 spin_lock(&udc
->lock
);
1742 /* May happen if Vbus pin toggles during probe() */
1746 vbus
= vbus_is_present(udc
);
1747 if (vbus
!= udc
->vbus_prev
) {
1750 usba_writel(udc
, CTRL
, USBA_ENABLE_MASK
);
1751 usba_writel(udc
, INT_ENB
, USBA_END_OF_RESET
);
1753 udc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1754 reset_all_endpoints(udc
);
1756 usba_writel(udc
, CTRL
, USBA_DISABLE_MASK
);
1757 if (udc
->driver
->disconnect
) {
1758 spin_unlock(&udc
->lock
);
1759 udc
->driver
->disconnect(&udc
->gadget
);
1760 spin_lock(&udc
->lock
);
1763 udc
->vbus_prev
= vbus
;
1767 spin_unlock(&udc
->lock
);
1772 static int atmel_usba_start(struct usb_gadget
*gadget
,
1773 struct usb_gadget_driver
*driver
)
1776 struct usba_udc
*udc
= container_of(gadget
, struct usba_udc
, gadget
);
1777 unsigned long flags
;
1779 spin_lock_irqsave(&udc
->lock
, flags
);
1781 udc
->devstatus
= 1 << USB_DEVICE_SELF_POWERED
;
1782 udc
->driver
= driver
;
1783 spin_unlock_irqrestore(&udc
->lock
, flags
);
1785 ret
= clk_prepare_enable(udc
->pclk
);
1788 ret
= clk_prepare_enable(udc
->hclk
);
1790 clk_disable_unprepare(udc
->pclk
);
1794 DBG(DBG_GADGET
, "registered driver `%s'\n", driver
->driver
.name
);
1797 if (gpio_is_valid(udc
->vbus_pin
))
1798 enable_irq(gpio_to_irq(udc
->vbus_pin
));
1800 /* If Vbus is present, enable the controller and wait for reset */
1801 spin_lock_irqsave(&udc
->lock
, flags
);
1802 if (vbus_is_present(udc
) && udc
->vbus_prev
== 0) {
1804 usba_writel(udc
, CTRL
, USBA_ENABLE_MASK
);
1805 usba_writel(udc
, INT_ENB
, USBA_END_OF_RESET
);
1807 spin_unlock_irqrestore(&udc
->lock
, flags
);
1812 static int atmel_usba_stop(struct usb_gadget
*gadget
,
1813 struct usb_gadget_driver
*driver
)
1815 struct usba_udc
*udc
= container_of(gadget
, struct usba_udc
, gadget
);
1816 unsigned long flags
;
1818 if (gpio_is_valid(udc
->vbus_pin
))
1819 disable_irq(gpio_to_irq(udc
->vbus_pin
));
1821 spin_lock_irqsave(&udc
->lock
, flags
);
1822 udc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1823 reset_all_endpoints(udc
);
1824 spin_unlock_irqrestore(&udc
->lock
, flags
);
1826 /* This will also disable the DP pullup */
1828 usba_writel(udc
, CTRL
, USBA_DISABLE_MASK
);
1832 clk_disable_unprepare(udc
->hclk
);
1833 clk_disable_unprepare(udc
->pclk
);
1835 DBG(DBG_GADGET
, "unregistered driver `%s'\n", driver
->driver
.name
);
1841 static struct usba_ep
* atmel_udc_of_init(struct platform_device
*pdev
,
1842 struct usba_udc
*udc
)
1846 enum of_gpio_flags flags
;
1847 struct device_node
*np
= pdev
->dev
.of_node
;
1848 struct device_node
*pp
;
1850 struct usba_ep
*eps
, *ep
;
1854 udc
->vbus_pin
= of_get_named_gpio_flags(np
, "atmel,vbus-gpio", 0,
1856 udc
->vbus_pin_inverted
= (flags
& OF_GPIO_ACTIVE_LOW
) ? 1 : 0;
1859 while ((pp
= of_get_next_child(np
, pp
)))
1862 eps
= devm_kzalloc(&pdev
->dev
, sizeof(struct usba_ep
) * udc
->num_ep
,
1865 return ERR_PTR(-ENOMEM
);
1867 udc
->gadget
.ep0
= &eps
[0].ep
;
1869 INIT_LIST_HEAD(&eps
[0].ep
.ep_list
);
1873 while ((pp
= of_get_next_child(np
, pp
))) {
1876 ret
= of_property_read_u32(pp
, "reg", &val
);
1878 dev_err(&pdev
->dev
, "of_probe: reg error(%d)\n", ret
);
1883 ret
= of_property_read_u32(pp
, "atmel,fifo-size", &val
);
1885 dev_err(&pdev
->dev
, "of_probe: fifo-size error(%d)\n", ret
);
1888 ep
->fifo_size
= val
;
1890 ret
= of_property_read_u32(pp
, "atmel,nb-banks", &val
);
1892 dev_err(&pdev
->dev
, "of_probe: nb-banks error(%d)\n", ret
);
1897 ep
->can_dma
= of_property_read_bool(pp
, "atmel,can-dma");
1898 ep
->can_isoc
= of_property_read_bool(pp
, "atmel,can-isoc");
1900 ret
= of_property_read_string(pp
, "name", &name
);
1903 ep
->ep_regs
= udc
->regs
+ USBA_EPT_BASE(i
);
1904 ep
->dma_regs
= udc
->regs
+ USBA_DMA_BASE(i
);
1905 ep
->fifo
= udc
->fifo
+ USBA_FIFO_BASE(i
);
1906 ep
->ep
.ops
= &usba_ep_ops
;
1907 usb_ep_set_maxpacket_limit(&ep
->ep
, ep
->fifo_size
);
1909 INIT_LIST_HEAD(&ep
->queue
);
1912 list_add_tail(&ep
->ep
.ep_list
, &udc
->gadget
.ep_list
);
1919 return ERR_PTR(ret
);
1922 static struct usba_ep
* atmel_udc_of_init(struct platform_device
*pdev
,
1923 struct usba_udc
*udc
)
1925 return ERR_PTR(-ENOSYS
);
1929 static struct usba_ep
* usba_udc_pdata(struct platform_device
*pdev
,
1930 struct usba_udc
*udc
)
1932 struct usba_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
1933 struct usba_ep
*eps
;
1937 return ERR_PTR(-ENXIO
);
1939 eps
= devm_kzalloc(&pdev
->dev
, sizeof(struct usba_ep
) * pdata
->num_ep
,
1942 return ERR_PTR(-ENOMEM
);
1944 udc
->gadget
.ep0
= &eps
[0].ep
;
1946 udc
->vbus_pin
= pdata
->vbus_pin
;
1947 udc
->vbus_pin_inverted
= pdata
->vbus_pin_inverted
;
1948 udc
->num_ep
= pdata
->num_ep
;
1950 INIT_LIST_HEAD(&eps
[0].ep
.ep_list
);
1952 for (i
= 0; i
< pdata
->num_ep
; i
++) {
1953 struct usba_ep
*ep
= &eps
[i
];
1955 ep
->ep_regs
= udc
->regs
+ USBA_EPT_BASE(i
);
1956 ep
->dma_regs
= udc
->regs
+ USBA_DMA_BASE(i
);
1957 ep
->fifo
= udc
->fifo
+ USBA_FIFO_BASE(i
);
1958 ep
->ep
.ops
= &usba_ep_ops
;
1959 ep
->ep
.name
= pdata
->ep
[i
].name
;
1960 ep
->fifo_size
= pdata
->ep
[i
].fifo_size
;
1961 usb_ep_set_maxpacket_limit(&ep
->ep
, ep
->fifo_size
);
1963 INIT_LIST_HEAD(&ep
->queue
);
1964 ep
->nr_banks
= pdata
->ep
[i
].nr_banks
;
1965 ep
->index
= pdata
->ep
[i
].index
;
1966 ep
->can_dma
= pdata
->ep
[i
].can_dma
;
1967 ep
->can_isoc
= pdata
->ep
[i
].can_isoc
;
1970 list_add_tail(&ep
->ep
.ep_list
, &udc
->gadget
.ep_list
);
1976 static int __init
usba_udc_probe(struct platform_device
*pdev
)
1978 struct resource
*regs
, *fifo
;
1979 struct clk
*pclk
, *hclk
;
1980 struct usba_udc
*udc
;
1983 udc
= devm_kzalloc(&pdev
->dev
, sizeof(*udc
), GFP_KERNEL
);
1987 udc
->gadget
= usba_gadget_template
;
1988 INIT_LIST_HEAD(&udc
->gadget
.ep_list
);
1990 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, CTRL_IOMEM_ID
);
1991 fifo
= platform_get_resource(pdev
, IORESOURCE_MEM
, FIFO_IOMEM_ID
);
1995 irq
= platform_get_irq(pdev
, 0);
1999 pclk
= devm_clk_get(&pdev
->dev
, "pclk");
2001 return PTR_ERR(pclk
);
2002 hclk
= devm_clk_get(&pdev
->dev
, "hclk");
2004 return PTR_ERR(hclk
);
2006 spin_lock_init(&udc
->lock
);
2010 udc
->vbus_pin
= -ENODEV
;
2013 udc
->regs
= devm_ioremap(&pdev
->dev
, regs
->start
, resource_size(regs
));
2015 dev_err(&pdev
->dev
, "Unable to map I/O memory, aborting.\n");
2018 dev_info(&pdev
->dev
, "MMIO registers at 0x%08lx mapped at %p\n",
2019 (unsigned long)regs
->start
, udc
->regs
);
2020 udc
->fifo
= devm_ioremap(&pdev
->dev
, fifo
->start
, resource_size(fifo
));
2022 dev_err(&pdev
->dev
, "Unable to map FIFO, aborting.\n");
2025 dev_info(&pdev
->dev
, "FIFO at 0x%08lx mapped at %p\n",
2026 (unsigned long)fifo
->start
, udc
->fifo
);
2028 platform_set_drvdata(pdev
, udc
);
2030 /* Make sure we start from a clean slate */
2031 ret
= clk_prepare_enable(pclk
);
2033 dev_err(&pdev
->dev
, "Unable to enable pclk, aborting.\n");
2037 usba_writel(udc
, CTRL
, USBA_DISABLE_MASK
);
2038 clk_disable_unprepare(pclk
);
2040 if (pdev
->dev
.of_node
)
2041 udc
->usba_ep
= atmel_udc_of_init(pdev
, udc
);
2043 udc
->usba_ep
= usba_udc_pdata(pdev
, udc
);
2045 if (IS_ERR(udc
->usba_ep
))
2046 return PTR_ERR(udc
->usba_ep
);
2048 ret
= devm_request_irq(&pdev
->dev
, irq
, usba_udc_irq
, 0,
2049 "atmel_usba_udc", udc
);
2051 dev_err(&pdev
->dev
, "Cannot request irq %d (error %d)\n",
2057 if (gpio_is_valid(udc
->vbus_pin
)) {
2058 if (!devm_gpio_request(&pdev
->dev
, udc
->vbus_pin
, "atmel_usba_udc")) {
2059 ret
= devm_request_irq(&pdev
->dev
,
2060 gpio_to_irq(udc
->vbus_pin
),
2062 "atmel_usba_udc", udc
);
2064 udc
->vbus_pin
= -ENODEV
;
2065 dev_warn(&udc
->pdev
->dev
,
2066 "failed to request vbus irq; "
2067 "assuming always on\n");
2069 disable_irq(gpio_to_irq(udc
->vbus_pin
));
2072 /* gpio_request fail so use -EINVAL for gpio_is_valid */
2073 udc
->vbus_pin
= -EINVAL
;
2077 ret
= usb_add_gadget_udc(&pdev
->dev
, &udc
->gadget
);
2081 usba_init_debugfs(udc
);
2082 for (i
= 1; i
< udc
->num_ep
; i
++)
2083 usba_ep_init_debugfs(udc
, &udc
->usba_ep
[i
]);
2088 static int __exit
usba_udc_remove(struct platform_device
*pdev
)
2090 struct usba_udc
*udc
;
2093 udc
= platform_get_drvdata(pdev
);
2095 usb_del_gadget_udc(&udc
->gadget
);
2097 for (i
= 1; i
< udc
->num_ep
; i
++)
2098 usba_ep_cleanup_debugfs(&udc
->usba_ep
[i
]);
2099 usba_cleanup_debugfs(udc
);
2104 #if defined(CONFIG_OF)
2105 static const struct of_device_id atmel_udc_dt_ids
[] = {
2106 { .compatible
= "atmel,at91sam9rl-udc" },
2110 MODULE_DEVICE_TABLE(of
, atmel_udc_dt_ids
);
2113 static struct platform_driver udc_driver
= {
2114 .remove
= __exit_p(usba_udc_remove
),
2116 .name
= "atmel_usba_udc",
2117 .owner
= THIS_MODULE
,
2118 .of_match_table
= of_match_ptr(atmel_udc_dt_ids
),
2122 module_platform_driver_probe(udc_driver
, usba_udc_probe
);
2124 MODULE_DESCRIPTION("Atmel USBA UDC driver");
2125 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
2126 MODULE_LICENSE("GPL");
2127 MODULE_ALIAS("platform:atmel_usba_udc");