1 /* 2 * Driver for the Atmel USBA high speed USB device controller 3 * 4 * Copyright (C) 2005-2007 Atmel Corporation 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 2 as 8 * published by the Free Software Foundation. 9 */ 10 #include <linux/clk.h> 11 #include <linux/module.h> 12 #include <linux/init.h> 13 #include <linux/interrupt.h> 14 #include <linux/io.h> 15 #include <linux/slab.h> 16 #include <linux/device.h> 17 #include <linux/dma-mapping.h> 18 #include <linux/list.h> 19 #include <linux/platform_device.h> 20 #include <linux/usb/ch9.h> 21 #include <linux/usb/gadget.h> 22 #include <linux/usb/atmel_usba_udc.h> 23 #include <linux/delay.h> 24 #include <linux/platform_data/atmel.h> 25 #include <linux/of.h> 26 #include <linux/of_gpio.h> 27 28 #include <asm/gpio.h> 29 30 #include "atmel_usba_udc.h" 31 32 #ifdef CONFIG_USB_GADGET_DEBUG_FS 33 #include <linux/debugfs.h> 34 #include <linux/uaccess.h> 35 36 static int queue_dbg_open(struct inode *inode, struct file *file) 37 { 38 struct usba_ep *ep = inode->i_private; 39 struct usba_request *req, *req_copy; 40 struct list_head *queue_data; 41 42 queue_data = kmalloc(sizeof(*queue_data), GFP_KERNEL); 43 if (!queue_data) 44 return -ENOMEM; 45 INIT_LIST_HEAD(queue_data); 46 47 spin_lock_irq(&ep->udc->lock); 48 list_for_each_entry(req, &ep->queue, queue) { 49 req_copy = kmemdup(req, sizeof(*req_copy), GFP_ATOMIC); 50 if (!req_copy) 51 goto fail; 52 list_add_tail(&req_copy->queue, queue_data); 53 } 54 spin_unlock_irq(&ep->udc->lock); 55 56 file->private_data = queue_data; 57 return 0; 58 59 fail: 60 spin_unlock_irq(&ep->udc->lock); 61 list_for_each_entry_safe(req, req_copy, queue_data, queue) { 62 list_del(&req->queue); 63 kfree(req); 64 } 65 kfree(queue_data); 66 return -ENOMEM; 67 } 68 69 /* 70 * bbbbbbbb llllllll IZS sssss nnnn FDL\n\0 71 * 72 * b: buffer address 73 * l: buffer length 74 * I/i: interrupt/no interrupt 75 * Z/z: zero/no zero 76 * S/s: short ok/short not ok 77 * s: status 78 * n: nr_packets 79 * F/f: submitted/not submitted to FIFO 80 * D/d: using/not using DMA 81 * L/l: last transaction/not last transaction 82 */ 83 static ssize_t queue_dbg_read(struct file *file, char __user *buf, 84 size_t nbytes, loff_t *ppos) 85 { 86 struct list_head *queue = file->private_data; 87 struct usba_request *req, *tmp_req; 88 size_t len, remaining, actual = 0; 89 char tmpbuf[38]; 90 91 if (!access_ok(VERIFY_WRITE, buf, nbytes)) 92 return -EFAULT; 93 94 mutex_lock(&file_inode(file)->i_mutex); 95 list_for_each_entry_safe(req, tmp_req, queue, queue) { 96 len = snprintf(tmpbuf, sizeof(tmpbuf), 97 "%8p %08x %c%c%c %5d %c%c%c\n", 98 req->req.buf, req->req.length, 99 req->req.no_interrupt ? 'i' : 'I', 100 req->req.zero ? 'Z' : 'z', 101 req->req.short_not_ok ? 's' : 'S', 102 req->req.status, 103 req->submitted ? 'F' : 'f', 104 req->using_dma ? 'D' : 'd', 105 req->last_transaction ? 'L' : 'l'); 106 len = min(len, sizeof(tmpbuf)); 107 if (len > nbytes) 108 break; 109 110 list_del(&req->queue); 111 kfree(req); 112 113 remaining = __copy_to_user(buf, tmpbuf, len); 114 actual += len - remaining; 115 if (remaining) 116 break; 117 118 nbytes -= len; 119 buf += len; 120 } 121 mutex_unlock(&file_inode(file)->i_mutex); 122 123 return actual; 124 } 125 126 static int queue_dbg_release(struct inode *inode, struct file *file) 127 { 128 struct list_head *queue_data = file->private_data; 129 struct usba_request *req, *tmp_req; 130 131 list_for_each_entry_safe(req, tmp_req, queue_data, queue) { 132 list_del(&req->queue); 133 kfree(req); 134 } 135 kfree(queue_data); 136 return 0; 137 } 138 139 static int regs_dbg_open(struct inode *inode, struct file *file) 140 { 141 struct usba_udc *udc; 142 unsigned int i; 143 u32 *data; 144 int ret = -ENOMEM; 145 146 mutex_lock(&inode->i_mutex); 147 udc = inode->i_private; 148 data = kmalloc(inode->i_size, GFP_KERNEL); 149 if (!data) 150 goto out; 151 152 spin_lock_irq(&udc->lock); 153 for (i = 0; i < inode->i_size / 4; i++) 154 data[i] = __raw_readl(udc->regs + i * 4); 155 spin_unlock_irq(&udc->lock); 156 157 file->private_data = data; 158 ret = 0; 159 160 out: 161 mutex_unlock(&inode->i_mutex); 162 163 return ret; 164 } 165 166 static ssize_t regs_dbg_read(struct file *file, char __user *buf, 167 size_t nbytes, loff_t *ppos) 168 { 169 struct inode *inode = file_inode(file); 170 int ret; 171 172 mutex_lock(&inode->i_mutex); 173 ret = simple_read_from_buffer(buf, nbytes, ppos, 174 file->private_data, 175 file_inode(file)->i_size); 176 mutex_unlock(&inode->i_mutex); 177 178 return ret; 179 } 180 181 static int regs_dbg_release(struct inode *inode, struct file *file) 182 { 183 kfree(file->private_data); 184 return 0; 185 } 186 187 const struct file_operations queue_dbg_fops = { 188 .owner = THIS_MODULE, 189 .open = queue_dbg_open, 190 .llseek = no_llseek, 191 .read = queue_dbg_read, 192 .release = queue_dbg_release, 193 }; 194 195 const struct file_operations regs_dbg_fops = { 196 .owner = THIS_MODULE, 197 .open = regs_dbg_open, 198 .llseek = generic_file_llseek, 199 .read = regs_dbg_read, 200 .release = regs_dbg_release, 201 }; 202 203 static void usba_ep_init_debugfs(struct usba_udc *udc, 204 struct usba_ep *ep) 205 { 206 struct dentry *ep_root; 207 208 ep_root = debugfs_create_dir(ep->ep.name, udc->debugfs_root); 209 if (!ep_root) 210 goto err_root; 211 ep->debugfs_dir = ep_root; 212 213 ep->debugfs_queue = debugfs_create_file("queue", 0400, ep_root, 214 ep, &queue_dbg_fops); 215 if (!ep->debugfs_queue) 216 goto err_queue; 217 218 if (ep->can_dma) { 219 ep->debugfs_dma_status 220 = debugfs_create_u32("dma_status", 0400, ep_root, 221 &ep->last_dma_status); 222 if (!ep->debugfs_dma_status) 223 goto err_dma_status; 224 } 225 if (ep_is_control(ep)) { 226 ep->debugfs_state 227 = debugfs_create_u32("state", 0400, ep_root, 228 &ep->state); 229 if (!ep->debugfs_state) 230 goto err_state; 231 } 232 233 return; 234 235 err_state: 236 if (ep->can_dma) 237 debugfs_remove(ep->debugfs_dma_status); 238 err_dma_status: 239 debugfs_remove(ep->debugfs_queue); 240 err_queue: 241 debugfs_remove(ep_root); 242 err_root: 243 dev_err(&ep->udc->pdev->dev, 244 "failed to create debugfs directory for %s\n", ep->ep.name); 245 } 246 247 static void usba_ep_cleanup_debugfs(struct usba_ep *ep) 248 { 249 debugfs_remove(ep->debugfs_queue); 250 debugfs_remove(ep->debugfs_dma_status); 251 debugfs_remove(ep->debugfs_state); 252 debugfs_remove(ep->debugfs_dir); 253 ep->debugfs_dma_status = NULL; 254 ep->debugfs_dir = NULL; 255 } 256 257 static void usba_init_debugfs(struct usba_udc *udc) 258 { 259 struct dentry *root, *regs; 260 struct resource *regs_resource; 261 262 root = debugfs_create_dir(udc->gadget.name, NULL); 263 if (IS_ERR(root) || !root) 264 goto err_root; 265 udc->debugfs_root = root; 266 267 regs = debugfs_create_file("regs", 0400, root, udc, ®s_dbg_fops); 268 if (!regs) 269 goto err_regs; 270 271 regs_resource = platform_get_resource(udc->pdev, IORESOURCE_MEM, 272 CTRL_IOMEM_ID); 273 regs->d_inode->i_size = resource_size(regs_resource); 274 udc->debugfs_regs = regs; 275 276 usba_ep_init_debugfs(udc, to_usba_ep(udc->gadget.ep0)); 277 278 return; 279 280 err_regs: 281 debugfs_remove(root); 282 err_root: 283 udc->debugfs_root = NULL; 284 dev_err(&udc->pdev->dev, "debugfs is not available\n"); 285 } 286 287 static void usba_cleanup_debugfs(struct usba_udc *udc) 288 { 289 usba_ep_cleanup_debugfs(to_usba_ep(udc->gadget.ep0)); 290 debugfs_remove(udc->debugfs_regs); 291 debugfs_remove(udc->debugfs_root); 292 udc->debugfs_regs = NULL; 293 udc->debugfs_root = NULL; 294 } 295 #else 296 static inline void usba_ep_init_debugfs(struct usba_udc *udc, 297 struct usba_ep *ep) 298 { 299 300 } 301 302 static inline void usba_ep_cleanup_debugfs(struct usba_ep *ep) 303 { 304 305 } 306 307 static inline void usba_init_debugfs(struct usba_udc *udc) 308 { 309 310 } 311 312 static inline void usba_cleanup_debugfs(struct usba_udc *udc) 313 { 314 315 } 316 #endif 317 318 static int vbus_is_present(struct usba_udc *udc) 319 { 320 if (gpio_is_valid(udc->vbus_pin)) 321 return gpio_get_value(udc->vbus_pin) ^ udc->vbus_pin_inverted; 322 323 /* No Vbus detection: Assume always present */ 324 return 1; 325 } 326 327 #if defined(CONFIG_ARCH_AT91SAM9RL) 328 329 #include <linux/clk/at91_pmc.h> 330 331 static void toggle_bias(int is_on) 332 { 333 unsigned int uckr = at91_pmc_read(AT91_CKGR_UCKR); 334 335 if (is_on) 336 at91_pmc_write(AT91_CKGR_UCKR, uckr | AT91_PMC_BIASEN); 337 else 338 at91_pmc_write(AT91_CKGR_UCKR, uckr & ~(AT91_PMC_BIASEN)); 339 } 340 341 #else 342 343 static void toggle_bias(int is_on) 344 { 345 } 346 347 #endif /* CONFIG_ARCH_AT91SAM9RL */ 348 349 static void next_fifo_transaction(struct usba_ep *ep, struct usba_request *req) 350 { 351 unsigned int transaction_len; 352 353 transaction_len = req->req.length - req->req.actual; 354 req->last_transaction = 1; 355 if (transaction_len > ep->ep.maxpacket) { 356 transaction_len = ep->ep.maxpacket; 357 req->last_transaction = 0; 358 } else if (transaction_len == ep->ep.maxpacket && req->req.zero) 359 req->last_transaction = 0; 360 361 DBG(DBG_QUEUE, "%s: submit_transaction, req %p (length %d)%s\n", 362 ep->ep.name, req, transaction_len, 363 req->last_transaction ? ", done" : ""); 364 365 memcpy_toio(ep->fifo, req->req.buf + req->req.actual, transaction_len); 366 usba_ep_writel(ep, SET_STA, USBA_TX_PK_RDY); 367 req->req.actual += transaction_len; 368 } 369 370 static void submit_request(struct usba_ep *ep, struct usba_request *req) 371 { 372 DBG(DBG_QUEUE, "%s: submit_request: req %p (length %d)\n", 373 ep->ep.name, req, req->req.length); 374 375 req->req.actual = 0; 376 req->submitted = 1; 377 378 if (req->using_dma) { 379 if (req->req.length == 0) { 380 usba_ep_writel(ep, CTL_ENB, USBA_TX_PK_RDY); 381 return; 382 } 383 384 if (req->req.zero) 385 usba_ep_writel(ep, CTL_ENB, USBA_SHORT_PACKET); 386 else 387 usba_ep_writel(ep, CTL_DIS, USBA_SHORT_PACKET); 388 389 usba_dma_writel(ep, ADDRESS, req->req.dma); 390 usba_dma_writel(ep, CONTROL, req->ctrl); 391 } else { 392 next_fifo_transaction(ep, req); 393 if (req->last_transaction) { 394 usba_ep_writel(ep, CTL_DIS, USBA_TX_PK_RDY); 395 usba_ep_writel(ep, CTL_ENB, USBA_TX_COMPLETE); 396 } else { 397 usba_ep_writel(ep, CTL_DIS, USBA_TX_COMPLETE); 398 usba_ep_writel(ep, CTL_ENB, USBA_TX_PK_RDY); 399 } 400 } 401 } 402 403 static void submit_next_request(struct usba_ep *ep) 404 { 405 struct usba_request *req; 406 407 if (list_empty(&ep->queue)) { 408 usba_ep_writel(ep, CTL_DIS, USBA_TX_PK_RDY | USBA_RX_BK_RDY); 409 return; 410 } 411 412 req = list_entry(ep->queue.next, struct usba_request, queue); 413 if (!req->submitted) 414 submit_request(ep, req); 415 } 416 417 static void send_status(struct usba_udc *udc, struct usba_ep *ep) 418 { 419 ep->state = STATUS_STAGE_IN; 420 usba_ep_writel(ep, SET_STA, USBA_TX_PK_RDY); 421 usba_ep_writel(ep, CTL_ENB, USBA_TX_COMPLETE); 422 } 423 424 static void receive_data(struct usba_ep *ep) 425 { 426 struct usba_udc *udc = ep->udc; 427 struct usba_request *req; 428 unsigned long status; 429 unsigned int bytecount, nr_busy; 430 int is_complete = 0; 431 432 status = usba_ep_readl(ep, STA); 433 nr_busy = USBA_BFEXT(BUSY_BANKS, status); 434 435 DBG(DBG_QUEUE, "receive data: nr_busy=%u\n", nr_busy); 436 437 while (nr_busy > 0) { 438 if (list_empty(&ep->queue)) { 439 usba_ep_writel(ep, CTL_DIS, USBA_RX_BK_RDY); 440 break; 441 } 442 req = list_entry(ep->queue.next, 443 struct usba_request, queue); 444 445 bytecount = USBA_BFEXT(BYTE_COUNT, status); 446 447 if (status & (1 << 31)) 448 is_complete = 1; 449 if (req->req.actual + bytecount >= req->req.length) { 450 is_complete = 1; 451 bytecount = req->req.length - req->req.actual; 452 } 453 454 memcpy_fromio(req->req.buf + req->req.actual, 455 ep->fifo, bytecount); 456 req->req.actual += bytecount; 457 458 usba_ep_writel(ep, CLR_STA, USBA_RX_BK_RDY); 459 460 if (is_complete) { 461 DBG(DBG_QUEUE, "%s: request done\n", ep->ep.name); 462 req->req.status = 0; 463 list_del_init(&req->queue); 464 usba_ep_writel(ep, CTL_DIS, USBA_RX_BK_RDY); 465 spin_unlock(&udc->lock); 466 usb_gadget_giveback_request(&ep->ep, &req->req); 467 spin_lock(&udc->lock); 468 } 469 470 status = usba_ep_readl(ep, STA); 471 nr_busy = USBA_BFEXT(BUSY_BANKS, status); 472 473 if (is_complete && ep_is_control(ep)) { 474 send_status(udc, ep); 475 break; 476 } 477 } 478 } 479 480 static void 481 request_complete(struct usba_ep *ep, struct usba_request *req, int status) 482 { 483 struct usba_udc *udc = ep->udc; 484 485 WARN_ON(!list_empty(&req->queue)); 486 487 if (req->req.status == -EINPROGRESS) 488 req->req.status = status; 489 490 if (req->using_dma) 491 usb_gadget_unmap_request(&udc->gadget, &req->req, ep->is_in); 492 493 DBG(DBG_GADGET | DBG_REQ, 494 "%s: req %p complete: status %d, actual %u\n", 495 ep->ep.name, req, req->req.status, req->req.actual); 496 497 spin_unlock(&udc->lock); 498 usb_gadget_giveback_request(&ep->ep, &req->req); 499 spin_lock(&udc->lock); 500 } 501 502 static void 503 request_complete_list(struct usba_ep *ep, struct list_head *list, int status) 504 { 505 struct usba_request *req, *tmp_req; 506 507 list_for_each_entry_safe(req, tmp_req, list, queue) { 508 list_del_init(&req->queue); 509 request_complete(ep, req, status); 510 } 511 } 512 513 static int 514 usba_ep_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc) 515 { 516 struct usba_ep *ep = to_usba_ep(_ep); 517 struct usba_udc *udc = ep->udc; 518 unsigned long flags, ept_cfg, maxpacket; 519 unsigned int nr_trans; 520 521 DBG(DBG_GADGET, "%s: ep_enable: desc=%p\n", ep->ep.name, desc); 522 523 maxpacket = usb_endpoint_maxp(desc) & 0x7ff; 524 525 if (((desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) != ep->index) 526 || ep->index == 0 527 || desc->bDescriptorType != USB_DT_ENDPOINT 528 || maxpacket == 0 529 || maxpacket > ep->fifo_size) { 530 DBG(DBG_ERR, "ep_enable: Invalid argument"); 531 return -EINVAL; 532 } 533 534 ep->is_isoc = 0; 535 ep->is_in = 0; 536 537 if (maxpacket <= 8) 538 ept_cfg = USBA_BF(EPT_SIZE, USBA_EPT_SIZE_8); 539 else 540 /* LSB is bit 1, not 0 */ 541 ept_cfg = USBA_BF(EPT_SIZE, fls(maxpacket - 1) - 3); 542 543 DBG(DBG_HW, "%s: EPT_SIZE = %lu (maxpacket = %lu)\n", 544 ep->ep.name, ept_cfg, maxpacket); 545 546 if (usb_endpoint_dir_in(desc)) { 547 ep->is_in = 1; 548 ept_cfg |= USBA_EPT_DIR_IN; 549 } 550 551 switch (usb_endpoint_type(desc)) { 552 case USB_ENDPOINT_XFER_CONTROL: 553 ept_cfg |= USBA_BF(EPT_TYPE, USBA_EPT_TYPE_CONTROL); 554 ept_cfg |= USBA_BF(BK_NUMBER, USBA_BK_NUMBER_ONE); 555 break; 556 case USB_ENDPOINT_XFER_ISOC: 557 if (!ep->can_isoc) { 558 DBG(DBG_ERR, "ep_enable: %s is not isoc capable\n", 559 ep->ep.name); 560 return -EINVAL; 561 } 562 563 /* 564 * Bits 11:12 specify number of _additional_ 565 * transactions per microframe. 566 */ 567 nr_trans = ((usb_endpoint_maxp(desc) >> 11) & 3) + 1; 568 if (nr_trans > 3) 569 return -EINVAL; 570 571 ep->is_isoc = 1; 572 ept_cfg |= USBA_BF(EPT_TYPE, USBA_EPT_TYPE_ISO); 573 574 /* 575 * Do triple-buffering on high-bandwidth iso endpoints. 576 */ 577 if (nr_trans > 1 && ep->nr_banks == 3) 578 ept_cfg |= USBA_BF(BK_NUMBER, USBA_BK_NUMBER_TRIPLE); 579 else 580 ept_cfg |= USBA_BF(BK_NUMBER, USBA_BK_NUMBER_DOUBLE); 581 ept_cfg |= USBA_BF(NB_TRANS, nr_trans); 582 break; 583 case USB_ENDPOINT_XFER_BULK: 584 ept_cfg |= USBA_BF(EPT_TYPE, USBA_EPT_TYPE_BULK); 585 ept_cfg |= USBA_BF(BK_NUMBER, USBA_BK_NUMBER_DOUBLE); 586 break; 587 case USB_ENDPOINT_XFER_INT: 588 ept_cfg |= USBA_BF(EPT_TYPE, USBA_EPT_TYPE_INT); 589 ept_cfg |= USBA_BF(BK_NUMBER, USBA_BK_NUMBER_DOUBLE); 590 break; 591 } 592 593 spin_lock_irqsave(&ep->udc->lock, flags); 594 595 ep->ep.desc = desc; 596 ep->ep.maxpacket = maxpacket; 597 598 usba_ep_writel(ep, CFG, ept_cfg); 599 usba_ep_writel(ep, CTL_ENB, USBA_EPT_ENABLE); 600 601 if (ep->can_dma) { 602 u32 ctrl; 603 604 usba_writel(udc, INT_ENB, 605 (usba_readl(udc, INT_ENB) 606 | USBA_BF(EPT_INT, 1 << ep->index) 607 | USBA_BF(DMA_INT, 1 << ep->index))); 608 ctrl = USBA_AUTO_VALID | USBA_INTDIS_DMA; 609 usba_ep_writel(ep, CTL_ENB, ctrl); 610 } else { 611 usba_writel(udc, INT_ENB, 612 (usba_readl(udc, INT_ENB) 613 | USBA_BF(EPT_INT, 1 << ep->index))); 614 } 615 616 spin_unlock_irqrestore(&udc->lock, flags); 617 618 DBG(DBG_HW, "EPT_CFG%d after init: %#08lx\n", ep->index, 619 (unsigned long)usba_ep_readl(ep, CFG)); 620 DBG(DBG_HW, "INT_ENB after init: %#08lx\n", 621 (unsigned long)usba_readl(udc, INT_ENB)); 622 623 return 0; 624 } 625 626 static int usba_ep_disable(struct usb_ep *_ep) 627 { 628 struct usba_ep *ep = to_usba_ep(_ep); 629 struct usba_udc *udc = ep->udc; 630 LIST_HEAD(req_list); 631 unsigned long flags; 632 633 DBG(DBG_GADGET, "ep_disable: %s\n", ep->ep.name); 634 635 spin_lock_irqsave(&udc->lock, flags); 636 637 if (!ep->ep.desc) { 638 spin_unlock_irqrestore(&udc->lock, flags); 639 /* REVISIT because this driver disables endpoints in 640 * reset_all_endpoints() before calling disconnect(), 641 * most gadget drivers would trigger this non-error ... 642 */ 643 if (udc->gadget.speed != USB_SPEED_UNKNOWN) 644 DBG(DBG_ERR, "ep_disable: %s not enabled\n", 645 ep->ep.name); 646 return -EINVAL; 647 } 648 ep->ep.desc = NULL; 649 650 list_splice_init(&ep->queue, &req_list); 651 if (ep->can_dma) { 652 usba_dma_writel(ep, CONTROL, 0); 653 usba_dma_writel(ep, ADDRESS, 0); 654 usba_dma_readl(ep, STATUS); 655 } 656 usba_ep_writel(ep, CTL_DIS, USBA_EPT_ENABLE); 657 usba_writel(udc, INT_ENB, 658 usba_readl(udc, INT_ENB) 659 & ~USBA_BF(EPT_INT, 1 << ep->index)); 660 661 request_complete_list(ep, &req_list, -ESHUTDOWN); 662 663 spin_unlock_irqrestore(&udc->lock, flags); 664 665 return 0; 666 } 667 668 static struct usb_request * 669 usba_ep_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags) 670 { 671 struct usba_request *req; 672 673 DBG(DBG_GADGET, "ep_alloc_request: %p, 0x%x\n", _ep, gfp_flags); 674 675 req = kzalloc(sizeof(*req), gfp_flags); 676 if (!req) 677 return NULL; 678 679 INIT_LIST_HEAD(&req->queue); 680 681 return &req->req; 682 } 683 684 static void 685 usba_ep_free_request(struct usb_ep *_ep, struct usb_request *_req) 686 { 687 struct usba_request *req = to_usba_req(_req); 688 689 DBG(DBG_GADGET, "ep_free_request: %p, %p\n", _ep, _req); 690 691 kfree(req); 692 } 693 694 static int queue_dma(struct usba_udc *udc, struct usba_ep *ep, 695 struct usba_request *req, gfp_t gfp_flags) 696 { 697 unsigned long flags; 698 int ret; 699 700 DBG(DBG_DMA, "%s: req l/%u d/%08x %c%c%c\n", 701 ep->ep.name, req->req.length, req->req.dma, 702 req->req.zero ? 'Z' : 'z', 703 req->req.short_not_ok ? 'S' : 's', 704 req->req.no_interrupt ? 'I' : 'i'); 705 706 if (req->req.length > 0x10000) { 707 /* Lengths from 0 to 65536 (inclusive) are supported */ 708 DBG(DBG_ERR, "invalid request length %u\n", req->req.length); 709 return -EINVAL; 710 } 711 712 ret = usb_gadget_map_request(&udc->gadget, &req->req, ep->is_in); 713 if (ret) 714 return ret; 715 716 req->using_dma = 1; 717 req->ctrl = USBA_BF(DMA_BUF_LEN, req->req.length) 718 | USBA_DMA_CH_EN | USBA_DMA_END_BUF_IE 719 | USBA_DMA_END_TR_EN | USBA_DMA_END_TR_IE; 720 721 if (ep->is_in) 722 req->ctrl |= USBA_DMA_END_BUF_EN; 723 724 /* 725 * Add this request to the queue and submit for DMA if 726 * possible. Check if we're still alive first -- we may have 727 * received a reset since last time we checked. 728 */ 729 ret = -ESHUTDOWN; 730 spin_lock_irqsave(&udc->lock, flags); 731 if (ep->ep.desc) { 732 if (list_empty(&ep->queue)) 733 submit_request(ep, req); 734 735 list_add_tail(&req->queue, &ep->queue); 736 ret = 0; 737 } 738 spin_unlock_irqrestore(&udc->lock, flags); 739 740 return ret; 741 } 742 743 static int 744 usba_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags) 745 { 746 struct usba_request *req = to_usba_req(_req); 747 struct usba_ep *ep = to_usba_ep(_ep); 748 struct usba_udc *udc = ep->udc; 749 unsigned long flags; 750 int ret; 751 752 DBG(DBG_GADGET | DBG_QUEUE | DBG_REQ, "%s: queue req %p, len %u\n", 753 ep->ep.name, req, _req->length); 754 755 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN || 756 !ep->ep.desc) 757 return -ESHUTDOWN; 758 759 req->submitted = 0; 760 req->using_dma = 0; 761 req->last_transaction = 0; 762 763 _req->status = -EINPROGRESS; 764 _req->actual = 0; 765 766 if (ep->can_dma) 767 return queue_dma(udc, ep, req, gfp_flags); 768 769 /* May have received a reset since last time we checked */ 770 ret = -ESHUTDOWN; 771 spin_lock_irqsave(&udc->lock, flags); 772 if (ep->ep.desc) { 773 list_add_tail(&req->queue, &ep->queue); 774 775 if ((!ep_is_control(ep) && ep->is_in) || 776 (ep_is_control(ep) 777 && (ep->state == DATA_STAGE_IN 778 || ep->state == STATUS_STAGE_IN))) 779 usba_ep_writel(ep, CTL_ENB, USBA_TX_PK_RDY); 780 else 781 usba_ep_writel(ep, CTL_ENB, USBA_RX_BK_RDY); 782 ret = 0; 783 } 784 spin_unlock_irqrestore(&udc->lock, flags); 785 786 return ret; 787 } 788 789 static void 790 usba_update_req(struct usba_ep *ep, struct usba_request *req, u32 status) 791 { 792 req->req.actual = req->req.length - USBA_BFEXT(DMA_BUF_LEN, status); 793 } 794 795 static int stop_dma(struct usba_ep *ep, u32 *pstatus) 796 { 797 unsigned int timeout; 798 u32 status; 799 800 /* 801 * Stop the DMA controller. When writing both CH_EN 802 * and LINK to 0, the other bits are not affected. 803 */ 804 usba_dma_writel(ep, CONTROL, 0); 805 806 /* Wait for the FIFO to empty */ 807 for (timeout = 40; timeout; --timeout) { 808 status = usba_dma_readl(ep, STATUS); 809 if (!(status & USBA_DMA_CH_EN)) 810 break; 811 udelay(1); 812 } 813 814 if (pstatus) 815 *pstatus = status; 816 817 if (timeout == 0) { 818 dev_err(&ep->udc->pdev->dev, 819 "%s: timed out waiting for DMA FIFO to empty\n", 820 ep->ep.name); 821 return -ETIMEDOUT; 822 } 823 824 return 0; 825 } 826 827 static int usba_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req) 828 { 829 struct usba_ep *ep = to_usba_ep(_ep); 830 struct usba_udc *udc = ep->udc; 831 struct usba_request *req = to_usba_req(_req); 832 unsigned long flags; 833 u32 status; 834 835 DBG(DBG_GADGET | DBG_QUEUE, "ep_dequeue: %s, req %p\n", 836 ep->ep.name, req); 837 838 spin_lock_irqsave(&udc->lock, flags); 839 840 if (req->using_dma) { 841 /* 842 * If this request is currently being transferred, 843 * stop the DMA controller and reset the FIFO. 844 */ 845 if (ep->queue.next == &req->queue) { 846 status = usba_dma_readl(ep, STATUS); 847 if (status & USBA_DMA_CH_EN) 848 stop_dma(ep, &status); 849 850 #ifdef CONFIG_USB_GADGET_DEBUG_FS 851 ep->last_dma_status = status; 852 #endif 853 854 usba_writel(udc, EPT_RST, 1 << ep->index); 855 856 usba_update_req(ep, req, status); 857 } 858 } 859 860 /* 861 * Errors should stop the queue from advancing until the 862 * completion function returns. 863 */ 864 list_del_init(&req->queue); 865 866 request_complete(ep, req, -ECONNRESET); 867 868 /* Process the next request if any */ 869 submit_next_request(ep); 870 spin_unlock_irqrestore(&udc->lock, flags); 871 872 return 0; 873 } 874 875 static int usba_ep_set_halt(struct usb_ep *_ep, int value) 876 { 877 struct usba_ep *ep = to_usba_ep(_ep); 878 struct usba_udc *udc = ep->udc; 879 unsigned long flags; 880 int ret = 0; 881 882 DBG(DBG_GADGET, "endpoint %s: %s HALT\n", ep->ep.name, 883 value ? "set" : "clear"); 884 885 if (!ep->ep.desc) { 886 DBG(DBG_ERR, "Attempted to halt uninitialized ep %s\n", 887 ep->ep.name); 888 return -ENODEV; 889 } 890 if (ep->is_isoc) { 891 DBG(DBG_ERR, "Attempted to halt isochronous ep %s\n", 892 ep->ep.name); 893 return -ENOTTY; 894 } 895 896 spin_lock_irqsave(&udc->lock, flags); 897 898 /* 899 * We can't halt IN endpoints while there are still data to be 900 * transferred 901 */ 902 if (!list_empty(&ep->queue) 903 || ((value && ep->is_in && (usba_ep_readl(ep, STA) 904 & USBA_BF(BUSY_BANKS, -1L))))) { 905 ret = -EAGAIN; 906 } else { 907 if (value) 908 usba_ep_writel(ep, SET_STA, USBA_FORCE_STALL); 909 else 910 usba_ep_writel(ep, CLR_STA, 911 USBA_FORCE_STALL | USBA_TOGGLE_CLR); 912 usba_ep_readl(ep, STA); 913 } 914 915 spin_unlock_irqrestore(&udc->lock, flags); 916 917 return ret; 918 } 919 920 static int usba_ep_fifo_status(struct usb_ep *_ep) 921 { 922 struct usba_ep *ep = to_usba_ep(_ep); 923 924 return USBA_BFEXT(BYTE_COUNT, usba_ep_readl(ep, STA)); 925 } 926 927 static void usba_ep_fifo_flush(struct usb_ep *_ep) 928 { 929 struct usba_ep *ep = to_usba_ep(_ep); 930 struct usba_udc *udc = ep->udc; 931 932 usba_writel(udc, EPT_RST, 1 << ep->index); 933 } 934 935 static const struct usb_ep_ops usba_ep_ops = { 936 .enable = usba_ep_enable, 937 .disable = usba_ep_disable, 938 .alloc_request = usba_ep_alloc_request, 939 .free_request = usba_ep_free_request, 940 .queue = usba_ep_queue, 941 .dequeue = usba_ep_dequeue, 942 .set_halt = usba_ep_set_halt, 943 .fifo_status = usba_ep_fifo_status, 944 .fifo_flush = usba_ep_fifo_flush, 945 }; 946 947 static int usba_udc_get_frame(struct usb_gadget *gadget) 948 { 949 struct usba_udc *udc = to_usba_udc(gadget); 950 951 return USBA_BFEXT(FRAME_NUMBER, usba_readl(udc, FNUM)); 952 } 953 954 static int usba_udc_wakeup(struct usb_gadget *gadget) 955 { 956 struct usba_udc *udc = to_usba_udc(gadget); 957 unsigned long flags; 958 u32 ctrl; 959 int ret = -EINVAL; 960 961 spin_lock_irqsave(&udc->lock, flags); 962 if (udc->devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) { 963 ctrl = usba_readl(udc, CTRL); 964 usba_writel(udc, CTRL, ctrl | USBA_REMOTE_WAKE_UP); 965 ret = 0; 966 } 967 spin_unlock_irqrestore(&udc->lock, flags); 968 969 return ret; 970 } 971 972 static int 973 usba_udc_set_selfpowered(struct usb_gadget *gadget, int is_selfpowered) 974 { 975 struct usba_udc *udc = to_usba_udc(gadget); 976 unsigned long flags; 977 978 spin_lock_irqsave(&udc->lock, flags); 979 if (is_selfpowered) 980 udc->devstatus |= 1 << USB_DEVICE_SELF_POWERED; 981 else 982 udc->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED); 983 spin_unlock_irqrestore(&udc->lock, flags); 984 985 return 0; 986 } 987 988 static int atmel_usba_start(struct usb_gadget *gadget, 989 struct usb_gadget_driver *driver); 990 static int atmel_usba_stop(struct usb_gadget *gadget, 991 struct usb_gadget_driver *driver); 992 static const struct usb_gadget_ops usba_udc_ops = { 993 .get_frame = usba_udc_get_frame, 994 .wakeup = usba_udc_wakeup, 995 .set_selfpowered = usba_udc_set_selfpowered, 996 .udc_start = atmel_usba_start, 997 .udc_stop = atmel_usba_stop, 998 }; 999 1000 static struct usb_endpoint_descriptor usba_ep0_desc = { 1001 .bLength = USB_DT_ENDPOINT_SIZE, 1002 .bDescriptorType = USB_DT_ENDPOINT, 1003 .bEndpointAddress = 0, 1004 .bmAttributes = USB_ENDPOINT_XFER_CONTROL, 1005 .wMaxPacketSize = cpu_to_le16(64), 1006 /* FIXME: I have no idea what to put here */ 1007 .bInterval = 1, 1008 }; 1009 1010 static void nop_release(struct device *dev) 1011 { 1012 1013 } 1014 1015 static struct usb_gadget usba_gadget_template = { 1016 .ops = &usba_udc_ops, 1017 .max_speed = USB_SPEED_HIGH, 1018 .name = "atmel_usba_udc", 1019 .dev = { 1020 .init_name = "gadget", 1021 .release = nop_release, 1022 }, 1023 }; 1024 1025 /* 1026 * Called with interrupts disabled and udc->lock held. 1027 */ 1028 static void reset_all_endpoints(struct usba_udc *udc) 1029 { 1030 struct usba_ep *ep; 1031 struct usba_request *req, *tmp_req; 1032 1033 usba_writel(udc, EPT_RST, ~0UL); 1034 1035 ep = to_usba_ep(udc->gadget.ep0); 1036 list_for_each_entry_safe(req, tmp_req, &ep->queue, queue) { 1037 list_del_init(&req->queue); 1038 request_complete(ep, req, -ECONNRESET); 1039 } 1040 1041 /* NOTE: normally, the next call to the gadget driver is in 1042 * charge of disabling endpoints... usually disconnect(). 1043 * The exception would be entering a high speed test mode. 1044 * 1045 * FIXME remove this code ... and retest thoroughly. 1046 */ 1047 list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) { 1048 if (ep->ep.desc) { 1049 spin_unlock(&udc->lock); 1050 usba_ep_disable(&ep->ep); 1051 spin_lock(&udc->lock); 1052 } 1053 } 1054 } 1055 1056 static struct usba_ep *get_ep_by_addr(struct usba_udc *udc, u16 wIndex) 1057 { 1058 struct usba_ep *ep; 1059 1060 if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0) 1061 return to_usba_ep(udc->gadget.ep0); 1062 1063 list_for_each_entry (ep, &udc->gadget.ep_list, ep.ep_list) { 1064 u8 bEndpointAddress; 1065 1066 if (!ep->ep.desc) 1067 continue; 1068 bEndpointAddress = ep->ep.desc->bEndpointAddress; 1069 if ((wIndex ^ bEndpointAddress) & USB_DIR_IN) 1070 continue; 1071 if ((bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) 1072 == (wIndex & USB_ENDPOINT_NUMBER_MASK)) 1073 return ep; 1074 } 1075 1076 return NULL; 1077 } 1078 1079 /* Called with interrupts disabled and udc->lock held */ 1080 static inline void set_protocol_stall(struct usba_udc *udc, struct usba_ep *ep) 1081 { 1082 usba_ep_writel(ep, SET_STA, USBA_FORCE_STALL); 1083 ep->state = WAIT_FOR_SETUP; 1084 } 1085 1086 static inline int is_stalled(struct usba_udc *udc, struct usba_ep *ep) 1087 { 1088 if (usba_ep_readl(ep, STA) & USBA_FORCE_STALL) 1089 return 1; 1090 return 0; 1091 } 1092 1093 static inline void set_address(struct usba_udc *udc, unsigned int addr) 1094 { 1095 u32 regval; 1096 1097 DBG(DBG_BUS, "setting address %u...\n", addr); 1098 regval = usba_readl(udc, CTRL); 1099 regval = USBA_BFINS(DEV_ADDR, addr, regval); 1100 usba_writel(udc, CTRL, regval); 1101 } 1102 1103 static int do_test_mode(struct usba_udc *udc) 1104 { 1105 static const char test_packet_buffer[] = { 1106 /* JKJKJKJK * 9 */ 1107 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 1108 /* JJKKJJKK * 8 */ 1109 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 1110 /* JJKKJJKK * 8 */ 1111 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 1112 /* JJJJJJJKKKKKKK * 8 */ 1113 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 1114 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 1115 /* JJJJJJJK * 8 */ 1116 0x7F, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD, 1117 /* {JKKKKKKK * 10}, JK */ 1118 0xFC, 0x7E, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD, 0x7E 1119 }; 1120 struct usba_ep *ep; 1121 struct device *dev = &udc->pdev->dev; 1122 int test_mode; 1123 1124 test_mode = udc->test_mode; 1125 1126 /* Start from a clean slate */ 1127 reset_all_endpoints(udc); 1128 1129 switch (test_mode) { 1130 case 0x0100: 1131 /* Test_J */ 1132 usba_writel(udc, TST, USBA_TST_J_MODE); 1133 dev_info(dev, "Entering Test_J mode...\n"); 1134 break; 1135 case 0x0200: 1136 /* Test_K */ 1137 usba_writel(udc, TST, USBA_TST_K_MODE); 1138 dev_info(dev, "Entering Test_K mode...\n"); 1139 break; 1140 case 0x0300: 1141 /* 1142 * Test_SE0_NAK: Force high-speed mode and set up ep0 1143 * for Bulk IN transfers 1144 */ 1145 ep = &udc->usba_ep[0]; 1146 usba_writel(udc, TST, 1147 USBA_BF(SPEED_CFG, USBA_SPEED_CFG_FORCE_HIGH)); 1148 usba_ep_writel(ep, CFG, 1149 USBA_BF(EPT_SIZE, USBA_EPT_SIZE_64) 1150 | USBA_EPT_DIR_IN 1151 | USBA_BF(EPT_TYPE, USBA_EPT_TYPE_BULK) 1152 | USBA_BF(BK_NUMBER, 1)); 1153 if (!(usba_ep_readl(ep, CFG) & USBA_EPT_MAPPED)) { 1154 set_protocol_stall(udc, ep); 1155 dev_err(dev, "Test_SE0_NAK: ep0 not mapped\n"); 1156 } else { 1157 usba_ep_writel(ep, CTL_ENB, USBA_EPT_ENABLE); 1158 dev_info(dev, "Entering Test_SE0_NAK mode...\n"); 1159 } 1160 break; 1161 case 0x0400: 1162 /* Test_Packet */ 1163 ep = &udc->usba_ep[0]; 1164 usba_ep_writel(ep, CFG, 1165 USBA_BF(EPT_SIZE, USBA_EPT_SIZE_64) 1166 | USBA_EPT_DIR_IN 1167 | USBA_BF(EPT_TYPE, USBA_EPT_TYPE_BULK) 1168 | USBA_BF(BK_NUMBER, 1)); 1169 if (!(usba_ep_readl(ep, CFG) & USBA_EPT_MAPPED)) { 1170 set_protocol_stall(udc, ep); 1171 dev_err(dev, "Test_Packet: ep0 not mapped\n"); 1172 } else { 1173 usba_ep_writel(ep, CTL_ENB, USBA_EPT_ENABLE); 1174 usba_writel(udc, TST, USBA_TST_PKT_MODE); 1175 memcpy_toio(ep->fifo, test_packet_buffer, 1176 sizeof(test_packet_buffer)); 1177 usba_ep_writel(ep, SET_STA, USBA_TX_PK_RDY); 1178 dev_info(dev, "Entering Test_Packet mode...\n"); 1179 } 1180 break; 1181 default: 1182 dev_err(dev, "Invalid test mode: 0x%04x\n", test_mode); 1183 return -EINVAL; 1184 } 1185 1186 return 0; 1187 } 1188 1189 /* Avoid overly long expressions */ 1190 static inline bool feature_is_dev_remote_wakeup(struct usb_ctrlrequest *crq) 1191 { 1192 if (crq->wValue == cpu_to_le16(USB_DEVICE_REMOTE_WAKEUP)) 1193 return true; 1194 return false; 1195 } 1196 1197 static inline bool feature_is_dev_test_mode(struct usb_ctrlrequest *crq) 1198 { 1199 if (crq->wValue == cpu_to_le16(USB_DEVICE_TEST_MODE)) 1200 return true; 1201 return false; 1202 } 1203 1204 static inline bool feature_is_ep_halt(struct usb_ctrlrequest *crq) 1205 { 1206 if (crq->wValue == cpu_to_le16(USB_ENDPOINT_HALT)) 1207 return true; 1208 return false; 1209 } 1210 1211 static int handle_ep0_setup(struct usba_udc *udc, struct usba_ep *ep, 1212 struct usb_ctrlrequest *crq) 1213 { 1214 int retval = 0; 1215 1216 switch (crq->bRequest) { 1217 case USB_REQ_GET_STATUS: { 1218 u16 status; 1219 1220 if (crq->bRequestType == (USB_DIR_IN | USB_RECIP_DEVICE)) { 1221 status = cpu_to_le16(udc->devstatus); 1222 } else if (crq->bRequestType 1223 == (USB_DIR_IN | USB_RECIP_INTERFACE)) { 1224 status = cpu_to_le16(0); 1225 } else if (crq->bRequestType 1226 == (USB_DIR_IN | USB_RECIP_ENDPOINT)) { 1227 struct usba_ep *target; 1228 1229 target = get_ep_by_addr(udc, le16_to_cpu(crq->wIndex)); 1230 if (!target) 1231 goto stall; 1232 1233 status = 0; 1234 if (is_stalled(udc, target)) 1235 status |= cpu_to_le16(1); 1236 } else 1237 goto delegate; 1238 1239 /* Write directly to the FIFO. No queueing is done. */ 1240 if (crq->wLength != cpu_to_le16(sizeof(status))) 1241 goto stall; 1242 ep->state = DATA_STAGE_IN; 1243 __raw_writew(status, ep->fifo); 1244 usba_ep_writel(ep, SET_STA, USBA_TX_PK_RDY); 1245 break; 1246 } 1247 1248 case USB_REQ_CLEAR_FEATURE: { 1249 if (crq->bRequestType == USB_RECIP_DEVICE) { 1250 if (feature_is_dev_remote_wakeup(crq)) 1251 udc->devstatus 1252 &= ~(1 << USB_DEVICE_REMOTE_WAKEUP); 1253 else 1254 /* Can't CLEAR_FEATURE TEST_MODE */ 1255 goto stall; 1256 } else if (crq->bRequestType == USB_RECIP_ENDPOINT) { 1257 struct usba_ep *target; 1258 1259 if (crq->wLength != cpu_to_le16(0) 1260 || !feature_is_ep_halt(crq)) 1261 goto stall; 1262 target = get_ep_by_addr(udc, le16_to_cpu(crq->wIndex)); 1263 if (!target) 1264 goto stall; 1265 1266 usba_ep_writel(target, CLR_STA, USBA_FORCE_STALL); 1267 if (target->index != 0) 1268 usba_ep_writel(target, CLR_STA, 1269 USBA_TOGGLE_CLR); 1270 } else { 1271 goto delegate; 1272 } 1273 1274 send_status(udc, ep); 1275 break; 1276 } 1277 1278 case USB_REQ_SET_FEATURE: { 1279 if (crq->bRequestType == USB_RECIP_DEVICE) { 1280 if (feature_is_dev_test_mode(crq)) { 1281 send_status(udc, ep); 1282 ep->state = STATUS_STAGE_TEST; 1283 udc->test_mode = le16_to_cpu(crq->wIndex); 1284 return 0; 1285 } else if (feature_is_dev_remote_wakeup(crq)) { 1286 udc->devstatus |= 1 << USB_DEVICE_REMOTE_WAKEUP; 1287 } else { 1288 goto stall; 1289 } 1290 } else if (crq->bRequestType == USB_RECIP_ENDPOINT) { 1291 struct usba_ep *target; 1292 1293 if (crq->wLength != cpu_to_le16(0) 1294 || !feature_is_ep_halt(crq)) 1295 goto stall; 1296 1297 target = get_ep_by_addr(udc, le16_to_cpu(crq->wIndex)); 1298 if (!target) 1299 goto stall; 1300 1301 usba_ep_writel(target, SET_STA, USBA_FORCE_STALL); 1302 } else 1303 goto delegate; 1304 1305 send_status(udc, ep); 1306 break; 1307 } 1308 1309 case USB_REQ_SET_ADDRESS: 1310 if (crq->bRequestType != (USB_DIR_OUT | USB_RECIP_DEVICE)) 1311 goto delegate; 1312 1313 set_address(udc, le16_to_cpu(crq->wValue)); 1314 send_status(udc, ep); 1315 ep->state = STATUS_STAGE_ADDR; 1316 break; 1317 1318 default: 1319 delegate: 1320 spin_unlock(&udc->lock); 1321 retval = udc->driver->setup(&udc->gadget, crq); 1322 spin_lock(&udc->lock); 1323 } 1324 1325 return retval; 1326 1327 stall: 1328 pr_err("udc: %s: Invalid setup request: %02x.%02x v%04x i%04x l%d, " 1329 "halting endpoint...\n", 1330 ep->ep.name, crq->bRequestType, crq->bRequest, 1331 le16_to_cpu(crq->wValue), le16_to_cpu(crq->wIndex), 1332 le16_to_cpu(crq->wLength)); 1333 set_protocol_stall(udc, ep); 1334 return -1; 1335 } 1336 1337 static void usba_control_irq(struct usba_udc *udc, struct usba_ep *ep) 1338 { 1339 struct usba_request *req; 1340 u32 epstatus; 1341 u32 epctrl; 1342 1343 restart: 1344 epstatus = usba_ep_readl(ep, STA); 1345 epctrl = usba_ep_readl(ep, CTL); 1346 1347 DBG(DBG_INT, "%s [%d]: s/%08x c/%08x\n", 1348 ep->ep.name, ep->state, epstatus, epctrl); 1349 1350 req = NULL; 1351 if (!list_empty(&ep->queue)) 1352 req = list_entry(ep->queue.next, 1353 struct usba_request, queue); 1354 1355 if ((epctrl & USBA_TX_PK_RDY) && !(epstatus & USBA_TX_PK_RDY)) { 1356 if (req->submitted) 1357 next_fifo_transaction(ep, req); 1358 else 1359 submit_request(ep, req); 1360 1361 if (req->last_transaction) { 1362 usba_ep_writel(ep, CTL_DIS, USBA_TX_PK_RDY); 1363 usba_ep_writel(ep, CTL_ENB, USBA_TX_COMPLETE); 1364 } 1365 goto restart; 1366 } 1367 if ((epstatus & epctrl) & USBA_TX_COMPLETE) { 1368 usba_ep_writel(ep, CLR_STA, USBA_TX_COMPLETE); 1369 1370 switch (ep->state) { 1371 case DATA_STAGE_IN: 1372 usba_ep_writel(ep, CTL_ENB, USBA_RX_BK_RDY); 1373 usba_ep_writel(ep, CTL_DIS, USBA_TX_COMPLETE); 1374 ep->state = STATUS_STAGE_OUT; 1375 break; 1376 case STATUS_STAGE_ADDR: 1377 /* Activate our new address */ 1378 usba_writel(udc, CTRL, (usba_readl(udc, CTRL) 1379 | USBA_FADDR_EN)); 1380 usba_ep_writel(ep, CTL_DIS, USBA_TX_COMPLETE); 1381 ep->state = WAIT_FOR_SETUP; 1382 break; 1383 case STATUS_STAGE_IN: 1384 if (req) { 1385 list_del_init(&req->queue); 1386 request_complete(ep, req, 0); 1387 submit_next_request(ep); 1388 } 1389 usba_ep_writel(ep, CTL_DIS, USBA_TX_COMPLETE); 1390 ep->state = WAIT_FOR_SETUP; 1391 break; 1392 case STATUS_STAGE_TEST: 1393 usba_ep_writel(ep, CTL_DIS, USBA_TX_COMPLETE); 1394 ep->state = WAIT_FOR_SETUP; 1395 if (do_test_mode(udc)) 1396 set_protocol_stall(udc, ep); 1397 break; 1398 default: 1399 pr_err("udc: %s: TXCOMP: Invalid endpoint state %d, " 1400 "halting endpoint...\n", 1401 ep->ep.name, ep->state); 1402 set_protocol_stall(udc, ep); 1403 break; 1404 } 1405 1406 goto restart; 1407 } 1408 if ((epstatus & epctrl) & USBA_RX_BK_RDY) { 1409 switch (ep->state) { 1410 case STATUS_STAGE_OUT: 1411 usba_ep_writel(ep, CLR_STA, USBA_RX_BK_RDY); 1412 usba_ep_writel(ep, CTL_DIS, USBA_RX_BK_RDY); 1413 1414 if (req) { 1415 list_del_init(&req->queue); 1416 request_complete(ep, req, 0); 1417 } 1418 ep->state = WAIT_FOR_SETUP; 1419 break; 1420 1421 case DATA_STAGE_OUT: 1422 receive_data(ep); 1423 break; 1424 1425 default: 1426 usba_ep_writel(ep, CLR_STA, USBA_RX_BK_RDY); 1427 usba_ep_writel(ep, CTL_DIS, USBA_RX_BK_RDY); 1428 pr_err("udc: %s: RXRDY: Invalid endpoint state %d, " 1429 "halting endpoint...\n", 1430 ep->ep.name, ep->state); 1431 set_protocol_stall(udc, ep); 1432 break; 1433 } 1434 1435 goto restart; 1436 } 1437 if (epstatus & USBA_RX_SETUP) { 1438 union { 1439 struct usb_ctrlrequest crq; 1440 unsigned long data[2]; 1441 } crq; 1442 unsigned int pkt_len; 1443 int ret; 1444 1445 if (ep->state != WAIT_FOR_SETUP) { 1446 /* 1447 * Didn't expect a SETUP packet at this 1448 * point. Clean up any pending requests (which 1449 * may be successful). 1450 */ 1451 int status = -EPROTO; 1452 1453 /* 1454 * RXRDY and TXCOMP are dropped when SETUP 1455 * packets arrive. Just pretend we received 1456 * the status packet. 1457 */ 1458 if (ep->state == STATUS_STAGE_OUT 1459 || ep->state == STATUS_STAGE_IN) { 1460 usba_ep_writel(ep, CTL_DIS, USBA_RX_BK_RDY); 1461 status = 0; 1462 } 1463 1464 if (req) { 1465 list_del_init(&req->queue); 1466 request_complete(ep, req, status); 1467 } 1468 } 1469 1470 pkt_len = USBA_BFEXT(BYTE_COUNT, usba_ep_readl(ep, STA)); 1471 DBG(DBG_HW, "Packet length: %u\n", pkt_len); 1472 if (pkt_len != sizeof(crq)) { 1473 pr_warning("udc: Invalid packet length %u " 1474 "(expected %zu)\n", pkt_len, sizeof(crq)); 1475 set_protocol_stall(udc, ep); 1476 return; 1477 } 1478 1479 DBG(DBG_FIFO, "Copying ctrl request from 0x%p:\n", ep->fifo); 1480 memcpy_fromio(crq.data, ep->fifo, sizeof(crq)); 1481 1482 /* Free up one bank in the FIFO so that we can 1483 * generate or receive a reply right away. */ 1484 usba_ep_writel(ep, CLR_STA, USBA_RX_SETUP); 1485 1486 /* printk(KERN_DEBUG "setup: %d: %02x.%02x\n", 1487 ep->state, crq.crq.bRequestType, 1488 crq.crq.bRequest); */ 1489 1490 if (crq.crq.bRequestType & USB_DIR_IN) { 1491 /* 1492 * The USB 2.0 spec states that "if wLength is 1493 * zero, there is no data transfer phase." 1494 * However, testusb #14 seems to actually 1495 * expect a data phase even if wLength = 0... 1496 */ 1497 ep->state = DATA_STAGE_IN; 1498 } else { 1499 if (crq.crq.wLength != cpu_to_le16(0)) 1500 ep->state = DATA_STAGE_OUT; 1501 else 1502 ep->state = STATUS_STAGE_IN; 1503 } 1504 1505 ret = -1; 1506 if (ep->index == 0) 1507 ret = handle_ep0_setup(udc, ep, &crq.crq); 1508 else { 1509 spin_unlock(&udc->lock); 1510 ret = udc->driver->setup(&udc->gadget, &crq.crq); 1511 spin_lock(&udc->lock); 1512 } 1513 1514 DBG(DBG_BUS, "req %02x.%02x, length %d, state %d, ret %d\n", 1515 crq.crq.bRequestType, crq.crq.bRequest, 1516 le16_to_cpu(crq.crq.wLength), ep->state, ret); 1517 1518 if (ret < 0) { 1519 /* Let the host know that we failed */ 1520 set_protocol_stall(udc, ep); 1521 } 1522 } 1523 } 1524 1525 static void usba_ep_irq(struct usba_udc *udc, struct usba_ep *ep) 1526 { 1527 struct usba_request *req; 1528 u32 epstatus; 1529 u32 epctrl; 1530 1531 epstatus = usba_ep_readl(ep, STA); 1532 epctrl = usba_ep_readl(ep, CTL); 1533 1534 DBG(DBG_INT, "%s: interrupt, status: 0x%08x\n", ep->ep.name, epstatus); 1535 1536 while ((epctrl & USBA_TX_PK_RDY) && !(epstatus & USBA_TX_PK_RDY)) { 1537 DBG(DBG_BUS, "%s: TX PK ready\n", ep->ep.name); 1538 1539 if (list_empty(&ep->queue)) { 1540 dev_warn(&udc->pdev->dev, "ep_irq: queue empty\n"); 1541 usba_ep_writel(ep, CTL_DIS, USBA_TX_PK_RDY); 1542 return; 1543 } 1544 1545 req = list_entry(ep->queue.next, struct usba_request, queue); 1546 1547 if (req->using_dma) { 1548 /* Send a zero-length packet */ 1549 usba_ep_writel(ep, SET_STA, 1550 USBA_TX_PK_RDY); 1551 usba_ep_writel(ep, CTL_DIS, 1552 USBA_TX_PK_RDY); 1553 list_del_init(&req->queue); 1554 submit_next_request(ep); 1555 request_complete(ep, req, 0); 1556 } else { 1557 if (req->submitted) 1558 next_fifo_transaction(ep, req); 1559 else 1560 submit_request(ep, req); 1561 1562 if (req->last_transaction) { 1563 list_del_init(&req->queue); 1564 submit_next_request(ep); 1565 request_complete(ep, req, 0); 1566 } 1567 } 1568 1569 epstatus = usba_ep_readl(ep, STA); 1570 epctrl = usba_ep_readl(ep, CTL); 1571 } 1572 if ((epstatus & epctrl) & USBA_RX_BK_RDY) { 1573 DBG(DBG_BUS, "%s: RX data ready\n", ep->ep.name); 1574 receive_data(ep); 1575 usba_ep_writel(ep, CLR_STA, USBA_RX_BK_RDY); 1576 } 1577 } 1578 1579 static void usba_dma_irq(struct usba_udc *udc, struct usba_ep *ep) 1580 { 1581 struct usba_request *req; 1582 u32 status, control, pending; 1583 1584 status = usba_dma_readl(ep, STATUS); 1585 control = usba_dma_readl(ep, CONTROL); 1586 #ifdef CONFIG_USB_GADGET_DEBUG_FS 1587 ep->last_dma_status = status; 1588 #endif 1589 pending = status & control; 1590 DBG(DBG_INT | DBG_DMA, "dma irq, s/%#08x, c/%#08x\n", status, control); 1591 1592 if (status & USBA_DMA_CH_EN) { 1593 dev_err(&udc->pdev->dev, 1594 "DMA_CH_EN is set after transfer is finished!\n"); 1595 dev_err(&udc->pdev->dev, 1596 "status=%#08x, pending=%#08x, control=%#08x\n", 1597 status, pending, control); 1598 1599 /* 1600 * try to pretend nothing happened. We might have to 1601 * do something here... 1602 */ 1603 } 1604 1605 if (list_empty(&ep->queue)) 1606 /* Might happen if a reset comes along at the right moment */ 1607 return; 1608 1609 if (pending & (USBA_DMA_END_TR_ST | USBA_DMA_END_BUF_ST)) { 1610 req = list_entry(ep->queue.next, struct usba_request, queue); 1611 usba_update_req(ep, req, status); 1612 1613 list_del_init(&req->queue); 1614 submit_next_request(ep); 1615 request_complete(ep, req, 0); 1616 } 1617 } 1618 1619 static irqreturn_t usba_udc_irq(int irq, void *devid) 1620 { 1621 struct usba_udc *udc = devid; 1622 u32 status; 1623 u32 dma_status; 1624 u32 ep_status; 1625 1626 spin_lock(&udc->lock); 1627 1628 status = usba_readl(udc, INT_STA); 1629 DBG(DBG_INT, "irq, status=%#08x\n", status); 1630 1631 if (status & USBA_DET_SUSPEND) { 1632 toggle_bias(0); 1633 usba_writel(udc, INT_CLR, USBA_DET_SUSPEND); 1634 DBG(DBG_BUS, "Suspend detected\n"); 1635 if (udc->gadget.speed != USB_SPEED_UNKNOWN 1636 && udc->driver && udc->driver->suspend) { 1637 spin_unlock(&udc->lock); 1638 udc->driver->suspend(&udc->gadget); 1639 spin_lock(&udc->lock); 1640 } 1641 } 1642 1643 if (status & USBA_WAKE_UP) { 1644 toggle_bias(1); 1645 usba_writel(udc, INT_CLR, USBA_WAKE_UP); 1646 DBG(DBG_BUS, "Wake Up CPU detected\n"); 1647 } 1648 1649 if (status & USBA_END_OF_RESUME) { 1650 usba_writel(udc, INT_CLR, USBA_END_OF_RESUME); 1651 DBG(DBG_BUS, "Resume detected\n"); 1652 if (udc->gadget.speed != USB_SPEED_UNKNOWN 1653 && udc->driver && udc->driver->resume) { 1654 spin_unlock(&udc->lock); 1655 udc->driver->resume(&udc->gadget); 1656 spin_lock(&udc->lock); 1657 } 1658 } 1659 1660 dma_status = USBA_BFEXT(DMA_INT, status); 1661 if (dma_status) { 1662 int i; 1663 1664 for (i = 1; i <= USBA_NR_DMAS; i++) 1665 if (dma_status & (1 << i)) 1666 usba_dma_irq(udc, &udc->usba_ep[i]); 1667 } 1668 1669 ep_status = USBA_BFEXT(EPT_INT, status); 1670 if (ep_status) { 1671 int i; 1672 1673 for (i = 0; i < udc->num_ep; i++) 1674 if (ep_status & (1 << i)) { 1675 if (ep_is_control(&udc->usba_ep[i])) 1676 usba_control_irq(udc, &udc->usba_ep[i]); 1677 else 1678 usba_ep_irq(udc, &udc->usba_ep[i]); 1679 } 1680 } 1681 1682 if (status & USBA_END_OF_RESET) { 1683 struct usba_ep *ep0; 1684 1685 usba_writel(udc, INT_CLR, USBA_END_OF_RESET); 1686 reset_all_endpoints(udc); 1687 1688 if (udc->gadget.speed != USB_SPEED_UNKNOWN 1689 && udc->driver && udc->driver->disconnect) { 1690 udc->gadget.speed = USB_SPEED_UNKNOWN; 1691 spin_unlock(&udc->lock); 1692 udc->driver->disconnect(&udc->gadget); 1693 spin_lock(&udc->lock); 1694 } 1695 1696 if (status & USBA_HIGH_SPEED) 1697 udc->gadget.speed = USB_SPEED_HIGH; 1698 else 1699 udc->gadget.speed = USB_SPEED_FULL; 1700 DBG(DBG_BUS, "%s bus reset detected\n", 1701 usb_speed_string(udc->gadget.speed)); 1702 1703 ep0 = &udc->usba_ep[0]; 1704 ep0->ep.desc = &usba_ep0_desc; 1705 ep0->state = WAIT_FOR_SETUP; 1706 usba_ep_writel(ep0, CFG, 1707 (USBA_BF(EPT_SIZE, EP0_EPT_SIZE) 1708 | USBA_BF(EPT_TYPE, USBA_EPT_TYPE_CONTROL) 1709 | USBA_BF(BK_NUMBER, USBA_BK_NUMBER_ONE))); 1710 usba_ep_writel(ep0, CTL_ENB, 1711 USBA_EPT_ENABLE | USBA_RX_SETUP); 1712 usba_writel(udc, INT_ENB, 1713 (usba_readl(udc, INT_ENB) 1714 | USBA_BF(EPT_INT, 1) 1715 | USBA_DET_SUSPEND 1716 | USBA_END_OF_RESUME)); 1717 1718 /* 1719 * Unclear why we hit this irregularly, e.g. in usbtest, 1720 * but it's clearly harmless... 1721 */ 1722 if (!(usba_ep_readl(ep0, CFG) & USBA_EPT_MAPPED)) 1723 dev_dbg(&udc->pdev->dev, 1724 "ODD: EP0 configuration is invalid!\n"); 1725 } 1726 1727 spin_unlock(&udc->lock); 1728 1729 return IRQ_HANDLED; 1730 } 1731 1732 static irqreturn_t usba_vbus_irq(int irq, void *devid) 1733 { 1734 struct usba_udc *udc = devid; 1735 int vbus; 1736 1737 /* debounce */ 1738 udelay(10); 1739 1740 spin_lock(&udc->lock); 1741 1742 /* May happen if Vbus pin toggles during probe() */ 1743 if (!udc->driver) 1744 goto out; 1745 1746 vbus = vbus_is_present(udc); 1747 if (vbus != udc->vbus_prev) { 1748 if (vbus) { 1749 toggle_bias(1); 1750 usba_writel(udc, CTRL, USBA_ENABLE_MASK); 1751 usba_writel(udc, INT_ENB, USBA_END_OF_RESET); 1752 } else { 1753 udc->gadget.speed = USB_SPEED_UNKNOWN; 1754 reset_all_endpoints(udc); 1755 toggle_bias(0); 1756 usba_writel(udc, CTRL, USBA_DISABLE_MASK); 1757 if (udc->driver->disconnect) { 1758 spin_unlock(&udc->lock); 1759 udc->driver->disconnect(&udc->gadget); 1760 spin_lock(&udc->lock); 1761 } 1762 } 1763 udc->vbus_prev = vbus; 1764 } 1765 1766 out: 1767 spin_unlock(&udc->lock); 1768 1769 return IRQ_HANDLED; 1770 } 1771 1772 static int atmel_usba_start(struct usb_gadget *gadget, 1773 struct usb_gadget_driver *driver) 1774 { 1775 int ret; 1776 struct usba_udc *udc = container_of(gadget, struct usba_udc, gadget); 1777 unsigned long flags; 1778 1779 spin_lock_irqsave(&udc->lock, flags); 1780 1781 udc->devstatus = 1 << USB_DEVICE_SELF_POWERED; 1782 udc->driver = driver; 1783 spin_unlock_irqrestore(&udc->lock, flags); 1784 1785 ret = clk_prepare_enable(udc->pclk); 1786 if (ret) 1787 return ret; 1788 ret = clk_prepare_enable(udc->hclk); 1789 if (ret) { 1790 clk_disable_unprepare(udc->pclk); 1791 return ret; 1792 } 1793 1794 DBG(DBG_GADGET, "registered driver `%s'\n", driver->driver.name); 1795 1796 udc->vbus_prev = 0; 1797 if (gpio_is_valid(udc->vbus_pin)) 1798 enable_irq(gpio_to_irq(udc->vbus_pin)); 1799 1800 /* If Vbus is present, enable the controller and wait for reset */ 1801 spin_lock_irqsave(&udc->lock, flags); 1802 if (vbus_is_present(udc) && udc->vbus_prev == 0) { 1803 toggle_bias(1); 1804 usba_writel(udc, CTRL, USBA_ENABLE_MASK); 1805 usba_writel(udc, INT_ENB, USBA_END_OF_RESET); 1806 } 1807 spin_unlock_irqrestore(&udc->lock, flags); 1808 1809 return 0; 1810 } 1811 1812 static int atmel_usba_stop(struct usb_gadget *gadget, 1813 struct usb_gadget_driver *driver) 1814 { 1815 struct usba_udc *udc = container_of(gadget, struct usba_udc, gadget); 1816 unsigned long flags; 1817 1818 if (gpio_is_valid(udc->vbus_pin)) 1819 disable_irq(gpio_to_irq(udc->vbus_pin)); 1820 1821 spin_lock_irqsave(&udc->lock, flags); 1822 udc->gadget.speed = USB_SPEED_UNKNOWN; 1823 reset_all_endpoints(udc); 1824 spin_unlock_irqrestore(&udc->lock, flags); 1825 1826 /* This will also disable the DP pullup */ 1827 toggle_bias(0); 1828 usba_writel(udc, CTRL, USBA_DISABLE_MASK); 1829 1830 clk_disable_unprepare(udc->hclk); 1831 clk_disable_unprepare(udc->pclk); 1832 1833 DBG(DBG_GADGET, "unregistered driver `%s'\n", udc->driver->driver.name); 1834 1835 udc->driver = NULL; 1836 1837 return 0; 1838 } 1839 1840 #ifdef CONFIG_OF 1841 static struct usba_ep * atmel_udc_of_init(struct platform_device *pdev, 1842 struct usba_udc *udc) 1843 { 1844 u32 val; 1845 const char *name; 1846 enum of_gpio_flags flags; 1847 struct device_node *np = pdev->dev.of_node; 1848 struct device_node *pp; 1849 int i, ret; 1850 struct usba_ep *eps, *ep; 1851 1852 udc->num_ep = 0; 1853 1854 udc->vbus_pin = of_get_named_gpio_flags(np, "atmel,vbus-gpio", 0, 1855 &flags); 1856 udc->vbus_pin_inverted = (flags & OF_GPIO_ACTIVE_LOW) ? 1 : 0; 1857 1858 pp = NULL; 1859 while ((pp = of_get_next_child(np, pp))) 1860 udc->num_ep++; 1861 1862 eps = devm_kzalloc(&pdev->dev, sizeof(struct usba_ep) * udc->num_ep, 1863 GFP_KERNEL); 1864 if (!eps) 1865 return ERR_PTR(-ENOMEM); 1866 1867 udc->gadget.ep0 = &eps[0].ep; 1868 1869 INIT_LIST_HEAD(&eps[0].ep.ep_list); 1870 1871 pp = NULL; 1872 i = 0; 1873 while ((pp = of_get_next_child(np, pp))) { 1874 ep = &eps[i]; 1875 1876 ret = of_property_read_u32(pp, "reg", &val); 1877 if (ret) { 1878 dev_err(&pdev->dev, "of_probe: reg error(%d)\n", ret); 1879 goto err; 1880 } 1881 ep->index = val; 1882 1883 ret = of_property_read_u32(pp, "atmel,fifo-size", &val); 1884 if (ret) { 1885 dev_err(&pdev->dev, "of_probe: fifo-size error(%d)\n", ret); 1886 goto err; 1887 } 1888 ep->fifo_size = val; 1889 1890 ret = of_property_read_u32(pp, "atmel,nb-banks", &val); 1891 if (ret) { 1892 dev_err(&pdev->dev, "of_probe: nb-banks error(%d)\n", ret); 1893 goto err; 1894 } 1895 ep->nr_banks = val; 1896 1897 ep->can_dma = of_property_read_bool(pp, "atmel,can-dma"); 1898 ep->can_isoc = of_property_read_bool(pp, "atmel,can-isoc"); 1899 1900 ret = of_property_read_string(pp, "name", &name); 1901 ep->ep.name = name; 1902 1903 ep->ep_regs = udc->regs + USBA_EPT_BASE(i); 1904 ep->dma_regs = udc->regs + USBA_DMA_BASE(i); 1905 ep->fifo = udc->fifo + USBA_FIFO_BASE(i); 1906 ep->ep.ops = &usba_ep_ops; 1907 usb_ep_set_maxpacket_limit(&ep->ep, ep->fifo_size); 1908 ep->udc = udc; 1909 INIT_LIST_HEAD(&ep->queue); 1910 1911 if (i) 1912 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list); 1913 1914 i++; 1915 } 1916 1917 if (i == 0) { 1918 dev_err(&pdev->dev, "of_probe: no endpoint specified\n"); 1919 ret = -EINVAL; 1920 goto err; 1921 } 1922 1923 return eps; 1924 err: 1925 return ERR_PTR(ret); 1926 } 1927 #else 1928 static struct usba_ep * atmel_udc_of_init(struct platform_device *pdev, 1929 struct usba_udc *udc) 1930 { 1931 return ERR_PTR(-ENOSYS); 1932 } 1933 #endif 1934 1935 static struct usba_ep * usba_udc_pdata(struct platform_device *pdev, 1936 struct usba_udc *udc) 1937 { 1938 struct usba_platform_data *pdata = dev_get_platdata(&pdev->dev); 1939 struct usba_ep *eps; 1940 int i; 1941 1942 if (!pdata) 1943 return ERR_PTR(-ENXIO); 1944 1945 eps = devm_kzalloc(&pdev->dev, sizeof(struct usba_ep) * pdata->num_ep, 1946 GFP_KERNEL); 1947 if (!eps) 1948 return ERR_PTR(-ENOMEM); 1949 1950 udc->gadget.ep0 = &eps[0].ep; 1951 1952 udc->vbus_pin = pdata->vbus_pin; 1953 udc->vbus_pin_inverted = pdata->vbus_pin_inverted; 1954 udc->num_ep = pdata->num_ep; 1955 1956 INIT_LIST_HEAD(&eps[0].ep.ep_list); 1957 1958 for (i = 0; i < pdata->num_ep; i++) { 1959 struct usba_ep *ep = &eps[i]; 1960 1961 ep->ep_regs = udc->regs + USBA_EPT_BASE(i); 1962 ep->dma_regs = udc->regs + USBA_DMA_BASE(i); 1963 ep->fifo = udc->fifo + USBA_FIFO_BASE(i); 1964 ep->ep.ops = &usba_ep_ops; 1965 ep->ep.name = pdata->ep[i].name; 1966 ep->fifo_size = pdata->ep[i].fifo_size; 1967 usb_ep_set_maxpacket_limit(&ep->ep, ep->fifo_size); 1968 ep->udc = udc; 1969 INIT_LIST_HEAD(&ep->queue); 1970 ep->nr_banks = pdata->ep[i].nr_banks; 1971 ep->index = pdata->ep[i].index; 1972 ep->can_dma = pdata->ep[i].can_dma; 1973 ep->can_isoc = pdata->ep[i].can_isoc; 1974 1975 if (i) 1976 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list); 1977 } 1978 1979 return eps; 1980 } 1981 1982 static int usba_udc_probe(struct platform_device *pdev) 1983 { 1984 struct resource *regs, *fifo; 1985 struct clk *pclk, *hclk; 1986 struct usba_udc *udc; 1987 int irq, ret, i; 1988 1989 udc = devm_kzalloc(&pdev->dev, sizeof(*udc), GFP_KERNEL); 1990 if (!udc) 1991 return -ENOMEM; 1992 1993 udc->gadget = usba_gadget_template; 1994 INIT_LIST_HEAD(&udc->gadget.ep_list); 1995 1996 regs = platform_get_resource(pdev, IORESOURCE_MEM, CTRL_IOMEM_ID); 1997 fifo = platform_get_resource(pdev, IORESOURCE_MEM, FIFO_IOMEM_ID); 1998 if (!regs || !fifo) 1999 return -ENXIO; 2000 2001 irq = platform_get_irq(pdev, 0); 2002 if (irq < 0) 2003 return irq; 2004 2005 pclk = devm_clk_get(&pdev->dev, "pclk"); 2006 if (IS_ERR(pclk)) 2007 return PTR_ERR(pclk); 2008 hclk = devm_clk_get(&pdev->dev, "hclk"); 2009 if (IS_ERR(hclk)) 2010 return PTR_ERR(hclk); 2011 2012 spin_lock_init(&udc->lock); 2013 udc->pdev = pdev; 2014 udc->pclk = pclk; 2015 udc->hclk = hclk; 2016 udc->vbus_pin = -ENODEV; 2017 2018 ret = -ENOMEM; 2019 udc->regs = devm_ioremap(&pdev->dev, regs->start, resource_size(regs)); 2020 if (!udc->regs) { 2021 dev_err(&pdev->dev, "Unable to map I/O memory, aborting.\n"); 2022 return ret; 2023 } 2024 dev_info(&pdev->dev, "MMIO registers at 0x%08lx mapped at %p\n", 2025 (unsigned long)regs->start, udc->regs); 2026 udc->fifo = devm_ioremap(&pdev->dev, fifo->start, resource_size(fifo)); 2027 if (!udc->fifo) { 2028 dev_err(&pdev->dev, "Unable to map FIFO, aborting.\n"); 2029 return ret; 2030 } 2031 dev_info(&pdev->dev, "FIFO at 0x%08lx mapped at %p\n", 2032 (unsigned long)fifo->start, udc->fifo); 2033 2034 platform_set_drvdata(pdev, udc); 2035 2036 /* Make sure we start from a clean slate */ 2037 ret = clk_prepare_enable(pclk); 2038 if (ret) { 2039 dev_err(&pdev->dev, "Unable to enable pclk, aborting.\n"); 2040 return ret; 2041 } 2042 toggle_bias(0); 2043 usba_writel(udc, CTRL, USBA_DISABLE_MASK); 2044 clk_disable_unprepare(pclk); 2045 2046 if (pdev->dev.of_node) 2047 udc->usba_ep = atmel_udc_of_init(pdev, udc); 2048 else 2049 udc->usba_ep = usba_udc_pdata(pdev, udc); 2050 2051 if (IS_ERR(udc->usba_ep)) 2052 return PTR_ERR(udc->usba_ep); 2053 2054 ret = devm_request_irq(&pdev->dev, irq, usba_udc_irq, 0, 2055 "atmel_usba_udc", udc); 2056 if (ret) { 2057 dev_err(&pdev->dev, "Cannot request irq %d (error %d)\n", 2058 irq, ret); 2059 return ret; 2060 } 2061 udc->irq = irq; 2062 2063 if (gpio_is_valid(udc->vbus_pin)) { 2064 if (!devm_gpio_request(&pdev->dev, udc->vbus_pin, "atmel_usba_udc")) { 2065 ret = devm_request_irq(&pdev->dev, 2066 gpio_to_irq(udc->vbus_pin), 2067 usba_vbus_irq, 0, 2068 "atmel_usba_udc", udc); 2069 if (ret) { 2070 udc->vbus_pin = -ENODEV; 2071 dev_warn(&udc->pdev->dev, 2072 "failed to request vbus irq; " 2073 "assuming always on\n"); 2074 } else { 2075 disable_irq(gpio_to_irq(udc->vbus_pin)); 2076 } 2077 } else { 2078 /* gpio_request fail so use -EINVAL for gpio_is_valid */ 2079 udc->vbus_pin = -EINVAL; 2080 } 2081 } 2082 2083 ret = usb_add_gadget_udc(&pdev->dev, &udc->gadget); 2084 if (ret) 2085 return ret; 2086 2087 usba_init_debugfs(udc); 2088 for (i = 1; i < udc->num_ep; i++) 2089 usba_ep_init_debugfs(udc, &udc->usba_ep[i]); 2090 2091 return 0; 2092 } 2093 2094 static int __exit usba_udc_remove(struct platform_device *pdev) 2095 { 2096 struct usba_udc *udc; 2097 int i; 2098 2099 udc = platform_get_drvdata(pdev); 2100 2101 usb_del_gadget_udc(&udc->gadget); 2102 2103 for (i = 1; i < udc->num_ep; i++) 2104 usba_ep_cleanup_debugfs(&udc->usba_ep[i]); 2105 usba_cleanup_debugfs(udc); 2106 2107 return 0; 2108 } 2109 2110 #if defined(CONFIG_OF) 2111 static const struct of_device_id atmel_udc_dt_ids[] = { 2112 { .compatible = "atmel,at91sam9rl-udc" }, 2113 { /* sentinel */ } 2114 }; 2115 2116 MODULE_DEVICE_TABLE(of, atmel_udc_dt_ids); 2117 #endif 2118 2119 static struct platform_driver udc_driver = { 2120 .remove = __exit_p(usba_udc_remove), 2121 .driver = { 2122 .name = "atmel_usba_udc", 2123 .owner = THIS_MODULE, 2124 .of_match_table = of_match_ptr(atmel_udc_dt_ids), 2125 }, 2126 }; 2127 2128 module_platform_driver_probe(udc_driver, usba_udc_probe); 2129 2130 MODULE_DESCRIPTION("Atmel USBA UDC driver"); 2131 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)"); 2132 MODULE_LICENSE("GPL"); 2133 MODULE_ALIAS("platform:atmel_usba_udc"); 2134