usb: cleanup useless struct usb20otg_pdata_id
[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                 usb_remove_hcd(hcd);
72
73                 /* reset cru and reinitialize EHCI controller */
74                 pldata->soft_reset(pldata, RST_RECNT);
75                 usb_add_hcd(hcd, hcd->irq, IRQF_DISABLED | IRQF_SHARED);
76                 if (pldata->phy_suspend)
77                         pldata->phy_suspend(pldata, USB_PHY_SUSPEND);
78                 /* do not disable EHCI clk, otherwise RK3288
79                  * host1(DWC_OTG) can't work normally.
80                  */
81                 /* pldata->clock_enable(pldata, 0); */
82         } else if (rk_ehci->host_setenable == 1) {
83                 /* pldata->clock_enable(pldata, 1); */
84                 if (pldata->phy_suspend)
85                         pldata->phy_suspend(pldata, USB_PHY_ENABLED);
86                 mdelay(5);
87                 EHCI_PRINT("%s, enable host controller\n", __func__);
88         }
89         rk_ehci->host_enabled = rk_ehci->host_setenable;
90
91 out:
92         return;
93 }
94
95 static void rk_ehci_hcd_connect_detect(unsigned long pdata)
96 {
97         struct rk_ehci_hcd *rk_ehci = (struct rk_ehci_hcd *)pdata;
98         struct ehci_hcd *ehci = rk_ehci->ehci;
99         struct rkehci_platform_data *pldata;
100         uint32_t status;
101         unsigned long flags;
102
103         local_irq_save(flags);
104
105         pldata = rk_ehci->pldata;
106
107         if (pldata->get_status(USB_STATUS_DPDM)) {
108                 /* usb device connected */
109                 rk_ehci->host_setenable = 1;
110         } else {
111                 /* no device, suspend host */
112                 status = readl(&ehci->regs->port_status[0]);
113                 if (!(status & PORT_CONNECT)) {
114                         rk_ehci->host_setenable = 2;
115                 }
116         }
117
118         if ((rk_ehci->host_enabled)
119             && (rk_ehci->host_setenable != rk_ehci->host_enabled)) {
120                 schedule_delayed_work(&rk_ehci->host_enable_work, 1);
121         }
122
123         mod_timer(&rk_ehci->connect_detect_timer, jiffies + (HZ << 1));
124
125         local_irq_restore(flags);
126         return;
127 }
128
129 static ssize_t ehci_power_show(struct device *_dev,
130                                struct device_attribute *attr, char *buf)
131 {
132         return sprintf(buf, "%d\n", rkehci_status);
133 }
134
135 static ssize_t ehci_power_store(struct device *_dev,
136                                 struct device_attribute *attr,
137                                 const char *buf, size_t count)
138 {
139         uint32_t val = simple_strtoul(buf, NULL, 16);
140         struct usb_hcd *hcd = dev_get_drvdata(_dev);
141         struct rkehci_platform_data *pldata = _dev->platform_data;
142         struct rk_ehci_hcd *rk_ehci = (struct rk_ehci_hcd *)hcd_to_ehci(hcd)->priv;
143
144         EHCI_PRINT("%s: %d setting to: %d\n", __func__, rkehci_status, val);
145         if (val == rkehci_status)
146                 goto out;
147
148         rkehci_status = val;
149         switch (val) {
150         case 0: /* power down */
151                 rk_ehci->host_enabled = 0;
152                 msleep(5);
153                 usb_remove_hcd(hcd);
154                 break;
155         case 1: /*  power on */
156                 pldata->soft_reset(pldata, RST_POR);
157                 usb_add_hcd(hcd, hcd->irq, IRQF_DISABLED | IRQF_SHARED);
158                 rk_ehci->host_enabled = 2;
159                 break;
160         default:
161                 break;
162         }
163 out:
164         return count;
165 }
166
167 static DEVICE_ATTR(ehci_power, S_IRUGO | S_IWUSR, ehci_power_show,
168                    ehci_power_store);
169 static ssize_t debug_show(struct device *dev, struct device_attribute *attr,
170                           char *buf)
171 {
172         struct ehci_hcd *ehci = hcd_to_ehci(dev_get_drvdata(dev));
173         volatile uint32_t *addr;
174
175         EHCI_PRINT("******** EHCI Capability Registers **********\n");
176         addr = &ehci->caps->hc_capbase;
177         EHCI_PRINT("HCIVERSION / CAPLENGTH  @0z%p:  0x%08x\n",
178                    addr, readl_relaxed(addr));
179         addr = &ehci->caps->hcs_params;
180         EHCI_PRINT("HCSPARAMS               @0x%p:  0x%08x\n",
181                    addr, readl_relaxed(addr));
182         addr = &ehci->caps->hcc_params;
183         EHCI_PRINT("HCCPARAMS               @0x%p:  0x%08x\n",
184                    addr, readl_relaxed(addr));
185         EHCI_PRINT("********* EHCI Operational Registers *********\n");
186         addr = &ehci->regs->command;
187         EHCI_PRINT("USBCMD                  @0x%p:  0x%08x\n",
188                    addr, readl_relaxed(addr));
189         addr = &ehci->regs->status;
190         EHCI_PRINT("USBSTS                  @0x%p:  0x%08x\n",
191                    addr, readl_relaxed(addr));
192         addr = &ehci->regs->intr_enable;
193         EHCI_PRINT("USBINTR                 @0x%p:  0x%08x\n",
194                    addr, readl_relaxed(addr));
195         addr = &ehci->regs->frame_index;
196         EHCI_PRINT("FRINDEX                 @0x%p:  0x%08x\n",
197                    addr, readl_relaxed(addr));
198         addr = &ehci->regs->segment;
199         EHCI_PRINT("CTRLDSSEGMENT           @0x%p:  0x%08x\n",
200                    addr, readl_relaxed(addr));
201         addr = &ehci->regs->frame_list;
202         EHCI_PRINT("PERIODICLISTBASE        @0x%p:  0x%08x\n",
203                    addr, readl_relaxed(addr));
204         addr = &ehci->regs->async_next;
205         EHCI_PRINT("ASYNCLISTADDR           @0x%p:  0x%08x\n",
206                    addr, readl_relaxed(addr));
207         addr = &ehci->regs->configured_flag;
208         EHCI_PRINT("CONFIGFLAG              @0x%p:  0x%08x\n",
209                    addr, readl_relaxed(addr));
210         addr = &ehci->regs->port_status[0];
211         EHCI_PRINT("PORTSC                  @0x%p:  0x%08x\n",
212                    addr, readl_relaxed(addr));
213         return sprintf(buf, "EHCI Registers Dump\n");
214 }
215
216 static DEVICE_ATTR(debug_ehci, S_IRUGO, debug_show, NULL);
217
218 static int test_sq(struct ehci_hcd *ehci)
219 {
220         u32 portc = readl(&ehci->regs->port_status);
221
222         if ((portc & PORT_PE) && !(portc & PORT_SUSPEND)) {
223                 writel(PORT_TEST_PKT, &ehci->regs->port_status);
224                 EHCI_PRINT("Start packet test\n");
225                 return 0;
226
227         } else
228                 EHCI_PRINT("Invalid connect status PORTC = 0x%08x\n", portc);
229
230         return -1;
231 }
232
233 static ssize_t test_sq_store(struct device *dev,
234                                  struct device_attribute *attr,
235                                  const char *buf, size_t count)
236
237 {
238         struct rkehci_platform_data *pldata = dev->platform_data;
239         struct ehci_hcd *ehci = hcd_to_ehci(dev_get_drvdata(dev));
240
241         if (pldata->phy_status == USB_PHY_SUSPEND) {
242                 EHCI_PRINT("Invalid status : SUSPEND\n");
243                 return -EBUSY;
244         }
245
246         return (test_sq(ehci)) ? -EBUSY : count;
247 }
248
249 static DEVICE_ATTR(test_sq, S_IWUSR, NULL, test_sq_store);
250
251 static struct of_device_id rk_ehci_of_match[] = {
252         {
253          .compatible = "rockchip,rk3288_rk_ehci_host",
254          .data = &rkehci_pdata_rk3288,
255          },
256         {},
257 };
258
259 MODULE_DEVICE_TABLE(of, rk_ehci_of_match);
260
261 static int ehci_rk_probe(struct platform_device *pdev)
262 {
263         struct usb_hcd *hcd;
264         struct ehci_hcd *ehci;
265         struct resource *res;
266         struct device *dev = &pdev->dev;
267         struct rkehci_platform_data *pldata;
268         int ret;
269         struct device_node *node = pdev->dev.of_node;
270         struct rk_ehci_hcd *rk_ehci;
271         const struct of_device_id *match =
272             of_match_device(of_match_ptr(rk_ehci_of_match), &pdev->dev);
273
274         dev_dbg(&pdev->dev, "ehci_rk probe!\n");
275
276         if (match && match->data) {
277                 dev->platform_data = (void *)match->data;
278         } else {
279                 dev_err(dev, "ehci_rk match failed\n");
280                 return -EINVAL;
281         }
282
283         pldata = dev->platform_data;
284         pldata->dev = dev;
285
286         if (!node) {
287                 dev_err(dev, "device node not found\n");
288                 return -EINVAL;
289         }
290
291         if (!pdev->dev.dma_mask)
292                 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
293         if (!pdev->dev.coherent_dma_mask)
294                 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
295
296         device_create_file(dev, &dev_attr_ehci_power);
297         device_create_file(dev, &dev_attr_debug_ehci);
298         device_create_file(dev, &dev_attr_test_sq);
299
300         hcd =
301             usb_create_hcd(&rk_ehci_hc_driver, &pdev->dev,
302                            dev_name(&pdev->dev));
303         if (!hcd) {
304                 dev_err(&pdev->dev, "Unable to create HCD\n");
305                 return -ENOMEM;
306         }
307
308         if (pldata->hw_init)
309                 pldata->hw_init();
310
311         if (pldata->clock_init) {
312                 pldata->clock_init(pldata);
313                 pldata->clock_enable(pldata, 1);
314         }
315
316         if (pldata->phy_suspend)
317                 pldata->phy_suspend(pldata, USB_PHY_ENABLED);
318
319         if (pldata->soft_reset)
320                 pldata->soft_reset(pldata, RST_POR);;
321
322         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
323         if (!res) {
324                 dev_err(&pdev->dev, "Unable to get memory resource\n");
325                 ret = -ENODEV;
326                 goto put_hcd;
327         }
328
329         hcd->rsrc_start = res->start;
330         hcd->rsrc_len = resource_size(res);
331         hcd->regs = devm_ioremap_resource(dev, res);
332         if (!hcd->regs) {
333                 dev_err(&pdev->dev, "ioremap failed\n");
334                 ret = -ENOMEM;
335                 goto put_hcd;
336         }
337
338         hcd->irq = platform_get_irq(pdev, 0);
339         if (hcd->irq < 0) {
340                 dev_err(&pdev->dev, "Unable to get IRQ resource\n");
341                 ret = hcd->irq;
342                 goto put_hcd;
343         }
344
345         ehci = hcd_to_ehci(hcd);
346         ehci->caps = hcd->regs;
347         ehci->regs = hcd->regs + 0x10;
348         EHCI_PRINT("%s %p %p\n", __func__, ehci->caps, ehci->regs);
349
350         ehci->hcs_params = readl(&ehci->caps->hcs_params);
351
352         ret = usb_add_hcd(hcd, hcd->irq, IRQF_DISABLED | IRQF_SHARED);
353         if (ret) {
354                 dev_err(&pdev->dev, "Failed to add USB HCD\n");
355                 goto put_hcd;
356         }
357
358         rk_ehci = (struct rk_ehci_hcd *)hcd_to_ehci(hcd)->priv;
359
360         if (!rk_ehci) {
361                 ret = -ENOMEM;
362                 goto put_hcd;
363         }
364
365         rk_ehci->ehci = ehci;
366         rk_ehci->pldata = pldata;
367         rk_ehci->host_enabled = 2;
368         rk_ehci->host_setenable = 2;
369         rk_ehci->connect_detect_timer.function = rk_ehci_hcd_connect_detect;
370         rk_ehci->connect_detect_timer.data = (unsigned long)(rk_ehci);
371         init_timer(&rk_ehci->connect_detect_timer);
372         mod_timer(&rk_ehci->connect_detect_timer, jiffies + (HZ << 1));
373         INIT_DELAYED_WORK(&rk_ehci->host_enable_work, rk_ehci_hcd_enable);
374
375         if (pldata->phy_suspend) {
376                 if (pldata->phy_status == USB_PHY_ENABLED) {
377                         pldata->phy_suspend(pldata, USB_PHY_SUSPEND);
378                         /* do not disable EHCI clk, otherwise RK3288
379                          * host1(DWC_OTG) can't work normally.
380                          * udelay(3);
381                          * pldata->clock_enable(pldata, 0);
382                          */
383                 }
384         }
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 int __init ehci_rk_init(void)
462 {
463         if (usb_disabled())
464                 return -ENODEV;
465
466         ehci_init_driver(&rk_ehci_hc_driver, &rk_overrides);
467         return platform_driver_register(&rk_ehci_driver);
468 }
469 module_init(ehci_rk_init);
470
471 static void __exit ehci_rk_cleanup(void)
472 {
473         platform_driver_unregister(&rk_ehci_driver);
474 }
475 module_exit(ehci_rk_cleanup);
476 MODULE_AUTHOR("Rockchip");
477 MODULE_LICENSE("GPL v2");
478