Get rid of 'remove_new' relic from platform driver struct
[linux.git] / drivers / ufs / host / ufs-mediatek.c
blob6fc848d0ada8bcf13aaf9f095ad9e4e43876c743
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2019 MediaTek Inc.
4 * Authors:
5 * Stanley Chu <stanley.chu@mediatek.com>
6 * Peter Wang <peter.wang@mediatek.com>
7 */
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>
14 #include <linux/of.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" },
53 {},
55 MODULE_DEVICE_TABLE(of, ufs_mtk_of_match);
58 * Details of UIC Errors
60 static const char *const ufs_uic_err_str[] = {
61 "PHY Adapter Layer",
62 "Data Link Layer",
63 "Network Link Layer",
64 "Transport Link Layer",
65 "DME"
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[] = {
77 "NAC_RECEIVED",
78 "TCx_REPLAY_TIMER_EXPIRED",
79 "AFCx_REQUEST_TIMER_EXPIRED",
80 "FCx_PROTECTION_TIMER_EXPIRED",
81 "CRC_ERROR",
82 "RX_BUFFER_OVERFLOW",
83 "MAX_FRAME_LENGTH_EXCEEDED",
84 "WRONG_SEQUENCE_NUMBER",
85 "AFC_FRAME_SYNTAX_ERROR",
86 "NAC_FRAME_SYNTAX_ERROR",
87 "EOF_SYNTAX_ERROR",
88 "FRAME_SYNTAX_ERROR",
89 "BAD_CTRL_SYMBOL_TYPE",
90 "PA_INIT_ERROR",
91 "PA_ERROR_IND_RECEIVED",
92 "PA_INIT"
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)
146 u32 tmp;
148 if (enable) {
149 ufshcd_dme_get(hba,
150 UIC_ARG_MIB(VS_SAVEPOWERCONTROL), &tmp);
151 tmp = tmp |
152 (1 << RX_SYMBOL_CLK_GATE_EN) |
153 (1 << SYS_CLK_GATE_EN) |
154 (1 << TX_CLK_GATE_EN);
155 ufshcd_dme_set(hba,
156 UIC_ARG_MIB(VS_SAVEPOWERCONTROL), tmp);
158 ufshcd_dme_get(hba,
159 UIC_ARG_MIB(VS_DEBUGCLOCKENABLE), &tmp);
160 tmp = tmp & ~(1 << TX_SYMBOL_CLK_REQ_FORCE);
161 ufshcd_dme_set(hba,
162 UIC_ARG_MIB(VS_DEBUGCLOCKENABLE), tmp);
163 } else {
164 ufshcd_dme_get(hba,
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));
169 ufshcd_dme_set(hba,
170 UIC_ARG_MIB(VS_SAVEPOWERCONTROL), tmp);
172 ufshcd_dme_get(hba,
173 UIC_ARG_MIB(VS_DEBUGCLOCKENABLE), &tmp);
174 tmp = tmp | (1 << TX_SYMBOL_CLK_REQ_FORCE);
175 ufshcd_dme_set(hba,
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);
185 if (res.a0) {
186 dev_info(hba->dev, "%s: crypto enable failed, err: %lu\n",
187 __func__, res.a0);
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,
216 char *str)
218 *rc = devm_reset_control_get(hba->dev, str);
219 if (IS_ERR(*rc)) {
220 dev_info(hba->dev, "Failed to get reset control %s: %ld\n",
221 str, PTR_ERR(*rc));
222 *rc = NULL;
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,
231 "hci_rst");
232 ufs_mtk_init_reset_control(hba, &host->unipro_reset,
233 "unipro_rst");
234 ufs_mtk_init_reset_control(hba, &host->crypto_reset,
235 "crypto_rst");
236 ufs_mtk_init_reset_control(hba, &host->mphy_reset,
237 "mphy_rst");
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;
248 } else {
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;
260 hba->ahit = 0;
264 * Turn on CLK_CG early to bypass abnormal ERR_CHK signal
265 * to prevent host hang issue
267 ufshcd_writel(hba,
268 ufshcd_readl(hba, REG_UFS_XOUFS_CTRL) | 0x80,
269 REG_UFS_XOUFS_CTRL);
272 return 0;
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;
280 int err = 0;
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.
289 err = -EPROBE_DEFER;
290 dev_info(dev,
291 "%s: required phy hasn't probed yet. err = %d\n",
292 __func__, err);
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__,
297 err);
301 if (err)
302 host->mphy = NULL;
304 * Allow unbound mphy because not every platform needs specific
305 * mphy control.
307 if (err == -ENODEV)
308 err = 0;
310 return err;
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;
318 u32 value;
320 if (host->ref_clk_enabled == on)
321 return 0;
323 ufs_mtk_ref_clk_notify(on, PRE_CHANGE, res);
325 if (on) {
326 ufshcd_writel(hba, REFCLK_REQUEST, REG_UFS_REFCLK_CTRL);
327 } else {
328 ufshcd_delay_us(host->ref_clk_gating_wait_us, 10);
329 ufshcd_writel(hba, REFCLK_RELEASE, REG_UFS_REFCLK_CTRL);
332 /* Wait for ack */
333 timeout = ktime_add_us(ktime_get(), REFCLK_REQ_TIMEOUT_US);
334 do {
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))
340 goto out;
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);
349 return -ETIMEDOUT;
351 out:
352 host->ref_clk_enabled = on;
353 if (on)
354 ufshcd_delay_us(host->ref_clk_ungating_wait_us, 10);
356 ufs_mtk_ref_clk_notify(on, POST_CHANGE, res);
358 return 0;
361 static void ufs_mtk_setup_ref_clk_wait_us(struct ufs_hba *hba,
362 u16 gating_us)
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;
369 } else {
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);
386 } else {
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;
395 u32 val, sm;
396 bool wait_idle;
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 */
402 udelay(10);
403 wait_idle = false;
405 do {
406 time_checked = ktime_get_mono_fast_ns();
407 ufs_mtk_dbg_sel(hba);
408 val = ufshcd_readl(hba, REG_UFS_PROBE);
410 sm = val & 0x1f;
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)) {
417 wait_idle = true;
418 udelay(50);
419 continue;
420 } else if (!wait_idle)
421 break;
423 if (wait_idle && (sm == VS_HCE_BASE))
424 break;
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;
435 u32 val;
437 timeout = ktime_add_ms(ktime_get(), max_wait_ms);
438 do {
439 time_checked = ktime_get();
440 ufs_mtk_dbg_sel(hba);
441 val = ufshcd_readl(hba, REG_UFS_PROBE);
442 val = val >> 28;
444 if (val == state)
445 return 0;
447 /* Sleep for max. 200us */
448 usleep_range(100, 200);
449 } while (ktime_before(time_checked, timeout));
451 return -ETIMEDOUT;
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;
459 int ret = 0;
461 if (!mphy || !(on ^ host->mphy_powered_on))
462 return 0;
464 if (on) {
465 if (ufs_mtk_is_va09_supported(hba)) {
466 ret = regulator_enable(host->reg_va09);
467 if (ret < 0)
468 goto out;
469 /* wait 200 us to stablize VA09 */
470 usleep_range(200, 210);
471 ufs_mtk_va09_pwr_ctrl(res, 1);
473 phy_power_on(mphy);
474 } else {
475 phy_power_off(mphy);
476 if (ufs_mtk_is_va09_supported(hba)) {
477 ufs_mtk_va09_pwr_ctrl(res, 0);
478 ret = regulator_disable(host->reg_va09);
481 out:
482 if (ret) {
483 dev_info(hba->dev,
484 "failed to %s va09: %d\n",
485 on ? "enable" : "disable",
486 ret);
487 } else {
488 host->mphy_powered_on = on;
491 return ret;
494 static int ufs_mtk_get_host_clk(struct device *dev, const char *name,
495 struct clk **clk_out)
497 struct clk *clk;
498 int err = 0;
500 clk = devm_clk_get(dev, name);
501 if (IS_ERR(clk))
502 err = PTR_ERR(clk);
503 else
504 *clk_out = clk;
506 return err;
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;
514 int volt, ret;
516 if (!ufs_mtk_is_boost_crypt_enabled(hba))
517 return;
519 cfg = host->crypt;
520 volt = cfg->vcore_volt;
521 reg = cfg->reg_vcore;
523 ret = clk_prepare_enable(cfg->clk_crypt_mux);
524 if (ret) {
525 dev_info(hba->dev, "clk_prepare_enable(): %d\n",
526 ret);
527 return;
530 if (boost) {
531 ret = regulator_set_voltage(reg, volt, INT_MAX);
532 if (ret) {
533 dev_info(hba->dev,
534 "failed to set vcore to %d\n", volt);
535 goto out;
538 ret = clk_set_parent(cfg->clk_crypt_mux,
539 cfg->clk_crypt_perf);
540 if (ret) {
541 dev_info(hba->dev,
542 "failed to set clk_crypt_perf\n");
543 regulator_set_voltage(reg, 0, INT_MAX);
544 goto out;
546 } else {
547 ret = clk_set_parent(cfg->clk_crypt_mux,
548 cfg->clk_crypt_lp);
549 if (ret) {
550 dev_info(hba->dev,
551 "failed to set clk_crypt_lp\n");
552 goto out;
555 ret = regulator_set_voltage(reg, 0, INT_MAX);
556 if (ret) {
557 dev_info(hba->dev,
558 "failed to set vcore to MIN\n");
561 out:
562 clk_disable_unprepare(cfg->clk_crypt_mux);
565 static int ufs_mtk_init_host_clk(struct ufs_hba *hba, const char *name,
566 struct clk **clk)
568 int ret;
570 ret = ufs_mtk_get_host_clk(hba->dev, name, clk);
571 if (ret) {
572 dev_info(hba->dev, "%s: failed to get %s: %d", __func__,
573 name, ret);
576 return ret;
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;
585 u32 volt;
587 host->crypt = devm_kzalloc(dev, sizeof(*(host->crypt)),
588 GFP_KERNEL);
589 if (!host->crypt)
590 goto disable_caps;
592 reg = devm_regulator_get_optional(dev, "dvfsrc-vcore");
593 if (IS_ERR(reg)) {
594 dev_info(dev, "failed to get dvfsrc-vcore: %ld",
595 PTR_ERR(reg));
596 goto disable_caps;
599 if (of_property_read_u32(dev->of_node, "boost-crypt-vcore-min",
600 &volt)) {
601 dev_info(dev, "failed to get boost-crypt-vcore-min");
602 goto disable_caps;
605 cfg = host->crypt;
606 if (ufs_mtk_init_host_clk(hba, "crypt_mux",
607 &cfg->clk_crypt_mux))
608 goto disable_caps;
610 if (ufs_mtk_init_host_clk(hba, "crypt_lp",
611 &cfg->clk_crypt_lp))
612 goto disable_caps;
614 if (ufs_mtk_init_host_clk(hba, "crypt_perf",
615 &cfg->clk_crypt_perf))
616 goto disable_caps;
618 cfg->reg_vcore = reg;
619 cfg->vcore_volt = volt;
620 host->caps |= UFS_MTK_CAP_BOOST_CRYPT_ENGINE;
622 disable_caps:
623 return;
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");
633 else
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);
678 if (on) {
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);
683 } else {
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);
694 u32 irq, i;
696 if (!hba->mcq_enabled)
697 return;
699 if (host->mcq_nr_intr == 0)
700 return;
702 for (i = 0; i < host->mcq_nr_intr; i++) {
703 irq = host->mcq_intr_info[i].irq;
704 disable_irq(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);
712 u32 irq, i;
714 if (!hba->mcq_enabled)
715 return;
717 if (host->mcq_nr_intr == 0)
718 return;
720 if (host->is_mcq_intr_enabled == true)
721 return;
723 for (i = 0; i < host->mcq_nr_intr; i++) {
724 irq = host->mcq_intr_info[i].irq;
725 enable_irq(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;
743 int ret = 0;
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.
750 if (!host)
751 return 0;
753 if (!on && status == PRE_CHANGE) {
754 if (ufshcd_is_link_off(hba)) {
755 clk_pwr_off = true;
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,
765 VS_LINK_HIBERN8,
766 15);
767 if (!ret)
768 clk_pwr_off = true;
771 if (clk_pwr_off)
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);
779 return ret;
782 static void ufs_mtk_get_controller_version(struct ufs_hba *hba)
784 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
785 int ret, ver = 0;
787 if (host->hw_ver.major)
788 return;
790 /* Set default (minimum) version anyway */
791 host->hw_ver.major = 2;
793 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_LOCALVERINFO), &ver);
794 if (!ret) {
795 if (ver >= UFS_UNIPRO_VER_1_8) {
796 host->hw_ver.major = 3;
798 * Fix HCI version for some platforms with
799 * incorrect version
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;
846 dev_info(hba->dev,
847 "%s: Clk-scaling not ready. Feature disabled.",
848 __func__);
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;
860 int err, ver;
862 if (hba->vreg_info.vcc)
863 return 0;
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);
869 else
870 return -ENODEV;
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);
874 } else {
875 return 0;
878 err = ufshcd_populate_vreg(dev, vcc_name, &info->vcc, false);
879 if (err)
880 return err;
882 err = ufshcd_get_vreg(dev, info->vcc);
883 if (err)
884 return err;
886 err = regulator_enable(info->vcc->reg);
887 if (!err) {
888 info->vcc->enabled = true;
889 dev_info(dev, "%s: %s enabled\n", __func__, vcc_name);
892 return err;
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;
903 } else {
904 vreg_on = &info->vccq2;
905 vreg_off = &info->vccq;
908 if (*vreg_on)
909 (*vreg_on)->always_on = true;
911 if (*vreg_off) {
912 regulator_disable((*vreg_off)->reg);
913 devm_kfree(hba->dev, (*vreg_off)->name);
914 devm_kfree(hba->dev, *vreg_off);
915 *vreg_off = NULL;
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;
923 int i;
924 int irq;
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)
930 goto failed;
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);
935 if (irq < 0) {
936 host->mcq_intr_info[i].irq = MTK_MCQ_INVALID_IRQ;
937 goto failed;
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);
944 return;
945 failed:
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
958 * and regulators.
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;
969 int err = 0;
970 struct arm_smccc_res res;
972 host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
973 if (!host) {
974 err = -ENOMEM;
975 dev_info(dev, "%s: no memory for mtk ufs host\n", __func__);
976 goto out;
979 host->hba = hba;
980 ufshcd_set_variant(hba, host);
982 id = of_match_device(ufs_mtk_of_match, dev);
983 if (!id) {
984 err = -EINVAL;
985 goto out;
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);
994 if (err)
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 if (host->caps & UFS_MTK_CAP_DISABLE_MCQ)
1030 hba->quirks |= UFSHCD_QUIRK_BROKEN_LSDBS_CAP;
1032 ufs_mtk_init_clocks(hba);
1035 * ufshcd_vops_init() is invoked after
1036 * ufshcd_setup_clock(true) in ufshcd_hba_init() thus
1037 * phy clock setup is skipped.
1039 * Enable phy clocks specifically here.
1041 ufs_mtk_mphy_power_on(hba, true);
1043 if (ufs_mtk_is_rtff_mtcmos(hba)) {
1044 /* First Restore here, to avoid backup unexpected value */
1045 ufs_mtk_mtcmos_ctrl(false, res);
1047 /* Power on to init */
1048 ufs_mtk_mtcmos_ctrl(true, res);
1051 ufs_mtk_setup_clocks(hba, true, POST_CHANGE);
1053 host->ip_ver = ufshcd_readl(hba, REG_UFS_MTK_IP_VER);
1055 goto out;
1057 out_variant_clear:
1058 ufshcd_set_variant(hba, NULL);
1059 out:
1060 return err;
1063 static bool ufs_mtk_pmc_via_fastauto(struct ufs_hba *hba,
1064 struct ufs_pa_layer_attr *dev_req_params)
1066 if (!ufs_mtk_is_pmc_via_fastauto(hba))
1067 return false;
1069 if (dev_req_params->hs_rate == hba->pwr_info.hs_rate)
1070 return false;
1072 if (dev_req_params->pwr_tx != FAST_MODE &&
1073 dev_req_params->gear_tx < UFS_HS_G4)
1074 return false;
1076 if (dev_req_params->pwr_rx != FAST_MODE &&
1077 dev_req_params->gear_rx < UFS_HS_G4)
1078 return false;
1080 return true;
1083 static int ufs_mtk_pre_pwr_change(struct ufs_hba *hba,
1084 struct ufs_pa_layer_attr *dev_max_params,
1085 struct ufs_pa_layer_attr *dev_req_params)
1087 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
1088 struct ufs_host_params host_params;
1089 int ret;
1091 ufshcd_init_host_params(&host_params);
1092 host_params.hs_rx_gear = UFS_HS_G5;
1093 host_params.hs_tx_gear = UFS_HS_G5;
1095 ret = ufshcd_negotiate_pwr_params(&host_params, dev_max_params, dev_req_params);
1096 if (ret) {
1097 pr_info("%s: failed to determine capabilities\n",
1098 __func__);
1101 if (ufs_mtk_pmc_via_fastauto(hba, dev_req_params)) {
1102 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), true);
1103 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), UFS_HS_G1);
1105 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), true);
1106 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), UFS_HS_G1);
1108 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
1109 dev_req_params->lane_tx);
1110 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
1111 dev_req_params->lane_rx);
1112 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
1113 dev_req_params->hs_rate);
1115 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXHSADAPTTYPE),
1116 PA_NO_ADAPT);
1118 ret = ufshcd_uic_change_pwr_mode(hba,
1119 FASTAUTO_MODE << 4 | FASTAUTO_MODE);
1121 if (ret) {
1122 dev_err(hba->dev, "%s: HSG1B FASTAUTO failed ret=%d\n",
1123 __func__, ret);
1127 if (host->hw_ver.major >= 3) {
1128 ret = ufshcd_dme_configure_adapt(hba,
1129 dev_req_params->gear_tx,
1130 PA_INITIAL_ADAPT);
1133 return ret;
1136 static int ufs_mtk_pwr_change_notify(struct ufs_hba *hba,
1137 enum ufs_notify_change_status stage,
1138 struct ufs_pa_layer_attr *dev_max_params,
1139 struct ufs_pa_layer_attr *dev_req_params)
1141 int ret = 0;
1143 switch (stage) {
1144 case PRE_CHANGE:
1145 ret = ufs_mtk_pre_pwr_change(hba, dev_max_params,
1146 dev_req_params);
1147 break;
1148 case POST_CHANGE:
1149 break;
1150 default:
1151 ret = -EINVAL;
1152 break;
1155 return ret;
1158 static int ufs_mtk_unipro_set_lpm(struct ufs_hba *hba, bool lpm)
1160 int ret;
1161 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
1163 ret = ufshcd_dme_set(hba,
1164 UIC_ARG_MIB_SEL(VS_UNIPROPOWERDOWNCONTROL, 0),
1165 lpm ? 1 : 0);
1166 if (!ret || !lpm) {
1168 * Forcibly set as non-LPM mode if UIC commands is failed
1169 * to use default hba_enable_delay_us value for re-enabling
1170 * the host.
1172 host->unipro_lpm = lpm;
1175 return ret;
1178 static int ufs_mtk_pre_link(struct ufs_hba *hba)
1180 int ret;
1181 u32 tmp;
1183 ufs_mtk_get_controller_version(hba);
1185 ret = ufs_mtk_unipro_set_lpm(hba, false);
1186 if (ret)
1187 return ret;
1190 * Setting PA_Local_TX_LCC_Enable to 0 before link startup
1191 * to make sure that both host and device TX LCC are disabled
1192 * once link startup is completed.
1194 ret = ufshcd_disable_host_tx_lcc(hba);
1195 if (ret)
1196 return ret;
1198 /* disable deep stall */
1199 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(VS_SAVEPOWERCONTROL), &tmp);
1200 if (ret)
1201 return ret;
1203 tmp &= ~(1 << 6);
1205 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(VS_SAVEPOWERCONTROL), tmp);
1207 return ret;
1210 static void ufs_mtk_setup_clk_gating(struct ufs_hba *hba)
1212 u32 ah_ms;
1214 if (ufshcd_is_clkgating_allowed(hba)) {
1215 if (ufshcd_is_auto_hibern8_supported(hba) && hba->ahit)
1216 ah_ms = FIELD_GET(UFSHCI_AHIBERN8_TIMER_MASK,
1217 hba->ahit);
1218 else
1219 ah_ms = 10;
1220 ufshcd_clkgate_delay_set(hba->dev, ah_ms + 5);
1224 static void ufs_mtk_post_link(struct ufs_hba *hba)
1226 /* enable unipro clock gating feature */
1227 ufs_mtk_cfg_unipro_cg(hba, true);
1229 /* will be configured during probe hba */
1230 if (ufshcd_is_auto_hibern8_supported(hba))
1231 hba->ahit = FIELD_PREP(UFSHCI_AHIBERN8_TIMER_MASK, 10) |
1232 FIELD_PREP(UFSHCI_AHIBERN8_SCALE_MASK, 3);
1234 ufs_mtk_setup_clk_gating(hba);
1237 static int ufs_mtk_link_startup_notify(struct ufs_hba *hba,
1238 enum ufs_notify_change_status stage)
1240 int ret = 0;
1242 switch (stage) {
1243 case PRE_CHANGE:
1244 ret = ufs_mtk_pre_link(hba);
1245 break;
1246 case POST_CHANGE:
1247 ufs_mtk_post_link(hba);
1248 break;
1249 default:
1250 ret = -EINVAL;
1251 break;
1254 return ret;
1257 static int ufs_mtk_device_reset(struct ufs_hba *hba)
1259 struct arm_smccc_res res;
1261 /* disable hba before device reset */
1262 ufshcd_hba_stop(hba);
1264 ufs_mtk_device_reset_ctrl(0, res);
1267 * The reset signal is active low. UFS devices shall detect
1268 * more than or equal to 1us of positive or negative RST_n
1269 * pulse width.
1271 * To be on safe side, keep the reset low for at least 10us.
1273 usleep_range(10, 15);
1275 ufs_mtk_device_reset_ctrl(1, res);
1277 /* Some devices may need time to respond to rst_n */
1278 usleep_range(10000, 15000);
1280 dev_info(hba->dev, "device reset done\n");
1282 return 0;
1285 static int ufs_mtk_link_set_hpm(struct ufs_hba *hba)
1287 int err;
1289 err = ufshcd_hba_enable(hba);
1290 if (err)
1291 return err;
1293 err = ufs_mtk_unipro_set_lpm(hba, false);
1294 if (err)
1295 return err;
1297 err = ufshcd_uic_hibern8_exit(hba);
1298 if (err)
1299 return err;
1301 /* Check link state to make sure exit h8 success */
1302 ufs_mtk_wait_idle_state(hba, 5);
1303 err = ufs_mtk_wait_link_state(hba, VS_LINK_UP, 100);
1304 if (err) {
1305 dev_warn(hba->dev, "exit h8 state fail, err=%d\n", err);
1306 return err;
1308 ufshcd_set_link_active(hba);
1310 err = ufshcd_make_hba_operational(hba);
1311 if (err)
1312 return err;
1314 if (hba->mcq_enabled) {
1315 ufs_mtk_config_mcq(hba, false);
1316 ufshcd_mcq_make_queues_operational(hba);
1317 ufshcd_mcq_config_mac(hba, hba->nutrs);
1318 ufshcd_mcq_enable(hba);
1321 return 0;
1324 static int ufs_mtk_link_set_lpm(struct ufs_hba *hba)
1326 int err;
1328 /* Disable reset confirm feature by UniPro */
1329 ufshcd_writel(hba,
1330 (ufshcd_readl(hba, REG_UFS_XOUFS_CTRL) & ~0x100),
1331 REG_UFS_XOUFS_CTRL);
1333 err = ufs_mtk_unipro_set_lpm(hba, true);
1334 if (err) {
1335 /* Resume UniPro state for following error recovery */
1336 ufs_mtk_unipro_set_lpm(hba, false);
1337 return err;
1340 return 0;
1343 static void ufs_mtk_vccqx_set_lpm(struct ufs_hba *hba, bool lpm)
1345 struct ufs_vreg *vccqx = NULL;
1347 if (hba->vreg_info.vccq)
1348 vccqx = hba->vreg_info.vccq;
1349 else
1350 vccqx = hba->vreg_info.vccq2;
1352 regulator_set_mode(vccqx->reg,
1353 lpm ? REGULATOR_MODE_IDLE : REGULATOR_MODE_NORMAL);
1356 static void ufs_mtk_vsx_set_lpm(struct ufs_hba *hba, bool lpm)
1358 struct arm_smccc_res res;
1360 ufs_mtk_device_pwr_ctrl(!lpm,
1361 (unsigned long)hba->dev_info.wspecversion,
1362 res);
1365 static void ufs_mtk_dev_vreg_set_lpm(struct ufs_hba *hba, bool lpm)
1367 bool skip_vccqx = false;
1369 /* Prevent entering LPM when device is still active */
1370 if (lpm && ufshcd_is_ufs_dev_active(hba))
1371 return;
1373 /* Skip vccqx lpm control and control vsx only */
1374 if (!hba->vreg_info.vccq && !hba->vreg_info.vccq2)
1375 skip_vccqx = true;
1377 /* VCC is always-on, control vsx only */
1378 if (!hba->vreg_info.vcc)
1379 skip_vccqx = true;
1381 /* Broken vcc keep vcc always on, most case control vsx only */
1382 if (lpm && hba->vreg_info.vcc && hba->vreg_info.vcc->enabled) {
1383 /* Some device vccqx/vsx can enter lpm */
1384 if (ufs_mtk_is_allow_vccqx_lpm(hba))
1385 skip_vccqx = false;
1386 else /* control vsx only */
1387 skip_vccqx = true;
1390 if (lpm) {
1391 if (!skip_vccqx)
1392 ufs_mtk_vccqx_set_lpm(hba, lpm);
1393 ufs_mtk_vsx_set_lpm(hba, lpm);
1394 } else {
1395 ufs_mtk_vsx_set_lpm(hba, lpm);
1396 if (!skip_vccqx)
1397 ufs_mtk_vccqx_set_lpm(hba, lpm);
1401 static void ufs_mtk_auto_hibern8_disable(struct ufs_hba *hba)
1403 int ret;
1405 /* disable auto-hibern8 */
1406 ufshcd_writel(hba, 0, REG_AUTO_HIBERNATE_IDLE_TIMER);
1408 /* wait host return to idle state when auto-hibern8 off */
1409 ufs_mtk_wait_idle_state(hba, 5);
1411 ret = ufs_mtk_wait_link_state(hba, VS_LINK_UP, 100);
1412 if (ret)
1413 dev_warn(hba->dev, "exit h8 state fail, ret=%d\n", ret);
1416 static int ufs_mtk_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op,
1417 enum ufs_notify_change_status status)
1419 int err;
1420 struct arm_smccc_res res;
1422 if (status == PRE_CHANGE) {
1423 if (ufshcd_is_auto_hibern8_supported(hba))
1424 ufs_mtk_auto_hibern8_disable(hba);
1425 return 0;
1428 if (ufshcd_is_link_hibern8(hba)) {
1429 err = ufs_mtk_link_set_lpm(hba);
1430 if (err)
1431 goto fail;
1434 if (!ufshcd_is_link_active(hba)) {
1436 * Make sure no error will be returned to prevent
1437 * ufshcd_suspend() re-enabling regulators while vreg is still
1438 * in low-power mode.
1440 err = ufs_mtk_mphy_power_on(hba, false);
1441 if (err)
1442 goto fail;
1445 if (ufshcd_is_link_off(hba))
1446 ufs_mtk_device_reset_ctrl(0, res);
1448 ufs_mtk_sram_pwr_ctrl(false, res);
1450 return 0;
1451 fail:
1453 * Set link as off state enforcedly to trigger
1454 * ufshcd_host_reset_and_restore() in ufshcd_suspend()
1455 * for completed host reset.
1457 ufshcd_set_link_off(hba);
1458 return -EAGAIN;
1461 static int ufs_mtk_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
1463 int err;
1464 struct arm_smccc_res res;
1466 if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL)
1467 ufs_mtk_dev_vreg_set_lpm(hba, false);
1469 ufs_mtk_sram_pwr_ctrl(true, res);
1471 err = ufs_mtk_mphy_power_on(hba, true);
1472 if (err)
1473 goto fail;
1475 if (ufshcd_is_link_hibern8(hba)) {
1476 err = ufs_mtk_link_set_hpm(hba);
1477 if (err)
1478 goto fail;
1481 return 0;
1482 fail:
1483 return ufshcd_link_recovery(hba);
1486 static void ufs_mtk_dbg_register_dump(struct ufs_hba *hba)
1488 /* Dump ufshci register 0x140 ~ 0x14C */
1489 ufshcd_dump_regs(hba, REG_UFS_XOUFS_CTRL, 0x10,
1490 "XOUFS Ctrl (0x140): ");
1492 ufshcd_dump_regs(hba, REG_UFS_EXTREG, 0x4, "Ext Reg ");
1494 /* Dump ufshci register 0x2200 ~ 0x22AC */
1495 ufshcd_dump_regs(hba, REG_UFS_MPHYCTRL,
1496 REG_UFS_REJECT_MON - REG_UFS_MPHYCTRL + 4,
1497 "MPHY Ctrl (0x2200): ");
1499 /* Direct debugging information to REG_MTK_PROBE */
1500 ufs_mtk_dbg_sel(hba);
1501 ufshcd_dump_regs(hba, REG_UFS_PROBE, 0x4, "Debug Probe ");
1504 static int ufs_mtk_apply_dev_quirks(struct ufs_hba *hba)
1506 struct ufs_dev_info *dev_info = &hba->dev_info;
1507 u16 mid = dev_info->wmanufacturerid;
1509 if (mid == UFS_VENDOR_SAMSUNG) {
1510 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 6);
1511 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME), 10);
1512 } else if (mid == UFS_VENDOR_MICRON) {
1513 /* Only for the host which have TX skew issue */
1514 if (ufs_mtk_is_tx_skew_fix(hba) &&
1515 (STR_PRFX_EQUAL("MT128GBCAV2U31", dev_info->model) ||
1516 STR_PRFX_EQUAL("MT256GBCAV4U31", dev_info->model) ||
1517 STR_PRFX_EQUAL("MT512GBCAV8U31", dev_info->model) ||
1518 STR_PRFX_EQUAL("MT256GBEAX4U40", dev_info->model) ||
1519 STR_PRFX_EQUAL("MT512GAYAX4U40", dev_info->model) ||
1520 STR_PRFX_EQUAL("MT001TAYAX8U40", dev_info->model))) {
1521 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 8);
1526 * Decide waiting time before gating reference clock and
1527 * after ungating reference clock according to vendors'
1528 * requirements.
1530 if (mid == UFS_VENDOR_SAMSUNG)
1531 ufs_mtk_setup_ref_clk_wait_us(hba, 1);
1532 else if (mid == UFS_VENDOR_SKHYNIX)
1533 ufs_mtk_setup_ref_clk_wait_us(hba, 30);
1534 else if (mid == UFS_VENDOR_TOSHIBA)
1535 ufs_mtk_setup_ref_clk_wait_us(hba, 100);
1536 else
1537 ufs_mtk_setup_ref_clk_wait_us(hba,
1538 REFCLK_DEFAULT_WAIT_US);
1539 return 0;
1542 static void ufs_mtk_fixup_dev_quirks(struct ufs_hba *hba)
1544 ufshcd_fixup_dev_quirks(hba, ufs_mtk_dev_fixups);
1546 if (ufs_mtk_is_broken_vcc(hba) && hba->vreg_info.vcc &&
1547 (hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_AFTER_LPM)) {
1548 hba->vreg_info.vcc->always_on = true;
1550 * VCC will be kept always-on thus we don't
1551 * need any delay during regulator operations
1553 hba->dev_quirks &= ~(UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM |
1554 UFS_DEVICE_QUIRK_DELAY_AFTER_LPM);
1557 ufs_mtk_vreg_fix_vcc(hba);
1558 ufs_mtk_vreg_fix_vccqx(hba);
1561 static void ufs_mtk_event_notify(struct ufs_hba *hba,
1562 enum ufs_event_type evt, void *data)
1564 unsigned int val = *(u32 *)data;
1565 unsigned long reg;
1566 u8 bit;
1568 trace_ufs_mtk_event(evt, val);
1570 /* Print details of UIC Errors */
1571 if (evt <= UFS_EVT_DME_ERR) {
1572 dev_info(hba->dev,
1573 "Host UIC Error Code (%s): %08x\n",
1574 ufs_uic_err_str[evt], val);
1575 reg = val;
1578 if (evt == UFS_EVT_PA_ERR) {
1579 for_each_set_bit(bit, &reg, ARRAY_SIZE(ufs_uic_pa_err_str))
1580 dev_info(hba->dev, "%s\n", ufs_uic_pa_err_str[bit]);
1583 if (evt == UFS_EVT_DL_ERR) {
1584 for_each_set_bit(bit, &reg, ARRAY_SIZE(ufs_uic_dl_err_str))
1585 dev_info(hba->dev, "%s\n", ufs_uic_dl_err_str[bit]);
1589 static void ufs_mtk_config_scaling_param(struct ufs_hba *hba,
1590 struct devfreq_dev_profile *profile,
1591 struct devfreq_simple_ondemand_data *data)
1593 /* Customize min gear in clk scaling */
1594 hba->clk_scaling.min_gear = UFS_HS_G4;
1596 hba->vps->devfreq_profile.polling_ms = 200;
1597 hba->vps->ondemand_data.upthreshold = 50;
1598 hba->vps->ondemand_data.downdifferential = 20;
1602 * ufs_mtk_clk_scale - Internal clk scaling operation
1604 * MTK platform supports clk scaling by switching parent of ufs_sel(mux).
1605 * The ufs_sel downstream to ufs_ck which feeds directly to UFS hardware.
1606 * Max and min clocks rate of ufs_sel defined in dts should match rate of
1607 * "ufs_sel_max_src" and "ufs_sel_min_src" respectively.
1608 * This prevent changing rate of pll clock that is shared between modules.
1610 * @hba: per adapter instance
1611 * @scale_up: True for scaling up and false for scaling down
1613 static void ufs_mtk_clk_scale(struct ufs_hba *hba, bool scale_up)
1615 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
1616 struct ufs_mtk_clk *mclk = &host->mclk;
1617 struct ufs_clk_info *clki = mclk->ufs_sel_clki;
1618 int ret = 0;
1620 ret = clk_prepare_enable(clki->clk);
1621 if (ret) {
1622 dev_info(hba->dev,
1623 "clk_prepare_enable() fail, ret: %d\n", ret);
1624 return;
1627 if (scale_up) {
1628 ret = clk_set_parent(clki->clk, mclk->ufs_sel_max_clki->clk);
1629 clki->curr_freq = clki->max_freq;
1630 } else {
1631 ret = clk_set_parent(clki->clk, mclk->ufs_sel_min_clki->clk);
1632 clki->curr_freq = clki->min_freq;
1635 if (ret) {
1636 dev_info(hba->dev,
1637 "Failed to set ufs_sel_clki, ret: %d\n", ret);
1640 clk_disable_unprepare(clki->clk);
1642 trace_ufs_mtk_clk_scale(clki->name, scale_up, clk_get_rate(clki->clk));
1645 static int ufs_mtk_clk_scale_notify(struct ufs_hba *hba, bool scale_up,
1646 enum ufs_notify_change_status status)
1648 if (!ufshcd_is_clkscaling_supported(hba))
1649 return 0;
1651 if (status == PRE_CHANGE) {
1652 /* Switch parent before clk_set_rate() */
1653 ufs_mtk_clk_scale(hba, scale_up);
1654 } else {
1655 /* Request interrupt latency QoS accordingly */
1656 ufs_mtk_scale_perf(hba, scale_up);
1659 return 0;
1662 static int ufs_mtk_get_hba_mac(struct ufs_hba *hba)
1664 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
1666 /* MCQ operation not permitted */
1667 if (host->caps & UFS_MTK_CAP_DISABLE_MCQ)
1668 return -EPERM;
1670 return MAX_SUPP_MAC;
1673 static int ufs_mtk_op_runtime_config(struct ufs_hba *hba)
1675 struct ufshcd_mcq_opr_info_t *opr;
1676 int i;
1678 hba->mcq_opr[OPR_SQD].offset = REG_UFS_MTK_SQD;
1679 hba->mcq_opr[OPR_SQIS].offset = REG_UFS_MTK_SQIS;
1680 hba->mcq_opr[OPR_CQD].offset = REG_UFS_MTK_CQD;
1681 hba->mcq_opr[OPR_CQIS].offset = REG_UFS_MTK_CQIS;
1683 for (i = 0; i < OPR_MAX; i++) {
1684 opr = &hba->mcq_opr[i];
1685 opr->stride = REG_UFS_MCQ_STRIDE;
1686 opr->base = hba->mmio_base + opr->offset;
1689 return 0;
1692 static int ufs_mtk_mcq_config_resource(struct ufs_hba *hba)
1694 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
1696 /* fail mcq initialization if interrupt is not filled properly */
1697 if (!host->mcq_nr_intr) {
1698 dev_info(hba->dev, "IRQs not ready. MCQ disabled.");
1699 return -EINVAL;
1702 hba->mcq_base = hba->mmio_base + MCQ_QUEUE_OFFSET(hba->mcq_capabilities);
1703 return 0;
1706 static irqreturn_t ufs_mtk_mcq_intr(int irq, void *__intr_info)
1708 struct ufs_mtk_mcq_intr_info *mcq_intr_info = __intr_info;
1709 struct ufs_hba *hba = mcq_intr_info->hba;
1710 struct ufs_hw_queue *hwq;
1711 u32 events;
1712 int qid = mcq_intr_info->qid;
1714 hwq = &hba->uhq[qid];
1716 events = ufshcd_mcq_read_cqis(hba, qid);
1717 if (events)
1718 ufshcd_mcq_write_cqis(hba, events, qid);
1720 if (events & UFSHCD_MCQ_CQIS_TAIL_ENT_PUSH_STS)
1721 ufshcd_mcq_poll_cqe_lock(hba, hwq);
1723 return IRQ_HANDLED;
1726 static int ufs_mtk_config_mcq_irq(struct ufs_hba *hba)
1728 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
1729 u32 irq, i;
1730 int ret;
1732 for (i = 0; i < host->mcq_nr_intr; i++) {
1733 irq = host->mcq_intr_info[i].irq;
1734 if (irq == MTK_MCQ_INVALID_IRQ) {
1735 dev_err(hba->dev, "invalid irq. %d\n", i);
1736 return -ENOPARAM;
1739 host->mcq_intr_info[i].qid = i;
1740 ret = devm_request_irq(hba->dev, irq, ufs_mtk_mcq_intr, 0, UFSHCD,
1741 &host->mcq_intr_info[i]);
1743 dev_dbg(hba->dev, "request irq %d intr %s\n", irq, ret ? "failed" : "");
1745 if (ret) {
1746 dev_err(hba->dev, "Cannot request irq %d\n", ret);
1747 return ret;
1751 return 0;
1754 static int ufs_mtk_config_mcq(struct ufs_hba *hba, bool irq)
1756 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
1757 int ret = 0;
1759 if (!host->mcq_set_intr) {
1760 /* Disable irq option register */
1761 ufshcd_rmwl(hba, MCQ_INTR_EN_MSK, 0, REG_UFS_MMIO_OPT_CTRL_0);
1763 if (irq) {
1764 ret = ufs_mtk_config_mcq_irq(hba);
1765 if (ret)
1766 return ret;
1769 host->mcq_set_intr = true;
1772 ufshcd_rmwl(hba, MCQ_AH8, MCQ_AH8, REG_UFS_MMIO_OPT_CTRL_0);
1773 ufshcd_rmwl(hba, MCQ_INTR_EN_MSK, MCQ_MULTI_INTR_EN, REG_UFS_MMIO_OPT_CTRL_0);
1775 return 0;
1778 static int ufs_mtk_config_esi(struct ufs_hba *hba)
1780 return ufs_mtk_config_mcq(hba, true);
1783 static void ufs_mtk_config_scsi_dev(struct scsi_device *sdev)
1785 struct ufs_hba *hba = shost_priv(sdev->host);
1787 dev_dbg(hba->dev, "lu %llu scsi device configured", sdev->lun);
1788 if (sdev->lun == 2)
1789 blk_queue_flag_set(QUEUE_FLAG_SAME_FORCE, sdev->request_queue);
1793 * struct ufs_hba_mtk_vops - UFS MTK specific variant operations
1795 * The variant operations configure the necessary controller and PHY
1796 * handshake during initialization.
1798 static const struct ufs_hba_variant_ops ufs_hba_mtk_vops = {
1799 .name = "mediatek.ufshci",
1800 .max_num_rtt = MTK_MAX_NUM_RTT,
1801 .init = ufs_mtk_init,
1802 .get_ufs_hci_version = ufs_mtk_get_ufs_hci_version,
1803 .setup_clocks = ufs_mtk_setup_clocks,
1804 .hce_enable_notify = ufs_mtk_hce_enable_notify,
1805 .link_startup_notify = ufs_mtk_link_startup_notify,
1806 .pwr_change_notify = ufs_mtk_pwr_change_notify,
1807 .apply_dev_quirks = ufs_mtk_apply_dev_quirks,
1808 .fixup_dev_quirks = ufs_mtk_fixup_dev_quirks,
1809 .suspend = ufs_mtk_suspend,
1810 .resume = ufs_mtk_resume,
1811 .dbg_register_dump = ufs_mtk_dbg_register_dump,
1812 .device_reset = ufs_mtk_device_reset,
1813 .event_notify = ufs_mtk_event_notify,
1814 .config_scaling_param = ufs_mtk_config_scaling_param,
1815 .clk_scale_notify = ufs_mtk_clk_scale_notify,
1816 /* mcq vops */
1817 .get_hba_mac = ufs_mtk_get_hba_mac,
1818 .op_runtime_config = ufs_mtk_op_runtime_config,
1819 .mcq_config_resource = ufs_mtk_mcq_config_resource,
1820 .config_esi = ufs_mtk_config_esi,
1821 .config_scsi_dev = ufs_mtk_config_scsi_dev,
1825 * ufs_mtk_probe - probe routine of the driver
1826 * @pdev: pointer to Platform device handle
1828 * Return: zero for success and non-zero for failure.
1830 static int ufs_mtk_probe(struct platform_device *pdev)
1832 int err;
1833 struct device *dev = &pdev->dev;
1834 struct device_node *reset_node;
1835 struct platform_device *reset_pdev;
1836 struct device_link *link;
1838 reset_node = of_find_compatible_node(NULL, NULL,
1839 "ti,syscon-reset");
1840 if (!reset_node) {
1841 dev_notice(dev, "find ti,syscon-reset fail\n");
1842 goto skip_reset;
1844 reset_pdev = of_find_device_by_node(reset_node);
1845 if (!reset_pdev) {
1846 dev_notice(dev, "find reset_pdev fail\n");
1847 goto skip_reset;
1849 link = device_link_add(dev, &reset_pdev->dev,
1850 DL_FLAG_AUTOPROBE_CONSUMER);
1851 put_device(&reset_pdev->dev);
1852 if (!link) {
1853 dev_notice(dev, "add reset device_link fail\n");
1854 goto skip_reset;
1856 /* supplier is not probed */
1857 if (link->status == DL_STATE_DORMANT) {
1858 err = -EPROBE_DEFER;
1859 goto out;
1862 skip_reset:
1863 /* perform generic probe */
1864 err = ufshcd_pltfrm_init(pdev, &ufs_hba_mtk_vops);
1866 out:
1867 if (err)
1868 dev_err(dev, "probe failed %d\n", err);
1870 of_node_put(reset_node);
1871 return err;
1875 * ufs_mtk_remove - set driver_data of the device to NULL
1876 * @pdev: pointer to platform device handle
1878 * Always return 0
1880 static void ufs_mtk_remove(struct platform_device *pdev)
1882 struct ufs_hba *hba = platform_get_drvdata(pdev);
1884 pm_runtime_get_sync(&(pdev)->dev);
1885 ufshcd_remove(hba);
1888 #ifdef CONFIG_PM_SLEEP
1889 static int ufs_mtk_system_suspend(struct device *dev)
1891 struct ufs_hba *hba = dev_get_drvdata(dev);
1892 struct arm_smccc_res res;
1893 int ret;
1895 ret = ufshcd_system_suspend(dev);
1896 if (ret)
1897 return ret;
1899 ufs_mtk_dev_vreg_set_lpm(hba, true);
1901 if (ufs_mtk_is_rtff_mtcmos(hba))
1902 ufs_mtk_mtcmos_ctrl(false, res);
1904 return 0;
1907 static int ufs_mtk_system_resume(struct device *dev)
1909 struct ufs_hba *hba = dev_get_drvdata(dev);
1910 struct arm_smccc_res res;
1912 ufs_mtk_dev_vreg_set_lpm(hba, false);
1914 if (ufs_mtk_is_rtff_mtcmos(hba))
1915 ufs_mtk_mtcmos_ctrl(true, res);
1917 return ufshcd_system_resume(dev);
1919 #endif
1921 #ifdef CONFIG_PM
1922 static int ufs_mtk_runtime_suspend(struct device *dev)
1924 struct ufs_hba *hba = dev_get_drvdata(dev);
1925 struct arm_smccc_res res;
1926 int ret = 0;
1928 ret = ufshcd_runtime_suspend(dev);
1929 if (ret)
1930 return ret;
1932 ufs_mtk_dev_vreg_set_lpm(hba, true);
1934 if (ufs_mtk_is_rtff_mtcmos(hba))
1935 ufs_mtk_mtcmos_ctrl(false, res);
1937 return 0;
1940 static int ufs_mtk_runtime_resume(struct device *dev)
1942 struct ufs_hba *hba = dev_get_drvdata(dev);
1943 struct arm_smccc_res res;
1945 if (ufs_mtk_is_rtff_mtcmos(hba))
1946 ufs_mtk_mtcmos_ctrl(true, res);
1948 ufs_mtk_dev_vreg_set_lpm(hba, false);
1950 return ufshcd_runtime_resume(dev);
1952 #endif
1954 static const struct dev_pm_ops ufs_mtk_pm_ops = {
1955 SET_SYSTEM_SLEEP_PM_OPS(ufs_mtk_system_suspend,
1956 ufs_mtk_system_resume)
1957 SET_RUNTIME_PM_OPS(ufs_mtk_runtime_suspend,
1958 ufs_mtk_runtime_resume, NULL)
1959 .prepare = ufshcd_suspend_prepare,
1960 .complete = ufshcd_resume_complete,
1963 static struct platform_driver ufs_mtk_pltform = {
1964 .probe = ufs_mtk_probe,
1965 .remove = ufs_mtk_remove,
1966 .driver = {
1967 .name = "ufshcd-mtk",
1968 .pm = &ufs_mtk_pm_ops,
1969 .of_match_table = ufs_mtk_of_match,
1973 MODULE_AUTHOR("Stanley Chu <stanley.chu@mediatek.com>");
1974 MODULE_AUTHOR("Peter Wang <peter.wang@mediatek.com>");
1975 MODULE_DESCRIPTION("MediaTek UFS Host Driver");
1976 MODULE_LICENSE("GPL v2");
1978 module_platform_driver(ufs_mtk_pltform);