Lines Matching +full:mtk +full:- +full:xhci

1 // SPDX-License-Identifier: GPL-2.0
3 * MediaTek xHCI Host Controller Driver
10 #include <linux/dma-mapping.h>
23 #include "xhci.h"
24 #include "xhci-mtk.h"
60 /* xHCI CSR */
133 static void xhci_mtk_set_frame_interval(struct xhci_hcd_mtk *mtk) in xhci_mtk_set_frame_interval() argument
135 struct device *dev = mtk->dev; in xhci_mtk_set_frame_interval()
136 struct usb_hcd *hcd = mtk->hcd; in xhci_mtk_set_frame_interval()
139 if (!of_device_is_compatible(dev->of_node, "mediatek,mt8195-xhci")) in xhci_mtk_set_frame_interval()
142 value = readl(hcd->regs + HFCNTR_CFG); in xhci_mtk_set_frame_interval()
145 writel(value, hcd->regs + HFCNTR_CFG); in xhci_mtk_set_frame_interval()
147 value = readl(hcd->regs + LS_EOF_CFG); in xhci_mtk_set_frame_interval()
150 writel(value, hcd->regs + LS_EOF_CFG); in xhci_mtk_set_frame_interval()
152 value = readl(hcd->regs + FS_EOF_CFG); in xhci_mtk_set_frame_interval()
155 writel(value, hcd->regs + FS_EOF_CFG); in xhci_mtk_set_frame_interval()
157 value = readl(hcd->regs + SS_GEN1_EOF_CFG); in xhci_mtk_set_frame_interval()
160 writel(value, hcd->regs + SS_GEN1_EOF_CFG); in xhci_mtk_set_frame_interval()
162 value = readl(hcd->regs + SS_GEN2_EOF_CFG); in xhci_mtk_set_frame_interval()
165 writel(value, hcd->regs + SS_GEN2_EOF_CFG); in xhci_mtk_set_frame_interval()
168 static int xhci_mtk_host_enable(struct xhci_hcd_mtk *mtk) in xhci_mtk_host_enable() argument
170 struct mu3c_ippc_regs __iomem *ippc = mtk->ippc_regs; in xhci_mtk_host_enable()
176 if (!mtk->has_ippc) in xhci_mtk_host_enable()
180 value = readl(&ippc->ip_pw_ctr1); in xhci_mtk_host_enable()
182 writel(value, &ippc->ip_pw_ctr1); in xhci_mtk_host_enable()
185 for (i = 0; i < mtk->num_u3_ports; i++) { in xhci_mtk_host_enable()
186 if ((0x1 << i) & mtk->u3p_dis_msk) { in xhci_mtk_host_enable()
191 value = readl(&ippc->u3_ctrl_p[i]); in xhci_mtk_host_enable()
194 writel(value, &ippc->u3_ctrl_p[i]); in xhci_mtk_host_enable()
198 for (i = 0; i < mtk->num_u2_ports; i++) { in xhci_mtk_host_enable()
199 if (BIT(i) & mtk->u2p_dis_msk) in xhci_mtk_host_enable()
202 value = readl(&ippc->u2_ctrl_p[i]); in xhci_mtk_host_enable()
205 writel(value, &ippc->u2_ctrl_p[i]); in xhci_mtk_host_enable()
215 if (mtk->num_u3_ports > u3_ports_disabled) in xhci_mtk_host_enable()
218 ret = readl_poll_timeout(&ippc->ip_pw_sts1, value, in xhci_mtk_host_enable()
221 dev_err(mtk->dev, "clocks are not stable (0x%x)\n", value); in xhci_mtk_host_enable()
228 static int xhci_mtk_host_disable(struct xhci_hcd_mtk *mtk) in xhci_mtk_host_disable() argument
230 struct mu3c_ippc_regs __iomem *ippc = mtk->ippc_regs; in xhci_mtk_host_disable()
235 if (!mtk->has_ippc) in xhci_mtk_host_disable()
239 for (i = 0; i < mtk->num_u3_ports; i++) { in xhci_mtk_host_disable()
240 if ((0x1 << i) & mtk->u3p_dis_msk) in xhci_mtk_host_disable()
243 value = readl(&ippc->u3_ctrl_p[i]); in xhci_mtk_host_disable()
245 writel(value, &ippc->u3_ctrl_p[i]); in xhci_mtk_host_disable()
249 for (i = 0; i < mtk->num_u2_ports; i++) { in xhci_mtk_host_disable()
250 if (BIT(i) & mtk->u2p_dis_msk) in xhci_mtk_host_disable()
253 value = readl(&ippc->u2_ctrl_p[i]); in xhci_mtk_host_disable()
255 writel(value, &ippc->u2_ctrl_p[i]); in xhci_mtk_host_disable()
259 value = readl(&ippc->ip_pw_ctr1); in xhci_mtk_host_disable()
261 writel(value, &ippc->ip_pw_ctr1); in xhci_mtk_host_disable()
264 ret = readl_poll_timeout(&ippc->ip_pw_sts1, value, in xhci_mtk_host_disable()
267 dev_err(mtk->dev, "ip sleep failed!!!\n"); in xhci_mtk_host_disable()
274 static int xhci_mtk_ssusb_config(struct xhci_hcd_mtk *mtk) in xhci_mtk_ssusb_config() argument
276 struct mu3c_ippc_regs __iomem *ippc = mtk->ippc_regs; in xhci_mtk_ssusb_config()
279 if (!mtk->has_ippc) in xhci_mtk_ssusb_config()
283 value = readl(&ippc->ip_pw_ctr0); in xhci_mtk_ssusb_config()
285 writel(value, &ippc->ip_pw_ctr0); in xhci_mtk_ssusb_config()
287 value = readl(&ippc->ip_pw_ctr0); in xhci_mtk_ssusb_config()
289 writel(value, &ippc->ip_pw_ctr0); in xhci_mtk_ssusb_config()
292 * device ip is default power-on in fact in xhci_mtk_ssusb_config()
293 * power down device ip, otherwise ip-sleep will fail in xhci_mtk_ssusb_config()
295 value = readl(&ippc->ip_pw_ctr2); in xhci_mtk_ssusb_config()
297 writel(value, &ippc->ip_pw_ctr2); in xhci_mtk_ssusb_config()
299 value = readl(&ippc->ip_xhci_cap); in xhci_mtk_ssusb_config()
300 mtk->num_u3_ports = CAP_U3_PORT_NUM(value); in xhci_mtk_ssusb_config()
301 mtk->num_u2_ports = CAP_U2_PORT_NUM(value); in xhci_mtk_ssusb_config()
302 dev_dbg(mtk->dev, "%s u2p:%d, u3p:%d\n", __func__, in xhci_mtk_ssusb_config()
303 mtk->num_u2_ports, mtk->num_u3_ports); in xhci_mtk_ssusb_config()
305 return xhci_mtk_host_enable(mtk); in xhci_mtk_ssusb_config()
308 /* only clocks can be turn off for ip-sleep wakeup mode */
309 static void usb_wakeup_ip_sleep_set(struct xhci_hcd_mtk *mtk, bool enable) in usb_wakeup_ip_sleep_set() argument
313 switch (mtk->uwk_vers) { in usb_wakeup_ip_sleep_set()
315 reg = mtk->uwk_reg_base + PERI_WK_CTRL1; in usb_wakeup_ip_sleep_set()
320 reg = mtk->uwk_reg_base + PERI_WK_CTRL0; in usb_wakeup_ip_sleep_set()
325 reg = mtk->uwk_reg_base + PERI_WK_CTRL0; in usb_wakeup_ip_sleep_set()
330 reg = mtk->uwk_reg_base + PERI_WK_CTRL1_8195; in usb_wakeup_ip_sleep_set()
335 reg = mtk->uwk_reg_base + PERI_WK_CTRL0_8195; in usb_wakeup_ip_sleep_set()
340 reg = mtk->uwk_reg_base + PERI_WK_CTRL0_8195; in usb_wakeup_ip_sleep_set()
345 reg = mtk->uwk_reg_base + PERI_WK_CTRL0_8195; in usb_wakeup_ip_sleep_set()
350 reg = mtk->uwk_reg_base + PERI_SSUSB_SPM_CTRL; in usb_wakeup_ip_sleep_set()
357 regmap_update_bits(mtk->uwk, reg, msk, val); in usb_wakeup_ip_sleep_set()
360 static int usb_wakeup_of_property_parse(struct xhci_hcd_mtk *mtk, in usb_wakeup_of_property_parse() argument
367 mtk->uwk_en = of_property_read_bool(dn, "wakeup-source"); in usb_wakeup_of_property_parse()
368 if (!mtk->uwk_en) in usb_wakeup_of_property_parse()
372 "mediatek,syscon-wakeup", 2, 0, &args); in usb_wakeup_of_property_parse()
376 mtk->uwk_reg_base = args.args[0]; in usb_wakeup_of_property_parse()
377 mtk->uwk_vers = args.args[1]; in usb_wakeup_of_property_parse()
378 mtk->uwk = syscon_node_to_regmap(args.np); in usb_wakeup_of_property_parse()
380 dev_info(mtk->dev, "uwk - reg:0x%x, version:%d\n", in usb_wakeup_of_property_parse()
381 mtk->uwk_reg_base, mtk->uwk_vers); in usb_wakeup_of_property_parse()
383 return PTR_ERR_OR_ZERO(mtk->uwk); in usb_wakeup_of_property_parse()
386 static void usb_wakeup_set(struct xhci_hcd_mtk *mtk, bool enable) in usb_wakeup_set() argument
388 if (mtk->uwk_en) in usb_wakeup_set()
389 usb_wakeup_ip_sleep_set(mtk, enable); in usb_wakeup_set()
392 static int xhci_mtk_clks_get(struct xhci_hcd_mtk *mtk) in xhci_mtk_clks_get() argument
394 struct clk_bulk_data *clks = mtk->clks; in xhci_mtk_clks_get()
403 return devm_clk_bulk_get_optional(mtk->dev, BULK_CLKS_NUM, clks); in xhci_mtk_clks_get()
406 static int xhci_mtk_vregs_get(struct xhci_hcd_mtk *mtk) in xhci_mtk_vregs_get() argument
408 struct regulator_bulk_data *supplies = mtk->supplies; in xhci_mtk_vregs_get()
413 return devm_regulator_bulk_get(mtk->dev, BULK_VREGS_NUM, supplies); in xhci_mtk_vregs_get()
416 static void xhci_mtk_quirks(struct device *dev, struct xhci_hcd *xhci) in xhci_mtk_quirks() argument
418 struct usb_hcd *hcd = xhci_to_hcd(xhci); in xhci_mtk_quirks()
419 struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd); in xhci_mtk_quirks() local
421 xhci->quirks |= XHCI_MTK_HOST; in xhci_mtk_quirks()
423 * MTK host controller gives a spurious successful event after a in xhci_mtk_quirks()
426 xhci->quirks |= XHCI_SPURIOUS_SUCCESS; in xhci_mtk_quirks()
427 if (mtk->lpm_support) in xhci_mtk_quirks()
428 xhci->quirks |= XHCI_LPM_SUPPORT; in xhci_mtk_quirks()
429 if (mtk->u2_lpm_disable) in xhci_mtk_quirks()
430 xhci->quirks |= XHCI_HW_LPM_DISABLE; in xhci_mtk_quirks()
433 * MTK xHCI 0.96: PSA is 1 by default even if doesn't support stream, in xhci_mtk_quirks()
436 if (xhci->hci_version < 0x100 && HCC_MAX_PSA(xhci->hcc_params) == 4) in xhci_mtk_quirks()
437 xhci->quirks |= XHCI_BROKEN_STREAMS; in xhci_mtk_quirks()
443 struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd); in xhci_mtk_setup() local
447 ret = xhci_mtk_ssusb_config(mtk); in xhci_mtk_setup()
452 xhci_mtk_set_frame_interval(mtk); in xhci_mtk_setup()
460 ret = xhci_mtk_sch_init(mtk); in xhci_mtk_setup()
477 struct device *dev = &pdev->dev; in xhci_mtk_probe()
478 struct device_node *node = dev->of_node; in xhci_mtk_probe()
479 struct xhci_hcd_mtk *mtk; in xhci_mtk_probe() local
481 struct xhci_hcd *xhci; in xhci_mtk_probe() local
485 int ret = -ENODEV; in xhci_mtk_probe()
490 return -ENODEV; in xhci_mtk_probe()
493 mtk = devm_kzalloc(dev, sizeof(*mtk), GFP_KERNEL); in xhci_mtk_probe()
494 if (!mtk) in xhci_mtk_probe()
495 return -ENOMEM; in xhci_mtk_probe()
497 mtk->dev = dev; in xhci_mtk_probe()
499 ret = xhci_mtk_vregs_get(mtk); in xhci_mtk_probe()
503 ret = xhci_mtk_clks_get(mtk); in xhci_mtk_probe()
509 if (irq == -EPROBE_DEFER) in xhci_mtk_probe()
519 if (wakeup_irq == -EPROBE_DEFER) in xhci_mtk_probe()
522 mtk->lpm_support = of_property_read_bool(node, "usb3-lpm-capable"); in xhci_mtk_probe()
523 mtk->u2_lpm_disable = of_property_read_bool(node, "usb2-lpm-disable"); in xhci_mtk_probe()
525 of_property_read_u32(node, "mediatek,u3p-dis-msk", in xhci_mtk_probe()
526 &mtk->u3p_dis_msk); in xhci_mtk_probe()
527 of_property_read_u32(node, "mediatek,u2p-dis-msk", in xhci_mtk_probe()
528 &mtk->u2p_dis_msk); in xhci_mtk_probe()
530 ret = usb_wakeup_of_property_parse(mtk, node); in xhci_mtk_probe()
542 ret = regulator_bulk_enable(BULK_VREGS_NUM, mtk->supplies); in xhci_mtk_probe()
546 ret = clk_bulk_prepare_enable(BULK_CLKS_NUM, mtk->clks); in xhci_mtk_probe()
558 ret = -ENOMEM; in xhci_mtk_probe()
564 * Swap it with mtk HCD. in xhci_mtk_probe()
566 mtk->hcd = platform_get_drvdata(pdev); in xhci_mtk_probe()
567 platform_set_drvdata(pdev, mtk); in xhci_mtk_probe()
570 hcd->regs = devm_ioremap_resource(dev, res); in xhci_mtk_probe()
571 if (IS_ERR(hcd->regs)) { in xhci_mtk_probe()
572 ret = PTR_ERR(hcd->regs); in xhci_mtk_probe()
575 hcd->rsrc_start = res->start; in xhci_mtk_probe()
576 hcd->rsrc_len = resource_size(res); in xhci_mtk_probe()
580 mtk->ippc_regs = devm_ioremap_resource(dev, res); in xhci_mtk_probe()
581 if (IS_ERR(mtk->ippc_regs)) { in xhci_mtk_probe()
582 ret = PTR_ERR(mtk->ippc_regs); in xhci_mtk_probe()
585 mtk->has_ippc = true; in xhci_mtk_probe()
591 xhci = hcd_to_xhci(hcd); in xhci_mtk_probe()
592 xhci->main_hcd = hcd; in xhci_mtk_probe()
593 xhci->allow_single_roothub = 1; in xhci_mtk_probe()
598 * the xHCI spec on MTK's controller. in xhci_mtk_probe()
600 xhci->imod_interval = 5000; in xhci_mtk_probe()
601 device_property_read_u32(dev, "imod-interval-ns", &xhci->imod_interval); in xhci_mtk_probe()
607 if (!xhci_has_one_roothub(xhci)) { in xhci_mtk_probe()
608 xhci->shared_hcd = usb_create_shared_hcd(driver, dev, in xhci_mtk_probe()
610 if (!xhci->shared_hcd) { in xhci_mtk_probe()
611 ret = -ENOMEM; in xhci_mtk_probe()
616 usb3_hcd = xhci_get_usb3_hcd(xhci); in xhci_mtk_probe()
617 if (usb3_hcd && HCC_MAX_PSA(xhci->hcc_params) >= 4 && in xhci_mtk_probe()
618 !(xhci->quirks & XHCI_BROKEN_STREAMS)) in xhci_mtk_probe()
619 usb3_hcd->can_do_streams = 1; in xhci_mtk_probe()
621 if (xhci->shared_hcd) { in xhci_mtk_probe()
622 ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED); in xhci_mtk_probe()
644 usb_remove_hcd(xhci->shared_hcd); in xhci_mtk_probe()
647 usb_put_hcd(xhci->shared_hcd); in xhci_mtk_probe()
650 xhci_mtk_sch_exit(mtk); in xhci_mtk_probe()
660 clk_bulk_disable_unprepare(BULK_CLKS_NUM, mtk->clks); in xhci_mtk_probe()
663 regulator_bulk_disable(BULK_VREGS_NUM, mtk->supplies); in xhci_mtk_probe()
673 struct xhci_hcd_mtk *mtk = platform_get_drvdata(pdev); in xhci_mtk_remove() local
674 struct usb_hcd *hcd = mtk->hcd; in xhci_mtk_remove()
675 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_mtk_remove() local
676 struct usb_hcd *shared_hcd = xhci->shared_hcd; in xhci_mtk_remove()
677 struct device *dev = &pdev->dev; in xhci_mtk_remove()
680 xhci->xhc_state |= XHCI_STATE_REMOVING; in xhci_mtk_remove()
686 xhci->shared_hcd = NULL; in xhci_mtk_remove()
694 xhci_mtk_sch_exit(mtk); in xhci_mtk_remove()
695 clk_bulk_disable_unprepare(BULK_CLKS_NUM, mtk->clks); in xhci_mtk_remove()
696 regulator_bulk_disable(BULK_VREGS_NUM, mtk->supplies); in xhci_mtk_remove()
705 struct xhci_hcd_mtk *mtk = dev_get_drvdata(dev); in xhci_mtk_suspend() local
706 struct usb_hcd *hcd = mtk->hcd; in xhci_mtk_suspend()
707 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_mtk_suspend() local
708 struct usb_hcd *shared_hcd = xhci->shared_hcd; in xhci_mtk_suspend()
711 xhci_dbg(xhci, "%s: stop port polling\n", __func__); in xhci_mtk_suspend()
712 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags); in xhci_mtk_suspend()
713 del_timer_sync(&hcd->rh_timer); in xhci_mtk_suspend()
715 clear_bit(HCD_FLAG_POLL_RH, &shared_hcd->flags); in xhci_mtk_suspend()
716 del_timer_sync(&shared_hcd->rh_timer); in xhci_mtk_suspend()
719 ret = xhci_mtk_host_disable(mtk); in xhci_mtk_suspend()
723 clk_bulk_disable_unprepare(BULK_CLKS_NUM, mtk->clks); in xhci_mtk_suspend()
724 usb_wakeup_set(mtk, true); in xhci_mtk_suspend()
728 xhci_dbg(xhci, "%s: restart port polling\n", __func__); in xhci_mtk_suspend()
730 set_bit(HCD_FLAG_POLL_RH, &shared_hcd->flags); in xhci_mtk_suspend()
733 set_bit(HCD_FLAG_POLL_RH, &hcd->flags); in xhci_mtk_suspend()
740 struct xhci_hcd_mtk *mtk = dev_get_drvdata(dev); in xhci_mtk_resume() local
741 struct usb_hcd *hcd = mtk->hcd; in xhci_mtk_resume()
742 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_mtk_resume() local
743 struct usb_hcd *shared_hcd = xhci->shared_hcd; in xhci_mtk_resume()
746 usb_wakeup_set(mtk, false); in xhci_mtk_resume()
747 ret = clk_bulk_prepare_enable(BULK_CLKS_NUM, mtk->clks); in xhci_mtk_resume()
751 ret = xhci_mtk_host_enable(mtk); in xhci_mtk_resume()
755 xhci_dbg(xhci, "%s: restart port polling\n", __func__); in xhci_mtk_resume()
757 set_bit(HCD_FLAG_POLL_RH, &shared_hcd->flags); in xhci_mtk_resume()
760 set_bit(HCD_FLAG_POLL_RH, &hcd->flags); in xhci_mtk_resume()
765 clk_bulk_disable_unprepare(BULK_CLKS_NUM, mtk->clks); in xhci_mtk_resume()
767 usb_wakeup_set(mtk, true); in xhci_mtk_resume()
773 struct xhci_hcd_mtk *mtk = dev_get_drvdata(dev); in xhci_mtk_runtime_suspend() local
774 struct xhci_hcd *xhci = hcd_to_xhci(mtk->hcd); in xhci_mtk_runtime_suspend() local
777 if (xhci->xhc_state) in xhci_mtk_runtime_suspend()
778 return -ESHUTDOWN; in xhci_mtk_runtime_suspend()
783 /* -EBUSY: let PM automatically reschedule another autosuspend */ in xhci_mtk_runtime_suspend()
784 return ret ? -EBUSY : 0; in xhci_mtk_runtime_suspend()
789 struct xhci_hcd_mtk *mtk = dev_get_drvdata(dev); in xhci_mtk_runtime_resume() local
790 struct xhci_hcd *xhci = hcd_to_xhci(mtk->hcd); in xhci_mtk_runtime_resume() local
793 if (xhci->xhc_state) in xhci_mtk_runtime_resume()
794 return -ESHUTDOWN; in xhci_mtk_runtime_resume()
811 { .compatible = "mediatek,mt8173-xhci"},
812 { .compatible = "mediatek,mt8195-xhci"},
813 { .compatible = "mediatek,mtk-xhci"},
822 .name = "xhci-mtk",
842 MODULE_DESCRIPTION("MediaTek xHCI Host Controller Driver");