blob: 3d4aa8493ec55f473abb0c7282568d3d27e41e42 [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"
Venkat Gopalakrishnand371f142015-05-29 17:49:46 -070044#include "cmdq_hci.h"
Asutosh Das0ef24812012-12-18 16:14:02 +053045
Venkat Gopalakrishnan0a92d532014-12-16 17:31:00 -080046#define CORE_POWER 0x0
47#define CORE_SW_RST (1 << 7)
48
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -070049#define SDHCI_VER_100 0x2B
Venkat Gopalakrishnan0a92d532014-12-16 17:31:00 -080050#define CORE_MCI_DATA_CNT 0x30
51#define CORE_MCI_STATUS 0x34
52#define CORE_MCI_FIFO_CNT 0x44
53
54#define CORE_VERSION_STEP_MASK 0x0000FFFF
55#define CORE_VERSION_MINOR_MASK 0x0FFF0000
56#define CORE_VERSION_MINOR_SHIFT 16
57#define CORE_VERSION_MAJOR_MASK 0xF0000000
58#define CORE_VERSION_MAJOR_SHIFT 28
59#define CORE_VERSION_TARGET_MASK 0x000000FF
60
61#define CORE_GENERICS 0x70
62#define SWITCHABLE_SIGNALLING_VOL (1 << 29)
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +053063
64#define CORE_VERSION_MAJOR_MASK 0xF0000000
65#define CORE_VERSION_MAJOR_SHIFT 28
66
Asutosh Das0ef24812012-12-18 16:14:02 +053067#define CORE_HC_MODE 0x78
68#define HC_MODE_EN 0x1
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -070069#define FF_CLK_SW_RST_DIS (1 << 13)
Asutosh Das0ef24812012-12-18 16:14:02 +053070
Sahitya Tummala67717bc2013-08-02 09:21:37 +053071#define CORE_MCI_VERSION 0x050
72#define CORE_TESTBUS_CONFIG 0x0CC
73#define CORE_TESTBUS_ENA (1 << 3)
Venkat Gopalakrishnan0a92d532014-12-16 17:31:00 -080074#define CORE_TESTBUS_SEL2_BIT 4
75#define CORE_TESTBUS_SEL2 (1 << CORE_TESTBUS_SEL2_BIT)
Sahitya Tummala67717bc2013-08-02 09:21:37 +053076
Asutosh Das0ef24812012-12-18 16:14:02 +053077#define CORE_PWRCTL_STATUS 0xDC
78#define CORE_PWRCTL_MASK 0xE0
79#define CORE_PWRCTL_CLEAR 0xE4
80#define CORE_PWRCTL_CTL 0xE8
81
82#define CORE_PWRCTL_BUS_OFF 0x01
83#define CORE_PWRCTL_BUS_ON (1 << 1)
84#define CORE_PWRCTL_IO_LOW (1 << 2)
85#define CORE_PWRCTL_IO_HIGH (1 << 3)
86
87#define CORE_PWRCTL_BUS_SUCCESS 0x01
88#define CORE_PWRCTL_BUS_FAIL (1 << 1)
89#define CORE_PWRCTL_IO_SUCCESS (1 << 2)
90#define CORE_PWRCTL_IO_FAIL (1 << 3)
91
92#define INT_MASK 0xF
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -070093#define MAX_PHASES 16
94
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -070095#define CORE_DLL_CONFIG 0x100
96#define CORE_CMD_DAT_TRACK_SEL (1 << 0)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -070097#define CORE_DLL_EN (1 << 16)
98#define CORE_CDR_EN (1 << 17)
99#define CORE_CK_OUT_EN (1 << 18)
100#define CORE_CDR_EXT_EN (1 << 19)
101#define CORE_DLL_PDN (1 << 29)
102#define CORE_DLL_RST (1 << 30)
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700103
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700104#define CORE_DLL_STATUS 0x108
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700105#define CORE_DLL_LOCK (1 << 7)
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700106#define CORE_DDR_DLL_LOCK (1 << 11)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700107
108#define CORE_VENDOR_SPEC 0x10C
Krishna Konda46fd1432014-10-30 21:13:27 -0700109#define CORE_CLK_PWRSAVE (1 << 1)
110#define CORE_HC_MCLK_SEL_DFLT (2 << 8)
111#define CORE_HC_MCLK_SEL_HS400 (3 << 8)
112#define CORE_HC_MCLK_SEL_MASK (3 << 8)
113#define CORE_HC_AUTO_CMD21_EN (1 << 6)
114#define CORE_IO_PAD_PWR_SWITCH_EN (1 << 15)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700115#define CORE_IO_PAD_PWR_SWITCH (1 << 16)
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700116#define CORE_HC_SELECT_IN_EN (1 << 18)
117#define CORE_HC_SELECT_IN_HS400 (6 << 19)
118#define CORE_HC_SELECT_IN_MASK (7 << 19)
Venkat Gopalakrishnan17edb352015-06-24 14:46:49 -0700119#define CORE_VENDOR_SPEC_POR_VAL 0xA1C
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700120
Venkat Gopalakrishnan0a92d532014-12-16 17:31:00 -0800121#define CORE_VENDOR_SPEC_ADMA_ERR_ADDR0 0x114
122#define CORE_VENDOR_SPEC_ADMA_ERR_ADDR1 0x118
123
Krishna Konda7feab352013-09-17 23:55:40 -0700124#define CORE_VENDOR_SPEC_CAPABILITIES0 0x11C
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +0530125#define CORE_8_BIT_SUPPORT (1 << 18)
126#define CORE_3_3V_SUPPORT (1 << 24)
127#define CORE_3_0V_SUPPORT (1 << 25)
128#define CORE_1_8V_SUPPORT (1 << 26)
Gilad Broner2a10ca02014-10-02 17:20:35 +0300129#define CORE_SYS_BUS_SUPPORT_64_BIT BIT(28)
Krishna Konda7feab352013-09-17 23:55:40 -0700130
Venkat Gopalakrishnan0a92d532014-12-16 17:31:00 -0800131#define CORE_SDCC_DEBUG_REG 0x124
Sahitya Tummala67717bc2013-08-02 09:21:37 +0530132
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700133#define CORE_CSR_CDC_CTLR_CFG0 0x130
134#define CORE_SW_TRIG_FULL_CALIB (1 << 16)
135#define CORE_HW_AUTOCAL_ENA (1 << 17)
136
137#define CORE_CSR_CDC_CTLR_CFG1 0x134
138#define CORE_CSR_CDC_CAL_TIMER_CFG0 0x138
139#define CORE_TIMER_ENA (1 << 16)
140
141#define CORE_CSR_CDC_CAL_TIMER_CFG1 0x13C
142#define CORE_CSR_CDC_REFCOUNT_CFG 0x140
143#define CORE_CSR_CDC_COARSE_CAL_CFG 0x144
144#define CORE_CDC_OFFSET_CFG 0x14C
145#define CORE_CSR_CDC_DELAY_CFG 0x150
146#define CORE_CDC_SLAVE_DDA_CFG 0x160
147#define CORE_CSR_CDC_STATUS0 0x164
148#define CORE_CALIBRATION_DONE (1 << 0)
149
150#define CORE_CDC_ERROR_CODE_MASK 0x7000000
151
152#define CORE_CSR_CDC_GEN_CFG 0x178
153#define CORE_CDC_SWITCH_BYPASS_OFF (1 << 0)
154#define CORE_CDC_SWITCH_RC_EN (1 << 1)
155
156#define CORE_DDR_200_CFG 0x184
157#define CORE_CDC_T4_DLY_SEL (1 << 0)
158#define CORE_START_CDC_TRAFFIC (1 << 6)
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700159#define CORE_VENDOR_SPEC3 0x1B0
160#define CORE_PWRSAVE_DLL (1 << 3)
161
162#define CORE_DLL_CONFIG_2 0x1B4
163#define CORE_DDR_CAL_EN (1 << 0)
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800164#define CORE_FLL_CYCLE_CNT (1 << 18)
165#define CORE_DLL_CLOCK_DISABLE (1 << 21)
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700166
167#define CORE_DDR_CONFIG 0x1B8
168#define DDR_CONFIG_POR_VAL 0x80040853
169
Sahitya Tummala56874732015-05-21 08:24:03 +0530170#define MSM_MMC_DEFAULT_CPU_DMA_LATENCY 200 /* usecs */
171
Venkat Gopalakrishnan450745e2014-07-24 20:39:34 -0700172/* 512 descriptors */
173#define SDHCI_MSM_MAX_SEGMENTS (1 << 9)
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +0530174#define SDHCI_MSM_MMC_CLK_GATE_DELAY 200 /* msecs */
Asutosh Das648f9d12013-01-10 21:11:04 +0530175
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700176#define CORE_FREQ_100MHZ (100 * 1000 * 1000)
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800177#define TCXO_FREQ 19200000
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700178
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700179#define INVALID_TUNING_PHASE -1
180
Krishna Konda96e6b112013-10-28 15:25:03 -0700181#define NUM_TUNING_PHASES 16
182#define MAX_DRV_TYPES_SUPPORTED_HS200 3
Konstantin Dorfman98377d32015-02-25 10:09:41 +0200183#define MSM_AUTOSUSPEND_DELAY_MS 100
Krishna Konda96e6b112013-10-28 15:25:03 -0700184
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700185static const u32 tuning_block_64[] = {
186 0x00FF0FFF, 0xCCC3CCFF, 0xFFCC3CC3, 0xEFFEFFFE,
187 0xDDFFDFFF, 0xFBFFFBFF, 0xFF7FFFBF, 0xEFBDF777,
188 0xF0FFF0FF, 0x3CCCFC0F, 0xCFCC33CC, 0xEEFFEFFF,
189 0xFDFFFDFF, 0xFFBFFFDF, 0xFFF7FFBB, 0xDE7B7FF7
190};
191
192static const u32 tuning_block_128[] = {
193 0xFF00FFFF, 0x0000FFFF, 0xCCCCFFFF, 0xCCCC33CC,
194 0xCC3333CC, 0xFFFFCCCC, 0xFFFFEEFF, 0xFFEEEEFF,
195 0xFFDDFFFF, 0xDDDDFFFF, 0xBBFFFFFF, 0xBBFFFFFF,
196 0xFFFFFFBB, 0xFFFFFF77, 0x77FF7777, 0xFFEEDDBB,
197 0x00FFFFFF, 0x00FFFFFF, 0xCCFFFF00, 0xCC33CCCC,
198 0x3333CCCC, 0xFFCCCCCC, 0xFFEEFFFF, 0xEEEEFFFF,
199 0xDDFFFFFF, 0xDDFFFFFF, 0xFFFFFFDD, 0xFFFFFFBB,
200 0xFFFFBBBB, 0xFFFF77FF, 0xFF7777FF, 0xEEDDBB77
201};
Asutosh Das0ef24812012-12-18 16:14:02 +0530202
Venkat Gopalakrishnan270580a2013-03-11 12:17:57 -0700203static int disable_slots;
204/* root can write, others read */
205module_param(disable_slots, int, S_IRUGO|S_IWUSR);
206
Asutosh Das0ef24812012-12-18 16:14:02 +0530207enum vdd_io_level {
208 /* set vdd_io_data->low_vol_level */
209 VDD_IO_LOW,
210 /* set vdd_io_data->high_vol_level */
211 VDD_IO_HIGH,
212 /*
213 * set whatever there in voltage_level (third argument) of
214 * sdhci_msm_set_vdd_io_vol() function.
215 */
216 VDD_IO_SET_LEVEL,
217};
218
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700219/* MSM platform specific tuning */
220static inline int msm_dll_poll_ck_out_en(struct sdhci_host *host,
221 u8 poll)
222{
223 int rc = 0;
224 u32 wait_cnt = 50;
225 u8 ck_out_en = 0;
226 struct mmc_host *mmc = host->mmc;
227
228 /* poll for CK_OUT_EN bit. max. poll time = 50us */
229 ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) &
230 CORE_CK_OUT_EN);
231
232 while (ck_out_en != poll) {
233 if (--wait_cnt == 0) {
234 pr_err("%s: %s: CK_OUT_EN bit is not %d\n",
235 mmc_hostname(mmc), __func__, poll);
236 rc = -ETIMEDOUT;
237 goto out;
238 }
239 udelay(1);
240
241 ck_out_en = !!(readl_relaxed(host->ioaddr +
242 CORE_DLL_CONFIG) & CORE_CK_OUT_EN);
243 }
244out:
245 return rc;
246}
247
Asutosh Dase5e9ca62013-07-30 19:08:36 +0530248/*
249 * Enable CDR to track changes of DAT lines and adjust sampling
250 * point according to voltage/temperature variations
251 */
252static int msm_enable_cdr_cm_sdc4_dll(struct sdhci_host *host)
253{
254 int rc = 0;
255 u32 config;
256
257 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
258 config |= CORE_CDR_EN;
259 config &= ~(CORE_CDR_EXT_EN | CORE_CK_OUT_EN);
260 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
261
262 rc = msm_dll_poll_ck_out_en(host, 0);
263 if (rc)
264 goto err;
265
266 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) |
267 CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
268
269 rc = msm_dll_poll_ck_out_en(host, 1);
270 if (rc)
271 goto err;
272 goto out;
273err:
274 pr_err("%s: %s: failed\n", mmc_hostname(host->mmc), __func__);
275out:
276 return rc;
277}
278
279static ssize_t store_auto_cmd21(struct device *dev, struct device_attribute
280 *attr, const char *buf, size_t count)
281{
282 struct sdhci_host *host = dev_get_drvdata(dev);
283 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
284 struct sdhci_msm_host *msm_host = pltfm_host->priv;
285 u32 tmp;
286 unsigned long flags;
287
288 if (!kstrtou32(buf, 0, &tmp)) {
289 spin_lock_irqsave(&host->lock, flags);
290 msm_host->en_auto_cmd21 = !!tmp;
291 spin_unlock_irqrestore(&host->lock, flags);
292 }
293 return count;
294}
295
296static ssize_t show_auto_cmd21(struct device *dev,
297 struct device_attribute *attr, char *buf)
298{
299 struct sdhci_host *host = dev_get_drvdata(dev);
300 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
301 struct sdhci_msm_host *msm_host = pltfm_host->priv;
302
303 return snprintf(buf, PAGE_SIZE, "%d\n", msm_host->en_auto_cmd21);
304}
305
306/* MSM auto-tuning handler */
307static int sdhci_msm_config_auto_tuning_cmd(struct sdhci_host *host,
308 bool enable,
309 u32 type)
310{
311 int rc = 0;
312 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
313 struct sdhci_msm_host *msm_host = pltfm_host->priv;
314 u32 val = 0;
315
316 if (!msm_host->en_auto_cmd21)
317 return 0;
318
319 if (type == MMC_SEND_TUNING_BLOCK_HS200)
320 val = CORE_HC_AUTO_CMD21_EN;
321 else
322 return 0;
323
324 if (enable) {
325 rc = msm_enable_cdr_cm_sdc4_dll(host);
326 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
327 val, host->ioaddr + CORE_VENDOR_SPEC);
328 } else {
329 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
330 ~val, host->ioaddr + CORE_VENDOR_SPEC);
331 }
332 return rc;
333}
334
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700335static int msm_config_cm_dll_phase(struct sdhci_host *host, u8 phase)
336{
337 int rc = 0;
338 u8 grey_coded_phase_table[] = {0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4,
339 0xC, 0xD, 0xF, 0xE, 0xA, 0xB, 0x9,
340 0x8};
341 unsigned long flags;
342 u32 config;
343 struct mmc_host *mmc = host->mmc;
344
345 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
346 spin_lock_irqsave(&host->lock, flags);
347
348 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
349 config &= ~(CORE_CDR_EN | CORE_CK_OUT_EN);
350 config |= (CORE_CDR_EXT_EN | CORE_DLL_EN);
351 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
352
353 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '0' */
354 rc = msm_dll_poll_ck_out_en(host, 0);
355 if (rc)
356 goto err_out;
357
358 /*
359 * Write the selected DLL clock output phase (0 ... 15)
360 * to CDR_SELEXT bit field of DLL_CONFIG register.
361 */
362 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
363 & ~(0xF << 20))
364 | (grey_coded_phase_table[phase] << 20)),
365 host->ioaddr + CORE_DLL_CONFIG);
366
367 /* Set CK_OUT_EN bit of DLL_CONFIG register to 1. */
368 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
369 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
370
371 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '1' */
372 rc = msm_dll_poll_ck_out_en(host, 1);
373 if (rc)
374 goto err_out;
375
376 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
377 config |= CORE_CDR_EN;
378 config &= ~CORE_CDR_EXT_EN;
379 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
380 goto out;
381
382err_out:
383 pr_err("%s: %s: Failed to set DLL phase: %d\n",
384 mmc_hostname(mmc), __func__, phase);
385out:
386 spin_unlock_irqrestore(&host->lock, flags);
387 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
388 return rc;
389}
390
391/*
392 * Find out the greatest range of consecuitive selected
393 * DLL clock output phases that can be used as sampling
394 * setting for SD3.0 UHS-I card read operation (in SDR104
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700395 * timing mode) or for eMMC4.5 card read operation (in
396 * HS400/HS200 timing mode).
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700397 * Select the 3/4 of the range and configure the DLL with the
398 * selected DLL clock output phase.
399 */
400
401static int msm_find_most_appropriate_phase(struct sdhci_host *host,
402 u8 *phase_table, u8 total_phases)
403{
404 int ret;
405 u8 ranges[MAX_PHASES][MAX_PHASES] = { {0}, {0} };
406 u8 phases_per_row[MAX_PHASES] = {0};
407 int row_index = 0, col_index = 0, selected_row_index = 0, curr_max = 0;
408 int i, cnt, phase_0_raw_index = 0, phase_15_raw_index = 0;
409 bool phase_0_found = false, phase_15_found = false;
410 struct mmc_host *mmc = host->mmc;
411
412 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
413 if (!total_phases || (total_phases > MAX_PHASES)) {
414 pr_err("%s: %s: invalid argument: total_phases=%d\n",
415 mmc_hostname(mmc), __func__, total_phases);
416 return -EINVAL;
417 }
418
419 for (cnt = 0; cnt < total_phases; cnt++) {
420 ranges[row_index][col_index] = phase_table[cnt];
421 phases_per_row[row_index] += 1;
422 col_index++;
423
424 if ((cnt + 1) == total_phases) {
425 continue;
426 /* check if next phase in phase_table is consecutive or not */
427 } else if ((phase_table[cnt] + 1) != phase_table[cnt + 1]) {
428 row_index++;
429 col_index = 0;
430 }
431 }
432
433 if (row_index >= MAX_PHASES)
434 return -EINVAL;
435
436 /* Check if phase-0 is present in first valid window? */
437 if (!ranges[0][0]) {
438 phase_0_found = true;
439 phase_0_raw_index = 0;
440 /* Check if cycle exist between 2 valid windows */
441 for (cnt = 1; cnt <= row_index; cnt++) {
442 if (phases_per_row[cnt]) {
443 for (i = 0; i < phases_per_row[cnt]; i++) {
444 if (ranges[cnt][i] == 15) {
445 phase_15_found = true;
446 phase_15_raw_index = cnt;
447 break;
448 }
449 }
450 }
451 }
452 }
453
454 /* If 2 valid windows form cycle then merge them as single window */
455 if (phase_0_found && phase_15_found) {
456 /* number of phases in raw where phase 0 is present */
457 u8 phases_0 = phases_per_row[phase_0_raw_index];
458 /* number of phases in raw where phase 15 is present */
459 u8 phases_15 = phases_per_row[phase_15_raw_index];
460
461 if (phases_0 + phases_15 >= MAX_PHASES)
462 /*
463 * If there are more than 1 phase windows then total
464 * number of phases in both the windows should not be
465 * more than or equal to MAX_PHASES.
466 */
467 return -EINVAL;
468
469 /* Merge 2 cyclic windows */
470 i = phases_15;
471 for (cnt = 0; cnt < phases_0; cnt++) {
472 ranges[phase_15_raw_index][i] =
473 ranges[phase_0_raw_index][cnt];
474 if (++i >= MAX_PHASES)
475 break;
476 }
477
478 phases_per_row[phase_0_raw_index] = 0;
479 phases_per_row[phase_15_raw_index] = phases_15 + phases_0;
480 }
481
482 for (cnt = 0; cnt <= row_index; cnt++) {
483 if (phases_per_row[cnt] > curr_max) {
484 curr_max = phases_per_row[cnt];
485 selected_row_index = cnt;
486 }
487 }
488
489 i = ((curr_max * 3) / 4);
490 if (i)
491 i--;
492
493 ret = (int)ranges[selected_row_index][i];
494
495 if (ret >= MAX_PHASES) {
496 ret = -EINVAL;
497 pr_err("%s: %s: invalid phase selected=%d\n",
498 mmc_hostname(mmc), __func__, ret);
499 }
500
501 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
502 return ret;
503}
504
505static inline void msm_cm_dll_set_freq(struct sdhci_host *host)
506{
507 u32 mclk_freq = 0;
508
509 /* Program the MCLK value to MCLK_FREQ bit field */
510 if (host->clock <= 112000000)
511 mclk_freq = 0;
512 else if (host->clock <= 125000000)
513 mclk_freq = 1;
514 else if (host->clock <= 137000000)
515 mclk_freq = 2;
516 else if (host->clock <= 150000000)
517 mclk_freq = 3;
518 else if (host->clock <= 162000000)
519 mclk_freq = 4;
520 else if (host->clock <= 175000000)
521 mclk_freq = 5;
522 else if (host->clock <= 187000000)
523 mclk_freq = 6;
524 else if (host->clock <= 200000000)
525 mclk_freq = 7;
526
527 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
528 & ~(7 << 24)) | (mclk_freq << 24)),
529 host->ioaddr + CORE_DLL_CONFIG);
530}
531
532/* Initialize the DLL (Programmable Delay Line ) */
533static int msm_init_cm_dll(struct sdhci_host *host)
534{
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800535 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
536 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700537 struct mmc_host *mmc = host->mmc;
538 int rc = 0;
539 unsigned long flags;
540 u32 wait_cnt;
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530541 bool prev_pwrsave, curr_pwrsave;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700542
543 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
544 spin_lock_irqsave(&host->lock, flags);
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530545 prev_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
546 CORE_CLK_PWRSAVE);
547 curr_pwrsave = prev_pwrsave;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700548 /*
549 * Make sure that clock is always enabled when DLL
550 * tuning is in progress. Keeping PWRSAVE ON may
551 * turn off the clock. So let's disable the PWRSAVE
552 * here and re-enable it once tuning is completed.
553 */
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530554 if (prev_pwrsave) {
555 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
556 & ~CORE_CLK_PWRSAVE),
557 host->ioaddr + CORE_VENDOR_SPEC);
558 curr_pwrsave = false;
559 }
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700560
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800561 if (msm_host->use_updated_dll_reset) {
562 /* Disable the DLL clock */
563 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
564 & ~CORE_CK_OUT_EN),
565 host->ioaddr + CORE_DLL_CONFIG);
566
567 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
568 | CORE_DLL_CLOCK_DISABLE),
569 host->ioaddr + CORE_DLL_CONFIG_2);
570 }
571
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700572 /* Write 1 to DLL_RST bit of DLL_CONFIG register */
573 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
574 | CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
575
576 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
577 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
578 | CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
579 msm_cm_dll_set_freq(host);
580
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800581 if (msm_host->use_updated_dll_reset) {
582 u32 mclk_freq = 0;
583
584 if ((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
585 & CORE_FLL_CYCLE_CNT))
586 mclk_freq = (u32) ((host->clock / TCXO_FREQ) * 8);
587 else
588 mclk_freq = (u32) ((host->clock / TCXO_FREQ) * 4);
589
590 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
591 & ~(0xFF << 10)) | (mclk_freq << 10)),
592 host->ioaddr + CORE_DLL_CONFIG_2);
593 /* wait for 5us before enabling DLL clock */
594 udelay(5);
595 }
596
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700597 /* Write 0 to DLL_RST bit of DLL_CONFIG register */
598 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
599 & ~CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
600
601 /* Write 0 to DLL_PDN bit of DLL_CONFIG register */
602 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
603 & ~CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
604
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800605 if (msm_host->use_updated_dll_reset) {
606 msm_cm_dll_set_freq(host);
607 /* Enable the DLL clock */
608 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
609 & ~CORE_DLL_CLOCK_DISABLE),
610 host->ioaddr + CORE_DLL_CONFIG_2);
611 }
612
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700613 /* Set DLL_EN bit to 1. */
614 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
615 | CORE_DLL_EN), host->ioaddr + CORE_DLL_CONFIG);
616
617 /* Set CK_OUT_EN bit to 1. */
618 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
619 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
620
621 wait_cnt = 50;
622 /* Wait until DLL_LOCK bit of DLL_STATUS register becomes '1' */
623 while (!(readl_relaxed(host->ioaddr + CORE_DLL_STATUS) &
624 CORE_DLL_LOCK)) {
625 /* max. wait for 50us sec for LOCK bit to be set */
626 if (--wait_cnt == 0) {
627 pr_err("%s: %s: DLL failed to LOCK\n",
628 mmc_hostname(mmc), __func__);
629 rc = -ETIMEDOUT;
630 goto out;
631 }
632 /* wait for 1us before polling again */
633 udelay(1);
634 }
635
636out:
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530637 /* Restore the correct PWRSAVE state */
638 if (prev_pwrsave ^ curr_pwrsave) {
639 u32 reg = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC);
640
641 if (prev_pwrsave)
642 reg |= CORE_CLK_PWRSAVE;
643 else
644 reg &= ~CORE_CLK_PWRSAVE;
645
646 writel_relaxed(reg, host->ioaddr + CORE_VENDOR_SPEC);
647 }
648
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700649 spin_unlock_irqrestore(&host->lock, flags);
650 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
651 return rc;
652}
653
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700654static int sdhci_msm_cdclp533_calibration(struct sdhci_host *host)
655{
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700656 u32 calib_done;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700657 int ret = 0;
658 int cdc_err = 0;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700659
660 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
661
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700662 /* Write 0 to CDC_T4_DLY_SEL field in VENDOR_SPEC_DDR200_CFG */
663 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
664 & ~CORE_CDC_T4_DLY_SEL),
665 host->ioaddr + CORE_DDR_200_CFG);
666
667 /* Write 0 to CDC_SWITCH_BYPASS_OFF field in CORE_CSR_CDC_GEN_CFG */
668 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG)
669 & ~CORE_CDC_SWITCH_BYPASS_OFF),
670 host->ioaddr + CORE_CSR_CDC_GEN_CFG);
671
672 /* Write 1 to CDC_SWITCH_RC_EN field in CORE_CSR_CDC_GEN_CFG */
673 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG)
674 | CORE_CDC_SWITCH_RC_EN),
675 host->ioaddr + CORE_CSR_CDC_GEN_CFG);
676
677 /* Write 0 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
678 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
679 & ~CORE_START_CDC_TRAFFIC),
680 host->ioaddr + CORE_DDR_200_CFG);
681
682 /*
683 * Perform CDC Register Initialization Sequence
684 *
685 * CORE_CSR_CDC_CTLR_CFG0 0x11800EC
686 * CORE_CSR_CDC_CTLR_CFG1 0x3011111
687 * CORE_CSR_CDC_CAL_TIMER_CFG0 0x1201000
688 * CORE_CSR_CDC_CAL_TIMER_CFG1 0x4
689 * CORE_CSR_CDC_REFCOUNT_CFG 0xCB732020
690 * CORE_CSR_CDC_COARSE_CAL_CFG 0xB19
691 * CORE_CSR_CDC_DELAY_CFG 0x3AC
692 * CORE_CDC_OFFSET_CFG 0x0
693 * CORE_CDC_SLAVE_DDA_CFG 0x16334
694 */
695
696 writel_relaxed(0x11800EC, host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
697 writel_relaxed(0x3011111, host->ioaddr + CORE_CSR_CDC_CTLR_CFG1);
698 writel_relaxed(0x1201000, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
699 writel_relaxed(0x4, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG1);
700 writel_relaxed(0xCB732020, host->ioaddr + CORE_CSR_CDC_REFCOUNT_CFG);
701 writel_relaxed(0xB19, host->ioaddr + CORE_CSR_CDC_COARSE_CAL_CFG);
Subhash Jadavanibe406d92014-06-17 16:47:48 -0700702 writel_relaxed(0x4E2, host->ioaddr + CORE_CSR_CDC_DELAY_CFG);
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700703 writel_relaxed(0x0, host->ioaddr + CORE_CDC_OFFSET_CFG);
704 writel_relaxed(0x16334, host->ioaddr + CORE_CDC_SLAVE_DDA_CFG);
705
706 /* CDC HW Calibration */
707
708 /* Write 1 to SW_TRIG_FULL_CALIB field in CORE_CSR_CDC_CTLR_CFG0 */
709 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
710 | CORE_SW_TRIG_FULL_CALIB),
711 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
712
713 /* Write 0 to SW_TRIG_FULL_CALIB field in CORE_CSR_CDC_CTLR_CFG0 */
714 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
715 & ~CORE_SW_TRIG_FULL_CALIB),
716 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
717
718 /* Write 1 to HW_AUTOCAL_ENA field in CORE_CSR_CDC_CTLR_CFG0 */
719 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
720 | CORE_HW_AUTOCAL_ENA),
721 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
722
723 /* Write 1 to TIMER_ENA field in CORE_CSR_CDC_CAL_TIMER_CFG0 */
724 writel_relaxed((readl_relaxed(host->ioaddr +
725 CORE_CSR_CDC_CAL_TIMER_CFG0) | CORE_TIMER_ENA),
726 host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
727
728 mb();
729
730 /* Poll on CALIBRATION_DONE field in CORE_CSR_CDC_STATUS0 to be 1 */
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700731 ret = readl_poll_timeout(host->ioaddr + CORE_CSR_CDC_STATUS0,
732 calib_done, (calib_done & CORE_CALIBRATION_DONE), 1, 50);
733
734 if (ret == -ETIMEDOUT) {
735 pr_err("%s: %s: CDC Calibration was not completed\n",
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700736 mmc_hostname(host->mmc), __func__);
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700737 goto out;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700738 }
739
740 /* Verify CDC_ERROR_CODE field in CORE_CSR_CDC_STATUS0 is 0 */
741 cdc_err = readl_relaxed(host->ioaddr + CORE_CSR_CDC_STATUS0)
742 & CORE_CDC_ERROR_CODE_MASK;
743 if (cdc_err) {
744 pr_err("%s: %s: CDC Error Code %d\n",
745 mmc_hostname(host->mmc), __func__, cdc_err);
746 ret = -EINVAL;
747 goto out;
748 }
749
750 /* Write 1 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
751 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
752 | CORE_START_CDC_TRAFFIC),
753 host->ioaddr + CORE_DDR_200_CFG);
754out:
755 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
756 __func__, ret);
757 return ret;
758}
759
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700760static int sdhci_msm_cm_dll_sdc4_calibration(struct sdhci_host *host)
761{
Ritesh Harjani764065e2015-05-13 14:14:45 +0530762 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
763 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700764 u32 dll_status;
765 int ret = 0;
766
767 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
768
769 /*
770 * Currently the CORE_DDR_CONFIG register defaults to desired
771 * configuration on reset. Currently reprogramming the power on
772 * reset (POR) value in case it might have been modified by
773 * bootloaders. In the future, if this changes, then the desired
774 * values will need to be programmed appropriately.
775 */
776 writel_relaxed(DDR_CONFIG_POR_VAL, host->ioaddr + CORE_DDR_CONFIG);
777
778 /* Write 1 to DDR_CAL_EN field in CORE_DLL_CONFIG_2 */
779 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
780 | CORE_DDR_CAL_EN),
781 host->ioaddr + CORE_DLL_CONFIG_2);
782
783 /* Poll on DDR_DLL_LOCK bit in CORE_DLL_STATUS to be set */
784 ret = readl_poll_timeout(host->ioaddr + CORE_DLL_STATUS,
785 dll_status, (dll_status & CORE_DDR_DLL_LOCK), 10, 1000);
786
787 if (ret == -ETIMEDOUT) {
788 pr_err("%s: %s: CM_DLL_SDC4 Calibration was not completed\n",
789 mmc_hostname(host->mmc), __func__);
790 goto out;
791 }
792
Ritesh Harjani764065e2015-05-13 14:14:45 +0530793 /*
794 * set CORE_PWRSAVE_DLL bit in CORE_VENDOR_SPEC3.
795 * when MCLK is gated OFF, it is not gated for less than 0.5us
796 * and MCLK must be switched on for at-least 1us before DATA
797 * starts coming. Controllers with 14lpp tech DLL cannot
798 * guarantee above requirement. So PWRSAVE_DLL should not be
799 * turned on for host controllers using this DLL.
800 */
801 if (!msm_host->use_14lpp_dll)
802 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC3)
803 | CORE_PWRSAVE_DLL),
804 host->ioaddr + CORE_VENDOR_SPEC3);
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700805 mb();
806out:
807 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
808 __func__, ret);
809 return ret;
810}
811
812static int sdhci_msm_hs400_dll_calibration(struct sdhci_host *host)
813{
814 int ret = 0;
815 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
816 struct sdhci_msm_host *msm_host = pltfm_host->priv;
817
818 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
819
820 /*
821 * Retuning in HS400 (DDR mode) will fail, just reset the
822 * tuning block and restore the saved tuning phase.
823 */
824 ret = msm_init_cm_dll(host);
825 if (ret)
826 goto out;
827
828 /* Set the selected phase in delay line hw block */
829 ret = msm_config_cm_dll_phase(host, msm_host->saved_tuning_phase);
830 if (ret)
831 goto out;
832
Krishna Konda0e8efba2014-06-23 14:50:38 -0700833 /* Write 1 to CMD_DAT_TRACK_SEL field in DLL_CONFIG */
834 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
835 | CORE_CMD_DAT_TRACK_SEL),
836 host->ioaddr + CORE_DLL_CONFIG);
837
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700838 if (msm_host->use_cdclp533)
839 /* Calibrate CDCLP533 DLL HW */
840 ret = sdhci_msm_cdclp533_calibration(host);
841 else
842 /* Calibrate CM_DLL_SDC4 HW */
843 ret = sdhci_msm_cm_dll_sdc4_calibration(host);
844out:
845 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
846 __func__, ret);
847 return ret;
848}
849
Krishna Konda96e6b112013-10-28 15:25:03 -0700850static void sdhci_msm_set_mmc_drv_type(struct sdhci_host *host, u32 opcode,
851 u8 drv_type)
852{
853 struct mmc_command cmd = {0};
854 struct mmc_request mrq = {NULL};
855 struct mmc_host *mmc = host->mmc;
856 u8 val = ((drv_type << 4) | 2);
857
858 cmd.opcode = MMC_SWITCH;
859 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
860 (EXT_CSD_HS_TIMING << 16) |
861 (val << 8) |
862 EXT_CSD_CMD_SET_NORMAL;
863 cmd.flags = MMC_CMD_AC | MMC_RSP_R1B;
864 /* 1 sec */
865 cmd.busy_timeout = 1000 * 1000;
866
867 memset(cmd.resp, 0, sizeof(cmd.resp));
868 cmd.retries = 3;
869
870 mrq.cmd = &cmd;
871 cmd.data = NULL;
872
873 mmc_wait_for_req(mmc, &mrq);
874 pr_debug("%s: %s: set card drive type to %d\n",
875 mmc_hostname(mmc), __func__,
876 drv_type);
877}
878
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700879int sdhci_msm_execute_tuning(struct sdhci_host *host, u32 opcode)
880{
881 unsigned long flags;
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530882 int tuning_seq_cnt = 3;
Krishna Konda96e6b112013-10-28 15:25:03 -0700883 u8 phase, *data_buf, tuned_phases[NUM_TUNING_PHASES], tuned_phase_cnt;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700884 const u32 *tuning_block_pattern = tuning_block_64;
885 int size = sizeof(tuning_block_64); /* Tuning pattern size in bytes */
886 int rc;
887 struct mmc_host *mmc = host->mmc;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530888 struct mmc_ios ios = host->mmc->ios;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700889 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
890 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Krishna Konda96e6b112013-10-28 15:25:03 -0700891 u8 drv_type = 0;
892 bool drv_type_changed = false;
893 struct mmc_card *card = host->mmc->card;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530894
895 /*
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700896 * Tuning is required for SDR104, HS200 and HS400 cards and
897 * if clock frequency is greater than 100MHz in these modes.
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530898 */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700899 if (host->clock <= CORE_FREQ_100MHZ ||
900 !((ios.timing == MMC_TIMING_MMC_HS400) ||
901 (ios.timing == MMC_TIMING_MMC_HS200) ||
902 (ios.timing == MMC_TIMING_UHS_SDR104)))
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530903 return 0;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700904
905 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700906
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700907 /* CDC/SDC4 DLL HW calibration is only required for HS400 mode*/
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700908 if (msm_host->tuning_done && !msm_host->calibration_done &&
909 (mmc->ios.timing == MMC_TIMING_MMC_HS400)) {
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700910 rc = sdhci_msm_hs400_dll_calibration(host);
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700911 spin_lock_irqsave(&host->lock, flags);
912 if (!rc)
913 msm_host->calibration_done = true;
914 spin_unlock_irqrestore(&host->lock, flags);
915 goto out;
916 }
917
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700918 spin_lock_irqsave(&host->lock, flags);
919
920 if ((opcode == MMC_SEND_TUNING_BLOCK_HS200) &&
921 (mmc->ios.bus_width == MMC_BUS_WIDTH_8)) {
922 tuning_block_pattern = tuning_block_128;
923 size = sizeof(tuning_block_128);
924 }
925 spin_unlock_irqrestore(&host->lock, flags);
926
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700927 data_buf = kmalloc(size, GFP_KERNEL);
928 if (!data_buf) {
929 rc = -ENOMEM;
930 goto out;
931 }
932
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530933retry:
Krishna Konda96e6b112013-10-28 15:25:03 -0700934 tuned_phase_cnt = 0;
935
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530936 /* first of all reset the tuning block */
937 rc = msm_init_cm_dll(host);
938 if (rc)
939 goto kfree;
940
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700941 phase = 0;
942 do {
943 struct mmc_command cmd = {0};
944 struct mmc_data data = {0};
945 struct mmc_request mrq = {
946 .cmd = &cmd,
947 .data = &data
948 };
949 struct scatterlist sg;
950
951 /* set the phase in delay line hw block */
952 rc = msm_config_cm_dll_phase(host, phase);
953 if (rc)
954 goto kfree;
955
956 cmd.opcode = opcode;
957 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
958
959 data.blksz = size;
960 data.blocks = 1;
961 data.flags = MMC_DATA_READ;
962 data.timeout_ns = 1000 * 1000 * 1000; /* 1 sec */
963
964 data.sg = &sg;
965 data.sg_len = 1;
966 sg_init_one(&sg, data_buf, size);
967 memset(data_buf, 0, size);
968 mmc_wait_for_req(mmc, &mrq);
969
970 if (!cmd.error && !data.error &&
971 !memcmp(data_buf, tuning_block_pattern, size)) {
972 /* tuning is successful at this tuning point */
973 tuned_phases[tuned_phase_cnt++] = phase;
Krishna Konda96e6b112013-10-28 15:25:03 -0700974 pr_debug("%s: %s: found *** good *** phase = %d\n",
975 mmc_hostname(mmc), __func__, phase);
976 } else {
977 pr_debug("%s: %s: found ## bad ## phase = %d\n",
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700978 mmc_hostname(mmc), __func__, phase);
979 }
980 } while (++phase < 16);
981
Sahitya Tummaladfdb4af2014-04-01 14:29:13 +0530982 if ((tuned_phase_cnt == NUM_TUNING_PHASES) &&
983 card && mmc_card_mmc(card)) {
Krishna Konda96e6b112013-10-28 15:25:03 -0700984 /*
985 * If all phases pass then its a problem. So change the card's
986 * drive type to a different value, if supported and repeat
987 * tuning until at least one phase fails. Then set the original
988 * drive type back.
989 *
990 * If all the phases still pass after trying all possible
991 * drive types, then one of those 16 phases will be picked.
992 * This is no different from what was going on before the
993 * modification to change drive type and retune.
994 */
995 pr_debug("%s: tuned phases count: %d\n", mmc_hostname(mmc),
996 tuned_phase_cnt);
997
998 /* set drive type to other value . default setting is 0x0 */
999 while (++drv_type <= MAX_DRV_TYPES_SUPPORTED_HS200) {
1000 if (card->ext_csd.raw_driver_strength &
1001 (1 << drv_type)) {
1002 sdhci_msm_set_mmc_drv_type(host, opcode,
1003 drv_type);
1004 if (!drv_type_changed)
1005 drv_type_changed = true;
1006 goto retry;
1007 }
1008 }
1009 }
1010
1011 /* reset drive type to default (50 ohm) if changed */
1012 if (drv_type_changed)
1013 sdhci_msm_set_mmc_drv_type(host, opcode, 0);
1014
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001015 if (tuned_phase_cnt) {
1016 rc = msm_find_most_appropriate_phase(host, tuned_phases,
1017 tuned_phase_cnt);
1018 if (rc < 0)
1019 goto kfree;
1020 else
1021 phase = (u8)rc;
1022
1023 /*
1024 * Finally set the selected phase in delay
1025 * line hw block.
1026 */
1027 rc = msm_config_cm_dll_phase(host, phase);
1028 if (rc)
1029 goto kfree;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07001030 msm_host->saved_tuning_phase = phase;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001031 pr_debug("%s: %s: finally setting the tuning phase to %d\n",
1032 mmc_hostname(mmc), __func__, phase);
1033 } else {
Sahitya Tummala9fe16532013-06-13 10:36:57 +05301034 if (--tuning_seq_cnt)
1035 goto retry;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001036 /* tuning failed */
1037 pr_err("%s: %s: no tuning point found\n",
1038 mmc_hostname(mmc), __func__);
Sahitya Tummala9fe16532013-06-13 10:36:57 +05301039 rc = -EIO;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001040 }
1041
1042kfree:
1043 kfree(data_buf);
1044out:
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07001045 spin_lock_irqsave(&host->lock, flags);
1046 if (!rc)
1047 msm_host->tuning_done = true;
1048 spin_unlock_irqrestore(&host->lock, flags);
1049 pr_debug("%s: Exit %s, err(%d)\n", mmc_hostname(mmc), __func__, rc);
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001050 return rc;
1051}
1052
Asutosh Das0ef24812012-12-18 16:14:02 +05301053static int sdhci_msm_setup_gpio(struct sdhci_msm_pltfm_data *pdata, bool enable)
1054{
1055 struct sdhci_msm_gpio_data *curr;
1056 int i, ret = 0;
1057
1058 curr = pdata->pin_data->gpio_data;
1059 for (i = 0; i < curr->size; i++) {
1060 if (!gpio_is_valid(curr->gpio[i].no)) {
1061 ret = -EINVAL;
1062 pr_err("%s: Invalid gpio = %d\n", __func__,
1063 curr->gpio[i].no);
1064 goto free_gpios;
1065 }
1066 if (enable) {
1067 ret = gpio_request(curr->gpio[i].no,
1068 curr->gpio[i].name);
1069 if (ret) {
1070 pr_err("%s: gpio_request(%d, %s) failed %d\n",
1071 __func__, curr->gpio[i].no,
1072 curr->gpio[i].name, ret);
1073 goto free_gpios;
1074 }
1075 curr->gpio[i].is_enabled = true;
1076 } else {
1077 gpio_free(curr->gpio[i].no);
1078 curr->gpio[i].is_enabled = false;
1079 }
1080 }
1081 return ret;
1082
1083free_gpios:
1084 for (i--; i >= 0; i--) {
1085 gpio_free(curr->gpio[i].no);
1086 curr->gpio[i].is_enabled = false;
1087 }
1088 return ret;
1089}
1090
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301091static int sdhci_msm_setup_pinctrl(struct sdhci_msm_pltfm_data *pdata,
1092 bool enable)
1093{
1094 int ret = 0;
1095
1096 if (enable)
1097 ret = pinctrl_select_state(pdata->pctrl_data->pctrl,
1098 pdata->pctrl_data->pins_active);
1099 else
1100 ret = pinctrl_select_state(pdata->pctrl_data->pctrl,
1101 pdata->pctrl_data->pins_sleep);
1102
1103 if (ret < 0)
1104 pr_err("%s state for pinctrl failed with %d\n",
1105 enable ? "Enabling" : "Disabling", ret);
1106
1107 return ret;
1108}
1109
Asutosh Das0ef24812012-12-18 16:14:02 +05301110static int sdhci_msm_setup_pins(struct sdhci_msm_pltfm_data *pdata, bool enable)
1111{
1112 int ret = 0;
1113
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301114 if (pdata->pin_cfg_sts == enable) {
Asutosh Das0ef24812012-12-18 16:14:02 +05301115 return 0;
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301116 } else if (pdata->pctrl_data) {
1117 ret = sdhci_msm_setup_pinctrl(pdata, enable);
1118 goto out;
1119 } else if (!pdata->pin_data) {
1120 return 0;
1121 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301122
Sahitya Tummala1cd7e072014-02-14 13:19:01 +05301123 if (pdata->pin_data->is_gpio)
1124 ret = sdhci_msm_setup_gpio(pdata, enable);
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301125out:
Asutosh Das0ef24812012-12-18 16:14:02 +05301126 if (!ret)
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301127 pdata->pin_cfg_sts = enable;
Asutosh Das0ef24812012-12-18 16:14:02 +05301128
1129 return ret;
1130}
1131
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301132static int sdhci_msm_dt_get_array(struct device *dev, const char *prop_name,
1133 u32 **out, int *len, u32 size)
1134{
1135 int ret = 0;
1136 struct device_node *np = dev->of_node;
1137 size_t sz;
1138 u32 *arr = NULL;
1139
1140 if (!of_get_property(np, prop_name, len)) {
1141 ret = -EINVAL;
1142 goto out;
1143 }
1144 sz = *len = *len / sizeof(*arr);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07001145 if (sz <= 0 || (size > 0 && (sz > size))) {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301146 dev_err(dev, "%s invalid size\n", prop_name);
1147 ret = -EINVAL;
1148 goto out;
1149 }
1150
1151 arr = devm_kzalloc(dev, sz * sizeof(*arr), GFP_KERNEL);
1152 if (!arr) {
1153 dev_err(dev, "%s failed allocating memory\n", prop_name);
1154 ret = -ENOMEM;
1155 goto out;
1156 }
1157
1158 ret = of_property_read_u32_array(np, prop_name, arr, sz);
1159 if (ret < 0) {
1160 dev_err(dev, "%s failed reading array %d\n", prop_name, ret);
1161 goto out;
1162 }
1163 *out = arr;
1164out:
1165 if (ret)
1166 *len = 0;
1167 return ret;
1168}
1169
Asutosh Das0ef24812012-12-18 16:14:02 +05301170#define MAX_PROP_SIZE 32
1171static int sdhci_msm_dt_parse_vreg_info(struct device *dev,
1172 struct sdhci_msm_reg_data **vreg_data, const char *vreg_name)
1173{
1174 int len, ret = 0;
1175 const __be32 *prop;
1176 char prop_name[MAX_PROP_SIZE];
1177 struct sdhci_msm_reg_data *vreg;
1178 struct device_node *np = dev->of_node;
1179
1180 snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", vreg_name);
1181 if (!of_parse_phandle(np, prop_name, 0)) {
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301182 dev_info(dev, "No vreg data found for %s\n", vreg_name);
Asutosh Das0ef24812012-12-18 16:14:02 +05301183 return ret;
1184 }
1185
1186 vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
1187 if (!vreg) {
1188 dev_err(dev, "No memory for vreg: %s\n", vreg_name);
1189 ret = -ENOMEM;
1190 return ret;
1191 }
1192
1193 vreg->name = vreg_name;
1194
1195 snprintf(prop_name, MAX_PROP_SIZE,
1196 "qcom,%s-always-on", vreg_name);
1197 if (of_get_property(np, prop_name, NULL))
1198 vreg->is_always_on = true;
1199
1200 snprintf(prop_name, MAX_PROP_SIZE,
1201 "qcom,%s-lpm-sup", vreg_name);
1202 if (of_get_property(np, prop_name, NULL))
1203 vreg->lpm_sup = true;
1204
1205 snprintf(prop_name, MAX_PROP_SIZE,
1206 "qcom,%s-voltage-level", vreg_name);
1207 prop = of_get_property(np, prop_name, &len);
1208 if (!prop || (len != (2 * sizeof(__be32)))) {
1209 dev_warn(dev, "%s %s property\n",
1210 prop ? "invalid format" : "no", prop_name);
1211 } else {
1212 vreg->low_vol_level = be32_to_cpup(&prop[0]);
1213 vreg->high_vol_level = be32_to_cpup(&prop[1]);
1214 }
1215
1216 snprintf(prop_name, MAX_PROP_SIZE,
1217 "qcom,%s-current-level", vreg_name);
1218 prop = of_get_property(np, prop_name, &len);
1219 if (!prop || (len != (2 * sizeof(__be32)))) {
1220 dev_warn(dev, "%s %s property\n",
1221 prop ? "invalid format" : "no", prop_name);
1222 } else {
1223 vreg->lpm_uA = be32_to_cpup(&prop[0]);
1224 vreg->hpm_uA = be32_to_cpup(&prop[1]);
1225 }
1226
1227 *vreg_data = vreg;
1228 dev_dbg(dev, "%s: %s %s vol=[%d %d]uV, curr=[%d %d]uA\n",
1229 vreg->name, vreg->is_always_on ? "always_on," : "",
1230 vreg->lpm_sup ? "lpm_sup," : "", vreg->low_vol_level,
1231 vreg->high_vol_level, vreg->lpm_uA, vreg->hpm_uA);
1232
1233 return ret;
1234}
1235
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301236static int sdhci_msm_parse_pinctrl_info(struct device *dev,
1237 struct sdhci_msm_pltfm_data *pdata)
1238{
1239 struct sdhci_pinctrl_data *pctrl_data;
1240 struct pinctrl *pctrl;
1241 int ret = 0;
1242
1243 /* Try to obtain pinctrl handle */
1244 pctrl = devm_pinctrl_get(dev);
1245 if (IS_ERR(pctrl)) {
1246 ret = PTR_ERR(pctrl);
1247 goto out;
1248 }
1249 pctrl_data = devm_kzalloc(dev, sizeof(*pctrl_data), GFP_KERNEL);
1250 if (!pctrl_data) {
1251 dev_err(dev, "No memory for sdhci_pinctrl_data\n");
1252 ret = -ENOMEM;
1253 goto out;
1254 }
1255 pctrl_data->pctrl = pctrl;
1256 /* Look-up and keep the states handy to be used later */
1257 pctrl_data->pins_active = pinctrl_lookup_state(
1258 pctrl_data->pctrl, "active");
1259 if (IS_ERR(pctrl_data->pins_active)) {
1260 ret = PTR_ERR(pctrl_data->pins_active);
1261 dev_err(dev, "Could not get active pinstates, err:%d\n", ret);
1262 goto out;
1263 }
1264 pctrl_data->pins_sleep = pinctrl_lookup_state(
1265 pctrl_data->pctrl, "sleep");
1266 if (IS_ERR(pctrl_data->pins_sleep)) {
1267 ret = PTR_ERR(pctrl_data->pins_sleep);
1268 dev_err(dev, "Could not get sleep pinstates, err:%d\n", ret);
1269 goto out;
1270 }
1271 pdata->pctrl_data = pctrl_data;
1272out:
1273 return ret;
1274}
1275
Asutosh Das0ef24812012-12-18 16:14:02 +05301276#define GPIO_NAME_MAX_LEN 32
1277static int sdhci_msm_dt_parse_gpio_info(struct device *dev,
1278 struct sdhci_msm_pltfm_data *pdata)
1279{
1280 int ret = 0, cnt, i;
1281 struct sdhci_msm_pin_data *pin_data;
1282 struct device_node *np = dev->of_node;
1283
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301284 ret = sdhci_msm_parse_pinctrl_info(dev, pdata);
1285 if (!ret) {
1286 goto out;
1287 } else if (ret == -EPROBE_DEFER) {
1288 dev_err(dev, "Pinctrl framework not registered, err:%d\n", ret);
1289 goto out;
1290 } else {
1291 dev_err(dev, "Parsing Pinctrl failed with %d, falling back on GPIO lib\n",
1292 ret);
Sahitya Tummala1cd7e072014-02-14 13:19:01 +05301293 ret = 0;
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301294 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301295 pin_data = devm_kzalloc(dev, sizeof(*pin_data), GFP_KERNEL);
1296 if (!pin_data) {
1297 dev_err(dev, "No memory for pin_data\n");
1298 ret = -ENOMEM;
1299 goto out;
1300 }
1301
1302 cnt = of_gpio_count(np);
1303 if (cnt > 0) {
1304 pin_data->gpio_data = devm_kzalloc(dev,
1305 sizeof(struct sdhci_msm_gpio_data), GFP_KERNEL);
1306 if (!pin_data->gpio_data) {
1307 dev_err(dev, "No memory for gpio_data\n");
1308 ret = -ENOMEM;
1309 goto out;
1310 }
1311 pin_data->gpio_data->size = cnt;
1312 pin_data->gpio_data->gpio = devm_kzalloc(dev, cnt *
1313 sizeof(struct sdhci_msm_gpio), GFP_KERNEL);
1314
1315 if (!pin_data->gpio_data->gpio) {
1316 dev_err(dev, "No memory for gpio\n");
1317 ret = -ENOMEM;
1318 goto out;
1319 }
1320
1321 for (i = 0; i < cnt; i++) {
1322 const char *name = NULL;
1323 char result[GPIO_NAME_MAX_LEN];
1324 pin_data->gpio_data->gpio[i].no = of_get_gpio(np, i);
1325 of_property_read_string_index(np,
1326 "qcom,gpio-names", i, &name);
1327
1328 snprintf(result, GPIO_NAME_MAX_LEN, "%s-%s",
1329 dev_name(dev), name ? name : "?");
1330 pin_data->gpio_data->gpio[i].name = result;
1331 dev_dbg(dev, "%s: gpio[%s] = %d\n", __func__,
1332 pin_data->gpio_data->gpio[i].name,
1333 pin_data->gpio_data->gpio[i].no);
Asutosh Das0ef24812012-12-18 16:14:02 +05301334 }
1335 }
Sahitya Tummala1cd7e072014-02-14 13:19:01 +05301336 pdata->pin_data = pin_data;
Asutosh Das0ef24812012-12-18 16:14:02 +05301337out:
1338 if (ret)
1339 dev_err(dev, "%s failed with err %d\n", __func__, ret);
1340 return ret;
1341}
1342
Maya Erez994cf2f2014-10-21 20:22:04 +03001343#ifdef CONFIG_SMP
1344static void sdhci_msm_populate_affinity_type(struct sdhci_msm_pltfm_data *pdata,
1345 struct device_node *np)
1346{
1347 const char *cpu_affinity = NULL;
1348
1349 pdata->cpu_affinity_type = PM_QOS_REQ_AFFINE_IRQ;
1350 if (!of_property_read_string(np, "qcom,cpu-affinity",
1351 &cpu_affinity)) {
1352 if (!strcmp(cpu_affinity, "all_cores"))
1353 pdata->cpu_affinity_type = PM_QOS_REQ_ALL_CORES;
1354 else if (!strcmp(cpu_affinity, "affine_cores"))
1355 pdata->cpu_affinity_type = PM_QOS_REQ_AFFINE_CORES;
1356 else if (!strcmp(cpu_affinity, "affine_irq"))
1357 pdata->cpu_affinity_type = PM_QOS_REQ_AFFINE_IRQ;
1358 }
1359}
1360#else
1361static void sdhci_msm_populate_affinity_type(struct sdhci_msm_pltfm_data *pdata,
1362 struct device_node *np)
1363{
1364}
1365#endif
1366
Asutosh Das0ef24812012-12-18 16:14:02 +05301367/* Parse platform data */
Dov Levenglickc9033ab2015-03-10 16:00:56 +02001368static
1369struct sdhci_msm_pltfm_data *sdhci_msm_populate_pdata(struct device *dev,
1370 struct sdhci_msm_host *msm_host)
Asutosh Das0ef24812012-12-18 16:14:02 +05301371{
1372 struct sdhci_msm_pltfm_data *pdata = NULL;
1373 struct device_node *np = dev->of_node;
1374 u32 bus_width = 0;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05301375 u32 cpu_dma_latency;
Asutosh Das0ef24812012-12-18 16:14:02 +05301376 int len, i;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301377 int clk_table_len;
1378 u32 *clk_table = NULL;
Sujit Reddy Thumma1958d3d2013-06-03 09:54:32 +05301379 enum of_gpio_flags flags = OF_GPIO_ACTIVE_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05301380
1381 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1382 if (!pdata) {
1383 dev_err(dev, "failed to allocate memory for platform data\n");
1384 goto out;
1385 }
1386
Sujit Reddy Thumma1958d3d2013-06-03 09:54:32 +05301387 pdata->status_gpio = of_get_named_gpio_flags(np, "cd-gpios", 0, &flags);
1388 if (gpio_is_valid(pdata->status_gpio) & !(flags & OF_GPIO_ACTIVE_LOW))
1389 pdata->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
Sahitya Tummala581df132013-03-12 14:57:46 +05301390
Asutosh Das0ef24812012-12-18 16:14:02 +05301391 of_property_read_u32(np, "qcom,bus-width", &bus_width);
1392 if (bus_width == 8)
1393 pdata->mmc_bus_width = MMC_CAP_8_BIT_DATA;
1394 else if (bus_width == 4)
1395 pdata->mmc_bus_width = MMC_CAP_4_BIT_DATA;
1396 else {
1397 dev_notice(dev, "invalid bus-width, default to 1-bit mode\n");
1398 pdata->mmc_bus_width = 0;
1399 }
1400
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05301401 if (!of_property_read_u32(np, "qcom,cpu-dma-latency-us",
1402 &cpu_dma_latency))
1403 pdata->cpu_dma_latency_us = cpu_dma_latency;
Asutosh Das598a4d42014-02-05 16:38:23 +05301404 else
1405 pdata->cpu_dma_latency_us = MSM_MMC_DEFAULT_CPU_DMA_LATENCY;
Talel Shenhar7dc5f792015-05-18 12:12:48 +03001406
1407 if (sdhci_msm_dt_get_array(dev, "qcom,devfreq,freq-table",
1408 &msm_host->mmc->clk_scaling.freq_table,
1409 &msm_host->mmc->clk_scaling.freq_table_sz, 0))
1410 pr_debug("%s: no clock scaling frequencies were supplied\n",
1411 dev_name(dev));
1412 else if (!msm_host->mmc->clk_scaling.freq_table ||
1413 !msm_host->mmc->clk_scaling.freq_table_sz)
1414 dev_err(dev, "bad dts clock scaling frequencies\n");
1415
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301416 if (sdhci_msm_dt_get_array(dev, "qcom,clk-rates",
1417 &clk_table, &clk_table_len, 0)) {
1418 dev_err(dev, "failed parsing supported clock rates\n");
1419 goto out;
1420 }
1421 if (!clk_table || !clk_table_len) {
1422 dev_err(dev, "Invalid clock table\n");
1423 goto out;
1424 }
1425 pdata->sup_clk_table = clk_table;
1426 pdata->sup_clk_cnt = clk_table_len;
1427
Asutosh Das0ef24812012-12-18 16:14:02 +05301428 pdata->vreg_data = devm_kzalloc(dev, sizeof(struct
1429 sdhci_msm_slot_reg_data),
1430 GFP_KERNEL);
1431 if (!pdata->vreg_data) {
1432 dev_err(dev, "failed to allocate memory for vreg data\n");
1433 goto out;
1434 }
1435
1436 if (sdhci_msm_dt_parse_vreg_info(dev, &pdata->vreg_data->vdd_data,
1437 "vdd")) {
1438 dev_err(dev, "failed parsing vdd data\n");
1439 goto out;
1440 }
1441 if (sdhci_msm_dt_parse_vreg_info(dev,
1442 &pdata->vreg_data->vdd_io_data,
1443 "vdd-io")) {
1444 dev_err(dev, "failed parsing vdd-io data\n");
1445 goto out;
1446 }
1447
1448 if (sdhci_msm_dt_parse_gpio_info(dev, pdata)) {
1449 dev_err(dev, "failed parsing gpio data\n");
1450 goto out;
1451 }
1452
Asutosh Das0ef24812012-12-18 16:14:02 +05301453 len = of_property_count_strings(np, "qcom,bus-speed-mode");
1454
1455 for (i = 0; i < len; i++) {
1456 const char *name = NULL;
1457
1458 of_property_read_string_index(np,
1459 "qcom,bus-speed-mode", i, &name);
1460 if (!name)
1461 continue;
1462
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07001463 if (!strncmp(name, "HS400_1p8v", sizeof("HS400_1p8v")))
1464 pdata->caps2 |= MMC_CAP2_HS400_1_8V;
1465 else if (!strncmp(name, "HS400_1p2v", sizeof("HS400_1p2v")))
1466 pdata->caps2 |= MMC_CAP2_HS400_1_2V;
1467 else if (!strncmp(name, "HS200_1p8v", sizeof("HS200_1p8v")))
Asutosh Das0ef24812012-12-18 16:14:02 +05301468 pdata->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
1469 else if (!strncmp(name, "HS200_1p2v", sizeof("HS200_1p2v")))
1470 pdata->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
1471 else if (!strncmp(name, "DDR_1p8v", sizeof("DDR_1p8v")))
1472 pdata->caps |= MMC_CAP_1_8V_DDR
1473 | MMC_CAP_UHS_DDR50;
1474 else if (!strncmp(name, "DDR_1p2v", sizeof("DDR_1p2v")))
1475 pdata->caps |= MMC_CAP_1_2V_DDR
1476 | MMC_CAP_UHS_DDR50;
1477 }
1478
1479 if (of_get_property(np, "qcom,nonremovable", NULL))
1480 pdata->nonremovable = true;
1481
Guoping Yuf7c91332014-08-20 16:56:18 +08001482 if (of_get_property(np, "qcom,nonhotplug", NULL))
1483 pdata->nonhotplug = true;
1484
Venkat Gopalakrishnan9a62e042015-03-03 16:14:55 -08001485 pdata->largeaddressbus =
1486 of_property_read_bool(np, "qcom,large-address-bus");
1487
Maya Erez994cf2f2014-10-21 20:22:04 +03001488 sdhci_msm_populate_affinity_type(pdata, np);
1489
Dov Levenglickc9033ab2015-03-10 16:00:56 +02001490 if (of_property_read_bool(np, "qcom,wakeup-on-idle"))
1491 msm_host->mmc->wakeup_on_idle = true;
1492
Asutosh Das0ef24812012-12-18 16:14:02 +05301493 return pdata;
1494out:
1495 return NULL;
1496}
1497
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301498/* Returns required bandwidth in Bytes per Sec */
1499static unsigned int sdhci_get_bw_required(struct sdhci_host *host,
1500 struct mmc_ios *ios)
1501{
Sahitya Tummala2886c922013-04-03 18:03:31 +05301502 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1503 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1504
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301505 unsigned int bw;
1506
Sahitya Tummala2886c922013-04-03 18:03:31 +05301507 bw = msm_host->clk_rate;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301508 /*
1509 * For DDR mode, SDCC controller clock will be at
1510 * the double rate than the actual clock that goes to card.
1511 */
1512 if (ios->bus_width == MMC_BUS_WIDTH_4)
1513 bw /= 2;
1514 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1515 bw /= 8;
1516
1517 return bw;
1518}
1519
1520static int sdhci_msm_bus_get_vote_for_bw(struct sdhci_msm_host *host,
1521 unsigned int bw)
1522{
1523 unsigned int *table = host->pdata->voting_data->bw_vecs;
1524 unsigned int size = host->pdata->voting_data->bw_vecs_size;
1525 int i;
1526
1527 if (host->msm_bus_vote.is_max_bw_needed && bw)
1528 return host->msm_bus_vote.max_bw_vote;
1529
1530 for (i = 0; i < size; i++) {
1531 if (bw <= table[i])
1532 break;
1533 }
1534
1535 if (i && (i == size))
1536 i--;
1537
1538 return i;
1539}
1540
1541/*
1542 * This function must be called with host lock acquired.
1543 * Caller of this function should also ensure that msm bus client
1544 * handle is not null.
1545 */
1546static inline int sdhci_msm_bus_set_vote(struct sdhci_msm_host *msm_host,
1547 int vote,
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301548 unsigned long *flags)
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301549{
1550 struct sdhci_host *host = platform_get_drvdata(msm_host->pdev);
1551 int rc = 0;
1552
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301553 BUG_ON(!flags);
1554
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301555 if (vote != msm_host->msm_bus_vote.curr_vote) {
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301556 spin_unlock_irqrestore(&host->lock, *flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301557 rc = msm_bus_scale_client_update_request(
1558 msm_host->msm_bus_vote.client_handle, vote);
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301559 spin_lock_irqsave(&host->lock, *flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301560 if (rc) {
1561 pr_err("%s: msm_bus_scale_client_update_request() failed: bus_client_handle=0x%x, vote=%d, err=%d\n",
1562 mmc_hostname(host->mmc),
1563 msm_host->msm_bus_vote.client_handle, vote, rc);
1564 goto out;
1565 }
1566 msm_host->msm_bus_vote.curr_vote = vote;
1567 }
1568out:
1569 return rc;
1570}
1571
1572/*
1573 * Internal work. Work to set 0 bandwidth for msm bus.
1574 */
1575static void sdhci_msm_bus_work(struct work_struct *work)
1576{
1577 struct sdhci_msm_host *msm_host;
1578 struct sdhci_host *host;
1579 unsigned long flags;
1580
1581 msm_host = container_of(work, struct sdhci_msm_host,
1582 msm_bus_vote.vote_work.work);
1583 host = platform_get_drvdata(msm_host->pdev);
1584
1585 if (!msm_host->msm_bus_vote.client_handle)
1586 return;
1587
1588 spin_lock_irqsave(&host->lock, flags);
1589 /* don't vote for 0 bandwidth if any request is in progress */
1590 if (!host->mrq) {
1591 sdhci_msm_bus_set_vote(msm_host,
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301592 msm_host->msm_bus_vote.min_bw_vote, &flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301593 } else
1594 pr_warning("%s: %s: Transfer in progress. skipping bus voting to 0 bandwidth\n",
1595 mmc_hostname(host->mmc), __func__);
1596 spin_unlock_irqrestore(&host->lock, flags);
1597}
1598
1599/*
1600 * This function cancels any scheduled delayed work and sets the bus
1601 * vote based on bw (bandwidth) argument.
1602 */
1603static void sdhci_msm_bus_cancel_work_and_set_vote(struct sdhci_host *host,
1604 unsigned int bw)
1605{
1606 int vote;
1607 unsigned long flags;
1608 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1609 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1610
1611 cancel_delayed_work_sync(&msm_host->msm_bus_vote.vote_work);
1612 spin_lock_irqsave(&host->lock, flags);
1613 vote = sdhci_msm_bus_get_vote_for_bw(msm_host, bw);
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301614 sdhci_msm_bus_set_vote(msm_host, vote, &flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301615 spin_unlock_irqrestore(&host->lock, flags);
1616}
1617
1618#define MSM_MMC_BUS_VOTING_DELAY 200 /* msecs */
1619
1620/* This function queues a work which will set the bandwidth requiement to 0 */
1621static void sdhci_msm_bus_queue_work(struct sdhci_host *host)
1622{
1623 unsigned long flags;
1624 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1625 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1626
1627 spin_lock_irqsave(&host->lock, flags);
1628 if (msm_host->msm_bus_vote.min_bw_vote !=
1629 msm_host->msm_bus_vote.curr_vote)
1630 queue_delayed_work(system_wq,
1631 &msm_host->msm_bus_vote.vote_work,
1632 msecs_to_jiffies(MSM_MMC_BUS_VOTING_DELAY));
1633 spin_unlock_irqrestore(&host->lock, flags);
1634}
1635
1636static int sdhci_msm_bus_register(struct sdhci_msm_host *host,
1637 struct platform_device *pdev)
1638{
1639 int rc = 0;
1640 struct msm_bus_scale_pdata *bus_pdata;
1641
1642 struct sdhci_msm_bus_voting_data *data;
1643 struct device *dev = &pdev->dev;
1644
1645 data = devm_kzalloc(dev,
1646 sizeof(struct sdhci_msm_bus_voting_data), GFP_KERNEL);
1647 if (!data) {
1648 dev_err(&pdev->dev,
1649 "%s: failed to allocate memory\n", __func__);
1650 rc = -ENOMEM;
1651 goto out;
1652 }
1653 data->bus_pdata = msm_bus_cl_get_pdata(pdev);
1654 if (data->bus_pdata) {
1655 rc = sdhci_msm_dt_get_array(dev, "qcom,bus-bw-vectors-bps",
1656 &data->bw_vecs, &data->bw_vecs_size, 0);
1657 if (rc) {
1658 dev_err(&pdev->dev,
1659 "%s: Failed to get bus-bw-vectors-bps\n",
1660 __func__);
1661 goto out;
1662 }
1663 host->pdata->voting_data = data;
1664 }
1665 if (host->pdata->voting_data &&
1666 host->pdata->voting_data->bus_pdata &&
1667 host->pdata->voting_data->bw_vecs &&
1668 host->pdata->voting_data->bw_vecs_size) {
1669
1670 bus_pdata = host->pdata->voting_data->bus_pdata;
1671 host->msm_bus_vote.client_handle =
1672 msm_bus_scale_register_client(bus_pdata);
1673 if (!host->msm_bus_vote.client_handle) {
1674 dev_err(&pdev->dev, "msm_bus_scale_register_client()\n");
1675 rc = -EFAULT;
1676 goto out;
1677 }
1678 /* cache the vote index for minimum and maximum bandwidth */
1679 host->msm_bus_vote.min_bw_vote =
1680 sdhci_msm_bus_get_vote_for_bw(host, 0);
1681 host->msm_bus_vote.max_bw_vote =
1682 sdhci_msm_bus_get_vote_for_bw(host, UINT_MAX);
1683 } else {
1684 devm_kfree(dev, data);
1685 }
1686
1687out:
1688 return rc;
1689}
1690
1691static void sdhci_msm_bus_unregister(struct sdhci_msm_host *host)
1692{
1693 if (host->msm_bus_vote.client_handle)
1694 msm_bus_scale_unregister_client(
1695 host->msm_bus_vote.client_handle);
1696}
1697
1698static void sdhci_msm_bus_voting(struct sdhci_host *host, u32 enable)
1699{
1700 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1701 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1702 struct mmc_ios *ios = &host->mmc->ios;
1703 unsigned int bw;
1704
1705 if (!msm_host->msm_bus_vote.client_handle)
1706 return;
1707
1708 bw = sdhci_get_bw_required(host, ios);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301709 if (enable) {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301710 sdhci_msm_bus_cancel_work_and_set_vote(host, bw);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301711 } else {
1712 /*
1713 * If clock gating is enabled, then remove the vote
1714 * immediately because clocks will be disabled only
1715 * after SDHCI_MSM_MMC_CLK_GATE_DELAY and thus no
1716 * additional delay is required to remove the bus vote.
1717 */
1718#ifdef CONFIG_MMC_CLKGATE
1719 if (host->mmc->clkgate_delay)
1720 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
1721 else
1722#endif
1723 sdhci_msm_bus_queue_work(host);
1724 }
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301725}
1726
Asutosh Das0ef24812012-12-18 16:14:02 +05301727/* Regulator utility functions */
1728static int sdhci_msm_vreg_init_reg(struct device *dev,
1729 struct sdhci_msm_reg_data *vreg)
1730{
1731 int ret = 0;
1732
1733 /* check if regulator is already initialized? */
1734 if (vreg->reg)
1735 goto out;
1736
1737 /* Get the regulator handle */
1738 vreg->reg = devm_regulator_get(dev, vreg->name);
1739 if (IS_ERR(vreg->reg)) {
1740 ret = PTR_ERR(vreg->reg);
1741 pr_err("%s: devm_regulator_get(%s) failed. ret=%d\n",
1742 __func__, vreg->name, ret);
1743 goto out;
1744 }
1745
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301746 if (regulator_count_voltages(vreg->reg) > 0) {
1747 vreg->set_voltage_sup = true;
1748 /* sanity check */
1749 if (!vreg->high_vol_level || !vreg->hpm_uA) {
1750 pr_err("%s: %s invalid constraints specified\n",
1751 __func__, vreg->name);
1752 ret = -EINVAL;
1753 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301754 }
1755
1756out:
1757 return ret;
1758}
1759
1760static void sdhci_msm_vreg_deinit_reg(struct sdhci_msm_reg_data *vreg)
1761{
1762 if (vreg->reg)
1763 devm_regulator_put(vreg->reg);
1764}
1765
1766static int sdhci_msm_vreg_set_optimum_mode(struct sdhci_msm_reg_data
1767 *vreg, int uA_load)
1768{
1769 int ret = 0;
1770
1771 /*
1772 * regulators that do not support regulator_set_voltage also
1773 * do not support regulator_set_optimum_mode
1774 */
1775 if (vreg->set_voltage_sup) {
1776 ret = regulator_set_load(vreg->reg, uA_load);
1777 if (ret < 0)
1778 pr_err("%s: regulator_set_load(reg=%s,uA_load=%d) failed. ret=%d\n",
1779 __func__, vreg->name, uA_load, ret);
1780 else
1781 /*
1782 * regulator_set_load() can return non zero
1783 * value even for success case.
1784 */
1785 ret = 0;
1786 }
1787 return ret;
1788}
1789
1790static int sdhci_msm_vreg_set_voltage(struct sdhci_msm_reg_data *vreg,
1791 int min_uV, int max_uV)
1792{
1793 int ret = 0;
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301794 if (vreg->set_voltage_sup) {
1795 ret = regulator_set_voltage(vreg->reg, min_uV, max_uV);
1796 if (ret) {
1797 pr_err("%s: regulator_set_voltage(%s)failed. min_uV=%d,max_uV=%d,ret=%d\n",
Asutosh Das0ef24812012-12-18 16:14:02 +05301798 __func__, vreg->name, min_uV, max_uV, ret);
1799 }
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301800 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301801
1802 return ret;
1803}
1804
1805static int sdhci_msm_vreg_enable(struct sdhci_msm_reg_data *vreg)
1806{
1807 int ret = 0;
1808
1809 /* Put regulator in HPM (high power mode) */
1810 ret = sdhci_msm_vreg_set_optimum_mode(vreg, vreg->hpm_uA);
1811 if (ret < 0)
1812 return ret;
1813
1814 if (!vreg->is_enabled) {
1815 /* Set voltage level */
1816 ret = sdhci_msm_vreg_set_voltage(vreg, vreg->high_vol_level,
1817 vreg->high_vol_level);
1818 if (ret)
1819 return ret;
1820 }
1821 ret = regulator_enable(vreg->reg);
1822 if (ret) {
1823 pr_err("%s: regulator_enable(%s) failed. ret=%d\n",
1824 __func__, vreg->name, ret);
1825 return ret;
1826 }
1827 vreg->is_enabled = true;
1828 return ret;
1829}
1830
1831static int sdhci_msm_vreg_disable(struct sdhci_msm_reg_data *vreg)
1832{
1833 int ret = 0;
1834
1835 /* Never disable regulator marked as always_on */
1836 if (vreg->is_enabled && !vreg->is_always_on) {
1837 ret = regulator_disable(vreg->reg);
1838 if (ret) {
1839 pr_err("%s: regulator_disable(%s) failed. ret=%d\n",
1840 __func__, vreg->name, ret);
1841 goto out;
1842 }
1843 vreg->is_enabled = false;
1844
1845 ret = sdhci_msm_vreg_set_optimum_mode(vreg, 0);
1846 if (ret < 0)
1847 goto out;
1848
1849 /* Set min. voltage level to 0 */
1850 ret = sdhci_msm_vreg_set_voltage(vreg, 0, vreg->high_vol_level);
1851 if (ret)
1852 goto out;
1853 } else if (vreg->is_enabled && vreg->is_always_on) {
1854 if (vreg->lpm_sup) {
1855 /* Put always_on regulator in LPM (low power mode) */
1856 ret = sdhci_msm_vreg_set_optimum_mode(vreg,
1857 vreg->lpm_uA);
1858 if (ret < 0)
1859 goto out;
1860 }
1861 }
1862out:
1863 return ret;
1864}
1865
1866static int sdhci_msm_setup_vreg(struct sdhci_msm_pltfm_data *pdata,
1867 bool enable, bool is_init)
1868{
1869 int ret = 0, i;
1870 struct sdhci_msm_slot_reg_data *curr_slot;
1871 struct sdhci_msm_reg_data *vreg_table[2];
1872
1873 curr_slot = pdata->vreg_data;
1874 if (!curr_slot) {
1875 pr_debug("%s: vreg info unavailable,assuming the slot is powered by always on domain\n",
1876 __func__);
1877 goto out;
1878 }
1879
1880 vreg_table[0] = curr_slot->vdd_data;
1881 vreg_table[1] = curr_slot->vdd_io_data;
1882
1883 for (i = 0; i < ARRAY_SIZE(vreg_table); i++) {
1884 if (vreg_table[i]) {
1885 if (enable)
1886 ret = sdhci_msm_vreg_enable(vreg_table[i]);
1887 else
1888 ret = sdhci_msm_vreg_disable(vreg_table[i]);
1889 if (ret)
1890 goto out;
1891 }
1892 }
1893out:
1894 return ret;
1895}
1896
1897/*
1898 * Reset vreg by ensuring it is off during probe. A call
1899 * to enable vreg is needed to balance disable vreg
1900 */
1901static int sdhci_msm_vreg_reset(struct sdhci_msm_pltfm_data *pdata)
1902{
1903 int ret;
1904
1905 ret = sdhci_msm_setup_vreg(pdata, 1, true);
1906 if (ret)
1907 return ret;
1908 ret = sdhci_msm_setup_vreg(pdata, 0, true);
1909 return ret;
1910}
1911
1912/* This init function should be called only once for each SDHC slot */
1913static int sdhci_msm_vreg_init(struct device *dev,
1914 struct sdhci_msm_pltfm_data *pdata,
1915 bool is_init)
1916{
1917 int ret = 0;
1918 struct sdhci_msm_slot_reg_data *curr_slot;
1919 struct sdhci_msm_reg_data *curr_vdd_reg, *curr_vdd_io_reg;
1920
1921 curr_slot = pdata->vreg_data;
1922 if (!curr_slot)
1923 goto out;
1924
1925 curr_vdd_reg = curr_slot->vdd_data;
1926 curr_vdd_io_reg = curr_slot->vdd_io_data;
1927
1928 if (!is_init)
1929 /* Deregister all regulators from regulator framework */
1930 goto vdd_io_reg_deinit;
1931
1932 /*
1933 * Get the regulator handle from voltage regulator framework
1934 * and then try to set the voltage level for the regulator
1935 */
1936 if (curr_vdd_reg) {
1937 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_reg);
1938 if (ret)
1939 goto out;
1940 }
1941 if (curr_vdd_io_reg) {
1942 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_io_reg);
1943 if (ret)
1944 goto vdd_reg_deinit;
1945 }
1946 ret = sdhci_msm_vreg_reset(pdata);
1947 if (ret)
1948 dev_err(dev, "vreg reset failed (%d)\n", ret);
1949 goto out;
1950
1951vdd_io_reg_deinit:
1952 if (curr_vdd_io_reg)
1953 sdhci_msm_vreg_deinit_reg(curr_vdd_io_reg);
1954vdd_reg_deinit:
1955 if (curr_vdd_reg)
1956 sdhci_msm_vreg_deinit_reg(curr_vdd_reg);
1957out:
1958 return ret;
1959}
1960
1961
1962static int sdhci_msm_set_vdd_io_vol(struct sdhci_msm_pltfm_data *pdata,
1963 enum vdd_io_level level,
1964 unsigned int voltage_level)
1965{
1966 int ret = 0;
1967 int set_level;
1968 struct sdhci_msm_reg_data *vdd_io_reg;
1969
1970 if (!pdata->vreg_data)
1971 return ret;
1972
1973 vdd_io_reg = pdata->vreg_data->vdd_io_data;
1974 if (vdd_io_reg && vdd_io_reg->is_enabled) {
1975 switch (level) {
1976 case VDD_IO_LOW:
1977 set_level = vdd_io_reg->low_vol_level;
1978 break;
1979 case VDD_IO_HIGH:
1980 set_level = vdd_io_reg->high_vol_level;
1981 break;
1982 case VDD_IO_SET_LEVEL:
1983 set_level = voltage_level;
1984 break;
1985 default:
1986 pr_err("%s: invalid argument level = %d",
1987 __func__, level);
1988 ret = -EINVAL;
1989 return ret;
1990 }
1991 ret = sdhci_msm_vreg_set_voltage(vdd_io_reg, set_level,
1992 set_level);
1993 }
1994 return ret;
1995}
1996
1997static irqreturn_t sdhci_msm_pwr_irq(int irq, void *data)
1998{
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001999 struct sdhci_host *host = (struct sdhci_host *)data;
2000 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2001 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Das0ef24812012-12-18 16:14:02 +05302002 u8 irq_status = 0;
2003 u8 irq_ack = 0;
2004 int ret = 0;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302005 int pwr_state = 0, io_level = 0;
2006 unsigned long flags;
Asutosh Das0ef24812012-12-18 16:14:02 +05302007
2008 irq_status = readb_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
2009 pr_debug("%s: Received IRQ(%d), status=0x%x\n",
2010 mmc_hostname(msm_host->mmc), irq, irq_status);
2011
2012 /* Clear the interrupt */
2013 writeb_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
2014 /*
2015 * SDHC has core_mem and hc_mem device memory and these memory
2016 * addresses do not fall within 1KB region. Hence, any update to
2017 * core_mem address space would require an mb() to ensure this gets
2018 * completed before its next update to registers within hc_mem.
2019 */
2020 mb();
2021
2022 /* Handle BUS ON/OFF*/
2023 if (irq_status & CORE_PWRCTL_BUS_ON) {
2024 ret = sdhci_msm_setup_vreg(msm_host->pdata, true, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302025 if (!ret) {
Asutosh Das0ef24812012-12-18 16:14:02 +05302026 ret = sdhci_msm_setup_pins(msm_host->pdata, true);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302027 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
2028 VDD_IO_HIGH, 0);
2029 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302030 if (ret)
2031 irq_ack |= CORE_PWRCTL_BUS_FAIL;
2032 else
2033 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302034
2035 pwr_state = REQ_BUS_ON;
2036 io_level = REQ_IO_HIGH;
Asutosh Das0ef24812012-12-18 16:14:02 +05302037 }
2038 if (irq_status & CORE_PWRCTL_BUS_OFF) {
2039 ret = sdhci_msm_setup_vreg(msm_host->pdata, false, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302040 if (!ret) {
Asutosh Das0ef24812012-12-18 16:14:02 +05302041 ret = sdhci_msm_setup_pins(msm_host->pdata, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302042 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
2043 VDD_IO_LOW, 0);
2044 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302045 if (ret)
2046 irq_ack |= CORE_PWRCTL_BUS_FAIL;
2047 else
2048 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302049
2050 pwr_state = REQ_BUS_OFF;
2051 io_level = REQ_IO_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05302052 }
2053 /* Handle IO LOW/HIGH */
2054 if (irq_status & CORE_PWRCTL_IO_LOW) {
2055 /* Switch voltage Low */
2056 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_LOW, 0);
2057 if (ret)
2058 irq_ack |= CORE_PWRCTL_IO_FAIL;
2059 else
2060 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302061
2062 io_level = REQ_IO_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05302063 }
2064 if (irq_status & CORE_PWRCTL_IO_HIGH) {
2065 /* Switch voltage High */
2066 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_HIGH, 0);
2067 if (ret)
2068 irq_ack |= CORE_PWRCTL_IO_FAIL;
2069 else
2070 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302071
2072 io_level = REQ_IO_HIGH;
Asutosh Das0ef24812012-12-18 16:14:02 +05302073 }
2074
2075 /* ACK status to the core */
2076 writeb_relaxed(irq_ack, (msm_host->core_mem + CORE_PWRCTL_CTL));
2077 /*
2078 * SDHC has core_mem and hc_mem device memory and these memory
2079 * addresses do not fall within 1KB region. Hence, any update to
2080 * core_mem address space would require an mb() to ensure this gets
2081 * completed before its next update to registers within hc_mem.
2082 */
2083 mb();
2084
Krishna Konda46fd1432014-10-30 21:13:27 -07002085 if ((io_level & REQ_IO_HIGH) && (msm_host->caps_0 & CORE_3_0V_SUPPORT))
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002086 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2087 ~CORE_IO_PAD_PWR_SWITCH),
2088 host->ioaddr + CORE_VENDOR_SPEC);
Krishna Konda46fd1432014-10-30 21:13:27 -07002089 else if ((io_level & REQ_IO_LOW) ||
2090 (msm_host->caps_0 & CORE_1_8V_SUPPORT))
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002091 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
2092 CORE_IO_PAD_PWR_SWITCH),
2093 host->ioaddr + CORE_VENDOR_SPEC);
2094 mb();
2095
Asutosh Das0ef24812012-12-18 16:14:02 +05302096 pr_debug("%s: Handled IRQ(%d), ret=%d, ack=0x%x\n",
2097 mmc_hostname(msm_host->mmc), irq, ret, irq_ack);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302098 spin_lock_irqsave(&host->lock, flags);
2099 if (pwr_state)
2100 msm_host->curr_pwr_state = pwr_state;
2101 if (io_level)
2102 msm_host->curr_io_level = io_level;
2103 complete(&msm_host->pwr_irq_completion);
2104 spin_unlock_irqrestore(&host->lock, flags);
2105
Asutosh Das0ef24812012-12-18 16:14:02 +05302106 return IRQ_HANDLED;
2107}
2108
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302109static ssize_t
Sahitya Tummala5c55b932013-06-20 14:00:18 +05302110show_polling(struct device *dev, struct device_attribute *attr, char *buf)
2111{
2112 struct sdhci_host *host = dev_get_drvdata(dev);
2113 int poll;
2114 unsigned long flags;
2115
2116 spin_lock_irqsave(&host->lock, flags);
2117 poll = !!(host->mmc->caps & MMC_CAP_NEEDS_POLL);
2118 spin_unlock_irqrestore(&host->lock, flags);
2119
2120 return snprintf(buf, PAGE_SIZE, "%d\n", poll);
2121}
2122
2123static ssize_t
2124store_polling(struct device *dev, struct device_attribute *attr,
2125 const char *buf, size_t count)
2126{
2127 struct sdhci_host *host = dev_get_drvdata(dev);
2128 int value;
2129 unsigned long flags;
2130
2131 if (!kstrtou32(buf, 0, &value)) {
2132 spin_lock_irqsave(&host->lock, flags);
2133 if (value) {
2134 host->mmc->caps |= MMC_CAP_NEEDS_POLL;
2135 mmc_detect_change(host->mmc, 0);
2136 } else {
2137 host->mmc->caps &= ~MMC_CAP_NEEDS_POLL;
2138 }
2139 spin_unlock_irqrestore(&host->lock, flags);
2140 }
2141 return count;
2142}
2143
2144static ssize_t
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302145show_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
2146 char *buf)
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
2152 return snprintf(buf, PAGE_SIZE, "%u\n",
2153 msm_host->msm_bus_vote.is_max_bw_needed);
2154}
2155
2156static ssize_t
2157store_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
2158 const char *buf, size_t count)
2159{
2160 struct sdhci_host *host = dev_get_drvdata(dev);
2161 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2162 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2163 uint32_t value;
2164 unsigned long flags;
2165
2166 if (!kstrtou32(buf, 0, &value)) {
2167 spin_lock_irqsave(&host->lock, flags);
2168 msm_host->msm_bus_vote.is_max_bw_needed = !!value;
2169 spin_unlock_irqrestore(&host->lock, flags);
2170 }
2171 return count;
2172}
2173
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302174static void sdhci_msm_check_power_status(struct sdhci_host *host, u32 req_type)
Asutosh Das0ef24812012-12-18 16:14:02 +05302175{
2176 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2177 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302178 unsigned long flags;
2179 bool done = false;
Sahitya Tummala481fbb02013-08-06 15:22:28 +05302180 u32 io_sig_sts;
Asutosh Das0ef24812012-12-18 16:14:02 +05302181
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302182 spin_lock_irqsave(&host->lock, flags);
2183 pr_debug("%s: %s: request %d curr_pwr_state %x curr_io_level %x\n",
2184 mmc_hostname(host->mmc), __func__, req_type,
2185 msm_host->curr_pwr_state, msm_host->curr_io_level);
Sahitya Tummala481fbb02013-08-06 15:22:28 +05302186 io_sig_sts = readl_relaxed(msm_host->core_mem + CORE_GENERICS);
2187 /*
2188 * The IRQ for request type IO High/Low will be generated when -
2189 * 1. SWITCHABLE_SIGNALLING_VOL is enabled in HW.
2190 * 2. If 1 is true and when there is a state change in 1.8V enable
2191 * bit (bit 3) of SDHCI_HOST_CONTROL2 register. The reset state of
2192 * that bit is 0 which indicates 3.3V IO voltage. So, when MMC core
2193 * layer tries to set it to 3.3V before card detection happens, the
2194 * IRQ doesn't get triggered as there is no state change in this bit.
2195 * The driver already handles this case by changing the IO voltage
2196 * level to high as part of controller power up sequence. Hence, check
2197 * for host->pwr to handle a case where IO voltage high request is
2198 * issued even before controller power up.
2199 */
2200 if (req_type & (REQ_IO_HIGH | REQ_IO_LOW)) {
2201 if (!(io_sig_sts & SWITCHABLE_SIGNALLING_VOL) ||
2202 ((req_type & REQ_IO_HIGH) && !host->pwr)) {
2203 pr_debug("%s: do not wait for power IRQ that never comes\n",
2204 mmc_hostname(host->mmc));
2205 spin_unlock_irqrestore(&host->lock, flags);
2206 return;
2207 }
2208 }
2209
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302210 if ((req_type & msm_host->curr_pwr_state) ||
2211 (req_type & msm_host->curr_io_level))
2212 done = true;
2213 spin_unlock_irqrestore(&host->lock, flags);
Asutosh Das0ef24812012-12-18 16:14:02 +05302214
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302215 /*
2216 * This is needed here to hanlde a case where IRQ gets
2217 * triggered even before this function is called so that
2218 * x->done counter of completion gets reset. Otherwise,
2219 * next call to wait_for_completion returns immediately
2220 * without actually waiting for the IRQ to be handled.
2221 */
2222 if (done)
2223 init_completion(&msm_host->pwr_irq_completion);
2224 else
2225 wait_for_completion(&msm_host->pwr_irq_completion);
2226
2227 pr_debug("%s: %s: request %d done\n", mmc_hostname(host->mmc),
2228 __func__, req_type);
Asutosh Das0ef24812012-12-18 16:14:02 +05302229}
2230
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002231static void sdhci_msm_toggle_cdr(struct sdhci_host *host, bool enable)
2232{
2233 if (enable)
2234 writel_relaxed((readl_relaxed(host->ioaddr +
2235 CORE_DLL_CONFIG) | CORE_CDR_EN),
2236 host->ioaddr + CORE_DLL_CONFIG);
2237 else
2238 writel_relaxed((readl_relaxed(host->ioaddr +
2239 CORE_DLL_CONFIG) & ~CORE_CDR_EN),
2240 host->ioaddr + CORE_DLL_CONFIG);
2241}
2242
Asutosh Das648f9d12013-01-10 21:11:04 +05302243static unsigned int sdhci_msm_max_segs(void)
2244{
2245 return SDHCI_MSM_MAX_SEGMENTS;
2246}
2247
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302248static unsigned int sdhci_msm_get_min_clock(struct sdhci_host *host)
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302249{
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302250 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2251 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302252
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302253 return msm_host->pdata->sup_clk_table[0];
2254}
2255
2256static unsigned int sdhci_msm_get_max_clock(struct sdhci_host *host)
2257{
2258 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2259 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2260 int max_clk_index = msm_host->pdata->sup_clk_cnt;
2261
2262 return msm_host->pdata->sup_clk_table[max_clk_index - 1];
2263}
2264
2265static unsigned int sdhci_msm_get_sup_clk_rate(struct sdhci_host *host,
2266 u32 req_clk)
2267{
2268 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2269 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2270 unsigned int sel_clk = -1;
2271 unsigned char cnt;
2272
2273 if (req_clk < sdhci_msm_get_min_clock(host)) {
2274 sel_clk = sdhci_msm_get_min_clock(host);
2275 return sel_clk;
2276 }
2277
2278 for (cnt = 0; cnt < msm_host->pdata->sup_clk_cnt; cnt++) {
2279 if (msm_host->pdata->sup_clk_table[cnt] > req_clk) {
2280 break;
2281 } else if (msm_host->pdata->sup_clk_table[cnt] == req_clk) {
2282 sel_clk = msm_host->pdata->sup_clk_table[cnt];
2283 break;
2284 } else {
2285 sel_clk = msm_host->pdata->sup_clk_table[cnt];
2286 }
2287 }
2288 return sel_clk;
2289}
2290
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302291static int sdhci_msm_enable_controller_clock(struct sdhci_host *host)
2292{
2293 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2294 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2295 int rc = 0;
2296
2297 if (atomic_read(&msm_host->controller_clock))
2298 return 0;
2299
2300 sdhci_msm_bus_voting(host, 1);
2301
2302 if (!IS_ERR(msm_host->pclk)) {
2303 rc = clk_prepare_enable(msm_host->pclk);
2304 if (rc) {
2305 pr_err("%s: %s: failed to enable the pclk with error %d\n",
2306 mmc_hostname(host->mmc), __func__, rc);
2307 goto remove_vote;
2308 }
2309 }
2310
2311 rc = clk_prepare_enable(msm_host->clk);
2312 if (rc) {
2313 pr_err("%s: %s: failed to enable the host-clk with error %d\n",
2314 mmc_hostname(host->mmc), __func__, rc);
2315 goto disable_pclk;
2316 }
2317
2318 atomic_set(&msm_host->controller_clock, 1);
2319 pr_debug("%s: %s: enabled controller clock\n",
2320 mmc_hostname(host->mmc), __func__);
2321 goto out;
2322
2323disable_pclk:
2324 if (!IS_ERR(msm_host->pclk))
2325 clk_disable_unprepare(msm_host->pclk);
2326remove_vote:
2327 if (msm_host->msm_bus_vote.client_handle)
2328 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2329out:
2330 return rc;
2331}
2332
2333
2334
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302335static int sdhci_msm_prepare_clocks(struct sdhci_host *host, bool enable)
2336{
2337 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2338 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2339 int rc = 0;
2340
2341 if (enable && !atomic_read(&msm_host->clks_on)) {
2342 pr_debug("%s: request to enable clocks\n",
2343 mmc_hostname(host->mmc));
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302344
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302345 /*
2346 * The bus-width or the clock rate might have changed
2347 * after controller clocks are enbaled, update bus vote
2348 * in such case.
2349 */
2350 if (atomic_read(&msm_host->controller_clock))
2351 sdhci_msm_bus_voting(host, 1);
2352
2353 rc = sdhci_msm_enable_controller_clock(host);
2354 if (rc)
2355 goto remove_vote;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302356
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302357 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
2358 rc = clk_prepare_enable(msm_host->bus_clk);
2359 if (rc) {
2360 pr_err("%s: %s: failed to enable the bus-clock with error %d\n",
2361 mmc_hostname(host->mmc), __func__, rc);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302362 goto disable_controller_clk;
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302363 }
2364 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002365 if (!IS_ERR(msm_host->ff_clk)) {
2366 rc = clk_prepare_enable(msm_host->ff_clk);
2367 if (rc) {
2368 pr_err("%s: %s: failed to enable the ff_clk with error %d\n",
2369 mmc_hostname(host->mmc), __func__, rc);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302370 goto disable_bus_clk;
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002371 }
2372 }
2373 if (!IS_ERR(msm_host->sleep_clk)) {
2374 rc = clk_prepare_enable(msm_host->sleep_clk);
2375 if (rc) {
2376 pr_err("%s: %s: failed to enable the sleep_clk with error %d\n",
2377 mmc_hostname(host->mmc), __func__, rc);
2378 goto disable_ff_clk;
2379 }
2380 }
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302381 mb();
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302382
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302383 } else if (!enable && atomic_read(&msm_host->clks_on)) {
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302384 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
2385 mb();
Sahitya Tummaladc182982013-08-20 15:32:09 +05302386 /*
2387 * During 1.8V signal switching the clock source must
2388 * still be ON as it requires accessing SDHC
2389 * registers (SDHCi host control2 register bit 3 must
2390 * be written and polled after stopping the SDCLK).
2391 */
2392 if (host->mmc->card_clock_off)
2393 return 0;
2394 pr_debug("%s: request to disable clocks\n",
2395 mmc_hostname(host->mmc));
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002396 if (!IS_ERR_OR_NULL(msm_host->sleep_clk))
2397 clk_disable_unprepare(msm_host->sleep_clk);
2398 if (!IS_ERR_OR_NULL(msm_host->ff_clk))
2399 clk_disable_unprepare(msm_host->ff_clk);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302400 clk_disable_unprepare(msm_host->clk);
2401 if (!IS_ERR(msm_host->pclk))
2402 clk_disable_unprepare(msm_host->pclk);
2403 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2404 clk_disable_unprepare(msm_host->bus_clk);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302405
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302406 atomic_set(&msm_host->controller_clock, 0);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302407 sdhci_msm_bus_voting(host, 0);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302408 }
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302409 atomic_set(&msm_host->clks_on, enable);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302410 goto out;
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002411disable_ff_clk:
2412 if (!IS_ERR_OR_NULL(msm_host->ff_clk))
2413 clk_disable_unprepare(msm_host->ff_clk);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302414disable_bus_clk:
2415 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2416 clk_disable_unprepare(msm_host->bus_clk);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302417disable_controller_clk:
2418 if (!IS_ERR_OR_NULL(msm_host->clk))
2419 clk_disable_unprepare(msm_host->clk);
2420 if (!IS_ERR_OR_NULL(msm_host->pclk))
2421 clk_disable_unprepare(msm_host->pclk);
2422 atomic_set(&msm_host->controller_clock, 0);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302423remove_vote:
2424 if (msm_host->msm_bus_vote.client_handle)
2425 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302426out:
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302427 return rc;
2428}
2429
2430static void sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock)
2431{
2432 int rc;
2433 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2434 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2435 struct mmc_ios curr_ios = host->mmc->ios;
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002436 u32 sup_clock, ddr_clock, dll_lock;
Sahitya Tummala043744a2013-06-24 09:55:33 +05302437 bool curr_pwrsave;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302438
2439 if (!clock) {
Sujit Reddy Thummabf1aecc2014-01-10 10:58:54 +05302440 /*
2441 * disable pwrsave to ensure clock is not auto-gated until
2442 * the rate is >400KHz (initialization complete).
2443 */
2444 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2445 ~CORE_CLK_PWRSAVE, host->ioaddr + CORE_VENDOR_SPEC);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302446 sdhci_msm_prepare_clocks(host, false);
2447 host->clock = clock;
2448 goto out;
2449 }
2450
2451 rc = sdhci_msm_prepare_clocks(host, true);
2452 if (rc)
2453 goto out;
2454
Sahitya Tummala043744a2013-06-24 09:55:33 +05302455 curr_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2456 CORE_CLK_PWRSAVE);
Sahitya Tummalae000b242013-08-29 16:21:08 +05302457 if ((clock > 400000) &&
Venkat Gopalakrishnanc0a367272015-02-24 13:09:09 -08002458 !curr_pwrsave && mmc_host_may_gate_card(host->mmc->card))
Sahitya Tummala043744a2013-06-24 09:55:33 +05302459 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2460 | CORE_CLK_PWRSAVE,
2461 host->ioaddr + CORE_VENDOR_SPEC);
2462 /*
2463 * Disable pwrsave for a newly added card if doesn't allow clock
2464 * gating.
2465 */
Venkat Gopalakrishnanc0a367272015-02-24 13:09:09 -08002466 else if (curr_pwrsave && !mmc_host_may_gate_card(host->mmc->card))
Sahitya Tummala043744a2013-06-24 09:55:33 +05302467 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2468 & ~CORE_CLK_PWRSAVE,
2469 host->ioaddr + CORE_VENDOR_SPEC);
2470
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302471 sup_clock = sdhci_msm_get_sup_clk_rate(host, clock);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002472 if ((curr_ios.timing == MMC_TIMING_UHS_DDR50) ||
Venkat Gopalakrishnan0a29da92015-01-09 12:19:16 -08002473 (curr_ios.timing == MMC_TIMING_MMC_DDR52) ||
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002474 (curr_ios.timing == MMC_TIMING_MMC_HS400)) {
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302475 /*
2476 * The SDHC requires internal clock frequency to be double the
2477 * actual clock that will be set for DDR mode. The controller
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002478 * uses the faster clock(100/400MHz) for some of its parts and
2479 * send the actual required clock (50/200MHz) to the card.
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302480 */
2481 ddr_clock = clock * 2;
2482 sup_clock = sdhci_msm_get_sup_clk_rate(host,
2483 ddr_clock);
2484 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002485
2486 /*
2487 * In general all timing modes are controlled via UHS mode select in
2488 * Host Control2 register. eMMC specific HS200/HS400 doesn't have
2489 * their respective modes defined here, hence we use these values.
2490 *
2491 * HS200 - SDR104 (Since they both are equivalent in functionality)
2492 * HS400 - This involves multiple configurations
2493 * Initially SDR104 - when tuning is required as HS200
2494 * Then when switching to DDR @ 400MHz (HS400) we use
2495 * the vendor specific HC_SELECT_IN to control the mode.
2496 *
2497 * In addition to controlling the modes we also need to select the
2498 * correct input clock for DLL depending on the mode.
2499 *
2500 * HS400 - divided clock (free running MCLK/2)
2501 * All other modes - default (free running MCLK)
2502 */
2503 if (curr_ios.timing == MMC_TIMING_MMC_HS400) {
2504 /* Select the divided clock (free running MCLK/2) */
2505 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2506 & ~CORE_HC_MCLK_SEL_MASK)
2507 | CORE_HC_MCLK_SEL_HS400),
2508 host->ioaddr + CORE_VENDOR_SPEC);
2509 /*
2510 * Select HS400 mode using the HC_SELECT_IN from VENDOR SPEC
2511 * register
2512 */
2513 if (msm_host->tuning_done && !msm_host->calibration_done) {
2514 /*
2515 * Write 0x6 to HC_SELECT_IN and 1 to HC_SELECT_IN_EN
2516 * field in VENDOR_SPEC_FUNC
2517 */
2518 writel_relaxed((readl_relaxed(host->ioaddr + \
2519 CORE_VENDOR_SPEC)
2520 | CORE_HC_SELECT_IN_HS400
2521 | CORE_HC_SELECT_IN_EN),
2522 host->ioaddr + CORE_VENDOR_SPEC);
2523 }
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002524 if (!host->mmc->ios.old_rate && !msm_host->use_cdclp533) {
2525 /*
2526 * Poll on DLL_LOCK and DDR_DLL_LOCK bits in
2527 * CORE_DLL_STATUS to be set. This should get set
2528 * with in 15 us at 200 MHz.
2529 */
2530 rc = readl_poll_timeout(host->ioaddr + CORE_DLL_STATUS,
2531 dll_lock, (dll_lock & (CORE_DLL_LOCK |
2532 CORE_DDR_DLL_LOCK)), 10, 1000);
2533 if (rc == -ETIMEDOUT)
2534 pr_err("%s: Unable to get DLL_LOCK/DDR_DLL_LOCK, dll_status: 0x%08x\n",
2535 mmc_hostname(host->mmc),
2536 dll_lock);
2537 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002538 } else {
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002539 if (!msm_host->use_cdclp533)
2540 /* set CORE_PWRSAVE_DLL bit in CORE_VENDOR_SPEC3 */
2541 writel_relaxed((readl_relaxed(host->ioaddr +
2542 CORE_VENDOR_SPEC3) & ~CORE_PWRSAVE_DLL),
2543 host->ioaddr + CORE_VENDOR_SPEC3);
2544
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002545 /* Select the default clock (free running MCLK) */
2546 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2547 & ~CORE_HC_MCLK_SEL_MASK)
2548 | CORE_HC_MCLK_SEL_DFLT),
2549 host->ioaddr + CORE_VENDOR_SPEC);
2550
2551 /*
2552 * Disable HC_SELECT_IN to be able to use the UHS mode select
2553 * configuration from Host Control2 register for all other
2554 * modes.
2555 *
2556 * Write 0 to HC_SELECT_IN and HC_SELECT_IN_EN field
2557 * in VENDOR_SPEC_FUNC
2558 */
2559 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2560 & ~CORE_HC_SELECT_IN_EN
2561 & ~CORE_HC_SELECT_IN_MASK),
2562 host->ioaddr + CORE_VENDOR_SPEC);
2563 }
2564 mb();
2565
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302566 if (sup_clock != msm_host->clk_rate) {
2567 pr_debug("%s: %s: setting clk rate to %u\n",
2568 mmc_hostname(host->mmc), __func__, sup_clock);
2569 rc = clk_set_rate(msm_host->clk, sup_clock);
2570 if (rc) {
2571 pr_err("%s: %s: Failed to set rate %u for host-clk : %d\n",
2572 mmc_hostname(host->mmc), __func__,
2573 sup_clock, rc);
2574 goto out;
2575 }
2576 msm_host->clk_rate = sup_clock;
2577 host->clock = clock;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302578 /*
2579 * Update the bus vote in case of frequency change due to
2580 * clock scaling.
2581 */
2582 sdhci_msm_bus_voting(host, 1);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302583 }
2584out:
2585 sdhci_set_clock(host, clock);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302586}
2587
Sahitya Tummala14613432013-03-21 11:13:25 +05302588static void sdhci_msm_set_uhs_signaling(struct sdhci_host *host,
2589 unsigned int uhs)
2590{
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002591 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2592 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala14613432013-03-21 11:13:25 +05302593 u16 ctrl_2;
2594
2595 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2596 /* Select Bus Speed Mode for host */
2597 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Venkat Gopalakrishnan0a29da92015-01-09 12:19:16 -08002598 if ((uhs == MMC_TIMING_MMC_HS400) ||
2599 (uhs == MMC_TIMING_MMC_HS200) ||
2600 (uhs == MMC_TIMING_UHS_SDR104))
Sahitya Tummala14613432013-03-21 11:13:25 +05302601 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2602 else if (uhs == MMC_TIMING_UHS_SDR12)
2603 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
2604 else if (uhs == MMC_TIMING_UHS_SDR25)
2605 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
2606 else if (uhs == MMC_TIMING_UHS_SDR50)
2607 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
Venkat Gopalakrishnan0a29da92015-01-09 12:19:16 -08002608 else if ((uhs == MMC_TIMING_UHS_DDR50) ||
2609 (uhs == MMC_TIMING_MMC_DDR52))
Sahitya Tummala14613432013-03-21 11:13:25 +05302610 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302611 /*
2612 * When clock frquency is less than 100MHz, the feedback clock must be
2613 * provided and DLL must not be used so that tuning can be skipped. To
2614 * provide feedback clock, the mode selection can be any value less
2615 * than 3'b011 in bits [2:0] of HOST CONTROL2 register.
2616 */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002617 if (host->clock <= CORE_FREQ_100MHZ) {
2618 if ((uhs == MMC_TIMING_MMC_HS400) ||
2619 (uhs == MMC_TIMING_MMC_HS200) ||
2620 (uhs == MMC_TIMING_UHS_SDR104))
2621 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302622
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002623 /*
2624 * Make sure DLL is disabled when not required
2625 *
2626 * Write 1 to DLL_RST bit of DLL_CONFIG register
2627 */
2628 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
2629 | CORE_DLL_RST),
2630 host->ioaddr + CORE_DLL_CONFIG);
2631
2632 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
2633 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
2634 | CORE_DLL_PDN),
2635 host->ioaddr + CORE_DLL_CONFIG);
2636 mb();
2637
2638 /*
2639 * The DLL needs to be restored and CDCLP533 recalibrated
2640 * when the clock frequency is set back to 400MHz.
2641 */
2642 msm_host->calibration_done = false;
2643 }
2644
2645 pr_debug("%s: %s-clock:%u uhs mode:%u ctrl_2:0x%x\n",
2646 mmc_hostname(host->mmc), __func__, host->clock, uhs, ctrl_2);
Sahitya Tummala14613432013-03-21 11:13:25 +05302647 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
2648
2649}
2650
Venkat Gopalakrishnan34811972015-03-04 14:39:01 -08002651#define MAX_TEST_BUS 60
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302652
2653void sdhci_msm_dump_vendor_regs(struct sdhci_host *host)
2654{
2655 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2656 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2657 int tbsel, tbsel2;
2658 int i, index = 0;
2659 u32 test_bus_val = 0;
2660 u32 debug_reg[MAX_TEST_BUS] = {0};
2661
2662 pr_info("----------- VENDOR REGISTER DUMP -----------\n");
2663 pr_info("Data cnt: 0x%08x | Fifo cnt: 0x%08x | Int sts: 0x%08x\n",
2664 readl_relaxed(msm_host->core_mem + CORE_MCI_DATA_CNT),
2665 readl_relaxed(msm_host->core_mem + CORE_MCI_FIFO_CNT),
2666 readl_relaxed(msm_host->core_mem + CORE_MCI_STATUS));
2667 pr_info("DLL cfg: 0x%08x | DLL sts: 0x%08x | SDCC ver: 0x%08x\n",
2668 readl_relaxed(host->ioaddr + CORE_DLL_CONFIG),
2669 readl_relaxed(host->ioaddr + CORE_DLL_STATUS),
2670 readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION));
2671 pr_info("Vndr func: 0x%08x | Vndr adma err : addr0: 0x%08x addr1: 0x%08x\n",
2672 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC),
2673 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_ADMA_ERR_ADDR0),
2674 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_ADMA_ERR_ADDR1));
2675
2676 /*
2677 * tbsel indicates [2:0] bits and tbsel2 indicates [7:4] bits
2678 * of CORE_TESTBUS_CONFIG register.
2679 *
2680 * To select test bus 0 to 7 use tbsel and to select any test bus
2681 * above 7 use (tbsel2 | tbsel) to get the test bus number. For eg,
2682 * to select test bus 14, write 0x1E to CORE_TESTBUS_CONFIG register
2683 * i.e., tbsel2[7:4] = 0001, tbsel[2:0] = 110.
2684 */
Venkat Gopalakrishnan34811972015-03-04 14:39:01 -08002685 for (tbsel2 = 0; tbsel2 < 7; tbsel2++) {
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302686 for (tbsel = 0; tbsel < 8; tbsel++) {
2687 if (index >= MAX_TEST_BUS)
2688 break;
2689 test_bus_val = (tbsel2 << CORE_TESTBUS_SEL2_BIT) |
2690 tbsel | CORE_TESTBUS_ENA;
2691 writel_relaxed(test_bus_val,
2692 msm_host->core_mem + CORE_TESTBUS_CONFIG);
2693 debug_reg[index++] = readl_relaxed(msm_host->core_mem +
2694 CORE_SDCC_DEBUG_REG);
2695 }
2696 }
2697 for (i = 0; i < MAX_TEST_BUS; i = i + 4)
2698 pr_info(" Test bus[%d to %d]: 0x%08x 0x%08x 0x%08x 0x%08x\n",
2699 i, i + 3, debug_reg[i], debug_reg[i+1],
2700 debug_reg[i+2], debug_reg[i+3]);
Venkat Gopalakrishnan06f7f792015-05-29 17:56:59 -07002701}
2702
2703static void sdhci_msm_clear_set_dumpregs(struct sdhci_host *host, bool set)
2704{
2705 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2706 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2707
2708 if (set) {
2709 writel_relaxed(CORE_TESTBUS_ENA,
2710 msm_host->core_mem + CORE_TESTBUS_CONFIG);
2711 } else {
2712 u32 value;
2713
2714 value = readl_relaxed(msm_host->core_mem + CORE_TESTBUS_CONFIG);
2715 value &= ~CORE_TESTBUS_ENA;
2716 writel_relaxed(value, msm_host->core_mem + CORE_TESTBUS_CONFIG);
2717 }
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302718}
2719
Asutosh Das0ef24812012-12-18 16:14:02 +05302720static struct sdhci_ops sdhci_msm_ops = {
Sahitya Tummala14613432013-03-21 11:13:25 +05302721 .set_uhs_signaling = sdhci_msm_set_uhs_signaling,
Asutosh Das0ef24812012-12-18 16:14:02 +05302722 .check_power_status = sdhci_msm_check_power_status,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002723 .platform_execute_tuning = sdhci_msm_execute_tuning,
2724 .toggle_cdr = sdhci_msm_toggle_cdr,
Asutosh Das648f9d12013-01-10 21:11:04 +05302725 .get_max_segments = sdhci_msm_max_segs,
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302726 .set_clock = sdhci_msm_set_clock,
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302727 .get_min_clock = sdhci_msm_get_min_clock,
2728 .get_max_clock = sdhci_msm_get_max_clock,
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302729 .dump_vendor_regs = sdhci_msm_dump_vendor_regs,
Asutosh Dase5e9ca62013-07-30 19:08:36 +05302730 .config_auto_tuning_cmd = sdhci_msm_config_auto_tuning_cmd,
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302731 .enable_controller_clock = sdhci_msm_enable_controller_clock,
Venkat Gopalakrishnanb8cb7072015-01-09 11:04:34 -08002732 .set_bus_width = sdhci_set_bus_width,
Venkat Gopalakrishnan411df072015-01-09 11:09:44 -08002733 .reset = sdhci_reset,
Venkat Gopalakrishnan06f7f792015-05-29 17:56:59 -07002734 .clear_set_dumpregs = sdhci_msm_clear_set_dumpregs,
Asutosh Das0ef24812012-12-18 16:14:02 +05302735};
2736
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302737static void sdhci_set_default_hw_caps(struct sdhci_msm_host *msm_host,
2738 struct sdhci_host *host)
2739{
Krishna Konda46fd1432014-10-30 21:13:27 -07002740 u32 version, caps = 0;
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302741 u16 minor;
2742 u8 major;
2743
2744 version = readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION);
2745 major = (version & CORE_VERSION_MAJOR_MASK) >>
2746 CORE_VERSION_MAJOR_SHIFT;
2747 minor = version & CORE_VERSION_TARGET_MASK;
2748
Krishna Konda46fd1432014-10-30 21:13:27 -07002749 caps = readl_relaxed(host->ioaddr + SDHCI_CAPABILITIES);
2750
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302751 /*
2752 * Starting with SDCC 5 controller (core major version = 1)
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002753 * controller won't advertise 3.0v, 1.8v and 8-bit features
2754 * except for some targets.
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302755 */
2756 if (major >= 1 && minor != 0x11 && minor != 0x12) {
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002757 struct sdhci_msm_reg_data *vdd_io_reg;
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002758 /*
2759 * Enable 1.8V support capability on controllers that
2760 * support dual voltage
2761 */
2762 vdd_io_reg = msm_host->pdata->vreg_data->vdd_io_data;
Krishna Konda46fd1432014-10-30 21:13:27 -07002763 if (vdd_io_reg && (vdd_io_reg->high_vol_level > 2700000))
2764 caps |= CORE_3_0V_SUPPORT;
2765 if (vdd_io_reg && (vdd_io_reg->low_vol_level < 1950000))
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002766 caps |= CORE_1_8V_SUPPORT;
Pratibhasagar Vada47992013-12-09 20:42:32 +05302767 if (msm_host->pdata->mmc_bus_width == MMC_CAP_8_BIT_DATA)
2768 caps |= CORE_8_BIT_SUPPORT;
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302769 }
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002770
2771 /*
2772 * SDCC 5 controller with major version 1, minor version 0x34 and later
2773 * with HS 400 mode support will use CM DLL instead of CDC LP 533 DLL.
2774 */
2775 if ((major == 1) && (minor < 0x34))
2776 msm_host->use_cdclp533 = true;
Gilad Broner2a10ca02014-10-02 17:20:35 +03002777
2778 /*
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -08002779 * SDCC 5 controller with major version 1, minor version 0x42 and later
2780 * will require additional steps when resetting DLL.
2781 */
2782 if ((major == 1) && (minor >= 0x42))
2783 msm_host->use_updated_dll_reset = true;
2784
2785 /*
Talel Shenhar9a25b882015-06-02 13:36:35 +03002786 * SDCC 5 controller with major version 1 and minor version 0x42,
2787 * 0x46 and 0x49 currently uses 14lpp tech DLL whose internal
2788 * gating cannot guarantee MCLK timing requirement i.e.
Ritesh Harjani764065e2015-05-13 14:14:45 +05302789 * when MCLK is gated OFF, it is not gated for less than 0.5us
2790 * and MCLK must be switched on for at-least 1us before DATA
2791 * starts coming.
2792 */
Talel Shenhar9a25b882015-06-02 13:36:35 +03002793 if ((major == 1) && ((minor == 0x42) || (minor == 0x46) ||
2794 (minor == 0x49)))
Ritesh Harjani764065e2015-05-13 14:14:45 +05302795 msm_host->use_14lpp_dll = true;
2796 /*
Gilad Broner2a10ca02014-10-02 17:20:35 +03002797 * Mask 64-bit support for controller with 32-bit address bus so that
2798 * smaller descriptor size will be used and improve memory consumption.
Gilad Broner2a10ca02014-10-02 17:20:35 +03002799 */
Venkat Gopalakrishnan9a62e042015-03-03 16:14:55 -08002800 if (!msm_host->pdata->largeaddressbus)
2801 caps &= ~CORE_SYS_BUS_SUPPORT_64_BIT;
2802
Gilad Broner2a10ca02014-10-02 17:20:35 +03002803 writel_relaxed(caps, host->ioaddr + CORE_VENDOR_SPEC_CAPABILITIES0);
Krishna Konda46fd1432014-10-30 21:13:27 -07002804 /* keep track of the value in SDHCI_CAPABILITIES */
2805 msm_host->caps_0 = caps;
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302806}
2807
Venkat Gopalakrishnand371f142015-05-29 17:49:46 -07002808#ifdef CONFIG_MMC_CQ_HCI
2809static void sdhci_msm_cmdq_init(struct sdhci_host *host,
2810 struct platform_device *pdev)
2811{
2812 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2813 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2814
2815 host->cq_host = cmdq_pltfm_init(pdev);
2816 if (IS_ERR(host->cq_host))
2817 dev_dbg(&pdev->dev, "cmdq-pltfm init: failed: %ld\n",
2818 PTR_ERR(host->cq_host));
2819 else
2820 msm_host->mmc->caps2 |= MMC_CAP2_CMD_QUEUE;
2821}
2822#else
2823static void sdhci_msm_cmdq_init(struct sdhci_host *host,
2824 struct platform_device *pdev)
2825{
2826
2827}
2828#endif
2829
Asutosh Das0ef24812012-12-18 16:14:02 +05302830static int sdhci_msm_probe(struct platform_device *pdev)
2831{
2832 struct sdhci_host *host;
2833 struct sdhci_pltfm_host *pltfm_host;
2834 struct sdhci_msm_host *msm_host;
2835 struct resource *core_memres = NULL;
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02002836 int ret = 0, dead = 0;
Stephen Boyd8dce5c62013-04-24 14:19:46 -07002837 u16 host_version;
Venkat Gopalakrishnan17edb352015-06-24 14:46:49 -07002838 u32 irq_status, irq_ctl;
Asutosh Das0ef24812012-12-18 16:14:02 +05302839
2840 pr_debug("%s: Enter %s\n", dev_name(&pdev->dev), __func__);
2841 msm_host = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_msm_host),
2842 GFP_KERNEL);
2843 if (!msm_host) {
2844 ret = -ENOMEM;
2845 goto out;
2846 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302847
2848 msm_host->sdhci_msm_pdata.ops = &sdhci_msm_ops;
2849 host = sdhci_pltfm_init(pdev, &msm_host->sdhci_msm_pdata, 0);
2850 if (IS_ERR(host)) {
2851 ret = PTR_ERR(host);
2852 goto out;
2853 }
2854
2855 pltfm_host = sdhci_priv(host);
2856 pltfm_host->priv = msm_host;
2857 msm_host->mmc = host->mmc;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302858 msm_host->pdev = pdev;
Asutosh Das0ef24812012-12-18 16:14:02 +05302859
2860 /* Extract platform data */
2861 if (pdev->dev.of_node) {
Venkat Gopalakrishnan270580a2013-03-11 12:17:57 -07002862 ret = of_alias_get_id(pdev->dev.of_node, "sdhc");
2863 if (ret < 0) {
2864 dev_err(&pdev->dev, "Failed to get slot index %d\n",
2865 ret);
2866 goto pltfm_free;
2867 }
2868 if (disable_slots & (1 << (ret - 1))) {
2869 dev_info(&pdev->dev, "%s: Slot %d disabled\n", __func__,
2870 ret);
2871 ret = -ENODEV;
2872 goto pltfm_free;
2873 }
2874
Dov Levenglickc9033ab2015-03-10 16:00:56 +02002875 msm_host->pdata = sdhci_msm_populate_pdata(&pdev->dev,
2876 msm_host);
Asutosh Das0ef24812012-12-18 16:14:02 +05302877 if (!msm_host->pdata) {
2878 dev_err(&pdev->dev, "DT parsing error\n");
2879 goto pltfm_free;
2880 }
2881 } else {
2882 dev_err(&pdev->dev, "No device tree node\n");
2883 goto pltfm_free;
2884 }
2885
2886 /* Setup Clocks */
2887
2888 /* Setup SDCC bus voter clock. */
2889 msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus_clk");
2890 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
2891 /* Vote for max. clk rate for max. performance */
2892 ret = clk_set_rate(msm_host->bus_clk, INT_MAX);
2893 if (ret)
2894 goto pltfm_free;
2895 ret = clk_prepare_enable(msm_host->bus_clk);
2896 if (ret)
2897 goto pltfm_free;
2898 }
2899
2900 /* Setup main peripheral bus clock */
2901 msm_host->pclk = devm_clk_get(&pdev->dev, "iface_clk");
2902 if (!IS_ERR(msm_host->pclk)) {
2903 ret = clk_prepare_enable(msm_host->pclk);
2904 if (ret)
2905 goto bus_clk_disable;
2906 }
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302907 atomic_set(&msm_host->controller_clock, 1);
Asutosh Das0ef24812012-12-18 16:14:02 +05302908
2909 /* Setup SDC MMC clock */
2910 msm_host->clk = devm_clk_get(&pdev->dev, "core_clk");
2911 if (IS_ERR(msm_host->clk)) {
2912 ret = PTR_ERR(msm_host->clk);
2913 goto pclk_disable;
2914 }
2915
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302916 /* Set to the minimum supported clock frequency */
2917 ret = clk_set_rate(msm_host->clk, sdhci_msm_get_min_clock(host));
2918 if (ret) {
2919 dev_err(&pdev->dev, "MClk rate set failed (%d)\n", ret);
Sahitya Tummala020ede0d2013-06-07 13:03:07 +05302920 goto pclk_disable;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302921 }
Sahitya Tummala020ede0d2013-06-07 13:03:07 +05302922 ret = clk_prepare_enable(msm_host->clk);
2923 if (ret)
2924 goto pclk_disable;
2925
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302926 msm_host->clk_rate = sdhci_msm_get_min_clock(host);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302927 atomic_set(&msm_host->clks_on, 1);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302928
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002929 /* Setup CDC calibration fixed feedback clock */
2930 msm_host->ff_clk = devm_clk_get(&pdev->dev, "cal_clk");
2931 if (!IS_ERR(msm_host->ff_clk)) {
2932 ret = clk_prepare_enable(msm_host->ff_clk);
2933 if (ret)
2934 goto clk_disable;
2935 }
2936
2937 /* Setup CDC calibration sleep clock */
2938 msm_host->sleep_clk = devm_clk_get(&pdev->dev, "sleep_clk");
2939 if (!IS_ERR(msm_host->sleep_clk)) {
2940 ret = clk_prepare_enable(msm_host->sleep_clk);
2941 if (ret)
2942 goto ff_clk_disable;
2943 }
2944
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07002945 msm_host->saved_tuning_phase = INVALID_TUNING_PHASE;
2946
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302947 ret = sdhci_msm_bus_register(msm_host, pdev);
2948 if (ret)
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002949 goto sleep_clk_disable;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302950
2951 if (msm_host->msm_bus_vote.client_handle)
2952 INIT_DELAYED_WORK(&msm_host->msm_bus_vote.vote_work,
2953 sdhci_msm_bus_work);
2954 sdhci_msm_bus_voting(host, 1);
2955
Asutosh Das0ef24812012-12-18 16:14:02 +05302956 /* Setup regulators */
2957 ret = sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, true);
2958 if (ret) {
2959 dev_err(&pdev->dev, "Regulator setup failed (%d)\n", ret);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302960 goto bus_unregister;
Asutosh Das0ef24812012-12-18 16:14:02 +05302961 }
2962
2963 /* Reset the core and Enable SDHC mode */
2964 core_memres = platform_get_resource_byname(pdev,
2965 IORESOURCE_MEM, "core_mem");
Asutosh Das890bdee2014-08-08 23:01:42 +05302966 if (!core_memres) {
2967 dev_err(&pdev->dev, "Failed to get iomem resource\n");
2968 goto vreg_deinit;
2969 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302970 msm_host->core_mem = devm_ioremap(&pdev->dev, core_memres->start,
2971 resource_size(core_memres));
2972
2973 if (!msm_host->core_mem) {
2974 dev_err(&pdev->dev, "Failed to remap registers\n");
2975 ret = -ENOMEM;
2976 goto vreg_deinit;
2977 }
2978
Sahitya Tummala66b0fe32013-04-25 11:50:56 +05302979 /*
Venkat Gopalakrishnan17edb352015-06-24 14:46:49 -07002980 * Reset the vendor spec register to power on reset state.
Sahitya Tummala66b0fe32013-04-25 11:50:56 +05302981 */
Venkat Gopalakrishnan17edb352015-06-24 14:46:49 -07002982 writel_relaxed(CORE_VENDOR_SPEC_POR_VAL,
2983 host->ioaddr + CORE_VENDOR_SPEC);
Sahitya Tummala66b0fe32013-04-25 11:50:56 +05302984
Asutosh Das0ef24812012-12-18 16:14:02 +05302985 /* Set HC_MODE_EN bit in HC_MODE register */
2986 writel_relaxed(HC_MODE_EN, (msm_host->core_mem + CORE_HC_MODE));
2987
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002988 /* Set FF_CLK_SW_RST_DIS bit in HC_MODE register */
2989 writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_HC_MODE) |
2990 FF_CLK_SW_RST_DIS, msm_host->core_mem + CORE_HC_MODE);
2991
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302992 sdhci_set_default_hw_caps(msm_host, host);
Krishna Konda46fd1432014-10-30 21:13:27 -07002993
2994 /*
2995 * Set the PAD_PWR_SWTICH_EN bit so that the PAD_PWR_SWITCH bit can
2996 * be used as required later on.
2997 */
2998 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
2999 CORE_IO_PAD_PWR_SWITCH_EN),
3000 host->ioaddr + CORE_VENDOR_SPEC);
Asutosh Das0ef24812012-12-18 16:14:02 +05303001 /*
Subhash Jadavani28137342013-05-14 17:46:43 +05303002 * CORE_SW_RST above may trigger power irq if previous status of PWRCTL
3003 * was either BUS_ON or IO_HIGH_V. So before we enable the power irq
3004 * interrupt in GIC (by registering the interrupt handler), we need to
3005 * ensure that any pending power irq interrupt status is acknowledged
3006 * otherwise power irq interrupt handler would be fired prematurely.
3007 */
3008 irq_status = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
3009 writel_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
3010 irq_ctl = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_CTL);
3011 if (irq_status & (CORE_PWRCTL_BUS_ON | CORE_PWRCTL_BUS_OFF))
3012 irq_ctl |= CORE_PWRCTL_BUS_SUCCESS;
3013 if (irq_status & (CORE_PWRCTL_IO_HIGH | CORE_PWRCTL_IO_LOW))
3014 irq_ctl |= CORE_PWRCTL_IO_SUCCESS;
3015 writel_relaxed(irq_ctl, (msm_host->core_mem + CORE_PWRCTL_CTL));
Krishna Konda46fd1432014-10-30 21:13:27 -07003016
Subhash Jadavani28137342013-05-14 17:46:43 +05303017 /*
3018 * Ensure that above writes are propogated before interrupt enablement
3019 * in GIC.
3020 */
3021 mb();
3022
3023 /*
Asutosh Das0ef24812012-12-18 16:14:02 +05303024 * Following are the deviations from SDHC spec v3.0 -
3025 * 1. Card detection is handled using separate GPIO.
3026 * 2. Bus power control is handled by interacting with PMIC.
3027 */
3028 host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
3029 host->quirks |= SDHCI_QUIRK_SINGLE_POWER_WRITE;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05303030 host->quirks |= SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN;
3031 host->quirks2 |= SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK;
Sahitya Tummala87d43942013-04-12 11:49:11 +05303032 host->quirks2 |= SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD;
Sahitya Tummala314162c2013-04-12 12:11:20 +05303033 host->quirks2 |= SDHCI_QUIRK2_BROKEN_PRESET_VALUE;
Sahitya Tummala7c9780d2013-04-12 11:59:25 +05303034 host->quirks2 |= SDHCI_QUIRK2_USE_RESERVED_MAX_TIMEOUT;
Asutosh Das0ef24812012-12-18 16:14:02 +05303035
Sahitya Tummalaa5733ab52013-06-10 16:32:51 +05303036 if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK)
3037 host->quirks2 |= SDHCI_QUIRK2_DIVIDE_TOUT_BY_4;
3038
Stephen Boyd8dce5c62013-04-24 14:19:46 -07003039 host_version = readw_relaxed((host->ioaddr + SDHCI_HOST_VERSION));
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07003040 dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n",
3041 host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >>
3042 SDHCI_VENDOR_VER_SHIFT));
3043 if (((host_version & SDHCI_VENDOR_VER_MASK) >>
3044 SDHCI_VENDOR_VER_SHIFT) == SDHCI_VER_100) {
3045 /*
3046 * Add 40us delay in interrupt handler when
3047 * operating at initialization frequency(400KHz).
3048 */
3049 host->quirks2 |= SDHCI_QUIRK2_SLOW_INT_CLR;
3050 /*
3051 * Set Software Reset for DAT line in Software
3052 * Reset Register (Bit 2).
3053 */
3054 host->quirks2 |= SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT;
3055 }
3056
Asutosh Das214b9662013-06-13 14:27:42 +05303057 host->quirks2 |= SDHCI_QUIRK2_IGN_DATA_END_BIT_ERROR;
3058
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07003059 /* Setup PWRCTL irq */
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02003060 msm_host->pwr_irq = platform_get_irq_byname(pdev, "pwr_irq");
3061 if (msm_host->pwr_irq < 0) {
Asutosh Das0ef24812012-12-18 16:14:02 +05303062 dev_err(&pdev->dev, "Failed to get pwr_irq by name (%d)\n",
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02003063 msm_host->pwr_irq);
Asutosh Das0ef24812012-12-18 16:14:02 +05303064 goto vreg_deinit;
3065 }
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02003066 ret = devm_request_threaded_irq(&pdev->dev, msm_host->pwr_irq, NULL,
Asutosh Das0ef24812012-12-18 16:14:02 +05303067 sdhci_msm_pwr_irq, IRQF_ONESHOT,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07003068 dev_name(&pdev->dev), host);
Asutosh Das0ef24812012-12-18 16:14:02 +05303069 if (ret) {
3070 dev_err(&pdev->dev, "Request threaded irq(%d) failed (%d)\n",
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02003071 msm_host->pwr_irq, ret);
Asutosh Das0ef24812012-12-18 16:14:02 +05303072 goto vreg_deinit;
3073 }
3074
3075 /* Enable pwr irq interrupts */
3076 writel_relaxed(INT_MASK, (msm_host->core_mem + CORE_PWRCTL_MASK));
3077
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05303078#ifdef CONFIG_MMC_CLKGATE
3079 /* Set clock gating delay to be used when CONFIG_MMC_CLKGATE is set */
3080 msm_host->mmc->clkgate_delay = SDHCI_MSM_MMC_CLK_GATE_DELAY;
3081#endif
3082
Asutosh Das0ef24812012-12-18 16:14:02 +05303083 /* Set host capabilities */
3084 msm_host->mmc->caps |= msm_host->pdata->mmc_bus_width;
3085 msm_host->mmc->caps |= msm_host->pdata->caps;
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003086 msm_host->mmc->caps |= MMC_CAP_AGGRESSIVE_PM;
Asutosh Das0ef24812012-12-18 16:14:02 +05303087 msm_host->mmc->caps2 |= msm_host->pdata->caps2;
Venkat Gopalakrishnan97c16112014-12-16 18:26:25 -08003088 msm_host->mmc->caps2 |= MMC_CAP2_BOOTPART_NOACC;
3089 msm_host->mmc->caps2 |= MMC_CAP2_FULL_PWR_CYCLE;
Subhash Jadavani6d472b22013-05-29 15:52:10 +05303090 msm_host->mmc->caps2 |= MMC_CAP2_ASYNC_SDIO_IRQ_4BIT_MODE;
Venkat Gopalakrishnan97c16112014-12-16 18:26:25 -08003091 msm_host->mmc->caps2 |= MMC_CAP2_HS400_POST_TUNING;
Talel Shenhara834c022015-05-13 14:08:39 +03003092 msm_host->mmc->caps2 |= MMC_CAP2_CLK_SCALE;
Asutosh Dasbea115d2013-06-24 18:20:45 +05303093 msm_host->mmc->pm_caps |= MMC_PM_KEEP_POWER;
Asutosh Das0ef24812012-12-18 16:14:02 +05303094
3095 if (msm_host->pdata->nonremovable)
3096 msm_host->mmc->caps |= MMC_CAP_NONREMOVABLE;
3097
Guoping Yuf7c91332014-08-20 16:56:18 +08003098 if (msm_host->pdata->nonhotplug)
3099 msm_host->mmc->caps2 |= MMC_CAP2_NONHOTPLUG;
3100
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05303101 host->cpu_dma_latency_us = msm_host->pdata->cpu_dma_latency_us;
Maya Erez994cf2f2014-10-21 20:22:04 +03003102 host->pm_qos_req_dma.type = msm_host->pdata->cpu_affinity_type;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05303103
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05303104 init_completion(&msm_host->pwr_irq_completion);
3105
Sahitya Tummala581df132013-03-12 14:57:46 +05303106 if (gpio_is_valid(msm_host->pdata->status_gpio)) {
Sahitya Tummala6ddabb42014-06-05 13:26:55 +05303107 /*
3108 * Set up the card detect GPIO in active configuration before
3109 * configuring it as an IRQ. Otherwise, it can be in some
3110 * weird/inconsistent state resulting in flood of interrupts.
3111 */
3112 sdhci_msm_setup_pins(msm_host->pdata, true);
3113
Sahitya Tummala581df132013-03-12 14:57:46 +05303114 ret = mmc_gpio_request_cd(msm_host->mmc,
3115 msm_host->pdata->status_gpio, 0);
3116 if (ret) {
3117 dev_err(&pdev->dev, "%s: Failed to request card detection IRQ %d\n",
3118 __func__, ret);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05303119 goto vreg_deinit;
Sahitya Tummala581df132013-03-12 14:57:46 +05303120 }
3121 }
3122
Krishna Konda7feab352013-09-17 23:55:40 -07003123 if ((sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT) &&
3124 (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(64)))) {
3125 host->dma_mask = DMA_BIT_MASK(64);
3126 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
3127 } else if (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(32))) {
Sahitya Tummalaeaa21862013-03-20 19:34:59 +05303128 host->dma_mask = DMA_BIT_MASK(32);
3129 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
3130 } else {
3131 dev_err(&pdev->dev, "%s: Failed to set dma mask\n", __func__);
3132 }
3133
Venkat Gopalakrishnand371f142015-05-29 17:49:46 -07003134 sdhci_msm_cmdq_init(host, pdev);
Asutosh Das0ef24812012-12-18 16:14:02 +05303135 ret = sdhci_add_host(host);
3136 if (ret) {
3137 dev_err(&pdev->dev, "Add host failed (%d)\n", ret);
Sahitya Tummala581df132013-03-12 14:57:46 +05303138 goto vreg_deinit;
Asutosh Das0ef24812012-12-18 16:14:02 +05303139 }
3140
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003141 pm_runtime_set_active(&pdev->dev);
3142 pm_runtime_enable(&pdev->dev);
3143 pm_runtime_set_autosuspend_delay(&pdev->dev, MSM_AUTOSUSPEND_DELAY_MS);
3144 pm_runtime_use_autosuspend(&pdev->dev);
3145
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05303146 msm_host->msm_bus_vote.max_bus_bw.show = show_sdhci_max_bus_bw;
3147 msm_host->msm_bus_vote.max_bus_bw.store = store_sdhci_max_bus_bw;
3148 sysfs_attr_init(&msm_host->msm_bus_vote.max_bus_bw.attr);
3149 msm_host->msm_bus_vote.max_bus_bw.attr.name = "max_bus_bw";
3150 msm_host->msm_bus_vote.max_bus_bw.attr.mode = S_IRUGO | S_IWUSR;
3151 ret = device_create_file(&pdev->dev,
3152 &msm_host->msm_bus_vote.max_bus_bw);
3153 if (ret)
3154 goto remove_host;
3155
Sahitya Tummala5c55b932013-06-20 14:00:18 +05303156 if (!gpio_is_valid(msm_host->pdata->status_gpio)) {
3157 msm_host->polling.show = show_polling;
3158 msm_host->polling.store = store_polling;
3159 sysfs_attr_init(&msm_host->polling.attr);
3160 msm_host->polling.attr.name = "polling";
3161 msm_host->polling.attr.mode = S_IRUGO | S_IWUSR;
3162 ret = device_create_file(&pdev->dev, &msm_host->polling);
3163 if (ret)
3164 goto remove_max_bus_bw_file;
3165 }
Asutosh Dase5e9ca62013-07-30 19:08:36 +05303166
3167 msm_host->auto_cmd21_attr.show = show_auto_cmd21;
3168 msm_host->auto_cmd21_attr.store = store_auto_cmd21;
3169 sysfs_attr_init(&msm_host->auto_cmd21_attr.attr);
3170 msm_host->auto_cmd21_attr.attr.name = "enable_auto_cmd21";
3171 msm_host->auto_cmd21_attr.attr.mode = S_IRUGO | S_IWUSR;
3172 ret = device_create_file(&pdev->dev, &msm_host->auto_cmd21_attr);
3173 if (ret) {
3174 pr_err("%s: %s: failed creating auto-cmd21 attr: %d\n",
3175 mmc_hostname(host->mmc), __func__, ret);
3176 device_remove_file(&pdev->dev, &msm_host->auto_cmd21_attr);
3177 }
Asutosh Das0ef24812012-12-18 16:14:02 +05303178 /* Successful initialization */
3179 goto out;
3180
Sahitya Tummala5c55b932013-06-20 14:00:18 +05303181remove_max_bus_bw_file:
3182 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Asutosh Das0ef24812012-12-18 16:14:02 +05303183remove_host:
3184 dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003185 pm_runtime_disable(&pdev->dev);
Asutosh Das0ef24812012-12-18 16:14:02 +05303186 sdhci_remove_host(host, dead);
3187vreg_deinit:
3188 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05303189bus_unregister:
3190 if (msm_host->msm_bus_vote.client_handle)
3191 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
3192 sdhci_msm_bus_unregister(msm_host);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07003193sleep_clk_disable:
3194 if (!IS_ERR(msm_host->sleep_clk))
3195 clk_disable_unprepare(msm_host->sleep_clk);
3196ff_clk_disable:
3197 if (!IS_ERR(msm_host->ff_clk))
3198 clk_disable_unprepare(msm_host->ff_clk);
Asutosh Das0ef24812012-12-18 16:14:02 +05303199clk_disable:
3200 if (!IS_ERR(msm_host->clk))
3201 clk_disable_unprepare(msm_host->clk);
3202pclk_disable:
3203 if (!IS_ERR(msm_host->pclk))
3204 clk_disable_unprepare(msm_host->pclk);
3205bus_clk_disable:
3206 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
3207 clk_disable_unprepare(msm_host->bus_clk);
3208pltfm_free:
3209 sdhci_pltfm_free(pdev);
3210out:
3211 pr_debug("%s: Exit %s\n", dev_name(&pdev->dev), __func__);
3212 return ret;
3213}
3214
3215static int sdhci_msm_remove(struct platform_device *pdev)
3216{
3217 struct sdhci_host *host = platform_get_drvdata(pdev);
3218 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3219 struct sdhci_msm_host *msm_host = pltfm_host->priv;
3220 struct sdhci_msm_pltfm_data *pdata = msm_host->pdata;
3221 int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) ==
3222 0xffffffff);
3223
3224 pr_debug("%s: %s\n", dev_name(&pdev->dev), __func__);
Sahitya Tummala5c55b932013-06-20 14:00:18 +05303225 if (!gpio_is_valid(msm_host->pdata->status_gpio))
3226 device_remove_file(&pdev->dev, &msm_host->polling);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05303227 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003228 pm_runtime_disable(&pdev->dev);
Asutosh Das0ef24812012-12-18 16:14:02 +05303229 sdhci_remove_host(host, dead);
3230 sdhci_pltfm_free(pdev);
Sahitya Tummala581df132013-03-12 14:57:46 +05303231
Asutosh Das0ef24812012-12-18 16:14:02 +05303232 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05303233
Pratibhasagar V9acf2642013-11-21 21:07:21 +05303234 sdhci_msm_setup_pins(pdata, true);
3235 sdhci_msm_setup_pins(pdata, false);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05303236
3237 if (msm_host->msm_bus_vote.client_handle) {
3238 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
3239 sdhci_msm_bus_unregister(msm_host);
3240 }
Asutosh Das0ef24812012-12-18 16:14:02 +05303241 return 0;
3242}
3243
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003244#ifdef CONFIG_PM
3245static int sdhci_msm_runtime_suspend(struct device *dev)
3246{
3247 struct sdhci_host *host = dev_get_drvdata(dev);
3248 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3249 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003250 ktime_t start = ktime_get();
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003251
3252 disable_irq(host->irq);
3253 disable_irq(msm_host->pwr_irq);
3254
3255 /*
3256 * Remove the vote immediately only if clocks are off in which
3257 * case we might have queued work to remove vote but it may not
3258 * be completed before runtime suspend or system suspend.
3259 */
3260 if (!atomic_read(&msm_host->clks_on)) {
3261 if (msm_host->msm_bus_vote.client_handle)
3262 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
3263 }
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003264 trace_sdhci_msm_runtime_suspend(mmc_hostname(host->mmc), 0,
3265 ktime_to_us(ktime_sub(ktime_get(), start)));
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003266
3267 return 0;
3268}
3269
3270static int sdhci_msm_runtime_resume(struct device *dev)
3271{
3272 struct sdhci_host *host = dev_get_drvdata(dev);
3273 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3274 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003275 ktime_t start = ktime_get();
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003276
3277 enable_irq(msm_host->pwr_irq);
3278 enable_irq(host->irq);
3279
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003280 trace_sdhci_msm_runtime_resume(mmc_hostname(host->mmc), 0,
3281 ktime_to_us(ktime_sub(ktime_get(), start)));
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003282 return 0;
3283}
3284
3285static int sdhci_msm_suspend(struct device *dev)
3286{
3287 struct sdhci_host *host = dev_get_drvdata(dev);
3288 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3289 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003290 int ret = 0;
3291 ktime_t start = ktime_get();
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003292
3293 if (gpio_is_valid(msm_host->pdata->status_gpio) &&
3294 (msm_host->mmc->slot.cd_irq >= 0))
3295 disable_irq(msm_host->mmc->slot.cd_irq);
3296
3297 if (pm_runtime_suspended(dev)) {
3298 pr_debug("%s: %s: already runtime suspended\n",
3299 mmc_hostname(host->mmc), __func__);
3300 goto out;
3301 }
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003302 ret = sdhci_msm_runtime_suspend(dev);
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003303out:
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003304 trace_sdhci_msm_suspend(mmc_hostname(host->mmc), ret,
3305 ktime_to_us(ktime_sub(ktime_get(), start)));
3306 return ret;
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003307}
3308
3309static int sdhci_msm_resume(struct device *dev)
3310{
3311 struct sdhci_host *host = dev_get_drvdata(dev);
3312 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3313 struct sdhci_msm_host *msm_host = pltfm_host->priv;
3314 int ret = 0;
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003315 ktime_t start = ktime_get();
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003316
3317 if (gpio_is_valid(msm_host->pdata->status_gpio) &&
3318 (msm_host->mmc->slot.cd_irq >= 0))
3319 enable_irq(msm_host->mmc->slot.cd_irq);
3320
3321 if (pm_runtime_suspended(dev)) {
3322 pr_debug("%s: %s: runtime suspended, defer system resume\n",
3323 mmc_hostname(host->mmc), __func__);
3324 goto out;
3325 }
3326
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003327 ret = sdhci_msm_runtime_resume(dev);
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003328out:
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003329 trace_sdhci_msm_resume(mmc_hostname(host->mmc), ret,
3330 ktime_to_us(ktime_sub(ktime_get(), start)));
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003331 return ret;
3332}
3333
3334static const struct dev_pm_ops sdhci_msm_pmops = {
3335 SET_SYSTEM_SLEEP_PM_OPS(sdhci_msm_suspend, sdhci_msm_resume)
3336 SET_RUNTIME_PM_OPS(sdhci_msm_runtime_suspend, sdhci_msm_runtime_resume,
3337 NULL)
3338};
3339
3340#define SDHCI_MSM_PMOPS (&sdhci_msm_pmops)
3341
3342#else
3343#define SDHCI_MSM_PMOPS NULL
3344#endif
Asutosh Das0ef24812012-12-18 16:14:02 +05303345static const struct of_device_id sdhci_msm_dt_match[] = {
3346 {.compatible = "qcom,sdhci-msm"},
3347};
3348MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match);
3349
3350static struct platform_driver sdhci_msm_driver = {
3351 .probe = sdhci_msm_probe,
3352 .remove = sdhci_msm_remove,
3353 .driver = {
3354 .name = "sdhci_msm",
3355 .owner = THIS_MODULE,
3356 .of_match_table = sdhci_msm_dt_match,
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003357 .pm = SDHCI_MSM_PMOPS,
Asutosh Das0ef24812012-12-18 16:14:02 +05303358 },
3359};
3360
3361module_platform_driver(sdhci_msm_driver);
3362
3363MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Secure Digital Host Controller Interface driver");
3364MODULE_LICENSE("GPL v2");