Commit | Line | Data |
---|---|---|
0eb0d9f4 GD |
1 | /* |
2 | * drivers/mmc/host/sdhci-msm.c - Qualcomm SDHCI Platform driver | |
3 | * | |
4 | * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved. | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2 and | |
8 | * only version 2 as published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | */ | |
16 | ||
17 | #include <linux/module.h> | |
18 | #include <linux/of_device.h> | |
0eb0d9f4 | 19 | #include <linux/delay.h> |
415b5a75 | 20 | #include <linux/mmc/mmc.h> |
67e6db11 | 21 | #include <linux/pm_runtime.h> |
415b5a75 | 22 | #include <linux/slab.h> |
cc392c58 | 23 | #include <linux/iopoll.h> |
0eb0d9f4 GD |
24 | |
25 | #include "sdhci-pltfm.h" | |
26 | ||
3a3ad3e9 GD |
27 | #define CORE_MCI_VERSION 0x50 |
28 | #define CORE_VERSION_MAJOR_SHIFT 28 | |
29 | #define CORE_VERSION_MAJOR_MASK (0xf << CORE_VERSION_MAJOR_SHIFT) | |
30 | #define CORE_VERSION_MINOR_MASK 0xff | |
31 | ||
0eb0d9f4 GD |
32 | #define CORE_HC_MODE 0x78 |
33 | #define HC_MODE_EN 0x1 | |
34 | #define CORE_POWER 0x0 | |
35 | #define CORE_SW_RST BIT(7) | |
ff06ce41 | 36 | #define FF_CLK_SW_RST_DIS BIT(13) |
0eb0d9f4 | 37 | |
ad81d387 GD |
38 | #define CORE_PWRCTL_STATUS 0xdc |
39 | #define CORE_PWRCTL_MASK 0xe0 | |
40 | #define CORE_PWRCTL_CLEAR 0xe4 | |
41 | #define CORE_PWRCTL_CTL 0xe8 | |
42 | #define CORE_PWRCTL_BUS_OFF BIT(0) | |
43 | #define CORE_PWRCTL_BUS_ON BIT(1) | |
44 | #define CORE_PWRCTL_IO_LOW BIT(2) | |
45 | #define CORE_PWRCTL_IO_HIGH BIT(3) | |
46 | #define CORE_PWRCTL_BUS_SUCCESS BIT(0) | |
47 | #define CORE_PWRCTL_IO_SUCCESS BIT(2) | |
48 | #define REQ_BUS_OFF BIT(0) | |
49 | #define REQ_BUS_ON BIT(1) | |
50 | #define REQ_IO_LOW BIT(2) | |
51 | #define REQ_IO_HIGH BIT(3) | |
52 | #define INT_MASK 0xf | |
415b5a75 GD |
53 | #define MAX_PHASES 16 |
54 | #define CORE_DLL_LOCK BIT(7) | |
02e4293d | 55 | #define CORE_DDR_DLL_LOCK BIT(11) |
415b5a75 GD |
56 | #define CORE_DLL_EN BIT(16) |
57 | #define CORE_CDR_EN BIT(17) | |
58 | #define CORE_CK_OUT_EN BIT(18) | |
59 | #define CORE_CDR_EXT_EN BIT(19) | |
60 | #define CORE_DLL_PDN BIT(29) | |
61 | #define CORE_DLL_RST BIT(30) | |
62 | #define CORE_DLL_CONFIG 0x100 | |
cc392c58 | 63 | #define CORE_CMD_DAT_TRACK_SEL BIT(0) |
415b5a75 GD |
64 | #define CORE_DLL_STATUS 0x108 |
65 | ||
83736352 | 66 | #define CORE_DLL_CONFIG_2 0x1b4 |
02e4293d | 67 | #define CORE_DDR_CAL_EN BIT(0) |
83736352 VG |
68 | #define CORE_FLL_CYCLE_CNT BIT(18) |
69 | #define CORE_DLL_CLOCK_DISABLE BIT(21) | |
70 | ||
415b5a75 | 71 | #define CORE_VENDOR_SPEC 0x10c |
5574ddcc | 72 | #define CORE_VENDOR_SPEC_POR_VAL 0xa1c |
415b5a75 | 73 | #define CORE_CLK_PWRSAVE BIT(1) |
ff06ce41 VG |
74 | #define CORE_HC_MCLK_SEL_DFLT (2 << 8) |
75 | #define CORE_HC_MCLK_SEL_HS400 (3 << 8) | |
76 | #define CORE_HC_MCLK_SEL_MASK (3 << 8) | |
77 | #define CORE_HC_SELECT_IN_EN BIT(18) | |
78 | #define CORE_HC_SELECT_IN_HS400 (6 << 19) | |
79 | #define CORE_HC_SELECT_IN_MASK (7 << 19) | |
415b5a75 | 80 | |
cc392c58 RH |
81 | #define CORE_CSR_CDC_CTLR_CFG0 0x130 |
82 | #define CORE_SW_TRIG_FULL_CALIB BIT(16) | |
83 | #define CORE_HW_AUTOCAL_ENA BIT(17) | |
84 | ||
85 | #define CORE_CSR_CDC_CTLR_CFG1 0x134 | |
86 | #define CORE_CSR_CDC_CAL_TIMER_CFG0 0x138 | |
87 | #define CORE_TIMER_ENA BIT(16) | |
88 | ||
89 | #define CORE_CSR_CDC_CAL_TIMER_CFG1 0x13C | |
90 | #define CORE_CSR_CDC_REFCOUNT_CFG 0x140 | |
91 | #define CORE_CSR_CDC_COARSE_CAL_CFG 0x144 | |
92 | #define CORE_CDC_OFFSET_CFG 0x14C | |
93 | #define CORE_CSR_CDC_DELAY_CFG 0x150 | |
94 | #define CORE_CDC_SLAVE_DDA_CFG 0x160 | |
95 | #define CORE_CSR_CDC_STATUS0 0x164 | |
96 | #define CORE_CALIBRATION_DONE BIT(0) | |
97 | ||
98 | #define CORE_CDC_ERROR_CODE_MASK 0x7000000 | |
99 | ||
100 | #define CORE_CSR_CDC_GEN_CFG 0x178 | |
101 | #define CORE_CDC_SWITCH_BYPASS_OFF BIT(0) | |
102 | #define CORE_CDC_SWITCH_RC_EN BIT(1) | |
103 | ||
104 | #define CORE_DDR_200_CFG 0x184 | |
105 | #define CORE_CDC_T4_DLY_SEL BIT(0) | |
44bf2312 | 106 | #define CORE_CMDIN_RCLK_EN BIT(1) |
cc392c58 | 107 | #define CORE_START_CDC_TRAFFIC BIT(6) |
02e4293d RH |
108 | #define CORE_VENDOR_SPEC3 0x1b0 |
109 | #define CORE_PWRSAVE_DLL BIT(3) | |
110 | ||
111 | #define CORE_DDR_CONFIG 0x1b8 | |
112 | #define DDR_CONFIG_POR_VAL 0x80040853 | |
cc392c58 | 113 | |
3a3ad3e9 GD |
114 | #define CORE_VENDOR_SPEC_CAPABILITIES0 0x11c |
115 | ||
abf270e5 | 116 | #define INVALID_TUNING_PHASE -1 |
80031bde | 117 | #define SDHCI_MSM_MIN_CLOCK 400000 |
ff06ce41 | 118 | #define CORE_FREQ_100MHZ (100 * 1000 * 1000) |
80031bde | 119 | |
415b5a75 GD |
120 | #define CDR_SELEXT_SHIFT 20 |
121 | #define CDR_SELEXT_MASK (0xf << CDR_SELEXT_SHIFT) | |
122 | #define CMUX_SHIFT_PHASE_SHIFT 24 | |
123 | #define CMUX_SHIFT_PHASE_MASK (7 << CMUX_SHIFT_PHASE_SHIFT) | |
124 | ||
67e6db11 | 125 | #define MSM_MMC_AUTOSUSPEND_DELAY_MS 50 |
c0309b38 VV |
126 | |
127 | /* Timeout value to avoid infinite waiting for pwr_irq */ | |
128 | #define MSM_PWR_IRQ_TIMEOUT_MS 5000 | |
129 | ||
0eb0d9f4 GD |
130 | struct sdhci_msm_host { |
131 | struct platform_device *pdev; | |
132 | void __iomem *core_mem; /* MSM SDCC mapped address */ | |
ad81d387 | 133 | int pwr_irq; /* power irq */ |
0eb0d9f4 | 134 | struct clk *bus_clk; /* SDHC bus voter clock */ |
83736352 | 135 | struct clk *xo_clk; /* TCXO clk needed for FLL feature of cm_dll*/ |
4946b3af | 136 | struct clk_bulk_data bulk_clks[4]; /* core, iface, cal, sleep clocks */ |
edc609fd | 137 | unsigned long clk_rate; |
0eb0d9f4 | 138 | struct mmc_host *mmc; |
83736352 | 139 | bool use_14lpp_dll_reset; |
ff06ce41 VG |
140 | bool tuning_done; |
141 | bool calibration_done; | |
abf270e5 | 142 | u8 saved_tuning_phase; |
02e4293d | 143 | bool use_cdclp533; |
c0309b38 VV |
144 | u32 curr_pwr_state; |
145 | u32 curr_io_level; | |
146 | wait_queue_head_t pwr_irq_wait; | |
147 | bool pwr_irq_flag; | |
0eb0d9f4 GD |
148 | }; |
149 | ||
0fb8a3d4 RH |
150 | static unsigned int msm_get_clock_rate_for_bus_mode(struct sdhci_host *host, |
151 | unsigned int clock) | |
152 | { | |
153 | struct mmc_ios ios = host->mmc->ios; | |
154 | /* | |
155 | * The SDHC requires internal clock frequency to be double the | |
156 | * actual clock that will be set for DDR mode. The controller | |
157 | * uses the faster clock(100/400MHz) for some of its parts and | |
158 | * send the actual required clock (50/200MHz) to the card. | |
159 | */ | |
160 | if (ios.timing == MMC_TIMING_UHS_DDR50 || | |
161 | ios.timing == MMC_TIMING_MMC_DDR52 || | |
d7507aa1 RH |
162 | ios.timing == MMC_TIMING_MMC_HS400 || |
163 | host->flags & SDHCI_HS400_TUNING) | |
0fb8a3d4 RH |
164 | clock *= 2; |
165 | return clock; | |
166 | } | |
167 | ||
168 | static void msm_set_clock_rate_for_bus_mode(struct sdhci_host *host, | |
169 | unsigned int clock) | |
170 | { | |
171 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); | |
172 | struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host); | |
173 | struct mmc_ios curr_ios = host->mmc->ios; | |
e4bf91f6 | 174 | struct clk *core_clk = msm_host->bulk_clks[0].clk; |
0fb8a3d4 RH |
175 | int rc; |
176 | ||
177 | clock = msm_get_clock_rate_for_bus_mode(host, clock); | |
e4bf91f6 | 178 | rc = clk_set_rate(core_clk, clock); |
0fb8a3d4 RH |
179 | if (rc) { |
180 | pr_err("%s: Failed to set clock at rate %u at timing %d\n", | |
181 | mmc_hostname(host->mmc), clock, | |
182 | curr_ios.timing); | |
183 | return; | |
184 | } | |
185 | msm_host->clk_rate = clock; | |
186 | pr_debug("%s: Setting clock at rate %lu at timing %d\n", | |
e4bf91f6 | 187 | mmc_hostname(host->mmc), clk_get_rate(core_clk), |
0fb8a3d4 RH |
188 | curr_ios.timing); |
189 | } | |
190 | ||
0eb0d9f4 | 191 | /* Platform specific tuning */ |
415b5a75 GD |
192 | static inline int msm_dll_poll_ck_out_en(struct sdhci_host *host, u8 poll) |
193 | { | |
194 | u32 wait_cnt = 50; | |
195 | u8 ck_out_en; | |
196 | struct mmc_host *mmc = host->mmc; | |
197 | ||
198 | /* Poll for CK_OUT_EN bit. max. poll time = 50us */ | |
199 | ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) & | |
200 | CORE_CK_OUT_EN); | |
201 | ||
202 | while (ck_out_en != poll) { | |
203 | if (--wait_cnt == 0) { | |
204 | dev_err(mmc_dev(mmc), "%s: CK_OUT_EN bit is not %d\n", | |
205 | mmc_hostname(mmc), poll); | |
206 | return -ETIMEDOUT; | |
207 | } | |
208 | udelay(1); | |
209 | ||
210 | ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) & | |
211 | CORE_CK_OUT_EN); | |
212 | } | |
213 | ||
214 | return 0; | |
215 | } | |
216 | ||
217 | static int msm_config_cm_dll_phase(struct sdhci_host *host, u8 phase) | |
218 | { | |
219 | int rc; | |
220 | static const u8 grey_coded_phase_table[] = { | |
221 | 0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4, | |
222 | 0xc, 0xd, 0xf, 0xe, 0xa, 0xb, 0x9, 0x8 | |
223 | }; | |
224 | unsigned long flags; | |
225 | u32 config; | |
226 | struct mmc_host *mmc = host->mmc; | |
227 | ||
abf270e5 RH |
228 | if (phase > 0xf) |
229 | return -EINVAL; | |
230 | ||
415b5a75 GD |
231 | spin_lock_irqsave(&host->lock, flags); |
232 | ||
233 | config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG); | |
234 | config &= ~(CORE_CDR_EN | CORE_CK_OUT_EN); | |
235 | config |= (CORE_CDR_EXT_EN | CORE_DLL_EN); | |
236 | writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG); | |
237 | ||
238 | /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '0' */ | |
239 | rc = msm_dll_poll_ck_out_en(host, 0); | |
240 | if (rc) | |
241 | goto err_out; | |
242 | ||
243 | /* | |
244 | * Write the selected DLL clock output phase (0 ... 15) | |
245 | * to CDR_SELEXT bit field of DLL_CONFIG register. | |
246 | */ | |
247 | config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG); | |
248 | config &= ~CDR_SELEXT_MASK; | |
249 | config |= grey_coded_phase_table[phase] << CDR_SELEXT_SHIFT; | |
250 | writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG); | |
251 | ||
29301f40 RH |
252 | config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG); |
253 | config |= CORE_CK_OUT_EN; | |
254 | writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG); | |
415b5a75 GD |
255 | |
256 | /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '1' */ | |
257 | rc = msm_dll_poll_ck_out_en(host, 1); | |
258 | if (rc) | |
259 | goto err_out; | |
260 | ||
261 | config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG); | |
262 | config |= CORE_CDR_EN; | |
263 | config &= ~CORE_CDR_EXT_EN; | |
264 | writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG); | |
265 | goto out; | |
266 | ||
267 | err_out: | |
268 | dev_err(mmc_dev(mmc), "%s: Failed to set DLL phase: %d\n", | |
269 | mmc_hostname(mmc), phase); | |
270 | out: | |
271 | spin_unlock_irqrestore(&host->lock, flags); | |
272 | return rc; | |
273 | } | |
274 | ||
275 | /* | |
276 | * Find out the greatest range of consecuitive selected | |
277 | * DLL clock output phases that can be used as sampling | |
278 | * setting for SD3.0 UHS-I card read operation (in SDR104 | |
ff06ce41 VG |
279 | * timing mode) or for eMMC4.5 card read operation (in |
280 | * HS400/HS200 timing mode). | |
415b5a75 GD |
281 | * Select the 3/4 of the range and configure the DLL with the |
282 | * selected DLL clock output phase. | |
283 | */ | |
284 | ||
285 | static int msm_find_most_appropriate_phase(struct sdhci_host *host, | |
286 | u8 *phase_table, u8 total_phases) | |
287 | { | |
288 | int ret; | |
289 | u8 ranges[MAX_PHASES][MAX_PHASES] = { {0}, {0} }; | |
290 | u8 phases_per_row[MAX_PHASES] = { 0 }; | |
291 | int row_index = 0, col_index = 0, selected_row_index = 0, curr_max = 0; | |
292 | int i, cnt, phase_0_raw_index = 0, phase_15_raw_index = 0; | |
293 | bool phase_0_found = false, phase_15_found = false; | |
294 | struct mmc_host *mmc = host->mmc; | |
295 | ||
296 | if (!total_phases || (total_phases > MAX_PHASES)) { | |
297 | dev_err(mmc_dev(mmc), "%s: Invalid argument: total_phases=%d\n", | |
298 | mmc_hostname(mmc), total_phases); | |
299 | return -EINVAL; | |
300 | } | |
301 | ||
302 | for (cnt = 0; cnt < total_phases; cnt++) { | |
303 | ranges[row_index][col_index] = phase_table[cnt]; | |
304 | phases_per_row[row_index] += 1; | |
305 | col_index++; | |
306 | ||
307 | if ((cnt + 1) == total_phases) { | |
308 | continue; | |
309 | /* check if next phase in phase_table is consecutive or not */ | |
310 | } else if ((phase_table[cnt] + 1) != phase_table[cnt + 1]) { | |
311 | row_index++; | |
312 | col_index = 0; | |
313 | } | |
314 | } | |
315 | ||
316 | if (row_index >= MAX_PHASES) | |
317 | return -EINVAL; | |
318 | ||
319 | /* Check if phase-0 is present in first valid window? */ | |
320 | if (!ranges[0][0]) { | |
321 | phase_0_found = true; | |
322 | phase_0_raw_index = 0; | |
323 | /* Check if cycle exist between 2 valid windows */ | |
324 | for (cnt = 1; cnt <= row_index; cnt++) { | |
325 | if (phases_per_row[cnt]) { | |
326 | for (i = 0; i < phases_per_row[cnt]; i++) { | |
327 | if (ranges[cnt][i] == 15) { | |
328 | phase_15_found = true; | |
329 | phase_15_raw_index = cnt; | |
330 | break; | |
331 | } | |
332 | } | |
333 | } | |
334 | } | |
335 | } | |
336 | ||
337 | /* If 2 valid windows form cycle then merge them as single window */ | |
338 | if (phase_0_found && phase_15_found) { | |
339 | /* number of phases in raw where phase 0 is present */ | |
340 | u8 phases_0 = phases_per_row[phase_0_raw_index]; | |
341 | /* number of phases in raw where phase 15 is present */ | |
342 | u8 phases_15 = phases_per_row[phase_15_raw_index]; | |
343 | ||
344 | if (phases_0 + phases_15 >= MAX_PHASES) | |
345 | /* | |
346 | * If there are more than 1 phase windows then total | |
347 | * number of phases in both the windows should not be | |
348 | * more than or equal to MAX_PHASES. | |
349 | */ | |
350 | return -EINVAL; | |
351 | ||
352 | /* Merge 2 cyclic windows */ | |
353 | i = phases_15; | |
354 | for (cnt = 0; cnt < phases_0; cnt++) { | |
355 | ranges[phase_15_raw_index][i] = | |
356 | ranges[phase_0_raw_index][cnt]; | |
357 | if (++i >= MAX_PHASES) | |
358 | break; | |
359 | } | |
360 | ||
361 | phases_per_row[phase_0_raw_index] = 0; | |
362 | phases_per_row[phase_15_raw_index] = phases_15 + phases_0; | |
363 | } | |
364 | ||
365 | for (cnt = 0; cnt <= row_index; cnt++) { | |
366 | if (phases_per_row[cnt] > curr_max) { | |
367 | curr_max = phases_per_row[cnt]; | |
368 | selected_row_index = cnt; | |
369 | } | |
370 | } | |
371 | ||
372 | i = (curr_max * 3) / 4; | |
373 | if (i) | |
374 | i--; | |
375 | ||
376 | ret = ranges[selected_row_index][i]; | |
377 | ||
378 | if (ret >= MAX_PHASES) { | |
379 | ret = -EINVAL; | |
380 | dev_err(mmc_dev(mmc), "%s: Invalid phase selected=%d\n", | |
381 | mmc_hostname(mmc), ret); | |
382 | } | |
383 | ||
384 | return ret; | |
385 | } | |
386 | ||
387 | static inline void msm_cm_dll_set_freq(struct sdhci_host *host) | |
0eb0d9f4 | 388 | { |
415b5a75 GD |
389 | u32 mclk_freq = 0, config; |
390 | ||
391 | /* Program the MCLK value to MCLK_FREQ bit field */ | |
392 | if (host->clock <= 112000000) | |
393 | mclk_freq = 0; | |
394 | else if (host->clock <= 125000000) | |
395 | mclk_freq = 1; | |
396 | else if (host->clock <= 137000000) | |
397 | mclk_freq = 2; | |
398 | else if (host->clock <= 150000000) | |
399 | mclk_freq = 3; | |
400 | else if (host->clock <= 162000000) | |
401 | mclk_freq = 4; | |
402 | else if (host->clock <= 175000000) | |
403 | mclk_freq = 5; | |
404 | else if (host->clock <= 187000000) | |
405 | mclk_freq = 6; | |
406 | else if (host->clock <= 200000000) | |
407 | mclk_freq = 7; | |
408 | ||
409 | config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG); | |
410 | config &= ~CMUX_SHIFT_PHASE_MASK; | |
411 | config |= mclk_freq << CMUX_SHIFT_PHASE_SHIFT; | |
412 | writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG); | |
413 | } | |
414 | ||
415 | /* Initialize the DLL (Programmable Delay Line) */ | |
416 | static int msm_init_cm_dll(struct sdhci_host *host) | |
417 | { | |
418 | struct mmc_host *mmc = host->mmc; | |
83736352 VG |
419 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
420 | struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host); | |
415b5a75 GD |
421 | int wait_cnt = 50; |
422 | unsigned long flags; | |
29301f40 | 423 | u32 config; |
415b5a75 GD |
424 | |
425 | spin_lock_irqsave(&host->lock, flags); | |
426 | ||
0eb0d9f4 | 427 | /* |
415b5a75 GD |
428 | * Make sure that clock is always enabled when DLL |
429 | * tuning is in progress. Keeping PWRSAVE ON may | |
430 | * turn off the clock. | |
0eb0d9f4 | 431 | */ |
29301f40 RH |
432 | config = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC); |
433 | config &= ~CORE_CLK_PWRSAVE; | |
434 | writel_relaxed(config, host->ioaddr + CORE_VENDOR_SPEC); | |
415b5a75 | 435 | |
83736352 VG |
436 | if (msm_host->use_14lpp_dll_reset) { |
437 | config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG); | |
438 | config &= ~CORE_CK_OUT_EN; | |
439 | writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG); | |
440 | ||
441 | config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2); | |
442 | config |= CORE_DLL_CLOCK_DISABLE; | |
443 | writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG_2); | |
444 | } | |
445 | ||
29301f40 RH |
446 | config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG); |
447 | config |= CORE_DLL_RST; | |
448 | writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG); | |
415b5a75 | 449 | |
29301f40 RH |
450 | config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG); |
451 | config |= CORE_DLL_PDN; | |
452 | writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG); | |
415b5a75 GD |
453 | msm_cm_dll_set_freq(host); |
454 | ||
83736352 VG |
455 | if (msm_host->use_14lpp_dll_reset && |
456 | !IS_ERR_OR_NULL(msm_host->xo_clk)) { | |
457 | u32 mclk_freq = 0; | |
458 | ||
459 | config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2); | |
460 | config &= CORE_FLL_CYCLE_CNT; | |
461 | if (config) | |
462 | mclk_freq = DIV_ROUND_CLOSEST_ULL((host->clock * 8), | |
463 | clk_get_rate(msm_host->xo_clk)); | |
464 | else | |
465 | mclk_freq = DIV_ROUND_CLOSEST_ULL((host->clock * 4), | |
466 | clk_get_rate(msm_host->xo_clk)); | |
467 | ||
468 | config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2); | |
469 | config &= ~(0xFF << 10); | |
470 | config |= mclk_freq << 10; | |
471 | ||
472 | writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG_2); | |
473 | /* wait for 5us before enabling DLL clock */ | |
474 | udelay(5); | |
475 | } | |
476 | ||
29301f40 RH |
477 | config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG); |
478 | config &= ~CORE_DLL_RST; | |
479 | writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG); | |
415b5a75 | 480 | |
29301f40 RH |
481 | config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG); |
482 | config &= ~CORE_DLL_PDN; | |
483 | writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG); | |
415b5a75 | 484 | |
83736352 VG |
485 | if (msm_host->use_14lpp_dll_reset) { |
486 | msm_cm_dll_set_freq(host); | |
487 | config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2); | |
488 | config &= ~CORE_DLL_CLOCK_DISABLE; | |
489 | writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG_2); | |
490 | } | |
491 | ||
29301f40 RH |
492 | config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG); |
493 | config |= CORE_DLL_EN; | |
494 | writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG); | |
415b5a75 | 495 | |
29301f40 RH |
496 | config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG); |
497 | config |= CORE_CK_OUT_EN; | |
498 | writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG); | |
415b5a75 GD |
499 | |
500 | /* Wait until DLL_LOCK bit of DLL_STATUS register becomes '1' */ | |
501 | while (!(readl_relaxed(host->ioaddr + CORE_DLL_STATUS) & | |
502 | CORE_DLL_LOCK)) { | |
503 | /* max. wait for 50us sec for LOCK bit to be set */ | |
504 | if (--wait_cnt == 0) { | |
505 | dev_err(mmc_dev(mmc), "%s: DLL failed to LOCK\n", | |
506 | mmc_hostname(mmc)); | |
507 | spin_unlock_irqrestore(&host->lock, flags); | |
508 | return -ETIMEDOUT; | |
509 | } | |
510 | udelay(1); | |
511 | } | |
512 | ||
513 | spin_unlock_irqrestore(&host->lock, flags); | |
0eb0d9f4 GD |
514 | return 0; |
515 | } | |
516 | ||
b54aaa8a RH |
517 | static void msm_hc_select_default(struct sdhci_host *host) |
518 | { | |
519 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); | |
520 | struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host); | |
521 | u32 config; | |
522 | ||
523 | if (!msm_host->use_cdclp533) { | |
524 | config = readl_relaxed(host->ioaddr + | |
525 | CORE_VENDOR_SPEC3); | |
526 | config &= ~CORE_PWRSAVE_DLL; | |
527 | writel_relaxed(config, host->ioaddr + | |
528 | CORE_VENDOR_SPEC3); | |
529 | } | |
530 | ||
531 | config = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC); | |
532 | config &= ~CORE_HC_MCLK_SEL_MASK; | |
533 | config |= CORE_HC_MCLK_SEL_DFLT; | |
534 | writel_relaxed(config, host->ioaddr + CORE_VENDOR_SPEC); | |
535 | ||
536 | /* | |
537 | * Disable HC_SELECT_IN to be able to use the UHS mode select | |
538 | * configuration from Host Control2 register for all other | |
539 | * modes. | |
540 | * Write 0 to HC_SELECT_IN and HC_SELECT_IN_EN field | |
541 | * in VENDOR_SPEC_FUNC | |
542 | */ | |
543 | config = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC); | |
544 | config &= ~CORE_HC_SELECT_IN_EN; | |
545 | config &= ~CORE_HC_SELECT_IN_MASK; | |
546 | writel_relaxed(config, host->ioaddr + CORE_VENDOR_SPEC); | |
547 | ||
548 | /* | |
549 | * Make sure above writes impacting free running MCLK are completed | |
550 | * before changing the clk_rate at GCC. | |
551 | */ | |
552 | wmb(); | |
553 | } | |
554 | ||
555 | static void msm_hc_select_hs400(struct sdhci_host *host) | |
556 | { | |
557 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); | |
558 | struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host); | |
44bf2312 | 559 | struct mmc_ios ios = host->mmc->ios; |
b54aaa8a RH |
560 | u32 config, dll_lock; |
561 | int rc; | |
562 | ||
563 | /* Select the divided clock (free running MCLK/2) */ | |
564 | config = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC); | |
565 | config &= ~CORE_HC_MCLK_SEL_MASK; | |
566 | config |= CORE_HC_MCLK_SEL_HS400; | |
567 | ||
568 | writel_relaxed(config, host->ioaddr + CORE_VENDOR_SPEC); | |
569 | /* | |
570 | * Select HS400 mode using the HC_SELECT_IN from VENDOR SPEC | |
571 | * register | |
572 | */ | |
44bf2312 RH |
573 | if ((msm_host->tuning_done || ios.enhanced_strobe) && |
574 | !msm_host->calibration_done) { | |
b54aaa8a RH |
575 | config = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC); |
576 | config |= CORE_HC_SELECT_IN_HS400; | |
577 | config |= CORE_HC_SELECT_IN_EN; | |
578 | writel_relaxed(config, host->ioaddr + CORE_VENDOR_SPEC); | |
579 | } | |
580 | if (!msm_host->clk_rate && !msm_host->use_cdclp533) { | |
581 | /* | |
582 | * Poll on DLL_LOCK or DDR_DLL_LOCK bits in | |
583 | * CORE_DLL_STATUS to be set. This should get set | |
584 | * within 15 us at 200 MHz. | |
585 | */ | |
586 | rc = readl_relaxed_poll_timeout(host->ioaddr + | |
587 | CORE_DLL_STATUS, | |
588 | dll_lock, | |
589 | (dll_lock & | |
590 | (CORE_DLL_LOCK | | |
591 | CORE_DDR_DLL_LOCK)), 10, | |
592 | 1000); | |
593 | if (rc == -ETIMEDOUT) | |
594 | pr_err("%s: Unable to get DLL_LOCK/DDR_DLL_LOCK, dll_status: 0x%08x\n", | |
595 | mmc_hostname(host->mmc), dll_lock); | |
596 | } | |
597 | /* | |
598 | * Make sure above writes impacting free running MCLK are completed | |
599 | * before changing the clk_rate at GCC. | |
600 | */ | |
601 | wmb(); | |
602 | } | |
603 | ||
604 | /* | |
605 | * sdhci_msm_hc_select_mode :- In general all timing modes are | |
606 | * controlled via UHS mode select in Host Control2 register. | |
607 | * eMMC specific HS200/HS400 doesn't have their respective modes | |
608 | * defined here, hence we use these values. | |
609 | * | |
610 | * HS200 - SDR104 (Since they both are equivalent in functionality) | |
611 | * HS400 - This involves multiple configurations | |
612 | * Initially SDR104 - when tuning is required as HS200 | |
613 | * Then when switching to DDR @ 400MHz (HS400) we use | |
614 | * the vendor specific HC_SELECT_IN to control the mode. | |
615 | * | |
616 | * In addition to controlling the modes we also need to select the | |
617 | * correct input clock for DLL depending on the mode. | |
618 | * | |
619 | * HS400 - divided clock (free running MCLK/2) | |
620 | * All other modes - default (free running MCLK) | |
621 | */ | |
30de038d | 622 | static void sdhci_msm_hc_select_mode(struct sdhci_host *host) |
b54aaa8a RH |
623 | { |
624 | struct mmc_ios ios = host->mmc->ios; | |
625 | ||
d7507aa1 RH |
626 | if (ios.timing == MMC_TIMING_MMC_HS400 || |
627 | host->flags & SDHCI_HS400_TUNING) | |
b54aaa8a RH |
628 | msm_hc_select_hs400(host); |
629 | else | |
630 | msm_hc_select_default(host); | |
631 | } | |
632 | ||
cc392c58 RH |
633 | static int sdhci_msm_cdclp533_calibration(struct sdhci_host *host) |
634 | { | |
635 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); | |
636 | struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host); | |
637 | u32 config, calib_done; | |
638 | int ret; | |
639 | ||
640 | pr_debug("%s: %s: Enter\n", mmc_hostname(host->mmc), __func__); | |
641 | ||
642 | /* | |
643 | * Retuning in HS400 (DDR mode) will fail, just reset the | |
644 | * tuning block and restore the saved tuning phase. | |
645 | */ | |
646 | ret = msm_init_cm_dll(host); | |
647 | if (ret) | |
648 | goto out; | |
649 | ||
650 | /* Set the selected phase in delay line hw block */ | |
651 | ret = msm_config_cm_dll_phase(host, msm_host->saved_tuning_phase); | |
652 | if (ret) | |
653 | goto out; | |
654 | ||
655 | config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG); | |
656 | config |= CORE_CMD_DAT_TRACK_SEL; | |
657 | writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG); | |
658 | ||
659 | config = readl_relaxed(host->ioaddr + CORE_DDR_200_CFG); | |
660 | config &= ~CORE_CDC_T4_DLY_SEL; | |
661 | writel_relaxed(config, host->ioaddr + CORE_DDR_200_CFG); | |
662 | ||
663 | config = readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG); | |
664 | config &= ~CORE_CDC_SWITCH_BYPASS_OFF; | |
665 | writel_relaxed(config, host->ioaddr + CORE_CSR_CDC_GEN_CFG); | |
666 | ||
667 | config = readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG); | |
668 | config |= CORE_CDC_SWITCH_RC_EN; | |
669 | writel_relaxed(config, host->ioaddr + CORE_CSR_CDC_GEN_CFG); | |
670 | ||
671 | config = readl_relaxed(host->ioaddr + CORE_DDR_200_CFG); | |
672 | config &= ~CORE_START_CDC_TRAFFIC; | |
673 | writel_relaxed(config, host->ioaddr + CORE_DDR_200_CFG); | |
674 | ||
543c576d | 675 | /* Perform CDC Register Initialization Sequence */ |
cc392c58 RH |
676 | |
677 | writel_relaxed(0x11800EC, host->ioaddr + CORE_CSR_CDC_CTLR_CFG0); | |
678 | writel_relaxed(0x3011111, host->ioaddr + CORE_CSR_CDC_CTLR_CFG1); | |
679 | writel_relaxed(0x1201000, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0); | |
680 | writel_relaxed(0x4, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG1); | |
681 | writel_relaxed(0xCB732020, host->ioaddr + CORE_CSR_CDC_REFCOUNT_CFG); | |
682 | writel_relaxed(0xB19, host->ioaddr + CORE_CSR_CDC_COARSE_CAL_CFG); | |
083c9aa0 | 683 | writel_relaxed(0x4E2, host->ioaddr + CORE_CSR_CDC_DELAY_CFG); |
cc392c58 RH |
684 | writel_relaxed(0x0, host->ioaddr + CORE_CDC_OFFSET_CFG); |
685 | writel_relaxed(0x16334, host->ioaddr + CORE_CDC_SLAVE_DDA_CFG); | |
686 | ||
687 | /* CDC HW Calibration */ | |
688 | ||
689 | config = readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0); | |
690 | config |= CORE_SW_TRIG_FULL_CALIB; | |
691 | writel_relaxed(config, host->ioaddr + CORE_CSR_CDC_CTLR_CFG0); | |
692 | ||
693 | config = readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0); | |
694 | config &= ~CORE_SW_TRIG_FULL_CALIB; | |
695 | writel_relaxed(config, host->ioaddr + CORE_CSR_CDC_CTLR_CFG0); | |
696 | ||
697 | config = readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0); | |
698 | config |= CORE_HW_AUTOCAL_ENA; | |
699 | writel_relaxed(config, host->ioaddr + CORE_CSR_CDC_CTLR_CFG0); | |
700 | ||
701 | config = readl_relaxed(host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0); | |
702 | config |= CORE_TIMER_ENA; | |
703 | writel_relaxed(config, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0); | |
704 | ||
705 | ret = readl_relaxed_poll_timeout(host->ioaddr + CORE_CSR_CDC_STATUS0, | |
706 | calib_done, | |
707 | (calib_done & CORE_CALIBRATION_DONE), | |
708 | 1, 50); | |
709 | ||
710 | if (ret == -ETIMEDOUT) { | |
711 | pr_err("%s: %s: CDC calibration was not completed\n", | |
712 | mmc_hostname(host->mmc), __func__); | |
713 | goto out; | |
714 | } | |
715 | ||
716 | ret = readl_relaxed(host->ioaddr + CORE_CSR_CDC_STATUS0) | |
717 | & CORE_CDC_ERROR_CODE_MASK; | |
718 | if (ret) { | |
719 | pr_err("%s: %s: CDC error code %d\n", | |
720 | mmc_hostname(host->mmc), __func__, ret); | |
721 | ret = -EINVAL; | |
722 | goto out; | |
723 | } | |
724 | ||
725 | config = readl_relaxed(host->ioaddr + CORE_DDR_200_CFG); | |
726 | config |= CORE_START_CDC_TRAFFIC; | |
727 | writel_relaxed(config, host->ioaddr + CORE_DDR_200_CFG); | |
728 | out: | |
729 | pr_debug("%s: %s: Exit, ret %d\n", mmc_hostname(host->mmc), | |
730 | __func__, ret); | |
731 | return ret; | |
732 | } | |
733 | ||
02e4293d RH |
734 | static int sdhci_msm_cm_dll_sdc4_calibration(struct sdhci_host *host) |
735 | { | |
44bf2312 | 736 | struct mmc_host *mmc = host->mmc; |
02e4293d RH |
737 | u32 dll_status, config; |
738 | int ret; | |
739 | ||
740 | pr_debug("%s: %s: Enter\n", mmc_hostname(host->mmc), __func__); | |
741 | ||
742 | /* | |
743 | * Currently the CORE_DDR_CONFIG register defaults to desired | |
744 | * configuration on reset. Currently reprogramming the power on | |
745 | * reset (POR) value in case it might have been modified by | |
746 | * bootloaders. In the future, if this changes, then the desired | |
747 | * values will need to be programmed appropriately. | |
748 | */ | |
749 | writel_relaxed(DDR_CONFIG_POR_VAL, host->ioaddr + CORE_DDR_CONFIG); | |
750 | ||
44bf2312 RH |
751 | if (mmc->ios.enhanced_strobe) { |
752 | config = readl_relaxed(host->ioaddr + CORE_DDR_200_CFG); | |
753 | config |= CORE_CMDIN_RCLK_EN; | |
754 | writel_relaxed(config, host->ioaddr + CORE_DDR_200_CFG); | |
755 | } | |
756 | ||
02e4293d RH |
757 | config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2); |
758 | config |= CORE_DDR_CAL_EN; | |
759 | writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG_2); | |
760 | ||
761 | ret = readl_relaxed_poll_timeout(host->ioaddr + CORE_DLL_STATUS, | |
762 | dll_status, | |
763 | (dll_status & CORE_DDR_DLL_LOCK), | |
764 | 10, 1000); | |
765 | ||
766 | if (ret == -ETIMEDOUT) { | |
767 | pr_err("%s: %s: CM_DLL_SDC4 calibration was not completed\n", | |
768 | mmc_hostname(host->mmc), __func__); | |
769 | goto out; | |
770 | } | |
771 | ||
772 | config = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC3); | |
773 | config |= CORE_PWRSAVE_DLL; | |
774 | writel_relaxed(config, host->ioaddr + CORE_VENDOR_SPEC3); | |
775 | ||
776 | /* | |
777 | * Drain writebuffer to ensure above DLL calibration | |
778 | * and PWRSAVE DLL is enabled. | |
779 | */ | |
780 | wmb(); | |
781 | out: | |
782 | pr_debug("%s: %s: Exit, ret %d\n", mmc_hostname(host->mmc), | |
783 | __func__, ret); | |
784 | return ret; | |
785 | } | |
786 | ||
787 | static int sdhci_msm_hs400_dll_calibration(struct sdhci_host *host) | |
788 | { | |
789 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); | |
790 | struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host); | |
44bf2312 | 791 | struct mmc_host *mmc = host->mmc; |
02e4293d RH |
792 | int ret; |
793 | u32 config; | |
794 | ||
795 | pr_debug("%s: %s: Enter\n", mmc_hostname(host->mmc), __func__); | |
796 | ||
797 | /* | |
798 | * Retuning in HS400 (DDR mode) will fail, just reset the | |
799 | * tuning block and restore the saved tuning phase. | |
800 | */ | |
801 | ret = msm_init_cm_dll(host); | |
802 | if (ret) | |
803 | goto out; | |
804 | ||
44bf2312 RH |
805 | if (!mmc->ios.enhanced_strobe) { |
806 | /* Set the selected phase in delay line hw block */ | |
807 | ret = msm_config_cm_dll_phase(host, | |
808 | msm_host->saved_tuning_phase); | |
809 | if (ret) | |
810 | goto out; | |
811 | config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG); | |
812 | config |= CORE_CMD_DAT_TRACK_SEL; | |
813 | writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG); | |
814 | } | |
02e4293d | 815 | |
02e4293d RH |
816 | if (msm_host->use_cdclp533) |
817 | ret = sdhci_msm_cdclp533_calibration(host); | |
818 | else | |
819 | ret = sdhci_msm_cm_dll_sdc4_calibration(host); | |
820 | out: | |
821 | pr_debug("%s: %s: Exit, ret %d\n", mmc_hostname(host->mmc), | |
822 | __func__, ret); | |
823 | return ret; | |
824 | } | |
825 | ||
4436c535 | 826 | static int sdhci_msm_execute_tuning(struct mmc_host *mmc, u32 opcode) |
415b5a75 | 827 | { |
4436c535 | 828 | struct sdhci_host *host = mmc_priv(mmc); |
415b5a75 | 829 | int tuning_seq_cnt = 3; |
33d73935 | 830 | u8 phase, tuned_phases[16], tuned_phase_cnt = 0; |
415b5a75 | 831 | int rc; |
415b5a75 | 832 | struct mmc_ios ios = host->mmc->ios; |
abf270e5 RH |
833 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
834 | struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host); | |
415b5a75 GD |
835 | |
836 | /* | |
837 | * Tuning is required for SDR104, HS200 and HS400 cards and | |
838 | * if clock frequency is greater than 100MHz in these modes. | |
839 | */ | |
ff06ce41 VG |
840 | if (host->clock <= CORE_FREQ_100MHZ || |
841 | !(ios.timing == MMC_TIMING_MMC_HS400 || | |
842 | ios.timing == MMC_TIMING_MMC_HS200 || | |
843 | ios.timing == MMC_TIMING_UHS_SDR104)) | |
415b5a75 GD |
844 | return 0; |
845 | ||
d7507aa1 RH |
846 | /* |
847 | * For HS400 tuning in HS200 timing requires: | |
848 | * - select MCLK/2 in VENDOR_SPEC | |
849 | * - program MCLK to 400MHz (or nearest supported) in GCC | |
850 | */ | |
851 | if (host->flags & SDHCI_HS400_TUNING) { | |
852 | sdhci_msm_hc_select_mode(host); | |
853 | msm_set_clock_rate_for_bus_mode(host, ios.clock); | |
4436c535 | 854 | host->flags &= ~SDHCI_HS400_TUNING; |
d7507aa1 RH |
855 | } |
856 | ||
415b5a75 GD |
857 | retry: |
858 | /* First of all reset the tuning block */ | |
859 | rc = msm_init_cm_dll(host); | |
860 | if (rc) | |
33d73935 | 861 | return rc; |
415b5a75 GD |
862 | |
863 | phase = 0; | |
864 | do { | |
415b5a75 GD |
865 | /* Set the phase in delay line hw block */ |
866 | rc = msm_config_cm_dll_phase(host, phase); | |
867 | if (rc) | |
33d73935 | 868 | return rc; |
415b5a75 | 869 | |
abf270e5 | 870 | msm_host->saved_tuning_phase = phase; |
9979dbe5 | 871 | rc = mmc_send_tuning(mmc, opcode, NULL); |
33d73935 | 872 | if (!rc) { |
415b5a75 GD |
873 | /* Tuning is successful at this tuning point */ |
874 | tuned_phases[tuned_phase_cnt++] = phase; | |
875 | dev_dbg(mmc_dev(mmc), "%s: Found good phase = %d\n", | |
876 | mmc_hostname(mmc), phase); | |
877 | } | |
878 | } while (++phase < ARRAY_SIZE(tuned_phases)); | |
879 | ||
880 | if (tuned_phase_cnt) { | |
881 | rc = msm_find_most_appropriate_phase(host, tuned_phases, | |
882 | tuned_phase_cnt); | |
883 | if (rc < 0) | |
33d73935 | 884 | return rc; |
415b5a75 GD |
885 | else |
886 | phase = rc; | |
887 | ||
888 | /* | |
889 | * Finally set the selected phase in delay | |
890 | * line hw block. | |
891 | */ | |
892 | rc = msm_config_cm_dll_phase(host, phase); | |
893 | if (rc) | |
33d73935 | 894 | return rc; |
415b5a75 GD |
895 | dev_dbg(mmc_dev(mmc), "%s: Setting the tuning phase to %d\n", |
896 | mmc_hostname(mmc), phase); | |
897 | } else { | |
898 | if (--tuning_seq_cnt) | |
899 | goto retry; | |
900 | /* Tuning failed */ | |
901 | dev_dbg(mmc_dev(mmc), "%s: No tuning point found\n", | |
902 | mmc_hostname(mmc)); | |
903 | rc = -EIO; | |
904 | } | |
905 | ||
ff06ce41 VG |
906 | if (!rc) |
907 | msm_host->tuning_done = true; | |
415b5a75 GD |
908 | return rc; |
909 | } | |
910 | ||
db9bd163 RH |
911 | /* |
912 | * sdhci_msm_hs400 - Calibrate the DLL for HS400 bus speed mode operation. | |
44bf2312 | 913 | * This needs to be done for both tuning and enhanced_strobe mode. |
db9bd163 RH |
914 | * DLL operation is only needed for clock > 100MHz. For clock <= 100MHz |
915 | * fixed feedback clock is used. | |
916 | */ | |
917 | static void sdhci_msm_hs400(struct sdhci_host *host, struct mmc_ios *ios) | |
918 | { | |
919 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); | |
920 | struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host); | |
921 | int ret; | |
922 | ||
923 | if (host->clock > CORE_FREQ_100MHZ && | |
44bf2312 RH |
924 | (msm_host->tuning_done || ios->enhanced_strobe) && |
925 | !msm_host->calibration_done) { | |
db9bd163 RH |
926 | ret = sdhci_msm_hs400_dll_calibration(host); |
927 | if (!ret) | |
928 | msm_host->calibration_done = true; | |
929 | else | |
930 | pr_err("%s: Failed to calibrate DLL for hs400 mode (%d)\n", | |
931 | mmc_hostname(host->mmc), ret); | |
932 | } | |
933 | } | |
934 | ||
ee320674 RH |
935 | static void sdhci_msm_set_uhs_signaling(struct sdhci_host *host, |
936 | unsigned int uhs) | |
937 | { | |
938 | struct mmc_host *mmc = host->mmc; | |
ff06ce41 VG |
939 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
940 | struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host); | |
ee320674 | 941 | u16 ctrl_2; |
ff06ce41 | 942 | u32 config; |
ee320674 RH |
943 | |
944 | ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2); | |
945 | /* Select Bus Speed Mode for host */ | |
946 | ctrl_2 &= ~SDHCI_CTRL_UHS_MASK; | |
947 | switch (uhs) { | |
948 | case MMC_TIMING_UHS_SDR12: | |
949 | ctrl_2 |= SDHCI_CTRL_UHS_SDR12; | |
950 | break; | |
951 | case MMC_TIMING_UHS_SDR25: | |
952 | ctrl_2 |= SDHCI_CTRL_UHS_SDR25; | |
953 | break; | |
954 | case MMC_TIMING_UHS_SDR50: | |
955 | ctrl_2 |= SDHCI_CTRL_UHS_SDR50; | |
956 | break; | |
ff06ce41 | 957 | case MMC_TIMING_MMC_HS400: |
ee320674 RH |
958 | case MMC_TIMING_MMC_HS200: |
959 | case MMC_TIMING_UHS_SDR104: | |
960 | ctrl_2 |= SDHCI_CTRL_UHS_SDR104; | |
961 | break; | |
962 | case MMC_TIMING_UHS_DDR50: | |
963 | case MMC_TIMING_MMC_DDR52: | |
964 | ctrl_2 |= SDHCI_CTRL_UHS_DDR50; | |
965 | break; | |
966 | } | |
967 | ||
968 | /* | |
969 | * When clock frequency is less than 100MHz, the feedback clock must be | |
970 | * provided and DLL must not be used so that tuning can be skipped. To | |
971 | * provide feedback clock, the mode selection can be any value less | |
972 | * than 3'b011 in bits [2:0] of HOST CONTROL2 register. | |
973 | */ | |
ff06ce41 VG |
974 | if (host->clock <= CORE_FREQ_100MHZ) { |
975 | if (uhs == MMC_TIMING_MMC_HS400 || | |
976 | uhs == MMC_TIMING_MMC_HS200 || | |
977 | uhs == MMC_TIMING_UHS_SDR104) | |
978 | ctrl_2 &= ~SDHCI_CTRL_UHS_MASK; | |
979 | /* | |
980 | * DLL is not required for clock <= 100MHz | |
981 | * Thus, make sure DLL it is disabled when not required | |
982 | */ | |
983 | config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG); | |
984 | config |= CORE_DLL_RST; | |
985 | writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG); | |
986 | ||
987 | config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG); | |
988 | config |= CORE_DLL_PDN; | |
989 | writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG); | |
990 | ||
991 | /* | |
992 | * The DLL needs to be restored and CDCLP533 recalibrated | |
993 | * when the clock frequency is set back to 400MHz. | |
994 | */ | |
995 | msm_host->calibration_done = false; | |
996 | } | |
ee320674 RH |
997 | |
998 | dev_dbg(mmc_dev(mmc), "%s: clock=%u uhs=%u ctrl_2=0x%x\n", | |
999 | mmc_hostname(host->mmc), host->clock, uhs, ctrl_2); | |
1000 | sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2); | |
cc392c58 | 1001 | |
db9bd163 RH |
1002 | if (mmc->ios.timing == MMC_TIMING_MMC_HS400) |
1003 | sdhci_msm_hs400(host, &mmc->ios); | |
ee320674 RH |
1004 | } |
1005 | ||
c0309b38 VV |
1006 | static inline void sdhci_msm_init_pwr_irq_wait(struct sdhci_msm_host *msm_host) |
1007 | { | |
1008 | init_waitqueue_head(&msm_host->pwr_irq_wait); | |
1009 | } | |
1010 | ||
1011 | static inline void sdhci_msm_complete_pwr_irq_wait( | |
1012 | struct sdhci_msm_host *msm_host) | |
1013 | { | |
1014 | wake_up(&msm_host->pwr_irq_wait); | |
1015 | } | |
1016 | ||
1017 | /* | |
1018 | * sdhci_msm_check_power_status API should be called when registers writes | |
1019 | * which can toggle sdhci IO bus ON/OFF or change IO lines HIGH/LOW happens. | |
1020 | * To what state the register writes will change the IO lines should be passed | |
1021 | * as the argument req_type. This API will check whether the IO line's state | |
1022 | * is already the expected state and will wait for power irq only if | |
1023 | * power irq is expected to be trigerred based on the current IO line state | |
1024 | * and expected IO line state. | |
1025 | */ | |
1026 | static void sdhci_msm_check_power_status(struct sdhci_host *host, u32 req_type) | |
1027 | { | |
1028 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); | |
1029 | struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host); | |
1030 | bool done = false; | |
1031 | ||
1032 | pr_debug("%s: %s: request %d curr_pwr_state %x curr_io_level %x\n", | |
1033 | mmc_hostname(host->mmc), __func__, req_type, | |
1034 | msm_host->curr_pwr_state, msm_host->curr_io_level); | |
1035 | ||
1036 | /* | |
1037 | * The IRQ for request type IO High/LOW will be generated when - | |
1038 | * there is a state change in 1.8V enable bit (bit 3) of | |
1039 | * SDHCI_HOST_CONTROL2 register. The reset state of that bit is 0 | |
1040 | * which indicates 3.3V IO voltage. So, when MMC core layer tries | |
1041 | * to set it to 3.3V before card detection happens, the | |
1042 | * IRQ doesn't get triggered as there is no state change in this bit. | |
1043 | * The driver already handles this case by changing the IO voltage | |
1044 | * level to high as part of controller power up sequence. Hence, check | |
1045 | * for host->pwr to handle a case where IO voltage high request is | |
1046 | * issued even before controller power up. | |
1047 | */ | |
1048 | if ((req_type & REQ_IO_HIGH) && !host->pwr) { | |
1049 | pr_debug("%s: do not wait for power IRQ that never comes, req_type: %d\n", | |
1050 | mmc_hostname(host->mmc), req_type); | |
1051 | return; | |
1052 | } | |
1053 | if ((req_type & msm_host->curr_pwr_state) || | |
1054 | (req_type & msm_host->curr_io_level)) | |
1055 | done = true; | |
1056 | /* | |
1057 | * This is needed here to handle cases where register writes will | |
1058 | * not change the current bus state or io level of the controller. | |
1059 | * In this case, no power irq will be triggerred and we should | |
1060 | * not wait. | |
1061 | */ | |
1062 | if (!done) { | |
1063 | if (!wait_event_timeout(msm_host->pwr_irq_wait, | |
1064 | msm_host->pwr_irq_flag, | |
1065 | msecs_to_jiffies(MSM_PWR_IRQ_TIMEOUT_MS))) | |
1066 | __WARN_printf("%s: pwr_irq for req: (%d) timed out\n", | |
1067 | mmc_hostname(host->mmc), req_type); | |
1068 | } | |
1069 | pr_debug("%s: %s: request %d done\n", mmc_hostname(host->mmc), | |
1070 | __func__, req_type); | |
1071 | } | |
1072 | ||
401b2d06 ST |
1073 | static void sdhci_msm_dump_pwr_ctrl_regs(struct sdhci_host *host) |
1074 | { | |
1075 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); | |
1076 | struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host); | |
1077 | ||
1078 | pr_err("%s: PWRCTL_STATUS: 0x%08x | PWRCTL_MASK: 0x%08x | PWRCTL_CTL: 0x%08x\n", | |
1079 | mmc_hostname(host->mmc), | |
1080 | readl_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS), | |
1081 | readl_relaxed(msm_host->core_mem + CORE_PWRCTL_MASK), | |
1082 | readl_relaxed(msm_host->core_mem + CORE_PWRCTL_CTL)); | |
1083 | } | |
1084 | ||
1085 | static void sdhci_msm_handle_pwr_irq(struct sdhci_host *host, int irq) | |
ad81d387 GD |
1086 | { |
1087 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); | |
1088 | struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host); | |
1089 | u32 irq_status, irq_ack = 0; | |
401b2d06 | 1090 | int retry = 10; |
c0309b38 VV |
1091 | int pwr_state = 0, io_level = 0; |
1092 | ||
ad81d387 GD |
1093 | |
1094 | irq_status = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS); | |
1095 | irq_status &= INT_MASK; | |
1096 | ||
1097 | writel_relaxed(irq_status, msm_host->core_mem + CORE_PWRCTL_CLEAR); | |
1098 | ||
401b2d06 ST |
1099 | /* |
1100 | * There is a rare HW scenario where the first clear pulse could be | |
1101 | * lost when actual reset and clear/read of status register is | |
1102 | * happening at a time. Hence, retry for at least 10 times to make | |
1103 | * sure status register is cleared. Otherwise, this will result in | |
1104 | * a spurious power IRQ resulting in system instability. | |
1105 | */ | |
1106 | while (irq_status & readl_relaxed(msm_host->core_mem + | |
1107 | CORE_PWRCTL_STATUS)) { | |
1108 | if (retry == 0) { | |
1109 | pr_err("%s: Timedout clearing (0x%x) pwrctl status register\n", | |
1110 | mmc_hostname(host->mmc), irq_status); | |
1111 | sdhci_msm_dump_pwr_ctrl_regs(host); | |
1112 | WARN_ON(1); | |
1113 | break; | |
1114 | } | |
1115 | writel_relaxed(irq_status, | |
1116 | msm_host->core_mem + CORE_PWRCTL_CLEAR); | |
1117 | retry--; | |
1118 | udelay(10); | |
1119 | } | |
1120 | ||
c0309b38 VV |
1121 | /* Handle BUS ON/OFF*/ |
1122 | if (irq_status & CORE_PWRCTL_BUS_ON) { | |
1123 | pwr_state = REQ_BUS_ON; | |
1124 | io_level = REQ_IO_HIGH; | |
1125 | irq_ack |= CORE_PWRCTL_BUS_SUCCESS; | |
1126 | } | |
1127 | if (irq_status & CORE_PWRCTL_BUS_OFF) { | |
1128 | pwr_state = REQ_BUS_OFF; | |
1129 | io_level = REQ_IO_LOW; | |
ad81d387 | 1130 | irq_ack |= CORE_PWRCTL_BUS_SUCCESS; |
c0309b38 VV |
1131 | } |
1132 | /* Handle IO LOW/HIGH */ | |
1133 | if (irq_status & CORE_PWRCTL_IO_LOW) { | |
1134 | io_level = REQ_IO_LOW; | |
ad81d387 | 1135 | irq_ack |= CORE_PWRCTL_IO_SUCCESS; |
c0309b38 VV |
1136 | } |
1137 | if (irq_status & CORE_PWRCTL_IO_HIGH) { | |
1138 | io_level = REQ_IO_HIGH; | |
1139 | irq_ack |= CORE_PWRCTL_IO_SUCCESS; | |
1140 | } | |
ad81d387 GD |
1141 | |
1142 | /* | |
1143 | * The driver has to acknowledge the interrupt, switch voltages and | |
1144 | * report back if it succeded or not to this register. The voltage | |
1145 | * switches are handled by the sdhci core, so just report success. | |
1146 | */ | |
1147 | writel_relaxed(irq_ack, msm_host->core_mem + CORE_PWRCTL_CTL); | |
401b2d06 | 1148 | |
c0309b38 VV |
1149 | if (pwr_state) |
1150 | msm_host->curr_pwr_state = pwr_state; | |
1151 | if (io_level) | |
1152 | msm_host->curr_io_level = io_level; | |
1153 | ||
401b2d06 ST |
1154 | pr_debug("%s: %s: Handled IRQ(%d), irq_status=0x%x, ack=0x%x\n", |
1155 | mmc_hostname(msm_host->mmc), __func__, irq, irq_status, | |
1156 | irq_ack); | |
ad81d387 GD |
1157 | } |
1158 | ||
1159 | static irqreturn_t sdhci_msm_pwr_irq(int irq, void *data) | |
1160 | { | |
1161 | struct sdhci_host *host = (struct sdhci_host *)data; | |
c0309b38 VV |
1162 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
1163 | struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host); | |
ad81d387 | 1164 | |
401b2d06 | 1165 | sdhci_msm_handle_pwr_irq(host, irq); |
c0309b38 VV |
1166 | msm_host->pwr_irq_flag = 1; |
1167 | sdhci_msm_complete_pwr_irq_wait(msm_host); | |
1168 | ||
ad81d387 GD |
1169 | |
1170 | return IRQ_HANDLED; | |
1171 | } | |
1172 | ||
80031bde RH |
1173 | static unsigned int sdhci_msm_get_max_clock(struct sdhci_host *host) |
1174 | { | |
1175 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); | |
1176 | struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host); | |
e4bf91f6 | 1177 | struct clk *core_clk = msm_host->bulk_clks[0].clk; |
80031bde | 1178 | |
e4bf91f6 | 1179 | return clk_round_rate(core_clk, ULONG_MAX); |
80031bde RH |
1180 | } |
1181 | ||
1182 | static unsigned int sdhci_msm_get_min_clock(struct sdhci_host *host) | |
1183 | { | |
1184 | return SDHCI_MSM_MIN_CLOCK; | |
1185 | } | |
1186 | ||
edc609fd RH |
1187 | /** |
1188 | * __sdhci_msm_set_clock - sdhci_msm clock control. | |
1189 | * | |
1190 | * Description: | |
1191 | * MSM controller does not use internal divider and | |
1192 | * instead directly control the GCC clock as per | |
1193 | * HW recommendation. | |
1194 | **/ | |
30de038d | 1195 | static void __sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock) |
edc609fd RH |
1196 | { |
1197 | u16 clk; | |
1198 | /* | |
1199 | * Keep actual_clock as zero - | |
1200 | * - since there is no divider used so no need of having actual_clock. | |
1201 | * - MSM controller uses SDCLK for data timeout calculation. If | |
1202 | * actual_clock is zero, host->clock is taken for calculation. | |
1203 | */ | |
1204 | host->mmc->actual_clock = 0; | |
1205 | ||
1206 | sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL); | |
1207 | ||
1208 | if (clock == 0) | |
1209 | return; | |
1210 | ||
1211 | /* | |
1212 | * MSM controller do not use clock divider. | |
1213 | * Thus read SDHCI_CLOCK_CONTROL and only enable | |
1214 | * clock with no divider value programmed. | |
1215 | */ | |
1216 | clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL); | |
1217 | sdhci_enable_clk(host, clk); | |
1218 | } | |
1219 | ||
1220 | /* sdhci_msm_set_clock - Called with (host->lock) spinlock held. */ | |
1221 | static void sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock) | |
1222 | { | |
1223 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); | |
1224 | struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host); | |
edc609fd RH |
1225 | |
1226 | if (!clock) { | |
1227 | msm_host->clk_rate = clock; | |
1228 | goto out; | |
1229 | } | |
1230 | ||
b54aaa8a | 1231 | sdhci_msm_hc_select_mode(host); |
edc609fd | 1232 | |
0fb8a3d4 | 1233 | msm_set_clock_rate_for_bus_mode(host, clock); |
edc609fd RH |
1234 | out: |
1235 | __sdhci_msm_set_clock(host, clock); | |
1236 | } | |
1237 | ||
c0309b38 VV |
1238 | /* |
1239 | * Platform specific register write functions. This is so that, if any | |
1240 | * register write needs to be followed up by platform specific actions, | |
1241 | * they can be added here. These functions can go to sleep when writes | |
1242 | * to certain registers are done. | |
1243 | * These functions are relying on sdhci_set_ios not using spinlock. | |
1244 | */ | |
1245 | static int __sdhci_msm_check_write(struct sdhci_host *host, u16 val, int reg) | |
1246 | { | |
1247 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); | |
1248 | struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host); | |
1249 | u32 req_type = 0; | |
1250 | ||
1251 | switch (reg) { | |
1252 | case SDHCI_HOST_CONTROL2: | |
1253 | req_type = (val & SDHCI_CTRL_VDD_180) ? REQ_IO_LOW : | |
1254 | REQ_IO_HIGH; | |
1255 | break; | |
1256 | case SDHCI_SOFTWARE_RESET: | |
1257 | if (host->pwr && (val & SDHCI_RESET_ALL)) | |
1258 | req_type = REQ_BUS_OFF; | |
1259 | break; | |
1260 | case SDHCI_POWER_CONTROL: | |
1261 | req_type = !val ? REQ_BUS_OFF : REQ_BUS_ON; | |
1262 | break; | |
1263 | } | |
1264 | ||
1265 | if (req_type) { | |
1266 | msm_host->pwr_irq_flag = 0; | |
1267 | /* | |
1268 | * Since this register write may trigger a power irq, ensure | |
1269 | * all previous register writes are complete by this point. | |
1270 | */ | |
1271 | mb(); | |
1272 | } | |
1273 | return req_type; | |
1274 | } | |
1275 | ||
1276 | /* This function may sleep*/ | |
1277 | static void sdhci_msm_writew(struct sdhci_host *host, u16 val, int reg) | |
1278 | { | |
1279 | u32 req_type = 0; | |
1280 | ||
1281 | req_type = __sdhci_msm_check_write(host, val, reg); | |
1282 | writew_relaxed(val, host->ioaddr + reg); | |
1283 | ||
1284 | if (req_type) | |
1285 | sdhci_msm_check_power_status(host, req_type); | |
1286 | } | |
1287 | ||
1288 | /* This function may sleep*/ | |
1289 | static void sdhci_msm_writeb(struct sdhci_host *host, u8 val, int reg) | |
1290 | { | |
1291 | u32 req_type = 0; | |
1292 | ||
1293 | req_type = __sdhci_msm_check_write(host, val, reg); | |
1294 | ||
1295 | writeb_relaxed(val, host->ioaddr + reg); | |
1296 | ||
1297 | if (req_type) | |
1298 | sdhci_msm_check_power_status(host, req_type); | |
1299 | } | |
1300 | ||
0eb0d9f4 GD |
1301 | static const struct of_device_id sdhci_msm_dt_match[] = { |
1302 | { .compatible = "qcom,sdhci-msm-v4" }, | |
1303 | {}, | |
1304 | }; | |
1305 | ||
1306 | MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match); | |
1307 | ||
a50396a4 | 1308 | static const struct sdhci_ops sdhci_msm_ops = { |
ed1761d7 | 1309 | .reset = sdhci_reset, |
edc609fd | 1310 | .set_clock = sdhci_msm_set_clock, |
80031bde RH |
1311 | .get_min_clock = sdhci_msm_get_min_clock, |
1312 | .get_max_clock = sdhci_msm_get_max_clock, | |
ed1761d7 | 1313 | .set_bus_width = sdhci_set_bus_width, |
ee320674 | 1314 | .set_uhs_signaling = sdhci_msm_set_uhs_signaling, |
c0309b38 VV |
1315 | .write_w = sdhci_msm_writew, |
1316 | .write_b = sdhci_msm_writeb, | |
0eb0d9f4 GD |
1317 | }; |
1318 | ||
a50396a4 JZ |
1319 | static const struct sdhci_pltfm_data sdhci_msm_pdata = { |
1320 | .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION | | |
9718f84b | 1321 | SDHCI_QUIRK_NO_CARD_NO_RESET | |
a0e31428 RH |
1322 | SDHCI_QUIRK_SINGLE_POWER_WRITE | |
1323 | SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN, | |
1324 | .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN, | |
a50396a4 JZ |
1325 | .ops = &sdhci_msm_ops, |
1326 | }; | |
1327 | ||
0eb0d9f4 GD |
1328 | static int sdhci_msm_probe(struct platform_device *pdev) |
1329 | { | |
1330 | struct sdhci_host *host; | |
1331 | struct sdhci_pltfm_host *pltfm_host; | |
1332 | struct sdhci_msm_host *msm_host; | |
1333 | struct resource *core_memres; | |
e4bf91f6 | 1334 | struct clk *clk; |
0eb0d9f4 | 1335 | int ret; |
3a3ad3e9 | 1336 | u16 host_version, core_minor; |
29301f40 | 1337 | u32 core_version, config; |
3a3ad3e9 | 1338 | u8 core_major; |
0eb0d9f4 | 1339 | |
6f699531 | 1340 | host = sdhci_pltfm_init(pdev, &sdhci_msm_pdata, sizeof(*msm_host)); |
0eb0d9f4 GD |
1341 | if (IS_ERR(host)) |
1342 | return PTR_ERR(host); | |
1343 | ||
2a641e53 | 1344 | host->sdma_boundary = 0; |
0eb0d9f4 | 1345 | pltfm_host = sdhci_priv(host); |
6f699531 | 1346 | msm_host = sdhci_pltfm_priv(pltfm_host); |
0eb0d9f4 GD |
1347 | msm_host->mmc = host->mmc; |
1348 | msm_host->pdev = pdev; | |
1349 | ||
1350 | ret = mmc_of_parse(host->mmc); | |
1351 | if (ret) | |
1352 | goto pltfm_free; | |
1353 | ||
1354 | sdhci_get_of_property(pdev); | |
1355 | ||
abf270e5 RH |
1356 | msm_host->saved_tuning_phase = INVALID_TUNING_PHASE; |
1357 | ||
0eb0d9f4 GD |
1358 | /* Setup SDCC bus voter clock. */ |
1359 | msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus"); | |
1360 | if (!IS_ERR(msm_host->bus_clk)) { | |
1361 | /* Vote for max. clk rate for max. performance */ | |
1362 | ret = clk_set_rate(msm_host->bus_clk, INT_MAX); | |
1363 | if (ret) | |
1364 | goto pltfm_free; | |
1365 | ret = clk_prepare_enable(msm_host->bus_clk); | |
1366 | if (ret) | |
1367 | goto pltfm_free; | |
1368 | } | |
1369 | ||
1370 | /* Setup main peripheral bus clock */ | |
e4bf91f6 BA |
1371 | clk = devm_clk_get(&pdev->dev, "iface"); |
1372 | if (IS_ERR(clk)) { | |
1373 | ret = PTR_ERR(clk); | |
2801b95e | 1374 | dev_err(&pdev->dev, "Peripheral clk setup failed (%d)\n", ret); |
0eb0d9f4 GD |
1375 | goto bus_clk_disable; |
1376 | } | |
e4bf91f6 | 1377 | msm_host->bulk_clks[1].clk = clk; |
0eb0d9f4 GD |
1378 | |
1379 | /* Setup SDC MMC clock */ | |
e4bf91f6 BA |
1380 | clk = devm_clk_get(&pdev->dev, "core"); |
1381 | if (IS_ERR(clk)) { | |
1382 | ret = PTR_ERR(clk); | |
0eb0d9f4 | 1383 | dev_err(&pdev->dev, "SDC MMC clk setup failed (%d)\n", ret); |
e4bf91f6 | 1384 | goto bus_clk_disable; |
0eb0d9f4 | 1385 | } |
e4bf91f6 BA |
1386 | msm_host->bulk_clks[0].clk = clk; |
1387 | ||
1388 | /* Vote for maximum clock rate for maximum performance */ | |
1389 | ret = clk_set_rate(clk, INT_MAX); | |
1390 | if (ret) | |
1391 | dev_warn(&pdev->dev, "core clock boost failed\n"); | |
1392 | ||
4946b3af BA |
1393 | clk = devm_clk_get(&pdev->dev, "cal"); |
1394 | if (IS_ERR(clk)) | |
1395 | clk = NULL; | |
1396 | msm_host->bulk_clks[2].clk = clk; | |
1397 | ||
1398 | clk = devm_clk_get(&pdev->dev, "sleep"); | |
1399 | if (IS_ERR(clk)) | |
1400 | clk = NULL; | |
1401 | msm_host->bulk_clks[3].clk = clk; | |
1402 | ||
e4bf91f6 BA |
1403 | ret = clk_bulk_prepare_enable(ARRAY_SIZE(msm_host->bulk_clks), |
1404 | msm_host->bulk_clks); | |
1405 | if (ret) | |
1406 | goto bus_clk_disable; | |
0eb0d9f4 | 1407 | |
83736352 VG |
1408 | /* |
1409 | * xo clock is needed for FLL feature of cm_dll. | |
1410 | * In case if xo clock is not mentioned in DT, warn and proceed. | |
1411 | */ | |
1412 | msm_host->xo_clk = devm_clk_get(&pdev->dev, "xo"); | |
1413 | if (IS_ERR(msm_host->xo_clk)) { | |
1414 | ret = PTR_ERR(msm_host->xo_clk); | |
1415 | dev_warn(&pdev->dev, "TCXO clk not present (%d)\n", ret); | |
1416 | } | |
1417 | ||
0eb0d9f4 GD |
1418 | core_memres = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
1419 | msm_host->core_mem = devm_ioremap_resource(&pdev->dev, core_memres); | |
1420 | ||
1421 | if (IS_ERR(msm_host->core_mem)) { | |
1422 | dev_err(&pdev->dev, "Failed to remap registers\n"); | |
1423 | ret = PTR_ERR(msm_host->core_mem); | |
1424 | goto clk_disable; | |
1425 | } | |
1426 | ||
5574ddcc VG |
1427 | /* Reset the vendor spec register to power on reset state */ |
1428 | writel_relaxed(CORE_VENDOR_SPEC_POR_VAL, | |
1429 | host->ioaddr + CORE_VENDOR_SPEC); | |
0eb0d9f4 GD |
1430 | |
1431 | /* Set HC_MODE_EN bit in HC_MODE register */ | |
1432 | writel_relaxed(HC_MODE_EN, (msm_host->core_mem + CORE_HC_MODE)); | |
1433 | ||
ff06ce41 VG |
1434 | config = readl_relaxed(msm_host->core_mem + CORE_HC_MODE); |
1435 | config |= FF_CLK_SW_RST_DIS; | |
1436 | writel_relaxed(config, msm_host->core_mem + CORE_HC_MODE); | |
1437 | ||
0eb0d9f4 GD |
1438 | host_version = readw_relaxed((host->ioaddr + SDHCI_HOST_VERSION)); |
1439 | dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n", | |
1440 | host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >> | |
1441 | SDHCI_VENDOR_VER_SHIFT)); | |
1442 | ||
3a3ad3e9 GD |
1443 | core_version = readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION); |
1444 | core_major = (core_version & CORE_VERSION_MAJOR_MASK) >> | |
1445 | CORE_VERSION_MAJOR_SHIFT; | |
1446 | core_minor = core_version & CORE_VERSION_MINOR_MASK; | |
1447 | dev_dbg(&pdev->dev, "MCI Version: 0x%08x, major: 0x%04x, minor: 0x%02x\n", | |
1448 | core_version, core_major, core_minor); | |
1449 | ||
83736352 VG |
1450 | if (core_major == 1 && core_minor >= 0x42) |
1451 | msm_host->use_14lpp_dll_reset = true; | |
1452 | ||
02e4293d RH |
1453 | /* |
1454 | * SDCC 5 controller with major version 1, minor version 0x34 and later | |
1455 | * with HS 400 mode support will use CM DLL instead of CDC LP 533 DLL. | |
1456 | */ | |
1457 | if (core_major == 1 && core_minor < 0x34) | |
1458 | msm_host->use_cdclp533 = true; | |
1459 | ||
3a3ad3e9 GD |
1460 | /* |
1461 | * Support for some capabilities is not advertised by newer | |
1462 | * controller versions and must be explicitly enabled. | |
1463 | */ | |
1464 | if (core_major >= 1 && core_minor != 0x11 && core_minor != 0x12) { | |
29301f40 RH |
1465 | config = readl_relaxed(host->ioaddr + SDHCI_CAPABILITIES); |
1466 | config |= SDHCI_CAN_VDD_300 | SDHCI_CAN_DO_8BIT; | |
1467 | writel_relaxed(config, host->ioaddr + | |
3a3ad3e9 GD |
1468 | CORE_VENDOR_SPEC_CAPABILITIES0); |
1469 | } | |
1470 | ||
c7ccee22 SJ |
1471 | /* |
1472 | * Power on reset state may trigger power irq if previous status of | |
1473 | * PWRCTL was either BUS_ON or IO_HIGH_V. So before enabling pwr irq | |
1474 | * interrupt in GIC, any pending power irq interrupt should be | |
1475 | * acknowledged. Otherwise power irq interrupt handler would be | |
1476 | * fired prematurely. | |
1477 | */ | |
401b2d06 | 1478 | sdhci_msm_handle_pwr_irq(host, 0); |
c7ccee22 SJ |
1479 | |
1480 | /* | |
1481 | * Ensure that above writes are propogated before interrupt enablement | |
1482 | * in GIC. | |
1483 | */ | |
1484 | mb(); | |
1485 | ||
ad81d387 GD |
1486 | /* Setup IRQ for handling power/voltage tasks with PMIC */ |
1487 | msm_host->pwr_irq = platform_get_irq_byname(pdev, "pwr_irq"); | |
1488 | if (msm_host->pwr_irq < 0) { | |
1489 | dev_err(&pdev->dev, "Get pwr_irq failed (%d)\n", | |
1490 | msm_host->pwr_irq); | |
d1f63f0c | 1491 | ret = msm_host->pwr_irq; |
ad81d387 GD |
1492 | goto clk_disable; |
1493 | } | |
1494 | ||
c0309b38 | 1495 | sdhci_msm_init_pwr_irq_wait(msm_host); |
c7ccee22 SJ |
1496 | /* Enable pwr irq interrupts */ |
1497 | writel_relaxed(INT_MASK, msm_host->core_mem + CORE_PWRCTL_MASK); | |
1498 | ||
ad81d387 GD |
1499 | ret = devm_request_threaded_irq(&pdev->dev, msm_host->pwr_irq, NULL, |
1500 | sdhci_msm_pwr_irq, IRQF_ONESHOT, | |
1501 | dev_name(&pdev->dev), host); | |
1502 | if (ret) { | |
1503 | dev_err(&pdev->dev, "Request IRQ failed (%d)\n", ret); | |
1504 | goto clk_disable; | |
1505 | } | |
1506 | ||
67e6db11 PG |
1507 | pm_runtime_get_noresume(&pdev->dev); |
1508 | pm_runtime_set_active(&pdev->dev); | |
1509 | pm_runtime_enable(&pdev->dev); | |
1510 | pm_runtime_set_autosuspend_delay(&pdev->dev, | |
1511 | MSM_MMC_AUTOSUSPEND_DELAY_MS); | |
1512 | pm_runtime_use_autosuspend(&pdev->dev); | |
1513 | ||
4436c535 | 1514 | host->mmc_host_ops.execute_tuning = sdhci_msm_execute_tuning; |
0eb0d9f4 GD |
1515 | ret = sdhci_add_host(host); |
1516 | if (ret) | |
67e6db11 PG |
1517 | goto pm_runtime_disable; |
1518 | ||
1519 | pm_runtime_mark_last_busy(&pdev->dev); | |
1520 | pm_runtime_put_autosuspend(&pdev->dev); | |
0eb0d9f4 GD |
1521 | |
1522 | return 0; | |
1523 | ||
67e6db11 PG |
1524 | pm_runtime_disable: |
1525 | pm_runtime_disable(&pdev->dev); | |
1526 | pm_runtime_set_suspended(&pdev->dev); | |
1527 | pm_runtime_put_noidle(&pdev->dev); | |
0eb0d9f4 | 1528 | clk_disable: |
e4bf91f6 BA |
1529 | clk_bulk_disable_unprepare(ARRAY_SIZE(msm_host->bulk_clks), |
1530 | msm_host->bulk_clks); | |
0eb0d9f4 GD |
1531 | bus_clk_disable: |
1532 | if (!IS_ERR(msm_host->bus_clk)) | |
1533 | clk_disable_unprepare(msm_host->bus_clk); | |
1534 | pltfm_free: | |
1535 | sdhci_pltfm_free(pdev); | |
1536 | return ret; | |
1537 | } | |
1538 | ||
1539 | static int sdhci_msm_remove(struct platform_device *pdev) | |
1540 | { | |
1541 | struct sdhci_host *host = platform_get_drvdata(pdev); | |
1542 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); | |
6f699531 | 1543 | struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host); |
0eb0d9f4 GD |
1544 | int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) == |
1545 | 0xffffffff); | |
1546 | ||
1547 | sdhci_remove_host(host, dead); | |
67e6db11 PG |
1548 | |
1549 | pm_runtime_get_sync(&pdev->dev); | |
1550 | pm_runtime_disable(&pdev->dev); | |
1551 | pm_runtime_put_noidle(&pdev->dev); | |
1552 | ||
e4bf91f6 BA |
1553 | clk_bulk_disable_unprepare(ARRAY_SIZE(msm_host->bulk_clks), |
1554 | msm_host->bulk_clks); | |
0eb0d9f4 GD |
1555 | if (!IS_ERR(msm_host->bus_clk)) |
1556 | clk_disable_unprepare(msm_host->bus_clk); | |
6f699531 | 1557 | sdhci_pltfm_free(pdev); |
0eb0d9f4 GD |
1558 | return 0; |
1559 | } | |
1560 | ||
67e6db11 PG |
1561 | #ifdef CONFIG_PM |
1562 | static int sdhci_msm_runtime_suspend(struct device *dev) | |
1563 | { | |
1564 | struct sdhci_host *host = dev_get_drvdata(dev); | |
1565 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); | |
1566 | struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host); | |
1567 | ||
e4bf91f6 BA |
1568 | clk_bulk_disable_unprepare(ARRAY_SIZE(msm_host->bulk_clks), |
1569 | msm_host->bulk_clks); | |
67e6db11 PG |
1570 | |
1571 | return 0; | |
1572 | } | |
1573 | ||
1574 | static int sdhci_msm_runtime_resume(struct device *dev) | |
1575 | { | |
1576 | struct sdhci_host *host = dev_get_drvdata(dev); | |
1577 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); | |
1578 | struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host); | |
67e6db11 | 1579 | |
e4bf91f6 BA |
1580 | return clk_bulk_prepare_enable(ARRAY_SIZE(msm_host->bulk_clks), |
1581 | msm_host->bulk_clks); | |
67e6db11 PG |
1582 | } |
1583 | #endif | |
1584 | ||
1585 | static const struct dev_pm_ops sdhci_msm_pm_ops = { | |
1586 | SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, | |
1587 | pm_runtime_force_resume) | |
1588 | SET_RUNTIME_PM_OPS(sdhci_msm_runtime_suspend, | |
1589 | sdhci_msm_runtime_resume, | |
1590 | NULL) | |
1591 | }; | |
1592 | ||
0eb0d9f4 GD |
1593 | static struct platform_driver sdhci_msm_driver = { |
1594 | .probe = sdhci_msm_probe, | |
1595 | .remove = sdhci_msm_remove, | |
1596 | .driver = { | |
1597 | .name = "sdhci_msm", | |
0eb0d9f4 | 1598 | .of_match_table = sdhci_msm_dt_match, |
67e6db11 | 1599 | .pm = &sdhci_msm_pm_ops, |
0eb0d9f4 GD |
1600 | }, |
1601 | }; | |
1602 | ||
1603 | module_platform_driver(sdhci_msm_driver); | |
1604 | ||
1605 | MODULE_DESCRIPTION("Qualcomm Secure Digital Host Controller Interface driver"); | |
1606 | MODULE_LICENSE("GPL v2"); |