blob: 6737b7dba09a974ce0ebfaa47205ba2e466761f7 [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>
34#include <linux/mmc/mmc.h>
Sahitya Tummala581df132013-03-12 14:57:46 +053035#include <linux/mmc/slot-gpio.h>
Sahitya Tummalaeaa21862013-03-20 19:34:59 +053036#include <linux/dma-mapping.h>
Sahitya Tummala66b0fe32013-04-25 11:50:56 +053037#include <linux/iopoll.h>
Pratibhasagar V9acf2642013-11-21 21:07:21 +053038#include <linux/pinctrl/consumer.h>
39#include <linux/iopoll.h>
Sahitya Tummala8a3e8182013-03-10 14:12:52 +053040#include <linux/msm-bus.h>
Asutosh Das0ef24812012-12-18 16:14:02 +053041
42#include "sdhci-pltfm.h"
43
Venkat Gopalakrishnan0a92d532014-12-16 17:31:00 -080044#define CORE_POWER 0x0
45#define CORE_SW_RST (1 << 7)
46
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -070047#define SDHCI_VER_100 0x2B
Venkat Gopalakrishnan0a92d532014-12-16 17:31:00 -080048#define CORE_MCI_DATA_CNT 0x30
49#define CORE_MCI_STATUS 0x34
50#define CORE_MCI_FIFO_CNT 0x44
51
52#define CORE_VERSION_STEP_MASK 0x0000FFFF
53#define CORE_VERSION_MINOR_MASK 0x0FFF0000
54#define CORE_VERSION_MINOR_SHIFT 16
55#define CORE_VERSION_MAJOR_MASK 0xF0000000
56#define CORE_VERSION_MAJOR_SHIFT 28
57#define CORE_VERSION_TARGET_MASK 0x000000FF
58
59#define CORE_GENERICS 0x70
60#define SWITCHABLE_SIGNALLING_VOL (1 << 29)
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +053061
62#define CORE_VERSION_MAJOR_MASK 0xF0000000
63#define CORE_VERSION_MAJOR_SHIFT 28
64
Asutosh Das0ef24812012-12-18 16:14:02 +053065#define CORE_HC_MODE 0x78
66#define HC_MODE_EN 0x1
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -070067#define FF_CLK_SW_RST_DIS (1 << 13)
Asutosh Das0ef24812012-12-18 16:14:02 +053068
Sahitya Tummala67717bc2013-08-02 09:21:37 +053069#define CORE_MCI_VERSION 0x050
70#define CORE_TESTBUS_CONFIG 0x0CC
71#define CORE_TESTBUS_ENA (1 << 3)
Venkat Gopalakrishnan0a92d532014-12-16 17:31:00 -080072#define CORE_TESTBUS_SEL2_BIT 4
73#define CORE_TESTBUS_SEL2 (1 << CORE_TESTBUS_SEL2_BIT)
Sahitya Tummala67717bc2013-08-02 09:21:37 +053074
Asutosh Das0ef24812012-12-18 16:14:02 +053075#define CORE_PWRCTL_STATUS 0xDC
76#define CORE_PWRCTL_MASK 0xE0
77#define CORE_PWRCTL_CLEAR 0xE4
78#define CORE_PWRCTL_CTL 0xE8
79
80#define CORE_PWRCTL_BUS_OFF 0x01
81#define CORE_PWRCTL_BUS_ON (1 << 1)
82#define CORE_PWRCTL_IO_LOW (1 << 2)
83#define CORE_PWRCTL_IO_HIGH (1 << 3)
84
85#define CORE_PWRCTL_BUS_SUCCESS 0x01
86#define CORE_PWRCTL_BUS_FAIL (1 << 1)
87#define CORE_PWRCTL_IO_SUCCESS (1 << 2)
88#define CORE_PWRCTL_IO_FAIL (1 << 3)
89
90#define INT_MASK 0xF
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -070091#define MAX_PHASES 16
92
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -070093#define CORE_DLL_CONFIG 0x100
94#define CORE_CMD_DAT_TRACK_SEL (1 << 0)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -070095#define CORE_DLL_EN (1 << 16)
96#define CORE_CDR_EN (1 << 17)
97#define CORE_CK_OUT_EN (1 << 18)
98#define CORE_CDR_EXT_EN (1 << 19)
99#define CORE_DLL_PDN (1 << 29)
100#define CORE_DLL_RST (1 << 30)
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700101
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700102#define CORE_DLL_STATUS 0x108
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700103#define CORE_DLL_LOCK (1 << 7)
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700104#define CORE_DDR_DLL_LOCK (1 << 11)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700105
106#define CORE_VENDOR_SPEC 0x10C
107#define CORE_CLK_PWRSAVE (1 << 1)
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700108#define CORE_HC_MCLK_SEL_DFLT (2 << 8)
109#define CORE_HC_MCLK_SEL_HS400 (3 << 8)
110#define CORE_HC_MCLK_SEL_MASK (3 << 8)
Asutosh Dase5e9ca62013-07-30 19:08:36 +0530111#define CORE_HC_AUTO_CMD21_EN (1 << 6)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700112#define CORE_IO_PAD_PWR_SWITCH (1 << 16)
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700113#define CORE_HC_SELECT_IN_EN (1 << 18)
114#define CORE_HC_SELECT_IN_HS400 (6 << 19)
115#define CORE_HC_SELECT_IN_MASK (7 << 19)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700116
Venkat Gopalakrishnan0a92d532014-12-16 17:31:00 -0800117#define CORE_VENDOR_SPEC_ADMA_ERR_ADDR0 0x114
118#define CORE_VENDOR_SPEC_ADMA_ERR_ADDR1 0x118
119
Krishna Konda7feab352013-09-17 23:55:40 -0700120#define CORE_VENDOR_SPEC_CAPABILITIES0 0x11C
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +0530121#define CORE_8_BIT_SUPPORT (1 << 18)
122#define CORE_3_3V_SUPPORT (1 << 24)
123#define CORE_3_0V_SUPPORT (1 << 25)
124#define CORE_1_8V_SUPPORT (1 << 26)
Gilad Broner2a10ca02014-10-02 17:20:35 +0300125#define CORE_SYS_BUS_SUPPORT_64_BIT BIT(28)
Krishna Konda7feab352013-09-17 23:55:40 -0700126
Venkat Gopalakrishnan0a92d532014-12-16 17:31:00 -0800127#define CORE_SDCC_DEBUG_REG 0x124
Sahitya Tummala67717bc2013-08-02 09:21:37 +0530128
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700129#define CORE_CSR_CDC_CTLR_CFG0 0x130
130#define CORE_SW_TRIG_FULL_CALIB (1 << 16)
131#define CORE_HW_AUTOCAL_ENA (1 << 17)
132
133#define CORE_CSR_CDC_CTLR_CFG1 0x134
134#define CORE_CSR_CDC_CAL_TIMER_CFG0 0x138
135#define CORE_TIMER_ENA (1 << 16)
136
137#define CORE_CSR_CDC_CAL_TIMER_CFG1 0x13C
138#define CORE_CSR_CDC_REFCOUNT_CFG 0x140
139#define CORE_CSR_CDC_COARSE_CAL_CFG 0x144
140#define CORE_CDC_OFFSET_CFG 0x14C
141#define CORE_CSR_CDC_DELAY_CFG 0x150
142#define CORE_CDC_SLAVE_DDA_CFG 0x160
143#define CORE_CSR_CDC_STATUS0 0x164
144#define CORE_CALIBRATION_DONE (1 << 0)
145
146#define CORE_CDC_ERROR_CODE_MASK 0x7000000
147
148#define CORE_CSR_CDC_GEN_CFG 0x178
149#define CORE_CDC_SWITCH_BYPASS_OFF (1 << 0)
150#define CORE_CDC_SWITCH_RC_EN (1 << 1)
151
152#define CORE_DDR_200_CFG 0x184
153#define CORE_CDC_T4_DLY_SEL (1 << 0)
154#define CORE_START_CDC_TRAFFIC (1 << 6)
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700155#define CORE_VENDOR_SPEC3 0x1B0
156#define CORE_PWRSAVE_DLL (1 << 3)
157
158#define CORE_DLL_CONFIG_2 0x1B4
159#define CORE_DDR_CAL_EN (1 << 0)
160
161#define CORE_DDR_CONFIG 0x1B8
162#define DDR_CONFIG_POR_VAL 0x80040853
163
Venkat Gopalakrishnan450745e2014-07-24 20:39:34 -0700164/* 512 descriptors */
165#define SDHCI_MSM_MAX_SEGMENTS (1 << 9)
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +0530166#define SDHCI_MSM_MMC_CLK_GATE_DELAY 200 /* msecs */
Asutosh Das648f9d12013-01-10 21:11:04 +0530167
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700168#define CORE_FREQ_100MHZ (100 * 1000 * 1000)
169
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700170#define INVALID_TUNING_PHASE -1
171
Krishna Konda96e6b112013-10-28 15:25:03 -0700172#define NUM_TUNING_PHASES 16
173#define MAX_DRV_TYPES_SUPPORTED_HS200 3
174
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700175static const u32 tuning_block_64[] = {
176 0x00FF0FFF, 0xCCC3CCFF, 0xFFCC3CC3, 0xEFFEFFFE,
177 0xDDFFDFFF, 0xFBFFFBFF, 0xFF7FFFBF, 0xEFBDF777,
178 0xF0FFF0FF, 0x3CCCFC0F, 0xCFCC33CC, 0xEEFFEFFF,
179 0xFDFFFDFF, 0xFFBFFFDF, 0xFFF7FFBB, 0xDE7B7FF7
180};
181
182static const u32 tuning_block_128[] = {
183 0xFF00FFFF, 0x0000FFFF, 0xCCCCFFFF, 0xCCCC33CC,
184 0xCC3333CC, 0xFFFFCCCC, 0xFFFFEEFF, 0xFFEEEEFF,
185 0xFFDDFFFF, 0xDDDDFFFF, 0xBBFFFFFF, 0xBBFFFFFF,
186 0xFFFFFFBB, 0xFFFFFF77, 0x77FF7777, 0xFFEEDDBB,
187 0x00FFFFFF, 0x00FFFFFF, 0xCCFFFF00, 0xCC33CCCC,
188 0x3333CCCC, 0xFFCCCCCC, 0xFFEEFFFF, 0xEEEEFFFF,
189 0xDDFFFFFF, 0xDDFFFFFF, 0xFFFFFFDD, 0xFFFFFFBB,
190 0xFFFFBBBB, 0xFFFF77FF, 0xFF7777FF, 0xEEDDBB77
191};
Asutosh Das0ef24812012-12-18 16:14:02 +0530192
Venkat Gopalakrishnan270580a2013-03-11 12:17:57 -0700193static int disable_slots;
194/* root can write, others read */
195module_param(disable_slots, int, S_IRUGO|S_IWUSR);
196
Asutosh Das0ef24812012-12-18 16:14:02 +0530197/* This structure keeps information per regulator */
198struct sdhci_msm_reg_data {
199 /* voltage regulator handle */
200 struct regulator *reg;
201 /* regulator name */
202 const char *name;
203 /* voltage level to be set */
204 u32 low_vol_level;
205 u32 high_vol_level;
206 /* Load values for low power and high power mode */
207 u32 lpm_uA;
208 u32 hpm_uA;
209
210 /* is this regulator enabled? */
211 bool is_enabled;
212 /* is this regulator needs to be always on? */
213 bool is_always_on;
214 /* is low power mode setting required for this regulator? */
215 bool lpm_sup;
216 bool set_voltage_sup;
217};
218
Asutosh Das598a4d42014-02-05 16:38:23 +0530219#define MSM_MMC_DEFAULT_CPU_DMA_LATENCY 200 /* usecs */
Asutosh Das0ef24812012-12-18 16:14:02 +0530220/*
221 * This structure keeps information for all the
222 * regulators required for a SDCC slot.
223 */
224struct sdhci_msm_slot_reg_data {
225 /* keeps VDD/VCC regulator info */
226 struct sdhci_msm_reg_data *vdd_data;
227 /* keeps VDD IO regulator info */
228 struct sdhci_msm_reg_data *vdd_io_data;
229};
230
231struct sdhci_msm_gpio {
232 u32 no;
233 const char *name;
234 bool is_enabled;
235};
236
237struct sdhci_msm_gpio_data {
238 struct sdhci_msm_gpio *gpio;
239 u8 size;
240};
241
242struct sdhci_msm_pin_data {
243 /*
244 * = 1 if controller pins are using gpios
245 * = 0 if controller has dedicated MSM pads
246 */
Sahitya Tummala1cd7e072014-02-14 13:19:01 +0530247 u8 is_gpio;
Asutosh Das0ef24812012-12-18 16:14:02 +0530248 struct sdhci_msm_gpio_data *gpio_data;
249};
250
Pratibhasagar V9acf2642013-11-21 21:07:21 +0530251struct sdhci_pinctrl_data {
252 struct pinctrl *pctrl;
253 struct pinctrl_state *pins_active;
254 struct pinctrl_state *pins_sleep;
255};
256
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530257struct sdhci_msm_bus_voting_data {
258 struct msm_bus_scale_pdata *bus_pdata;
259 unsigned int *bw_vecs;
260 unsigned int bw_vecs_size;
261};
262
Asutosh Das0ef24812012-12-18 16:14:02 +0530263struct sdhci_msm_pltfm_data {
264 /* Supported UHS-I Modes */
265 u32 caps;
266
267 /* More capabilities */
268 u32 caps2;
269
270 unsigned long mmc_bus_width;
Asutosh Das0ef24812012-12-18 16:14:02 +0530271 struct sdhci_msm_slot_reg_data *vreg_data;
272 bool nonremovable;
Guoping Yuf7c91332014-08-20 16:56:18 +0800273 bool nonhotplug;
Pratibhasagar V9acf2642013-11-21 21:07:21 +0530274 bool pin_cfg_sts;
Asutosh Das0ef24812012-12-18 16:14:02 +0530275 struct sdhci_msm_pin_data *pin_data;
Pratibhasagar V9acf2642013-11-21 21:07:21 +0530276 struct sdhci_pinctrl_data *pctrl_data;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +0530277 u32 cpu_dma_latency_us;
Sahitya Tummala581df132013-03-12 14:57:46 +0530278 int status_gpio; /* card detection GPIO that is configured as IRQ */
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530279 struct sdhci_msm_bus_voting_data *voting_data;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530280 u32 *sup_clk_table;
281 unsigned char sup_clk_cnt;
Maya Erez994cf2f2014-10-21 20:22:04 +0300282 enum pm_qos_req_type cpu_affinity_type;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530283};
284
285struct sdhci_msm_bus_vote {
286 uint32_t client_handle;
287 uint32_t curr_vote;
288 int min_bw_vote;
289 int max_bw_vote;
290 bool is_max_bw_needed;
291 struct delayed_work vote_work;
292 struct device_attribute max_bus_bw;
Asutosh Das0ef24812012-12-18 16:14:02 +0530293};
294
295struct sdhci_msm_host {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530296 struct platform_device *pdev;
Asutosh Das0ef24812012-12-18 16:14:02 +0530297 void __iomem *core_mem; /* MSM SDCC mapped address */
298 struct clk *clk; /* main SD/MMC bus clock */
299 struct clk *pclk; /* SDHC peripheral bus clock */
300 struct clk *bus_clk; /* SDHC bus voter clock */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700301 struct clk *ff_clk; /* CDC calibration fixed feedback clock */
302 struct clk *sleep_clk; /* CDC calibration sleep clock */
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +0530303 atomic_t clks_on; /* Set if clocks are enabled */
Asutosh Das0ef24812012-12-18 16:14:02 +0530304 struct sdhci_msm_pltfm_data *pdata;
305 struct mmc_host *mmc;
306 struct sdhci_pltfm_data sdhci_msm_pdata;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +0530307 u32 curr_pwr_state;
308 u32 curr_io_level;
309 struct completion pwr_irq_completion;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530310 struct sdhci_msm_bus_vote msm_bus_vote;
Sahitya Tummala5c55b932013-06-20 14:00:18 +0530311 struct device_attribute polling;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530312 u32 clk_rate; /* Keeps track of current clock rate that is set */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700313 bool tuning_done;
314 bool calibration_done;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700315 u8 saved_tuning_phase;
Asutosh Dase5e9ca62013-07-30 19:08:36 +0530316 bool en_auto_cmd21;
317 struct device_attribute auto_cmd21_attr;
Asutosh Das9d7ee2f2013-11-08 12:33:47 +0530318 atomic_t controller_clock;
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700319 bool use_cdclp533;
Asutosh Das0ef24812012-12-18 16:14:02 +0530320};
321
322enum vdd_io_level {
323 /* set vdd_io_data->low_vol_level */
324 VDD_IO_LOW,
325 /* set vdd_io_data->high_vol_level */
326 VDD_IO_HIGH,
327 /*
328 * set whatever there in voltage_level (third argument) of
329 * sdhci_msm_set_vdd_io_vol() function.
330 */
331 VDD_IO_SET_LEVEL,
332};
333
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700334/* MSM platform specific tuning */
335static inline int msm_dll_poll_ck_out_en(struct sdhci_host *host,
336 u8 poll)
337{
338 int rc = 0;
339 u32 wait_cnt = 50;
340 u8 ck_out_en = 0;
341 struct mmc_host *mmc = host->mmc;
342
343 /* poll for CK_OUT_EN bit. max. poll time = 50us */
344 ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) &
345 CORE_CK_OUT_EN);
346
347 while (ck_out_en != poll) {
348 if (--wait_cnt == 0) {
349 pr_err("%s: %s: CK_OUT_EN bit is not %d\n",
350 mmc_hostname(mmc), __func__, poll);
351 rc = -ETIMEDOUT;
352 goto out;
353 }
354 udelay(1);
355
356 ck_out_en = !!(readl_relaxed(host->ioaddr +
357 CORE_DLL_CONFIG) & CORE_CK_OUT_EN);
358 }
359out:
360 return rc;
361}
362
Asutosh Dase5e9ca62013-07-30 19:08:36 +0530363/*
364 * Enable CDR to track changes of DAT lines and adjust sampling
365 * point according to voltage/temperature variations
366 */
367static int msm_enable_cdr_cm_sdc4_dll(struct sdhci_host *host)
368{
369 int rc = 0;
370 u32 config;
371
372 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
373 config |= CORE_CDR_EN;
374 config &= ~(CORE_CDR_EXT_EN | CORE_CK_OUT_EN);
375 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
376
377 rc = msm_dll_poll_ck_out_en(host, 0);
378 if (rc)
379 goto err;
380
381 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) |
382 CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
383
384 rc = msm_dll_poll_ck_out_en(host, 1);
385 if (rc)
386 goto err;
387 goto out;
388err:
389 pr_err("%s: %s: failed\n", mmc_hostname(host->mmc), __func__);
390out:
391 return rc;
392}
393
394static ssize_t store_auto_cmd21(struct device *dev, struct device_attribute
395 *attr, const char *buf, size_t count)
396{
397 struct sdhci_host *host = dev_get_drvdata(dev);
398 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
399 struct sdhci_msm_host *msm_host = pltfm_host->priv;
400 u32 tmp;
401 unsigned long flags;
402
403 if (!kstrtou32(buf, 0, &tmp)) {
404 spin_lock_irqsave(&host->lock, flags);
405 msm_host->en_auto_cmd21 = !!tmp;
406 spin_unlock_irqrestore(&host->lock, flags);
407 }
408 return count;
409}
410
411static ssize_t show_auto_cmd21(struct device *dev,
412 struct device_attribute *attr, char *buf)
413{
414 struct sdhci_host *host = dev_get_drvdata(dev);
415 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
416 struct sdhci_msm_host *msm_host = pltfm_host->priv;
417
418 return snprintf(buf, PAGE_SIZE, "%d\n", msm_host->en_auto_cmd21);
419}
420
421/* MSM auto-tuning handler */
422static int sdhci_msm_config_auto_tuning_cmd(struct sdhci_host *host,
423 bool enable,
424 u32 type)
425{
426 int rc = 0;
427 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
428 struct sdhci_msm_host *msm_host = pltfm_host->priv;
429 u32 val = 0;
430
431 if (!msm_host->en_auto_cmd21)
432 return 0;
433
434 if (type == MMC_SEND_TUNING_BLOCK_HS200)
435 val = CORE_HC_AUTO_CMD21_EN;
436 else
437 return 0;
438
439 if (enable) {
440 rc = msm_enable_cdr_cm_sdc4_dll(host);
441 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
442 val, host->ioaddr + CORE_VENDOR_SPEC);
443 } else {
444 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
445 ~val, host->ioaddr + CORE_VENDOR_SPEC);
446 }
447 return rc;
448}
449
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700450static int msm_config_cm_dll_phase(struct sdhci_host *host, u8 phase)
451{
452 int rc = 0;
453 u8 grey_coded_phase_table[] = {0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4,
454 0xC, 0xD, 0xF, 0xE, 0xA, 0xB, 0x9,
455 0x8};
456 unsigned long flags;
457 u32 config;
458 struct mmc_host *mmc = host->mmc;
459
460 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
461 spin_lock_irqsave(&host->lock, flags);
462
463 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
464 config &= ~(CORE_CDR_EN | CORE_CK_OUT_EN);
465 config |= (CORE_CDR_EXT_EN | CORE_DLL_EN);
466 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
467
468 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '0' */
469 rc = msm_dll_poll_ck_out_en(host, 0);
470 if (rc)
471 goto err_out;
472
473 /*
474 * Write the selected DLL clock output phase (0 ... 15)
475 * to CDR_SELEXT bit field of DLL_CONFIG register.
476 */
477 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
478 & ~(0xF << 20))
479 | (grey_coded_phase_table[phase] << 20)),
480 host->ioaddr + CORE_DLL_CONFIG);
481
482 /* Set CK_OUT_EN bit of DLL_CONFIG register to 1. */
483 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
484 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
485
486 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '1' */
487 rc = msm_dll_poll_ck_out_en(host, 1);
488 if (rc)
489 goto err_out;
490
491 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
492 config |= CORE_CDR_EN;
493 config &= ~CORE_CDR_EXT_EN;
494 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
495 goto out;
496
497err_out:
498 pr_err("%s: %s: Failed to set DLL phase: %d\n",
499 mmc_hostname(mmc), __func__, phase);
500out:
501 spin_unlock_irqrestore(&host->lock, flags);
502 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
503 return rc;
504}
505
506/*
507 * Find out the greatest range of consecuitive selected
508 * DLL clock output phases that can be used as sampling
509 * setting for SD3.0 UHS-I card read operation (in SDR104
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700510 * timing mode) or for eMMC4.5 card read operation (in
511 * HS400/HS200 timing mode).
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700512 * Select the 3/4 of the range and configure the DLL with the
513 * selected DLL clock output phase.
514 */
515
516static int msm_find_most_appropriate_phase(struct sdhci_host *host,
517 u8 *phase_table, u8 total_phases)
518{
519 int ret;
520 u8 ranges[MAX_PHASES][MAX_PHASES] = { {0}, {0} };
521 u8 phases_per_row[MAX_PHASES] = {0};
522 int row_index = 0, col_index = 0, selected_row_index = 0, curr_max = 0;
523 int i, cnt, phase_0_raw_index = 0, phase_15_raw_index = 0;
524 bool phase_0_found = false, phase_15_found = false;
525 struct mmc_host *mmc = host->mmc;
526
527 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
528 if (!total_phases || (total_phases > MAX_PHASES)) {
529 pr_err("%s: %s: invalid argument: total_phases=%d\n",
530 mmc_hostname(mmc), __func__, total_phases);
531 return -EINVAL;
532 }
533
534 for (cnt = 0; cnt < total_phases; cnt++) {
535 ranges[row_index][col_index] = phase_table[cnt];
536 phases_per_row[row_index] += 1;
537 col_index++;
538
539 if ((cnt + 1) == total_phases) {
540 continue;
541 /* check if next phase in phase_table is consecutive or not */
542 } else if ((phase_table[cnt] + 1) != phase_table[cnt + 1]) {
543 row_index++;
544 col_index = 0;
545 }
546 }
547
548 if (row_index >= MAX_PHASES)
549 return -EINVAL;
550
551 /* Check if phase-0 is present in first valid window? */
552 if (!ranges[0][0]) {
553 phase_0_found = true;
554 phase_0_raw_index = 0;
555 /* Check if cycle exist between 2 valid windows */
556 for (cnt = 1; cnt <= row_index; cnt++) {
557 if (phases_per_row[cnt]) {
558 for (i = 0; i < phases_per_row[cnt]; i++) {
559 if (ranges[cnt][i] == 15) {
560 phase_15_found = true;
561 phase_15_raw_index = cnt;
562 break;
563 }
564 }
565 }
566 }
567 }
568
569 /* If 2 valid windows form cycle then merge them as single window */
570 if (phase_0_found && phase_15_found) {
571 /* number of phases in raw where phase 0 is present */
572 u8 phases_0 = phases_per_row[phase_0_raw_index];
573 /* number of phases in raw where phase 15 is present */
574 u8 phases_15 = phases_per_row[phase_15_raw_index];
575
576 if (phases_0 + phases_15 >= MAX_PHASES)
577 /*
578 * If there are more than 1 phase windows then total
579 * number of phases in both the windows should not be
580 * more than or equal to MAX_PHASES.
581 */
582 return -EINVAL;
583
584 /* Merge 2 cyclic windows */
585 i = phases_15;
586 for (cnt = 0; cnt < phases_0; cnt++) {
587 ranges[phase_15_raw_index][i] =
588 ranges[phase_0_raw_index][cnt];
589 if (++i >= MAX_PHASES)
590 break;
591 }
592
593 phases_per_row[phase_0_raw_index] = 0;
594 phases_per_row[phase_15_raw_index] = phases_15 + phases_0;
595 }
596
597 for (cnt = 0; cnt <= row_index; cnt++) {
598 if (phases_per_row[cnt] > curr_max) {
599 curr_max = phases_per_row[cnt];
600 selected_row_index = cnt;
601 }
602 }
603
604 i = ((curr_max * 3) / 4);
605 if (i)
606 i--;
607
608 ret = (int)ranges[selected_row_index][i];
609
610 if (ret >= MAX_PHASES) {
611 ret = -EINVAL;
612 pr_err("%s: %s: invalid phase selected=%d\n",
613 mmc_hostname(mmc), __func__, ret);
614 }
615
616 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
617 return ret;
618}
619
620static inline void msm_cm_dll_set_freq(struct sdhci_host *host)
621{
622 u32 mclk_freq = 0;
623
624 /* Program the MCLK value to MCLK_FREQ bit field */
625 if (host->clock <= 112000000)
626 mclk_freq = 0;
627 else if (host->clock <= 125000000)
628 mclk_freq = 1;
629 else if (host->clock <= 137000000)
630 mclk_freq = 2;
631 else if (host->clock <= 150000000)
632 mclk_freq = 3;
633 else if (host->clock <= 162000000)
634 mclk_freq = 4;
635 else if (host->clock <= 175000000)
636 mclk_freq = 5;
637 else if (host->clock <= 187000000)
638 mclk_freq = 6;
639 else if (host->clock <= 200000000)
640 mclk_freq = 7;
641
642 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
643 & ~(7 << 24)) | (mclk_freq << 24)),
644 host->ioaddr + CORE_DLL_CONFIG);
645}
646
647/* Initialize the DLL (Programmable Delay Line ) */
648static int msm_init_cm_dll(struct sdhci_host *host)
649{
650 struct mmc_host *mmc = host->mmc;
651 int rc = 0;
652 unsigned long flags;
653 u32 wait_cnt;
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530654 bool prev_pwrsave, curr_pwrsave;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700655
656 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
657 spin_lock_irqsave(&host->lock, flags);
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530658 prev_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
659 CORE_CLK_PWRSAVE);
660 curr_pwrsave = prev_pwrsave;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700661 /*
662 * Make sure that clock is always enabled when DLL
663 * tuning is in progress. Keeping PWRSAVE ON may
664 * turn off the clock. So let's disable the PWRSAVE
665 * here and re-enable it once tuning is completed.
666 */
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530667 if (prev_pwrsave) {
668 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
669 & ~CORE_CLK_PWRSAVE),
670 host->ioaddr + CORE_VENDOR_SPEC);
671 curr_pwrsave = false;
672 }
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700673
674 /* Write 1 to DLL_RST bit of DLL_CONFIG register */
675 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
676 | CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
677
678 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
679 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
680 | CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
681 msm_cm_dll_set_freq(host);
682
683 /* Write 0 to DLL_RST bit of DLL_CONFIG register */
684 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
685 & ~CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
686
687 /* Write 0 to DLL_PDN bit of DLL_CONFIG register */
688 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
689 & ~CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
690
691 /* Set DLL_EN bit to 1. */
692 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
693 | CORE_DLL_EN), host->ioaddr + CORE_DLL_CONFIG);
694
695 /* Set CK_OUT_EN bit to 1. */
696 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
697 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
698
699 wait_cnt = 50;
700 /* Wait until DLL_LOCK bit of DLL_STATUS register becomes '1' */
701 while (!(readl_relaxed(host->ioaddr + CORE_DLL_STATUS) &
702 CORE_DLL_LOCK)) {
703 /* max. wait for 50us sec for LOCK bit to be set */
704 if (--wait_cnt == 0) {
705 pr_err("%s: %s: DLL failed to LOCK\n",
706 mmc_hostname(mmc), __func__);
707 rc = -ETIMEDOUT;
708 goto out;
709 }
710 /* wait for 1us before polling again */
711 udelay(1);
712 }
713
714out:
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530715 /* Restore the correct PWRSAVE state */
716 if (prev_pwrsave ^ curr_pwrsave) {
717 u32 reg = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC);
718
719 if (prev_pwrsave)
720 reg |= CORE_CLK_PWRSAVE;
721 else
722 reg &= ~CORE_CLK_PWRSAVE;
723
724 writel_relaxed(reg, host->ioaddr + CORE_VENDOR_SPEC);
725 }
726
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700727 spin_unlock_irqrestore(&host->lock, flags);
728 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
729 return rc;
730}
731
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700732static int sdhci_msm_cdclp533_calibration(struct sdhci_host *host)
733{
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700734 u32 calib_done;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700735 int ret = 0;
736 int cdc_err = 0;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700737
738 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
739
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700740 /* Write 0 to CDC_T4_DLY_SEL field in VENDOR_SPEC_DDR200_CFG */
741 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
742 & ~CORE_CDC_T4_DLY_SEL),
743 host->ioaddr + CORE_DDR_200_CFG);
744
745 /* Write 0 to CDC_SWITCH_BYPASS_OFF field in CORE_CSR_CDC_GEN_CFG */
746 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG)
747 & ~CORE_CDC_SWITCH_BYPASS_OFF),
748 host->ioaddr + CORE_CSR_CDC_GEN_CFG);
749
750 /* Write 1 to CDC_SWITCH_RC_EN field in CORE_CSR_CDC_GEN_CFG */
751 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG)
752 | CORE_CDC_SWITCH_RC_EN),
753 host->ioaddr + CORE_CSR_CDC_GEN_CFG);
754
755 /* Write 0 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
756 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
757 & ~CORE_START_CDC_TRAFFIC),
758 host->ioaddr + CORE_DDR_200_CFG);
759
760 /*
761 * Perform CDC Register Initialization Sequence
762 *
763 * CORE_CSR_CDC_CTLR_CFG0 0x11800EC
764 * CORE_CSR_CDC_CTLR_CFG1 0x3011111
765 * CORE_CSR_CDC_CAL_TIMER_CFG0 0x1201000
766 * CORE_CSR_CDC_CAL_TIMER_CFG1 0x4
767 * CORE_CSR_CDC_REFCOUNT_CFG 0xCB732020
768 * CORE_CSR_CDC_COARSE_CAL_CFG 0xB19
769 * CORE_CSR_CDC_DELAY_CFG 0x3AC
770 * CORE_CDC_OFFSET_CFG 0x0
771 * CORE_CDC_SLAVE_DDA_CFG 0x16334
772 */
773
774 writel_relaxed(0x11800EC, host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
775 writel_relaxed(0x3011111, host->ioaddr + CORE_CSR_CDC_CTLR_CFG1);
776 writel_relaxed(0x1201000, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
777 writel_relaxed(0x4, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG1);
778 writel_relaxed(0xCB732020, host->ioaddr + CORE_CSR_CDC_REFCOUNT_CFG);
779 writel_relaxed(0xB19, host->ioaddr + CORE_CSR_CDC_COARSE_CAL_CFG);
Subhash Jadavanibe406d92014-06-17 16:47:48 -0700780 writel_relaxed(0x4E2, host->ioaddr + CORE_CSR_CDC_DELAY_CFG);
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700781 writel_relaxed(0x0, host->ioaddr + CORE_CDC_OFFSET_CFG);
782 writel_relaxed(0x16334, host->ioaddr + CORE_CDC_SLAVE_DDA_CFG);
783
784 /* CDC HW Calibration */
785
786 /* Write 1 to SW_TRIG_FULL_CALIB field in CORE_CSR_CDC_CTLR_CFG0 */
787 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
788 | CORE_SW_TRIG_FULL_CALIB),
789 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
790
791 /* Write 0 to SW_TRIG_FULL_CALIB field in CORE_CSR_CDC_CTLR_CFG0 */
792 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
793 & ~CORE_SW_TRIG_FULL_CALIB),
794 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
795
796 /* Write 1 to HW_AUTOCAL_ENA field in CORE_CSR_CDC_CTLR_CFG0 */
797 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
798 | CORE_HW_AUTOCAL_ENA),
799 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
800
801 /* Write 1 to TIMER_ENA field in CORE_CSR_CDC_CAL_TIMER_CFG0 */
802 writel_relaxed((readl_relaxed(host->ioaddr +
803 CORE_CSR_CDC_CAL_TIMER_CFG0) | CORE_TIMER_ENA),
804 host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
805
806 mb();
807
808 /* Poll on CALIBRATION_DONE field in CORE_CSR_CDC_STATUS0 to be 1 */
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700809 ret = readl_poll_timeout(host->ioaddr + CORE_CSR_CDC_STATUS0,
810 calib_done, (calib_done & CORE_CALIBRATION_DONE), 1, 50);
811
812 if (ret == -ETIMEDOUT) {
813 pr_err("%s: %s: CDC Calibration was not completed\n",
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700814 mmc_hostname(host->mmc), __func__);
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700815 goto out;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700816 }
817
818 /* Verify CDC_ERROR_CODE field in CORE_CSR_CDC_STATUS0 is 0 */
819 cdc_err = readl_relaxed(host->ioaddr + CORE_CSR_CDC_STATUS0)
820 & CORE_CDC_ERROR_CODE_MASK;
821 if (cdc_err) {
822 pr_err("%s: %s: CDC Error Code %d\n",
823 mmc_hostname(host->mmc), __func__, cdc_err);
824 ret = -EINVAL;
825 goto out;
826 }
827
828 /* Write 1 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
829 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
830 | CORE_START_CDC_TRAFFIC),
831 host->ioaddr + CORE_DDR_200_CFG);
832out:
833 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
834 __func__, ret);
835 return ret;
836}
837
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700838static int sdhci_msm_cm_dll_sdc4_calibration(struct sdhci_host *host)
839{
840 u32 dll_status;
841 int ret = 0;
842
843 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
844
845 /*
846 * Currently the CORE_DDR_CONFIG register defaults to desired
847 * configuration on reset. Currently reprogramming the power on
848 * reset (POR) value in case it might have been modified by
849 * bootloaders. In the future, if this changes, then the desired
850 * values will need to be programmed appropriately.
851 */
852 writel_relaxed(DDR_CONFIG_POR_VAL, host->ioaddr + CORE_DDR_CONFIG);
853
854 /* Write 1 to DDR_CAL_EN field in CORE_DLL_CONFIG_2 */
855 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
856 | CORE_DDR_CAL_EN),
857 host->ioaddr + CORE_DLL_CONFIG_2);
858
859 /* Poll on DDR_DLL_LOCK bit in CORE_DLL_STATUS to be set */
860 ret = readl_poll_timeout(host->ioaddr + CORE_DLL_STATUS,
861 dll_status, (dll_status & CORE_DDR_DLL_LOCK), 10, 1000);
862
863 if (ret == -ETIMEDOUT) {
864 pr_err("%s: %s: CM_DLL_SDC4 Calibration was not completed\n",
865 mmc_hostname(host->mmc), __func__);
866 goto out;
867 }
868
869 /* set CORE_PWRSAVE_DLL bit in CORE_VENDOR_SPEC3 */
870 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC3)
871 | CORE_PWRSAVE_DLL),
872 host->ioaddr + CORE_VENDOR_SPEC3);
873 mb();
874out:
875 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
876 __func__, ret);
877 return ret;
878}
879
880static int sdhci_msm_hs400_dll_calibration(struct sdhci_host *host)
881{
882 int ret = 0;
883 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
884 struct sdhci_msm_host *msm_host = pltfm_host->priv;
885
886 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
887
888 /*
889 * Retuning in HS400 (DDR mode) will fail, just reset the
890 * tuning block and restore the saved tuning phase.
891 */
892 ret = msm_init_cm_dll(host);
893 if (ret)
894 goto out;
895
896 /* Set the selected phase in delay line hw block */
897 ret = msm_config_cm_dll_phase(host, msm_host->saved_tuning_phase);
898 if (ret)
899 goto out;
900
Krishna Konda0e8efba2014-06-23 14:50:38 -0700901 /* Write 1 to CMD_DAT_TRACK_SEL field in DLL_CONFIG */
902 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
903 | CORE_CMD_DAT_TRACK_SEL),
904 host->ioaddr + CORE_DLL_CONFIG);
905
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700906 if (msm_host->use_cdclp533)
907 /* Calibrate CDCLP533 DLL HW */
908 ret = sdhci_msm_cdclp533_calibration(host);
909 else
910 /* Calibrate CM_DLL_SDC4 HW */
911 ret = sdhci_msm_cm_dll_sdc4_calibration(host);
912out:
913 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
914 __func__, ret);
915 return ret;
916}
917
Krishna Konda96e6b112013-10-28 15:25:03 -0700918static void sdhci_msm_set_mmc_drv_type(struct sdhci_host *host, u32 opcode,
919 u8 drv_type)
920{
921 struct mmc_command cmd = {0};
922 struct mmc_request mrq = {NULL};
923 struct mmc_host *mmc = host->mmc;
924 u8 val = ((drv_type << 4) | 2);
925
926 cmd.opcode = MMC_SWITCH;
927 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
928 (EXT_CSD_HS_TIMING << 16) |
929 (val << 8) |
930 EXT_CSD_CMD_SET_NORMAL;
931 cmd.flags = MMC_CMD_AC | MMC_RSP_R1B;
932 /* 1 sec */
933 cmd.busy_timeout = 1000 * 1000;
934
935 memset(cmd.resp, 0, sizeof(cmd.resp));
936 cmd.retries = 3;
937
938 mrq.cmd = &cmd;
939 cmd.data = NULL;
940
941 mmc_wait_for_req(mmc, &mrq);
942 pr_debug("%s: %s: set card drive type to %d\n",
943 mmc_hostname(mmc), __func__,
944 drv_type);
945}
946
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700947int sdhci_msm_execute_tuning(struct sdhci_host *host, u32 opcode)
948{
949 unsigned long flags;
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530950 int tuning_seq_cnt = 3;
Krishna Konda96e6b112013-10-28 15:25:03 -0700951 u8 phase, *data_buf, tuned_phases[NUM_TUNING_PHASES], tuned_phase_cnt;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700952 const u32 *tuning_block_pattern = tuning_block_64;
953 int size = sizeof(tuning_block_64); /* Tuning pattern size in bytes */
954 int rc;
955 struct mmc_host *mmc = host->mmc;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530956 struct mmc_ios ios = host->mmc->ios;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700957 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
958 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Krishna Konda96e6b112013-10-28 15:25:03 -0700959 u8 drv_type = 0;
960 bool drv_type_changed = false;
961 struct mmc_card *card = host->mmc->card;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530962
963 /*
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700964 * Tuning is required for SDR104, HS200 and HS400 cards and
965 * if clock frequency is greater than 100MHz in these modes.
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530966 */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700967 if (host->clock <= CORE_FREQ_100MHZ ||
968 !((ios.timing == MMC_TIMING_MMC_HS400) ||
969 (ios.timing == MMC_TIMING_MMC_HS200) ||
970 (ios.timing == MMC_TIMING_UHS_SDR104)))
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530971 return 0;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700972
973 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700974
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700975 /* CDC/SDC4 DLL HW calibration is only required for HS400 mode*/
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700976 if (msm_host->tuning_done && !msm_host->calibration_done &&
977 (mmc->ios.timing == MMC_TIMING_MMC_HS400)) {
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700978 rc = sdhci_msm_hs400_dll_calibration(host);
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700979 spin_lock_irqsave(&host->lock, flags);
980 if (!rc)
981 msm_host->calibration_done = true;
982 spin_unlock_irqrestore(&host->lock, flags);
983 goto out;
984 }
985
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700986 spin_lock_irqsave(&host->lock, flags);
987
988 if ((opcode == MMC_SEND_TUNING_BLOCK_HS200) &&
989 (mmc->ios.bus_width == MMC_BUS_WIDTH_8)) {
990 tuning_block_pattern = tuning_block_128;
991 size = sizeof(tuning_block_128);
992 }
993 spin_unlock_irqrestore(&host->lock, flags);
994
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700995 data_buf = kmalloc(size, GFP_KERNEL);
996 if (!data_buf) {
997 rc = -ENOMEM;
998 goto out;
999 }
1000
Sahitya Tummala9fe16532013-06-13 10:36:57 +05301001retry:
Krishna Konda96e6b112013-10-28 15:25:03 -07001002 tuned_phase_cnt = 0;
1003
Sahitya Tummala9fe16532013-06-13 10:36:57 +05301004 /* first of all reset the tuning block */
1005 rc = msm_init_cm_dll(host);
1006 if (rc)
1007 goto kfree;
1008
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001009 phase = 0;
1010 do {
1011 struct mmc_command cmd = {0};
1012 struct mmc_data data = {0};
1013 struct mmc_request mrq = {
1014 .cmd = &cmd,
1015 .data = &data
1016 };
1017 struct scatterlist sg;
1018
1019 /* set the phase in delay line hw block */
1020 rc = msm_config_cm_dll_phase(host, phase);
1021 if (rc)
1022 goto kfree;
1023
1024 cmd.opcode = opcode;
1025 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1026
1027 data.blksz = size;
1028 data.blocks = 1;
1029 data.flags = MMC_DATA_READ;
1030 data.timeout_ns = 1000 * 1000 * 1000; /* 1 sec */
1031
1032 data.sg = &sg;
1033 data.sg_len = 1;
1034 sg_init_one(&sg, data_buf, size);
1035 memset(data_buf, 0, size);
1036 mmc_wait_for_req(mmc, &mrq);
1037
1038 if (!cmd.error && !data.error &&
1039 !memcmp(data_buf, tuning_block_pattern, size)) {
1040 /* tuning is successful at this tuning point */
1041 tuned_phases[tuned_phase_cnt++] = phase;
Krishna Konda96e6b112013-10-28 15:25:03 -07001042 pr_debug("%s: %s: found *** good *** phase = %d\n",
1043 mmc_hostname(mmc), __func__, phase);
1044 } else {
1045 pr_debug("%s: %s: found ## bad ## phase = %d\n",
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001046 mmc_hostname(mmc), __func__, phase);
1047 }
1048 } while (++phase < 16);
1049
Sahitya Tummaladfdb4af2014-04-01 14:29:13 +05301050 if ((tuned_phase_cnt == NUM_TUNING_PHASES) &&
1051 card && mmc_card_mmc(card)) {
Krishna Konda96e6b112013-10-28 15:25:03 -07001052 /*
1053 * If all phases pass then its a problem. So change the card's
1054 * drive type to a different value, if supported and repeat
1055 * tuning until at least one phase fails. Then set the original
1056 * drive type back.
1057 *
1058 * If all the phases still pass after trying all possible
1059 * drive types, then one of those 16 phases will be picked.
1060 * This is no different from what was going on before the
1061 * modification to change drive type and retune.
1062 */
1063 pr_debug("%s: tuned phases count: %d\n", mmc_hostname(mmc),
1064 tuned_phase_cnt);
1065
1066 /* set drive type to other value . default setting is 0x0 */
1067 while (++drv_type <= MAX_DRV_TYPES_SUPPORTED_HS200) {
1068 if (card->ext_csd.raw_driver_strength &
1069 (1 << drv_type)) {
1070 sdhci_msm_set_mmc_drv_type(host, opcode,
1071 drv_type);
1072 if (!drv_type_changed)
1073 drv_type_changed = true;
1074 goto retry;
1075 }
1076 }
1077 }
1078
1079 /* reset drive type to default (50 ohm) if changed */
1080 if (drv_type_changed)
1081 sdhci_msm_set_mmc_drv_type(host, opcode, 0);
1082
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001083 if (tuned_phase_cnt) {
1084 rc = msm_find_most_appropriate_phase(host, tuned_phases,
1085 tuned_phase_cnt);
1086 if (rc < 0)
1087 goto kfree;
1088 else
1089 phase = (u8)rc;
1090
1091 /*
1092 * Finally set the selected phase in delay
1093 * line hw block.
1094 */
1095 rc = msm_config_cm_dll_phase(host, phase);
1096 if (rc)
1097 goto kfree;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07001098 msm_host->saved_tuning_phase = phase;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001099 pr_debug("%s: %s: finally setting the tuning phase to %d\n",
1100 mmc_hostname(mmc), __func__, phase);
1101 } else {
Sahitya Tummala9fe16532013-06-13 10:36:57 +05301102 if (--tuning_seq_cnt)
1103 goto retry;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001104 /* tuning failed */
1105 pr_err("%s: %s: no tuning point found\n",
1106 mmc_hostname(mmc), __func__);
Sahitya Tummala9fe16532013-06-13 10:36:57 +05301107 rc = -EIO;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001108 }
1109
1110kfree:
1111 kfree(data_buf);
1112out:
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07001113 spin_lock_irqsave(&host->lock, flags);
1114 if (!rc)
1115 msm_host->tuning_done = true;
1116 spin_unlock_irqrestore(&host->lock, flags);
1117 pr_debug("%s: Exit %s, err(%d)\n", mmc_hostname(mmc), __func__, rc);
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001118 return rc;
1119}
1120
Asutosh Das0ef24812012-12-18 16:14:02 +05301121static int sdhci_msm_setup_gpio(struct sdhci_msm_pltfm_data *pdata, bool enable)
1122{
1123 struct sdhci_msm_gpio_data *curr;
1124 int i, ret = 0;
1125
1126 curr = pdata->pin_data->gpio_data;
1127 for (i = 0; i < curr->size; i++) {
1128 if (!gpio_is_valid(curr->gpio[i].no)) {
1129 ret = -EINVAL;
1130 pr_err("%s: Invalid gpio = %d\n", __func__,
1131 curr->gpio[i].no);
1132 goto free_gpios;
1133 }
1134 if (enable) {
1135 ret = gpio_request(curr->gpio[i].no,
1136 curr->gpio[i].name);
1137 if (ret) {
1138 pr_err("%s: gpio_request(%d, %s) failed %d\n",
1139 __func__, curr->gpio[i].no,
1140 curr->gpio[i].name, ret);
1141 goto free_gpios;
1142 }
1143 curr->gpio[i].is_enabled = true;
1144 } else {
1145 gpio_free(curr->gpio[i].no);
1146 curr->gpio[i].is_enabled = false;
1147 }
1148 }
1149 return ret;
1150
1151free_gpios:
1152 for (i--; i >= 0; i--) {
1153 gpio_free(curr->gpio[i].no);
1154 curr->gpio[i].is_enabled = false;
1155 }
1156 return ret;
1157}
1158
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301159static int sdhci_msm_setup_pinctrl(struct sdhci_msm_pltfm_data *pdata,
1160 bool enable)
1161{
1162 int ret = 0;
1163
1164 if (enable)
1165 ret = pinctrl_select_state(pdata->pctrl_data->pctrl,
1166 pdata->pctrl_data->pins_active);
1167 else
1168 ret = pinctrl_select_state(pdata->pctrl_data->pctrl,
1169 pdata->pctrl_data->pins_sleep);
1170
1171 if (ret < 0)
1172 pr_err("%s state for pinctrl failed with %d\n",
1173 enable ? "Enabling" : "Disabling", ret);
1174
1175 return ret;
1176}
1177
Asutosh Das0ef24812012-12-18 16:14:02 +05301178static int sdhci_msm_setup_pins(struct sdhci_msm_pltfm_data *pdata, bool enable)
1179{
1180 int ret = 0;
1181
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301182 if (pdata->pin_cfg_sts == enable) {
Asutosh Das0ef24812012-12-18 16:14:02 +05301183 return 0;
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301184 } else if (pdata->pctrl_data) {
1185 ret = sdhci_msm_setup_pinctrl(pdata, enable);
1186 goto out;
1187 } else if (!pdata->pin_data) {
1188 return 0;
1189 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301190
Sahitya Tummala1cd7e072014-02-14 13:19:01 +05301191 if (pdata->pin_data->is_gpio)
1192 ret = sdhci_msm_setup_gpio(pdata, enable);
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301193out:
Asutosh Das0ef24812012-12-18 16:14:02 +05301194 if (!ret)
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301195 pdata->pin_cfg_sts = enable;
Asutosh Das0ef24812012-12-18 16:14:02 +05301196
1197 return ret;
1198}
1199
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301200static int sdhci_msm_dt_get_array(struct device *dev, const char *prop_name,
1201 u32 **out, int *len, u32 size)
1202{
1203 int ret = 0;
1204 struct device_node *np = dev->of_node;
1205 size_t sz;
1206 u32 *arr = NULL;
1207
1208 if (!of_get_property(np, prop_name, len)) {
1209 ret = -EINVAL;
1210 goto out;
1211 }
1212 sz = *len = *len / sizeof(*arr);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07001213 if (sz <= 0 || (size > 0 && (sz > size))) {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301214 dev_err(dev, "%s invalid size\n", prop_name);
1215 ret = -EINVAL;
1216 goto out;
1217 }
1218
1219 arr = devm_kzalloc(dev, sz * sizeof(*arr), GFP_KERNEL);
1220 if (!arr) {
1221 dev_err(dev, "%s failed allocating memory\n", prop_name);
1222 ret = -ENOMEM;
1223 goto out;
1224 }
1225
1226 ret = of_property_read_u32_array(np, prop_name, arr, sz);
1227 if (ret < 0) {
1228 dev_err(dev, "%s failed reading array %d\n", prop_name, ret);
1229 goto out;
1230 }
1231 *out = arr;
1232out:
1233 if (ret)
1234 *len = 0;
1235 return ret;
1236}
1237
Asutosh Das0ef24812012-12-18 16:14:02 +05301238#define MAX_PROP_SIZE 32
1239static int sdhci_msm_dt_parse_vreg_info(struct device *dev,
1240 struct sdhci_msm_reg_data **vreg_data, const char *vreg_name)
1241{
1242 int len, ret = 0;
1243 const __be32 *prop;
1244 char prop_name[MAX_PROP_SIZE];
1245 struct sdhci_msm_reg_data *vreg;
1246 struct device_node *np = dev->of_node;
1247
1248 snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", vreg_name);
1249 if (!of_parse_phandle(np, prop_name, 0)) {
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301250 dev_info(dev, "No vreg data found for %s\n", vreg_name);
Asutosh Das0ef24812012-12-18 16:14:02 +05301251 return ret;
1252 }
1253
1254 vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
1255 if (!vreg) {
1256 dev_err(dev, "No memory for vreg: %s\n", vreg_name);
1257 ret = -ENOMEM;
1258 return ret;
1259 }
1260
1261 vreg->name = vreg_name;
1262
1263 snprintf(prop_name, MAX_PROP_SIZE,
1264 "qcom,%s-always-on", vreg_name);
1265 if (of_get_property(np, prop_name, NULL))
1266 vreg->is_always_on = true;
1267
1268 snprintf(prop_name, MAX_PROP_SIZE,
1269 "qcom,%s-lpm-sup", vreg_name);
1270 if (of_get_property(np, prop_name, NULL))
1271 vreg->lpm_sup = true;
1272
1273 snprintf(prop_name, MAX_PROP_SIZE,
1274 "qcom,%s-voltage-level", vreg_name);
1275 prop = of_get_property(np, prop_name, &len);
1276 if (!prop || (len != (2 * sizeof(__be32)))) {
1277 dev_warn(dev, "%s %s property\n",
1278 prop ? "invalid format" : "no", prop_name);
1279 } else {
1280 vreg->low_vol_level = be32_to_cpup(&prop[0]);
1281 vreg->high_vol_level = be32_to_cpup(&prop[1]);
1282 }
1283
1284 snprintf(prop_name, MAX_PROP_SIZE,
1285 "qcom,%s-current-level", vreg_name);
1286 prop = of_get_property(np, prop_name, &len);
1287 if (!prop || (len != (2 * sizeof(__be32)))) {
1288 dev_warn(dev, "%s %s property\n",
1289 prop ? "invalid format" : "no", prop_name);
1290 } else {
1291 vreg->lpm_uA = be32_to_cpup(&prop[0]);
1292 vreg->hpm_uA = be32_to_cpup(&prop[1]);
1293 }
1294
1295 *vreg_data = vreg;
1296 dev_dbg(dev, "%s: %s %s vol=[%d %d]uV, curr=[%d %d]uA\n",
1297 vreg->name, vreg->is_always_on ? "always_on," : "",
1298 vreg->lpm_sup ? "lpm_sup," : "", vreg->low_vol_level,
1299 vreg->high_vol_level, vreg->lpm_uA, vreg->hpm_uA);
1300
1301 return ret;
1302}
1303
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301304static int sdhci_msm_parse_pinctrl_info(struct device *dev,
1305 struct sdhci_msm_pltfm_data *pdata)
1306{
1307 struct sdhci_pinctrl_data *pctrl_data;
1308 struct pinctrl *pctrl;
1309 int ret = 0;
1310
1311 /* Try to obtain pinctrl handle */
1312 pctrl = devm_pinctrl_get(dev);
1313 if (IS_ERR(pctrl)) {
1314 ret = PTR_ERR(pctrl);
1315 goto out;
1316 }
1317 pctrl_data = devm_kzalloc(dev, sizeof(*pctrl_data), GFP_KERNEL);
1318 if (!pctrl_data) {
1319 dev_err(dev, "No memory for sdhci_pinctrl_data\n");
1320 ret = -ENOMEM;
1321 goto out;
1322 }
1323 pctrl_data->pctrl = pctrl;
1324 /* Look-up and keep the states handy to be used later */
1325 pctrl_data->pins_active = pinctrl_lookup_state(
1326 pctrl_data->pctrl, "active");
1327 if (IS_ERR(pctrl_data->pins_active)) {
1328 ret = PTR_ERR(pctrl_data->pins_active);
1329 dev_err(dev, "Could not get active pinstates, err:%d\n", ret);
1330 goto out;
1331 }
1332 pctrl_data->pins_sleep = pinctrl_lookup_state(
1333 pctrl_data->pctrl, "sleep");
1334 if (IS_ERR(pctrl_data->pins_sleep)) {
1335 ret = PTR_ERR(pctrl_data->pins_sleep);
1336 dev_err(dev, "Could not get sleep pinstates, err:%d\n", ret);
1337 goto out;
1338 }
1339 pdata->pctrl_data = pctrl_data;
1340out:
1341 return ret;
1342}
1343
Asutosh Das0ef24812012-12-18 16:14:02 +05301344#define GPIO_NAME_MAX_LEN 32
1345static int sdhci_msm_dt_parse_gpio_info(struct device *dev,
1346 struct sdhci_msm_pltfm_data *pdata)
1347{
1348 int ret = 0, cnt, i;
1349 struct sdhci_msm_pin_data *pin_data;
1350 struct device_node *np = dev->of_node;
1351
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301352 ret = sdhci_msm_parse_pinctrl_info(dev, pdata);
1353 if (!ret) {
1354 goto out;
1355 } else if (ret == -EPROBE_DEFER) {
1356 dev_err(dev, "Pinctrl framework not registered, err:%d\n", ret);
1357 goto out;
1358 } else {
1359 dev_err(dev, "Parsing Pinctrl failed with %d, falling back on GPIO lib\n",
1360 ret);
Sahitya Tummala1cd7e072014-02-14 13:19:01 +05301361 ret = 0;
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301362 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301363 pin_data = devm_kzalloc(dev, sizeof(*pin_data), GFP_KERNEL);
1364 if (!pin_data) {
1365 dev_err(dev, "No memory for pin_data\n");
1366 ret = -ENOMEM;
1367 goto out;
1368 }
1369
1370 cnt = of_gpio_count(np);
1371 if (cnt > 0) {
1372 pin_data->gpio_data = devm_kzalloc(dev,
1373 sizeof(struct sdhci_msm_gpio_data), GFP_KERNEL);
1374 if (!pin_data->gpio_data) {
1375 dev_err(dev, "No memory for gpio_data\n");
1376 ret = -ENOMEM;
1377 goto out;
1378 }
1379 pin_data->gpio_data->size = cnt;
1380 pin_data->gpio_data->gpio = devm_kzalloc(dev, cnt *
1381 sizeof(struct sdhci_msm_gpio), GFP_KERNEL);
1382
1383 if (!pin_data->gpio_data->gpio) {
1384 dev_err(dev, "No memory for gpio\n");
1385 ret = -ENOMEM;
1386 goto out;
1387 }
1388
1389 for (i = 0; i < cnt; i++) {
1390 const char *name = NULL;
1391 char result[GPIO_NAME_MAX_LEN];
1392 pin_data->gpio_data->gpio[i].no = of_get_gpio(np, i);
1393 of_property_read_string_index(np,
1394 "qcom,gpio-names", i, &name);
1395
1396 snprintf(result, GPIO_NAME_MAX_LEN, "%s-%s",
1397 dev_name(dev), name ? name : "?");
1398 pin_data->gpio_data->gpio[i].name = result;
1399 dev_dbg(dev, "%s: gpio[%s] = %d\n", __func__,
1400 pin_data->gpio_data->gpio[i].name,
1401 pin_data->gpio_data->gpio[i].no);
Asutosh Das0ef24812012-12-18 16:14:02 +05301402 }
1403 }
Sahitya Tummala1cd7e072014-02-14 13:19:01 +05301404 pdata->pin_data = pin_data;
Asutosh Das0ef24812012-12-18 16:14:02 +05301405out:
1406 if (ret)
1407 dev_err(dev, "%s failed with err %d\n", __func__, ret);
1408 return ret;
1409}
1410
Maya Erez994cf2f2014-10-21 20:22:04 +03001411#ifdef CONFIG_SMP
1412static void sdhci_msm_populate_affinity_type(struct sdhci_msm_pltfm_data *pdata,
1413 struct device_node *np)
1414{
1415 const char *cpu_affinity = NULL;
1416
1417 pdata->cpu_affinity_type = PM_QOS_REQ_AFFINE_IRQ;
1418 if (!of_property_read_string(np, "qcom,cpu-affinity",
1419 &cpu_affinity)) {
1420 if (!strcmp(cpu_affinity, "all_cores"))
1421 pdata->cpu_affinity_type = PM_QOS_REQ_ALL_CORES;
1422 else if (!strcmp(cpu_affinity, "affine_cores"))
1423 pdata->cpu_affinity_type = PM_QOS_REQ_AFFINE_CORES;
1424 else if (!strcmp(cpu_affinity, "affine_irq"))
1425 pdata->cpu_affinity_type = PM_QOS_REQ_AFFINE_IRQ;
1426 }
1427}
1428#else
1429static void sdhci_msm_populate_affinity_type(struct sdhci_msm_pltfm_data *pdata,
1430 struct device_node *np)
1431{
1432}
1433#endif
1434
Asutosh Das0ef24812012-12-18 16:14:02 +05301435/* Parse platform data */
1436static struct sdhci_msm_pltfm_data *sdhci_msm_populate_pdata(struct device *dev)
1437{
1438 struct sdhci_msm_pltfm_data *pdata = NULL;
1439 struct device_node *np = dev->of_node;
1440 u32 bus_width = 0;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05301441 u32 cpu_dma_latency;
Asutosh Das0ef24812012-12-18 16:14:02 +05301442 int len, i;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301443 int clk_table_len;
1444 u32 *clk_table = NULL;
Sujit Reddy Thumma1958d3d2013-06-03 09:54:32 +05301445 enum of_gpio_flags flags = OF_GPIO_ACTIVE_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05301446
1447 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1448 if (!pdata) {
1449 dev_err(dev, "failed to allocate memory for platform data\n");
1450 goto out;
1451 }
1452
Sujit Reddy Thumma1958d3d2013-06-03 09:54:32 +05301453 pdata->status_gpio = of_get_named_gpio_flags(np, "cd-gpios", 0, &flags);
1454 if (gpio_is_valid(pdata->status_gpio) & !(flags & OF_GPIO_ACTIVE_LOW))
1455 pdata->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
Sahitya Tummala581df132013-03-12 14:57:46 +05301456
Asutosh Das0ef24812012-12-18 16:14:02 +05301457 of_property_read_u32(np, "qcom,bus-width", &bus_width);
1458 if (bus_width == 8)
1459 pdata->mmc_bus_width = MMC_CAP_8_BIT_DATA;
1460 else if (bus_width == 4)
1461 pdata->mmc_bus_width = MMC_CAP_4_BIT_DATA;
1462 else {
1463 dev_notice(dev, "invalid bus-width, default to 1-bit mode\n");
1464 pdata->mmc_bus_width = 0;
1465 }
1466
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05301467 if (!of_property_read_u32(np, "qcom,cpu-dma-latency-us",
1468 &cpu_dma_latency))
1469 pdata->cpu_dma_latency_us = cpu_dma_latency;
Asutosh Das598a4d42014-02-05 16:38:23 +05301470 else
1471 pdata->cpu_dma_latency_us = MSM_MMC_DEFAULT_CPU_DMA_LATENCY;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301472 if (sdhci_msm_dt_get_array(dev, "qcom,clk-rates",
1473 &clk_table, &clk_table_len, 0)) {
1474 dev_err(dev, "failed parsing supported clock rates\n");
1475 goto out;
1476 }
1477 if (!clk_table || !clk_table_len) {
1478 dev_err(dev, "Invalid clock table\n");
1479 goto out;
1480 }
1481 pdata->sup_clk_table = clk_table;
1482 pdata->sup_clk_cnt = clk_table_len;
1483
Asutosh Das0ef24812012-12-18 16:14:02 +05301484 pdata->vreg_data = devm_kzalloc(dev, sizeof(struct
1485 sdhci_msm_slot_reg_data),
1486 GFP_KERNEL);
1487 if (!pdata->vreg_data) {
1488 dev_err(dev, "failed to allocate memory for vreg data\n");
1489 goto out;
1490 }
1491
1492 if (sdhci_msm_dt_parse_vreg_info(dev, &pdata->vreg_data->vdd_data,
1493 "vdd")) {
1494 dev_err(dev, "failed parsing vdd data\n");
1495 goto out;
1496 }
1497 if (sdhci_msm_dt_parse_vreg_info(dev,
1498 &pdata->vreg_data->vdd_io_data,
1499 "vdd-io")) {
1500 dev_err(dev, "failed parsing vdd-io data\n");
1501 goto out;
1502 }
1503
1504 if (sdhci_msm_dt_parse_gpio_info(dev, pdata)) {
1505 dev_err(dev, "failed parsing gpio data\n");
1506 goto out;
1507 }
1508
Asutosh Das0ef24812012-12-18 16:14:02 +05301509 len = of_property_count_strings(np, "qcom,bus-speed-mode");
1510
1511 for (i = 0; i < len; i++) {
1512 const char *name = NULL;
1513
1514 of_property_read_string_index(np,
1515 "qcom,bus-speed-mode", i, &name);
1516 if (!name)
1517 continue;
1518
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07001519 if (!strncmp(name, "HS400_1p8v", sizeof("HS400_1p8v")))
1520 pdata->caps2 |= MMC_CAP2_HS400_1_8V;
1521 else if (!strncmp(name, "HS400_1p2v", sizeof("HS400_1p2v")))
1522 pdata->caps2 |= MMC_CAP2_HS400_1_2V;
1523 else if (!strncmp(name, "HS200_1p8v", sizeof("HS200_1p8v")))
Asutosh Das0ef24812012-12-18 16:14:02 +05301524 pdata->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
1525 else if (!strncmp(name, "HS200_1p2v", sizeof("HS200_1p2v")))
1526 pdata->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
1527 else if (!strncmp(name, "DDR_1p8v", sizeof("DDR_1p8v")))
1528 pdata->caps |= MMC_CAP_1_8V_DDR
1529 | MMC_CAP_UHS_DDR50;
1530 else if (!strncmp(name, "DDR_1p2v", sizeof("DDR_1p2v")))
1531 pdata->caps |= MMC_CAP_1_2V_DDR
1532 | MMC_CAP_UHS_DDR50;
1533 }
1534
1535 if (of_get_property(np, "qcom,nonremovable", NULL))
1536 pdata->nonremovable = true;
1537
Guoping Yuf7c91332014-08-20 16:56:18 +08001538 if (of_get_property(np, "qcom,nonhotplug", NULL))
1539 pdata->nonhotplug = true;
1540
Maya Erez994cf2f2014-10-21 20:22:04 +03001541 sdhci_msm_populate_affinity_type(pdata, np);
1542
Asutosh Das0ef24812012-12-18 16:14:02 +05301543 return pdata;
1544out:
1545 return NULL;
1546}
1547
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301548/* Returns required bandwidth in Bytes per Sec */
1549static unsigned int sdhci_get_bw_required(struct sdhci_host *host,
1550 struct mmc_ios *ios)
1551{
Sahitya Tummala2886c922013-04-03 18:03:31 +05301552 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1553 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1554
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301555 unsigned int bw;
1556
Sahitya Tummala2886c922013-04-03 18:03:31 +05301557 bw = msm_host->clk_rate;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301558 /*
1559 * For DDR mode, SDCC controller clock will be at
1560 * the double rate than the actual clock that goes to card.
1561 */
1562 if (ios->bus_width == MMC_BUS_WIDTH_4)
1563 bw /= 2;
1564 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1565 bw /= 8;
1566
1567 return bw;
1568}
1569
1570static int sdhci_msm_bus_get_vote_for_bw(struct sdhci_msm_host *host,
1571 unsigned int bw)
1572{
1573 unsigned int *table = host->pdata->voting_data->bw_vecs;
1574 unsigned int size = host->pdata->voting_data->bw_vecs_size;
1575 int i;
1576
1577 if (host->msm_bus_vote.is_max_bw_needed && bw)
1578 return host->msm_bus_vote.max_bw_vote;
1579
1580 for (i = 0; i < size; i++) {
1581 if (bw <= table[i])
1582 break;
1583 }
1584
1585 if (i && (i == size))
1586 i--;
1587
1588 return i;
1589}
1590
1591/*
1592 * This function must be called with host lock acquired.
1593 * Caller of this function should also ensure that msm bus client
1594 * handle is not null.
1595 */
1596static inline int sdhci_msm_bus_set_vote(struct sdhci_msm_host *msm_host,
1597 int vote,
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301598 unsigned long *flags)
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301599{
1600 struct sdhci_host *host = platform_get_drvdata(msm_host->pdev);
1601 int rc = 0;
1602
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301603 BUG_ON(!flags);
1604
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301605 if (vote != msm_host->msm_bus_vote.curr_vote) {
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301606 spin_unlock_irqrestore(&host->lock, *flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301607 rc = msm_bus_scale_client_update_request(
1608 msm_host->msm_bus_vote.client_handle, vote);
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301609 spin_lock_irqsave(&host->lock, *flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301610 if (rc) {
1611 pr_err("%s: msm_bus_scale_client_update_request() failed: bus_client_handle=0x%x, vote=%d, err=%d\n",
1612 mmc_hostname(host->mmc),
1613 msm_host->msm_bus_vote.client_handle, vote, rc);
1614 goto out;
1615 }
1616 msm_host->msm_bus_vote.curr_vote = vote;
1617 }
1618out:
1619 return rc;
1620}
1621
1622/*
1623 * Internal work. Work to set 0 bandwidth for msm bus.
1624 */
1625static void sdhci_msm_bus_work(struct work_struct *work)
1626{
1627 struct sdhci_msm_host *msm_host;
1628 struct sdhci_host *host;
1629 unsigned long flags;
1630
1631 msm_host = container_of(work, struct sdhci_msm_host,
1632 msm_bus_vote.vote_work.work);
1633 host = platform_get_drvdata(msm_host->pdev);
1634
1635 if (!msm_host->msm_bus_vote.client_handle)
1636 return;
1637
1638 spin_lock_irqsave(&host->lock, flags);
1639 /* don't vote for 0 bandwidth if any request is in progress */
1640 if (!host->mrq) {
1641 sdhci_msm_bus_set_vote(msm_host,
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301642 msm_host->msm_bus_vote.min_bw_vote, &flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301643 } else
1644 pr_warning("%s: %s: Transfer in progress. skipping bus voting to 0 bandwidth\n",
1645 mmc_hostname(host->mmc), __func__);
1646 spin_unlock_irqrestore(&host->lock, flags);
1647}
1648
1649/*
1650 * This function cancels any scheduled delayed work and sets the bus
1651 * vote based on bw (bandwidth) argument.
1652 */
1653static void sdhci_msm_bus_cancel_work_and_set_vote(struct sdhci_host *host,
1654 unsigned int bw)
1655{
1656 int vote;
1657 unsigned long flags;
1658 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1659 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1660
1661 cancel_delayed_work_sync(&msm_host->msm_bus_vote.vote_work);
1662 spin_lock_irqsave(&host->lock, flags);
1663 vote = sdhci_msm_bus_get_vote_for_bw(msm_host, bw);
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301664 sdhci_msm_bus_set_vote(msm_host, vote, &flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301665 spin_unlock_irqrestore(&host->lock, flags);
1666}
1667
1668#define MSM_MMC_BUS_VOTING_DELAY 200 /* msecs */
1669
1670/* This function queues a work which will set the bandwidth requiement to 0 */
1671static void sdhci_msm_bus_queue_work(struct sdhci_host *host)
1672{
1673 unsigned long flags;
1674 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1675 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1676
1677 spin_lock_irqsave(&host->lock, flags);
1678 if (msm_host->msm_bus_vote.min_bw_vote !=
1679 msm_host->msm_bus_vote.curr_vote)
1680 queue_delayed_work(system_wq,
1681 &msm_host->msm_bus_vote.vote_work,
1682 msecs_to_jiffies(MSM_MMC_BUS_VOTING_DELAY));
1683 spin_unlock_irqrestore(&host->lock, flags);
1684}
1685
1686static int sdhci_msm_bus_register(struct sdhci_msm_host *host,
1687 struct platform_device *pdev)
1688{
1689 int rc = 0;
1690 struct msm_bus_scale_pdata *bus_pdata;
1691
1692 struct sdhci_msm_bus_voting_data *data;
1693 struct device *dev = &pdev->dev;
1694
1695 data = devm_kzalloc(dev,
1696 sizeof(struct sdhci_msm_bus_voting_data), GFP_KERNEL);
1697 if (!data) {
1698 dev_err(&pdev->dev,
1699 "%s: failed to allocate memory\n", __func__);
1700 rc = -ENOMEM;
1701 goto out;
1702 }
1703 data->bus_pdata = msm_bus_cl_get_pdata(pdev);
1704 if (data->bus_pdata) {
1705 rc = sdhci_msm_dt_get_array(dev, "qcom,bus-bw-vectors-bps",
1706 &data->bw_vecs, &data->bw_vecs_size, 0);
1707 if (rc) {
1708 dev_err(&pdev->dev,
1709 "%s: Failed to get bus-bw-vectors-bps\n",
1710 __func__);
1711 goto out;
1712 }
1713 host->pdata->voting_data = data;
1714 }
1715 if (host->pdata->voting_data &&
1716 host->pdata->voting_data->bus_pdata &&
1717 host->pdata->voting_data->bw_vecs &&
1718 host->pdata->voting_data->bw_vecs_size) {
1719
1720 bus_pdata = host->pdata->voting_data->bus_pdata;
1721 host->msm_bus_vote.client_handle =
1722 msm_bus_scale_register_client(bus_pdata);
1723 if (!host->msm_bus_vote.client_handle) {
1724 dev_err(&pdev->dev, "msm_bus_scale_register_client()\n");
1725 rc = -EFAULT;
1726 goto out;
1727 }
1728 /* cache the vote index for minimum and maximum bandwidth */
1729 host->msm_bus_vote.min_bw_vote =
1730 sdhci_msm_bus_get_vote_for_bw(host, 0);
1731 host->msm_bus_vote.max_bw_vote =
1732 sdhci_msm_bus_get_vote_for_bw(host, UINT_MAX);
1733 } else {
1734 devm_kfree(dev, data);
1735 }
1736
1737out:
1738 return rc;
1739}
1740
1741static void sdhci_msm_bus_unregister(struct sdhci_msm_host *host)
1742{
1743 if (host->msm_bus_vote.client_handle)
1744 msm_bus_scale_unregister_client(
1745 host->msm_bus_vote.client_handle);
1746}
1747
1748static void sdhci_msm_bus_voting(struct sdhci_host *host, u32 enable)
1749{
1750 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1751 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1752 struct mmc_ios *ios = &host->mmc->ios;
1753 unsigned int bw;
1754
1755 if (!msm_host->msm_bus_vote.client_handle)
1756 return;
1757
1758 bw = sdhci_get_bw_required(host, ios);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301759 if (enable) {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301760 sdhci_msm_bus_cancel_work_and_set_vote(host, bw);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301761 } else {
1762 /*
1763 * If clock gating is enabled, then remove the vote
1764 * immediately because clocks will be disabled only
1765 * after SDHCI_MSM_MMC_CLK_GATE_DELAY and thus no
1766 * additional delay is required to remove the bus vote.
1767 */
1768#ifdef CONFIG_MMC_CLKGATE
1769 if (host->mmc->clkgate_delay)
1770 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
1771 else
1772#endif
1773 sdhci_msm_bus_queue_work(host);
1774 }
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301775}
1776
Asutosh Das0ef24812012-12-18 16:14:02 +05301777/* Regulator utility functions */
1778static int sdhci_msm_vreg_init_reg(struct device *dev,
1779 struct sdhci_msm_reg_data *vreg)
1780{
1781 int ret = 0;
1782
1783 /* check if regulator is already initialized? */
1784 if (vreg->reg)
1785 goto out;
1786
1787 /* Get the regulator handle */
1788 vreg->reg = devm_regulator_get(dev, vreg->name);
1789 if (IS_ERR(vreg->reg)) {
1790 ret = PTR_ERR(vreg->reg);
1791 pr_err("%s: devm_regulator_get(%s) failed. ret=%d\n",
1792 __func__, vreg->name, ret);
1793 goto out;
1794 }
1795
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301796 if (regulator_count_voltages(vreg->reg) > 0) {
1797 vreg->set_voltage_sup = true;
1798 /* sanity check */
1799 if (!vreg->high_vol_level || !vreg->hpm_uA) {
1800 pr_err("%s: %s invalid constraints specified\n",
1801 __func__, vreg->name);
1802 ret = -EINVAL;
1803 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301804 }
1805
1806out:
1807 return ret;
1808}
1809
1810static void sdhci_msm_vreg_deinit_reg(struct sdhci_msm_reg_data *vreg)
1811{
1812 if (vreg->reg)
1813 devm_regulator_put(vreg->reg);
1814}
1815
1816static int sdhci_msm_vreg_set_optimum_mode(struct sdhci_msm_reg_data
1817 *vreg, int uA_load)
1818{
1819 int ret = 0;
1820
1821 /*
1822 * regulators that do not support regulator_set_voltage also
1823 * do not support regulator_set_optimum_mode
1824 */
1825 if (vreg->set_voltage_sup) {
1826 ret = regulator_set_load(vreg->reg, uA_load);
1827 if (ret < 0)
1828 pr_err("%s: regulator_set_load(reg=%s,uA_load=%d) failed. ret=%d\n",
1829 __func__, vreg->name, uA_load, ret);
1830 else
1831 /*
1832 * regulator_set_load() can return non zero
1833 * value even for success case.
1834 */
1835 ret = 0;
1836 }
1837 return ret;
1838}
1839
1840static int sdhci_msm_vreg_set_voltage(struct sdhci_msm_reg_data *vreg,
1841 int min_uV, int max_uV)
1842{
1843 int ret = 0;
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301844 if (vreg->set_voltage_sup) {
1845 ret = regulator_set_voltage(vreg->reg, min_uV, max_uV);
1846 if (ret) {
1847 pr_err("%s: regulator_set_voltage(%s)failed. min_uV=%d,max_uV=%d,ret=%d\n",
Asutosh Das0ef24812012-12-18 16:14:02 +05301848 __func__, vreg->name, min_uV, max_uV, ret);
1849 }
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301850 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301851
1852 return ret;
1853}
1854
1855static int sdhci_msm_vreg_enable(struct sdhci_msm_reg_data *vreg)
1856{
1857 int ret = 0;
1858
1859 /* Put regulator in HPM (high power mode) */
1860 ret = sdhci_msm_vreg_set_optimum_mode(vreg, vreg->hpm_uA);
1861 if (ret < 0)
1862 return ret;
1863
1864 if (!vreg->is_enabled) {
1865 /* Set voltage level */
1866 ret = sdhci_msm_vreg_set_voltage(vreg, vreg->high_vol_level,
1867 vreg->high_vol_level);
1868 if (ret)
1869 return ret;
1870 }
1871 ret = regulator_enable(vreg->reg);
1872 if (ret) {
1873 pr_err("%s: regulator_enable(%s) failed. ret=%d\n",
1874 __func__, vreg->name, ret);
1875 return ret;
1876 }
1877 vreg->is_enabled = true;
1878 return ret;
1879}
1880
1881static int sdhci_msm_vreg_disable(struct sdhci_msm_reg_data *vreg)
1882{
1883 int ret = 0;
1884
1885 /* Never disable regulator marked as always_on */
1886 if (vreg->is_enabled && !vreg->is_always_on) {
1887 ret = regulator_disable(vreg->reg);
1888 if (ret) {
1889 pr_err("%s: regulator_disable(%s) failed. ret=%d\n",
1890 __func__, vreg->name, ret);
1891 goto out;
1892 }
1893 vreg->is_enabled = false;
1894
1895 ret = sdhci_msm_vreg_set_optimum_mode(vreg, 0);
1896 if (ret < 0)
1897 goto out;
1898
1899 /* Set min. voltage level to 0 */
1900 ret = sdhci_msm_vreg_set_voltage(vreg, 0, vreg->high_vol_level);
1901 if (ret)
1902 goto out;
1903 } else if (vreg->is_enabled && vreg->is_always_on) {
1904 if (vreg->lpm_sup) {
1905 /* Put always_on regulator in LPM (low power mode) */
1906 ret = sdhci_msm_vreg_set_optimum_mode(vreg,
1907 vreg->lpm_uA);
1908 if (ret < 0)
1909 goto out;
1910 }
1911 }
1912out:
1913 return ret;
1914}
1915
1916static int sdhci_msm_setup_vreg(struct sdhci_msm_pltfm_data *pdata,
1917 bool enable, bool is_init)
1918{
1919 int ret = 0, i;
1920 struct sdhci_msm_slot_reg_data *curr_slot;
1921 struct sdhci_msm_reg_data *vreg_table[2];
1922
1923 curr_slot = pdata->vreg_data;
1924 if (!curr_slot) {
1925 pr_debug("%s: vreg info unavailable,assuming the slot is powered by always on domain\n",
1926 __func__);
1927 goto out;
1928 }
1929
1930 vreg_table[0] = curr_slot->vdd_data;
1931 vreg_table[1] = curr_slot->vdd_io_data;
1932
1933 for (i = 0; i < ARRAY_SIZE(vreg_table); i++) {
1934 if (vreg_table[i]) {
1935 if (enable)
1936 ret = sdhci_msm_vreg_enable(vreg_table[i]);
1937 else
1938 ret = sdhci_msm_vreg_disable(vreg_table[i]);
1939 if (ret)
1940 goto out;
1941 }
1942 }
1943out:
1944 return ret;
1945}
1946
1947/*
1948 * Reset vreg by ensuring it is off during probe. A call
1949 * to enable vreg is needed to balance disable vreg
1950 */
1951static int sdhci_msm_vreg_reset(struct sdhci_msm_pltfm_data *pdata)
1952{
1953 int ret;
1954
1955 ret = sdhci_msm_setup_vreg(pdata, 1, true);
1956 if (ret)
1957 return ret;
1958 ret = sdhci_msm_setup_vreg(pdata, 0, true);
1959 return ret;
1960}
1961
1962/* This init function should be called only once for each SDHC slot */
1963static int sdhci_msm_vreg_init(struct device *dev,
1964 struct sdhci_msm_pltfm_data *pdata,
1965 bool is_init)
1966{
1967 int ret = 0;
1968 struct sdhci_msm_slot_reg_data *curr_slot;
1969 struct sdhci_msm_reg_data *curr_vdd_reg, *curr_vdd_io_reg;
1970
1971 curr_slot = pdata->vreg_data;
1972 if (!curr_slot)
1973 goto out;
1974
1975 curr_vdd_reg = curr_slot->vdd_data;
1976 curr_vdd_io_reg = curr_slot->vdd_io_data;
1977
1978 if (!is_init)
1979 /* Deregister all regulators from regulator framework */
1980 goto vdd_io_reg_deinit;
1981
1982 /*
1983 * Get the regulator handle from voltage regulator framework
1984 * and then try to set the voltage level for the regulator
1985 */
1986 if (curr_vdd_reg) {
1987 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_reg);
1988 if (ret)
1989 goto out;
1990 }
1991 if (curr_vdd_io_reg) {
1992 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_io_reg);
1993 if (ret)
1994 goto vdd_reg_deinit;
1995 }
1996 ret = sdhci_msm_vreg_reset(pdata);
1997 if (ret)
1998 dev_err(dev, "vreg reset failed (%d)\n", ret);
1999 goto out;
2000
2001vdd_io_reg_deinit:
2002 if (curr_vdd_io_reg)
2003 sdhci_msm_vreg_deinit_reg(curr_vdd_io_reg);
2004vdd_reg_deinit:
2005 if (curr_vdd_reg)
2006 sdhci_msm_vreg_deinit_reg(curr_vdd_reg);
2007out:
2008 return ret;
2009}
2010
2011
2012static int sdhci_msm_set_vdd_io_vol(struct sdhci_msm_pltfm_data *pdata,
2013 enum vdd_io_level level,
2014 unsigned int voltage_level)
2015{
2016 int ret = 0;
2017 int set_level;
2018 struct sdhci_msm_reg_data *vdd_io_reg;
2019
2020 if (!pdata->vreg_data)
2021 return ret;
2022
2023 vdd_io_reg = pdata->vreg_data->vdd_io_data;
2024 if (vdd_io_reg && vdd_io_reg->is_enabled) {
2025 switch (level) {
2026 case VDD_IO_LOW:
2027 set_level = vdd_io_reg->low_vol_level;
2028 break;
2029 case VDD_IO_HIGH:
2030 set_level = vdd_io_reg->high_vol_level;
2031 break;
2032 case VDD_IO_SET_LEVEL:
2033 set_level = voltage_level;
2034 break;
2035 default:
2036 pr_err("%s: invalid argument level = %d",
2037 __func__, level);
2038 ret = -EINVAL;
2039 return ret;
2040 }
2041 ret = sdhci_msm_vreg_set_voltage(vdd_io_reg, set_level,
2042 set_level);
2043 }
2044 return ret;
2045}
2046
2047static irqreturn_t sdhci_msm_pwr_irq(int irq, void *data)
2048{
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002049 struct sdhci_host *host = (struct sdhci_host *)data;
2050 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2051 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Das0ef24812012-12-18 16:14:02 +05302052 u8 irq_status = 0;
2053 u8 irq_ack = 0;
2054 int ret = 0;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302055 int pwr_state = 0, io_level = 0;
2056 unsigned long flags;
Asutosh Das0ef24812012-12-18 16:14:02 +05302057
2058 irq_status = readb_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
2059 pr_debug("%s: Received IRQ(%d), status=0x%x\n",
2060 mmc_hostname(msm_host->mmc), irq, irq_status);
2061
2062 /* Clear the interrupt */
2063 writeb_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
2064 /*
2065 * SDHC has core_mem and hc_mem device memory and these memory
2066 * addresses do not fall within 1KB region. Hence, any update to
2067 * core_mem address space would require an mb() to ensure this gets
2068 * completed before its next update to registers within hc_mem.
2069 */
2070 mb();
2071
2072 /* Handle BUS ON/OFF*/
2073 if (irq_status & CORE_PWRCTL_BUS_ON) {
2074 ret = sdhci_msm_setup_vreg(msm_host->pdata, true, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302075 if (!ret) {
Asutosh Das0ef24812012-12-18 16:14:02 +05302076 ret = sdhci_msm_setup_pins(msm_host->pdata, true);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302077 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
2078 VDD_IO_HIGH, 0);
2079 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302080 if (ret)
2081 irq_ack |= CORE_PWRCTL_BUS_FAIL;
2082 else
2083 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302084
2085 pwr_state = REQ_BUS_ON;
2086 io_level = REQ_IO_HIGH;
Asutosh Das0ef24812012-12-18 16:14:02 +05302087 }
2088 if (irq_status & CORE_PWRCTL_BUS_OFF) {
2089 ret = sdhci_msm_setup_vreg(msm_host->pdata, false, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302090 if (!ret) {
Asutosh Das0ef24812012-12-18 16:14:02 +05302091 ret = sdhci_msm_setup_pins(msm_host->pdata, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302092 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
2093 VDD_IO_LOW, 0);
2094 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302095 if (ret)
2096 irq_ack |= CORE_PWRCTL_BUS_FAIL;
2097 else
2098 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302099
2100 pwr_state = REQ_BUS_OFF;
2101 io_level = REQ_IO_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05302102 }
2103 /* Handle IO LOW/HIGH */
2104 if (irq_status & CORE_PWRCTL_IO_LOW) {
2105 /* Switch voltage Low */
2106 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_LOW, 0);
2107 if (ret)
2108 irq_ack |= CORE_PWRCTL_IO_FAIL;
2109 else
2110 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302111
2112 io_level = REQ_IO_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05302113 }
2114 if (irq_status & CORE_PWRCTL_IO_HIGH) {
2115 /* Switch voltage High */
2116 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_HIGH, 0);
2117 if (ret)
2118 irq_ack |= CORE_PWRCTL_IO_FAIL;
2119 else
2120 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302121
2122 io_level = REQ_IO_HIGH;
Asutosh Das0ef24812012-12-18 16:14:02 +05302123 }
2124
2125 /* ACK status to the core */
2126 writeb_relaxed(irq_ack, (msm_host->core_mem + CORE_PWRCTL_CTL));
2127 /*
2128 * SDHC has core_mem and hc_mem device memory and these memory
2129 * addresses do not fall within 1KB region. Hence, any update to
2130 * core_mem address space would require an mb() to ensure this gets
2131 * completed before its next update to registers within hc_mem.
2132 */
2133 mb();
2134
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302135 if (io_level & REQ_IO_HIGH)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002136 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2137 ~CORE_IO_PAD_PWR_SWITCH),
2138 host->ioaddr + CORE_VENDOR_SPEC);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302139 else if (io_level & REQ_IO_LOW)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002140 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
2141 CORE_IO_PAD_PWR_SWITCH),
2142 host->ioaddr + CORE_VENDOR_SPEC);
2143 mb();
2144
Asutosh Das0ef24812012-12-18 16:14:02 +05302145 pr_debug("%s: Handled IRQ(%d), ret=%d, ack=0x%x\n",
2146 mmc_hostname(msm_host->mmc), irq, ret, irq_ack);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302147 spin_lock_irqsave(&host->lock, flags);
2148 if (pwr_state)
2149 msm_host->curr_pwr_state = pwr_state;
2150 if (io_level)
2151 msm_host->curr_io_level = io_level;
2152 complete(&msm_host->pwr_irq_completion);
2153 spin_unlock_irqrestore(&host->lock, flags);
2154
Asutosh Das0ef24812012-12-18 16:14:02 +05302155 return IRQ_HANDLED;
2156}
2157
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302158static ssize_t
Sahitya Tummala5c55b932013-06-20 14:00:18 +05302159show_polling(struct device *dev, struct device_attribute *attr, char *buf)
2160{
2161 struct sdhci_host *host = dev_get_drvdata(dev);
2162 int poll;
2163 unsigned long flags;
2164
2165 spin_lock_irqsave(&host->lock, flags);
2166 poll = !!(host->mmc->caps & MMC_CAP_NEEDS_POLL);
2167 spin_unlock_irqrestore(&host->lock, flags);
2168
2169 return snprintf(buf, PAGE_SIZE, "%d\n", poll);
2170}
2171
2172static ssize_t
2173store_polling(struct device *dev, struct device_attribute *attr,
2174 const char *buf, size_t count)
2175{
2176 struct sdhci_host *host = dev_get_drvdata(dev);
2177 int value;
2178 unsigned long flags;
2179
2180 if (!kstrtou32(buf, 0, &value)) {
2181 spin_lock_irqsave(&host->lock, flags);
2182 if (value) {
2183 host->mmc->caps |= MMC_CAP_NEEDS_POLL;
2184 mmc_detect_change(host->mmc, 0);
2185 } else {
2186 host->mmc->caps &= ~MMC_CAP_NEEDS_POLL;
2187 }
2188 spin_unlock_irqrestore(&host->lock, flags);
2189 }
2190 return count;
2191}
2192
2193static ssize_t
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302194show_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
2195 char *buf)
2196{
2197 struct sdhci_host *host = dev_get_drvdata(dev);
2198 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2199 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2200
2201 return snprintf(buf, PAGE_SIZE, "%u\n",
2202 msm_host->msm_bus_vote.is_max_bw_needed);
2203}
2204
2205static ssize_t
2206store_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
2207 const char *buf, size_t count)
2208{
2209 struct sdhci_host *host = dev_get_drvdata(dev);
2210 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2211 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2212 uint32_t value;
2213 unsigned long flags;
2214
2215 if (!kstrtou32(buf, 0, &value)) {
2216 spin_lock_irqsave(&host->lock, flags);
2217 msm_host->msm_bus_vote.is_max_bw_needed = !!value;
2218 spin_unlock_irqrestore(&host->lock, flags);
2219 }
2220 return count;
2221}
2222
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302223static void sdhci_msm_check_power_status(struct sdhci_host *host, u32 req_type)
Asutosh Das0ef24812012-12-18 16:14:02 +05302224{
2225 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2226 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302227 unsigned long flags;
2228 bool done = false;
Sahitya Tummala481fbb02013-08-06 15:22:28 +05302229 u32 io_sig_sts;
Asutosh Das0ef24812012-12-18 16:14:02 +05302230
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302231 spin_lock_irqsave(&host->lock, flags);
2232 pr_debug("%s: %s: request %d curr_pwr_state %x curr_io_level %x\n",
2233 mmc_hostname(host->mmc), __func__, req_type,
2234 msm_host->curr_pwr_state, msm_host->curr_io_level);
Sahitya Tummala481fbb02013-08-06 15:22:28 +05302235 io_sig_sts = readl_relaxed(msm_host->core_mem + CORE_GENERICS);
2236 /*
2237 * The IRQ for request type IO High/Low will be generated when -
2238 * 1. SWITCHABLE_SIGNALLING_VOL is enabled in HW.
2239 * 2. If 1 is true and when there is a state change in 1.8V enable
2240 * bit (bit 3) of SDHCI_HOST_CONTROL2 register. The reset state of
2241 * that bit is 0 which indicates 3.3V IO voltage. So, when MMC core
2242 * layer tries to set it to 3.3V before card detection happens, the
2243 * IRQ doesn't get triggered as there is no state change in this bit.
2244 * The driver already handles this case by changing the IO voltage
2245 * level to high as part of controller power up sequence. Hence, check
2246 * for host->pwr to handle a case where IO voltage high request is
2247 * issued even before controller power up.
2248 */
2249 if (req_type & (REQ_IO_HIGH | REQ_IO_LOW)) {
2250 if (!(io_sig_sts & SWITCHABLE_SIGNALLING_VOL) ||
2251 ((req_type & REQ_IO_HIGH) && !host->pwr)) {
2252 pr_debug("%s: do not wait for power IRQ that never comes\n",
2253 mmc_hostname(host->mmc));
2254 spin_unlock_irqrestore(&host->lock, flags);
2255 return;
2256 }
2257 }
2258
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302259 if ((req_type & msm_host->curr_pwr_state) ||
2260 (req_type & msm_host->curr_io_level))
2261 done = true;
2262 spin_unlock_irqrestore(&host->lock, flags);
Asutosh Das0ef24812012-12-18 16:14:02 +05302263
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302264 /*
2265 * This is needed here to hanlde a case where IRQ gets
2266 * triggered even before this function is called so that
2267 * x->done counter of completion gets reset. Otherwise,
2268 * next call to wait_for_completion returns immediately
2269 * without actually waiting for the IRQ to be handled.
2270 */
2271 if (done)
2272 init_completion(&msm_host->pwr_irq_completion);
2273 else
2274 wait_for_completion(&msm_host->pwr_irq_completion);
2275
2276 pr_debug("%s: %s: request %d done\n", mmc_hostname(host->mmc),
2277 __func__, req_type);
Asutosh Das0ef24812012-12-18 16:14:02 +05302278}
2279
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002280static void sdhci_msm_toggle_cdr(struct sdhci_host *host, bool enable)
2281{
2282 if (enable)
2283 writel_relaxed((readl_relaxed(host->ioaddr +
2284 CORE_DLL_CONFIG) | CORE_CDR_EN),
2285 host->ioaddr + CORE_DLL_CONFIG);
2286 else
2287 writel_relaxed((readl_relaxed(host->ioaddr +
2288 CORE_DLL_CONFIG) & ~CORE_CDR_EN),
2289 host->ioaddr + CORE_DLL_CONFIG);
2290}
2291
Asutosh Das648f9d12013-01-10 21:11:04 +05302292static unsigned int sdhci_msm_max_segs(void)
2293{
2294 return SDHCI_MSM_MAX_SEGMENTS;
2295}
2296
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302297static unsigned int sdhci_msm_get_min_clock(struct sdhci_host *host)
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302298{
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302299 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2300 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302301
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302302 return msm_host->pdata->sup_clk_table[0];
2303}
2304
2305static unsigned int sdhci_msm_get_max_clock(struct sdhci_host *host)
2306{
2307 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2308 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2309 int max_clk_index = msm_host->pdata->sup_clk_cnt;
2310
2311 return msm_host->pdata->sup_clk_table[max_clk_index - 1];
2312}
2313
2314static unsigned int sdhci_msm_get_sup_clk_rate(struct sdhci_host *host,
2315 u32 req_clk)
2316{
2317 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2318 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2319 unsigned int sel_clk = -1;
2320 unsigned char cnt;
2321
2322 if (req_clk < sdhci_msm_get_min_clock(host)) {
2323 sel_clk = sdhci_msm_get_min_clock(host);
2324 return sel_clk;
2325 }
2326
2327 for (cnt = 0; cnt < msm_host->pdata->sup_clk_cnt; cnt++) {
2328 if (msm_host->pdata->sup_clk_table[cnt] > req_clk) {
2329 break;
2330 } else if (msm_host->pdata->sup_clk_table[cnt] == req_clk) {
2331 sel_clk = msm_host->pdata->sup_clk_table[cnt];
2332 break;
2333 } else {
2334 sel_clk = msm_host->pdata->sup_clk_table[cnt];
2335 }
2336 }
2337 return sel_clk;
2338}
2339
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302340static int sdhci_msm_enable_controller_clock(struct sdhci_host *host)
2341{
2342 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2343 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2344 int rc = 0;
2345
2346 if (atomic_read(&msm_host->controller_clock))
2347 return 0;
2348
2349 sdhci_msm_bus_voting(host, 1);
2350
2351 if (!IS_ERR(msm_host->pclk)) {
2352 rc = clk_prepare_enable(msm_host->pclk);
2353 if (rc) {
2354 pr_err("%s: %s: failed to enable the pclk with error %d\n",
2355 mmc_hostname(host->mmc), __func__, rc);
2356 goto remove_vote;
2357 }
2358 }
2359
2360 rc = clk_prepare_enable(msm_host->clk);
2361 if (rc) {
2362 pr_err("%s: %s: failed to enable the host-clk with error %d\n",
2363 mmc_hostname(host->mmc), __func__, rc);
2364 goto disable_pclk;
2365 }
2366
2367 atomic_set(&msm_host->controller_clock, 1);
2368 pr_debug("%s: %s: enabled controller clock\n",
2369 mmc_hostname(host->mmc), __func__);
2370 goto out;
2371
2372disable_pclk:
2373 if (!IS_ERR(msm_host->pclk))
2374 clk_disable_unprepare(msm_host->pclk);
2375remove_vote:
2376 if (msm_host->msm_bus_vote.client_handle)
2377 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2378out:
2379 return rc;
2380}
2381
2382
2383
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302384static int sdhci_msm_prepare_clocks(struct sdhci_host *host, bool enable)
2385{
2386 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2387 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2388 int rc = 0;
2389
2390 if (enable && !atomic_read(&msm_host->clks_on)) {
2391 pr_debug("%s: request to enable clocks\n",
2392 mmc_hostname(host->mmc));
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302393
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302394 /*
2395 * The bus-width or the clock rate might have changed
2396 * after controller clocks are enbaled, update bus vote
2397 * in such case.
2398 */
2399 if (atomic_read(&msm_host->controller_clock))
2400 sdhci_msm_bus_voting(host, 1);
2401
2402 rc = sdhci_msm_enable_controller_clock(host);
2403 if (rc)
2404 goto remove_vote;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302405
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302406 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
2407 rc = clk_prepare_enable(msm_host->bus_clk);
2408 if (rc) {
2409 pr_err("%s: %s: failed to enable the bus-clock with error %d\n",
2410 mmc_hostname(host->mmc), __func__, rc);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302411 goto disable_controller_clk;
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302412 }
2413 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002414 if (!IS_ERR(msm_host->ff_clk)) {
2415 rc = clk_prepare_enable(msm_host->ff_clk);
2416 if (rc) {
2417 pr_err("%s: %s: failed to enable the ff_clk with error %d\n",
2418 mmc_hostname(host->mmc), __func__, rc);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302419 goto disable_bus_clk;
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002420 }
2421 }
2422 if (!IS_ERR(msm_host->sleep_clk)) {
2423 rc = clk_prepare_enable(msm_host->sleep_clk);
2424 if (rc) {
2425 pr_err("%s: %s: failed to enable the sleep_clk with error %d\n",
2426 mmc_hostname(host->mmc), __func__, rc);
2427 goto disable_ff_clk;
2428 }
2429 }
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302430 mb();
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302431
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302432 } else if (!enable && atomic_read(&msm_host->clks_on)) {
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302433 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
2434 mb();
Sahitya Tummaladc182982013-08-20 15:32:09 +05302435 /*
2436 * During 1.8V signal switching the clock source must
2437 * still be ON as it requires accessing SDHC
2438 * registers (SDHCi host control2 register bit 3 must
2439 * be written and polled after stopping the SDCLK).
2440 */
2441 if (host->mmc->card_clock_off)
2442 return 0;
2443 pr_debug("%s: request to disable clocks\n",
2444 mmc_hostname(host->mmc));
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002445 if (!IS_ERR_OR_NULL(msm_host->sleep_clk))
2446 clk_disable_unprepare(msm_host->sleep_clk);
2447 if (!IS_ERR_OR_NULL(msm_host->ff_clk))
2448 clk_disable_unprepare(msm_host->ff_clk);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302449 clk_disable_unprepare(msm_host->clk);
2450 if (!IS_ERR(msm_host->pclk))
2451 clk_disable_unprepare(msm_host->pclk);
2452 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2453 clk_disable_unprepare(msm_host->bus_clk);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302454
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302455 atomic_set(&msm_host->controller_clock, 0);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302456 sdhci_msm_bus_voting(host, 0);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302457 }
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302458 atomic_set(&msm_host->clks_on, enable);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302459 goto out;
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002460disable_ff_clk:
2461 if (!IS_ERR_OR_NULL(msm_host->ff_clk))
2462 clk_disable_unprepare(msm_host->ff_clk);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302463disable_bus_clk:
2464 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2465 clk_disable_unprepare(msm_host->bus_clk);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302466disable_controller_clk:
2467 if (!IS_ERR_OR_NULL(msm_host->clk))
2468 clk_disable_unprepare(msm_host->clk);
2469 if (!IS_ERR_OR_NULL(msm_host->pclk))
2470 clk_disable_unprepare(msm_host->pclk);
2471 atomic_set(&msm_host->controller_clock, 0);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302472remove_vote:
2473 if (msm_host->msm_bus_vote.client_handle)
2474 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302475out:
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302476 return rc;
2477}
2478
2479static void sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock)
2480{
2481 int rc;
2482 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2483 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2484 struct mmc_ios curr_ios = host->mmc->ios;
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002485 u32 sup_clock, ddr_clock, dll_lock;
Sahitya Tummala043744a2013-06-24 09:55:33 +05302486 bool curr_pwrsave;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302487
2488 if (!clock) {
Sujit Reddy Thummabf1aecc2014-01-10 10:58:54 +05302489 /*
2490 * disable pwrsave to ensure clock is not auto-gated until
2491 * the rate is >400KHz (initialization complete).
2492 */
2493 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2494 ~CORE_CLK_PWRSAVE, host->ioaddr + CORE_VENDOR_SPEC);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302495 sdhci_msm_prepare_clocks(host, false);
2496 host->clock = clock;
2497 goto out;
2498 }
2499
2500 rc = sdhci_msm_prepare_clocks(host, true);
2501 if (rc)
2502 goto out;
2503
Sahitya Tummala043744a2013-06-24 09:55:33 +05302504 curr_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2505 CORE_CLK_PWRSAVE);
Sahitya Tummalae000b242013-08-29 16:21:08 +05302506 if ((clock > 400000) &&
Sahitya Tummala043744a2013-06-24 09:55:33 +05302507 !curr_pwrsave && mmc_host_may_gate_card(host->mmc->card))
2508 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2509 | CORE_CLK_PWRSAVE,
2510 host->ioaddr + CORE_VENDOR_SPEC);
2511 /*
2512 * Disable pwrsave for a newly added card if doesn't allow clock
2513 * gating.
2514 */
2515 else if (curr_pwrsave && !mmc_host_may_gate_card(host->mmc->card))
2516 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2517 & ~CORE_CLK_PWRSAVE,
2518 host->ioaddr + CORE_VENDOR_SPEC);
2519
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302520 sup_clock = sdhci_msm_get_sup_clk_rate(host, clock);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002521 if ((curr_ios.timing == MMC_TIMING_UHS_DDR50) ||
2522 (curr_ios.timing == MMC_TIMING_MMC_HS400)) {
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302523 /*
2524 * The SDHC requires internal clock frequency to be double the
2525 * actual clock that will be set for DDR mode. The controller
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002526 * uses the faster clock(100/400MHz) for some of its parts and
2527 * send the actual required clock (50/200MHz) to the card.
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302528 */
2529 ddr_clock = clock * 2;
2530 sup_clock = sdhci_msm_get_sup_clk_rate(host,
2531 ddr_clock);
2532 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002533
2534 /*
2535 * In general all timing modes are controlled via UHS mode select in
2536 * Host Control2 register. eMMC specific HS200/HS400 doesn't have
2537 * their respective modes defined here, hence we use these values.
2538 *
2539 * HS200 - SDR104 (Since they both are equivalent in functionality)
2540 * HS400 - This involves multiple configurations
2541 * Initially SDR104 - when tuning is required as HS200
2542 * Then when switching to DDR @ 400MHz (HS400) we use
2543 * the vendor specific HC_SELECT_IN to control the mode.
2544 *
2545 * In addition to controlling the modes we also need to select the
2546 * correct input clock for DLL depending on the mode.
2547 *
2548 * HS400 - divided clock (free running MCLK/2)
2549 * All other modes - default (free running MCLK)
2550 */
2551 if (curr_ios.timing == MMC_TIMING_MMC_HS400) {
2552 /* Select the divided clock (free running MCLK/2) */
2553 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2554 & ~CORE_HC_MCLK_SEL_MASK)
2555 | CORE_HC_MCLK_SEL_HS400),
2556 host->ioaddr + CORE_VENDOR_SPEC);
2557 /*
2558 * Select HS400 mode using the HC_SELECT_IN from VENDOR SPEC
2559 * register
2560 */
2561 if (msm_host->tuning_done && !msm_host->calibration_done) {
2562 /*
2563 * Write 0x6 to HC_SELECT_IN and 1 to HC_SELECT_IN_EN
2564 * field in VENDOR_SPEC_FUNC
2565 */
2566 writel_relaxed((readl_relaxed(host->ioaddr + \
2567 CORE_VENDOR_SPEC)
2568 | CORE_HC_SELECT_IN_HS400
2569 | CORE_HC_SELECT_IN_EN),
2570 host->ioaddr + CORE_VENDOR_SPEC);
2571 }
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002572 if (!host->mmc->ios.old_rate && !msm_host->use_cdclp533) {
2573 /*
2574 * Poll on DLL_LOCK and DDR_DLL_LOCK bits in
2575 * CORE_DLL_STATUS to be set. This should get set
2576 * with in 15 us at 200 MHz.
2577 */
2578 rc = readl_poll_timeout(host->ioaddr + CORE_DLL_STATUS,
2579 dll_lock, (dll_lock & (CORE_DLL_LOCK |
2580 CORE_DDR_DLL_LOCK)), 10, 1000);
2581 if (rc == -ETIMEDOUT)
2582 pr_err("%s: Unable to get DLL_LOCK/DDR_DLL_LOCK, dll_status: 0x%08x\n",
2583 mmc_hostname(host->mmc),
2584 dll_lock);
2585 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002586 } else {
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002587 if (!msm_host->use_cdclp533)
2588 /* set CORE_PWRSAVE_DLL bit in CORE_VENDOR_SPEC3 */
2589 writel_relaxed((readl_relaxed(host->ioaddr +
2590 CORE_VENDOR_SPEC3) & ~CORE_PWRSAVE_DLL),
2591 host->ioaddr + CORE_VENDOR_SPEC3);
2592
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002593 /* Select the default clock (free running MCLK) */
2594 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2595 & ~CORE_HC_MCLK_SEL_MASK)
2596 | CORE_HC_MCLK_SEL_DFLT),
2597 host->ioaddr + CORE_VENDOR_SPEC);
2598
2599 /*
2600 * Disable HC_SELECT_IN to be able to use the UHS mode select
2601 * configuration from Host Control2 register for all other
2602 * modes.
2603 *
2604 * Write 0 to HC_SELECT_IN and HC_SELECT_IN_EN field
2605 * in VENDOR_SPEC_FUNC
2606 */
2607 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2608 & ~CORE_HC_SELECT_IN_EN
2609 & ~CORE_HC_SELECT_IN_MASK),
2610 host->ioaddr + CORE_VENDOR_SPEC);
2611 }
2612 mb();
2613
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302614 if (sup_clock != msm_host->clk_rate) {
2615 pr_debug("%s: %s: setting clk rate to %u\n",
2616 mmc_hostname(host->mmc), __func__, sup_clock);
2617 rc = clk_set_rate(msm_host->clk, sup_clock);
2618 if (rc) {
2619 pr_err("%s: %s: Failed to set rate %u for host-clk : %d\n",
2620 mmc_hostname(host->mmc), __func__,
2621 sup_clock, rc);
2622 goto out;
2623 }
2624 msm_host->clk_rate = sup_clock;
2625 host->clock = clock;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302626 /*
2627 * Update the bus vote in case of frequency change due to
2628 * clock scaling.
2629 */
2630 sdhci_msm_bus_voting(host, 1);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302631 }
2632out:
2633 sdhci_set_clock(host, clock);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302634}
2635
Sahitya Tummala14613432013-03-21 11:13:25 +05302636static void sdhci_msm_set_uhs_signaling(struct sdhci_host *host,
2637 unsigned int uhs)
2638{
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002639 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2640 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala14613432013-03-21 11:13:25 +05302641 u16 ctrl_2;
2642
2643 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2644 /* Select Bus Speed Mode for host */
2645 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002646 if (uhs == MMC_TIMING_MMC_HS400)
2647 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2648 else if (uhs == MMC_TIMING_MMC_HS200)
Sahitya Tummala14613432013-03-21 11:13:25 +05302649 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2650 else if (uhs == MMC_TIMING_UHS_SDR12)
2651 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
2652 else if (uhs == MMC_TIMING_UHS_SDR25)
2653 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
2654 else if (uhs == MMC_TIMING_UHS_SDR50)
2655 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
2656 else if (uhs == MMC_TIMING_UHS_SDR104)
2657 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2658 else if (uhs == MMC_TIMING_UHS_DDR50)
2659 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302660 /*
2661 * When clock frquency is less than 100MHz, the feedback clock must be
2662 * provided and DLL must not be used so that tuning can be skipped. To
2663 * provide feedback clock, the mode selection can be any value less
2664 * than 3'b011 in bits [2:0] of HOST CONTROL2 register.
2665 */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002666 if (host->clock <= CORE_FREQ_100MHZ) {
2667 if ((uhs == MMC_TIMING_MMC_HS400) ||
2668 (uhs == MMC_TIMING_MMC_HS200) ||
2669 (uhs == MMC_TIMING_UHS_SDR104))
2670 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302671
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002672 /*
2673 * Make sure DLL is disabled when not required
2674 *
2675 * Write 1 to DLL_RST bit of DLL_CONFIG register
2676 */
2677 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
2678 | CORE_DLL_RST),
2679 host->ioaddr + CORE_DLL_CONFIG);
2680
2681 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
2682 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
2683 | CORE_DLL_PDN),
2684 host->ioaddr + CORE_DLL_CONFIG);
2685 mb();
2686
2687 /*
2688 * The DLL needs to be restored and CDCLP533 recalibrated
2689 * when the clock frequency is set back to 400MHz.
2690 */
2691 msm_host->calibration_done = false;
2692 }
2693
2694 pr_debug("%s: %s-clock:%u uhs mode:%u ctrl_2:0x%x\n",
2695 mmc_hostname(host->mmc), __func__, host->clock, uhs, ctrl_2);
Sahitya Tummala14613432013-03-21 11:13:25 +05302696 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
2697
2698}
2699
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302700#define MAX_TEST_BUS 20
2701
2702void sdhci_msm_dump_vendor_regs(struct sdhci_host *host)
2703{
2704 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2705 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2706 int tbsel, tbsel2;
2707 int i, index = 0;
2708 u32 test_bus_val = 0;
2709 u32 debug_reg[MAX_TEST_BUS] = {0};
2710
2711 pr_info("----------- VENDOR REGISTER DUMP -----------\n");
2712 pr_info("Data cnt: 0x%08x | Fifo cnt: 0x%08x | Int sts: 0x%08x\n",
2713 readl_relaxed(msm_host->core_mem + CORE_MCI_DATA_CNT),
2714 readl_relaxed(msm_host->core_mem + CORE_MCI_FIFO_CNT),
2715 readl_relaxed(msm_host->core_mem + CORE_MCI_STATUS));
2716 pr_info("DLL cfg: 0x%08x | DLL sts: 0x%08x | SDCC ver: 0x%08x\n",
2717 readl_relaxed(host->ioaddr + CORE_DLL_CONFIG),
2718 readl_relaxed(host->ioaddr + CORE_DLL_STATUS),
2719 readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION));
2720 pr_info("Vndr func: 0x%08x | Vndr adma err : addr0: 0x%08x addr1: 0x%08x\n",
2721 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC),
2722 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_ADMA_ERR_ADDR0),
2723 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_ADMA_ERR_ADDR1));
2724
2725 /*
2726 * tbsel indicates [2:0] bits and tbsel2 indicates [7:4] bits
2727 * of CORE_TESTBUS_CONFIG register.
2728 *
2729 * To select test bus 0 to 7 use tbsel and to select any test bus
2730 * above 7 use (tbsel2 | tbsel) to get the test bus number. For eg,
2731 * to select test bus 14, write 0x1E to CORE_TESTBUS_CONFIG register
2732 * i.e., tbsel2[7:4] = 0001, tbsel[2:0] = 110.
2733 */
2734 for (tbsel2 = 0; tbsel2 < 3; tbsel2++) {
2735 for (tbsel = 0; tbsel < 8; tbsel++) {
2736 if (index >= MAX_TEST_BUS)
2737 break;
2738 test_bus_val = (tbsel2 << CORE_TESTBUS_SEL2_BIT) |
2739 tbsel | CORE_TESTBUS_ENA;
2740 writel_relaxed(test_bus_val,
2741 msm_host->core_mem + CORE_TESTBUS_CONFIG);
2742 debug_reg[index++] = readl_relaxed(msm_host->core_mem +
2743 CORE_SDCC_DEBUG_REG);
2744 }
2745 }
2746 for (i = 0; i < MAX_TEST_BUS; i = i + 4)
2747 pr_info(" Test bus[%d to %d]: 0x%08x 0x%08x 0x%08x 0x%08x\n",
2748 i, i + 3, debug_reg[i], debug_reg[i+1],
2749 debug_reg[i+2], debug_reg[i+3]);
2750 /* Disable test bus */
2751 writel_relaxed(~CORE_TESTBUS_ENA, msm_host->core_mem +
2752 CORE_TESTBUS_CONFIG);
2753}
2754
Asutosh Das0ef24812012-12-18 16:14:02 +05302755static struct sdhci_ops sdhci_msm_ops = {
Sahitya Tummala14613432013-03-21 11:13:25 +05302756 .set_uhs_signaling = sdhci_msm_set_uhs_signaling,
Asutosh Das0ef24812012-12-18 16:14:02 +05302757 .check_power_status = sdhci_msm_check_power_status,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002758 .platform_execute_tuning = sdhci_msm_execute_tuning,
2759 .toggle_cdr = sdhci_msm_toggle_cdr,
Asutosh Das648f9d12013-01-10 21:11:04 +05302760 .get_max_segments = sdhci_msm_max_segs,
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302761 .set_clock = sdhci_msm_set_clock,
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302762 .get_min_clock = sdhci_msm_get_min_clock,
2763 .get_max_clock = sdhci_msm_get_max_clock,
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302764 .dump_vendor_regs = sdhci_msm_dump_vendor_regs,
Asutosh Dase5e9ca62013-07-30 19:08:36 +05302765 .config_auto_tuning_cmd = sdhci_msm_config_auto_tuning_cmd,
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302766 .enable_controller_clock = sdhci_msm_enable_controller_clock,
Venkat Gopalakrishnanb8cb7072015-01-09 11:04:34 -08002767 .set_bus_width = sdhci_set_bus_width,
Venkat Gopalakrishnan411df072015-01-09 11:09:44 -08002768 .reset = sdhci_reset,
Asutosh Das0ef24812012-12-18 16:14:02 +05302769};
2770
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302771static void sdhci_set_default_hw_caps(struct sdhci_msm_host *msm_host,
2772 struct sdhci_host *host)
2773{
2774 u32 version, caps;
2775 u16 minor;
2776 u8 major;
2777
2778 version = readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION);
2779 major = (version & CORE_VERSION_MAJOR_MASK) >>
2780 CORE_VERSION_MAJOR_SHIFT;
2781 minor = version & CORE_VERSION_TARGET_MASK;
2782
2783 /*
2784 * Starting with SDCC 5 controller (core major version = 1)
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002785 * controller won't advertise 3.0v, 1.8v and 8-bit features
2786 * except for some targets.
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302787 */
2788 if (major >= 1 && minor != 0x11 && minor != 0x12) {
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002789 struct sdhci_msm_reg_data *vdd_io_reg;
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302790 caps = CORE_3_0V_SUPPORT;
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002791 /*
2792 * Enable 1.8V support capability on controllers that
2793 * support dual voltage
2794 */
2795 vdd_io_reg = msm_host->pdata->vreg_data->vdd_io_data;
2796 if (vdd_io_reg &&
2797 (vdd_io_reg->low_vol_level != vdd_io_reg->high_vol_level))
2798 caps |= CORE_1_8V_SUPPORT;
Pratibhasagar Vada47992013-12-09 20:42:32 +05302799 if (msm_host->pdata->mmc_bus_width == MMC_CAP_8_BIT_DATA)
2800 caps |= CORE_8_BIT_SUPPORT;
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302801 writel_relaxed(
2802 (readl_relaxed(host->ioaddr + SDHCI_CAPABILITIES) |
2803 caps), host->ioaddr + CORE_VENDOR_SPEC_CAPABILITIES0);
2804 }
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002805
2806 /*
2807 * SDCC 5 controller with major version 1, minor version 0x34 and later
2808 * with HS 400 mode support will use CM DLL instead of CDC LP 533 DLL.
2809 */
2810 if ((major == 1) && (minor < 0x34))
2811 msm_host->use_cdclp533 = true;
Gilad Broner2a10ca02014-10-02 17:20:35 +03002812
2813 /*
2814 * Mask 64-bit support for controller with 32-bit address bus so that
2815 * smaller descriptor size will be used and improve memory consumption.
2816 * In case bus addressing ever changes, controller version should be
2817 * used in order to decide whether or not to mask 64-bit support.
2818 */
2819 caps = readl_relaxed(host->ioaddr + SDHCI_CAPABILITIES);
2820 caps &= ~CORE_SYS_BUS_SUPPORT_64_BIT;
2821 writel_relaxed(caps, host->ioaddr + CORE_VENDOR_SPEC_CAPABILITIES0);
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302822}
2823
Asutosh Das0ef24812012-12-18 16:14:02 +05302824static int sdhci_msm_probe(struct platform_device *pdev)
2825{
2826 struct sdhci_host *host;
2827 struct sdhci_pltfm_host *pltfm_host;
2828 struct sdhci_msm_host *msm_host;
2829 struct resource *core_memres = NULL;
2830 int ret = 0, pwr_irq = 0, dead = 0;
Stephen Boyd8dce5c62013-04-24 14:19:46 -07002831 u16 host_version;
Subhash Jadavani28137342013-05-14 17:46:43 +05302832 u32 pwr, irq_status, irq_ctl;
Asutosh Das0ef24812012-12-18 16:14:02 +05302833
2834 pr_debug("%s: Enter %s\n", dev_name(&pdev->dev), __func__);
2835 msm_host = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_msm_host),
2836 GFP_KERNEL);
2837 if (!msm_host) {
2838 ret = -ENOMEM;
2839 goto out;
2840 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302841
2842 msm_host->sdhci_msm_pdata.ops = &sdhci_msm_ops;
2843 host = sdhci_pltfm_init(pdev, &msm_host->sdhci_msm_pdata, 0);
2844 if (IS_ERR(host)) {
2845 ret = PTR_ERR(host);
2846 goto out;
2847 }
2848
2849 pltfm_host = sdhci_priv(host);
2850 pltfm_host->priv = msm_host;
2851 msm_host->mmc = host->mmc;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302852 msm_host->pdev = pdev;
Asutosh Das0ef24812012-12-18 16:14:02 +05302853
2854 /* Extract platform data */
2855 if (pdev->dev.of_node) {
Venkat Gopalakrishnan270580a2013-03-11 12:17:57 -07002856 ret = of_alias_get_id(pdev->dev.of_node, "sdhc");
2857 if (ret < 0) {
2858 dev_err(&pdev->dev, "Failed to get slot index %d\n",
2859 ret);
2860 goto pltfm_free;
2861 }
2862 if (disable_slots & (1 << (ret - 1))) {
2863 dev_info(&pdev->dev, "%s: Slot %d disabled\n", __func__,
2864 ret);
2865 ret = -ENODEV;
2866 goto pltfm_free;
2867 }
2868
Asutosh Das0ef24812012-12-18 16:14:02 +05302869 msm_host->pdata = sdhci_msm_populate_pdata(&pdev->dev);
2870 if (!msm_host->pdata) {
2871 dev_err(&pdev->dev, "DT parsing error\n");
2872 goto pltfm_free;
2873 }
2874 } else {
2875 dev_err(&pdev->dev, "No device tree node\n");
2876 goto pltfm_free;
2877 }
2878
2879 /* Setup Clocks */
2880
2881 /* Setup SDCC bus voter clock. */
2882 msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus_clk");
2883 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
2884 /* Vote for max. clk rate for max. performance */
2885 ret = clk_set_rate(msm_host->bus_clk, INT_MAX);
2886 if (ret)
2887 goto pltfm_free;
2888 ret = clk_prepare_enable(msm_host->bus_clk);
2889 if (ret)
2890 goto pltfm_free;
2891 }
2892
2893 /* Setup main peripheral bus clock */
2894 msm_host->pclk = devm_clk_get(&pdev->dev, "iface_clk");
2895 if (!IS_ERR(msm_host->pclk)) {
2896 ret = clk_prepare_enable(msm_host->pclk);
2897 if (ret)
2898 goto bus_clk_disable;
2899 }
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302900 atomic_set(&msm_host->controller_clock, 1);
Asutosh Das0ef24812012-12-18 16:14:02 +05302901
2902 /* Setup SDC MMC clock */
2903 msm_host->clk = devm_clk_get(&pdev->dev, "core_clk");
2904 if (IS_ERR(msm_host->clk)) {
2905 ret = PTR_ERR(msm_host->clk);
2906 goto pclk_disable;
2907 }
2908
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302909 /* Set to the minimum supported clock frequency */
2910 ret = clk_set_rate(msm_host->clk, sdhci_msm_get_min_clock(host));
2911 if (ret) {
2912 dev_err(&pdev->dev, "MClk rate set failed (%d)\n", ret);
Sahitya Tummala020ede0d2013-06-07 13:03:07 +05302913 goto pclk_disable;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302914 }
Sahitya Tummala020ede0d2013-06-07 13:03:07 +05302915 ret = clk_prepare_enable(msm_host->clk);
2916 if (ret)
2917 goto pclk_disable;
2918
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302919 msm_host->clk_rate = sdhci_msm_get_min_clock(host);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302920 atomic_set(&msm_host->clks_on, 1);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302921
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002922 /* Setup CDC calibration fixed feedback clock */
2923 msm_host->ff_clk = devm_clk_get(&pdev->dev, "cal_clk");
2924 if (!IS_ERR(msm_host->ff_clk)) {
2925 ret = clk_prepare_enable(msm_host->ff_clk);
2926 if (ret)
2927 goto clk_disable;
2928 }
2929
2930 /* Setup CDC calibration sleep clock */
2931 msm_host->sleep_clk = devm_clk_get(&pdev->dev, "sleep_clk");
2932 if (!IS_ERR(msm_host->sleep_clk)) {
2933 ret = clk_prepare_enable(msm_host->sleep_clk);
2934 if (ret)
2935 goto ff_clk_disable;
2936 }
2937
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07002938 msm_host->saved_tuning_phase = INVALID_TUNING_PHASE;
2939
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302940 ret = sdhci_msm_bus_register(msm_host, pdev);
2941 if (ret)
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002942 goto sleep_clk_disable;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302943
2944 if (msm_host->msm_bus_vote.client_handle)
2945 INIT_DELAYED_WORK(&msm_host->msm_bus_vote.vote_work,
2946 sdhci_msm_bus_work);
2947 sdhci_msm_bus_voting(host, 1);
2948
Asutosh Das0ef24812012-12-18 16:14:02 +05302949 /* Setup regulators */
2950 ret = sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, true);
2951 if (ret) {
2952 dev_err(&pdev->dev, "Regulator setup failed (%d)\n", ret);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302953 goto bus_unregister;
Asutosh Das0ef24812012-12-18 16:14:02 +05302954 }
2955
2956 /* Reset the core and Enable SDHC mode */
2957 core_memres = platform_get_resource_byname(pdev,
2958 IORESOURCE_MEM, "core_mem");
Asutosh Das890bdee2014-08-08 23:01:42 +05302959 if (!core_memres) {
2960 dev_err(&pdev->dev, "Failed to get iomem resource\n");
2961 goto vreg_deinit;
2962 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302963 msm_host->core_mem = devm_ioremap(&pdev->dev, core_memres->start,
2964 resource_size(core_memres));
2965
2966 if (!msm_host->core_mem) {
2967 dev_err(&pdev->dev, "Failed to remap registers\n");
2968 ret = -ENOMEM;
2969 goto vreg_deinit;
2970 }
2971
Stepan Moskovchenkoa4d0fa72013-09-13 22:19:33 -07002972 /* Unset HC_MODE_EN bit in HC_MODE register */
2973 writel_relaxed(0, (msm_host->core_mem + CORE_HC_MODE));
2974
Asutosh Das0ef24812012-12-18 16:14:02 +05302975 /* Set SW_RST bit in POWER register (Offset 0x0) */
Sahitya Tummala66b0fe32013-04-25 11:50:56 +05302976 writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_POWER) |
2977 CORE_SW_RST, msm_host->core_mem + CORE_POWER);
2978 /*
2979 * SW reset can take upto 10HCLK + 15MCLK cycles.
2980 * Calculating based on min clk rates (hclk = 27MHz,
2981 * mclk = 400KHz) it comes to ~40us. Let's poll for
2982 * max. 1ms for reset completion.
2983 */
2984 ret = readl_poll_timeout(msm_host->core_mem + CORE_POWER,
Matt Wagantallc7097a22014-04-29 15:48:15 -07002985 pwr, !(pwr & CORE_SW_RST), 10, 1000);
Sahitya Tummala66b0fe32013-04-25 11:50:56 +05302986
2987 if (ret) {
2988 dev_err(&pdev->dev, "reset failed (%d)\n", ret);
2989 goto vreg_deinit;
2990 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302991 /* Set HC_MODE_EN bit in HC_MODE register */
2992 writel_relaxed(HC_MODE_EN, (msm_host->core_mem + CORE_HC_MODE));
2993
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002994 /* Set FF_CLK_SW_RST_DIS bit in HC_MODE register */
2995 writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_HC_MODE) |
2996 FF_CLK_SW_RST_DIS, msm_host->core_mem + CORE_HC_MODE);
2997
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302998 sdhci_set_default_hw_caps(msm_host, host);
Asutosh Das0ef24812012-12-18 16:14:02 +05302999 /*
Subhash Jadavani28137342013-05-14 17:46:43 +05303000 * CORE_SW_RST above may trigger power irq if previous status of PWRCTL
3001 * was either BUS_ON or IO_HIGH_V. So before we enable the power irq
3002 * interrupt in GIC (by registering the interrupt handler), we need to
3003 * ensure that any pending power irq interrupt status is acknowledged
3004 * otherwise power irq interrupt handler would be fired prematurely.
3005 */
3006 irq_status = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
3007 writel_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
3008 irq_ctl = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_CTL);
3009 if (irq_status & (CORE_PWRCTL_BUS_ON | CORE_PWRCTL_BUS_OFF))
3010 irq_ctl |= CORE_PWRCTL_BUS_SUCCESS;
3011 if (irq_status & (CORE_PWRCTL_IO_HIGH | CORE_PWRCTL_IO_LOW))
3012 irq_ctl |= CORE_PWRCTL_IO_SUCCESS;
3013 writel_relaxed(irq_ctl, (msm_host->core_mem + CORE_PWRCTL_CTL));
3014 /*
3015 * Ensure that above writes are propogated before interrupt enablement
3016 * in GIC.
3017 */
3018 mb();
3019
3020 /*
Asutosh Das0ef24812012-12-18 16:14:02 +05303021 * Following are the deviations from SDHC spec v3.0 -
3022 * 1. Card detection is handled using separate GPIO.
3023 * 2. Bus power control is handled by interacting with PMIC.
3024 */
3025 host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
3026 host->quirks |= SDHCI_QUIRK_SINGLE_POWER_WRITE;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05303027 host->quirks |= SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN;
3028 host->quirks2 |= SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK;
Sahitya Tummala87d43942013-04-12 11:49:11 +05303029 host->quirks2 |= SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD;
Sahitya Tummala314162c2013-04-12 12:11:20 +05303030 host->quirks2 |= SDHCI_QUIRK2_BROKEN_PRESET_VALUE;
Sahitya Tummala7c9780d2013-04-12 11:59:25 +05303031 host->quirks2 |= SDHCI_QUIRK2_USE_RESERVED_MAX_TIMEOUT;
Asutosh Das0ef24812012-12-18 16:14:02 +05303032
Sahitya Tummalaa5733ab52013-06-10 16:32:51 +05303033 if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK)
3034 host->quirks2 |= SDHCI_QUIRK2_DIVIDE_TOUT_BY_4;
3035
Stephen Boyd8dce5c62013-04-24 14:19:46 -07003036 host_version = readw_relaxed((host->ioaddr + SDHCI_HOST_VERSION));
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07003037 dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n",
3038 host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >>
3039 SDHCI_VENDOR_VER_SHIFT));
3040 if (((host_version & SDHCI_VENDOR_VER_MASK) >>
3041 SDHCI_VENDOR_VER_SHIFT) == SDHCI_VER_100) {
3042 /*
3043 * Add 40us delay in interrupt handler when
3044 * operating at initialization frequency(400KHz).
3045 */
3046 host->quirks2 |= SDHCI_QUIRK2_SLOW_INT_CLR;
3047 /*
3048 * Set Software Reset for DAT line in Software
3049 * Reset Register (Bit 2).
3050 */
3051 host->quirks2 |= SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT;
3052 }
3053
Asutosh Das214b9662013-06-13 14:27:42 +05303054 host->quirks2 |= SDHCI_QUIRK2_IGN_DATA_END_BIT_ERROR;
3055
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07003056 /* Setup PWRCTL irq */
Asutosh Das0ef24812012-12-18 16:14:02 +05303057 pwr_irq = platform_get_irq_byname(pdev, "pwr_irq");
3058 if (pwr_irq < 0) {
3059 dev_err(&pdev->dev, "Failed to get pwr_irq by name (%d)\n",
3060 pwr_irq);
3061 goto vreg_deinit;
3062 }
3063 ret = devm_request_threaded_irq(&pdev->dev, pwr_irq, NULL,
3064 sdhci_msm_pwr_irq, IRQF_ONESHOT,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07003065 dev_name(&pdev->dev), host);
Asutosh Das0ef24812012-12-18 16:14:02 +05303066 if (ret) {
3067 dev_err(&pdev->dev, "Request threaded irq(%d) failed (%d)\n",
3068 pwr_irq, ret);
3069 goto vreg_deinit;
3070 }
3071
3072 /* Enable pwr irq interrupts */
3073 writel_relaxed(INT_MASK, (msm_host->core_mem + CORE_PWRCTL_MASK));
3074
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05303075#ifdef CONFIG_MMC_CLKGATE
3076 /* Set clock gating delay to be used when CONFIG_MMC_CLKGATE is set */
3077 msm_host->mmc->clkgate_delay = SDHCI_MSM_MMC_CLK_GATE_DELAY;
3078#endif
3079
Asutosh Das0ef24812012-12-18 16:14:02 +05303080 /* Set host capabilities */
3081 msm_host->mmc->caps |= msm_host->pdata->mmc_bus_width;
3082 msm_host->mmc->caps |= msm_host->pdata->caps;
Asutosh Das0ef24812012-12-18 16:14:02 +05303083 msm_host->mmc->caps2 |= msm_host->pdata->caps2;
3084 msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR;
3085 msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR_CONTROL;
Venkat Gopalakrishnan97c16112014-12-16 18:26:25 -08003086 msm_host->mmc->caps2 |= MMC_CAP2_BOOTPART_NOACC;
3087 msm_host->mmc->caps2 |= MMC_CAP2_FULL_PWR_CYCLE;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05303088 msm_host->mmc->caps2 |= MMC_CAP2_CLK_SCALE;
Subhash Jadavani6d472b22013-05-29 15:52:10 +05303089 msm_host->mmc->caps2 |= MMC_CAP2_ASYNC_SDIO_IRQ_4BIT_MODE;
Venkat Gopalakrishnan97c16112014-12-16 18:26:25 -08003090 msm_host->mmc->caps2 |= MMC_CAP2_HS400_POST_TUNING;
Asutosh Dasbea115d2013-06-24 18:20:45 +05303091 msm_host->mmc->pm_caps |= MMC_PM_KEEP_POWER;
Asutosh Das0ef24812012-12-18 16:14:02 +05303092
3093 if (msm_host->pdata->nonremovable)
3094 msm_host->mmc->caps |= MMC_CAP_NONREMOVABLE;
3095
Guoping Yuf7c91332014-08-20 16:56:18 +08003096 if (msm_host->pdata->nonhotplug)
3097 msm_host->mmc->caps2 |= MMC_CAP2_NONHOTPLUG;
3098
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05303099 host->cpu_dma_latency_us = msm_host->pdata->cpu_dma_latency_us;
Maya Erez994cf2f2014-10-21 20:22:04 +03003100 host->pm_qos_req_dma.type = msm_host->pdata->cpu_affinity_type;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05303101
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05303102 init_completion(&msm_host->pwr_irq_completion);
3103
Sahitya Tummala581df132013-03-12 14:57:46 +05303104 if (gpio_is_valid(msm_host->pdata->status_gpio)) {
Sahitya Tummala6ddabb42014-06-05 13:26:55 +05303105 /*
3106 * Set up the card detect GPIO in active configuration before
3107 * configuring it as an IRQ. Otherwise, it can be in some
3108 * weird/inconsistent state resulting in flood of interrupts.
3109 */
3110 sdhci_msm_setup_pins(msm_host->pdata, true);
3111
Sahitya Tummala581df132013-03-12 14:57:46 +05303112 ret = mmc_gpio_request_cd(msm_host->mmc,
3113 msm_host->pdata->status_gpio, 0);
3114 if (ret) {
3115 dev_err(&pdev->dev, "%s: Failed to request card detection IRQ %d\n",
3116 __func__, ret);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05303117 goto vreg_deinit;
Sahitya Tummala581df132013-03-12 14:57:46 +05303118 }
3119 }
3120
Krishna Konda7feab352013-09-17 23:55:40 -07003121 if ((sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT) &&
3122 (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(64)))) {
3123 host->dma_mask = DMA_BIT_MASK(64);
3124 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
3125 } else if (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(32))) {
Sahitya Tummalaeaa21862013-03-20 19:34:59 +05303126 host->dma_mask = DMA_BIT_MASK(32);
3127 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
3128 } else {
3129 dev_err(&pdev->dev, "%s: Failed to set dma mask\n", __func__);
3130 }
3131
Asutosh Das0ef24812012-12-18 16:14:02 +05303132 ret = sdhci_add_host(host);
3133 if (ret) {
3134 dev_err(&pdev->dev, "Add host failed (%d)\n", ret);
Sahitya Tummala581df132013-03-12 14:57:46 +05303135 goto vreg_deinit;
Asutosh Das0ef24812012-12-18 16:14:02 +05303136 }
3137
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05303138 msm_host->msm_bus_vote.max_bus_bw.show = show_sdhci_max_bus_bw;
3139 msm_host->msm_bus_vote.max_bus_bw.store = store_sdhci_max_bus_bw;
3140 sysfs_attr_init(&msm_host->msm_bus_vote.max_bus_bw.attr);
3141 msm_host->msm_bus_vote.max_bus_bw.attr.name = "max_bus_bw";
3142 msm_host->msm_bus_vote.max_bus_bw.attr.mode = S_IRUGO | S_IWUSR;
3143 ret = device_create_file(&pdev->dev,
3144 &msm_host->msm_bus_vote.max_bus_bw);
3145 if (ret)
3146 goto remove_host;
3147
Sahitya Tummala5c55b932013-06-20 14:00:18 +05303148 if (!gpio_is_valid(msm_host->pdata->status_gpio)) {
3149 msm_host->polling.show = show_polling;
3150 msm_host->polling.store = store_polling;
3151 sysfs_attr_init(&msm_host->polling.attr);
3152 msm_host->polling.attr.name = "polling";
3153 msm_host->polling.attr.mode = S_IRUGO | S_IWUSR;
3154 ret = device_create_file(&pdev->dev, &msm_host->polling);
3155 if (ret)
3156 goto remove_max_bus_bw_file;
3157 }
Asutosh Dase5e9ca62013-07-30 19:08:36 +05303158
3159 msm_host->auto_cmd21_attr.show = show_auto_cmd21;
3160 msm_host->auto_cmd21_attr.store = store_auto_cmd21;
3161 sysfs_attr_init(&msm_host->auto_cmd21_attr.attr);
3162 msm_host->auto_cmd21_attr.attr.name = "enable_auto_cmd21";
3163 msm_host->auto_cmd21_attr.attr.mode = S_IRUGO | S_IWUSR;
3164 ret = device_create_file(&pdev->dev, &msm_host->auto_cmd21_attr);
3165 if (ret) {
3166 pr_err("%s: %s: failed creating auto-cmd21 attr: %d\n",
3167 mmc_hostname(host->mmc), __func__, ret);
3168 device_remove_file(&pdev->dev, &msm_host->auto_cmd21_attr);
3169 }
3170
Asutosh Das0ef24812012-12-18 16:14:02 +05303171 /* Successful initialization */
3172 goto out;
3173
Sahitya Tummala5c55b932013-06-20 14:00:18 +05303174remove_max_bus_bw_file:
3175 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Asutosh Das0ef24812012-12-18 16:14:02 +05303176remove_host:
3177 dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
3178 sdhci_remove_host(host, dead);
3179vreg_deinit:
3180 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05303181bus_unregister:
3182 if (msm_host->msm_bus_vote.client_handle)
3183 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
3184 sdhci_msm_bus_unregister(msm_host);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07003185sleep_clk_disable:
3186 if (!IS_ERR(msm_host->sleep_clk))
3187 clk_disable_unprepare(msm_host->sleep_clk);
3188ff_clk_disable:
3189 if (!IS_ERR(msm_host->ff_clk))
3190 clk_disable_unprepare(msm_host->ff_clk);
Asutosh Das0ef24812012-12-18 16:14:02 +05303191clk_disable:
3192 if (!IS_ERR(msm_host->clk))
3193 clk_disable_unprepare(msm_host->clk);
3194pclk_disable:
3195 if (!IS_ERR(msm_host->pclk))
3196 clk_disable_unprepare(msm_host->pclk);
3197bus_clk_disable:
3198 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
3199 clk_disable_unprepare(msm_host->bus_clk);
3200pltfm_free:
3201 sdhci_pltfm_free(pdev);
3202out:
3203 pr_debug("%s: Exit %s\n", dev_name(&pdev->dev), __func__);
3204 return ret;
3205}
3206
3207static int sdhci_msm_remove(struct platform_device *pdev)
3208{
3209 struct sdhci_host *host = platform_get_drvdata(pdev);
3210 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3211 struct sdhci_msm_host *msm_host = pltfm_host->priv;
3212 struct sdhci_msm_pltfm_data *pdata = msm_host->pdata;
3213 int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) ==
3214 0xffffffff);
3215
3216 pr_debug("%s: %s\n", dev_name(&pdev->dev), __func__);
Sahitya Tummala5c55b932013-06-20 14:00:18 +05303217 if (!gpio_is_valid(msm_host->pdata->status_gpio))
3218 device_remove_file(&pdev->dev, &msm_host->polling);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05303219 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Asutosh Das0ef24812012-12-18 16:14:02 +05303220 sdhci_remove_host(host, dead);
3221 sdhci_pltfm_free(pdev);
Sahitya Tummala581df132013-03-12 14:57:46 +05303222
Asutosh Das0ef24812012-12-18 16:14:02 +05303223 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05303224
Pratibhasagar V9acf2642013-11-21 21:07:21 +05303225 sdhci_msm_setup_pins(pdata, true);
3226 sdhci_msm_setup_pins(pdata, false);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05303227
3228 if (msm_host->msm_bus_vote.client_handle) {
3229 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
3230 sdhci_msm_bus_unregister(msm_host);
3231 }
Asutosh Das0ef24812012-12-18 16:14:02 +05303232 return 0;
3233}
3234
3235static const struct of_device_id sdhci_msm_dt_match[] = {
3236 {.compatible = "qcom,sdhci-msm"},
3237};
3238MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match);
3239
3240static struct platform_driver sdhci_msm_driver = {
3241 .probe = sdhci_msm_probe,
3242 .remove = sdhci_msm_remove,
3243 .driver = {
3244 .name = "sdhci_msm",
3245 .owner = THIS_MODULE,
3246 .of_match_table = sdhci_msm_dt_match,
3247 },
3248};
3249
3250module_platform_driver(sdhci_msm_driver);
3251
3252MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Secure Digital Host Controller Interface driver");
3253MODULE_LICENSE("GPL v2");