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/regulator/consumer.h>
21 #include <linux/reset.h>
23 #include <ufs/ufshcd.h>
24 #include "ufshcd-pltfrm.h"
25 #include <ufs/ufs_quirks.h>
26 #include <ufs/unipro.h>
28 #include "ufs-mediatek.h"
29 #include "ufs-mediatek-sip.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" },
55 MODULE_DEVICE_TABLE(of, ufs_mtk_of_match);
58 * Details of UIC Errors
60 static const char *const ufs_uic_err_str[] = {
64 "Transport Link Layer",
68 static const char *const ufs_uic_pa_err_str[] = {
69 "PHY error on Lane 0",
70 "PHY error on Lane 1",
71 "PHY error on Lane 2",
72 "PHY error on Lane 3",
73 "Generic PHY Adapter Error. This should be the LINERESET indication"
76 static const char *const ufs_uic_dl_err_str[] = {
78 "TCx_REPLAY_TIMER_EXPIRED",
79 "AFCx_REQUEST_TIMER_EXPIRED",
80 "FCx_PROTECTION_TIMER_EXPIRED",
83 "MAX_FRAME_LENGTH_EXCEEDED",
84 "WRONG_SEQUENCE_NUMBER",
85 "AFC_FRAME_SYNTAX_ERROR",
86 "NAC_FRAME_SYNTAX_ERROR",
89 "BAD_CTRL_SYMBOL_TYPE",
91 "PA_ERROR_IND_RECEIVED",
95 static bool ufs_mtk_is_boost_crypt_enabled(struct ufs_hba *hba)
97 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
99 return !!(host->caps & UFS_MTK_CAP_BOOST_CRYPT_ENGINE);
102 static bool ufs_mtk_is_va09_supported(struct ufs_hba *hba)
104 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
106 return !!(host->caps & UFS_MTK_CAP_VA09_PWR_CTRL);
109 static bool ufs_mtk_is_broken_vcc(struct ufs_hba *hba)
111 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
113 return !!(host->caps & UFS_MTK_CAP_BROKEN_VCC);
116 static bool ufs_mtk_is_pmc_via_fastauto(struct ufs_hba *hba)
118 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
120 return !!(host->caps & UFS_MTK_CAP_PMC_VIA_FASTAUTO);
123 static bool ufs_mtk_is_tx_skew_fix(struct ufs_hba *hba)
125 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
127 return (host->caps & UFS_MTK_CAP_TX_SKEW_FIX);
130 static bool ufs_mtk_is_rtff_mtcmos(struct ufs_hba *hba)
132 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
134 return (host->caps & UFS_MTK_CAP_RTFF_MTCMOS);
137 static bool ufs_mtk_is_allow_vccqx_lpm(struct ufs_hba *hba)
139 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
141 return (host->caps & UFS_MTK_CAP_ALLOW_VCCQX_LPM);
144 static void ufs_mtk_cfg_unipro_cg(struct ufs_hba *hba, bool enable)
150 UIC_ARG_MIB(VS_SAVEPOWERCONTROL), &tmp);
152 (1 << RX_SYMBOL_CLK_GATE_EN) |
153 (1 << SYS_CLK_GATE_EN) |
154 (1 << TX_CLK_GATE_EN);
156 UIC_ARG_MIB(VS_SAVEPOWERCONTROL), tmp);
159 UIC_ARG_MIB(VS_DEBUGCLOCKENABLE), &tmp);
160 tmp = tmp & ~(1 << TX_SYMBOL_CLK_REQ_FORCE);
162 UIC_ARG_MIB(VS_DEBUGCLOCKENABLE), tmp);
165 UIC_ARG_MIB(VS_SAVEPOWERCONTROL), &tmp);
166 tmp = tmp & ~((1 << RX_SYMBOL_CLK_GATE_EN) |
167 (1 << SYS_CLK_GATE_EN) |
168 (1 << TX_CLK_GATE_EN));
170 UIC_ARG_MIB(VS_SAVEPOWERCONTROL), tmp);
173 UIC_ARG_MIB(VS_DEBUGCLOCKENABLE), &tmp);
174 tmp = tmp | (1 << TX_SYMBOL_CLK_REQ_FORCE);
176 UIC_ARG_MIB(VS_DEBUGCLOCKENABLE), tmp);
180 static void ufs_mtk_crypto_enable(struct ufs_hba *hba)
182 struct arm_smccc_res res;
184 ufs_mtk_crypto_ctrl(res, 1);
186 dev_info(hba->dev, "%s: crypto enable failed, err: %lu\n",
188 hba->caps &= ~UFSHCD_CAP_CRYPTO;
192 static void ufs_mtk_host_reset(struct ufs_hba *hba)
194 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
195 struct arm_smccc_res res;
197 reset_control_assert(host->hci_reset);
198 reset_control_assert(host->crypto_reset);
199 reset_control_assert(host->unipro_reset);
200 reset_control_assert(host->mphy_reset);
202 usleep_range(100, 110);
204 reset_control_deassert(host->unipro_reset);
205 reset_control_deassert(host->crypto_reset);
206 reset_control_deassert(host->hci_reset);
207 reset_control_deassert(host->mphy_reset);
209 /* restore mphy setting aftre mphy reset */
210 if (host->mphy_reset)
211 ufs_mtk_mphy_ctrl(UFS_MPHY_RESTORE, res);
214 static void ufs_mtk_init_reset_control(struct ufs_hba *hba,
215 struct reset_control **rc,
218 *rc = devm_reset_control_get(hba->dev, str);
220 dev_info(hba->dev, "Failed to get reset control %s: %ld\n",
226 static void ufs_mtk_init_reset(struct ufs_hba *hba)
228 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
230 ufs_mtk_init_reset_control(hba, &host->hci_reset,
232 ufs_mtk_init_reset_control(hba, &host->unipro_reset,
234 ufs_mtk_init_reset_control(hba, &host->crypto_reset,
236 ufs_mtk_init_reset_control(hba, &host->mphy_reset,
240 static int ufs_mtk_hce_enable_notify(struct ufs_hba *hba,
241 enum ufs_notify_change_status status)
243 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
245 if (status == PRE_CHANGE) {
246 if (host->unipro_lpm) {
247 hba->vps->hba_enable_delay_us = 0;
249 hba->vps->hba_enable_delay_us = 600;
250 ufs_mtk_host_reset(hba);
253 if (hba->caps & UFSHCD_CAP_CRYPTO)
254 ufs_mtk_crypto_enable(hba);
256 if (host->caps & UFS_MTK_CAP_DISABLE_AH8) {
257 ufshcd_writel(hba, 0,
258 REG_AUTO_HIBERNATE_IDLE_TIMER);
259 hba->capabilities &= ~MASK_AUTO_HIBERN8_SUPPORT;
264 * Turn on CLK_CG early to bypass abnormal ERR_CHK signal
265 * to prevent host hang issue
268 ufshcd_readl(hba, REG_UFS_XOUFS_CTRL) | 0x80,
275 static int ufs_mtk_bind_mphy(struct ufs_hba *hba)
277 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
278 struct device *dev = hba->dev;
279 struct device_node *np = dev->of_node;
282 host->mphy = devm_of_phy_get_by_index(dev, np, 0);
284 if (host->mphy == ERR_PTR(-EPROBE_DEFER)) {
286 * UFS driver might be probed before the phy driver does.
287 * In that case we would like to return EPROBE_DEFER code.
291 "%s: required phy hasn't probed yet. err = %d\n",
293 } else if (IS_ERR(host->mphy)) {
294 err = PTR_ERR(host->mphy);
295 if (err != -ENODEV) {
296 dev_info(dev, "%s: PHY get failed %d\n", __func__,
304 * Allow unbound mphy because not every platform needs specific
313 static int ufs_mtk_setup_ref_clk(struct ufs_hba *hba, bool on)
315 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
316 struct arm_smccc_res res;
317 ktime_t timeout, time_checked;
320 if (host->ref_clk_enabled == on)
323 ufs_mtk_ref_clk_notify(on, PRE_CHANGE, res);
326 ufshcd_writel(hba, REFCLK_REQUEST, REG_UFS_REFCLK_CTRL);
328 ufshcd_delay_us(host->ref_clk_gating_wait_us, 10);
329 ufshcd_writel(hba, REFCLK_RELEASE, REG_UFS_REFCLK_CTRL);
333 timeout = ktime_add_us(ktime_get(), REFCLK_REQ_TIMEOUT_US);
335 time_checked = ktime_get();
336 value = ufshcd_readl(hba, REG_UFS_REFCLK_CTRL);
338 /* Wait until ack bit equals to req bit */
339 if (((value & REFCLK_ACK) >> 1) == (value & REFCLK_REQUEST))
342 usleep_range(100, 200);
343 } while (ktime_before(time_checked, timeout));
345 dev_err(hba->dev, "missing ack of refclk req, reg: 0x%x\n", value);
347 ufs_mtk_ref_clk_notify(host->ref_clk_enabled, POST_CHANGE, res);
352 host->ref_clk_enabled = on;
354 ufshcd_delay_us(host->ref_clk_ungating_wait_us, 10);
356 ufs_mtk_ref_clk_notify(on, POST_CHANGE, res);
361 static void ufs_mtk_setup_ref_clk_wait_us(struct ufs_hba *hba,
364 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
366 if (hba->dev_info.clk_gating_wait_us) {
367 host->ref_clk_gating_wait_us =
368 hba->dev_info.clk_gating_wait_us;
370 host->ref_clk_gating_wait_us = gating_us;
373 host->ref_clk_ungating_wait_us = REFCLK_DEFAULT_WAIT_US;
376 static void ufs_mtk_dbg_sel(struct ufs_hba *hba)
378 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
380 if (((host->ip_ver >> 16) & 0xFF) >= 0x36) {
381 ufshcd_writel(hba, 0x820820, REG_UFS_DEBUG_SEL);
382 ufshcd_writel(hba, 0x0, REG_UFS_DEBUG_SEL_B0);
383 ufshcd_writel(hba, 0x55555555, REG_UFS_DEBUG_SEL_B1);
384 ufshcd_writel(hba, 0xaaaaaaaa, REG_UFS_DEBUG_SEL_B2);
385 ufshcd_writel(hba, 0xffffffff, REG_UFS_DEBUG_SEL_B3);
387 ufshcd_writel(hba, 0x20, REG_UFS_DEBUG_SEL);
391 static void ufs_mtk_wait_idle_state(struct ufs_hba *hba,
392 unsigned long retry_ms)
394 u64 timeout, time_checked;
398 /* cannot use plain ktime_get() in suspend */
399 timeout = ktime_get_mono_fast_ns() + retry_ms * 1000000UL;
401 /* wait a specific time after check base */
406 time_checked = ktime_get_mono_fast_ns();
407 ufs_mtk_dbg_sel(hba);
408 val = ufshcd_readl(hba, REG_UFS_PROBE);
413 * if state is in H8 enter and H8 enter confirm
414 * wait until return to idle state.
416 if ((sm >= VS_HIB_ENTER) && (sm <= VS_HIB_EXIT)) {
420 } else if (!wait_idle)
423 if (wait_idle && (sm == VS_HCE_BASE))
425 } while (time_checked < timeout);
427 if (wait_idle && sm != VS_HCE_BASE)
428 dev_info(hba->dev, "wait idle tmo: 0x%x\n", val);
431 static int ufs_mtk_wait_link_state(struct ufs_hba *hba, u32 state,
432 unsigned long max_wait_ms)
434 ktime_t timeout, time_checked;
437 timeout = ktime_add_ms(ktime_get(), max_wait_ms);
439 time_checked = ktime_get();
440 ufs_mtk_dbg_sel(hba);
441 val = ufshcd_readl(hba, REG_UFS_PROBE);
447 /* Sleep for max. 200us */
448 usleep_range(100, 200);
449 } while (ktime_before(time_checked, timeout));
454 static int ufs_mtk_mphy_power_on(struct ufs_hba *hba, bool on)
456 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
457 struct phy *mphy = host->mphy;
458 struct arm_smccc_res res;
461 if (!mphy || !(on ^ host->mphy_powered_on))
465 if (ufs_mtk_is_va09_supported(hba)) {
466 ret = regulator_enable(host->reg_va09);
469 /* wait 200 us to stablize VA09 */
470 usleep_range(200, 210);
471 ufs_mtk_va09_pwr_ctrl(res, 1);
476 if (ufs_mtk_is_va09_supported(hba)) {
477 ufs_mtk_va09_pwr_ctrl(res, 0);
478 ret = regulator_disable(host->reg_va09);
484 "failed to %s va09: %d\n",
485 on ? "enable" : "disable",
488 host->mphy_powered_on = on;
494 static int ufs_mtk_get_host_clk(struct device *dev, const char *name,
495 struct clk **clk_out)
500 clk = devm_clk_get(dev, name);
509 static void ufs_mtk_boost_crypt(struct ufs_hba *hba, bool boost)
511 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
512 struct ufs_mtk_crypt_cfg *cfg;
513 struct regulator *reg;
516 if (!ufs_mtk_is_boost_crypt_enabled(hba))
520 volt = cfg->vcore_volt;
521 reg = cfg->reg_vcore;
523 ret = clk_prepare_enable(cfg->clk_crypt_mux);
525 dev_info(hba->dev, "clk_prepare_enable(): %d\n",
531 ret = regulator_set_voltage(reg, volt, INT_MAX);
534 "failed to set vcore to %d\n", volt);
538 ret = clk_set_parent(cfg->clk_crypt_mux,
539 cfg->clk_crypt_perf);
542 "failed to set clk_crypt_perf\n");
543 regulator_set_voltage(reg, 0, INT_MAX);
547 ret = clk_set_parent(cfg->clk_crypt_mux,
551 "failed to set clk_crypt_lp\n");
555 ret = regulator_set_voltage(reg, 0, INT_MAX);
558 "failed to set vcore to MIN\n");
562 clk_disable_unprepare(cfg->clk_crypt_mux);
565 static int ufs_mtk_init_host_clk(struct ufs_hba *hba, const char *name,
570 ret = ufs_mtk_get_host_clk(hba->dev, name, clk);
572 dev_info(hba->dev, "%s: failed to get %s: %d", __func__,
579 static void ufs_mtk_init_boost_crypt(struct ufs_hba *hba)
581 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
582 struct ufs_mtk_crypt_cfg *cfg;
583 struct device *dev = hba->dev;
584 struct regulator *reg;
587 host->crypt = devm_kzalloc(dev, sizeof(*(host->crypt)),
592 reg = devm_regulator_get_optional(dev, "dvfsrc-vcore");
594 dev_info(dev, "failed to get dvfsrc-vcore: %ld",
599 if (of_property_read_u32(dev->of_node, "boost-crypt-vcore-min",
601 dev_info(dev, "failed to get boost-crypt-vcore-min");
606 if (ufs_mtk_init_host_clk(hba, "crypt_mux",
607 &cfg->clk_crypt_mux))
610 if (ufs_mtk_init_host_clk(hba, "crypt_lp",
614 if (ufs_mtk_init_host_clk(hba, "crypt_perf",
615 &cfg->clk_crypt_perf))
618 cfg->reg_vcore = reg;
619 cfg->vcore_volt = volt;
620 host->caps |= UFS_MTK_CAP_BOOST_CRYPT_ENGINE;
626 static void ufs_mtk_init_va09_pwr_ctrl(struct ufs_hba *hba)
628 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
630 host->reg_va09 = regulator_get(hba->dev, "va09");
631 if (IS_ERR(host->reg_va09))
632 dev_info(hba->dev, "failed to get va09");
634 host->caps |= UFS_MTK_CAP_VA09_PWR_CTRL;
637 static void ufs_mtk_init_host_caps(struct ufs_hba *hba)
639 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
640 struct device_node *np = hba->dev->of_node;
642 if (of_property_read_bool(np, "mediatek,ufs-boost-crypt"))
643 ufs_mtk_init_boost_crypt(hba);
645 if (of_property_read_bool(np, "mediatek,ufs-support-va09"))
646 ufs_mtk_init_va09_pwr_ctrl(hba);
648 if (of_property_read_bool(np, "mediatek,ufs-disable-ah8"))
649 host->caps |= UFS_MTK_CAP_DISABLE_AH8;
651 if (of_property_read_bool(np, "mediatek,ufs-broken-vcc"))
652 host->caps |= UFS_MTK_CAP_BROKEN_VCC;
654 if (of_property_read_bool(np, "mediatek,ufs-pmc-via-fastauto"))
655 host->caps |= UFS_MTK_CAP_PMC_VIA_FASTAUTO;
657 if (of_property_read_bool(np, "mediatek,ufs-tx-skew-fix"))
658 host->caps |= UFS_MTK_CAP_TX_SKEW_FIX;
660 if (of_property_read_bool(np, "mediatek,ufs-disable-mcq"))
661 host->caps |= UFS_MTK_CAP_DISABLE_MCQ;
663 if (of_property_read_bool(np, "mediatek,ufs-rtff-mtcmos"))
664 host->caps |= UFS_MTK_CAP_RTFF_MTCMOS;
666 dev_info(hba->dev, "caps: 0x%x", host->caps);
669 static void ufs_mtk_scale_perf(struct ufs_hba *hba, bool scale_up)
671 ufs_mtk_boost_crypt(hba, scale_up);
674 static void ufs_mtk_pwr_ctrl(struct ufs_hba *hba, bool on)
676 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
679 phy_power_on(host->mphy);
680 ufs_mtk_setup_ref_clk(hba, on);
681 if (!ufshcd_is_clkscaling_supported(hba))
682 ufs_mtk_scale_perf(hba, on);
684 if (!ufshcd_is_clkscaling_supported(hba))
685 ufs_mtk_scale_perf(hba, on);
686 ufs_mtk_setup_ref_clk(hba, on);
687 phy_power_off(host->mphy);
691 static void ufs_mtk_mcq_disable_irq(struct ufs_hba *hba)
693 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
696 if (!hba->mcq_enabled)
699 if (host->mcq_nr_intr == 0)
702 for (i = 0; i < host->mcq_nr_intr; i++) {
703 irq = host->mcq_intr_info[i].irq;
706 host->is_mcq_intr_enabled = false;
709 static void ufs_mtk_mcq_enable_irq(struct ufs_hba *hba)
711 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
714 if (!hba->mcq_enabled)
717 if (host->mcq_nr_intr == 0)
720 if (host->is_mcq_intr_enabled == true)
723 for (i = 0; i < host->mcq_nr_intr; i++) {
724 irq = host->mcq_intr_info[i].irq;
727 host->is_mcq_intr_enabled = true;
731 * ufs_mtk_setup_clocks - enables/disable clocks
732 * @hba: host controller instance
733 * @on: If true, enable clocks else disable them.
734 * @status: PRE_CHANGE or POST_CHANGE notify
736 * Return: 0 on success, non-zero on failure.
738 static int ufs_mtk_setup_clocks(struct ufs_hba *hba, bool on,
739 enum ufs_notify_change_status status)
741 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
742 bool clk_pwr_off = false;
746 * In case ufs_mtk_init() is not yet done, simply ignore.
747 * This ufs_mtk_setup_clocks() shall be called from
748 * ufs_mtk_init() after init is done.
753 if (!on && status == PRE_CHANGE) {
754 if (ufshcd_is_link_off(hba)) {
756 } else if (ufshcd_is_link_hibern8(hba) ||
757 (!ufshcd_can_hibern8_during_gating(hba) &&
758 ufshcd_is_auto_hibern8_enabled(hba))) {
760 * Gate ref-clk and poweroff mphy if link state is in
761 * OFF or Hibern8 by either Auto-Hibern8 or
762 * ufshcd_link_state_transition().
764 ret = ufs_mtk_wait_link_state(hba,
772 ufs_mtk_pwr_ctrl(hba, false);
773 ufs_mtk_mcq_disable_irq(hba);
774 } else if (on && status == POST_CHANGE) {
775 ufs_mtk_pwr_ctrl(hba, true);
776 ufs_mtk_mcq_enable_irq(hba);
782 static void ufs_mtk_get_controller_version(struct ufs_hba *hba)
784 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
787 if (host->hw_ver.major)
790 /* Set default (minimum) version anyway */
791 host->hw_ver.major = 2;
793 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_LOCALVERINFO), &ver);
795 if (ver >= UFS_UNIPRO_VER_1_8) {
796 host->hw_ver.major = 3;
798 * Fix HCI version for some platforms with
801 if (hba->ufs_version < ufshci_version(3, 0))
802 hba->ufs_version = ufshci_version(3, 0);
807 static u32 ufs_mtk_get_ufs_hci_version(struct ufs_hba *hba)
809 return hba->ufs_version;
813 * ufs_mtk_init_clocks - Init mtk driver private clocks
815 * @hba: per adapter instance
817 static void ufs_mtk_init_clocks(struct ufs_hba *hba)
819 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
820 struct list_head *head = &hba->clk_list_head;
821 struct ufs_mtk_clk *mclk = &host->mclk;
822 struct ufs_clk_info *clki, *clki_tmp;
825 * Find private clocks and store them in struct ufs_mtk_clk.
826 * Remove "ufs_sel_min_src" and "ufs_sel_min_src" from list to avoid
827 * being switched on/off in clock gating.
829 list_for_each_entry_safe(clki, clki_tmp, head, list) {
830 if (!strcmp(clki->name, "ufs_sel")) {
831 host->mclk.ufs_sel_clki = clki;
832 } else if (!strcmp(clki->name, "ufs_sel_max_src")) {
833 host->mclk.ufs_sel_max_clki = clki;
834 clk_disable_unprepare(clki->clk);
835 list_del(&clki->list);
836 } else if (!strcmp(clki->name, "ufs_sel_min_src")) {
837 host->mclk.ufs_sel_min_clki = clki;
838 clk_disable_unprepare(clki->clk);
839 list_del(&clki->list);
843 if (!mclk->ufs_sel_clki || !mclk->ufs_sel_max_clki ||
844 !mclk->ufs_sel_min_clki) {
845 hba->caps &= ~UFSHCD_CAP_CLK_SCALING;
847 "%s: Clk-scaling not ready. Feature disabled.",
852 #define MAX_VCC_NAME 30
853 static int ufs_mtk_vreg_fix_vcc(struct ufs_hba *hba)
855 struct ufs_vreg_info *info = &hba->vreg_info;
856 struct device_node *np = hba->dev->of_node;
857 struct device *dev = hba->dev;
858 char vcc_name[MAX_VCC_NAME];
859 struct arm_smccc_res res;
862 if (hba->vreg_info.vcc)
865 if (of_property_read_bool(np, "mediatek,ufs-vcc-by-num")) {
866 ufs_mtk_get_vcc_num(res);
867 if (res.a1 > UFS_VCC_NONE && res.a1 < UFS_VCC_MAX)
868 snprintf(vcc_name, MAX_VCC_NAME, "vcc-opt%lu", res.a1);
871 } else if (of_property_read_bool(np, "mediatek,ufs-vcc-by-ver")) {
872 ver = (hba->dev_info.wspecversion & 0xF00) >> 8;
873 snprintf(vcc_name, MAX_VCC_NAME, "vcc-ufs%u", ver);
878 err = ufshcd_populate_vreg(dev, vcc_name, &info->vcc, false);
882 err = ufshcd_get_vreg(dev, info->vcc);
886 err = regulator_enable(info->vcc->reg);
888 info->vcc->enabled = true;
889 dev_info(dev, "%s: %s enabled\n", __func__, vcc_name);
895 static void ufs_mtk_vreg_fix_vccqx(struct ufs_hba *hba)
897 struct ufs_vreg_info *info = &hba->vreg_info;
898 struct ufs_vreg **vreg_on, **vreg_off;
900 if (hba->dev_info.wspecversion >= 0x0300) {
901 vreg_on = &info->vccq;
902 vreg_off = &info->vccq2;
904 vreg_on = &info->vccq2;
905 vreg_off = &info->vccq;
909 (*vreg_on)->always_on = true;
912 regulator_disable((*vreg_off)->reg);
913 devm_kfree(hba->dev, (*vreg_off)->name);
914 devm_kfree(hba->dev, *vreg_off);
919 static void ufs_mtk_init_mcq_irq(struct ufs_hba *hba)
921 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
922 struct platform_device *pdev;
926 host->mcq_nr_intr = UFSHCD_MAX_Q_NR;
927 pdev = container_of(hba->dev, struct platform_device, dev);
929 if (host->caps & UFS_MTK_CAP_DISABLE_MCQ)
932 for (i = 0; i < host->mcq_nr_intr; i++) {
933 /* irq index 0 is legacy irq, sq/cq irq start from index 1 */
934 irq = platform_get_irq(pdev, i + 1);
936 host->mcq_intr_info[i].irq = MTK_MCQ_INVALID_IRQ;
939 host->mcq_intr_info[i].hba = hba;
940 host->mcq_intr_info[i].irq = irq;
941 dev_info(hba->dev, "get platform mcq irq: %d, %d\n", i, irq);
946 /* invalidate irq info */
947 for (i = 0; i < host->mcq_nr_intr; i++)
948 host->mcq_intr_info[i].irq = MTK_MCQ_INVALID_IRQ;
950 host->mcq_nr_intr = 0;
954 * ufs_mtk_init - find other essential mmio bases
955 * @hba: host controller instance
957 * Binds PHY with controller and powers up PHY enabling clocks
960 * Return: -EPROBE_DEFER if binding fails, returns negative error
961 * on phy power up failure and returns zero on success.
963 static int ufs_mtk_init(struct ufs_hba *hba)
965 const struct of_device_id *id;
966 struct device *dev = hba->dev;
967 struct ufs_mtk_host *host;
968 struct Scsi_Host *shost = hba->host;
970 struct arm_smccc_res res;
972 host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
975 dev_info(dev, "%s: no memory for mtk ufs host\n", __func__);
980 ufshcd_set_variant(hba, host);
982 id = of_match_device(ufs_mtk_of_match, dev);
988 /* Initialize host capability */
989 ufs_mtk_init_host_caps(hba);
991 ufs_mtk_init_mcq_irq(hba);
993 err = ufs_mtk_bind_mphy(hba);
995 goto out_variant_clear;
997 ufs_mtk_init_reset(hba);
999 /* backup mphy setting if mphy can reset */
1000 if (host->mphy_reset)
1001 ufs_mtk_mphy_ctrl(UFS_MPHY_BACKUP, res);
1003 /* Enable runtime autosuspend */
1004 hba->caps |= UFSHCD_CAP_RPM_AUTOSUSPEND;
1006 /* Enable clock-gating */
1007 hba->caps |= UFSHCD_CAP_CLK_GATING;
1009 /* Enable inline encryption */
1010 hba->caps |= UFSHCD_CAP_CRYPTO;
1012 /* Enable WriteBooster */
1013 hba->caps |= UFSHCD_CAP_WB_EN;
1015 /* Enable clk scaling*/
1016 hba->caps |= UFSHCD_CAP_CLK_SCALING;
1018 /* Set runtime pm delay to replace default */
1019 shost->rpm_autosuspend_delay = MTK_RPM_AUTOSUSPEND_DELAY_MS;
1021 hba->quirks |= UFSHCI_QUIRK_SKIP_MANUAL_WB_FLUSH_CTRL;
1022 hba->quirks |= UFSHCD_QUIRK_MCQ_BROKEN_INTR;
1023 hba->quirks |= UFSHCD_QUIRK_MCQ_BROKEN_RTC;
1024 hba->vps->wb_flush_threshold = UFS_WB_BUF_REMAIN_PERCENT(80);
1026 if (host->caps & UFS_MTK_CAP_DISABLE_AH8)
1027 hba->caps |= UFSHCD_CAP_HIBERN8_WITH_CLK_GATING;
1029 ufs_mtk_init_clocks(hba);
1032 * ufshcd_vops_init() is invoked after
1033 * ufshcd_setup_clock(true) in ufshcd_hba_init() thus
1034 * phy clock setup is skipped.
1036 * Enable phy clocks specifically here.
1038 ufs_mtk_mphy_power_on(hba, true);
1040 if (ufs_mtk_is_rtff_mtcmos(hba)) {
1041 /* First Restore here, to avoid backup unexpected value */
1042 ufs_mtk_mtcmos_ctrl(false, res);
1044 /* Power on to init */
1045 ufs_mtk_mtcmos_ctrl(true, res);
1048 ufs_mtk_setup_clocks(hba, true, POST_CHANGE);
1050 host->ip_ver = ufshcd_readl(hba, REG_UFS_MTK_IP_VER);
1055 ufshcd_set_variant(hba, NULL);
1060 static bool ufs_mtk_pmc_via_fastauto(struct ufs_hba *hba,
1061 struct ufs_pa_layer_attr *dev_req_params)
1063 if (!ufs_mtk_is_pmc_via_fastauto(hba))
1066 if (dev_req_params->hs_rate == hba->pwr_info.hs_rate)
1069 if (dev_req_params->pwr_tx != FAST_MODE &&
1070 dev_req_params->gear_tx < UFS_HS_G4)
1073 if (dev_req_params->pwr_rx != FAST_MODE &&
1074 dev_req_params->gear_rx < UFS_HS_G4)
1080 static int ufs_mtk_pre_pwr_change(struct ufs_hba *hba,
1081 struct ufs_pa_layer_attr *dev_max_params,
1082 struct ufs_pa_layer_attr *dev_req_params)
1084 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
1085 struct ufs_host_params host_params;
1088 ufshcd_init_host_params(&host_params);
1089 host_params.hs_rx_gear = UFS_HS_G5;
1090 host_params.hs_tx_gear = UFS_HS_G5;
1092 ret = ufshcd_negotiate_pwr_params(&host_params, dev_max_params, dev_req_params);
1094 pr_info("%s: failed to determine capabilities\n",
1098 if (ufs_mtk_pmc_via_fastauto(hba, dev_req_params)) {
1099 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), true);
1100 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), UFS_HS_G1);
1102 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), true);
1103 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), UFS_HS_G1);
1105 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
1106 dev_req_params->lane_tx);
1107 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
1108 dev_req_params->lane_rx);
1109 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
1110 dev_req_params->hs_rate);
1112 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXHSADAPTTYPE),
1115 ret = ufshcd_uic_change_pwr_mode(hba,
1116 FASTAUTO_MODE << 4 | FASTAUTO_MODE);
1119 dev_err(hba->dev, "%s: HSG1B FASTAUTO failed ret=%d\n",
1124 if (host->hw_ver.major >= 3) {
1125 ret = ufshcd_dme_configure_adapt(hba,
1126 dev_req_params->gear_tx,
1133 static int ufs_mtk_pwr_change_notify(struct ufs_hba *hba,
1134 enum ufs_notify_change_status stage,
1135 struct ufs_pa_layer_attr *dev_max_params,
1136 struct ufs_pa_layer_attr *dev_req_params)
1142 ret = ufs_mtk_pre_pwr_change(hba, dev_max_params,
1155 static int ufs_mtk_unipro_set_lpm(struct ufs_hba *hba, bool lpm)
1158 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
1160 ret = ufshcd_dme_set(hba,
1161 UIC_ARG_MIB_SEL(VS_UNIPROPOWERDOWNCONTROL, 0),
1165 * Forcibly set as non-LPM mode if UIC commands is failed
1166 * to use default hba_enable_delay_us value for re-enabling
1169 host->unipro_lpm = lpm;
1175 static int ufs_mtk_pre_link(struct ufs_hba *hba)
1180 ufs_mtk_get_controller_version(hba);
1182 ret = ufs_mtk_unipro_set_lpm(hba, false);
1187 * Setting PA_Local_TX_LCC_Enable to 0 before link startup
1188 * to make sure that both host and device TX LCC are disabled
1189 * once link startup is completed.
1191 ret = ufshcd_disable_host_tx_lcc(hba);
1195 /* disable deep stall */
1196 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(VS_SAVEPOWERCONTROL), &tmp);
1202 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(VS_SAVEPOWERCONTROL), tmp);
1207 static void ufs_mtk_setup_clk_gating(struct ufs_hba *hba)
1211 if (ufshcd_is_clkgating_allowed(hba)) {
1212 if (ufshcd_is_auto_hibern8_supported(hba) && hba->ahit)
1213 ah_ms = FIELD_GET(UFSHCI_AHIBERN8_TIMER_MASK,
1217 ufshcd_clkgate_delay_set(hba->dev, ah_ms + 5);
1221 static void ufs_mtk_post_link(struct ufs_hba *hba)
1223 /* enable unipro clock gating feature */
1224 ufs_mtk_cfg_unipro_cg(hba, true);
1226 /* will be configured during probe hba */
1227 if (ufshcd_is_auto_hibern8_supported(hba))
1228 hba->ahit = FIELD_PREP(UFSHCI_AHIBERN8_TIMER_MASK, 10) |
1229 FIELD_PREP(UFSHCI_AHIBERN8_SCALE_MASK, 3);
1231 ufs_mtk_setup_clk_gating(hba);
1234 static int ufs_mtk_link_startup_notify(struct ufs_hba *hba,
1235 enum ufs_notify_change_status stage)
1241 ret = ufs_mtk_pre_link(hba);
1244 ufs_mtk_post_link(hba);
1254 static int ufs_mtk_device_reset(struct ufs_hba *hba)
1256 struct arm_smccc_res res;
1258 /* disable hba before device reset */
1259 ufshcd_hba_stop(hba);
1261 ufs_mtk_device_reset_ctrl(0, res);
1264 * The reset signal is active low. UFS devices shall detect
1265 * more than or equal to 1us of positive or negative RST_n
1268 * To be on safe side, keep the reset low for at least 10us.
1270 usleep_range(10, 15);
1272 ufs_mtk_device_reset_ctrl(1, res);
1274 /* Some devices may need time to respond to rst_n */
1275 usleep_range(10000, 15000);
1277 dev_info(hba->dev, "device reset done\n");
1282 static int ufs_mtk_link_set_hpm(struct ufs_hba *hba)
1286 err = ufshcd_hba_enable(hba);
1290 err = ufs_mtk_unipro_set_lpm(hba, false);
1294 err = ufshcd_uic_hibern8_exit(hba);
1298 /* Check link state to make sure exit h8 success */
1299 ufs_mtk_wait_idle_state(hba, 5);
1300 err = ufs_mtk_wait_link_state(hba, VS_LINK_UP, 100);
1302 dev_warn(hba->dev, "exit h8 state fail, err=%d\n", err);
1305 ufshcd_set_link_active(hba);
1307 err = ufshcd_make_hba_operational(hba);
1311 if (hba->mcq_enabled) {
1312 ufs_mtk_config_mcq(hba, false);
1313 ufshcd_mcq_make_queues_operational(hba);
1314 ufshcd_mcq_config_mac(hba, hba->nutrs);
1315 ufshcd_mcq_enable(hba);
1321 static int ufs_mtk_link_set_lpm(struct ufs_hba *hba)
1325 /* Disable reset confirm feature by UniPro */
1327 (ufshcd_readl(hba, REG_UFS_XOUFS_CTRL) & ~0x100),
1328 REG_UFS_XOUFS_CTRL);
1330 err = ufs_mtk_unipro_set_lpm(hba, true);
1332 /* Resume UniPro state for following error recovery */
1333 ufs_mtk_unipro_set_lpm(hba, false);
1340 static void ufs_mtk_vccqx_set_lpm(struct ufs_hba *hba, bool lpm)
1342 struct ufs_vreg *vccqx = NULL;
1344 if (hba->vreg_info.vccq)
1345 vccqx = hba->vreg_info.vccq;
1347 vccqx = hba->vreg_info.vccq2;
1349 regulator_set_mode(vccqx->reg,
1350 lpm ? REGULATOR_MODE_IDLE : REGULATOR_MODE_NORMAL);
1353 static void ufs_mtk_vsx_set_lpm(struct ufs_hba *hba, bool lpm)
1355 struct arm_smccc_res res;
1357 ufs_mtk_device_pwr_ctrl(!lpm,
1358 (unsigned long)hba->dev_info.wspecversion,
1362 static void ufs_mtk_dev_vreg_set_lpm(struct ufs_hba *hba, bool lpm)
1364 bool skip_vccqx = false;
1366 /* Prevent entering LPM when device is still active */
1367 if (lpm && ufshcd_is_ufs_dev_active(hba))
1370 /* Skip vccqx lpm control and control vsx only */
1371 if (!hba->vreg_info.vccq && !hba->vreg_info.vccq2)
1374 /* VCC is always-on, control vsx only */
1375 if (!hba->vreg_info.vcc)
1378 /* Broken vcc keep vcc always on, most case control vsx only */
1379 if (lpm && hba->vreg_info.vcc && hba->vreg_info.vcc->enabled) {
1380 /* Some device vccqx/vsx can enter lpm */
1381 if (ufs_mtk_is_allow_vccqx_lpm(hba))
1383 else /* control vsx only */
1389 ufs_mtk_vccqx_set_lpm(hba, lpm);
1390 ufs_mtk_vsx_set_lpm(hba, lpm);
1392 ufs_mtk_vsx_set_lpm(hba, lpm);
1394 ufs_mtk_vccqx_set_lpm(hba, lpm);
1398 static void ufs_mtk_auto_hibern8_disable(struct ufs_hba *hba)
1402 /* disable auto-hibern8 */
1403 ufshcd_writel(hba, 0, REG_AUTO_HIBERNATE_IDLE_TIMER);
1405 /* wait host return to idle state when auto-hibern8 off */
1406 ufs_mtk_wait_idle_state(hba, 5);
1408 ret = ufs_mtk_wait_link_state(hba, VS_LINK_UP, 100);
1410 dev_warn(hba->dev, "exit h8 state fail, ret=%d\n", ret);
1413 static int ufs_mtk_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op,
1414 enum ufs_notify_change_status status)
1417 struct arm_smccc_res res;
1419 if (status == PRE_CHANGE) {
1420 if (ufshcd_is_auto_hibern8_supported(hba))
1421 ufs_mtk_auto_hibern8_disable(hba);
1425 if (ufshcd_is_link_hibern8(hba)) {
1426 err = ufs_mtk_link_set_lpm(hba);
1431 if (!ufshcd_is_link_active(hba)) {
1433 * Make sure no error will be returned to prevent
1434 * ufshcd_suspend() re-enabling regulators while vreg is still
1435 * in low-power mode.
1437 err = ufs_mtk_mphy_power_on(hba, false);
1442 if (ufshcd_is_link_off(hba))
1443 ufs_mtk_device_reset_ctrl(0, res);
1445 ufs_mtk_sram_pwr_ctrl(false, res);
1450 * Set link as off state enforcedly to trigger
1451 * ufshcd_host_reset_and_restore() in ufshcd_suspend()
1452 * for completed host reset.
1454 ufshcd_set_link_off(hba);
1458 static int ufs_mtk_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
1461 struct arm_smccc_res res;
1463 if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL)
1464 ufs_mtk_dev_vreg_set_lpm(hba, false);
1466 ufs_mtk_sram_pwr_ctrl(true, res);
1468 err = ufs_mtk_mphy_power_on(hba, true);
1472 if (ufshcd_is_link_hibern8(hba)) {
1473 err = ufs_mtk_link_set_hpm(hba);
1480 return ufshcd_link_recovery(hba);
1483 static void ufs_mtk_dbg_register_dump(struct ufs_hba *hba)
1485 /* Dump ufshci register 0x140 ~ 0x14C */
1486 ufshcd_dump_regs(hba, REG_UFS_XOUFS_CTRL, 0x10,
1487 "XOUFS Ctrl (0x140): ");
1489 ufshcd_dump_regs(hba, REG_UFS_EXTREG, 0x4, "Ext Reg ");
1491 /* Dump ufshci register 0x2200 ~ 0x22AC */
1492 ufshcd_dump_regs(hba, REG_UFS_MPHYCTRL,
1493 REG_UFS_REJECT_MON - REG_UFS_MPHYCTRL + 4,
1494 "MPHY Ctrl (0x2200): ");
1496 /* Direct debugging information to REG_MTK_PROBE */
1497 ufs_mtk_dbg_sel(hba);
1498 ufshcd_dump_regs(hba, REG_UFS_PROBE, 0x4, "Debug Probe ");
1501 static int ufs_mtk_apply_dev_quirks(struct ufs_hba *hba)
1503 struct ufs_dev_info *dev_info = &hba->dev_info;
1504 u16 mid = dev_info->wmanufacturerid;
1506 if (mid == UFS_VENDOR_SAMSUNG) {
1507 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 6);
1508 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME), 10);
1509 } else if (mid == UFS_VENDOR_MICRON) {
1510 /* Only for the host which have TX skew issue */
1511 if (ufs_mtk_is_tx_skew_fix(hba) &&
1512 (STR_PRFX_EQUAL("MT128GBCAV2U31", dev_info->model) ||
1513 STR_PRFX_EQUAL("MT256GBCAV4U31", dev_info->model) ||
1514 STR_PRFX_EQUAL("MT512GBCAV8U31", dev_info->model) ||
1515 STR_PRFX_EQUAL("MT256GBEAX4U40", dev_info->model) ||
1516 STR_PRFX_EQUAL("MT512GAYAX4U40", dev_info->model) ||
1517 STR_PRFX_EQUAL("MT001TAYAX8U40", dev_info->model))) {
1518 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 8);
1523 * Decide waiting time before gating reference clock and
1524 * after ungating reference clock according to vendors'
1527 if (mid == UFS_VENDOR_SAMSUNG)
1528 ufs_mtk_setup_ref_clk_wait_us(hba, 1);
1529 else if (mid == UFS_VENDOR_SKHYNIX)
1530 ufs_mtk_setup_ref_clk_wait_us(hba, 30);
1531 else if (mid == UFS_VENDOR_TOSHIBA)
1532 ufs_mtk_setup_ref_clk_wait_us(hba, 100);
1534 ufs_mtk_setup_ref_clk_wait_us(hba,
1535 REFCLK_DEFAULT_WAIT_US);
1539 static void ufs_mtk_fixup_dev_quirks(struct ufs_hba *hba)
1541 ufshcd_fixup_dev_quirks(hba, ufs_mtk_dev_fixups);
1543 if (ufs_mtk_is_broken_vcc(hba) && hba->vreg_info.vcc &&
1544 (hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_AFTER_LPM)) {
1545 hba->vreg_info.vcc->always_on = true;
1547 * VCC will be kept always-on thus we don't
1548 * need any delay during regulator operations
1550 hba->dev_quirks &= ~(UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM |
1551 UFS_DEVICE_QUIRK_DELAY_AFTER_LPM);
1554 ufs_mtk_vreg_fix_vcc(hba);
1555 ufs_mtk_vreg_fix_vccqx(hba);
1558 static void ufs_mtk_event_notify(struct ufs_hba *hba,
1559 enum ufs_event_type evt, void *data)
1561 unsigned int val = *(u32 *)data;
1565 trace_ufs_mtk_event(evt, val);
1567 /* Print details of UIC Errors */
1568 if (evt <= UFS_EVT_DME_ERR) {
1570 "Host UIC Error Code (%s): %08x\n",
1571 ufs_uic_err_str[evt], val);
1575 if (evt == UFS_EVT_PA_ERR) {
1576 for_each_set_bit(bit, ®, ARRAY_SIZE(ufs_uic_pa_err_str))
1577 dev_info(hba->dev, "%s\n", ufs_uic_pa_err_str[bit]);
1580 if (evt == UFS_EVT_DL_ERR) {
1581 for_each_set_bit(bit, ®, ARRAY_SIZE(ufs_uic_dl_err_str))
1582 dev_info(hba->dev, "%s\n", ufs_uic_dl_err_str[bit]);
1586 static void ufs_mtk_config_scaling_param(struct ufs_hba *hba,
1587 struct devfreq_dev_profile *profile,
1588 struct devfreq_simple_ondemand_data *data)
1590 /* Customize min gear in clk scaling */
1591 hba->clk_scaling.min_gear = UFS_HS_G4;
1593 hba->vps->devfreq_profile.polling_ms = 200;
1594 hba->vps->ondemand_data.upthreshold = 50;
1595 hba->vps->ondemand_data.downdifferential = 20;
1599 * ufs_mtk_clk_scale - Internal clk scaling operation
1601 * MTK platform supports clk scaling by switching parent of ufs_sel(mux).
1602 * The ufs_sel downstream to ufs_ck which feeds directly to UFS hardware.
1603 * Max and min clocks rate of ufs_sel defined in dts should match rate of
1604 * "ufs_sel_max_src" and "ufs_sel_min_src" respectively.
1605 * This prevent changing rate of pll clock that is shared between modules.
1607 * @hba: per adapter instance
1608 * @scale_up: True for scaling up and false for scaling down
1610 static void ufs_mtk_clk_scale(struct ufs_hba *hba, bool scale_up)
1612 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
1613 struct ufs_mtk_clk *mclk = &host->mclk;
1614 struct ufs_clk_info *clki = mclk->ufs_sel_clki;
1617 ret = clk_prepare_enable(clki->clk);
1620 "clk_prepare_enable() fail, ret: %d\n", ret);
1625 ret = clk_set_parent(clki->clk, mclk->ufs_sel_max_clki->clk);
1626 clki->curr_freq = clki->max_freq;
1628 ret = clk_set_parent(clki->clk, mclk->ufs_sel_min_clki->clk);
1629 clki->curr_freq = clki->min_freq;
1634 "Failed to set ufs_sel_clki, ret: %d\n", ret);
1637 clk_disable_unprepare(clki->clk);
1639 trace_ufs_mtk_clk_scale(clki->name, scale_up, clk_get_rate(clki->clk));
1642 static int ufs_mtk_clk_scale_notify(struct ufs_hba *hba, bool scale_up,
1643 enum ufs_notify_change_status status)
1645 if (!ufshcd_is_clkscaling_supported(hba))
1648 if (status == PRE_CHANGE) {
1649 /* Switch parent before clk_set_rate() */
1650 ufs_mtk_clk_scale(hba, scale_up);
1652 /* Request interrupt latency QoS accordingly */
1653 ufs_mtk_scale_perf(hba, scale_up);
1659 static int ufs_mtk_get_hba_mac(struct ufs_hba *hba)
1661 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
1663 /* MCQ operation not permitted */
1664 if (host->caps & UFS_MTK_CAP_DISABLE_MCQ)
1667 return MAX_SUPP_MAC;
1670 static int ufs_mtk_op_runtime_config(struct ufs_hba *hba)
1672 struct ufshcd_mcq_opr_info_t *opr;
1675 hba->mcq_opr[OPR_SQD].offset = REG_UFS_MTK_SQD;
1676 hba->mcq_opr[OPR_SQIS].offset = REG_UFS_MTK_SQIS;
1677 hba->mcq_opr[OPR_CQD].offset = REG_UFS_MTK_CQD;
1678 hba->mcq_opr[OPR_CQIS].offset = REG_UFS_MTK_CQIS;
1680 for (i = 0; i < OPR_MAX; i++) {
1681 opr = &hba->mcq_opr[i];
1682 opr->stride = REG_UFS_MCQ_STRIDE;
1683 opr->base = hba->mmio_base + opr->offset;
1689 static int ufs_mtk_mcq_config_resource(struct ufs_hba *hba)
1691 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
1693 /* fail mcq initialization if interrupt is not filled properly */
1694 if (!host->mcq_nr_intr) {
1695 dev_info(hba->dev, "IRQs not ready. MCQ disabled.");
1699 hba->mcq_base = hba->mmio_base + MCQ_QUEUE_OFFSET(hba->mcq_capabilities);
1703 static irqreturn_t ufs_mtk_mcq_intr(int irq, void *__intr_info)
1705 struct ufs_mtk_mcq_intr_info *mcq_intr_info = __intr_info;
1706 struct ufs_hba *hba = mcq_intr_info->hba;
1707 struct ufs_hw_queue *hwq;
1709 int qid = mcq_intr_info->qid;
1711 hwq = &hba->uhq[qid];
1713 events = ufshcd_mcq_read_cqis(hba, qid);
1715 ufshcd_mcq_write_cqis(hba, events, qid);
1717 if (events & UFSHCD_MCQ_CQIS_TAIL_ENT_PUSH_STS)
1718 ufshcd_mcq_poll_cqe_lock(hba, hwq);
1723 static int ufs_mtk_config_mcq_irq(struct ufs_hba *hba)
1725 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
1729 for (i = 0; i < host->mcq_nr_intr; i++) {
1730 irq = host->mcq_intr_info[i].irq;
1731 if (irq == MTK_MCQ_INVALID_IRQ) {
1732 dev_err(hba->dev, "invalid irq. %d\n", i);
1736 host->mcq_intr_info[i].qid = i;
1737 ret = devm_request_irq(hba->dev, irq, ufs_mtk_mcq_intr, 0, UFSHCD,
1738 &host->mcq_intr_info[i]);
1740 dev_dbg(hba->dev, "request irq %d intr %s\n", irq, ret ? "failed" : "");
1743 dev_err(hba->dev, "Cannot request irq %d\n", ret);
1751 static int ufs_mtk_config_mcq(struct ufs_hba *hba, bool irq)
1753 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
1756 if (!host->mcq_set_intr) {
1757 /* Disable irq option register */
1758 ufshcd_rmwl(hba, MCQ_INTR_EN_MSK, 0, REG_UFS_MMIO_OPT_CTRL_0);
1761 ret = ufs_mtk_config_mcq_irq(hba);
1766 host->mcq_set_intr = true;
1769 ufshcd_rmwl(hba, MCQ_AH8, MCQ_AH8, REG_UFS_MMIO_OPT_CTRL_0);
1770 ufshcd_rmwl(hba, MCQ_INTR_EN_MSK, MCQ_MULTI_INTR_EN, REG_UFS_MMIO_OPT_CTRL_0);
1775 static int ufs_mtk_config_esi(struct ufs_hba *hba)
1777 return ufs_mtk_config_mcq(hba, true);
1781 * struct ufs_hba_mtk_vops - UFS MTK specific variant operations
1783 * The variant operations configure the necessary controller and PHY
1784 * handshake during initialization.
1786 static const struct ufs_hba_variant_ops ufs_hba_mtk_vops = {
1787 .name = "mediatek.ufshci",
1788 .max_num_rtt = MTK_MAX_NUM_RTT,
1789 .init = ufs_mtk_init,
1790 .get_ufs_hci_version = ufs_mtk_get_ufs_hci_version,
1791 .setup_clocks = ufs_mtk_setup_clocks,
1792 .hce_enable_notify = ufs_mtk_hce_enable_notify,
1793 .link_startup_notify = ufs_mtk_link_startup_notify,
1794 .pwr_change_notify = ufs_mtk_pwr_change_notify,
1795 .apply_dev_quirks = ufs_mtk_apply_dev_quirks,
1796 .fixup_dev_quirks = ufs_mtk_fixup_dev_quirks,
1797 .suspend = ufs_mtk_suspend,
1798 .resume = ufs_mtk_resume,
1799 .dbg_register_dump = ufs_mtk_dbg_register_dump,
1800 .device_reset = ufs_mtk_device_reset,
1801 .event_notify = ufs_mtk_event_notify,
1802 .config_scaling_param = ufs_mtk_config_scaling_param,
1803 .clk_scale_notify = ufs_mtk_clk_scale_notify,
1805 .get_hba_mac = ufs_mtk_get_hba_mac,
1806 .op_runtime_config = ufs_mtk_op_runtime_config,
1807 .mcq_config_resource = ufs_mtk_mcq_config_resource,
1808 .config_esi = ufs_mtk_config_esi,
1812 * ufs_mtk_probe - probe routine of the driver
1813 * @pdev: pointer to Platform device handle
1815 * Return: zero for success and non-zero for failure.
1817 static int ufs_mtk_probe(struct platform_device *pdev)
1820 struct device *dev = &pdev->dev;
1821 struct device_node *reset_node;
1822 struct platform_device *reset_pdev;
1823 struct device_link *link;
1825 reset_node = of_find_compatible_node(NULL, NULL,
1828 dev_notice(dev, "find ti,syscon-reset fail\n");
1831 reset_pdev = of_find_device_by_node(reset_node);
1833 dev_notice(dev, "find reset_pdev fail\n");
1836 link = device_link_add(dev, &reset_pdev->dev,
1837 DL_FLAG_AUTOPROBE_CONSUMER);
1838 put_device(&reset_pdev->dev);
1840 dev_notice(dev, "add reset device_link fail\n");
1843 /* supplier is not probed */
1844 if (link->status == DL_STATE_DORMANT) {
1845 err = -EPROBE_DEFER;
1850 /* perform generic probe */
1851 err = ufshcd_pltfrm_init(pdev, &ufs_hba_mtk_vops);
1855 dev_err(dev, "probe failed %d\n", err);
1857 of_node_put(reset_node);
1862 * ufs_mtk_remove - set driver_data of the device to NULL
1863 * @pdev: pointer to platform device handle
1867 static void ufs_mtk_remove(struct platform_device *pdev)
1869 struct ufs_hba *hba = platform_get_drvdata(pdev);
1871 pm_runtime_get_sync(&(pdev)->dev);
1875 #ifdef CONFIG_PM_SLEEP
1876 static int ufs_mtk_system_suspend(struct device *dev)
1878 struct ufs_hba *hba = dev_get_drvdata(dev);
1879 struct arm_smccc_res res;
1882 ret = ufshcd_system_suspend(dev);
1886 ufs_mtk_dev_vreg_set_lpm(hba, true);
1888 if (ufs_mtk_is_rtff_mtcmos(hba))
1889 ufs_mtk_mtcmos_ctrl(false, res);
1894 static int ufs_mtk_system_resume(struct device *dev)
1896 struct ufs_hba *hba = dev_get_drvdata(dev);
1897 struct arm_smccc_res res;
1899 ufs_mtk_dev_vreg_set_lpm(hba, false);
1901 if (ufs_mtk_is_rtff_mtcmos(hba))
1902 ufs_mtk_mtcmos_ctrl(true, res);
1904 return ufshcd_system_resume(dev);
1909 static int ufs_mtk_runtime_suspend(struct device *dev)
1911 struct ufs_hba *hba = dev_get_drvdata(dev);
1912 struct arm_smccc_res res;
1915 ret = ufshcd_runtime_suspend(dev);
1919 ufs_mtk_dev_vreg_set_lpm(hba, true);
1921 if (ufs_mtk_is_rtff_mtcmos(hba))
1922 ufs_mtk_mtcmos_ctrl(false, res);
1927 static int ufs_mtk_runtime_resume(struct device *dev)
1929 struct ufs_hba *hba = dev_get_drvdata(dev);
1930 struct arm_smccc_res res;
1932 if (ufs_mtk_is_rtff_mtcmos(hba))
1933 ufs_mtk_mtcmos_ctrl(true, res);
1935 ufs_mtk_dev_vreg_set_lpm(hba, false);
1937 return ufshcd_runtime_resume(dev);
1941 static const struct dev_pm_ops ufs_mtk_pm_ops = {
1942 SET_SYSTEM_SLEEP_PM_OPS(ufs_mtk_system_suspend,
1943 ufs_mtk_system_resume)
1944 SET_RUNTIME_PM_OPS(ufs_mtk_runtime_suspend,
1945 ufs_mtk_runtime_resume, NULL)
1946 .prepare = ufshcd_suspend_prepare,
1947 .complete = ufshcd_resume_complete,
1950 static struct platform_driver ufs_mtk_pltform = {
1951 .probe = ufs_mtk_probe,
1952 .remove_new = ufs_mtk_remove,
1954 .name = "ufshcd-mtk",
1955 .pm = &ufs_mtk_pm_ops,
1956 .of_match_table = ufs_mtk_of_match,
1962 MODULE_DESCRIPTION("MediaTek UFS Host Driver");
1963 MODULE_LICENSE("GPL v2");
1965 module_platform_driver(ufs_mtk_pltform);