1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2019 MediaTek Inc.
5 * Stanley Chu <stanley.chu@mediatek.com>
6 * Peter Wang <peter.wang@mediatek.com>
9 #include <linux/arm-smccc.h>
10 #include <linux/bitfield.h>
12 #include <linux/of_address.h>
13 #include <linux/of_device.h>
14 #include <linux/phy/phy.h>
15 #include <linux/platform_device.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/reset.h>
18 #include <linux/soc/mediatek/mtk_sip_svc.h>
21 #include "ufshcd-crypto.h"
22 #include "ufshcd-pltfrm.h"
23 #include "ufs_quirks.h"
25 #include "ufs-mediatek.h"
27 #define CREATE_TRACE_POINTS
28 #include "ufs-mediatek-trace.h"
30 #define ufs_mtk_smc(cmd, val, res) \
31 arm_smccc_smc(MTK_SIP_UFS_CONTROL, \
32 cmd, val, 0, 0, 0, 0, 0, &(res))
34 #define ufs_mtk_va09_pwr_ctrl(res, on) \
35 ufs_mtk_smc(UFS_MTK_SIP_VA09_PWR_CTRL, on, res)
37 #define ufs_mtk_crypto_ctrl(res, enable) \
38 ufs_mtk_smc(UFS_MTK_SIP_CRYPTO_CTRL, enable, res)
40 #define ufs_mtk_ref_clk_notify(on, res) \
41 ufs_mtk_smc(UFS_MTK_SIP_REF_CLK_NOTIFICATION, on, res)
43 #define ufs_mtk_device_reset_ctrl(high, res) \
44 ufs_mtk_smc(UFS_MTK_SIP_DEVICE_RESET, high, res)
46 static struct ufs_dev_fix ufs_mtk_dev_fixups[] = {
47 UFS_FIX(UFS_VENDOR_MICRON, UFS_ANY_MODEL,
48 UFS_DEVICE_QUIRK_DELAY_AFTER_LPM),
49 UFS_FIX(UFS_VENDOR_SKHYNIX, "H9HQ21AFAMZDAR",
50 UFS_DEVICE_QUIRK_SUPPORT_EXTENDED_FEATURES),
54 static const struct of_device_id ufs_mtk_of_match[] = {
55 { .compatible = "mediatek,mt8183-ufshci" },
59 static bool ufs_mtk_is_boost_crypt_enabled(struct ufs_hba *hba)
61 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
63 return !!(host->caps & UFS_MTK_CAP_BOOST_CRYPT_ENGINE);
66 static bool ufs_mtk_is_va09_supported(struct ufs_hba *hba)
68 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
70 return !!(host->caps & UFS_MTK_CAP_VA09_PWR_CTRL);
73 static void ufs_mtk_cfg_unipro_cg(struct ufs_hba *hba, bool enable)
79 UIC_ARG_MIB(VS_SAVEPOWERCONTROL), &tmp);
81 (1 << RX_SYMBOL_CLK_GATE_EN) |
82 (1 << SYS_CLK_GATE_EN) |
83 (1 << TX_CLK_GATE_EN);
85 UIC_ARG_MIB(VS_SAVEPOWERCONTROL), tmp);
88 UIC_ARG_MIB(VS_DEBUGCLOCKENABLE), &tmp);
89 tmp = tmp & ~(1 << TX_SYMBOL_CLK_REQ_FORCE);
91 UIC_ARG_MIB(VS_DEBUGCLOCKENABLE), tmp);
94 UIC_ARG_MIB(VS_SAVEPOWERCONTROL), &tmp);
95 tmp = tmp & ~((1 << RX_SYMBOL_CLK_GATE_EN) |
96 (1 << SYS_CLK_GATE_EN) |
97 (1 << TX_CLK_GATE_EN));
99 UIC_ARG_MIB(VS_SAVEPOWERCONTROL), tmp);
102 UIC_ARG_MIB(VS_DEBUGCLOCKENABLE), &tmp);
103 tmp = tmp | (1 << TX_SYMBOL_CLK_REQ_FORCE);
105 UIC_ARG_MIB(VS_DEBUGCLOCKENABLE), tmp);
109 static void ufs_mtk_crypto_enable(struct ufs_hba *hba)
111 struct arm_smccc_res res;
113 ufs_mtk_crypto_ctrl(res, 1);
115 dev_info(hba->dev, "%s: crypto enable failed, err: %lu\n",
117 hba->caps &= ~UFSHCD_CAP_CRYPTO;
121 static void ufs_mtk_host_reset(struct ufs_hba *hba)
123 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
125 reset_control_assert(host->hci_reset);
126 reset_control_assert(host->crypto_reset);
127 reset_control_assert(host->unipro_reset);
129 usleep_range(100, 110);
131 reset_control_deassert(host->unipro_reset);
132 reset_control_deassert(host->crypto_reset);
133 reset_control_deassert(host->hci_reset);
136 static void ufs_mtk_init_reset_control(struct ufs_hba *hba,
137 struct reset_control **rc,
140 *rc = devm_reset_control_get(hba->dev, str);
142 dev_info(hba->dev, "Failed to get reset control %s: %ld\n",
148 static void ufs_mtk_init_reset(struct ufs_hba *hba)
150 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
152 ufs_mtk_init_reset_control(hba, &host->hci_reset,
154 ufs_mtk_init_reset_control(hba, &host->unipro_reset,
156 ufs_mtk_init_reset_control(hba, &host->crypto_reset,
160 static int ufs_mtk_hce_enable_notify(struct ufs_hba *hba,
161 enum ufs_notify_change_status status)
163 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
166 if (status == PRE_CHANGE) {
167 if (host->unipro_lpm) {
168 hba->vps->hba_enable_delay_us = 0;
170 hba->vps->hba_enable_delay_us = 600;
171 ufs_mtk_host_reset(hba);
174 if (hba->caps & UFSHCD_CAP_CRYPTO)
175 ufs_mtk_crypto_enable(hba);
177 if (host->caps & UFS_MTK_CAP_DISABLE_AH8) {
178 spin_lock_irqsave(hba->host->host_lock, flags);
179 ufshcd_writel(hba, 0,
180 REG_AUTO_HIBERNATE_IDLE_TIMER);
181 spin_unlock_irqrestore(hba->host->host_lock,
184 hba->capabilities &= ~MASK_AUTO_HIBERN8_SUPPORT;
192 static int ufs_mtk_bind_mphy(struct ufs_hba *hba)
194 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
195 struct device *dev = hba->dev;
196 struct device_node *np = dev->of_node;
199 host->mphy = devm_of_phy_get_by_index(dev, np, 0);
201 if (host->mphy == ERR_PTR(-EPROBE_DEFER)) {
203 * UFS driver might be probed before the phy driver does.
204 * In that case we would like to return EPROBE_DEFER code.
208 "%s: required phy hasn't probed yet. err = %d\n",
210 } else if (IS_ERR(host->mphy)) {
211 err = PTR_ERR(host->mphy);
212 if (err != -ENODEV) {
213 dev_info(dev, "%s: PHY get failed %d\n", __func__,
221 * Allow unbound mphy because not every platform needs specific
230 static int ufs_mtk_setup_ref_clk(struct ufs_hba *hba, bool on)
232 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
233 struct arm_smccc_res res;
234 ktime_t timeout, time_checked;
237 if (host->ref_clk_enabled == on)
241 ufs_mtk_ref_clk_notify(on, res);
242 ufshcd_delay_us(host->ref_clk_ungating_wait_us, 10);
243 ufshcd_writel(hba, REFCLK_REQUEST, REG_UFS_REFCLK_CTRL);
245 ufshcd_writel(hba, REFCLK_RELEASE, REG_UFS_REFCLK_CTRL);
249 timeout = ktime_add_us(ktime_get(), REFCLK_REQ_TIMEOUT_US);
251 time_checked = ktime_get();
252 value = ufshcd_readl(hba, REG_UFS_REFCLK_CTRL);
254 /* Wait until ack bit equals to req bit */
255 if (((value & REFCLK_ACK) >> 1) == (value & REFCLK_REQUEST))
258 usleep_range(100, 200);
259 } while (ktime_before(time_checked, timeout));
261 dev_err(hba->dev, "missing ack of refclk req, reg: 0x%x\n", value);
263 ufs_mtk_ref_clk_notify(host->ref_clk_enabled, res);
268 host->ref_clk_enabled = on;
270 ufshcd_delay_us(host->ref_clk_gating_wait_us, 10);
271 ufs_mtk_ref_clk_notify(on, res);
277 static void ufs_mtk_setup_ref_clk_wait_us(struct ufs_hba *hba,
278 u16 gating_us, u16 ungating_us)
280 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
282 if (hba->dev_info.clk_gating_wait_us) {
283 host->ref_clk_gating_wait_us =
284 hba->dev_info.clk_gating_wait_us;
286 host->ref_clk_gating_wait_us = gating_us;
289 host->ref_clk_ungating_wait_us = ungating_us;
292 static int ufs_mtk_wait_link_state(struct ufs_hba *hba, u32 state,
293 unsigned long max_wait_ms)
295 ktime_t timeout, time_checked;
298 timeout = ktime_add_ms(ktime_get(), max_wait_ms);
300 time_checked = ktime_get();
301 ufshcd_writel(hba, 0x20, REG_UFS_DEBUG_SEL);
302 val = ufshcd_readl(hba, REG_UFS_PROBE);
308 /* Sleep for max. 200us */
309 usleep_range(100, 200);
310 } while (ktime_before(time_checked, timeout));
318 static int ufs_mtk_mphy_power_on(struct ufs_hba *hba, bool on)
320 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
321 struct phy *mphy = host->mphy;
322 struct arm_smccc_res res;
325 if (!mphy || !(on ^ host->mphy_powered_on))
329 if (ufs_mtk_is_va09_supported(hba)) {
330 ret = regulator_enable(host->reg_va09);
333 /* wait 200 us to stablize VA09 */
334 usleep_range(200, 210);
335 ufs_mtk_va09_pwr_ctrl(res, 1);
340 if (ufs_mtk_is_va09_supported(hba)) {
341 ufs_mtk_va09_pwr_ctrl(res, 0);
342 ret = regulator_disable(host->reg_va09);
350 "failed to %s va09: %d\n",
351 on ? "enable" : "disable",
354 host->mphy_powered_on = on;
360 static int ufs_mtk_get_host_clk(struct device *dev, const char *name,
361 struct clk **clk_out)
366 clk = devm_clk_get(dev, name);
375 static void ufs_mtk_boost_crypt(struct ufs_hba *hba, bool boost)
377 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
378 struct ufs_mtk_crypt_cfg *cfg;
379 struct regulator *reg;
382 if (!ufs_mtk_is_boost_crypt_enabled(hba))
386 volt = cfg->vcore_volt;
387 reg = cfg->reg_vcore;
389 ret = clk_prepare_enable(cfg->clk_crypt_mux);
391 dev_info(hba->dev, "clk_prepare_enable(): %d\n",
397 ret = regulator_set_voltage(reg, volt, INT_MAX);
400 "failed to set vcore to %d\n", volt);
404 ret = clk_set_parent(cfg->clk_crypt_mux,
405 cfg->clk_crypt_perf);
408 "failed to set clk_crypt_perf\n");
409 regulator_set_voltage(reg, 0, INT_MAX);
413 ret = clk_set_parent(cfg->clk_crypt_mux,
417 "failed to set clk_crypt_lp\n");
421 ret = regulator_set_voltage(reg, 0, INT_MAX);
424 "failed to set vcore to MIN\n");
428 clk_disable_unprepare(cfg->clk_crypt_mux);
431 static int ufs_mtk_init_host_clk(struct ufs_hba *hba, const char *name,
436 ret = ufs_mtk_get_host_clk(hba->dev, name, clk);
438 dev_info(hba->dev, "%s: failed to get %s: %d", __func__,
445 static void ufs_mtk_init_boost_crypt(struct ufs_hba *hba)
447 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
448 struct ufs_mtk_crypt_cfg *cfg;
449 struct device *dev = hba->dev;
450 struct regulator *reg;
453 host->crypt = devm_kzalloc(dev, sizeof(*(host->crypt)),
458 reg = devm_regulator_get_optional(dev, "dvfsrc-vcore");
460 dev_info(dev, "failed to get dvfsrc-vcore: %ld",
465 if (of_property_read_u32(dev->of_node, "boost-crypt-vcore-min",
467 dev_info(dev, "failed to get boost-crypt-vcore-min");
472 if (ufs_mtk_init_host_clk(hba, "crypt_mux",
473 &cfg->clk_crypt_mux))
476 if (ufs_mtk_init_host_clk(hba, "crypt_lp",
480 if (ufs_mtk_init_host_clk(hba, "crypt_perf",
481 &cfg->clk_crypt_perf))
484 cfg->reg_vcore = reg;
485 cfg->vcore_volt = volt;
486 host->caps |= UFS_MTK_CAP_BOOST_CRYPT_ENGINE;
492 static void ufs_mtk_init_va09_pwr_ctrl(struct ufs_hba *hba)
494 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
496 host->reg_va09 = regulator_get(hba->dev, "va09");
498 dev_info(hba->dev, "failed to get va09");
500 host->caps |= UFS_MTK_CAP_VA09_PWR_CTRL;
503 static void ufs_mtk_init_host_caps(struct ufs_hba *hba)
505 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
506 struct device_node *np = hba->dev->of_node;
508 if (of_property_read_bool(np, "mediatek,ufs-boost-crypt"))
509 ufs_mtk_init_boost_crypt(hba);
511 if (of_property_read_bool(np, "mediatek,ufs-support-va09"))
512 ufs_mtk_init_va09_pwr_ctrl(hba);
514 if (of_property_read_bool(np, "mediatek,ufs-disable-ah8"))
515 host->caps |= UFS_MTK_CAP_DISABLE_AH8;
517 dev_info(hba->dev, "caps: 0x%x", host->caps);
520 static void ufs_mtk_scale_perf(struct ufs_hba *hba, bool up)
522 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
524 ufs_mtk_boost_crypt(hba, up);
525 ufs_mtk_setup_ref_clk(hba, up);
528 phy_power_on(host->mphy);
530 phy_power_off(host->mphy);
534 * ufs_mtk_setup_clocks - enables/disable clocks
535 * @hba: host controller instance
536 * @on: If true, enable clocks else disable them.
537 * @status: PRE_CHANGE or POST_CHANGE notify
539 * Returns 0 on success, non-zero on failure.
541 static int ufs_mtk_setup_clocks(struct ufs_hba *hba, bool on,
542 enum ufs_notify_change_status status)
544 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
545 bool clk_pwr_off = false;
549 * In case ufs_mtk_init() is not yet done, simply ignore.
550 * This ufs_mtk_setup_clocks() shall be called from
551 * ufs_mtk_init() after init is done.
556 if (!on && status == PRE_CHANGE) {
557 if (ufshcd_is_link_off(hba)) {
559 } else if (ufshcd_is_link_hibern8(hba) ||
560 (!ufshcd_can_hibern8_during_gating(hba) &&
561 ufshcd_is_auto_hibern8_enabled(hba))) {
563 * Gate ref-clk and poweroff mphy if link state is in
564 * OFF or Hibern8 by either Auto-Hibern8 or
565 * ufshcd_link_state_transition().
567 ret = ufs_mtk_wait_link_state(hba,
575 ufs_mtk_scale_perf(hba, false);
576 } else if (on && status == POST_CHANGE) {
577 ufs_mtk_scale_perf(hba, true);
583 static void ufs_mtk_get_controller_version(struct ufs_hba *hba)
585 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
588 if (host->hw_ver.major)
591 /* Set default (minimum) version anyway */
592 host->hw_ver.major = 2;
594 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_LOCALVERINFO), &ver);
596 if (ver >= UFS_UNIPRO_VER_1_8)
597 host->hw_ver.major = 3;
602 * ufs_mtk_init - find other essential mmio bases
603 * @hba: host controller instance
605 * Binds PHY with controller and powers up PHY enabling clocks
608 * Returns -EPROBE_DEFER if binding fails, returns negative error
609 * on phy power up failure and returns zero on success.
611 static int ufs_mtk_init(struct ufs_hba *hba)
613 const struct of_device_id *id;
614 struct device *dev = hba->dev;
615 struct ufs_mtk_host *host;
618 host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
621 dev_info(dev, "%s: no memory for mtk ufs host\n", __func__);
626 ufshcd_set_variant(hba, host);
628 id = of_match_device(ufs_mtk_of_match, dev);
634 /* Initialize host capability */
635 ufs_mtk_init_host_caps(hba);
637 err = ufs_mtk_bind_mphy(hba);
639 goto out_variant_clear;
641 ufs_mtk_init_reset(hba);
643 /* Enable runtime autosuspend */
644 hba->caps |= UFSHCD_CAP_RPM_AUTOSUSPEND;
646 /* Enable clock-gating */
647 hba->caps |= UFSHCD_CAP_CLK_GATING;
649 /* Enable inline encryption */
650 hba->caps |= UFSHCD_CAP_CRYPTO;
652 /* Enable WriteBooster */
653 hba->caps |= UFSHCD_CAP_WB_EN;
654 hba->vps->wb_flush_threshold = UFS_WB_BUF_REMAIN_PERCENT(80);
656 if (host->caps & UFS_MTK_CAP_DISABLE_AH8)
657 hba->caps |= UFSHCD_CAP_HIBERN8_WITH_CLK_GATING;
660 * ufshcd_vops_init() is invoked after
661 * ufshcd_setup_clock(true) in ufshcd_hba_init() thus
662 * phy clock setup is skipped.
664 * Enable phy clocks specifically here.
666 ufs_mtk_mphy_power_on(hba, true);
667 ufs_mtk_setup_clocks(hba, true, POST_CHANGE);
672 ufshcd_set_variant(hba, NULL);
677 static int ufs_mtk_pre_pwr_change(struct ufs_hba *hba,
678 struct ufs_pa_layer_attr *dev_max_params,
679 struct ufs_pa_layer_attr *dev_req_params)
681 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
682 struct ufs_dev_params host_cap;
685 ufshcd_init_pwr_dev_param(&host_cap);
686 host_cap.hs_rx_gear = UFS_HS_G4;
687 host_cap.hs_tx_gear = UFS_HS_G4;
689 ret = ufshcd_get_pwr_dev_param(&host_cap,
693 pr_info("%s: failed to determine capabilities\n",
697 if (host->hw_ver.major >= 3) {
698 ret = ufshcd_dme_configure_adapt(hba,
699 dev_req_params->gear_tx,
706 static int ufs_mtk_pwr_change_notify(struct ufs_hba *hba,
707 enum ufs_notify_change_status stage,
708 struct ufs_pa_layer_attr *dev_max_params,
709 struct ufs_pa_layer_attr *dev_req_params)
715 ret = ufs_mtk_pre_pwr_change(hba, dev_max_params,
728 static int ufs_mtk_unipro_set_lpm(struct ufs_hba *hba, bool lpm)
731 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
733 ret = ufshcd_dme_set(hba,
734 UIC_ARG_MIB_SEL(VS_UNIPROPOWERDOWNCONTROL, 0),
738 * Forcibly set as non-LPM mode if UIC commands is failed
739 * to use default hba_enable_delay_us value for re-enabling
742 host->unipro_lpm = lpm;
748 static int ufs_mtk_pre_link(struct ufs_hba *hba)
753 ufs_mtk_get_controller_version(hba);
755 ret = ufs_mtk_unipro_set_lpm(hba, false);
760 * Setting PA_Local_TX_LCC_Enable to 0 before link startup
761 * to make sure that both host and device TX LCC are disabled
762 * once link startup is completed.
764 ret = ufshcd_disable_host_tx_lcc(hba);
768 /* disable deep stall */
769 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(VS_SAVEPOWERCONTROL), &tmp);
775 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(VS_SAVEPOWERCONTROL), tmp);
780 static void ufs_mtk_setup_clk_gating(struct ufs_hba *hba)
785 if (ufshcd_is_clkgating_allowed(hba)) {
786 if (ufshcd_is_auto_hibern8_supported(hba) && hba->ahit)
787 ah_ms = FIELD_GET(UFSHCI_AHIBERN8_TIMER_MASK,
791 spin_lock_irqsave(hba->host->host_lock, flags);
792 hba->clk_gating.delay_ms = ah_ms + 5;
793 spin_unlock_irqrestore(hba->host->host_lock, flags);
797 static int ufs_mtk_post_link(struct ufs_hba *hba)
799 /* enable unipro clock gating feature */
800 ufs_mtk_cfg_unipro_cg(hba, true);
802 /* configure auto-hibern8 timer to 10ms */
803 if (ufshcd_is_auto_hibern8_supported(hba)) {
804 ufshcd_auto_hibern8_update(hba,
805 FIELD_PREP(UFSHCI_AHIBERN8_TIMER_MASK, 10) |
806 FIELD_PREP(UFSHCI_AHIBERN8_SCALE_MASK, 3));
809 ufs_mtk_setup_clk_gating(hba);
814 static int ufs_mtk_link_startup_notify(struct ufs_hba *hba,
815 enum ufs_notify_change_status stage)
821 ret = ufs_mtk_pre_link(hba);
824 ret = ufs_mtk_post_link(hba);
834 static int ufs_mtk_device_reset(struct ufs_hba *hba)
836 struct arm_smccc_res res;
838 ufs_mtk_device_reset_ctrl(0, res);
841 * The reset signal is active low. UFS devices shall detect
842 * more than or equal to 1us of positive or negative RST_n
845 * To be on safe side, keep the reset low for at least 10us.
847 usleep_range(10, 15);
849 ufs_mtk_device_reset_ctrl(1, res);
851 /* Some devices may need time to respond to rst_n */
852 usleep_range(10000, 15000);
854 dev_info(hba->dev, "device reset done\n");
859 static int ufs_mtk_link_set_hpm(struct ufs_hba *hba)
863 err = ufshcd_hba_enable(hba);
867 err = ufs_mtk_unipro_set_lpm(hba, false);
871 err = ufshcd_uic_hibern8_exit(hba);
873 ufshcd_set_link_active(hba);
877 err = ufshcd_make_hba_operational(hba);
884 static int ufs_mtk_link_set_lpm(struct ufs_hba *hba)
888 err = ufs_mtk_unipro_set_lpm(hba, true);
890 /* Resume UniPro state for following error recovery */
891 ufs_mtk_unipro_set_lpm(hba, false);
898 static void ufs_mtk_vreg_set_lpm(struct ufs_hba *hba, bool lpm)
900 if (!hba->vreg_info.vccq2 || !hba->vreg_info.vcc)
903 if (lpm & !hba->vreg_info.vcc->enabled)
904 regulator_set_mode(hba->vreg_info.vccq2->reg,
905 REGULATOR_MODE_IDLE);
907 regulator_set_mode(hba->vreg_info.vccq2->reg,
908 REGULATOR_MODE_NORMAL);
911 static int ufs_mtk_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
915 if (ufshcd_is_link_hibern8(hba)) {
916 err = ufs_mtk_link_set_lpm(hba);
921 if (!ufshcd_is_link_active(hba)) {
923 * Make sure no error will be returned to prevent
924 * ufshcd_suspend() re-enabling regulators while vreg is still
927 ufs_mtk_vreg_set_lpm(hba, true);
928 err = ufs_mtk_mphy_power_on(hba, false);
936 * Set link as off state enforcedly to trigger
937 * ufshcd_host_reset_and_restore() in ufshcd_suspend()
938 * for completed host reset.
940 ufshcd_set_link_off(hba);
944 static int ufs_mtk_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
948 err = ufs_mtk_mphy_power_on(hba, true);
952 ufs_mtk_vreg_set_lpm(hba, false);
954 if (ufshcd_is_link_hibern8(hba)) {
955 err = ufs_mtk_link_set_hpm(hba);
962 return ufshcd_link_recovery(hba);
965 static void ufs_mtk_dbg_register_dump(struct ufs_hba *hba)
967 ufshcd_dump_regs(hba, REG_UFS_REFCLK_CTRL, 0x4, "Ref-Clk Ctrl ");
969 ufshcd_dump_regs(hba, REG_UFS_EXTREG, 0x4, "Ext Reg ");
971 ufshcd_dump_regs(hba, REG_UFS_MPHYCTRL,
972 REG_UFS_REJECT_MON - REG_UFS_MPHYCTRL + 4,
975 /* Direct debugging information to REG_MTK_PROBE */
976 ufshcd_writel(hba, 0x20, REG_UFS_DEBUG_SEL);
977 ufshcd_dump_regs(hba, REG_UFS_PROBE, 0x4, "Debug Probe ");
980 static int ufs_mtk_apply_dev_quirks(struct ufs_hba *hba)
982 struct ufs_dev_info *dev_info = &hba->dev_info;
983 u16 mid = dev_info->wmanufacturerid;
985 if (mid == UFS_VENDOR_SAMSUNG)
986 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 6);
989 * Decide waiting time before gating reference clock and
990 * after ungating reference clock according to vendors'
993 if (mid == UFS_VENDOR_SAMSUNG)
994 ufs_mtk_setup_ref_clk_wait_us(hba, 1, 1);
995 else if (mid == UFS_VENDOR_SKHYNIX)
996 ufs_mtk_setup_ref_clk_wait_us(hba, 30, 30);
997 else if (mid == UFS_VENDOR_TOSHIBA)
998 ufs_mtk_setup_ref_clk_wait_us(hba, 100, 32);
1003 static void ufs_mtk_fixup_dev_quirks(struct ufs_hba *hba)
1005 ufshcd_fixup_dev_quirks(hba, ufs_mtk_dev_fixups);
1008 static void ufs_mtk_event_notify(struct ufs_hba *hba,
1009 enum ufs_event_type evt, void *data)
1011 unsigned int val = *(u32 *)data;
1013 trace_ufs_mtk_event(evt, val);
1017 * struct ufs_hba_mtk_vops - UFS MTK specific variant operations
1019 * The variant operations configure the necessary controller and PHY
1020 * handshake during initialization.
1022 static const struct ufs_hba_variant_ops ufs_hba_mtk_vops = {
1023 .name = "mediatek.ufshci",
1024 .init = ufs_mtk_init,
1025 .setup_clocks = ufs_mtk_setup_clocks,
1026 .hce_enable_notify = ufs_mtk_hce_enable_notify,
1027 .link_startup_notify = ufs_mtk_link_startup_notify,
1028 .pwr_change_notify = ufs_mtk_pwr_change_notify,
1029 .apply_dev_quirks = ufs_mtk_apply_dev_quirks,
1030 .fixup_dev_quirks = ufs_mtk_fixup_dev_quirks,
1031 .suspend = ufs_mtk_suspend,
1032 .resume = ufs_mtk_resume,
1033 .dbg_register_dump = ufs_mtk_dbg_register_dump,
1034 .device_reset = ufs_mtk_device_reset,
1035 .event_notify = ufs_mtk_event_notify,
1039 * ufs_mtk_probe - probe routine of the driver
1040 * @pdev: pointer to Platform device handle
1042 * Return zero for success and non-zero for failure
1044 static int ufs_mtk_probe(struct platform_device *pdev)
1047 struct device *dev = &pdev->dev;
1049 /* perform generic probe */
1050 err = ufshcd_pltfrm_init(pdev, &ufs_hba_mtk_vops);
1052 dev_info(dev, "probe failed %d\n", err);
1058 * ufs_mtk_remove - set driver_data of the device to NULL
1059 * @pdev: pointer to platform device handle
1063 static int ufs_mtk_remove(struct platform_device *pdev)
1065 struct ufs_hba *hba = platform_get_drvdata(pdev);
1067 pm_runtime_get_sync(&(pdev)->dev);
1072 static const struct dev_pm_ops ufs_mtk_pm_ops = {
1073 .suspend = ufshcd_pltfrm_suspend,
1074 .resume = ufshcd_pltfrm_resume,
1075 .runtime_suspend = ufshcd_pltfrm_runtime_suspend,
1076 .runtime_resume = ufshcd_pltfrm_runtime_resume,
1077 .runtime_idle = ufshcd_pltfrm_runtime_idle,
1080 static struct platform_driver ufs_mtk_pltform = {
1081 .probe = ufs_mtk_probe,
1082 .remove = ufs_mtk_remove,
1083 .shutdown = ufshcd_pltfrm_shutdown,
1085 .name = "ufshcd-mtk",
1086 .pm = &ufs_mtk_pm_ops,
1087 .of_match_table = ufs_mtk_of_match,
1091 MODULE_AUTHOR("Stanley Chu <stanley.chu@mediatek.com>");
1092 MODULE_AUTHOR("Peter Wang <peter.wang@mediatek.com>");
1093 MODULE_DESCRIPTION("MediaTek UFS Host Driver");
1094 MODULE_LICENSE("GPL v2");
1096 module_platform_driver(ufs_mtk_pltform);