blob: af4c6449387961b8934b70ef14fc6d567687f462 [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
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700179
Sahitya Tummala56874732015-05-21 08:24:03 +0530180#define MSM_MMC_DEFAULT_CPU_DMA_LATENCY 200 /* usecs */
181
Venkat Gopalakrishnan450745e2014-07-24 20:39:34 -0700182/* 512 descriptors */
183#define SDHCI_MSM_MAX_SEGMENTS (1 << 9)
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +0530184#define SDHCI_MSM_MMC_CLK_GATE_DELAY 200 /* msecs */
Asutosh Das648f9d12013-01-10 21:11:04 +0530185
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700186#define CORE_FREQ_100MHZ (100 * 1000 * 1000)
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800187#define TCXO_FREQ 19200000
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700188
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700189#define INVALID_TUNING_PHASE -1
190
Krishna Konda96e6b112013-10-28 15:25:03 -0700191#define NUM_TUNING_PHASES 16
192#define MAX_DRV_TYPES_SUPPORTED_HS200 3
Konstantin Dorfman98377d32015-02-25 10:09:41 +0200193#define MSM_AUTOSUSPEND_DELAY_MS 100
Krishna Konda96e6b112013-10-28 15:25:03 -0700194
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700195static const u32 tuning_block_64[] = {
196 0x00FF0FFF, 0xCCC3CCFF, 0xFFCC3CC3, 0xEFFEFFFE,
197 0xDDFFDFFF, 0xFBFFFBFF, 0xFF7FFFBF, 0xEFBDF777,
198 0xF0FFF0FF, 0x3CCCFC0F, 0xCFCC33CC, 0xEEFFEFFF,
199 0xFDFFFDFF, 0xFFBFFFDF, 0xFFF7FFBB, 0xDE7B7FF7
200};
201
202static const u32 tuning_block_128[] = {
203 0xFF00FFFF, 0x0000FFFF, 0xCCCCFFFF, 0xCCCC33CC,
204 0xCC3333CC, 0xFFFFCCCC, 0xFFFFEEFF, 0xFFEEEEFF,
205 0xFFDDFFFF, 0xDDDDFFFF, 0xBBFFFFFF, 0xBBFFFFFF,
206 0xFFFFFFBB, 0xFFFFFF77, 0x77FF7777, 0xFFEEDDBB,
207 0x00FFFFFF, 0x00FFFFFF, 0xCCFFFF00, 0xCC33CCCC,
208 0x3333CCCC, 0xFFCCCCCC, 0xFFEEFFFF, 0xEEEEFFFF,
209 0xDDFFFFFF, 0xDDFFFFFF, 0xFFFFFFDD, 0xFFFFFFBB,
210 0xFFFFBBBB, 0xFFFF77FF, 0xFF7777FF, 0xEEDDBB77
211};
Asutosh Das0ef24812012-12-18 16:14:02 +0530212
Venkat Gopalakrishnan270580a2013-03-11 12:17:57 -0700213static int disable_slots;
214/* root can write, others read */
215module_param(disable_slots, int, S_IRUGO|S_IWUSR);
216
Asutosh Das0ef24812012-12-18 16:14:02 +0530217enum vdd_io_level {
218 /* set vdd_io_data->low_vol_level */
219 VDD_IO_LOW,
220 /* set vdd_io_data->high_vol_level */
221 VDD_IO_HIGH,
222 /*
223 * set whatever there in voltage_level (third argument) of
224 * sdhci_msm_set_vdd_io_vol() function.
225 */
226 VDD_IO_SET_LEVEL,
227};
228
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700229/* MSM platform specific tuning */
230static inline int msm_dll_poll_ck_out_en(struct sdhci_host *host,
231 u8 poll)
232{
233 int rc = 0;
234 u32 wait_cnt = 50;
235 u8 ck_out_en = 0;
236 struct mmc_host *mmc = host->mmc;
237
238 /* poll for CK_OUT_EN bit. max. poll time = 50us */
239 ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) &
240 CORE_CK_OUT_EN);
241
242 while (ck_out_en != poll) {
243 if (--wait_cnt == 0) {
244 pr_err("%s: %s: CK_OUT_EN bit is not %d\n",
245 mmc_hostname(mmc), __func__, poll);
246 rc = -ETIMEDOUT;
247 goto out;
248 }
249 udelay(1);
250
251 ck_out_en = !!(readl_relaxed(host->ioaddr +
252 CORE_DLL_CONFIG) & CORE_CK_OUT_EN);
253 }
254out:
255 return rc;
256}
257
Asutosh Dase5e9ca62013-07-30 19:08:36 +0530258/*
259 * Enable CDR to track changes of DAT lines and adjust sampling
260 * point according to voltage/temperature variations
261 */
262static int msm_enable_cdr_cm_sdc4_dll(struct sdhci_host *host)
263{
264 int rc = 0;
265 u32 config;
266
267 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
268 config |= CORE_CDR_EN;
269 config &= ~(CORE_CDR_EXT_EN | CORE_CK_OUT_EN);
270 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
271
272 rc = msm_dll_poll_ck_out_en(host, 0);
273 if (rc)
274 goto err;
275
276 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) |
277 CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
278
279 rc = msm_dll_poll_ck_out_en(host, 1);
280 if (rc)
281 goto err;
282 goto out;
283err:
284 pr_err("%s: %s: failed\n", mmc_hostname(host->mmc), __func__);
285out:
286 return rc;
287}
288
289static ssize_t store_auto_cmd21(struct device *dev, struct device_attribute
290 *attr, const char *buf, size_t count)
291{
292 struct sdhci_host *host = dev_get_drvdata(dev);
293 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
294 struct sdhci_msm_host *msm_host = pltfm_host->priv;
295 u32 tmp;
296 unsigned long flags;
297
298 if (!kstrtou32(buf, 0, &tmp)) {
299 spin_lock_irqsave(&host->lock, flags);
300 msm_host->en_auto_cmd21 = !!tmp;
301 spin_unlock_irqrestore(&host->lock, flags);
302 }
303 return count;
304}
305
306static ssize_t show_auto_cmd21(struct device *dev,
307 struct device_attribute *attr, char *buf)
308{
309 struct sdhci_host *host = dev_get_drvdata(dev);
310 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
311 struct sdhci_msm_host *msm_host = pltfm_host->priv;
312
313 return snprintf(buf, PAGE_SIZE, "%d\n", msm_host->en_auto_cmd21);
314}
315
316/* MSM auto-tuning handler */
317static int sdhci_msm_config_auto_tuning_cmd(struct sdhci_host *host,
318 bool enable,
319 u32 type)
320{
321 int rc = 0;
322 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
323 struct sdhci_msm_host *msm_host = pltfm_host->priv;
324 u32 val = 0;
325
326 if (!msm_host->en_auto_cmd21)
327 return 0;
328
329 if (type == MMC_SEND_TUNING_BLOCK_HS200)
330 val = CORE_HC_AUTO_CMD21_EN;
331 else
332 return 0;
333
334 if (enable) {
335 rc = msm_enable_cdr_cm_sdc4_dll(host);
336 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
337 val, host->ioaddr + CORE_VENDOR_SPEC);
338 } else {
339 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
340 ~val, host->ioaddr + CORE_VENDOR_SPEC);
341 }
342 return rc;
343}
344
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700345static int msm_config_cm_dll_phase(struct sdhci_host *host, u8 phase)
346{
347 int rc = 0;
348 u8 grey_coded_phase_table[] = {0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4,
349 0xC, 0xD, 0xF, 0xE, 0xA, 0xB, 0x9,
350 0x8};
351 unsigned long flags;
352 u32 config;
353 struct mmc_host *mmc = host->mmc;
354
355 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
356 spin_lock_irqsave(&host->lock, flags);
357
358 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
359 config &= ~(CORE_CDR_EN | CORE_CK_OUT_EN);
360 config |= (CORE_CDR_EXT_EN | CORE_DLL_EN);
361 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
362
363 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '0' */
364 rc = msm_dll_poll_ck_out_en(host, 0);
365 if (rc)
366 goto err_out;
367
368 /*
369 * Write the selected DLL clock output phase (0 ... 15)
370 * to CDR_SELEXT bit field of DLL_CONFIG register.
371 */
372 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
373 & ~(0xF << 20))
374 | (grey_coded_phase_table[phase] << 20)),
375 host->ioaddr + CORE_DLL_CONFIG);
376
377 /* Set CK_OUT_EN bit of DLL_CONFIG register to 1. */
378 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
379 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
380
381 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '1' */
382 rc = msm_dll_poll_ck_out_en(host, 1);
383 if (rc)
384 goto err_out;
385
386 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
387 config |= CORE_CDR_EN;
388 config &= ~CORE_CDR_EXT_EN;
389 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
390 goto out;
391
392err_out:
393 pr_err("%s: %s: Failed to set DLL phase: %d\n",
394 mmc_hostname(mmc), __func__, phase);
395out:
396 spin_unlock_irqrestore(&host->lock, flags);
397 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
398 return rc;
399}
400
401/*
402 * Find out the greatest range of consecuitive selected
403 * DLL clock output phases that can be used as sampling
404 * setting for SD3.0 UHS-I card read operation (in SDR104
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700405 * timing mode) or for eMMC4.5 card read operation (in
406 * HS400/HS200 timing mode).
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700407 * Select the 3/4 of the range and configure the DLL with the
408 * selected DLL clock output phase.
409 */
410
411static int msm_find_most_appropriate_phase(struct sdhci_host *host,
412 u8 *phase_table, u8 total_phases)
413{
414 int ret;
415 u8 ranges[MAX_PHASES][MAX_PHASES] = { {0}, {0} };
416 u8 phases_per_row[MAX_PHASES] = {0};
417 int row_index = 0, col_index = 0, selected_row_index = 0, curr_max = 0;
418 int i, cnt, phase_0_raw_index = 0, phase_15_raw_index = 0;
419 bool phase_0_found = false, phase_15_found = false;
420 struct mmc_host *mmc = host->mmc;
421
422 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
423 if (!total_phases || (total_phases > MAX_PHASES)) {
424 pr_err("%s: %s: invalid argument: total_phases=%d\n",
425 mmc_hostname(mmc), __func__, total_phases);
426 return -EINVAL;
427 }
428
429 for (cnt = 0; cnt < total_phases; cnt++) {
430 ranges[row_index][col_index] = phase_table[cnt];
431 phases_per_row[row_index] += 1;
432 col_index++;
433
434 if ((cnt + 1) == total_phases) {
435 continue;
436 /* check if next phase in phase_table is consecutive or not */
437 } else if ((phase_table[cnt] + 1) != phase_table[cnt + 1]) {
438 row_index++;
439 col_index = 0;
440 }
441 }
442
443 if (row_index >= MAX_PHASES)
444 return -EINVAL;
445
446 /* Check if phase-0 is present in first valid window? */
447 if (!ranges[0][0]) {
448 phase_0_found = true;
449 phase_0_raw_index = 0;
450 /* Check if cycle exist between 2 valid windows */
451 for (cnt = 1; cnt <= row_index; cnt++) {
452 if (phases_per_row[cnt]) {
453 for (i = 0; i < phases_per_row[cnt]; i++) {
454 if (ranges[cnt][i] == 15) {
455 phase_15_found = true;
456 phase_15_raw_index = cnt;
457 break;
458 }
459 }
460 }
461 }
462 }
463
464 /* If 2 valid windows form cycle then merge them as single window */
465 if (phase_0_found && phase_15_found) {
466 /* number of phases in raw where phase 0 is present */
467 u8 phases_0 = phases_per_row[phase_0_raw_index];
468 /* number of phases in raw where phase 15 is present */
469 u8 phases_15 = phases_per_row[phase_15_raw_index];
470
471 if (phases_0 + phases_15 >= MAX_PHASES)
472 /*
473 * If there are more than 1 phase windows then total
474 * number of phases in both the windows should not be
475 * more than or equal to MAX_PHASES.
476 */
477 return -EINVAL;
478
479 /* Merge 2 cyclic windows */
480 i = phases_15;
481 for (cnt = 0; cnt < phases_0; cnt++) {
482 ranges[phase_15_raw_index][i] =
483 ranges[phase_0_raw_index][cnt];
484 if (++i >= MAX_PHASES)
485 break;
486 }
487
488 phases_per_row[phase_0_raw_index] = 0;
489 phases_per_row[phase_15_raw_index] = phases_15 + phases_0;
490 }
491
492 for (cnt = 0; cnt <= row_index; cnt++) {
493 if (phases_per_row[cnt] > curr_max) {
494 curr_max = phases_per_row[cnt];
495 selected_row_index = cnt;
496 }
497 }
498
499 i = ((curr_max * 3) / 4);
500 if (i)
501 i--;
502
503 ret = (int)ranges[selected_row_index][i];
504
505 if (ret >= MAX_PHASES) {
506 ret = -EINVAL;
507 pr_err("%s: %s: invalid phase selected=%d\n",
508 mmc_hostname(mmc), __func__, ret);
509 }
510
511 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
512 return ret;
513}
514
515static inline void msm_cm_dll_set_freq(struct sdhci_host *host)
516{
517 u32 mclk_freq = 0;
518
519 /* Program the MCLK value to MCLK_FREQ bit field */
520 if (host->clock <= 112000000)
521 mclk_freq = 0;
522 else if (host->clock <= 125000000)
523 mclk_freq = 1;
524 else if (host->clock <= 137000000)
525 mclk_freq = 2;
526 else if (host->clock <= 150000000)
527 mclk_freq = 3;
528 else if (host->clock <= 162000000)
529 mclk_freq = 4;
530 else if (host->clock <= 175000000)
531 mclk_freq = 5;
532 else if (host->clock <= 187000000)
533 mclk_freq = 6;
534 else if (host->clock <= 200000000)
535 mclk_freq = 7;
536
537 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
538 & ~(7 << 24)) | (mclk_freq << 24)),
539 host->ioaddr + CORE_DLL_CONFIG);
540}
541
542/* Initialize the DLL (Programmable Delay Line ) */
543static int msm_init_cm_dll(struct sdhci_host *host)
544{
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800545 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
546 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700547 struct mmc_host *mmc = host->mmc;
548 int rc = 0;
549 unsigned long flags;
550 u32 wait_cnt;
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530551 bool prev_pwrsave, curr_pwrsave;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700552
553 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
554 spin_lock_irqsave(&host->lock, flags);
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530555 prev_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
556 CORE_CLK_PWRSAVE);
557 curr_pwrsave = prev_pwrsave;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700558 /*
559 * Make sure that clock is always enabled when DLL
560 * tuning is in progress. Keeping PWRSAVE ON may
561 * turn off the clock. So let's disable the PWRSAVE
562 * here and re-enable it once tuning is completed.
563 */
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530564 if (prev_pwrsave) {
565 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
566 & ~CORE_CLK_PWRSAVE),
567 host->ioaddr + CORE_VENDOR_SPEC);
568 curr_pwrsave = false;
569 }
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700570
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800571 if (msm_host->use_updated_dll_reset) {
572 /* Disable the DLL clock */
573 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
574 & ~CORE_CK_OUT_EN),
575 host->ioaddr + CORE_DLL_CONFIG);
576
577 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
578 | CORE_DLL_CLOCK_DISABLE),
579 host->ioaddr + CORE_DLL_CONFIG_2);
580 }
581
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700582 /* Write 1 to DLL_RST bit of DLL_CONFIG register */
583 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
584 | CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
585
586 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
587 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
588 | CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
589 msm_cm_dll_set_freq(host);
590
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800591 if (msm_host->use_updated_dll_reset) {
592 u32 mclk_freq = 0;
593
594 if ((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
595 & CORE_FLL_CYCLE_CNT))
596 mclk_freq = (u32) ((host->clock / TCXO_FREQ) * 8);
597 else
598 mclk_freq = (u32) ((host->clock / TCXO_FREQ) * 4);
599
600 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
601 & ~(0xFF << 10)) | (mclk_freq << 10)),
602 host->ioaddr + CORE_DLL_CONFIG_2);
603 /* wait for 5us before enabling DLL clock */
604 udelay(5);
605 }
606
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700607 /* Write 0 to DLL_RST bit of DLL_CONFIG register */
608 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
609 & ~CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
610
611 /* Write 0 to DLL_PDN bit of DLL_CONFIG register */
612 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
613 & ~CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
614
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800615 if (msm_host->use_updated_dll_reset) {
616 msm_cm_dll_set_freq(host);
617 /* Enable the DLL clock */
618 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
619 & ~CORE_DLL_CLOCK_DISABLE),
620 host->ioaddr + CORE_DLL_CONFIG_2);
621 }
622
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700623 /* Set DLL_EN bit to 1. */
624 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
625 | CORE_DLL_EN), host->ioaddr + CORE_DLL_CONFIG);
626
627 /* Set CK_OUT_EN bit to 1. */
628 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
629 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
630
631 wait_cnt = 50;
632 /* Wait until DLL_LOCK bit of DLL_STATUS register becomes '1' */
633 while (!(readl_relaxed(host->ioaddr + CORE_DLL_STATUS) &
634 CORE_DLL_LOCK)) {
635 /* max. wait for 50us sec for LOCK bit to be set */
636 if (--wait_cnt == 0) {
637 pr_err("%s: %s: DLL failed to LOCK\n",
638 mmc_hostname(mmc), __func__);
639 rc = -ETIMEDOUT;
640 goto out;
641 }
642 /* wait for 1us before polling again */
643 udelay(1);
644 }
645
646out:
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530647 /* Restore the correct PWRSAVE state */
648 if (prev_pwrsave ^ curr_pwrsave) {
649 u32 reg = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC);
650
651 if (prev_pwrsave)
652 reg |= CORE_CLK_PWRSAVE;
653 else
654 reg &= ~CORE_CLK_PWRSAVE;
655
656 writel_relaxed(reg, host->ioaddr + CORE_VENDOR_SPEC);
657 }
658
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700659 spin_unlock_irqrestore(&host->lock, flags);
660 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
661 return rc;
662}
663
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700664static int sdhci_msm_cdclp533_calibration(struct sdhci_host *host)
665{
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700666 u32 calib_done;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700667 int ret = 0;
668 int cdc_err = 0;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700669
670 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
671
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700672 /* Write 0 to CDC_T4_DLY_SEL field in VENDOR_SPEC_DDR200_CFG */
673 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
674 & ~CORE_CDC_T4_DLY_SEL),
675 host->ioaddr + CORE_DDR_200_CFG);
676
677 /* Write 0 to CDC_SWITCH_BYPASS_OFF field in CORE_CSR_CDC_GEN_CFG */
678 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG)
679 & ~CORE_CDC_SWITCH_BYPASS_OFF),
680 host->ioaddr + CORE_CSR_CDC_GEN_CFG);
681
682 /* Write 1 to CDC_SWITCH_RC_EN field in CORE_CSR_CDC_GEN_CFG */
683 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG)
684 | CORE_CDC_SWITCH_RC_EN),
685 host->ioaddr + CORE_CSR_CDC_GEN_CFG);
686
687 /* Write 0 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
688 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
689 & ~CORE_START_CDC_TRAFFIC),
690 host->ioaddr + CORE_DDR_200_CFG);
691
692 /*
693 * Perform CDC Register Initialization Sequence
694 *
695 * CORE_CSR_CDC_CTLR_CFG0 0x11800EC
696 * CORE_CSR_CDC_CTLR_CFG1 0x3011111
697 * CORE_CSR_CDC_CAL_TIMER_CFG0 0x1201000
698 * CORE_CSR_CDC_CAL_TIMER_CFG1 0x4
699 * CORE_CSR_CDC_REFCOUNT_CFG 0xCB732020
700 * CORE_CSR_CDC_COARSE_CAL_CFG 0xB19
701 * CORE_CSR_CDC_DELAY_CFG 0x3AC
702 * CORE_CDC_OFFSET_CFG 0x0
703 * CORE_CDC_SLAVE_DDA_CFG 0x16334
704 */
705
706 writel_relaxed(0x11800EC, host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
707 writel_relaxed(0x3011111, host->ioaddr + CORE_CSR_CDC_CTLR_CFG1);
708 writel_relaxed(0x1201000, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
709 writel_relaxed(0x4, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG1);
710 writel_relaxed(0xCB732020, host->ioaddr + CORE_CSR_CDC_REFCOUNT_CFG);
711 writel_relaxed(0xB19, host->ioaddr + CORE_CSR_CDC_COARSE_CAL_CFG);
Subhash Jadavanibe406d92014-06-17 16:47:48 -0700712 writel_relaxed(0x4E2, host->ioaddr + CORE_CSR_CDC_DELAY_CFG);
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700713 writel_relaxed(0x0, host->ioaddr + CORE_CDC_OFFSET_CFG);
714 writel_relaxed(0x16334, host->ioaddr + CORE_CDC_SLAVE_DDA_CFG);
715
716 /* CDC HW Calibration */
717
718 /* Write 1 to SW_TRIG_FULL_CALIB field in CORE_CSR_CDC_CTLR_CFG0 */
719 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
720 | CORE_SW_TRIG_FULL_CALIB),
721 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
722
723 /* Write 0 to SW_TRIG_FULL_CALIB field in CORE_CSR_CDC_CTLR_CFG0 */
724 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
725 & ~CORE_SW_TRIG_FULL_CALIB),
726 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
727
728 /* Write 1 to HW_AUTOCAL_ENA field in CORE_CSR_CDC_CTLR_CFG0 */
729 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
730 | CORE_HW_AUTOCAL_ENA),
731 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
732
733 /* Write 1 to TIMER_ENA field in CORE_CSR_CDC_CAL_TIMER_CFG0 */
734 writel_relaxed((readl_relaxed(host->ioaddr +
735 CORE_CSR_CDC_CAL_TIMER_CFG0) | CORE_TIMER_ENA),
736 host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
737
738 mb();
739
740 /* Poll on CALIBRATION_DONE field in CORE_CSR_CDC_STATUS0 to be 1 */
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700741 ret = readl_poll_timeout(host->ioaddr + CORE_CSR_CDC_STATUS0,
742 calib_done, (calib_done & CORE_CALIBRATION_DONE), 1, 50);
743
744 if (ret == -ETIMEDOUT) {
745 pr_err("%s: %s: CDC Calibration was not completed\n",
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700746 mmc_hostname(host->mmc), __func__);
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700747 goto out;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700748 }
749
750 /* Verify CDC_ERROR_CODE field in CORE_CSR_CDC_STATUS0 is 0 */
751 cdc_err = readl_relaxed(host->ioaddr + CORE_CSR_CDC_STATUS0)
752 & CORE_CDC_ERROR_CODE_MASK;
753 if (cdc_err) {
754 pr_err("%s: %s: CDC Error Code %d\n",
755 mmc_hostname(host->mmc), __func__, cdc_err);
756 ret = -EINVAL;
757 goto out;
758 }
759
760 /* Write 1 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
761 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
762 | CORE_START_CDC_TRAFFIC),
763 host->ioaddr + CORE_DDR_200_CFG);
764out:
765 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
766 __func__, ret);
767 return ret;
768}
769
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700770static int sdhci_msm_cm_dll_sdc4_calibration(struct sdhci_host *host)
771{
Ritesh Harjani764065e2015-05-13 14:14:45 +0530772 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
773 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Pavan Anamulaf7bf5112015-08-21 18:09:42 +0530774 u32 dll_status, ddr_config;
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700775 int ret = 0;
776
777 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
778
779 /*
Pavan Anamulaf7bf5112015-08-21 18:09:42 +0530780 * Reprogramming the value in case it might have been modified by
781 * bootloaders.
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700782 */
Pavan Anamulaf7bf5112015-08-21 18:09:42 +0530783 ddr_config = DDR_CONFIG_POR_VAL & ~DDR_CONFIG_PRG_RCLK_DLY_MASK;
784 ddr_config |= DDR_CONFIG_PRG_RCLK_DLY;
785 writel_relaxed(ddr_config, host->ioaddr + CORE_DDR_CONFIG);
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700786
Ritesh Harjaniea709662015-05-27 15:40:24 +0530787 if (msm_host->enhanced_strobe)
788 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
789 | CORE_CMDIN_RCLK_EN),
790 host->ioaddr + CORE_DDR_200_CFG);
791
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700792 /* Write 1 to DDR_CAL_EN field in CORE_DLL_CONFIG_2 */
793 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
794 | CORE_DDR_CAL_EN),
795 host->ioaddr + CORE_DLL_CONFIG_2);
796
797 /* Poll on DDR_DLL_LOCK bit in CORE_DLL_STATUS to be set */
798 ret = readl_poll_timeout(host->ioaddr + CORE_DLL_STATUS,
799 dll_status, (dll_status & CORE_DDR_DLL_LOCK), 10, 1000);
800
801 if (ret == -ETIMEDOUT) {
802 pr_err("%s: %s: CM_DLL_SDC4 Calibration was not completed\n",
803 mmc_hostname(host->mmc), __func__);
804 goto out;
805 }
806
Ritesh Harjani764065e2015-05-13 14:14:45 +0530807 /*
808 * set CORE_PWRSAVE_DLL bit in CORE_VENDOR_SPEC3.
809 * when MCLK is gated OFF, it is not gated for less than 0.5us
810 * and MCLK must be switched on for at-least 1us before DATA
811 * starts coming. Controllers with 14lpp tech DLL cannot
812 * guarantee above requirement. So PWRSAVE_DLL should not be
813 * turned on for host controllers using this DLL.
814 */
815 if (!msm_host->use_14lpp_dll)
816 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC3)
817 | CORE_PWRSAVE_DLL),
818 host->ioaddr + CORE_VENDOR_SPEC3);
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700819 mb();
820out:
821 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
822 __func__, ret);
823 return ret;
824}
825
Ritesh Harjaniea709662015-05-27 15:40:24 +0530826static int sdhci_msm_enhanced_strobe(struct sdhci_host *host)
827{
828 int ret = 0;
829 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
830 struct sdhci_msm_host *msm_host = pltfm_host->priv;
831 struct mmc_host *mmc = host->mmc;
832
833 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
834
835 if (!msm_host->enhanced_strobe) {
836 pr_debug("%s: host does not support hs400 enhanced strobe\n",
837 mmc_hostname(mmc));
838 return -EINVAL;
839 }
840
841 if (msm_host->calibration_done ||
842 !(mmc->ios.timing == MMC_TIMING_MMC_HS400)) {
843 return 0;
844 }
845
846 /*
847 * Reset the tuning block.
848 */
849 ret = msm_init_cm_dll(host);
850 if (ret)
851 goto out;
852
853 ret = sdhci_msm_cm_dll_sdc4_calibration(host);
854out:
855 if (!ret)
856 msm_host->calibration_done = true;
857 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
858 __func__, ret);
859 return ret;
860}
861
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700862static int sdhci_msm_hs400_dll_calibration(struct sdhci_host *host)
863{
864 int ret = 0;
865 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
866 struct sdhci_msm_host *msm_host = pltfm_host->priv;
867
868 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
869
870 /*
871 * Retuning in HS400 (DDR mode) will fail, just reset the
872 * tuning block and restore the saved tuning phase.
873 */
874 ret = msm_init_cm_dll(host);
875 if (ret)
876 goto out;
877
878 /* Set the selected phase in delay line hw block */
879 ret = msm_config_cm_dll_phase(host, msm_host->saved_tuning_phase);
880 if (ret)
881 goto out;
882
Krishna Konda0e8efba2014-06-23 14:50:38 -0700883 /* Write 1 to CMD_DAT_TRACK_SEL field in DLL_CONFIG */
884 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
885 | CORE_CMD_DAT_TRACK_SEL),
886 host->ioaddr + CORE_DLL_CONFIG);
887
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700888 if (msm_host->use_cdclp533)
889 /* Calibrate CDCLP533 DLL HW */
890 ret = sdhci_msm_cdclp533_calibration(host);
891 else
892 /* Calibrate CM_DLL_SDC4 HW */
893 ret = sdhci_msm_cm_dll_sdc4_calibration(host);
894out:
895 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
896 __func__, ret);
897 return ret;
898}
899
Krishna Konda96e6b112013-10-28 15:25:03 -0700900static void sdhci_msm_set_mmc_drv_type(struct sdhci_host *host, u32 opcode,
901 u8 drv_type)
902{
903 struct mmc_command cmd = {0};
904 struct mmc_request mrq = {NULL};
905 struct mmc_host *mmc = host->mmc;
906 u8 val = ((drv_type << 4) | 2);
907
908 cmd.opcode = MMC_SWITCH;
909 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
910 (EXT_CSD_HS_TIMING << 16) |
911 (val << 8) |
912 EXT_CSD_CMD_SET_NORMAL;
913 cmd.flags = MMC_CMD_AC | MMC_RSP_R1B;
914 /* 1 sec */
915 cmd.busy_timeout = 1000 * 1000;
916
917 memset(cmd.resp, 0, sizeof(cmd.resp));
918 cmd.retries = 3;
919
920 mrq.cmd = &cmd;
921 cmd.data = NULL;
922
923 mmc_wait_for_req(mmc, &mrq);
924 pr_debug("%s: %s: set card drive type to %d\n",
925 mmc_hostname(mmc), __func__,
926 drv_type);
927}
928
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700929int sdhci_msm_execute_tuning(struct sdhci_host *host, u32 opcode)
930{
931 unsigned long flags;
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530932 int tuning_seq_cnt = 3;
Krishna Konda96e6b112013-10-28 15:25:03 -0700933 u8 phase, *data_buf, tuned_phases[NUM_TUNING_PHASES], tuned_phase_cnt;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700934 const u32 *tuning_block_pattern = tuning_block_64;
935 int size = sizeof(tuning_block_64); /* Tuning pattern size in bytes */
936 int rc;
937 struct mmc_host *mmc = host->mmc;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530938 struct mmc_ios ios = host->mmc->ios;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700939 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
940 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Krishna Konda96e6b112013-10-28 15:25:03 -0700941 u8 drv_type = 0;
942 bool drv_type_changed = false;
943 struct mmc_card *card = host->mmc->card;
Sahitya Tummalafaff7f82015-02-25 14:24:52 +0530944 int sts_retry;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530945
946 /*
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700947 * Tuning is required for SDR104, HS200 and HS400 cards and
948 * if clock frequency is greater than 100MHz in these modes.
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530949 */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700950 if (host->clock <= CORE_FREQ_100MHZ ||
951 !((ios.timing == MMC_TIMING_MMC_HS400) ||
952 (ios.timing == MMC_TIMING_MMC_HS200) ||
953 (ios.timing == MMC_TIMING_UHS_SDR104)))
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530954 return 0;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700955
956 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700957
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700958 /* CDC/SDC4 DLL HW calibration is only required for HS400 mode*/
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700959 if (msm_host->tuning_done && !msm_host->calibration_done &&
960 (mmc->ios.timing == MMC_TIMING_MMC_HS400)) {
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700961 rc = sdhci_msm_hs400_dll_calibration(host);
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700962 spin_lock_irqsave(&host->lock, flags);
963 if (!rc)
964 msm_host->calibration_done = true;
965 spin_unlock_irqrestore(&host->lock, flags);
966 goto out;
967 }
968
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700969 spin_lock_irqsave(&host->lock, flags);
970
971 if ((opcode == MMC_SEND_TUNING_BLOCK_HS200) &&
972 (mmc->ios.bus_width == MMC_BUS_WIDTH_8)) {
973 tuning_block_pattern = tuning_block_128;
974 size = sizeof(tuning_block_128);
975 }
976 spin_unlock_irqrestore(&host->lock, flags);
977
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700978 data_buf = kmalloc(size, GFP_KERNEL);
979 if (!data_buf) {
980 rc = -ENOMEM;
981 goto out;
982 }
983
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530984retry:
Krishna Konda96e6b112013-10-28 15:25:03 -0700985 tuned_phase_cnt = 0;
986
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530987 /* first of all reset the tuning block */
988 rc = msm_init_cm_dll(host);
989 if (rc)
990 goto kfree;
991
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700992 phase = 0;
993 do {
994 struct mmc_command cmd = {0};
995 struct mmc_data data = {0};
996 struct mmc_request mrq = {
997 .cmd = &cmd,
998 .data = &data
999 };
1000 struct scatterlist sg;
Sahitya Tummalafaff7f82015-02-25 14:24:52 +05301001 struct mmc_command sts_cmd = {0};
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001002
1003 /* set the phase in delay line hw block */
1004 rc = msm_config_cm_dll_phase(host, phase);
1005 if (rc)
1006 goto kfree;
1007
1008 cmd.opcode = opcode;
1009 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1010
1011 data.blksz = size;
1012 data.blocks = 1;
1013 data.flags = MMC_DATA_READ;
1014 data.timeout_ns = 1000 * 1000 * 1000; /* 1 sec */
1015
1016 data.sg = &sg;
1017 data.sg_len = 1;
1018 sg_init_one(&sg, data_buf, size);
1019 memset(data_buf, 0, size);
1020 mmc_wait_for_req(mmc, &mrq);
1021
Sahitya Tummalafaff7f82015-02-25 14:24:52 +05301022 if (card && (cmd.error || data.error)) {
1023 sts_cmd.opcode = MMC_SEND_STATUS;
1024 sts_cmd.arg = card->rca << 16;
1025 sts_cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1026 sts_retry = 5;
1027 while (sts_retry) {
1028 mmc_wait_for_cmd(mmc, &sts_cmd, 0);
1029
1030 if (sts_cmd.error ||
1031 (R1_CURRENT_STATE(sts_cmd.resp[0])
1032 != R1_STATE_TRAN)) {
1033 sts_retry--;
1034 /*
1035 * wait for at least 146 MCLK cycles for
1036 * the card to move to TRANS state. As
1037 * the MCLK would be min 200MHz for
1038 * tuning, we need max 0.73us delay. To
1039 * be on safer side 1ms delay is given.
1040 */
1041 usleep_range(1000, 1200);
1042 pr_debug("%s: phase %d sts cmd err %d resp 0x%x\n",
1043 mmc_hostname(mmc), phase,
1044 sts_cmd.error, sts_cmd.resp[0]);
1045 continue;
1046 }
1047 break;
1048 };
1049 }
1050
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001051 if (!cmd.error && !data.error &&
1052 !memcmp(data_buf, tuning_block_pattern, size)) {
1053 /* tuning is successful at this tuning point */
1054 tuned_phases[tuned_phase_cnt++] = phase;
Krishna Konda96e6b112013-10-28 15:25:03 -07001055 pr_debug("%s: %s: found *** good *** phase = %d\n",
1056 mmc_hostname(mmc), __func__, phase);
1057 } else {
1058 pr_debug("%s: %s: found ## bad ## phase = %d\n",
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001059 mmc_hostname(mmc), __func__, phase);
1060 }
1061 } while (++phase < 16);
1062
Sahitya Tummaladfdb4af2014-04-01 14:29:13 +05301063 if ((tuned_phase_cnt == NUM_TUNING_PHASES) &&
1064 card && mmc_card_mmc(card)) {
Krishna Konda96e6b112013-10-28 15:25:03 -07001065 /*
1066 * If all phases pass then its a problem. So change the card's
1067 * drive type to a different value, if supported and repeat
1068 * tuning until at least one phase fails. Then set the original
1069 * drive type back.
1070 *
1071 * If all the phases still pass after trying all possible
1072 * drive types, then one of those 16 phases will be picked.
1073 * This is no different from what was going on before the
1074 * modification to change drive type and retune.
1075 */
1076 pr_debug("%s: tuned phases count: %d\n", mmc_hostname(mmc),
1077 tuned_phase_cnt);
1078
1079 /* set drive type to other value . default setting is 0x0 */
1080 while (++drv_type <= MAX_DRV_TYPES_SUPPORTED_HS200) {
1081 if (card->ext_csd.raw_driver_strength &
1082 (1 << drv_type)) {
1083 sdhci_msm_set_mmc_drv_type(host, opcode,
1084 drv_type);
1085 if (!drv_type_changed)
1086 drv_type_changed = true;
1087 goto retry;
1088 }
1089 }
1090 }
1091
1092 /* reset drive type to default (50 ohm) if changed */
1093 if (drv_type_changed)
1094 sdhci_msm_set_mmc_drv_type(host, opcode, 0);
1095
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001096 if (tuned_phase_cnt) {
1097 rc = msm_find_most_appropriate_phase(host, tuned_phases,
1098 tuned_phase_cnt);
1099 if (rc < 0)
1100 goto kfree;
1101 else
1102 phase = (u8)rc;
1103
1104 /*
1105 * Finally set the selected phase in delay
1106 * line hw block.
1107 */
1108 rc = msm_config_cm_dll_phase(host, phase);
1109 if (rc)
1110 goto kfree;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07001111 msm_host->saved_tuning_phase = phase;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001112 pr_debug("%s: %s: finally setting the tuning phase to %d\n",
1113 mmc_hostname(mmc), __func__, phase);
1114 } else {
Sahitya Tummala9fe16532013-06-13 10:36:57 +05301115 if (--tuning_seq_cnt)
1116 goto retry;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001117 /* tuning failed */
1118 pr_err("%s: %s: no tuning point found\n",
1119 mmc_hostname(mmc), __func__);
Sahitya Tummala9fe16532013-06-13 10:36:57 +05301120 rc = -EIO;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001121 }
1122
1123kfree:
1124 kfree(data_buf);
1125out:
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07001126 spin_lock_irqsave(&host->lock, flags);
1127 if (!rc)
1128 msm_host->tuning_done = true;
1129 spin_unlock_irqrestore(&host->lock, flags);
1130 pr_debug("%s: Exit %s, err(%d)\n", mmc_hostname(mmc), __func__, rc);
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001131 return rc;
1132}
1133
Asutosh Das0ef24812012-12-18 16:14:02 +05301134static int sdhci_msm_setup_gpio(struct sdhci_msm_pltfm_data *pdata, bool enable)
1135{
1136 struct sdhci_msm_gpio_data *curr;
1137 int i, ret = 0;
1138
1139 curr = pdata->pin_data->gpio_data;
1140 for (i = 0; i < curr->size; i++) {
1141 if (!gpio_is_valid(curr->gpio[i].no)) {
1142 ret = -EINVAL;
1143 pr_err("%s: Invalid gpio = %d\n", __func__,
1144 curr->gpio[i].no);
1145 goto free_gpios;
1146 }
1147 if (enable) {
1148 ret = gpio_request(curr->gpio[i].no,
1149 curr->gpio[i].name);
1150 if (ret) {
1151 pr_err("%s: gpio_request(%d, %s) failed %d\n",
1152 __func__, curr->gpio[i].no,
1153 curr->gpio[i].name, ret);
1154 goto free_gpios;
1155 }
1156 curr->gpio[i].is_enabled = true;
1157 } else {
1158 gpio_free(curr->gpio[i].no);
1159 curr->gpio[i].is_enabled = false;
1160 }
1161 }
1162 return ret;
1163
1164free_gpios:
1165 for (i--; i >= 0; i--) {
1166 gpio_free(curr->gpio[i].no);
1167 curr->gpio[i].is_enabled = false;
1168 }
1169 return ret;
1170}
1171
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301172static int sdhci_msm_setup_pinctrl(struct sdhci_msm_pltfm_data *pdata,
1173 bool enable)
1174{
1175 int ret = 0;
1176
1177 if (enable)
1178 ret = pinctrl_select_state(pdata->pctrl_data->pctrl,
1179 pdata->pctrl_data->pins_active);
1180 else
1181 ret = pinctrl_select_state(pdata->pctrl_data->pctrl,
1182 pdata->pctrl_data->pins_sleep);
1183
1184 if (ret < 0)
1185 pr_err("%s state for pinctrl failed with %d\n",
1186 enable ? "Enabling" : "Disabling", ret);
1187
1188 return ret;
1189}
1190
Asutosh Das0ef24812012-12-18 16:14:02 +05301191static int sdhci_msm_setup_pins(struct sdhci_msm_pltfm_data *pdata, bool enable)
1192{
1193 int ret = 0;
1194
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301195 if (pdata->pin_cfg_sts == enable) {
Asutosh Das0ef24812012-12-18 16:14:02 +05301196 return 0;
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301197 } else if (pdata->pctrl_data) {
1198 ret = sdhci_msm_setup_pinctrl(pdata, enable);
1199 goto out;
1200 } else if (!pdata->pin_data) {
1201 return 0;
1202 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301203
Sahitya Tummala1cd7e072014-02-14 13:19:01 +05301204 if (pdata->pin_data->is_gpio)
1205 ret = sdhci_msm_setup_gpio(pdata, enable);
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301206out:
Asutosh Das0ef24812012-12-18 16:14:02 +05301207 if (!ret)
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301208 pdata->pin_cfg_sts = enable;
Asutosh Das0ef24812012-12-18 16:14:02 +05301209
1210 return ret;
1211}
1212
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301213static int sdhci_msm_dt_get_array(struct device *dev, const char *prop_name,
1214 u32 **out, int *len, u32 size)
1215{
1216 int ret = 0;
1217 struct device_node *np = dev->of_node;
1218 size_t sz;
1219 u32 *arr = NULL;
1220
1221 if (!of_get_property(np, prop_name, len)) {
1222 ret = -EINVAL;
1223 goto out;
1224 }
1225 sz = *len = *len / sizeof(*arr);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07001226 if (sz <= 0 || (size > 0 && (sz > size))) {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301227 dev_err(dev, "%s invalid size\n", prop_name);
1228 ret = -EINVAL;
1229 goto out;
1230 }
1231
1232 arr = devm_kzalloc(dev, sz * sizeof(*arr), GFP_KERNEL);
1233 if (!arr) {
1234 dev_err(dev, "%s failed allocating memory\n", prop_name);
1235 ret = -ENOMEM;
1236 goto out;
1237 }
1238
1239 ret = of_property_read_u32_array(np, prop_name, arr, sz);
1240 if (ret < 0) {
1241 dev_err(dev, "%s failed reading array %d\n", prop_name, ret);
1242 goto out;
1243 }
1244 *out = arr;
1245out:
1246 if (ret)
1247 *len = 0;
1248 return ret;
1249}
1250
Asutosh Das0ef24812012-12-18 16:14:02 +05301251#define MAX_PROP_SIZE 32
1252static int sdhci_msm_dt_parse_vreg_info(struct device *dev,
1253 struct sdhci_msm_reg_data **vreg_data, const char *vreg_name)
1254{
1255 int len, ret = 0;
1256 const __be32 *prop;
1257 char prop_name[MAX_PROP_SIZE];
1258 struct sdhci_msm_reg_data *vreg;
1259 struct device_node *np = dev->of_node;
1260
1261 snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", vreg_name);
1262 if (!of_parse_phandle(np, prop_name, 0)) {
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301263 dev_info(dev, "No vreg data found for %s\n", vreg_name);
Asutosh Das0ef24812012-12-18 16:14:02 +05301264 return ret;
1265 }
1266
1267 vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
1268 if (!vreg) {
1269 dev_err(dev, "No memory for vreg: %s\n", vreg_name);
1270 ret = -ENOMEM;
1271 return ret;
1272 }
1273
1274 vreg->name = vreg_name;
1275
1276 snprintf(prop_name, MAX_PROP_SIZE,
1277 "qcom,%s-always-on", vreg_name);
1278 if (of_get_property(np, prop_name, NULL))
1279 vreg->is_always_on = true;
1280
1281 snprintf(prop_name, MAX_PROP_SIZE,
1282 "qcom,%s-lpm-sup", vreg_name);
1283 if (of_get_property(np, prop_name, NULL))
1284 vreg->lpm_sup = true;
1285
1286 snprintf(prop_name, MAX_PROP_SIZE,
1287 "qcom,%s-voltage-level", vreg_name);
1288 prop = of_get_property(np, prop_name, &len);
1289 if (!prop || (len != (2 * sizeof(__be32)))) {
1290 dev_warn(dev, "%s %s property\n",
1291 prop ? "invalid format" : "no", prop_name);
1292 } else {
1293 vreg->low_vol_level = be32_to_cpup(&prop[0]);
1294 vreg->high_vol_level = be32_to_cpup(&prop[1]);
1295 }
1296
1297 snprintf(prop_name, MAX_PROP_SIZE,
1298 "qcom,%s-current-level", vreg_name);
1299 prop = of_get_property(np, prop_name, &len);
1300 if (!prop || (len != (2 * sizeof(__be32)))) {
1301 dev_warn(dev, "%s %s property\n",
1302 prop ? "invalid format" : "no", prop_name);
1303 } else {
1304 vreg->lpm_uA = be32_to_cpup(&prop[0]);
1305 vreg->hpm_uA = be32_to_cpup(&prop[1]);
1306 }
1307
1308 *vreg_data = vreg;
1309 dev_dbg(dev, "%s: %s %s vol=[%d %d]uV, curr=[%d %d]uA\n",
1310 vreg->name, vreg->is_always_on ? "always_on," : "",
1311 vreg->lpm_sup ? "lpm_sup," : "", vreg->low_vol_level,
1312 vreg->high_vol_level, vreg->lpm_uA, vreg->hpm_uA);
1313
1314 return ret;
1315}
1316
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301317static int sdhci_msm_parse_pinctrl_info(struct device *dev,
1318 struct sdhci_msm_pltfm_data *pdata)
1319{
1320 struct sdhci_pinctrl_data *pctrl_data;
1321 struct pinctrl *pctrl;
1322 int ret = 0;
1323
1324 /* Try to obtain pinctrl handle */
1325 pctrl = devm_pinctrl_get(dev);
1326 if (IS_ERR(pctrl)) {
1327 ret = PTR_ERR(pctrl);
1328 goto out;
1329 }
1330 pctrl_data = devm_kzalloc(dev, sizeof(*pctrl_data), GFP_KERNEL);
1331 if (!pctrl_data) {
1332 dev_err(dev, "No memory for sdhci_pinctrl_data\n");
1333 ret = -ENOMEM;
1334 goto out;
1335 }
1336 pctrl_data->pctrl = pctrl;
1337 /* Look-up and keep the states handy to be used later */
1338 pctrl_data->pins_active = pinctrl_lookup_state(
1339 pctrl_data->pctrl, "active");
1340 if (IS_ERR(pctrl_data->pins_active)) {
1341 ret = PTR_ERR(pctrl_data->pins_active);
1342 dev_err(dev, "Could not get active pinstates, err:%d\n", ret);
1343 goto out;
1344 }
1345 pctrl_data->pins_sleep = pinctrl_lookup_state(
1346 pctrl_data->pctrl, "sleep");
1347 if (IS_ERR(pctrl_data->pins_sleep)) {
1348 ret = PTR_ERR(pctrl_data->pins_sleep);
1349 dev_err(dev, "Could not get sleep pinstates, err:%d\n", ret);
1350 goto out;
1351 }
1352 pdata->pctrl_data = pctrl_data;
1353out:
1354 return ret;
1355}
1356
Asutosh Das0ef24812012-12-18 16:14:02 +05301357#define GPIO_NAME_MAX_LEN 32
1358static int sdhci_msm_dt_parse_gpio_info(struct device *dev,
1359 struct sdhci_msm_pltfm_data *pdata)
1360{
1361 int ret = 0, cnt, i;
1362 struct sdhci_msm_pin_data *pin_data;
1363 struct device_node *np = dev->of_node;
1364
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301365 ret = sdhci_msm_parse_pinctrl_info(dev, pdata);
1366 if (!ret) {
1367 goto out;
1368 } else if (ret == -EPROBE_DEFER) {
1369 dev_err(dev, "Pinctrl framework not registered, err:%d\n", ret);
1370 goto out;
1371 } else {
1372 dev_err(dev, "Parsing Pinctrl failed with %d, falling back on GPIO lib\n",
1373 ret);
Sahitya Tummala1cd7e072014-02-14 13:19:01 +05301374 ret = 0;
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301375 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301376 pin_data = devm_kzalloc(dev, sizeof(*pin_data), GFP_KERNEL);
1377 if (!pin_data) {
1378 dev_err(dev, "No memory for pin_data\n");
1379 ret = -ENOMEM;
1380 goto out;
1381 }
1382
1383 cnt = of_gpio_count(np);
1384 if (cnt > 0) {
1385 pin_data->gpio_data = devm_kzalloc(dev,
1386 sizeof(struct sdhci_msm_gpio_data), GFP_KERNEL);
1387 if (!pin_data->gpio_data) {
1388 dev_err(dev, "No memory for gpio_data\n");
1389 ret = -ENOMEM;
1390 goto out;
1391 }
1392 pin_data->gpio_data->size = cnt;
1393 pin_data->gpio_data->gpio = devm_kzalloc(dev, cnt *
1394 sizeof(struct sdhci_msm_gpio), GFP_KERNEL);
1395
1396 if (!pin_data->gpio_data->gpio) {
1397 dev_err(dev, "No memory for gpio\n");
1398 ret = -ENOMEM;
1399 goto out;
1400 }
1401
1402 for (i = 0; i < cnt; i++) {
1403 const char *name = NULL;
1404 char result[GPIO_NAME_MAX_LEN];
1405 pin_data->gpio_data->gpio[i].no = of_get_gpio(np, i);
1406 of_property_read_string_index(np,
1407 "qcom,gpio-names", i, &name);
1408
1409 snprintf(result, GPIO_NAME_MAX_LEN, "%s-%s",
1410 dev_name(dev), name ? name : "?");
1411 pin_data->gpio_data->gpio[i].name = result;
1412 dev_dbg(dev, "%s: gpio[%s] = %d\n", __func__,
1413 pin_data->gpio_data->gpio[i].name,
1414 pin_data->gpio_data->gpio[i].no);
Asutosh Das0ef24812012-12-18 16:14:02 +05301415 }
1416 }
Sahitya Tummala1cd7e072014-02-14 13:19:01 +05301417 pdata->pin_data = pin_data;
Asutosh Das0ef24812012-12-18 16:14:02 +05301418out:
1419 if (ret)
1420 dev_err(dev, "%s failed with err %d\n", __func__, ret);
1421 return ret;
1422}
1423
Maya Erez994cf2f2014-10-21 20:22:04 +03001424#ifdef CONFIG_SMP
1425static void sdhci_msm_populate_affinity_type(struct sdhci_msm_pltfm_data *pdata,
1426 struct device_node *np)
1427{
1428 const char *cpu_affinity = NULL;
1429
1430 pdata->cpu_affinity_type = PM_QOS_REQ_AFFINE_IRQ;
1431 if (!of_property_read_string(np, "qcom,cpu-affinity",
1432 &cpu_affinity)) {
1433 if (!strcmp(cpu_affinity, "all_cores"))
1434 pdata->cpu_affinity_type = PM_QOS_REQ_ALL_CORES;
1435 else if (!strcmp(cpu_affinity, "affine_cores"))
1436 pdata->cpu_affinity_type = PM_QOS_REQ_AFFINE_CORES;
1437 else if (!strcmp(cpu_affinity, "affine_irq"))
1438 pdata->cpu_affinity_type = PM_QOS_REQ_AFFINE_IRQ;
1439 }
1440}
1441#else
1442static void sdhci_msm_populate_affinity_type(struct sdhci_msm_pltfm_data *pdata,
1443 struct device_node *np)
1444{
1445}
1446#endif
1447
Asutosh Das0ef24812012-12-18 16:14:02 +05301448/* Parse platform data */
Dov Levenglickc9033ab2015-03-10 16:00:56 +02001449static
1450struct sdhci_msm_pltfm_data *sdhci_msm_populate_pdata(struct device *dev,
1451 struct sdhci_msm_host *msm_host)
Asutosh Das0ef24812012-12-18 16:14:02 +05301452{
1453 struct sdhci_msm_pltfm_data *pdata = NULL;
1454 struct device_node *np = dev->of_node;
1455 u32 bus_width = 0;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05301456 u32 cpu_dma_latency;
Asutosh Das0ef24812012-12-18 16:14:02 +05301457 int len, i;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301458 int clk_table_len;
1459 u32 *clk_table = NULL;
Sujit Reddy Thumma1958d3d2013-06-03 09:54:32 +05301460 enum of_gpio_flags flags = OF_GPIO_ACTIVE_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05301461
1462 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1463 if (!pdata) {
1464 dev_err(dev, "failed to allocate memory for platform data\n");
1465 goto out;
1466 }
1467
Sujit Reddy Thumma1958d3d2013-06-03 09:54:32 +05301468 pdata->status_gpio = of_get_named_gpio_flags(np, "cd-gpios", 0, &flags);
1469 if (gpio_is_valid(pdata->status_gpio) & !(flags & OF_GPIO_ACTIVE_LOW))
1470 pdata->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
Sahitya Tummala581df132013-03-12 14:57:46 +05301471
Asutosh Das0ef24812012-12-18 16:14:02 +05301472 of_property_read_u32(np, "qcom,bus-width", &bus_width);
1473 if (bus_width == 8)
1474 pdata->mmc_bus_width = MMC_CAP_8_BIT_DATA;
1475 else if (bus_width == 4)
1476 pdata->mmc_bus_width = MMC_CAP_4_BIT_DATA;
1477 else {
1478 dev_notice(dev, "invalid bus-width, default to 1-bit mode\n");
1479 pdata->mmc_bus_width = 0;
1480 }
1481
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05301482 if (!of_property_read_u32(np, "qcom,cpu-dma-latency-us",
1483 &cpu_dma_latency))
1484 pdata->cpu_dma_latency_us = cpu_dma_latency;
Asutosh Das598a4d42014-02-05 16:38:23 +05301485 else
1486 pdata->cpu_dma_latency_us = MSM_MMC_DEFAULT_CPU_DMA_LATENCY;
Talel Shenhar7dc5f792015-05-18 12:12:48 +03001487
1488 if (sdhci_msm_dt_get_array(dev, "qcom,devfreq,freq-table",
1489 &msm_host->mmc->clk_scaling.freq_table,
1490 &msm_host->mmc->clk_scaling.freq_table_sz, 0))
1491 pr_debug("%s: no clock scaling frequencies were supplied\n",
1492 dev_name(dev));
1493 else if (!msm_host->mmc->clk_scaling.freq_table ||
1494 !msm_host->mmc->clk_scaling.freq_table_sz)
1495 dev_err(dev, "bad dts clock scaling frequencies\n");
1496
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301497 if (sdhci_msm_dt_get_array(dev, "qcom,clk-rates",
1498 &clk_table, &clk_table_len, 0)) {
1499 dev_err(dev, "failed parsing supported clock rates\n");
1500 goto out;
1501 }
1502 if (!clk_table || !clk_table_len) {
1503 dev_err(dev, "Invalid clock table\n");
1504 goto out;
1505 }
1506 pdata->sup_clk_table = clk_table;
1507 pdata->sup_clk_cnt = clk_table_len;
1508
Asutosh Das0ef24812012-12-18 16:14:02 +05301509 pdata->vreg_data = devm_kzalloc(dev, sizeof(struct
1510 sdhci_msm_slot_reg_data),
1511 GFP_KERNEL);
1512 if (!pdata->vreg_data) {
1513 dev_err(dev, "failed to allocate memory for vreg data\n");
1514 goto out;
1515 }
1516
1517 if (sdhci_msm_dt_parse_vreg_info(dev, &pdata->vreg_data->vdd_data,
1518 "vdd")) {
1519 dev_err(dev, "failed parsing vdd data\n");
1520 goto out;
1521 }
1522 if (sdhci_msm_dt_parse_vreg_info(dev,
1523 &pdata->vreg_data->vdd_io_data,
1524 "vdd-io")) {
1525 dev_err(dev, "failed parsing vdd-io data\n");
1526 goto out;
1527 }
1528
1529 if (sdhci_msm_dt_parse_gpio_info(dev, pdata)) {
1530 dev_err(dev, "failed parsing gpio data\n");
1531 goto out;
1532 }
1533
Asutosh Das0ef24812012-12-18 16:14:02 +05301534 len = of_property_count_strings(np, "qcom,bus-speed-mode");
1535
1536 for (i = 0; i < len; i++) {
1537 const char *name = NULL;
1538
1539 of_property_read_string_index(np,
1540 "qcom,bus-speed-mode", i, &name);
1541 if (!name)
1542 continue;
1543
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07001544 if (!strncmp(name, "HS400_1p8v", sizeof("HS400_1p8v")))
1545 pdata->caps2 |= MMC_CAP2_HS400_1_8V;
1546 else if (!strncmp(name, "HS400_1p2v", sizeof("HS400_1p2v")))
1547 pdata->caps2 |= MMC_CAP2_HS400_1_2V;
1548 else if (!strncmp(name, "HS200_1p8v", sizeof("HS200_1p8v")))
Asutosh Das0ef24812012-12-18 16:14:02 +05301549 pdata->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
1550 else if (!strncmp(name, "HS200_1p2v", sizeof("HS200_1p2v")))
1551 pdata->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
1552 else if (!strncmp(name, "DDR_1p8v", sizeof("DDR_1p8v")))
1553 pdata->caps |= MMC_CAP_1_8V_DDR
1554 | MMC_CAP_UHS_DDR50;
1555 else if (!strncmp(name, "DDR_1p2v", sizeof("DDR_1p2v")))
1556 pdata->caps |= MMC_CAP_1_2V_DDR
1557 | MMC_CAP_UHS_DDR50;
1558 }
1559
1560 if (of_get_property(np, "qcom,nonremovable", NULL))
1561 pdata->nonremovable = true;
1562
Guoping Yuf7c91332014-08-20 16:56:18 +08001563 if (of_get_property(np, "qcom,nonhotplug", NULL))
1564 pdata->nonhotplug = true;
1565
Venkat Gopalakrishnan9a62e042015-03-03 16:14:55 -08001566 pdata->largeaddressbus =
1567 of_property_read_bool(np, "qcom,large-address-bus");
1568
Maya Erez994cf2f2014-10-21 20:22:04 +03001569 sdhci_msm_populate_affinity_type(pdata, np);
1570
Dov Levenglickc9033ab2015-03-10 16:00:56 +02001571 if (of_property_read_bool(np, "qcom,wakeup-on-idle"))
1572 msm_host->mmc->wakeup_on_idle = true;
1573
Asutosh Das0ef24812012-12-18 16:14:02 +05301574 return pdata;
1575out:
1576 return NULL;
1577}
1578
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301579/* Returns required bandwidth in Bytes per Sec */
1580static unsigned int sdhci_get_bw_required(struct sdhci_host *host,
1581 struct mmc_ios *ios)
1582{
Sahitya Tummala2886c922013-04-03 18:03:31 +05301583 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1584 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1585
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301586 unsigned int bw;
1587
Sahitya Tummala2886c922013-04-03 18:03:31 +05301588 bw = msm_host->clk_rate;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301589 /*
1590 * For DDR mode, SDCC controller clock will be at
1591 * the double rate than the actual clock that goes to card.
1592 */
1593 if (ios->bus_width == MMC_BUS_WIDTH_4)
1594 bw /= 2;
1595 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1596 bw /= 8;
1597
1598 return bw;
1599}
1600
1601static int sdhci_msm_bus_get_vote_for_bw(struct sdhci_msm_host *host,
1602 unsigned int bw)
1603{
1604 unsigned int *table = host->pdata->voting_data->bw_vecs;
1605 unsigned int size = host->pdata->voting_data->bw_vecs_size;
1606 int i;
1607
1608 if (host->msm_bus_vote.is_max_bw_needed && bw)
1609 return host->msm_bus_vote.max_bw_vote;
1610
1611 for (i = 0; i < size; i++) {
1612 if (bw <= table[i])
1613 break;
1614 }
1615
1616 if (i && (i == size))
1617 i--;
1618
1619 return i;
1620}
1621
1622/*
1623 * This function must be called with host lock acquired.
1624 * Caller of this function should also ensure that msm bus client
1625 * handle is not null.
1626 */
1627static inline int sdhci_msm_bus_set_vote(struct sdhci_msm_host *msm_host,
1628 int vote,
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301629 unsigned long *flags)
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301630{
1631 struct sdhci_host *host = platform_get_drvdata(msm_host->pdev);
1632 int rc = 0;
1633
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301634 BUG_ON(!flags);
1635
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301636 if (vote != msm_host->msm_bus_vote.curr_vote) {
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301637 spin_unlock_irqrestore(&host->lock, *flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301638 rc = msm_bus_scale_client_update_request(
1639 msm_host->msm_bus_vote.client_handle, vote);
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301640 spin_lock_irqsave(&host->lock, *flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301641 if (rc) {
1642 pr_err("%s: msm_bus_scale_client_update_request() failed: bus_client_handle=0x%x, vote=%d, err=%d\n",
1643 mmc_hostname(host->mmc),
1644 msm_host->msm_bus_vote.client_handle, vote, rc);
1645 goto out;
1646 }
1647 msm_host->msm_bus_vote.curr_vote = vote;
1648 }
1649out:
1650 return rc;
1651}
1652
1653/*
1654 * Internal work. Work to set 0 bandwidth for msm bus.
1655 */
1656static void sdhci_msm_bus_work(struct work_struct *work)
1657{
1658 struct sdhci_msm_host *msm_host;
1659 struct sdhci_host *host;
1660 unsigned long flags;
1661
1662 msm_host = container_of(work, struct sdhci_msm_host,
1663 msm_bus_vote.vote_work.work);
1664 host = platform_get_drvdata(msm_host->pdev);
1665
1666 if (!msm_host->msm_bus_vote.client_handle)
1667 return;
1668
1669 spin_lock_irqsave(&host->lock, flags);
1670 /* don't vote for 0 bandwidth if any request is in progress */
1671 if (!host->mrq) {
1672 sdhci_msm_bus_set_vote(msm_host,
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301673 msm_host->msm_bus_vote.min_bw_vote, &flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301674 } else
1675 pr_warning("%s: %s: Transfer in progress. skipping bus voting to 0 bandwidth\n",
1676 mmc_hostname(host->mmc), __func__);
1677 spin_unlock_irqrestore(&host->lock, flags);
1678}
1679
1680/*
1681 * This function cancels any scheduled delayed work and sets the bus
1682 * vote based on bw (bandwidth) argument.
1683 */
1684static void sdhci_msm_bus_cancel_work_and_set_vote(struct sdhci_host *host,
1685 unsigned int bw)
1686{
1687 int vote;
1688 unsigned long flags;
1689 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1690 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1691
1692 cancel_delayed_work_sync(&msm_host->msm_bus_vote.vote_work);
1693 spin_lock_irqsave(&host->lock, flags);
1694 vote = sdhci_msm_bus_get_vote_for_bw(msm_host, bw);
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301695 sdhci_msm_bus_set_vote(msm_host, vote, &flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301696 spin_unlock_irqrestore(&host->lock, flags);
1697}
1698
1699#define MSM_MMC_BUS_VOTING_DELAY 200 /* msecs */
1700
1701/* This function queues a work which will set the bandwidth requiement to 0 */
1702static void sdhci_msm_bus_queue_work(struct sdhci_host *host)
1703{
1704 unsigned long flags;
1705 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1706 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1707
1708 spin_lock_irqsave(&host->lock, flags);
1709 if (msm_host->msm_bus_vote.min_bw_vote !=
1710 msm_host->msm_bus_vote.curr_vote)
1711 queue_delayed_work(system_wq,
1712 &msm_host->msm_bus_vote.vote_work,
1713 msecs_to_jiffies(MSM_MMC_BUS_VOTING_DELAY));
1714 spin_unlock_irqrestore(&host->lock, flags);
1715}
1716
1717static int sdhci_msm_bus_register(struct sdhci_msm_host *host,
1718 struct platform_device *pdev)
1719{
1720 int rc = 0;
1721 struct msm_bus_scale_pdata *bus_pdata;
1722
1723 struct sdhci_msm_bus_voting_data *data;
1724 struct device *dev = &pdev->dev;
1725
1726 data = devm_kzalloc(dev,
1727 sizeof(struct sdhci_msm_bus_voting_data), GFP_KERNEL);
1728 if (!data) {
1729 dev_err(&pdev->dev,
1730 "%s: failed to allocate memory\n", __func__);
1731 rc = -ENOMEM;
1732 goto out;
1733 }
1734 data->bus_pdata = msm_bus_cl_get_pdata(pdev);
1735 if (data->bus_pdata) {
1736 rc = sdhci_msm_dt_get_array(dev, "qcom,bus-bw-vectors-bps",
1737 &data->bw_vecs, &data->bw_vecs_size, 0);
1738 if (rc) {
1739 dev_err(&pdev->dev,
1740 "%s: Failed to get bus-bw-vectors-bps\n",
1741 __func__);
1742 goto out;
1743 }
1744 host->pdata->voting_data = data;
1745 }
1746 if (host->pdata->voting_data &&
1747 host->pdata->voting_data->bus_pdata &&
1748 host->pdata->voting_data->bw_vecs &&
1749 host->pdata->voting_data->bw_vecs_size) {
1750
1751 bus_pdata = host->pdata->voting_data->bus_pdata;
1752 host->msm_bus_vote.client_handle =
1753 msm_bus_scale_register_client(bus_pdata);
1754 if (!host->msm_bus_vote.client_handle) {
1755 dev_err(&pdev->dev, "msm_bus_scale_register_client()\n");
1756 rc = -EFAULT;
1757 goto out;
1758 }
1759 /* cache the vote index for minimum and maximum bandwidth */
1760 host->msm_bus_vote.min_bw_vote =
1761 sdhci_msm_bus_get_vote_for_bw(host, 0);
1762 host->msm_bus_vote.max_bw_vote =
1763 sdhci_msm_bus_get_vote_for_bw(host, UINT_MAX);
1764 } else {
1765 devm_kfree(dev, data);
1766 }
1767
1768out:
1769 return rc;
1770}
1771
1772static void sdhci_msm_bus_unregister(struct sdhci_msm_host *host)
1773{
1774 if (host->msm_bus_vote.client_handle)
1775 msm_bus_scale_unregister_client(
1776 host->msm_bus_vote.client_handle);
1777}
1778
1779static void sdhci_msm_bus_voting(struct sdhci_host *host, u32 enable)
1780{
1781 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1782 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1783 struct mmc_ios *ios = &host->mmc->ios;
1784 unsigned int bw;
1785
1786 if (!msm_host->msm_bus_vote.client_handle)
1787 return;
1788
1789 bw = sdhci_get_bw_required(host, ios);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301790 if (enable) {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301791 sdhci_msm_bus_cancel_work_and_set_vote(host, bw);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301792 } else {
1793 /*
1794 * If clock gating is enabled, then remove the vote
1795 * immediately because clocks will be disabled only
1796 * after SDHCI_MSM_MMC_CLK_GATE_DELAY and thus no
1797 * additional delay is required to remove the bus vote.
1798 */
1799#ifdef CONFIG_MMC_CLKGATE
1800 if (host->mmc->clkgate_delay)
1801 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
1802 else
1803#endif
1804 sdhci_msm_bus_queue_work(host);
1805 }
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301806}
1807
Asutosh Das0ef24812012-12-18 16:14:02 +05301808/* Regulator utility functions */
1809static int sdhci_msm_vreg_init_reg(struct device *dev,
1810 struct sdhci_msm_reg_data *vreg)
1811{
1812 int ret = 0;
1813
1814 /* check if regulator is already initialized? */
1815 if (vreg->reg)
1816 goto out;
1817
1818 /* Get the regulator handle */
1819 vreg->reg = devm_regulator_get(dev, vreg->name);
1820 if (IS_ERR(vreg->reg)) {
1821 ret = PTR_ERR(vreg->reg);
1822 pr_err("%s: devm_regulator_get(%s) failed. ret=%d\n",
1823 __func__, vreg->name, ret);
1824 goto out;
1825 }
1826
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301827 if (regulator_count_voltages(vreg->reg) > 0) {
1828 vreg->set_voltage_sup = true;
1829 /* sanity check */
1830 if (!vreg->high_vol_level || !vreg->hpm_uA) {
1831 pr_err("%s: %s invalid constraints specified\n",
1832 __func__, vreg->name);
1833 ret = -EINVAL;
1834 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301835 }
1836
1837out:
1838 return ret;
1839}
1840
1841static void sdhci_msm_vreg_deinit_reg(struct sdhci_msm_reg_data *vreg)
1842{
1843 if (vreg->reg)
1844 devm_regulator_put(vreg->reg);
1845}
1846
1847static int sdhci_msm_vreg_set_optimum_mode(struct sdhci_msm_reg_data
1848 *vreg, int uA_load)
1849{
1850 int ret = 0;
1851
1852 /*
1853 * regulators that do not support regulator_set_voltage also
1854 * do not support regulator_set_optimum_mode
1855 */
1856 if (vreg->set_voltage_sup) {
1857 ret = regulator_set_load(vreg->reg, uA_load);
1858 if (ret < 0)
1859 pr_err("%s: regulator_set_load(reg=%s,uA_load=%d) failed. ret=%d\n",
1860 __func__, vreg->name, uA_load, ret);
1861 else
1862 /*
1863 * regulator_set_load() can return non zero
1864 * value even for success case.
1865 */
1866 ret = 0;
1867 }
1868 return ret;
1869}
1870
1871static int sdhci_msm_vreg_set_voltage(struct sdhci_msm_reg_data *vreg,
1872 int min_uV, int max_uV)
1873{
1874 int ret = 0;
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301875 if (vreg->set_voltage_sup) {
1876 ret = regulator_set_voltage(vreg->reg, min_uV, max_uV);
1877 if (ret) {
1878 pr_err("%s: regulator_set_voltage(%s)failed. min_uV=%d,max_uV=%d,ret=%d\n",
Asutosh Das0ef24812012-12-18 16:14:02 +05301879 __func__, vreg->name, min_uV, max_uV, ret);
1880 }
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301881 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301882
1883 return ret;
1884}
1885
1886static int sdhci_msm_vreg_enable(struct sdhci_msm_reg_data *vreg)
1887{
1888 int ret = 0;
1889
1890 /* Put regulator in HPM (high power mode) */
1891 ret = sdhci_msm_vreg_set_optimum_mode(vreg, vreg->hpm_uA);
1892 if (ret < 0)
1893 return ret;
1894
1895 if (!vreg->is_enabled) {
1896 /* Set voltage level */
1897 ret = sdhci_msm_vreg_set_voltage(vreg, vreg->high_vol_level,
1898 vreg->high_vol_level);
1899 if (ret)
1900 return ret;
1901 }
1902 ret = regulator_enable(vreg->reg);
1903 if (ret) {
1904 pr_err("%s: regulator_enable(%s) failed. ret=%d\n",
1905 __func__, vreg->name, ret);
1906 return ret;
1907 }
1908 vreg->is_enabled = true;
1909 return ret;
1910}
1911
1912static int sdhci_msm_vreg_disable(struct sdhci_msm_reg_data *vreg)
1913{
1914 int ret = 0;
1915
1916 /* Never disable regulator marked as always_on */
1917 if (vreg->is_enabled && !vreg->is_always_on) {
1918 ret = regulator_disable(vreg->reg);
1919 if (ret) {
1920 pr_err("%s: regulator_disable(%s) failed. ret=%d\n",
1921 __func__, vreg->name, ret);
1922 goto out;
1923 }
1924 vreg->is_enabled = false;
1925
1926 ret = sdhci_msm_vreg_set_optimum_mode(vreg, 0);
1927 if (ret < 0)
1928 goto out;
1929
1930 /* Set min. voltage level to 0 */
1931 ret = sdhci_msm_vreg_set_voltage(vreg, 0, vreg->high_vol_level);
1932 if (ret)
1933 goto out;
1934 } else if (vreg->is_enabled && vreg->is_always_on) {
1935 if (vreg->lpm_sup) {
1936 /* Put always_on regulator in LPM (low power mode) */
1937 ret = sdhci_msm_vreg_set_optimum_mode(vreg,
1938 vreg->lpm_uA);
1939 if (ret < 0)
1940 goto out;
1941 }
1942 }
1943out:
1944 return ret;
1945}
1946
1947static int sdhci_msm_setup_vreg(struct sdhci_msm_pltfm_data *pdata,
1948 bool enable, bool is_init)
1949{
1950 int ret = 0, i;
1951 struct sdhci_msm_slot_reg_data *curr_slot;
1952 struct sdhci_msm_reg_data *vreg_table[2];
1953
1954 curr_slot = pdata->vreg_data;
1955 if (!curr_slot) {
1956 pr_debug("%s: vreg info unavailable,assuming the slot is powered by always on domain\n",
1957 __func__);
1958 goto out;
1959 }
1960
1961 vreg_table[0] = curr_slot->vdd_data;
1962 vreg_table[1] = curr_slot->vdd_io_data;
1963
1964 for (i = 0; i < ARRAY_SIZE(vreg_table); i++) {
1965 if (vreg_table[i]) {
1966 if (enable)
1967 ret = sdhci_msm_vreg_enable(vreg_table[i]);
1968 else
1969 ret = sdhci_msm_vreg_disable(vreg_table[i]);
1970 if (ret)
1971 goto out;
1972 }
1973 }
1974out:
1975 return ret;
1976}
1977
1978/*
1979 * Reset vreg by ensuring it is off during probe. A call
1980 * to enable vreg is needed to balance disable vreg
1981 */
1982static int sdhci_msm_vreg_reset(struct sdhci_msm_pltfm_data *pdata)
1983{
1984 int ret;
1985
1986 ret = sdhci_msm_setup_vreg(pdata, 1, true);
1987 if (ret)
1988 return ret;
1989 ret = sdhci_msm_setup_vreg(pdata, 0, true);
1990 return ret;
1991}
1992
1993/* This init function should be called only once for each SDHC slot */
1994static int sdhci_msm_vreg_init(struct device *dev,
1995 struct sdhci_msm_pltfm_data *pdata,
1996 bool is_init)
1997{
1998 int ret = 0;
1999 struct sdhci_msm_slot_reg_data *curr_slot;
2000 struct sdhci_msm_reg_data *curr_vdd_reg, *curr_vdd_io_reg;
2001
2002 curr_slot = pdata->vreg_data;
2003 if (!curr_slot)
2004 goto out;
2005
2006 curr_vdd_reg = curr_slot->vdd_data;
2007 curr_vdd_io_reg = curr_slot->vdd_io_data;
2008
2009 if (!is_init)
2010 /* Deregister all regulators from regulator framework */
2011 goto vdd_io_reg_deinit;
2012
2013 /*
2014 * Get the regulator handle from voltage regulator framework
2015 * and then try to set the voltage level for the regulator
2016 */
2017 if (curr_vdd_reg) {
2018 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_reg);
2019 if (ret)
2020 goto out;
2021 }
2022 if (curr_vdd_io_reg) {
2023 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_io_reg);
2024 if (ret)
2025 goto vdd_reg_deinit;
2026 }
2027 ret = sdhci_msm_vreg_reset(pdata);
2028 if (ret)
2029 dev_err(dev, "vreg reset failed (%d)\n", ret);
2030 goto out;
2031
2032vdd_io_reg_deinit:
2033 if (curr_vdd_io_reg)
2034 sdhci_msm_vreg_deinit_reg(curr_vdd_io_reg);
2035vdd_reg_deinit:
2036 if (curr_vdd_reg)
2037 sdhci_msm_vreg_deinit_reg(curr_vdd_reg);
2038out:
2039 return ret;
2040}
2041
2042
2043static int sdhci_msm_set_vdd_io_vol(struct sdhci_msm_pltfm_data *pdata,
2044 enum vdd_io_level level,
2045 unsigned int voltage_level)
2046{
2047 int ret = 0;
2048 int set_level;
2049 struct sdhci_msm_reg_data *vdd_io_reg;
2050
2051 if (!pdata->vreg_data)
2052 return ret;
2053
2054 vdd_io_reg = pdata->vreg_data->vdd_io_data;
2055 if (vdd_io_reg && vdd_io_reg->is_enabled) {
2056 switch (level) {
2057 case VDD_IO_LOW:
2058 set_level = vdd_io_reg->low_vol_level;
2059 break;
2060 case VDD_IO_HIGH:
2061 set_level = vdd_io_reg->high_vol_level;
2062 break;
2063 case VDD_IO_SET_LEVEL:
2064 set_level = voltage_level;
2065 break;
2066 default:
2067 pr_err("%s: invalid argument level = %d",
2068 __func__, level);
2069 ret = -EINVAL;
2070 return ret;
2071 }
2072 ret = sdhci_msm_vreg_set_voltage(vdd_io_reg, set_level,
2073 set_level);
2074 }
2075 return ret;
2076}
2077
Pavan Anamula53ffa0b2015-07-22 21:46:32 +05302078void sdhci_msm_dump_pwr_ctrl_regs(struct sdhci_host *host)
2079{
2080 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2081 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2082
2083 pr_err("%s: PWRCTL_STATUS: 0x%08x | PWRCTL_MASK: 0x%08x | PWRCTL_CTL: 0x%08x\n",
2084 mmc_hostname(host->mmc),
2085 readl_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS),
2086 readl_relaxed(msm_host->core_mem + CORE_PWRCTL_MASK),
2087 readl_relaxed(msm_host->core_mem + CORE_PWRCTL_CTL));
2088}
2089
Asutosh Das0ef24812012-12-18 16:14:02 +05302090static irqreturn_t sdhci_msm_pwr_irq(int irq, void *data)
2091{
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002092 struct sdhci_host *host = (struct sdhci_host *)data;
2093 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2094 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Das0ef24812012-12-18 16:14:02 +05302095 u8 irq_status = 0;
2096 u8 irq_ack = 0;
2097 int ret = 0;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302098 int pwr_state = 0, io_level = 0;
2099 unsigned long flags;
Pavan Anamula53ffa0b2015-07-22 21:46:32 +05302100 int retry = 10;
Asutosh Das0ef24812012-12-18 16:14:02 +05302101
2102 irq_status = readb_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
2103 pr_debug("%s: Received IRQ(%d), status=0x%x\n",
2104 mmc_hostname(msm_host->mmc), irq, irq_status);
2105
2106 /* Clear the interrupt */
2107 writeb_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
2108 /*
2109 * SDHC has core_mem and hc_mem device memory and these memory
2110 * addresses do not fall within 1KB region. Hence, any update to
2111 * core_mem address space would require an mb() to ensure this gets
2112 * completed before its next update to registers within hc_mem.
2113 */
2114 mb();
Pavan Anamula53ffa0b2015-07-22 21:46:32 +05302115 /*
2116 * There is a rare HW scenario where the first clear pulse could be
2117 * lost when actual reset and clear/read of status register is
2118 * happening at a time. Hence, retry for at least 10 times to make
2119 * sure status register is cleared. Otherwise, this will result in
2120 * a spurious power IRQ resulting in system instability.
2121 */
2122 while (irq_status &
2123 readb_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS)) {
2124 if (retry == 0) {
2125 pr_err("%s: Timedout clearing (0x%x) pwrctl status register\n",
2126 mmc_hostname(host->mmc), irq_status);
2127 sdhci_msm_dump_pwr_ctrl_regs(host);
2128 BUG_ON(1);
2129 }
2130 writeb_relaxed(irq_status,
2131 (msm_host->core_mem + CORE_PWRCTL_CLEAR));
2132 retry--;
2133 udelay(10);
2134 }
2135 if (likely(retry < 10))
2136 pr_debug("%s: success clearing (0x%x) pwrctl status register, retries left %d\n",
2137 mmc_hostname(host->mmc), irq_status, retry);
Asutosh Das0ef24812012-12-18 16:14:02 +05302138
2139 /* Handle BUS ON/OFF*/
2140 if (irq_status & CORE_PWRCTL_BUS_ON) {
2141 ret = sdhci_msm_setup_vreg(msm_host->pdata, true, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302142 if (!ret) {
Asutosh Das0ef24812012-12-18 16:14:02 +05302143 ret = sdhci_msm_setup_pins(msm_host->pdata, true);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302144 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
2145 VDD_IO_HIGH, 0);
2146 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302147 if (ret)
2148 irq_ack |= CORE_PWRCTL_BUS_FAIL;
2149 else
2150 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302151
2152 pwr_state = REQ_BUS_ON;
2153 io_level = REQ_IO_HIGH;
Asutosh Das0ef24812012-12-18 16:14:02 +05302154 }
2155 if (irq_status & CORE_PWRCTL_BUS_OFF) {
2156 ret = sdhci_msm_setup_vreg(msm_host->pdata, false, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302157 if (!ret) {
Asutosh Das0ef24812012-12-18 16:14:02 +05302158 ret = sdhci_msm_setup_pins(msm_host->pdata, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302159 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
2160 VDD_IO_LOW, 0);
2161 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302162 if (ret)
2163 irq_ack |= CORE_PWRCTL_BUS_FAIL;
2164 else
2165 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302166
2167 pwr_state = REQ_BUS_OFF;
2168 io_level = REQ_IO_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05302169 }
2170 /* Handle IO LOW/HIGH */
2171 if (irq_status & CORE_PWRCTL_IO_LOW) {
2172 /* Switch voltage Low */
2173 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_LOW, 0);
2174 if (ret)
2175 irq_ack |= CORE_PWRCTL_IO_FAIL;
2176 else
2177 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302178
2179 io_level = REQ_IO_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05302180 }
2181 if (irq_status & CORE_PWRCTL_IO_HIGH) {
2182 /* Switch voltage High */
2183 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_HIGH, 0);
2184 if (ret)
2185 irq_ack |= CORE_PWRCTL_IO_FAIL;
2186 else
2187 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302188
2189 io_level = REQ_IO_HIGH;
Asutosh Das0ef24812012-12-18 16:14:02 +05302190 }
2191
2192 /* ACK status to the core */
2193 writeb_relaxed(irq_ack, (msm_host->core_mem + CORE_PWRCTL_CTL));
2194 /*
2195 * SDHC has core_mem and hc_mem device memory and these memory
2196 * addresses do not fall within 1KB region. Hence, any update to
2197 * core_mem address space would require an mb() to ensure this gets
2198 * completed before its next update to registers within hc_mem.
2199 */
2200 mb();
2201
Krishna Konda46fd1432014-10-30 21:13:27 -07002202 if ((io_level & REQ_IO_HIGH) && (msm_host->caps_0 & CORE_3_0V_SUPPORT))
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002203 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2204 ~CORE_IO_PAD_PWR_SWITCH),
2205 host->ioaddr + CORE_VENDOR_SPEC);
Krishna Konda46fd1432014-10-30 21:13:27 -07002206 else if ((io_level & REQ_IO_LOW) ||
2207 (msm_host->caps_0 & CORE_1_8V_SUPPORT))
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002208 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
2209 CORE_IO_PAD_PWR_SWITCH),
2210 host->ioaddr + CORE_VENDOR_SPEC);
2211 mb();
2212
Asutosh Das0ef24812012-12-18 16:14:02 +05302213 pr_debug("%s: Handled IRQ(%d), ret=%d, ack=0x%x\n",
2214 mmc_hostname(msm_host->mmc), irq, ret, irq_ack);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302215 spin_lock_irqsave(&host->lock, flags);
2216 if (pwr_state)
2217 msm_host->curr_pwr_state = pwr_state;
2218 if (io_level)
2219 msm_host->curr_io_level = io_level;
2220 complete(&msm_host->pwr_irq_completion);
2221 spin_unlock_irqrestore(&host->lock, flags);
2222
Asutosh Das0ef24812012-12-18 16:14:02 +05302223 return IRQ_HANDLED;
2224}
2225
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302226static ssize_t
Sahitya Tummala5c55b932013-06-20 14:00:18 +05302227show_polling(struct device *dev, struct device_attribute *attr, char *buf)
2228{
2229 struct sdhci_host *host = dev_get_drvdata(dev);
2230 int poll;
2231 unsigned long flags;
2232
2233 spin_lock_irqsave(&host->lock, flags);
2234 poll = !!(host->mmc->caps & MMC_CAP_NEEDS_POLL);
2235 spin_unlock_irqrestore(&host->lock, flags);
2236
2237 return snprintf(buf, PAGE_SIZE, "%d\n", poll);
2238}
2239
2240static ssize_t
2241store_polling(struct device *dev, struct device_attribute *attr,
2242 const char *buf, size_t count)
2243{
2244 struct sdhci_host *host = dev_get_drvdata(dev);
2245 int value;
2246 unsigned long flags;
2247
2248 if (!kstrtou32(buf, 0, &value)) {
2249 spin_lock_irqsave(&host->lock, flags);
2250 if (value) {
2251 host->mmc->caps |= MMC_CAP_NEEDS_POLL;
2252 mmc_detect_change(host->mmc, 0);
2253 } else {
2254 host->mmc->caps &= ~MMC_CAP_NEEDS_POLL;
2255 }
2256 spin_unlock_irqrestore(&host->lock, flags);
2257 }
2258 return count;
2259}
2260
2261static ssize_t
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302262show_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
2263 char *buf)
2264{
2265 struct sdhci_host *host = dev_get_drvdata(dev);
2266 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2267 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2268
2269 return snprintf(buf, PAGE_SIZE, "%u\n",
2270 msm_host->msm_bus_vote.is_max_bw_needed);
2271}
2272
2273static ssize_t
2274store_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
2275 const char *buf, size_t count)
2276{
2277 struct sdhci_host *host = dev_get_drvdata(dev);
2278 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2279 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2280 uint32_t value;
2281 unsigned long flags;
2282
2283 if (!kstrtou32(buf, 0, &value)) {
2284 spin_lock_irqsave(&host->lock, flags);
2285 msm_host->msm_bus_vote.is_max_bw_needed = !!value;
2286 spin_unlock_irqrestore(&host->lock, flags);
2287 }
2288 return count;
2289}
2290
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302291static void sdhci_msm_check_power_status(struct sdhci_host *host, u32 req_type)
Asutosh Das0ef24812012-12-18 16:14:02 +05302292{
2293 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2294 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302295 unsigned long flags;
2296 bool done = false;
Sahitya Tummala481fbb02013-08-06 15:22:28 +05302297 u32 io_sig_sts;
Asutosh Das0ef24812012-12-18 16:14:02 +05302298
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302299 spin_lock_irqsave(&host->lock, flags);
2300 pr_debug("%s: %s: request %d curr_pwr_state %x curr_io_level %x\n",
2301 mmc_hostname(host->mmc), __func__, req_type,
2302 msm_host->curr_pwr_state, msm_host->curr_io_level);
Sahitya Tummala481fbb02013-08-06 15:22:28 +05302303 io_sig_sts = readl_relaxed(msm_host->core_mem + CORE_GENERICS);
2304 /*
2305 * The IRQ for request type IO High/Low will be generated when -
2306 * 1. SWITCHABLE_SIGNALLING_VOL is enabled in HW.
2307 * 2. If 1 is true and when there is a state change in 1.8V enable
2308 * bit (bit 3) of SDHCI_HOST_CONTROL2 register. The reset state of
2309 * that bit is 0 which indicates 3.3V IO voltage. So, when MMC core
2310 * layer tries to set it to 3.3V before card detection happens, the
2311 * IRQ doesn't get triggered as there is no state change in this bit.
2312 * The driver already handles this case by changing the IO voltage
2313 * level to high as part of controller power up sequence. Hence, check
2314 * for host->pwr to handle a case where IO voltage high request is
2315 * issued even before controller power up.
2316 */
2317 if (req_type & (REQ_IO_HIGH | REQ_IO_LOW)) {
2318 if (!(io_sig_sts & SWITCHABLE_SIGNALLING_VOL) ||
2319 ((req_type & REQ_IO_HIGH) && !host->pwr)) {
2320 pr_debug("%s: do not wait for power IRQ that never comes\n",
2321 mmc_hostname(host->mmc));
2322 spin_unlock_irqrestore(&host->lock, flags);
2323 return;
2324 }
2325 }
2326
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302327 if ((req_type & msm_host->curr_pwr_state) ||
2328 (req_type & msm_host->curr_io_level))
2329 done = true;
2330 spin_unlock_irqrestore(&host->lock, flags);
Asutosh Das0ef24812012-12-18 16:14:02 +05302331
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302332 /*
2333 * This is needed here to hanlde a case where IRQ gets
2334 * triggered even before this function is called so that
2335 * x->done counter of completion gets reset. Otherwise,
2336 * next call to wait_for_completion returns immediately
2337 * without actually waiting for the IRQ to be handled.
2338 */
2339 if (done)
2340 init_completion(&msm_host->pwr_irq_completion);
2341 else
2342 wait_for_completion(&msm_host->pwr_irq_completion);
2343
2344 pr_debug("%s: %s: request %d done\n", mmc_hostname(host->mmc),
2345 __func__, req_type);
Asutosh Das0ef24812012-12-18 16:14:02 +05302346}
2347
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002348static void sdhci_msm_toggle_cdr(struct sdhci_host *host, bool enable)
2349{
2350 if (enable)
2351 writel_relaxed((readl_relaxed(host->ioaddr +
2352 CORE_DLL_CONFIG) | CORE_CDR_EN),
2353 host->ioaddr + CORE_DLL_CONFIG);
2354 else
2355 writel_relaxed((readl_relaxed(host->ioaddr +
2356 CORE_DLL_CONFIG) & ~CORE_CDR_EN),
2357 host->ioaddr + CORE_DLL_CONFIG);
2358}
2359
Asutosh Das648f9d12013-01-10 21:11:04 +05302360static unsigned int sdhci_msm_max_segs(void)
2361{
2362 return SDHCI_MSM_MAX_SEGMENTS;
2363}
2364
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302365static unsigned int sdhci_msm_get_min_clock(struct sdhci_host *host)
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302366{
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302367 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2368 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302369
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302370 return msm_host->pdata->sup_clk_table[0];
2371}
2372
2373static unsigned int sdhci_msm_get_max_clock(struct sdhci_host *host)
2374{
2375 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2376 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2377 int max_clk_index = msm_host->pdata->sup_clk_cnt;
2378
2379 return msm_host->pdata->sup_clk_table[max_clk_index - 1];
2380}
2381
2382static unsigned int sdhci_msm_get_sup_clk_rate(struct sdhci_host *host,
2383 u32 req_clk)
2384{
2385 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2386 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2387 unsigned int sel_clk = -1;
2388 unsigned char cnt;
2389
2390 if (req_clk < sdhci_msm_get_min_clock(host)) {
2391 sel_clk = sdhci_msm_get_min_clock(host);
2392 return sel_clk;
2393 }
2394
2395 for (cnt = 0; cnt < msm_host->pdata->sup_clk_cnt; cnt++) {
2396 if (msm_host->pdata->sup_clk_table[cnt] > req_clk) {
2397 break;
2398 } else if (msm_host->pdata->sup_clk_table[cnt] == req_clk) {
2399 sel_clk = msm_host->pdata->sup_clk_table[cnt];
2400 break;
2401 } else {
2402 sel_clk = msm_host->pdata->sup_clk_table[cnt];
2403 }
2404 }
2405 return sel_clk;
2406}
2407
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302408static int sdhci_msm_enable_controller_clock(struct sdhci_host *host)
2409{
2410 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2411 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2412 int rc = 0;
2413
2414 if (atomic_read(&msm_host->controller_clock))
2415 return 0;
2416
2417 sdhci_msm_bus_voting(host, 1);
2418
2419 if (!IS_ERR(msm_host->pclk)) {
2420 rc = clk_prepare_enable(msm_host->pclk);
2421 if (rc) {
2422 pr_err("%s: %s: failed to enable the pclk with error %d\n",
2423 mmc_hostname(host->mmc), __func__, rc);
2424 goto remove_vote;
2425 }
2426 }
2427
2428 rc = clk_prepare_enable(msm_host->clk);
2429 if (rc) {
2430 pr_err("%s: %s: failed to enable the host-clk with error %d\n",
2431 mmc_hostname(host->mmc), __func__, rc);
2432 goto disable_pclk;
2433 }
2434
2435 atomic_set(&msm_host->controller_clock, 1);
2436 pr_debug("%s: %s: enabled controller clock\n",
2437 mmc_hostname(host->mmc), __func__);
2438 goto out;
2439
2440disable_pclk:
2441 if (!IS_ERR(msm_host->pclk))
2442 clk_disable_unprepare(msm_host->pclk);
2443remove_vote:
2444 if (msm_host->msm_bus_vote.client_handle)
2445 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2446out:
2447 return rc;
2448}
2449
2450
2451
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302452static int sdhci_msm_prepare_clocks(struct sdhci_host *host, bool enable)
2453{
2454 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2455 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2456 int rc = 0;
2457
2458 if (enable && !atomic_read(&msm_host->clks_on)) {
2459 pr_debug("%s: request to enable clocks\n",
2460 mmc_hostname(host->mmc));
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302461
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302462 /*
2463 * The bus-width or the clock rate might have changed
2464 * after controller clocks are enbaled, update bus vote
2465 * in such case.
2466 */
2467 if (atomic_read(&msm_host->controller_clock))
2468 sdhci_msm_bus_voting(host, 1);
2469
2470 rc = sdhci_msm_enable_controller_clock(host);
2471 if (rc)
2472 goto remove_vote;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302473
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302474 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
2475 rc = clk_prepare_enable(msm_host->bus_clk);
2476 if (rc) {
2477 pr_err("%s: %s: failed to enable the bus-clock with error %d\n",
2478 mmc_hostname(host->mmc), __func__, rc);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302479 goto disable_controller_clk;
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302480 }
2481 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002482 if (!IS_ERR(msm_host->ff_clk)) {
2483 rc = clk_prepare_enable(msm_host->ff_clk);
2484 if (rc) {
2485 pr_err("%s: %s: failed to enable the ff_clk with error %d\n",
2486 mmc_hostname(host->mmc), __func__, rc);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302487 goto disable_bus_clk;
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002488 }
2489 }
2490 if (!IS_ERR(msm_host->sleep_clk)) {
2491 rc = clk_prepare_enable(msm_host->sleep_clk);
2492 if (rc) {
2493 pr_err("%s: %s: failed to enable the sleep_clk with error %d\n",
2494 mmc_hostname(host->mmc), __func__, rc);
2495 goto disable_ff_clk;
2496 }
2497 }
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302498 mb();
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302499
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302500 } else if (!enable && atomic_read(&msm_host->clks_on)) {
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302501 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
2502 mb();
Sahitya Tummaladc182982013-08-20 15:32:09 +05302503 /*
2504 * During 1.8V signal switching the clock source must
2505 * still be ON as it requires accessing SDHC
2506 * registers (SDHCi host control2 register bit 3 must
2507 * be written and polled after stopping the SDCLK).
2508 */
2509 if (host->mmc->card_clock_off)
2510 return 0;
2511 pr_debug("%s: request to disable clocks\n",
2512 mmc_hostname(host->mmc));
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002513 if (!IS_ERR_OR_NULL(msm_host->sleep_clk))
2514 clk_disable_unprepare(msm_host->sleep_clk);
2515 if (!IS_ERR_OR_NULL(msm_host->ff_clk))
2516 clk_disable_unprepare(msm_host->ff_clk);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302517 clk_disable_unprepare(msm_host->clk);
2518 if (!IS_ERR(msm_host->pclk))
2519 clk_disable_unprepare(msm_host->pclk);
2520 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2521 clk_disable_unprepare(msm_host->bus_clk);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302522
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302523 atomic_set(&msm_host->controller_clock, 0);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302524 sdhci_msm_bus_voting(host, 0);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302525 }
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302526 atomic_set(&msm_host->clks_on, enable);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302527 goto out;
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002528disable_ff_clk:
2529 if (!IS_ERR_OR_NULL(msm_host->ff_clk))
2530 clk_disable_unprepare(msm_host->ff_clk);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302531disable_bus_clk:
2532 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2533 clk_disable_unprepare(msm_host->bus_clk);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302534disable_controller_clk:
2535 if (!IS_ERR_OR_NULL(msm_host->clk))
2536 clk_disable_unprepare(msm_host->clk);
2537 if (!IS_ERR_OR_NULL(msm_host->pclk))
2538 clk_disable_unprepare(msm_host->pclk);
2539 atomic_set(&msm_host->controller_clock, 0);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302540remove_vote:
2541 if (msm_host->msm_bus_vote.client_handle)
2542 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302543out:
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302544 return rc;
2545}
2546
2547static void sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock)
2548{
2549 int rc;
2550 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2551 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2552 struct mmc_ios curr_ios = host->mmc->ios;
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002553 u32 sup_clock, ddr_clock, dll_lock;
Sahitya Tummala043744a2013-06-24 09:55:33 +05302554 bool curr_pwrsave;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302555
2556 if (!clock) {
Sujit Reddy Thummabf1aecc2014-01-10 10:58:54 +05302557 /*
2558 * disable pwrsave to ensure clock is not auto-gated until
2559 * the rate is >400KHz (initialization complete).
2560 */
2561 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2562 ~CORE_CLK_PWRSAVE, host->ioaddr + CORE_VENDOR_SPEC);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302563 sdhci_msm_prepare_clocks(host, false);
2564 host->clock = clock;
2565 goto out;
2566 }
2567
2568 rc = sdhci_msm_prepare_clocks(host, true);
2569 if (rc)
2570 goto out;
2571
Sahitya Tummala043744a2013-06-24 09:55:33 +05302572 curr_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2573 CORE_CLK_PWRSAVE);
Sahitya Tummalae000b242013-08-29 16:21:08 +05302574 if ((clock > 400000) &&
Venkat Gopalakrishnanc0a367272015-02-24 13:09:09 -08002575 !curr_pwrsave && mmc_host_may_gate_card(host->mmc->card))
Sahitya Tummala043744a2013-06-24 09:55:33 +05302576 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2577 | CORE_CLK_PWRSAVE,
2578 host->ioaddr + CORE_VENDOR_SPEC);
2579 /*
2580 * Disable pwrsave for a newly added card if doesn't allow clock
2581 * gating.
2582 */
Venkat Gopalakrishnanc0a367272015-02-24 13:09:09 -08002583 else if (curr_pwrsave && !mmc_host_may_gate_card(host->mmc->card))
Sahitya Tummala043744a2013-06-24 09:55:33 +05302584 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2585 & ~CORE_CLK_PWRSAVE,
2586 host->ioaddr + CORE_VENDOR_SPEC);
2587
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302588 sup_clock = sdhci_msm_get_sup_clk_rate(host, clock);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002589 if ((curr_ios.timing == MMC_TIMING_UHS_DDR50) ||
Venkat Gopalakrishnan0a29da92015-01-09 12:19:16 -08002590 (curr_ios.timing == MMC_TIMING_MMC_DDR52) ||
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002591 (curr_ios.timing == MMC_TIMING_MMC_HS400)) {
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302592 /*
2593 * The SDHC requires internal clock frequency to be double the
2594 * actual clock that will be set for DDR mode. The controller
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002595 * uses the faster clock(100/400MHz) for some of its parts and
2596 * send the actual required clock (50/200MHz) to the card.
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302597 */
2598 ddr_clock = clock * 2;
2599 sup_clock = sdhci_msm_get_sup_clk_rate(host,
2600 ddr_clock);
2601 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002602
2603 /*
2604 * In general all timing modes are controlled via UHS mode select in
2605 * Host Control2 register. eMMC specific HS200/HS400 doesn't have
2606 * their respective modes defined here, hence we use these values.
2607 *
2608 * HS200 - SDR104 (Since they both are equivalent in functionality)
2609 * HS400 - This involves multiple configurations
2610 * Initially SDR104 - when tuning is required as HS200
2611 * Then when switching to DDR @ 400MHz (HS400) we use
2612 * the vendor specific HC_SELECT_IN to control the mode.
2613 *
2614 * In addition to controlling the modes we also need to select the
2615 * correct input clock for DLL depending on the mode.
2616 *
2617 * HS400 - divided clock (free running MCLK/2)
2618 * All other modes - default (free running MCLK)
2619 */
2620 if (curr_ios.timing == MMC_TIMING_MMC_HS400) {
2621 /* Select the divided clock (free running MCLK/2) */
2622 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2623 & ~CORE_HC_MCLK_SEL_MASK)
2624 | CORE_HC_MCLK_SEL_HS400),
2625 host->ioaddr + CORE_VENDOR_SPEC);
2626 /*
2627 * Select HS400 mode using the HC_SELECT_IN from VENDOR SPEC
2628 * register
2629 */
Ritesh Harjaniea709662015-05-27 15:40:24 +05302630 if ((msm_host->tuning_done || msm_host->enhanced_strobe) &&
2631 !msm_host->calibration_done) {
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002632 /*
2633 * Write 0x6 to HC_SELECT_IN and 1 to HC_SELECT_IN_EN
2634 * field in VENDOR_SPEC_FUNC
2635 */
2636 writel_relaxed((readl_relaxed(host->ioaddr + \
2637 CORE_VENDOR_SPEC)
2638 | CORE_HC_SELECT_IN_HS400
2639 | CORE_HC_SELECT_IN_EN),
2640 host->ioaddr + CORE_VENDOR_SPEC);
2641 }
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002642 if (!host->mmc->ios.old_rate && !msm_host->use_cdclp533) {
2643 /*
2644 * Poll on DLL_LOCK and DDR_DLL_LOCK bits in
2645 * CORE_DLL_STATUS to be set. This should get set
2646 * with in 15 us at 200 MHz.
2647 */
2648 rc = readl_poll_timeout(host->ioaddr + CORE_DLL_STATUS,
2649 dll_lock, (dll_lock & (CORE_DLL_LOCK |
2650 CORE_DDR_DLL_LOCK)), 10, 1000);
2651 if (rc == -ETIMEDOUT)
2652 pr_err("%s: Unable to get DLL_LOCK/DDR_DLL_LOCK, dll_status: 0x%08x\n",
2653 mmc_hostname(host->mmc),
2654 dll_lock);
2655 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002656 } else {
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002657 if (!msm_host->use_cdclp533)
2658 /* set CORE_PWRSAVE_DLL bit in CORE_VENDOR_SPEC3 */
2659 writel_relaxed((readl_relaxed(host->ioaddr +
2660 CORE_VENDOR_SPEC3) & ~CORE_PWRSAVE_DLL),
2661 host->ioaddr + CORE_VENDOR_SPEC3);
2662
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002663 /* Select the default clock (free running MCLK) */
2664 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2665 & ~CORE_HC_MCLK_SEL_MASK)
2666 | CORE_HC_MCLK_SEL_DFLT),
2667 host->ioaddr + CORE_VENDOR_SPEC);
2668
2669 /*
2670 * Disable HC_SELECT_IN to be able to use the UHS mode select
2671 * configuration from Host Control2 register for all other
2672 * modes.
2673 *
2674 * Write 0 to HC_SELECT_IN and HC_SELECT_IN_EN field
2675 * in VENDOR_SPEC_FUNC
2676 */
2677 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2678 & ~CORE_HC_SELECT_IN_EN
2679 & ~CORE_HC_SELECT_IN_MASK),
2680 host->ioaddr + CORE_VENDOR_SPEC);
2681 }
2682 mb();
2683
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302684 if (sup_clock != msm_host->clk_rate) {
2685 pr_debug("%s: %s: setting clk rate to %u\n",
2686 mmc_hostname(host->mmc), __func__, sup_clock);
2687 rc = clk_set_rate(msm_host->clk, sup_clock);
2688 if (rc) {
2689 pr_err("%s: %s: Failed to set rate %u for host-clk : %d\n",
2690 mmc_hostname(host->mmc), __func__,
2691 sup_clock, rc);
2692 goto out;
2693 }
2694 msm_host->clk_rate = sup_clock;
2695 host->clock = clock;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302696 /*
2697 * Update the bus vote in case of frequency change due to
2698 * clock scaling.
2699 */
2700 sdhci_msm_bus_voting(host, 1);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302701 }
2702out:
2703 sdhci_set_clock(host, clock);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302704}
2705
Sahitya Tummala14613432013-03-21 11:13:25 +05302706static void sdhci_msm_set_uhs_signaling(struct sdhci_host *host,
2707 unsigned int uhs)
2708{
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002709 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2710 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala14613432013-03-21 11:13:25 +05302711 u16 ctrl_2;
2712
2713 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2714 /* Select Bus Speed Mode for host */
2715 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Venkat Gopalakrishnan0a29da92015-01-09 12:19:16 -08002716 if ((uhs == MMC_TIMING_MMC_HS400) ||
2717 (uhs == MMC_TIMING_MMC_HS200) ||
2718 (uhs == MMC_TIMING_UHS_SDR104))
Sahitya Tummala14613432013-03-21 11:13:25 +05302719 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2720 else if (uhs == MMC_TIMING_UHS_SDR12)
2721 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
2722 else if (uhs == MMC_TIMING_UHS_SDR25)
2723 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
2724 else if (uhs == MMC_TIMING_UHS_SDR50)
2725 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
Venkat Gopalakrishnan0a29da92015-01-09 12:19:16 -08002726 else if ((uhs == MMC_TIMING_UHS_DDR50) ||
2727 (uhs == MMC_TIMING_MMC_DDR52))
Sahitya Tummala14613432013-03-21 11:13:25 +05302728 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302729 /*
2730 * When clock frquency is less than 100MHz, the feedback clock must be
2731 * provided and DLL must not be used so that tuning can be skipped. To
2732 * provide feedback clock, the mode selection can be any value less
2733 * than 3'b011 in bits [2:0] of HOST CONTROL2 register.
2734 */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002735 if (host->clock <= CORE_FREQ_100MHZ) {
2736 if ((uhs == MMC_TIMING_MMC_HS400) ||
2737 (uhs == MMC_TIMING_MMC_HS200) ||
2738 (uhs == MMC_TIMING_UHS_SDR104))
2739 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302740
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002741 /*
2742 * Make sure DLL is disabled when not required
2743 *
2744 * Write 1 to DLL_RST bit of DLL_CONFIG register
2745 */
2746 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
2747 | CORE_DLL_RST),
2748 host->ioaddr + CORE_DLL_CONFIG);
2749
2750 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
2751 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
2752 | CORE_DLL_PDN),
2753 host->ioaddr + CORE_DLL_CONFIG);
2754 mb();
2755
2756 /*
2757 * The DLL needs to be restored and CDCLP533 recalibrated
2758 * when the clock frequency is set back to 400MHz.
2759 */
2760 msm_host->calibration_done = false;
2761 }
2762
2763 pr_debug("%s: %s-clock:%u uhs mode:%u ctrl_2:0x%x\n",
2764 mmc_hostname(host->mmc), __func__, host->clock, uhs, ctrl_2);
Sahitya Tummala14613432013-03-21 11:13:25 +05302765 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
2766
2767}
2768
Venkat Gopalakrishnan34811972015-03-04 14:39:01 -08002769#define MAX_TEST_BUS 60
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302770
2771void sdhci_msm_dump_vendor_regs(struct sdhci_host *host)
2772{
2773 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2774 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2775 int tbsel, tbsel2;
2776 int i, index = 0;
2777 u32 test_bus_val = 0;
2778 u32 debug_reg[MAX_TEST_BUS] = {0};
2779
2780 pr_info("----------- VENDOR REGISTER DUMP -----------\n");
2781 pr_info("Data cnt: 0x%08x | Fifo cnt: 0x%08x | Int sts: 0x%08x\n",
2782 readl_relaxed(msm_host->core_mem + CORE_MCI_DATA_CNT),
2783 readl_relaxed(msm_host->core_mem + CORE_MCI_FIFO_CNT),
2784 readl_relaxed(msm_host->core_mem + CORE_MCI_STATUS));
2785 pr_info("DLL cfg: 0x%08x | DLL sts: 0x%08x | SDCC ver: 0x%08x\n",
2786 readl_relaxed(host->ioaddr + CORE_DLL_CONFIG),
2787 readl_relaxed(host->ioaddr + CORE_DLL_STATUS),
2788 readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION));
2789 pr_info("Vndr func: 0x%08x | Vndr adma err : addr0: 0x%08x addr1: 0x%08x\n",
2790 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC),
2791 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_ADMA_ERR_ADDR0),
2792 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_ADMA_ERR_ADDR1));
Sahitya Tummala2ba05ae2015-03-03 14:03:20 +05302793 pr_info("Vndr func2: 0x%08x\n",
2794 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_FUNC2));
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302795
2796 /*
2797 * tbsel indicates [2:0] bits and tbsel2 indicates [7:4] bits
2798 * of CORE_TESTBUS_CONFIG register.
2799 *
2800 * To select test bus 0 to 7 use tbsel and to select any test bus
2801 * above 7 use (tbsel2 | tbsel) to get the test bus number. For eg,
2802 * to select test bus 14, write 0x1E to CORE_TESTBUS_CONFIG register
2803 * i.e., tbsel2[7:4] = 0001, tbsel[2:0] = 110.
2804 */
Venkat Gopalakrishnan34811972015-03-04 14:39:01 -08002805 for (tbsel2 = 0; tbsel2 < 7; tbsel2++) {
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302806 for (tbsel = 0; tbsel < 8; tbsel++) {
2807 if (index >= MAX_TEST_BUS)
2808 break;
2809 test_bus_val = (tbsel2 << CORE_TESTBUS_SEL2_BIT) |
2810 tbsel | CORE_TESTBUS_ENA;
2811 writel_relaxed(test_bus_val,
2812 msm_host->core_mem + CORE_TESTBUS_CONFIG);
2813 debug_reg[index++] = readl_relaxed(msm_host->core_mem +
2814 CORE_SDCC_DEBUG_REG);
2815 }
2816 }
2817 for (i = 0; i < MAX_TEST_BUS; i = i + 4)
2818 pr_info(" Test bus[%d to %d]: 0x%08x 0x%08x 0x%08x 0x%08x\n",
2819 i, i + 3, debug_reg[i], debug_reg[i+1],
2820 debug_reg[i+2], debug_reg[i+3]);
Venkat Gopalakrishnan06f7f792015-05-29 17:56:59 -07002821}
2822
Ritesh Harjani4a5ffd12015-07-15 13:32:07 +05302823/*
2824 * sdhci_msm_enhanced_strobe_mask :-
2825 * Before running CMDQ transfers in HS400 Enhanced Strobe mode,
2826 * SW should write 3 to
2827 * HC_VENDOR_SPECIFIC_FUNC3.CMDEN_HS400_INPUT_MASK_CNT register.
2828 * The default reset value of this register is 2.
2829 */
2830static void sdhci_msm_enhanced_strobe_mask(struct sdhci_host *host, bool set)
2831{
2832 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2833 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2834
2835 if (!msm_host->enhanced_strobe) {
2836 pr_debug("%s: host does not support hs400 enhanced strobe\n",
2837 mmc_hostname(host->mmc));
2838 return;
2839 }
2840
2841 if (set) {
2842 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC3)
2843 | CORE_CMDEN_HS400_INPUT_MASK_CNT),
2844 host->ioaddr + CORE_VENDOR_SPEC3);
2845 } else {
2846 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC3)
2847 & ~CORE_CMDEN_HS400_INPUT_MASK_CNT),
2848 host->ioaddr + CORE_VENDOR_SPEC3);
2849 }
2850}
2851
Venkat Gopalakrishnan06f7f792015-05-29 17:56:59 -07002852static void sdhci_msm_clear_set_dumpregs(struct sdhci_host *host, bool set)
2853{
2854 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2855 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2856
2857 if (set) {
2858 writel_relaxed(CORE_TESTBUS_ENA,
2859 msm_host->core_mem + CORE_TESTBUS_CONFIG);
2860 } else {
2861 u32 value;
2862
2863 value = readl_relaxed(msm_host->core_mem + CORE_TESTBUS_CONFIG);
2864 value &= ~CORE_TESTBUS_ENA;
2865 writel_relaxed(value, msm_host->core_mem + CORE_TESTBUS_CONFIG);
2866 }
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302867}
2868
Dov Levenglick9c575e22015-07-20 09:30:52 +03002869static void sdhci_msm_detect(struct sdhci_host *host, bool detected)
2870{
2871 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2872 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2873 struct mmc_host *mmc = msm_host->mmc;
2874 struct mmc_card *card = mmc->card;
2875
2876 if (detected && mmc_card_sdio(card))
2877 mmc->pm_caps |= MMC_PM_KEEP_POWER;
2878 else
2879 mmc->pm_caps &= ~MMC_PM_KEEP_POWER;
2880}
2881
Pavan Anamula691dd592015-08-25 16:11:20 +05302882void sdhci_msm_reset_workaround(struct sdhci_host *host, u32 enable)
2883{
2884 u32 vendor_func2;
2885 unsigned long timeout;
2886
2887 vendor_func2 = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_FUNC2);
2888
2889 if (enable) {
2890 writel_relaxed(vendor_func2 | HC_SW_RST_REQ, host->ioaddr +
2891 CORE_VENDOR_SPEC_FUNC2);
2892 timeout = 10000;
2893 while (readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_FUNC2) &
2894 HC_SW_RST_REQ) {
2895 if (timeout == 0) {
2896 pr_info("%s: Applying wait idle disable workaround\n",
2897 mmc_hostname(host->mmc));
2898 /*
2899 * Apply the reset workaround to not wait for
2900 * pending data transfers on AXI before
2901 * resetting the controller. This could be
2902 * risky if the transfers were stuck on the
2903 * AXI bus.
2904 */
2905 vendor_func2 = readl_relaxed(host->ioaddr +
2906 CORE_VENDOR_SPEC_FUNC2);
2907 writel_relaxed(vendor_func2 |
2908 HC_SW_RST_WAIT_IDLE_DIS,
2909 host->ioaddr + CORE_VENDOR_SPEC_FUNC2);
2910 host->reset_wa_t = ktime_get();
2911 return;
2912 }
2913 timeout--;
2914 udelay(10);
2915 }
2916 pr_info("%s: waiting for SW_RST_REQ is successful\n",
2917 mmc_hostname(host->mmc));
2918 } else {
2919 writel_relaxed(vendor_func2 & ~HC_SW_RST_WAIT_IDLE_DIS,
2920 host->ioaddr + CORE_VENDOR_SPEC_FUNC2);
2921 }
2922}
2923
Asutosh Das0ef24812012-12-18 16:14:02 +05302924static struct sdhci_ops sdhci_msm_ops = {
Sahitya Tummala14613432013-03-21 11:13:25 +05302925 .set_uhs_signaling = sdhci_msm_set_uhs_signaling,
Asutosh Das0ef24812012-12-18 16:14:02 +05302926 .check_power_status = sdhci_msm_check_power_status,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002927 .platform_execute_tuning = sdhci_msm_execute_tuning,
Ritesh Harjaniea709662015-05-27 15:40:24 +05302928 .enhanced_strobe = sdhci_msm_enhanced_strobe,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002929 .toggle_cdr = sdhci_msm_toggle_cdr,
Asutosh Das648f9d12013-01-10 21:11:04 +05302930 .get_max_segments = sdhci_msm_max_segs,
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302931 .set_clock = sdhci_msm_set_clock,
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302932 .get_min_clock = sdhci_msm_get_min_clock,
2933 .get_max_clock = sdhci_msm_get_max_clock,
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302934 .dump_vendor_regs = sdhci_msm_dump_vendor_regs,
Asutosh Dase5e9ca62013-07-30 19:08:36 +05302935 .config_auto_tuning_cmd = sdhci_msm_config_auto_tuning_cmd,
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302936 .enable_controller_clock = sdhci_msm_enable_controller_clock,
Venkat Gopalakrishnanb8cb7072015-01-09 11:04:34 -08002937 .set_bus_width = sdhci_set_bus_width,
Venkat Gopalakrishnan411df072015-01-09 11:09:44 -08002938 .reset = sdhci_reset,
Venkat Gopalakrishnan06f7f792015-05-29 17:56:59 -07002939 .clear_set_dumpregs = sdhci_msm_clear_set_dumpregs,
Ritesh Harjani4a5ffd12015-07-15 13:32:07 +05302940 .enhanced_strobe_mask = sdhci_msm_enhanced_strobe_mask,
Dov Levenglick9c575e22015-07-20 09:30:52 +03002941 .detect = sdhci_msm_detect,
Pavan Anamula691dd592015-08-25 16:11:20 +05302942 .reset_workaround = sdhci_msm_reset_workaround,
Asutosh Das0ef24812012-12-18 16:14:02 +05302943};
2944
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302945static void sdhci_set_default_hw_caps(struct sdhci_msm_host *msm_host,
2946 struct sdhci_host *host)
2947{
Krishna Konda46fd1432014-10-30 21:13:27 -07002948 u32 version, caps = 0;
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302949 u16 minor;
2950 u8 major;
Sahitya Tummala2ba05ae2015-03-03 14:03:20 +05302951 u32 val;
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302952
2953 version = readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION);
2954 major = (version & CORE_VERSION_MAJOR_MASK) >>
2955 CORE_VERSION_MAJOR_SHIFT;
2956 minor = version & CORE_VERSION_TARGET_MASK;
2957
Krishna Konda46fd1432014-10-30 21:13:27 -07002958 caps = readl_relaxed(host->ioaddr + SDHCI_CAPABILITIES);
2959
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302960 /*
2961 * Starting with SDCC 5 controller (core major version = 1)
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002962 * controller won't advertise 3.0v, 1.8v and 8-bit features
2963 * except for some targets.
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302964 */
2965 if (major >= 1 && minor != 0x11 && minor != 0x12) {
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002966 struct sdhci_msm_reg_data *vdd_io_reg;
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002967 /*
2968 * Enable 1.8V support capability on controllers that
2969 * support dual voltage
2970 */
2971 vdd_io_reg = msm_host->pdata->vreg_data->vdd_io_data;
Krishna Konda46fd1432014-10-30 21:13:27 -07002972 if (vdd_io_reg && (vdd_io_reg->high_vol_level > 2700000))
2973 caps |= CORE_3_0V_SUPPORT;
2974 if (vdd_io_reg && (vdd_io_reg->low_vol_level < 1950000))
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002975 caps |= CORE_1_8V_SUPPORT;
Pratibhasagar Vada47992013-12-09 20:42:32 +05302976 if (msm_host->pdata->mmc_bus_width == MMC_CAP_8_BIT_DATA)
2977 caps |= CORE_8_BIT_SUPPORT;
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302978 }
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002979
2980 /*
Sahitya Tummala2ba05ae2015-03-03 14:03:20 +05302981 * Enable one MID mode for SDCC5 (major 1) on 8916/8939 (minor 0x2e) and
2982 * on 8992 (minor 0x3e) as a workaround to reset for data stuck issue.
2983 */
2984 if (major == 1 && (minor == 0x2e || minor == 0x3e)) {
Pavan Anamula691dd592015-08-25 16:11:20 +05302985 host->quirks2 |= SDHCI_QUIRK2_USE_RESET_WORKAROUND;
Sahitya Tummala2ba05ae2015-03-03 14:03:20 +05302986 val = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_FUNC2);
2987 writel_relaxed((val | CORE_ONE_MID_EN),
2988 host->ioaddr + CORE_VENDOR_SPEC_FUNC2);
2989 }
2990 /*
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002991 * SDCC 5 controller with major version 1, minor version 0x34 and later
2992 * with HS 400 mode support will use CM DLL instead of CDC LP 533 DLL.
2993 */
2994 if ((major == 1) && (minor < 0x34))
2995 msm_host->use_cdclp533 = true;
Gilad Broner2a10ca02014-10-02 17:20:35 +03002996
2997 /*
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -08002998 * SDCC 5 controller with major version 1, minor version 0x42 and later
2999 * will require additional steps when resetting DLL.
Ritesh Harjaniea709662015-05-27 15:40:24 +05303000 * It also supports HS400 enhanced strobe mode.
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -08003001 */
Ritesh Harjaniea709662015-05-27 15:40:24 +05303002 if ((major == 1) && (minor >= 0x42)) {
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -08003003 msm_host->use_updated_dll_reset = true;
Ritesh Harjaniea709662015-05-27 15:40:24 +05303004 msm_host->enhanced_strobe = true;
3005 }
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -08003006
3007 /*
Talel Shenhar9a25b882015-06-02 13:36:35 +03003008 * SDCC 5 controller with major version 1 and minor version 0x42,
3009 * 0x46 and 0x49 currently uses 14lpp tech DLL whose internal
3010 * gating cannot guarantee MCLK timing requirement i.e.
Ritesh Harjani764065e2015-05-13 14:14:45 +05303011 * when MCLK is gated OFF, it is not gated for less than 0.5us
3012 * and MCLK must be switched on for at-least 1us before DATA
3013 * starts coming.
3014 */
Talel Shenhar9a25b882015-06-02 13:36:35 +03003015 if ((major == 1) && ((minor == 0x42) || (minor == 0x46) ||
3016 (minor == 0x49)))
Ritesh Harjani764065e2015-05-13 14:14:45 +05303017 msm_host->use_14lpp_dll = true;
3018 /*
Gilad Broner2a10ca02014-10-02 17:20:35 +03003019 * Mask 64-bit support for controller with 32-bit address bus so that
3020 * smaller descriptor size will be used and improve memory consumption.
Gilad Broner2a10ca02014-10-02 17:20:35 +03003021 */
Venkat Gopalakrishnan9a62e042015-03-03 16:14:55 -08003022 if (!msm_host->pdata->largeaddressbus)
3023 caps &= ~CORE_SYS_BUS_SUPPORT_64_BIT;
3024
Gilad Broner2a10ca02014-10-02 17:20:35 +03003025 writel_relaxed(caps, host->ioaddr + CORE_VENDOR_SPEC_CAPABILITIES0);
Krishna Konda46fd1432014-10-30 21:13:27 -07003026 /* keep track of the value in SDHCI_CAPABILITIES */
3027 msm_host->caps_0 = caps;
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05303028}
3029
Venkat Gopalakrishnand371f142015-05-29 17:49:46 -07003030#ifdef CONFIG_MMC_CQ_HCI
3031static void sdhci_msm_cmdq_init(struct sdhci_host *host,
3032 struct platform_device *pdev)
3033{
3034 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3035 struct sdhci_msm_host *msm_host = pltfm_host->priv;
3036
3037 host->cq_host = cmdq_pltfm_init(pdev);
3038 if (IS_ERR(host->cq_host))
3039 dev_dbg(&pdev->dev, "cmdq-pltfm init: failed: %ld\n",
3040 PTR_ERR(host->cq_host));
3041 else
3042 msm_host->mmc->caps2 |= MMC_CAP2_CMD_QUEUE;
3043}
3044#else
3045static void sdhci_msm_cmdq_init(struct sdhci_host *host,
3046 struct platform_device *pdev)
3047{
3048
3049}
3050#endif
3051
Subhash Jadavaniebcd00d2015-07-09 17:28:42 -07003052static bool sdhci_msm_is_bootdevice(struct device *dev)
3053{
3054 if (strnstr(saved_command_line, "androidboot.bootdevice=",
3055 strlen(saved_command_line))) {
3056 char search_string[50];
3057
3058 snprintf(search_string, ARRAY_SIZE(search_string),
3059 "androidboot.bootdevice=%s", dev_name(dev));
3060 if (strnstr(saved_command_line, search_string,
3061 strlen(saved_command_line)))
3062 return true;
3063 else
3064 return false;
3065 }
3066
3067 /*
3068 * "androidboot.bootdevice=" argument is not present then
3069 * return true as we don't know the boot device anyways.
3070 */
3071 return true;
3072}
3073
Asutosh Das0ef24812012-12-18 16:14:02 +05303074static int sdhci_msm_probe(struct platform_device *pdev)
3075{
3076 struct sdhci_host *host;
3077 struct sdhci_pltfm_host *pltfm_host;
3078 struct sdhci_msm_host *msm_host;
3079 struct resource *core_memres = NULL;
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02003080 int ret = 0, dead = 0;
Stephen Boyd8dce5c62013-04-24 14:19:46 -07003081 u16 host_version;
Venkat Gopalakrishnan17edb352015-06-24 14:46:49 -07003082 u32 irq_status, irq_ctl;
Asutosh Das0ef24812012-12-18 16:14:02 +05303083
3084 pr_debug("%s: Enter %s\n", dev_name(&pdev->dev), __func__);
3085 msm_host = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_msm_host),
3086 GFP_KERNEL);
3087 if (!msm_host) {
3088 ret = -ENOMEM;
3089 goto out;
3090 }
Asutosh Das0ef24812012-12-18 16:14:02 +05303091
3092 msm_host->sdhci_msm_pdata.ops = &sdhci_msm_ops;
3093 host = sdhci_pltfm_init(pdev, &msm_host->sdhci_msm_pdata, 0);
3094 if (IS_ERR(host)) {
3095 ret = PTR_ERR(host);
3096 goto out;
3097 }
3098
3099 pltfm_host = sdhci_priv(host);
3100 pltfm_host->priv = msm_host;
3101 msm_host->mmc = host->mmc;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05303102 msm_host->pdev = pdev;
Asutosh Das0ef24812012-12-18 16:14:02 +05303103
3104 /* Extract platform data */
3105 if (pdev->dev.of_node) {
Venkat Gopalakrishnan270580a2013-03-11 12:17:57 -07003106 ret = of_alias_get_id(pdev->dev.of_node, "sdhc");
3107 if (ret < 0) {
3108 dev_err(&pdev->dev, "Failed to get slot index %d\n",
3109 ret);
3110 goto pltfm_free;
3111 }
Subhash Jadavaniebcd00d2015-07-09 17:28:42 -07003112
3113 /* skip the probe if eMMC isn't a boot device */
3114 if ((ret == 1) && !sdhci_msm_is_bootdevice(&pdev->dev))
3115 goto pltfm_free;
3116
Venkat Gopalakrishnan270580a2013-03-11 12:17:57 -07003117 if (disable_slots & (1 << (ret - 1))) {
3118 dev_info(&pdev->dev, "%s: Slot %d disabled\n", __func__,
3119 ret);
3120 ret = -ENODEV;
3121 goto pltfm_free;
3122 }
3123
Dov Levenglickc9033ab2015-03-10 16:00:56 +02003124 msm_host->pdata = sdhci_msm_populate_pdata(&pdev->dev,
3125 msm_host);
Asutosh Das0ef24812012-12-18 16:14:02 +05303126 if (!msm_host->pdata) {
3127 dev_err(&pdev->dev, "DT parsing error\n");
3128 goto pltfm_free;
3129 }
3130 } else {
3131 dev_err(&pdev->dev, "No device tree node\n");
3132 goto pltfm_free;
3133 }
3134
3135 /* Setup Clocks */
3136
3137 /* Setup SDCC bus voter clock. */
3138 msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus_clk");
3139 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
3140 /* Vote for max. clk rate for max. performance */
3141 ret = clk_set_rate(msm_host->bus_clk, INT_MAX);
3142 if (ret)
3143 goto pltfm_free;
3144 ret = clk_prepare_enable(msm_host->bus_clk);
3145 if (ret)
3146 goto pltfm_free;
3147 }
3148
3149 /* Setup main peripheral bus clock */
3150 msm_host->pclk = devm_clk_get(&pdev->dev, "iface_clk");
3151 if (!IS_ERR(msm_host->pclk)) {
3152 ret = clk_prepare_enable(msm_host->pclk);
3153 if (ret)
3154 goto bus_clk_disable;
3155 }
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05303156 atomic_set(&msm_host->controller_clock, 1);
Asutosh Das0ef24812012-12-18 16:14:02 +05303157
3158 /* Setup SDC MMC clock */
3159 msm_host->clk = devm_clk_get(&pdev->dev, "core_clk");
3160 if (IS_ERR(msm_host->clk)) {
3161 ret = PTR_ERR(msm_host->clk);
3162 goto pclk_disable;
3163 }
3164
Sahitya Tummala22dd3362013-02-28 19:50:51 +05303165 /* Set to the minimum supported clock frequency */
3166 ret = clk_set_rate(msm_host->clk, sdhci_msm_get_min_clock(host));
3167 if (ret) {
3168 dev_err(&pdev->dev, "MClk rate set failed (%d)\n", ret);
Sahitya Tummala020ede0d2013-06-07 13:03:07 +05303169 goto pclk_disable;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05303170 }
Sahitya Tummala020ede0d2013-06-07 13:03:07 +05303171 ret = clk_prepare_enable(msm_host->clk);
3172 if (ret)
3173 goto pclk_disable;
3174
Sahitya Tummala22dd3362013-02-28 19:50:51 +05303175 msm_host->clk_rate = sdhci_msm_get_min_clock(host);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05303176 atomic_set(&msm_host->clks_on, 1);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05303177
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07003178 /* Setup CDC calibration fixed feedback clock */
3179 msm_host->ff_clk = devm_clk_get(&pdev->dev, "cal_clk");
3180 if (!IS_ERR(msm_host->ff_clk)) {
3181 ret = clk_prepare_enable(msm_host->ff_clk);
3182 if (ret)
3183 goto clk_disable;
3184 }
3185
3186 /* Setup CDC calibration sleep clock */
3187 msm_host->sleep_clk = devm_clk_get(&pdev->dev, "sleep_clk");
3188 if (!IS_ERR(msm_host->sleep_clk)) {
3189 ret = clk_prepare_enable(msm_host->sleep_clk);
3190 if (ret)
3191 goto ff_clk_disable;
3192 }
3193
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07003194 msm_host->saved_tuning_phase = INVALID_TUNING_PHASE;
3195
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05303196 ret = sdhci_msm_bus_register(msm_host, pdev);
3197 if (ret)
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07003198 goto sleep_clk_disable;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05303199
3200 if (msm_host->msm_bus_vote.client_handle)
3201 INIT_DELAYED_WORK(&msm_host->msm_bus_vote.vote_work,
3202 sdhci_msm_bus_work);
3203 sdhci_msm_bus_voting(host, 1);
3204
Asutosh Das0ef24812012-12-18 16:14:02 +05303205 /* Setup regulators */
3206 ret = sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, true);
3207 if (ret) {
3208 dev_err(&pdev->dev, "Regulator setup failed (%d)\n", ret);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05303209 goto bus_unregister;
Asutosh Das0ef24812012-12-18 16:14:02 +05303210 }
3211
3212 /* Reset the core and Enable SDHC mode */
3213 core_memres = platform_get_resource_byname(pdev,
3214 IORESOURCE_MEM, "core_mem");
Asutosh Das890bdee2014-08-08 23:01:42 +05303215 if (!core_memres) {
3216 dev_err(&pdev->dev, "Failed to get iomem resource\n");
3217 goto vreg_deinit;
3218 }
Asutosh Das0ef24812012-12-18 16:14:02 +05303219 msm_host->core_mem = devm_ioremap(&pdev->dev, core_memres->start,
3220 resource_size(core_memres));
3221
3222 if (!msm_host->core_mem) {
3223 dev_err(&pdev->dev, "Failed to remap registers\n");
3224 ret = -ENOMEM;
3225 goto vreg_deinit;
3226 }
3227
Sahitya Tummala66b0fe32013-04-25 11:50:56 +05303228 /*
Venkat Gopalakrishnan17edb352015-06-24 14:46:49 -07003229 * Reset the vendor spec register to power on reset state.
Sahitya Tummala66b0fe32013-04-25 11:50:56 +05303230 */
Venkat Gopalakrishnan17edb352015-06-24 14:46:49 -07003231 writel_relaxed(CORE_VENDOR_SPEC_POR_VAL,
3232 host->ioaddr + CORE_VENDOR_SPEC);
Sahitya Tummala66b0fe32013-04-25 11:50:56 +05303233
Asutosh Das0ef24812012-12-18 16:14:02 +05303234 /* Set HC_MODE_EN bit in HC_MODE register */
3235 writel_relaxed(HC_MODE_EN, (msm_host->core_mem + CORE_HC_MODE));
3236
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07003237 /* Set FF_CLK_SW_RST_DIS bit in HC_MODE register */
3238 writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_HC_MODE) |
3239 FF_CLK_SW_RST_DIS, msm_host->core_mem + CORE_HC_MODE);
3240
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05303241 sdhci_set_default_hw_caps(msm_host, host);
Krishna Konda46fd1432014-10-30 21:13:27 -07003242
3243 /*
3244 * Set the PAD_PWR_SWTICH_EN bit so that the PAD_PWR_SWITCH bit can
3245 * be used as required later on.
3246 */
3247 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
3248 CORE_IO_PAD_PWR_SWITCH_EN),
3249 host->ioaddr + CORE_VENDOR_SPEC);
Asutosh Das0ef24812012-12-18 16:14:02 +05303250 /*
Subhash Jadavani28137342013-05-14 17:46:43 +05303251 * CORE_SW_RST above may trigger power irq if previous status of PWRCTL
3252 * was either BUS_ON or IO_HIGH_V. So before we enable the power irq
3253 * interrupt in GIC (by registering the interrupt handler), we need to
3254 * ensure that any pending power irq interrupt status is acknowledged
3255 * otherwise power irq interrupt handler would be fired prematurely.
3256 */
3257 irq_status = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
3258 writel_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
3259 irq_ctl = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_CTL);
3260 if (irq_status & (CORE_PWRCTL_BUS_ON | CORE_PWRCTL_BUS_OFF))
3261 irq_ctl |= CORE_PWRCTL_BUS_SUCCESS;
3262 if (irq_status & (CORE_PWRCTL_IO_HIGH | CORE_PWRCTL_IO_LOW))
3263 irq_ctl |= CORE_PWRCTL_IO_SUCCESS;
3264 writel_relaxed(irq_ctl, (msm_host->core_mem + CORE_PWRCTL_CTL));
Krishna Konda46fd1432014-10-30 21:13:27 -07003265
Subhash Jadavani28137342013-05-14 17:46:43 +05303266 /*
3267 * Ensure that above writes are propogated before interrupt enablement
3268 * in GIC.
3269 */
3270 mb();
3271
3272 /*
Asutosh Das0ef24812012-12-18 16:14:02 +05303273 * Following are the deviations from SDHC spec v3.0 -
3274 * 1. Card detection is handled using separate GPIO.
3275 * 2. Bus power control is handled by interacting with PMIC.
3276 */
3277 host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
3278 host->quirks |= SDHCI_QUIRK_SINGLE_POWER_WRITE;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05303279 host->quirks |= SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN;
Talel Shenhar4661c2a2015-06-24 15:49:30 +03003280 host->quirks |= SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05303281 host->quirks2 |= SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK;
Sahitya Tummala87d43942013-04-12 11:49:11 +05303282 host->quirks2 |= SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD;
Sahitya Tummala314162c2013-04-12 12:11:20 +05303283 host->quirks2 |= SDHCI_QUIRK2_BROKEN_PRESET_VALUE;
Sahitya Tummala7c9780d2013-04-12 11:59:25 +05303284 host->quirks2 |= SDHCI_QUIRK2_USE_RESERVED_MAX_TIMEOUT;
Asutosh Das0ef24812012-12-18 16:14:02 +05303285
Sahitya Tummalaa5733ab52013-06-10 16:32:51 +05303286 if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK)
3287 host->quirks2 |= SDHCI_QUIRK2_DIVIDE_TOUT_BY_4;
3288
Stephen Boyd8dce5c62013-04-24 14:19:46 -07003289 host_version = readw_relaxed((host->ioaddr + SDHCI_HOST_VERSION));
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07003290 dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n",
3291 host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >>
3292 SDHCI_VENDOR_VER_SHIFT));
3293 if (((host_version & SDHCI_VENDOR_VER_MASK) >>
3294 SDHCI_VENDOR_VER_SHIFT) == SDHCI_VER_100) {
3295 /*
3296 * Add 40us delay in interrupt handler when
3297 * operating at initialization frequency(400KHz).
3298 */
3299 host->quirks2 |= SDHCI_QUIRK2_SLOW_INT_CLR;
3300 /*
3301 * Set Software Reset for DAT line in Software
3302 * Reset Register (Bit 2).
3303 */
3304 host->quirks2 |= SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT;
3305 }
3306
Asutosh Das214b9662013-06-13 14:27:42 +05303307 host->quirks2 |= SDHCI_QUIRK2_IGN_DATA_END_BIT_ERROR;
3308
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07003309 /* Setup PWRCTL irq */
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02003310 msm_host->pwr_irq = platform_get_irq_byname(pdev, "pwr_irq");
3311 if (msm_host->pwr_irq < 0) {
Asutosh Das0ef24812012-12-18 16:14:02 +05303312 dev_err(&pdev->dev, "Failed to get pwr_irq by name (%d)\n",
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02003313 msm_host->pwr_irq);
Asutosh Das0ef24812012-12-18 16:14:02 +05303314 goto vreg_deinit;
3315 }
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02003316 ret = devm_request_threaded_irq(&pdev->dev, msm_host->pwr_irq, NULL,
Asutosh Das0ef24812012-12-18 16:14:02 +05303317 sdhci_msm_pwr_irq, IRQF_ONESHOT,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07003318 dev_name(&pdev->dev), host);
Asutosh Das0ef24812012-12-18 16:14:02 +05303319 if (ret) {
3320 dev_err(&pdev->dev, "Request threaded irq(%d) failed (%d)\n",
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02003321 msm_host->pwr_irq, ret);
Asutosh Das0ef24812012-12-18 16:14:02 +05303322 goto vreg_deinit;
3323 }
3324
3325 /* Enable pwr irq interrupts */
3326 writel_relaxed(INT_MASK, (msm_host->core_mem + CORE_PWRCTL_MASK));
3327
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05303328#ifdef CONFIG_MMC_CLKGATE
3329 /* Set clock gating delay to be used when CONFIG_MMC_CLKGATE is set */
3330 msm_host->mmc->clkgate_delay = SDHCI_MSM_MMC_CLK_GATE_DELAY;
3331#endif
3332
Asutosh Das0ef24812012-12-18 16:14:02 +05303333 /* Set host capabilities */
3334 msm_host->mmc->caps |= msm_host->pdata->mmc_bus_width;
3335 msm_host->mmc->caps |= msm_host->pdata->caps;
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003336 msm_host->mmc->caps |= MMC_CAP_AGGRESSIVE_PM;
Asutosh Das0ef24812012-12-18 16:14:02 +05303337 msm_host->mmc->caps2 |= msm_host->pdata->caps2;
Venkat Gopalakrishnan97c16112014-12-16 18:26:25 -08003338 msm_host->mmc->caps2 |= MMC_CAP2_BOOTPART_NOACC;
3339 msm_host->mmc->caps2 |= MMC_CAP2_FULL_PWR_CYCLE;
Subhash Jadavani6d472b22013-05-29 15:52:10 +05303340 msm_host->mmc->caps2 |= MMC_CAP2_ASYNC_SDIO_IRQ_4BIT_MODE;
Venkat Gopalakrishnan97c16112014-12-16 18:26:25 -08003341 msm_host->mmc->caps2 |= MMC_CAP2_HS400_POST_TUNING;
Talel Shenhar3d1dbf32015-05-13 14:08:39 +03003342 msm_host->mmc->caps2 |= MMC_CAP2_CLK_SCALE;
Pavan Anamula07d62ef2015-08-24 18:56:22 +05303343 msm_host->mmc->caps2 |= MMC_CAP2_SANITIZE;
Asutosh Das0ef24812012-12-18 16:14:02 +05303344
3345 if (msm_host->pdata->nonremovable)
3346 msm_host->mmc->caps |= MMC_CAP_NONREMOVABLE;
3347
Guoping Yuf7c91332014-08-20 16:56:18 +08003348 if (msm_host->pdata->nonhotplug)
3349 msm_host->mmc->caps2 |= MMC_CAP2_NONHOTPLUG;
3350
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05303351 host->cpu_dma_latency_us = msm_host->pdata->cpu_dma_latency_us;
Maya Erez994cf2f2014-10-21 20:22:04 +03003352 host->pm_qos_req_dma.type = msm_host->pdata->cpu_affinity_type;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05303353
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05303354 init_completion(&msm_host->pwr_irq_completion);
3355
Sahitya Tummala581df132013-03-12 14:57:46 +05303356 if (gpio_is_valid(msm_host->pdata->status_gpio)) {
Sahitya Tummala6ddabb42014-06-05 13:26:55 +05303357 /*
3358 * Set up the card detect GPIO in active configuration before
3359 * configuring it as an IRQ. Otherwise, it can be in some
3360 * weird/inconsistent state resulting in flood of interrupts.
3361 */
3362 sdhci_msm_setup_pins(msm_host->pdata, true);
3363
Sahitya Tummalaa3888f42015-02-05 14:05:27 +05303364 /*
3365 * This delay is needed for stabilizing the card detect GPIO
3366 * line after changing the pull configs.
3367 */
3368 usleep_range(10000, 10500);
Sahitya Tummala581df132013-03-12 14:57:46 +05303369 ret = mmc_gpio_request_cd(msm_host->mmc,
3370 msm_host->pdata->status_gpio, 0);
3371 if (ret) {
3372 dev_err(&pdev->dev, "%s: Failed to request card detection IRQ %d\n",
3373 __func__, ret);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05303374 goto vreg_deinit;
Sahitya Tummala581df132013-03-12 14:57:46 +05303375 }
3376 }
3377
Krishna Konda7feab352013-09-17 23:55:40 -07003378 if ((sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT) &&
3379 (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(64)))) {
3380 host->dma_mask = DMA_BIT_MASK(64);
3381 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
Pavan Anamulaeeb3b142015-07-22 18:17:32 +05303382 mmc_dev(host->mmc)->coherent_dma_mask = host->dma_mask;
Krishna Konda7feab352013-09-17 23:55:40 -07003383 } else if (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(32))) {
Sahitya Tummalaeaa21862013-03-20 19:34:59 +05303384 host->dma_mask = DMA_BIT_MASK(32);
3385 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
Pavan Anamulaeeb3b142015-07-22 18:17:32 +05303386 mmc_dev(host->mmc)->coherent_dma_mask = host->dma_mask;
Sahitya Tummalaeaa21862013-03-20 19:34:59 +05303387 } else {
3388 dev_err(&pdev->dev, "%s: Failed to set dma mask\n", __func__);
3389 }
3390
Venkat Gopalakrishnand371f142015-05-29 17:49:46 -07003391 sdhci_msm_cmdq_init(host, pdev);
Asutosh Das0ef24812012-12-18 16:14:02 +05303392 ret = sdhci_add_host(host);
3393 if (ret) {
3394 dev_err(&pdev->dev, "Add host failed (%d)\n", ret);
Sahitya Tummala581df132013-03-12 14:57:46 +05303395 goto vreg_deinit;
Asutosh Das0ef24812012-12-18 16:14:02 +05303396 }
3397
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003398 pm_runtime_set_active(&pdev->dev);
3399 pm_runtime_enable(&pdev->dev);
3400 pm_runtime_set_autosuspend_delay(&pdev->dev, MSM_AUTOSUSPEND_DELAY_MS);
3401 pm_runtime_use_autosuspend(&pdev->dev);
3402
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05303403 msm_host->msm_bus_vote.max_bus_bw.show = show_sdhci_max_bus_bw;
3404 msm_host->msm_bus_vote.max_bus_bw.store = store_sdhci_max_bus_bw;
3405 sysfs_attr_init(&msm_host->msm_bus_vote.max_bus_bw.attr);
3406 msm_host->msm_bus_vote.max_bus_bw.attr.name = "max_bus_bw";
3407 msm_host->msm_bus_vote.max_bus_bw.attr.mode = S_IRUGO | S_IWUSR;
3408 ret = device_create_file(&pdev->dev,
3409 &msm_host->msm_bus_vote.max_bus_bw);
3410 if (ret)
3411 goto remove_host;
3412
Sahitya Tummala5c55b932013-06-20 14:00:18 +05303413 if (!gpio_is_valid(msm_host->pdata->status_gpio)) {
3414 msm_host->polling.show = show_polling;
3415 msm_host->polling.store = store_polling;
3416 sysfs_attr_init(&msm_host->polling.attr);
3417 msm_host->polling.attr.name = "polling";
3418 msm_host->polling.attr.mode = S_IRUGO | S_IWUSR;
3419 ret = device_create_file(&pdev->dev, &msm_host->polling);
3420 if (ret)
3421 goto remove_max_bus_bw_file;
3422 }
Asutosh Dase5e9ca62013-07-30 19:08:36 +05303423
3424 msm_host->auto_cmd21_attr.show = show_auto_cmd21;
3425 msm_host->auto_cmd21_attr.store = store_auto_cmd21;
3426 sysfs_attr_init(&msm_host->auto_cmd21_attr.attr);
3427 msm_host->auto_cmd21_attr.attr.name = "enable_auto_cmd21";
3428 msm_host->auto_cmd21_attr.attr.mode = S_IRUGO | S_IWUSR;
3429 ret = device_create_file(&pdev->dev, &msm_host->auto_cmd21_attr);
3430 if (ret) {
3431 pr_err("%s: %s: failed creating auto-cmd21 attr: %d\n",
3432 mmc_hostname(host->mmc), __func__, ret);
3433 device_remove_file(&pdev->dev, &msm_host->auto_cmd21_attr);
3434 }
Asutosh Das0ef24812012-12-18 16:14:02 +05303435 /* Successful initialization */
3436 goto out;
3437
Sahitya Tummala5c55b932013-06-20 14:00:18 +05303438remove_max_bus_bw_file:
3439 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Asutosh Das0ef24812012-12-18 16:14:02 +05303440remove_host:
3441 dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003442 pm_runtime_disable(&pdev->dev);
Asutosh Das0ef24812012-12-18 16:14:02 +05303443 sdhci_remove_host(host, dead);
3444vreg_deinit:
3445 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05303446bus_unregister:
3447 if (msm_host->msm_bus_vote.client_handle)
3448 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
3449 sdhci_msm_bus_unregister(msm_host);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07003450sleep_clk_disable:
3451 if (!IS_ERR(msm_host->sleep_clk))
3452 clk_disable_unprepare(msm_host->sleep_clk);
3453ff_clk_disable:
3454 if (!IS_ERR(msm_host->ff_clk))
3455 clk_disable_unprepare(msm_host->ff_clk);
Asutosh Das0ef24812012-12-18 16:14:02 +05303456clk_disable:
3457 if (!IS_ERR(msm_host->clk))
3458 clk_disable_unprepare(msm_host->clk);
3459pclk_disable:
3460 if (!IS_ERR(msm_host->pclk))
3461 clk_disable_unprepare(msm_host->pclk);
3462bus_clk_disable:
3463 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
3464 clk_disable_unprepare(msm_host->bus_clk);
3465pltfm_free:
3466 sdhci_pltfm_free(pdev);
3467out:
3468 pr_debug("%s: Exit %s\n", dev_name(&pdev->dev), __func__);
3469 return ret;
3470}
3471
3472static int sdhci_msm_remove(struct platform_device *pdev)
3473{
3474 struct sdhci_host *host = platform_get_drvdata(pdev);
3475 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3476 struct sdhci_msm_host *msm_host = pltfm_host->priv;
3477 struct sdhci_msm_pltfm_data *pdata = msm_host->pdata;
3478 int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) ==
3479 0xffffffff);
3480
3481 pr_debug("%s: %s\n", dev_name(&pdev->dev), __func__);
Sahitya Tummala5c55b932013-06-20 14:00:18 +05303482 if (!gpio_is_valid(msm_host->pdata->status_gpio))
3483 device_remove_file(&pdev->dev, &msm_host->polling);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05303484 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003485 pm_runtime_disable(&pdev->dev);
Asutosh Das0ef24812012-12-18 16:14:02 +05303486 sdhci_remove_host(host, dead);
3487 sdhci_pltfm_free(pdev);
Sahitya Tummala581df132013-03-12 14:57:46 +05303488
Asutosh Das0ef24812012-12-18 16:14:02 +05303489 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05303490
Pratibhasagar V9acf2642013-11-21 21:07:21 +05303491 sdhci_msm_setup_pins(pdata, true);
3492 sdhci_msm_setup_pins(pdata, false);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05303493
3494 if (msm_host->msm_bus_vote.client_handle) {
3495 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
3496 sdhci_msm_bus_unregister(msm_host);
3497 }
Asutosh Das0ef24812012-12-18 16:14:02 +05303498 return 0;
3499}
3500
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003501#ifdef CONFIG_PM
3502static int sdhci_msm_runtime_suspend(struct device *dev)
3503{
3504 struct sdhci_host *host = dev_get_drvdata(dev);
3505 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3506 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003507 ktime_t start = ktime_get();
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003508
3509 disable_irq(host->irq);
3510 disable_irq(msm_host->pwr_irq);
3511
3512 /*
3513 * Remove the vote immediately only if clocks are off in which
3514 * case we might have queued work to remove vote but it may not
3515 * be completed before runtime suspend or system suspend.
3516 */
3517 if (!atomic_read(&msm_host->clks_on)) {
3518 if (msm_host->msm_bus_vote.client_handle)
3519 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
3520 }
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003521 trace_sdhci_msm_runtime_suspend(mmc_hostname(host->mmc), 0,
3522 ktime_to_us(ktime_sub(ktime_get(), start)));
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003523
3524 return 0;
3525}
3526
3527static int sdhci_msm_runtime_resume(struct device *dev)
3528{
3529 struct sdhci_host *host = dev_get_drvdata(dev);
3530 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3531 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003532 ktime_t start = ktime_get();
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003533
3534 enable_irq(msm_host->pwr_irq);
3535 enable_irq(host->irq);
3536
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003537 trace_sdhci_msm_runtime_resume(mmc_hostname(host->mmc), 0,
3538 ktime_to_us(ktime_sub(ktime_get(), start)));
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003539 return 0;
3540}
3541
3542static int sdhci_msm_suspend(struct device *dev)
3543{
3544 struct sdhci_host *host = dev_get_drvdata(dev);
3545 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3546 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003547 int ret = 0;
3548 ktime_t start = ktime_get();
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003549
3550 if (gpio_is_valid(msm_host->pdata->status_gpio) &&
3551 (msm_host->mmc->slot.cd_irq >= 0))
3552 disable_irq(msm_host->mmc->slot.cd_irq);
3553
3554 if (pm_runtime_suspended(dev)) {
3555 pr_debug("%s: %s: already runtime suspended\n",
3556 mmc_hostname(host->mmc), __func__);
3557 goto out;
3558 }
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003559 ret = sdhci_msm_runtime_suspend(dev);
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003560out:
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003561 trace_sdhci_msm_suspend(mmc_hostname(host->mmc), ret,
3562 ktime_to_us(ktime_sub(ktime_get(), start)));
3563 return ret;
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003564}
3565
3566static int sdhci_msm_resume(struct device *dev)
3567{
3568 struct sdhci_host *host = dev_get_drvdata(dev);
3569 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3570 struct sdhci_msm_host *msm_host = pltfm_host->priv;
3571 int ret = 0;
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003572 ktime_t start = ktime_get();
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003573
3574 if (gpio_is_valid(msm_host->pdata->status_gpio) &&
3575 (msm_host->mmc->slot.cd_irq >= 0))
3576 enable_irq(msm_host->mmc->slot.cd_irq);
3577
3578 if (pm_runtime_suspended(dev)) {
3579 pr_debug("%s: %s: runtime suspended, defer system resume\n",
3580 mmc_hostname(host->mmc), __func__);
3581 goto out;
3582 }
3583
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003584 ret = sdhci_msm_runtime_resume(dev);
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003585out:
Konstantin Dorfmanddab0cc2015-02-25 16:23:50 +02003586 trace_sdhci_msm_resume(mmc_hostname(host->mmc), ret,
3587 ktime_to_us(ktime_sub(ktime_get(), start)));
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003588 return ret;
3589}
3590
3591static const struct dev_pm_ops sdhci_msm_pmops = {
3592 SET_SYSTEM_SLEEP_PM_OPS(sdhci_msm_suspend, sdhci_msm_resume)
3593 SET_RUNTIME_PM_OPS(sdhci_msm_runtime_suspend, sdhci_msm_runtime_resume,
3594 NULL)
3595};
3596
3597#define SDHCI_MSM_PMOPS (&sdhci_msm_pmops)
3598
3599#else
3600#define SDHCI_MSM_PMOPS NULL
3601#endif
Asutosh Das0ef24812012-12-18 16:14:02 +05303602static const struct of_device_id sdhci_msm_dt_match[] = {
3603 {.compatible = "qcom,sdhci-msm"},
Venkat Gopalakrishnan272ba402015-06-25 12:00:02 -07003604 {},
Asutosh Das0ef24812012-12-18 16:14:02 +05303605};
3606MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match);
3607
3608static struct platform_driver sdhci_msm_driver = {
3609 .probe = sdhci_msm_probe,
3610 .remove = sdhci_msm_remove,
3611 .driver = {
3612 .name = "sdhci_msm",
3613 .owner = THIS_MODULE,
3614 .of_match_table = sdhci_msm_dt_match,
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003615 .pm = SDHCI_MSM_PMOPS,
Asutosh Das0ef24812012-12-18 16:14:02 +05303616 },
3617};
3618
3619module_platform_driver(sdhci_msm_driver);
3620
3621MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Secure Digital Host Controller Interface driver");
3622MODULE_LICENSE("GPL v2");