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(0x1d1, 0x1ff, 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);
193 static void usb20otg_phy_power_down(int power_down)
195 if (power_down == PHY_POWER_DOWN) {
196 if (control_usb->linestate_wakeup) {
197 /* enable otg0_linestate irq */
198 writel(UOC_HIWORD_UPDATE(0x3, 0x3, 12),
199 RK_GRF_VIRT + RK3036_GRF_UOC0_CON5);
200 /* enable otg1_linestate irq */
201 writel(UOC_HIWORD_UPDATE(0x3, 0x3, 14),
202 RK_GRF_VIRT + RK3036_GRF_UOC1_CON5);
204 } else if (power_down == PHY_POWER_UP) {
208 struct dwc_otg_platform_data usb20otg_pdata_rk3036 = {
213 .hw_init = usb20otg_hw_init,
214 .phy_suspend = usb20otg_phy_suspend,
215 .soft_reset = usb20otg_soft_reset,
216 .clock_init = usb20otg_clock_init,
217 .clock_enable = usb20otg_clock_enable,
218 .get_status = usb20otg_get_status,
219 .power_enable = usb20otg_power_enable,
220 .dwc_otg_uart_mode = dwc_otg_uart_mode,
221 .bc_detect_cb = rk_battery_charger_detect_cb,
222 .phy_power_down = usb20otg_phy_power_down,
226 #ifdef CONFIG_USB20_HOST
227 static void usb20host_hw_init(void)
229 /* other haredware init,include:
230 * DRV_VBUS GPIO init */
231 if (gpio_is_valid(control_usb->host_gpios->gpio)) {
232 if (!gpio_get_value(control_usb->host_gpios->gpio))
233 gpio_set_value(control_usb->host_gpios->gpio, 1);
237 static void usb20host_phy_suspend(void *pdata, int suspend)
239 struct dwc_otg_platform_data *usbpdata = pdata;
242 /* enable soft control */
243 writel(UOC_HIWORD_UPDATE(0x1d5, 0x1ff, 0),
244 RK_GRF_VIRT + RK3036_GRF_UOC1_CON5);
245 usbpdata->phy_status = 1;
248 writel(UOC_HIWORD_UPDATE(0x0, 0x1, 0),
249 RK_GRF_VIRT + RK3036_GRF_UOC1_CON5);
250 usbpdata->phy_status = 0;
254 static void usb20host_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
256 struct dwc_otg_platform_data *usbpdata = pdata;
257 struct reset_control *rst_host_h, *rst_host_p, *rst_host_c;
259 rst_host_h = devm_reset_control_get(usbpdata->dev, "host_ahb");
260 rst_host_p = devm_reset_control_get(usbpdata->dev, "host_phy");
261 rst_host_c = devm_reset_control_get(usbpdata->dev, "host_controller");
262 if (IS_ERR(rst_host_h) || IS_ERR(rst_host_p) || IS_ERR(rst_host_c)) {
263 dev_err(usbpdata->dev, "Fail to get reset control from dts\n");
270 writel(UOC_HIWORD_UPDATE(0x1, 0x3, 0),
271 RK_GRF_VIRT + RK3036_GRF_UOC1_CON5);
272 reset_control_assert(rst_host_p);
274 writel(UOC_HIWORD_UPDATE(0x2, 0x3, 0),
275 RK_GRF_VIRT + RK3036_GRF_UOC1_CON5);
278 reset_control_deassert(rst_host_p);
280 /* Controller reset */
281 reset_control_assert(rst_host_c);
282 reset_control_assert(rst_host_h);
286 reset_control_deassert(rst_host_c);
287 reset_control_deassert(rst_host_h);
295 static void usb20host_clock_init(void *pdata)
297 struct dwc_otg_platform_data *usbpdata = pdata;
298 struct clk *ahbclk, *phyclk;
300 ahbclk = devm_clk_get(usbpdata->dev, "hclk_usb1");
301 if (IS_ERR(ahbclk)) {
302 dev_err(usbpdata->dev, "Failed to get hclk_usb1\n");
306 phyclk = devm_clk_get(usbpdata->dev, "clk_usbphy1");
307 if (IS_ERR(phyclk)) {
308 dev_err(usbpdata->dev, "Failed to get clk_usbphy1\n");
312 usbpdata->phyclk = phyclk;
313 usbpdata->ahbclk = ahbclk;
316 static void usb20host_clock_enable(void *pdata, int enable)
318 struct dwc_otg_platform_data *usbpdata = pdata;
321 clk_prepare_enable(usbpdata->ahbclk);
322 clk_prepare_enable(usbpdata->phyclk);
324 clk_disable_unprepare(usbpdata->ahbclk);
325 clk_disable_unprepare(usbpdata->phyclk);
329 static int usb20host_get_status(int id)
332 u32 soc_status0 = readl(RK_GRF_VIRT + RK3036_GRF_SOC_STATUS0);
335 case USB_STATUS_BVABLID:
337 ret = soc_status0 & (0x1 << 13);
339 case USB_STATUS_DPDM:
341 ret = soc_status0 & (0x3 << 14);
348 ret = control_usb->chip_id;
350 case USB_REMOTE_WAKEUP:
351 ret = control_usb->remote_wakeup;
354 ret = control_usb->usb_irq_wakeup;
362 static void usb20host_power_enable(int enable)
365 /* disable host_drv power */
366 /* do not disable power in default */
367 } else if (1 == enable) {
368 /* enable host_drv power */
369 if (gpio_is_valid(control_usb->host_gpios->gpio))
370 gpio_set_value(control_usb->host_gpios->gpio, 1);
374 struct dwc_otg_platform_data usb20host_pdata_rk3036 = {
379 .hw_init = usb20host_hw_init,
380 .phy_suspend = usb20host_phy_suspend,
381 .soft_reset = usb20host_soft_reset,
382 .clock_init = usb20host_clock_init,
383 .clock_enable = usb20host_clock_enable,
384 .get_status = usb20host_get_status,
385 .power_enable = usb20host_power_enable,
390 static const struct of_device_id rk_usb_control_id_table[] = {
392 .compatible = "rockchip,rk3036-usb-control",
397 /*********************************************************************
398 rk3036 usb detections
399 *********************************************************************/
401 #define WAKE_LOCK_TIMEOUT (HZ * 10)
402 static inline void do_wakeup(struct work_struct *work)
404 /* wake up the system */
405 rk_send_wakeup_key();
408 static void usb_battery_charger_detect_work(struct work_struct *work)
410 rk_battery_charger_detect_cb(usb_battery_charger_detect(1));
413 /********** handler for bvalid irq **********/
414 static irqreturn_t bvalid_irq_handler(int irq, void *dev_id)
417 writel(UOC_HIWORD_UPDATE(0x1, 0x1, 15),
418 RK_GRF_VIRT + RK3036_GRF_UOC0_CON5);
419 #ifdef CONFIG_RK_USB_UART
420 /* usb otg dp/dm switch to usb phy */
421 dwc_otg_uart_mode(NULL, PHY_USB_MODE);
424 if (control_usb->usb_irq_wakeup) {
425 wake_lock_timeout(&control_usb->usb_wakelock,
427 schedule_delayed_work(&control_usb->usb_det_wakeup_work,
431 schedule_delayed_work(&control_usb->usb_charger_det_work, HZ / 10);
436 /********** Handler for linestate irq **********/
437 static irqreturn_t otg0_linestate_irq_handler(int irq, void *dev_id)
440 * Here is a chip hwrdware bug, when disable/enable
441 * linestate irq bit the state machine will not reset
442 * So here have to add a delay to wait the linestate filter
443 * timer run out (linestate filter time had been set to 100us)
447 /* clear and disable irq */
448 writel(UOC_HIWORD_UPDATE(0x2, 0x3, 12),
449 RK_GRF_VIRT + RK3036_GRF_UOC0_CON5);
452 if (control_usb->usb_irq_wakeup) {
453 wake_lock_timeout(&control_usb->usb_wakelock,
460 static irqreturn_t otg1_linestate_irq_handler(int irq, void *dev_id)
463 * Here is a chip hwrdware bug, when disable/enable
464 * linestate irq bit the state machine will not reset
465 * So here have to add a delay to wait the linestate filter
466 * timer run out (linestate filter time had been set to 100us)
470 /* clear and disable irq */
471 writel(UOC_HIWORD_UPDATE(0x2, 0x3, 14),
472 RK_GRF_VIRT + RK3036_GRF_UOC1_CON5);
475 if (control_usb->usb_irq_wakeup) {
476 wake_lock_timeout(&control_usb->usb_wakelock,
482 /************* register usb detection irqs **************/
483 static int otg_irq_detect_init(struct platform_device *pdev)
488 if (control_usb->usb_irq_wakeup) {
489 wake_lock_init(&control_usb->usb_wakelock, WAKE_LOCK_SUSPEND,
491 INIT_DELAYED_WORK(&control_usb->usb_det_wakeup_work, do_wakeup);
494 /*register otg_bvalid irq */
495 irq = platform_get_irq_byname(pdev, "otg_bvalid");
496 if ((irq > 0) && control_usb->usb_irq_wakeup) {
497 ret = request_irq(irq, bvalid_irq_handler,
498 0, "otg_bvalid", NULL);
500 dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
502 /* enable bvalid irq */
503 writel(UOC_HIWORD_UPDATE(0x1, 0x1, 14),
504 RK_GRF_VIRT + RK3036_GRF_UOC0_CON5);
508 if (!control_usb->linestate_wakeup)
511 /* Set otg0&1_linestate_filter time to 100us */
512 writel(UOC_HIWORD_UPDATE(0x0, 0xf, 6), RK_GRF_VIRT + 0x1a0);
514 /* Register otg0_linestate irq */
515 irq = platform_get_irq_byname(pdev, "otg0_linestate");
517 ret = request_irq(irq, otg0_linestate_irq_handler,
518 0, "otg0_linestate", NULL);
520 dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
522 /* Clear otg0_linestate irq */
523 writel(UOC_HIWORD_UPDATE(0x2, 0x3, 12),
524 RK_GRF_VIRT + RK3036_GRF_UOC0_CON5);
528 /* Register otg1_linestate irq */
529 irq = platform_get_irq_byname(pdev, "otg1_linestate");
531 ret = request_irq(irq, otg1_linestate_irq_handler,
532 0, "otg1_linestate", NULL);
534 dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
536 /* Clear otg1_linestate irq */
537 writel(UOC_HIWORD_UPDATE(0x2, 0x3, 14),
538 RK_GRF_VIRT + RK3036_GRF_UOC1_CON5);
544 /********** end of rk3036 usb detections **********/
545 static int rk_usb_control_probe(struct platform_device *pdev)
548 struct device_node *np = pdev->dev.of_node;
552 devm_kzalloc(&pdev->dev, sizeof(*control_usb), GFP_KERNEL);
554 dev_err(&pdev->dev, "unable to alloc memory for control usb\n");
559 control_usb->chip_id = RK3036_USB_CTLR;
560 control_usb->remote_wakeup = of_property_read_bool(np,
561 "rockchip,remote_wakeup");
562 control_usb->usb_irq_wakeup = of_property_read_bool(np,
563 "rockchip,usb_irq_wakeup");
564 control_usb->linestate_wakeup = of_property_read_bool(np,
565 "rockchip,linestate_wakeup");
567 INIT_DELAYED_WORK(&control_usb->usb_charger_det_work,
568 usb_battery_charger_detect_work);
570 control_usb->host_gpios =
571 devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
572 if (!control_usb->host_gpios) {
573 dev_err(&pdev->dev, "unable to alloc memory for host_gpios\n");
578 gpio = of_get_named_gpio(np, "host_drv_gpio", 0);
579 control_usb->host_gpios->gpio = gpio;
581 if (!gpio_is_valid(gpio)) {
582 dev_err(&pdev->dev, "invalid host gpio%d\n", gpio);
584 err = devm_gpio_request(&pdev->dev, gpio, "host_drv_gpio");
587 "failed to request GPIO%d for host_drv\n",
592 gpio_direction_output(control_usb->host_gpios->gpio, 1);
595 control_usb->otg_gpios =
596 devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
597 if (!control_usb->otg_gpios) {
598 dev_err(&pdev->dev, "unable to alloc memory for otg_gpios\n");
603 gpio = of_get_named_gpio(np, "otg_drv_gpio", 0);
604 control_usb->otg_gpios->gpio = gpio;
606 if (!gpio_is_valid(gpio)) {
607 dev_err(&pdev->dev, "invalid otg gpio%d\n", gpio);
609 err = devm_gpio_request(&pdev->dev, gpio, "otg_drv_gpio");
612 "failed to request GPIO%d for otg_drv\n", gpio);
616 gpio_direction_output(control_usb->otg_gpios->gpio, 0);
623 static int rk_usb_control_remove(struct platform_device *pdev)
628 static struct platform_driver rk_usb_control_driver = {
629 .probe = rk_usb_control_probe,
630 .remove = rk_usb_control_remove,
632 .name = "rk3036-usb-control",
633 .owner = THIS_MODULE,
634 .of_match_table = of_match_ptr(rk_usb_control_id_table),
640 static const struct of_device_id dwc_otg_control_usb_id_table[] = {
642 .compatible = "rockchip,rk3036-dwc-control-usb",
648 static int dwc_otg_control_usb_probe(struct platform_device *pdev)
650 struct clk *hclk_usb_peri;
654 dev_err(&pdev->dev, "unable to alloc memory for control usb\n");
659 hclk_usb_peri = devm_clk_get(&pdev->dev, "hclk_usb_peri");
660 if (IS_ERR(hclk_usb_peri)) {
661 dev_err(&pdev->dev, "Failed to get hclk_usb_peri\n");
666 control_usb->hclk_usb_peri = hclk_usb_peri;
667 clk_prepare_enable(hclk_usb_peri);
669 #ifdef CONFIG_USB20_OTG
670 if (usb20otg_get_status(USB_STATUS_BVABLID))
671 schedule_delayed_work(&control_usb->usb_charger_det_work,
675 ret = otg_irq_detect_init(pdev);
682 clk_disable_unprepare(hclk_usb_peri);
687 static int dwc_otg_control_usb_remove(struct platform_device *pdev)
689 clk_disable_unprepare(control_usb->hclk_usb_peri);
693 static struct platform_driver dwc_otg_control_usb_driver = {
694 .probe = dwc_otg_control_usb_probe,
695 .remove = dwc_otg_control_usb_remove,
697 .name = "rk3036-dwc-control-usb",
698 .owner = THIS_MODULE,
699 .of_match_table = of_match_ptr(dwc_otg_control_usb_id_table),
703 static int __init dwc_otg_control_usb_init(void)
707 retval |= platform_driver_register(&rk_usb_control_driver);
708 retval |= platform_driver_register(&dwc_otg_control_usb_driver);
712 subsys_initcall(dwc_otg_control_usb_init);
714 static void __exit dwc_otg_control_usb_exit(void)
716 platform_driver_unregister(&rk_usb_control_driver);
717 platform_driver_unregister(&dwc_otg_control_usb_driver);
720 module_exit(dwc_otg_control_usb_exit);
721 MODULE_ALIAS("platform: dwc_control_usb");
722 MODULE_AUTHOR("RockChip Inc.");
723 MODULE_DESCRIPTION("RockChip Control Module USB Driver");
724 MODULE_LICENSE("GPL v2");