3 #include "usbdev_grf_regs.h"
4 #include "dwc_otg_regs.h"
5 static struct dwc_otg_control_usb *control_usb;
7 #ifdef CONFIG_USB20_OTG
8 static void usb20otg_hw_init(void)
10 /* other haredware init,include:
11 * DRV_VBUS GPIO init */
12 if (gpio_is_valid(control_usb->otg_gpios->gpio)) {
13 if (gpio_get_value(control_usb->otg_gpios->gpio))
14 gpio_set_value(control_usb->otg_gpios->gpio, 0);
18 static void usb20otg_phy_suspend(void *pdata, int suspend)
20 struct dwc_otg_platform_data *usbpdata = pdata;
23 /* enable soft control */
24 writel(UOC_HIWORD_UPDATE(0x1d1, 0x1ff, 0),
25 RK_GRF_VIRT + RK3036_GRF_UOC0_CON5);
26 usbpdata->phy_status = 1;
29 writel(UOC_HIWORD_UPDATE(0x0, 0x1, 0),
30 RK_GRF_VIRT + RK3036_GRF_UOC0_CON5);
31 usbpdata->phy_status = 0;
35 static void usb20otg_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
37 struct dwc_otg_platform_data *usbpdata = pdata;
38 struct reset_control *rst_otg_h, *rst_otg_p, *rst_otg_c;
40 rst_otg_h = devm_reset_control_get(usbpdata->dev, "otg_ahb");
41 rst_otg_p = devm_reset_control_get(usbpdata->dev, "otg_phy");
42 rst_otg_c = devm_reset_control_get(usbpdata->dev, "otg_controller");
43 if (IS_ERR(rst_otg_h) || IS_ERR(rst_otg_p) || IS_ERR(rst_otg_c)) {
44 dev_err(usbpdata->dev, "Fail to get reset control from dts\n");
51 writel(UOC_HIWORD_UPDATE(0x1, 0x3, 0),
52 RK_GRF_VIRT + RK3036_GRF_UOC0_CON5);
53 reset_control_assert(rst_otg_p);
55 writel(UOC_HIWORD_UPDATE(0x2, 0x3, 0),
56 RK_GRF_VIRT + RK3036_GRF_UOC0_CON5);
58 reset_control_deassert(rst_otg_p);
61 /* Controller reset */
62 reset_control_assert(rst_otg_c);
63 reset_control_assert(rst_otg_h);
67 reset_control_deassert(rst_otg_c);
68 reset_control_deassert(rst_otg_h);
76 static void usb20otg_clock_init(void *pdata)
78 struct dwc_otg_platform_data *usbpdata = pdata;
79 struct clk *ahbclk, *phyclk;
81 ahbclk = devm_clk_get(usbpdata->dev, "hclk_usb0");
83 dev_err(usbpdata->dev, "Failed to get hclk_usb0\n");
87 phyclk = devm_clk_get(usbpdata->dev, "clk_usbphy0");
89 dev_err(usbpdata->dev, "Failed to get clk_usbphy0\n");
93 usbpdata->phyclk = phyclk;
94 usbpdata->ahbclk = ahbclk;
97 static void usb20otg_clock_enable(void *pdata, int enable)
99 struct dwc_otg_platform_data *usbpdata = pdata;
102 clk_prepare_enable(usbpdata->ahbclk);
103 clk_prepare_enable(usbpdata->phyclk);
105 clk_disable_unprepare(usbpdata->ahbclk);
107 clk_disable_unprepare(usbpdata->phyclk);
112 static int usb20otg_get_status(int id)
115 u32 soc_status0 = readl(RK_GRF_VIRT + RK3036_GRF_SOC_STATUS0);
118 case USB_STATUS_BVABLID:
120 ret = soc_status0 & (0x1 << 8);
122 case USB_STATUS_DPDM:
124 ret = soc_status0 & (0x3 << 9);
128 ret = soc_status0 & (0x1 << 11);
131 ret = control_usb->chip_id;
133 case USB_REMOTE_WAKEUP:
134 ret = control_usb->remote_wakeup;
137 ret = control_usb->usb_irq_wakeup;
145 #ifdef CONFIG_RK_USB_UART
147 * dwc_otg_uart_enabled - check if a usb-uart bypass func is enabled in DT
149 * Returns true if the status property of node "usb_uart" is set to "okay"
150 * or "ok", if this property is absent it will use the default status "ok"
153 static bool dwc_otg_uart_enabled(void)
155 struct device_node *np;
157 np = of_find_node_by_name(NULL, "usb_uart");
158 if (np && of_device_is_available(np))
164 static void dwc_otg_uart_mode(void *pdata, int enter_usb_uart_mode)
166 if ((1 == enter_usb_uart_mode) && dwc_otg_uart_enabled()) {
167 /* bypass dm, enter uart mode */
168 writel(UOC_HIWORD_UPDATE(0x3, 0x3, 12), RK_GRF_VIRT +
169 RK3036_GRF_UOC1_CON4);
170 } else if (0 == enter_usb_uart_mode) {
172 writel(UOC_HIWORD_UPDATE(0x0, 0x3, 12), RK_GRF_VIRT +
173 RK3036_GRF_UOC1_CON4);
177 static void dwc_otg_uart_mode(void *pdata, int enter_usb_uart_mode)
182 static void usb20otg_power_enable(int enable)
185 /* disable otg_drv power */
186 if (gpio_is_valid(control_usb->otg_gpios->gpio))
187 gpio_set_value(control_usb->otg_gpios->gpio, 0);
188 else if (usb20otg_get_status(USB_STATUS_BVABLID))
189 rk_battery_charger_detect_cb(USB_OTG_POWER_OFF);
190 } else if (1 == enable) {
191 /* enable otg_drv power */
192 if (gpio_is_valid(control_usb->otg_gpios->gpio))
193 gpio_set_value(control_usb->otg_gpios->gpio, 1);
194 else if (!usb20otg_get_status(USB_STATUS_BVABLID))
195 rk_battery_charger_detect_cb(USB_OTG_POWER_ON);
198 static void usb20otg_phy_power_down(int power_down)
200 if (power_down == PHY_POWER_DOWN) {
201 if (control_usb->linestate_wakeup) {
202 /* enable otg0_linestate irq */
203 writel(UOC_HIWORD_UPDATE(0x3, 0x3, 12),
204 RK_GRF_VIRT + RK3036_GRF_UOC0_CON5);
205 /* enable otg1_linestate irq */
206 writel(UOC_HIWORD_UPDATE(0x3, 0x3, 14),
207 RK_GRF_VIRT + RK3036_GRF_UOC1_CON5);
209 } else if (power_down == PHY_POWER_UP) {
213 struct dwc_otg_platform_data usb20otg_pdata_rk3036 = {
218 .hw_init = usb20otg_hw_init,
219 .phy_suspend = usb20otg_phy_suspend,
220 .soft_reset = usb20otg_soft_reset,
221 .clock_init = usb20otg_clock_init,
222 .clock_enable = usb20otg_clock_enable,
223 .get_status = usb20otg_get_status,
224 .power_enable = usb20otg_power_enable,
225 .dwc_otg_uart_mode = dwc_otg_uart_mode,
226 .bc_detect_cb = rk_battery_charger_detect_cb,
227 .phy_power_down = usb20otg_phy_power_down,
231 #ifdef CONFIG_USB20_HOST
232 static void usb20host_hw_init(void)
234 /* other haredware init,include:
235 * DRV_VBUS GPIO init */
236 if (gpio_is_valid(control_usb->host_gpios->gpio)) {
237 if (!gpio_get_value(control_usb->host_gpios->gpio))
238 gpio_set_value(control_usb->host_gpios->gpio, 1);
242 static void usb20host_phy_suspend(void *pdata, int suspend)
244 struct dwc_otg_platform_data *usbpdata = pdata;
247 /* enable soft control */
248 writel(UOC_HIWORD_UPDATE(0x1d5, 0x1ff, 0),
249 RK_GRF_VIRT + RK3036_GRF_UOC1_CON5);
250 usbpdata->phy_status = 1;
253 writel(UOC_HIWORD_UPDATE(0x0, 0x1, 0),
254 RK_GRF_VIRT + RK3036_GRF_UOC1_CON5);
255 usbpdata->phy_status = 0;
259 static void usb20host_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
261 struct dwc_otg_platform_data *usbpdata = pdata;
262 struct reset_control *rst_host_h, *rst_host_p, *rst_host_c;
264 rst_host_h = devm_reset_control_get(usbpdata->dev, "host_ahb");
265 rst_host_p = devm_reset_control_get(usbpdata->dev, "host_phy");
266 rst_host_c = devm_reset_control_get(usbpdata->dev, "host_controller");
267 if (IS_ERR(rst_host_h) || IS_ERR(rst_host_p) || IS_ERR(rst_host_c)) {
268 dev_err(usbpdata->dev, "Fail to get reset control from dts\n");
275 writel(UOC_HIWORD_UPDATE(0x1, 0x3, 0),
276 RK_GRF_VIRT + RK3036_GRF_UOC1_CON5);
277 reset_control_assert(rst_host_p);
279 writel(UOC_HIWORD_UPDATE(0x2, 0x3, 0),
280 RK_GRF_VIRT + RK3036_GRF_UOC1_CON5);
283 reset_control_deassert(rst_host_p);
285 /* Controller reset */
286 reset_control_assert(rst_host_c);
287 reset_control_assert(rst_host_h);
291 reset_control_deassert(rst_host_c);
292 reset_control_deassert(rst_host_h);
300 static void usb20host_clock_init(void *pdata)
302 struct dwc_otg_platform_data *usbpdata = pdata;
303 struct clk *ahbclk, *phyclk;
305 ahbclk = devm_clk_get(usbpdata->dev, "hclk_usb1");
306 if (IS_ERR(ahbclk)) {
307 dev_err(usbpdata->dev, "Failed to get hclk_usb1\n");
311 phyclk = devm_clk_get(usbpdata->dev, "clk_usbphy1");
312 if (IS_ERR(phyclk)) {
313 dev_err(usbpdata->dev, "Failed to get clk_usbphy1\n");
317 usbpdata->phyclk = phyclk;
318 usbpdata->ahbclk = ahbclk;
321 static void usb20host_clock_enable(void *pdata, int enable)
323 struct dwc_otg_platform_data *usbpdata = pdata;
326 clk_prepare_enable(usbpdata->ahbclk);
327 clk_prepare_enable(usbpdata->phyclk);
329 clk_disable_unprepare(usbpdata->ahbclk);
330 clk_disable_unprepare(usbpdata->phyclk);
334 static int usb20host_get_status(int id)
337 u32 soc_status0 = readl(RK_GRF_VIRT + RK3036_GRF_SOC_STATUS0);
340 case USB_STATUS_BVABLID:
342 ret = soc_status0 & (0x1 << 13);
344 case USB_STATUS_DPDM:
346 ret = soc_status0 & (0x3 << 14);
353 ret = control_usb->chip_id;
355 case USB_REMOTE_WAKEUP:
356 ret = control_usb->remote_wakeup;
359 ret = control_usb->usb_irq_wakeup;
367 static void usb20host_power_enable(int enable)
370 /* disable host_drv power */
371 /* do not disable power in default */
372 } else if (1 == enable) {
373 /* enable host_drv power */
374 if (gpio_is_valid(control_usb->host_gpios->gpio))
375 gpio_set_value(control_usb->host_gpios->gpio, 1);
379 struct dwc_otg_platform_data usb20host_pdata_rk3036 = {
384 .hw_init = usb20host_hw_init,
385 .phy_suspend = usb20host_phy_suspend,
386 .soft_reset = usb20host_soft_reset,
387 .clock_init = usb20host_clock_init,
388 .clock_enable = usb20host_clock_enable,
389 .get_status = usb20host_get_status,
390 .power_enable = usb20host_power_enable,
395 static const struct of_device_id rk_usb_control_id_table[] = {
397 .compatible = "rockchip,rk3036-usb-control",
402 /*********************************************************************
403 rk3036 usb detections
404 *********************************************************************/
406 #define WAKE_LOCK_TIMEOUT (HZ * 10)
407 static inline void do_wakeup(struct work_struct *work)
409 /* wake up the system */
410 rk_send_wakeup_key();
413 static void usb_battery_charger_detect_work(struct work_struct *work)
415 rk_battery_charger_detect_cb(usb_battery_charger_detect(1));
418 /********** handler for bvalid irq **********/
419 static irqreturn_t bvalid_irq_handler(int irq, void *dev_id)
422 writel(UOC_HIWORD_UPDATE(0x1, 0x1, 15),
423 RK_GRF_VIRT + RK3036_GRF_UOC0_CON5);
424 #ifdef CONFIG_RK_USB_UART
425 /* usb otg dp/dm switch to usb phy */
426 dwc_otg_uart_mode(NULL, PHY_USB_MODE);
429 if (control_usb->usb_irq_wakeup) {
430 wake_lock_timeout(&control_usb->usb_wakelock,
432 schedule_delayed_work(&control_usb->usb_det_wakeup_work,
436 schedule_delayed_work(&control_usb->usb_charger_det_work, HZ / 10);
441 /********** Handler for linestate irq **********/
442 static irqreturn_t otg0_linestate_irq_handler(int irq, void *dev_id)
445 * Here is a chip hwrdware bug, when disable/enable
446 * linestate irq bit the state machine will not reset
447 * So here have to add a delay to wait the linestate filter
448 * timer run out (linestate filter time had been set to 100us)
452 /* clear and disable irq */
453 writel(UOC_HIWORD_UPDATE(0x2, 0x3, 12),
454 RK_GRF_VIRT + RK3036_GRF_UOC0_CON5);
457 if (control_usb->usb_irq_wakeup) {
458 wake_lock_timeout(&control_usb->usb_wakelock,
465 static irqreturn_t otg1_linestate_irq_handler(int irq, void *dev_id)
468 * Here is a chip hwrdware bug, when disable/enable
469 * linestate irq bit the state machine will not reset
470 * So here have to add a delay to wait the linestate filter
471 * timer run out (linestate filter time had been set to 100us)
475 /* clear and disable irq */
476 writel(UOC_HIWORD_UPDATE(0x2, 0x3, 14),
477 RK_GRF_VIRT + RK3036_GRF_UOC1_CON5);
480 if (control_usb->usb_irq_wakeup) {
481 wake_lock_timeout(&control_usb->usb_wakelock,
487 /************* register usb detection irqs **************/
488 static int otg_irq_detect_init(struct platform_device *pdev)
493 if (control_usb->usb_irq_wakeup) {
494 wake_lock_init(&control_usb->usb_wakelock, WAKE_LOCK_SUSPEND,
496 INIT_DELAYED_WORK(&control_usb->usb_det_wakeup_work, do_wakeup);
499 /*register otg_bvalid irq */
500 irq = platform_get_irq_byname(pdev, "otg_bvalid");
501 if ((irq > 0) && control_usb->usb_irq_wakeup) {
502 ret = request_irq(irq, bvalid_irq_handler,
503 0, "otg_bvalid", NULL);
505 dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
507 /* enable bvalid irq */
508 writel(UOC_HIWORD_UPDATE(0x1, 0x1, 14),
509 RK_GRF_VIRT + RK3036_GRF_UOC0_CON5);
513 if (!control_usb->linestate_wakeup)
516 /* Set otg0&1_linestate_filter time to 100us */
517 writel(UOC_HIWORD_UPDATE(0x0, 0xf, 6), RK_GRF_VIRT + 0x1a0);
519 /* Register otg0_linestate irq */
520 irq = platform_get_irq_byname(pdev, "otg0_linestate");
522 ret = request_irq(irq, otg0_linestate_irq_handler,
523 0, "otg0_linestate", NULL);
525 dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
527 /* Clear otg0_linestate irq */
528 writel(UOC_HIWORD_UPDATE(0x2, 0x3, 12),
529 RK_GRF_VIRT + RK3036_GRF_UOC0_CON5);
533 /* Register otg1_linestate irq */
534 irq = platform_get_irq_byname(pdev, "otg1_linestate");
536 ret = request_irq(irq, otg1_linestate_irq_handler,
537 0, "otg1_linestate", NULL);
539 dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
541 /* Clear otg1_linestate irq */
542 writel(UOC_HIWORD_UPDATE(0x2, 0x3, 14),
543 RK_GRF_VIRT + RK3036_GRF_UOC1_CON5);
549 /********** end of rk3036 usb detections **********/
550 static int rk_usb_control_probe(struct platform_device *pdev)
553 struct device_node *np = pdev->dev.of_node;
557 devm_kzalloc(&pdev->dev, sizeof(*control_usb), GFP_KERNEL);
559 dev_err(&pdev->dev, "unable to alloc memory for control usb\n");
564 control_usb->chip_id = RK3036_USB_CTLR;
565 control_usb->remote_wakeup = of_property_read_bool(np,
566 "rockchip,remote_wakeup");
567 control_usb->usb_irq_wakeup = of_property_read_bool(np,
568 "rockchip,usb_irq_wakeup");
569 control_usb->linestate_wakeup = of_property_read_bool(np,
570 "rockchip,linestate_wakeup");
572 INIT_DELAYED_WORK(&control_usb->usb_charger_det_work,
573 usb_battery_charger_detect_work);
575 control_usb->host_gpios =
576 devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
577 if (!control_usb->host_gpios) {
578 dev_err(&pdev->dev, "unable to alloc memory for host_gpios\n");
583 gpio = of_get_named_gpio(np, "host_drv_gpio", 0);
584 control_usb->host_gpios->gpio = gpio;
586 if (!gpio_is_valid(gpio)) {
587 dev_err(&pdev->dev, "invalid host gpio%d\n", gpio);
589 err = devm_gpio_request(&pdev->dev, gpio, "host_drv_gpio");
592 "failed to request GPIO%d for host_drv\n",
597 gpio_direction_output(control_usb->host_gpios->gpio, 1);
600 control_usb->otg_gpios =
601 devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
602 if (!control_usb->otg_gpios) {
603 dev_err(&pdev->dev, "unable to alloc memory for otg_gpios\n");
608 gpio = of_get_named_gpio(np, "otg_drv_gpio", 0);
609 control_usb->otg_gpios->gpio = gpio;
611 if (!gpio_is_valid(gpio)) {
612 dev_err(&pdev->dev, "invalid otg gpio%d\n", gpio);
614 err = devm_gpio_request(&pdev->dev, gpio, "otg_drv_gpio");
617 "failed to request GPIO%d for otg_drv\n", gpio);
621 gpio_direction_output(control_usb->otg_gpios->gpio, 0);
628 static int rk_usb_control_remove(struct platform_device *pdev)
633 static struct platform_driver rk_usb_control_driver = {
634 .probe = rk_usb_control_probe,
635 .remove = rk_usb_control_remove,
637 .name = "rk3036-usb-control",
638 .owner = THIS_MODULE,
639 .of_match_table = of_match_ptr(rk_usb_control_id_table),
645 static const struct of_device_id dwc_otg_control_usb_id_table[] = {
647 .compatible = "rockchip,rk3036-dwc-control-usb",
653 static int dwc_otg_control_usb_probe(struct platform_device *pdev)
655 struct clk *hclk_usb_peri;
659 dev_err(&pdev->dev, "unable to alloc memory for control usb\n");
664 hclk_usb_peri = devm_clk_get(&pdev->dev, "hclk_usb_peri");
665 if (IS_ERR(hclk_usb_peri)) {
666 dev_err(&pdev->dev, "Failed to get hclk_usb_peri\n");
671 control_usb->hclk_usb_peri = hclk_usb_peri;
672 clk_prepare_enable(hclk_usb_peri);
674 #ifdef CONFIG_USB20_OTG
675 if (usb20otg_get_status(USB_STATUS_BVABLID))
676 schedule_delayed_work(&control_usb->usb_charger_det_work,
680 ret = otg_irq_detect_init(pdev);
687 clk_disable_unprepare(hclk_usb_peri);
692 static int dwc_otg_control_usb_remove(struct platform_device *pdev)
694 clk_disable_unprepare(control_usb->hclk_usb_peri);
698 static struct platform_driver dwc_otg_control_usb_driver = {
699 .probe = dwc_otg_control_usb_probe,
700 .remove = dwc_otg_control_usb_remove,
702 .name = "rk3036-dwc-control-usb",
703 .owner = THIS_MODULE,
704 .of_match_table = of_match_ptr(dwc_otg_control_usb_id_table),
708 static int __init dwc_otg_control_usb_init(void)
712 retval |= platform_driver_register(&rk_usb_control_driver);
713 retval |= platform_driver_register(&dwc_otg_control_usb_driver);
717 subsys_initcall(dwc_otg_control_usb_init);
719 static void __exit dwc_otg_control_usb_exit(void)
721 platform_driver_unregister(&rk_usb_control_driver);
722 platform_driver_unregister(&dwc_otg_control_usb_driver);
725 module_exit(dwc_otg_control_usb_exit);
726 MODULE_ALIAS("platform: dwc_control_usb");
727 MODULE_AUTHOR("RockChip Inc.");
728 MODULE_DESCRIPTION("RockChip Control Module USB Driver");
729 MODULE_LICENSE("GPL v2");