USB: RK3288 USB CTLR initialization
[firefly-linux-kernel-4.4.55.git] / drivers / usb / host / ehci-rk.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 #define EHCI_PRINT(x...)   printk( KERN_INFO "EHCI: " x )
35
36 static struct rkehci_pdata_id rkehci_pdata[] = {
37         {
38                 .name = "rk3188-reserved",
39                 .pdata = NULL,
40         },
41         {
42                 .name = "rk3288-ehci",
43                 .pdata = &rkehci_pdata_rk3288,
44         },
45         { },
46 };
47
48 static void ehci_port_power (struct ehci_hcd *ehci, int is_on)
49 {
50         unsigned port;
51
52         if (!HCS_PPC (ehci->hcs_params))
53                 return;
54
55         ehci_dbg (ehci, "...power%s ports...\n", is_on ? "up" : "down");
56         for (port = HCS_N_PORTS (ehci->hcs_params); port > 0; )
57                 (void) ehci_hub_control(ehci_to_hcd(ehci),
58                                 is_on ? SetPortFeature : ClearPortFeature,
59                                 USB_PORT_FEAT_POWER,
60                                 port--, NULL, 0);
61         /* Flush those writes */
62         ehci_readl(ehci, &ehci->regs->command);
63         msleep(20);
64 }
65
66 static struct hc_driver rk_ehci_hc_driver = {
67         .description            = hcd_name,
68         .product_desc           = "Rockchip On-Chip EHCI Host Controller",
69         .hcd_priv_size          = sizeof(struct ehci_hcd),
70
71         /*
72          * generic hardware linkage
73          */
74         .irq                    = ehci_irq,
75         .flags                  = HCD_USB2 | HCD_MEMORY,
76
77         .reset                  = ehci_init,
78         .start                  = ehci_run,
79
80         .stop                   = ehci_stop,
81         .shutdown               = ehci_shutdown,
82
83         /*
84          * managing i/o requests and associated device resources
85          */
86         .urb_enqueue            = ehci_urb_enqueue,
87         .urb_dequeue            = ehci_urb_dequeue,
88         .endpoint_disable       = ehci_endpoint_disable,
89         .endpoint_reset         = ehci_endpoint_reset,
90         .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
91
92         /*
93          * scheduling support
94          */
95         .get_frame_number       = ehci_get_frame,
96
97         /*
98          * root hub support
99          */
100         .hub_status_data        = ehci_hub_status_data,
101         .hub_control            = ehci_hub_control,
102         .relinquish_port        = ehci_relinquish_port,
103         .port_handed_over       = ehci_port_handed_over,
104
105         /*
106          * PM support
107          */
108 #ifdef CONFIG_PM
109         .bus_suspend            = ehci_bus_suspend,
110         .bus_resume             = ehci_bus_resume,
111 #endif
112 };
113 static ssize_t ehci_power_show( struct device *_dev,
114                                 struct device_attribute *attr, char *buf)
115 {
116         return sprintf(buf, "%d\n", rkehci_status);
117 }
118 static ssize_t ehci_power_store( struct device *_dev,
119                                         struct device_attribute *attr,
120                                         const char *buf, size_t count )
121 {
122         uint32_t val = simple_strtoul(buf, NULL, 16);
123         struct usb_hcd *hcd = dev_get_drvdata(_dev);
124         struct ehci_hcd *ehci = hcd_to_ehci(hcd);
125         struct rkehci_platform_data *pldata = _dev->platform_data;
126
127         printk("%s: %d setting to: %d\n", __func__, rkehci_status, val);
128         if(val == rkehci_status)
129                 goto out;
130
131         rkehci_status = val;
132         switch(val){
133                 case 0: //power down
134                         ehci_port_power(ehci, 0);
135                         msleep(5);
136                         usb_remove_hcd(hcd);
137                         break;
138                 case 1:// power on
139                         pldata->soft_reset();
140                         usb_add_hcd(hcd, hcd->irq, IRQF_DISABLED | IRQF_SHARED);
141                         ehci_port_power(ehci, 1);
142                         writel_relaxed(0x1d4d ,hcd->regs +0x90);
143                         writel_relaxed(0x4 ,hcd->regs +0xa0);
144                         dsb();
145                         break;
146                 default:
147                         break;
148         }
149 out:
150         return count;
151 }
152 static DEVICE_ATTR(ehci_power, S_IRUGO|S_IWUSR, ehci_power_show, ehci_power_store);
153 static ssize_t debug_show( struct device *_dev,
154                                 struct device_attribute *attr, char *buf)
155 {
156         volatile uint32_t *addr;
157
158         EHCI_PRINT("******** EHCI Capability Registers **********\n");
159         addr = &g_ehci->caps->hc_capbase;
160         EHCI_PRINT("HCIVERSION / CAPLENGTH  @0x%08x:  0x%08x\n", (uint32_t)addr, readl_relaxed(addr));
161         addr = &g_ehci->caps->hcs_params;
162         EHCI_PRINT("HCSPARAMS               @0x%08x:  0x%08x\n", (uint32_t)addr, readl_relaxed(addr));
163         addr = &g_ehci->caps->hcc_params;
164         EHCI_PRINT("HCCPARAMS               @0x%08x:  0x%08x\n", (uint32_t)addr, readl_relaxed(addr));
165         EHCI_PRINT("********* EHCI Operational Registers *********\n");
166         addr = &g_ehci->regs->command;
167         EHCI_PRINT("USBCMD                  @0x%08x:  0x%08x\n", (uint32_t)addr, readl_relaxed(addr));
168         addr = &g_ehci->regs->status;
169         EHCI_PRINT("USBSTS                  @0x%08x:  0x%08x\n", (uint32_t)addr, readl_relaxed(addr));
170         addr = &g_ehci->regs->intr_enable;
171         EHCI_PRINT("USBINTR                 @0x%08x:  0x%08x\n", (uint32_t)addr, readl_relaxed(addr));
172         addr = &g_ehci->regs->frame_index;
173         EHCI_PRINT("FRINDEX                 @0x%08x:  0x%08x\n", (uint32_t)addr, readl_relaxed(addr));
174         addr = &g_ehci->regs->segment;
175         EHCI_PRINT("CTRLDSSEGMENT           @0x%08x:  0x%08x\n", (uint32_t)addr, readl_relaxed(addr));
176         addr = &g_ehci->regs->frame_list;
177         EHCI_PRINT("PERIODICLISTBASE        @0x%08x:  0x%08x\n", (uint32_t)addr, readl_relaxed(addr)); 
178         addr = &g_ehci->regs->async_next;
179         EHCI_PRINT("ASYNCLISTADDR           @0x%08x:  0x%08x\n", (uint32_t)addr, readl_relaxed(addr));
180         addr = &g_ehci->regs->configured_flag;
181         EHCI_PRINT("CONFIGFLAG              @0x%08x:  0x%08x\n", (uint32_t)addr, readl_relaxed(addr));
182         addr = g_ehci->regs->port_status;
183         EHCI_PRINT("PORTSC                  @0x%08x:  0x%08x\n", (uint32_t)addr, readl_relaxed(addr));
184         return sprintf(buf, "EHCI Registers Dump\n");
185 }
186 static DEVICE_ATTR(debug_ehci, S_IRUGO, debug_show, NULL);
187
188 static struct of_device_id rk_ehci_of_match[] = {
189         {
190                 .compatible = "rockchip,rk3288_rk_ehci_host",
191                 .data = &rkehci_pdata[RK3288_USB_CTLR],
192         },
193         { },
194 };
195 MODULE_DEVICE_TABLE(of, rk_ehci_of_match);
196
197 static int ehci_rk_probe(struct platform_device *pdev)
198 {
199         struct usb_hcd *hcd;
200         struct ehci_hcd *ehci;
201         struct resource *res;
202         struct device *dev = &pdev->dev;
203         struct rkehci_platform_data *pldata;
204         int ret;
205         int retval = 0;
206         static u64 usb_dmamask = 0xffffffffUL;
207         struct device_node *node = pdev->dev.of_node;
208         struct rkehci_pdata_id *p;
209         const struct of_device_id *match =
210                 of_match_device(of_match_ptr( rk_ehci_of_match ), &pdev->dev);
211
212         dev_dbg(&pdev->dev, "ehci_rk proble\n");
213         
214         if (match){
215                 p = (struct rkehci_pdata_id *)match->data;
216         }else{
217                 dev_err(dev, "ehci_rk match failed\n");
218                 return -EINVAL;
219         }
220
221         dev->platform_data = p->pdata;
222         pldata = dev->platform_data;
223         pldata->dev = dev;
224
225         if (!node) {
226                 dev_err(dev, "device node not found\n");
227                 return -EINVAL;
228         }
229
230         dev->dma_mask = &usb_dmamask;
231
232         retval = device_create_file(dev, &dev_attr_ehci_power);
233         retval = device_create_file(dev, &dev_attr_debug_ehci);
234         hcd = usb_create_hcd(&rk_ehci_hc_driver, &pdev->dev, dev_name(&pdev->dev));
235         if (!hcd) {
236                 dev_err(&pdev->dev, "Unable to create HCD\n");
237                 return  -ENOMEM;
238         }
239
240         if(pldata->hw_init)
241                 pldata->hw_init();
242
243         if(pldata->clock_init){
244                 pldata->clock_init(pldata);
245                 pldata->clock_enable(pldata, 1);
246         }
247
248         if(pldata->soft_reset)
249                 pldata->soft_reset();
250
251         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
252         if (!res) {
253                 dev_err(&pdev->dev, "Unable to get memory resource\n");
254                 ret = -ENODEV;
255                 goto put_hcd;
256         }
257
258         hcd->rsrc_start = res->start;
259         hcd->rsrc_len = resource_size(res);
260         hcd->regs = devm_ioremap_resource(dev, res);
261
262         if (!hcd->regs) {
263                 dev_err(&pdev->dev, "ioremap failed\n");
264                 ret = -ENOMEM;
265                 goto put_hcd;
266         }
267
268         hcd->irq = platform_get_irq(pdev, 0);
269         if (hcd->irq < 0) {
270                 dev_err(&pdev->dev, "Unable to get IRQ resource\n");
271                 ret = hcd->irq;
272                 goto put_hcd;
273         }
274
275         ehci = hcd_to_ehci(hcd);
276         ehci->caps = hcd->regs;
277         ehci->regs = hcd->regs + 0x10;
278         printk("%s %p %p\n", __func__, ehci->caps, ehci->regs);
279
280         dbg_hcs_params(ehci, "reset");
281         dbg_hcc_params(ehci, "reset");
282
283         ehci->hcs_params = readl(&ehci->caps->hcs_params);
284
285         ret = usb_add_hcd(hcd, hcd->irq, IRQF_DISABLED | IRQF_SHARED);
286         if (ret) {
287                 dev_err(&pdev->dev, "Failed to add USB HCD\n");
288                 goto put_hcd;
289         }
290
291         g_ehci = ehci;
292         ehci_port_power(ehci, 1);
293         writel_relaxed(0x1d4d ,hcd->regs +0x90);
294         writel_relaxed(0x4 ,hcd->regs +0xa0);
295         dsb();
296
297         printk("%s ok\n", __func__);
298
299         return 0;
300  
301 put_hcd:
302         if(pldata->clock_enable)
303                 pldata->clock_enable(pldata, 0);
304         usb_put_hcd(hcd);
305
306         return ret;
307 }
308 static int ehci_rk_remove(struct platform_device *pdev)
309 {
310         struct usb_hcd *hcd = platform_get_drvdata(pdev);
311
312         usb_put_hcd(hcd);
313
314         return 0;
315 }
316 #ifdef CONFIG_PM
317 static int ehci_rk_pm_suspend(struct device *dev)
318 {
319         struct usb_hcd *hcd = dev_get_drvdata(dev);
320         bool wakeup = device_may_wakeup(dev);
321
322         dev_dbg(dev, "ehci-rk PM suspend\n");
323
324         /*
325          * EHCI helper function has also the same check before manipulating
326          * port wakeup flags.  We do check here the same condition before
327          * calling the same helper function to avoid bringing hardware
328          * from Low power mode when there is no need for adjusting port
329          * wakeup flags.
330          */
331         if (hcd->self.root_hub->do_remote_wakeup && !wakeup) {
332                 pm_runtime_resume(dev);
333                 ehci_prepare_ports_for_controller_suspend(hcd_to_ehci(hcd),
334                                 wakeup);
335         }
336
337         return 0;
338 }
339
340 static int ehci_rk_pm_resume(struct device *dev)
341 {
342         struct usb_hcd *hcd = dev_get_drvdata(dev);
343
344         dev_dbg(dev, "ehci-rk PM resume\n");
345         ehci_prepare_ports_for_controller_resume(hcd_to_ehci(hcd));
346
347         return 0;
348 }
349 #else
350 #define ehci_rk_pm_suspend      NULL
351 #define ehci_rk_pm_resume       NULL
352 #endif
353
354 static const struct dev_pm_ops ehci_rk_dev_pm_ops = {
355         .suspend         = ehci_rk_pm_suspend,
356         .resume          = ehci_rk_pm_resume,
357 };
358
359 static struct platform_driver ehci_rk_driver = {
360         .probe  = ehci_rk_probe,
361         .remove = ehci_rk_remove,
362         .driver = {
363                    .name = "rk_ehci_host",
364                    .of_match_table = of_match_ptr(rk_ehci_of_match),
365 #ifdef CONFIG_PM
366                    .pm = &ehci_rk_dev_pm_ops,
367 #endif
368         },
369 };