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>
24 # include <linux/usb.h>
25 # include <linux/usb/hcd.h>
26 # include <linux/usb/otg.h>
28 # include "../dwc_otg_310/usbdev_rk.h"
31 static int rkehci_status = 1;
32 static struct hc_driver rk_ehci_hc_driver;
35 struct ehci_hcd *ehci;
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;
42 #define EHCI_PRINT(x...) printk(KERN_INFO "EHCI: " x)
44 static struct rkehci_pdata_id rkehci_pdata[] = {
46 .name = "rk3188-reserved",
50 .name = "rk3288-ehci",
51 .pdata = &rkehci_pdata_rk3288,
56 static void rk_ehci_hcd_enable(struct work_struct *work)
58 struct rk_ehci_hcd *rk_ehci;
60 struct rkehci_platform_data *pldata;
61 struct ehci_hcd *ehci;
63 rk_ehci = container_of(work, struct rk_ehci_hcd, host_enable_work.work);
64 pldata = rk_ehci->pldata;
66 hcd = ehci_to_hcd(ehci);
68 if (rk_ehci->host_enabled == rk_ehci->host_setenable) {
69 EHCI_PRINT("%s, enable flag %d\n", __func__,
70 rk_ehci->host_setenable);
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;
82 EHCI_PRINT("%s, disable host controller\n", __func__);
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.
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);
99 EHCI_PRINT("%s, enable host controller\n", __func__);
101 rk_ehci->host_enabled = rk_ehci->host_setenable;
107 static void rk_ehci_hcd_connect_detect(unsigned long pdata)
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;
115 local_irq_save(flags);
117 pldata = rk_ehci->pldata;
119 if (pldata->get_status(USB_STATUS_DPDM)) {
120 /* usb device connected */
121 rk_ehci->host_setenable = 1;
123 /* no device, suspend host */
124 status = readl(&ehci->regs->port_status[0]);
125 if (!(status & PORT_CONNECT)) {
126 rk_ehci->host_setenable = 2;
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);
135 mod_timer(&rk_ehci->connect_detect_timer, jiffies + (HZ << 1));
137 local_irq_restore(flags);
141 static ssize_t ehci_power_show(struct device *_dev,
142 struct device_attribute *attr, char *buf)
144 return sprintf(buf, "%d\n", rkehci_status);
147 static ssize_t ehci_power_store(struct device *_dev,
148 struct device_attribute *attr,
149 const char *buf, size_t count)
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;
156 EHCI_PRINT("%s: %d setting to: %d\n", __func__, rkehci_status, val);
157 if (val == rkehci_status)
162 case 0: /* power down */
163 rk_ehci->host_enabled = 0;
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;
179 static DEVICE_ATTR(ehci_power, S_IRUGO | S_IWUSR, ehci_power_show,
181 static ssize_t debug_show(struct device *dev, struct device_attribute *attr,
184 struct ehci_hcd *ehci = hcd_to_ehci(dev_get_drvdata(dev));
185 volatile uint32_t *addr;
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");
228 static DEVICE_ATTR(debug_ehci, S_IRUGO, debug_show, NULL);
230 static int test_sq(struct ehci_hcd *ehci)
232 u32 portc = readl(&ehci->regs->port_status);
234 if ((portc & PORT_PE) && !(portc & PORT_SUSPEND)) {
235 writel(PORT_TEST_PKT, &ehci->regs->port_status);
236 EHCI_PRINT("Start packet test\n");
240 EHCI_PRINT("Invalid connect status PORTC = 0x%08x\n", portc);
245 static ssize_t test_sq_store(struct device *dev,
246 struct device_attribute *attr,
247 const char *buf, size_t count)
250 struct rkehci_platform_data *pldata = dev->platform_data;
251 struct ehci_hcd *ehci = hcd_to_ehci(dev_get_drvdata(dev));
253 if (pldata->phy_status == USB_PHY_SUSPEND) {
254 EHCI_PRINT("Invalid status : SUSPEND\n");
258 return (test_sq(ehci)) ? -EBUSY : count;
261 static DEVICE_ATTR(test_sq, S_IWUSR, NULL, test_sq_store);
263 static struct of_device_id rk_ehci_of_match[] = {
265 .compatible = "rockchip,rk3288_rk_ehci_host",
266 .data = &rkehci_pdata[RK3288_USB_CTLR],
271 MODULE_DEVICE_TABLE(of, rk_ehci_of_match);
273 static int ehci_rk_probe(struct platform_device *pdev)
276 struct ehci_hcd *ehci;
277 struct resource *res;
278 struct device *dev = &pdev->dev;
279 struct rkehci_platform_data *pldata;
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);
287 dev_dbg(&pdev->dev, "ehci_rk probe!\n");
290 p = (struct rkehci_pdata_id *)match->data;
292 dev_err(dev, "ehci_rk match failed\n");
296 dev->platform_data = p->pdata;
297 pldata = dev->platform_data;
301 dev_err(dev, "device node not found\n");
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);
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);
315 usb_create_hcd(&rk_ehci_hc_driver, &pdev->dev,
316 dev_name(&pdev->dev));
318 dev_err(&pdev->dev, "Unable to create HCD\n");
325 if (pldata->clock_init) {
326 pldata->clock_init(pldata);
327 pldata->clock_enable(pldata, 1);
330 if (pldata->phy_suspend)
331 pldata->phy_suspend(pldata, USB_PHY_ENABLED);
333 if (pldata->soft_reset)
334 pldata->soft_reset(pldata, RST_POR);;
336 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
338 dev_err(&pdev->dev, "Unable to get memory resource\n");
343 hcd->rsrc_start = res->start;
344 hcd->rsrc_len = resource_size(res);
345 hcd->regs = devm_ioremap_resource(dev, res);
347 dev_err(&pdev->dev, "ioremap failed\n");
352 hcd->irq = platform_get_irq(pdev, 0);
354 dev_err(&pdev->dev, "Unable to get IRQ resource\n");
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);
364 ehci->hcs_params = readl(&ehci->caps->hcs_params);
366 ret = usb_add_hcd(hcd, hcd->irq, IRQF_DISABLED | IRQF_SHARED);
368 dev_err(&pdev->dev, "Failed to add USB HCD\n");
372 rk_ehci = (struct rk_ehci_hcd *)hcd_to_ehci(hcd)->priv;
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);
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.
395 * pldata->clock_enable(pldata, 0);
400 EHCI_PRINT("%s ok\n", __func__);
405 if (pldata->clock_enable)
406 pldata->clock_enable(pldata, 0);
412 static int ehci_rk_remove(struct platform_device *pdev)
414 struct usb_hcd *hcd = platform_get_drvdata(pdev);
415 struct device *dev = &pdev->dev;
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);
427 static int ehci_rk_pm_suspend(struct device *dev)
429 struct usb_hcd *hcd = dev_get_drvdata(dev);
430 bool do_wakeup = device_may_wakeup(dev);
433 dev_dbg(dev, "ehci-rockchip PM suspend\n");
435 ret = ehci_suspend(hcd, do_wakeup);
440 static int ehci_rk_pm_resume(struct device *dev)
442 struct usb_hcd *hcd = dev_get_drvdata(dev);
444 dev_dbg(dev, "ehci-rockchip PM resume\n");
445 ehci_resume(hcd, false);
450 #define ehci_rk_pm_suspend NULL
451 #define ehci_rk_pm_resume NULL
454 static const struct dev_pm_ops ehci_rk_dev_pm_ops = {
455 .suspend = ehci_rk_pm_suspend,
456 .resume = ehci_rk_pm_resume,
459 static struct platform_driver rk_ehci_driver = {
460 .probe = ehci_rk_probe,
461 .remove = ehci_rk_remove,
463 .name = "rockchip_ehci_host",
464 .of_match_table = of_match_ptr(rk_ehci_of_match),
466 .pm = &ehci_rk_dev_pm_ops,
471 static const struct ehci_driver_overrides rk_overrides __initdata = {
472 .extra_priv_size = sizeof(struct rk_ehci_hcd),
475 static int __init ehci_rk_init(void)
480 ehci_init_driver(&rk_ehci_hc_driver, &rk_overrides);
481 return platform_driver_register(&rk_ehci_driver);
483 module_init(ehci_rk_init);
485 static void __exit ehci_rk_cleanup(void)
487 platform_driver_unregister(&rk_ehci_driver);
489 module_exit(ehci_rk_cleanup);
490 MODULE_AUTHOR("Rockchip");
491 MODULE_LICENSE("GPL v2");