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