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/of_platform.h>
18 #include <linux/phy/phy.h>
19 #include <linux/platform_device.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/reset.h>
23 #include <ufs/ufshcd.h>
24 #include "ufshcd-pltfrm.h"
25 #include <ufs/ufs_quirks.h>
26 #include <ufs/unipro.h>
28 #include "ufs-mediatek.h"
29 #include "ufs-mediatek-sip.h"
31 static int ufs_mtk_config_mcq(struct ufs_hba
*hba
, bool irq
);
33 #define CREATE_TRACE_POINTS
34 #include "ufs-mediatek-trace.h"
35 #undef CREATE_TRACE_POINTS
37 #define MAX_SUPP_MAC 64
38 #define MCQ_QUEUE_OFFSET(c) ((((c) >> 16) & 0xFF) * 0x200)
40 static const struct ufs_dev_quirk ufs_mtk_dev_fixups
[] = {
41 { .wmanufacturerid
= UFS_ANY_VENDOR
,
42 .model
= UFS_ANY_MODEL
,
43 .quirk
= UFS_DEVICE_QUIRK_DELAY_AFTER_LPM
|
44 UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM
},
45 { .wmanufacturerid
= UFS_VENDOR_SKHYNIX
,
46 .model
= "H9HQ21AFAMZDAR",
47 .quirk
= UFS_DEVICE_QUIRK_SUPPORT_EXTENDED_FEATURES
},
51 static const struct of_device_id ufs_mtk_of_match
[] = {
52 { .compatible
= "mediatek,mt8183-ufshci" },
55 MODULE_DEVICE_TABLE(of
, ufs_mtk_of_match
);
58 * Details of UIC Errors
60 static const char *const ufs_uic_err_str
[] = {
64 "Transport Link Layer",
68 static const char *const ufs_uic_pa_err_str
[] = {
69 "PHY error on Lane 0",
70 "PHY error on Lane 1",
71 "PHY error on Lane 2",
72 "PHY error on Lane 3",
73 "Generic PHY Adapter Error. This should be the LINERESET indication"
76 static const char *const ufs_uic_dl_err_str
[] = {
78 "TCx_REPLAY_TIMER_EXPIRED",
79 "AFCx_REQUEST_TIMER_EXPIRED",
80 "FCx_PROTECTION_TIMER_EXPIRED",
83 "MAX_FRAME_LENGTH_EXCEEDED",
84 "WRONG_SEQUENCE_NUMBER",
85 "AFC_FRAME_SYNTAX_ERROR",
86 "NAC_FRAME_SYNTAX_ERROR",
89 "BAD_CTRL_SYMBOL_TYPE",
91 "PA_ERROR_IND_RECEIVED",
95 static bool ufs_mtk_is_boost_crypt_enabled(struct ufs_hba
*hba
)
97 struct ufs_mtk_host
*host
= ufshcd_get_variant(hba
);
99 return !!(host
->caps
& UFS_MTK_CAP_BOOST_CRYPT_ENGINE
);
102 static bool ufs_mtk_is_va09_supported(struct ufs_hba
*hba
)
104 struct ufs_mtk_host
*host
= ufshcd_get_variant(hba
);
106 return !!(host
->caps
& UFS_MTK_CAP_VA09_PWR_CTRL
);
109 static bool ufs_mtk_is_broken_vcc(struct ufs_hba
*hba
)
111 struct ufs_mtk_host
*host
= ufshcd_get_variant(hba
);
113 return !!(host
->caps
& UFS_MTK_CAP_BROKEN_VCC
);
116 static bool ufs_mtk_is_pmc_via_fastauto(struct ufs_hba
*hba
)
118 struct ufs_mtk_host
*host
= ufshcd_get_variant(hba
);
120 return !!(host
->caps
& UFS_MTK_CAP_PMC_VIA_FASTAUTO
);
123 static bool ufs_mtk_is_tx_skew_fix(struct ufs_hba
*hba
)
125 struct ufs_mtk_host
*host
= ufshcd_get_variant(hba
);
127 return (host
->caps
& UFS_MTK_CAP_TX_SKEW_FIX
);
130 static bool ufs_mtk_is_rtff_mtcmos(struct ufs_hba
*hba
)
132 struct ufs_mtk_host
*host
= ufshcd_get_variant(hba
);
134 return (host
->caps
& UFS_MTK_CAP_RTFF_MTCMOS
);
137 static bool ufs_mtk_is_allow_vccqx_lpm(struct ufs_hba
*hba
)
139 struct ufs_mtk_host
*host
= ufshcd_get_variant(hba
);
141 return (host
->caps
& UFS_MTK_CAP_ALLOW_VCCQX_LPM
);
144 static void ufs_mtk_cfg_unipro_cg(struct ufs_hba
*hba
, bool enable
)
150 UIC_ARG_MIB(VS_SAVEPOWERCONTROL
), &tmp
);
152 (1 << RX_SYMBOL_CLK_GATE_EN
) |
153 (1 << SYS_CLK_GATE_EN
) |
154 (1 << TX_CLK_GATE_EN
);
156 UIC_ARG_MIB(VS_SAVEPOWERCONTROL
), tmp
);
159 UIC_ARG_MIB(VS_DEBUGCLOCKENABLE
), &tmp
);
160 tmp
= tmp
& ~(1 << TX_SYMBOL_CLK_REQ_FORCE
);
162 UIC_ARG_MIB(VS_DEBUGCLOCKENABLE
), tmp
);
165 UIC_ARG_MIB(VS_SAVEPOWERCONTROL
), &tmp
);
166 tmp
= tmp
& ~((1 << RX_SYMBOL_CLK_GATE_EN
) |
167 (1 << SYS_CLK_GATE_EN
) |
168 (1 << TX_CLK_GATE_EN
));
170 UIC_ARG_MIB(VS_SAVEPOWERCONTROL
), tmp
);
173 UIC_ARG_MIB(VS_DEBUGCLOCKENABLE
), &tmp
);
174 tmp
= tmp
| (1 << TX_SYMBOL_CLK_REQ_FORCE
);
176 UIC_ARG_MIB(VS_DEBUGCLOCKENABLE
), tmp
);
180 static void ufs_mtk_crypto_enable(struct ufs_hba
*hba
)
182 struct arm_smccc_res res
;
184 ufs_mtk_crypto_ctrl(res
, 1);
186 dev_info(hba
->dev
, "%s: crypto enable failed, err: %lu\n",
188 hba
->caps
&= ~UFSHCD_CAP_CRYPTO
;
192 static void ufs_mtk_host_reset(struct ufs_hba
*hba
)
194 struct ufs_mtk_host
*host
= ufshcd_get_variant(hba
);
195 struct arm_smccc_res res
;
197 reset_control_assert(host
->hci_reset
);
198 reset_control_assert(host
->crypto_reset
);
199 reset_control_assert(host
->unipro_reset
);
200 reset_control_assert(host
->mphy_reset
);
202 usleep_range(100, 110);
204 reset_control_deassert(host
->unipro_reset
);
205 reset_control_deassert(host
->crypto_reset
);
206 reset_control_deassert(host
->hci_reset
);
207 reset_control_deassert(host
->mphy_reset
);
209 /* restore mphy setting aftre mphy reset */
210 if (host
->mphy_reset
)
211 ufs_mtk_mphy_ctrl(UFS_MPHY_RESTORE
, res
);
214 static void ufs_mtk_init_reset_control(struct ufs_hba
*hba
,
215 struct reset_control
**rc
,
218 *rc
= devm_reset_control_get(hba
->dev
, str
);
220 dev_info(hba
->dev
, "Failed to get reset control %s: %ld\n",
226 static void ufs_mtk_init_reset(struct ufs_hba
*hba
)
228 struct ufs_mtk_host
*host
= ufshcd_get_variant(hba
);
230 ufs_mtk_init_reset_control(hba
, &host
->hci_reset
,
232 ufs_mtk_init_reset_control(hba
, &host
->unipro_reset
,
234 ufs_mtk_init_reset_control(hba
, &host
->crypto_reset
,
236 ufs_mtk_init_reset_control(hba
, &host
->mphy_reset
,
240 static int ufs_mtk_hce_enable_notify(struct ufs_hba
*hba
,
241 enum ufs_notify_change_status status
)
243 struct ufs_mtk_host
*host
= ufshcd_get_variant(hba
);
245 if (status
== PRE_CHANGE
) {
246 if (host
->unipro_lpm
) {
247 hba
->vps
->hba_enable_delay_us
= 0;
249 hba
->vps
->hba_enable_delay_us
= 600;
250 ufs_mtk_host_reset(hba
);
253 if (hba
->caps
& UFSHCD_CAP_CRYPTO
)
254 ufs_mtk_crypto_enable(hba
);
256 if (host
->caps
& UFS_MTK_CAP_DISABLE_AH8
) {
257 ufshcd_writel(hba
, 0,
258 REG_AUTO_HIBERNATE_IDLE_TIMER
);
259 hba
->capabilities
&= ~MASK_AUTO_HIBERN8_SUPPORT
;
264 * Turn on CLK_CG early to bypass abnormal ERR_CHK signal
265 * to prevent host hang issue
268 ufshcd_readl(hba
, REG_UFS_XOUFS_CTRL
) | 0x80,
275 static int ufs_mtk_bind_mphy(struct ufs_hba
*hba
)
277 struct ufs_mtk_host
*host
= ufshcd_get_variant(hba
);
278 struct device
*dev
= hba
->dev
;
279 struct device_node
*np
= dev
->of_node
;
282 host
->mphy
= devm_of_phy_get_by_index(dev
, np
, 0);
284 if (host
->mphy
== ERR_PTR(-EPROBE_DEFER
)) {
286 * UFS driver might be probed before the phy driver does.
287 * In that case we would like to return EPROBE_DEFER code.
291 "%s: required phy hasn't probed yet. err = %d\n",
293 } else if (IS_ERR(host
->mphy
)) {
294 err
= PTR_ERR(host
->mphy
);
295 if (err
!= -ENODEV
) {
296 dev_info(dev
, "%s: PHY get failed %d\n", __func__
,
304 * Allow unbound mphy because not every platform needs specific
313 static int ufs_mtk_setup_ref_clk(struct ufs_hba
*hba
, bool on
)
315 struct ufs_mtk_host
*host
= ufshcd_get_variant(hba
);
316 struct arm_smccc_res res
;
317 ktime_t timeout
, time_checked
;
320 if (host
->ref_clk_enabled
== on
)
323 ufs_mtk_ref_clk_notify(on
, PRE_CHANGE
, res
);
326 ufshcd_writel(hba
, REFCLK_REQUEST
, REG_UFS_REFCLK_CTRL
);
328 ufshcd_delay_us(host
->ref_clk_gating_wait_us
, 10);
329 ufshcd_writel(hba
, REFCLK_RELEASE
, REG_UFS_REFCLK_CTRL
);
333 timeout
= ktime_add_us(ktime_get(), REFCLK_REQ_TIMEOUT_US
);
335 time_checked
= ktime_get();
336 value
= ufshcd_readl(hba
, REG_UFS_REFCLK_CTRL
);
338 /* Wait until ack bit equals to req bit */
339 if (((value
& REFCLK_ACK
) >> 1) == (value
& REFCLK_REQUEST
))
342 usleep_range(100, 200);
343 } while (ktime_before(time_checked
, timeout
));
345 dev_err(hba
->dev
, "missing ack of refclk req, reg: 0x%x\n", value
);
347 ufs_mtk_ref_clk_notify(host
->ref_clk_enabled
, POST_CHANGE
, res
);
352 host
->ref_clk_enabled
= on
;
354 ufshcd_delay_us(host
->ref_clk_ungating_wait_us
, 10);
356 ufs_mtk_ref_clk_notify(on
, POST_CHANGE
, res
);
361 static void ufs_mtk_setup_ref_clk_wait_us(struct ufs_hba
*hba
,
364 struct ufs_mtk_host
*host
= ufshcd_get_variant(hba
);
366 if (hba
->dev_info
.clk_gating_wait_us
) {
367 host
->ref_clk_gating_wait_us
=
368 hba
->dev_info
.clk_gating_wait_us
;
370 host
->ref_clk_gating_wait_us
= gating_us
;
373 host
->ref_clk_ungating_wait_us
= REFCLK_DEFAULT_WAIT_US
;
376 static void ufs_mtk_dbg_sel(struct ufs_hba
*hba
)
378 struct ufs_mtk_host
*host
= ufshcd_get_variant(hba
);
380 if (((host
->ip_ver
>> 16) & 0xFF) >= 0x36) {
381 ufshcd_writel(hba
, 0x820820, REG_UFS_DEBUG_SEL
);
382 ufshcd_writel(hba
, 0x0, REG_UFS_DEBUG_SEL_B0
);
383 ufshcd_writel(hba
, 0x55555555, REG_UFS_DEBUG_SEL_B1
);
384 ufshcd_writel(hba
, 0xaaaaaaaa, REG_UFS_DEBUG_SEL_B2
);
385 ufshcd_writel(hba
, 0xffffffff, REG_UFS_DEBUG_SEL_B3
);
387 ufshcd_writel(hba
, 0x20, REG_UFS_DEBUG_SEL
);
391 static void ufs_mtk_wait_idle_state(struct ufs_hba
*hba
,
392 unsigned long retry_ms
)
394 u64 timeout
, time_checked
;
398 /* cannot use plain ktime_get() in suspend */
399 timeout
= ktime_get_mono_fast_ns() + retry_ms
* 1000000UL;
401 /* wait a specific time after check base */
406 time_checked
= ktime_get_mono_fast_ns();
407 ufs_mtk_dbg_sel(hba
);
408 val
= ufshcd_readl(hba
, REG_UFS_PROBE
);
413 * if state is in H8 enter and H8 enter confirm
414 * wait until return to idle state.
416 if ((sm
>= VS_HIB_ENTER
) && (sm
<= VS_HIB_EXIT
)) {
420 } else if (!wait_idle
)
423 if (wait_idle
&& (sm
== VS_HCE_BASE
))
425 } while (time_checked
< timeout
);
427 if (wait_idle
&& sm
!= VS_HCE_BASE
)
428 dev_info(hba
->dev
, "wait idle tmo: 0x%x\n", val
);
431 static int ufs_mtk_wait_link_state(struct ufs_hba
*hba
, u32 state
,
432 unsigned long max_wait_ms
)
434 ktime_t timeout
, time_checked
;
437 timeout
= ktime_add_ms(ktime_get(), max_wait_ms
);
439 time_checked
= ktime_get();
440 ufs_mtk_dbg_sel(hba
);
441 val
= ufshcd_readl(hba
, REG_UFS_PROBE
);
447 /* Sleep for max. 200us */
448 usleep_range(100, 200);
449 } while (ktime_before(time_checked
, timeout
));
454 static int ufs_mtk_mphy_power_on(struct ufs_hba
*hba
, bool on
)
456 struct ufs_mtk_host
*host
= ufshcd_get_variant(hba
);
457 struct phy
*mphy
= host
->mphy
;
458 struct arm_smccc_res res
;
461 if (!mphy
|| !(on
^ host
->mphy_powered_on
))
465 if (ufs_mtk_is_va09_supported(hba
)) {
466 ret
= regulator_enable(host
->reg_va09
);
469 /* wait 200 us to stablize VA09 */
470 usleep_range(200, 210);
471 ufs_mtk_va09_pwr_ctrl(res
, 1);
476 if (ufs_mtk_is_va09_supported(hba
)) {
477 ufs_mtk_va09_pwr_ctrl(res
, 0);
478 ret
= regulator_disable(host
->reg_va09
);
484 "failed to %s va09: %d\n",
485 on
? "enable" : "disable",
488 host
->mphy_powered_on
= on
;
494 static int ufs_mtk_get_host_clk(struct device
*dev
, const char *name
,
495 struct clk
**clk_out
)
500 clk
= devm_clk_get(dev
, name
);
509 static void ufs_mtk_boost_crypt(struct ufs_hba
*hba
, bool boost
)
511 struct ufs_mtk_host
*host
= ufshcd_get_variant(hba
);
512 struct ufs_mtk_crypt_cfg
*cfg
;
513 struct regulator
*reg
;
516 if (!ufs_mtk_is_boost_crypt_enabled(hba
))
520 volt
= cfg
->vcore_volt
;
521 reg
= cfg
->reg_vcore
;
523 ret
= clk_prepare_enable(cfg
->clk_crypt_mux
);
525 dev_info(hba
->dev
, "clk_prepare_enable(): %d\n",
531 ret
= regulator_set_voltage(reg
, volt
, INT_MAX
);
534 "failed to set vcore to %d\n", volt
);
538 ret
= clk_set_parent(cfg
->clk_crypt_mux
,
539 cfg
->clk_crypt_perf
);
542 "failed to set clk_crypt_perf\n");
543 regulator_set_voltage(reg
, 0, INT_MAX
);
547 ret
= clk_set_parent(cfg
->clk_crypt_mux
,
551 "failed to set clk_crypt_lp\n");
555 ret
= regulator_set_voltage(reg
, 0, INT_MAX
);
558 "failed to set vcore to MIN\n");
562 clk_disable_unprepare(cfg
->clk_crypt_mux
);
565 static int ufs_mtk_init_host_clk(struct ufs_hba
*hba
, const char *name
,
570 ret
= ufs_mtk_get_host_clk(hba
->dev
, name
, clk
);
572 dev_info(hba
->dev
, "%s: failed to get %s: %d", __func__
,
579 static void ufs_mtk_init_boost_crypt(struct ufs_hba
*hba
)
581 struct ufs_mtk_host
*host
= ufshcd_get_variant(hba
);
582 struct ufs_mtk_crypt_cfg
*cfg
;
583 struct device
*dev
= hba
->dev
;
584 struct regulator
*reg
;
587 host
->crypt
= devm_kzalloc(dev
, sizeof(*(host
->crypt
)),
592 reg
= devm_regulator_get_optional(dev
, "dvfsrc-vcore");
594 dev_info(dev
, "failed to get dvfsrc-vcore: %ld",
599 if (of_property_read_u32(dev
->of_node
, "boost-crypt-vcore-min",
601 dev_info(dev
, "failed to get boost-crypt-vcore-min");
606 if (ufs_mtk_init_host_clk(hba
, "crypt_mux",
607 &cfg
->clk_crypt_mux
))
610 if (ufs_mtk_init_host_clk(hba
, "crypt_lp",
614 if (ufs_mtk_init_host_clk(hba
, "crypt_perf",
615 &cfg
->clk_crypt_perf
))
618 cfg
->reg_vcore
= reg
;
619 cfg
->vcore_volt
= volt
;
620 host
->caps
|= UFS_MTK_CAP_BOOST_CRYPT_ENGINE
;
626 static void ufs_mtk_init_va09_pwr_ctrl(struct ufs_hba
*hba
)
628 struct ufs_mtk_host
*host
= ufshcd_get_variant(hba
);
630 host
->reg_va09
= regulator_get(hba
->dev
, "va09");
631 if (IS_ERR(host
->reg_va09
))
632 dev_info(hba
->dev
, "failed to get va09");
634 host
->caps
|= UFS_MTK_CAP_VA09_PWR_CTRL
;
637 static void ufs_mtk_init_host_caps(struct ufs_hba
*hba
)
639 struct ufs_mtk_host
*host
= ufshcd_get_variant(hba
);
640 struct device_node
*np
= hba
->dev
->of_node
;
642 if (of_property_read_bool(np
, "mediatek,ufs-boost-crypt"))
643 ufs_mtk_init_boost_crypt(hba
);
645 if (of_property_read_bool(np
, "mediatek,ufs-support-va09"))
646 ufs_mtk_init_va09_pwr_ctrl(hba
);
648 if (of_property_read_bool(np
, "mediatek,ufs-disable-ah8"))
649 host
->caps
|= UFS_MTK_CAP_DISABLE_AH8
;
651 if (of_property_read_bool(np
, "mediatek,ufs-broken-vcc"))
652 host
->caps
|= UFS_MTK_CAP_BROKEN_VCC
;
654 if (of_property_read_bool(np
, "mediatek,ufs-pmc-via-fastauto"))
655 host
->caps
|= UFS_MTK_CAP_PMC_VIA_FASTAUTO
;
657 if (of_property_read_bool(np
, "mediatek,ufs-tx-skew-fix"))
658 host
->caps
|= UFS_MTK_CAP_TX_SKEW_FIX
;
660 if (of_property_read_bool(np
, "mediatek,ufs-disable-mcq"))
661 host
->caps
|= UFS_MTK_CAP_DISABLE_MCQ
;
663 if (of_property_read_bool(np
, "mediatek,ufs-rtff-mtcmos"))
664 host
->caps
|= UFS_MTK_CAP_RTFF_MTCMOS
;
666 dev_info(hba
->dev
, "caps: 0x%x", host
->caps
);
669 static void ufs_mtk_scale_perf(struct ufs_hba
*hba
, bool scale_up
)
671 ufs_mtk_boost_crypt(hba
, scale_up
);
674 static void ufs_mtk_pwr_ctrl(struct ufs_hba
*hba
, bool on
)
676 struct ufs_mtk_host
*host
= ufshcd_get_variant(hba
);
679 phy_power_on(host
->mphy
);
680 ufs_mtk_setup_ref_clk(hba
, on
);
681 if (!ufshcd_is_clkscaling_supported(hba
))
682 ufs_mtk_scale_perf(hba
, on
);
684 if (!ufshcd_is_clkscaling_supported(hba
))
685 ufs_mtk_scale_perf(hba
, on
);
686 ufs_mtk_setup_ref_clk(hba
, on
);
687 phy_power_off(host
->mphy
);
691 static void ufs_mtk_mcq_disable_irq(struct ufs_hba
*hba
)
693 struct ufs_mtk_host
*host
= ufshcd_get_variant(hba
);
696 if (!hba
->mcq_enabled
)
699 if (host
->mcq_nr_intr
== 0)
702 for (i
= 0; i
< host
->mcq_nr_intr
; i
++) {
703 irq
= host
->mcq_intr_info
[i
].irq
;
706 host
->is_mcq_intr_enabled
= false;
709 static void ufs_mtk_mcq_enable_irq(struct ufs_hba
*hba
)
711 struct ufs_mtk_host
*host
= ufshcd_get_variant(hba
);
714 if (!hba
->mcq_enabled
)
717 if (host
->mcq_nr_intr
== 0)
720 if (host
->is_mcq_intr_enabled
== true)
723 for (i
= 0; i
< host
->mcq_nr_intr
; i
++) {
724 irq
= host
->mcq_intr_info
[i
].irq
;
727 host
->is_mcq_intr_enabled
= true;
731 * ufs_mtk_setup_clocks - enables/disable clocks
732 * @hba: host controller instance
733 * @on: If true, enable clocks else disable them.
734 * @status: PRE_CHANGE or POST_CHANGE notify
736 * Return: 0 on success, non-zero on failure.
738 static int ufs_mtk_setup_clocks(struct ufs_hba
*hba
, bool on
,
739 enum ufs_notify_change_status status
)
741 struct ufs_mtk_host
*host
= ufshcd_get_variant(hba
);
742 bool clk_pwr_off
= false;
746 * In case ufs_mtk_init() is not yet done, simply ignore.
747 * This ufs_mtk_setup_clocks() shall be called from
748 * ufs_mtk_init() after init is done.
753 if (!on
&& status
== PRE_CHANGE
) {
754 if (ufshcd_is_link_off(hba
)) {
756 } else if (ufshcd_is_link_hibern8(hba
) ||
757 (!ufshcd_can_hibern8_during_gating(hba
) &&
758 ufshcd_is_auto_hibern8_enabled(hba
))) {
760 * Gate ref-clk and poweroff mphy if link state is in
761 * OFF or Hibern8 by either Auto-Hibern8 or
762 * ufshcd_link_state_transition().
764 ret
= ufs_mtk_wait_link_state(hba
,
772 ufs_mtk_pwr_ctrl(hba
, false);
773 ufs_mtk_mcq_disable_irq(hba
);
774 } else if (on
&& status
== POST_CHANGE
) {
775 ufs_mtk_pwr_ctrl(hba
, true);
776 ufs_mtk_mcq_enable_irq(hba
);
782 static void ufs_mtk_get_controller_version(struct ufs_hba
*hba
)
784 struct ufs_mtk_host
*host
= ufshcd_get_variant(hba
);
787 if (host
->hw_ver
.major
)
790 /* Set default (minimum) version anyway */
791 host
->hw_ver
.major
= 2;
793 ret
= ufshcd_dme_get(hba
, UIC_ARG_MIB(PA_LOCALVERINFO
), &ver
);
795 if (ver
>= UFS_UNIPRO_VER_1_8
) {
796 host
->hw_ver
.major
= 3;
798 * Fix HCI version for some platforms with
801 if (hba
->ufs_version
< ufshci_version(3, 0))
802 hba
->ufs_version
= ufshci_version(3, 0);
807 static u32
ufs_mtk_get_ufs_hci_version(struct ufs_hba
*hba
)
809 return hba
->ufs_version
;
813 * ufs_mtk_init_clocks - Init mtk driver private clocks
815 * @hba: per adapter instance
817 static void ufs_mtk_init_clocks(struct ufs_hba
*hba
)
819 struct ufs_mtk_host
*host
= ufshcd_get_variant(hba
);
820 struct list_head
*head
= &hba
->clk_list_head
;
821 struct ufs_mtk_clk
*mclk
= &host
->mclk
;
822 struct ufs_clk_info
*clki
, *clki_tmp
;
825 * Find private clocks and store them in struct ufs_mtk_clk.
826 * Remove "ufs_sel_min_src" and "ufs_sel_min_src" from list to avoid
827 * being switched on/off in clock gating.
829 list_for_each_entry_safe(clki
, clki_tmp
, head
, list
) {
830 if (!strcmp(clki
->name
, "ufs_sel")) {
831 host
->mclk
.ufs_sel_clki
= clki
;
832 } else if (!strcmp(clki
->name
, "ufs_sel_max_src")) {
833 host
->mclk
.ufs_sel_max_clki
= clki
;
834 clk_disable_unprepare(clki
->clk
);
835 list_del(&clki
->list
);
836 } else if (!strcmp(clki
->name
, "ufs_sel_min_src")) {
837 host
->mclk
.ufs_sel_min_clki
= clki
;
838 clk_disable_unprepare(clki
->clk
);
839 list_del(&clki
->list
);
843 if (!mclk
->ufs_sel_clki
|| !mclk
->ufs_sel_max_clki
||
844 !mclk
->ufs_sel_min_clki
) {
845 hba
->caps
&= ~UFSHCD_CAP_CLK_SCALING
;
847 "%s: Clk-scaling not ready. Feature disabled.",
852 #define MAX_VCC_NAME 30
853 static int ufs_mtk_vreg_fix_vcc(struct ufs_hba
*hba
)
855 struct ufs_vreg_info
*info
= &hba
->vreg_info
;
856 struct device_node
*np
= hba
->dev
->of_node
;
857 struct device
*dev
= hba
->dev
;
858 char vcc_name
[MAX_VCC_NAME
];
859 struct arm_smccc_res res
;
862 if (hba
->vreg_info
.vcc
)
865 if (of_property_read_bool(np
, "mediatek,ufs-vcc-by-num")) {
866 ufs_mtk_get_vcc_num(res
);
867 if (res
.a1
> UFS_VCC_NONE
&& res
.a1
< UFS_VCC_MAX
)
868 snprintf(vcc_name
, MAX_VCC_NAME
, "vcc-opt%lu", res
.a1
);
871 } else if (of_property_read_bool(np
, "mediatek,ufs-vcc-by-ver")) {
872 ver
= (hba
->dev_info
.wspecversion
& 0xF00) >> 8;
873 snprintf(vcc_name
, MAX_VCC_NAME
, "vcc-ufs%u", ver
);
878 err
= ufshcd_populate_vreg(dev
, vcc_name
, &info
->vcc
, false);
882 err
= ufshcd_get_vreg(dev
, info
->vcc
);
886 err
= regulator_enable(info
->vcc
->reg
);
888 info
->vcc
->enabled
= true;
889 dev_info(dev
, "%s: %s enabled\n", __func__
, vcc_name
);
895 static void ufs_mtk_vreg_fix_vccqx(struct ufs_hba
*hba
)
897 struct ufs_vreg_info
*info
= &hba
->vreg_info
;
898 struct ufs_vreg
**vreg_on
, **vreg_off
;
900 if (hba
->dev_info
.wspecversion
>= 0x0300) {
901 vreg_on
= &info
->vccq
;
902 vreg_off
= &info
->vccq2
;
904 vreg_on
= &info
->vccq2
;
905 vreg_off
= &info
->vccq
;
909 (*vreg_on
)->always_on
= true;
912 regulator_disable((*vreg_off
)->reg
);
913 devm_kfree(hba
->dev
, (*vreg_off
)->name
);
914 devm_kfree(hba
->dev
, *vreg_off
);
919 static void ufs_mtk_init_mcq_irq(struct ufs_hba
*hba
)
921 struct ufs_mtk_host
*host
= ufshcd_get_variant(hba
);
922 struct platform_device
*pdev
;
926 host
->mcq_nr_intr
= UFSHCD_MAX_Q_NR
;
927 pdev
= container_of(hba
->dev
, struct platform_device
, dev
);
929 if (host
->caps
& UFS_MTK_CAP_DISABLE_MCQ
)
932 for (i
= 0; i
< host
->mcq_nr_intr
; i
++) {
933 /* irq index 0 is legacy irq, sq/cq irq start from index 1 */
934 irq
= platform_get_irq(pdev
, i
+ 1);
936 host
->mcq_intr_info
[i
].irq
= MTK_MCQ_INVALID_IRQ
;
939 host
->mcq_intr_info
[i
].hba
= hba
;
940 host
->mcq_intr_info
[i
].irq
= irq
;
941 dev_info(hba
->dev
, "get platform mcq irq: %d, %d\n", i
, irq
);
946 /* invalidate irq info */
947 for (i
= 0; i
< host
->mcq_nr_intr
; i
++)
948 host
->mcq_intr_info
[i
].irq
= MTK_MCQ_INVALID_IRQ
;
950 host
->mcq_nr_intr
= 0;
954 * ufs_mtk_init - find other essential mmio bases
955 * @hba: host controller instance
957 * Binds PHY with controller and powers up PHY enabling clocks
960 * Return: -EPROBE_DEFER if binding fails, returns negative error
961 * on phy power up failure and returns zero on success.
963 static int ufs_mtk_init(struct ufs_hba
*hba
)
965 const struct of_device_id
*id
;
966 struct device
*dev
= hba
->dev
;
967 struct ufs_mtk_host
*host
;
968 struct Scsi_Host
*shost
= hba
->host
;
970 struct arm_smccc_res res
;
972 host
= devm_kzalloc(dev
, sizeof(*host
), GFP_KERNEL
);
975 dev_info(dev
, "%s: no memory for mtk ufs host\n", __func__
);
980 ufshcd_set_variant(hba
, host
);
982 id
= of_match_device(ufs_mtk_of_match
, dev
);
988 /* Initialize host capability */
989 ufs_mtk_init_host_caps(hba
);
991 ufs_mtk_init_mcq_irq(hba
);
993 err
= ufs_mtk_bind_mphy(hba
);
995 goto out_variant_clear
;
997 ufs_mtk_init_reset(hba
);
999 /* backup mphy setting if mphy can reset */
1000 if (host
->mphy_reset
)
1001 ufs_mtk_mphy_ctrl(UFS_MPHY_BACKUP
, res
);
1003 /* Enable runtime autosuspend */
1004 hba
->caps
|= UFSHCD_CAP_RPM_AUTOSUSPEND
;
1006 /* Enable clock-gating */
1007 hba
->caps
|= UFSHCD_CAP_CLK_GATING
;
1009 /* Enable inline encryption */
1010 hba
->caps
|= UFSHCD_CAP_CRYPTO
;
1012 /* Enable WriteBooster */
1013 hba
->caps
|= UFSHCD_CAP_WB_EN
;
1015 /* Enable clk scaling*/
1016 hba
->caps
|= UFSHCD_CAP_CLK_SCALING
;
1018 /* Set runtime pm delay to replace default */
1019 shost
->rpm_autosuspend_delay
= MTK_RPM_AUTOSUSPEND_DELAY_MS
;
1021 hba
->quirks
|= UFSHCI_QUIRK_SKIP_MANUAL_WB_FLUSH_CTRL
;
1022 hba
->quirks
|= UFSHCD_QUIRK_MCQ_BROKEN_INTR
;
1023 hba
->quirks
|= UFSHCD_QUIRK_MCQ_BROKEN_RTC
;
1024 hba
->vps
->wb_flush_threshold
= UFS_WB_BUF_REMAIN_PERCENT(80);
1026 if (host
->caps
& UFS_MTK_CAP_DISABLE_AH8
)
1027 hba
->caps
|= UFSHCD_CAP_HIBERN8_WITH_CLK_GATING
;
1029 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
);
1058 ufshcd_set_variant(hba
, NULL
);
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
))
1069 if (dev_req_params
->hs_rate
== hba
->pwr_info
.hs_rate
)
1072 if (dev_req_params
->pwr_tx
!= FAST_MODE
&&
1073 dev_req_params
->gear_tx
< UFS_HS_G4
)
1076 if (dev_req_params
->pwr_rx
!= FAST_MODE
&&
1077 dev_req_params
->gear_rx
< UFS_HS_G4
)
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
;
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
);
1097 pr_info("%s: failed to determine capabilities\n",
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
),
1118 ret
= ufshcd_uic_change_pwr_mode(hba
,
1119 FASTAUTO_MODE
<< 4 | FASTAUTO_MODE
);
1122 dev_err(hba
->dev
, "%s: HSG1B FASTAUTO failed ret=%d\n",
1127 if (host
->hw_ver
.major
>= 3) {
1128 ret
= ufshcd_dme_configure_adapt(hba
,
1129 dev_req_params
->gear_tx
,
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
)
1145 ret
= ufs_mtk_pre_pwr_change(hba
, dev_max_params
,
1158 static int ufs_mtk_unipro_set_lpm(struct ufs_hba
*hba
, bool lpm
)
1161 struct ufs_mtk_host
*host
= ufshcd_get_variant(hba
);
1163 ret
= ufshcd_dme_set(hba
,
1164 UIC_ARG_MIB_SEL(VS_UNIPROPOWERDOWNCONTROL
, 0),
1168 * Forcibly set as non-LPM mode if UIC commands is failed
1169 * to use default hba_enable_delay_us value for re-enabling
1172 host
->unipro_lpm
= lpm
;
1178 static int ufs_mtk_pre_link(struct ufs_hba
*hba
)
1183 ufs_mtk_get_controller_version(hba
);
1185 ret
= ufs_mtk_unipro_set_lpm(hba
, false);
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
);
1198 /* disable deep stall */
1199 ret
= ufshcd_dme_get(hba
, UIC_ARG_MIB(VS_SAVEPOWERCONTROL
), &tmp
);
1205 ret
= ufshcd_dme_set(hba
, UIC_ARG_MIB(VS_SAVEPOWERCONTROL
), tmp
);
1210 static void ufs_mtk_setup_clk_gating(struct ufs_hba
*hba
)
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
,
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
)
1244 ret
= ufs_mtk_pre_link(hba
);
1247 ufs_mtk_post_link(hba
);
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
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");
1285 static int ufs_mtk_link_set_hpm(struct ufs_hba
*hba
)
1289 err
= ufshcd_hba_enable(hba
);
1293 err
= ufs_mtk_unipro_set_lpm(hba
, false);
1297 err
= ufshcd_uic_hibern8_exit(hba
);
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);
1305 dev_warn(hba
->dev
, "exit h8 state fail, err=%d\n", err
);
1308 ufshcd_set_link_active(hba
);
1310 err
= ufshcd_make_hba_operational(hba
);
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
);
1324 static int ufs_mtk_link_set_lpm(struct ufs_hba
*hba
)
1328 /* Disable reset confirm feature by UniPro */
1330 (ufshcd_readl(hba
, REG_UFS_XOUFS_CTRL
) & ~0x100),
1331 REG_UFS_XOUFS_CTRL
);
1333 err
= ufs_mtk_unipro_set_lpm(hba
, true);
1335 /* Resume UniPro state for following error recovery */
1336 ufs_mtk_unipro_set_lpm(hba
, false);
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
;
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
,
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
))
1373 /* Skip vccqx lpm control and control vsx only */
1374 if (!hba
->vreg_info
.vccq
&& !hba
->vreg_info
.vccq2
)
1377 /* VCC is always-on, control vsx only */
1378 if (!hba
->vreg_info
.vcc
)
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
))
1386 else /* control vsx only */
1392 ufs_mtk_vccqx_set_lpm(hba
, lpm
);
1393 ufs_mtk_vsx_set_lpm(hba
, lpm
);
1395 ufs_mtk_vsx_set_lpm(hba
, lpm
);
1397 ufs_mtk_vccqx_set_lpm(hba
, lpm
);
1401 static void ufs_mtk_auto_hibern8_disable(struct ufs_hba
*hba
)
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);
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
)
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
);
1428 if (ufshcd_is_link_hibern8(hba
)) {
1429 err
= ufs_mtk_link_set_lpm(hba
);
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);
1445 if (ufshcd_is_link_off(hba
))
1446 ufs_mtk_device_reset_ctrl(0, res
);
1448 ufs_mtk_sram_pwr_ctrl(false, res
);
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
);
1461 static int ufs_mtk_resume(struct ufs_hba
*hba
, enum ufs_pm_op pm_op
)
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);
1475 if (ufshcd_is_link_hibern8(hba
)) {
1476 err
= ufs_mtk_link_set_hpm(hba
);
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'
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);
1537 ufs_mtk_setup_ref_clk_wait_us(hba
,
1538 REFCLK_DEFAULT_WAIT_US
);
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
;
1568 trace_ufs_mtk_event(evt
, val
);
1570 /* Print details of UIC Errors */
1571 if (evt
<= UFS_EVT_DME_ERR
) {
1573 "Host UIC Error Code (%s): %08x\n",
1574 ufs_uic_err_str
[evt
], val
);
1578 if (evt
== UFS_EVT_PA_ERR
) {
1579 for_each_set_bit(bit
, ®
, 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
, ®
, 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
;
1620 ret
= clk_prepare_enable(clki
->clk
);
1623 "clk_prepare_enable() fail, ret: %d\n", ret
);
1628 ret
= clk_set_parent(clki
->clk
, mclk
->ufs_sel_max_clki
->clk
);
1629 clki
->curr_freq
= clki
->max_freq
;
1631 ret
= clk_set_parent(clki
->clk
, mclk
->ufs_sel_min_clki
->clk
);
1632 clki
->curr_freq
= clki
->min_freq
;
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
))
1651 if (status
== PRE_CHANGE
) {
1652 /* Switch parent before clk_set_rate() */
1653 ufs_mtk_clk_scale(hba
, scale_up
);
1655 /* Request interrupt latency QoS accordingly */
1656 ufs_mtk_scale_perf(hba
, scale_up
);
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
)
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
;
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
;
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.");
1702 hba
->mcq_base
= hba
->mmio_base
+ MCQ_QUEUE_OFFSET(hba
->mcq_capabilities
);
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
;
1712 int qid
= mcq_intr_info
->qid
;
1714 hwq
= &hba
->uhq
[qid
];
1716 events
= ufshcd_mcq_read_cqis(hba
, qid
);
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
);
1726 static int ufs_mtk_config_mcq_irq(struct ufs_hba
*hba
)
1728 struct ufs_mtk_host
*host
= ufshcd_get_variant(hba
);
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
);
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" : "");
1746 dev_err(hba
->dev
, "Cannot request irq %d\n", ret
);
1754 static int ufs_mtk_config_mcq(struct ufs_hba
*hba
, bool irq
)
1756 struct ufs_mtk_host
*host
= ufshcd_get_variant(hba
);
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
);
1764 ret
= ufs_mtk_config_mcq_irq(hba
);
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
);
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
);
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
,
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
)
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
,
1841 dev_notice(dev
, "find ti,syscon-reset fail\n");
1844 reset_pdev
= of_find_device_by_node(reset_node
);
1846 dev_notice(dev
, "find reset_pdev fail\n");
1849 link
= device_link_add(dev
, &reset_pdev
->dev
,
1850 DL_FLAG_AUTOPROBE_CONSUMER
);
1851 put_device(&reset_pdev
->dev
);
1853 dev_notice(dev
, "add reset device_link fail\n");
1856 /* supplier is not probed */
1857 if (link
->status
== DL_STATE_DORMANT
) {
1858 err
= -EPROBE_DEFER
;
1863 /* perform generic probe */
1864 err
= ufshcd_pltfrm_init(pdev
, &ufs_hba_mtk_vops
);
1868 dev_err(dev
, "probe failed %d\n", err
);
1870 of_node_put(reset_node
);
1875 * ufs_mtk_remove - set driver_data of the device to NULL
1876 * @pdev: pointer to platform device handle
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
);
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
;
1895 ret
= ufshcd_system_suspend(dev
);
1899 ufs_mtk_dev_vreg_set_lpm(hba
, true);
1901 if (ufs_mtk_is_rtff_mtcmos(hba
))
1902 ufs_mtk_mtcmos_ctrl(false, res
);
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
);
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
;
1928 ret
= ufshcd_runtime_suspend(dev
);
1932 ufs_mtk_dev_vreg_set_lpm(hba
, true);
1934 if (ufs_mtk_is_rtff_mtcmos(hba
))
1935 ufs_mtk_mtcmos_ctrl(false, res
);
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
);
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
,
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
);