summaryrefslogtreecommitdiff
path: root/target/linux/ramips/patches-3.10/0512-USB-add-xhci-support-for-mt7621.patch
blob: 7cc81a2a6d9e345accf1b47a98ab88c13b3f8951 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -1254,7 +1254,7 @@ static void hub_quiesce(struct usb_hub *
 	if (type != HUB_SUSPEND) {
 		/* Disconnect all the children */
 		for (i = 0; i < hdev->maxchild; ++i) {
-			if (hub->ports[i]->child)
+			if (hub->ports[i] && hub->ports[i]->child)
 				usb_disconnect(&hub->ports[i]->child);
 		}
 	}
--- a/drivers/usb/core/port.c
+++ b/drivers/usb/core/port.c
@@ -193,6 +193,7 @@ exit:
 void usb_hub_remove_port_device(struct usb_hub *hub,
 				       int port1)
 {
-	device_unregister(&hub->ports[port1 - 1]->dev);
+	if (hub->ports[port1 - 1])
+		device_unregister(&hub->ports[port1 - 1]->dev);
 }
 
--- a/drivers/usb/host/Kconfig
+++ b/drivers/usb/host/Kconfig
@@ -28,7 +28,11 @@ config USB_XHCI_HCD
 if USB_XHCI_HCD
 
 config USB_XHCI_PLATFORM
-	tristate
+	bool "xHCI platform"
+
+config USB_MT7621_XHCI_PLATFORM
+	bool "MTK MT7621 xHCI"
+	depends on USB_XHCI_PLATFORM
 
 config USB_XHCI_HCD_DEBUGGING
 	bool "Debugging for the xHCI host controller"
--- a/drivers/usb/host/Makefile
+++ b/drivers/usb/host/Makefile
@@ -13,15 +13,23 @@ fhci-$(CONFIG_FHCI_DEBUG) += fhci-dbg.o
 
 xhci-hcd-y := xhci.o xhci-mem.o
 xhci-hcd-y += xhci-ring.o xhci-hub.o xhci-dbg.o
+ifndef CONFIG_USB_MT7621_XHCI_PLATFORM
 xhci-hcd-$(CONFIG_PCI)	+= xhci-pci.o
+endif
+
+ifdef CONFIG_USB_MT7621_XHCI_PLATFORM
+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
+endif
 
 ifneq ($(CONFIG_USB_XHCI_PLATFORM), )
-	xhci-hcd-y		+= xhci-plat.o
+xhci-hcd-y		+= xhci-plat.o
 endif
 
 obj-$(CONFIG_USB_WHCI_HCD)	+= whci/
 
+ifndef CONFIG_USB_MT7621_XHCI_PLATFORM
 obj-$(CONFIG_PCI)		+= pci-quirks.o
+endif
 
 obj-$(CONFIG_USB_EHCI_HCD)	+= ehci-hcd.o
 obj-$(CONFIG_USB_EHCI_PCI)	+= ehci-pci.o
--- a/drivers/usb/host/pci-quirks.h
+++ b/drivers/usb/host/pci-quirks.h
@@ -1,7 +1,7 @@
 #ifndef __LINUX_USB_PCI_QUIRKS_H
 #define __LINUX_USB_PCI_QUIRKS_H
 
-#ifdef CONFIG_PCI
+#if defined (CONFIG_PCI) && !defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
 void uhci_reset_hc(struct pci_dev *pdev, unsigned long base);
 int uhci_check_and_reset_hc(struct pci_dev *pdev, unsigned long base);
 #endif  /* CONFIG_PCI */
--- a/drivers/usb/host/xhci.c
+++ b/drivers/usb/host/xhci.c
@@ -30,6 +30,16 @@
 
 #include "xhci.h"
 
+#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
+#include <asm/uaccess.h>
+#include <linux/dma-mapping.h>
+#include <linux/platform_device.h>
+#include "mtk-phy.h"
+#include "xhci-mtk-scheduler.h"
+#include "xhci-mtk-power.h"
+#include "xhci-mtk.h"
+#endif
+
 #define DRIVER_AUTHOR "Sarah Sharp"
 #define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver"
 
@@ -38,6 +48,18 @@ static int link_quirk;
 module_param(link_quirk, int, S_IRUGO | S_IWUSR);
 MODULE_PARM_DESC(link_quirk, "Don't clear the chain bit on a link TRB");
 
+#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
+long xhci_mtk_test_unlock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
+static struct file_operations xhci_mtk_test_fops = {
+    .owner =		THIS_MODULE,
+    .read =		xhci_mtk_test_read,
+    .write =		xhci_mtk_test_write,
+    .unlocked_ioctl =	xhci_mtk_test_unlock_ioctl,
+    .open =		xhci_mtk_test_open,
+    .release =		xhci_mtk_test_release,
+};
+#endif
+
 /* TODO: copied from ehci-hcd.c - can this be refactored? */
 /*
  * xhci_handshake - spin reading hc until handshake completes or fails
@@ -189,7 +211,7 @@ int xhci_reset(struct xhci_hcd *xhci)
 	return ret;
 }
 
-#ifdef CONFIG_PCI
+#if defined (CONFIG_PCI) && !defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
 static int xhci_free_msi(struct xhci_hcd *xhci)
 {
 	int i;
@@ -389,6 +411,7 @@ static int xhci_try_enable_msi(struct us
 		return ret;
 	}
 	hcd->irq = pdev->irq;
+
 	return 0;
 }
 
@@ -430,6 +453,11 @@ static void compliance_mode_recovery(uns
 			xhci_dbg(xhci, "Attempting compliance mode recovery\n");
 			hcd = xhci->shared_hcd;
 
+#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
+			temp |= (1 << 31);
+			xhci_writel(xhci, temp, xhci->usb3_ports[i]);
+#endif
+
 			if (hcd->state == HC_STATE_SUSPENDED)
 				usb_hcd_resume_root_hub(hcd);
 
@@ -478,6 +506,9 @@ bool xhci_compliance_mode_recovery_timer
 {
 	const char *dmi_product_name, *dmi_sys_vendor;
 
+#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
+	return true;
+#endif
 	dmi_product_name = dmi_get_system_info(DMI_PRODUCT_NAME);
 	dmi_sys_vendor = dmi_get_system_info(DMI_SYS_VENDOR);
 	if (!dmi_product_name || !dmi_sys_vendor)
@@ -521,6 +552,10 @@ int xhci_init(struct usb_hcd *hcd)
 	} else {
 		xhci_dbg(xhci, "xHCI doesn't need link TRB QUIRK\n");
 	}
+
+#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
+	mtk_xhci_scheduler_init();
+#endif
 	retval = xhci_mem_init(xhci, GFP_KERNEL);
 	xhci_dbg(xhci, "Finished xhci_init\n");
 
@@ -664,7 +699,11 @@ int xhci_run(struct usb_hcd *hcd)
 	xhci_dbg(xhci, "// Set the interrupt modulation register\n");
 	temp = xhci_readl(xhci, &xhci->ir_set->irq_control);
 	temp &= ~ER_IRQ_INTERVAL_MASK;
+#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
+	temp |= (u32) 16;
+#else
 	temp |= (u32) 160;
+#endif
 	xhci_writel(xhci, temp, &xhci->ir_set->irq_control);
 
 	/* Set the HCD state before we enable the irqs */
@@ -685,6 +724,9 @@ int xhci_run(struct usb_hcd *hcd)
 		xhci_queue_vendor_command(xhci, 0, 0, 0,
 				TRB_TYPE(TRB_NEC_GET_FW));
 
+#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
+	enableXhciAllPortPower(xhci);
+#endif
 	xhci_dbg(xhci, "Finished xhci_run for USB2 roothub\n");
 	return 0;
 }
@@ -1002,7 +1044,6 @@ int xhci_resume(struct xhci_hcd *xhci, b
 
 	/* If restore operation fails, re-initialize the HC during resume */
 	if ((temp & STS_SRE) || hibernated) {
-
 		if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
 				!(xhci_all_ports_seen_u0(xhci))) {
 			del_timer_sync(&xhci->comp_mode_recovery_timer);
@@ -1586,6 +1627,13 @@ int xhci_drop_endpoint(struct usb_hcd *h
 	u32 drop_flag;
 	u32 new_add_flags, new_drop_flags, new_slot_info;
 	int ret;
+#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
+#if MTK_SCH_NEW
+	struct sch_ep *sch_ep = NULL;
+	int isTT;
+	int ep_type;
+#endif
+#endif
 
 	ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
 	if (ret <= 0)
@@ -1637,6 +1685,40 @@ int xhci_drop_endpoint(struct usb_hcd *h
 
 	xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
 
+#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
+#if MTK_SCH_NEW
+	slot_ctx = xhci_get_slot_ctx(xhci, xhci->devs[udev->slot_id]->out_ctx);
+	if ((slot_ctx->tt_info & 0xff) > 0) {
+		isTT = 1;
+	}
+	else {
+		isTT = 0;
+	}
+	if (usb_endpoint_xfer_int(&ep->desc)) {
+		ep_type = USB_EP_INT;
+	}
+	else if (usb_endpoint_xfer_isoc(&ep->desc)) {
+		ep_type = USB_EP_ISOC;
+	}
+	else if (usb_endpoint_xfer_bulk(&ep->desc)) {
+		ep_type = USB_EP_BULK;
+	}
+	else
+		ep_type = USB_EP_CONTROL;
+
+	sch_ep = mtk_xhci_scheduler_remove_ep(udev->speed, usb_endpoint_dir_in(&ep->desc)
+		, isTT, ep_type, (mtk_u32 *)ep);
+	if (sch_ep != NULL) {
+		kfree(sch_ep);
+	}
+	else {
+		xhci_dbg(xhci, "[MTK]Doesn't find ep_sch instance when removing endpoint\n");
+	}
+#else
+	mtk_xhci_scheduler_remove_ep(xhci, udev, ep);
+#endif
+#endif
+
 	xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
 			(unsigned int) ep->desc.bEndpointAddress,
 			udev->slot_id,
@@ -1672,6 +1754,18 @@ int xhci_add_endpoint(struct usb_hcd *hc
 	u32 new_add_flags, new_drop_flags, new_slot_info;
 	struct xhci_virt_device *virt_dev;
 	int ret = 0;
+#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
+	struct xhci_ep_ctx *in_ep_ctx;
+#if MTK_SCH_NEW
+	struct sch_ep *sch_ep;
+	int isTT;
+	int ep_type;
+	int maxp = 0;
+	int burst = 0;
+	int mult = 0;
+	int interval;
+#endif
+#endif
 
 	ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
 	if (ret <= 0) {
@@ -1734,6 +1828,56 @@ int xhci_add_endpoint(struct usb_hcd *hc
 		return -ENOMEM;
 	}
 
+#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
+	in_ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
+#if MTK_SCH_NEW
+	slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
+	if ((slot_ctx->tt_info & 0xff) > 0) {
+		isTT = 1;
+	}
+	else {
+		isTT = 0;
+	}
+	if (usb_endpoint_xfer_int(&ep->desc)) {
+		ep_type = USB_EP_INT;
+	}
+	else if (usb_endpoint_xfer_isoc(&ep->desc)) {
+		ep_type = USB_EP_ISOC;
+	}
+	else if (usb_endpoint_xfer_bulk(&ep->desc)) {
+		ep_type = USB_EP_BULK;
+	}
+	else
+		ep_type = USB_EP_CONTROL;
+
+	if (udev->speed == USB_SPEED_FULL || udev->speed == USB_SPEED_HIGH 
+		|| udev->speed == USB_SPEED_LOW) {
+		maxp = ep->desc.wMaxPacketSize & 0x7FF;
+		burst = ep->desc.wMaxPacketSize >> 11;
+		mult = 0;
+	}
+	else if (udev->speed == USB_SPEED_SUPER) {
+		maxp = ep->desc.wMaxPacketSize & 0x7FF;
+		burst = ep->ss_ep_comp.bMaxBurst;
+		mult = ep->ss_ep_comp.bmAttributes & 0x3;
+	}
+	interval = (1 << ((in_ep_ctx->ep_info >> 16) & 0xff));
+	sch_ep = kmalloc(sizeof(struct sch_ep), GFP_KERNEL);
+	if (mtk_xhci_scheduler_add_ep(udev->speed, usb_endpoint_dir_in(&ep->desc),
+		isTT, ep_type, maxp, interval, burst, mult, (mtk_u32 *)ep
+		, (mtk_u32 *)in_ep_ctx, sch_ep) != SCH_SUCCESS) {
+		xhci_err(xhci, "[MTK] not enough bandwidth\n");
+
+		return -ENOSPC;
+	}
+#else
+	if (mtk_xhci_scheduler_add_ep(xhci, udev, ep, in_ep_ctx) != SCH_SUCCESS) {
+		xhci_err(xhci, "[MTK] not enough bandwidth\n");
+
+		return -ENOSPC;
+	}
+#endif
+#endif
 	ctrl_ctx->add_flags |= cpu_to_le32(added_ctxs);
 	new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
 
@@ -2697,7 +2841,7 @@ int xhci_check_bandwidth(struct usb_hcd
 	if (ctrl_ctx->add_flags == cpu_to_le32(SLOT_FLAG) &&
 			ctrl_ctx->drop_flags == 0)
 		return 0;
-
+	
 	xhci_dbg(xhci, "New Input Control Context:\n");
 	slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
 	xhci_dbg_ctx(xhci, virt_dev->in_ctx,
@@ -4233,10 +4377,14 @@ static u16 xhci_call_host_update_timeout
 		u16 *timeout)
 {
 	if (state == USB3_LPM_U1) {
+#if !defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
 		if (xhci->quirks & XHCI_INTEL_HOST)
+#endif
 			return xhci_calculate_intel_u1_timeout(udev, desc);
 	} else {
+#if !defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
 		if (xhci->quirks & XHCI_INTEL_HOST)
+#endif
 			return xhci_calculate_intel_u2_timeout(udev, desc);
 	}
 
@@ -4662,7 +4810,9 @@ int xhci_gen_setup(struct usb_hcd *hcd,
 	/* Accept arbitrarily long scatter-gather lists */
 	hcd->self.sg_tablesize = ~0;
 	/* XHCI controllers don't stop the ep queue on short packets :| */
+#if !defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
 	hcd->self.no_stop_on_short = 1;
+#endif
 
 	if (usb_hcd_is_primary_hcd(hcd)) {
 		xhci = kzalloc(sizeof(struct xhci_hcd), GFP_KERNEL);
@@ -4731,6 +4881,10 @@ int xhci_gen_setup(struct usb_hcd *hcd,
 		goto error;
 	xhci_dbg(xhci, "Reset complete\n");
 
+#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
+	setInitialReg();
+#endif
+
 	temp = xhci_readl(xhci, &xhci->cap_regs->hcc_params);
 	if (HCC_64BIT_ADDR(temp)) {
 		xhci_dbg(xhci, "Enabling 64-bit DMA addresses.\n");
@@ -4755,8 +4909,21 @@ MODULE_DESCRIPTION(DRIVER_DESC);
 MODULE_AUTHOR(DRIVER_AUTHOR);
 MODULE_LICENSE("GPL");
 
+#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
+static struct platform_device xhci_platform_dev = {
+	.name = "xhci-hcd",
+	.id   = -1,
+	.dev  = { 
+		.coherent_dma_mask = 0xffffffff,
+        },
+};
+#endif
+
 static int __init xhci_hcd_init(void)
 {
+#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
+	struct platform_device *pPlatformDev;
+#endif
 	int retval;
 
 	retval = xhci_register_pci();
@@ -4769,6 +4936,33 @@ static int __init xhci_hcd_init(void)
 		printk(KERN_DEBUG "Problem registering platform driver.");
 		goto unreg_pci;
 	}
+
+#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
+	retval = register_chrdev(XHCI_MTK_TEST_MAJOR, DEVICE_NAME, &xhci_mtk_test_fops);
+
+	u3phy_init();
+	if (u3phy_ops->u2_slew_rate_calibration) {
+                u3phy_ops->u2_slew_rate_calibration(u3phy);
+                u3phy_ops->u2_slew_rate_calibration(u3phy_p1);
+        }
+        else{
+                printk(KERN_ERR "WARN: PHY doesn't implement u2 slew rate calibration function\n");
+        }
+        u3phy_ops->init(u3phy);
+	reinitIP();
+
+	pPlatformDev = &xhci_platform_dev;
+	memset(pPlatformDev, 0, sizeof(struct platform_device));
+	pPlatformDev->name = "xhci-hcd";
+	pPlatformDev->id = -1;
+	pPlatformDev->dev.coherent_dma_mask = 0xffffffff;
+	pPlatformDev->dev.dma_mask = &pPlatformDev->dev.coherent_dma_mask;
+
+	retval = platform_device_register(&xhci_platform_dev);
+	if (retval < 0)
+		xhci_unregister_plat();
+#endif
+
 	/*
 	 * Check the compiler generated sizes of structures that must be laid
 	 * out in specific ways for hardware access.
@@ -4786,6 +4980,7 @@ static int __init xhci_hcd_init(void)
 	BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8);
 	/* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
 	BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8);
+
 	return 0;
 unreg_pci:
 	xhci_unregister_pci();
--- a/drivers/usb/host/xhci-dbg.c
+++ b/drivers/usb/host/xhci-dbg.c
@@ -21,6 +21,9 @@
  */
 
 #include "xhci.h"
+#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
+#include "xhci-mtk.h"
+#endif
 
 #define XHCI_INIT_VALUE 0x0
 
--- a/drivers/usb/host/xhci.h
+++ b/drivers/usb/host/xhci.h
@@ -29,9 +29,24 @@
 #include <linux/usb/hcd.h>
 
 /* Code sharing between pci-quirks and xhci hcd */
-#include	"xhci-ext-caps.h"
+#include "xhci-ext-caps.h"
 #include "pci-quirks.h"
 
+#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
+#define XHC_IRQ (22 + 8)
+#define XHC_IO_START 0x1E1C0000
+#define XHC_IO_LENGTH 0x10000
+/* mtk scheduler bitmasks */
+#define BPKTS(p)	((p) & 0x3f)
+#define BCSCOUNT(p)	(((p) & 0x7) << 8)
+#define BBM(p)		((p) << 11)
+#define BOFFSET(p)	((p) & 0x3fff)
+#define BREPEAT(p)	(((p) & 0x7fff) << 16)
+#endif
+
+
+
+
 /* xHCI PCI Configuration Registers */
 #define XHCI_SBRN_OFFSET	(0x60)
 
@@ -1536,8 +1551,12 @@ struct xhci_hcd {
 	/* Compliance Mode Recovery Data */
 	struct timer_list	comp_mode_recovery_timer;
 	u32			port_status_u0;
+#ifdef CONFIG_USB_MT7621_XHCI_PLATFORM
+#define COMP_MODE_RCVRY_MSECS 5000
+#else
 /* Compliance Mode Timer Triggered every 2 seconds */
 #define COMP_MODE_RCVRY_MSECS 2000
+#endif
 };
 
 /* convert between an HCD pointer and the corresponding EHCI_HCD */
@@ -1703,7 +1722,7 @@ void xhci_urb_free_priv(struct xhci_hcd
 void xhci_free_command(struct xhci_hcd *xhci,
 		struct xhci_command *command);
 
-#ifdef CONFIG_PCI
+#if defined (CONFIG_PCI) && !defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
 /* xHCI PCI glue */
 int xhci_register_pci(void);
 void xhci_unregister_pci(void);
--- a/drivers/usb/host/xhci-mem.c
+++ b/drivers/usb/host/xhci-mem.c
@@ -65,6 +65,9 @@ static struct xhci_segment *xhci_segment
 
 static void xhci_segment_free(struct xhci_hcd *xhci, struct xhci_segment *seg)
 {
+	if (!seg)
+		return;
+
 	if (seg->trbs) {
 		dma_pool_free(xhci->segment_pool, seg->trbs, seg->dma);
 		seg->trbs = NULL;
@@ -1446,9 +1449,17 @@ int xhci_endpoint_init(struct xhci_hcd *
 			max_burst = (usb_endpoint_maxp(&ep->desc)
 				     & 0x1800) >> 11;
 		}
+#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
+		if ((max_packet % 4 == 2) && (max_packet % 16 != 14) && (max_burst == 0) && usb_endpoint_dir_in(&ep->desc))
+		max_packet += 2;
+#endif
 		break;
 	case USB_SPEED_FULL:
 	case USB_SPEED_LOW:
+#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
+		if ((max_packet % 4 == 2) && (max_packet % 16 != 14) && (max_burst == 0) && usb_endpoint_dir_in(&ep->desc))
+		max_packet += 2;
+#endif
 		break;
 	default:
 		BUG();
--- a/drivers/usb/host/xhci-plat.c
+++ b/drivers/usb/host/xhci-plat.c
@@ -25,6 +25,13 @@ static void xhci_plat_quirks(struct devi
 	 * dev struct in order to setup MSI
 	 */
 	xhci->quirks |= XHCI_PLAT;
+#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
+	/* MTK host controller gives a spurious successful event after a 
+	 * short transfer. Ignore it.
+	 */
+	xhci->quirks |= XHCI_SPURIOUS_SUCCESS;
+	xhci->quirks |= XHCI_LPM_SUPPORT;
+#endif
 }
 
 /* called during probe() after chip reset completes */
@@ -96,20 +103,32 @@ static int xhci_plat_probe(struct platfo
 
 	driver = &xhci_plat_xhci_driver;
 
+#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
+	irq = XHC_IRQ;
+#else
 	irq = platform_get_irq(pdev, 0);
+#endif
+
 	if (irq < 0)
 		return -ENODEV;
 
+#if !defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 	if (!res)
 		return -ENODEV;
+#endif
 
 	hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
 	if (!hcd)
 		return -ENOMEM;
 
+#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
+	hcd->rsrc_start = (uint32_t)XHC_IO_START;
+	hcd->rsrc_len = XHC_IO_LENGTH;
+#else
 	hcd->rsrc_start = res->start;
 	hcd->rsrc_len = resource_size(res);
+#endif
 
 	if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len,
 				driver->description)) {
--- a/drivers/usb/host/xhci-ring.c
+++ b/drivers/usb/host/xhci-ring.c
@@ -236,7 +236,6 @@ static void inc_enq(struct xhci_hcd *xhc
 			 */
 			if (!chain && !more_trbs_coming)
 				break;
-
 			/* If we're not dealing with 0.95 hardware or
 			 * isoc rings on AMD 0.96 host,
 			 * carry over the chain bit of the previous TRB
@@ -273,16 +272,20 @@ static void inc_enq(struct xhci_hcd *xhc
 static inline int room_on_ring(struct xhci_hcd *xhci, struct xhci_ring *ring,
 		unsigned int num_trbs)
 {
+#if !defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
 	int num_trbs_in_deq_seg;
+#endif
 
 	if (ring->num_trbs_free < num_trbs)
 		return 0;
 
+#if !defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
 	if (ring->type != TYPE_COMMAND && ring->type != TYPE_EVENT) {
 		num_trbs_in_deq_seg = ring->dequeue - ring->deq_seg->trbs;
 		if (ring->num_trbs_free < num_trbs + num_trbs_in_deq_seg)
 			return 0;
 	}
+#endif
 
 	return 1;
 }
@@ -2910,6 +2913,7 @@ static int prepare_ring(struct xhci_hcd
 		next = ring->enqueue;
 
 		while (last_trb(xhci, ring, ring->enq_seg, next)) {
+#if !defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
 			/* If we're not dealing with 0.95 hardware or isoc rings
 			 * on AMD 0.96 host, clear the chain bit.
 			 */
@@ -2919,7 +2923,9 @@ static int prepare_ring(struct xhci_hcd
 				next->link.control &= cpu_to_le32(~TRB_CHAIN);
 			else
 				next->link.control |= cpu_to_le32(TRB_CHAIN);
-
+#else
+			next->link.control &= cpu_to_le32(~TRB_CHAIN);
+#endif
 			wmb();
 			next->link.control ^= cpu_to_le32(TRB_CYCLE);
 
@@ -3049,6 +3055,9 @@ static void giveback_first_trb(struct xh
 		start_trb->field[3] |= cpu_to_le32(start_cycle);
 	else
 		start_trb->field[3] &= cpu_to_le32(~TRB_CYCLE);
+#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
+	wmb();
+#endif
 	xhci_ring_ep_doorbell(xhci, slot_id, ep_index, stream_id);
 }
 
@@ -3108,6 +3117,29 @@ static u32 xhci_td_remainder(unsigned in
 		return (remainder >> 10) << 17;
 }
 
+#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
+static u32 mtk_xhci_td_remainder(unsigned int td_transfer_size, unsigned int td_running_total, unsigned int maxp, unsigned trb_buffer_length)
+{
+	u32 max = 31;
+	int remainder, td_packet_count, packet_transferred;
+	
+	//0 for the last TRB
+	//FIXME: need to workaround if there is ZLP in this TD
+	if (td_running_total + trb_buffer_length == td_transfer_size)
+		return 0;
+	
+	//FIXME: need to take care of high-bandwidth (MAX_ESIT)
+	packet_transferred = (td_running_total /*+ trb_buffer_length*/) / maxp;
+	td_packet_count = DIV_ROUND_UP(td_transfer_size, maxp);
+	remainder = td_packet_count - packet_transferred;
+		
+	if (remainder > max)
+		return max << 17;
+	else
+		return remainder << 17;
+}
+#endif
+
 /*
  * For xHCI 1.0 host controllers, TD size is the number of max packet sized
  * packets remaining in the TD (*not* including this TRB).
@@ -3245,6 +3277,7 @@ static int queue_bulk_sg_tx(struct xhci_
 		}
 
 		/* Set the TRB length, TD size, and interrupter fields. */
+#if !defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
 		if (xhci->hci_version < 0x100) {
 			remainder = xhci_td_remainder(
 					urb->transfer_buffer_length -
@@ -3254,6 +3287,13 @@ static int queue_bulk_sg_tx(struct xhci_
 					trb_buff_len, total_packet_count, urb,
 					num_trbs - 1);
 		}
+#else
+		if (num_trbs > 1)
+			remainder = mtk_xhci_td_remainder(urb->transfer_buffer_length, 
+				running_total, urb->ep->desc.wMaxPacketSize, trb_buff_len);
+#endif
+
+	
 		length_field = TRB_LEN(trb_buff_len) |
 			remainder |
 			TRB_INTR_TARGET(0);
@@ -3316,6 +3356,9 @@ int xhci_queue_bulk_tx(struct xhci_hcd *
 	int running_total, trb_buff_len, ret;
 	unsigned int total_packet_count;
 	u64 addr;
+#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
+	int max_packet;
+#endif
 
 	if (urb->num_sgs)
 		return queue_bulk_sg_tx(xhci, mem_flags, urb, slot_id, ep_index);
@@ -3341,6 +3384,25 @@ int xhci_queue_bulk_tx(struct xhci_hcd *
 		running_total += TRB_MAX_BUFF_SIZE;
 	}
 	/* FIXME: this doesn't deal with URB_ZERO_PACKET - need one more */
+#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
+	switch(urb->dev->speed){
+		case USB_SPEED_SUPER:
+			max_packet = urb->ep->desc.wMaxPacketSize;
+			break;
+		case USB_SPEED_HIGH:
+		case USB_SPEED_FULL:
+		case USB_SPEED_LOW:
+		case USB_SPEED_WIRELESS:
+		case USB_SPEED_UNKNOWN:
+		default:
+			max_packet = urb->ep->desc.wMaxPacketSize & 0x7ff;
+			break;
+	}
+	if((urb->transfer_flags & URB_ZERO_PACKET) 
+		&& ((urb->transfer_buffer_length % max_packet) == 0)){
+		num_trbs++;
+	}
+#endif
 
 	ret = prepare_transfer(xhci, xhci->devs[slot_id],
 			ep_index, urb->stream_id,
@@ -3400,6 +3462,7 @@ int xhci_queue_bulk_tx(struct xhci_hcd *
 			field |= TRB_ISP;
 
 		/* Set the TRB length, TD size, and interrupter fields. */
+#if !defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
 		if (xhci->hci_version < 0x100) {
 			remainder = xhci_td_remainder(
 					urb->transfer_buffer_length -
@@ -3409,6 +3472,10 @@ int xhci_queue_bulk_tx(struct xhci_hcd *
 					trb_buff_len, total_packet_count, urb,
 					num_trbs - 1);
 		}
+#else
+		remainder = mtk_xhci_td_remainder(urb->transfer_buffer_length, running_total, max_packet, trb_buff_len);
+#endif
+
 		length_field = TRB_LEN(trb_buff_len) |
 			remainder |
 			TRB_INTR_TARGET(0);
@@ -3498,7 +3565,11 @@ int xhci_queue_ctrl_tx(struct xhci_hcd *
 		field |= 0x1;
 
 	/* xHCI 1.0 6.4.1.2.1: Transfer Type field */
+#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
+	if (1) {
+#else
 	if (xhci->hci_version == 0x100) {
+#endif
 		if (urb->transfer_buffer_length > 0) {
 			if (setup->bRequestType & USB_DIR_IN)
 				field |= TRB_TX_TYPE(TRB_DATA_IN);
@@ -3522,7 +3593,12 @@ int xhci_queue_ctrl_tx(struct xhci_hcd *
 		field = TRB_TYPE(TRB_DATA);
 
 	length_field = TRB_LEN(urb->transfer_buffer_length) |
+#if !defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
 		xhci_td_remainder(urb->transfer_buffer_length) |
+#else
+		//CC: MTK style, no scatter-gather for control transfer
+		0 |
+#endif
 		TRB_INTR_TARGET(0);
 	if (urb->transfer_buffer_length > 0) {
 		if (setup->bRequestType & USB_DIR_IN)
@@ -3533,7 +3609,7 @@ int xhci_queue_ctrl_tx(struct xhci_hcd *
 				length_field,
 				field | ep_ring->cycle_state);
 	}
-
+	
 	/* Save the DMA address of the last TRB in the TD */
 	td->last_trb = ep_ring->enqueue;
 
@@ -3645,6 +3721,9 @@ static int xhci_queue_isoc_tx(struct xhc
 	u64 start_addr, addr;
 	int i, j;
 	bool more_trbs_coming;
+#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
+	int max_packet;
+#endif
 
 	ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
 
@@ -3658,6 +3737,21 @@ static int xhci_queue_isoc_tx(struct xhc
 	start_trb = &ep_ring->enqueue->generic;
 	start_cycle = ep_ring->cycle_state;
 
+#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
+	switch(urb->dev->speed){
+		case USB_SPEED_SUPER:
+			max_packet = urb->ep->desc.wMaxPacketSize;
+			break;
+		case USB_SPEED_HIGH:
+		case USB_SPEED_FULL:
+		case USB_SPEED_LOW:
+		case USB_SPEED_WIRELESS:
+		case USB_SPEED_UNKNOWN:
+			max_packet = urb->ep->desc.wMaxPacketSize & 0x7ff;
+			break;
+	}
+#endif
+
 	urb_priv = urb->hcpriv;
 	/* Queue the first TRB, even if it's zero-length */
 	for (i = 0; i < num_tds; i++) {
@@ -3729,9 +3823,13 @@ static int xhci_queue_isoc_tx(struct xhc
 			} else {
 				td->last_trb = ep_ring->enqueue;
 				field |= TRB_IOC;
+#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
+				if (!(xhci->quirks & XHCI_AVOID_BEI)) {
+#else
 				if (xhci->hci_version == 0x100 &&
 						!(xhci->quirks &
 							XHCI_AVOID_BEI)) {
+#endif
 					/* Set BEI bit except for the last td */
 					if (i < num_tds - 1)
 						field |= TRB_BEI;
@@ -3746,6 +3844,7 @@ static int xhci_queue_isoc_tx(struct xhc
 				trb_buff_len = td_remain_len;
 
 			/* Set the TRB length, TD size, & interrupter fields. */
+#if !defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
 			if (xhci->hci_version < 0x100) {
 				remainder = xhci_td_remainder(
 						td_len - running_total);
@@ -3755,6 +3854,10 @@ static int xhci_queue_isoc_tx(struct xhc
 						total_packet_count, urb,
 						(trbs_per_td - j - 1));
 			}
+#else
+			remainder = mtk_xhci_td_remainder(urb->transfer_buffer_length, running_total, max_packet, trb_buff_len);
+#endif
+
 			length_field = TRB_LEN(trb_buff_len) |
 				remainder |
 				TRB_INTR_TARGET(0);