2 * Copyright 2011, Marvell Semiconductor Inc.
3 * Lei Wen <leiwen@marvell.com>
5 * SPDX-License-Identifier: GPL-2.0+
7 * Back ported to the 8xx platform (from the 8260 platform) by
8 * Murray.Jensen@cmst.csiro.au, 27-Jan-01.
17 #include <linux/types.h>
18 #include <usb/mv_udc.h>
20 #if CONFIG_USB_MAX_CONTROLLER_COUNT > 1
21 #error This driver only supports one single controller.
25 * Check if the system has too long cachelines. If the cachelines are
26 * longer then 128b, the driver will not be able flush/invalidate data
27 * cache over separate QH entries. We use 128b because one QH entry is
28 * 64b long and there are always two QH list entries for each endpoint.
30 #if ARCH_DMA_MINALIGN > 128
31 #error This driver can not work on systems with caches longer than 128b
35 #define DBG(x...) do {} while (0)
37 #define DBG(x...) printf(x)
38 static const char *reqname(unsigned r
)
41 case USB_REQ_GET_STATUS
: return "GET_STATUS";
42 case USB_REQ_CLEAR_FEATURE
: return "CLEAR_FEATURE";
43 case USB_REQ_SET_FEATURE
: return "SET_FEATURE";
44 case USB_REQ_SET_ADDRESS
: return "SET_ADDRESS";
45 case USB_REQ_GET_DESCRIPTOR
: return "GET_DESCRIPTOR";
46 case USB_REQ_SET_DESCRIPTOR
: return "SET_DESCRIPTOR";
47 case USB_REQ_GET_CONFIGURATION
: return "GET_CONFIGURATION";
48 case USB_REQ_SET_CONFIGURATION
: return "SET_CONFIGURATION";
49 case USB_REQ_GET_INTERFACE
: return "GET_INTERFACE";
50 case USB_REQ_SET_INTERFACE
: return "SET_INTERFACE";
51 default: return "*UNKNOWN*";
56 static struct usb_endpoint_descriptor ep0_out_desc
= {
57 .bLength
= sizeof(struct usb_endpoint_descriptor
),
58 .bDescriptorType
= USB_DT_ENDPOINT
,
59 .bEndpointAddress
= 0,
60 .bmAttributes
= USB_ENDPOINT_XFER_CONTROL
,
63 static struct usb_endpoint_descriptor ep0_in_desc
= {
64 .bLength
= sizeof(struct usb_endpoint_descriptor
),
65 .bDescriptorType
= USB_DT_ENDPOINT
,
66 .bEndpointAddress
= USB_DIR_IN
,
67 .bmAttributes
= USB_ENDPOINT_XFER_CONTROL
,
70 static int mv_pullup(struct usb_gadget
*gadget
, int is_on
);
71 static int mv_ep_enable(struct usb_ep
*ep
,
72 const struct usb_endpoint_descriptor
*desc
);
73 static int mv_ep_disable(struct usb_ep
*ep
);
74 static int mv_ep_queue(struct usb_ep
*ep
,
75 struct usb_request
*req
, gfp_t gfp_flags
);
76 static struct usb_request
*
77 mv_ep_alloc_request(struct usb_ep
*ep
, unsigned int gfp_flags
);
78 static void mv_ep_free_request(struct usb_ep
*ep
, struct usb_request
*_req
);
80 static struct usb_gadget_ops mv_udc_ops
= {
84 static struct usb_ep_ops mv_ep_ops
= {
85 .enable
= mv_ep_enable
,
86 .disable
= mv_ep_disable
,
88 .alloc_request
= mv_ep_alloc_request
,
89 .free_request
= mv_ep_free_request
,
92 /* Init values for USB endpoints. */
93 static const struct usb_ep mv_ep_init
[2] = {
106 static struct mv_drv controller
= {
114 * mv_get_qh() - return queue head for endpoint
115 * @ep_num: Endpoint number
116 * @dir_in: Direction of the endpoint (IN = 1, OUT = 0)
118 * This function returns the QH associated with particular endpoint
119 * and it's direction.
121 static struct ept_queue_head
*mv_get_qh(int ep_num
, int dir_in
)
123 return &controller
.epts
[(ep_num
* 2) + dir_in
];
127 * mv_get_qtd() - return queue item for endpoint
128 * @ep_num: Endpoint number
129 * @dir_in: Direction of the endpoint (IN = 1, OUT = 0)
131 * This function returns the QH associated with particular endpoint
132 * and it's direction.
134 static struct ept_queue_item
*mv_get_qtd(int ep_num
, int dir_in
)
136 return controller
.items
[(ep_num
* 2) + dir_in
];
140 * mv_flush_qh - flush cache over queue head
141 * @ep_num: Endpoint number
143 * This function flushes cache over QH for particular endpoint.
145 static void mv_flush_qh(int ep_num
)
147 struct ept_queue_head
*head
= mv_get_qh(ep_num
, 0);
148 const uint32_t start
= (uint32_t)head
;
149 const uint32_t end
= start
+ 2 * sizeof(*head
);
151 flush_dcache_range(start
, end
);
155 * mv_invalidate_qh - invalidate cache over queue head
156 * @ep_num: Endpoint number
158 * This function invalidates cache over QH for particular endpoint.
160 static void mv_invalidate_qh(int ep_num
)
162 struct ept_queue_head
*head
= mv_get_qh(ep_num
, 0);
163 uint32_t start
= (uint32_t)head
;
164 uint32_t end
= start
+ 2 * sizeof(*head
);
166 invalidate_dcache_range(start
, end
);
170 * mv_flush_qtd - flush cache over queue item
171 * @ep_num: Endpoint number
173 * This function flushes cache over qTD pair for particular endpoint.
175 static void mv_flush_qtd(int ep_num
)
177 struct ept_queue_item
*item
= mv_get_qtd(ep_num
, 0);
178 const uint32_t start
= (uint32_t)item
;
179 const uint32_t end_raw
= start
+ 2 * sizeof(*item
);
180 const uint32_t end
= roundup(end_raw
, ARCH_DMA_MINALIGN
);
182 flush_dcache_range(start
, end
);
186 * mv_invalidate_qtd - invalidate cache over queue item
187 * @ep_num: Endpoint number
189 * This function invalidates cache over qTD pair for particular endpoint.
191 static void mv_invalidate_qtd(int ep_num
)
193 struct ept_queue_item
*item
= mv_get_qtd(ep_num
, 0);
194 const uint32_t start
= (uint32_t)item
;
195 const uint32_t end_raw
= start
+ 2 * sizeof(*item
);
196 const uint32_t end
= roundup(end_raw
, ARCH_DMA_MINALIGN
);
198 invalidate_dcache_range(start
, end
);
201 static struct usb_request
*
202 mv_ep_alloc_request(struct usb_ep
*ep
, unsigned int gfp_flags
)
204 struct mv_ep
*mv_ep
= container_of(ep
, struct mv_ep
, ep
);
208 static void mv_ep_free_request(struct usb_ep
*ep
, struct usb_request
*_req
)
213 static void ep_enable(int num
, int in
)
215 struct ept_queue_head
*head
;
216 struct mv_udc
*udc
= (struct mv_udc
*)controller
.ctrl
->hcor
;
218 head
= mv_get_qh(num
, in
);
220 n
= readl(&udc
->epctrl
[num
]);
222 n
|= (CTRL_TXE
| CTRL_TXR
| CTRL_TXT_BULK
);
224 n
|= (CTRL_RXE
| CTRL_RXR
| CTRL_RXT_BULK
);
227 head
->config
= CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE
) | CONFIG_ZLT
;
230 writel(n
, &udc
->epctrl
[num
]);
233 static int mv_ep_enable(struct usb_ep
*ep
,
234 const struct usb_endpoint_descriptor
*desc
)
236 struct mv_ep
*mv_ep
= container_of(ep
, struct mv_ep
, ep
);
238 num
= desc
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
239 in
= (desc
->bEndpointAddress
& USB_DIR_IN
) != 0;
245 static int mv_ep_disable(struct usb_ep
*ep
)
250 static int mv_bounce(struct mv_ep
*ep
)
252 uint32_t addr
= (uint32_t)ep
->req
.buf
;
255 /* Input buffer address is not aligned. */
256 if (addr
& (ARCH_DMA_MINALIGN
- 1))
259 /* Input buffer length is not aligned. */
260 if (ep
->req
.length
& (ARCH_DMA_MINALIGN
- 1))
263 /* The buffer is well aligned, only flush cache. */
264 ep
->b_len
= ep
->req
.length
;
265 ep
->b_buf
= ep
->req
.buf
;
269 /* Use internal buffer for small payloads. */
270 if (ep
->req
.length
<= 64) {
272 ep
->b_buf
= ep
->b_fast
;
274 ep
->b_len
= roundup(ep
->req
.length
, ARCH_DMA_MINALIGN
);
275 ep
->b_buf
= memalign(ARCH_DMA_MINALIGN
, ep
->b_len
);
280 memcpy(ep
->b_buf
, ep
->req
.buf
, ep
->req
.length
);
283 ba
= (uint32_t)ep
->b_buf
;
284 flush_dcache_range(ba
, ba
+ ep
->b_len
);
289 static void mv_debounce(struct mv_ep
*ep
)
291 uint32_t addr
= (uint32_t)ep
->req
.buf
;
292 uint32_t ba
= (uint32_t)ep
->b_buf
;
294 invalidate_dcache_range(ba
, ba
+ ep
->b_len
);
296 /* Input buffer address is not aligned. */
297 if (addr
& (ARCH_DMA_MINALIGN
- 1))
300 /* Input buffer length is not aligned. */
301 if (ep
->req
.length
& (ARCH_DMA_MINALIGN
- 1))
304 /* The buffer is well aligned, only invalidate cache. */
308 memcpy(ep
->req
.buf
, ep
->b_buf
, ep
->req
.length
);
310 /* Large payloads use allocated buffer, free it. */
311 if (ep
->req
.length
> 64)
315 static int mv_ep_queue(struct usb_ep
*ep
,
316 struct usb_request
*req
, gfp_t gfp_flags
)
318 struct mv_ep
*mv_ep
= container_of(ep
, struct mv_ep
, ep
);
319 struct mv_udc
*udc
= (struct mv_udc
*)controller
.ctrl
->hcor
;
320 struct ept_queue_item
*item
;
321 struct ept_queue_head
*head
;
322 int bit
, num
, len
, in
, ret
;
323 num
= mv_ep
->desc
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
324 in
= (mv_ep
->desc
->bEndpointAddress
& USB_DIR_IN
) != 0;
325 item
= mv_get_qtd(num
, in
);
326 head
= mv_get_qh(num
, in
);
329 ret
= mv_bounce(mv_ep
);
333 item
->next
= TERMINATE
;
334 item
->info
= INFO_BYTES(len
) | INFO_IOC
| INFO_ACTIVE
;
335 item
->page0
= (uint32_t)mv_ep
->b_buf
;
336 item
->page1
= ((uint32_t)mv_ep
->b_buf
& 0xfffff000) + 0x1000;
338 head
->next
= (unsigned) item
;
341 DBG("ept%d %s queue len %x, buffer %p\n",
342 num
, in
? "in" : "out", len
, mv_ep
->b_buf
);
352 writel(bit
, &udc
->epprime
);
357 static void handle_ep_complete(struct mv_ep
*ep
)
359 struct ept_queue_item
*item
;
361 num
= ep
->desc
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
362 in
= (ep
->desc
->bEndpointAddress
& USB_DIR_IN
) != 0;
364 ep
->desc
= &ep0_out_desc
;
365 item
= mv_get_qtd(num
, in
);
366 mv_invalidate_qtd(num
);
368 if (item
->info
& 0xff)
369 printf("EP%d/%s FAIL nfo=%x pg0=%x\n",
370 num
, in
? "in" : "out", item
->info
, item
->page0
);
372 len
= (item
->info
>> 16) & 0x7fff;
376 ep
->req
.length
-= len
;
377 DBG("ept%d %s complete %x\n",
378 num
, in
? "in" : "out", len
);
379 ep
->req
.complete(&ep
->ep
, &ep
->req
);
382 usb_ep_queue(&ep
->ep
, &ep
->req
, 0);
383 ep
->desc
= &ep0_in_desc
;
387 #define SETUP(type, request) (((type) << 8) | (request))
389 static void handle_setup(void)
391 struct usb_request
*req
= &controller
.ep
[0].req
;
392 struct mv_udc
*udc
= (struct mv_udc
*)controller
.ctrl
->hcor
;
393 struct ept_queue_head
*head
;
394 struct usb_ctrlrequest r
;
396 int num
, in
, _num
, _in
, i
;
398 head
= mv_get_qh(0, 0); /* EP0 OUT */
401 memcpy(&r
, head
->setup_data
, sizeof(struct usb_ctrlrequest
));
402 writel(EPT_RX(0), &udc
->epstat
);
403 DBG("handle setup %s, %x, %x index %x value %x\n", reqname(r
.bRequest
),
404 r
.bRequestType
, r
.bRequest
, r
.wIndex
, r
.wValue
);
406 switch (SETUP(r
.bRequestType
, r
.bRequest
)) {
407 case SETUP(USB_RECIP_ENDPOINT
, USB_REQ_CLEAR_FEATURE
):
408 _num
= r
.wIndex
& 15;
409 _in
= !!(r
.wIndex
& 0x80);
411 if ((r
.wValue
== 0) && (r
.wLength
== 0)) {
413 for (i
= 0; i
< NUM_ENDPOINTS
; i
++) {
414 if (!controller
.ep
[i
].desc
)
416 num
= controller
.ep
[i
].desc
->bEndpointAddress
417 & USB_ENDPOINT_NUMBER_MASK
;
418 in
= (controller
.ep
[i
].desc
->bEndpointAddress
420 if ((num
== _num
) && (in
== _in
)) {
422 usb_ep_queue(controller
.gadget
.ep0
,
430 case SETUP(USB_RECIP_DEVICE
, USB_REQ_SET_ADDRESS
):
432 * write address delayed (will take effect
433 * after the next IN txn)
435 writel((r
.wValue
<< 25) | (1 << 24), &udc
->devaddr
);
437 usb_ep_queue(controller
.gadget
.ep0
, req
, 0);
440 case SETUP(USB_DIR_IN
| USB_RECIP_DEVICE
, USB_REQ_GET_STATUS
):
442 buf
= (char *)req
->buf
;
443 buf
[0] = 1 << USB_DEVICE_SELF_POWERED
;
445 usb_ep_queue(controller
.gadget
.ep0
, req
, 0);
448 /* pass request up to the gadget driver */
449 if (controller
.driver
)
450 status
= controller
.driver
->setup(&controller
.gadget
, &r
);
456 DBG("STALL reqname %s type %x value %x, index %x\n",
457 reqname(r
.bRequest
), r
.bRequestType
, r
.wValue
, r
.wIndex
);
458 writel((1<<16) | (1 << 0), &udc
->epctrl
[0]);
461 static void stop_activity(void)
464 struct ept_queue_head
*head
;
465 struct mv_udc
*udc
= (struct mv_udc
*)controller
.ctrl
->hcor
;
466 writel(readl(&udc
->epcomp
), &udc
->epcomp
);
467 writel(readl(&udc
->epstat
), &udc
->epstat
);
468 writel(0xffffffff, &udc
->epflush
);
470 /* error out any pending reqs */
471 for (i
= 0; i
< NUM_ENDPOINTS
; i
++) {
473 writel(0, &udc
->epctrl
[i
]);
474 if (controller
.ep
[i
].desc
) {
475 num
= controller
.ep
[i
].desc
->bEndpointAddress
476 & USB_ENDPOINT_NUMBER_MASK
;
477 in
= (controller
.ep
[i
].desc
->bEndpointAddress
479 head
= mv_get_qh(num
, in
);
480 head
->info
= INFO_ACTIVE
;
488 struct mv_udc
*udc
= (struct mv_udc
*)controller
.ctrl
->hcor
;
489 unsigned n
= readl(&udc
->usbsts
);
490 writel(n
, &udc
->usbsts
);
493 n
&= (STS_SLI
| STS_URI
| STS_PCI
| STS_UI
| STS_UEI
);
498 DBG("-- reset --\n");
502 DBG("-- suspend --\n");
505 DBG("-- portchange --\n");
506 bit
= (readl(&udc
->portsc
) >> 26) & 3;
508 controller
.gadget
.speed
= USB_SPEED_HIGH
;
509 for (i
= 1; i
< NUM_ENDPOINTS
&& n
; i
++)
510 if (controller
.ep
[i
].desc
)
511 controller
.ep
[i
].ep
.maxpacket
= 512;
513 controller
.gadget
.speed
= USB_SPEED_FULL
;
518 printf("<UEI %x>\n", readl(&udc
->epcomp
));
520 if ((n
& STS_UI
) || (n
& STS_UEI
)) {
521 n
= readl(&udc
->epstat
);
525 n
= readl(&udc
->epcomp
);
527 writel(n
, &udc
->epcomp
);
529 for (i
= 0; i
< NUM_ENDPOINTS
&& n
; i
++) {
530 if (controller
.ep
[i
].desc
) {
531 num
= controller
.ep
[i
].desc
->bEndpointAddress
532 & USB_ENDPOINT_NUMBER_MASK
;
533 in
= (controller
.ep
[i
].desc
->bEndpointAddress
535 bit
= (in
) ? EPT_TX(num
) : EPT_RX(num
);
537 handle_ep_complete(&controller
.ep
[i
]);
543 int usb_gadget_handle_interrupts(void)
546 struct mv_udc
*udc
= (struct mv_udc
*)controller
.ctrl
->hcor
;
548 value
= readl(&udc
->usbsts
);
555 static int mv_pullup(struct usb_gadget
*gadget
, int is_on
)
557 struct mv_udc
*udc
= (struct mv_udc
*)controller
.ctrl
->hcor
;
560 writel(USBCMD_ITC(MICRO_8FRAME
) | USBCMD_RST
, &udc
->usbcmd
);
563 writel((unsigned)controller
.epts
, &udc
->epinitaddr
);
565 /* select DEVICE mode */
566 writel(USBMODE_DEVICE
, &udc
->usbmode
);
568 writel(0xffffffff, &udc
->epflush
);
570 /* Turn on the USB connection by enabling the pullup resistor */
571 writel(USBCMD_ITC(MICRO_8FRAME
) | USBCMD_RUN
, &udc
->usbcmd
);
574 writel(USBCMD_FS2
, &udc
->usbcmd
);
576 if (controller
.driver
)
577 controller
.driver
->disconnect(gadget
);
583 void udc_disconnect(void)
585 struct mv_udc
*udc
= (struct mv_udc
*)controller
.ctrl
->hcor
;
588 writel(USBCMD_FS2
, &udc
->usbcmd
);
590 if (controller
.driver
)
591 controller
.driver
->disconnect(&controller
.gadget
);
594 static int mvudc_probe(void)
596 struct ept_queue_head
*head
;
600 const int num
= 2 * NUM_ENDPOINTS
;
602 const int eplist_min_align
= 4096;
603 const int eplist_align
= roundup(eplist_min_align
, ARCH_DMA_MINALIGN
);
604 const int eplist_raw_sz
= num
* sizeof(struct ept_queue_head
);
605 const int eplist_sz
= roundup(eplist_raw_sz
, ARCH_DMA_MINALIGN
);
607 const int ilist_align
= roundup(ARCH_DMA_MINALIGN
, 32);
608 const int ilist_ent_raw_sz
= 2 * sizeof(struct ept_queue_item
);
609 const int ilist_ent_sz
= roundup(ilist_ent_raw_sz
, ARCH_DMA_MINALIGN
);
610 const int ilist_sz
= NUM_ENDPOINTS
* ilist_ent_sz
;
612 /* The QH list must be aligned to 4096 bytes. */
613 controller
.epts
= memalign(eplist_align
, eplist_sz
);
614 if (!controller
.epts
)
616 memset(controller
.epts
, 0, eplist_sz
);
619 * Each qTD item must be 32-byte aligned, each qTD touple must be
620 * cacheline aligned. There are two qTD items for each endpoint and
621 * only one of them is used for the endpoint at time, so we can group
624 controller
.items_mem
= memalign(ilist_align
, ilist_sz
);
625 if (!controller
.items_mem
) {
626 free(controller
.epts
);
630 for (i
= 0; i
< 2 * NUM_ENDPOINTS
; i
++) {
632 * Configure QH for each endpoint. The structure of the QH list
633 * is such that each two subsequent fields, N and N+1 where N is
634 * even, in the QH list represent QH for one endpoint. The Nth
635 * entry represents OUT configuration and the N+1th entry does
636 * represent IN configuration of the endpoint.
638 head
= controller
.epts
+ i
;
640 head
->config
= CONFIG_MAX_PKT(EP0_MAX_PACKET_SIZE
)
641 | CONFIG_ZLT
| CONFIG_IOS
;
643 head
->config
= CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE
)
645 head
->next
= TERMINATE
;
648 imem
= controller
.items_mem
+ ((i
>> 1) * ilist_ent_sz
);
650 imem
+= sizeof(struct ept_queue_item
);
652 controller
.items
[i
] = (struct ept_queue_item
*)imem
;
660 INIT_LIST_HEAD(&controller
.gadget
.ep_list
);
663 memcpy(&controller
.ep
[0].ep
, &mv_ep_init
[0], sizeof(*mv_ep_init
));
664 controller
.ep
[0].desc
= &ep0_in_desc
;
665 controller
.gadget
.ep0
= &controller
.ep
[0].ep
;
666 INIT_LIST_HEAD(&controller
.gadget
.ep0
->ep_list
);
669 for (i
= 1; i
< NUM_ENDPOINTS
; i
++) {
670 memcpy(&controller
.ep
[i
].ep
, &mv_ep_init
[1],
671 sizeof(*mv_ep_init
));
672 list_add_tail(&controller
.ep
[i
].ep
.ep_list
,
673 &controller
.gadget
.ep_list
);
679 int usb_gadget_register_driver(struct usb_gadget_driver
*driver
)
686 if (!driver
->bind
|| !driver
->setup
|| !driver
->disconnect
)
688 if (driver
->speed
!= USB_SPEED_FULL
&& driver
->speed
!= USB_SPEED_HIGH
)
691 ret
= usb_lowlevel_init(0, (void **)&controller
.ctrl
);
697 udc
= (struct mv_udc
*)controller
.ctrl
->hcor
;
699 /* select ULPI phy */
700 writel(PTS(PTS_ENABLE
) | PFSC
, &udc
->portsc
);
703 ret
= driver
->bind(&controller
.gadget
);
705 DBG("driver->bind() returned %d\n", ret
);
708 controller
.driver
= driver
;
713 int usb_gadget_unregister_driver(struct usb_gadget_driver
*driver
)