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