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 #define DBG(x...) do {} while (0)
27 #define DBG(x...) printf(x)
28 static const char *reqname(unsigned r)
31 case USB_REQ_GET_STATUS: return "GET_STATUS";
32 case USB_REQ_CLEAR_FEATURE: return "CLEAR_FEATURE";
33 case USB_REQ_SET_FEATURE: return "SET_FEATURE";
34 case USB_REQ_SET_ADDRESS: return "SET_ADDRESS";
35 case USB_REQ_GET_DESCRIPTOR: return "GET_DESCRIPTOR";
36 case USB_REQ_SET_DESCRIPTOR: return "SET_DESCRIPTOR";
37 case USB_REQ_GET_CONFIGURATION: return "GET_CONFIGURATION";
38 case USB_REQ_SET_CONFIGURATION: return "SET_CONFIGURATION";
39 case USB_REQ_GET_INTERFACE: return "GET_INTERFACE";
40 case USB_REQ_SET_INTERFACE: return "SET_INTERFACE";
41 default: return "*UNKNOWN*";
46 #define PAGE_SIZE 4096
48 static struct usb_endpoint_descriptor ep0_out_desc = {
49 .bLength = sizeof(struct usb_endpoint_descriptor),
50 .bDescriptorType = USB_DT_ENDPOINT,
51 .bEndpointAddress = 0,
52 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
55 static struct usb_endpoint_descriptor ep0_in_desc = {
56 .bLength = sizeof(struct usb_endpoint_descriptor),
57 .bDescriptorType = USB_DT_ENDPOINT,
58 .bEndpointAddress = USB_DIR_IN,
59 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
62 struct ept_queue_head *epts;
63 struct ept_queue_item *items[2 * NUM_ENDPOINTS];
64 static int mv_pullup(struct usb_gadget *gadget, int is_on);
65 static int mv_ep_enable(struct usb_ep *ep,
66 const struct usb_endpoint_descriptor *desc);
67 static int mv_ep_disable(struct usb_ep *ep);
68 static int mv_ep_queue(struct usb_ep *ep,
69 struct usb_request *req, gfp_t gfp_flags);
70 static struct usb_request *
71 mv_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags);
72 static void mv_ep_free_request(struct usb_ep *ep, struct usb_request *_req);
74 static struct usb_gadget_ops mv_udc_ops = {
78 static struct usb_ep_ops mv_ep_ops = {
79 .enable = mv_ep_enable,
80 .disable = mv_ep_disable,
82 .alloc_request = mv_ep_alloc_request,
83 .free_request = mv_ep_free_request,
86 static struct mv_drv controller = {
92 static struct usb_request *
93 mv_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags)
95 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
99 static void mv_ep_free_request(struct usb_ep *ep, struct usb_request *_req)
104 static void ep_enable(int num, int in)
106 struct ept_queue_head *head;
107 struct mv_udc *udc = controller.udc;
109 head = epts + 2*num + in;
111 n = readl(&udc->epctrl[num]);
113 n |= (CTRL_TXE | CTRL_TXR | CTRL_TXT_BULK);
115 n |= (CTRL_RXE | CTRL_RXR | CTRL_RXT_BULK);
118 head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE) | CONFIG_ZLT;
119 writel(n, &udc->epctrl[num]);
122 static int mv_ep_enable(struct usb_ep *ep,
123 const struct usb_endpoint_descriptor *desc)
125 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
127 num = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
128 in = (desc->bEndpointAddress & USB_DIR_IN) != 0;
134 static int mv_ep_disable(struct usb_ep *ep)
139 static int mv_ep_queue(struct usb_ep *ep,
140 struct usb_request *req, gfp_t gfp_flags)
142 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
143 struct mv_udc *udc = controller.udc;
144 struct ept_queue_item *item;
145 struct ept_queue_head *head;
147 int bit, num, len, in;
148 num = mv_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
149 in = (mv_ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
150 item = items[2 * num + in];
151 head = epts + 2 * num + in;
152 phys = (unsigned)req->buf;
155 item->next = TERMINATE;
156 item->info = INFO_BYTES(len) | INFO_IOC | INFO_ACTIVE;
158 item->page1 = (phys & 0xfffff000) + 0x1000;
160 head->next = (unsigned) item;
163 DBG("ept%d %s queue len %x, buffer %x\n",
164 num, in ? "in" : "out", len, phys);
171 flush_cache(phys, len);
172 flush_cache((unsigned long)item, sizeof(struct ept_queue_item));
173 writel(bit, &udc->epprime);
178 static void handle_ep_complete(struct mv_ep *ep)
180 struct ept_queue_item *item;
182 num = ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
183 in = (ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
185 ep->desc = &ep0_out_desc;
186 item = items[2 * num + in];
188 if (item->info & 0xff)
189 printf("EP%d/%s FAIL nfo=%x pg0=%x\n",
190 num, in ? "in" : "out", item->info, item->page0);
192 len = (item->info >> 16) & 0x7fff;
193 ep->req.length -= len;
194 DBG("ept%d %s complete %x\n",
195 num, in ? "in" : "out", len);
196 ep->req.complete(&ep->ep, &ep->req);
199 usb_ep_queue(&ep->ep, &ep->req, 0);
200 ep->desc = &ep0_in_desc;
204 #define SETUP(type, request) (((type) << 8) | (request))
206 static void handle_setup(void)
208 struct usb_request *req = &controller.ep[0].req;
209 struct mv_udc *udc = controller.udc;
210 struct ept_queue_head *head;
211 struct usb_ctrlrequest r;
213 int num, in, _num, _in, i;
217 flush_cache((unsigned long)head, sizeof(struct ept_queue_head));
218 memcpy(&r, head->setup_data, sizeof(struct usb_ctrlrequest));
219 writel(EPT_RX(0), &udc->epstat);
220 DBG("handle setup %s, %x, %x index %x value %x\n", reqname(r.bRequest),
221 r.bRequestType, r.bRequest, r.wIndex, r.wValue);
223 switch (SETUP(r.bRequestType, r.bRequest)) {
224 case SETUP(USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE):
225 _num = r.wIndex & 15;
226 _in = !!(r.wIndex & 0x80);
228 if ((r.wValue == 0) && (r.wLength == 0)) {
230 for (i = 0; i < NUM_ENDPOINTS; i++) {
231 if (!controller.ep[i].desc)
233 num = controller.ep[i].desc->bEndpointAddress
234 & USB_ENDPOINT_NUMBER_MASK;
235 in = (controller.ep[i].desc->bEndpointAddress
237 if ((num == _num) && (in == _in)) {
239 usb_ep_queue(controller.gadget.ep0,
247 case SETUP(USB_RECIP_DEVICE, USB_REQ_SET_ADDRESS):
249 * write address delayed (will take effect
250 * after the next IN txn)
252 writel((r.wValue << 25) | (1 << 24), &udc->devaddr);
254 usb_ep_queue(controller.gadget.ep0, req, 0);
257 case SETUP(USB_DIR_IN | USB_RECIP_DEVICE, USB_REQ_GET_STATUS):
259 buf = (char *)req->buf;
260 buf[0] = 1 << USB_DEVICE_SELF_POWERED;
262 usb_ep_queue(controller.gadget.ep0, req, 0);
265 /* pass request up to the gadget driver */
266 if (controller.driver)
267 status = controller.driver->setup(&controller.gadget, &r);
273 DBG("STALL reqname %s type %x value %x, index %x\n",
274 reqname(r.bRequest), r.bRequestType, r.wValue, r.wIndex);
275 writel((1<<16) | (1 << 0), &udc->epctrl[0]);
278 static void stop_activity(void)
281 struct ept_queue_head *head;
282 struct mv_udc *udc = controller.udc;
283 writel(readl(&udc->epcomp), &udc->epcomp);
284 writel(readl(&udc->epstat), &udc->epstat);
285 writel(0xffffffff, &udc->epflush);
287 /* error out any pending reqs */
288 for (i = 0; i < NUM_ENDPOINTS; i++) {
290 writel(0, &udc->epctrl[i]);
291 if (controller.ep[i].desc) {
292 num = controller.ep[i].desc->bEndpointAddress
293 & USB_ENDPOINT_NUMBER_MASK;
294 in = (controller.ep[i].desc->bEndpointAddress
296 head = epts + (num * 2) + (in);
297 head->info = INFO_ACTIVE;
304 struct mv_udc *udc = controller.udc;
305 unsigned n = readl(&udc->usbsts);
306 writel(n, &udc->usbsts);
309 n &= (STS_SLI | STS_URI | STS_PCI | STS_UI | STS_UEI);
314 DBG("-- reset --\n");
318 DBG("-- suspend --\n");
321 DBG("-- portchange --\n");
322 bit = (readl(&udc->portsc) >> 26) & 3;
324 controller.gadget.speed = USB_SPEED_HIGH;
325 for (i = 1; i < NUM_ENDPOINTS && n; i++)
326 if (controller.ep[i].desc)
327 controller.ep[i].ep.maxpacket = 512;
329 controller.gadget.speed = USB_SPEED_FULL;
334 printf("<UEI %x>\n", readl(&udc->epcomp));
336 if ((n & STS_UI) || (n & STS_UEI)) {
337 n = readl(&udc->epstat);
341 n = readl(&udc->epcomp);
343 writel(n, &udc->epcomp);
345 for (i = 0; i < NUM_ENDPOINTS && n; i++) {
346 if (controller.ep[i].desc) {
347 num = controller.ep[i].desc->bEndpointAddress
348 & USB_ENDPOINT_NUMBER_MASK;
349 in = (controller.ep[i].desc->bEndpointAddress
351 bit = (in) ? EPT_TX(num) : EPT_RX(num);
353 handle_ep_complete(&controller.ep[i]);
359 int usb_gadget_handle_interrupts(void)
362 struct mv_udc *udc = controller.udc;
364 value = readl(&udc->usbsts);
371 static int mv_pullup(struct usb_gadget *gadget, int is_on)
373 struct mv_udc *udc = controller.udc;
376 writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RST, &udc->usbcmd);
379 writel((unsigned) epts, &udc->epinitaddr);
381 /* select DEVICE mode */
382 writel(USBMODE_DEVICE, &udc->usbmode);
384 writel(0xffffffff, &udc->epflush);
386 /* Turn on the USB connection by enabling the pullup resistor */
387 writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RUN, &udc->usbcmd);
390 writel(USBCMD_FS2, &udc->usbcmd);
392 if (controller.driver)
393 controller.driver->disconnect(gadget);
399 void udc_disconnect(void)
401 struct mv_udc *udc = controller.udc;
404 writel(USBCMD_FS2, &udc->usbcmd);
406 if (controller.driver)
407 controller.driver->disconnect(&controller.gadget);
410 static int mvudc_probe(void)
412 struct ept_queue_head *head;
415 controller.gadget.ops = &mv_udc_ops;
416 controller.udc = (struct mv_udc *)CONFIG_USB_REG_BASE;
417 epts = memalign(PAGE_SIZE, QH_MAXNUM * sizeof(struct ept_queue_head));
418 memset(epts, 0, QH_MAXNUM * sizeof(struct ept_queue_head));
419 for (i = 0; i < 2 * NUM_ENDPOINTS; i++) {
421 * For item0 and item1, they are served as ep0
426 head->config = CONFIG_MAX_PKT(EP0_MAX_PACKET_SIZE)
427 | CONFIG_ZLT | CONFIG_IOS;
429 head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE)
431 head->next = TERMINATE;
434 items[i] = memalign(PAGE_SIZE, sizeof(struct ept_queue_item));
437 INIT_LIST_HEAD(&controller.gadget.ep_list);
438 controller.gadget.ep0 = &controller.ep[0].ep;
439 controller.ep[0].ep.maxpacket = 64;
440 controller.ep[0].ep.name = "ep0";
441 controller.ep[0].desc = &ep0_in_desc;
442 INIT_LIST_HEAD(&controller.gadget.ep0->ep_list);
443 for (i = 0; i < 2 * NUM_ENDPOINTS; i++) {
445 controller.ep[i].ep.maxpacket = 512;
446 controller.ep[i].ep.name = "ep-";
447 list_add_tail(&controller.ep[i].ep.ep_list,
448 &controller.gadget.ep_list);
449 controller.ep[i].desc = NULL;
451 controller.ep[i].ep.ops = &mv_ep_ops;
456 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
458 struct mv_udc *udc = controller.udc;
463 || driver->speed < USB_SPEED_FULL
466 DBG("bad parameter.\n");
470 if (!mvudc_probe()) {
471 usb_lowlevel_init(0, &ctrl);
472 /* select ULPI phy */
473 writel(PTS(PTS_ENABLE) | PFSC, &udc->portsc);
475 retval = driver->bind(&controller.gadget);
477 DBG("driver->bind() returned %d\n", retval);
480 controller.driver = driver;
485 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)