blob: e8ef0895236d3bba44b016f348dc3883eefbe9b3 [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)
Ritesh Harjaniea709662015-05-27 15:40:24 +0530158#define CORE_CMDIN_RCLK_EN (1 << 1)
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700159#define CORE_START_CDC_TRAFFIC (1 << 6)
Ritesh Harjaniea709662015-05-27 15:40:24 +0530160
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700161#define CORE_VENDOR_SPEC3 0x1B0
162#define CORE_PWRSAVE_DLL (1 << 3)
Ritesh Harjani4a5ffd12015-07-15 13:32:07 +0530163#define CORE_CMDEN_HS400_INPUT_MASK_CNT (1 << 13)
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700164
165#define CORE_DLL_CONFIG_2 0x1B4
166#define CORE_DDR_CAL_EN (1 << 0)
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800167#define CORE_FLL_CYCLE_CNT (1 << 18)
168#define CORE_DLL_CLOCK_DISABLE (1 << 21)
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700169
170#define CORE_DDR_CONFIG 0x1B8
171#define DDR_CONFIG_POR_VAL 0x80040853
172
Sahitya Tummala56874732015-05-21 08:24:03 +0530173#define MSM_MMC_DEFAULT_CPU_DMA_LATENCY 200 /* usecs */
174
Venkat Gopalakrishnan450745e2014-07-24 20:39:34 -0700175/* 512 descriptors */
176#define SDHCI_MSM_MAX_SEGMENTS (1 << 9)
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +0530177#define SDHCI_MSM_MMC_CLK_GATE_DELAY 200 /* msecs */
Asutosh Das648f9d12013-01-10 21:11:04 +0530178
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700179#define CORE_FREQ_100MHZ (100 * 1000 * 1000)
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800180#define TCXO_FREQ 19200000
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700181
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700182#define INVALID_TUNING_PHASE -1
183
Krishna Konda96e6b112013-10-28 15:25:03 -0700184#define NUM_TUNING_PHASES 16
185#define MAX_DRV_TYPES_SUPPORTED_HS200 3
Konstantin Dorfman98377d32015-02-25 10:09:41 +0200186#define MSM_AUTOSUSPEND_DELAY_MS 100
Krishna Konda96e6b112013-10-28 15:25:03 -0700187
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700188static const u32 tuning_block_64[] = {
189 0x00FF0FFF, 0xCCC3CCFF, 0xFFCC3CC3, 0xEFFEFFFE,
190 0xDDFFDFFF, 0xFBFFFBFF, 0xFF7FFFBF, 0xEFBDF777,
191 0xF0FFF0FF, 0x3CCCFC0F, 0xCFCC33CC, 0xEEFFEFFF,
192 0xFDFFFDFF, 0xFFBFFFDF, 0xFFF7FFBB, 0xDE7B7FF7
193};
194
195static const u32 tuning_block_128[] = {
196 0xFF00FFFF, 0x0000FFFF, 0xCCCCFFFF, 0xCCCC33CC,
197 0xCC3333CC, 0xFFFFCCCC, 0xFFFFEEFF, 0xFFEEEEFF,
198 0xFFDDFFFF, 0xDDDDFFFF, 0xBBFFFFFF, 0xBBFFFFFF,
199 0xFFFFFFBB, 0xFFFFFF77, 0x77FF7777, 0xFFEEDDBB,
200 0x00FFFFFF, 0x00FFFFFF, 0xCCFFFF00, 0xCC33CCCC,
201 0x3333CCCC, 0xFFCCCCCC, 0xFFEEFFFF, 0xEEEEFFFF,
202 0xDDFFFFFF, 0xDDFFFFFF, 0xFFFFFFDD, 0xFFFFFFBB,
203 0xFFFFBBBB, 0xFFFF77FF, 0xFF7777FF, 0xEEDDBB77
204};
Asutosh Das0ef24812012-12-18 16:14:02 +0530205
Venkat Gopalakrishnan270580a2013-03-11 12:17:57 -0700206static int disable_slots;
207/* root can write, others read */
208module_param(disable_slots, int, S_IRUGO|S_IWUSR);
209
Asutosh Das0ef24812012-12-18 16:14:02 +0530210enum vdd_io_level {
211 /* set vdd_io_data->low_vol_level */
212 VDD_IO_LOW,
213 /* set vdd_io_data->high_vol_level */
214 VDD_IO_HIGH,
215 /*
216 * set whatever there in voltage_level (third argument) of
217 * sdhci_msm_set_vdd_io_vol() function.
218 */
219 VDD_IO_SET_LEVEL,
220};
221
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700222/* MSM platform specific tuning */
223static inline int msm_dll_poll_ck_out_en(struct sdhci_host *host,
224 u8 poll)
225{
226 int rc = 0;
227 u32 wait_cnt = 50;
228 u8 ck_out_en = 0;
229 struct mmc_host *mmc = host->mmc;
230
231 /* poll for CK_OUT_EN bit. max. poll time = 50us */
232 ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) &
233 CORE_CK_OUT_EN);
234
235 while (ck_out_en != poll) {
236 if (--wait_cnt == 0) {
237 pr_err("%s: %s: CK_OUT_EN bit is not %d\n",
238 mmc_hostname(mmc), __func__, poll);
239 rc = -ETIMEDOUT;
240 goto out;
241 }
242 udelay(1);
243
244 ck_out_en = !!(readl_relaxed(host->ioaddr +
245 CORE_DLL_CONFIG) & CORE_CK_OUT_EN);
246 }
247out:
248 return rc;
249}
250
Asutosh Dase5e9ca62013-07-30 19:08:36 +0530251/*
252 * Enable CDR to track changes of DAT lines and adjust sampling
253 * point according to voltage/temperature variations
254 */
255static int msm_enable_cdr_cm_sdc4_dll(struct sdhci_host *host)
256{
257 int rc = 0;
258 u32 config;
259
260 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
261 config |= CORE_CDR_EN;
262 config &= ~(CORE_CDR_EXT_EN | CORE_CK_OUT_EN);
263 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
264
265 rc = msm_dll_poll_ck_out_en(host, 0);
266 if (rc)
267 goto err;
268
269 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) |
270 CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
271
272 rc = msm_dll_poll_ck_out_en(host, 1);
273 if (rc)
274 goto err;
275 goto out;
276err:
277 pr_err("%s: %s: failed\n", mmc_hostname(host->mmc), __func__);
278out:
279 return rc;
280}
281
282static ssize_t store_auto_cmd21(struct device *dev, struct device_attribute
283 *attr, const char *buf, size_t count)
284{
285 struct sdhci_host *host = dev_get_drvdata(dev);
286 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
287 struct sdhci_msm_host *msm_host = pltfm_host->priv;
288 u32 tmp;
289 unsigned long flags;
290
291 if (!kstrtou32(buf, 0, &tmp)) {
292 spin_lock_irqsave(&host->lock, flags);
293 msm_host->en_auto_cmd21 = !!tmp;
294 spin_unlock_irqrestore(&host->lock, flags);
295 }
296 return count;
297}
298
299static ssize_t show_auto_cmd21(struct device *dev,
300 struct device_attribute *attr, char *buf)
301{
302 struct sdhci_host *host = dev_get_drvdata(dev);
303 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
304 struct sdhci_msm_host *msm_host = pltfm_host->priv;
305
306 return snprintf(buf, PAGE_SIZE, "%d\n", msm_host->en_auto_cmd21);
307}
308
309/* MSM auto-tuning handler */
310static int sdhci_msm_config_auto_tuning_cmd(struct sdhci_host *host,
311 bool enable,
312 u32 type)
313{
314 int rc = 0;
315 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
316 struct sdhci_msm_host *msm_host = pltfm_host->priv;
317 u32 val = 0;
318
319 if (!msm_host->en_auto_cmd21)
320 return 0;
321
322 if (type == MMC_SEND_TUNING_BLOCK_HS200)
323 val = CORE_HC_AUTO_CMD21_EN;
324 else
325 return 0;
326
327 if (enable) {
328 rc = msm_enable_cdr_cm_sdc4_dll(host);
329 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
330 val, host->ioaddr + CORE_VENDOR_SPEC);
331 } else {
332 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
333 ~val, host->ioaddr + CORE_VENDOR_SPEC);
334 }
335 return rc;
336}
337
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700338static int msm_config_cm_dll_phase(struct sdhci_host *host, u8 phase)
339{
340 int rc = 0;
341 u8 grey_coded_phase_table[] = {0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4,
342 0xC, 0xD, 0xF, 0xE, 0xA, 0xB, 0x9,
343 0x8};
344 unsigned long flags;
345 u32 config;
346 struct mmc_host *mmc = host->mmc;
347
348 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
349 spin_lock_irqsave(&host->lock, flags);
350
351 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
352 config &= ~(CORE_CDR_EN | CORE_CK_OUT_EN);
353 config |= (CORE_CDR_EXT_EN | CORE_DLL_EN);
354 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
355
356 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '0' */
357 rc = msm_dll_poll_ck_out_en(host, 0);
358 if (rc)
359 goto err_out;
360
361 /*
362 * Write the selected DLL clock output phase (0 ... 15)
363 * to CDR_SELEXT bit field of DLL_CONFIG register.
364 */
365 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
366 & ~(0xF << 20))
367 | (grey_coded_phase_table[phase] << 20)),
368 host->ioaddr + CORE_DLL_CONFIG);
369
370 /* Set CK_OUT_EN bit of DLL_CONFIG register to 1. */
371 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
372 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
373
374 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '1' */
375 rc = msm_dll_poll_ck_out_en(host, 1);
376 if (rc)
377 goto err_out;
378
379 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
380 config |= CORE_CDR_EN;
381 config &= ~CORE_CDR_EXT_EN;
382 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
383 goto out;
384
385err_out:
386 pr_err("%s: %s: Failed to set DLL phase: %d\n",
387 mmc_hostname(mmc), __func__, phase);
388out:
389 spin_unlock_irqrestore(&host->lock, flags);
390 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
391 return rc;
392}
393
394/*
395 * Find out the greatest range of consecuitive selected
396 * DLL clock output phases that can be used as sampling
397 * setting for SD3.0 UHS-I card read operation (in SDR104
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700398 * timing mode) or for eMMC4.5 card read operation (in
399 * HS400/HS200 timing mode).
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700400 * Select the 3/4 of the range and configure the DLL with the
401 * selected DLL clock output phase.
402 */
403
404static int msm_find_most_appropriate_phase(struct sdhci_host *host,
405 u8 *phase_table, u8 total_phases)
406{
407 int ret;
408 u8 ranges[MAX_PHASES][MAX_PHASES] = { {0}, {0} };
409 u8 phases_per_row[MAX_PHASES] = {0};
410 int row_index = 0, col_index = 0, selected_row_index = 0, curr_max = 0;
411 int i, cnt, phase_0_raw_index = 0, phase_15_raw_index = 0;
412 bool phase_0_found = false, phase_15_found = false;
413 struct mmc_host *mmc = host->mmc;
414
415 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
416 if (!total_phases || (total_phases > MAX_PHASES)) {
417 pr_err("%s: %s: invalid argument: total_phases=%d\n",
418 mmc_hostname(mmc), __func__, total_phases);
419 return -EINVAL;
420 }
421
422 for (cnt = 0; cnt < total_phases; cnt++) {
423 ranges[row_index][col_index] = phase_table[cnt];
424 phases_per_row[row_index] += 1;
425 col_index++;
426
427 if ((cnt + 1) == total_phases) {
428 continue;
429 /* check if next phase in phase_table is consecutive or not */
430 } else if ((phase_table[cnt] + 1) != phase_table[cnt + 1]) {
431 row_index++;
432 col_index = 0;
433 }
434 }
435
436 if (row_index >= MAX_PHASES)
437 return -EINVAL;
438
439 /* Check if phase-0 is present in first valid window? */
440 if (!ranges[0][0]) {
441 phase_0_found = true;
442 phase_0_raw_index = 0;
443 /* Check if cycle exist between 2 valid windows */
444 for (cnt = 1; cnt <= row_index; cnt++) {
445 if (phases_per_row[cnt]) {
446 for (i = 0; i < phases_per_row[cnt]; i++) {
447 if (ranges[cnt][i] == 15) {
448 phase_15_found = true;
449 phase_15_raw_index = cnt;
450 break;
451 }
452 }
453 }
454 }
455 }
456
457 /* If 2 valid windows form cycle then merge them as single window */
458 if (phase_0_found && phase_15_found) {
459 /* number of phases in raw where phase 0 is present */
460 u8 phases_0 = phases_per_row[phase_0_raw_index];
461 /* number of phases in raw where phase 15 is present */
462 u8 phases_15 = phases_per_row[phase_15_raw_index];
463
464 if (phases_0 + phases_15 >= MAX_PHASES)
465 /*
466 * If there are more than 1 phase windows then total
467 * number of phases in both the windows should not be
468 * more than or equal to MAX_PHASES.
469 */
470 return -EINVAL;
471
472 /* Merge 2 cyclic windows */
473 i = phases_15;
474 for (cnt = 0; cnt < phases_0; cnt++) {
475 ranges[phase_15_raw_index][i] =
476 ranges[phase_0_raw_index][cnt];
477 if (++i >= MAX_PHASES)
478 break;
479 }
480
481 phases_per_row[phase_0_raw_index] = 0;
482 phases_per_row[phase_15_raw_index] = phases_15 + phases_0;
483 }
484
485 for (cnt = 0; cnt <= row_index; cnt++) {
486 if (phases_per_row[cnt] > curr_max) {
487 curr_max = phases_per_row[cnt];
488 selected_row_index = cnt;
489 }
490 }
491
492 i = ((curr_max * 3) / 4);
493 if (i)
494 i--;
495
496 ret = (int)ranges[selected_row_index][i];
497
498 if (ret >= MAX_PHASES) {
499 ret = -EINVAL;
500 pr_err("%s: %s: invalid phase selected=%d\n",
501 mmc_hostname(mmc), __func__, ret);
502 }
503
504 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
505 return ret;
506}
507
508static inline void msm_cm_dll_set_freq(struct sdhci_host *host)
509{
510 u32 mclk_freq = 0;
511
512 /* Program the MCLK value to MCLK_FREQ bit field */
513 if (host->clock <= 112000000)
514 mclk_freq = 0;
515 else if (host->clock <= 125000000)
516 mclk_freq = 1;
517 else if (host->clock <= 137000000)
518 mclk_freq = 2;
519 else if (host->clock <= 150000000)
520 mclk_freq = 3;
521 else if (host->clock <= 162000000)
522 mclk_freq = 4;
523 else if (host->clock <= 175000000)
524 mclk_freq = 5;
525 else if (host->clock <= 187000000)
526 mclk_freq = 6;
527 else if (host->clock <= 200000000)
528 mclk_freq = 7;
529
530 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
531 & ~(7 << 24)) | (mclk_freq << 24)),
532 host->ioaddr + CORE_DLL_CONFIG);
533}
534
535/* Initialize the DLL (Programmable Delay Line ) */
536static int msm_init_cm_dll(struct sdhci_host *host)
537{
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800538 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
539 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700540 struct mmc_host *mmc = host->mmc;
541 int rc = 0;
542 unsigned long flags;
543 u32 wait_cnt;
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530544 bool prev_pwrsave, curr_pwrsave;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700545
546 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
547 spin_lock_irqsave(&host->lock, flags);
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530548 prev_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
549 CORE_CLK_PWRSAVE);
550 curr_pwrsave = prev_pwrsave;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700551 /*
552 * Make sure that clock is always enabled when DLL
553 * tuning is in progress. Keeping PWRSAVE ON may
554 * turn off the clock. So let's disable the PWRSAVE
555 * here and re-enable it once tuning is completed.
556 */
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530557 if (prev_pwrsave) {
558 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
559 & ~CORE_CLK_PWRSAVE),
560 host->ioaddr + CORE_VENDOR_SPEC);
561 curr_pwrsave = false;
562 }
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700563
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800564 if (msm_host->use_updated_dll_reset) {
565 /* Disable the DLL clock */
566 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
567 & ~CORE_CK_OUT_EN),
568 host->ioaddr + CORE_DLL_CONFIG);
569
570 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
571 | CORE_DLL_CLOCK_DISABLE),
572 host->ioaddr + CORE_DLL_CONFIG_2);
573 }
574
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700575 /* Write 1 to DLL_RST bit of DLL_CONFIG register */
576 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
577 | CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
578
579 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
580 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
581 | CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
582 msm_cm_dll_set_freq(host);
583
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800584 if (msm_host->use_updated_dll_reset) {
585 u32 mclk_freq = 0;
586
587 if ((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
588 & CORE_FLL_CYCLE_CNT))
589 mclk_freq = (u32) ((host->clock / TCXO_FREQ) * 8);
590 else
591 mclk_freq = (u32) ((host->clock / TCXO_FREQ) * 4);
592
593 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
594 & ~(0xFF << 10)) | (mclk_freq << 10)),
595 host->ioaddr + CORE_DLL_CONFIG_2);
596 /* wait for 5us before enabling DLL clock */
597 udelay(5);
598 }
599
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700600 /* Write 0 to DLL_RST bit of DLL_CONFIG register */
601 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
602 & ~CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
603
604 /* Write 0 to DLL_PDN bit of DLL_CONFIG register */
605 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
606 & ~CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
607
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800608 if (msm_host->use_updated_dll_reset) {
609 msm_cm_dll_set_freq(host);
610 /* Enable the DLL clock */
611 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
612 & ~CORE_DLL_CLOCK_DISABLE),
613 host->ioaddr + CORE_DLL_CONFIG_2);
614 }
615
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700616 /* Set DLL_EN bit to 1. */
617 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
618 | CORE_DLL_EN), host->ioaddr + CORE_DLL_CONFIG);
619
620 /* Set CK_OUT_EN bit to 1. */
621 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
622 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
623
624 wait_cnt = 50;
625 /* Wait until DLL_LOCK bit of DLL_STATUS register becomes '1' */
626 while (!(readl_relaxed(host->ioaddr + CORE_DLL_STATUS) &
627 CORE_DLL_LOCK)) {
628 /* max. wait for 50us sec for LOCK bit to be set */
629 if (--wait_cnt == 0) {
630 pr_err("%s: %s: DLL failed to LOCK\n",
631 mmc_hostname(mmc), __func__);
632 rc = -ETIMEDOUT;
633 goto out;
634 }
635 /* wait for 1us before polling again */
636 udelay(1);
637 }
638
639out:
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530640 /* Restore the correct PWRSAVE state */
641 if (prev_pwrsave ^ curr_pwrsave) {
642 u32 reg = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC);
643
644 if (prev_pwrsave)
645 reg |= CORE_CLK_PWRSAVE;
646 else
647 reg &= ~CORE_CLK_PWRSAVE;
648
649 writel_relaxed(reg, host->ioaddr + CORE_VENDOR_SPEC);
650 }
651
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700652 spin_unlock_irqrestore(&host->lock, flags);
653 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
654 return rc;
655}
656
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700657static int sdhci_msm_cdclp533_calibration(struct sdhci_host *host)
658{
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700659 u32 calib_done;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700660 int ret = 0;
661 int cdc_err = 0;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700662
663 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
664
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700665 /* Write 0 to CDC_T4_DLY_SEL field in VENDOR_SPEC_DDR200_CFG */
666 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
667 & ~CORE_CDC_T4_DLY_SEL),
668 host->ioaddr + CORE_DDR_200_CFG);
669
670 /* Write 0 to CDC_SWITCH_BYPASS_OFF field in CORE_CSR_CDC_GEN_CFG */
671 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG)
672 & ~CORE_CDC_SWITCH_BYPASS_OFF),
673 host->ioaddr + CORE_CSR_CDC_GEN_CFG);
674
675 /* Write 1 to CDC_SWITCH_RC_EN field in CORE_CSR_CDC_GEN_CFG */
676 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG)
677 | CORE_CDC_SWITCH_RC_EN),
678 host->ioaddr + CORE_CSR_CDC_GEN_CFG);
679
680 /* Write 0 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
681 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
682 & ~CORE_START_CDC_TRAFFIC),
683 host->ioaddr + CORE_DDR_200_CFG);
684
685 /*
686 * Perform CDC Register Initialization Sequence
687 *
688 * CORE_CSR_CDC_CTLR_CFG0 0x11800EC
689 * CORE_CSR_CDC_CTLR_CFG1 0x3011111
690 * CORE_CSR_CDC_CAL_TIMER_CFG0 0x1201000
691 * CORE_CSR_CDC_CAL_TIMER_CFG1 0x4
692 * CORE_CSR_CDC_REFCOUNT_CFG 0xCB732020
693 * CORE_CSR_CDC_COARSE_CAL_CFG 0xB19
694 * CORE_CSR_CDC_DELAY_CFG 0x3AC
695 * CORE_CDC_OFFSET_CFG 0x0
696 * CORE_CDC_SLAVE_DDA_CFG 0x16334
697 */
698
699 writel_relaxed(0x11800EC, host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
700 writel_relaxed(0x3011111, host->ioaddr + CORE_CSR_CDC_CTLR_CFG1);
701 writel_relaxed(0x1201000, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
702 writel_relaxed(0x4, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG1);
703 writel_relaxed(0xCB732020, host->ioaddr + CORE_CSR_CDC_REFCOUNT_CFG);
704 writel_relaxed(0xB19, host->ioaddr + CORE_CSR_CDC_COARSE_CAL_CFG);
Subhash Jadavanibe406d92014-06-17 16:47:48 -0700705 writel_relaxed(0x4E2, host->ioaddr + CORE_CSR_CDC_DELAY_CFG);
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700706 writel_relaxed(0x0, host->ioaddr + CORE_CDC_OFFSET_CFG);
707 writel_relaxed(0x16334, host->ioaddr + CORE_CDC_SLAVE_DDA_CFG);
708
709 /* CDC HW Calibration */
710
711 /* Write 1 to SW_TRIG_FULL_CALIB field in CORE_CSR_CDC_CTLR_CFG0 */
712 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
713 | CORE_SW_TRIG_FULL_CALIB),
714 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
715
716 /* Write 0 to SW_TRIG_FULL_CALIB field in CORE_CSR_CDC_CTLR_CFG0 */
717 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
718 & ~CORE_SW_TRIG_FULL_CALIB),
719 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
720
721 /* Write 1 to HW_AUTOCAL_ENA field in CORE_CSR_CDC_CTLR_CFG0 */
722 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
723 | CORE_HW_AUTOCAL_ENA),
724 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
725
726 /* Write 1 to TIMER_ENA field in CORE_CSR_CDC_CAL_TIMER_CFG0 */
727 writel_relaxed((readl_relaxed(host->ioaddr +
728 CORE_CSR_CDC_CAL_TIMER_CFG0) | CORE_TIMER_ENA),
729 host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
730
731 mb();
732
733 /* Poll on CALIBRATION_DONE field in CORE_CSR_CDC_STATUS0 to be 1 */
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700734 ret = readl_poll_timeout(host->ioaddr + CORE_CSR_CDC_STATUS0,
735 calib_done, (calib_done & CORE_CALIBRATION_DONE), 1, 50);
736
737 if (ret == -ETIMEDOUT) {
738 pr_err("%s: %s: CDC Calibration was not completed\n",
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700739 mmc_hostname(host->mmc), __func__);
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700740 goto out;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700741 }
742
743 /* Verify CDC_ERROR_CODE field in CORE_CSR_CDC_STATUS0 is 0 */
744 cdc_err = readl_relaxed(host->ioaddr + CORE_CSR_CDC_STATUS0)
745 & CORE_CDC_ERROR_CODE_MASK;
746 if (cdc_err) {
747 pr_err("%s: %s: CDC Error Code %d\n",
748 mmc_hostname(host->mmc), __func__, cdc_err);
749 ret = -EINVAL;
750 goto out;
751 }
752
753 /* Write 1 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
754 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
755 | CORE_START_CDC_TRAFFIC),
756 host->ioaddr + CORE_DDR_200_CFG);
757out:
758 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
759 __func__, ret);
760 return ret;
761}
762
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700763static int sdhci_msm_cm_dll_sdc4_calibration(struct sdhci_host *host)
764{
Ritesh Harjani764065e2015-05-13 14:14:45 +0530765 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
766 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700767 u32 dll_status;
768 int ret = 0;
769
770 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
771
772 /*
773 * Currently the CORE_DDR_CONFIG register defaults to desired
774 * configuration on reset. Currently reprogramming the power on
775 * reset (POR) value in case it might have been modified by
776 * bootloaders. In the future, if this changes, then the desired
777 * values will need to be programmed appropriately.
778 */
779 writel_relaxed(DDR_CONFIG_POR_VAL, host->ioaddr + CORE_DDR_CONFIG);
780
Ritesh Harjaniea709662015-05-27 15:40:24 +0530781 if (msm_host->enhanced_strobe)
782 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
783 | CORE_CMDIN_RCLK_EN),
784 host->ioaddr + CORE_DDR_200_CFG);
785
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700786 /* Write 1 to DDR_CAL_EN field in CORE_DLL_CONFIG_2 */
787 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
788 | CORE_DDR_CAL_EN),
789 host->ioaddr + CORE_DLL_CONFIG_2);
790
791 /* Poll on DDR_DLL_LOCK bit in CORE_DLL_STATUS to be set */
792 ret = readl_poll_timeout(host->ioaddr + CORE_DLL_STATUS,
793 dll_status, (dll_status & CORE_DDR_DLL_LOCK), 10, 1000);
794
795 if (ret == -ETIMEDOUT) {
796 pr_err("%s: %s: CM_DLL_SDC4 Calibration was not completed\n",
797 mmc_hostname(host->mmc), __func__);
798 goto out;
799 }
800
Ritesh Harjani764065e2015-05-13 14:14:45 +0530801 /*
802 * set CORE_PWRSAVE_DLL bit in CORE_VENDOR_SPEC3.
803 * when MCLK is gated OFF, it is not gated for less than 0.5us
804 * and MCLK must be switched on for at-least 1us before DATA
805 * starts coming. Controllers with 14lpp tech DLL cannot
806 * guarantee above requirement. So PWRSAVE_DLL should not be
807 * turned on for host controllers using this DLL.
808 */
809 if (!msm_host->use_14lpp_dll)
810 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC3)
811 | CORE_PWRSAVE_DLL),
812 host->ioaddr + CORE_VENDOR_SPEC3);
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700813 mb();
814out:
815 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
816 __func__, ret);
817 return ret;
818}
819
Ritesh Harjaniea709662015-05-27 15:40:24 +0530820static int sdhci_msm_enhanced_strobe(struct sdhci_host *host)
821{
822 int ret = 0;
823 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
824 struct sdhci_msm_host *msm_host = pltfm_host->priv;
825 struct mmc_host *mmc = host->mmc;
826
827 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
828
829 if (!msm_host->enhanced_strobe) {
830 pr_debug("%s: host does not support hs400 enhanced strobe\n",
831 mmc_hostname(mmc));
832 return -EINVAL;
833 }
834
835 if (msm_host->calibration_done ||
836 !(mmc->ios.timing == MMC_TIMING_MMC_HS400)) {
837 return 0;
838 }
839
840 /*
841 * Reset the tuning block.
842 */
843 ret = msm_init_cm_dll(host);
844 if (ret)
845 goto out;
846
847 ret = sdhci_msm_cm_dll_sdc4_calibration(host);
848out:
849 if (!ret)
850 msm_host->calibration_done = true;
851 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
852 __func__, ret);
853 return ret;
854}
855
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700856static int sdhci_msm_hs400_dll_calibration(struct sdhci_host *host)
857{
858 int ret = 0;
859 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
860 struct sdhci_msm_host *msm_host = pltfm_host->priv;
861
862 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
863
864 /*
865 * Retuning in HS400 (DDR mode) will fail, just reset the
866 * tuning block and restore the saved tuning phase.
867 */
868 ret = msm_init_cm_dll(host);
869 if (ret)
870 goto out;
871
872 /* Set the selected phase in delay line hw block */
873 ret = msm_config_cm_dll_phase(host, msm_host->saved_tuning_phase);
874 if (ret)
875 goto out;
876
Krishna Konda0e8efba2014-06-23 14:50:38 -0700877 /* Write 1 to CMD_DAT_TRACK_SEL field in DLL_CONFIG */
878 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
879 | CORE_CMD_DAT_TRACK_SEL),
880 host->ioaddr + CORE_DLL_CONFIG);
881
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700882 if (msm_host->use_cdclp533)
883 /* Calibrate CDCLP533 DLL HW */
884 ret = sdhci_msm_cdclp533_calibration(host);
885 else
886 /* Calibrate CM_DLL_SDC4 HW */
887 ret = sdhci_msm_cm_dll_sdc4_calibration(host);
888out:
889 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
890 __func__, ret);
891 return ret;
892}
893
Krishna Konda96e6b112013-10-28 15:25:03 -0700894static void sdhci_msm_set_mmc_drv_type(struct sdhci_host *host, u32 opcode,
895 u8 drv_type)
896{
897 struct mmc_command cmd = {0};
898 struct mmc_request mrq = {NULL};
899 struct mmc_host *mmc = host->mmc;
900 u8 val = ((drv_type << 4) | 2);
901
902 cmd.opcode = MMC_SWITCH;
903 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
904 (EXT_CSD_HS_TIMING << 16) |
905 (val << 8) |
906 EXT_CSD_CMD_SET_NORMAL;
907 cmd.flags = MMC_CMD_AC | MMC_RSP_R1B;
908 /* 1 sec */
909 cmd.busy_timeout = 1000 * 1000;
910
911 memset(cmd.resp, 0, sizeof(cmd.resp));
912 cmd.retries = 3;
913
914 mrq.cmd = &cmd;
915 cmd.data = NULL;
916
917 mmc_wait_for_req(mmc, &mrq);
918 pr_debug("%s: %s: set card drive type to %d\n",
919 mmc_hostname(mmc), __func__,
920 drv_type);
921}
922
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700923int sdhci_msm_execute_tuning(struct sdhci_host *host, u32 opcode)
924{
925 unsigned long flags;
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530926 int tuning_seq_cnt = 3;
Krishna Konda96e6b112013-10-28 15:25:03 -0700927 u8 phase, *data_buf, tuned_phases[NUM_TUNING_PHASES], tuned_phase_cnt;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700928 const u32 *tuning_block_pattern = tuning_block_64;
929 int size = sizeof(tuning_block_64); /* Tuning pattern size in bytes */
930 int rc;
931 struct mmc_host *mmc = host->mmc;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530932 struct mmc_ios ios = host->mmc->ios;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700933 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
934 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Krishna Konda96e6b112013-10-28 15:25:03 -0700935 u8 drv_type = 0;
936 bool drv_type_changed = false;
937 struct mmc_card *card = host->mmc->card;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530938
939 /*
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700940 * Tuning is required for SDR104, HS200 and HS400 cards and
941 * if clock frequency is greater than 100MHz in these modes.
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530942 */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700943 if (host->clock <= CORE_FREQ_100MHZ ||
944 !((ios.timing == MMC_TIMING_MMC_HS400) ||
945 (ios.timing == MMC_TIMING_MMC_HS200) ||
946 (ios.timing == MMC_TIMING_UHS_SDR104)))
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530947 return 0;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700948
949 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700950
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700951 /* CDC/SDC4 DLL HW calibration is only required for HS400 mode*/
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700952 if (msm_host->tuning_done && !msm_host->calibration_done &&
953 (mmc->ios.timing == MMC_TIMING_MMC_HS400)) {
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700954 rc = sdhci_msm_hs400_dll_calibration(host);
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700955 spin_lock_irqsave(&host->lock, flags);
956 if (!rc)
957 msm_host->calibration_done = true;
958 spin_unlock_irqrestore(&host->lock, flags);
959 goto out;
960 }
961
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700962 spin_lock_irqsave(&host->lock, flags);
963
964 if ((opcode == MMC_SEND_TUNING_BLOCK_HS200) &&
965 (mmc->ios.bus_width == MMC_BUS_WIDTH_8)) {
966 tuning_block_pattern = tuning_block_128;
967 size = sizeof(tuning_block_128);
968 }
969 spin_unlock_irqrestore(&host->lock, flags);
970
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700971 data_buf = kmalloc(size, GFP_KERNEL);
972 if (!data_buf) {
973 rc = -ENOMEM;
974 goto out;
975 }
976
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530977retry:
Krishna Konda96e6b112013-10-28 15:25:03 -0700978 tuned_phase_cnt = 0;
979
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530980 /* first of all reset the tuning block */
981 rc = msm_init_cm_dll(host);
982 if (rc)
983 goto kfree;
984
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700985 phase = 0;
986 do {
987 struct mmc_command cmd = {0};
988 struct mmc_data data = {0};
989 struct mmc_request mrq = {
990 .cmd = &cmd,
991 .data = &data
992 };
993 struct scatterlist sg;
994
995 /* set the phase in delay line hw block */
996 rc = msm_config_cm_dll_phase(host, phase);
997 if (rc)
998 goto kfree;
999
1000 cmd.opcode = opcode;
1001 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1002
1003 data.blksz = size;
1004 data.blocks = 1;
1005 data.flags = MMC_DATA_READ;
1006 data.timeout_ns = 1000 * 1000 * 1000; /* 1 sec */
1007
1008 data.sg = &sg;
1009 data.sg_len = 1;
1010 sg_init_one(&sg, data_buf, size);
1011 memset(data_buf, 0, size);
1012 mmc_wait_for_req(mmc, &mrq);
1013
1014 if (!cmd.error && !data.error &&
1015 !memcmp(data_buf, tuning_block_pattern, size)) {
1016 /* tuning is successful at this tuning point */
1017 tuned_phases[tuned_phase_cnt++] = phase;
Krishna Konda96e6b112013-10-28 15:25:03 -07001018 pr_debug("%s: %s: found *** good *** phase = %d\n",
1019 mmc_hostname(mmc), __func__, phase);
1020 } else {
1021 pr_debug("%s: %s: found ## bad ## phase = %d\n",
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001022 mmc_hostname(mmc), __func__, phase);
1023 }
1024 } while (++phase < 16);
1025
Sahitya Tummaladfdb4af2014-04-01 14:29:13 +05301026 if ((tuned_phase_cnt == NUM_TUNING_PHASES) &&
1027 card && mmc_card_mmc(card)) {
Krishna Konda96e6b112013-10-28 15:25:03 -07001028 /*
1029 * If all phases pass then its a problem. So change the card's
1030 * drive type to a different value, if supported and repeat
1031 * tuning until at least one phase fails. Then set the original
1032 * drive type back.
1033 *
1034 * If all the phases still pass after trying all possible
1035 * drive types, then one of those 16 phases will be picked.
1036 * This is no different from what was going on before the
1037 * modification to change drive type and retune.
1038 */
1039 pr_debug("%s: tuned phases count: %d\n", mmc_hostname(mmc),
1040 tuned_phase_cnt);
1041
1042 /* set drive type to other value . default setting is 0x0 */
1043 while (++drv_type <= MAX_DRV_TYPES_SUPPORTED_HS200) {
1044 if (card->ext_csd.raw_driver_strength &
1045 (1 << drv_type)) {
1046 sdhci_msm_set_mmc_drv_type(host, opcode,
1047 drv_type);
1048 if (!drv_type_changed)
1049 drv_type_changed = true;
1050 goto retry;
1051 }
1052 }
1053 }
1054
1055 /* reset drive type to default (50 ohm) if changed */
1056 if (drv_type_changed)
1057 sdhci_msm_set_mmc_drv_type(host, opcode, 0);
1058
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001059 if (tuned_phase_cnt) {
1060 rc = msm_find_most_appropriate_phase(host, tuned_phases,
1061 tuned_phase_cnt);
1062 if (rc < 0)
1063 goto kfree;
1064 else
1065 phase = (u8)rc;
1066
1067 /*
1068 * Finally set the selected phase in delay
1069 * line hw block.
1070 */
1071 rc = msm_config_cm_dll_phase(host, phase);
1072 if (rc)
1073 goto kfree;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07001074 msm_host->saved_tuning_phase = phase;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001075 pr_debug("%s: %s: finally setting the tuning phase to %d\n",
1076 mmc_hostname(mmc), __func__, phase);
1077 } else {
Sahitya Tummala9fe16532013-06-13 10:36:57 +05301078 if (--tuning_seq_cnt)
1079 goto retry;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001080 /* tuning failed */
1081 pr_err("%s: %s: no tuning point found\n",
1082 mmc_hostname(mmc), __func__);
Sahitya Tummala9fe16532013-06-13 10:36:57 +05301083 rc = -EIO;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001084 }
1085
1086kfree:
1087 kfree(data_buf);
1088out:
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07001089 spin_lock_irqsave(&host->lock, flags);
1090 if (!rc)
1091 msm_host->tuning_done = true;
1092 spin_unlock_irqrestore(&host->lock, flags);
1093 pr_debug("%s: Exit %s, err(%d)\n", mmc_hostname(mmc), __func__, rc);
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001094 return rc;
1095}
1096
Asutosh Das0ef24812012-12-18 16:14:02 +05301097static int sdhci_msm_setup_gpio(struct sdhci_msm_pltfm_data *pdata, bool enable)
1098{
1099 struct sdhci_msm_gpio_data *curr;
1100 int i, ret = 0;
1101
1102 curr = pdata->pin_data->gpio_data;
1103 for (i = 0; i < curr->size; i++) {
1104 if (!gpio_is_valid(curr->gpio[i].no)) {
1105 ret = -EINVAL;
1106 pr_err("%s: Invalid gpio = %d\n", __func__,
1107 curr->gpio[i].no);
1108 goto free_gpios;
1109 }
1110 if (enable) {
1111 ret = gpio_request(curr->gpio[i].no,
1112 curr->gpio[i].name);
1113 if (ret) {
1114 pr_err("%s: gpio_request(%d, %s) failed %d\n",
1115 __func__, curr->gpio[i].no,
1116 curr->gpio[i].name, ret);
1117 goto free_gpios;
1118 }
1119 curr->gpio[i].is_enabled = true;
1120 } else {
1121 gpio_free(curr->gpio[i].no);
1122 curr->gpio[i].is_enabled = false;
1123 }
1124 }
1125 return ret;
1126
1127free_gpios:
1128 for (i--; i >= 0; i--) {
1129 gpio_free(curr->gpio[i].no);
1130 curr->gpio[i].is_enabled = false;
1131 }
1132 return ret;
1133}
1134
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301135static int sdhci_msm_setup_pinctrl(struct sdhci_msm_pltfm_data *pdata,
1136 bool enable)
1137{
1138 int ret = 0;
1139
1140 if (enable)
1141 ret = pinctrl_select_state(pdata->pctrl_data->pctrl,
1142 pdata->pctrl_data->pins_active);
1143 else
1144 ret = pinctrl_select_state(pdata->pctrl_data->pctrl,
1145 pdata->pctrl_data->pins_sleep);
1146
1147 if (ret < 0)
1148 pr_err("%s state for pinctrl failed with %d\n",
1149 enable ? "Enabling" : "Disabling", ret);
1150
1151 return ret;
1152}
1153
Asutosh Das0ef24812012-12-18 16:14:02 +05301154static int sdhci_msm_setup_pins(struct sdhci_msm_pltfm_data *pdata, bool enable)
1155{
1156 int ret = 0;
1157
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301158 if (pdata->pin_cfg_sts == enable) {
Asutosh Das0ef24812012-12-18 16:14:02 +05301159 return 0;
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301160 } else if (pdata->pctrl_data) {
1161 ret = sdhci_msm_setup_pinctrl(pdata, enable);
1162 goto out;
1163 } else if (!pdata->pin_data) {
1164 return 0;
1165 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301166
Sahitya Tummala1cd7e072014-02-14 13:19:01 +05301167 if (pdata->pin_data->is_gpio)
1168 ret = sdhci_msm_setup_gpio(pdata, enable);
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301169out:
Asutosh Das0ef24812012-12-18 16:14:02 +05301170 if (!ret)
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301171 pdata->pin_cfg_sts = enable;
Asutosh Das0ef24812012-12-18 16:14:02 +05301172
1173 return ret;
1174}
1175
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301176static int sdhci_msm_dt_get_array(struct device *dev, const char *prop_name,
1177 u32 **out, int *len, u32 size)
1178{
1179 int ret = 0;
1180 struct device_node *np = dev->of_node;
1181 size_t sz;
1182 u32 *arr = NULL;
1183
1184 if (!of_get_property(np, prop_name, len)) {
1185 ret = -EINVAL;
1186 goto out;
1187 }
1188 sz = *len = *len / sizeof(*arr);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07001189 if (sz <= 0 || (size > 0 && (sz > size))) {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301190 dev_err(dev, "%s invalid size\n", prop_name);
1191 ret = -EINVAL;
1192 goto out;
1193 }
1194
1195 arr = devm_kzalloc(dev, sz * sizeof(*arr), GFP_KERNEL);
1196 if (!arr) {
1197 dev_err(dev, "%s failed allocating memory\n", prop_name);
1198 ret = -ENOMEM;
1199 goto out;
1200 }
1201
1202 ret = of_property_read_u32_array(np, prop_name, arr, sz);
1203 if (ret < 0) {
1204 dev_err(dev, "%s failed reading array %d\n", prop_name, ret);
1205 goto out;
1206 }
1207 *out = arr;
1208out:
1209 if (ret)
1210 *len = 0;
1211 return ret;
1212}
1213
Asutosh Das0ef24812012-12-18 16:14:02 +05301214#define MAX_PROP_SIZE 32
1215static int sdhci_msm_dt_parse_vreg_info(struct device *dev,
1216 struct sdhci_msm_reg_data **vreg_data, const char *vreg_name)
1217{
1218 int len, ret = 0;
1219 const __be32 *prop;
1220 char prop_name[MAX_PROP_SIZE];
1221 struct sdhci_msm_reg_data *vreg;
1222 struct device_node *np = dev->of_node;
1223
1224 snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", vreg_name);
1225 if (!of_parse_phandle(np, prop_name, 0)) {
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301226 dev_info(dev, "No vreg data found for %s\n", vreg_name);
Asutosh Das0ef24812012-12-18 16:14:02 +05301227 return ret;
1228 }
1229
1230 vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
1231 if (!vreg) {
1232 dev_err(dev, "No memory for vreg: %s\n", vreg_name);
1233 ret = -ENOMEM;
1234 return ret;
1235 }
1236
1237 vreg->name = vreg_name;
1238
1239 snprintf(prop_name, MAX_PROP_SIZE,
1240 "qcom,%s-always-on", vreg_name);
1241 if (of_get_property(np, prop_name, NULL))
1242 vreg->is_always_on = true;
1243
1244 snprintf(prop_name, MAX_PROP_SIZE,
1245 "qcom,%s-lpm-sup", vreg_name);
1246 if (of_get_property(np, prop_name, NULL))
1247 vreg->lpm_sup = true;
1248
1249 snprintf(prop_name, MAX_PROP_SIZE,
1250 "qcom,%s-voltage-level", vreg_name);
1251 prop = of_get_property(np, prop_name, &len);
1252 if (!prop || (len != (2 * sizeof(__be32)))) {
1253 dev_warn(dev, "%s %s property\n",
1254 prop ? "invalid format" : "no", prop_name);
1255 } else {
1256 vreg->low_vol_level = be32_to_cpup(&prop[0]);
1257 vreg->high_vol_level = be32_to_cpup(&prop[1]);
1258 }
1259
1260 snprintf(prop_name, MAX_PROP_SIZE,
1261 "qcom,%s-current-level", vreg_name);
1262 prop = of_get_property(np, prop_name, &len);
1263 if (!prop || (len != (2 * sizeof(__be32)))) {
1264 dev_warn(dev, "%s %s property\n",
1265 prop ? "invalid format" : "no", prop_name);
1266 } else {
1267 vreg->lpm_uA = be32_to_cpup(&prop[0]);
1268 vreg->hpm_uA = be32_to_cpup(&prop[1]);
1269 }
1270
1271 *vreg_data = vreg;
1272 dev_dbg(dev, "%s: %s %s vol=[%d %d]uV, curr=[%d %d]uA\n",
1273 vreg->name, vreg->is_always_on ? "always_on," : "",
1274 vreg->lpm_sup ? "lpm_sup," : "", vreg->low_vol_level,
1275 vreg->high_vol_level, vreg->lpm_uA, vreg->hpm_uA);
1276
1277 return ret;
1278}
1279
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301280static int sdhci_msm_parse_pinctrl_info(struct device *dev,
1281 struct sdhci_msm_pltfm_data *pdata)
1282{
1283 struct sdhci_pinctrl_data *pctrl_data;
1284 struct pinctrl *pctrl;
1285 int ret = 0;
1286
1287 /* Try to obtain pinctrl handle */
1288 pctrl = devm_pinctrl_get(dev);
1289 if (IS_ERR(pctrl)) {
1290 ret = PTR_ERR(pctrl);
1291 goto out;
1292 }
1293 pctrl_data = devm_kzalloc(dev, sizeof(*pctrl_data), GFP_KERNEL);
1294 if (!pctrl_data) {
1295 dev_err(dev, "No memory for sdhci_pinctrl_data\n");
1296 ret = -ENOMEM;
1297 goto out;
1298 }
1299 pctrl_data->pctrl = pctrl;
1300 /* Look-up and keep the states handy to be used later */
1301 pctrl_data->pins_active = pinctrl_lookup_state(
1302 pctrl_data->pctrl, "active");
1303 if (IS_ERR(pctrl_data->pins_active)) {
1304 ret = PTR_ERR(pctrl_data->pins_active);
1305 dev_err(dev, "Could not get active pinstates, err:%d\n", ret);
1306 goto out;
1307 }
1308 pctrl_data->pins_sleep = pinctrl_lookup_state(
1309 pctrl_data->pctrl, "sleep");
1310 if (IS_ERR(pctrl_data->pins_sleep)) {
1311 ret = PTR_ERR(pctrl_data->pins_sleep);
1312 dev_err(dev, "Could not get sleep pinstates, err:%d\n", ret);
1313 goto out;
1314 }
1315 pdata->pctrl_data = pctrl_data;
1316out:
1317 return ret;
1318}
1319
Asutosh Das0ef24812012-12-18 16:14:02 +05301320#define GPIO_NAME_MAX_LEN 32
1321static int sdhci_msm_dt_parse_gpio_info(struct device *dev,
1322 struct sdhci_msm_pltfm_data *pdata)
1323{
1324 int ret = 0, cnt, i;
1325 struct sdhci_msm_pin_data *pin_data;
1326 struct device_node *np = dev->of_node;
1327
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301328 ret = sdhci_msm_parse_pinctrl_info(dev, pdata);
1329 if (!ret) {
1330 goto out;
1331 } else if (ret == -EPROBE_DEFER) {
1332 dev_err(dev, "Pinctrl framework not registered, err:%d\n", ret);
1333 goto out;
1334 } else {
1335 dev_err(dev, "Parsing Pinctrl failed with %d, falling back on GPIO lib\n",
1336 ret);
Sahitya Tummala1cd7e072014-02-14 13:19:01 +05301337 ret = 0;
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301338 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301339 pin_data = devm_kzalloc(dev, sizeof(*pin_data), GFP_KERNEL);
1340 if (!pin_data) {
1341 dev_err(dev, "No memory for pin_data\n");
1342 ret = -ENOMEM;
1343 goto out;
1344 }
1345
1346 cnt = of_gpio_count(np);
1347 if (cnt > 0) {
1348 pin_data->gpio_data = devm_kzalloc(dev,
1349 sizeof(struct sdhci_msm_gpio_data), GFP_KERNEL);
1350 if (!pin_data->gpio_data) {
1351 dev_err(dev, "No memory for gpio_data\n");
1352 ret = -ENOMEM;
1353 goto out;
1354 }
1355 pin_data->gpio_data->size = cnt;
1356 pin_data->gpio_data->gpio = devm_kzalloc(dev, cnt *
1357 sizeof(struct sdhci_msm_gpio), GFP_KERNEL);
1358
1359 if (!pin_data->gpio_data->gpio) {
1360 dev_err(dev, "No memory for gpio\n");
1361 ret = -ENOMEM;
1362 goto out;
1363 }
1364
1365 for (i = 0; i < cnt; i++) {
1366 const char *name = NULL;
1367 char result[GPIO_NAME_MAX_LEN];
1368 pin_data->gpio_data->gpio[i].no = of_get_gpio(np, i);
1369 of_property_read_string_index(np,
1370 "qcom,gpio-names", i, &name);
1371
1372 snprintf(result, GPIO_NAME_MAX_LEN, "%s-%s",
1373 dev_name(dev), name ? name : "?");
1374 pin_data->gpio_data->gpio[i].name = result;
1375 dev_dbg(dev, "%s: gpio[%s] = %d\n", __func__,
1376 pin_data->gpio_data->gpio[i].name,
1377 pin_data->gpio_data->gpio[i].no);
Asutosh Das0ef24812012-12-18 16:14:02 +05301378 }
1379 }
Sahitya Tummala1cd7e072014-02-14 13:19:01 +05301380 pdata->pin_data = pin_data;
Asutosh Das0ef24812012-12-18 16:14:02 +05301381out:
1382 if (ret)
1383 dev_err(dev, "%s failed with err %d\n", __func__, ret);
1384 return ret;
1385}
1386
Maya Erez994cf2f2014-10-21 20:22:04 +03001387#ifdef CONFIG_SMP
1388static void sdhci_msm_populate_affinity_type(struct sdhci_msm_pltfm_data *pdata,
1389 struct device_node *np)
1390{
1391 const char *cpu_affinity = NULL;
1392
1393 pdata->cpu_affinity_type = PM_QOS_REQ_AFFINE_IRQ;
1394 if (!of_property_read_string(np, "qcom,cpu-affinity",
1395 &cpu_affinity)) {
1396 if (!strcmp(cpu_affinity, "all_cores"))
1397 pdata->cpu_affinity_type = PM_QOS_REQ_ALL_CORES;
1398 else if (!strcmp(cpu_affinity, "affine_cores"))
1399 pdata->cpu_affinity_type = PM_QOS_REQ_AFFINE_CORES;
1400 else if (!strcmp(cpu_affinity, "affine_irq"))
1401 pdata->cpu_affinity_type = PM_QOS_REQ_AFFINE_IRQ;
1402 }
1403}
1404#else
1405static void sdhci_msm_populate_affinity_type(struct sdhci_msm_pltfm_data *pdata,
1406 struct device_node *np)
1407{
1408}
1409#endif
1410
Asutosh Das0ef24812012-12-18 16:14:02 +05301411/* Parse platform data */
Dov Levenglickc9033ab2015-03-10 16:00:56 +02001412static
1413struct sdhci_msm_pltfm_data *sdhci_msm_populate_pdata(struct device *dev,
1414 struct sdhci_msm_host *msm_host)
Asutosh Das0ef24812012-12-18 16:14:02 +05301415{
1416 struct sdhci_msm_pltfm_data *pdata = NULL;
1417 struct device_node *np = dev->of_node;
1418 u32 bus_width = 0;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05301419 u32 cpu_dma_latency;
Asutosh Das0ef24812012-12-18 16:14:02 +05301420 int len, i;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301421 int clk_table_len;
1422 u32 *clk_table = NULL;
Sujit Reddy Thumma1958d3d2013-06-03 09:54:32 +05301423 enum of_gpio_flags flags = OF_GPIO_ACTIVE_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05301424
1425 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1426 if (!pdata) {
1427 dev_err(dev, "failed to allocate memory for platform data\n");
1428 goto out;
1429 }
1430
Sujit Reddy Thumma1958d3d2013-06-03 09:54:32 +05301431 pdata->status_gpio = of_get_named_gpio_flags(np, "cd-gpios", 0, &flags);
1432 if (gpio_is_valid(pdata->status_gpio) & !(flags & OF_GPIO_ACTIVE_LOW))
1433 pdata->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
Sahitya Tummala581df132013-03-12 14:57:46 +05301434
Asutosh Das0ef24812012-12-18 16:14:02 +05301435 of_property_read_u32(np, "qcom,bus-width", &bus_width);
1436 if (bus_width == 8)
1437 pdata->mmc_bus_width = MMC_CAP_8_BIT_DATA;
1438 else if (bus_width == 4)
1439 pdata->mmc_bus_width = MMC_CAP_4_BIT_DATA;
1440 else {
1441 dev_notice(dev, "invalid bus-width, default to 1-bit mode\n");
1442 pdata->mmc_bus_width = 0;
1443 }
1444
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05301445 if (!of_property_read_u32(np, "qcom,cpu-dma-latency-us",
1446 &cpu_dma_latency))
1447 pdata->cpu_dma_latency_us = cpu_dma_latency;
Asutosh Das598a4d42014-02-05 16:38:23 +05301448 else
1449 pdata->cpu_dma_latency_us = MSM_MMC_DEFAULT_CPU_DMA_LATENCY;
Talel Shenhar7dc5f792015-05-18 12:12:48 +03001450
1451 if (sdhci_msm_dt_get_array(dev, "qcom,devfreq,freq-table",
1452 &msm_host->mmc->clk_scaling.freq_table,
1453 &msm_host->mmc->clk_scaling.freq_table_sz, 0))
1454 pr_debug("%s: no clock scaling frequencies were supplied\n",
1455 dev_name(dev));
1456 else if (!msm_host->mmc->clk_scaling.freq_table ||
1457 !msm_host->mmc->clk_scaling.freq_table_sz)
1458 dev_err(dev, "bad dts clock scaling frequencies\n");
1459
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301460 if (sdhci_msm_dt_get_array(dev, "qcom,clk-rates",
1461 &clk_table, &clk_table_len, 0)) {
1462 dev_err(dev, "failed parsing supported clock rates\n");
1463 goto out;
1464 }
1465 if (!clk_table || !clk_table_len) {
1466 dev_err(dev, "Invalid clock table\n");
1467 goto out;
1468 }
1469 pdata->sup_clk_table = clk_table;
1470 pdata->sup_clk_cnt = clk_table_len;
1471
Asutosh Das0ef24812012-12-18 16:14:02 +05301472 pdata->vreg_data = devm_kzalloc(dev, sizeof(struct
1473 sdhci_msm_slot_reg_data),
1474 GFP_KERNEL);
1475 if (!pdata->vreg_data) {
1476 dev_err(dev, "failed to allocate memory for vreg data\n");
1477 goto out;
1478 }
1479
1480 if (sdhci_msm_dt_parse_vreg_info(dev, &pdata->vreg_data->vdd_data,
1481 "vdd")) {
1482 dev_err(dev, "failed parsing vdd data\n");
1483 goto out;
1484 }
1485 if (sdhci_msm_dt_parse_vreg_info(dev,
1486 &pdata->vreg_data->vdd_io_data,
1487 "vdd-io")) {
1488 dev_err(dev, "failed parsing vdd-io data\n");
1489 goto out;
1490 }
1491
1492 if (sdhci_msm_dt_parse_gpio_info(dev, pdata)) {
1493 dev_err(dev, "failed parsing gpio data\n");
1494 goto out;
1495 }
1496
Asutosh Das0ef24812012-12-18 16:14:02 +05301497 len = of_property_count_strings(np, "qcom,bus-speed-mode");
1498
1499 for (i = 0; i < len; i++) {
1500 const char *name = NULL;
1501
1502 of_property_read_string_index(np,
1503 "qcom,bus-speed-mode", i, &name);
1504 if (!name)
1505 continue;
1506
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07001507 if (!strncmp(name, "HS400_1p8v", sizeof("HS400_1p8v")))
1508 pdata->caps2 |= MMC_CAP2_HS400_1_8V;
1509 else if (!strncmp(name, "HS400_1p2v", sizeof("HS400_1p2v")))
1510 pdata->caps2 |= MMC_CAP2_HS400_1_2V;
1511 else if (!strncmp(name, "HS200_1p8v", sizeof("HS200_1p8v")))
Asutosh Das0ef24812012-12-18 16:14:02 +05301512 pdata->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
1513 else if (!strncmp(name, "HS200_1p2v", sizeof("HS200_1p2v")))
1514 pdata->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
1515 else if (!strncmp(name, "DDR_1p8v", sizeof("DDR_1p8v")))
1516 pdata->caps |= MMC_CAP_1_8V_DDR
1517 | MMC_CAP_UHS_DDR50;
1518 else if (!strncmp(name, "DDR_1p2v", sizeof("DDR_1p2v")))
1519 pdata->caps |= MMC_CAP_1_2V_DDR
1520 | MMC_CAP_UHS_DDR50;
1521 }
1522
1523 if (of_get_property(np, "qcom,nonremovable", NULL))
1524 pdata->nonremovable = true;
1525
Guoping Yuf7c91332014-08-20 16:56:18 +08001526 if (of_get_property(np, "qcom,nonhotplug", NULL))
1527 pdata->nonhotplug = true;
1528
Venkat Gopalakrishnan9a62e042015-03-03 16:14:55 -08001529 pdata->largeaddressbus =
1530 of_property_read_bool(np, "qcom,large-address-bus");
1531
Maya Erez994cf2f2014-10-21 20:22:04 +03001532 sdhci_msm_populate_affinity_type(pdata, np);
1533
Dov Levenglickc9033ab2015-03-10 16:00:56 +02001534 if (of_property_read_bool(np, "qcom,wakeup-on-idle"))
1535 msm_host->mmc->wakeup_on_idle = true;
1536
Asutosh Das0ef24812012-12-18 16:14:02 +05301537 return pdata;
1538out:
1539 return NULL;
1540}
1541
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301542/* Returns required bandwidth in Bytes per Sec */
1543static unsigned int sdhci_get_bw_required(struct sdhci_host *host,
1544 struct mmc_ios *ios)
1545{
Sahitya Tummala2886c922013-04-03 18:03:31 +05301546 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1547 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1548
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301549 unsigned int bw;
1550
Sahitya Tummala2886c922013-04-03 18:03:31 +05301551 bw = msm_host->clk_rate;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301552 /*
1553 * For DDR mode, SDCC controller clock will be at
1554 * the double rate than the actual clock that goes to card.
1555 */
1556 if (ios->bus_width == MMC_BUS_WIDTH_4)
1557 bw /= 2;
1558 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1559 bw /= 8;
1560
1561 return bw;
1562}
1563
1564static int sdhci_msm_bus_get_vote_for_bw(struct sdhci_msm_host *host,
1565 unsigned int bw)
1566{
1567 unsigned int *table = host->pdata->voting_data->bw_vecs;
1568 unsigned int size = host->pdata->voting_data->bw_vecs_size;
1569 int i;
1570
1571 if (host->msm_bus_vote.is_max_bw_needed && bw)
1572 return host->msm_bus_vote.max_bw_vote;
1573
1574 for (i = 0; i < size; i++) {
1575 if (bw <= table[i])
1576 break;
1577 }
1578
1579 if (i && (i == size))
1580 i--;
1581
1582 return i;
1583}
1584
1585/*
1586 * This function must be called with host lock acquired.
1587 * Caller of this function should also ensure that msm bus client
1588 * handle is not null.
1589 */
1590static inline int sdhci_msm_bus_set_vote(struct sdhci_msm_host *msm_host,
1591 int vote,
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301592 unsigned long *flags)
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301593{
1594 struct sdhci_host *host = platform_get_drvdata(msm_host->pdev);
1595 int rc = 0;
1596
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301597 BUG_ON(!flags);
1598
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301599 if (vote != msm_host->msm_bus_vote.curr_vote) {
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301600 spin_unlock_irqrestore(&host->lock, *flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301601 rc = msm_bus_scale_client_update_request(
1602 msm_host->msm_bus_vote.client_handle, vote);
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301603 spin_lock_irqsave(&host->lock, *flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301604 if (rc) {
1605 pr_err("%s: msm_bus_scale_client_update_request() failed: bus_client_handle=0x%x, vote=%d, err=%d\n",
1606 mmc_hostname(host->mmc),
1607 msm_host->msm_bus_vote.client_handle, vote, rc);
1608 goto out;
1609 }
1610 msm_host->msm_bus_vote.curr_vote = vote;
1611 }
1612out:
1613 return rc;
1614}
1615
1616/*
1617 * Internal work. Work to set 0 bandwidth for msm bus.
1618 */
1619static void sdhci_msm_bus_work(struct work_struct *work)
1620{
1621 struct sdhci_msm_host *msm_host;
1622 struct sdhci_host *host;
1623 unsigned long flags;
1624
1625 msm_host = container_of(work, struct sdhci_msm_host,
1626 msm_bus_vote.vote_work.work);
1627 host = platform_get_drvdata(msm_host->pdev);
1628
1629 if (!msm_host->msm_bus_vote.client_handle)
1630 return;
1631
1632 spin_lock_irqsave(&host->lock, flags);
1633 /* don't vote for 0 bandwidth if any request is in progress */
1634 if (!host->mrq) {
1635 sdhci_msm_bus_set_vote(msm_host,
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301636 msm_host->msm_bus_vote.min_bw_vote, &flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301637 } else
1638 pr_warning("%s: %s: Transfer in progress. skipping bus voting to 0 bandwidth\n",
1639 mmc_hostname(host->mmc), __func__);
1640 spin_unlock_irqrestore(&host->lock, flags);
1641}
1642
1643/*
1644 * This function cancels any scheduled delayed work and sets the bus
1645 * vote based on bw (bandwidth) argument.
1646 */
1647static void sdhci_msm_bus_cancel_work_and_set_vote(struct sdhci_host *host,
1648 unsigned int bw)
1649{
1650 int vote;
1651 unsigned long flags;
1652 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1653 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1654
1655 cancel_delayed_work_sync(&msm_host->msm_bus_vote.vote_work);
1656 spin_lock_irqsave(&host->lock, flags);
1657 vote = sdhci_msm_bus_get_vote_for_bw(msm_host, bw);
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301658 sdhci_msm_bus_set_vote(msm_host, vote, &flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301659 spin_unlock_irqrestore(&host->lock, flags);
1660}
1661
1662#define MSM_MMC_BUS_VOTING_DELAY 200 /* msecs */
1663
1664/* This function queues a work which will set the bandwidth requiement to 0 */
1665static void sdhci_msm_bus_queue_work(struct sdhci_host *host)
1666{
1667 unsigned long flags;
1668 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1669 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1670
1671 spin_lock_irqsave(&host->lock, flags);
1672 if (msm_host->msm_bus_vote.min_bw_vote !=
1673 msm_host->msm_bus_vote.curr_vote)
1674 queue_delayed_work(system_wq,
1675 &msm_host->msm_bus_vote.vote_work,
1676 msecs_to_jiffies(MSM_MMC_BUS_VOTING_DELAY));
1677 spin_unlock_irqrestore(&host->lock, flags);
1678}
1679
1680static int sdhci_msm_bus_register(struct sdhci_msm_host *host,
1681 struct platform_device *pdev)
1682{
1683 int rc = 0;
1684 struct msm_bus_scale_pdata *bus_pdata;
1685
1686 struct sdhci_msm_bus_voting_data *data;
1687 struct device *dev = &pdev->dev;
1688
1689 data = devm_kzalloc(dev,
1690 sizeof(struct sdhci_msm_bus_voting_data), GFP_KERNEL);
1691 if (!data) {
1692 dev_err(&pdev->dev,
1693 "%s: failed to allocate memory\n", __func__);
1694 rc = -ENOMEM;
1695 goto out;
1696 }
1697 data->bus_pdata = msm_bus_cl_get_pdata(pdev);
1698 if (data->bus_pdata) {
1699 rc = sdhci_msm_dt_get_array(dev, "qcom,bus-bw-vectors-bps",
1700 &data->bw_vecs, &data->bw_vecs_size, 0);
1701 if (rc) {
1702 dev_err(&pdev->dev,
1703 "%s: Failed to get bus-bw-vectors-bps\n",
1704 __func__);
1705 goto out;
1706 }
1707 host->pdata->voting_data = data;
1708 }
1709 if (host->pdata->voting_data &&
1710 host->pdata->voting_data->bus_pdata &&
1711 host->pdata->voting_data->bw_vecs &&
1712 host->pdata->voting_data->bw_vecs_size) {
1713
1714 bus_pdata = host->pdata->voting_data->bus_pdata;
1715 host->msm_bus_vote.client_handle =
1716 msm_bus_scale_register_client(bus_pdata);
1717 if (!host->msm_bus_vote.client_handle) {
1718 dev_err(&pdev->dev, "msm_bus_scale_register_client()\n");
1719 rc = -EFAULT;
1720 goto out;
1721 }
1722 /* cache the vote index for minimum and maximum bandwidth */
1723 host->msm_bus_vote.min_bw_vote =
1724 sdhci_msm_bus_get_vote_for_bw(host, 0);
1725 host->msm_bus_vote.max_bw_vote =
1726 sdhci_msm_bus_get_vote_for_bw(host, UINT_MAX);
1727 } else {
1728 devm_kfree(dev, data);
1729 }
1730
1731out:
1732 return rc;
1733}
1734
1735static void sdhci_msm_bus_unregister(struct sdhci_msm_host *host)
1736{
1737 if (host->msm_bus_vote.client_handle)
1738 msm_bus_scale_unregister_client(
1739 host->msm_bus_vote.client_handle);
1740}
1741
1742static void sdhci_msm_bus_voting(struct sdhci_host *host, u32 enable)
1743{
1744 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1745 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1746 struct mmc_ios *ios = &host->mmc->ios;
1747 unsigned int bw;
1748
1749 if (!msm_host->msm_bus_vote.client_handle)
1750 return;
1751
1752 bw = sdhci_get_bw_required(host, ios);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301753 if (enable) {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301754 sdhci_msm_bus_cancel_work_and_set_vote(host, bw);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301755 } else {
1756 /*
1757 * If clock gating is enabled, then remove the vote
1758 * immediately because clocks will be disabled only
1759 * after SDHCI_MSM_MMC_CLK_GATE_DELAY and thus no
1760 * additional delay is required to remove the bus vote.
1761 */
1762#ifdef CONFIG_MMC_CLKGATE
1763 if (host->mmc->clkgate_delay)
1764 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
1765 else
1766#endif
1767 sdhci_msm_bus_queue_work(host);
1768 }
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301769}
1770
Asutosh Das0ef24812012-12-18 16:14:02 +05301771/* Regulator utility functions */
1772static int sdhci_msm_vreg_init_reg(struct device *dev,
1773 struct sdhci_msm_reg_data *vreg)
1774{
1775 int ret = 0;
1776
1777 /* check if regulator is already initialized? */
1778 if (vreg->reg)
1779 goto out;
1780
1781 /* Get the regulator handle */
1782 vreg->reg = devm_regulator_get(dev, vreg->name);
1783 if (IS_ERR(vreg->reg)) {
1784 ret = PTR_ERR(vreg->reg);
1785 pr_err("%s: devm_regulator_get(%s) failed. ret=%d\n",
1786 __func__, vreg->name, ret);
1787 goto out;
1788 }
1789
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301790 if (regulator_count_voltages(vreg->reg) > 0) {
1791 vreg->set_voltage_sup = true;
1792 /* sanity check */
1793 if (!vreg->high_vol_level || !vreg->hpm_uA) {
1794 pr_err("%s: %s invalid constraints specified\n",
1795 __func__, vreg->name);
1796 ret = -EINVAL;
1797 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301798 }
1799
1800out:
1801 return ret;
1802}
1803
1804static void sdhci_msm_vreg_deinit_reg(struct sdhci_msm_reg_data *vreg)
1805{
1806 if (vreg->reg)
1807 devm_regulator_put(vreg->reg);
1808}
1809
1810static int sdhci_msm_vreg_set_optimum_mode(struct sdhci_msm_reg_data
1811 *vreg, int uA_load)
1812{
1813 int ret = 0;
1814
1815 /*
1816 * regulators that do not support regulator_set_voltage also
1817 * do not support regulator_set_optimum_mode
1818 */
1819 if (vreg->set_voltage_sup) {
1820 ret = regulator_set_load(vreg->reg, uA_load);
1821 if (ret < 0)
1822 pr_err("%s: regulator_set_load(reg=%s,uA_load=%d) failed. ret=%d\n",
1823 __func__, vreg->name, uA_load, ret);
1824 else
1825 /*
1826 * regulator_set_load() can return non zero
1827 * value even for success case.
1828 */
1829 ret = 0;
1830 }
1831 return ret;
1832}
1833
1834static int sdhci_msm_vreg_set_voltage(struct sdhci_msm_reg_data *vreg,
1835 int min_uV, int max_uV)
1836{
1837 int ret = 0;
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301838 if (vreg->set_voltage_sup) {
1839 ret = regulator_set_voltage(vreg->reg, min_uV, max_uV);
1840 if (ret) {
1841 pr_err("%s: regulator_set_voltage(%s)failed. min_uV=%d,max_uV=%d,ret=%d\n",
Asutosh Das0ef24812012-12-18 16:14:02 +05301842 __func__, vreg->name, min_uV, max_uV, ret);
1843 }
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301844 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301845
1846 return ret;
1847}
1848
1849static int sdhci_msm_vreg_enable(struct sdhci_msm_reg_data *vreg)
1850{
1851 int ret = 0;
1852
1853 /* Put regulator in HPM (high power mode) */
1854 ret = sdhci_msm_vreg_set_optimum_mode(vreg, vreg->hpm_uA);
1855 if (ret < 0)
1856 return ret;
1857
1858 if (!vreg->is_enabled) {
1859 /* Set voltage level */
1860 ret = sdhci_msm_vreg_set_voltage(vreg, vreg->high_vol_level,
1861 vreg->high_vol_level);
1862 if (ret)
1863 return ret;
1864 }
1865 ret = regulator_enable(vreg->reg);
1866 if (ret) {
1867 pr_err("%s: regulator_enable(%s) failed. ret=%d\n",
1868 __func__, vreg->name, ret);
1869 return ret;
1870 }
1871 vreg->is_enabled = true;
1872 return ret;
1873}
1874
1875static int sdhci_msm_vreg_disable(struct sdhci_msm_reg_data *vreg)
1876{
1877 int ret = 0;
1878
1879 /* Never disable regulator marked as always_on */
1880 if (vreg->is_enabled && !vreg->is_always_on) {
1881 ret = regulator_disable(vreg->reg);
1882 if (ret) {
1883 pr_err("%s: regulator_disable(%s) failed. ret=%d\n",
1884 __func__, vreg->name, ret);
1885 goto out;
1886 }
1887 vreg->is_enabled = false;
1888
1889 ret = sdhci_msm_vreg_set_optimum_mode(vreg, 0);
1890 if (ret < 0)
1891 goto out;
1892
1893 /* Set min. voltage level to 0 */
1894 ret = sdhci_msm_vreg_set_voltage(vreg, 0, vreg->high_vol_level);
1895 if (ret)
1896 goto out;
1897 } else if (vreg->is_enabled && vreg->is_always_on) {
1898 if (vreg->lpm_sup) {
1899 /* Put always_on regulator in LPM (low power mode) */
1900 ret = sdhci_msm_vreg_set_optimum_mode(vreg,
1901 vreg->lpm_uA);
1902 if (ret < 0)
1903 goto out;
1904 }
1905 }
1906out:
1907 return ret;
1908}
1909
1910static int sdhci_msm_setup_vreg(struct sdhci_msm_pltfm_data *pdata,
1911 bool enable, bool is_init)
1912{
1913 int ret = 0, i;
1914 struct sdhci_msm_slot_reg_data *curr_slot;
1915 struct sdhci_msm_reg_data *vreg_table[2];
1916
1917 curr_slot = pdata->vreg_data;
1918 if (!curr_slot) {
1919 pr_debug("%s: vreg info unavailable,assuming the slot is powered by always on domain\n",
1920 __func__);
1921 goto out;
1922 }
1923
1924 vreg_table[0] = curr_slot->vdd_data;
1925 vreg_table[1] = curr_slot->vdd_io_data;
1926
1927 for (i = 0; i < ARRAY_SIZE(vreg_table); i++) {
1928 if (vreg_table[i]) {
1929 if (enable)
1930 ret = sdhci_msm_vreg_enable(vreg_table[i]);
1931 else
1932 ret = sdhci_msm_vreg_disable(vreg_table[i]);
1933 if (ret)
1934 goto out;
1935 }
1936 }
1937out:
1938 return ret;
1939}
1940
1941/*
1942 * Reset vreg by ensuring it is off during probe. A call
1943 * to enable vreg is needed to balance disable vreg
1944 */
1945static int sdhci_msm_vreg_reset(struct sdhci_msm_pltfm_data *pdata)
1946{
1947 int ret;
1948
1949 ret = sdhci_msm_setup_vreg(pdata, 1, true);
1950 if (ret)
1951 return ret;
1952 ret = sdhci_msm_setup_vreg(pdata, 0, true);
1953 return ret;
1954}
1955
1956/* This init function should be called only once for each SDHC slot */
1957static int sdhci_msm_vreg_init(struct device *dev,
1958 struct sdhci_msm_pltfm_data *pdata,
1959 bool is_init)
1960{
1961 int ret = 0;
1962 struct sdhci_msm_slot_reg_data *curr_slot;
1963 struct sdhci_msm_reg_data *curr_vdd_reg, *curr_vdd_io_reg;
1964
1965 curr_slot = pdata->vreg_data;
1966 if (!curr_slot)
1967 goto out;
1968
1969 curr_vdd_reg = curr_slot->vdd_data;
1970 curr_vdd_io_reg = curr_slot->vdd_io_data;
1971
1972 if (!is_init)
1973 /* Deregister all regulators from regulator framework */
1974 goto vdd_io_reg_deinit;
1975
1976 /*
1977 * Get the regulator handle from voltage regulator framework
1978 * and then try to set the voltage level for the regulator
1979 */
1980 if (curr_vdd_reg) {
1981 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_reg);
1982 if (ret)
1983 goto out;
1984 }
1985 if (curr_vdd_io_reg) {
1986 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_io_reg);
1987 if (ret)
1988 goto vdd_reg_deinit;
1989 }
1990 ret = sdhci_msm_vreg_reset(pdata);
1991 if (ret)
1992 dev_err(dev, "vreg reset failed (%d)\n", ret);
1993 goto out;
1994
1995vdd_io_reg_deinit:
1996 if (curr_vdd_io_reg)
1997 sdhci_msm_vreg_deinit_reg(curr_vdd_io_reg);
1998vdd_reg_deinit:
1999 if (curr_vdd_reg)
2000 sdhci_msm_vreg_deinit_reg(curr_vdd_reg);
2001out:
2002 return ret;
2003}
2004
2005
2006static int sdhci_msm_set_vdd_io_vol(struct sdhci_msm_pltfm_data *pdata,
2007 enum vdd_io_level level,
2008 unsigned int voltage_level)
2009{
2010 int ret = 0;
2011 int set_level;
2012 struct sdhci_msm_reg_data *vdd_io_reg;
2013
2014 if (!pdata->vreg_data)
2015 return ret;
2016
2017 vdd_io_reg = pdata->vreg_data->vdd_io_data;
2018 if (vdd_io_reg && vdd_io_reg->is_enabled) {
2019 switch (level) {
2020 case VDD_IO_LOW:
2021 set_level = vdd_io_reg->low_vol_level;
2022 break;
2023 case VDD_IO_HIGH:
2024 set_level = vdd_io_reg->high_vol_level;
2025 break;
2026 case VDD_IO_SET_LEVEL:
2027 set_level = voltage_level;
2028 break;
2029 default:
2030 pr_err("%s: invalid argument level = %d",
2031 __func__, level);
2032 ret = -EINVAL;
2033 return ret;
2034 }
2035 ret = sdhci_msm_vreg_set_voltage(vdd_io_reg, set_level,
2036 set_level);
2037 }
2038 return ret;
2039}
2040
2041static irqreturn_t sdhci_msm_pwr_irq(int irq, void *data)
2042{
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002043 struct sdhci_host *host = (struct sdhci_host *)data;
2044 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2045 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Das0ef24812012-12-18 16:14:02 +05302046 u8 irq_status = 0;
2047 u8 irq_ack = 0;
2048 int ret = 0;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302049 int pwr_state = 0, io_level = 0;
2050 unsigned long flags;
Asutosh Das0ef24812012-12-18 16:14:02 +05302051
2052 irq_status = readb_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
2053 pr_debug("%s: Received IRQ(%d), status=0x%x\n",
2054 mmc_hostname(msm_host->mmc), irq, irq_status);
2055
2056 /* Clear the interrupt */
2057 writeb_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
2058 /*
2059 * SDHC has core_mem and hc_mem device memory and these memory
2060 * addresses do not fall within 1KB region. Hence, any update to
2061 * core_mem address space would require an mb() to ensure this gets
2062 * completed before its next update to registers within hc_mem.
2063 */
2064 mb();
2065
2066 /* Handle BUS ON/OFF*/
2067 if (irq_status & CORE_PWRCTL_BUS_ON) {
2068 ret = sdhci_msm_setup_vreg(msm_host->pdata, true, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302069 if (!ret) {
Asutosh Das0ef24812012-12-18 16:14:02 +05302070 ret = sdhci_msm_setup_pins(msm_host->pdata, true);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302071 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
2072 VDD_IO_HIGH, 0);
2073 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302074 if (ret)
2075 irq_ack |= CORE_PWRCTL_BUS_FAIL;
2076 else
2077 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302078
2079 pwr_state = REQ_BUS_ON;
2080 io_level = REQ_IO_HIGH;
Asutosh Das0ef24812012-12-18 16:14:02 +05302081 }
2082 if (irq_status & CORE_PWRCTL_BUS_OFF) {
2083 ret = sdhci_msm_setup_vreg(msm_host->pdata, false, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302084 if (!ret) {
Asutosh Das0ef24812012-12-18 16:14:02 +05302085 ret = sdhci_msm_setup_pins(msm_host->pdata, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302086 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
2087 VDD_IO_LOW, 0);
2088 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302089 if (ret)
2090 irq_ack |= CORE_PWRCTL_BUS_FAIL;
2091 else
2092 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302093
2094 pwr_state = REQ_BUS_OFF;
2095 io_level = REQ_IO_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05302096 }
2097 /* Handle IO LOW/HIGH */
2098 if (irq_status & CORE_PWRCTL_IO_LOW) {
2099 /* Switch voltage Low */
2100 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_LOW, 0);
2101 if (ret)
2102 irq_ack |= CORE_PWRCTL_IO_FAIL;
2103 else
2104 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302105
2106 io_level = REQ_IO_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05302107 }
2108 if (irq_status & CORE_PWRCTL_IO_HIGH) {
2109 /* Switch voltage High */
2110 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_HIGH, 0);
2111 if (ret)
2112 irq_ack |= CORE_PWRCTL_IO_FAIL;
2113 else
2114 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302115
2116 io_level = REQ_IO_HIGH;
Asutosh Das0ef24812012-12-18 16:14:02 +05302117 }
2118
2119 /* ACK status to the core */
2120 writeb_relaxed(irq_ack, (msm_host->core_mem + CORE_PWRCTL_CTL));
2121 /*
2122 * SDHC has core_mem and hc_mem device memory and these memory
2123 * addresses do not fall within 1KB region. Hence, any update to
2124 * core_mem address space would require an mb() to ensure this gets
2125 * completed before its next update to registers within hc_mem.
2126 */
2127 mb();
2128
Krishna Konda46fd1432014-10-30 21:13:27 -07002129 if ((io_level & REQ_IO_HIGH) && (msm_host->caps_0 & CORE_3_0V_SUPPORT))
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002130 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2131 ~CORE_IO_PAD_PWR_SWITCH),
2132 host->ioaddr + CORE_VENDOR_SPEC);
Krishna Konda46fd1432014-10-30 21:13:27 -07002133 else if ((io_level & REQ_IO_LOW) ||
2134 (msm_host->caps_0 & CORE_1_8V_SUPPORT))
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002135 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
2136 CORE_IO_PAD_PWR_SWITCH),
2137 host->ioaddr + CORE_VENDOR_SPEC);
2138 mb();
2139
Asutosh Das0ef24812012-12-18 16:14:02 +05302140 pr_debug("%s: Handled IRQ(%d), ret=%d, ack=0x%x\n",
2141 mmc_hostname(msm_host->mmc), irq, ret, irq_ack);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302142 spin_lock_irqsave(&host->lock, flags);
2143 if (pwr_state)
2144 msm_host->curr_pwr_state = pwr_state;
2145 if (io_level)
2146 msm_host->curr_io_level = io_level;
2147 complete(&msm_host->pwr_irq_completion);
2148 spin_unlock_irqrestore(&host->lock, flags);
2149
Asutosh Das0ef24812012-12-18 16:14:02 +05302150 return IRQ_HANDLED;
2151}
2152
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302153static ssize_t
Sahitya Tummala5c55b932013-06-20 14:00:18 +05302154show_polling(struct device *dev, struct device_attribute *attr, char *buf)
2155{
2156 struct sdhci_host *host = dev_get_drvdata(dev);
2157 int poll;
2158 unsigned long flags;
2159
2160 spin_lock_irqsave(&host->lock, flags);
2161 poll = !!(host->mmc->caps & MMC_CAP_NEEDS_POLL);
2162 spin_unlock_irqrestore(&host->lock, flags);
2163
2164 return snprintf(buf, PAGE_SIZE, "%d\n", poll);
2165}
2166
2167static ssize_t
2168store_polling(struct device *dev, struct device_attribute *attr,
2169 const char *buf, size_t count)
2170{
2171 struct sdhci_host *host = dev_get_drvdata(dev);
2172 int value;
2173 unsigned long flags;
2174
2175 if (!kstrtou32(buf, 0, &value)) {
2176 spin_lock_irqsave(&host->lock, flags);
2177 if (value) {
2178 host->mmc->caps |= MMC_CAP_NEEDS_POLL;
2179 mmc_detect_change(host->mmc, 0);
2180 } else {
2181 host->mmc->caps &= ~MMC_CAP_NEEDS_POLL;
2182 }
2183 spin_unlock_irqrestore(&host->lock, flags);
2184 }
2185 return count;
2186}
2187
2188static ssize_t
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302189show_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
2190 char *buf)
2191{
2192 struct sdhci_host *host = dev_get_drvdata(dev);
2193 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2194 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2195
2196 return snprintf(buf, PAGE_SIZE, "%u\n",
2197 msm_host->msm_bus_vote.is_max_bw_needed);
2198}
2199
2200static ssize_t
2201store_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
2202 const char *buf, size_t count)
2203{
2204 struct sdhci_host *host = dev_get_drvdata(dev);
2205 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2206 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2207 uint32_t value;
2208 unsigned long flags;
2209
2210 if (!kstrtou32(buf, 0, &value)) {
2211 spin_lock_irqsave(&host->lock, flags);
2212 msm_host->msm_bus_vote.is_max_bw_needed = !!value;
2213 spin_unlock_irqrestore(&host->lock, flags);
2214 }
2215 return count;
2216}
2217
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302218static void sdhci_msm_check_power_status(struct sdhci_host *host, u32 req_type)
Asutosh Das0ef24812012-12-18 16:14:02 +05302219{
2220 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2221 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302222 unsigned long flags;
2223 bool done = false;
Sahitya Tummala481fbb02013-08-06 15:22:28 +05302224 u32 io_sig_sts;
Asutosh Das0ef24812012-12-18 16:14:02 +05302225
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302226 spin_lock_irqsave(&host->lock, flags);
2227 pr_debug("%s: %s: request %d curr_pwr_state %x curr_io_level %x\n",
2228 mmc_hostname(host->mmc), __func__, req_type,
2229 msm_host->curr_pwr_state, msm_host->curr_io_level);
Sahitya Tummala481fbb02013-08-06 15:22:28 +05302230 io_sig_sts = readl_relaxed(msm_host->core_mem + CORE_GENERICS);
2231 /*
2232 * The IRQ for request type IO High/Low will be generated when -
2233 * 1. SWITCHABLE_SIGNALLING_VOL is enabled in HW.
2234 * 2. If 1 is true and when there is a state change in 1.8V enable
2235 * bit (bit 3) of SDHCI_HOST_CONTROL2 register. The reset state of
2236 * that bit is 0 which indicates 3.3V IO voltage. So, when MMC core
2237 * layer tries to set it to 3.3V before card detection happens, the
2238 * IRQ doesn't get triggered as there is no state change in this bit.
2239 * The driver already handles this case by changing the IO voltage
2240 * level to high as part of controller power up sequence. Hence, check
2241 * for host->pwr to handle a case where IO voltage high request is
2242 * issued even before controller power up.
2243 */
2244 if (req_type & (REQ_IO_HIGH | REQ_IO_LOW)) {
2245 if (!(io_sig_sts & SWITCHABLE_SIGNALLING_VOL) ||
2246 ((req_type & REQ_IO_HIGH) && !host->pwr)) {
2247 pr_debug("%s: do not wait for power IRQ that never comes\n",
2248 mmc_hostname(host->mmc));
2249 spin_unlock_irqrestore(&host->lock, flags);
2250 return;
2251 }
2252 }
2253
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302254 if ((req_type & msm_host->curr_pwr_state) ||
2255 (req_type & msm_host->curr_io_level))
2256 done = true;
2257 spin_unlock_irqrestore(&host->lock, flags);
Asutosh Das0ef24812012-12-18 16:14:02 +05302258
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302259 /*
2260 * This is needed here to hanlde a case where IRQ gets
2261 * triggered even before this function is called so that
2262 * x->done counter of completion gets reset. Otherwise,
2263 * next call to wait_for_completion returns immediately
2264 * without actually waiting for the IRQ to be handled.
2265 */
2266 if (done)
2267 init_completion(&msm_host->pwr_irq_completion);
2268 else
2269 wait_for_completion(&msm_host->pwr_irq_completion);
2270
2271 pr_debug("%s: %s: request %d done\n", mmc_hostname(host->mmc),
2272 __func__, req_type);
Asutosh Das0ef24812012-12-18 16:14:02 +05302273}
2274
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002275static void sdhci_msm_toggle_cdr(struct sdhci_host *host, bool enable)
2276{
2277 if (enable)
2278 writel_relaxed((readl_relaxed(host->ioaddr +
2279 CORE_DLL_CONFIG) | CORE_CDR_EN),
2280 host->ioaddr + CORE_DLL_CONFIG);
2281 else
2282 writel_relaxed((readl_relaxed(host->ioaddr +
2283 CORE_DLL_CONFIG) & ~CORE_CDR_EN),
2284 host->ioaddr + CORE_DLL_CONFIG);
2285}
2286
Asutosh Das648f9d12013-01-10 21:11:04 +05302287static unsigned int sdhci_msm_max_segs(void)
2288{
2289 return SDHCI_MSM_MAX_SEGMENTS;
2290}
2291
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302292static unsigned int sdhci_msm_get_min_clock(struct sdhci_host *host)
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302293{
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302294 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2295 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302296
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302297 return msm_host->pdata->sup_clk_table[0];
2298}
2299
2300static unsigned int sdhci_msm_get_max_clock(struct sdhci_host *host)
2301{
2302 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2303 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2304 int max_clk_index = msm_host->pdata->sup_clk_cnt;
2305
2306 return msm_host->pdata->sup_clk_table[max_clk_index - 1];
2307}
2308
2309static unsigned int sdhci_msm_get_sup_clk_rate(struct sdhci_host *host,
2310 u32 req_clk)
2311{
2312 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2313 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2314 unsigned int sel_clk = -1;
2315 unsigned char cnt;
2316
2317 if (req_clk < sdhci_msm_get_min_clock(host)) {
2318 sel_clk = sdhci_msm_get_min_clock(host);
2319 return sel_clk;
2320 }
2321
2322 for (cnt = 0; cnt < msm_host->pdata->sup_clk_cnt; cnt++) {
2323 if (msm_host->pdata->sup_clk_table[cnt] > req_clk) {
2324 break;
2325 } else if (msm_host->pdata->sup_clk_table[cnt] == req_clk) {
2326 sel_clk = msm_host->pdata->sup_clk_table[cnt];
2327 break;
2328 } else {
2329 sel_clk = msm_host->pdata->sup_clk_table[cnt];
2330 }
2331 }
2332 return sel_clk;
2333}
2334
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302335static int sdhci_msm_enable_controller_clock(struct sdhci_host *host)
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 (atomic_read(&msm_host->controller_clock))
2342 return 0;
2343
2344 sdhci_msm_bus_voting(host, 1);
2345
2346 if (!IS_ERR(msm_host->pclk)) {
2347 rc = clk_prepare_enable(msm_host->pclk);
2348 if (rc) {
2349 pr_err("%s: %s: failed to enable the pclk with error %d\n",
2350 mmc_hostname(host->mmc), __func__, rc);
2351 goto remove_vote;
2352 }
2353 }
2354
2355 rc = clk_prepare_enable(msm_host->clk);
2356 if (rc) {
2357 pr_err("%s: %s: failed to enable the host-clk with error %d\n",
2358 mmc_hostname(host->mmc), __func__, rc);
2359 goto disable_pclk;
2360 }
2361
2362 atomic_set(&msm_host->controller_clock, 1);
2363 pr_debug("%s: %s: enabled controller clock\n",
2364 mmc_hostname(host->mmc), __func__);
2365 goto out;
2366
2367disable_pclk:
2368 if (!IS_ERR(msm_host->pclk))
2369 clk_disable_unprepare(msm_host->pclk);
2370remove_vote:
2371 if (msm_host->msm_bus_vote.client_handle)
2372 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2373out:
2374 return rc;
2375}
2376
2377
2378
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302379static int sdhci_msm_prepare_clocks(struct sdhci_host *host, bool enable)
2380{
2381 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2382 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2383 int rc = 0;
2384
2385 if (enable && !atomic_read(&msm_host->clks_on)) {
2386 pr_debug("%s: request to enable clocks\n",
2387 mmc_hostname(host->mmc));
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302388
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302389 /*
2390 * The bus-width or the clock rate might have changed
2391 * after controller clocks are enbaled, update bus vote
2392 * in such case.
2393 */
2394 if (atomic_read(&msm_host->controller_clock))
2395 sdhci_msm_bus_voting(host, 1);
2396
2397 rc = sdhci_msm_enable_controller_clock(host);
2398 if (rc)
2399 goto remove_vote;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302400
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302401 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
2402 rc = clk_prepare_enable(msm_host->bus_clk);
2403 if (rc) {
2404 pr_err("%s: %s: failed to enable the bus-clock with error %d\n",
2405 mmc_hostname(host->mmc), __func__, rc);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302406 goto disable_controller_clk;
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302407 }
2408 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002409 if (!IS_ERR(msm_host->ff_clk)) {
2410 rc = clk_prepare_enable(msm_host->ff_clk);
2411 if (rc) {
2412 pr_err("%s: %s: failed to enable the ff_clk with error %d\n",
2413 mmc_hostname(host->mmc), __func__, rc);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302414 goto disable_bus_clk;
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002415 }
2416 }
2417 if (!IS_ERR(msm_host->sleep_clk)) {
2418 rc = clk_prepare_enable(msm_host->sleep_clk);
2419 if (rc) {
2420 pr_err("%s: %s: failed to enable the sleep_clk with error %d\n",
2421 mmc_hostname(host->mmc), __func__, rc);
2422 goto disable_ff_clk;
2423 }
2424 }
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302425 mb();
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302426
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302427 } else if (!enable && atomic_read(&msm_host->clks_on)) {
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302428 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
2429 mb();
Sahitya Tummaladc182982013-08-20 15:32:09 +05302430 /*
2431 * During 1.8V signal switching the clock source must
2432 * still be ON as it requires accessing SDHC
2433 * registers (SDHCi host control2 register bit 3 must
2434 * be written and polled after stopping the SDCLK).
2435 */
2436 if (host->mmc->card_clock_off)
2437 return 0;
2438 pr_debug("%s: request to disable clocks\n",
2439 mmc_hostname(host->mmc));
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002440 if (!IS_ERR_OR_NULL(msm_host->sleep_clk))
2441 clk_disable_unprepare(msm_host->sleep_clk);
2442 if (!IS_ERR_OR_NULL(msm_host->ff_clk))
2443 clk_disable_unprepare(msm_host->ff_clk);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302444 clk_disable_unprepare(msm_host->clk);
2445 if (!IS_ERR(msm_host->pclk))
2446 clk_disable_unprepare(msm_host->pclk);
2447 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2448 clk_disable_unprepare(msm_host->bus_clk);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302449
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302450 atomic_set(&msm_host->controller_clock, 0);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302451 sdhci_msm_bus_voting(host, 0);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302452 }
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302453 atomic_set(&msm_host->clks_on, enable);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302454 goto out;
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002455disable_ff_clk:
2456 if (!IS_ERR_OR_NULL(msm_host->ff_clk))
2457 clk_disable_unprepare(msm_host->ff_clk);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302458disable_bus_clk:
2459 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2460 clk_disable_unprepare(msm_host->bus_clk);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302461disable_controller_clk:
2462 if (!IS_ERR_OR_NULL(msm_host->clk))
2463 clk_disable_unprepare(msm_host->clk);
2464 if (!IS_ERR_OR_NULL(msm_host->pclk))
2465 clk_disable_unprepare(msm_host->pclk);
2466 atomic_set(&msm_host->controller_clock, 0);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302467remove_vote:
2468 if (msm_host->msm_bus_vote.client_handle)
2469 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302470out:
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302471 return rc;
2472}
2473
2474static void sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock)
2475{
2476 int rc;
2477 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2478 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2479 struct mmc_ios curr_ios = host->mmc->ios;
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002480 u32 sup_clock, ddr_clock, dll_lock;
Sahitya Tummala043744a2013-06-24 09:55:33 +05302481 bool curr_pwrsave;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302482
2483 if (!clock) {
Sujit Reddy Thummabf1aecc2014-01-10 10:58:54 +05302484 /*
2485 * disable pwrsave to ensure clock is not auto-gated until
2486 * the rate is >400KHz (initialization complete).
2487 */
2488 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2489 ~CORE_CLK_PWRSAVE, host->ioaddr + CORE_VENDOR_SPEC);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302490 sdhci_msm_prepare_clocks(host, false);
2491 host->clock = clock;
2492 goto out;
2493 }
2494
2495 rc = sdhci_msm_prepare_clocks(host, true);
2496 if (rc)
2497 goto out;
2498
Sahitya Tummala043744a2013-06-24 09:55:33 +05302499 curr_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2500 CORE_CLK_PWRSAVE);
Sahitya Tummalae000b242013-08-29 16:21:08 +05302501 if ((clock > 400000) &&
Venkat Gopalakrishnanc0a367272015-02-24 13:09:09 -08002502 !curr_pwrsave && mmc_host_may_gate_card(host->mmc->card))
Sahitya Tummala043744a2013-06-24 09:55:33 +05302503 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2504 | CORE_CLK_PWRSAVE,
2505 host->ioaddr + CORE_VENDOR_SPEC);
2506 /*
2507 * Disable pwrsave for a newly added card if doesn't allow clock
2508 * gating.
2509 */
Venkat Gopalakrishnanc0a367272015-02-24 13:09:09 -08002510 else if (curr_pwrsave && !mmc_host_may_gate_card(host->mmc->card))
Sahitya Tummala043744a2013-06-24 09:55:33 +05302511 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2512 & ~CORE_CLK_PWRSAVE,
2513 host->ioaddr + CORE_VENDOR_SPEC);
2514
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302515 sup_clock = sdhci_msm_get_sup_clk_rate(host, clock);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002516 if ((curr_ios.timing == MMC_TIMING_UHS_DDR50) ||
Venkat Gopalakrishnan0a29da92015-01-09 12:19:16 -08002517 (curr_ios.timing == MMC_TIMING_MMC_DDR52) ||
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002518 (curr_ios.timing == MMC_TIMING_MMC_HS400)) {
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302519 /*
2520 * The SDHC requires internal clock frequency to be double the
2521 * actual clock that will be set for DDR mode. The controller
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002522 * uses the faster clock(100/400MHz) for some of its parts and
2523 * send the actual required clock (50/200MHz) to the card.
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302524 */
2525 ddr_clock = clock * 2;
2526 sup_clock = sdhci_msm_get_sup_clk_rate(host,
2527 ddr_clock);
2528 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002529
2530 /*
2531 * In general all timing modes are controlled via UHS mode select in
2532 * Host Control2 register. eMMC specific HS200/HS400 doesn't have
2533 * their respective modes defined here, hence we use these values.
2534 *
2535 * HS200 - SDR104 (Since they both are equivalent in functionality)
2536 * HS400 - This involves multiple configurations
2537 * Initially SDR104 - when tuning is required as HS200
2538 * Then when switching to DDR @ 400MHz (HS400) we use
2539 * the vendor specific HC_SELECT_IN to control the mode.
2540 *
2541 * In addition to controlling the modes we also need to select the
2542 * correct input clock for DLL depending on the mode.
2543 *
2544 * HS400 - divided clock (free running MCLK/2)
2545 * All other modes - default (free running MCLK)
2546 */
2547 if (curr_ios.timing == MMC_TIMING_MMC_HS400) {
2548 /* Select the divided clock (free running MCLK/2) */
2549 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2550 & ~CORE_HC_MCLK_SEL_MASK)
2551 | CORE_HC_MCLK_SEL_HS400),
2552 host->ioaddr + CORE_VENDOR_SPEC);
2553 /*
2554 * Select HS400 mode using the HC_SELECT_IN from VENDOR SPEC
2555 * register
2556 */
Ritesh Harjaniea709662015-05-27 15:40:24 +05302557 if ((msm_host->tuning_done || msm_host->enhanced_strobe) &&
2558 !msm_host->calibration_done) {
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002559 /*
2560 * Write 0x6 to HC_SELECT_IN and 1 to HC_SELECT_IN_EN
2561 * field in VENDOR_SPEC_FUNC
2562 */
2563 writel_relaxed((readl_relaxed(host->ioaddr + \
2564 CORE_VENDOR_SPEC)
2565 | CORE_HC_SELECT_IN_HS400
2566 | CORE_HC_SELECT_IN_EN),
2567 host->ioaddr + CORE_VENDOR_SPEC);
2568 }
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002569 if (!host->mmc->ios.old_rate && !msm_host->use_cdclp533) {
2570 /*
2571 * Poll on DLL_LOCK and DDR_DLL_LOCK bits in
2572 * CORE_DLL_STATUS to be set. This should get set
2573 * with in 15 us at 200 MHz.
2574 */
2575 rc = readl_poll_timeout(host->ioaddr + CORE_DLL_STATUS,
2576 dll_lock, (dll_lock & (CORE_DLL_LOCK |
2577 CORE_DDR_DLL_LOCK)), 10, 1000);
2578 if (rc == -ETIMEDOUT)
2579 pr_err("%s: Unable to get DLL_LOCK/DDR_DLL_LOCK, dll_status: 0x%08x\n",
2580 mmc_hostname(host->mmc),
2581 dll_lock);
2582 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002583 } else {
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002584 if (!msm_host->use_cdclp533)
2585 /* set CORE_PWRSAVE_DLL bit in CORE_VENDOR_SPEC3 */
2586 writel_relaxed((readl_relaxed(host->ioaddr +
2587 CORE_VENDOR_SPEC3) & ~CORE_PWRSAVE_DLL),
2588 host->ioaddr + CORE_VENDOR_SPEC3);
2589
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002590 /* Select the default clock (free running MCLK) */
2591 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2592 & ~CORE_HC_MCLK_SEL_MASK)
2593 | CORE_HC_MCLK_SEL_DFLT),
2594 host->ioaddr + CORE_VENDOR_SPEC);
2595
2596 /*
2597 * Disable HC_SELECT_IN to be able to use the UHS mode select
2598 * configuration from Host Control2 register for all other
2599 * modes.
2600 *
2601 * Write 0 to HC_SELECT_IN and HC_SELECT_IN_EN field
2602 * in VENDOR_SPEC_FUNC
2603 */
2604 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2605 & ~CORE_HC_SELECT_IN_EN
2606 & ~CORE_HC_SELECT_IN_MASK),
2607 host->ioaddr + CORE_VENDOR_SPEC);
2608 }
2609 mb();
2610
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302611 if (sup_clock != msm_host->clk_rate) {
2612 pr_debug("%s: %s: setting clk rate to %u\n",
2613 mmc_hostname(host->mmc), __func__, sup_clock);
2614 rc = clk_set_rate(msm_host->clk, sup_clock);
2615 if (rc) {
2616 pr_err("%s: %s: Failed to set rate %u for host-clk : %d\n",
2617 mmc_hostname(host->mmc), __func__,
2618 sup_clock, rc);
2619 goto out;
2620 }
2621 msm_host->clk_rate = sup_clock;
2622 host->clock = clock;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302623 /*
2624 * Update the bus vote in case of frequency change due to
2625 * clock scaling.
2626 */
2627 sdhci_msm_bus_voting(host, 1);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302628 }
2629out:
2630 sdhci_set_clock(host, clock);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302631}
2632
Sahitya Tummala14613432013-03-21 11:13:25 +05302633static void sdhci_msm_set_uhs_signaling(struct sdhci_host *host,
2634 unsigned int uhs)
2635{
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002636 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2637 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala14613432013-03-21 11:13:25 +05302638 u16 ctrl_2;
2639
2640 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2641 /* Select Bus Speed Mode for host */
2642 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Venkat Gopalakrishnan0a29da92015-01-09 12:19:16 -08002643 if ((uhs == MMC_TIMING_MMC_HS400) ||
2644 (uhs == MMC_TIMING_MMC_HS200) ||
2645 (uhs == MMC_TIMING_UHS_SDR104))
Sahitya Tummala14613432013-03-21 11:13:25 +05302646 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2647 else if (uhs == MMC_TIMING_UHS_SDR12)
2648 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
2649 else if (uhs == MMC_TIMING_UHS_SDR25)
2650 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
2651 else if (uhs == MMC_TIMING_UHS_SDR50)
2652 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
Venkat Gopalakrishnan0a29da92015-01-09 12:19:16 -08002653 else if ((uhs == MMC_TIMING_UHS_DDR50) ||
2654 (uhs == MMC_TIMING_MMC_DDR52))
Sahitya Tummala14613432013-03-21 11:13:25 +05302655 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302656 /*
2657 * When clock frquency is less than 100MHz, the feedback clock must be
2658 * provided and DLL must not be used so that tuning can be skipped. To
2659 * provide feedback clock, the mode selection can be any value less
2660 * than 3'b011 in bits [2:0] of HOST CONTROL2 register.
2661 */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002662 if (host->clock <= CORE_FREQ_100MHZ) {
2663 if ((uhs == MMC_TIMING_MMC_HS400) ||
2664 (uhs == MMC_TIMING_MMC_HS200) ||
2665 (uhs == MMC_TIMING_UHS_SDR104))
2666 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302667
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002668 /*
2669 * Make sure DLL is disabled when not required
2670 *
2671 * Write 1 to DLL_RST bit of DLL_CONFIG register
2672 */
2673 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
2674 | CORE_DLL_RST),
2675 host->ioaddr + CORE_DLL_CONFIG);
2676
2677 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
2678 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
2679 | CORE_DLL_PDN),
2680 host->ioaddr + CORE_DLL_CONFIG);
2681 mb();
2682
2683 /*
2684 * The DLL needs to be restored and CDCLP533 recalibrated
2685 * when the clock frequency is set back to 400MHz.
2686 */
2687 msm_host->calibration_done = false;
2688 }
2689
2690 pr_debug("%s: %s-clock:%u uhs mode:%u ctrl_2:0x%x\n",
2691 mmc_hostname(host->mmc), __func__, host->clock, uhs, ctrl_2);
Sahitya Tummala14613432013-03-21 11:13:25 +05302692 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
2693
2694}
2695
Venkat Gopalakrishnan34811972015-03-04 14:39:01 -08002696#define MAX_TEST_BUS 60
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302697
2698void sdhci_msm_dump_vendor_regs(struct sdhci_host *host)
2699{
2700 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2701 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2702 int tbsel, tbsel2;
2703 int i, index = 0;
2704 u32 test_bus_val = 0;
2705 u32 debug_reg[MAX_TEST_BUS] = {0};
2706
2707 pr_info("----------- VENDOR REGISTER DUMP -----------\n");
2708 pr_info("Data cnt: 0x%08x | Fifo cnt: 0x%08x | Int sts: 0x%08x\n",
2709 readl_relaxed(msm_host->core_mem + CORE_MCI_DATA_CNT),
2710 readl_relaxed(msm_host->core_mem + CORE_MCI_FIFO_CNT),
2711 readl_relaxed(msm_host->core_mem + CORE_MCI_STATUS));
2712 pr_info("DLL cfg: 0x%08x | DLL sts: 0x%08x | SDCC ver: 0x%08x\n",
2713 readl_relaxed(host->ioaddr + CORE_DLL_CONFIG),
2714 readl_relaxed(host->ioaddr + CORE_DLL_STATUS),
2715 readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION));
2716 pr_info("Vndr func: 0x%08x | Vndr adma err : addr0: 0x%08x addr1: 0x%08x\n",
2717 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC),
2718 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_ADMA_ERR_ADDR0),
2719 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_ADMA_ERR_ADDR1));
2720
2721 /*
2722 * tbsel indicates [2:0] bits and tbsel2 indicates [7:4] bits
2723 * of CORE_TESTBUS_CONFIG register.
2724 *
2725 * To select test bus 0 to 7 use tbsel and to select any test bus
2726 * above 7 use (tbsel2 | tbsel) to get the test bus number. For eg,
2727 * to select test bus 14, write 0x1E to CORE_TESTBUS_CONFIG register
2728 * i.e., tbsel2[7:4] = 0001, tbsel[2:0] = 110.
2729 */
Venkat Gopalakrishnan34811972015-03-04 14:39:01 -08002730 for (tbsel2 = 0; tbsel2 < 7; tbsel2++) {
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302731 for (tbsel = 0; tbsel < 8; tbsel++) {
2732 if (index >= MAX_TEST_BUS)
2733 break;
2734 test_bus_val = (tbsel2 << CORE_TESTBUS_SEL2_BIT) |
2735 tbsel | CORE_TESTBUS_ENA;
2736 writel_relaxed(test_bus_val,
2737 msm_host->core_mem + CORE_TESTBUS_CONFIG);
2738 debug_reg[index++] = readl_relaxed(msm_host->core_mem +
2739 CORE_SDCC_DEBUG_REG);
2740 }
2741 }
2742 for (i = 0; i < MAX_TEST_BUS; i = i + 4)
2743 pr_info(" Test bus[%d to %d]: 0x%08x 0x%08x 0x%08x 0x%08x\n",
2744 i, i + 3, debug_reg[i], debug_reg[i+1],
2745 debug_reg[i+2], debug_reg[i+3]);
Venkat Gopalakrishnan06f7f792015-05-29 17:56:59 -07002746}
2747
Ritesh Harjani4a5ffd12015-07-15 13:32:07 +05302748/*
2749 * sdhci_msm_enhanced_strobe_mask :-
2750 * Before running CMDQ transfers in HS400 Enhanced Strobe mode,
2751 * SW should write 3 to
2752 * HC_VENDOR_SPECIFIC_FUNC3.CMDEN_HS400_INPUT_MASK_CNT register.
2753 * The default reset value of this register is 2.
2754 */
2755static void sdhci_msm_enhanced_strobe_mask(struct sdhci_host *host, bool set)
2756{
2757 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2758 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2759
2760 if (!msm_host->enhanced_strobe) {
2761 pr_debug("%s: host does not support hs400 enhanced strobe\n",
2762 mmc_hostname(host->mmc));
2763 return;
2764 }
2765
2766 if (set) {
2767 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC3)
2768 | CORE_CMDEN_HS400_INPUT_MASK_CNT),
2769 host->ioaddr + CORE_VENDOR_SPEC3);
2770 } else {
2771 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC3)
2772 & ~CORE_CMDEN_HS400_INPUT_MASK_CNT),
2773 host->ioaddr + CORE_VENDOR_SPEC3);
2774 }
2775}
2776
Venkat Gopalakrishnan06f7f792015-05-29 17:56:59 -07002777static void sdhci_msm_clear_set_dumpregs(struct sdhci_host *host, bool set)
2778{
2779 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2780 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2781
2782 if (set) {
2783 writel_relaxed(CORE_TESTBUS_ENA,
2784 msm_host->core_mem + CORE_TESTBUS_CONFIG);
2785 } else {
2786 u32 value;
2787
2788 value = readl_relaxed(msm_host->core_mem + CORE_TESTBUS_CONFIG);
2789 value &= ~CORE_TESTBUS_ENA;
2790 writel_relaxed(value, msm_host->core_mem + CORE_TESTBUS_CONFIG);
2791 }
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302792}
2793
Dov Levenglick9c575e22015-07-20 09:30:52 +03002794static void sdhci_msm_detect(struct sdhci_host *host, bool detected)
2795{
2796 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2797 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2798 struct mmc_host *mmc = msm_host->mmc;
2799 struct mmc_card *card = mmc->card;
2800
2801 if (detected && mmc_card_sdio(card))
2802 mmc->pm_caps |= MMC_PM_KEEP_POWER;
2803 else
2804 mmc->pm_caps &= ~MMC_PM_KEEP_POWER;
2805}
2806
Asutosh Das0ef24812012-12-18 16:14:02 +05302807static struct sdhci_ops sdhci_msm_ops = {
Sahitya Tummala14613432013-03-21 11:13:25 +05302808 .set_uhs_signaling = sdhci_msm_set_uhs_signaling,
Asutosh Das0ef24812012-12-18 16:14:02 +05302809 .check_power_status = sdhci_msm_check_power_status,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002810 .platform_execute_tuning = sdhci_msm_execute_tuning,
Ritesh Harjaniea709662015-05-27 15:40:24 +05302811 .enhanced_strobe = sdhci_msm_enhanced_strobe,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002812 .toggle_cdr = sdhci_msm_toggle_cdr,
Asutosh Das648f9d12013-01-10 21:11:04 +05302813 .get_max_segments = sdhci_msm_max_segs,
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302814 .set_clock = sdhci_msm_set_clock,
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302815 .get_min_clock = sdhci_msm_get_min_clock,
2816 .get_max_clock = sdhci_msm_get_max_clock,
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302817 .dump_vendor_regs = sdhci_msm_dump_vendor_regs,
Asutosh Dase5e9ca62013-07-30 19:08:36 +05302818 .config_auto_tuning_cmd = sdhci_msm_config_auto_tuning_cmd,
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302819 .enable_controller_clock = sdhci_msm_enable_controller_clock,
Venkat Gopalakrishnanb8cb7072015-01-09 11:04:34 -08002820 .set_bus_width = sdhci_set_bus_width,
Venkat Gopalakrishnan411df072015-01-09 11:09:44 -08002821 .reset = sdhci_reset,
Venkat Gopalakrishnan06f7f792015-05-29 17:56:59 -07002822 .clear_set_dumpregs = sdhci_msm_clear_set_dumpregs,
Ritesh Harjani4a5ffd12015-07-15 13:32:07 +05302823 .enhanced_strobe_mask = sdhci_msm_enhanced_strobe_mask,
Dov Levenglick9c575e22015-07-20 09:30:52 +03002824 .detect = sdhci_msm_detect,
Asutosh Das0ef24812012-12-18 16:14:02 +05302825};
2826
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302827static void sdhci_set_default_hw_caps(struct sdhci_msm_host *msm_host,
2828 struct sdhci_host *host)
2829{
Krishna Konda46fd1432014-10-30 21:13:27 -07002830 u32 version, caps = 0;
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302831 u16 minor;
2832 u8 major;
2833
2834 version = readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION);
2835 major = (version & CORE_VERSION_MAJOR_MASK) >>
2836 CORE_VERSION_MAJOR_SHIFT;
2837 minor = version & CORE_VERSION_TARGET_MASK;
2838
Krishna Konda46fd1432014-10-30 21:13:27 -07002839 caps = readl_relaxed(host->ioaddr + SDHCI_CAPABILITIES);
2840
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302841 /*
2842 * Starting with SDCC 5 controller (core major version = 1)
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002843 * controller won't advertise 3.0v, 1.8v and 8-bit features
2844 * except for some targets.
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302845 */
2846 if (major >= 1 && minor != 0x11 && minor != 0x12) {
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002847 struct sdhci_msm_reg_data *vdd_io_reg;
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002848 /*
2849 * Enable 1.8V support capability on controllers that
2850 * support dual voltage
2851 */
2852 vdd_io_reg = msm_host->pdata->vreg_data->vdd_io_data;
Krishna Konda46fd1432014-10-30 21:13:27 -07002853 if (vdd_io_reg && (vdd_io_reg->high_vol_level > 2700000))
2854 caps |= CORE_3_0V_SUPPORT;
2855 if (vdd_io_reg && (vdd_io_reg->low_vol_level < 1950000))
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002856 caps |= CORE_1_8V_SUPPORT;
Pratibhasagar Vada47992013-12-09 20:42:32 +05302857 if (msm_host->pdata->mmc_bus_width == MMC_CAP_8_BIT_DATA)
2858 caps |= CORE_8_BIT_SUPPORT;
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302859 }
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002860
2861 /*
2862 * SDCC 5 controller with major version 1, minor version 0x34 and later
2863 * with HS 400 mode support will use CM DLL instead of CDC LP 533 DLL.
2864 */
2865 if ((major == 1) && (minor < 0x34))
2866 msm_host->use_cdclp533 = true;
Gilad Broner2a10ca02014-10-02 17:20:35 +03002867
2868 /*
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -08002869 * SDCC 5 controller with major version 1, minor version 0x42 and later
2870 * will require additional steps when resetting DLL.
Ritesh Harjaniea709662015-05-27 15:40:24 +05302871 * It also supports HS400 enhanced strobe mode.
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -08002872 */
Ritesh Harjaniea709662015-05-27 15:40:24 +05302873 if ((major == 1) && (minor >= 0x42)) {
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -08002874 msm_host->use_updated_dll_reset = true;
Ritesh Harjaniea709662015-05-27 15:40:24 +05302875 msm_host->enhanced_strobe = true;
2876 }
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -08002877
2878 /*
Talel Shenhar9a25b882015-06-02 13:36:35 +03002879 * SDCC 5 controller with major version 1 and minor version 0x42,
2880 * 0x46 and 0x49 currently uses 14lpp tech DLL whose internal
2881 * gating cannot guarantee MCLK timing requirement i.e.
Ritesh Harjani764065e2015-05-13 14:14:45 +05302882 * when MCLK is gated OFF, it is not gated for less than 0.5us
2883 * and MCLK must be switched on for at-least 1us before DATA
2884 * starts coming.
2885 */
Talel Shenhar9a25b882015-06-02 13:36:35 +03002886 if ((major == 1) && ((minor == 0x42) || (minor == 0x46) ||
2887 (minor == 0x49)))
Ritesh Harjani764065e2015-05-13 14:14:45 +05302888 msm_host->use_14lpp_dll = true;
2889 /*
Gilad Broner2a10ca02014-10-02 17:20:35 +03002890 * Mask 64-bit support for controller with 32-bit address bus so that
2891 * smaller descriptor size will be used and improve memory consumption.
Gilad Broner2a10ca02014-10-02 17:20:35 +03002892 */
Venkat Gopalakrishnan9a62e042015-03-03 16:14:55 -08002893 if (!msm_host->pdata->largeaddressbus)
2894 caps &= ~CORE_SYS_BUS_SUPPORT_64_BIT;
2895
Gilad Broner2a10ca02014-10-02 17:20:35 +03002896 writel_relaxed(caps, host->ioaddr + CORE_VENDOR_SPEC_CAPABILITIES0);
Krishna Konda46fd1432014-10-30 21:13:27 -07002897 /* keep track of the value in SDHCI_CAPABILITIES */
2898 msm_host->caps_0 = caps;
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302899}
2900
Venkat Gopalakrishnand371f142015-05-29 17:49:46 -07002901#ifdef CONFIG_MMC_CQ_HCI
2902static void sdhci_msm_cmdq_init(struct sdhci_host *host,
2903 struct platform_device *pdev)
2904{
2905 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2906 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2907
2908 host->cq_host = cmdq_pltfm_init(pdev);
2909 if (IS_ERR(host->cq_host))
2910 dev_dbg(&pdev->dev, "cmdq-pltfm init: failed: %ld\n",
2911 PTR_ERR(host->cq_host));
2912 else
2913 msm_host->mmc->caps2 |= MMC_CAP2_CMD_QUEUE;
2914}
2915#else
2916static void sdhci_msm_cmdq_init(struct sdhci_host *host,
2917 struct platform_device *pdev)
2918{
2919
2920}
2921#endif
2922
Subhash Jadavaniebcd00d2015-07-09 17:28:42 -07002923static bool sdhci_msm_is_bootdevice(struct device *dev)
2924{
2925 if (strnstr(saved_command_line, "androidboot.bootdevice=",
2926 strlen(saved_command_line))) {
2927 char search_string[50];
2928
2929 snprintf(search_string, ARRAY_SIZE(search_string),
2930 "androidboot.bootdevice=%s", dev_name(dev));
2931 if (strnstr(saved_command_line, search_string,
2932 strlen(saved_command_line)))
2933 return true;
2934 else
2935 return false;
2936 }
2937
2938 /*
2939 * "androidboot.bootdevice=" argument is not present then
2940 * return true as we don't know the boot device anyways.
2941 */
2942 return true;
2943}
2944
Asutosh Das0ef24812012-12-18 16:14:02 +05302945static int sdhci_msm_probe(struct platform_device *pdev)
2946{
2947 struct sdhci_host *host;
2948 struct sdhci_pltfm_host *pltfm_host;
2949 struct sdhci_msm_host *msm_host;
2950 struct resource *core_memres = NULL;
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02002951 int ret = 0, dead = 0;
Stephen Boyd8dce5c62013-04-24 14:19:46 -07002952 u16 host_version;
Venkat Gopalakrishnan17edb352015-06-24 14:46:49 -07002953 u32 irq_status, irq_ctl;
Asutosh Das0ef24812012-12-18 16:14:02 +05302954
2955 pr_debug("%s: Enter %s\n", dev_name(&pdev->dev), __func__);
2956 msm_host = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_msm_host),
2957 GFP_KERNEL);
2958 if (!msm_host) {
2959 ret = -ENOMEM;
2960 goto out;
2961 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302962
2963 msm_host->sdhci_msm_pdata.ops = &sdhci_msm_ops;
2964 host = sdhci_pltfm_init(pdev, &msm_host->sdhci_msm_pdata, 0);
2965 if (IS_ERR(host)) {
2966 ret = PTR_ERR(host);
2967 goto out;
2968 }
2969
2970 pltfm_host = sdhci_priv(host);
2971 pltfm_host->priv = msm_host;
2972 msm_host->mmc = host->mmc;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302973 msm_host->pdev = pdev;
Asutosh Das0ef24812012-12-18 16:14:02 +05302974
2975 /* Extract platform data */
2976 if (pdev->dev.of_node) {
Venkat Gopalakrishnan270580a2013-03-11 12:17:57 -07002977 ret = of_alias_get_id(pdev->dev.of_node, "sdhc");
2978 if (ret < 0) {
2979 dev_err(&pdev->dev, "Failed to get slot index %d\n",
2980 ret);
2981 goto pltfm_free;
2982 }
Subhash Jadavaniebcd00d2015-07-09 17:28:42 -07002983
2984 /* skip the probe if eMMC isn't a boot device */
2985 if ((ret == 1) && !sdhci_msm_is_bootdevice(&pdev->dev))
2986 goto pltfm_free;
2987
Venkat Gopalakrishnan270580a2013-03-11 12:17:57 -07002988 if (disable_slots & (1 << (ret - 1))) {
2989 dev_info(&pdev->dev, "%s: Slot %d disabled\n", __func__,
2990 ret);
2991 ret = -ENODEV;
2992 goto pltfm_free;
2993 }
2994
Dov Levenglickc9033ab2015-03-10 16:00:56 +02002995 msm_host->pdata = sdhci_msm_populate_pdata(&pdev->dev,
2996 msm_host);
Asutosh Das0ef24812012-12-18 16:14:02 +05302997 if (!msm_host->pdata) {
2998 dev_err(&pdev->dev, "DT parsing error\n");
2999 goto pltfm_free;
3000 }
3001 } else {
3002 dev_err(&pdev->dev, "No device tree node\n");
3003 goto pltfm_free;
3004 }
3005
3006 /* Setup Clocks */
3007
3008 /* Setup SDCC bus voter clock. */
3009 msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus_clk");
3010 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
3011 /* Vote for max. clk rate for max. performance */
3012 ret = clk_set_rate(msm_host->bus_clk, INT_MAX);
3013 if (ret)
3014 goto pltfm_free;
3015 ret = clk_prepare_enable(msm_host->bus_clk);
3016 if (ret)
3017 goto pltfm_free;
3018 }
3019
3020 /* Setup main peripheral bus clock */
3021 msm_host->pclk = devm_clk_get(&pdev->dev, "iface_clk");
3022 if (!IS_ERR(msm_host->pclk)) {
3023 ret = clk_prepare_enable(msm_host->pclk);
3024 if (ret)
3025 goto bus_clk_disable;
3026 }
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05303027 atomic_set(&msm_host->controller_clock, 1);
Asutosh Das0ef24812012-12-18 16:14:02 +05303028
3029 /* Setup SDC MMC clock */
3030 msm_host->clk = devm_clk_get(&pdev->dev, "core_clk");
3031 if (IS_ERR(msm_host->clk)) {
3032 ret = PTR_ERR(msm_host->clk);
3033 goto pclk_disable;
3034 }
3035
Sahitya Tummala22dd3362013-02-28 19:50:51 +05303036 /* Set to the minimum supported clock frequency */
3037 ret = clk_set_rate(msm_host->clk, sdhci_msm_get_min_clock(host));
3038 if (ret) {
3039 dev_err(&pdev->dev, "MClk rate set failed (%d)\n", ret);
Sahitya Tummala020ede0d2013-06-07 13:03:07 +05303040 goto pclk_disable;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05303041 }
Sahitya Tummala020ede0d2013-06-07 13:03:07 +05303042 ret = clk_prepare_enable(msm_host->clk);
3043 if (ret)
3044 goto pclk_disable;
3045
Sahitya Tummala22dd3362013-02-28 19:50:51 +05303046 msm_host->clk_rate = sdhci_msm_get_min_clock(host);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05303047 atomic_set(&msm_host->clks_on, 1);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05303048
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07003049 /* Setup CDC calibration fixed feedback clock */
3050 msm_host->ff_clk = devm_clk_get(&pdev->dev, "cal_clk");
3051 if (!IS_ERR(msm_host->ff_clk)) {
3052 ret = clk_prepare_enable(msm_host->ff_clk);
3053 if (ret)
3054 goto clk_disable;
3055 }
3056
3057 /* Setup CDC calibration sleep clock */
3058 msm_host->sleep_clk = devm_clk_get(&pdev->dev, "sleep_clk");
3059 if (!IS_ERR(msm_host->sleep_clk)) {
3060 ret = clk_prepare_enable(msm_host->sleep_clk);
3061 if (ret)
3062 goto ff_clk_disable;
3063 }
3064
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07003065 msm_host->saved_tuning_phase = INVALID_TUNING_PHASE;
3066
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05303067 ret = sdhci_msm_bus_register(msm_host, pdev);
3068 if (ret)
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07003069 goto sleep_clk_disable;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05303070
3071 if (msm_host->msm_bus_vote.client_handle)
3072 INIT_DELAYED_WORK(&msm_host->msm_bus_vote.vote_work,
3073 sdhci_msm_bus_work);
3074 sdhci_msm_bus_voting(host, 1);
3075
Asutosh Das0ef24812012-12-18 16:14:02 +05303076 /* Setup regulators */
3077 ret = sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, true);
3078 if (ret) {
3079 dev_err(&pdev->dev, "Regulator setup failed (%d)\n", ret);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05303080 goto bus_unregister;
Asutosh Das0ef24812012-12-18 16:14:02 +05303081 }
3082
3083 /* Reset the core and Enable SDHC mode */
3084 core_memres = platform_get_resource_byname(pdev,
3085 IORESOURCE_MEM, "core_mem");
Asutosh Das890bdee2014-08-08 23:01:42 +05303086 if (!core_memres) {
3087 dev_err(&pdev->dev, "Failed to get iomem resource\n");
3088 goto vreg_deinit;
3089 }
Asutosh Das0ef24812012-12-18 16:14:02 +05303090 msm_host->core_mem = devm_ioremap(&pdev->dev, core_memres->start,
3091 resource_size(core_memres));
3092
3093 if (!msm_host->core_mem) {
3094 dev_err(&pdev->dev, "Failed to remap registers\n");
3095 ret = -ENOMEM;
3096 goto vreg_deinit;
3097 }
3098
Sahitya Tummala66b0fe32013-04-25 11:50:56 +05303099 /*
Venkat Gopalakrishnan17edb352015-06-24 14:46:49 -07003100 * Reset the vendor spec register to power on reset state.
Sahitya Tummala66b0fe32013-04-25 11:50:56 +05303101 */
Venkat Gopalakrishnan17edb352015-06-24 14:46:49 -07003102 writel_relaxed(CORE_VENDOR_SPEC_POR_VAL,
3103 host->ioaddr + CORE_VENDOR_SPEC);
Sahitya Tummala66b0fe32013-04-25 11:50:56 +05303104
Asutosh Das0ef24812012-12-18 16:14:02 +05303105 /* Set HC_MODE_EN bit in HC_MODE register */
3106 writel_relaxed(HC_MODE_EN, (msm_host->core_mem + CORE_HC_MODE));
3107
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07003108 /* Set FF_CLK_SW_RST_DIS bit in HC_MODE register */
3109 writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_HC_MODE) |
3110 FF_CLK_SW_RST_DIS, msm_host->core_mem + CORE_HC_MODE);
3111
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05303112 sdhci_set_default_hw_caps(msm_host, host);
Krishna Konda46fd1432014-10-30 21:13:27 -07003113
3114 /*
3115 * Set the PAD_PWR_SWTICH_EN bit so that the PAD_PWR_SWITCH bit can
3116 * be used as required later on.
3117 */
3118 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
3119 CORE_IO_PAD_PWR_SWITCH_EN),
3120 host->ioaddr + CORE_VENDOR_SPEC);
Asutosh Das0ef24812012-12-18 16:14:02 +05303121 /*
Subhash Jadavani28137342013-05-14 17:46:43 +05303122 * CORE_SW_RST above may trigger power irq if previous status of PWRCTL
3123 * was either BUS_ON or IO_HIGH_V. So before we enable the power irq
3124 * interrupt in GIC (by registering the interrupt handler), we need to
3125 * ensure that any pending power irq interrupt status is acknowledged
3126 * otherwise power irq interrupt handler would be fired prematurely.
3127 */
3128 irq_status = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
3129 writel_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
3130 irq_ctl = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_CTL);
3131 if (irq_status & (CORE_PWRCTL_BUS_ON | CORE_PWRCTL_BUS_OFF))
3132 irq_ctl |= CORE_PWRCTL_BUS_SUCCESS;
3133 if (irq_status & (CORE_PWRCTL_IO_HIGH | CORE_PWRCTL_IO_LOW))
3134 irq_ctl |= CORE_PWRCTL_IO_SUCCESS;
3135 writel_relaxed(irq_ctl, (msm_host->core_mem + CORE_PWRCTL_CTL));
Krishna Konda46fd1432014-10-30 21:13:27 -07003136
Subhash Jadavani28137342013-05-14 17:46:43 +05303137 /*
3138 * Ensure that above writes are propogated before interrupt enablement
3139 * in GIC.
3140 */
3141 mb();
3142
3143 /*
Asutosh Das0ef24812012-12-18 16:14:02 +05303144 * Following are the deviations from SDHC spec v3.0 -
3145 * 1. Card detection is handled using separate GPIO.
3146 * 2. Bus power control is handled by interacting with PMIC.
3147 */
3148 host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
3149 host->quirks |= SDHCI_QUIRK_SINGLE_POWER_WRITE;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05303150 host->quirks |= SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN;
Talel Shenhar4661c2a2015-06-24 15:49:30 +03003151 host->quirks |= SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05303152 host->quirks2 |= SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK;
Sahitya Tummala87d43942013-04-12 11:49:11 +05303153 host->quirks2 |= SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD;
Sahitya Tummala314162c2013-04-12 12:11:20 +05303154 host->quirks2 |= SDHCI_QUIRK2_BROKEN_PRESET_VALUE;
Sahitya Tummala7c9780d2013-04-12 11:59:25 +05303155 host->quirks2 |= SDHCI_QUIRK2_USE_RESERVED_MAX_TIMEOUT;
Asutosh Das0ef24812012-12-18 16:14:02 +05303156
Sahitya Tummalaa5733ab52013-06-10 16:32:51 +05303157 if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK)
3158 host->quirks2 |= SDHCI_QUIRK2_DIVIDE_TOUT_BY_4;
3159
Stephen Boyd8dce5c62013-04-24 14:19:46 -07003160 host_version = readw_relaxed((host->ioaddr + SDHCI_HOST_VERSION));
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07003161 dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n",
3162 host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >>
3163 SDHCI_VENDOR_VER_SHIFT));
3164 if (((host_version & SDHCI_VENDOR_VER_MASK) >>
3165 SDHCI_VENDOR_VER_SHIFT) == SDHCI_VER_100) {
3166 /*
3167 * Add 40us delay in interrupt handler when
3168 * operating at initialization frequency(400KHz).
3169 */
3170 host->quirks2 |= SDHCI_QUIRK2_SLOW_INT_CLR;
3171 /*
3172 * Set Software Reset for DAT line in Software
3173 * Reset Register (Bit 2).
3174 */
3175 host->quirks2 |= SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT;
3176 }
3177
Asutosh Das214b9662013-06-13 14:27:42 +05303178 host->quirks2 |= SDHCI_QUIRK2_IGN_DATA_END_BIT_ERROR;
3179
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07003180 /* Setup PWRCTL irq */
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02003181 msm_host->pwr_irq = platform_get_irq_byname(pdev, "pwr_irq");
3182 if (msm_host->pwr_irq < 0) {
Asutosh Das0ef24812012-12-18 16:14:02 +05303183 dev_err(&pdev->dev, "Failed to get pwr_irq by name (%d)\n",
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02003184 msm_host->pwr_irq);
Asutosh Das0ef24812012-12-18 16:14:02 +05303185 goto vreg_deinit;
3186 }
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02003187 ret = devm_request_threaded_irq(&pdev->dev, msm_host->pwr_irq, NULL,
Asutosh Das0ef24812012-12-18 16:14:02 +05303188 sdhci_msm_pwr_irq, IRQF_ONESHOT,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07003189 dev_name(&pdev->dev), host);
Asutosh Das0ef24812012-12-18 16:14:02 +05303190 if (ret) {
3191 dev_err(&pdev->dev, "Request threaded irq(%d) failed (%d)\n",
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02003192 msm_host->pwr_irq, ret);
Asutosh Das0ef24812012-12-18 16:14:02 +05303193 goto vreg_deinit;
3194 }
3195
3196 /* Enable pwr irq interrupts */
3197 writel_relaxed(INT_MASK, (msm_host->core_mem + CORE_PWRCTL_MASK));
3198
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05303199#ifdef CONFIG_MMC_CLKGATE
3200 /* Set clock gating delay to be used when CONFIG_MMC_CLKGATE is set */
3201 msm_host->mmc->clkgate_delay = SDHCI_MSM_MMC_CLK_GATE_DELAY;
3202#endif
3203
Asutosh Das0ef24812012-12-18 16:14:02 +05303204 /* Set host capabilities */
3205 msm_host->mmc->caps |= msm_host->pdata->mmc_bus_width;
3206 msm_host->mmc->caps |= msm_host->pdata->caps;
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003207 msm_host->mmc->caps |= MMC_CAP_AGGRESSIVE_PM;
Asutosh Das0ef24812012-12-18 16:14:02 +05303208 msm_host->mmc->caps2 |= msm_host->pdata->caps2;
Venkat Gopalakrishnan97c16112014-12-16 18:26:25 -08003209 msm_host->mmc->caps2 |= MMC_CAP2_BOOTPART_NOACC;
3210 msm_host->mmc->caps2 |= MMC_CAP2_FULL_PWR_CYCLE;
Subhash Jadavani6d472b22013-05-29 15:52:10 +05303211 msm_host->mmc->caps2 |= MMC_CAP2_ASYNC_SDIO_IRQ_4BIT_MODE;
Venkat Gopalakrishnan97c16112014-12-16 18:26:25 -08003212 msm_host->mmc->caps2 |= MMC_CAP2_HS400_POST_TUNING;
Talel Shenhar3d1dbf32015-05-13 14:08:39 +03003213 msm_host->mmc->caps2 |= MMC_CAP2_CLK_SCALE;
Asutosh Das0ef24812012-12-18 16:14:02 +05303214
3215 if (msm_host->pdata->nonremovable)
3216 msm_host->mmc->caps |= MMC_CAP_NONREMOVABLE;
3217
Guoping Yuf7c91332014-08-20 16:56:18 +08003218 if (msm_host->pdata->nonhotplug)
3219 msm_host->mmc->caps2 |= MMC_CAP2_NONHOTPLUG;
3220
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05303221 host->cpu_dma_latency_us = msm_host->pdata->cpu_dma_latency_us;
Maya Erez994cf2f2014-10-21 20:22:04 +03003222 host->pm_qos_req_dma.type = msm_host->pdata->cpu_affinity_type;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05303223
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05303224 init_completion(&msm_host->pwr_irq_completion);
3225
Sahitya Tummala581df132013-03-12 14:57:46 +05303226 if (gpio_is_valid(msm_host->pdata->status_gpio)) {
Sahitya Tummala6ddabb42014-06-05 13:26:55 +05303227 /*
3228 * Set up the card detect GPIO in active configuration before
3229 * configuring it as an IRQ. Otherwise, it can be in some
3230 * weird/inconsistent state resulting in flood of interrupts.
3231 */
3232 sdhci_msm_setup_pins(msm_host->pdata, true);
3233
Sahitya Tummalaa3888f42015-02-05 14:05:27 +05303234 /*
3235 * This delay is needed for stabilizing the card detect GPIO
3236 * line after changing the pull configs.
3237 */
3238 usleep_range(10000, 10500);
Sahitya Tummala581df132013-03-12 14:57:46 +05303239 ret = mmc_gpio_request_cd(msm_host->mmc,
3240 msm_host->pdata->status_gpio, 0);
3241 if (ret) {
3242 dev_err(&pdev->dev, "%s: Failed to request card detection IRQ %d\n",
3243 __func__, ret);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05303244 goto vreg_deinit;
Sahitya Tummala581df132013-03-12 14:57:46 +05303245 }
3246 }
3247
Krishna Konda7feab352013-09-17 23:55:40 -07003248 if ((sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT) &&
3249 (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(64)))) {
3250 host->dma_mask = DMA_BIT_MASK(64);
3251 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
3252 } else if (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(32))) {
Sahitya Tummalaeaa21862013-03-20 19:34:59 +05303253 host->dma_mask = DMA_BIT_MASK(32);
3254 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
3255 } else {
3256 dev_err(&pdev->dev, "%s: Failed to set dma mask\n", __func__);
3257 }
3258
Venkat Gopalakrishnand371f142015-05-29 17:49:46 -07003259 sdhci_msm_cmdq_init(host, pdev);
Asutosh Das0ef24812012-12-18 16:14:02 +05303260 ret = sdhci_add_host(host);
3261 if (ret) {
3262 dev_err(&pdev->dev, "Add host failed (%d)\n", ret);
Sahitya Tummala581df132013-03-12 14:57:46 +05303263 goto vreg_deinit;
Asutosh Das0ef24812012-12-18 16:14:02 +05303264 }
3265
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003266 pm_runtime_set_active(&pdev->dev);
3267 pm_runtime_enable(&pdev->dev);
3268 pm_runtime_set_autosuspend_delay(&pdev->dev, MSM_AUTOSUSPEND_DELAY_MS);
3269 pm_runtime_use_autosuspend(&pdev->dev);
3270
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05303271 msm_host->msm_bus_vote.max_bus_bw.show = show_sdhci_max_bus_bw;
3272 msm_host->msm_bus_vote.max_bus_bw.store = store_sdhci_max_bus_bw;
3273 sysfs_attr_init(&msm_host->msm_bus_vote.max_bus_bw.attr);
3274 msm_host->msm_bus_vote.max_bus_bw.attr.name = "max_bus_bw";
3275 msm_host->msm_bus_vote.max_bus_bw.attr.mode = S_IRUGO | S_IWUSR;
3276 ret = device_create_file(&pdev->dev,
3277 &msm_host->msm_bus_vote.max_bus_bw);
3278 if (ret)
3279 goto remove_host;
3280
Sahitya Tummala5c55b932013-06-20 14:00:18 +05303281 if (!gpio_is_valid(msm_host->pdata->status_gpio)) {
3282 msm_host->polling.show = show_polling;
3283 msm_host->polling.store = store_polling;
3284 sysfs_attr_init(&msm_host->polling.attr);
3285 msm_host->polling.attr.name = "polling";
3286 msm_host->polling.attr.mode = S_IRUGO | S_IWUSR;
3287 ret = device_create_file(&pdev->dev, &msm_host->polling);
3288 if (ret)
3289 goto remove_max_bus_bw_file;
3290 }
Asutosh Dase5e9ca62013-07-30 19:08:36 +05303291
3292 msm_host->auto_cmd21_attr.show = show_auto_cmd21;
3293 msm_host->auto_cmd21_attr.store = store_auto_cmd21;
3294 sysfs_attr_init(&msm_host->auto_cmd21_attr.attr);
3295 msm_host->auto_cmd21_attr.attr.name = "enable_auto_cmd21";
3296 msm_host->auto_cmd21_attr.attr.mode = S_IRUGO | S_IWUSR;
3297 ret = device_create_file(&pdev->dev, &msm_host->auto_cmd21_attr);
3298 if (ret) {
3299 pr_err("%s: %s: failed creating auto-cmd21 attr: %d\n",
3300 mmc_hostname(host->mmc), __func__, ret);
3301 device_remove_file(&pdev->dev, &msm_host->auto_cmd21_attr);
3302 }
Asutosh Das0ef24812012-12-18 16:14:02 +05303303 /* Successful initialization */
3304 goto out;
3305
Sahitya Tummala5c55b932013-06-20 14:00:18 +05303306remove_max_bus_bw_file:
3307 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Asutosh Das0ef24812012-12-18 16:14:02 +05303308remove_host:
3309 dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003310 pm_runtime_disable(&pdev->dev);
Asutosh Das0ef24812012-12-18 16:14:02 +05303311 sdhci_remove_host(host, dead);
3312vreg_deinit:
3313 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05303314bus_unregister:
3315 if (msm_host->msm_bus_vote.client_handle)
3316 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
3317 sdhci_msm_bus_unregister(msm_host);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07003318sleep_clk_disable:
3319 if (!IS_ERR(msm_host->sleep_clk))
3320 clk_disable_unprepare(msm_host->sleep_clk);
3321ff_clk_disable:
3322 if (!IS_ERR(msm_host->ff_clk))
3323 clk_disable_unprepare(msm_host->ff_clk);
Asutosh Das0ef24812012-12-18 16:14:02 +05303324clk_disable:
3325 if (!IS_ERR(msm_host->clk))
3326 clk_disable_unprepare(msm_host->clk);
3327pclk_disable:
3328 if (!IS_ERR(msm_host->pclk))
3329 clk_disable_unprepare(msm_host->pclk);
3330bus_clk_disable:
3331 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
3332 clk_disable_unprepare(msm_host->bus_clk);
3333pltfm_free:
3334 sdhci_pltfm_free(pdev);
3335out:
3336 pr_debug("%s: Exit %s\n", dev_name(&pdev->dev), __func__);
3337 return ret;
3338}
3339
3340static int sdhci_msm_remove(struct platform_device *pdev)
3341{
3342 struct sdhci_host *host = platform_get_drvdata(pdev);
3343 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3344 struct sdhci_msm_host *msm_host = pltfm_host->priv;
3345 struct sdhci_msm_pltfm_data *pdata = msm_host->pdata;
3346 int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) ==
3347 0xffffffff);
3348
3349 pr_debug("%s: %s\n", dev_name(&pdev->dev), __func__);
Sahitya Tummala5c55b932013-06-20 14:00:18 +05303350 if (!gpio_is_valid(msm_host->pdata->status_gpio))
3351 device_remove_file(&pdev->dev, &msm_host->polling);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05303352 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003353 pm_runtime_disable(&pdev->dev);
Asutosh Das0ef24812012-12-18 16:14:02 +05303354 sdhci_remove_host(host, dead);
3355 sdhci_pltfm_free(pdev);
Sahitya Tummala581df132013-03-12 14:57:46 +05303356
Asutosh Das0ef24812012-12-18 16:14:02 +05303357 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05303358
Pratibhasagar V9acf2642013-11-21 21:07:21 +05303359 sdhci_msm_setup_pins(pdata, true);
3360 sdhci_msm_setup_pins(pdata, false);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05303361
3362 if (msm_host->msm_bus_vote.client_handle) {
3363 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
3364 sdhci_msm_bus_unregister(msm_host);
3365 }
Asutosh Das0ef24812012-12-18 16:14:02 +05303366 return 0;
3367}
3368
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003369#ifdef CONFIG_PM
3370static int sdhci_msm_runtime_suspend(struct device *dev)
3371{
3372 struct sdhci_host *host = dev_get_drvdata(dev);
3373 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3374 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003375 ktime_t start = ktime_get();
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003376
3377 disable_irq(host->irq);
3378 disable_irq(msm_host->pwr_irq);
3379
3380 /*
3381 * Remove the vote immediately only if clocks are off in which
3382 * case we might have queued work to remove vote but it may not
3383 * be completed before runtime suspend or system suspend.
3384 */
3385 if (!atomic_read(&msm_host->clks_on)) {
3386 if (msm_host->msm_bus_vote.client_handle)
3387 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
3388 }
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003389 trace_sdhci_msm_runtime_suspend(mmc_hostname(host->mmc), 0,
3390 ktime_to_us(ktime_sub(ktime_get(), start)));
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003391
3392 return 0;
3393}
3394
3395static int sdhci_msm_runtime_resume(struct device *dev)
3396{
3397 struct sdhci_host *host = dev_get_drvdata(dev);
3398 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3399 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003400 ktime_t start = ktime_get();
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003401
3402 enable_irq(msm_host->pwr_irq);
3403 enable_irq(host->irq);
3404
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003405 trace_sdhci_msm_runtime_resume(mmc_hostname(host->mmc), 0,
3406 ktime_to_us(ktime_sub(ktime_get(), start)));
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003407 return 0;
3408}
3409
3410static int sdhci_msm_suspend(struct device *dev)
3411{
3412 struct sdhci_host *host = dev_get_drvdata(dev);
3413 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3414 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003415 int ret = 0;
3416 ktime_t start = ktime_get();
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003417
3418 if (gpio_is_valid(msm_host->pdata->status_gpio) &&
3419 (msm_host->mmc->slot.cd_irq >= 0))
3420 disable_irq(msm_host->mmc->slot.cd_irq);
3421
3422 if (pm_runtime_suspended(dev)) {
3423 pr_debug("%s: %s: already runtime suspended\n",
3424 mmc_hostname(host->mmc), __func__);
3425 goto out;
3426 }
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003427 ret = sdhci_msm_runtime_suspend(dev);
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003428out:
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003429 trace_sdhci_msm_suspend(mmc_hostname(host->mmc), ret,
3430 ktime_to_us(ktime_sub(ktime_get(), start)));
3431 return ret;
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003432}
3433
3434static int sdhci_msm_resume(struct device *dev)
3435{
3436 struct sdhci_host *host = dev_get_drvdata(dev);
3437 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3438 struct sdhci_msm_host *msm_host = pltfm_host->priv;
3439 int ret = 0;
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003440 ktime_t start = ktime_get();
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003441
3442 if (gpio_is_valid(msm_host->pdata->status_gpio) &&
3443 (msm_host->mmc->slot.cd_irq >= 0))
3444 enable_irq(msm_host->mmc->slot.cd_irq);
3445
3446 if (pm_runtime_suspended(dev)) {
3447 pr_debug("%s: %s: runtime suspended, defer system resume\n",
3448 mmc_hostname(host->mmc), __func__);
3449 goto out;
3450 }
3451
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003452 ret = sdhci_msm_runtime_resume(dev);
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003453out:
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003454 trace_sdhci_msm_resume(mmc_hostname(host->mmc), ret,
3455 ktime_to_us(ktime_sub(ktime_get(), start)));
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003456 return ret;
3457}
3458
3459static const struct dev_pm_ops sdhci_msm_pmops = {
3460 SET_SYSTEM_SLEEP_PM_OPS(sdhci_msm_suspend, sdhci_msm_resume)
3461 SET_RUNTIME_PM_OPS(sdhci_msm_runtime_suspend, sdhci_msm_runtime_resume,
3462 NULL)
3463};
3464
3465#define SDHCI_MSM_PMOPS (&sdhci_msm_pmops)
3466
3467#else
3468#define SDHCI_MSM_PMOPS NULL
3469#endif
Asutosh Das0ef24812012-12-18 16:14:02 +05303470static const struct of_device_id sdhci_msm_dt_match[] = {
3471 {.compatible = "qcom,sdhci-msm"},
Venkat Gopalakrishnan272ba402015-06-25 12:00:02 -07003472 {},
Asutosh Das0ef24812012-12-18 16:14:02 +05303473};
3474MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match);
3475
3476static struct platform_driver sdhci_msm_driver = {
3477 .probe = sdhci_msm_probe,
3478 .remove = sdhci_msm_remove,
3479 .driver = {
3480 .name = "sdhci_msm",
3481 .owner = THIS_MODULE,
3482 .of_match_table = sdhci_msm_dt_match,
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003483 .pm = SDHCI_MSM_PMOPS,
Asutosh Das0ef24812012-12-18 16:14:02 +05303484 },
3485};
3486
3487module_platform_driver(sdhci_msm_driver);
3488
3489MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Secure Digital Host Controller Interface driver");
3490MODULE_LICENSE("GPL v2");