blob: a861603a9130375040276d72b7e7e2c26963d8c9 [file] [log] [blame]
Asutosh Das0ef24812012-12-18 16:14:02 +05301/*
2 * drivers/mmc/host/sdhci-msm.c - Qualcomm Technologies, Inc. MSM SDHCI Platform
3 * driver source file
4 *
Venkat Gopalakrishnan0a92d532014-12-16 17:31:00 -08005 * Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
Asutosh Das0ef24812012-12-18 16:14:02 +05306 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 and
9 * only version 2 as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
18#include <linux/module.h>
19#include <linux/mmc/host.h>
20#include <linux/mmc/card.h>
21#include <linux/mmc/sdio_func.h>
22#include <linux/gfp.h>
23#include <linux/of.h>
24#include <linux/of_gpio.h>
25#include <linux/regulator/consumer.h>
26#include <linux/types.h>
27#include <linux/input.h>
28#include <linux/platform_device.h>
29#include <linux/wait.h>
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -070030#include <linux/io.h>
31#include <linux/delay.h>
32#include <linux/scatterlist.h>
33#include <linux/slab.h>
Sahitya Tummala581df132013-03-12 14:57:46 +053034#include <linux/mmc/slot-gpio.h>
Sahitya Tummalaeaa21862013-03-20 19:34:59 +053035#include <linux/dma-mapping.h>
Sahitya Tummala66b0fe32013-04-25 11:50:56 +053036#include <linux/iopoll.h>
Pratibhasagar V9acf2642013-11-21 21:07:21 +053037#include <linux/pinctrl/consumer.h>
38#include <linux/iopoll.h>
Sahitya Tummala8a3e8182013-03-10 14:12:52 +053039#include <linux/msm-bus.h>
Konstantin Dorfman98377d32015-02-25 10:09:41 +020040#include <linux/pm_runtime.h>
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +020041#include <trace/events/mmc.h>
Asutosh Das0ef24812012-12-18 16:14:02 +053042
Sahitya Tummala56874732015-05-21 08:24:03 +053043#include "sdhci-msm.h"
Asutosh Das0ef24812012-12-18 16:14:02 +053044
Venkat Gopalakrishnan0a92d532014-12-16 17:31:00 -080045#define CORE_POWER 0x0
46#define CORE_SW_RST (1 << 7)
47
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -070048#define SDHCI_VER_100 0x2B
Venkat Gopalakrishnan0a92d532014-12-16 17:31:00 -080049#define CORE_MCI_DATA_CNT 0x30
50#define CORE_MCI_STATUS 0x34
51#define CORE_MCI_FIFO_CNT 0x44
52
53#define CORE_VERSION_STEP_MASK 0x0000FFFF
54#define CORE_VERSION_MINOR_MASK 0x0FFF0000
55#define CORE_VERSION_MINOR_SHIFT 16
56#define CORE_VERSION_MAJOR_MASK 0xF0000000
57#define CORE_VERSION_MAJOR_SHIFT 28
58#define CORE_VERSION_TARGET_MASK 0x000000FF
59
60#define CORE_GENERICS 0x70
61#define SWITCHABLE_SIGNALLING_VOL (1 << 29)
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +053062
63#define CORE_VERSION_MAJOR_MASK 0xF0000000
64#define CORE_VERSION_MAJOR_SHIFT 28
65
Asutosh Das0ef24812012-12-18 16:14:02 +053066#define CORE_HC_MODE 0x78
67#define HC_MODE_EN 0x1
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -070068#define FF_CLK_SW_RST_DIS (1 << 13)
Asutosh Das0ef24812012-12-18 16:14:02 +053069
Sahitya Tummala67717bc2013-08-02 09:21:37 +053070#define CORE_MCI_VERSION 0x050
71#define CORE_TESTBUS_CONFIG 0x0CC
72#define CORE_TESTBUS_ENA (1 << 3)
Venkat Gopalakrishnan0a92d532014-12-16 17:31:00 -080073#define CORE_TESTBUS_SEL2_BIT 4
74#define CORE_TESTBUS_SEL2 (1 << CORE_TESTBUS_SEL2_BIT)
Sahitya Tummala67717bc2013-08-02 09:21:37 +053075
Asutosh Das0ef24812012-12-18 16:14:02 +053076#define CORE_PWRCTL_STATUS 0xDC
77#define CORE_PWRCTL_MASK 0xE0
78#define CORE_PWRCTL_CLEAR 0xE4
79#define CORE_PWRCTL_CTL 0xE8
80
81#define CORE_PWRCTL_BUS_OFF 0x01
82#define CORE_PWRCTL_BUS_ON (1 << 1)
83#define CORE_PWRCTL_IO_LOW (1 << 2)
84#define CORE_PWRCTL_IO_HIGH (1 << 3)
85
86#define CORE_PWRCTL_BUS_SUCCESS 0x01
87#define CORE_PWRCTL_BUS_FAIL (1 << 1)
88#define CORE_PWRCTL_IO_SUCCESS (1 << 2)
89#define CORE_PWRCTL_IO_FAIL (1 << 3)
90
91#define INT_MASK 0xF
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -070092#define MAX_PHASES 16
93
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -070094#define CORE_DLL_CONFIG 0x100
95#define CORE_CMD_DAT_TRACK_SEL (1 << 0)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -070096#define CORE_DLL_EN (1 << 16)
97#define CORE_CDR_EN (1 << 17)
98#define CORE_CK_OUT_EN (1 << 18)
99#define CORE_CDR_EXT_EN (1 << 19)
100#define CORE_DLL_PDN (1 << 29)
101#define CORE_DLL_RST (1 << 30)
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700102
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700103#define CORE_DLL_STATUS 0x108
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700104#define CORE_DLL_LOCK (1 << 7)
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700105#define CORE_DDR_DLL_LOCK (1 << 11)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700106
107#define CORE_VENDOR_SPEC 0x10C
Krishna Konda46fd1432014-10-30 21:13:27 -0700108#define CORE_CLK_PWRSAVE (1 << 1)
109#define CORE_HC_MCLK_SEL_DFLT (2 << 8)
110#define CORE_HC_MCLK_SEL_HS400 (3 << 8)
111#define CORE_HC_MCLK_SEL_MASK (3 << 8)
112#define CORE_HC_AUTO_CMD21_EN (1 << 6)
113#define CORE_IO_PAD_PWR_SWITCH_EN (1 << 15)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700114#define CORE_IO_PAD_PWR_SWITCH (1 << 16)
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700115#define CORE_HC_SELECT_IN_EN (1 << 18)
116#define CORE_HC_SELECT_IN_HS400 (6 << 19)
117#define CORE_HC_SELECT_IN_MASK (7 << 19)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700118
Venkat Gopalakrishnan0a92d532014-12-16 17:31:00 -0800119#define CORE_VENDOR_SPEC_ADMA_ERR_ADDR0 0x114
120#define CORE_VENDOR_SPEC_ADMA_ERR_ADDR1 0x118
121
Krishna Konda7feab352013-09-17 23:55:40 -0700122#define CORE_VENDOR_SPEC_CAPABILITIES0 0x11C
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +0530123#define CORE_8_BIT_SUPPORT (1 << 18)
124#define CORE_3_3V_SUPPORT (1 << 24)
125#define CORE_3_0V_SUPPORT (1 << 25)
126#define CORE_1_8V_SUPPORT (1 << 26)
Gilad Broner2a10ca02014-10-02 17:20:35 +0300127#define CORE_SYS_BUS_SUPPORT_64_BIT BIT(28)
Krishna Konda7feab352013-09-17 23:55:40 -0700128
Venkat Gopalakrishnan0a92d532014-12-16 17:31:00 -0800129#define CORE_SDCC_DEBUG_REG 0x124
Sahitya Tummala67717bc2013-08-02 09:21:37 +0530130
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700131#define CORE_CSR_CDC_CTLR_CFG0 0x130
132#define CORE_SW_TRIG_FULL_CALIB (1 << 16)
133#define CORE_HW_AUTOCAL_ENA (1 << 17)
134
135#define CORE_CSR_CDC_CTLR_CFG1 0x134
136#define CORE_CSR_CDC_CAL_TIMER_CFG0 0x138
137#define CORE_TIMER_ENA (1 << 16)
138
139#define CORE_CSR_CDC_CAL_TIMER_CFG1 0x13C
140#define CORE_CSR_CDC_REFCOUNT_CFG 0x140
141#define CORE_CSR_CDC_COARSE_CAL_CFG 0x144
142#define CORE_CDC_OFFSET_CFG 0x14C
143#define CORE_CSR_CDC_DELAY_CFG 0x150
144#define CORE_CDC_SLAVE_DDA_CFG 0x160
145#define CORE_CSR_CDC_STATUS0 0x164
146#define CORE_CALIBRATION_DONE (1 << 0)
147
148#define CORE_CDC_ERROR_CODE_MASK 0x7000000
149
150#define CORE_CSR_CDC_GEN_CFG 0x178
151#define CORE_CDC_SWITCH_BYPASS_OFF (1 << 0)
152#define CORE_CDC_SWITCH_RC_EN (1 << 1)
153
154#define CORE_DDR_200_CFG 0x184
155#define CORE_CDC_T4_DLY_SEL (1 << 0)
156#define CORE_START_CDC_TRAFFIC (1 << 6)
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700157#define CORE_VENDOR_SPEC3 0x1B0
158#define CORE_PWRSAVE_DLL (1 << 3)
159
160#define CORE_DLL_CONFIG_2 0x1B4
161#define CORE_DDR_CAL_EN (1 << 0)
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800162#define CORE_FLL_CYCLE_CNT (1 << 18)
163#define CORE_DLL_CLOCK_DISABLE (1 << 21)
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700164
165#define CORE_DDR_CONFIG 0x1B8
166#define DDR_CONFIG_POR_VAL 0x80040853
167
Sahitya Tummala56874732015-05-21 08:24:03 +0530168#define MSM_MMC_DEFAULT_CPU_DMA_LATENCY 200 /* usecs */
169
Venkat Gopalakrishnan450745e2014-07-24 20:39:34 -0700170/* 512 descriptors */
171#define SDHCI_MSM_MAX_SEGMENTS (1 << 9)
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +0530172#define SDHCI_MSM_MMC_CLK_GATE_DELAY 200 /* msecs */
Asutosh Das648f9d12013-01-10 21:11:04 +0530173
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700174#define CORE_FREQ_100MHZ (100 * 1000 * 1000)
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800175#define TCXO_FREQ 19200000
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700176
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700177#define INVALID_TUNING_PHASE -1
178
Krishna Konda96e6b112013-10-28 15:25:03 -0700179#define NUM_TUNING_PHASES 16
180#define MAX_DRV_TYPES_SUPPORTED_HS200 3
Konstantin Dorfman98377d32015-02-25 10:09:41 +0200181#define MSM_AUTOSUSPEND_DELAY_MS 100
Krishna Konda96e6b112013-10-28 15:25:03 -0700182
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700183static const u32 tuning_block_64[] = {
184 0x00FF0FFF, 0xCCC3CCFF, 0xFFCC3CC3, 0xEFFEFFFE,
185 0xDDFFDFFF, 0xFBFFFBFF, 0xFF7FFFBF, 0xEFBDF777,
186 0xF0FFF0FF, 0x3CCCFC0F, 0xCFCC33CC, 0xEEFFEFFF,
187 0xFDFFFDFF, 0xFFBFFFDF, 0xFFF7FFBB, 0xDE7B7FF7
188};
189
190static const u32 tuning_block_128[] = {
191 0xFF00FFFF, 0x0000FFFF, 0xCCCCFFFF, 0xCCCC33CC,
192 0xCC3333CC, 0xFFFFCCCC, 0xFFFFEEFF, 0xFFEEEEFF,
193 0xFFDDFFFF, 0xDDDDFFFF, 0xBBFFFFFF, 0xBBFFFFFF,
194 0xFFFFFFBB, 0xFFFFFF77, 0x77FF7777, 0xFFEEDDBB,
195 0x00FFFFFF, 0x00FFFFFF, 0xCCFFFF00, 0xCC33CCCC,
196 0x3333CCCC, 0xFFCCCCCC, 0xFFEEFFFF, 0xEEEEFFFF,
197 0xDDFFFFFF, 0xDDFFFFFF, 0xFFFFFFDD, 0xFFFFFFBB,
198 0xFFFFBBBB, 0xFFFF77FF, 0xFF7777FF, 0xEEDDBB77
199};
Asutosh Das0ef24812012-12-18 16:14:02 +0530200
Venkat Gopalakrishnan270580a2013-03-11 12:17:57 -0700201static int disable_slots;
202/* root can write, others read */
203module_param(disable_slots, int, S_IRUGO|S_IWUSR);
204
Asutosh Das0ef24812012-12-18 16:14:02 +0530205enum vdd_io_level {
206 /* set vdd_io_data->low_vol_level */
207 VDD_IO_LOW,
208 /* set vdd_io_data->high_vol_level */
209 VDD_IO_HIGH,
210 /*
211 * set whatever there in voltage_level (third argument) of
212 * sdhci_msm_set_vdd_io_vol() function.
213 */
214 VDD_IO_SET_LEVEL,
215};
216
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700217/* MSM platform specific tuning */
218static inline int msm_dll_poll_ck_out_en(struct sdhci_host *host,
219 u8 poll)
220{
221 int rc = 0;
222 u32 wait_cnt = 50;
223 u8 ck_out_en = 0;
224 struct mmc_host *mmc = host->mmc;
225
226 /* poll for CK_OUT_EN bit. max. poll time = 50us */
227 ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) &
228 CORE_CK_OUT_EN);
229
230 while (ck_out_en != poll) {
231 if (--wait_cnt == 0) {
232 pr_err("%s: %s: CK_OUT_EN bit is not %d\n",
233 mmc_hostname(mmc), __func__, poll);
234 rc = -ETIMEDOUT;
235 goto out;
236 }
237 udelay(1);
238
239 ck_out_en = !!(readl_relaxed(host->ioaddr +
240 CORE_DLL_CONFIG) & CORE_CK_OUT_EN);
241 }
242out:
243 return rc;
244}
245
Asutosh Dase5e9ca62013-07-30 19:08:36 +0530246/*
247 * Enable CDR to track changes of DAT lines and adjust sampling
248 * point according to voltage/temperature variations
249 */
250static int msm_enable_cdr_cm_sdc4_dll(struct sdhci_host *host)
251{
252 int rc = 0;
253 u32 config;
254
255 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
256 config |= CORE_CDR_EN;
257 config &= ~(CORE_CDR_EXT_EN | CORE_CK_OUT_EN);
258 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
259
260 rc = msm_dll_poll_ck_out_en(host, 0);
261 if (rc)
262 goto err;
263
264 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) |
265 CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
266
267 rc = msm_dll_poll_ck_out_en(host, 1);
268 if (rc)
269 goto err;
270 goto out;
271err:
272 pr_err("%s: %s: failed\n", mmc_hostname(host->mmc), __func__);
273out:
274 return rc;
275}
276
277static ssize_t store_auto_cmd21(struct device *dev, struct device_attribute
278 *attr, const char *buf, size_t count)
279{
280 struct sdhci_host *host = dev_get_drvdata(dev);
281 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
282 struct sdhci_msm_host *msm_host = pltfm_host->priv;
283 u32 tmp;
284 unsigned long flags;
285
286 if (!kstrtou32(buf, 0, &tmp)) {
287 spin_lock_irqsave(&host->lock, flags);
288 msm_host->en_auto_cmd21 = !!tmp;
289 spin_unlock_irqrestore(&host->lock, flags);
290 }
291 return count;
292}
293
294static ssize_t show_auto_cmd21(struct device *dev,
295 struct device_attribute *attr, char *buf)
296{
297 struct sdhci_host *host = dev_get_drvdata(dev);
298 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
299 struct sdhci_msm_host *msm_host = pltfm_host->priv;
300
301 return snprintf(buf, PAGE_SIZE, "%d\n", msm_host->en_auto_cmd21);
302}
303
304/* MSM auto-tuning handler */
305static int sdhci_msm_config_auto_tuning_cmd(struct sdhci_host *host,
306 bool enable,
307 u32 type)
308{
309 int rc = 0;
310 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
311 struct sdhci_msm_host *msm_host = pltfm_host->priv;
312 u32 val = 0;
313
314 if (!msm_host->en_auto_cmd21)
315 return 0;
316
317 if (type == MMC_SEND_TUNING_BLOCK_HS200)
318 val = CORE_HC_AUTO_CMD21_EN;
319 else
320 return 0;
321
322 if (enable) {
323 rc = msm_enable_cdr_cm_sdc4_dll(host);
324 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
325 val, host->ioaddr + CORE_VENDOR_SPEC);
326 } else {
327 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
328 ~val, host->ioaddr + CORE_VENDOR_SPEC);
329 }
330 return rc;
331}
332
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700333static int msm_config_cm_dll_phase(struct sdhci_host *host, u8 phase)
334{
335 int rc = 0;
336 u8 grey_coded_phase_table[] = {0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4,
337 0xC, 0xD, 0xF, 0xE, 0xA, 0xB, 0x9,
338 0x8};
339 unsigned long flags;
340 u32 config;
341 struct mmc_host *mmc = host->mmc;
342
343 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
344 spin_lock_irqsave(&host->lock, flags);
345
346 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
347 config &= ~(CORE_CDR_EN | CORE_CK_OUT_EN);
348 config |= (CORE_CDR_EXT_EN | CORE_DLL_EN);
349 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
350
351 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '0' */
352 rc = msm_dll_poll_ck_out_en(host, 0);
353 if (rc)
354 goto err_out;
355
356 /*
357 * Write the selected DLL clock output phase (0 ... 15)
358 * to CDR_SELEXT bit field of DLL_CONFIG register.
359 */
360 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
361 & ~(0xF << 20))
362 | (grey_coded_phase_table[phase] << 20)),
363 host->ioaddr + CORE_DLL_CONFIG);
364
365 /* Set CK_OUT_EN bit of DLL_CONFIG register to 1. */
366 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
367 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
368
369 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '1' */
370 rc = msm_dll_poll_ck_out_en(host, 1);
371 if (rc)
372 goto err_out;
373
374 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
375 config |= CORE_CDR_EN;
376 config &= ~CORE_CDR_EXT_EN;
377 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
378 goto out;
379
380err_out:
381 pr_err("%s: %s: Failed to set DLL phase: %d\n",
382 mmc_hostname(mmc), __func__, phase);
383out:
384 spin_unlock_irqrestore(&host->lock, flags);
385 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
386 return rc;
387}
388
389/*
390 * Find out the greatest range of consecuitive selected
391 * DLL clock output phases that can be used as sampling
392 * setting for SD3.0 UHS-I card read operation (in SDR104
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700393 * timing mode) or for eMMC4.5 card read operation (in
394 * HS400/HS200 timing mode).
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700395 * Select the 3/4 of the range and configure the DLL with the
396 * selected DLL clock output phase.
397 */
398
399static int msm_find_most_appropriate_phase(struct sdhci_host *host,
400 u8 *phase_table, u8 total_phases)
401{
402 int ret;
403 u8 ranges[MAX_PHASES][MAX_PHASES] = { {0}, {0} };
404 u8 phases_per_row[MAX_PHASES] = {0};
405 int row_index = 0, col_index = 0, selected_row_index = 0, curr_max = 0;
406 int i, cnt, phase_0_raw_index = 0, phase_15_raw_index = 0;
407 bool phase_0_found = false, phase_15_found = false;
408 struct mmc_host *mmc = host->mmc;
409
410 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
411 if (!total_phases || (total_phases > MAX_PHASES)) {
412 pr_err("%s: %s: invalid argument: total_phases=%d\n",
413 mmc_hostname(mmc), __func__, total_phases);
414 return -EINVAL;
415 }
416
417 for (cnt = 0; cnt < total_phases; cnt++) {
418 ranges[row_index][col_index] = phase_table[cnt];
419 phases_per_row[row_index] += 1;
420 col_index++;
421
422 if ((cnt + 1) == total_phases) {
423 continue;
424 /* check if next phase in phase_table is consecutive or not */
425 } else if ((phase_table[cnt] + 1) != phase_table[cnt + 1]) {
426 row_index++;
427 col_index = 0;
428 }
429 }
430
431 if (row_index >= MAX_PHASES)
432 return -EINVAL;
433
434 /* Check if phase-0 is present in first valid window? */
435 if (!ranges[0][0]) {
436 phase_0_found = true;
437 phase_0_raw_index = 0;
438 /* Check if cycle exist between 2 valid windows */
439 for (cnt = 1; cnt <= row_index; cnt++) {
440 if (phases_per_row[cnt]) {
441 for (i = 0; i < phases_per_row[cnt]; i++) {
442 if (ranges[cnt][i] == 15) {
443 phase_15_found = true;
444 phase_15_raw_index = cnt;
445 break;
446 }
447 }
448 }
449 }
450 }
451
452 /* If 2 valid windows form cycle then merge them as single window */
453 if (phase_0_found && phase_15_found) {
454 /* number of phases in raw where phase 0 is present */
455 u8 phases_0 = phases_per_row[phase_0_raw_index];
456 /* number of phases in raw where phase 15 is present */
457 u8 phases_15 = phases_per_row[phase_15_raw_index];
458
459 if (phases_0 + phases_15 >= MAX_PHASES)
460 /*
461 * If there are more than 1 phase windows then total
462 * number of phases in both the windows should not be
463 * more than or equal to MAX_PHASES.
464 */
465 return -EINVAL;
466
467 /* Merge 2 cyclic windows */
468 i = phases_15;
469 for (cnt = 0; cnt < phases_0; cnt++) {
470 ranges[phase_15_raw_index][i] =
471 ranges[phase_0_raw_index][cnt];
472 if (++i >= MAX_PHASES)
473 break;
474 }
475
476 phases_per_row[phase_0_raw_index] = 0;
477 phases_per_row[phase_15_raw_index] = phases_15 + phases_0;
478 }
479
480 for (cnt = 0; cnt <= row_index; cnt++) {
481 if (phases_per_row[cnt] > curr_max) {
482 curr_max = phases_per_row[cnt];
483 selected_row_index = cnt;
484 }
485 }
486
487 i = ((curr_max * 3) / 4);
488 if (i)
489 i--;
490
491 ret = (int)ranges[selected_row_index][i];
492
493 if (ret >= MAX_PHASES) {
494 ret = -EINVAL;
495 pr_err("%s: %s: invalid phase selected=%d\n",
496 mmc_hostname(mmc), __func__, ret);
497 }
498
499 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
500 return ret;
501}
502
503static inline void msm_cm_dll_set_freq(struct sdhci_host *host)
504{
505 u32 mclk_freq = 0;
506
507 /* Program the MCLK value to MCLK_FREQ bit field */
508 if (host->clock <= 112000000)
509 mclk_freq = 0;
510 else if (host->clock <= 125000000)
511 mclk_freq = 1;
512 else if (host->clock <= 137000000)
513 mclk_freq = 2;
514 else if (host->clock <= 150000000)
515 mclk_freq = 3;
516 else if (host->clock <= 162000000)
517 mclk_freq = 4;
518 else if (host->clock <= 175000000)
519 mclk_freq = 5;
520 else if (host->clock <= 187000000)
521 mclk_freq = 6;
522 else if (host->clock <= 200000000)
523 mclk_freq = 7;
524
525 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
526 & ~(7 << 24)) | (mclk_freq << 24)),
527 host->ioaddr + CORE_DLL_CONFIG);
528}
529
530/* Initialize the DLL (Programmable Delay Line ) */
531static int msm_init_cm_dll(struct sdhci_host *host)
532{
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800533 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
534 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700535 struct mmc_host *mmc = host->mmc;
536 int rc = 0;
537 unsigned long flags;
538 u32 wait_cnt;
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530539 bool prev_pwrsave, curr_pwrsave;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700540
541 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
542 spin_lock_irqsave(&host->lock, flags);
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530543 prev_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
544 CORE_CLK_PWRSAVE);
545 curr_pwrsave = prev_pwrsave;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700546 /*
547 * Make sure that clock is always enabled when DLL
548 * tuning is in progress. Keeping PWRSAVE ON may
549 * turn off the clock. So let's disable the PWRSAVE
550 * here and re-enable it once tuning is completed.
551 */
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530552 if (prev_pwrsave) {
553 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
554 & ~CORE_CLK_PWRSAVE),
555 host->ioaddr + CORE_VENDOR_SPEC);
556 curr_pwrsave = false;
557 }
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700558
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800559 if (msm_host->use_updated_dll_reset) {
560 /* Disable the DLL clock */
561 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
562 & ~CORE_CK_OUT_EN),
563 host->ioaddr + CORE_DLL_CONFIG);
564
565 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
566 | CORE_DLL_CLOCK_DISABLE),
567 host->ioaddr + CORE_DLL_CONFIG_2);
568 }
569
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700570 /* Write 1 to DLL_RST bit of DLL_CONFIG register */
571 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
572 | CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
573
574 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
575 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
576 | CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
577 msm_cm_dll_set_freq(host);
578
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800579 if (msm_host->use_updated_dll_reset) {
580 u32 mclk_freq = 0;
581
582 if ((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
583 & CORE_FLL_CYCLE_CNT))
584 mclk_freq = (u32) ((host->clock / TCXO_FREQ) * 8);
585 else
586 mclk_freq = (u32) ((host->clock / TCXO_FREQ) * 4);
587
588 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
589 & ~(0xFF << 10)) | (mclk_freq << 10)),
590 host->ioaddr + CORE_DLL_CONFIG_2);
591 /* wait for 5us before enabling DLL clock */
592 udelay(5);
593 }
594
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700595 /* Write 0 to DLL_RST bit of DLL_CONFIG register */
596 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
597 & ~CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
598
599 /* Write 0 to DLL_PDN bit of DLL_CONFIG register */
600 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
601 & ~CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
602
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800603 if (msm_host->use_updated_dll_reset) {
604 msm_cm_dll_set_freq(host);
605 /* Enable the DLL clock */
606 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
607 & ~CORE_DLL_CLOCK_DISABLE),
608 host->ioaddr + CORE_DLL_CONFIG_2);
609 }
610
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700611 /* Set DLL_EN bit to 1. */
612 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
613 | CORE_DLL_EN), host->ioaddr + CORE_DLL_CONFIG);
614
615 /* Set CK_OUT_EN bit to 1. */
616 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
617 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
618
619 wait_cnt = 50;
620 /* Wait until DLL_LOCK bit of DLL_STATUS register becomes '1' */
621 while (!(readl_relaxed(host->ioaddr + CORE_DLL_STATUS) &
622 CORE_DLL_LOCK)) {
623 /* max. wait for 50us sec for LOCK bit to be set */
624 if (--wait_cnt == 0) {
625 pr_err("%s: %s: DLL failed to LOCK\n",
626 mmc_hostname(mmc), __func__);
627 rc = -ETIMEDOUT;
628 goto out;
629 }
630 /* wait for 1us before polling again */
631 udelay(1);
632 }
633
634out:
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530635 /* Restore the correct PWRSAVE state */
636 if (prev_pwrsave ^ curr_pwrsave) {
637 u32 reg = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC);
638
639 if (prev_pwrsave)
640 reg |= CORE_CLK_PWRSAVE;
641 else
642 reg &= ~CORE_CLK_PWRSAVE;
643
644 writel_relaxed(reg, host->ioaddr + CORE_VENDOR_SPEC);
645 }
646
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700647 spin_unlock_irqrestore(&host->lock, flags);
648 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
649 return rc;
650}
651
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700652static int sdhci_msm_cdclp533_calibration(struct sdhci_host *host)
653{
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700654 u32 calib_done;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700655 int ret = 0;
656 int cdc_err = 0;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700657
658 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
659
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700660 /* Write 0 to CDC_T4_DLY_SEL field in VENDOR_SPEC_DDR200_CFG */
661 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
662 & ~CORE_CDC_T4_DLY_SEL),
663 host->ioaddr + CORE_DDR_200_CFG);
664
665 /* Write 0 to CDC_SWITCH_BYPASS_OFF field in CORE_CSR_CDC_GEN_CFG */
666 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG)
667 & ~CORE_CDC_SWITCH_BYPASS_OFF),
668 host->ioaddr + CORE_CSR_CDC_GEN_CFG);
669
670 /* Write 1 to CDC_SWITCH_RC_EN field in CORE_CSR_CDC_GEN_CFG */
671 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG)
672 | CORE_CDC_SWITCH_RC_EN),
673 host->ioaddr + CORE_CSR_CDC_GEN_CFG);
674
675 /* Write 0 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
676 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
677 & ~CORE_START_CDC_TRAFFIC),
678 host->ioaddr + CORE_DDR_200_CFG);
679
680 /*
681 * Perform CDC Register Initialization Sequence
682 *
683 * CORE_CSR_CDC_CTLR_CFG0 0x11800EC
684 * CORE_CSR_CDC_CTLR_CFG1 0x3011111
685 * CORE_CSR_CDC_CAL_TIMER_CFG0 0x1201000
686 * CORE_CSR_CDC_CAL_TIMER_CFG1 0x4
687 * CORE_CSR_CDC_REFCOUNT_CFG 0xCB732020
688 * CORE_CSR_CDC_COARSE_CAL_CFG 0xB19
689 * CORE_CSR_CDC_DELAY_CFG 0x3AC
690 * CORE_CDC_OFFSET_CFG 0x0
691 * CORE_CDC_SLAVE_DDA_CFG 0x16334
692 */
693
694 writel_relaxed(0x11800EC, host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
695 writel_relaxed(0x3011111, host->ioaddr + CORE_CSR_CDC_CTLR_CFG1);
696 writel_relaxed(0x1201000, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
697 writel_relaxed(0x4, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG1);
698 writel_relaxed(0xCB732020, host->ioaddr + CORE_CSR_CDC_REFCOUNT_CFG);
699 writel_relaxed(0xB19, host->ioaddr + CORE_CSR_CDC_COARSE_CAL_CFG);
Subhash Jadavanibe406d92014-06-17 16:47:48 -0700700 writel_relaxed(0x4E2, host->ioaddr + CORE_CSR_CDC_DELAY_CFG);
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700701 writel_relaxed(0x0, host->ioaddr + CORE_CDC_OFFSET_CFG);
702 writel_relaxed(0x16334, host->ioaddr + CORE_CDC_SLAVE_DDA_CFG);
703
704 /* CDC HW Calibration */
705
706 /* Write 1 to SW_TRIG_FULL_CALIB field in CORE_CSR_CDC_CTLR_CFG0 */
707 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
708 | CORE_SW_TRIG_FULL_CALIB),
709 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
710
711 /* Write 0 to SW_TRIG_FULL_CALIB field in CORE_CSR_CDC_CTLR_CFG0 */
712 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
713 & ~CORE_SW_TRIG_FULL_CALIB),
714 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
715
716 /* Write 1 to HW_AUTOCAL_ENA field in CORE_CSR_CDC_CTLR_CFG0 */
717 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
718 | CORE_HW_AUTOCAL_ENA),
719 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
720
721 /* Write 1 to TIMER_ENA field in CORE_CSR_CDC_CAL_TIMER_CFG0 */
722 writel_relaxed((readl_relaxed(host->ioaddr +
723 CORE_CSR_CDC_CAL_TIMER_CFG0) | CORE_TIMER_ENA),
724 host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
725
726 mb();
727
728 /* Poll on CALIBRATION_DONE field in CORE_CSR_CDC_STATUS0 to be 1 */
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700729 ret = readl_poll_timeout(host->ioaddr + CORE_CSR_CDC_STATUS0,
730 calib_done, (calib_done & CORE_CALIBRATION_DONE), 1, 50);
731
732 if (ret == -ETIMEDOUT) {
733 pr_err("%s: %s: CDC Calibration was not completed\n",
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700734 mmc_hostname(host->mmc), __func__);
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700735 goto out;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700736 }
737
738 /* Verify CDC_ERROR_CODE field in CORE_CSR_CDC_STATUS0 is 0 */
739 cdc_err = readl_relaxed(host->ioaddr + CORE_CSR_CDC_STATUS0)
740 & CORE_CDC_ERROR_CODE_MASK;
741 if (cdc_err) {
742 pr_err("%s: %s: CDC Error Code %d\n",
743 mmc_hostname(host->mmc), __func__, cdc_err);
744 ret = -EINVAL;
745 goto out;
746 }
747
748 /* Write 1 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
749 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
750 | CORE_START_CDC_TRAFFIC),
751 host->ioaddr + CORE_DDR_200_CFG);
752out:
753 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
754 __func__, ret);
755 return ret;
756}
757
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700758static int sdhci_msm_cm_dll_sdc4_calibration(struct sdhci_host *host)
759{
Ritesh Harjani764065e2015-05-13 14:14:45 +0530760 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
761 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700762 u32 dll_status;
763 int ret = 0;
764
765 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
766
767 /*
768 * Currently the CORE_DDR_CONFIG register defaults to desired
769 * configuration on reset. Currently reprogramming the power on
770 * reset (POR) value in case it might have been modified by
771 * bootloaders. In the future, if this changes, then the desired
772 * values will need to be programmed appropriately.
773 */
774 writel_relaxed(DDR_CONFIG_POR_VAL, host->ioaddr + CORE_DDR_CONFIG);
775
776 /* Write 1 to DDR_CAL_EN field in CORE_DLL_CONFIG_2 */
777 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
778 | CORE_DDR_CAL_EN),
779 host->ioaddr + CORE_DLL_CONFIG_2);
780
781 /* Poll on DDR_DLL_LOCK bit in CORE_DLL_STATUS to be set */
782 ret = readl_poll_timeout(host->ioaddr + CORE_DLL_STATUS,
783 dll_status, (dll_status & CORE_DDR_DLL_LOCK), 10, 1000);
784
785 if (ret == -ETIMEDOUT) {
786 pr_err("%s: %s: CM_DLL_SDC4 Calibration was not completed\n",
787 mmc_hostname(host->mmc), __func__);
788 goto out;
789 }
790
Ritesh Harjani764065e2015-05-13 14:14:45 +0530791 /*
792 * set CORE_PWRSAVE_DLL bit in CORE_VENDOR_SPEC3.
793 * when MCLK is gated OFF, it is not gated for less than 0.5us
794 * and MCLK must be switched on for at-least 1us before DATA
795 * starts coming. Controllers with 14lpp tech DLL cannot
796 * guarantee above requirement. So PWRSAVE_DLL should not be
797 * turned on for host controllers using this DLL.
798 */
799 if (!msm_host->use_14lpp_dll)
800 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC3)
801 | CORE_PWRSAVE_DLL),
802 host->ioaddr + CORE_VENDOR_SPEC3);
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700803 mb();
804out:
805 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
806 __func__, ret);
807 return ret;
808}
809
810static int sdhci_msm_hs400_dll_calibration(struct sdhci_host *host)
811{
812 int ret = 0;
813 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
814 struct sdhci_msm_host *msm_host = pltfm_host->priv;
815
816 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
817
818 /*
819 * Retuning in HS400 (DDR mode) will fail, just reset the
820 * tuning block and restore the saved tuning phase.
821 */
822 ret = msm_init_cm_dll(host);
823 if (ret)
824 goto out;
825
826 /* Set the selected phase in delay line hw block */
827 ret = msm_config_cm_dll_phase(host, msm_host->saved_tuning_phase);
828 if (ret)
829 goto out;
830
Krishna Konda0e8efba2014-06-23 14:50:38 -0700831 /* Write 1 to CMD_DAT_TRACK_SEL field in DLL_CONFIG */
832 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
833 | CORE_CMD_DAT_TRACK_SEL),
834 host->ioaddr + CORE_DLL_CONFIG);
835
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700836 if (msm_host->use_cdclp533)
837 /* Calibrate CDCLP533 DLL HW */
838 ret = sdhci_msm_cdclp533_calibration(host);
839 else
840 /* Calibrate CM_DLL_SDC4 HW */
841 ret = sdhci_msm_cm_dll_sdc4_calibration(host);
842out:
843 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
844 __func__, ret);
845 return ret;
846}
847
Krishna Konda96e6b112013-10-28 15:25:03 -0700848static void sdhci_msm_set_mmc_drv_type(struct sdhci_host *host, u32 opcode,
849 u8 drv_type)
850{
851 struct mmc_command cmd = {0};
852 struct mmc_request mrq = {NULL};
853 struct mmc_host *mmc = host->mmc;
854 u8 val = ((drv_type << 4) | 2);
855
856 cmd.opcode = MMC_SWITCH;
857 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
858 (EXT_CSD_HS_TIMING << 16) |
859 (val << 8) |
860 EXT_CSD_CMD_SET_NORMAL;
861 cmd.flags = MMC_CMD_AC | MMC_RSP_R1B;
862 /* 1 sec */
863 cmd.busy_timeout = 1000 * 1000;
864
865 memset(cmd.resp, 0, sizeof(cmd.resp));
866 cmd.retries = 3;
867
868 mrq.cmd = &cmd;
869 cmd.data = NULL;
870
871 mmc_wait_for_req(mmc, &mrq);
872 pr_debug("%s: %s: set card drive type to %d\n",
873 mmc_hostname(mmc), __func__,
874 drv_type);
875}
876
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700877int sdhci_msm_execute_tuning(struct sdhci_host *host, u32 opcode)
878{
879 unsigned long flags;
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530880 int tuning_seq_cnt = 3;
Krishna Konda96e6b112013-10-28 15:25:03 -0700881 u8 phase, *data_buf, tuned_phases[NUM_TUNING_PHASES], tuned_phase_cnt;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700882 const u32 *tuning_block_pattern = tuning_block_64;
883 int size = sizeof(tuning_block_64); /* Tuning pattern size in bytes */
884 int rc;
885 struct mmc_host *mmc = host->mmc;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530886 struct mmc_ios ios = host->mmc->ios;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700887 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
888 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Krishna Konda96e6b112013-10-28 15:25:03 -0700889 u8 drv_type = 0;
890 bool drv_type_changed = false;
891 struct mmc_card *card = host->mmc->card;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530892
893 /*
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700894 * Tuning is required for SDR104, HS200 and HS400 cards and
895 * if clock frequency is greater than 100MHz in these modes.
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530896 */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700897 if (host->clock <= CORE_FREQ_100MHZ ||
898 !((ios.timing == MMC_TIMING_MMC_HS400) ||
899 (ios.timing == MMC_TIMING_MMC_HS200) ||
900 (ios.timing == MMC_TIMING_UHS_SDR104)))
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530901 return 0;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700902
903 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700904
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700905 /* CDC/SDC4 DLL HW calibration is only required for HS400 mode*/
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700906 if (msm_host->tuning_done && !msm_host->calibration_done &&
907 (mmc->ios.timing == MMC_TIMING_MMC_HS400)) {
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700908 rc = sdhci_msm_hs400_dll_calibration(host);
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700909 spin_lock_irqsave(&host->lock, flags);
910 if (!rc)
911 msm_host->calibration_done = true;
912 spin_unlock_irqrestore(&host->lock, flags);
913 goto out;
914 }
915
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700916 spin_lock_irqsave(&host->lock, flags);
917
918 if ((opcode == MMC_SEND_TUNING_BLOCK_HS200) &&
919 (mmc->ios.bus_width == MMC_BUS_WIDTH_8)) {
920 tuning_block_pattern = tuning_block_128;
921 size = sizeof(tuning_block_128);
922 }
923 spin_unlock_irqrestore(&host->lock, flags);
924
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700925 data_buf = kmalloc(size, GFP_KERNEL);
926 if (!data_buf) {
927 rc = -ENOMEM;
928 goto out;
929 }
930
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530931retry:
Krishna Konda96e6b112013-10-28 15:25:03 -0700932 tuned_phase_cnt = 0;
933
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530934 /* first of all reset the tuning block */
935 rc = msm_init_cm_dll(host);
936 if (rc)
937 goto kfree;
938
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700939 phase = 0;
940 do {
941 struct mmc_command cmd = {0};
942 struct mmc_data data = {0};
943 struct mmc_request mrq = {
944 .cmd = &cmd,
945 .data = &data
946 };
947 struct scatterlist sg;
948
949 /* set the phase in delay line hw block */
950 rc = msm_config_cm_dll_phase(host, phase);
951 if (rc)
952 goto kfree;
953
954 cmd.opcode = opcode;
955 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
956
957 data.blksz = size;
958 data.blocks = 1;
959 data.flags = MMC_DATA_READ;
960 data.timeout_ns = 1000 * 1000 * 1000; /* 1 sec */
961
962 data.sg = &sg;
963 data.sg_len = 1;
964 sg_init_one(&sg, data_buf, size);
965 memset(data_buf, 0, size);
966 mmc_wait_for_req(mmc, &mrq);
967
968 if (!cmd.error && !data.error &&
969 !memcmp(data_buf, tuning_block_pattern, size)) {
970 /* tuning is successful at this tuning point */
971 tuned_phases[tuned_phase_cnt++] = phase;
Krishna Konda96e6b112013-10-28 15:25:03 -0700972 pr_debug("%s: %s: found *** good *** phase = %d\n",
973 mmc_hostname(mmc), __func__, phase);
974 } else {
975 pr_debug("%s: %s: found ## bad ## phase = %d\n",
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700976 mmc_hostname(mmc), __func__, phase);
977 }
978 } while (++phase < 16);
979
Sahitya Tummaladfdb4af2014-04-01 14:29:13 +0530980 if ((tuned_phase_cnt == NUM_TUNING_PHASES) &&
981 card && mmc_card_mmc(card)) {
Krishna Konda96e6b112013-10-28 15:25:03 -0700982 /*
983 * If all phases pass then its a problem. So change the card's
984 * drive type to a different value, if supported and repeat
985 * tuning until at least one phase fails. Then set the original
986 * drive type back.
987 *
988 * If all the phases still pass after trying all possible
989 * drive types, then one of those 16 phases will be picked.
990 * This is no different from what was going on before the
991 * modification to change drive type and retune.
992 */
993 pr_debug("%s: tuned phases count: %d\n", mmc_hostname(mmc),
994 tuned_phase_cnt);
995
996 /* set drive type to other value . default setting is 0x0 */
997 while (++drv_type <= MAX_DRV_TYPES_SUPPORTED_HS200) {
998 if (card->ext_csd.raw_driver_strength &
999 (1 << drv_type)) {
1000 sdhci_msm_set_mmc_drv_type(host, opcode,
1001 drv_type);
1002 if (!drv_type_changed)
1003 drv_type_changed = true;
1004 goto retry;
1005 }
1006 }
1007 }
1008
1009 /* reset drive type to default (50 ohm) if changed */
1010 if (drv_type_changed)
1011 sdhci_msm_set_mmc_drv_type(host, opcode, 0);
1012
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001013 if (tuned_phase_cnt) {
1014 rc = msm_find_most_appropriate_phase(host, tuned_phases,
1015 tuned_phase_cnt);
1016 if (rc < 0)
1017 goto kfree;
1018 else
1019 phase = (u8)rc;
1020
1021 /*
1022 * Finally set the selected phase in delay
1023 * line hw block.
1024 */
1025 rc = msm_config_cm_dll_phase(host, phase);
1026 if (rc)
1027 goto kfree;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07001028 msm_host->saved_tuning_phase = phase;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001029 pr_debug("%s: %s: finally setting the tuning phase to %d\n",
1030 mmc_hostname(mmc), __func__, phase);
1031 } else {
Sahitya Tummala9fe16532013-06-13 10:36:57 +05301032 if (--tuning_seq_cnt)
1033 goto retry;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001034 /* tuning failed */
1035 pr_err("%s: %s: no tuning point found\n",
1036 mmc_hostname(mmc), __func__);
Sahitya Tummala9fe16532013-06-13 10:36:57 +05301037 rc = -EIO;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001038 }
1039
1040kfree:
1041 kfree(data_buf);
1042out:
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07001043 spin_lock_irqsave(&host->lock, flags);
1044 if (!rc)
1045 msm_host->tuning_done = true;
1046 spin_unlock_irqrestore(&host->lock, flags);
1047 pr_debug("%s: Exit %s, err(%d)\n", mmc_hostname(mmc), __func__, rc);
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001048 return rc;
1049}
1050
Asutosh Das0ef24812012-12-18 16:14:02 +05301051static int sdhci_msm_setup_gpio(struct sdhci_msm_pltfm_data *pdata, bool enable)
1052{
1053 struct sdhci_msm_gpio_data *curr;
1054 int i, ret = 0;
1055
1056 curr = pdata->pin_data->gpio_data;
1057 for (i = 0; i < curr->size; i++) {
1058 if (!gpio_is_valid(curr->gpio[i].no)) {
1059 ret = -EINVAL;
1060 pr_err("%s: Invalid gpio = %d\n", __func__,
1061 curr->gpio[i].no);
1062 goto free_gpios;
1063 }
1064 if (enable) {
1065 ret = gpio_request(curr->gpio[i].no,
1066 curr->gpio[i].name);
1067 if (ret) {
1068 pr_err("%s: gpio_request(%d, %s) failed %d\n",
1069 __func__, curr->gpio[i].no,
1070 curr->gpio[i].name, ret);
1071 goto free_gpios;
1072 }
1073 curr->gpio[i].is_enabled = true;
1074 } else {
1075 gpio_free(curr->gpio[i].no);
1076 curr->gpio[i].is_enabled = false;
1077 }
1078 }
1079 return ret;
1080
1081free_gpios:
1082 for (i--; i >= 0; i--) {
1083 gpio_free(curr->gpio[i].no);
1084 curr->gpio[i].is_enabled = false;
1085 }
1086 return ret;
1087}
1088
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301089static int sdhci_msm_setup_pinctrl(struct sdhci_msm_pltfm_data *pdata,
1090 bool enable)
1091{
1092 int ret = 0;
1093
1094 if (enable)
1095 ret = pinctrl_select_state(pdata->pctrl_data->pctrl,
1096 pdata->pctrl_data->pins_active);
1097 else
1098 ret = pinctrl_select_state(pdata->pctrl_data->pctrl,
1099 pdata->pctrl_data->pins_sleep);
1100
1101 if (ret < 0)
1102 pr_err("%s state for pinctrl failed with %d\n",
1103 enable ? "Enabling" : "Disabling", ret);
1104
1105 return ret;
1106}
1107
Asutosh Das0ef24812012-12-18 16:14:02 +05301108static int sdhci_msm_setup_pins(struct sdhci_msm_pltfm_data *pdata, bool enable)
1109{
1110 int ret = 0;
1111
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301112 if (pdata->pin_cfg_sts == enable) {
Asutosh Das0ef24812012-12-18 16:14:02 +05301113 return 0;
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301114 } else if (pdata->pctrl_data) {
1115 ret = sdhci_msm_setup_pinctrl(pdata, enable);
1116 goto out;
1117 } else if (!pdata->pin_data) {
1118 return 0;
1119 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301120
Sahitya Tummala1cd7e072014-02-14 13:19:01 +05301121 if (pdata->pin_data->is_gpio)
1122 ret = sdhci_msm_setup_gpio(pdata, enable);
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301123out:
Asutosh Das0ef24812012-12-18 16:14:02 +05301124 if (!ret)
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301125 pdata->pin_cfg_sts = enable;
Asutosh Das0ef24812012-12-18 16:14:02 +05301126
1127 return ret;
1128}
1129
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301130static int sdhci_msm_dt_get_array(struct device *dev, const char *prop_name,
1131 u32 **out, int *len, u32 size)
1132{
1133 int ret = 0;
1134 struct device_node *np = dev->of_node;
1135 size_t sz;
1136 u32 *arr = NULL;
1137
1138 if (!of_get_property(np, prop_name, len)) {
1139 ret = -EINVAL;
1140 goto out;
1141 }
1142 sz = *len = *len / sizeof(*arr);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07001143 if (sz <= 0 || (size > 0 && (sz > size))) {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301144 dev_err(dev, "%s invalid size\n", prop_name);
1145 ret = -EINVAL;
1146 goto out;
1147 }
1148
1149 arr = devm_kzalloc(dev, sz * sizeof(*arr), GFP_KERNEL);
1150 if (!arr) {
1151 dev_err(dev, "%s failed allocating memory\n", prop_name);
1152 ret = -ENOMEM;
1153 goto out;
1154 }
1155
1156 ret = of_property_read_u32_array(np, prop_name, arr, sz);
1157 if (ret < 0) {
1158 dev_err(dev, "%s failed reading array %d\n", prop_name, ret);
1159 goto out;
1160 }
1161 *out = arr;
1162out:
1163 if (ret)
1164 *len = 0;
1165 return ret;
1166}
1167
Asutosh Das0ef24812012-12-18 16:14:02 +05301168#define MAX_PROP_SIZE 32
1169static int sdhci_msm_dt_parse_vreg_info(struct device *dev,
1170 struct sdhci_msm_reg_data **vreg_data, const char *vreg_name)
1171{
1172 int len, ret = 0;
1173 const __be32 *prop;
1174 char prop_name[MAX_PROP_SIZE];
1175 struct sdhci_msm_reg_data *vreg;
1176 struct device_node *np = dev->of_node;
1177
1178 snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", vreg_name);
1179 if (!of_parse_phandle(np, prop_name, 0)) {
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301180 dev_info(dev, "No vreg data found for %s\n", vreg_name);
Asutosh Das0ef24812012-12-18 16:14:02 +05301181 return ret;
1182 }
1183
1184 vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
1185 if (!vreg) {
1186 dev_err(dev, "No memory for vreg: %s\n", vreg_name);
1187 ret = -ENOMEM;
1188 return ret;
1189 }
1190
1191 vreg->name = vreg_name;
1192
1193 snprintf(prop_name, MAX_PROP_SIZE,
1194 "qcom,%s-always-on", vreg_name);
1195 if (of_get_property(np, prop_name, NULL))
1196 vreg->is_always_on = true;
1197
1198 snprintf(prop_name, MAX_PROP_SIZE,
1199 "qcom,%s-lpm-sup", vreg_name);
1200 if (of_get_property(np, prop_name, NULL))
1201 vreg->lpm_sup = true;
1202
1203 snprintf(prop_name, MAX_PROP_SIZE,
1204 "qcom,%s-voltage-level", vreg_name);
1205 prop = of_get_property(np, prop_name, &len);
1206 if (!prop || (len != (2 * sizeof(__be32)))) {
1207 dev_warn(dev, "%s %s property\n",
1208 prop ? "invalid format" : "no", prop_name);
1209 } else {
1210 vreg->low_vol_level = be32_to_cpup(&prop[0]);
1211 vreg->high_vol_level = be32_to_cpup(&prop[1]);
1212 }
1213
1214 snprintf(prop_name, MAX_PROP_SIZE,
1215 "qcom,%s-current-level", vreg_name);
1216 prop = of_get_property(np, prop_name, &len);
1217 if (!prop || (len != (2 * sizeof(__be32)))) {
1218 dev_warn(dev, "%s %s property\n",
1219 prop ? "invalid format" : "no", prop_name);
1220 } else {
1221 vreg->lpm_uA = be32_to_cpup(&prop[0]);
1222 vreg->hpm_uA = be32_to_cpup(&prop[1]);
1223 }
1224
1225 *vreg_data = vreg;
1226 dev_dbg(dev, "%s: %s %s vol=[%d %d]uV, curr=[%d %d]uA\n",
1227 vreg->name, vreg->is_always_on ? "always_on," : "",
1228 vreg->lpm_sup ? "lpm_sup," : "", vreg->low_vol_level,
1229 vreg->high_vol_level, vreg->lpm_uA, vreg->hpm_uA);
1230
1231 return ret;
1232}
1233
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301234static int sdhci_msm_parse_pinctrl_info(struct device *dev,
1235 struct sdhci_msm_pltfm_data *pdata)
1236{
1237 struct sdhci_pinctrl_data *pctrl_data;
1238 struct pinctrl *pctrl;
1239 int ret = 0;
1240
1241 /* Try to obtain pinctrl handle */
1242 pctrl = devm_pinctrl_get(dev);
1243 if (IS_ERR(pctrl)) {
1244 ret = PTR_ERR(pctrl);
1245 goto out;
1246 }
1247 pctrl_data = devm_kzalloc(dev, sizeof(*pctrl_data), GFP_KERNEL);
1248 if (!pctrl_data) {
1249 dev_err(dev, "No memory for sdhci_pinctrl_data\n");
1250 ret = -ENOMEM;
1251 goto out;
1252 }
1253 pctrl_data->pctrl = pctrl;
1254 /* Look-up and keep the states handy to be used later */
1255 pctrl_data->pins_active = pinctrl_lookup_state(
1256 pctrl_data->pctrl, "active");
1257 if (IS_ERR(pctrl_data->pins_active)) {
1258 ret = PTR_ERR(pctrl_data->pins_active);
1259 dev_err(dev, "Could not get active pinstates, err:%d\n", ret);
1260 goto out;
1261 }
1262 pctrl_data->pins_sleep = pinctrl_lookup_state(
1263 pctrl_data->pctrl, "sleep");
1264 if (IS_ERR(pctrl_data->pins_sleep)) {
1265 ret = PTR_ERR(pctrl_data->pins_sleep);
1266 dev_err(dev, "Could not get sleep pinstates, err:%d\n", ret);
1267 goto out;
1268 }
1269 pdata->pctrl_data = pctrl_data;
1270out:
1271 return ret;
1272}
1273
Asutosh Das0ef24812012-12-18 16:14:02 +05301274#define GPIO_NAME_MAX_LEN 32
1275static int sdhci_msm_dt_parse_gpio_info(struct device *dev,
1276 struct sdhci_msm_pltfm_data *pdata)
1277{
1278 int ret = 0, cnt, i;
1279 struct sdhci_msm_pin_data *pin_data;
1280 struct device_node *np = dev->of_node;
1281
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301282 ret = sdhci_msm_parse_pinctrl_info(dev, pdata);
1283 if (!ret) {
1284 goto out;
1285 } else if (ret == -EPROBE_DEFER) {
1286 dev_err(dev, "Pinctrl framework not registered, err:%d\n", ret);
1287 goto out;
1288 } else {
1289 dev_err(dev, "Parsing Pinctrl failed with %d, falling back on GPIO lib\n",
1290 ret);
Sahitya Tummala1cd7e072014-02-14 13:19:01 +05301291 ret = 0;
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301292 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301293 pin_data = devm_kzalloc(dev, sizeof(*pin_data), GFP_KERNEL);
1294 if (!pin_data) {
1295 dev_err(dev, "No memory for pin_data\n");
1296 ret = -ENOMEM;
1297 goto out;
1298 }
1299
1300 cnt = of_gpio_count(np);
1301 if (cnt > 0) {
1302 pin_data->gpio_data = devm_kzalloc(dev,
1303 sizeof(struct sdhci_msm_gpio_data), GFP_KERNEL);
1304 if (!pin_data->gpio_data) {
1305 dev_err(dev, "No memory for gpio_data\n");
1306 ret = -ENOMEM;
1307 goto out;
1308 }
1309 pin_data->gpio_data->size = cnt;
1310 pin_data->gpio_data->gpio = devm_kzalloc(dev, cnt *
1311 sizeof(struct sdhci_msm_gpio), GFP_KERNEL);
1312
1313 if (!pin_data->gpio_data->gpio) {
1314 dev_err(dev, "No memory for gpio\n");
1315 ret = -ENOMEM;
1316 goto out;
1317 }
1318
1319 for (i = 0; i < cnt; i++) {
1320 const char *name = NULL;
1321 char result[GPIO_NAME_MAX_LEN];
1322 pin_data->gpio_data->gpio[i].no = of_get_gpio(np, i);
1323 of_property_read_string_index(np,
1324 "qcom,gpio-names", i, &name);
1325
1326 snprintf(result, GPIO_NAME_MAX_LEN, "%s-%s",
1327 dev_name(dev), name ? name : "?");
1328 pin_data->gpio_data->gpio[i].name = result;
1329 dev_dbg(dev, "%s: gpio[%s] = %d\n", __func__,
1330 pin_data->gpio_data->gpio[i].name,
1331 pin_data->gpio_data->gpio[i].no);
Asutosh Das0ef24812012-12-18 16:14:02 +05301332 }
1333 }
Sahitya Tummala1cd7e072014-02-14 13:19:01 +05301334 pdata->pin_data = pin_data;
Asutosh Das0ef24812012-12-18 16:14:02 +05301335out:
1336 if (ret)
1337 dev_err(dev, "%s failed with err %d\n", __func__, ret);
1338 return ret;
1339}
1340
Maya Erez994cf2f2014-10-21 20:22:04 +03001341#ifdef CONFIG_SMP
1342static void sdhci_msm_populate_affinity_type(struct sdhci_msm_pltfm_data *pdata,
1343 struct device_node *np)
1344{
1345 const char *cpu_affinity = NULL;
1346
1347 pdata->cpu_affinity_type = PM_QOS_REQ_AFFINE_IRQ;
1348 if (!of_property_read_string(np, "qcom,cpu-affinity",
1349 &cpu_affinity)) {
1350 if (!strcmp(cpu_affinity, "all_cores"))
1351 pdata->cpu_affinity_type = PM_QOS_REQ_ALL_CORES;
1352 else if (!strcmp(cpu_affinity, "affine_cores"))
1353 pdata->cpu_affinity_type = PM_QOS_REQ_AFFINE_CORES;
1354 else if (!strcmp(cpu_affinity, "affine_irq"))
1355 pdata->cpu_affinity_type = PM_QOS_REQ_AFFINE_IRQ;
1356 }
1357}
1358#else
1359static void sdhci_msm_populate_affinity_type(struct sdhci_msm_pltfm_data *pdata,
1360 struct device_node *np)
1361{
1362}
1363#endif
1364
Asutosh Das0ef24812012-12-18 16:14:02 +05301365/* Parse platform data */
Dov Levenglickc9033ab2015-03-10 16:00:56 +02001366static
1367struct sdhci_msm_pltfm_data *sdhci_msm_populate_pdata(struct device *dev,
1368 struct sdhci_msm_host *msm_host)
Asutosh Das0ef24812012-12-18 16:14:02 +05301369{
1370 struct sdhci_msm_pltfm_data *pdata = NULL;
1371 struct device_node *np = dev->of_node;
1372 u32 bus_width = 0;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05301373 u32 cpu_dma_latency;
Asutosh Das0ef24812012-12-18 16:14:02 +05301374 int len, i;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301375 int clk_table_len;
1376 u32 *clk_table = NULL;
Sujit Reddy Thumma1958d3d2013-06-03 09:54:32 +05301377 enum of_gpio_flags flags = OF_GPIO_ACTIVE_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05301378
1379 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1380 if (!pdata) {
1381 dev_err(dev, "failed to allocate memory for platform data\n");
1382 goto out;
1383 }
1384
Sujit Reddy Thumma1958d3d2013-06-03 09:54:32 +05301385 pdata->status_gpio = of_get_named_gpio_flags(np, "cd-gpios", 0, &flags);
1386 if (gpio_is_valid(pdata->status_gpio) & !(flags & OF_GPIO_ACTIVE_LOW))
1387 pdata->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
Sahitya Tummala581df132013-03-12 14:57:46 +05301388
Asutosh Das0ef24812012-12-18 16:14:02 +05301389 of_property_read_u32(np, "qcom,bus-width", &bus_width);
1390 if (bus_width == 8)
1391 pdata->mmc_bus_width = MMC_CAP_8_BIT_DATA;
1392 else if (bus_width == 4)
1393 pdata->mmc_bus_width = MMC_CAP_4_BIT_DATA;
1394 else {
1395 dev_notice(dev, "invalid bus-width, default to 1-bit mode\n");
1396 pdata->mmc_bus_width = 0;
1397 }
1398
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05301399 if (!of_property_read_u32(np, "qcom,cpu-dma-latency-us",
1400 &cpu_dma_latency))
1401 pdata->cpu_dma_latency_us = cpu_dma_latency;
Asutosh Das598a4d42014-02-05 16:38:23 +05301402 else
1403 pdata->cpu_dma_latency_us = MSM_MMC_DEFAULT_CPU_DMA_LATENCY;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301404 if (sdhci_msm_dt_get_array(dev, "qcom,clk-rates",
1405 &clk_table, &clk_table_len, 0)) {
1406 dev_err(dev, "failed parsing supported clock rates\n");
1407 goto out;
1408 }
1409 if (!clk_table || !clk_table_len) {
1410 dev_err(dev, "Invalid clock table\n");
1411 goto out;
1412 }
1413 pdata->sup_clk_table = clk_table;
1414 pdata->sup_clk_cnt = clk_table_len;
1415
Asutosh Das0ef24812012-12-18 16:14:02 +05301416 pdata->vreg_data = devm_kzalloc(dev, sizeof(struct
1417 sdhci_msm_slot_reg_data),
1418 GFP_KERNEL);
1419 if (!pdata->vreg_data) {
1420 dev_err(dev, "failed to allocate memory for vreg data\n");
1421 goto out;
1422 }
1423
1424 if (sdhci_msm_dt_parse_vreg_info(dev, &pdata->vreg_data->vdd_data,
1425 "vdd")) {
1426 dev_err(dev, "failed parsing vdd data\n");
1427 goto out;
1428 }
1429 if (sdhci_msm_dt_parse_vreg_info(dev,
1430 &pdata->vreg_data->vdd_io_data,
1431 "vdd-io")) {
1432 dev_err(dev, "failed parsing vdd-io data\n");
1433 goto out;
1434 }
1435
1436 if (sdhci_msm_dt_parse_gpio_info(dev, pdata)) {
1437 dev_err(dev, "failed parsing gpio data\n");
1438 goto out;
1439 }
1440
Asutosh Das0ef24812012-12-18 16:14:02 +05301441 len = of_property_count_strings(np, "qcom,bus-speed-mode");
1442
1443 for (i = 0; i < len; i++) {
1444 const char *name = NULL;
1445
1446 of_property_read_string_index(np,
1447 "qcom,bus-speed-mode", i, &name);
1448 if (!name)
1449 continue;
1450
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07001451 if (!strncmp(name, "HS400_1p8v", sizeof("HS400_1p8v")))
1452 pdata->caps2 |= MMC_CAP2_HS400_1_8V;
1453 else if (!strncmp(name, "HS400_1p2v", sizeof("HS400_1p2v")))
1454 pdata->caps2 |= MMC_CAP2_HS400_1_2V;
1455 else if (!strncmp(name, "HS200_1p8v", sizeof("HS200_1p8v")))
Asutosh Das0ef24812012-12-18 16:14:02 +05301456 pdata->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
1457 else if (!strncmp(name, "HS200_1p2v", sizeof("HS200_1p2v")))
1458 pdata->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
1459 else if (!strncmp(name, "DDR_1p8v", sizeof("DDR_1p8v")))
1460 pdata->caps |= MMC_CAP_1_8V_DDR
1461 | MMC_CAP_UHS_DDR50;
1462 else if (!strncmp(name, "DDR_1p2v", sizeof("DDR_1p2v")))
1463 pdata->caps |= MMC_CAP_1_2V_DDR
1464 | MMC_CAP_UHS_DDR50;
1465 }
1466
1467 if (of_get_property(np, "qcom,nonremovable", NULL))
1468 pdata->nonremovable = true;
1469
Guoping Yuf7c91332014-08-20 16:56:18 +08001470 if (of_get_property(np, "qcom,nonhotplug", NULL))
1471 pdata->nonhotplug = true;
1472
Venkat Gopalakrishnan9a62e042015-03-03 16:14:55 -08001473 pdata->largeaddressbus =
1474 of_property_read_bool(np, "qcom,large-address-bus");
1475
Maya Erez994cf2f2014-10-21 20:22:04 +03001476 sdhci_msm_populate_affinity_type(pdata, np);
1477
Dov Levenglickc9033ab2015-03-10 16:00:56 +02001478 if (of_property_read_bool(np, "qcom,wakeup-on-idle"))
1479 msm_host->mmc->wakeup_on_idle = true;
1480
Asutosh Das0ef24812012-12-18 16:14:02 +05301481 return pdata;
1482out:
1483 return NULL;
1484}
1485
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301486/* Returns required bandwidth in Bytes per Sec */
1487static unsigned int sdhci_get_bw_required(struct sdhci_host *host,
1488 struct mmc_ios *ios)
1489{
Sahitya Tummala2886c922013-04-03 18:03:31 +05301490 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1491 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1492
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301493 unsigned int bw;
1494
Sahitya Tummala2886c922013-04-03 18:03:31 +05301495 bw = msm_host->clk_rate;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301496 /*
1497 * For DDR mode, SDCC controller clock will be at
1498 * the double rate than the actual clock that goes to card.
1499 */
1500 if (ios->bus_width == MMC_BUS_WIDTH_4)
1501 bw /= 2;
1502 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1503 bw /= 8;
1504
1505 return bw;
1506}
1507
1508static int sdhci_msm_bus_get_vote_for_bw(struct sdhci_msm_host *host,
1509 unsigned int bw)
1510{
1511 unsigned int *table = host->pdata->voting_data->bw_vecs;
1512 unsigned int size = host->pdata->voting_data->bw_vecs_size;
1513 int i;
1514
1515 if (host->msm_bus_vote.is_max_bw_needed && bw)
1516 return host->msm_bus_vote.max_bw_vote;
1517
1518 for (i = 0; i < size; i++) {
1519 if (bw <= table[i])
1520 break;
1521 }
1522
1523 if (i && (i == size))
1524 i--;
1525
1526 return i;
1527}
1528
1529/*
1530 * This function must be called with host lock acquired.
1531 * Caller of this function should also ensure that msm bus client
1532 * handle is not null.
1533 */
1534static inline int sdhci_msm_bus_set_vote(struct sdhci_msm_host *msm_host,
1535 int vote,
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301536 unsigned long *flags)
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301537{
1538 struct sdhci_host *host = platform_get_drvdata(msm_host->pdev);
1539 int rc = 0;
1540
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301541 BUG_ON(!flags);
1542
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301543 if (vote != msm_host->msm_bus_vote.curr_vote) {
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301544 spin_unlock_irqrestore(&host->lock, *flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301545 rc = msm_bus_scale_client_update_request(
1546 msm_host->msm_bus_vote.client_handle, vote);
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301547 spin_lock_irqsave(&host->lock, *flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301548 if (rc) {
1549 pr_err("%s: msm_bus_scale_client_update_request() failed: bus_client_handle=0x%x, vote=%d, err=%d\n",
1550 mmc_hostname(host->mmc),
1551 msm_host->msm_bus_vote.client_handle, vote, rc);
1552 goto out;
1553 }
1554 msm_host->msm_bus_vote.curr_vote = vote;
1555 }
1556out:
1557 return rc;
1558}
1559
1560/*
1561 * Internal work. Work to set 0 bandwidth for msm bus.
1562 */
1563static void sdhci_msm_bus_work(struct work_struct *work)
1564{
1565 struct sdhci_msm_host *msm_host;
1566 struct sdhci_host *host;
1567 unsigned long flags;
1568
1569 msm_host = container_of(work, struct sdhci_msm_host,
1570 msm_bus_vote.vote_work.work);
1571 host = platform_get_drvdata(msm_host->pdev);
1572
1573 if (!msm_host->msm_bus_vote.client_handle)
1574 return;
1575
1576 spin_lock_irqsave(&host->lock, flags);
1577 /* don't vote for 0 bandwidth if any request is in progress */
1578 if (!host->mrq) {
1579 sdhci_msm_bus_set_vote(msm_host,
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301580 msm_host->msm_bus_vote.min_bw_vote, &flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301581 } else
1582 pr_warning("%s: %s: Transfer in progress. skipping bus voting to 0 bandwidth\n",
1583 mmc_hostname(host->mmc), __func__);
1584 spin_unlock_irqrestore(&host->lock, flags);
1585}
1586
1587/*
1588 * This function cancels any scheduled delayed work and sets the bus
1589 * vote based on bw (bandwidth) argument.
1590 */
1591static void sdhci_msm_bus_cancel_work_and_set_vote(struct sdhci_host *host,
1592 unsigned int bw)
1593{
1594 int vote;
1595 unsigned long flags;
1596 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1597 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1598
1599 cancel_delayed_work_sync(&msm_host->msm_bus_vote.vote_work);
1600 spin_lock_irqsave(&host->lock, flags);
1601 vote = sdhci_msm_bus_get_vote_for_bw(msm_host, bw);
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301602 sdhci_msm_bus_set_vote(msm_host, vote, &flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301603 spin_unlock_irqrestore(&host->lock, flags);
1604}
1605
1606#define MSM_MMC_BUS_VOTING_DELAY 200 /* msecs */
1607
1608/* This function queues a work which will set the bandwidth requiement to 0 */
1609static void sdhci_msm_bus_queue_work(struct sdhci_host *host)
1610{
1611 unsigned long flags;
1612 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1613 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1614
1615 spin_lock_irqsave(&host->lock, flags);
1616 if (msm_host->msm_bus_vote.min_bw_vote !=
1617 msm_host->msm_bus_vote.curr_vote)
1618 queue_delayed_work(system_wq,
1619 &msm_host->msm_bus_vote.vote_work,
1620 msecs_to_jiffies(MSM_MMC_BUS_VOTING_DELAY));
1621 spin_unlock_irqrestore(&host->lock, flags);
1622}
1623
1624static int sdhci_msm_bus_register(struct sdhci_msm_host *host,
1625 struct platform_device *pdev)
1626{
1627 int rc = 0;
1628 struct msm_bus_scale_pdata *bus_pdata;
1629
1630 struct sdhci_msm_bus_voting_data *data;
1631 struct device *dev = &pdev->dev;
1632
1633 data = devm_kzalloc(dev,
1634 sizeof(struct sdhci_msm_bus_voting_data), GFP_KERNEL);
1635 if (!data) {
1636 dev_err(&pdev->dev,
1637 "%s: failed to allocate memory\n", __func__);
1638 rc = -ENOMEM;
1639 goto out;
1640 }
1641 data->bus_pdata = msm_bus_cl_get_pdata(pdev);
1642 if (data->bus_pdata) {
1643 rc = sdhci_msm_dt_get_array(dev, "qcom,bus-bw-vectors-bps",
1644 &data->bw_vecs, &data->bw_vecs_size, 0);
1645 if (rc) {
1646 dev_err(&pdev->dev,
1647 "%s: Failed to get bus-bw-vectors-bps\n",
1648 __func__);
1649 goto out;
1650 }
1651 host->pdata->voting_data = data;
1652 }
1653 if (host->pdata->voting_data &&
1654 host->pdata->voting_data->bus_pdata &&
1655 host->pdata->voting_data->bw_vecs &&
1656 host->pdata->voting_data->bw_vecs_size) {
1657
1658 bus_pdata = host->pdata->voting_data->bus_pdata;
1659 host->msm_bus_vote.client_handle =
1660 msm_bus_scale_register_client(bus_pdata);
1661 if (!host->msm_bus_vote.client_handle) {
1662 dev_err(&pdev->dev, "msm_bus_scale_register_client()\n");
1663 rc = -EFAULT;
1664 goto out;
1665 }
1666 /* cache the vote index for minimum and maximum bandwidth */
1667 host->msm_bus_vote.min_bw_vote =
1668 sdhci_msm_bus_get_vote_for_bw(host, 0);
1669 host->msm_bus_vote.max_bw_vote =
1670 sdhci_msm_bus_get_vote_for_bw(host, UINT_MAX);
1671 } else {
1672 devm_kfree(dev, data);
1673 }
1674
1675out:
1676 return rc;
1677}
1678
1679static void sdhci_msm_bus_unregister(struct sdhci_msm_host *host)
1680{
1681 if (host->msm_bus_vote.client_handle)
1682 msm_bus_scale_unregister_client(
1683 host->msm_bus_vote.client_handle);
1684}
1685
1686static void sdhci_msm_bus_voting(struct sdhci_host *host, u32 enable)
1687{
1688 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1689 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1690 struct mmc_ios *ios = &host->mmc->ios;
1691 unsigned int bw;
1692
1693 if (!msm_host->msm_bus_vote.client_handle)
1694 return;
1695
1696 bw = sdhci_get_bw_required(host, ios);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301697 if (enable) {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301698 sdhci_msm_bus_cancel_work_and_set_vote(host, bw);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301699 } else {
1700 /*
1701 * If clock gating is enabled, then remove the vote
1702 * immediately because clocks will be disabled only
1703 * after SDHCI_MSM_MMC_CLK_GATE_DELAY and thus no
1704 * additional delay is required to remove the bus vote.
1705 */
1706#ifdef CONFIG_MMC_CLKGATE
1707 if (host->mmc->clkgate_delay)
1708 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
1709 else
1710#endif
1711 sdhci_msm_bus_queue_work(host);
1712 }
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301713}
1714
Asutosh Das0ef24812012-12-18 16:14:02 +05301715/* Regulator utility functions */
1716static int sdhci_msm_vreg_init_reg(struct device *dev,
1717 struct sdhci_msm_reg_data *vreg)
1718{
1719 int ret = 0;
1720
1721 /* check if regulator is already initialized? */
1722 if (vreg->reg)
1723 goto out;
1724
1725 /* Get the regulator handle */
1726 vreg->reg = devm_regulator_get(dev, vreg->name);
1727 if (IS_ERR(vreg->reg)) {
1728 ret = PTR_ERR(vreg->reg);
1729 pr_err("%s: devm_regulator_get(%s) failed. ret=%d\n",
1730 __func__, vreg->name, ret);
1731 goto out;
1732 }
1733
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301734 if (regulator_count_voltages(vreg->reg) > 0) {
1735 vreg->set_voltage_sup = true;
1736 /* sanity check */
1737 if (!vreg->high_vol_level || !vreg->hpm_uA) {
1738 pr_err("%s: %s invalid constraints specified\n",
1739 __func__, vreg->name);
1740 ret = -EINVAL;
1741 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301742 }
1743
1744out:
1745 return ret;
1746}
1747
1748static void sdhci_msm_vreg_deinit_reg(struct sdhci_msm_reg_data *vreg)
1749{
1750 if (vreg->reg)
1751 devm_regulator_put(vreg->reg);
1752}
1753
1754static int sdhci_msm_vreg_set_optimum_mode(struct sdhci_msm_reg_data
1755 *vreg, int uA_load)
1756{
1757 int ret = 0;
1758
1759 /*
1760 * regulators that do not support regulator_set_voltage also
1761 * do not support regulator_set_optimum_mode
1762 */
1763 if (vreg->set_voltage_sup) {
1764 ret = regulator_set_load(vreg->reg, uA_load);
1765 if (ret < 0)
1766 pr_err("%s: regulator_set_load(reg=%s,uA_load=%d) failed. ret=%d\n",
1767 __func__, vreg->name, uA_load, ret);
1768 else
1769 /*
1770 * regulator_set_load() can return non zero
1771 * value even for success case.
1772 */
1773 ret = 0;
1774 }
1775 return ret;
1776}
1777
1778static int sdhci_msm_vreg_set_voltage(struct sdhci_msm_reg_data *vreg,
1779 int min_uV, int max_uV)
1780{
1781 int ret = 0;
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301782 if (vreg->set_voltage_sup) {
1783 ret = regulator_set_voltage(vreg->reg, min_uV, max_uV);
1784 if (ret) {
1785 pr_err("%s: regulator_set_voltage(%s)failed. min_uV=%d,max_uV=%d,ret=%d\n",
Asutosh Das0ef24812012-12-18 16:14:02 +05301786 __func__, vreg->name, min_uV, max_uV, ret);
1787 }
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301788 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301789
1790 return ret;
1791}
1792
1793static int sdhci_msm_vreg_enable(struct sdhci_msm_reg_data *vreg)
1794{
1795 int ret = 0;
1796
1797 /* Put regulator in HPM (high power mode) */
1798 ret = sdhci_msm_vreg_set_optimum_mode(vreg, vreg->hpm_uA);
1799 if (ret < 0)
1800 return ret;
1801
1802 if (!vreg->is_enabled) {
1803 /* Set voltage level */
1804 ret = sdhci_msm_vreg_set_voltage(vreg, vreg->high_vol_level,
1805 vreg->high_vol_level);
1806 if (ret)
1807 return ret;
1808 }
1809 ret = regulator_enable(vreg->reg);
1810 if (ret) {
1811 pr_err("%s: regulator_enable(%s) failed. ret=%d\n",
1812 __func__, vreg->name, ret);
1813 return ret;
1814 }
1815 vreg->is_enabled = true;
1816 return ret;
1817}
1818
1819static int sdhci_msm_vreg_disable(struct sdhci_msm_reg_data *vreg)
1820{
1821 int ret = 0;
1822
1823 /* Never disable regulator marked as always_on */
1824 if (vreg->is_enabled && !vreg->is_always_on) {
1825 ret = regulator_disable(vreg->reg);
1826 if (ret) {
1827 pr_err("%s: regulator_disable(%s) failed. ret=%d\n",
1828 __func__, vreg->name, ret);
1829 goto out;
1830 }
1831 vreg->is_enabled = false;
1832
1833 ret = sdhci_msm_vreg_set_optimum_mode(vreg, 0);
1834 if (ret < 0)
1835 goto out;
1836
1837 /* Set min. voltage level to 0 */
1838 ret = sdhci_msm_vreg_set_voltage(vreg, 0, vreg->high_vol_level);
1839 if (ret)
1840 goto out;
1841 } else if (vreg->is_enabled && vreg->is_always_on) {
1842 if (vreg->lpm_sup) {
1843 /* Put always_on regulator in LPM (low power mode) */
1844 ret = sdhci_msm_vreg_set_optimum_mode(vreg,
1845 vreg->lpm_uA);
1846 if (ret < 0)
1847 goto out;
1848 }
1849 }
1850out:
1851 return ret;
1852}
1853
1854static int sdhci_msm_setup_vreg(struct sdhci_msm_pltfm_data *pdata,
1855 bool enable, bool is_init)
1856{
1857 int ret = 0, i;
1858 struct sdhci_msm_slot_reg_data *curr_slot;
1859 struct sdhci_msm_reg_data *vreg_table[2];
1860
1861 curr_slot = pdata->vreg_data;
1862 if (!curr_slot) {
1863 pr_debug("%s: vreg info unavailable,assuming the slot is powered by always on domain\n",
1864 __func__);
1865 goto out;
1866 }
1867
1868 vreg_table[0] = curr_slot->vdd_data;
1869 vreg_table[1] = curr_slot->vdd_io_data;
1870
1871 for (i = 0; i < ARRAY_SIZE(vreg_table); i++) {
1872 if (vreg_table[i]) {
1873 if (enable)
1874 ret = sdhci_msm_vreg_enable(vreg_table[i]);
1875 else
1876 ret = sdhci_msm_vreg_disable(vreg_table[i]);
1877 if (ret)
1878 goto out;
1879 }
1880 }
1881out:
1882 return ret;
1883}
1884
1885/*
1886 * Reset vreg by ensuring it is off during probe. A call
1887 * to enable vreg is needed to balance disable vreg
1888 */
1889static int sdhci_msm_vreg_reset(struct sdhci_msm_pltfm_data *pdata)
1890{
1891 int ret;
1892
1893 ret = sdhci_msm_setup_vreg(pdata, 1, true);
1894 if (ret)
1895 return ret;
1896 ret = sdhci_msm_setup_vreg(pdata, 0, true);
1897 return ret;
1898}
1899
1900/* This init function should be called only once for each SDHC slot */
1901static int sdhci_msm_vreg_init(struct device *dev,
1902 struct sdhci_msm_pltfm_data *pdata,
1903 bool is_init)
1904{
1905 int ret = 0;
1906 struct sdhci_msm_slot_reg_data *curr_slot;
1907 struct sdhci_msm_reg_data *curr_vdd_reg, *curr_vdd_io_reg;
1908
1909 curr_slot = pdata->vreg_data;
1910 if (!curr_slot)
1911 goto out;
1912
1913 curr_vdd_reg = curr_slot->vdd_data;
1914 curr_vdd_io_reg = curr_slot->vdd_io_data;
1915
1916 if (!is_init)
1917 /* Deregister all regulators from regulator framework */
1918 goto vdd_io_reg_deinit;
1919
1920 /*
1921 * Get the regulator handle from voltage regulator framework
1922 * and then try to set the voltage level for the regulator
1923 */
1924 if (curr_vdd_reg) {
1925 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_reg);
1926 if (ret)
1927 goto out;
1928 }
1929 if (curr_vdd_io_reg) {
1930 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_io_reg);
1931 if (ret)
1932 goto vdd_reg_deinit;
1933 }
1934 ret = sdhci_msm_vreg_reset(pdata);
1935 if (ret)
1936 dev_err(dev, "vreg reset failed (%d)\n", ret);
1937 goto out;
1938
1939vdd_io_reg_deinit:
1940 if (curr_vdd_io_reg)
1941 sdhci_msm_vreg_deinit_reg(curr_vdd_io_reg);
1942vdd_reg_deinit:
1943 if (curr_vdd_reg)
1944 sdhci_msm_vreg_deinit_reg(curr_vdd_reg);
1945out:
1946 return ret;
1947}
1948
1949
1950static int sdhci_msm_set_vdd_io_vol(struct sdhci_msm_pltfm_data *pdata,
1951 enum vdd_io_level level,
1952 unsigned int voltage_level)
1953{
1954 int ret = 0;
1955 int set_level;
1956 struct sdhci_msm_reg_data *vdd_io_reg;
1957
1958 if (!pdata->vreg_data)
1959 return ret;
1960
1961 vdd_io_reg = pdata->vreg_data->vdd_io_data;
1962 if (vdd_io_reg && vdd_io_reg->is_enabled) {
1963 switch (level) {
1964 case VDD_IO_LOW:
1965 set_level = vdd_io_reg->low_vol_level;
1966 break;
1967 case VDD_IO_HIGH:
1968 set_level = vdd_io_reg->high_vol_level;
1969 break;
1970 case VDD_IO_SET_LEVEL:
1971 set_level = voltage_level;
1972 break;
1973 default:
1974 pr_err("%s: invalid argument level = %d",
1975 __func__, level);
1976 ret = -EINVAL;
1977 return ret;
1978 }
1979 ret = sdhci_msm_vreg_set_voltage(vdd_io_reg, set_level,
1980 set_level);
1981 }
1982 return ret;
1983}
1984
1985static irqreturn_t sdhci_msm_pwr_irq(int irq, void *data)
1986{
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001987 struct sdhci_host *host = (struct sdhci_host *)data;
1988 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1989 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Das0ef24812012-12-18 16:14:02 +05301990 u8 irq_status = 0;
1991 u8 irq_ack = 0;
1992 int ret = 0;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301993 int pwr_state = 0, io_level = 0;
1994 unsigned long flags;
Asutosh Das0ef24812012-12-18 16:14:02 +05301995
1996 irq_status = readb_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
1997 pr_debug("%s: Received IRQ(%d), status=0x%x\n",
1998 mmc_hostname(msm_host->mmc), irq, irq_status);
1999
2000 /* Clear the interrupt */
2001 writeb_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
2002 /*
2003 * SDHC has core_mem and hc_mem device memory and these memory
2004 * addresses do not fall within 1KB region. Hence, any update to
2005 * core_mem address space would require an mb() to ensure this gets
2006 * completed before its next update to registers within hc_mem.
2007 */
2008 mb();
2009
2010 /* Handle BUS ON/OFF*/
2011 if (irq_status & CORE_PWRCTL_BUS_ON) {
2012 ret = sdhci_msm_setup_vreg(msm_host->pdata, true, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302013 if (!ret) {
Asutosh Das0ef24812012-12-18 16:14:02 +05302014 ret = sdhci_msm_setup_pins(msm_host->pdata, true);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302015 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
2016 VDD_IO_HIGH, 0);
2017 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302018 if (ret)
2019 irq_ack |= CORE_PWRCTL_BUS_FAIL;
2020 else
2021 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302022
2023 pwr_state = REQ_BUS_ON;
2024 io_level = REQ_IO_HIGH;
Asutosh Das0ef24812012-12-18 16:14:02 +05302025 }
2026 if (irq_status & CORE_PWRCTL_BUS_OFF) {
2027 ret = sdhci_msm_setup_vreg(msm_host->pdata, false, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302028 if (!ret) {
Asutosh Das0ef24812012-12-18 16:14:02 +05302029 ret = sdhci_msm_setup_pins(msm_host->pdata, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302030 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
2031 VDD_IO_LOW, 0);
2032 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302033 if (ret)
2034 irq_ack |= CORE_PWRCTL_BUS_FAIL;
2035 else
2036 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302037
2038 pwr_state = REQ_BUS_OFF;
2039 io_level = REQ_IO_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05302040 }
2041 /* Handle IO LOW/HIGH */
2042 if (irq_status & CORE_PWRCTL_IO_LOW) {
2043 /* Switch voltage Low */
2044 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_LOW, 0);
2045 if (ret)
2046 irq_ack |= CORE_PWRCTL_IO_FAIL;
2047 else
2048 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302049
2050 io_level = REQ_IO_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05302051 }
2052 if (irq_status & CORE_PWRCTL_IO_HIGH) {
2053 /* Switch voltage High */
2054 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_HIGH, 0);
2055 if (ret)
2056 irq_ack |= CORE_PWRCTL_IO_FAIL;
2057 else
2058 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302059
2060 io_level = REQ_IO_HIGH;
Asutosh Das0ef24812012-12-18 16:14:02 +05302061 }
2062
2063 /* ACK status to the core */
2064 writeb_relaxed(irq_ack, (msm_host->core_mem + CORE_PWRCTL_CTL));
2065 /*
2066 * SDHC has core_mem and hc_mem device memory and these memory
2067 * addresses do not fall within 1KB region. Hence, any update to
2068 * core_mem address space would require an mb() to ensure this gets
2069 * completed before its next update to registers within hc_mem.
2070 */
2071 mb();
2072
Krishna Konda46fd1432014-10-30 21:13:27 -07002073 if ((io_level & REQ_IO_HIGH) && (msm_host->caps_0 & CORE_3_0V_SUPPORT))
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002074 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2075 ~CORE_IO_PAD_PWR_SWITCH),
2076 host->ioaddr + CORE_VENDOR_SPEC);
Krishna Konda46fd1432014-10-30 21:13:27 -07002077 else if ((io_level & REQ_IO_LOW) ||
2078 (msm_host->caps_0 & CORE_1_8V_SUPPORT))
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002079 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
2080 CORE_IO_PAD_PWR_SWITCH),
2081 host->ioaddr + CORE_VENDOR_SPEC);
2082 mb();
2083
Asutosh Das0ef24812012-12-18 16:14:02 +05302084 pr_debug("%s: Handled IRQ(%d), ret=%d, ack=0x%x\n",
2085 mmc_hostname(msm_host->mmc), irq, ret, irq_ack);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302086 spin_lock_irqsave(&host->lock, flags);
2087 if (pwr_state)
2088 msm_host->curr_pwr_state = pwr_state;
2089 if (io_level)
2090 msm_host->curr_io_level = io_level;
2091 complete(&msm_host->pwr_irq_completion);
2092 spin_unlock_irqrestore(&host->lock, flags);
2093
Asutosh Das0ef24812012-12-18 16:14:02 +05302094 return IRQ_HANDLED;
2095}
2096
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302097static ssize_t
Sahitya Tummala5c55b932013-06-20 14:00:18 +05302098show_polling(struct device *dev, struct device_attribute *attr, char *buf)
2099{
2100 struct sdhci_host *host = dev_get_drvdata(dev);
2101 int poll;
2102 unsigned long flags;
2103
2104 spin_lock_irqsave(&host->lock, flags);
2105 poll = !!(host->mmc->caps & MMC_CAP_NEEDS_POLL);
2106 spin_unlock_irqrestore(&host->lock, flags);
2107
2108 return snprintf(buf, PAGE_SIZE, "%d\n", poll);
2109}
2110
2111static ssize_t
2112store_polling(struct device *dev, struct device_attribute *attr,
2113 const char *buf, size_t count)
2114{
2115 struct sdhci_host *host = dev_get_drvdata(dev);
2116 int value;
2117 unsigned long flags;
2118
2119 if (!kstrtou32(buf, 0, &value)) {
2120 spin_lock_irqsave(&host->lock, flags);
2121 if (value) {
2122 host->mmc->caps |= MMC_CAP_NEEDS_POLL;
2123 mmc_detect_change(host->mmc, 0);
2124 } else {
2125 host->mmc->caps &= ~MMC_CAP_NEEDS_POLL;
2126 }
2127 spin_unlock_irqrestore(&host->lock, flags);
2128 }
2129 return count;
2130}
2131
2132static ssize_t
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302133show_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
2134 char *buf)
2135{
2136 struct sdhci_host *host = dev_get_drvdata(dev);
2137 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2138 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2139
2140 return snprintf(buf, PAGE_SIZE, "%u\n",
2141 msm_host->msm_bus_vote.is_max_bw_needed);
2142}
2143
2144static ssize_t
2145store_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
2146 const char *buf, size_t count)
2147{
2148 struct sdhci_host *host = dev_get_drvdata(dev);
2149 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2150 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2151 uint32_t value;
2152 unsigned long flags;
2153
2154 if (!kstrtou32(buf, 0, &value)) {
2155 spin_lock_irqsave(&host->lock, flags);
2156 msm_host->msm_bus_vote.is_max_bw_needed = !!value;
2157 spin_unlock_irqrestore(&host->lock, flags);
2158 }
2159 return count;
2160}
2161
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302162static void sdhci_msm_check_power_status(struct sdhci_host *host, u32 req_type)
Asutosh Das0ef24812012-12-18 16:14:02 +05302163{
2164 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2165 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302166 unsigned long flags;
2167 bool done = false;
Sahitya Tummala481fbb02013-08-06 15:22:28 +05302168 u32 io_sig_sts;
Asutosh Das0ef24812012-12-18 16:14:02 +05302169
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302170 spin_lock_irqsave(&host->lock, flags);
2171 pr_debug("%s: %s: request %d curr_pwr_state %x curr_io_level %x\n",
2172 mmc_hostname(host->mmc), __func__, req_type,
2173 msm_host->curr_pwr_state, msm_host->curr_io_level);
Sahitya Tummala481fbb02013-08-06 15:22:28 +05302174 io_sig_sts = readl_relaxed(msm_host->core_mem + CORE_GENERICS);
2175 /*
2176 * The IRQ for request type IO High/Low will be generated when -
2177 * 1. SWITCHABLE_SIGNALLING_VOL is enabled in HW.
2178 * 2. If 1 is true and when there is a state change in 1.8V enable
2179 * bit (bit 3) of SDHCI_HOST_CONTROL2 register. The reset state of
2180 * that bit is 0 which indicates 3.3V IO voltage. So, when MMC core
2181 * layer tries to set it to 3.3V before card detection happens, the
2182 * IRQ doesn't get triggered as there is no state change in this bit.
2183 * The driver already handles this case by changing the IO voltage
2184 * level to high as part of controller power up sequence. Hence, check
2185 * for host->pwr to handle a case where IO voltage high request is
2186 * issued even before controller power up.
2187 */
2188 if (req_type & (REQ_IO_HIGH | REQ_IO_LOW)) {
2189 if (!(io_sig_sts & SWITCHABLE_SIGNALLING_VOL) ||
2190 ((req_type & REQ_IO_HIGH) && !host->pwr)) {
2191 pr_debug("%s: do not wait for power IRQ that never comes\n",
2192 mmc_hostname(host->mmc));
2193 spin_unlock_irqrestore(&host->lock, flags);
2194 return;
2195 }
2196 }
2197
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302198 if ((req_type & msm_host->curr_pwr_state) ||
2199 (req_type & msm_host->curr_io_level))
2200 done = true;
2201 spin_unlock_irqrestore(&host->lock, flags);
Asutosh Das0ef24812012-12-18 16:14:02 +05302202
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302203 /*
2204 * This is needed here to hanlde a case where IRQ gets
2205 * triggered even before this function is called so that
2206 * x->done counter of completion gets reset. Otherwise,
2207 * next call to wait_for_completion returns immediately
2208 * without actually waiting for the IRQ to be handled.
2209 */
2210 if (done)
2211 init_completion(&msm_host->pwr_irq_completion);
2212 else
2213 wait_for_completion(&msm_host->pwr_irq_completion);
2214
2215 pr_debug("%s: %s: request %d done\n", mmc_hostname(host->mmc),
2216 __func__, req_type);
Asutosh Das0ef24812012-12-18 16:14:02 +05302217}
2218
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002219static void sdhci_msm_toggle_cdr(struct sdhci_host *host, bool enable)
2220{
2221 if (enable)
2222 writel_relaxed((readl_relaxed(host->ioaddr +
2223 CORE_DLL_CONFIG) | CORE_CDR_EN),
2224 host->ioaddr + CORE_DLL_CONFIG);
2225 else
2226 writel_relaxed((readl_relaxed(host->ioaddr +
2227 CORE_DLL_CONFIG) & ~CORE_CDR_EN),
2228 host->ioaddr + CORE_DLL_CONFIG);
2229}
2230
Asutosh Das648f9d12013-01-10 21:11:04 +05302231static unsigned int sdhci_msm_max_segs(void)
2232{
2233 return SDHCI_MSM_MAX_SEGMENTS;
2234}
2235
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302236static unsigned int sdhci_msm_get_min_clock(struct sdhci_host *host)
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302237{
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302238 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2239 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302240
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302241 return msm_host->pdata->sup_clk_table[0];
2242}
2243
2244static unsigned int sdhci_msm_get_max_clock(struct sdhci_host *host)
2245{
2246 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2247 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2248 int max_clk_index = msm_host->pdata->sup_clk_cnt;
2249
2250 return msm_host->pdata->sup_clk_table[max_clk_index - 1];
2251}
2252
2253static unsigned int sdhci_msm_get_sup_clk_rate(struct sdhci_host *host,
2254 u32 req_clk)
2255{
2256 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2257 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2258 unsigned int sel_clk = -1;
2259 unsigned char cnt;
2260
2261 if (req_clk < sdhci_msm_get_min_clock(host)) {
2262 sel_clk = sdhci_msm_get_min_clock(host);
2263 return sel_clk;
2264 }
2265
2266 for (cnt = 0; cnt < msm_host->pdata->sup_clk_cnt; cnt++) {
2267 if (msm_host->pdata->sup_clk_table[cnt] > req_clk) {
2268 break;
2269 } else if (msm_host->pdata->sup_clk_table[cnt] == req_clk) {
2270 sel_clk = msm_host->pdata->sup_clk_table[cnt];
2271 break;
2272 } else {
2273 sel_clk = msm_host->pdata->sup_clk_table[cnt];
2274 }
2275 }
2276 return sel_clk;
2277}
2278
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302279static int sdhci_msm_enable_controller_clock(struct sdhci_host *host)
2280{
2281 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2282 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2283 int rc = 0;
2284
2285 if (atomic_read(&msm_host->controller_clock))
2286 return 0;
2287
2288 sdhci_msm_bus_voting(host, 1);
2289
2290 if (!IS_ERR(msm_host->pclk)) {
2291 rc = clk_prepare_enable(msm_host->pclk);
2292 if (rc) {
2293 pr_err("%s: %s: failed to enable the pclk with error %d\n",
2294 mmc_hostname(host->mmc), __func__, rc);
2295 goto remove_vote;
2296 }
2297 }
2298
2299 rc = clk_prepare_enable(msm_host->clk);
2300 if (rc) {
2301 pr_err("%s: %s: failed to enable the host-clk with error %d\n",
2302 mmc_hostname(host->mmc), __func__, rc);
2303 goto disable_pclk;
2304 }
2305
2306 atomic_set(&msm_host->controller_clock, 1);
2307 pr_debug("%s: %s: enabled controller clock\n",
2308 mmc_hostname(host->mmc), __func__);
2309 goto out;
2310
2311disable_pclk:
2312 if (!IS_ERR(msm_host->pclk))
2313 clk_disable_unprepare(msm_host->pclk);
2314remove_vote:
2315 if (msm_host->msm_bus_vote.client_handle)
2316 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2317out:
2318 return rc;
2319}
2320
2321
2322
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302323static int sdhci_msm_prepare_clocks(struct sdhci_host *host, bool enable)
2324{
2325 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2326 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2327 int rc = 0;
2328
2329 if (enable && !atomic_read(&msm_host->clks_on)) {
2330 pr_debug("%s: request to enable clocks\n",
2331 mmc_hostname(host->mmc));
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302332
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302333 /*
2334 * The bus-width or the clock rate might have changed
2335 * after controller clocks are enbaled, update bus vote
2336 * in such case.
2337 */
2338 if (atomic_read(&msm_host->controller_clock))
2339 sdhci_msm_bus_voting(host, 1);
2340
2341 rc = sdhci_msm_enable_controller_clock(host);
2342 if (rc)
2343 goto remove_vote;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302344
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302345 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
2346 rc = clk_prepare_enable(msm_host->bus_clk);
2347 if (rc) {
2348 pr_err("%s: %s: failed to enable the bus-clock with error %d\n",
2349 mmc_hostname(host->mmc), __func__, rc);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302350 goto disable_controller_clk;
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302351 }
2352 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002353 if (!IS_ERR(msm_host->ff_clk)) {
2354 rc = clk_prepare_enable(msm_host->ff_clk);
2355 if (rc) {
2356 pr_err("%s: %s: failed to enable the ff_clk with error %d\n",
2357 mmc_hostname(host->mmc), __func__, rc);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302358 goto disable_bus_clk;
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002359 }
2360 }
2361 if (!IS_ERR(msm_host->sleep_clk)) {
2362 rc = clk_prepare_enable(msm_host->sleep_clk);
2363 if (rc) {
2364 pr_err("%s: %s: failed to enable the sleep_clk with error %d\n",
2365 mmc_hostname(host->mmc), __func__, rc);
2366 goto disable_ff_clk;
2367 }
2368 }
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302369 mb();
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302370
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302371 } else if (!enable && atomic_read(&msm_host->clks_on)) {
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302372 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
2373 mb();
Sahitya Tummaladc182982013-08-20 15:32:09 +05302374 /*
2375 * During 1.8V signal switching the clock source must
2376 * still be ON as it requires accessing SDHC
2377 * registers (SDHCi host control2 register bit 3 must
2378 * be written and polled after stopping the SDCLK).
2379 */
2380 if (host->mmc->card_clock_off)
2381 return 0;
2382 pr_debug("%s: request to disable clocks\n",
2383 mmc_hostname(host->mmc));
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002384 if (!IS_ERR_OR_NULL(msm_host->sleep_clk))
2385 clk_disable_unprepare(msm_host->sleep_clk);
2386 if (!IS_ERR_OR_NULL(msm_host->ff_clk))
2387 clk_disable_unprepare(msm_host->ff_clk);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302388 clk_disable_unprepare(msm_host->clk);
2389 if (!IS_ERR(msm_host->pclk))
2390 clk_disable_unprepare(msm_host->pclk);
2391 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2392 clk_disable_unprepare(msm_host->bus_clk);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302393
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302394 atomic_set(&msm_host->controller_clock, 0);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302395 sdhci_msm_bus_voting(host, 0);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302396 }
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302397 atomic_set(&msm_host->clks_on, enable);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302398 goto out;
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002399disable_ff_clk:
2400 if (!IS_ERR_OR_NULL(msm_host->ff_clk))
2401 clk_disable_unprepare(msm_host->ff_clk);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302402disable_bus_clk:
2403 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2404 clk_disable_unprepare(msm_host->bus_clk);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302405disable_controller_clk:
2406 if (!IS_ERR_OR_NULL(msm_host->clk))
2407 clk_disable_unprepare(msm_host->clk);
2408 if (!IS_ERR_OR_NULL(msm_host->pclk))
2409 clk_disable_unprepare(msm_host->pclk);
2410 atomic_set(&msm_host->controller_clock, 0);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302411remove_vote:
2412 if (msm_host->msm_bus_vote.client_handle)
2413 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302414out:
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302415 return rc;
2416}
2417
2418static void sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock)
2419{
2420 int rc;
2421 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2422 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2423 struct mmc_ios curr_ios = host->mmc->ios;
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002424 u32 sup_clock, ddr_clock, dll_lock;
Sahitya Tummala043744a2013-06-24 09:55:33 +05302425 bool curr_pwrsave;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302426
2427 if (!clock) {
Sujit Reddy Thummabf1aecc2014-01-10 10:58:54 +05302428 /*
2429 * disable pwrsave to ensure clock is not auto-gated until
2430 * the rate is >400KHz (initialization complete).
2431 */
2432 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2433 ~CORE_CLK_PWRSAVE, host->ioaddr + CORE_VENDOR_SPEC);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302434 sdhci_msm_prepare_clocks(host, false);
2435 host->clock = clock;
2436 goto out;
2437 }
2438
2439 rc = sdhci_msm_prepare_clocks(host, true);
2440 if (rc)
2441 goto out;
2442
Sahitya Tummala043744a2013-06-24 09:55:33 +05302443 curr_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2444 CORE_CLK_PWRSAVE);
Sahitya Tummalae000b242013-08-29 16:21:08 +05302445 if ((clock > 400000) &&
Venkat Gopalakrishnanc0a367272015-02-24 13:09:09 -08002446 !curr_pwrsave && mmc_host_may_gate_card(host->mmc->card))
Sahitya Tummala043744a2013-06-24 09:55:33 +05302447 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2448 | CORE_CLK_PWRSAVE,
2449 host->ioaddr + CORE_VENDOR_SPEC);
2450 /*
2451 * Disable pwrsave for a newly added card if doesn't allow clock
2452 * gating.
2453 */
Venkat Gopalakrishnanc0a367272015-02-24 13:09:09 -08002454 else if (curr_pwrsave && !mmc_host_may_gate_card(host->mmc->card))
Sahitya Tummala043744a2013-06-24 09:55:33 +05302455 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2456 & ~CORE_CLK_PWRSAVE,
2457 host->ioaddr + CORE_VENDOR_SPEC);
2458
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302459 sup_clock = sdhci_msm_get_sup_clk_rate(host, clock);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002460 if ((curr_ios.timing == MMC_TIMING_UHS_DDR50) ||
Venkat Gopalakrishnan0a29da92015-01-09 12:19:16 -08002461 (curr_ios.timing == MMC_TIMING_MMC_DDR52) ||
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002462 (curr_ios.timing == MMC_TIMING_MMC_HS400)) {
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302463 /*
2464 * The SDHC requires internal clock frequency to be double the
2465 * actual clock that will be set for DDR mode. The controller
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002466 * uses the faster clock(100/400MHz) for some of its parts and
2467 * send the actual required clock (50/200MHz) to the card.
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302468 */
2469 ddr_clock = clock * 2;
2470 sup_clock = sdhci_msm_get_sup_clk_rate(host,
2471 ddr_clock);
2472 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002473
2474 /*
2475 * In general all timing modes are controlled via UHS mode select in
2476 * Host Control2 register. eMMC specific HS200/HS400 doesn't have
2477 * their respective modes defined here, hence we use these values.
2478 *
2479 * HS200 - SDR104 (Since they both are equivalent in functionality)
2480 * HS400 - This involves multiple configurations
2481 * Initially SDR104 - when tuning is required as HS200
2482 * Then when switching to DDR @ 400MHz (HS400) we use
2483 * the vendor specific HC_SELECT_IN to control the mode.
2484 *
2485 * In addition to controlling the modes we also need to select the
2486 * correct input clock for DLL depending on the mode.
2487 *
2488 * HS400 - divided clock (free running MCLK/2)
2489 * All other modes - default (free running MCLK)
2490 */
2491 if (curr_ios.timing == MMC_TIMING_MMC_HS400) {
2492 /* Select the divided clock (free running MCLK/2) */
2493 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2494 & ~CORE_HC_MCLK_SEL_MASK)
2495 | CORE_HC_MCLK_SEL_HS400),
2496 host->ioaddr + CORE_VENDOR_SPEC);
2497 /*
2498 * Select HS400 mode using the HC_SELECT_IN from VENDOR SPEC
2499 * register
2500 */
2501 if (msm_host->tuning_done && !msm_host->calibration_done) {
2502 /*
2503 * Write 0x6 to HC_SELECT_IN and 1 to HC_SELECT_IN_EN
2504 * field in VENDOR_SPEC_FUNC
2505 */
2506 writel_relaxed((readl_relaxed(host->ioaddr + \
2507 CORE_VENDOR_SPEC)
2508 | CORE_HC_SELECT_IN_HS400
2509 | CORE_HC_SELECT_IN_EN),
2510 host->ioaddr + CORE_VENDOR_SPEC);
2511 }
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002512 if (!host->mmc->ios.old_rate && !msm_host->use_cdclp533) {
2513 /*
2514 * Poll on DLL_LOCK and DDR_DLL_LOCK bits in
2515 * CORE_DLL_STATUS to be set. This should get set
2516 * with in 15 us at 200 MHz.
2517 */
2518 rc = readl_poll_timeout(host->ioaddr + CORE_DLL_STATUS,
2519 dll_lock, (dll_lock & (CORE_DLL_LOCK |
2520 CORE_DDR_DLL_LOCK)), 10, 1000);
2521 if (rc == -ETIMEDOUT)
2522 pr_err("%s: Unable to get DLL_LOCK/DDR_DLL_LOCK, dll_status: 0x%08x\n",
2523 mmc_hostname(host->mmc),
2524 dll_lock);
2525 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002526 } else {
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002527 if (!msm_host->use_cdclp533)
2528 /* set CORE_PWRSAVE_DLL bit in CORE_VENDOR_SPEC3 */
2529 writel_relaxed((readl_relaxed(host->ioaddr +
2530 CORE_VENDOR_SPEC3) & ~CORE_PWRSAVE_DLL),
2531 host->ioaddr + CORE_VENDOR_SPEC3);
2532
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002533 /* Select the default clock (free running MCLK) */
2534 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2535 & ~CORE_HC_MCLK_SEL_MASK)
2536 | CORE_HC_MCLK_SEL_DFLT),
2537 host->ioaddr + CORE_VENDOR_SPEC);
2538
2539 /*
2540 * Disable HC_SELECT_IN to be able to use the UHS mode select
2541 * configuration from Host Control2 register for all other
2542 * modes.
2543 *
2544 * Write 0 to HC_SELECT_IN and HC_SELECT_IN_EN field
2545 * in VENDOR_SPEC_FUNC
2546 */
2547 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2548 & ~CORE_HC_SELECT_IN_EN
2549 & ~CORE_HC_SELECT_IN_MASK),
2550 host->ioaddr + CORE_VENDOR_SPEC);
2551 }
2552 mb();
2553
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302554 if (sup_clock != msm_host->clk_rate) {
2555 pr_debug("%s: %s: setting clk rate to %u\n",
2556 mmc_hostname(host->mmc), __func__, sup_clock);
2557 rc = clk_set_rate(msm_host->clk, sup_clock);
2558 if (rc) {
2559 pr_err("%s: %s: Failed to set rate %u for host-clk : %d\n",
2560 mmc_hostname(host->mmc), __func__,
2561 sup_clock, rc);
2562 goto out;
2563 }
2564 msm_host->clk_rate = sup_clock;
2565 host->clock = clock;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302566 /*
2567 * Update the bus vote in case of frequency change due to
2568 * clock scaling.
2569 */
2570 sdhci_msm_bus_voting(host, 1);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302571 }
2572out:
2573 sdhci_set_clock(host, clock);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302574}
2575
Sahitya Tummala14613432013-03-21 11:13:25 +05302576static void sdhci_msm_set_uhs_signaling(struct sdhci_host *host,
2577 unsigned int uhs)
2578{
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002579 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2580 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala14613432013-03-21 11:13:25 +05302581 u16 ctrl_2;
2582
2583 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2584 /* Select Bus Speed Mode for host */
2585 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Venkat Gopalakrishnan0a29da92015-01-09 12:19:16 -08002586 if ((uhs == MMC_TIMING_MMC_HS400) ||
2587 (uhs == MMC_TIMING_MMC_HS200) ||
2588 (uhs == MMC_TIMING_UHS_SDR104))
Sahitya Tummala14613432013-03-21 11:13:25 +05302589 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2590 else if (uhs == MMC_TIMING_UHS_SDR12)
2591 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
2592 else if (uhs == MMC_TIMING_UHS_SDR25)
2593 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
2594 else if (uhs == MMC_TIMING_UHS_SDR50)
2595 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
Venkat Gopalakrishnan0a29da92015-01-09 12:19:16 -08002596 else if ((uhs == MMC_TIMING_UHS_DDR50) ||
2597 (uhs == MMC_TIMING_MMC_DDR52))
Sahitya Tummala14613432013-03-21 11:13:25 +05302598 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302599 /*
2600 * When clock frquency is less than 100MHz, the feedback clock must be
2601 * provided and DLL must not be used so that tuning can be skipped. To
2602 * provide feedback clock, the mode selection can be any value less
2603 * than 3'b011 in bits [2:0] of HOST CONTROL2 register.
2604 */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002605 if (host->clock <= CORE_FREQ_100MHZ) {
2606 if ((uhs == MMC_TIMING_MMC_HS400) ||
2607 (uhs == MMC_TIMING_MMC_HS200) ||
2608 (uhs == MMC_TIMING_UHS_SDR104))
2609 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302610
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002611 /*
2612 * Make sure DLL is disabled when not required
2613 *
2614 * Write 1 to DLL_RST bit of DLL_CONFIG register
2615 */
2616 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
2617 | CORE_DLL_RST),
2618 host->ioaddr + CORE_DLL_CONFIG);
2619
2620 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
2621 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
2622 | CORE_DLL_PDN),
2623 host->ioaddr + CORE_DLL_CONFIG);
2624 mb();
2625
2626 /*
2627 * The DLL needs to be restored and CDCLP533 recalibrated
2628 * when the clock frequency is set back to 400MHz.
2629 */
2630 msm_host->calibration_done = false;
2631 }
2632
2633 pr_debug("%s: %s-clock:%u uhs mode:%u ctrl_2:0x%x\n",
2634 mmc_hostname(host->mmc), __func__, host->clock, uhs, ctrl_2);
Sahitya Tummala14613432013-03-21 11:13:25 +05302635 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
2636
2637}
2638
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302639#define MAX_TEST_BUS 20
2640
2641void sdhci_msm_dump_vendor_regs(struct sdhci_host *host)
2642{
2643 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2644 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2645 int tbsel, tbsel2;
2646 int i, index = 0;
2647 u32 test_bus_val = 0;
2648 u32 debug_reg[MAX_TEST_BUS] = {0};
2649
2650 pr_info("----------- VENDOR REGISTER DUMP -----------\n");
2651 pr_info("Data cnt: 0x%08x | Fifo cnt: 0x%08x | Int sts: 0x%08x\n",
2652 readl_relaxed(msm_host->core_mem + CORE_MCI_DATA_CNT),
2653 readl_relaxed(msm_host->core_mem + CORE_MCI_FIFO_CNT),
2654 readl_relaxed(msm_host->core_mem + CORE_MCI_STATUS));
2655 pr_info("DLL cfg: 0x%08x | DLL sts: 0x%08x | SDCC ver: 0x%08x\n",
2656 readl_relaxed(host->ioaddr + CORE_DLL_CONFIG),
2657 readl_relaxed(host->ioaddr + CORE_DLL_STATUS),
2658 readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION));
2659 pr_info("Vndr func: 0x%08x | Vndr adma err : addr0: 0x%08x addr1: 0x%08x\n",
2660 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC),
2661 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_ADMA_ERR_ADDR0),
2662 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_ADMA_ERR_ADDR1));
2663
2664 /*
2665 * tbsel indicates [2:0] bits and tbsel2 indicates [7:4] bits
2666 * of CORE_TESTBUS_CONFIG register.
2667 *
2668 * To select test bus 0 to 7 use tbsel and to select any test bus
2669 * above 7 use (tbsel2 | tbsel) to get the test bus number. For eg,
2670 * to select test bus 14, write 0x1E to CORE_TESTBUS_CONFIG register
2671 * i.e., tbsel2[7:4] = 0001, tbsel[2:0] = 110.
2672 */
2673 for (tbsel2 = 0; tbsel2 < 3; tbsel2++) {
2674 for (tbsel = 0; tbsel < 8; tbsel++) {
2675 if (index >= MAX_TEST_BUS)
2676 break;
2677 test_bus_val = (tbsel2 << CORE_TESTBUS_SEL2_BIT) |
2678 tbsel | CORE_TESTBUS_ENA;
2679 writel_relaxed(test_bus_val,
2680 msm_host->core_mem + CORE_TESTBUS_CONFIG);
2681 debug_reg[index++] = readl_relaxed(msm_host->core_mem +
2682 CORE_SDCC_DEBUG_REG);
2683 }
2684 }
2685 for (i = 0; i < MAX_TEST_BUS; i = i + 4)
2686 pr_info(" Test bus[%d to %d]: 0x%08x 0x%08x 0x%08x 0x%08x\n",
2687 i, i + 3, debug_reg[i], debug_reg[i+1],
2688 debug_reg[i+2], debug_reg[i+3]);
2689 /* Disable test bus */
2690 writel_relaxed(~CORE_TESTBUS_ENA, msm_host->core_mem +
2691 CORE_TESTBUS_CONFIG);
2692}
2693
Asutosh Das0ef24812012-12-18 16:14:02 +05302694static struct sdhci_ops sdhci_msm_ops = {
Sahitya Tummala14613432013-03-21 11:13:25 +05302695 .set_uhs_signaling = sdhci_msm_set_uhs_signaling,
Asutosh Das0ef24812012-12-18 16:14:02 +05302696 .check_power_status = sdhci_msm_check_power_status,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002697 .platform_execute_tuning = sdhci_msm_execute_tuning,
2698 .toggle_cdr = sdhci_msm_toggle_cdr,
Asutosh Das648f9d12013-01-10 21:11:04 +05302699 .get_max_segments = sdhci_msm_max_segs,
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302700 .set_clock = sdhci_msm_set_clock,
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302701 .get_min_clock = sdhci_msm_get_min_clock,
2702 .get_max_clock = sdhci_msm_get_max_clock,
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302703 .dump_vendor_regs = sdhci_msm_dump_vendor_regs,
Asutosh Dase5e9ca62013-07-30 19:08:36 +05302704 .config_auto_tuning_cmd = sdhci_msm_config_auto_tuning_cmd,
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302705 .enable_controller_clock = sdhci_msm_enable_controller_clock,
Venkat Gopalakrishnanb8cb7072015-01-09 11:04:34 -08002706 .set_bus_width = sdhci_set_bus_width,
Venkat Gopalakrishnan411df072015-01-09 11:09:44 -08002707 .reset = sdhci_reset,
Asutosh Das0ef24812012-12-18 16:14:02 +05302708};
2709
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302710static void sdhci_set_default_hw_caps(struct sdhci_msm_host *msm_host,
2711 struct sdhci_host *host)
2712{
Krishna Konda46fd1432014-10-30 21:13:27 -07002713 u32 version, caps = 0;
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302714 u16 minor;
2715 u8 major;
2716
2717 version = readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION);
2718 major = (version & CORE_VERSION_MAJOR_MASK) >>
2719 CORE_VERSION_MAJOR_SHIFT;
2720 minor = version & CORE_VERSION_TARGET_MASK;
2721
Krishna Konda46fd1432014-10-30 21:13:27 -07002722 caps = readl_relaxed(host->ioaddr + SDHCI_CAPABILITIES);
2723
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302724 /*
2725 * Starting with SDCC 5 controller (core major version = 1)
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002726 * controller won't advertise 3.0v, 1.8v and 8-bit features
2727 * except for some targets.
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302728 */
2729 if (major >= 1 && minor != 0x11 && minor != 0x12) {
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002730 struct sdhci_msm_reg_data *vdd_io_reg;
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002731 /*
2732 * Enable 1.8V support capability on controllers that
2733 * support dual voltage
2734 */
2735 vdd_io_reg = msm_host->pdata->vreg_data->vdd_io_data;
Krishna Konda46fd1432014-10-30 21:13:27 -07002736 if (vdd_io_reg && (vdd_io_reg->high_vol_level > 2700000))
2737 caps |= CORE_3_0V_SUPPORT;
2738 if (vdd_io_reg && (vdd_io_reg->low_vol_level < 1950000))
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002739 caps |= CORE_1_8V_SUPPORT;
Pratibhasagar Vada47992013-12-09 20:42:32 +05302740 if (msm_host->pdata->mmc_bus_width == MMC_CAP_8_BIT_DATA)
2741 caps |= CORE_8_BIT_SUPPORT;
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302742 }
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002743
2744 /*
2745 * SDCC 5 controller with major version 1, minor version 0x34 and later
2746 * with HS 400 mode support will use CM DLL instead of CDC LP 533 DLL.
2747 */
2748 if ((major == 1) && (minor < 0x34))
2749 msm_host->use_cdclp533 = true;
Gilad Broner2a10ca02014-10-02 17:20:35 +03002750
2751 /*
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -08002752 * SDCC 5 controller with major version 1, minor version 0x42 and later
2753 * will require additional steps when resetting DLL.
2754 */
2755 if ((major == 1) && (minor >= 0x42))
2756 msm_host->use_updated_dll_reset = true;
2757
2758 /*
Talel Shenhar9a25b882015-06-02 13:36:35 +03002759 * SDCC 5 controller with major version 1 and minor version 0x42,
2760 * 0x46 and 0x49 currently uses 14lpp tech DLL whose internal
2761 * gating cannot guarantee MCLK timing requirement i.e.
Ritesh Harjani764065e2015-05-13 14:14:45 +05302762 * when MCLK is gated OFF, it is not gated for less than 0.5us
2763 * and MCLK must be switched on for at-least 1us before DATA
2764 * starts coming.
2765 */
Talel Shenhar9a25b882015-06-02 13:36:35 +03002766 if ((major == 1) && ((minor == 0x42) || (minor == 0x46) ||
2767 (minor == 0x49)))
Ritesh Harjani764065e2015-05-13 14:14:45 +05302768 msm_host->use_14lpp_dll = true;
2769 /*
Gilad Broner2a10ca02014-10-02 17:20:35 +03002770 * Mask 64-bit support for controller with 32-bit address bus so that
2771 * smaller descriptor size will be used and improve memory consumption.
Gilad Broner2a10ca02014-10-02 17:20:35 +03002772 */
Venkat Gopalakrishnan9a62e042015-03-03 16:14:55 -08002773 if (!msm_host->pdata->largeaddressbus)
2774 caps &= ~CORE_SYS_BUS_SUPPORT_64_BIT;
2775
Gilad Broner2a10ca02014-10-02 17:20:35 +03002776 writel_relaxed(caps, host->ioaddr + CORE_VENDOR_SPEC_CAPABILITIES0);
Krishna Konda46fd1432014-10-30 21:13:27 -07002777 /* keep track of the value in SDHCI_CAPABILITIES */
2778 msm_host->caps_0 = caps;
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302779}
2780
Asutosh Das0ef24812012-12-18 16:14:02 +05302781static int sdhci_msm_probe(struct platform_device *pdev)
2782{
2783 struct sdhci_host *host;
2784 struct sdhci_pltfm_host *pltfm_host;
2785 struct sdhci_msm_host *msm_host;
2786 struct resource *core_memres = NULL;
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02002787 int ret = 0, dead = 0;
Stephen Boyd8dce5c62013-04-24 14:19:46 -07002788 u16 host_version;
Subhash Jadavani28137342013-05-14 17:46:43 +05302789 u32 pwr, irq_status, irq_ctl;
Asutosh Das0ef24812012-12-18 16:14:02 +05302790
2791 pr_debug("%s: Enter %s\n", dev_name(&pdev->dev), __func__);
2792 msm_host = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_msm_host),
2793 GFP_KERNEL);
2794 if (!msm_host) {
2795 ret = -ENOMEM;
2796 goto out;
2797 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302798
2799 msm_host->sdhci_msm_pdata.ops = &sdhci_msm_ops;
2800 host = sdhci_pltfm_init(pdev, &msm_host->sdhci_msm_pdata, 0);
2801 if (IS_ERR(host)) {
2802 ret = PTR_ERR(host);
2803 goto out;
2804 }
2805
2806 pltfm_host = sdhci_priv(host);
2807 pltfm_host->priv = msm_host;
2808 msm_host->mmc = host->mmc;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302809 msm_host->pdev = pdev;
Asutosh Das0ef24812012-12-18 16:14:02 +05302810
2811 /* Extract platform data */
2812 if (pdev->dev.of_node) {
Venkat Gopalakrishnan270580a2013-03-11 12:17:57 -07002813 ret = of_alias_get_id(pdev->dev.of_node, "sdhc");
2814 if (ret < 0) {
2815 dev_err(&pdev->dev, "Failed to get slot index %d\n",
2816 ret);
2817 goto pltfm_free;
2818 }
2819 if (disable_slots & (1 << (ret - 1))) {
2820 dev_info(&pdev->dev, "%s: Slot %d disabled\n", __func__,
2821 ret);
2822 ret = -ENODEV;
2823 goto pltfm_free;
2824 }
2825
Dov Levenglickc9033ab2015-03-10 16:00:56 +02002826 msm_host->pdata = sdhci_msm_populate_pdata(&pdev->dev,
2827 msm_host);
Asutosh Das0ef24812012-12-18 16:14:02 +05302828 if (!msm_host->pdata) {
2829 dev_err(&pdev->dev, "DT parsing error\n");
2830 goto pltfm_free;
2831 }
2832 } else {
2833 dev_err(&pdev->dev, "No device tree node\n");
2834 goto pltfm_free;
2835 }
2836
2837 /* Setup Clocks */
2838
2839 /* Setup SDCC bus voter clock. */
2840 msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus_clk");
2841 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
2842 /* Vote for max. clk rate for max. performance */
2843 ret = clk_set_rate(msm_host->bus_clk, INT_MAX);
2844 if (ret)
2845 goto pltfm_free;
2846 ret = clk_prepare_enable(msm_host->bus_clk);
2847 if (ret)
2848 goto pltfm_free;
2849 }
2850
2851 /* Setup main peripheral bus clock */
2852 msm_host->pclk = devm_clk_get(&pdev->dev, "iface_clk");
2853 if (!IS_ERR(msm_host->pclk)) {
2854 ret = clk_prepare_enable(msm_host->pclk);
2855 if (ret)
2856 goto bus_clk_disable;
2857 }
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302858 atomic_set(&msm_host->controller_clock, 1);
Asutosh Das0ef24812012-12-18 16:14:02 +05302859
2860 /* Setup SDC MMC clock */
2861 msm_host->clk = devm_clk_get(&pdev->dev, "core_clk");
2862 if (IS_ERR(msm_host->clk)) {
2863 ret = PTR_ERR(msm_host->clk);
2864 goto pclk_disable;
2865 }
2866
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302867 /* Set to the minimum supported clock frequency */
2868 ret = clk_set_rate(msm_host->clk, sdhci_msm_get_min_clock(host));
2869 if (ret) {
2870 dev_err(&pdev->dev, "MClk rate set failed (%d)\n", ret);
Sahitya Tummala020ede0d2013-06-07 13:03:07 +05302871 goto pclk_disable;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302872 }
Sahitya Tummala020ede0d2013-06-07 13:03:07 +05302873 ret = clk_prepare_enable(msm_host->clk);
2874 if (ret)
2875 goto pclk_disable;
2876
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302877 msm_host->clk_rate = sdhci_msm_get_min_clock(host);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302878 atomic_set(&msm_host->clks_on, 1);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302879
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002880 /* Setup CDC calibration fixed feedback clock */
2881 msm_host->ff_clk = devm_clk_get(&pdev->dev, "cal_clk");
2882 if (!IS_ERR(msm_host->ff_clk)) {
2883 ret = clk_prepare_enable(msm_host->ff_clk);
2884 if (ret)
2885 goto clk_disable;
2886 }
2887
2888 /* Setup CDC calibration sleep clock */
2889 msm_host->sleep_clk = devm_clk_get(&pdev->dev, "sleep_clk");
2890 if (!IS_ERR(msm_host->sleep_clk)) {
2891 ret = clk_prepare_enable(msm_host->sleep_clk);
2892 if (ret)
2893 goto ff_clk_disable;
2894 }
2895
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07002896 msm_host->saved_tuning_phase = INVALID_TUNING_PHASE;
2897
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302898 ret = sdhci_msm_bus_register(msm_host, pdev);
2899 if (ret)
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002900 goto sleep_clk_disable;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302901
2902 if (msm_host->msm_bus_vote.client_handle)
2903 INIT_DELAYED_WORK(&msm_host->msm_bus_vote.vote_work,
2904 sdhci_msm_bus_work);
2905 sdhci_msm_bus_voting(host, 1);
2906
Asutosh Das0ef24812012-12-18 16:14:02 +05302907 /* Setup regulators */
2908 ret = sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, true);
2909 if (ret) {
2910 dev_err(&pdev->dev, "Regulator setup failed (%d)\n", ret);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302911 goto bus_unregister;
Asutosh Das0ef24812012-12-18 16:14:02 +05302912 }
2913
2914 /* Reset the core and Enable SDHC mode */
2915 core_memres = platform_get_resource_byname(pdev,
2916 IORESOURCE_MEM, "core_mem");
Asutosh Das890bdee2014-08-08 23:01:42 +05302917 if (!core_memres) {
2918 dev_err(&pdev->dev, "Failed to get iomem resource\n");
2919 goto vreg_deinit;
2920 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302921 msm_host->core_mem = devm_ioremap(&pdev->dev, core_memres->start,
2922 resource_size(core_memres));
2923
2924 if (!msm_host->core_mem) {
2925 dev_err(&pdev->dev, "Failed to remap registers\n");
2926 ret = -ENOMEM;
2927 goto vreg_deinit;
2928 }
2929
Stepan Moskovchenkoa4d0fa72013-09-13 22:19:33 -07002930 /* Unset HC_MODE_EN bit in HC_MODE register */
2931 writel_relaxed(0, (msm_host->core_mem + CORE_HC_MODE));
2932
Asutosh Das0ef24812012-12-18 16:14:02 +05302933 /* Set SW_RST bit in POWER register (Offset 0x0) */
Sahitya Tummala66b0fe32013-04-25 11:50:56 +05302934 writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_POWER) |
2935 CORE_SW_RST, msm_host->core_mem + CORE_POWER);
2936 /*
2937 * SW reset can take upto 10HCLK + 15MCLK cycles.
2938 * Calculating based on min clk rates (hclk = 27MHz,
2939 * mclk = 400KHz) it comes to ~40us. Let's poll for
2940 * max. 1ms for reset completion.
2941 */
2942 ret = readl_poll_timeout(msm_host->core_mem + CORE_POWER,
Matt Wagantallc7097a22014-04-29 15:48:15 -07002943 pwr, !(pwr & CORE_SW_RST), 10, 1000);
Sahitya Tummala66b0fe32013-04-25 11:50:56 +05302944
2945 if (ret) {
2946 dev_err(&pdev->dev, "reset failed (%d)\n", ret);
2947 goto vreg_deinit;
2948 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302949 /* Set HC_MODE_EN bit in HC_MODE register */
2950 writel_relaxed(HC_MODE_EN, (msm_host->core_mem + CORE_HC_MODE));
2951
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002952 /* Set FF_CLK_SW_RST_DIS bit in HC_MODE register */
2953 writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_HC_MODE) |
2954 FF_CLK_SW_RST_DIS, msm_host->core_mem + CORE_HC_MODE);
2955
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302956 sdhci_set_default_hw_caps(msm_host, host);
Krishna Konda46fd1432014-10-30 21:13:27 -07002957
2958 /*
2959 * Set the PAD_PWR_SWTICH_EN bit so that the PAD_PWR_SWITCH bit can
2960 * be used as required later on.
2961 */
2962 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
2963 CORE_IO_PAD_PWR_SWITCH_EN),
2964 host->ioaddr + CORE_VENDOR_SPEC);
Asutosh Das0ef24812012-12-18 16:14:02 +05302965 /*
Subhash Jadavani28137342013-05-14 17:46:43 +05302966 * CORE_SW_RST above may trigger power irq if previous status of PWRCTL
2967 * was either BUS_ON or IO_HIGH_V. So before we enable the power irq
2968 * interrupt in GIC (by registering the interrupt handler), we need to
2969 * ensure that any pending power irq interrupt status is acknowledged
2970 * otherwise power irq interrupt handler would be fired prematurely.
2971 */
2972 irq_status = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
2973 writel_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
2974 irq_ctl = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_CTL);
2975 if (irq_status & (CORE_PWRCTL_BUS_ON | CORE_PWRCTL_BUS_OFF))
2976 irq_ctl |= CORE_PWRCTL_BUS_SUCCESS;
2977 if (irq_status & (CORE_PWRCTL_IO_HIGH | CORE_PWRCTL_IO_LOW))
2978 irq_ctl |= CORE_PWRCTL_IO_SUCCESS;
2979 writel_relaxed(irq_ctl, (msm_host->core_mem + CORE_PWRCTL_CTL));
Krishna Konda46fd1432014-10-30 21:13:27 -07002980
Subhash Jadavani28137342013-05-14 17:46:43 +05302981 /*
2982 * Ensure that above writes are propogated before interrupt enablement
2983 * in GIC.
2984 */
2985 mb();
2986
2987 /*
Asutosh Das0ef24812012-12-18 16:14:02 +05302988 * Following are the deviations from SDHC spec v3.0 -
2989 * 1. Card detection is handled using separate GPIO.
2990 * 2. Bus power control is handled by interacting with PMIC.
2991 */
2992 host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
2993 host->quirks |= SDHCI_QUIRK_SINGLE_POWER_WRITE;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302994 host->quirks |= SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN;
2995 host->quirks2 |= SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK;
Sahitya Tummala87d43942013-04-12 11:49:11 +05302996 host->quirks2 |= SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD;
Sahitya Tummala314162c2013-04-12 12:11:20 +05302997 host->quirks2 |= SDHCI_QUIRK2_BROKEN_PRESET_VALUE;
Sahitya Tummala7c9780d2013-04-12 11:59:25 +05302998 host->quirks2 |= SDHCI_QUIRK2_USE_RESERVED_MAX_TIMEOUT;
Asutosh Das0ef24812012-12-18 16:14:02 +05302999
Sahitya Tummalaa5733ab52013-06-10 16:32:51 +05303000 if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK)
3001 host->quirks2 |= SDHCI_QUIRK2_DIVIDE_TOUT_BY_4;
3002
Stephen Boyd8dce5c62013-04-24 14:19:46 -07003003 host_version = readw_relaxed((host->ioaddr + SDHCI_HOST_VERSION));
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07003004 dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n",
3005 host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >>
3006 SDHCI_VENDOR_VER_SHIFT));
3007 if (((host_version & SDHCI_VENDOR_VER_MASK) >>
3008 SDHCI_VENDOR_VER_SHIFT) == SDHCI_VER_100) {
3009 /*
3010 * Add 40us delay in interrupt handler when
3011 * operating at initialization frequency(400KHz).
3012 */
3013 host->quirks2 |= SDHCI_QUIRK2_SLOW_INT_CLR;
3014 /*
3015 * Set Software Reset for DAT line in Software
3016 * Reset Register (Bit 2).
3017 */
3018 host->quirks2 |= SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT;
3019 }
3020
Asutosh Das214b9662013-06-13 14:27:42 +05303021 host->quirks2 |= SDHCI_QUIRK2_IGN_DATA_END_BIT_ERROR;
3022
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07003023 /* Setup PWRCTL irq */
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02003024 msm_host->pwr_irq = platform_get_irq_byname(pdev, "pwr_irq");
3025 if (msm_host->pwr_irq < 0) {
Asutosh Das0ef24812012-12-18 16:14:02 +05303026 dev_err(&pdev->dev, "Failed to get pwr_irq by name (%d)\n",
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02003027 msm_host->pwr_irq);
Asutosh Das0ef24812012-12-18 16:14:02 +05303028 goto vreg_deinit;
3029 }
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02003030 ret = devm_request_threaded_irq(&pdev->dev, msm_host->pwr_irq, NULL,
Asutosh Das0ef24812012-12-18 16:14:02 +05303031 sdhci_msm_pwr_irq, IRQF_ONESHOT,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07003032 dev_name(&pdev->dev), host);
Asutosh Das0ef24812012-12-18 16:14:02 +05303033 if (ret) {
3034 dev_err(&pdev->dev, "Request threaded irq(%d) failed (%d)\n",
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02003035 msm_host->pwr_irq, ret);
Asutosh Das0ef24812012-12-18 16:14:02 +05303036 goto vreg_deinit;
3037 }
3038
3039 /* Enable pwr irq interrupts */
3040 writel_relaxed(INT_MASK, (msm_host->core_mem + CORE_PWRCTL_MASK));
3041
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05303042#ifdef CONFIG_MMC_CLKGATE
3043 /* Set clock gating delay to be used when CONFIG_MMC_CLKGATE is set */
3044 msm_host->mmc->clkgate_delay = SDHCI_MSM_MMC_CLK_GATE_DELAY;
3045#endif
3046
Asutosh Das0ef24812012-12-18 16:14:02 +05303047 /* Set host capabilities */
3048 msm_host->mmc->caps |= msm_host->pdata->mmc_bus_width;
3049 msm_host->mmc->caps |= msm_host->pdata->caps;
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003050 msm_host->mmc->caps |= MMC_CAP_AGGRESSIVE_PM;
Asutosh Das0ef24812012-12-18 16:14:02 +05303051 msm_host->mmc->caps2 |= msm_host->pdata->caps2;
Venkat Gopalakrishnan97c16112014-12-16 18:26:25 -08003052 msm_host->mmc->caps2 |= MMC_CAP2_BOOTPART_NOACC;
3053 msm_host->mmc->caps2 |= MMC_CAP2_FULL_PWR_CYCLE;
Subhash Jadavani6d472b22013-05-29 15:52:10 +05303054 msm_host->mmc->caps2 |= MMC_CAP2_ASYNC_SDIO_IRQ_4BIT_MODE;
Venkat Gopalakrishnan97c16112014-12-16 18:26:25 -08003055 msm_host->mmc->caps2 |= MMC_CAP2_HS400_POST_TUNING;
Asutosh Dasbea115d2013-06-24 18:20:45 +05303056 msm_host->mmc->pm_caps |= MMC_PM_KEEP_POWER;
Asutosh Das0ef24812012-12-18 16:14:02 +05303057
3058 if (msm_host->pdata->nonremovable)
3059 msm_host->mmc->caps |= MMC_CAP_NONREMOVABLE;
3060
Guoping Yuf7c91332014-08-20 16:56:18 +08003061 if (msm_host->pdata->nonhotplug)
3062 msm_host->mmc->caps2 |= MMC_CAP2_NONHOTPLUG;
3063
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05303064 host->cpu_dma_latency_us = msm_host->pdata->cpu_dma_latency_us;
Maya Erez994cf2f2014-10-21 20:22:04 +03003065 host->pm_qos_req_dma.type = msm_host->pdata->cpu_affinity_type;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05303066
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05303067 init_completion(&msm_host->pwr_irq_completion);
3068
Sahitya Tummala581df132013-03-12 14:57:46 +05303069 if (gpio_is_valid(msm_host->pdata->status_gpio)) {
Sahitya Tummala6ddabb42014-06-05 13:26:55 +05303070 /*
3071 * Set up the card detect GPIO in active configuration before
3072 * configuring it as an IRQ. Otherwise, it can be in some
3073 * weird/inconsistent state resulting in flood of interrupts.
3074 */
3075 sdhci_msm_setup_pins(msm_host->pdata, true);
3076
Sahitya Tummala581df132013-03-12 14:57:46 +05303077 ret = mmc_gpio_request_cd(msm_host->mmc,
3078 msm_host->pdata->status_gpio, 0);
3079 if (ret) {
3080 dev_err(&pdev->dev, "%s: Failed to request card detection IRQ %d\n",
3081 __func__, ret);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05303082 goto vreg_deinit;
Sahitya Tummala581df132013-03-12 14:57:46 +05303083 }
3084 }
3085
Krishna Konda7feab352013-09-17 23:55:40 -07003086 if ((sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT) &&
3087 (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(64)))) {
3088 host->dma_mask = DMA_BIT_MASK(64);
3089 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
3090 } else if (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(32))) {
Sahitya Tummalaeaa21862013-03-20 19:34:59 +05303091 host->dma_mask = DMA_BIT_MASK(32);
3092 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
3093 } else {
3094 dev_err(&pdev->dev, "%s: Failed to set dma mask\n", __func__);
3095 }
3096
Asutosh Das0ef24812012-12-18 16:14:02 +05303097 ret = sdhci_add_host(host);
3098 if (ret) {
3099 dev_err(&pdev->dev, "Add host failed (%d)\n", ret);
Sahitya Tummala581df132013-03-12 14:57:46 +05303100 goto vreg_deinit;
Asutosh Das0ef24812012-12-18 16:14:02 +05303101 }
3102
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003103 pm_runtime_set_active(&pdev->dev);
3104 pm_runtime_enable(&pdev->dev);
3105 pm_runtime_set_autosuspend_delay(&pdev->dev, MSM_AUTOSUSPEND_DELAY_MS);
3106 pm_runtime_use_autosuspend(&pdev->dev);
3107
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05303108 msm_host->msm_bus_vote.max_bus_bw.show = show_sdhci_max_bus_bw;
3109 msm_host->msm_bus_vote.max_bus_bw.store = store_sdhci_max_bus_bw;
3110 sysfs_attr_init(&msm_host->msm_bus_vote.max_bus_bw.attr);
3111 msm_host->msm_bus_vote.max_bus_bw.attr.name = "max_bus_bw";
3112 msm_host->msm_bus_vote.max_bus_bw.attr.mode = S_IRUGO | S_IWUSR;
3113 ret = device_create_file(&pdev->dev,
3114 &msm_host->msm_bus_vote.max_bus_bw);
3115 if (ret)
3116 goto remove_host;
3117
Sahitya Tummala5c55b932013-06-20 14:00:18 +05303118 if (!gpio_is_valid(msm_host->pdata->status_gpio)) {
3119 msm_host->polling.show = show_polling;
3120 msm_host->polling.store = store_polling;
3121 sysfs_attr_init(&msm_host->polling.attr);
3122 msm_host->polling.attr.name = "polling";
3123 msm_host->polling.attr.mode = S_IRUGO | S_IWUSR;
3124 ret = device_create_file(&pdev->dev, &msm_host->polling);
3125 if (ret)
3126 goto remove_max_bus_bw_file;
3127 }
Asutosh Dase5e9ca62013-07-30 19:08:36 +05303128
3129 msm_host->auto_cmd21_attr.show = show_auto_cmd21;
3130 msm_host->auto_cmd21_attr.store = store_auto_cmd21;
3131 sysfs_attr_init(&msm_host->auto_cmd21_attr.attr);
3132 msm_host->auto_cmd21_attr.attr.name = "enable_auto_cmd21";
3133 msm_host->auto_cmd21_attr.attr.mode = S_IRUGO | S_IWUSR;
3134 ret = device_create_file(&pdev->dev, &msm_host->auto_cmd21_attr);
3135 if (ret) {
3136 pr_err("%s: %s: failed creating auto-cmd21 attr: %d\n",
3137 mmc_hostname(host->mmc), __func__, ret);
3138 device_remove_file(&pdev->dev, &msm_host->auto_cmd21_attr);
3139 }
Asutosh Das0ef24812012-12-18 16:14:02 +05303140 /* Successful initialization */
3141 goto out;
3142
Sahitya Tummala5c55b932013-06-20 14:00:18 +05303143remove_max_bus_bw_file:
3144 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Asutosh Das0ef24812012-12-18 16:14:02 +05303145remove_host:
3146 dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003147 pm_runtime_disable(&pdev->dev);
Asutosh Das0ef24812012-12-18 16:14:02 +05303148 sdhci_remove_host(host, dead);
3149vreg_deinit:
3150 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05303151bus_unregister:
3152 if (msm_host->msm_bus_vote.client_handle)
3153 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
3154 sdhci_msm_bus_unregister(msm_host);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07003155sleep_clk_disable:
3156 if (!IS_ERR(msm_host->sleep_clk))
3157 clk_disable_unprepare(msm_host->sleep_clk);
3158ff_clk_disable:
3159 if (!IS_ERR(msm_host->ff_clk))
3160 clk_disable_unprepare(msm_host->ff_clk);
Asutosh Das0ef24812012-12-18 16:14:02 +05303161clk_disable:
3162 if (!IS_ERR(msm_host->clk))
3163 clk_disable_unprepare(msm_host->clk);
3164pclk_disable:
3165 if (!IS_ERR(msm_host->pclk))
3166 clk_disable_unprepare(msm_host->pclk);
3167bus_clk_disable:
3168 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
3169 clk_disable_unprepare(msm_host->bus_clk);
3170pltfm_free:
3171 sdhci_pltfm_free(pdev);
3172out:
3173 pr_debug("%s: Exit %s\n", dev_name(&pdev->dev), __func__);
3174 return ret;
3175}
3176
3177static int sdhci_msm_remove(struct platform_device *pdev)
3178{
3179 struct sdhci_host *host = platform_get_drvdata(pdev);
3180 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3181 struct sdhci_msm_host *msm_host = pltfm_host->priv;
3182 struct sdhci_msm_pltfm_data *pdata = msm_host->pdata;
3183 int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) ==
3184 0xffffffff);
3185
3186 pr_debug("%s: %s\n", dev_name(&pdev->dev), __func__);
Sahitya Tummala5c55b932013-06-20 14:00:18 +05303187 if (!gpio_is_valid(msm_host->pdata->status_gpio))
3188 device_remove_file(&pdev->dev, &msm_host->polling);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05303189 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003190 pm_runtime_disable(&pdev->dev);
Asutosh Das0ef24812012-12-18 16:14:02 +05303191 sdhci_remove_host(host, dead);
3192 sdhci_pltfm_free(pdev);
Sahitya Tummala581df132013-03-12 14:57:46 +05303193
Asutosh Das0ef24812012-12-18 16:14:02 +05303194 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05303195
Pratibhasagar V9acf2642013-11-21 21:07:21 +05303196 sdhci_msm_setup_pins(pdata, true);
3197 sdhci_msm_setup_pins(pdata, false);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05303198
3199 if (msm_host->msm_bus_vote.client_handle) {
3200 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
3201 sdhci_msm_bus_unregister(msm_host);
3202 }
Asutosh Das0ef24812012-12-18 16:14:02 +05303203 return 0;
3204}
3205
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003206#ifdef CONFIG_PM
3207static int sdhci_msm_runtime_suspend(struct device *dev)
3208{
3209 struct sdhci_host *host = dev_get_drvdata(dev);
3210 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3211 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003212 ktime_t start = ktime_get();
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003213
3214 disable_irq(host->irq);
3215 disable_irq(msm_host->pwr_irq);
3216
3217 /*
3218 * Remove the vote immediately only if clocks are off in which
3219 * case we might have queued work to remove vote but it may not
3220 * be completed before runtime suspend or system suspend.
3221 */
3222 if (!atomic_read(&msm_host->clks_on)) {
3223 if (msm_host->msm_bus_vote.client_handle)
3224 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
3225 }
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003226 trace_sdhci_msm_runtime_suspend(mmc_hostname(host->mmc), 0,
3227 ktime_to_us(ktime_sub(ktime_get(), start)));
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003228
3229 return 0;
3230}
3231
3232static int sdhci_msm_runtime_resume(struct device *dev)
3233{
3234 struct sdhci_host *host = dev_get_drvdata(dev);
3235 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3236 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003237 ktime_t start = ktime_get();
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003238
3239 enable_irq(msm_host->pwr_irq);
3240 enable_irq(host->irq);
3241
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003242 trace_sdhci_msm_runtime_resume(mmc_hostname(host->mmc), 0,
3243 ktime_to_us(ktime_sub(ktime_get(), start)));
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003244 return 0;
3245}
3246
3247static int sdhci_msm_suspend(struct device *dev)
3248{
3249 struct sdhci_host *host = dev_get_drvdata(dev);
3250 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3251 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003252 int ret = 0;
3253 ktime_t start = ktime_get();
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003254
3255 if (gpio_is_valid(msm_host->pdata->status_gpio) &&
3256 (msm_host->mmc->slot.cd_irq >= 0))
3257 disable_irq(msm_host->mmc->slot.cd_irq);
3258
3259 if (pm_runtime_suspended(dev)) {
3260 pr_debug("%s: %s: already runtime suspended\n",
3261 mmc_hostname(host->mmc), __func__);
3262 goto out;
3263 }
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003264 ret = sdhci_msm_runtime_suspend(dev);
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003265out:
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003266 trace_sdhci_msm_suspend(mmc_hostname(host->mmc), ret,
3267 ktime_to_us(ktime_sub(ktime_get(), start)));
3268 return ret;
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003269}
3270
3271static int sdhci_msm_resume(struct device *dev)
3272{
3273 struct sdhci_host *host = dev_get_drvdata(dev);
3274 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3275 struct sdhci_msm_host *msm_host = pltfm_host->priv;
3276 int ret = 0;
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003277 ktime_t start = ktime_get();
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003278
3279 if (gpio_is_valid(msm_host->pdata->status_gpio) &&
3280 (msm_host->mmc->slot.cd_irq >= 0))
3281 enable_irq(msm_host->mmc->slot.cd_irq);
3282
3283 if (pm_runtime_suspended(dev)) {
3284 pr_debug("%s: %s: runtime suspended, defer system resume\n",
3285 mmc_hostname(host->mmc), __func__);
3286 goto out;
3287 }
3288
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003289 ret = sdhci_msm_runtime_resume(dev);
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003290out:
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003291 trace_sdhci_msm_resume(mmc_hostname(host->mmc), ret,
3292 ktime_to_us(ktime_sub(ktime_get(), start)));
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003293 return ret;
3294}
3295
3296static const struct dev_pm_ops sdhci_msm_pmops = {
3297 SET_SYSTEM_SLEEP_PM_OPS(sdhci_msm_suspend, sdhci_msm_resume)
3298 SET_RUNTIME_PM_OPS(sdhci_msm_runtime_suspend, sdhci_msm_runtime_resume,
3299 NULL)
3300};
3301
3302#define SDHCI_MSM_PMOPS (&sdhci_msm_pmops)
3303
3304#else
3305#define SDHCI_MSM_PMOPS NULL
3306#endif
Asutosh Das0ef24812012-12-18 16:14:02 +05303307static const struct of_device_id sdhci_msm_dt_match[] = {
3308 {.compatible = "qcom,sdhci-msm"},
3309};
3310MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match);
3311
3312static struct platform_driver sdhci_msm_driver = {
3313 .probe = sdhci_msm_probe,
3314 .remove = sdhci_msm_remove,
3315 .driver = {
3316 .name = "sdhci_msm",
3317 .owner = THIS_MODULE,
3318 .of_match_table = sdhci_msm_dt_match,
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003319 .pm = SDHCI_MSM_PMOPS,
Asutosh Das0ef24812012-12-18 16:14:02 +05303320 },
3321};
3322
3323module_platform_driver(sdhci_msm_driver);
3324
3325MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Secure Digital Host Controller Interface driver");
3326MODULE_LICENSE("GPL v2");