Merge branch develop-3.10
[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
218         if ((portc & PORT_PE) && !(portc & PORT_SUSPEND)) {
219                 writel(PORT_TEST_PKT, &ehci->regs->port_status);
220                 EHCI_PRINT("Start packet test\n");
221                 return 0;
222
223         } else
224                 EHCI_PRINT("Invalid connect status PORTC = 0x%08x\n", portc);
225
226         return -1;
227 }
228
229 static ssize_t test_sq_store(struct device *dev,
230                                  struct device_attribute *attr,
231                                  const char *buf, size_t count)
232
233 {
234         struct rkehci_platform_data *pldata = dev->platform_data;
235         struct ehci_hcd *ehci = hcd_to_ehci(dev_get_drvdata(dev));
236
237         if (pldata->phy_status == USB_PHY_SUSPEND) {
238                 EHCI_PRINT("Invalid status : SUSPEND\n");
239                 return -EBUSY;
240         }
241
242         return (test_sq(ehci)) ? -EBUSY : count;
243 }
244
245 static DEVICE_ATTR(test_sq, S_IWUSR, NULL, test_sq_store);
246
247 static struct of_device_id rk_ehci_of_match[] = {
248         {
249          .compatible = "rockchip,rk3288_rk_ehci_host",
250          .data = &rkehci_pdata_rk3288,
251          },
252         {
253          .compatible = "rockchip,rk3126_ehci",
254          .data = &usb20ehci_pdata_rk3126,
255          },
256         {
257          .compatible = "rockchip,rk3368_ehci",
258          .data = &usb20ehci_pdata_rk3368,
259          },
260         {},
261 };
262
263 MODULE_DEVICE_TABLE(of, rk_ehci_of_match);
264
265 static int ehci_rk_probe(struct platform_device *pdev)
266 {
267         struct usb_hcd *hcd;
268         struct ehci_hcd *ehci;
269         struct resource *res;
270         struct device *dev = &pdev->dev;
271         struct rkehci_platform_data *pldata;
272         int ret;
273         struct device_node *node = pdev->dev.of_node;
274         struct rk_ehci_hcd *rk_ehci;
275         const struct of_device_id *match =
276             of_match_device(of_match_ptr(rk_ehci_of_match), &pdev->dev);
277
278         dev_dbg(&pdev->dev, "ehci_rk probe!\n");
279
280         if (match && match->data) {
281                 dev->platform_data = (void *)match->data;
282         } else {
283                 dev_err(dev, "ehci_rk match failed\n");
284                 return -EINVAL;
285         }
286
287         pldata = dev->platform_data;
288         pldata->dev = dev;
289
290         if (!node) {
291                 dev_err(dev, "device node not found\n");
292                 return -EINVAL;
293         }
294
295         if (!pdev->dev.dma_mask)
296                 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
297         if (!pdev->dev.coherent_dma_mask)
298                 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
299
300         device_create_file(dev, &dev_attr_ehci_power);
301         device_create_file(dev, &dev_attr_debug_ehci);
302         device_create_file(dev, &dev_attr_test_sq);
303
304         hcd =
305             usb_create_hcd(&rk_ehci_hc_driver, &pdev->dev,
306                            dev_name(&pdev->dev));
307         if (!hcd) {
308                 dev_err(&pdev->dev, "Unable to create HCD\n");
309                 return -ENOMEM;
310         }
311
312         if (pldata->hw_init)
313                 pldata->hw_init();
314
315         if (pldata->clock_init) {
316                 pldata->clock_init(pldata);
317                 pldata->clock_enable(pldata, 1);
318         }
319
320         if (pldata->phy_suspend)
321                 pldata->phy_suspend(pldata, USB_PHY_ENABLED);
322
323         if (pldata->soft_reset)
324                 pldata->soft_reset(pldata, RST_POR);;
325
326         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
327         if (!res) {
328                 dev_err(&pdev->dev, "Unable to get memory resource\n");
329                 ret = -ENODEV;
330                 goto put_hcd;
331         }
332
333         hcd->rsrc_start = res->start;
334         hcd->rsrc_len = resource_size(res);
335         hcd->regs = devm_ioremap_resource(dev, res);
336         if (!hcd->regs) {
337                 dev_err(&pdev->dev, "ioremap failed\n");
338                 ret = -ENOMEM;
339                 goto put_hcd;
340         }
341
342         hcd->irq = platform_get_irq(pdev, 0);
343         if (hcd->irq < 0) {
344                 dev_err(&pdev->dev, "Unable to get IRQ resource\n");
345                 ret = hcd->irq;
346                 goto put_hcd;
347         }
348
349         ehci = hcd_to_ehci(hcd);
350         ehci->caps = hcd->regs;
351         ehci->regs = hcd->regs + 0x10;
352         EHCI_PRINT("%s %p %p\n", __func__, ehci->caps, ehci->regs);
353
354         ehci->hcs_params = readl(&ehci->caps->hcs_params);
355
356         ret = usb_add_hcd(hcd, hcd->irq, IRQF_DISABLED | IRQF_SHARED);
357         if (ret) {
358                 dev_err(&pdev->dev, "Failed to add USB HCD\n");
359                 goto put_hcd;
360         }
361
362         rk_ehci = (struct rk_ehci_hcd *)hcd_to_ehci(hcd)->priv;
363
364         if (!rk_ehci) {
365                 ret = -ENOMEM;
366                 goto put_hcd;
367         }
368
369         rk_ehci->ehci = ehci;
370         rk_ehci->pldata = pldata;
371         rk_ehci->host_enabled = 2;
372         rk_ehci->host_setenable = 2;
373         rk_ehci->connect_detect_timer.function = rk_ehci_hcd_connect_detect;
374         rk_ehci->connect_detect_timer.data = (unsigned long)(rk_ehci);
375         init_timer(&rk_ehci->connect_detect_timer);
376         mod_timer(&rk_ehci->connect_detect_timer, jiffies + (HZ << 1));
377         INIT_DELAYED_WORK(&rk_ehci->host_enable_work, rk_ehci_hcd_enable);
378
379         if (pldata->phy_suspend) {
380                 if (pldata->phy_status == USB_PHY_ENABLED) {
381                         pldata->phy_suspend(pldata, USB_PHY_SUSPEND);
382                         /* do not disable EHCI clk, otherwise RK3288
383                          * host1(DWC_OTG) can't work normally.
384                          * udelay(3);
385                          * pldata->clock_enable(pldata, 0);
386                          */
387                 }
388         }
389
390         EHCI_PRINT("%s ok\n", __func__);
391
392         return 0;
393
394 put_hcd:
395         if (pldata->clock_enable)
396                 pldata->clock_enable(pldata, 0);
397         usb_put_hcd(hcd);
398
399         return ret;
400 }
401
402 static int ehci_rk_remove(struct platform_device *pdev)
403 {
404         struct usb_hcd *hcd = platform_get_drvdata(pdev);
405         struct device *dev = &pdev->dev;
406
407         device_remove_file(dev, &dev_attr_ehci_power);
408         device_remove_file(dev, &dev_attr_debug_ehci);
409         device_remove_file(dev, &dev_attr_test_sq);
410
411         usb_put_hcd(hcd);
412
413         return 0;
414 }
415
416 #ifdef CONFIG_PM
417 static int ehci_rk_pm_suspend(struct device *dev)
418 {
419         struct usb_hcd *hcd = dev_get_drvdata(dev);
420         bool do_wakeup = device_may_wakeup(dev);
421         int ret;
422
423         dev_dbg(dev, "ehci-rockchip PM suspend\n");
424
425         ret = ehci_suspend(hcd, do_wakeup);
426
427         return ret;
428 }
429
430 static int ehci_rk_pm_resume(struct device *dev)
431 {
432         struct usb_hcd *hcd = dev_get_drvdata(dev);
433
434         dev_dbg(dev, "ehci-rockchip PM resume\n");
435         ehci_resume(hcd, false);
436
437         return 0;
438 }
439 #else
440 #define ehci_rk_pm_suspend      NULL
441 #define ehci_rk_pm_resume       NULL
442 #endif
443
444 static const struct dev_pm_ops ehci_rk_dev_pm_ops = {
445         .suspend = ehci_rk_pm_suspend,
446         .resume = ehci_rk_pm_resume,
447 };
448
449 static struct platform_driver rk_ehci_driver = {
450         .probe = ehci_rk_probe,
451         .remove = ehci_rk_remove,
452         .driver = {
453                    .name = "rockchip_ehci_host",
454                    .of_match_table = of_match_ptr(rk_ehci_of_match),
455 #ifdef CONFIG_PM
456                    .pm = &ehci_rk_dev_pm_ops,
457 #endif
458                    },
459 };
460
461 static const struct ehci_driver_overrides rk_overrides __initdata = {
462         .extra_priv_size = sizeof(struct rk_ehci_hcd),
463 };
464
465 static void rk32_ehci_relinquish_port(struct usb_hcd *hcd, int portnum)
466 {
467         struct ehci_hcd         *ehci = hcd_to_ehci(hcd);
468         u32     *status_reg = &ehci->regs->port_status[--portnum];
469         u32     portsc;
470
471         portsc = ehci_readl(ehci, status_reg);
472         portsc &= ~(PORT_OWNER | PORT_RWC_BITS);
473
474         ehci_writel(ehci, portsc, status_reg);
475 }
476
477 static int __init ehci_rk_init(void)
478 {
479         if (usb_disabled())
480                 return -ENODEV;
481
482         ehci_init_driver(&rk_ehci_hc_driver, &rk_overrides);
483
484         /*
485          * Work-around: RK3288 do not support OHCI controller, so our
486          * vendor-spec ehci driver has to prevent handing off this port to
487          * OHCI by standard ehci-hub driver, put PORT_OWNER back to 0 manually.
488          */
489         if (cpu_is_rk3288())
490                 rk_ehci_hc_driver.relinquish_port = rk32_ehci_relinquish_port;
491
492         return platform_driver_register(&rk_ehci_driver);
493 }
494
495 module_init(ehci_rk_init);
496
497 static void __exit ehci_rk_cleanup(void)
498 {
499         platform_driver_unregister(&rk_ehci_driver);
500 }
501 module_exit(ehci_rk_cleanup);
502 MODULE_AUTHOR("Rockchip");
503 MODULE_LICENSE("GPL v2");
504