2 * drivers/mmc/host/sdhci-msm.c - Qualcomm SDHCI Platform driver
4 * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 and
8 * only version 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
17 #include <linux/module.h>
18 #include <linux/of_device.h>
19 #include <linux/delay.h>
20 #include <linux/mmc/mmc.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/slab.h>
23 #include <linux/iopoll.h>
24 #include <linux/regulator/consumer.h>
26 #include "sdhci-pltfm.h"
28 #define CORE_MCI_VERSION 0x50
29 #define CORE_VERSION_MAJOR_SHIFT 28
30 #define CORE_VERSION_MAJOR_MASK (0xf << CORE_VERSION_MAJOR_SHIFT)
31 #define CORE_VERSION_MINOR_MASK 0xff
33 #define CORE_MCI_GENERICS 0x70
34 #define SWITCHABLE_SIGNALING_VOLTAGE BIT(29)
36 #define CORE_HC_MODE 0x78
37 #define HC_MODE_EN 0x1
38 #define CORE_POWER 0x0
39 #define CORE_SW_RST BIT(7)
40 #define FF_CLK_SW_RST_DIS BIT(13)
42 #define CORE_PWRCTL_STATUS 0xdc
43 #define CORE_PWRCTL_MASK 0xe0
44 #define CORE_PWRCTL_CLEAR 0xe4
45 #define CORE_PWRCTL_CTL 0xe8
46 #define CORE_PWRCTL_BUS_OFF BIT(0)
47 #define CORE_PWRCTL_BUS_ON BIT(1)
48 #define CORE_PWRCTL_IO_LOW BIT(2)
49 #define CORE_PWRCTL_IO_HIGH BIT(3)
50 #define CORE_PWRCTL_BUS_SUCCESS BIT(0)
51 #define CORE_PWRCTL_IO_SUCCESS BIT(2)
52 #define REQ_BUS_OFF BIT(0)
53 #define REQ_BUS_ON BIT(1)
54 #define REQ_IO_LOW BIT(2)
55 #define REQ_IO_HIGH BIT(3)
58 #define CORE_DLL_LOCK BIT(7)
59 #define CORE_DDR_DLL_LOCK BIT(11)
60 #define CORE_DLL_EN BIT(16)
61 #define CORE_CDR_EN BIT(17)
62 #define CORE_CK_OUT_EN BIT(18)
63 #define CORE_CDR_EXT_EN BIT(19)
64 #define CORE_DLL_PDN BIT(29)
65 #define CORE_DLL_RST BIT(30)
66 #define CORE_DLL_CONFIG 0x100
67 #define CORE_CMD_DAT_TRACK_SEL BIT(0)
68 #define CORE_DLL_STATUS 0x108
70 #define CORE_DLL_CONFIG_2 0x1b4
71 #define CORE_DDR_CAL_EN BIT(0)
72 #define CORE_FLL_CYCLE_CNT BIT(18)
73 #define CORE_DLL_CLOCK_DISABLE BIT(21)
75 #define CORE_VENDOR_SPEC 0x10c
76 #define CORE_VENDOR_SPEC_POR_VAL 0xa1c
77 #define CORE_CLK_PWRSAVE BIT(1)
78 #define CORE_HC_MCLK_SEL_DFLT (2 << 8)
79 #define CORE_HC_MCLK_SEL_HS400 (3 << 8)
80 #define CORE_HC_MCLK_SEL_MASK (3 << 8)
81 #define CORE_HC_SELECT_IN_EN BIT(18)
82 #define CORE_HC_SELECT_IN_HS400 (6 << 19)
83 #define CORE_HC_SELECT_IN_MASK (7 << 19)
85 #define CORE_3_0V_SUPPORT (1 << 25)
86 #define CORE_1_8V_SUPPORT (1 << 26)
88 #define CORE_CSR_CDC_CTLR_CFG0 0x130
89 #define CORE_SW_TRIG_FULL_CALIB BIT(16)
90 #define CORE_HW_AUTOCAL_ENA BIT(17)
92 #define CORE_CSR_CDC_CTLR_CFG1 0x134
93 #define CORE_CSR_CDC_CAL_TIMER_CFG0 0x138
94 #define CORE_TIMER_ENA BIT(16)
96 #define CORE_CSR_CDC_CAL_TIMER_CFG1 0x13C
97 #define CORE_CSR_CDC_REFCOUNT_CFG 0x140
98 #define CORE_CSR_CDC_COARSE_CAL_CFG 0x144
99 #define CORE_CDC_OFFSET_CFG 0x14C
100 #define CORE_CSR_CDC_DELAY_CFG 0x150
101 #define CORE_CDC_SLAVE_DDA_CFG 0x160
102 #define CORE_CSR_CDC_STATUS0 0x164
103 #define CORE_CALIBRATION_DONE BIT(0)
105 #define CORE_CDC_ERROR_CODE_MASK 0x7000000
107 #define CORE_CSR_CDC_GEN_CFG 0x178
108 #define CORE_CDC_SWITCH_BYPASS_OFF BIT(0)
109 #define CORE_CDC_SWITCH_RC_EN BIT(1)
111 #define CORE_DDR_200_CFG 0x184
112 #define CORE_CDC_T4_DLY_SEL BIT(0)
113 #define CORE_CMDIN_RCLK_EN BIT(1)
114 #define CORE_START_CDC_TRAFFIC BIT(6)
115 #define CORE_VENDOR_SPEC3 0x1b0
116 #define CORE_PWRSAVE_DLL BIT(3)
118 #define CORE_DDR_CONFIG 0x1b8
119 #define DDR_CONFIG_POR_VAL 0x80040853
121 #define CORE_VENDOR_SPEC_CAPABILITIES0 0x11c
123 #define INVALID_TUNING_PHASE -1
124 #define SDHCI_MSM_MIN_CLOCK 400000
125 #define CORE_FREQ_100MHZ (100 * 1000 * 1000)
127 #define CDR_SELEXT_SHIFT 20
128 #define CDR_SELEXT_MASK (0xf << CDR_SELEXT_SHIFT)
129 #define CMUX_SHIFT_PHASE_SHIFT 24
130 #define CMUX_SHIFT_PHASE_MASK (7 << CMUX_SHIFT_PHASE_SHIFT)
132 #define MSM_MMC_AUTOSUSPEND_DELAY_MS 50
134 /* Timeout value to avoid infinite waiting for pwr_irq */
135 #define MSM_PWR_IRQ_TIMEOUT_MS 5000
137 struct sdhci_msm_host {
138 struct platform_device *pdev;
139 void __iomem *core_mem; /* MSM SDCC mapped address */
140 int pwr_irq; /* power irq */
141 struct clk *bus_clk; /* SDHC bus voter clock */
142 struct clk *xo_clk; /* TCXO clk needed for FLL feature of cm_dll*/
143 struct clk_bulk_data bulk_clks[4]; /* core, iface, cal, sleep clocks */
144 unsigned long clk_rate;
145 struct mmc_host *mmc;
146 bool use_14lpp_dll_reset;
148 bool calibration_done;
149 u8 saved_tuning_phase;
153 wait_queue_head_t pwr_irq_wait;
158 static unsigned int msm_get_clock_rate_for_bus_mode(struct sdhci_host *host,
161 struct mmc_ios ios = host->mmc->ios;
163 * The SDHC requires internal clock frequency to be double the
164 * actual clock that will be set for DDR mode. The controller
165 * uses the faster clock(100/400MHz) for some of its parts and
166 * send the actual required clock (50/200MHz) to the card.
168 if (ios.timing == MMC_TIMING_UHS_DDR50 ||
169 ios.timing == MMC_TIMING_MMC_DDR52 ||
170 ios.timing == MMC_TIMING_MMC_HS400 ||
171 host->flags & SDHCI_HS400_TUNING)
176 static void msm_set_clock_rate_for_bus_mode(struct sdhci_host *host,
179 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
180 struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
181 struct mmc_ios curr_ios = host->mmc->ios;
182 struct clk *core_clk = msm_host->bulk_clks[0].clk;
185 clock = msm_get_clock_rate_for_bus_mode(host, clock);
186 rc = clk_set_rate(core_clk, clock);
188 pr_err("%s: Failed to set clock at rate %u at timing %d\n",
189 mmc_hostname(host->mmc), clock,
193 msm_host->clk_rate = clock;
194 pr_debug("%s: Setting clock at rate %lu at timing %d\n",
195 mmc_hostname(host->mmc), clk_get_rate(core_clk),
199 /* Platform specific tuning */
200 static inline int msm_dll_poll_ck_out_en(struct sdhci_host *host, u8 poll)
204 struct mmc_host *mmc = host->mmc;
206 /* Poll for CK_OUT_EN bit. max. poll time = 50us */
207 ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) &
210 while (ck_out_en != poll) {
211 if (--wait_cnt == 0) {
212 dev_err(mmc_dev(mmc), "%s: CK_OUT_EN bit is not %d\n",
213 mmc_hostname(mmc), poll);
218 ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) &
225 static int msm_config_cm_dll_phase(struct sdhci_host *host, u8 phase)
228 static const u8 grey_coded_phase_table[] = {
229 0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4,
230 0xc, 0xd, 0xf, 0xe, 0xa, 0xb, 0x9, 0x8
234 struct mmc_host *mmc = host->mmc;
239 spin_lock_irqsave(&host->lock, flags);
241 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
242 config &= ~(CORE_CDR_EN | CORE_CK_OUT_EN);
243 config |= (CORE_CDR_EXT_EN | CORE_DLL_EN);
244 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
246 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '0' */
247 rc = msm_dll_poll_ck_out_en(host, 0);
252 * Write the selected DLL clock output phase (0 ... 15)
253 * to CDR_SELEXT bit field of DLL_CONFIG register.
255 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
256 config &= ~CDR_SELEXT_MASK;
257 config |= grey_coded_phase_table[phase] << CDR_SELEXT_SHIFT;
258 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
260 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
261 config |= CORE_CK_OUT_EN;
262 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
264 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '1' */
265 rc = msm_dll_poll_ck_out_en(host, 1);
269 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
270 config |= CORE_CDR_EN;
271 config &= ~CORE_CDR_EXT_EN;
272 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
276 dev_err(mmc_dev(mmc), "%s: Failed to set DLL phase: %d\n",
277 mmc_hostname(mmc), phase);
279 spin_unlock_irqrestore(&host->lock, flags);
284 * Find out the greatest range of consecuitive selected
285 * DLL clock output phases that can be used as sampling
286 * setting for SD3.0 UHS-I card read operation (in SDR104
287 * timing mode) or for eMMC4.5 card read operation (in
288 * HS400/HS200 timing mode).
289 * Select the 3/4 of the range and configure the DLL with the
290 * selected DLL clock output phase.
293 static int msm_find_most_appropriate_phase(struct sdhci_host *host,
294 u8 *phase_table, u8 total_phases)
297 u8 ranges[MAX_PHASES][MAX_PHASES] = { {0}, {0} };
298 u8 phases_per_row[MAX_PHASES] = { 0 };
299 int row_index = 0, col_index = 0, selected_row_index = 0, curr_max = 0;
300 int i, cnt, phase_0_raw_index = 0, phase_15_raw_index = 0;
301 bool phase_0_found = false, phase_15_found = false;
302 struct mmc_host *mmc = host->mmc;
304 if (!total_phases || (total_phases > MAX_PHASES)) {
305 dev_err(mmc_dev(mmc), "%s: Invalid argument: total_phases=%d\n",
306 mmc_hostname(mmc), total_phases);
310 for (cnt = 0; cnt < total_phases; cnt++) {
311 ranges[row_index][col_index] = phase_table[cnt];
312 phases_per_row[row_index] += 1;
315 if ((cnt + 1) == total_phases) {
317 /* check if next phase in phase_table is consecutive or not */
318 } else if ((phase_table[cnt] + 1) != phase_table[cnt + 1]) {
324 if (row_index >= MAX_PHASES)
327 /* Check if phase-0 is present in first valid window? */
329 phase_0_found = true;
330 phase_0_raw_index = 0;
331 /* Check if cycle exist between 2 valid windows */
332 for (cnt = 1; cnt <= row_index; cnt++) {
333 if (phases_per_row[cnt]) {
334 for (i = 0; i < phases_per_row[cnt]; i++) {
335 if (ranges[cnt][i] == 15) {
336 phase_15_found = true;
337 phase_15_raw_index = cnt;
345 /* If 2 valid windows form cycle then merge them as single window */
346 if (phase_0_found && phase_15_found) {
347 /* number of phases in raw where phase 0 is present */
348 u8 phases_0 = phases_per_row[phase_0_raw_index];
349 /* number of phases in raw where phase 15 is present */
350 u8 phases_15 = phases_per_row[phase_15_raw_index];
352 if (phases_0 + phases_15 >= MAX_PHASES)
354 * If there are more than 1 phase windows then total
355 * number of phases in both the windows should not be
356 * more than or equal to MAX_PHASES.
360 /* Merge 2 cyclic windows */
362 for (cnt = 0; cnt < phases_0; cnt++) {
363 ranges[phase_15_raw_index][i] =
364 ranges[phase_0_raw_index][cnt];
365 if (++i >= MAX_PHASES)
369 phases_per_row[phase_0_raw_index] = 0;
370 phases_per_row[phase_15_raw_index] = phases_15 + phases_0;
373 for (cnt = 0; cnt <= row_index; cnt++) {
374 if (phases_per_row[cnt] > curr_max) {
375 curr_max = phases_per_row[cnt];
376 selected_row_index = cnt;
380 i = (curr_max * 3) / 4;
384 ret = ranges[selected_row_index][i];
386 if (ret >= MAX_PHASES) {
388 dev_err(mmc_dev(mmc), "%s: Invalid phase selected=%d\n",
389 mmc_hostname(mmc), ret);
395 static inline void msm_cm_dll_set_freq(struct sdhci_host *host)
397 u32 mclk_freq = 0, config;
399 /* Program the MCLK value to MCLK_FREQ bit field */
400 if (host->clock <= 112000000)
402 else if (host->clock <= 125000000)
404 else if (host->clock <= 137000000)
406 else if (host->clock <= 150000000)
408 else if (host->clock <= 162000000)
410 else if (host->clock <= 175000000)
412 else if (host->clock <= 187000000)
414 else if (host->clock <= 200000000)
417 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
418 config &= ~CMUX_SHIFT_PHASE_MASK;
419 config |= mclk_freq << CMUX_SHIFT_PHASE_SHIFT;
420 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
423 /* Initialize the DLL (Programmable Delay Line) */
424 static int msm_init_cm_dll(struct sdhci_host *host)
426 struct mmc_host *mmc = host->mmc;
427 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
428 struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
433 spin_lock_irqsave(&host->lock, flags);
436 * Make sure that clock is always enabled when DLL
437 * tuning is in progress. Keeping PWRSAVE ON may
438 * turn off the clock.
440 config = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC);
441 config &= ~CORE_CLK_PWRSAVE;
442 writel_relaxed(config, host->ioaddr + CORE_VENDOR_SPEC);
444 if (msm_host->use_14lpp_dll_reset) {
445 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
446 config &= ~CORE_CK_OUT_EN;
447 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
449 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2);
450 config |= CORE_DLL_CLOCK_DISABLE;
451 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG_2);
454 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
455 config |= CORE_DLL_RST;
456 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
458 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
459 config |= CORE_DLL_PDN;
460 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
461 msm_cm_dll_set_freq(host);
463 if (msm_host->use_14lpp_dll_reset &&
464 !IS_ERR_OR_NULL(msm_host->xo_clk)) {
467 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2);
468 config &= CORE_FLL_CYCLE_CNT;
470 mclk_freq = DIV_ROUND_CLOSEST_ULL((host->clock * 8),
471 clk_get_rate(msm_host->xo_clk));
473 mclk_freq = DIV_ROUND_CLOSEST_ULL((host->clock * 4),
474 clk_get_rate(msm_host->xo_clk));
476 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2);
477 config &= ~(0xFF << 10);
478 config |= mclk_freq << 10;
480 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG_2);
481 /* wait for 5us before enabling DLL clock */
485 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
486 config &= ~CORE_DLL_RST;
487 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
489 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
490 config &= ~CORE_DLL_PDN;
491 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
493 if (msm_host->use_14lpp_dll_reset) {
494 msm_cm_dll_set_freq(host);
495 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2);
496 config &= ~CORE_DLL_CLOCK_DISABLE;
497 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG_2);
500 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
501 config |= CORE_DLL_EN;
502 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
504 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
505 config |= CORE_CK_OUT_EN;
506 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
508 /* Wait until DLL_LOCK bit of DLL_STATUS register becomes '1' */
509 while (!(readl_relaxed(host->ioaddr + CORE_DLL_STATUS) &
511 /* max. wait for 50us sec for LOCK bit to be set */
512 if (--wait_cnt == 0) {
513 dev_err(mmc_dev(mmc), "%s: DLL failed to LOCK\n",
515 spin_unlock_irqrestore(&host->lock, flags);
521 spin_unlock_irqrestore(&host->lock, flags);
525 static void msm_hc_select_default(struct sdhci_host *host)
527 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
528 struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
531 if (!msm_host->use_cdclp533) {
532 config = readl_relaxed(host->ioaddr +
534 config &= ~CORE_PWRSAVE_DLL;
535 writel_relaxed(config, host->ioaddr +
539 config = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC);
540 config &= ~CORE_HC_MCLK_SEL_MASK;
541 config |= CORE_HC_MCLK_SEL_DFLT;
542 writel_relaxed(config, host->ioaddr + CORE_VENDOR_SPEC);
545 * Disable HC_SELECT_IN to be able to use the UHS mode select
546 * configuration from Host Control2 register for all other
548 * Write 0 to HC_SELECT_IN and HC_SELECT_IN_EN field
549 * in VENDOR_SPEC_FUNC
551 config = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC);
552 config &= ~CORE_HC_SELECT_IN_EN;
553 config &= ~CORE_HC_SELECT_IN_MASK;
554 writel_relaxed(config, host->ioaddr + CORE_VENDOR_SPEC);
557 * Make sure above writes impacting free running MCLK are completed
558 * before changing the clk_rate at GCC.
563 static void msm_hc_select_hs400(struct sdhci_host *host)
565 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
566 struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
567 struct mmc_ios ios = host->mmc->ios;
568 u32 config, dll_lock;
571 /* Select the divided clock (free running MCLK/2) */
572 config = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC);
573 config &= ~CORE_HC_MCLK_SEL_MASK;
574 config |= CORE_HC_MCLK_SEL_HS400;
576 writel_relaxed(config, host->ioaddr + CORE_VENDOR_SPEC);
578 * Select HS400 mode using the HC_SELECT_IN from VENDOR SPEC
581 if ((msm_host->tuning_done || ios.enhanced_strobe) &&
582 !msm_host->calibration_done) {
583 config = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC);
584 config |= CORE_HC_SELECT_IN_HS400;
585 config |= CORE_HC_SELECT_IN_EN;
586 writel_relaxed(config, host->ioaddr + CORE_VENDOR_SPEC);
588 if (!msm_host->clk_rate && !msm_host->use_cdclp533) {
590 * Poll on DLL_LOCK or DDR_DLL_LOCK bits in
591 * CORE_DLL_STATUS to be set. This should get set
592 * within 15 us at 200 MHz.
594 rc = readl_relaxed_poll_timeout(host->ioaddr +
599 CORE_DDR_DLL_LOCK)), 10,
601 if (rc == -ETIMEDOUT)
602 pr_err("%s: Unable to get DLL_LOCK/DDR_DLL_LOCK, dll_status: 0x%08x\n",
603 mmc_hostname(host->mmc), dll_lock);
606 * Make sure above writes impacting free running MCLK are completed
607 * before changing the clk_rate at GCC.
613 * sdhci_msm_hc_select_mode :- In general all timing modes are
614 * controlled via UHS mode select in Host Control2 register.
615 * eMMC specific HS200/HS400 doesn't have their respective modes
616 * defined here, hence we use these values.
618 * HS200 - SDR104 (Since they both are equivalent in functionality)
619 * HS400 - This involves multiple configurations
620 * Initially SDR104 - when tuning is required as HS200
621 * Then when switching to DDR @ 400MHz (HS400) we use
622 * the vendor specific HC_SELECT_IN to control the mode.
624 * In addition to controlling the modes we also need to select the
625 * correct input clock for DLL depending on the mode.
627 * HS400 - divided clock (free running MCLK/2)
628 * All other modes - default (free running MCLK)
630 static void sdhci_msm_hc_select_mode(struct sdhci_host *host)
632 struct mmc_ios ios = host->mmc->ios;
634 if (ios.timing == MMC_TIMING_MMC_HS400 ||
635 host->flags & SDHCI_HS400_TUNING)
636 msm_hc_select_hs400(host);
638 msm_hc_select_default(host);
641 static int sdhci_msm_cdclp533_calibration(struct sdhci_host *host)
643 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
644 struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
645 u32 config, calib_done;
648 pr_debug("%s: %s: Enter\n", mmc_hostname(host->mmc), __func__);
651 * Retuning in HS400 (DDR mode) will fail, just reset the
652 * tuning block and restore the saved tuning phase.
654 ret = msm_init_cm_dll(host);
658 /* Set the selected phase in delay line hw block */
659 ret = msm_config_cm_dll_phase(host, msm_host->saved_tuning_phase);
663 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
664 config |= CORE_CMD_DAT_TRACK_SEL;
665 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
667 config = readl_relaxed(host->ioaddr + CORE_DDR_200_CFG);
668 config &= ~CORE_CDC_T4_DLY_SEL;
669 writel_relaxed(config, host->ioaddr + CORE_DDR_200_CFG);
671 config = readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG);
672 config &= ~CORE_CDC_SWITCH_BYPASS_OFF;
673 writel_relaxed(config, host->ioaddr + CORE_CSR_CDC_GEN_CFG);
675 config = readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG);
676 config |= CORE_CDC_SWITCH_RC_EN;
677 writel_relaxed(config, host->ioaddr + CORE_CSR_CDC_GEN_CFG);
679 config = readl_relaxed(host->ioaddr + CORE_DDR_200_CFG);
680 config &= ~CORE_START_CDC_TRAFFIC;
681 writel_relaxed(config, host->ioaddr + CORE_DDR_200_CFG);
683 /* Perform CDC Register Initialization Sequence */
685 writel_relaxed(0x11800EC, host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
686 writel_relaxed(0x3011111, host->ioaddr + CORE_CSR_CDC_CTLR_CFG1);
687 writel_relaxed(0x1201000, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
688 writel_relaxed(0x4, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG1);
689 writel_relaxed(0xCB732020, host->ioaddr + CORE_CSR_CDC_REFCOUNT_CFG);
690 writel_relaxed(0xB19, host->ioaddr + CORE_CSR_CDC_COARSE_CAL_CFG);
691 writel_relaxed(0x4E2, host->ioaddr + CORE_CSR_CDC_DELAY_CFG);
692 writel_relaxed(0x0, host->ioaddr + CORE_CDC_OFFSET_CFG);
693 writel_relaxed(0x16334, host->ioaddr + CORE_CDC_SLAVE_DDA_CFG);
695 /* CDC HW Calibration */
697 config = readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
698 config |= CORE_SW_TRIG_FULL_CALIB;
699 writel_relaxed(config, host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
701 config = readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
702 config &= ~CORE_SW_TRIG_FULL_CALIB;
703 writel_relaxed(config, host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
705 config = readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
706 config |= CORE_HW_AUTOCAL_ENA;
707 writel_relaxed(config, host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
709 config = readl_relaxed(host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
710 config |= CORE_TIMER_ENA;
711 writel_relaxed(config, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
713 ret = readl_relaxed_poll_timeout(host->ioaddr + CORE_CSR_CDC_STATUS0,
715 (calib_done & CORE_CALIBRATION_DONE),
718 if (ret == -ETIMEDOUT) {
719 pr_err("%s: %s: CDC calibration was not completed\n",
720 mmc_hostname(host->mmc), __func__);
724 ret = readl_relaxed(host->ioaddr + CORE_CSR_CDC_STATUS0)
725 & CORE_CDC_ERROR_CODE_MASK;
727 pr_err("%s: %s: CDC error code %d\n",
728 mmc_hostname(host->mmc), __func__, ret);
733 config = readl_relaxed(host->ioaddr + CORE_DDR_200_CFG);
734 config |= CORE_START_CDC_TRAFFIC;
735 writel_relaxed(config, host->ioaddr + CORE_DDR_200_CFG);
737 pr_debug("%s: %s: Exit, ret %d\n", mmc_hostname(host->mmc),
742 static int sdhci_msm_cm_dll_sdc4_calibration(struct sdhci_host *host)
744 struct mmc_host *mmc = host->mmc;
745 u32 dll_status, config;
748 pr_debug("%s: %s: Enter\n", mmc_hostname(host->mmc), __func__);
751 * Currently the CORE_DDR_CONFIG register defaults to desired
752 * configuration on reset. Currently reprogramming the power on
753 * reset (POR) value in case it might have been modified by
754 * bootloaders. In the future, if this changes, then the desired
755 * values will need to be programmed appropriately.
757 writel_relaxed(DDR_CONFIG_POR_VAL, host->ioaddr + CORE_DDR_CONFIG);
759 if (mmc->ios.enhanced_strobe) {
760 config = readl_relaxed(host->ioaddr + CORE_DDR_200_CFG);
761 config |= CORE_CMDIN_RCLK_EN;
762 writel_relaxed(config, host->ioaddr + CORE_DDR_200_CFG);
765 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2);
766 config |= CORE_DDR_CAL_EN;
767 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG_2);
769 ret = readl_relaxed_poll_timeout(host->ioaddr + CORE_DLL_STATUS,
771 (dll_status & CORE_DDR_DLL_LOCK),
774 if (ret == -ETIMEDOUT) {
775 pr_err("%s: %s: CM_DLL_SDC4 calibration was not completed\n",
776 mmc_hostname(host->mmc), __func__);
780 config = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC3);
781 config |= CORE_PWRSAVE_DLL;
782 writel_relaxed(config, host->ioaddr + CORE_VENDOR_SPEC3);
785 * Drain writebuffer to ensure above DLL calibration
786 * and PWRSAVE DLL is enabled.
790 pr_debug("%s: %s: Exit, ret %d\n", mmc_hostname(host->mmc),
795 static int sdhci_msm_hs400_dll_calibration(struct sdhci_host *host)
797 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
798 struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
799 struct mmc_host *mmc = host->mmc;
803 pr_debug("%s: %s: Enter\n", mmc_hostname(host->mmc), __func__);
806 * Retuning in HS400 (DDR mode) will fail, just reset the
807 * tuning block and restore the saved tuning phase.
809 ret = msm_init_cm_dll(host);
813 if (!mmc->ios.enhanced_strobe) {
814 /* Set the selected phase in delay line hw block */
815 ret = msm_config_cm_dll_phase(host,
816 msm_host->saved_tuning_phase);
819 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
820 config |= CORE_CMD_DAT_TRACK_SEL;
821 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
824 if (msm_host->use_cdclp533)
825 ret = sdhci_msm_cdclp533_calibration(host);
827 ret = sdhci_msm_cm_dll_sdc4_calibration(host);
829 pr_debug("%s: %s: Exit, ret %d\n", mmc_hostname(host->mmc),
834 static int sdhci_msm_execute_tuning(struct mmc_host *mmc, u32 opcode)
836 struct sdhci_host *host = mmc_priv(mmc);
837 int tuning_seq_cnt = 3;
838 u8 phase, tuned_phases[16], tuned_phase_cnt = 0;
840 struct mmc_ios ios = host->mmc->ios;
841 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
842 struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
845 * Tuning is required for SDR104, HS200 and HS400 cards and
846 * if clock frequency is greater than 100MHz in these modes.
848 if (host->clock <= CORE_FREQ_100MHZ ||
849 !(ios.timing == MMC_TIMING_MMC_HS400 ||
850 ios.timing == MMC_TIMING_MMC_HS200 ||
851 ios.timing == MMC_TIMING_UHS_SDR104))
855 * For HS400 tuning in HS200 timing requires:
856 * - select MCLK/2 in VENDOR_SPEC
857 * - program MCLK to 400MHz (or nearest supported) in GCC
859 if (host->flags & SDHCI_HS400_TUNING) {
860 sdhci_msm_hc_select_mode(host);
861 msm_set_clock_rate_for_bus_mode(host, ios.clock);
862 host->flags &= ~SDHCI_HS400_TUNING;
866 /* First of all reset the tuning block */
867 rc = msm_init_cm_dll(host);
873 /* Set the phase in delay line hw block */
874 rc = msm_config_cm_dll_phase(host, phase);
878 msm_host->saved_tuning_phase = phase;
879 rc = mmc_send_tuning(mmc, opcode, NULL);
881 /* Tuning is successful at this tuning point */
882 tuned_phases[tuned_phase_cnt++] = phase;
883 dev_dbg(mmc_dev(mmc), "%s: Found good phase = %d\n",
884 mmc_hostname(mmc), phase);
886 } while (++phase < ARRAY_SIZE(tuned_phases));
888 if (tuned_phase_cnt) {
889 rc = msm_find_most_appropriate_phase(host, tuned_phases,
897 * Finally set the selected phase in delay
900 rc = msm_config_cm_dll_phase(host, phase);
903 dev_dbg(mmc_dev(mmc), "%s: Setting the tuning phase to %d\n",
904 mmc_hostname(mmc), phase);
906 if (--tuning_seq_cnt)
909 dev_dbg(mmc_dev(mmc), "%s: No tuning point found\n",
915 msm_host->tuning_done = true;
920 * sdhci_msm_hs400 - Calibrate the DLL for HS400 bus speed mode operation.
921 * This needs to be done for both tuning and enhanced_strobe mode.
922 * DLL operation is only needed for clock > 100MHz. For clock <= 100MHz
923 * fixed feedback clock is used.
925 static void sdhci_msm_hs400(struct sdhci_host *host, struct mmc_ios *ios)
927 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
928 struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
931 if (host->clock > CORE_FREQ_100MHZ &&
932 (msm_host->tuning_done || ios->enhanced_strobe) &&
933 !msm_host->calibration_done) {
934 ret = sdhci_msm_hs400_dll_calibration(host);
936 msm_host->calibration_done = true;
938 pr_err("%s: Failed to calibrate DLL for hs400 mode (%d)\n",
939 mmc_hostname(host->mmc), ret);
943 static void sdhci_msm_set_uhs_signaling(struct sdhci_host *host,
946 struct mmc_host *mmc = host->mmc;
947 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
948 struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
952 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
953 /* Select Bus Speed Mode for host */
954 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
956 case MMC_TIMING_UHS_SDR12:
957 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
959 case MMC_TIMING_UHS_SDR25:
960 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
962 case MMC_TIMING_UHS_SDR50:
963 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
965 case MMC_TIMING_MMC_HS400:
966 case MMC_TIMING_MMC_HS200:
967 case MMC_TIMING_UHS_SDR104:
968 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
970 case MMC_TIMING_UHS_DDR50:
971 case MMC_TIMING_MMC_DDR52:
972 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
977 * When clock frequency is less than 100MHz, the feedback clock must be
978 * provided and DLL must not be used so that tuning can be skipped. To
979 * provide feedback clock, the mode selection can be any value less
980 * than 3'b011 in bits [2:0] of HOST CONTROL2 register.
982 if (host->clock <= CORE_FREQ_100MHZ) {
983 if (uhs == MMC_TIMING_MMC_HS400 ||
984 uhs == MMC_TIMING_MMC_HS200 ||
985 uhs == MMC_TIMING_UHS_SDR104)
986 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
988 * DLL is not required for clock <= 100MHz
989 * Thus, make sure DLL it is disabled when not required
991 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
992 config |= CORE_DLL_RST;
993 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
995 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
996 config |= CORE_DLL_PDN;
997 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
1000 * The DLL needs to be restored and CDCLP533 recalibrated
1001 * when the clock frequency is set back to 400MHz.
1003 msm_host->calibration_done = false;
1006 dev_dbg(mmc_dev(mmc), "%s: clock=%u uhs=%u ctrl_2=0x%x\n",
1007 mmc_hostname(host->mmc), host->clock, uhs, ctrl_2);
1008 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1010 if (mmc->ios.timing == MMC_TIMING_MMC_HS400)
1011 sdhci_msm_hs400(host, &mmc->ios);
1014 static inline void sdhci_msm_init_pwr_irq_wait(struct sdhci_msm_host *msm_host)
1016 init_waitqueue_head(&msm_host->pwr_irq_wait);
1019 static inline void sdhci_msm_complete_pwr_irq_wait(
1020 struct sdhci_msm_host *msm_host)
1022 wake_up(&msm_host->pwr_irq_wait);
1026 * sdhci_msm_check_power_status API should be called when registers writes
1027 * which can toggle sdhci IO bus ON/OFF or change IO lines HIGH/LOW happens.
1028 * To what state the register writes will change the IO lines should be passed
1029 * as the argument req_type. This API will check whether the IO line's state
1030 * is already the expected state and will wait for power irq only if
1031 * power irq is expected to be trigerred based on the current IO line state
1032 * and expected IO line state.
1034 static void sdhci_msm_check_power_status(struct sdhci_host *host, u32 req_type)
1036 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1037 struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
1041 pr_debug("%s: %s: request %d curr_pwr_state %x curr_io_level %x\n",
1042 mmc_hostname(host->mmc), __func__, req_type,
1043 msm_host->curr_pwr_state, msm_host->curr_io_level);
1046 * The power interrupt will not be generated for signal voltage
1047 * switches if SWITCHABLE_SIGNALING_VOLTAGE in MCI_GENERICS is not set.
1049 val = readl(msm_host->core_mem + CORE_MCI_GENERICS);
1050 if ((req_type & REQ_IO_HIGH || req_type & REQ_IO_LOW) &&
1051 !(val & SWITCHABLE_SIGNALING_VOLTAGE)) {
1056 * The IRQ for request type IO High/LOW will be generated when -
1057 * there is a state change in 1.8V enable bit (bit 3) of
1058 * SDHCI_HOST_CONTROL2 register. The reset state of that bit is 0
1059 * which indicates 3.3V IO voltage. So, when MMC core layer tries
1060 * to set it to 3.3V before card detection happens, the
1061 * IRQ doesn't get triggered as there is no state change in this bit.
1062 * The driver already handles this case by changing the IO voltage
1063 * level to high as part of controller power up sequence. Hence, check
1064 * for host->pwr to handle a case where IO voltage high request is
1065 * issued even before controller power up.
1067 if ((req_type & REQ_IO_HIGH) && !host->pwr) {
1068 pr_debug("%s: do not wait for power IRQ that never comes, req_type: %d\n",
1069 mmc_hostname(host->mmc), req_type);
1072 if ((req_type & msm_host->curr_pwr_state) ||
1073 (req_type & msm_host->curr_io_level))
1076 * This is needed here to handle cases where register writes will
1077 * not change the current bus state or io level of the controller.
1078 * In this case, no power irq will be triggerred and we should
1082 if (!wait_event_timeout(msm_host->pwr_irq_wait,
1083 msm_host->pwr_irq_flag,
1084 msecs_to_jiffies(MSM_PWR_IRQ_TIMEOUT_MS)))
1085 dev_warn(&msm_host->pdev->dev,
1086 "%s: pwr_irq for req: (%d) timed out\n",
1087 mmc_hostname(host->mmc), req_type);
1089 pr_debug("%s: %s: request %d done\n", mmc_hostname(host->mmc),
1090 __func__, req_type);
1093 static void sdhci_msm_dump_pwr_ctrl_regs(struct sdhci_host *host)
1095 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1096 struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
1098 pr_err("%s: PWRCTL_STATUS: 0x%08x | PWRCTL_MASK: 0x%08x | PWRCTL_CTL: 0x%08x\n",
1099 mmc_hostname(host->mmc),
1100 readl_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS),
1101 readl_relaxed(msm_host->core_mem + CORE_PWRCTL_MASK),
1102 readl_relaxed(msm_host->core_mem + CORE_PWRCTL_CTL));
1105 static void sdhci_msm_handle_pwr_irq(struct sdhci_host *host, int irq)
1107 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1108 struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
1109 u32 irq_status, irq_ack = 0;
1111 u32 pwr_state = 0, io_level = 0;
1114 irq_status = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
1115 irq_status &= INT_MASK;
1117 writel_relaxed(irq_status, msm_host->core_mem + CORE_PWRCTL_CLEAR);
1120 * There is a rare HW scenario where the first clear pulse could be
1121 * lost when actual reset and clear/read of status register is
1122 * happening at a time. Hence, retry for at least 10 times to make
1123 * sure status register is cleared. Otherwise, this will result in
1124 * a spurious power IRQ resulting in system instability.
1126 while (irq_status & readl_relaxed(msm_host->core_mem +
1127 CORE_PWRCTL_STATUS)) {
1129 pr_err("%s: Timedout clearing (0x%x) pwrctl status register\n",
1130 mmc_hostname(host->mmc), irq_status);
1131 sdhci_msm_dump_pwr_ctrl_regs(host);
1135 writel_relaxed(irq_status,
1136 msm_host->core_mem + CORE_PWRCTL_CLEAR);
1141 /* Handle BUS ON/OFF*/
1142 if (irq_status & CORE_PWRCTL_BUS_ON) {
1143 pwr_state = REQ_BUS_ON;
1144 io_level = REQ_IO_HIGH;
1145 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
1147 if (irq_status & CORE_PWRCTL_BUS_OFF) {
1148 pwr_state = REQ_BUS_OFF;
1149 io_level = REQ_IO_LOW;
1150 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
1152 /* Handle IO LOW/HIGH */
1153 if (irq_status & CORE_PWRCTL_IO_LOW) {
1154 io_level = REQ_IO_LOW;
1155 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
1157 if (irq_status & CORE_PWRCTL_IO_HIGH) {
1158 io_level = REQ_IO_HIGH;
1159 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
1163 * The driver has to acknowledge the interrupt, switch voltages and
1164 * report back if it succeded or not to this register. The voltage
1165 * switches are handled by the sdhci core, so just report success.
1167 writel_relaxed(irq_ack, msm_host->core_mem + CORE_PWRCTL_CTL);
1170 msm_host->curr_pwr_state = pwr_state;
1172 msm_host->curr_io_level = io_level;
1174 pr_debug("%s: %s: Handled IRQ(%d), irq_status=0x%x, ack=0x%x\n",
1175 mmc_hostname(msm_host->mmc), __func__, irq, irq_status,
1179 static irqreturn_t sdhci_msm_pwr_irq(int irq, void *data)
1181 struct sdhci_host *host = (struct sdhci_host *)data;
1182 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1183 struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
1185 sdhci_msm_handle_pwr_irq(host, irq);
1186 msm_host->pwr_irq_flag = 1;
1187 sdhci_msm_complete_pwr_irq_wait(msm_host);
1193 static unsigned int sdhci_msm_get_max_clock(struct sdhci_host *host)
1195 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1196 struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
1197 struct clk *core_clk = msm_host->bulk_clks[0].clk;
1199 return clk_round_rate(core_clk, ULONG_MAX);
1202 static unsigned int sdhci_msm_get_min_clock(struct sdhci_host *host)
1204 return SDHCI_MSM_MIN_CLOCK;
1208 * __sdhci_msm_set_clock - sdhci_msm clock control.
1211 * MSM controller does not use internal divider and
1212 * instead directly control the GCC clock as per
1213 * HW recommendation.
1215 static void __sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock)
1219 * Keep actual_clock as zero -
1220 * - since there is no divider used so no need of having actual_clock.
1221 * - MSM controller uses SDCLK for data timeout calculation. If
1222 * actual_clock is zero, host->clock is taken for calculation.
1224 host->mmc->actual_clock = 0;
1226 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
1232 * MSM controller do not use clock divider.
1233 * Thus read SDHCI_CLOCK_CONTROL and only enable
1234 * clock with no divider value programmed.
1236 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1237 sdhci_enable_clk(host, clk);
1240 /* sdhci_msm_set_clock - Called with (host->lock) spinlock held. */
1241 static void sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock)
1243 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1244 struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
1247 msm_host->clk_rate = clock;
1251 sdhci_msm_hc_select_mode(host);
1253 msm_set_clock_rate_for_bus_mode(host, clock);
1255 __sdhci_msm_set_clock(host, clock);
1259 * Platform specific register write functions. This is so that, if any
1260 * register write needs to be followed up by platform specific actions,
1261 * they can be added here. These functions can go to sleep when writes
1262 * to certain registers are done.
1263 * These functions are relying on sdhci_set_ios not using spinlock.
1265 static int __sdhci_msm_check_write(struct sdhci_host *host, u16 val, int reg)
1267 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1268 struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
1272 case SDHCI_HOST_CONTROL2:
1273 req_type = (val & SDHCI_CTRL_VDD_180) ? REQ_IO_LOW :
1276 case SDHCI_SOFTWARE_RESET:
1277 if (host->pwr && (val & SDHCI_RESET_ALL))
1278 req_type = REQ_BUS_OFF;
1280 case SDHCI_POWER_CONTROL:
1281 req_type = !val ? REQ_BUS_OFF : REQ_BUS_ON;
1286 msm_host->pwr_irq_flag = 0;
1288 * Since this register write may trigger a power irq, ensure
1289 * all previous register writes are complete by this point.
1296 /* This function may sleep*/
1297 static void sdhci_msm_writew(struct sdhci_host *host, u16 val, int reg)
1301 req_type = __sdhci_msm_check_write(host, val, reg);
1302 writew_relaxed(val, host->ioaddr + reg);
1305 sdhci_msm_check_power_status(host, req_type);
1308 /* This function may sleep*/
1309 static void sdhci_msm_writeb(struct sdhci_host *host, u8 val, int reg)
1313 req_type = __sdhci_msm_check_write(host, val, reg);
1315 writeb_relaxed(val, host->ioaddr + reg);
1318 sdhci_msm_check_power_status(host, req_type);
1321 static void sdhci_msm_set_regulator_caps(struct sdhci_msm_host *msm_host)
1323 struct mmc_host *mmc = msm_host->mmc;
1324 struct regulator *supply = mmc->supply.vqmmc;
1327 if (!IS_ERR(mmc->supply.vqmmc)) {
1328 if (regulator_is_supported_voltage(supply, 1700000, 1950000))
1329 caps |= CORE_1_8V_SUPPORT;
1330 if (regulator_is_supported_voltage(supply, 2700000, 3600000))
1331 caps |= CORE_3_0V_SUPPORT;
1334 pr_warn("%s: 1.8/3V not supported for vqmmc\n",
1338 msm_host->caps_0 |= caps;
1339 pr_debug("%s: supported caps: 0x%08x\n", mmc_hostname(mmc), caps);
1342 static const struct of_device_id sdhci_msm_dt_match[] = {
1343 { .compatible = "qcom,sdhci-msm-v4" },
1347 MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match);
1349 static const struct sdhci_ops sdhci_msm_ops = {
1350 .reset = sdhci_reset,
1351 .set_clock = sdhci_msm_set_clock,
1352 .get_min_clock = sdhci_msm_get_min_clock,
1353 .get_max_clock = sdhci_msm_get_max_clock,
1354 .set_bus_width = sdhci_set_bus_width,
1355 .set_uhs_signaling = sdhci_msm_set_uhs_signaling,
1356 .write_w = sdhci_msm_writew,
1357 .write_b = sdhci_msm_writeb,
1360 static const struct sdhci_pltfm_data sdhci_msm_pdata = {
1361 .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
1362 SDHCI_QUIRK_NO_CARD_NO_RESET |
1363 SDHCI_QUIRK_SINGLE_POWER_WRITE |
1364 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
1365 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
1366 .ops = &sdhci_msm_ops,
1369 static int sdhci_msm_probe(struct platform_device *pdev)
1371 struct sdhci_host *host;
1372 struct sdhci_pltfm_host *pltfm_host;
1373 struct sdhci_msm_host *msm_host;
1374 struct resource *core_memres;
1377 u16 host_version, core_minor;
1378 u32 core_version, config;
1381 host = sdhci_pltfm_init(pdev, &sdhci_msm_pdata, sizeof(*msm_host));
1383 return PTR_ERR(host);
1385 host->sdma_boundary = 0;
1386 pltfm_host = sdhci_priv(host);
1387 msm_host = sdhci_pltfm_priv(pltfm_host);
1388 msm_host->mmc = host->mmc;
1389 msm_host->pdev = pdev;
1391 ret = mmc_of_parse(host->mmc);
1395 sdhci_get_of_property(pdev);
1397 msm_host->saved_tuning_phase = INVALID_TUNING_PHASE;
1399 /* Setup SDCC bus voter clock. */
1400 msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus");
1401 if (!IS_ERR(msm_host->bus_clk)) {
1402 /* Vote for max. clk rate for max. performance */
1403 ret = clk_set_rate(msm_host->bus_clk, INT_MAX);
1406 ret = clk_prepare_enable(msm_host->bus_clk);
1411 /* Setup main peripheral bus clock */
1412 clk = devm_clk_get(&pdev->dev, "iface");
1415 dev_err(&pdev->dev, "Peripheral clk setup failed (%d)\n", ret);
1416 goto bus_clk_disable;
1418 msm_host->bulk_clks[1].clk = clk;
1420 /* Setup SDC MMC clock */
1421 clk = devm_clk_get(&pdev->dev, "core");
1424 dev_err(&pdev->dev, "SDC MMC clk setup failed (%d)\n", ret);
1425 goto bus_clk_disable;
1427 msm_host->bulk_clks[0].clk = clk;
1429 /* Vote for maximum clock rate for maximum performance */
1430 ret = clk_set_rate(clk, INT_MAX);
1432 dev_warn(&pdev->dev, "core clock boost failed\n");
1434 clk = devm_clk_get(&pdev->dev, "cal");
1437 msm_host->bulk_clks[2].clk = clk;
1439 clk = devm_clk_get(&pdev->dev, "sleep");
1442 msm_host->bulk_clks[3].clk = clk;
1444 ret = clk_bulk_prepare_enable(ARRAY_SIZE(msm_host->bulk_clks),
1445 msm_host->bulk_clks);
1447 goto bus_clk_disable;
1450 * xo clock is needed for FLL feature of cm_dll.
1451 * In case if xo clock is not mentioned in DT, warn and proceed.
1453 msm_host->xo_clk = devm_clk_get(&pdev->dev, "xo");
1454 if (IS_ERR(msm_host->xo_clk)) {
1455 ret = PTR_ERR(msm_host->xo_clk);
1456 dev_warn(&pdev->dev, "TCXO clk not present (%d)\n", ret);
1459 core_memres = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1460 msm_host->core_mem = devm_ioremap_resource(&pdev->dev, core_memres);
1462 if (IS_ERR(msm_host->core_mem)) {
1463 dev_err(&pdev->dev, "Failed to remap registers\n");
1464 ret = PTR_ERR(msm_host->core_mem);
1468 /* Reset the vendor spec register to power on reset state */
1469 writel_relaxed(CORE_VENDOR_SPEC_POR_VAL,
1470 host->ioaddr + CORE_VENDOR_SPEC);
1472 /* Set HC_MODE_EN bit in HC_MODE register */
1473 writel_relaxed(HC_MODE_EN, (msm_host->core_mem + CORE_HC_MODE));
1475 config = readl_relaxed(msm_host->core_mem + CORE_HC_MODE);
1476 config |= FF_CLK_SW_RST_DIS;
1477 writel_relaxed(config, msm_host->core_mem + CORE_HC_MODE);
1479 host_version = readw_relaxed((host->ioaddr + SDHCI_HOST_VERSION));
1480 dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n",
1481 host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >>
1482 SDHCI_VENDOR_VER_SHIFT));
1484 core_version = readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION);
1485 core_major = (core_version & CORE_VERSION_MAJOR_MASK) >>
1486 CORE_VERSION_MAJOR_SHIFT;
1487 core_minor = core_version & CORE_VERSION_MINOR_MASK;
1488 dev_dbg(&pdev->dev, "MCI Version: 0x%08x, major: 0x%04x, minor: 0x%02x\n",
1489 core_version, core_major, core_minor);
1491 if (core_major == 1 && core_minor >= 0x42)
1492 msm_host->use_14lpp_dll_reset = true;
1495 * SDCC 5 controller with major version 1, minor version 0x34 and later
1496 * with HS 400 mode support will use CM DLL instead of CDC LP 533 DLL.
1498 if (core_major == 1 && core_minor < 0x34)
1499 msm_host->use_cdclp533 = true;
1502 * Support for some capabilities is not advertised by newer
1503 * controller versions and must be explicitly enabled.
1505 if (core_major >= 1 && core_minor != 0x11 && core_minor != 0x12) {
1506 config = readl_relaxed(host->ioaddr + SDHCI_CAPABILITIES);
1507 config |= SDHCI_CAN_VDD_300 | SDHCI_CAN_DO_8BIT;
1508 writel_relaxed(config, host->ioaddr +
1509 CORE_VENDOR_SPEC_CAPABILITIES0);
1513 * Power on reset state may trigger power irq if previous status of
1514 * PWRCTL was either BUS_ON or IO_HIGH_V. So before enabling pwr irq
1515 * interrupt in GIC, any pending power irq interrupt should be
1516 * acknowledged. Otherwise power irq interrupt handler would be
1517 * fired prematurely.
1519 sdhci_msm_handle_pwr_irq(host, 0);
1522 * Ensure that above writes are propogated before interrupt enablement
1527 /* Setup IRQ for handling power/voltage tasks with PMIC */
1528 msm_host->pwr_irq = platform_get_irq_byname(pdev, "pwr_irq");
1529 if (msm_host->pwr_irq < 0) {
1530 dev_err(&pdev->dev, "Get pwr_irq failed (%d)\n",
1532 ret = msm_host->pwr_irq;
1536 sdhci_msm_init_pwr_irq_wait(msm_host);
1537 /* Enable pwr irq interrupts */
1538 writel_relaxed(INT_MASK, msm_host->core_mem + CORE_PWRCTL_MASK);
1540 ret = devm_request_threaded_irq(&pdev->dev, msm_host->pwr_irq, NULL,
1541 sdhci_msm_pwr_irq, IRQF_ONESHOT,
1542 dev_name(&pdev->dev), host);
1544 dev_err(&pdev->dev, "Request IRQ failed (%d)\n", ret);
1548 pm_runtime_get_noresume(&pdev->dev);
1549 pm_runtime_set_active(&pdev->dev);
1550 pm_runtime_enable(&pdev->dev);
1551 pm_runtime_set_autosuspend_delay(&pdev->dev,
1552 MSM_MMC_AUTOSUSPEND_DELAY_MS);
1553 pm_runtime_use_autosuspend(&pdev->dev);
1555 host->mmc_host_ops.execute_tuning = sdhci_msm_execute_tuning;
1556 ret = sdhci_add_host(host);
1558 goto pm_runtime_disable;
1559 sdhci_msm_set_regulator_caps(msm_host);
1561 pm_runtime_mark_last_busy(&pdev->dev);
1562 pm_runtime_put_autosuspend(&pdev->dev);
1567 pm_runtime_disable(&pdev->dev);
1568 pm_runtime_set_suspended(&pdev->dev);
1569 pm_runtime_put_noidle(&pdev->dev);
1571 clk_bulk_disable_unprepare(ARRAY_SIZE(msm_host->bulk_clks),
1572 msm_host->bulk_clks);
1574 if (!IS_ERR(msm_host->bus_clk))
1575 clk_disable_unprepare(msm_host->bus_clk);
1577 sdhci_pltfm_free(pdev);
1581 static int sdhci_msm_remove(struct platform_device *pdev)
1583 struct sdhci_host *host = platform_get_drvdata(pdev);
1584 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1585 struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
1586 int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) ==
1589 sdhci_remove_host(host, dead);
1591 pm_runtime_get_sync(&pdev->dev);
1592 pm_runtime_disable(&pdev->dev);
1593 pm_runtime_put_noidle(&pdev->dev);
1595 clk_bulk_disable_unprepare(ARRAY_SIZE(msm_host->bulk_clks),
1596 msm_host->bulk_clks);
1597 if (!IS_ERR(msm_host->bus_clk))
1598 clk_disable_unprepare(msm_host->bus_clk);
1599 sdhci_pltfm_free(pdev);
1604 static int sdhci_msm_runtime_suspend(struct device *dev)
1606 struct sdhci_host *host = dev_get_drvdata(dev);
1607 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1608 struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
1610 clk_bulk_disable_unprepare(ARRAY_SIZE(msm_host->bulk_clks),
1611 msm_host->bulk_clks);
1616 static int sdhci_msm_runtime_resume(struct device *dev)
1618 struct sdhci_host *host = dev_get_drvdata(dev);
1619 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1620 struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
1622 return clk_bulk_prepare_enable(ARRAY_SIZE(msm_host->bulk_clks),
1623 msm_host->bulk_clks);
1627 static const struct dev_pm_ops sdhci_msm_pm_ops = {
1628 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1629 pm_runtime_force_resume)
1630 SET_RUNTIME_PM_OPS(sdhci_msm_runtime_suspend,
1631 sdhci_msm_runtime_resume,
1635 static struct platform_driver sdhci_msm_driver = {
1636 .probe = sdhci_msm_probe,
1637 .remove = sdhci_msm_remove,
1639 .name = "sdhci_msm",
1640 .of_match_table = sdhci_msm_dt_match,
1641 .pm = &sdhci_msm_pm_ops,
1645 module_platform_driver(sdhci_msm_driver);
1647 MODULE_DESCRIPTION("Qualcomm Secure Digital Host Controller Interface driver");
1648 MODULE_LICENSE("GPL v2");