acaf1dcc52068510eada583fdd41cac6030422ac
[openwrt.git] / target / linux / ramips / patches-3.10 / 0512-USB-add-xhci-support-for-mt7621.patch
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);
10                 }
11         }
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,
16                                        int port1)
17  {
18 -       device_unregister(&hub->ports[port1 - 1]->dev);
19 +       if (hub->ports[port1 - 1])
20 +               device_unregister(&hub->ports[port1 - 1]->dev);
21  }
22  
23 --- a/drivers/usb/host/Kconfig
24 +++ b/drivers/usb/host/Kconfig
25 @@ -28,7 +28,11 @@ config USB_XHCI_HCD
26  if USB_XHCI_HCD
27  
28  config USB_XHCI_PLATFORM
29 -       tristate
30 +       bool "xHCI platform"
31 +
32 +config USB_MT7621_XHCI_PLATFORM
33 +       bool "MTK MT7621 xHCI"
34 +       depends on USB_XHCI_PLATFORM
35  
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
41  
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
46 +endif
47 +
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
50 +endif
51  
52  ifneq ($(CONFIG_USB_XHCI_PLATFORM), )
53 -       xhci-hcd-y              += xhci-plat.o
54 +xhci-hcd-y             += xhci-plat.o
55  endif
56  
57  obj-$(CONFIG_USB_WHCI_HCD)     += whci/
58  
59 +ifndef CONFIG_USB_MT7621_XHCI_PLATFORM
60  obj-$(CONFIG_PCI)              += pci-quirks.o
61 +endif
62  
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
67 @@ -1,7 +1,7 @@
68  #ifndef __LINUX_USB_PCI_QUIRKS_H
69  #define __LINUX_USB_PCI_QUIRKS_H
70  
71 -#ifdef CONFIG_PCI
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
78 @@ -30,6 +30,16 @@
79  
80  #include "xhci.h"
81  
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>
86 +#include "mtk-phy.h"
87 +#include "xhci-mtk-scheduler.h"
88 +#include "xhci-mtk-power.h"
89 +#include "xhci-mtk.h"
90 +#endif
91 +
92  #define DRIVER_AUTHOR "Sarah Sharp"
93  #define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver"
94  
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");
98  
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,
108 +};
109 +#endif
110 +
111  /* TODO: copied from ehci-hcd.c - can this be refactored? */
112  /*
113   * xhci_handshake - spin reading hc until handshake completes or fails
114 @@ -189,7 +211,7 @@ int xhci_reset(struct xhci_hcd *xhci)
115         return ret;
116  }
117  
118 -#ifdef CONFIG_PCI
119 +#if defined (CONFIG_PCI) && !defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
120  static int xhci_free_msi(struct xhci_hcd *xhci)
121  {
122         int i;
123 @@ -386,6 +408,7 @@ static int xhci_try_enable_msi(struct us
124                 return ret;
125         }
126         hcd->irq = pdev->irq;
127 +
128         return 0;
129  }
130  
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;
134  
135 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
136 +                       temp |= (1 << 31);
137 +                       xhci_writel(xhci, temp, xhci->usb3_ports[i]);
138 +#endif
139 +
140                         if (hcd->state == HC_STATE_SUSPENDED)
141                                 usb_hcd_resume_root_hub(hcd);
142  
143 @@ -475,6 +503,9 @@ bool xhci_compliance_mode_recovery_timer
144  {
145         const char *dmi_product_name, *dmi_sys_vendor;
146  
147 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
148 +       return true;
149 +#endif
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)
154         } else {
155                 xhci_dbg(xhci, "xHCI doesn't need link TRB QUIRK\n");
156         }
157 +
158 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
159 +       mtk_xhci_scheduler_init();
160 +#endif
161         retval = xhci_mem_init(xhci, GFP_KERNEL);
162         xhci_dbg(xhci, "Finished xhci_init\n");
163  
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)
169 +       temp |= (u32) 16;
170 +#else
171         temp |= (u32) 160;
172 +#endif
173         xhci_writel(xhci, temp, &xhci->ir_set->irq_control);
174  
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));
179  
180 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
181 +       enableXhciAllPortPower(xhci);
182 +#endif
183         xhci_dbg(xhci, "Finished xhci_run for USB2 roothub\n");
184         return 0;
185  }
186 @@ -999,7 +1041,6 @@ int xhci_resume(struct xhci_hcd *xhci, b
187  
188         /* If restore operation fails, re-initialize the HC during resume */
189         if ((temp & STS_SRE) || hibernated) {
190 -
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
195         u32 drop_flag;
196         u32 new_add_flags, new_drop_flags, new_slot_info;
197         int ret;
198 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
199 +#if MTK_SCH_NEW
200 +       struct sch_ep *sch_ep = NULL;
201 +       int isTT;
202 +       int ep_type;
203 +#endif
204 +#endif
205  
206         ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
207         if (ret <= 0)
208 @@ -1634,6 +1682,40 @@ int xhci_drop_endpoint(struct usb_hcd *h
209  
210         xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
211  
212 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
213 +#if MTK_SCH_NEW
214 +       slot_ctx = xhci_get_slot_ctx(xhci, xhci->devs[udev->slot_id]->out_ctx);
215 +       if ((slot_ctx->tt_info & 0xff) > 0) {
216 +               isTT = 1;
217 +       }
218 +       else {
219 +               isTT = 0;
220 +       }
221 +       if (usb_endpoint_xfer_int(&ep->desc)) {
222 +               ep_type = USB_EP_INT;
223 +       }
224 +       else if (usb_endpoint_xfer_isoc(&ep->desc)) {
225 +               ep_type = USB_EP_ISOC;
226 +       }
227 +       else if (usb_endpoint_xfer_bulk(&ep->desc)) {
228 +               ep_type = USB_EP_BULK;
229 +       }
230 +       else
231 +               ep_type = USB_EP_CONTROL;
232 +
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) {
236 +               kfree(sch_ep);
237 +       }
238 +       else {
239 +               xhci_dbg(xhci, "[MTK]Doesn't find ep_sch instance when removing endpoint\n");
240 +       }
241 +#else
242 +       mtk_xhci_scheduler_remove_ep(xhci, udev, ep);
243 +#endif
244 +#endif
245 +
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,
248                         udev->slot_id,
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;
252         int ret = 0;
253 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
254 +       struct xhci_ep_ctx *in_ep_ctx;
255 +#if MTK_SCH_NEW
256 +       struct sch_ep *sch_ep;
257 +       int isTT;
258 +       int ep_type;
259 +       int maxp = 0;
260 +       int burst = 0;
261 +       int mult = 0;
262 +       int interval;
263 +#endif
264 +#endif
265  
266         ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
267         if (ret <= 0) {
268 @@ -1731,6 +1825,56 @@ int xhci_add_endpoint(struct usb_hcd *hc
269                 return -ENOMEM;
270         }
271  
272 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
273 +       in_ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
274 +#if MTK_SCH_NEW
275 +       slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
276 +       if ((slot_ctx->tt_info & 0xff) > 0) {
277 +               isTT = 1;
278 +       }
279 +       else {
280 +               isTT = 0;
281 +       }
282 +       if (usb_endpoint_xfer_int(&ep->desc)) {
283 +               ep_type = USB_EP_INT;
284 +       }
285 +       else if (usb_endpoint_xfer_isoc(&ep->desc)) {
286 +               ep_type = USB_EP_ISOC;
287 +       }
288 +       else if (usb_endpoint_xfer_bulk(&ep->desc)) {
289 +               ep_type = USB_EP_BULK;
290 +       }
291 +       else
292 +               ep_type = USB_EP_CONTROL;
293 +
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;
298 +               mult = 0;
299 +       }
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;
304 +       }
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");
311 +
312 +               return -ENOSPC;
313 +       }
314 +#else
315 +       if (mtk_xhci_scheduler_add_ep(xhci, udev, ep, in_ep_ctx) != SCH_SUCCESS) {
316 +               xhci_err(xhci, "[MTK] not enough bandwidth\n");
317 +
318 +               return -ENOSPC;
319 +       }
320 +#endif
321 +#endif
322         ctrl_ctx->add_flags |= cpu_to_le32(added_ctxs);
323         new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
324  
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)
328                 return 0;
329 -
330 +       
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
335                 u16 *timeout)
336  {
337         if (state == USB3_LPM_U1) {
338 +#if !defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
339                 if (xhci->quirks & XHCI_INTEL_HOST)
340 +#endif
341                         return xhci_calculate_intel_u1_timeout(udev, desc);
342         } else {
343 +#if !defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
344                 if (xhci->quirks & XHCI_INTEL_HOST)
345 +#endif
346                         return xhci_calculate_intel_u2_timeout(udev, desc);
347         }
348  
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;
355 +#endif
356  
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,
360                 goto error;
361         xhci_dbg(xhci, "Reset complete\n");
362  
363 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
364 +       setInitialReg();
365 +#endif
366 +
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");
373  
374 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
375 +static struct platform_device xhci_platform_dev = {
376 +       .name = "xhci-hcd",
377 +       .id   = -1,
378 +       .dev  = { 
379 +               .coherent_dma_mask = 0xffffffff,
380 +        },
381 +};
382 +#endif
383 +
384  static int __init xhci_hcd_init(void)
385  {
386 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
387 +       struct platform_device *pPlatformDev;
388 +#endif
389         int retval;
390  
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.");
394                 goto unreg_pci;
395         }
396 +
397 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
398 +       retval = register_chrdev(XHCI_MTK_TEST_MAJOR, DEVICE_NAME, &xhci_mtk_test_fops);
399 +
400 +       u3phy_init();
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);
404 +        }
405 +        else{
406 +                printk(KERN_ERR "WARN: PHY doesn't implement u2 slew rate calibration function\n");
407 +        }
408 +        u3phy_ops->init(u3phy);
409 +       reinitIP();
410 +
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;
417 +
418 +       retval = platform_device_register(&xhci_platform_dev);
419 +       if (retval < 0)
420 +               xhci_unregister_plat();
421 +#endif
422 +
423         /*
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);
430 +
431         return 0;
432  unreg_pci:
433         xhci_unregister_pci();
434 --- a/drivers/usb/host/xhci-dbg.c
435 +++ b/drivers/usb/host/xhci-dbg.c
436 @@ -21,6 +21,9 @@
437   */
438  
439  #include "xhci.h"
440 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
441 +#include "xhci-mtk.h"
442 +#endif
443  
444  #define XHCI_INIT_VALUE 0x0
445  
446 --- a/drivers/usb/host/xhci.h
447 +++ b/drivers/usb/host/xhci.h
448 @@ -29,9 +29,24 @@
449  #include <linux/usb/hcd.h>
450  
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"
455  
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)
466 +#endif
467 +
468 +
469 +
470 +
471  /* xHCI PCI Configuration Registers */
472  #define XHCI_SBRN_OFFSET       (0x60)
473  
474 @@ -1536,8 +1551,12 @@ struct xhci_hcd {
475         /* Compliance Mode Recovery Data */
476         struct timer_list       comp_mode_recovery_timer;
477         u32                     port_status_u0;
478 +#ifdef CONFIG_USB_MT7621_XHCI_PLATFORM
479 +#define COMP_MODE_RCVRY_MSECS 5000
480 +#else
481  /* Compliance Mode Timer Triggered every 2 seconds */
482  #define COMP_MODE_RCVRY_MSECS 2000
483 +#endif
484  };
485  
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);
490  
491 -#ifdef CONFIG_PCI
492 +#if defined (CONFIG_PCI) && !defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
493  /* xHCI PCI glue */
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
499  
500  static void xhci_segment_free(struct xhci_hcd *xhci, struct xhci_segment *seg)
501  {
502 +       if (!seg)
503 +               return;
504 +
505         if (seg->trbs) {
506                 dma_pool_free(xhci->segment_pool, seg->trbs, seg->dma);
507                 seg->trbs = NULL;
508 @@ -1446,9 +1449,17 @@ int xhci_endpoint_init(struct xhci_hcd *
509                         max_burst = (usb_endpoint_maxp(&ep->desc)
510                                      & 0x1800) >> 11;
511                 }
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))
514 +               max_packet += 2;
515 +#endif
516                 break;
517         case USB_SPEED_FULL:
518         case USB_SPEED_LOW:
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))
521 +               max_packet += 2;
522 +#endif
523                 break;
524         default:
525                 BUG();
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
530          */
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.
535 +        */
536 +       xhci->quirks |= XHCI_SPURIOUS_SUCCESS;
537 +       xhci->quirks |= XHCI_LPM_SUPPORT;
538 +#endif
539  }
540  
541  /* called during probe() after chip reset completes */
542 @@ -96,20 +103,32 @@ static int xhci_plat_probe(struct platfo
543  
544         driver = &xhci_plat_xhci_driver;
545  
546 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
547 +       irq = XHC_IRQ;
548 +#else
549         irq = platform_get_irq(pdev, 0);
550 +#endif
551 +
552         if (irq < 0)
553                 return -ENODEV;
554  
555 +#if !defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
556         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
557         if (!res)
558                 return -ENODEV;
559 +#endif
560  
561         hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
562         if (!hcd)
563                 return -ENOMEM;
564  
565 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
566 +       hcd->rsrc_start = (uint32_t)XHC_IO_START;
567 +       hcd->rsrc_len = XHC_IO_LENGTH;
568 +#else
569         hcd->rsrc_start = res->start;
570         hcd->rsrc_len = resource_size(res);
571 +#endif
572  
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
578                          */
579                         if (!chain && !more_trbs_coming)
580                                 break;
581 -
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)
588  {
589 +#if !defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
590         int num_trbs_in_deq_seg;
591 +#endif
592  
593         if (ring->num_trbs_free < num_trbs)
594                 return 0;
595  
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)
600                         return 0;
601         }
602 +#endif
603  
604         return 1;
605  }
606 @@ -2910,6 +2913,7 @@ static int prepare_ring(struct xhci_hcd
607                 next = ring->enqueue;
608  
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.
613                          */
614 @@ -2919,7 +2923,9 @@ static int prepare_ring(struct xhci_hcd
615                                 next->link.control &= cpu_to_le32(~TRB_CHAIN);
616                         else
617                                 next->link.control |= cpu_to_le32(TRB_CHAIN);
618 -
619 +#else
620 +                       next->link.control &= cpu_to_le32(~TRB_CHAIN);
621 +#endif
622                         wmb();
623                         next->link.control ^= cpu_to_le32(TRB_CYCLE);
624  
625 @@ -3049,6 +3055,9 @@ static void giveback_first_trb(struct xh
626                 start_trb->field[3] |= cpu_to_le32(start_cycle);
627         else
628                 start_trb->field[3] &= cpu_to_le32(~TRB_CYCLE);
629 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
630 +       wmb();
631 +#endif
632         xhci_ring_ep_doorbell(xhci, slot_id, ep_index, stream_id);
633  }
634  
635 @@ -3108,6 +3117,29 @@ static u32 xhci_td_remainder(unsigned in
636                 return (remainder >> 10) << 17;
637  }
638  
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)
641 +{
642 +       u32 max = 31;
643 +       int remainder, td_packet_count, packet_transferred;
644 +       
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)
648 +               return 0;
649 +       
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;
654 +               
655 +       if (remainder > max)
656 +               return max << 17;
657 +       else
658 +               return remainder << 17;
659 +}
660 +#endif
661 +
662  /*
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_
666                 }
667  
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,
675                                         num_trbs - 1);
676                 }
677 +#else
678 +               if (num_trbs > 1)
679 +                       remainder = mtk_xhci_td_remainder(urb->transfer_buffer_length, 
680 +                               running_total, urb->ep->desc.wMaxPacketSize, trb_buff_len);
681 +#endif
682 +
683 +       
684                 length_field = TRB_LEN(trb_buff_len) |
685                         remainder |
686                         TRB_INTR_TARGET(0);
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;
690         u64 addr;
691 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
692 +       int max_packet;
693 +#endif
694  
695         if (urb->num_sgs)
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;
699         }
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;
705 +                       break;
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:
711 +               default:
712 +                       max_packet = urb->ep->desc.wMaxPacketSize & 0x7ff;
713 +                       break;
714 +       }
715 +       if((urb->transfer_flags & URB_ZERO_PACKET) 
716 +               && ((urb->transfer_buffer_length % max_packet) == 0)){
717 +               num_trbs++;
718 +       }
719 +#endif
720  
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 *
724                         field |= TRB_ISP;
725  
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,
733                                         num_trbs - 1);
734                 }
735 +#else
736 +               remainder = mtk_xhci_td_remainder(urb->transfer_buffer_length, running_total, max_packet, trb_buff_len);
737 +#endif
738 +
739                 length_field = TRB_LEN(trb_buff_len) |
740                         remainder |
741                         TRB_INTR_TARGET(0);
742 @@ -3498,7 +3565,11 @@ int xhci_queue_ctrl_tx(struct xhci_hcd *
743                 field |= 0x1;
744  
745         /* xHCI 1.0 6.4.1.2.1: Transfer Type field */
746 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
747 +       if (1) {
748 +#else
749         if (xhci->hci_version == 0x100) {
750 +#endif
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);
756  
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) |
760 +#else
761 +               //CC: MTK style, no scatter-gather for control transfer
762 +               0 |
763 +#endif
764                 TRB_INTR_TARGET(0);
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 *
768                                 length_field,
769                                 field | ep_ring->cycle_state);
770         }
771 -
772 +       
773         /* Save the DMA address of the last TRB in the TD */
774         td->last_trb = ep_ring->enqueue;
775  
776 @@ -3645,6 +3721,9 @@ static int xhci_queue_isoc_tx(struct xhc
777         u64 start_addr, addr;
778         int i, j;
779         bool more_trbs_coming;
780 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
781 +       int max_packet;
782 +#endif
783  
784         ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
785  
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;
789  
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;
794 +                       break;
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;
801 +                       break;
802 +       }
803 +#endif
804 +
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
809                         } else {
810                                 td->last_trb = ep_ring->enqueue;
811                                 field |= TRB_IOC;
812 +#if defined (CONFIG_USB_MT7621_XHCI_PLATFORM)
813 +                               if (!(xhci->quirks & XHCI_AVOID_BEI)) {
814 +#else
815                                 if (xhci->hci_version == 0x100 &&
816                                                 !(xhci->quirks &
817                                                         XHCI_AVOID_BEI)) {
818 +#endif
819                                         /* Set BEI bit except for the last td */
820                                         if (i < num_tds - 1)
821                                                 field |= TRB_BEI;
822 @@ -3746,6 +3844,7 @@ static int xhci_queue_isoc_tx(struct xhc
823                                 trb_buff_len = td_remain_len;
824  
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));
833                         }
834 +#else
835 +                       remainder = mtk_xhci_td_remainder(urb->transfer_buffer_length, running_total, max_packet, trb_buff_len);
836 +#endif
837 +
838                         length_field = TRB_LEN(trb_buff_len) |
839                                 remainder |
840                                 TRB_INTR_TARGET(0);