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