usb: ehci: add rockchip relinquishing port quirk support
[firefly-linux-kernel-4.4.55.git] / drivers / usb / host / ehci-rockchip.c
1 /*
2  * EHCI-compliant USB host controller driver for Rockchip SoCs
3  *
4  * Copyright (C) 2010 Google, Inc.
5  * Copyright (C) 2009 - 2013 NVIDIA Corporation
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation; either version 2 of the License, or (at your
10  * option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  *
17  */
18 # include <linux/platform_device.h>
19 # include <linux/clk.h>
20 # include <linux/err.h>
21 # include <linux/device.h>
22 # include <linux/of.h>
23 # include <linux/of_platform.h>
24 # include <linux/usb.h>
25 # include <linux/usb/hcd.h>
26 # include <linux/usb/otg.h>
27
28 # include "../dwc_otg_310/usbdev_rk.h"
29 # include "ehci.h"
30
31 static int rkehci_status = 1;
32 static struct hc_driver rk_ehci_hc_driver;
33
34 struct rk_ehci_hcd {
35         struct ehci_hcd *ehci;
36         uint8_t host_enabled;
37         uint8_t host_setenable;
38         struct rkehci_platform_data *pldata;
39         struct timer_list connect_detect_timer;
40         struct delayed_work host_enable_work;
41 };
42 #define EHCI_PRINT(x...)   printk(KERN_INFO "EHCI: " x)
43
44 static void rk_ehci_hcd_enable(struct work_struct *work)
45 {
46         struct rk_ehci_hcd *rk_ehci;
47         struct usb_hcd *hcd;
48         struct rkehci_platform_data *pldata;
49         struct ehci_hcd *ehci;
50
51         rk_ehci = container_of(work, struct rk_ehci_hcd, host_enable_work.work);
52         pldata = rk_ehci->pldata;
53         ehci = rk_ehci->ehci;
54         hcd = ehci_to_hcd(ehci);
55
56         if (rk_ehci->host_enabled == rk_ehci->host_setenable) {
57                 EHCI_PRINT("%s, enable flag %d\n", __func__,
58                        rk_ehci->host_setenable);
59                 goto out;
60         }
61
62         if (rk_ehci->host_setenable == 2) {
63                 /* enable -> disable */
64                 if (pldata->get_status(USB_STATUS_DPDM)) {
65                         /* usb device connected */
66                         rk_ehci->host_setenable = 1;
67                         goto out;
68                 }
69
70                 EHCI_PRINT("%s, disable host controller\n", __func__);
71
72                 if (pldata->phy_suspend)
73                         pldata->phy_suspend(pldata, USB_PHY_SUSPEND);
74                 /* do not disable EHCI clk, otherwise RK3288
75                  * host1(DWC_OTG) can't work normally.
76                  */
77                 /* pldata->clock_enable(pldata, 0); */
78         } else if (rk_ehci->host_setenable == 1) {
79                 /* pldata->clock_enable(pldata, 1); */
80                 if (pldata->phy_suspend)
81                         pldata->phy_suspend(pldata, USB_PHY_ENABLED);
82                 mdelay(5);
83                 EHCI_PRINT("%s, enable host controller\n", __func__);
84         }
85         rk_ehci->host_enabled = rk_ehci->host_setenable;
86
87 out:
88         return;
89 }
90
91 static void rk_ehci_hcd_connect_detect(unsigned long pdata)
92 {
93         struct rk_ehci_hcd *rk_ehci = (struct rk_ehci_hcd *)pdata;
94         struct ehci_hcd *ehci = rk_ehci->ehci;
95         struct rkehci_platform_data *pldata;
96         uint32_t status;
97         unsigned long flags;
98
99         local_irq_save(flags);
100
101         pldata = rk_ehci->pldata;
102
103         if (pldata->get_status(USB_STATUS_DPDM)) {
104                 /* usb device connected */
105                 rk_ehci->host_setenable = 1;
106         } else {
107                 /* no device, suspend host */
108                 status = readl(&ehci->regs->port_status[0]);
109                 if (!(status & PORT_CONNECT)) {
110                         rk_ehci->host_setenable = 2;
111                 }
112         }
113
114         if ((rk_ehci->host_enabled)
115             && (rk_ehci->host_setenable != rk_ehci->host_enabled)) {
116                 schedule_delayed_work(&rk_ehci->host_enable_work, 1);
117         }
118
119         mod_timer(&rk_ehci->connect_detect_timer, jiffies + (HZ << 1));
120
121         local_irq_restore(flags);
122         return;
123 }
124
125 static ssize_t ehci_power_show(struct device *_dev,
126                                struct device_attribute *attr, char *buf)
127 {
128         return sprintf(buf, "%d\n", rkehci_status);
129 }
130
131 static ssize_t ehci_power_store(struct device *_dev,
132                                 struct device_attribute *attr,
133                                 const char *buf, size_t count)
134 {
135         uint32_t val = simple_strtoul(buf, NULL, 16);
136         struct usb_hcd *hcd = dev_get_drvdata(_dev);
137         struct rkehci_platform_data *pldata = _dev->platform_data;
138         struct rk_ehci_hcd *rk_ehci = (struct rk_ehci_hcd *)hcd_to_ehci(hcd)->priv;
139
140         EHCI_PRINT("%s: %d setting to: %d\n", __func__, rkehci_status, val);
141         if (val == rkehci_status)
142                 goto out;
143
144         rkehci_status = val;
145         switch (val) {
146         case 0: /* power down */
147                 rk_ehci->host_enabled = 0;
148                 msleep(5);
149                 usb_remove_hcd(hcd);
150                 break;
151         case 1: /*  power on */
152                 pldata->soft_reset(pldata, RST_POR);
153                 usb_add_hcd(hcd, hcd->irq, IRQF_DISABLED | IRQF_SHARED);
154                 rk_ehci->host_enabled = 2;
155                 break;
156         default:
157                 break;
158         }
159 out:
160         return count;
161 }
162
163 static DEVICE_ATTR(ehci_power, S_IRUGO | S_IWUSR, ehci_power_show,
164                    ehci_power_store);
165 static ssize_t debug_show(struct device *dev, struct device_attribute *attr,
166                           char *buf)
167 {
168         struct ehci_hcd *ehci = hcd_to_ehci(dev_get_drvdata(dev));
169         volatile uint32_t *addr;
170
171         EHCI_PRINT("******** EHCI Capability Registers **********\n");
172         addr = &ehci->caps->hc_capbase;
173         EHCI_PRINT("HCIVERSION / CAPLENGTH  @0z%p:  0x%08x\n",
174                    addr, readl_relaxed(addr));
175         addr = &ehci->caps->hcs_params;
176         EHCI_PRINT("HCSPARAMS               @0x%p:  0x%08x\n",
177                    addr, readl_relaxed(addr));
178         addr = &ehci->caps->hcc_params;
179         EHCI_PRINT("HCCPARAMS               @0x%p:  0x%08x\n",
180                    addr, readl_relaxed(addr));
181         EHCI_PRINT("********* EHCI Operational Registers *********\n");
182         addr = &ehci->regs->command;
183         EHCI_PRINT("USBCMD                  @0x%p:  0x%08x\n",
184                    addr, readl_relaxed(addr));
185         addr = &ehci->regs->status;
186         EHCI_PRINT("USBSTS                  @0x%p:  0x%08x\n",
187                    addr, readl_relaxed(addr));
188         addr = &ehci->regs->intr_enable;
189         EHCI_PRINT("USBINTR                 @0x%p:  0x%08x\n",
190                    addr, readl_relaxed(addr));
191         addr = &ehci->regs->frame_index;
192         EHCI_PRINT("FRINDEX                 @0x%p:  0x%08x\n",
193                    addr, readl_relaxed(addr));
194         addr = &ehci->regs->segment;
195         EHCI_PRINT("CTRLDSSEGMENT           @0x%p:  0x%08x\n",
196                    addr, readl_relaxed(addr));
197         addr = &ehci->regs->frame_list;
198         EHCI_PRINT("PERIODICLISTBASE        @0x%p:  0x%08x\n",
199                    addr, readl_relaxed(addr));
200         addr = &ehci->regs->async_next;
201         EHCI_PRINT("ASYNCLISTADDR           @0x%p:  0x%08x\n",
202                    addr, readl_relaxed(addr));
203         addr = &ehci->regs->configured_flag;
204         EHCI_PRINT("CONFIGFLAG              @0x%p:  0x%08x\n",
205                    addr, readl_relaxed(addr));
206         addr = &ehci->regs->port_status[0];
207         EHCI_PRINT("PORTSC                  @0x%p:  0x%08x\n",
208                    addr, readl_relaxed(addr));
209         return sprintf(buf, "EHCI Registers Dump\n");
210 }
211
212 static DEVICE_ATTR(debug_ehci, S_IRUGO, debug_show, NULL);
213
214 static int test_sq(struct ehci_hcd *ehci)
215 {
216         u32 portc = readl(&ehci->regs->port_status);
217         struct rk_ehci_hcd *rk_ehci = (struct rk_ehci_hcd *)ehci->priv;
218
219         if ((portc & PORT_PE) && !(portc & PORT_SUSPEND)) {
220                 /* At first del usb connect detect timer */
221                 del_timer_sync(&rk_ehci->connect_detect_timer);
222                 writel(PORT_TEST_PKT, &ehci->regs->port_status);
223                 EHCI_PRINT("Start packet test\n");
224                 return 0;
225
226         } else
227                 EHCI_PRINT("Invalid connect status PORTC = 0x%08x\n", portc);
228
229         return -1;
230 }
231
232 static ssize_t test_sq_store(struct device *dev,
233                                  struct device_attribute *attr,
234                                  const char *buf, size_t count)
235
236 {
237         struct rkehci_platform_data *pldata = dev->platform_data;
238         struct ehci_hcd *ehci = hcd_to_ehci(dev_get_drvdata(dev));
239
240         if (pldata->phy_status == USB_PHY_SUSPEND) {
241                 EHCI_PRINT("Invalid status : SUSPEND\n");
242                 return -EBUSY;
243         }
244
245         return (test_sq(ehci)) ? -EBUSY : count;
246 }
247
248 static DEVICE_ATTR(test_sq, S_IWUSR, NULL, test_sq_store);
249
250 static struct of_device_id rk_ehci_of_match[] = {
251 #ifdef CONFIG_ARM
252         {
253          .compatible = "rockchip,rk3288_rk_ehci_host",
254          .data = &rkehci_pdata_rk3288,
255          },
256         {
257          .compatible = "rockchip,rk3126_ehci",
258          .data = &usb20ehci_pdata_rk3126,
259          },
260 #endif
261 #ifdef CONFIG_ARM64
262         {
263          .compatible = "rockchip,rk3368_ehci",
264          .data = &usb20ehci_pdata_rk3368,
265          },
266 #endif
267         {},
268 };
269
270 MODULE_DEVICE_TABLE(of, rk_ehci_of_match);
271
272 static int ehci_rk_probe(struct platform_device *pdev)
273 {
274         struct usb_hcd *hcd;
275         struct ehci_hcd *ehci;
276         struct resource *res;
277         struct device *dev = &pdev->dev;
278         struct rkehci_platform_data *pldata;
279         int ret;
280         struct device_node *node = pdev->dev.of_node;
281         struct rk_ehci_hcd *rk_ehci;
282         const struct of_device_id *match =
283             of_match_device(of_match_ptr(rk_ehci_of_match), &pdev->dev);
284
285         dev_dbg(&pdev->dev, "ehci_rk probe!\n");
286
287         if (match && match->data) {
288                 dev->platform_data = (void *)match->data;
289         } else {
290                 dev_err(dev, "ehci_rk match failed\n");
291                 return -EINVAL;
292         }
293
294         pldata = dev->platform_data;
295         pldata->dev = dev;
296
297         if (!node) {
298                 dev_err(dev, "device node not found\n");
299                 return -EINVAL;
300         }
301
302         if (!pdev->dev.dma_mask)
303                 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
304         if (!pdev->dev.coherent_dma_mask)
305                 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
306
307         device_create_file(dev, &dev_attr_ehci_power);
308         device_create_file(dev, &dev_attr_debug_ehci);
309         device_create_file(dev, &dev_attr_test_sq);
310
311         hcd =
312             usb_create_hcd(&rk_ehci_hc_driver, &pdev->dev,
313                            dev_name(&pdev->dev));
314         if (!hcd) {
315                 dev_err(&pdev->dev, "Unable to create HCD\n");
316                 return -ENOMEM;
317         }
318
319         if (pldata->hw_init)
320                 pldata->hw_init();
321
322         if (pldata->clock_init) {
323                 pldata->clock_init(pldata);
324                 pldata->clock_enable(pldata, 1);
325         }
326
327         if (pldata->phy_suspend)
328                 pldata->phy_suspend(pldata, USB_PHY_ENABLED);
329
330         if (pldata->soft_reset)
331                 pldata->soft_reset(pldata, RST_POR);;
332
333         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
334         if (!res) {
335                 dev_err(&pdev->dev, "Unable to get memory resource\n");
336                 ret = -ENODEV;
337                 goto put_hcd;
338         }
339
340         hcd->rsrc_start = res->start;
341         hcd->rsrc_len = resource_size(res);
342         hcd->regs = devm_ioremap_resource(dev, res);
343         if (!hcd->regs) {
344                 dev_err(&pdev->dev, "ioremap failed\n");
345                 ret = -ENOMEM;
346                 goto put_hcd;
347         }
348
349         hcd->irq = platform_get_irq(pdev, 0);
350         if (hcd->irq < 0) {
351                 dev_err(&pdev->dev, "Unable to get IRQ resource\n");
352                 ret = hcd->irq;
353                 goto put_hcd;
354         }
355
356         ehci = hcd_to_ehci(hcd);
357         ehci->caps = hcd->regs;
358         ehci->regs = hcd->regs + 0x10;
359         EHCI_PRINT("%s %p %p\n", __func__, ehci->caps, ehci->regs);
360
361         ehci->hcs_params = readl(&ehci->caps->hcs_params);
362
363         ret = usb_add_hcd(hcd, hcd->irq, IRQF_DISABLED | IRQF_SHARED);
364         if (ret) {
365                 dev_err(&pdev->dev, "Failed to add USB HCD\n");
366                 goto put_hcd;
367         }
368
369         rk_ehci = (struct rk_ehci_hcd *)hcd_to_ehci(hcd)->priv;
370
371         if (!rk_ehci) {
372                 ret = -ENOMEM;
373                 goto put_hcd;
374         }
375
376         rk_ehci->ehci = ehci;
377         rk_ehci->pldata = pldata;
378         rk_ehci->host_enabled = -1;
379         rk_ehci->host_setenable = -1;
380         rk_ehci->connect_detect_timer.function = rk_ehci_hcd_connect_detect;
381         rk_ehci->connect_detect_timer.data = (unsigned long)(rk_ehci);
382         init_timer(&rk_ehci->connect_detect_timer);
383         mod_timer(&rk_ehci->connect_detect_timer, jiffies + (HZ << 1));
384         INIT_DELAYED_WORK(&rk_ehci->host_enable_work, rk_ehci_hcd_enable);
385
386         EHCI_PRINT("%s ok\n", __func__);
387
388         return 0;
389
390 put_hcd:
391         if (pldata->clock_enable)
392                 pldata->clock_enable(pldata, 0);
393         usb_put_hcd(hcd);
394
395         return ret;
396 }
397
398 static int ehci_rk_remove(struct platform_device *pdev)
399 {
400         struct usb_hcd *hcd = platform_get_drvdata(pdev);
401         struct device *dev = &pdev->dev;
402
403         device_remove_file(dev, &dev_attr_ehci_power);
404         device_remove_file(dev, &dev_attr_debug_ehci);
405         device_remove_file(dev, &dev_attr_test_sq);
406
407         usb_put_hcd(hcd);
408
409         return 0;
410 }
411
412 #ifdef CONFIG_PM
413 static int ehci_rk_pm_suspend(struct device *dev)
414 {
415         struct usb_hcd *hcd = dev_get_drvdata(dev);
416         bool do_wakeup = device_may_wakeup(dev);
417         int ret;
418
419         dev_dbg(dev, "ehci-rockchip PM suspend\n");
420
421         ret = ehci_suspend(hcd, do_wakeup);
422
423         return ret;
424 }
425
426 static int ehci_rk_pm_resume(struct device *dev)
427 {
428         struct usb_hcd *hcd = dev_get_drvdata(dev);
429
430         dev_dbg(dev, "ehci-rockchip PM resume\n");
431         ehci_resume(hcd, false);
432
433         return 0;
434 }
435 #else
436 #define ehci_rk_pm_suspend      NULL
437 #define ehci_rk_pm_resume       NULL
438 #endif
439
440 static const struct dev_pm_ops ehci_rk_dev_pm_ops = {
441         .suspend = ehci_rk_pm_suspend,
442         .resume = ehci_rk_pm_resume,
443 };
444
445 static struct platform_driver rk_ehci_driver = {
446         .probe = ehci_rk_probe,
447         .remove = ehci_rk_remove,
448         .driver = {
449                    .name = "rockchip_ehci_host",
450                    .of_match_table = of_match_ptr(rk_ehci_of_match),
451 #ifdef CONFIG_PM
452                    .pm = &ehci_rk_dev_pm_ops,
453 #endif
454                    },
455 };
456
457 static const struct ehci_driver_overrides rk_overrides __initdata = {
458         .extra_priv_size = sizeof(struct rk_ehci_hcd),
459 };
460
461 static void rk32_ehci_relinquish_port(struct usb_hcd *hcd, int portnum)
462 {
463         struct ehci_hcd         *ehci = hcd_to_ehci(hcd);
464         u32     *status_reg = &ehci->regs->port_status[--portnum];
465         u32     portsc;
466
467         portsc = ehci_readl(ehci, status_reg);
468         portsc &= ~(PORT_OWNER | PORT_RWC_BITS);
469
470         ehci_writel(ehci, portsc, status_reg);
471 }
472
473 static int __init ehci_rk_init(void)
474 {
475         if (usb_disabled())
476                 return -ENODEV;
477
478         ehci_init_driver(&rk_ehci_hc_driver, &rk_overrides);
479
480         /*
481          * Walk around: RK3288 do not support OHCI controller, so our
482          * vendor-spec ehci driver has to prevent handing off this port to
483          * OHCI by standard ehci-hub driver, put PORT_OWNER back to 0 manually.
484          */
485         if (cpu_is_rk3288())
486                 rk_ehci_hc_driver.relinquish_port = rk32_ehci_relinquish_port;
487
488         return platform_driver_register(&rk_ehci_driver);
489 }
490
491 module_init(ehci_rk_init);
492
493 static void __exit ehci_rk_cleanup(void)
494 {
495         platform_driver_unregister(&rk_ehci_driver);
496 }
497 module_exit(ehci_rk_cleanup);
498 MODULE_AUTHOR("Rockchip");
499 MODULE_LICENSE("GPL v2");
500