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;
21 /* enable soft control */
22 writel(UOC_HIWORD_UPDATE(0x55, 0x7f, 0),
23 RK_GRF_VIRT + RK312X_GRF_UOC0_CON0);
24 usbpdata->phy_status = 1;
27 writel(UOC_HIWORD_UPDATE(0x0, 0x1, 0),
28 RK_GRF_VIRT + RK312X_GRF_UOC0_CON0);
29 usbpdata->phy_status = 0;
33 static void usb20otg_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
35 struct dwc_otg_platform_data *usbpdata = pdata;
36 struct reset_control *rst_otg_h, *rst_otg_p, *rst_otg_c;
38 rst_otg_h = devm_reset_control_get(usbpdata->dev, "otg_ahb");
39 rst_otg_p = devm_reset_control_get(usbpdata->dev, "otg_phy");
40 rst_otg_c = devm_reset_control_get(usbpdata->dev, "otg_controller");
41 if (IS_ERR(rst_otg_h) || IS_ERR(rst_otg_p) || IS_ERR(rst_otg_c)) {
42 dev_err(usbpdata->dev, "Fail to get reset control from dts\n");
49 writel(UOC_HIWORD_UPDATE(0x1, 0x3, 0),
50 RK_GRF_VIRT + RK312X_GRF_UOC0_CON0);
51 reset_control_assert(rst_otg_p);
53 writel(UOC_HIWORD_UPDATE(0x2, 0x3, 0),
54 RK_GRF_VIRT + RK312X_GRF_UOC0_CON0);
56 reset_control_deassert(rst_otg_p);
59 /* Controller reset */
60 reset_control_assert(rst_otg_c);
61 reset_control_assert(rst_otg_h);
65 reset_control_deassert(rst_otg_c);
66 reset_control_deassert(rst_otg_h);
74 static void usb20otg_clock_init(void *pdata)
76 struct dwc_otg_platform_data *usbpdata = pdata;
77 struct clk *ahbclk, *phyclk;
79 ahbclk = devm_clk_get(usbpdata->dev, "hclk_usb0");
81 dev_err(usbpdata->dev, "Failed to get hclk_usb0\n");
85 phyclk = devm_clk_get(usbpdata->dev, "clk_usbphy0");
87 dev_err(usbpdata->dev, "Failed to get clk_usbphy0\n");
91 usbpdata->phyclk = phyclk;
92 usbpdata->ahbclk = ahbclk;
95 static void usb20otg_clock_enable(void *pdata, int enable)
97 struct dwc_otg_platform_data *usbpdata = pdata;
100 clk_prepare_enable(usbpdata->ahbclk);
101 clk_prepare_enable(usbpdata->phyclk);
103 clk_disable_unprepare(usbpdata->ahbclk);
105 clk_disable_unprepare(usbpdata->phyclk);
110 static int usb20otg_get_status(int id)
113 u32 soc_status0 = readl(RK_GRF_VIRT + RK312X_GRF_SOC_STATUS0);
115 case USB_STATUS_BVABLID:
117 ret = soc_status0 & (0x1 << 5);
119 case USB_STATUS_DPDM:
121 ret = soc_status0 & (0x3 << 6);
125 ret = soc_status0 & (0x1 << 8);
128 ret = control_usb->chip_id;
130 case USB_REMOTE_WAKEUP:
131 ret = control_usb->remote_wakeup;
134 ret = control_usb->usb_irq_wakeup;
142 #ifdef CONFIG_RK_USB_UART
144 * dwc_otg_uart_enabled - check if a usb-uart bypass func is enabled in DT
146 * Returns true if the status property of node "usb_uart" is set to "okay"
147 * or "ok", if this property is absent it will use the default status "ok"
150 static bool dwc_otg_uart_enabled(void)
152 struct device_node *np;
154 np = of_find_node_by_name(NULL, "usb_uart");
155 if (np && of_device_is_available(np))
161 static void dwc_otg_uart_mode(void *pdata, int enter_usb_uart_mode)
163 if ((1 == enter_usb_uart_mode) && dwc_otg_uart_enabled()) {
164 /* bypass dm, enter uart mode */
165 writel(UOC_HIWORD_UPDATE(0x3, 0x3, 12), RK_GRF_VIRT +
166 RK312X_GRF_UOC1_CON4);
168 } else if (0 == enter_usb_uart_mode) {
170 writel(UOC_HIWORD_UPDATE(0x0, 0x3, 12), RK_GRF_VIRT +
171 RK312X_GRF_UOC1_CON4);
175 static void dwc_otg_uart_mode(void *pdata, int enter_usb_uart_mode)
180 static void usb20otg_power_enable(int enable)
183 /* disable otg_drv power */
184 if (gpio_is_valid(control_usb->otg_gpios->gpio))
185 gpio_set_value(control_usb->otg_gpios->gpio, 0);
186 } else if (1 == enable) {
187 /* enable otg_drv power */
188 if (gpio_is_valid(control_usb->otg_gpios->gpio))
189 gpio_set_value(control_usb->otg_gpios->gpio, 1);
193 struct dwc_otg_platform_data usb20otg_pdata_rk3126 = {
198 .hw_init = usb20otg_hw_init,
199 .phy_suspend = usb20otg_phy_suspend,
200 .soft_reset = usb20otg_soft_reset,
201 .clock_init = usb20otg_clock_init,
202 .clock_enable = usb20otg_clock_enable,
203 .get_status = usb20otg_get_status,
204 .power_enable = usb20otg_power_enable,
205 .dwc_otg_uart_mode = dwc_otg_uart_mode,
206 .bc_detect_cb = usb20otg_battery_charger_detect_cb,
210 #ifdef CONFIG_USB20_HOST
211 static void usb20host_hw_init(void)
213 writel(UOC_HIWORD_UPDATE(1, 1, 3),
214 RK_GRF_VIRT + RK312X_GRF_SOC_CON2);
215 /* other haredware init,include:
216 * DRV_VBUS GPIO init */
217 if (gpio_is_valid(control_usb->host_gpios->gpio)) {
218 if (!gpio_get_value(control_usb->host_gpios->gpio))
219 gpio_set_value(control_usb->host_gpios->gpio, 1);
223 static void usb20host_phy_suspend(void *pdata, int suspend)
225 struct dwc_otg_platform_data *usbpdata = pdata;
228 /* enable soft control */
229 writel(UOC_HIWORD_UPDATE(0x1d5, 0x1ff, 0),
230 RK_GRF_VIRT + RK312X_GRF_UOC1_CON5);
231 usbpdata->phy_status = 1;
234 writel(UOC_HIWORD_UPDATE(0x0, 0x1, 0),
235 RK_GRF_VIRT + RK312X_GRF_UOC1_CON5);
236 usbpdata->phy_status = 0;
240 static void usb20host_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
242 struct dwc_otg_platform_data *usbpdata = pdata;
243 struct reset_control *rst_host_h, *rst_host_p, *rst_host_c;
245 rst_host_h = devm_reset_control_get(usbpdata->dev, "host_ahb");
246 rst_host_p = devm_reset_control_get(usbpdata->dev, "host_phy");
247 rst_host_c = devm_reset_control_get(usbpdata->dev, "host_controller");
248 if (IS_ERR(rst_host_h) || IS_ERR(rst_host_p) || IS_ERR(rst_host_c)) {
249 dev_err(usbpdata->dev, "Fail to get reset control from dts\n");
256 writel(UOC_HIWORD_UPDATE(0x1, 0x3, 0),
257 RK_GRF_VIRT + RK312X_GRF_UOC1_CON5);
258 reset_control_assert(rst_host_p);
260 writel(UOC_HIWORD_UPDATE(0x2, 0x3, 0),
261 RK_GRF_VIRT + RK312X_GRF_UOC1_CON5);
264 reset_control_deassert(rst_host_p);
266 /* Controller reset */
267 reset_control_assert(rst_host_c);
268 reset_control_assert(rst_host_h);
272 reset_control_deassert(rst_host_c);
273 reset_control_deassert(rst_host_h);
281 static void usb20host_clock_init(void *pdata)
283 struct dwc_otg_platform_data *usbpdata = pdata;
284 struct clk *ahbclk, *phyclk;
286 ahbclk = devm_clk_get(usbpdata->dev, "hclk_usb1");
287 if (IS_ERR(ahbclk)) {
288 dev_err(usbpdata->dev, "Failed to get hclk_usb1\n");
292 phyclk = devm_clk_get(usbpdata->dev, "clk_usbphy1");
293 if (IS_ERR(phyclk)) {
294 dev_err(usbpdata->dev, "Failed to get clk_usbphy1\n");
298 usbpdata->phyclk = phyclk;
299 usbpdata->ahbclk = ahbclk;
302 static void usb20host_clock_enable(void *pdata, int enable)
304 struct dwc_otg_platform_data *usbpdata = pdata;
307 clk_prepare_enable(usbpdata->ahbclk);
308 clk_prepare_enable(usbpdata->phyclk);
310 clk_disable_unprepare(usbpdata->ahbclk);
311 clk_disable_unprepare(usbpdata->phyclk);
315 static int usb20host_get_status(int id)
318 u32 soc_status0 = readl(RK_GRF_VIRT + RK312X_GRF_SOC_STATUS0);
321 case USB_STATUS_BVABLID:
323 ret = soc_status0 & (0x1 << 10);
325 case USB_STATUS_DPDM:
327 ret = soc_status0 & (0x3 << 11);
334 ret = control_usb->chip_id;
336 case USB_REMOTE_WAKEUP:
337 ret = control_usb->remote_wakeup;
340 ret = control_usb->usb_irq_wakeup;
348 static void usb20host_power_enable(int enable)
351 /* disable host_drv power */
352 /* do not disable power in default */
353 } else if (1 == enable) {
354 /* enable host_drv power */
355 if (gpio_is_valid(control_usb->host_gpios->gpio))
356 gpio_set_value(control_usb->host_gpios->gpio, 1);
360 struct dwc_otg_platform_data usb20host_pdata_rk3126 = {
365 .hw_init = usb20host_hw_init,
366 .phy_suspend = usb20host_phy_suspend,
367 .soft_reset = usb20host_soft_reset,
368 .clock_init = usb20host_clock_init,
369 .clock_enable = usb20host_clock_enable,
370 .get_status = usb20host_get_status,
371 .power_enable = usb20host_power_enable,
376 static const struct of_device_id rk_usb_control_id_table[] = {
378 .compatible = "rockchip,rk3126-usb-control",
383 /*********************************************************************
384 rk3126 usb detections
385 *********************************************************************/
387 #define WAKE_LOCK_TIMEOUT (HZ * 10)
388 static inline void do_wakeup(struct work_struct *work)
390 /* wake up the system */
391 rk_send_wakeup_key();
394 static void usb_battery_charger_detect_work(struct work_struct *work)
396 rk_usb_charger_status = usb_battery_charger_detect(0);
399 /********** handler for bvalid irq **********/
400 static irqreturn_t bvalid_irq_handler(int irq, void *dev_id)
403 writel(UOC_HIWORD_UPDATE(0x1, 0x1, 15),
404 RK_GRF_VIRT + RK312X_GRF_UOC0_CON0);
405 #ifdef CONFIG_RK_USB_UART
406 /* usb otg dp/dm switch to usb phy */
407 dwc_otg_uart_mode(NULL, PHY_USB_MODE);
410 if (control_usb->usb_irq_wakeup) {
411 wake_lock_timeout(&control_usb->usb_wakelock,
413 schedule_delayed_work(&control_usb->usb_det_wakeup_work,
417 rk_usb_charger_status = USB_BC_TYPE_SDP;
418 schedule_delayed_work(&control_usb->usb_charger_det_work, HZ / 10);
423 /************* register usb detection irqs **************/
424 static int otg_irq_detect_init(struct platform_device *pdev)
429 if (control_usb->usb_irq_wakeup) {
430 wake_lock_init(&control_usb->usb_wakelock, WAKE_LOCK_SUSPEND,
432 INIT_DELAYED_WORK(&control_usb->usb_det_wakeup_work, do_wakeup);
435 /*register otg_bvalid irq */
436 irq = platform_get_irq_byname(pdev, "otg_bvalid");
437 if ((irq > 0) && control_usb->usb_irq_wakeup) {
438 ret = request_irq(irq, bvalid_irq_handler,
439 0, "otg_bvalid", NULL);
441 dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
443 /* enable bvalid irq */
444 writel(UOC_HIWORD_UPDATE(0x1, 0x1, 14),
445 RK_GRF_VIRT + RK312X_GRF_UOC0_CON0);
451 /********** end of rk3126 usb detections **********/
452 static int rk_usb_control_probe(struct platform_device *pdev)
455 struct device_node *np = pdev->dev.of_node;
459 devm_kzalloc(&pdev->dev, sizeof(*control_usb), GFP_KERNEL);
461 dev_err(&pdev->dev, "unable to alloc memory for control usb\n");
466 control_usb->chip_id = RK3126_USB_CTLR;
467 control_usb->remote_wakeup = of_property_read_bool(np,
468 "rockchip,remote_wakeup");
469 control_usb->usb_irq_wakeup = of_property_read_bool(np,
470 "rockchip,usb_irq_wakeup");
472 INIT_DELAYED_WORK(&control_usb->usb_charger_det_work,
473 usb_battery_charger_detect_work);
475 control_usb->host_gpios =
476 devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
477 if (!control_usb->host_gpios) {
478 dev_err(&pdev->dev, "unable to alloc memory for host_gpios\n");
483 gpio = of_get_named_gpio(np, "host_drv_gpio", 0);
484 control_usb->host_gpios->gpio = gpio;
486 if (!gpio_is_valid(gpio)) {
487 dev_err(&pdev->dev, "invalid host gpio%d\n", gpio);
489 err = devm_gpio_request(&pdev->dev, gpio, "host_drv_gpio");
492 "failed to request GPIO%d for host_drv\n",
497 gpio_direction_output(control_usb->host_gpios->gpio, 1);
500 control_usb->otg_gpios =
501 devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
502 if (!control_usb->otg_gpios) {
503 dev_err(&pdev->dev, "unable to alloc memory for otg_gpios\n");
508 gpio = of_get_named_gpio(np, "otg_drv_gpio", 0);
509 control_usb->otg_gpios->gpio = gpio;
511 if (!gpio_is_valid(gpio)) {
512 dev_err(&pdev->dev, "invalid otg gpio%d\n", gpio);
514 err = devm_gpio_request(&pdev->dev, gpio, "otg_drv_gpio");
517 "failed to request GPIO%d for otg_drv\n", gpio);
521 gpio_direction_output(control_usb->otg_gpios->gpio, 0);
528 static int rk_usb_control_remove(struct platform_device *pdev)
533 static struct platform_driver rk_usb_control_driver = {
534 .probe = rk_usb_control_probe,
535 .remove = rk_usb_control_remove,
537 .name = "rk3126-usb-control",
538 .owner = THIS_MODULE,
539 .of_match_table = of_match_ptr(rk_usb_control_id_table),
545 static const struct of_device_id dwc_otg_control_usb_id_table[] = {
547 .compatible = "rockchip,rk3126-dwc-control-usb",
553 static int dwc_otg_control_usb_probe(struct platform_device *pdev)
555 struct clk *hclk_usb_peri;
559 dev_err(&pdev->dev, "unable to alloc memory for control usb\n");
564 hclk_usb_peri = devm_clk_get(&pdev->dev, "hclk_usb_peri");
565 if (IS_ERR(hclk_usb_peri)) {
566 dev_err(&pdev->dev, "Failed to get hclk_usb_peri\n");
571 control_usb->hclk_usb_peri = hclk_usb_peri;
572 clk_prepare_enable(hclk_usb_peri);
574 #ifdef CONFIG_USB20_OTG
575 if (usb20otg_get_status(USB_STATUS_BVABLID)) {
576 rk_usb_charger_status = USB_BC_TYPE_SDP;
577 schedule_delayed_work(&control_usb->usb_charger_det_work,
582 ret = otg_irq_detect_init(pdev);
589 clk_disable_unprepare(hclk_usb_peri);
594 static int dwc_otg_control_usb_remove(struct platform_device *pdev)
596 clk_disable_unprepare(control_usb->hclk_usb_peri);
600 static struct platform_driver dwc_otg_control_usb_driver = {
601 .probe = dwc_otg_control_usb_probe,
602 .remove = dwc_otg_control_usb_remove,
604 .name = "rk3126-dwc-control-usb",
605 .owner = THIS_MODULE,
606 .of_match_table = of_match_ptr(dwc_otg_control_usb_id_table),
610 static int __init dwc_otg_control_usb_init(void)
614 retval |= platform_driver_register(&rk_usb_control_driver);
615 retval |= platform_driver_register(&dwc_otg_control_usb_driver);
619 subsys_initcall(dwc_otg_control_usb_init);
621 static void __exit dwc_otg_control_usb_exit(void)
623 platform_driver_unregister(&rk_usb_control_driver);
624 platform_driver_unregister(&dwc_otg_control_usb_driver);
627 module_exit(dwc_otg_control_usb_exit);
628 MODULE_ALIAS("platform: dwc_control_usb");
629 MODULE_AUTHOR("RockChip Inc.");
630 MODULE_DESCRIPTION("RockChip Control Module USB Driver");
631 MODULE_LICENSE("GPL v2");