1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2019 MediaTek Inc.
9 #include <linux/arm-smccc.h>
10 #include <linux/bitfield.h>
11 #include <linux/clk.h>
12 #include <linux/delay.h>
13 #include <linux/module.h>
15 #include <linux/of_address.h>
16 #include <linux/of_device.h>
17 #include <linux/of_platform.h>
18 #include <linux/phy/phy.h>
19 #include <linux/platform_device.h>
20 #include <linux/pm_qos.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/reset.h>
23 #include <linux/soc/mediatek/mtk_sip_svc.h>
25 #include <ufs/ufshcd.h>
26 #include "ufshcd-pltfrm.h"
27 #include <ufs/ufs_quirks.h>
28 #include <ufs/unipro.h>
29 #include "ufs-mediatek.h"
31 static int ufs_mtk_config_mcq(struct ufs_hba *hba, bool irq);
33 #define CREATE_TRACE_POINTS
34 #include "ufs-mediatek-trace.h"
35 #undef CREATE_TRACE_POINTS
37 #define MAX_SUPP_MAC 64
38 #define MCQ_QUEUE_OFFSET(c) ((((c) >> 16) & 0xFF) * 0x200)
40 static const struct ufs_dev_quirk ufs_mtk_dev_fixups[] = {
41 { .wmanufacturerid = UFS_ANY_VENDOR,
42 .model = UFS_ANY_MODEL,
43 .quirk = UFS_DEVICE_QUIRK_DELAY_AFTER_LPM |
44 UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM },
45 { .wmanufacturerid = UFS_VENDOR_SKHYNIX,
46 .model = "H9HQ21AFAMZDAR",
47 .quirk = UFS_DEVICE_QUIRK_SUPPORT_EXTENDED_FEATURES },
51 static const struct of_device_id ufs_mtk_of_match[] = {
52 { .compatible = "mediatek,mt8183-ufshci" },
57 * Details of UIC Errors
59 static const char *const ufs_uic_err_str[] = {
63 "Transport Link Layer",
67 static const char *const ufs_uic_pa_err_str[] = {
68 "PHY error on Lane 0",
69 "PHY error on Lane 1",
70 "PHY error on Lane 2",
71 "PHY error on Lane 3",
72 "Generic PHY Adapter Error. This should be the LINERESET indication"
75 static const char *const ufs_uic_dl_err_str[] = {
77 "TCx_REPLAY_TIMER_EXPIRED",
78 "AFCx_REQUEST_TIMER_EXPIRED",
79 "FCx_PROTECTION_TIMER_EXPIRED",
82 "MAX_FRAME_LENGTH_EXCEEDED",
83 "WRONG_SEQUENCE_NUMBER",
84 "AFC_FRAME_SYNTAX_ERROR",
85 "NAC_FRAME_SYNTAX_ERROR",
88 "BAD_CTRL_SYMBOL_TYPE",
90 "PA_ERROR_IND_RECEIVED",
94 static bool ufs_mtk_is_boost_crypt_enabled(struct ufs_hba *hba)
96 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
98 return !!(host->caps & UFS_MTK_CAP_BOOST_CRYPT_ENGINE);
101 static bool ufs_mtk_is_va09_supported(struct ufs_hba *hba)
103 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
105 return !!(host->caps & UFS_MTK_CAP_VA09_PWR_CTRL);
108 static bool ufs_mtk_is_broken_vcc(struct ufs_hba *hba)
110 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
112 return !!(host->caps & UFS_MTK_CAP_BROKEN_VCC);
115 static bool ufs_mtk_is_pmc_via_fastauto(struct ufs_hba *hba)
117 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
119 return !!(host->caps & UFS_MTK_CAP_PMC_VIA_FASTAUTO);
122 static void ufs_mtk_cfg_unipro_cg(struct ufs_hba *hba, bool enable)
128 UIC_ARG_MIB(VS_SAVEPOWERCONTROL), &tmp);
130 (1 << RX_SYMBOL_CLK_GATE_EN) |
131 (1 << SYS_CLK_GATE_EN) |
132 (1 << TX_CLK_GATE_EN);
134 UIC_ARG_MIB(VS_SAVEPOWERCONTROL), tmp);
137 UIC_ARG_MIB(VS_DEBUGCLOCKENABLE), &tmp);
138 tmp = tmp & ~(1 << TX_SYMBOL_CLK_REQ_FORCE);
140 UIC_ARG_MIB(VS_DEBUGCLOCKENABLE), tmp);
143 UIC_ARG_MIB(VS_SAVEPOWERCONTROL), &tmp);
144 tmp = tmp & ~((1 << RX_SYMBOL_CLK_GATE_EN) |
145 (1 << SYS_CLK_GATE_EN) |
146 (1 << TX_CLK_GATE_EN));
148 UIC_ARG_MIB(VS_SAVEPOWERCONTROL), tmp);
151 UIC_ARG_MIB(VS_DEBUGCLOCKENABLE), &tmp);
152 tmp = tmp | (1 << TX_SYMBOL_CLK_REQ_FORCE);
154 UIC_ARG_MIB(VS_DEBUGCLOCKENABLE), tmp);
158 static void ufs_mtk_crypto_enable(struct ufs_hba *hba)
160 struct arm_smccc_res res;
162 ufs_mtk_crypto_ctrl(res, 1);
164 dev_info(hba->dev, "%s: crypto enable failed, err: %lu\n",
166 hba->caps &= ~UFSHCD_CAP_CRYPTO;
170 static void ufs_mtk_host_reset(struct ufs_hba *hba)
172 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
174 reset_control_assert(host->hci_reset);
175 reset_control_assert(host->crypto_reset);
176 reset_control_assert(host->unipro_reset);
178 usleep_range(100, 110);
180 reset_control_deassert(host->unipro_reset);
181 reset_control_deassert(host->crypto_reset);
182 reset_control_deassert(host->hci_reset);
185 static void ufs_mtk_init_reset_control(struct ufs_hba *hba,
186 struct reset_control **rc,
189 *rc = devm_reset_control_get(hba->dev, str);
191 dev_info(hba->dev, "Failed to get reset control %s: %ld\n",
197 static void ufs_mtk_init_reset(struct ufs_hba *hba)
199 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
201 ufs_mtk_init_reset_control(hba, &host->hci_reset,
203 ufs_mtk_init_reset_control(hba, &host->unipro_reset,
205 ufs_mtk_init_reset_control(hba, &host->crypto_reset,
209 static int ufs_mtk_hce_enable_notify(struct ufs_hba *hba,
210 enum ufs_notify_change_status status)
212 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
214 if (status == PRE_CHANGE) {
215 if (host->unipro_lpm) {
216 hba->vps->hba_enable_delay_us = 0;
218 hba->vps->hba_enable_delay_us = 600;
219 ufs_mtk_host_reset(hba);
222 if (hba->caps & UFSHCD_CAP_CRYPTO)
223 ufs_mtk_crypto_enable(hba);
225 if (host->caps & UFS_MTK_CAP_DISABLE_AH8) {
226 ufshcd_writel(hba, 0,
227 REG_AUTO_HIBERNATE_IDLE_TIMER);
228 hba->capabilities &= ~MASK_AUTO_HIBERN8_SUPPORT;
233 * Turn on CLK_CG early to bypass abnormal ERR_CHK signal
234 * to prevent host hang issue
237 ufshcd_readl(hba, REG_UFS_XOUFS_CTRL) | 0x80,
244 static int ufs_mtk_bind_mphy(struct ufs_hba *hba)
246 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
247 struct device *dev = hba->dev;
248 struct device_node *np = dev->of_node;
251 host->mphy = devm_of_phy_get_by_index(dev, np, 0);
253 if (host->mphy == ERR_PTR(-EPROBE_DEFER)) {
255 * UFS driver might be probed before the phy driver does.
256 * In that case we would like to return EPROBE_DEFER code.
260 "%s: required phy hasn't probed yet. err = %d\n",
262 } else if (IS_ERR(host->mphy)) {
263 err = PTR_ERR(host->mphy);
264 if (err != -ENODEV) {
265 dev_info(dev, "%s: PHY get failed %d\n", __func__,
273 * Allow unbound mphy because not every platform needs specific
282 static int ufs_mtk_setup_ref_clk(struct ufs_hba *hba, bool on)
284 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
285 struct arm_smccc_res res;
286 ktime_t timeout, time_checked;
289 if (host->ref_clk_enabled == on)
292 ufs_mtk_ref_clk_notify(on, PRE_CHANGE, res);
295 ufshcd_writel(hba, REFCLK_REQUEST, REG_UFS_REFCLK_CTRL);
297 ufshcd_delay_us(host->ref_clk_gating_wait_us, 10);
298 ufshcd_writel(hba, REFCLK_RELEASE, REG_UFS_REFCLK_CTRL);
302 timeout = ktime_add_us(ktime_get(), REFCLK_REQ_TIMEOUT_US);
304 time_checked = ktime_get();
305 value = ufshcd_readl(hba, REG_UFS_REFCLK_CTRL);
307 /* Wait until ack bit equals to req bit */
308 if (((value & REFCLK_ACK) >> 1) == (value & REFCLK_REQUEST))
311 usleep_range(100, 200);
312 } while (ktime_before(time_checked, timeout));
314 dev_err(hba->dev, "missing ack of refclk req, reg: 0x%x\n", value);
316 ufs_mtk_ref_clk_notify(host->ref_clk_enabled, POST_CHANGE, res);
321 host->ref_clk_enabled = on;
323 ufshcd_delay_us(host->ref_clk_ungating_wait_us, 10);
325 ufs_mtk_ref_clk_notify(on, POST_CHANGE, res);
330 static void ufs_mtk_setup_ref_clk_wait_us(struct ufs_hba *hba,
333 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
335 if (hba->dev_info.clk_gating_wait_us) {
336 host->ref_clk_gating_wait_us =
337 hba->dev_info.clk_gating_wait_us;
339 host->ref_clk_gating_wait_us = gating_us;
342 host->ref_clk_ungating_wait_us = REFCLK_DEFAULT_WAIT_US;
345 static void ufs_mtk_dbg_sel(struct ufs_hba *hba)
347 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
349 if (((host->ip_ver >> 16) & 0xFF) >= 0x36) {
350 ufshcd_writel(hba, 0x820820, REG_UFS_DEBUG_SEL);
351 ufshcd_writel(hba, 0x0, REG_UFS_DEBUG_SEL_B0);
352 ufshcd_writel(hba, 0x55555555, REG_UFS_DEBUG_SEL_B1);
353 ufshcd_writel(hba, 0xaaaaaaaa, REG_UFS_DEBUG_SEL_B2);
354 ufshcd_writel(hba, 0xffffffff, REG_UFS_DEBUG_SEL_B3);
356 ufshcd_writel(hba, 0x20, REG_UFS_DEBUG_SEL);
360 static void ufs_mtk_wait_idle_state(struct ufs_hba *hba,
361 unsigned long retry_ms)
363 u64 timeout, time_checked;
367 /* cannot use plain ktime_get() in suspend */
368 timeout = ktime_get_mono_fast_ns() + retry_ms * 1000000UL;
370 /* wait a specific time after check base */
375 time_checked = ktime_get_mono_fast_ns();
376 ufs_mtk_dbg_sel(hba);
377 val = ufshcd_readl(hba, REG_UFS_PROBE);
382 * if state is in H8 enter and H8 enter confirm
383 * wait until return to idle state.
385 if ((sm >= VS_HIB_ENTER) && (sm <= VS_HIB_EXIT)) {
389 } else if (!wait_idle)
392 if (wait_idle && (sm == VS_HCE_BASE))
394 } while (time_checked < timeout);
396 if (wait_idle && sm != VS_HCE_BASE)
397 dev_info(hba->dev, "wait idle tmo: 0x%x\n", val);
400 static int ufs_mtk_wait_link_state(struct ufs_hba *hba, u32 state,
401 unsigned long max_wait_ms)
403 ktime_t timeout, time_checked;
406 timeout = ktime_add_ms(ktime_get(), max_wait_ms);
408 time_checked = ktime_get();
409 ufs_mtk_dbg_sel(hba);
410 val = ufshcd_readl(hba, REG_UFS_PROBE);
416 /* Sleep for max. 200us */
417 usleep_range(100, 200);
418 } while (ktime_before(time_checked, timeout));
423 static int ufs_mtk_mphy_power_on(struct ufs_hba *hba, bool on)
425 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
426 struct phy *mphy = host->mphy;
427 struct arm_smccc_res res;
430 if (!mphy || !(on ^ host->mphy_powered_on))
434 if (ufs_mtk_is_va09_supported(hba)) {
435 ret = regulator_enable(host->reg_va09);
438 /* wait 200 us to stablize VA09 */
439 usleep_range(200, 210);
440 ufs_mtk_va09_pwr_ctrl(res, 1);
445 if (ufs_mtk_is_va09_supported(hba)) {
446 ufs_mtk_va09_pwr_ctrl(res, 0);
447 ret = regulator_disable(host->reg_va09);
453 "failed to %s va09: %d\n",
454 on ? "enable" : "disable",
457 host->mphy_powered_on = on;
463 static int ufs_mtk_get_host_clk(struct device *dev, const char *name,
464 struct clk **clk_out)
469 clk = devm_clk_get(dev, name);
478 static void ufs_mtk_boost_crypt(struct ufs_hba *hba, bool boost)
480 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
481 struct ufs_mtk_crypt_cfg *cfg;
482 struct regulator *reg;
485 if (!ufs_mtk_is_boost_crypt_enabled(hba))
489 volt = cfg->vcore_volt;
490 reg = cfg->reg_vcore;
492 ret = clk_prepare_enable(cfg->clk_crypt_mux);
494 dev_info(hba->dev, "clk_prepare_enable(): %d\n",
500 ret = regulator_set_voltage(reg, volt, INT_MAX);
503 "failed to set vcore to %d\n", volt);
507 ret = clk_set_parent(cfg->clk_crypt_mux,
508 cfg->clk_crypt_perf);
511 "failed to set clk_crypt_perf\n");
512 regulator_set_voltage(reg, 0, INT_MAX);
516 ret = clk_set_parent(cfg->clk_crypt_mux,
520 "failed to set clk_crypt_lp\n");
524 ret = regulator_set_voltage(reg, 0, INT_MAX);
527 "failed to set vcore to MIN\n");
531 clk_disable_unprepare(cfg->clk_crypt_mux);
534 static int ufs_mtk_init_host_clk(struct ufs_hba *hba, const char *name,
539 ret = ufs_mtk_get_host_clk(hba->dev, name, clk);
541 dev_info(hba->dev, "%s: failed to get %s: %d", __func__,
548 static void ufs_mtk_init_boost_crypt(struct ufs_hba *hba)
550 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
551 struct ufs_mtk_crypt_cfg *cfg;
552 struct device *dev = hba->dev;
553 struct regulator *reg;
556 host->crypt = devm_kzalloc(dev, sizeof(*(host->crypt)),
561 reg = devm_regulator_get_optional(dev, "dvfsrc-vcore");
563 dev_info(dev, "failed to get dvfsrc-vcore: %ld",
568 if (of_property_read_u32(dev->of_node, "boost-crypt-vcore-min",
570 dev_info(dev, "failed to get boost-crypt-vcore-min");
575 if (ufs_mtk_init_host_clk(hba, "crypt_mux",
576 &cfg->clk_crypt_mux))
579 if (ufs_mtk_init_host_clk(hba, "crypt_lp",
583 if (ufs_mtk_init_host_clk(hba, "crypt_perf",
584 &cfg->clk_crypt_perf))
587 cfg->reg_vcore = reg;
588 cfg->vcore_volt = volt;
589 host->caps |= UFS_MTK_CAP_BOOST_CRYPT_ENGINE;
595 static void ufs_mtk_init_va09_pwr_ctrl(struct ufs_hba *hba)
597 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
599 host->reg_va09 = regulator_get(hba->dev, "va09");
600 if (IS_ERR(host->reg_va09))
601 dev_info(hba->dev, "failed to get va09");
603 host->caps |= UFS_MTK_CAP_VA09_PWR_CTRL;
606 static void ufs_mtk_init_host_caps(struct ufs_hba *hba)
608 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
609 struct device_node *np = hba->dev->of_node;
611 if (of_property_read_bool(np, "mediatek,ufs-boost-crypt"))
612 ufs_mtk_init_boost_crypt(hba);
614 if (of_property_read_bool(np, "mediatek,ufs-support-va09"))
615 ufs_mtk_init_va09_pwr_ctrl(hba);
617 if (of_property_read_bool(np, "mediatek,ufs-disable-ah8"))
618 host->caps |= UFS_MTK_CAP_DISABLE_AH8;
620 if (of_property_read_bool(np, "mediatek,ufs-broken-vcc"))
621 host->caps |= UFS_MTK_CAP_BROKEN_VCC;
623 if (of_property_read_bool(np, "mediatek,ufs-pmc-via-fastauto"))
624 host->caps |= UFS_MTK_CAP_PMC_VIA_FASTAUTO;
626 dev_info(hba->dev, "caps: 0x%x", host->caps);
629 static void ufs_mtk_boost_pm_qos(struct ufs_hba *hba, bool boost)
631 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
633 if (!host || !host->pm_qos_init)
636 cpu_latency_qos_update_request(&host->pm_qos_req,
637 boost ? 0 : PM_QOS_DEFAULT_VALUE);
640 static void ufs_mtk_scale_perf(struct ufs_hba *hba, bool scale_up)
642 ufs_mtk_boost_crypt(hba, scale_up);
643 ufs_mtk_boost_pm_qos(hba, scale_up);
646 static void ufs_mtk_pwr_ctrl(struct ufs_hba *hba, bool on)
648 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
651 phy_power_on(host->mphy);
652 ufs_mtk_setup_ref_clk(hba, on);
653 if (!ufshcd_is_clkscaling_supported(hba))
654 ufs_mtk_scale_perf(hba, on);
656 if (!ufshcd_is_clkscaling_supported(hba))
657 ufs_mtk_scale_perf(hba, on);
658 ufs_mtk_setup_ref_clk(hba, on);
659 phy_power_off(host->mphy);
664 * ufs_mtk_setup_clocks - enables/disable clocks
665 * @hba: host controller instance
666 * @on: If true, enable clocks else disable them.
667 * @status: PRE_CHANGE or POST_CHANGE notify
669 * Return: 0 on success, non-zero on failure.
671 static int ufs_mtk_setup_clocks(struct ufs_hba *hba, bool on,
672 enum ufs_notify_change_status status)
674 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
675 bool clk_pwr_off = false;
679 * In case ufs_mtk_init() is not yet done, simply ignore.
680 * This ufs_mtk_setup_clocks() shall be called from
681 * ufs_mtk_init() after init is done.
686 if (!on && status == PRE_CHANGE) {
687 if (ufshcd_is_link_off(hba)) {
689 } else if (ufshcd_is_link_hibern8(hba) ||
690 (!ufshcd_can_hibern8_during_gating(hba) &&
691 ufshcd_is_auto_hibern8_enabled(hba))) {
693 * Gate ref-clk and poweroff mphy if link state is in
694 * OFF or Hibern8 by either Auto-Hibern8 or
695 * ufshcd_link_state_transition().
697 ret = ufs_mtk_wait_link_state(hba,
705 ufs_mtk_pwr_ctrl(hba, false);
706 } else if (on && status == POST_CHANGE) {
707 ufs_mtk_pwr_ctrl(hba, true);
713 static void ufs_mtk_get_controller_version(struct ufs_hba *hba)
715 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
718 if (host->hw_ver.major)
721 /* Set default (minimum) version anyway */
722 host->hw_ver.major = 2;
724 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_LOCALVERINFO), &ver);
726 if (ver >= UFS_UNIPRO_VER_1_8) {
727 host->hw_ver.major = 3;
729 * Fix HCI version for some platforms with
732 if (hba->ufs_version < ufshci_version(3, 0))
733 hba->ufs_version = ufshci_version(3, 0);
738 static u32 ufs_mtk_get_ufs_hci_version(struct ufs_hba *hba)
740 return hba->ufs_version;
744 * ufs_mtk_init_clocks - Init mtk driver private clocks
746 * @hba: per adapter instance
748 static void ufs_mtk_init_clocks(struct ufs_hba *hba)
750 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
751 struct list_head *head = &hba->clk_list_head;
752 struct ufs_mtk_clk *mclk = &host->mclk;
753 struct ufs_clk_info *clki, *clki_tmp;
756 * Find private clocks and store them in struct ufs_mtk_clk.
757 * Remove "ufs_sel_min_src" and "ufs_sel_min_src" from list to avoid
758 * being switched on/off in clock gating.
760 list_for_each_entry_safe(clki, clki_tmp, head, list) {
761 if (!strcmp(clki->name, "ufs_sel")) {
762 host->mclk.ufs_sel_clki = clki;
763 } else if (!strcmp(clki->name, "ufs_sel_max_src")) {
764 host->mclk.ufs_sel_max_clki = clki;
765 clk_disable_unprepare(clki->clk);
766 list_del(&clki->list);
767 } else if (!strcmp(clki->name, "ufs_sel_min_src")) {
768 host->mclk.ufs_sel_min_clki = clki;
769 clk_disable_unprepare(clki->clk);
770 list_del(&clki->list);
774 if (!mclk->ufs_sel_clki || !mclk->ufs_sel_max_clki ||
775 !mclk->ufs_sel_min_clki) {
776 hba->caps &= ~UFSHCD_CAP_CLK_SCALING;
778 "%s: Clk-scaling not ready. Feature disabled.",
783 #define MAX_VCC_NAME 30
784 static int ufs_mtk_vreg_fix_vcc(struct ufs_hba *hba)
786 struct ufs_vreg_info *info = &hba->vreg_info;
787 struct device_node *np = hba->dev->of_node;
788 struct device *dev = hba->dev;
789 char vcc_name[MAX_VCC_NAME];
790 struct arm_smccc_res res;
793 if (hba->vreg_info.vcc)
796 if (of_property_read_bool(np, "mediatek,ufs-vcc-by-num")) {
797 ufs_mtk_get_vcc_num(res);
798 if (res.a1 > UFS_VCC_NONE && res.a1 < UFS_VCC_MAX)
799 snprintf(vcc_name, MAX_VCC_NAME, "vcc-opt%lu", res.a1);
802 } else if (of_property_read_bool(np, "mediatek,ufs-vcc-by-ver")) {
803 ver = (hba->dev_info.wspecversion & 0xF00) >> 8;
804 snprintf(vcc_name, MAX_VCC_NAME, "vcc-ufs%u", ver);
809 err = ufshcd_populate_vreg(dev, vcc_name, &info->vcc, false);
813 err = ufshcd_get_vreg(dev, info->vcc);
817 err = regulator_enable(info->vcc->reg);
819 info->vcc->enabled = true;
820 dev_info(dev, "%s: %s enabled\n", __func__, vcc_name);
826 static void ufs_mtk_vreg_fix_vccqx(struct ufs_hba *hba)
828 struct ufs_vreg_info *info = &hba->vreg_info;
829 struct ufs_vreg **vreg_on, **vreg_off;
831 if (hba->dev_info.wspecversion >= 0x0300) {
832 vreg_on = &info->vccq;
833 vreg_off = &info->vccq2;
835 vreg_on = &info->vccq2;
836 vreg_off = &info->vccq;
840 (*vreg_on)->always_on = true;
843 regulator_disable((*vreg_off)->reg);
844 devm_kfree(hba->dev, (*vreg_off)->name);
845 devm_kfree(hba->dev, *vreg_off);
850 static void ufs_mtk_init_mcq_irq(struct ufs_hba *hba)
852 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
853 struct platform_device *pdev;
857 host->mcq_nr_intr = UFSHCD_MAX_Q_NR;
858 pdev = container_of(hba->dev, struct platform_device, dev);
860 for (i = 0; i < host->mcq_nr_intr; i++) {
861 /* irq index 0 is legacy irq, sq/cq irq start from index 1 */
862 irq = platform_get_irq(pdev, i + 1);
864 host->mcq_intr_info[i].irq = MTK_MCQ_INVALID_IRQ;
867 host->mcq_intr_info[i].hba = hba;
868 host->mcq_intr_info[i].irq = irq;
869 dev_info(hba->dev, "get platform mcq irq: %d, %d\n", i, irq);
874 /* invalidate irq info */
875 for (i = 0; i < host->mcq_nr_intr; i++)
876 host->mcq_intr_info[i].irq = MTK_MCQ_INVALID_IRQ;
878 host->mcq_nr_intr = 0;
882 * ufs_mtk_init - find other essential mmio bases
883 * @hba: host controller instance
885 * Binds PHY with controller and powers up PHY enabling clocks
888 * Return: -EPROBE_DEFER if binding fails, returns negative error
889 * on phy power up failure and returns zero on success.
891 static int ufs_mtk_init(struct ufs_hba *hba)
893 const struct of_device_id *id;
894 struct device *dev = hba->dev;
895 struct ufs_mtk_host *host;
898 host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
901 dev_info(dev, "%s: no memory for mtk ufs host\n", __func__);
906 ufshcd_set_variant(hba, host);
908 id = of_match_device(ufs_mtk_of_match, dev);
914 /* Initialize host capability */
915 ufs_mtk_init_host_caps(hba);
917 ufs_mtk_init_mcq_irq(hba);
919 err = ufs_mtk_bind_mphy(hba);
921 goto out_variant_clear;
923 ufs_mtk_init_reset(hba);
925 /* Enable runtime autosuspend */
926 hba->caps |= UFSHCD_CAP_RPM_AUTOSUSPEND;
928 /* Enable clock-gating */
929 hba->caps |= UFSHCD_CAP_CLK_GATING;
931 /* Enable inline encryption */
932 hba->caps |= UFSHCD_CAP_CRYPTO;
934 /* Enable WriteBooster */
935 hba->caps |= UFSHCD_CAP_WB_EN;
937 /* Enable clk scaling*/
938 hba->caps |= UFSHCD_CAP_CLK_SCALING;
940 hba->quirks |= UFSHCI_QUIRK_SKIP_MANUAL_WB_FLUSH_CTRL;
941 hba->quirks |= UFSHCD_QUIRK_MCQ_BROKEN_INTR;
942 hba->quirks |= UFSHCD_QUIRK_MCQ_BROKEN_RTC;
943 hba->vps->wb_flush_threshold = UFS_WB_BUF_REMAIN_PERCENT(80);
945 if (host->caps & UFS_MTK_CAP_DISABLE_AH8)
946 hba->caps |= UFSHCD_CAP_HIBERN8_WITH_CLK_GATING;
948 ufs_mtk_init_clocks(hba);
951 * ufshcd_vops_init() is invoked after
952 * ufshcd_setup_clock(true) in ufshcd_hba_init() thus
953 * phy clock setup is skipped.
955 * Enable phy clocks specifically here.
957 ufs_mtk_mphy_power_on(hba, true);
958 ufs_mtk_setup_clocks(hba, true, POST_CHANGE);
960 host->ip_ver = ufshcd_readl(hba, REG_UFS_MTK_IP_VER);
962 /* Initialize pm-qos request */
963 cpu_latency_qos_add_request(&host->pm_qos_req, PM_QOS_DEFAULT_VALUE);
964 host->pm_qos_init = true;
969 ufshcd_set_variant(hba, NULL);
974 static bool ufs_mtk_pmc_via_fastauto(struct ufs_hba *hba,
975 struct ufs_pa_layer_attr *dev_req_params)
977 if (!ufs_mtk_is_pmc_via_fastauto(hba))
980 if (dev_req_params->hs_rate == hba->pwr_info.hs_rate)
983 if (dev_req_params->pwr_tx != FAST_MODE &&
984 dev_req_params->gear_tx < UFS_HS_G4)
987 if (dev_req_params->pwr_rx != FAST_MODE &&
988 dev_req_params->gear_rx < UFS_HS_G4)
994 static int ufs_mtk_pre_pwr_change(struct ufs_hba *hba,
995 struct ufs_pa_layer_attr *dev_max_params,
996 struct ufs_pa_layer_attr *dev_req_params)
998 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
999 struct ufs_dev_params host_cap;
1002 ufshcd_init_pwr_dev_param(&host_cap);
1003 host_cap.hs_rx_gear = UFS_HS_G5;
1004 host_cap.hs_tx_gear = UFS_HS_G5;
1006 ret = ufshcd_get_pwr_dev_param(&host_cap,
1010 pr_info("%s: failed to determine capabilities\n",
1014 if (ufs_mtk_pmc_via_fastauto(hba, dev_req_params)) {
1015 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), true);
1016 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), UFS_HS_G1);
1018 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), true);
1019 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), UFS_HS_G1);
1021 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
1022 dev_req_params->lane_tx);
1023 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
1024 dev_req_params->lane_rx);
1025 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
1026 dev_req_params->hs_rate);
1028 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXHSADAPTTYPE),
1031 ret = ufshcd_uic_change_pwr_mode(hba,
1032 FASTAUTO_MODE << 4 | FASTAUTO_MODE);
1035 dev_err(hba->dev, "%s: HSG1B FASTAUTO failed ret=%d\n",
1040 if (host->hw_ver.major >= 3) {
1041 ret = ufshcd_dme_configure_adapt(hba,
1042 dev_req_params->gear_tx,
1049 static int ufs_mtk_pwr_change_notify(struct ufs_hba *hba,
1050 enum ufs_notify_change_status stage,
1051 struct ufs_pa_layer_attr *dev_max_params,
1052 struct ufs_pa_layer_attr *dev_req_params)
1058 ret = ufs_mtk_pre_pwr_change(hba, dev_max_params,
1071 static int ufs_mtk_unipro_set_lpm(struct ufs_hba *hba, bool lpm)
1074 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
1076 ret = ufshcd_dme_set(hba,
1077 UIC_ARG_MIB_SEL(VS_UNIPROPOWERDOWNCONTROL, 0),
1081 * Forcibly set as non-LPM mode if UIC commands is failed
1082 * to use default hba_enable_delay_us value for re-enabling
1085 host->unipro_lpm = lpm;
1091 static int ufs_mtk_pre_link(struct ufs_hba *hba)
1096 ufs_mtk_get_controller_version(hba);
1098 ret = ufs_mtk_unipro_set_lpm(hba, false);
1103 * Setting PA_Local_TX_LCC_Enable to 0 before link startup
1104 * to make sure that both host and device TX LCC are disabled
1105 * once link startup is completed.
1107 ret = ufshcd_disable_host_tx_lcc(hba);
1111 /* disable deep stall */
1112 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(VS_SAVEPOWERCONTROL), &tmp);
1118 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(VS_SAVEPOWERCONTROL), tmp);
1123 static void ufs_mtk_setup_clk_gating(struct ufs_hba *hba)
1127 if (ufshcd_is_clkgating_allowed(hba)) {
1128 if (ufshcd_is_auto_hibern8_supported(hba) && hba->ahit)
1129 ah_ms = FIELD_GET(UFSHCI_AHIBERN8_TIMER_MASK,
1133 ufshcd_clkgate_delay_set(hba->dev, ah_ms + 5);
1137 static void ufs_mtk_post_link(struct ufs_hba *hba)
1139 /* enable unipro clock gating feature */
1140 ufs_mtk_cfg_unipro_cg(hba, true);
1142 /* will be configured during probe hba */
1143 if (ufshcd_is_auto_hibern8_supported(hba))
1144 hba->ahit = FIELD_PREP(UFSHCI_AHIBERN8_TIMER_MASK, 10) |
1145 FIELD_PREP(UFSHCI_AHIBERN8_SCALE_MASK, 3);
1147 ufs_mtk_setup_clk_gating(hba);
1150 static int ufs_mtk_link_startup_notify(struct ufs_hba *hba,
1151 enum ufs_notify_change_status stage)
1157 ret = ufs_mtk_pre_link(hba);
1160 ufs_mtk_post_link(hba);
1170 static int ufs_mtk_device_reset(struct ufs_hba *hba)
1172 struct arm_smccc_res res;
1174 /* disable hba before device reset */
1175 ufshcd_hba_stop(hba);
1177 ufs_mtk_device_reset_ctrl(0, res);
1180 * The reset signal is active low. UFS devices shall detect
1181 * more than or equal to 1us of positive or negative RST_n
1184 * To be on safe side, keep the reset low for at least 10us.
1186 usleep_range(10, 15);
1188 ufs_mtk_device_reset_ctrl(1, res);
1190 /* Some devices may need time to respond to rst_n */
1191 usleep_range(10000, 15000);
1193 dev_info(hba->dev, "device reset done\n");
1198 static int ufs_mtk_link_set_hpm(struct ufs_hba *hba)
1202 err = ufshcd_hba_enable(hba);
1206 err = ufs_mtk_unipro_set_lpm(hba, false);
1210 err = ufshcd_uic_hibern8_exit(hba);
1212 ufshcd_set_link_active(hba);
1216 if (!hba->mcq_enabled) {
1217 err = ufshcd_make_hba_operational(hba);
1219 ufs_mtk_config_mcq(hba, false);
1220 ufshcd_mcq_make_queues_operational(hba);
1221 ufshcd_mcq_config_mac(hba, hba->nutrs);
1222 /* Enable MCQ mode */
1223 ufshcd_writel(hba, ufshcd_readl(hba, REG_UFS_MEM_CFG) | 0x1,
1233 static int ufs_mtk_link_set_lpm(struct ufs_hba *hba)
1237 /* Disable reset confirm feature by UniPro */
1239 (ufshcd_readl(hba, REG_UFS_XOUFS_CTRL) & ~0x100),
1240 REG_UFS_XOUFS_CTRL);
1242 err = ufs_mtk_unipro_set_lpm(hba, true);
1244 /* Resume UniPro state for following error recovery */
1245 ufs_mtk_unipro_set_lpm(hba, false);
1252 static void ufs_mtk_vccqx_set_lpm(struct ufs_hba *hba, bool lpm)
1254 struct ufs_vreg *vccqx = NULL;
1256 if (hba->vreg_info.vccq)
1257 vccqx = hba->vreg_info.vccq;
1259 vccqx = hba->vreg_info.vccq2;
1261 regulator_set_mode(vccqx->reg,
1262 lpm ? REGULATOR_MODE_IDLE : REGULATOR_MODE_NORMAL);
1265 static void ufs_mtk_vsx_set_lpm(struct ufs_hba *hba, bool lpm)
1267 struct arm_smccc_res res;
1269 ufs_mtk_device_pwr_ctrl(!lpm,
1270 (unsigned long)hba->dev_info.wspecversion,
1274 static void ufs_mtk_dev_vreg_set_lpm(struct ufs_hba *hba, bool lpm)
1276 if (!hba->vreg_info.vccq && !hba->vreg_info.vccq2)
1279 /* Skip if VCC is assumed always-on */
1280 if (!hba->vreg_info.vcc)
1283 /* Bypass LPM when device is still active */
1284 if (lpm && ufshcd_is_ufs_dev_active(hba))
1287 /* Bypass LPM if VCC is enabled */
1288 if (lpm && hba->vreg_info.vcc->enabled)
1292 ufs_mtk_vccqx_set_lpm(hba, lpm);
1293 ufs_mtk_vsx_set_lpm(hba, lpm);
1295 ufs_mtk_vsx_set_lpm(hba, lpm);
1296 ufs_mtk_vccqx_set_lpm(hba, lpm);
1300 static void ufs_mtk_auto_hibern8_disable(struct ufs_hba *hba)
1304 /* disable auto-hibern8 */
1305 ufshcd_writel(hba, 0, REG_AUTO_HIBERNATE_IDLE_TIMER);
1307 /* wait host return to idle state when auto-hibern8 off */
1308 ufs_mtk_wait_idle_state(hba, 5);
1310 ret = ufs_mtk_wait_link_state(hba, VS_LINK_UP, 100);
1312 dev_warn(hba->dev, "exit h8 state fail, ret=%d\n", ret);
1315 static int ufs_mtk_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op,
1316 enum ufs_notify_change_status status)
1319 struct arm_smccc_res res;
1321 if (status == PRE_CHANGE) {
1322 if (ufshcd_is_auto_hibern8_supported(hba))
1323 ufs_mtk_auto_hibern8_disable(hba);
1327 if (ufshcd_is_link_hibern8(hba)) {
1328 err = ufs_mtk_link_set_lpm(hba);
1333 if (!ufshcd_is_link_active(hba)) {
1335 * Make sure no error will be returned to prevent
1336 * ufshcd_suspend() re-enabling regulators while vreg is still
1337 * in low-power mode.
1339 err = ufs_mtk_mphy_power_on(hba, false);
1344 if (ufshcd_is_link_off(hba))
1345 ufs_mtk_device_reset_ctrl(0, res);
1347 ufs_mtk_host_pwr_ctrl(HOST_PWR_HCI, false, res);
1352 * Set link as off state enforcedly to trigger
1353 * ufshcd_host_reset_and_restore() in ufshcd_suspend()
1354 * for completed host reset.
1356 ufshcd_set_link_off(hba);
1360 static int ufs_mtk_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
1363 struct arm_smccc_res res;
1365 if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL)
1366 ufs_mtk_dev_vreg_set_lpm(hba, false);
1368 ufs_mtk_host_pwr_ctrl(HOST_PWR_HCI, true, res);
1370 err = ufs_mtk_mphy_power_on(hba, true);
1374 if (ufshcd_is_link_hibern8(hba)) {
1375 err = ufs_mtk_link_set_hpm(hba);
1382 return ufshcd_link_recovery(hba);
1385 static void ufs_mtk_dbg_register_dump(struct ufs_hba *hba)
1387 /* Dump ufshci register 0x140 ~ 0x14C */
1388 ufshcd_dump_regs(hba, REG_UFS_XOUFS_CTRL, 0x10,
1389 "XOUFS Ctrl (0x140): ");
1391 ufshcd_dump_regs(hba, REG_UFS_EXTREG, 0x4, "Ext Reg ");
1393 /* Dump ufshci register 0x2200 ~ 0x22AC */
1394 ufshcd_dump_regs(hba, REG_UFS_MPHYCTRL,
1395 REG_UFS_REJECT_MON - REG_UFS_MPHYCTRL + 4,
1396 "MPHY Ctrl (0x2200): ");
1398 /* Direct debugging information to REG_MTK_PROBE */
1399 ufs_mtk_dbg_sel(hba);
1400 ufshcd_dump_regs(hba, REG_UFS_PROBE, 0x4, "Debug Probe ");
1403 static int ufs_mtk_apply_dev_quirks(struct ufs_hba *hba)
1405 struct ufs_dev_info *dev_info = &hba->dev_info;
1406 u16 mid = dev_info->wmanufacturerid;
1408 if (mid == UFS_VENDOR_SAMSUNG) {
1409 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 6);
1410 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME), 10);
1414 * Decide waiting time before gating reference clock and
1415 * after ungating reference clock according to vendors'
1418 if (mid == UFS_VENDOR_SAMSUNG)
1419 ufs_mtk_setup_ref_clk_wait_us(hba, 1);
1420 else if (mid == UFS_VENDOR_SKHYNIX)
1421 ufs_mtk_setup_ref_clk_wait_us(hba, 30);
1422 else if (mid == UFS_VENDOR_TOSHIBA)
1423 ufs_mtk_setup_ref_clk_wait_us(hba, 100);
1425 ufs_mtk_setup_ref_clk_wait_us(hba,
1426 REFCLK_DEFAULT_WAIT_US);
1430 static void ufs_mtk_fixup_dev_quirks(struct ufs_hba *hba)
1432 ufshcd_fixup_dev_quirks(hba, ufs_mtk_dev_fixups);
1434 if (ufs_mtk_is_broken_vcc(hba) && hba->vreg_info.vcc &&
1435 (hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_AFTER_LPM)) {
1436 hba->vreg_info.vcc->always_on = true;
1438 * VCC will be kept always-on thus we don't
1439 * need any delay during regulator operations
1441 hba->dev_quirks &= ~(UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM |
1442 UFS_DEVICE_QUIRK_DELAY_AFTER_LPM);
1445 ufs_mtk_vreg_fix_vcc(hba);
1446 ufs_mtk_vreg_fix_vccqx(hba);
1449 static void ufs_mtk_event_notify(struct ufs_hba *hba,
1450 enum ufs_event_type evt, void *data)
1452 unsigned int val = *(u32 *)data;
1456 trace_ufs_mtk_event(evt, val);
1458 /* Print details of UIC Errors */
1459 if (evt <= UFS_EVT_DME_ERR) {
1461 "Host UIC Error Code (%s): %08x\n",
1462 ufs_uic_err_str[evt], val);
1466 if (evt == UFS_EVT_PA_ERR) {
1467 for_each_set_bit(bit, ®, ARRAY_SIZE(ufs_uic_pa_err_str))
1468 dev_info(hba->dev, "%s\n", ufs_uic_pa_err_str[bit]);
1471 if (evt == UFS_EVT_DL_ERR) {
1472 for_each_set_bit(bit, ®, ARRAY_SIZE(ufs_uic_dl_err_str))
1473 dev_info(hba->dev, "%s\n", ufs_uic_dl_err_str[bit]);
1477 static void ufs_mtk_config_scaling_param(struct ufs_hba *hba,
1478 struct devfreq_dev_profile *profile,
1479 struct devfreq_simple_ondemand_data *data)
1481 /* Customize min gear in clk scaling */
1482 hba->clk_scaling.min_gear = UFS_HS_G4;
1484 hba->vps->devfreq_profile.polling_ms = 200;
1485 hba->vps->ondemand_data.upthreshold = 50;
1486 hba->vps->ondemand_data.downdifferential = 20;
1490 * ufs_mtk_clk_scale - Internal clk scaling operation
1492 * MTK platform supports clk scaling by switching parent of ufs_sel(mux).
1493 * The ufs_sel downstream to ufs_ck which feeds directly to UFS hardware.
1494 * Max and min clocks rate of ufs_sel defined in dts should match rate of
1495 * "ufs_sel_max_src" and "ufs_sel_min_src" respectively.
1496 * This prevent changing rate of pll clock that is shared between modules.
1498 * @hba: per adapter instance
1499 * @scale_up: True for scaling up and false for scaling down
1501 static void ufs_mtk_clk_scale(struct ufs_hba *hba, bool scale_up)
1503 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
1504 struct ufs_mtk_clk *mclk = &host->mclk;
1505 struct ufs_clk_info *clki = mclk->ufs_sel_clki;
1508 ret = clk_prepare_enable(clki->clk);
1511 "clk_prepare_enable() fail, ret: %d\n", ret);
1516 ret = clk_set_parent(clki->clk, mclk->ufs_sel_max_clki->clk);
1517 clki->curr_freq = clki->max_freq;
1519 ret = clk_set_parent(clki->clk, mclk->ufs_sel_min_clki->clk);
1520 clki->curr_freq = clki->min_freq;
1525 "Failed to set ufs_sel_clki, ret: %d\n", ret);
1528 clk_disable_unprepare(clki->clk);
1530 trace_ufs_mtk_clk_scale(clki->name, scale_up, clk_get_rate(clki->clk));
1533 static int ufs_mtk_clk_scale_notify(struct ufs_hba *hba, bool scale_up,
1534 enum ufs_notify_change_status status)
1536 if (!ufshcd_is_clkscaling_supported(hba))
1539 if (status == PRE_CHANGE) {
1540 /* Switch parent before clk_set_rate() */
1541 ufs_mtk_clk_scale(hba, scale_up);
1543 /* Request interrupt latency QoS accordingly */
1544 ufs_mtk_scale_perf(hba, scale_up);
1550 static int ufs_mtk_get_hba_mac(struct ufs_hba *hba)
1552 return MAX_SUPP_MAC;
1555 static int ufs_mtk_op_runtime_config(struct ufs_hba *hba)
1557 struct ufshcd_mcq_opr_info_t *opr;
1560 hba->mcq_opr[OPR_SQD].offset = REG_UFS_MTK_SQD;
1561 hba->mcq_opr[OPR_SQIS].offset = REG_UFS_MTK_SQIS;
1562 hba->mcq_opr[OPR_CQD].offset = REG_UFS_MTK_CQD;
1563 hba->mcq_opr[OPR_CQIS].offset = REG_UFS_MTK_CQIS;
1565 for (i = 0; i < OPR_MAX; i++) {
1566 opr = &hba->mcq_opr[i];
1567 opr->stride = REG_UFS_MCQ_STRIDE;
1568 opr->base = hba->mmio_base + opr->offset;
1574 static int ufs_mtk_mcq_config_resource(struct ufs_hba *hba)
1576 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
1578 /* fail mcq initialization if interrupt is not filled properly */
1579 if (!host->mcq_nr_intr) {
1580 dev_info(hba->dev, "IRQs not ready. MCQ disabled.");
1584 hba->mcq_base = hba->mmio_base + MCQ_QUEUE_OFFSET(hba->mcq_capabilities);
1588 static irqreturn_t ufs_mtk_mcq_intr(int irq, void *__intr_info)
1590 struct ufs_mtk_mcq_intr_info *mcq_intr_info = __intr_info;
1591 struct ufs_hba *hba = mcq_intr_info->hba;
1592 struct ufs_hw_queue *hwq;
1594 int qid = mcq_intr_info->qid;
1596 hwq = &hba->uhq[qid];
1598 events = ufshcd_mcq_read_cqis(hba, qid);
1600 ufshcd_mcq_write_cqis(hba, events, qid);
1602 if (events & UFSHCD_MCQ_CQIS_TAIL_ENT_PUSH_STS)
1603 ufshcd_mcq_poll_cqe_lock(hba, hwq);
1608 static int ufs_mtk_config_mcq_irq(struct ufs_hba *hba)
1610 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
1614 for (i = 0; i < host->mcq_nr_intr; i++) {
1615 irq = host->mcq_intr_info[i].irq;
1616 if (irq == MTK_MCQ_INVALID_IRQ) {
1617 dev_err(hba->dev, "invalid irq. %d\n", i);
1621 host->mcq_intr_info[i].qid = i;
1622 ret = devm_request_irq(hba->dev, irq, ufs_mtk_mcq_intr, 0, UFSHCD,
1623 &host->mcq_intr_info[i]);
1625 dev_dbg(hba->dev, "request irq %d intr %s\n", irq, ret ? "failed" : "");
1628 dev_err(hba->dev, "Cannot request irq %d\n", ret);
1636 static int ufs_mtk_config_mcq(struct ufs_hba *hba, bool irq)
1638 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
1641 if (!host->mcq_set_intr) {
1642 /* Disable irq option register */
1643 ufshcd_rmwl(hba, MCQ_INTR_EN_MSK, 0, REG_UFS_MMIO_OPT_CTRL_0);
1646 ret = ufs_mtk_config_mcq_irq(hba);
1651 host->mcq_set_intr = true;
1654 ufshcd_rmwl(hba, MCQ_AH8, MCQ_AH8, REG_UFS_MMIO_OPT_CTRL_0);
1655 ufshcd_rmwl(hba, MCQ_INTR_EN_MSK, MCQ_MULTI_INTR_EN, REG_UFS_MMIO_OPT_CTRL_0);
1660 static int ufs_mtk_config_esi(struct ufs_hba *hba)
1662 return ufs_mtk_config_mcq(hba, true);
1666 * struct ufs_hba_mtk_vops - UFS MTK specific variant operations
1668 * The variant operations configure the necessary controller and PHY
1669 * handshake during initialization.
1671 static const struct ufs_hba_variant_ops ufs_hba_mtk_vops = {
1672 .name = "mediatek.ufshci",
1673 .init = ufs_mtk_init,
1674 .get_ufs_hci_version = ufs_mtk_get_ufs_hci_version,
1675 .setup_clocks = ufs_mtk_setup_clocks,
1676 .hce_enable_notify = ufs_mtk_hce_enable_notify,
1677 .link_startup_notify = ufs_mtk_link_startup_notify,
1678 .pwr_change_notify = ufs_mtk_pwr_change_notify,
1679 .apply_dev_quirks = ufs_mtk_apply_dev_quirks,
1680 .fixup_dev_quirks = ufs_mtk_fixup_dev_quirks,
1681 .suspend = ufs_mtk_suspend,
1682 .resume = ufs_mtk_resume,
1683 .dbg_register_dump = ufs_mtk_dbg_register_dump,
1684 .device_reset = ufs_mtk_device_reset,
1685 .event_notify = ufs_mtk_event_notify,
1686 .config_scaling_param = ufs_mtk_config_scaling_param,
1687 .clk_scale_notify = ufs_mtk_clk_scale_notify,
1689 .get_hba_mac = ufs_mtk_get_hba_mac,
1690 .op_runtime_config = ufs_mtk_op_runtime_config,
1691 .mcq_config_resource = ufs_mtk_mcq_config_resource,
1692 .config_esi = ufs_mtk_config_esi,
1696 * ufs_mtk_probe - probe routine of the driver
1697 * @pdev: pointer to Platform device handle
1699 * Return: zero for success and non-zero for failure.
1701 static int ufs_mtk_probe(struct platform_device *pdev)
1704 struct device *dev = &pdev->dev;
1705 struct device_node *reset_node;
1706 struct platform_device *reset_pdev;
1707 struct device_link *link;
1709 reset_node = of_find_compatible_node(NULL, NULL,
1712 dev_notice(dev, "find ti,syscon-reset fail\n");
1715 reset_pdev = of_find_device_by_node(reset_node);
1717 dev_notice(dev, "find reset_pdev fail\n");
1720 link = device_link_add(dev, &reset_pdev->dev,
1721 DL_FLAG_AUTOPROBE_CONSUMER);
1722 put_device(&reset_pdev->dev);
1724 dev_notice(dev, "add reset device_link fail\n");
1727 /* supplier is not probed */
1728 if (link->status == DL_STATE_DORMANT) {
1729 err = -EPROBE_DEFER;
1734 /* perform generic probe */
1735 err = ufshcd_pltfrm_init(pdev, &ufs_hba_mtk_vops);
1739 dev_err(dev, "probe failed %d\n", err);
1741 of_node_put(reset_node);
1746 * ufs_mtk_remove - set driver_data of the device to NULL
1747 * @pdev: pointer to platform device handle
1751 static void ufs_mtk_remove(struct platform_device *pdev)
1753 struct ufs_hba *hba = platform_get_drvdata(pdev);
1755 pm_runtime_get_sync(&(pdev)->dev);
1759 #ifdef CONFIG_PM_SLEEP
1760 static int ufs_mtk_system_suspend(struct device *dev)
1762 struct ufs_hba *hba = dev_get_drvdata(dev);
1765 ret = ufshcd_system_suspend(dev);
1769 ufs_mtk_dev_vreg_set_lpm(hba, true);
1774 static int ufs_mtk_system_resume(struct device *dev)
1776 struct ufs_hba *hba = dev_get_drvdata(dev);
1778 ufs_mtk_dev_vreg_set_lpm(hba, false);
1780 return ufshcd_system_resume(dev);
1785 static int ufs_mtk_runtime_suspend(struct device *dev)
1787 struct ufs_hba *hba = dev_get_drvdata(dev);
1790 ret = ufshcd_runtime_suspend(dev);
1794 ufs_mtk_dev_vreg_set_lpm(hba, true);
1799 static int ufs_mtk_runtime_resume(struct device *dev)
1801 struct ufs_hba *hba = dev_get_drvdata(dev);
1803 ufs_mtk_dev_vreg_set_lpm(hba, false);
1805 return ufshcd_runtime_resume(dev);
1809 static const struct dev_pm_ops ufs_mtk_pm_ops = {
1810 SET_SYSTEM_SLEEP_PM_OPS(ufs_mtk_system_suspend,
1811 ufs_mtk_system_resume)
1812 SET_RUNTIME_PM_OPS(ufs_mtk_runtime_suspend,
1813 ufs_mtk_runtime_resume, NULL)
1814 .prepare = ufshcd_suspend_prepare,
1815 .complete = ufshcd_resume_complete,
1818 static struct platform_driver ufs_mtk_pltform = {
1819 .probe = ufs_mtk_probe,
1820 .remove_new = ufs_mtk_remove,
1822 .name = "ufshcd-mtk",
1823 .pm = &ufs_mtk_pm_ops,
1824 .of_match_table = ufs_mtk_of_match,
1830 MODULE_DESCRIPTION("MediaTek UFS Host Driver");
1831 MODULE_LICENSE("GPL v2");
1833 module_platform_driver(ufs_mtk_pltform);