2 * Copyright 2011, Marvell Semiconductor Inc.
3 * Lei Wen <leiwen@marvell.com>
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 * Back ported to the 8xx platform (from the 8260 platform) by
24 * Murray.Jensen@cmst.csiro.au, 27-Jan-01.
33 #include <linux/types.h>
34 #include <usb/mv_udc.h>
37 #define DBG(x...) do {} while (0)
39 #define DBG(x...) printf(x)
40 static const char *reqname(unsigned r)
43 case USB_REQ_GET_STATUS: return "GET_STATUS";
44 case USB_REQ_CLEAR_FEATURE: return "CLEAR_FEATURE";
45 case USB_REQ_SET_FEATURE: return "SET_FEATURE";
46 case USB_REQ_SET_ADDRESS: return "SET_ADDRESS";
47 case USB_REQ_GET_DESCRIPTOR: return "GET_DESCRIPTOR";
48 case USB_REQ_SET_DESCRIPTOR: return "SET_DESCRIPTOR";
49 case USB_REQ_GET_CONFIGURATION: return "GET_CONFIGURATION";
50 case USB_REQ_SET_CONFIGURATION: return "SET_CONFIGURATION";
51 case USB_REQ_GET_INTERFACE: return "GET_INTERFACE";
52 case USB_REQ_SET_INTERFACE: return "SET_INTERFACE";
53 default: return "*UNKNOWN*";
58 #define PAGE_SIZE 4096
60 static struct usb_endpoint_descriptor ep0_out_desc = {
61 .bLength = sizeof(struct usb_endpoint_descriptor),
62 .bDescriptorType = USB_DT_ENDPOINT,
63 .bEndpointAddress = 0,
64 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
67 static struct usb_endpoint_descriptor ep0_in_desc = {
68 .bLength = sizeof(struct usb_endpoint_descriptor),
69 .bDescriptorType = USB_DT_ENDPOINT,
70 .bEndpointAddress = USB_DIR_IN,
71 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
74 struct ept_queue_head *epts;
75 struct ept_queue_item *items[2 * NUM_ENDPOINTS];
76 static int mv_pullup(struct usb_gadget *gadget, int is_on);
77 static int mv_ep_enable(struct usb_ep *ep,
78 const struct usb_endpoint_descriptor *desc);
79 static int mv_ep_disable(struct usb_ep *ep);
80 static int mv_ep_queue(struct usb_ep *ep,
81 struct usb_request *req, gfp_t gfp_flags);
82 static struct usb_request *
83 mv_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags);
84 static void mv_ep_free_request(struct usb_ep *ep, struct usb_request *_req);
86 static struct usb_gadget_ops mv_udc_ops = {
90 static struct usb_ep_ops mv_ep_ops = {
91 .enable = mv_ep_enable,
92 .disable = mv_ep_disable,
94 .alloc_request = mv_ep_alloc_request,
95 .free_request = mv_ep_free_request,
98 static struct mv_ep ep[2 * NUM_ENDPOINTS];
99 static struct mv_drv controller = {
106 static struct usb_request *
107 mv_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags)
109 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
113 static void mv_ep_free_request(struct usb_ep *ep, struct usb_request *_req)
118 static void ep_enable(int num, int in)
120 struct ept_queue_head *head;
121 struct mv_udc *udc = controller.udc;
123 head = epts + 2*num + in;
125 n = readl(&udc->epctrl[num]);
127 n |= (CTRL_TXE | CTRL_TXR | CTRL_TXT_BULK);
129 n |= (CTRL_RXE | CTRL_RXR | CTRL_RXT_BULK);
132 head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE) | CONFIG_ZLT;
133 writel(n, &udc->epctrl[num]);
136 static int mv_ep_enable(struct usb_ep *ep,
137 const struct usb_endpoint_descriptor *desc)
139 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
141 num = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
142 in = (desc->bEndpointAddress & USB_DIR_IN) != 0;
148 static int mv_ep_disable(struct usb_ep *ep)
153 static int mv_ep_queue(struct usb_ep *ep,
154 struct usb_request *req, gfp_t gfp_flags)
156 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
157 struct mv_udc *udc = controller.udc;
158 struct ept_queue_item *item;
159 struct ept_queue_head *head;
161 int bit, num, len, in;
162 num = mv_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
163 in = (mv_ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
164 item = items[2 * num + in];
165 head = epts + 2 * num + in;
166 phys = (unsigned)req->buf;
169 item->next = TERMINATE;
170 item->info = INFO_BYTES(len) | INFO_IOC | INFO_ACTIVE;
172 item->page1 = (phys & 0xfffff000) + 0x1000;
174 head->next = (unsigned) item;
177 DBG("ept%d %s queue len %x, buffer %x\n",
178 num, in ? "in" : "out", len, phys);
185 flush_cache(phys, len);
186 flush_cache((unsigned long)item, sizeof(struct ept_queue_item));
187 writel(bit, &udc->epprime);
192 static void handle_ep_complete(struct mv_ep *ep)
194 struct ept_queue_item *item;
196 num = ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
197 in = (ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
199 ep->desc = &ep0_out_desc;
200 item = items[2 * num + in];
202 if (item->info & 0xff)
203 printf("EP%d/%s FAIL nfo=%x pg0=%x\n",
204 num, in ? "in" : "out", item->info, item->page0);
206 len = (item->info >> 16) & 0x7fff;
207 ep->req.length -= len;
208 DBG("ept%d %s complete %x\n",
209 num, in ? "in" : "out", len);
210 ep->req.complete(&ep->ep, &ep->req);
213 usb_ep_queue(&ep->ep, &ep->req, 0);
214 ep->desc = &ep0_in_desc;
218 #define SETUP(type, request) (((type) << 8) | (request))
220 static void handle_setup(void)
222 struct usb_request *req = &ep[0].req;
223 struct mv_udc *udc = controller.udc;
224 struct ept_queue_head *head;
225 struct usb_ctrlrequest r;
227 int num, in, _num, _in, i;
231 flush_cache((unsigned long)head, sizeof(struct ept_queue_head));
232 memcpy(&r, head->setup_data, sizeof(struct usb_ctrlrequest));
233 writel(EPT_RX(0), &udc->epstat);
234 DBG("handle setup %s, %x, %x index %x value %x\n", reqname(r.bRequest),
235 r.bRequestType, r.bRequest, r.wIndex, r.wValue);
237 switch (SETUP(r.bRequestType, r.bRequest)) {
238 case SETUP(USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE):
239 _num = r.wIndex & 15;
240 _in = !!(r.wIndex & 0x80);
242 if ((r.wValue == 0) && (r.wLength == 0)) {
244 for (i = 0; i < NUM_ENDPOINTS; i++) {
247 num = ep[i].desc->bEndpointAddress
248 & USB_ENDPOINT_NUMBER_MASK;
249 in = (ep[i].desc->bEndpointAddress
251 if ((num == _num) && (in == _in)) {
253 usb_ep_queue(controller.gadget.ep0,
261 case SETUP(USB_RECIP_DEVICE, USB_REQ_SET_ADDRESS):
263 * write address delayed (will take effect
264 * after the next IN txn)
266 writel((r.wValue << 25) | (1 << 24), &udc->devaddr);
268 usb_ep_queue(controller.gadget.ep0, req, 0);
271 case SETUP(USB_DIR_IN | USB_RECIP_DEVICE, USB_REQ_GET_STATUS):
273 buf = (char *)req->buf;
274 buf[0] = 1 << USB_DEVICE_SELF_POWERED;
276 usb_ep_queue(controller.gadget.ep0, req, 0);
279 /* pass request up to the gadget driver */
280 if (controller.driver)
281 status = controller.driver->setup(&controller.gadget, &r);
287 DBG("STALL reqname %s type %x value %x, index %x\n",
288 reqname(r.bRequest), r.bRequestType, r.wValue, r.wIndex);
289 writel((1<<16) | (1 << 0), &udc->epctrl[0]);
292 static void stop_activity(void)
295 struct ept_queue_head *head;
296 struct mv_udc *udc = controller.udc;
297 writel(readl(&udc->epcomp), &udc->epcomp);
298 writel(readl(&udc->epstat), &udc->epstat);
299 writel(0xffffffff, &udc->epflush);
301 /* error out any pending reqs */
302 for (i = 0; i < NUM_ENDPOINTS; i++) {
304 writel(0, &udc->epctrl[i]);
306 num = ep[i].desc->bEndpointAddress
307 & USB_ENDPOINT_NUMBER_MASK;
308 in = (ep[i].desc->bEndpointAddress & USB_DIR_IN) != 0;
309 head = epts + (num * 2) + (in);
310 head->info = INFO_ACTIVE;
317 struct mv_udc *udc = controller.udc;
318 unsigned n = readl(&udc->usbsts);
319 writel(n, &udc->usbsts);
322 n &= (STS_SLI | STS_URI | STS_PCI | STS_UI | STS_UEI);
327 DBG("-- reset --\n");
331 DBG("-- suspend --\n");
334 DBG("-- portchange --\n");
335 bit = (readl(&udc->portsc) >> 26) & 3;
337 controller.gadget.speed = USB_SPEED_HIGH;
338 for (i = 1; i < NUM_ENDPOINTS && n; i++)
340 ep[i].ep.maxpacket = 512;
342 controller.gadget.speed = USB_SPEED_FULL;
347 printf("<UEI %x>\n", readl(&udc->epcomp));
349 if ((n & STS_UI) || (n & STS_UEI)) {
350 n = readl(&udc->epstat);
354 n = readl(&udc->epcomp);
356 writel(n, &udc->epcomp);
358 for (i = 0; i < NUM_ENDPOINTS && n; i++) {
360 num = ep[i].desc->bEndpointAddress
361 & USB_ENDPOINT_NUMBER_MASK;
362 in = (ep[i].desc->bEndpointAddress
364 bit = (in) ? EPT_TX(num) : EPT_RX(num);
366 handle_ep_complete(&ep[i]);
372 int usb_gadget_handle_interrupts(void)
375 struct mv_udc *udc = controller.udc;
377 value = readl(&udc->usbsts);
384 static int mv_pullup(struct usb_gadget *gadget, int is_on)
386 struct mv_udc *udc = controller.udc;
389 writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RST, &udc->usbcmd);
392 writel((unsigned) epts, &udc->epinitaddr);
394 /* select DEVICE mode */
395 writel(USBMODE_DEVICE, &udc->usbmode);
397 writel(0xffffffff, &udc->epflush);
399 /* Turn on the USB connection by enabling the pullup resistor */
400 writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RUN, &udc->usbcmd);
403 writel(USBCMD_FS2, &udc->usbcmd);
405 if (controller.driver)
406 controller.driver->disconnect(gadget);
412 void udc_disconnect(void)
414 struct mv_udc *udc = controller.udc;
417 writel(USBCMD_FS2, &udc->usbcmd);
419 if (controller.driver)
420 controller.driver->disconnect(&controller.gadget);
423 static int mvudc_probe(void)
425 struct ept_queue_head *head;
428 controller.gadget.ops = &mv_udc_ops;
429 controller.udc = (struct mv_udc *)CONFIG_USB_REG_BASE;
430 epts = memalign(PAGE_SIZE, QH_MAXNUM * sizeof(struct ept_queue_head));
431 memset(epts, 0, QH_MAXNUM * sizeof(struct ept_queue_head));
432 for (i = 0; i < 2 * NUM_ENDPOINTS; i++) {
434 * For item0 and item1, they are served as ep0
439 head->config = CONFIG_MAX_PKT(EP0_MAX_PACKET_SIZE)
440 | CONFIG_ZLT | CONFIG_IOS;
442 head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE)
444 head->next = TERMINATE;
447 items[i] = memalign(PAGE_SIZE, sizeof(struct ept_queue_item));
450 INIT_LIST_HEAD(&controller.gadget.ep_list);
451 ep[0].ep.maxpacket = 64;
452 ep[0].ep.name = "ep0";
453 ep[0].desc = &ep0_in_desc;
454 INIT_LIST_HEAD(&controller.gadget.ep0->ep_list);
455 for (i = 0; i < 2 * NUM_ENDPOINTS; i++) {
457 ep[i].ep.maxpacket = 512;
458 ep[i].ep.name = "ep-";
459 list_add_tail(&ep[i].ep.ep_list,
460 &controller.gadget.ep_list);
463 ep[i].ep.ops = &mv_ep_ops;
468 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
470 struct mv_udc *udc = controller.udc;
474 || driver->speed < USB_SPEED_FULL
477 DBG("bad parameter.\n");
481 if (!mvudc_probe()) {
483 /* select ULPI phy */
484 writel(PTS(PTS_ENABLE) | PFSC, &udc->portsc);
486 retval = driver->bind(&controller.gadget);
488 DBG("driver->bind() returned %d\n", retval);
491 controller.driver = driver;
496 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)