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 | |
| 46 | #define PAGE_SIZE 4096 |
| 47 | #define QH_MAXNUM 32 |
| 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, |
| 53 | }; |
| 54 | |
| 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, |
| 60 | }; |
| 61 | |
| 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); |
| 73 | |
| 74 | static struct usb_gadget_ops mv_udc_ops = { |
| 75 | .pullup = mv_pullup, |
| 76 | }; |
| 77 | |
| 78 | static struct usb_ep_ops mv_ep_ops = { |
| 79 | .enable = mv_ep_enable, |
| 80 | .disable = mv_ep_disable, |
| 81 | .queue = mv_ep_queue, |
| 82 | .alloc_request = mv_ep_alloc_request, |
| 83 | .free_request = mv_ep_free_request, |
| 84 | }; |
| 85 | |
| 86 | static struct mv_ep ep[2 * NUM_ENDPOINTS]; |
| 87 | static struct mv_drv controller = { |
| 88 | .gadget = { |
| 89 | .ep0 = &ep[0].ep, |
| 90 | .name = "mv_udc", |
| 91 | }, |
| 92 | }; |
| 93 | |
| 94 | static struct usb_request * |
| 95 | mv_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags) |
| 96 | { |
| 97 | struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep); |
| 98 | return &mv_ep->req; |
| 99 | } |
| 100 | |
| 101 | static void mv_ep_free_request(struct usb_ep *ep, struct usb_request *_req) |
| 102 | { |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | static void ep_enable(int num, int in) |
| 107 | { |
| 108 | struct ept_queue_head *head; |
| 109 | struct mv_udc *udc = controller.udc; |
| 110 | unsigned n; |
| 111 | head = epts + 2*num + in; |
| 112 | |
| 113 | n = readl(&udc->epctrl[num]); |
| 114 | if (in) |
| 115 | n |= (CTRL_TXE | CTRL_TXR | CTRL_TXT_BULK); |
| 116 | else |
| 117 | n |= (CTRL_RXE | CTRL_RXR | CTRL_RXT_BULK); |
| 118 | |
| 119 | if (num != 0) |
| 120 | head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE) | CONFIG_ZLT; |
| 121 | writel(n, &udc->epctrl[num]); |
| 122 | } |
| 123 | |
| 124 | static int mv_ep_enable(struct usb_ep *ep, |
| 125 | const struct usb_endpoint_descriptor *desc) |
| 126 | { |
| 127 | struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep); |
| 128 | int num, in; |
| 129 | num = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; |
| 130 | in = (desc->bEndpointAddress & USB_DIR_IN) != 0; |
| 131 | ep_enable(num, in); |
| 132 | mv_ep->desc = desc; |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | static int mv_ep_disable(struct usb_ep *ep) |
| 137 | { |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | static int mv_ep_queue(struct usb_ep *ep, |
| 142 | struct usb_request *req, gfp_t gfp_flags) |
| 143 | { |
| 144 | struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep); |
| 145 | struct mv_udc *udc = controller.udc; |
| 146 | struct ept_queue_item *item; |
| 147 | struct ept_queue_head *head; |
| 148 | unsigned phys; |
| 149 | int bit, num, len, in; |
| 150 | num = mv_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; |
| 151 | in = (mv_ep->desc->bEndpointAddress & USB_DIR_IN) != 0; |
| 152 | item = items[2 * num + in]; |
| 153 | head = epts + 2 * num + in; |
| 154 | phys = (unsigned)req->buf; |
| 155 | len = req->length; |
| 156 | |
| 157 | item->next = TERMINATE; |
| 158 | item->info = INFO_BYTES(len) | INFO_IOC | INFO_ACTIVE; |
| 159 | item->page0 = phys; |
| 160 | item->page1 = (phys & 0xfffff000) + 0x1000; |
| 161 | |
| 162 | head->next = (unsigned) item; |
| 163 | head->info = 0; |
| 164 | |
| 165 | DBG("ept%d %s queue len %x, buffer %x\n", |
| 166 | num, in ? "in" : "out", len, phys); |
| 167 | |
| 168 | if (in) |
| 169 | bit = EPT_TX(num); |
| 170 | else |
| 171 | bit = EPT_RX(num); |
| 172 | |
| 173 | flush_cache(phys, len); |
| 174 | flush_cache((unsigned long)item, sizeof(struct ept_queue_item)); |
| 175 | writel(bit, &udc->epprime); |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static void handle_ep_complete(struct mv_ep *ep) |
| 181 | { |
| 182 | struct ept_queue_item *item; |
| 183 | int num, in, len; |
| 184 | num = ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; |
| 185 | in = (ep->desc->bEndpointAddress & USB_DIR_IN) != 0; |
| 186 | if (num == 0) |
| 187 | ep->desc = &ep0_out_desc; |
| 188 | item = items[2 * num + in]; |
| 189 | |
| 190 | if (item->info & 0xff) |
| 191 | printf("EP%d/%s FAIL nfo=%x pg0=%x\n", |
| 192 | num, in ? "in" : "out", item->info, item->page0); |
| 193 | |
| 194 | len = (item->info >> 16) & 0x7fff; |
| 195 | ep->req.length -= len; |
| 196 | DBG("ept%d %s complete %x\n", |
| 197 | num, in ? "in" : "out", len); |
| 198 | ep->req.complete(&ep->ep, &ep->req); |
| 199 | if (num == 0) { |
| 200 | ep->req.length = 0; |
| 201 | usb_ep_queue(&ep->ep, &ep->req, 0); |
| 202 | ep->desc = &ep0_in_desc; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | #define SETUP(type, request) (((type) << 8) | (request)) |
| 207 | |
| 208 | static void handle_setup(void) |
| 209 | { |
| 210 | struct usb_request *req = &ep[0].req; |
| 211 | struct mv_udc *udc = controller.udc; |
| 212 | struct ept_queue_head *head; |
| 213 | struct usb_ctrlrequest r; |
| 214 | int status = 0; |
| 215 | int num, in, _num, _in, i; |
| 216 | char *buf; |
| 217 | head = epts; |
| 218 | |
| 219 | flush_cache((unsigned long)head, sizeof(struct ept_queue_head)); |
| 220 | memcpy(&r, head->setup_data, sizeof(struct usb_ctrlrequest)); |
| 221 | writel(EPT_RX(0), &udc->epstat); |
| 222 | DBG("handle setup %s, %x, %x index %x value %x\n", reqname(r.bRequest), |
| 223 | r.bRequestType, r.bRequest, r.wIndex, r.wValue); |
| 224 | |
| 225 | switch (SETUP(r.bRequestType, r.bRequest)) { |
| 226 | case SETUP(USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE): |
| 227 | _num = r.wIndex & 15; |
| 228 | _in = !!(r.wIndex & 0x80); |
| 229 | |
| 230 | if ((r.wValue == 0) && (r.wLength == 0)) { |
| 231 | req->length = 0; |
| 232 | for (i = 0; i < NUM_ENDPOINTS; i++) { |
| 233 | if (!ep[i].desc) |
| 234 | continue; |
| 235 | num = ep[i].desc->bEndpointAddress |
| 236 | & USB_ENDPOINT_NUMBER_MASK; |
| 237 | in = (ep[i].desc->bEndpointAddress |
| 238 | & USB_DIR_IN) != 0; |
| 239 | if ((num == _num) && (in == _in)) { |
| 240 | ep_enable(num, in); |
| 241 | usb_ep_queue(controller.gadget.ep0, |
| 242 | req, 0); |
| 243 | break; |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | return; |
| 248 | |
| 249 | case SETUP(USB_RECIP_DEVICE, USB_REQ_SET_ADDRESS): |
| 250 | /* |
| 251 | * write address delayed (will take effect |
| 252 | * after the next IN txn) |
| 253 | */ |
| 254 | writel((r.wValue << 25) | (1 << 24), &udc->devaddr); |
| 255 | req->length = 0; |
| 256 | usb_ep_queue(controller.gadget.ep0, req, 0); |
| 257 | return; |
| 258 | |
| 259 | case SETUP(USB_DIR_IN | USB_RECIP_DEVICE, USB_REQ_GET_STATUS): |
| 260 | req->length = 2; |
| 261 | buf = (char *)req->buf; |
| 262 | buf[0] = 1 << USB_DEVICE_SELF_POWERED; |
| 263 | buf[1] = 0; |
| 264 | usb_ep_queue(controller.gadget.ep0, req, 0); |
| 265 | return; |
| 266 | } |
| 267 | /* pass request up to the gadget driver */ |
| 268 | if (controller.driver) |
| 269 | status = controller.driver->setup(&controller.gadget, &r); |
| 270 | else |
| 271 | status = -ENODEV; |
| 272 | |
| 273 | if (!status) |
| 274 | return; |
| 275 | DBG("STALL reqname %s type %x value %x, index %x\n", |
| 276 | reqname(r.bRequest), r.bRequestType, r.wValue, r.wIndex); |
| 277 | writel((1<<16) | (1 << 0), &udc->epctrl[0]); |
| 278 | } |
| 279 | |
| 280 | static void stop_activity(void) |
| 281 | { |
| 282 | int i, num, in; |
| 283 | struct ept_queue_head *head; |
| 284 | struct mv_udc *udc = controller.udc; |
| 285 | writel(readl(&udc->epcomp), &udc->epcomp); |
| 286 | writel(readl(&udc->epstat), &udc->epstat); |
| 287 | writel(0xffffffff, &udc->epflush); |
| 288 | |
| 289 | /* error out any pending reqs */ |
| 290 | for (i = 0; i < NUM_ENDPOINTS; i++) { |
| 291 | if (i != 0) |
| 292 | writel(0, &udc->epctrl[i]); |
| 293 | if (ep[i].desc) { |
| 294 | num = ep[i].desc->bEndpointAddress |
| 295 | & USB_ENDPOINT_NUMBER_MASK; |
| 296 | in = (ep[i].desc->bEndpointAddress & USB_DIR_IN) != 0; |
| 297 | head = epts + (num * 2) + (in); |
| 298 | head->info = INFO_ACTIVE; |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | void udc_irq(void) |
| 304 | { |
| 305 | struct mv_udc *udc = controller.udc; |
| 306 | unsigned n = readl(&udc->usbsts); |
| 307 | writel(n, &udc->usbsts); |
| 308 | int bit, i, num, in; |
| 309 | |
| 310 | n &= (STS_SLI | STS_URI | STS_PCI | STS_UI | STS_UEI); |
| 311 | if (n == 0) |
| 312 | return; |
| 313 | |
| 314 | if (n & STS_URI) { |
| 315 | DBG("-- reset --\n"); |
| 316 | stop_activity(); |
| 317 | } |
| 318 | if (n & STS_SLI) |
| 319 | DBG("-- suspend --\n"); |
| 320 | |
| 321 | if (n & STS_PCI) { |
| 322 | DBG("-- portchange --\n"); |
| 323 | bit = (readl(&udc->portsc) >> 26) & 3; |
| 324 | if (bit == 2) { |
| 325 | controller.gadget.speed = USB_SPEED_HIGH; |
| 326 | for (i = 1; i < NUM_ENDPOINTS && n; i++) |
| 327 | if (ep[i].desc) |
| 328 | ep[i].ep.maxpacket = 512; |
| 329 | } else { |
| 330 | controller.gadget.speed = USB_SPEED_FULL; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | if (n & STS_UEI) |
| 335 | printf("<UEI %x>\n", readl(&udc->epcomp)); |
| 336 | |
| 337 | if ((n & STS_UI) || (n & STS_UEI)) { |
| 338 | n = readl(&udc->epstat); |
| 339 | if (n & EPT_RX(0)) |
| 340 | handle_setup(); |
| 341 | |
| 342 | n = readl(&udc->epcomp); |
| 343 | if (n != 0) |
| 344 | writel(n, &udc->epcomp); |
| 345 | |
| 346 | for (i = 0; i < NUM_ENDPOINTS && n; i++) { |
| 347 | if (ep[i].desc) { |
| 348 | num = ep[i].desc->bEndpointAddress |
| 349 | & USB_ENDPOINT_NUMBER_MASK; |
| 350 | in = (ep[i].desc->bEndpointAddress |
| 351 | & USB_DIR_IN) != 0; |
| 352 | bit = (in) ? EPT_TX(num) : EPT_RX(num); |
| 353 | if (n & bit) |
| 354 | handle_ep_complete(&ep[i]); |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | int usb_gadget_handle_interrupts(void) |
| 361 | { |
| 362 | u32 value; |
| 363 | struct mv_udc *udc = controller.udc; |
| 364 | |
| 365 | value = readl(&udc->usbsts); |
| 366 | if (value) |
| 367 | udc_irq(); |
| 368 | |
| 369 | return value; |
| 370 | } |
| 371 | |
| 372 | static int mv_pullup(struct usb_gadget *gadget, int is_on) |
| 373 | { |
| 374 | struct mv_udc *udc = controller.udc; |
| 375 | if (is_on) { |
| 376 | /* RESET */ |
| 377 | writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RST, &udc->usbcmd); |
| 378 | udelay(200); |
| 379 | |
| 380 | writel((unsigned) epts, &udc->epinitaddr); |
| 381 | |
| 382 | /* select DEVICE mode */ |
| 383 | writel(USBMODE_DEVICE, &udc->usbmode); |
| 384 | |
| 385 | writel(0xffffffff, &udc->epflush); |
| 386 | |
| 387 | /* Turn on the USB connection by enabling the pullup resistor */ |
| 388 | writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RUN, &udc->usbcmd); |
| 389 | } else { |
| 390 | stop_activity(); |
| 391 | writel(USBCMD_FS2, &udc->usbcmd); |
| 392 | udelay(800); |
| 393 | if (controller.driver) |
| 394 | controller.driver->disconnect(gadget); |
| 395 | } |
| 396 | |
| 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | void udc_disconnect(void) |
| 401 | { |
| 402 | struct mv_udc *udc = controller.udc; |
| 403 | /* disable pullup */ |
| 404 | stop_activity(); |
| 405 | writel(USBCMD_FS2, &udc->usbcmd); |
| 406 | udelay(800); |
| 407 | if (controller.driver) |
| 408 | controller.driver->disconnect(&controller.gadget); |
| 409 | } |
| 410 | |
| 411 | static int mvudc_probe(void) |
| 412 | { |
| 413 | struct ept_queue_head *head; |
| 414 | int i; |
| 415 | |
| 416 | controller.gadget.ops = &mv_udc_ops; |
| 417 | controller.udc = (struct mv_udc *)CONFIG_USB_REG_BASE; |
| 418 | epts = memalign(PAGE_SIZE, QH_MAXNUM * sizeof(struct ept_queue_head)); |
| 419 | memset(epts, 0, QH_MAXNUM * sizeof(struct ept_queue_head)); |
| 420 | for (i = 0; i < 2 * NUM_ENDPOINTS; i++) { |
| 421 | /* |
| 422 | * For item0 and item1, they are served as ep0 |
| 423 | * out&in seperately |
| 424 | */ |
| 425 | head = epts + i; |
| 426 | if (i < 2) |
| 427 | head->config = CONFIG_MAX_PKT(EP0_MAX_PACKET_SIZE) |
| 428 | | CONFIG_ZLT | CONFIG_IOS; |
| 429 | else |
| 430 | head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE) |
| 431 | | CONFIG_ZLT; |
| 432 | head->next = TERMINATE; |
| 433 | head->info = 0; |
| 434 | |
| 435 | items[i] = memalign(PAGE_SIZE, sizeof(struct ept_queue_item)); |
| 436 | } |
| 437 | |
| 438 | INIT_LIST_HEAD(&controller.gadget.ep_list); |
| 439 | ep[0].ep.maxpacket = 64; |
| 440 | ep[0].ep.name = "ep0"; |
| 441 | ep[0].desc = &ep0_in_desc; |
| 442 | INIT_LIST_HEAD(&controller.gadget.ep0->ep_list); |
| 443 | for (i = 0; i < 2 * NUM_ENDPOINTS; i++) { |
| 444 | if (i != 0) { |
| 445 | ep[i].ep.maxpacket = 512; |
| 446 | ep[i].ep.name = "ep-"; |
| 447 | list_add_tail(&ep[i].ep.ep_list, |
| 448 | &controller.gadget.ep_list); |
| 449 | ep[i].desc = NULL; |
| 450 | } |
| 451 | ep[i].ep.ops = &mv_ep_ops; |
| 452 | } |
| 453 | return 0; |
| 454 | } |
| 455 | |
| 456 | int usb_gadget_register_driver(struct usb_gadget_driver *driver) |
| 457 | { |
| 458 | struct mv_udc *udc = controller.udc; |
| 459 | int retval; |
Marek Vasut | 1edb999 | 2013-07-10 03:16:27 +0200 | [diff] [blame^] | 460 | void *ctrl; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 461 | |
| 462 | if (!driver |
| 463 | || driver->speed < USB_SPEED_FULL |
| 464 | || !driver->bind |
| 465 | || !driver->setup) { |
| 466 | DBG("bad parameter.\n"); |
| 467 | return -EINVAL; |
| 468 | } |
| 469 | |
| 470 | if (!mvudc_probe()) { |
Marek Vasut | 1edb999 | 2013-07-10 03:16:27 +0200 | [diff] [blame^] | 471 | usb_lowlevel_init(0, &ctrl); |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 472 | /* select ULPI phy */ |
| 473 | writel(PTS(PTS_ENABLE) | PFSC, &udc->portsc); |
| 474 | } |
| 475 | retval = driver->bind(&controller.gadget); |
| 476 | if (retval) { |
| 477 | DBG("driver->bind() returned %d\n", retval); |
| 478 | return retval; |
| 479 | } |
| 480 | controller.driver = driver; |
| 481 | |
| 482 | return 0; |
| 483 | } |
| 484 | |
| 485 | int usb_gadget_unregister_driver(struct usb_gadget_driver *driver) |
| 486 | { |
| 487 | return 0; |
| 488 | } |