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 <asm/unaligned.h>
18 #include <linux/types.h>
19 #include <usb/mv_udc.h>
22 * Check if the system has too long cachelines. If the cachelines are
23 * longer then 128b, the driver will not be able flush/invalidate data
24 * cache over separate QH entries. We use 128b because one QH entry is
25 * 64b long and there are always two QH list entries for each endpoint.
27 #if ARCH_DMA_MINALIGN > 128
28 #error This driver can not work on systems with caches longer than 128b
32 #define DBG(x...) do {} while (0)
34 #define DBG(x...) printf(x)
35 static const char *reqname(unsigned r)
38 case USB_REQ_GET_STATUS: return "GET_STATUS";
39 case USB_REQ_CLEAR_FEATURE: return "CLEAR_FEATURE";
40 case USB_REQ_SET_FEATURE: return "SET_FEATURE";
41 case USB_REQ_SET_ADDRESS: return "SET_ADDRESS";
42 case USB_REQ_GET_DESCRIPTOR: return "GET_DESCRIPTOR";
43 case USB_REQ_SET_DESCRIPTOR: return "SET_DESCRIPTOR";
44 case USB_REQ_GET_CONFIGURATION: return "GET_CONFIGURATION";
45 case USB_REQ_SET_CONFIGURATION: return "SET_CONFIGURATION";
46 case USB_REQ_GET_INTERFACE: return "GET_INTERFACE";
47 case USB_REQ_SET_INTERFACE: return "SET_INTERFACE";
48 default: return "*UNKNOWN*";
53 static struct usb_endpoint_descriptor ep0_out_desc = {
54 .bLength = sizeof(struct usb_endpoint_descriptor),
55 .bDescriptorType = USB_DT_ENDPOINT,
56 .bEndpointAddress = 0,
57 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
60 static struct usb_endpoint_descriptor ep0_in_desc = {
61 .bLength = sizeof(struct usb_endpoint_descriptor),
62 .bDescriptorType = USB_DT_ENDPOINT,
63 .bEndpointAddress = USB_DIR_IN,
64 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
67 static int mv_pullup(struct usb_gadget *gadget, int is_on);
68 static int mv_ep_enable(struct usb_ep *ep,
69 const struct usb_endpoint_descriptor *desc);
70 static int mv_ep_disable(struct usb_ep *ep);
71 static int mv_ep_queue(struct usb_ep *ep,
72 struct usb_request *req, gfp_t gfp_flags);
73 static struct usb_request *
74 mv_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags);
75 static void mv_ep_free_request(struct usb_ep *ep, struct usb_request *_req);
77 static struct usb_gadget_ops mv_udc_ops = {
81 static struct usb_ep_ops mv_ep_ops = {
82 .enable = mv_ep_enable,
83 .disable = mv_ep_disable,
85 .alloc_request = mv_ep_alloc_request,
86 .free_request = mv_ep_free_request,
89 /* Init values for USB endpoints. */
90 static const struct usb_ep mv_ep_init[2] = {
103 static struct mv_drv controller = {
112 * mv_get_qh() - return queue head for endpoint
113 * @ep_num: Endpoint number
114 * @dir_in: Direction of the endpoint (IN = 1, OUT = 0)
116 * This function returns the QH associated with particular endpoint
117 * and it's direction.
119 static struct ept_queue_head *mv_get_qh(int ep_num, int dir_in)
121 return &controller.epts[(ep_num * 2) + dir_in];
125 * mv_get_qtd() - return queue item for endpoint
126 * @ep_num: Endpoint number
127 * @dir_in: Direction of the endpoint (IN = 1, OUT = 0)
129 * This function returns the QH associated with particular endpoint
130 * and it's direction.
132 static struct ept_queue_item *mv_get_qtd(int ep_num, int dir_in)
134 return controller.items[(ep_num * 2) + dir_in];
138 * mv_flush_qh - flush cache over queue head
139 * @ep_num: Endpoint number
141 * This function flushes cache over QH for particular endpoint.
143 static void mv_flush_qh(int ep_num)
145 struct ept_queue_head *head = mv_get_qh(ep_num, 0);
146 const uint32_t start = (uint32_t)head;
147 const uint32_t end = start + 2 * sizeof(*head);
149 flush_dcache_range(start, end);
153 * mv_invalidate_qh - invalidate cache over queue head
154 * @ep_num: Endpoint number
156 * This function invalidates cache over QH for particular endpoint.
158 static void mv_invalidate_qh(int ep_num)
160 struct ept_queue_head *head = mv_get_qh(ep_num, 0);
161 uint32_t start = (uint32_t)head;
162 uint32_t end = start + 2 * sizeof(*head);
164 invalidate_dcache_range(start, end);
168 * mv_flush_qtd - flush cache over queue item
169 * @ep_num: Endpoint number
171 * This function flushes cache over qTD pair for particular endpoint.
173 static void mv_flush_qtd(int ep_num)
175 struct ept_queue_item *item = mv_get_qtd(ep_num, 0);
176 const uint32_t start = (uint32_t)item;
177 const uint32_t end_raw = start + 2 * sizeof(*item);
178 const uint32_t end = roundup(end_raw, ARCH_DMA_MINALIGN);
180 flush_dcache_range(start, end);
184 * mv_invalidate_qtd - invalidate cache over queue item
185 * @ep_num: Endpoint number
187 * This function invalidates cache over qTD pair for particular endpoint.
189 static void mv_invalidate_qtd(int ep_num)
191 struct ept_queue_item *item = mv_get_qtd(ep_num, 0);
192 const uint32_t start = (uint32_t)item;
193 const uint32_t end_raw = start + 2 * sizeof(*item);
194 const uint32_t end = roundup(end_raw, ARCH_DMA_MINALIGN);
196 invalidate_dcache_range(start, end);
199 static struct usb_request *
200 mv_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags)
202 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
206 static void mv_ep_free_request(struct usb_ep *ep, struct usb_request *_req)
211 static void ep_enable(int num, int in, int maxpacket)
213 struct ept_queue_head *head;
214 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
216 head = mv_get_qh(num, in);
218 n = readl(&udc->epctrl[num]);
220 n |= (CTRL_TXE | CTRL_TXR | CTRL_TXT_BULK);
222 n |= (CTRL_RXE | CTRL_RXR | CTRL_RXT_BULK);
225 head->config = CONFIG_MAX_PKT(maxpacket) | CONFIG_ZLT;
228 writel(n, &udc->epctrl[num]);
231 static int mv_ep_enable(struct usb_ep *ep,
232 const struct usb_endpoint_descriptor *desc)
234 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
236 num = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
237 in = (desc->bEndpointAddress & USB_DIR_IN) != 0;
241 int max = get_unaligned_le16(&desc->wMaxPacketSize);
243 if ((max > 64) && (controller.gadget.speed == USB_SPEED_FULL))
245 if (ep->maxpacket != max) {
246 DBG("%s: from %d to %d\n", __func__,
251 ep_enable(num, in, ep->maxpacket);
252 DBG("%s: num=%d maxpacket=%d\n", __func__, num, ep->maxpacket);
256 static int mv_ep_disable(struct usb_ep *ep)
258 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
264 static int mv_bounce(struct mv_ep *ep)
266 uint32_t addr = (uint32_t)ep->req.buf;
269 /* Input buffer address is not aligned. */
270 if (addr & (ARCH_DMA_MINALIGN - 1))
273 /* Input buffer length is not aligned. */
274 if (ep->req.length & (ARCH_DMA_MINALIGN - 1))
277 /* The buffer is well aligned, only flush cache. */
278 ep->b_len = ep->req.length;
279 ep->b_buf = ep->req.buf;
283 /* Use internal buffer for small payloads. */
284 if (ep->req.length <= 64) {
286 ep->b_buf = ep->b_fast;
288 ep->b_len = roundup(ep->req.length, ARCH_DMA_MINALIGN);
289 ep->b_buf = memalign(ARCH_DMA_MINALIGN, ep->b_len);
294 memcpy(ep->b_buf, ep->req.buf, ep->req.length);
297 ba = (uint32_t)ep->b_buf;
298 flush_dcache_range(ba, ba + ep->b_len);
303 static void mv_debounce(struct mv_ep *ep)
305 uint32_t addr = (uint32_t)ep->req.buf;
306 uint32_t ba = (uint32_t)ep->b_buf;
308 invalidate_dcache_range(ba, ba + ep->b_len);
310 /* Input buffer address is not aligned. */
311 if (addr & (ARCH_DMA_MINALIGN - 1))
314 /* Input buffer length is not aligned. */
315 if (ep->req.length & (ARCH_DMA_MINALIGN - 1))
318 /* The buffer is well aligned, only invalidate cache. */
322 memcpy(ep->req.buf, ep->b_buf, ep->req.length);
324 /* Large payloads use allocated buffer, free it. */
325 if (ep->req.length > 64)
329 static int mv_ep_queue(struct usb_ep *ep,
330 struct usb_request *req, gfp_t gfp_flags)
332 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
333 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
334 struct ept_queue_item *item;
335 struct ept_queue_head *head;
336 int bit, num, len, in, ret;
337 num = mv_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
338 in = (mv_ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
339 item = mv_get_qtd(num, in);
340 head = mv_get_qh(num, in);
343 ret = mv_bounce(mv_ep);
347 item->next = TERMINATE;
348 item->info = INFO_BYTES(len) | INFO_IOC | INFO_ACTIVE;
349 item->page0 = (uint32_t)mv_ep->b_buf;
350 item->page1 = ((uint32_t)mv_ep->b_buf & 0xfffff000) + 0x1000;
353 head->next = (unsigned) item;
356 DBG("ept%d %s queue len %x, buffer %p\n",
357 num, in ? "in" : "out", len, mv_ep->b_buf);
365 writel(bit, &udc->epprime);
370 static void handle_ep_complete(struct mv_ep *ep)
372 struct ept_queue_item *item;
374 num = ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
375 in = (ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
377 ep->desc = &ep0_out_desc;
378 item = mv_get_qtd(num, in);
379 mv_invalidate_qtd(num);
381 if (item->info & 0xff)
382 printf("EP%d/%s FAIL info=%x pg0=%x\n",
383 num, in ? "in" : "out", item->info, item->page0);
385 len = (item->info >> 16) & 0x7fff;
389 ep->req.length -= len;
390 DBG("ept%d %s complete %x\n",
391 num, in ? "in" : "out", len);
392 ep->req.complete(&ep->ep, &ep->req);
395 usb_ep_queue(&ep->ep, &ep->req, 0);
396 ep->desc = &ep0_in_desc;
400 #define SETUP(type, request) (((type) << 8) | (request))
402 static void handle_setup(void)
404 struct usb_request *req = &controller.ep[0].req;
405 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
406 struct ept_queue_head *head;
407 struct usb_ctrlrequest r;
409 int num, in, _num, _in, i;
411 head = mv_get_qh(0, 0); /* EP0 OUT */
414 memcpy(&r, head->setup_data, sizeof(struct usb_ctrlrequest));
415 writel(EPT_RX(0), &udc->epstat);
416 DBG("handle setup %s, %x, %x index %x value %x\n", reqname(r.bRequest),
417 r.bRequestType, r.bRequest, r.wIndex, r.wValue);
419 switch (SETUP(r.bRequestType, r.bRequest)) {
420 case SETUP(USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE):
421 _num = r.wIndex & 15;
422 _in = !!(r.wIndex & 0x80);
424 if ((r.wValue == 0) && (r.wLength == 0)) {
426 for (i = 0; i < NUM_ENDPOINTS; i++) {
427 struct mv_ep *ep = &controller.ep[i];
431 num = ep->desc->bEndpointAddress
432 & USB_ENDPOINT_NUMBER_MASK;
433 in = (ep->desc->bEndpointAddress
435 if ((num == _num) && (in == _in)) {
436 ep_enable(num, in, ep->ep.maxpacket);
437 usb_ep_queue(controller.gadget.ep0,
445 case SETUP(USB_RECIP_DEVICE, USB_REQ_SET_ADDRESS):
447 * write address delayed (will take effect
448 * after the next IN txn)
450 writel((r.wValue << 25) | (1 << 24), &udc->devaddr);
452 usb_ep_queue(controller.gadget.ep0, req, 0);
455 case SETUP(USB_DIR_IN | USB_RECIP_DEVICE, USB_REQ_GET_STATUS):
457 buf = (char *)req->buf;
458 buf[0] = 1 << USB_DEVICE_SELF_POWERED;
460 usb_ep_queue(controller.gadget.ep0, req, 0);
463 /* pass request up to the gadget driver */
464 if (controller.driver)
465 status = controller.driver->setup(&controller.gadget, &r);
471 DBG("STALL reqname %s type %x value %x, index %x\n",
472 reqname(r.bRequest), r.bRequestType, r.wValue, r.wIndex);
473 writel((1<<16) | (1 << 0), &udc->epctrl[0]);
476 static void stop_activity(void)
479 struct ept_queue_head *head;
480 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
481 writel(readl(&udc->epcomp), &udc->epcomp);
482 writel(readl(&udc->epstat), &udc->epstat);
483 writel(0xffffffff, &udc->epflush);
485 /* error out any pending reqs */
486 for (i = 0; i < NUM_ENDPOINTS; i++) {
488 writel(0, &udc->epctrl[i]);
489 if (controller.ep[i].desc) {
490 num = controller.ep[i].desc->bEndpointAddress
491 & USB_ENDPOINT_NUMBER_MASK;
492 in = (controller.ep[i].desc->bEndpointAddress
494 head = mv_get_qh(num, in);
495 head->info = INFO_ACTIVE;
503 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
504 unsigned n = readl(&udc->usbsts);
505 writel(n, &udc->usbsts);
508 n &= (STS_SLI | STS_URI | STS_PCI | STS_UI | STS_UEI);
513 DBG("-- reset --\n");
517 DBG("-- suspend --\n");
521 int speed = USB_SPEED_FULL;
523 bit = (readl(&udc->portsc) >> 26) & 3;
524 DBG("-- portchange %x %s\n", bit, (bit == 2) ? "High" : "Full");
526 speed = USB_SPEED_HIGH;
529 controller.gadget.speed = speed;
530 for (i = 1; i < NUM_ENDPOINTS; i++) {
531 if (controller.ep[i].ep.maxpacket > max)
532 controller.ep[i].ep.maxpacket = max;
537 printf("<UEI %x>\n", readl(&udc->epcomp));
539 if ((n & STS_UI) || (n & STS_UEI)) {
540 n = readl(&udc->epstat);
544 n = readl(&udc->epcomp);
546 writel(n, &udc->epcomp);
548 for (i = 0; i < NUM_ENDPOINTS && n; i++) {
549 if (controller.ep[i].desc) {
550 num = controller.ep[i].desc->bEndpointAddress
551 & USB_ENDPOINT_NUMBER_MASK;
552 in = (controller.ep[i].desc->bEndpointAddress
554 bit = (in) ? EPT_TX(num) : EPT_RX(num);
556 handle_ep_complete(&controller.ep[i]);
562 int usb_gadget_handle_interrupts(void)
565 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
567 value = readl(&udc->usbsts);
574 static int mv_pullup(struct usb_gadget *gadget, int is_on)
576 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
579 writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RST, &udc->usbcmd);
582 writel((unsigned)controller.epts, &udc->epinitaddr);
584 /* select DEVICE mode */
585 writel(USBMODE_DEVICE, &udc->usbmode);
587 writel(0xffffffff, &udc->epflush);
589 /* Turn on the USB connection by enabling the pullup resistor */
590 writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RUN, &udc->usbcmd);
593 writel(USBCMD_FS2, &udc->usbcmd);
595 if (controller.driver)
596 controller.driver->disconnect(gadget);
602 void udc_disconnect(void)
604 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
607 writel(USBCMD_FS2, &udc->usbcmd);
609 if (controller.driver)
610 controller.driver->disconnect(&controller.gadget);
613 static int mvudc_probe(void)
615 struct ept_queue_head *head;
619 const int num = 2 * NUM_ENDPOINTS;
621 const int eplist_min_align = 4096;
622 const int eplist_align = roundup(eplist_min_align, ARCH_DMA_MINALIGN);
623 const int eplist_raw_sz = num * sizeof(struct ept_queue_head);
624 const int eplist_sz = roundup(eplist_raw_sz, ARCH_DMA_MINALIGN);
626 const int ilist_align = roundup(ARCH_DMA_MINALIGN, 32);
627 const int ilist_ent_raw_sz = 2 * sizeof(struct ept_queue_item);
628 const int ilist_ent_sz = roundup(ilist_ent_raw_sz, ARCH_DMA_MINALIGN);
629 const int ilist_sz = NUM_ENDPOINTS * ilist_ent_sz;
631 /* The QH list must be aligned to 4096 bytes. */
632 controller.epts = memalign(eplist_align, eplist_sz);
633 if (!controller.epts)
635 memset(controller.epts, 0, eplist_sz);
638 * Each qTD item must be 32-byte aligned, each qTD touple must be
639 * cacheline aligned. There are two qTD items for each endpoint and
640 * only one of them is used for the endpoint at time, so we can group
643 controller.items_mem = memalign(ilist_align, ilist_sz);
644 if (!controller.items_mem) {
645 free(controller.epts);
648 memset(controller.items_mem, 0, ilist_sz);
650 for (i = 0; i < 2 * NUM_ENDPOINTS; i++) {
652 * Configure QH for each endpoint. The structure of the QH list
653 * is such that each two subsequent fields, N and N+1 where N is
654 * even, in the QH list represent QH for one endpoint. The Nth
655 * entry represents OUT configuration and the N+1th entry does
656 * represent IN configuration of the endpoint.
658 head = controller.epts + i;
660 head->config = CONFIG_MAX_PKT(EP0_MAX_PACKET_SIZE)
661 | CONFIG_ZLT | CONFIG_IOS;
663 head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE)
665 head->next = TERMINATE;
668 imem = controller.items_mem + ((i >> 1) * ilist_ent_sz);
670 imem += sizeof(struct ept_queue_item);
672 controller.items[i] = (struct ept_queue_item *)imem;
680 INIT_LIST_HEAD(&controller.gadget.ep_list);
683 memcpy(&controller.ep[0].ep, &mv_ep_init[0], sizeof(*mv_ep_init));
684 controller.ep[0].desc = &ep0_in_desc;
685 controller.gadget.ep0 = &controller.ep[0].ep;
686 INIT_LIST_HEAD(&controller.gadget.ep0->ep_list);
689 for (i = 1; i < NUM_ENDPOINTS; i++) {
690 memcpy(&controller.ep[i].ep, &mv_ep_init[1],
691 sizeof(*mv_ep_init));
692 list_add_tail(&controller.ep[i].ep.ep_list,
693 &controller.gadget.ep_list);
699 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
706 if (!driver->bind || !driver->setup || !driver->disconnect)
708 if (driver->speed != USB_SPEED_FULL && driver->speed != USB_SPEED_HIGH)
711 ret = usb_lowlevel_init(0, USB_INIT_DEVICE, (void **)&controller.ctrl);
717 udc = (struct mv_udc *)controller.ctrl->hcor;
719 /* select ULPI phy */
720 writel(PTS(PTS_ENABLE) | PFSC, &udc->portsc);
723 ret = driver->bind(&controller.gadget);
725 DBG("driver->bind() returned %d\n", ret);
728 controller.driver = driver;
733 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)