blob: a96bf3b3e446433234f6c93ea936308d8fa2e26d [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)
163
164#define CORE_DLL_CONFIG_2 0x1B4
165#define CORE_DDR_CAL_EN (1 << 0)
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800166#define CORE_FLL_CYCLE_CNT (1 << 18)
167#define CORE_DLL_CLOCK_DISABLE (1 << 21)
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700168
169#define CORE_DDR_CONFIG 0x1B8
170#define DDR_CONFIG_POR_VAL 0x80040853
171
Sahitya Tummala56874732015-05-21 08:24:03 +0530172#define MSM_MMC_DEFAULT_CPU_DMA_LATENCY 200 /* usecs */
173
Venkat Gopalakrishnan450745e2014-07-24 20:39:34 -0700174/* 512 descriptors */
175#define SDHCI_MSM_MAX_SEGMENTS (1 << 9)
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +0530176#define SDHCI_MSM_MMC_CLK_GATE_DELAY 200 /* msecs */
Asutosh Das648f9d12013-01-10 21:11:04 +0530177
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700178#define CORE_FREQ_100MHZ (100 * 1000 * 1000)
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800179#define TCXO_FREQ 19200000
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700180
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700181#define INVALID_TUNING_PHASE -1
182
Krishna Konda96e6b112013-10-28 15:25:03 -0700183#define NUM_TUNING_PHASES 16
184#define MAX_DRV_TYPES_SUPPORTED_HS200 3
Konstantin Dorfman98377d32015-02-25 10:09:41 +0200185#define MSM_AUTOSUSPEND_DELAY_MS 100
Krishna Konda96e6b112013-10-28 15:25:03 -0700186
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700187static const u32 tuning_block_64[] = {
188 0x00FF0FFF, 0xCCC3CCFF, 0xFFCC3CC3, 0xEFFEFFFE,
189 0xDDFFDFFF, 0xFBFFFBFF, 0xFF7FFFBF, 0xEFBDF777,
190 0xF0FFF0FF, 0x3CCCFC0F, 0xCFCC33CC, 0xEEFFEFFF,
191 0xFDFFFDFF, 0xFFBFFFDF, 0xFFF7FFBB, 0xDE7B7FF7
192};
193
194static const u32 tuning_block_128[] = {
195 0xFF00FFFF, 0x0000FFFF, 0xCCCCFFFF, 0xCCCC33CC,
196 0xCC3333CC, 0xFFFFCCCC, 0xFFFFEEFF, 0xFFEEEEFF,
197 0xFFDDFFFF, 0xDDDDFFFF, 0xBBFFFFFF, 0xBBFFFFFF,
198 0xFFFFFFBB, 0xFFFFFF77, 0x77FF7777, 0xFFEEDDBB,
199 0x00FFFFFF, 0x00FFFFFF, 0xCCFFFF00, 0xCC33CCCC,
200 0x3333CCCC, 0xFFCCCCCC, 0xFFEEFFFF, 0xEEEEFFFF,
201 0xDDFFFFFF, 0xDDFFFFFF, 0xFFFFFFDD, 0xFFFFFFBB,
202 0xFFFFBBBB, 0xFFFF77FF, 0xFF7777FF, 0xEEDDBB77
203};
Asutosh Das0ef24812012-12-18 16:14:02 +0530204
Venkat Gopalakrishnan270580a2013-03-11 12:17:57 -0700205static int disable_slots;
206/* root can write, others read */
207module_param(disable_slots, int, S_IRUGO|S_IWUSR);
208
Asutosh Das0ef24812012-12-18 16:14:02 +0530209enum vdd_io_level {
210 /* set vdd_io_data->low_vol_level */
211 VDD_IO_LOW,
212 /* set vdd_io_data->high_vol_level */
213 VDD_IO_HIGH,
214 /*
215 * set whatever there in voltage_level (third argument) of
216 * sdhci_msm_set_vdd_io_vol() function.
217 */
218 VDD_IO_SET_LEVEL,
219};
220
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700221/* MSM platform specific tuning */
222static inline int msm_dll_poll_ck_out_en(struct sdhci_host *host,
223 u8 poll)
224{
225 int rc = 0;
226 u32 wait_cnt = 50;
227 u8 ck_out_en = 0;
228 struct mmc_host *mmc = host->mmc;
229
230 /* poll for CK_OUT_EN bit. max. poll time = 50us */
231 ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) &
232 CORE_CK_OUT_EN);
233
234 while (ck_out_en != poll) {
235 if (--wait_cnt == 0) {
236 pr_err("%s: %s: CK_OUT_EN bit is not %d\n",
237 mmc_hostname(mmc), __func__, poll);
238 rc = -ETIMEDOUT;
239 goto out;
240 }
241 udelay(1);
242
243 ck_out_en = !!(readl_relaxed(host->ioaddr +
244 CORE_DLL_CONFIG) & CORE_CK_OUT_EN);
245 }
246out:
247 return rc;
248}
249
Asutosh Dase5e9ca62013-07-30 19:08:36 +0530250/*
251 * Enable CDR to track changes of DAT lines and adjust sampling
252 * point according to voltage/temperature variations
253 */
254static int msm_enable_cdr_cm_sdc4_dll(struct sdhci_host *host)
255{
256 int rc = 0;
257 u32 config;
258
259 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
260 config |= CORE_CDR_EN;
261 config &= ~(CORE_CDR_EXT_EN | CORE_CK_OUT_EN);
262 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
263
264 rc = msm_dll_poll_ck_out_en(host, 0);
265 if (rc)
266 goto err;
267
268 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) |
269 CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
270
271 rc = msm_dll_poll_ck_out_en(host, 1);
272 if (rc)
273 goto err;
274 goto out;
275err:
276 pr_err("%s: %s: failed\n", mmc_hostname(host->mmc), __func__);
277out:
278 return rc;
279}
280
281static ssize_t store_auto_cmd21(struct device *dev, struct device_attribute
282 *attr, const char *buf, size_t count)
283{
284 struct sdhci_host *host = dev_get_drvdata(dev);
285 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
286 struct sdhci_msm_host *msm_host = pltfm_host->priv;
287 u32 tmp;
288 unsigned long flags;
289
290 if (!kstrtou32(buf, 0, &tmp)) {
291 spin_lock_irqsave(&host->lock, flags);
292 msm_host->en_auto_cmd21 = !!tmp;
293 spin_unlock_irqrestore(&host->lock, flags);
294 }
295 return count;
296}
297
298static ssize_t show_auto_cmd21(struct device *dev,
299 struct device_attribute *attr, char *buf)
300{
301 struct sdhci_host *host = dev_get_drvdata(dev);
302 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
303 struct sdhci_msm_host *msm_host = pltfm_host->priv;
304
305 return snprintf(buf, PAGE_SIZE, "%d\n", msm_host->en_auto_cmd21);
306}
307
308/* MSM auto-tuning handler */
309static int sdhci_msm_config_auto_tuning_cmd(struct sdhci_host *host,
310 bool enable,
311 u32 type)
312{
313 int rc = 0;
314 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
315 struct sdhci_msm_host *msm_host = pltfm_host->priv;
316 u32 val = 0;
317
318 if (!msm_host->en_auto_cmd21)
319 return 0;
320
321 if (type == MMC_SEND_TUNING_BLOCK_HS200)
322 val = CORE_HC_AUTO_CMD21_EN;
323 else
324 return 0;
325
326 if (enable) {
327 rc = msm_enable_cdr_cm_sdc4_dll(host);
328 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
329 val, host->ioaddr + CORE_VENDOR_SPEC);
330 } else {
331 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
332 ~val, host->ioaddr + CORE_VENDOR_SPEC);
333 }
334 return rc;
335}
336
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700337static int msm_config_cm_dll_phase(struct sdhci_host *host, u8 phase)
338{
339 int rc = 0;
340 u8 grey_coded_phase_table[] = {0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4,
341 0xC, 0xD, 0xF, 0xE, 0xA, 0xB, 0x9,
342 0x8};
343 unsigned long flags;
344 u32 config;
345 struct mmc_host *mmc = host->mmc;
346
347 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
348 spin_lock_irqsave(&host->lock, flags);
349
350 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
351 config &= ~(CORE_CDR_EN | CORE_CK_OUT_EN);
352 config |= (CORE_CDR_EXT_EN | CORE_DLL_EN);
353 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
354
355 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '0' */
356 rc = msm_dll_poll_ck_out_en(host, 0);
357 if (rc)
358 goto err_out;
359
360 /*
361 * Write the selected DLL clock output phase (0 ... 15)
362 * to CDR_SELEXT bit field of DLL_CONFIG register.
363 */
364 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
365 & ~(0xF << 20))
366 | (grey_coded_phase_table[phase] << 20)),
367 host->ioaddr + CORE_DLL_CONFIG);
368
369 /* Set CK_OUT_EN bit of DLL_CONFIG register to 1. */
370 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
371 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
372
373 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '1' */
374 rc = msm_dll_poll_ck_out_en(host, 1);
375 if (rc)
376 goto err_out;
377
378 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
379 config |= CORE_CDR_EN;
380 config &= ~CORE_CDR_EXT_EN;
381 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
382 goto out;
383
384err_out:
385 pr_err("%s: %s: Failed to set DLL phase: %d\n",
386 mmc_hostname(mmc), __func__, phase);
387out:
388 spin_unlock_irqrestore(&host->lock, flags);
389 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
390 return rc;
391}
392
393/*
394 * Find out the greatest range of consecuitive selected
395 * DLL clock output phases that can be used as sampling
396 * setting for SD3.0 UHS-I card read operation (in SDR104
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700397 * timing mode) or for eMMC4.5 card read operation (in
398 * HS400/HS200 timing mode).
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700399 * Select the 3/4 of the range and configure the DLL with the
400 * selected DLL clock output phase.
401 */
402
403static int msm_find_most_appropriate_phase(struct sdhci_host *host,
404 u8 *phase_table, u8 total_phases)
405{
406 int ret;
407 u8 ranges[MAX_PHASES][MAX_PHASES] = { {0}, {0} };
408 u8 phases_per_row[MAX_PHASES] = {0};
409 int row_index = 0, col_index = 0, selected_row_index = 0, curr_max = 0;
410 int i, cnt, phase_0_raw_index = 0, phase_15_raw_index = 0;
411 bool phase_0_found = false, phase_15_found = false;
412 struct mmc_host *mmc = host->mmc;
413
414 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
415 if (!total_phases || (total_phases > MAX_PHASES)) {
416 pr_err("%s: %s: invalid argument: total_phases=%d\n",
417 mmc_hostname(mmc), __func__, total_phases);
418 return -EINVAL;
419 }
420
421 for (cnt = 0; cnt < total_phases; cnt++) {
422 ranges[row_index][col_index] = phase_table[cnt];
423 phases_per_row[row_index] += 1;
424 col_index++;
425
426 if ((cnt + 1) == total_phases) {
427 continue;
428 /* check if next phase in phase_table is consecutive or not */
429 } else if ((phase_table[cnt] + 1) != phase_table[cnt + 1]) {
430 row_index++;
431 col_index = 0;
432 }
433 }
434
435 if (row_index >= MAX_PHASES)
436 return -EINVAL;
437
438 /* Check if phase-0 is present in first valid window? */
439 if (!ranges[0][0]) {
440 phase_0_found = true;
441 phase_0_raw_index = 0;
442 /* Check if cycle exist between 2 valid windows */
443 for (cnt = 1; cnt <= row_index; cnt++) {
444 if (phases_per_row[cnt]) {
445 for (i = 0; i < phases_per_row[cnt]; i++) {
446 if (ranges[cnt][i] == 15) {
447 phase_15_found = true;
448 phase_15_raw_index = cnt;
449 break;
450 }
451 }
452 }
453 }
454 }
455
456 /* If 2 valid windows form cycle then merge them as single window */
457 if (phase_0_found && phase_15_found) {
458 /* number of phases in raw where phase 0 is present */
459 u8 phases_0 = phases_per_row[phase_0_raw_index];
460 /* number of phases in raw where phase 15 is present */
461 u8 phases_15 = phases_per_row[phase_15_raw_index];
462
463 if (phases_0 + phases_15 >= MAX_PHASES)
464 /*
465 * If there are more than 1 phase windows then total
466 * number of phases in both the windows should not be
467 * more than or equal to MAX_PHASES.
468 */
469 return -EINVAL;
470
471 /* Merge 2 cyclic windows */
472 i = phases_15;
473 for (cnt = 0; cnt < phases_0; cnt++) {
474 ranges[phase_15_raw_index][i] =
475 ranges[phase_0_raw_index][cnt];
476 if (++i >= MAX_PHASES)
477 break;
478 }
479
480 phases_per_row[phase_0_raw_index] = 0;
481 phases_per_row[phase_15_raw_index] = phases_15 + phases_0;
482 }
483
484 for (cnt = 0; cnt <= row_index; cnt++) {
485 if (phases_per_row[cnt] > curr_max) {
486 curr_max = phases_per_row[cnt];
487 selected_row_index = cnt;
488 }
489 }
490
491 i = ((curr_max * 3) / 4);
492 if (i)
493 i--;
494
495 ret = (int)ranges[selected_row_index][i];
496
497 if (ret >= MAX_PHASES) {
498 ret = -EINVAL;
499 pr_err("%s: %s: invalid phase selected=%d\n",
500 mmc_hostname(mmc), __func__, ret);
501 }
502
503 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
504 return ret;
505}
506
507static inline void msm_cm_dll_set_freq(struct sdhci_host *host)
508{
509 u32 mclk_freq = 0;
510
511 /* Program the MCLK value to MCLK_FREQ bit field */
512 if (host->clock <= 112000000)
513 mclk_freq = 0;
514 else if (host->clock <= 125000000)
515 mclk_freq = 1;
516 else if (host->clock <= 137000000)
517 mclk_freq = 2;
518 else if (host->clock <= 150000000)
519 mclk_freq = 3;
520 else if (host->clock <= 162000000)
521 mclk_freq = 4;
522 else if (host->clock <= 175000000)
523 mclk_freq = 5;
524 else if (host->clock <= 187000000)
525 mclk_freq = 6;
526 else if (host->clock <= 200000000)
527 mclk_freq = 7;
528
529 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
530 & ~(7 << 24)) | (mclk_freq << 24)),
531 host->ioaddr + CORE_DLL_CONFIG);
532}
533
534/* Initialize the DLL (Programmable Delay Line ) */
535static int msm_init_cm_dll(struct sdhci_host *host)
536{
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800537 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
538 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700539 struct mmc_host *mmc = host->mmc;
540 int rc = 0;
541 unsigned long flags;
542 u32 wait_cnt;
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530543 bool prev_pwrsave, curr_pwrsave;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700544
545 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
546 spin_lock_irqsave(&host->lock, flags);
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530547 prev_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
548 CORE_CLK_PWRSAVE);
549 curr_pwrsave = prev_pwrsave;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700550 /*
551 * Make sure that clock is always enabled when DLL
552 * tuning is in progress. Keeping PWRSAVE ON may
553 * turn off the clock. So let's disable the PWRSAVE
554 * here and re-enable it once tuning is completed.
555 */
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530556 if (prev_pwrsave) {
557 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
558 & ~CORE_CLK_PWRSAVE),
559 host->ioaddr + CORE_VENDOR_SPEC);
560 curr_pwrsave = false;
561 }
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700562
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800563 if (msm_host->use_updated_dll_reset) {
564 /* Disable the DLL clock */
565 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
566 & ~CORE_CK_OUT_EN),
567 host->ioaddr + CORE_DLL_CONFIG);
568
569 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
570 | CORE_DLL_CLOCK_DISABLE),
571 host->ioaddr + CORE_DLL_CONFIG_2);
572 }
573
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700574 /* Write 1 to DLL_RST bit of DLL_CONFIG register */
575 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
576 | CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
577
578 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
579 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
580 | CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
581 msm_cm_dll_set_freq(host);
582
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800583 if (msm_host->use_updated_dll_reset) {
584 u32 mclk_freq = 0;
585
586 if ((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
587 & CORE_FLL_CYCLE_CNT))
588 mclk_freq = (u32) ((host->clock / TCXO_FREQ) * 8);
589 else
590 mclk_freq = (u32) ((host->clock / TCXO_FREQ) * 4);
591
592 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
593 & ~(0xFF << 10)) | (mclk_freq << 10)),
594 host->ioaddr + CORE_DLL_CONFIG_2);
595 /* wait for 5us before enabling DLL clock */
596 udelay(5);
597 }
598
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700599 /* Write 0 to DLL_RST bit of DLL_CONFIG register */
600 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
601 & ~CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
602
603 /* Write 0 to DLL_PDN bit of DLL_CONFIG register */
604 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
605 & ~CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
606
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800607 if (msm_host->use_updated_dll_reset) {
608 msm_cm_dll_set_freq(host);
609 /* Enable the DLL clock */
610 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
611 & ~CORE_DLL_CLOCK_DISABLE),
612 host->ioaddr + CORE_DLL_CONFIG_2);
613 }
614
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700615 /* Set DLL_EN bit to 1. */
616 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
617 | CORE_DLL_EN), host->ioaddr + CORE_DLL_CONFIG);
618
619 /* Set CK_OUT_EN bit to 1. */
620 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
621 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
622
623 wait_cnt = 50;
624 /* Wait until DLL_LOCK bit of DLL_STATUS register becomes '1' */
625 while (!(readl_relaxed(host->ioaddr + CORE_DLL_STATUS) &
626 CORE_DLL_LOCK)) {
627 /* max. wait for 50us sec for LOCK bit to be set */
628 if (--wait_cnt == 0) {
629 pr_err("%s: %s: DLL failed to LOCK\n",
630 mmc_hostname(mmc), __func__);
631 rc = -ETIMEDOUT;
632 goto out;
633 }
634 /* wait for 1us before polling again */
635 udelay(1);
636 }
637
638out:
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530639 /* Restore the correct PWRSAVE state */
640 if (prev_pwrsave ^ curr_pwrsave) {
641 u32 reg = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC);
642
643 if (prev_pwrsave)
644 reg |= CORE_CLK_PWRSAVE;
645 else
646 reg &= ~CORE_CLK_PWRSAVE;
647
648 writel_relaxed(reg, host->ioaddr + CORE_VENDOR_SPEC);
649 }
650
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700651 spin_unlock_irqrestore(&host->lock, flags);
652 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
653 return rc;
654}
655
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700656static int sdhci_msm_cdclp533_calibration(struct sdhci_host *host)
657{
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700658 u32 calib_done;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700659 int ret = 0;
660 int cdc_err = 0;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700661
662 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
663
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700664 /* Write 0 to CDC_T4_DLY_SEL field in VENDOR_SPEC_DDR200_CFG */
665 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
666 & ~CORE_CDC_T4_DLY_SEL),
667 host->ioaddr + CORE_DDR_200_CFG);
668
669 /* Write 0 to CDC_SWITCH_BYPASS_OFF field in CORE_CSR_CDC_GEN_CFG */
670 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG)
671 & ~CORE_CDC_SWITCH_BYPASS_OFF),
672 host->ioaddr + CORE_CSR_CDC_GEN_CFG);
673
674 /* Write 1 to CDC_SWITCH_RC_EN field in CORE_CSR_CDC_GEN_CFG */
675 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG)
676 | CORE_CDC_SWITCH_RC_EN),
677 host->ioaddr + CORE_CSR_CDC_GEN_CFG);
678
679 /* Write 0 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
680 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
681 & ~CORE_START_CDC_TRAFFIC),
682 host->ioaddr + CORE_DDR_200_CFG);
683
684 /*
685 * Perform CDC Register Initialization Sequence
686 *
687 * CORE_CSR_CDC_CTLR_CFG0 0x11800EC
688 * CORE_CSR_CDC_CTLR_CFG1 0x3011111
689 * CORE_CSR_CDC_CAL_TIMER_CFG0 0x1201000
690 * CORE_CSR_CDC_CAL_TIMER_CFG1 0x4
691 * CORE_CSR_CDC_REFCOUNT_CFG 0xCB732020
692 * CORE_CSR_CDC_COARSE_CAL_CFG 0xB19
693 * CORE_CSR_CDC_DELAY_CFG 0x3AC
694 * CORE_CDC_OFFSET_CFG 0x0
695 * CORE_CDC_SLAVE_DDA_CFG 0x16334
696 */
697
698 writel_relaxed(0x11800EC, host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
699 writel_relaxed(0x3011111, host->ioaddr + CORE_CSR_CDC_CTLR_CFG1);
700 writel_relaxed(0x1201000, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
701 writel_relaxed(0x4, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG1);
702 writel_relaxed(0xCB732020, host->ioaddr + CORE_CSR_CDC_REFCOUNT_CFG);
703 writel_relaxed(0xB19, host->ioaddr + CORE_CSR_CDC_COARSE_CAL_CFG);
Subhash Jadavanibe406d92014-06-17 16:47:48 -0700704 writel_relaxed(0x4E2, host->ioaddr + CORE_CSR_CDC_DELAY_CFG);
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700705 writel_relaxed(0x0, host->ioaddr + CORE_CDC_OFFSET_CFG);
706 writel_relaxed(0x16334, host->ioaddr + CORE_CDC_SLAVE_DDA_CFG);
707
708 /* CDC HW Calibration */
709
710 /* Write 1 to SW_TRIG_FULL_CALIB field in CORE_CSR_CDC_CTLR_CFG0 */
711 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
712 | CORE_SW_TRIG_FULL_CALIB),
713 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
714
715 /* Write 0 to SW_TRIG_FULL_CALIB field in CORE_CSR_CDC_CTLR_CFG0 */
716 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
717 & ~CORE_SW_TRIG_FULL_CALIB),
718 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
719
720 /* Write 1 to HW_AUTOCAL_ENA field in CORE_CSR_CDC_CTLR_CFG0 */
721 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
722 | CORE_HW_AUTOCAL_ENA),
723 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
724
725 /* Write 1 to TIMER_ENA field in CORE_CSR_CDC_CAL_TIMER_CFG0 */
726 writel_relaxed((readl_relaxed(host->ioaddr +
727 CORE_CSR_CDC_CAL_TIMER_CFG0) | CORE_TIMER_ENA),
728 host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
729
730 mb();
731
732 /* Poll on CALIBRATION_DONE field in CORE_CSR_CDC_STATUS0 to be 1 */
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700733 ret = readl_poll_timeout(host->ioaddr + CORE_CSR_CDC_STATUS0,
734 calib_done, (calib_done & CORE_CALIBRATION_DONE), 1, 50);
735
736 if (ret == -ETIMEDOUT) {
737 pr_err("%s: %s: CDC Calibration was not completed\n",
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700738 mmc_hostname(host->mmc), __func__);
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700739 goto out;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700740 }
741
742 /* Verify CDC_ERROR_CODE field in CORE_CSR_CDC_STATUS0 is 0 */
743 cdc_err = readl_relaxed(host->ioaddr + CORE_CSR_CDC_STATUS0)
744 & CORE_CDC_ERROR_CODE_MASK;
745 if (cdc_err) {
746 pr_err("%s: %s: CDC Error Code %d\n",
747 mmc_hostname(host->mmc), __func__, cdc_err);
748 ret = -EINVAL;
749 goto out;
750 }
751
752 /* Write 1 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
753 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
754 | CORE_START_CDC_TRAFFIC),
755 host->ioaddr + CORE_DDR_200_CFG);
756out:
757 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
758 __func__, ret);
759 return ret;
760}
761
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700762static int sdhci_msm_cm_dll_sdc4_calibration(struct sdhci_host *host)
763{
Ritesh Harjani764065e2015-05-13 14:14:45 +0530764 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
765 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700766 u32 dll_status;
767 int ret = 0;
768
769 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
770
771 /*
772 * Currently the CORE_DDR_CONFIG register defaults to desired
773 * configuration on reset. Currently reprogramming the power on
774 * reset (POR) value in case it might have been modified by
775 * bootloaders. In the future, if this changes, then the desired
776 * values will need to be programmed appropriately.
777 */
778 writel_relaxed(DDR_CONFIG_POR_VAL, host->ioaddr + CORE_DDR_CONFIG);
779
Ritesh Harjaniea709662015-05-27 15:40:24 +0530780 if (msm_host->enhanced_strobe)
781 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
782 | CORE_CMDIN_RCLK_EN),
783 host->ioaddr + CORE_DDR_200_CFG);
784
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700785 /* Write 1 to DDR_CAL_EN field in CORE_DLL_CONFIG_2 */
786 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
787 | CORE_DDR_CAL_EN),
788 host->ioaddr + CORE_DLL_CONFIG_2);
789
790 /* Poll on DDR_DLL_LOCK bit in CORE_DLL_STATUS to be set */
791 ret = readl_poll_timeout(host->ioaddr + CORE_DLL_STATUS,
792 dll_status, (dll_status & CORE_DDR_DLL_LOCK), 10, 1000);
793
794 if (ret == -ETIMEDOUT) {
795 pr_err("%s: %s: CM_DLL_SDC4 Calibration was not completed\n",
796 mmc_hostname(host->mmc), __func__);
797 goto out;
798 }
799
Ritesh Harjani764065e2015-05-13 14:14:45 +0530800 /*
801 * set CORE_PWRSAVE_DLL bit in CORE_VENDOR_SPEC3.
802 * when MCLK is gated OFF, it is not gated for less than 0.5us
803 * and MCLK must be switched on for at-least 1us before DATA
804 * starts coming. Controllers with 14lpp tech DLL cannot
805 * guarantee above requirement. So PWRSAVE_DLL should not be
806 * turned on for host controllers using this DLL.
807 */
808 if (!msm_host->use_14lpp_dll)
809 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC3)
810 | CORE_PWRSAVE_DLL),
811 host->ioaddr + CORE_VENDOR_SPEC3);
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700812 mb();
813out:
814 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
815 __func__, ret);
816 return ret;
817}
818
Ritesh Harjaniea709662015-05-27 15:40:24 +0530819static int sdhci_msm_enhanced_strobe(struct sdhci_host *host)
820{
821 int ret = 0;
822 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
823 struct sdhci_msm_host *msm_host = pltfm_host->priv;
824 struct mmc_host *mmc = host->mmc;
825
826 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
827
828 if (!msm_host->enhanced_strobe) {
829 pr_debug("%s: host does not support hs400 enhanced strobe\n",
830 mmc_hostname(mmc));
831 return -EINVAL;
832 }
833
834 if (msm_host->calibration_done ||
835 !(mmc->ios.timing == MMC_TIMING_MMC_HS400)) {
836 return 0;
837 }
838
839 /*
840 * Reset the tuning block.
841 */
842 ret = msm_init_cm_dll(host);
843 if (ret)
844 goto out;
845
846 ret = sdhci_msm_cm_dll_sdc4_calibration(host);
847out:
848 if (!ret)
849 msm_host->calibration_done = true;
850 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
851 __func__, ret);
852 return ret;
853}
854
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700855static int sdhci_msm_hs400_dll_calibration(struct sdhci_host *host)
856{
857 int ret = 0;
858 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
859 struct sdhci_msm_host *msm_host = pltfm_host->priv;
860
861 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
862
863 /*
864 * Retuning in HS400 (DDR mode) will fail, just reset the
865 * tuning block and restore the saved tuning phase.
866 */
867 ret = msm_init_cm_dll(host);
868 if (ret)
869 goto out;
870
871 /* Set the selected phase in delay line hw block */
872 ret = msm_config_cm_dll_phase(host, msm_host->saved_tuning_phase);
873 if (ret)
874 goto out;
875
Krishna Konda0e8efba2014-06-23 14:50:38 -0700876 /* Write 1 to CMD_DAT_TRACK_SEL field in DLL_CONFIG */
877 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
878 | CORE_CMD_DAT_TRACK_SEL),
879 host->ioaddr + CORE_DLL_CONFIG);
880
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700881 if (msm_host->use_cdclp533)
882 /* Calibrate CDCLP533 DLL HW */
883 ret = sdhci_msm_cdclp533_calibration(host);
884 else
885 /* Calibrate CM_DLL_SDC4 HW */
886 ret = sdhci_msm_cm_dll_sdc4_calibration(host);
887out:
888 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
889 __func__, ret);
890 return ret;
891}
892
Krishna Konda96e6b112013-10-28 15:25:03 -0700893static void sdhci_msm_set_mmc_drv_type(struct sdhci_host *host, u32 opcode,
894 u8 drv_type)
895{
896 struct mmc_command cmd = {0};
897 struct mmc_request mrq = {NULL};
898 struct mmc_host *mmc = host->mmc;
899 u8 val = ((drv_type << 4) | 2);
900
901 cmd.opcode = MMC_SWITCH;
902 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
903 (EXT_CSD_HS_TIMING << 16) |
904 (val << 8) |
905 EXT_CSD_CMD_SET_NORMAL;
906 cmd.flags = MMC_CMD_AC | MMC_RSP_R1B;
907 /* 1 sec */
908 cmd.busy_timeout = 1000 * 1000;
909
910 memset(cmd.resp, 0, sizeof(cmd.resp));
911 cmd.retries = 3;
912
913 mrq.cmd = &cmd;
914 cmd.data = NULL;
915
916 mmc_wait_for_req(mmc, &mrq);
917 pr_debug("%s: %s: set card drive type to %d\n",
918 mmc_hostname(mmc), __func__,
919 drv_type);
920}
921
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700922int sdhci_msm_execute_tuning(struct sdhci_host *host, u32 opcode)
923{
924 unsigned long flags;
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530925 int tuning_seq_cnt = 3;
Krishna Konda96e6b112013-10-28 15:25:03 -0700926 u8 phase, *data_buf, tuned_phases[NUM_TUNING_PHASES], tuned_phase_cnt;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700927 const u32 *tuning_block_pattern = tuning_block_64;
928 int size = sizeof(tuning_block_64); /* Tuning pattern size in bytes */
929 int rc;
930 struct mmc_host *mmc = host->mmc;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530931 struct mmc_ios ios = host->mmc->ios;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700932 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
933 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Krishna Konda96e6b112013-10-28 15:25:03 -0700934 u8 drv_type = 0;
935 bool drv_type_changed = false;
936 struct mmc_card *card = host->mmc->card;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530937
938 /*
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700939 * Tuning is required for SDR104, HS200 and HS400 cards and
940 * if clock frequency is greater than 100MHz in these modes.
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530941 */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700942 if (host->clock <= CORE_FREQ_100MHZ ||
943 !((ios.timing == MMC_TIMING_MMC_HS400) ||
944 (ios.timing == MMC_TIMING_MMC_HS200) ||
945 (ios.timing == MMC_TIMING_UHS_SDR104)))
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530946 return 0;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700947
948 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700949
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700950 /* CDC/SDC4 DLL HW calibration is only required for HS400 mode*/
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700951 if (msm_host->tuning_done && !msm_host->calibration_done &&
952 (mmc->ios.timing == MMC_TIMING_MMC_HS400)) {
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700953 rc = sdhci_msm_hs400_dll_calibration(host);
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700954 spin_lock_irqsave(&host->lock, flags);
955 if (!rc)
956 msm_host->calibration_done = true;
957 spin_unlock_irqrestore(&host->lock, flags);
958 goto out;
959 }
960
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700961 spin_lock_irqsave(&host->lock, flags);
962
963 if ((opcode == MMC_SEND_TUNING_BLOCK_HS200) &&
964 (mmc->ios.bus_width == MMC_BUS_WIDTH_8)) {
965 tuning_block_pattern = tuning_block_128;
966 size = sizeof(tuning_block_128);
967 }
968 spin_unlock_irqrestore(&host->lock, flags);
969
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700970 data_buf = kmalloc(size, GFP_KERNEL);
971 if (!data_buf) {
972 rc = -ENOMEM;
973 goto out;
974 }
975
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530976retry:
Krishna Konda96e6b112013-10-28 15:25:03 -0700977 tuned_phase_cnt = 0;
978
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530979 /* first of all reset the tuning block */
980 rc = msm_init_cm_dll(host);
981 if (rc)
982 goto kfree;
983
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700984 phase = 0;
985 do {
986 struct mmc_command cmd = {0};
987 struct mmc_data data = {0};
988 struct mmc_request mrq = {
989 .cmd = &cmd,
990 .data = &data
991 };
992 struct scatterlist sg;
993
994 /* set the phase in delay line hw block */
995 rc = msm_config_cm_dll_phase(host, phase);
996 if (rc)
997 goto kfree;
998
999 cmd.opcode = opcode;
1000 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1001
1002 data.blksz = size;
1003 data.blocks = 1;
1004 data.flags = MMC_DATA_READ;
1005 data.timeout_ns = 1000 * 1000 * 1000; /* 1 sec */
1006
1007 data.sg = &sg;
1008 data.sg_len = 1;
1009 sg_init_one(&sg, data_buf, size);
1010 memset(data_buf, 0, size);
1011 mmc_wait_for_req(mmc, &mrq);
1012
1013 if (!cmd.error && !data.error &&
1014 !memcmp(data_buf, tuning_block_pattern, size)) {
1015 /* tuning is successful at this tuning point */
1016 tuned_phases[tuned_phase_cnt++] = phase;
Krishna Konda96e6b112013-10-28 15:25:03 -07001017 pr_debug("%s: %s: found *** good *** phase = %d\n",
1018 mmc_hostname(mmc), __func__, phase);
1019 } else {
1020 pr_debug("%s: %s: found ## bad ## phase = %d\n",
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001021 mmc_hostname(mmc), __func__, phase);
1022 }
1023 } while (++phase < 16);
1024
Sahitya Tummaladfdb4af2014-04-01 14:29:13 +05301025 if ((tuned_phase_cnt == NUM_TUNING_PHASES) &&
1026 card && mmc_card_mmc(card)) {
Krishna Konda96e6b112013-10-28 15:25:03 -07001027 /*
1028 * If all phases pass then its a problem. So change the card's
1029 * drive type to a different value, if supported and repeat
1030 * tuning until at least one phase fails. Then set the original
1031 * drive type back.
1032 *
1033 * If all the phases still pass after trying all possible
1034 * drive types, then one of those 16 phases will be picked.
1035 * This is no different from what was going on before the
1036 * modification to change drive type and retune.
1037 */
1038 pr_debug("%s: tuned phases count: %d\n", mmc_hostname(mmc),
1039 tuned_phase_cnt);
1040
1041 /* set drive type to other value . default setting is 0x0 */
1042 while (++drv_type <= MAX_DRV_TYPES_SUPPORTED_HS200) {
1043 if (card->ext_csd.raw_driver_strength &
1044 (1 << drv_type)) {
1045 sdhci_msm_set_mmc_drv_type(host, opcode,
1046 drv_type);
1047 if (!drv_type_changed)
1048 drv_type_changed = true;
1049 goto retry;
1050 }
1051 }
1052 }
1053
1054 /* reset drive type to default (50 ohm) if changed */
1055 if (drv_type_changed)
1056 sdhci_msm_set_mmc_drv_type(host, opcode, 0);
1057
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001058 if (tuned_phase_cnt) {
1059 rc = msm_find_most_appropriate_phase(host, tuned_phases,
1060 tuned_phase_cnt);
1061 if (rc < 0)
1062 goto kfree;
1063 else
1064 phase = (u8)rc;
1065
1066 /*
1067 * Finally set the selected phase in delay
1068 * line hw block.
1069 */
1070 rc = msm_config_cm_dll_phase(host, phase);
1071 if (rc)
1072 goto kfree;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07001073 msm_host->saved_tuning_phase = phase;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001074 pr_debug("%s: %s: finally setting the tuning phase to %d\n",
1075 mmc_hostname(mmc), __func__, phase);
1076 } else {
Sahitya Tummala9fe16532013-06-13 10:36:57 +05301077 if (--tuning_seq_cnt)
1078 goto retry;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001079 /* tuning failed */
1080 pr_err("%s: %s: no tuning point found\n",
1081 mmc_hostname(mmc), __func__);
Sahitya Tummala9fe16532013-06-13 10:36:57 +05301082 rc = -EIO;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001083 }
1084
1085kfree:
1086 kfree(data_buf);
1087out:
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07001088 spin_lock_irqsave(&host->lock, flags);
1089 if (!rc)
1090 msm_host->tuning_done = true;
1091 spin_unlock_irqrestore(&host->lock, flags);
1092 pr_debug("%s: Exit %s, err(%d)\n", mmc_hostname(mmc), __func__, rc);
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001093 return rc;
1094}
1095
Asutosh Das0ef24812012-12-18 16:14:02 +05301096static int sdhci_msm_setup_gpio(struct sdhci_msm_pltfm_data *pdata, bool enable)
1097{
1098 struct sdhci_msm_gpio_data *curr;
1099 int i, ret = 0;
1100
1101 curr = pdata->pin_data->gpio_data;
1102 for (i = 0; i < curr->size; i++) {
1103 if (!gpio_is_valid(curr->gpio[i].no)) {
1104 ret = -EINVAL;
1105 pr_err("%s: Invalid gpio = %d\n", __func__,
1106 curr->gpio[i].no);
1107 goto free_gpios;
1108 }
1109 if (enable) {
1110 ret = gpio_request(curr->gpio[i].no,
1111 curr->gpio[i].name);
1112 if (ret) {
1113 pr_err("%s: gpio_request(%d, %s) failed %d\n",
1114 __func__, curr->gpio[i].no,
1115 curr->gpio[i].name, ret);
1116 goto free_gpios;
1117 }
1118 curr->gpio[i].is_enabled = true;
1119 } else {
1120 gpio_free(curr->gpio[i].no);
1121 curr->gpio[i].is_enabled = false;
1122 }
1123 }
1124 return ret;
1125
1126free_gpios:
1127 for (i--; i >= 0; i--) {
1128 gpio_free(curr->gpio[i].no);
1129 curr->gpio[i].is_enabled = false;
1130 }
1131 return ret;
1132}
1133
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301134static int sdhci_msm_setup_pinctrl(struct sdhci_msm_pltfm_data *pdata,
1135 bool enable)
1136{
1137 int ret = 0;
1138
1139 if (enable)
1140 ret = pinctrl_select_state(pdata->pctrl_data->pctrl,
1141 pdata->pctrl_data->pins_active);
1142 else
1143 ret = pinctrl_select_state(pdata->pctrl_data->pctrl,
1144 pdata->pctrl_data->pins_sleep);
1145
1146 if (ret < 0)
1147 pr_err("%s state for pinctrl failed with %d\n",
1148 enable ? "Enabling" : "Disabling", ret);
1149
1150 return ret;
1151}
1152
Asutosh Das0ef24812012-12-18 16:14:02 +05301153static int sdhci_msm_setup_pins(struct sdhci_msm_pltfm_data *pdata, bool enable)
1154{
1155 int ret = 0;
1156
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301157 if (pdata->pin_cfg_sts == enable) {
Asutosh Das0ef24812012-12-18 16:14:02 +05301158 return 0;
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301159 } else if (pdata->pctrl_data) {
1160 ret = sdhci_msm_setup_pinctrl(pdata, enable);
1161 goto out;
1162 } else if (!pdata->pin_data) {
1163 return 0;
1164 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301165
Sahitya Tummala1cd7e072014-02-14 13:19:01 +05301166 if (pdata->pin_data->is_gpio)
1167 ret = sdhci_msm_setup_gpio(pdata, enable);
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301168out:
Asutosh Das0ef24812012-12-18 16:14:02 +05301169 if (!ret)
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301170 pdata->pin_cfg_sts = enable;
Asutosh Das0ef24812012-12-18 16:14:02 +05301171
1172 return ret;
1173}
1174
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301175static int sdhci_msm_dt_get_array(struct device *dev, const char *prop_name,
1176 u32 **out, int *len, u32 size)
1177{
1178 int ret = 0;
1179 struct device_node *np = dev->of_node;
1180 size_t sz;
1181 u32 *arr = NULL;
1182
1183 if (!of_get_property(np, prop_name, len)) {
1184 ret = -EINVAL;
1185 goto out;
1186 }
1187 sz = *len = *len / sizeof(*arr);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07001188 if (sz <= 0 || (size > 0 && (sz > size))) {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301189 dev_err(dev, "%s invalid size\n", prop_name);
1190 ret = -EINVAL;
1191 goto out;
1192 }
1193
1194 arr = devm_kzalloc(dev, sz * sizeof(*arr), GFP_KERNEL);
1195 if (!arr) {
1196 dev_err(dev, "%s failed allocating memory\n", prop_name);
1197 ret = -ENOMEM;
1198 goto out;
1199 }
1200
1201 ret = of_property_read_u32_array(np, prop_name, arr, sz);
1202 if (ret < 0) {
1203 dev_err(dev, "%s failed reading array %d\n", prop_name, ret);
1204 goto out;
1205 }
1206 *out = arr;
1207out:
1208 if (ret)
1209 *len = 0;
1210 return ret;
1211}
1212
Asutosh Das0ef24812012-12-18 16:14:02 +05301213#define MAX_PROP_SIZE 32
1214static int sdhci_msm_dt_parse_vreg_info(struct device *dev,
1215 struct sdhci_msm_reg_data **vreg_data, const char *vreg_name)
1216{
1217 int len, ret = 0;
1218 const __be32 *prop;
1219 char prop_name[MAX_PROP_SIZE];
1220 struct sdhci_msm_reg_data *vreg;
1221 struct device_node *np = dev->of_node;
1222
1223 snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", vreg_name);
1224 if (!of_parse_phandle(np, prop_name, 0)) {
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301225 dev_info(dev, "No vreg data found for %s\n", vreg_name);
Asutosh Das0ef24812012-12-18 16:14:02 +05301226 return ret;
1227 }
1228
1229 vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
1230 if (!vreg) {
1231 dev_err(dev, "No memory for vreg: %s\n", vreg_name);
1232 ret = -ENOMEM;
1233 return ret;
1234 }
1235
1236 vreg->name = vreg_name;
1237
1238 snprintf(prop_name, MAX_PROP_SIZE,
1239 "qcom,%s-always-on", vreg_name);
1240 if (of_get_property(np, prop_name, NULL))
1241 vreg->is_always_on = true;
1242
1243 snprintf(prop_name, MAX_PROP_SIZE,
1244 "qcom,%s-lpm-sup", vreg_name);
1245 if (of_get_property(np, prop_name, NULL))
1246 vreg->lpm_sup = true;
1247
1248 snprintf(prop_name, MAX_PROP_SIZE,
1249 "qcom,%s-voltage-level", vreg_name);
1250 prop = of_get_property(np, prop_name, &len);
1251 if (!prop || (len != (2 * sizeof(__be32)))) {
1252 dev_warn(dev, "%s %s property\n",
1253 prop ? "invalid format" : "no", prop_name);
1254 } else {
1255 vreg->low_vol_level = be32_to_cpup(&prop[0]);
1256 vreg->high_vol_level = be32_to_cpup(&prop[1]);
1257 }
1258
1259 snprintf(prop_name, MAX_PROP_SIZE,
1260 "qcom,%s-current-level", vreg_name);
1261 prop = of_get_property(np, prop_name, &len);
1262 if (!prop || (len != (2 * sizeof(__be32)))) {
1263 dev_warn(dev, "%s %s property\n",
1264 prop ? "invalid format" : "no", prop_name);
1265 } else {
1266 vreg->lpm_uA = be32_to_cpup(&prop[0]);
1267 vreg->hpm_uA = be32_to_cpup(&prop[1]);
1268 }
1269
1270 *vreg_data = vreg;
1271 dev_dbg(dev, "%s: %s %s vol=[%d %d]uV, curr=[%d %d]uA\n",
1272 vreg->name, vreg->is_always_on ? "always_on," : "",
1273 vreg->lpm_sup ? "lpm_sup," : "", vreg->low_vol_level,
1274 vreg->high_vol_level, vreg->lpm_uA, vreg->hpm_uA);
1275
1276 return ret;
1277}
1278
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301279static int sdhci_msm_parse_pinctrl_info(struct device *dev,
1280 struct sdhci_msm_pltfm_data *pdata)
1281{
1282 struct sdhci_pinctrl_data *pctrl_data;
1283 struct pinctrl *pctrl;
1284 int ret = 0;
1285
1286 /* Try to obtain pinctrl handle */
1287 pctrl = devm_pinctrl_get(dev);
1288 if (IS_ERR(pctrl)) {
1289 ret = PTR_ERR(pctrl);
1290 goto out;
1291 }
1292 pctrl_data = devm_kzalloc(dev, sizeof(*pctrl_data), GFP_KERNEL);
1293 if (!pctrl_data) {
1294 dev_err(dev, "No memory for sdhci_pinctrl_data\n");
1295 ret = -ENOMEM;
1296 goto out;
1297 }
1298 pctrl_data->pctrl = pctrl;
1299 /* Look-up and keep the states handy to be used later */
1300 pctrl_data->pins_active = pinctrl_lookup_state(
1301 pctrl_data->pctrl, "active");
1302 if (IS_ERR(pctrl_data->pins_active)) {
1303 ret = PTR_ERR(pctrl_data->pins_active);
1304 dev_err(dev, "Could not get active pinstates, err:%d\n", ret);
1305 goto out;
1306 }
1307 pctrl_data->pins_sleep = pinctrl_lookup_state(
1308 pctrl_data->pctrl, "sleep");
1309 if (IS_ERR(pctrl_data->pins_sleep)) {
1310 ret = PTR_ERR(pctrl_data->pins_sleep);
1311 dev_err(dev, "Could not get sleep pinstates, err:%d\n", ret);
1312 goto out;
1313 }
1314 pdata->pctrl_data = pctrl_data;
1315out:
1316 return ret;
1317}
1318
Asutosh Das0ef24812012-12-18 16:14:02 +05301319#define GPIO_NAME_MAX_LEN 32
1320static int sdhci_msm_dt_parse_gpio_info(struct device *dev,
1321 struct sdhci_msm_pltfm_data *pdata)
1322{
1323 int ret = 0, cnt, i;
1324 struct sdhci_msm_pin_data *pin_data;
1325 struct device_node *np = dev->of_node;
1326
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301327 ret = sdhci_msm_parse_pinctrl_info(dev, pdata);
1328 if (!ret) {
1329 goto out;
1330 } else if (ret == -EPROBE_DEFER) {
1331 dev_err(dev, "Pinctrl framework not registered, err:%d\n", ret);
1332 goto out;
1333 } else {
1334 dev_err(dev, "Parsing Pinctrl failed with %d, falling back on GPIO lib\n",
1335 ret);
Sahitya Tummala1cd7e072014-02-14 13:19:01 +05301336 ret = 0;
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301337 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301338 pin_data = devm_kzalloc(dev, sizeof(*pin_data), GFP_KERNEL);
1339 if (!pin_data) {
1340 dev_err(dev, "No memory for pin_data\n");
1341 ret = -ENOMEM;
1342 goto out;
1343 }
1344
1345 cnt = of_gpio_count(np);
1346 if (cnt > 0) {
1347 pin_data->gpio_data = devm_kzalloc(dev,
1348 sizeof(struct sdhci_msm_gpio_data), GFP_KERNEL);
1349 if (!pin_data->gpio_data) {
1350 dev_err(dev, "No memory for gpio_data\n");
1351 ret = -ENOMEM;
1352 goto out;
1353 }
1354 pin_data->gpio_data->size = cnt;
1355 pin_data->gpio_data->gpio = devm_kzalloc(dev, cnt *
1356 sizeof(struct sdhci_msm_gpio), GFP_KERNEL);
1357
1358 if (!pin_data->gpio_data->gpio) {
1359 dev_err(dev, "No memory for gpio\n");
1360 ret = -ENOMEM;
1361 goto out;
1362 }
1363
1364 for (i = 0; i < cnt; i++) {
1365 const char *name = NULL;
1366 char result[GPIO_NAME_MAX_LEN];
1367 pin_data->gpio_data->gpio[i].no = of_get_gpio(np, i);
1368 of_property_read_string_index(np,
1369 "qcom,gpio-names", i, &name);
1370
1371 snprintf(result, GPIO_NAME_MAX_LEN, "%s-%s",
1372 dev_name(dev), name ? name : "?");
1373 pin_data->gpio_data->gpio[i].name = result;
1374 dev_dbg(dev, "%s: gpio[%s] = %d\n", __func__,
1375 pin_data->gpio_data->gpio[i].name,
1376 pin_data->gpio_data->gpio[i].no);
Asutosh Das0ef24812012-12-18 16:14:02 +05301377 }
1378 }
Sahitya Tummala1cd7e072014-02-14 13:19:01 +05301379 pdata->pin_data = pin_data;
Asutosh Das0ef24812012-12-18 16:14:02 +05301380out:
1381 if (ret)
1382 dev_err(dev, "%s failed with err %d\n", __func__, ret);
1383 return ret;
1384}
1385
Maya Erez994cf2f2014-10-21 20:22:04 +03001386#ifdef CONFIG_SMP
1387static void sdhci_msm_populate_affinity_type(struct sdhci_msm_pltfm_data *pdata,
1388 struct device_node *np)
1389{
1390 const char *cpu_affinity = NULL;
1391
1392 pdata->cpu_affinity_type = PM_QOS_REQ_AFFINE_IRQ;
1393 if (!of_property_read_string(np, "qcom,cpu-affinity",
1394 &cpu_affinity)) {
1395 if (!strcmp(cpu_affinity, "all_cores"))
1396 pdata->cpu_affinity_type = PM_QOS_REQ_ALL_CORES;
1397 else if (!strcmp(cpu_affinity, "affine_cores"))
1398 pdata->cpu_affinity_type = PM_QOS_REQ_AFFINE_CORES;
1399 else if (!strcmp(cpu_affinity, "affine_irq"))
1400 pdata->cpu_affinity_type = PM_QOS_REQ_AFFINE_IRQ;
1401 }
1402}
1403#else
1404static void sdhci_msm_populate_affinity_type(struct sdhci_msm_pltfm_data *pdata,
1405 struct device_node *np)
1406{
1407}
1408#endif
1409
Asutosh Das0ef24812012-12-18 16:14:02 +05301410/* Parse platform data */
Dov Levenglickc9033ab2015-03-10 16:00:56 +02001411static
1412struct sdhci_msm_pltfm_data *sdhci_msm_populate_pdata(struct device *dev,
1413 struct sdhci_msm_host *msm_host)
Asutosh Das0ef24812012-12-18 16:14:02 +05301414{
1415 struct sdhci_msm_pltfm_data *pdata = NULL;
1416 struct device_node *np = dev->of_node;
1417 u32 bus_width = 0;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05301418 u32 cpu_dma_latency;
Asutosh Das0ef24812012-12-18 16:14:02 +05301419 int len, i;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301420 int clk_table_len;
1421 u32 *clk_table = NULL;
Sujit Reddy Thumma1958d3d2013-06-03 09:54:32 +05301422 enum of_gpio_flags flags = OF_GPIO_ACTIVE_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05301423
1424 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1425 if (!pdata) {
1426 dev_err(dev, "failed to allocate memory for platform data\n");
1427 goto out;
1428 }
1429
Sujit Reddy Thumma1958d3d2013-06-03 09:54:32 +05301430 pdata->status_gpio = of_get_named_gpio_flags(np, "cd-gpios", 0, &flags);
1431 if (gpio_is_valid(pdata->status_gpio) & !(flags & OF_GPIO_ACTIVE_LOW))
1432 pdata->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
Sahitya Tummala581df132013-03-12 14:57:46 +05301433
Asutosh Das0ef24812012-12-18 16:14:02 +05301434 of_property_read_u32(np, "qcom,bus-width", &bus_width);
1435 if (bus_width == 8)
1436 pdata->mmc_bus_width = MMC_CAP_8_BIT_DATA;
1437 else if (bus_width == 4)
1438 pdata->mmc_bus_width = MMC_CAP_4_BIT_DATA;
1439 else {
1440 dev_notice(dev, "invalid bus-width, default to 1-bit mode\n");
1441 pdata->mmc_bus_width = 0;
1442 }
1443
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05301444 if (!of_property_read_u32(np, "qcom,cpu-dma-latency-us",
1445 &cpu_dma_latency))
1446 pdata->cpu_dma_latency_us = cpu_dma_latency;
Asutosh Das598a4d42014-02-05 16:38:23 +05301447 else
1448 pdata->cpu_dma_latency_us = MSM_MMC_DEFAULT_CPU_DMA_LATENCY;
Talel Shenhar7dc5f792015-05-18 12:12:48 +03001449
1450 if (sdhci_msm_dt_get_array(dev, "qcom,devfreq,freq-table",
1451 &msm_host->mmc->clk_scaling.freq_table,
1452 &msm_host->mmc->clk_scaling.freq_table_sz, 0))
1453 pr_debug("%s: no clock scaling frequencies were supplied\n",
1454 dev_name(dev));
1455 else if (!msm_host->mmc->clk_scaling.freq_table ||
1456 !msm_host->mmc->clk_scaling.freq_table_sz)
1457 dev_err(dev, "bad dts clock scaling frequencies\n");
1458
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301459 if (sdhci_msm_dt_get_array(dev, "qcom,clk-rates",
1460 &clk_table, &clk_table_len, 0)) {
1461 dev_err(dev, "failed parsing supported clock rates\n");
1462 goto out;
1463 }
1464 if (!clk_table || !clk_table_len) {
1465 dev_err(dev, "Invalid clock table\n");
1466 goto out;
1467 }
1468 pdata->sup_clk_table = clk_table;
1469 pdata->sup_clk_cnt = clk_table_len;
1470
Asutosh Das0ef24812012-12-18 16:14:02 +05301471 pdata->vreg_data = devm_kzalloc(dev, sizeof(struct
1472 sdhci_msm_slot_reg_data),
1473 GFP_KERNEL);
1474 if (!pdata->vreg_data) {
1475 dev_err(dev, "failed to allocate memory for vreg data\n");
1476 goto out;
1477 }
1478
1479 if (sdhci_msm_dt_parse_vreg_info(dev, &pdata->vreg_data->vdd_data,
1480 "vdd")) {
1481 dev_err(dev, "failed parsing vdd data\n");
1482 goto out;
1483 }
1484 if (sdhci_msm_dt_parse_vreg_info(dev,
1485 &pdata->vreg_data->vdd_io_data,
1486 "vdd-io")) {
1487 dev_err(dev, "failed parsing vdd-io data\n");
1488 goto out;
1489 }
1490
1491 if (sdhci_msm_dt_parse_gpio_info(dev, pdata)) {
1492 dev_err(dev, "failed parsing gpio data\n");
1493 goto out;
1494 }
1495
Asutosh Das0ef24812012-12-18 16:14:02 +05301496 len = of_property_count_strings(np, "qcom,bus-speed-mode");
1497
1498 for (i = 0; i < len; i++) {
1499 const char *name = NULL;
1500
1501 of_property_read_string_index(np,
1502 "qcom,bus-speed-mode", i, &name);
1503 if (!name)
1504 continue;
1505
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07001506 if (!strncmp(name, "HS400_1p8v", sizeof("HS400_1p8v")))
1507 pdata->caps2 |= MMC_CAP2_HS400_1_8V;
1508 else if (!strncmp(name, "HS400_1p2v", sizeof("HS400_1p2v")))
1509 pdata->caps2 |= MMC_CAP2_HS400_1_2V;
1510 else if (!strncmp(name, "HS200_1p8v", sizeof("HS200_1p8v")))
Asutosh Das0ef24812012-12-18 16:14:02 +05301511 pdata->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
1512 else if (!strncmp(name, "HS200_1p2v", sizeof("HS200_1p2v")))
1513 pdata->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
1514 else if (!strncmp(name, "DDR_1p8v", sizeof("DDR_1p8v")))
1515 pdata->caps |= MMC_CAP_1_8V_DDR
1516 | MMC_CAP_UHS_DDR50;
1517 else if (!strncmp(name, "DDR_1p2v", sizeof("DDR_1p2v")))
1518 pdata->caps |= MMC_CAP_1_2V_DDR
1519 | MMC_CAP_UHS_DDR50;
1520 }
1521
1522 if (of_get_property(np, "qcom,nonremovable", NULL))
1523 pdata->nonremovable = true;
1524
Guoping Yuf7c91332014-08-20 16:56:18 +08001525 if (of_get_property(np, "qcom,nonhotplug", NULL))
1526 pdata->nonhotplug = true;
1527
Venkat Gopalakrishnan9a62e042015-03-03 16:14:55 -08001528 pdata->largeaddressbus =
1529 of_property_read_bool(np, "qcom,large-address-bus");
1530
Maya Erez994cf2f2014-10-21 20:22:04 +03001531 sdhci_msm_populate_affinity_type(pdata, np);
1532
Dov Levenglickc9033ab2015-03-10 16:00:56 +02001533 if (of_property_read_bool(np, "qcom,wakeup-on-idle"))
1534 msm_host->mmc->wakeup_on_idle = true;
1535
Asutosh Das0ef24812012-12-18 16:14:02 +05301536 return pdata;
1537out:
1538 return NULL;
1539}
1540
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301541/* Returns required bandwidth in Bytes per Sec */
1542static unsigned int sdhci_get_bw_required(struct sdhci_host *host,
1543 struct mmc_ios *ios)
1544{
Sahitya Tummala2886c922013-04-03 18:03:31 +05301545 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1546 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1547
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301548 unsigned int bw;
1549
Sahitya Tummala2886c922013-04-03 18:03:31 +05301550 bw = msm_host->clk_rate;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301551 /*
1552 * For DDR mode, SDCC controller clock will be at
1553 * the double rate than the actual clock that goes to card.
1554 */
1555 if (ios->bus_width == MMC_BUS_WIDTH_4)
1556 bw /= 2;
1557 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1558 bw /= 8;
1559
1560 return bw;
1561}
1562
1563static int sdhci_msm_bus_get_vote_for_bw(struct sdhci_msm_host *host,
1564 unsigned int bw)
1565{
1566 unsigned int *table = host->pdata->voting_data->bw_vecs;
1567 unsigned int size = host->pdata->voting_data->bw_vecs_size;
1568 int i;
1569
1570 if (host->msm_bus_vote.is_max_bw_needed && bw)
1571 return host->msm_bus_vote.max_bw_vote;
1572
1573 for (i = 0; i < size; i++) {
1574 if (bw <= table[i])
1575 break;
1576 }
1577
1578 if (i && (i == size))
1579 i--;
1580
1581 return i;
1582}
1583
1584/*
1585 * This function must be called with host lock acquired.
1586 * Caller of this function should also ensure that msm bus client
1587 * handle is not null.
1588 */
1589static inline int sdhci_msm_bus_set_vote(struct sdhci_msm_host *msm_host,
1590 int vote,
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301591 unsigned long *flags)
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301592{
1593 struct sdhci_host *host = platform_get_drvdata(msm_host->pdev);
1594 int rc = 0;
1595
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301596 BUG_ON(!flags);
1597
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301598 if (vote != msm_host->msm_bus_vote.curr_vote) {
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301599 spin_unlock_irqrestore(&host->lock, *flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301600 rc = msm_bus_scale_client_update_request(
1601 msm_host->msm_bus_vote.client_handle, vote);
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301602 spin_lock_irqsave(&host->lock, *flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301603 if (rc) {
1604 pr_err("%s: msm_bus_scale_client_update_request() failed: bus_client_handle=0x%x, vote=%d, err=%d\n",
1605 mmc_hostname(host->mmc),
1606 msm_host->msm_bus_vote.client_handle, vote, rc);
1607 goto out;
1608 }
1609 msm_host->msm_bus_vote.curr_vote = vote;
1610 }
1611out:
1612 return rc;
1613}
1614
1615/*
1616 * Internal work. Work to set 0 bandwidth for msm bus.
1617 */
1618static void sdhci_msm_bus_work(struct work_struct *work)
1619{
1620 struct sdhci_msm_host *msm_host;
1621 struct sdhci_host *host;
1622 unsigned long flags;
1623
1624 msm_host = container_of(work, struct sdhci_msm_host,
1625 msm_bus_vote.vote_work.work);
1626 host = platform_get_drvdata(msm_host->pdev);
1627
1628 if (!msm_host->msm_bus_vote.client_handle)
1629 return;
1630
1631 spin_lock_irqsave(&host->lock, flags);
1632 /* don't vote for 0 bandwidth if any request is in progress */
1633 if (!host->mrq) {
1634 sdhci_msm_bus_set_vote(msm_host,
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301635 msm_host->msm_bus_vote.min_bw_vote, &flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301636 } else
1637 pr_warning("%s: %s: Transfer in progress. skipping bus voting to 0 bandwidth\n",
1638 mmc_hostname(host->mmc), __func__);
1639 spin_unlock_irqrestore(&host->lock, flags);
1640}
1641
1642/*
1643 * This function cancels any scheduled delayed work and sets the bus
1644 * vote based on bw (bandwidth) argument.
1645 */
1646static void sdhci_msm_bus_cancel_work_and_set_vote(struct sdhci_host *host,
1647 unsigned int bw)
1648{
1649 int vote;
1650 unsigned long flags;
1651 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1652 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1653
1654 cancel_delayed_work_sync(&msm_host->msm_bus_vote.vote_work);
1655 spin_lock_irqsave(&host->lock, flags);
1656 vote = sdhci_msm_bus_get_vote_for_bw(msm_host, bw);
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301657 sdhci_msm_bus_set_vote(msm_host, vote, &flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301658 spin_unlock_irqrestore(&host->lock, flags);
1659}
1660
1661#define MSM_MMC_BUS_VOTING_DELAY 200 /* msecs */
1662
1663/* This function queues a work which will set the bandwidth requiement to 0 */
1664static void sdhci_msm_bus_queue_work(struct sdhci_host *host)
1665{
1666 unsigned long flags;
1667 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1668 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1669
1670 spin_lock_irqsave(&host->lock, flags);
1671 if (msm_host->msm_bus_vote.min_bw_vote !=
1672 msm_host->msm_bus_vote.curr_vote)
1673 queue_delayed_work(system_wq,
1674 &msm_host->msm_bus_vote.vote_work,
1675 msecs_to_jiffies(MSM_MMC_BUS_VOTING_DELAY));
1676 spin_unlock_irqrestore(&host->lock, flags);
1677}
1678
1679static int sdhci_msm_bus_register(struct sdhci_msm_host *host,
1680 struct platform_device *pdev)
1681{
1682 int rc = 0;
1683 struct msm_bus_scale_pdata *bus_pdata;
1684
1685 struct sdhci_msm_bus_voting_data *data;
1686 struct device *dev = &pdev->dev;
1687
1688 data = devm_kzalloc(dev,
1689 sizeof(struct sdhci_msm_bus_voting_data), GFP_KERNEL);
1690 if (!data) {
1691 dev_err(&pdev->dev,
1692 "%s: failed to allocate memory\n", __func__);
1693 rc = -ENOMEM;
1694 goto out;
1695 }
1696 data->bus_pdata = msm_bus_cl_get_pdata(pdev);
1697 if (data->bus_pdata) {
1698 rc = sdhci_msm_dt_get_array(dev, "qcom,bus-bw-vectors-bps",
1699 &data->bw_vecs, &data->bw_vecs_size, 0);
1700 if (rc) {
1701 dev_err(&pdev->dev,
1702 "%s: Failed to get bus-bw-vectors-bps\n",
1703 __func__);
1704 goto out;
1705 }
1706 host->pdata->voting_data = data;
1707 }
1708 if (host->pdata->voting_data &&
1709 host->pdata->voting_data->bus_pdata &&
1710 host->pdata->voting_data->bw_vecs &&
1711 host->pdata->voting_data->bw_vecs_size) {
1712
1713 bus_pdata = host->pdata->voting_data->bus_pdata;
1714 host->msm_bus_vote.client_handle =
1715 msm_bus_scale_register_client(bus_pdata);
1716 if (!host->msm_bus_vote.client_handle) {
1717 dev_err(&pdev->dev, "msm_bus_scale_register_client()\n");
1718 rc = -EFAULT;
1719 goto out;
1720 }
1721 /* cache the vote index for minimum and maximum bandwidth */
1722 host->msm_bus_vote.min_bw_vote =
1723 sdhci_msm_bus_get_vote_for_bw(host, 0);
1724 host->msm_bus_vote.max_bw_vote =
1725 sdhci_msm_bus_get_vote_for_bw(host, UINT_MAX);
1726 } else {
1727 devm_kfree(dev, data);
1728 }
1729
1730out:
1731 return rc;
1732}
1733
1734static void sdhci_msm_bus_unregister(struct sdhci_msm_host *host)
1735{
1736 if (host->msm_bus_vote.client_handle)
1737 msm_bus_scale_unregister_client(
1738 host->msm_bus_vote.client_handle);
1739}
1740
1741static void sdhci_msm_bus_voting(struct sdhci_host *host, u32 enable)
1742{
1743 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1744 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1745 struct mmc_ios *ios = &host->mmc->ios;
1746 unsigned int bw;
1747
1748 if (!msm_host->msm_bus_vote.client_handle)
1749 return;
1750
1751 bw = sdhci_get_bw_required(host, ios);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301752 if (enable) {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301753 sdhci_msm_bus_cancel_work_and_set_vote(host, bw);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301754 } else {
1755 /*
1756 * If clock gating is enabled, then remove the vote
1757 * immediately because clocks will be disabled only
1758 * after SDHCI_MSM_MMC_CLK_GATE_DELAY and thus no
1759 * additional delay is required to remove the bus vote.
1760 */
1761#ifdef CONFIG_MMC_CLKGATE
1762 if (host->mmc->clkgate_delay)
1763 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
1764 else
1765#endif
1766 sdhci_msm_bus_queue_work(host);
1767 }
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301768}
1769
Asutosh Das0ef24812012-12-18 16:14:02 +05301770/* Regulator utility functions */
1771static int sdhci_msm_vreg_init_reg(struct device *dev,
1772 struct sdhci_msm_reg_data *vreg)
1773{
1774 int ret = 0;
1775
1776 /* check if regulator is already initialized? */
1777 if (vreg->reg)
1778 goto out;
1779
1780 /* Get the regulator handle */
1781 vreg->reg = devm_regulator_get(dev, vreg->name);
1782 if (IS_ERR(vreg->reg)) {
1783 ret = PTR_ERR(vreg->reg);
1784 pr_err("%s: devm_regulator_get(%s) failed. ret=%d\n",
1785 __func__, vreg->name, ret);
1786 goto out;
1787 }
1788
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301789 if (regulator_count_voltages(vreg->reg) > 0) {
1790 vreg->set_voltage_sup = true;
1791 /* sanity check */
1792 if (!vreg->high_vol_level || !vreg->hpm_uA) {
1793 pr_err("%s: %s invalid constraints specified\n",
1794 __func__, vreg->name);
1795 ret = -EINVAL;
1796 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301797 }
1798
1799out:
1800 return ret;
1801}
1802
1803static void sdhci_msm_vreg_deinit_reg(struct sdhci_msm_reg_data *vreg)
1804{
1805 if (vreg->reg)
1806 devm_regulator_put(vreg->reg);
1807}
1808
1809static int sdhci_msm_vreg_set_optimum_mode(struct sdhci_msm_reg_data
1810 *vreg, int uA_load)
1811{
1812 int ret = 0;
1813
1814 /*
1815 * regulators that do not support regulator_set_voltage also
1816 * do not support regulator_set_optimum_mode
1817 */
1818 if (vreg->set_voltage_sup) {
1819 ret = regulator_set_load(vreg->reg, uA_load);
1820 if (ret < 0)
1821 pr_err("%s: regulator_set_load(reg=%s,uA_load=%d) failed. ret=%d\n",
1822 __func__, vreg->name, uA_load, ret);
1823 else
1824 /*
1825 * regulator_set_load() can return non zero
1826 * value even for success case.
1827 */
1828 ret = 0;
1829 }
1830 return ret;
1831}
1832
1833static int sdhci_msm_vreg_set_voltage(struct sdhci_msm_reg_data *vreg,
1834 int min_uV, int max_uV)
1835{
1836 int ret = 0;
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301837 if (vreg->set_voltage_sup) {
1838 ret = regulator_set_voltage(vreg->reg, min_uV, max_uV);
1839 if (ret) {
1840 pr_err("%s: regulator_set_voltage(%s)failed. min_uV=%d,max_uV=%d,ret=%d\n",
Asutosh Das0ef24812012-12-18 16:14:02 +05301841 __func__, vreg->name, min_uV, max_uV, ret);
1842 }
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301843 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301844
1845 return ret;
1846}
1847
1848static int sdhci_msm_vreg_enable(struct sdhci_msm_reg_data *vreg)
1849{
1850 int ret = 0;
1851
1852 /* Put regulator in HPM (high power mode) */
1853 ret = sdhci_msm_vreg_set_optimum_mode(vreg, vreg->hpm_uA);
1854 if (ret < 0)
1855 return ret;
1856
1857 if (!vreg->is_enabled) {
1858 /* Set voltage level */
1859 ret = sdhci_msm_vreg_set_voltage(vreg, vreg->high_vol_level,
1860 vreg->high_vol_level);
1861 if (ret)
1862 return ret;
1863 }
1864 ret = regulator_enable(vreg->reg);
1865 if (ret) {
1866 pr_err("%s: regulator_enable(%s) failed. ret=%d\n",
1867 __func__, vreg->name, ret);
1868 return ret;
1869 }
1870 vreg->is_enabled = true;
1871 return ret;
1872}
1873
1874static int sdhci_msm_vreg_disable(struct sdhci_msm_reg_data *vreg)
1875{
1876 int ret = 0;
1877
1878 /* Never disable regulator marked as always_on */
1879 if (vreg->is_enabled && !vreg->is_always_on) {
1880 ret = regulator_disable(vreg->reg);
1881 if (ret) {
1882 pr_err("%s: regulator_disable(%s) failed. ret=%d\n",
1883 __func__, vreg->name, ret);
1884 goto out;
1885 }
1886 vreg->is_enabled = false;
1887
1888 ret = sdhci_msm_vreg_set_optimum_mode(vreg, 0);
1889 if (ret < 0)
1890 goto out;
1891
1892 /* Set min. voltage level to 0 */
1893 ret = sdhci_msm_vreg_set_voltage(vreg, 0, vreg->high_vol_level);
1894 if (ret)
1895 goto out;
1896 } else if (vreg->is_enabled && vreg->is_always_on) {
1897 if (vreg->lpm_sup) {
1898 /* Put always_on regulator in LPM (low power mode) */
1899 ret = sdhci_msm_vreg_set_optimum_mode(vreg,
1900 vreg->lpm_uA);
1901 if (ret < 0)
1902 goto out;
1903 }
1904 }
1905out:
1906 return ret;
1907}
1908
1909static int sdhci_msm_setup_vreg(struct sdhci_msm_pltfm_data *pdata,
1910 bool enable, bool is_init)
1911{
1912 int ret = 0, i;
1913 struct sdhci_msm_slot_reg_data *curr_slot;
1914 struct sdhci_msm_reg_data *vreg_table[2];
1915
1916 curr_slot = pdata->vreg_data;
1917 if (!curr_slot) {
1918 pr_debug("%s: vreg info unavailable,assuming the slot is powered by always on domain\n",
1919 __func__);
1920 goto out;
1921 }
1922
1923 vreg_table[0] = curr_slot->vdd_data;
1924 vreg_table[1] = curr_slot->vdd_io_data;
1925
1926 for (i = 0; i < ARRAY_SIZE(vreg_table); i++) {
1927 if (vreg_table[i]) {
1928 if (enable)
1929 ret = sdhci_msm_vreg_enable(vreg_table[i]);
1930 else
1931 ret = sdhci_msm_vreg_disable(vreg_table[i]);
1932 if (ret)
1933 goto out;
1934 }
1935 }
1936out:
1937 return ret;
1938}
1939
1940/*
1941 * Reset vreg by ensuring it is off during probe. A call
1942 * to enable vreg is needed to balance disable vreg
1943 */
1944static int sdhci_msm_vreg_reset(struct sdhci_msm_pltfm_data *pdata)
1945{
1946 int ret;
1947
1948 ret = sdhci_msm_setup_vreg(pdata, 1, true);
1949 if (ret)
1950 return ret;
1951 ret = sdhci_msm_setup_vreg(pdata, 0, true);
1952 return ret;
1953}
1954
1955/* This init function should be called only once for each SDHC slot */
1956static int sdhci_msm_vreg_init(struct device *dev,
1957 struct sdhci_msm_pltfm_data *pdata,
1958 bool is_init)
1959{
1960 int ret = 0;
1961 struct sdhci_msm_slot_reg_data *curr_slot;
1962 struct sdhci_msm_reg_data *curr_vdd_reg, *curr_vdd_io_reg;
1963
1964 curr_slot = pdata->vreg_data;
1965 if (!curr_slot)
1966 goto out;
1967
1968 curr_vdd_reg = curr_slot->vdd_data;
1969 curr_vdd_io_reg = curr_slot->vdd_io_data;
1970
1971 if (!is_init)
1972 /* Deregister all regulators from regulator framework */
1973 goto vdd_io_reg_deinit;
1974
1975 /*
1976 * Get the regulator handle from voltage regulator framework
1977 * and then try to set the voltage level for the regulator
1978 */
1979 if (curr_vdd_reg) {
1980 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_reg);
1981 if (ret)
1982 goto out;
1983 }
1984 if (curr_vdd_io_reg) {
1985 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_io_reg);
1986 if (ret)
1987 goto vdd_reg_deinit;
1988 }
1989 ret = sdhci_msm_vreg_reset(pdata);
1990 if (ret)
1991 dev_err(dev, "vreg reset failed (%d)\n", ret);
1992 goto out;
1993
1994vdd_io_reg_deinit:
1995 if (curr_vdd_io_reg)
1996 sdhci_msm_vreg_deinit_reg(curr_vdd_io_reg);
1997vdd_reg_deinit:
1998 if (curr_vdd_reg)
1999 sdhci_msm_vreg_deinit_reg(curr_vdd_reg);
2000out:
2001 return ret;
2002}
2003
2004
2005static int sdhci_msm_set_vdd_io_vol(struct sdhci_msm_pltfm_data *pdata,
2006 enum vdd_io_level level,
2007 unsigned int voltage_level)
2008{
2009 int ret = 0;
2010 int set_level;
2011 struct sdhci_msm_reg_data *vdd_io_reg;
2012
2013 if (!pdata->vreg_data)
2014 return ret;
2015
2016 vdd_io_reg = pdata->vreg_data->vdd_io_data;
2017 if (vdd_io_reg && vdd_io_reg->is_enabled) {
2018 switch (level) {
2019 case VDD_IO_LOW:
2020 set_level = vdd_io_reg->low_vol_level;
2021 break;
2022 case VDD_IO_HIGH:
2023 set_level = vdd_io_reg->high_vol_level;
2024 break;
2025 case VDD_IO_SET_LEVEL:
2026 set_level = voltage_level;
2027 break;
2028 default:
2029 pr_err("%s: invalid argument level = %d",
2030 __func__, level);
2031 ret = -EINVAL;
2032 return ret;
2033 }
2034 ret = sdhci_msm_vreg_set_voltage(vdd_io_reg, set_level,
2035 set_level);
2036 }
2037 return ret;
2038}
2039
2040static irqreturn_t sdhci_msm_pwr_irq(int irq, void *data)
2041{
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002042 struct sdhci_host *host = (struct sdhci_host *)data;
2043 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2044 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Das0ef24812012-12-18 16:14:02 +05302045 u8 irq_status = 0;
2046 u8 irq_ack = 0;
2047 int ret = 0;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302048 int pwr_state = 0, io_level = 0;
2049 unsigned long flags;
Asutosh Das0ef24812012-12-18 16:14:02 +05302050
2051 irq_status = readb_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
2052 pr_debug("%s: Received IRQ(%d), status=0x%x\n",
2053 mmc_hostname(msm_host->mmc), irq, irq_status);
2054
2055 /* Clear the interrupt */
2056 writeb_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
2057 /*
2058 * SDHC has core_mem and hc_mem device memory and these memory
2059 * addresses do not fall within 1KB region. Hence, any update to
2060 * core_mem address space would require an mb() to ensure this gets
2061 * completed before its next update to registers within hc_mem.
2062 */
2063 mb();
2064
2065 /* Handle BUS ON/OFF*/
2066 if (irq_status & CORE_PWRCTL_BUS_ON) {
2067 ret = sdhci_msm_setup_vreg(msm_host->pdata, true, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302068 if (!ret) {
Asutosh Das0ef24812012-12-18 16:14:02 +05302069 ret = sdhci_msm_setup_pins(msm_host->pdata, true);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302070 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
2071 VDD_IO_HIGH, 0);
2072 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302073 if (ret)
2074 irq_ack |= CORE_PWRCTL_BUS_FAIL;
2075 else
2076 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302077
2078 pwr_state = REQ_BUS_ON;
2079 io_level = REQ_IO_HIGH;
Asutosh Das0ef24812012-12-18 16:14:02 +05302080 }
2081 if (irq_status & CORE_PWRCTL_BUS_OFF) {
2082 ret = sdhci_msm_setup_vreg(msm_host->pdata, false, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302083 if (!ret) {
Asutosh Das0ef24812012-12-18 16:14:02 +05302084 ret = sdhci_msm_setup_pins(msm_host->pdata, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302085 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
2086 VDD_IO_LOW, 0);
2087 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302088 if (ret)
2089 irq_ack |= CORE_PWRCTL_BUS_FAIL;
2090 else
2091 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302092
2093 pwr_state = REQ_BUS_OFF;
2094 io_level = REQ_IO_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05302095 }
2096 /* Handle IO LOW/HIGH */
2097 if (irq_status & CORE_PWRCTL_IO_LOW) {
2098 /* Switch voltage Low */
2099 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_LOW, 0);
2100 if (ret)
2101 irq_ack |= CORE_PWRCTL_IO_FAIL;
2102 else
2103 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302104
2105 io_level = REQ_IO_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05302106 }
2107 if (irq_status & CORE_PWRCTL_IO_HIGH) {
2108 /* Switch voltage High */
2109 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_HIGH, 0);
2110 if (ret)
2111 irq_ack |= CORE_PWRCTL_IO_FAIL;
2112 else
2113 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302114
2115 io_level = REQ_IO_HIGH;
Asutosh Das0ef24812012-12-18 16:14:02 +05302116 }
2117
2118 /* ACK status to the core */
2119 writeb_relaxed(irq_ack, (msm_host->core_mem + CORE_PWRCTL_CTL));
2120 /*
2121 * SDHC has core_mem and hc_mem device memory and these memory
2122 * addresses do not fall within 1KB region. Hence, any update to
2123 * core_mem address space would require an mb() to ensure this gets
2124 * completed before its next update to registers within hc_mem.
2125 */
2126 mb();
2127
Krishna Konda46fd1432014-10-30 21:13:27 -07002128 if ((io_level & REQ_IO_HIGH) && (msm_host->caps_0 & CORE_3_0V_SUPPORT))
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002129 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2130 ~CORE_IO_PAD_PWR_SWITCH),
2131 host->ioaddr + CORE_VENDOR_SPEC);
Krishna Konda46fd1432014-10-30 21:13:27 -07002132 else if ((io_level & REQ_IO_LOW) ||
2133 (msm_host->caps_0 & CORE_1_8V_SUPPORT))
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002134 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
2135 CORE_IO_PAD_PWR_SWITCH),
2136 host->ioaddr + CORE_VENDOR_SPEC);
2137 mb();
2138
Asutosh Das0ef24812012-12-18 16:14:02 +05302139 pr_debug("%s: Handled IRQ(%d), ret=%d, ack=0x%x\n",
2140 mmc_hostname(msm_host->mmc), irq, ret, irq_ack);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302141 spin_lock_irqsave(&host->lock, flags);
2142 if (pwr_state)
2143 msm_host->curr_pwr_state = pwr_state;
2144 if (io_level)
2145 msm_host->curr_io_level = io_level;
2146 complete(&msm_host->pwr_irq_completion);
2147 spin_unlock_irqrestore(&host->lock, flags);
2148
Asutosh Das0ef24812012-12-18 16:14:02 +05302149 return IRQ_HANDLED;
2150}
2151
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302152static ssize_t
Sahitya Tummala5c55b932013-06-20 14:00:18 +05302153show_polling(struct device *dev, struct device_attribute *attr, char *buf)
2154{
2155 struct sdhci_host *host = dev_get_drvdata(dev);
2156 int poll;
2157 unsigned long flags;
2158
2159 spin_lock_irqsave(&host->lock, flags);
2160 poll = !!(host->mmc->caps & MMC_CAP_NEEDS_POLL);
2161 spin_unlock_irqrestore(&host->lock, flags);
2162
2163 return snprintf(buf, PAGE_SIZE, "%d\n", poll);
2164}
2165
2166static ssize_t
2167store_polling(struct device *dev, struct device_attribute *attr,
2168 const char *buf, size_t count)
2169{
2170 struct sdhci_host *host = dev_get_drvdata(dev);
2171 int value;
2172 unsigned long flags;
2173
2174 if (!kstrtou32(buf, 0, &value)) {
2175 spin_lock_irqsave(&host->lock, flags);
2176 if (value) {
2177 host->mmc->caps |= MMC_CAP_NEEDS_POLL;
2178 mmc_detect_change(host->mmc, 0);
2179 } else {
2180 host->mmc->caps &= ~MMC_CAP_NEEDS_POLL;
2181 }
2182 spin_unlock_irqrestore(&host->lock, flags);
2183 }
2184 return count;
2185}
2186
2187static ssize_t
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302188show_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
2189 char *buf)
2190{
2191 struct sdhci_host *host = dev_get_drvdata(dev);
2192 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2193 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2194
2195 return snprintf(buf, PAGE_SIZE, "%u\n",
2196 msm_host->msm_bus_vote.is_max_bw_needed);
2197}
2198
2199static ssize_t
2200store_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
2201 const char *buf, size_t count)
2202{
2203 struct sdhci_host *host = dev_get_drvdata(dev);
2204 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2205 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2206 uint32_t value;
2207 unsigned long flags;
2208
2209 if (!kstrtou32(buf, 0, &value)) {
2210 spin_lock_irqsave(&host->lock, flags);
2211 msm_host->msm_bus_vote.is_max_bw_needed = !!value;
2212 spin_unlock_irqrestore(&host->lock, flags);
2213 }
2214 return count;
2215}
2216
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302217static void sdhci_msm_check_power_status(struct sdhci_host *host, u32 req_type)
Asutosh Das0ef24812012-12-18 16:14:02 +05302218{
2219 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2220 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302221 unsigned long flags;
2222 bool done = false;
Sahitya Tummala481fbb02013-08-06 15:22:28 +05302223 u32 io_sig_sts;
Asutosh Das0ef24812012-12-18 16:14:02 +05302224
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302225 spin_lock_irqsave(&host->lock, flags);
2226 pr_debug("%s: %s: request %d curr_pwr_state %x curr_io_level %x\n",
2227 mmc_hostname(host->mmc), __func__, req_type,
2228 msm_host->curr_pwr_state, msm_host->curr_io_level);
Sahitya Tummala481fbb02013-08-06 15:22:28 +05302229 io_sig_sts = readl_relaxed(msm_host->core_mem + CORE_GENERICS);
2230 /*
2231 * The IRQ for request type IO High/Low will be generated when -
2232 * 1. SWITCHABLE_SIGNALLING_VOL is enabled in HW.
2233 * 2. If 1 is true and when there is a state change in 1.8V enable
2234 * bit (bit 3) of SDHCI_HOST_CONTROL2 register. The reset state of
2235 * that bit is 0 which indicates 3.3V IO voltage. So, when MMC core
2236 * layer tries to set it to 3.3V before card detection happens, the
2237 * IRQ doesn't get triggered as there is no state change in this bit.
2238 * The driver already handles this case by changing the IO voltage
2239 * level to high as part of controller power up sequence. Hence, check
2240 * for host->pwr to handle a case where IO voltage high request is
2241 * issued even before controller power up.
2242 */
2243 if (req_type & (REQ_IO_HIGH | REQ_IO_LOW)) {
2244 if (!(io_sig_sts & SWITCHABLE_SIGNALLING_VOL) ||
2245 ((req_type & REQ_IO_HIGH) && !host->pwr)) {
2246 pr_debug("%s: do not wait for power IRQ that never comes\n",
2247 mmc_hostname(host->mmc));
2248 spin_unlock_irqrestore(&host->lock, flags);
2249 return;
2250 }
2251 }
2252
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302253 if ((req_type & msm_host->curr_pwr_state) ||
2254 (req_type & msm_host->curr_io_level))
2255 done = true;
2256 spin_unlock_irqrestore(&host->lock, flags);
Asutosh Das0ef24812012-12-18 16:14:02 +05302257
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302258 /*
2259 * This is needed here to hanlde a case where IRQ gets
2260 * triggered even before this function is called so that
2261 * x->done counter of completion gets reset. Otherwise,
2262 * next call to wait_for_completion returns immediately
2263 * without actually waiting for the IRQ to be handled.
2264 */
2265 if (done)
2266 init_completion(&msm_host->pwr_irq_completion);
2267 else
2268 wait_for_completion(&msm_host->pwr_irq_completion);
2269
2270 pr_debug("%s: %s: request %d done\n", mmc_hostname(host->mmc),
2271 __func__, req_type);
Asutosh Das0ef24812012-12-18 16:14:02 +05302272}
2273
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002274static void sdhci_msm_toggle_cdr(struct sdhci_host *host, bool enable)
2275{
2276 if (enable)
2277 writel_relaxed((readl_relaxed(host->ioaddr +
2278 CORE_DLL_CONFIG) | CORE_CDR_EN),
2279 host->ioaddr + CORE_DLL_CONFIG);
2280 else
2281 writel_relaxed((readl_relaxed(host->ioaddr +
2282 CORE_DLL_CONFIG) & ~CORE_CDR_EN),
2283 host->ioaddr + CORE_DLL_CONFIG);
2284}
2285
Asutosh Das648f9d12013-01-10 21:11:04 +05302286static unsigned int sdhci_msm_max_segs(void)
2287{
2288 return SDHCI_MSM_MAX_SEGMENTS;
2289}
2290
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302291static unsigned int sdhci_msm_get_min_clock(struct sdhci_host *host)
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302292{
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302293 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2294 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302295
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302296 return msm_host->pdata->sup_clk_table[0];
2297}
2298
2299static unsigned int sdhci_msm_get_max_clock(struct sdhci_host *host)
2300{
2301 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2302 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2303 int max_clk_index = msm_host->pdata->sup_clk_cnt;
2304
2305 return msm_host->pdata->sup_clk_table[max_clk_index - 1];
2306}
2307
2308static unsigned int sdhci_msm_get_sup_clk_rate(struct sdhci_host *host,
2309 u32 req_clk)
2310{
2311 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2312 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2313 unsigned int sel_clk = -1;
2314 unsigned char cnt;
2315
2316 if (req_clk < sdhci_msm_get_min_clock(host)) {
2317 sel_clk = sdhci_msm_get_min_clock(host);
2318 return sel_clk;
2319 }
2320
2321 for (cnt = 0; cnt < msm_host->pdata->sup_clk_cnt; cnt++) {
2322 if (msm_host->pdata->sup_clk_table[cnt] > req_clk) {
2323 break;
2324 } else if (msm_host->pdata->sup_clk_table[cnt] == req_clk) {
2325 sel_clk = msm_host->pdata->sup_clk_table[cnt];
2326 break;
2327 } else {
2328 sel_clk = msm_host->pdata->sup_clk_table[cnt];
2329 }
2330 }
2331 return sel_clk;
2332}
2333
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302334static int sdhci_msm_enable_controller_clock(struct sdhci_host *host)
2335{
2336 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2337 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2338 int rc = 0;
2339
2340 if (atomic_read(&msm_host->controller_clock))
2341 return 0;
2342
2343 sdhci_msm_bus_voting(host, 1);
2344
2345 if (!IS_ERR(msm_host->pclk)) {
2346 rc = clk_prepare_enable(msm_host->pclk);
2347 if (rc) {
2348 pr_err("%s: %s: failed to enable the pclk with error %d\n",
2349 mmc_hostname(host->mmc), __func__, rc);
2350 goto remove_vote;
2351 }
2352 }
2353
2354 rc = clk_prepare_enable(msm_host->clk);
2355 if (rc) {
2356 pr_err("%s: %s: failed to enable the host-clk with error %d\n",
2357 mmc_hostname(host->mmc), __func__, rc);
2358 goto disable_pclk;
2359 }
2360
2361 atomic_set(&msm_host->controller_clock, 1);
2362 pr_debug("%s: %s: enabled controller clock\n",
2363 mmc_hostname(host->mmc), __func__);
2364 goto out;
2365
2366disable_pclk:
2367 if (!IS_ERR(msm_host->pclk))
2368 clk_disable_unprepare(msm_host->pclk);
2369remove_vote:
2370 if (msm_host->msm_bus_vote.client_handle)
2371 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2372out:
2373 return rc;
2374}
2375
2376
2377
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302378static int sdhci_msm_prepare_clocks(struct sdhci_host *host, bool enable)
2379{
2380 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2381 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2382 int rc = 0;
2383
2384 if (enable && !atomic_read(&msm_host->clks_on)) {
2385 pr_debug("%s: request to enable clocks\n",
2386 mmc_hostname(host->mmc));
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302387
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302388 /*
2389 * The bus-width or the clock rate might have changed
2390 * after controller clocks are enbaled, update bus vote
2391 * in such case.
2392 */
2393 if (atomic_read(&msm_host->controller_clock))
2394 sdhci_msm_bus_voting(host, 1);
2395
2396 rc = sdhci_msm_enable_controller_clock(host);
2397 if (rc)
2398 goto remove_vote;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302399
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302400 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
2401 rc = clk_prepare_enable(msm_host->bus_clk);
2402 if (rc) {
2403 pr_err("%s: %s: failed to enable the bus-clock with error %d\n",
2404 mmc_hostname(host->mmc), __func__, rc);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302405 goto disable_controller_clk;
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302406 }
2407 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002408 if (!IS_ERR(msm_host->ff_clk)) {
2409 rc = clk_prepare_enable(msm_host->ff_clk);
2410 if (rc) {
2411 pr_err("%s: %s: failed to enable the ff_clk with error %d\n",
2412 mmc_hostname(host->mmc), __func__, rc);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302413 goto disable_bus_clk;
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002414 }
2415 }
2416 if (!IS_ERR(msm_host->sleep_clk)) {
2417 rc = clk_prepare_enable(msm_host->sleep_clk);
2418 if (rc) {
2419 pr_err("%s: %s: failed to enable the sleep_clk with error %d\n",
2420 mmc_hostname(host->mmc), __func__, rc);
2421 goto disable_ff_clk;
2422 }
2423 }
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302424 mb();
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302425
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302426 } else if (!enable && atomic_read(&msm_host->clks_on)) {
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302427 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
2428 mb();
Sahitya Tummaladc182982013-08-20 15:32:09 +05302429 /*
2430 * During 1.8V signal switching the clock source must
2431 * still be ON as it requires accessing SDHC
2432 * registers (SDHCi host control2 register bit 3 must
2433 * be written and polled after stopping the SDCLK).
2434 */
2435 if (host->mmc->card_clock_off)
2436 return 0;
2437 pr_debug("%s: request to disable clocks\n",
2438 mmc_hostname(host->mmc));
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002439 if (!IS_ERR_OR_NULL(msm_host->sleep_clk))
2440 clk_disable_unprepare(msm_host->sleep_clk);
2441 if (!IS_ERR_OR_NULL(msm_host->ff_clk))
2442 clk_disable_unprepare(msm_host->ff_clk);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302443 clk_disable_unprepare(msm_host->clk);
2444 if (!IS_ERR(msm_host->pclk))
2445 clk_disable_unprepare(msm_host->pclk);
2446 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2447 clk_disable_unprepare(msm_host->bus_clk);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302448
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302449 atomic_set(&msm_host->controller_clock, 0);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302450 sdhci_msm_bus_voting(host, 0);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302451 }
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302452 atomic_set(&msm_host->clks_on, enable);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302453 goto out;
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002454disable_ff_clk:
2455 if (!IS_ERR_OR_NULL(msm_host->ff_clk))
2456 clk_disable_unprepare(msm_host->ff_clk);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302457disable_bus_clk:
2458 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2459 clk_disable_unprepare(msm_host->bus_clk);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302460disable_controller_clk:
2461 if (!IS_ERR_OR_NULL(msm_host->clk))
2462 clk_disable_unprepare(msm_host->clk);
2463 if (!IS_ERR_OR_NULL(msm_host->pclk))
2464 clk_disable_unprepare(msm_host->pclk);
2465 atomic_set(&msm_host->controller_clock, 0);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302466remove_vote:
2467 if (msm_host->msm_bus_vote.client_handle)
2468 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302469out:
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302470 return rc;
2471}
2472
2473static void sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock)
2474{
2475 int rc;
2476 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2477 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2478 struct mmc_ios curr_ios = host->mmc->ios;
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002479 u32 sup_clock, ddr_clock, dll_lock;
Sahitya Tummala043744a2013-06-24 09:55:33 +05302480 bool curr_pwrsave;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302481
2482 if (!clock) {
Sujit Reddy Thummabf1aecc2014-01-10 10:58:54 +05302483 /*
2484 * disable pwrsave to ensure clock is not auto-gated until
2485 * the rate is >400KHz (initialization complete).
2486 */
2487 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2488 ~CORE_CLK_PWRSAVE, host->ioaddr + CORE_VENDOR_SPEC);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302489 sdhci_msm_prepare_clocks(host, false);
2490 host->clock = clock;
2491 goto out;
2492 }
2493
2494 rc = sdhci_msm_prepare_clocks(host, true);
2495 if (rc)
2496 goto out;
2497
Sahitya Tummala043744a2013-06-24 09:55:33 +05302498 curr_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2499 CORE_CLK_PWRSAVE);
Sahitya Tummalae000b242013-08-29 16:21:08 +05302500 if ((clock > 400000) &&
Venkat Gopalakrishnanc0a367272015-02-24 13:09:09 -08002501 !curr_pwrsave && mmc_host_may_gate_card(host->mmc->card))
Sahitya Tummala043744a2013-06-24 09:55:33 +05302502 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2503 | CORE_CLK_PWRSAVE,
2504 host->ioaddr + CORE_VENDOR_SPEC);
2505 /*
2506 * Disable pwrsave for a newly added card if doesn't allow clock
2507 * gating.
2508 */
Venkat Gopalakrishnanc0a367272015-02-24 13:09:09 -08002509 else if (curr_pwrsave && !mmc_host_may_gate_card(host->mmc->card))
Sahitya Tummala043744a2013-06-24 09:55:33 +05302510 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2511 & ~CORE_CLK_PWRSAVE,
2512 host->ioaddr + CORE_VENDOR_SPEC);
2513
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302514 sup_clock = sdhci_msm_get_sup_clk_rate(host, clock);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002515 if ((curr_ios.timing == MMC_TIMING_UHS_DDR50) ||
Venkat Gopalakrishnan0a29da92015-01-09 12:19:16 -08002516 (curr_ios.timing == MMC_TIMING_MMC_DDR52) ||
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002517 (curr_ios.timing == MMC_TIMING_MMC_HS400)) {
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302518 /*
2519 * The SDHC requires internal clock frequency to be double the
2520 * actual clock that will be set for DDR mode. The controller
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002521 * uses the faster clock(100/400MHz) for some of its parts and
2522 * send the actual required clock (50/200MHz) to the card.
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302523 */
2524 ddr_clock = clock * 2;
2525 sup_clock = sdhci_msm_get_sup_clk_rate(host,
2526 ddr_clock);
2527 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002528
2529 /*
2530 * In general all timing modes are controlled via UHS mode select in
2531 * Host Control2 register. eMMC specific HS200/HS400 doesn't have
2532 * their respective modes defined here, hence we use these values.
2533 *
2534 * HS200 - SDR104 (Since they both are equivalent in functionality)
2535 * HS400 - This involves multiple configurations
2536 * Initially SDR104 - when tuning is required as HS200
2537 * Then when switching to DDR @ 400MHz (HS400) we use
2538 * the vendor specific HC_SELECT_IN to control the mode.
2539 *
2540 * In addition to controlling the modes we also need to select the
2541 * correct input clock for DLL depending on the mode.
2542 *
2543 * HS400 - divided clock (free running MCLK/2)
2544 * All other modes - default (free running MCLK)
2545 */
2546 if (curr_ios.timing == MMC_TIMING_MMC_HS400) {
2547 /* Select the divided clock (free running MCLK/2) */
2548 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2549 & ~CORE_HC_MCLK_SEL_MASK)
2550 | CORE_HC_MCLK_SEL_HS400),
2551 host->ioaddr + CORE_VENDOR_SPEC);
2552 /*
2553 * Select HS400 mode using the HC_SELECT_IN from VENDOR SPEC
2554 * register
2555 */
Ritesh Harjaniea709662015-05-27 15:40:24 +05302556 if ((msm_host->tuning_done || msm_host->enhanced_strobe) &&
2557 !msm_host->calibration_done) {
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002558 /*
2559 * Write 0x6 to HC_SELECT_IN and 1 to HC_SELECT_IN_EN
2560 * field in VENDOR_SPEC_FUNC
2561 */
2562 writel_relaxed((readl_relaxed(host->ioaddr + \
2563 CORE_VENDOR_SPEC)
2564 | CORE_HC_SELECT_IN_HS400
2565 | CORE_HC_SELECT_IN_EN),
2566 host->ioaddr + CORE_VENDOR_SPEC);
2567 }
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002568 if (!host->mmc->ios.old_rate && !msm_host->use_cdclp533) {
2569 /*
2570 * Poll on DLL_LOCK and DDR_DLL_LOCK bits in
2571 * CORE_DLL_STATUS to be set. This should get set
2572 * with in 15 us at 200 MHz.
2573 */
2574 rc = readl_poll_timeout(host->ioaddr + CORE_DLL_STATUS,
2575 dll_lock, (dll_lock & (CORE_DLL_LOCK |
2576 CORE_DDR_DLL_LOCK)), 10, 1000);
2577 if (rc == -ETIMEDOUT)
2578 pr_err("%s: Unable to get DLL_LOCK/DDR_DLL_LOCK, dll_status: 0x%08x\n",
2579 mmc_hostname(host->mmc),
2580 dll_lock);
2581 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002582 } else {
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002583 if (!msm_host->use_cdclp533)
2584 /* set CORE_PWRSAVE_DLL bit in CORE_VENDOR_SPEC3 */
2585 writel_relaxed((readl_relaxed(host->ioaddr +
2586 CORE_VENDOR_SPEC3) & ~CORE_PWRSAVE_DLL),
2587 host->ioaddr + CORE_VENDOR_SPEC3);
2588
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002589 /* Select the default clock (free running MCLK) */
2590 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2591 & ~CORE_HC_MCLK_SEL_MASK)
2592 | CORE_HC_MCLK_SEL_DFLT),
2593 host->ioaddr + CORE_VENDOR_SPEC);
2594
2595 /*
2596 * Disable HC_SELECT_IN to be able to use the UHS mode select
2597 * configuration from Host Control2 register for all other
2598 * modes.
2599 *
2600 * Write 0 to HC_SELECT_IN and HC_SELECT_IN_EN field
2601 * in VENDOR_SPEC_FUNC
2602 */
2603 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2604 & ~CORE_HC_SELECT_IN_EN
2605 & ~CORE_HC_SELECT_IN_MASK),
2606 host->ioaddr + CORE_VENDOR_SPEC);
2607 }
2608 mb();
2609
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302610 if (sup_clock != msm_host->clk_rate) {
2611 pr_debug("%s: %s: setting clk rate to %u\n",
2612 mmc_hostname(host->mmc), __func__, sup_clock);
2613 rc = clk_set_rate(msm_host->clk, sup_clock);
2614 if (rc) {
2615 pr_err("%s: %s: Failed to set rate %u for host-clk : %d\n",
2616 mmc_hostname(host->mmc), __func__,
2617 sup_clock, rc);
2618 goto out;
2619 }
2620 msm_host->clk_rate = sup_clock;
2621 host->clock = clock;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302622 /*
2623 * Update the bus vote in case of frequency change due to
2624 * clock scaling.
2625 */
2626 sdhci_msm_bus_voting(host, 1);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302627 }
2628out:
2629 sdhci_set_clock(host, clock);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302630}
2631
Sahitya Tummala14613432013-03-21 11:13:25 +05302632static void sdhci_msm_set_uhs_signaling(struct sdhci_host *host,
2633 unsigned int uhs)
2634{
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002635 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2636 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala14613432013-03-21 11:13:25 +05302637 u16 ctrl_2;
2638
2639 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2640 /* Select Bus Speed Mode for host */
2641 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Venkat Gopalakrishnan0a29da92015-01-09 12:19:16 -08002642 if ((uhs == MMC_TIMING_MMC_HS400) ||
2643 (uhs == MMC_TIMING_MMC_HS200) ||
2644 (uhs == MMC_TIMING_UHS_SDR104))
Sahitya Tummala14613432013-03-21 11:13:25 +05302645 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2646 else if (uhs == MMC_TIMING_UHS_SDR12)
2647 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
2648 else if (uhs == MMC_TIMING_UHS_SDR25)
2649 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
2650 else if (uhs == MMC_TIMING_UHS_SDR50)
2651 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
Venkat Gopalakrishnan0a29da92015-01-09 12:19:16 -08002652 else if ((uhs == MMC_TIMING_UHS_DDR50) ||
2653 (uhs == MMC_TIMING_MMC_DDR52))
Sahitya Tummala14613432013-03-21 11:13:25 +05302654 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302655 /*
2656 * When clock frquency is less than 100MHz, the feedback clock must be
2657 * provided and DLL must not be used so that tuning can be skipped. To
2658 * provide feedback clock, the mode selection can be any value less
2659 * than 3'b011 in bits [2:0] of HOST CONTROL2 register.
2660 */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002661 if (host->clock <= CORE_FREQ_100MHZ) {
2662 if ((uhs == MMC_TIMING_MMC_HS400) ||
2663 (uhs == MMC_TIMING_MMC_HS200) ||
2664 (uhs == MMC_TIMING_UHS_SDR104))
2665 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302666
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002667 /*
2668 * Make sure DLL is disabled when not required
2669 *
2670 * Write 1 to DLL_RST bit of DLL_CONFIG register
2671 */
2672 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
2673 | CORE_DLL_RST),
2674 host->ioaddr + CORE_DLL_CONFIG);
2675
2676 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
2677 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
2678 | CORE_DLL_PDN),
2679 host->ioaddr + CORE_DLL_CONFIG);
2680 mb();
2681
2682 /*
2683 * The DLL needs to be restored and CDCLP533 recalibrated
2684 * when the clock frequency is set back to 400MHz.
2685 */
2686 msm_host->calibration_done = false;
2687 }
2688
2689 pr_debug("%s: %s-clock:%u uhs mode:%u ctrl_2:0x%x\n",
2690 mmc_hostname(host->mmc), __func__, host->clock, uhs, ctrl_2);
Sahitya Tummala14613432013-03-21 11:13:25 +05302691 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
2692
2693}
2694
Venkat Gopalakrishnan34811972015-03-04 14:39:01 -08002695#define MAX_TEST_BUS 60
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302696
2697void sdhci_msm_dump_vendor_regs(struct sdhci_host *host)
2698{
2699 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2700 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2701 int tbsel, tbsel2;
2702 int i, index = 0;
2703 u32 test_bus_val = 0;
2704 u32 debug_reg[MAX_TEST_BUS] = {0};
2705
2706 pr_info("----------- VENDOR REGISTER DUMP -----------\n");
2707 pr_info("Data cnt: 0x%08x | Fifo cnt: 0x%08x | Int sts: 0x%08x\n",
2708 readl_relaxed(msm_host->core_mem + CORE_MCI_DATA_CNT),
2709 readl_relaxed(msm_host->core_mem + CORE_MCI_FIFO_CNT),
2710 readl_relaxed(msm_host->core_mem + CORE_MCI_STATUS));
2711 pr_info("DLL cfg: 0x%08x | DLL sts: 0x%08x | SDCC ver: 0x%08x\n",
2712 readl_relaxed(host->ioaddr + CORE_DLL_CONFIG),
2713 readl_relaxed(host->ioaddr + CORE_DLL_STATUS),
2714 readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION));
2715 pr_info("Vndr func: 0x%08x | Vndr adma err : addr0: 0x%08x addr1: 0x%08x\n",
2716 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC),
2717 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_ADMA_ERR_ADDR0),
2718 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_ADMA_ERR_ADDR1));
2719
2720 /*
2721 * tbsel indicates [2:0] bits and tbsel2 indicates [7:4] bits
2722 * of CORE_TESTBUS_CONFIG register.
2723 *
2724 * To select test bus 0 to 7 use tbsel and to select any test bus
2725 * above 7 use (tbsel2 | tbsel) to get the test bus number. For eg,
2726 * to select test bus 14, write 0x1E to CORE_TESTBUS_CONFIG register
2727 * i.e., tbsel2[7:4] = 0001, tbsel[2:0] = 110.
2728 */
Venkat Gopalakrishnan34811972015-03-04 14:39:01 -08002729 for (tbsel2 = 0; tbsel2 < 7; tbsel2++) {
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302730 for (tbsel = 0; tbsel < 8; tbsel++) {
2731 if (index >= MAX_TEST_BUS)
2732 break;
2733 test_bus_val = (tbsel2 << CORE_TESTBUS_SEL2_BIT) |
2734 tbsel | CORE_TESTBUS_ENA;
2735 writel_relaxed(test_bus_val,
2736 msm_host->core_mem + CORE_TESTBUS_CONFIG);
2737 debug_reg[index++] = readl_relaxed(msm_host->core_mem +
2738 CORE_SDCC_DEBUG_REG);
2739 }
2740 }
2741 for (i = 0; i < MAX_TEST_BUS; i = i + 4)
2742 pr_info(" Test bus[%d to %d]: 0x%08x 0x%08x 0x%08x 0x%08x\n",
2743 i, i + 3, debug_reg[i], debug_reg[i+1],
2744 debug_reg[i+2], debug_reg[i+3]);
Venkat Gopalakrishnan06f7f792015-05-29 17:56:59 -07002745}
2746
2747static void sdhci_msm_clear_set_dumpregs(struct sdhci_host *host, bool set)
2748{
2749 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2750 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2751
2752 if (set) {
2753 writel_relaxed(CORE_TESTBUS_ENA,
2754 msm_host->core_mem + CORE_TESTBUS_CONFIG);
2755 } else {
2756 u32 value;
2757
2758 value = readl_relaxed(msm_host->core_mem + CORE_TESTBUS_CONFIG);
2759 value &= ~CORE_TESTBUS_ENA;
2760 writel_relaxed(value, msm_host->core_mem + CORE_TESTBUS_CONFIG);
2761 }
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302762}
2763
Asutosh Das0ef24812012-12-18 16:14:02 +05302764static struct sdhci_ops sdhci_msm_ops = {
Sahitya Tummala14613432013-03-21 11:13:25 +05302765 .set_uhs_signaling = sdhci_msm_set_uhs_signaling,
Asutosh Das0ef24812012-12-18 16:14:02 +05302766 .check_power_status = sdhci_msm_check_power_status,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002767 .platform_execute_tuning = sdhci_msm_execute_tuning,
Ritesh Harjaniea709662015-05-27 15:40:24 +05302768 .enhanced_strobe = sdhci_msm_enhanced_strobe,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002769 .toggle_cdr = sdhci_msm_toggle_cdr,
Asutosh Das648f9d12013-01-10 21:11:04 +05302770 .get_max_segments = sdhci_msm_max_segs,
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302771 .set_clock = sdhci_msm_set_clock,
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302772 .get_min_clock = sdhci_msm_get_min_clock,
2773 .get_max_clock = sdhci_msm_get_max_clock,
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302774 .dump_vendor_regs = sdhci_msm_dump_vendor_regs,
Asutosh Dase5e9ca62013-07-30 19:08:36 +05302775 .config_auto_tuning_cmd = sdhci_msm_config_auto_tuning_cmd,
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302776 .enable_controller_clock = sdhci_msm_enable_controller_clock,
Venkat Gopalakrishnanb8cb7072015-01-09 11:04:34 -08002777 .set_bus_width = sdhci_set_bus_width,
Venkat Gopalakrishnan411df072015-01-09 11:09:44 -08002778 .reset = sdhci_reset,
Venkat Gopalakrishnan06f7f792015-05-29 17:56:59 -07002779 .clear_set_dumpregs = sdhci_msm_clear_set_dumpregs,
Asutosh Das0ef24812012-12-18 16:14:02 +05302780};
2781
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302782static void sdhci_set_default_hw_caps(struct sdhci_msm_host *msm_host,
2783 struct sdhci_host *host)
2784{
Krishna Konda46fd1432014-10-30 21:13:27 -07002785 u32 version, caps = 0;
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302786 u16 minor;
2787 u8 major;
2788
2789 version = readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION);
2790 major = (version & CORE_VERSION_MAJOR_MASK) >>
2791 CORE_VERSION_MAJOR_SHIFT;
2792 minor = version & CORE_VERSION_TARGET_MASK;
2793
Krishna Konda46fd1432014-10-30 21:13:27 -07002794 caps = readl_relaxed(host->ioaddr + SDHCI_CAPABILITIES);
2795
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302796 /*
2797 * Starting with SDCC 5 controller (core major version = 1)
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002798 * controller won't advertise 3.0v, 1.8v and 8-bit features
2799 * except for some targets.
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302800 */
2801 if (major >= 1 && minor != 0x11 && minor != 0x12) {
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002802 struct sdhci_msm_reg_data *vdd_io_reg;
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002803 /*
2804 * Enable 1.8V support capability on controllers that
2805 * support dual voltage
2806 */
2807 vdd_io_reg = msm_host->pdata->vreg_data->vdd_io_data;
Krishna Konda46fd1432014-10-30 21:13:27 -07002808 if (vdd_io_reg && (vdd_io_reg->high_vol_level > 2700000))
2809 caps |= CORE_3_0V_SUPPORT;
2810 if (vdd_io_reg && (vdd_io_reg->low_vol_level < 1950000))
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002811 caps |= CORE_1_8V_SUPPORT;
Pratibhasagar Vada47992013-12-09 20:42:32 +05302812 if (msm_host->pdata->mmc_bus_width == MMC_CAP_8_BIT_DATA)
2813 caps |= CORE_8_BIT_SUPPORT;
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302814 }
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002815
2816 /*
2817 * SDCC 5 controller with major version 1, minor version 0x34 and later
2818 * with HS 400 mode support will use CM DLL instead of CDC LP 533 DLL.
2819 */
2820 if ((major == 1) && (minor < 0x34))
2821 msm_host->use_cdclp533 = true;
Gilad Broner2a10ca02014-10-02 17:20:35 +03002822
2823 /*
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -08002824 * SDCC 5 controller with major version 1, minor version 0x42 and later
2825 * will require additional steps when resetting DLL.
Ritesh Harjaniea709662015-05-27 15:40:24 +05302826 * It also supports HS400 enhanced strobe mode.
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -08002827 */
Ritesh Harjaniea709662015-05-27 15:40:24 +05302828 if ((major == 1) && (minor >= 0x42)) {
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -08002829 msm_host->use_updated_dll_reset = true;
Ritesh Harjaniea709662015-05-27 15:40:24 +05302830 msm_host->enhanced_strobe = true;
2831 }
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -08002832
2833 /*
Talel Shenhar9a25b882015-06-02 13:36:35 +03002834 * SDCC 5 controller with major version 1 and minor version 0x42,
2835 * 0x46 and 0x49 currently uses 14lpp tech DLL whose internal
2836 * gating cannot guarantee MCLK timing requirement i.e.
Ritesh Harjani764065e2015-05-13 14:14:45 +05302837 * when MCLK is gated OFF, it is not gated for less than 0.5us
2838 * and MCLK must be switched on for at-least 1us before DATA
2839 * starts coming.
2840 */
Talel Shenhar9a25b882015-06-02 13:36:35 +03002841 if ((major == 1) && ((minor == 0x42) || (minor == 0x46) ||
2842 (minor == 0x49)))
Ritesh Harjani764065e2015-05-13 14:14:45 +05302843 msm_host->use_14lpp_dll = true;
2844 /*
Gilad Broner2a10ca02014-10-02 17:20:35 +03002845 * Mask 64-bit support for controller with 32-bit address bus so that
2846 * smaller descriptor size will be used and improve memory consumption.
Gilad Broner2a10ca02014-10-02 17:20:35 +03002847 */
Venkat Gopalakrishnan9a62e042015-03-03 16:14:55 -08002848 if (!msm_host->pdata->largeaddressbus)
2849 caps &= ~CORE_SYS_BUS_SUPPORT_64_BIT;
2850
Gilad Broner2a10ca02014-10-02 17:20:35 +03002851 writel_relaxed(caps, host->ioaddr + CORE_VENDOR_SPEC_CAPABILITIES0);
Krishna Konda46fd1432014-10-30 21:13:27 -07002852 /* keep track of the value in SDHCI_CAPABILITIES */
2853 msm_host->caps_0 = caps;
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302854}
2855
Venkat Gopalakrishnand371f142015-05-29 17:49:46 -07002856#ifdef CONFIG_MMC_CQ_HCI
2857static void sdhci_msm_cmdq_init(struct sdhci_host *host,
2858 struct platform_device *pdev)
2859{
2860 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2861 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2862
2863 host->cq_host = cmdq_pltfm_init(pdev);
2864 if (IS_ERR(host->cq_host))
2865 dev_dbg(&pdev->dev, "cmdq-pltfm init: failed: %ld\n",
2866 PTR_ERR(host->cq_host));
2867 else
2868 msm_host->mmc->caps2 |= MMC_CAP2_CMD_QUEUE;
2869}
2870#else
2871static void sdhci_msm_cmdq_init(struct sdhci_host *host,
2872 struct platform_device *pdev)
2873{
2874
2875}
2876#endif
2877
Asutosh Das0ef24812012-12-18 16:14:02 +05302878static int sdhci_msm_probe(struct platform_device *pdev)
2879{
2880 struct sdhci_host *host;
2881 struct sdhci_pltfm_host *pltfm_host;
2882 struct sdhci_msm_host *msm_host;
2883 struct resource *core_memres = NULL;
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02002884 int ret = 0, dead = 0;
Stephen Boyd8dce5c62013-04-24 14:19:46 -07002885 u16 host_version;
Venkat Gopalakrishnan17edb352015-06-24 14:46:49 -07002886 u32 irq_status, irq_ctl;
Asutosh Das0ef24812012-12-18 16:14:02 +05302887
2888 pr_debug("%s: Enter %s\n", dev_name(&pdev->dev), __func__);
2889 msm_host = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_msm_host),
2890 GFP_KERNEL);
2891 if (!msm_host) {
2892 ret = -ENOMEM;
2893 goto out;
2894 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302895
2896 msm_host->sdhci_msm_pdata.ops = &sdhci_msm_ops;
2897 host = sdhci_pltfm_init(pdev, &msm_host->sdhci_msm_pdata, 0);
2898 if (IS_ERR(host)) {
2899 ret = PTR_ERR(host);
2900 goto out;
2901 }
2902
2903 pltfm_host = sdhci_priv(host);
2904 pltfm_host->priv = msm_host;
2905 msm_host->mmc = host->mmc;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302906 msm_host->pdev = pdev;
Asutosh Das0ef24812012-12-18 16:14:02 +05302907
2908 /* Extract platform data */
2909 if (pdev->dev.of_node) {
Venkat Gopalakrishnan270580a2013-03-11 12:17:57 -07002910 ret = of_alias_get_id(pdev->dev.of_node, "sdhc");
2911 if (ret < 0) {
2912 dev_err(&pdev->dev, "Failed to get slot index %d\n",
2913 ret);
2914 goto pltfm_free;
2915 }
2916 if (disable_slots & (1 << (ret - 1))) {
2917 dev_info(&pdev->dev, "%s: Slot %d disabled\n", __func__,
2918 ret);
2919 ret = -ENODEV;
2920 goto pltfm_free;
2921 }
2922
Dov Levenglickc9033ab2015-03-10 16:00:56 +02002923 msm_host->pdata = sdhci_msm_populate_pdata(&pdev->dev,
2924 msm_host);
Asutosh Das0ef24812012-12-18 16:14:02 +05302925 if (!msm_host->pdata) {
2926 dev_err(&pdev->dev, "DT parsing error\n");
2927 goto pltfm_free;
2928 }
2929 } else {
2930 dev_err(&pdev->dev, "No device tree node\n");
2931 goto pltfm_free;
2932 }
2933
2934 /* Setup Clocks */
2935
2936 /* Setup SDCC bus voter clock. */
2937 msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus_clk");
2938 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
2939 /* Vote for max. clk rate for max. performance */
2940 ret = clk_set_rate(msm_host->bus_clk, INT_MAX);
2941 if (ret)
2942 goto pltfm_free;
2943 ret = clk_prepare_enable(msm_host->bus_clk);
2944 if (ret)
2945 goto pltfm_free;
2946 }
2947
2948 /* Setup main peripheral bus clock */
2949 msm_host->pclk = devm_clk_get(&pdev->dev, "iface_clk");
2950 if (!IS_ERR(msm_host->pclk)) {
2951 ret = clk_prepare_enable(msm_host->pclk);
2952 if (ret)
2953 goto bus_clk_disable;
2954 }
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302955 atomic_set(&msm_host->controller_clock, 1);
Asutosh Das0ef24812012-12-18 16:14:02 +05302956
2957 /* Setup SDC MMC clock */
2958 msm_host->clk = devm_clk_get(&pdev->dev, "core_clk");
2959 if (IS_ERR(msm_host->clk)) {
2960 ret = PTR_ERR(msm_host->clk);
2961 goto pclk_disable;
2962 }
2963
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302964 /* Set to the minimum supported clock frequency */
2965 ret = clk_set_rate(msm_host->clk, sdhci_msm_get_min_clock(host));
2966 if (ret) {
2967 dev_err(&pdev->dev, "MClk rate set failed (%d)\n", ret);
Sahitya Tummala020ede0d2013-06-07 13:03:07 +05302968 goto pclk_disable;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302969 }
Sahitya Tummala020ede0d2013-06-07 13:03:07 +05302970 ret = clk_prepare_enable(msm_host->clk);
2971 if (ret)
2972 goto pclk_disable;
2973
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302974 msm_host->clk_rate = sdhci_msm_get_min_clock(host);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302975 atomic_set(&msm_host->clks_on, 1);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302976
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002977 /* Setup CDC calibration fixed feedback clock */
2978 msm_host->ff_clk = devm_clk_get(&pdev->dev, "cal_clk");
2979 if (!IS_ERR(msm_host->ff_clk)) {
2980 ret = clk_prepare_enable(msm_host->ff_clk);
2981 if (ret)
2982 goto clk_disable;
2983 }
2984
2985 /* Setup CDC calibration sleep clock */
2986 msm_host->sleep_clk = devm_clk_get(&pdev->dev, "sleep_clk");
2987 if (!IS_ERR(msm_host->sleep_clk)) {
2988 ret = clk_prepare_enable(msm_host->sleep_clk);
2989 if (ret)
2990 goto ff_clk_disable;
2991 }
2992
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07002993 msm_host->saved_tuning_phase = INVALID_TUNING_PHASE;
2994
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302995 ret = sdhci_msm_bus_register(msm_host, pdev);
2996 if (ret)
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002997 goto sleep_clk_disable;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302998
2999 if (msm_host->msm_bus_vote.client_handle)
3000 INIT_DELAYED_WORK(&msm_host->msm_bus_vote.vote_work,
3001 sdhci_msm_bus_work);
3002 sdhci_msm_bus_voting(host, 1);
3003
Asutosh Das0ef24812012-12-18 16:14:02 +05303004 /* Setup regulators */
3005 ret = sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, true);
3006 if (ret) {
3007 dev_err(&pdev->dev, "Regulator setup failed (%d)\n", ret);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05303008 goto bus_unregister;
Asutosh Das0ef24812012-12-18 16:14:02 +05303009 }
3010
3011 /* Reset the core and Enable SDHC mode */
3012 core_memres = platform_get_resource_byname(pdev,
3013 IORESOURCE_MEM, "core_mem");
Asutosh Das890bdee2014-08-08 23:01:42 +05303014 if (!core_memres) {
3015 dev_err(&pdev->dev, "Failed to get iomem resource\n");
3016 goto vreg_deinit;
3017 }
Asutosh Das0ef24812012-12-18 16:14:02 +05303018 msm_host->core_mem = devm_ioremap(&pdev->dev, core_memres->start,
3019 resource_size(core_memres));
3020
3021 if (!msm_host->core_mem) {
3022 dev_err(&pdev->dev, "Failed to remap registers\n");
3023 ret = -ENOMEM;
3024 goto vreg_deinit;
3025 }
3026
Sahitya Tummala66b0fe32013-04-25 11:50:56 +05303027 /*
Venkat Gopalakrishnan17edb352015-06-24 14:46:49 -07003028 * Reset the vendor spec register to power on reset state.
Sahitya Tummala66b0fe32013-04-25 11:50:56 +05303029 */
Venkat Gopalakrishnan17edb352015-06-24 14:46:49 -07003030 writel_relaxed(CORE_VENDOR_SPEC_POR_VAL,
3031 host->ioaddr + CORE_VENDOR_SPEC);
Sahitya Tummala66b0fe32013-04-25 11:50:56 +05303032
Asutosh Das0ef24812012-12-18 16:14:02 +05303033 /* Set HC_MODE_EN bit in HC_MODE register */
3034 writel_relaxed(HC_MODE_EN, (msm_host->core_mem + CORE_HC_MODE));
3035
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07003036 /* Set FF_CLK_SW_RST_DIS bit in HC_MODE register */
3037 writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_HC_MODE) |
3038 FF_CLK_SW_RST_DIS, msm_host->core_mem + CORE_HC_MODE);
3039
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05303040 sdhci_set_default_hw_caps(msm_host, host);
Krishna Konda46fd1432014-10-30 21:13:27 -07003041
3042 /*
3043 * Set the PAD_PWR_SWTICH_EN bit so that the PAD_PWR_SWITCH bit can
3044 * be used as required later on.
3045 */
3046 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
3047 CORE_IO_PAD_PWR_SWITCH_EN),
3048 host->ioaddr + CORE_VENDOR_SPEC);
Asutosh Das0ef24812012-12-18 16:14:02 +05303049 /*
Subhash Jadavani28137342013-05-14 17:46:43 +05303050 * CORE_SW_RST above may trigger power irq if previous status of PWRCTL
3051 * was either BUS_ON or IO_HIGH_V. So before we enable the power irq
3052 * interrupt in GIC (by registering the interrupt handler), we need to
3053 * ensure that any pending power irq interrupt status is acknowledged
3054 * otherwise power irq interrupt handler would be fired prematurely.
3055 */
3056 irq_status = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
3057 writel_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
3058 irq_ctl = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_CTL);
3059 if (irq_status & (CORE_PWRCTL_BUS_ON | CORE_PWRCTL_BUS_OFF))
3060 irq_ctl |= CORE_PWRCTL_BUS_SUCCESS;
3061 if (irq_status & (CORE_PWRCTL_IO_HIGH | CORE_PWRCTL_IO_LOW))
3062 irq_ctl |= CORE_PWRCTL_IO_SUCCESS;
3063 writel_relaxed(irq_ctl, (msm_host->core_mem + CORE_PWRCTL_CTL));
Krishna Konda46fd1432014-10-30 21:13:27 -07003064
Subhash Jadavani28137342013-05-14 17:46:43 +05303065 /*
3066 * Ensure that above writes are propogated before interrupt enablement
3067 * in GIC.
3068 */
3069 mb();
3070
3071 /*
Asutosh Das0ef24812012-12-18 16:14:02 +05303072 * Following are the deviations from SDHC spec v3.0 -
3073 * 1. Card detection is handled using separate GPIO.
3074 * 2. Bus power control is handled by interacting with PMIC.
3075 */
3076 host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
3077 host->quirks |= SDHCI_QUIRK_SINGLE_POWER_WRITE;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05303078 host->quirks |= SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN;
Talel Shenhar4661c2a2015-06-24 15:49:30 +03003079 host->quirks |= SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05303080 host->quirks2 |= SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK;
Sahitya Tummala87d439442013-04-12 11:49:11 +05303081 host->quirks2 |= SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD;
Sahitya Tummala314162c2013-04-12 12:11:20 +05303082 host->quirks2 |= SDHCI_QUIRK2_BROKEN_PRESET_VALUE;
Sahitya Tummala7c9780d2013-04-12 11:59:25 +05303083 host->quirks2 |= SDHCI_QUIRK2_USE_RESERVED_MAX_TIMEOUT;
Asutosh Das0ef24812012-12-18 16:14:02 +05303084
Sahitya Tummalaa5733ab52013-06-10 16:32:51 +05303085 if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK)
3086 host->quirks2 |= SDHCI_QUIRK2_DIVIDE_TOUT_BY_4;
3087
Stephen Boyd8dce5c62013-04-24 14:19:46 -07003088 host_version = readw_relaxed((host->ioaddr + SDHCI_HOST_VERSION));
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07003089 dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n",
3090 host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >>
3091 SDHCI_VENDOR_VER_SHIFT));
3092 if (((host_version & SDHCI_VENDOR_VER_MASK) >>
3093 SDHCI_VENDOR_VER_SHIFT) == SDHCI_VER_100) {
3094 /*
3095 * Add 40us delay in interrupt handler when
3096 * operating at initialization frequency(400KHz).
3097 */
3098 host->quirks2 |= SDHCI_QUIRK2_SLOW_INT_CLR;
3099 /*
3100 * Set Software Reset for DAT line in Software
3101 * Reset Register (Bit 2).
3102 */
3103 host->quirks2 |= SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT;
3104 }
3105
Asutosh Das214b9662013-06-13 14:27:42 +05303106 host->quirks2 |= SDHCI_QUIRK2_IGN_DATA_END_BIT_ERROR;
3107
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07003108 /* Setup PWRCTL irq */
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02003109 msm_host->pwr_irq = platform_get_irq_byname(pdev, "pwr_irq");
3110 if (msm_host->pwr_irq < 0) {
Asutosh Das0ef24812012-12-18 16:14:02 +05303111 dev_err(&pdev->dev, "Failed to get pwr_irq by name (%d)\n",
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02003112 msm_host->pwr_irq);
Asutosh Das0ef24812012-12-18 16:14:02 +05303113 goto vreg_deinit;
3114 }
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02003115 ret = devm_request_threaded_irq(&pdev->dev, msm_host->pwr_irq, NULL,
Asutosh Das0ef24812012-12-18 16:14:02 +05303116 sdhci_msm_pwr_irq, IRQF_ONESHOT,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07003117 dev_name(&pdev->dev), host);
Asutosh Das0ef24812012-12-18 16:14:02 +05303118 if (ret) {
3119 dev_err(&pdev->dev, "Request threaded irq(%d) failed (%d)\n",
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02003120 msm_host->pwr_irq, ret);
Asutosh Das0ef24812012-12-18 16:14:02 +05303121 goto vreg_deinit;
3122 }
3123
3124 /* Enable pwr irq interrupts */
3125 writel_relaxed(INT_MASK, (msm_host->core_mem + CORE_PWRCTL_MASK));
3126
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05303127#ifdef CONFIG_MMC_CLKGATE
3128 /* Set clock gating delay to be used when CONFIG_MMC_CLKGATE is set */
3129 msm_host->mmc->clkgate_delay = SDHCI_MSM_MMC_CLK_GATE_DELAY;
3130#endif
3131
Asutosh Das0ef24812012-12-18 16:14:02 +05303132 /* Set host capabilities */
3133 msm_host->mmc->caps |= msm_host->pdata->mmc_bus_width;
3134 msm_host->mmc->caps |= msm_host->pdata->caps;
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003135 msm_host->mmc->caps |= MMC_CAP_AGGRESSIVE_PM;
Asutosh Das0ef24812012-12-18 16:14:02 +05303136 msm_host->mmc->caps2 |= msm_host->pdata->caps2;
Venkat Gopalakrishnan97c16112014-12-16 18:26:25 -08003137 msm_host->mmc->caps2 |= MMC_CAP2_BOOTPART_NOACC;
3138 msm_host->mmc->caps2 |= MMC_CAP2_FULL_PWR_CYCLE;
Subhash Jadavani6d472b22013-05-29 15:52:10 +05303139 msm_host->mmc->caps2 |= MMC_CAP2_ASYNC_SDIO_IRQ_4BIT_MODE;
Venkat Gopalakrishnan97c16112014-12-16 18:26:25 -08003140 msm_host->mmc->caps2 |= MMC_CAP2_HS400_POST_TUNING;
Talel Shenhara834c022015-05-13 14:08:39 +03003141 msm_host->mmc->caps2 |= MMC_CAP2_CLK_SCALE;
Asutosh Dasbea115d2013-06-24 18:20:45 +05303142 msm_host->mmc->pm_caps |= MMC_PM_KEEP_POWER;
Asutosh Das0ef24812012-12-18 16:14:02 +05303143
3144 if (msm_host->pdata->nonremovable)
3145 msm_host->mmc->caps |= MMC_CAP_NONREMOVABLE;
3146
Guoping Yuf7c91332014-08-20 16:56:18 +08003147 if (msm_host->pdata->nonhotplug)
3148 msm_host->mmc->caps2 |= MMC_CAP2_NONHOTPLUG;
3149
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05303150 host->cpu_dma_latency_us = msm_host->pdata->cpu_dma_latency_us;
Maya Erez994cf2f2014-10-21 20:22:04 +03003151 host->pm_qos_req_dma.type = msm_host->pdata->cpu_affinity_type;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05303152
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05303153 init_completion(&msm_host->pwr_irq_completion);
3154
Sahitya Tummala581df132013-03-12 14:57:46 +05303155 if (gpio_is_valid(msm_host->pdata->status_gpio)) {
Sahitya Tummala6ddabb42014-06-05 13:26:55 +05303156 /*
3157 * Set up the card detect GPIO in active configuration before
3158 * configuring it as an IRQ. Otherwise, it can be in some
3159 * weird/inconsistent state resulting in flood of interrupts.
3160 */
3161 sdhci_msm_setup_pins(msm_host->pdata, true);
3162
Sahitya Tummala581df132013-03-12 14:57:46 +05303163 ret = mmc_gpio_request_cd(msm_host->mmc,
3164 msm_host->pdata->status_gpio, 0);
3165 if (ret) {
3166 dev_err(&pdev->dev, "%s: Failed to request card detection IRQ %d\n",
3167 __func__, ret);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05303168 goto vreg_deinit;
Sahitya Tummala581df132013-03-12 14:57:46 +05303169 }
3170 }
3171
Krishna Konda7feab352013-09-17 23:55:40 -07003172 if ((sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT) &&
3173 (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(64)))) {
3174 host->dma_mask = DMA_BIT_MASK(64);
3175 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
3176 } else if (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(32))) {
Sahitya Tummalaeaa21862013-03-20 19:34:59 +05303177 host->dma_mask = DMA_BIT_MASK(32);
3178 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
3179 } else {
3180 dev_err(&pdev->dev, "%s: Failed to set dma mask\n", __func__);
3181 }
3182
Venkat Gopalakrishnand371f142015-05-29 17:49:46 -07003183 sdhci_msm_cmdq_init(host, pdev);
Asutosh Das0ef24812012-12-18 16:14:02 +05303184 ret = sdhci_add_host(host);
3185 if (ret) {
3186 dev_err(&pdev->dev, "Add host failed (%d)\n", ret);
Sahitya Tummala581df132013-03-12 14:57:46 +05303187 goto vreg_deinit;
Asutosh Das0ef24812012-12-18 16:14:02 +05303188 }
3189
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003190 pm_runtime_set_active(&pdev->dev);
3191 pm_runtime_enable(&pdev->dev);
3192 pm_runtime_set_autosuspend_delay(&pdev->dev, MSM_AUTOSUSPEND_DELAY_MS);
3193 pm_runtime_use_autosuspend(&pdev->dev);
3194
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05303195 msm_host->msm_bus_vote.max_bus_bw.show = show_sdhci_max_bus_bw;
3196 msm_host->msm_bus_vote.max_bus_bw.store = store_sdhci_max_bus_bw;
3197 sysfs_attr_init(&msm_host->msm_bus_vote.max_bus_bw.attr);
3198 msm_host->msm_bus_vote.max_bus_bw.attr.name = "max_bus_bw";
3199 msm_host->msm_bus_vote.max_bus_bw.attr.mode = S_IRUGO | S_IWUSR;
3200 ret = device_create_file(&pdev->dev,
3201 &msm_host->msm_bus_vote.max_bus_bw);
3202 if (ret)
3203 goto remove_host;
3204
Sahitya Tummala5c55b932013-06-20 14:00:18 +05303205 if (!gpio_is_valid(msm_host->pdata->status_gpio)) {
3206 msm_host->polling.show = show_polling;
3207 msm_host->polling.store = store_polling;
3208 sysfs_attr_init(&msm_host->polling.attr);
3209 msm_host->polling.attr.name = "polling";
3210 msm_host->polling.attr.mode = S_IRUGO | S_IWUSR;
3211 ret = device_create_file(&pdev->dev, &msm_host->polling);
3212 if (ret)
3213 goto remove_max_bus_bw_file;
3214 }
Asutosh Dase5e9ca62013-07-30 19:08:36 +05303215
3216 msm_host->auto_cmd21_attr.show = show_auto_cmd21;
3217 msm_host->auto_cmd21_attr.store = store_auto_cmd21;
3218 sysfs_attr_init(&msm_host->auto_cmd21_attr.attr);
3219 msm_host->auto_cmd21_attr.attr.name = "enable_auto_cmd21";
3220 msm_host->auto_cmd21_attr.attr.mode = S_IRUGO | S_IWUSR;
3221 ret = device_create_file(&pdev->dev, &msm_host->auto_cmd21_attr);
3222 if (ret) {
3223 pr_err("%s: %s: failed creating auto-cmd21 attr: %d\n",
3224 mmc_hostname(host->mmc), __func__, ret);
3225 device_remove_file(&pdev->dev, &msm_host->auto_cmd21_attr);
3226 }
Asutosh Das0ef24812012-12-18 16:14:02 +05303227 /* Successful initialization */
3228 goto out;
3229
Sahitya Tummala5c55b932013-06-20 14:00:18 +05303230remove_max_bus_bw_file:
3231 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Asutosh Das0ef24812012-12-18 16:14:02 +05303232remove_host:
3233 dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003234 pm_runtime_disable(&pdev->dev);
Asutosh Das0ef24812012-12-18 16:14:02 +05303235 sdhci_remove_host(host, dead);
3236vreg_deinit:
3237 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05303238bus_unregister:
3239 if (msm_host->msm_bus_vote.client_handle)
3240 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
3241 sdhci_msm_bus_unregister(msm_host);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07003242sleep_clk_disable:
3243 if (!IS_ERR(msm_host->sleep_clk))
3244 clk_disable_unprepare(msm_host->sleep_clk);
3245ff_clk_disable:
3246 if (!IS_ERR(msm_host->ff_clk))
3247 clk_disable_unprepare(msm_host->ff_clk);
Asutosh Das0ef24812012-12-18 16:14:02 +05303248clk_disable:
3249 if (!IS_ERR(msm_host->clk))
3250 clk_disable_unprepare(msm_host->clk);
3251pclk_disable:
3252 if (!IS_ERR(msm_host->pclk))
3253 clk_disable_unprepare(msm_host->pclk);
3254bus_clk_disable:
3255 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
3256 clk_disable_unprepare(msm_host->bus_clk);
3257pltfm_free:
3258 sdhci_pltfm_free(pdev);
3259out:
3260 pr_debug("%s: Exit %s\n", dev_name(&pdev->dev), __func__);
3261 return ret;
3262}
3263
3264static int sdhci_msm_remove(struct platform_device *pdev)
3265{
3266 struct sdhci_host *host = platform_get_drvdata(pdev);
3267 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3268 struct sdhci_msm_host *msm_host = pltfm_host->priv;
3269 struct sdhci_msm_pltfm_data *pdata = msm_host->pdata;
3270 int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) ==
3271 0xffffffff);
3272
3273 pr_debug("%s: %s\n", dev_name(&pdev->dev), __func__);
Sahitya Tummala5c55b932013-06-20 14:00:18 +05303274 if (!gpio_is_valid(msm_host->pdata->status_gpio))
3275 device_remove_file(&pdev->dev, &msm_host->polling);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05303276 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003277 pm_runtime_disable(&pdev->dev);
Asutosh Das0ef24812012-12-18 16:14:02 +05303278 sdhci_remove_host(host, dead);
3279 sdhci_pltfm_free(pdev);
Sahitya Tummala581df132013-03-12 14:57:46 +05303280
Asutosh Das0ef24812012-12-18 16:14:02 +05303281 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05303282
Pratibhasagar V9acf2642013-11-21 21:07:21 +05303283 sdhci_msm_setup_pins(pdata, true);
3284 sdhci_msm_setup_pins(pdata, false);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05303285
3286 if (msm_host->msm_bus_vote.client_handle) {
3287 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
3288 sdhci_msm_bus_unregister(msm_host);
3289 }
Asutosh Das0ef24812012-12-18 16:14:02 +05303290 return 0;
3291}
3292
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003293#ifdef CONFIG_PM
3294static int sdhci_msm_runtime_suspend(struct device *dev)
3295{
3296 struct sdhci_host *host = dev_get_drvdata(dev);
3297 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3298 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003299 ktime_t start = ktime_get();
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003300
3301 disable_irq(host->irq);
3302 disable_irq(msm_host->pwr_irq);
3303
3304 /*
3305 * Remove the vote immediately only if clocks are off in which
3306 * case we might have queued work to remove vote but it may not
3307 * be completed before runtime suspend or system suspend.
3308 */
3309 if (!atomic_read(&msm_host->clks_on)) {
3310 if (msm_host->msm_bus_vote.client_handle)
3311 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
3312 }
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003313 trace_sdhci_msm_runtime_suspend(mmc_hostname(host->mmc), 0,
3314 ktime_to_us(ktime_sub(ktime_get(), start)));
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003315
3316 return 0;
3317}
3318
3319static int sdhci_msm_runtime_resume(struct device *dev)
3320{
3321 struct sdhci_host *host = dev_get_drvdata(dev);
3322 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3323 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003324 ktime_t start = ktime_get();
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003325
3326 enable_irq(msm_host->pwr_irq);
3327 enable_irq(host->irq);
3328
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003329 trace_sdhci_msm_runtime_resume(mmc_hostname(host->mmc), 0,
3330 ktime_to_us(ktime_sub(ktime_get(), start)));
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003331 return 0;
3332}
3333
3334static int sdhci_msm_suspend(struct device *dev)
3335{
3336 struct sdhci_host *host = dev_get_drvdata(dev);
3337 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3338 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003339 int ret = 0;
3340 ktime_t start = ktime_get();
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003341
3342 if (gpio_is_valid(msm_host->pdata->status_gpio) &&
3343 (msm_host->mmc->slot.cd_irq >= 0))
3344 disable_irq(msm_host->mmc->slot.cd_irq);
3345
3346 if (pm_runtime_suspended(dev)) {
3347 pr_debug("%s: %s: already runtime suspended\n",
3348 mmc_hostname(host->mmc), __func__);
3349 goto out;
3350 }
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003351 ret = sdhci_msm_runtime_suspend(dev);
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003352out:
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003353 trace_sdhci_msm_suspend(mmc_hostname(host->mmc), ret,
3354 ktime_to_us(ktime_sub(ktime_get(), start)));
3355 return ret;
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003356}
3357
3358static int sdhci_msm_resume(struct device *dev)
3359{
3360 struct sdhci_host *host = dev_get_drvdata(dev);
3361 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3362 struct sdhci_msm_host *msm_host = pltfm_host->priv;
3363 int ret = 0;
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003364 ktime_t start = ktime_get();
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003365
3366 if (gpio_is_valid(msm_host->pdata->status_gpio) &&
3367 (msm_host->mmc->slot.cd_irq >= 0))
3368 enable_irq(msm_host->mmc->slot.cd_irq);
3369
3370 if (pm_runtime_suspended(dev)) {
3371 pr_debug("%s: %s: runtime suspended, defer system resume\n",
3372 mmc_hostname(host->mmc), __func__);
3373 goto out;
3374 }
3375
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003376 ret = sdhci_msm_runtime_resume(dev);
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003377out:
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003378 trace_sdhci_msm_resume(mmc_hostname(host->mmc), ret,
3379 ktime_to_us(ktime_sub(ktime_get(), start)));
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003380 return ret;
3381}
3382
3383static const struct dev_pm_ops sdhci_msm_pmops = {
3384 SET_SYSTEM_SLEEP_PM_OPS(sdhci_msm_suspend, sdhci_msm_resume)
3385 SET_RUNTIME_PM_OPS(sdhci_msm_runtime_suspend, sdhci_msm_runtime_resume,
3386 NULL)
3387};
3388
3389#define SDHCI_MSM_PMOPS (&sdhci_msm_pmops)
3390
3391#else
3392#define SDHCI_MSM_PMOPS NULL
3393#endif
Asutosh Das0ef24812012-12-18 16:14:02 +05303394static const struct of_device_id sdhci_msm_dt_match[] = {
3395 {.compatible = "qcom,sdhci-msm"},
Venkat Gopalakrishnan272ba402015-06-25 12:00:02 -07003396 {},
Asutosh Das0ef24812012-12-18 16:14:02 +05303397};
3398MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match);
3399
3400static struct platform_driver sdhci_msm_driver = {
3401 .probe = sdhci_msm_probe,
3402 .remove = sdhci_msm_remove,
3403 .driver = {
3404 .name = "sdhci_msm",
3405 .owner = THIS_MODULE,
3406 .of_match_table = sdhci_msm_dt_match,
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003407 .pm = SDHCI_MSM_PMOPS,
Asutosh Das0ef24812012-12-18 16:14:02 +05303408 },
3409};
3410
3411module_platform_driver(sdhci_msm_driver);
3412
3413MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Secure Digital Host Controller Interface driver");
3414MODULE_LICENSE("GPL v2");