USB: fix EHCI pm_suspend and pm_resume.
[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 "ehci.h"
25 #ifdef CONFIG_DWC_OTG_274
26 # include "../dwc_otg/usbdev_rk.h"
27 #endif
28 #ifdef CONFIG_DWC_OTG_310
29 # include "../dwc_otg_310/usbdev_rk.h"
30 #endif
31
32 static int rkehci_status = 1;
33 static struct ehci_hcd *g_ehci;
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 ehci_port_power (struct ehci_hcd *ehci, int is_on)
57 {
58         unsigned port;
59
60         if (!HCS_PPC (ehci->hcs_params))
61                 return;
62
63         ehci_dbg (ehci, "...power%s ports...\n", is_on ? "up" : "down");
64         for (port = HCS_N_PORTS (ehci->hcs_params); port > 0; )
65                 (void) ehci_hub_control(ehci_to_hcd(ehci),
66                                 is_on ? SetPortFeature : ClearPortFeature,
67                                 USB_PORT_FEAT_POWER,
68                                 port--, NULL, 0);
69         /* Flush those writes */
70         ehci_readl(ehci, &ehci->regs->command);
71         msleep(20);
72 }
73
74 static void rk_ehci_hcd_enable(struct work_struct *work)
75 {
76         struct rk_ehci_hcd *rk_ehci;
77         struct usb_hcd *hcd;
78         struct rkehci_platform_data *pldata;
79         struct ehci_hcd *ehci;
80
81         rk_ehci = container_of(work, struct rk_ehci_hcd, host_enable_work.work);
82         pldata = rk_ehci->pldata;
83         ehci = rk_ehci->ehci;
84         hcd = ehci_to_hcd(ehci);
85
86         if(rk_ehci->host_enabled == rk_ehci->host_setenable){
87                 printk("%s, enable flag %d\n", __func__, rk_ehci->host_setenable);
88                 goto out;
89         }
90
91         if(rk_ehci->host_setenable == 2){// enable -> disable
92                 if(pldata->get_status(USB_STATUS_DPDM)){// usb device connected
93                         rk_ehci->host_setenable = 1;
94                         goto out;
95                 }
96
97                 printk("%s, disable host controller\n", __func__);
98                 ehci_port_power(ehci, 0);
99                 usb_remove_hcd(hcd);
100
101                 /* reset cru and reinitialize EHCI controller */
102                 pldata->soft_reset();
103                 usb_add_hcd(hcd, hcd->irq, IRQF_DISABLED | IRQF_SHARED);
104                 if(pldata->phy_suspend)
105                         pldata->phy_suspend(pldata, USB_PHY_SUSPEND);
106                 /* do not disable EHCI clk, otherwise RK3288
107                  * host1(DWC_OTG) can't work normally.
108                  */
109                 //pldata->clock_enable(pldata, 0);
110         }else if(rk_ehci->host_setenable == 1){
111                 //pldata->clock_enable(pldata, 1);
112                 if(pldata->phy_suspend)
113                         pldata->phy_suspend(pldata, USB_PHY_ENABLED);
114                 mdelay(5);
115                 ehci_port_power(ehci, 1);
116                 printk("%s, enable host controller\n", __func__);
117         }
118         rk_ehci->host_enabled = rk_ehci->host_setenable;
119
120 out:
121         return;
122 }
123
124 static void rk_ehci_hcd_connect_detect(unsigned long pdata)
125 {
126         struct rk_ehci_hcd *rk_ehci= (struct rk_ehci_hcd*)pdata;
127         struct ehci_hcd *ehci = rk_ehci->ehci;
128         struct rkehci_platform_data *pldata;
129         uint32_t status;
130         unsigned long flags;
131
132         local_irq_save(flags);
133
134         pldata = rk_ehci->pldata;
135
136         if(pldata->get_status(USB_STATUS_DPDM)){
137                 // usb device connected
138                 rk_ehci->host_setenable = 1;
139         }else{
140                 // no device, suspend host
141                 status = readl(&ehci->regs->port_status[0]);
142                 if(!(status & PORT_CONNECT)){
143                         rk_ehci->host_setenable = 2;
144                 }
145         }
146
147         if((rk_ehci->host_enabled) && (rk_ehci->host_setenable != rk_ehci->host_enabled)){
148                 schedule_delayed_work(&rk_ehci->host_enable_work, 1);
149         }
150
151         mod_timer(&rk_ehci->connect_detect_timer,jiffies + (HZ<<1));
152
153         local_irq_restore(flags);
154         return;
155 }
156
157 static struct hc_driver rk_ehci_hc_driver = {
158         .description            = hcd_name,
159         .product_desc           = "Rockchip On-Chip EHCI Host Controller",
160         .hcd_priv_size          = sizeof(struct ehci_hcd),
161
162         /*
163          * generic hardware linkage
164          */
165         .irq                    = ehci_irq,
166         .flags                  = HCD_USB2 | HCD_MEMORY,
167
168         .reset                  = ehci_init,
169         .start                  = ehci_run,
170
171         .stop                   = ehci_stop,
172         .shutdown               = ehci_shutdown,
173
174         /*
175          * managing i/o requests and associated device resources
176          */
177         .urb_enqueue            = ehci_urb_enqueue,
178         .urb_dequeue            = ehci_urb_dequeue,
179         .endpoint_disable       = ehci_endpoint_disable,
180         .endpoint_reset         = ehci_endpoint_reset,
181         .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
182
183         /*
184          * scheduling support
185          */
186         .get_frame_number       = ehci_get_frame,
187
188         /*
189          * root hub support
190          */
191         .hub_status_data        = ehci_hub_status_data,
192         .hub_control            = ehci_hub_control,
193         .relinquish_port        = ehci_relinquish_port,
194         .port_handed_over       = ehci_port_handed_over,
195
196         /*
197          * PM support
198          */
199 #ifdef CONFIG_PM
200         .bus_suspend            = ehci_bus_suspend,
201         .bus_resume             = ehci_bus_resume,
202 #endif
203 };
204 static ssize_t ehci_power_show( struct device *_dev,
205                                 struct device_attribute *attr, char *buf)
206 {
207         return sprintf(buf, "%d\n", rkehci_status);
208 }
209 static ssize_t ehci_power_store( struct device *_dev,
210                                         struct device_attribute *attr,
211                                         const char *buf, size_t count )
212 {
213         uint32_t val = simple_strtoul(buf, NULL, 16);
214         struct usb_hcd *hcd = dev_get_drvdata(_dev);
215         struct ehci_hcd *ehci = hcd_to_ehci(hcd);
216         struct rkehci_platform_data *pldata = _dev->platform_data;
217
218         printk("%s: %d setting to: %d\n", __func__, rkehci_status, val);
219         if(val == rkehci_status)
220                 goto out;
221
222         rkehci_status = val;
223         switch(val){
224                 case 0: //power down
225                         ehci_port_power(ehci, 0);
226                         msleep(5);
227                         usb_remove_hcd(hcd);
228                         break;
229                 case 1:// power on
230                         pldata->soft_reset();
231                         usb_add_hcd(hcd, hcd->irq, IRQF_DISABLED | IRQF_SHARED);
232                         ehci_port_power(ehci, 1);
233 //                      writel_relaxed(0x1d4d ,hcd->regs +0x90);
234 //                      writel_relaxed(0x4 ,hcd->regs +0xa0);
235 //                      dsb();
236                         break;
237                 default:
238                         break;
239         }
240 out:
241         return count;
242 }
243 static DEVICE_ATTR(ehci_power, S_IRUGO|S_IWUSR, ehci_power_show, ehci_power_store);
244 static ssize_t debug_show( struct device *_dev,
245                                 struct device_attribute *attr, char *buf)
246 {
247         volatile uint32_t *addr;
248
249         EHCI_PRINT("******** EHCI Capability Registers **********\n");
250         addr = &g_ehci->caps->hc_capbase;
251         EHCI_PRINT("HCIVERSION / CAPLENGTH  @0x%08x:  0x%08x\n", (uint32_t)addr, readl_relaxed(addr));
252         addr = &g_ehci->caps->hcs_params;
253         EHCI_PRINT("HCSPARAMS               @0x%08x:  0x%08x\n", (uint32_t)addr, readl_relaxed(addr));
254         addr = &g_ehci->caps->hcc_params;
255         EHCI_PRINT("HCCPARAMS               @0x%08x:  0x%08x\n", (uint32_t)addr, readl_relaxed(addr));
256         EHCI_PRINT("********* EHCI Operational Registers *********\n");
257         addr = &g_ehci->regs->command;
258         EHCI_PRINT("USBCMD                  @0x%08x:  0x%08x\n", (uint32_t)addr, readl_relaxed(addr));
259         addr = &g_ehci->regs->status;
260         EHCI_PRINT("USBSTS                  @0x%08x:  0x%08x\n", (uint32_t)addr, readl_relaxed(addr));
261         addr = &g_ehci->regs->intr_enable;
262         EHCI_PRINT("USBINTR                 @0x%08x:  0x%08x\n", (uint32_t)addr, readl_relaxed(addr));
263         addr = &g_ehci->regs->frame_index;
264         EHCI_PRINT("FRINDEX                 @0x%08x:  0x%08x\n", (uint32_t)addr, readl_relaxed(addr));
265         addr = &g_ehci->regs->segment;
266         EHCI_PRINT("CTRLDSSEGMENT           @0x%08x:  0x%08x\n", (uint32_t)addr, readl_relaxed(addr));
267         addr = &g_ehci->regs->frame_list;
268         EHCI_PRINT("PERIODICLISTBASE        @0x%08x:  0x%08x\n", (uint32_t)addr, readl_relaxed(addr)); 
269         addr = &g_ehci->regs->async_next;
270         EHCI_PRINT("ASYNCLISTADDR           @0x%08x:  0x%08x\n", (uint32_t)addr, readl_relaxed(addr));
271         addr = &g_ehci->regs->configured_flag;
272         EHCI_PRINT("CONFIGFLAG              @0x%08x:  0x%08x\n", (uint32_t)addr, readl_relaxed(addr));
273         addr = g_ehci->regs->port_status;
274         EHCI_PRINT("PORTSC                  @0x%08x:  0x%08x\n", (uint32_t)addr, readl_relaxed(addr));
275         return sprintf(buf, "EHCI Registers Dump\n");
276 }
277 static DEVICE_ATTR(debug_ehci, S_IRUGO, debug_show, NULL);
278
279 static struct of_device_id rk_ehci_of_match[] = {
280         {
281                 .compatible = "rockchip,rk3288_rk_ehci_host",
282                 .data = &rkehci_pdata[RK3288_USB_CTLR],
283         },
284         { },
285 };
286 MODULE_DEVICE_TABLE(of, rk_ehci_of_match);
287
288 static int ehci_rk_probe(struct platform_device *pdev)
289 {
290         struct usb_hcd *hcd;
291         struct ehci_hcd *ehci;
292         struct resource *res;
293         struct device *dev = &pdev->dev;
294         struct rkehci_platform_data *pldata;
295         int ret;
296         int retval = 0;
297         static u64 usb_dmamask = 0xffffffffUL;
298         struct device_node *node = pdev->dev.of_node;
299         struct rkehci_pdata_id *p;
300         struct rk_ehci_hcd *rk_ehci;
301         const struct of_device_id *match =
302                 of_match_device(of_match_ptr( rk_ehci_of_match ), &pdev->dev);
303
304         dev_dbg(&pdev->dev, "ehci_rk proble\n");
305         
306         if (match){
307                 p = (struct rkehci_pdata_id *)match->data;
308         }else{
309                 dev_err(dev, "ehci_rk match failed\n");
310                 return -EINVAL;
311         }
312
313         dev->platform_data = p->pdata;
314         pldata = dev->platform_data;
315         pldata->dev = dev;
316
317         if (!node) {
318                 dev_err(dev, "device node not found\n");
319                 return -EINVAL;
320         }
321
322         dev->dma_mask = &usb_dmamask;
323
324         retval = device_create_file(dev, &dev_attr_ehci_power);
325         retval = device_create_file(dev, &dev_attr_debug_ehci);
326         hcd = usb_create_hcd(&rk_ehci_hc_driver, &pdev->dev, dev_name(&pdev->dev));
327         if (!hcd) {
328                 dev_err(&pdev->dev, "Unable to create HCD\n");
329                 return  -ENOMEM;
330         }
331
332         if(pldata->hw_init)
333                 pldata->hw_init();
334
335         if(pldata->clock_init){
336                 pldata->clock_init(pldata);
337                 pldata->clock_enable(pldata, 1);
338         }
339
340         if(pldata->phy_suspend)
341                 pldata->phy_suspend(pldata, USB_PHY_ENABLED);
342
343         if(pldata->soft_reset)
344                 pldata->soft_reset();
345
346         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
347         if (!res) {
348                 dev_err(&pdev->dev, "Unable to get memory resource\n");
349                 ret = -ENODEV;
350                 goto put_hcd;
351         }
352
353         hcd->rsrc_start = res->start;
354         hcd->rsrc_len = resource_size(res);
355         hcd->regs = devm_ioremap_resource(dev, res);
356
357         if (!hcd->regs) {
358                 dev_err(&pdev->dev, "ioremap failed\n");
359                 ret = -ENOMEM;
360                 goto put_hcd;
361         }
362
363         hcd->irq = platform_get_irq(pdev, 0);
364         if (hcd->irq < 0) {
365                 dev_err(&pdev->dev, "Unable to get IRQ resource\n");
366                 ret = hcd->irq;
367                 goto put_hcd;
368         }
369
370         ehci = hcd_to_ehci(hcd);
371         ehci->caps = hcd->regs;
372         ehci->regs = hcd->regs + 0x10;
373         printk("%s %p %p\n", __func__, ehci->caps, ehci->regs);
374
375         dbg_hcs_params(ehci, "reset");
376         dbg_hcc_params(ehci, "reset");
377
378         ehci->hcs_params = readl(&ehci->caps->hcs_params);
379
380         ret = usb_add_hcd(hcd, hcd->irq, IRQF_DISABLED | IRQF_SHARED);
381         if (ret) {
382                 dev_err(&pdev->dev, "Failed to add USB HCD\n");
383                 goto put_hcd;
384         }
385
386         g_ehci = ehci;
387
388         rk_ehci = devm_kzalloc(&pdev->dev, sizeof(struct rk_ehci_hcd),
389                                                    GFP_KERNEL);
390         if(!rk_ehci){
391                 ret = -ENOMEM;
392                 goto put_hcd;
393         }
394
395         rk_ehci->ehci = ehci;
396         rk_ehci->pldata = pldata;
397         rk_ehci->host_enabled = 2;
398         rk_ehci->host_setenable = 2;
399         rk_ehci->connect_detect_timer.function = rk_ehci_hcd_connect_detect;
400         rk_ehci->connect_detect_timer.data = (unsigned long)(rk_ehci);
401         init_timer( &rk_ehci->connect_detect_timer );
402         mod_timer( &rk_ehci->connect_detect_timer, jiffies+(HZ<<3) );
403         INIT_DELAYED_WORK( &rk_ehci->host_enable_work, rk_ehci_hcd_enable );
404
405         ehci_port_power(ehci, 0);
406
407         if(pldata->phy_suspend){
408                 if( pldata->phy_status == USB_PHY_ENABLED ){
409                         pldata->phy_suspend(pldata, USB_PHY_SUSPEND);
410                         /* do not disable EHCI clk, otherwise RK3288
411                          * host1(DWC_OTG) can't work normally.
412                          * udelay(3);
413                          * pldata->clock_enable(pldata, 0);
414                          */
415                 }
416         }
417
418         printk("%s ok\n", __func__);
419
420         return 0;
421  
422 put_hcd:
423         if(pldata->clock_enable)
424                 pldata->clock_enable(pldata, 0);
425         usb_put_hcd(hcd);
426
427         return ret;
428 }
429 static int ehci_rk_remove(struct platform_device *pdev)
430 {
431         struct usb_hcd *hcd = platform_get_drvdata(pdev);
432
433         usb_put_hcd(hcd);
434
435         return 0;
436 }
437 #ifdef CONFIG_PM
438 static int ehci_rk_pm_suspend(struct device *dev)
439 {
440         struct usb_hcd *hcd = dev_get_drvdata(dev);
441         bool do_wakeup  = device_may_wakeup(dev);
442         int ret;
443
444         dev_dbg(dev, "ehci-rockchip PM suspend\n");
445
446         ret = ehci_suspend(hcd, do_wakeup);
447
448         return ret;
449 }
450
451 static int ehci_rk_pm_resume(struct device *dev)
452 {
453         struct usb_hcd *hcd = dev_get_drvdata(dev);
454
455         dev_dbg(dev, "ehci-rockchip PM resume\n");
456         ehci_resume(hcd, false);
457
458         return 0;
459 }
460 #else
461 #define ehci_rk_pm_suspend      NULL
462 #define ehci_rk_pm_resume       NULL
463 #endif
464
465 static const struct dev_pm_ops ehci_rk_dev_pm_ops = {
466         .suspend        = ehci_rk_pm_suspend,
467         .resume         = ehci_rk_pm_resume,
468 };
469
470 static struct platform_driver ehci_rk_driver = {
471         .probe  = ehci_rk_probe,
472         .remove = ehci_rk_remove,
473         .driver = {
474                    .name = "rockchip_ehci_host",
475                    .of_match_table = of_match_ptr(rk_ehci_of_match),
476 #ifdef CONFIG_PM
477                    .pm = &ehci_rk_dev_pm_ops,
478 #endif
479         },
480 };