blob: d3a0e9ea511ec118c936cebc0890d793099623fd [file] [log] [blame]
Asutosh Das33a4ff52012-12-18 16:14:02 +05301/*
2 * drivers/mmc/host/sdhci-msm.c - Qualcomm MSM SDHCI Platform
3 * driver source file
4 *
Sujit Reddy Thumma0e9ec032014-01-10 10:58:54 +05305 * Copyright (c) 2012-2014, The Linux Foundation. All rights reserved.
Asutosh Das33a4ff52012-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 Gopalakrishnan8609a432012-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>
Asutosh Das1369f7b2013-06-03 18:56:52 +053034#include <linux/irq.h>
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -070035#include <linux/mmc/mmc.h>
Asutosh Dasbbc84782013-02-11 15:31:35 +053036#include <linux/pm.h>
37#include <linux/pm_runtime.h>
Sahitya Tummala62448d92013-03-12 14:57:46 +053038#include <linux/mmc/cd-gpio.h>
Sahitya Tummala2fa7eb12013-03-20 19:34:59 +053039#include <linux/dma-mapping.h>
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -070040#include <mach/gpio.h>
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +053041#include <mach/msm_bus.h>
Asutosh Das1369f7b2013-06-03 18:56:52 +053042#include <mach/mpm.h>
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +030043#include <linux/iopoll.h>
Asutosh Das33a4ff52012-12-18 16:14:02 +053044
45#include "sdhci-pltfm.h"
46
Asutosh Das1369f7b2013-06-03 18:56:52 +053047enum sdc_mpm_pin_state {
48 SDC_DAT1_DISABLE,
49 SDC_DAT1_ENABLE,
50 SDC_DAT1_ENWAKE,
51 SDC_DAT1_DISWAKE,
52};
53
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -070054#define SDHCI_VER_100 0x2B
Asutosh Das33a4ff52012-12-18 16:14:02 +053055#define CORE_HC_MODE 0x78
56#define HC_MODE_EN 0x1
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -070057#define FF_CLK_SW_RST_DIS (1 << 13)
Asutosh Das33a4ff52012-12-18 16:14:02 +053058
59#define CORE_POWER 0x0
60#define CORE_SW_RST (1 << 7)
61
62#define CORE_PWRCTL_STATUS 0xDC
63#define CORE_PWRCTL_MASK 0xE0
64#define CORE_PWRCTL_CLEAR 0xE4
65#define CORE_PWRCTL_CTL 0xE8
66
67#define CORE_PWRCTL_BUS_OFF 0x01
68#define CORE_PWRCTL_BUS_ON (1 << 1)
69#define CORE_PWRCTL_IO_LOW (1 << 2)
70#define CORE_PWRCTL_IO_HIGH (1 << 3)
71
72#define CORE_PWRCTL_BUS_SUCCESS 0x01
73#define CORE_PWRCTL_BUS_FAIL (1 << 1)
74#define CORE_PWRCTL_IO_SUCCESS (1 << 2)
75#define CORE_PWRCTL_IO_FAIL (1 << 3)
76
77#define INT_MASK 0xF
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -070078#define MAX_PHASES 16
79
Venkat Gopalakrishnanb6cfa292013-06-12 11:16:37 -070080#define CORE_DLL_CONFIG 0x100
81#define CORE_CMD_DAT_TRACK_SEL (1 << 0)
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -070082#define CORE_DLL_EN (1 << 16)
83#define CORE_CDR_EN (1 << 17)
84#define CORE_CK_OUT_EN (1 << 18)
85#define CORE_CDR_EXT_EN (1 << 19)
86#define CORE_DLL_PDN (1 << 29)
87#define CORE_DLL_RST (1 << 30)
Venkat Gopalakrishnanb6cfa292013-06-12 11:16:37 -070088
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -070089#define CORE_DLL_STATUS 0x108
Venkat Gopalakrishnanb6cfa292013-06-12 11:16:37 -070090#define CORE_DLL_LOCK (1 << 7)
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -070091
92#define CORE_VENDOR_SPEC 0x10C
93#define CORE_CLK_PWRSAVE (1 << 1)
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -070094#define CORE_HC_MCLK_SEL_DFLT (2 << 8)
95#define CORE_HC_MCLK_SEL_HS400 (3 << 8)
96#define CORE_HC_MCLK_SEL_MASK (3 << 8)
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -070097#define CORE_IO_PAD_PWR_SWITCH (1 << 16)
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -070098#define CORE_HC_SELECT_IN_EN (1 << 18)
99#define CORE_HC_SELECT_IN_HS400 (6 << 19)
100#define CORE_HC_SELECT_IN_MASK (7 << 19)
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700101
Venkat Gopalakrishnanb6cfa292013-06-12 11:16:37 -0700102#define CORE_VENDOR_SPEC_ADMA_ERR_ADDR0 0x114
103#define CORE_VENDOR_SPEC_ADMA_ERR_ADDR1 0x118
104
105#define CORE_CSR_CDC_CTLR_CFG0 0x130
106#define CORE_SW_TRIG_FULL_CALIB (1 << 16)
107#define CORE_HW_AUTOCAL_ENA (1 << 17)
108
109#define CORE_CSR_CDC_CTLR_CFG1 0x134
110#define CORE_CSR_CDC_CAL_TIMER_CFG0 0x138
111#define CORE_TIMER_ENA (1 << 16)
112
113#define CORE_CSR_CDC_CAL_TIMER_CFG1 0x13C
114#define CORE_CSR_CDC_REFCOUNT_CFG 0x140
115#define CORE_CSR_CDC_COARSE_CAL_CFG 0x144
116#define CORE_CDC_OFFSET_CFG 0x14C
117#define CORE_CSR_CDC_DELAY_CFG 0x150
118#define CORE_CDC_SLAVE_DDA_CFG 0x160
119#define CORE_CSR_CDC_STATUS0 0x164
120#define CORE_CALIBRATION_DONE (1 << 0)
121
122#define CORE_CDC_ERROR_CODE_MASK 0x7000000
123
124#define CORE_CSR_CDC_GEN_CFG 0x178
125#define CORE_CDC_SWITCH_BYPASS_OFF (1 << 0)
126#define CORE_CDC_SWITCH_RC_EN (1 << 1)
127
128#define CORE_DDR_200_CFG 0x184
129#define CORE_CDC_T4_DLY_SEL (1 << 0)
130#define CORE_START_CDC_TRAFFIC (1 << 6)
131
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +0300132#define CORE_MCI_DATA_CTRL 0x2C
133#define CORE_MCI_DPSM_ENABLE (1 << 0)
134
135#define CORE_TESTBUS_CONFIG 0x0CC
136#define CORE_TESTBUS_ENA (1 << 3)
137#define CORE_TESTBUS_SEL2 (1 << 4)
138
Venkat Gopalakrishnan0a179c82013-06-26 17:56:11 -0700139#define CORE_MCI_VERSION 0x050
140#define CORE_VERSION_310 0x10000011
141
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +0300142/*
143 * Waiting until end of potential AHB access for data:
144 * 16 AHB cycles (160ns for 100MHz and 320ns for 50MHz) +
145 * delay on AHB (2us) = maximum 2.32us
146 * Taking x10 times margin
147 */
148#define CORE_AHB_DATA_DELAY_US 23
149/* Waiting until end of potential AHB access for descriptor:
150 * Single (1 AHB cycle) + delay on AHB bus = max 2us
151 * INCR4 (4 AHB cycles) + delay on AHB bus = max 2us
152 * Single (1 AHB cycle) + delay on AHB bus = max 2us
153 * Total 8 us delay with margin
154 */
155#define CORE_AHB_DESC_DELAY_US 8
156
157#define CORE_SDCC_DEBUG_REG 0x124
158#define CORE_DEBUG_REG_AHB_HTRANS (3 << 12)
159
Asutosh Das3781bd82013-01-10 21:11:04 +0530160/* 8KB descriptors */
161#define SDHCI_MSM_MAX_SEGMENTS (1 << 13)
Sahitya Tummala04c3a462013-01-11 11:30:45 +0530162#define SDHCI_MSM_MMC_CLK_GATE_DELAY 200 /* msecs */
Asutosh Das3781bd82013-01-10 21:11:04 +0530163
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -0700164#define CORE_FREQ_100MHZ (100 * 1000 * 1000)
165
Venkat Gopalakrishnanb6cfa292013-06-12 11:16:37 -0700166#define INVALID_TUNING_PHASE -1
167
Asutosh Das1369f7b2013-06-03 18:56:52 +0530168#define sdhci_is_valid_mpm_wakeup_int(_h) ((_h)->pdata->mpm_sdiowakeup_int >= 0)
169#define sdhci_is_valid_gpio_wakeup_int(_h) ((_h)->pdata->sdiowakeup_irq >= 0)
170
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700171static const u32 tuning_block_64[] = {
172 0x00FF0FFF, 0xCCC3CCFF, 0xFFCC3CC3, 0xEFFEFFFE,
173 0xDDFFDFFF, 0xFBFFFBFF, 0xFF7FFFBF, 0xEFBDF777,
174 0xF0FFF0FF, 0x3CCCFC0F, 0xCFCC33CC, 0xEEFFEFFF,
175 0xFDFFFDFF, 0xFFBFFFDF, 0xFFF7FFBB, 0xDE7B7FF7
176};
177
178static const u32 tuning_block_128[] = {
179 0xFF00FFFF, 0x0000FFFF, 0xCCCCFFFF, 0xCCCC33CC,
180 0xCC3333CC, 0xFFFFCCCC, 0xFFFFEEFF, 0xFFEEEEFF,
181 0xFFDDFFFF, 0xDDDDFFFF, 0xBBFFFFFF, 0xBBFFFFFF,
182 0xFFFFFFBB, 0xFFFFFF77, 0x77FF7777, 0xFFEEDDBB,
183 0x00FFFFFF, 0x00FFFFFF, 0xCCFFFF00, 0xCC33CCCC,
184 0x3333CCCC, 0xFFCCCCCC, 0xFFEEFFFF, 0xEEEEFFFF,
185 0xDDFFFFFF, 0xDDFFFFFF, 0xFFFFFFDD, 0xFFFFFFBB,
186 0xFFFFBBBB, 0xFFFF77FF, 0xFF7777FF, 0xEEDDBB77
187};
Asutosh Das33a4ff52012-12-18 16:14:02 +0530188
Venkat Gopalakrishnanc61ab7e2013-03-11 12:17:57 -0700189static int disable_slots;
190/* root can write, others read */
191module_param(disable_slots, int, S_IRUGO|S_IWUSR);
192
Asutosh Das33a4ff52012-12-18 16:14:02 +0530193/* This structure keeps information per regulator */
194struct sdhci_msm_reg_data {
195 /* voltage regulator handle */
196 struct regulator *reg;
197 /* regulator name */
198 const char *name;
199 /* voltage level to be set */
200 u32 low_vol_level;
201 u32 high_vol_level;
202 /* Load values for low power and high power mode */
203 u32 lpm_uA;
204 u32 hpm_uA;
205
206 /* is this regulator enabled? */
207 bool is_enabled;
208 /* is this regulator needs to be always on? */
209 bool is_always_on;
210 /* is low power mode setting required for this regulator? */
211 bool lpm_sup;
Asutosh Das95afcad2013-06-28 15:03:44 +0530212 bool set_voltage_sup;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530213};
214
215/*
216 * This structure keeps information for all the
217 * regulators required for a SDCC slot.
218 */
219struct sdhci_msm_slot_reg_data {
220 /* keeps VDD/VCC regulator info */
221 struct sdhci_msm_reg_data *vdd_data;
222 /* keeps VDD IO regulator info */
223 struct sdhci_msm_reg_data *vdd_io_data;
224};
225
226struct sdhci_msm_gpio {
227 u32 no;
228 const char *name;
229 bool is_enabled;
230};
231
232struct sdhci_msm_gpio_data {
233 struct sdhci_msm_gpio *gpio;
234 u8 size;
235};
236
Asutosh Das390519d2012-12-21 12:21:42 +0530237struct sdhci_msm_pad_pull {
238 enum msm_tlmm_pull_tgt no;
239 u32 val;
240};
241
242struct sdhci_msm_pad_pull_data {
243 struct sdhci_msm_pad_pull *on;
244 struct sdhci_msm_pad_pull *off;
245 u8 size;
246};
247
248struct sdhci_msm_pad_drv {
249 enum msm_tlmm_hdrive_tgt no;
250 u32 val;
251};
252
253struct sdhci_msm_pad_drv_data {
254 struct sdhci_msm_pad_drv *on;
255 struct sdhci_msm_pad_drv *off;
256 u8 size;
257};
258
259struct sdhci_msm_pad_data {
260 struct sdhci_msm_pad_pull_data *pull;
261 struct sdhci_msm_pad_drv_data *drv;
262};
263
264
Asutosh Das33a4ff52012-12-18 16:14:02 +0530265struct sdhci_msm_pin_data {
266 /*
267 * = 1 if controller pins are using gpios
268 * = 0 if controller has dedicated MSM pads
269 */
Asutosh Das390519d2012-12-21 12:21:42 +0530270 u8 is_gpio;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530271 bool cfg_sts;
272 struct sdhci_msm_gpio_data *gpio_data;
Asutosh Das390519d2012-12-21 12:21:42 +0530273 struct sdhci_msm_pad_data *pad_data;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530274};
275
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +0530276struct sdhci_msm_bus_voting_data {
277 struct msm_bus_scale_pdata *bus_pdata;
278 unsigned int *bw_vecs;
279 unsigned int bw_vecs_size;
280};
281
Asutosh Das33a4ff52012-12-18 16:14:02 +0530282struct sdhci_msm_pltfm_data {
283 /* Supported UHS-I Modes */
284 u32 caps;
285
286 /* More capabilities */
287 u32 caps2;
288
289 unsigned long mmc_bus_width;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530290 struct sdhci_msm_slot_reg_data *vreg_data;
291 bool nonremovable;
292 struct sdhci_msm_pin_data *pin_data;
Sahitya Tummalab4e84042013-03-10 07:03:17 +0530293 u32 cpu_dma_latency_us;
Sahitya Tummala62448d92013-03-12 14:57:46 +0530294 int status_gpio; /* card detection GPIO that is configured as IRQ */
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +0530295 struct sdhci_msm_bus_voting_data *voting_data;
Sahitya Tummala00240122013-02-28 19:50:51 +0530296 u32 *sup_clk_table;
297 unsigned char sup_clk_cnt;
Asutosh Das1369f7b2013-06-03 18:56:52 +0530298 int mpm_sdiowakeup_int;
299 int sdiowakeup_irq;
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +0530300};
301
302struct sdhci_msm_bus_vote {
303 uint32_t client_handle;
304 uint32_t curr_vote;
305 int min_bw_vote;
306 int max_bw_vote;
307 bool is_max_bw_needed;
308 struct delayed_work vote_work;
309 struct device_attribute max_bus_bw;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530310};
311
312struct sdhci_msm_host {
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +0530313 struct platform_device *pdev;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530314 void __iomem *core_mem; /* MSM SDCC mapped address */
Asutosh Dasbbc84782013-02-11 15:31:35 +0530315 int pwr_irq; /* power irq */
Asutosh Das33a4ff52012-12-18 16:14:02 +0530316 struct clk *clk; /* main SD/MMC bus clock */
317 struct clk *pclk; /* SDHC peripheral bus clock */
318 struct clk *bus_clk; /* SDHC bus voter clock */
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -0700319 struct clk *ff_clk; /* CDC calibration fixed feedback clock */
320 struct clk *sleep_clk; /* CDC calibration sleep clock */
Sahitya Tummala04c3a462013-01-11 11:30:45 +0530321 atomic_t clks_on; /* Set if clocks are enabled */
Asutosh Das33a4ff52012-12-18 16:14:02 +0530322 struct sdhci_msm_pltfm_data *pdata;
323 struct mmc_host *mmc;
324 struct sdhci_pltfm_data sdhci_msm_pdata;
Sahitya Tummala179e7382013-03-20 19:24:01 +0530325 u32 curr_pwr_state;
326 u32 curr_io_level;
327 struct completion pwr_irq_completion;
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +0530328 struct sdhci_msm_bus_vote msm_bus_vote;
Sahitya Tummala3b292c32013-06-20 14:00:18 +0530329 struct device_attribute polling;
Sahitya Tummala00240122013-02-28 19:50:51 +0530330 u32 clk_rate; /* Keeps track of current clock rate that is set */
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -0700331 bool tuning_done;
332 bool calibration_done;
Venkat Gopalakrishnanb6cfa292013-06-12 11:16:37 -0700333 u8 saved_tuning_phase;
Asutosh Das6c0804b2013-11-08 12:33:47 +0530334 atomic_t controller_clock;
Asutosh Das1369f7b2013-06-03 18:56:52 +0530335 bool is_sdiowakeup_enabled;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530336};
337
338enum vdd_io_level {
339 /* set vdd_io_data->low_vol_level */
340 VDD_IO_LOW,
341 /* set vdd_io_data->high_vol_level */
342 VDD_IO_HIGH,
343 /*
344 * set whatever there in voltage_level (third argument) of
345 * sdhci_msm_set_vdd_io_vol() function.
346 */
347 VDD_IO_SET_LEVEL,
348};
349
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700350/* MSM platform specific tuning */
351static inline int msm_dll_poll_ck_out_en(struct sdhci_host *host,
352 u8 poll)
353{
354 int rc = 0;
355 u32 wait_cnt = 50;
356 u8 ck_out_en = 0;
357 struct mmc_host *mmc = host->mmc;
358
359 /* poll for CK_OUT_EN bit. max. poll time = 50us */
360 ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) &
361 CORE_CK_OUT_EN);
362
363 while (ck_out_en != poll) {
364 if (--wait_cnt == 0) {
365 pr_err("%s: %s: CK_OUT_EN bit is not %d\n",
366 mmc_hostname(mmc), __func__, poll);
367 rc = -ETIMEDOUT;
368 goto out;
369 }
370 udelay(1);
371
372 ck_out_en = !!(readl_relaxed(host->ioaddr +
373 CORE_DLL_CONFIG) & CORE_CK_OUT_EN);
374 }
375out:
376 return rc;
377}
378
379static int msm_config_cm_dll_phase(struct sdhci_host *host, u8 phase)
380{
381 int rc = 0;
382 u8 grey_coded_phase_table[] = {0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4,
383 0xC, 0xD, 0xF, 0xE, 0xA, 0xB, 0x9,
384 0x8};
385 unsigned long flags;
386 u32 config;
387 struct mmc_host *mmc = host->mmc;
388
389 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
390 spin_lock_irqsave(&host->lock, flags);
391
392 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
393 config &= ~(CORE_CDR_EN | CORE_CK_OUT_EN);
394 config |= (CORE_CDR_EXT_EN | CORE_DLL_EN);
395 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
396
397 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '0' */
398 rc = msm_dll_poll_ck_out_en(host, 0);
399 if (rc)
400 goto err_out;
401
402 /*
403 * Write the selected DLL clock output phase (0 ... 15)
404 * to CDR_SELEXT bit field of DLL_CONFIG register.
405 */
406 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
407 & ~(0xF << 20))
408 | (grey_coded_phase_table[phase] << 20)),
409 host->ioaddr + CORE_DLL_CONFIG);
410
411 /* Set CK_OUT_EN bit of DLL_CONFIG register to 1. */
412 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
413 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
414
415 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '1' */
416 rc = msm_dll_poll_ck_out_en(host, 1);
417 if (rc)
418 goto err_out;
419
420 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
421 config |= CORE_CDR_EN;
422 config &= ~CORE_CDR_EXT_EN;
423 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
424 goto out;
425
426err_out:
427 pr_err("%s: %s: Failed to set DLL phase: %d\n",
428 mmc_hostname(mmc), __func__, phase);
429out:
430 spin_unlock_irqrestore(&host->lock, flags);
431 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
432 return rc;
433}
434
435/*
436 * Find out the greatest range of consecuitive selected
437 * DLL clock output phases that can be used as sampling
438 * setting for SD3.0 UHS-I card read operation (in SDR104
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -0700439 * timing mode) or for eMMC4.5 card read operation (in
440 * HS400/HS200 timing mode).
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700441 * Select the 3/4 of the range and configure the DLL with the
442 * selected DLL clock output phase.
443 */
444
445static int msm_find_most_appropriate_phase(struct sdhci_host *host,
446 u8 *phase_table, u8 total_phases)
447{
448 int ret;
449 u8 ranges[MAX_PHASES][MAX_PHASES] = { {0}, {0} };
450 u8 phases_per_row[MAX_PHASES] = {0};
451 int row_index = 0, col_index = 0, selected_row_index = 0, curr_max = 0;
452 int i, cnt, phase_0_raw_index = 0, phase_15_raw_index = 0;
453 bool phase_0_found = false, phase_15_found = false;
454 struct mmc_host *mmc = host->mmc;
455
456 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
457 if (!total_phases || (total_phases > MAX_PHASES)) {
458 pr_err("%s: %s: invalid argument: total_phases=%d\n",
459 mmc_hostname(mmc), __func__, total_phases);
460 return -EINVAL;
461 }
462
463 for (cnt = 0; cnt < total_phases; cnt++) {
464 ranges[row_index][col_index] = phase_table[cnt];
465 phases_per_row[row_index] += 1;
466 col_index++;
467
468 if ((cnt + 1) == total_phases) {
469 continue;
470 /* check if next phase in phase_table is consecutive or not */
471 } else if ((phase_table[cnt] + 1) != phase_table[cnt + 1]) {
472 row_index++;
473 col_index = 0;
474 }
475 }
476
477 if (row_index >= MAX_PHASES)
478 return -EINVAL;
479
480 /* Check if phase-0 is present in first valid window? */
481 if (!ranges[0][0]) {
482 phase_0_found = true;
483 phase_0_raw_index = 0;
484 /* Check if cycle exist between 2 valid windows */
485 for (cnt = 1; cnt <= row_index; cnt++) {
486 if (phases_per_row[cnt]) {
487 for (i = 0; i < phases_per_row[cnt]; i++) {
488 if (ranges[cnt][i] == 15) {
489 phase_15_found = true;
490 phase_15_raw_index = cnt;
491 break;
492 }
493 }
494 }
495 }
496 }
497
498 /* If 2 valid windows form cycle then merge them as single window */
499 if (phase_0_found && phase_15_found) {
500 /* number of phases in raw where phase 0 is present */
501 u8 phases_0 = phases_per_row[phase_0_raw_index];
502 /* number of phases in raw where phase 15 is present */
503 u8 phases_15 = phases_per_row[phase_15_raw_index];
504
505 if (phases_0 + phases_15 >= MAX_PHASES)
506 /*
507 * If there are more than 1 phase windows then total
508 * number of phases in both the windows should not be
509 * more than or equal to MAX_PHASES.
510 */
511 return -EINVAL;
512
513 /* Merge 2 cyclic windows */
514 i = phases_15;
515 for (cnt = 0; cnt < phases_0; cnt++) {
516 ranges[phase_15_raw_index][i] =
517 ranges[phase_0_raw_index][cnt];
518 if (++i >= MAX_PHASES)
519 break;
520 }
521
522 phases_per_row[phase_0_raw_index] = 0;
523 phases_per_row[phase_15_raw_index] = phases_15 + phases_0;
524 }
525
526 for (cnt = 0; cnt <= row_index; cnt++) {
527 if (phases_per_row[cnt] > curr_max) {
528 curr_max = phases_per_row[cnt];
529 selected_row_index = cnt;
530 }
531 }
532
533 i = ((curr_max * 3) / 4);
534 if (i)
535 i--;
536
537 ret = (int)ranges[selected_row_index][i];
538
539 if (ret >= MAX_PHASES) {
540 ret = -EINVAL;
541 pr_err("%s: %s: invalid phase selected=%d\n",
542 mmc_hostname(mmc), __func__, ret);
543 }
544
545 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
546 return ret;
547}
548
549static inline void msm_cm_dll_set_freq(struct sdhci_host *host)
550{
551 u32 mclk_freq = 0;
552
553 /* Program the MCLK value to MCLK_FREQ bit field */
554 if (host->clock <= 112000000)
555 mclk_freq = 0;
556 else if (host->clock <= 125000000)
557 mclk_freq = 1;
558 else if (host->clock <= 137000000)
559 mclk_freq = 2;
560 else if (host->clock <= 150000000)
561 mclk_freq = 3;
562 else if (host->clock <= 162000000)
563 mclk_freq = 4;
564 else if (host->clock <= 175000000)
565 mclk_freq = 5;
566 else if (host->clock <= 187000000)
567 mclk_freq = 6;
568 else if (host->clock <= 200000000)
569 mclk_freq = 7;
570
571 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
572 & ~(7 << 24)) | (mclk_freq << 24)),
573 host->ioaddr + CORE_DLL_CONFIG);
574}
575
576/* Initialize the DLL (Programmable Delay Line ) */
577static int msm_init_cm_dll(struct sdhci_host *host)
578{
579 struct mmc_host *mmc = host->mmc;
580 int rc = 0;
581 unsigned long flags;
582 u32 wait_cnt;
Subhash Jadavaniefb5f622013-05-28 18:21:57 +0530583 bool prev_pwrsave, curr_pwrsave;
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700584
585 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
586 spin_lock_irqsave(&host->lock, flags);
Subhash Jadavaniefb5f622013-05-28 18:21:57 +0530587 prev_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
588 CORE_CLK_PWRSAVE);
589 curr_pwrsave = prev_pwrsave;
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700590 /*
591 * Make sure that clock is always enabled when DLL
592 * tuning is in progress. Keeping PWRSAVE ON may
593 * turn off the clock. So let's disable the PWRSAVE
594 * here and re-enable it once tuning is completed.
595 */
Subhash Jadavaniefb5f622013-05-28 18:21:57 +0530596 if (prev_pwrsave) {
597 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
598 & ~CORE_CLK_PWRSAVE),
599 host->ioaddr + CORE_VENDOR_SPEC);
600 curr_pwrsave = false;
601 }
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700602
603 /* Write 1 to DLL_RST bit of DLL_CONFIG register */
604 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
605 | CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
606
607 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
608 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
609 | CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
610 msm_cm_dll_set_freq(host);
611
612 /* Write 0 to DLL_RST bit of DLL_CONFIG register */
613 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
614 & ~CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
615
616 /* Write 0 to DLL_PDN bit of DLL_CONFIG register */
617 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
618 & ~CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
619
620 /* Set DLL_EN bit to 1. */
621 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
622 | CORE_DLL_EN), host->ioaddr + CORE_DLL_CONFIG);
623
624 /* Set CK_OUT_EN bit to 1. */
625 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
626 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
627
628 wait_cnt = 50;
629 /* Wait until DLL_LOCK bit of DLL_STATUS register becomes '1' */
630 while (!(readl_relaxed(host->ioaddr + CORE_DLL_STATUS) &
631 CORE_DLL_LOCK)) {
632 /* max. wait for 50us sec for LOCK bit to be set */
633 if (--wait_cnt == 0) {
634 pr_err("%s: %s: DLL failed to LOCK\n",
635 mmc_hostname(mmc), __func__);
636 rc = -ETIMEDOUT;
637 goto out;
638 }
639 /* wait for 1us before polling again */
640 udelay(1);
641 }
642
643out:
Subhash Jadavaniefb5f622013-05-28 18:21:57 +0530644 /* Restore the correct PWRSAVE state */
645 if (prev_pwrsave ^ curr_pwrsave) {
646 u32 reg = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC);
647
648 if (prev_pwrsave)
649 reg |= CORE_CLK_PWRSAVE;
650 else
651 reg &= ~CORE_CLK_PWRSAVE;
652
653 writel_relaxed(reg, host->ioaddr + CORE_VENDOR_SPEC);
654 }
655
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700656 spin_unlock_irqrestore(&host->lock, flags);
657 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
658 return rc;
659}
660
Venkat Gopalakrishnanb6cfa292013-06-12 11:16:37 -0700661static int sdhci_msm_cdclp533_calibration(struct sdhci_host *host)
662{
663 u32 wait_cnt;
664 int ret = 0;
665 int cdc_err = 0;
666 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
667 struct sdhci_msm_host *msm_host = pltfm_host->priv;
668
669 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
670
671 /*
672 * Retuning in HS400 (DDR mode) will fail, just reset the
673 * tuning block and restore the saved tuning phase.
674 */
675 ret = msm_init_cm_dll(host);
676 if (ret)
677 goto out;
678
679 /* Set the selected phase in delay line hw block */
680 ret = msm_config_cm_dll_phase(host, msm_host->saved_tuning_phase);
681 if (ret)
682 goto out;
683
684 /* Write 1 to CMD_DAT_TRACK_SEL field in DLL_CONFIG */
685 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
686 | CORE_CMD_DAT_TRACK_SEL),
687 host->ioaddr + CORE_DLL_CONFIG);
688
689 /* Write 0 to CDC_T4_DLY_SEL field in VENDOR_SPEC_DDR200_CFG */
690 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
691 & ~CORE_CDC_T4_DLY_SEL),
692 host->ioaddr + CORE_DDR_200_CFG);
693
694 /* Write 0 to CDC_SWITCH_BYPASS_OFF field in CORE_CSR_CDC_GEN_CFG */
695 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG)
696 & ~CORE_CDC_SWITCH_BYPASS_OFF),
697 host->ioaddr + CORE_CSR_CDC_GEN_CFG);
698
699 /* Write 1 to CDC_SWITCH_RC_EN field in CORE_CSR_CDC_GEN_CFG */
700 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG)
701 | CORE_CDC_SWITCH_RC_EN),
702 host->ioaddr + CORE_CSR_CDC_GEN_CFG);
703
704 /* Write 0 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
705 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
706 & ~CORE_START_CDC_TRAFFIC),
707 host->ioaddr + CORE_DDR_200_CFG);
708
709 /*
710 * Perform CDC Register Initialization Sequence
711 *
712 * CORE_CSR_CDC_CTLR_CFG0 0x11800EC
713 * CORE_CSR_CDC_CTLR_CFG1 0x3011111
714 * CORE_CSR_CDC_CAL_TIMER_CFG0 0x1201000
715 * CORE_CSR_CDC_CAL_TIMER_CFG1 0x4
716 * CORE_CSR_CDC_REFCOUNT_CFG 0xCB732020
717 * CORE_CSR_CDC_COARSE_CAL_CFG 0xB19
718 * CORE_CSR_CDC_DELAY_CFG 0x3AC
719 * CORE_CDC_OFFSET_CFG 0x0
720 * CORE_CDC_SLAVE_DDA_CFG 0x16334
721 */
722
723 writel_relaxed(0x11800EC, host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
724 writel_relaxed(0x3011111, host->ioaddr + CORE_CSR_CDC_CTLR_CFG1);
725 writel_relaxed(0x1201000, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
726 writel_relaxed(0x4, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG1);
727 writel_relaxed(0xCB732020, host->ioaddr + CORE_CSR_CDC_REFCOUNT_CFG);
728 writel_relaxed(0xB19, host->ioaddr + CORE_CSR_CDC_COARSE_CAL_CFG);
729 writel_relaxed(0x3AC, host->ioaddr + CORE_CSR_CDC_DELAY_CFG);
730 writel_relaxed(0x0, host->ioaddr + CORE_CDC_OFFSET_CFG);
731 writel_relaxed(0x16334, host->ioaddr + CORE_CDC_SLAVE_DDA_CFG);
732
733 /* CDC HW Calibration */
734
735 /* Write 1 to SW_TRIG_FULL_CALIB field in CORE_CSR_CDC_CTLR_CFG0 */
736 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
737 | CORE_SW_TRIG_FULL_CALIB),
738 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
739
740 /* Write 0 to SW_TRIG_FULL_CALIB field in CORE_CSR_CDC_CTLR_CFG0 */
741 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
742 & ~CORE_SW_TRIG_FULL_CALIB),
743 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
744
745 /* Write 1 to HW_AUTOCAL_ENA field in CORE_CSR_CDC_CTLR_CFG0 */
746 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
747 | CORE_HW_AUTOCAL_ENA),
748 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
749
750 /* Write 1 to TIMER_ENA field in CORE_CSR_CDC_CAL_TIMER_CFG0 */
751 writel_relaxed((readl_relaxed(host->ioaddr +
752 CORE_CSR_CDC_CAL_TIMER_CFG0) | CORE_TIMER_ENA),
753 host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
754
755 mb();
756
757 /* Poll on CALIBRATION_DONE field in CORE_CSR_CDC_STATUS0 to be 1 */
758 wait_cnt = 50;
759 while (!(readl_relaxed(host->ioaddr + CORE_CSR_CDC_STATUS0)
760 & CORE_CALIBRATION_DONE)) {
761 /* max. wait for 50us sec for CALIBRATION_DONE bit to be set */
762 if (--wait_cnt == 0) {
763 pr_err("%s: %s: CDC Calibration was not completed\n",
764 mmc_hostname(host->mmc), __func__);
765 ret = -ETIMEDOUT;
766 goto out;
767 }
768 /* wait for 1us before polling again */
769 udelay(1);
770 }
771
772 /* Verify CDC_ERROR_CODE field in CORE_CSR_CDC_STATUS0 is 0 */
773 cdc_err = readl_relaxed(host->ioaddr + CORE_CSR_CDC_STATUS0)
774 & CORE_CDC_ERROR_CODE_MASK;
775 if (cdc_err) {
776 pr_err("%s: %s: CDC Error Code %d\n",
777 mmc_hostname(host->mmc), __func__, cdc_err);
778 ret = -EINVAL;
779 goto out;
780 }
781
782 /* Write 1 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
783 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
784 | CORE_START_CDC_TRAFFIC),
785 host->ioaddr + CORE_DDR_200_CFG);
786out:
787 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
788 __func__, ret);
789 return ret;
790}
791
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700792int sdhci_msm_execute_tuning(struct sdhci_host *host, u32 opcode)
793{
794 unsigned long flags;
Sahitya Tummala714e9642013-06-13 10:36:57 +0530795 int tuning_seq_cnt = 3;
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700796 u8 phase, *data_buf, tuned_phases[16], tuned_phase_cnt = 0;
797 const u32 *tuning_block_pattern = tuning_block_64;
798 int size = sizeof(tuning_block_64); /* Tuning pattern size in bytes */
799 int rc;
800 struct mmc_host *mmc = host->mmc;
Sahitya Tummala00240122013-02-28 19:50:51 +0530801 struct mmc_ios ios = host->mmc->ios;
Venkat Gopalakrishnanb6cfa292013-06-12 11:16:37 -0700802 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
803 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala00240122013-02-28 19:50:51 +0530804
805 /*
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -0700806 * Tuning is required for SDR104, HS200 and HS400 cards and
807 * if clock frequency is greater than 100MHz in these modes.
Sahitya Tummala00240122013-02-28 19:50:51 +0530808 */
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -0700809 if (host->clock <= CORE_FREQ_100MHZ ||
810 !((ios.timing == MMC_TIMING_MMC_HS400) ||
811 (ios.timing == MMC_TIMING_MMC_HS200) ||
812 (ios.timing == MMC_TIMING_UHS_SDR104)))
Sahitya Tummala00240122013-02-28 19:50:51 +0530813 return 0;
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700814
815 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
Venkat Gopalakrishnanb6cfa292013-06-12 11:16:37 -0700816
817 /* CDCLP533 HW calibration is only required for HS400 mode*/
818 if (msm_host->tuning_done && !msm_host->calibration_done &&
819 (mmc->ios.timing == MMC_TIMING_MMC_HS400)) {
820 rc = sdhci_msm_cdclp533_calibration(host);
821 spin_lock_irqsave(&host->lock, flags);
822 if (!rc)
823 msm_host->calibration_done = true;
824 spin_unlock_irqrestore(&host->lock, flags);
825 goto out;
826 }
827
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700828 spin_lock_irqsave(&host->lock, flags);
829
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -0700830 if (((opcode == MMC_SEND_TUNING_BLOCK_HS400) ||
831 (opcode == MMC_SEND_TUNING_BLOCK_HS200)) &&
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700832 (mmc->ios.bus_width == MMC_BUS_WIDTH_8)) {
833 tuning_block_pattern = tuning_block_128;
834 size = sizeof(tuning_block_128);
835 }
836 spin_unlock_irqrestore(&host->lock, flags);
837
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700838 data_buf = kmalloc(size, GFP_KERNEL);
839 if (!data_buf) {
840 rc = -ENOMEM;
841 goto out;
842 }
843
Sahitya Tummala714e9642013-06-13 10:36:57 +0530844retry:
845 /* first of all reset the tuning block */
846 rc = msm_init_cm_dll(host);
847 if (rc)
848 goto kfree;
849
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700850 phase = 0;
851 do {
852 struct mmc_command cmd = {0};
853 struct mmc_data data = {0};
854 struct mmc_request mrq = {
855 .cmd = &cmd,
856 .data = &data
857 };
858 struct scatterlist sg;
859
860 /* set the phase in delay line hw block */
861 rc = msm_config_cm_dll_phase(host, phase);
862 if (rc)
863 goto kfree;
864
865 cmd.opcode = opcode;
866 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
867
868 data.blksz = size;
869 data.blocks = 1;
870 data.flags = MMC_DATA_READ;
871 data.timeout_ns = 1000 * 1000 * 1000; /* 1 sec */
872
873 data.sg = &sg;
874 data.sg_len = 1;
875 sg_init_one(&sg, data_buf, size);
876 memset(data_buf, 0, size);
877 mmc_wait_for_req(mmc, &mrq);
878
879 if (!cmd.error && !data.error &&
880 !memcmp(data_buf, tuning_block_pattern, size)) {
881 /* tuning is successful at this tuning point */
882 tuned_phases[tuned_phase_cnt++] = phase;
883 pr_debug("%s: %s: found good phase = %d\n",
884 mmc_hostname(mmc), __func__, phase);
885 }
886 } while (++phase < 16);
887
888 if (tuned_phase_cnt) {
889 rc = msm_find_most_appropriate_phase(host, tuned_phases,
890 tuned_phase_cnt);
891 if (rc < 0)
892 goto kfree;
893 else
894 phase = (u8)rc;
895
896 /*
897 * Finally set the selected phase in delay
898 * line hw block.
899 */
900 rc = msm_config_cm_dll_phase(host, phase);
901 if (rc)
902 goto kfree;
Venkat Gopalakrishnanb6cfa292013-06-12 11:16:37 -0700903 msm_host->saved_tuning_phase = phase;
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700904 pr_debug("%s: %s: finally setting the tuning phase to %d\n",
905 mmc_hostname(mmc), __func__, phase);
906 } else {
Sahitya Tummala714e9642013-06-13 10:36:57 +0530907 if (--tuning_seq_cnt)
908 goto retry;
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700909 /* tuning failed */
910 pr_err("%s: %s: no tuning point found\n",
911 mmc_hostname(mmc), __func__);
Sahitya Tummala714e9642013-06-13 10:36:57 +0530912 rc = -EIO;
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700913 }
914
915kfree:
916 kfree(data_buf);
917out:
Venkat Gopalakrishnanb6cfa292013-06-12 11:16:37 -0700918 spin_lock_irqsave(&host->lock, flags);
919 if (!rc)
920 msm_host->tuning_done = true;
921 spin_unlock_irqrestore(&host->lock, flags);
922 pr_debug("%s: Exit %s, err(%d)\n", mmc_hostname(mmc), __func__, rc);
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700923 return rc;
924}
925
Asutosh Das33a4ff52012-12-18 16:14:02 +0530926static int sdhci_msm_setup_gpio(struct sdhci_msm_pltfm_data *pdata, bool enable)
927{
928 struct sdhci_msm_gpio_data *curr;
929 int i, ret = 0;
930
931 curr = pdata->pin_data->gpio_data;
932 for (i = 0; i < curr->size; i++) {
933 if (!gpio_is_valid(curr->gpio[i].no)) {
934 ret = -EINVAL;
935 pr_err("%s: Invalid gpio = %d\n", __func__,
936 curr->gpio[i].no);
937 goto free_gpios;
938 }
939 if (enable) {
940 ret = gpio_request(curr->gpio[i].no,
941 curr->gpio[i].name);
942 if (ret) {
943 pr_err("%s: gpio_request(%d, %s) failed %d\n",
944 __func__, curr->gpio[i].no,
945 curr->gpio[i].name, ret);
946 goto free_gpios;
947 }
948 curr->gpio[i].is_enabled = true;
949 } else {
950 gpio_free(curr->gpio[i].no);
951 curr->gpio[i].is_enabled = false;
952 }
953 }
954 return ret;
955
956free_gpios:
957 for (i--; i >= 0; i--) {
958 gpio_free(curr->gpio[i].no);
959 curr->gpio[i].is_enabled = false;
960 }
961 return ret;
962}
963
Asutosh Das390519d2012-12-21 12:21:42 +0530964static int sdhci_msm_setup_pad(struct sdhci_msm_pltfm_data *pdata, bool enable)
965{
966 struct sdhci_msm_pad_data *curr;
967 int i;
968
969 curr = pdata->pin_data->pad_data;
970 for (i = 0; i < curr->drv->size; i++) {
971 if (enable)
972 msm_tlmm_set_hdrive(curr->drv->on[i].no,
973 curr->drv->on[i].val);
974 else
975 msm_tlmm_set_hdrive(curr->drv->off[i].no,
976 curr->drv->off[i].val);
977 }
978
979 for (i = 0; i < curr->pull->size; i++) {
980 if (enable)
981 msm_tlmm_set_pull(curr->pull->on[i].no,
982 curr->pull->on[i].val);
983 else
984 msm_tlmm_set_pull(curr->pull->off[i].no,
985 curr->pull->off[i].val);
986 }
987
988 return 0;
989}
990
Asutosh Das33a4ff52012-12-18 16:14:02 +0530991static int sdhci_msm_setup_pins(struct sdhci_msm_pltfm_data *pdata, bool enable)
992{
993 int ret = 0;
994
995 if (!pdata->pin_data || (pdata->pin_data->cfg_sts == enable))
996 return 0;
Asutosh Das390519d2012-12-21 12:21:42 +0530997 if (pdata->pin_data->is_gpio)
998 ret = sdhci_msm_setup_gpio(pdata, enable);
999 else
1000 ret = sdhci_msm_setup_pad(pdata, enable);
Asutosh Das33a4ff52012-12-18 16:14:02 +05301001
Asutosh Das33a4ff52012-12-18 16:14:02 +05301002 if (!ret)
1003 pdata->pin_data->cfg_sts = enable;
1004
1005 return ret;
1006}
1007
Asutosh Das390519d2012-12-21 12:21:42 +05301008static int sdhci_msm_dt_get_array(struct device *dev, const char *prop_name,
1009 u32 **out, int *len, u32 size)
1010{
1011 int ret = 0;
1012 struct device_node *np = dev->of_node;
1013 size_t sz;
1014 u32 *arr = NULL;
1015
1016 if (!of_get_property(np, prop_name, len)) {
1017 ret = -EINVAL;
1018 goto out;
1019 }
1020 sz = *len = *len / sizeof(*arr);
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -07001021 if (sz <= 0 || (size > 0 && (sz > size))) {
Asutosh Das390519d2012-12-21 12:21:42 +05301022 dev_err(dev, "%s invalid size\n", prop_name);
1023 ret = -EINVAL;
1024 goto out;
1025 }
1026
1027 arr = devm_kzalloc(dev, sz * sizeof(*arr), GFP_KERNEL);
1028 if (!arr) {
1029 dev_err(dev, "%s failed allocating memory\n", prop_name);
1030 ret = -ENOMEM;
1031 goto out;
1032 }
1033
1034 ret = of_property_read_u32_array(np, prop_name, arr, sz);
1035 if (ret < 0) {
1036 dev_err(dev, "%s failed reading array %d\n", prop_name, ret);
1037 goto out;
1038 }
1039 *out = arr;
1040out:
1041 if (ret)
1042 *len = 0;
1043 return ret;
1044}
1045
Asutosh Das33a4ff52012-12-18 16:14:02 +05301046#define MAX_PROP_SIZE 32
1047static int sdhci_msm_dt_parse_vreg_info(struct device *dev,
1048 struct sdhci_msm_reg_data **vreg_data, const char *vreg_name)
1049{
1050 int len, ret = 0;
1051 const __be32 *prop;
1052 char prop_name[MAX_PROP_SIZE];
1053 struct sdhci_msm_reg_data *vreg;
1054 struct device_node *np = dev->of_node;
1055
1056 snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", vreg_name);
1057 if (!of_parse_phandle(np, prop_name, 0)) {
Asutosh Das95afcad2013-06-28 15:03:44 +05301058 dev_info(dev, "No vreg data found for %s\n", vreg_name);
Asutosh Das33a4ff52012-12-18 16:14:02 +05301059 return ret;
1060 }
1061
1062 vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
1063 if (!vreg) {
1064 dev_err(dev, "No memory for vreg: %s\n", vreg_name);
1065 ret = -ENOMEM;
1066 return ret;
1067 }
1068
1069 vreg->name = vreg_name;
1070
1071 snprintf(prop_name, MAX_PROP_SIZE,
1072 "qcom,%s-always-on", vreg_name);
1073 if (of_get_property(np, prop_name, NULL))
1074 vreg->is_always_on = true;
1075
1076 snprintf(prop_name, MAX_PROP_SIZE,
1077 "qcom,%s-lpm-sup", vreg_name);
1078 if (of_get_property(np, prop_name, NULL))
1079 vreg->lpm_sup = true;
1080
1081 snprintf(prop_name, MAX_PROP_SIZE,
1082 "qcom,%s-voltage-level", vreg_name);
1083 prop = of_get_property(np, prop_name, &len);
1084 if (!prop || (len != (2 * sizeof(__be32)))) {
1085 dev_warn(dev, "%s %s property\n",
1086 prop ? "invalid format" : "no", prop_name);
1087 } else {
1088 vreg->low_vol_level = be32_to_cpup(&prop[0]);
1089 vreg->high_vol_level = be32_to_cpup(&prop[1]);
1090 }
1091
1092 snprintf(prop_name, MAX_PROP_SIZE,
1093 "qcom,%s-current-level", vreg_name);
1094 prop = of_get_property(np, prop_name, &len);
1095 if (!prop || (len != (2 * sizeof(__be32)))) {
1096 dev_warn(dev, "%s %s property\n",
1097 prop ? "invalid format" : "no", prop_name);
1098 } else {
1099 vreg->lpm_uA = be32_to_cpup(&prop[0]);
1100 vreg->hpm_uA = be32_to_cpup(&prop[1]);
1101 }
1102
1103 *vreg_data = vreg;
1104 dev_dbg(dev, "%s: %s %s vol=[%d %d]uV, curr=[%d %d]uA\n",
1105 vreg->name, vreg->is_always_on ? "always_on," : "",
1106 vreg->lpm_sup ? "lpm_sup," : "", vreg->low_vol_level,
1107 vreg->high_vol_level, vreg->lpm_uA, vreg->hpm_uA);
1108
1109 return ret;
1110}
1111
Asutosh Das390519d2012-12-21 12:21:42 +05301112/* GPIO/Pad data extraction */
1113static int sdhci_msm_dt_get_pad_pull_info(struct device *dev, int id,
1114 struct sdhci_msm_pad_pull_data **pad_pull_data)
1115{
1116 int ret = 0, base = 0, len, i;
1117 u32 *tmp;
1118 struct sdhci_msm_pad_pull_data *pull_data;
1119 struct sdhci_msm_pad_pull *pull;
1120
1121 switch (id) {
1122 case 1:
1123 base = TLMM_PULL_SDC1_CLK;
1124 break;
1125 case 2:
1126 base = TLMM_PULL_SDC2_CLK;
1127 break;
1128 case 3:
1129 base = TLMM_PULL_SDC3_CLK;
1130 break;
1131 case 4:
1132 base = TLMM_PULL_SDC4_CLK;
1133 break;
1134 default:
1135 dev_err(dev, "%s: Invalid slot id\n", __func__);
1136 ret = -EINVAL;
1137 goto out;
1138 }
1139
1140 pull_data = devm_kzalloc(dev, sizeof(struct sdhci_msm_pad_pull_data),
1141 GFP_KERNEL);
1142 if (!pull_data) {
1143 dev_err(dev, "No memory for msm_mmc_pad_pull_data\n");
1144 ret = -ENOMEM;
1145 goto out;
1146 }
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -07001147 pull_data->size = 4; /* array size for clk, cmd, data and rclk */
Asutosh Das390519d2012-12-21 12:21:42 +05301148
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -07001149 /* Allocate on, off configs for clk, cmd, data and rclk */
Asutosh Das390519d2012-12-21 12:21:42 +05301150 pull = devm_kzalloc(dev, 2 * pull_data->size *\
1151 sizeof(struct sdhci_msm_pad_pull), GFP_KERNEL);
1152 if (!pull) {
1153 dev_err(dev, "No memory for msm_mmc_pad_pull\n");
1154 ret = -ENOMEM;
1155 goto out;
1156 }
1157 pull_data->on = pull;
1158 pull_data->off = pull + pull_data->size;
1159
1160 ret = sdhci_msm_dt_get_array(dev, "qcom,pad-pull-on",
1161 &tmp, &len, pull_data->size);
1162 if (ret)
1163 goto out;
1164
1165 for (i = 0; i < len; i++) {
1166 pull_data->on[i].no = base + i;
1167 pull_data->on[i].val = tmp[i];
1168 dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
1169 i, pull_data->on[i].val);
1170 }
1171
1172 ret = sdhci_msm_dt_get_array(dev, "qcom,pad-pull-off",
1173 &tmp, &len, pull_data->size);
1174 if (ret)
1175 goto out;
1176
1177 for (i = 0; i < len; i++) {
1178 pull_data->off[i].no = base + i;
1179 pull_data->off[i].val = tmp[i];
1180 dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
1181 i, pull_data->off[i].val);
1182 }
1183
1184 *pad_pull_data = pull_data;
1185out:
1186 return ret;
1187}
1188
1189static int sdhci_msm_dt_get_pad_drv_info(struct device *dev, int id,
1190 struct sdhci_msm_pad_drv_data **pad_drv_data)
1191{
1192 int ret = 0, base = 0, len, i;
1193 u32 *tmp;
1194 struct sdhci_msm_pad_drv_data *drv_data;
1195 struct sdhci_msm_pad_drv *drv;
1196
1197 switch (id) {
1198 case 1:
1199 base = TLMM_HDRV_SDC1_CLK;
1200 break;
1201 case 2:
1202 base = TLMM_HDRV_SDC2_CLK;
1203 break;
1204 case 3:
1205 base = TLMM_HDRV_SDC3_CLK;
1206 break;
1207 case 4:
1208 base = TLMM_HDRV_SDC4_CLK;
1209 break;
1210 default:
1211 dev_err(dev, "%s: Invalid slot id\n", __func__);
1212 ret = -EINVAL;
1213 goto out;
1214 }
1215
1216 drv_data = devm_kzalloc(dev, sizeof(struct sdhci_msm_pad_drv_data),
1217 GFP_KERNEL);
1218 if (!drv_data) {
1219 dev_err(dev, "No memory for msm_mmc_pad_drv_data\n");
1220 ret = -ENOMEM;
1221 goto out;
1222 }
1223 drv_data->size = 3; /* array size for clk, cmd, data */
1224
1225 /* Allocate on, off configs for clk, cmd, data */
1226 drv = devm_kzalloc(dev, 2 * drv_data->size *\
1227 sizeof(struct sdhci_msm_pad_drv), GFP_KERNEL);
1228 if (!drv) {
1229 dev_err(dev, "No memory msm_mmc_pad_drv\n");
1230 ret = -ENOMEM;
1231 goto out;
1232 }
1233 drv_data->on = drv;
1234 drv_data->off = drv + drv_data->size;
1235
1236 ret = sdhci_msm_dt_get_array(dev, "qcom,pad-drv-on",
1237 &tmp, &len, drv_data->size);
1238 if (ret)
1239 goto out;
1240
1241 for (i = 0; i < len; i++) {
1242 drv_data->on[i].no = base + i;
1243 drv_data->on[i].val = tmp[i];
1244 dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
1245 i, drv_data->on[i].val);
1246 }
1247
1248 ret = sdhci_msm_dt_get_array(dev, "qcom,pad-drv-off",
1249 &tmp, &len, drv_data->size);
1250 if (ret)
1251 goto out;
1252
1253 for (i = 0; i < len; i++) {
1254 drv_data->off[i].no = base + i;
1255 drv_data->off[i].val = tmp[i];
1256 dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
1257 i, drv_data->off[i].val);
1258 }
1259
1260 *pad_drv_data = drv_data;
1261out:
1262 return ret;
1263}
1264
Asutosh Das33a4ff52012-12-18 16:14:02 +05301265#define GPIO_NAME_MAX_LEN 32
1266static int sdhci_msm_dt_parse_gpio_info(struct device *dev,
1267 struct sdhci_msm_pltfm_data *pdata)
1268{
Asutosh Das390519d2012-12-21 12:21:42 +05301269 int ret = 0, id = 0, cnt, i;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301270 struct sdhci_msm_pin_data *pin_data;
1271 struct device_node *np = dev->of_node;
1272
1273 pin_data = devm_kzalloc(dev, sizeof(*pin_data), GFP_KERNEL);
1274 if (!pin_data) {
1275 dev_err(dev, "No memory for pin_data\n");
1276 ret = -ENOMEM;
1277 goto out;
1278 }
1279
1280 cnt = of_gpio_count(np);
1281 if (cnt > 0) {
Asutosh Das390519d2012-12-21 12:21:42 +05301282 pin_data->is_gpio = true;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301283 pin_data->gpio_data = devm_kzalloc(dev,
1284 sizeof(struct sdhci_msm_gpio_data), GFP_KERNEL);
1285 if (!pin_data->gpio_data) {
1286 dev_err(dev, "No memory for gpio_data\n");
1287 ret = -ENOMEM;
1288 goto out;
1289 }
1290 pin_data->gpio_data->size = cnt;
1291 pin_data->gpio_data->gpio = devm_kzalloc(dev, cnt *
1292 sizeof(struct sdhci_msm_gpio), GFP_KERNEL);
1293
1294 if (!pin_data->gpio_data->gpio) {
1295 dev_err(dev, "No memory for gpio\n");
1296 ret = -ENOMEM;
1297 goto out;
1298 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301299 for (i = 0; i < cnt; i++) {
1300 const char *name = NULL;
1301 char result[GPIO_NAME_MAX_LEN];
1302 pin_data->gpio_data->gpio[i].no = of_get_gpio(np, i);
1303 of_property_read_string_index(np,
1304 "qcom,gpio-names", i, &name);
1305
1306 snprintf(result, GPIO_NAME_MAX_LEN, "%s-%s",
1307 dev_name(dev), name ? name : "?");
1308 pin_data->gpio_data->gpio[i].name = result;
1309 dev_dbg(dev, "%s: gpio[%s] = %d\n", __func__,
Asutosh Das390519d2012-12-21 12:21:42 +05301310 pin_data->gpio_data->gpio[i].name,
1311 pin_data->gpio_data->gpio[i].no);
Asutosh Das33a4ff52012-12-18 16:14:02 +05301312 }
Asutosh Das390519d2012-12-21 12:21:42 +05301313 } else {
1314 pin_data->pad_data =
1315 devm_kzalloc(dev,
1316 sizeof(struct sdhci_msm_pad_data),
1317 GFP_KERNEL);
1318 if (!pin_data->pad_data) {
1319 dev_err(dev,
1320 "No memory for pin_data->pad_data\n");
1321 ret = -ENOMEM;
1322 goto out;
1323 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301324
Asutosh Das390519d2012-12-21 12:21:42 +05301325 ret = of_alias_get_id(np, "sdhc");
1326 if (ret < 0) {
1327 dev_err(dev, "Failed to get slot index %d\n", ret);
1328 goto out;
1329 }
1330 id = ret;
1331
1332 ret = sdhci_msm_dt_get_pad_pull_info(
1333 dev, id, &pin_data->pad_data->pull);
1334 if (ret)
1335 goto out;
1336 ret = sdhci_msm_dt_get_pad_drv_info(
1337 dev, id, &pin_data->pad_data->drv);
1338 if (ret)
1339 goto out;
1340
1341 }
1342 pdata->pin_data = pin_data;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301343out:
1344 if (ret)
1345 dev_err(dev, "%s failed with err %d\n", __func__, ret);
1346 return ret;
1347}
1348
1349/* Parse platform data */
1350static struct sdhci_msm_pltfm_data *sdhci_msm_populate_pdata(struct device *dev)
1351{
1352 struct sdhci_msm_pltfm_data *pdata = NULL;
1353 struct device_node *np = dev->of_node;
1354 u32 bus_width = 0;
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301355 u32 cpu_dma_latency;
Asutosh Das1369f7b2013-06-03 18:56:52 +05301356 int len, i, mpm_int;
Sahitya Tummala00240122013-02-28 19:50:51 +05301357 int clk_table_len;
1358 u32 *clk_table = NULL;
Sujit Reddy Thumma4ddff322013-06-03 09:54:32 +05301359 enum of_gpio_flags flags = OF_GPIO_ACTIVE_LOW;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301360
1361 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1362 if (!pdata) {
1363 dev_err(dev, "failed to allocate memory for platform data\n");
1364 goto out;
1365 }
1366
Sujit Reddy Thumma4ddff322013-06-03 09:54:32 +05301367 pdata->status_gpio = of_get_named_gpio_flags(np, "cd-gpios", 0, &flags);
1368 if (gpio_is_valid(pdata->status_gpio) & !(flags & OF_GPIO_ACTIVE_LOW))
1369 pdata->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
Sahitya Tummala62448d92013-03-12 14:57:46 +05301370
Asutosh Das33a4ff52012-12-18 16:14:02 +05301371 of_property_read_u32(np, "qcom,bus-width", &bus_width);
1372 if (bus_width == 8)
1373 pdata->mmc_bus_width = MMC_CAP_8_BIT_DATA;
1374 else if (bus_width == 4)
1375 pdata->mmc_bus_width = MMC_CAP_4_BIT_DATA;
1376 else {
1377 dev_notice(dev, "invalid bus-width, default to 1-bit mode\n");
1378 pdata->mmc_bus_width = 0;
1379 }
1380
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301381 if (!of_property_read_u32(np, "qcom,cpu-dma-latency-us",
1382 &cpu_dma_latency))
1383 pdata->cpu_dma_latency_us = cpu_dma_latency;
1384
Sahitya Tummala00240122013-02-28 19:50:51 +05301385 if (sdhci_msm_dt_get_array(dev, "qcom,clk-rates",
1386 &clk_table, &clk_table_len, 0)) {
1387 dev_err(dev, "failed parsing supported clock rates\n");
1388 goto out;
1389 }
1390 if (!clk_table || !clk_table_len) {
1391 dev_err(dev, "Invalid clock table\n");
1392 goto out;
1393 }
1394 pdata->sup_clk_table = clk_table;
1395 pdata->sup_clk_cnt = clk_table_len;
1396
Asutosh Das33a4ff52012-12-18 16:14:02 +05301397 pdata->vreg_data = devm_kzalloc(dev, sizeof(struct
1398 sdhci_msm_slot_reg_data),
1399 GFP_KERNEL);
1400 if (!pdata->vreg_data) {
1401 dev_err(dev, "failed to allocate memory for vreg data\n");
1402 goto out;
1403 }
1404
1405 if (sdhci_msm_dt_parse_vreg_info(dev, &pdata->vreg_data->vdd_data,
1406 "vdd")) {
1407 dev_err(dev, "failed parsing vdd data\n");
1408 goto out;
1409 }
1410 if (sdhci_msm_dt_parse_vreg_info(dev,
1411 &pdata->vreg_data->vdd_io_data,
1412 "vdd-io")) {
1413 dev_err(dev, "failed parsing vdd-io data\n");
1414 goto out;
1415 }
1416
1417 if (sdhci_msm_dt_parse_gpio_info(dev, pdata)) {
1418 dev_err(dev, "failed parsing gpio data\n");
1419 goto out;
1420 }
1421
Asutosh Das33a4ff52012-12-18 16:14:02 +05301422 len = of_property_count_strings(np, "qcom,bus-speed-mode");
1423
1424 for (i = 0; i < len; i++) {
1425 const char *name = NULL;
1426
1427 of_property_read_string_index(np,
1428 "qcom,bus-speed-mode", i, &name);
1429 if (!name)
1430 continue;
1431
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -07001432 if (!strncmp(name, "HS400_1p8v", sizeof("HS400_1p8v")))
1433 pdata->caps2 |= MMC_CAP2_HS400_1_8V;
1434 else if (!strncmp(name, "HS400_1p2v", sizeof("HS400_1p2v")))
1435 pdata->caps2 |= MMC_CAP2_HS400_1_2V;
1436 else if (!strncmp(name, "HS200_1p8v", sizeof("HS200_1p8v")))
Asutosh Das33a4ff52012-12-18 16:14:02 +05301437 pdata->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
1438 else if (!strncmp(name, "HS200_1p2v", sizeof("HS200_1p2v")))
1439 pdata->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
1440 else if (!strncmp(name, "DDR_1p8v", sizeof("DDR_1p8v")))
1441 pdata->caps |= MMC_CAP_1_8V_DDR
1442 | MMC_CAP_UHS_DDR50;
1443 else if (!strncmp(name, "DDR_1p2v", sizeof("DDR_1p2v")))
1444 pdata->caps |= MMC_CAP_1_2V_DDR
1445 | MMC_CAP_UHS_DDR50;
1446 }
1447
1448 if (of_get_property(np, "qcom,nonremovable", NULL))
1449 pdata->nonremovable = true;
1450
Asutosh Das1369f7b2013-06-03 18:56:52 +05301451 if (!of_property_read_u32(np, "qcom,dat1-mpm-int",
1452 &mpm_int))
1453 pdata->mpm_sdiowakeup_int = mpm_int;
1454 else
1455 pdata->mpm_sdiowakeup_int = -1;
1456
Asutosh Das33a4ff52012-12-18 16:14:02 +05301457 return pdata;
1458out:
1459 return NULL;
1460}
1461
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301462/* Returns required bandwidth in Bytes per Sec */
1463static unsigned int sdhci_get_bw_required(struct sdhci_host *host,
1464 struct mmc_ios *ios)
1465{
Sahitya Tummala53aff982013-04-03 18:03:31 +05301466 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1467 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1468
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301469 unsigned int bw;
1470
Sahitya Tummala53aff982013-04-03 18:03:31 +05301471 bw = msm_host->clk_rate;
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301472 /*
1473 * For DDR mode, SDCC controller clock will be at
1474 * the double rate than the actual clock that goes to card.
1475 */
1476 if (ios->bus_width == MMC_BUS_WIDTH_4)
1477 bw /= 2;
1478 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1479 bw /= 8;
1480
1481 return bw;
1482}
1483
1484static int sdhci_msm_bus_get_vote_for_bw(struct sdhci_msm_host *host,
1485 unsigned int bw)
1486{
1487 unsigned int *table = host->pdata->voting_data->bw_vecs;
1488 unsigned int size = host->pdata->voting_data->bw_vecs_size;
1489 int i;
1490
1491 if (host->msm_bus_vote.is_max_bw_needed && bw)
1492 return host->msm_bus_vote.max_bw_vote;
1493
1494 for (i = 0; i < size; i++) {
1495 if (bw <= table[i])
1496 break;
1497 }
1498
1499 if (i && (i == size))
1500 i--;
1501
1502 return i;
1503}
1504
1505/*
1506 * This function must be called with host lock acquired.
1507 * Caller of this function should also ensure that msm bus client
1508 * handle is not null.
1509 */
1510static inline int sdhci_msm_bus_set_vote(struct sdhci_msm_host *msm_host,
1511 int vote,
1512 unsigned long flags)
1513{
1514 struct sdhci_host *host = platform_get_drvdata(msm_host->pdev);
1515 int rc = 0;
1516
1517 if (vote != msm_host->msm_bus_vote.curr_vote) {
1518 spin_unlock_irqrestore(&host->lock, flags);
1519 rc = msm_bus_scale_client_update_request(
1520 msm_host->msm_bus_vote.client_handle, vote);
1521 spin_lock_irqsave(&host->lock, flags);
1522 if (rc) {
1523 pr_err("%s: msm_bus_scale_client_update_request() failed: bus_client_handle=0x%x, vote=%d, err=%d\n",
1524 mmc_hostname(host->mmc),
1525 msm_host->msm_bus_vote.client_handle, vote, rc);
1526 goto out;
1527 }
1528 msm_host->msm_bus_vote.curr_vote = vote;
1529 }
1530out:
1531 return rc;
1532}
1533
1534/*
1535 * Internal work. Work to set 0 bandwidth for msm bus.
1536 */
1537static void sdhci_msm_bus_work(struct work_struct *work)
1538{
1539 struct sdhci_msm_host *msm_host;
1540 struct sdhci_host *host;
1541 unsigned long flags;
1542
1543 msm_host = container_of(work, struct sdhci_msm_host,
1544 msm_bus_vote.vote_work.work);
1545 host = platform_get_drvdata(msm_host->pdev);
1546
1547 if (!msm_host->msm_bus_vote.client_handle)
1548 return;
1549
1550 spin_lock_irqsave(&host->lock, flags);
1551 /* don't vote for 0 bandwidth if any request is in progress */
1552 if (!host->mrq) {
1553 sdhci_msm_bus_set_vote(msm_host,
1554 msm_host->msm_bus_vote.min_bw_vote, flags);
1555 } else
1556 pr_warning("%s: %s: Transfer in progress. skipping bus voting to 0 bandwidth\n",
1557 mmc_hostname(host->mmc), __func__);
1558 spin_unlock_irqrestore(&host->lock, flags);
1559}
1560
1561/*
1562 * This function cancels any scheduled delayed work and sets the bus
1563 * vote based on bw (bandwidth) argument.
1564 */
1565static void sdhci_msm_bus_cancel_work_and_set_vote(struct sdhci_host *host,
1566 unsigned int bw)
1567{
1568 int vote;
1569 unsigned long flags;
1570 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1571 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1572
1573 cancel_delayed_work_sync(&msm_host->msm_bus_vote.vote_work);
1574 spin_lock_irqsave(&host->lock, flags);
1575 vote = sdhci_msm_bus_get_vote_for_bw(msm_host, bw);
1576 sdhci_msm_bus_set_vote(msm_host, vote, flags);
1577 spin_unlock_irqrestore(&host->lock, flags);
1578}
1579
1580#define MSM_MMC_BUS_VOTING_DELAY 200 /* msecs */
1581
1582/* This function queues a work which will set the bandwidth requiement to 0 */
1583static void sdhci_msm_bus_queue_work(struct sdhci_host *host)
1584{
1585 unsigned long flags;
1586 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1587 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1588
1589 spin_lock_irqsave(&host->lock, flags);
1590 if (msm_host->msm_bus_vote.min_bw_vote !=
1591 msm_host->msm_bus_vote.curr_vote)
1592 queue_delayed_work(system_nrt_wq,
1593 &msm_host->msm_bus_vote.vote_work,
1594 msecs_to_jiffies(MSM_MMC_BUS_VOTING_DELAY));
1595 spin_unlock_irqrestore(&host->lock, flags);
1596}
1597
1598static int sdhci_msm_bus_register(struct sdhci_msm_host *host,
1599 struct platform_device *pdev)
1600{
1601 int rc = 0;
1602 struct msm_bus_scale_pdata *bus_pdata;
1603
1604 struct sdhci_msm_bus_voting_data *data;
1605 struct device *dev = &pdev->dev;
1606
1607 data = devm_kzalloc(dev,
1608 sizeof(struct sdhci_msm_bus_voting_data), GFP_KERNEL);
1609 if (!data) {
1610 dev_err(&pdev->dev,
1611 "%s: failed to allocate memory\n", __func__);
1612 rc = -ENOMEM;
1613 goto out;
1614 }
1615 data->bus_pdata = msm_bus_cl_get_pdata(pdev);
1616 if (data->bus_pdata) {
1617 rc = sdhci_msm_dt_get_array(dev, "qcom,bus-bw-vectors-bps",
1618 &data->bw_vecs, &data->bw_vecs_size, 0);
1619 if (rc) {
1620 dev_err(&pdev->dev,
1621 "%s: Failed to get bus-bw-vectors-bps\n",
1622 __func__);
1623 goto out;
1624 }
1625 host->pdata->voting_data = data;
1626 }
1627 if (host->pdata->voting_data &&
1628 host->pdata->voting_data->bus_pdata &&
1629 host->pdata->voting_data->bw_vecs &&
1630 host->pdata->voting_data->bw_vecs_size) {
1631
1632 bus_pdata = host->pdata->voting_data->bus_pdata;
1633 host->msm_bus_vote.client_handle =
1634 msm_bus_scale_register_client(bus_pdata);
1635 if (!host->msm_bus_vote.client_handle) {
1636 dev_err(&pdev->dev, "msm_bus_scale_register_client()\n");
1637 rc = -EFAULT;
1638 goto out;
1639 }
1640 /* cache the vote index for minimum and maximum bandwidth */
1641 host->msm_bus_vote.min_bw_vote =
1642 sdhci_msm_bus_get_vote_for_bw(host, 0);
1643 host->msm_bus_vote.max_bw_vote =
1644 sdhci_msm_bus_get_vote_for_bw(host, UINT_MAX);
1645 } else {
1646 devm_kfree(dev, data);
1647 }
1648
1649out:
1650 return rc;
1651}
1652
1653static void sdhci_msm_bus_unregister(struct sdhci_msm_host *host)
1654{
1655 if (host->msm_bus_vote.client_handle)
1656 msm_bus_scale_unregister_client(
1657 host->msm_bus_vote.client_handle);
1658}
1659
1660static void sdhci_msm_bus_voting(struct sdhci_host *host, u32 enable)
1661{
1662 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1663 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1664 struct mmc_ios *ios = &host->mmc->ios;
1665 unsigned int bw;
1666
1667 if (!msm_host->msm_bus_vote.client_handle)
1668 return;
1669
1670 bw = sdhci_get_bw_required(host, ios);
Sahitya Tummalac45ae732013-05-23 15:59:22 +05301671 if (enable) {
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301672 sdhci_msm_bus_cancel_work_and_set_vote(host, bw);
Sahitya Tummalac45ae732013-05-23 15:59:22 +05301673 } else {
1674 /*
1675 * If clock gating is enabled, then remove the vote
1676 * immediately because clocks will be disabled only
1677 * after SDHCI_MSM_MMC_CLK_GATE_DELAY and thus no
1678 * additional delay is required to remove the bus vote.
1679 */
1680 if (host->mmc->clkgate_delay)
1681 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
1682 else
1683 sdhci_msm_bus_queue_work(host);
1684 }
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301685}
1686
Asutosh Das33a4ff52012-12-18 16:14:02 +05301687/* Regulator utility functions */
1688static int sdhci_msm_vreg_init_reg(struct device *dev,
1689 struct sdhci_msm_reg_data *vreg)
1690{
1691 int ret = 0;
1692
1693 /* check if regulator is already initialized? */
1694 if (vreg->reg)
1695 goto out;
1696
1697 /* Get the regulator handle */
1698 vreg->reg = devm_regulator_get(dev, vreg->name);
1699 if (IS_ERR(vreg->reg)) {
1700 ret = PTR_ERR(vreg->reg);
1701 pr_err("%s: devm_regulator_get(%s) failed. ret=%d\n",
1702 __func__, vreg->name, ret);
1703 goto out;
1704 }
1705
Asutosh Das95afcad2013-06-28 15:03:44 +05301706 if (regulator_count_voltages(vreg->reg) > 0) {
1707 vreg->set_voltage_sup = true;
1708 /* sanity check */
1709 if (!vreg->high_vol_level || !vreg->hpm_uA) {
1710 pr_err("%s: %s invalid constraints specified\n",
1711 __func__, vreg->name);
1712 ret = -EINVAL;
1713 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301714 }
1715
1716out:
1717 return ret;
1718}
1719
1720static void sdhci_msm_vreg_deinit_reg(struct sdhci_msm_reg_data *vreg)
1721{
1722 if (vreg->reg)
1723 devm_regulator_put(vreg->reg);
1724}
1725
1726static int sdhci_msm_vreg_set_optimum_mode(struct sdhci_msm_reg_data
1727 *vreg, int uA_load)
1728{
1729 int ret = 0;
1730
1731 /*
1732 * regulators that do not support regulator_set_voltage also
1733 * do not support regulator_set_optimum_mode
1734 */
Asutosh Das95afcad2013-06-28 15:03:44 +05301735 if (vreg->set_voltage_sup) {
1736 ret = regulator_set_optimum_mode(vreg->reg, uA_load);
1737 if (ret < 0)
1738 pr_err("%s: regulator_set_optimum_mode(reg=%s,uA_load=%d) failed. ret=%d\n",
Asutosh Das33a4ff52012-12-18 16:14:02 +05301739 __func__, vreg->name, uA_load, ret);
1740 else
1741 /*
1742 * regulator_set_optimum_mode() can return non zero
1743 * value even for success case.
1744 */
1745 ret = 0;
Asutosh Das95afcad2013-06-28 15:03:44 +05301746 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301747 return ret;
1748}
1749
1750static int sdhci_msm_vreg_set_voltage(struct sdhci_msm_reg_data *vreg,
1751 int min_uV, int max_uV)
1752{
1753 int ret = 0;
Asutosh Das95afcad2013-06-28 15:03:44 +05301754 if (vreg->set_voltage_sup) {
1755 ret = regulator_set_voltage(vreg->reg, min_uV, max_uV);
1756 if (ret) {
1757 pr_err("%s: regulator_set_voltage(%s)failed. min_uV=%d,max_uV=%d,ret=%d\n",
Asutosh Das33a4ff52012-12-18 16:14:02 +05301758 __func__, vreg->name, min_uV, max_uV, ret);
1759 }
Asutosh Das95afcad2013-06-28 15:03:44 +05301760 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301761
1762 return ret;
1763}
1764
1765static int sdhci_msm_vreg_enable(struct sdhci_msm_reg_data *vreg)
1766{
1767 int ret = 0;
1768
1769 /* Put regulator in HPM (high power mode) */
1770 ret = sdhci_msm_vreg_set_optimum_mode(vreg, vreg->hpm_uA);
1771 if (ret < 0)
1772 return ret;
1773
1774 if (!vreg->is_enabled) {
1775 /* Set voltage level */
1776 ret = sdhci_msm_vreg_set_voltage(vreg, vreg->high_vol_level,
1777 vreg->high_vol_level);
1778 if (ret)
1779 return ret;
1780 }
1781 ret = regulator_enable(vreg->reg);
1782 if (ret) {
1783 pr_err("%s: regulator_enable(%s) failed. ret=%d\n",
1784 __func__, vreg->name, ret);
1785 return ret;
1786 }
1787 vreg->is_enabled = true;
1788 return ret;
1789}
1790
1791static int sdhci_msm_vreg_disable(struct sdhci_msm_reg_data *vreg)
1792{
1793 int ret = 0;
1794
1795 /* Never disable regulator marked as always_on */
1796 if (vreg->is_enabled && !vreg->is_always_on) {
1797 ret = regulator_disable(vreg->reg);
1798 if (ret) {
1799 pr_err("%s: regulator_disable(%s) failed. ret=%d\n",
1800 __func__, vreg->name, ret);
1801 goto out;
1802 }
1803 vreg->is_enabled = false;
1804
1805 ret = sdhci_msm_vreg_set_optimum_mode(vreg, 0);
1806 if (ret < 0)
1807 goto out;
1808
1809 /* Set min. voltage level to 0 */
1810 ret = sdhci_msm_vreg_set_voltage(vreg, 0, vreg->high_vol_level);
1811 if (ret)
1812 goto out;
1813 } else if (vreg->is_enabled && vreg->is_always_on) {
1814 if (vreg->lpm_sup) {
1815 /* Put always_on regulator in LPM (low power mode) */
1816 ret = sdhci_msm_vreg_set_optimum_mode(vreg,
1817 vreg->lpm_uA);
1818 if (ret < 0)
1819 goto out;
1820 }
1821 }
1822out:
1823 return ret;
1824}
1825
1826static int sdhci_msm_setup_vreg(struct sdhci_msm_pltfm_data *pdata,
1827 bool enable, bool is_init)
1828{
1829 int ret = 0, i;
1830 struct sdhci_msm_slot_reg_data *curr_slot;
1831 struct sdhci_msm_reg_data *vreg_table[2];
1832
1833 curr_slot = pdata->vreg_data;
1834 if (!curr_slot) {
1835 pr_debug("%s: vreg info unavailable,assuming the slot is powered by always on domain\n",
1836 __func__);
1837 goto out;
1838 }
1839
1840 vreg_table[0] = curr_slot->vdd_data;
1841 vreg_table[1] = curr_slot->vdd_io_data;
1842
1843 for (i = 0; i < ARRAY_SIZE(vreg_table); i++) {
1844 if (vreg_table[i]) {
1845 if (enable)
1846 ret = sdhci_msm_vreg_enable(vreg_table[i]);
1847 else
1848 ret = sdhci_msm_vreg_disable(vreg_table[i]);
1849 if (ret)
1850 goto out;
1851 }
1852 }
1853out:
1854 return ret;
1855}
1856
1857/*
1858 * Reset vreg by ensuring it is off during probe. A call
1859 * to enable vreg is needed to balance disable vreg
1860 */
1861static int sdhci_msm_vreg_reset(struct sdhci_msm_pltfm_data *pdata)
1862{
1863 int ret;
1864
1865 ret = sdhci_msm_setup_vreg(pdata, 1, true);
1866 if (ret)
1867 return ret;
1868 ret = sdhci_msm_setup_vreg(pdata, 0, true);
1869 return ret;
1870}
1871
1872/* This init function should be called only once for each SDHC slot */
1873static int sdhci_msm_vreg_init(struct device *dev,
1874 struct sdhci_msm_pltfm_data *pdata,
1875 bool is_init)
1876{
1877 int ret = 0;
1878 struct sdhci_msm_slot_reg_data *curr_slot;
1879 struct sdhci_msm_reg_data *curr_vdd_reg, *curr_vdd_io_reg;
1880
1881 curr_slot = pdata->vreg_data;
1882 if (!curr_slot)
1883 goto out;
1884
1885 curr_vdd_reg = curr_slot->vdd_data;
1886 curr_vdd_io_reg = curr_slot->vdd_io_data;
1887
1888 if (!is_init)
1889 /* Deregister all regulators from regulator framework */
1890 goto vdd_io_reg_deinit;
1891
1892 /*
1893 * Get the regulator handle from voltage regulator framework
1894 * and then try to set the voltage level for the regulator
1895 */
1896 if (curr_vdd_reg) {
1897 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_reg);
1898 if (ret)
1899 goto out;
1900 }
1901 if (curr_vdd_io_reg) {
1902 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_io_reg);
1903 if (ret)
1904 goto vdd_reg_deinit;
1905 }
1906 ret = sdhci_msm_vreg_reset(pdata);
1907 if (ret)
1908 dev_err(dev, "vreg reset failed (%d)\n", ret);
1909 goto out;
1910
1911vdd_io_reg_deinit:
1912 if (curr_vdd_io_reg)
1913 sdhci_msm_vreg_deinit_reg(curr_vdd_io_reg);
1914vdd_reg_deinit:
1915 if (curr_vdd_reg)
1916 sdhci_msm_vreg_deinit_reg(curr_vdd_reg);
1917out:
1918 return ret;
1919}
1920
1921
1922static int sdhci_msm_set_vdd_io_vol(struct sdhci_msm_pltfm_data *pdata,
1923 enum vdd_io_level level,
1924 unsigned int voltage_level)
1925{
1926 int ret = 0;
1927 int set_level;
1928 struct sdhci_msm_reg_data *vdd_io_reg;
1929
1930 if (!pdata->vreg_data)
1931 return ret;
1932
1933 vdd_io_reg = pdata->vreg_data->vdd_io_data;
1934 if (vdd_io_reg && vdd_io_reg->is_enabled) {
1935 switch (level) {
1936 case VDD_IO_LOW:
1937 set_level = vdd_io_reg->low_vol_level;
1938 break;
1939 case VDD_IO_HIGH:
1940 set_level = vdd_io_reg->high_vol_level;
1941 break;
1942 case VDD_IO_SET_LEVEL:
1943 set_level = voltage_level;
1944 break;
1945 default:
1946 pr_err("%s: invalid argument level = %d",
1947 __func__, level);
1948 ret = -EINVAL;
1949 return ret;
1950 }
1951 ret = sdhci_msm_vreg_set_voltage(vdd_io_reg, set_level,
1952 set_level);
1953 }
1954 return ret;
1955}
1956
Asutosh Das1369f7b2013-06-03 18:56:52 +05301957/*
1958 * Acquire spin-lock host->lock before calling this function
1959 */
1960static void sdhci_msm_cfg_sdiowakeup_gpio_irq(struct sdhci_host *host,
1961 bool enable)
1962{
1963 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1964 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1965
1966 if (enable && !msm_host->is_sdiowakeup_enabled)
1967 enable_irq(msm_host->pdata->sdiowakeup_irq);
1968 else if (!enable && msm_host->is_sdiowakeup_enabled)
1969 disable_irq_nosync(msm_host->pdata->sdiowakeup_irq);
1970 else
1971 dev_warn(&msm_host->pdev->dev, "%s: wakeup to config: %d curr: %d\n",
1972 __func__, enable, msm_host->is_sdiowakeup_enabled);
1973 msm_host->is_sdiowakeup_enabled = enable;
1974}
1975
1976static irqreturn_t sdhci_msm_sdiowakeup_irq(int irq, void *data)
1977{
1978 struct sdhci_host *host = (struct sdhci_host *)data;
1979 unsigned long flags;
1980
1981 pr_debug("%s: irq (%d) received\n", __func__, irq);
1982
1983 spin_lock_irqsave(&host->lock, flags);
1984 sdhci_msm_cfg_sdiowakeup_gpio_irq(host, false);
1985 spin_unlock_irqrestore(&host->lock, flags);
1986
1987 return IRQ_HANDLED;
1988}
1989
Asutosh Das33a4ff52012-12-18 16:14:02 +05301990static irqreturn_t sdhci_msm_pwr_irq(int irq, void *data)
1991{
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07001992 struct sdhci_host *host = (struct sdhci_host *)data;
1993 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1994 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301995 u8 irq_status = 0;
1996 u8 irq_ack = 0;
1997 int ret = 0;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301998 int pwr_state = 0, io_level = 0;
1999 unsigned long flags;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302000
2001 irq_status = readb_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
2002 pr_debug("%s: Received IRQ(%d), status=0x%x\n",
2003 mmc_hostname(msm_host->mmc), irq, irq_status);
2004
2005 /* Clear the interrupt */
2006 writeb_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
2007 /*
2008 * SDHC has core_mem and hc_mem device memory and these memory
2009 * addresses do not fall within 1KB region. Hence, any update to
2010 * core_mem address space would require an mb() to ensure this gets
2011 * completed before its next update to registers within hc_mem.
2012 */
2013 mb();
2014
2015 /* Handle BUS ON/OFF*/
2016 if (irq_status & CORE_PWRCTL_BUS_ON) {
2017 ret = sdhci_msm_setup_vreg(msm_host->pdata, true, false);
Sahitya Tummala179e7382013-03-20 19:24:01 +05302018 if (!ret) {
Asutosh Das33a4ff52012-12-18 16:14:02 +05302019 ret = sdhci_msm_setup_pins(msm_host->pdata, true);
Sahitya Tummala179e7382013-03-20 19:24:01 +05302020 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
2021 VDD_IO_HIGH, 0);
2022 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05302023 if (ret)
2024 irq_ack |= CORE_PWRCTL_BUS_FAIL;
2025 else
2026 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala179e7382013-03-20 19:24:01 +05302027
2028 pwr_state = REQ_BUS_ON;
2029 io_level = REQ_IO_HIGH;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302030 }
2031 if (irq_status & CORE_PWRCTL_BUS_OFF) {
2032 ret = sdhci_msm_setup_vreg(msm_host->pdata, false, false);
Sahitya Tummala179e7382013-03-20 19:24:01 +05302033 if (!ret) {
Asutosh Das33a4ff52012-12-18 16:14:02 +05302034 ret = sdhci_msm_setup_pins(msm_host->pdata, false);
Sahitya Tummala179e7382013-03-20 19:24:01 +05302035 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
2036 VDD_IO_LOW, 0);
2037 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05302038 if (ret)
2039 irq_ack |= CORE_PWRCTL_BUS_FAIL;
2040 else
2041 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala179e7382013-03-20 19:24:01 +05302042
2043 pwr_state = REQ_BUS_OFF;
2044 io_level = REQ_IO_LOW;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302045 }
2046 /* Handle IO LOW/HIGH */
2047 if (irq_status & CORE_PWRCTL_IO_LOW) {
2048 /* Switch voltage Low */
2049 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_LOW, 0);
2050 if (ret)
2051 irq_ack |= CORE_PWRCTL_IO_FAIL;
2052 else
2053 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala179e7382013-03-20 19:24:01 +05302054
2055 io_level = REQ_IO_LOW;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302056 }
2057 if (irq_status & CORE_PWRCTL_IO_HIGH) {
2058 /* Switch voltage High */
2059 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_HIGH, 0);
2060 if (ret)
2061 irq_ack |= CORE_PWRCTL_IO_FAIL;
2062 else
2063 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala179e7382013-03-20 19:24:01 +05302064
2065 io_level = REQ_IO_HIGH;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302066 }
2067
2068 /* ACK status to the core */
2069 writeb_relaxed(irq_ack, (msm_host->core_mem + CORE_PWRCTL_CTL));
2070 /*
2071 * SDHC has core_mem and hc_mem device memory and these memory
2072 * addresses do not fall within 1KB region. Hence, any update to
2073 * core_mem address space would require an mb() to ensure this gets
2074 * completed before its next update to registers within hc_mem.
2075 */
2076 mb();
2077
Sahitya Tummala179e7382013-03-20 19:24:01 +05302078 if (io_level & REQ_IO_HIGH)
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07002079 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2080 ~CORE_IO_PAD_PWR_SWITCH),
2081 host->ioaddr + CORE_VENDOR_SPEC);
Sahitya Tummala179e7382013-03-20 19:24:01 +05302082 else if (io_level & REQ_IO_LOW)
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07002083 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
2084 CORE_IO_PAD_PWR_SWITCH),
2085 host->ioaddr + CORE_VENDOR_SPEC);
2086 mb();
2087
Asutosh Das33a4ff52012-12-18 16:14:02 +05302088 pr_debug("%s: Handled IRQ(%d), ret=%d, ack=0x%x\n",
2089 mmc_hostname(msm_host->mmc), irq, ret, irq_ack);
Sahitya Tummala179e7382013-03-20 19:24:01 +05302090 spin_lock_irqsave(&host->lock, flags);
2091 if (pwr_state)
2092 msm_host->curr_pwr_state = pwr_state;
2093 if (io_level)
2094 msm_host->curr_io_level = io_level;
2095 complete(&msm_host->pwr_irq_completion);
2096 spin_unlock_irqrestore(&host->lock, flags);
2097
Asutosh Das33a4ff52012-12-18 16:14:02 +05302098 return IRQ_HANDLED;
2099}
2100
2101/* This function returns the max. current supported by VDD rail in mA */
2102static unsigned int sdhci_msm_get_vreg_vdd_max_current(struct sdhci_msm_host
2103 *host)
2104{
2105 struct sdhci_msm_slot_reg_data *curr_slot = host->pdata->vreg_data;
2106 if (!curr_slot)
2107 return 0;
2108 if (curr_slot->vdd_data)
2109 return curr_slot->vdd_data->hpm_uA / 1000;
2110 else
2111 return 0;
2112}
Sahitya Tummala3b292c32013-06-20 14:00:18 +05302113
2114static ssize_t
2115show_polling(struct device *dev, struct device_attribute *attr, char *buf)
2116{
2117 struct sdhci_host *host = dev_get_drvdata(dev);
2118 int poll;
2119 unsigned long flags;
2120
2121 spin_lock_irqsave(&host->lock, flags);
2122 poll = !!(host->mmc->caps & MMC_CAP_NEEDS_POLL);
2123 spin_unlock_irqrestore(&host->lock, flags);
2124
2125 return snprintf(buf, PAGE_SIZE, "%d\n", poll);
2126}
2127
2128static ssize_t
2129store_polling(struct device *dev, struct device_attribute *attr,
2130 const char *buf, size_t count)
2131{
2132 struct sdhci_host *host = dev_get_drvdata(dev);
2133 int value;
2134 unsigned long flags;
2135
2136 if (!kstrtou32(buf, 0, &value)) {
2137 spin_lock_irqsave(&host->lock, flags);
2138 if (value) {
2139 host->mmc->caps |= MMC_CAP_NEEDS_POLL;
2140 mmc_detect_change(host->mmc, 0);
2141 } else {
2142 host->mmc->caps &= ~MMC_CAP_NEEDS_POLL;
2143 }
2144 spin_unlock_irqrestore(&host->lock, flags);
2145 }
2146 return count;
2147}
2148
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302149static ssize_t
2150show_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
2151 char *buf)
2152{
2153 struct sdhci_host *host = dev_get_drvdata(dev);
2154 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2155 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2156
2157 return snprintf(buf, PAGE_SIZE, "%u\n",
2158 msm_host->msm_bus_vote.is_max_bw_needed);
2159}
2160
2161static ssize_t
2162store_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
2163 const char *buf, size_t count)
2164{
2165 struct sdhci_host *host = dev_get_drvdata(dev);
2166 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2167 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2168 uint32_t value;
2169 unsigned long flags;
2170
2171 if (!kstrtou32(buf, 0, &value)) {
2172 spin_lock_irqsave(&host->lock, flags);
2173 msm_host->msm_bus_vote.is_max_bw_needed = !!value;
2174 spin_unlock_irqrestore(&host->lock, flags);
2175 }
2176 return count;
2177}
Asutosh Das33a4ff52012-12-18 16:14:02 +05302178
Sahitya Tummala179e7382013-03-20 19:24:01 +05302179static void sdhci_msm_check_power_status(struct sdhci_host *host, u32 req_type)
Asutosh Das33a4ff52012-12-18 16:14:02 +05302180{
2181 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2182 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala179e7382013-03-20 19:24:01 +05302183 unsigned long flags;
2184 bool done = false;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302185
Sahitya Tummala179e7382013-03-20 19:24:01 +05302186 spin_lock_irqsave(&host->lock, flags);
2187 pr_debug("%s: %s: request %d curr_pwr_state %x curr_io_level %x\n",
2188 mmc_hostname(host->mmc), __func__, req_type,
2189 msm_host->curr_pwr_state, msm_host->curr_io_level);
2190 if ((req_type & msm_host->curr_pwr_state) ||
2191 (req_type & msm_host->curr_io_level))
2192 done = true;
2193 spin_unlock_irqrestore(&host->lock, flags);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302194
Sahitya Tummala179e7382013-03-20 19:24:01 +05302195 /*
2196 * This is needed here to hanlde a case where IRQ gets
2197 * triggered even before this function is called so that
2198 * x->done counter of completion gets reset. Otherwise,
2199 * next call to wait_for_completion returns immediately
2200 * without actually waiting for the IRQ to be handled.
2201 */
2202 if (done)
2203 init_completion(&msm_host->pwr_irq_completion);
2204 else
2205 wait_for_completion(&msm_host->pwr_irq_completion);
2206
2207 pr_debug("%s: %s: request %d done\n", mmc_hostname(host->mmc),
2208 __func__, req_type);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302209}
2210
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07002211static void sdhci_msm_toggle_cdr(struct sdhci_host *host, bool enable)
2212{
2213 if (enable)
2214 writel_relaxed((readl_relaxed(host->ioaddr +
2215 CORE_DLL_CONFIG) | CORE_CDR_EN),
2216 host->ioaddr + CORE_DLL_CONFIG);
2217 else
2218 writel_relaxed((readl_relaxed(host->ioaddr +
2219 CORE_DLL_CONFIG) & ~CORE_CDR_EN),
2220 host->ioaddr + CORE_DLL_CONFIG);
2221}
2222
Asutosh Das3781bd82013-01-10 21:11:04 +05302223static unsigned int sdhci_msm_max_segs(void)
2224{
2225 return SDHCI_MSM_MAX_SEGMENTS;
2226}
2227
Sahitya Tummala00240122013-02-28 19:50:51 +05302228static unsigned int sdhci_msm_get_min_clock(struct sdhci_host *host)
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302229{
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302230 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2231 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302232
Sahitya Tummala00240122013-02-28 19:50:51 +05302233 return msm_host->pdata->sup_clk_table[0];
2234}
2235
2236static unsigned int sdhci_msm_get_max_clock(struct sdhci_host *host)
2237{
2238 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2239 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2240 int max_clk_index = msm_host->pdata->sup_clk_cnt;
2241
2242 return msm_host->pdata->sup_clk_table[max_clk_index - 1];
2243}
2244
2245static unsigned int sdhci_msm_get_sup_clk_rate(struct sdhci_host *host,
2246 u32 req_clk)
2247{
2248 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2249 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2250 unsigned int sel_clk = -1;
2251 unsigned char cnt;
2252
2253 if (req_clk < sdhci_msm_get_min_clock(host)) {
2254 sel_clk = sdhci_msm_get_min_clock(host);
2255 return sel_clk;
2256 }
2257
2258 for (cnt = 0; cnt < msm_host->pdata->sup_clk_cnt; cnt++) {
2259 if (msm_host->pdata->sup_clk_table[cnt] > req_clk) {
2260 break;
2261 } else if (msm_host->pdata->sup_clk_table[cnt] == req_clk) {
2262 sel_clk = msm_host->pdata->sup_clk_table[cnt];
2263 break;
2264 } else {
2265 sel_clk = msm_host->pdata->sup_clk_table[cnt];
2266 }
2267 }
2268 return sel_clk;
2269}
2270
Asutosh Das6c0804b2013-11-08 12:33:47 +05302271static int sdhci_msm_enable_controller_clock(struct sdhci_host *host)
2272{
2273 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2274 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2275 int rc = 0;
2276
2277 if (atomic_read(&msm_host->controller_clock))
2278 return 0;
2279
2280 sdhci_msm_bus_voting(host, 1);
2281
2282 if (!IS_ERR(msm_host->pclk)) {
2283 rc = clk_prepare_enable(msm_host->pclk);
2284 if (rc) {
2285 pr_err("%s: %s: failed to enable the pclk with error %d\n",
2286 mmc_hostname(host->mmc), __func__, rc);
2287 goto remove_vote;
2288 }
2289 }
2290
2291 rc = clk_prepare_enable(msm_host->clk);
2292 if (rc) {
2293 pr_err("%s: %s: failed to enable the host-clk with error %d\n",
2294 mmc_hostname(host->mmc), __func__, rc);
2295 goto disable_pclk;
2296 }
2297
2298 atomic_set(&msm_host->controller_clock, 1);
2299 pr_debug("%s: %s: enabled controller clock\n",
2300 mmc_hostname(host->mmc), __func__);
2301 goto out;
2302
2303disable_pclk:
2304 if (!IS_ERR(msm_host->pclk))
2305 clk_disable_unprepare(msm_host->pclk);
2306remove_vote:
2307 if (msm_host->msm_bus_vote.client_handle)
2308 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2309out:
2310 return rc;
2311}
2312
2313
2314
Sahitya Tummala00240122013-02-28 19:50:51 +05302315static int sdhci_msm_prepare_clocks(struct sdhci_host *host, bool enable)
2316{
2317 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2318 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2319 int rc = 0;
2320
2321 if (enable && !atomic_read(&msm_host->clks_on)) {
2322 pr_debug("%s: request to enable clocks\n",
2323 mmc_hostname(host->mmc));
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302324
Asutosh Das6c0804b2013-11-08 12:33:47 +05302325 /*
2326 * The bus-width or the clock rate might have changed
2327 * after controller clocks are enbaled, update bus vote
2328 * in such case.
2329 */
2330 if (atomic_read(&msm_host->controller_clock))
2331 sdhci_msm_bus_voting(host, 1);
2332
2333 rc = sdhci_msm_enable_controller_clock(host);
2334 if (rc)
2335 goto remove_vote;
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302336
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302337 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
2338 rc = clk_prepare_enable(msm_host->bus_clk);
2339 if (rc) {
2340 pr_err("%s: %s: failed to enable the bus-clock with error %d\n",
2341 mmc_hostname(host->mmc), __func__, rc);
Asutosh Das6c0804b2013-11-08 12:33:47 +05302342 goto disable_controller_clk;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302343 }
2344 }
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -07002345 if (!IS_ERR(msm_host->ff_clk)) {
2346 rc = clk_prepare_enable(msm_host->ff_clk);
2347 if (rc) {
2348 pr_err("%s: %s: failed to enable the ff_clk with error %d\n",
2349 mmc_hostname(host->mmc), __func__, rc);
Asutosh Das6c0804b2013-11-08 12:33:47 +05302350 goto disable_bus_clk;
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -07002351 }
2352 }
2353 if (!IS_ERR(msm_host->sleep_clk)) {
2354 rc = clk_prepare_enable(msm_host->sleep_clk);
2355 if (rc) {
2356 pr_err("%s: %s: failed to enable the sleep_clk with error %d\n",
2357 mmc_hostname(host->mmc), __func__, rc);
2358 goto disable_ff_clk;
2359 }
2360 }
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302361 mb();
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302362
Sahitya Tummala00240122013-02-28 19:50:51 +05302363 } else if (!enable && atomic_read(&msm_host->clks_on)) {
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302364 pr_debug("%s: request to disable clocks\n",
2365 mmc_hostname(host->mmc));
2366 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
2367 mb();
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -07002368 if (!IS_ERR_OR_NULL(msm_host->sleep_clk))
2369 clk_disable_unprepare(msm_host->sleep_clk);
2370 if (!IS_ERR_OR_NULL(msm_host->ff_clk))
2371 clk_disable_unprepare(msm_host->ff_clk);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302372 clk_disable_unprepare(msm_host->clk);
2373 if (!IS_ERR(msm_host->pclk))
2374 clk_disable_unprepare(msm_host->pclk);
2375 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2376 clk_disable_unprepare(msm_host->bus_clk);
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302377
Asutosh Das6c0804b2013-11-08 12:33:47 +05302378 atomic_set(&msm_host->controller_clock, 0);
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302379 sdhci_msm_bus_voting(host, 0);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302380 }
Sahitya Tummala00240122013-02-28 19:50:51 +05302381 atomic_set(&msm_host->clks_on, enable);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302382 goto out;
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -07002383disable_ff_clk:
2384 if (!IS_ERR_OR_NULL(msm_host->ff_clk))
2385 clk_disable_unprepare(msm_host->ff_clk);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302386disable_bus_clk:
2387 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2388 clk_disable_unprepare(msm_host->bus_clk);
Asutosh Das6c0804b2013-11-08 12:33:47 +05302389disable_controller_clk:
2390 if (!IS_ERR_OR_NULL(msm_host->clk))
2391 clk_disable_unprepare(msm_host->clk);
2392 if (!IS_ERR_OR_NULL(msm_host->pclk))
2393 clk_disable_unprepare(msm_host->pclk);
2394 atomic_set(&msm_host->controller_clock, 0);
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302395remove_vote:
2396 if (msm_host->msm_bus_vote.client_handle)
2397 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302398out:
Sahitya Tummala00240122013-02-28 19:50:51 +05302399 return rc;
2400}
2401
2402static void sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock)
2403{
2404 int rc;
2405 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2406 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2407 struct mmc_ios curr_ios = host->mmc->ios;
2408 u32 sup_clock, ddr_clock;
Sahitya Tummalac69e2a22013-06-24 09:55:33 +05302409 bool curr_pwrsave;
Sahitya Tummala00240122013-02-28 19:50:51 +05302410
2411 if (!clock) {
Sujit Reddy Thumma0e9ec032014-01-10 10:58:54 +05302412 /*
2413 * disable pwrsave to ensure clock is not auto-gated until
2414 * the rate is >400KHz (initialization complete).
2415 */
2416 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2417 ~CORE_CLK_PWRSAVE, host->ioaddr + CORE_VENDOR_SPEC);
Sahitya Tummala00240122013-02-28 19:50:51 +05302418 sdhci_msm_prepare_clocks(host, false);
2419 host->clock = clock;
2420 return;
2421 }
2422
2423 rc = sdhci_msm_prepare_clocks(host, true);
2424 if (rc)
2425 return;
2426
Sahitya Tummalac69e2a22013-06-24 09:55:33 +05302427 curr_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2428 CORE_CLK_PWRSAVE);
Sahitya Tummala2c4bd642013-08-29 16:21:08 +05302429 if ((clock > 400000) &&
Sahitya Tummalac69e2a22013-06-24 09:55:33 +05302430 !curr_pwrsave && mmc_host_may_gate_card(host->mmc->card))
2431 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2432 | CORE_CLK_PWRSAVE,
2433 host->ioaddr + CORE_VENDOR_SPEC);
2434 /*
2435 * Disable pwrsave for a newly added card if doesn't allow clock
2436 * gating.
2437 */
2438 else if (curr_pwrsave && !mmc_host_may_gate_card(host->mmc->card))
2439 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2440 & ~CORE_CLK_PWRSAVE,
2441 host->ioaddr + CORE_VENDOR_SPEC);
2442
Sahitya Tummala00240122013-02-28 19:50:51 +05302443 sup_clock = sdhci_msm_get_sup_clk_rate(host, clock);
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -07002444 if ((curr_ios.timing == MMC_TIMING_UHS_DDR50) ||
2445 (curr_ios.timing == MMC_TIMING_MMC_HS400)) {
Sahitya Tummala00240122013-02-28 19:50:51 +05302446 /*
2447 * The SDHC requires internal clock frequency to be double the
2448 * actual clock that will be set for DDR mode. The controller
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -07002449 * uses the faster clock(100/400MHz) for some of its parts and
2450 * send the actual required clock (50/200MHz) to the card.
Sahitya Tummala00240122013-02-28 19:50:51 +05302451 */
2452 ddr_clock = clock * 2;
2453 sup_clock = sdhci_msm_get_sup_clk_rate(host,
2454 ddr_clock);
2455 }
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -07002456
2457 /*
2458 * In general all timing modes are controlled via UHS mode select in
2459 * Host Control2 register. eMMC specific HS200/HS400 doesn't have
2460 * their respective modes defined here, hence we use these values.
2461 *
2462 * HS200 - SDR104 (Since they both are equivalent in functionality)
2463 * HS400 - This involves multiple configurations
2464 * Initially SDR104 - when tuning is required as HS200
2465 * Then when switching to DDR @ 400MHz (HS400) we use
2466 * the vendor specific HC_SELECT_IN to control the mode.
2467 *
2468 * In addition to controlling the modes we also need to select the
2469 * correct input clock for DLL depending on the mode.
2470 *
2471 * HS400 - divided clock (free running MCLK/2)
2472 * All other modes - default (free running MCLK)
2473 */
2474 if (curr_ios.timing == MMC_TIMING_MMC_HS400) {
2475 /* Select the divided clock (free running MCLK/2) */
2476 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2477 & ~CORE_HC_MCLK_SEL_MASK)
2478 | CORE_HC_MCLK_SEL_HS400),
2479 host->ioaddr + CORE_VENDOR_SPEC);
2480 /*
2481 * Select HS400 mode using the HC_SELECT_IN from VENDOR SPEC
2482 * register
2483 */
2484 if (msm_host->tuning_done && !msm_host->calibration_done) {
2485 /*
2486 * Write 0x6 to HC_SELECT_IN and 1 to HC_SELECT_IN_EN
2487 * field in VENDOR_SPEC_FUNC
2488 */
2489 writel_relaxed((readl_relaxed(host->ioaddr + \
2490 CORE_VENDOR_SPEC)
2491 | CORE_HC_SELECT_IN_HS400
2492 | CORE_HC_SELECT_IN_EN),
2493 host->ioaddr + CORE_VENDOR_SPEC);
2494 }
2495 } else {
2496 /* Select the default clock (free running MCLK) */
2497 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2498 & ~CORE_HC_MCLK_SEL_MASK)
2499 | CORE_HC_MCLK_SEL_DFLT),
2500 host->ioaddr + CORE_VENDOR_SPEC);
2501
2502 /*
2503 * Disable HC_SELECT_IN to be able to use the UHS mode select
2504 * configuration from Host Control2 register for all other
2505 * modes.
2506 *
2507 * Write 0 to HC_SELECT_IN and HC_SELECT_IN_EN field
2508 * in VENDOR_SPEC_FUNC
2509 */
2510 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2511 & ~CORE_HC_SELECT_IN_EN
2512 & ~CORE_HC_SELECT_IN_MASK),
2513 host->ioaddr + CORE_VENDOR_SPEC);
2514 }
2515 mb();
2516
Sahitya Tummala00240122013-02-28 19:50:51 +05302517 if (sup_clock != msm_host->clk_rate) {
2518 pr_debug("%s: %s: setting clk rate to %u\n",
2519 mmc_hostname(host->mmc), __func__, sup_clock);
2520 rc = clk_set_rate(msm_host->clk, sup_clock);
2521 if (rc) {
2522 pr_err("%s: %s: Failed to set rate %u for host-clk : %d\n",
2523 mmc_hostname(host->mmc), __func__,
2524 sup_clock, rc);
2525 return;
2526 }
2527 msm_host->clk_rate = sup_clock;
2528 host->clock = clock;
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302529 /*
2530 * Update the bus vote in case of frequency change due to
2531 * clock scaling.
2532 */
2533 sdhci_msm_bus_voting(host, 1);
Sahitya Tummala00240122013-02-28 19:50:51 +05302534 }
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302535}
2536
Sahitya Tummala5e4f9642013-03-21 11:13:25 +05302537static int sdhci_msm_set_uhs_signaling(struct sdhci_host *host,
2538 unsigned int uhs)
2539{
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -07002540 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2541 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala5e4f9642013-03-21 11:13:25 +05302542 u16 ctrl_2;
2543
2544 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2545 /* Select Bus Speed Mode for host */
2546 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -07002547 if (uhs == MMC_TIMING_MMC_HS400)
2548 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2549 else if (uhs == MMC_TIMING_MMC_HS200)
Sahitya Tummala5e4f9642013-03-21 11:13:25 +05302550 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2551 else if (uhs == MMC_TIMING_UHS_SDR12)
2552 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
2553 else if (uhs == MMC_TIMING_UHS_SDR25)
2554 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
2555 else if (uhs == MMC_TIMING_UHS_SDR50)
2556 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
2557 else if (uhs == MMC_TIMING_UHS_SDR104)
2558 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2559 else if (uhs == MMC_TIMING_UHS_DDR50)
2560 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Sahitya Tummala00240122013-02-28 19:50:51 +05302561 /*
2562 * When clock frquency is less than 100MHz, the feedback clock must be
2563 * provided and DLL must not be used so that tuning can be skipped. To
2564 * provide feedback clock, the mode selection can be any value less
2565 * than 3'b011 in bits [2:0] of HOST CONTROL2 register.
2566 */
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -07002567 if (host->clock <= CORE_FREQ_100MHZ) {
2568 if ((uhs == MMC_TIMING_MMC_HS400) ||
2569 (uhs == MMC_TIMING_MMC_HS200) ||
2570 (uhs == MMC_TIMING_UHS_SDR104))
2571 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Sahitya Tummala00240122013-02-28 19:50:51 +05302572
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -07002573 /*
2574 * Make sure DLL is disabled when not required
2575 *
2576 * Write 1 to DLL_RST bit of DLL_CONFIG register
2577 */
2578 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
2579 | CORE_DLL_RST),
2580 host->ioaddr + CORE_DLL_CONFIG);
2581
2582 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
2583 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
2584 | CORE_DLL_PDN),
2585 host->ioaddr + CORE_DLL_CONFIG);
2586 mb();
2587
2588 /*
2589 * The DLL needs to be restored and CDCLP533 recalibrated
2590 * when the clock frequency is set back to 400MHz.
2591 */
2592 msm_host->calibration_done = false;
2593 }
2594
2595 pr_debug("%s: %s-clock:%u uhs mode:%u ctrl_2:0x%x\n",
2596 mmc_hostname(host->mmc), __func__, host->clock, uhs, ctrl_2);
Sahitya Tummala5e4f9642013-03-21 11:13:25 +05302597 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
2598
2599 return 0;
2600}
2601
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002602/*
2603 * sdhci_msm_disable_data_xfer - disable undergoing AHB bus data transfer
2604 *
2605 * Write 0 to bit 0 in MCI_DATA_CTL (offset 0x2C) - clearing TxActive bit by
2606 * access to legacy registers. It will stop current burst and prevent start of
2607 * the next on.
2608 *
2609 * Polling CORE_AHB_DATA_DELAY_US timeout, by reading bit 13:12 until they are 0
2610 * in CORE_SDCC_DEBUG_REG (offset 0x124) will validate that AHB burst was
2611 * completed and a new one didn't start.
2612 *
2613 * Waiting for 4us while AHB finishes descriptors fetch.
2614 */
2615static void sdhci_msm_disable_data_xfer(struct sdhci_host *host)
2616{
2617 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2618 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2619 u32 value;
2620 int ret;
Venkat Gopalakrishnan0a179c82013-06-26 17:56:11 -07002621 u32 version;
2622
2623 version = readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION);
2624 /* Core version 3.1.0 doesn't need this workaround */
2625 if (version == CORE_VERSION_310)
2626 return;
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002627
2628 value = readl_relaxed(msm_host->core_mem + CORE_MCI_DATA_CTRL);
2629 value &= ~(u32)CORE_MCI_DPSM_ENABLE;
2630 writel_relaxed(value, msm_host->core_mem + CORE_MCI_DATA_CTRL);
2631
2632 /* Enable the test bus for device slot */
2633 writel_relaxed(CORE_TESTBUS_ENA | CORE_TESTBUS_SEL2,
2634 msm_host->core_mem + CORE_TESTBUS_CONFIG);
2635
2636 ret = readl_poll_timeout_noirq(msm_host->core_mem
2637 + CORE_SDCC_DEBUG_REG, value,
2638 !(value & CORE_DEBUG_REG_AHB_HTRANS),
2639 CORE_AHB_DATA_DELAY_US, 1);
2640 if (ret) {
2641 pr_err("%s: %s: can't stop ongoing AHB bus access by ADMA\n",
2642 mmc_hostname(host->mmc), __func__);
2643 BUG();
2644 }
2645 /* Disable the test bus for device slot */
2646 value = readl_relaxed(msm_host->core_mem + CORE_TESTBUS_CONFIG);
2647 value &= ~CORE_TESTBUS_ENA;
2648 writel_relaxed(value, msm_host->core_mem + CORE_TESTBUS_CONFIG);
2649
2650 udelay(CORE_AHB_DESC_DELAY_US);
2651}
2652
Asutosh Das33a4ff52012-12-18 16:14:02 +05302653static struct sdhci_ops sdhci_msm_ops = {
Sahitya Tummala5e4f9642013-03-21 11:13:25 +05302654 .set_uhs_signaling = sdhci_msm_set_uhs_signaling,
Asutosh Das33a4ff52012-12-18 16:14:02 +05302655 .check_power_status = sdhci_msm_check_power_status,
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07002656 .execute_tuning = sdhci_msm_execute_tuning,
2657 .toggle_cdr = sdhci_msm_toggle_cdr,
Asutosh Das3781bd82013-01-10 21:11:04 +05302658 .get_max_segments = sdhci_msm_max_segs,
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302659 .set_clock = sdhci_msm_set_clock,
Sahitya Tummala00240122013-02-28 19:50:51 +05302660 .get_min_clock = sdhci_msm_get_min_clock,
2661 .get_max_clock = sdhci_msm_get_max_clock,
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002662 .disable_data_xfer = sdhci_msm_disable_data_xfer,
Asutosh Das6c0804b2013-11-08 12:33:47 +05302663 .enable_controller_clock = sdhci_msm_enable_controller_clock,
Asutosh Das33a4ff52012-12-18 16:14:02 +05302664};
2665
Asutosh Das1369f7b2013-06-03 18:56:52 +05302666static int sdhci_msm_cfg_mpm_pin_wakeup(struct sdhci_host *host, unsigned mode)
2667{
2668 int ret = 0;
2669 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2670 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2671 unsigned int pin = msm_host->pdata->mpm_sdiowakeup_int;
2672
2673 if (!pin)
2674 return 0;
2675
2676 switch (mode) {
2677 case SDC_DAT1_DISABLE:
2678 ret = msm_mpm_enable_pin(pin, 0);
2679 break;
2680 case SDC_DAT1_ENABLE:
2681 ret = msm_mpm_set_pin_type(pin, IRQ_TYPE_LEVEL_LOW);
2682 if (!ret)
2683 ret = msm_mpm_enable_pin(pin, 1);
2684 break;
2685 case SDC_DAT1_ENWAKE:
2686 ret = msm_mpm_set_pin_wake(pin, 1);
2687 break;
2688 case SDC_DAT1_DISWAKE:
2689 ret = msm_mpm_set_pin_wake(pin, 0);
2690 break;
2691 default:
2692 ret = -EINVAL;
2693 break;
2694 }
2695 return ret;
2696}
2697
Asutosh Das33a4ff52012-12-18 16:14:02 +05302698static int __devinit sdhci_msm_probe(struct platform_device *pdev)
2699{
2700 struct sdhci_host *host;
2701 struct sdhci_pltfm_host *pltfm_host;
2702 struct sdhci_msm_host *msm_host;
2703 struct resource *core_memres = NULL;
Asutosh Dasbbc84782013-02-11 15:31:35 +05302704 int ret = 0, dead = 0;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302705 u32 vdd_max_current;
Stephen Boyd3edbd8f2013-04-24 14:19:46 -07002706 u16 host_version;
Subhash Jadavanic08d2062013-05-14 17:46:43 +05302707 u32 pwr, irq_status, irq_ctl;
Asutosh Das1369f7b2013-06-03 18:56:52 +05302708 unsigned long flags;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302709
2710 pr_debug("%s: Enter %s\n", dev_name(&pdev->dev), __func__);
2711 msm_host = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_msm_host),
2712 GFP_KERNEL);
2713 if (!msm_host) {
2714 ret = -ENOMEM;
2715 goto out;
2716 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05302717
2718 msm_host->sdhci_msm_pdata.ops = &sdhci_msm_ops;
2719 host = sdhci_pltfm_init(pdev, &msm_host->sdhci_msm_pdata);
2720 if (IS_ERR(host)) {
2721 ret = PTR_ERR(host);
2722 goto out;
2723 }
2724
2725 pltfm_host = sdhci_priv(host);
2726 pltfm_host->priv = msm_host;
2727 msm_host->mmc = host->mmc;
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302728 msm_host->pdev = pdev;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302729
2730 /* Extract platform data */
2731 if (pdev->dev.of_node) {
Venkat Gopalakrishnanc61ab7e2013-03-11 12:17:57 -07002732 ret = of_alias_get_id(pdev->dev.of_node, "sdhc");
2733 if (ret < 0) {
2734 dev_err(&pdev->dev, "Failed to get slot index %d\n",
2735 ret);
2736 goto pltfm_free;
2737 }
2738 if (disable_slots & (1 << (ret - 1))) {
2739 dev_info(&pdev->dev, "%s: Slot %d disabled\n", __func__,
2740 ret);
2741 ret = -ENODEV;
2742 goto pltfm_free;
2743 }
2744
Asutosh Das33a4ff52012-12-18 16:14:02 +05302745 msm_host->pdata = sdhci_msm_populate_pdata(&pdev->dev);
2746 if (!msm_host->pdata) {
2747 dev_err(&pdev->dev, "DT parsing error\n");
2748 goto pltfm_free;
2749 }
2750 } else {
2751 dev_err(&pdev->dev, "No device tree node\n");
2752 goto pltfm_free;
2753 }
2754
2755 /* Setup Clocks */
2756
2757 /* Setup SDCC bus voter clock. */
2758 msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus_clk");
2759 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
2760 /* Vote for max. clk rate for max. performance */
2761 ret = clk_set_rate(msm_host->bus_clk, INT_MAX);
2762 if (ret)
2763 goto pltfm_free;
2764 ret = clk_prepare_enable(msm_host->bus_clk);
2765 if (ret)
2766 goto pltfm_free;
2767 }
2768
2769 /* Setup main peripheral bus clock */
2770 msm_host->pclk = devm_clk_get(&pdev->dev, "iface_clk");
2771 if (!IS_ERR(msm_host->pclk)) {
2772 ret = clk_prepare_enable(msm_host->pclk);
2773 if (ret)
2774 goto bus_clk_disable;
2775 }
Asutosh Das6c0804b2013-11-08 12:33:47 +05302776 atomic_set(&msm_host->controller_clock, 1);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302777
2778 /* Setup SDC MMC clock */
2779 msm_host->clk = devm_clk_get(&pdev->dev, "core_clk");
2780 if (IS_ERR(msm_host->clk)) {
2781 ret = PTR_ERR(msm_host->clk);
2782 goto pclk_disable;
2783 }
2784
Sahitya Tummala00240122013-02-28 19:50:51 +05302785 /* Set to the minimum supported clock frequency */
2786 ret = clk_set_rate(msm_host->clk, sdhci_msm_get_min_clock(host));
2787 if (ret) {
2788 dev_err(&pdev->dev, "MClk rate set failed (%d)\n", ret);
Sahitya Tummalac954ab02013-06-07 13:03:07 +05302789 goto pclk_disable;
Sahitya Tummala00240122013-02-28 19:50:51 +05302790 }
Sahitya Tummalac954ab02013-06-07 13:03:07 +05302791 ret = clk_prepare_enable(msm_host->clk);
2792 if (ret)
2793 goto pclk_disable;
2794
Sahitya Tummala00240122013-02-28 19:50:51 +05302795 msm_host->clk_rate = sdhci_msm_get_min_clock(host);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302796 atomic_set(&msm_host->clks_on, 1);
Sahitya Tummala00240122013-02-28 19:50:51 +05302797
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -07002798 /* Setup CDC calibration fixed feedback clock */
2799 msm_host->ff_clk = devm_clk_get(&pdev->dev, "cal_clk");
2800 if (!IS_ERR(msm_host->ff_clk)) {
2801 ret = clk_prepare_enable(msm_host->ff_clk);
2802 if (ret)
2803 goto clk_disable;
2804 }
2805
2806 /* Setup CDC calibration sleep clock */
2807 msm_host->sleep_clk = devm_clk_get(&pdev->dev, "sleep_clk");
2808 if (!IS_ERR(msm_host->sleep_clk)) {
2809 ret = clk_prepare_enable(msm_host->sleep_clk);
2810 if (ret)
2811 goto ff_clk_disable;
2812 }
2813
Venkat Gopalakrishnanb6cfa292013-06-12 11:16:37 -07002814 msm_host->saved_tuning_phase = INVALID_TUNING_PHASE;
2815
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302816 ret = sdhci_msm_bus_register(msm_host, pdev);
2817 if (ret)
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -07002818 goto sleep_clk_disable;
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302819
2820 if (msm_host->msm_bus_vote.client_handle)
2821 INIT_DELAYED_WORK(&msm_host->msm_bus_vote.vote_work,
2822 sdhci_msm_bus_work);
2823 sdhci_msm_bus_voting(host, 1);
2824
Asutosh Das33a4ff52012-12-18 16:14:02 +05302825 /* Setup regulators */
2826 ret = sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, true);
2827 if (ret) {
2828 dev_err(&pdev->dev, "Regulator setup failed (%d)\n", ret);
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302829 goto bus_unregister;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302830 }
2831
2832 /* Reset the core and Enable SDHC mode */
2833 core_memres = platform_get_resource_byname(pdev,
2834 IORESOURCE_MEM, "core_mem");
2835 msm_host->core_mem = devm_ioremap(&pdev->dev, core_memres->start,
2836 resource_size(core_memres));
2837
2838 if (!msm_host->core_mem) {
2839 dev_err(&pdev->dev, "Failed to remap registers\n");
2840 ret = -ENOMEM;
2841 goto vreg_deinit;
2842 }
2843
Stepan Moskovchenkoe0938982013-09-13 22:19:33 -07002844 /* Unset HC_MODE_EN bit in HC_MODE register */
2845 writel_relaxed(0, (msm_host->core_mem + CORE_HC_MODE));
2846
Asutosh Das33a4ff52012-12-18 16:14:02 +05302847 /* Set SW_RST bit in POWER register (Offset 0x0) */
Sahitya Tummalad5d76e72013-04-25 11:50:56 +05302848 writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_POWER) |
2849 CORE_SW_RST, msm_host->core_mem + CORE_POWER);
2850 /*
2851 * SW reset can take upto 10HCLK + 15MCLK cycles.
2852 * Calculating based on min clk rates (hclk = 27MHz,
2853 * mclk = 400KHz) it comes to ~40us. Let's poll for
2854 * max. 1ms for reset completion.
2855 */
2856 ret = readl_poll_timeout(msm_host->core_mem + CORE_POWER,
2857 pwr, !(pwr & CORE_SW_RST), 100, 10);
2858
2859 if (ret) {
2860 dev_err(&pdev->dev, "reset failed (%d)\n", ret);
2861 goto vreg_deinit;
2862 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05302863 /* Set HC_MODE_EN bit in HC_MODE register */
2864 writel_relaxed(HC_MODE_EN, (msm_host->core_mem + CORE_HC_MODE));
2865
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -07002866 /* Set FF_CLK_SW_RST_DIS bit in HC_MODE register */
2867 writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_HC_MODE) |
2868 FF_CLK_SW_RST_DIS, msm_host->core_mem + CORE_HC_MODE);
2869
Asutosh Das33a4ff52012-12-18 16:14:02 +05302870 /*
Subhash Jadavanic08d2062013-05-14 17:46:43 +05302871 * CORE_SW_RST above may trigger power irq if previous status of PWRCTL
2872 * was either BUS_ON or IO_HIGH_V. So before we enable the power irq
2873 * interrupt in GIC (by registering the interrupt handler), we need to
2874 * ensure that any pending power irq interrupt status is acknowledged
2875 * otherwise power irq interrupt handler would be fired prematurely.
2876 */
2877 irq_status = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
2878 writel_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
2879 irq_ctl = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_CTL);
2880 if (irq_status & (CORE_PWRCTL_BUS_ON | CORE_PWRCTL_BUS_OFF))
2881 irq_ctl |= CORE_PWRCTL_BUS_SUCCESS;
2882 if (irq_status & (CORE_PWRCTL_IO_HIGH | CORE_PWRCTL_IO_LOW))
2883 irq_ctl |= CORE_PWRCTL_IO_SUCCESS;
2884 writel_relaxed(irq_ctl, (msm_host->core_mem + CORE_PWRCTL_CTL));
2885 /*
2886 * Ensure that above writes are propogated before interrupt enablement
2887 * in GIC.
2888 */
2889 mb();
2890
2891 /*
Asutosh Das33a4ff52012-12-18 16:14:02 +05302892 * Following are the deviations from SDHC spec v3.0 -
2893 * 1. Card detection is handled using separate GPIO.
2894 * 2. Bus power control is handled by interacting with PMIC.
2895 */
2896 host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
2897 host->quirks |= SDHCI_QUIRK_SINGLE_POWER_WRITE;
Sahitya Tummala00240122013-02-28 19:50:51 +05302898 host->quirks |= SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN;
2899 host->quirks2 |= SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK;
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302900 host->quirks2 |= SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING;
Krishna Kondaa20d3362013-04-01 21:01:59 -07002901 host->quirks2 |= SDHCI_QUIRK2_USE_MAX_DISCARD_SIZE;
Sahitya Tummalad2ae8832013-04-12 11:49:11 +05302902 host->quirks2 |= SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD;
Sahitya Tummalae6886bd2013-04-12 12:11:20 +05302903 host->quirks2 |= SDHCI_QUIRK2_BROKEN_PRESET_VALUE;
Sahitya Tummala4d12d0b2013-04-12 11:59:25 +05302904 host->quirks2 |= SDHCI_QUIRK2_USE_RESERVED_MAX_TIMEOUT;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302905
Sahitya Tummalaf667cc12013-06-10 16:32:51 +05302906 if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK)
2907 host->quirks2 |= SDHCI_QUIRK2_DIVIDE_TOUT_BY_4;
2908
Stephen Boyd3edbd8f2013-04-24 14:19:46 -07002909 host_version = readw_relaxed((host->ioaddr + SDHCI_HOST_VERSION));
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002910 dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n",
2911 host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >>
2912 SDHCI_VENDOR_VER_SHIFT));
2913 if (((host_version & SDHCI_VENDOR_VER_MASK) >>
2914 SDHCI_VENDOR_VER_SHIFT) == SDHCI_VER_100) {
2915 /*
2916 * Add 40us delay in interrupt handler when
2917 * operating at initialization frequency(400KHz).
2918 */
2919 host->quirks2 |= SDHCI_QUIRK2_SLOW_INT_CLR;
2920 /*
2921 * Set Software Reset for DAT line in Software
2922 * Reset Register (Bit 2).
2923 */
2924 host->quirks2 |= SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT;
2925 }
2926
Asutosh Dasbefeb732013-06-13 14:27:42 +05302927 host->quirks2 |= SDHCI_QUIRK2_IGN_DATA_END_BIT_ERROR;
2928
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002929 /* Setup PWRCTL irq */
Asutosh Dasbbc84782013-02-11 15:31:35 +05302930 msm_host->pwr_irq = platform_get_irq_byname(pdev, "pwr_irq");
2931 if (msm_host->pwr_irq < 0) {
Asutosh Das33a4ff52012-12-18 16:14:02 +05302932 dev_err(&pdev->dev, "Failed to get pwr_irq by name (%d)\n",
Asutosh Dasbbc84782013-02-11 15:31:35 +05302933 msm_host->pwr_irq);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302934 goto vreg_deinit;
2935 }
Asutosh Dasbbc84782013-02-11 15:31:35 +05302936 ret = devm_request_threaded_irq(&pdev->dev, msm_host->pwr_irq, NULL,
Asutosh Das33a4ff52012-12-18 16:14:02 +05302937 sdhci_msm_pwr_irq, IRQF_ONESHOT,
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07002938 dev_name(&pdev->dev), host);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302939 if (ret) {
2940 dev_err(&pdev->dev, "Request threaded irq(%d) failed (%d)\n",
Asutosh Dasbbc84782013-02-11 15:31:35 +05302941 msm_host->pwr_irq, ret);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302942 goto vreg_deinit;
2943 }
2944
2945 /* Enable pwr irq interrupts */
2946 writel_relaxed(INT_MASK, (msm_host->core_mem + CORE_PWRCTL_MASK));
2947
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302948 /* Set clock gating delay to be used when CONFIG_MMC_CLKGATE is set */
2949 msm_host->mmc->clkgate_delay = SDHCI_MSM_MMC_CLK_GATE_DELAY;
2950
Asutosh Das33a4ff52012-12-18 16:14:02 +05302951 /* Set host capabilities */
2952 msm_host->mmc->caps |= msm_host->pdata->mmc_bus_width;
2953 msm_host->mmc->caps |= msm_host->pdata->caps;
2954
2955 vdd_max_current = sdhci_msm_get_vreg_vdd_max_current(msm_host);
2956 if (vdd_max_current >= 800)
2957 msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_800;
2958 else if (vdd_max_current >= 600)
2959 msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_600;
2960 else if (vdd_max_current >= 400)
2961 msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_400;
2962 else
2963 msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_200;
2964
2965 if (vdd_max_current > 150)
2966 msm_host->mmc->caps |= MMC_CAP_SET_XPC_180 |
2967 MMC_CAP_SET_XPC_300|
2968 MMC_CAP_SET_XPC_330;
2969
2970 msm_host->mmc->caps2 |= msm_host->pdata->caps2;
Asutosh Dasbbc84782013-02-11 15:31:35 +05302971 msm_host->mmc->caps2 |= MMC_CAP2_CORE_RUNTIME_PM;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302972 msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR;
2973 msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR_CONTROL;
2974 msm_host->mmc->caps2 |= (MMC_CAP2_BOOTPART_NOACC |
2975 MMC_CAP2_DETECT_ON_ERR);
2976 msm_host->mmc->caps2 |= MMC_CAP2_SANITIZE;
2977 msm_host->mmc->caps2 |= MMC_CAP2_CACHE_CTRL;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302978 msm_host->mmc->caps2 |= MMC_CAP2_POWEROFF_NOTIFY;
Sahitya Tummala00240122013-02-28 19:50:51 +05302979 msm_host->mmc->caps2 |= MMC_CAP2_CLK_SCALE;
Konstantin Dorfmanfa436d52013-04-17 16:26:11 +03002980 msm_host->mmc->caps2 |= MMC_CAP2_STOP_REQUEST;
Subhash Jadavani61a52c92013-05-29 15:52:10 +05302981 msm_host->mmc->caps2 |= MMC_CAP2_ASYNC_SDIO_IRQ_4BIT_MODE;
Asutosh Das41c35212013-09-19 11:14:27 +05302982 msm_host->mmc->caps2 |= MMC_CAP2_CORE_PM;
Asutosh Das1369f7b2013-06-03 18:56:52 +05302983 msm_host->mmc->pm_caps |= MMC_PM_KEEP_POWER | MMC_PM_WAKE_SDIO_IRQ;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302984
2985 if (msm_host->pdata->nonremovable)
2986 msm_host->mmc->caps |= MMC_CAP_NONREMOVABLE;
2987
Sahitya Tummalab4e84042013-03-10 07:03:17 +05302988 host->cpu_dma_latency_us = msm_host->pdata->cpu_dma_latency_us;
2989
Sahitya Tummala179e7382013-03-20 19:24:01 +05302990 init_completion(&msm_host->pwr_irq_completion);
2991
Sahitya Tummala62448d92013-03-12 14:57:46 +05302992 if (gpio_is_valid(msm_host->pdata->status_gpio)) {
2993 ret = mmc_cd_gpio_request(msm_host->mmc,
2994 msm_host->pdata->status_gpio);
2995 if (ret) {
2996 dev_err(&pdev->dev, "%s: Failed to request card detection IRQ %d\n",
2997 __func__, ret);
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302998 goto vreg_deinit;
Sahitya Tummala62448d92013-03-12 14:57:46 +05302999 }
3000 }
3001
Sahitya Tummala2fa7eb12013-03-20 19:34:59 +05303002 if (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(32))) {
3003 host->dma_mask = DMA_BIT_MASK(32);
3004 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
3005 } else {
3006 dev_err(&pdev->dev, "%s: Failed to set dma mask\n", __func__);
3007 }
3008
Asutosh Das1369f7b2013-06-03 18:56:52 +05303009 msm_host->pdata->sdiowakeup_irq = platform_get_irq_byname(pdev,
3010 "sdiowakeup_irq");
3011 if (msm_host->pdata->sdiowakeup_irq >= 0) {
3012 msm_host->is_sdiowakeup_enabled = true;
3013 ret = request_irq(msm_host->pdata->sdiowakeup_irq,
3014 sdhci_msm_sdiowakeup_irq,
3015 IRQF_SHARED | IRQF_TRIGGER_LOW,
3016 "sdhci-msm sdiowakeup", host);
3017 if (ret) {
3018 dev_err(&pdev->dev, "%s: request sdiowakeup IRQ %d: failed: %d\n",
3019 __func__, msm_host->pdata->sdiowakeup_irq, ret);
3020 msm_host->pdata->sdiowakeup_irq = -1;
3021 msm_host->is_sdiowakeup_enabled = false;
3022 goto free_cd_gpio;
3023 } else {
3024 spin_lock_irqsave(&host->lock, flags);
3025 sdhci_msm_cfg_sdiowakeup_gpio_irq(host, false);
3026 spin_unlock_irqrestore(&host->lock, flags);
3027 }
3028 }
3029
Asutosh Das33a4ff52012-12-18 16:14:02 +05303030 ret = sdhci_add_host(host);
3031 if (ret) {
3032 dev_err(&pdev->dev, "Add host failed (%d)\n", ret);
Sahitya Tummala62448d92013-03-12 14:57:46 +05303033 goto free_cd_gpio;
Asutosh Das33a4ff52012-12-18 16:14:02 +05303034 }
3035
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05303036 msm_host->msm_bus_vote.max_bus_bw.show = show_sdhci_max_bus_bw;
3037 msm_host->msm_bus_vote.max_bus_bw.store = store_sdhci_max_bus_bw;
3038 sysfs_attr_init(&msm_host->msm_bus_vote.max_bus_bw.attr);
3039 msm_host->msm_bus_vote.max_bus_bw.attr.name = "max_bus_bw";
3040 msm_host->msm_bus_vote.max_bus_bw.attr.mode = S_IRUGO | S_IWUSR;
3041 ret = device_create_file(&pdev->dev,
3042 &msm_host->msm_bus_vote.max_bus_bw);
3043 if (ret)
3044 goto remove_host;
3045
Sahitya Tummala3b292c32013-06-20 14:00:18 +05303046 if (!gpio_is_valid(msm_host->pdata->status_gpio)) {
3047 msm_host->polling.show = show_polling;
3048 msm_host->polling.store = store_polling;
3049 sysfs_attr_init(&msm_host->polling.attr);
3050 msm_host->polling.attr.name = "polling";
3051 msm_host->polling.attr.mode = S_IRUGO | S_IWUSR;
3052 ret = device_create_file(&pdev->dev, &msm_host->polling);
3053 if (ret)
3054 goto remove_max_bus_bw_file;
3055 }
Asutosh Dasbbc84782013-02-11 15:31:35 +05303056 ret = pm_runtime_set_active(&pdev->dev);
3057 if (ret)
3058 pr_err("%s: %s: pm_runtime_set_active failed: err: %d\n",
3059 mmc_hostname(host->mmc), __func__, ret);
Asutosh Das41c35212013-09-19 11:14:27 +05303060 else if (mmc_use_core_runtime_pm(host->mmc))
Asutosh Dasbbc84782013-02-11 15:31:35 +05303061 pm_runtime_enable(&pdev->dev);
3062
Asutosh Das1369f7b2013-06-03 18:56:52 +05303063 if (msm_host->pdata->mpm_sdiowakeup_int != -1) {
3064 ret = sdhci_msm_cfg_mpm_pin_wakeup(host, SDC_DAT1_ENABLE);
3065 if (ret) {
3066 pr_err("%s: enabling wakeup: failed: ret: %d\n",
3067 mmc_hostname(host->mmc), ret);
3068 ret = 0;
3069 msm_host->pdata->mpm_sdiowakeup_int = -1;
3070 }
3071 }
3072
Asutosh Das33a4ff52012-12-18 16:14:02 +05303073 /* Successful initialization */
3074 goto out;
3075
Sahitya Tummala3b292c32013-06-20 14:00:18 +05303076remove_max_bus_bw_file:
3077 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Asutosh Das33a4ff52012-12-18 16:14:02 +05303078remove_host:
3079 dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
3080 sdhci_remove_host(host, dead);
Sahitya Tummala62448d92013-03-12 14:57:46 +05303081free_cd_gpio:
3082 if (gpio_is_valid(msm_host->pdata->status_gpio))
3083 mmc_cd_gpio_free(msm_host->mmc);
Asutosh Das1369f7b2013-06-03 18:56:52 +05303084 if (sdhci_is_valid_gpio_wakeup_int(msm_host))
3085 free_irq(msm_host->pdata->sdiowakeup_irq, host);
Asutosh Das33a4ff52012-12-18 16:14:02 +05303086vreg_deinit:
3087 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummalac45ae732013-05-23 15:59:22 +05303088bus_unregister:
3089 if (msm_host->msm_bus_vote.client_handle)
3090 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
3091 sdhci_msm_bus_unregister(msm_host);
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -07003092sleep_clk_disable:
3093 if (!IS_ERR(msm_host->sleep_clk))
3094 clk_disable_unprepare(msm_host->sleep_clk);
3095ff_clk_disable:
3096 if (!IS_ERR(msm_host->ff_clk))
3097 clk_disable_unprepare(msm_host->ff_clk);
Asutosh Das33a4ff52012-12-18 16:14:02 +05303098clk_disable:
3099 if (!IS_ERR(msm_host->clk))
3100 clk_disable_unprepare(msm_host->clk);
3101pclk_disable:
3102 if (!IS_ERR(msm_host->pclk))
3103 clk_disable_unprepare(msm_host->pclk);
3104bus_clk_disable:
3105 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
3106 clk_disable_unprepare(msm_host->bus_clk);
3107pltfm_free:
3108 sdhci_pltfm_free(pdev);
3109out:
3110 pr_debug("%s: Exit %s\n", dev_name(&pdev->dev), __func__);
3111 return ret;
3112}
3113
3114static int __devexit sdhci_msm_remove(struct platform_device *pdev)
3115{
3116 struct sdhci_host *host = platform_get_drvdata(pdev);
3117 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3118 struct sdhci_msm_host *msm_host = pltfm_host->priv;
3119 struct sdhci_msm_pltfm_data *pdata = msm_host->pdata;
3120 int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) ==
3121 0xffffffff);
3122
3123 pr_debug("%s: %s\n", dev_name(&pdev->dev), __func__);
Sahitya Tummala3b292c32013-06-20 14:00:18 +05303124 if (!gpio_is_valid(msm_host->pdata->status_gpio))
3125 device_remove_file(&pdev->dev, &msm_host->polling);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05303126 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Asutosh Das33a4ff52012-12-18 16:14:02 +05303127 sdhci_remove_host(host, dead);
Asutosh Dasbbc84782013-02-11 15:31:35 +05303128 pm_runtime_disable(&pdev->dev);
Asutosh Das33a4ff52012-12-18 16:14:02 +05303129 sdhci_pltfm_free(pdev);
Sahitya Tummala62448d92013-03-12 14:57:46 +05303130
Asutosh Das1369f7b2013-06-03 18:56:52 +05303131 if (sdhci_is_valid_mpm_wakeup_int(msm_host))
3132 sdhci_msm_cfg_mpm_pin_wakeup(host, SDC_DAT1_DISABLE);
3133
3134 if (sdhci_is_valid_gpio_wakeup_int(msm_host))
3135 free_irq(msm_host->pdata->sdiowakeup_irq, host);
3136
Sahitya Tummala62448d92013-03-12 14:57:46 +05303137 if (gpio_is_valid(msm_host->pdata->status_gpio))
3138 mmc_cd_gpio_free(msm_host->mmc);
3139
Asutosh Das33a4ff52012-12-18 16:14:02 +05303140 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05303141
Asutosh Das33a4ff52012-12-18 16:14:02 +05303142 if (pdata->pin_data)
Asutosh Das390519d2012-12-21 12:21:42 +05303143 sdhci_msm_setup_pins(pdata, false);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05303144
3145 if (msm_host->msm_bus_vote.client_handle) {
3146 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
3147 sdhci_msm_bus_unregister(msm_host);
3148 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05303149 return 0;
3150}
3151
Asutosh Das1369f7b2013-06-03 18:56:52 +05303152static int sdhci_msm_cfg_sdio_wakeup(struct sdhci_host *host, bool enable)
3153{
3154 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3155 struct sdhci_msm_host *msm_host = pltfm_host->priv;
3156 unsigned long flags;
3157 int ret = 0;
3158
3159 if (!(host->mmc->card && mmc_card_sdio(host->mmc->card) &&
3160 (sdhci_is_valid_mpm_wakeup_int(msm_host) ||
3161 sdhci_is_valid_gpio_wakeup_int(msm_host)) &&
3162 mmc_card_wake_sdio_irq(host->mmc))) {
3163 return 1;
3164 }
3165
3166 spin_lock_irqsave(&host->lock, flags);
3167 if (enable) {
3168 /* configure DAT1 gpio if applicable */
3169 if (sdhci_is_valid_gpio_wakeup_int(msm_host)) {
3170 ret = enable_irq_wake(msm_host->pdata->sdiowakeup_irq);
3171 if (!ret)
3172 sdhci_msm_cfg_sdiowakeup_gpio_irq(host, true);
3173 goto out;
3174 } else {
3175 ret = sdhci_msm_cfg_mpm_pin_wakeup(host,
3176 SDC_DAT1_ENWAKE);
3177 if (ret)
3178 goto out;
3179 ret = enable_irq_wake(host->irq);
3180 if (ret)
3181 sdhci_msm_cfg_mpm_pin_wakeup(host,
3182 SDC_DAT1_DISWAKE);
3183 }
3184 } else {
3185 if (sdhci_is_valid_gpio_wakeup_int(msm_host)) {
3186 ret = disable_irq_wake(msm_host->pdata->sdiowakeup_irq);
3187 sdhci_msm_cfg_sdiowakeup_gpio_irq(host, false);
3188 } else {
3189 ret = sdhci_msm_cfg_mpm_pin_wakeup(host,
3190 SDC_DAT1_DISWAKE);
3191 if (ret)
3192 goto out;
3193 ret = disable_irq_wake(host->irq);
3194 }
3195 }
3196out:
3197 if (ret)
3198 pr_err("%s: %s: %sable wakeup: failed: %d gpio: %d mpm: %d\n",
3199 mmc_hostname(host->mmc), __func__, enable ? "en" : "dis",
3200 ret, msm_host->pdata->sdiowakeup_irq,
3201 msm_host->pdata->mpm_sdiowakeup_int);
3202 spin_unlock_irqrestore(&host->lock, flags);
3203 return ret;
3204}
3205
Asutosh Dasbbc84782013-02-11 15:31:35 +05303206static int sdhci_msm_runtime_suspend(struct device *dev)
3207{
3208 struct sdhci_host *host = dev_get_drvdata(dev);
3209 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3210 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Das1369f7b2013-06-03 18:56:52 +05303211 int ret;
3212
3213 ret = sdhci_msm_cfg_sdio_wakeup(host, true);
3214 /* pwr_irq is not monitored by mpm on suspend, hence disable it */
3215 if (!ret)
3216 goto skip_disable_host_irq;
Asutosh Dasbbc84782013-02-11 15:31:35 +05303217
3218 disable_irq(host->irq);
Asutosh Das1369f7b2013-06-03 18:56:52 +05303219
3220skip_disable_host_irq:
Asutosh Dasbbc84782013-02-11 15:31:35 +05303221 disable_irq(msm_host->pwr_irq);
3222
Sahitya Tummalac45ae732013-05-23 15:59:22 +05303223 /*
3224 * Remove the vote immediately only if clocks are off in which
3225 * case we might have queued work to remove vote but it may not
3226 * be completed before runtime suspend or system suspend.
3227 */
3228 if (!atomic_read(&msm_host->clks_on)) {
3229 if (msm_host->msm_bus_vote.client_handle)
3230 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
3231 }
3232
Asutosh Dasbbc84782013-02-11 15:31:35 +05303233 return 0;
3234}
3235
3236static int sdhci_msm_runtime_resume(struct device *dev)
3237{
3238 struct sdhci_host *host = dev_get_drvdata(dev);
3239 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3240 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Das1369f7b2013-06-03 18:56:52 +05303241 int ret;
Asutosh Dasbbc84782013-02-11 15:31:35 +05303242
Asutosh Das1369f7b2013-06-03 18:56:52 +05303243 ret = sdhci_msm_cfg_sdio_wakeup(host, false);
3244 if (!ret)
3245 goto skip_enable_host_irq;
3246
Asutosh Dasbbc84782013-02-11 15:31:35 +05303247 enable_irq(host->irq);
3248
Asutosh Das1369f7b2013-06-03 18:56:52 +05303249skip_enable_host_irq:
3250 enable_irq(msm_host->pwr_irq);
3251
Asutosh Dasbbc84782013-02-11 15:31:35 +05303252 return 0;
3253}
3254
3255#ifdef CONFIG_PM_SLEEP
3256
3257static int sdhci_msm_suspend(struct device *dev)
3258{
3259 struct sdhci_host *host = dev_get_drvdata(dev);
Sahitya Tummala62448d92013-03-12 14:57:46 +05303260 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3261 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Dasbbc84782013-02-11 15:31:35 +05303262 int ret = 0;
3263
Sahitya Tummala62448d92013-03-12 14:57:46 +05303264 if (gpio_is_valid(msm_host->pdata->status_gpio))
3265 mmc_cd_gpio_free(msm_host->mmc);
3266
Asutosh Dasbbc84782013-02-11 15:31:35 +05303267 if (pm_runtime_suspended(dev)) {
3268 pr_debug("%s: %s: already runtime suspended\n",
3269 mmc_hostname(host->mmc), __func__);
3270 goto out;
3271 }
3272
3273 return sdhci_msm_runtime_suspend(dev);
3274out:
3275 return ret;
3276}
3277
3278static int sdhci_msm_resume(struct device *dev)
3279{
3280 struct sdhci_host *host = dev_get_drvdata(dev);
Sahitya Tummala62448d92013-03-12 14:57:46 +05303281 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3282 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Dasbbc84782013-02-11 15:31:35 +05303283 int ret = 0;
3284
Sahitya Tummala62448d92013-03-12 14:57:46 +05303285 if (gpio_is_valid(msm_host->pdata->status_gpio)) {
3286 ret = mmc_cd_gpio_request(msm_host->mmc,
3287 msm_host->pdata->status_gpio);
3288 if (ret)
3289 pr_err("%s: %s: Failed to request card detection IRQ %d\n",
3290 mmc_hostname(host->mmc), __func__, ret);
3291 }
3292
Asutosh Dasbbc84782013-02-11 15:31:35 +05303293 if (pm_runtime_suspended(dev)) {
3294 pr_debug("%s: %s: runtime suspended, defer system resume\n",
3295 mmc_hostname(host->mmc), __func__);
3296 goto out;
3297 }
3298
3299 return sdhci_msm_runtime_resume(dev);
3300out:
3301 return ret;
3302}
Asutosh Das1369f7b2013-06-03 18:56:52 +05303303
3304static int sdhci_msm_suspend_noirq(struct device *dev)
3305{
3306 struct sdhci_host *host = dev_get_drvdata(dev);
3307 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3308 struct sdhci_msm_host *msm_host = pltfm_host->priv;
3309 int ret = 0;
3310
3311 /*
3312 * ksdioirqd may get scheduled after sdhc suspend, hence retry
3313 * suspend in case the clocks are ON
3314 */
3315 if (atomic_read(&msm_host->clks_on)) {
3316 pr_warn("%s: %s: clock ON after suspend, aborting suspend\n",
3317 mmc_hostname(host->mmc), __func__);
3318 ret = -EAGAIN;
3319 }
3320
3321 return ret;
3322}
Asutosh Dasbbc84782013-02-11 15:31:35 +05303323#endif
3324
3325#ifdef CONFIG_PM
3326static const struct dev_pm_ops sdhci_msm_pmops = {
3327 SET_SYSTEM_SLEEP_PM_OPS(sdhci_msm_suspend, sdhci_msm_resume)
3328 SET_RUNTIME_PM_OPS(sdhci_msm_runtime_suspend, sdhci_msm_runtime_resume,
3329 NULL)
Asutosh Das1369f7b2013-06-03 18:56:52 +05303330 .suspend_noirq = sdhci_msm_suspend_noirq,
Asutosh Dasbbc84782013-02-11 15:31:35 +05303331};
3332
3333#define SDHCI_MSM_PMOPS (&sdhci_msm_pmops)
3334
3335#else
3336#define SDHCI_PM_OPS NULL
3337#endif
Asutosh Das33a4ff52012-12-18 16:14:02 +05303338static const struct of_device_id sdhci_msm_dt_match[] = {
3339 {.compatible = "qcom,sdhci-msm"},
Sujit Reddy Thummae5594822013-11-28 08:51:19 +05303340 { /* sentinel */ }
Asutosh Das33a4ff52012-12-18 16:14:02 +05303341};
3342MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match);
3343
3344static struct platform_driver sdhci_msm_driver = {
3345 .probe = sdhci_msm_probe,
3346 .remove = __devexit_p(sdhci_msm_remove),
3347 .driver = {
3348 .name = "sdhci_msm",
3349 .owner = THIS_MODULE,
3350 .of_match_table = sdhci_msm_dt_match,
Asutosh Dasbbc84782013-02-11 15:31:35 +05303351 .pm = SDHCI_MSM_PMOPS,
Asutosh Das33a4ff52012-12-18 16:14:02 +05303352 },
3353};
3354
3355module_platform_driver(sdhci_msm_driver);
3356
3357MODULE_DESCRIPTION("Qualcomm Secure Digital Host Controller Interface driver");
3358MODULE_LICENSE("GPL v2");