2 * EHCI-compliant USB host controller driver for Rockchip SoCs
4 * Copyright (C) 2010 Google, Inc.
5 * Copyright (C) 2009 - 2013 NVIDIA Corporation
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.
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
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>
25 #ifdef CONFIG_DWC_OTG_274
26 # include "../dwc_otg/usbdev_rk.h"
28 #ifdef CONFIG_DWC_OTG_310
29 # include "../dwc_otg_310/usbdev_rk.h"
32 static int rkehci_status = 1;
33 static struct ehci_hcd *g_ehci;
34 #define EHCI_PRINT(x...) printk( KERN_INFO "EHCI: " x )
36 static struct rkehci_pdata_id rkehci_pdata[] = {
38 .name = "rk3188-reserved",
42 .name = "rk3288-ehci",
43 .pdata = &rkehci_pdata_rk3288,
48 static void ehci_port_power (struct ehci_hcd *ehci, int is_on)
52 if (!HCS_PPC (ehci->hcs_params))
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,
61 /* Flush those writes */
62 ehci_readl(ehci, &ehci->regs->command);
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),
72 * generic hardware linkage
75 .flags = HCD_USB2 | HCD_MEMORY,
81 .shutdown = ehci_shutdown,
84 * managing i/o requests and associated device resources
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,
95 .get_frame_number = ehci_get_frame,
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,
109 .bus_suspend = ehci_bus_suspend,
110 .bus_resume = ehci_bus_resume,
113 static ssize_t ehci_power_show( struct device *_dev,
114 struct device_attribute *attr, char *buf)
116 return sprintf(buf, "%d\n", rkehci_status);
118 static ssize_t ehci_power_store( struct device *_dev,
119 struct device_attribute *attr,
120 const char *buf, size_t count )
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;
127 printk("%s: %d setting to: %d\n", __func__, rkehci_status, val);
128 if(val == rkehci_status)
134 ehci_port_power(ehci, 0);
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);
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)
156 volatile uint32_t *addr;
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");
186 static DEVICE_ATTR(debug_ehci, S_IRUGO, debug_show, NULL);
188 static struct of_device_id rk_ehci_of_match[] = {
190 .compatible = "rockchip,rk3288_rk_ehci_host",
191 .data = &rkehci_pdata[RK3288_USB_CTLR],
195 MODULE_DEVICE_TABLE(of, rk_ehci_of_match);
197 static int ehci_rk_probe(struct platform_device *pdev)
200 struct ehci_hcd *ehci;
201 struct resource *res;
202 struct device *dev = &pdev->dev;
203 struct rkehci_platform_data *pldata;
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);
212 dev_dbg(&pdev->dev, "ehci_rk proble\n");
215 p = (struct rkehci_pdata_id *)match->data;
217 dev_err(dev, "ehci_rk match failed\n");
221 dev->platform_data = p->pdata;
222 pldata = dev->platform_data;
226 dev_err(dev, "device node not found\n");
230 dev->dma_mask = &usb_dmamask;
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));
236 dev_err(&pdev->dev, "Unable to create HCD\n");
243 if(pldata->clock_init){
244 pldata->clock_init(pldata);
245 pldata->clock_enable(pldata, 1);
248 if(pldata->soft_reset)
249 pldata->soft_reset();
251 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
253 dev_err(&pdev->dev, "Unable to get memory resource\n");
258 hcd->rsrc_start = res->start;
259 hcd->rsrc_len = resource_size(res);
260 hcd->regs = devm_ioremap_resource(dev, res);
263 dev_err(&pdev->dev, "ioremap failed\n");
268 hcd->irq = platform_get_irq(pdev, 0);
270 dev_err(&pdev->dev, "Unable to get IRQ resource\n");
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);
280 dbg_hcs_params(ehci, "reset");
281 dbg_hcc_params(ehci, "reset");
283 ehci->hcs_params = readl(&ehci->caps->hcs_params);
285 ret = usb_add_hcd(hcd, hcd->irq, IRQF_DISABLED | IRQF_SHARED);
287 dev_err(&pdev->dev, "Failed to add USB HCD\n");
292 ehci_port_power(ehci, 1);
293 // writel_relaxed(0x1d4d ,hcd->regs +0x90);
294 // writel_relaxed(0x4 ,hcd->regs +0xa0);
297 printk("%s ok\n", __func__);
302 if(pldata->clock_enable)
303 pldata->clock_enable(pldata, 0);
308 static int ehci_rk_remove(struct platform_device *pdev)
310 struct usb_hcd *hcd = platform_get_drvdata(pdev);
317 static int ehci_rk_pm_suspend(struct device *dev)
319 struct usb_hcd *hcd = dev_get_drvdata(dev);
320 bool wakeup = device_may_wakeup(dev);
322 dev_dbg(dev, "ehci-rockchip PM suspend\n");
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
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),
340 static int ehci_rk_pm_resume(struct device *dev)
342 struct usb_hcd *hcd = dev_get_drvdata(dev);
344 dev_dbg(dev, "ehci-rockchip PM resume\n");
345 ehci_prepare_ports_for_controller_resume(hcd_to_ehci(hcd));
350 #define ehci_rk_pm_suspend NULL
351 #define ehci_rk_pm_resume NULL
354 static const struct dev_pm_ops ehci_rk_dev_pm_ops = {
355 .suspend = ehci_rk_pm_suspend,
356 .resume = ehci_rk_pm_resume,
359 static struct platform_driver ehci_rk_driver = {
360 .probe = ehci_rk_probe,
361 .remove = ehci_rk_remove,
363 .name = "rockchip_ehci_host",
364 .of_match_table = of_match_ptr(rk_ehci_of_match),
366 .pm = &ehci_rk_dev_pm_ops,