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)
36 writel(UOC_HIWORD_UPDATE(0x1, 0x3, 0),
37 RK_GRF_VIRT + RK3036_GRF_UOC0_CON5);
38 writel(UOC_HIWORD_UPDATE(0x1, 0x3, 0),
39 RK_GRF_VIRT + RK3036_GRF_UOC1_CON5);
40 /* cru_set_soft_reset(SOFT_RST_USBPOR, true); */
41 writel(UOC_HIWORD_UPDATE(0x1, 0x1, 9),
42 RK_CRU_VIRT + RK3036_CRU_SOFTRST6_CON);
43 /* cru_set_soft_reset(SOFT_RST_UTMI0, true); */
44 writel(UOC_HIWORD_UPDATE(0x1, 0x1, 7),
45 RK_CRU_VIRT + RK3036_CRU_SOFTRST6_CON);
46 /* cru_set_soft_reset(SOFT_RST_UTMI1, true); */
47 writel(UOC_HIWORD_UPDATE(0x1, 0x1, 8),
48 RK_CRU_VIRT + RK3036_CRU_SOFTRST6_CON);
52 writel(UOC_HIWORD_UPDATE(0x2, 0x3, 0),
53 RK_GRF_VIRT + RK3036_GRF_UOC0_CON5);
54 writel(UOC_HIWORD_UPDATE(0x2, 0x3, 0),
55 RK_GRF_VIRT + RK3036_GRF_UOC1_CON5);
58 /* cru_set_soft_reset(SOFT_RST_USBPOR, false); */
59 writel(UOC_HIWORD_UPDATE(0x0, 0x1, 9),
60 RK_CRU_VIRT + RK3036_CRU_SOFTRST6_CON);
62 /* cru_set_soft_reset(SOFT_RST_UTMI0, false); */
63 writel(UOC_HIWORD_UPDATE(0x0, 0x1, 7),
64 RK_CRU_VIRT + RK3036_CRU_SOFTRST6_CON);
65 /* cru_set_soft_reset(SOFT_RST_UTMI1, false); */
66 writel(UOC_HIWORD_UPDATE(0x0, 0x1, 8),
67 RK_CRU_VIRT + RK3036_CRU_SOFTRST6_CON);
70 /* cru_set_soft_reset(SOFT_RST_OTGC0, true); */
71 writel(UOC_HIWORD_UPDATE(0x1, 0x1, 7),
72 RK_CRU_VIRT + RK3036_CRU_SOFTRST4_CON);
73 /* cru_set_soft_reset(SOFT_RST_OTGC1, true); */
74 writel(UOC_HIWORD_UPDATE(0x1, 0x1, 10),
75 RK_CRU_VIRT + RK3036_CRU_SOFTRST4_CON);
78 /* cru_set_soft_reset(SOFT_RST_USBOTG0, true); */
79 writel(UOC_HIWORD_UPDATE(0x1, 0x1, 5),
80 RK_CRU_VIRT + RK3036_CRU_SOFTRST4_CON);
81 /* cru_set_soft_reset(SOFT_RST_USBOTG1, true); */
82 writel(UOC_HIWORD_UPDATE(0x1, 0x1, 8),
83 RK_CRU_VIRT + RK3036_CRU_SOFTRST4_CON);
86 cru_set_soft_reset(SOFT_RST_OTGC0,false);
87 cru_set_soft_reset(SOFT_RST_OTGC1,false);
88 ru_set_soft_reset(SOFT_RST_USBOTG0,false);
89 cru_set_soft_reset(SOFT_RST_USBOTG1,false);
91 writel(UOC_HIWORD_UPDATE(0x0, 0x1, 7),
92 RK_CRU_VIRT + RK3036_CRU_SOFTRST4_CON);
93 writel(UOC_HIWORD_UPDATE(0x0, 0x1, 10),
94 RK_CRU_VIRT + RK3036_CRU_SOFTRST4_CON);
95 writel(UOC_HIWORD_UPDATE(0x0, 0x1, 5),
96 RK_CRU_VIRT + RK3036_CRU_SOFTRST4_CON);
97 writel(UOC_HIWORD_UPDATE(0x0, 0x1, 8),
98 RK_CRU_VIRT + RK3036_CRU_SOFTRST4_CON);
101 static void usb20otg_clock_init(void *pdata)
103 struct dwc_otg_platform_data *usbpdata = pdata;
104 struct clk *ahbclk, *phyclk;
106 ahbclk = devm_clk_get(usbpdata->dev, "hclk_usb0");
107 if (IS_ERR(ahbclk)) {
108 dev_err(usbpdata->dev, "Failed to get hclk_usb0\n");
112 phyclk = devm_clk_get(usbpdata->dev, "clk_usbphy0");
113 if (IS_ERR(phyclk)) {
114 dev_err(usbpdata->dev, "Failed to get clk_usbphy0\n");
118 usbpdata->phyclk = phyclk;
119 usbpdata->ahbclk = ahbclk;
122 static void usb20otg_clock_enable(void *pdata, int enable)
124 struct dwc_otg_platform_data *usbpdata = pdata;
127 clk_prepare_enable(usbpdata->ahbclk);
128 clk_prepare_enable(usbpdata->phyclk);
130 clk_disable_unprepare(usbpdata->ahbclk);
132 clk_disable_unprepare(usbpdata->phyclk);
137 static int usb20otg_get_status(int id)
140 u32 soc_status0 = readl(RK_GRF_VIRT + RK3036_GRF_CPU_STATUS0);
143 case USB_STATUS_BVABLID:
145 ret = soc_status0 & (0x1 << 8);
147 case USB_STATUS_DPDM:
149 ret = soc_status0 & (0x3 << 9);
153 ret = soc_status0 & (0x1 << 11);
156 ret = control_usb->chip_id;
158 case USB_REMOTE_WAKEUP:
159 ret = control_usb->remote_wakeup;
162 ret = control_usb->usb_irq_wakeup;
170 static void dwc_otg_uart_mode(void *pdata, int enter_usb_uart_mode)
172 #ifdef CONFIG_RK_USB_UART
173 if (1 == enter_usb_uart_mode) {
174 /* bypass dm, enter uart mode */
175 writel(UOC_HIWORD_UPDATE(0x3, 0x3, 12),
176 RK_GRF_VIRT + RK3036_GRF_UOC1_CON4);
177 } else if (0 == enter_usb_uart_mode) {
179 writel(UOC_HIWORD_UPDATE(0x0, 0x3, 12),
180 RK_GRF_VIRT + RK3036_GRF_UOC1_CON4);
185 static void usb20otg_power_enable(int enable)
188 /* disable otg_drv power */
189 if (gpio_is_valid(control_usb->otg_gpios->gpio))
190 gpio_set_value(control_usb->otg_gpios->gpio, 0);
191 } else if (1 == enable) {
192 /* enable otg_drv power */
193 if (gpio_is_valid(control_usb->otg_gpios->gpio))
194 gpio_set_value(control_usb->otg_gpios->gpio, 1);
198 struct dwc_otg_platform_data usb20otg_pdata_rk3036 = {
203 .hw_init = usb20otg_hw_init,
204 .phy_suspend = usb20otg_phy_suspend,
205 .soft_reset = usb20otg_soft_reset,
206 .clock_init = usb20otg_clock_init,
207 .clock_enable = usb20otg_clock_enable,
208 .get_status = usb20otg_get_status,
209 .power_enable = usb20otg_power_enable,
210 .dwc_otg_uart_mode = dwc_otg_uart_mode,
211 .bc_detect_cb = usb20otg_battery_charger_detect_cb,
215 #ifdef CONFIG_USB20_HOST
216 static void usb20host_hw_init(void)
218 /* other haredware init,include:
219 * DRV_VBUS GPIO init */
220 if (gpio_is_valid(control_usb->host_gpios->gpio)) {
221 if (!gpio_get_value(control_usb->host_gpios->gpio))
222 gpio_set_value(control_usb->host_gpios->gpio, 1);
226 static void usb20host_phy_suspend(void *pdata, int suspend)
228 struct dwc_otg_platform_data *usbpdata = pdata;
231 /* enable soft control */
232 writel(UOC_HIWORD_UPDATE(0x1d5, 0x1ff, 0),
233 RK_GRF_VIRT + RK3036_GRF_UOC1_CON5);
234 usbpdata->phy_status = 1;
237 writel(UOC_HIWORD_UPDATE(0x0, 0x1, 0),
238 RK_GRF_VIRT + RK3036_GRF_UOC1_CON5);
239 usbpdata->phy_status = 0;
243 static void usb20host_soft_reset(void)
248 static void usb20host_clock_init(void *pdata)
250 struct dwc_otg_platform_data *usbpdata = pdata;
251 struct clk *ahbclk, *phyclk;
253 ahbclk = devm_clk_get(usbpdata->dev, "hclk_usb1");
254 if (IS_ERR(ahbclk)) {
255 dev_err(usbpdata->dev, "Failed to get hclk_usb1\n");
259 phyclk = devm_clk_get(usbpdata->dev, "clk_usbphy1");
260 if (IS_ERR(phyclk)) {
261 dev_err(usbpdata->dev, "Failed to get clk_usbphy1\n");
265 usbpdata->phyclk = phyclk;
266 usbpdata->ahbclk = ahbclk;
269 static void usb20host_clock_enable(void *pdata, int enable)
271 struct dwc_otg_platform_data *usbpdata = pdata;
274 clk_prepare_enable(usbpdata->ahbclk);
275 clk_prepare_enable(usbpdata->phyclk);
277 clk_disable_unprepare(usbpdata->ahbclk);
278 clk_disable_unprepare(usbpdata->phyclk);
282 static int usb20host_get_status(int id)
285 u32 soc_status0 = readl(RK_GRF_VIRT + RK3036_GRF_CPU_STATUS0);
288 case USB_STATUS_BVABLID:
290 ret = soc_status0 & (0x1 << 13);
292 case USB_STATUS_DPDM:
294 ret = soc_status0 & (0x3 << 14);
301 ret = control_usb->chip_id;
303 case USB_REMOTE_WAKEUP:
304 ret = control_usb->remote_wakeup;
307 ret = control_usb->usb_irq_wakeup;
315 static void usb20host_power_enable(int enable)
318 /* disable host_drv power */
319 /* do not disable power in default */
320 } else if (1 == enable) {
321 /* enable host_drv power */
322 if (gpio_is_valid(control_usb->host_gpios->gpio))
323 gpio_set_value(control_usb->host_gpios->gpio, 1);
327 struct dwc_otg_platform_data usb20host_pdata_rk3036 = {
332 .hw_init = usb20host_hw_init,
333 .phy_suspend = usb20host_phy_suspend,
334 .soft_reset = usb20host_soft_reset,
335 .clock_init = usb20host_clock_init,
336 .clock_enable = usb20host_clock_enable,
337 .get_status = usb20host_get_status,
338 .power_enable = usb20host_power_enable,
343 static const struct of_device_id rk_usb_control_id_table[] = {
345 .compatible = "rockchip,rk3036-usb-control",
350 /*********************************************************************
351 rk3036 usb detections
352 *********************************************************************/
354 #define WAKE_LOCK_TIMEOUT (HZ * 10)
355 static inline void do_wakeup(struct work_struct *work)
357 /* wake up the system */
358 rk_send_wakeup_key();
361 static void usb_battery_charger_detect_work(struct work_struct *work)
363 rk_usb_charger_status = usb_battery_charger_detect(0);
366 /********** handler for bvalid irq **********/
367 static irqreturn_t bvalid_irq_handler(int irq, void *dev_id)
370 writel(UOC_HIWORD_UPDATE(0x1, 0x1, 15),
371 RK_GRF_VIRT + RK3036_GRF_UOC0_CON5);
372 #ifdef CONFIG_RK_USB_UART
373 /* usb otg dp/dm switch to usb phy */
374 dwc_otg_uart_mode(NULL, PHY_USB_MODE);
377 if (control_usb->usb_irq_wakeup) {
378 wake_lock_timeout(&control_usb->usb_wakelock,
380 schedule_delayed_work(&control_usb->usb_det_wakeup_work,
384 rk_usb_charger_status = USB_BC_TYPE_SDP;
385 schedule_delayed_work(&control_usb->usb_charger_det_work, HZ / 10);
390 /************* register usb detection irqs **************/
391 static int otg_irq_detect_init(struct platform_device *pdev)
396 if (control_usb->usb_irq_wakeup) {
397 wake_lock_init(&control_usb->usb_wakelock, WAKE_LOCK_SUSPEND,
399 INIT_DELAYED_WORK(&control_usb->usb_det_wakeup_work, do_wakeup);
402 /*register otg_bvalid irq */
403 irq = platform_get_irq_byname(pdev, "otg_bvalid");
404 if ((irq > 0) && control_usb->usb_irq_wakeup) {
405 ret = request_irq(irq, bvalid_irq_handler,
406 0, "otg_bvalid", NULL);
408 dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
410 /* enable bvalid irq */
411 writel(UOC_HIWORD_UPDATE(0x1, 0x1, 14),
412 RK_GRF_VIRT + RK3036_GRF_UOC0_CON5);
418 /********** end of rk3036 usb detections **********/
419 static int rk_usb_control_probe(struct platform_device *pdev)
422 struct device_node *np = pdev->dev.of_node;
426 devm_kzalloc(&pdev->dev, sizeof(*control_usb), GFP_KERNEL);
428 dev_err(&pdev->dev, "unable to alloc memory for control usb\n");
433 control_usb->chip_id = RK3036_USB_CTLR;
434 control_usb->remote_wakeup = of_property_read_bool(np,
435 "rockchip,remote_wakeup");
436 control_usb->usb_irq_wakeup = of_property_read_bool(np,
437 "rockchip,usb_irq_wakeup");
439 INIT_DELAYED_WORK(&control_usb->usb_charger_det_work,
440 usb_battery_charger_detect_work);
442 control_usb->host_gpios =
443 devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
444 if (!control_usb->host_gpios) {
445 dev_err(&pdev->dev, "unable to alloc memory for host_gpios\n");
450 gpio = of_get_named_gpio(np, "host_drv_gpio", 0);
451 control_usb->host_gpios->gpio = gpio;
453 if (!gpio_is_valid(gpio)) {
454 dev_err(&pdev->dev, "invalid host gpio%d\n", gpio);
456 err = devm_gpio_request(&pdev->dev, gpio, "host_drv_gpio");
459 "failed to request GPIO%d for host_drv\n",
464 gpio_direction_output(control_usb->host_gpios->gpio, 1);
467 control_usb->otg_gpios =
468 devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
469 if (!control_usb->otg_gpios) {
470 dev_err(&pdev->dev, "unable to alloc memory for otg_gpios\n");
475 gpio = of_get_named_gpio(np, "otg_drv_gpio", 0);
476 control_usb->otg_gpios->gpio = gpio;
478 if (!gpio_is_valid(gpio)) {
479 dev_err(&pdev->dev, "invalid otg gpio%d\n", gpio);
481 err = devm_gpio_request(&pdev->dev, gpio, "otg_drv_gpio");
484 "failed to request GPIO%d for otg_drv\n", gpio);
488 gpio_direction_output(control_usb->otg_gpios->gpio, 0);
495 static int rk_usb_control_remove(struct platform_device *pdev)
500 static struct platform_driver rk_usb_control_driver = {
501 .probe = rk_usb_control_probe,
502 .remove = rk_usb_control_remove,
504 .name = "rk3036-usb-control",
505 .owner = THIS_MODULE,
506 .of_match_table = of_match_ptr(rk_usb_control_id_table),
512 static const struct of_device_id dwc_otg_control_usb_id_table[] = {
514 .compatible = "rockchip,rk3036-dwc-control-usb",
520 static int dwc_otg_control_usb_probe(struct platform_device *pdev)
522 struct clk *hclk_usb_peri;
526 dev_err(&pdev->dev, "unable to alloc memory for control usb\n");
531 hclk_usb_peri = devm_clk_get(&pdev->dev, "hclk_usb_peri");
532 if (IS_ERR(hclk_usb_peri)) {
533 dev_err(&pdev->dev, "Failed to get hclk_usb_peri\n");
538 control_usb->hclk_usb_peri = hclk_usb_peri;
539 clk_prepare_enable(hclk_usb_peri);
541 #ifdef CONFIG_USB20_OTG
542 if (usb20otg_get_status(USB_STATUS_BVABLID)) {
543 rk_usb_charger_status = USB_BC_TYPE_SDP;
544 schedule_delayed_work(&control_usb->usb_charger_det_work,
549 ret = otg_irq_detect_init(pdev);
556 clk_disable_unprepare(hclk_usb_peri);
561 static int dwc_otg_control_usb_remove(struct platform_device *pdev)
563 clk_disable_unprepare(control_usb->hclk_usb_peri);
567 static struct platform_driver dwc_otg_control_usb_driver = {
568 .probe = dwc_otg_control_usb_probe,
569 .remove = dwc_otg_control_usb_remove,
571 .name = "rk3036-dwc-control-usb",
572 .owner = THIS_MODULE,
573 .of_match_table = of_match_ptr(dwc_otg_control_usb_id_table),
577 static int __init dwc_otg_control_usb_init(void)
581 retval |= platform_driver_register(&rk_usb_control_driver);
582 retval |= platform_driver_register(&dwc_otg_control_usb_driver);
586 subsys_initcall(dwc_otg_control_usb_init);
588 static void __exit dwc_otg_control_usb_exit(void)
590 platform_driver_unregister(&rk_usb_control_driver);
591 platform_driver_unregister(&dwc_otg_control_usb_driver);
594 module_exit(dwc_otg_control_usb_exit);
595 MODULE_ALIAS("platform: dwc_control_usb");
596 MODULE_AUTHOR("RockChip Inc.");
597 MODULE_DESCRIPTION("RockChip Control Module USB Driver");
598 MODULE_LICENSE("GPL v2");