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>
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/phy/phy.h>
18 #include <linux/platform_device.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/reset.h>
21 #include <linux/sched/clock.h>
22 #include <linux/soc/mediatek/mtk_sip_svc.h>
24 #include <ufs/ufshcd.h>
25 #include "ufshcd-pltfrm.h"
26 #include <ufs/ufs_quirks.h>
27 #include <ufs/unipro.h>
28 #include "ufs-mediatek.h"
30 #define CREATE_TRACE_POINTS
31 #include "ufs-mediatek-trace.h"
33 static const struct ufs_dev_quirk ufs_mtk_dev_fixups[] = {
34 { .wmanufacturerid = UFS_ANY_VENDOR,
35 .model = UFS_ANY_MODEL,
36 .quirk = UFS_DEVICE_QUIRK_DELAY_AFTER_LPM |
37 UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM },
38 { .wmanufacturerid = UFS_VENDOR_SKHYNIX,
39 .model = "H9HQ21AFAMZDAR",
40 .quirk = UFS_DEVICE_QUIRK_SUPPORT_EXTENDED_FEATURES },
44 static const struct of_device_id ufs_mtk_of_match[] = {
45 { .compatible = "mediatek,mt8183-ufshci" },
49 static bool ufs_mtk_is_boost_crypt_enabled(struct ufs_hba *hba)
51 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
53 return !!(host->caps & UFS_MTK_CAP_BOOST_CRYPT_ENGINE);
56 static bool ufs_mtk_is_va09_supported(struct ufs_hba *hba)
58 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
60 return !!(host->caps & UFS_MTK_CAP_VA09_PWR_CTRL);
63 static bool ufs_mtk_is_broken_vcc(struct ufs_hba *hba)
65 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
67 return !!(host->caps & UFS_MTK_CAP_BROKEN_VCC);
70 static bool ufs_mtk_is_pmc_via_fastauto(struct ufs_hba *hba)
72 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
74 return (host->caps & UFS_MTK_CAP_PMC_VIA_FASTAUTO);
77 static void ufs_mtk_cfg_unipro_cg(struct ufs_hba *hba, bool enable)
83 UIC_ARG_MIB(VS_SAVEPOWERCONTROL), &tmp);
85 (1 << RX_SYMBOL_CLK_GATE_EN) |
86 (1 << SYS_CLK_GATE_EN) |
87 (1 << TX_CLK_GATE_EN);
89 UIC_ARG_MIB(VS_SAVEPOWERCONTROL), tmp);
92 UIC_ARG_MIB(VS_DEBUGCLOCKENABLE), &tmp);
93 tmp = tmp & ~(1 << TX_SYMBOL_CLK_REQ_FORCE);
95 UIC_ARG_MIB(VS_DEBUGCLOCKENABLE), tmp);
98 UIC_ARG_MIB(VS_SAVEPOWERCONTROL), &tmp);
99 tmp = tmp & ~((1 << RX_SYMBOL_CLK_GATE_EN) |
100 (1 << SYS_CLK_GATE_EN) |
101 (1 << TX_CLK_GATE_EN));
103 UIC_ARG_MIB(VS_SAVEPOWERCONTROL), tmp);
106 UIC_ARG_MIB(VS_DEBUGCLOCKENABLE), &tmp);
107 tmp = tmp | (1 << TX_SYMBOL_CLK_REQ_FORCE);
109 UIC_ARG_MIB(VS_DEBUGCLOCKENABLE), tmp);
113 static void ufs_mtk_crypto_enable(struct ufs_hba *hba)
115 struct arm_smccc_res res;
117 ufs_mtk_crypto_ctrl(res, 1);
119 dev_info(hba->dev, "%s: crypto enable failed, err: %lu\n",
121 hba->caps &= ~UFSHCD_CAP_CRYPTO;
125 static void ufs_mtk_host_reset(struct ufs_hba *hba)
127 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
129 reset_control_assert(host->hci_reset);
130 reset_control_assert(host->crypto_reset);
131 reset_control_assert(host->unipro_reset);
133 usleep_range(100, 110);
135 reset_control_deassert(host->unipro_reset);
136 reset_control_deassert(host->crypto_reset);
137 reset_control_deassert(host->hci_reset);
140 static void ufs_mtk_init_reset_control(struct ufs_hba *hba,
141 struct reset_control **rc,
144 *rc = devm_reset_control_get(hba->dev, str);
146 dev_info(hba->dev, "Failed to get reset control %s: %ld\n",
152 static void ufs_mtk_init_reset(struct ufs_hba *hba)
154 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
156 ufs_mtk_init_reset_control(hba, &host->hci_reset,
158 ufs_mtk_init_reset_control(hba, &host->unipro_reset,
160 ufs_mtk_init_reset_control(hba, &host->crypto_reset,
164 static int ufs_mtk_hce_enable_notify(struct ufs_hba *hba,
165 enum ufs_notify_change_status status)
167 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
169 if (status == PRE_CHANGE) {
170 if (host->unipro_lpm) {
171 hba->vps->hba_enable_delay_us = 0;
173 hba->vps->hba_enable_delay_us = 600;
174 ufs_mtk_host_reset(hba);
177 if (hba->caps & UFSHCD_CAP_CRYPTO)
178 ufs_mtk_crypto_enable(hba);
180 if (host->caps & UFS_MTK_CAP_DISABLE_AH8) {
181 ufshcd_writel(hba, 0,
182 REG_AUTO_HIBERNATE_IDLE_TIMER);
183 hba->capabilities &= ~MASK_AUTO_HIBERN8_SUPPORT;
191 static int ufs_mtk_bind_mphy(struct ufs_hba *hba)
193 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
194 struct device *dev = hba->dev;
195 struct device_node *np = dev->of_node;
198 host->mphy = devm_of_phy_get_by_index(dev, np, 0);
200 if (host->mphy == ERR_PTR(-EPROBE_DEFER)) {
202 * UFS driver might be probed before the phy driver does.
203 * In that case we would like to return EPROBE_DEFER code.
207 "%s: required phy hasn't probed yet. err = %d\n",
209 } else if (IS_ERR(host->mphy)) {
210 err = PTR_ERR(host->mphy);
211 if (err != -ENODEV) {
212 dev_info(dev, "%s: PHY get failed %d\n", __func__,
220 * Allow unbound mphy because not every platform needs specific
229 static int ufs_mtk_setup_ref_clk(struct ufs_hba *hba, bool on)
231 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
232 struct arm_smccc_res res;
233 ktime_t timeout, time_checked;
236 if (host->ref_clk_enabled == on)
240 ufs_mtk_ref_clk_notify(on, res);
241 ufshcd_writel(hba, REFCLK_REQUEST, REG_UFS_REFCLK_CTRL);
243 ufshcd_delay_us(host->ref_clk_gating_wait_us, 10);
244 ufshcd_writel(hba, REFCLK_RELEASE, REG_UFS_REFCLK_CTRL);
248 timeout = ktime_add_us(ktime_get(), REFCLK_REQ_TIMEOUT_US);
250 time_checked = ktime_get();
251 value = ufshcd_readl(hba, REG_UFS_REFCLK_CTRL);
253 /* Wait until ack bit equals to req bit */
254 if (((value & REFCLK_ACK) >> 1) == (value & REFCLK_REQUEST))
257 usleep_range(100, 200);
258 } while (ktime_before(time_checked, timeout));
260 dev_err(hba->dev, "missing ack of refclk req, reg: 0x%x\n", value);
262 ufs_mtk_ref_clk_notify(host->ref_clk_enabled, res);
267 host->ref_clk_enabled = on;
269 ufshcd_delay_us(host->ref_clk_ungating_wait_us, 10);
271 ufs_mtk_ref_clk_notify(on, res);
276 static void ufs_mtk_setup_ref_clk_wait_us(struct ufs_hba *hba,
279 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
281 if (hba->dev_info.clk_gating_wait_us) {
282 host->ref_clk_gating_wait_us =
283 hba->dev_info.clk_gating_wait_us;
285 host->ref_clk_gating_wait_us = gating_us;
288 host->ref_clk_ungating_wait_us = REFCLK_DEFAULT_WAIT_US;
291 static void ufs_mtk_dbg_sel(struct ufs_hba *hba)
293 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
295 if (((host->ip_ver >> 16) & 0xFF) >= 0x36) {
296 ufshcd_writel(hba, 0x820820, REG_UFS_DEBUG_SEL);
297 ufshcd_writel(hba, 0x0, REG_UFS_DEBUG_SEL_B0);
298 ufshcd_writel(hba, 0x55555555, REG_UFS_DEBUG_SEL_B1);
299 ufshcd_writel(hba, 0xaaaaaaaa, REG_UFS_DEBUG_SEL_B2);
300 ufshcd_writel(hba, 0xffffffff, REG_UFS_DEBUG_SEL_B3);
302 ufshcd_writel(hba, 0x20, REG_UFS_DEBUG_SEL);
306 static void ufs_mtk_wait_idle_state(struct ufs_hba *hba,
307 unsigned long retry_ms)
309 u64 timeout, time_checked;
313 /* cannot use plain ktime_get() in suspend */
314 timeout = ktime_get_mono_fast_ns() + retry_ms * 1000000UL;
316 /* wait a specific time after check base */
321 time_checked = ktime_get_mono_fast_ns();
322 ufs_mtk_dbg_sel(hba);
323 val = ufshcd_readl(hba, REG_UFS_PROBE);
328 * if state is in H8 enter and H8 enter confirm
329 * wait until return to idle state.
331 if ((sm >= VS_HIB_ENTER) && (sm <= VS_HIB_EXIT)) {
335 } else if (!wait_idle)
338 if (wait_idle && (sm == VS_HCE_BASE))
340 } while (time_checked < timeout);
342 if (wait_idle && sm != VS_HCE_BASE)
343 dev_info(hba->dev, "wait idle tmo: 0x%x\n", val);
346 static int ufs_mtk_wait_link_state(struct ufs_hba *hba, u32 state,
347 unsigned long max_wait_ms)
349 ktime_t timeout, time_checked;
352 timeout = ktime_add_ms(ktime_get(), max_wait_ms);
354 time_checked = ktime_get();
355 ufs_mtk_dbg_sel(hba);
356 val = ufshcd_readl(hba, REG_UFS_PROBE);
362 /* Sleep for max. 200us */
363 usleep_range(100, 200);
364 } while (ktime_before(time_checked, timeout));
372 static int ufs_mtk_mphy_power_on(struct ufs_hba *hba, bool on)
374 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
375 struct phy *mphy = host->mphy;
376 struct arm_smccc_res res;
379 if (!mphy || !(on ^ host->mphy_powered_on))
383 if (ufs_mtk_is_va09_supported(hba)) {
384 ret = regulator_enable(host->reg_va09);
387 /* wait 200 us to stablize VA09 */
388 usleep_range(200, 210);
389 ufs_mtk_va09_pwr_ctrl(res, 1);
394 if (ufs_mtk_is_va09_supported(hba)) {
395 ufs_mtk_va09_pwr_ctrl(res, 0);
396 ret = regulator_disable(host->reg_va09);
404 "failed to %s va09: %d\n",
405 on ? "enable" : "disable",
408 host->mphy_powered_on = on;
414 static int ufs_mtk_get_host_clk(struct device *dev, const char *name,
415 struct clk **clk_out)
420 clk = devm_clk_get(dev, name);
429 static void ufs_mtk_boost_crypt(struct ufs_hba *hba, bool boost)
431 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
432 struct ufs_mtk_crypt_cfg *cfg;
433 struct regulator *reg;
436 if (!ufs_mtk_is_boost_crypt_enabled(hba))
440 volt = cfg->vcore_volt;
441 reg = cfg->reg_vcore;
443 ret = clk_prepare_enable(cfg->clk_crypt_mux);
445 dev_info(hba->dev, "clk_prepare_enable(): %d\n",
451 ret = regulator_set_voltage(reg, volt, INT_MAX);
454 "failed to set vcore to %d\n", volt);
458 ret = clk_set_parent(cfg->clk_crypt_mux,
459 cfg->clk_crypt_perf);
462 "failed to set clk_crypt_perf\n");
463 regulator_set_voltage(reg, 0, INT_MAX);
467 ret = clk_set_parent(cfg->clk_crypt_mux,
471 "failed to set clk_crypt_lp\n");
475 ret = regulator_set_voltage(reg, 0, INT_MAX);
478 "failed to set vcore to MIN\n");
482 clk_disable_unprepare(cfg->clk_crypt_mux);
485 static int ufs_mtk_init_host_clk(struct ufs_hba *hba, const char *name,
490 ret = ufs_mtk_get_host_clk(hba->dev, name, clk);
492 dev_info(hba->dev, "%s: failed to get %s: %d", __func__,
499 static void ufs_mtk_init_boost_crypt(struct ufs_hba *hba)
501 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
502 struct ufs_mtk_crypt_cfg *cfg;
503 struct device *dev = hba->dev;
504 struct regulator *reg;
507 host->crypt = devm_kzalloc(dev, sizeof(*(host->crypt)),
512 reg = devm_regulator_get_optional(dev, "dvfsrc-vcore");
514 dev_info(dev, "failed to get dvfsrc-vcore: %ld",
519 if (of_property_read_u32(dev->of_node, "boost-crypt-vcore-min",
521 dev_info(dev, "failed to get boost-crypt-vcore-min");
526 if (ufs_mtk_init_host_clk(hba, "crypt_mux",
527 &cfg->clk_crypt_mux))
530 if (ufs_mtk_init_host_clk(hba, "crypt_lp",
534 if (ufs_mtk_init_host_clk(hba, "crypt_perf",
535 &cfg->clk_crypt_perf))
538 cfg->reg_vcore = reg;
539 cfg->vcore_volt = volt;
540 host->caps |= UFS_MTK_CAP_BOOST_CRYPT_ENGINE;
546 static void ufs_mtk_init_va09_pwr_ctrl(struct ufs_hba *hba)
548 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
550 host->reg_va09 = regulator_get(hba->dev, "va09");
551 if (IS_ERR(host->reg_va09))
552 dev_info(hba->dev, "failed to get va09");
554 host->caps |= UFS_MTK_CAP_VA09_PWR_CTRL;
557 static void ufs_mtk_init_host_caps(struct ufs_hba *hba)
559 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
560 struct device_node *np = hba->dev->of_node;
562 if (of_property_read_bool(np, "mediatek,ufs-boost-crypt"))
563 ufs_mtk_init_boost_crypt(hba);
565 if (of_property_read_bool(np, "mediatek,ufs-support-va09"))
566 ufs_mtk_init_va09_pwr_ctrl(hba);
568 if (of_property_read_bool(np, "mediatek,ufs-disable-ah8"))
569 host->caps |= UFS_MTK_CAP_DISABLE_AH8;
571 if (of_property_read_bool(np, "mediatek,ufs-broken-vcc"))
572 host->caps |= UFS_MTK_CAP_BROKEN_VCC;
574 if (of_property_read_bool(np, "mediatek,ufs-pmc-via-fastauto"))
575 host->caps |= UFS_MTK_CAP_PMC_VIA_FASTAUTO;
577 dev_info(hba->dev, "caps: 0x%x", host->caps);
580 static void ufs_mtk_scale_perf(struct ufs_hba *hba, bool up)
582 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
584 ufs_mtk_boost_crypt(hba, up);
585 ufs_mtk_setup_ref_clk(hba, up);
588 phy_power_on(host->mphy);
590 phy_power_off(host->mphy);
594 * ufs_mtk_setup_clocks - enables/disable clocks
595 * @hba: host controller instance
596 * @on: If true, enable clocks else disable them.
597 * @status: PRE_CHANGE or POST_CHANGE notify
599 * Returns 0 on success, non-zero on failure.
601 static int ufs_mtk_setup_clocks(struct ufs_hba *hba, bool on,
602 enum ufs_notify_change_status status)
604 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
605 bool clk_pwr_off = false;
609 * In case ufs_mtk_init() is not yet done, simply ignore.
610 * This ufs_mtk_setup_clocks() shall be called from
611 * ufs_mtk_init() after init is done.
616 if (!on && status == PRE_CHANGE) {
617 if (ufshcd_is_link_off(hba)) {
619 } else if (ufshcd_is_link_hibern8(hba) ||
620 (!ufshcd_can_hibern8_during_gating(hba) &&
621 ufshcd_is_auto_hibern8_enabled(hba))) {
623 * Gate ref-clk and poweroff mphy if link state is in
624 * OFF or Hibern8 by either Auto-Hibern8 or
625 * ufshcd_link_state_transition().
627 ret = ufs_mtk_wait_link_state(hba,
635 ufs_mtk_scale_perf(hba, false);
636 } else if (on && status == POST_CHANGE) {
637 ufs_mtk_scale_perf(hba, true);
643 static void ufs_mtk_get_controller_version(struct ufs_hba *hba)
645 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
648 if (host->hw_ver.major)
651 /* Set default (minimum) version anyway */
652 host->hw_ver.major = 2;
654 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_LOCALVERINFO), &ver);
656 if (ver >= UFS_UNIPRO_VER_1_8) {
657 host->hw_ver.major = 3;
659 * Fix HCI version for some platforms with
662 if (hba->ufs_version < ufshci_version(3, 0))
663 hba->ufs_version = ufshci_version(3, 0);
668 static u32 ufs_mtk_get_ufs_hci_version(struct ufs_hba *hba)
670 return hba->ufs_version;
673 #define MAX_VCC_NAME 30
674 static int ufs_mtk_vreg_fix_vcc(struct ufs_hba *hba)
676 struct ufs_vreg_info *info = &hba->vreg_info;
677 struct device_node *np = hba->dev->of_node;
678 struct device *dev = hba->dev;
679 char vcc_name[MAX_VCC_NAME];
680 struct arm_smccc_res res;
683 if (hba->vreg_info.vcc)
686 if (of_property_read_bool(np, "mediatek,ufs-vcc-by-num")) {
687 ufs_mtk_get_vcc_num(res);
688 if (res.a1 > UFS_VCC_NONE && res.a1 < UFS_VCC_MAX)
689 snprintf(vcc_name, MAX_VCC_NAME, "vcc-opt%lu", res.a1);
692 } else if (of_property_read_bool(np, "mediatek,ufs-vcc-by-ver")) {
693 ver = (hba->dev_info.wspecversion & 0xF00) >> 8;
694 snprintf(vcc_name, MAX_VCC_NAME, "vcc-ufs%u", ver);
699 err = ufshcd_populate_vreg(dev, vcc_name, &info->vcc);
703 err = ufshcd_get_vreg(dev, info->vcc);
707 err = regulator_enable(info->vcc->reg);
709 info->vcc->enabled = true;
710 dev_info(dev, "%s: %s enabled\n", __func__, vcc_name);
716 static void ufs_mtk_vreg_fix_vccqx(struct ufs_hba *hba)
718 struct ufs_vreg_info *info = &hba->vreg_info;
719 struct ufs_vreg **vreg_on, **vreg_off;
721 if (hba->dev_info.wspecversion >= 0x0300) {
722 vreg_on = &info->vccq;
723 vreg_off = &info->vccq2;
725 vreg_on = &info->vccq2;
726 vreg_off = &info->vccq;
730 (*vreg_on)->always_on = true;
733 regulator_disable((*vreg_off)->reg);
734 devm_kfree(hba->dev, (*vreg_off)->name);
735 devm_kfree(hba->dev, *vreg_off);
740 * ufs_mtk_init - find other essential mmio bases
741 * @hba: host controller instance
743 * Binds PHY with controller and powers up PHY enabling clocks
746 * Returns -EPROBE_DEFER if binding fails, returns negative error
747 * on phy power up failure and returns zero on success.
749 static int ufs_mtk_init(struct ufs_hba *hba)
751 const struct of_device_id *id;
752 struct device *dev = hba->dev;
753 struct ufs_mtk_host *host;
756 host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
759 dev_info(dev, "%s: no memory for mtk ufs host\n", __func__);
764 ufshcd_set_variant(hba, host);
766 id = of_match_device(ufs_mtk_of_match, dev);
772 /* Initialize host capability */
773 ufs_mtk_init_host_caps(hba);
775 err = ufs_mtk_bind_mphy(hba);
777 goto out_variant_clear;
779 ufs_mtk_init_reset(hba);
781 /* Enable runtime autosuspend */
782 hba->caps |= UFSHCD_CAP_RPM_AUTOSUSPEND;
784 /* Enable clock-gating */
785 hba->caps |= UFSHCD_CAP_CLK_GATING;
787 /* Enable inline encryption */
788 hba->caps |= UFSHCD_CAP_CRYPTO;
790 /* Enable WriteBooster */
791 hba->caps |= UFSHCD_CAP_WB_EN;
792 hba->quirks |= UFSHCI_QUIRK_SKIP_MANUAL_WB_FLUSH_CTRL;
793 hba->vps->wb_flush_threshold = UFS_WB_BUF_REMAIN_PERCENT(80);
795 if (host->caps & UFS_MTK_CAP_DISABLE_AH8)
796 hba->caps |= UFSHCD_CAP_HIBERN8_WITH_CLK_GATING;
799 * ufshcd_vops_init() is invoked after
800 * ufshcd_setup_clock(true) in ufshcd_hba_init() thus
801 * phy clock setup is skipped.
803 * Enable phy clocks specifically here.
805 ufs_mtk_mphy_power_on(hba, true);
806 ufs_mtk_setup_clocks(hba, true, POST_CHANGE);
808 host->ip_ver = ufshcd_readl(hba, REG_UFS_MTK_IP_VER);
813 ufshcd_set_variant(hba, NULL);
818 static bool ufs_mtk_pmc_via_fastauto(struct ufs_hba *hba,
819 struct ufs_pa_layer_attr *dev_req_params)
821 if (!ufs_mtk_is_pmc_via_fastauto(hba))
824 if (dev_req_params->hs_rate == hba->pwr_info.hs_rate)
827 if (dev_req_params->pwr_tx != FAST_MODE &&
828 dev_req_params->gear_tx < UFS_HS_G4)
831 if (dev_req_params->pwr_rx != FAST_MODE &&
832 dev_req_params->gear_rx < UFS_HS_G4)
838 static int ufs_mtk_pre_pwr_change(struct ufs_hba *hba,
839 struct ufs_pa_layer_attr *dev_max_params,
840 struct ufs_pa_layer_attr *dev_req_params)
842 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
843 struct ufs_dev_params host_cap;
846 ufshcd_init_pwr_dev_param(&host_cap);
847 host_cap.hs_rx_gear = UFS_HS_G5;
848 host_cap.hs_tx_gear = UFS_HS_G5;
850 ret = ufshcd_get_pwr_dev_param(&host_cap,
854 pr_info("%s: failed to determine capabilities\n",
858 if (ufs_mtk_pmc_via_fastauto(hba, dev_req_params)) {
859 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), true);
860 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), UFS_HS_G1);
862 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), true);
863 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), UFS_HS_G1);
865 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
866 dev_req_params->lane_tx);
867 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
868 dev_req_params->lane_rx);
869 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
870 dev_req_params->hs_rate);
872 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXHSADAPTTYPE),
875 ret = ufshcd_uic_change_pwr_mode(hba,
876 FASTAUTO_MODE << 4 | FASTAUTO_MODE);
879 dev_err(hba->dev, "%s: HSG1B FASTAUTO failed ret=%d\n",
884 if (host->hw_ver.major >= 3) {
885 ret = ufshcd_dme_configure_adapt(hba,
886 dev_req_params->gear_tx,
893 static int ufs_mtk_pwr_change_notify(struct ufs_hba *hba,
894 enum ufs_notify_change_status stage,
895 struct ufs_pa_layer_attr *dev_max_params,
896 struct ufs_pa_layer_attr *dev_req_params)
902 ret = ufs_mtk_pre_pwr_change(hba, dev_max_params,
915 static int ufs_mtk_unipro_set_lpm(struct ufs_hba *hba, bool lpm)
918 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
920 ret = ufshcd_dme_set(hba,
921 UIC_ARG_MIB_SEL(VS_UNIPROPOWERDOWNCONTROL, 0),
925 * Forcibly set as non-LPM mode if UIC commands is failed
926 * to use default hba_enable_delay_us value for re-enabling
929 host->unipro_lpm = lpm;
935 static int ufs_mtk_pre_link(struct ufs_hba *hba)
940 ufs_mtk_get_controller_version(hba);
942 ret = ufs_mtk_unipro_set_lpm(hba, false);
947 * Setting PA_Local_TX_LCC_Enable to 0 before link startup
948 * to make sure that both host and device TX LCC are disabled
949 * once link startup is completed.
951 ret = ufshcd_disable_host_tx_lcc(hba);
955 /* disable deep stall */
956 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(VS_SAVEPOWERCONTROL), &tmp);
962 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(VS_SAVEPOWERCONTROL), tmp);
967 static void ufs_mtk_setup_clk_gating(struct ufs_hba *hba)
971 if (ufshcd_is_clkgating_allowed(hba)) {
972 if (ufshcd_is_auto_hibern8_supported(hba) && hba->ahit)
973 ah_ms = FIELD_GET(UFSHCI_AHIBERN8_TIMER_MASK,
977 ufshcd_clkgate_delay_set(hba->dev, ah_ms + 5);
981 static int ufs_mtk_post_link(struct ufs_hba *hba)
983 /* enable unipro clock gating feature */
984 ufs_mtk_cfg_unipro_cg(hba, true);
986 /* will be configured during probe hba */
987 if (ufshcd_is_auto_hibern8_supported(hba))
988 hba->ahit = FIELD_PREP(UFSHCI_AHIBERN8_TIMER_MASK, 10) |
989 FIELD_PREP(UFSHCI_AHIBERN8_SCALE_MASK, 3);
991 ufs_mtk_setup_clk_gating(hba);
996 static int ufs_mtk_link_startup_notify(struct ufs_hba *hba,
997 enum ufs_notify_change_status stage)
1003 ret = ufs_mtk_pre_link(hba);
1006 ret = ufs_mtk_post_link(hba);
1016 static int ufs_mtk_device_reset(struct ufs_hba *hba)
1018 struct arm_smccc_res res;
1020 /* disable hba before device reset */
1021 ufshcd_hba_stop(hba);
1023 ufs_mtk_device_reset_ctrl(0, res);
1026 * The reset signal is active low. UFS devices shall detect
1027 * more than or equal to 1us of positive or negative RST_n
1030 * To be on safe side, keep the reset low for at least 10us.
1032 usleep_range(10, 15);
1034 ufs_mtk_device_reset_ctrl(1, res);
1036 /* Some devices may need time to respond to rst_n */
1037 usleep_range(10000, 15000);
1039 dev_info(hba->dev, "device reset done\n");
1044 static int ufs_mtk_link_set_hpm(struct ufs_hba *hba)
1048 err = ufshcd_hba_enable(hba);
1052 err = ufs_mtk_unipro_set_lpm(hba, false);
1056 err = ufshcd_uic_hibern8_exit(hba);
1058 ufshcd_set_link_active(hba);
1062 err = ufshcd_make_hba_operational(hba);
1069 static int ufs_mtk_link_set_lpm(struct ufs_hba *hba)
1073 err = ufs_mtk_unipro_set_lpm(hba, true);
1075 /* Resume UniPro state for following error recovery */
1076 ufs_mtk_unipro_set_lpm(hba, false);
1083 static void ufs_mtk_vccqx_set_lpm(struct ufs_hba *hba, bool lpm)
1085 struct ufs_vreg *vccqx = NULL;
1087 if (hba->vreg_info.vccq)
1088 vccqx = hba->vreg_info.vccq;
1090 vccqx = hba->vreg_info.vccq2;
1092 regulator_set_mode(vccqx->reg,
1093 lpm ? REGULATOR_MODE_IDLE : REGULATOR_MODE_NORMAL);
1096 static void ufs_mtk_vsx_set_lpm(struct ufs_hba *hba, bool lpm)
1098 struct arm_smccc_res res;
1100 ufs_mtk_device_pwr_ctrl(!lpm,
1101 (unsigned long)hba->dev_info.wspecversion,
1105 static void ufs_mtk_dev_vreg_set_lpm(struct ufs_hba *hba, bool lpm)
1107 if (!hba->vreg_info.vccq && !hba->vreg_info.vccq2)
1110 /* Skip if VCC is assumed always-on */
1111 if (!hba->vreg_info.vcc)
1114 /* Bypass LPM when device is still active */
1115 if (lpm && ufshcd_is_ufs_dev_active(hba))
1118 /* Bypass LPM if VCC is enabled */
1119 if (lpm && hba->vreg_info.vcc->enabled)
1123 ufs_mtk_vccqx_set_lpm(hba, lpm);
1124 ufs_mtk_vsx_set_lpm(hba, lpm);
1126 ufs_mtk_vsx_set_lpm(hba, lpm);
1127 ufs_mtk_vccqx_set_lpm(hba, lpm);
1131 static void ufs_mtk_auto_hibern8_disable(struct ufs_hba *hba)
1135 /* disable auto-hibern8 */
1136 ufshcd_writel(hba, 0, REG_AUTO_HIBERNATE_IDLE_TIMER);
1138 /* wait host return to idle state when auto-hibern8 off */
1139 ufs_mtk_wait_idle_state(hba, 5);
1141 ret = ufs_mtk_wait_link_state(hba, VS_LINK_UP, 100);
1143 dev_warn(hba->dev, "exit h8 state fail, ret=%d\n", ret);
1146 static int ufs_mtk_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op,
1147 enum ufs_notify_change_status status)
1150 struct arm_smccc_res res;
1152 if (status == PRE_CHANGE) {
1153 if (!ufshcd_is_auto_hibern8_supported(hba))
1155 ufs_mtk_auto_hibern8_disable(hba);
1159 if (ufshcd_is_link_hibern8(hba)) {
1160 err = ufs_mtk_link_set_lpm(hba);
1165 if (!ufshcd_is_link_active(hba)) {
1167 * Make sure no error will be returned to prevent
1168 * ufshcd_suspend() re-enabling regulators while vreg is still
1169 * in low-power mode.
1171 err = ufs_mtk_mphy_power_on(hba, false);
1176 if (ufshcd_is_link_off(hba))
1177 ufs_mtk_device_reset_ctrl(0, res);
1182 * Set link as off state enforcedly to trigger
1183 * ufshcd_host_reset_and_restore() in ufshcd_suspend()
1184 * for completed host reset.
1186 ufshcd_set_link_off(hba);
1190 static int ufs_mtk_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
1194 if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL)
1195 ufs_mtk_dev_vreg_set_lpm(hba, false);
1197 err = ufs_mtk_mphy_power_on(hba, true);
1201 if (ufshcd_is_link_hibern8(hba)) {
1202 err = ufs_mtk_link_set_hpm(hba);
1209 return ufshcd_link_recovery(hba);
1212 static void ufs_mtk_dbg_register_dump(struct ufs_hba *hba)
1214 ufshcd_dump_regs(hba, REG_UFS_REFCLK_CTRL, 0x4, "Ref-Clk Ctrl ");
1216 ufshcd_dump_regs(hba, REG_UFS_EXTREG, 0x4, "Ext Reg ");
1218 ufshcd_dump_regs(hba, REG_UFS_MPHYCTRL,
1219 REG_UFS_REJECT_MON - REG_UFS_MPHYCTRL + 4,
1222 /* Direct debugging information to REG_MTK_PROBE */
1223 ufs_mtk_dbg_sel(hba);
1224 ufshcd_dump_regs(hba, REG_UFS_PROBE, 0x4, "Debug Probe ");
1227 static int ufs_mtk_apply_dev_quirks(struct ufs_hba *hba)
1229 struct ufs_dev_info *dev_info = &hba->dev_info;
1230 u16 mid = dev_info->wmanufacturerid;
1232 if (mid == UFS_VENDOR_SAMSUNG)
1233 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 6);
1236 * Decide waiting time before gating reference clock and
1237 * after ungating reference clock according to vendors'
1240 if (mid == UFS_VENDOR_SAMSUNG)
1241 ufs_mtk_setup_ref_clk_wait_us(hba, 1);
1242 else if (mid == UFS_VENDOR_SKHYNIX)
1243 ufs_mtk_setup_ref_clk_wait_us(hba, 30);
1244 else if (mid == UFS_VENDOR_TOSHIBA)
1245 ufs_mtk_setup_ref_clk_wait_us(hba, 100);
1247 ufs_mtk_setup_ref_clk_wait_us(hba,
1248 REFCLK_DEFAULT_WAIT_US);
1252 static void ufs_mtk_fixup_dev_quirks(struct ufs_hba *hba)
1254 ufshcd_fixup_dev_quirks(hba, ufs_mtk_dev_fixups);
1256 if (ufs_mtk_is_broken_vcc(hba) && hba->vreg_info.vcc &&
1257 (hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_AFTER_LPM)) {
1258 hba->vreg_info.vcc->always_on = true;
1260 * VCC will be kept always-on thus we don't
1261 * need any delay during regulator operations
1263 hba->dev_quirks &= ~(UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM |
1264 UFS_DEVICE_QUIRK_DELAY_AFTER_LPM);
1267 ufs_mtk_vreg_fix_vcc(hba);
1268 ufs_mtk_vreg_fix_vccqx(hba);
1271 static void ufs_mtk_event_notify(struct ufs_hba *hba,
1272 enum ufs_event_type evt, void *data)
1274 unsigned int val = *(u32 *)data;
1276 trace_ufs_mtk_event(evt, val);
1280 * struct ufs_hba_mtk_vops - UFS MTK specific variant operations
1282 * The variant operations configure the necessary controller and PHY
1283 * handshake during initialization.
1285 static const struct ufs_hba_variant_ops ufs_hba_mtk_vops = {
1286 .name = "mediatek.ufshci",
1287 .init = ufs_mtk_init,
1288 .get_ufs_hci_version = ufs_mtk_get_ufs_hci_version,
1289 .setup_clocks = ufs_mtk_setup_clocks,
1290 .hce_enable_notify = ufs_mtk_hce_enable_notify,
1291 .link_startup_notify = ufs_mtk_link_startup_notify,
1292 .pwr_change_notify = ufs_mtk_pwr_change_notify,
1293 .apply_dev_quirks = ufs_mtk_apply_dev_quirks,
1294 .fixup_dev_quirks = ufs_mtk_fixup_dev_quirks,
1295 .suspend = ufs_mtk_suspend,
1296 .resume = ufs_mtk_resume,
1297 .dbg_register_dump = ufs_mtk_dbg_register_dump,
1298 .device_reset = ufs_mtk_device_reset,
1299 .event_notify = ufs_mtk_event_notify,
1303 * ufs_mtk_probe - probe routine of the driver
1304 * @pdev: pointer to Platform device handle
1306 * Return zero for success and non-zero for failure
1308 static int ufs_mtk_probe(struct platform_device *pdev)
1311 struct device *dev = &pdev->dev;
1312 struct device_node *reset_node;
1313 struct platform_device *reset_pdev;
1314 struct device_link *link;
1316 reset_node = of_find_compatible_node(NULL, NULL,
1319 dev_notice(dev, "find ti,syscon-reset fail\n");
1322 reset_pdev = of_find_device_by_node(reset_node);
1324 dev_notice(dev, "find reset_pdev fail\n");
1327 link = device_link_add(dev, &reset_pdev->dev,
1328 DL_FLAG_AUTOPROBE_CONSUMER);
1329 put_device(&reset_pdev->dev);
1331 dev_notice(dev, "add reset device_link fail\n");
1334 /* supplier is not probed */
1335 if (link->status == DL_STATE_DORMANT) {
1336 err = -EPROBE_DEFER;
1341 /* perform generic probe */
1342 err = ufshcd_pltfrm_init(pdev, &ufs_hba_mtk_vops);
1346 dev_info(dev, "probe failed %d\n", err);
1348 of_node_put(reset_node);
1353 * ufs_mtk_remove - set driver_data of the device to NULL
1354 * @pdev: pointer to platform device handle
1358 static int ufs_mtk_remove(struct platform_device *pdev)
1360 struct ufs_hba *hba = platform_get_drvdata(pdev);
1362 pm_runtime_get_sync(&(pdev)->dev);
1367 #ifdef CONFIG_PM_SLEEP
1368 static int ufs_mtk_system_suspend(struct device *dev)
1370 struct ufs_hba *hba = dev_get_drvdata(dev);
1373 ret = ufshcd_system_suspend(dev);
1377 ufs_mtk_dev_vreg_set_lpm(hba, true);
1382 static int ufs_mtk_system_resume(struct device *dev)
1384 struct ufs_hba *hba = dev_get_drvdata(dev);
1386 ufs_mtk_dev_vreg_set_lpm(hba, false);
1388 return ufshcd_system_resume(dev);
1392 static int ufs_mtk_runtime_suspend(struct device *dev)
1394 struct ufs_hba *hba = dev_get_drvdata(dev);
1397 ret = ufshcd_runtime_suspend(dev);
1401 ufs_mtk_dev_vreg_set_lpm(hba, true);
1406 static int ufs_mtk_runtime_resume(struct device *dev)
1408 struct ufs_hba *hba = dev_get_drvdata(dev);
1410 ufs_mtk_dev_vreg_set_lpm(hba, false);
1412 return ufshcd_runtime_resume(dev);
1415 static const struct dev_pm_ops ufs_mtk_pm_ops = {
1416 SET_SYSTEM_SLEEP_PM_OPS(ufs_mtk_system_suspend,
1417 ufs_mtk_system_resume)
1418 SET_RUNTIME_PM_OPS(ufs_mtk_runtime_suspend,
1419 ufs_mtk_runtime_resume, NULL)
1420 .prepare = ufshcd_suspend_prepare,
1421 .complete = ufshcd_resume_complete,
1424 static struct platform_driver ufs_mtk_pltform = {
1425 .probe = ufs_mtk_probe,
1426 .remove = ufs_mtk_remove,
1427 .shutdown = ufshcd_pltfrm_shutdown,
1429 .name = "ufshcd-mtk",
1430 .pm = &ufs_mtk_pm_ops,
1431 .of_match_table = ufs_mtk_of_match,
1435 MODULE_AUTHOR("Stanley Chu <stanley.chu@mediatek.com>");
1436 MODULE_AUTHOR("Peter Wang <peter.wang@mediatek.com>");
1437 MODULE_DESCRIPTION("MediaTek UFS Host Driver");
1438 MODULE_LICENSE("GPL v2");
1440 module_platform_driver(ufs_mtk_pltform);