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;
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 ehci_port_power (struct ehci_hcd *ehci, int is_on)
60 if (!HCS_PPC (ehci->hcs_params))
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,
69 /* Flush those writes */
70 ehci_readl(ehci, &ehci->regs->command);
74 static void rk_ehci_hcd_enable(struct work_struct *work)
76 struct rk_ehci_hcd *rk_ehci;
78 struct rkehci_platform_data *pldata;
79 struct ehci_hcd *ehci;
81 rk_ehci = container_of(work, struct rk_ehci_hcd, host_enable_work.work);
82 pldata = rk_ehci->pldata;
84 hcd = ehci_to_hcd(ehci);
86 if(rk_ehci->host_enabled == rk_ehci->host_setenable){
87 printk("%s, enable flag %d\n", __func__, rk_ehci->host_setenable);
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;
97 printk("%s, disable host controller\n", __func__);
98 ehci_port_power(ehci, 0);
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.
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);
115 ehci_port_power(ehci, 1);
116 printk("%s, enable host controller\n", __func__);
118 rk_ehci->host_enabled = rk_ehci->host_setenable;
124 static void rk_ehci_hcd_connect_detect(unsigned long pdata)
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;
132 local_irq_save(flags);
134 pldata = rk_ehci->pldata;
136 if(pldata->get_status(USB_STATUS_DPDM)){
137 // usb device connected
138 rk_ehci->host_setenable = 1;
140 // no device, suspend host
141 status = readl(&ehci->regs->port_status[0]);
142 if(!(status & PORT_CONNECT)){
143 rk_ehci->host_setenable = 2;
147 if((rk_ehci->host_enabled) && (rk_ehci->host_setenable != rk_ehci->host_enabled)){
148 schedule_delayed_work(&rk_ehci->host_enable_work, 1);
151 mod_timer(&rk_ehci->connect_detect_timer,jiffies + (HZ<<1));
153 local_irq_restore(flags);
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),
163 * generic hardware linkage
166 .flags = HCD_USB2 | HCD_MEMORY,
172 .shutdown = ehci_shutdown,
175 * managing i/o requests and associated device resources
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,
186 .get_frame_number = ehci_get_frame,
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,
200 .bus_suspend = ehci_bus_suspend,
201 .bus_resume = ehci_bus_resume,
204 static ssize_t ehci_power_show( struct device *_dev,
205 struct device_attribute *attr, char *buf)
207 return sprintf(buf, "%d\n", rkehci_status);
209 static ssize_t ehci_power_store( struct device *_dev,
210 struct device_attribute *attr,
211 const char *buf, size_t count )
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;
218 printk("%s: %d setting to: %d\n", __func__, rkehci_status, val);
219 if(val == rkehci_status)
225 ehci_port_power(ehci, 0);
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);
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)
247 volatile uint32_t *addr;
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");
277 static DEVICE_ATTR(debug_ehci, S_IRUGO, debug_show, NULL);
279 static struct of_device_id rk_ehci_of_match[] = {
281 .compatible = "rockchip,rk3288_rk_ehci_host",
282 .data = &rkehci_pdata[RK3288_USB_CTLR],
286 MODULE_DEVICE_TABLE(of, rk_ehci_of_match);
288 static int ehci_rk_probe(struct platform_device *pdev)
291 struct ehci_hcd *ehci;
292 struct resource *res;
293 struct device *dev = &pdev->dev;
294 struct rkehci_platform_data *pldata;
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);
304 dev_dbg(&pdev->dev, "ehci_rk proble\n");
307 p = (struct rkehci_pdata_id *)match->data;
309 dev_err(dev, "ehci_rk match failed\n");
313 dev->platform_data = p->pdata;
314 pldata = dev->platform_data;
318 dev_err(dev, "device node not found\n");
322 dev->dma_mask = &usb_dmamask;
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));
328 dev_err(&pdev->dev, "Unable to create HCD\n");
335 if(pldata->clock_init){
336 pldata->clock_init(pldata);
337 pldata->clock_enable(pldata, 1);
340 if(pldata->phy_suspend)
341 pldata->phy_suspend(pldata, USB_PHY_ENABLED);
343 if(pldata->soft_reset)
344 pldata->soft_reset();
346 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
348 dev_err(&pdev->dev, "Unable to get memory resource\n");
353 hcd->rsrc_start = res->start;
354 hcd->rsrc_len = resource_size(res);
355 hcd->regs = devm_ioremap_resource(dev, res);
358 dev_err(&pdev->dev, "ioremap failed\n");
363 hcd->irq = platform_get_irq(pdev, 0);
365 dev_err(&pdev->dev, "Unable to get IRQ resource\n");
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);
375 dbg_hcs_params(ehci, "reset");
376 dbg_hcc_params(ehci, "reset");
378 ehci->hcs_params = readl(&ehci->caps->hcs_params);
380 ret = usb_add_hcd(hcd, hcd->irq, IRQF_DISABLED | IRQF_SHARED);
382 dev_err(&pdev->dev, "Failed to add USB HCD\n");
388 rk_ehci = devm_kzalloc(&pdev->dev, sizeof(struct rk_ehci_hcd),
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 );
405 ehci_port_power(ehci, 0);
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.
413 * pldata->clock_enable(pldata, 0);
418 printk("%s ok\n", __func__);
423 if(pldata->clock_enable)
424 pldata->clock_enable(pldata, 0);
429 static int ehci_rk_remove(struct platform_device *pdev)
431 struct usb_hcd *hcd = platform_get_drvdata(pdev);
438 static int ehci_rk_pm_suspend(struct device *dev)
440 struct usb_hcd *hcd = dev_get_drvdata(dev);
441 bool do_wakeup = device_may_wakeup(dev);
444 dev_dbg(dev, "ehci-rockchip PM suspend\n");
446 ret = ehci_suspend(hcd, do_wakeup);
451 static int ehci_rk_pm_resume(struct device *dev)
453 struct usb_hcd *hcd = dev_get_drvdata(dev);
455 dev_dbg(dev, "ehci-rockchip PM resume\n");
456 ehci_resume(hcd, false);
461 #define ehci_rk_pm_suspend NULL
462 #define ehci_rk_pm_resume NULL
465 static const struct dev_pm_ops ehci_rk_dev_pm_ops = {
466 .suspend = ehci_rk_pm_suspend,
467 .resume = ehci_rk_pm_resume,
470 static struct platform_driver ehci_rk_driver = {
471 .probe = ehci_rk_probe,
472 .remove = ehci_rk_remove,
474 .name = "rockchip_ehci_host",
475 .of_match_table = of_match_ptr(rk_ehci_of_match),
477 .pm = &ehci_rk_dev_pm_ops,