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 rk_battery_charger_detect_cb(USB_OTG_POWER_OFF);
186 /* disable otg_drv power */
187 if (gpio_is_valid(control_usb->otg_gpios->gpio))
188 gpio_set_value(control_usb->otg_gpios->gpio, 0);
189 } else if (1 == enable) {
190 rk_battery_charger_detect_cb(USB_OTG_POWER_ON);
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);
196 static void usb20otg_phy_power_down(int power_down)
198 if (power_down == PHY_POWER_DOWN) {
199 if (control_usb->linestate_wakeup) {
200 /* enable otg0_linestate irq */
201 writel(UOC_HIWORD_UPDATE(0x3, 0x3, 12),
202 RK_GRF_VIRT + RK3036_GRF_UOC0_CON5);
203 /* enable otg1_linestate irq */
204 writel(UOC_HIWORD_UPDATE(0x3, 0x3, 14),
205 RK_GRF_VIRT + RK3036_GRF_UOC1_CON5);
207 } else if (power_down == PHY_POWER_UP) {
211 struct dwc_otg_platform_data usb20otg_pdata_rk3036 = {
216 .hw_init = usb20otg_hw_init,
217 .phy_suspend = usb20otg_phy_suspend,
218 .soft_reset = usb20otg_soft_reset,
219 .clock_init = usb20otg_clock_init,
220 .clock_enable = usb20otg_clock_enable,
221 .get_status = usb20otg_get_status,
222 .power_enable = usb20otg_power_enable,
223 .dwc_otg_uart_mode = dwc_otg_uart_mode,
224 .bc_detect_cb = rk_battery_charger_detect_cb,
225 .phy_power_down = usb20otg_phy_power_down,
229 #ifdef CONFIG_USB20_HOST
230 static void usb20host_hw_init(void)
232 /* other haredware init,include:
233 * DRV_VBUS GPIO init */
234 if (gpio_is_valid(control_usb->host_gpios->gpio)) {
235 if (!gpio_get_value(control_usb->host_gpios->gpio))
236 gpio_set_value(control_usb->host_gpios->gpio, 1);
240 static void usb20host_phy_suspend(void *pdata, int suspend)
242 struct dwc_otg_platform_data *usbpdata = pdata;
245 /* enable soft control */
246 writel(UOC_HIWORD_UPDATE(0x1d5, 0x1ff, 0),
247 RK_GRF_VIRT + RK3036_GRF_UOC1_CON5);
248 usbpdata->phy_status = 1;
251 writel(UOC_HIWORD_UPDATE(0x0, 0x1, 0),
252 RK_GRF_VIRT + RK3036_GRF_UOC1_CON5);
253 usbpdata->phy_status = 0;
257 static void usb20host_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
259 struct dwc_otg_platform_data *usbpdata = pdata;
260 struct reset_control *rst_host_h, *rst_host_p, *rst_host_c;
262 rst_host_h = devm_reset_control_get(usbpdata->dev, "host_ahb");
263 rst_host_p = devm_reset_control_get(usbpdata->dev, "host_phy");
264 rst_host_c = devm_reset_control_get(usbpdata->dev, "host_controller");
265 if (IS_ERR(rst_host_h) || IS_ERR(rst_host_p) || IS_ERR(rst_host_c)) {
266 dev_err(usbpdata->dev, "Fail to get reset control from dts\n");
273 writel(UOC_HIWORD_UPDATE(0x1, 0x3, 0),
274 RK_GRF_VIRT + RK3036_GRF_UOC1_CON5);
275 reset_control_assert(rst_host_p);
277 writel(UOC_HIWORD_UPDATE(0x2, 0x3, 0),
278 RK_GRF_VIRT + RK3036_GRF_UOC1_CON5);
281 reset_control_deassert(rst_host_p);
283 /* Controller reset */
284 reset_control_assert(rst_host_c);
285 reset_control_assert(rst_host_h);
289 reset_control_deassert(rst_host_c);
290 reset_control_deassert(rst_host_h);
298 static void usb20host_clock_init(void *pdata)
300 struct dwc_otg_platform_data *usbpdata = pdata;
301 struct clk *ahbclk, *phyclk;
303 ahbclk = devm_clk_get(usbpdata->dev, "hclk_usb1");
304 if (IS_ERR(ahbclk)) {
305 dev_err(usbpdata->dev, "Failed to get hclk_usb1\n");
309 phyclk = devm_clk_get(usbpdata->dev, "clk_usbphy1");
310 if (IS_ERR(phyclk)) {
311 dev_err(usbpdata->dev, "Failed to get clk_usbphy1\n");
315 usbpdata->phyclk = phyclk;
316 usbpdata->ahbclk = ahbclk;
319 static void usb20host_clock_enable(void *pdata, int enable)
321 struct dwc_otg_platform_data *usbpdata = pdata;
324 clk_prepare_enable(usbpdata->ahbclk);
325 clk_prepare_enable(usbpdata->phyclk);
327 clk_disable_unprepare(usbpdata->ahbclk);
328 clk_disable_unprepare(usbpdata->phyclk);
332 static int usb20host_get_status(int id)
335 u32 soc_status0 = readl(RK_GRF_VIRT + RK3036_GRF_SOC_STATUS0);
338 case USB_STATUS_BVABLID:
340 ret = soc_status0 & (0x1 << 13);
342 case USB_STATUS_DPDM:
344 ret = soc_status0 & (0x3 << 14);
351 ret = control_usb->chip_id;
353 case USB_REMOTE_WAKEUP:
354 ret = control_usb->remote_wakeup;
357 ret = control_usb->usb_irq_wakeup;
365 static void usb20host_power_enable(int enable)
368 /* disable host_drv power */
369 /* do not disable power in default */
370 } else if (1 == enable) {
371 /* enable host_drv power */
372 if (gpio_is_valid(control_usb->host_gpios->gpio))
373 gpio_set_value(control_usb->host_gpios->gpio, 1);
377 struct dwc_otg_platform_data usb20host_pdata_rk3036 = {
382 .hw_init = usb20host_hw_init,
383 .phy_suspend = usb20host_phy_suspend,
384 .soft_reset = usb20host_soft_reset,
385 .clock_init = usb20host_clock_init,
386 .clock_enable = usb20host_clock_enable,
387 .get_status = usb20host_get_status,
388 .power_enable = usb20host_power_enable,
393 static const struct of_device_id rk_usb_control_id_table[] = {
395 .compatible = "rockchip,rk3036-usb-control",
400 /*********************************************************************
401 rk3036 usb detections
402 *********************************************************************/
404 #define WAKE_LOCK_TIMEOUT (HZ * 10)
405 static inline void do_wakeup(struct work_struct *work)
407 /* wake up the system */
408 rk_send_wakeup_key();
411 static void usb_battery_charger_detect_work(struct work_struct *work)
413 rk_battery_charger_detect_cb(usb_battery_charger_detect(1));
416 /********** handler for bvalid irq **********/
417 static irqreturn_t bvalid_irq_handler(int irq, void *dev_id)
420 writel(UOC_HIWORD_UPDATE(0x1, 0x1, 15),
421 RK_GRF_VIRT + RK3036_GRF_UOC0_CON5);
422 #ifdef CONFIG_RK_USB_UART
423 /* usb otg dp/dm switch to usb phy */
424 dwc_otg_uart_mode(NULL, PHY_USB_MODE);
427 if (control_usb->usb_irq_wakeup) {
428 wake_lock_timeout(&control_usb->usb_wakelock,
430 schedule_delayed_work(&control_usb->usb_det_wakeup_work,
434 schedule_delayed_work(&control_usb->usb_charger_det_work, HZ / 10);
439 /********** Handler for linestate irq **********/
440 static irqreturn_t otg0_linestate_irq_handler(int irq, void *dev_id)
443 * Here is a chip hwrdware bug, when disable/enable
444 * linestate irq bit the state machine will not reset
445 * So here have to add a delay to wait the linestate filter
446 * timer run out (linestate filter time had been set to 100us)
450 /* clear and disable irq */
451 writel(UOC_HIWORD_UPDATE(0x2, 0x3, 12),
452 RK_GRF_VIRT + RK3036_GRF_UOC0_CON5);
455 if (control_usb->usb_irq_wakeup) {
456 wake_lock_timeout(&control_usb->usb_wakelock,
463 static irqreturn_t otg1_linestate_irq_handler(int irq, void *dev_id)
466 * Here is a chip hwrdware bug, when disable/enable
467 * linestate irq bit the state machine will not reset
468 * So here have to add a delay to wait the linestate filter
469 * timer run out (linestate filter time had been set to 100us)
473 /* clear and disable irq */
474 writel(UOC_HIWORD_UPDATE(0x2, 0x3, 14),
475 RK_GRF_VIRT + RK3036_GRF_UOC1_CON5);
478 if (control_usb->usb_irq_wakeup) {
479 wake_lock_timeout(&control_usb->usb_wakelock,
485 /************* register usb detection irqs **************/
486 static int otg_irq_detect_init(struct platform_device *pdev)
491 if (control_usb->usb_irq_wakeup) {
492 wake_lock_init(&control_usb->usb_wakelock, WAKE_LOCK_SUSPEND,
494 INIT_DELAYED_WORK(&control_usb->usb_det_wakeup_work, do_wakeup);
497 /*register otg_bvalid irq */
498 irq = platform_get_irq_byname(pdev, "otg_bvalid");
499 if ((irq > 0) && control_usb->usb_irq_wakeup) {
500 ret = request_irq(irq, bvalid_irq_handler,
501 0, "otg_bvalid", NULL);
503 dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
505 /* enable bvalid irq */
506 writel(UOC_HIWORD_UPDATE(0x1, 0x1, 14),
507 RK_GRF_VIRT + RK3036_GRF_UOC0_CON5);
511 if (!control_usb->linestate_wakeup)
514 /* Set otg0&1_linestate_filter time to 100us */
515 writel(UOC_HIWORD_UPDATE(0x0, 0xf, 6), RK_GRF_VIRT + 0x1a0);
517 /* Register otg0_linestate irq */
518 irq = platform_get_irq_byname(pdev, "otg0_linestate");
520 ret = request_irq(irq, otg0_linestate_irq_handler,
521 0, "otg0_linestate", NULL);
523 dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
525 /* Clear otg0_linestate irq */
526 writel(UOC_HIWORD_UPDATE(0x2, 0x3, 12),
527 RK_GRF_VIRT + RK3036_GRF_UOC0_CON5);
531 /* Register otg1_linestate irq */
532 irq = platform_get_irq_byname(pdev, "otg1_linestate");
534 ret = request_irq(irq, otg1_linestate_irq_handler,
535 0, "otg1_linestate", NULL);
537 dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
539 /* Clear otg1_linestate irq */
540 writel(UOC_HIWORD_UPDATE(0x2, 0x3, 14),
541 RK_GRF_VIRT + RK3036_GRF_UOC1_CON5);
547 /********** end of rk3036 usb detections **********/
548 static int rk_usb_control_probe(struct platform_device *pdev)
551 struct device_node *np = pdev->dev.of_node;
555 devm_kzalloc(&pdev->dev, sizeof(*control_usb), GFP_KERNEL);
557 dev_err(&pdev->dev, "unable to alloc memory for control usb\n");
562 control_usb->chip_id = RK3036_USB_CTLR;
563 control_usb->remote_wakeup = of_property_read_bool(np,
564 "rockchip,remote_wakeup");
565 control_usb->usb_irq_wakeup = of_property_read_bool(np,
566 "rockchip,usb_irq_wakeup");
567 control_usb->linestate_wakeup = of_property_read_bool(np,
568 "rockchip,linestate_wakeup");
570 INIT_DELAYED_WORK(&control_usb->usb_charger_det_work,
571 usb_battery_charger_detect_work);
573 control_usb->host_gpios =
574 devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
575 if (!control_usb->host_gpios) {
576 dev_err(&pdev->dev, "unable to alloc memory for host_gpios\n");
581 gpio = of_get_named_gpio(np, "host_drv_gpio", 0);
582 control_usb->host_gpios->gpio = gpio;
584 if (!gpio_is_valid(gpio)) {
585 dev_err(&pdev->dev, "invalid host gpio%d\n", gpio);
587 err = devm_gpio_request(&pdev->dev, gpio, "host_drv_gpio");
590 "failed to request GPIO%d for host_drv\n",
595 gpio_direction_output(control_usb->host_gpios->gpio, 1);
598 control_usb->otg_gpios =
599 devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
600 if (!control_usb->otg_gpios) {
601 dev_err(&pdev->dev, "unable to alloc memory for otg_gpios\n");
606 gpio = of_get_named_gpio(np, "otg_drv_gpio", 0);
607 control_usb->otg_gpios->gpio = gpio;
609 if (!gpio_is_valid(gpio)) {
610 dev_err(&pdev->dev, "invalid otg gpio%d\n", gpio);
612 err = devm_gpio_request(&pdev->dev, gpio, "otg_drv_gpio");
615 "failed to request GPIO%d for otg_drv\n", gpio);
619 gpio_direction_output(control_usb->otg_gpios->gpio, 0);
626 static int rk_usb_control_remove(struct platform_device *pdev)
631 static struct platform_driver rk_usb_control_driver = {
632 .probe = rk_usb_control_probe,
633 .remove = rk_usb_control_remove,
635 .name = "rk3036-usb-control",
636 .owner = THIS_MODULE,
637 .of_match_table = of_match_ptr(rk_usb_control_id_table),
643 static const struct of_device_id dwc_otg_control_usb_id_table[] = {
645 .compatible = "rockchip,rk3036-dwc-control-usb",
651 static int dwc_otg_control_usb_probe(struct platform_device *pdev)
653 struct clk *hclk_usb_peri;
657 dev_err(&pdev->dev, "unable to alloc memory for control usb\n");
662 hclk_usb_peri = devm_clk_get(&pdev->dev, "hclk_usb_peri");
663 if (IS_ERR(hclk_usb_peri)) {
664 dev_err(&pdev->dev, "Failed to get hclk_usb_peri\n");
669 control_usb->hclk_usb_peri = hclk_usb_peri;
670 clk_prepare_enable(hclk_usb_peri);
672 #ifdef CONFIG_USB20_OTG
673 if (usb20otg_get_status(USB_STATUS_BVABLID))
674 schedule_delayed_work(&control_usb->usb_charger_det_work,
678 ret = otg_irq_detect_init(pdev);
685 clk_disable_unprepare(hclk_usb_peri);
690 static int dwc_otg_control_usb_remove(struct platform_device *pdev)
692 clk_disable_unprepare(control_usb->hclk_usb_peri);
696 static struct platform_driver dwc_otg_control_usb_driver = {
697 .probe = dwc_otg_control_usb_probe,
698 .remove = dwc_otg_control_usb_remove,
700 .name = "rk3036-dwc-control-usb",
701 .owner = THIS_MODULE,
702 .of_match_table = of_match_ptr(dwc_otg_control_usb_id_table),
706 static int __init dwc_otg_control_usb_init(void)
710 retval |= platform_driver_register(&rk_usb_control_driver);
711 retval |= platform_driver_register(&dwc_otg_control_usb_driver);
715 subsys_initcall(dwc_otg_control_usb_init);
717 static void __exit dwc_otg_control_usb_exit(void)
719 platform_driver_unregister(&rk_usb_control_driver);
720 platform_driver_unregister(&dwc_otg_control_usb_driver);
723 module_exit(dwc_otg_control_usb_exit);
724 MODULE_ALIAS("platform: dwc_control_usb");
725 MODULE_AUTHOR("RockChip Inc.");
726 MODULE_DESCRIPTION("RockChip Control Module USB Driver");
727 MODULE_LICENSE("GPL v2");