2 * Driver for the Atmel USBA high speed USB device controller
3 * [Original from Linux kernel: drivers/usb/gadget/atmel_usba_udc.c]
5 * Copyright (C) 2005-2013 Atmel Corporation
6 * Bo Shen <voice.shen@atmel.com>
8 * SPDX-License-Identifier: GPL-2.0+
12 #include <asm/errno.h>
14 #include <asm/hardware.h>
15 #include <linux/list.h>
16 #include <linux/usb/ch9.h>
17 #include <linux/usb/gadget.h>
18 #include <linux/usb/atmel_usba_udc.h>
20 #include <usb/lin_gadget_compat.h>
22 #include "atmel_usba_udc.h"
24 static int vbus_is_present(struct usba_udc
*udc
)
26 /* No Vbus detection: Assume always present */
30 static void next_fifo_transaction(struct usba_ep
*ep
, struct usba_request
*req
)
32 unsigned int transaction_len
;
34 transaction_len
= req
->req
.length
- req
->req
.actual
;
35 req
->last_transaction
= 1;
36 if (transaction_len
> ep
->ep
.maxpacket
) {
37 transaction_len
= ep
->ep
.maxpacket
;
38 req
->last_transaction
= 0;
39 } else if (transaction_len
== ep
->ep
.maxpacket
&& req
->req
.zero
) {
40 req
->last_transaction
= 0;
43 DBG(DBG_QUEUE
, "%s: submit_transaction, req %p (length %d)%s\n",
44 ep
->ep
.name
, req
, transaction_len
,
45 req
->last_transaction
? ", done" : "");
47 memcpy(ep
->fifo
, req
->req
.buf
+ req
->req
.actual
, transaction_len
);
48 usba_ep_writel(ep
, SET_STA
, USBA_TX_PK_RDY
);
49 req
->req
.actual
+= transaction_len
;
52 static void submit_request(struct usba_ep
*ep
, struct usba_request
*req
)
54 DBG(DBG_QUEUE
, "%s: submit_request: req %p (length %d), dma: %d\n",
55 ep
->ep
.name
, req
, req
->req
.length
, req
->using_dma
);
60 next_fifo_transaction(ep
, req
);
61 if (req
->last_transaction
) {
62 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_PK_RDY
);
63 usba_ep_writel(ep
, CTL_ENB
, USBA_TX_COMPLETE
);
65 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_COMPLETE
);
66 usba_ep_writel(ep
, CTL_ENB
, USBA_TX_PK_RDY
);
70 static void submit_next_request(struct usba_ep
*ep
)
72 struct usba_request
*req
;
74 if (list_empty(&ep
->queue
)) {
75 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_PK_RDY
| USBA_RX_BK_RDY
);
79 req
= list_entry(ep
->queue
.next
, struct usba_request
, queue
);
81 submit_request(ep
, req
);
84 static void send_status(struct usba_udc
*udc
, struct usba_ep
*ep
)
86 ep
->state
= STATUS_STAGE_IN
;
87 usba_ep_writel(ep
, SET_STA
, USBA_TX_PK_RDY
);
88 usba_ep_writel(ep
, CTL_ENB
, USBA_TX_COMPLETE
);
91 static void receive_data(struct usba_ep
*ep
)
93 struct usba_udc
*udc
= ep
->udc
;
94 struct usba_request
*req
;
96 unsigned int bytecount
, nr_busy
;
99 status
= usba_ep_readl(ep
, STA
);
100 nr_busy
= USBA_BFEXT(BUSY_BANKS
, status
);
102 DBG(DBG_QUEUE
, "receive data: nr_busy=%u\n", nr_busy
);
104 while (nr_busy
> 0) {
105 if (list_empty(&ep
->queue
)) {
106 usba_ep_writel(ep
, CTL_DIS
, USBA_RX_BK_RDY
);
109 req
= list_entry(ep
->queue
.next
,
110 struct usba_request
, queue
);
112 bytecount
= USBA_BFEXT(BYTE_COUNT
, status
);
114 if (status
& USBA_SHORT_PACKET
)
116 if (req
->req
.actual
+ bytecount
>= req
->req
.length
) {
118 bytecount
= req
->req
.length
- req
->req
.actual
;
121 memcpy(req
->req
.buf
+ req
->req
.actual
, ep
->fifo
, bytecount
);
122 req
->req
.actual
+= bytecount
;
124 usba_ep_writel(ep
, CLR_STA
, USBA_RX_BK_RDY
);
127 DBG(DBG_QUEUE
, "%s: request done\n", ep
->ep
.name
);
129 list_del_init(&req
->queue
);
130 usba_ep_writel(ep
, CTL_DIS
, USBA_RX_BK_RDY
);
131 spin_lock(&udc
->lock
);
132 req
->req
.complete(&ep
->ep
, &req
->req
);
133 spin_unlock(&udc
->lock
);
136 status
= usba_ep_readl(ep
, STA
);
137 nr_busy
= USBA_BFEXT(BUSY_BANKS
, status
);
139 if (is_complete
&& ep_is_control(ep
)) {
140 send_status(udc
, ep
);
147 request_complete(struct usba_ep
*ep
, struct usba_request
*req
, int status
)
149 if (req
->req
.status
== -EINPROGRESS
)
150 req
->req
.status
= status
;
152 DBG(DBG_GADGET
| DBG_REQ
, "%s: req %p complete: status %d, actual %u\n",
153 ep
->ep
.name
, req
, req
->req
.status
, req
->req
.actual
);
155 req
->req
.complete(&ep
->ep
, &req
->req
);
159 request_complete_list(struct usba_ep
*ep
, struct list_head
*list
, int status
)
161 struct usba_request
*req
, *tmp_req
;
163 list_for_each_entry_safe(req
, tmp_req
, list
, queue
) {
164 list_del_init(&req
->queue
);
165 request_complete(ep
, req
, status
);
170 usba_ep_enable(struct usb_ep
*_ep
, const struct usb_endpoint_descriptor
*desc
)
172 struct usba_ep
*ep
= to_usba_ep(_ep
);
173 struct usba_udc
*udc
= ep
->udc
;
174 unsigned long flags
, ept_cfg
, maxpacket
;
175 unsigned int nr_trans
;
177 DBG(DBG_GADGET
, "%s: ep_enable: desc=%p\n", ep
->ep
.name
, desc
);
179 maxpacket
= usb_endpoint_maxp(desc
) & 0x7ff;
181 if (((desc
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
)
184 desc
->bDescriptorType
!= USB_DT_ENDPOINT
||
186 maxpacket
> ep
->fifo_size
) {
187 DBG(DBG_ERR
, "ep_enable: Invalid argument");
195 ept_cfg
= USBA_BF(EPT_SIZE
, USBA_EPT_SIZE_8
);
197 /* LSB is bit 1, not 0 */
198 ept_cfg
= USBA_BF(EPT_SIZE
, fls(maxpacket
- 1) - 3);
200 DBG(DBG_HW
, "%s: EPT_SIZE = %lu (maxpacket = %lu)\n",
201 ep
->ep
.name
, ept_cfg
, maxpacket
);
203 if (usb_endpoint_dir_in(desc
)) {
205 ept_cfg
|= USBA_EPT_DIR_IN
;
208 switch (usb_endpoint_type(desc
)) {
209 case USB_ENDPOINT_XFER_CONTROL
:
210 ept_cfg
|= USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_CONTROL
);
211 ept_cfg
|= USBA_BF(BK_NUMBER
, USBA_BK_NUMBER_ONE
);
213 case USB_ENDPOINT_XFER_ISOC
:
215 DBG(DBG_ERR
, "ep_enable: %s is not isoc capable\n",
221 * Bits 11:12 specify number of _additional_
222 * transactions per microframe.
224 nr_trans
= ((usb_endpoint_maxp(desc
) >> 11) & 3) + 1;
229 ept_cfg
|= USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_ISO
);
232 * Do triple-buffering on high-bandwidth iso endpoints.
234 if (nr_trans
> 1 && ep
->nr_banks
== 3)
235 ept_cfg
|= USBA_BF(BK_NUMBER
, USBA_BK_NUMBER_TRIPLE
);
237 ept_cfg
|= USBA_BF(BK_NUMBER
, USBA_BK_NUMBER_DOUBLE
);
238 ept_cfg
|= USBA_BF(NB_TRANS
, nr_trans
);
240 case USB_ENDPOINT_XFER_BULK
:
241 ept_cfg
|= USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_BULK
);
242 ept_cfg
|= USBA_BF(BK_NUMBER
, USBA_BK_NUMBER_ONE
);
244 case USB_ENDPOINT_XFER_INT
:
245 ept_cfg
|= USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_INT
);
246 ept_cfg
|= USBA_BF(BK_NUMBER
, USBA_BK_NUMBER_ONE
);
250 spin_lock_irqsave(&ep
->udc
->lock
, flags
);
253 ep
->ep
.maxpacket
= maxpacket
;
255 usba_ep_writel(ep
, CFG
, ept_cfg
);
256 usba_ep_writel(ep
, CTL_ENB
, USBA_EPT_ENABLE
);
258 usba_writel(udc
, INT_ENB
,
259 (usba_readl(udc
, INT_ENB
)
260 | USBA_BF(EPT_INT
, 1 << ep
->index
)));
262 spin_unlock_irqrestore(&udc
->lock
, flags
);
264 DBG(DBG_HW
, "EPT_CFG%d after init: %#08lx\n", ep
->index
,
265 (unsigned long)usba_ep_readl(ep
, CFG
));
266 DBG(DBG_HW
, "INT_ENB after init: %#08lx\n",
267 (unsigned long)usba_readl(udc
, INT_ENB
));
272 static int usba_ep_disable(struct usb_ep
*_ep
)
274 struct usba_ep
*ep
= to_usba_ep(_ep
);
275 struct usba_udc
*udc
= ep
->udc
;
279 DBG(DBG_GADGET
, "ep_disable: %s\n", ep
->ep
.name
);
281 spin_lock_irqsave(&udc
->lock
, flags
);
284 spin_unlock_irqrestore(&udc
->lock
, flags
);
285 /* REVISIT because this driver disables endpoints in
286 * reset_all_endpoints() before calling disconnect(),
287 * most gadget drivers would trigger this non-error ...
289 if (udc
->gadget
.speed
!= USB_SPEED_UNKNOWN
)
290 DBG(DBG_ERR
, "ep_disable: %s not enabled\n",
296 list_splice_init(&ep
->queue
, &req_list
);
297 usba_ep_writel(ep
, CFG
, 0);
298 usba_ep_writel(ep
, CTL_DIS
, USBA_EPT_ENABLE
);
299 usba_writel(udc
, INT_ENB
,
300 usba_readl(udc
, INT_ENB
) &
301 ~USBA_BF(EPT_INT
, 1 << ep
->index
));
303 request_complete_list(ep
, &req_list
, -ESHUTDOWN
);
305 spin_unlock_irqrestore(&udc
->lock
, flags
);
310 static struct usb_request
*
311 usba_ep_alloc_request(struct usb_ep
*_ep
, gfp_t gfp_flags
)
313 struct usba_request
*req
;
315 DBG(DBG_GADGET
, "ep_alloc_request: %p, 0x%x\n", _ep
, gfp_flags
);
317 req
= malloc(sizeof(struct usba_request
));
321 INIT_LIST_HEAD(&req
->queue
);
327 usba_ep_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
329 struct usba_request
*req
= to_usba_req(_req
);
331 DBG(DBG_GADGET
, "ep_free_request: %p, %p\n", _ep
, _req
);
337 usba_ep_queue(struct usb_ep
*_ep
, struct usb_request
*_req
, gfp_t gfp_flags
)
339 struct usba_request
*req
= to_usba_req(_req
);
340 struct usba_ep
*ep
= to_usba_ep(_ep
);
341 struct usba_udc
*udc
= ep
->udc
;
345 DBG(DBG_GADGET
| DBG_QUEUE
| DBG_REQ
, "%s: queue req %p, len %u\n",
346 ep
->ep
.name
, req
, _req
->length
);
348 if (!udc
->driver
|| udc
->gadget
.speed
== USB_SPEED_UNKNOWN
||
354 req
->last_transaction
= 0;
356 _req
->status
= -EINPROGRESS
;
359 /* May have received a reset since last time we checked */
361 spin_lock_irqsave(&udc
->lock
, flags
);
363 list_add_tail(&req
->queue
, &ep
->queue
);
365 if ((!ep_is_control(ep
) && ep
->is_in
) ||
366 (ep_is_control(ep
) && (ep
->state
== DATA_STAGE_IN
||
367 ep
->state
== STATUS_STAGE_IN
)))
368 usba_ep_writel(ep
, CTL_ENB
, USBA_TX_PK_RDY
);
370 usba_ep_writel(ep
, CTL_ENB
, USBA_RX_BK_RDY
);
374 spin_unlock_irqrestore(&udc
->lock
, flags
);
379 static int usba_ep_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
381 struct usba_ep
*ep
= to_usba_ep(_ep
);
382 struct usba_request
*req
= to_usba_req(_req
);
384 DBG(DBG_GADGET
| DBG_QUEUE
, "ep_dequeue: %s, req %p\n",
388 * Errors should stop the queue from advancing until the
389 * completion function returns.
391 list_del_init(&req
->queue
);
393 request_complete(ep
, req
, -ECONNRESET
);
395 /* Process the next request if any */
396 submit_next_request(ep
);
401 static int usba_ep_set_halt(struct usb_ep
*_ep
, int value
)
403 struct usba_ep
*ep
= to_usba_ep(_ep
);
407 DBG(DBG_GADGET
, "endpoint %s: %s HALT\n", ep
->ep
.name
,
408 value
? "set" : "clear");
411 DBG(DBG_ERR
, "Attempted to halt uninitialized ep %s\n",
417 DBG(DBG_ERR
, "Attempted to halt isochronous ep %s\n",
422 spin_lock_irqsave(&udc
->lock
, flags
);
425 * We can't halt IN endpoints while there are still data to be
428 if (!list_empty(&ep
->queue
) ||
429 ((value
&& ep
->is_in
&& (usba_ep_readl(ep
, STA
) &
430 USBA_BF(BUSY_BANKS
, -1L))))) {
434 usba_ep_writel(ep
, SET_STA
, USBA_FORCE_STALL
);
436 usba_ep_writel(ep
, CLR_STA
,
437 USBA_FORCE_STALL
| USBA_TOGGLE_CLR
);
438 usba_ep_readl(ep
, STA
);
441 spin_unlock_irqrestore(&udc
->lock
, flags
);
446 static int usba_ep_fifo_status(struct usb_ep
*_ep
)
448 struct usba_ep
*ep
= to_usba_ep(_ep
);
450 return USBA_BFEXT(BYTE_COUNT
, usba_ep_readl(ep
, STA
));
453 static void usba_ep_fifo_flush(struct usb_ep
*_ep
)
455 struct usba_ep
*ep
= to_usba_ep(_ep
);
456 struct usba_udc
*udc
= ep
->udc
;
458 usba_writel(udc
, EPT_RST
, 1 << ep
->index
);
461 static const struct usb_ep_ops usba_ep_ops
= {
462 .enable
= usba_ep_enable
,
463 .disable
= usba_ep_disable
,
464 .alloc_request
= usba_ep_alloc_request
,
465 .free_request
= usba_ep_free_request
,
466 .queue
= usba_ep_queue
,
467 .dequeue
= usba_ep_dequeue
,
468 .set_halt
= usba_ep_set_halt
,
469 .fifo_status
= usba_ep_fifo_status
,
470 .fifo_flush
= usba_ep_fifo_flush
,
473 static int usba_udc_get_frame(struct usb_gadget
*gadget
)
475 struct usba_udc
*udc
= to_usba_udc(gadget
);
477 return USBA_BFEXT(FRAME_NUMBER
, usba_readl(udc
, FNUM
));
480 static int usba_udc_wakeup(struct usb_gadget
*gadget
)
482 struct usba_udc
*udc
= to_usba_udc(gadget
);
487 spin_lock_irqsave(&udc
->lock
, flags
);
488 if (udc
->devstatus
& (1 << USB_DEVICE_REMOTE_WAKEUP
)) {
489 ctrl
= usba_readl(udc
, CTRL
);
490 usba_writel(udc
, CTRL
, ctrl
| USBA_REMOTE_WAKE_UP
);
493 spin_unlock_irqrestore(&udc
->lock
, flags
);
499 usba_udc_set_selfpowered(struct usb_gadget
*gadget
, int is_selfpowered
)
501 struct usba_udc
*udc
= to_usba_udc(gadget
);
504 spin_lock_irqsave(&udc
->lock
, flags
);
506 udc
->devstatus
|= 1 << USB_DEVICE_SELF_POWERED
;
508 udc
->devstatus
&= ~(1 << USB_DEVICE_SELF_POWERED
);
509 spin_unlock_irqrestore(&udc
->lock
, flags
);
514 static const struct usb_gadget_ops usba_udc_ops
= {
515 .get_frame
= usba_udc_get_frame
,
516 .wakeup
= usba_udc_wakeup
,
517 .set_selfpowered
= usba_udc_set_selfpowered
,
520 static struct usb_endpoint_descriptor usba_ep0_desc
= {
521 .bLength
= USB_DT_ENDPOINT_SIZE
,
522 .bDescriptorType
= USB_DT_ENDPOINT
,
523 .bEndpointAddress
= 0,
524 .bmAttributes
= USB_ENDPOINT_XFER_CONTROL
,
525 .wMaxPacketSize
= cpu_to_le16(64),
526 /* FIXME: I have no idea what to put here */
531 * Called with interrupts disabled and udc->lock held.
533 static void reset_all_endpoints(struct usba_udc
*udc
)
536 struct usba_request
*req
, *tmp_req
;
538 usba_writel(udc
, EPT_RST
, ~0UL);
540 ep
= to_usba_ep(udc
->gadget
.ep0
);
541 list_for_each_entry_safe(req
, tmp_req
, &ep
->queue
, queue
) {
542 list_del_init(&req
->queue
);
543 request_complete(ep
, req
, -ECONNRESET
);
546 /* NOTE: normally, the next call to the gadget driver is in
547 * charge of disabling endpoints... usually disconnect().
548 * The exception would be entering a high speed test mode.
550 * FIXME remove this code ... and retest thoroughly.
552 list_for_each_entry(ep
, &udc
->gadget
.ep_list
, ep
.ep_list
) {
554 spin_unlock(&udc
->lock
);
555 usba_ep_disable(&ep
->ep
);
556 spin_lock(&udc
->lock
);
561 static struct usba_ep
*get_ep_by_addr(struct usba_udc
*udc
, u16 wIndex
)
565 if ((wIndex
& USB_ENDPOINT_NUMBER_MASK
) == 0)
566 return to_usba_ep(udc
->gadget
.ep0
);
568 list_for_each_entry(ep
, &udc
->gadget
.ep_list
, ep
.ep_list
) {
573 bEndpointAddress
= ep
->desc
->bEndpointAddress
;
574 if ((wIndex
^ bEndpointAddress
) & USB_DIR_IN
)
576 if ((bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
)
577 == (wIndex
& USB_ENDPOINT_NUMBER_MASK
))
584 /* Called with interrupts disabled and udc->lock held */
585 static inline void set_protocol_stall(struct usba_udc
*udc
, struct usba_ep
*ep
)
587 usba_ep_writel(ep
, SET_STA
, USBA_FORCE_STALL
);
588 ep
->state
= WAIT_FOR_SETUP
;
591 static inline int is_stalled(struct usba_udc
*udc
, struct usba_ep
*ep
)
593 if (usba_ep_readl(ep
, STA
) & USBA_FORCE_STALL
)
598 static inline void set_address(struct usba_udc
*udc
, unsigned int addr
)
602 DBG(DBG_BUS
, "setting address %u...\n", addr
);
603 regval
= usba_readl(udc
, CTRL
);
604 regval
= USBA_BFINS(DEV_ADDR
, addr
, regval
);
605 usba_writel(udc
, CTRL
, regval
);
608 static int do_test_mode(struct usba_udc
*udc
)
610 static const char test_packet_buffer
[] = {
612 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
614 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
616 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
617 /* JJJJJJJKKKKKKK * 8 */
618 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
619 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
621 0x7F, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD,
622 /* {JKKKKKKK * 10}, JK */
623 0xFC, 0x7E, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD, 0x7E
628 test_mode
= udc
->test_mode
;
630 /* Start from a clean slate */
631 reset_all_endpoints(udc
);
636 usba_writel(udc
, TST
, USBA_TST_J_MODE
);
637 DBG(DBG_ALL
, "Entering Test_J mode...\n");
641 usba_writel(udc
, TST
, USBA_TST_K_MODE
);
642 DBG(DBG_ALL
, "Entering Test_K mode...\n");
646 * Test_SE0_NAK: Force high-speed mode and set up ep0
647 * for Bulk IN transfers
649 ep
= &udc
->usba_ep
[0];
650 usba_writel(udc
, TST
,
651 USBA_BF(SPEED_CFG
, USBA_SPEED_CFG_FORCE_HIGH
));
652 usba_ep_writel(ep
, CFG
,
653 USBA_BF(EPT_SIZE
, USBA_EPT_SIZE_64
)
655 | USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_BULK
)
656 | USBA_BF(BK_NUMBER
, 1));
657 if (!(usba_ep_readl(ep
, CFG
) & USBA_EPT_MAPPED
)) {
658 set_protocol_stall(udc
, ep
);
659 DBG(DBG_ALL
, "Test_SE0_NAK: ep0 not mapped\n");
661 usba_ep_writel(ep
, CTL_ENB
, USBA_EPT_ENABLE
);
662 DBG(DBG_ALL
, "Entering Test_SE0_NAK mode...\n");
667 ep
= &udc
->usba_ep
[0];
668 usba_ep_writel(ep
, CFG
,
669 USBA_BF(EPT_SIZE
, USBA_EPT_SIZE_64
)
671 | USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_BULK
)
672 | USBA_BF(BK_NUMBER
, 1));
673 if (!(usba_ep_readl(ep
, CFG
) & USBA_EPT_MAPPED
)) {
674 set_protocol_stall(udc
, ep
);
675 DBG(DBG_ALL
, "Test_Packet: ep0 not mapped\n");
677 usba_ep_writel(ep
, CTL_ENB
, USBA_EPT_ENABLE
);
678 usba_writel(udc
, TST
, USBA_TST_PKT_MODE
);
679 memcpy(ep
->fifo
, test_packet_buffer
,
680 sizeof(test_packet_buffer
));
681 usba_ep_writel(ep
, SET_STA
, USBA_TX_PK_RDY
);
682 DBG(DBG_ALL
, "Entering Test_Packet mode...\n");
686 DBG(DBG_ERR
, "Invalid test mode: 0x%04x\n", test_mode
);
693 /* Avoid overly long expressions */
694 static inline bool feature_is_dev_remote_wakeup(struct usb_ctrlrequest
*crq
)
696 if (crq
->wValue
== cpu_to_le16(USB_DEVICE_REMOTE_WAKEUP
))
701 static inline bool feature_is_dev_test_mode(struct usb_ctrlrequest
*crq
)
703 if (crq
->wValue
== cpu_to_le16(USB_DEVICE_TEST_MODE
))
708 static inline bool feature_is_ep_halt(struct usb_ctrlrequest
*crq
)
710 if (crq
->wValue
== cpu_to_le16(USB_ENDPOINT_HALT
))
715 static int handle_ep0_setup(struct usba_udc
*udc
, struct usba_ep
*ep
,
716 struct usb_ctrlrequest
*crq
)
720 switch (crq
->bRequest
) {
721 case USB_REQ_GET_STATUS
: {
724 if (crq
->bRequestType
== (USB_DIR_IN
| USB_RECIP_DEVICE
)) {
725 status
= cpu_to_le16(udc
->devstatus
);
726 } else if (crq
->bRequestType
727 == (USB_DIR_IN
| USB_RECIP_INTERFACE
)) {
728 status
= cpu_to_le16(0);
729 } else if (crq
->bRequestType
730 == (USB_DIR_IN
| USB_RECIP_ENDPOINT
)) {
731 struct usba_ep
*target
;
733 target
= get_ep_by_addr(udc
, le16_to_cpu(crq
->wIndex
));
738 if (is_stalled(udc
, target
))
739 status
|= cpu_to_le16(1);
744 /* Write directly to the FIFO. No queueing is done. */
745 if (crq
->wLength
!= cpu_to_le16(sizeof(status
)))
747 ep
->state
= DATA_STAGE_IN
;
748 __raw_writew(status
, ep
->fifo
);
749 usba_ep_writel(ep
, SET_STA
, USBA_TX_PK_RDY
);
753 case USB_REQ_CLEAR_FEATURE
: {
754 if (crq
->bRequestType
== USB_RECIP_DEVICE
) {
755 if (feature_is_dev_remote_wakeup(crq
))
757 &= ~(1 << USB_DEVICE_REMOTE_WAKEUP
);
759 /* Can't CLEAR_FEATURE TEST_MODE */
761 } else if (crq
->bRequestType
== USB_RECIP_ENDPOINT
) {
762 struct usba_ep
*target
;
764 if (crq
->wLength
!= cpu_to_le16(0) ||
765 !feature_is_ep_halt(crq
))
767 target
= get_ep_by_addr(udc
, le16_to_cpu(crq
->wIndex
));
771 usba_ep_writel(target
, CLR_STA
, USBA_FORCE_STALL
);
772 if (target
->index
!= 0)
773 usba_ep_writel(target
, CLR_STA
,
779 send_status(udc
, ep
);
783 case USB_REQ_SET_FEATURE
: {
784 if (crq
->bRequestType
== USB_RECIP_DEVICE
) {
785 if (feature_is_dev_test_mode(crq
)) {
786 send_status(udc
, ep
);
787 ep
->state
= STATUS_STAGE_TEST
;
788 udc
->test_mode
= le16_to_cpu(crq
->wIndex
);
790 } else if (feature_is_dev_remote_wakeup(crq
)) {
791 udc
->devstatus
|= 1 << USB_DEVICE_REMOTE_WAKEUP
;
795 } else if (crq
->bRequestType
== USB_RECIP_ENDPOINT
) {
796 struct usba_ep
*target
;
798 if (crq
->wLength
!= cpu_to_le16(0) ||
799 !feature_is_ep_halt(crq
))
802 target
= get_ep_by_addr(udc
, le16_to_cpu(crq
->wIndex
));
806 usba_ep_writel(target
, SET_STA
, USBA_FORCE_STALL
);
811 send_status(udc
, ep
);
815 case USB_REQ_SET_ADDRESS
:
816 if (crq
->bRequestType
!= (USB_DIR_OUT
| USB_RECIP_DEVICE
))
819 set_address(udc
, le16_to_cpu(crq
->wValue
));
820 send_status(udc
, ep
);
821 ep
->state
= STATUS_STAGE_ADDR
;
826 spin_unlock(&udc
->lock
);
827 retval
= udc
->driver
->setup(&udc
->gadget
, crq
);
828 spin_lock(&udc
->lock
);
834 DBG(DBG_ALL
, "%s: Invalid setup request: %02x.%02x v%04x i%04x l%d\n",
835 ep
->ep
.name
, crq
->bRequestType
, crq
->bRequest
,
836 le16_to_cpu(crq
->wValue
), le16_to_cpu(crq
->wIndex
),
837 le16_to_cpu(crq
->wLength
));
838 set_protocol_stall(udc
, ep
);
843 static void usba_control_irq(struct usba_udc
*udc
, struct usba_ep
*ep
)
845 struct usba_request
*req
;
850 epstatus
= usba_ep_readl(ep
, STA
);
851 epctrl
= usba_ep_readl(ep
, CTL
);
853 DBG(DBG_INT
, "%s [%d]: s/%08x c/%08x\n",
854 ep
->ep
.name
, ep
->state
, epstatus
, epctrl
);
857 if (!list_empty(&ep
->queue
))
858 req
= list_entry(ep
->queue
.next
,
859 struct usba_request
, queue
);
861 if ((epctrl
& USBA_TX_PK_RDY
) && !(epstatus
& USBA_TX_PK_RDY
)) {
863 next_fifo_transaction(ep
, req
);
865 submit_request(ep
, req
);
867 if (req
->last_transaction
) {
868 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_PK_RDY
);
869 usba_ep_writel(ep
, CTL_ENB
, USBA_TX_COMPLETE
);
873 if ((epstatus
& epctrl
) & USBA_TX_COMPLETE
) {
874 usba_ep_writel(ep
, CLR_STA
, USBA_TX_COMPLETE
);
878 usba_ep_writel(ep
, CTL_ENB
, USBA_RX_BK_RDY
);
879 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_COMPLETE
);
880 ep
->state
= STATUS_STAGE_OUT
;
882 case STATUS_STAGE_ADDR
:
883 /* Activate our new address */
884 usba_writel(udc
, CTRL
, (usba_readl(udc
, CTRL
)
886 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_COMPLETE
);
887 ep
->state
= WAIT_FOR_SETUP
;
889 case STATUS_STAGE_IN
:
891 list_del_init(&req
->queue
);
892 request_complete(ep
, req
, 0);
893 submit_next_request(ep
);
895 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_COMPLETE
);
896 ep
->state
= WAIT_FOR_SETUP
;
898 case STATUS_STAGE_TEST
:
899 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_COMPLETE
);
900 ep
->state
= WAIT_FOR_SETUP
;
901 if (do_test_mode(udc
))
902 set_protocol_stall(udc
, ep
);
905 DBG(DBG_ALL
, "%s: TXCOMP: Invalid endpoint state %d\n",
906 ep
->ep
.name
, ep
->state
);
907 set_protocol_stall(udc
, ep
);
913 if ((epstatus
& epctrl
) & USBA_RX_BK_RDY
) {
915 case STATUS_STAGE_OUT
:
916 usba_ep_writel(ep
, CLR_STA
, USBA_RX_BK_RDY
);
917 usba_ep_writel(ep
, CTL_DIS
, USBA_RX_BK_RDY
);
920 list_del_init(&req
->queue
);
921 request_complete(ep
, req
, 0);
923 ep
->state
= WAIT_FOR_SETUP
;
931 usba_ep_writel(ep
, CLR_STA
, USBA_RX_BK_RDY
);
932 usba_ep_writel(ep
, CTL_DIS
, USBA_RX_BK_RDY
);
933 DBG(DBG_ALL
, "%s: RXRDY: Invalid endpoint state %d\n",
934 ep
->ep
.name
, ep
->state
);
935 set_protocol_stall(udc
, ep
);
941 if (epstatus
& USBA_RX_SETUP
) {
943 struct usb_ctrlrequest crq
;
944 unsigned long data
[2];
946 unsigned int pkt_len
;
949 if (ep
->state
!= WAIT_FOR_SETUP
) {
951 * Didn't expect a SETUP packet at this
952 * point. Clean up any pending requests (which
953 * may be successful).
955 int status
= -EPROTO
;
958 * RXRDY and TXCOMP are dropped when SETUP
959 * packets arrive. Just pretend we received
962 if (ep
->state
== STATUS_STAGE_OUT
||
963 ep
->state
== STATUS_STAGE_IN
) {
964 usba_ep_writel(ep
, CTL_DIS
, USBA_RX_BK_RDY
);
969 list_del_init(&req
->queue
);
970 request_complete(ep
, req
, status
);
974 pkt_len
= USBA_BFEXT(BYTE_COUNT
, usba_ep_readl(ep
, STA
));
975 DBG(DBG_HW
, "Packet length: %u\n", pkt_len
);
976 if (pkt_len
!= sizeof(crq
)) {
977 DBG(DBG_ALL
, "udc: Invalid length %u (expected %zu)\n",
978 pkt_len
, sizeof(crq
));
979 set_protocol_stall(udc
, ep
);
983 DBG(DBG_FIFO
, "Copying ctrl request from 0x%p:\n", ep
->fifo
);
984 memcpy(crq
.data
, ep
->fifo
, sizeof(crq
));
986 /* Free up one bank in the FIFO so that we can
987 * generate or receive a reply right away. */
988 usba_ep_writel(ep
, CLR_STA
, USBA_RX_SETUP
);
990 if (crq
.crq
.bRequestType
& USB_DIR_IN
) {
992 * The USB 2.0 spec states that "if wLength is
993 * zero, there is no data transfer phase."
994 * However, testusb #14 seems to actually
995 * expect a data phase even if wLength = 0...
997 ep
->state
= DATA_STAGE_IN
;
999 if (crq
.crq
.wLength
!= cpu_to_le16(0))
1000 ep
->state
= DATA_STAGE_OUT
;
1002 ep
->state
= STATUS_STAGE_IN
;
1006 if (ep
->index
== 0) {
1007 ret
= handle_ep0_setup(udc
, ep
, &crq
.crq
);
1009 spin_unlock(&udc
->lock
);
1010 ret
= udc
->driver
->setup(&udc
->gadget
, &crq
.crq
);
1011 spin_lock(&udc
->lock
);
1014 DBG(DBG_BUS
, "req %02x.%02x, length %d, state %d, ret %d\n",
1015 crq
.crq
.bRequestType
, crq
.crq
.bRequest
,
1016 le16_to_cpu(crq
.crq
.wLength
), ep
->state
, ret
);
1019 /* Let the host know that we failed */
1020 set_protocol_stall(udc
, ep
);
1025 static void usba_ep_irq(struct usba_udc
*udc
, struct usba_ep
*ep
)
1027 struct usba_request
*req
;
1031 epstatus
= usba_ep_readl(ep
, STA
);
1032 epctrl
= usba_ep_readl(ep
, CTL
);
1034 DBG(DBG_INT
, "%s: interrupt, status: 0x%08x\n", ep
->ep
.name
, epstatus
);
1036 while ((epctrl
& USBA_TX_PK_RDY
) && !(epstatus
& USBA_TX_PK_RDY
)) {
1037 DBG(DBG_BUS
, "%s: TX PK ready\n", ep
->ep
.name
);
1039 if (list_empty(&ep
->queue
)) {
1040 DBG(DBG_INT
, "ep_irq: queue empty\n");
1041 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_PK_RDY
);
1045 req
= list_entry(ep
->queue
.next
, struct usba_request
, queue
);
1048 next_fifo_transaction(ep
, req
);
1050 submit_request(ep
, req
);
1052 if (req
->last_transaction
) {
1053 list_del_init(&req
->queue
);
1054 submit_next_request(ep
);
1055 request_complete(ep
, req
, 0);
1058 epstatus
= usba_ep_readl(ep
, STA
);
1059 epctrl
= usba_ep_readl(ep
, CTL
);
1062 if ((epstatus
& epctrl
) & USBA_RX_BK_RDY
) {
1063 DBG(DBG_BUS
, "%s: RX data ready\n", ep
->ep
.name
);
1065 usba_ep_writel(ep
, CLR_STA
, USBA_RX_BK_RDY
);
1069 static int usba_udc_irq(struct usba_udc
*udc
)
1071 u32 status
, ep_status
;
1073 spin_lock(&udc
->lock
);
1075 status
= usba_readl(udc
, INT_STA
);
1076 DBG(DBG_INT
, "irq, status=%#08x\n", status
);
1078 if (status
& USBA_DET_SUSPEND
) {
1079 usba_writel(udc
, INT_CLR
, USBA_DET_SUSPEND
);
1080 DBG(DBG_BUS
, "Suspend detected\n");
1081 if (udc
->gadget
.speed
!= USB_SPEED_UNKNOWN
&&
1082 udc
->driver
&& udc
->driver
->suspend
) {
1083 spin_unlock(&udc
->lock
);
1084 udc
->driver
->suspend(&udc
->gadget
);
1085 spin_lock(&udc
->lock
);
1089 if (status
& USBA_WAKE_UP
) {
1090 usba_writel(udc
, INT_CLR
, USBA_WAKE_UP
);
1091 DBG(DBG_BUS
, "Wake Up CPU detected\n");
1094 if (status
& USBA_END_OF_RESUME
) {
1095 usba_writel(udc
, INT_CLR
, USBA_END_OF_RESUME
);
1096 DBG(DBG_BUS
, "Resume detected\n");
1097 if (udc
->gadget
.speed
!= USB_SPEED_UNKNOWN
&&
1098 udc
->driver
&& udc
->driver
->resume
) {
1099 spin_unlock(&udc
->lock
);
1100 udc
->driver
->resume(&udc
->gadget
);
1101 spin_lock(&udc
->lock
);
1105 ep_status
= USBA_BFEXT(EPT_INT
, status
);
1109 for (i
= 0; i
< USBA_NR_ENDPOINTS
; i
++)
1110 if (ep_status
& (1 << i
)) {
1111 if (ep_is_control(&udc
->usba_ep
[i
]))
1112 usba_control_irq(udc
, &udc
->usba_ep
[i
]);
1114 usba_ep_irq(udc
, &udc
->usba_ep
[i
]);
1118 if (status
& USBA_END_OF_RESET
) {
1119 struct usba_ep
*ep0
;
1121 usba_writel(udc
, INT_CLR
, USBA_END_OF_RESET
);
1122 reset_all_endpoints(udc
);
1124 if (udc
->gadget
.speed
!= USB_SPEED_UNKNOWN
&&
1125 udc
->driver
->disconnect
) {
1126 udc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1127 spin_unlock(&udc
->lock
);
1128 udc
->driver
->disconnect(&udc
->gadget
);
1129 spin_lock(&udc
->lock
);
1132 if (status
& USBA_HIGH_SPEED
)
1133 udc
->gadget
.speed
= USB_SPEED_HIGH
;
1135 udc
->gadget
.speed
= USB_SPEED_FULL
;
1137 ep0
= &udc
->usba_ep
[0];
1138 ep0
->desc
= &usba_ep0_desc
;
1139 ep0
->state
= WAIT_FOR_SETUP
;
1140 usba_ep_writel(ep0
, CFG
,
1141 (USBA_BF(EPT_SIZE
, EP0_EPT_SIZE
)
1142 | USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_CONTROL
)
1143 | USBA_BF(BK_NUMBER
, USBA_BK_NUMBER_ONE
)));
1144 usba_ep_writel(ep0
, CTL_ENB
,
1145 USBA_EPT_ENABLE
| USBA_RX_SETUP
);
1146 usba_writel(udc
, INT_ENB
,
1147 (usba_readl(udc
, INT_ENB
)
1148 | USBA_BF(EPT_INT
, 1)
1150 | USBA_END_OF_RESUME
));
1153 * Unclear why we hit this irregularly, e.g. in usbtest,
1154 * but it's clearly harmless...
1156 if (!(usba_ep_readl(ep0
, CFG
) & USBA_EPT_MAPPED
))
1157 DBG(DBG_ALL
, "ODD: EP0 configuration is invalid!\n");
1160 spin_unlock(&udc
->lock
);
1165 static int atmel_usba_start(struct usba_udc
*udc
)
1167 udc
->devstatus
= 1 << USB_DEVICE_SELF_POWERED
;
1171 /* If Vbus is present, enable the controller and wait for reset */
1172 if (vbus_is_present(udc
) && udc
->vbus_prev
== 0) {
1173 usba_writel(udc
, CTRL
, USBA_ENABLE_MASK
);
1174 usba_writel(udc
, INT_ENB
, USBA_END_OF_RESET
);
1180 static int atmel_usba_stop(struct usba_udc
*udc
)
1182 udc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1183 reset_all_endpoints(udc
);
1185 /* This will also disable the DP pullup */
1186 usba_writel(udc
, CTRL
, USBA_DISABLE_MASK
);
1191 static struct usba_udc controller
= {
1192 .regs
= (unsigned *)ATMEL_BASE_UDPHS
,
1193 .fifo
= (unsigned *)ATMEL_BASE_UDPHS_FIFO
,
1195 .ops
= &usba_udc_ops
,
1196 .ep_list
= LIST_HEAD_INIT(controller
.gadget
.ep_list
),
1197 .speed
= USB_SPEED_HIGH
,
1199 .name
= "atmel_usba_udc",
1203 int usb_gadget_handle_interrupts(void)
1205 struct usba_udc
*udc
= &controller
;
1207 return usba_udc_irq(udc
);
1211 int usb_gadget_register_driver(struct usb_gadget_driver
*driver
)
1213 struct usba_udc
*udc
= &controller
;
1216 if (!driver
|| !driver
->bind
|| !driver
->setup
) {
1217 printf("bad paramter\n");
1222 printf("UDC already has a gadget driver\n");
1226 atmel_usba_start(udc
);
1228 udc
->driver
= driver
;
1230 ret
= driver
->bind(&udc
->gadget
);
1232 error("driver->bind() returned %d\n", ret
);
1239 int usb_gadget_unregister_driver(struct usb_gadget_driver
*driver
)
1241 struct usba_udc
*udc
= &controller
;
1243 if (!driver
|| !driver
->unbind
|| !driver
->disconnect
) {
1244 error("bad paramter\n");
1248 driver
->disconnect(&udc
->gadget
);
1249 driver
->unbind(&udc
->gadget
);
1252 atmel_usba_stop(udc
);
1257 static struct usba_ep
*usba_udc_pdata(struct usba_platform_data
*pdata
,
1258 struct usba_udc
*udc
)
1260 struct usba_ep
*eps
;
1263 eps
= malloc(sizeof(struct usba_ep
) * pdata
->num_ep
);
1265 error("failed to alloc eps\n");
1269 udc
->gadget
.ep0
= &eps
[0].ep
;
1271 INIT_LIST_HEAD(&udc
->gadget
.ep_list
);
1272 INIT_LIST_HEAD(&eps
[0].ep
.ep_list
);
1274 for (i
= 0; i
< pdata
->num_ep
; i
++) {
1275 struct usba_ep
*ep
= &eps
[i
];
1277 ep
->ep_regs
= udc
->regs
+ USBA_EPT_BASE(i
);
1278 ep
->dma_regs
= udc
->regs
+ USBA_DMA_BASE(i
);
1279 ep
->fifo
= udc
->fifo
+ USBA_FIFO_BASE(i
);
1280 ep
->ep
.ops
= &usba_ep_ops
;
1281 ep
->ep
.name
= pdata
->ep
[i
].name
;
1282 ep
->ep
.maxpacket
= pdata
->ep
[i
].fifo_size
;
1283 ep
->fifo_size
= ep
->ep
.maxpacket
;
1285 INIT_LIST_HEAD(&ep
->queue
);
1286 ep
->nr_banks
= pdata
->ep
[i
].nr_banks
;
1287 ep
->index
= pdata
->ep
[i
].index
;
1288 ep
->can_dma
= pdata
->ep
[i
].can_dma
;
1289 ep
->can_isoc
= pdata
->ep
[i
].can_isoc
;
1291 list_add_tail(&ep
->ep
.ep_list
, &udc
->gadget
.ep_list
);
1297 int usba_udc_probe(struct usba_platform_data
*pdata
)
1299 struct usba_udc
*udc
;
1303 udc
->usba_ep
= usba_udc_pdata(pdata
, udc
);