1 --- a/drivers/usb/core/hub.c
2 +++ b/drivers/usb/core/hub.c
3 @@ -1259,7 +1259,7 @@ static void hub_quiesce(struct usb_hub *
4 if (type != HUB_SUSPEND) {
5 /* Disconnect all the children */
6 for (i = 0; i < hdev->maxchild; ++i) {
7 - if (hub->ports[i]->child)
8 + if (hub->ports[i] && hub->ports[i]->child)
9 usb_disconnect(&hub->ports[i]->child);
12 --- a/drivers/usb/core/port.c
13 +++ b/drivers/usb/core/port.c
14 @@ -193,6 +193,7 @@ exit:
15 void usb_hub_remove_port_device(struct usb_hub *hub,
18 - device_unregister(&hub->ports[port1 - 1]->dev);
19 + if (hub->ports[port1 - 1])
20 + device_unregister(&hub->ports[port1 - 1]->dev);
23 --- a/drivers/usb/host/Kconfig
24 +++ b/drivers/usb/host/Kconfig
25 @@ -28,7 +28,11 @@ config USB_XHCI_HCD
28 config USB_XHCI_PLATFORM
30 + bool "xHCI platform"
32 +config USB_MT7621_XHCI_PLATFORM
33 + bool "MTK MT7621 xHCI"
34 + depends on USB_XHCI_PLATFORM
36 config USB_XHCI_HCD_DEBUGGING
37 bool "Debugging for the xHCI host controller"
38 --- a/drivers/usb/host/Makefile
39 +++ b/drivers/usb/host/Makefile
40 @@ -13,15 +13,23 @@ fhci-$(CONFIG_FHCI_DEBUG) += fhci-dbg.o
42 xhci-hcd-y := xhci.o xhci-mem.o
43 xhci-hcd-y += xhci-ring.o xhci-hub.o xhci-dbg.o
44 +ifndef CONFIG_USB_MT7621_XHCI_PLATFORM
45 xhci-hcd-$(CONFIG_PCI) += xhci-pci.o
48 +ifdef CONFIG_USB_MT7621_XHCI_PLATFORM
49 +xhci-hcd-y += mtk-phy.o xhci-mtk-scheduler.o xhci-mtk-power.o xhci-mtk.o mtk-phy-7621.o mtk-phy-ahb.o
52 ifneq ($(CONFIG_USB_XHCI_PLATFORM), )
53 - xhci-hcd-y += xhci-plat.o
54 +xhci-hcd-y += xhci-plat.o
57 obj-$(CONFIG_USB_WHCI_HCD) += whci/
59 +ifndef CONFIG_USB_MT7621_XHCI_PLATFORM
60 obj-$(CONFIG_PCI) += pci-quirks.o
63 obj-$(CONFIG_USB_EHCI_HCD) += ehci-hcd.o
64 obj-$(CONFIG_USB_EHCI_PCI) += ehci-pci.o
65 --- a/drivers/usb/host/pci-quirks.h
66 +++ b/drivers/usb/host/pci-quirks.h
68 #ifndef __LINUX_USB_PCI_QUIRKS_H
69 #define __LINUX_USB_PCI_QUIRKS_H
72 +#if defined (CONFIG_PCI) && !defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
73 void uhci_reset_hc(struct pci_dev *pdev, unsigned long base);
74 int uhci_check_and_reset_hc(struct pci_dev *pdev, unsigned long base);
75 #endif /* CONFIG_PCI */
76 --- a/drivers/usb/host/xhci.c
77 +++ b/drivers/usb/host/xhci.c
82 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
83 +#include <asm/uaccess.h>
84 +#include <linux/dma-mapping.h>
85 +#include <linux/platform_device.h>
87 +#include "xhci-mtk-scheduler.h"
88 +#include "xhci-mtk-power.h"
89 +#include "xhci-mtk.h"
92 #define DRIVER_AUTHOR "Sarah Sharp"
93 #define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver"
95 @@ -38,6 +48,18 @@ static int link_quirk;
96 module_param(link_quirk, int, S_IRUGO | S_IWUSR);
97 MODULE_PARM_DESC(link_quirk, "Don't clear the chain bit on a link TRB");
99 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
100 +long xhci_mtk_test_unlock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
101 +static struct file_operations xhci_mtk_test_fops = {
102 + .owner = THIS_MODULE,
103 + .read = xhci_mtk_test_read,
104 + .write = xhci_mtk_test_write,
105 + .unlocked_ioctl = xhci_mtk_test_unlock_ioctl,
106 + .open = xhci_mtk_test_open,
107 + .release = xhci_mtk_test_release,
111 /* TODO: copied from ehci-hcd.c - can this be refactored? */
113 * xhci_handshake - spin reading hc until handshake completes or fails
114 @@ -189,7 +211,7 @@ int xhci_reset(struct xhci_hcd *xhci)
119 +#if defined (CONFIG_PCI) && !defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
120 static int xhci_free_msi(struct xhci_hcd *xhci)
123 @@ -386,6 +408,7 @@ static int xhci_try_enable_msi(struct us
126 hcd->irq = pdev->irq;
131 @@ -427,6 +450,11 @@ static void compliance_mode_recovery(uns
132 xhci_dbg(xhci, "Attempting compliance mode recovery\n");
133 hcd = xhci->shared_hcd;
135 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
137 + xhci_writel(xhci, temp, xhci->usb3_ports[i]);
140 if (hcd->state == HC_STATE_SUSPENDED)
141 usb_hcd_resume_root_hub(hcd);
143 @@ -475,6 +503,9 @@ bool xhci_compliance_mode_recovery_timer
145 const char *dmi_product_name, *dmi_sys_vendor;
147 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
150 dmi_product_name = dmi_get_system_info(DMI_PRODUCT_NAME);
151 dmi_sys_vendor = dmi_get_system_info(DMI_SYS_VENDOR);
152 if (!dmi_product_name || !dmi_sys_vendor)
153 @@ -518,6 +549,10 @@ int xhci_init(struct usb_hcd *hcd)
155 xhci_dbg(xhci, "xHCI doesn't need link TRB QUIRK\n");
158 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
159 + mtk_xhci_scheduler_init();
161 retval = xhci_mem_init(xhci, GFP_KERNEL);
162 xhci_dbg(xhci, "Finished xhci_init\n");
164 @@ -661,7 +696,11 @@ int xhci_run(struct usb_hcd *hcd)
165 xhci_dbg(xhci, "// Set the interrupt modulation register\n");
166 temp = xhci_readl(xhci, &xhci->ir_set->irq_control);
167 temp &= ~ER_IRQ_INTERVAL_MASK;
168 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
173 xhci_writel(xhci, temp, &xhci->ir_set->irq_control);
175 /* Set the HCD state before we enable the irqs */
176 @@ -682,6 +721,9 @@ int xhci_run(struct usb_hcd *hcd)
177 xhci_queue_vendor_command(xhci, 0, 0, 0,
178 TRB_TYPE(TRB_NEC_GET_FW));
180 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
181 + enableXhciAllPortPower(xhci);
183 xhci_dbg(xhci, "Finished xhci_run for USB2 roothub\n");
186 @@ -999,7 +1041,6 @@ int xhci_resume(struct xhci_hcd *xhci, b
188 /* If restore operation fails, re-initialize the HC during resume */
189 if ((temp & STS_SRE) || hibernated) {
191 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
192 !(xhci_all_ports_seen_u0(xhci))) {
193 del_timer_sync(&xhci->comp_mode_recovery_timer);
194 @@ -1583,6 +1624,13 @@ int xhci_drop_endpoint(struct usb_hcd *h
196 u32 new_add_flags, new_drop_flags, new_slot_info;
198 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
200 + struct sch_ep *sch_ep = NULL;
206 ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
208 @@ -1634,6 +1682,40 @@ int xhci_drop_endpoint(struct usb_hcd *h
210 xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
212 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
214 + slot_ctx = xhci_get_slot_ctx(xhci, xhci->devs[udev->slot_id]->out_ctx);
215 + if ((slot_ctx->tt_info & 0xff) > 0) {
221 + if (usb_endpoint_xfer_int(&ep->desc)) {
222 + ep_type = USB_EP_INT;
224 + else if (usb_endpoint_xfer_isoc(&ep->desc)) {
225 + ep_type = USB_EP_ISOC;
227 + else if (usb_endpoint_xfer_bulk(&ep->desc)) {
228 + ep_type = USB_EP_BULK;
231 + ep_type = USB_EP_CONTROL;
233 + sch_ep = mtk_xhci_scheduler_remove_ep(udev->speed, usb_endpoint_dir_in(&ep->desc)
234 + , isTT, ep_type, (mtk_u32 *)ep);
235 + if (sch_ep != NULL) {
239 + xhci_dbg(xhci, "[MTK]Doesn't find ep_sch instance when removing endpoint\n");
242 + mtk_xhci_scheduler_remove_ep(xhci, udev, ep);
246 xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
247 (unsigned int) ep->desc.bEndpointAddress,
249 @@ -1669,6 +1751,18 @@ int xhci_add_endpoint(struct usb_hcd *hc
250 u32 new_add_flags, new_drop_flags, new_slot_info;
251 struct xhci_virt_device *virt_dev;
253 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
254 + struct xhci_ep_ctx *in_ep_ctx;
256 + struct sch_ep *sch_ep;
266 ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
268 @@ -1731,6 +1825,56 @@ int xhci_add_endpoint(struct usb_hcd *hc
272 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
273 + in_ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
275 + slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
276 + if ((slot_ctx->tt_info & 0xff) > 0) {
282 + if (usb_endpoint_xfer_int(&ep->desc)) {
283 + ep_type = USB_EP_INT;
285 + else if (usb_endpoint_xfer_isoc(&ep->desc)) {
286 + ep_type = USB_EP_ISOC;
288 + else if (usb_endpoint_xfer_bulk(&ep->desc)) {
289 + ep_type = USB_EP_BULK;
292 + ep_type = USB_EP_CONTROL;
294 + if (udev->speed == USB_SPEED_FULL || udev->speed == USB_SPEED_HIGH
295 + || udev->speed == USB_SPEED_LOW) {
296 + maxp = ep->desc.wMaxPacketSize & 0x7FF;
297 + burst = ep->desc.wMaxPacketSize >> 11;
300 + else if (udev->speed == USB_SPEED_SUPER) {
301 + maxp = ep->desc.wMaxPacketSize & 0x7FF;
302 + burst = ep->ss_ep_comp.bMaxBurst;
303 + mult = ep->ss_ep_comp.bmAttributes & 0x3;
305 + interval = (1 << ((in_ep_ctx->ep_info >> 16) & 0xff));
306 + sch_ep = kmalloc(sizeof(struct sch_ep), GFP_KERNEL);
307 + if (mtk_xhci_scheduler_add_ep(udev->speed, usb_endpoint_dir_in(&ep->desc),
308 + isTT, ep_type, maxp, interval, burst, mult, (mtk_u32 *)ep
309 + , (mtk_u32 *)in_ep_ctx, sch_ep) != SCH_SUCCESS) {
310 + xhci_err(xhci, "[MTK] not enough bandwidth\n");
315 + if (mtk_xhci_scheduler_add_ep(xhci, udev, ep, in_ep_ctx) != SCH_SUCCESS) {
316 + xhci_err(xhci, "[MTK] not enough bandwidth\n");
322 ctrl_ctx->add_flags |= cpu_to_le32(added_ctxs);
323 new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
325 @@ -2694,7 +2838,7 @@ int xhci_check_bandwidth(struct usb_hcd
326 if (ctrl_ctx->add_flags == cpu_to_le32(SLOT_FLAG) &&
327 ctrl_ctx->drop_flags == 0)
331 xhci_dbg(xhci, "New Input Control Context:\n");
332 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
333 xhci_dbg_ctx(xhci, virt_dev->in_ctx,
334 @@ -4230,10 +4374,14 @@ static u16 xhci_call_host_update_timeout
337 if (state == USB3_LPM_U1) {
338 +#if !defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
339 if (xhci->quirks & XHCI_INTEL_HOST)
341 return xhci_calculate_intel_u1_timeout(udev, desc);
343 +#if !defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
344 if (xhci->quirks & XHCI_INTEL_HOST)
346 return xhci_calculate_intel_u2_timeout(udev, desc);
349 @@ -4659,7 +4807,9 @@ int xhci_gen_setup(struct usb_hcd *hcd,
350 /* Accept arbitrarily long scatter-gather lists */
351 hcd->self.sg_tablesize = ~0;
352 /* XHCI controllers don't stop the ep queue on short packets :| */
353 +#if !defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
354 hcd->self.no_stop_on_short = 1;
357 if (usb_hcd_is_primary_hcd(hcd)) {
358 xhci = kzalloc(sizeof(struct xhci_hcd), GFP_KERNEL);
359 @@ -4728,6 +4878,10 @@ int xhci_gen_setup(struct usb_hcd *hcd,
361 xhci_dbg(xhci, "Reset complete\n");
363 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
367 temp = xhci_readl(xhci, &xhci->cap_regs->hcc_params);
368 if (HCC_64BIT_ADDR(temp)) {
369 xhci_dbg(xhci, "Enabling 64-bit DMA addresses.\n");
370 @@ -4752,8 +4906,21 @@ MODULE_DESCRIPTION(DRIVER_DESC);
371 MODULE_AUTHOR(DRIVER_AUTHOR);
372 MODULE_LICENSE("GPL");
374 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
375 +static struct platform_device xhci_platform_dev = {
376 + .name = "xhci-hcd",
379 + .coherent_dma_mask = 0xffffffff,
384 static int __init xhci_hcd_init(void)
386 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
387 + struct platform_device *pPlatformDev;
391 retval = xhci_register_pci();
392 @@ -4766,6 +4933,33 @@ static int __init xhci_hcd_init(void)
393 printk(KERN_DEBUG "Problem registering platform driver.");
397 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
398 + retval = register_chrdev(XHCI_MTK_TEST_MAJOR, DEVICE_NAME, &xhci_mtk_test_fops);
401 + if (u3phy_ops->u2_slew_rate_calibration) {
402 + u3phy_ops->u2_slew_rate_calibration(u3phy);
403 + u3phy_ops->u2_slew_rate_calibration(u3phy_p1);
406 + printk(KERN_ERR "WARN: PHY doesn't implement u2 slew rate calibration function\n");
408 + u3phy_ops->init(u3phy);
411 + pPlatformDev = &xhci_platform_dev;
412 + memset(pPlatformDev, 0, sizeof(struct platform_device));
413 + pPlatformDev->name = "xhci-hcd";
414 + pPlatformDev->id = -1;
415 + pPlatformDev->dev.coherent_dma_mask = 0xffffffff;
416 + pPlatformDev->dev.dma_mask = &pPlatformDev->dev.coherent_dma_mask;
418 + retval = platform_device_register(&xhci_platform_dev);
420 + xhci_unregister_plat();
424 * Check the compiler generated sizes of structures that must be laid
425 * out in specific ways for hardware access.
426 @@ -4783,6 +4977,7 @@ static int __init xhci_hcd_init(void)
427 BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8);
428 /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
429 BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8);
433 xhci_unregister_pci();
434 --- a/drivers/usb/host/xhci-dbg.c
435 +++ b/drivers/usb/host/xhci-dbg.c
440 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
441 +#include "xhci-mtk.h"
444 #define XHCI_INIT_VALUE 0x0
446 --- a/drivers/usb/host/xhci.h
447 +++ b/drivers/usb/host/xhci.h
449 #include <linux/usb/hcd.h>
451 /* Code sharing between pci-quirks and xhci hcd */
452 -#include "xhci-ext-caps.h"
453 +#include "xhci-ext-caps.h"
454 #include "pci-quirks.h"
456 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
457 +#define XHC_IRQ (22 + 8)
458 +#define XHC_IO_START 0x1E1C0000
459 +#define XHC_IO_LENGTH 0x10000
460 +/* mtk scheduler bitmasks */
461 +#define BPKTS(p) ((p) & 0x3f)
462 +#define BCSCOUNT(p) (((p) & 0x7) << 8)
463 +#define BBM(p) ((p) << 11)
464 +#define BOFFSET(p) ((p) & 0x3fff)
465 +#define BREPEAT(p) (((p) & 0x7fff) << 16)
471 /* xHCI PCI Configuration Registers */
472 #define XHCI_SBRN_OFFSET (0x60)
474 @@ -1536,8 +1551,12 @@ struct xhci_hcd {
475 /* Compliance Mode Recovery Data */
476 struct timer_list comp_mode_recovery_timer;
478 +#ifdef CONFIG_USB_MT7621_XHCI_PLATFORM
479 +#define COMP_MODE_RCVRY_MSECS 5000
481 /* Compliance Mode Timer Triggered every 2 seconds */
482 #define COMP_MODE_RCVRY_MSECS 2000
486 /* convert between an HCD pointer and the corresponding EHCI_HCD */
487 @@ -1703,7 +1722,7 @@ void xhci_urb_free_priv(struct xhci_hcd
488 void xhci_free_command(struct xhci_hcd *xhci,
489 struct xhci_command *command);
492 +#if defined (CONFIG_PCI) && !defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
494 int xhci_register_pci(void);
495 void xhci_unregister_pci(void);
496 --- a/drivers/usb/host/xhci-mem.c
497 +++ b/drivers/usb/host/xhci-mem.c
498 @@ -65,6 +65,9 @@ static struct xhci_segment *xhci_segment
500 static void xhci_segment_free(struct xhci_hcd *xhci, struct xhci_segment *seg)
506 dma_pool_free(xhci->segment_pool, seg->trbs, seg->dma);
508 @@ -1446,9 +1449,17 @@ int xhci_endpoint_init(struct xhci_hcd *
509 max_burst = (usb_endpoint_maxp(&ep->desc)
512 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
513 + if ((max_packet % 4 == 2) && (max_packet % 16 != 14) && (max_burst == 0) && usb_endpoint_dir_in(&ep->desc))
519 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
520 + if ((max_packet % 4 == 2) && (max_packet % 16 != 14) && (max_burst == 0) && usb_endpoint_dir_in(&ep->desc))
526 --- a/drivers/usb/host/xhci-plat.c
527 +++ b/drivers/usb/host/xhci-plat.c
528 @@ -25,6 +25,13 @@ static void xhci_plat_quirks(struct devi
529 * dev struct in order to setup MSI
531 xhci->quirks |= XHCI_PLAT;
532 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
533 + /* MTK host controller gives a spurious successful event after a
534 + * short transfer. Ignore it.
536 + xhci->quirks |= XHCI_SPURIOUS_SUCCESS;
537 + xhci->quirks |= XHCI_LPM_SUPPORT;
541 /* called during probe() after chip reset completes */
542 @@ -96,20 +103,32 @@ static int xhci_plat_probe(struct platfo
544 driver = &xhci_plat_xhci_driver;
546 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
549 irq = platform_get_irq(pdev, 0);
555 +#if !defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
556 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
561 hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
565 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
566 + hcd->rsrc_start = (uint32_t)XHC_IO_START;
567 + hcd->rsrc_len = XHC_IO_LENGTH;
569 hcd->rsrc_start = res->start;
570 hcd->rsrc_len = resource_size(res);
573 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len,
574 driver->description)) {
575 --- a/drivers/usb/host/xhci-ring.c
576 +++ b/drivers/usb/host/xhci-ring.c
577 @@ -236,7 +236,6 @@ static void inc_enq(struct xhci_hcd *xhc
579 if (!chain && !more_trbs_coming)
582 /* If we're not dealing with 0.95 hardware or
583 * isoc rings on AMD 0.96 host,
584 * carry over the chain bit of the previous TRB
585 @@ -273,16 +272,20 @@ static void inc_enq(struct xhci_hcd *xhc
586 static inline int room_on_ring(struct xhci_hcd *xhci, struct xhci_ring *ring,
587 unsigned int num_trbs)
589 +#if !defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
590 int num_trbs_in_deq_seg;
593 if (ring->num_trbs_free < num_trbs)
596 +#if !defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
597 if (ring->type != TYPE_COMMAND && ring->type != TYPE_EVENT) {
598 num_trbs_in_deq_seg = ring->dequeue - ring->deq_seg->trbs;
599 if (ring->num_trbs_free < num_trbs + num_trbs_in_deq_seg)
606 @@ -2910,6 +2913,7 @@ static int prepare_ring(struct xhci_hcd
607 next = ring->enqueue;
609 while (last_trb(xhci, ring, ring->enq_seg, next)) {
610 +#if !defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
611 /* If we're not dealing with 0.95 hardware or isoc rings
612 * on AMD 0.96 host, clear the chain bit.
614 @@ -2919,7 +2923,9 @@ static int prepare_ring(struct xhci_hcd
615 next->link.control &= cpu_to_le32(~TRB_CHAIN);
617 next->link.control |= cpu_to_le32(TRB_CHAIN);
620 + next->link.control &= cpu_to_le32(~TRB_CHAIN);
623 next->link.control ^= cpu_to_le32(TRB_CYCLE);
625 @@ -3049,6 +3055,9 @@ static void giveback_first_trb(struct xh
626 start_trb->field[3] |= cpu_to_le32(start_cycle);
628 start_trb->field[3] &= cpu_to_le32(~TRB_CYCLE);
629 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
632 xhci_ring_ep_doorbell(xhci, slot_id, ep_index, stream_id);
635 @@ -3108,6 +3117,29 @@ static u32 xhci_td_remainder(unsigned in
636 return (remainder >> 10) << 17;
639 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
640 +static u32 mtk_xhci_td_remainder(unsigned int td_transfer_size, unsigned int td_running_total, unsigned int maxp, unsigned trb_buffer_length)
643 + int remainder, td_packet_count, packet_transferred;
645 + //0 for the last TRB
646 + //FIXME: need to workaround if there is ZLP in this TD
647 + if (td_running_total + trb_buffer_length == td_transfer_size)
650 + //FIXME: need to take care of high-bandwidth (MAX_ESIT)
651 + packet_transferred = (td_running_total /*+ trb_buffer_length*/) / maxp;
652 + td_packet_count = DIV_ROUND_UP(td_transfer_size, maxp);
653 + remainder = td_packet_count - packet_transferred;
655 + if (remainder > max)
658 + return remainder << 17;
663 * For xHCI 1.0 host controllers, TD size is the number of max packet sized
664 * packets remaining in the TD (*not* including this TRB).
665 @@ -3245,6 +3277,7 @@ static int queue_bulk_sg_tx(struct xhci_
668 /* Set the TRB length, TD size, and interrupter fields. */
669 +#if !defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
670 if (xhci->hci_version < 0x100) {
671 remainder = xhci_td_remainder(
672 urb->transfer_buffer_length -
673 @@ -3254,6 +3287,13 @@ static int queue_bulk_sg_tx(struct xhci_
674 trb_buff_len, total_packet_count, urb,
679 + remainder = mtk_xhci_td_remainder(urb->transfer_buffer_length,
680 + running_total, urb->ep->desc.wMaxPacketSize, trb_buff_len);
684 length_field = TRB_LEN(trb_buff_len) |
687 @@ -3316,6 +3356,9 @@ int xhci_queue_bulk_tx(struct xhci_hcd *
688 int running_total, trb_buff_len, ret;
689 unsigned int total_packet_count;
691 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
696 return queue_bulk_sg_tx(xhci, mem_flags, urb, slot_id, ep_index);
697 @@ -3341,6 +3384,25 @@ int xhci_queue_bulk_tx(struct xhci_hcd *
698 running_total += TRB_MAX_BUFF_SIZE;
700 /* FIXME: this doesn't deal with URB_ZERO_PACKET - need one more */
701 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
702 + switch(urb->dev->speed){
703 + case USB_SPEED_SUPER:
704 + max_packet = urb->ep->desc.wMaxPacketSize;
706 + case USB_SPEED_HIGH:
707 + case USB_SPEED_FULL:
708 + case USB_SPEED_LOW:
709 + case USB_SPEED_WIRELESS:
710 + case USB_SPEED_UNKNOWN:
712 + max_packet = urb->ep->desc.wMaxPacketSize & 0x7ff;
715 + if((urb->transfer_flags & URB_ZERO_PACKET)
716 + && ((urb->transfer_buffer_length % max_packet) == 0)){
721 ret = prepare_transfer(xhci, xhci->devs[slot_id],
722 ep_index, urb->stream_id,
723 @@ -3400,6 +3462,7 @@ int xhci_queue_bulk_tx(struct xhci_hcd *
726 /* Set the TRB length, TD size, and interrupter fields. */
727 +#if !defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
728 if (xhci->hci_version < 0x100) {
729 remainder = xhci_td_remainder(
730 urb->transfer_buffer_length -
731 @@ -3409,6 +3472,10 @@ int xhci_queue_bulk_tx(struct xhci_hcd *
732 trb_buff_len, total_packet_count, urb,
736 + remainder = mtk_xhci_td_remainder(urb->transfer_buffer_length, running_total, max_packet, trb_buff_len);
739 length_field = TRB_LEN(trb_buff_len) |
742 @@ -3498,7 +3565,11 @@ int xhci_queue_ctrl_tx(struct xhci_hcd *
745 /* xHCI 1.0 6.4.1.2.1: Transfer Type field */
746 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
749 if (xhci->hci_version == 0x100) {
751 if (urb->transfer_buffer_length > 0) {
752 if (setup->bRequestType & USB_DIR_IN)
753 field |= TRB_TX_TYPE(TRB_DATA_IN);
754 @@ -3522,7 +3593,12 @@ int xhci_queue_ctrl_tx(struct xhci_hcd *
755 field = TRB_TYPE(TRB_DATA);
757 length_field = TRB_LEN(urb->transfer_buffer_length) |
758 +#if !defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
759 xhci_td_remainder(urb->transfer_buffer_length) |
761 + //CC: MTK style, no scatter-gather for control transfer
765 if (urb->transfer_buffer_length > 0) {
766 if (setup->bRequestType & USB_DIR_IN)
767 @@ -3533,7 +3609,7 @@ int xhci_queue_ctrl_tx(struct xhci_hcd *
769 field | ep_ring->cycle_state);
773 /* Save the DMA address of the last TRB in the TD */
774 td->last_trb = ep_ring->enqueue;
776 @@ -3645,6 +3721,9 @@ static int xhci_queue_isoc_tx(struct xhc
777 u64 start_addr, addr;
779 bool more_trbs_coming;
780 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
784 ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
786 @@ -3658,6 +3737,21 @@ static int xhci_queue_isoc_tx(struct xhc
787 start_trb = &ep_ring->enqueue->generic;
788 start_cycle = ep_ring->cycle_state;
790 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
791 + switch(urb->dev->speed){
792 + case USB_SPEED_SUPER:
793 + max_packet = urb->ep->desc.wMaxPacketSize;
795 + case USB_SPEED_HIGH:
796 + case USB_SPEED_FULL:
797 + case USB_SPEED_LOW:
798 + case USB_SPEED_WIRELESS:
799 + case USB_SPEED_UNKNOWN:
800 + max_packet = urb->ep->desc.wMaxPacketSize & 0x7ff;
805 urb_priv = urb->hcpriv;
806 /* Queue the first TRB, even if it's zero-length */
807 for (i = 0; i < num_tds; i++) {
808 @@ -3729,9 +3823,13 @@ static int xhci_queue_isoc_tx(struct xhc
810 td->last_trb = ep_ring->enqueue;
812 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
813 + if (!(xhci->quirks & XHCI_AVOID_BEI)) {
815 if (xhci->hci_version == 0x100 &&
819 /* Set BEI bit except for the last td */
822 @@ -3746,6 +3844,7 @@ static int xhci_queue_isoc_tx(struct xhc
823 trb_buff_len = td_remain_len;
825 /* Set the TRB length, TD size, & interrupter fields. */
826 +#if !defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
827 if (xhci->hci_version < 0x100) {
828 remainder = xhci_td_remainder(
829 td_len - running_total);
830 @@ -3755,6 +3854,10 @@ static int xhci_queue_isoc_tx(struct xhc
831 total_packet_count, urb,
832 (trbs_per_td - j - 1));
835 + remainder = mtk_xhci_td_remainder(urb->transfer_buffer_length, running_total, max_packet, trb_buff_len);
838 length_field = TRB_LEN(trb_buff_len) |