xref: /openbmc/linux/drivers/mmc/host/sdhci-tegra.c (revision c6e7ab90)
1 /*
2  * Copyright (C) 2010 Google, Inc.
3  *
4  * This software is licensed under the terms of the GNU General Public
5  * License version 2, as published by the Free Software Foundation, and
6  * may be copied, distributed, and modified under those terms.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  */
14 
15 #include <linux/delay.h>
16 #include <linux/err.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/iopoll.h>
20 #include <linux/platform_device.h>
21 #include <linux/clk.h>
22 #include <linux/io.h>
23 #include <linux/of.h>
24 #include <linux/of_device.h>
25 #include <linux/pinctrl/consumer.h>
26 #include <linux/regulator/consumer.h>
27 #include <linux/reset.h>
28 #include <linux/mmc/card.h>
29 #include <linux/mmc/host.h>
30 #include <linux/mmc/mmc.h>
31 #include <linux/mmc/slot-gpio.h>
32 #include <linux/gpio/consumer.h>
33 #include <linux/ktime.h>
34 
35 #include "sdhci-pltfm.h"
36 #include "cqhci.h"
37 
38 /* Tegra SDHOST controller vendor register definitions */
39 #define SDHCI_TEGRA_VENDOR_CLOCK_CTRL			0x100
40 #define SDHCI_CLOCK_CTRL_TAP_MASK			0x00ff0000
41 #define SDHCI_CLOCK_CTRL_TAP_SHIFT			16
42 #define SDHCI_CLOCK_CTRL_TRIM_MASK			0x1f000000
43 #define SDHCI_CLOCK_CTRL_TRIM_SHIFT			24
44 #define SDHCI_CLOCK_CTRL_SDR50_TUNING_OVERRIDE		BIT(5)
45 #define SDHCI_CLOCK_CTRL_PADPIPE_CLKEN_OVERRIDE		BIT(3)
46 #define SDHCI_CLOCK_CTRL_SPI_MODE_CLKEN_OVERRIDE	BIT(2)
47 
48 #define SDHCI_TEGRA_VENDOR_SYS_SW_CTRL			0x104
49 #define SDHCI_TEGRA_SYS_SW_CTRL_ENHANCED_STROBE		BIT(31)
50 
51 #define SDHCI_TEGRA_VENDOR_CAP_OVERRIDES		0x10c
52 #define SDHCI_TEGRA_CAP_OVERRIDES_DQS_TRIM_MASK		0x00003f00
53 #define SDHCI_TEGRA_CAP_OVERRIDES_DQS_TRIM_SHIFT	8
54 
55 #define SDHCI_TEGRA_VENDOR_MISC_CTRL			0x120
56 #define SDHCI_MISC_CTRL_ENABLE_SDR104			0x8
57 #define SDHCI_MISC_CTRL_ENABLE_SDR50			0x10
58 #define SDHCI_MISC_CTRL_ENABLE_SDHCI_SPEC_300		0x20
59 #define SDHCI_MISC_CTRL_ENABLE_DDR50			0x200
60 
61 #define SDHCI_TEGRA_VENDOR_DLLCAL_CFG			0x1b0
62 #define SDHCI_TEGRA_DLLCAL_CALIBRATE			BIT(31)
63 
64 #define SDHCI_TEGRA_VENDOR_DLLCAL_STA			0x1bc
65 #define SDHCI_TEGRA_DLLCAL_STA_ACTIVE			BIT(31)
66 
67 #define SDHCI_VNDR_TUN_CTRL0_0				0x1c0
68 #define SDHCI_VNDR_TUN_CTRL0_TUN_HW_TAP			0x20000
69 #define SDHCI_VNDR_TUN_CTRL0_START_TAP_VAL_MASK		0x03fc0000
70 #define SDHCI_VNDR_TUN_CTRL0_START_TAP_VAL_SHIFT	18
71 #define SDHCI_VNDR_TUN_CTRL0_MUL_M_MASK			0x00001fc0
72 #define SDHCI_VNDR_TUN_CTRL0_MUL_M_SHIFT		6
73 #define SDHCI_VNDR_TUN_CTRL0_TUN_ITER_MASK		0x000e000
74 #define SDHCI_VNDR_TUN_CTRL0_TUN_ITER_SHIFT		13
75 #define TRIES_128					2
76 #define TRIES_256					4
77 #define SDHCI_VNDR_TUN_CTRL0_TUN_WORD_SEL_MASK		0x7
78 
79 #define SDHCI_TEGRA_VNDR_TUN_CTRL1_0			0x1c4
80 #define SDHCI_TEGRA_VNDR_TUN_STATUS0			0x1C8
81 #define SDHCI_TEGRA_VNDR_TUN_STATUS1			0x1CC
82 #define SDHCI_TEGRA_VNDR_TUN_STATUS1_TAP_MASK		0xFF
83 #define SDHCI_TEGRA_VNDR_TUN_STATUS1_END_TAP_SHIFT	0x8
84 #define TUNING_WORD_BIT_SIZE				32
85 
86 #define SDHCI_TEGRA_AUTO_CAL_CONFIG			0x1e4
87 #define SDHCI_AUTO_CAL_START				BIT(31)
88 #define SDHCI_AUTO_CAL_ENABLE				BIT(29)
89 #define SDHCI_AUTO_CAL_PDPU_OFFSET_MASK			0x0000ffff
90 
91 #define SDHCI_TEGRA_SDMEM_COMP_PADCTRL			0x1e0
92 #define SDHCI_TEGRA_SDMEM_COMP_PADCTRL_VREF_SEL_MASK	0x0000000f
93 #define SDHCI_TEGRA_SDMEM_COMP_PADCTRL_VREF_SEL_VAL	0x7
94 #define SDHCI_TEGRA_SDMEM_COMP_PADCTRL_E_INPUT_E_PWRD	BIT(31)
95 #define SDHCI_COMP_PADCTRL_DRVUPDN_OFFSET_MASK		0x07FFF000
96 
97 #define SDHCI_TEGRA_AUTO_CAL_STATUS			0x1ec
98 #define SDHCI_TEGRA_AUTO_CAL_ACTIVE			BIT(31)
99 
100 #define NVQUIRK_FORCE_SDHCI_SPEC_200			BIT(0)
101 #define NVQUIRK_ENABLE_BLOCK_GAP_DET			BIT(1)
102 #define NVQUIRK_ENABLE_SDHCI_SPEC_300			BIT(2)
103 #define NVQUIRK_ENABLE_SDR50				BIT(3)
104 #define NVQUIRK_ENABLE_SDR104				BIT(4)
105 #define NVQUIRK_ENABLE_DDR50				BIT(5)
106 #define NVQUIRK_HAS_PADCALIB				BIT(6)
107 #define NVQUIRK_NEEDS_PAD_CONTROL			BIT(7)
108 #define NVQUIRK_DIS_CARD_CLK_CONFIG_TAP			BIT(8)
109 #define NVQUIRK_CQHCI_DCMD_R1B_CMD_TIMING		BIT(9)
110 
111 /* SDMMC CQE Base Address for Tegra Host Ver 4.1 and Higher */
112 #define SDHCI_TEGRA_CQE_BASE_ADDR			0xF000
113 
114 struct sdhci_tegra_soc_data {
115 	const struct sdhci_pltfm_data *pdata;
116 	u32 nvquirks;
117 	u8 min_tap_delay;
118 	u8 max_tap_delay;
119 };
120 
121 /* Magic pull up and pull down pad calibration offsets */
122 struct sdhci_tegra_autocal_offsets {
123 	u32 pull_up_3v3;
124 	u32 pull_down_3v3;
125 	u32 pull_up_3v3_timeout;
126 	u32 pull_down_3v3_timeout;
127 	u32 pull_up_1v8;
128 	u32 pull_down_1v8;
129 	u32 pull_up_1v8_timeout;
130 	u32 pull_down_1v8_timeout;
131 	u32 pull_up_sdr104;
132 	u32 pull_down_sdr104;
133 	u32 pull_up_hs400;
134 	u32 pull_down_hs400;
135 };
136 
137 struct sdhci_tegra {
138 	const struct sdhci_tegra_soc_data *soc_data;
139 	struct gpio_desc *power_gpio;
140 	bool ddr_signaling;
141 	bool pad_calib_required;
142 	bool pad_control_available;
143 
144 	struct reset_control *rst;
145 	struct pinctrl *pinctrl_sdmmc;
146 	struct pinctrl_state *pinctrl_state_3v3;
147 	struct pinctrl_state *pinctrl_state_1v8;
148 	struct pinctrl_state *pinctrl_state_3v3_drv;
149 	struct pinctrl_state *pinctrl_state_1v8_drv;
150 
151 	struct sdhci_tegra_autocal_offsets autocal_offsets;
152 	ktime_t last_calib;
153 
154 	u32 default_tap;
155 	u32 default_trim;
156 	u32 dqs_trim;
157 	bool enable_hwcq;
158 	unsigned long curr_clk_rate;
159 	u8 tuned_tap_delay;
160 };
161 
162 static u16 tegra_sdhci_readw(struct sdhci_host *host, int reg)
163 {
164 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
165 	struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
166 	const struct sdhci_tegra_soc_data *soc_data = tegra_host->soc_data;
167 
168 	if (unlikely((soc_data->nvquirks & NVQUIRK_FORCE_SDHCI_SPEC_200) &&
169 			(reg == SDHCI_HOST_VERSION))) {
170 		/* Erratum: Version register is invalid in HW. */
171 		return SDHCI_SPEC_200;
172 	}
173 
174 	return readw(host->ioaddr + reg);
175 }
176 
177 static void tegra_sdhci_writew(struct sdhci_host *host, u16 val, int reg)
178 {
179 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
180 
181 	switch (reg) {
182 	case SDHCI_TRANSFER_MODE:
183 		/*
184 		 * Postpone this write, we must do it together with a
185 		 * command write that is down below.
186 		 */
187 		pltfm_host->xfer_mode_shadow = val;
188 		return;
189 	case SDHCI_COMMAND:
190 		writel((val << 16) | pltfm_host->xfer_mode_shadow,
191 			host->ioaddr + SDHCI_TRANSFER_MODE);
192 		return;
193 	}
194 
195 	writew(val, host->ioaddr + reg);
196 }
197 
198 static void tegra_sdhci_writel(struct sdhci_host *host, u32 val, int reg)
199 {
200 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
201 	struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
202 	const struct sdhci_tegra_soc_data *soc_data = tegra_host->soc_data;
203 
204 	/* Seems like we're getting spurious timeout and crc errors, so
205 	 * disable signalling of them. In case of real errors software
206 	 * timers should take care of eventually detecting them.
207 	 */
208 	if (unlikely(reg == SDHCI_SIGNAL_ENABLE))
209 		val &= ~(SDHCI_INT_TIMEOUT|SDHCI_INT_CRC);
210 
211 	writel(val, host->ioaddr + reg);
212 
213 	if (unlikely((soc_data->nvquirks & NVQUIRK_ENABLE_BLOCK_GAP_DET) &&
214 			(reg == SDHCI_INT_ENABLE))) {
215 		/* Erratum: Must enable block gap interrupt detection */
216 		u8 gap_ctrl = readb(host->ioaddr + SDHCI_BLOCK_GAP_CONTROL);
217 		if (val & SDHCI_INT_CARD_INT)
218 			gap_ctrl |= 0x8;
219 		else
220 			gap_ctrl &= ~0x8;
221 		writeb(gap_ctrl, host->ioaddr + SDHCI_BLOCK_GAP_CONTROL);
222 	}
223 }
224 
225 static bool tegra_sdhci_configure_card_clk(struct sdhci_host *host, bool enable)
226 {
227 	bool status;
228 	u32 reg;
229 
230 	reg = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
231 	status = !!(reg & SDHCI_CLOCK_CARD_EN);
232 
233 	if (status == enable)
234 		return status;
235 
236 	if (enable)
237 		reg |= SDHCI_CLOCK_CARD_EN;
238 	else
239 		reg &= ~SDHCI_CLOCK_CARD_EN;
240 
241 	sdhci_writew(host, reg, SDHCI_CLOCK_CONTROL);
242 
243 	return status;
244 }
245 
246 static void tegra210_sdhci_writew(struct sdhci_host *host, u16 val, int reg)
247 {
248 	bool is_tuning_cmd = 0;
249 	bool clk_enabled;
250 	u8 cmd;
251 
252 	if (reg == SDHCI_COMMAND) {
253 		cmd = SDHCI_GET_CMD(val);
254 		is_tuning_cmd = cmd == MMC_SEND_TUNING_BLOCK ||
255 				cmd == MMC_SEND_TUNING_BLOCK_HS200;
256 	}
257 
258 	if (is_tuning_cmd)
259 		clk_enabled = tegra_sdhci_configure_card_clk(host, 0);
260 
261 	writew(val, host->ioaddr + reg);
262 
263 	if (is_tuning_cmd) {
264 		udelay(1);
265 		sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
266 		tegra_sdhci_configure_card_clk(host, clk_enabled);
267 	}
268 }
269 
270 static bool tegra_sdhci_is_pad_and_regulator_valid(struct sdhci_host *host)
271 {
272 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
273 	struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
274 	int has_1v8, has_3v3;
275 
276 	/*
277 	 * The SoCs which have NVQUIRK_NEEDS_PAD_CONTROL require software pad
278 	 * voltage configuration in order to perform voltage switching. This
279 	 * means that valid pinctrl info is required on SDHCI instances capable
280 	 * of performing voltage switching. Whether or not an SDHCI instance is
281 	 * capable of voltage switching is determined based on the regulator.
282 	 */
283 
284 	if (!(tegra_host->soc_data->nvquirks & NVQUIRK_NEEDS_PAD_CONTROL))
285 		return true;
286 
287 	if (IS_ERR(host->mmc->supply.vqmmc))
288 		return false;
289 
290 	has_1v8 = regulator_is_supported_voltage(host->mmc->supply.vqmmc,
291 						 1700000, 1950000);
292 
293 	has_3v3 = regulator_is_supported_voltage(host->mmc->supply.vqmmc,
294 						 2700000, 3600000);
295 
296 	if (has_1v8 == 1 && has_3v3 == 1)
297 		return tegra_host->pad_control_available;
298 
299 	/* Fixed voltage, no pad control required. */
300 	return true;
301 }
302 
303 static void tegra_sdhci_set_tap(struct sdhci_host *host, unsigned int tap)
304 {
305 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
306 	struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
307 	const struct sdhci_tegra_soc_data *soc_data = tegra_host->soc_data;
308 	bool card_clk_enabled = false;
309 	u32 reg;
310 
311 	/*
312 	 * Touching the tap values is a bit tricky on some SoC generations.
313 	 * The quirk enables a workaround for a glitch that sometimes occurs if
314 	 * the tap values are changed.
315 	 */
316 
317 	if (soc_data->nvquirks & NVQUIRK_DIS_CARD_CLK_CONFIG_TAP)
318 		card_clk_enabled = tegra_sdhci_configure_card_clk(host, false);
319 
320 	reg = sdhci_readl(host, SDHCI_TEGRA_VENDOR_CLOCK_CTRL);
321 	reg &= ~SDHCI_CLOCK_CTRL_TAP_MASK;
322 	reg |= tap << SDHCI_CLOCK_CTRL_TAP_SHIFT;
323 	sdhci_writel(host, reg, SDHCI_TEGRA_VENDOR_CLOCK_CTRL);
324 
325 	if (soc_data->nvquirks & NVQUIRK_DIS_CARD_CLK_CONFIG_TAP &&
326 	    card_clk_enabled) {
327 		udelay(1);
328 		sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
329 		tegra_sdhci_configure_card_clk(host, card_clk_enabled);
330 	}
331 }
332 
333 static void tegra_sdhci_hs400_enhanced_strobe(struct mmc_host *mmc,
334 					      struct mmc_ios *ios)
335 {
336 	struct sdhci_host *host = mmc_priv(mmc);
337 	u32 val;
338 
339 	val = sdhci_readl(host, SDHCI_TEGRA_VENDOR_SYS_SW_CTRL);
340 
341 	if (ios->enhanced_strobe)
342 		val |= SDHCI_TEGRA_SYS_SW_CTRL_ENHANCED_STROBE;
343 	else
344 		val &= ~SDHCI_TEGRA_SYS_SW_CTRL_ENHANCED_STROBE;
345 
346 	sdhci_writel(host, val, SDHCI_TEGRA_VENDOR_SYS_SW_CTRL);
347 
348 }
349 
350 static void tegra_sdhci_reset(struct sdhci_host *host, u8 mask)
351 {
352 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
353 	struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
354 	const struct sdhci_tegra_soc_data *soc_data = tegra_host->soc_data;
355 	u32 misc_ctrl, clk_ctrl, pad_ctrl;
356 
357 	sdhci_reset(host, mask);
358 
359 	if (!(mask & SDHCI_RESET_ALL))
360 		return;
361 
362 	tegra_sdhci_set_tap(host, tegra_host->default_tap);
363 
364 	misc_ctrl = sdhci_readl(host, SDHCI_TEGRA_VENDOR_MISC_CTRL);
365 	clk_ctrl = sdhci_readl(host, SDHCI_TEGRA_VENDOR_CLOCK_CTRL);
366 
367 	misc_ctrl &= ~(SDHCI_MISC_CTRL_ENABLE_SDHCI_SPEC_300 |
368 		       SDHCI_MISC_CTRL_ENABLE_SDR50 |
369 		       SDHCI_MISC_CTRL_ENABLE_DDR50 |
370 		       SDHCI_MISC_CTRL_ENABLE_SDR104);
371 
372 	clk_ctrl &= ~(SDHCI_CLOCK_CTRL_TRIM_MASK |
373 		      SDHCI_CLOCK_CTRL_SPI_MODE_CLKEN_OVERRIDE);
374 
375 	if (tegra_sdhci_is_pad_and_regulator_valid(host)) {
376 		/* Erratum: Enable SDHCI spec v3.00 support */
377 		if (soc_data->nvquirks & NVQUIRK_ENABLE_SDHCI_SPEC_300)
378 			misc_ctrl |= SDHCI_MISC_CTRL_ENABLE_SDHCI_SPEC_300;
379 		/* Advertise UHS modes as supported by host */
380 		if (soc_data->nvquirks & NVQUIRK_ENABLE_SDR50)
381 			misc_ctrl |= SDHCI_MISC_CTRL_ENABLE_SDR50;
382 		if (soc_data->nvquirks & NVQUIRK_ENABLE_DDR50)
383 			misc_ctrl |= SDHCI_MISC_CTRL_ENABLE_DDR50;
384 		if (soc_data->nvquirks & NVQUIRK_ENABLE_SDR104)
385 			misc_ctrl |= SDHCI_MISC_CTRL_ENABLE_SDR104;
386 		if (soc_data->nvquirks & SDHCI_MISC_CTRL_ENABLE_SDR50)
387 			clk_ctrl |= SDHCI_CLOCK_CTRL_SDR50_TUNING_OVERRIDE;
388 	}
389 
390 	clk_ctrl |= tegra_host->default_trim << SDHCI_CLOCK_CTRL_TRIM_SHIFT;
391 
392 	sdhci_writel(host, misc_ctrl, SDHCI_TEGRA_VENDOR_MISC_CTRL);
393 	sdhci_writel(host, clk_ctrl, SDHCI_TEGRA_VENDOR_CLOCK_CTRL);
394 
395 	if (soc_data->nvquirks & NVQUIRK_HAS_PADCALIB) {
396 		pad_ctrl = sdhci_readl(host, SDHCI_TEGRA_SDMEM_COMP_PADCTRL);
397 		pad_ctrl &= ~SDHCI_TEGRA_SDMEM_COMP_PADCTRL_VREF_SEL_MASK;
398 		pad_ctrl |= SDHCI_TEGRA_SDMEM_COMP_PADCTRL_VREF_SEL_VAL;
399 		sdhci_writel(host, pad_ctrl, SDHCI_TEGRA_SDMEM_COMP_PADCTRL);
400 
401 		tegra_host->pad_calib_required = true;
402 	}
403 
404 	tegra_host->ddr_signaling = false;
405 }
406 
407 static void tegra_sdhci_configure_cal_pad(struct sdhci_host *host, bool enable)
408 {
409 	u32 val;
410 
411 	/*
412 	 * Enable or disable the additional I/O pad used by the drive strength
413 	 * calibration process.
414 	 */
415 	val = sdhci_readl(host, SDHCI_TEGRA_SDMEM_COMP_PADCTRL);
416 
417 	if (enable)
418 		val |= SDHCI_TEGRA_SDMEM_COMP_PADCTRL_E_INPUT_E_PWRD;
419 	else
420 		val &= ~SDHCI_TEGRA_SDMEM_COMP_PADCTRL_E_INPUT_E_PWRD;
421 
422 	sdhci_writel(host, val, SDHCI_TEGRA_SDMEM_COMP_PADCTRL);
423 
424 	if (enable)
425 		usleep_range(1, 2);
426 }
427 
428 static void tegra_sdhci_set_pad_autocal_offset(struct sdhci_host *host,
429 					       u16 pdpu)
430 {
431 	u32 reg;
432 
433 	reg = sdhci_readl(host, SDHCI_TEGRA_AUTO_CAL_CONFIG);
434 	reg &= ~SDHCI_AUTO_CAL_PDPU_OFFSET_MASK;
435 	reg |= pdpu;
436 	sdhci_writel(host, reg, SDHCI_TEGRA_AUTO_CAL_CONFIG);
437 }
438 
439 static int tegra_sdhci_set_padctrl(struct sdhci_host *host, int voltage,
440 				   bool state_drvupdn)
441 {
442 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
443 	struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
444 	struct sdhci_tegra_autocal_offsets *offsets =
445 						&tegra_host->autocal_offsets;
446 	struct pinctrl_state *pinctrl_drvupdn = NULL;
447 	int ret = 0;
448 	u8 drvup = 0, drvdn = 0;
449 	u32 reg;
450 
451 	if (!state_drvupdn) {
452 		/* PADS Drive Strength */
453 		if (voltage == MMC_SIGNAL_VOLTAGE_180) {
454 			if (tegra_host->pinctrl_state_1v8_drv) {
455 				pinctrl_drvupdn =
456 					tegra_host->pinctrl_state_1v8_drv;
457 			} else {
458 				drvup = offsets->pull_up_1v8_timeout;
459 				drvdn = offsets->pull_down_1v8_timeout;
460 			}
461 		} else {
462 			if (tegra_host->pinctrl_state_3v3_drv) {
463 				pinctrl_drvupdn =
464 					tegra_host->pinctrl_state_3v3_drv;
465 			} else {
466 				drvup = offsets->pull_up_3v3_timeout;
467 				drvdn = offsets->pull_down_3v3_timeout;
468 			}
469 		}
470 
471 		if (pinctrl_drvupdn != NULL) {
472 			ret = pinctrl_select_state(tegra_host->pinctrl_sdmmc,
473 							pinctrl_drvupdn);
474 			if (ret < 0)
475 				dev_err(mmc_dev(host->mmc),
476 					"failed pads drvupdn, ret: %d\n", ret);
477 		} else if ((drvup) || (drvdn)) {
478 			reg = sdhci_readl(host,
479 					SDHCI_TEGRA_SDMEM_COMP_PADCTRL);
480 			reg &= ~SDHCI_COMP_PADCTRL_DRVUPDN_OFFSET_MASK;
481 			reg |= (drvup << 20) | (drvdn << 12);
482 			sdhci_writel(host, reg,
483 					SDHCI_TEGRA_SDMEM_COMP_PADCTRL);
484 		}
485 
486 	} else {
487 		/* Dual Voltage PADS Voltage selection */
488 		if (!tegra_host->pad_control_available)
489 			return 0;
490 
491 		if (voltage == MMC_SIGNAL_VOLTAGE_180) {
492 			ret = pinctrl_select_state(tegra_host->pinctrl_sdmmc,
493 						tegra_host->pinctrl_state_1v8);
494 			if (ret < 0)
495 				dev_err(mmc_dev(host->mmc),
496 					"setting 1.8V failed, ret: %d\n", ret);
497 		} else {
498 			ret = pinctrl_select_state(tegra_host->pinctrl_sdmmc,
499 						tegra_host->pinctrl_state_3v3);
500 			if (ret < 0)
501 				dev_err(mmc_dev(host->mmc),
502 					"setting 3.3V failed, ret: %d\n", ret);
503 		}
504 	}
505 
506 	return ret;
507 }
508 
509 static void tegra_sdhci_pad_autocalib(struct sdhci_host *host)
510 {
511 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
512 	struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
513 	struct sdhci_tegra_autocal_offsets offsets =
514 			tegra_host->autocal_offsets;
515 	struct mmc_ios *ios = &host->mmc->ios;
516 	bool card_clk_enabled;
517 	u16 pdpu;
518 	u32 reg;
519 	int ret;
520 
521 	switch (ios->timing) {
522 	case MMC_TIMING_UHS_SDR104:
523 		pdpu = offsets.pull_down_sdr104 << 8 | offsets.pull_up_sdr104;
524 		break;
525 	case MMC_TIMING_MMC_HS400:
526 		pdpu = offsets.pull_down_hs400 << 8 | offsets.pull_up_hs400;
527 		break;
528 	default:
529 		if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)
530 			pdpu = offsets.pull_down_1v8 << 8 | offsets.pull_up_1v8;
531 		else
532 			pdpu = offsets.pull_down_3v3 << 8 | offsets.pull_up_3v3;
533 	}
534 
535 	/* Set initial offset before auto-calibration */
536 	tegra_sdhci_set_pad_autocal_offset(host, pdpu);
537 
538 	card_clk_enabled = tegra_sdhci_configure_card_clk(host, false);
539 
540 	tegra_sdhci_configure_cal_pad(host, true);
541 
542 	reg = sdhci_readl(host, SDHCI_TEGRA_AUTO_CAL_CONFIG);
543 	reg |= SDHCI_AUTO_CAL_ENABLE | SDHCI_AUTO_CAL_START;
544 	sdhci_writel(host, reg, SDHCI_TEGRA_AUTO_CAL_CONFIG);
545 
546 	usleep_range(1, 2);
547 	/* 10 ms timeout */
548 	ret = readl_poll_timeout(host->ioaddr + SDHCI_TEGRA_AUTO_CAL_STATUS,
549 				 reg, !(reg & SDHCI_TEGRA_AUTO_CAL_ACTIVE),
550 				 1000, 10000);
551 
552 	tegra_sdhci_configure_cal_pad(host, false);
553 
554 	tegra_sdhci_configure_card_clk(host, card_clk_enabled);
555 
556 	if (ret) {
557 		dev_err(mmc_dev(host->mmc), "Pad autocal timed out\n");
558 
559 		/* Disable automatic cal and use fixed Drive Strengths */
560 		reg = sdhci_readl(host, SDHCI_TEGRA_AUTO_CAL_CONFIG);
561 		reg &= ~SDHCI_AUTO_CAL_ENABLE;
562 		sdhci_writel(host, reg, SDHCI_TEGRA_AUTO_CAL_CONFIG);
563 
564 		ret = tegra_sdhci_set_padctrl(host, ios->signal_voltage, false);
565 		if (ret < 0)
566 			dev_err(mmc_dev(host->mmc),
567 				"Setting drive strengths failed: %d\n", ret);
568 	}
569 }
570 
571 static void tegra_sdhci_parse_pad_autocal_dt(struct sdhci_host *host)
572 {
573 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
574 	struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
575 	struct sdhci_tegra_autocal_offsets *autocal =
576 			&tegra_host->autocal_offsets;
577 	int err;
578 
579 	err = device_property_read_u32(host->mmc->parent,
580 			"nvidia,pad-autocal-pull-up-offset-3v3",
581 			&autocal->pull_up_3v3);
582 	if (err)
583 		autocal->pull_up_3v3 = 0;
584 
585 	err = device_property_read_u32(host->mmc->parent,
586 			"nvidia,pad-autocal-pull-down-offset-3v3",
587 			&autocal->pull_down_3v3);
588 	if (err)
589 		autocal->pull_down_3v3 = 0;
590 
591 	err = device_property_read_u32(host->mmc->parent,
592 			"nvidia,pad-autocal-pull-up-offset-1v8",
593 			&autocal->pull_up_1v8);
594 	if (err)
595 		autocal->pull_up_1v8 = 0;
596 
597 	err = device_property_read_u32(host->mmc->parent,
598 			"nvidia,pad-autocal-pull-down-offset-1v8",
599 			&autocal->pull_down_1v8);
600 	if (err)
601 		autocal->pull_down_1v8 = 0;
602 
603 	err = device_property_read_u32(host->mmc->parent,
604 			"nvidia,pad-autocal-pull-up-offset-3v3-timeout",
605 			&autocal->pull_up_3v3_timeout);
606 	if (err) {
607 		if (!IS_ERR(tegra_host->pinctrl_state_3v3) &&
608 			(tegra_host->pinctrl_state_3v3_drv == NULL))
609 			pr_warn("%s: Missing autocal timeout 3v3-pad drvs\n",
610 				mmc_hostname(host->mmc));
611 		autocal->pull_up_3v3_timeout = 0;
612 	}
613 
614 	err = device_property_read_u32(host->mmc->parent,
615 			"nvidia,pad-autocal-pull-down-offset-3v3-timeout",
616 			&autocal->pull_down_3v3_timeout);
617 	if (err) {
618 		if (!IS_ERR(tegra_host->pinctrl_state_3v3) &&
619 			(tegra_host->pinctrl_state_3v3_drv == NULL))
620 			pr_warn("%s: Missing autocal timeout 3v3-pad drvs\n",
621 				mmc_hostname(host->mmc));
622 		autocal->pull_down_3v3_timeout = 0;
623 	}
624 
625 	err = device_property_read_u32(host->mmc->parent,
626 			"nvidia,pad-autocal-pull-up-offset-1v8-timeout",
627 			&autocal->pull_up_1v8_timeout);
628 	if (err) {
629 		if (!IS_ERR(tegra_host->pinctrl_state_1v8) &&
630 			(tegra_host->pinctrl_state_1v8_drv == NULL))
631 			pr_warn("%s: Missing autocal timeout 1v8-pad drvs\n",
632 				mmc_hostname(host->mmc));
633 		autocal->pull_up_1v8_timeout = 0;
634 	}
635 
636 	err = device_property_read_u32(host->mmc->parent,
637 			"nvidia,pad-autocal-pull-down-offset-1v8-timeout",
638 			&autocal->pull_down_1v8_timeout);
639 	if (err) {
640 		if (!IS_ERR(tegra_host->pinctrl_state_1v8) &&
641 			(tegra_host->pinctrl_state_1v8_drv == NULL))
642 			pr_warn("%s: Missing autocal timeout 1v8-pad drvs\n",
643 				mmc_hostname(host->mmc));
644 		autocal->pull_down_1v8_timeout = 0;
645 	}
646 
647 	err = device_property_read_u32(host->mmc->parent,
648 			"nvidia,pad-autocal-pull-up-offset-sdr104",
649 			&autocal->pull_up_sdr104);
650 	if (err)
651 		autocal->pull_up_sdr104 = autocal->pull_up_1v8;
652 
653 	err = device_property_read_u32(host->mmc->parent,
654 			"nvidia,pad-autocal-pull-down-offset-sdr104",
655 			&autocal->pull_down_sdr104);
656 	if (err)
657 		autocal->pull_down_sdr104 = autocal->pull_down_1v8;
658 
659 	err = device_property_read_u32(host->mmc->parent,
660 			"nvidia,pad-autocal-pull-up-offset-hs400",
661 			&autocal->pull_up_hs400);
662 	if (err)
663 		autocal->pull_up_hs400 = autocal->pull_up_1v8;
664 
665 	err = device_property_read_u32(host->mmc->parent,
666 			"nvidia,pad-autocal-pull-down-offset-hs400",
667 			&autocal->pull_down_hs400);
668 	if (err)
669 		autocal->pull_down_hs400 = autocal->pull_down_1v8;
670 }
671 
672 static void tegra_sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
673 {
674 	struct sdhci_host *host = mmc_priv(mmc);
675 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
676 	struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
677 	ktime_t since_calib = ktime_sub(ktime_get(), tegra_host->last_calib);
678 
679 	/* 100 ms calibration interval is specified in the TRM */
680 	if (ktime_to_ms(since_calib) > 100) {
681 		tegra_sdhci_pad_autocalib(host);
682 		tegra_host->last_calib = ktime_get();
683 	}
684 
685 	sdhci_request(mmc, mrq);
686 }
687 
688 static void tegra_sdhci_parse_tap_and_trim(struct sdhci_host *host)
689 {
690 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
691 	struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
692 	int err;
693 
694 	err = device_property_read_u32(host->mmc->parent, "nvidia,default-tap",
695 				       &tegra_host->default_tap);
696 	if (err)
697 		tegra_host->default_tap = 0;
698 
699 	err = device_property_read_u32(host->mmc->parent, "nvidia,default-trim",
700 				       &tegra_host->default_trim);
701 	if (err)
702 		tegra_host->default_trim = 0;
703 
704 	err = device_property_read_u32(host->mmc->parent, "nvidia,dqs-trim",
705 				       &tegra_host->dqs_trim);
706 	if (err)
707 		tegra_host->dqs_trim = 0x11;
708 }
709 
710 static void tegra_sdhci_parse_dt(struct sdhci_host *host)
711 {
712 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
713 	struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
714 
715 	if (device_property_read_bool(host->mmc->parent, "supports-cqe"))
716 		tegra_host->enable_hwcq = true;
717 	else
718 		tegra_host->enable_hwcq = false;
719 
720 	tegra_sdhci_parse_pad_autocal_dt(host);
721 	tegra_sdhci_parse_tap_and_trim(host);
722 }
723 
724 static void tegra_sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
725 {
726 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
727 	struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
728 	unsigned long host_clk;
729 
730 	if (!clock)
731 		return sdhci_set_clock(host, clock);
732 
733 	/*
734 	 * In DDR50/52 modes the Tegra SDHCI controllers require the SDHCI
735 	 * divider to be configured to divided the host clock by two. The SDHCI
736 	 * clock divider is calculated as part of sdhci_set_clock() by
737 	 * sdhci_calc_clk(). The divider is calculated from host->max_clk and
738 	 * the requested clock rate.
739 	 *
740 	 * By setting the host->max_clk to clock * 2 the divider calculation
741 	 * will always result in the correct value for DDR50/52 modes,
742 	 * regardless of clock rate rounding, which may happen if the value
743 	 * from clk_get_rate() is used.
744 	 */
745 	host_clk = tegra_host->ddr_signaling ? clock * 2 : clock;
746 	clk_set_rate(pltfm_host->clk, host_clk);
747 	tegra_host->curr_clk_rate = host_clk;
748 	if (tegra_host->ddr_signaling)
749 		host->max_clk = host_clk;
750 	else
751 		host->max_clk = clk_get_rate(pltfm_host->clk);
752 
753 	sdhci_set_clock(host, clock);
754 
755 	if (tegra_host->pad_calib_required) {
756 		tegra_sdhci_pad_autocalib(host);
757 		tegra_host->pad_calib_required = false;
758 	}
759 }
760 
761 static unsigned int tegra_sdhci_get_max_clock(struct sdhci_host *host)
762 {
763 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
764 
765 	return clk_round_rate(pltfm_host->clk, UINT_MAX);
766 }
767 
768 static void tegra_sdhci_set_dqs_trim(struct sdhci_host *host, u8 trim)
769 {
770 	u32 val;
771 
772 	val = sdhci_readl(host, SDHCI_TEGRA_VENDOR_CAP_OVERRIDES);
773 	val &= ~SDHCI_TEGRA_CAP_OVERRIDES_DQS_TRIM_MASK;
774 	val |= trim << SDHCI_TEGRA_CAP_OVERRIDES_DQS_TRIM_SHIFT;
775 	sdhci_writel(host, val, SDHCI_TEGRA_VENDOR_CAP_OVERRIDES);
776 }
777 
778 static void tegra_sdhci_hs400_dll_cal(struct sdhci_host *host)
779 {
780 	u32 reg;
781 	int err;
782 
783 	reg = sdhci_readl(host, SDHCI_TEGRA_VENDOR_DLLCAL_CFG);
784 	reg |= SDHCI_TEGRA_DLLCAL_CALIBRATE;
785 	sdhci_writel(host, reg, SDHCI_TEGRA_VENDOR_DLLCAL_CFG);
786 
787 	/* 1 ms sleep, 5 ms timeout */
788 	err = readl_poll_timeout(host->ioaddr + SDHCI_TEGRA_VENDOR_DLLCAL_STA,
789 				 reg, !(reg & SDHCI_TEGRA_DLLCAL_STA_ACTIVE),
790 				 1000, 5000);
791 	if (err)
792 		dev_err(mmc_dev(host->mmc),
793 			"HS400 delay line calibration timed out\n");
794 }
795 
796 static void tegra_sdhci_tap_correction(struct sdhci_host *host, u8 thd_up,
797 				       u8 thd_low, u8 fixed_tap)
798 {
799 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
800 	struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
801 	u32 val, tun_status;
802 	u8 word, bit, edge1, tap, window;
803 	bool tap_result;
804 	bool start_fail = false;
805 	bool start_pass = false;
806 	bool end_pass = false;
807 	bool first_fail = false;
808 	bool first_pass = false;
809 	u8 start_pass_tap = 0;
810 	u8 end_pass_tap = 0;
811 	u8 first_fail_tap = 0;
812 	u8 first_pass_tap = 0;
813 	u8 total_tuning_words = host->tuning_loop_count / TUNING_WORD_BIT_SIZE;
814 
815 	/*
816 	 * Read auto-tuned results and extract good valid passing window by
817 	 * filtering out un-wanted bubble/partial/merged windows.
818 	 */
819 	for (word = 0; word < total_tuning_words; word++) {
820 		val = sdhci_readl(host, SDHCI_VNDR_TUN_CTRL0_0);
821 		val &= ~SDHCI_VNDR_TUN_CTRL0_TUN_WORD_SEL_MASK;
822 		val |= word;
823 		sdhci_writel(host, val, SDHCI_VNDR_TUN_CTRL0_0);
824 		tun_status = sdhci_readl(host, SDHCI_TEGRA_VNDR_TUN_STATUS0);
825 		bit = 0;
826 		while (bit < TUNING_WORD_BIT_SIZE) {
827 			tap = word * TUNING_WORD_BIT_SIZE + bit;
828 			tap_result = tun_status & (1 << bit);
829 			if (!tap_result && !start_fail) {
830 				start_fail = true;
831 				if (!first_fail) {
832 					first_fail_tap = tap;
833 					first_fail = true;
834 				}
835 
836 			} else if (tap_result && start_fail && !start_pass) {
837 				start_pass_tap = tap;
838 				start_pass = true;
839 				if (!first_pass) {
840 					first_pass_tap = tap;
841 					first_pass = true;
842 				}
843 
844 			} else if (!tap_result && start_fail && start_pass &&
845 				   !end_pass) {
846 				end_pass_tap = tap - 1;
847 				end_pass = true;
848 			} else if (tap_result && start_pass && start_fail &&
849 				   end_pass) {
850 				window = end_pass_tap - start_pass_tap;
851 				/* discard merged window and bubble window */
852 				if (window >= thd_up || window < thd_low) {
853 					start_pass_tap = tap;
854 					end_pass = false;
855 				} else {
856 					/* set tap at middle of valid window */
857 					tap = start_pass_tap + window / 2;
858 					tegra_host->tuned_tap_delay = tap;
859 					return;
860 				}
861 			}
862 
863 			bit++;
864 		}
865 	}
866 
867 	if (!first_fail) {
868 		WARN_ON("no edge detected, continue with hw tuned delay.\n");
869 	} else if (first_pass) {
870 		/* set tap location at fixed tap relative to the first edge */
871 		edge1 = first_fail_tap + (first_pass_tap - first_fail_tap) / 2;
872 		if (edge1 - 1 > fixed_tap)
873 			tegra_host->tuned_tap_delay = edge1 - fixed_tap;
874 		else
875 			tegra_host->tuned_tap_delay = edge1 + fixed_tap;
876 	}
877 }
878 
879 static void tegra_sdhci_post_tuning(struct sdhci_host *host)
880 {
881 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
882 	struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
883 	const struct sdhci_tegra_soc_data *soc_data = tegra_host->soc_data;
884 	u32 avg_tap_dly, val, min_tap_dly, max_tap_dly;
885 	u8 fixed_tap, start_tap, end_tap, window_width;
886 	u8 thdupper, thdlower;
887 	u8 num_iter;
888 	u32 clk_rate_mhz, period_ps, bestcase, worstcase;
889 
890 	/* retain HW tuned tap to use incase if no correction is needed */
891 	val = sdhci_readl(host, SDHCI_TEGRA_VENDOR_CLOCK_CTRL);
892 	tegra_host->tuned_tap_delay = (val & SDHCI_CLOCK_CTRL_TAP_MASK) >>
893 				      SDHCI_CLOCK_CTRL_TAP_SHIFT;
894 	if (soc_data->min_tap_delay && soc_data->max_tap_delay) {
895 		min_tap_dly = soc_data->min_tap_delay;
896 		max_tap_dly = soc_data->max_tap_delay;
897 		clk_rate_mhz = tegra_host->curr_clk_rate / USEC_PER_SEC;
898 		period_ps = USEC_PER_SEC / clk_rate_mhz;
899 		bestcase = period_ps / min_tap_dly;
900 		worstcase = period_ps / max_tap_dly;
901 		/*
902 		 * Upper and Lower bound thresholds used to detect merged and
903 		 * bubble windows
904 		 */
905 		thdupper = (2 * worstcase + bestcase) / 2;
906 		thdlower = worstcase / 4;
907 		/*
908 		 * fixed tap is used when HW tuning result contains single edge
909 		 * and tap is set at fixed tap delay relative to the first edge
910 		 */
911 		avg_tap_dly = (period_ps * 2) / (min_tap_dly + max_tap_dly);
912 		fixed_tap = avg_tap_dly / 2;
913 
914 		val = sdhci_readl(host, SDHCI_TEGRA_VNDR_TUN_STATUS1);
915 		start_tap = val & SDHCI_TEGRA_VNDR_TUN_STATUS1_TAP_MASK;
916 		end_tap = (val >> SDHCI_TEGRA_VNDR_TUN_STATUS1_END_TAP_SHIFT) &
917 			  SDHCI_TEGRA_VNDR_TUN_STATUS1_TAP_MASK;
918 		window_width = end_tap - start_tap;
919 		num_iter = host->tuning_loop_count;
920 		/*
921 		 * partial window includes edges of the tuning range.
922 		 * merged window includes more taps so window width is higher
923 		 * than upper threshold.
924 		 */
925 		if (start_tap == 0 || (end_tap == (num_iter - 1)) ||
926 		    (end_tap == num_iter - 2) || window_width >= thdupper) {
927 			pr_debug("%s: Apply tuning correction\n",
928 				 mmc_hostname(host->mmc));
929 			tegra_sdhci_tap_correction(host, thdupper, thdlower,
930 						   fixed_tap);
931 		}
932 	}
933 
934 	tegra_sdhci_set_tap(host, tegra_host->tuned_tap_delay);
935 }
936 
937 static int tegra_sdhci_execute_hw_tuning(struct mmc_host *mmc, u32 opcode)
938 {
939 	struct sdhci_host *host = mmc_priv(mmc);
940 	int err;
941 
942 	err = sdhci_execute_tuning(mmc, opcode);
943 	if (!err && !host->tuning_err)
944 		tegra_sdhci_post_tuning(host);
945 
946 	return err;
947 }
948 
949 static void tegra_sdhci_set_uhs_signaling(struct sdhci_host *host,
950 					  unsigned timing)
951 {
952 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
953 	struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
954 	bool set_default_tap = false;
955 	bool set_dqs_trim = false;
956 	bool do_hs400_dll_cal = false;
957 	u8 iter = TRIES_256;
958 	u32 val;
959 
960 	tegra_host->ddr_signaling = false;
961 	switch (timing) {
962 	case MMC_TIMING_UHS_SDR50:
963 		break;
964 	case MMC_TIMING_UHS_SDR104:
965 	case MMC_TIMING_MMC_HS200:
966 		/* Don't set default tap on tunable modes. */
967 		iter = TRIES_128;
968 		break;
969 	case MMC_TIMING_MMC_HS400:
970 		set_dqs_trim = true;
971 		do_hs400_dll_cal = true;
972 		iter = TRIES_128;
973 		break;
974 	case MMC_TIMING_MMC_DDR52:
975 	case MMC_TIMING_UHS_DDR50:
976 		tegra_host->ddr_signaling = true;
977 		set_default_tap = true;
978 		break;
979 	default:
980 		set_default_tap = true;
981 		break;
982 	}
983 
984 	val = sdhci_readl(host, SDHCI_VNDR_TUN_CTRL0_0);
985 	val &= ~(SDHCI_VNDR_TUN_CTRL0_TUN_ITER_MASK |
986 		 SDHCI_VNDR_TUN_CTRL0_START_TAP_VAL_MASK |
987 		 SDHCI_VNDR_TUN_CTRL0_MUL_M_MASK);
988 	val |= (iter << SDHCI_VNDR_TUN_CTRL0_TUN_ITER_SHIFT |
989 		0 << SDHCI_VNDR_TUN_CTRL0_START_TAP_VAL_SHIFT |
990 		1 << SDHCI_VNDR_TUN_CTRL0_MUL_M_SHIFT);
991 	sdhci_writel(host, val, SDHCI_VNDR_TUN_CTRL0_0);
992 	sdhci_writel(host, 0, SDHCI_TEGRA_VNDR_TUN_CTRL1_0);
993 
994 	host->tuning_loop_count = (iter == TRIES_128) ? 128 : 256;
995 
996 	sdhci_set_uhs_signaling(host, timing);
997 
998 	tegra_sdhci_pad_autocalib(host);
999 
1000 	if (tegra_host->tuned_tap_delay && !set_default_tap)
1001 		tegra_sdhci_set_tap(host, tegra_host->tuned_tap_delay);
1002 	else
1003 		tegra_sdhci_set_tap(host, tegra_host->default_tap);
1004 
1005 	if (set_dqs_trim)
1006 		tegra_sdhci_set_dqs_trim(host, tegra_host->dqs_trim);
1007 
1008 	if (do_hs400_dll_cal)
1009 		tegra_sdhci_hs400_dll_cal(host);
1010 }
1011 
1012 static int tegra_sdhci_execute_tuning(struct sdhci_host *host, u32 opcode)
1013 {
1014 	unsigned int min, max;
1015 
1016 	/*
1017 	 * Start search for minimum tap value at 10, as smaller values are
1018 	 * may wrongly be reported as working but fail at higher speeds,
1019 	 * according to the TRM.
1020 	 */
1021 	min = 10;
1022 	while (min < 255) {
1023 		tegra_sdhci_set_tap(host, min);
1024 		if (!mmc_send_tuning(host->mmc, opcode, NULL))
1025 			break;
1026 		min++;
1027 	}
1028 
1029 	/* Find the maximum tap value that still passes. */
1030 	max = min + 1;
1031 	while (max < 255) {
1032 		tegra_sdhci_set_tap(host, max);
1033 		if (mmc_send_tuning(host->mmc, opcode, NULL)) {
1034 			max--;
1035 			break;
1036 		}
1037 		max++;
1038 	}
1039 
1040 	/* The TRM states the ideal tap value is at 75% in the passing range. */
1041 	tegra_sdhci_set_tap(host, min + ((max - min) * 3 / 4));
1042 
1043 	return mmc_send_tuning(host->mmc, opcode, NULL);
1044 }
1045 
1046 static int sdhci_tegra_start_signal_voltage_switch(struct mmc_host *mmc,
1047 						   struct mmc_ios *ios)
1048 {
1049 	struct sdhci_host *host = mmc_priv(mmc);
1050 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1051 	struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
1052 	int ret = 0;
1053 
1054 	if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1055 		ret = tegra_sdhci_set_padctrl(host, ios->signal_voltage, true);
1056 		if (ret < 0)
1057 			return ret;
1058 		ret = sdhci_start_signal_voltage_switch(mmc, ios);
1059 	} else if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
1060 		ret = sdhci_start_signal_voltage_switch(mmc, ios);
1061 		if (ret < 0)
1062 			return ret;
1063 		ret = tegra_sdhci_set_padctrl(host, ios->signal_voltage, true);
1064 	}
1065 
1066 	if (tegra_host->pad_calib_required)
1067 		tegra_sdhci_pad_autocalib(host);
1068 
1069 	return ret;
1070 }
1071 
1072 static int tegra_sdhci_init_pinctrl_info(struct device *dev,
1073 					 struct sdhci_tegra *tegra_host)
1074 {
1075 	tegra_host->pinctrl_sdmmc = devm_pinctrl_get(dev);
1076 	if (IS_ERR(tegra_host->pinctrl_sdmmc)) {
1077 		dev_dbg(dev, "No pinctrl info, err: %ld\n",
1078 			PTR_ERR(tegra_host->pinctrl_sdmmc));
1079 		return -1;
1080 	}
1081 
1082 	tegra_host->pinctrl_state_1v8_drv = pinctrl_lookup_state(
1083 				tegra_host->pinctrl_sdmmc, "sdmmc-1v8-drv");
1084 	if (IS_ERR(tegra_host->pinctrl_state_1v8_drv)) {
1085 		if (PTR_ERR(tegra_host->pinctrl_state_1v8_drv) == -ENODEV)
1086 			tegra_host->pinctrl_state_1v8_drv = NULL;
1087 	}
1088 
1089 	tegra_host->pinctrl_state_3v3_drv = pinctrl_lookup_state(
1090 				tegra_host->pinctrl_sdmmc, "sdmmc-3v3-drv");
1091 	if (IS_ERR(tegra_host->pinctrl_state_3v3_drv)) {
1092 		if (PTR_ERR(tegra_host->pinctrl_state_3v3_drv) == -ENODEV)
1093 			tegra_host->pinctrl_state_3v3_drv = NULL;
1094 	}
1095 
1096 	tegra_host->pinctrl_state_3v3 =
1097 		pinctrl_lookup_state(tegra_host->pinctrl_sdmmc, "sdmmc-3v3");
1098 	if (IS_ERR(tegra_host->pinctrl_state_3v3)) {
1099 		dev_warn(dev, "Missing 3.3V pad state, err: %ld\n",
1100 			 PTR_ERR(tegra_host->pinctrl_state_3v3));
1101 		return -1;
1102 	}
1103 
1104 	tegra_host->pinctrl_state_1v8 =
1105 		pinctrl_lookup_state(tegra_host->pinctrl_sdmmc, "sdmmc-1v8");
1106 	if (IS_ERR(tegra_host->pinctrl_state_1v8)) {
1107 		dev_warn(dev, "Missing 1.8V pad state, err: %ld\n",
1108 			 PTR_ERR(tegra_host->pinctrl_state_1v8));
1109 		return -1;
1110 	}
1111 
1112 	tegra_host->pad_control_available = true;
1113 
1114 	return 0;
1115 }
1116 
1117 static void tegra_sdhci_voltage_switch(struct sdhci_host *host)
1118 {
1119 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1120 	struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
1121 	const struct sdhci_tegra_soc_data *soc_data = tegra_host->soc_data;
1122 
1123 	if (soc_data->nvquirks & NVQUIRK_HAS_PADCALIB)
1124 		tegra_host->pad_calib_required = true;
1125 }
1126 
1127 static void sdhci_tegra_update_dcmd_desc(struct mmc_host *mmc,
1128 					 struct mmc_request *mrq, u64 *data)
1129 {
1130 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(mmc_priv(mmc));
1131 	struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
1132 	const struct sdhci_tegra_soc_data *soc_data = tegra_host->soc_data;
1133 
1134 	if (soc_data->nvquirks & NVQUIRK_CQHCI_DCMD_R1B_CMD_TIMING &&
1135 	    mrq->cmd->flags & MMC_RSP_R1B)
1136 		*data |= CQHCI_CMD_TIMING(1);
1137 }
1138 
1139 static void sdhci_tegra_cqe_enable(struct mmc_host *mmc)
1140 {
1141 	struct cqhci_host *cq_host = mmc->cqe_private;
1142 	u32 cqcfg = 0;
1143 
1144 	/*
1145 	 * Tegra SDMMC Controller design prevents write access to BLOCK_COUNT
1146 	 * registers when CQE is enabled.
1147 	 */
1148 	cqcfg = cqhci_readl(cq_host, CQHCI_CFG);
1149 	if (cqcfg & CQHCI_ENABLE)
1150 		cqhci_writel(cq_host, (cqcfg & ~CQHCI_ENABLE), CQHCI_CFG);
1151 
1152 	sdhci_cqe_enable(mmc);
1153 
1154 	if (cqcfg & CQHCI_ENABLE)
1155 		cqhci_writel(cq_host, cqcfg, CQHCI_CFG);
1156 }
1157 
1158 static void sdhci_tegra_dumpregs(struct mmc_host *mmc)
1159 {
1160 	sdhci_dumpregs(mmc_priv(mmc));
1161 }
1162 
1163 static u32 sdhci_tegra_cqhci_irq(struct sdhci_host *host, u32 intmask)
1164 {
1165 	int cmd_error = 0;
1166 	int data_error = 0;
1167 
1168 	if (!sdhci_cqe_irq(host, intmask, &cmd_error, &data_error))
1169 		return intmask;
1170 
1171 	cqhci_irq(host->mmc, intmask, cmd_error, data_error);
1172 
1173 	return 0;
1174 }
1175 
1176 static const struct cqhci_host_ops sdhci_tegra_cqhci_ops = {
1177 	.enable	= sdhci_tegra_cqe_enable,
1178 	.disable = sdhci_cqe_disable,
1179 	.dumpregs = sdhci_tegra_dumpregs,
1180 	.update_dcmd_desc = sdhci_tegra_update_dcmd_desc,
1181 };
1182 
1183 static const struct sdhci_ops tegra_sdhci_ops = {
1184 	.read_w     = tegra_sdhci_readw,
1185 	.write_l    = tegra_sdhci_writel,
1186 	.set_clock  = tegra_sdhci_set_clock,
1187 	.set_bus_width = sdhci_set_bus_width,
1188 	.reset      = tegra_sdhci_reset,
1189 	.platform_execute_tuning = tegra_sdhci_execute_tuning,
1190 	.set_uhs_signaling = tegra_sdhci_set_uhs_signaling,
1191 	.voltage_switch = tegra_sdhci_voltage_switch,
1192 	.get_max_clock = tegra_sdhci_get_max_clock,
1193 };
1194 
1195 static const struct sdhci_pltfm_data sdhci_tegra20_pdata = {
1196 	.quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
1197 		  SDHCI_QUIRK_SINGLE_POWER_WRITE |
1198 		  SDHCI_QUIRK_NO_HISPD_BIT |
1199 		  SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC |
1200 		  SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
1201 	.ops  = &tegra_sdhci_ops,
1202 };
1203 
1204 static const struct sdhci_tegra_soc_data soc_data_tegra20 = {
1205 	.pdata = &sdhci_tegra20_pdata,
1206 	.nvquirks = NVQUIRK_FORCE_SDHCI_SPEC_200 |
1207 		    NVQUIRK_ENABLE_BLOCK_GAP_DET,
1208 };
1209 
1210 static const struct sdhci_pltfm_data sdhci_tegra30_pdata = {
1211 	.quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
1212 		  SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
1213 		  SDHCI_QUIRK_SINGLE_POWER_WRITE |
1214 		  SDHCI_QUIRK_NO_HISPD_BIT |
1215 		  SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC |
1216 		  SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
1217 	.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1218 		   SDHCI_QUIRK2_BROKEN_HS200 |
1219 		   /*
1220 		    * Auto-CMD23 leads to "Got command interrupt 0x00010000 even
1221 		    * though no command operation was in progress."
1222 		    *
1223 		    * The exact reason is unknown, as the same hardware seems
1224 		    * to support Auto CMD23 on a downstream 3.1 kernel.
1225 		    */
1226 		   SDHCI_QUIRK2_ACMD23_BROKEN,
1227 	.ops  = &tegra_sdhci_ops,
1228 };
1229 
1230 static const struct sdhci_tegra_soc_data soc_data_tegra30 = {
1231 	.pdata = &sdhci_tegra30_pdata,
1232 	.nvquirks = NVQUIRK_ENABLE_SDHCI_SPEC_300 |
1233 		    NVQUIRK_ENABLE_SDR50 |
1234 		    NVQUIRK_ENABLE_SDR104 |
1235 		    NVQUIRK_HAS_PADCALIB,
1236 };
1237 
1238 static const struct sdhci_ops tegra114_sdhci_ops = {
1239 	.read_w     = tegra_sdhci_readw,
1240 	.write_w    = tegra_sdhci_writew,
1241 	.write_l    = tegra_sdhci_writel,
1242 	.set_clock  = tegra_sdhci_set_clock,
1243 	.set_bus_width = sdhci_set_bus_width,
1244 	.reset      = tegra_sdhci_reset,
1245 	.platform_execute_tuning = tegra_sdhci_execute_tuning,
1246 	.set_uhs_signaling = tegra_sdhci_set_uhs_signaling,
1247 	.voltage_switch = tegra_sdhci_voltage_switch,
1248 	.get_max_clock = tegra_sdhci_get_max_clock,
1249 };
1250 
1251 static const struct sdhci_pltfm_data sdhci_tegra114_pdata = {
1252 	.quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
1253 		  SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
1254 		  SDHCI_QUIRK_SINGLE_POWER_WRITE |
1255 		  SDHCI_QUIRK_NO_HISPD_BIT |
1256 		  SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC |
1257 		  SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
1258 	.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
1259 	.ops  = &tegra114_sdhci_ops,
1260 };
1261 
1262 static const struct sdhci_tegra_soc_data soc_data_tegra114 = {
1263 	.pdata = &sdhci_tegra114_pdata,
1264 };
1265 
1266 static const struct sdhci_pltfm_data sdhci_tegra124_pdata = {
1267 	.quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
1268 		  SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
1269 		  SDHCI_QUIRK_SINGLE_POWER_WRITE |
1270 		  SDHCI_QUIRK_NO_HISPD_BIT |
1271 		  SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC |
1272 		  SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
1273 	.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1274 		   /*
1275 		    * The TRM states that the SD/MMC controller found on
1276 		    * Tegra124 can address 34 bits (the maximum supported by
1277 		    * the Tegra memory controller), but tests show that DMA
1278 		    * to or from above 4 GiB doesn't work. This is possibly
1279 		    * caused by missing programming, though it's not obvious
1280 		    * what sequence is required. Mark 64-bit DMA broken for
1281 		    * now to fix this for existing users (e.g. Nyan boards).
1282 		    */
1283 		   SDHCI_QUIRK2_BROKEN_64_BIT_DMA,
1284 	.ops  = &tegra114_sdhci_ops,
1285 };
1286 
1287 static const struct sdhci_tegra_soc_data soc_data_tegra124 = {
1288 	.pdata = &sdhci_tegra124_pdata,
1289 };
1290 
1291 static const struct sdhci_ops tegra210_sdhci_ops = {
1292 	.read_w     = tegra_sdhci_readw,
1293 	.write_w    = tegra210_sdhci_writew,
1294 	.write_l    = tegra_sdhci_writel,
1295 	.set_clock  = tegra_sdhci_set_clock,
1296 	.set_bus_width = sdhci_set_bus_width,
1297 	.reset      = tegra_sdhci_reset,
1298 	.set_uhs_signaling = tegra_sdhci_set_uhs_signaling,
1299 	.voltage_switch = tegra_sdhci_voltage_switch,
1300 	.get_max_clock = tegra_sdhci_get_max_clock,
1301 };
1302 
1303 static const struct sdhci_pltfm_data sdhci_tegra210_pdata = {
1304 	.quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
1305 		  SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
1306 		  SDHCI_QUIRK_SINGLE_POWER_WRITE |
1307 		  SDHCI_QUIRK_NO_HISPD_BIT |
1308 		  SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC |
1309 		  SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
1310 	.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
1311 	.ops  = &tegra210_sdhci_ops,
1312 };
1313 
1314 static const struct sdhci_tegra_soc_data soc_data_tegra210 = {
1315 	.pdata = &sdhci_tegra210_pdata,
1316 	.nvquirks = NVQUIRK_NEEDS_PAD_CONTROL |
1317 		    NVQUIRK_HAS_PADCALIB |
1318 		    NVQUIRK_DIS_CARD_CLK_CONFIG_TAP |
1319 		    NVQUIRK_ENABLE_SDR50 |
1320 		    NVQUIRK_ENABLE_SDR104,
1321 	.min_tap_delay = 106,
1322 	.max_tap_delay = 185,
1323 };
1324 
1325 static const struct sdhci_ops tegra186_sdhci_ops = {
1326 	.read_w     = tegra_sdhci_readw,
1327 	.write_l    = tegra_sdhci_writel,
1328 	.set_clock  = tegra_sdhci_set_clock,
1329 	.set_bus_width = sdhci_set_bus_width,
1330 	.reset      = tegra_sdhci_reset,
1331 	.set_uhs_signaling = tegra_sdhci_set_uhs_signaling,
1332 	.voltage_switch = tegra_sdhci_voltage_switch,
1333 	.get_max_clock = tegra_sdhci_get_max_clock,
1334 	.irq = sdhci_tegra_cqhci_irq,
1335 };
1336 
1337 static const struct sdhci_pltfm_data sdhci_tegra186_pdata = {
1338 	.quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
1339 		  SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
1340 		  SDHCI_QUIRK_SINGLE_POWER_WRITE |
1341 		  SDHCI_QUIRK_NO_HISPD_BIT |
1342 		  SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC |
1343 		  SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
1344 	.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1345 		   /* SDHCI controllers on Tegra186 support 40-bit addressing.
1346 		    * IOVA addresses are 48-bit wide on Tegra186.
1347 		    * With 64-bit dma mask used for SDHCI, accesses can
1348 		    * be broken. Disable 64-bit dma, which would fall back
1349 		    * to 32-bit dma mask. Ideally 40-bit dma mask would work,
1350 		    * But it is not supported as of now.
1351 		    */
1352 		   SDHCI_QUIRK2_BROKEN_64_BIT_DMA,
1353 	.ops  = &tegra186_sdhci_ops,
1354 };
1355 
1356 static const struct sdhci_tegra_soc_data soc_data_tegra186 = {
1357 	.pdata = &sdhci_tegra186_pdata,
1358 	.nvquirks = NVQUIRK_NEEDS_PAD_CONTROL |
1359 		    NVQUIRK_HAS_PADCALIB |
1360 		    NVQUIRK_DIS_CARD_CLK_CONFIG_TAP |
1361 		    NVQUIRK_ENABLE_SDR50 |
1362 		    NVQUIRK_ENABLE_SDR104 |
1363 		    NVQUIRK_CQHCI_DCMD_R1B_CMD_TIMING,
1364 	.min_tap_delay = 84,
1365 	.max_tap_delay = 136,
1366 };
1367 
1368 static const struct sdhci_tegra_soc_data soc_data_tegra194 = {
1369 	.pdata = &sdhci_tegra186_pdata,
1370 	.nvquirks = NVQUIRK_NEEDS_PAD_CONTROL |
1371 		    NVQUIRK_HAS_PADCALIB |
1372 		    NVQUIRK_DIS_CARD_CLK_CONFIG_TAP |
1373 		    NVQUIRK_ENABLE_SDR50 |
1374 		    NVQUIRK_ENABLE_SDR104,
1375 	.min_tap_delay = 96,
1376 	.max_tap_delay = 139,
1377 };
1378 
1379 static const struct of_device_id sdhci_tegra_dt_match[] = {
1380 	{ .compatible = "nvidia,tegra194-sdhci", .data = &soc_data_tegra194 },
1381 	{ .compatible = "nvidia,tegra186-sdhci", .data = &soc_data_tegra186 },
1382 	{ .compatible = "nvidia,tegra210-sdhci", .data = &soc_data_tegra210 },
1383 	{ .compatible = "nvidia,tegra124-sdhci", .data = &soc_data_tegra124 },
1384 	{ .compatible = "nvidia,tegra114-sdhci", .data = &soc_data_tegra114 },
1385 	{ .compatible = "nvidia,tegra30-sdhci", .data = &soc_data_tegra30 },
1386 	{ .compatible = "nvidia,tegra20-sdhci", .data = &soc_data_tegra20 },
1387 	{}
1388 };
1389 MODULE_DEVICE_TABLE(of, sdhci_tegra_dt_match);
1390 
1391 static int sdhci_tegra_add_host(struct sdhci_host *host)
1392 {
1393 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1394 	struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
1395 	struct cqhci_host *cq_host;
1396 	bool dma64;
1397 	int ret;
1398 
1399 	if (!tegra_host->enable_hwcq)
1400 		return sdhci_add_host(host);
1401 
1402 	sdhci_enable_v4_mode(host);
1403 
1404 	ret = sdhci_setup_host(host);
1405 	if (ret)
1406 		return ret;
1407 
1408 	host->mmc->caps2 |= MMC_CAP2_CQE | MMC_CAP2_CQE_DCMD;
1409 
1410 	cq_host = devm_kzalloc(host->mmc->parent,
1411 				sizeof(*cq_host), GFP_KERNEL);
1412 	if (!cq_host) {
1413 		ret = -ENOMEM;
1414 		goto cleanup;
1415 	}
1416 
1417 	cq_host->mmio = host->ioaddr + SDHCI_TEGRA_CQE_BASE_ADDR;
1418 	cq_host->ops = &sdhci_tegra_cqhci_ops;
1419 
1420 	dma64 = host->flags & SDHCI_USE_64_BIT_DMA;
1421 	if (dma64)
1422 		cq_host->caps |= CQHCI_TASK_DESC_SZ_128;
1423 
1424 	ret = cqhci_init(cq_host, host->mmc, dma64);
1425 	if (ret)
1426 		goto cleanup;
1427 
1428 	ret = __sdhci_add_host(host);
1429 	if (ret)
1430 		goto cleanup;
1431 
1432 	return 0;
1433 
1434 cleanup:
1435 	sdhci_cleanup_host(host);
1436 	return ret;
1437 }
1438 
1439 static int sdhci_tegra_probe(struct platform_device *pdev)
1440 {
1441 	const struct of_device_id *match;
1442 	const struct sdhci_tegra_soc_data *soc_data;
1443 	struct sdhci_host *host;
1444 	struct sdhci_pltfm_host *pltfm_host;
1445 	struct sdhci_tegra *tegra_host;
1446 	struct clk *clk;
1447 	int rc;
1448 
1449 	match = of_match_device(sdhci_tegra_dt_match, &pdev->dev);
1450 	if (!match)
1451 		return -EINVAL;
1452 	soc_data = match->data;
1453 
1454 	host = sdhci_pltfm_init(pdev, soc_data->pdata, sizeof(*tegra_host));
1455 	if (IS_ERR(host))
1456 		return PTR_ERR(host);
1457 	pltfm_host = sdhci_priv(host);
1458 
1459 	tegra_host = sdhci_pltfm_priv(pltfm_host);
1460 	tegra_host->ddr_signaling = false;
1461 	tegra_host->pad_calib_required = false;
1462 	tegra_host->pad_control_available = false;
1463 	tegra_host->soc_data = soc_data;
1464 
1465 	if (soc_data->nvquirks & NVQUIRK_NEEDS_PAD_CONTROL) {
1466 		rc = tegra_sdhci_init_pinctrl_info(&pdev->dev, tegra_host);
1467 		if (rc == 0)
1468 			host->mmc_host_ops.start_signal_voltage_switch =
1469 				sdhci_tegra_start_signal_voltage_switch;
1470 	}
1471 
1472 	/* Hook to periodically rerun pad calibration */
1473 	if (soc_data->nvquirks & NVQUIRK_HAS_PADCALIB)
1474 		host->mmc_host_ops.request = tegra_sdhci_request;
1475 
1476 	host->mmc_host_ops.hs400_enhanced_strobe =
1477 			tegra_sdhci_hs400_enhanced_strobe;
1478 
1479 	if (!host->ops->platform_execute_tuning)
1480 		host->mmc_host_ops.execute_tuning =
1481 				tegra_sdhci_execute_hw_tuning;
1482 
1483 	rc = mmc_of_parse(host->mmc);
1484 	if (rc)
1485 		goto err_parse_dt;
1486 
1487 	if (tegra_host->soc_data->nvquirks & NVQUIRK_ENABLE_DDR50)
1488 		host->mmc->caps |= MMC_CAP_1_8V_DDR;
1489 
1490 	tegra_sdhci_parse_dt(host);
1491 
1492 	tegra_host->power_gpio = devm_gpiod_get_optional(&pdev->dev, "power",
1493 							 GPIOD_OUT_HIGH);
1494 	if (IS_ERR(tegra_host->power_gpio)) {
1495 		rc = PTR_ERR(tegra_host->power_gpio);
1496 		goto err_power_req;
1497 	}
1498 
1499 	clk = devm_clk_get(mmc_dev(host->mmc), NULL);
1500 	if (IS_ERR(clk)) {
1501 		dev_err(mmc_dev(host->mmc), "clk err\n");
1502 		rc = PTR_ERR(clk);
1503 		goto err_clk_get;
1504 	}
1505 	clk_prepare_enable(clk);
1506 	pltfm_host->clk = clk;
1507 
1508 	tegra_host->rst = devm_reset_control_get_exclusive(&pdev->dev,
1509 							   "sdhci");
1510 	if (IS_ERR(tegra_host->rst)) {
1511 		rc = PTR_ERR(tegra_host->rst);
1512 		dev_err(&pdev->dev, "failed to get reset control: %d\n", rc);
1513 		goto err_rst_get;
1514 	}
1515 
1516 	rc = reset_control_assert(tegra_host->rst);
1517 	if (rc)
1518 		goto err_rst_get;
1519 
1520 	usleep_range(2000, 4000);
1521 
1522 	rc = reset_control_deassert(tegra_host->rst);
1523 	if (rc)
1524 		goto err_rst_get;
1525 
1526 	usleep_range(2000, 4000);
1527 
1528 	rc = sdhci_tegra_add_host(host);
1529 	if (rc)
1530 		goto err_add_host;
1531 
1532 	return 0;
1533 
1534 err_add_host:
1535 	reset_control_assert(tegra_host->rst);
1536 err_rst_get:
1537 	clk_disable_unprepare(pltfm_host->clk);
1538 err_clk_get:
1539 err_power_req:
1540 err_parse_dt:
1541 	sdhci_pltfm_free(pdev);
1542 	return rc;
1543 }
1544 
1545 static int sdhci_tegra_remove(struct platform_device *pdev)
1546 {
1547 	struct sdhci_host *host = platform_get_drvdata(pdev);
1548 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1549 	struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
1550 
1551 	sdhci_remove_host(host, 0);
1552 
1553 	reset_control_assert(tegra_host->rst);
1554 	usleep_range(2000, 4000);
1555 	clk_disable_unprepare(pltfm_host->clk);
1556 
1557 	sdhci_pltfm_free(pdev);
1558 
1559 	return 0;
1560 }
1561 
1562 static struct platform_driver sdhci_tegra_driver = {
1563 	.driver		= {
1564 		.name	= "sdhci-tegra",
1565 		.of_match_table = sdhci_tegra_dt_match,
1566 		.pm	= &sdhci_pltfm_pmops,
1567 	},
1568 	.probe		= sdhci_tegra_probe,
1569 	.remove		= sdhci_tegra_remove,
1570 };
1571 
1572 module_platform_driver(sdhci_tegra_driver);
1573 
1574 MODULE_DESCRIPTION("SDHCI driver for Tegra");
1575 MODULE_AUTHOR("Google, Inc.");
1576 MODULE_LICENSE("GPL v2");
1577