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