1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2016 MediaTek Inc.
5 * Author: Chunfeng Yun <chunfeng.yun@mediatek.com>
8 #include <linux/dma-mapping.h>
9 #include <linux/iopoll.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/of_address.h>
13 #include <linux/of_irq.h>
14 #include <linux/platform_device.h>
15 #include <linux/pm_wakeirq.h>
16 #include <linux/reset.h>
20 #include "mtu3_debug.h"
22 /* u2-port0 should be powered on and enabled; */
23 int ssusb_check_clocks(struct ssusb_mtk *ssusb, u32 ex_clks)
25 void __iomem *ibase = ssusb->ippc_base;
29 check_val = ex_clks | SSUSB_SYS125_RST_B_STS | SSUSB_SYSPLL_STABLE |
32 ret = readl_poll_timeout(ibase + U3D_SSUSB_IP_PW_STS1, value,
33 (check_val == (value & check_val)), 100, 20000);
35 dev_err(ssusb->dev, "clks of sts1 are not stable!\n");
39 ret = readl_poll_timeout(ibase + U3D_SSUSB_IP_PW_STS2, value,
40 (value & SSUSB_U2_MAC_SYS_RST_B_STS), 100, 10000);
42 dev_err(ssusb->dev, "mac2 clock is not stable\n");
49 static int wait_for_ip_sleep(struct ssusb_mtk *ssusb)
51 bool sleep_check = true;
56 sleep_check = ssusb_gadget_ip_sleep_check(ssusb);
61 /* wait for ip enter sleep mode */
62 ret = readl_poll_timeout(ssusb->ippc_base + U3D_SSUSB_IP_PW_STS1, value,
63 (value & SSUSB_IP_SLEEP_STS), 100, 100000);
65 dev_err(ssusb->dev, "ip sleep failed!!!\n");
68 /* workaround: avoid wrong wakeup signal latch for some soc */
69 usleep_range(100, 200);
75 static int ssusb_phy_init(struct ssusb_mtk *ssusb)
80 for (i = 0; i < ssusb->num_phys; i++) {
81 ret = phy_init(ssusb->phys[i]);
89 phy_exit(ssusb->phys[i - 1]);
94 static int ssusb_phy_exit(struct ssusb_mtk *ssusb)
98 for (i = 0; i < ssusb->num_phys; i++)
99 phy_exit(ssusb->phys[i]);
104 static int ssusb_phy_power_on(struct ssusb_mtk *ssusb)
109 for (i = 0; i < ssusb->num_phys; i++) {
110 ret = phy_power_on(ssusb->phys[i]);
118 phy_power_off(ssusb->phys[i - 1]);
123 static void ssusb_phy_power_off(struct ssusb_mtk *ssusb)
127 for (i = 0; i < ssusb->num_phys; i++)
128 phy_power_off(ssusb->phys[i]);
131 static int ssusb_rscs_init(struct ssusb_mtk *ssusb)
135 ret = regulator_enable(ssusb->vusb33);
137 dev_err(ssusb->dev, "failed to enable vusb33\n");
141 ret = clk_bulk_prepare_enable(BULK_CLKS_CNT, ssusb->clks);
145 ret = ssusb_phy_init(ssusb);
147 dev_err(ssusb->dev, "failed to init phy\n");
151 ret = ssusb_phy_power_on(ssusb);
153 dev_err(ssusb->dev, "failed to power on phy\n");
160 ssusb_phy_exit(ssusb);
162 clk_bulk_disable_unprepare(BULK_CLKS_CNT, ssusb->clks);
164 regulator_disable(ssusb->vusb33);
169 static void ssusb_rscs_exit(struct ssusb_mtk *ssusb)
171 clk_bulk_disable_unprepare(BULK_CLKS_CNT, ssusb->clks);
172 regulator_disable(ssusb->vusb33);
173 ssusb_phy_power_off(ssusb);
174 ssusb_phy_exit(ssusb);
177 static void ssusb_ip_sw_reset(struct ssusb_mtk *ssusb)
179 /* reset whole ip (xhci & u3d) */
180 mtu3_setbits(ssusb->ippc_base, U3D_SSUSB_IP_PW_CTRL0, SSUSB_IP_SW_RST);
182 mtu3_clrbits(ssusb->ippc_base, U3D_SSUSB_IP_PW_CTRL0, SSUSB_IP_SW_RST);
185 * device ip may be powered on in firmware/BROM stage before entering
187 * power down device ip, otherwise ip-sleep will fail when working as
190 mtu3_setbits(ssusb->ippc_base, U3D_SSUSB_IP_PW_CTRL2, SSUSB_IP_DEV_PDN);
193 static void ssusb_u3_drd_check(struct ssusb_mtk *ssusb)
195 struct otg_switch_mtk *otg_sx = &ssusb->otg_switch;
200 /* u3 port0 is disabled */
201 if (ssusb->u3p_dis_msk & BIT(0)) {
202 otg_sx->is_u3_drd = false;
206 value = mtu3_readl(ssusb->ippc_base, U3D_SSUSB_IP_DEV_CAP);
207 dev_u3p_num = SSUSB_IP_DEV_U3_PORT_NUM(value);
209 value = mtu3_readl(ssusb->ippc_base, U3D_SSUSB_IP_XHCI_CAP);
210 host_u3p_num = SSUSB_IP_XHCI_U3_PORT_NUM(value);
212 otg_sx->is_u3_drd = !!(dev_u3p_num && host_u3p_num);
215 dev_info(ssusb->dev, "usb3-drd: %d\n", otg_sx->is_u3_drd);
218 static int get_ssusb_rscs(struct platform_device *pdev, struct ssusb_mtk *ssusb)
220 struct device_node *node = pdev->dev.of_node;
221 struct otg_switch_mtk *otg_sx = &ssusb->otg_switch;
222 struct clk_bulk_data *clks = ssusb->clks;
223 struct device *dev = &pdev->dev;
227 ssusb->vusb33 = devm_regulator_get(dev, "vusb33");
228 if (IS_ERR(ssusb->vusb33)) {
229 dev_err(dev, "failed to get vusb33\n");
230 return PTR_ERR(ssusb->vusb33);
233 clks[0].id = "sys_ck";
234 clks[1].id = "ref_ck";
235 clks[2].id = "mcu_ck";
236 clks[3].id = "dma_ck";
237 ret = devm_clk_bulk_get_optional(dev, BULK_CLKS_CNT, clks);
241 ssusb->num_phys = of_count_phandle_with_args(node,
242 "phys", "#phy-cells");
243 if (ssusb->num_phys > 0) {
244 ssusb->phys = devm_kcalloc(dev, ssusb->num_phys,
245 sizeof(*ssusb->phys), GFP_KERNEL);
252 for (i = 0; i < ssusb->num_phys; i++) {
253 ssusb->phys[i] = devm_of_phy_get_by_index(dev, node, i);
254 if (IS_ERR(ssusb->phys[i])) {
255 dev_err(dev, "failed to get phy-%d\n", i);
256 return PTR_ERR(ssusb->phys[i]);
260 ssusb->ippc_base = devm_platform_ioremap_resource_byname(pdev, "ippc");
261 if (IS_ERR(ssusb->ippc_base))
262 return PTR_ERR(ssusb->ippc_base);
264 ssusb->wakeup_irq = platform_get_irq_byname_optional(pdev, "wakeup");
265 if (ssusb->wakeup_irq == -EPROBE_DEFER)
266 return ssusb->wakeup_irq;
268 ssusb->dr_mode = usb_get_dr_mode(dev);
269 if (ssusb->dr_mode == USB_DR_MODE_UNKNOWN)
270 ssusb->dr_mode = USB_DR_MODE_OTG;
272 of_property_read_u32(node, "mediatek,u3p-dis-msk", &ssusb->u3p_dis_msk);
274 if (ssusb->dr_mode == USB_DR_MODE_PERIPHERAL)
277 /* if host role is supported */
278 ret = ssusb_wakeup_of_property_parse(ssusb, node);
280 dev_err(dev, "failed to parse uwk property\n");
284 /* optional property, ignore the error if it does not exist */
285 of_property_read_u32(node, "mediatek,u2p-dis-msk",
286 &ssusb->u2p_dis_msk);
288 otg_sx->vbus = devm_regulator_get(dev, "vbus");
289 if (IS_ERR(otg_sx->vbus)) {
290 dev_err(dev, "failed to get vbus\n");
291 return PTR_ERR(otg_sx->vbus);
294 if (ssusb->dr_mode == USB_DR_MODE_HOST)
297 /* if dual-role mode is supported */
298 otg_sx->manual_drd_enabled =
299 of_property_read_bool(node, "enable-manual-drd");
300 otg_sx->role_sw_used = of_property_read_bool(node, "usb-role-switch");
302 /* can't disable port0 when use dual-role mode */
303 ssusb->u2p_dis_msk &= ~0x1;
305 if (otg_sx->role_sw_used || otg_sx->manual_drd_enabled)
308 if (of_property_read_bool(node, "extcon")) {
309 otg_sx->edev = extcon_get_edev_by_phandle(ssusb->dev, 0);
310 if (IS_ERR(otg_sx->edev)) {
311 return dev_err_probe(dev, PTR_ERR(otg_sx->edev),
312 "couldn't get extcon device\n");
317 dev_info(dev, "dr_mode: %d, drd: %s\n", ssusb->dr_mode,
318 otg_sx->manual_drd_enabled ? "manual" : "auto");
319 dev_info(dev, "u2p_dis_msk: %x, u3p_dis_msk: %x\n",
320 ssusb->u2p_dis_msk, ssusb->u3p_dis_msk);
325 static int mtu3_probe(struct platform_device *pdev)
327 struct device_node *node = pdev->dev.of_node;
328 struct device *dev = &pdev->dev;
329 struct ssusb_mtk *ssusb;
332 /* all elements are set to ZERO as default value */
333 ssusb = devm_kzalloc(dev, sizeof(*ssusb), GFP_KERNEL);
337 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
339 dev_err(dev, "No suitable DMA config available\n");
343 platform_set_drvdata(pdev, ssusb);
346 ret = get_ssusb_rscs(pdev, ssusb);
350 ssusb_debugfs_create_root(ssusb);
352 /* enable power domain */
353 pm_runtime_set_active(dev);
354 pm_runtime_use_autosuspend(dev);
355 pm_runtime_set_autosuspend_delay(dev, 4000);
356 pm_runtime_enable(dev);
357 pm_runtime_get_sync(dev);
359 ret = ssusb_rscs_init(ssusb);
363 if (ssusb->wakeup_irq > 0) {
364 ret = dev_pm_set_dedicated_wake_irq_reverse(dev, ssusb->wakeup_irq);
366 dev_err(dev, "failed to set wakeup irq %d\n", ssusb->wakeup_irq);
369 dev_info(dev, "wakeup irq %d\n", ssusb->wakeup_irq);
372 ret = device_reset_optional(dev);
374 dev_err_probe(dev, ret, "failed to reset controller\n");
378 ssusb_ip_sw_reset(ssusb);
379 ssusb_u3_drd_check(ssusb);
381 if (IS_ENABLED(CONFIG_USB_MTU3_HOST))
382 ssusb->dr_mode = USB_DR_MODE_HOST;
383 else if (IS_ENABLED(CONFIG_USB_MTU3_GADGET))
384 ssusb->dr_mode = USB_DR_MODE_PERIPHERAL;
386 /* default as host */
387 ssusb->is_host = !(ssusb->dr_mode == USB_DR_MODE_PERIPHERAL);
389 switch (ssusb->dr_mode) {
390 case USB_DR_MODE_PERIPHERAL:
391 ret = ssusb_gadget_init(ssusb);
393 dev_err(dev, "failed to initialize gadget\n");
397 case USB_DR_MODE_HOST:
398 ret = ssusb_host_init(ssusb, node);
400 dev_err(dev, "failed to initialize host\n");
404 case USB_DR_MODE_OTG:
405 ret = ssusb_gadget_init(ssusb);
407 dev_err(dev, "failed to initialize gadget\n");
411 ret = ssusb_host_init(ssusb, node);
413 dev_err(dev, "failed to initialize host\n");
417 ret = ssusb_otg_switch_init(ssusb);
419 dev_err(dev, "failed to initialize switch\n");
424 dev_err(dev, "unsupported mode: %d\n", ssusb->dr_mode);
429 device_enable_async_suspend(dev);
430 pm_runtime_mark_last_busy(dev);
431 pm_runtime_put_autosuspend(dev);
432 pm_runtime_forbid(dev);
437 ssusb_host_exit(ssusb);
439 ssusb_gadget_exit(ssusb);
441 ssusb_rscs_exit(ssusb);
443 pm_runtime_put_noidle(dev);
444 pm_runtime_disable(dev);
445 ssusb_debugfs_remove_root(ssusb);
450 static int mtu3_remove(struct platform_device *pdev)
452 struct ssusb_mtk *ssusb = platform_get_drvdata(pdev);
454 pm_runtime_get_sync(&pdev->dev);
456 switch (ssusb->dr_mode) {
457 case USB_DR_MODE_PERIPHERAL:
458 ssusb_gadget_exit(ssusb);
460 case USB_DR_MODE_HOST:
461 ssusb_host_exit(ssusb);
463 case USB_DR_MODE_OTG:
464 ssusb_otg_switch_exit(ssusb);
465 ssusb_gadget_exit(ssusb);
466 ssusb_host_exit(ssusb);
472 ssusb_rscs_exit(ssusb);
473 ssusb_debugfs_remove_root(ssusb);
474 pm_runtime_disable(&pdev->dev);
475 pm_runtime_put_noidle(&pdev->dev);
476 pm_runtime_set_suspended(&pdev->dev);
481 static int resume_ip_and_ports(struct ssusb_mtk *ssusb, pm_message_t msg)
483 switch (ssusb->dr_mode) {
484 case USB_DR_MODE_PERIPHERAL:
485 ssusb_gadget_resume(ssusb, msg);
487 case USB_DR_MODE_HOST:
488 ssusb_host_resume(ssusb, false);
490 case USB_DR_MODE_OTG:
491 ssusb_host_resume(ssusb, !ssusb->is_host);
493 ssusb_gadget_resume(ssusb, msg);
503 static int mtu3_suspend_common(struct device *dev, pm_message_t msg)
505 struct ssusb_mtk *ssusb = dev_get_drvdata(dev);
508 dev_dbg(dev, "%s\n", __func__);
510 switch (ssusb->dr_mode) {
511 case USB_DR_MODE_PERIPHERAL:
512 ret = ssusb_gadget_suspend(ssusb, msg);
517 case USB_DR_MODE_HOST:
518 ssusb_host_suspend(ssusb);
520 case USB_DR_MODE_OTG:
521 if (!ssusb->is_host) {
522 ret = ssusb_gadget_suspend(ssusb, msg);
526 ssusb_host_suspend(ssusb);
532 ret = wait_for_ip_sleep(ssusb);
536 ssusb_phy_power_off(ssusb);
537 clk_bulk_disable_unprepare(BULK_CLKS_CNT, ssusb->clks);
538 ssusb_wakeup_set(ssusb, true);
542 resume_ip_and_ports(ssusb, msg);
547 static int mtu3_resume_common(struct device *dev, pm_message_t msg)
549 struct ssusb_mtk *ssusb = dev_get_drvdata(dev);
552 dev_dbg(dev, "%s\n", __func__);
554 ssusb_wakeup_set(ssusb, false);
555 ret = clk_bulk_prepare_enable(BULK_CLKS_CNT, ssusb->clks);
559 ret = ssusb_phy_power_on(ssusb);
563 return resume_ip_and_ports(ssusb, msg);
566 clk_bulk_disable_unprepare(BULK_CLKS_CNT, ssusb->clks);
571 static int __maybe_unused mtu3_suspend(struct device *dev)
573 return mtu3_suspend_common(dev, PMSG_SUSPEND);
576 static int __maybe_unused mtu3_resume(struct device *dev)
578 return mtu3_resume_common(dev, PMSG_SUSPEND);
581 static int __maybe_unused mtu3_runtime_suspend(struct device *dev)
583 if (!device_may_wakeup(dev))
586 return mtu3_suspend_common(dev, PMSG_AUTO_SUSPEND);
589 static int __maybe_unused mtu3_runtime_resume(struct device *dev)
591 if (!device_may_wakeup(dev))
594 return mtu3_resume_common(dev, PMSG_AUTO_SUSPEND);
597 static const struct dev_pm_ops mtu3_pm_ops = {
598 SET_SYSTEM_SLEEP_PM_OPS(mtu3_suspend, mtu3_resume)
599 SET_RUNTIME_PM_OPS(mtu3_runtime_suspend,
600 mtu3_runtime_resume, NULL)
603 #define DEV_PM_OPS (IS_ENABLED(CONFIG_PM) ? &mtu3_pm_ops : NULL)
605 static const struct of_device_id mtu3_of_match[] = {
606 {.compatible = "mediatek,mt8173-mtu3",},
607 {.compatible = "mediatek,mtu3",},
610 MODULE_DEVICE_TABLE(of, mtu3_of_match);
612 static struct platform_driver mtu3_driver = {
614 .remove = mtu3_remove,
616 .name = MTU3_DRIVER_NAME,
618 .of_match_table = mtu3_of_match,
621 module_platform_driver(mtu3_driver);
623 MODULE_AUTHOR("Chunfeng Yun <chunfeng.yun@mediatek.com>");
624 MODULE_LICENSE("GPL v2");
625 MODULE_DESCRIPTION("MediaTek USB3 DRD Controller Driver");