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