1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
3 * platform.c - DesignWare HS OTG Controller platform driver
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/clk.h>
12 #include <linux/device.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/of_device.h>
15 #include <linux/mutex.h>
16 #include <linux/platform_device.h>
17 #include <linux/phy/phy.h>
18 #include <linux/platform_data/s3c-hsotg.h>
19 #include <linux/reset.h>
21 #include <linux/usb/of.h>
27 static const char dwc2_driver_name[] = "dwc2";
30 * Check the dr_mode against the module configuration and hardware
33 * The hardware, module, and dr_mode, can each be set to host, device,
34 * or otg. Check that all these values are compatible and adjust the
35 * value of dr_mode if possible.
38 * HW MOD dr_mode dr_mode
39 * ------------------------------
50 * OTG OTG any : dr_mode
52 static int dwc2_get_dr_mode(struct dwc2_hsotg *hsotg)
54 enum usb_dr_mode mode;
56 hsotg->dr_mode = usb_get_dr_mode(hsotg->dev);
57 if (hsotg->dr_mode == USB_DR_MODE_UNKNOWN)
58 hsotg->dr_mode = USB_DR_MODE_OTG;
60 mode = hsotg->dr_mode;
62 if (dwc2_hw_is_device(hsotg)) {
63 if (IS_ENABLED(CONFIG_USB_DWC2_HOST)) {
65 "Controller does not support host mode.\n");
68 mode = USB_DR_MODE_PERIPHERAL;
69 } else if (dwc2_hw_is_host(hsotg)) {
70 if (IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL)) {
72 "Controller does not support device mode.\n");
75 mode = USB_DR_MODE_HOST;
77 if (IS_ENABLED(CONFIG_USB_DWC2_HOST))
78 mode = USB_DR_MODE_HOST;
79 else if (IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL))
80 mode = USB_DR_MODE_PERIPHERAL;
83 if (mode != hsotg->dr_mode) {
85 "Configuration mismatch. dr_mode forced to %s\n",
86 mode == USB_DR_MODE_HOST ? "host" : "device");
88 hsotg->dr_mode = mode;
94 static int __dwc2_lowlevel_hw_enable(struct dwc2_hsotg *hsotg)
96 struct platform_device *pdev = to_platform_device(hsotg->dev);
99 ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
104 if (hsotg->utmi_clk) {
105 ret = clk_prepare_enable(hsotg->utmi_clk);
111 ret = clk_prepare_enable(hsotg->clk);
113 goto err_dis_utmi_clk;
117 ret = usb_phy_init(hsotg->uphy);
118 } else if (hsotg->plat && hsotg->plat->phy_init) {
119 ret = hsotg->plat->phy_init(pdev, hsotg->plat->phy_type);
121 ret = phy_init(hsotg->phy);
123 ret = phy_power_on(hsotg->phy);
125 phy_exit(hsotg->phy);
136 clk_disable_unprepare(hsotg->clk);
140 clk_disable_unprepare(hsotg->utmi_clk);
143 regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies), hsotg->supplies);
149 * dwc2_lowlevel_hw_enable - enable platform lowlevel hw resources
150 * @hsotg: The driver state
152 * A wrapper for platform code responsible for controlling
153 * low-level USB platform resources (phy, clock, regulators)
155 int dwc2_lowlevel_hw_enable(struct dwc2_hsotg *hsotg)
157 int ret = __dwc2_lowlevel_hw_enable(hsotg);
160 hsotg->ll_hw_enabled = true;
164 static int __dwc2_lowlevel_hw_disable(struct dwc2_hsotg *hsotg)
166 struct platform_device *pdev = to_platform_device(hsotg->dev);
170 usb_phy_shutdown(hsotg->uphy);
171 } else if (hsotg->plat && hsotg->plat->phy_exit) {
172 ret = hsotg->plat->phy_exit(pdev, hsotg->plat->phy_type);
174 ret = phy_power_off(hsotg->phy);
176 ret = phy_exit(hsotg->phy);
182 clk_disable_unprepare(hsotg->clk);
185 clk_disable_unprepare(hsotg->utmi_clk);
187 return regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies), hsotg->supplies);
191 * dwc2_lowlevel_hw_disable - disable platform lowlevel hw resources
192 * @hsotg: The driver state
194 * A wrapper for platform code responsible for controlling
195 * low-level USB platform resources (phy, clock, regulators)
197 int dwc2_lowlevel_hw_disable(struct dwc2_hsotg *hsotg)
199 int ret = __dwc2_lowlevel_hw_disable(hsotg);
202 hsotg->ll_hw_enabled = false;
206 static int dwc2_lowlevel_hw_init(struct dwc2_hsotg *hsotg)
210 hsotg->reset = devm_reset_control_get_optional(hsotg->dev, "dwc2");
211 if (IS_ERR(hsotg->reset))
212 return dev_err_probe(hsotg->dev, PTR_ERR(hsotg->reset),
213 "error getting reset control\n");
215 reset_control_deassert(hsotg->reset);
217 hsotg->reset_ecc = devm_reset_control_get_optional(hsotg->dev, "dwc2-ecc");
218 if (IS_ERR(hsotg->reset_ecc))
219 return dev_err_probe(hsotg->dev, PTR_ERR(hsotg->reset_ecc),
220 "error getting reset control for ecc\n");
222 reset_control_deassert(hsotg->reset_ecc);
225 * Attempt to find a generic PHY, then look for an old style
226 * USB PHY and then fall back to pdata
228 hsotg->phy = devm_phy_get(hsotg->dev, "usb2-phy");
229 if (IS_ERR(hsotg->phy)) {
230 ret = PTR_ERR(hsotg->phy);
237 return dev_err_probe(hsotg->dev, ret, "error getting phy\n");
242 hsotg->uphy = devm_usb_get_phy(hsotg->dev, USB_PHY_TYPE_USB2);
243 if (IS_ERR(hsotg->uphy)) {
244 ret = PTR_ERR(hsotg->uphy);
251 return dev_err_probe(hsotg->dev, ret, "error getting usb phy\n");
256 hsotg->plat = dev_get_platdata(hsotg->dev);
259 hsotg->clk = devm_clk_get_optional(hsotg->dev, "otg");
260 if (IS_ERR(hsotg->clk))
261 return dev_err_probe(hsotg->dev, PTR_ERR(hsotg->clk), "cannot get otg clock\n");
263 hsotg->utmi_clk = devm_clk_get_optional(hsotg->dev, "utmi");
264 if (IS_ERR(hsotg->utmi_clk))
265 return dev_err_probe(hsotg->dev, PTR_ERR(hsotg->utmi_clk),
266 "cannot get utmi clock\n");
269 for (i = 0; i < ARRAY_SIZE(hsotg->supplies); i++)
270 hsotg->supplies[i].supply = dwc2_hsotg_supply_names[i];
272 ret = devm_regulator_bulk_get(hsotg->dev, ARRAY_SIZE(hsotg->supplies),
275 return dev_err_probe(hsotg->dev, ret, "failed to request supplies\n");
281 * dwc2_driver_remove() - Called when the DWC_otg core is unregistered with the
284 * @dev: Platform device
286 * This routine is called, for example, when the rmmod command is executed. The
287 * device may or may not be electrically present. If it is present, the driver
288 * stops device processing. Any resources used on behalf of this device are
291 static int dwc2_driver_remove(struct platform_device *dev)
293 struct dwc2_hsotg *hsotg = platform_get_drvdata(dev);
294 struct dwc2_gregs_backup *gr;
297 gr = &hsotg->gr_backup;
299 /* Exit Hibernation when driver is removed. */
300 if (hsotg->hibernated) {
301 if (gr->gotgctl & GOTGCTL_CURMODE_HOST)
302 ret = dwc2_exit_hibernation(hsotg, 0, 0, 1);
304 ret = dwc2_exit_hibernation(hsotg, 0, 0, 0);
308 "exit hibernation failed.\n");
311 /* Exit Partial Power Down when driver is removed. */
313 ret = dwc2_exit_partial_power_down(hsotg, 0, true);
316 "exit partial_power_down failed\n");
319 /* Exit clock gating when driver is removed. */
320 if (hsotg->params.power_down == DWC2_POWER_DOWN_PARAM_NONE &&
321 hsotg->bus_suspended) {
322 if (dwc2_is_device_mode(hsotg))
323 dwc2_gadget_exit_clock_gating(hsotg, 0);
325 dwc2_host_exit_clock_gating(hsotg, 0);
328 dwc2_debugfs_exit(hsotg);
329 if (hsotg->hcd_enabled)
330 dwc2_hcd_remove(hsotg);
331 if (hsotg->gadget_enabled)
332 dwc2_hsotg_remove(hsotg);
334 dwc2_drd_exit(hsotg);
336 if (hsotg->params.activate_stm_id_vb_detection)
337 regulator_disable(hsotg->usb33d);
339 if (hsotg->ll_hw_enabled)
340 dwc2_lowlevel_hw_disable(hsotg);
342 reset_control_assert(hsotg->reset);
343 reset_control_assert(hsotg->reset_ecc);
349 * dwc2_driver_shutdown() - Called on device shutdown
351 * @dev: Platform device
353 * In specific conditions (involving usb hubs) dwc2 devices can create a
354 * lot of interrupts, even to the point of overwhelming devices running
355 * at low frequencies. Some devices need to do special clock handling
356 * at shutdown-time which may bring the system clock below the threshold
357 * of being able to handle the dwc2 interrupts. Disabling dwc2-irqs
358 * prevents reboots/poweroffs from getting stuck in such cases.
360 static void dwc2_driver_shutdown(struct platform_device *dev)
362 struct dwc2_hsotg *hsotg = platform_get_drvdata(dev);
364 dwc2_disable_global_interrupts(hsotg);
365 synchronize_irq(hsotg->irq);
369 * dwc2_check_core_endianness() - Returns true if core and AHB have
370 * opposite endianness.
371 * @hsotg: Programming view of the DWC_otg controller.
373 static bool dwc2_check_core_endianness(struct dwc2_hsotg *hsotg)
377 snpsid = ioread32(hsotg->regs + GSNPSID);
378 if ((snpsid & GSNPSID_ID_MASK) == DWC2_OTG_ID ||
379 (snpsid & GSNPSID_ID_MASK) == DWC2_FS_IOT_ID ||
380 (snpsid & GSNPSID_ID_MASK) == DWC2_HS_IOT_ID)
386 * dwc2_check_core_version() - Check core version
388 * @hsotg: Programming view of the DWC_otg controller
391 int dwc2_check_core_version(struct dwc2_hsotg *hsotg)
393 struct dwc2_hw_params *hw = &hsotg->hw_params;
396 * Attempt to ensure this device is really a DWC_otg Controller.
397 * Read and verify the GSNPSID register contents. The value should be
398 * 0x45f4xxxx, 0x5531xxxx or 0x5532xxxx
401 hw->snpsid = dwc2_readl(hsotg, GSNPSID);
402 if ((hw->snpsid & GSNPSID_ID_MASK) != DWC2_OTG_ID &&
403 (hw->snpsid & GSNPSID_ID_MASK) != DWC2_FS_IOT_ID &&
404 (hw->snpsid & GSNPSID_ID_MASK) != DWC2_HS_IOT_ID) {
405 dev_err(hsotg->dev, "Bad value for GSNPSID: 0x%08x\n",
410 dev_dbg(hsotg->dev, "Core Release: %1x.%1x%1x%1x (snpsid=%x)\n",
411 hw->snpsid >> 12 & 0xf, hw->snpsid >> 8 & 0xf,
412 hw->snpsid >> 4 & 0xf, hw->snpsid & 0xf, hw->snpsid);
417 * dwc2_driver_probe() - Called when the DWC_otg core is bound to the DWC_otg
420 * @dev: Platform device
422 * This routine creates the driver components required to control the device
423 * (core, HCD, and PCD) and initializes the device. The driver components are
424 * stored in a dwc2_hsotg structure. A reference to the dwc2_hsotg is saved
425 * in the device private data. This allows the driver to access the dwc2_hsotg
426 * structure on subsequent calls to driver methods for this device.
428 static int dwc2_driver_probe(struct platform_device *dev)
430 struct dwc2_hsotg *hsotg;
431 struct resource *res;
434 hsotg = devm_kzalloc(&dev->dev, sizeof(*hsotg), GFP_KERNEL);
438 hsotg->dev = &dev->dev;
441 * Use reasonable defaults so platforms don't have to provide these.
443 if (!dev->dev.dma_mask)
444 dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
445 retval = dma_set_coherent_mask(&dev->dev, DMA_BIT_MASK(32));
447 dev_err(&dev->dev, "can't set coherent DMA mask: %d\n", retval);
451 hsotg->regs = devm_platform_get_and_ioremap_resource(dev, 0, &res);
452 if (IS_ERR(hsotg->regs))
453 return PTR_ERR(hsotg->regs);
455 dev_dbg(&dev->dev, "mapped PA %08lx to VA %p\n",
456 (unsigned long)res->start, hsotg->regs);
458 retval = dwc2_lowlevel_hw_init(hsotg);
462 spin_lock_init(&hsotg->lock);
464 hsotg->irq = platform_get_irq(dev, 0);
468 dev_dbg(hsotg->dev, "registering common handler for irq%d\n",
470 retval = devm_request_irq(hsotg->dev, hsotg->irq,
471 dwc2_handle_common_intr, IRQF_SHARED,
472 dev_name(hsotg->dev), hsotg);
476 hsotg->vbus_supply = devm_regulator_get_optional(hsotg->dev, "vbus");
477 if (IS_ERR(hsotg->vbus_supply)) {
478 retval = PTR_ERR(hsotg->vbus_supply);
479 hsotg->vbus_supply = NULL;
480 if (retval != -ENODEV)
484 retval = dwc2_lowlevel_hw_enable(hsotg);
488 hsotg->needs_byte_swap = dwc2_check_core_endianness(hsotg);
490 retval = dwc2_get_dr_mode(hsotg);
494 hsotg->need_phy_for_wake =
495 of_property_read_bool(dev->dev.of_node,
496 "snps,need-phy-for-wake");
499 * Before performing any core related operations
500 * check core version.
502 retval = dwc2_check_core_version(hsotg);
507 * Reset before dwc2_get_hwparams() then it could get power-on real
508 * reset value form registers.
510 retval = dwc2_core_reset(hsotg, false);
514 /* Detect config values from hardware */
515 retval = dwc2_get_hwparams(hsotg);
520 * For OTG cores, set the force mode bits to reflect the value
521 * of dr_mode. Force mode bits should not be touched at any
522 * other time after this.
524 dwc2_force_dr_mode(hsotg);
526 retval = dwc2_init_params(hsotg);
530 if (hsotg->params.activate_stm_id_vb_detection) {
533 hsotg->usb33d = devm_regulator_get(hsotg->dev, "usb33d");
534 if (IS_ERR(hsotg->usb33d)) {
535 retval = PTR_ERR(hsotg->usb33d);
536 dev_err_probe(hsotg->dev, retval, "failed to request usb33d supply\n");
539 retval = regulator_enable(hsotg->usb33d);
541 dev_err_probe(hsotg->dev, retval, "failed to enable usb33d supply\n");
545 ggpio = dwc2_readl(hsotg, GGPIO);
546 ggpio |= GGPIO_STM32_OTG_GCCFG_IDEN;
547 ggpio |= GGPIO_STM32_OTG_GCCFG_VBDEN;
548 dwc2_writel(hsotg, ggpio, GGPIO);
550 /* ID/VBUS detection startup time */
551 usleep_range(5000, 7000);
554 retval = dwc2_drd_init(hsotg);
556 dev_err_probe(hsotg->dev, retval, "failed to initialize dual-role\n");
560 if (hsotg->dr_mode != USB_DR_MODE_HOST) {
561 retval = dwc2_gadget_init(hsotg);
564 hsotg->gadget_enabled = 1;
568 * If we need PHY for wakeup we must be wakeup capable.
569 * When we have a device that can wake without the PHY we
570 * can adjust this condition.
572 if (hsotg->need_phy_for_wake)
573 device_set_wakeup_capable(&dev->dev, true);
575 hsotg->reset_phy_on_wake =
576 of_property_read_bool(dev->dev.of_node,
577 "snps,reset-phy-on-wake");
578 if (hsotg->reset_phy_on_wake && !hsotg->phy) {
580 "Quirk reset-phy-on-wake only supports generic PHYs\n");
581 hsotg->reset_phy_on_wake = false;
584 if (hsotg->dr_mode != USB_DR_MODE_PERIPHERAL) {
585 retval = dwc2_hcd_init(hsotg);
587 if (hsotg->gadget_enabled)
588 dwc2_hsotg_remove(hsotg);
591 hsotg->hcd_enabled = 1;
594 platform_set_drvdata(dev, hsotg);
595 hsotg->hibernated = 0;
597 dwc2_debugfs_init(hsotg);
599 /* Gadget code manages lowlevel hw on its own */
600 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
601 dwc2_lowlevel_hw_disable(hsotg);
603 #if IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL) || \
604 IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
605 /* Postponed adding a new gadget to the udc class driver list */
606 if (hsotg->gadget_enabled) {
607 retval = usb_add_gadget_udc(hsotg->dev, &hsotg->gadget);
609 hsotg->gadget.udc = NULL;
610 dwc2_hsotg_remove(hsotg);
614 #endif /* CONFIG_USB_DWC2_PERIPHERAL || CONFIG_USB_DWC2_DUAL_ROLE */
617 #if IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL) || \
618 IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
620 dwc2_debugfs_exit(hsotg);
621 if (hsotg->hcd_enabled)
622 dwc2_hcd_remove(hsotg);
625 dwc2_drd_exit(hsotg);
628 if (hsotg->params.activate_stm_id_vb_detection)
629 regulator_disable(hsotg->usb33d);
631 if (hsotg->ll_hw_enabled)
632 dwc2_lowlevel_hw_disable(hsotg);
636 static int __maybe_unused dwc2_suspend(struct device *dev)
638 struct dwc2_hsotg *dwc2 = dev_get_drvdata(dev);
639 bool is_device_mode = dwc2_is_device_mode(dwc2);
643 dwc2_hsotg_suspend(dwc2);
645 dwc2_drd_suspend(dwc2);
647 if (dwc2->params.activate_stm_id_vb_detection) {
652 * Need to force the mode to the current mode to avoid Mode
653 * Mismatch Interrupt when ID detection will be disabled.
655 dwc2_force_mode(dwc2, !is_device_mode);
657 spin_lock_irqsave(&dwc2->lock, flags);
658 gotgctl = dwc2_readl(dwc2, GOTGCTL);
659 /* bypass debounce filter, enable overrides */
660 gotgctl |= GOTGCTL_DBNCE_FLTR_BYPASS;
661 gotgctl |= GOTGCTL_BVALOEN | GOTGCTL_AVALOEN;
662 /* Force A / B session if needed */
663 if (gotgctl & GOTGCTL_ASESVLD)
664 gotgctl |= GOTGCTL_AVALOVAL;
665 if (gotgctl & GOTGCTL_BSESVLD)
666 gotgctl |= GOTGCTL_BVALOVAL;
667 dwc2_writel(dwc2, gotgctl, GOTGCTL);
668 spin_unlock_irqrestore(&dwc2->lock, flags);
670 ggpio = dwc2_readl(dwc2, GGPIO);
671 ggpio &= ~GGPIO_STM32_OTG_GCCFG_IDEN;
672 ggpio &= ~GGPIO_STM32_OTG_GCCFG_VBDEN;
673 dwc2_writel(dwc2, ggpio, GGPIO);
675 regulator_disable(dwc2->usb33d);
678 if (dwc2->ll_hw_enabled &&
679 (is_device_mode || dwc2_host_can_poweroff_phy(dwc2))) {
680 ret = __dwc2_lowlevel_hw_disable(dwc2);
681 dwc2->phy_off_for_suspend = true;
687 static int __maybe_unused dwc2_resume(struct device *dev)
689 struct dwc2_hsotg *dwc2 = dev_get_drvdata(dev);
692 if (dwc2->phy_off_for_suspend && dwc2->ll_hw_enabled) {
693 ret = __dwc2_lowlevel_hw_enable(dwc2);
697 dwc2->phy_off_for_suspend = false;
699 if (dwc2->params.activate_stm_id_vb_detection) {
703 ret = regulator_enable(dwc2->usb33d);
707 ggpio = dwc2_readl(dwc2, GGPIO);
708 ggpio |= GGPIO_STM32_OTG_GCCFG_IDEN;
709 ggpio |= GGPIO_STM32_OTG_GCCFG_VBDEN;
710 dwc2_writel(dwc2, ggpio, GGPIO);
712 /* ID/VBUS detection startup time */
713 usleep_range(5000, 7000);
715 spin_lock_irqsave(&dwc2->lock, flags);
716 gotgctl = dwc2_readl(dwc2, GOTGCTL);
717 gotgctl &= ~GOTGCTL_DBNCE_FLTR_BYPASS;
718 gotgctl &= ~(GOTGCTL_BVALOEN | GOTGCTL_AVALOEN |
719 GOTGCTL_BVALOVAL | GOTGCTL_AVALOVAL);
720 dwc2_writel(dwc2, gotgctl, GOTGCTL);
721 spin_unlock_irqrestore(&dwc2->lock, flags);
724 if (!dwc2->role_sw) {
725 /* Need to restore FORCEDEVMODE/FORCEHOSTMODE */
726 dwc2_force_dr_mode(dwc2);
728 dwc2_drd_resume(dwc2);
731 if (dwc2_is_device_mode(dwc2))
732 ret = dwc2_hsotg_resume(dwc2);
737 static const struct dev_pm_ops dwc2_dev_pm_ops = {
738 SET_SYSTEM_SLEEP_PM_OPS(dwc2_suspend, dwc2_resume)
741 static struct platform_driver dwc2_platform_driver = {
743 .name = dwc2_driver_name,
744 .of_match_table = dwc2_of_match_table,
745 .acpi_match_table = ACPI_PTR(dwc2_acpi_match),
746 .pm = &dwc2_dev_pm_ops,
748 .probe = dwc2_driver_probe,
749 .remove = dwc2_driver_remove,
750 .shutdown = dwc2_driver_shutdown,
753 module_platform_driver(dwc2_platform_driver);