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()
402 return devm_clk_bulk_get_optional(mtk->dev, BULK_CLKS_NUM, clks); in xhci_mtk_clks_get()
405 static int xhci_mtk_vregs_get(struct xhci_hcd_mtk *mtk) in xhci_mtk_vregs_get() argument
407 struct regulator_bulk_data *supplies = mtk->supplies; in xhci_mtk_vregs_get()
412 return devm_regulator_bulk_get(mtk->dev, BULK_VREGS_NUM, supplies); in xhci_mtk_vregs_get()
415 static void xhci_mtk_quirks(struct device *dev, struct xhci_hcd *xhci) in xhci_mtk_quirks() argument
417 struct usb_hcd *hcd = xhci_to_hcd(xhci); in xhci_mtk_quirks()
418 struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd); in xhci_mtk_quirks() local
425 xhci->quirks |= XHCI_PLAT; in xhci_mtk_quirks()
426 xhci->quirks |= XHCI_MTK_HOST; in xhci_mtk_quirks()
428 * MTK host controller gives a spurious successful event after a in xhci_mtk_quirks()
431 xhci->quirks |= XHCI_SPURIOUS_SUCCESS; in xhci_mtk_quirks()
432 if (mtk->lpm_support) in xhci_mtk_quirks()
433 xhci->quirks |= XHCI_LPM_SUPPORT; in xhci_mtk_quirks()
434 if (mtk->u2_lpm_disable) in xhci_mtk_quirks()
435 xhci->quirks |= XHCI_HW_LPM_DISABLE; in xhci_mtk_quirks()
438 * MTK xHCI 0.96: PSA is 1 by default even if doesn't support stream, in xhci_mtk_quirks()
441 if (xhci->hci_version < 0x100 && HCC_MAX_PSA(xhci->hcc_params) == 4) in xhci_mtk_quirks()
442 xhci->quirks |= XHCI_BROKEN_STREAMS; in xhci_mtk_quirks()
448 struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd); in xhci_mtk_setup() local
452 ret = xhci_mtk_ssusb_config(mtk); in xhci_mtk_setup()
457 xhci_mtk_set_frame_interval(mtk); in xhci_mtk_setup()
465 ret = xhci_mtk_sch_init(mtk); in xhci_mtk_setup()
482 struct device *dev = &pdev->dev; in xhci_mtk_probe()
483 struct device_node *node = dev->of_node; in xhci_mtk_probe()
484 struct xhci_hcd_mtk *mtk; in xhci_mtk_probe() local
486 struct xhci_hcd *xhci; in xhci_mtk_probe() local
489 int ret = -ENODEV; in xhci_mtk_probe()
494 return -ENODEV; in xhci_mtk_probe()
497 mtk = devm_kzalloc(dev, sizeof(*mtk), GFP_KERNEL); in xhci_mtk_probe()
498 if (!mtk) in xhci_mtk_probe()
499 return -ENOMEM; in xhci_mtk_probe()
501 mtk->dev = dev; in xhci_mtk_probe()
503 ret = xhci_mtk_vregs_get(mtk); in xhci_mtk_probe()
507 ret = xhci_mtk_clks_get(mtk); in xhci_mtk_probe()
513 if (irq == -EPROBE_DEFER) in xhci_mtk_probe()
523 if (wakeup_irq == -EPROBE_DEFER) in xhci_mtk_probe()
526 mtk->lpm_support = of_property_read_bool(node, "usb3-lpm-capable"); in xhci_mtk_probe()
527 mtk->u2_lpm_disable = of_property_read_bool(node, "usb2-lpm-disable"); in xhci_mtk_probe()
529 of_property_read_u32(node, "mediatek,u3p-dis-msk", in xhci_mtk_probe()
530 &mtk->u3p_dis_msk); in xhci_mtk_probe()
531 of_property_read_u32(node, "mediatek,u2p-dis-msk", in xhci_mtk_probe()
532 &mtk->u2p_dis_msk); in xhci_mtk_probe()
534 ret = usb_wakeup_of_property_parse(mtk, node); in xhci_mtk_probe()
546 ret = regulator_bulk_enable(BULK_VREGS_NUM, mtk->supplies); in xhci_mtk_probe()
550 ret = clk_bulk_prepare_enable(BULK_CLKS_NUM, mtk->clks); in xhci_mtk_probe()
562 ret = -ENOMEM; in xhci_mtk_probe()
568 * Swap it with mtk HCD. in xhci_mtk_probe()
570 mtk->hcd = platform_get_drvdata(pdev); in xhci_mtk_probe()
571 platform_set_drvdata(pdev, mtk); in xhci_mtk_probe()
574 hcd->regs = devm_ioremap_resource(dev, res); in xhci_mtk_probe()
575 if (IS_ERR(hcd->regs)) { in xhci_mtk_probe()
576 ret = PTR_ERR(hcd->regs); in xhci_mtk_probe()
579 hcd->rsrc_start = res->start; in xhci_mtk_probe()
580 hcd->rsrc_len = resource_size(res); in xhci_mtk_probe()
584 mtk->ippc_regs = devm_ioremap_resource(dev, res); in xhci_mtk_probe()
585 if (IS_ERR(mtk->ippc_regs)) { in xhci_mtk_probe()
586 ret = PTR_ERR(mtk->ippc_regs); in xhci_mtk_probe()
589 mtk->has_ippc = true; in xhci_mtk_probe()
594 xhci = hcd_to_xhci(hcd); in xhci_mtk_probe()
595 xhci->main_hcd = hcd; in xhci_mtk_probe()
600 * the xHCI spec on MTK's controller. in xhci_mtk_probe()
602 xhci->imod_interval = 5000; in xhci_mtk_probe()
603 device_property_read_u32(dev, "imod-interval-ns", &xhci->imod_interval); in xhci_mtk_probe()
605 xhci->shared_hcd = usb_create_shared_hcd(driver, dev, in xhci_mtk_probe()
607 if (!xhci->shared_hcd) { in xhci_mtk_probe()
608 ret = -ENOMEM; in xhci_mtk_probe()
616 if (HCC_MAX_PSA(xhci->hcc_params) >= 4 && in xhci_mtk_probe()
617 !(xhci->quirks & XHCI_BROKEN_STREAMS)) in xhci_mtk_probe()
618 xhci->shared_hcd->can_do_streams = 1; in xhci_mtk_probe()
620 ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED); in xhci_mtk_probe()
641 usb_remove_hcd(xhci->shared_hcd); in xhci_mtk_probe()
642 xhci->shared_hcd = NULL; in xhci_mtk_probe()
648 xhci_mtk_sch_exit(mtk); in xhci_mtk_probe()
649 usb_put_hcd(xhci->shared_hcd); in xhci_mtk_probe()
658 clk_bulk_disable_unprepare(BULK_CLKS_NUM, mtk->clks); in xhci_mtk_probe()
661 regulator_bulk_disable(BULK_VREGS_NUM, mtk->supplies); in xhci_mtk_probe()
671 struct xhci_hcd_mtk *mtk = platform_get_drvdata(pdev); in xhci_mtk_remove() local
672 struct usb_hcd *hcd = mtk->hcd; in xhci_mtk_remove()
673 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_mtk_remove() local
674 struct usb_hcd *shared_hcd = xhci->shared_hcd; in xhci_mtk_remove()
675 struct device *dev = &pdev->dev; in xhci_mtk_remove()
678 xhci->xhc_state |= XHCI_STATE_REMOVING; in xhci_mtk_remove()
683 xhci->shared_hcd = NULL; in xhci_mtk_remove()
687 xhci_mtk_sch_exit(mtk); in xhci_mtk_remove()
688 clk_bulk_disable_unprepare(BULK_CLKS_NUM, mtk->clks); in xhci_mtk_remove()
689 regulator_bulk_disable(BULK_VREGS_NUM, mtk->supplies); in xhci_mtk_remove()
700 struct xhci_hcd_mtk *mtk = dev_get_drvdata(dev); in xhci_mtk_suspend() local
701 struct usb_hcd *hcd = mtk->hcd; in xhci_mtk_suspend()
702 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_mtk_suspend() local
705 xhci_dbg(xhci, "%s: stop port polling\n", __func__); in xhci_mtk_suspend()
706 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags); in xhci_mtk_suspend()
707 del_timer_sync(&hcd->rh_timer); in xhci_mtk_suspend()
708 clear_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags); in xhci_mtk_suspend()
709 del_timer_sync(&xhci->shared_hcd->rh_timer); in xhci_mtk_suspend()
711 ret = xhci_mtk_host_disable(mtk); in xhci_mtk_suspend()
715 clk_bulk_disable_unprepare(BULK_CLKS_NUM, mtk->clks); in xhci_mtk_suspend()
716 usb_wakeup_set(mtk, true); in xhci_mtk_suspend()
720 xhci_dbg(xhci, "%s: restart port polling\n", __func__); in xhci_mtk_suspend()
721 set_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags); in xhci_mtk_suspend()
722 usb_hcd_poll_rh_status(xhci->shared_hcd); in xhci_mtk_suspend()
723 set_bit(HCD_FLAG_POLL_RH, &hcd->flags); in xhci_mtk_suspend()
730 struct xhci_hcd_mtk *mtk = dev_get_drvdata(dev); in xhci_mtk_resume() local
731 struct usb_hcd *hcd = mtk->hcd; in xhci_mtk_resume()
732 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_mtk_resume() local
735 usb_wakeup_set(mtk, false); in xhci_mtk_resume()
736 ret = clk_bulk_prepare_enable(BULK_CLKS_NUM, mtk->clks); in xhci_mtk_resume()
740 ret = xhci_mtk_host_enable(mtk); in xhci_mtk_resume()
744 xhci_dbg(xhci, "%s: restart port polling\n", __func__); in xhci_mtk_resume()
745 set_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags); in xhci_mtk_resume()
746 usb_hcd_poll_rh_status(xhci->shared_hcd); in xhci_mtk_resume()
747 set_bit(HCD_FLAG_POLL_RH, &hcd->flags); in xhci_mtk_resume()
752 clk_bulk_disable_unprepare(BULK_CLKS_NUM, mtk->clks); in xhci_mtk_resume()
754 usb_wakeup_set(mtk, true); in xhci_mtk_resume()
760 struct xhci_hcd_mtk *mtk = dev_get_drvdata(dev); in xhci_mtk_runtime_suspend() local
761 struct xhci_hcd *xhci = hcd_to_xhci(mtk->hcd); in xhci_mtk_runtime_suspend() local
764 if (xhci->xhc_state) in xhci_mtk_runtime_suspend()
765 return -ESHUTDOWN; in xhci_mtk_runtime_suspend()
770 /* -EBUSY: let PM automatically reschedule another autosuspend */ in xhci_mtk_runtime_suspend()
771 return ret ? -EBUSY : 0; in xhci_mtk_runtime_suspend()
776 struct xhci_hcd_mtk *mtk = dev_get_drvdata(dev); in xhci_mtk_runtime_resume() local
777 struct xhci_hcd *xhci = hcd_to_xhci(mtk->hcd); in xhci_mtk_runtime_resume() local
780 if (xhci->xhc_state) in xhci_mtk_runtime_resume()
781 return -ESHUTDOWN; in xhci_mtk_runtime_resume()
798 { .compatible = "mediatek,mt8173-xhci"},
799 { .compatible = "mediatek,mt8195-xhci"},
800 { .compatible = "mediatek,mtk-xhci"},
809 .name = "xhci-mtk",
829 MODULE_DESCRIPTION("MediaTek xHCI Host Controller Driver");