blob: a4508bd1f1402065622f839f2277b995f8e88f5a [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
Sahitya Tummala2ba05ae2015-03-03 14:03:20 +0530124#define CORE_VENDOR_SPEC_FUNC2 0x110
Pavan Anamula691dd592015-08-25 16:11:20 +0530125#define HC_SW_RST_WAIT_IDLE_DIS (1 << 20)
126#define HC_SW_RST_REQ (1 << 21)
Sahitya Tummala2ba05ae2015-03-03 14:03:20 +0530127#define CORE_ONE_MID_EN (1 << 25)
128
Krishna Konda7feab352013-09-17 23:55:40 -0700129#define CORE_VENDOR_SPEC_CAPABILITIES0 0x11C
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +0530130#define CORE_8_BIT_SUPPORT (1 << 18)
131#define CORE_3_3V_SUPPORT (1 << 24)
132#define CORE_3_0V_SUPPORT (1 << 25)
133#define CORE_1_8V_SUPPORT (1 << 26)
Gilad Broner2a10ca02014-10-02 17:20:35 +0300134#define CORE_SYS_BUS_SUPPORT_64_BIT BIT(28)
Krishna Konda7feab352013-09-17 23:55:40 -0700135
Venkat Gopalakrishnan0a92d532014-12-16 17:31:00 -0800136#define CORE_SDCC_DEBUG_REG 0x124
Sahitya Tummala67717bc2013-08-02 09:21:37 +0530137
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700138#define CORE_CSR_CDC_CTLR_CFG0 0x130
139#define CORE_SW_TRIG_FULL_CALIB (1 << 16)
140#define CORE_HW_AUTOCAL_ENA (1 << 17)
141
142#define CORE_CSR_CDC_CTLR_CFG1 0x134
143#define CORE_CSR_CDC_CAL_TIMER_CFG0 0x138
144#define CORE_TIMER_ENA (1 << 16)
145
146#define CORE_CSR_CDC_CAL_TIMER_CFG1 0x13C
147#define CORE_CSR_CDC_REFCOUNT_CFG 0x140
148#define CORE_CSR_CDC_COARSE_CAL_CFG 0x144
149#define CORE_CDC_OFFSET_CFG 0x14C
150#define CORE_CSR_CDC_DELAY_CFG 0x150
151#define CORE_CDC_SLAVE_DDA_CFG 0x160
152#define CORE_CSR_CDC_STATUS0 0x164
153#define CORE_CALIBRATION_DONE (1 << 0)
154
155#define CORE_CDC_ERROR_CODE_MASK 0x7000000
156
157#define CORE_CSR_CDC_GEN_CFG 0x178
158#define CORE_CDC_SWITCH_BYPASS_OFF (1 << 0)
159#define CORE_CDC_SWITCH_RC_EN (1 << 1)
160
161#define CORE_DDR_200_CFG 0x184
162#define CORE_CDC_T4_DLY_SEL (1 << 0)
Ritesh Harjaniea709662015-05-27 15:40:24 +0530163#define CORE_CMDIN_RCLK_EN (1 << 1)
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700164#define CORE_START_CDC_TRAFFIC (1 << 6)
Ritesh Harjaniea709662015-05-27 15:40:24 +0530165
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700166#define CORE_VENDOR_SPEC3 0x1B0
167#define CORE_PWRSAVE_DLL (1 << 3)
Ritesh Harjani4a5ffd12015-07-15 13:32:07 +0530168#define CORE_CMDEN_HS400_INPUT_MASK_CNT (1 << 13)
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700169
170#define CORE_DLL_CONFIG_2 0x1B4
171#define CORE_DDR_CAL_EN (1 << 0)
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800172#define CORE_FLL_CYCLE_CNT (1 << 18)
173#define CORE_DLL_CLOCK_DISABLE (1 << 21)
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700174
Pavan Anamulaf7bf5112015-08-21 18:09:42 +0530175#define CORE_DDR_CONFIG 0x1B8
176#define DDR_CONFIG_POR_VAL 0x80040853
177#define DDR_CONFIG_PRG_RCLK_DLY_MASK 0x1FF
178#define DDR_CONFIG_PRG_RCLK_DLY 115
Venkat Gopalakrishnanb47cf402015-09-04 18:32:25 -0700179#define CORE_DDR_CONFIG_2 0x1BC
180#define DDR_CONFIG_2_POR_VAL 0x80040873
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700181
Sahitya Tummala56874732015-05-21 08:24:03 +0530182#define MSM_MMC_DEFAULT_CPU_DMA_LATENCY 200 /* usecs */
183
Venkat Gopalakrishnan450745e2014-07-24 20:39:34 -0700184/* 512 descriptors */
185#define SDHCI_MSM_MAX_SEGMENTS (1 << 9)
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +0530186#define SDHCI_MSM_MMC_CLK_GATE_DELAY 200 /* msecs */
Asutosh Das648f9d12013-01-10 21:11:04 +0530187
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700188#define CORE_FREQ_100MHZ (100 * 1000 * 1000)
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800189#define TCXO_FREQ 19200000
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700190
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700191#define INVALID_TUNING_PHASE -1
192
Krishna Konda96e6b112013-10-28 15:25:03 -0700193#define NUM_TUNING_PHASES 16
194#define MAX_DRV_TYPES_SUPPORTED_HS200 3
Konstantin Dorfman98377d32015-02-25 10:09:41 +0200195#define MSM_AUTOSUSPEND_DELAY_MS 100
Krishna Konda96e6b112013-10-28 15:25:03 -0700196
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700197static const u32 tuning_block_64[] = {
198 0x00FF0FFF, 0xCCC3CCFF, 0xFFCC3CC3, 0xEFFEFFFE,
199 0xDDFFDFFF, 0xFBFFFBFF, 0xFF7FFFBF, 0xEFBDF777,
200 0xF0FFF0FF, 0x3CCCFC0F, 0xCFCC33CC, 0xEEFFEFFF,
201 0xFDFFFDFF, 0xFFBFFFDF, 0xFFF7FFBB, 0xDE7B7FF7
202};
203
204static const u32 tuning_block_128[] = {
205 0xFF00FFFF, 0x0000FFFF, 0xCCCCFFFF, 0xCCCC33CC,
206 0xCC3333CC, 0xFFFFCCCC, 0xFFFFEEFF, 0xFFEEEEFF,
207 0xFFDDFFFF, 0xDDDDFFFF, 0xBBFFFFFF, 0xBBFFFFFF,
208 0xFFFFFFBB, 0xFFFFFF77, 0x77FF7777, 0xFFEEDDBB,
209 0x00FFFFFF, 0x00FFFFFF, 0xCCFFFF00, 0xCC33CCCC,
210 0x3333CCCC, 0xFFCCCCCC, 0xFFEEFFFF, 0xEEEEFFFF,
211 0xDDFFFFFF, 0xDDFFFFFF, 0xFFFFFFDD, 0xFFFFFFBB,
212 0xFFFFBBBB, 0xFFFF77FF, 0xFF7777FF, 0xEEDDBB77
213};
Asutosh Das0ef24812012-12-18 16:14:02 +0530214
Venkat Gopalakrishnan270580a2013-03-11 12:17:57 -0700215static int disable_slots;
216/* root can write, others read */
217module_param(disable_slots, int, S_IRUGO|S_IWUSR);
218
Asutosh Das0ef24812012-12-18 16:14:02 +0530219enum vdd_io_level {
220 /* set vdd_io_data->low_vol_level */
221 VDD_IO_LOW,
222 /* set vdd_io_data->high_vol_level */
223 VDD_IO_HIGH,
224 /*
225 * set whatever there in voltage_level (third argument) of
226 * sdhci_msm_set_vdd_io_vol() function.
227 */
228 VDD_IO_SET_LEVEL,
229};
230
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700231/* MSM platform specific tuning */
232static inline int msm_dll_poll_ck_out_en(struct sdhci_host *host,
233 u8 poll)
234{
235 int rc = 0;
236 u32 wait_cnt = 50;
237 u8 ck_out_en = 0;
238 struct mmc_host *mmc = host->mmc;
239
240 /* poll for CK_OUT_EN bit. max. poll time = 50us */
241 ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) &
242 CORE_CK_OUT_EN);
243
244 while (ck_out_en != poll) {
245 if (--wait_cnt == 0) {
246 pr_err("%s: %s: CK_OUT_EN bit is not %d\n",
247 mmc_hostname(mmc), __func__, poll);
248 rc = -ETIMEDOUT;
249 goto out;
250 }
251 udelay(1);
252
253 ck_out_en = !!(readl_relaxed(host->ioaddr +
254 CORE_DLL_CONFIG) & CORE_CK_OUT_EN);
255 }
256out:
257 return rc;
258}
259
Asutosh Dase5e9ca62013-07-30 19:08:36 +0530260/*
261 * Enable CDR to track changes of DAT lines and adjust sampling
262 * point according to voltage/temperature variations
263 */
264static int msm_enable_cdr_cm_sdc4_dll(struct sdhci_host *host)
265{
266 int rc = 0;
267 u32 config;
268
269 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
270 config |= CORE_CDR_EN;
271 config &= ~(CORE_CDR_EXT_EN | CORE_CK_OUT_EN);
272 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
273
274 rc = msm_dll_poll_ck_out_en(host, 0);
275 if (rc)
276 goto err;
277
278 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) |
279 CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
280
281 rc = msm_dll_poll_ck_out_en(host, 1);
282 if (rc)
283 goto err;
284 goto out;
285err:
286 pr_err("%s: %s: failed\n", mmc_hostname(host->mmc), __func__);
287out:
288 return rc;
289}
290
291static ssize_t store_auto_cmd21(struct device *dev, struct device_attribute
292 *attr, const char *buf, size_t count)
293{
294 struct sdhci_host *host = dev_get_drvdata(dev);
295 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
296 struct sdhci_msm_host *msm_host = pltfm_host->priv;
297 u32 tmp;
298 unsigned long flags;
299
300 if (!kstrtou32(buf, 0, &tmp)) {
301 spin_lock_irqsave(&host->lock, flags);
302 msm_host->en_auto_cmd21 = !!tmp;
303 spin_unlock_irqrestore(&host->lock, flags);
304 }
305 return count;
306}
307
308static ssize_t show_auto_cmd21(struct device *dev,
309 struct device_attribute *attr, char *buf)
310{
311 struct sdhci_host *host = dev_get_drvdata(dev);
312 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
313 struct sdhci_msm_host *msm_host = pltfm_host->priv;
314
315 return snprintf(buf, PAGE_SIZE, "%d\n", msm_host->en_auto_cmd21);
316}
317
318/* MSM auto-tuning handler */
319static int sdhci_msm_config_auto_tuning_cmd(struct sdhci_host *host,
320 bool enable,
321 u32 type)
322{
323 int rc = 0;
324 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
325 struct sdhci_msm_host *msm_host = pltfm_host->priv;
326 u32 val = 0;
327
328 if (!msm_host->en_auto_cmd21)
329 return 0;
330
331 if (type == MMC_SEND_TUNING_BLOCK_HS200)
332 val = CORE_HC_AUTO_CMD21_EN;
333 else
334 return 0;
335
336 if (enable) {
337 rc = msm_enable_cdr_cm_sdc4_dll(host);
338 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
339 val, host->ioaddr + CORE_VENDOR_SPEC);
340 } else {
341 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
342 ~val, host->ioaddr + CORE_VENDOR_SPEC);
343 }
344 return rc;
345}
346
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700347static int msm_config_cm_dll_phase(struct sdhci_host *host, u8 phase)
348{
349 int rc = 0;
350 u8 grey_coded_phase_table[] = {0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4,
351 0xC, 0xD, 0xF, 0xE, 0xA, 0xB, 0x9,
352 0x8};
353 unsigned long flags;
354 u32 config;
355 struct mmc_host *mmc = host->mmc;
356
357 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
358 spin_lock_irqsave(&host->lock, flags);
359
360 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
361 config &= ~(CORE_CDR_EN | CORE_CK_OUT_EN);
362 config |= (CORE_CDR_EXT_EN | CORE_DLL_EN);
363 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
364
365 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '0' */
366 rc = msm_dll_poll_ck_out_en(host, 0);
367 if (rc)
368 goto err_out;
369
370 /*
371 * Write the selected DLL clock output phase (0 ... 15)
372 * to CDR_SELEXT bit field of DLL_CONFIG register.
373 */
374 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
375 & ~(0xF << 20))
376 | (grey_coded_phase_table[phase] << 20)),
377 host->ioaddr + CORE_DLL_CONFIG);
378
379 /* Set CK_OUT_EN bit of DLL_CONFIG register to 1. */
380 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
381 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
382
383 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '1' */
384 rc = msm_dll_poll_ck_out_en(host, 1);
385 if (rc)
386 goto err_out;
387
388 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
389 config |= CORE_CDR_EN;
390 config &= ~CORE_CDR_EXT_EN;
391 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
392 goto out;
393
394err_out:
395 pr_err("%s: %s: Failed to set DLL phase: %d\n",
396 mmc_hostname(mmc), __func__, phase);
397out:
398 spin_unlock_irqrestore(&host->lock, flags);
399 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
400 return rc;
401}
402
403/*
404 * Find out the greatest range of consecuitive selected
405 * DLL clock output phases that can be used as sampling
406 * setting for SD3.0 UHS-I card read operation (in SDR104
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700407 * timing mode) or for eMMC4.5 card read operation (in
408 * HS400/HS200 timing mode).
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700409 * Select the 3/4 of the range and configure the DLL with the
410 * selected DLL clock output phase.
411 */
412
413static int msm_find_most_appropriate_phase(struct sdhci_host *host,
414 u8 *phase_table, u8 total_phases)
415{
416 int ret;
417 u8 ranges[MAX_PHASES][MAX_PHASES] = { {0}, {0} };
418 u8 phases_per_row[MAX_PHASES] = {0};
419 int row_index = 0, col_index = 0, selected_row_index = 0, curr_max = 0;
420 int i, cnt, phase_0_raw_index = 0, phase_15_raw_index = 0;
421 bool phase_0_found = false, phase_15_found = false;
422 struct mmc_host *mmc = host->mmc;
423
424 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
425 if (!total_phases || (total_phases > MAX_PHASES)) {
426 pr_err("%s: %s: invalid argument: total_phases=%d\n",
427 mmc_hostname(mmc), __func__, total_phases);
428 return -EINVAL;
429 }
430
431 for (cnt = 0; cnt < total_phases; cnt++) {
432 ranges[row_index][col_index] = phase_table[cnt];
433 phases_per_row[row_index] += 1;
434 col_index++;
435
436 if ((cnt + 1) == total_phases) {
437 continue;
438 /* check if next phase in phase_table is consecutive or not */
439 } else if ((phase_table[cnt] + 1) != phase_table[cnt + 1]) {
440 row_index++;
441 col_index = 0;
442 }
443 }
444
445 if (row_index >= MAX_PHASES)
446 return -EINVAL;
447
448 /* Check if phase-0 is present in first valid window? */
449 if (!ranges[0][0]) {
450 phase_0_found = true;
451 phase_0_raw_index = 0;
452 /* Check if cycle exist between 2 valid windows */
453 for (cnt = 1; cnt <= row_index; cnt++) {
454 if (phases_per_row[cnt]) {
455 for (i = 0; i < phases_per_row[cnt]; i++) {
456 if (ranges[cnt][i] == 15) {
457 phase_15_found = true;
458 phase_15_raw_index = cnt;
459 break;
460 }
461 }
462 }
463 }
464 }
465
466 /* If 2 valid windows form cycle then merge them as single window */
467 if (phase_0_found && phase_15_found) {
468 /* number of phases in raw where phase 0 is present */
469 u8 phases_0 = phases_per_row[phase_0_raw_index];
470 /* number of phases in raw where phase 15 is present */
471 u8 phases_15 = phases_per_row[phase_15_raw_index];
472
473 if (phases_0 + phases_15 >= MAX_PHASES)
474 /*
475 * If there are more than 1 phase windows then total
476 * number of phases in both the windows should not be
477 * more than or equal to MAX_PHASES.
478 */
479 return -EINVAL;
480
481 /* Merge 2 cyclic windows */
482 i = phases_15;
483 for (cnt = 0; cnt < phases_0; cnt++) {
484 ranges[phase_15_raw_index][i] =
485 ranges[phase_0_raw_index][cnt];
486 if (++i >= MAX_PHASES)
487 break;
488 }
489
490 phases_per_row[phase_0_raw_index] = 0;
491 phases_per_row[phase_15_raw_index] = phases_15 + phases_0;
492 }
493
494 for (cnt = 0; cnt <= row_index; cnt++) {
495 if (phases_per_row[cnt] > curr_max) {
496 curr_max = phases_per_row[cnt];
497 selected_row_index = cnt;
498 }
499 }
500
501 i = ((curr_max * 3) / 4);
502 if (i)
503 i--;
504
505 ret = (int)ranges[selected_row_index][i];
506
507 if (ret >= MAX_PHASES) {
508 ret = -EINVAL;
509 pr_err("%s: %s: invalid phase selected=%d\n",
510 mmc_hostname(mmc), __func__, ret);
511 }
512
513 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
514 return ret;
515}
516
517static inline void msm_cm_dll_set_freq(struct sdhci_host *host)
518{
519 u32 mclk_freq = 0;
520
521 /* Program the MCLK value to MCLK_FREQ bit field */
522 if (host->clock <= 112000000)
523 mclk_freq = 0;
524 else if (host->clock <= 125000000)
525 mclk_freq = 1;
526 else if (host->clock <= 137000000)
527 mclk_freq = 2;
528 else if (host->clock <= 150000000)
529 mclk_freq = 3;
530 else if (host->clock <= 162000000)
531 mclk_freq = 4;
532 else if (host->clock <= 175000000)
533 mclk_freq = 5;
534 else if (host->clock <= 187000000)
535 mclk_freq = 6;
536 else if (host->clock <= 200000000)
537 mclk_freq = 7;
538
539 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
540 & ~(7 << 24)) | (mclk_freq << 24)),
541 host->ioaddr + CORE_DLL_CONFIG);
542}
543
544/* Initialize the DLL (Programmable Delay Line ) */
545static int msm_init_cm_dll(struct sdhci_host *host)
546{
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800547 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
548 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700549 struct mmc_host *mmc = host->mmc;
550 int rc = 0;
551 unsigned long flags;
552 u32 wait_cnt;
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530553 bool prev_pwrsave, curr_pwrsave;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700554
555 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
556 spin_lock_irqsave(&host->lock, flags);
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530557 prev_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
558 CORE_CLK_PWRSAVE);
559 curr_pwrsave = prev_pwrsave;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700560 /*
561 * Make sure that clock is always enabled when DLL
562 * tuning is in progress. Keeping PWRSAVE ON may
563 * turn off the clock. So let's disable the PWRSAVE
564 * here and re-enable it once tuning is completed.
565 */
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530566 if (prev_pwrsave) {
567 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
568 & ~CORE_CLK_PWRSAVE),
569 host->ioaddr + CORE_VENDOR_SPEC);
570 curr_pwrsave = false;
571 }
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700572
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800573 if (msm_host->use_updated_dll_reset) {
574 /* Disable the DLL clock */
575 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
576 & ~CORE_CK_OUT_EN),
577 host->ioaddr + CORE_DLL_CONFIG);
578
579 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
580 | CORE_DLL_CLOCK_DISABLE),
581 host->ioaddr + CORE_DLL_CONFIG_2);
582 }
583
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700584 /* Write 1 to DLL_RST bit of DLL_CONFIG register */
585 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
586 | CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
587
588 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
589 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
590 | CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
591 msm_cm_dll_set_freq(host);
592
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800593 if (msm_host->use_updated_dll_reset) {
594 u32 mclk_freq = 0;
595
596 if ((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
597 & CORE_FLL_CYCLE_CNT))
598 mclk_freq = (u32) ((host->clock / TCXO_FREQ) * 8);
599 else
600 mclk_freq = (u32) ((host->clock / TCXO_FREQ) * 4);
601
602 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
603 & ~(0xFF << 10)) | (mclk_freq << 10)),
604 host->ioaddr + CORE_DLL_CONFIG_2);
605 /* wait for 5us before enabling DLL clock */
606 udelay(5);
607 }
608
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700609 /* Write 0 to DLL_RST bit of DLL_CONFIG register */
610 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
611 & ~CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
612
613 /* Write 0 to DLL_PDN bit of DLL_CONFIG register */
614 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
615 & ~CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
616
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800617 if (msm_host->use_updated_dll_reset) {
618 msm_cm_dll_set_freq(host);
619 /* Enable the DLL clock */
620 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
621 & ~CORE_DLL_CLOCK_DISABLE),
622 host->ioaddr + CORE_DLL_CONFIG_2);
623 }
624
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700625 /* Set DLL_EN bit to 1. */
626 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
627 | CORE_DLL_EN), host->ioaddr + CORE_DLL_CONFIG);
628
629 /* Set CK_OUT_EN bit to 1. */
630 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
631 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
632
633 wait_cnt = 50;
634 /* Wait until DLL_LOCK bit of DLL_STATUS register becomes '1' */
635 while (!(readl_relaxed(host->ioaddr + CORE_DLL_STATUS) &
636 CORE_DLL_LOCK)) {
637 /* max. wait for 50us sec for LOCK bit to be set */
638 if (--wait_cnt == 0) {
639 pr_err("%s: %s: DLL failed to LOCK\n",
640 mmc_hostname(mmc), __func__);
641 rc = -ETIMEDOUT;
642 goto out;
643 }
644 /* wait for 1us before polling again */
645 udelay(1);
646 }
647
648out:
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530649 /* Restore the correct PWRSAVE state */
650 if (prev_pwrsave ^ curr_pwrsave) {
651 u32 reg = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC);
652
653 if (prev_pwrsave)
654 reg |= CORE_CLK_PWRSAVE;
655 else
656 reg &= ~CORE_CLK_PWRSAVE;
657
658 writel_relaxed(reg, host->ioaddr + CORE_VENDOR_SPEC);
659 }
660
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700661 spin_unlock_irqrestore(&host->lock, flags);
662 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
663 return rc;
664}
665
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700666static int sdhci_msm_cdclp533_calibration(struct sdhci_host *host)
667{
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700668 u32 calib_done;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700669 int ret = 0;
670 int cdc_err = 0;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700671
672 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
673
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700674 /* Write 0 to CDC_T4_DLY_SEL field in VENDOR_SPEC_DDR200_CFG */
675 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
676 & ~CORE_CDC_T4_DLY_SEL),
677 host->ioaddr + CORE_DDR_200_CFG);
678
679 /* Write 0 to CDC_SWITCH_BYPASS_OFF field in CORE_CSR_CDC_GEN_CFG */
680 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG)
681 & ~CORE_CDC_SWITCH_BYPASS_OFF),
682 host->ioaddr + CORE_CSR_CDC_GEN_CFG);
683
684 /* Write 1 to CDC_SWITCH_RC_EN field in CORE_CSR_CDC_GEN_CFG */
685 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG)
686 | CORE_CDC_SWITCH_RC_EN),
687 host->ioaddr + CORE_CSR_CDC_GEN_CFG);
688
689 /* Write 0 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
690 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
691 & ~CORE_START_CDC_TRAFFIC),
692 host->ioaddr + CORE_DDR_200_CFG);
693
694 /*
695 * Perform CDC Register Initialization Sequence
696 *
697 * CORE_CSR_CDC_CTLR_CFG0 0x11800EC
698 * CORE_CSR_CDC_CTLR_CFG1 0x3011111
699 * CORE_CSR_CDC_CAL_TIMER_CFG0 0x1201000
700 * CORE_CSR_CDC_CAL_TIMER_CFG1 0x4
701 * CORE_CSR_CDC_REFCOUNT_CFG 0xCB732020
702 * CORE_CSR_CDC_COARSE_CAL_CFG 0xB19
703 * CORE_CSR_CDC_DELAY_CFG 0x3AC
704 * CORE_CDC_OFFSET_CFG 0x0
705 * CORE_CDC_SLAVE_DDA_CFG 0x16334
706 */
707
708 writel_relaxed(0x11800EC, host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
709 writel_relaxed(0x3011111, host->ioaddr + CORE_CSR_CDC_CTLR_CFG1);
710 writel_relaxed(0x1201000, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
711 writel_relaxed(0x4, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG1);
712 writel_relaxed(0xCB732020, host->ioaddr + CORE_CSR_CDC_REFCOUNT_CFG);
713 writel_relaxed(0xB19, host->ioaddr + CORE_CSR_CDC_COARSE_CAL_CFG);
Subhash Jadavanibe406d92014-06-17 16:47:48 -0700714 writel_relaxed(0x4E2, host->ioaddr + CORE_CSR_CDC_DELAY_CFG);
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700715 writel_relaxed(0x0, host->ioaddr + CORE_CDC_OFFSET_CFG);
716 writel_relaxed(0x16334, host->ioaddr + CORE_CDC_SLAVE_DDA_CFG);
717
718 /* CDC HW Calibration */
719
720 /* Write 1 to SW_TRIG_FULL_CALIB field in CORE_CSR_CDC_CTLR_CFG0 */
721 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
722 | CORE_SW_TRIG_FULL_CALIB),
723 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
724
725 /* Write 0 to SW_TRIG_FULL_CALIB field in CORE_CSR_CDC_CTLR_CFG0 */
726 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
727 & ~CORE_SW_TRIG_FULL_CALIB),
728 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
729
730 /* Write 1 to HW_AUTOCAL_ENA field in CORE_CSR_CDC_CTLR_CFG0 */
731 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
732 | CORE_HW_AUTOCAL_ENA),
733 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
734
735 /* Write 1 to TIMER_ENA field in CORE_CSR_CDC_CAL_TIMER_CFG0 */
736 writel_relaxed((readl_relaxed(host->ioaddr +
737 CORE_CSR_CDC_CAL_TIMER_CFG0) | CORE_TIMER_ENA),
738 host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
739
740 mb();
741
742 /* Poll on CALIBRATION_DONE field in CORE_CSR_CDC_STATUS0 to be 1 */
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700743 ret = readl_poll_timeout(host->ioaddr + CORE_CSR_CDC_STATUS0,
744 calib_done, (calib_done & CORE_CALIBRATION_DONE), 1, 50);
745
746 if (ret == -ETIMEDOUT) {
747 pr_err("%s: %s: CDC Calibration was not completed\n",
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700748 mmc_hostname(host->mmc), __func__);
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700749 goto out;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700750 }
751
752 /* Verify CDC_ERROR_CODE field in CORE_CSR_CDC_STATUS0 is 0 */
753 cdc_err = readl_relaxed(host->ioaddr + CORE_CSR_CDC_STATUS0)
754 & CORE_CDC_ERROR_CODE_MASK;
755 if (cdc_err) {
756 pr_err("%s: %s: CDC Error Code %d\n",
757 mmc_hostname(host->mmc), __func__, cdc_err);
758 ret = -EINVAL;
759 goto out;
760 }
761
762 /* Write 1 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
763 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
764 | CORE_START_CDC_TRAFFIC),
765 host->ioaddr + CORE_DDR_200_CFG);
766out:
767 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
768 __func__, ret);
769 return ret;
770}
771
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700772static int sdhci_msm_cm_dll_sdc4_calibration(struct sdhci_host *host)
773{
Ritesh Harjani764065e2015-05-13 14:14:45 +0530774 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
775 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Pavan Anamulaf7bf5112015-08-21 18:09:42 +0530776 u32 dll_status, ddr_config;
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700777 int ret = 0;
778
779 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
780
781 /*
Pavan Anamulaf7bf5112015-08-21 18:09:42 +0530782 * Reprogramming the value in case it might have been modified by
783 * bootloaders.
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700784 */
Venkat Gopalakrishnanb47cf402015-09-04 18:32:25 -0700785 if (msm_host->rclk_delay_fix) {
786 writel_relaxed(DDR_CONFIG_2_POR_VAL,
787 host->ioaddr + CORE_DDR_CONFIG_2);
788 } else {
789 ddr_config = DDR_CONFIG_POR_VAL &
790 ~DDR_CONFIG_PRG_RCLK_DLY_MASK;
791 ddr_config |= DDR_CONFIG_PRG_RCLK_DLY;
792 writel_relaxed(ddr_config, host->ioaddr + CORE_DDR_CONFIG);
793 }
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700794
Ritesh Harjaniea709662015-05-27 15:40:24 +0530795 if (msm_host->enhanced_strobe)
796 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
797 | CORE_CMDIN_RCLK_EN),
798 host->ioaddr + CORE_DDR_200_CFG);
799
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700800 /* Write 1 to DDR_CAL_EN field in CORE_DLL_CONFIG_2 */
801 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
802 | CORE_DDR_CAL_EN),
803 host->ioaddr + CORE_DLL_CONFIG_2);
804
805 /* Poll on DDR_DLL_LOCK bit in CORE_DLL_STATUS to be set */
806 ret = readl_poll_timeout(host->ioaddr + CORE_DLL_STATUS,
807 dll_status, (dll_status & CORE_DDR_DLL_LOCK), 10, 1000);
808
809 if (ret == -ETIMEDOUT) {
810 pr_err("%s: %s: CM_DLL_SDC4 Calibration was not completed\n",
811 mmc_hostname(host->mmc), __func__);
812 goto out;
813 }
814
Ritesh Harjani764065e2015-05-13 14:14:45 +0530815 /*
816 * set CORE_PWRSAVE_DLL bit in CORE_VENDOR_SPEC3.
817 * when MCLK is gated OFF, it is not gated for less than 0.5us
818 * and MCLK must be switched on for at-least 1us before DATA
819 * starts coming. Controllers with 14lpp tech DLL cannot
820 * guarantee above requirement. So PWRSAVE_DLL should not be
821 * turned on for host controllers using this DLL.
822 */
823 if (!msm_host->use_14lpp_dll)
824 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC3)
825 | CORE_PWRSAVE_DLL),
826 host->ioaddr + CORE_VENDOR_SPEC3);
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700827 mb();
828out:
829 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
830 __func__, ret);
831 return ret;
832}
833
Ritesh Harjaniea709662015-05-27 15:40:24 +0530834static int sdhci_msm_enhanced_strobe(struct sdhci_host *host)
835{
836 int ret = 0;
837 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
838 struct sdhci_msm_host *msm_host = pltfm_host->priv;
839 struct mmc_host *mmc = host->mmc;
840
841 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
842
843 if (!msm_host->enhanced_strobe) {
844 pr_debug("%s: host does not support hs400 enhanced strobe\n",
845 mmc_hostname(mmc));
846 return -EINVAL;
847 }
848
849 if (msm_host->calibration_done ||
850 !(mmc->ios.timing == MMC_TIMING_MMC_HS400)) {
851 return 0;
852 }
853
854 /*
855 * Reset the tuning block.
856 */
857 ret = msm_init_cm_dll(host);
858 if (ret)
859 goto out;
860
861 ret = sdhci_msm_cm_dll_sdc4_calibration(host);
862out:
863 if (!ret)
864 msm_host->calibration_done = true;
865 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
866 __func__, ret);
867 return ret;
868}
869
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700870static int sdhci_msm_hs400_dll_calibration(struct sdhci_host *host)
871{
872 int ret = 0;
873 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
874 struct sdhci_msm_host *msm_host = pltfm_host->priv;
875
876 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
877
878 /*
879 * Retuning in HS400 (DDR mode) will fail, just reset the
880 * tuning block and restore the saved tuning phase.
881 */
882 ret = msm_init_cm_dll(host);
883 if (ret)
884 goto out;
885
886 /* Set the selected phase in delay line hw block */
887 ret = msm_config_cm_dll_phase(host, msm_host->saved_tuning_phase);
888 if (ret)
889 goto out;
890
Krishna Konda0e8efba2014-06-23 14:50:38 -0700891 /* Write 1 to CMD_DAT_TRACK_SEL field in DLL_CONFIG */
892 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
893 | CORE_CMD_DAT_TRACK_SEL),
894 host->ioaddr + CORE_DLL_CONFIG);
895
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700896 if (msm_host->use_cdclp533)
897 /* Calibrate CDCLP533 DLL HW */
898 ret = sdhci_msm_cdclp533_calibration(host);
899 else
900 /* Calibrate CM_DLL_SDC4 HW */
901 ret = sdhci_msm_cm_dll_sdc4_calibration(host);
902out:
903 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
904 __func__, ret);
905 return ret;
906}
907
Krishna Konda96e6b112013-10-28 15:25:03 -0700908static void sdhci_msm_set_mmc_drv_type(struct sdhci_host *host, u32 opcode,
909 u8 drv_type)
910{
911 struct mmc_command cmd = {0};
912 struct mmc_request mrq = {NULL};
913 struct mmc_host *mmc = host->mmc;
914 u8 val = ((drv_type << 4) | 2);
915
916 cmd.opcode = MMC_SWITCH;
917 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
918 (EXT_CSD_HS_TIMING << 16) |
919 (val << 8) |
920 EXT_CSD_CMD_SET_NORMAL;
921 cmd.flags = MMC_CMD_AC | MMC_RSP_R1B;
922 /* 1 sec */
923 cmd.busy_timeout = 1000 * 1000;
924
925 memset(cmd.resp, 0, sizeof(cmd.resp));
926 cmd.retries = 3;
927
928 mrq.cmd = &cmd;
929 cmd.data = NULL;
930
931 mmc_wait_for_req(mmc, &mrq);
932 pr_debug("%s: %s: set card drive type to %d\n",
933 mmc_hostname(mmc), __func__,
934 drv_type);
935}
936
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700937int sdhci_msm_execute_tuning(struct sdhci_host *host, u32 opcode)
938{
939 unsigned long flags;
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530940 int tuning_seq_cnt = 3;
Krishna Konda96e6b112013-10-28 15:25:03 -0700941 u8 phase, *data_buf, tuned_phases[NUM_TUNING_PHASES], tuned_phase_cnt;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700942 const u32 *tuning_block_pattern = tuning_block_64;
943 int size = sizeof(tuning_block_64); /* Tuning pattern size in bytes */
944 int rc;
945 struct mmc_host *mmc = host->mmc;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530946 struct mmc_ios ios = host->mmc->ios;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700947 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
948 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Krishna Konda96e6b112013-10-28 15:25:03 -0700949 u8 drv_type = 0;
950 bool drv_type_changed = false;
951 struct mmc_card *card = host->mmc->card;
Sahitya Tummalafaff7f82015-02-25 14:24:52 +0530952 int sts_retry;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530953
954 /*
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700955 * Tuning is required for SDR104, HS200 and HS400 cards and
956 * if clock frequency is greater than 100MHz in these modes.
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530957 */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700958 if (host->clock <= CORE_FREQ_100MHZ ||
959 !((ios.timing == MMC_TIMING_MMC_HS400) ||
960 (ios.timing == MMC_TIMING_MMC_HS200) ||
961 (ios.timing == MMC_TIMING_UHS_SDR104)))
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530962 return 0;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700963
964 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700965
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700966 /* CDC/SDC4 DLL HW calibration is only required for HS400 mode*/
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700967 if (msm_host->tuning_done && !msm_host->calibration_done &&
968 (mmc->ios.timing == MMC_TIMING_MMC_HS400)) {
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700969 rc = sdhci_msm_hs400_dll_calibration(host);
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700970 spin_lock_irqsave(&host->lock, flags);
971 if (!rc)
972 msm_host->calibration_done = true;
973 spin_unlock_irqrestore(&host->lock, flags);
974 goto out;
975 }
976
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700977 spin_lock_irqsave(&host->lock, flags);
978
979 if ((opcode == MMC_SEND_TUNING_BLOCK_HS200) &&
980 (mmc->ios.bus_width == MMC_BUS_WIDTH_8)) {
981 tuning_block_pattern = tuning_block_128;
982 size = sizeof(tuning_block_128);
983 }
984 spin_unlock_irqrestore(&host->lock, flags);
985
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700986 data_buf = kmalloc(size, GFP_KERNEL);
987 if (!data_buf) {
988 rc = -ENOMEM;
989 goto out;
990 }
991
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530992retry:
Krishna Konda96e6b112013-10-28 15:25:03 -0700993 tuned_phase_cnt = 0;
994
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530995 /* first of all reset the tuning block */
996 rc = msm_init_cm_dll(host);
997 if (rc)
998 goto kfree;
999
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001000 phase = 0;
1001 do {
1002 struct mmc_command cmd = {0};
1003 struct mmc_data data = {0};
1004 struct mmc_request mrq = {
1005 .cmd = &cmd,
1006 .data = &data
1007 };
1008 struct scatterlist sg;
Sahitya Tummalafaff7f82015-02-25 14:24:52 +05301009 struct mmc_command sts_cmd = {0};
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001010
1011 /* set the phase in delay line hw block */
1012 rc = msm_config_cm_dll_phase(host, phase);
1013 if (rc)
1014 goto kfree;
1015
1016 cmd.opcode = opcode;
1017 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1018
1019 data.blksz = size;
1020 data.blocks = 1;
1021 data.flags = MMC_DATA_READ;
1022 data.timeout_ns = 1000 * 1000 * 1000; /* 1 sec */
1023
1024 data.sg = &sg;
1025 data.sg_len = 1;
1026 sg_init_one(&sg, data_buf, size);
1027 memset(data_buf, 0, size);
1028 mmc_wait_for_req(mmc, &mrq);
1029
Sahitya Tummalafaff7f82015-02-25 14:24:52 +05301030 if (card && (cmd.error || data.error)) {
1031 sts_cmd.opcode = MMC_SEND_STATUS;
1032 sts_cmd.arg = card->rca << 16;
1033 sts_cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1034 sts_retry = 5;
1035 while (sts_retry) {
1036 mmc_wait_for_cmd(mmc, &sts_cmd, 0);
1037
1038 if (sts_cmd.error ||
1039 (R1_CURRENT_STATE(sts_cmd.resp[0])
1040 != R1_STATE_TRAN)) {
1041 sts_retry--;
1042 /*
1043 * wait for at least 146 MCLK cycles for
1044 * the card to move to TRANS state. As
1045 * the MCLK would be min 200MHz for
1046 * tuning, we need max 0.73us delay. To
1047 * be on safer side 1ms delay is given.
1048 */
1049 usleep_range(1000, 1200);
1050 pr_debug("%s: phase %d sts cmd err %d resp 0x%x\n",
1051 mmc_hostname(mmc), phase,
1052 sts_cmd.error, sts_cmd.resp[0]);
1053 continue;
1054 }
1055 break;
1056 };
1057 }
1058
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001059 if (!cmd.error && !data.error &&
1060 !memcmp(data_buf, tuning_block_pattern, size)) {
1061 /* tuning is successful at this tuning point */
1062 tuned_phases[tuned_phase_cnt++] = phase;
Krishna Konda96e6b112013-10-28 15:25:03 -07001063 pr_debug("%s: %s: found *** good *** phase = %d\n",
1064 mmc_hostname(mmc), __func__, phase);
1065 } else {
1066 pr_debug("%s: %s: found ## bad ## phase = %d\n",
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001067 mmc_hostname(mmc), __func__, phase);
1068 }
1069 } while (++phase < 16);
1070
Sahitya Tummaladfdb4af2014-04-01 14:29:13 +05301071 if ((tuned_phase_cnt == NUM_TUNING_PHASES) &&
1072 card && mmc_card_mmc(card)) {
Krishna Konda96e6b112013-10-28 15:25:03 -07001073 /*
1074 * If all phases pass then its a problem. So change the card's
1075 * drive type to a different value, if supported and repeat
1076 * tuning until at least one phase fails. Then set the original
1077 * drive type back.
1078 *
1079 * If all the phases still pass after trying all possible
1080 * drive types, then one of those 16 phases will be picked.
1081 * This is no different from what was going on before the
1082 * modification to change drive type and retune.
1083 */
1084 pr_debug("%s: tuned phases count: %d\n", mmc_hostname(mmc),
1085 tuned_phase_cnt);
1086
1087 /* set drive type to other value . default setting is 0x0 */
1088 while (++drv_type <= MAX_DRV_TYPES_SUPPORTED_HS200) {
1089 if (card->ext_csd.raw_driver_strength &
1090 (1 << drv_type)) {
1091 sdhci_msm_set_mmc_drv_type(host, opcode,
1092 drv_type);
1093 if (!drv_type_changed)
1094 drv_type_changed = true;
1095 goto retry;
1096 }
1097 }
1098 }
1099
1100 /* reset drive type to default (50 ohm) if changed */
1101 if (drv_type_changed)
1102 sdhci_msm_set_mmc_drv_type(host, opcode, 0);
1103
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001104 if (tuned_phase_cnt) {
1105 rc = msm_find_most_appropriate_phase(host, tuned_phases,
1106 tuned_phase_cnt);
1107 if (rc < 0)
1108 goto kfree;
1109 else
1110 phase = (u8)rc;
1111
1112 /*
1113 * Finally set the selected phase in delay
1114 * line hw block.
1115 */
1116 rc = msm_config_cm_dll_phase(host, phase);
1117 if (rc)
1118 goto kfree;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07001119 msm_host->saved_tuning_phase = phase;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001120 pr_debug("%s: %s: finally setting the tuning phase to %d\n",
1121 mmc_hostname(mmc), __func__, phase);
1122 } else {
Sahitya Tummala9fe16532013-06-13 10:36:57 +05301123 if (--tuning_seq_cnt)
1124 goto retry;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001125 /* tuning failed */
1126 pr_err("%s: %s: no tuning point found\n",
1127 mmc_hostname(mmc), __func__);
Sahitya Tummala9fe16532013-06-13 10:36:57 +05301128 rc = -EIO;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001129 }
1130
1131kfree:
1132 kfree(data_buf);
1133out:
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07001134 spin_lock_irqsave(&host->lock, flags);
1135 if (!rc)
1136 msm_host->tuning_done = true;
1137 spin_unlock_irqrestore(&host->lock, flags);
1138 pr_debug("%s: Exit %s, err(%d)\n", mmc_hostname(mmc), __func__, rc);
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001139 return rc;
1140}
1141
Asutosh Das0ef24812012-12-18 16:14:02 +05301142static int sdhci_msm_setup_gpio(struct sdhci_msm_pltfm_data *pdata, bool enable)
1143{
1144 struct sdhci_msm_gpio_data *curr;
1145 int i, ret = 0;
1146
1147 curr = pdata->pin_data->gpio_data;
1148 for (i = 0; i < curr->size; i++) {
1149 if (!gpio_is_valid(curr->gpio[i].no)) {
1150 ret = -EINVAL;
1151 pr_err("%s: Invalid gpio = %d\n", __func__,
1152 curr->gpio[i].no);
1153 goto free_gpios;
1154 }
1155 if (enable) {
1156 ret = gpio_request(curr->gpio[i].no,
1157 curr->gpio[i].name);
1158 if (ret) {
1159 pr_err("%s: gpio_request(%d, %s) failed %d\n",
1160 __func__, curr->gpio[i].no,
1161 curr->gpio[i].name, ret);
1162 goto free_gpios;
1163 }
1164 curr->gpio[i].is_enabled = true;
1165 } else {
1166 gpio_free(curr->gpio[i].no);
1167 curr->gpio[i].is_enabled = false;
1168 }
1169 }
1170 return ret;
1171
1172free_gpios:
1173 for (i--; i >= 0; i--) {
1174 gpio_free(curr->gpio[i].no);
1175 curr->gpio[i].is_enabled = false;
1176 }
1177 return ret;
1178}
1179
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301180static int sdhci_msm_setup_pinctrl(struct sdhci_msm_pltfm_data *pdata,
1181 bool enable)
1182{
1183 int ret = 0;
1184
1185 if (enable)
1186 ret = pinctrl_select_state(pdata->pctrl_data->pctrl,
1187 pdata->pctrl_data->pins_active);
1188 else
1189 ret = pinctrl_select_state(pdata->pctrl_data->pctrl,
1190 pdata->pctrl_data->pins_sleep);
1191
1192 if (ret < 0)
1193 pr_err("%s state for pinctrl failed with %d\n",
1194 enable ? "Enabling" : "Disabling", ret);
1195
1196 return ret;
1197}
1198
Asutosh Das0ef24812012-12-18 16:14:02 +05301199static int sdhci_msm_setup_pins(struct sdhci_msm_pltfm_data *pdata, bool enable)
1200{
1201 int ret = 0;
1202
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301203 if (pdata->pin_cfg_sts == enable) {
Asutosh Das0ef24812012-12-18 16:14:02 +05301204 return 0;
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301205 } else if (pdata->pctrl_data) {
1206 ret = sdhci_msm_setup_pinctrl(pdata, enable);
1207 goto out;
1208 } else if (!pdata->pin_data) {
1209 return 0;
1210 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301211
Sahitya Tummala1cd7e072014-02-14 13:19:01 +05301212 if (pdata->pin_data->is_gpio)
1213 ret = sdhci_msm_setup_gpio(pdata, enable);
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301214out:
Asutosh Das0ef24812012-12-18 16:14:02 +05301215 if (!ret)
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301216 pdata->pin_cfg_sts = enable;
Asutosh Das0ef24812012-12-18 16:14:02 +05301217
1218 return ret;
1219}
1220
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301221static int sdhci_msm_dt_get_array(struct device *dev, const char *prop_name,
1222 u32 **out, int *len, u32 size)
1223{
1224 int ret = 0;
1225 struct device_node *np = dev->of_node;
1226 size_t sz;
1227 u32 *arr = NULL;
1228
1229 if (!of_get_property(np, prop_name, len)) {
1230 ret = -EINVAL;
1231 goto out;
1232 }
1233 sz = *len = *len / sizeof(*arr);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07001234 if (sz <= 0 || (size > 0 && (sz > size))) {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301235 dev_err(dev, "%s invalid size\n", prop_name);
1236 ret = -EINVAL;
1237 goto out;
1238 }
1239
1240 arr = devm_kzalloc(dev, sz * sizeof(*arr), GFP_KERNEL);
1241 if (!arr) {
1242 dev_err(dev, "%s failed allocating memory\n", prop_name);
1243 ret = -ENOMEM;
1244 goto out;
1245 }
1246
1247 ret = of_property_read_u32_array(np, prop_name, arr, sz);
1248 if (ret < 0) {
1249 dev_err(dev, "%s failed reading array %d\n", prop_name, ret);
1250 goto out;
1251 }
1252 *out = arr;
1253out:
1254 if (ret)
1255 *len = 0;
1256 return ret;
1257}
1258
Asutosh Das0ef24812012-12-18 16:14:02 +05301259#define MAX_PROP_SIZE 32
1260static int sdhci_msm_dt_parse_vreg_info(struct device *dev,
1261 struct sdhci_msm_reg_data **vreg_data, const char *vreg_name)
1262{
1263 int len, ret = 0;
1264 const __be32 *prop;
1265 char prop_name[MAX_PROP_SIZE];
1266 struct sdhci_msm_reg_data *vreg;
1267 struct device_node *np = dev->of_node;
1268
1269 snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", vreg_name);
1270 if (!of_parse_phandle(np, prop_name, 0)) {
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301271 dev_info(dev, "No vreg data found for %s\n", vreg_name);
Asutosh Das0ef24812012-12-18 16:14:02 +05301272 return ret;
1273 }
1274
1275 vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
1276 if (!vreg) {
1277 dev_err(dev, "No memory for vreg: %s\n", vreg_name);
1278 ret = -ENOMEM;
1279 return ret;
1280 }
1281
1282 vreg->name = vreg_name;
1283
1284 snprintf(prop_name, MAX_PROP_SIZE,
1285 "qcom,%s-always-on", vreg_name);
1286 if (of_get_property(np, prop_name, NULL))
1287 vreg->is_always_on = true;
1288
1289 snprintf(prop_name, MAX_PROP_SIZE,
1290 "qcom,%s-lpm-sup", vreg_name);
1291 if (of_get_property(np, prop_name, NULL))
1292 vreg->lpm_sup = true;
1293
1294 snprintf(prop_name, MAX_PROP_SIZE,
1295 "qcom,%s-voltage-level", vreg_name);
1296 prop = of_get_property(np, prop_name, &len);
1297 if (!prop || (len != (2 * sizeof(__be32)))) {
1298 dev_warn(dev, "%s %s property\n",
1299 prop ? "invalid format" : "no", prop_name);
1300 } else {
1301 vreg->low_vol_level = be32_to_cpup(&prop[0]);
1302 vreg->high_vol_level = be32_to_cpup(&prop[1]);
1303 }
1304
1305 snprintf(prop_name, MAX_PROP_SIZE,
1306 "qcom,%s-current-level", vreg_name);
1307 prop = of_get_property(np, prop_name, &len);
1308 if (!prop || (len != (2 * sizeof(__be32)))) {
1309 dev_warn(dev, "%s %s property\n",
1310 prop ? "invalid format" : "no", prop_name);
1311 } else {
1312 vreg->lpm_uA = be32_to_cpup(&prop[0]);
1313 vreg->hpm_uA = be32_to_cpup(&prop[1]);
1314 }
1315
1316 *vreg_data = vreg;
1317 dev_dbg(dev, "%s: %s %s vol=[%d %d]uV, curr=[%d %d]uA\n",
1318 vreg->name, vreg->is_always_on ? "always_on," : "",
1319 vreg->lpm_sup ? "lpm_sup," : "", vreg->low_vol_level,
1320 vreg->high_vol_level, vreg->lpm_uA, vreg->hpm_uA);
1321
1322 return ret;
1323}
1324
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301325static int sdhci_msm_parse_pinctrl_info(struct device *dev,
1326 struct sdhci_msm_pltfm_data *pdata)
1327{
1328 struct sdhci_pinctrl_data *pctrl_data;
1329 struct pinctrl *pctrl;
1330 int ret = 0;
1331
1332 /* Try to obtain pinctrl handle */
1333 pctrl = devm_pinctrl_get(dev);
1334 if (IS_ERR(pctrl)) {
1335 ret = PTR_ERR(pctrl);
1336 goto out;
1337 }
1338 pctrl_data = devm_kzalloc(dev, sizeof(*pctrl_data), GFP_KERNEL);
1339 if (!pctrl_data) {
1340 dev_err(dev, "No memory for sdhci_pinctrl_data\n");
1341 ret = -ENOMEM;
1342 goto out;
1343 }
1344 pctrl_data->pctrl = pctrl;
1345 /* Look-up and keep the states handy to be used later */
1346 pctrl_data->pins_active = pinctrl_lookup_state(
1347 pctrl_data->pctrl, "active");
1348 if (IS_ERR(pctrl_data->pins_active)) {
1349 ret = PTR_ERR(pctrl_data->pins_active);
1350 dev_err(dev, "Could not get active pinstates, err:%d\n", ret);
1351 goto out;
1352 }
1353 pctrl_data->pins_sleep = pinctrl_lookup_state(
1354 pctrl_data->pctrl, "sleep");
1355 if (IS_ERR(pctrl_data->pins_sleep)) {
1356 ret = PTR_ERR(pctrl_data->pins_sleep);
1357 dev_err(dev, "Could not get sleep pinstates, err:%d\n", ret);
1358 goto out;
1359 }
1360 pdata->pctrl_data = pctrl_data;
1361out:
1362 return ret;
1363}
1364
Asutosh Das0ef24812012-12-18 16:14:02 +05301365#define GPIO_NAME_MAX_LEN 32
1366static int sdhci_msm_dt_parse_gpio_info(struct device *dev,
1367 struct sdhci_msm_pltfm_data *pdata)
1368{
1369 int ret = 0, cnt, i;
1370 struct sdhci_msm_pin_data *pin_data;
1371 struct device_node *np = dev->of_node;
1372
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301373 ret = sdhci_msm_parse_pinctrl_info(dev, pdata);
1374 if (!ret) {
1375 goto out;
1376 } else if (ret == -EPROBE_DEFER) {
1377 dev_err(dev, "Pinctrl framework not registered, err:%d\n", ret);
1378 goto out;
1379 } else {
1380 dev_err(dev, "Parsing Pinctrl failed with %d, falling back on GPIO lib\n",
1381 ret);
Sahitya Tummala1cd7e072014-02-14 13:19:01 +05301382 ret = 0;
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301383 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301384 pin_data = devm_kzalloc(dev, sizeof(*pin_data), GFP_KERNEL);
1385 if (!pin_data) {
1386 dev_err(dev, "No memory for pin_data\n");
1387 ret = -ENOMEM;
1388 goto out;
1389 }
1390
1391 cnt = of_gpio_count(np);
1392 if (cnt > 0) {
1393 pin_data->gpio_data = devm_kzalloc(dev,
1394 sizeof(struct sdhci_msm_gpio_data), GFP_KERNEL);
1395 if (!pin_data->gpio_data) {
1396 dev_err(dev, "No memory for gpio_data\n");
1397 ret = -ENOMEM;
1398 goto out;
1399 }
1400 pin_data->gpio_data->size = cnt;
1401 pin_data->gpio_data->gpio = devm_kzalloc(dev, cnt *
1402 sizeof(struct sdhci_msm_gpio), GFP_KERNEL);
1403
1404 if (!pin_data->gpio_data->gpio) {
1405 dev_err(dev, "No memory for gpio\n");
1406 ret = -ENOMEM;
1407 goto out;
1408 }
1409
1410 for (i = 0; i < cnt; i++) {
1411 const char *name = NULL;
1412 char result[GPIO_NAME_MAX_LEN];
1413 pin_data->gpio_data->gpio[i].no = of_get_gpio(np, i);
1414 of_property_read_string_index(np,
1415 "qcom,gpio-names", i, &name);
1416
1417 snprintf(result, GPIO_NAME_MAX_LEN, "%s-%s",
1418 dev_name(dev), name ? name : "?");
1419 pin_data->gpio_data->gpio[i].name = result;
1420 dev_dbg(dev, "%s: gpio[%s] = %d\n", __func__,
1421 pin_data->gpio_data->gpio[i].name,
1422 pin_data->gpio_data->gpio[i].no);
Asutosh Das0ef24812012-12-18 16:14:02 +05301423 }
1424 }
Sahitya Tummala1cd7e072014-02-14 13:19:01 +05301425 pdata->pin_data = pin_data;
Asutosh Das0ef24812012-12-18 16:14:02 +05301426out:
1427 if (ret)
1428 dev_err(dev, "%s failed with err %d\n", __func__, ret);
1429 return ret;
1430}
1431
Maya Erez994cf2f2014-10-21 20:22:04 +03001432#ifdef CONFIG_SMP
1433static void sdhci_msm_populate_affinity_type(struct sdhci_msm_pltfm_data *pdata,
1434 struct device_node *np)
1435{
1436 const char *cpu_affinity = NULL;
1437
1438 pdata->cpu_affinity_type = PM_QOS_REQ_AFFINE_IRQ;
1439 if (!of_property_read_string(np, "qcom,cpu-affinity",
1440 &cpu_affinity)) {
1441 if (!strcmp(cpu_affinity, "all_cores"))
1442 pdata->cpu_affinity_type = PM_QOS_REQ_ALL_CORES;
1443 else if (!strcmp(cpu_affinity, "affine_cores"))
1444 pdata->cpu_affinity_type = PM_QOS_REQ_AFFINE_CORES;
1445 else if (!strcmp(cpu_affinity, "affine_irq"))
1446 pdata->cpu_affinity_type = PM_QOS_REQ_AFFINE_IRQ;
1447 }
1448}
1449#else
1450static void sdhci_msm_populate_affinity_type(struct sdhci_msm_pltfm_data *pdata,
1451 struct device_node *np)
1452{
1453}
1454#endif
1455
Asutosh Das0ef24812012-12-18 16:14:02 +05301456/* Parse platform data */
Dov Levenglickc9033ab2015-03-10 16:00:56 +02001457static
1458struct sdhci_msm_pltfm_data *sdhci_msm_populate_pdata(struct device *dev,
1459 struct sdhci_msm_host *msm_host)
Asutosh Das0ef24812012-12-18 16:14:02 +05301460{
1461 struct sdhci_msm_pltfm_data *pdata = NULL;
1462 struct device_node *np = dev->of_node;
1463 u32 bus_width = 0;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05301464 u32 cpu_dma_latency;
Asutosh Das0ef24812012-12-18 16:14:02 +05301465 int len, i;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301466 int clk_table_len;
1467 u32 *clk_table = NULL;
Sujit Reddy Thumma1958d3d2013-06-03 09:54:32 +05301468 enum of_gpio_flags flags = OF_GPIO_ACTIVE_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05301469
1470 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1471 if (!pdata) {
1472 dev_err(dev, "failed to allocate memory for platform data\n");
1473 goto out;
1474 }
1475
Sujit Reddy Thumma1958d3d2013-06-03 09:54:32 +05301476 pdata->status_gpio = of_get_named_gpio_flags(np, "cd-gpios", 0, &flags);
1477 if (gpio_is_valid(pdata->status_gpio) & !(flags & OF_GPIO_ACTIVE_LOW))
1478 pdata->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
Sahitya Tummala581df132013-03-12 14:57:46 +05301479
Asutosh Das0ef24812012-12-18 16:14:02 +05301480 of_property_read_u32(np, "qcom,bus-width", &bus_width);
1481 if (bus_width == 8)
1482 pdata->mmc_bus_width = MMC_CAP_8_BIT_DATA;
1483 else if (bus_width == 4)
1484 pdata->mmc_bus_width = MMC_CAP_4_BIT_DATA;
1485 else {
1486 dev_notice(dev, "invalid bus-width, default to 1-bit mode\n");
1487 pdata->mmc_bus_width = 0;
1488 }
1489
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05301490 if (!of_property_read_u32(np, "qcom,cpu-dma-latency-us",
1491 &cpu_dma_latency))
1492 pdata->cpu_dma_latency_us = cpu_dma_latency;
Asutosh Das598a4d42014-02-05 16:38:23 +05301493 else
1494 pdata->cpu_dma_latency_us = MSM_MMC_DEFAULT_CPU_DMA_LATENCY;
Talel Shenhar7dc5f792015-05-18 12:12:48 +03001495
1496 if (sdhci_msm_dt_get_array(dev, "qcom,devfreq,freq-table",
1497 &msm_host->mmc->clk_scaling.freq_table,
1498 &msm_host->mmc->clk_scaling.freq_table_sz, 0))
1499 pr_debug("%s: no clock scaling frequencies were supplied\n",
1500 dev_name(dev));
1501 else if (!msm_host->mmc->clk_scaling.freq_table ||
1502 !msm_host->mmc->clk_scaling.freq_table_sz)
1503 dev_err(dev, "bad dts clock scaling frequencies\n");
1504
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301505 if (sdhci_msm_dt_get_array(dev, "qcom,clk-rates",
1506 &clk_table, &clk_table_len, 0)) {
1507 dev_err(dev, "failed parsing supported clock rates\n");
1508 goto out;
1509 }
1510 if (!clk_table || !clk_table_len) {
1511 dev_err(dev, "Invalid clock table\n");
1512 goto out;
1513 }
1514 pdata->sup_clk_table = clk_table;
1515 pdata->sup_clk_cnt = clk_table_len;
1516
Asutosh Das0ef24812012-12-18 16:14:02 +05301517 pdata->vreg_data = devm_kzalloc(dev, sizeof(struct
1518 sdhci_msm_slot_reg_data),
1519 GFP_KERNEL);
1520 if (!pdata->vreg_data) {
1521 dev_err(dev, "failed to allocate memory for vreg data\n");
1522 goto out;
1523 }
1524
1525 if (sdhci_msm_dt_parse_vreg_info(dev, &pdata->vreg_data->vdd_data,
1526 "vdd")) {
1527 dev_err(dev, "failed parsing vdd data\n");
1528 goto out;
1529 }
1530 if (sdhci_msm_dt_parse_vreg_info(dev,
1531 &pdata->vreg_data->vdd_io_data,
1532 "vdd-io")) {
1533 dev_err(dev, "failed parsing vdd-io data\n");
1534 goto out;
1535 }
1536
1537 if (sdhci_msm_dt_parse_gpio_info(dev, pdata)) {
1538 dev_err(dev, "failed parsing gpio data\n");
1539 goto out;
1540 }
1541
Asutosh Das0ef24812012-12-18 16:14:02 +05301542 len = of_property_count_strings(np, "qcom,bus-speed-mode");
1543
1544 for (i = 0; i < len; i++) {
1545 const char *name = NULL;
1546
1547 of_property_read_string_index(np,
1548 "qcom,bus-speed-mode", i, &name);
1549 if (!name)
1550 continue;
1551
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07001552 if (!strncmp(name, "HS400_1p8v", sizeof("HS400_1p8v")))
1553 pdata->caps2 |= MMC_CAP2_HS400_1_8V;
1554 else if (!strncmp(name, "HS400_1p2v", sizeof("HS400_1p2v")))
1555 pdata->caps2 |= MMC_CAP2_HS400_1_2V;
1556 else if (!strncmp(name, "HS200_1p8v", sizeof("HS200_1p8v")))
Asutosh Das0ef24812012-12-18 16:14:02 +05301557 pdata->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
1558 else if (!strncmp(name, "HS200_1p2v", sizeof("HS200_1p2v")))
1559 pdata->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
1560 else if (!strncmp(name, "DDR_1p8v", sizeof("DDR_1p8v")))
1561 pdata->caps |= MMC_CAP_1_8V_DDR
1562 | MMC_CAP_UHS_DDR50;
1563 else if (!strncmp(name, "DDR_1p2v", sizeof("DDR_1p2v")))
1564 pdata->caps |= MMC_CAP_1_2V_DDR
1565 | MMC_CAP_UHS_DDR50;
1566 }
1567
1568 if (of_get_property(np, "qcom,nonremovable", NULL))
1569 pdata->nonremovable = true;
1570
Guoping Yuf7c91332014-08-20 16:56:18 +08001571 if (of_get_property(np, "qcom,nonhotplug", NULL))
1572 pdata->nonhotplug = true;
1573
Venkat Gopalakrishnan9a62e042015-03-03 16:14:55 -08001574 pdata->largeaddressbus =
1575 of_property_read_bool(np, "qcom,large-address-bus");
1576
Maya Erez994cf2f2014-10-21 20:22:04 +03001577 sdhci_msm_populate_affinity_type(pdata, np);
1578
Dov Levenglickc9033ab2015-03-10 16:00:56 +02001579 if (of_property_read_bool(np, "qcom,wakeup-on-idle"))
1580 msm_host->mmc->wakeup_on_idle = true;
1581
Asutosh Das0ef24812012-12-18 16:14:02 +05301582 return pdata;
1583out:
1584 return NULL;
1585}
1586
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301587/* Returns required bandwidth in Bytes per Sec */
1588static unsigned int sdhci_get_bw_required(struct sdhci_host *host,
1589 struct mmc_ios *ios)
1590{
Sahitya Tummala2886c922013-04-03 18:03:31 +05301591 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1592 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1593
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301594 unsigned int bw;
1595
Sahitya Tummala2886c922013-04-03 18:03:31 +05301596 bw = msm_host->clk_rate;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301597 /*
1598 * For DDR mode, SDCC controller clock will be at
1599 * the double rate than the actual clock that goes to card.
1600 */
1601 if (ios->bus_width == MMC_BUS_WIDTH_4)
1602 bw /= 2;
1603 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1604 bw /= 8;
1605
1606 return bw;
1607}
1608
1609static int sdhci_msm_bus_get_vote_for_bw(struct sdhci_msm_host *host,
1610 unsigned int bw)
1611{
1612 unsigned int *table = host->pdata->voting_data->bw_vecs;
1613 unsigned int size = host->pdata->voting_data->bw_vecs_size;
1614 int i;
1615
1616 if (host->msm_bus_vote.is_max_bw_needed && bw)
1617 return host->msm_bus_vote.max_bw_vote;
1618
1619 for (i = 0; i < size; i++) {
1620 if (bw <= table[i])
1621 break;
1622 }
1623
1624 if (i && (i == size))
1625 i--;
1626
1627 return i;
1628}
1629
1630/*
1631 * This function must be called with host lock acquired.
1632 * Caller of this function should also ensure that msm bus client
1633 * handle is not null.
1634 */
1635static inline int sdhci_msm_bus_set_vote(struct sdhci_msm_host *msm_host,
1636 int vote,
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301637 unsigned long *flags)
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301638{
1639 struct sdhci_host *host = platform_get_drvdata(msm_host->pdev);
1640 int rc = 0;
1641
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301642 BUG_ON(!flags);
1643
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301644 if (vote != msm_host->msm_bus_vote.curr_vote) {
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301645 spin_unlock_irqrestore(&host->lock, *flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301646 rc = msm_bus_scale_client_update_request(
1647 msm_host->msm_bus_vote.client_handle, vote);
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301648 spin_lock_irqsave(&host->lock, *flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301649 if (rc) {
1650 pr_err("%s: msm_bus_scale_client_update_request() failed: bus_client_handle=0x%x, vote=%d, err=%d\n",
1651 mmc_hostname(host->mmc),
1652 msm_host->msm_bus_vote.client_handle, vote, rc);
1653 goto out;
1654 }
1655 msm_host->msm_bus_vote.curr_vote = vote;
1656 }
1657out:
1658 return rc;
1659}
1660
1661/*
1662 * Internal work. Work to set 0 bandwidth for msm bus.
1663 */
1664static void sdhci_msm_bus_work(struct work_struct *work)
1665{
1666 struct sdhci_msm_host *msm_host;
1667 struct sdhci_host *host;
1668 unsigned long flags;
1669
1670 msm_host = container_of(work, struct sdhci_msm_host,
1671 msm_bus_vote.vote_work.work);
1672 host = platform_get_drvdata(msm_host->pdev);
1673
1674 if (!msm_host->msm_bus_vote.client_handle)
1675 return;
1676
1677 spin_lock_irqsave(&host->lock, flags);
1678 /* don't vote for 0 bandwidth if any request is in progress */
1679 if (!host->mrq) {
1680 sdhci_msm_bus_set_vote(msm_host,
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301681 msm_host->msm_bus_vote.min_bw_vote, &flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301682 } else
1683 pr_warning("%s: %s: Transfer in progress. skipping bus voting to 0 bandwidth\n",
1684 mmc_hostname(host->mmc), __func__);
1685 spin_unlock_irqrestore(&host->lock, flags);
1686}
1687
1688/*
1689 * This function cancels any scheduled delayed work and sets the bus
1690 * vote based on bw (bandwidth) argument.
1691 */
1692static void sdhci_msm_bus_cancel_work_and_set_vote(struct sdhci_host *host,
1693 unsigned int bw)
1694{
1695 int vote;
1696 unsigned long flags;
1697 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1698 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1699
1700 cancel_delayed_work_sync(&msm_host->msm_bus_vote.vote_work);
1701 spin_lock_irqsave(&host->lock, flags);
1702 vote = sdhci_msm_bus_get_vote_for_bw(msm_host, bw);
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301703 sdhci_msm_bus_set_vote(msm_host, vote, &flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301704 spin_unlock_irqrestore(&host->lock, flags);
1705}
1706
1707#define MSM_MMC_BUS_VOTING_DELAY 200 /* msecs */
1708
1709/* This function queues a work which will set the bandwidth requiement to 0 */
1710static void sdhci_msm_bus_queue_work(struct sdhci_host *host)
1711{
1712 unsigned long flags;
1713 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1714 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1715
1716 spin_lock_irqsave(&host->lock, flags);
1717 if (msm_host->msm_bus_vote.min_bw_vote !=
1718 msm_host->msm_bus_vote.curr_vote)
1719 queue_delayed_work(system_wq,
1720 &msm_host->msm_bus_vote.vote_work,
1721 msecs_to_jiffies(MSM_MMC_BUS_VOTING_DELAY));
1722 spin_unlock_irqrestore(&host->lock, flags);
1723}
1724
1725static int sdhci_msm_bus_register(struct sdhci_msm_host *host,
1726 struct platform_device *pdev)
1727{
1728 int rc = 0;
1729 struct msm_bus_scale_pdata *bus_pdata;
1730
1731 struct sdhci_msm_bus_voting_data *data;
1732 struct device *dev = &pdev->dev;
1733
1734 data = devm_kzalloc(dev,
1735 sizeof(struct sdhci_msm_bus_voting_data), GFP_KERNEL);
1736 if (!data) {
1737 dev_err(&pdev->dev,
1738 "%s: failed to allocate memory\n", __func__);
1739 rc = -ENOMEM;
1740 goto out;
1741 }
1742 data->bus_pdata = msm_bus_cl_get_pdata(pdev);
1743 if (data->bus_pdata) {
1744 rc = sdhci_msm_dt_get_array(dev, "qcom,bus-bw-vectors-bps",
1745 &data->bw_vecs, &data->bw_vecs_size, 0);
1746 if (rc) {
1747 dev_err(&pdev->dev,
1748 "%s: Failed to get bus-bw-vectors-bps\n",
1749 __func__);
1750 goto out;
1751 }
1752 host->pdata->voting_data = data;
1753 }
1754 if (host->pdata->voting_data &&
1755 host->pdata->voting_data->bus_pdata &&
1756 host->pdata->voting_data->bw_vecs &&
1757 host->pdata->voting_data->bw_vecs_size) {
1758
1759 bus_pdata = host->pdata->voting_data->bus_pdata;
1760 host->msm_bus_vote.client_handle =
1761 msm_bus_scale_register_client(bus_pdata);
1762 if (!host->msm_bus_vote.client_handle) {
1763 dev_err(&pdev->dev, "msm_bus_scale_register_client()\n");
1764 rc = -EFAULT;
1765 goto out;
1766 }
1767 /* cache the vote index for minimum and maximum bandwidth */
1768 host->msm_bus_vote.min_bw_vote =
1769 sdhci_msm_bus_get_vote_for_bw(host, 0);
1770 host->msm_bus_vote.max_bw_vote =
1771 sdhci_msm_bus_get_vote_for_bw(host, UINT_MAX);
1772 } else {
1773 devm_kfree(dev, data);
1774 }
1775
1776out:
1777 return rc;
1778}
1779
1780static void sdhci_msm_bus_unregister(struct sdhci_msm_host *host)
1781{
1782 if (host->msm_bus_vote.client_handle)
1783 msm_bus_scale_unregister_client(
1784 host->msm_bus_vote.client_handle);
1785}
1786
1787static void sdhci_msm_bus_voting(struct sdhci_host *host, u32 enable)
1788{
1789 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1790 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1791 struct mmc_ios *ios = &host->mmc->ios;
1792 unsigned int bw;
1793
1794 if (!msm_host->msm_bus_vote.client_handle)
1795 return;
1796
1797 bw = sdhci_get_bw_required(host, ios);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301798 if (enable) {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301799 sdhci_msm_bus_cancel_work_and_set_vote(host, bw);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301800 } else {
1801 /*
1802 * If clock gating is enabled, then remove the vote
1803 * immediately because clocks will be disabled only
1804 * after SDHCI_MSM_MMC_CLK_GATE_DELAY and thus no
1805 * additional delay is required to remove the bus vote.
1806 */
1807#ifdef CONFIG_MMC_CLKGATE
1808 if (host->mmc->clkgate_delay)
1809 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
1810 else
1811#endif
1812 sdhci_msm_bus_queue_work(host);
1813 }
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301814}
1815
Asutosh Das0ef24812012-12-18 16:14:02 +05301816/* Regulator utility functions */
1817static int sdhci_msm_vreg_init_reg(struct device *dev,
1818 struct sdhci_msm_reg_data *vreg)
1819{
1820 int ret = 0;
1821
1822 /* check if regulator is already initialized? */
1823 if (vreg->reg)
1824 goto out;
1825
1826 /* Get the regulator handle */
1827 vreg->reg = devm_regulator_get(dev, vreg->name);
1828 if (IS_ERR(vreg->reg)) {
1829 ret = PTR_ERR(vreg->reg);
1830 pr_err("%s: devm_regulator_get(%s) failed. ret=%d\n",
1831 __func__, vreg->name, ret);
1832 goto out;
1833 }
1834
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301835 if (regulator_count_voltages(vreg->reg) > 0) {
1836 vreg->set_voltage_sup = true;
1837 /* sanity check */
1838 if (!vreg->high_vol_level || !vreg->hpm_uA) {
1839 pr_err("%s: %s invalid constraints specified\n",
1840 __func__, vreg->name);
1841 ret = -EINVAL;
1842 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301843 }
1844
1845out:
1846 return ret;
1847}
1848
1849static void sdhci_msm_vreg_deinit_reg(struct sdhci_msm_reg_data *vreg)
1850{
1851 if (vreg->reg)
1852 devm_regulator_put(vreg->reg);
1853}
1854
1855static int sdhci_msm_vreg_set_optimum_mode(struct sdhci_msm_reg_data
1856 *vreg, int uA_load)
1857{
1858 int ret = 0;
1859
1860 /*
1861 * regulators that do not support regulator_set_voltage also
1862 * do not support regulator_set_optimum_mode
1863 */
1864 if (vreg->set_voltage_sup) {
1865 ret = regulator_set_load(vreg->reg, uA_load);
1866 if (ret < 0)
1867 pr_err("%s: regulator_set_load(reg=%s,uA_load=%d) failed. ret=%d\n",
1868 __func__, vreg->name, uA_load, ret);
1869 else
1870 /*
1871 * regulator_set_load() can return non zero
1872 * value even for success case.
1873 */
1874 ret = 0;
1875 }
1876 return ret;
1877}
1878
1879static int sdhci_msm_vreg_set_voltage(struct sdhci_msm_reg_data *vreg,
1880 int min_uV, int max_uV)
1881{
1882 int ret = 0;
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301883 if (vreg->set_voltage_sup) {
1884 ret = regulator_set_voltage(vreg->reg, min_uV, max_uV);
1885 if (ret) {
1886 pr_err("%s: regulator_set_voltage(%s)failed. min_uV=%d,max_uV=%d,ret=%d\n",
Asutosh Das0ef24812012-12-18 16:14:02 +05301887 __func__, vreg->name, min_uV, max_uV, ret);
1888 }
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301889 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301890
1891 return ret;
1892}
1893
1894static int sdhci_msm_vreg_enable(struct sdhci_msm_reg_data *vreg)
1895{
1896 int ret = 0;
1897
1898 /* Put regulator in HPM (high power mode) */
1899 ret = sdhci_msm_vreg_set_optimum_mode(vreg, vreg->hpm_uA);
1900 if (ret < 0)
1901 return ret;
1902
1903 if (!vreg->is_enabled) {
1904 /* Set voltage level */
1905 ret = sdhci_msm_vreg_set_voltage(vreg, vreg->high_vol_level,
1906 vreg->high_vol_level);
1907 if (ret)
1908 return ret;
1909 }
1910 ret = regulator_enable(vreg->reg);
1911 if (ret) {
1912 pr_err("%s: regulator_enable(%s) failed. ret=%d\n",
1913 __func__, vreg->name, ret);
1914 return ret;
1915 }
1916 vreg->is_enabled = true;
1917 return ret;
1918}
1919
1920static int sdhci_msm_vreg_disable(struct sdhci_msm_reg_data *vreg)
1921{
1922 int ret = 0;
1923
1924 /* Never disable regulator marked as always_on */
1925 if (vreg->is_enabled && !vreg->is_always_on) {
1926 ret = regulator_disable(vreg->reg);
1927 if (ret) {
1928 pr_err("%s: regulator_disable(%s) failed. ret=%d\n",
1929 __func__, vreg->name, ret);
1930 goto out;
1931 }
1932 vreg->is_enabled = false;
1933
1934 ret = sdhci_msm_vreg_set_optimum_mode(vreg, 0);
1935 if (ret < 0)
1936 goto out;
1937
1938 /* Set min. voltage level to 0 */
1939 ret = sdhci_msm_vreg_set_voltage(vreg, 0, vreg->high_vol_level);
1940 if (ret)
1941 goto out;
1942 } else if (vreg->is_enabled && vreg->is_always_on) {
1943 if (vreg->lpm_sup) {
1944 /* Put always_on regulator in LPM (low power mode) */
1945 ret = sdhci_msm_vreg_set_optimum_mode(vreg,
1946 vreg->lpm_uA);
1947 if (ret < 0)
1948 goto out;
1949 }
1950 }
1951out:
1952 return ret;
1953}
1954
1955static int sdhci_msm_setup_vreg(struct sdhci_msm_pltfm_data *pdata,
1956 bool enable, bool is_init)
1957{
1958 int ret = 0, i;
1959 struct sdhci_msm_slot_reg_data *curr_slot;
1960 struct sdhci_msm_reg_data *vreg_table[2];
1961
1962 curr_slot = pdata->vreg_data;
1963 if (!curr_slot) {
1964 pr_debug("%s: vreg info unavailable,assuming the slot is powered by always on domain\n",
1965 __func__);
1966 goto out;
1967 }
1968
1969 vreg_table[0] = curr_slot->vdd_data;
1970 vreg_table[1] = curr_slot->vdd_io_data;
1971
1972 for (i = 0; i < ARRAY_SIZE(vreg_table); i++) {
1973 if (vreg_table[i]) {
1974 if (enable)
1975 ret = sdhci_msm_vreg_enable(vreg_table[i]);
1976 else
1977 ret = sdhci_msm_vreg_disable(vreg_table[i]);
1978 if (ret)
1979 goto out;
1980 }
1981 }
1982out:
1983 return ret;
1984}
1985
1986/*
1987 * Reset vreg by ensuring it is off during probe. A call
1988 * to enable vreg is needed to balance disable vreg
1989 */
1990static int sdhci_msm_vreg_reset(struct sdhci_msm_pltfm_data *pdata)
1991{
1992 int ret;
1993
1994 ret = sdhci_msm_setup_vreg(pdata, 1, true);
1995 if (ret)
1996 return ret;
1997 ret = sdhci_msm_setup_vreg(pdata, 0, true);
1998 return ret;
1999}
2000
2001/* This init function should be called only once for each SDHC slot */
2002static int sdhci_msm_vreg_init(struct device *dev,
2003 struct sdhci_msm_pltfm_data *pdata,
2004 bool is_init)
2005{
2006 int ret = 0;
2007 struct sdhci_msm_slot_reg_data *curr_slot;
2008 struct sdhci_msm_reg_data *curr_vdd_reg, *curr_vdd_io_reg;
2009
2010 curr_slot = pdata->vreg_data;
2011 if (!curr_slot)
2012 goto out;
2013
2014 curr_vdd_reg = curr_slot->vdd_data;
2015 curr_vdd_io_reg = curr_slot->vdd_io_data;
2016
2017 if (!is_init)
2018 /* Deregister all regulators from regulator framework */
2019 goto vdd_io_reg_deinit;
2020
2021 /*
2022 * Get the regulator handle from voltage regulator framework
2023 * and then try to set the voltage level for the regulator
2024 */
2025 if (curr_vdd_reg) {
2026 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_reg);
2027 if (ret)
2028 goto out;
2029 }
2030 if (curr_vdd_io_reg) {
2031 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_io_reg);
2032 if (ret)
2033 goto vdd_reg_deinit;
2034 }
2035 ret = sdhci_msm_vreg_reset(pdata);
2036 if (ret)
2037 dev_err(dev, "vreg reset failed (%d)\n", ret);
2038 goto out;
2039
2040vdd_io_reg_deinit:
2041 if (curr_vdd_io_reg)
2042 sdhci_msm_vreg_deinit_reg(curr_vdd_io_reg);
2043vdd_reg_deinit:
2044 if (curr_vdd_reg)
2045 sdhci_msm_vreg_deinit_reg(curr_vdd_reg);
2046out:
2047 return ret;
2048}
2049
2050
2051static int sdhci_msm_set_vdd_io_vol(struct sdhci_msm_pltfm_data *pdata,
2052 enum vdd_io_level level,
2053 unsigned int voltage_level)
2054{
2055 int ret = 0;
2056 int set_level;
2057 struct sdhci_msm_reg_data *vdd_io_reg;
2058
2059 if (!pdata->vreg_data)
2060 return ret;
2061
2062 vdd_io_reg = pdata->vreg_data->vdd_io_data;
2063 if (vdd_io_reg && vdd_io_reg->is_enabled) {
2064 switch (level) {
2065 case VDD_IO_LOW:
2066 set_level = vdd_io_reg->low_vol_level;
2067 break;
2068 case VDD_IO_HIGH:
2069 set_level = vdd_io_reg->high_vol_level;
2070 break;
2071 case VDD_IO_SET_LEVEL:
2072 set_level = voltage_level;
2073 break;
2074 default:
2075 pr_err("%s: invalid argument level = %d",
2076 __func__, level);
2077 ret = -EINVAL;
2078 return ret;
2079 }
2080 ret = sdhci_msm_vreg_set_voltage(vdd_io_reg, set_level,
2081 set_level);
2082 }
2083 return ret;
2084}
2085
Pavan Anamula53ffa0b2015-07-22 21:46:32 +05302086void sdhci_msm_dump_pwr_ctrl_regs(struct sdhci_host *host)
2087{
2088 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2089 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2090
2091 pr_err("%s: PWRCTL_STATUS: 0x%08x | PWRCTL_MASK: 0x%08x | PWRCTL_CTL: 0x%08x\n",
2092 mmc_hostname(host->mmc),
2093 readl_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS),
2094 readl_relaxed(msm_host->core_mem + CORE_PWRCTL_MASK),
2095 readl_relaxed(msm_host->core_mem + CORE_PWRCTL_CTL));
2096}
2097
Asutosh Das0ef24812012-12-18 16:14:02 +05302098static irqreturn_t sdhci_msm_pwr_irq(int irq, void *data)
2099{
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002100 struct sdhci_host *host = (struct sdhci_host *)data;
2101 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2102 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Das0ef24812012-12-18 16:14:02 +05302103 u8 irq_status = 0;
2104 u8 irq_ack = 0;
2105 int ret = 0;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302106 int pwr_state = 0, io_level = 0;
2107 unsigned long flags;
Pavan Anamula53ffa0b2015-07-22 21:46:32 +05302108 int retry = 10;
Asutosh Das0ef24812012-12-18 16:14:02 +05302109
2110 irq_status = readb_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
2111 pr_debug("%s: Received IRQ(%d), status=0x%x\n",
2112 mmc_hostname(msm_host->mmc), irq, irq_status);
2113
2114 /* Clear the interrupt */
2115 writeb_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
2116 /*
2117 * SDHC has core_mem and hc_mem device memory and these memory
2118 * addresses do not fall within 1KB region. Hence, any update to
2119 * core_mem address space would require an mb() to ensure this gets
2120 * completed before its next update to registers within hc_mem.
2121 */
2122 mb();
Pavan Anamula53ffa0b2015-07-22 21:46:32 +05302123 /*
2124 * There is a rare HW scenario where the first clear pulse could be
2125 * lost when actual reset and clear/read of status register is
2126 * happening at a time. Hence, retry for at least 10 times to make
2127 * sure status register is cleared. Otherwise, this will result in
2128 * a spurious power IRQ resulting in system instability.
2129 */
2130 while (irq_status &
2131 readb_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS)) {
2132 if (retry == 0) {
2133 pr_err("%s: Timedout clearing (0x%x) pwrctl status register\n",
2134 mmc_hostname(host->mmc), irq_status);
2135 sdhci_msm_dump_pwr_ctrl_regs(host);
2136 BUG_ON(1);
2137 }
2138 writeb_relaxed(irq_status,
2139 (msm_host->core_mem + CORE_PWRCTL_CLEAR));
2140 retry--;
2141 udelay(10);
2142 }
2143 if (likely(retry < 10))
2144 pr_debug("%s: success clearing (0x%x) pwrctl status register, retries left %d\n",
2145 mmc_hostname(host->mmc), irq_status, retry);
Asutosh Das0ef24812012-12-18 16:14:02 +05302146
2147 /* Handle BUS ON/OFF*/
2148 if (irq_status & CORE_PWRCTL_BUS_ON) {
2149 ret = sdhci_msm_setup_vreg(msm_host->pdata, true, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302150 if (!ret) {
Asutosh Das0ef24812012-12-18 16:14:02 +05302151 ret = sdhci_msm_setup_pins(msm_host->pdata, true);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302152 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
2153 VDD_IO_HIGH, 0);
2154 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302155 if (ret)
2156 irq_ack |= CORE_PWRCTL_BUS_FAIL;
2157 else
2158 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302159
2160 pwr_state = REQ_BUS_ON;
2161 io_level = REQ_IO_HIGH;
Asutosh Das0ef24812012-12-18 16:14:02 +05302162 }
2163 if (irq_status & CORE_PWRCTL_BUS_OFF) {
2164 ret = sdhci_msm_setup_vreg(msm_host->pdata, false, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302165 if (!ret) {
Asutosh Das0ef24812012-12-18 16:14:02 +05302166 ret = sdhci_msm_setup_pins(msm_host->pdata, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302167 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
2168 VDD_IO_LOW, 0);
2169 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302170 if (ret)
2171 irq_ack |= CORE_PWRCTL_BUS_FAIL;
2172 else
2173 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302174
2175 pwr_state = REQ_BUS_OFF;
2176 io_level = REQ_IO_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05302177 }
2178 /* Handle IO LOW/HIGH */
2179 if (irq_status & CORE_PWRCTL_IO_LOW) {
2180 /* Switch voltage Low */
2181 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_LOW, 0);
2182 if (ret)
2183 irq_ack |= CORE_PWRCTL_IO_FAIL;
2184 else
2185 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302186
2187 io_level = REQ_IO_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05302188 }
2189 if (irq_status & CORE_PWRCTL_IO_HIGH) {
2190 /* Switch voltage High */
2191 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_HIGH, 0);
2192 if (ret)
2193 irq_ack |= CORE_PWRCTL_IO_FAIL;
2194 else
2195 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302196
2197 io_level = REQ_IO_HIGH;
Asutosh Das0ef24812012-12-18 16:14:02 +05302198 }
2199
2200 /* ACK status to the core */
2201 writeb_relaxed(irq_ack, (msm_host->core_mem + CORE_PWRCTL_CTL));
2202 /*
2203 * SDHC has core_mem and hc_mem device memory and these memory
2204 * addresses do not fall within 1KB region. Hence, any update to
2205 * core_mem address space would require an mb() to ensure this gets
2206 * completed before its next update to registers within hc_mem.
2207 */
2208 mb();
2209
Krishna Konda46fd1432014-10-30 21:13:27 -07002210 if ((io_level & REQ_IO_HIGH) && (msm_host->caps_0 & CORE_3_0V_SUPPORT))
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002211 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2212 ~CORE_IO_PAD_PWR_SWITCH),
2213 host->ioaddr + CORE_VENDOR_SPEC);
Krishna Konda46fd1432014-10-30 21:13:27 -07002214 else if ((io_level & REQ_IO_LOW) ||
2215 (msm_host->caps_0 & CORE_1_8V_SUPPORT))
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002216 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
2217 CORE_IO_PAD_PWR_SWITCH),
2218 host->ioaddr + CORE_VENDOR_SPEC);
2219 mb();
2220
Asutosh Das0ef24812012-12-18 16:14:02 +05302221 pr_debug("%s: Handled IRQ(%d), ret=%d, ack=0x%x\n",
2222 mmc_hostname(msm_host->mmc), irq, ret, irq_ack);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302223 spin_lock_irqsave(&host->lock, flags);
2224 if (pwr_state)
2225 msm_host->curr_pwr_state = pwr_state;
2226 if (io_level)
2227 msm_host->curr_io_level = io_level;
2228 complete(&msm_host->pwr_irq_completion);
2229 spin_unlock_irqrestore(&host->lock, flags);
2230
Asutosh Das0ef24812012-12-18 16:14:02 +05302231 return IRQ_HANDLED;
2232}
2233
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302234static ssize_t
Sahitya Tummala5c55b932013-06-20 14:00:18 +05302235show_polling(struct device *dev, struct device_attribute *attr, char *buf)
2236{
2237 struct sdhci_host *host = dev_get_drvdata(dev);
2238 int poll;
2239 unsigned long flags;
2240
2241 spin_lock_irqsave(&host->lock, flags);
2242 poll = !!(host->mmc->caps & MMC_CAP_NEEDS_POLL);
2243 spin_unlock_irqrestore(&host->lock, flags);
2244
2245 return snprintf(buf, PAGE_SIZE, "%d\n", poll);
2246}
2247
2248static ssize_t
2249store_polling(struct device *dev, struct device_attribute *attr,
2250 const char *buf, size_t count)
2251{
2252 struct sdhci_host *host = dev_get_drvdata(dev);
2253 int value;
2254 unsigned long flags;
2255
2256 if (!kstrtou32(buf, 0, &value)) {
2257 spin_lock_irqsave(&host->lock, flags);
2258 if (value) {
2259 host->mmc->caps |= MMC_CAP_NEEDS_POLL;
2260 mmc_detect_change(host->mmc, 0);
2261 } else {
2262 host->mmc->caps &= ~MMC_CAP_NEEDS_POLL;
2263 }
2264 spin_unlock_irqrestore(&host->lock, flags);
2265 }
2266 return count;
2267}
2268
2269static ssize_t
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302270show_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
2271 char *buf)
2272{
2273 struct sdhci_host *host = dev_get_drvdata(dev);
2274 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2275 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2276
2277 return snprintf(buf, PAGE_SIZE, "%u\n",
2278 msm_host->msm_bus_vote.is_max_bw_needed);
2279}
2280
2281static ssize_t
2282store_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
2283 const char *buf, size_t count)
2284{
2285 struct sdhci_host *host = dev_get_drvdata(dev);
2286 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2287 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2288 uint32_t value;
2289 unsigned long flags;
2290
2291 if (!kstrtou32(buf, 0, &value)) {
2292 spin_lock_irqsave(&host->lock, flags);
2293 msm_host->msm_bus_vote.is_max_bw_needed = !!value;
2294 spin_unlock_irqrestore(&host->lock, flags);
2295 }
2296 return count;
2297}
2298
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302299static void sdhci_msm_check_power_status(struct sdhci_host *host, u32 req_type)
Asutosh Das0ef24812012-12-18 16:14:02 +05302300{
2301 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2302 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302303 unsigned long flags;
2304 bool done = false;
Sahitya Tummala481fbb02013-08-06 15:22:28 +05302305 u32 io_sig_sts;
Asutosh Das0ef24812012-12-18 16:14:02 +05302306
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302307 spin_lock_irqsave(&host->lock, flags);
2308 pr_debug("%s: %s: request %d curr_pwr_state %x curr_io_level %x\n",
2309 mmc_hostname(host->mmc), __func__, req_type,
2310 msm_host->curr_pwr_state, msm_host->curr_io_level);
Sahitya Tummala481fbb02013-08-06 15:22:28 +05302311 io_sig_sts = readl_relaxed(msm_host->core_mem + CORE_GENERICS);
2312 /*
2313 * The IRQ for request type IO High/Low will be generated when -
2314 * 1. SWITCHABLE_SIGNALLING_VOL is enabled in HW.
2315 * 2. If 1 is true and when there is a state change in 1.8V enable
2316 * bit (bit 3) of SDHCI_HOST_CONTROL2 register. The reset state of
2317 * that bit is 0 which indicates 3.3V IO voltage. So, when MMC core
2318 * layer tries to set it to 3.3V before card detection happens, the
2319 * IRQ doesn't get triggered as there is no state change in this bit.
2320 * The driver already handles this case by changing the IO voltage
2321 * level to high as part of controller power up sequence. Hence, check
2322 * for host->pwr to handle a case where IO voltage high request is
2323 * issued even before controller power up.
2324 */
2325 if (req_type & (REQ_IO_HIGH | REQ_IO_LOW)) {
2326 if (!(io_sig_sts & SWITCHABLE_SIGNALLING_VOL) ||
2327 ((req_type & REQ_IO_HIGH) && !host->pwr)) {
2328 pr_debug("%s: do not wait for power IRQ that never comes\n",
2329 mmc_hostname(host->mmc));
2330 spin_unlock_irqrestore(&host->lock, flags);
2331 return;
2332 }
2333 }
2334
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302335 if ((req_type & msm_host->curr_pwr_state) ||
2336 (req_type & msm_host->curr_io_level))
2337 done = true;
2338 spin_unlock_irqrestore(&host->lock, flags);
Asutosh Das0ef24812012-12-18 16:14:02 +05302339
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302340 /*
2341 * This is needed here to hanlde a case where IRQ gets
2342 * triggered even before this function is called so that
2343 * x->done counter of completion gets reset. Otherwise,
2344 * next call to wait_for_completion returns immediately
2345 * without actually waiting for the IRQ to be handled.
2346 */
2347 if (done)
2348 init_completion(&msm_host->pwr_irq_completion);
2349 else
2350 wait_for_completion(&msm_host->pwr_irq_completion);
2351
2352 pr_debug("%s: %s: request %d done\n", mmc_hostname(host->mmc),
2353 __func__, req_type);
Asutosh Das0ef24812012-12-18 16:14:02 +05302354}
2355
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002356static void sdhci_msm_toggle_cdr(struct sdhci_host *host, bool enable)
2357{
Ritesh Harjani8e36f662014-11-14 11:09:56 +05302358 u32 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
2359
2360 if (enable) {
2361 config |= CORE_CDR_EN;
2362 config &= ~CORE_CDR_EXT_EN;
2363 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
2364 } else {
2365 config &= ~CORE_CDR_EN;
2366 config |= CORE_CDR_EXT_EN;
2367 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
2368 }
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002369}
2370
Asutosh Das648f9d12013-01-10 21:11:04 +05302371static unsigned int sdhci_msm_max_segs(void)
2372{
2373 return SDHCI_MSM_MAX_SEGMENTS;
2374}
2375
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302376static unsigned int sdhci_msm_get_min_clock(struct sdhci_host *host)
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302377{
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302378 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2379 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302380
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302381 return msm_host->pdata->sup_clk_table[0];
2382}
2383
2384static unsigned int sdhci_msm_get_max_clock(struct sdhci_host *host)
2385{
2386 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2387 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2388 int max_clk_index = msm_host->pdata->sup_clk_cnt;
2389
2390 return msm_host->pdata->sup_clk_table[max_clk_index - 1];
2391}
2392
2393static unsigned int sdhci_msm_get_sup_clk_rate(struct sdhci_host *host,
2394 u32 req_clk)
2395{
2396 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2397 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2398 unsigned int sel_clk = -1;
2399 unsigned char cnt;
2400
2401 if (req_clk < sdhci_msm_get_min_clock(host)) {
2402 sel_clk = sdhci_msm_get_min_clock(host);
2403 return sel_clk;
2404 }
2405
2406 for (cnt = 0; cnt < msm_host->pdata->sup_clk_cnt; cnt++) {
2407 if (msm_host->pdata->sup_clk_table[cnt] > req_clk) {
2408 break;
2409 } else if (msm_host->pdata->sup_clk_table[cnt] == req_clk) {
2410 sel_clk = msm_host->pdata->sup_clk_table[cnt];
2411 break;
2412 } else {
2413 sel_clk = msm_host->pdata->sup_clk_table[cnt];
2414 }
2415 }
2416 return sel_clk;
2417}
2418
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302419static int sdhci_msm_enable_controller_clock(struct sdhci_host *host)
2420{
2421 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2422 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2423 int rc = 0;
2424
2425 if (atomic_read(&msm_host->controller_clock))
2426 return 0;
2427
2428 sdhci_msm_bus_voting(host, 1);
2429
2430 if (!IS_ERR(msm_host->pclk)) {
2431 rc = clk_prepare_enable(msm_host->pclk);
2432 if (rc) {
2433 pr_err("%s: %s: failed to enable the pclk with error %d\n",
2434 mmc_hostname(host->mmc), __func__, rc);
2435 goto remove_vote;
2436 }
2437 }
2438
2439 rc = clk_prepare_enable(msm_host->clk);
2440 if (rc) {
2441 pr_err("%s: %s: failed to enable the host-clk with error %d\n",
2442 mmc_hostname(host->mmc), __func__, rc);
2443 goto disable_pclk;
2444 }
2445
2446 atomic_set(&msm_host->controller_clock, 1);
2447 pr_debug("%s: %s: enabled controller clock\n",
2448 mmc_hostname(host->mmc), __func__);
2449 goto out;
2450
2451disable_pclk:
2452 if (!IS_ERR(msm_host->pclk))
2453 clk_disable_unprepare(msm_host->pclk);
2454remove_vote:
2455 if (msm_host->msm_bus_vote.client_handle)
2456 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2457out:
2458 return rc;
2459}
2460
2461
2462
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302463static int sdhci_msm_prepare_clocks(struct sdhci_host *host, bool enable)
2464{
2465 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2466 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2467 int rc = 0;
2468
2469 if (enable && !atomic_read(&msm_host->clks_on)) {
2470 pr_debug("%s: request to enable clocks\n",
2471 mmc_hostname(host->mmc));
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302472
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302473 /*
2474 * The bus-width or the clock rate might have changed
2475 * after controller clocks are enbaled, update bus vote
2476 * in such case.
2477 */
2478 if (atomic_read(&msm_host->controller_clock))
2479 sdhci_msm_bus_voting(host, 1);
2480
2481 rc = sdhci_msm_enable_controller_clock(host);
2482 if (rc)
2483 goto remove_vote;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302484
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302485 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
2486 rc = clk_prepare_enable(msm_host->bus_clk);
2487 if (rc) {
2488 pr_err("%s: %s: failed to enable the bus-clock with error %d\n",
2489 mmc_hostname(host->mmc), __func__, rc);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302490 goto disable_controller_clk;
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302491 }
2492 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002493 if (!IS_ERR(msm_host->ff_clk)) {
2494 rc = clk_prepare_enable(msm_host->ff_clk);
2495 if (rc) {
2496 pr_err("%s: %s: failed to enable the ff_clk with error %d\n",
2497 mmc_hostname(host->mmc), __func__, rc);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302498 goto disable_bus_clk;
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002499 }
2500 }
2501 if (!IS_ERR(msm_host->sleep_clk)) {
2502 rc = clk_prepare_enable(msm_host->sleep_clk);
2503 if (rc) {
2504 pr_err("%s: %s: failed to enable the sleep_clk with error %d\n",
2505 mmc_hostname(host->mmc), __func__, rc);
2506 goto disable_ff_clk;
2507 }
2508 }
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302509 mb();
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302510
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302511 } else if (!enable && atomic_read(&msm_host->clks_on)) {
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302512 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
2513 mb();
Sahitya Tummaladc182982013-08-20 15:32:09 +05302514 /*
2515 * During 1.8V signal switching the clock source must
2516 * still be ON as it requires accessing SDHC
2517 * registers (SDHCi host control2 register bit 3 must
2518 * be written and polled after stopping the SDCLK).
2519 */
2520 if (host->mmc->card_clock_off)
2521 return 0;
2522 pr_debug("%s: request to disable clocks\n",
2523 mmc_hostname(host->mmc));
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002524 if (!IS_ERR_OR_NULL(msm_host->sleep_clk))
2525 clk_disable_unprepare(msm_host->sleep_clk);
2526 if (!IS_ERR_OR_NULL(msm_host->ff_clk))
2527 clk_disable_unprepare(msm_host->ff_clk);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302528 clk_disable_unprepare(msm_host->clk);
2529 if (!IS_ERR(msm_host->pclk))
2530 clk_disable_unprepare(msm_host->pclk);
2531 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2532 clk_disable_unprepare(msm_host->bus_clk);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302533
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302534 atomic_set(&msm_host->controller_clock, 0);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302535 sdhci_msm_bus_voting(host, 0);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302536 }
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302537 atomic_set(&msm_host->clks_on, enable);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302538 goto out;
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002539disable_ff_clk:
2540 if (!IS_ERR_OR_NULL(msm_host->ff_clk))
2541 clk_disable_unprepare(msm_host->ff_clk);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302542disable_bus_clk:
2543 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2544 clk_disable_unprepare(msm_host->bus_clk);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302545disable_controller_clk:
2546 if (!IS_ERR_OR_NULL(msm_host->clk))
2547 clk_disable_unprepare(msm_host->clk);
2548 if (!IS_ERR_OR_NULL(msm_host->pclk))
2549 clk_disable_unprepare(msm_host->pclk);
2550 atomic_set(&msm_host->controller_clock, 0);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302551remove_vote:
2552 if (msm_host->msm_bus_vote.client_handle)
2553 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302554out:
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302555 return rc;
2556}
2557
2558static void sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock)
2559{
2560 int rc;
2561 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2562 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2563 struct mmc_ios curr_ios = host->mmc->ios;
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002564 u32 sup_clock, ddr_clock, dll_lock;
Sahitya Tummala043744a2013-06-24 09:55:33 +05302565 bool curr_pwrsave;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302566
2567 if (!clock) {
Sujit Reddy Thummabf1aecc2014-01-10 10:58:54 +05302568 /*
2569 * disable pwrsave to ensure clock is not auto-gated until
2570 * the rate is >400KHz (initialization complete).
2571 */
2572 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2573 ~CORE_CLK_PWRSAVE, host->ioaddr + CORE_VENDOR_SPEC);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302574 sdhci_msm_prepare_clocks(host, false);
2575 host->clock = clock;
2576 goto out;
2577 }
2578
2579 rc = sdhci_msm_prepare_clocks(host, true);
2580 if (rc)
2581 goto out;
2582
Sahitya Tummala043744a2013-06-24 09:55:33 +05302583 curr_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2584 CORE_CLK_PWRSAVE);
Sahitya Tummalae000b242013-08-29 16:21:08 +05302585 if ((clock > 400000) &&
Venkat Gopalakrishnanc0a367272015-02-24 13:09:09 -08002586 !curr_pwrsave && mmc_host_may_gate_card(host->mmc->card))
Sahitya Tummala043744a2013-06-24 09:55:33 +05302587 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2588 | CORE_CLK_PWRSAVE,
2589 host->ioaddr + CORE_VENDOR_SPEC);
2590 /*
2591 * Disable pwrsave for a newly added card if doesn't allow clock
2592 * gating.
2593 */
Venkat Gopalakrishnanc0a367272015-02-24 13:09:09 -08002594 else if (curr_pwrsave && !mmc_host_may_gate_card(host->mmc->card))
Sahitya Tummala043744a2013-06-24 09:55:33 +05302595 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2596 & ~CORE_CLK_PWRSAVE,
2597 host->ioaddr + CORE_VENDOR_SPEC);
2598
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302599 sup_clock = sdhci_msm_get_sup_clk_rate(host, clock);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002600 if ((curr_ios.timing == MMC_TIMING_UHS_DDR50) ||
Venkat Gopalakrishnan0a29da92015-01-09 12:19:16 -08002601 (curr_ios.timing == MMC_TIMING_MMC_DDR52) ||
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002602 (curr_ios.timing == MMC_TIMING_MMC_HS400)) {
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302603 /*
2604 * The SDHC requires internal clock frequency to be double the
2605 * actual clock that will be set for DDR mode. The controller
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002606 * uses the faster clock(100/400MHz) for some of its parts and
2607 * send the actual required clock (50/200MHz) to the card.
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302608 */
2609 ddr_clock = clock * 2;
2610 sup_clock = sdhci_msm_get_sup_clk_rate(host,
2611 ddr_clock);
2612 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002613
2614 /*
2615 * In general all timing modes are controlled via UHS mode select in
2616 * Host Control2 register. eMMC specific HS200/HS400 doesn't have
2617 * their respective modes defined here, hence we use these values.
2618 *
2619 * HS200 - SDR104 (Since they both are equivalent in functionality)
2620 * HS400 - This involves multiple configurations
2621 * Initially SDR104 - when tuning is required as HS200
2622 * Then when switching to DDR @ 400MHz (HS400) we use
2623 * the vendor specific HC_SELECT_IN to control the mode.
2624 *
2625 * In addition to controlling the modes we also need to select the
2626 * correct input clock for DLL depending on the mode.
2627 *
2628 * HS400 - divided clock (free running MCLK/2)
2629 * All other modes - default (free running MCLK)
2630 */
2631 if (curr_ios.timing == MMC_TIMING_MMC_HS400) {
2632 /* Select the divided clock (free running MCLK/2) */
2633 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2634 & ~CORE_HC_MCLK_SEL_MASK)
2635 | CORE_HC_MCLK_SEL_HS400),
2636 host->ioaddr + CORE_VENDOR_SPEC);
2637 /*
2638 * Select HS400 mode using the HC_SELECT_IN from VENDOR SPEC
2639 * register
2640 */
Ritesh Harjaniea709662015-05-27 15:40:24 +05302641 if ((msm_host->tuning_done || msm_host->enhanced_strobe) &&
2642 !msm_host->calibration_done) {
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002643 /*
2644 * Write 0x6 to HC_SELECT_IN and 1 to HC_SELECT_IN_EN
2645 * field in VENDOR_SPEC_FUNC
2646 */
2647 writel_relaxed((readl_relaxed(host->ioaddr + \
2648 CORE_VENDOR_SPEC)
2649 | CORE_HC_SELECT_IN_HS400
2650 | CORE_HC_SELECT_IN_EN),
2651 host->ioaddr + CORE_VENDOR_SPEC);
2652 }
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002653 if (!host->mmc->ios.old_rate && !msm_host->use_cdclp533) {
2654 /*
2655 * Poll on DLL_LOCK and DDR_DLL_LOCK bits in
2656 * CORE_DLL_STATUS to be set. This should get set
2657 * with in 15 us at 200 MHz.
2658 */
2659 rc = readl_poll_timeout(host->ioaddr + CORE_DLL_STATUS,
2660 dll_lock, (dll_lock & (CORE_DLL_LOCK |
2661 CORE_DDR_DLL_LOCK)), 10, 1000);
2662 if (rc == -ETIMEDOUT)
2663 pr_err("%s: Unable to get DLL_LOCK/DDR_DLL_LOCK, dll_status: 0x%08x\n",
2664 mmc_hostname(host->mmc),
2665 dll_lock);
2666 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002667 } else {
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002668 if (!msm_host->use_cdclp533)
2669 /* set CORE_PWRSAVE_DLL bit in CORE_VENDOR_SPEC3 */
2670 writel_relaxed((readl_relaxed(host->ioaddr +
2671 CORE_VENDOR_SPEC3) & ~CORE_PWRSAVE_DLL),
2672 host->ioaddr + CORE_VENDOR_SPEC3);
2673
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002674 /* Select the default clock (free running MCLK) */
2675 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2676 & ~CORE_HC_MCLK_SEL_MASK)
2677 | CORE_HC_MCLK_SEL_DFLT),
2678 host->ioaddr + CORE_VENDOR_SPEC);
2679
2680 /*
2681 * Disable HC_SELECT_IN to be able to use the UHS mode select
2682 * configuration from Host Control2 register for all other
2683 * modes.
2684 *
2685 * Write 0 to HC_SELECT_IN and HC_SELECT_IN_EN field
2686 * in VENDOR_SPEC_FUNC
2687 */
2688 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2689 & ~CORE_HC_SELECT_IN_EN
2690 & ~CORE_HC_SELECT_IN_MASK),
2691 host->ioaddr + CORE_VENDOR_SPEC);
2692 }
2693 mb();
2694
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302695 if (sup_clock != msm_host->clk_rate) {
2696 pr_debug("%s: %s: setting clk rate to %u\n",
2697 mmc_hostname(host->mmc), __func__, sup_clock);
2698 rc = clk_set_rate(msm_host->clk, sup_clock);
2699 if (rc) {
2700 pr_err("%s: %s: Failed to set rate %u for host-clk : %d\n",
2701 mmc_hostname(host->mmc), __func__,
2702 sup_clock, rc);
2703 goto out;
2704 }
2705 msm_host->clk_rate = sup_clock;
2706 host->clock = clock;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302707 /*
2708 * Update the bus vote in case of frequency change due to
2709 * clock scaling.
2710 */
2711 sdhci_msm_bus_voting(host, 1);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302712 }
2713out:
2714 sdhci_set_clock(host, clock);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302715}
2716
Sahitya Tummala14613432013-03-21 11:13:25 +05302717static void sdhci_msm_set_uhs_signaling(struct sdhci_host *host,
2718 unsigned int uhs)
2719{
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002720 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2721 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala14613432013-03-21 11:13:25 +05302722 u16 ctrl_2;
2723
2724 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2725 /* Select Bus Speed Mode for host */
2726 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Venkat Gopalakrishnan0a29da92015-01-09 12:19:16 -08002727 if ((uhs == MMC_TIMING_MMC_HS400) ||
2728 (uhs == MMC_TIMING_MMC_HS200) ||
2729 (uhs == MMC_TIMING_UHS_SDR104))
Sahitya Tummala14613432013-03-21 11:13:25 +05302730 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2731 else if (uhs == MMC_TIMING_UHS_SDR12)
2732 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
2733 else if (uhs == MMC_TIMING_UHS_SDR25)
2734 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
2735 else if (uhs == MMC_TIMING_UHS_SDR50)
2736 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
Venkat Gopalakrishnan0a29da92015-01-09 12:19:16 -08002737 else if ((uhs == MMC_TIMING_UHS_DDR50) ||
2738 (uhs == MMC_TIMING_MMC_DDR52))
Sahitya Tummala14613432013-03-21 11:13:25 +05302739 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302740 /*
2741 * When clock frquency is less than 100MHz, the feedback clock must be
2742 * provided and DLL must not be used so that tuning can be skipped. To
2743 * provide feedback clock, the mode selection can be any value less
2744 * than 3'b011 in bits [2:0] of HOST CONTROL2 register.
2745 */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002746 if (host->clock <= CORE_FREQ_100MHZ) {
2747 if ((uhs == MMC_TIMING_MMC_HS400) ||
2748 (uhs == MMC_TIMING_MMC_HS200) ||
2749 (uhs == MMC_TIMING_UHS_SDR104))
2750 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302751
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002752 /*
2753 * Make sure DLL is disabled when not required
2754 *
2755 * Write 1 to DLL_RST bit of DLL_CONFIG register
2756 */
2757 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
2758 | CORE_DLL_RST),
2759 host->ioaddr + CORE_DLL_CONFIG);
2760
2761 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
2762 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
2763 | CORE_DLL_PDN),
2764 host->ioaddr + CORE_DLL_CONFIG);
2765 mb();
2766
2767 /*
2768 * The DLL needs to be restored and CDCLP533 recalibrated
2769 * when the clock frequency is set back to 400MHz.
2770 */
2771 msm_host->calibration_done = false;
2772 }
2773
2774 pr_debug("%s: %s-clock:%u uhs mode:%u ctrl_2:0x%x\n",
2775 mmc_hostname(host->mmc), __func__, host->clock, uhs, ctrl_2);
Sahitya Tummala14613432013-03-21 11:13:25 +05302776 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
2777
2778}
2779
Venkat Gopalakrishnan34811972015-03-04 14:39:01 -08002780#define MAX_TEST_BUS 60
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302781
2782void sdhci_msm_dump_vendor_regs(struct sdhci_host *host)
2783{
2784 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2785 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2786 int tbsel, tbsel2;
2787 int i, index = 0;
2788 u32 test_bus_val = 0;
2789 u32 debug_reg[MAX_TEST_BUS] = {0};
2790
2791 pr_info("----------- VENDOR REGISTER DUMP -----------\n");
2792 pr_info("Data cnt: 0x%08x | Fifo cnt: 0x%08x | Int sts: 0x%08x\n",
2793 readl_relaxed(msm_host->core_mem + CORE_MCI_DATA_CNT),
2794 readl_relaxed(msm_host->core_mem + CORE_MCI_FIFO_CNT),
2795 readl_relaxed(msm_host->core_mem + CORE_MCI_STATUS));
2796 pr_info("DLL cfg: 0x%08x | DLL sts: 0x%08x | SDCC ver: 0x%08x\n",
2797 readl_relaxed(host->ioaddr + CORE_DLL_CONFIG),
2798 readl_relaxed(host->ioaddr + CORE_DLL_STATUS),
2799 readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION));
2800 pr_info("Vndr func: 0x%08x | Vndr adma err : addr0: 0x%08x addr1: 0x%08x\n",
2801 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC),
2802 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_ADMA_ERR_ADDR0),
2803 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_ADMA_ERR_ADDR1));
Sahitya Tummala2ba05ae2015-03-03 14:03:20 +05302804 pr_info("Vndr func2: 0x%08x\n",
2805 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_FUNC2));
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302806
2807 /*
2808 * tbsel indicates [2:0] bits and tbsel2 indicates [7:4] bits
2809 * of CORE_TESTBUS_CONFIG register.
2810 *
2811 * To select test bus 0 to 7 use tbsel and to select any test bus
2812 * above 7 use (tbsel2 | tbsel) to get the test bus number. For eg,
2813 * to select test bus 14, write 0x1E to CORE_TESTBUS_CONFIG register
2814 * i.e., tbsel2[7:4] = 0001, tbsel[2:0] = 110.
2815 */
Venkat Gopalakrishnan34811972015-03-04 14:39:01 -08002816 for (tbsel2 = 0; tbsel2 < 7; tbsel2++) {
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302817 for (tbsel = 0; tbsel < 8; tbsel++) {
2818 if (index >= MAX_TEST_BUS)
2819 break;
2820 test_bus_val = (tbsel2 << CORE_TESTBUS_SEL2_BIT) |
2821 tbsel | CORE_TESTBUS_ENA;
2822 writel_relaxed(test_bus_val,
2823 msm_host->core_mem + CORE_TESTBUS_CONFIG);
2824 debug_reg[index++] = readl_relaxed(msm_host->core_mem +
2825 CORE_SDCC_DEBUG_REG);
2826 }
2827 }
2828 for (i = 0; i < MAX_TEST_BUS; i = i + 4)
2829 pr_info(" Test bus[%d to %d]: 0x%08x 0x%08x 0x%08x 0x%08x\n",
2830 i, i + 3, debug_reg[i], debug_reg[i+1],
2831 debug_reg[i+2], debug_reg[i+3]);
Venkat Gopalakrishnan06f7f792015-05-29 17:56:59 -07002832}
2833
Ritesh Harjani4a5ffd12015-07-15 13:32:07 +05302834/*
2835 * sdhci_msm_enhanced_strobe_mask :-
2836 * Before running CMDQ transfers in HS400 Enhanced Strobe mode,
2837 * SW should write 3 to
2838 * HC_VENDOR_SPECIFIC_FUNC3.CMDEN_HS400_INPUT_MASK_CNT register.
2839 * The default reset value of this register is 2.
2840 */
2841static void sdhci_msm_enhanced_strobe_mask(struct sdhci_host *host, bool set)
2842{
2843 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2844 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2845
2846 if (!msm_host->enhanced_strobe) {
2847 pr_debug("%s: host does not support hs400 enhanced strobe\n",
2848 mmc_hostname(host->mmc));
2849 return;
2850 }
2851
2852 if (set) {
2853 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC3)
2854 | CORE_CMDEN_HS400_INPUT_MASK_CNT),
2855 host->ioaddr + CORE_VENDOR_SPEC3);
2856 } else {
2857 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC3)
2858 & ~CORE_CMDEN_HS400_INPUT_MASK_CNT),
2859 host->ioaddr + CORE_VENDOR_SPEC3);
2860 }
2861}
2862
Venkat Gopalakrishnan06f7f792015-05-29 17:56:59 -07002863static void sdhci_msm_clear_set_dumpregs(struct sdhci_host *host, bool set)
2864{
2865 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2866 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2867
2868 if (set) {
2869 writel_relaxed(CORE_TESTBUS_ENA,
2870 msm_host->core_mem + CORE_TESTBUS_CONFIG);
2871 } else {
2872 u32 value;
2873
2874 value = readl_relaxed(msm_host->core_mem + CORE_TESTBUS_CONFIG);
2875 value &= ~CORE_TESTBUS_ENA;
2876 writel_relaxed(value, msm_host->core_mem + CORE_TESTBUS_CONFIG);
2877 }
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302878}
2879
Dov Levenglick9c575e22015-07-20 09:30:52 +03002880static void sdhci_msm_detect(struct sdhci_host *host, bool detected)
2881{
2882 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2883 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2884 struct mmc_host *mmc = msm_host->mmc;
2885 struct mmc_card *card = mmc->card;
2886
2887 if (detected && mmc_card_sdio(card))
2888 mmc->pm_caps |= MMC_PM_KEEP_POWER;
2889 else
2890 mmc->pm_caps &= ~MMC_PM_KEEP_POWER;
2891}
2892
Pavan Anamula691dd592015-08-25 16:11:20 +05302893void sdhci_msm_reset_workaround(struct sdhci_host *host, u32 enable)
2894{
2895 u32 vendor_func2;
2896 unsigned long timeout;
2897
2898 vendor_func2 = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_FUNC2);
2899
2900 if (enable) {
2901 writel_relaxed(vendor_func2 | HC_SW_RST_REQ, host->ioaddr +
2902 CORE_VENDOR_SPEC_FUNC2);
2903 timeout = 10000;
2904 while (readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_FUNC2) &
2905 HC_SW_RST_REQ) {
2906 if (timeout == 0) {
2907 pr_info("%s: Applying wait idle disable workaround\n",
2908 mmc_hostname(host->mmc));
2909 /*
2910 * Apply the reset workaround to not wait for
2911 * pending data transfers on AXI before
2912 * resetting the controller. This could be
2913 * risky if the transfers were stuck on the
2914 * AXI bus.
2915 */
2916 vendor_func2 = readl_relaxed(host->ioaddr +
2917 CORE_VENDOR_SPEC_FUNC2);
2918 writel_relaxed(vendor_func2 |
2919 HC_SW_RST_WAIT_IDLE_DIS,
2920 host->ioaddr + CORE_VENDOR_SPEC_FUNC2);
2921 host->reset_wa_t = ktime_get();
2922 return;
2923 }
2924 timeout--;
2925 udelay(10);
2926 }
2927 pr_info("%s: waiting for SW_RST_REQ is successful\n",
2928 mmc_hostname(host->mmc));
2929 } else {
2930 writel_relaxed(vendor_func2 & ~HC_SW_RST_WAIT_IDLE_DIS,
2931 host->ioaddr + CORE_VENDOR_SPEC_FUNC2);
2932 }
2933}
2934
Asutosh Das0ef24812012-12-18 16:14:02 +05302935static struct sdhci_ops sdhci_msm_ops = {
Sahitya Tummala14613432013-03-21 11:13:25 +05302936 .set_uhs_signaling = sdhci_msm_set_uhs_signaling,
Asutosh Das0ef24812012-12-18 16:14:02 +05302937 .check_power_status = sdhci_msm_check_power_status,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002938 .platform_execute_tuning = sdhci_msm_execute_tuning,
Ritesh Harjaniea709662015-05-27 15:40:24 +05302939 .enhanced_strobe = sdhci_msm_enhanced_strobe,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002940 .toggle_cdr = sdhci_msm_toggle_cdr,
Asutosh Das648f9d12013-01-10 21:11:04 +05302941 .get_max_segments = sdhci_msm_max_segs,
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302942 .set_clock = sdhci_msm_set_clock,
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302943 .get_min_clock = sdhci_msm_get_min_clock,
2944 .get_max_clock = sdhci_msm_get_max_clock,
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302945 .dump_vendor_regs = sdhci_msm_dump_vendor_regs,
Asutosh Dase5e9ca62013-07-30 19:08:36 +05302946 .config_auto_tuning_cmd = sdhci_msm_config_auto_tuning_cmd,
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302947 .enable_controller_clock = sdhci_msm_enable_controller_clock,
Venkat Gopalakrishnanb8cb7072015-01-09 11:04:34 -08002948 .set_bus_width = sdhci_set_bus_width,
Venkat Gopalakrishnan411df072015-01-09 11:09:44 -08002949 .reset = sdhci_reset,
Venkat Gopalakrishnan06f7f792015-05-29 17:56:59 -07002950 .clear_set_dumpregs = sdhci_msm_clear_set_dumpregs,
Ritesh Harjani4a5ffd12015-07-15 13:32:07 +05302951 .enhanced_strobe_mask = sdhci_msm_enhanced_strobe_mask,
Dov Levenglick9c575e22015-07-20 09:30:52 +03002952 .detect = sdhci_msm_detect,
Pavan Anamula691dd592015-08-25 16:11:20 +05302953 .reset_workaround = sdhci_msm_reset_workaround,
Asutosh Das0ef24812012-12-18 16:14:02 +05302954};
2955
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302956static void sdhci_set_default_hw_caps(struct sdhci_msm_host *msm_host,
2957 struct sdhci_host *host)
2958{
Krishna Konda46fd1432014-10-30 21:13:27 -07002959 u32 version, caps = 0;
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302960 u16 minor;
2961 u8 major;
Sahitya Tummala2ba05ae2015-03-03 14:03:20 +05302962 u32 val;
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302963
2964 version = readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION);
2965 major = (version & CORE_VERSION_MAJOR_MASK) >>
2966 CORE_VERSION_MAJOR_SHIFT;
2967 minor = version & CORE_VERSION_TARGET_MASK;
2968
Krishna Konda46fd1432014-10-30 21:13:27 -07002969 caps = readl_relaxed(host->ioaddr + SDHCI_CAPABILITIES);
2970
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302971 /*
2972 * Starting with SDCC 5 controller (core major version = 1)
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002973 * controller won't advertise 3.0v, 1.8v and 8-bit features
2974 * except for some targets.
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302975 */
2976 if (major >= 1 && minor != 0x11 && minor != 0x12) {
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002977 struct sdhci_msm_reg_data *vdd_io_reg;
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002978 /*
2979 * Enable 1.8V support capability on controllers that
2980 * support dual voltage
2981 */
2982 vdd_io_reg = msm_host->pdata->vreg_data->vdd_io_data;
Krishna Konda46fd1432014-10-30 21:13:27 -07002983 if (vdd_io_reg && (vdd_io_reg->high_vol_level > 2700000))
2984 caps |= CORE_3_0V_SUPPORT;
2985 if (vdd_io_reg && (vdd_io_reg->low_vol_level < 1950000))
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002986 caps |= CORE_1_8V_SUPPORT;
Pratibhasagar Vada47992013-12-09 20:42:32 +05302987 if (msm_host->pdata->mmc_bus_width == MMC_CAP_8_BIT_DATA)
2988 caps |= CORE_8_BIT_SUPPORT;
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302989 }
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002990
2991 /*
Sahitya Tummala2ba05ae2015-03-03 14:03:20 +05302992 * Enable one MID mode for SDCC5 (major 1) on 8916/8939 (minor 0x2e) and
2993 * on 8992 (minor 0x3e) as a workaround to reset for data stuck issue.
2994 */
2995 if (major == 1 && (minor == 0x2e || minor == 0x3e)) {
Pavan Anamula691dd592015-08-25 16:11:20 +05302996 host->quirks2 |= SDHCI_QUIRK2_USE_RESET_WORKAROUND;
Sahitya Tummala2ba05ae2015-03-03 14:03:20 +05302997 val = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_FUNC2);
2998 writel_relaxed((val | CORE_ONE_MID_EN),
2999 host->ioaddr + CORE_VENDOR_SPEC_FUNC2);
3000 }
3001 /*
Krishna Konda2faa7bb2014-06-04 01:25:16 -07003002 * SDCC 5 controller with major version 1, minor version 0x34 and later
3003 * with HS 400 mode support will use CM DLL instead of CDC LP 533 DLL.
3004 */
3005 if ((major == 1) && (minor < 0x34))
3006 msm_host->use_cdclp533 = true;
Gilad Broner2a10ca02014-10-02 17:20:35 +03003007
3008 /*
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -08003009 * SDCC 5 controller with major version 1, minor version 0x42 and later
3010 * will require additional steps when resetting DLL.
Ritesh Harjaniea709662015-05-27 15:40:24 +05303011 * It also supports HS400 enhanced strobe mode.
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -08003012 */
Ritesh Harjaniea709662015-05-27 15:40:24 +05303013 if ((major == 1) && (minor >= 0x42)) {
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -08003014 msm_host->use_updated_dll_reset = true;
Ritesh Harjaniea709662015-05-27 15:40:24 +05303015 msm_host->enhanced_strobe = true;
3016 }
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -08003017
3018 /*
Talel Shenhar9a25b882015-06-02 13:36:35 +03003019 * SDCC 5 controller with major version 1 and minor version 0x42,
3020 * 0x46 and 0x49 currently uses 14lpp tech DLL whose internal
3021 * gating cannot guarantee MCLK timing requirement i.e.
Ritesh Harjani764065e2015-05-13 14:14:45 +05303022 * when MCLK is gated OFF, it is not gated for less than 0.5us
3023 * and MCLK must be switched on for at-least 1us before DATA
3024 * starts coming.
3025 */
Talel Shenhar9a25b882015-06-02 13:36:35 +03003026 if ((major == 1) && ((minor == 0x42) || (minor == 0x46) ||
3027 (minor == 0x49)))
Ritesh Harjani764065e2015-05-13 14:14:45 +05303028 msm_host->use_14lpp_dll = true;
Venkat Gopalakrishnanb47cf402015-09-04 18:32:25 -07003029
3030 if ((major == 1) && (minor >= 0x49))
3031 msm_host->rclk_delay_fix = true;
Ritesh Harjani764065e2015-05-13 14:14:45 +05303032 /*
Gilad Broner2a10ca02014-10-02 17:20:35 +03003033 * Mask 64-bit support for controller with 32-bit address bus so that
3034 * smaller descriptor size will be used and improve memory consumption.
Gilad Broner2a10ca02014-10-02 17:20:35 +03003035 */
Venkat Gopalakrishnan9a62e042015-03-03 16:14:55 -08003036 if (!msm_host->pdata->largeaddressbus)
3037 caps &= ~CORE_SYS_BUS_SUPPORT_64_BIT;
3038
Gilad Broner2a10ca02014-10-02 17:20:35 +03003039 writel_relaxed(caps, host->ioaddr + CORE_VENDOR_SPEC_CAPABILITIES0);
Krishna Konda46fd1432014-10-30 21:13:27 -07003040 /* keep track of the value in SDHCI_CAPABILITIES */
3041 msm_host->caps_0 = caps;
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05303042}
3043
Venkat Gopalakrishnand371f142015-05-29 17:49:46 -07003044#ifdef CONFIG_MMC_CQ_HCI
3045static void sdhci_msm_cmdq_init(struct sdhci_host *host,
3046 struct platform_device *pdev)
3047{
3048 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3049 struct sdhci_msm_host *msm_host = pltfm_host->priv;
3050
3051 host->cq_host = cmdq_pltfm_init(pdev);
3052 if (IS_ERR(host->cq_host))
3053 dev_dbg(&pdev->dev, "cmdq-pltfm init: failed: %ld\n",
3054 PTR_ERR(host->cq_host));
3055 else
3056 msm_host->mmc->caps2 |= MMC_CAP2_CMD_QUEUE;
3057}
3058#else
3059static void sdhci_msm_cmdq_init(struct sdhci_host *host,
3060 struct platform_device *pdev)
3061{
3062
3063}
3064#endif
3065
Subhash Jadavaniebcd00d2015-07-09 17:28:42 -07003066static bool sdhci_msm_is_bootdevice(struct device *dev)
3067{
3068 if (strnstr(saved_command_line, "androidboot.bootdevice=",
3069 strlen(saved_command_line))) {
3070 char search_string[50];
3071
3072 snprintf(search_string, ARRAY_SIZE(search_string),
3073 "androidboot.bootdevice=%s", dev_name(dev));
3074 if (strnstr(saved_command_line, search_string,
3075 strlen(saved_command_line)))
3076 return true;
3077 else
3078 return false;
3079 }
3080
3081 /*
3082 * "androidboot.bootdevice=" argument is not present then
3083 * return true as we don't know the boot device anyways.
3084 */
3085 return true;
3086}
3087
Asutosh Das0ef24812012-12-18 16:14:02 +05303088static int sdhci_msm_probe(struct platform_device *pdev)
3089{
3090 struct sdhci_host *host;
3091 struct sdhci_pltfm_host *pltfm_host;
3092 struct sdhci_msm_host *msm_host;
3093 struct resource *core_memres = NULL;
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02003094 int ret = 0, dead = 0;
Stephen Boyd8dce5c62013-04-24 14:19:46 -07003095 u16 host_version;
Venkat Gopalakrishnan17edb352015-06-24 14:46:49 -07003096 u32 irq_status, irq_ctl;
Asutosh Das0ef24812012-12-18 16:14:02 +05303097
3098 pr_debug("%s: Enter %s\n", dev_name(&pdev->dev), __func__);
3099 msm_host = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_msm_host),
3100 GFP_KERNEL);
3101 if (!msm_host) {
3102 ret = -ENOMEM;
3103 goto out;
3104 }
Asutosh Das0ef24812012-12-18 16:14:02 +05303105
3106 msm_host->sdhci_msm_pdata.ops = &sdhci_msm_ops;
3107 host = sdhci_pltfm_init(pdev, &msm_host->sdhci_msm_pdata, 0);
3108 if (IS_ERR(host)) {
3109 ret = PTR_ERR(host);
3110 goto out;
3111 }
3112
3113 pltfm_host = sdhci_priv(host);
3114 pltfm_host->priv = msm_host;
3115 msm_host->mmc = host->mmc;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05303116 msm_host->pdev = pdev;
Asutosh Das0ef24812012-12-18 16:14:02 +05303117
3118 /* Extract platform data */
3119 if (pdev->dev.of_node) {
Venkat Gopalakrishnan270580a2013-03-11 12:17:57 -07003120 ret = of_alias_get_id(pdev->dev.of_node, "sdhc");
3121 if (ret < 0) {
3122 dev_err(&pdev->dev, "Failed to get slot index %d\n",
3123 ret);
3124 goto pltfm_free;
3125 }
Subhash Jadavaniebcd00d2015-07-09 17:28:42 -07003126
3127 /* skip the probe if eMMC isn't a boot device */
Venkat Gopalakrishnanb5dd7762015-09-10 12:25:27 -07003128 if ((ret == 1) && !sdhci_msm_is_bootdevice(&pdev->dev)) {
3129 ret = -ENODEV;
Subhash Jadavaniebcd00d2015-07-09 17:28:42 -07003130 goto pltfm_free;
Venkat Gopalakrishnanb5dd7762015-09-10 12:25:27 -07003131 }
Subhash Jadavaniebcd00d2015-07-09 17:28:42 -07003132
Venkat Gopalakrishnan270580a2013-03-11 12:17:57 -07003133 if (disable_slots & (1 << (ret - 1))) {
3134 dev_info(&pdev->dev, "%s: Slot %d disabled\n", __func__,
3135 ret);
3136 ret = -ENODEV;
3137 goto pltfm_free;
3138 }
3139
Dov Levenglickc9033ab2015-03-10 16:00:56 +02003140 msm_host->pdata = sdhci_msm_populate_pdata(&pdev->dev,
3141 msm_host);
Asutosh Das0ef24812012-12-18 16:14:02 +05303142 if (!msm_host->pdata) {
3143 dev_err(&pdev->dev, "DT parsing error\n");
3144 goto pltfm_free;
3145 }
3146 } else {
3147 dev_err(&pdev->dev, "No device tree node\n");
3148 goto pltfm_free;
3149 }
3150
3151 /* Setup Clocks */
3152
3153 /* Setup SDCC bus voter clock. */
3154 msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus_clk");
3155 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
3156 /* Vote for max. clk rate for max. performance */
3157 ret = clk_set_rate(msm_host->bus_clk, INT_MAX);
3158 if (ret)
3159 goto pltfm_free;
3160 ret = clk_prepare_enable(msm_host->bus_clk);
3161 if (ret)
3162 goto pltfm_free;
3163 }
3164
3165 /* Setup main peripheral bus clock */
3166 msm_host->pclk = devm_clk_get(&pdev->dev, "iface_clk");
3167 if (!IS_ERR(msm_host->pclk)) {
3168 ret = clk_prepare_enable(msm_host->pclk);
3169 if (ret)
3170 goto bus_clk_disable;
3171 }
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05303172 atomic_set(&msm_host->controller_clock, 1);
Asutosh Das0ef24812012-12-18 16:14:02 +05303173
3174 /* Setup SDC MMC clock */
3175 msm_host->clk = devm_clk_get(&pdev->dev, "core_clk");
3176 if (IS_ERR(msm_host->clk)) {
3177 ret = PTR_ERR(msm_host->clk);
3178 goto pclk_disable;
3179 }
3180
Sahitya Tummala22dd3362013-02-28 19:50:51 +05303181 /* Set to the minimum supported clock frequency */
3182 ret = clk_set_rate(msm_host->clk, sdhci_msm_get_min_clock(host));
3183 if (ret) {
3184 dev_err(&pdev->dev, "MClk rate set failed (%d)\n", ret);
Sahitya Tummala020ede0d2013-06-07 13:03:07 +05303185 goto pclk_disable;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05303186 }
Sahitya Tummala020ede0d2013-06-07 13:03:07 +05303187 ret = clk_prepare_enable(msm_host->clk);
3188 if (ret)
3189 goto pclk_disable;
3190
Sahitya Tummala22dd3362013-02-28 19:50:51 +05303191 msm_host->clk_rate = sdhci_msm_get_min_clock(host);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05303192 atomic_set(&msm_host->clks_on, 1);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05303193
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07003194 /* Setup CDC calibration fixed feedback clock */
3195 msm_host->ff_clk = devm_clk_get(&pdev->dev, "cal_clk");
3196 if (!IS_ERR(msm_host->ff_clk)) {
3197 ret = clk_prepare_enable(msm_host->ff_clk);
3198 if (ret)
3199 goto clk_disable;
3200 }
3201
3202 /* Setup CDC calibration sleep clock */
3203 msm_host->sleep_clk = devm_clk_get(&pdev->dev, "sleep_clk");
3204 if (!IS_ERR(msm_host->sleep_clk)) {
3205 ret = clk_prepare_enable(msm_host->sleep_clk);
3206 if (ret)
3207 goto ff_clk_disable;
3208 }
3209
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07003210 msm_host->saved_tuning_phase = INVALID_TUNING_PHASE;
3211
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05303212 ret = sdhci_msm_bus_register(msm_host, pdev);
3213 if (ret)
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07003214 goto sleep_clk_disable;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05303215
3216 if (msm_host->msm_bus_vote.client_handle)
3217 INIT_DELAYED_WORK(&msm_host->msm_bus_vote.vote_work,
3218 sdhci_msm_bus_work);
3219 sdhci_msm_bus_voting(host, 1);
3220
Asutosh Das0ef24812012-12-18 16:14:02 +05303221 /* Setup regulators */
3222 ret = sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, true);
3223 if (ret) {
3224 dev_err(&pdev->dev, "Regulator setup failed (%d)\n", ret);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05303225 goto bus_unregister;
Asutosh Das0ef24812012-12-18 16:14:02 +05303226 }
3227
3228 /* Reset the core and Enable SDHC mode */
3229 core_memres = platform_get_resource_byname(pdev,
3230 IORESOURCE_MEM, "core_mem");
Asutosh Das890bdee2014-08-08 23:01:42 +05303231 if (!core_memres) {
3232 dev_err(&pdev->dev, "Failed to get iomem resource\n");
3233 goto vreg_deinit;
3234 }
Asutosh Das0ef24812012-12-18 16:14:02 +05303235 msm_host->core_mem = devm_ioremap(&pdev->dev, core_memres->start,
3236 resource_size(core_memres));
3237
3238 if (!msm_host->core_mem) {
3239 dev_err(&pdev->dev, "Failed to remap registers\n");
3240 ret = -ENOMEM;
3241 goto vreg_deinit;
3242 }
3243
Sahitya Tummala66b0fe32013-04-25 11:50:56 +05303244 /*
Venkat Gopalakrishnan17edb352015-06-24 14:46:49 -07003245 * Reset the vendor spec register to power on reset state.
Sahitya Tummala66b0fe32013-04-25 11:50:56 +05303246 */
Venkat Gopalakrishnan17edb352015-06-24 14:46:49 -07003247 writel_relaxed(CORE_VENDOR_SPEC_POR_VAL,
3248 host->ioaddr + CORE_VENDOR_SPEC);
Sahitya Tummala66b0fe32013-04-25 11:50:56 +05303249
Asutosh Das0ef24812012-12-18 16:14:02 +05303250 /* Set HC_MODE_EN bit in HC_MODE register */
3251 writel_relaxed(HC_MODE_EN, (msm_host->core_mem + CORE_HC_MODE));
3252
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07003253 /* Set FF_CLK_SW_RST_DIS bit in HC_MODE register */
3254 writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_HC_MODE) |
3255 FF_CLK_SW_RST_DIS, msm_host->core_mem + CORE_HC_MODE);
3256
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05303257 sdhci_set_default_hw_caps(msm_host, host);
Krishna Konda46fd1432014-10-30 21:13:27 -07003258
3259 /*
3260 * Set the PAD_PWR_SWTICH_EN bit so that the PAD_PWR_SWITCH bit can
3261 * be used as required later on.
3262 */
3263 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
3264 CORE_IO_PAD_PWR_SWITCH_EN),
3265 host->ioaddr + CORE_VENDOR_SPEC);
Asutosh Das0ef24812012-12-18 16:14:02 +05303266 /*
Subhash Jadavani28137342013-05-14 17:46:43 +05303267 * CORE_SW_RST above may trigger power irq if previous status of PWRCTL
3268 * was either BUS_ON or IO_HIGH_V. So before we enable the power irq
3269 * interrupt in GIC (by registering the interrupt handler), we need to
3270 * ensure that any pending power irq interrupt status is acknowledged
3271 * otherwise power irq interrupt handler would be fired prematurely.
3272 */
3273 irq_status = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
3274 writel_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
3275 irq_ctl = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_CTL);
3276 if (irq_status & (CORE_PWRCTL_BUS_ON | CORE_PWRCTL_BUS_OFF))
3277 irq_ctl |= CORE_PWRCTL_BUS_SUCCESS;
3278 if (irq_status & (CORE_PWRCTL_IO_HIGH | CORE_PWRCTL_IO_LOW))
3279 irq_ctl |= CORE_PWRCTL_IO_SUCCESS;
3280 writel_relaxed(irq_ctl, (msm_host->core_mem + CORE_PWRCTL_CTL));
Krishna Konda46fd1432014-10-30 21:13:27 -07003281
Subhash Jadavani28137342013-05-14 17:46:43 +05303282 /*
3283 * Ensure that above writes are propogated before interrupt enablement
3284 * in GIC.
3285 */
3286 mb();
3287
3288 /*
Asutosh Das0ef24812012-12-18 16:14:02 +05303289 * Following are the deviations from SDHC spec v3.0 -
3290 * 1. Card detection is handled using separate GPIO.
3291 * 2. Bus power control is handled by interacting with PMIC.
3292 */
3293 host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
3294 host->quirks |= SDHCI_QUIRK_SINGLE_POWER_WRITE;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05303295 host->quirks |= SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN;
Talel Shenhar4661c2a2015-06-24 15:49:30 +03003296 host->quirks |= SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05303297 host->quirks2 |= SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK;
Sahitya Tummala87d43942013-04-12 11:49:11 +05303298 host->quirks2 |= SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD;
Sahitya Tummala314162c2013-04-12 12:11:20 +05303299 host->quirks2 |= SDHCI_QUIRK2_BROKEN_PRESET_VALUE;
Sahitya Tummala7c9780d2013-04-12 11:59:25 +05303300 host->quirks2 |= SDHCI_QUIRK2_USE_RESERVED_MAX_TIMEOUT;
Asutosh Das0ef24812012-12-18 16:14:02 +05303301
Sahitya Tummalaa5733ab52013-06-10 16:32:51 +05303302 if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK)
3303 host->quirks2 |= SDHCI_QUIRK2_DIVIDE_TOUT_BY_4;
3304
Stephen Boyd8dce5c62013-04-24 14:19:46 -07003305 host_version = readw_relaxed((host->ioaddr + SDHCI_HOST_VERSION));
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07003306 dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n",
3307 host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >>
3308 SDHCI_VENDOR_VER_SHIFT));
3309 if (((host_version & SDHCI_VENDOR_VER_MASK) >>
3310 SDHCI_VENDOR_VER_SHIFT) == SDHCI_VER_100) {
3311 /*
3312 * Add 40us delay in interrupt handler when
3313 * operating at initialization frequency(400KHz).
3314 */
3315 host->quirks2 |= SDHCI_QUIRK2_SLOW_INT_CLR;
3316 /*
3317 * Set Software Reset for DAT line in Software
3318 * Reset Register (Bit 2).
3319 */
3320 host->quirks2 |= SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT;
3321 }
3322
Asutosh Das214b9662013-06-13 14:27:42 +05303323 host->quirks2 |= SDHCI_QUIRK2_IGN_DATA_END_BIT_ERROR;
3324
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07003325 /* Setup PWRCTL irq */
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02003326 msm_host->pwr_irq = platform_get_irq_byname(pdev, "pwr_irq");
3327 if (msm_host->pwr_irq < 0) {
Asutosh Das0ef24812012-12-18 16:14:02 +05303328 dev_err(&pdev->dev, "Failed to get pwr_irq by name (%d)\n",
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02003329 msm_host->pwr_irq);
Asutosh Das0ef24812012-12-18 16:14:02 +05303330 goto vreg_deinit;
3331 }
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02003332 ret = devm_request_threaded_irq(&pdev->dev, msm_host->pwr_irq, NULL,
Asutosh Das0ef24812012-12-18 16:14:02 +05303333 sdhci_msm_pwr_irq, IRQF_ONESHOT,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07003334 dev_name(&pdev->dev), host);
Asutosh Das0ef24812012-12-18 16:14:02 +05303335 if (ret) {
3336 dev_err(&pdev->dev, "Request threaded irq(%d) failed (%d)\n",
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02003337 msm_host->pwr_irq, ret);
Asutosh Das0ef24812012-12-18 16:14:02 +05303338 goto vreg_deinit;
3339 }
3340
3341 /* Enable pwr irq interrupts */
3342 writel_relaxed(INT_MASK, (msm_host->core_mem + CORE_PWRCTL_MASK));
3343
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05303344#ifdef CONFIG_MMC_CLKGATE
3345 /* Set clock gating delay to be used when CONFIG_MMC_CLKGATE is set */
3346 msm_host->mmc->clkgate_delay = SDHCI_MSM_MMC_CLK_GATE_DELAY;
3347#endif
3348
Asutosh Das0ef24812012-12-18 16:14:02 +05303349 /* Set host capabilities */
3350 msm_host->mmc->caps |= msm_host->pdata->mmc_bus_width;
3351 msm_host->mmc->caps |= msm_host->pdata->caps;
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003352 msm_host->mmc->caps |= MMC_CAP_AGGRESSIVE_PM;
Ritesh Harjani34354722015-08-05 11:27:00 +05303353 msm_host->mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY;
Asutosh Das0ef24812012-12-18 16:14:02 +05303354 msm_host->mmc->caps2 |= msm_host->pdata->caps2;
Venkat Gopalakrishnan97c16112014-12-16 18:26:25 -08003355 msm_host->mmc->caps2 |= MMC_CAP2_BOOTPART_NOACC;
3356 msm_host->mmc->caps2 |= MMC_CAP2_FULL_PWR_CYCLE;
Subhash Jadavani6d472b22013-05-29 15:52:10 +05303357 msm_host->mmc->caps2 |= MMC_CAP2_ASYNC_SDIO_IRQ_4BIT_MODE;
Venkat Gopalakrishnan97c16112014-12-16 18:26:25 -08003358 msm_host->mmc->caps2 |= MMC_CAP2_HS400_POST_TUNING;
Talel Shenhar3d1dbf32015-05-13 14:08:39 +03003359 msm_host->mmc->caps2 |= MMC_CAP2_CLK_SCALE;
Pavan Anamula07d62ef2015-08-24 18:56:22 +05303360 msm_host->mmc->caps2 |= MMC_CAP2_SANITIZE;
Asutosh Das0ef24812012-12-18 16:14:02 +05303361
3362 if (msm_host->pdata->nonremovable)
3363 msm_host->mmc->caps |= MMC_CAP_NONREMOVABLE;
3364
Guoping Yuf7c91332014-08-20 16:56:18 +08003365 if (msm_host->pdata->nonhotplug)
3366 msm_host->mmc->caps2 |= MMC_CAP2_NONHOTPLUG;
3367
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05303368 host->cpu_dma_latency_us = msm_host->pdata->cpu_dma_latency_us;
Maya Erez994cf2f2014-10-21 20:22:04 +03003369 host->pm_qos_req_dma.type = msm_host->pdata->cpu_affinity_type;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05303370
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05303371 init_completion(&msm_host->pwr_irq_completion);
3372
Sahitya Tummala581df132013-03-12 14:57:46 +05303373 if (gpio_is_valid(msm_host->pdata->status_gpio)) {
Sahitya Tummala6ddabb42014-06-05 13:26:55 +05303374 /*
3375 * Set up the card detect GPIO in active configuration before
3376 * configuring it as an IRQ. Otherwise, it can be in some
3377 * weird/inconsistent state resulting in flood of interrupts.
3378 */
3379 sdhci_msm_setup_pins(msm_host->pdata, true);
3380
Sahitya Tummalaa3888f42015-02-05 14:05:27 +05303381 /*
3382 * This delay is needed for stabilizing the card detect GPIO
3383 * line after changing the pull configs.
3384 */
3385 usleep_range(10000, 10500);
Sahitya Tummala581df132013-03-12 14:57:46 +05303386 ret = mmc_gpio_request_cd(msm_host->mmc,
3387 msm_host->pdata->status_gpio, 0);
3388 if (ret) {
3389 dev_err(&pdev->dev, "%s: Failed to request card detection IRQ %d\n",
3390 __func__, ret);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05303391 goto vreg_deinit;
Sahitya Tummala581df132013-03-12 14:57:46 +05303392 }
3393 }
3394
Krishna Konda7feab352013-09-17 23:55:40 -07003395 if ((sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT) &&
3396 (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(64)))) {
3397 host->dma_mask = DMA_BIT_MASK(64);
3398 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
Pavan Anamulaeeb3b142015-07-22 18:17:32 +05303399 mmc_dev(host->mmc)->coherent_dma_mask = host->dma_mask;
Krishna Konda7feab352013-09-17 23:55:40 -07003400 } else if (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(32))) {
Sahitya Tummalaeaa21862013-03-20 19:34:59 +05303401 host->dma_mask = DMA_BIT_MASK(32);
3402 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
Pavan Anamulaeeb3b142015-07-22 18:17:32 +05303403 mmc_dev(host->mmc)->coherent_dma_mask = host->dma_mask;
Sahitya Tummalaeaa21862013-03-20 19:34:59 +05303404 } else {
3405 dev_err(&pdev->dev, "%s: Failed to set dma mask\n", __func__);
3406 }
3407
Venkat Gopalakrishnand371f142015-05-29 17:49:46 -07003408 sdhci_msm_cmdq_init(host, pdev);
Asutosh Das0ef24812012-12-18 16:14:02 +05303409 ret = sdhci_add_host(host);
3410 if (ret) {
3411 dev_err(&pdev->dev, "Add host failed (%d)\n", ret);
Sahitya Tummala581df132013-03-12 14:57:46 +05303412 goto vreg_deinit;
Asutosh Das0ef24812012-12-18 16:14:02 +05303413 }
3414
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003415 pm_runtime_set_active(&pdev->dev);
3416 pm_runtime_enable(&pdev->dev);
3417 pm_runtime_set_autosuspend_delay(&pdev->dev, MSM_AUTOSUSPEND_DELAY_MS);
3418 pm_runtime_use_autosuspend(&pdev->dev);
3419
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05303420 msm_host->msm_bus_vote.max_bus_bw.show = show_sdhci_max_bus_bw;
3421 msm_host->msm_bus_vote.max_bus_bw.store = store_sdhci_max_bus_bw;
3422 sysfs_attr_init(&msm_host->msm_bus_vote.max_bus_bw.attr);
3423 msm_host->msm_bus_vote.max_bus_bw.attr.name = "max_bus_bw";
3424 msm_host->msm_bus_vote.max_bus_bw.attr.mode = S_IRUGO | S_IWUSR;
3425 ret = device_create_file(&pdev->dev,
3426 &msm_host->msm_bus_vote.max_bus_bw);
3427 if (ret)
3428 goto remove_host;
3429
Sahitya Tummala5c55b932013-06-20 14:00:18 +05303430 if (!gpio_is_valid(msm_host->pdata->status_gpio)) {
3431 msm_host->polling.show = show_polling;
3432 msm_host->polling.store = store_polling;
3433 sysfs_attr_init(&msm_host->polling.attr);
3434 msm_host->polling.attr.name = "polling";
3435 msm_host->polling.attr.mode = S_IRUGO | S_IWUSR;
3436 ret = device_create_file(&pdev->dev, &msm_host->polling);
3437 if (ret)
3438 goto remove_max_bus_bw_file;
3439 }
Asutosh Dase5e9ca62013-07-30 19:08:36 +05303440
3441 msm_host->auto_cmd21_attr.show = show_auto_cmd21;
3442 msm_host->auto_cmd21_attr.store = store_auto_cmd21;
3443 sysfs_attr_init(&msm_host->auto_cmd21_attr.attr);
3444 msm_host->auto_cmd21_attr.attr.name = "enable_auto_cmd21";
3445 msm_host->auto_cmd21_attr.attr.mode = S_IRUGO | S_IWUSR;
3446 ret = device_create_file(&pdev->dev, &msm_host->auto_cmd21_attr);
3447 if (ret) {
3448 pr_err("%s: %s: failed creating auto-cmd21 attr: %d\n",
3449 mmc_hostname(host->mmc), __func__, ret);
3450 device_remove_file(&pdev->dev, &msm_host->auto_cmd21_attr);
3451 }
Asutosh Das0ef24812012-12-18 16:14:02 +05303452 /* Successful initialization */
3453 goto out;
3454
Sahitya Tummala5c55b932013-06-20 14:00:18 +05303455remove_max_bus_bw_file:
3456 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Asutosh Das0ef24812012-12-18 16:14:02 +05303457remove_host:
3458 dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003459 pm_runtime_disable(&pdev->dev);
Asutosh Das0ef24812012-12-18 16:14:02 +05303460 sdhci_remove_host(host, dead);
3461vreg_deinit:
3462 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05303463bus_unregister:
3464 if (msm_host->msm_bus_vote.client_handle)
3465 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
3466 sdhci_msm_bus_unregister(msm_host);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07003467sleep_clk_disable:
3468 if (!IS_ERR(msm_host->sleep_clk))
3469 clk_disable_unprepare(msm_host->sleep_clk);
3470ff_clk_disable:
3471 if (!IS_ERR(msm_host->ff_clk))
3472 clk_disable_unprepare(msm_host->ff_clk);
Asutosh Das0ef24812012-12-18 16:14:02 +05303473clk_disable:
3474 if (!IS_ERR(msm_host->clk))
3475 clk_disable_unprepare(msm_host->clk);
3476pclk_disable:
3477 if (!IS_ERR(msm_host->pclk))
3478 clk_disable_unprepare(msm_host->pclk);
3479bus_clk_disable:
3480 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
3481 clk_disable_unprepare(msm_host->bus_clk);
3482pltfm_free:
3483 sdhci_pltfm_free(pdev);
3484out:
3485 pr_debug("%s: Exit %s\n", dev_name(&pdev->dev), __func__);
3486 return ret;
3487}
3488
3489static int sdhci_msm_remove(struct platform_device *pdev)
3490{
3491 struct sdhci_host *host = platform_get_drvdata(pdev);
3492 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3493 struct sdhci_msm_host *msm_host = pltfm_host->priv;
3494 struct sdhci_msm_pltfm_data *pdata = msm_host->pdata;
3495 int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) ==
3496 0xffffffff);
3497
3498 pr_debug("%s: %s\n", dev_name(&pdev->dev), __func__);
Sahitya Tummala5c55b932013-06-20 14:00:18 +05303499 if (!gpio_is_valid(msm_host->pdata->status_gpio))
3500 device_remove_file(&pdev->dev, &msm_host->polling);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05303501 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003502 pm_runtime_disable(&pdev->dev);
Asutosh Das0ef24812012-12-18 16:14:02 +05303503 sdhci_remove_host(host, dead);
3504 sdhci_pltfm_free(pdev);
Sahitya Tummala581df132013-03-12 14:57:46 +05303505
Asutosh Das0ef24812012-12-18 16:14:02 +05303506 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05303507
Pratibhasagar V9acf2642013-11-21 21:07:21 +05303508 sdhci_msm_setup_pins(pdata, true);
3509 sdhci_msm_setup_pins(pdata, false);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05303510
3511 if (msm_host->msm_bus_vote.client_handle) {
3512 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
3513 sdhci_msm_bus_unregister(msm_host);
3514 }
Asutosh Das0ef24812012-12-18 16:14:02 +05303515 return 0;
3516}
3517
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003518#ifdef CONFIG_PM
3519static int sdhci_msm_runtime_suspend(struct device *dev)
3520{
3521 struct sdhci_host *host = dev_get_drvdata(dev);
3522 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3523 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003524 ktime_t start = ktime_get();
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003525
3526 disable_irq(host->irq);
3527 disable_irq(msm_host->pwr_irq);
3528
3529 /*
3530 * Remove the vote immediately only if clocks are off in which
3531 * case we might have queued work to remove vote but it may not
3532 * be completed before runtime suspend or system suspend.
3533 */
3534 if (!atomic_read(&msm_host->clks_on)) {
3535 if (msm_host->msm_bus_vote.client_handle)
3536 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
3537 }
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003538 trace_sdhci_msm_runtime_suspend(mmc_hostname(host->mmc), 0,
3539 ktime_to_us(ktime_sub(ktime_get(), start)));
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003540
3541 return 0;
3542}
3543
3544static int sdhci_msm_runtime_resume(struct device *dev)
3545{
3546 struct sdhci_host *host = dev_get_drvdata(dev);
3547 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3548 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003549 ktime_t start = ktime_get();
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003550
3551 enable_irq(msm_host->pwr_irq);
3552 enable_irq(host->irq);
3553
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003554 trace_sdhci_msm_runtime_resume(mmc_hostname(host->mmc), 0,
3555 ktime_to_us(ktime_sub(ktime_get(), start)));
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003556 return 0;
3557}
3558
3559static int sdhci_msm_suspend(struct device *dev)
3560{
3561 struct sdhci_host *host = dev_get_drvdata(dev);
3562 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3563 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003564 int ret = 0;
3565 ktime_t start = ktime_get();
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003566
3567 if (gpio_is_valid(msm_host->pdata->status_gpio) &&
3568 (msm_host->mmc->slot.cd_irq >= 0))
3569 disable_irq(msm_host->mmc->slot.cd_irq);
3570
3571 if (pm_runtime_suspended(dev)) {
3572 pr_debug("%s: %s: already runtime suspended\n",
3573 mmc_hostname(host->mmc), __func__);
3574 goto out;
3575 }
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003576 ret = sdhci_msm_runtime_suspend(dev);
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003577out:
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003578 trace_sdhci_msm_suspend(mmc_hostname(host->mmc), ret,
3579 ktime_to_us(ktime_sub(ktime_get(), start)));
3580 return ret;
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003581}
3582
3583static int sdhci_msm_resume(struct device *dev)
3584{
3585 struct sdhci_host *host = dev_get_drvdata(dev);
3586 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3587 struct sdhci_msm_host *msm_host = pltfm_host->priv;
3588 int ret = 0;
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003589 ktime_t start = ktime_get();
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003590
3591 if (gpio_is_valid(msm_host->pdata->status_gpio) &&
3592 (msm_host->mmc->slot.cd_irq >= 0))
3593 enable_irq(msm_host->mmc->slot.cd_irq);
3594
3595 if (pm_runtime_suspended(dev)) {
3596 pr_debug("%s: %s: runtime suspended, defer system resume\n",
3597 mmc_hostname(host->mmc), __func__);
3598 goto out;
3599 }
3600
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003601 ret = sdhci_msm_runtime_resume(dev);
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003602out:
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003603 trace_sdhci_msm_resume(mmc_hostname(host->mmc), ret,
3604 ktime_to_us(ktime_sub(ktime_get(), start)));
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003605 return ret;
3606}
3607
3608static const struct dev_pm_ops sdhci_msm_pmops = {
3609 SET_SYSTEM_SLEEP_PM_OPS(sdhci_msm_suspend, sdhci_msm_resume)
3610 SET_RUNTIME_PM_OPS(sdhci_msm_runtime_suspend, sdhci_msm_runtime_resume,
3611 NULL)
3612};
3613
3614#define SDHCI_MSM_PMOPS (&sdhci_msm_pmops)
3615
3616#else
3617#define SDHCI_MSM_PMOPS NULL
3618#endif
Asutosh Das0ef24812012-12-18 16:14:02 +05303619static const struct of_device_id sdhci_msm_dt_match[] = {
3620 {.compatible = "qcom,sdhci-msm"},
Venkat Gopalakrishnan272ba402015-06-25 12:00:02 -07003621 {},
Asutosh Das0ef24812012-12-18 16:14:02 +05303622};
3623MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match);
3624
3625static struct platform_driver sdhci_msm_driver = {
3626 .probe = sdhci_msm_probe,
3627 .remove = sdhci_msm_remove,
3628 .driver = {
3629 .name = "sdhci_msm",
3630 .owner = THIS_MODULE,
3631 .of_match_table = sdhci_msm_dt_match,
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003632 .pm = SDHCI_MSM_PMOPS,
Asutosh Das0ef24812012-12-18 16:14:02 +05303633 },
3634};
3635
3636module_platform_driver(sdhci_msm_driver);
3637
3638MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Secure Digital Host Controller Interface driver");
3639MODULE_LICENSE("GPL v2");