Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011, Marvell Semiconductor Inc. |
| 3 | * Lei Wen <leiwen@marvell.com> |
| 4 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 6 | * |
| 7 | * Back ported to the 8xx platform (from the 8260 platform) by |
| 8 | * Murray.Jensen@cmst.csiro.au, 27-Jan-01. |
| 9 | */ |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <command.h> |
| 13 | #include <config.h> |
| 14 | #include <net.h> |
| 15 | #include <malloc.h> |
| 16 | #include <asm/io.h> |
| 17 | #include <linux/types.h> |
| 18 | #include <usb/mv_udc.h> |
| 19 | |
Marek Vasut | 1edb999 | 2013-07-10 03:16:27 +0200 | [diff] [blame] | 20 | #if CONFIG_USB_MAX_CONTROLLER_COUNT > 1 |
| 21 | #error This driver only supports one single controller. |
| 22 | #endif |
| 23 | |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 24 | #ifndef DEBUG |
| 25 | #define DBG(x...) do {} while (0) |
| 26 | #else |
| 27 | #define DBG(x...) printf(x) |
| 28 | static const char *reqname(unsigned r) |
| 29 | { |
| 30 | switch (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*"; |
| 42 | } |
| 43 | } |
| 44 | #endif |
| 45 | |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 46 | static struct usb_endpoint_descriptor ep0_out_desc = { |
| 47 | .bLength = sizeof(struct usb_endpoint_descriptor), |
| 48 | .bDescriptorType = USB_DT_ENDPOINT, |
| 49 | .bEndpointAddress = 0, |
| 50 | .bmAttributes = USB_ENDPOINT_XFER_CONTROL, |
| 51 | }; |
| 52 | |
| 53 | static struct usb_endpoint_descriptor ep0_in_desc = { |
| 54 | .bLength = sizeof(struct usb_endpoint_descriptor), |
| 55 | .bDescriptorType = USB_DT_ENDPOINT, |
| 56 | .bEndpointAddress = USB_DIR_IN, |
| 57 | .bmAttributes = USB_ENDPOINT_XFER_CONTROL, |
| 58 | }; |
| 59 | |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 60 | static int mv_pullup(struct usb_gadget *gadget, int is_on); |
| 61 | static int mv_ep_enable(struct usb_ep *ep, |
| 62 | const struct usb_endpoint_descriptor *desc); |
| 63 | static int mv_ep_disable(struct usb_ep *ep); |
| 64 | static int mv_ep_queue(struct usb_ep *ep, |
| 65 | struct usb_request *req, gfp_t gfp_flags); |
| 66 | static struct usb_request * |
| 67 | mv_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags); |
| 68 | static void mv_ep_free_request(struct usb_ep *ep, struct usb_request *_req); |
| 69 | |
| 70 | static struct usb_gadget_ops mv_udc_ops = { |
| 71 | .pullup = mv_pullup, |
| 72 | }; |
| 73 | |
| 74 | static struct usb_ep_ops mv_ep_ops = { |
| 75 | .enable = mv_ep_enable, |
| 76 | .disable = mv_ep_disable, |
| 77 | .queue = mv_ep_queue, |
| 78 | .alloc_request = mv_ep_alloc_request, |
| 79 | .free_request = mv_ep_free_request, |
| 80 | }; |
| 81 | |
Marek Vasut | e126341 | 2013-07-10 03:16:30 +0200 | [diff] [blame] | 82 | /* Init values for USB endpoints. */ |
| 83 | static const struct usb_ep mv_ep_init[2] = { |
| 84 | [0] = { /* EP 0 */ |
| 85 | .maxpacket = 64, |
| 86 | .name = "ep0", |
| 87 | .ops = &mv_ep_ops, |
| 88 | }, |
| 89 | [1] = { /* EP 1..n */ |
| 90 | .maxpacket = 512, |
| 91 | .name = "ep-", |
| 92 | .ops = &mv_ep_ops, |
| 93 | }, |
| 94 | }; |
| 95 | |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 96 | static struct mv_drv controller = { |
Marek Vasut | be0b0e7 | 2013-07-10 03:16:35 +0200 | [diff] [blame] | 97 | .gadget = { |
| 98 | .name = "mv_udc", |
| 99 | .ops = &mv_udc_ops, |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 100 | }, |
| 101 | }; |
| 102 | |
| 103 | static struct usb_request * |
| 104 | mv_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags) |
| 105 | { |
| 106 | struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep); |
| 107 | return &mv_ep->req; |
| 108 | } |
| 109 | |
| 110 | static void mv_ep_free_request(struct usb_ep *ep, struct usb_request *_req) |
| 111 | { |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | static void ep_enable(int num, int in) |
| 116 | { |
| 117 | struct ept_queue_head *head; |
Marek Vasut | 1f4b491 | 2013-07-10 03:16:32 +0200 | [diff] [blame] | 118 | struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 119 | unsigned n; |
Marek Vasut | 393004e | 2013-07-10 03:16:36 +0200 | [diff] [blame] | 120 | head = controller.epts + 2*num + in; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 121 | |
| 122 | n = readl(&udc->epctrl[num]); |
| 123 | if (in) |
| 124 | n |= (CTRL_TXE | CTRL_TXR | CTRL_TXT_BULK); |
| 125 | else |
| 126 | n |= (CTRL_RXE | CTRL_RXR | CTRL_RXT_BULK); |
| 127 | |
| 128 | if (num != 0) |
| 129 | head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE) | CONFIG_ZLT; |
| 130 | writel(n, &udc->epctrl[num]); |
| 131 | } |
| 132 | |
| 133 | static int mv_ep_enable(struct usb_ep *ep, |
| 134 | const struct usb_endpoint_descriptor *desc) |
| 135 | { |
| 136 | struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep); |
| 137 | int num, in; |
| 138 | num = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; |
| 139 | in = (desc->bEndpointAddress & USB_DIR_IN) != 0; |
| 140 | ep_enable(num, in); |
| 141 | mv_ep->desc = desc; |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static int mv_ep_disable(struct usb_ep *ep) |
| 146 | { |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | static int mv_ep_queue(struct usb_ep *ep, |
| 151 | struct usb_request *req, gfp_t gfp_flags) |
| 152 | { |
| 153 | struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep); |
Marek Vasut | 1f4b491 | 2013-07-10 03:16:32 +0200 | [diff] [blame] | 154 | struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 155 | struct ept_queue_item *item; |
| 156 | struct ept_queue_head *head; |
| 157 | unsigned phys; |
| 158 | int bit, num, len, in; |
| 159 | num = mv_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; |
| 160 | in = (mv_ep->desc->bEndpointAddress & USB_DIR_IN) != 0; |
Marek Vasut | 393004e | 2013-07-10 03:16:36 +0200 | [diff] [blame] | 161 | item = controller.items[2 * num + in]; |
| 162 | head = controller.epts + 2 * num + in; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 163 | phys = (unsigned)req->buf; |
| 164 | len = req->length; |
| 165 | |
| 166 | item->next = TERMINATE; |
| 167 | item->info = INFO_BYTES(len) | INFO_IOC | INFO_ACTIVE; |
| 168 | item->page0 = phys; |
| 169 | item->page1 = (phys & 0xfffff000) + 0x1000; |
| 170 | |
| 171 | head->next = (unsigned) item; |
| 172 | head->info = 0; |
| 173 | |
| 174 | DBG("ept%d %s queue len %x, buffer %x\n", |
| 175 | num, in ? "in" : "out", len, phys); |
| 176 | |
| 177 | if (in) |
| 178 | bit = EPT_TX(num); |
| 179 | else |
| 180 | bit = EPT_RX(num); |
| 181 | |
| 182 | flush_cache(phys, len); |
| 183 | flush_cache((unsigned long)item, sizeof(struct ept_queue_item)); |
| 184 | writel(bit, &udc->epprime); |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | static void handle_ep_complete(struct mv_ep *ep) |
| 190 | { |
| 191 | struct ept_queue_item *item; |
| 192 | int num, in, len; |
| 193 | num = ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; |
| 194 | in = (ep->desc->bEndpointAddress & USB_DIR_IN) != 0; |
| 195 | if (num == 0) |
| 196 | ep->desc = &ep0_out_desc; |
Marek Vasut | 393004e | 2013-07-10 03:16:36 +0200 | [diff] [blame] | 197 | item = controller.items[2 * num + in]; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 198 | |
| 199 | if (item->info & 0xff) |
| 200 | printf("EP%d/%s FAIL nfo=%x pg0=%x\n", |
| 201 | num, in ? "in" : "out", item->info, item->page0); |
| 202 | |
| 203 | len = (item->info >> 16) & 0x7fff; |
| 204 | ep->req.length -= len; |
| 205 | DBG("ept%d %s complete %x\n", |
| 206 | num, in ? "in" : "out", len); |
| 207 | ep->req.complete(&ep->ep, &ep->req); |
| 208 | if (num == 0) { |
| 209 | ep->req.length = 0; |
| 210 | usb_ep_queue(&ep->ep, &ep->req, 0); |
| 211 | ep->desc = &ep0_in_desc; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | #define SETUP(type, request) (((type) << 8) | (request)) |
| 216 | |
| 217 | static void handle_setup(void) |
| 218 | { |
Marek Vasut | 9837732 | 2013-07-10 03:16:29 +0200 | [diff] [blame] | 219 | struct usb_request *req = &controller.ep[0].req; |
Marek Vasut | 1f4b491 | 2013-07-10 03:16:32 +0200 | [diff] [blame] | 220 | struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 221 | struct ept_queue_head *head; |
| 222 | struct usb_ctrlrequest r; |
| 223 | int status = 0; |
| 224 | int num, in, _num, _in, i; |
| 225 | char *buf; |
Marek Vasut | 393004e | 2013-07-10 03:16:36 +0200 | [diff] [blame] | 226 | head = controller.epts + 2 * 0 + 0; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 227 | |
| 228 | flush_cache((unsigned long)head, sizeof(struct ept_queue_head)); |
| 229 | memcpy(&r, head->setup_data, sizeof(struct usb_ctrlrequest)); |
| 230 | writel(EPT_RX(0), &udc->epstat); |
| 231 | DBG("handle setup %s, %x, %x index %x value %x\n", reqname(r.bRequest), |
| 232 | r.bRequestType, r.bRequest, r.wIndex, r.wValue); |
| 233 | |
| 234 | switch (SETUP(r.bRequestType, r.bRequest)) { |
| 235 | case SETUP(USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE): |
| 236 | _num = r.wIndex & 15; |
| 237 | _in = !!(r.wIndex & 0x80); |
| 238 | |
| 239 | if ((r.wValue == 0) && (r.wLength == 0)) { |
| 240 | req->length = 0; |
| 241 | for (i = 0; i < NUM_ENDPOINTS; i++) { |
Marek Vasut | 9837732 | 2013-07-10 03:16:29 +0200 | [diff] [blame] | 242 | if (!controller.ep[i].desc) |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 243 | continue; |
Marek Vasut | 9837732 | 2013-07-10 03:16:29 +0200 | [diff] [blame] | 244 | num = controller.ep[i].desc->bEndpointAddress |
| 245 | & USB_ENDPOINT_NUMBER_MASK; |
| 246 | in = (controller.ep[i].desc->bEndpointAddress |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 247 | & USB_DIR_IN) != 0; |
| 248 | if ((num == _num) && (in == _in)) { |
| 249 | ep_enable(num, in); |
| 250 | usb_ep_queue(controller.gadget.ep0, |
| 251 | req, 0); |
| 252 | break; |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | return; |
| 257 | |
| 258 | case SETUP(USB_RECIP_DEVICE, USB_REQ_SET_ADDRESS): |
| 259 | /* |
| 260 | * write address delayed (will take effect |
| 261 | * after the next IN txn) |
| 262 | */ |
| 263 | writel((r.wValue << 25) | (1 << 24), &udc->devaddr); |
| 264 | req->length = 0; |
| 265 | usb_ep_queue(controller.gadget.ep0, req, 0); |
| 266 | return; |
| 267 | |
| 268 | case SETUP(USB_DIR_IN | USB_RECIP_DEVICE, USB_REQ_GET_STATUS): |
| 269 | req->length = 2; |
| 270 | buf = (char *)req->buf; |
| 271 | buf[0] = 1 << USB_DEVICE_SELF_POWERED; |
| 272 | buf[1] = 0; |
| 273 | usb_ep_queue(controller.gadget.ep0, req, 0); |
| 274 | return; |
| 275 | } |
| 276 | /* pass request up to the gadget driver */ |
| 277 | if (controller.driver) |
| 278 | status = controller.driver->setup(&controller.gadget, &r); |
| 279 | else |
| 280 | status = -ENODEV; |
| 281 | |
| 282 | if (!status) |
| 283 | return; |
| 284 | DBG("STALL reqname %s type %x value %x, index %x\n", |
| 285 | reqname(r.bRequest), r.bRequestType, r.wValue, r.wIndex); |
| 286 | writel((1<<16) | (1 << 0), &udc->epctrl[0]); |
| 287 | } |
| 288 | |
| 289 | static void stop_activity(void) |
| 290 | { |
| 291 | int i, num, in; |
| 292 | struct ept_queue_head *head; |
Marek Vasut | 1f4b491 | 2013-07-10 03:16:32 +0200 | [diff] [blame] | 293 | struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 294 | writel(readl(&udc->epcomp), &udc->epcomp); |
| 295 | writel(readl(&udc->epstat), &udc->epstat); |
| 296 | writel(0xffffffff, &udc->epflush); |
| 297 | |
| 298 | /* error out any pending reqs */ |
| 299 | for (i = 0; i < NUM_ENDPOINTS; i++) { |
| 300 | if (i != 0) |
| 301 | writel(0, &udc->epctrl[i]); |
Marek Vasut | 9837732 | 2013-07-10 03:16:29 +0200 | [diff] [blame] | 302 | if (controller.ep[i].desc) { |
| 303 | num = controller.ep[i].desc->bEndpointAddress |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 304 | & USB_ENDPOINT_NUMBER_MASK; |
Marek Vasut | 9837732 | 2013-07-10 03:16:29 +0200 | [diff] [blame] | 305 | in = (controller.ep[i].desc->bEndpointAddress |
| 306 | & USB_DIR_IN) != 0; |
Marek Vasut | 393004e | 2013-07-10 03:16:36 +0200 | [diff] [blame] | 307 | head = controller.epts + (num * 2) + (in); |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 308 | head->info = INFO_ACTIVE; |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | void udc_irq(void) |
| 314 | { |
Marek Vasut | 1f4b491 | 2013-07-10 03:16:32 +0200 | [diff] [blame] | 315 | struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 316 | unsigned n = readl(&udc->usbsts); |
| 317 | writel(n, &udc->usbsts); |
| 318 | int bit, i, num, in; |
| 319 | |
| 320 | n &= (STS_SLI | STS_URI | STS_PCI | STS_UI | STS_UEI); |
| 321 | if (n == 0) |
| 322 | return; |
| 323 | |
| 324 | if (n & STS_URI) { |
| 325 | DBG("-- reset --\n"); |
| 326 | stop_activity(); |
| 327 | } |
| 328 | if (n & STS_SLI) |
| 329 | DBG("-- suspend --\n"); |
| 330 | |
| 331 | if (n & STS_PCI) { |
| 332 | DBG("-- portchange --\n"); |
| 333 | bit = (readl(&udc->portsc) >> 26) & 3; |
| 334 | if (bit == 2) { |
| 335 | controller.gadget.speed = USB_SPEED_HIGH; |
| 336 | for (i = 1; i < NUM_ENDPOINTS && n; i++) |
Marek Vasut | 9837732 | 2013-07-10 03:16:29 +0200 | [diff] [blame] | 337 | if (controller.ep[i].desc) |
| 338 | controller.ep[i].ep.maxpacket = 512; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 339 | } else { |
| 340 | controller.gadget.speed = USB_SPEED_FULL; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | if (n & STS_UEI) |
| 345 | printf("<UEI %x>\n", readl(&udc->epcomp)); |
| 346 | |
| 347 | if ((n & STS_UI) || (n & STS_UEI)) { |
| 348 | n = readl(&udc->epstat); |
| 349 | if (n & EPT_RX(0)) |
| 350 | handle_setup(); |
| 351 | |
| 352 | n = readl(&udc->epcomp); |
| 353 | if (n != 0) |
| 354 | writel(n, &udc->epcomp); |
| 355 | |
| 356 | for (i = 0; i < NUM_ENDPOINTS && n; i++) { |
Marek Vasut | 9837732 | 2013-07-10 03:16:29 +0200 | [diff] [blame] | 357 | if (controller.ep[i].desc) { |
| 358 | num = controller.ep[i].desc->bEndpointAddress |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 359 | & USB_ENDPOINT_NUMBER_MASK; |
Marek Vasut | 9837732 | 2013-07-10 03:16:29 +0200 | [diff] [blame] | 360 | in = (controller.ep[i].desc->bEndpointAddress |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 361 | & USB_DIR_IN) != 0; |
| 362 | bit = (in) ? EPT_TX(num) : EPT_RX(num); |
| 363 | if (n & bit) |
Marek Vasut | 9837732 | 2013-07-10 03:16:29 +0200 | [diff] [blame] | 364 | handle_ep_complete(&controller.ep[i]); |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | int usb_gadget_handle_interrupts(void) |
| 371 | { |
| 372 | u32 value; |
Marek Vasut | 1f4b491 | 2013-07-10 03:16:32 +0200 | [diff] [blame] | 373 | struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 374 | |
| 375 | value = readl(&udc->usbsts); |
| 376 | if (value) |
| 377 | udc_irq(); |
| 378 | |
| 379 | return value; |
| 380 | } |
| 381 | |
| 382 | static int mv_pullup(struct usb_gadget *gadget, int is_on) |
| 383 | { |
Marek Vasut | 1f4b491 | 2013-07-10 03:16:32 +0200 | [diff] [blame] | 384 | struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 385 | if (is_on) { |
| 386 | /* RESET */ |
| 387 | writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RST, &udc->usbcmd); |
| 388 | udelay(200); |
| 389 | |
Marek Vasut | 393004e | 2013-07-10 03:16:36 +0200 | [diff] [blame] | 390 | writel((unsigned) controller.epts, &udc->epinitaddr); |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 391 | |
| 392 | /* select DEVICE mode */ |
| 393 | writel(USBMODE_DEVICE, &udc->usbmode); |
| 394 | |
| 395 | writel(0xffffffff, &udc->epflush); |
| 396 | |
| 397 | /* Turn on the USB connection by enabling the pullup resistor */ |
| 398 | writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RUN, &udc->usbcmd); |
| 399 | } else { |
| 400 | stop_activity(); |
| 401 | writel(USBCMD_FS2, &udc->usbcmd); |
| 402 | udelay(800); |
| 403 | if (controller.driver) |
| 404 | controller.driver->disconnect(gadget); |
| 405 | } |
| 406 | |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | void udc_disconnect(void) |
| 411 | { |
Marek Vasut | 1f4b491 | 2013-07-10 03:16:32 +0200 | [diff] [blame] | 412 | struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 413 | /* disable pullup */ |
| 414 | stop_activity(); |
| 415 | writel(USBCMD_FS2, &udc->usbcmd); |
| 416 | udelay(800); |
| 417 | if (controller.driver) |
| 418 | controller.driver->disconnect(&controller.gadget); |
| 419 | } |
| 420 | |
| 421 | static int mvudc_probe(void) |
| 422 | { |
| 423 | struct ept_queue_head *head; |
| 424 | int i; |
Marek Vasut | 456707d | 2013-07-10 03:16:37 +0200 | [diff] [blame^] | 425 | |
Marek Vasut | 806d76c | 2013-07-10 03:16:34 +0200 | [diff] [blame] | 426 | const int num = 2 * NUM_ENDPOINTS; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 427 | |
Marek Vasut | 456707d | 2013-07-10 03:16:37 +0200 | [diff] [blame^] | 428 | const int eplist_min_align = 4096; |
| 429 | const int eplist_align = roundup(eplist_min_align, ARCH_DMA_MINALIGN); |
| 430 | const int eplist_raw_sz = num * sizeof(struct ept_queue_head); |
| 431 | const int eplist_sz = roundup(eplist_raw_sz, ARCH_DMA_MINALIGN); |
| 432 | |
| 433 | /* The QH list must be aligned to 4096 bytes. */ |
| 434 | controller.epts = memalign(eplist_align, eplist_sz); |
| 435 | if (!controller.epts) |
| 436 | return -ENOMEM; |
| 437 | memset(controller.epts, 0, eplist_sz); |
| 438 | |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 439 | for (i = 0; i < 2 * NUM_ENDPOINTS; i++) { |
| 440 | /* |
Marek Vasut | 456707d | 2013-07-10 03:16:37 +0200 | [diff] [blame^] | 441 | * Configure QH for each endpoint. The structure of the QH list |
| 442 | * is such that each two subsequent fields, N and N+1 where N is |
| 443 | * even, in the QH list represent QH for one endpoint. The Nth |
| 444 | * entry represents OUT configuration and the N+1th entry does |
| 445 | * represent IN configuration of the endpoint. |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 446 | */ |
Marek Vasut | 393004e | 2013-07-10 03:16:36 +0200 | [diff] [blame] | 447 | head = controller.epts + i; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 448 | if (i < 2) |
| 449 | head->config = CONFIG_MAX_PKT(EP0_MAX_PACKET_SIZE) |
| 450 | | CONFIG_ZLT | CONFIG_IOS; |
| 451 | else |
| 452 | head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE) |
| 453 | | CONFIG_ZLT; |
| 454 | head->next = TERMINATE; |
| 455 | head->info = 0; |
| 456 | |
Marek Vasut | 456707d | 2013-07-10 03:16:37 +0200 | [diff] [blame^] | 457 | controller.items[i] = memalign(roundup(32, ARCH_DMA_MINALIGN), |
Marek Vasut | 393004e | 2013-07-10 03:16:36 +0200 | [diff] [blame] | 458 | sizeof(struct ept_queue_item)); |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | INIT_LIST_HEAD(&controller.gadget.ep_list); |
Marek Vasut | e126341 | 2013-07-10 03:16:30 +0200 | [diff] [blame] | 462 | |
| 463 | /* Init EP 0 */ |
| 464 | memcpy(&controller.ep[0].ep, &mv_ep_init[0], sizeof(*mv_ep_init)); |
Marek Vasut | 9837732 | 2013-07-10 03:16:29 +0200 | [diff] [blame] | 465 | controller.ep[0].desc = &ep0_in_desc; |
Marek Vasut | e126341 | 2013-07-10 03:16:30 +0200 | [diff] [blame] | 466 | controller.gadget.ep0 = &controller.ep[0].ep; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 467 | INIT_LIST_HEAD(&controller.gadget.ep0->ep_list); |
Marek Vasut | e126341 | 2013-07-10 03:16:30 +0200 | [diff] [blame] | 468 | |
| 469 | /* Init EP 1..n */ |
| 470 | for (i = 1; i < NUM_ENDPOINTS; i++) { |
| 471 | memcpy(&controller.ep[i].ep, &mv_ep_init[1], |
| 472 | sizeof(*mv_ep_init)); |
| 473 | list_add_tail(&controller.ep[i].ep.ep_list, |
| 474 | &controller.gadget.ep_list); |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 475 | } |
Marek Vasut | e126341 | 2013-07-10 03:16:30 +0200 | [diff] [blame] | 476 | |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | int usb_gadget_register_driver(struct usb_gadget_driver *driver) |
| 481 | { |
Marek Vasut | 1f4b491 | 2013-07-10 03:16:32 +0200 | [diff] [blame] | 482 | struct mv_udc *udc; |
| 483 | int ret; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 484 | |
Marek Vasut | fe67fe7 | 2013-07-10 03:16:33 +0200 | [diff] [blame] | 485 | if (!driver) |
| 486 | return -EINVAL; |
| 487 | if (!driver->bind || !driver->setup || !driver->disconnect) |
| 488 | return -EINVAL; |
| 489 | if (driver->speed != USB_SPEED_FULL && driver->speed != USB_SPEED_HIGH) |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 490 | return -EINVAL; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 491 | |
Marek Vasut | 1f4b491 | 2013-07-10 03:16:32 +0200 | [diff] [blame] | 492 | ret = usb_lowlevel_init(0, (void **)&controller.ctrl); |
| 493 | if (ret) |
| 494 | return ret; |
| 495 | |
| 496 | ret = mvudc_probe(); |
| 497 | if (!ret) { |
| 498 | udc = (struct mv_udc *)controller.ctrl->hcor; |
| 499 | |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 500 | /* select ULPI phy */ |
| 501 | writel(PTS(PTS_ENABLE) | PFSC, &udc->portsc); |
| 502 | } |
Marek Vasut | 1f4b491 | 2013-07-10 03:16:32 +0200 | [diff] [blame] | 503 | |
| 504 | ret = driver->bind(&controller.gadget); |
| 505 | if (ret) { |
| 506 | DBG("driver->bind() returned %d\n", ret); |
| 507 | return ret; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 508 | } |
| 509 | controller.driver = driver; |
| 510 | |
| 511 | return 0; |
| 512 | } |
| 513 | |
| 514 | int usb_gadget_unregister_driver(struct usb_gadget_driver *driver) |
| 515 | { |
| 516 | return 0; |
| 517 | } |