1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for the Atmel USBA high speed USB device controller
5 * Copyright (C) 2005-2007 Atmel Corporation
8 #include <linux/clk/at91_pmc.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/interrupt.h>
13 #include <linux/slab.h>
14 #include <linux/device.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/list.h>
17 #include <linux/mfd/syscon.h>
18 #include <linux/platform_device.h>
19 #include <linux/regmap.h>
20 #include <linux/ctype.h>
21 #include <linux/usb/ch9.h>
22 #include <linux/usb/gadget.h>
23 #include <linux/usb/atmel_usba_udc.h>
24 #include <linux/delay.h>
26 #include <linux/of_gpio.h>
28 #include "atmel_usba_udc.h"
29 #define USBA_VBUS_IRQFLAGS (IRQF_ONESHOT \
30 | IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING)
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 inode_lock(file_inode(file
));
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 inode_unlock(file_inode(file
));
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
;
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
] = readl_relaxed(udc
->regs
+ i
* 4);
155 spin_unlock_irq(&udc
->lock
);
157 file
->private_data
= data
;
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
);
173 ret
= simple_read_from_buffer(buf
, nbytes
, ppos
,
175 file_inode(file
)->i_size
);
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_resource
= platform_get_resource(udc
->pdev
, IORESOURCE_MEM
,
271 regs
= debugfs_create_file_size("regs", 0400, root
, udc
,
273 resource_size(regs_resource
));
276 udc
->debugfs_regs
= regs
;
279 usba_ep_init_debugfs(udc
, to_usba_ep(udc
->gadget
.ep0
));
284 debugfs_remove(root
);
286 udc
->debugfs_root
= NULL
;
287 dev_err(&udc
->pdev
->dev
, "debugfs is not available\n");
290 static void usba_cleanup_debugfs(struct usba_udc
*udc
)
292 usba_ep_cleanup_debugfs(to_usba_ep(udc
->gadget
.ep0
));
293 debugfs_remove(udc
->debugfs_regs
);
294 debugfs_remove(udc
->debugfs_root
);
295 udc
->debugfs_regs
= NULL
;
296 udc
->debugfs_root
= NULL
;
299 static inline void usba_ep_init_debugfs(struct usba_udc
*udc
,
305 static inline void usba_ep_cleanup_debugfs(struct usba_ep
*ep
)
310 static inline void usba_init_debugfs(struct usba_udc
*udc
)
315 static inline void usba_cleanup_debugfs(struct usba_udc
*udc
)
321 static ushort fifo_mode
;
323 module_param(fifo_mode
, ushort
, 0x0);
324 MODULE_PARM_DESC(fifo_mode
, "Endpoint configuration mode");
326 /* mode 0 - uses autoconfig */
328 /* mode 1 - fits in 8KB, generic max fifo configuration */
329 static struct usba_fifo_cfg mode_1_cfg
[] = {
330 { .hw_ep_num
= 0, .fifo_size
= 64, .nr_banks
= 1, },
331 { .hw_ep_num
= 1, .fifo_size
= 1024, .nr_banks
= 2, },
332 { .hw_ep_num
= 2, .fifo_size
= 1024, .nr_banks
= 1, },
333 { .hw_ep_num
= 3, .fifo_size
= 1024, .nr_banks
= 1, },
334 { .hw_ep_num
= 4, .fifo_size
= 1024, .nr_banks
= 1, },
335 { .hw_ep_num
= 5, .fifo_size
= 1024, .nr_banks
= 1, },
336 { .hw_ep_num
= 6, .fifo_size
= 1024, .nr_banks
= 1, },
339 /* mode 2 - fits in 8KB, performance max fifo configuration */
340 static struct usba_fifo_cfg mode_2_cfg
[] = {
341 { .hw_ep_num
= 0, .fifo_size
= 64, .nr_banks
= 1, },
342 { .hw_ep_num
= 1, .fifo_size
= 1024, .nr_banks
= 3, },
343 { .hw_ep_num
= 2, .fifo_size
= 1024, .nr_banks
= 2, },
344 { .hw_ep_num
= 3, .fifo_size
= 1024, .nr_banks
= 2, },
347 /* mode 3 - fits in 8KB, mixed fifo configuration */
348 static struct usba_fifo_cfg mode_3_cfg
[] = {
349 { .hw_ep_num
= 0, .fifo_size
= 64, .nr_banks
= 1, },
350 { .hw_ep_num
= 1, .fifo_size
= 1024, .nr_banks
= 2, },
351 { .hw_ep_num
= 2, .fifo_size
= 512, .nr_banks
= 2, },
352 { .hw_ep_num
= 3, .fifo_size
= 512, .nr_banks
= 2, },
353 { .hw_ep_num
= 4, .fifo_size
= 512, .nr_banks
= 2, },
354 { .hw_ep_num
= 5, .fifo_size
= 512, .nr_banks
= 2, },
355 { .hw_ep_num
= 6, .fifo_size
= 512, .nr_banks
= 2, },
358 /* mode 4 - fits in 8KB, custom fifo configuration */
359 static struct usba_fifo_cfg mode_4_cfg
[] = {
360 { .hw_ep_num
= 0, .fifo_size
= 64, .nr_banks
= 1, },
361 { .hw_ep_num
= 1, .fifo_size
= 512, .nr_banks
= 2, },
362 { .hw_ep_num
= 2, .fifo_size
= 512, .nr_banks
= 2, },
363 { .hw_ep_num
= 3, .fifo_size
= 8, .nr_banks
= 2, },
364 { .hw_ep_num
= 4, .fifo_size
= 512, .nr_banks
= 2, },
365 { .hw_ep_num
= 5, .fifo_size
= 512, .nr_banks
= 2, },
366 { .hw_ep_num
= 6, .fifo_size
= 16, .nr_banks
= 2, },
367 { .hw_ep_num
= 7, .fifo_size
= 8, .nr_banks
= 2, },
368 { .hw_ep_num
= 8, .fifo_size
= 8, .nr_banks
= 2, },
370 /* Add additional configurations here */
372 static int usba_config_fifo_table(struct usba_udc
*udc
)
380 udc
->fifo_cfg
= NULL
;
384 udc
->fifo_cfg
= mode_1_cfg
;
385 n
= ARRAY_SIZE(mode_1_cfg
);
388 udc
->fifo_cfg
= mode_2_cfg
;
389 n
= ARRAY_SIZE(mode_2_cfg
);
392 udc
->fifo_cfg
= mode_3_cfg
;
393 n
= ARRAY_SIZE(mode_3_cfg
);
396 udc
->fifo_cfg
= mode_4_cfg
;
397 n
= ARRAY_SIZE(mode_4_cfg
);
400 DBG(DBG_HW
, "Setup fifo_mode %d\n", fifo_mode
);
405 static inline u32
usba_int_enb_get(struct usba_udc
*udc
)
407 return udc
->int_enb_cache
;
410 static inline void usba_int_enb_set(struct usba_udc
*udc
, u32 val
)
412 usba_writel(udc
, INT_ENB
, val
);
413 udc
->int_enb_cache
= val
;
416 static int vbus_is_present(struct usba_udc
*udc
)
418 if (gpio_is_valid(udc
->vbus_pin
))
419 return gpio_get_value(udc
->vbus_pin
) ^ udc
->vbus_pin_inverted
;
421 /* No Vbus detection: Assume always present */
425 static void toggle_bias(struct usba_udc
*udc
, int is_on
)
427 if (udc
->errata
&& udc
->errata
->toggle_bias
)
428 udc
->errata
->toggle_bias(udc
, is_on
);
431 static void generate_bias_pulse(struct usba_udc
*udc
)
433 if (!udc
->bias_pulse_needed
)
436 if (udc
->errata
&& udc
->errata
->pulse_bias
)
437 udc
->errata
->pulse_bias(udc
);
439 udc
->bias_pulse_needed
= false;
442 static void next_fifo_transaction(struct usba_ep
*ep
, struct usba_request
*req
)
444 unsigned int transaction_len
;
446 transaction_len
= req
->req
.length
- req
->req
.actual
;
447 req
->last_transaction
= 1;
448 if (transaction_len
> ep
->ep
.maxpacket
) {
449 transaction_len
= ep
->ep
.maxpacket
;
450 req
->last_transaction
= 0;
451 } else if (transaction_len
== ep
->ep
.maxpacket
&& req
->req
.zero
)
452 req
->last_transaction
= 0;
454 DBG(DBG_QUEUE
, "%s: submit_transaction, req %p (length %d)%s\n",
455 ep
->ep
.name
, req
, transaction_len
,
456 req
->last_transaction
? ", done" : "");
458 memcpy_toio(ep
->fifo
, req
->req
.buf
+ req
->req
.actual
, transaction_len
);
459 usba_ep_writel(ep
, SET_STA
, USBA_TX_PK_RDY
);
460 req
->req
.actual
+= transaction_len
;
463 static void submit_request(struct usba_ep
*ep
, struct usba_request
*req
)
465 DBG(DBG_QUEUE
, "%s: submit_request: req %p (length %d)\n",
466 ep
->ep
.name
, req
, req
->req
.length
);
471 if (req
->using_dma
) {
472 if (req
->req
.length
== 0) {
473 usba_ep_writel(ep
, CTL_ENB
, USBA_TX_PK_RDY
);
478 usba_ep_writel(ep
, CTL_ENB
, USBA_SHORT_PACKET
);
480 usba_ep_writel(ep
, CTL_DIS
, USBA_SHORT_PACKET
);
482 usba_dma_writel(ep
, ADDRESS
, req
->req
.dma
);
483 usba_dma_writel(ep
, CONTROL
, req
->ctrl
);
485 next_fifo_transaction(ep
, req
);
486 if (req
->last_transaction
) {
487 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_PK_RDY
);
488 usba_ep_writel(ep
, CTL_ENB
, USBA_TX_COMPLETE
);
490 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_COMPLETE
);
491 usba_ep_writel(ep
, CTL_ENB
, USBA_TX_PK_RDY
);
496 static void submit_next_request(struct usba_ep
*ep
)
498 struct usba_request
*req
;
500 if (list_empty(&ep
->queue
)) {
501 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_PK_RDY
| USBA_RX_BK_RDY
);
505 req
= list_entry(ep
->queue
.next
, struct usba_request
, queue
);
507 submit_request(ep
, req
);
510 static void send_status(struct usba_udc
*udc
, struct usba_ep
*ep
)
512 ep
->state
= STATUS_STAGE_IN
;
513 usba_ep_writel(ep
, SET_STA
, USBA_TX_PK_RDY
);
514 usba_ep_writel(ep
, CTL_ENB
, USBA_TX_COMPLETE
);
517 static void receive_data(struct usba_ep
*ep
)
519 struct usba_udc
*udc
= ep
->udc
;
520 struct usba_request
*req
;
521 unsigned long status
;
522 unsigned int bytecount
, nr_busy
;
525 status
= usba_ep_readl(ep
, STA
);
526 nr_busy
= USBA_BFEXT(BUSY_BANKS
, status
);
528 DBG(DBG_QUEUE
, "receive data: nr_busy=%u\n", nr_busy
);
530 while (nr_busy
> 0) {
531 if (list_empty(&ep
->queue
)) {
532 usba_ep_writel(ep
, CTL_DIS
, USBA_RX_BK_RDY
);
535 req
= list_entry(ep
->queue
.next
,
536 struct usba_request
, queue
);
538 bytecount
= USBA_BFEXT(BYTE_COUNT
, status
);
540 if (status
& (1 << 31))
542 if (req
->req
.actual
+ bytecount
>= req
->req
.length
) {
544 bytecount
= req
->req
.length
- req
->req
.actual
;
547 memcpy_fromio(req
->req
.buf
+ req
->req
.actual
,
548 ep
->fifo
, bytecount
);
549 req
->req
.actual
+= bytecount
;
551 usba_ep_writel(ep
, CLR_STA
, USBA_RX_BK_RDY
);
554 DBG(DBG_QUEUE
, "%s: request done\n", ep
->ep
.name
);
556 list_del_init(&req
->queue
);
557 usba_ep_writel(ep
, CTL_DIS
, USBA_RX_BK_RDY
);
558 spin_unlock(&udc
->lock
);
559 usb_gadget_giveback_request(&ep
->ep
, &req
->req
);
560 spin_lock(&udc
->lock
);
563 status
= usba_ep_readl(ep
, STA
);
564 nr_busy
= USBA_BFEXT(BUSY_BANKS
, status
);
566 if (is_complete
&& ep_is_control(ep
)) {
567 send_status(udc
, ep
);
574 request_complete(struct usba_ep
*ep
, struct usba_request
*req
, int status
)
576 struct usba_udc
*udc
= ep
->udc
;
578 WARN_ON(!list_empty(&req
->queue
));
580 if (req
->req
.status
== -EINPROGRESS
)
581 req
->req
.status
= status
;
584 usb_gadget_unmap_request(&udc
->gadget
, &req
->req
, ep
->is_in
);
586 DBG(DBG_GADGET
| DBG_REQ
,
587 "%s: req %p complete: status %d, actual %u\n",
588 ep
->ep
.name
, req
, req
->req
.status
, req
->req
.actual
);
590 spin_unlock(&udc
->lock
);
591 usb_gadget_giveback_request(&ep
->ep
, &req
->req
);
592 spin_lock(&udc
->lock
);
596 request_complete_list(struct usba_ep
*ep
, struct list_head
*list
, int status
)
598 struct usba_request
*req
, *tmp_req
;
600 list_for_each_entry_safe(req
, tmp_req
, list
, queue
) {
601 list_del_init(&req
->queue
);
602 request_complete(ep
, req
, status
);
607 usba_ep_enable(struct usb_ep
*_ep
, const struct usb_endpoint_descriptor
*desc
)
609 struct usba_ep
*ep
= to_usba_ep(_ep
);
610 struct usba_udc
*udc
= ep
->udc
;
611 unsigned long flags
, maxpacket
;
612 unsigned int nr_trans
;
614 DBG(DBG_GADGET
, "%s: ep_enable: desc=%p\n", ep
->ep
.name
, desc
);
616 maxpacket
= usb_endpoint_maxp(desc
);
618 if (((desc
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
) != ep
->index
)
620 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
622 || maxpacket
> ep
->fifo_size
) {
623 DBG(DBG_ERR
, "ep_enable: Invalid argument");
630 DBG(DBG_ERR
, "%s: EPT_CFG = 0x%lx (maxpacket = %lu)\n",
631 ep
->ep
.name
, ep
->ept_cfg
, maxpacket
);
633 if (usb_endpoint_dir_in(desc
)) {
635 ep
->ept_cfg
|= USBA_EPT_DIR_IN
;
638 switch (usb_endpoint_type(desc
)) {
639 case USB_ENDPOINT_XFER_CONTROL
:
640 ep
->ept_cfg
|= USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_CONTROL
);
642 case USB_ENDPOINT_XFER_ISOC
:
644 DBG(DBG_ERR
, "ep_enable: %s is not isoc capable\n",
650 * Bits 11:12 specify number of _additional_
651 * transactions per microframe.
653 nr_trans
= usb_endpoint_maxp_mult(desc
);
658 ep
->ept_cfg
|= USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_ISO
);
659 ep
->ept_cfg
|= USBA_BF(NB_TRANS
, nr_trans
);
662 case USB_ENDPOINT_XFER_BULK
:
663 ep
->ept_cfg
|= USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_BULK
);
665 case USB_ENDPOINT_XFER_INT
:
666 ep
->ept_cfg
|= USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_INT
);
670 spin_lock_irqsave(&ep
->udc
->lock
, flags
);
673 ep
->ep
.maxpacket
= maxpacket
;
675 usba_ep_writel(ep
, CFG
, ep
->ept_cfg
);
676 usba_ep_writel(ep
, CTL_ENB
, USBA_EPT_ENABLE
);
681 usba_int_enb_set(udc
, usba_int_enb_get(udc
) |
682 USBA_BF(EPT_INT
, 1 << ep
->index
) |
683 USBA_BF(DMA_INT
, 1 << ep
->index
));
684 ctrl
= USBA_AUTO_VALID
| USBA_INTDIS_DMA
;
685 usba_ep_writel(ep
, CTL_ENB
, ctrl
);
687 usba_int_enb_set(udc
, usba_int_enb_get(udc
) |
688 USBA_BF(EPT_INT
, 1 << ep
->index
));
691 spin_unlock_irqrestore(&udc
->lock
, flags
);
693 DBG(DBG_HW
, "EPT_CFG%d after init: %#08lx\n", ep
->index
,
694 (unsigned long)usba_ep_readl(ep
, CFG
));
695 DBG(DBG_HW
, "INT_ENB after init: %#08lx\n",
696 (unsigned long)usba_int_enb_get(udc
));
701 static int usba_ep_disable(struct usb_ep
*_ep
)
703 struct usba_ep
*ep
= to_usba_ep(_ep
);
704 struct usba_udc
*udc
= ep
->udc
;
708 DBG(DBG_GADGET
, "ep_disable: %s\n", ep
->ep
.name
);
710 spin_lock_irqsave(&udc
->lock
, flags
);
713 spin_unlock_irqrestore(&udc
->lock
, flags
);
714 /* REVISIT because this driver disables endpoints in
715 * reset_all_endpoints() before calling disconnect(),
716 * most gadget drivers would trigger this non-error ...
718 if (udc
->gadget
.speed
!= USB_SPEED_UNKNOWN
)
719 DBG(DBG_ERR
, "ep_disable: %s not enabled\n",
725 list_splice_init(&ep
->queue
, &req_list
);
727 usba_dma_writel(ep
, CONTROL
, 0);
728 usba_dma_writel(ep
, ADDRESS
, 0);
729 usba_dma_readl(ep
, STATUS
);
731 usba_ep_writel(ep
, CTL_DIS
, USBA_EPT_ENABLE
);
732 usba_int_enb_set(udc
, usba_int_enb_get(udc
) &
733 ~USBA_BF(EPT_INT
, 1 << ep
->index
));
735 request_complete_list(ep
, &req_list
, -ESHUTDOWN
);
737 spin_unlock_irqrestore(&udc
->lock
, flags
);
742 static struct usb_request
*
743 usba_ep_alloc_request(struct usb_ep
*_ep
, gfp_t gfp_flags
)
745 struct usba_request
*req
;
747 DBG(DBG_GADGET
, "ep_alloc_request: %p, 0x%x\n", _ep
, gfp_flags
);
749 req
= kzalloc(sizeof(*req
), gfp_flags
);
753 INIT_LIST_HEAD(&req
->queue
);
759 usba_ep_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
761 struct usba_request
*req
= to_usba_req(_req
);
763 DBG(DBG_GADGET
, "ep_free_request: %p, %p\n", _ep
, _req
);
768 static int queue_dma(struct usba_udc
*udc
, struct usba_ep
*ep
,
769 struct usba_request
*req
, gfp_t gfp_flags
)
774 DBG(DBG_DMA
, "%s: req l/%u d/%pad %c%c%c\n",
775 ep
->ep
.name
, req
->req
.length
, &req
->req
.dma
,
776 req
->req
.zero
? 'Z' : 'z',
777 req
->req
.short_not_ok
? 'S' : 's',
778 req
->req
.no_interrupt
? 'I' : 'i');
780 if (req
->req
.length
> 0x10000) {
781 /* Lengths from 0 to 65536 (inclusive) are supported */
782 DBG(DBG_ERR
, "invalid request length %u\n", req
->req
.length
);
786 ret
= usb_gadget_map_request(&udc
->gadget
, &req
->req
, ep
->is_in
);
791 req
->ctrl
= USBA_BF(DMA_BUF_LEN
, req
->req
.length
)
792 | USBA_DMA_CH_EN
| USBA_DMA_END_BUF_IE
793 | USBA_DMA_END_BUF_EN
;
796 req
->ctrl
|= USBA_DMA_END_TR_EN
| USBA_DMA_END_TR_IE
;
799 * Add this request to the queue and submit for DMA if
800 * possible. Check if we're still alive first -- we may have
801 * received a reset since last time we checked.
804 spin_lock_irqsave(&udc
->lock
, flags
);
806 if (list_empty(&ep
->queue
))
807 submit_request(ep
, req
);
809 list_add_tail(&req
->queue
, &ep
->queue
);
812 spin_unlock_irqrestore(&udc
->lock
, flags
);
818 usba_ep_queue(struct usb_ep
*_ep
, struct usb_request
*_req
, gfp_t gfp_flags
)
820 struct usba_request
*req
= to_usba_req(_req
);
821 struct usba_ep
*ep
= to_usba_ep(_ep
);
822 struct usba_udc
*udc
= ep
->udc
;
826 DBG(DBG_GADGET
| DBG_QUEUE
| DBG_REQ
, "%s: queue req %p, len %u\n",
827 ep
->ep
.name
, req
, _req
->length
);
829 if (!udc
->driver
|| udc
->gadget
.speed
== USB_SPEED_UNKNOWN
||
835 req
->last_transaction
= 0;
837 _req
->status
= -EINPROGRESS
;
841 return queue_dma(udc
, ep
, req
, gfp_flags
);
843 /* May have received a reset since last time we checked */
845 spin_lock_irqsave(&udc
->lock
, flags
);
847 list_add_tail(&req
->queue
, &ep
->queue
);
849 if ((!ep_is_control(ep
) && ep
->is_in
) ||
851 && (ep
->state
== DATA_STAGE_IN
852 || ep
->state
== STATUS_STAGE_IN
)))
853 usba_ep_writel(ep
, CTL_ENB
, USBA_TX_PK_RDY
);
855 usba_ep_writel(ep
, CTL_ENB
, USBA_RX_BK_RDY
);
858 spin_unlock_irqrestore(&udc
->lock
, flags
);
864 usba_update_req(struct usba_ep
*ep
, struct usba_request
*req
, u32 status
)
866 req
->req
.actual
= req
->req
.length
- USBA_BFEXT(DMA_BUF_LEN
, status
);
869 static int stop_dma(struct usba_ep
*ep
, u32
*pstatus
)
871 unsigned int timeout
;
875 * Stop the DMA controller. When writing both CH_EN
876 * and LINK to 0, the other bits are not affected.
878 usba_dma_writel(ep
, CONTROL
, 0);
880 /* Wait for the FIFO to empty */
881 for (timeout
= 40; timeout
; --timeout
) {
882 status
= usba_dma_readl(ep
, STATUS
);
883 if (!(status
& USBA_DMA_CH_EN
))
892 dev_err(&ep
->udc
->pdev
->dev
,
893 "%s: timed out waiting for DMA FIFO to empty\n",
901 static int usba_ep_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
903 struct usba_ep
*ep
= to_usba_ep(_ep
);
904 struct usba_udc
*udc
= ep
->udc
;
905 struct usba_request
*req
;
909 DBG(DBG_GADGET
| DBG_QUEUE
, "ep_dequeue: %s, req %p\n",
912 spin_lock_irqsave(&udc
->lock
, flags
);
914 list_for_each_entry(req
, &ep
->queue
, queue
) {
915 if (&req
->req
== _req
)
919 if (&req
->req
!= _req
) {
920 spin_unlock_irqrestore(&udc
->lock
, flags
);
924 if (req
->using_dma
) {
926 * If this request is currently being transferred,
927 * stop the DMA controller and reset the FIFO.
929 if (ep
->queue
.next
== &req
->queue
) {
930 status
= usba_dma_readl(ep
, STATUS
);
931 if (status
& USBA_DMA_CH_EN
)
932 stop_dma(ep
, &status
);
934 #ifdef CONFIG_USB_GADGET_DEBUG_FS
935 ep
->last_dma_status
= status
;
938 usba_writel(udc
, EPT_RST
, 1 << ep
->index
);
940 usba_update_req(ep
, req
, status
);
945 * Errors should stop the queue from advancing until the
946 * completion function returns.
948 list_del_init(&req
->queue
);
950 request_complete(ep
, req
, -ECONNRESET
);
952 /* Process the next request if any */
953 submit_next_request(ep
);
954 spin_unlock_irqrestore(&udc
->lock
, flags
);
959 static int usba_ep_set_halt(struct usb_ep
*_ep
, int value
)
961 struct usba_ep
*ep
= to_usba_ep(_ep
);
962 struct usba_udc
*udc
= ep
->udc
;
966 DBG(DBG_GADGET
, "endpoint %s: %s HALT\n", ep
->ep
.name
,
967 value
? "set" : "clear");
970 DBG(DBG_ERR
, "Attempted to halt uninitialized ep %s\n",
975 DBG(DBG_ERR
, "Attempted to halt isochronous ep %s\n",
980 spin_lock_irqsave(&udc
->lock
, flags
);
983 * We can't halt IN endpoints while there are still data to be
986 if (!list_empty(&ep
->queue
)
987 || ((value
&& ep
->is_in
&& (usba_ep_readl(ep
, STA
)
988 & USBA_BF(BUSY_BANKS
, -1L))))) {
992 usba_ep_writel(ep
, SET_STA
, USBA_FORCE_STALL
);
994 usba_ep_writel(ep
, CLR_STA
,
995 USBA_FORCE_STALL
| USBA_TOGGLE_CLR
);
996 usba_ep_readl(ep
, STA
);
999 spin_unlock_irqrestore(&udc
->lock
, flags
);
1004 static int usba_ep_fifo_status(struct usb_ep
*_ep
)
1006 struct usba_ep
*ep
= to_usba_ep(_ep
);
1008 return USBA_BFEXT(BYTE_COUNT
, usba_ep_readl(ep
, STA
));
1011 static void usba_ep_fifo_flush(struct usb_ep
*_ep
)
1013 struct usba_ep
*ep
= to_usba_ep(_ep
);
1014 struct usba_udc
*udc
= ep
->udc
;
1016 usba_writel(udc
, EPT_RST
, 1 << ep
->index
);
1019 static const struct usb_ep_ops usba_ep_ops
= {
1020 .enable
= usba_ep_enable
,
1021 .disable
= usba_ep_disable
,
1022 .alloc_request
= usba_ep_alloc_request
,
1023 .free_request
= usba_ep_free_request
,
1024 .queue
= usba_ep_queue
,
1025 .dequeue
= usba_ep_dequeue
,
1026 .set_halt
= usba_ep_set_halt
,
1027 .fifo_status
= usba_ep_fifo_status
,
1028 .fifo_flush
= usba_ep_fifo_flush
,
1031 static int usba_udc_get_frame(struct usb_gadget
*gadget
)
1033 struct usba_udc
*udc
= to_usba_udc(gadget
);
1035 return USBA_BFEXT(FRAME_NUMBER
, usba_readl(udc
, FNUM
));
1038 static int usba_udc_wakeup(struct usb_gadget
*gadget
)
1040 struct usba_udc
*udc
= to_usba_udc(gadget
);
1041 unsigned long flags
;
1045 spin_lock_irqsave(&udc
->lock
, flags
);
1046 if (udc
->devstatus
& (1 << USB_DEVICE_REMOTE_WAKEUP
)) {
1047 ctrl
= usba_readl(udc
, CTRL
);
1048 usba_writel(udc
, CTRL
, ctrl
| USBA_REMOTE_WAKE_UP
);
1051 spin_unlock_irqrestore(&udc
->lock
, flags
);
1057 usba_udc_set_selfpowered(struct usb_gadget
*gadget
, int is_selfpowered
)
1059 struct usba_udc
*udc
= to_usba_udc(gadget
);
1060 unsigned long flags
;
1062 gadget
->is_selfpowered
= (is_selfpowered
!= 0);
1063 spin_lock_irqsave(&udc
->lock
, flags
);
1065 udc
->devstatus
|= 1 << USB_DEVICE_SELF_POWERED
;
1067 udc
->devstatus
&= ~(1 << USB_DEVICE_SELF_POWERED
);
1068 spin_unlock_irqrestore(&udc
->lock
, flags
);
1073 static int atmel_usba_start(struct usb_gadget
*gadget
,
1074 struct usb_gadget_driver
*driver
);
1075 static int atmel_usba_stop(struct usb_gadget
*gadget
);
1077 static struct usb_ep
*atmel_usba_match_ep(struct usb_gadget
*gadget
,
1078 struct usb_endpoint_descriptor
*desc
,
1079 struct usb_ss_ep_comp_descriptor
*ep_comp
)
1084 /* Look at endpoints until an unclaimed one looks usable */
1085 list_for_each_entry(_ep
, &gadget
->ep_list
, ep_list
) {
1086 if (usb_gadget_ep_match_desc(gadget
, _ep
, desc
, ep_comp
))
1094 if (fifo_mode
== 0) {
1095 /* Optimize hw fifo size based on ep type and other info */
1096 ep
= to_usba_ep(_ep
);
1098 switch (usb_endpoint_type(desc
)) {
1099 case USB_ENDPOINT_XFER_CONTROL
:
1102 case USB_ENDPOINT_XFER_ISOC
:
1103 ep
->fifo_size
= 1024;
1107 case USB_ENDPOINT_XFER_BULK
:
1108 ep
->fifo_size
= 512;
1112 case USB_ENDPOINT_XFER_INT
:
1113 if (desc
->wMaxPacketSize
== 0)
1115 roundup_pow_of_two(_ep
->maxpacket_limit
);
1118 roundup_pow_of_two(le16_to_cpu(desc
->wMaxPacketSize
));
1123 /* It might be a little bit late to set this */
1124 usb_ep_set_maxpacket_limit(&ep
->ep
, ep
->fifo_size
);
1126 /* Generate ept_cfg basd on FIFO size and number of banks */
1127 if (ep
->fifo_size
<= 8)
1128 ep
->ept_cfg
= USBA_BF(EPT_SIZE
, USBA_EPT_SIZE_8
);
1130 /* LSB is bit 1, not 0 */
1132 USBA_BF(EPT_SIZE
, fls(ep
->fifo_size
- 1) - 3);
1134 ep
->ept_cfg
|= USBA_BF(BK_NUMBER
, ep
->nr_banks
);
1136 ep
->udc
->configured_ep
++;
1142 static const struct usb_gadget_ops usba_udc_ops
= {
1143 .get_frame
= usba_udc_get_frame
,
1144 .wakeup
= usba_udc_wakeup
,
1145 .set_selfpowered
= usba_udc_set_selfpowered
,
1146 .udc_start
= atmel_usba_start
,
1147 .udc_stop
= atmel_usba_stop
,
1148 .match_ep
= atmel_usba_match_ep
,
1151 static struct usb_endpoint_descriptor usba_ep0_desc
= {
1152 .bLength
= USB_DT_ENDPOINT_SIZE
,
1153 .bDescriptorType
= USB_DT_ENDPOINT
,
1154 .bEndpointAddress
= 0,
1155 .bmAttributes
= USB_ENDPOINT_XFER_CONTROL
,
1156 .wMaxPacketSize
= cpu_to_le16(64),
1157 /* FIXME: I have no idea what to put here */
1161 static struct usb_gadget usba_gadget_template
= {
1162 .ops
= &usba_udc_ops
,
1163 .max_speed
= USB_SPEED_HIGH
,
1164 .name
= "atmel_usba_udc",
1168 * Called with interrupts disabled and udc->lock held.
1170 static void reset_all_endpoints(struct usba_udc
*udc
)
1173 struct usba_request
*req
, *tmp_req
;
1175 usba_writel(udc
, EPT_RST
, ~0UL);
1177 ep
= to_usba_ep(udc
->gadget
.ep0
);
1178 list_for_each_entry_safe(req
, tmp_req
, &ep
->queue
, queue
) {
1179 list_del_init(&req
->queue
);
1180 request_complete(ep
, req
, -ECONNRESET
);
1184 static struct usba_ep
*get_ep_by_addr(struct usba_udc
*udc
, u16 wIndex
)
1188 if ((wIndex
& USB_ENDPOINT_NUMBER_MASK
) == 0)
1189 return to_usba_ep(udc
->gadget
.ep0
);
1191 list_for_each_entry (ep
, &udc
->gadget
.ep_list
, ep
.ep_list
) {
1192 u8 bEndpointAddress
;
1196 bEndpointAddress
= ep
->ep
.desc
->bEndpointAddress
;
1197 if ((wIndex
^ bEndpointAddress
) & USB_DIR_IN
)
1199 if ((bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
)
1200 == (wIndex
& USB_ENDPOINT_NUMBER_MASK
))
1207 /* Called with interrupts disabled and udc->lock held */
1208 static inline void set_protocol_stall(struct usba_udc
*udc
, struct usba_ep
*ep
)
1210 usba_ep_writel(ep
, SET_STA
, USBA_FORCE_STALL
);
1211 ep
->state
= WAIT_FOR_SETUP
;
1214 static inline int is_stalled(struct usba_udc
*udc
, struct usba_ep
*ep
)
1216 if (usba_ep_readl(ep
, STA
) & USBA_FORCE_STALL
)
1221 static inline void set_address(struct usba_udc
*udc
, unsigned int addr
)
1225 DBG(DBG_BUS
, "setting address %u...\n", addr
);
1226 regval
= usba_readl(udc
, CTRL
);
1227 regval
= USBA_BFINS(DEV_ADDR
, addr
, regval
);
1228 usba_writel(udc
, CTRL
, regval
);
1231 static int do_test_mode(struct usba_udc
*udc
)
1233 static const char test_packet_buffer
[] = {
1235 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1237 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
1239 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
1240 /* JJJJJJJKKKKKKK * 8 */
1241 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1242 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1244 0x7F, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD,
1245 /* {JKKKKKKK * 10}, JK */
1246 0xFC, 0x7E, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD, 0x7E
1249 struct device
*dev
= &udc
->pdev
->dev
;
1252 test_mode
= udc
->test_mode
;
1254 /* Start from a clean slate */
1255 reset_all_endpoints(udc
);
1257 switch (test_mode
) {
1260 usba_writel(udc
, TST
, USBA_TST_J_MODE
);
1261 dev_info(dev
, "Entering Test_J mode...\n");
1265 usba_writel(udc
, TST
, USBA_TST_K_MODE
);
1266 dev_info(dev
, "Entering Test_K mode...\n");
1270 * Test_SE0_NAK: Force high-speed mode and set up ep0
1271 * for Bulk IN transfers
1273 ep
= &udc
->usba_ep
[0];
1274 usba_writel(udc
, TST
,
1275 USBA_BF(SPEED_CFG
, USBA_SPEED_CFG_FORCE_HIGH
));
1276 usba_ep_writel(ep
, CFG
,
1277 USBA_BF(EPT_SIZE
, USBA_EPT_SIZE_64
)
1279 | USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_BULK
)
1280 | USBA_BF(BK_NUMBER
, 1));
1281 if (!(usba_ep_readl(ep
, CFG
) & USBA_EPT_MAPPED
)) {
1282 set_protocol_stall(udc
, ep
);
1283 dev_err(dev
, "Test_SE0_NAK: ep0 not mapped\n");
1285 usba_ep_writel(ep
, CTL_ENB
, USBA_EPT_ENABLE
);
1286 dev_info(dev
, "Entering Test_SE0_NAK mode...\n");
1291 ep
= &udc
->usba_ep
[0];
1292 usba_ep_writel(ep
, CFG
,
1293 USBA_BF(EPT_SIZE
, USBA_EPT_SIZE_64
)
1295 | USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_BULK
)
1296 | USBA_BF(BK_NUMBER
, 1));
1297 if (!(usba_ep_readl(ep
, CFG
) & USBA_EPT_MAPPED
)) {
1298 set_protocol_stall(udc
, ep
);
1299 dev_err(dev
, "Test_Packet: ep0 not mapped\n");
1301 usba_ep_writel(ep
, CTL_ENB
, USBA_EPT_ENABLE
);
1302 usba_writel(udc
, TST
, USBA_TST_PKT_MODE
);
1303 memcpy_toio(ep
->fifo
, test_packet_buffer
,
1304 sizeof(test_packet_buffer
));
1305 usba_ep_writel(ep
, SET_STA
, USBA_TX_PK_RDY
);
1306 dev_info(dev
, "Entering Test_Packet mode...\n");
1310 dev_err(dev
, "Invalid test mode: 0x%04x\n", test_mode
);
1317 /* Avoid overly long expressions */
1318 static inline bool feature_is_dev_remote_wakeup(struct usb_ctrlrequest
*crq
)
1320 if (crq
->wValue
== cpu_to_le16(USB_DEVICE_REMOTE_WAKEUP
))
1325 static inline bool feature_is_dev_test_mode(struct usb_ctrlrequest
*crq
)
1327 if (crq
->wValue
== cpu_to_le16(USB_DEVICE_TEST_MODE
))
1332 static inline bool feature_is_ep_halt(struct usb_ctrlrequest
*crq
)
1334 if (crq
->wValue
== cpu_to_le16(USB_ENDPOINT_HALT
))
1339 static int handle_ep0_setup(struct usba_udc
*udc
, struct usba_ep
*ep
,
1340 struct usb_ctrlrequest
*crq
)
1344 switch (crq
->bRequest
) {
1345 case USB_REQ_GET_STATUS
: {
1348 if (crq
->bRequestType
== (USB_DIR_IN
| USB_RECIP_DEVICE
)) {
1349 status
= cpu_to_le16(udc
->devstatus
);
1350 } else if (crq
->bRequestType
1351 == (USB_DIR_IN
| USB_RECIP_INTERFACE
)) {
1352 status
= cpu_to_le16(0);
1353 } else if (crq
->bRequestType
1354 == (USB_DIR_IN
| USB_RECIP_ENDPOINT
)) {
1355 struct usba_ep
*target
;
1357 target
= get_ep_by_addr(udc
, le16_to_cpu(crq
->wIndex
));
1362 if (is_stalled(udc
, target
))
1363 status
|= cpu_to_le16(1);
1367 /* Write directly to the FIFO. No queueing is done. */
1368 if (crq
->wLength
!= cpu_to_le16(sizeof(status
)))
1370 ep
->state
= DATA_STAGE_IN
;
1371 writew_relaxed(status
, ep
->fifo
);
1372 usba_ep_writel(ep
, SET_STA
, USBA_TX_PK_RDY
);
1376 case USB_REQ_CLEAR_FEATURE
: {
1377 if (crq
->bRequestType
== USB_RECIP_DEVICE
) {
1378 if (feature_is_dev_remote_wakeup(crq
))
1380 &= ~(1 << USB_DEVICE_REMOTE_WAKEUP
);
1382 /* Can't CLEAR_FEATURE TEST_MODE */
1384 } else if (crq
->bRequestType
== USB_RECIP_ENDPOINT
) {
1385 struct usba_ep
*target
;
1387 if (crq
->wLength
!= cpu_to_le16(0)
1388 || !feature_is_ep_halt(crq
))
1390 target
= get_ep_by_addr(udc
, le16_to_cpu(crq
->wIndex
));
1394 usba_ep_writel(target
, CLR_STA
, USBA_FORCE_STALL
);
1395 if (target
->index
!= 0)
1396 usba_ep_writel(target
, CLR_STA
,
1402 send_status(udc
, ep
);
1406 case USB_REQ_SET_FEATURE
: {
1407 if (crq
->bRequestType
== USB_RECIP_DEVICE
) {
1408 if (feature_is_dev_test_mode(crq
)) {
1409 send_status(udc
, ep
);
1410 ep
->state
= STATUS_STAGE_TEST
;
1411 udc
->test_mode
= le16_to_cpu(crq
->wIndex
);
1413 } else if (feature_is_dev_remote_wakeup(crq
)) {
1414 udc
->devstatus
|= 1 << USB_DEVICE_REMOTE_WAKEUP
;
1418 } else if (crq
->bRequestType
== USB_RECIP_ENDPOINT
) {
1419 struct usba_ep
*target
;
1421 if (crq
->wLength
!= cpu_to_le16(0)
1422 || !feature_is_ep_halt(crq
))
1425 target
= get_ep_by_addr(udc
, le16_to_cpu(crq
->wIndex
));
1429 usba_ep_writel(target
, SET_STA
, USBA_FORCE_STALL
);
1433 send_status(udc
, ep
);
1437 case USB_REQ_SET_ADDRESS
:
1438 if (crq
->bRequestType
!= (USB_DIR_OUT
| USB_RECIP_DEVICE
))
1441 set_address(udc
, le16_to_cpu(crq
->wValue
));
1442 send_status(udc
, ep
);
1443 ep
->state
= STATUS_STAGE_ADDR
;
1448 spin_unlock(&udc
->lock
);
1449 retval
= udc
->driver
->setup(&udc
->gadget
, crq
);
1450 spin_lock(&udc
->lock
);
1456 pr_err("udc: %s: Invalid setup request: %02x.%02x v%04x i%04x l%d, "
1457 "halting endpoint...\n",
1458 ep
->ep
.name
, crq
->bRequestType
, crq
->bRequest
,
1459 le16_to_cpu(crq
->wValue
), le16_to_cpu(crq
->wIndex
),
1460 le16_to_cpu(crq
->wLength
));
1461 set_protocol_stall(udc
, ep
);
1465 static void usba_control_irq(struct usba_udc
*udc
, struct usba_ep
*ep
)
1467 struct usba_request
*req
;
1472 epstatus
= usba_ep_readl(ep
, STA
);
1473 epctrl
= usba_ep_readl(ep
, CTL
);
1475 DBG(DBG_INT
, "%s [%d]: s/%08x c/%08x\n",
1476 ep
->ep
.name
, ep
->state
, epstatus
, epctrl
);
1479 if (!list_empty(&ep
->queue
))
1480 req
= list_entry(ep
->queue
.next
,
1481 struct usba_request
, queue
);
1483 if ((epctrl
& USBA_TX_PK_RDY
) && !(epstatus
& USBA_TX_PK_RDY
)) {
1485 next_fifo_transaction(ep
, req
);
1487 submit_request(ep
, req
);
1489 if (req
->last_transaction
) {
1490 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_PK_RDY
);
1491 usba_ep_writel(ep
, CTL_ENB
, USBA_TX_COMPLETE
);
1495 if ((epstatus
& epctrl
) & USBA_TX_COMPLETE
) {
1496 usba_ep_writel(ep
, CLR_STA
, USBA_TX_COMPLETE
);
1498 switch (ep
->state
) {
1500 usba_ep_writel(ep
, CTL_ENB
, USBA_RX_BK_RDY
);
1501 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_COMPLETE
);
1502 ep
->state
= STATUS_STAGE_OUT
;
1504 case STATUS_STAGE_ADDR
:
1505 /* Activate our new address */
1506 usba_writel(udc
, CTRL
, (usba_readl(udc
, CTRL
)
1508 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_COMPLETE
);
1509 ep
->state
= WAIT_FOR_SETUP
;
1511 case STATUS_STAGE_IN
:
1513 list_del_init(&req
->queue
);
1514 request_complete(ep
, req
, 0);
1515 submit_next_request(ep
);
1517 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_COMPLETE
);
1518 ep
->state
= WAIT_FOR_SETUP
;
1520 case STATUS_STAGE_TEST
:
1521 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_COMPLETE
);
1522 ep
->state
= WAIT_FOR_SETUP
;
1523 if (do_test_mode(udc
))
1524 set_protocol_stall(udc
, ep
);
1527 pr_err("udc: %s: TXCOMP: Invalid endpoint state %d, "
1528 "halting endpoint...\n",
1529 ep
->ep
.name
, ep
->state
);
1530 set_protocol_stall(udc
, ep
);
1536 if ((epstatus
& epctrl
) & USBA_RX_BK_RDY
) {
1537 switch (ep
->state
) {
1538 case STATUS_STAGE_OUT
:
1539 usba_ep_writel(ep
, CLR_STA
, USBA_RX_BK_RDY
);
1540 usba_ep_writel(ep
, CTL_DIS
, USBA_RX_BK_RDY
);
1543 list_del_init(&req
->queue
);
1544 request_complete(ep
, req
, 0);
1546 ep
->state
= WAIT_FOR_SETUP
;
1549 case DATA_STAGE_OUT
:
1554 usba_ep_writel(ep
, CLR_STA
, USBA_RX_BK_RDY
);
1555 usba_ep_writel(ep
, CTL_DIS
, USBA_RX_BK_RDY
);
1556 pr_err("udc: %s: RXRDY: Invalid endpoint state %d, "
1557 "halting endpoint...\n",
1558 ep
->ep
.name
, ep
->state
);
1559 set_protocol_stall(udc
, ep
);
1565 if (epstatus
& USBA_RX_SETUP
) {
1567 struct usb_ctrlrequest crq
;
1568 unsigned long data
[2];
1570 unsigned int pkt_len
;
1573 if (ep
->state
!= WAIT_FOR_SETUP
) {
1575 * Didn't expect a SETUP packet at this
1576 * point. Clean up any pending requests (which
1577 * may be successful).
1579 int status
= -EPROTO
;
1582 * RXRDY and TXCOMP are dropped when SETUP
1583 * packets arrive. Just pretend we received
1584 * the status packet.
1586 if (ep
->state
== STATUS_STAGE_OUT
1587 || ep
->state
== STATUS_STAGE_IN
) {
1588 usba_ep_writel(ep
, CTL_DIS
, USBA_RX_BK_RDY
);
1593 list_del_init(&req
->queue
);
1594 request_complete(ep
, req
, status
);
1598 pkt_len
= USBA_BFEXT(BYTE_COUNT
, usba_ep_readl(ep
, STA
));
1599 DBG(DBG_HW
, "Packet length: %u\n", pkt_len
);
1600 if (pkt_len
!= sizeof(crq
)) {
1601 pr_warn("udc: Invalid packet length %u (expected %zu)\n",
1602 pkt_len
, sizeof(crq
));
1603 set_protocol_stall(udc
, ep
);
1607 DBG(DBG_FIFO
, "Copying ctrl request from 0x%p:\n", ep
->fifo
);
1608 memcpy_fromio(crq
.data
, ep
->fifo
, sizeof(crq
));
1610 /* Free up one bank in the FIFO so that we can
1611 * generate or receive a reply right away. */
1612 usba_ep_writel(ep
, CLR_STA
, USBA_RX_SETUP
);
1614 /* printk(KERN_DEBUG "setup: %d: %02x.%02x\n",
1615 ep->state, crq.crq.bRequestType,
1616 crq.crq.bRequest); */
1618 if (crq
.crq
.bRequestType
& USB_DIR_IN
) {
1620 * The USB 2.0 spec states that "if wLength is
1621 * zero, there is no data transfer phase."
1622 * However, testusb #14 seems to actually
1623 * expect a data phase even if wLength = 0...
1625 ep
->state
= DATA_STAGE_IN
;
1627 if (crq
.crq
.wLength
!= cpu_to_le16(0))
1628 ep
->state
= DATA_STAGE_OUT
;
1630 ep
->state
= STATUS_STAGE_IN
;
1635 ret
= handle_ep0_setup(udc
, ep
, &crq
.crq
);
1637 spin_unlock(&udc
->lock
);
1638 ret
= udc
->driver
->setup(&udc
->gadget
, &crq
.crq
);
1639 spin_lock(&udc
->lock
);
1642 DBG(DBG_BUS
, "req %02x.%02x, length %d, state %d, ret %d\n",
1643 crq
.crq
.bRequestType
, crq
.crq
.bRequest
,
1644 le16_to_cpu(crq
.crq
.wLength
), ep
->state
, ret
);
1647 /* Let the host know that we failed */
1648 set_protocol_stall(udc
, ep
);
1653 static void usba_ep_irq(struct usba_udc
*udc
, struct usba_ep
*ep
)
1655 struct usba_request
*req
;
1659 epstatus
= usba_ep_readl(ep
, STA
);
1660 epctrl
= usba_ep_readl(ep
, CTL
);
1662 DBG(DBG_INT
, "%s: interrupt, status: 0x%08x\n", ep
->ep
.name
, epstatus
);
1664 while ((epctrl
& USBA_TX_PK_RDY
) && !(epstatus
& USBA_TX_PK_RDY
)) {
1665 DBG(DBG_BUS
, "%s: TX PK ready\n", ep
->ep
.name
);
1667 if (list_empty(&ep
->queue
)) {
1668 dev_warn(&udc
->pdev
->dev
, "ep_irq: queue empty\n");
1669 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_PK_RDY
);
1673 req
= list_entry(ep
->queue
.next
, struct usba_request
, queue
);
1675 if (req
->using_dma
) {
1676 /* Send a zero-length packet */
1677 usba_ep_writel(ep
, SET_STA
,
1679 usba_ep_writel(ep
, CTL_DIS
,
1681 list_del_init(&req
->queue
);
1682 submit_next_request(ep
);
1683 request_complete(ep
, req
, 0);
1686 next_fifo_transaction(ep
, req
);
1688 submit_request(ep
, req
);
1690 if (req
->last_transaction
) {
1691 list_del_init(&req
->queue
);
1692 submit_next_request(ep
);
1693 request_complete(ep
, req
, 0);
1697 epstatus
= usba_ep_readl(ep
, STA
);
1698 epctrl
= usba_ep_readl(ep
, CTL
);
1700 if ((epstatus
& epctrl
) & USBA_RX_BK_RDY
) {
1701 DBG(DBG_BUS
, "%s: RX data ready\n", ep
->ep
.name
);
1706 static void usba_dma_irq(struct usba_udc
*udc
, struct usba_ep
*ep
)
1708 struct usba_request
*req
;
1709 u32 status
, control
, pending
;
1711 status
= usba_dma_readl(ep
, STATUS
);
1712 control
= usba_dma_readl(ep
, CONTROL
);
1713 #ifdef CONFIG_USB_GADGET_DEBUG_FS
1714 ep
->last_dma_status
= status
;
1716 pending
= status
& control
;
1717 DBG(DBG_INT
| DBG_DMA
, "dma irq, s/%#08x, c/%#08x\n", status
, control
);
1719 if (status
& USBA_DMA_CH_EN
) {
1720 dev_err(&udc
->pdev
->dev
,
1721 "DMA_CH_EN is set after transfer is finished!\n");
1722 dev_err(&udc
->pdev
->dev
,
1723 "status=%#08x, pending=%#08x, control=%#08x\n",
1724 status
, pending
, control
);
1727 * try to pretend nothing happened. We might have to
1728 * do something here...
1732 if (list_empty(&ep
->queue
))
1733 /* Might happen if a reset comes along at the right moment */
1736 if (pending
& (USBA_DMA_END_TR_ST
| USBA_DMA_END_BUF_ST
)) {
1737 req
= list_entry(ep
->queue
.next
, struct usba_request
, queue
);
1738 usba_update_req(ep
, req
, status
);
1740 list_del_init(&req
->queue
);
1741 submit_next_request(ep
);
1742 request_complete(ep
, req
, 0);
1746 static irqreturn_t
usba_udc_irq(int irq
, void *devid
)
1748 struct usba_udc
*udc
= devid
;
1749 u32 status
, int_enb
;
1753 spin_lock(&udc
->lock
);
1755 int_enb
= usba_int_enb_get(udc
);
1756 status
= usba_readl(udc
, INT_STA
) & (int_enb
| USBA_HIGH_SPEED
);
1757 DBG(DBG_INT
, "irq, status=%#08x\n", status
);
1759 if (status
& USBA_DET_SUSPEND
) {
1760 toggle_bias(udc
, 0);
1761 usba_writel(udc
, INT_CLR
, USBA_DET_SUSPEND
);
1762 usba_int_enb_set(udc
, int_enb
| USBA_WAKE_UP
);
1763 udc
->bias_pulse_needed
= true;
1764 DBG(DBG_BUS
, "Suspend detected\n");
1765 if (udc
->gadget
.speed
!= USB_SPEED_UNKNOWN
1766 && udc
->driver
&& udc
->driver
->suspend
) {
1767 spin_unlock(&udc
->lock
);
1768 udc
->driver
->suspend(&udc
->gadget
);
1769 spin_lock(&udc
->lock
);
1773 if (status
& USBA_WAKE_UP
) {
1774 toggle_bias(udc
, 1);
1775 usba_writel(udc
, INT_CLR
, USBA_WAKE_UP
);
1776 usba_int_enb_set(udc
, int_enb
& ~USBA_WAKE_UP
);
1777 DBG(DBG_BUS
, "Wake Up CPU detected\n");
1780 if (status
& USBA_END_OF_RESUME
) {
1781 usba_writel(udc
, INT_CLR
, USBA_END_OF_RESUME
);
1782 generate_bias_pulse(udc
);
1783 DBG(DBG_BUS
, "Resume detected\n");
1784 if (udc
->gadget
.speed
!= USB_SPEED_UNKNOWN
1785 && udc
->driver
&& udc
->driver
->resume
) {
1786 spin_unlock(&udc
->lock
);
1787 udc
->driver
->resume(&udc
->gadget
);
1788 spin_lock(&udc
->lock
);
1792 dma_status
= USBA_BFEXT(DMA_INT
, status
);
1796 for (i
= 1; i
<= USBA_NR_DMAS
; i
++)
1797 if (dma_status
& (1 << i
))
1798 usba_dma_irq(udc
, &udc
->usba_ep
[i
]);
1801 ep_status
= USBA_BFEXT(EPT_INT
, status
);
1805 for (i
= 0; i
< udc
->num_ep
; i
++)
1806 if (ep_status
& (1 << i
)) {
1807 if (ep_is_control(&udc
->usba_ep
[i
]))
1808 usba_control_irq(udc
, &udc
->usba_ep
[i
]);
1810 usba_ep_irq(udc
, &udc
->usba_ep
[i
]);
1814 if (status
& USBA_END_OF_RESET
) {
1815 struct usba_ep
*ep0
, *ep
;
1818 usba_writel(udc
, INT_CLR
, USBA_END_OF_RESET
);
1819 generate_bias_pulse(udc
);
1820 reset_all_endpoints(udc
);
1822 if (udc
->gadget
.speed
!= USB_SPEED_UNKNOWN
&& udc
->driver
) {
1823 udc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1824 spin_unlock(&udc
->lock
);
1825 usb_gadget_udc_reset(&udc
->gadget
, udc
->driver
);
1826 spin_lock(&udc
->lock
);
1829 if (status
& USBA_HIGH_SPEED
)
1830 udc
->gadget
.speed
= USB_SPEED_HIGH
;
1832 udc
->gadget
.speed
= USB_SPEED_FULL
;
1833 DBG(DBG_BUS
, "%s bus reset detected\n",
1834 usb_speed_string(udc
->gadget
.speed
));
1836 ep0
= &udc
->usba_ep
[0];
1837 ep0
->ep
.desc
= &usba_ep0_desc
;
1838 ep0
->state
= WAIT_FOR_SETUP
;
1839 usba_ep_writel(ep0
, CFG
,
1840 (USBA_BF(EPT_SIZE
, EP0_EPT_SIZE
)
1841 | USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_CONTROL
)
1842 | USBA_BF(BK_NUMBER
, USBA_BK_NUMBER_ONE
)));
1843 usba_ep_writel(ep0
, CTL_ENB
,
1844 USBA_EPT_ENABLE
| USBA_RX_SETUP
);
1845 usba_int_enb_set(udc
, int_enb
| USBA_BF(EPT_INT
, 1) |
1846 USBA_DET_SUSPEND
| USBA_END_OF_RESUME
);
1849 * Unclear why we hit this irregularly, e.g. in usbtest,
1850 * but it's clearly harmless...
1852 if (!(usba_ep_readl(ep0
, CFG
) & USBA_EPT_MAPPED
))
1853 dev_err(&udc
->pdev
->dev
,
1854 "ODD: EP0 configuration is invalid!\n");
1856 /* Preallocate other endpoints */
1857 n
= fifo_mode
? udc
->num_ep
: udc
->configured_ep
;
1858 for (i
= 1; i
< n
; i
++) {
1859 ep
= &udc
->usba_ep
[i
];
1860 usba_ep_writel(ep
, CFG
, ep
->ept_cfg
);
1861 if (!(usba_ep_readl(ep
, CFG
) & USBA_EPT_MAPPED
))
1862 dev_err(&udc
->pdev
->dev
,
1863 "ODD: EP%d configuration is invalid!\n", i
);
1867 spin_unlock(&udc
->lock
);
1872 static int start_clock(struct usba_udc
*udc
)
1879 ret
= clk_prepare_enable(udc
->pclk
);
1882 ret
= clk_prepare_enable(udc
->hclk
);
1884 clk_disable_unprepare(udc
->pclk
);
1888 udc
->clocked
= true;
1892 static void stop_clock(struct usba_udc
*udc
)
1897 clk_disable_unprepare(udc
->hclk
);
1898 clk_disable_unprepare(udc
->pclk
);
1900 udc
->clocked
= false;
1903 static int usba_start(struct usba_udc
*udc
)
1905 unsigned long flags
;
1908 ret
= start_clock(udc
);
1912 spin_lock_irqsave(&udc
->lock
, flags
);
1913 toggle_bias(udc
, 1);
1914 usba_writel(udc
, CTRL
, USBA_ENABLE_MASK
);
1915 usba_int_enb_set(udc
, USBA_END_OF_RESET
);
1916 spin_unlock_irqrestore(&udc
->lock
, flags
);
1921 static void usba_stop(struct usba_udc
*udc
)
1923 unsigned long flags
;
1925 spin_lock_irqsave(&udc
->lock
, flags
);
1926 udc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1927 reset_all_endpoints(udc
);
1929 /* This will also disable the DP pullup */
1930 toggle_bias(udc
, 0);
1931 usba_writel(udc
, CTRL
, USBA_DISABLE_MASK
);
1932 spin_unlock_irqrestore(&udc
->lock
, flags
);
1937 static irqreturn_t
usba_vbus_irq_thread(int irq
, void *devid
)
1939 struct usba_udc
*udc
= devid
;
1945 mutex_lock(&udc
->vbus_mutex
);
1947 vbus
= vbus_is_present(udc
);
1948 if (vbus
!= udc
->vbus_prev
) {
1954 if (udc
->driver
->disconnect
)
1955 udc
->driver
->disconnect(&udc
->gadget
);
1957 udc
->vbus_prev
= vbus
;
1960 mutex_unlock(&udc
->vbus_mutex
);
1964 static int atmel_usba_start(struct usb_gadget
*gadget
,
1965 struct usb_gadget_driver
*driver
)
1968 struct usba_udc
*udc
= container_of(gadget
, struct usba_udc
, gadget
);
1969 unsigned long flags
;
1971 spin_lock_irqsave(&udc
->lock
, flags
);
1972 udc
->devstatus
= 1 << USB_DEVICE_SELF_POWERED
;
1973 udc
->driver
= driver
;
1974 spin_unlock_irqrestore(&udc
->lock
, flags
);
1976 mutex_lock(&udc
->vbus_mutex
);
1978 if (gpio_is_valid(udc
->vbus_pin
))
1979 enable_irq(gpio_to_irq(udc
->vbus_pin
));
1981 /* If Vbus is present, enable the controller and wait for reset */
1982 udc
->vbus_prev
= vbus_is_present(udc
);
1983 if (udc
->vbus_prev
) {
1984 ret
= usba_start(udc
);
1989 mutex_unlock(&udc
->vbus_mutex
);
1993 if (gpio_is_valid(udc
->vbus_pin
))
1994 disable_irq(gpio_to_irq(udc
->vbus_pin
));
1996 mutex_unlock(&udc
->vbus_mutex
);
1998 spin_lock_irqsave(&udc
->lock
, flags
);
1999 udc
->devstatus
&= ~(1 << USB_DEVICE_SELF_POWERED
);
2001 spin_unlock_irqrestore(&udc
->lock
, flags
);
2005 static int atmel_usba_stop(struct usb_gadget
*gadget
)
2007 struct usba_udc
*udc
= container_of(gadget
, struct usba_udc
, gadget
);
2009 if (gpio_is_valid(udc
->vbus_pin
))
2010 disable_irq(gpio_to_irq(udc
->vbus_pin
));
2013 udc
->configured_ep
= 1;
2023 static void at91sam9rl_toggle_bias(struct usba_udc
*udc
, int is_on
)
2025 regmap_update_bits(udc
->pmc
, AT91_CKGR_UCKR
, AT91_PMC_BIASEN
,
2026 is_on
? AT91_PMC_BIASEN
: 0);
2029 static void at91sam9g45_pulse_bias(struct usba_udc
*udc
)
2031 regmap_update_bits(udc
->pmc
, AT91_CKGR_UCKR
, AT91_PMC_BIASEN
, 0);
2032 regmap_update_bits(udc
->pmc
, AT91_CKGR_UCKR
, AT91_PMC_BIASEN
,
2036 static const struct usba_udc_errata at91sam9rl_errata
= {
2037 .toggle_bias
= at91sam9rl_toggle_bias
,
2040 static const struct usba_udc_errata at91sam9g45_errata
= {
2041 .pulse_bias
= at91sam9g45_pulse_bias
,
2044 static const struct of_device_id atmel_udc_dt_ids
[] = {
2045 { .compatible
= "atmel,at91sam9rl-udc", .data
= &at91sam9rl_errata
},
2046 { .compatible
= "atmel,at91sam9g45-udc", .data
= &at91sam9g45_errata
},
2047 { .compatible
= "atmel,sama5d3-udc" },
2051 MODULE_DEVICE_TABLE(of
, atmel_udc_dt_ids
);
2053 static struct usba_ep
* atmel_udc_of_init(struct platform_device
*pdev
,
2054 struct usba_udc
*udc
)
2058 enum of_gpio_flags flags
;
2059 struct device_node
*np
= pdev
->dev
.of_node
;
2060 const struct of_device_id
*match
;
2061 struct device_node
*pp
;
2063 struct usba_ep
*eps
, *ep
;
2065 match
= of_match_node(atmel_udc_dt_ids
, np
);
2067 return ERR_PTR(-EINVAL
);
2069 udc
->errata
= match
->data
;
2070 udc
->pmc
= syscon_regmap_lookup_by_compatible("atmel,at91sam9g45-pmc");
2071 if (IS_ERR(udc
->pmc
))
2072 udc
->pmc
= syscon_regmap_lookup_by_compatible("atmel,at91sam9x5-pmc");
2073 if (udc
->errata
&& IS_ERR(udc
->pmc
))
2074 return ERR_CAST(udc
->pmc
);
2078 udc
->vbus_pin
= of_get_named_gpio_flags(np
, "atmel,vbus-gpio", 0,
2080 udc
->vbus_pin_inverted
= (flags
& OF_GPIO_ACTIVE_LOW
) ? 1 : 0;
2082 if (fifo_mode
== 0) {
2084 while ((pp
= of_get_next_child(np
, pp
)))
2086 udc
->configured_ep
= 1;
2088 udc
->num_ep
= usba_config_fifo_table(udc
);
2091 eps
= devm_kzalloc(&pdev
->dev
, sizeof(struct usba_ep
) * udc
->num_ep
,
2094 return ERR_PTR(-ENOMEM
);
2096 udc
->gadget
.ep0
= &eps
[0].ep
;
2098 INIT_LIST_HEAD(&eps
[0].ep
.ep_list
);
2102 while ((pp
= of_get_next_child(np
, pp
)) && i
< udc
->num_ep
) {
2105 ret
= of_property_read_u32(pp
, "reg", &val
);
2107 dev_err(&pdev
->dev
, "of_probe: reg error(%d)\n", ret
);
2110 ep
->index
= fifo_mode
? udc
->fifo_cfg
[i
].hw_ep_num
: val
;
2112 ret
= of_property_read_u32(pp
, "atmel,fifo-size", &val
);
2114 dev_err(&pdev
->dev
, "of_probe: fifo-size error(%d)\n", ret
);
2118 if (val
< udc
->fifo_cfg
[i
].fifo_size
) {
2119 dev_warn(&pdev
->dev
,
2120 "Using max fifo-size value from DT\n");
2121 ep
->fifo_size
= val
;
2123 ep
->fifo_size
= udc
->fifo_cfg
[i
].fifo_size
;
2126 ep
->fifo_size
= val
;
2129 ret
= of_property_read_u32(pp
, "atmel,nb-banks", &val
);
2131 dev_err(&pdev
->dev
, "of_probe: nb-banks error(%d)\n", ret
);
2135 if (val
< udc
->fifo_cfg
[i
].nr_banks
) {
2136 dev_warn(&pdev
->dev
,
2137 "Using max nb-banks value from DT\n");
2140 ep
->nr_banks
= udc
->fifo_cfg
[i
].nr_banks
;
2146 ep
->can_dma
= of_property_read_bool(pp
, "atmel,can-dma");
2147 ep
->can_isoc
= of_property_read_bool(pp
, "atmel,can-isoc");
2149 ret
= of_property_read_string(pp
, "name", &name
);
2151 dev_err(&pdev
->dev
, "of_probe: name error(%d)\n", ret
);
2154 sprintf(ep
->name
, "ep%d", ep
->index
);
2155 ep
->ep
.name
= ep
->name
;
2157 ep
->ep_regs
= udc
->regs
+ USBA_EPT_BASE(i
);
2158 ep
->dma_regs
= udc
->regs
+ USBA_DMA_BASE(i
);
2159 ep
->fifo
= udc
->fifo
+ USBA_FIFO_BASE(i
);
2160 ep
->ep
.ops
= &usba_ep_ops
;
2161 usb_ep_set_maxpacket_limit(&ep
->ep
, ep
->fifo_size
);
2163 INIT_LIST_HEAD(&ep
->queue
);
2165 if (ep
->index
== 0) {
2166 ep
->ep
.caps
.type_control
= true;
2168 ep
->ep
.caps
.type_iso
= ep
->can_isoc
;
2169 ep
->ep
.caps
.type_bulk
= true;
2170 ep
->ep
.caps
.type_int
= true;
2173 ep
->ep
.caps
.dir_in
= true;
2174 ep
->ep
.caps
.dir_out
= true;
2176 if (fifo_mode
!= 0) {
2178 * Generate ept_cfg based on FIFO size and
2181 if (ep
->fifo_size
<= 8)
2182 ep
->ept_cfg
= USBA_BF(EPT_SIZE
, USBA_EPT_SIZE_8
);
2184 /* LSB is bit 1, not 0 */
2186 USBA_BF(EPT_SIZE
, fls(ep
->fifo_size
- 1) - 3);
2188 ep
->ept_cfg
|= USBA_BF(BK_NUMBER
, ep
->nr_banks
);
2192 list_add_tail(&ep
->ep
.ep_list
, &udc
->gadget
.ep_list
);
2198 dev_err(&pdev
->dev
, "of_probe: no endpoint specified\n");
2205 return ERR_PTR(ret
);
2208 static struct usba_ep
* atmel_udc_of_init(struct platform_device
*pdev
,
2209 struct usba_udc
*udc
)
2211 return ERR_PTR(-ENOSYS
);
2215 static struct usba_ep
* usba_udc_pdata(struct platform_device
*pdev
,
2216 struct usba_udc
*udc
)
2218 struct usba_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
2219 struct usba_ep
*eps
;
2223 return ERR_PTR(-ENXIO
);
2225 eps
= devm_kzalloc(&pdev
->dev
, sizeof(struct usba_ep
) * pdata
->num_ep
,
2228 return ERR_PTR(-ENOMEM
);
2230 udc
->gadget
.ep0
= &eps
[0].ep
;
2232 udc
->vbus_pin
= pdata
->vbus_pin
;
2233 udc
->vbus_pin_inverted
= pdata
->vbus_pin_inverted
;
2234 udc
->num_ep
= pdata
->num_ep
;
2236 INIT_LIST_HEAD(&eps
[0].ep
.ep_list
);
2238 for (i
= 0; i
< pdata
->num_ep
; i
++) {
2239 struct usba_ep
*ep
= &eps
[i
];
2241 ep
->ep_regs
= udc
->regs
+ USBA_EPT_BASE(i
);
2242 ep
->dma_regs
= udc
->regs
+ USBA_DMA_BASE(i
);
2243 ep
->fifo
= udc
->fifo
+ USBA_FIFO_BASE(i
);
2244 ep
->ep
.ops
= &usba_ep_ops
;
2245 ep
->ep
.name
= pdata
->ep
[i
].name
;
2246 ep
->fifo_size
= pdata
->ep
[i
].fifo_size
;
2247 usb_ep_set_maxpacket_limit(&ep
->ep
, ep
->fifo_size
);
2249 INIT_LIST_HEAD(&ep
->queue
);
2250 ep
->nr_banks
= pdata
->ep
[i
].nr_banks
;
2251 ep
->index
= pdata
->ep
[i
].index
;
2252 ep
->can_dma
= pdata
->ep
[i
].can_dma
;
2253 ep
->can_isoc
= pdata
->ep
[i
].can_isoc
;
2256 ep
->ep
.caps
.type_control
= true;
2258 ep
->ep
.caps
.type_iso
= ep
->can_isoc
;
2259 ep
->ep
.caps
.type_bulk
= true;
2260 ep
->ep
.caps
.type_int
= true;
2263 ep
->ep
.caps
.dir_in
= true;
2264 ep
->ep
.caps
.dir_out
= true;
2267 list_add_tail(&ep
->ep
.ep_list
, &udc
->gadget
.ep_list
);
2273 static int usba_udc_probe(struct platform_device
*pdev
)
2275 struct resource
*regs
, *fifo
;
2276 struct clk
*pclk
, *hclk
;
2277 struct usba_udc
*udc
;
2280 udc
= devm_kzalloc(&pdev
->dev
, sizeof(*udc
), GFP_KERNEL
);
2284 udc
->gadget
= usba_gadget_template
;
2285 INIT_LIST_HEAD(&udc
->gadget
.ep_list
);
2287 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, CTRL_IOMEM_ID
);
2288 fifo
= platform_get_resource(pdev
, IORESOURCE_MEM
, FIFO_IOMEM_ID
);
2292 irq
= platform_get_irq(pdev
, 0);
2296 pclk
= devm_clk_get(&pdev
->dev
, "pclk");
2298 return PTR_ERR(pclk
);
2299 hclk
= devm_clk_get(&pdev
->dev
, "hclk");
2301 return PTR_ERR(hclk
);
2303 spin_lock_init(&udc
->lock
);
2304 mutex_init(&udc
->vbus_mutex
);
2308 udc
->vbus_pin
= -ENODEV
;
2311 udc
->regs
= devm_ioremap(&pdev
->dev
, regs
->start
, resource_size(regs
));
2313 dev_err(&pdev
->dev
, "Unable to map I/O memory, aborting.\n");
2316 dev_info(&pdev
->dev
, "MMIO registers at 0x%08lx mapped at %p\n",
2317 (unsigned long)regs
->start
, udc
->regs
);
2318 udc
->fifo
= devm_ioremap(&pdev
->dev
, fifo
->start
, resource_size(fifo
));
2320 dev_err(&pdev
->dev
, "Unable to map FIFO, aborting.\n");
2323 dev_info(&pdev
->dev
, "FIFO at 0x%08lx mapped at %p\n",
2324 (unsigned long)fifo
->start
, udc
->fifo
);
2326 platform_set_drvdata(pdev
, udc
);
2328 /* Make sure we start from a clean slate */
2329 ret
= clk_prepare_enable(pclk
);
2331 dev_err(&pdev
->dev
, "Unable to enable pclk, aborting.\n");
2335 usba_writel(udc
, CTRL
, USBA_DISABLE_MASK
);
2336 clk_disable_unprepare(pclk
);
2338 if (pdev
->dev
.of_node
)
2339 udc
->usba_ep
= atmel_udc_of_init(pdev
, udc
);
2341 udc
->usba_ep
= usba_udc_pdata(pdev
, udc
);
2343 toggle_bias(udc
, 0);
2345 if (IS_ERR(udc
->usba_ep
))
2346 return PTR_ERR(udc
->usba_ep
);
2348 ret
= devm_request_irq(&pdev
->dev
, irq
, usba_udc_irq
, 0,
2349 "atmel_usba_udc", udc
);
2351 dev_err(&pdev
->dev
, "Cannot request irq %d (error %d)\n",
2357 if (gpio_is_valid(udc
->vbus_pin
)) {
2358 if (!devm_gpio_request(&pdev
->dev
, udc
->vbus_pin
, "atmel_usba_udc")) {
2359 irq_set_status_flags(gpio_to_irq(udc
->vbus_pin
),
2361 ret
= devm_request_threaded_irq(&pdev
->dev
,
2362 gpio_to_irq(udc
->vbus_pin
), NULL
,
2363 usba_vbus_irq_thread
, USBA_VBUS_IRQFLAGS
,
2364 "atmel_usba_udc", udc
);
2366 udc
->vbus_pin
= -ENODEV
;
2367 dev_warn(&udc
->pdev
->dev
,
2368 "failed to request vbus irq; "
2369 "assuming always on\n");
2372 /* gpio_request fail so use -EINVAL for gpio_is_valid */
2373 udc
->vbus_pin
= -EINVAL
;
2377 ret
= usb_add_gadget_udc(&pdev
->dev
, &udc
->gadget
);
2380 device_init_wakeup(&pdev
->dev
, 1);
2382 usba_init_debugfs(udc
);
2383 for (i
= 1; i
< udc
->num_ep
; i
++)
2384 usba_ep_init_debugfs(udc
, &udc
->usba_ep
[i
]);
2389 static int usba_udc_remove(struct platform_device
*pdev
)
2391 struct usba_udc
*udc
;
2394 udc
= platform_get_drvdata(pdev
);
2396 device_init_wakeup(&pdev
->dev
, 0);
2397 usb_del_gadget_udc(&udc
->gadget
);
2399 for (i
= 1; i
< udc
->num_ep
; i
++)
2400 usba_ep_cleanup_debugfs(&udc
->usba_ep
[i
]);
2401 usba_cleanup_debugfs(udc
);
2406 #ifdef CONFIG_PM_SLEEP
2407 static int usba_udc_suspend(struct device
*dev
)
2409 struct usba_udc
*udc
= dev_get_drvdata(dev
);
2415 mutex_lock(&udc
->vbus_mutex
);
2417 if (!device_may_wakeup(dev
)) {
2423 * Device may wake up. We stay clocked if we failed
2424 * to request vbus irq, assuming always on.
2426 if (gpio_is_valid(udc
->vbus_pin
)) {
2428 enable_irq_wake(gpio_to_irq(udc
->vbus_pin
));
2432 mutex_unlock(&udc
->vbus_mutex
);
2436 static int usba_udc_resume(struct device
*dev
)
2438 struct usba_udc
*udc
= dev_get_drvdata(dev
);
2444 if (device_may_wakeup(dev
) && gpio_is_valid(udc
->vbus_pin
))
2445 disable_irq_wake(gpio_to_irq(udc
->vbus_pin
));
2447 /* If Vbus is present, enable the controller and wait for reset */
2448 mutex_lock(&udc
->vbus_mutex
);
2449 udc
->vbus_prev
= vbus_is_present(udc
);
2452 mutex_unlock(&udc
->vbus_mutex
);
2458 static SIMPLE_DEV_PM_OPS(usba_udc_pm_ops
, usba_udc_suspend
, usba_udc_resume
);
2460 static struct platform_driver udc_driver
= {
2461 .remove
= usba_udc_remove
,
2463 .name
= "atmel_usba_udc",
2464 .pm
= &usba_udc_pm_ops
,
2465 .of_match_table
= of_match_ptr(atmel_udc_dt_ids
),
2469 module_platform_driver_probe(udc_driver
, usba_udc_probe
);
2471 MODULE_DESCRIPTION("Atmel USBA UDC driver");
2472 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
2473 MODULE_LICENSE("GPL");
2474 MODULE_ALIAS("platform:atmel_usba_udc");