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