xref: /openbmc/linux/drivers/mmc/host/sdhci-msm.c (revision e0d07278)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * drivers/mmc/host/sdhci-msm.c - Qualcomm SDHCI Platform driver
4  *
5  * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
6  */
7 
8 #include <linux/module.h>
9 #include <linux/of_device.h>
10 #include <linux/delay.h>
11 #include <linux/mmc/mmc.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/pm_opp.h>
14 #include <linux/slab.h>
15 #include <linux/iopoll.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/interconnect.h>
18 #include <linux/pinctrl/consumer.h>
19 
20 #include "sdhci-pltfm.h"
21 #include "cqhci.h"
22 
23 #define CORE_MCI_VERSION		0x50
24 #define CORE_VERSION_MAJOR_SHIFT	28
25 #define CORE_VERSION_MAJOR_MASK		(0xf << CORE_VERSION_MAJOR_SHIFT)
26 #define CORE_VERSION_MINOR_MASK		0xff
27 
28 #define CORE_MCI_GENERICS		0x70
29 #define SWITCHABLE_SIGNALING_VOLTAGE	BIT(29)
30 
31 #define HC_MODE_EN		0x1
32 #define CORE_POWER		0x0
33 #define CORE_SW_RST		BIT(7)
34 #define FF_CLK_SW_RST_DIS	BIT(13)
35 
36 #define CORE_PWRCTL_BUS_OFF	BIT(0)
37 #define CORE_PWRCTL_BUS_ON	BIT(1)
38 #define CORE_PWRCTL_IO_LOW	BIT(2)
39 #define CORE_PWRCTL_IO_HIGH	BIT(3)
40 #define CORE_PWRCTL_BUS_SUCCESS BIT(0)
41 #define CORE_PWRCTL_BUS_FAIL    BIT(1)
42 #define CORE_PWRCTL_IO_SUCCESS	BIT(2)
43 #define CORE_PWRCTL_IO_FAIL     BIT(3)
44 #define REQ_BUS_OFF		BIT(0)
45 #define REQ_BUS_ON		BIT(1)
46 #define REQ_IO_LOW		BIT(2)
47 #define REQ_IO_HIGH		BIT(3)
48 #define INT_MASK		0xf
49 #define MAX_PHASES		16
50 #define CORE_DLL_LOCK		BIT(7)
51 #define CORE_DDR_DLL_LOCK	BIT(11)
52 #define CORE_DLL_EN		BIT(16)
53 #define CORE_CDR_EN		BIT(17)
54 #define CORE_CK_OUT_EN		BIT(18)
55 #define CORE_CDR_EXT_EN		BIT(19)
56 #define CORE_DLL_PDN		BIT(29)
57 #define CORE_DLL_RST		BIT(30)
58 #define CORE_CMD_DAT_TRACK_SEL	BIT(0)
59 
60 #define CORE_DDR_CAL_EN		BIT(0)
61 #define CORE_FLL_CYCLE_CNT	BIT(18)
62 #define CORE_DLL_CLOCK_DISABLE	BIT(21)
63 
64 #define DLL_USR_CTL_POR_VAL	0x10800
65 #define ENABLE_DLL_LOCK_STATUS	BIT(26)
66 #define FINE_TUNE_MODE_EN	BIT(27)
67 #define BIAS_OK_SIGNAL		BIT(29)
68 
69 #define DLL_CONFIG_3_LOW_FREQ_VAL	0x08
70 #define DLL_CONFIG_3_HIGH_FREQ_VAL	0x10
71 
72 #define CORE_VENDOR_SPEC_POR_VAL 0xa9c
73 #define CORE_CLK_PWRSAVE	BIT(1)
74 #define CORE_HC_MCLK_SEL_DFLT	(2 << 8)
75 #define CORE_HC_MCLK_SEL_HS400	(3 << 8)
76 #define CORE_HC_MCLK_SEL_MASK	(3 << 8)
77 #define CORE_IO_PAD_PWR_SWITCH_EN	BIT(15)
78 #define CORE_IO_PAD_PWR_SWITCH	BIT(16)
79 #define CORE_HC_SELECT_IN_EN	BIT(18)
80 #define CORE_HC_SELECT_IN_HS400	(6 << 19)
81 #define CORE_HC_SELECT_IN_MASK	(7 << 19)
82 
83 #define CORE_3_0V_SUPPORT	BIT(25)
84 #define CORE_1_8V_SUPPORT	BIT(26)
85 #define CORE_VOLT_SUPPORT	(CORE_3_0V_SUPPORT | CORE_1_8V_SUPPORT)
86 
87 #define CORE_CSR_CDC_CTLR_CFG0		0x130
88 #define CORE_SW_TRIG_FULL_CALIB		BIT(16)
89 #define CORE_HW_AUTOCAL_ENA		BIT(17)
90 
91 #define CORE_CSR_CDC_CTLR_CFG1		0x134
92 #define CORE_CSR_CDC_CAL_TIMER_CFG0	0x138
93 #define CORE_TIMER_ENA			BIT(16)
94 
95 #define CORE_CSR_CDC_CAL_TIMER_CFG1	0x13C
96 #define CORE_CSR_CDC_REFCOUNT_CFG	0x140
97 #define CORE_CSR_CDC_COARSE_CAL_CFG	0x144
98 #define CORE_CDC_OFFSET_CFG		0x14C
99 #define CORE_CSR_CDC_DELAY_CFG		0x150
100 #define CORE_CDC_SLAVE_DDA_CFG		0x160
101 #define CORE_CSR_CDC_STATUS0		0x164
102 #define CORE_CALIBRATION_DONE		BIT(0)
103 
104 #define CORE_CDC_ERROR_CODE_MASK	0x7000000
105 
106 #define CORE_CSR_CDC_GEN_CFG		0x178
107 #define CORE_CDC_SWITCH_BYPASS_OFF	BIT(0)
108 #define CORE_CDC_SWITCH_RC_EN		BIT(1)
109 
110 #define CORE_CDC_T4_DLY_SEL		BIT(0)
111 #define CORE_CMDIN_RCLK_EN		BIT(1)
112 #define CORE_START_CDC_TRAFFIC		BIT(6)
113 
114 #define CORE_PWRSAVE_DLL	BIT(3)
115 
116 #define DDR_CONFIG_POR_VAL	0x80040873
117 
118 
119 #define INVALID_TUNING_PHASE	-1
120 #define SDHCI_MSM_MIN_CLOCK	400000
121 #define CORE_FREQ_100MHZ	(100 * 1000 * 1000)
122 
123 #define CDR_SELEXT_SHIFT	20
124 #define CDR_SELEXT_MASK		(0xf << CDR_SELEXT_SHIFT)
125 #define CMUX_SHIFT_PHASE_SHIFT	24
126 #define CMUX_SHIFT_PHASE_MASK	(7 << CMUX_SHIFT_PHASE_SHIFT)
127 
128 #define MSM_MMC_AUTOSUSPEND_DELAY_MS	50
129 
130 /* Timeout value to avoid infinite waiting for pwr_irq */
131 #define MSM_PWR_IRQ_TIMEOUT_MS 5000
132 
133 /* Max load for eMMC Vdd-io supply */
134 #define MMC_VQMMC_MAX_LOAD_UA	325000
135 
136 #define msm_host_readl(msm_host, host, offset) \
137 	msm_host->var_ops->msm_readl_relaxed(host, offset)
138 
139 #define msm_host_writel(msm_host, val, host, offset) \
140 	msm_host->var_ops->msm_writel_relaxed(val, host, offset)
141 
142 /* CQHCI vendor specific registers */
143 #define CQHCI_VENDOR_CFG1	0xA00
144 #define CQHCI_VENDOR_DIS_RST_ON_CQ_EN	(0x3 << 13)
145 
146 struct sdhci_msm_offset {
147 	u32 core_hc_mode;
148 	u32 core_mci_data_cnt;
149 	u32 core_mci_status;
150 	u32 core_mci_fifo_cnt;
151 	u32 core_mci_version;
152 	u32 core_generics;
153 	u32 core_testbus_config;
154 	u32 core_testbus_sel2_bit;
155 	u32 core_testbus_ena;
156 	u32 core_testbus_sel2;
157 	u32 core_pwrctl_status;
158 	u32 core_pwrctl_mask;
159 	u32 core_pwrctl_clear;
160 	u32 core_pwrctl_ctl;
161 	u32 core_sdcc_debug_reg;
162 	u32 core_dll_config;
163 	u32 core_dll_status;
164 	u32 core_vendor_spec;
165 	u32 core_vendor_spec_adma_err_addr0;
166 	u32 core_vendor_spec_adma_err_addr1;
167 	u32 core_vendor_spec_func2;
168 	u32 core_vendor_spec_capabilities0;
169 	u32 core_ddr_200_cfg;
170 	u32 core_vendor_spec3;
171 	u32 core_dll_config_2;
172 	u32 core_dll_config_3;
173 	u32 core_ddr_config_old; /* Applicable to sdcc minor ver < 0x49 */
174 	u32 core_ddr_config;
175 	u32 core_dll_usr_ctl; /* Present on SDCC5.1 onwards */
176 };
177 
178 static const struct sdhci_msm_offset sdhci_msm_v5_offset = {
179 	.core_mci_data_cnt = 0x35c,
180 	.core_mci_status = 0x324,
181 	.core_mci_fifo_cnt = 0x308,
182 	.core_mci_version = 0x318,
183 	.core_generics = 0x320,
184 	.core_testbus_config = 0x32c,
185 	.core_testbus_sel2_bit = 3,
186 	.core_testbus_ena = (1 << 31),
187 	.core_testbus_sel2 = (1 << 3),
188 	.core_pwrctl_status = 0x240,
189 	.core_pwrctl_mask = 0x244,
190 	.core_pwrctl_clear = 0x248,
191 	.core_pwrctl_ctl = 0x24c,
192 	.core_sdcc_debug_reg = 0x358,
193 	.core_dll_config = 0x200,
194 	.core_dll_status = 0x208,
195 	.core_vendor_spec = 0x20c,
196 	.core_vendor_spec_adma_err_addr0 = 0x214,
197 	.core_vendor_spec_adma_err_addr1 = 0x218,
198 	.core_vendor_spec_func2 = 0x210,
199 	.core_vendor_spec_capabilities0 = 0x21c,
200 	.core_ddr_200_cfg = 0x224,
201 	.core_vendor_spec3 = 0x250,
202 	.core_dll_config_2 = 0x254,
203 	.core_dll_config_3 = 0x258,
204 	.core_ddr_config = 0x25c,
205 	.core_dll_usr_ctl = 0x388,
206 };
207 
208 static const struct sdhci_msm_offset sdhci_msm_mci_offset = {
209 	.core_hc_mode = 0x78,
210 	.core_mci_data_cnt = 0x30,
211 	.core_mci_status = 0x34,
212 	.core_mci_fifo_cnt = 0x44,
213 	.core_mci_version = 0x050,
214 	.core_generics = 0x70,
215 	.core_testbus_config = 0x0cc,
216 	.core_testbus_sel2_bit = 4,
217 	.core_testbus_ena = (1 << 3),
218 	.core_testbus_sel2 = (1 << 4),
219 	.core_pwrctl_status = 0xdc,
220 	.core_pwrctl_mask = 0xe0,
221 	.core_pwrctl_clear = 0xe4,
222 	.core_pwrctl_ctl = 0xe8,
223 	.core_sdcc_debug_reg = 0x124,
224 	.core_dll_config = 0x100,
225 	.core_dll_status = 0x108,
226 	.core_vendor_spec = 0x10c,
227 	.core_vendor_spec_adma_err_addr0 = 0x114,
228 	.core_vendor_spec_adma_err_addr1 = 0x118,
229 	.core_vendor_spec_func2 = 0x110,
230 	.core_vendor_spec_capabilities0 = 0x11c,
231 	.core_ddr_200_cfg = 0x184,
232 	.core_vendor_spec3 = 0x1b0,
233 	.core_dll_config_2 = 0x1b4,
234 	.core_ddr_config_old = 0x1b8,
235 	.core_ddr_config = 0x1bc,
236 };
237 
238 struct sdhci_msm_variant_ops {
239 	u32 (*msm_readl_relaxed)(struct sdhci_host *host, u32 offset);
240 	void (*msm_writel_relaxed)(u32 val, struct sdhci_host *host,
241 			u32 offset);
242 };
243 
244 /*
245  * From V5, register spaces have changed. Wrap this info in a structure
246  * and choose the data_structure based on version info mentioned in DT.
247  */
248 struct sdhci_msm_variant_info {
249 	bool mci_removed;
250 	bool restore_dll_config;
251 	bool uses_tassadar_dll;
252 	const struct sdhci_msm_variant_ops *var_ops;
253 	const struct sdhci_msm_offset *offset;
254 };
255 
256 struct sdhci_msm_host {
257 	struct platform_device *pdev;
258 	void __iomem *core_mem;	/* MSM SDCC mapped address */
259 	int pwr_irq;		/* power irq */
260 	struct clk *bus_clk;	/* SDHC bus voter clock */
261 	struct clk *xo_clk;	/* TCXO clk needed for FLL feature of cm_dll*/
262 	struct clk_bulk_data bulk_clks[4]; /* core, iface, cal, sleep clocks */
263 	unsigned long clk_rate;
264 	struct mmc_host *mmc;
265 	struct opp_table *opp_table;
266 	bool has_opp_table;
267 	bool use_14lpp_dll_reset;
268 	bool tuning_done;
269 	bool calibration_done;
270 	u8 saved_tuning_phase;
271 	bool use_cdclp533;
272 	u32 curr_pwr_state;
273 	u32 curr_io_level;
274 	wait_queue_head_t pwr_irq_wait;
275 	bool pwr_irq_flag;
276 	u32 caps_0;
277 	bool mci_removed;
278 	bool restore_dll_config;
279 	const struct sdhci_msm_variant_ops *var_ops;
280 	const struct sdhci_msm_offset *offset;
281 	bool use_cdr;
282 	u32 transfer_mode;
283 	bool updated_ddr_cfg;
284 	bool uses_tassadar_dll;
285 	u32 dll_config;
286 	u32 ddr_config;
287 	bool vqmmc_enabled;
288 };
289 
290 static const struct sdhci_msm_offset *sdhci_priv_msm_offset(struct sdhci_host *host)
291 {
292 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
293 	struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
294 
295 	return msm_host->offset;
296 }
297 
298 /*
299  * APIs to read/write to vendor specific registers which were there in the
300  * core_mem region before MCI was removed.
301  */
302 static u32 sdhci_msm_mci_variant_readl_relaxed(struct sdhci_host *host,
303 		u32 offset)
304 {
305 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
306 	struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
307 
308 	return readl_relaxed(msm_host->core_mem + offset);
309 }
310 
311 static u32 sdhci_msm_v5_variant_readl_relaxed(struct sdhci_host *host,
312 		u32 offset)
313 {
314 	return readl_relaxed(host->ioaddr + offset);
315 }
316 
317 static void sdhci_msm_mci_variant_writel_relaxed(u32 val,
318 		struct sdhci_host *host, u32 offset)
319 {
320 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
321 	struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
322 
323 	writel_relaxed(val, msm_host->core_mem + offset);
324 }
325 
326 static void sdhci_msm_v5_variant_writel_relaxed(u32 val,
327 		struct sdhci_host *host, u32 offset)
328 {
329 	writel_relaxed(val, host->ioaddr + offset);
330 }
331 
332 static unsigned int msm_get_clock_rate_for_bus_mode(struct sdhci_host *host,
333 						    unsigned int clock)
334 {
335 	struct mmc_ios ios = host->mmc->ios;
336 	/*
337 	 * The SDHC requires internal clock frequency to be double the
338 	 * actual clock that will be set for DDR mode. The controller
339 	 * uses the faster clock(100/400MHz) for some of its parts and
340 	 * send the actual required clock (50/200MHz) to the card.
341 	 */
342 	if (ios.timing == MMC_TIMING_UHS_DDR50 ||
343 	    ios.timing == MMC_TIMING_MMC_DDR52 ||
344 	    ios.timing == MMC_TIMING_MMC_HS400 ||
345 	    host->flags & SDHCI_HS400_TUNING)
346 		clock *= 2;
347 	return clock;
348 }
349 
350 static void msm_set_clock_rate_for_bus_mode(struct sdhci_host *host,
351 					    unsigned int clock)
352 {
353 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
354 	struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
355 	struct mmc_ios curr_ios = host->mmc->ios;
356 	struct clk *core_clk = msm_host->bulk_clks[0].clk;
357 	int rc;
358 
359 	clock = msm_get_clock_rate_for_bus_mode(host, clock);
360 	rc = dev_pm_opp_set_rate(mmc_dev(host->mmc), clock);
361 	if (rc) {
362 		pr_err("%s: Failed to set clock at rate %u at timing %d\n",
363 		       mmc_hostname(host->mmc), clock,
364 		       curr_ios.timing);
365 		return;
366 	}
367 	msm_host->clk_rate = clock;
368 	pr_debug("%s: Setting clock at rate %lu at timing %d\n",
369 		 mmc_hostname(host->mmc), clk_get_rate(core_clk),
370 		 curr_ios.timing);
371 }
372 
373 /* Platform specific tuning */
374 static inline int msm_dll_poll_ck_out_en(struct sdhci_host *host, u8 poll)
375 {
376 	u32 wait_cnt = 50;
377 	u8 ck_out_en;
378 	struct mmc_host *mmc = host->mmc;
379 	const struct sdhci_msm_offset *msm_offset =
380 					sdhci_priv_msm_offset(host);
381 
382 	/* Poll for CK_OUT_EN bit.  max. poll time = 50us */
383 	ck_out_en = !!(readl_relaxed(host->ioaddr +
384 			msm_offset->core_dll_config) & CORE_CK_OUT_EN);
385 
386 	while (ck_out_en != poll) {
387 		if (--wait_cnt == 0) {
388 			dev_err(mmc_dev(mmc), "%s: CK_OUT_EN bit is not %d\n",
389 			       mmc_hostname(mmc), poll);
390 			return -ETIMEDOUT;
391 		}
392 		udelay(1);
393 
394 		ck_out_en = !!(readl_relaxed(host->ioaddr +
395 			msm_offset->core_dll_config) & CORE_CK_OUT_EN);
396 	}
397 
398 	return 0;
399 }
400 
401 static int msm_config_cm_dll_phase(struct sdhci_host *host, u8 phase)
402 {
403 	int rc;
404 	static const u8 grey_coded_phase_table[] = {
405 		0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4,
406 		0xc, 0xd, 0xf, 0xe, 0xa, 0xb, 0x9, 0x8
407 	};
408 	unsigned long flags;
409 	u32 config;
410 	struct mmc_host *mmc = host->mmc;
411 	const struct sdhci_msm_offset *msm_offset =
412 					sdhci_priv_msm_offset(host);
413 
414 	if (phase > 0xf)
415 		return -EINVAL;
416 
417 	spin_lock_irqsave(&host->lock, flags);
418 
419 	config = readl_relaxed(host->ioaddr + msm_offset->core_dll_config);
420 	config &= ~(CORE_CDR_EN | CORE_CK_OUT_EN);
421 	config |= (CORE_CDR_EXT_EN | CORE_DLL_EN);
422 	writel_relaxed(config, host->ioaddr + msm_offset->core_dll_config);
423 
424 	/* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '0' */
425 	rc = msm_dll_poll_ck_out_en(host, 0);
426 	if (rc)
427 		goto err_out;
428 
429 	/*
430 	 * Write the selected DLL clock output phase (0 ... 15)
431 	 * to CDR_SELEXT bit field of DLL_CONFIG register.
432 	 */
433 	config = readl_relaxed(host->ioaddr + msm_offset->core_dll_config);
434 	config &= ~CDR_SELEXT_MASK;
435 	config |= grey_coded_phase_table[phase] << CDR_SELEXT_SHIFT;
436 	writel_relaxed(config, host->ioaddr + msm_offset->core_dll_config);
437 
438 	config = readl_relaxed(host->ioaddr + msm_offset->core_dll_config);
439 	config |= CORE_CK_OUT_EN;
440 	writel_relaxed(config, host->ioaddr + msm_offset->core_dll_config);
441 
442 	/* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '1' */
443 	rc = msm_dll_poll_ck_out_en(host, 1);
444 	if (rc)
445 		goto err_out;
446 
447 	config = readl_relaxed(host->ioaddr + msm_offset->core_dll_config);
448 	config |= CORE_CDR_EN;
449 	config &= ~CORE_CDR_EXT_EN;
450 	writel_relaxed(config, host->ioaddr + msm_offset->core_dll_config);
451 	goto out;
452 
453 err_out:
454 	dev_err(mmc_dev(mmc), "%s: Failed to set DLL phase: %d\n",
455 	       mmc_hostname(mmc), phase);
456 out:
457 	spin_unlock_irqrestore(&host->lock, flags);
458 	return rc;
459 }
460 
461 /*
462  * Find out the greatest range of consecuitive selected
463  * DLL clock output phases that can be used as sampling
464  * setting for SD3.0 UHS-I card read operation (in SDR104
465  * timing mode) or for eMMC4.5 card read operation (in
466  * HS400/HS200 timing mode).
467  * Select the 3/4 of the range and configure the DLL with the
468  * selected DLL clock output phase.
469  */
470 
471 static int msm_find_most_appropriate_phase(struct sdhci_host *host,
472 					   u8 *phase_table, u8 total_phases)
473 {
474 	int ret;
475 	u8 ranges[MAX_PHASES][MAX_PHASES] = { {0}, {0} };
476 	u8 phases_per_row[MAX_PHASES] = { 0 };
477 	int row_index = 0, col_index = 0, selected_row_index = 0, curr_max = 0;
478 	int i, cnt, phase_0_raw_index = 0, phase_15_raw_index = 0;
479 	bool phase_0_found = false, phase_15_found = false;
480 	struct mmc_host *mmc = host->mmc;
481 
482 	if (!total_phases || (total_phases > MAX_PHASES)) {
483 		dev_err(mmc_dev(mmc), "%s: Invalid argument: total_phases=%d\n",
484 		       mmc_hostname(mmc), total_phases);
485 		return -EINVAL;
486 	}
487 
488 	for (cnt = 0; cnt < total_phases; cnt++) {
489 		ranges[row_index][col_index] = phase_table[cnt];
490 		phases_per_row[row_index] += 1;
491 		col_index++;
492 
493 		if ((cnt + 1) == total_phases) {
494 			continue;
495 		/* check if next phase in phase_table is consecutive or not */
496 		} else if ((phase_table[cnt] + 1) != phase_table[cnt + 1]) {
497 			row_index++;
498 			col_index = 0;
499 		}
500 	}
501 
502 	if (row_index >= MAX_PHASES)
503 		return -EINVAL;
504 
505 	/* Check if phase-0 is present in first valid window? */
506 	if (!ranges[0][0]) {
507 		phase_0_found = true;
508 		phase_0_raw_index = 0;
509 		/* Check if cycle exist between 2 valid windows */
510 		for (cnt = 1; cnt <= row_index; cnt++) {
511 			if (phases_per_row[cnt]) {
512 				for (i = 0; i < phases_per_row[cnt]; i++) {
513 					if (ranges[cnt][i] == 15) {
514 						phase_15_found = true;
515 						phase_15_raw_index = cnt;
516 						break;
517 					}
518 				}
519 			}
520 		}
521 	}
522 
523 	/* If 2 valid windows form cycle then merge them as single window */
524 	if (phase_0_found && phase_15_found) {
525 		/* number of phases in raw where phase 0 is present */
526 		u8 phases_0 = phases_per_row[phase_0_raw_index];
527 		/* number of phases in raw where phase 15 is present */
528 		u8 phases_15 = phases_per_row[phase_15_raw_index];
529 
530 		if (phases_0 + phases_15 >= MAX_PHASES)
531 			/*
532 			 * If there are more than 1 phase windows then total
533 			 * number of phases in both the windows should not be
534 			 * more than or equal to MAX_PHASES.
535 			 */
536 			return -EINVAL;
537 
538 		/* Merge 2 cyclic windows */
539 		i = phases_15;
540 		for (cnt = 0; cnt < phases_0; cnt++) {
541 			ranges[phase_15_raw_index][i] =
542 			    ranges[phase_0_raw_index][cnt];
543 			if (++i >= MAX_PHASES)
544 				break;
545 		}
546 
547 		phases_per_row[phase_0_raw_index] = 0;
548 		phases_per_row[phase_15_raw_index] = phases_15 + phases_0;
549 	}
550 
551 	for (cnt = 0; cnt <= row_index; cnt++) {
552 		if (phases_per_row[cnt] > curr_max) {
553 			curr_max = phases_per_row[cnt];
554 			selected_row_index = cnt;
555 		}
556 	}
557 
558 	i = (curr_max * 3) / 4;
559 	if (i)
560 		i--;
561 
562 	ret = ranges[selected_row_index][i];
563 
564 	if (ret >= MAX_PHASES) {
565 		ret = -EINVAL;
566 		dev_err(mmc_dev(mmc), "%s: Invalid phase selected=%d\n",
567 		       mmc_hostname(mmc), ret);
568 	}
569 
570 	return ret;
571 }
572 
573 static inline void msm_cm_dll_set_freq(struct sdhci_host *host)
574 {
575 	u32 mclk_freq = 0, config;
576 	const struct sdhci_msm_offset *msm_offset =
577 					sdhci_priv_msm_offset(host);
578 
579 	/* Program the MCLK value to MCLK_FREQ bit field */
580 	if (host->clock <= 112000000)
581 		mclk_freq = 0;
582 	else if (host->clock <= 125000000)
583 		mclk_freq = 1;
584 	else if (host->clock <= 137000000)
585 		mclk_freq = 2;
586 	else if (host->clock <= 150000000)
587 		mclk_freq = 3;
588 	else if (host->clock <= 162000000)
589 		mclk_freq = 4;
590 	else if (host->clock <= 175000000)
591 		mclk_freq = 5;
592 	else if (host->clock <= 187000000)
593 		mclk_freq = 6;
594 	else if (host->clock <= 200000000)
595 		mclk_freq = 7;
596 
597 	config = readl_relaxed(host->ioaddr + msm_offset->core_dll_config);
598 	config &= ~CMUX_SHIFT_PHASE_MASK;
599 	config |= mclk_freq << CMUX_SHIFT_PHASE_SHIFT;
600 	writel_relaxed(config, host->ioaddr + msm_offset->core_dll_config);
601 }
602 
603 /* Initialize the DLL (Programmable Delay Line) */
604 static int msm_init_cm_dll(struct sdhci_host *host)
605 {
606 	struct mmc_host *mmc = host->mmc;
607 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
608 	struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
609 	int wait_cnt = 50;
610 	unsigned long flags, xo_clk = 0;
611 	u32 config;
612 	const struct sdhci_msm_offset *msm_offset =
613 					msm_host->offset;
614 
615 	if (msm_host->use_14lpp_dll_reset && !IS_ERR_OR_NULL(msm_host->xo_clk))
616 		xo_clk = clk_get_rate(msm_host->xo_clk);
617 
618 	spin_lock_irqsave(&host->lock, flags);
619 
620 	/*
621 	 * Make sure that clock is always enabled when DLL
622 	 * tuning is in progress. Keeping PWRSAVE ON may
623 	 * turn off the clock.
624 	 */
625 	config = readl_relaxed(host->ioaddr + msm_offset->core_vendor_spec);
626 	config &= ~CORE_CLK_PWRSAVE;
627 	writel_relaxed(config, host->ioaddr + msm_offset->core_vendor_spec);
628 
629 	if (msm_host->dll_config)
630 		writel_relaxed(msm_host->dll_config,
631 				host->ioaddr + msm_offset->core_dll_config);
632 
633 	if (msm_host->use_14lpp_dll_reset) {
634 		config = readl_relaxed(host->ioaddr +
635 				msm_offset->core_dll_config);
636 		config &= ~CORE_CK_OUT_EN;
637 		writel_relaxed(config, host->ioaddr +
638 				msm_offset->core_dll_config);
639 
640 		config = readl_relaxed(host->ioaddr +
641 				msm_offset->core_dll_config_2);
642 		config |= CORE_DLL_CLOCK_DISABLE;
643 		writel_relaxed(config, host->ioaddr +
644 				msm_offset->core_dll_config_2);
645 	}
646 
647 	config = readl_relaxed(host->ioaddr +
648 			msm_offset->core_dll_config);
649 	config |= CORE_DLL_RST;
650 	writel_relaxed(config, host->ioaddr +
651 			msm_offset->core_dll_config);
652 
653 	config = readl_relaxed(host->ioaddr +
654 			msm_offset->core_dll_config);
655 	config |= CORE_DLL_PDN;
656 	writel_relaxed(config, host->ioaddr +
657 			msm_offset->core_dll_config);
658 
659 	if (!msm_host->dll_config)
660 		msm_cm_dll_set_freq(host);
661 
662 	if (msm_host->use_14lpp_dll_reset &&
663 	    !IS_ERR_OR_NULL(msm_host->xo_clk)) {
664 		u32 mclk_freq = 0;
665 
666 		config = readl_relaxed(host->ioaddr +
667 				msm_offset->core_dll_config_2);
668 		config &= CORE_FLL_CYCLE_CNT;
669 		if (config)
670 			mclk_freq = DIV_ROUND_CLOSEST_ULL((host->clock * 8),
671 					xo_clk);
672 		else
673 			mclk_freq = DIV_ROUND_CLOSEST_ULL((host->clock * 4),
674 					xo_clk);
675 
676 		config = readl_relaxed(host->ioaddr +
677 				msm_offset->core_dll_config_2);
678 		config &= ~(0xFF << 10);
679 		config |= mclk_freq << 10;
680 
681 		writel_relaxed(config, host->ioaddr +
682 				msm_offset->core_dll_config_2);
683 		/* wait for 5us before enabling DLL clock */
684 		udelay(5);
685 	}
686 
687 	config = readl_relaxed(host->ioaddr +
688 			msm_offset->core_dll_config);
689 	config &= ~CORE_DLL_RST;
690 	writel_relaxed(config, host->ioaddr +
691 			msm_offset->core_dll_config);
692 
693 	config = readl_relaxed(host->ioaddr +
694 			msm_offset->core_dll_config);
695 	config &= ~CORE_DLL_PDN;
696 	writel_relaxed(config, host->ioaddr +
697 			msm_offset->core_dll_config);
698 
699 	if (msm_host->use_14lpp_dll_reset) {
700 		if (!msm_host->dll_config)
701 			msm_cm_dll_set_freq(host);
702 		config = readl_relaxed(host->ioaddr +
703 				msm_offset->core_dll_config_2);
704 		config &= ~CORE_DLL_CLOCK_DISABLE;
705 		writel_relaxed(config, host->ioaddr +
706 				msm_offset->core_dll_config_2);
707 	}
708 
709 	/*
710 	 * Configure DLL user control register to enable DLL status.
711 	 * This setting is applicable to SDCC v5.1 onwards only.
712 	 */
713 	if (msm_host->uses_tassadar_dll) {
714 		config = DLL_USR_CTL_POR_VAL | FINE_TUNE_MODE_EN |
715 			ENABLE_DLL_LOCK_STATUS | BIAS_OK_SIGNAL;
716 		writel_relaxed(config, host->ioaddr +
717 				msm_offset->core_dll_usr_ctl);
718 
719 		config = readl_relaxed(host->ioaddr +
720 				msm_offset->core_dll_config_3);
721 		config &= ~0xFF;
722 		if (msm_host->clk_rate < 150000000)
723 			config |= DLL_CONFIG_3_LOW_FREQ_VAL;
724 		else
725 			config |= DLL_CONFIG_3_HIGH_FREQ_VAL;
726 		writel_relaxed(config, host->ioaddr +
727 			msm_offset->core_dll_config_3);
728 	}
729 
730 	config = readl_relaxed(host->ioaddr +
731 			msm_offset->core_dll_config);
732 	config |= CORE_DLL_EN;
733 	writel_relaxed(config, host->ioaddr +
734 			msm_offset->core_dll_config);
735 
736 	config = readl_relaxed(host->ioaddr +
737 			msm_offset->core_dll_config);
738 	config |= CORE_CK_OUT_EN;
739 	writel_relaxed(config, host->ioaddr +
740 			msm_offset->core_dll_config);
741 
742 	/* Wait until DLL_LOCK bit of DLL_STATUS register becomes '1' */
743 	while (!(readl_relaxed(host->ioaddr + msm_offset->core_dll_status) &
744 		 CORE_DLL_LOCK)) {
745 		/* max. wait for 50us sec for LOCK bit to be set */
746 		if (--wait_cnt == 0) {
747 			dev_err(mmc_dev(mmc), "%s: DLL failed to LOCK\n",
748 			       mmc_hostname(mmc));
749 			spin_unlock_irqrestore(&host->lock, flags);
750 			return -ETIMEDOUT;
751 		}
752 		udelay(1);
753 	}
754 
755 	spin_unlock_irqrestore(&host->lock, flags);
756 	return 0;
757 }
758 
759 static void msm_hc_select_default(struct sdhci_host *host)
760 {
761 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
762 	struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
763 	u32 config;
764 	const struct sdhci_msm_offset *msm_offset =
765 					msm_host->offset;
766 
767 	if (!msm_host->use_cdclp533) {
768 		config = readl_relaxed(host->ioaddr +
769 				msm_offset->core_vendor_spec3);
770 		config &= ~CORE_PWRSAVE_DLL;
771 		writel_relaxed(config, host->ioaddr +
772 				msm_offset->core_vendor_spec3);
773 	}
774 
775 	config = readl_relaxed(host->ioaddr + msm_offset->core_vendor_spec);
776 	config &= ~CORE_HC_MCLK_SEL_MASK;
777 	config |= CORE_HC_MCLK_SEL_DFLT;
778 	writel_relaxed(config, host->ioaddr + msm_offset->core_vendor_spec);
779 
780 	/*
781 	 * Disable HC_SELECT_IN to be able to use the UHS mode select
782 	 * configuration from Host Control2 register for all other
783 	 * modes.
784 	 * Write 0 to HC_SELECT_IN and HC_SELECT_IN_EN field
785 	 * in VENDOR_SPEC_FUNC
786 	 */
787 	config = readl_relaxed(host->ioaddr + msm_offset->core_vendor_spec);
788 	config &= ~CORE_HC_SELECT_IN_EN;
789 	config &= ~CORE_HC_SELECT_IN_MASK;
790 	writel_relaxed(config, host->ioaddr + msm_offset->core_vendor_spec);
791 
792 	/*
793 	 * Make sure above writes impacting free running MCLK are completed
794 	 * before changing the clk_rate at GCC.
795 	 */
796 	wmb();
797 }
798 
799 static void msm_hc_select_hs400(struct sdhci_host *host)
800 {
801 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
802 	struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
803 	struct mmc_ios ios = host->mmc->ios;
804 	u32 config, dll_lock;
805 	int rc;
806 	const struct sdhci_msm_offset *msm_offset =
807 					msm_host->offset;
808 
809 	/* Select the divided clock (free running MCLK/2) */
810 	config = readl_relaxed(host->ioaddr + msm_offset->core_vendor_spec);
811 	config &= ~CORE_HC_MCLK_SEL_MASK;
812 	config |= CORE_HC_MCLK_SEL_HS400;
813 
814 	writel_relaxed(config, host->ioaddr + msm_offset->core_vendor_spec);
815 	/*
816 	 * Select HS400 mode using the HC_SELECT_IN from VENDOR SPEC
817 	 * register
818 	 */
819 	if ((msm_host->tuning_done || ios.enhanced_strobe) &&
820 	    !msm_host->calibration_done) {
821 		config = readl_relaxed(host->ioaddr +
822 				msm_offset->core_vendor_spec);
823 		config |= CORE_HC_SELECT_IN_HS400;
824 		config |= CORE_HC_SELECT_IN_EN;
825 		writel_relaxed(config, host->ioaddr +
826 				msm_offset->core_vendor_spec);
827 	}
828 	if (!msm_host->clk_rate && !msm_host->use_cdclp533) {
829 		/*
830 		 * Poll on DLL_LOCK or DDR_DLL_LOCK bits in
831 		 * core_dll_status to be set. This should get set
832 		 * within 15 us at 200 MHz.
833 		 */
834 		rc = readl_relaxed_poll_timeout(host->ioaddr +
835 						msm_offset->core_dll_status,
836 						dll_lock,
837 						(dll_lock &
838 						(CORE_DLL_LOCK |
839 						CORE_DDR_DLL_LOCK)), 10,
840 						1000);
841 		if (rc == -ETIMEDOUT)
842 			pr_err("%s: Unable to get DLL_LOCK/DDR_DLL_LOCK, dll_status: 0x%08x\n",
843 			       mmc_hostname(host->mmc), dll_lock);
844 	}
845 	/*
846 	 * Make sure above writes impacting free running MCLK are completed
847 	 * before changing the clk_rate at GCC.
848 	 */
849 	wmb();
850 }
851 
852 /*
853  * sdhci_msm_hc_select_mode :- In general all timing modes are
854  * controlled via UHS mode select in Host Control2 register.
855  * eMMC specific HS200/HS400 doesn't have their respective modes
856  * defined here, hence we use these values.
857  *
858  * HS200 - SDR104 (Since they both are equivalent in functionality)
859  * HS400 - This involves multiple configurations
860  *		Initially SDR104 - when tuning is required as HS200
861  *		Then when switching to DDR @ 400MHz (HS400) we use
862  *		the vendor specific HC_SELECT_IN to control the mode.
863  *
864  * In addition to controlling the modes we also need to select the
865  * correct input clock for DLL depending on the mode.
866  *
867  * HS400 - divided clock (free running MCLK/2)
868  * All other modes - default (free running MCLK)
869  */
870 static void sdhci_msm_hc_select_mode(struct sdhci_host *host)
871 {
872 	struct mmc_ios ios = host->mmc->ios;
873 
874 	if (ios.timing == MMC_TIMING_MMC_HS400 ||
875 	    host->flags & SDHCI_HS400_TUNING)
876 		msm_hc_select_hs400(host);
877 	else
878 		msm_hc_select_default(host);
879 }
880 
881 static int sdhci_msm_cdclp533_calibration(struct sdhci_host *host)
882 {
883 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
884 	struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
885 	u32 config, calib_done;
886 	int ret;
887 	const struct sdhci_msm_offset *msm_offset =
888 					msm_host->offset;
889 
890 	pr_debug("%s: %s: Enter\n", mmc_hostname(host->mmc), __func__);
891 
892 	/*
893 	 * Retuning in HS400 (DDR mode) will fail, just reset the
894 	 * tuning block and restore the saved tuning phase.
895 	 */
896 	ret = msm_init_cm_dll(host);
897 	if (ret)
898 		goto out;
899 
900 	/* Set the selected phase in delay line hw block */
901 	ret = msm_config_cm_dll_phase(host, msm_host->saved_tuning_phase);
902 	if (ret)
903 		goto out;
904 
905 	config = readl_relaxed(host->ioaddr + msm_offset->core_dll_config);
906 	config |= CORE_CMD_DAT_TRACK_SEL;
907 	writel_relaxed(config, host->ioaddr + msm_offset->core_dll_config);
908 
909 	config = readl_relaxed(host->ioaddr + msm_offset->core_ddr_200_cfg);
910 	config &= ~CORE_CDC_T4_DLY_SEL;
911 	writel_relaxed(config, host->ioaddr + msm_offset->core_ddr_200_cfg);
912 
913 	config = readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG);
914 	config &= ~CORE_CDC_SWITCH_BYPASS_OFF;
915 	writel_relaxed(config, host->ioaddr + CORE_CSR_CDC_GEN_CFG);
916 
917 	config = readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG);
918 	config |= CORE_CDC_SWITCH_RC_EN;
919 	writel_relaxed(config, host->ioaddr + CORE_CSR_CDC_GEN_CFG);
920 
921 	config = readl_relaxed(host->ioaddr + msm_offset->core_ddr_200_cfg);
922 	config &= ~CORE_START_CDC_TRAFFIC;
923 	writel_relaxed(config, host->ioaddr + msm_offset->core_ddr_200_cfg);
924 
925 	/* Perform CDC Register Initialization Sequence */
926 
927 	writel_relaxed(0x11800EC, host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
928 	writel_relaxed(0x3011111, host->ioaddr + CORE_CSR_CDC_CTLR_CFG1);
929 	writel_relaxed(0x1201000, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
930 	writel_relaxed(0x4, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG1);
931 	writel_relaxed(0xCB732020, host->ioaddr + CORE_CSR_CDC_REFCOUNT_CFG);
932 	writel_relaxed(0xB19, host->ioaddr + CORE_CSR_CDC_COARSE_CAL_CFG);
933 	writel_relaxed(0x4E2, host->ioaddr + CORE_CSR_CDC_DELAY_CFG);
934 	writel_relaxed(0x0, host->ioaddr + CORE_CDC_OFFSET_CFG);
935 	writel_relaxed(0x16334, host->ioaddr + CORE_CDC_SLAVE_DDA_CFG);
936 
937 	/* CDC HW Calibration */
938 
939 	config = readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
940 	config |= CORE_SW_TRIG_FULL_CALIB;
941 	writel_relaxed(config, host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
942 
943 	config = readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
944 	config &= ~CORE_SW_TRIG_FULL_CALIB;
945 	writel_relaxed(config, host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
946 
947 	config = readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
948 	config |= CORE_HW_AUTOCAL_ENA;
949 	writel_relaxed(config, host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
950 
951 	config = readl_relaxed(host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
952 	config |= CORE_TIMER_ENA;
953 	writel_relaxed(config, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
954 
955 	ret = readl_relaxed_poll_timeout(host->ioaddr + CORE_CSR_CDC_STATUS0,
956 					 calib_done,
957 					 (calib_done & CORE_CALIBRATION_DONE),
958 					 1, 50);
959 
960 	if (ret == -ETIMEDOUT) {
961 		pr_err("%s: %s: CDC calibration was not completed\n",
962 		       mmc_hostname(host->mmc), __func__);
963 		goto out;
964 	}
965 
966 	ret = readl_relaxed(host->ioaddr + CORE_CSR_CDC_STATUS0)
967 			& CORE_CDC_ERROR_CODE_MASK;
968 	if (ret) {
969 		pr_err("%s: %s: CDC error code %d\n",
970 		       mmc_hostname(host->mmc), __func__, ret);
971 		ret = -EINVAL;
972 		goto out;
973 	}
974 
975 	config = readl_relaxed(host->ioaddr + msm_offset->core_ddr_200_cfg);
976 	config |= CORE_START_CDC_TRAFFIC;
977 	writel_relaxed(config, host->ioaddr + msm_offset->core_ddr_200_cfg);
978 out:
979 	pr_debug("%s: %s: Exit, ret %d\n", mmc_hostname(host->mmc),
980 		 __func__, ret);
981 	return ret;
982 }
983 
984 static int sdhci_msm_cm_dll_sdc4_calibration(struct sdhci_host *host)
985 {
986 	struct mmc_host *mmc = host->mmc;
987 	u32 dll_status, config, ddr_cfg_offset;
988 	int ret;
989 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
990 	struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
991 	const struct sdhci_msm_offset *msm_offset =
992 					sdhci_priv_msm_offset(host);
993 
994 	pr_debug("%s: %s: Enter\n", mmc_hostname(host->mmc), __func__);
995 
996 	/*
997 	 * Currently the core_ddr_config register defaults to desired
998 	 * configuration on reset. Currently reprogramming the power on
999 	 * reset (POR) value in case it might have been modified by
1000 	 * bootloaders. In the future, if this changes, then the desired
1001 	 * values will need to be programmed appropriately.
1002 	 */
1003 	if (msm_host->updated_ddr_cfg)
1004 		ddr_cfg_offset = msm_offset->core_ddr_config;
1005 	else
1006 		ddr_cfg_offset = msm_offset->core_ddr_config_old;
1007 	writel_relaxed(msm_host->ddr_config, host->ioaddr + ddr_cfg_offset);
1008 
1009 	if (mmc->ios.enhanced_strobe) {
1010 		config = readl_relaxed(host->ioaddr +
1011 				msm_offset->core_ddr_200_cfg);
1012 		config |= CORE_CMDIN_RCLK_EN;
1013 		writel_relaxed(config, host->ioaddr +
1014 				msm_offset->core_ddr_200_cfg);
1015 	}
1016 
1017 	config = readl_relaxed(host->ioaddr + msm_offset->core_dll_config_2);
1018 	config |= CORE_DDR_CAL_EN;
1019 	writel_relaxed(config, host->ioaddr + msm_offset->core_dll_config_2);
1020 
1021 	ret = readl_relaxed_poll_timeout(host->ioaddr +
1022 					msm_offset->core_dll_status,
1023 					dll_status,
1024 					(dll_status & CORE_DDR_DLL_LOCK),
1025 					10, 1000);
1026 
1027 	if (ret == -ETIMEDOUT) {
1028 		pr_err("%s: %s: CM_DLL_SDC4 calibration was not completed\n",
1029 		       mmc_hostname(host->mmc), __func__);
1030 		goto out;
1031 	}
1032 
1033 	/*
1034 	 * Set CORE_PWRSAVE_DLL bit in CORE_VENDOR_SPEC3.
1035 	 * When MCLK is gated OFF, it is not gated for less than 0.5us
1036 	 * and MCLK must be switched on for at-least 1us before DATA
1037 	 * starts coming. Controllers with 14lpp and later tech DLL cannot
1038 	 * guarantee above requirement. So PWRSAVE_DLL should not be
1039 	 * turned on for host controllers using this DLL.
1040 	 */
1041 	if (!msm_host->use_14lpp_dll_reset) {
1042 		config = readl_relaxed(host->ioaddr +
1043 				msm_offset->core_vendor_spec3);
1044 		config |= CORE_PWRSAVE_DLL;
1045 		writel_relaxed(config, host->ioaddr +
1046 				msm_offset->core_vendor_spec3);
1047 	}
1048 
1049 	/*
1050 	 * Drain writebuffer to ensure above DLL calibration
1051 	 * and PWRSAVE DLL is enabled.
1052 	 */
1053 	wmb();
1054 out:
1055 	pr_debug("%s: %s: Exit, ret %d\n", mmc_hostname(host->mmc),
1056 		 __func__, ret);
1057 	return ret;
1058 }
1059 
1060 static int sdhci_msm_hs400_dll_calibration(struct sdhci_host *host)
1061 {
1062 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1063 	struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
1064 	struct mmc_host *mmc = host->mmc;
1065 	int ret;
1066 	u32 config;
1067 	const struct sdhci_msm_offset *msm_offset =
1068 					msm_host->offset;
1069 
1070 	pr_debug("%s: %s: Enter\n", mmc_hostname(host->mmc), __func__);
1071 
1072 	/*
1073 	 * Retuning in HS400 (DDR mode) will fail, just reset the
1074 	 * tuning block and restore the saved tuning phase.
1075 	 */
1076 	ret = msm_init_cm_dll(host);
1077 	if (ret)
1078 		goto out;
1079 
1080 	if (!mmc->ios.enhanced_strobe) {
1081 		/* Set the selected phase in delay line hw block */
1082 		ret = msm_config_cm_dll_phase(host,
1083 					      msm_host->saved_tuning_phase);
1084 		if (ret)
1085 			goto out;
1086 		config = readl_relaxed(host->ioaddr +
1087 				msm_offset->core_dll_config);
1088 		config |= CORE_CMD_DAT_TRACK_SEL;
1089 		writel_relaxed(config, host->ioaddr +
1090 				msm_offset->core_dll_config);
1091 	}
1092 
1093 	if (msm_host->use_cdclp533)
1094 		ret = sdhci_msm_cdclp533_calibration(host);
1095 	else
1096 		ret = sdhci_msm_cm_dll_sdc4_calibration(host);
1097 out:
1098 	pr_debug("%s: %s: Exit, ret %d\n", mmc_hostname(host->mmc),
1099 		 __func__, ret);
1100 	return ret;
1101 }
1102 
1103 static bool sdhci_msm_is_tuning_needed(struct sdhci_host *host)
1104 {
1105 	struct mmc_ios *ios = &host->mmc->ios;
1106 
1107 	/*
1108 	 * Tuning is required for SDR104, HS200 and HS400 cards and
1109 	 * if clock frequency is greater than 100MHz in these modes.
1110 	 */
1111 	if (host->clock <= CORE_FREQ_100MHZ ||
1112 	    !(ios->timing == MMC_TIMING_MMC_HS400 ||
1113 	    ios->timing == MMC_TIMING_MMC_HS200 ||
1114 	    ios->timing == MMC_TIMING_UHS_SDR104) ||
1115 	    ios->enhanced_strobe)
1116 		return false;
1117 
1118 	return true;
1119 }
1120 
1121 static int sdhci_msm_restore_sdr_dll_config(struct sdhci_host *host)
1122 {
1123 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1124 	struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
1125 	int ret;
1126 
1127 	/*
1128 	 * SDR DLL comes into picture only for timing modes which needs
1129 	 * tuning.
1130 	 */
1131 	if (!sdhci_msm_is_tuning_needed(host))
1132 		return 0;
1133 
1134 	/* Reset the tuning block */
1135 	ret = msm_init_cm_dll(host);
1136 	if (ret)
1137 		return ret;
1138 
1139 	/* Restore the tuning block */
1140 	ret = msm_config_cm_dll_phase(host, msm_host->saved_tuning_phase);
1141 
1142 	return ret;
1143 }
1144 
1145 static void sdhci_msm_set_cdr(struct sdhci_host *host, bool enable)
1146 {
1147 	const struct sdhci_msm_offset *msm_offset = sdhci_priv_msm_offset(host);
1148 	u32 config, oldconfig = readl_relaxed(host->ioaddr +
1149 					      msm_offset->core_dll_config);
1150 
1151 	config = oldconfig;
1152 	if (enable) {
1153 		config |= CORE_CDR_EN;
1154 		config &= ~CORE_CDR_EXT_EN;
1155 	} else {
1156 		config &= ~CORE_CDR_EN;
1157 		config |= CORE_CDR_EXT_EN;
1158 	}
1159 
1160 	if (config != oldconfig) {
1161 		writel_relaxed(config, host->ioaddr +
1162 			       msm_offset->core_dll_config);
1163 	}
1164 }
1165 
1166 static int sdhci_msm_execute_tuning(struct mmc_host *mmc, u32 opcode)
1167 {
1168 	struct sdhci_host *host = mmc_priv(mmc);
1169 	int tuning_seq_cnt = 3;
1170 	u8 phase, tuned_phases[16], tuned_phase_cnt = 0;
1171 	int rc;
1172 	struct mmc_ios ios = host->mmc->ios;
1173 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1174 	struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
1175 
1176 	if (!sdhci_msm_is_tuning_needed(host)) {
1177 		msm_host->use_cdr = false;
1178 		sdhci_msm_set_cdr(host, false);
1179 		return 0;
1180 	}
1181 
1182 	/* Clock-Data-Recovery used to dynamically adjust RX sampling point */
1183 	msm_host->use_cdr = true;
1184 
1185 	/*
1186 	 * Clear tuning_done flag before tuning to ensure proper
1187 	 * HS400 settings.
1188 	 */
1189 	msm_host->tuning_done = 0;
1190 
1191 	/*
1192 	 * For HS400 tuning in HS200 timing requires:
1193 	 * - select MCLK/2 in VENDOR_SPEC
1194 	 * - program MCLK to 400MHz (or nearest supported) in GCC
1195 	 */
1196 	if (host->flags & SDHCI_HS400_TUNING) {
1197 		sdhci_msm_hc_select_mode(host);
1198 		msm_set_clock_rate_for_bus_mode(host, ios.clock);
1199 		host->flags &= ~SDHCI_HS400_TUNING;
1200 	}
1201 
1202 retry:
1203 	/* First of all reset the tuning block */
1204 	rc = msm_init_cm_dll(host);
1205 	if (rc)
1206 		return rc;
1207 
1208 	phase = 0;
1209 	do {
1210 		/* Set the phase in delay line hw block */
1211 		rc = msm_config_cm_dll_phase(host, phase);
1212 		if (rc)
1213 			return rc;
1214 
1215 		rc = mmc_send_tuning(mmc, opcode, NULL);
1216 		if (!rc) {
1217 			/* Tuning is successful at this tuning point */
1218 			tuned_phases[tuned_phase_cnt++] = phase;
1219 			dev_dbg(mmc_dev(mmc), "%s: Found good phase = %d\n",
1220 				 mmc_hostname(mmc), phase);
1221 		}
1222 	} while (++phase < ARRAY_SIZE(tuned_phases));
1223 
1224 	if (tuned_phase_cnt) {
1225 		rc = msm_find_most_appropriate_phase(host, tuned_phases,
1226 						     tuned_phase_cnt);
1227 		if (rc < 0)
1228 			return rc;
1229 		else
1230 			phase = rc;
1231 
1232 		/*
1233 		 * Finally set the selected phase in delay
1234 		 * line hw block.
1235 		 */
1236 		rc = msm_config_cm_dll_phase(host, phase);
1237 		if (rc)
1238 			return rc;
1239 		msm_host->saved_tuning_phase = phase;
1240 		dev_dbg(mmc_dev(mmc), "%s: Setting the tuning phase to %d\n",
1241 			 mmc_hostname(mmc), phase);
1242 	} else {
1243 		if (--tuning_seq_cnt)
1244 			goto retry;
1245 		/* Tuning failed */
1246 		dev_dbg(mmc_dev(mmc), "%s: No tuning point found\n",
1247 		       mmc_hostname(mmc));
1248 		rc = -EIO;
1249 	}
1250 
1251 	if (!rc)
1252 		msm_host->tuning_done = true;
1253 	return rc;
1254 }
1255 
1256 /*
1257  * sdhci_msm_hs400 - Calibrate the DLL for HS400 bus speed mode operation.
1258  * This needs to be done for both tuning and enhanced_strobe mode.
1259  * DLL operation is only needed for clock > 100MHz. For clock <= 100MHz
1260  * fixed feedback clock is used.
1261  */
1262 static void sdhci_msm_hs400(struct sdhci_host *host, struct mmc_ios *ios)
1263 {
1264 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1265 	struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
1266 	int ret;
1267 
1268 	if (host->clock > CORE_FREQ_100MHZ &&
1269 	    (msm_host->tuning_done || ios->enhanced_strobe) &&
1270 	    !msm_host->calibration_done) {
1271 		ret = sdhci_msm_hs400_dll_calibration(host);
1272 		if (!ret)
1273 			msm_host->calibration_done = true;
1274 		else
1275 			pr_err("%s: Failed to calibrate DLL for hs400 mode (%d)\n",
1276 			       mmc_hostname(host->mmc), ret);
1277 	}
1278 }
1279 
1280 static void sdhci_msm_set_uhs_signaling(struct sdhci_host *host,
1281 					unsigned int uhs)
1282 {
1283 	struct mmc_host *mmc = host->mmc;
1284 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1285 	struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
1286 	u16 ctrl_2;
1287 	u32 config;
1288 	const struct sdhci_msm_offset *msm_offset =
1289 					msm_host->offset;
1290 
1291 	ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1292 	/* Select Bus Speed Mode for host */
1293 	ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1294 	switch (uhs) {
1295 	case MMC_TIMING_UHS_SDR12:
1296 		ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1297 		break;
1298 	case MMC_TIMING_UHS_SDR25:
1299 		ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1300 		break;
1301 	case MMC_TIMING_UHS_SDR50:
1302 		ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1303 		break;
1304 	case MMC_TIMING_MMC_HS400:
1305 	case MMC_TIMING_MMC_HS200:
1306 	case MMC_TIMING_UHS_SDR104:
1307 		ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1308 		break;
1309 	case MMC_TIMING_UHS_DDR50:
1310 	case MMC_TIMING_MMC_DDR52:
1311 		ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1312 		break;
1313 	}
1314 
1315 	/*
1316 	 * When clock frequency is less than 100MHz, the feedback clock must be
1317 	 * provided and DLL must not be used so that tuning can be skipped. To
1318 	 * provide feedback clock, the mode selection can be any value less
1319 	 * than 3'b011 in bits [2:0] of HOST CONTROL2 register.
1320 	 */
1321 	if (host->clock <= CORE_FREQ_100MHZ) {
1322 		if (uhs == MMC_TIMING_MMC_HS400 ||
1323 		    uhs == MMC_TIMING_MMC_HS200 ||
1324 		    uhs == MMC_TIMING_UHS_SDR104)
1325 			ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1326 		/*
1327 		 * DLL is not required for clock <= 100MHz
1328 		 * Thus, make sure DLL it is disabled when not required
1329 		 */
1330 		config = readl_relaxed(host->ioaddr +
1331 				msm_offset->core_dll_config);
1332 		config |= CORE_DLL_RST;
1333 		writel_relaxed(config, host->ioaddr +
1334 				msm_offset->core_dll_config);
1335 
1336 		config = readl_relaxed(host->ioaddr +
1337 				msm_offset->core_dll_config);
1338 		config |= CORE_DLL_PDN;
1339 		writel_relaxed(config, host->ioaddr +
1340 				msm_offset->core_dll_config);
1341 
1342 		/*
1343 		 * The DLL needs to be restored and CDCLP533 recalibrated
1344 		 * when the clock frequency is set back to 400MHz.
1345 		 */
1346 		msm_host->calibration_done = false;
1347 	}
1348 
1349 	dev_dbg(mmc_dev(mmc), "%s: clock=%u uhs=%u ctrl_2=0x%x\n",
1350 		mmc_hostname(host->mmc), host->clock, uhs, ctrl_2);
1351 	sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1352 
1353 	if (mmc->ios.timing == MMC_TIMING_MMC_HS400)
1354 		sdhci_msm_hs400(host, &mmc->ios);
1355 }
1356 
1357 static int sdhci_msm_set_pincfg(struct sdhci_msm_host *msm_host, bool level)
1358 {
1359 	struct platform_device *pdev = msm_host->pdev;
1360 	int ret;
1361 
1362 	if (level)
1363 		ret = pinctrl_pm_select_default_state(&pdev->dev);
1364 	else
1365 		ret = pinctrl_pm_select_sleep_state(&pdev->dev);
1366 
1367 	return ret;
1368 }
1369 
1370 static int sdhci_msm_set_vmmc(struct mmc_host *mmc)
1371 {
1372 	if (IS_ERR(mmc->supply.vmmc))
1373 		return 0;
1374 
1375 	return mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, mmc->ios.vdd);
1376 }
1377 
1378 static int msm_toggle_vqmmc(struct sdhci_msm_host *msm_host,
1379 			      struct mmc_host *mmc, bool level)
1380 {
1381 	int ret;
1382 	struct mmc_ios ios;
1383 
1384 	if (msm_host->vqmmc_enabled == level)
1385 		return 0;
1386 
1387 	if (level) {
1388 		/* Set the IO voltage regulator to default voltage level */
1389 		if (msm_host->caps_0 & CORE_3_0V_SUPPORT)
1390 			ios.signal_voltage = MMC_SIGNAL_VOLTAGE_330;
1391 		else if (msm_host->caps_0 & CORE_1_8V_SUPPORT)
1392 			ios.signal_voltage = MMC_SIGNAL_VOLTAGE_180;
1393 
1394 		if (msm_host->caps_0 & CORE_VOLT_SUPPORT) {
1395 			ret = mmc_regulator_set_vqmmc(mmc, &ios);
1396 			if (ret < 0) {
1397 				dev_err(mmc_dev(mmc), "%s: vqmmc set volgate failed: %d\n",
1398 					mmc_hostname(mmc), ret);
1399 				goto out;
1400 			}
1401 		}
1402 		ret = regulator_enable(mmc->supply.vqmmc);
1403 	} else {
1404 		ret = regulator_disable(mmc->supply.vqmmc);
1405 	}
1406 
1407 	if (ret)
1408 		dev_err(mmc_dev(mmc), "%s: vqmm %sable failed: %d\n",
1409 			mmc_hostname(mmc), level ? "en":"dis", ret);
1410 	else
1411 		msm_host->vqmmc_enabled = level;
1412 out:
1413 	return ret;
1414 }
1415 
1416 static int msm_config_vqmmc_mode(struct sdhci_msm_host *msm_host,
1417 			      struct mmc_host *mmc, bool hpm)
1418 {
1419 	int load, ret;
1420 
1421 	load = hpm ? MMC_VQMMC_MAX_LOAD_UA : 0;
1422 	ret = regulator_set_load(mmc->supply.vqmmc, load);
1423 	if (ret)
1424 		dev_err(mmc_dev(mmc), "%s: vqmmc set load failed: %d\n",
1425 			mmc_hostname(mmc), ret);
1426 	return ret;
1427 }
1428 
1429 static int sdhci_msm_set_vqmmc(struct sdhci_msm_host *msm_host,
1430 			      struct mmc_host *mmc, bool level)
1431 {
1432 	int ret;
1433 	bool always_on;
1434 
1435 	if (IS_ERR(mmc->supply.vqmmc) ||
1436 			(mmc->ios.power_mode == MMC_POWER_UNDEFINED))
1437 		return 0;
1438 	/*
1439 	 * For eMMC don't turn off Vqmmc, Instead just configure it in LPM
1440 	 * and HPM modes by setting the corresponding load.
1441 	 *
1442 	 * Till eMMC is initialized (i.e. always_on == 0), just turn on/off
1443 	 * Vqmmc. Vqmmc gets turned off only if init fails and mmc_power_off
1444 	 * gets invoked. Once eMMC is initialized (i.e. always_on == 1),
1445 	 * Vqmmc should remain ON, So just set the load instead of turning it
1446 	 * off/on.
1447 	 */
1448 	always_on = !mmc_card_is_removable(mmc) &&
1449 			mmc->card && mmc_card_mmc(mmc->card);
1450 
1451 	if (always_on)
1452 		ret = msm_config_vqmmc_mode(msm_host, mmc, level);
1453 	else
1454 		ret = msm_toggle_vqmmc(msm_host, mmc, level);
1455 
1456 	return ret;
1457 }
1458 
1459 static inline void sdhci_msm_init_pwr_irq_wait(struct sdhci_msm_host *msm_host)
1460 {
1461 	init_waitqueue_head(&msm_host->pwr_irq_wait);
1462 }
1463 
1464 static inline void sdhci_msm_complete_pwr_irq_wait(
1465 		struct sdhci_msm_host *msm_host)
1466 {
1467 	wake_up(&msm_host->pwr_irq_wait);
1468 }
1469 
1470 /*
1471  * sdhci_msm_check_power_status API should be called when registers writes
1472  * which can toggle sdhci IO bus ON/OFF or change IO lines HIGH/LOW happens.
1473  * To what state the register writes will change the IO lines should be passed
1474  * as the argument req_type. This API will check whether the IO line's state
1475  * is already the expected state and will wait for power irq only if
1476  * power irq is expected to be triggered based on the current IO line state
1477  * and expected IO line state.
1478  */
1479 static void sdhci_msm_check_power_status(struct sdhci_host *host, u32 req_type)
1480 {
1481 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1482 	struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
1483 	bool done = false;
1484 	u32 val = SWITCHABLE_SIGNALING_VOLTAGE;
1485 	const struct sdhci_msm_offset *msm_offset =
1486 					msm_host->offset;
1487 
1488 	pr_debug("%s: %s: request %d curr_pwr_state %x curr_io_level %x\n",
1489 			mmc_hostname(host->mmc), __func__, req_type,
1490 			msm_host->curr_pwr_state, msm_host->curr_io_level);
1491 
1492 	/*
1493 	 * The power interrupt will not be generated for signal voltage
1494 	 * switches if SWITCHABLE_SIGNALING_VOLTAGE in MCI_GENERICS is not set.
1495 	 * Since sdhci-msm-v5, this bit has been removed and SW must consider
1496 	 * it as always set.
1497 	 */
1498 	if (!msm_host->mci_removed)
1499 		val = msm_host_readl(msm_host, host,
1500 				msm_offset->core_generics);
1501 	if ((req_type & REQ_IO_HIGH || req_type & REQ_IO_LOW) &&
1502 	    !(val & SWITCHABLE_SIGNALING_VOLTAGE)) {
1503 		return;
1504 	}
1505 
1506 	/*
1507 	 * The IRQ for request type IO High/LOW will be generated when -
1508 	 * there is a state change in 1.8V enable bit (bit 3) of
1509 	 * SDHCI_HOST_CONTROL2 register. The reset state of that bit is 0
1510 	 * which indicates 3.3V IO voltage. So, when MMC core layer tries
1511 	 * to set it to 3.3V before card detection happens, the
1512 	 * IRQ doesn't get triggered as there is no state change in this bit.
1513 	 * The driver already handles this case by changing the IO voltage
1514 	 * level to high as part of controller power up sequence. Hence, check
1515 	 * for host->pwr to handle a case where IO voltage high request is
1516 	 * issued even before controller power up.
1517 	 */
1518 	if ((req_type & REQ_IO_HIGH) && !host->pwr) {
1519 		pr_debug("%s: do not wait for power IRQ that never comes, req_type: %d\n",
1520 				mmc_hostname(host->mmc), req_type);
1521 		return;
1522 	}
1523 	if ((req_type & msm_host->curr_pwr_state) ||
1524 			(req_type & msm_host->curr_io_level))
1525 		done = true;
1526 	/*
1527 	 * This is needed here to handle cases where register writes will
1528 	 * not change the current bus state or io level of the controller.
1529 	 * In this case, no power irq will be triggerred and we should
1530 	 * not wait.
1531 	 */
1532 	if (!done) {
1533 		if (!wait_event_timeout(msm_host->pwr_irq_wait,
1534 				msm_host->pwr_irq_flag,
1535 				msecs_to_jiffies(MSM_PWR_IRQ_TIMEOUT_MS)))
1536 			dev_warn(&msm_host->pdev->dev,
1537 				 "%s: pwr_irq for req: (%d) timed out\n",
1538 				 mmc_hostname(host->mmc), req_type);
1539 	}
1540 	pr_debug("%s: %s: request %d done\n", mmc_hostname(host->mmc),
1541 			__func__, req_type);
1542 }
1543 
1544 static void sdhci_msm_dump_pwr_ctrl_regs(struct sdhci_host *host)
1545 {
1546 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1547 	struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
1548 	const struct sdhci_msm_offset *msm_offset =
1549 					msm_host->offset;
1550 
1551 	pr_err("%s: PWRCTL_STATUS: 0x%08x | PWRCTL_MASK: 0x%08x | PWRCTL_CTL: 0x%08x\n",
1552 		mmc_hostname(host->mmc),
1553 		msm_host_readl(msm_host, host, msm_offset->core_pwrctl_status),
1554 		msm_host_readl(msm_host, host, msm_offset->core_pwrctl_mask),
1555 		msm_host_readl(msm_host, host, msm_offset->core_pwrctl_ctl));
1556 }
1557 
1558 static void sdhci_msm_handle_pwr_irq(struct sdhci_host *host, int irq)
1559 {
1560 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1561 	struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
1562 	struct mmc_host *mmc = host->mmc;
1563 	u32 irq_status, irq_ack = 0;
1564 	int retry = 10, ret;
1565 	u32 pwr_state = 0, io_level = 0;
1566 	u32 config;
1567 	const struct sdhci_msm_offset *msm_offset = msm_host->offset;
1568 
1569 	irq_status = msm_host_readl(msm_host, host,
1570 			msm_offset->core_pwrctl_status);
1571 	irq_status &= INT_MASK;
1572 
1573 	msm_host_writel(msm_host, irq_status, host,
1574 			msm_offset->core_pwrctl_clear);
1575 
1576 	/*
1577 	 * There is a rare HW scenario where the first clear pulse could be
1578 	 * lost when actual reset and clear/read of status register is
1579 	 * happening at a time. Hence, retry for at least 10 times to make
1580 	 * sure status register is cleared. Otherwise, this will result in
1581 	 * a spurious power IRQ resulting in system instability.
1582 	 */
1583 	while (irq_status & msm_host_readl(msm_host, host,
1584 				msm_offset->core_pwrctl_status)) {
1585 		if (retry == 0) {
1586 			pr_err("%s: Timedout clearing (0x%x) pwrctl status register\n",
1587 					mmc_hostname(host->mmc), irq_status);
1588 			sdhci_msm_dump_pwr_ctrl_regs(host);
1589 			WARN_ON(1);
1590 			break;
1591 		}
1592 		msm_host_writel(msm_host, irq_status, host,
1593 			msm_offset->core_pwrctl_clear);
1594 		retry--;
1595 		udelay(10);
1596 	}
1597 
1598 	/* Handle BUS ON/OFF*/
1599 	if (irq_status & CORE_PWRCTL_BUS_ON) {
1600 		pwr_state = REQ_BUS_ON;
1601 		io_level = REQ_IO_HIGH;
1602 	}
1603 	if (irq_status & CORE_PWRCTL_BUS_OFF) {
1604 		pwr_state = REQ_BUS_OFF;
1605 		io_level = REQ_IO_LOW;
1606 	}
1607 
1608 	if (pwr_state) {
1609 		ret = sdhci_msm_set_vmmc(mmc);
1610 		if (!ret)
1611 			ret = sdhci_msm_set_vqmmc(msm_host, mmc,
1612 					pwr_state & REQ_BUS_ON);
1613 		if (!ret)
1614 			ret = sdhci_msm_set_pincfg(msm_host,
1615 					pwr_state & REQ_BUS_ON);
1616 		if (!ret)
1617 			irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
1618 		else
1619 			irq_ack |= CORE_PWRCTL_BUS_FAIL;
1620 	}
1621 
1622 	/* Handle IO LOW/HIGH */
1623 	if (irq_status & CORE_PWRCTL_IO_LOW)
1624 		io_level = REQ_IO_LOW;
1625 
1626 	if (irq_status & CORE_PWRCTL_IO_HIGH)
1627 		io_level = REQ_IO_HIGH;
1628 
1629 	if (io_level)
1630 		irq_ack |= CORE_PWRCTL_IO_SUCCESS;
1631 
1632 	if (io_level && !IS_ERR(mmc->supply.vqmmc) && !pwr_state) {
1633 		ret = mmc_regulator_set_vqmmc(mmc, &mmc->ios);
1634 		if (ret < 0) {
1635 			dev_err(mmc_dev(mmc), "%s: IO_level setting failed(%d). signal_voltage: %d, vdd: %d irq_status: 0x%08x\n",
1636 					mmc_hostname(mmc), ret,
1637 					mmc->ios.signal_voltage, mmc->ios.vdd,
1638 					irq_status);
1639 			irq_ack |= CORE_PWRCTL_IO_FAIL;
1640 		}
1641 	}
1642 
1643 	/*
1644 	 * The driver has to acknowledge the interrupt, switch voltages and
1645 	 * report back if it succeded or not to this register. The voltage
1646 	 * switches are handled by the sdhci core, so just report success.
1647 	 */
1648 	msm_host_writel(msm_host, irq_ack, host,
1649 			msm_offset->core_pwrctl_ctl);
1650 
1651 	/*
1652 	 * If we don't have info regarding the voltage levels supported by
1653 	 * regulators, don't change the IO PAD PWR SWITCH.
1654 	 */
1655 	if (msm_host->caps_0 & CORE_VOLT_SUPPORT) {
1656 		u32 new_config;
1657 		/*
1658 		 * We should unset IO PAD PWR switch only if the register write
1659 		 * can set IO lines high and the regulator also switches to 3 V.
1660 		 * Else, we should keep the IO PAD PWR switch set.
1661 		 * This is applicable to certain targets where eMMC vccq supply
1662 		 * is only 1.8V. In such targets, even during REQ_IO_HIGH, the
1663 		 * IO PAD PWR switch must be kept set to reflect actual
1664 		 * regulator voltage. This way, during initialization of
1665 		 * controllers with only 1.8V, we will set the IO PAD bit
1666 		 * without waiting for a REQ_IO_LOW.
1667 		 */
1668 		config = readl_relaxed(host->ioaddr +
1669 				msm_offset->core_vendor_spec);
1670 		new_config = config;
1671 
1672 		if ((io_level & REQ_IO_HIGH) &&
1673 				(msm_host->caps_0 & CORE_3_0V_SUPPORT))
1674 			new_config &= ~CORE_IO_PAD_PWR_SWITCH;
1675 		else if ((io_level & REQ_IO_LOW) ||
1676 				(msm_host->caps_0 & CORE_1_8V_SUPPORT))
1677 			new_config |= CORE_IO_PAD_PWR_SWITCH;
1678 
1679 		if (config ^ new_config)
1680 			writel_relaxed(new_config, host->ioaddr +
1681 					msm_offset->core_vendor_spec);
1682 	}
1683 
1684 	if (pwr_state)
1685 		msm_host->curr_pwr_state = pwr_state;
1686 	if (io_level)
1687 		msm_host->curr_io_level = io_level;
1688 
1689 	dev_dbg(mmc_dev(mmc), "%s: %s: Handled IRQ(%d), irq_status=0x%x, ack=0x%x\n",
1690 		mmc_hostname(msm_host->mmc), __func__, irq, irq_status,
1691 		irq_ack);
1692 }
1693 
1694 static irqreturn_t sdhci_msm_pwr_irq(int irq, void *data)
1695 {
1696 	struct sdhci_host *host = (struct sdhci_host *)data;
1697 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1698 	struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
1699 
1700 	sdhci_msm_handle_pwr_irq(host, irq);
1701 	msm_host->pwr_irq_flag = 1;
1702 	sdhci_msm_complete_pwr_irq_wait(msm_host);
1703 
1704 
1705 	return IRQ_HANDLED;
1706 }
1707 
1708 static unsigned int sdhci_msm_get_max_clock(struct sdhci_host *host)
1709 {
1710 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1711 	struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
1712 	struct clk *core_clk = msm_host->bulk_clks[0].clk;
1713 
1714 	return clk_round_rate(core_clk, ULONG_MAX);
1715 }
1716 
1717 static unsigned int sdhci_msm_get_min_clock(struct sdhci_host *host)
1718 {
1719 	return SDHCI_MSM_MIN_CLOCK;
1720 }
1721 
1722 /*
1723  * __sdhci_msm_set_clock - sdhci_msm clock control.
1724  *
1725  * Description:
1726  * MSM controller does not use internal divider and
1727  * instead directly control the GCC clock as per
1728  * HW recommendation.
1729  **/
1730 static void __sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock)
1731 {
1732 	u16 clk;
1733 	/*
1734 	 * Keep actual_clock as zero -
1735 	 * - since there is no divider used so no need of having actual_clock.
1736 	 * - MSM controller uses SDCLK for data timeout calculation. If
1737 	 *   actual_clock is zero, host->clock is taken for calculation.
1738 	 */
1739 	host->mmc->actual_clock = 0;
1740 
1741 	sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
1742 
1743 	if (clock == 0)
1744 		return;
1745 
1746 	/*
1747 	 * MSM controller do not use clock divider.
1748 	 * Thus read SDHCI_CLOCK_CONTROL and only enable
1749 	 * clock with no divider value programmed.
1750 	 */
1751 	clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1752 	sdhci_enable_clk(host, clk);
1753 }
1754 
1755 /* sdhci_msm_set_clock - Called with (host->lock) spinlock held. */
1756 static void sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock)
1757 {
1758 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1759 	struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
1760 
1761 	if (!clock) {
1762 		msm_host->clk_rate = clock;
1763 		goto out;
1764 	}
1765 
1766 	sdhci_msm_hc_select_mode(host);
1767 
1768 	msm_set_clock_rate_for_bus_mode(host, clock);
1769 out:
1770 	__sdhci_msm_set_clock(host, clock);
1771 }
1772 
1773 /*****************************************************************************\
1774  *                                                                           *
1775  * MSM Command Queue Engine (CQE)                                            *
1776  *                                                                           *
1777 \*****************************************************************************/
1778 
1779 static u32 sdhci_msm_cqe_irq(struct sdhci_host *host, u32 intmask)
1780 {
1781 	int cmd_error = 0;
1782 	int data_error = 0;
1783 
1784 	if (!sdhci_cqe_irq(host, intmask, &cmd_error, &data_error))
1785 		return intmask;
1786 
1787 	cqhci_irq(host->mmc, intmask, cmd_error, data_error);
1788 	return 0;
1789 }
1790 
1791 static void sdhci_msm_cqe_disable(struct mmc_host *mmc, bool recovery)
1792 {
1793 	struct sdhci_host *host = mmc_priv(mmc);
1794 	unsigned long flags;
1795 	u32 ctrl;
1796 
1797 	/*
1798 	 * When CQE is halted, the legacy SDHCI path operates only
1799 	 * on 16-byte descriptors in 64bit mode.
1800 	 */
1801 	if (host->flags & SDHCI_USE_64_BIT_DMA)
1802 		host->desc_sz = 16;
1803 
1804 	spin_lock_irqsave(&host->lock, flags);
1805 
1806 	/*
1807 	 * During CQE command transfers, command complete bit gets latched.
1808 	 * So s/w should clear command complete interrupt status when CQE is
1809 	 * either halted or disabled. Otherwise unexpected SDCHI legacy
1810 	 * interrupt gets triggered when CQE is halted/disabled.
1811 	 */
1812 	ctrl = sdhci_readl(host, SDHCI_INT_ENABLE);
1813 	ctrl |= SDHCI_INT_RESPONSE;
1814 	sdhci_writel(host,  ctrl, SDHCI_INT_ENABLE);
1815 	sdhci_writel(host, SDHCI_INT_RESPONSE, SDHCI_INT_STATUS);
1816 
1817 	spin_unlock_irqrestore(&host->lock, flags);
1818 
1819 	sdhci_cqe_disable(mmc, recovery);
1820 }
1821 
1822 static const struct cqhci_host_ops sdhci_msm_cqhci_ops = {
1823 	.enable		= sdhci_cqe_enable,
1824 	.disable	= sdhci_msm_cqe_disable,
1825 };
1826 
1827 static int sdhci_msm_cqe_add_host(struct sdhci_host *host,
1828 				struct platform_device *pdev)
1829 {
1830 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1831 	struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
1832 	struct cqhci_host *cq_host;
1833 	bool dma64;
1834 	u32 cqcfg;
1835 	int ret;
1836 
1837 	/*
1838 	 * When CQE is halted, SDHC operates only on 16byte ADMA descriptors.
1839 	 * So ensure ADMA table is allocated for 16byte descriptors.
1840 	 */
1841 	if (host->caps & SDHCI_CAN_64BIT)
1842 		host->alloc_desc_sz = 16;
1843 
1844 	ret = sdhci_setup_host(host);
1845 	if (ret)
1846 		return ret;
1847 
1848 	cq_host = cqhci_pltfm_init(pdev);
1849 	if (IS_ERR(cq_host)) {
1850 		ret = PTR_ERR(cq_host);
1851 		dev_err(&pdev->dev, "cqhci-pltfm init: failed: %d\n", ret);
1852 		goto cleanup;
1853 	}
1854 
1855 	msm_host->mmc->caps2 |= MMC_CAP2_CQE | MMC_CAP2_CQE_DCMD;
1856 	cq_host->ops = &sdhci_msm_cqhci_ops;
1857 
1858 	dma64 = host->flags & SDHCI_USE_64_BIT_DMA;
1859 
1860 	ret = cqhci_init(cq_host, host->mmc, dma64);
1861 	if (ret) {
1862 		dev_err(&pdev->dev, "%s: CQE init: failed (%d)\n",
1863 				mmc_hostname(host->mmc), ret);
1864 		goto cleanup;
1865 	}
1866 
1867 	/* Disable cqe reset due to cqe enable signal */
1868 	cqcfg = cqhci_readl(cq_host, CQHCI_VENDOR_CFG1);
1869 	cqcfg |= CQHCI_VENDOR_DIS_RST_ON_CQ_EN;
1870 	cqhci_writel(cq_host, cqcfg, CQHCI_VENDOR_CFG1);
1871 
1872 	/*
1873 	 * SDHC expects 12byte ADMA descriptors till CQE is enabled.
1874 	 * So limit desc_sz to 12 so that the data commands that are sent
1875 	 * during card initialization (before CQE gets enabled) would
1876 	 * get executed without any issues.
1877 	 */
1878 	if (host->flags & SDHCI_USE_64_BIT_DMA)
1879 		host->desc_sz = 12;
1880 
1881 	ret = __sdhci_add_host(host);
1882 	if (ret)
1883 		goto cleanup;
1884 
1885 	dev_info(&pdev->dev, "%s: CQE init: success\n",
1886 			mmc_hostname(host->mmc));
1887 	return ret;
1888 
1889 cleanup:
1890 	sdhci_cleanup_host(host);
1891 	return ret;
1892 }
1893 
1894 /*
1895  * Platform specific register write functions. This is so that, if any
1896  * register write needs to be followed up by platform specific actions,
1897  * they can be added here. These functions can go to sleep when writes
1898  * to certain registers are done.
1899  * These functions are relying on sdhci_set_ios not using spinlock.
1900  */
1901 static int __sdhci_msm_check_write(struct sdhci_host *host, u16 val, int reg)
1902 {
1903 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1904 	struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
1905 	u32 req_type = 0;
1906 
1907 	switch (reg) {
1908 	case SDHCI_HOST_CONTROL2:
1909 		req_type = (val & SDHCI_CTRL_VDD_180) ? REQ_IO_LOW :
1910 			REQ_IO_HIGH;
1911 		break;
1912 	case SDHCI_SOFTWARE_RESET:
1913 		if (host->pwr && (val & SDHCI_RESET_ALL))
1914 			req_type = REQ_BUS_OFF;
1915 		break;
1916 	case SDHCI_POWER_CONTROL:
1917 		req_type = !val ? REQ_BUS_OFF : REQ_BUS_ON;
1918 		break;
1919 	case SDHCI_TRANSFER_MODE:
1920 		msm_host->transfer_mode = val;
1921 		break;
1922 	case SDHCI_COMMAND:
1923 		if (!msm_host->use_cdr)
1924 			break;
1925 		if ((msm_host->transfer_mode & SDHCI_TRNS_READ) &&
1926 		    SDHCI_GET_CMD(val) != MMC_SEND_TUNING_BLOCK_HS200 &&
1927 		    SDHCI_GET_CMD(val) != MMC_SEND_TUNING_BLOCK)
1928 			sdhci_msm_set_cdr(host, true);
1929 		else
1930 			sdhci_msm_set_cdr(host, false);
1931 		break;
1932 	}
1933 
1934 	if (req_type) {
1935 		msm_host->pwr_irq_flag = 0;
1936 		/*
1937 		 * Since this register write may trigger a power irq, ensure
1938 		 * all previous register writes are complete by this point.
1939 		 */
1940 		mb();
1941 	}
1942 	return req_type;
1943 }
1944 
1945 /* This function may sleep*/
1946 static void sdhci_msm_writew(struct sdhci_host *host, u16 val, int reg)
1947 {
1948 	u32 req_type = 0;
1949 
1950 	req_type = __sdhci_msm_check_write(host, val, reg);
1951 	writew_relaxed(val, host->ioaddr + reg);
1952 
1953 	if (req_type)
1954 		sdhci_msm_check_power_status(host, req_type);
1955 }
1956 
1957 /* This function may sleep*/
1958 static void sdhci_msm_writeb(struct sdhci_host *host, u8 val, int reg)
1959 {
1960 	u32 req_type = 0;
1961 
1962 	req_type = __sdhci_msm_check_write(host, val, reg);
1963 
1964 	writeb_relaxed(val, host->ioaddr + reg);
1965 
1966 	if (req_type)
1967 		sdhci_msm_check_power_status(host, req_type);
1968 }
1969 
1970 static void sdhci_msm_set_regulator_caps(struct sdhci_msm_host *msm_host)
1971 {
1972 	struct mmc_host *mmc = msm_host->mmc;
1973 	struct regulator *supply = mmc->supply.vqmmc;
1974 	u32 caps = 0, config;
1975 	struct sdhci_host *host = mmc_priv(mmc);
1976 	const struct sdhci_msm_offset *msm_offset = msm_host->offset;
1977 
1978 	if (!IS_ERR(mmc->supply.vqmmc)) {
1979 		if (regulator_is_supported_voltage(supply, 1700000, 1950000))
1980 			caps |= CORE_1_8V_SUPPORT;
1981 		if (regulator_is_supported_voltage(supply, 2700000, 3600000))
1982 			caps |= CORE_3_0V_SUPPORT;
1983 
1984 		if (!caps)
1985 			pr_warn("%s: 1.8/3V not supported for vqmmc\n",
1986 					mmc_hostname(mmc));
1987 	}
1988 
1989 	if (caps) {
1990 		/*
1991 		 * Set the PAD_PWR_SWITCH_EN bit so that the PAD_PWR_SWITCH
1992 		 * bit can be used as required later on.
1993 		 */
1994 		u32 io_level = msm_host->curr_io_level;
1995 
1996 		config = readl_relaxed(host->ioaddr +
1997 				msm_offset->core_vendor_spec);
1998 		config |= CORE_IO_PAD_PWR_SWITCH_EN;
1999 
2000 		if ((io_level & REQ_IO_HIGH) && (caps &	CORE_3_0V_SUPPORT))
2001 			config &= ~CORE_IO_PAD_PWR_SWITCH;
2002 		else if ((io_level & REQ_IO_LOW) || (caps & CORE_1_8V_SUPPORT))
2003 			config |= CORE_IO_PAD_PWR_SWITCH;
2004 
2005 		writel_relaxed(config,
2006 				host->ioaddr + msm_offset->core_vendor_spec);
2007 	}
2008 	msm_host->caps_0 |= caps;
2009 	pr_debug("%s: supported caps: 0x%08x\n", mmc_hostname(mmc), caps);
2010 }
2011 
2012 static void sdhci_msm_reset(struct sdhci_host *host, u8 mask)
2013 {
2014 	if ((host->mmc->caps2 & MMC_CAP2_CQE) && (mask & SDHCI_RESET_ALL))
2015 		cqhci_deactivate(host->mmc);
2016 	sdhci_reset(host, mask);
2017 }
2018 
2019 static int sdhci_msm_register_vreg(struct sdhci_msm_host *msm_host)
2020 {
2021 	int ret;
2022 
2023 	ret = mmc_regulator_get_supply(msm_host->mmc);
2024 	if (ret)
2025 		return ret;
2026 
2027 	sdhci_msm_set_regulator_caps(msm_host);
2028 
2029 	return 0;
2030 }
2031 
2032 static int sdhci_msm_start_signal_voltage_switch(struct mmc_host *mmc,
2033 				      struct mmc_ios *ios)
2034 {
2035 	struct sdhci_host *host = mmc_priv(mmc);
2036 	u16 ctrl, status;
2037 
2038 	/*
2039 	 * Signal Voltage Switching is only applicable for Host Controllers
2040 	 * v3.00 and above.
2041 	 */
2042 	if (host->version < SDHCI_SPEC_300)
2043 		return 0;
2044 
2045 	ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2046 
2047 	switch (ios->signal_voltage) {
2048 	case MMC_SIGNAL_VOLTAGE_330:
2049 		if (!(host->flags & SDHCI_SIGNALING_330))
2050 			return -EINVAL;
2051 
2052 		/* Set 1.8V Signal Enable in the Host Control2 register to 0 */
2053 		ctrl &= ~SDHCI_CTRL_VDD_180;
2054 		break;
2055 	case MMC_SIGNAL_VOLTAGE_180:
2056 		if (!(host->flags & SDHCI_SIGNALING_180))
2057 			return -EINVAL;
2058 
2059 		/* Enable 1.8V Signal Enable in the Host Control2 register */
2060 		ctrl |= SDHCI_CTRL_VDD_180;
2061 		break;
2062 
2063 	default:
2064 		return -EINVAL;
2065 	}
2066 
2067 	sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2068 
2069 	/* Wait for 5ms */
2070 	usleep_range(5000, 5500);
2071 
2072 	/* regulator output should be stable within 5 ms */
2073 	status = ctrl & SDHCI_CTRL_VDD_180;
2074 	ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2075 	if ((ctrl & SDHCI_CTRL_VDD_180) == status)
2076 		return 0;
2077 
2078 	dev_warn(mmc_dev(mmc), "%s: Regulator output did not became stable\n",
2079 		mmc_hostname(mmc));
2080 
2081 	return -EAGAIN;
2082 }
2083 
2084 #define DRIVER_NAME "sdhci_msm"
2085 #define SDHCI_MSM_DUMP(f, x...) \
2086 	pr_err("%s: " DRIVER_NAME ": " f, mmc_hostname(host->mmc), ## x)
2087 
2088 static void sdhci_msm_dump_vendor_regs(struct sdhci_host *host)
2089 {
2090 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2091 	struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
2092 	const struct sdhci_msm_offset *msm_offset = msm_host->offset;
2093 
2094 	SDHCI_MSM_DUMP("----------- VENDOR REGISTER DUMP -----------\n");
2095 
2096 	SDHCI_MSM_DUMP(
2097 			"DLL sts: 0x%08x | DLL cfg:  0x%08x | DLL cfg2: 0x%08x\n",
2098 		readl_relaxed(host->ioaddr + msm_offset->core_dll_status),
2099 		readl_relaxed(host->ioaddr + msm_offset->core_dll_config),
2100 		readl_relaxed(host->ioaddr + msm_offset->core_dll_config_2));
2101 	SDHCI_MSM_DUMP(
2102 			"DLL cfg3: 0x%08x | DLL usr ctl:  0x%08x | DDR cfg: 0x%08x\n",
2103 		readl_relaxed(host->ioaddr + msm_offset->core_dll_config_3),
2104 		readl_relaxed(host->ioaddr + msm_offset->core_dll_usr_ctl),
2105 		readl_relaxed(host->ioaddr + msm_offset->core_ddr_config));
2106 	SDHCI_MSM_DUMP(
2107 			"Vndr func: 0x%08x | Vndr func2 : 0x%08x Vndr func3: 0x%08x\n",
2108 		readl_relaxed(host->ioaddr + msm_offset->core_vendor_spec),
2109 		readl_relaxed(host->ioaddr +
2110 			msm_offset->core_vendor_spec_func2),
2111 		readl_relaxed(host->ioaddr + msm_offset->core_vendor_spec3));
2112 }
2113 
2114 static const struct sdhci_msm_variant_ops mci_var_ops = {
2115 	.msm_readl_relaxed = sdhci_msm_mci_variant_readl_relaxed,
2116 	.msm_writel_relaxed = sdhci_msm_mci_variant_writel_relaxed,
2117 };
2118 
2119 static const struct sdhci_msm_variant_ops v5_var_ops = {
2120 	.msm_readl_relaxed = sdhci_msm_v5_variant_readl_relaxed,
2121 	.msm_writel_relaxed = sdhci_msm_v5_variant_writel_relaxed,
2122 };
2123 
2124 static const struct sdhci_msm_variant_info sdhci_msm_mci_var = {
2125 	.var_ops = &mci_var_ops,
2126 	.offset = &sdhci_msm_mci_offset,
2127 };
2128 
2129 static const struct sdhci_msm_variant_info sdhci_msm_v5_var = {
2130 	.mci_removed = true,
2131 	.var_ops = &v5_var_ops,
2132 	.offset = &sdhci_msm_v5_offset,
2133 };
2134 
2135 static const struct sdhci_msm_variant_info sdm845_sdhci_var = {
2136 	.mci_removed = true,
2137 	.restore_dll_config = true,
2138 	.var_ops = &v5_var_ops,
2139 	.offset = &sdhci_msm_v5_offset,
2140 };
2141 
2142 static const struct sdhci_msm_variant_info sm8250_sdhci_var = {
2143 	.mci_removed = true,
2144 	.uses_tassadar_dll = true,
2145 	.var_ops = &v5_var_ops,
2146 	.offset = &sdhci_msm_v5_offset,
2147 };
2148 
2149 static const struct of_device_id sdhci_msm_dt_match[] = {
2150 	{.compatible = "qcom,sdhci-msm-v4", .data = &sdhci_msm_mci_var},
2151 	{.compatible = "qcom,sdhci-msm-v5", .data = &sdhci_msm_v5_var},
2152 	{.compatible = "qcom,sdm845-sdhci", .data = &sdm845_sdhci_var},
2153 	{.compatible = "qcom,sm8250-sdhci", .data = &sm8250_sdhci_var},
2154 	{},
2155 };
2156 
2157 MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match);
2158 
2159 static const struct sdhci_ops sdhci_msm_ops = {
2160 	.reset = sdhci_msm_reset,
2161 	.set_clock = sdhci_msm_set_clock,
2162 	.get_min_clock = sdhci_msm_get_min_clock,
2163 	.get_max_clock = sdhci_msm_get_max_clock,
2164 	.set_bus_width = sdhci_set_bus_width,
2165 	.set_uhs_signaling = sdhci_msm_set_uhs_signaling,
2166 	.write_w = sdhci_msm_writew,
2167 	.write_b = sdhci_msm_writeb,
2168 	.irq	= sdhci_msm_cqe_irq,
2169 	.dump_vendor_regs = sdhci_msm_dump_vendor_regs,
2170 	.set_power = sdhci_set_power_noreg,
2171 };
2172 
2173 static const struct sdhci_pltfm_data sdhci_msm_pdata = {
2174 	.quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
2175 		  SDHCI_QUIRK_SINGLE_POWER_WRITE |
2176 		  SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
2177 		  SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12,
2178 
2179 	.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
2180 	.ops = &sdhci_msm_ops,
2181 };
2182 
2183 static inline void sdhci_msm_get_of_property(struct platform_device *pdev,
2184 		struct sdhci_host *host)
2185 {
2186 	struct device_node *node = pdev->dev.of_node;
2187 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2188 	struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
2189 
2190 	if (of_property_read_u32(node, "qcom,ddr-config",
2191 				&msm_host->ddr_config))
2192 		msm_host->ddr_config = DDR_CONFIG_POR_VAL;
2193 
2194 	of_property_read_u32(node, "qcom,dll-config", &msm_host->dll_config);
2195 }
2196 
2197 
2198 static int sdhci_msm_probe(struct platform_device *pdev)
2199 {
2200 	struct sdhci_host *host;
2201 	struct sdhci_pltfm_host *pltfm_host;
2202 	struct sdhci_msm_host *msm_host;
2203 	struct clk *clk;
2204 	int ret;
2205 	u16 host_version, core_minor;
2206 	u32 core_version, config;
2207 	u8 core_major;
2208 	const struct sdhci_msm_offset *msm_offset;
2209 	const struct sdhci_msm_variant_info *var_info;
2210 	struct device_node *node = pdev->dev.of_node;
2211 
2212 	host = sdhci_pltfm_init(pdev, &sdhci_msm_pdata, sizeof(*msm_host));
2213 	if (IS_ERR(host))
2214 		return PTR_ERR(host);
2215 
2216 	host->sdma_boundary = 0;
2217 	pltfm_host = sdhci_priv(host);
2218 	msm_host = sdhci_pltfm_priv(pltfm_host);
2219 	msm_host->mmc = host->mmc;
2220 	msm_host->pdev = pdev;
2221 
2222 	ret = mmc_of_parse(host->mmc);
2223 	if (ret)
2224 		goto pltfm_free;
2225 
2226 	/*
2227 	 * Based on the compatible string, load the required msm host info from
2228 	 * the data associated with the version info.
2229 	 */
2230 	var_info = of_device_get_match_data(&pdev->dev);
2231 
2232 	msm_host->mci_removed = var_info->mci_removed;
2233 	msm_host->restore_dll_config = var_info->restore_dll_config;
2234 	msm_host->var_ops = var_info->var_ops;
2235 	msm_host->offset = var_info->offset;
2236 	msm_host->uses_tassadar_dll = var_info->uses_tassadar_dll;
2237 
2238 	msm_offset = msm_host->offset;
2239 
2240 	sdhci_get_of_property(pdev);
2241 	sdhci_msm_get_of_property(pdev, host);
2242 
2243 	msm_host->saved_tuning_phase = INVALID_TUNING_PHASE;
2244 
2245 	/* Setup SDCC bus voter clock. */
2246 	msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus");
2247 	if (!IS_ERR(msm_host->bus_clk)) {
2248 		/* Vote for max. clk rate for max. performance */
2249 		ret = clk_set_rate(msm_host->bus_clk, INT_MAX);
2250 		if (ret)
2251 			goto pltfm_free;
2252 		ret = clk_prepare_enable(msm_host->bus_clk);
2253 		if (ret)
2254 			goto pltfm_free;
2255 	}
2256 
2257 	/* Setup main peripheral bus clock */
2258 	clk = devm_clk_get(&pdev->dev, "iface");
2259 	if (IS_ERR(clk)) {
2260 		ret = PTR_ERR(clk);
2261 		dev_err(&pdev->dev, "Peripheral clk setup failed (%d)\n", ret);
2262 		goto bus_clk_disable;
2263 	}
2264 	msm_host->bulk_clks[1].clk = clk;
2265 
2266 	/* Setup SDC MMC clock */
2267 	clk = devm_clk_get(&pdev->dev, "core");
2268 	if (IS_ERR(clk)) {
2269 		ret = PTR_ERR(clk);
2270 		dev_err(&pdev->dev, "SDC MMC clk setup failed (%d)\n", ret);
2271 		goto bus_clk_disable;
2272 	}
2273 	msm_host->bulk_clks[0].clk = clk;
2274 
2275 	 /* Check for optional interconnect paths */
2276 	ret = dev_pm_opp_of_find_icc_paths(&pdev->dev, NULL);
2277 	if (ret)
2278 		goto bus_clk_disable;
2279 
2280 	msm_host->opp_table = dev_pm_opp_set_clkname(&pdev->dev, "core");
2281 	if (IS_ERR(msm_host->opp_table)) {
2282 		ret = PTR_ERR(msm_host->opp_table);
2283 		goto bus_clk_disable;
2284 	}
2285 
2286 	/* OPP table is optional */
2287 	ret = dev_pm_opp_of_add_table(&pdev->dev);
2288 	if (!ret) {
2289 		msm_host->has_opp_table = true;
2290 	} else if (ret != -ENODEV) {
2291 		dev_err(&pdev->dev, "Invalid OPP table in Device tree\n");
2292 		goto opp_cleanup;
2293 	}
2294 
2295 	/* Vote for maximum clock rate for maximum performance */
2296 	ret = dev_pm_opp_set_rate(&pdev->dev, INT_MAX);
2297 	if (ret)
2298 		dev_warn(&pdev->dev, "core clock boost failed\n");
2299 
2300 	clk = devm_clk_get(&pdev->dev, "cal");
2301 	if (IS_ERR(clk))
2302 		clk = NULL;
2303 	msm_host->bulk_clks[2].clk = clk;
2304 
2305 	clk = devm_clk_get(&pdev->dev, "sleep");
2306 	if (IS_ERR(clk))
2307 		clk = NULL;
2308 	msm_host->bulk_clks[3].clk = clk;
2309 
2310 	ret = clk_bulk_prepare_enable(ARRAY_SIZE(msm_host->bulk_clks),
2311 				      msm_host->bulk_clks);
2312 	if (ret)
2313 		goto opp_cleanup;
2314 
2315 	/*
2316 	 * xo clock is needed for FLL feature of cm_dll.
2317 	 * In case if xo clock is not mentioned in DT, warn and proceed.
2318 	 */
2319 	msm_host->xo_clk = devm_clk_get(&pdev->dev, "xo");
2320 	if (IS_ERR(msm_host->xo_clk)) {
2321 		ret = PTR_ERR(msm_host->xo_clk);
2322 		dev_warn(&pdev->dev, "TCXO clk not present (%d)\n", ret);
2323 	}
2324 
2325 	if (!msm_host->mci_removed) {
2326 		msm_host->core_mem = devm_platform_ioremap_resource(pdev, 1);
2327 		if (IS_ERR(msm_host->core_mem)) {
2328 			ret = PTR_ERR(msm_host->core_mem);
2329 			goto clk_disable;
2330 		}
2331 	}
2332 
2333 	/* Reset the vendor spec register to power on reset state */
2334 	writel_relaxed(CORE_VENDOR_SPEC_POR_VAL,
2335 			host->ioaddr + msm_offset->core_vendor_spec);
2336 
2337 	if (!msm_host->mci_removed) {
2338 		/* Set HC_MODE_EN bit in HC_MODE register */
2339 		msm_host_writel(msm_host, HC_MODE_EN, host,
2340 				msm_offset->core_hc_mode);
2341 		config = msm_host_readl(msm_host, host,
2342 				msm_offset->core_hc_mode);
2343 		config |= FF_CLK_SW_RST_DIS;
2344 		msm_host_writel(msm_host, config, host,
2345 				msm_offset->core_hc_mode);
2346 	}
2347 
2348 	host_version = readw_relaxed((host->ioaddr + SDHCI_HOST_VERSION));
2349 	dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n",
2350 		host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >>
2351 			       SDHCI_VENDOR_VER_SHIFT));
2352 
2353 	core_version = msm_host_readl(msm_host, host,
2354 			msm_offset->core_mci_version);
2355 	core_major = (core_version & CORE_VERSION_MAJOR_MASK) >>
2356 		      CORE_VERSION_MAJOR_SHIFT;
2357 	core_minor = core_version & CORE_VERSION_MINOR_MASK;
2358 	dev_dbg(&pdev->dev, "MCI Version: 0x%08x, major: 0x%04x, minor: 0x%02x\n",
2359 		core_version, core_major, core_minor);
2360 
2361 	if (core_major == 1 && core_minor >= 0x42)
2362 		msm_host->use_14lpp_dll_reset = true;
2363 
2364 	/*
2365 	 * SDCC 5 controller with major version 1, minor version 0x34 and later
2366 	 * with HS 400 mode support will use CM DLL instead of CDC LP 533 DLL.
2367 	 */
2368 	if (core_major == 1 && core_minor < 0x34)
2369 		msm_host->use_cdclp533 = true;
2370 
2371 	/*
2372 	 * Support for some capabilities is not advertised by newer
2373 	 * controller versions and must be explicitly enabled.
2374 	 */
2375 	if (core_major >= 1 && core_minor != 0x11 && core_minor != 0x12) {
2376 		config = readl_relaxed(host->ioaddr + SDHCI_CAPABILITIES);
2377 		config |= SDHCI_CAN_VDD_300 | SDHCI_CAN_DO_8BIT;
2378 		writel_relaxed(config, host->ioaddr +
2379 				msm_offset->core_vendor_spec_capabilities0);
2380 	}
2381 
2382 	if (core_major == 1 && core_minor >= 0x49)
2383 		msm_host->updated_ddr_cfg = true;
2384 
2385 	ret = sdhci_msm_register_vreg(msm_host);
2386 	if (ret)
2387 		goto clk_disable;
2388 
2389 	/*
2390 	 * Power on reset state may trigger power irq if previous status of
2391 	 * PWRCTL was either BUS_ON or IO_HIGH_V. So before enabling pwr irq
2392 	 * interrupt in GIC, any pending power irq interrupt should be
2393 	 * acknowledged. Otherwise power irq interrupt handler would be
2394 	 * fired prematurely.
2395 	 */
2396 	sdhci_msm_handle_pwr_irq(host, 0);
2397 
2398 	/*
2399 	 * Ensure that above writes are propogated before interrupt enablement
2400 	 * in GIC.
2401 	 */
2402 	mb();
2403 
2404 	/* Setup IRQ for handling power/voltage tasks with PMIC */
2405 	msm_host->pwr_irq = platform_get_irq_byname(pdev, "pwr_irq");
2406 	if (msm_host->pwr_irq < 0) {
2407 		ret = msm_host->pwr_irq;
2408 		goto clk_disable;
2409 	}
2410 
2411 	sdhci_msm_init_pwr_irq_wait(msm_host);
2412 	/* Enable pwr irq interrupts */
2413 	msm_host_writel(msm_host, INT_MASK, host,
2414 		msm_offset->core_pwrctl_mask);
2415 
2416 	ret = devm_request_threaded_irq(&pdev->dev, msm_host->pwr_irq, NULL,
2417 					sdhci_msm_pwr_irq, IRQF_ONESHOT,
2418 					dev_name(&pdev->dev), host);
2419 	if (ret) {
2420 		dev_err(&pdev->dev, "Request IRQ failed (%d)\n", ret);
2421 		goto clk_disable;
2422 	}
2423 
2424 	msm_host->mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_NEED_RSP_BUSY;
2425 
2426 	pm_runtime_get_noresume(&pdev->dev);
2427 	pm_runtime_set_active(&pdev->dev);
2428 	pm_runtime_enable(&pdev->dev);
2429 	pm_runtime_set_autosuspend_delay(&pdev->dev,
2430 					 MSM_MMC_AUTOSUSPEND_DELAY_MS);
2431 	pm_runtime_use_autosuspend(&pdev->dev);
2432 
2433 	host->mmc_host_ops.start_signal_voltage_switch =
2434 		sdhci_msm_start_signal_voltage_switch;
2435 	host->mmc_host_ops.execute_tuning = sdhci_msm_execute_tuning;
2436 	if (of_property_read_bool(node, "supports-cqe"))
2437 		ret = sdhci_msm_cqe_add_host(host, pdev);
2438 	else
2439 		ret = sdhci_add_host(host);
2440 	if (ret)
2441 		goto pm_runtime_disable;
2442 
2443 	pm_runtime_mark_last_busy(&pdev->dev);
2444 	pm_runtime_put_autosuspend(&pdev->dev);
2445 
2446 	return 0;
2447 
2448 pm_runtime_disable:
2449 	pm_runtime_disable(&pdev->dev);
2450 	pm_runtime_set_suspended(&pdev->dev);
2451 	pm_runtime_put_noidle(&pdev->dev);
2452 clk_disable:
2453 	clk_bulk_disable_unprepare(ARRAY_SIZE(msm_host->bulk_clks),
2454 				   msm_host->bulk_clks);
2455 opp_cleanup:
2456 	if (msm_host->has_opp_table)
2457 		dev_pm_opp_of_remove_table(&pdev->dev);
2458 	dev_pm_opp_put_clkname(msm_host->opp_table);
2459 bus_clk_disable:
2460 	if (!IS_ERR(msm_host->bus_clk))
2461 		clk_disable_unprepare(msm_host->bus_clk);
2462 pltfm_free:
2463 	sdhci_pltfm_free(pdev);
2464 	return ret;
2465 }
2466 
2467 static int sdhci_msm_remove(struct platform_device *pdev)
2468 {
2469 	struct sdhci_host *host = platform_get_drvdata(pdev);
2470 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2471 	struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
2472 	int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) ==
2473 		    0xffffffff);
2474 
2475 	sdhci_remove_host(host, dead);
2476 
2477 	if (msm_host->has_opp_table)
2478 		dev_pm_opp_of_remove_table(&pdev->dev);
2479 	dev_pm_opp_put_clkname(msm_host->opp_table);
2480 	pm_runtime_get_sync(&pdev->dev);
2481 	pm_runtime_disable(&pdev->dev);
2482 	pm_runtime_put_noidle(&pdev->dev);
2483 
2484 	clk_bulk_disable_unprepare(ARRAY_SIZE(msm_host->bulk_clks),
2485 				   msm_host->bulk_clks);
2486 	if (!IS_ERR(msm_host->bus_clk))
2487 		clk_disable_unprepare(msm_host->bus_clk);
2488 	sdhci_pltfm_free(pdev);
2489 	return 0;
2490 }
2491 
2492 static __maybe_unused int sdhci_msm_runtime_suspend(struct device *dev)
2493 {
2494 	struct sdhci_host *host = dev_get_drvdata(dev);
2495 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2496 	struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
2497 
2498 	/* Drop the performance vote */
2499 	dev_pm_opp_set_rate(dev, 0);
2500 	clk_bulk_disable_unprepare(ARRAY_SIZE(msm_host->bulk_clks),
2501 				   msm_host->bulk_clks);
2502 
2503 	return 0;
2504 }
2505 
2506 static __maybe_unused int sdhci_msm_runtime_resume(struct device *dev)
2507 {
2508 	struct sdhci_host *host = dev_get_drvdata(dev);
2509 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2510 	struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
2511 	int ret;
2512 
2513 	ret = clk_bulk_prepare_enable(ARRAY_SIZE(msm_host->bulk_clks),
2514 				       msm_host->bulk_clks);
2515 	if (ret)
2516 		return ret;
2517 	/*
2518 	 * Whenever core-clock is gated dynamically, it's needed to
2519 	 * restore the SDR DLL settings when the clock is ungated.
2520 	 */
2521 	if (msm_host->restore_dll_config && msm_host->clk_rate)
2522 		ret = sdhci_msm_restore_sdr_dll_config(host);
2523 
2524 	dev_pm_opp_set_rate(dev, msm_host->clk_rate);
2525 
2526 	return ret;
2527 }
2528 
2529 static const struct dev_pm_ops sdhci_msm_pm_ops = {
2530 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
2531 				pm_runtime_force_resume)
2532 	SET_RUNTIME_PM_OPS(sdhci_msm_runtime_suspend,
2533 			   sdhci_msm_runtime_resume,
2534 			   NULL)
2535 };
2536 
2537 static struct platform_driver sdhci_msm_driver = {
2538 	.probe = sdhci_msm_probe,
2539 	.remove = sdhci_msm_remove,
2540 	.driver = {
2541 		   .name = "sdhci_msm",
2542 		   .of_match_table = sdhci_msm_dt_match,
2543 		   .pm = &sdhci_msm_pm_ops,
2544 	},
2545 };
2546 
2547 module_platform_driver(sdhci_msm_driver);
2548 
2549 MODULE_DESCRIPTION("Qualcomm Secure Digital Host Controller Interface driver");
2550 MODULE_LICENSE("GPL v2");
2551