xref: /linux/drivers/ufs/host/ufs-mediatek.c (revision 4f069ab1ec243e6b60dc438772905855c60774e8)
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  */
8 
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>
22 
23 #include <ufs/ufshcd.h>
24 #include "ufshcd-pltfrm.h"
25 #include <ufs/ufs_quirks.h>
26 #include <ufs/unipro.h>
27 
28 #include "ufs-mediatek.h"
29 #include "ufs-mediatek-sip.h"
30 
31 static int  ufs_mtk_config_mcq(struct ufs_hba *hba, bool irq);
32 static void _ufs_mtk_clk_scale(struct ufs_hba *hba, bool scale_up);
33 
34 #define CREATE_TRACE_POINTS
35 #include "ufs-mediatek-trace.h"
36 #undef CREATE_TRACE_POINTS
37 
38 #define MAX_SUPP_MAC 64
39 #define MCQ_QUEUE_OFFSET(c) ((((c) >> 16) & 0xFF) * 0x200)
40 
41 static const struct ufs_dev_quirk ufs_mtk_dev_fixups[] = {
42 	{ .wmanufacturerid = UFS_ANY_VENDOR,
43 	  .model = UFS_ANY_MODEL,
44 	  .quirk = UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM },
45 	{ .wmanufacturerid = UFS_VENDOR_SKHYNIX,
46 	  .model = "H9HQ21AFAMZDAR",
47 	  .quirk = UFS_DEVICE_QUIRK_SUPPORT_EXTENDED_FEATURES },
48 	{}
49 };
50 
51 static const struct of_device_id ufs_mtk_of_match[] = {
52 	{ .compatible = "mediatek,mt8183-ufshci" },
53 	{ .compatible = "mediatek,mt8195-ufshci" },
54 	{},
55 };
56 MODULE_DEVICE_TABLE(of, ufs_mtk_of_match);
57 
58 /*
59  * Details of UIC Errors
60  */
61 static const char *const ufs_uic_err_str[] = {
62 	"PHY Adapter Layer",
63 	"Data Link Layer",
64 	"Network Link Layer",
65 	"Transport Link Layer",
66 	"DME"
67 };
68 
69 static const char *const ufs_uic_pa_err_str[] = {
70 	"PHY error on Lane 0",
71 	"PHY error on Lane 1",
72 	"PHY error on Lane 2",
73 	"PHY error on Lane 3",
74 	"Generic PHY Adapter Error. This should be the LINERESET indication"
75 };
76 
77 static const char *const ufs_uic_dl_err_str[] = {
78 	"NAC_RECEIVED",
79 	"TCx_REPLAY_TIMER_EXPIRED",
80 	"AFCx_REQUEST_TIMER_EXPIRED",
81 	"FCx_PROTECTION_TIMER_EXPIRED",
82 	"CRC_ERROR",
83 	"RX_BUFFER_OVERFLOW",
84 	"MAX_FRAME_LENGTH_EXCEEDED",
85 	"WRONG_SEQUENCE_NUMBER",
86 	"AFC_FRAME_SYNTAX_ERROR",
87 	"NAC_FRAME_SYNTAX_ERROR",
88 	"EOF_SYNTAX_ERROR",
89 	"FRAME_SYNTAX_ERROR",
90 	"BAD_CTRL_SYMBOL_TYPE",
91 	"PA_INIT_ERROR",
92 	"PA_ERROR_IND_RECEIVED",
93 	"PA_INIT"
94 };
95 
96 static bool ufs_mtk_is_boost_crypt_enabled(struct ufs_hba *hba)
97 {
98 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
99 
100 	return host->caps & UFS_MTK_CAP_BOOST_CRYPT_ENGINE;
101 }
102 
103 static bool ufs_mtk_is_va09_supported(struct ufs_hba *hba)
104 {
105 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
106 
107 	return host->caps & UFS_MTK_CAP_VA09_PWR_CTRL;
108 }
109 
110 static bool ufs_mtk_is_broken_vcc(struct ufs_hba *hba)
111 {
112 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
113 
114 	return host->caps & UFS_MTK_CAP_BROKEN_VCC;
115 }
116 
117 static bool ufs_mtk_is_pmc_via_fastauto(struct ufs_hba *hba)
118 {
119 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
120 
121 	return host->caps & UFS_MTK_CAP_PMC_VIA_FASTAUTO;
122 }
123 
124 static bool ufs_mtk_is_tx_skew_fix(struct ufs_hba *hba)
125 {
126 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
127 
128 	return host->caps & UFS_MTK_CAP_TX_SKEW_FIX;
129 }
130 
131 static bool ufs_mtk_is_rtff_mtcmos(struct ufs_hba *hba)
132 {
133 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
134 
135 	return host->caps & UFS_MTK_CAP_RTFF_MTCMOS;
136 }
137 
138 static bool ufs_mtk_is_allow_vccqx_lpm(struct ufs_hba *hba)
139 {
140 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
141 
142 	return host->caps & UFS_MTK_CAP_ALLOW_VCCQX_LPM;
143 }
144 
145 static bool ufs_mtk_is_clk_scale_ready(struct ufs_hba *hba)
146 {
147 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
148 	struct ufs_mtk_clk *mclk = &host->mclk;
149 
150 	return mclk->ufs_sel_clki &&
151 		mclk->ufs_sel_max_clki &&
152 		mclk->ufs_sel_min_clki;
153 }
154 
155 static void ufs_mtk_cfg_unipro_cg(struct ufs_hba *hba, bool enable)
156 {
157 	u32 tmp;
158 
159 	if (enable) {
160 		ufshcd_dme_get(hba,
161 			       UIC_ARG_MIB(VS_SAVEPOWERCONTROL), &tmp);
162 		tmp = tmp |
163 		      (1 << RX_SYMBOL_CLK_GATE_EN) |
164 		      (1 << SYS_CLK_GATE_EN) |
165 		      (1 << TX_CLK_GATE_EN);
166 		ufshcd_dme_set(hba,
167 			       UIC_ARG_MIB(VS_SAVEPOWERCONTROL), tmp);
168 
169 		ufshcd_dme_get(hba,
170 			       UIC_ARG_MIB(VS_DEBUGCLOCKENABLE), &tmp);
171 		tmp = tmp & ~(1 << TX_SYMBOL_CLK_REQ_FORCE);
172 		ufshcd_dme_set(hba,
173 			       UIC_ARG_MIB(VS_DEBUGCLOCKENABLE), tmp);
174 	} else {
175 		ufshcd_dme_get(hba,
176 			       UIC_ARG_MIB(VS_SAVEPOWERCONTROL), &tmp);
177 		tmp = tmp & ~((1 << RX_SYMBOL_CLK_GATE_EN) |
178 			      (1 << SYS_CLK_GATE_EN) |
179 			      (1 << TX_CLK_GATE_EN));
180 		ufshcd_dme_set(hba,
181 			       UIC_ARG_MIB(VS_SAVEPOWERCONTROL), tmp);
182 
183 		ufshcd_dme_get(hba,
184 			       UIC_ARG_MIB(VS_DEBUGCLOCKENABLE), &tmp);
185 		tmp = tmp | (1 << TX_SYMBOL_CLK_REQ_FORCE);
186 		ufshcd_dme_set(hba,
187 			       UIC_ARG_MIB(VS_DEBUGCLOCKENABLE), tmp);
188 	}
189 }
190 
191 static void ufs_mtk_crypto_enable(struct ufs_hba *hba)
192 {
193 	struct arm_smccc_res res;
194 
195 	ufs_mtk_crypto_ctrl(res, 1);
196 	if (res.a0) {
197 		dev_info(hba->dev, "%s: crypto enable failed, err: %lu\n",
198 			 __func__, res.a0);
199 		hba->caps &= ~UFSHCD_CAP_CRYPTO;
200 	}
201 }
202 
203 static void ufs_mtk_host_reset(struct ufs_hba *hba)
204 {
205 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
206 	struct arm_smccc_res res;
207 
208 	reset_control_assert(host->hci_reset);
209 	reset_control_assert(host->crypto_reset);
210 	reset_control_assert(host->unipro_reset);
211 	reset_control_assert(host->mphy_reset);
212 
213 	usleep_range(100, 110);
214 
215 	reset_control_deassert(host->unipro_reset);
216 	reset_control_deassert(host->crypto_reset);
217 	reset_control_deassert(host->hci_reset);
218 	reset_control_deassert(host->mphy_reset);
219 
220 	/* restore mphy setting aftre mphy reset */
221 	if (host->mphy_reset)
222 		ufs_mtk_mphy_ctrl(UFS_MPHY_RESTORE, res);
223 }
224 
225 static void ufs_mtk_init_reset_control(struct ufs_hba *hba,
226 				       struct reset_control **rc,
227 				       char *str)
228 {
229 	*rc = devm_reset_control_get(hba->dev, str);
230 	if (IS_ERR(*rc)) {
231 		dev_info(hba->dev, "Failed to get reset control %s: %ld\n",
232 			 str, PTR_ERR(*rc));
233 		*rc = NULL;
234 	}
235 }
236 
237 static void ufs_mtk_init_reset(struct ufs_hba *hba)
238 {
239 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
240 
241 	ufs_mtk_init_reset_control(hba, &host->hci_reset,
242 				   "hci_rst");
243 	ufs_mtk_init_reset_control(hba, &host->unipro_reset,
244 				   "unipro_rst");
245 	ufs_mtk_init_reset_control(hba, &host->crypto_reset,
246 				   "crypto_rst");
247 	ufs_mtk_init_reset_control(hba, &host->mphy_reset,
248 				   "mphy_rst");
249 }
250 
251 static int ufs_mtk_hce_enable_notify(struct ufs_hba *hba,
252 				     enum ufs_notify_change_status status)
253 {
254 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
255 
256 	if (status == PRE_CHANGE) {
257 		if (host->unipro_lpm) {
258 			hba->vps->hba_enable_delay_us = 0;
259 		} else {
260 			hba->vps->hba_enable_delay_us = 600;
261 			ufs_mtk_host_reset(hba);
262 		}
263 
264 		if (hba->caps & UFSHCD_CAP_CRYPTO)
265 			ufs_mtk_crypto_enable(hba);
266 
267 		if (host->caps & UFS_MTK_CAP_DISABLE_AH8) {
268 			ufshcd_writel(hba, 0,
269 				      REG_AUTO_HIBERNATE_IDLE_TIMER);
270 			hba->capabilities &= ~MASK_AUTO_HIBERN8_SUPPORT;
271 			hba->ahit = 0;
272 		}
273 
274 		/*
275 		 * Turn on CLK_CG early to bypass abnormal ERR_CHK signal
276 		 * to prevent host hang issue
277 		 */
278 		ufshcd_writel(hba,
279 			      ufshcd_readl(hba, REG_UFS_XOUFS_CTRL) | 0x80,
280 			      REG_UFS_XOUFS_CTRL);
281 
282 		if (host->legacy_ip_ver)
283 			return 0;
284 
285 		/* DDR_EN setting */
286 		if (host->ip_ver >= IP_VER_MT6989) {
287 			ufshcd_rmwl(hba, UFS_MASK(0x7FFF, 8),
288 				0x453000, REG_UFS_MMIO_OPT_CTRL_0);
289 		}
290 
291 		if (host->ip_ver >= IP_VER_MT6991_A0) {
292 			/* Enable multi-rtt */
293 			ufshcd_rmwl(hba, MRTT_EN, MRTT_EN, REG_UFS_MMIO_OPT_CTRL_0);
294 			/* Enable random performance improvement */
295 			ufshcd_rmwl(hba, RDN_PFM_IMPV_DIS, 0, REG_UFS_MMIO_OPT_CTRL_0);
296 		}
297 	}
298 
299 	return 0;
300 }
301 
302 static int ufs_mtk_bind_mphy(struct ufs_hba *hba)
303 {
304 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
305 	struct device *dev = hba->dev;
306 	struct device_node *np = dev->of_node;
307 	int err = 0;
308 
309 	host->mphy = devm_of_phy_get_by_index(dev, np, 0);
310 
311 	if (host->mphy == ERR_PTR(-EPROBE_DEFER)) {
312 		/*
313 		 * UFS driver might be probed before the phy driver does.
314 		 * In that case we would like to return EPROBE_DEFER code.
315 		 */
316 		err = -EPROBE_DEFER;
317 		dev_info(dev,
318 			 "%s: required phy hasn't probed yet. err = %d\n",
319 			__func__, err);
320 	} else if (IS_ERR(host->mphy)) {
321 		err = PTR_ERR(host->mphy);
322 		if (err != -ENODEV) {
323 			dev_info(dev, "%s: PHY get failed %d\n", __func__,
324 				 err);
325 		}
326 	}
327 
328 	if (err)
329 		host->mphy = NULL;
330 	/*
331 	 * Allow unbound mphy because not every platform needs specific
332 	 * mphy control.
333 	 */
334 	if (err == -ENODEV)
335 		err = 0;
336 
337 	return err;
338 }
339 
340 static int ufs_mtk_setup_ref_clk(struct ufs_hba *hba, bool on)
341 {
342 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
343 	struct arm_smccc_res res;
344 	ktime_t timeout, time_checked;
345 	u32 value;
346 
347 	if (host->ref_clk_enabled == on)
348 		return 0;
349 
350 	ufs_mtk_ref_clk_notify(on, PRE_CHANGE, res);
351 
352 	if (on) {
353 		ufshcd_writel(hba, REFCLK_REQUEST, REG_UFS_REFCLK_CTRL);
354 	} else {
355 		ufshcd_delay_us(host->ref_clk_gating_wait_us, 10);
356 		ufshcd_writel(hba, REFCLK_RELEASE, REG_UFS_REFCLK_CTRL);
357 	}
358 
359 	/* Wait for ack */
360 	timeout = ktime_add_us(ktime_get(), REFCLK_REQ_TIMEOUT_US);
361 	do {
362 		time_checked = ktime_get();
363 		value = ufshcd_readl(hba, REG_UFS_REFCLK_CTRL);
364 
365 		/* Wait until ack bit equals to req bit */
366 		if (((value & REFCLK_ACK) >> 1) == (value & REFCLK_REQUEST))
367 			goto out;
368 
369 		usleep_range(100, 200);
370 	} while (ktime_before(time_checked, timeout));
371 
372 	dev_err(hba->dev, "missing ack of refclk req, reg: 0x%x\n", value);
373 
374 	/*
375 	 * If clock on timeout, assume clock is off, notify tfa do clock
376 	 * off setting.(keep DIFN disable, release resource)
377 	 * If clock off timeout, assume clock will off finally,
378 	 * set ref_clk_enabled directly.(keep DIFN disable, keep resource)
379 	 */
380 	if (on)
381 		ufs_mtk_ref_clk_notify(false, POST_CHANGE, res);
382 	else
383 		host->ref_clk_enabled = false;
384 
385 	return -ETIMEDOUT;
386 
387 out:
388 	host->ref_clk_enabled = on;
389 	if (on)
390 		ufshcd_delay_us(host->ref_clk_ungating_wait_us, 10);
391 
392 	ufs_mtk_ref_clk_notify(on, POST_CHANGE, res);
393 
394 	return 0;
395 }
396 
397 static void ufs_mtk_setup_ref_clk_wait_us(struct ufs_hba *hba,
398 					  u16 gating_us)
399 {
400 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
401 
402 	if (hba->dev_info.clk_gating_wait_us) {
403 		host->ref_clk_gating_wait_us =
404 			hba->dev_info.clk_gating_wait_us;
405 	} else {
406 		host->ref_clk_gating_wait_us = gating_us;
407 	}
408 
409 	host->ref_clk_ungating_wait_us = REFCLK_DEFAULT_WAIT_US;
410 }
411 
412 static void ufs_mtk_dbg_sel(struct ufs_hba *hba)
413 {
414 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
415 
416 	if (!host->legacy_ip_ver && host->ip_ver >= IP_VER_MT6983) {
417 		ufshcd_writel(hba, 0x820820, REG_UFS_DEBUG_SEL);
418 		ufshcd_writel(hba, 0x0, REG_UFS_DEBUG_SEL_B0);
419 		ufshcd_writel(hba, 0x55555555, REG_UFS_DEBUG_SEL_B1);
420 		ufshcd_writel(hba, 0xaaaaaaaa, REG_UFS_DEBUG_SEL_B2);
421 		ufshcd_writel(hba, 0xffffffff, REG_UFS_DEBUG_SEL_B3);
422 	} else {
423 		ufshcd_writel(hba, 0x20, REG_UFS_DEBUG_SEL);
424 	}
425 }
426 
427 static int ufs_mtk_wait_idle_state(struct ufs_hba *hba,
428 			    unsigned long retry_ms)
429 {
430 	u64 timeout, time_checked;
431 	u32 val, sm;
432 	bool wait_idle;
433 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
434 
435 	/* cannot use plain ktime_get() in suspend */
436 	timeout = ktime_get_mono_fast_ns() + retry_ms * 1000000UL;
437 
438 	/* wait a specific time after check base */
439 	udelay(10);
440 	wait_idle = false;
441 
442 	do {
443 		time_checked = ktime_get_mono_fast_ns();
444 		if (host->legacy_ip_ver || host->ip_ver < IP_VER_MT6899) {
445 			ufs_mtk_dbg_sel(hba);
446 			val = ufshcd_readl(hba, REG_UFS_PROBE);
447 		} else {
448 			val = ufshcd_readl(hba, REG_UFS_UFS_MMIO_OTSD_CTRL);
449 			val = val >> 16;
450 		}
451 
452 		sm = val & 0x1f;
453 
454 		/*
455 		 * if state is in H8 enter and H8 enter confirm
456 		 * wait until return to idle state.
457 		 */
458 		if ((sm >= VS_HIB_ENTER) && (sm <= VS_HIB_EXIT)) {
459 			wait_idle = true;
460 			udelay(50);
461 			continue;
462 		} else if (!wait_idle)
463 			break;
464 
465 		if (wait_idle && (sm == VS_HCE_BASE))
466 			break;
467 	} while (time_checked < timeout);
468 
469 	if (wait_idle && sm != VS_HCE_BASE) {
470 		dev_info(hba->dev, "wait idle tmo: 0x%x\n", val);
471 		return -ETIMEDOUT;
472 	}
473 
474 	return 0;
475 }
476 
477 static int ufs_mtk_wait_link_state(struct ufs_hba *hba, u32 state,
478 				   unsigned long max_wait_ms)
479 {
480 	ktime_t timeout, time_checked;
481 	u32 val;
482 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
483 
484 	timeout = ktime_add_ms(ktime_get(), max_wait_ms);
485 	do {
486 		time_checked = ktime_get();
487 
488 		if (host->legacy_ip_ver || host->ip_ver < IP_VER_MT6899) {
489 			ufs_mtk_dbg_sel(hba);
490 			val = ufshcd_readl(hba, REG_UFS_PROBE);
491 			val = val >> 28;
492 		} else {
493 			val = ufshcd_readl(hba, REG_UFS_UFS_MMIO_OTSD_CTRL);
494 			val = val >> 24;
495 		}
496 
497 		if (val == state)
498 			return 0;
499 
500 		/* Sleep for max. 200us */
501 		usleep_range(100, 200);
502 	} while (ktime_before(time_checked, timeout));
503 
504 	return -ETIMEDOUT;
505 }
506 
507 static int ufs_mtk_mphy_power_on(struct ufs_hba *hba, bool on)
508 {
509 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
510 	struct phy *mphy = host->mphy;
511 	struct arm_smccc_res res;
512 	int err;
513 	int ret = 0;
514 
515 	if (!mphy || !(on ^ host->mphy_powered_on))
516 		return 0;
517 
518 	if (on) {
519 		if (ufs_mtk_is_va09_supported(hba)) {
520 			ret = regulator_enable(host->reg_va09);
521 			if (ret < 0)
522 				goto out;
523 			/* wait 200 us to stablize VA09 */
524 			usleep_range(200, 210);
525 			ufs_mtk_va09_pwr_ctrl(res, 1);
526 		}
527 		ret = phy_power_on(mphy);
528 		if (ret) {
529 			if (ufs_mtk_is_va09_supported(hba)) {
530 				ufs_mtk_va09_pwr_ctrl(res, 0);
531 				err = regulator_disable(host->reg_va09);
532 				if (err)
533 					dev_err(hba->dev,
534 						"failed to unwind va09: %d\n", err);
535 			}
536 			dev_err(hba->dev, "failed to enable mphy: %d\n", ret);
537 			return ret;
538 		}
539 	} else {
540 		phy_power_off(mphy);
541 		if (ufs_mtk_is_va09_supported(hba)) {
542 			ufs_mtk_va09_pwr_ctrl(res, 0);
543 			ret = regulator_disable(host->reg_va09);
544 			if (ret) {
545 				/*
546 				 * The va09 regulator stays enabled, so
547 				 * bring the M-PHY back up to keep the
548 				 * software state in sync with the
549 				 * hardware.
550 				 */
551 				ufs_mtk_va09_pwr_ctrl(res, 1);
552 				phy_power_on(mphy);
553 			}
554 		}
555 	}
556 out:
557 	if (ret) {
558 		dev_info(hba->dev,
559 			 "failed to %s va09: %d\n",
560 			 on ? "enable" : "disable",
561 			 ret);
562 	} else {
563 		host->mphy_powered_on = on;
564 	}
565 
566 	return ret;
567 }
568 
569 static int ufs_mtk_get_host_clk(struct device *dev, const char *name,
570 				struct clk **clk_out)
571 {
572 	struct clk *clk;
573 	int err = 0;
574 
575 	clk = devm_clk_get(dev, name);
576 	if (IS_ERR(clk))
577 		err = PTR_ERR(clk);
578 	else
579 		*clk_out = clk;
580 
581 	return err;
582 }
583 
584 static void ufs_mtk_boost_crypt(struct ufs_hba *hba, bool boost)
585 {
586 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
587 	struct ufs_mtk_crypt_cfg *cfg;
588 	struct regulator *reg;
589 	int volt, ret;
590 
591 	if (!ufs_mtk_is_boost_crypt_enabled(hba))
592 		return;
593 
594 	cfg = host->crypt;
595 	volt = cfg->vcore_volt;
596 	reg = cfg->reg_vcore;
597 
598 	ret = clk_prepare_enable(cfg->clk_crypt_mux);
599 	if (ret) {
600 		dev_info(hba->dev, "clk_prepare_enable(): %d\n",
601 			 ret);
602 		return;
603 	}
604 
605 	if (boost) {
606 		ret = regulator_set_voltage(reg, volt, INT_MAX);
607 		if (ret) {
608 			dev_info(hba->dev,
609 				 "failed to set vcore to %d\n", volt);
610 			goto out;
611 		}
612 
613 		ret = clk_set_parent(cfg->clk_crypt_mux,
614 				     cfg->clk_crypt_perf);
615 		if (ret) {
616 			dev_info(hba->dev,
617 				 "failed to set clk_crypt_perf\n");
618 			regulator_set_voltage(reg, 0, INT_MAX);
619 			goto out;
620 		}
621 	} else {
622 		ret = clk_set_parent(cfg->clk_crypt_mux,
623 				     cfg->clk_crypt_lp);
624 		if (ret) {
625 			dev_info(hba->dev,
626 				 "failed to set clk_crypt_lp\n");
627 			goto out;
628 		}
629 
630 		ret = regulator_set_voltage(reg, 0, INT_MAX);
631 		if (ret) {
632 			dev_info(hba->dev,
633 				 "failed to set vcore to MIN\n");
634 		}
635 	}
636 out:
637 	clk_disable_unprepare(cfg->clk_crypt_mux);
638 }
639 
640 static int ufs_mtk_init_host_clk(struct ufs_hba *hba, const char *name,
641 				 struct clk **clk)
642 {
643 	int ret;
644 
645 	ret = ufs_mtk_get_host_clk(hba->dev, name, clk);
646 	if (ret) {
647 		dev_info(hba->dev, "%s: failed to get %s: %d", __func__,
648 			 name, ret);
649 	}
650 
651 	return ret;
652 }
653 
654 static void ufs_mtk_init_boost_crypt(struct ufs_hba *hba)
655 {
656 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
657 	struct ufs_mtk_crypt_cfg *cfg;
658 	struct device *dev = hba->dev;
659 	struct regulator *reg;
660 	u32 volt;
661 
662 	host->crypt = devm_kzalloc_obj(dev, *(host->crypt));
663 	if (!host->crypt)
664 		goto disable_caps;
665 
666 	reg = devm_regulator_get_optional(dev, "dvfsrc-vcore");
667 	if (IS_ERR(reg)) {
668 		dev_info(dev, "failed to get dvfsrc-vcore: %ld",
669 			 PTR_ERR(reg));
670 		goto disable_caps;
671 	}
672 
673 	if (of_property_read_u32(dev->of_node, "boost-crypt-vcore-min",
674 				 &volt)) {
675 		dev_info(dev, "failed to get boost-crypt-vcore-min");
676 		goto disable_caps;
677 	}
678 
679 	cfg = host->crypt;
680 	if (ufs_mtk_init_host_clk(hba, "crypt_mux",
681 				  &cfg->clk_crypt_mux))
682 		goto disable_caps;
683 
684 	if (ufs_mtk_init_host_clk(hba, "crypt_lp",
685 				  &cfg->clk_crypt_lp))
686 		goto disable_caps;
687 
688 	if (ufs_mtk_init_host_clk(hba, "crypt_perf",
689 				  &cfg->clk_crypt_perf))
690 		goto disable_caps;
691 
692 	cfg->reg_vcore = reg;
693 	cfg->vcore_volt = volt;
694 	host->caps |= UFS_MTK_CAP_BOOST_CRYPT_ENGINE;
695 
696 disable_caps:
697 	return;
698 }
699 
700 static void ufs_mtk_init_va09_pwr_ctrl(struct ufs_hba *hba)
701 {
702 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
703 
704 	host->reg_va09 = devm_regulator_get(hba->dev, "va09");
705 	if (IS_ERR(host->reg_va09))
706 		dev_info(hba->dev, "failed to get va09");
707 	else
708 		host->caps |= UFS_MTK_CAP_VA09_PWR_CTRL;
709 }
710 
711 static void ufs_mtk_init_host_caps(struct ufs_hba *hba)
712 {
713 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
714 	struct device_node *np = hba->dev->of_node;
715 
716 	if (of_property_read_bool(np, "mediatek,ufs-boost-crypt"))
717 		ufs_mtk_init_boost_crypt(hba);
718 
719 	if (of_property_read_bool(np, "mediatek,ufs-support-va09"))
720 		ufs_mtk_init_va09_pwr_ctrl(hba);
721 
722 	if (of_property_read_bool(np, "mediatek,ufs-disable-ah8"))
723 		host->caps |= UFS_MTK_CAP_DISABLE_AH8;
724 
725 	if (of_property_read_bool(np, "mediatek,ufs-broken-vcc"))
726 		host->caps |= UFS_MTK_CAP_BROKEN_VCC;
727 
728 	if (of_property_read_bool(np, "mediatek,ufs-pmc-via-fastauto"))
729 		host->caps |= UFS_MTK_CAP_PMC_VIA_FASTAUTO;
730 
731 	if (of_property_read_bool(np, "mediatek,ufs-tx-skew-fix"))
732 		host->caps |= UFS_MTK_CAP_TX_SKEW_FIX;
733 
734 	if (of_property_read_bool(np, "mediatek,ufs-disable-mcq"))
735 		host->caps |= UFS_MTK_CAP_DISABLE_MCQ;
736 
737 	if (of_property_read_bool(np, "mediatek,ufs-rtff-mtcmos"))
738 		host->caps |= UFS_MTK_CAP_RTFF_MTCMOS;
739 
740 	if (of_property_read_bool(np, "mediatek,ufs-broken-rtc"))
741 		host->caps |= UFS_MTK_CAP_MCQ_BROKEN_RTC;
742 
743 	dev_info(hba->dev, "caps: 0x%x", host->caps);
744 }
745 
746 static void ufs_mtk_scale_perf(struct ufs_hba *hba, bool scale_up)
747 {
748 	ufs_mtk_boost_crypt(hba, scale_up);
749 }
750 
751 static void ufs_mtk_pwr_ctrl(struct ufs_hba *hba, bool on)
752 {
753 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
754 
755 	if (on) {
756 		phy_power_on(host->mphy);
757 		ufs_mtk_setup_ref_clk(hba, on);
758 		if (!ufshcd_is_clkscaling_supported(hba))
759 			ufs_mtk_scale_perf(hba, on);
760 	} else {
761 		if (!ufshcd_is_clkscaling_supported(hba))
762 			ufs_mtk_scale_perf(hba, on);
763 		ufs_mtk_setup_ref_clk(hba, on);
764 		phy_power_off(host->mphy);
765 	}
766 }
767 
768 static void ufs_mtk_mcq_disable_irq(struct ufs_hba *hba)
769 {
770 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
771 	u32 irq, i;
772 
773 	if (!hba->mcq_enabled)
774 		return;
775 
776 	if (host->mcq_nr_intr == 0)
777 		return;
778 
779 	for (i = 0; i < host->mcq_nr_intr; i++) {
780 		irq = host->mcq_intr_info[i].irq;
781 		disable_irq(irq);
782 	}
783 	host->is_mcq_intr_enabled = false;
784 }
785 
786 static void ufs_mtk_mcq_enable_irq(struct ufs_hba *hba)
787 {
788 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
789 	u32 irq, i;
790 
791 	if (!hba->mcq_enabled)
792 		return;
793 
794 	if (host->mcq_nr_intr == 0)
795 		return;
796 
797 	if (host->is_mcq_intr_enabled == true)
798 		return;
799 
800 	for (i = 0; i < host->mcq_nr_intr; i++) {
801 		irq = host->mcq_intr_info[i].irq;
802 		enable_irq(irq);
803 	}
804 	host->is_mcq_intr_enabled = true;
805 }
806 
807 /**
808  * ufs_mtk_setup_clocks - enables/disable clocks
809  * @hba: host controller instance
810  * @on: If true, enable clocks else disable them.
811  * @status: PRE_CHANGE or POST_CHANGE notify
812  *
813  * Return: 0 on success, non-zero on failure.
814  */
815 static int ufs_mtk_setup_clocks(struct ufs_hba *hba, bool on,
816 				enum ufs_notify_change_status status)
817 {
818 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
819 	bool clk_pwr_off = false;
820 	int ret = 0;
821 
822 	/*
823 	 * In case ufs_mtk_init() is not yet done, simply ignore.
824 	 * This ufs_mtk_setup_clocks() shall be called from
825 	 * ufs_mtk_init() after init is done.
826 	 */
827 	if (!host)
828 		return 0;
829 
830 	if (!on && status == PRE_CHANGE) {
831 		if (ufshcd_is_link_off(hba)) {
832 			clk_pwr_off = true;
833 		} else if (ufshcd_is_link_hibern8(hba) ||
834 			 (!ufshcd_can_hibern8_during_gating(hba) &&
835 			 ufshcd_is_auto_hibern8_enabled(hba))) {
836 			/*
837 			 * Gate ref-clk and poweroff mphy if link state is in
838 			 * OFF or Hibern8 by either Auto-Hibern8 or
839 			 * ufshcd_link_state_transition().
840 			 */
841 			ret = ufs_mtk_wait_link_state(hba,
842 						      VS_LINK_HIBERN8,
843 						      15);
844 			if (!ret)
845 				clk_pwr_off = true;
846 		}
847 
848 		if (clk_pwr_off) {
849 			ufs_mtk_pwr_ctrl(hba, false);
850 		} else {
851 			dev_warn(hba->dev, "Clock is not turned off, hba->ahit = 0x%x, AHIT = 0x%x\n",
852 				hba->ahit,
853 				ufshcd_readl(hba,
854 					REG_AUTO_HIBERNATE_IDLE_TIMER));
855 		}
856 		ufs_mtk_mcq_disable_irq(hba);
857 	} else if (on && status == POST_CHANGE) {
858 		ufs_mtk_pwr_ctrl(hba, true);
859 		ufs_mtk_mcq_enable_irq(hba);
860 	}
861 
862 	return ret;
863 }
864 
865 static u32 ufs_mtk_mcq_get_irq(struct ufs_hba *hba, unsigned int cpu)
866 {
867 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
868 	struct blk_mq_tag_set *tag_set = &hba->host->tag_set;
869 	struct blk_mq_queue_map	*map = &tag_set->map[HCTX_TYPE_DEFAULT];
870 	unsigned int nr = map->nr_queues;
871 	unsigned int q_index;
872 
873 	q_index = map->mq_map[cpu];
874 	if (q_index >= nr) {
875 		dev_err(hba->dev, "hwq index %d exceed %d\n",
876 			q_index, nr);
877 		return MTK_MCQ_INVALID_IRQ;
878 	}
879 
880 	return host->mcq_intr_info[q_index].irq;
881 }
882 
883 static void ufs_mtk_mcq_set_irq_affinity(struct ufs_hba *hba, unsigned int cpu)
884 {
885 	unsigned int irq, _cpu;
886 	int ret;
887 
888 	irq = ufs_mtk_mcq_get_irq(hba, cpu);
889 	if (irq == MTK_MCQ_INVALID_IRQ) {
890 		dev_err(hba->dev, "invalid irq. unable to bind irq to cpu%d", cpu);
891 		return;
892 	}
893 
894 	/* force migrate irq of cpu0 to cpu3 */
895 	_cpu = (cpu == 0) ? 3 : cpu;
896 	ret = irq_set_affinity(irq, cpumask_of(_cpu));
897 	if (ret) {
898 		dev_err(hba->dev, "set irq %d affinity to CPU %d failed\n",
899 			irq, _cpu);
900 		return;
901 	}
902 	dev_info(hba->dev, "set irq %d affinity to CPU: %d\n", irq, _cpu);
903 }
904 
905 static bool ufs_mtk_is_legacy_chipset(struct ufs_hba *hba, u32 hw_ip_ver)
906 {
907 	bool is_legacy = false;
908 
909 	switch (hw_ip_ver) {
910 	case IP_LEGACY_VER_MT6893:
911 	case IP_LEGACY_VER_MT6781:
912 		/* can add other legacy chipset ID here accordingly */
913 		is_legacy = true;
914 		break;
915 	default:
916 		break;
917 	}
918 	dev_info(hba->dev, "legacy IP version - 0x%x, is legacy : %d", hw_ip_ver, is_legacy);
919 
920 	return is_legacy;
921 }
922 
923 /*
924  * HW version format has been changed from 01MMmmmm to 1MMMmmmm, since
925  * project MT6878. In order to perform correct version comparison,
926  * version number is changed by SW for the following projects.
927  * IP_VER_MT6983	0x00360000 to 0x10360000
928  * IP_VER_MT6897	0x01440000 to 0x10440000
929  * IP_VER_MT6989	0x01450000 to 0x10450000
930  * IP_VER_MT6991	0x01460000 to 0x10460000
931  */
932 static void ufs_mtk_get_hw_ip_version(struct ufs_hba *hba)
933 {
934 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
935 	u32 hw_ip_ver;
936 
937 	hw_ip_ver = ufshcd_readl(hba, REG_UFS_MTK_IP_VER);
938 
939 	if (((hw_ip_ver & (0xFF << 24)) == (0x1 << 24)) ||
940 	    ((hw_ip_ver & (0xFF << 24)) == 0)) {
941 		hw_ip_ver &= ~(0xFF << 24);
942 		hw_ip_ver |= (0x1 << 28);
943 	}
944 
945 	host->ip_ver = hw_ip_ver;
946 
947 	host->legacy_ip_ver = ufs_mtk_is_legacy_chipset(hba, hw_ip_ver);
948 }
949 
950 static void ufs_mtk_get_controller_version(struct ufs_hba *hba)
951 {
952 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
953 	int ret, ver = 0;
954 
955 	if (host->hw_ver.major)
956 		return;
957 
958 	/* Set default (minimum) version anyway */
959 	host->hw_ver.major = 2;
960 
961 	ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_LOCALVERINFO), &ver);
962 	if (!ret) {
963 		if (ver >= UFS_UNIPRO_VER_1_8) {
964 			host->hw_ver.major = 3;
965 			/*
966 			 * Fix HCI version for some platforms with
967 			 * incorrect version
968 			 */
969 			if (hba->ufs_version < ufshci_version(3, 0))
970 				hba->ufs_version = ufshci_version(3, 0);
971 		}
972 	}
973 }
974 
975 static u32 ufs_mtk_get_ufs_hci_version(struct ufs_hba *hba)
976 {
977 	return hba->ufs_version;
978 }
979 
980 /**
981  * ufs_mtk_init_clocks - Init mtk driver private clocks
982  *
983  * @hba: per adapter instance
984  */
985 static void ufs_mtk_init_clocks(struct ufs_hba *hba)
986 {
987 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
988 	struct list_head *head = &hba->clk_list_head;
989 	struct ufs_clk_info *clki, *clki_tmp;
990 	struct device *dev = hba->dev;
991 	struct regulator *reg;
992 	u32 volt;
993 
994 	/*
995 	 * Find private clocks and store them in struct ufs_mtk_clk.
996 	 * Remove "ufs_sel_min_src" and "ufs_sel_min_src" from list to avoid
997 	 * being switched on/off in clock gating.
998 	 */
999 	list_for_each_entry_safe(clki, clki_tmp, head, list) {
1000 		if (!strcmp(clki->name, "ufs_sel")) {
1001 			host->mclk.ufs_sel_clki = clki;
1002 		} else if (!strcmp(clki->name, "ufs_sel_max_src")) {
1003 			host->mclk.ufs_sel_max_clki = clki;
1004 			clk_disable_unprepare(clki->clk);
1005 			list_del(&clki->list);
1006 		} else if (!strcmp(clki->name, "ufs_sel_min_src")) {
1007 			host->mclk.ufs_sel_min_clki = clki;
1008 			clk_disable_unprepare(clki->clk);
1009 			list_del(&clki->list);
1010 		} else if (!strcmp(clki->name, "ufs_fde")) {
1011 			host->mclk.ufs_fde_clki = clki;
1012 		} else if (!strcmp(clki->name, "ufs_fde_max_src")) {
1013 			host->mclk.ufs_fde_max_clki = clki;
1014 			clk_disable_unprepare(clki->clk);
1015 			list_del(&clki->list);
1016 		} else if (!strcmp(clki->name, "ufs_fde_min_src")) {
1017 			host->mclk.ufs_fde_min_clki = clki;
1018 			clk_disable_unprepare(clki->clk);
1019 			list_del(&clki->list);
1020 		}
1021 	}
1022 
1023 	list_for_each_entry(clki, head, list) {
1024 		dev_info(hba->dev, "clk \"%s\" present", clki->name);
1025 	}
1026 
1027 	if (!ufs_mtk_is_clk_scale_ready(hba)) {
1028 		hba->caps &= ~UFSHCD_CAP_CLK_SCALING;
1029 		dev_info(hba->dev,
1030 			 "%s: Clk-scaling not ready. Feature disabled.",
1031 			 __func__);
1032 		return;
1033 	}
1034 
1035 	/*
1036 	 * Default get vcore if dts have these settings.
1037 	 * No matter clock scaling support or not. (may disable by customer)
1038 	 */
1039 	reg = devm_regulator_get_optional(dev, "dvfsrc-vcore");
1040 	if (IS_ERR(reg)) {
1041 		dev_info(dev, "failed to get dvfsrc-vcore: %ld",
1042 			 PTR_ERR(reg));
1043 		return;
1044 	}
1045 
1046 	if (of_property_read_u32(dev->of_node, "clk-scale-up-vcore-min",
1047 				 &volt)) {
1048 		dev_info(dev, "failed to get clk-scale-up-vcore-min");
1049 		return;
1050 	}
1051 
1052 	host->mclk.reg_vcore = reg;
1053 	host->mclk.vcore_volt = volt;
1054 
1055 	/* If default boot is max gear, request vcore */
1056 	if (reg && volt && host->clk_scale_up) {
1057 		if (regulator_set_voltage(reg, volt, INT_MAX)) {
1058 			dev_info(hba->dev,
1059 				"Failed to set vcore to %d\n", volt);
1060 		}
1061 	}
1062 }
1063 
1064 #define MAX_VCC_NAME 30
1065 static int ufs_mtk_vreg_fix_vcc(struct ufs_hba *hba)
1066 {
1067 	struct ufs_vreg_info *info = &hba->vreg_info;
1068 	struct device_node *np = hba->dev->of_node;
1069 	struct device *dev = hba->dev;
1070 	char vcc_name[MAX_VCC_NAME];
1071 	struct arm_smccc_res res;
1072 	int err, ver;
1073 
1074 	if (info->vcc)
1075 		return 0;
1076 
1077 	if (of_property_read_bool(np, "mediatek,ufs-vcc-by-num")) {
1078 		ufs_mtk_get_vcc_num(res);
1079 		if (res.a1 > UFS_VCC_NONE && res.a1 < UFS_VCC_MAX)
1080 			snprintf(vcc_name, MAX_VCC_NAME, "vcc-opt%lu", res.a1);
1081 		else
1082 			return -ENODEV;
1083 	} else if (of_property_read_bool(np, "mediatek,ufs-vcc-by-ver")) {
1084 		ver = (hba->dev_info.wspecversion & 0xF00) >> 8;
1085 		snprintf(vcc_name, MAX_VCC_NAME, "vcc-ufs%u", ver);
1086 	} else {
1087 		return 0;
1088 	}
1089 
1090 	err = ufshcd_populate_vreg(dev, vcc_name, &info->vcc, false);
1091 	if (err)
1092 		return err;
1093 
1094 	err = ufshcd_get_vreg(dev, info->vcc);
1095 	if (err)
1096 		return err;
1097 
1098 	err = regulator_enable(info->vcc->reg);
1099 	if (!err) {
1100 		info->vcc->enabled = true;
1101 		dev_info(dev, "%s: %s enabled\n", __func__, vcc_name);
1102 	}
1103 
1104 	return err;
1105 }
1106 
1107 static void ufs_mtk_vreg_fix_vccqx(struct ufs_hba *hba)
1108 {
1109 	struct ufs_vreg_info *info = &hba->vreg_info;
1110 	struct ufs_vreg **vreg_on, **vreg_off;
1111 
1112 	if (hba->dev_info.wspecversion >= 0x0300) {
1113 		vreg_on = &info->vccq;
1114 		vreg_off = &info->vccq2;
1115 	} else {
1116 		vreg_on = &info->vccq2;
1117 		vreg_off = &info->vccq;
1118 	}
1119 
1120 	if (*vreg_on)
1121 		(*vreg_on)->always_on = true;
1122 
1123 	if (*vreg_off) {
1124 		regulator_disable((*vreg_off)->reg);
1125 		devm_kfree(hba->dev, (*vreg_off)->name);
1126 		devm_kfree(hba->dev, *vreg_off);
1127 		*vreg_off = NULL;
1128 	}
1129 }
1130 
1131 static void ufs_mtk_setup_clk_gating(struct ufs_hba *hba)
1132 {
1133 	unsigned long flags;
1134 	u32 ah_ms = 10;
1135 	u32 ah_scale, ah_timer;
1136 	static const u32 scale_us[] = {1, 10, 100, 1000, 10000, 100000};
1137 
1138 	if (ufshcd_is_clkgating_allowed(hba)) {
1139 		if (ufshcd_is_auto_hibern8_supported(hba) && hba->ahit) {
1140 			ah_scale = FIELD_GET(UFSHCI_AHIBERN8_SCALE_MASK,
1141 					  hba->ahit);
1142 			ah_timer = FIELD_GET(UFSHCI_AHIBERN8_TIMER_MASK,
1143 					  hba->ahit);
1144 			if (ah_scale <= 5)
1145 				ah_ms = ah_timer * scale_us[ah_scale] / 1000;
1146 		}
1147 
1148 		spin_lock_irqsave(&hba->host->host_lock, flags);
1149 		hba->clk_gating.delay_ms = max(ah_ms, 10U);
1150 		spin_unlock_irqrestore(&hba->host->host_lock, flags);
1151 	}
1152 }
1153 
1154 static void ufs_mtk_fix_ahit(struct ufs_hba *hba)
1155 {
1156 	unsigned int us;
1157 
1158 	if (ufshcd_is_auto_hibern8_supported(hba)) {
1159 		switch (hba->dev_info.wmanufacturerid) {
1160 		case UFS_VENDOR_SAMSUNG:
1161 			/* configure auto-hibern8 timer to 3.5 ms */
1162 			us = 3500;
1163 			break;
1164 
1165 		case UFS_VENDOR_MICRON:
1166 			/* configure auto-hibern8 timer to 2 ms */
1167 			us = 2000;
1168 			break;
1169 
1170 		default:
1171 			/* configure auto-hibern8 timer to 1 ms */
1172 			us = 1000;
1173 			break;
1174 		}
1175 
1176 		hba->ahit = ufshcd_us_to_ahit(us);
1177 	}
1178 
1179 	ufs_mtk_setup_clk_gating(hba);
1180 }
1181 
1182 static void ufs_mtk_fix_clock_scaling(struct ufs_hba *hba)
1183 {
1184 	/* UFS version is below 4.0, clock scaling is not necessary */
1185 	if ((hba->dev_info.wspecversion < 0x0400)  &&
1186 		ufs_mtk_is_clk_scale_ready(hba)) {
1187 		hba->caps &= ~UFSHCD_CAP_CLK_SCALING;
1188 
1189 		_ufs_mtk_clk_scale(hba, false);
1190 	}
1191 }
1192 
1193 static void ufs_mtk_init_mcq_irq(struct ufs_hba *hba)
1194 {
1195 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
1196 	struct platform_device *pdev;
1197 	int i;
1198 	int irq;
1199 
1200 	host->mcq_nr_intr = UFSHCD_MAX_Q_NR;
1201 	pdev = container_of(hba->dev, struct platform_device, dev);
1202 
1203 	if (host->caps & UFS_MTK_CAP_DISABLE_MCQ)
1204 		goto failed;
1205 
1206 	for (i = 0; i < host->mcq_nr_intr; i++) {
1207 		/* irq index 0 is legacy irq, sq/cq irq start from index 1 */
1208 		irq = platform_get_irq(pdev, i + 1);
1209 		if (irq < 0) {
1210 			host->mcq_intr_info[i].irq = MTK_MCQ_INVALID_IRQ;
1211 			goto failed;
1212 		}
1213 		host->mcq_intr_info[i].hba = hba;
1214 		host->mcq_intr_info[i].irq = irq;
1215 		dev_info(hba->dev, "get platform mcq irq: %d, %d\n", i, irq);
1216 	}
1217 
1218 	return;
1219 failed:
1220        /* invalidate irq info */
1221 	for (i = 0; i < host->mcq_nr_intr; i++)
1222 		host->mcq_intr_info[i].irq = MTK_MCQ_INVALID_IRQ;
1223 
1224 	host->mcq_nr_intr = 0;
1225 }
1226 
1227 /**
1228  * ufs_mtk_init - find other essential mmio bases
1229  * @hba: host controller instance
1230  *
1231  * Binds PHY with controller and powers up PHY enabling clocks
1232  * and regulators.
1233  *
1234  * Return: -EPROBE_DEFER if binding fails, returns negative error
1235  * on phy power up failure and returns zero on success.
1236  */
1237 static int ufs_mtk_init(struct ufs_hba *hba)
1238 {
1239 	const struct of_device_id *id;
1240 	struct device *dev = hba->dev;
1241 	struct ufs_mtk_host *host;
1242 	struct Scsi_Host *shost = hba->host;
1243 	int err = 0;
1244 	struct arm_smccc_res res;
1245 
1246 	host = devm_kzalloc_obj(dev, *host);
1247 	if (!host) {
1248 		err = -ENOMEM;
1249 		dev_info(dev, "%s: no memory for mtk ufs host\n", __func__);
1250 		goto out;
1251 	}
1252 
1253 	host->hba = hba;
1254 	ufshcd_set_variant(hba, host);
1255 
1256 	id = of_match_device(ufs_mtk_of_match, dev);
1257 	if (!id) {
1258 		err = -EINVAL;
1259 		goto out;
1260 	}
1261 
1262 	/* Initialize host capability */
1263 	ufs_mtk_init_host_caps(hba);
1264 
1265 	ufs_mtk_init_mcq_irq(hba);
1266 
1267 	err = ufs_mtk_bind_mphy(hba);
1268 	if (err)
1269 		goto out_variant_clear;
1270 
1271 	ufs_mtk_init_reset(hba);
1272 
1273 	/* backup mphy setting if mphy can reset */
1274 	if (host->mphy_reset)
1275 		ufs_mtk_mphy_ctrl(UFS_MPHY_BACKUP, res);
1276 
1277 	/* Enable runtime autosuspend */
1278 	hba->caps |= UFSHCD_CAP_RPM_AUTOSUSPEND;
1279 
1280 	/* Enable clock-gating */
1281 	hba->caps |= UFSHCD_CAP_CLK_GATING;
1282 
1283 	/* Enable inline encryption */
1284 	hba->caps |= UFSHCD_CAP_CRYPTO;
1285 
1286 	/* Enable WriteBooster */
1287 	hba->caps |= UFSHCD_CAP_WB_EN;
1288 
1289 	/* Enable clk scaling*/
1290 	hba->caps |= UFSHCD_CAP_CLK_SCALING;
1291 	host->clk_scale_up = true; /* default is max freq */
1292 
1293 	/* Set runtime pm delay to replace default */
1294 	shost->rpm_autosuspend_delay = MTK_RPM_AUTOSUSPEND_DELAY_MS;
1295 
1296 	hba->quirks |= UFSHCI_QUIRK_SKIP_MANUAL_WB_FLUSH_CTRL;
1297 
1298 	hba->quirks |= UFSHCD_QUIRK_MCQ_BROKEN_INTR;
1299 	if (host->caps & UFS_MTK_CAP_MCQ_BROKEN_RTC)
1300 		hba->quirks |= UFSHCD_QUIRK_MCQ_BROKEN_RTC;
1301 
1302 	hba->vps->wb_flush_threshold = UFS_WB_BUF_REMAIN_PERCENT(80);
1303 
1304 	if (host->caps & UFS_MTK_CAP_DISABLE_AH8)
1305 		hba->caps |= UFSHCD_CAP_HIBERN8_WITH_CLK_GATING;
1306 
1307 	if (host->caps & UFS_MTK_CAP_DISABLE_MCQ)
1308 		hba->quirks |= UFSHCD_QUIRK_BROKEN_LSDBS_CAP;
1309 
1310 	ufs_mtk_init_clocks(hba);
1311 
1312 	/*
1313 	 * ufshcd_vops_init() is invoked after
1314 	 * ufshcd_setup_clock(true) in ufshcd_hba_init() thus
1315 	 * phy clock setup is skipped.
1316 	 *
1317 	 * Enable phy clocks specifically here.
1318 	 */
1319 	err = ufs_mtk_mphy_power_on(hba, true);
1320 	if (err)
1321 		goto out_variant_clear;
1322 
1323 	if (ufs_mtk_is_rtff_mtcmos(hba)) {
1324 		/* First Restore here, to avoid backup unexpected value */
1325 		ufs_mtk_mtcmos_ctrl(false, res);
1326 
1327 		/* Power on to init */
1328 		ufs_mtk_mtcmos_ctrl(true, res);
1329 	}
1330 
1331 	ufs_mtk_setup_clocks(hba, true, POST_CHANGE);
1332 
1333 	ufs_mtk_get_hw_ip_version(hba);
1334 
1335 	goto out;
1336 
1337 out_variant_clear:
1338 	ufshcd_set_variant(hba, NULL);
1339 out:
1340 	return err;
1341 }
1342 
1343 static void ufs_mtk_exit(struct ufs_hba *hba)
1344 {
1345 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
1346 
1347 	if (host)
1348 		ufs_mtk_mphy_power_on(hba, false);
1349 }
1350 
1351 static int ufs_mtk_negotiate_pwr_mode(struct ufs_hba *hba,
1352 				      const struct ufs_pa_layer_attr *dev_max_params,
1353 				      struct ufs_pa_layer_attr *dev_req_params)
1354 {
1355 	struct ufs_host_params host_params;
1356 
1357 	ufshcd_init_host_params(&host_params);
1358 	host_params.hs_rx_gear = UFS_HS_G5;
1359 	host_params.hs_tx_gear = UFS_HS_G5;
1360 
1361 	if (dev_max_params->pwr_rx == SLOW_MODE ||
1362 	    dev_max_params->pwr_tx == SLOW_MODE)
1363 		host_params.desired_working_mode = UFS_PWM_MODE;
1364 
1365 	return ufshcd_negotiate_pwr_params(&host_params, dev_max_params, dev_req_params);
1366 }
1367 
1368 static bool ufs_mtk_pmc_via_fastauto(struct ufs_hba *hba,
1369 				     struct ufs_pa_layer_attr *dev_req_params)
1370 {
1371 	if (!ufs_mtk_is_pmc_via_fastauto(hba))
1372 		return false;
1373 
1374 	if (dev_req_params->hs_rate == hba->pwr_info.hs_rate)
1375 		return false;
1376 
1377 	if (dev_req_params->pwr_tx != FAST_MODE &&
1378 	    dev_req_params->gear_tx < UFS_HS_G4)
1379 		return false;
1380 
1381 	if (dev_req_params->pwr_rx != FAST_MODE &&
1382 	    dev_req_params->gear_rx < UFS_HS_G4)
1383 		return false;
1384 
1385 	if (dev_req_params->pwr_tx == SLOW_MODE ||
1386 	    dev_req_params->pwr_rx == SLOW_MODE)
1387 		return false;
1388 
1389 	return true;
1390 }
1391 
1392 static void ufs_mtk_adjust_sync_length(struct ufs_hba *hba)
1393 {
1394 	int i;
1395 	u32 value;
1396 	u32 cnt, att, min;
1397 	struct attr_min {
1398 		u32 attr;
1399 		u32 min_value;
1400 	} pa_min_sync_length[] = {
1401 		{PA_TXHSG1SYNCLENGTH, 0x48},
1402 		{PA_TXHSG2SYNCLENGTH, 0x48},
1403 		{PA_TXHSG3SYNCLENGTH, 0x48},
1404 		{PA_TXHSG4SYNCLENGTH, 0x48},
1405 		{PA_TXHSG5SYNCLENGTH, 0x48}
1406 	};
1407 
1408 	cnt = sizeof(pa_min_sync_length) / sizeof(struct attr_min);
1409 	for (i = 0; i < cnt; i++) {
1410 		att = pa_min_sync_length[i].attr;
1411 		min = pa_min_sync_length[i].min_value;
1412 		ufshcd_dme_get(hba, UIC_ARG_MIB(att), &value);
1413 		if (value < min)
1414 			ufshcd_dme_set(hba, UIC_ARG_MIB(att), min);
1415 
1416 		ufshcd_dme_peer_get(hba, UIC_ARG_MIB(att), &value);
1417 		if (value < min)
1418 			ufshcd_dme_peer_set(hba, UIC_ARG_MIB(att), min);
1419 	}
1420 }
1421 
1422 static int ufs_mtk_pre_pwr_change(struct ufs_hba *hba,
1423 				struct ufs_pa_layer_attr *dev_req_params)
1424 {
1425 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
1426 	int ret = 0;
1427 
1428 	if (ufs_mtk_pmc_via_fastauto(hba, dev_req_params)) {
1429 		ufs_mtk_adjust_sync_length(hba);
1430 
1431 		ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), true);
1432 		ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), UFS_HS_G1);
1433 
1434 		ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), true);
1435 		ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), UFS_HS_G1);
1436 
1437 		ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
1438 			       dev_req_params->lane_tx);
1439 		ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
1440 			       dev_req_params->lane_rx);
1441 		ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
1442 			       dev_req_params->hs_rate);
1443 
1444 		ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXHSADAPTTYPE),
1445 			       PA_NO_ADAPT);
1446 
1447 		if (!(hba->quirks & UFSHCD_QUIRK_SKIP_DEF_UNIPRO_TIMEOUT_SETTING)) {
1448 			ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA0),
1449 					DL_FC0ProtectionTimeOutVal_Default);
1450 			ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA1),
1451 					DL_TC0ReplayTimeOutVal_Default);
1452 			ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA2),
1453 					DL_AFC0ReqTimeOutVal_Default);
1454 			ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA3),
1455 					DL_FC1ProtectionTimeOutVal_Default);
1456 			ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA4),
1457 					DL_TC1ReplayTimeOutVal_Default);
1458 			ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA5),
1459 					DL_AFC1ReqTimeOutVal_Default);
1460 
1461 			ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalFC0ProtectionTimeOutVal),
1462 					DL_FC0ProtectionTimeOutVal_Default);
1463 			ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalTC0ReplayTimeOutVal),
1464 					DL_TC0ReplayTimeOutVal_Default);
1465 			ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalAFC0ReqTimeOutVal),
1466 					DL_AFC0ReqTimeOutVal_Default);
1467 		}
1468 
1469 		ret = ufshcd_uic_change_pwr_mode(hba,
1470 					FASTAUTO_MODE << 4 | FASTAUTO_MODE);
1471 
1472 		if (ret) {
1473 			dev_err(hba->dev, "%s: HSG1B FASTAUTO failed ret=%d\n",
1474 				__func__, ret);
1475 		}
1476 	}
1477 
1478 	/* if already configured to the requested pwr_mode, skip adapt */
1479 	if (dev_req_params->gear_rx == hba->pwr_info.gear_rx &&
1480 	    dev_req_params->gear_tx == hba->pwr_info.gear_tx &&
1481 	    dev_req_params->lane_rx == hba->pwr_info.lane_rx &&
1482 	    dev_req_params->lane_tx == hba->pwr_info.lane_tx &&
1483 	    dev_req_params->pwr_rx == hba->pwr_info.pwr_rx &&
1484 	    dev_req_params->pwr_tx == hba->pwr_info.pwr_tx &&
1485 	    dev_req_params->hs_rate == hba->pwr_info.hs_rate) {
1486 		return ret;
1487 	}
1488 
1489 	if (dev_req_params->pwr_rx == FAST_MODE ||
1490 	    dev_req_params->pwr_rx == FASTAUTO_MODE) {
1491 		if (host->hw_ver.major >= 3) {
1492 			ret = ufshcd_dme_configure_adapt(hba,
1493 						   dev_req_params->gear_tx,
1494 						   PA_INITIAL_ADAPT);
1495 		} else {
1496 			ret = ufshcd_dme_configure_adapt(hba,
1497 				   dev_req_params->gear_tx,
1498 				   PA_NO_ADAPT);
1499 		}
1500 	} else {
1501 		ret = ufshcd_dme_configure_adapt(hba,
1502 			   dev_req_params->gear_tx,
1503 			   PA_NO_ADAPT);
1504 	}
1505 
1506 	return ret;
1507 }
1508 
1509 static int ufs_mtk_auto_hibern8_disable(struct ufs_hba *hba)
1510 {
1511 	int ret;
1512 
1513 	/* disable auto-hibern8 */
1514 	ufshcd_writel(hba, 0, REG_AUTO_HIBERNATE_IDLE_TIMER);
1515 
1516 	/* wait host return to idle state when auto-hibern8 off */
1517 	ret = ufs_mtk_wait_idle_state(hba, 5);
1518 	if (ret)
1519 		goto out;
1520 
1521 	ret = ufs_mtk_wait_link_state(hba, VS_LINK_UP, 100);
1522 
1523 out:
1524 	if (ret) {
1525 		dev_warn(hba->dev, "exit h8 state fail, ret=%d\n", ret);
1526 
1527 		ufshcd_force_error_recovery(hba);
1528 
1529 		/* trigger error handler and break suspend */
1530 		ret = -EBUSY;
1531 	}
1532 
1533 	return ret;
1534 }
1535 
1536 static int ufs_mtk_pwr_change_notify(struct ufs_hba *hba,
1537 				enum ufs_notify_change_status stage,
1538 				struct ufs_pa_layer_attr *dev_req_params)
1539 {
1540 	int ret = 0;
1541 
1542 	switch (stage) {
1543 	case PRE_CHANGE:
1544 		if (ufshcd_is_auto_hibern8_supported(hba)) {
1545 			/* Block sysfs AHIT writes while we force AHIT off */
1546 			mutex_lock(&hba->ahit_mutex);
1547 			hba->ahit_disable_depth++;
1548 			mutex_unlock(&hba->ahit_mutex);
1549 			ufs_mtk_auto_hibern8_disable(hba);
1550 		}
1551 		ret = ufs_mtk_pre_pwr_change(hba, dev_req_params);
1552 		break;
1553 	case POST_CHANGE:
1554 		if (ufshcd_is_auto_hibern8_supported(hba)) {
1555 			mutex_lock(&hba->ahit_mutex);
1556 			/* re-enable only when the last disabler drops off */
1557 			if (!--hba->ahit_disable_depth)
1558 				ufshcd_writel(hba, hba->ahit,
1559 					      REG_AUTO_HIBERNATE_IDLE_TIMER);
1560 			mutex_unlock(&hba->ahit_mutex);
1561 		}
1562 		break;
1563 	default:
1564 		ret = -EINVAL;
1565 		break;
1566 	}
1567 
1568 	return ret;
1569 }
1570 
1571 static int ufs_mtk_unipro_set_lpm(struct ufs_hba *hba, bool lpm)
1572 {
1573 	int ret;
1574 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
1575 
1576 	ret = ufshcd_dme_set(hba,
1577 			     UIC_ARG_MIB_SEL(VS_UNIPROPOWERDOWNCONTROL, 0),
1578 			     lpm ? 1 : 0);
1579 	if (!ret || !lpm) {
1580 		/*
1581 		 * Forcibly set as non-LPM mode if UIC commands is failed
1582 		 * to use default hba_enable_delay_us value for re-enabling
1583 		 * the host.
1584 		 */
1585 		host->unipro_lpm = lpm;
1586 	}
1587 
1588 	return ret;
1589 }
1590 
1591 static int ufs_mtk_pre_link(struct ufs_hba *hba)
1592 {
1593 	int ret;
1594 	u32 tmp;
1595 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
1596 
1597 	ufs_mtk_get_controller_version(hba);
1598 
1599 	ret = ufs_mtk_unipro_set_lpm(hba, false);
1600 	if (ret)
1601 		return ret;
1602 
1603 	/*
1604 	 * Setting PA_Local_TX_LCC_Enable to 0 before link startup
1605 	 * to make sure that both host and device TX LCC are disabled
1606 	 * once link startup is completed.
1607 	 */
1608 	ret = ufshcd_disable_host_tx_lcc(hba);
1609 	if (ret)
1610 		return ret;
1611 
1612 	/* disable deep stall */
1613 	ret = ufshcd_dme_get(hba, UIC_ARG_MIB(VS_SAVEPOWERCONTROL), &tmp);
1614 	if (ret)
1615 		return ret;
1616 
1617 	tmp &= ~(1 << 6);
1618 
1619 	ret = ufshcd_dme_set(hba, UIC_ARG_MIB(VS_SAVEPOWERCONTROL), tmp);
1620 
1621 	/* Enable the 1144 functions setting */
1622 	if (host->ip_ver == IP_VER_MT6989) {
1623 		ret = ufshcd_dme_get(hba, UIC_ARG_MIB(VS_DEBUGOMC), &tmp);
1624 		if (ret)
1625 			return ret;
1626 
1627 		tmp |= 0x10;
1628 		ret = ufshcd_dme_set(hba, UIC_ARG_MIB(VS_DEBUGOMC), tmp);
1629 	}
1630 
1631 	return ret;
1632 }
1633 
1634 static void ufs_mtk_post_link(struct ufs_hba *hba)
1635 {
1636 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
1637 	u32 tmp;
1638 
1639 	/* fix device PA_INIT no adapt */
1640 	if (host->ip_ver >= IP_VER_MT6899) {
1641 		ufshcd_dme_get(hba, UIC_ARG_MIB(VS_DEBUGOMC), &tmp);
1642 		tmp |= 0x100;
1643 		ufshcd_dme_set(hba, UIC_ARG_MIB(VS_DEBUGOMC), tmp);
1644 	}
1645 
1646 	/* enable unipro clock gating feature */
1647 	ufs_mtk_cfg_unipro_cg(hba, true);
1648 }
1649 
1650 static int ufs_mtk_link_startup_notify(struct ufs_hba *hba,
1651 				       enum ufs_notify_change_status stage)
1652 {
1653 	int ret = 0;
1654 
1655 	switch (stage) {
1656 	case PRE_CHANGE:
1657 		ret = ufs_mtk_pre_link(hba);
1658 		break;
1659 	case POST_CHANGE:
1660 		ufs_mtk_post_link(hba);
1661 		break;
1662 	default:
1663 		ret = -EINVAL;
1664 		break;
1665 	}
1666 
1667 	return ret;
1668 }
1669 
1670 static int ufs_mtk_device_reset(struct ufs_hba *hba)
1671 {
1672 	struct arm_smccc_res res;
1673 
1674 	ufs_mtk_device_reset_ctrl(0, res);
1675 
1676 	/* disable hba in middle of device reset */
1677 	ufshcd_hba_stop(hba);
1678 
1679 	/*
1680 	 * The reset signal is active low. UFS devices shall detect
1681 	 * more than or equal to 1us of positive or negative RST_n
1682 	 * pulse width.
1683 	 *
1684 	 * To be on safe side, keep the reset low for at least 10us.
1685 	 */
1686 	usleep_range(10, 15);
1687 
1688 	ufs_mtk_device_reset_ctrl(1, res);
1689 
1690 	/* Some devices may need time to respond to rst_n */
1691 	usleep_range(10000, 15000);
1692 
1693 	dev_info(hba->dev, "device reset done\n");
1694 
1695 	return 0;
1696 }
1697 
1698 static int ufs_mtk_link_set_hpm(struct ufs_hba *hba)
1699 {
1700 	int err;
1701 	u32 val;
1702 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
1703 
1704 	err = ufshcd_hba_enable(hba);
1705 	if (err)
1706 		return err;
1707 
1708 	err = ufs_mtk_unipro_set_lpm(hba, false);
1709 	if (err) {
1710 		if (host->ip_ver < IP_VER_MT6899) {
1711 			ufs_mtk_dbg_sel(hba);
1712 			val = ufshcd_readl(hba, REG_UFS_PROBE);
1713 		} else {
1714 			val = ufshcd_readl(hba, REG_UFS_UFS_MMIO_OTSD_CTRL);
1715 		}
1716 		ufshcd_update_evt_hist(hba, UFS_EVT_RESUME_ERR, (u32)val);
1717 		val = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
1718 		ufshcd_update_evt_hist(hba, UFS_EVT_RESUME_ERR, (u32)val);
1719 		return err;
1720 	}
1721 
1722 	err = ufshcd_uic_hibern8_exit(hba);
1723 	if (err)
1724 		return err;
1725 
1726 	/* Check link state to make sure exit h8 success */
1727 	err = ufs_mtk_wait_idle_state(hba, 5);
1728 	if (err) {
1729 		dev_warn(hba->dev, "wait idle fail, err=%d\n", err);
1730 		return err;
1731 	}
1732 	err = ufs_mtk_wait_link_state(hba, VS_LINK_UP, 100);
1733 	if (err) {
1734 		dev_warn(hba->dev, "exit h8 state fail, err=%d\n", err);
1735 		return err;
1736 	}
1737 	ufshcd_set_link_active(hba);
1738 
1739 	err = ufshcd_make_hba_operational(hba);
1740 	if (err)
1741 		return err;
1742 
1743 	if (hba->mcq_enabled) {
1744 		ufs_mtk_config_mcq(hba, false);
1745 		ufshcd_mcq_make_queues_operational(hba);
1746 		ufshcd_mcq_config_mac(hba, hba->nutrs);
1747 		ufshcd_mcq_enable(hba);
1748 	}
1749 
1750 	return 0;
1751 }
1752 
1753 static int ufs_mtk_link_set_lpm(struct ufs_hba *hba)
1754 {
1755 	int err;
1756 
1757 	/* Disable reset confirm feature by UniPro */
1758 	ufshcd_writel(hba,
1759 		      (ufshcd_readl(hba, REG_UFS_XOUFS_CTRL) & ~0x100),
1760 		      REG_UFS_XOUFS_CTRL);
1761 
1762 	err = ufs_mtk_unipro_set_lpm(hba, true);
1763 	if (err) {
1764 		/* Resume UniPro state for following error recovery */
1765 		ufs_mtk_unipro_set_lpm(hba, false);
1766 		return err;
1767 	}
1768 
1769 	return 0;
1770 }
1771 
1772 static void ufs_mtk_vccqx_set_lpm(struct ufs_hba *hba, bool lpm)
1773 {
1774 	struct ufs_vreg *vccqx = NULL;
1775 
1776 	if (!hba->vreg_info.vccq && !hba->vreg_info.vccq2)
1777 		return;
1778 
1779 	if (hba->vreg_info.vccq)
1780 		vccqx = hba->vreg_info.vccq;
1781 	else
1782 		vccqx = hba->vreg_info.vccq2;
1783 
1784 	regulator_set_mode(vccqx->reg,
1785 			   lpm ? REGULATOR_MODE_IDLE : REGULATOR_MODE_NORMAL);
1786 }
1787 
1788 static void ufs_mtk_vsx_set_lpm(struct ufs_hba *hba, bool lpm)
1789 {
1790 	struct arm_smccc_res res;
1791 
1792 	ufs_mtk_device_pwr_ctrl(!lpm,
1793 				(unsigned long)hba->dev_info.wspecversion,
1794 				res);
1795 }
1796 
1797 static void ufs_mtk_dev_vreg_set_lpm(struct ufs_hba *hba, bool lpm)
1798 {
1799 	bool skip_vccqx = false;
1800 
1801 	/* Prevent entering LPM when device is still active */
1802 	if (lpm && ufshcd_is_ufs_dev_active(hba))
1803 		return;
1804 
1805 	/* Skip vccqx lpm control and control vsx only */
1806 	if (!hba->vreg_info.vccq && !hba->vreg_info.vccq2)
1807 		skip_vccqx = true;
1808 
1809 	/* VCC is always-on, control vsx only */
1810 	if (!hba->vreg_info.vcc)
1811 		skip_vccqx = true;
1812 
1813 	/* Broken vcc keep vcc always on, most case control vsx only */
1814 	if (lpm && hba->vreg_info.vcc && hba->vreg_info.vcc->enabled) {
1815 		/* Some device vccqx/vsx can enter lpm */
1816 		if (ufs_mtk_is_allow_vccqx_lpm(hba))
1817 			skip_vccqx = false;
1818 		else /* control vsx only */
1819 			skip_vccqx = true;
1820 	}
1821 
1822 	if (lpm) {
1823 		if (!skip_vccqx)
1824 			ufs_mtk_vccqx_set_lpm(hba, lpm);
1825 		ufs_mtk_vsx_set_lpm(hba, lpm);
1826 	} else {
1827 		ufs_mtk_vsx_set_lpm(hba, lpm);
1828 		if (!skip_vccqx)
1829 			ufs_mtk_vccqx_set_lpm(hba, lpm);
1830 	}
1831 }
1832 
1833 static int ufs_mtk_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op,
1834 	enum ufs_notify_change_status status)
1835 {
1836 	int err;
1837 	struct arm_smccc_res res;
1838 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
1839 
1840 	if (status == PRE_CHANGE) {
1841 		if (ufshcd_is_auto_hibern8_supported(hba))
1842 			return ufs_mtk_auto_hibern8_disable(hba);
1843 		return 0;
1844 	}
1845 
1846 	if (ufshcd_is_link_hibern8(hba)) {
1847 		err = ufs_mtk_link_set_lpm(hba);
1848 		if (err)
1849 			goto fail;
1850 	}
1851 
1852 	if (!ufshcd_is_link_active(hba)) {
1853 		/*
1854 		 * Make sure no error will be returned to prevent
1855 		 * ufshcd_suspend() re-enabling regulators while vreg is still
1856 		 * in low-power mode.
1857 		 */
1858 		err = ufs_mtk_mphy_power_on(hba, false);
1859 		if (err)
1860 			goto fail;
1861 	}
1862 
1863 	if (ufshcd_is_link_off(hba))
1864 		ufs_mtk_device_reset_ctrl(0, res);
1865 
1866 	ufs_mtk_sram_pwr_ctrl(false, res);
1867 
1868 	/* Release pm_qos/clk if in scale-up mode during suspend */
1869 	if (ufshcd_is_clkscaling_supported(hba) && (host->clk_scale_up)) {
1870 		ufshcd_pm_qos_update(hba, false);
1871 		_ufs_mtk_clk_scale(hba, false);
1872 	} else if ((!ufshcd_is_clkscaling_supported(hba) &&
1873 		    hba->pwr_info.gear_rx >= UFS_HS_G5)) {
1874 		_ufs_mtk_clk_scale(hba, false);
1875 	}
1876 
1877 	return 0;
1878 fail:
1879 	/*
1880 	 * Set link as off state enforcedly to trigger
1881 	 * ufshcd_host_reset_and_restore() in ufshcd_suspend()
1882 	 * for completed host reset.
1883 	 */
1884 	ufshcd_set_link_off(hba);
1885 	return -EAGAIN;
1886 }
1887 
1888 static int ufs_mtk_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
1889 {
1890 	int err;
1891 	struct arm_smccc_res res;
1892 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
1893 
1894 	if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL)
1895 		ufs_mtk_dev_vreg_set_lpm(hba, false);
1896 
1897 	ufs_mtk_sram_pwr_ctrl(true, res);
1898 
1899 	err = ufs_mtk_mphy_power_on(hba, true);
1900 	if (err)
1901 		goto fail;
1902 
1903 	/* Request pm_qos/clk if in scale-up mode after resume */
1904 	if (ufshcd_is_clkscaling_supported(hba) && (host->clk_scale_up)) {
1905 		ufshcd_pm_qos_update(hba, true);
1906 		_ufs_mtk_clk_scale(hba, true);
1907 	} else if ((!ufshcd_is_clkscaling_supported(hba) &&
1908 		    hba->pwr_info.gear_rx >= UFS_HS_G5)) {
1909 		_ufs_mtk_clk_scale(hba, true);
1910 	}
1911 
1912 	if (ufshcd_is_link_hibern8(hba)) {
1913 		err = ufs_mtk_link_set_hpm(hba);
1914 		if (err)
1915 			goto fail;
1916 	}
1917 
1918 	return 0;
1919 
1920 fail:
1921 	/*
1922 	 * Check if the platform (parent) device has resumed, and ensure that
1923 	 * power, clock, and MTCMOS are all turned on.
1924 	 */
1925 	err = ufshcd_link_recovery(hba);
1926 	if (err) {
1927 		dev_err(hba->dev, "Device PM: req=%d, status:%d, err:%d\n",
1928 			hba->dev->power.request,
1929 			hba->dev->power.runtime_status,
1930 			hba->dev->power.runtime_error);
1931 	}
1932 
1933 	return 0; /* Cannot return a failure, otherwise, the I/O will hang. */
1934 }
1935 
1936 static void ufs_mtk_dbg_register_dump(struct ufs_hba *hba)
1937 {
1938 	/* Dump ufshci register 0x140 ~ 0x14C */
1939 	ufshcd_dump_regs(hba, REG_UFS_XOUFS_CTRL, 0x10,
1940 			 "XOUFS Ctrl (0x140): ");
1941 
1942 	ufshcd_dump_regs(hba, REG_UFS_EXTREG, 0x4, "Ext Reg ");
1943 
1944 	/* Dump ufshci register 0x2200 ~ 0x22AC */
1945 	ufshcd_dump_regs(hba, REG_UFS_MPHYCTRL,
1946 			 REG_UFS_REJECT_MON - REG_UFS_MPHYCTRL + 4,
1947 			 "MPHY Ctrl (0x2200): ");
1948 
1949 	/* Direct debugging information to REG_MTK_PROBE */
1950 	ufs_mtk_dbg_sel(hba);
1951 	ufshcd_dump_regs(hba, REG_UFS_PROBE, 0x4, "Debug Probe ");
1952 }
1953 
1954 static int ufs_mtk_apply_dev_quirks(struct ufs_hba *hba)
1955 {
1956 	struct ufs_dev_info *dev_info = &hba->dev_info;
1957 	u16 mid = dev_info->wmanufacturerid;
1958 	unsigned int cpu;
1959 
1960 	if (hba->mcq_enabled) {
1961 		/* Iterate all cpus to set affinity for mcq irqs */
1962 		for (cpu = 0; cpu < nr_cpu_ids; cpu++)
1963 			ufs_mtk_mcq_set_irq_affinity(hba, cpu);
1964 	}
1965 
1966 	if (mid == UFS_VENDOR_SAMSUNG) {
1967 		ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 6);
1968 		ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME), 10);
1969 	} else if (mid == UFS_VENDOR_MICRON) {
1970 		/* Only for the host which have TX skew issue */
1971 		if (ufs_mtk_is_tx_skew_fix(hba) &&
1972 			(STR_PRFX_EQUAL("MT128GBCAV2U31", dev_info->model) ||
1973 			STR_PRFX_EQUAL("MT256GBCAV4U31", dev_info->model) ||
1974 			STR_PRFX_EQUAL("MT512GBCAV8U31", dev_info->model) ||
1975 			STR_PRFX_EQUAL("MT256GBEAX4U40", dev_info->model) ||
1976 			STR_PRFX_EQUAL("MT512GAYAX4U40", dev_info->model) ||
1977 			STR_PRFX_EQUAL("MT001TAYAX8U40", dev_info->model))) {
1978 			ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 8);
1979 		}
1980 	}
1981 
1982 	/*
1983 	 * Decide waiting time before gating reference clock and
1984 	 * after ungating reference clock according to vendors'
1985 	 * requirements.
1986 	 */
1987 	if (mid == UFS_VENDOR_SAMSUNG)
1988 		ufs_mtk_setup_ref_clk_wait_us(hba, 1);
1989 	else if (mid == UFS_VENDOR_SKHYNIX)
1990 		ufs_mtk_setup_ref_clk_wait_us(hba, 30);
1991 	else if (mid == UFS_VENDOR_TOSHIBA)
1992 		ufs_mtk_setup_ref_clk_wait_us(hba, 100);
1993 	else
1994 		ufs_mtk_setup_ref_clk_wait_us(hba,
1995 					      REFCLK_DEFAULT_WAIT_US);
1996 	return 0;
1997 }
1998 
1999 static void ufs_mtk_fixup_dev_quirks(struct ufs_hba *hba)
2000 {
2001 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
2002 
2003 	ufshcd_fixup_dev_quirks(hba, ufs_mtk_dev_fixups);
2004 
2005 	if (ufs_mtk_is_broken_vcc(hba) && hba->vreg_info.vcc) {
2006 		hba->vreg_info.vcc->always_on = true;
2007 		/*
2008 		 * VCC will be kept always-on thus we don't
2009 		 * need any delay before putting device's VCC in LPM mode.
2010 		 */
2011 		hba->dev_quirks &= ~UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM;
2012 	}
2013 
2014 	/*
2015 	 * Add a delay after enabling UFS5 VCC to ensure the voltage
2016 	 * is stable before the refclk is enabled.
2017 	 */
2018 	if (hba->dev_info.wspecversion >= 0x0500 &&
2019 	    (host->ip_ver == IP_VER_MT6995_A0 ||
2020 	     host->ip_ver == IP_VER_MT6995_B0))
2021 		hba->quirks |= UFSHCD_QUIRK_VCC_ON_DELAY;
2022 
2023 	ufs_mtk_vreg_fix_vcc(hba);
2024 	ufs_mtk_vreg_fix_vccqx(hba);
2025 	ufs_mtk_fix_ahit(hba);
2026 	ufs_mtk_fix_clock_scaling(hba);
2027 }
2028 
2029 static void ufs_mtk_event_notify(struct ufs_hba *hba,
2030 				 enum ufs_event_type evt, void *data)
2031 {
2032 	unsigned int val = *(u32 *)data;
2033 	unsigned long reg;
2034 	u8 bit;
2035 
2036 	trace_ufs_mtk_event(evt, val);
2037 
2038 	/* Print details of UIC Errors */
2039 	if (evt <= UFS_EVT_DME_ERR) {
2040 		dev_info(hba->dev,
2041 			 "Host UIC Error Code (%s): %08x\n",
2042 			 ufs_uic_err_str[evt], val);
2043 		reg = val;
2044 	}
2045 
2046 	if (evt == UFS_EVT_PA_ERR) {
2047 		for_each_set_bit(bit, &reg, ARRAY_SIZE(ufs_uic_pa_err_str))
2048 			dev_info(hba->dev, "%s\n", ufs_uic_pa_err_str[bit]);
2049 	}
2050 
2051 	if (evt == UFS_EVT_DL_ERR) {
2052 		for_each_set_bit(bit, &reg, ARRAY_SIZE(ufs_uic_dl_err_str))
2053 			dev_info(hba->dev, "%s\n", ufs_uic_dl_err_str[bit]);
2054 	}
2055 }
2056 
2057 static void ufs_mtk_config_scaling_param(struct ufs_hba *hba,
2058 				struct devfreq_dev_profile *profile,
2059 				struct devfreq_simple_ondemand_data *data)
2060 {
2061 	/* Customize min gear in clk scaling */
2062 	hba->clk_scaling.min_gear = UFS_HS_G4;
2063 
2064 	hba->vps->devfreq_profile.polling_ms = 200;
2065 	hba->vps->ondemand_data.upthreshold = 50;
2066 	hba->vps->ondemand_data.downdifferential = 20;
2067 }
2068 
2069 static void _ufs_mtk_clk_scale(struct ufs_hba *hba, bool scale_up)
2070 {
2071 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
2072 	struct ufs_mtk_clk *mclk = &host->mclk;
2073 	struct ufs_clk_info *clki = mclk->ufs_sel_clki;
2074 	struct ufs_clk_info *fde_clki = mclk->ufs_fde_clki;
2075 	struct regulator *reg;
2076 	int volt, ret = 0;
2077 	bool clk_bind_vcore = false;
2078 	bool clk_fde_scale = false;
2079 
2080 	if (!hba->clk_scaling.is_initialized)
2081 		return;
2082 
2083 	if (!clki || !fde_clki)
2084 		return;
2085 
2086 	reg = host->mclk.reg_vcore;
2087 	volt = host->mclk.vcore_volt;
2088 	if (reg && volt != 0)
2089 		clk_bind_vcore = true;
2090 
2091 	if (mclk->ufs_fde_max_clki && mclk->ufs_fde_min_clki)
2092 		clk_fde_scale = true;
2093 
2094 	ret = clk_prepare_enable(clki->clk);
2095 	if (ret) {
2096 		dev_info(hba->dev,
2097 			 "clk_prepare_enable() fail, ret: %d\n", ret);
2098 		return;
2099 	}
2100 
2101 	if (clk_fde_scale) {
2102 		ret = clk_prepare_enable(fde_clki->clk);
2103 		if (ret) {
2104 			dev_info(hba->dev,
2105 				 "fde clk_prepare_enable() fail, ret: %d\n", ret);
2106 			return;
2107 		}
2108 	}
2109 
2110 	if (scale_up) {
2111 		if (clk_bind_vcore) {
2112 			ret = regulator_set_voltage(reg, volt, INT_MAX);
2113 			if (ret) {
2114 				dev_info(hba->dev,
2115 					"Failed to set vcore to %d\n", volt);
2116 				goto out;
2117 			}
2118 		}
2119 
2120 		ret = clk_set_parent(clki->clk, mclk->ufs_sel_max_clki->clk);
2121 		if (ret) {
2122 			dev_info(hba->dev, "Failed to set clk mux, ret = %d\n",
2123 				ret);
2124 		}
2125 
2126 		if (clk_fde_scale) {
2127 			ret = clk_set_parent(fde_clki->clk,
2128 				mclk->ufs_fde_max_clki->clk);
2129 			if (ret) {
2130 				dev_info(hba->dev,
2131 					"Failed to set fde clk mux, ret = %d\n",
2132 					ret);
2133 			}
2134 		}
2135 	} else {
2136 		if (clk_fde_scale) {
2137 			ret = clk_set_parent(fde_clki->clk,
2138 				mclk->ufs_fde_min_clki->clk);
2139 			if (ret) {
2140 				dev_info(hba->dev,
2141 					"Failed to set fde clk mux, ret = %d\n",
2142 					ret);
2143 				goto out;
2144 			}
2145 		}
2146 
2147 		ret = clk_set_parent(clki->clk, mclk->ufs_sel_min_clki->clk);
2148 		if (ret) {
2149 			dev_info(hba->dev, "Failed to set clk mux, ret = %d\n",
2150 				ret);
2151 			goto out;
2152 		}
2153 
2154 		if (clk_bind_vcore) {
2155 			ret = regulator_set_voltage(reg, 0, INT_MAX);
2156 			if (ret) {
2157 				dev_info(hba->dev,
2158 					"failed to set vcore to MIN\n");
2159 			}
2160 		}
2161 	}
2162 
2163 out:
2164 	clk_disable_unprepare(clki->clk);
2165 
2166 	if (clk_fde_scale)
2167 		clk_disable_unprepare(fde_clki->clk);
2168 }
2169 
2170 /**
2171  * ufs_mtk_clk_scale - Internal clk scaling operation
2172  *
2173  * MTK platform supports clk scaling by switching parent of ufs_sel(mux).
2174  * The ufs_sel downstream to ufs_ck which feeds directly to UFS hardware.
2175  * Max and min clocks rate of ufs_sel defined in dts should match rate of
2176  * "ufs_sel_max_src" and "ufs_sel_min_src" respectively.
2177  * This prevent changing rate of pll clock that is shared between modules.
2178  *
2179  * @hba: per adapter instance
2180  * @scale_up: True for scaling up and false for scaling down
2181  */
2182 static void ufs_mtk_clk_scale(struct ufs_hba *hba, bool scale_up)
2183 {
2184 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
2185 	struct ufs_mtk_clk *mclk = &host->mclk;
2186 	struct ufs_clk_info *clki = mclk->ufs_sel_clki;
2187 
2188 	if (host->clk_scale_up == scale_up)
2189 		goto out;
2190 
2191 	if (scale_up)
2192 		_ufs_mtk_clk_scale(hba, true);
2193 	else
2194 		_ufs_mtk_clk_scale(hba, false);
2195 
2196 	host->clk_scale_up = scale_up;
2197 
2198 	/* Must always set before clk_set_rate() */
2199 	if (scale_up)
2200 		clki->curr_freq = clki->max_freq;
2201 	else
2202 		clki->curr_freq = clki->min_freq;
2203 out:
2204 	trace_ufs_mtk_clk_scale(clki->name, scale_up, clk_get_rate(clki->clk));
2205 }
2206 
2207 static int ufs_mtk_clk_scale_notify(struct ufs_hba *hba, bool scale_up,
2208 				    unsigned long target_freq,
2209 				    enum ufs_notify_change_status status)
2210 {
2211 	if (!ufshcd_is_clkscaling_supported(hba))
2212 		return 0;
2213 
2214 	if (status == PRE_CHANGE) {
2215 		/* Switch parent before clk_set_rate() */
2216 		ufs_mtk_clk_scale(hba, scale_up);
2217 	} else {
2218 		/* Request interrupt latency QoS accordingly */
2219 		ufs_mtk_scale_perf(hba, scale_up);
2220 	}
2221 
2222 	return 0;
2223 }
2224 
2225 static int ufs_mtk_get_hba_nortt(struct ufs_hba *hba)
2226 {
2227 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
2228 
2229 	if (host->legacy_ip_ver || host->ip_ver < IP_VER_MT6995_B0)
2230 		return MTK_MAX_NUM_RTT_LEGACY;
2231 
2232 	return FIELD_GET(MASK_NUMBER_OUTSTANDING_RTT, hba->capabilities) + 1;
2233 }
2234 
2235 static int ufs_mtk_get_hba_mac(struct ufs_hba *hba)
2236 {
2237 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
2238 
2239 	/* MCQ operation not permitted */
2240 	if (host->caps & UFS_MTK_CAP_DISABLE_MCQ)
2241 		return -EPERM;
2242 
2243 	return MAX_SUPP_MAC;
2244 }
2245 
2246 static int ufs_mtk_op_runtime_config(struct ufs_hba *hba)
2247 {
2248 	struct ufshcd_mcq_opr_info_t *opr;
2249 	int i;
2250 
2251 	hba->mcq_opr[OPR_SQD].offset = REG_UFS_MTK_SQD;
2252 	hba->mcq_opr[OPR_SQIS].offset = REG_UFS_MTK_SQIS;
2253 	hba->mcq_opr[OPR_CQD].offset = REG_UFS_MTK_CQD;
2254 	hba->mcq_opr[OPR_CQIS].offset = REG_UFS_MTK_CQIS;
2255 
2256 	for (i = 0; i < OPR_MAX; i++) {
2257 		opr = &hba->mcq_opr[i];
2258 		opr->stride = REG_UFS_MCQ_STRIDE;
2259 		opr->base = hba->mmio_base + opr->offset;
2260 	}
2261 
2262 	return 0;
2263 }
2264 
2265 static int ufs_mtk_mcq_config_resource(struct ufs_hba *hba)
2266 {
2267 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
2268 
2269 	/* fail mcq initialization if interrupt is not filled properly */
2270 	if (!host->mcq_nr_intr) {
2271 		dev_info(hba->dev, "IRQs not ready. MCQ disabled.");
2272 		return -EINVAL;
2273 	}
2274 
2275 	hba->mcq_base = hba->mmio_base + MCQ_QUEUE_OFFSET(hba->mcq_capabilities);
2276 	return 0;
2277 }
2278 
2279 static irqreturn_t ufs_mtk_mcq_intr(int irq, void *__intr_info)
2280 {
2281 	struct ufs_mtk_mcq_intr_info *mcq_intr_info = __intr_info;
2282 	struct ufs_hba *hba = mcq_intr_info->hba;
2283 	struct ufs_hw_queue *hwq;
2284 	u32 events;
2285 	int qid = mcq_intr_info->qid;
2286 
2287 	hwq = &hba->uhq[qid];
2288 
2289 	events = ufshcd_mcq_read_cqis(hba, qid);
2290 	if (events)
2291 		ufshcd_mcq_write_cqis(hba, events, qid);
2292 
2293 	if (events & UFSHCD_MCQ_CQIS_TAIL_ENT_PUSH_STS)
2294 		ufshcd_mcq_poll_cqe_lock(hba, hwq);
2295 
2296 	return IRQ_HANDLED;
2297 }
2298 
2299 static int ufs_mtk_config_mcq_irq(struct ufs_hba *hba)
2300 {
2301 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
2302 	u32 irq, i;
2303 	int ret;
2304 
2305 	for (i = 0; i < host->mcq_nr_intr; i++) {
2306 		irq = host->mcq_intr_info[i].irq;
2307 		if (irq == MTK_MCQ_INVALID_IRQ) {
2308 			dev_err(hba->dev, "invalid irq. %d\n", i);
2309 			return -ENOPARAM;
2310 		}
2311 
2312 		host->mcq_intr_info[i].qid = i;
2313 		ret = devm_request_irq(hba->dev, irq, ufs_mtk_mcq_intr, 0, UFSHCD,
2314 				       &host->mcq_intr_info[i]);
2315 
2316 		dev_dbg(hba->dev, "request irq %d intr %s\n", irq, ret ? "failed" : "");
2317 
2318 		if (ret)
2319 			return ret;
2320 	}
2321 	host->is_mcq_intr_enabled = true;
2322 
2323 	return 0;
2324 }
2325 
2326 static int ufs_mtk_config_mcq(struct ufs_hba *hba, bool irq)
2327 {
2328 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
2329 	int ret = 0;
2330 
2331 	if (!host->mcq_set_intr) {
2332 		/* Disable irq option register */
2333 		ufshcd_rmwl(hba, MCQ_INTR_EN_MSK, 0, REG_UFS_MMIO_OPT_CTRL_0);
2334 
2335 		if (irq) {
2336 			ret = ufs_mtk_config_mcq_irq(hba);
2337 			if (ret)
2338 				return ret;
2339 		}
2340 
2341 		host->mcq_set_intr = true;
2342 	}
2343 
2344 	ufshcd_rmwl(hba, MCQ_AH8, MCQ_AH8, REG_UFS_MMIO_OPT_CTRL_0);
2345 	ufshcd_rmwl(hba, MCQ_INTR_EN_MSK, MCQ_MULTI_INTR_EN, REG_UFS_MMIO_OPT_CTRL_0);
2346 
2347 	return 0;
2348 }
2349 
2350 static int ufs_mtk_config_esi(struct ufs_hba *hba)
2351 {
2352 	return ufs_mtk_config_mcq(hba, true);
2353 }
2354 
2355 static void ufs_mtk_config_scsi_dev(struct scsi_device *sdev)
2356 {
2357 	struct ufs_hba *hba = shost_priv(sdev->host);
2358 
2359 	dev_dbg(hba->dev, "lu %llu scsi device configured", sdev->lun);
2360 	if (sdev->lun == 2)
2361 		blk_queue_flag_set(QUEUE_FLAG_SAME_FORCE, sdev->request_queue);
2362 }
2363 
2364 /*
2365  * struct ufs_hba_mtk_vops - UFS MTK specific variant operations
2366  *
2367  * The variant operations configure the necessary controller and PHY
2368  * handshake during initialization.
2369  */
2370 static const struct ufs_hba_variant_ops ufs_hba_mtk_vops = {
2371 	.name                = "mediatek.ufshci",
2372 	.init                = ufs_mtk_init,
2373 	.exit                = ufs_mtk_exit,
2374 	.get_ufs_hci_version = ufs_mtk_get_ufs_hci_version,
2375 	.setup_clocks        = ufs_mtk_setup_clocks,
2376 	.hce_enable_notify   = ufs_mtk_hce_enable_notify,
2377 	.link_startup_notify = ufs_mtk_link_startup_notify,
2378 	.negotiate_pwr_mode  = ufs_mtk_negotiate_pwr_mode,
2379 	.pwr_change_notify   = ufs_mtk_pwr_change_notify,
2380 	.apply_dev_quirks    = ufs_mtk_apply_dev_quirks,
2381 	.fixup_dev_quirks    = ufs_mtk_fixup_dev_quirks,
2382 	.suspend             = ufs_mtk_suspend,
2383 	.resume              = ufs_mtk_resume,
2384 	.dbg_register_dump   = ufs_mtk_dbg_register_dump,
2385 	.device_reset        = ufs_mtk_device_reset,
2386 	.event_notify        = ufs_mtk_event_notify,
2387 	.config_scaling_param = ufs_mtk_config_scaling_param,
2388 	.clk_scale_notify    = ufs_mtk_clk_scale_notify,
2389 	.get_hba_nortt       = ufs_mtk_get_hba_nortt,
2390 	/* mcq vops */
2391 	.get_hba_mac         = ufs_mtk_get_hba_mac,
2392 	.op_runtime_config   = ufs_mtk_op_runtime_config,
2393 	.mcq_config_resource = ufs_mtk_mcq_config_resource,
2394 	.config_esi          = ufs_mtk_config_esi,
2395 	.config_scsi_dev     = ufs_mtk_config_scsi_dev,
2396 };
2397 
2398 /**
2399  * ufs_mtk_probe - probe routine of the driver
2400  * @pdev: pointer to Platform device handle
2401  *
2402  * Return: zero for success and non-zero for failure.
2403  */
2404 static int ufs_mtk_probe(struct platform_device *pdev)
2405 {
2406 	int err;
2407 	struct device *dev = &pdev->dev, *phy_dev = NULL;
2408 	struct device_node *reset_node, *phy_node = NULL;
2409 	struct platform_device *reset_pdev, *phy_pdev = NULL;
2410 	struct device_link *link;
2411 	struct ufs_hba *hba;
2412 	struct ufs_mtk_host *host;
2413 
2414 	reset_node = of_find_compatible_node(NULL, NULL,
2415 					     "ti,syscon-reset");
2416 	if (!reset_node) {
2417 		dev_notice(dev, "find ti,syscon-reset fail\n");
2418 		goto skip_reset;
2419 	}
2420 	reset_pdev = of_find_device_by_node(reset_node);
2421 	if (!reset_pdev) {
2422 		dev_notice(dev, "find reset_pdev fail\n");
2423 		goto skip_reset;
2424 	}
2425 	link = device_link_add(dev, &reset_pdev->dev,
2426 		DL_FLAG_AUTOPROBE_CONSUMER);
2427 	put_device(&reset_pdev->dev);
2428 	if (!link) {
2429 		dev_notice(dev, "add reset device_link fail\n");
2430 		goto skip_reset;
2431 	}
2432 	/* supplier is not probed */
2433 	if (link->status == DL_STATE_DORMANT) {
2434 		err = -EPROBE_DEFER;
2435 		goto out;
2436 	}
2437 
2438 skip_reset:
2439 	/* find phy node */
2440 	phy_node = of_parse_phandle(dev->of_node, "phys", 0);
2441 
2442 	if (phy_node) {
2443 		phy_pdev = of_find_device_by_node(phy_node);
2444 		if (!phy_pdev)
2445 			goto skip_phy;
2446 		phy_dev = &phy_pdev->dev;
2447 
2448 		pm_runtime_set_active(phy_dev);
2449 		pm_runtime_enable(phy_dev);
2450 		pm_runtime_get_sync(phy_dev);
2451 
2452 		put_device(phy_dev);
2453 		dev_info(dev, "phys node found\n");
2454 	} else {
2455 		dev_notice(dev, "phys node not found\n");
2456 	}
2457 
2458 skip_phy:
2459 	/* perform generic probe */
2460 	err = ufshcd_pltfrm_init(pdev, &ufs_hba_mtk_vops);
2461 	if (err) {
2462 		dev_err(dev, "probe failed %d\n", err);
2463 		goto out;
2464 	}
2465 
2466 	hba = platform_get_drvdata(pdev);
2467 	if (!hba)
2468 		goto out;
2469 
2470 	if (phy_node && phy_dev) {
2471 		host = ufshcd_get_variant(hba);
2472 		host->phy_dev = phy_dev;
2473 	}
2474 
2475 	/*
2476 	 * Because the default power setting of VSx (the upper layer of
2477 	 * VCCQ/VCCQ2) is HWLP, we need to prevent VCCQ/VCCQ2 from
2478 	 * entering LPM.
2479 	 */
2480 	ufs_mtk_dev_vreg_set_lpm(hba, false);
2481 
2482 out:
2483 	of_node_put(phy_node);
2484 	of_node_put(reset_node);
2485 	return err;
2486 }
2487 
2488 /**
2489  * ufs_mtk_remove - set driver_data of the device to NULL
2490  * @pdev: pointer to platform device handle
2491  *
2492  * Always return 0
2493  */
2494 static void ufs_mtk_remove(struct platform_device *pdev)
2495 {
2496 	ufshcd_pltfrm_remove(pdev);
2497 }
2498 
2499 static int ufs_mtk_system_suspend(struct device *dev)
2500 {
2501 	struct ufs_hba *hba = dev_get_drvdata(dev);
2502 	struct arm_smccc_res res;
2503 	int ret;
2504 
2505 	if (hba->shutting_down) {
2506 		ret = -EBUSY;
2507 		goto out;
2508 	}
2509 
2510 	ret = ufshcd_system_suspend(dev);
2511 	if (ret)
2512 		goto out;
2513 
2514 	if (pm_runtime_suspended(hba->dev))
2515 		goto out;
2516 
2517 	ufs_mtk_dev_vreg_set_lpm(hba, true);
2518 
2519 	if (ufs_mtk_is_rtff_mtcmos(hba))
2520 		ufs_mtk_mtcmos_ctrl(false, res);
2521 
2522 out:
2523 	return ret;
2524 }
2525 
2526 static int ufs_mtk_system_resume(struct device *dev)
2527 {
2528 	int ret = 0;
2529 	struct ufs_hba *hba = dev_get_drvdata(dev);
2530 	struct arm_smccc_res res;
2531 
2532 	if (pm_runtime_suspended(hba->dev))
2533 		goto out;
2534 
2535 	if (ufs_mtk_is_rtff_mtcmos(hba))
2536 		ufs_mtk_mtcmos_ctrl(true, res);
2537 
2538 	ufs_mtk_dev_vreg_set_lpm(hba, false);
2539 
2540 out:
2541 	ret = ufshcd_system_resume(dev);
2542 
2543 	return ret;
2544 }
2545 
2546 static int ufs_mtk_runtime_suspend(struct device *dev)
2547 {
2548 	struct ufs_hba *hba = dev_get_drvdata(dev);
2549 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
2550 	struct arm_smccc_res res;
2551 	int ret = 0;
2552 
2553 	ret = ufshcd_runtime_suspend(dev);
2554 	if (ret)
2555 		return ret;
2556 
2557 	ufs_mtk_dev_vreg_set_lpm(hba, true);
2558 
2559 	if (ufs_mtk_is_rtff_mtcmos(hba))
2560 		ufs_mtk_mtcmos_ctrl(false, res);
2561 
2562 	if (host->phy_dev)
2563 		pm_runtime_put_sync(host->phy_dev);
2564 
2565 	return 0;
2566 }
2567 
2568 static int ufs_mtk_runtime_resume(struct device *dev)
2569 {
2570 	struct ufs_hba *hba = dev_get_drvdata(dev);
2571 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
2572 	struct arm_smccc_res res;
2573 
2574 	if (ufs_mtk_is_rtff_mtcmos(hba))
2575 		ufs_mtk_mtcmos_ctrl(true, res);
2576 
2577 	if (host->phy_dev)
2578 		pm_runtime_get_sync(host->phy_dev);
2579 
2580 	ufs_mtk_dev_vreg_set_lpm(hba, false);
2581 
2582 	return ufshcd_runtime_resume(dev);
2583 }
2584 
2585 static const struct dev_pm_ops ufs_mtk_pm_ops = {
2586 	SYSTEM_SLEEP_PM_OPS(ufs_mtk_system_suspend, ufs_mtk_system_resume)
2587 	RUNTIME_PM_OPS(ufs_mtk_runtime_suspend, ufs_mtk_runtime_resume, NULL)
2588 	.prepare	 = ufshcd_suspend_prepare,
2589 	.complete	 = ufshcd_resume_complete,
2590 };
2591 
2592 static struct platform_driver ufs_mtk_pltform = {
2593 	.probe      = ufs_mtk_probe,
2594 	.remove = ufs_mtk_remove,
2595 	.driver = {
2596 		.name   = "ufshcd-mtk",
2597 		.pm     = pm_ptr(&ufs_mtk_pm_ops),
2598 		.of_match_table = ufs_mtk_of_match,
2599 	},
2600 };
2601 
2602 MODULE_AUTHOR("Stanley Chu <stanley.chu@mediatek.com>");
2603 MODULE_AUTHOR("Peter Wang <peter.wang@mediatek.com>");
2604 MODULE_DESCRIPTION("MediaTek UFS Host Driver");
2605 MODULE_LICENSE("GPL v2");
2606 
2607 module_platform_driver(ufs_mtk_pltform);
2608