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, |
Troy Kisky | 4b4df47 | 2013-09-25 18:41:09 -0700 | [diff] [blame] | 110 | .is_dualspeed = 1, |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 111 | }, |
| 112 | }; |
| 113 | |
Marek Vasut | 9212f89 | 2013-07-10 03:16:39 +0200 | [diff] [blame] | 114 | /** |
| 115 | * mv_get_qh() - return queue head for endpoint |
| 116 | * @ep_num: Endpoint number |
| 117 | * @dir_in: Direction of the endpoint (IN = 1, OUT = 0) |
| 118 | * |
| 119 | * This function returns the QH associated with particular endpoint |
| 120 | * and it's direction. |
| 121 | */ |
| 122 | static struct ept_queue_head *mv_get_qh(int ep_num, int dir_in) |
| 123 | { |
| 124 | return &controller.epts[(ep_num * 2) + dir_in]; |
| 125 | } |
| 126 | |
Marek Vasut | a6f6b08 | 2013-07-10 03:16:41 +0200 | [diff] [blame] | 127 | /** |
| 128 | * mv_get_qtd() - return queue item for endpoint |
| 129 | * @ep_num: Endpoint number |
| 130 | * @dir_in: Direction of the endpoint (IN = 1, OUT = 0) |
| 131 | * |
| 132 | * This function returns the QH associated with particular endpoint |
| 133 | * and it's direction. |
| 134 | */ |
| 135 | static struct ept_queue_item *mv_get_qtd(int ep_num, int dir_in) |
| 136 | { |
| 137 | return controller.items[(ep_num * 2) + dir_in]; |
| 138 | } |
| 139 | |
Marek Vasut | 5846534 | 2013-07-10 03:16:42 +0200 | [diff] [blame] | 140 | /** |
| 141 | * mv_flush_qh - flush cache over queue head |
| 142 | * @ep_num: Endpoint number |
| 143 | * |
| 144 | * This function flushes cache over QH for particular endpoint. |
| 145 | */ |
| 146 | static void mv_flush_qh(int ep_num) |
| 147 | { |
| 148 | struct ept_queue_head *head = mv_get_qh(ep_num, 0); |
| 149 | const uint32_t start = (uint32_t)head; |
| 150 | const uint32_t end = start + 2 * sizeof(*head); |
| 151 | |
| 152 | flush_dcache_range(start, end); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * mv_invalidate_qh - invalidate cache over queue head |
| 157 | * @ep_num: Endpoint number |
| 158 | * |
| 159 | * This function invalidates cache over QH for particular endpoint. |
| 160 | */ |
| 161 | static void mv_invalidate_qh(int ep_num) |
| 162 | { |
| 163 | struct ept_queue_head *head = mv_get_qh(ep_num, 0); |
| 164 | uint32_t start = (uint32_t)head; |
| 165 | uint32_t end = start + 2 * sizeof(*head); |
| 166 | |
| 167 | invalidate_dcache_range(start, end); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * mv_flush_qtd - flush cache over queue item |
| 172 | * @ep_num: Endpoint number |
| 173 | * |
| 174 | * This function flushes cache over qTD pair for particular endpoint. |
| 175 | */ |
| 176 | static void mv_flush_qtd(int ep_num) |
| 177 | { |
| 178 | struct ept_queue_item *item = mv_get_qtd(ep_num, 0); |
| 179 | const uint32_t start = (uint32_t)item; |
| 180 | const uint32_t end_raw = start + 2 * sizeof(*item); |
| 181 | const uint32_t end = roundup(end_raw, ARCH_DMA_MINALIGN); |
| 182 | |
| 183 | flush_dcache_range(start, end); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * mv_invalidate_qtd - invalidate cache over queue item |
| 188 | * @ep_num: Endpoint number |
| 189 | * |
| 190 | * This function invalidates cache over qTD pair for particular endpoint. |
| 191 | */ |
| 192 | static void mv_invalidate_qtd(int ep_num) |
| 193 | { |
| 194 | struct ept_queue_item *item = mv_get_qtd(ep_num, 0); |
| 195 | const uint32_t start = (uint32_t)item; |
| 196 | const uint32_t end_raw = start + 2 * sizeof(*item); |
| 197 | const uint32_t end = roundup(end_raw, ARCH_DMA_MINALIGN); |
| 198 | |
| 199 | invalidate_dcache_range(start, end); |
| 200 | } |
| 201 | |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 202 | static struct usb_request * |
| 203 | mv_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags) |
| 204 | { |
| 205 | struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep); |
| 206 | return &mv_ep->req; |
| 207 | } |
| 208 | |
| 209 | static void mv_ep_free_request(struct usb_ep *ep, struct usb_request *_req) |
| 210 | { |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | static void ep_enable(int num, int in) |
| 215 | { |
| 216 | struct ept_queue_head *head; |
Marek Vasut | 1f4b491 | 2013-07-10 03:16:32 +0200 | [diff] [blame] | 217 | struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 218 | unsigned n; |
Marek Vasut | 9212f89 | 2013-07-10 03:16:39 +0200 | [diff] [blame] | 219 | head = mv_get_qh(num, in); |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 220 | |
| 221 | n = readl(&udc->epctrl[num]); |
| 222 | if (in) |
| 223 | n |= (CTRL_TXE | CTRL_TXR | CTRL_TXT_BULK); |
| 224 | else |
| 225 | n |= (CTRL_RXE | CTRL_RXR | CTRL_RXT_BULK); |
| 226 | |
Marek Vasut | 5846534 | 2013-07-10 03:16:42 +0200 | [diff] [blame] | 227 | if (num != 0) { |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 228 | head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE) | CONFIG_ZLT; |
Marek Vasut | 5846534 | 2013-07-10 03:16:42 +0200 | [diff] [blame] | 229 | mv_flush_qh(num); |
| 230 | } |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 231 | writel(n, &udc->epctrl[num]); |
| 232 | } |
| 233 | |
| 234 | static int mv_ep_enable(struct usb_ep *ep, |
| 235 | const struct usb_endpoint_descriptor *desc) |
| 236 | { |
| 237 | struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep); |
| 238 | int num, in; |
| 239 | num = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; |
| 240 | in = (desc->bEndpointAddress & USB_DIR_IN) != 0; |
| 241 | ep_enable(num, in); |
| 242 | mv_ep->desc = desc; |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | static int mv_ep_disable(struct usb_ep *ep) |
| 247 | { |
| 248 | return 0; |
| 249 | } |
| 250 | |
Marek Vasut | e0fc582 | 2013-07-10 03:16:43 +0200 | [diff] [blame] | 251 | static int mv_bounce(struct mv_ep *ep) |
| 252 | { |
| 253 | uint32_t addr = (uint32_t)ep->req.buf; |
| 254 | uint32_t ba; |
| 255 | |
| 256 | /* Input buffer address is not aligned. */ |
| 257 | if (addr & (ARCH_DMA_MINALIGN - 1)) |
| 258 | goto align; |
| 259 | |
| 260 | /* Input buffer length is not aligned. */ |
| 261 | if (ep->req.length & (ARCH_DMA_MINALIGN - 1)) |
| 262 | goto align; |
| 263 | |
| 264 | /* The buffer is well aligned, only flush cache. */ |
| 265 | ep->b_len = ep->req.length; |
| 266 | ep->b_buf = ep->req.buf; |
| 267 | goto flush; |
| 268 | |
| 269 | align: |
| 270 | /* Use internal buffer for small payloads. */ |
| 271 | if (ep->req.length <= 64) { |
| 272 | ep->b_len = 64; |
| 273 | ep->b_buf = ep->b_fast; |
| 274 | } else { |
| 275 | ep->b_len = roundup(ep->req.length, ARCH_DMA_MINALIGN); |
| 276 | ep->b_buf = memalign(ARCH_DMA_MINALIGN, ep->b_len); |
| 277 | if (!ep->b_buf) |
| 278 | return -ENOMEM; |
| 279 | } |
| 280 | |
| 281 | memcpy(ep->b_buf, ep->req.buf, ep->req.length); |
| 282 | |
| 283 | flush: |
| 284 | ba = (uint32_t)ep->b_buf; |
| 285 | flush_dcache_range(ba, ba + ep->b_len); |
| 286 | |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | static void mv_debounce(struct mv_ep *ep) |
| 291 | { |
| 292 | uint32_t addr = (uint32_t)ep->req.buf; |
| 293 | uint32_t ba = (uint32_t)ep->b_buf; |
| 294 | |
| 295 | invalidate_dcache_range(ba, ba + ep->b_len); |
| 296 | |
| 297 | /* Input buffer address is not aligned. */ |
| 298 | if (addr & (ARCH_DMA_MINALIGN - 1)) |
| 299 | goto copy; |
| 300 | |
| 301 | /* Input buffer length is not aligned. */ |
| 302 | if (ep->req.length & (ARCH_DMA_MINALIGN - 1)) |
| 303 | goto copy; |
| 304 | |
| 305 | /* The buffer is well aligned, only invalidate cache. */ |
| 306 | return; |
| 307 | |
| 308 | copy: |
| 309 | memcpy(ep->req.buf, ep->b_buf, ep->req.length); |
| 310 | |
| 311 | /* Large payloads use allocated buffer, free it. */ |
| 312 | if (ep->req.length > 64) |
| 313 | free(ep->b_buf); |
| 314 | } |
| 315 | |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 316 | static int mv_ep_queue(struct usb_ep *ep, |
| 317 | struct usb_request *req, gfp_t gfp_flags) |
| 318 | { |
| 319 | struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep); |
Marek Vasut | 1f4b491 | 2013-07-10 03:16:32 +0200 | [diff] [blame] | 320 | struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 321 | struct ept_queue_item *item; |
| 322 | struct ept_queue_head *head; |
Marek Vasut | e0fc582 | 2013-07-10 03:16:43 +0200 | [diff] [blame] | 323 | int bit, num, len, in, ret; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 324 | num = mv_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; |
| 325 | in = (mv_ep->desc->bEndpointAddress & USB_DIR_IN) != 0; |
Marek Vasut | a6f6b08 | 2013-07-10 03:16:41 +0200 | [diff] [blame] | 326 | item = mv_get_qtd(num, in); |
Marek Vasut | 9212f89 | 2013-07-10 03:16:39 +0200 | [diff] [blame] | 327 | head = mv_get_qh(num, in); |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 328 | len = req->length; |
| 329 | |
Marek Vasut | e0fc582 | 2013-07-10 03:16:43 +0200 | [diff] [blame] | 330 | ret = mv_bounce(mv_ep); |
| 331 | if (ret) |
| 332 | return ret; |
| 333 | |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 334 | item->next = TERMINATE; |
| 335 | item->info = INFO_BYTES(len) | INFO_IOC | INFO_ACTIVE; |
Marek Vasut | e0fc582 | 2013-07-10 03:16:43 +0200 | [diff] [blame] | 336 | item->page0 = (uint32_t)mv_ep->b_buf; |
| 337 | item->page1 = ((uint32_t)mv_ep->b_buf & 0xfffff000) + 0x1000; |
Troy Kisky | 573d131 | 2013-09-25 18:41:12 -0700 | [diff] [blame] | 338 | mv_flush_qtd(num); |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 339 | |
| 340 | head->next = (unsigned) item; |
| 341 | head->info = 0; |
| 342 | |
Marek Vasut | e0fc582 | 2013-07-10 03:16:43 +0200 | [diff] [blame] | 343 | DBG("ept%d %s queue len %x, buffer %p\n", |
| 344 | num, in ? "in" : "out", len, mv_ep->b_buf); |
Troy Kisky | 573d131 | 2013-09-25 18:41:12 -0700 | [diff] [blame] | 345 | mv_flush_qh(num); |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 346 | |
| 347 | if (in) |
| 348 | bit = EPT_TX(num); |
| 349 | else |
| 350 | bit = EPT_RX(num); |
| 351 | |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 352 | writel(bit, &udc->epprime); |
| 353 | |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | static void handle_ep_complete(struct mv_ep *ep) |
| 358 | { |
| 359 | struct ept_queue_item *item; |
| 360 | int num, in, len; |
| 361 | num = ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; |
| 362 | in = (ep->desc->bEndpointAddress & USB_DIR_IN) != 0; |
| 363 | if (num == 0) |
| 364 | ep->desc = &ep0_out_desc; |
Marek Vasut | a6f6b08 | 2013-07-10 03:16:41 +0200 | [diff] [blame] | 365 | item = mv_get_qtd(num, in); |
Marek Vasut | 5846534 | 2013-07-10 03:16:42 +0200 | [diff] [blame] | 366 | mv_invalidate_qtd(num); |
Wolfgang Denk | bd8ec7e | 2013-10-07 13:07:26 +0200 | [diff] [blame] | 367 | |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 368 | if (item->info & 0xff) |
Troy Kisky | 3703c45 | 2013-09-25 18:41:08 -0700 | [diff] [blame] | 369 | printf("EP%d/%s FAIL info=%x pg0=%x\n", |
| 370 | num, in ? "in" : "out", item->info, item->page0); |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 371 | |
| 372 | len = (item->info >> 16) & 0x7fff; |
Marek Vasut | e0fc582 | 2013-07-10 03:16:43 +0200 | [diff] [blame] | 373 | |
| 374 | mv_debounce(ep); |
| 375 | |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 376 | ep->req.length -= len; |
| 377 | DBG("ept%d %s complete %x\n", |
| 378 | num, in ? "in" : "out", len); |
| 379 | ep->req.complete(&ep->ep, &ep->req); |
| 380 | if (num == 0) { |
| 381 | ep->req.length = 0; |
| 382 | usb_ep_queue(&ep->ep, &ep->req, 0); |
| 383 | ep->desc = &ep0_in_desc; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | #define SETUP(type, request) (((type) << 8) | (request)) |
| 388 | |
| 389 | static void handle_setup(void) |
| 390 | { |
Marek Vasut | 9837732 | 2013-07-10 03:16:29 +0200 | [diff] [blame] | 391 | struct usb_request *req = &controller.ep[0].req; |
Marek Vasut | 1f4b491 | 2013-07-10 03:16:32 +0200 | [diff] [blame] | 392 | struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 393 | struct ept_queue_head *head; |
| 394 | struct usb_ctrlrequest r; |
| 395 | int status = 0; |
| 396 | int num, in, _num, _in, i; |
| 397 | char *buf; |
Marek Vasut | 9212f89 | 2013-07-10 03:16:39 +0200 | [diff] [blame] | 398 | head = mv_get_qh(0, 0); /* EP0 OUT */ |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 399 | |
Marek Vasut | 5846534 | 2013-07-10 03:16:42 +0200 | [diff] [blame] | 400 | mv_invalidate_qh(0); |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 401 | memcpy(&r, head->setup_data, sizeof(struct usb_ctrlrequest)); |
| 402 | writel(EPT_RX(0), &udc->epstat); |
| 403 | DBG("handle setup %s, %x, %x index %x value %x\n", reqname(r.bRequest), |
| 404 | r.bRequestType, r.bRequest, r.wIndex, r.wValue); |
| 405 | |
| 406 | switch (SETUP(r.bRequestType, r.bRequest)) { |
| 407 | case SETUP(USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE): |
| 408 | _num = r.wIndex & 15; |
| 409 | _in = !!(r.wIndex & 0x80); |
| 410 | |
| 411 | if ((r.wValue == 0) && (r.wLength == 0)) { |
| 412 | req->length = 0; |
| 413 | for (i = 0; i < NUM_ENDPOINTS; i++) { |
Marek Vasut | 9837732 | 2013-07-10 03:16:29 +0200 | [diff] [blame] | 414 | if (!controller.ep[i].desc) |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 415 | continue; |
Marek Vasut | 9837732 | 2013-07-10 03:16:29 +0200 | [diff] [blame] | 416 | num = controller.ep[i].desc->bEndpointAddress |
| 417 | & USB_ENDPOINT_NUMBER_MASK; |
| 418 | in = (controller.ep[i].desc->bEndpointAddress |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 419 | & USB_DIR_IN) != 0; |
| 420 | if ((num == _num) && (in == _in)) { |
| 421 | ep_enable(num, in); |
| 422 | usb_ep_queue(controller.gadget.ep0, |
| 423 | req, 0); |
| 424 | break; |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | return; |
| 429 | |
| 430 | case SETUP(USB_RECIP_DEVICE, USB_REQ_SET_ADDRESS): |
| 431 | /* |
| 432 | * write address delayed (will take effect |
| 433 | * after the next IN txn) |
| 434 | */ |
| 435 | writel((r.wValue << 25) | (1 << 24), &udc->devaddr); |
| 436 | req->length = 0; |
| 437 | usb_ep_queue(controller.gadget.ep0, req, 0); |
| 438 | return; |
| 439 | |
| 440 | case SETUP(USB_DIR_IN | USB_RECIP_DEVICE, USB_REQ_GET_STATUS): |
| 441 | req->length = 2; |
| 442 | buf = (char *)req->buf; |
| 443 | buf[0] = 1 << USB_DEVICE_SELF_POWERED; |
| 444 | buf[1] = 0; |
| 445 | usb_ep_queue(controller.gadget.ep0, req, 0); |
| 446 | return; |
| 447 | } |
| 448 | /* pass request up to the gadget driver */ |
| 449 | if (controller.driver) |
| 450 | status = controller.driver->setup(&controller.gadget, &r); |
| 451 | else |
| 452 | status = -ENODEV; |
| 453 | |
| 454 | if (!status) |
| 455 | return; |
| 456 | DBG("STALL reqname %s type %x value %x, index %x\n", |
| 457 | reqname(r.bRequest), r.bRequestType, r.wValue, r.wIndex); |
| 458 | writel((1<<16) | (1 << 0), &udc->epctrl[0]); |
| 459 | } |
| 460 | |
| 461 | static void stop_activity(void) |
| 462 | { |
| 463 | int i, num, in; |
| 464 | struct ept_queue_head *head; |
Marek Vasut | 1f4b491 | 2013-07-10 03:16:32 +0200 | [diff] [blame] | 465 | struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 466 | writel(readl(&udc->epcomp), &udc->epcomp); |
| 467 | writel(readl(&udc->epstat), &udc->epstat); |
| 468 | writel(0xffffffff, &udc->epflush); |
| 469 | |
| 470 | /* error out any pending reqs */ |
| 471 | for (i = 0; i < NUM_ENDPOINTS; i++) { |
| 472 | if (i != 0) |
| 473 | writel(0, &udc->epctrl[i]); |
Marek Vasut | 9837732 | 2013-07-10 03:16:29 +0200 | [diff] [blame] | 474 | if (controller.ep[i].desc) { |
| 475 | num = controller.ep[i].desc->bEndpointAddress |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 476 | & USB_ENDPOINT_NUMBER_MASK; |
Marek Vasut | 9837732 | 2013-07-10 03:16:29 +0200 | [diff] [blame] | 477 | in = (controller.ep[i].desc->bEndpointAddress |
| 478 | & USB_DIR_IN) != 0; |
Marek Vasut | 9212f89 | 2013-07-10 03:16:39 +0200 | [diff] [blame] | 479 | head = mv_get_qh(num, in); |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 480 | head->info = INFO_ACTIVE; |
Marek Vasut | 5846534 | 2013-07-10 03:16:42 +0200 | [diff] [blame] | 481 | mv_flush_qh(num); |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | void udc_irq(void) |
| 487 | { |
Marek Vasut | 1f4b491 | 2013-07-10 03:16:32 +0200 | [diff] [blame] | 488 | struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 489 | unsigned n = readl(&udc->usbsts); |
| 490 | writel(n, &udc->usbsts); |
| 491 | int bit, i, num, in; |
| 492 | |
| 493 | n &= (STS_SLI | STS_URI | STS_PCI | STS_UI | STS_UEI); |
| 494 | if (n == 0) |
| 495 | return; |
| 496 | |
| 497 | if (n & STS_URI) { |
| 498 | DBG("-- reset --\n"); |
| 499 | stop_activity(); |
| 500 | } |
| 501 | if (n & STS_SLI) |
| 502 | DBG("-- suspend --\n"); |
| 503 | |
| 504 | if (n & STS_PCI) { |
| 505 | DBG("-- portchange --\n"); |
| 506 | bit = (readl(&udc->portsc) >> 26) & 3; |
| 507 | if (bit == 2) { |
| 508 | controller.gadget.speed = USB_SPEED_HIGH; |
| 509 | for (i = 1; i < NUM_ENDPOINTS && n; i++) |
Marek Vasut | 9837732 | 2013-07-10 03:16:29 +0200 | [diff] [blame] | 510 | if (controller.ep[i].desc) |
| 511 | controller.ep[i].ep.maxpacket = 512; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 512 | } else { |
| 513 | controller.gadget.speed = USB_SPEED_FULL; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | if (n & STS_UEI) |
| 518 | printf("<UEI %x>\n", readl(&udc->epcomp)); |
| 519 | |
| 520 | if ((n & STS_UI) || (n & STS_UEI)) { |
| 521 | n = readl(&udc->epstat); |
| 522 | if (n & EPT_RX(0)) |
| 523 | handle_setup(); |
| 524 | |
| 525 | n = readl(&udc->epcomp); |
| 526 | if (n != 0) |
| 527 | writel(n, &udc->epcomp); |
| 528 | |
| 529 | for (i = 0; i < NUM_ENDPOINTS && n; i++) { |
Marek Vasut | 9837732 | 2013-07-10 03:16:29 +0200 | [diff] [blame] | 530 | if (controller.ep[i].desc) { |
| 531 | num = controller.ep[i].desc->bEndpointAddress |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 532 | & USB_ENDPOINT_NUMBER_MASK; |
Marek Vasut | 9837732 | 2013-07-10 03:16:29 +0200 | [diff] [blame] | 533 | in = (controller.ep[i].desc->bEndpointAddress |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 534 | & USB_DIR_IN) != 0; |
| 535 | bit = (in) ? EPT_TX(num) : EPT_RX(num); |
| 536 | if (n & bit) |
Marek Vasut | 9837732 | 2013-07-10 03:16:29 +0200 | [diff] [blame] | 537 | handle_ep_complete(&controller.ep[i]); |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | int usb_gadget_handle_interrupts(void) |
| 544 | { |
| 545 | u32 value; |
Marek Vasut | 1f4b491 | 2013-07-10 03:16:32 +0200 | [diff] [blame] | 546 | struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 547 | |
| 548 | value = readl(&udc->usbsts); |
| 549 | if (value) |
| 550 | udc_irq(); |
| 551 | |
| 552 | return value; |
| 553 | } |
| 554 | |
| 555 | static int mv_pullup(struct usb_gadget *gadget, int is_on) |
| 556 | { |
Marek Vasut | 1f4b491 | 2013-07-10 03:16:32 +0200 | [diff] [blame] | 557 | struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 558 | if (is_on) { |
| 559 | /* RESET */ |
| 560 | writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RST, &udc->usbcmd); |
| 561 | udelay(200); |
| 562 | |
Marek Vasut | 9212f89 | 2013-07-10 03:16:39 +0200 | [diff] [blame] | 563 | writel((unsigned)controller.epts, &udc->epinitaddr); |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 564 | |
| 565 | /* select DEVICE mode */ |
| 566 | writel(USBMODE_DEVICE, &udc->usbmode); |
| 567 | |
| 568 | writel(0xffffffff, &udc->epflush); |
| 569 | |
| 570 | /* Turn on the USB connection by enabling the pullup resistor */ |
| 571 | writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RUN, &udc->usbcmd); |
| 572 | } else { |
| 573 | stop_activity(); |
| 574 | writel(USBCMD_FS2, &udc->usbcmd); |
| 575 | udelay(800); |
| 576 | if (controller.driver) |
| 577 | controller.driver->disconnect(gadget); |
| 578 | } |
| 579 | |
| 580 | return 0; |
| 581 | } |
| 582 | |
| 583 | void udc_disconnect(void) |
| 584 | { |
Marek Vasut | 1f4b491 | 2013-07-10 03:16:32 +0200 | [diff] [blame] | 585 | struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 586 | /* disable pullup */ |
| 587 | stop_activity(); |
| 588 | writel(USBCMD_FS2, &udc->usbcmd); |
| 589 | udelay(800); |
| 590 | if (controller.driver) |
| 591 | controller.driver->disconnect(&controller.gadget); |
| 592 | } |
| 593 | |
| 594 | static int mvudc_probe(void) |
| 595 | { |
| 596 | struct ept_queue_head *head; |
Marek Vasut | b62fa7d | 2013-07-10 03:16:40 +0200 | [diff] [blame] | 597 | uint8_t *imem; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 598 | int i; |
Marek Vasut | 456707d | 2013-07-10 03:16:37 +0200 | [diff] [blame] | 599 | |
Marek Vasut | 806d76c | 2013-07-10 03:16:34 +0200 | [diff] [blame] | 600 | const int num = 2 * NUM_ENDPOINTS; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 601 | |
Marek Vasut | 456707d | 2013-07-10 03:16:37 +0200 | [diff] [blame] | 602 | const int eplist_min_align = 4096; |
| 603 | const int eplist_align = roundup(eplist_min_align, ARCH_DMA_MINALIGN); |
| 604 | const int eplist_raw_sz = num * sizeof(struct ept_queue_head); |
| 605 | const int eplist_sz = roundup(eplist_raw_sz, ARCH_DMA_MINALIGN); |
| 606 | |
Marek Vasut | b62fa7d | 2013-07-10 03:16:40 +0200 | [diff] [blame] | 607 | const int ilist_align = roundup(ARCH_DMA_MINALIGN, 32); |
| 608 | const int ilist_ent_raw_sz = 2 * sizeof(struct ept_queue_item); |
| 609 | const int ilist_ent_sz = roundup(ilist_ent_raw_sz, ARCH_DMA_MINALIGN); |
| 610 | const int ilist_sz = NUM_ENDPOINTS * ilist_ent_sz; |
| 611 | |
Marek Vasut | 456707d | 2013-07-10 03:16:37 +0200 | [diff] [blame] | 612 | /* The QH list must be aligned to 4096 bytes. */ |
| 613 | controller.epts = memalign(eplist_align, eplist_sz); |
| 614 | if (!controller.epts) |
| 615 | return -ENOMEM; |
| 616 | memset(controller.epts, 0, eplist_sz); |
| 617 | |
Marek Vasut | b62fa7d | 2013-07-10 03:16:40 +0200 | [diff] [blame] | 618 | /* |
| 619 | * Each qTD item must be 32-byte aligned, each qTD touple must be |
| 620 | * cacheline aligned. There are two qTD items for each endpoint and |
| 621 | * only one of them is used for the endpoint at time, so we can group |
| 622 | * them together. |
| 623 | */ |
| 624 | controller.items_mem = memalign(ilist_align, ilist_sz); |
| 625 | if (!controller.items_mem) { |
| 626 | free(controller.epts); |
| 627 | return -ENOMEM; |
| 628 | } |
Troy Kisky | 0dee8ce | 2013-09-25 18:41:14 -0700 | [diff] [blame^] | 629 | memset(controller.items_mem, 0, ilist_sz); |
Marek Vasut | b62fa7d | 2013-07-10 03:16:40 +0200 | [diff] [blame] | 630 | |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 631 | for (i = 0; i < 2 * NUM_ENDPOINTS; i++) { |
| 632 | /* |
Marek Vasut | 456707d | 2013-07-10 03:16:37 +0200 | [diff] [blame] | 633 | * Configure QH for each endpoint. The structure of the QH list |
| 634 | * is such that each two subsequent fields, N and N+1 where N is |
| 635 | * even, in the QH list represent QH for one endpoint. The Nth |
| 636 | * entry represents OUT configuration and the N+1th entry does |
| 637 | * represent IN configuration of the endpoint. |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 638 | */ |
Marek Vasut | 393004e | 2013-07-10 03:16:36 +0200 | [diff] [blame] | 639 | head = controller.epts + i; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 640 | if (i < 2) |
| 641 | head->config = CONFIG_MAX_PKT(EP0_MAX_PACKET_SIZE) |
| 642 | | CONFIG_ZLT | CONFIG_IOS; |
| 643 | else |
| 644 | head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE) |
| 645 | | CONFIG_ZLT; |
| 646 | head->next = TERMINATE; |
| 647 | head->info = 0; |
| 648 | |
Marek Vasut | b62fa7d | 2013-07-10 03:16:40 +0200 | [diff] [blame] | 649 | imem = controller.items_mem + ((i >> 1) * ilist_ent_sz); |
| 650 | if (i & 1) |
| 651 | imem += sizeof(struct ept_queue_item); |
| 652 | |
| 653 | controller.items[i] = (struct ept_queue_item *)imem; |
Marek Vasut | 5846534 | 2013-07-10 03:16:42 +0200 | [diff] [blame] | 654 | |
| 655 | if (i & 1) { |
| 656 | mv_flush_qh(i - 1); |
| 657 | mv_flush_qtd(i - 1); |
| 658 | } |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | INIT_LIST_HEAD(&controller.gadget.ep_list); |
Marek Vasut | e126341 | 2013-07-10 03:16:30 +0200 | [diff] [blame] | 662 | |
| 663 | /* Init EP 0 */ |
| 664 | 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] | 665 | controller.ep[0].desc = &ep0_in_desc; |
Marek Vasut | e126341 | 2013-07-10 03:16:30 +0200 | [diff] [blame] | 666 | controller.gadget.ep0 = &controller.ep[0].ep; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 667 | INIT_LIST_HEAD(&controller.gadget.ep0->ep_list); |
Marek Vasut | e126341 | 2013-07-10 03:16:30 +0200 | [diff] [blame] | 668 | |
| 669 | /* Init EP 1..n */ |
| 670 | for (i = 1; i < NUM_ENDPOINTS; i++) { |
| 671 | memcpy(&controller.ep[i].ep, &mv_ep_init[1], |
| 672 | sizeof(*mv_ep_init)); |
| 673 | list_add_tail(&controller.ep[i].ep.ep_list, |
| 674 | &controller.gadget.ep_list); |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 675 | } |
Marek Vasut | e126341 | 2013-07-10 03:16:30 +0200 | [diff] [blame] | 676 | |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 677 | return 0; |
| 678 | } |
| 679 | |
| 680 | int usb_gadget_register_driver(struct usb_gadget_driver *driver) |
| 681 | { |
Marek Vasut | 1f4b491 | 2013-07-10 03:16:32 +0200 | [diff] [blame] | 682 | struct mv_udc *udc; |
| 683 | int ret; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 684 | |
Marek Vasut | fe67fe7 | 2013-07-10 03:16:33 +0200 | [diff] [blame] | 685 | if (!driver) |
| 686 | return -EINVAL; |
| 687 | if (!driver->bind || !driver->setup || !driver->disconnect) |
| 688 | return -EINVAL; |
| 689 | if (driver->speed != USB_SPEED_FULL && driver->speed != USB_SPEED_HIGH) |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 690 | return -EINVAL; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 691 | |
Marek Vasut | 1f4b491 | 2013-07-10 03:16:32 +0200 | [diff] [blame] | 692 | ret = usb_lowlevel_init(0, (void **)&controller.ctrl); |
| 693 | if (ret) |
| 694 | return ret; |
| 695 | |
| 696 | ret = mvudc_probe(); |
| 697 | if (!ret) { |
| 698 | udc = (struct mv_udc *)controller.ctrl->hcor; |
| 699 | |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 700 | /* select ULPI phy */ |
| 701 | writel(PTS(PTS_ENABLE) | PFSC, &udc->portsc); |
| 702 | } |
Marek Vasut | 1f4b491 | 2013-07-10 03:16:32 +0200 | [diff] [blame] | 703 | |
| 704 | ret = driver->bind(&controller.gadget); |
| 705 | if (ret) { |
| 706 | DBG("driver->bind() returned %d\n", ret); |
| 707 | return ret; |
Lei Wen | a91ee2a | 2011-10-05 08:11:40 -0700 | [diff] [blame] | 708 | } |
| 709 | controller.driver = driver; |
| 710 | |
| 711 | return 0; |
| 712 | } |
| 713 | |
| 714 | int usb_gadget_unregister_driver(struct usb_gadget_driver *driver) |
| 715 | { |
| 716 | return 0; |
| 717 | } |