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/clk/at91_pmc.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/interrupt.h>
16 #include <linux/slab.h>
17 #include <linux/device.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/list.h>
20 #include <linux/mfd/syscon.h>
21 #include <linux/platform_device.h>
22 #include <linux/regmap.h>
23 #include <linux/ctype.h>
24 #include <linux/usb/ch9.h>
25 #include <linux/usb/gadget.h>
26 #include <linux/usb/atmel_usba_udc.h>
27 #include <linux/delay.h>
29 #include <linux/of_gpio.h>
31 #include "atmel_usba_udc.h"
33 #ifdef CONFIG_USB_GADGET_DEBUG_FS
34 #include <linux/debugfs.h>
35 #include <linux/uaccess.h>
37 static int queue_dbg_open(struct inode
*inode
, struct file
*file
)
39 struct usba_ep
*ep
= inode
->i_private
;
40 struct usba_request
*req
, *req_copy
;
41 struct list_head
*queue_data
;
43 queue_data
= kmalloc(sizeof(*queue_data
), GFP_KERNEL
);
46 INIT_LIST_HEAD(queue_data
);
48 spin_lock_irq(&ep
->udc
->lock
);
49 list_for_each_entry(req
, &ep
->queue
, queue
) {
50 req_copy
= kmemdup(req
, sizeof(*req_copy
), GFP_ATOMIC
);
53 list_add_tail(&req_copy
->queue
, queue_data
);
55 spin_unlock_irq(&ep
->udc
->lock
);
57 file
->private_data
= queue_data
;
61 spin_unlock_irq(&ep
->udc
->lock
);
62 list_for_each_entry_safe(req
, req_copy
, queue_data
, queue
) {
63 list_del(&req
->queue
);
71 * bbbbbbbb llllllll IZS sssss nnnn FDL\n\0
75 * I/i: interrupt/no interrupt
77 * S/s: short ok/short not ok
80 * F/f: submitted/not submitted to FIFO
81 * D/d: using/not using DMA
82 * L/l: last transaction/not last transaction
84 static ssize_t
queue_dbg_read(struct file
*file
, char __user
*buf
,
85 size_t nbytes
, loff_t
*ppos
)
87 struct list_head
*queue
= file
->private_data
;
88 struct usba_request
*req
, *tmp_req
;
89 size_t len
, remaining
, actual
= 0;
92 if (!access_ok(VERIFY_WRITE
, buf
, nbytes
))
95 inode_lock(file_inode(file
));
96 list_for_each_entry_safe(req
, tmp_req
, queue
, queue
) {
97 len
= snprintf(tmpbuf
, sizeof(tmpbuf
),
98 "%8p %08x %c%c%c %5d %c%c%c\n",
99 req
->req
.buf
, req
->req
.length
,
100 req
->req
.no_interrupt
? 'i' : 'I',
101 req
->req
.zero
? 'Z' : 'z',
102 req
->req
.short_not_ok
? 's' : 'S',
104 req
->submitted
? 'F' : 'f',
105 req
->using_dma
? 'D' : 'd',
106 req
->last_transaction
? 'L' : 'l');
107 len
= min(len
, sizeof(tmpbuf
));
111 list_del(&req
->queue
);
114 remaining
= __copy_to_user(buf
, tmpbuf
, len
);
115 actual
+= len
- remaining
;
122 inode_unlock(file_inode(file
));
127 static int queue_dbg_release(struct inode
*inode
, struct file
*file
)
129 struct list_head
*queue_data
= file
->private_data
;
130 struct usba_request
*req
, *tmp_req
;
132 list_for_each_entry_safe(req
, tmp_req
, queue_data
, queue
) {
133 list_del(&req
->queue
);
140 static int regs_dbg_open(struct inode
*inode
, struct file
*file
)
142 struct usba_udc
*udc
;
148 udc
= inode
->i_private
;
149 data
= kmalloc(inode
->i_size
, GFP_KERNEL
);
153 spin_lock_irq(&udc
->lock
);
154 for (i
= 0; i
< inode
->i_size
/ 4; i
++)
155 data
[i
] = usba_io_readl(udc
->regs
+ i
* 4);
156 spin_unlock_irq(&udc
->lock
);
158 file
->private_data
= data
;
167 static ssize_t
regs_dbg_read(struct file
*file
, char __user
*buf
,
168 size_t nbytes
, loff_t
*ppos
)
170 struct inode
*inode
= file_inode(file
);
174 ret
= simple_read_from_buffer(buf
, nbytes
, ppos
,
176 file_inode(file
)->i_size
);
182 static int regs_dbg_release(struct inode
*inode
, struct file
*file
)
184 kfree(file
->private_data
);
188 const struct file_operations queue_dbg_fops
= {
189 .owner
= THIS_MODULE
,
190 .open
= queue_dbg_open
,
192 .read
= queue_dbg_read
,
193 .release
= queue_dbg_release
,
196 const struct file_operations regs_dbg_fops
= {
197 .owner
= THIS_MODULE
,
198 .open
= regs_dbg_open
,
199 .llseek
= generic_file_llseek
,
200 .read
= regs_dbg_read
,
201 .release
= regs_dbg_release
,
204 static void usba_ep_init_debugfs(struct usba_udc
*udc
,
207 struct dentry
*ep_root
;
209 ep_root
= debugfs_create_dir(ep
->ep
.name
, udc
->debugfs_root
);
212 ep
->debugfs_dir
= ep_root
;
214 ep
->debugfs_queue
= debugfs_create_file("queue", 0400, ep_root
,
215 ep
, &queue_dbg_fops
);
216 if (!ep
->debugfs_queue
)
220 ep
->debugfs_dma_status
221 = debugfs_create_u32("dma_status", 0400, ep_root
,
222 &ep
->last_dma_status
);
223 if (!ep
->debugfs_dma_status
)
226 if (ep_is_control(ep
)) {
228 = debugfs_create_u32("state", 0400, ep_root
,
230 if (!ep
->debugfs_state
)
238 debugfs_remove(ep
->debugfs_dma_status
);
240 debugfs_remove(ep
->debugfs_queue
);
242 debugfs_remove(ep_root
);
244 dev_err(&ep
->udc
->pdev
->dev
,
245 "failed to create debugfs directory for %s\n", ep
->ep
.name
);
248 static void usba_ep_cleanup_debugfs(struct usba_ep
*ep
)
250 debugfs_remove(ep
->debugfs_queue
);
251 debugfs_remove(ep
->debugfs_dma_status
);
252 debugfs_remove(ep
->debugfs_state
);
253 debugfs_remove(ep
->debugfs_dir
);
254 ep
->debugfs_dma_status
= NULL
;
255 ep
->debugfs_dir
= NULL
;
258 static void usba_init_debugfs(struct usba_udc
*udc
)
260 struct dentry
*root
, *regs
;
261 struct resource
*regs_resource
;
263 root
= debugfs_create_dir(udc
->gadget
.name
, NULL
);
264 if (IS_ERR(root
) || !root
)
266 udc
->debugfs_root
= root
;
268 regs_resource
= platform_get_resource(udc
->pdev
, IORESOURCE_MEM
,
272 regs
= debugfs_create_file_size("regs", 0400, root
, udc
,
274 resource_size(regs_resource
));
277 udc
->debugfs_regs
= regs
;
280 usba_ep_init_debugfs(udc
, to_usba_ep(udc
->gadget
.ep0
));
285 debugfs_remove(root
);
287 udc
->debugfs_root
= NULL
;
288 dev_err(&udc
->pdev
->dev
, "debugfs is not available\n");
291 static void usba_cleanup_debugfs(struct usba_udc
*udc
)
293 usba_ep_cleanup_debugfs(to_usba_ep(udc
->gadget
.ep0
));
294 debugfs_remove(udc
->debugfs_regs
);
295 debugfs_remove(udc
->debugfs_root
);
296 udc
->debugfs_regs
= NULL
;
297 udc
->debugfs_root
= NULL
;
300 static inline void usba_ep_init_debugfs(struct usba_udc
*udc
,
306 static inline void usba_ep_cleanup_debugfs(struct usba_ep
*ep
)
311 static inline void usba_init_debugfs(struct usba_udc
*udc
)
316 static inline void usba_cleanup_debugfs(struct usba_udc
*udc
)
322 static ushort fifo_mode
;
324 /* "modprobe ... fifo_mode=1" etc */
325 module_param(fifo_mode
, ushort
, 0x0);
326 MODULE_PARM_DESC(fifo_mode
, "Endpoint configuration mode");
328 /* mode 0 - uses autoconfig */
330 /* mode 1 - fits in 8KB, generic max fifo configuration */
331 static struct usba_fifo_cfg mode_1_cfg
[] = {
332 { .hw_ep_num
= 0, .fifo_size
= 64, .nr_banks
= 1, },
333 { .hw_ep_num
= 1, .fifo_size
= 1024, .nr_banks
= 2, },
334 { .hw_ep_num
= 2, .fifo_size
= 1024, .nr_banks
= 1, },
335 { .hw_ep_num
= 3, .fifo_size
= 1024, .nr_banks
= 1, },
336 { .hw_ep_num
= 4, .fifo_size
= 1024, .nr_banks
= 1, },
337 { .hw_ep_num
= 5, .fifo_size
= 1024, .nr_banks
= 1, },
338 { .hw_ep_num
= 6, .fifo_size
= 1024, .nr_banks
= 1, },
341 /* mode 2 - fits in 8KB, performance max fifo configuration */
342 static struct usba_fifo_cfg mode_2_cfg
[] = {
343 { .hw_ep_num
= 0, .fifo_size
= 64, .nr_banks
= 1, },
344 { .hw_ep_num
= 1, .fifo_size
= 1024, .nr_banks
= 3, },
345 { .hw_ep_num
= 2, .fifo_size
= 1024, .nr_banks
= 2, },
346 { .hw_ep_num
= 3, .fifo_size
= 1024, .nr_banks
= 2, },
349 /* mode 3 - fits in 8KB, mixed fifo configuration */
350 static struct usba_fifo_cfg mode_3_cfg
[] = {
351 { .hw_ep_num
= 0, .fifo_size
= 64, .nr_banks
= 1, },
352 { .hw_ep_num
= 1, .fifo_size
= 1024, .nr_banks
= 2, },
353 { .hw_ep_num
= 2, .fifo_size
= 512, .nr_banks
= 2, },
354 { .hw_ep_num
= 3, .fifo_size
= 512, .nr_banks
= 2, },
355 { .hw_ep_num
= 4, .fifo_size
= 512, .nr_banks
= 2, },
356 { .hw_ep_num
= 5, .fifo_size
= 512, .nr_banks
= 2, },
357 { .hw_ep_num
= 6, .fifo_size
= 512, .nr_banks
= 2, },
360 /* mode 4 - fits in 8KB, custom fifo configuration */
361 static struct usba_fifo_cfg mode_4_cfg
[] = {
362 { .hw_ep_num
= 0, .fifo_size
= 64, .nr_banks
= 1, },
363 { .hw_ep_num
= 1, .fifo_size
= 512, .nr_banks
= 2, },
364 { .hw_ep_num
= 2, .fifo_size
= 512, .nr_banks
= 2, },
365 { .hw_ep_num
= 3, .fifo_size
= 8, .nr_banks
= 2, },
366 { .hw_ep_num
= 4, .fifo_size
= 512, .nr_banks
= 2, },
367 { .hw_ep_num
= 5, .fifo_size
= 512, .nr_banks
= 2, },
368 { .hw_ep_num
= 6, .fifo_size
= 16, .nr_banks
= 2, },
369 { .hw_ep_num
= 7, .fifo_size
= 8, .nr_banks
= 2, },
370 { .hw_ep_num
= 8, .fifo_size
= 8, .nr_banks
= 2, },
372 /* Add additional configurations here */
374 int usba_config_fifo_table(struct usba_udc
*udc
)
382 udc
->fifo_cfg
= NULL
;
386 udc
->fifo_cfg
= mode_1_cfg
;
387 n
= ARRAY_SIZE(mode_1_cfg
);
390 udc
->fifo_cfg
= mode_2_cfg
;
391 n
= ARRAY_SIZE(mode_2_cfg
);
394 udc
->fifo_cfg
= mode_3_cfg
;
395 n
= ARRAY_SIZE(mode_3_cfg
);
398 udc
->fifo_cfg
= mode_4_cfg
;
399 n
= ARRAY_SIZE(mode_4_cfg
);
402 DBG(DBG_HW
, "Setup fifo_mode %d\n", fifo_mode
);
407 static inline u32
usba_int_enb_get(struct usba_udc
*udc
)
409 return udc
->int_enb_cache
;
412 static inline void usba_int_enb_set(struct usba_udc
*udc
, u32 val
)
414 usba_writel(udc
, INT_ENB
, val
);
415 udc
->int_enb_cache
= val
;
418 static int vbus_is_present(struct usba_udc
*udc
)
420 if (gpio_is_valid(udc
->vbus_pin
))
421 return gpio_get_value(udc
->vbus_pin
) ^ udc
->vbus_pin_inverted
;
423 /* No Vbus detection: Assume always present */
427 static void toggle_bias(struct usba_udc
*udc
, int is_on
)
429 if (udc
->errata
&& udc
->errata
->toggle_bias
)
430 udc
->errata
->toggle_bias(udc
, is_on
);
433 static void generate_bias_pulse(struct usba_udc
*udc
)
435 if (!udc
->bias_pulse_needed
)
438 if (udc
->errata
&& udc
->errata
->pulse_bias
)
439 udc
->errata
->pulse_bias(udc
);
441 udc
->bias_pulse_needed
= false;
444 static void next_fifo_transaction(struct usba_ep
*ep
, struct usba_request
*req
)
446 unsigned int transaction_len
;
448 transaction_len
= req
->req
.length
- req
->req
.actual
;
449 req
->last_transaction
= 1;
450 if (transaction_len
> ep
->ep
.maxpacket
) {
451 transaction_len
= ep
->ep
.maxpacket
;
452 req
->last_transaction
= 0;
453 } else if (transaction_len
== ep
->ep
.maxpacket
&& req
->req
.zero
)
454 req
->last_transaction
= 0;
456 DBG(DBG_QUEUE
, "%s: submit_transaction, req %p (length %d)%s\n",
457 ep
->ep
.name
, req
, transaction_len
,
458 req
->last_transaction
? ", done" : "");
460 memcpy_toio(ep
->fifo
, req
->req
.buf
+ req
->req
.actual
, transaction_len
);
461 usba_ep_writel(ep
, SET_STA
, USBA_TX_PK_RDY
);
462 req
->req
.actual
+= transaction_len
;
465 static void submit_request(struct usba_ep
*ep
, struct usba_request
*req
)
467 DBG(DBG_QUEUE
, "%s: submit_request: req %p (length %d)\n",
468 ep
->ep
.name
, req
, req
->req
.length
);
473 if (req
->using_dma
) {
474 if (req
->req
.length
== 0) {
475 usba_ep_writel(ep
, CTL_ENB
, USBA_TX_PK_RDY
);
480 usba_ep_writel(ep
, CTL_ENB
, USBA_SHORT_PACKET
);
482 usba_ep_writel(ep
, CTL_DIS
, USBA_SHORT_PACKET
);
484 usba_dma_writel(ep
, ADDRESS
, req
->req
.dma
);
485 usba_dma_writel(ep
, CONTROL
, req
->ctrl
);
487 next_fifo_transaction(ep
, req
);
488 if (req
->last_transaction
) {
489 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_PK_RDY
);
490 usba_ep_writel(ep
, CTL_ENB
, USBA_TX_COMPLETE
);
492 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_COMPLETE
);
493 usba_ep_writel(ep
, CTL_ENB
, USBA_TX_PK_RDY
);
498 static void submit_next_request(struct usba_ep
*ep
)
500 struct usba_request
*req
;
502 if (list_empty(&ep
->queue
)) {
503 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_PK_RDY
| USBA_RX_BK_RDY
);
507 req
= list_entry(ep
->queue
.next
, struct usba_request
, queue
);
509 submit_request(ep
, req
);
512 static void send_status(struct usba_udc
*udc
, struct usba_ep
*ep
)
514 ep
->state
= STATUS_STAGE_IN
;
515 usba_ep_writel(ep
, SET_STA
, USBA_TX_PK_RDY
);
516 usba_ep_writel(ep
, CTL_ENB
, USBA_TX_COMPLETE
);
519 static void receive_data(struct usba_ep
*ep
)
521 struct usba_udc
*udc
= ep
->udc
;
522 struct usba_request
*req
;
523 unsigned long status
;
524 unsigned int bytecount
, nr_busy
;
527 status
= usba_ep_readl(ep
, STA
);
528 nr_busy
= USBA_BFEXT(BUSY_BANKS
, status
);
530 DBG(DBG_QUEUE
, "receive data: nr_busy=%u\n", nr_busy
);
532 while (nr_busy
> 0) {
533 if (list_empty(&ep
->queue
)) {
534 usba_ep_writel(ep
, CTL_DIS
, USBA_RX_BK_RDY
);
537 req
= list_entry(ep
->queue
.next
,
538 struct usba_request
, queue
);
540 bytecount
= USBA_BFEXT(BYTE_COUNT
, status
);
542 if (status
& (1 << 31))
544 if (req
->req
.actual
+ bytecount
>= req
->req
.length
) {
546 bytecount
= req
->req
.length
- req
->req
.actual
;
549 memcpy_fromio(req
->req
.buf
+ req
->req
.actual
,
550 ep
->fifo
, bytecount
);
551 req
->req
.actual
+= bytecount
;
553 usba_ep_writel(ep
, CLR_STA
, USBA_RX_BK_RDY
);
556 DBG(DBG_QUEUE
, "%s: request done\n", ep
->ep
.name
);
558 list_del_init(&req
->queue
);
559 usba_ep_writel(ep
, CTL_DIS
, USBA_RX_BK_RDY
);
560 spin_unlock(&udc
->lock
);
561 usb_gadget_giveback_request(&ep
->ep
, &req
->req
);
562 spin_lock(&udc
->lock
);
565 status
= usba_ep_readl(ep
, STA
);
566 nr_busy
= USBA_BFEXT(BUSY_BANKS
, status
);
568 if (is_complete
&& ep_is_control(ep
)) {
569 send_status(udc
, ep
);
576 request_complete(struct usba_ep
*ep
, struct usba_request
*req
, int status
)
578 struct usba_udc
*udc
= ep
->udc
;
580 WARN_ON(!list_empty(&req
->queue
));
582 if (req
->req
.status
== -EINPROGRESS
)
583 req
->req
.status
= status
;
586 usb_gadget_unmap_request(&udc
->gadget
, &req
->req
, ep
->is_in
);
588 DBG(DBG_GADGET
| DBG_REQ
,
589 "%s: req %p complete: status %d, actual %u\n",
590 ep
->ep
.name
, req
, req
->req
.status
, req
->req
.actual
);
592 spin_unlock(&udc
->lock
);
593 usb_gadget_giveback_request(&ep
->ep
, &req
->req
);
594 spin_lock(&udc
->lock
);
598 request_complete_list(struct usba_ep
*ep
, struct list_head
*list
, int status
)
600 struct usba_request
*req
, *tmp_req
;
602 list_for_each_entry_safe(req
, tmp_req
, list
, queue
) {
603 list_del_init(&req
->queue
);
604 request_complete(ep
, req
, status
);
609 usba_ep_enable(struct usb_ep
*_ep
, const struct usb_endpoint_descriptor
*desc
)
611 struct usba_ep
*ep
= to_usba_ep(_ep
);
612 struct usba_udc
*udc
= ep
->udc
;
613 unsigned long flags
, maxpacket
;
614 unsigned int nr_trans
;
616 DBG(DBG_GADGET
, "%s: ep_enable: desc=%p\n", ep
->ep
.name
, desc
);
618 maxpacket
= usb_endpoint_maxp(desc
);
620 if (((desc
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
) != ep
->index
)
622 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
624 || maxpacket
> ep
->fifo_size
) {
625 DBG(DBG_ERR
, "ep_enable: Invalid argument");
632 DBG(DBG_ERR
, "%s: EPT_CFG = 0x%lx (maxpacket = %lu)\n",
633 ep
->ep
.name
, ep
->ept_cfg
, maxpacket
);
635 if (usb_endpoint_dir_in(desc
)) {
637 ep
->ept_cfg
|= USBA_EPT_DIR_IN
;
640 switch (usb_endpoint_type(desc
)) {
641 case USB_ENDPOINT_XFER_CONTROL
:
642 ep
->ept_cfg
|= USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_CONTROL
);
644 case USB_ENDPOINT_XFER_ISOC
:
646 DBG(DBG_ERR
, "ep_enable: %s is not isoc capable\n",
652 * Bits 11:12 specify number of _additional_
653 * transactions per microframe.
655 nr_trans
= usb_endpoint_maxp_mult(desc
);
660 ep
->ept_cfg
|= USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_ISO
);
661 ep
->ept_cfg
|= USBA_BF(NB_TRANS
, nr_trans
);
664 case USB_ENDPOINT_XFER_BULK
:
665 ep
->ept_cfg
|= USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_BULK
);
667 case USB_ENDPOINT_XFER_INT
:
668 ep
->ept_cfg
|= USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_INT
);
672 spin_lock_irqsave(&ep
->udc
->lock
, flags
);
675 ep
->ep
.maxpacket
= maxpacket
;
677 usba_ep_writel(ep
, CFG
, ep
->ept_cfg
);
678 usba_ep_writel(ep
, CTL_ENB
, USBA_EPT_ENABLE
);
683 usba_int_enb_set(udc
, usba_int_enb_get(udc
) |
684 USBA_BF(EPT_INT
, 1 << ep
->index
) |
685 USBA_BF(DMA_INT
, 1 << ep
->index
));
686 ctrl
= USBA_AUTO_VALID
| USBA_INTDIS_DMA
;
687 usba_ep_writel(ep
, CTL_ENB
, ctrl
);
689 usba_int_enb_set(udc
, usba_int_enb_get(udc
) |
690 USBA_BF(EPT_INT
, 1 << ep
->index
));
693 spin_unlock_irqrestore(&udc
->lock
, flags
);
695 DBG(DBG_HW
, "EPT_CFG%d after init: %#08lx\n", ep
->index
,
696 (unsigned long)usba_ep_readl(ep
, CFG
));
697 DBG(DBG_HW
, "INT_ENB after init: %#08lx\n",
698 (unsigned long)usba_int_enb_get(udc
));
703 static int usba_ep_disable(struct usb_ep
*_ep
)
705 struct usba_ep
*ep
= to_usba_ep(_ep
);
706 struct usba_udc
*udc
= ep
->udc
;
710 DBG(DBG_GADGET
, "ep_disable: %s\n", ep
->ep
.name
);
712 spin_lock_irqsave(&udc
->lock
, flags
);
715 spin_unlock_irqrestore(&udc
->lock
, flags
);
716 /* REVISIT because this driver disables endpoints in
717 * reset_all_endpoints() before calling disconnect(),
718 * most gadget drivers would trigger this non-error ...
720 if (udc
->gadget
.speed
!= USB_SPEED_UNKNOWN
)
721 DBG(DBG_ERR
, "ep_disable: %s not enabled\n",
727 list_splice_init(&ep
->queue
, &req_list
);
729 usba_dma_writel(ep
, CONTROL
, 0);
730 usba_dma_writel(ep
, ADDRESS
, 0);
731 usba_dma_readl(ep
, STATUS
);
733 usba_ep_writel(ep
, CTL_DIS
, USBA_EPT_ENABLE
);
734 usba_int_enb_set(udc
, usba_int_enb_get(udc
) &
735 ~USBA_BF(EPT_INT
, 1 << ep
->index
));
737 request_complete_list(ep
, &req_list
, -ESHUTDOWN
);
739 spin_unlock_irqrestore(&udc
->lock
, flags
);
744 static struct usb_request
*
745 usba_ep_alloc_request(struct usb_ep
*_ep
, gfp_t gfp_flags
)
747 struct usba_request
*req
;
749 DBG(DBG_GADGET
, "ep_alloc_request: %p, 0x%x\n", _ep
, gfp_flags
);
751 req
= kzalloc(sizeof(*req
), gfp_flags
);
755 INIT_LIST_HEAD(&req
->queue
);
761 usba_ep_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
763 struct usba_request
*req
= to_usba_req(_req
);
765 DBG(DBG_GADGET
, "ep_free_request: %p, %p\n", _ep
, _req
);
770 static int queue_dma(struct usba_udc
*udc
, struct usba_ep
*ep
,
771 struct usba_request
*req
, gfp_t gfp_flags
)
776 DBG(DBG_DMA
, "%s: req l/%u d/%pad %c%c%c\n",
777 ep
->ep
.name
, req
->req
.length
, &req
->req
.dma
,
778 req
->req
.zero
? 'Z' : 'z',
779 req
->req
.short_not_ok
? 'S' : 's',
780 req
->req
.no_interrupt
? 'I' : 'i');
782 if (req
->req
.length
> 0x10000) {
783 /* Lengths from 0 to 65536 (inclusive) are supported */
784 DBG(DBG_ERR
, "invalid request length %u\n", req
->req
.length
);
788 ret
= usb_gadget_map_request(&udc
->gadget
, &req
->req
, ep
->is_in
);
793 req
->ctrl
= USBA_BF(DMA_BUF_LEN
, req
->req
.length
)
794 | USBA_DMA_CH_EN
| USBA_DMA_END_BUF_IE
795 | USBA_DMA_END_BUF_EN
;
798 req
->ctrl
|= USBA_DMA_END_TR_EN
| USBA_DMA_END_TR_IE
;
801 * Add this request to the queue and submit for DMA if
802 * possible. Check if we're still alive first -- we may have
803 * received a reset since last time we checked.
806 spin_lock_irqsave(&udc
->lock
, flags
);
808 if (list_empty(&ep
->queue
))
809 submit_request(ep
, req
);
811 list_add_tail(&req
->queue
, &ep
->queue
);
814 spin_unlock_irqrestore(&udc
->lock
, flags
);
820 usba_ep_queue(struct usb_ep
*_ep
, struct usb_request
*_req
, gfp_t gfp_flags
)
822 struct usba_request
*req
= to_usba_req(_req
);
823 struct usba_ep
*ep
= to_usba_ep(_ep
);
824 struct usba_udc
*udc
= ep
->udc
;
828 DBG(DBG_GADGET
| DBG_QUEUE
| DBG_REQ
, "%s: queue req %p, len %u\n",
829 ep
->ep
.name
, req
, _req
->length
);
831 if (!udc
->driver
|| udc
->gadget
.speed
== USB_SPEED_UNKNOWN
||
837 req
->last_transaction
= 0;
839 _req
->status
= -EINPROGRESS
;
843 return queue_dma(udc
, ep
, req
, gfp_flags
);
845 /* May have received a reset since last time we checked */
847 spin_lock_irqsave(&udc
->lock
, flags
);
849 list_add_tail(&req
->queue
, &ep
->queue
);
851 if ((!ep_is_control(ep
) && ep
->is_in
) ||
853 && (ep
->state
== DATA_STAGE_IN
854 || ep
->state
== STATUS_STAGE_IN
)))
855 usba_ep_writel(ep
, CTL_ENB
, USBA_TX_PK_RDY
);
857 usba_ep_writel(ep
, CTL_ENB
, USBA_RX_BK_RDY
);
860 spin_unlock_irqrestore(&udc
->lock
, flags
);
866 usba_update_req(struct usba_ep
*ep
, struct usba_request
*req
, u32 status
)
868 req
->req
.actual
= req
->req
.length
- USBA_BFEXT(DMA_BUF_LEN
, status
);
871 static int stop_dma(struct usba_ep
*ep
, u32
*pstatus
)
873 unsigned int timeout
;
877 * Stop the DMA controller. When writing both CH_EN
878 * and LINK to 0, the other bits are not affected.
880 usba_dma_writel(ep
, CONTROL
, 0);
882 /* Wait for the FIFO to empty */
883 for (timeout
= 40; timeout
; --timeout
) {
884 status
= usba_dma_readl(ep
, STATUS
);
885 if (!(status
& USBA_DMA_CH_EN
))
894 dev_err(&ep
->udc
->pdev
->dev
,
895 "%s: timed out waiting for DMA FIFO to empty\n",
903 static int usba_ep_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
905 struct usba_ep
*ep
= to_usba_ep(_ep
);
906 struct usba_udc
*udc
= ep
->udc
;
907 struct usba_request
*req
;
911 DBG(DBG_GADGET
| DBG_QUEUE
, "ep_dequeue: %s, req %p\n",
914 spin_lock_irqsave(&udc
->lock
, flags
);
916 list_for_each_entry(req
, &ep
->queue
, queue
) {
917 if (&req
->req
== _req
)
921 if (&req
->req
!= _req
) {
922 spin_unlock_irqrestore(&udc
->lock
, flags
);
926 if (req
->using_dma
) {
928 * If this request is currently being transferred,
929 * stop the DMA controller and reset the FIFO.
931 if (ep
->queue
.next
== &req
->queue
) {
932 status
= usba_dma_readl(ep
, STATUS
);
933 if (status
& USBA_DMA_CH_EN
)
934 stop_dma(ep
, &status
);
936 #ifdef CONFIG_USB_GADGET_DEBUG_FS
937 ep
->last_dma_status
= status
;
940 usba_writel(udc
, EPT_RST
, 1 << ep
->index
);
942 usba_update_req(ep
, req
, status
);
947 * Errors should stop the queue from advancing until the
948 * completion function returns.
950 list_del_init(&req
->queue
);
952 request_complete(ep
, req
, -ECONNRESET
);
954 /* Process the next request if any */
955 submit_next_request(ep
);
956 spin_unlock_irqrestore(&udc
->lock
, flags
);
961 static int usba_ep_set_halt(struct usb_ep
*_ep
, int value
)
963 struct usba_ep
*ep
= to_usba_ep(_ep
);
964 struct usba_udc
*udc
= ep
->udc
;
968 DBG(DBG_GADGET
, "endpoint %s: %s HALT\n", ep
->ep
.name
,
969 value
? "set" : "clear");
972 DBG(DBG_ERR
, "Attempted to halt uninitialized ep %s\n",
977 DBG(DBG_ERR
, "Attempted to halt isochronous ep %s\n",
982 spin_lock_irqsave(&udc
->lock
, flags
);
985 * We can't halt IN endpoints while there are still data to be
988 if (!list_empty(&ep
->queue
)
989 || ((value
&& ep
->is_in
&& (usba_ep_readl(ep
, STA
)
990 & USBA_BF(BUSY_BANKS
, -1L))))) {
994 usba_ep_writel(ep
, SET_STA
, USBA_FORCE_STALL
);
996 usba_ep_writel(ep
, CLR_STA
,
997 USBA_FORCE_STALL
| USBA_TOGGLE_CLR
);
998 usba_ep_readl(ep
, STA
);
1001 spin_unlock_irqrestore(&udc
->lock
, flags
);
1006 static int usba_ep_fifo_status(struct usb_ep
*_ep
)
1008 struct usba_ep
*ep
= to_usba_ep(_ep
);
1010 return USBA_BFEXT(BYTE_COUNT
, usba_ep_readl(ep
, STA
));
1013 static void usba_ep_fifo_flush(struct usb_ep
*_ep
)
1015 struct usba_ep
*ep
= to_usba_ep(_ep
);
1016 struct usba_udc
*udc
= ep
->udc
;
1018 usba_writel(udc
, EPT_RST
, 1 << ep
->index
);
1021 static const struct usb_ep_ops usba_ep_ops
= {
1022 .enable
= usba_ep_enable
,
1023 .disable
= usba_ep_disable
,
1024 .alloc_request
= usba_ep_alloc_request
,
1025 .free_request
= usba_ep_free_request
,
1026 .queue
= usba_ep_queue
,
1027 .dequeue
= usba_ep_dequeue
,
1028 .set_halt
= usba_ep_set_halt
,
1029 .fifo_status
= usba_ep_fifo_status
,
1030 .fifo_flush
= usba_ep_fifo_flush
,
1033 static int usba_udc_get_frame(struct usb_gadget
*gadget
)
1035 struct usba_udc
*udc
= to_usba_udc(gadget
);
1037 return USBA_BFEXT(FRAME_NUMBER
, usba_readl(udc
, FNUM
));
1040 static int usba_udc_wakeup(struct usb_gadget
*gadget
)
1042 struct usba_udc
*udc
= to_usba_udc(gadget
);
1043 unsigned long flags
;
1047 spin_lock_irqsave(&udc
->lock
, flags
);
1048 if (udc
->devstatus
& (1 << USB_DEVICE_REMOTE_WAKEUP
)) {
1049 ctrl
= usba_readl(udc
, CTRL
);
1050 usba_writel(udc
, CTRL
, ctrl
| USBA_REMOTE_WAKE_UP
);
1053 spin_unlock_irqrestore(&udc
->lock
, flags
);
1059 usba_udc_set_selfpowered(struct usb_gadget
*gadget
, int is_selfpowered
)
1061 struct usba_udc
*udc
= to_usba_udc(gadget
);
1062 unsigned long flags
;
1064 gadget
->is_selfpowered
= (is_selfpowered
!= 0);
1065 spin_lock_irqsave(&udc
->lock
, flags
);
1067 udc
->devstatus
|= 1 << USB_DEVICE_SELF_POWERED
;
1069 udc
->devstatus
&= ~(1 << USB_DEVICE_SELF_POWERED
);
1070 spin_unlock_irqrestore(&udc
->lock
, flags
);
1075 static int atmel_usba_start(struct usb_gadget
*gadget
,
1076 struct usb_gadget_driver
*driver
);
1077 static int atmel_usba_stop(struct usb_gadget
*gadget
);
1079 static struct usb_ep
*atmel_usba_match_ep(
1080 struct usb_gadget
*gadget
,
1081 struct usb_endpoint_descriptor
*desc
,
1082 struct usb_ss_ep_comp_descriptor
*ep_comp
1088 /* Look at endpoints until an unclaimed one looks usable */
1089 list_for_each_entry(_ep
, &gadget
->ep_list
, ep_list
) {
1090 if (usb_gadget_ep_match_desc(gadget
, _ep
, desc
, ep_comp
))
1098 if (fifo_mode
== 0) {
1099 /* Optimize hw fifo size based on ep type and other info */
1100 ep
= to_usba_ep(_ep
);
1102 switch (usb_endpoint_type(desc
)) {
1104 case USB_ENDPOINT_XFER_CONTROL
:
1107 case USB_ENDPOINT_XFER_ISOC
:
1108 ep
->fifo_size
= 1024;
1112 case USB_ENDPOINT_XFER_BULK
:
1113 ep
->fifo_size
= 512;
1117 case USB_ENDPOINT_XFER_INT
:
1118 if (desc
->wMaxPacketSize
== 0)
1120 roundup_pow_of_two(_ep
->maxpacket_limit
);
1123 roundup_pow_of_two(le16_to_cpu(desc
->wMaxPacketSize
));
1128 /* It might be a little bit late to set this */
1129 usb_ep_set_maxpacket_limit(&ep
->ep
, ep
->fifo_size
);
1131 /* Generate ept_cfg basd on FIFO size and number of banks */
1132 if (ep
->fifo_size
<= 8)
1133 ep
->ept_cfg
= USBA_BF(EPT_SIZE
, USBA_EPT_SIZE_8
);
1135 /* LSB is bit 1, not 0 */
1137 USBA_BF(EPT_SIZE
, fls(ep
->fifo_size
- 1) - 3);
1139 ep
->ept_cfg
|= USBA_BF(BK_NUMBER
, ep
->nr_banks
);
1141 ep
->udc
->configured_ep
++;
1147 static const struct usb_gadget_ops usba_udc_ops
= {
1148 .get_frame
= usba_udc_get_frame
,
1149 .wakeup
= usba_udc_wakeup
,
1150 .set_selfpowered
= usba_udc_set_selfpowered
,
1151 .udc_start
= atmel_usba_start
,
1152 .udc_stop
= atmel_usba_stop
,
1153 .match_ep
= atmel_usba_match_ep
,
1156 static struct usb_endpoint_descriptor usba_ep0_desc
= {
1157 .bLength
= USB_DT_ENDPOINT_SIZE
,
1158 .bDescriptorType
= USB_DT_ENDPOINT
,
1159 .bEndpointAddress
= 0,
1160 .bmAttributes
= USB_ENDPOINT_XFER_CONTROL
,
1161 .wMaxPacketSize
= cpu_to_le16(64),
1162 /* FIXME: I have no idea what to put here */
1166 static struct usb_gadget usba_gadget_template
= {
1167 .ops
= &usba_udc_ops
,
1168 .max_speed
= USB_SPEED_HIGH
,
1169 .name
= "atmel_usba_udc",
1173 * Called with interrupts disabled and udc->lock held.
1175 static void reset_all_endpoints(struct usba_udc
*udc
)
1178 struct usba_request
*req
, *tmp_req
;
1180 usba_writel(udc
, EPT_RST
, ~0UL);
1182 ep
= to_usba_ep(udc
->gadget
.ep0
);
1183 list_for_each_entry_safe(req
, tmp_req
, &ep
->queue
, queue
) {
1184 list_del_init(&req
->queue
);
1185 request_complete(ep
, req
, -ECONNRESET
);
1189 static struct usba_ep
*get_ep_by_addr(struct usba_udc
*udc
, u16 wIndex
)
1193 if ((wIndex
& USB_ENDPOINT_NUMBER_MASK
) == 0)
1194 return to_usba_ep(udc
->gadget
.ep0
);
1196 list_for_each_entry (ep
, &udc
->gadget
.ep_list
, ep
.ep_list
) {
1197 u8 bEndpointAddress
;
1201 bEndpointAddress
= ep
->ep
.desc
->bEndpointAddress
;
1202 if ((wIndex
^ bEndpointAddress
) & USB_DIR_IN
)
1204 if ((bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
)
1205 == (wIndex
& USB_ENDPOINT_NUMBER_MASK
))
1212 /* Called with interrupts disabled and udc->lock held */
1213 static inline void set_protocol_stall(struct usba_udc
*udc
, struct usba_ep
*ep
)
1215 usba_ep_writel(ep
, SET_STA
, USBA_FORCE_STALL
);
1216 ep
->state
= WAIT_FOR_SETUP
;
1219 static inline int is_stalled(struct usba_udc
*udc
, struct usba_ep
*ep
)
1221 if (usba_ep_readl(ep
, STA
) & USBA_FORCE_STALL
)
1226 static inline void set_address(struct usba_udc
*udc
, unsigned int addr
)
1230 DBG(DBG_BUS
, "setting address %u...\n", addr
);
1231 regval
= usba_readl(udc
, CTRL
);
1232 regval
= USBA_BFINS(DEV_ADDR
, addr
, regval
);
1233 usba_writel(udc
, CTRL
, regval
);
1236 static int do_test_mode(struct usba_udc
*udc
)
1238 static const char test_packet_buffer
[] = {
1240 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1242 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
1244 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
1245 /* JJJJJJJKKKKKKK * 8 */
1246 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1247 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1249 0x7F, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD,
1250 /* {JKKKKKKK * 10}, JK */
1251 0xFC, 0x7E, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD, 0x7E
1254 struct device
*dev
= &udc
->pdev
->dev
;
1257 test_mode
= udc
->test_mode
;
1259 /* Start from a clean slate */
1260 reset_all_endpoints(udc
);
1262 switch (test_mode
) {
1265 usba_writel(udc
, TST
, USBA_TST_J_MODE
);
1266 dev_info(dev
, "Entering Test_J mode...\n");
1270 usba_writel(udc
, TST
, USBA_TST_K_MODE
);
1271 dev_info(dev
, "Entering Test_K mode...\n");
1275 * Test_SE0_NAK: Force high-speed mode and set up ep0
1276 * for Bulk IN transfers
1278 ep
= &udc
->usba_ep
[0];
1279 usba_writel(udc
, TST
,
1280 USBA_BF(SPEED_CFG
, USBA_SPEED_CFG_FORCE_HIGH
));
1281 usba_ep_writel(ep
, CFG
,
1282 USBA_BF(EPT_SIZE
, USBA_EPT_SIZE_64
)
1284 | USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_BULK
)
1285 | USBA_BF(BK_NUMBER
, 1));
1286 if (!(usba_ep_readl(ep
, CFG
) & USBA_EPT_MAPPED
)) {
1287 set_protocol_stall(udc
, ep
);
1288 dev_err(dev
, "Test_SE0_NAK: ep0 not mapped\n");
1290 usba_ep_writel(ep
, CTL_ENB
, USBA_EPT_ENABLE
);
1291 dev_info(dev
, "Entering Test_SE0_NAK mode...\n");
1296 ep
= &udc
->usba_ep
[0];
1297 usba_ep_writel(ep
, CFG
,
1298 USBA_BF(EPT_SIZE
, USBA_EPT_SIZE_64
)
1300 | USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_BULK
)
1301 | USBA_BF(BK_NUMBER
, 1));
1302 if (!(usba_ep_readl(ep
, CFG
) & USBA_EPT_MAPPED
)) {
1303 set_protocol_stall(udc
, ep
);
1304 dev_err(dev
, "Test_Packet: ep0 not mapped\n");
1306 usba_ep_writel(ep
, CTL_ENB
, USBA_EPT_ENABLE
);
1307 usba_writel(udc
, TST
, USBA_TST_PKT_MODE
);
1308 memcpy_toio(ep
->fifo
, test_packet_buffer
,
1309 sizeof(test_packet_buffer
));
1310 usba_ep_writel(ep
, SET_STA
, USBA_TX_PK_RDY
);
1311 dev_info(dev
, "Entering Test_Packet mode...\n");
1315 dev_err(dev
, "Invalid test mode: 0x%04x\n", test_mode
);
1322 /* Avoid overly long expressions */
1323 static inline bool feature_is_dev_remote_wakeup(struct usb_ctrlrequest
*crq
)
1325 if (crq
->wValue
== cpu_to_le16(USB_DEVICE_REMOTE_WAKEUP
))
1330 static inline bool feature_is_dev_test_mode(struct usb_ctrlrequest
*crq
)
1332 if (crq
->wValue
== cpu_to_le16(USB_DEVICE_TEST_MODE
))
1337 static inline bool feature_is_ep_halt(struct usb_ctrlrequest
*crq
)
1339 if (crq
->wValue
== cpu_to_le16(USB_ENDPOINT_HALT
))
1344 static int handle_ep0_setup(struct usba_udc
*udc
, struct usba_ep
*ep
,
1345 struct usb_ctrlrequest
*crq
)
1349 switch (crq
->bRequest
) {
1350 case USB_REQ_GET_STATUS
: {
1353 if (crq
->bRequestType
== (USB_DIR_IN
| USB_RECIP_DEVICE
)) {
1354 status
= cpu_to_le16(udc
->devstatus
);
1355 } else if (crq
->bRequestType
1356 == (USB_DIR_IN
| USB_RECIP_INTERFACE
)) {
1357 status
= cpu_to_le16(0);
1358 } else if (crq
->bRequestType
1359 == (USB_DIR_IN
| USB_RECIP_ENDPOINT
)) {
1360 struct usba_ep
*target
;
1362 target
= get_ep_by_addr(udc
, le16_to_cpu(crq
->wIndex
));
1367 if (is_stalled(udc
, target
))
1368 status
|= cpu_to_le16(1);
1372 /* Write directly to the FIFO. No queueing is done. */
1373 if (crq
->wLength
!= cpu_to_le16(sizeof(status
)))
1375 ep
->state
= DATA_STAGE_IN
;
1376 usba_io_writew(status
, ep
->fifo
);
1377 usba_ep_writel(ep
, SET_STA
, USBA_TX_PK_RDY
);
1381 case USB_REQ_CLEAR_FEATURE
: {
1382 if (crq
->bRequestType
== USB_RECIP_DEVICE
) {
1383 if (feature_is_dev_remote_wakeup(crq
))
1385 &= ~(1 << USB_DEVICE_REMOTE_WAKEUP
);
1387 /* Can't CLEAR_FEATURE TEST_MODE */
1389 } else if (crq
->bRequestType
== USB_RECIP_ENDPOINT
) {
1390 struct usba_ep
*target
;
1392 if (crq
->wLength
!= cpu_to_le16(0)
1393 || !feature_is_ep_halt(crq
))
1395 target
= get_ep_by_addr(udc
, le16_to_cpu(crq
->wIndex
));
1399 usba_ep_writel(target
, CLR_STA
, USBA_FORCE_STALL
);
1400 if (target
->index
!= 0)
1401 usba_ep_writel(target
, CLR_STA
,
1407 send_status(udc
, ep
);
1411 case USB_REQ_SET_FEATURE
: {
1412 if (crq
->bRequestType
== USB_RECIP_DEVICE
) {
1413 if (feature_is_dev_test_mode(crq
)) {
1414 send_status(udc
, ep
);
1415 ep
->state
= STATUS_STAGE_TEST
;
1416 udc
->test_mode
= le16_to_cpu(crq
->wIndex
);
1418 } else if (feature_is_dev_remote_wakeup(crq
)) {
1419 udc
->devstatus
|= 1 << USB_DEVICE_REMOTE_WAKEUP
;
1423 } else if (crq
->bRequestType
== USB_RECIP_ENDPOINT
) {
1424 struct usba_ep
*target
;
1426 if (crq
->wLength
!= cpu_to_le16(0)
1427 || !feature_is_ep_halt(crq
))
1430 target
= get_ep_by_addr(udc
, le16_to_cpu(crq
->wIndex
));
1434 usba_ep_writel(target
, SET_STA
, USBA_FORCE_STALL
);
1438 send_status(udc
, ep
);
1442 case USB_REQ_SET_ADDRESS
:
1443 if (crq
->bRequestType
!= (USB_DIR_OUT
| USB_RECIP_DEVICE
))
1446 set_address(udc
, le16_to_cpu(crq
->wValue
));
1447 send_status(udc
, ep
);
1448 ep
->state
= STATUS_STAGE_ADDR
;
1453 spin_unlock(&udc
->lock
);
1454 retval
= udc
->driver
->setup(&udc
->gadget
, crq
);
1455 spin_lock(&udc
->lock
);
1461 pr_err("udc: %s: Invalid setup request: %02x.%02x v%04x i%04x l%d, "
1462 "halting endpoint...\n",
1463 ep
->ep
.name
, crq
->bRequestType
, crq
->bRequest
,
1464 le16_to_cpu(crq
->wValue
), le16_to_cpu(crq
->wIndex
),
1465 le16_to_cpu(crq
->wLength
));
1466 set_protocol_stall(udc
, ep
);
1470 static void usba_control_irq(struct usba_udc
*udc
, struct usba_ep
*ep
)
1472 struct usba_request
*req
;
1477 epstatus
= usba_ep_readl(ep
, STA
);
1478 epctrl
= usba_ep_readl(ep
, CTL
);
1480 DBG(DBG_INT
, "%s [%d]: s/%08x c/%08x\n",
1481 ep
->ep
.name
, ep
->state
, epstatus
, epctrl
);
1484 if (!list_empty(&ep
->queue
))
1485 req
= list_entry(ep
->queue
.next
,
1486 struct usba_request
, queue
);
1488 if ((epctrl
& USBA_TX_PK_RDY
) && !(epstatus
& USBA_TX_PK_RDY
)) {
1490 next_fifo_transaction(ep
, req
);
1492 submit_request(ep
, req
);
1494 if (req
->last_transaction
) {
1495 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_PK_RDY
);
1496 usba_ep_writel(ep
, CTL_ENB
, USBA_TX_COMPLETE
);
1500 if ((epstatus
& epctrl
) & USBA_TX_COMPLETE
) {
1501 usba_ep_writel(ep
, CLR_STA
, USBA_TX_COMPLETE
);
1503 switch (ep
->state
) {
1505 usba_ep_writel(ep
, CTL_ENB
, USBA_RX_BK_RDY
);
1506 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_COMPLETE
);
1507 ep
->state
= STATUS_STAGE_OUT
;
1509 case STATUS_STAGE_ADDR
:
1510 /* Activate our new address */
1511 usba_writel(udc
, CTRL
, (usba_readl(udc
, CTRL
)
1513 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_COMPLETE
);
1514 ep
->state
= WAIT_FOR_SETUP
;
1516 case STATUS_STAGE_IN
:
1518 list_del_init(&req
->queue
);
1519 request_complete(ep
, req
, 0);
1520 submit_next_request(ep
);
1522 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_COMPLETE
);
1523 ep
->state
= WAIT_FOR_SETUP
;
1525 case STATUS_STAGE_TEST
:
1526 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_COMPLETE
);
1527 ep
->state
= WAIT_FOR_SETUP
;
1528 if (do_test_mode(udc
))
1529 set_protocol_stall(udc
, ep
);
1532 pr_err("udc: %s: TXCOMP: Invalid endpoint state %d, "
1533 "halting endpoint...\n",
1534 ep
->ep
.name
, ep
->state
);
1535 set_protocol_stall(udc
, ep
);
1541 if ((epstatus
& epctrl
) & USBA_RX_BK_RDY
) {
1542 switch (ep
->state
) {
1543 case STATUS_STAGE_OUT
:
1544 usba_ep_writel(ep
, CLR_STA
, USBA_RX_BK_RDY
);
1545 usba_ep_writel(ep
, CTL_DIS
, USBA_RX_BK_RDY
);
1548 list_del_init(&req
->queue
);
1549 request_complete(ep
, req
, 0);
1551 ep
->state
= WAIT_FOR_SETUP
;
1554 case DATA_STAGE_OUT
:
1559 usba_ep_writel(ep
, CLR_STA
, USBA_RX_BK_RDY
);
1560 usba_ep_writel(ep
, CTL_DIS
, USBA_RX_BK_RDY
);
1561 pr_err("udc: %s: RXRDY: Invalid endpoint state %d, "
1562 "halting endpoint...\n",
1563 ep
->ep
.name
, ep
->state
);
1564 set_protocol_stall(udc
, ep
);
1570 if (epstatus
& USBA_RX_SETUP
) {
1572 struct usb_ctrlrequest crq
;
1573 unsigned long data
[2];
1575 unsigned int pkt_len
;
1578 if (ep
->state
!= WAIT_FOR_SETUP
) {
1580 * Didn't expect a SETUP packet at this
1581 * point. Clean up any pending requests (which
1582 * may be successful).
1584 int status
= -EPROTO
;
1587 * RXRDY and TXCOMP are dropped when SETUP
1588 * packets arrive. Just pretend we received
1589 * the status packet.
1591 if (ep
->state
== STATUS_STAGE_OUT
1592 || ep
->state
== STATUS_STAGE_IN
) {
1593 usba_ep_writel(ep
, CTL_DIS
, USBA_RX_BK_RDY
);
1598 list_del_init(&req
->queue
);
1599 request_complete(ep
, req
, status
);
1603 pkt_len
= USBA_BFEXT(BYTE_COUNT
, usba_ep_readl(ep
, STA
));
1604 DBG(DBG_HW
, "Packet length: %u\n", pkt_len
);
1605 if (pkt_len
!= sizeof(crq
)) {
1606 pr_warn("udc: Invalid packet length %u (expected %zu)\n",
1607 pkt_len
, sizeof(crq
));
1608 set_protocol_stall(udc
, ep
);
1612 DBG(DBG_FIFO
, "Copying ctrl request from 0x%p:\n", ep
->fifo
);
1613 memcpy_fromio(crq
.data
, ep
->fifo
, sizeof(crq
));
1615 /* Free up one bank in the FIFO so that we can
1616 * generate or receive a reply right away. */
1617 usba_ep_writel(ep
, CLR_STA
, USBA_RX_SETUP
);
1619 /* printk(KERN_DEBUG "setup: %d: %02x.%02x\n",
1620 ep->state, crq.crq.bRequestType,
1621 crq.crq.bRequest); */
1623 if (crq
.crq
.bRequestType
& USB_DIR_IN
) {
1625 * The USB 2.0 spec states that "if wLength is
1626 * zero, there is no data transfer phase."
1627 * However, testusb #14 seems to actually
1628 * expect a data phase even if wLength = 0...
1630 ep
->state
= DATA_STAGE_IN
;
1632 if (crq
.crq
.wLength
!= cpu_to_le16(0))
1633 ep
->state
= DATA_STAGE_OUT
;
1635 ep
->state
= STATUS_STAGE_IN
;
1640 ret
= handle_ep0_setup(udc
, ep
, &crq
.crq
);
1642 spin_unlock(&udc
->lock
);
1643 ret
= udc
->driver
->setup(&udc
->gadget
, &crq
.crq
);
1644 spin_lock(&udc
->lock
);
1647 DBG(DBG_BUS
, "req %02x.%02x, length %d, state %d, ret %d\n",
1648 crq
.crq
.bRequestType
, crq
.crq
.bRequest
,
1649 le16_to_cpu(crq
.crq
.wLength
), ep
->state
, ret
);
1652 /* Let the host know that we failed */
1653 set_protocol_stall(udc
, ep
);
1658 static void usba_ep_irq(struct usba_udc
*udc
, struct usba_ep
*ep
)
1660 struct usba_request
*req
;
1664 epstatus
= usba_ep_readl(ep
, STA
);
1665 epctrl
= usba_ep_readl(ep
, CTL
);
1667 DBG(DBG_INT
, "%s: interrupt, status: 0x%08x\n", ep
->ep
.name
, epstatus
);
1669 while ((epctrl
& USBA_TX_PK_RDY
) && !(epstatus
& USBA_TX_PK_RDY
)) {
1670 DBG(DBG_BUS
, "%s: TX PK ready\n", ep
->ep
.name
);
1672 if (list_empty(&ep
->queue
)) {
1673 dev_warn(&udc
->pdev
->dev
, "ep_irq: queue empty\n");
1674 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_PK_RDY
);
1678 req
= list_entry(ep
->queue
.next
, struct usba_request
, queue
);
1680 if (req
->using_dma
) {
1681 /* Send a zero-length packet */
1682 usba_ep_writel(ep
, SET_STA
,
1684 usba_ep_writel(ep
, CTL_DIS
,
1686 list_del_init(&req
->queue
);
1687 submit_next_request(ep
);
1688 request_complete(ep
, req
, 0);
1691 next_fifo_transaction(ep
, req
);
1693 submit_request(ep
, req
);
1695 if (req
->last_transaction
) {
1696 list_del_init(&req
->queue
);
1697 submit_next_request(ep
);
1698 request_complete(ep
, req
, 0);
1702 epstatus
= usba_ep_readl(ep
, STA
);
1703 epctrl
= usba_ep_readl(ep
, CTL
);
1705 if ((epstatus
& epctrl
) & USBA_RX_BK_RDY
) {
1706 DBG(DBG_BUS
, "%s: RX data ready\n", ep
->ep
.name
);
1711 static void usba_dma_irq(struct usba_udc
*udc
, struct usba_ep
*ep
)
1713 struct usba_request
*req
;
1714 u32 status
, control
, pending
;
1716 status
= usba_dma_readl(ep
, STATUS
);
1717 control
= usba_dma_readl(ep
, CONTROL
);
1718 #ifdef CONFIG_USB_GADGET_DEBUG_FS
1719 ep
->last_dma_status
= status
;
1721 pending
= status
& control
;
1722 DBG(DBG_INT
| DBG_DMA
, "dma irq, s/%#08x, c/%#08x\n", status
, control
);
1724 if (status
& USBA_DMA_CH_EN
) {
1725 dev_err(&udc
->pdev
->dev
,
1726 "DMA_CH_EN is set after transfer is finished!\n");
1727 dev_err(&udc
->pdev
->dev
,
1728 "status=%#08x, pending=%#08x, control=%#08x\n",
1729 status
, pending
, control
);
1732 * try to pretend nothing happened. We might have to
1733 * do something here...
1737 if (list_empty(&ep
->queue
))
1738 /* Might happen if a reset comes along at the right moment */
1741 if (pending
& (USBA_DMA_END_TR_ST
| USBA_DMA_END_BUF_ST
)) {
1742 req
= list_entry(ep
->queue
.next
, struct usba_request
, queue
);
1743 usba_update_req(ep
, req
, status
);
1745 list_del_init(&req
->queue
);
1746 submit_next_request(ep
);
1747 request_complete(ep
, req
, 0);
1751 static irqreturn_t
usba_udc_irq(int irq
, void *devid
)
1753 struct usba_udc
*udc
= devid
;
1754 u32 status
, int_enb
;
1758 spin_lock(&udc
->lock
);
1760 int_enb
= usba_int_enb_get(udc
);
1761 status
= usba_readl(udc
, INT_STA
) & (int_enb
| USBA_HIGH_SPEED
);
1762 DBG(DBG_INT
, "irq, status=%#08x\n", status
);
1764 if (status
& USBA_DET_SUSPEND
) {
1765 toggle_bias(udc
, 0);
1766 usba_writel(udc
, INT_CLR
, USBA_DET_SUSPEND
);
1767 usba_int_enb_set(udc
, int_enb
| USBA_WAKE_UP
);
1768 udc
->bias_pulse_needed
= true;
1769 DBG(DBG_BUS
, "Suspend detected\n");
1770 if (udc
->gadget
.speed
!= USB_SPEED_UNKNOWN
1771 && udc
->driver
&& udc
->driver
->suspend
) {
1772 spin_unlock(&udc
->lock
);
1773 udc
->driver
->suspend(&udc
->gadget
);
1774 spin_lock(&udc
->lock
);
1778 if (status
& USBA_WAKE_UP
) {
1779 toggle_bias(udc
, 1);
1780 usba_writel(udc
, INT_CLR
, USBA_WAKE_UP
);
1781 usba_int_enb_set(udc
, int_enb
& ~USBA_WAKE_UP
);
1782 DBG(DBG_BUS
, "Wake Up CPU detected\n");
1785 if (status
& USBA_END_OF_RESUME
) {
1786 usba_writel(udc
, INT_CLR
, USBA_END_OF_RESUME
);
1787 generate_bias_pulse(udc
);
1788 DBG(DBG_BUS
, "Resume detected\n");
1789 if (udc
->gadget
.speed
!= USB_SPEED_UNKNOWN
1790 && udc
->driver
&& udc
->driver
->resume
) {
1791 spin_unlock(&udc
->lock
);
1792 udc
->driver
->resume(&udc
->gadget
);
1793 spin_lock(&udc
->lock
);
1797 dma_status
= USBA_BFEXT(DMA_INT
, status
);
1801 for (i
= 1; i
<= USBA_NR_DMAS
; i
++)
1802 if (dma_status
& (1 << i
))
1803 usba_dma_irq(udc
, &udc
->usba_ep
[i
]);
1806 ep_status
= USBA_BFEXT(EPT_INT
, status
);
1810 for (i
= 0; i
< udc
->num_ep
; i
++)
1811 if (ep_status
& (1 << i
)) {
1812 if (ep_is_control(&udc
->usba_ep
[i
]))
1813 usba_control_irq(udc
, &udc
->usba_ep
[i
]);
1815 usba_ep_irq(udc
, &udc
->usba_ep
[i
]);
1819 if (status
& USBA_END_OF_RESET
) {
1820 struct usba_ep
*ep0
, *ep
;
1823 usba_writel(udc
, INT_CLR
, USBA_END_OF_RESET
);
1824 generate_bias_pulse(udc
);
1825 reset_all_endpoints(udc
);
1827 if (udc
->gadget
.speed
!= USB_SPEED_UNKNOWN
&& udc
->driver
) {
1828 udc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1829 spin_unlock(&udc
->lock
);
1830 usb_gadget_udc_reset(&udc
->gadget
, udc
->driver
);
1831 spin_lock(&udc
->lock
);
1834 if (status
& USBA_HIGH_SPEED
)
1835 udc
->gadget
.speed
= USB_SPEED_HIGH
;
1837 udc
->gadget
.speed
= USB_SPEED_FULL
;
1838 DBG(DBG_BUS
, "%s bus reset detected\n",
1839 usb_speed_string(udc
->gadget
.speed
));
1841 ep0
= &udc
->usba_ep
[0];
1842 ep0
->ep
.desc
= &usba_ep0_desc
;
1843 ep0
->state
= WAIT_FOR_SETUP
;
1844 usba_ep_writel(ep0
, CFG
,
1845 (USBA_BF(EPT_SIZE
, EP0_EPT_SIZE
)
1846 | USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_CONTROL
)
1847 | USBA_BF(BK_NUMBER
, USBA_BK_NUMBER_ONE
)));
1848 usba_ep_writel(ep0
, CTL_ENB
,
1849 USBA_EPT_ENABLE
| USBA_RX_SETUP
);
1850 usba_int_enb_set(udc
, int_enb
| USBA_BF(EPT_INT
, 1) |
1851 USBA_DET_SUSPEND
| USBA_END_OF_RESUME
);
1854 * Unclear why we hit this irregularly, e.g. in usbtest,
1855 * but it's clearly harmless...
1857 if (!(usba_ep_readl(ep0
, CFG
) & USBA_EPT_MAPPED
))
1858 dev_dbg(&udc
->pdev
->dev
,
1859 "ODD: EP0 configuration is invalid!\n");
1861 /* Preallocate other endpoints */
1862 n
= fifo_mode
? udc
->num_ep
: udc
->configured_ep
;
1863 for (i
= 1; i
< n
; i
++) {
1864 ep
= &udc
->usba_ep
[i
];
1865 usba_ep_writel(ep
, CFG
, ep
->ept_cfg
);
1866 if (!(usba_ep_readl(ep
, CFG
) & USBA_EPT_MAPPED
))
1867 dev_dbg(&udc
->pdev
->dev
,
1868 "ODD: EP%d configuration is invalid!\n", i
);
1872 spin_unlock(&udc
->lock
);
1877 static int start_clock(struct usba_udc
*udc
)
1884 ret
= clk_prepare_enable(udc
->pclk
);
1887 ret
= clk_prepare_enable(udc
->hclk
);
1889 clk_disable_unprepare(udc
->pclk
);
1893 udc
->clocked
= true;
1897 static void stop_clock(struct usba_udc
*udc
)
1902 clk_disable_unprepare(udc
->hclk
);
1903 clk_disable_unprepare(udc
->pclk
);
1905 udc
->clocked
= false;
1908 static int usba_start(struct usba_udc
*udc
)
1910 unsigned long flags
;
1913 ret
= start_clock(udc
);
1917 spin_lock_irqsave(&udc
->lock
, flags
);
1918 toggle_bias(udc
, 1);
1919 usba_writel(udc
, CTRL
, USBA_ENABLE_MASK
);
1920 usba_int_enb_set(udc
, USBA_END_OF_RESET
);
1921 spin_unlock_irqrestore(&udc
->lock
, flags
);
1926 static void usba_stop(struct usba_udc
*udc
)
1928 unsigned long flags
;
1930 spin_lock_irqsave(&udc
->lock
, flags
);
1931 udc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1932 reset_all_endpoints(udc
);
1934 /* This will also disable the DP pullup */
1935 toggle_bias(udc
, 0);
1936 usba_writel(udc
, CTRL
, USBA_DISABLE_MASK
);
1937 spin_unlock_irqrestore(&udc
->lock
, flags
);
1942 static irqreturn_t
usba_vbus_irq_thread(int irq
, void *devid
)
1944 struct usba_udc
*udc
= devid
;
1950 mutex_lock(&udc
->vbus_mutex
);
1952 vbus
= vbus_is_present(udc
);
1953 if (vbus
!= udc
->vbus_prev
) {
1959 if (udc
->driver
->disconnect
)
1960 udc
->driver
->disconnect(&udc
->gadget
);
1962 udc
->vbus_prev
= vbus
;
1965 mutex_unlock(&udc
->vbus_mutex
);
1969 static int atmel_usba_start(struct usb_gadget
*gadget
,
1970 struct usb_gadget_driver
*driver
)
1973 struct usba_udc
*udc
= container_of(gadget
, struct usba_udc
, gadget
);
1974 unsigned long flags
;
1976 spin_lock_irqsave(&udc
->lock
, flags
);
1977 udc
->devstatus
= 1 << USB_DEVICE_SELF_POWERED
;
1978 udc
->driver
= driver
;
1979 spin_unlock_irqrestore(&udc
->lock
, flags
);
1981 mutex_lock(&udc
->vbus_mutex
);
1983 if (gpio_is_valid(udc
->vbus_pin
))
1984 enable_irq(gpio_to_irq(udc
->vbus_pin
));
1986 /* If Vbus is present, enable the controller and wait for reset */
1987 udc
->vbus_prev
= vbus_is_present(udc
);
1988 if (udc
->vbus_prev
) {
1989 ret
= usba_start(udc
);
1994 mutex_unlock(&udc
->vbus_mutex
);
1998 if (gpio_is_valid(udc
->vbus_pin
))
1999 disable_irq(gpio_to_irq(udc
->vbus_pin
));
2001 mutex_unlock(&udc
->vbus_mutex
);
2003 spin_lock_irqsave(&udc
->lock
, flags
);
2004 udc
->devstatus
&= ~(1 << USB_DEVICE_SELF_POWERED
);
2006 spin_unlock_irqrestore(&udc
->lock
, flags
);
2010 static int atmel_usba_stop(struct usb_gadget
*gadget
)
2012 struct usba_udc
*udc
= container_of(gadget
, struct usba_udc
, gadget
);
2014 if (gpio_is_valid(udc
->vbus_pin
))
2015 disable_irq(gpio_to_irq(udc
->vbus_pin
));
2018 udc
->configured_ep
= 1;
2028 static void at91sam9rl_toggle_bias(struct usba_udc
*udc
, int is_on
)
2030 regmap_update_bits(udc
->pmc
, AT91_CKGR_UCKR
, AT91_PMC_BIASEN
,
2031 is_on
? AT91_PMC_BIASEN
: 0);
2034 static void at91sam9g45_pulse_bias(struct usba_udc
*udc
)
2036 regmap_update_bits(udc
->pmc
, AT91_CKGR_UCKR
, AT91_PMC_BIASEN
, 0);
2037 regmap_update_bits(udc
->pmc
, AT91_CKGR_UCKR
, AT91_PMC_BIASEN
,
2041 static const struct usba_udc_errata at91sam9rl_errata
= {
2042 .toggle_bias
= at91sam9rl_toggle_bias
,
2045 static const struct usba_udc_errata at91sam9g45_errata
= {
2046 .pulse_bias
= at91sam9g45_pulse_bias
,
2049 static const struct of_device_id atmel_udc_dt_ids
[] = {
2050 { .compatible
= "atmel,at91sam9rl-udc", .data
= &at91sam9rl_errata
},
2051 { .compatible
= "atmel,at91sam9g45-udc", .data
= &at91sam9g45_errata
},
2052 { .compatible
= "atmel,sama5d3-udc" },
2056 MODULE_DEVICE_TABLE(of
, atmel_udc_dt_ids
);
2058 static struct usba_ep
* atmel_udc_of_init(struct platform_device
*pdev
,
2059 struct usba_udc
*udc
)
2063 enum of_gpio_flags flags
;
2064 struct device_node
*np
= pdev
->dev
.of_node
;
2065 const struct of_device_id
*match
;
2066 struct device_node
*pp
;
2068 struct usba_ep
*eps
, *ep
;
2070 match
= of_match_node(atmel_udc_dt_ids
, np
);
2072 return ERR_PTR(-EINVAL
);
2074 udc
->errata
= match
->data
;
2075 udc
->pmc
= syscon_regmap_lookup_by_compatible("atmel,at91sam9g45-pmc");
2076 if (IS_ERR(udc
->pmc
))
2077 udc
->pmc
= syscon_regmap_lookup_by_compatible("atmel,at91sam9x5-pmc");
2078 if (udc
->errata
&& IS_ERR(udc
->pmc
))
2079 return ERR_CAST(udc
->pmc
);
2083 udc
->vbus_pin
= of_get_named_gpio_flags(np
, "atmel,vbus-gpio", 0,
2085 udc
->vbus_pin_inverted
= (flags
& OF_GPIO_ACTIVE_LOW
) ? 1 : 0;
2087 if (fifo_mode
== 0) {
2089 while ((pp
= of_get_next_child(np
, pp
)))
2091 udc
->configured_ep
= 1;
2093 udc
->num_ep
= usba_config_fifo_table(udc
);
2095 eps
= devm_kzalloc(&pdev
->dev
, sizeof(struct usba_ep
) * udc
->num_ep
,
2098 return ERR_PTR(-ENOMEM
);
2100 udc
->gadget
.ep0
= &eps
[0].ep
;
2102 INIT_LIST_HEAD(&eps
[0].ep
.ep_list
);
2106 while ((pp
= of_get_next_child(np
, pp
)) && i
< udc
->num_ep
) {
2109 ret
= of_property_read_u32(pp
, "reg", &val
);
2111 dev_err(&pdev
->dev
, "of_probe: reg error(%d)\n", ret
);
2114 ep
->index
= fifo_mode
? udc
->fifo_cfg
[i
].hw_ep_num
: val
;
2116 ret
= of_property_read_u32(pp
, "atmel,fifo-size", &val
);
2118 dev_err(&pdev
->dev
, "of_probe: fifo-size error(%d)\n", ret
);
2121 ep
->fifo_size
= fifo_mode
? udc
->fifo_cfg
[i
].fifo_size
: val
;
2123 ret
= of_property_read_u32(pp
, "atmel,nb-banks", &val
);
2125 dev_err(&pdev
->dev
, "of_probe: nb-banks error(%d)\n", ret
);
2128 ep
->nr_banks
= fifo_mode
? udc
->fifo_cfg
[i
].nr_banks
: val
;
2130 ep
->can_dma
= of_property_read_bool(pp
, "atmel,can-dma");
2131 ep
->can_isoc
= of_property_read_bool(pp
, "atmel,can-isoc");
2133 ret
= of_property_read_string(pp
, "name", &name
);
2135 dev_err(&pdev
->dev
, "of_probe: name error(%d)\n", ret
);
2138 sprintf(ep
->name
, "ep%d", ep
->index
);
2139 ep
->ep
.name
= ep
->name
;
2141 ep
->ep_regs
= udc
->regs
+ USBA_EPT_BASE(i
);
2142 ep
->dma_regs
= udc
->regs
+ USBA_DMA_BASE(i
);
2143 ep
->fifo
= udc
->fifo
+ USBA_FIFO_BASE(i
);
2144 ep
->ep
.ops
= &usba_ep_ops
;
2145 usb_ep_set_maxpacket_limit(&ep
->ep
, ep
->fifo_size
);
2147 INIT_LIST_HEAD(&ep
->queue
);
2149 if (ep
->index
== 0) {
2150 ep
->ep
.caps
.type_control
= true;
2152 ep
->ep
.caps
.type_iso
= ep
->can_isoc
;
2153 ep
->ep
.caps
.type_bulk
= true;
2154 ep
->ep
.caps
.type_int
= true;
2157 ep
->ep
.caps
.dir_in
= true;
2158 ep
->ep
.caps
.dir_out
= true;
2160 if (fifo_mode
!= 0) {
2162 * Generate ept_cfg based on FIFO size and
2165 if (ep
->fifo_size
<= 8)
2166 ep
->ept_cfg
= USBA_BF(EPT_SIZE
, USBA_EPT_SIZE_8
);
2168 /* LSB is bit 1, not 0 */
2170 USBA_BF(EPT_SIZE
, fls(ep
->fifo_size
- 1) - 3);
2172 ep
->ept_cfg
|= USBA_BF(BK_NUMBER
, ep
->nr_banks
);
2176 list_add_tail(&ep
->ep
.ep_list
, &udc
->gadget
.ep_list
);
2182 dev_err(&pdev
->dev
, "of_probe: no endpoint specified\n");
2189 return ERR_PTR(ret
);
2192 static struct usba_ep
* atmel_udc_of_init(struct platform_device
*pdev
,
2193 struct usba_udc
*udc
)
2195 return ERR_PTR(-ENOSYS
);
2199 static struct usba_ep
* usba_udc_pdata(struct platform_device
*pdev
,
2200 struct usba_udc
*udc
)
2202 struct usba_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
2203 struct usba_ep
*eps
;
2207 return ERR_PTR(-ENXIO
);
2209 eps
= devm_kzalloc(&pdev
->dev
, sizeof(struct usba_ep
) * pdata
->num_ep
,
2212 return ERR_PTR(-ENOMEM
);
2214 udc
->gadget
.ep0
= &eps
[0].ep
;
2216 udc
->vbus_pin
= pdata
->vbus_pin
;
2217 udc
->vbus_pin_inverted
= pdata
->vbus_pin_inverted
;
2218 udc
->num_ep
= pdata
->num_ep
;
2220 INIT_LIST_HEAD(&eps
[0].ep
.ep_list
);
2222 for (i
= 0; i
< pdata
->num_ep
; i
++) {
2223 struct usba_ep
*ep
= &eps
[i
];
2225 ep
->ep_regs
= udc
->regs
+ USBA_EPT_BASE(i
);
2226 ep
->dma_regs
= udc
->regs
+ USBA_DMA_BASE(i
);
2227 ep
->fifo
= udc
->fifo
+ USBA_FIFO_BASE(i
);
2228 ep
->ep
.ops
= &usba_ep_ops
;
2229 ep
->ep
.name
= pdata
->ep
[i
].name
;
2230 ep
->fifo_size
= pdata
->ep
[i
].fifo_size
;
2231 usb_ep_set_maxpacket_limit(&ep
->ep
, ep
->fifo_size
);
2233 INIT_LIST_HEAD(&ep
->queue
);
2234 ep
->nr_banks
= pdata
->ep
[i
].nr_banks
;
2235 ep
->index
= pdata
->ep
[i
].index
;
2236 ep
->can_dma
= pdata
->ep
[i
].can_dma
;
2237 ep
->can_isoc
= pdata
->ep
[i
].can_isoc
;
2240 ep
->ep
.caps
.type_control
= true;
2242 ep
->ep
.caps
.type_iso
= ep
->can_isoc
;
2243 ep
->ep
.caps
.type_bulk
= true;
2244 ep
->ep
.caps
.type_int
= true;
2247 ep
->ep
.caps
.dir_in
= true;
2248 ep
->ep
.caps
.dir_out
= true;
2251 list_add_tail(&ep
->ep
.ep_list
, &udc
->gadget
.ep_list
);
2257 static int usba_udc_probe(struct platform_device
*pdev
)
2259 struct resource
*regs
, *fifo
;
2260 struct clk
*pclk
, *hclk
;
2261 struct usba_udc
*udc
;
2264 udc
= devm_kzalloc(&pdev
->dev
, sizeof(*udc
), GFP_KERNEL
);
2268 udc
->gadget
= usba_gadget_template
;
2269 INIT_LIST_HEAD(&udc
->gadget
.ep_list
);
2271 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, CTRL_IOMEM_ID
);
2272 fifo
= platform_get_resource(pdev
, IORESOURCE_MEM
, FIFO_IOMEM_ID
);
2276 irq
= platform_get_irq(pdev
, 0);
2280 pclk
= devm_clk_get(&pdev
->dev
, "pclk");
2282 return PTR_ERR(pclk
);
2283 hclk
= devm_clk_get(&pdev
->dev
, "hclk");
2285 return PTR_ERR(hclk
);
2287 spin_lock_init(&udc
->lock
);
2288 mutex_init(&udc
->vbus_mutex
);
2292 udc
->vbus_pin
= -ENODEV
;
2295 udc
->regs
= devm_ioremap(&pdev
->dev
, regs
->start
, resource_size(regs
));
2297 dev_err(&pdev
->dev
, "Unable to map I/O memory, aborting.\n");
2300 dev_info(&pdev
->dev
, "MMIO registers at 0x%08lx mapped at %p\n",
2301 (unsigned long)regs
->start
, udc
->regs
);
2302 udc
->fifo
= devm_ioremap(&pdev
->dev
, fifo
->start
, resource_size(fifo
));
2304 dev_err(&pdev
->dev
, "Unable to map FIFO, aborting.\n");
2307 dev_info(&pdev
->dev
, "FIFO at 0x%08lx mapped at %p\n",
2308 (unsigned long)fifo
->start
, udc
->fifo
);
2310 platform_set_drvdata(pdev
, udc
);
2312 /* Make sure we start from a clean slate */
2313 ret
= clk_prepare_enable(pclk
);
2315 dev_err(&pdev
->dev
, "Unable to enable pclk, aborting.\n");
2319 usba_writel(udc
, CTRL
, USBA_DISABLE_MASK
);
2320 clk_disable_unprepare(pclk
);
2322 if (pdev
->dev
.of_node
)
2323 udc
->usba_ep
= atmel_udc_of_init(pdev
, udc
);
2325 udc
->usba_ep
= usba_udc_pdata(pdev
, udc
);
2327 toggle_bias(udc
, 0);
2329 if (IS_ERR(udc
->usba_ep
))
2330 return PTR_ERR(udc
->usba_ep
);
2332 ret
= devm_request_irq(&pdev
->dev
, irq
, usba_udc_irq
, 0,
2333 "atmel_usba_udc", udc
);
2335 dev_err(&pdev
->dev
, "Cannot request irq %d (error %d)\n",
2341 if (gpio_is_valid(udc
->vbus_pin
)) {
2342 if (!devm_gpio_request(&pdev
->dev
, udc
->vbus_pin
, "atmel_usba_udc")) {
2343 irq_set_status_flags(gpio_to_irq(udc
->vbus_pin
),
2345 ret
= devm_request_threaded_irq(&pdev
->dev
,
2346 gpio_to_irq(udc
->vbus_pin
), NULL
,
2347 usba_vbus_irq_thread
, IRQF_ONESHOT
,
2348 "atmel_usba_udc", udc
);
2350 udc
->vbus_pin
= -ENODEV
;
2351 dev_warn(&udc
->pdev
->dev
,
2352 "failed to request vbus irq; "
2353 "assuming always on\n");
2356 /* gpio_request fail so use -EINVAL for gpio_is_valid */
2357 udc
->vbus_pin
= -EINVAL
;
2361 ret
= usb_add_gadget_udc(&pdev
->dev
, &udc
->gadget
);
2364 device_init_wakeup(&pdev
->dev
, 1);
2366 usba_init_debugfs(udc
);
2367 for (i
= 1; i
< udc
->num_ep
; i
++)
2368 usba_ep_init_debugfs(udc
, &udc
->usba_ep
[i
]);
2373 static int usba_udc_remove(struct platform_device
*pdev
)
2375 struct usba_udc
*udc
;
2378 udc
= platform_get_drvdata(pdev
);
2380 device_init_wakeup(&pdev
->dev
, 0);
2381 usb_del_gadget_udc(&udc
->gadget
);
2383 for (i
= 1; i
< udc
->num_ep
; i
++)
2384 usba_ep_cleanup_debugfs(&udc
->usba_ep
[i
]);
2385 usba_cleanup_debugfs(udc
);
2390 #ifdef CONFIG_PM_SLEEP
2391 static int usba_udc_suspend(struct device
*dev
)
2393 struct usba_udc
*udc
= dev_get_drvdata(dev
);
2399 mutex_lock(&udc
->vbus_mutex
);
2401 if (!device_may_wakeup(dev
)) {
2407 * Device may wake up. We stay clocked if we failed
2408 * to request vbus irq, assuming always on.
2410 if (gpio_is_valid(udc
->vbus_pin
)) {
2412 enable_irq_wake(gpio_to_irq(udc
->vbus_pin
));
2416 mutex_unlock(&udc
->vbus_mutex
);
2420 static int usba_udc_resume(struct device
*dev
)
2422 struct usba_udc
*udc
= dev_get_drvdata(dev
);
2428 if (device_may_wakeup(dev
) && gpio_is_valid(udc
->vbus_pin
))
2429 disable_irq_wake(gpio_to_irq(udc
->vbus_pin
));
2431 /* If Vbus is present, enable the controller and wait for reset */
2432 mutex_lock(&udc
->vbus_mutex
);
2433 udc
->vbus_prev
= vbus_is_present(udc
);
2436 mutex_unlock(&udc
->vbus_mutex
);
2442 static SIMPLE_DEV_PM_OPS(usba_udc_pm_ops
, usba_udc_suspend
, usba_udc_resume
);
2444 static struct platform_driver udc_driver
= {
2445 .remove
= usba_udc_remove
,
2447 .name
= "atmel_usba_udc",
2448 .pm
= &usba_udc_pm_ops
,
2449 .of_match_table
= of_match_ptr(atmel_udc_dt_ids
),
2453 module_platform_driver_probe(udc_driver
, usba_udc_probe
);
2455 MODULE_DESCRIPTION("Atmel USBA UDC driver");
2456 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
2457 MODULE_LICENSE("GPL");
2458 MODULE_ALIAS("platform:atmel_usba_udc");