2 #include "usbdev_grf_regs.h"
3 #include "dwc_otg_regs.h"
4 static struct dwc_otg_control_usb *control_usb;
6 #ifdef CONFIG_USB20_OTG
7 static void usb20otg_hw_init(void)
9 /* other haredware init,include:
10 * DRV_VBUS GPIO init */
11 if (gpio_is_valid(control_usb->otg_gpios->gpio)) {
12 if (gpio_get_value(control_usb->otg_gpios->gpio))
13 gpio_set_value(control_usb->otg_gpios->gpio, 0);
17 static void usb20otg_phy_suspend(void *pdata, int suspend)
19 struct dwc_otg_platform_data *usbpdata = pdata;
22 /* enable soft control */
23 writel(UOC_HIWORD_UPDATE(0x55, 0x7f, 0),
24 RK_GRF_VIRT + RK3036_GRF_UOC0_CON5);
25 usbpdata->phy_status = 1;
28 writel(UOC_HIWORD_UPDATE(0x0, 0x1, 0),
29 RK_GRF_VIRT + RK3036_GRF_UOC0_CON5);
30 usbpdata->phy_status = 0;
34 static void usb20otg_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
36 struct dwc_otg_platform_data *usbpdata = pdata;
37 struct reset_control *rst_otg_h, *rst_otg_p, *rst_otg_c;
39 rst_otg_h = devm_reset_control_get(usbpdata->dev, "otg_ahb");
40 rst_otg_p = devm_reset_control_get(usbpdata->dev, "otg_phy");
41 rst_otg_c = devm_reset_control_get(usbpdata->dev, "otg_controller");
42 if (IS_ERR(rst_otg_h) || IS_ERR(rst_otg_p) || IS_ERR(rst_otg_c)) {
43 dev_err(usbpdata->dev, "Fail to get reset control from dts\n");
50 writel(UOC_HIWORD_UPDATE(0x1, 0x3, 0),
51 RK_GRF_VIRT + RK3036_GRF_UOC0_CON5);
52 reset_control_assert(rst_otg_p);
54 writel(UOC_HIWORD_UPDATE(0x2, 0x3, 0),
55 RK_GRF_VIRT + RK3036_GRF_UOC0_CON5);
57 reset_control_deassert(rst_otg_p);
60 /* Controller reset */
61 reset_control_assert(rst_otg_c);
62 reset_control_assert(rst_otg_h);
66 reset_control_deassert(rst_otg_c);
67 reset_control_deassert(rst_otg_h);
75 static void usb20otg_clock_init(void *pdata)
77 struct dwc_otg_platform_data *usbpdata = pdata;
78 struct clk *ahbclk, *phyclk;
80 ahbclk = devm_clk_get(usbpdata->dev, "hclk_usb0");
82 dev_err(usbpdata->dev, "Failed to get hclk_usb0\n");
86 phyclk = devm_clk_get(usbpdata->dev, "clk_usbphy0");
88 dev_err(usbpdata->dev, "Failed to get clk_usbphy0\n");
92 usbpdata->phyclk = phyclk;
93 usbpdata->ahbclk = ahbclk;
96 static void usb20otg_clock_enable(void *pdata, int enable)
98 struct dwc_otg_platform_data *usbpdata = pdata;
101 clk_prepare_enable(usbpdata->ahbclk);
102 clk_prepare_enable(usbpdata->phyclk);
104 clk_disable_unprepare(usbpdata->ahbclk);
106 clk_disable_unprepare(usbpdata->phyclk);
111 static int usb20otg_get_status(int id)
114 u32 soc_status0 = readl(RK_GRF_VIRT + RK3036_GRF_SOC_STATUS0);
117 case USB_STATUS_BVABLID:
119 ret = soc_status0 & (0x1 << 8);
121 case USB_STATUS_DPDM:
123 ret = soc_status0 & (0x3 << 9);
127 ret = soc_status0 & (0x1 << 11);
130 ret = control_usb->chip_id;
132 case USB_REMOTE_WAKEUP:
133 ret = control_usb->remote_wakeup;
136 ret = control_usb->usb_irq_wakeup;
144 #ifdef CONFIG_RK_USB_UART
146 * dwc_otg_uart_enabled - check if a usb-uart bypass func is enabled in DT
148 * Returns true if the status property of node "usb_uart" is set to "okay"
149 * or "ok", if this property is absent it will use the default status "ok"
152 static bool dwc_otg_uart_enabled(void)
154 struct device_node *np;
156 np = of_find_node_by_name(NULL, "usb_uart");
157 if (np && of_device_is_available(np))
163 static void dwc_otg_uart_mode(void *pdata, int enter_usb_uart_mode)
165 if ((1 == enter_usb_uart_mode) && dwc_otg_uart_enabled()) {
166 /* bypass dm, enter uart mode */
167 writel(UOC_HIWORD_UPDATE(0x3, 0x3, 12), RK_GRF_VIRT +
168 RK3036_GRF_UOC1_CON4);
169 } else if (0 == enter_usb_uart_mode) {
171 writel(UOC_HIWORD_UPDATE(0x0, 0x3, 12), RK_GRF_VIRT +
172 RK3036_GRF_UOC1_CON4);
176 static void dwc_otg_uart_mode(void *pdata, int enter_usb_uart_mode)
181 static void usb20otg_power_enable(int enable)
184 /* disable otg_drv power */
185 if (gpio_is_valid(control_usb->otg_gpios->gpio))
186 gpio_set_value(control_usb->otg_gpios->gpio, 0);
187 } else if (1 == enable) {
188 /* enable otg_drv power */
189 if (gpio_is_valid(control_usb->otg_gpios->gpio))
190 gpio_set_value(control_usb->otg_gpios->gpio, 1);
194 struct dwc_otg_platform_data usb20otg_pdata_rk3036 = {
199 .hw_init = usb20otg_hw_init,
200 .phy_suspend = usb20otg_phy_suspend,
201 .soft_reset = usb20otg_soft_reset,
202 .clock_init = usb20otg_clock_init,
203 .clock_enable = usb20otg_clock_enable,
204 .get_status = usb20otg_get_status,
205 .power_enable = usb20otg_power_enable,
206 .dwc_otg_uart_mode = dwc_otg_uart_mode,
207 .bc_detect_cb = rk_battery_charger_detect_cb,
211 #ifdef CONFIG_USB20_HOST
212 static void usb20host_hw_init(void)
214 /* other haredware init,include:
215 * DRV_VBUS GPIO init */
216 if (gpio_is_valid(control_usb->host_gpios->gpio)) {
217 if (!gpio_get_value(control_usb->host_gpios->gpio))
218 gpio_set_value(control_usb->host_gpios->gpio, 1);
222 static void usb20host_phy_suspend(void *pdata, int suspend)
224 struct dwc_otg_platform_data *usbpdata = pdata;
227 /* enable soft control */
228 writel(UOC_HIWORD_UPDATE(0x1d5, 0x1ff, 0),
229 RK_GRF_VIRT + RK3036_GRF_UOC1_CON5);
230 usbpdata->phy_status = 1;
233 writel(UOC_HIWORD_UPDATE(0x0, 0x1, 0),
234 RK_GRF_VIRT + RK3036_GRF_UOC1_CON5);
235 usbpdata->phy_status = 0;
239 static void usb20host_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
241 struct dwc_otg_platform_data *usbpdata = pdata;
242 struct reset_control *rst_host_h, *rst_host_p, *rst_host_c;
244 rst_host_h = devm_reset_control_get(usbpdata->dev, "host_ahb");
245 rst_host_p = devm_reset_control_get(usbpdata->dev, "host_phy");
246 rst_host_c = devm_reset_control_get(usbpdata->dev, "host_controller");
247 if (IS_ERR(rst_host_h) || IS_ERR(rst_host_p) || IS_ERR(rst_host_c)) {
248 dev_err(usbpdata->dev, "Fail to get reset control from dts\n");
255 writel(UOC_HIWORD_UPDATE(0x1, 0x3, 0),
256 RK_GRF_VIRT + RK3036_GRF_UOC1_CON5);
257 reset_control_assert(rst_host_p);
259 writel(UOC_HIWORD_UPDATE(0x2, 0x3, 0),
260 RK_GRF_VIRT + RK3036_GRF_UOC1_CON5);
263 reset_control_deassert(rst_host_p);
265 /* Controller reset */
266 reset_control_assert(rst_host_c);
267 reset_control_assert(rst_host_h);
271 reset_control_deassert(rst_host_c);
272 reset_control_deassert(rst_host_h);
280 static void usb20host_clock_init(void *pdata)
282 struct dwc_otg_platform_data *usbpdata = pdata;
283 struct clk *ahbclk, *phyclk;
285 ahbclk = devm_clk_get(usbpdata->dev, "hclk_usb1");
286 if (IS_ERR(ahbclk)) {
287 dev_err(usbpdata->dev, "Failed to get hclk_usb1\n");
291 phyclk = devm_clk_get(usbpdata->dev, "clk_usbphy1");
292 if (IS_ERR(phyclk)) {
293 dev_err(usbpdata->dev, "Failed to get clk_usbphy1\n");
297 usbpdata->phyclk = phyclk;
298 usbpdata->ahbclk = ahbclk;
301 static void usb20host_clock_enable(void *pdata, int enable)
303 struct dwc_otg_platform_data *usbpdata = pdata;
306 clk_prepare_enable(usbpdata->ahbclk);
307 clk_prepare_enable(usbpdata->phyclk);
309 clk_disable_unprepare(usbpdata->ahbclk);
310 clk_disable_unprepare(usbpdata->phyclk);
314 static int usb20host_get_status(int id)
317 u32 soc_status0 = readl(RK_GRF_VIRT + RK3036_GRF_SOC_STATUS0);
320 case USB_STATUS_BVABLID:
322 ret = soc_status0 & (0x1 << 13);
324 case USB_STATUS_DPDM:
326 ret = soc_status0 & (0x3 << 14);
333 ret = control_usb->chip_id;
335 case USB_REMOTE_WAKEUP:
336 ret = control_usb->remote_wakeup;
339 ret = control_usb->usb_irq_wakeup;
347 static void usb20host_power_enable(int enable)
350 /* disable host_drv power */
351 /* do not disable power in default */
352 } else if (1 == enable) {
353 /* enable host_drv power */
354 if (gpio_is_valid(control_usb->host_gpios->gpio))
355 gpio_set_value(control_usb->host_gpios->gpio, 1);
359 struct dwc_otg_platform_data usb20host_pdata_rk3036 = {
364 .hw_init = usb20host_hw_init,
365 .phy_suspend = usb20host_phy_suspend,
366 .soft_reset = usb20host_soft_reset,
367 .clock_init = usb20host_clock_init,
368 .clock_enable = usb20host_clock_enable,
369 .get_status = usb20host_get_status,
370 .power_enable = usb20host_power_enable,
375 static const struct of_device_id rk_usb_control_id_table[] = {
377 .compatible = "rockchip,rk3036-usb-control",
382 /*********************************************************************
383 rk3036 usb detections
384 *********************************************************************/
386 #define WAKE_LOCK_TIMEOUT (HZ * 10)
387 static inline void do_wakeup(struct work_struct *work)
389 /* wake up the system */
390 rk_send_wakeup_key();
393 static void usb_battery_charger_detect_work(struct work_struct *work)
395 rk_battery_charger_detect_cb(usb_battery_charger_detect(1));
398 /********** handler for bvalid irq **********/
399 static irqreturn_t bvalid_irq_handler(int irq, void *dev_id)
402 writel(UOC_HIWORD_UPDATE(0x1, 0x1, 15),
403 RK_GRF_VIRT + RK3036_GRF_UOC0_CON5);
404 #ifdef CONFIG_RK_USB_UART
405 /* usb otg dp/dm switch to usb phy */
406 dwc_otg_uart_mode(NULL, PHY_USB_MODE);
409 if (control_usb->usb_irq_wakeup) {
410 wake_lock_timeout(&control_usb->usb_wakelock,
412 schedule_delayed_work(&control_usb->usb_det_wakeup_work,
416 schedule_delayed_work(&control_usb->usb_charger_det_work, HZ / 10);
421 /************* register usb detection irqs **************/
422 static int otg_irq_detect_init(struct platform_device *pdev)
427 if (control_usb->usb_irq_wakeup) {
428 wake_lock_init(&control_usb->usb_wakelock, WAKE_LOCK_SUSPEND,
430 INIT_DELAYED_WORK(&control_usb->usb_det_wakeup_work, do_wakeup);
433 /*register otg_bvalid irq */
434 irq = platform_get_irq_byname(pdev, "otg_bvalid");
435 if ((irq > 0) && control_usb->usb_irq_wakeup) {
436 ret = request_irq(irq, bvalid_irq_handler,
437 0, "otg_bvalid", NULL);
439 dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
441 /* enable bvalid irq */
442 writel(UOC_HIWORD_UPDATE(0x1, 0x1, 14),
443 RK_GRF_VIRT + RK3036_GRF_UOC0_CON5);
449 /********** end of rk3036 usb detections **********/
450 static int rk_usb_control_probe(struct platform_device *pdev)
453 struct device_node *np = pdev->dev.of_node;
457 devm_kzalloc(&pdev->dev, sizeof(*control_usb), GFP_KERNEL);
459 dev_err(&pdev->dev, "unable to alloc memory for control usb\n");
464 control_usb->chip_id = RK3036_USB_CTLR;
465 control_usb->remote_wakeup = of_property_read_bool(np,
466 "rockchip,remote_wakeup");
467 control_usb->usb_irq_wakeup = of_property_read_bool(np,
468 "rockchip,usb_irq_wakeup");
470 INIT_DELAYED_WORK(&control_usb->usb_charger_det_work,
471 usb_battery_charger_detect_work);
473 control_usb->host_gpios =
474 devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
475 if (!control_usb->host_gpios) {
476 dev_err(&pdev->dev, "unable to alloc memory for host_gpios\n");
481 gpio = of_get_named_gpio(np, "host_drv_gpio", 0);
482 control_usb->host_gpios->gpio = gpio;
484 if (!gpio_is_valid(gpio)) {
485 dev_err(&pdev->dev, "invalid host gpio%d\n", gpio);
487 err = devm_gpio_request(&pdev->dev, gpio, "host_drv_gpio");
490 "failed to request GPIO%d for host_drv\n",
495 gpio_direction_output(control_usb->host_gpios->gpio, 1);
498 control_usb->otg_gpios =
499 devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
500 if (!control_usb->otg_gpios) {
501 dev_err(&pdev->dev, "unable to alloc memory for otg_gpios\n");
506 gpio = of_get_named_gpio(np, "otg_drv_gpio", 0);
507 control_usb->otg_gpios->gpio = gpio;
509 if (!gpio_is_valid(gpio)) {
510 dev_err(&pdev->dev, "invalid otg gpio%d\n", gpio);
512 err = devm_gpio_request(&pdev->dev, gpio, "otg_drv_gpio");
515 "failed to request GPIO%d for otg_drv\n", gpio);
519 gpio_direction_output(control_usb->otg_gpios->gpio, 0);
526 static int rk_usb_control_remove(struct platform_device *pdev)
531 static struct platform_driver rk_usb_control_driver = {
532 .probe = rk_usb_control_probe,
533 .remove = rk_usb_control_remove,
535 .name = "rk3036-usb-control",
536 .owner = THIS_MODULE,
537 .of_match_table = of_match_ptr(rk_usb_control_id_table),
543 static const struct of_device_id dwc_otg_control_usb_id_table[] = {
545 .compatible = "rockchip,rk3036-dwc-control-usb",
551 static int dwc_otg_control_usb_probe(struct platform_device *pdev)
553 struct clk *hclk_usb_peri;
557 dev_err(&pdev->dev, "unable to alloc memory for control usb\n");
562 hclk_usb_peri = devm_clk_get(&pdev->dev, "hclk_usb_peri");
563 if (IS_ERR(hclk_usb_peri)) {
564 dev_err(&pdev->dev, "Failed to get hclk_usb_peri\n");
569 control_usb->hclk_usb_peri = hclk_usb_peri;
570 clk_prepare_enable(hclk_usb_peri);
572 #ifdef CONFIG_USB20_OTG
573 if (usb20otg_get_status(USB_STATUS_BVABLID))
574 schedule_delayed_work(&control_usb->usb_charger_det_work,
578 ret = otg_irq_detect_init(pdev);
585 clk_disable_unprepare(hclk_usb_peri);
590 static int dwc_otg_control_usb_remove(struct platform_device *pdev)
592 clk_disable_unprepare(control_usb->hclk_usb_peri);
596 static struct platform_driver dwc_otg_control_usb_driver = {
597 .probe = dwc_otg_control_usb_probe,
598 .remove = dwc_otg_control_usb_remove,
600 .name = "rk3036-dwc-control-usb",
601 .owner = THIS_MODULE,
602 .of_match_table = of_match_ptr(dwc_otg_control_usb_id_table),
606 static int __init dwc_otg_control_usb_init(void)
610 retval |= platform_driver_register(&rk_usb_control_driver);
611 retval |= platform_driver_register(&dwc_otg_control_usb_driver);
615 subsys_initcall(dwc_otg_control_usb_init);
617 static void __exit dwc_otg_control_usb_exit(void)
619 platform_driver_unregister(&rk_usb_control_driver);
620 platform_driver_unregister(&dwc_otg_control_usb_driver);
623 module_exit(dwc_otg_control_usb_exit);
624 MODULE_ALIAS("platform: dwc_control_usb");
625 MODULE_AUTHOR("RockChip Inc.");
626 MODULE_DESCRIPTION("RockChip Control Module USB Driver");
627 MODULE_LICENSE("GPL v2");