2 * Copyright (C) 2010 Google, Inc.
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.
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.
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>
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>
35 #include "sdhci-pltfm.h"
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)
48 #define SDHCI_TEGRA_VENDOR_SYS_SW_CTRL 0x104
49 #define SDHCI_TEGRA_SYS_SW_CTRL_ENHANCED_STROBE BIT(31)
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
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
61 #define SDHCI_TEGRA_VENDOR_DLLCAL_CFG 0x1b0
62 #define SDHCI_TEGRA_DLLCAL_CALIBRATE BIT(31)
64 #define SDHCI_TEGRA_VENDOR_DLLCAL_STA 0x1bc
65 #define SDHCI_TEGRA_DLLCAL_STA_ACTIVE BIT(31)
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
77 #define SDHCI_VNDR_TUN_CTRL0_TUN_WORD_SEL_MASK 0x7
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
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
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
97 #define SDHCI_TEGRA_AUTO_CAL_STATUS 0x1ec
98 #define SDHCI_TEGRA_AUTO_CAL_ACTIVE BIT(31)
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)
111 /* SDMMC CQE Base Address for Tegra Host Ver 4.1 and Higher */
112 #define SDHCI_TEGRA_CQE_BASE_ADDR 0xF000
114 struct sdhci_tegra_soc_data {
115 const struct sdhci_pltfm_data *pdata;
121 /* Magic pull up and pull down pad calibration offsets */
122 struct sdhci_tegra_autocal_offsets {
125 u32 pull_up_3v3_timeout;
126 u32 pull_down_3v3_timeout;
129 u32 pull_up_1v8_timeout;
130 u32 pull_down_1v8_timeout;
132 u32 pull_down_sdr104;
138 const struct sdhci_tegra_soc_data *soc_data;
139 struct gpio_desc *power_gpio;
141 bool pad_calib_required;
142 bool pad_control_available;
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;
151 struct sdhci_tegra_autocal_offsets autocal_offsets;
158 unsigned long curr_clk_rate;
162 static u16 tegra_sdhci_readw(struct sdhci_host *host, int reg)
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;
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;
174 return readw(host->ioaddr + reg);
177 static void tegra_sdhci_writew(struct sdhci_host *host, u16 val, int reg)
179 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
182 case SDHCI_TRANSFER_MODE:
184 * Postpone this write, we must do it together with a
185 * command write that is down below.
187 pltfm_host->xfer_mode_shadow = val;
190 writel((val << 16) | pltfm_host->xfer_mode_shadow,
191 host->ioaddr + SDHCI_TRANSFER_MODE);
195 writew(val, host->ioaddr + reg);
198 static void tegra_sdhci_writel(struct sdhci_host *host, u32 val, int reg)
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;
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.
208 if (unlikely(reg == SDHCI_SIGNAL_ENABLE))
209 val &= ~(SDHCI_INT_TIMEOUT|SDHCI_INT_CRC);
211 writel(val, host->ioaddr + reg);
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)
221 writeb(gap_ctrl, host->ioaddr + SDHCI_BLOCK_GAP_CONTROL);
225 static bool tegra_sdhci_configure_card_clk(struct sdhci_host *host, bool enable)
230 reg = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
231 status = !!(reg & SDHCI_CLOCK_CARD_EN);
233 if (status == enable)
237 reg |= SDHCI_CLOCK_CARD_EN;
239 reg &= ~SDHCI_CLOCK_CARD_EN;
241 sdhci_writew(host, reg, SDHCI_CLOCK_CONTROL);
246 static void tegra210_sdhci_writew(struct sdhci_host *host, u16 val, int reg)
248 bool is_tuning_cmd = 0;
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;
259 clk_enabled = tegra_sdhci_configure_card_clk(host, 0);
261 writew(val, host->ioaddr + reg);
265 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
266 tegra_sdhci_configure_card_clk(host, clk_enabled);
270 static bool tegra_sdhci_is_pad_and_regulator_valid(struct sdhci_host *host)
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;
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.
284 if (!(tegra_host->soc_data->nvquirks & NVQUIRK_NEEDS_PAD_CONTROL))
287 if (IS_ERR(host->mmc->supply.vqmmc))
290 has_1v8 = regulator_is_supported_voltage(host->mmc->supply.vqmmc,
293 has_3v3 = regulator_is_supported_voltage(host->mmc->supply.vqmmc,
296 if (has_1v8 == 1 && has_3v3 == 1)
297 return tegra_host->pad_control_available;
299 /* Fixed voltage, no pad control required. */
303 static void tegra_sdhci_set_tap(struct sdhci_host *host, unsigned int tap)
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;
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.
317 if (soc_data->nvquirks & NVQUIRK_DIS_CARD_CLK_CONFIG_TAP)
318 card_clk_enabled = tegra_sdhci_configure_card_clk(host, false);
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);
325 if (soc_data->nvquirks & NVQUIRK_DIS_CARD_CLK_CONFIG_TAP &&
328 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
329 tegra_sdhci_configure_card_clk(host, card_clk_enabled);
333 static void tegra_sdhci_hs400_enhanced_strobe(struct mmc_host *mmc,
336 struct sdhci_host *host = mmc_priv(mmc);
339 val = sdhci_readl(host, SDHCI_TEGRA_VENDOR_SYS_SW_CTRL);
341 if (ios->enhanced_strobe)
342 val |= SDHCI_TEGRA_SYS_SW_CTRL_ENHANCED_STROBE;
344 val &= ~SDHCI_TEGRA_SYS_SW_CTRL_ENHANCED_STROBE;
346 sdhci_writel(host, val, SDHCI_TEGRA_VENDOR_SYS_SW_CTRL);
350 static void tegra_sdhci_reset(struct sdhci_host *host, u8 mask)
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;
357 sdhci_reset(host, mask);
359 if (!(mask & SDHCI_RESET_ALL))
362 tegra_sdhci_set_tap(host, tegra_host->default_tap);
364 misc_ctrl = sdhci_readl(host, SDHCI_TEGRA_VENDOR_MISC_CTRL);
365 clk_ctrl = sdhci_readl(host, SDHCI_TEGRA_VENDOR_CLOCK_CTRL);
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);
372 clk_ctrl &= ~(SDHCI_CLOCK_CTRL_TRIM_MASK |
373 SDHCI_CLOCK_CTRL_SPI_MODE_CLKEN_OVERRIDE);
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;
390 clk_ctrl |= tegra_host->default_trim << SDHCI_CLOCK_CTRL_TRIM_SHIFT;
392 sdhci_writel(host, misc_ctrl, SDHCI_TEGRA_VENDOR_MISC_CTRL);
393 sdhci_writel(host, clk_ctrl, SDHCI_TEGRA_VENDOR_CLOCK_CTRL);
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);
401 tegra_host->pad_calib_required = true;
404 tegra_host->ddr_signaling = false;
407 static void tegra_sdhci_configure_cal_pad(struct sdhci_host *host, bool enable)
412 * Enable or disable the additional I/O pad used by the drive strength
413 * calibration process.
415 val = sdhci_readl(host, SDHCI_TEGRA_SDMEM_COMP_PADCTRL);
418 val |= SDHCI_TEGRA_SDMEM_COMP_PADCTRL_E_INPUT_E_PWRD;
420 val &= ~SDHCI_TEGRA_SDMEM_COMP_PADCTRL_E_INPUT_E_PWRD;
422 sdhci_writel(host, val, SDHCI_TEGRA_SDMEM_COMP_PADCTRL);
428 static void tegra_sdhci_set_pad_autocal_offset(struct sdhci_host *host,
433 reg = sdhci_readl(host, SDHCI_TEGRA_AUTO_CAL_CONFIG);
434 reg &= ~SDHCI_AUTO_CAL_PDPU_OFFSET_MASK;
436 sdhci_writel(host, reg, SDHCI_TEGRA_AUTO_CAL_CONFIG);
439 static int tegra_sdhci_set_padctrl(struct sdhci_host *host, int voltage,
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;
448 u8 drvup = 0, drvdn = 0;
451 if (!state_drvupdn) {
452 /* PADS Drive Strength */
453 if (voltage == MMC_SIGNAL_VOLTAGE_180) {
454 if (tegra_host->pinctrl_state_1v8_drv) {
456 tegra_host->pinctrl_state_1v8_drv;
458 drvup = offsets->pull_up_1v8_timeout;
459 drvdn = offsets->pull_down_1v8_timeout;
462 if (tegra_host->pinctrl_state_3v3_drv) {
464 tegra_host->pinctrl_state_3v3_drv;
466 drvup = offsets->pull_up_3v3_timeout;
467 drvdn = offsets->pull_down_3v3_timeout;
471 if (pinctrl_drvupdn != NULL) {
472 ret = pinctrl_select_state(tegra_host->pinctrl_sdmmc,
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);
487 /* Dual Voltage PADS Voltage selection */
488 if (!tegra_host->pad_control_available)
491 if (voltage == MMC_SIGNAL_VOLTAGE_180) {
492 ret = pinctrl_select_state(tegra_host->pinctrl_sdmmc,
493 tegra_host->pinctrl_state_1v8);
495 dev_err(mmc_dev(host->mmc),
496 "setting 1.8V failed, ret: %d\n", ret);
498 ret = pinctrl_select_state(tegra_host->pinctrl_sdmmc,
499 tegra_host->pinctrl_state_3v3);
501 dev_err(mmc_dev(host->mmc),
502 "setting 3.3V failed, ret: %d\n", ret);
509 static void tegra_sdhci_pad_autocalib(struct sdhci_host *host)
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;
521 switch (ios->timing) {
522 case MMC_TIMING_UHS_SDR104:
523 pdpu = offsets.pull_down_sdr104 << 8 | offsets.pull_up_sdr104;
525 case MMC_TIMING_MMC_HS400:
526 pdpu = offsets.pull_down_hs400 << 8 | offsets.pull_up_hs400;
529 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)
530 pdpu = offsets.pull_down_1v8 << 8 | offsets.pull_up_1v8;
532 pdpu = offsets.pull_down_3v3 << 8 | offsets.pull_up_3v3;
535 /* Set initial offset before auto-calibration */
536 tegra_sdhci_set_pad_autocal_offset(host, pdpu);
538 card_clk_enabled = tegra_sdhci_configure_card_clk(host, false);
540 tegra_sdhci_configure_cal_pad(host, true);
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);
548 ret = readl_poll_timeout(host->ioaddr + SDHCI_TEGRA_AUTO_CAL_STATUS,
549 reg, !(reg & SDHCI_TEGRA_AUTO_CAL_ACTIVE),
552 tegra_sdhci_configure_cal_pad(host, false);
554 tegra_sdhci_configure_card_clk(host, card_clk_enabled);
557 dev_err(mmc_dev(host->mmc), "Pad autocal timed out\n");
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);
564 ret = tegra_sdhci_set_padctrl(host, ios->signal_voltage, false);
566 dev_err(mmc_dev(host->mmc),
567 "Setting drive strengths failed: %d\n", ret);
571 static void tegra_sdhci_parse_pad_autocal_dt(struct sdhci_host *host)
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;
579 err = device_property_read_u32(host->mmc->parent,
580 "nvidia,pad-autocal-pull-up-offset-3v3",
581 &autocal->pull_up_3v3);
583 autocal->pull_up_3v3 = 0;
585 err = device_property_read_u32(host->mmc->parent,
586 "nvidia,pad-autocal-pull-down-offset-3v3",
587 &autocal->pull_down_3v3);
589 autocal->pull_down_3v3 = 0;
591 err = device_property_read_u32(host->mmc->parent,
592 "nvidia,pad-autocal-pull-up-offset-1v8",
593 &autocal->pull_up_1v8);
595 autocal->pull_up_1v8 = 0;
597 err = device_property_read_u32(host->mmc->parent,
598 "nvidia,pad-autocal-pull-down-offset-1v8",
599 &autocal->pull_down_1v8);
601 autocal->pull_down_1v8 = 0;
603 err = device_property_read_u32(host->mmc->parent,
604 "nvidia,pad-autocal-pull-up-offset-3v3-timeout",
605 &autocal->pull_up_3v3_timeout);
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;
614 err = device_property_read_u32(host->mmc->parent,
615 "nvidia,pad-autocal-pull-down-offset-3v3-timeout",
616 &autocal->pull_down_3v3_timeout);
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;
625 err = device_property_read_u32(host->mmc->parent,
626 "nvidia,pad-autocal-pull-up-offset-1v8-timeout",
627 &autocal->pull_up_1v8_timeout);
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;
636 err = device_property_read_u32(host->mmc->parent,
637 "nvidia,pad-autocal-pull-down-offset-1v8-timeout",
638 &autocal->pull_down_1v8_timeout);
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;
647 err = device_property_read_u32(host->mmc->parent,
648 "nvidia,pad-autocal-pull-up-offset-sdr104",
649 &autocal->pull_up_sdr104);
651 autocal->pull_up_sdr104 = autocal->pull_up_1v8;
653 err = device_property_read_u32(host->mmc->parent,
654 "nvidia,pad-autocal-pull-down-offset-sdr104",
655 &autocal->pull_down_sdr104);
657 autocal->pull_down_sdr104 = autocal->pull_down_1v8;
659 err = device_property_read_u32(host->mmc->parent,
660 "nvidia,pad-autocal-pull-up-offset-hs400",
661 &autocal->pull_up_hs400);
663 autocal->pull_up_hs400 = autocal->pull_up_1v8;
665 err = device_property_read_u32(host->mmc->parent,
666 "nvidia,pad-autocal-pull-down-offset-hs400",
667 &autocal->pull_down_hs400);
669 autocal->pull_down_hs400 = autocal->pull_down_1v8;
672 static void tegra_sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
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);
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();
685 sdhci_request(mmc, mrq);
688 static void tegra_sdhci_parse_tap_and_trim(struct sdhci_host *host)
690 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
691 struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
694 err = device_property_read_u32(host->mmc->parent, "nvidia,default-tap",
695 &tegra_host->default_tap);
697 tegra_host->default_tap = 0;
699 err = device_property_read_u32(host->mmc->parent, "nvidia,default-trim",
700 &tegra_host->default_trim);
702 tegra_host->default_trim = 0;
704 err = device_property_read_u32(host->mmc->parent, "nvidia,dqs-trim",
705 &tegra_host->dqs_trim);
707 tegra_host->dqs_trim = 0x11;
710 static void tegra_sdhci_parse_dt(struct sdhci_host *host)
712 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
713 struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
715 if (device_property_read_bool(host->mmc->parent, "supports-cqe"))
716 tegra_host->enable_hwcq = true;
718 tegra_host->enable_hwcq = false;
720 tegra_sdhci_parse_pad_autocal_dt(host);
721 tegra_sdhci_parse_tap_and_trim(host);
724 static void tegra_sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
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;
731 return sdhci_set_clock(host, clock);
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.
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.
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;
751 host->max_clk = clk_get_rate(pltfm_host->clk);
753 sdhci_set_clock(host, clock);
755 if (tegra_host->pad_calib_required) {
756 tegra_sdhci_pad_autocalib(host);
757 tegra_host->pad_calib_required = false;
761 static unsigned int tegra_sdhci_get_max_clock(struct sdhci_host *host)
763 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
765 return clk_round_rate(pltfm_host->clk, UINT_MAX);
768 static void tegra_sdhci_set_dqs_trim(struct sdhci_host *host, u8 trim)
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);
778 static void tegra_sdhci_hs400_dll_cal(struct sdhci_host *host)
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);
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),
792 dev_err(mmc_dev(host->mmc),
793 "HS400 delay line calibration timed out\n");
796 static void tegra_sdhci_tap_correction(struct sdhci_host *host, u8 thd_up,
797 u8 thd_low, u8 fixed_tap)
799 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
800 struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
802 u8 word, bit, edge1, tap, window;
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;
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;
816 * Read auto-tuned results and extract good valid passing window by
817 * filtering out un-wanted bubble/partial/merged windows.
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;
823 sdhci_writel(host, val, SDHCI_VNDR_TUN_CTRL0_0);
824 tun_status = sdhci_readl(host, SDHCI_TEGRA_VNDR_TUN_STATUS0);
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) {
832 first_fail_tap = tap;
836 } else if (tap_result && start_fail && !start_pass) {
837 start_pass_tap = tap;
840 first_pass_tap = tap;
844 } else if (!tap_result && start_fail && start_pass &&
846 end_pass_tap = tap - 1;
848 } else if (tap_result && start_pass && start_fail &&
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;
856 /* set tap at middle of valid window */
857 tap = start_pass_tap + window / 2;
858 tegra_host->tuned_tap_delay = tap;
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;
875 tegra_host->tuned_tap_delay = edge1 + fixed_tap;
879 static void tegra_sdhci_post_tuning(struct sdhci_host *host)
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;
888 u32 clk_rate_mhz, period_ps, bestcase, worstcase;
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;
902 * Upper and Lower bound thresholds used to detect merged and
905 thdupper = (2 * worstcase + bestcase) / 2;
906 thdlower = worstcase / 4;
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
911 avg_tap_dly = (period_ps * 2) / (min_tap_dly + max_tap_dly);
912 fixed_tap = avg_tap_dly / 2;
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;
921 * partial window includes edges of the tuning range.
922 * merged window includes more taps so window width is higher
923 * than upper threshold.
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,
934 tegra_sdhci_set_tap(host, tegra_host->tuned_tap_delay);
937 static int tegra_sdhci_execute_hw_tuning(struct mmc_host *mmc, u32 opcode)
939 struct sdhci_host *host = mmc_priv(mmc);
942 err = sdhci_execute_tuning(mmc, opcode);
943 if (!err && !host->tuning_err)
944 tegra_sdhci_post_tuning(host);
949 static void tegra_sdhci_set_uhs_signaling(struct sdhci_host *host,
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;
960 tegra_host->ddr_signaling = false;
962 case MMC_TIMING_UHS_SDR50:
964 case MMC_TIMING_UHS_SDR104:
965 case MMC_TIMING_MMC_HS200:
966 /* Don't set default tap on tunable modes. */
969 case MMC_TIMING_MMC_HS400:
971 do_hs400_dll_cal = true;
974 case MMC_TIMING_MMC_DDR52:
975 case MMC_TIMING_UHS_DDR50:
976 tegra_host->ddr_signaling = true;
977 set_default_tap = true;
980 set_default_tap = true;
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);
994 host->tuning_loop_count = (iter == TRIES_128) ? 128 : 256;
996 sdhci_set_uhs_signaling(host, timing);
998 tegra_sdhci_pad_autocalib(host);
1000 if (tegra_host->tuned_tap_delay && !set_default_tap)
1001 tegra_sdhci_set_tap(host, tegra_host->tuned_tap_delay);
1003 tegra_sdhci_set_tap(host, tegra_host->default_tap);
1006 tegra_sdhci_set_dqs_trim(host, tegra_host->dqs_trim);
1008 if (do_hs400_dll_cal)
1009 tegra_sdhci_hs400_dll_cal(host);
1012 static int tegra_sdhci_execute_tuning(struct sdhci_host *host, u32 opcode)
1014 unsigned int min, max;
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.
1023 tegra_sdhci_set_tap(host, min);
1024 if (!mmc_send_tuning(host->mmc, opcode, NULL))
1029 /* Find the maximum tap value that still passes. */
1032 tegra_sdhci_set_tap(host, max);
1033 if (mmc_send_tuning(host->mmc, opcode, NULL)) {
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));
1043 return mmc_send_tuning(host->mmc, opcode, NULL);
1046 static int sdhci_tegra_start_signal_voltage_switch(struct mmc_host *mmc,
1047 struct mmc_ios *ios)
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);
1054 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1055 ret = tegra_sdhci_set_padctrl(host, ios->signal_voltage, true);
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);
1063 ret = tegra_sdhci_set_padctrl(host, ios->signal_voltage, true);
1066 if (tegra_host->pad_calib_required)
1067 tegra_sdhci_pad_autocalib(host);
1072 static int tegra_sdhci_init_pinctrl_info(struct device *dev,
1073 struct sdhci_tegra *tegra_host)
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));
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;
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;
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));
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));
1112 tegra_host->pad_control_available = true;
1117 static void tegra_sdhci_voltage_switch(struct sdhci_host *host)
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;
1123 if (soc_data->nvquirks & NVQUIRK_HAS_PADCALIB)
1124 tegra_host->pad_calib_required = true;
1127 static void tegra_cqhci_writel(struct cqhci_host *cq_host, u32 val, int reg)
1129 struct mmc_host *mmc = cq_host->mmc;
1135 * During CQE resume/unhalt, CQHCI driver unhalts CQE prior to
1136 * cqhci_host_ops enable where SDHCI DMA and BLOCK_SIZE registers need
1137 * to be re-configured.
1138 * Tegra CQHCI/SDHCI prevents write access to block size register when
1139 * CQE is unhalted. So handling CQE resume sequence here to configure
1140 * SDHCI block registers prior to exiting CQE halt state.
1142 if (reg == CQHCI_CTL && !(val & CQHCI_HALT) &&
1143 cqhci_readl(cq_host, CQHCI_CTL) & CQHCI_HALT) {
1144 sdhci_cqe_enable(mmc);
1145 writel(val, cq_host->mmio + reg);
1146 timeout = ktime_add_us(ktime_get(), 50);
1148 timed_out = ktime_compare(ktime_get(), timeout) > 0;
1149 ctrl = cqhci_readl(cq_host, CQHCI_CTL);
1150 if (!(ctrl & CQHCI_HALT) || timed_out)
1154 * CQE usually resumes very quick, but incase if Tegra CQE
1155 * doesn't resume retry unhalt.
1158 writel(val, cq_host->mmio + reg);
1160 writel(val, cq_host->mmio + reg);
1164 static void sdhci_tegra_update_dcmd_desc(struct mmc_host *mmc,
1165 struct mmc_request *mrq, u64 *data)
1167 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(mmc_priv(mmc));
1168 struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
1169 const struct sdhci_tegra_soc_data *soc_data = tegra_host->soc_data;
1171 if (soc_data->nvquirks & NVQUIRK_CQHCI_DCMD_R1B_CMD_TIMING &&
1172 mrq->cmd->flags & MMC_RSP_R1B)
1173 *data |= CQHCI_CMD_TIMING(1);
1176 static void sdhci_tegra_cqe_enable(struct mmc_host *mmc)
1178 struct cqhci_host *cq_host = mmc->cqe_private;
1182 * Tegra CQHCI/SDMMC design prevents write access to sdhci block size
1183 * register when CQE is enabled and unhalted.
1184 * CQHCI driver enables CQE prior to activation, so disable CQE before
1185 * programming block size in sdhci controller and enable it back.
1187 if (!cq_host->activated) {
1188 val = cqhci_readl(cq_host, CQHCI_CFG);
1189 if (val & CQHCI_ENABLE)
1190 cqhci_writel(cq_host, (val & ~CQHCI_ENABLE),
1192 sdhci_cqe_enable(mmc);
1193 if (val & CQHCI_ENABLE)
1194 cqhci_writel(cq_host, val, CQHCI_CFG);
1198 * CMD CRC errors are seen sometimes with some eMMC devices when status
1199 * command is sent during transfer of last data block which is the
1200 * default case as send status command block counter (CBC) is 1.
1201 * Recommended fix to set CBC to 0 allowing send status command only
1202 * when data lines are idle.
1204 val = cqhci_readl(cq_host, CQHCI_SSC1);
1205 val &= ~CQHCI_SSC1_CBC_MASK;
1206 cqhci_writel(cq_host, val, CQHCI_SSC1);
1209 static void sdhci_tegra_dumpregs(struct mmc_host *mmc)
1211 sdhci_dumpregs(mmc_priv(mmc));
1214 static u32 sdhci_tegra_cqhci_irq(struct sdhci_host *host, u32 intmask)
1219 if (!sdhci_cqe_irq(host, intmask, &cmd_error, &data_error))
1222 cqhci_irq(host->mmc, intmask, cmd_error, data_error);
1227 static const struct cqhci_host_ops sdhci_tegra_cqhci_ops = {
1228 .write_l = tegra_cqhci_writel,
1229 .enable = sdhci_tegra_cqe_enable,
1230 .disable = sdhci_cqe_disable,
1231 .dumpregs = sdhci_tegra_dumpregs,
1232 .update_dcmd_desc = sdhci_tegra_update_dcmd_desc,
1235 static const struct sdhci_ops tegra_sdhci_ops = {
1236 .read_w = tegra_sdhci_readw,
1237 .write_l = tegra_sdhci_writel,
1238 .set_clock = tegra_sdhci_set_clock,
1239 .set_bus_width = sdhci_set_bus_width,
1240 .reset = tegra_sdhci_reset,
1241 .platform_execute_tuning = tegra_sdhci_execute_tuning,
1242 .set_uhs_signaling = tegra_sdhci_set_uhs_signaling,
1243 .voltage_switch = tegra_sdhci_voltage_switch,
1244 .get_max_clock = tegra_sdhci_get_max_clock,
1247 static const struct sdhci_pltfm_data sdhci_tegra20_pdata = {
1248 .quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
1249 SDHCI_QUIRK_SINGLE_POWER_WRITE |
1250 SDHCI_QUIRK_NO_HISPD_BIT |
1251 SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC |
1252 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
1253 .ops = &tegra_sdhci_ops,
1256 static const struct sdhci_tegra_soc_data soc_data_tegra20 = {
1257 .pdata = &sdhci_tegra20_pdata,
1258 .nvquirks = NVQUIRK_FORCE_SDHCI_SPEC_200 |
1259 NVQUIRK_ENABLE_BLOCK_GAP_DET,
1262 static const struct sdhci_pltfm_data sdhci_tegra30_pdata = {
1263 .quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
1264 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
1265 SDHCI_QUIRK_SINGLE_POWER_WRITE |
1266 SDHCI_QUIRK_NO_HISPD_BIT |
1267 SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC |
1268 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
1269 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1270 SDHCI_QUIRK2_BROKEN_HS200 |
1272 * Auto-CMD23 leads to "Got command interrupt 0x00010000 even
1273 * though no command operation was in progress."
1275 * The exact reason is unknown, as the same hardware seems
1276 * to support Auto CMD23 on a downstream 3.1 kernel.
1278 SDHCI_QUIRK2_ACMD23_BROKEN,
1279 .ops = &tegra_sdhci_ops,
1282 static const struct sdhci_tegra_soc_data soc_data_tegra30 = {
1283 .pdata = &sdhci_tegra30_pdata,
1284 .nvquirks = NVQUIRK_ENABLE_SDHCI_SPEC_300 |
1285 NVQUIRK_ENABLE_SDR50 |
1286 NVQUIRK_ENABLE_SDR104 |
1287 NVQUIRK_HAS_PADCALIB,
1290 static const struct sdhci_ops tegra114_sdhci_ops = {
1291 .read_w = tegra_sdhci_readw,
1292 .write_w = tegra_sdhci_writew,
1293 .write_l = tegra_sdhci_writel,
1294 .set_clock = tegra_sdhci_set_clock,
1295 .set_bus_width = sdhci_set_bus_width,
1296 .reset = tegra_sdhci_reset,
1297 .platform_execute_tuning = tegra_sdhci_execute_tuning,
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,
1303 static const struct sdhci_pltfm_data sdhci_tegra114_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 = &tegra114_sdhci_ops,
1314 static const struct sdhci_tegra_soc_data soc_data_tegra114 = {
1315 .pdata = &sdhci_tegra114_pdata,
1318 static const struct sdhci_pltfm_data sdhci_tegra124_pdata = {
1319 .quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
1320 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
1321 SDHCI_QUIRK_SINGLE_POWER_WRITE |
1322 SDHCI_QUIRK_NO_HISPD_BIT |
1323 SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC |
1324 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
1325 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1327 * The TRM states that the SD/MMC controller found on
1328 * Tegra124 can address 34 bits (the maximum supported by
1329 * the Tegra memory controller), but tests show that DMA
1330 * to or from above 4 GiB doesn't work. This is possibly
1331 * caused by missing programming, though it's not obvious
1332 * what sequence is required. Mark 64-bit DMA broken for
1333 * now to fix this for existing users (e.g. Nyan boards).
1335 SDHCI_QUIRK2_BROKEN_64_BIT_DMA,
1336 .ops = &tegra114_sdhci_ops,
1339 static const struct sdhci_tegra_soc_data soc_data_tegra124 = {
1340 .pdata = &sdhci_tegra124_pdata,
1343 static const struct sdhci_ops tegra210_sdhci_ops = {
1344 .read_w = tegra_sdhci_readw,
1345 .write_w = tegra210_sdhci_writew,
1346 .write_l = tegra_sdhci_writel,
1347 .set_clock = tegra_sdhci_set_clock,
1348 .set_bus_width = sdhci_set_bus_width,
1349 .reset = tegra_sdhci_reset,
1350 .set_uhs_signaling = tegra_sdhci_set_uhs_signaling,
1351 .voltage_switch = tegra_sdhci_voltage_switch,
1352 .get_max_clock = tegra_sdhci_get_max_clock,
1355 static const struct sdhci_pltfm_data sdhci_tegra210_pdata = {
1356 .quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
1357 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
1358 SDHCI_QUIRK_SINGLE_POWER_WRITE |
1359 SDHCI_QUIRK_NO_HISPD_BIT |
1360 SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC |
1361 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
1362 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
1363 .ops = &tegra210_sdhci_ops,
1366 static const struct sdhci_tegra_soc_data soc_data_tegra210 = {
1367 .pdata = &sdhci_tegra210_pdata,
1368 .nvquirks = NVQUIRK_NEEDS_PAD_CONTROL |
1369 NVQUIRK_HAS_PADCALIB |
1370 NVQUIRK_DIS_CARD_CLK_CONFIG_TAP |
1371 NVQUIRK_ENABLE_SDR50 |
1372 NVQUIRK_ENABLE_SDR104,
1373 .min_tap_delay = 106,
1374 .max_tap_delay = 185,
1377 static const struct sdhci_ops tegra186_sdhci_ops = {
1378 .read_w = tegra_sdhci_readw,
1379 .write_l = tegra_sdhci_writel,
1380 .set_clock = tegra_sdhci_set_clock,
1381 .set_bus_width = sdhci_set_bus_width,
1382 .reset = tegra_sdhci_reset,
1383 .set_uhs_signaling = tegra_sdhci_set_uhs_signaling,
1384 .voltage_switch = tegra_sdhci_voltage_switch,
1385 .get_max_clock = tegra_sdhci_get_max_clock,
1386 .irq = sdhci_tegra_cqhci_irq,
1389 static const struct sdhci_pltfm_data sdhci_tegra186_pdata = {
1390 .quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
1391 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
1392 SDHCI_QUIRK_SINGLE_POWER_WRITE |
1393 SDHCI_QUIRK_NO_HISPD_BIT |
1394 SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC |
1395 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
1396 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1397 /* SDHCI controllers on Tegra186 support 40-bit addressing.
1398 * IOVA addresses are 48-bit wide on Tegra186.
1399 * With 64-bit dma mask used for SDHCI, accesses can
1400 * be broken. Disable 64-bit dma, which would fall back
1401 * to 32-bit dma mask. Ideally 40-bit dma mask would work,
1402 * But it is not supported as of now.
1404 SDHCI_QUIRK2_BROKEN_64_BIT_DMA,
1405 .ops = &tegra186_sdhci_ops,
1408 static const struct sdhci_tegra_soc_data soc_data_tegra186 = {
1409 .pdata = &sdhci_tegra186_pdata,
1410 .nvquirks = NVQUIRK_NEEDS_PAD_CONTROL |
1411 NVQUIRK_HAS_PADCALIB |
1412 NVQUIRK_DIS_CARD_CLK_CONFIG_TAP |
1413 NVQUIRK_ENABLE_SDR50 |
1414 NVQUIRK_ENABLE_SDR104 |
1415 NVQUIRK_CQHCI_DCMD_R1B_CMD_TIMING,
1416 .min_tap_delay = 84,
1417 .max_tap_delay = 136,
1420 static const struct sdhci_tegra_soc_data soc_data_tegra194 = {
1421 .pdata = &sdhci_tegra186_pdata,
1422 .nvquirks = NVQUIRK_NEEDS_PAD_CONTROL |
1423 NVQUIRK_HAS_PADCALIB |
1424 NVQUIRK_DIS_CARD_CLK_CONFIG_TAP |
1425 NVQUIRK_ENABLE_SDR50 |
1426 NVQUIRK_ENABLE_SDR104,
1427 .min_tap_delay = 96,
1428 .max_tap_delay = 139,
1431 static const struct of_device_id sdhci_tegra_dt_match[] = {
1432 { .compatible = "nvidia,tegra194-sdhci", .data = &soc_data_tegra194 },
1433 { .compatible = "nvidia,tegra186-sdhci", .data = &soc_data_tegra186 },
1434 { .compatible = "nvidia,tegra210-sdhci", .data = &soc_data_tegra210 },
1435 { .compatible = "nvidia,tegra124-sdhci", .data = &soc_data_tegra124 },
1436 { .compatible = "nvidia,tegra114-sdhci", .data = &soc_data_tegra114 },
1437 { .compatible = "nvidia,tegra30-sdhci", .data = &soc_data_tegra30 },
1438 { .compatible = "nvidia,tegra20-sdhci", .data = &soc_data_tegra20 },
1441 MODULE_DEVICE_TABLE(of, sdhci_tegra_dt_match);
1443 static int sdhci_tegra_add_host(struct sdhci_host *host)
1445 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1446 struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
1447 struct cqhci_host *cq_host;
1451 if (!tegra_host->enable_hwcq)
1452 return sdhci_add_host(host);
1454 sdhci_enable_v4_mode(host);
1456 ret = sdhci_setup_host(host);
1460 host->mmc->caps2 |= MMC_CAP2_CQE | MMC_CAP2_CQE_DCMD;
1462 cq_host = devm_kzalloc(host->mmc->parent,
1463 sizeof(*cq_host), GFP_KERNEL);
1469 cq_host->mmio = host->ioaddr + SDHCI_TEGRA_CQE_BASE_ADDR;
1470 cq_host->ops = &sdhci_tegra_cqhci_ops;
1472 dma64 = host->flags & SDHCI_USE_64_BIT_DMA;
1474 cq_host->caps |= CQHCI_TASK_DESC_SZ_128;
1476 ret = cqhci_init(cq_host, host->mmc, dma64);
1480 ret = __sdhci_add_host(host);
1487 sdhci_cleanup_host(host);
1491 static int sdhci_tegra_probe(struct platform_device *pdev)
1493 const struct of_device_id *match;
1494 const struct sdhci_tegra_soc_data *soc_data;
1495 struct sdhci_host *host;
1496 struct sdhci_pltfm_host *pltfm_host;
1497 struct sdhci_tegra *tegra_host;
1501 match = of_match_device(sdhci_tegra_dt_match, &pdev->dev);
1504 soc_data = match->data;
1506 host = sdhci_pltfm_init(pdev, soc_data->pdata, sizeof(*tegra_host));
1508 return PTR_ERR(host);
1509 pltfm_host = sdhci_priv(host);
1511 tegra_host = sdhci_pltfm_priv(pltfm_host);
1512 tegra_host->ddr_signaling = false;
1513 tegra_host->pad_calib_required = false;
1514 tegra_host->pad_control_available = false;
1515 tegra_host->soc_data = soc_data;
1517 if (soc_data->nvquirks & NVQUIRK_NEEDS_PAD_CONTROL) {
1518 rc = tegra_sdhci_init_pinctrl_info(&pdev->dev, tegra_host);
1520 host->mmc_host_ops.start_signal_voltage_switch =
1521 sdhci_tegra_start_signal_voltage_switch;
1524 /* Hook to periodically rerun pad calibration */
1525 if (soc_data->nvquirks & NVQUIRK_HAS_PADCALIB)
1526 host->mmc_host_ops.request = tegra_sdhci_request;
1528 host->mmc_host_ops.hs400_enhanced_strobe =
1529 tegra_sdhci_hs400_enhanced_strobe;
1531 if (!host->ops->platform_execute_tuning)
1532 host->mmc_host_ops.execute_tuning =
1533 tegra_sdhci_execute_hw_tuning;
1535 rc = mmc_of_parse(host->mmc);
1539 if (tegra_host->soc_data->nvquirks & NVQUIRK_ENABLE_DDR50)
1540 host->mmc->caps |= MMC_CAP_1_8V_DDR;
1542 tegra_sdhci_parse_dt(host);
1544 tegra_host->power_gpio = devm_gpiod_get_optional(&pdev->dev, "power",
1546 if (IS_ERR(tegra_host->power_gpio)) {
1547 rc = PTR_ERR(tegra_host->power_gpio);
1551 clk = devm_clk_get(mmc_dev(host->mmc), NULL);
1553 dev_err(mmc_dev(host->mmc), "clk err\n");
1557 clk_prepare_enable(clk);
1558 pltfm_host->clk = clk;
1560 tegra_host->rst = devm_reset_control_get_exclusive(&pdev->dev,
1562 if (IS_ERR(tegra_host->rst)) {
1563 rc = PTR_ERR(tegra_host->rst);
1564 dev_err(&pdev->dev, "failed to get reset control: %d\n", rc);
1568 rc = reset_control_assert(tegra_host->rst);
1572 usleep_range(2000, 4000);
1574 rc = reset_control_deassert(tegra_host->rst);
1578 usleep_range(2000, 4000);
1580 rc = sdhci_tegra_add_host(host);
1587 reset_control_assert(tegra_host->rst);
1589 clk_disable_unprepare(pltfm_host->clk);
1593 sdhci_pltfm_free(pdev);
1597 static int sdhci_tegra_remove(struct platform_device *pdev)
1599 struct sdhci_host *host = platform_get_drvdata(pdev);
1600 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1601 struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
1603 sdhci_remove_host(host, 0);
1605 reset_control_assert(tegra_host->rst);
1606 usleep_range(2000, 4000);
1607 clk_disable_unprepare(pltfm_host->clk);
1609 sdhci_pltfm_free(pdev);
1614 #ifdef CONFIG_PM_SLEEP
1615 static int __maybe_unused sdhci_tegra_suspend(struct device *dev)
1617 struct sdhci_host *host = dev_get_drvdata(dev);
1618 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1621 if (host->mmc->caps2 & MMC_CAP2_CQE) {
1622 ret = cqhci_suspend(host->mmc);
1627 ret = sdhci_suspend_host(host);
1629 cqhci_resume(host->mmc);
1633 clk_disable_unprepare(pltfm_host->clk);
1637 static int __maybe_unused sdhci_tegra_resume(struct device *dev)
1639 struct sdhci_host *host = dev_get_drvdata(dev);
1640 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1643 ret = clk_prepare_enable(pltfm_host->clk);
1647 ret = sdhci_resume_host(host);
1651 if (host->mmc->caps2 & MMC_CAP2_CQE) {
1652 ret = cqhci_resume(host->mmc);
1660 sdhci_suspend_host(host);
1662 clk_disable_unprepare(pltfm_host->clk);
1667 static SIMPLE_DEV_PM_OPS(sdhci_tegra_dev_pm_ops, sdhci_tegra_suspend,
1668 sdhci_tegra_resume);
1670 static struct platform_driver sdhci_tegra_driver = {
1672 .name = "sdhci-tegra",
1673 .of_match_table = sdhci_tegra_dt_match,
1674 .pm = &sdhci_tegra_dev_pm_ops,
1676 .probe = sdhci_tegra_probe,
1677 .remove = sdhci_tegra_remove,
1680 module_platform_driver(sdhci_tegra_driver);
1682 MODULE_DESCRIPTION("SDHCI driver for Tegra");
1683 MODULE_AUTHOR("Google, Inc.");
1684 MODULE_LICENSE("GPL v2");