blob: 232b99b665b6c87d334ca784466e89856f1dfc63 [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 *
5 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
6 *
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>
34#include <linux/mmc/mmc.h>
Asutosh Dasbbc84782013-02-11 15:31:35 +053035#include <linux/pm.h>
36#include <linux/pm_runtime.h>
Sahitya Tummala62448d92013-03-12 14:57:46 +053037#include <linux/mmc/cd-gpio.h>
Sahitya Tummala2fa7eb12013-03-20 19:34:59 +053038#include <linux/dma-mapping.h>
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -070039#include <mach/gpio.h>
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +053040#include <mach/msm_bus.h>
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +030041#include <linux/iopoll.h>
Asutosh Das33a4ff52012-12-18 16:14:02 +053042
43#include "sdhci-pltfm.h"
44
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -070045#define SDHCI_VER_100 0x2B
Asutosh Das33a4ff52012-12-18 16:14:02 +053046#define CORE_HC_MODE 0x78
47#define HC_MODE_EN 0x1
48
49#define CORE_POWER 0x0
50#define CORE_SW_RST (1 << 7)
51
52#define CORE_PWRCTL_STATUS 0xDC
53#define CORE_PWRCTL_MASK 0xE0
54#define CORE_PWRCTL_CLEAR 0xE4
55#define CORE_PWRCTL_CTL 0xE8
56
57#define CORE_PWRCTL_BUS_OFF 0x01
58#define CORE_PWRCTL_BUS_ON (1 << 1)
59#define CORE_PWRCTL_IO_LOW (1 << 2)
60#define CORE_PWRCTL_IO_HIGH (1 << 3)
61
62#define CORE_PWRCTL_BUS_SUCCESS 0x01
63#define CORE_PWRCTL_BUS_FAIL (1 << 1)
64#define CORE_PWRCTL_IO_SUCCESS (1 << 2)
65#define CORE_PWRCTL_IO_FAIL (1 << 3)
66
67#define INT_MASK 0xF
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -070068#define MAX_PHASES 16
69
70#define CORE_DLL_LOCK (1 << 7)
71#define CORE_DLL_EN (1 << 16)
72#define CORE_CDR_EN (1 << 17)
73#define CORE_CK_OUT_EN (1 << 18)
74#define CORE_CDR_EXT_EN (1 << 19)
75#define CORE_DLL_PDN (1 << 29)
76#define CORE_DLL_RST (1 << 30)
77#define CORE_DLL_CONFIG 0x100
78#define CORE_DLL_TEST_CTL 0x104
79#define CORE_DLL_STATUS 0x108
80
81#define CORE_VENDOR_SPEC 0x10C
82#define CORE_CLK_PWRSAVE (1 << 1)
83#define CORE_IO_PAD_PWR_SWITCH (1 << 16)
84
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +030085#define CORE_MCI_DATA_CTRL 0x2C
86#define CORE_MCI_DPSM_ENABLE (1 << 0)
87
88#define CORE_TESTBUS_CONFIG 0x0CC
89#define CORE_TESTBUS_ENA (1 << 3)
90#define CORE_TESTBUS_SEL2 (1 << 4)
91
92/*
93 * Waiting until end of potential AHB access for data:
94 * 16 AHB cycles (160ns for 100MHz and 320ns for 50MHz) +
95 * delay on AHB (2us) = maximum 2.32us
96 * Taking x10 times margin
97 */
98#define CORE_AHB_DATA_DELAY_US 23
99/* Waiting until end of potential AHB access for descriptor:
100 * Single (1 AHB cycle) + delay on AHB bus = max 2us
101 * INCR4 (4 AHB cycles) + delay on AHB bus = max 2us
102 * Single (1 AHB cycle) + delay on AHB bus = max 2us
103 * Total 8 us delay with margin
104 */
105#define CORE_AHB_DESC_DELAY_US 8
106
107#define CORE_SDCC_DEBUG_REG 0x124
108#define CORE_DEBUG_REG_AHB_HTRANS (3 << 12)
109
Asutosh Das3781bd82013-01-10 21:11:04 +0530110/* 8KB descriptors */
111#define SDHCI_MSM_MAX_SEGMENTS (1 << 13)
Sahitya Tummala04c3a462013-01-11 11:30:45 +0530112#define SDHCI_MSM_MMC_CLK_GATE_DELAY 200 /* msecs */
Asutosh Das3781bd82013-01-10 21:11:04 +0530113
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700114static const u32 tuning_block_64[] = {
115 0x00FF0FFF, 0xCCC3CCFF, 0xFFCC3CC3, 0xEFFEFFFE,
116 0xDDFFDFFF, 0xFBFFFBFF, 0xFF7FFFBF, 0xEFBDF777,
117 0xF0FFF0FF, 0x3CCCFC0F, 0xCFCC33CC, 0xEEFFEFFF,
118 0xFDFFFDFF, 0xFFBFFFDF, 0xFFF7FFBB, 0xDE7B7FF7
119};
120
121static const u32 tuning_block_128[] = {
122 0xFF00FFFF, 0x0000FFFF, 0xCCCCFFFF, 0xCCCC33CC,
123 0xCC3333CC, 0xFFFFCCCC, 0xFFFFEEFF, 0xFFEEEEFF,
124 0xFFDDFFFF, 0xDDDDFFFF, 0xBBFFFFFF, 0xBBFFFFFF,
125 0xFFFFFFBB, 0xFFFFFF77, 0x77FF7777, 0xFFEEDDBB,
126 0x00FFFFFF, 0x00FFFFFF, 0xCCFFFF00, 0xCC33CCCC,
127 0x3333CCCC, 0xFFCCCCCC, 0xFFEEFFFF, 0xEEEEFFFF,
128 0xDDFFFFFF, 0xDDFFFFFF, 0xFFFFFFDD, 0xFFFFFFBB,
129 0xFFFFBBBB, 0xFFFF77FF, 0xFF7777FF, 0xEEDDBB77
130};
Asutosh Das33a4ff52012-12-18 16:14:02 +0530131
Venkat Gopalakrishnanc61ab7e2013-03-11 12:17:57 -0700132static int disable_slots;
133/* root can write, others read */
134module_param(disable_slots, int, S_IRUGO|S_IWUSR);
135
Asutosh Das33a4ff52012-12-18 16:14:02 +0530136/* This structure keeps information per regulator */
137struct sdhci_msm_reg_data {
138 /* voltage regulator handle */
139 struct regulator *reg;
140 /* regulator name */
141 const char *name;
142 /* voltage level to be set */
143 u32 low_vol_level;
144 u32 high_vol_level;
145 /* Load values for low power and high power mode */
146 u32 lpm_uA;
147 u32 hpm_uA;
148
149 /* is this regulator enabled? */
150 bool is_enabled;
151 /* is this regulator needs to be always on? */
152 bool is_always_on;
153 /* is low power mode setting required for this regulator? */
154 bool lpm_sup;
155};
156
157/*
158 * This structure keeps information for all the
159 * regulators required for a SDCC slot.
160 */
161struct sdhci_msm_slot_reg_data {
162 /* keeps VDD/VCC regulator info */
163 struct sdhci_msm_reg_data *vdd_data;
164 /* keeps VDD IO regulator info */
165 struct sdhci_msm_reg_data *vdd_io_data;
166};
167
168struct sdhci_msm_gpio {
169 u32 no;
170 const char *name;
171 bool is_enabled;
172};
173
174struct sdhci_msm_gpio_data {
175 struct sdhci_msm_gpio *gpio;
176 u8 size;
177};
178
Asutosh Das390519d2012-12-21 12:21:42 +0530179struct sdhci_msm_pad_pull {
180 enum msm_tlmm_pull_tgt no;
181 u32 val;
182};
183
184struct sdhci_msm_pad_pull_data {
185 struct sdhci_msm_pad_pull *on;
186 struct sdhci_msm_pad_pull *off;
187 u8 size;
188};
189
190struct sdhci_msm_pad_drv {
191 enum msm_tlmm_hdrive_tgt no;
192 u32 val;
193};
194
195struct sdhci_msm_pad_drv_data {
196 struct sdhci_msm_pad_drv *on;
197 struct sdhci_msm_pad_drv *off;
198 u8 size;
199};
200
201struct sdhci_msm_pad_data {
202 struct sdhci_msm_pad_pull_data *pull;
203 struct sdhci_msm_pad_drv_data *drv;
204};
205
206
Asutosh Das33a4ff52012-12-18 16:14:02 +0530207struct sdhci_msm_pin_data {
208 /*
209 * = 1 if controller pins are using gpios
210 * = 0 if controller has dedicated MSM pads
211 */
Asutosh Das390519d2012-12-21 12:21:42 +0530212 u8 is_gpio;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530213 bool cfg_sts;
214 struct sdhci_msm_gpio_data *gpio_data;
Asutosh Das390519d2012-12-21 12:21:42 +0530215 struct sdhci_msm_pad_data *pad_data;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530216};
217
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +0530218struct sdhci_msm_bus_voting_data {
219 struct msm_bus_scale_pdata *bus_pdata;
220 unsigned int *bw_vecs;
221 unsigned int bw_vecs_size;
222};
223
Asutosh Das33a4ff52012-12-18 16:14:02 +0530224struct sdhci_msm_pltfm_data {
225 /* Supported UHS-I Modes */
226 u32 caps;
227
228 /* More capabilities */
229 u32 caps2;
230
231 unsigned long mmc_bus_width;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530232 struct sdhci_msm_slot_reg_data *vreg_data;
233 bool nonremovable;
234 struct sdhci_msm_pin_data *pin_data;
Sahitya Tummalab4e84042013-03-10 07:03:17 +0530235 u32 cpu_dma_latency_us;
Sahitya Tummala62448d92013-03-12 14:57:46 +0530236 int status_gpio; /* card detection GPIO that is configured as IRQ */
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +0530237 struct sdhci_msm_bus_voting_data *voting_data;
Sahitya Tummala00240122013-02-28 19:50:51 +0530238 u32 *sup_clk_table;
239 unsigned char sup_clk_cnt;
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +0530240};
241
242struct sdhci_msm_bus_vote {
243 uint32_t client_handle;
244 uint32_t curr_vote;
245 int min_bw_vote;
246 int max_bw_vote;
247 bool is_max_bw_needed;
248 struct delayed_work vote_work;
249 struct device_attribute max_bus_bw;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530250};
251
252struct sdhci_msm_host {
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +0530253 struct platform_device *pdev;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530254 void __iomem *core_mem; /* MSM SDCC mapped address */
Asutosh Dasbbc84782013-02-11 15:31:35 +0530255 int pwr_irq; /* power irq */
Asutosh Das33a4ff52012-12-18 16:14:02 +0530256 struct clk *clk; /* main SD/MMC bus clock */
257 struct clk *pclk; /* SDHC peripheral bus clock */
258 struct clk *bus_clk; /* SDHC bus voter clock */
Sahitya Tummala04c3a462013-01-11 11:30:45 +0530259 atomic_t clks_on; /* Set if clocks are enabled */
Asutosh Das33a4ff52012-12-18 16:14:02 +0530260 struct sdhci_msm_pltfm_data *pdata;
261 struct mmc_host *mmc;
262 struct sdhci_pltfm_data sdhci_msm_pdata;
Sahitya Tummala179e7382013-03-20 19:24:01 +0530263 u32 curr_pwr_state;
264 u32 curr_io_level;
265 struct completion pwr_irq_completion;
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +0530266 struct sdhci_msm_bus_vote msm_bus_vote;
Sahitya Tummala00240122013-02-28 19:50:51 +0530267 u32 clk_rate; /* Keeps track of current clock rate that is set */
Asutosh Das33a4ff52012-12-18 16:14:02 +0530268};
269
270enum vdd_io_level {
271 /* set vdd_io_data->low_vol_level */
272 VDD_IO_LOW,
273 /* set vdd_io_data->high_vol_level */
274 VDD_IO_HIGH,
275 /*
276 * set whatever there in voltage_level (third argument) of
277 * sdhci_msm_set_vdd_io_vol() function.
278 */
279 VDD_IO_SET_LEVEL,
280};
281
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700282/* MSM platform specific tuning */
283static inline int msm_dll_poll_ck_out_en(struct sdhci_host *host,
284 u8 poll)
285{
286 int rc = 0;
287 u32 wait_cnt = 50;
288 u8 ck_out_en = 0;
289 struct mmc_host *mmc = host->mmc;
290
291 /* poll for CK_OUT_EN bit. max. poll time = 50us */
292 ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) &
293 CORE_CK_OUT_EN);
294
295 while (ck_out_en != poll) {
296 if (--wait_cnt == 0) {
297 pr_err("%s: %s: CK_OUT_EN bit is not %d\n",
298 mmc_hostname(mmc), __func__, poll);
299 rc = -ETIMEDOUT;
300 goto out;
301 }
302 udelay(1);
303
304 ck_out_en = !!(readl_relaxed(host->ioaddr +
305 CORE_DLL_CONFIG) & CORE_CK_OUT_EN);
306 }
307out:
308 return rc;
309}
310
311static int msm_config_cm_dll_phase(struct sdhci_host *host, u8 phase)
312{
313 int rc = 0;
314 u8 grey_coded_phase_table[] = {0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4,
315 0xC, 0xD, 0xF, 0xE, 0xA, 0xB, 0x9,
316 0x8};
317 unsigned long flags;
318 u32 config;
319 struct mmc_host *mmc = host->mmc;
320
321 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
322 spin_lock_irqsave(&host->lock, flags);
323
324 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
325 config &= ~(CORE_CDR_EN | CORE_CK_OUT_EN);
326 config |= (CORE_CDR_EXT_EN | CORE_DLL_EN);
327 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
328
329 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '0' */
330 rc = msm_dll_poll_ck_out_en(host, 0);
331 if (rc)
332 goto err_out;
333
334 /*
335 * Write the selected DLL clock output phase (0 ... 15)
336 * to CDR_SELEXT bit field of DLL_CONFIG register.
337 */
338 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
339 & ~(0xF << 20))
340 | (grey_coded_phase_table[phase] << 20)),
341 host->ioaddr + CORE_DLL_CONFIG);
342
343 /* Set CK_OUT_EN bit of DLL_CONFIG register to 1. */
344 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
345 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
346
347 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '1' */
348 rc = msm_dll_poll_ck_out_en(host, 1);
349 if (rc)
350 goto err_out;
351
352 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
353 config |= CORE_CDR_EN;
354 config &= ~CORE_CDR_EXT_EN;
355 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
356 goto out;
357
358err_out:
359 pr_err("%s: %s: Failed to set DLL phase: %d\n",
360 mmc_hostname(mmc), __func__, phase);
361out:
362 spin_unlock_irqrestore(&host->lock, flags);
363 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
364 return rc;
365}
366
367/*
368 * Find out the greatest range of consecuitive selected
369 * DLL clock output phases that can be used as sampling
370 * setting for SD3.0 UHS-I card read operation (in SDR104
371 * timing mode) or for eMMC4.5 card read operation (in HS200
372 * timing mode).
373 * Select the 3/4 of the range and configure the DLL with the
374 * selected DLL clock output phase.
375 */
376
377static int msm_find_most_appropriate_phase(struct sdhci_host *host,
378 u8 *phase_table, u8 total_phases)
379{
380 int ret;
381 u8 ranges[MAX_PHASES][MAX_PHASES] = { {0}, {0} };
382 u8 phases_per_row[MAX_PHASES] = {0};
383 int row_index = 0, col_index = 0, selected_row_index = 0, curr_max = 0;
384 int i, cnt, phase_0_raw_index = 0, phase_15_raw_index = 0;
385 bool phase_0_found = false, phase_15_found = false;
386 struct mmc_host *mmc = host->mmc;
387
388 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
389 if (!total_phases || (total_phases > MAX_PHASES)) {
390 pr_err("%s: %s: invalid argument: total_phases=%d\n",
391 mmc_hostname(mmc), __func__, total_phases);
392 return -EINVAL;
393 }
394
395 for (cnt = 0; cnt < total_phases; cnt++) {
396 ranges[row_index][col_index] = phase_table[cnt];
397 phases_per_row[row_index] += 1;
398 col_index++;
399
400 if ((cnt + 1) == total_phases) {
401 continue;
402 /* check if next phase in phase_table is consecutive or not */
403 } else if ((phase_table[cnt] + 1) != phase_table[cnt + 1]) {
404 row_index++;
405 col_index = 0;
406 }
407 }
408
409 if (row_index >= MAX_PHASES)
410 return -EINVAL;
411
412 /* Check if phase-0 is present in first valid window? */
413 if (!ranges[0][0]) {
414 phase_0_found = true;
415 phase_0_raw_index = 0;
416 /* Check if cycle exist between 2 valid windows */
417 for (cnt = 1; cnt <= row_index; cnt++) {
418 if (phases_per_row[cnt]) {
419 for (i = 0; i < phases_per_row[cnt]; i++) {
420 if (ranges[cnt][i] == 15) {
421 phase_15_found = true;
422 phase_15_raw_index = cnt;
423 break;
424 }
425 }
426 }
427 }
428 }
429
430 /* If 2 valid windows form cycle then merge them as single window */
431 if (phase_0_found && phase_15_found) {
432 /* number of phases in raw where phase 0 is present */
433 u8 phases_0 = phases_per_row[phase_0_raw_index];
434 /* number of phases in raw where phase 15 is present */
435 u8 phases_15 = phases_per_row[phase_15_raw_index];
436
437 if (phases_0 + phases_15 >= MAX_PHASES)
438 /*
439 * If there are more than 1 phase windows then total
440 * number of phases in both the windows should not be
441 * more than or equal to MAX_PHASES.
442 */
443 return -EINVAL;
444
445 /* Merge 2 cyclic windows */
446 i = phases_15;
447 for (cnt = 0; cnt < phases_0; cnt++) {
448 ranges[phase_15_raw_index][i] =
449 ranges[phase_0_raw_index][cnt];
450 if (++i >= MAX_PHASES)
451 break;
452 }
453
454 phases_per_row[phase_0_raw_index] = 0;
455 phases_per_row[phase_15_raw_index] = phases_15 + phases_0;
456 }
457
458 for (cnt = 0; cnt <= row_index; cnt++) {
459 if (phases_per_row[cnt] > curr_max) {
460 curr_max = phases_per_row[cnt];
461 selected_row_index = cnt;
462 }
463 }
464
465 i = ((curr_max * 3) / 4);
466 if (i)
467 i--;
468
469 ret = (int)ranges[selected_row_index][i];
470
471 if (ret >= MAX_PHASES) {
472 ret = -EINVAL;
473 pr_err("%s: %s: invalid phase selected=%d\n",
474 mmc_hostname(mmc), __func__, ret);
475 }
476
477 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
478 return ret;
479}
480
481static inline void msm_cm_dll_set_freq(struct sdhci_host *host)
482{
483 u32 mclk_freq = 0;
484
485 /* Program the MCLK value to MCLK_FREQ bit field */
486 if (host->clock <= 112000000)
487 mclk_freq = 0;
488 else if (host->clock <= 125000000)
489 mclk_freq = 1;
490 else if (host->clock <= 137000000)
491 mclk_freq = 2;
492 else if (host->clock <= 150000000)
493 mclk_freq = 3;
494 else if (host->clock <= 162000000)
495 mclk_freq = 4;
496 else if (host->clock <= 175000000)
497 mclk_freq = 5;
498 else if (host->clock <= 187000000)
499 mclk_freq = 6;
500 else if (host->clock <= 200000000)
501 mclk_freq = 7;
502
503 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
504 & ~(7 << 24)) | (mclk_freq << 24)),
505 host->ioaddr + CORE_DLL_CONFIG);
506}
507
508/* Initialize the DLL (Programmable Delay Line ) */
509static int msm_init_cm_dll(struct sdhci_host *host)
510{
511 struct mmc_host *mmc = host->mmc;
512 int rc = 0;
513 unsigned long flags;
514 u32 wait_cnt;
515
516 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
517 spin_lock_irqsave(&host->lock, flags);
518
519 /*
520 * Make sure that clock is always enabled when DLL
521 * tuning is in progress. Keeping PWRSAVE ON may
522 * turn off the clock. So let's disable the PWRSAVE
523 * here and re-enable it once tuning is completed.
524 */
525 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
526 & ~CORE_CLK_PWRSAVE),
527 host->ioaddr + CORE_VENDOR_SPEC);
528
529 /* Write 1 to DLL_RST bit of DLL_CONFIG register */
530 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
531 | CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
532
533 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
534 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
535 | CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
536 msm_cm_dll_set_freq(host);
537
538 /* Write 0 to DLL_RST bit of DLL_CONFIG register */
539 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
540 & ~CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
541
542 /* Write 0 to DLL_PDN bit of DLL_CONFIG register */
543 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
544 & ~CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
545
546 /* Set DLL_EN bit to 1. */
547 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
548 | CORE_DLL_EN), host->ioaddr + CORE_DLL_CONFIG);
549
550 /* Set CK_OUT_EN bit to 1. */
551 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
552 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
553
554 wait_cnt = 50;
555 /* Wait until DLL_LOCK bit of DLL_STATUS register becomes '1' */
556 while (!(readl_relaxed(host->ioaddr + CORE_DLL_STATUS) &
557 CORE_DLL_LOCK)) {
558 /* max. wait for 50us sec for LOCK bit to be set */
559 if (--wait_cnt == 0) {
560 pr_err("%s: %s: DLL failed to LOCK\n",
561 mmc_hostname(mmc), __func__);
562 rc = -ETIMEDOUT;
563 goto out;
564 }
565 /* wait for 1us before polling again */
566 udelay(1);
567 }
568
569out:
570 /* re-enable PWRSAVE */
571 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
572 CORE_CLK_PWRSAVE),
573 host->ioaddr + CORE_VENDOR_SPEC);
574 spin_unlock_irqrestore(&host->lock, flags);
575 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
576 return rc;
577}
578
579int sdhci_msm_execute_tuning(struct sdhci_host *host, u32 opcode)
580{
581 unsigned long flags;
582 u8 phase, *data_buf, tuned_phases[16], tuned_phase_cnt = 0;
583 const u32 *tuning_block_pattern = tuning_block_64;
584 int size = sizeof(tuning_block_64); /* Tuning pattern size in bytes */
585 int rc;
586 struct mmc_host *mmc = host->mmc;
Sahitya Tummala00240122013-02-28 19:50:51 +0530587 struct mmc_ios ios = host->mmc->ios;
588
589 /*
590 * Tuning is required for SDR104 and HS200 cards and if clock frequency
591 * is greater than 100MHz in these modes.
592 */
593 if (host->clock <= (100 * 1000 * 1000) ||
594 !(ios.timing == MMC_TIMING_MMC_HS200 ||
595 ios.timing == MMC_TIMING_UHS_SDR104))
596 return 0;
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700597
598 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700599 spin_lock_irqsave(&host->lock, flags);
600
601 if ((opcode == MMC_SEND_TUNING_BLOCK_HS200) &&
602 (mmc->ios.bus_width == MMC_BUS_WIDTH_8)) {
603 tuning_block_pattern = tuning_block_128;
604 size = sizeof(tuning_block_128);
605 }
606 spin_unlock_irqrestore(&host->lock, flags);
607
608 /* first of all reset the tuning block */
609 rc = msm_init_cm_dll(host);
610 if (rc)
611 goto out;
612
613 data_buf = kmalloc(size, GFP_KERNEL);
614 if (!data_buf) {
615 rc = -ENOMEM;
616 goto out;
617 }
618
619 phase = 0;
620 do {
621 struct mmc_command cmd = {0};
622 struct mmc_data data = {0};
623 struct mmc_request mrq = {
624 .cmd = &cmd,
625 .data = &data
626 };
627 struct scatterlist sg;
628
629 /* set the phase in delay line hw block */
630 rc = msm_config_cm_dll_phase(host, phase);
631 if (rc)
632 goto kfree;
633
634 cmd.opcode = opcode;
635 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
636
637 data.blksz = size;
638 data.blocks = 1;
639 data.flags = MMC_DATA_READ;
640 data.timeout_ns = 1000 * 1000 * 1000; /* 1 sec */
641
642 data.sg = &sg;
643 data.sg_len = 1;
644 sg_init_one(&sg, data_buf, size);
645 memset(data_buf, 0, size);
646 mmc_wait_for_req(mmc, &mrq);
647
648 if (!cmd.error && !data.error &&
649 !memcmp(data_buf, tuning_block_pattern, size)) {
650 /* tuning is successful at this tuning point */
651 tuned_phases[tuned_phase_cnt++] = phase;
652 pr_debug("%s: %s: found good phase = %d\n",
653 mmc_hostname(mmc), __func__, phase);
654 }
655 } while (++phase < 16);
656
657 if (tuned_phase_cnt) {
658 rc = msm_find_most_appropriate_phase(host, tuned_phases,
659 tuned_phase_cnt);
660 if (rc < 0)
661 goto kfree;
662 else
663 phase = (u8)rc;
664
665 /*
666 * Finally set the selected phase in delay
667 * line hw block.
668 */
669 rc = msm_config_cm_dll_phase(host, phase);
670 if (rc)
671 goto kfree;
672 pr_debug("%s: %s: finally setting the tuning phase to %d\n",
673 mmc_hostname(mmc), __func__, phase);
674 } else {
675 /* tuning failed */
676 pr_err("%s: %s: no tuning point found\n",
677 mmc_hostname(mmc), __func__);
678 rc = -EAGAIN;
679 }
680
681kfree:
682 kfree(data_buf);
683out:
684 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
685 return rc;
686}
687
Asutosh Das33a4ff52012-12-18 16:14:02 +0530688static int sdhci_msm_setup_gpio(struct sdhci_msm_pltfm_data *pdata, bool enable)
689{
690 struct sdhci_msm_gpio_data *curr;
691 int i, ret = 0;
692
693 curr = pdata->pin_data->gpio_data;
694 for (i = 0; i < curr->size; i++) {
695 if (!gpio_is_valid(curr->gpio[i].no)) {
696 ret = -EINVAL;
697 pr_err("%s: Invalid gpio = %d\n", __func__,
698 curr->gpio[i].no);
699 goto free_gpios;
700 }
701 if (enable) {
702 ret = gpio_request(curr->gpio[i].no,
703 curr->gpio[i].name);
704 if (ret) {
705 pr_err("%s: gpio_request(%d, %s) failed %d\n",
706 __func__, curr->gpio[i].no,
707 curr->gpio[i].name, ret);
708 goto free_gpios;
709 }
710 curr->gpio[i].is_enabled = true;
711 } else {
712 gpio_free(curr->gpio[i].no);
713 curr->gpio[i].is_enabled = false;
714 }
715 }
716 return ret;
717
718free_gpios:
719 for (i--; i >= 0; i--) {
720 gpio_free(curr->gpio[i].no);
721 curr->gpio[i].is_enabled = false;
722 }
723 return ret;
724}
725
Asutosh Das390519d2012-12-21 12:21:42 +0530726static int sdhci_msm_setup_pad(struct sdhci_msm_pltfm_data *pdata, bool enable)
727{
728 struct sdhci_msm_pad_data *curr;
729 int i;
730
731 curr = pdata->pin_data->pad_data;
732 for (i = 0; i < curr->drv->size; i++) {
733 if (enable)
734 msm_tlmm_set_hdrive(curr->drv->on[i].no,
735 curr->drv->on[i].val);
736 else
737 msm_tlmm_set_hdrive(curr->drv->off[i].no,
738 curr->drv->off[i].val);
739 }
740
741 for (i = 0; i < curr->pull->size; i++) {
742 if (enable)
743 msm_tlmm_set_pull(curr->pull->on[i].no,
744 curr->pull->on[i].val);
745 else
746 msm_tlmm_set_pull(curr->pull->off[i].no,
747 curr->pull->off[i].val);
748 }
749
750 return 0;
751}
752
Asutosh Das33a4ff52012-12-18 16:14:02 +0530753static int sdhci_msm_setup_pins(struct sdhci_msm_pltfm_data *pdata, bool enable)
754{
755 int ret = 0;
756
757 if (!pdata->pin_data || (pdata->pin_data->cfg_sts == enable))
758 return 0;
Asutosh Das390519d2012-12-21 12:21:42 +0530759 if (pdata->pin_data->is_gpio)
760 ret = sdhci_msm_setup_gpio(pdata, enable);
761 else
762 ret = sdhci_msm_setup_pad(pdata, enable);
Asutosh Das33a4ff52012-12-18 16:14:02 +0530763
Asutosh Das33a4ff52012-12-18 16:14:02 +0530764 if (!ret)
765 pdata->pin_data->cfg_sts = enable;
766
767 return ret;
768}
769
Asutosh Das390519d2012-12-21 12:21:42 +0530770static int sdhci_msm_dt_get_array(struct device *dev, const char *prop_name,
771 u32 **out, int *len, u32 size)
772{
773 int ret = 0;
774 struct device_node *np = dev->of_node;
775 size_t sz;
776 u32 *arr = NULL;
777
778 if (!of_get_property(np, prop_name, len)) {
779 ret = -EINVAL;
780 goto out;
781 }
782 sz = *len = *len / sizeof(*arr);
783 if (sz <= 0 || (size > 0 && (sz != size))) {
784 dev_err(dev, "%s invalid size\n", prop_name);
785 ret = -EINVAL;
786 goto out;
787 }
788
789 arr = devm_kzalloc(dev, sz * sizeof(*arr), GFP_KERNEL);
790 if (!arr) {
791 dev_err(dev, "%s failed allocating memory\n", prop_name);
792 ret = -ENOMEM;
793 goto out;
794 }
795
796 ret = of_property_read_u32_array(np, prop_name, arr, sz);
797 if (ret < 0) {
798 dev_err(dev, "%s failed reading array %d\n", prop_name, ret);
799 goto out;
800 }
801 *out = arr;
802out:
803 if (ret)
804 *len = 0;
805 return ret;
806}
807
Asutosh Das33a4ff52012-12-18 16:14:02 +0530808#define MAX_PROP_SIZE 32
809static int sdhci_msm_dt_parse_vreg_info(struct device *dev,
810 struct sdhci_msm_reg_data **vreg_data, const char *vreg_name)
811{
812 int len, ret = 0;
813 const __be32 *prop;
814 char prop_name[MAX_PROP_SIZE];
815 struct sdhci_msm_reg_data *vreg;
816 struct device_node *np = dev->of_node;
817
818 snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", vreg_name);
819 if (!of_parse_phandle(np, prop_name, 0)) {
820 dev_err(dev, "No vreg data found for %s\n", vreg_name);
821 ret = -EINVAL;
822 return ret;
823 }
824
825 vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
826 if (!vreg) {
827 dev_err(dev, "No memory for vreg: %s\n", vreg_name);
828 ret = -ENOMEM;
829 return ret;
830 }
831
832 vreg->name = vreg_name;
833
834 snprintf(prop_name, MAX_PROP_SIZE,
835 "qcom,%s-always-on", vreg_name);
836 if (of_get_property(np, prop_name, NULL))
837 vreg->is_always_on = true;
838
839 snprintf(prop_name, MAX_PROP_SIZE,
840 "qcom,%s-lpm-sup", vreg_name);
841 if (of_get_property(np, prop_name, NULL))
842 vreg->lpm_sup = true;
843
844 snprintf(prop_name, MAX_PROP_SIZE,
845 "qcom,%s-voltage-level", vreg_name);
846 prop = of_get_property(np, prop_name, &len);
847 if (!prop || (len != (2 * sizeof(__be32)))) {
848 dev_warn(dev, "%s %s property\n",
849 prop ? "invalid format" : "no", prop_name);
850 } else {
851 vreg->low_vol_level = be32_to_cpup(&prop[0]);
852 vreg->high_vol_level = be32_to_cpup(&prop[1]);
853 }
854
855 snprintf(prop_name, MAX_PROP_SIZE,
856 "qcom,%s-current-level", vreg_name);
857 prop = of_get_property(np, prop_name, &len);
858 if (!prop || (len != (2 * sizeof(__be32)))) {
859 dev_warn(dev, "%s %s property\n",
860 prop ? "invalid format" : "no", prop_name);
861 } else {
862 vreg->lpm_uA = be32_to_cpup(&prop[0]);
863 vreg->hpm_uA = be32_to_cpup(&prop[1]);
864 }
865
866 *vreg_data = vreg;
867 dev_dbg(dev, "%s: %s %s vol=[%d %d]uV, curr=[%d %d]uA\n",
868 vreg->name, vreg->is_always_on ? "always_on," : "",
869 vreg->lpm_sup ? "lpm_sup," : "", vreg->low_vol_level,
870 vreg->high_vol_level, vreg->lpm_uA, vreg->hpm_uA);
871
872 return ret;
873}
874
Asutosh Das390519d2012-12-21 12:21:42 +0530875/* GPIO/Pad data extraction */
876static int sdhci_msm_dt_get_pad_pull_info(struct device *dev, int id,
877 struct sdhci_msm_pad_pull_data **pad_pull_data)
878{
879 int ret = 0, base = 0, len, i;
880 u32 *tmp;
881 struct sdhci_msm_pad_pull_data *pull_data;
882 struct sdhci_msm_pad_pull *pull;
883
884 switch (id) {
885 case 1:
886 base = TLMM_PULL_SDC1_CLK;
887 break;
888 case 2:
889 base = TLMM_PULL_SDC2_CLK;
890 break;
891 case 3:
892 base = TLMM_PULL_SDC3_CLK;
893 break;
894 case 4:
895 base = TLMM_PULL_SDC4_CLK;
896 break;
897 default:
898 dev_err(dev, "%s: Invalid slot id\n", __func__);
899 ret = -EINVAL;
900 goto out;
901 }
902
903 pull_data = devm_kzalloc(dev, sizeof(struct sdhci_msm_pad_pull_data),
904 GFP_KERNEL);
905 if (!pull_data) {
906 dev_err(dev, "No memory for msm_mmc_pad_pull_data\n");
907 ret = -ENOMEM;
908 goto out;
909 }
910 pull_data->size = 3; /* array size for clk, cmd, data */
911
912 /* Allocate on, off configs for clk, cmd, data */
913 pull = devm_kzalloc(dev, 2 * pull_data->size *\
914 sizeof(struct sdhci_msm_pad_pull), GFP_KERNEL);
915 if (!pull) {
916 dev_err(dev, "No memory for msm_mmc_pad_pull\n");
917 ret = -ENOMEM;
918 goto out;
919 }
920 pull_data->on = pull;
921 pull_data->off = pull + pull_data->size;
922
923 ret = sdhci_msm_dt_get_array(dev, "qcom,pad-pull-on",
924 &tmp, &len, pull_data->size);
925 if (ret)
926 goto out;
927
928 for (i = 0; i < len; i++) {
929 pull_data->on[i].no = base + i;
930 pull_data->on[i].val = tmp[i];
931 dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
932 i, pull_data->on[i].val);
933 }
934
935 ret = sdhci_msm_dt_get_array(dev, "qcom,pad-pull-off",
936 &tmp, &len, pull_data->size);
937 if (ret)
938 goto out;
939
940 for (i = 0; i < len; i++) {
941 pull_data->off[i].no = base + i;
942 pull_data->off[i].val = tmp[i];
943 dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
944 i, pull_data->off[i].val);
945 }
946
947 *pad_pull_data = pull_data;
948out:
949 return ret;
950}
951
952static int sdhci_msm_dt_get_pad_drv_info(struct device *dev, int id,
953 struct sdhci_msm_pad_drv_data **pad_drv_data)
954{
955 int ret = 0, base = 0, len, i;
956 u32 *tmp;
957 struct sdhci_msm_pad_drv_data *drv_data;
958 struct sdhci_msm_pad_drv *drv;
959
960 switch (id) {
961 case 1:
962 base = TLMM_HDRV_SDC1_CLK;
963 break;
964 case 2:
965 base = TLMM_HDRV_SDC2_CLK;
966 break;
967 case 3:
968 base = TLMM_HDRV_SDC3_CLK;
969 break;
970 case 4:
971 base = TLMM_HDRV_SDC4_CLK;
972 break;
973 default:
974 dev_err(dev, "%s: Invalid slot id\n", __func__);
975 ret = -EINVAL;
976 goto out;
977 }
978
979 drv_data = devm_kzalloc(dev, sizeof(struct sdhci_msm_pad_drv_data),
980 GFP_KERNEL);
981 if (!drv_data) {
982 dev_err(dev, "No memory for msm_mmc_pad_drv_data\n");
983 ret = -ENOMEM;
984 goto out;
985 }
986 drv_data->size = 3; /* array size for clk, cmd, data */
987
988 /* Allocate on, off configs for clk, cmd, data */
989 drv = devm_kzalloc(dev, 2 * drv_data->size *\
990 sizeof(struct sdhci_msm_pad_drv), GFP_KERNEL);
991 if (!drv) {
992 dev_err(dev, "No memory msm_mmc_pad_drv\n");
993 ret = -ENOMEM;
994 goto out;
995 }
996 drv_data->on = drv;
997 drv_data->off = drv + drv_data->size;
998
999 ret = sdhci_msm_dt_get_array(dev, "qcom,pad-drv-on",
1000 &tmp, &len, drv_data->size);
1001 if (ret)
1002 goto out;
1003
1004 for (i = 0; i < len; i++) {
1005 drv_data->on[i].no = base + i;
1006 drv_data->on[i].val = tmp[i];
1007 dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
1008 i, drv_data->on[i].val);
1009 }
1010
1011 ret = sdhci_msm_dt_get_array(dev, "qcom,pad-drv-off",
1012 &tmp, &len, drv_data->size);
1013 if (ret)
1014 goto out;
1015
1016 for (i = 0; i < len; i++) {
1017 drv_data->off[i].no = base + i;
1018 drv_data->off[i].val = tmp[i];
1019 dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
1020 i, drv_data->off[i].val);
1021 }
1022
1023 *pad_drv_data = drv_data;
1024out:
1025 return ret;
1026}
1027
Asutosh Das33a4ff52012-12-18 16:14:02 +05301028#define GPIO_NAME_MAX_LEN 32
1029static int sdhci_msm_dt_parse_gpio_info(struct device *dev,
1030 struct sdhci_msm_pltfm_data *pdata)
1031{
Asutosh Das390519d2012-12-21 12:21:42 +05301032 int ret = 0, id = 0, cnt, i;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301033 struct sdhci_msm_pin_data *pin_data;
1034 struct device_node *np = dev->of_node;
1035
1036 pin_data = devm_kzalloc(dev, sizeof(*pin_data), GFP_KERNEL);
1037 if (!pin_data) {
1038 dev_err(dev, "No memory for pin_data\n");
1039 ret = -ENOMEM;
1040 goto out;
1041 }
1042
1043 cnt = of_gpio_count(np);
1044 if (cnt > 0) {
Asutosh Das390519d2012-12-21 12:21:42 +05301045 pin_data->is_gpio = true;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301046 pin_data->gpio_data = devm_kzalloc(dev,
1047 sizeof(struct sdhci_msm_gpio_data), GFP_KERNEL);
1048 if (!pin_data->gpio_data) {
1049 dev_err(dev, "No memory for gpio_data\n");
1050 ret = -ENOMEM;
1051 goto out;
1052 }
1053 pin_data->gpio_data->size = cnt;
1054 pin_data->gpio_data->gpio = devm_kzalloc(dev, cnt *
1055 sizeof(struct sdhci_msm_gpio), GFP_KERNEL);
1056
1057 if (!pin_data->gpio_data->gpio) {
1058 dev_err(dev, "No memory for gpio\n");
1059 ret = -ENOMEM;
1060 goto out;
1061 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301062 for (i = 0; i < cnt; i++) {
1063 const char *name = NULL;
1064 char result[GPIO_NAME_MAX_LEN];
1065 pin_data->gpio_data->gpio[i].no = of_get_gpio(np, i);
1066 of_property_read_string_index(np,
1067 "qcom,gpio-names", i, &name);
1068
1069 snprintf(result, GPIO_NAME_MAX_LEN, "%s-%s",
1070 dev_name(dev), name ? name : "?");
1071 pin_data->gpio_data->gpio[i].name = result;
1072 dev_dbg(dev, "%s: gpio[%s] = %d\n", __func__,
Asutosh Das390519d2012-12-21 12:21:42 +05301073 pin_data->gpio_data->gpio[i].name,
1074 pin_data->gpio_data->gpio[i].no);
Asutosh Das33a4ff52012-12-18 16:14:02 +05301075 }
Asutosh Das390519d2012-12-21 12:21:42 +05301076 } else {
1077 pin_data->pad_data =
1078 devm_kzalloc(dev,
1079 sizeof(struct sdhci_msm_pad_data),
1080 GFP_KERNEL);
1081 if (!pin_data->pad_data) {
1082 dev_err(dev,
1083 "No memory for pin_data->pad_data\n");
1084 ret = -ENOMEM;
1085 goto out;
1086 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301087
Asutosh Das390519d2012-12-21 12:21:42 +05301088 ret = of_alias_get_id(np, "sdhc");
1089 if (ret < 0) {
1090 dev_err(dev, "Failed to get slot index %d\n", ret);
1091 goto out;
1092 }
1093 id = ret;
1094
1095 ret = sdhci_msm_dt_get_pad_pull_info(
1096 dev, id, &pin_data->pad_data->pull);
1097 if (ret)
1098 goto out;
1099 ret = sdhci_msm_dt_get_pad_drv_info(
1100 dev, id, &pin_data->pad_data->drv);
1101 if (ret)
1102 goto out;
1103
1104 }
1105 pdata->pin_data = pin_data;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301106out:
1107 if (ret)
1108 dev_err(dev, "%s failed with err %d\n", __func__, ret);
1109 return ret;
1110}
1111
1112/* Parse platform data */
1113static struct sdhci_msm_pltfm_data *sdhci_msm_populate_pdata(struct device *dev)
1114{
1115 struct sdhci_msm_pltfm_data *pdata = NULL;
1116 struct device_node *np = dev->of_node;
1117 u32 bus_width = 0;
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301118 u32 cpu_dma_latency;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301119 int len, i;
Sahitya Tummala00240122013-02-28 19:50:51 +05301120 int clk_table_len;
1121 u32 *clk_table = NULL;
Sujit Reddy Thumma4ddff322013-06-03 09:54:32 +05301122 enum of_gpio_flags flags = OF_GPIO_ACTIVE_LOW;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301123
1124 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1125 if (!pdata) {
1126 dev_err(dev, "failed to allocate memory for platform data\n");
1127 goto out;
1128 }
1129
Sujit Reddy Thumma4ddff322013-06-03 09:54:32 +05301130 pdata->status_gpio = of_get_named_gpio_flags(np, "cd-gpios", 0, &flags);
1131 if (gpio_is_valid(pdata->status_gpio) & !(flags & OF_GPIO_ACTIVE_LOW))
1132 pdata->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
Sahitya Tummala62448d92013-03-12 14:57:46 +05301133
Asutosh Das33a4ff52012-12-18 16:14:02 +05301134 of_property_read_u32(np, "qcom,bus-width", &bus_width);
1135 if (bus_width == 8)
1136 pdata->mmc_bus_width = MMC_CAP_8_BIT_DATA;
1137 else if (bus_width == 4)
1138 pdata->mmc_bus_width = MMC_CAP_4_BIT_DATA;
1139 else {
1140 dev_notice(dev, "invalid bus-width, default to 1-bit mode\n");
1141 pdata->mmc_bus_width = 0;
1142 }
1143
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301144 if (!of_property_read_u32(np, "qcom,cpu-dma-latency-us",
1145 &cpu_dma_latency))
1146 pdata->cpu_dma_latency_us = cpu_dma_latency;
1147
Sahitya Tummala00240122013-02-28 19:50:51 +05301148 if (sdhci_msm_dt_get_array(dev, "qcom,clk-rates",
1149 &clk_table, &clk_table_len, 0)) {
1150 dev_err(dev, "failed parsing supported clock rates\n");
1151 goto out;
1152 }
1153 if (!clk_table || !clk_table_len) {
1154 dev_err(dev, "Invalid clock table\n");
1155 goto out;
1156 }
1157 pdata->sup_clk_table = clk_table;
1158 pdata->sup_clk_cnt = clk_table_len;
1159
Asutosh Das33a4ff52012-12-18 16:14:02 +05301160 pdata->vreg_data = devm_kzalloc(dev, sizeof(struct
1161 sdhci_msm_slot_reg_data),
1162 GFP_KERNEL);
1163 if (!pdata->vreg_data) {
1164 dev_err(dev, "failed to allocate memory for vreg data\n");
1165 goto out;
1166 }
1167
1168 if (sdhci_msm_dt_parse_vreg_info(dev, &pdata->vreg_data->vdd_data,
1169 "vdd")) {
1170 dev_err(dev, "failed parsing vdd data\n");
1171 goto out;
1172 }
1173 if (sdhci_msm_dt_parse_vreg_info(dev,
1174 &pdata->vreg_data->vdd_io_data,
1175 "vdd-io")) {
1176 dev_err(dev, "failed parsing vdd-io data\n");
1177 goto out;
1178 }
1179
1180 if (sdhci_msm_dt_parse_gpio_info(dev, pdata)) {
1181 dev_err(dev, "failed parsing gpio data\n");
1182 goto out;
1183 }
1184
Asutosh Das33a4ff52012-12-18 16:14:02 +05301185 len = of_property_count_strings(np, "qcom,bus-speed-mode");
1186
1187 for (i = 0; i < len; i++) {
1188 const char *name = NULL;
1189
1190 of_property_read_string_index(np,
1191 "qcom,bus-speed-mode", i, &name);
1192 if (!name)
1193 continue;
1194
1195 if (!strncmp(name, "HS200_1p8v", sizeof("HS200_1p8v")))
1196 pdata->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
1197 else if (!strncmp(name, "HS200_1p2v", sizeof("HS200_1p2v")))
1198 pdata->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
1199 else if (!strncmp(name, "DDR_1p8v", sizeof("DDR_1p8v")))
1200 pdata->caps |= MMC_CAP_1_8V_DDR
1201 | MMC_CAP_UHS_DDR50;
1202 else if (!strncmp(name, "DDR_1p2v", sizeof("DDR_1p2v")))
1203 pdata->caps |= MMC_CAP_1_2V_DDR
1204 | MMC_CAP_UHS_DDR50;
1205 }
1206
1207 if (of_get_property(np, "qcom,nonremovable", NULL))
1208 pdata->nonremovable = true;
1209
1210 return pdata;
1211out:
1212 return NULL;
1213}
1214
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301215/* Returns required bandwidth in Bytes per Sec */
1216static unsigned int sdhci_get_bw_required(struct sdhci_host *host,
1217 struct mmc_ios *ios)
1218{
Sahitya Tummala53aff982013-04-03 18:03:31 +05301219 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1220 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1221
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301222 unsigned int bw;
1223
Sahitya Tummala53aff982013-04-03 18:03:31 +05301224 bw = msm_host->clk_rate;
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301225 /*
1226 * For DDR mode, SDCC controller clock will be at
1227 * the double rate than the actual clock that goes to card.
1228 */
1229 if (ios->bus_width == MMC_BUS_WIDTH_4)
1230 bw /= 2;
1231 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1232 bw /= 8;
1233
1234 return bw;
1235}
1236
1237static int sdhci_msm_bus_get_vote_for_bw(struct sdhci_msm_host *host,
1238 unsigned int bw)
1239{
1240 unsigned int *table = host->pdata->voting_data->bw_vecs;
1241 unsigned int size = host->pdata->voting_data->bw_vecs_size;
1242 int i;
1243
1244 if (host->msm_bus_vote.is_max_bw_needed && bw)
1245 return host->msm_bus_vote.max_bw_vote;
1246
1247 for (i = 0; i < size; i++) {
1248 if (bw <= table[i])
1249 break;
1250 }
1251
1252 if (i && (i == size))
1253 i--;
1254
1255 return i;
1256}
1257
1258/*
1259 * This function must be called with host lock acquired.
1260 * Caller of this function should also ensure that msm bus client
1261 * handle is not null.
1262 */
1263static inline int sdhci_msm_bus_set_vote(struct sdhci_msm_host *msm_host,
1264 int vote,
1265 unsigned long flags)
1266{
1267 struct sdhci_host *host = platform_get_drvdata(msm_host->pdev);
1268 int rc = 0;
1269
1270 if (vote != msm_host->msm_bus_vote.curr_vote) {
1271 spin_unlock_irqrestore(&host->lock, flags);
1272 rc = msm_bus_scale_client_update_request(
1273 msm_host->msm_bus_vote.client_handle, vote);
1274 spin_lock_irqsave(&host->lock, flags);
1275 if (rc) {
1276 pr_err("%s: msm_bus_scale_client_update_request() failed: bus_client_handle=0x%x, vote=%d, err=%d\n",
1277 mmc_hostname(host->mmc),
1278 msm_host->msm_bus_vote.client_handle, vote, rc);
1279 goto out;
1280 }
1281 msm_host->msm_bus_vote.curr_vote = vote;
1282 }
1283out:
1284 return rc;
1285}
1286
1287/*
1288 * Internal work. Work to set 0 bandwidth for msm bus.
1289 */
1290static void sdhci_msm_bus_work(struct work_struct *work)
1291{
1292 struct sdhci_msm_host *msm_host;
1293 struct sdhci_host *host;
1294 unsigned long flags;
1295
1296 msm_host = container_of(work, struct sdhci_msm_host,
1297 msm_bus_vote.vote_work.work);
1298 host = platform_get_drvdata(msm_host->pdev);
1299
1300 if (!msm_host->msm_bus_vote.client_handle)
1301 return;
1302
1303 spin_lock_irqsave(&host->lock, flags);
1304 /* don't vote for 0 bandwidth if any request is in progress */
1305 if (!host->mrq) {
1306 sdhci_msm_bus_set_vote(msm_host,
1307 msm_host->msm_bus_vote.min_bw_vote, flags);
1308 } else
1309 pr_warning("%s: %s: Transfer in progress. skipping bus voting to 0 bandwidth\n",
1310 mmc_hostname(host->mmc), __func__);
1311 spin_unlock_irqrestore(&host->lock, flags);
1312}
1313
1314/*
1315 * This function cancels any scheduled delayed work and sets the bus
1316 * vote based on bw (bandwidth) argument.
1317 */
1318static void sdhci_msm_bus_cancel_work_and_set_vote(struct sdhci_host *host,
1319 unsigned int bw)
1320{
1321 int vote;
1322 unsigned long flags;
1323 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1324 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1325
1326 cancel_delayed_work_sync(&msm_host->msm_bus_vote.vote_work);
1327 spin_lock_irqsave(&host->lock, flags);
1328 vote = sdhci_msm_bus_get_vote_for_bw(msm_host, bw);
1329 sdhci_msm_bus_set_vote(msm_host, vote, flags);
1330 spin_unlock_irqrestore(&host->lock, flags);
1331}
1332
1333#define MSM_MMC_BUS_VOTING_DELAY 200 /* msecs */
1334
1335/* This function queues a work which will set the bandwidth requiement to 0 */
1336static void sdhci_msm_bus_queue_work(struct sdhci_host *host)
1337{
1338 unsigned long flags;
1339 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1340 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1341
1342 spin_lock_irqsave(&host->lock, flags);
1343 if (msm_host->msm_bus_vote.min_bw_vote !=
1344 msm_host->msm_bus_vote.curr_vote)
1345 queue_delayed_work(system_nrt_wq,
1346 &msm_host->msm_bus_vote.vote_work,
1347 msecs_to_jiffies(MSM_MMC_BUS_VOTING_DELAY));
1348 spin_unlock_irqrestore(&host->lock, flags);
1349}
1350
1351static int sdhci_msm_bus_register(struct sdhci_msm_host *host,
1352 struct platform_device *pdev)
1353{
1354 int rc = 0;
1355 struct msm_bus_scale_pdata *bus_pdata;
1356
1357 struct sdhci_msm_bus_voting_data *data;
1358 struct device *dev = &pdev->dev;
1359
1360 data = devm_kzalloc(dev,
1361 sizeof(struct sdhci_msm_bus_voting_data), GFP_KERNEL);
1362 if (!data) {
1363 dev_err(&pdev->dev,
1364 "%s: failed to allocate memory\n", __func__);
1365 rc = -ENOMEM;
1366 goto out;
1367 }
1368 data->bus_pdata = msm_bus_cl_get_pdata(pdev);
1369 if (data->bus_pdata) {
1370 rc = sdhci_msm_dt_get_array(dev, "qcom,bus-bw-vectors-bps",
1371 &data->bw_vecs, &data->bw_vecs_size, 0);
1372 if (rc) {
1373 dev_err(&pdev->dev,
1374 "%s: Failed to get bus-bw-vectors-bps\n",
1375 __func__);
1376 goto out;
1377 }
1378 host->pdata->voting_data = data;
1379 }
1380 if (host->pdata->voting_data &&
1381 host->pdata->voting_data->bus_pdata &&
1382 host->pdata->voting_data->bw_vecs &&
1383 host->pdata->voting_data->bw_vecs_size) {
1384
1385 bus_pdata = host->pdata->voting_data->bus_pdata;
1386 host->msm_bus_vote.client_handle =
1387 msm_bus_scale_register_client(bus_pdata);
1388 if (!host->msm_bus_vote.client_handle) {
1389 dev_err(&pdev->dev, "msm_bus_scale_register_client()\n");
1390 rc = -EFAULT;
1391 goto out;
1392 }
1393 /* cache the vote index for minimum and maximum bandwidth */
1394 host->msm_bus_vote.min_bw_vote =
1395 sdhci_msm_bus_get_vote_for_bw(host, 0);
1396 host->msm_bus_vote.max_bw_vote =
1397 sdhci_msm_bus_get_vote_for_bw(host, UINT_MAX);
1398 } else {
1399 devm_kfree(dev, data);
1400 }
1401
1402out:
1403 return rc;
1404}
1405
1406static void sdhci_msm_bus_unregister(struct sdhci_msm_host *host)
1407{
1408 if (host->msm_bus_vote.client_handle)
1409 msm_bus_scale_unregister_client(
1410 host->msm_bus_vote.client_handle);
1411}
1412
1413static void sdhci_msm_bus_voting(struct sdhci_host *host, u32 enable)
1414{
1415 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1416 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1417 struct mmc_ios *ios = &host->mmc->ios;
1418 unsigned int bw;
1419
1420 if (!msm_host->msm_bus_vote.client_handle)
1421 return;
1422
1423 bw = sdhci_get_bw_required(host, ios);
Sahitya Tummalac45ae732013-05-23 15:59:22 +05301424 if (enable) {
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301425 sdhci_msm_bus_cancel_work_and_set_vote(host, bw);
Sahitya Tummalac45ae732013-05-23 15:59:22 +05301426 } else {
1427 /*
1428 * If clock gating is enabled, then remove the vote
1429 * immediately because clocks will be disabled only
1430 * after SDHCI_MSM_MMC_CLK_GATE_DELAY and thus no
1431 * additional delay is required to remove the bus vote.
1432 */
1433 if (host->mmc->clkgate_delay)
1434 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
1435 else
1436 sdhci_msm_bus_queue_work(host);
1437 }
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301438}
1439
Asutosh Das33a4ff52012-12-18 16:14:02 +05301440/* Regulator utility functions */
1441static int sdhci_msm_vreg_init_reg(struct device *dev,
1442 struct sdhci_msm_reg_data *vreg)
1443{
1444 int ret = 0;
1445
1446 /* check if regulator is already initialized? */
1447 if (vreg->reg)
1448 goto out;
1449
1450 /* Get the regulator handle */
1451 vreg->reg = devm_regulator_get(dev, vreg->name);
1452 if (IS_ERR(vreg->reg)) {
1453 ret = PTR_ERR(vreg->reg);
1454 pr_err("%s: devm_regulator_get(%s) failed. ret=%d\n",
1455 __func__, vreg->name, ret);
1456 goto out;
1457 }
1458
1459 /* sanity check */
1460 if (!vreg->high_vol_level || !vreg->hpm_uA) {
1461 pr_err("%s: %s invalid constraints specified\n",
1462 __func__, vreg->name);
1463 ret = -EINVAL;
1464 }
1465
1466out:
1467 return ret;
1468}
1469
1470static void sdhci_msm_vreg_deinit_reg(struct sdhci_msm_reg_data *vreg)
1471{
1472 if (vreg->reg)
1473 devm_regulator_put(vreg->reg);
1474}
1475
1476static int sdhci_msm_vreg_set_optimum_mode(struct sdhci_msm_reg_data
1477 *vreg, int uA_load)
1478{
1479 int ret = 0;
1480
1481 /*
1482 * regulators that do not support regulator_set_voltage also
1483 * do not support regulator_set_optimum_mode
1484 */
1485 ret = regulator_set_optimum_mode(vreg->reg, uA_load);
1486 if (ret < 0)
1487 pr_err("%s: regulator_set_optimum_mode(reg=%s,uA_load=%d) failed. ret=%d\n",
1488 __func__, vreg->name, uA_load, ret);
1489 else
1490 /*
1491 * regulator_set_optimum_mode() can return non zero
1492 * value even for success case.
1493 */
1494 ret = 0;
1495 return ret;
1496}
1497
1498static int sdhci_msm_vreg_set_voltage(struct sdhci_msm_reg_data *vreg,
1499 int min_uV, int max_uV)
1500{
1501 int ret = 0;
1502
1503 ret = regulator_set_voltage(vreg->reg, min_uV, max_uV);
1504 if (ret) {
1505 pr_err("%s: regulator_set_voltage(%s)failed. min_uV=%d,max_uV=%d,ret=%d\n",
1506 __func__, vreg->name, min_uV, max_uV, ret);
1507 }
1508
1509 return ret;
1510}
1511
1512static int sdhci_msm_vreg_enable(struct sdhci_msm_reg_data *vreg)
1513{
1514 int ret = 0;
1515
1516 /* Put regulator in HPM (high power mode) */
1517 ret = sdhci_msm_vreg_set_optimum_mode(vreg, vreg->hpm_uA);
1518 if (ret < 0)
1519 return ret;
1520
1521 if (!vreg->is_enabled) {
1522 /* Set voltage level */
1523 ret = sdhci_msm_vreg_set_voltage(vreg, vreg->high_vol_level,
1524 vreg->high_vol_level);
1525 if (ret)
1526 return ret;
1527 }
1528 ret = regulator_enable(vreg->reg);
1529 if (ret) {
1530 pr_err("%s: regulator_enable(%s) failed. ret=%d\n",
1531 __func__, vreg->name, ret);
1532 return ret;
1533 }
1534 vreg->is_enabled = true;
1535 return ret;
1536}
1537
1538static int sdhci_msm_vreg_disable(struct sdhci_msm_reg_data *vreg)
1539{
1540 int ret = 0;
1541
1542 /* Never disable regulator marked as always_on */
1543 if (vreg->is_enabled && !vreg->is_always_on) {
1544 ret = regulator_disable(vreg->reg);
1545 if (ret) {
1546 pr_err("%s: regulator_disable(%s) failed. ret=%d\n",
1547 __func__, vreg->name, ret);
1548 goto out;
1549 }
1550 vreg->is_enabled = false;
1551
1552 ret = sdhci_msm_vreg_set_optimum_mode(vreg, 0);
1553 if (ret < 0)
1554 goto out;
1555
1556 /* Set min. voltage level to 0 */
1557 ret = sdhci_msm_vreg_set_voltage(vreg, 0, vreg->high_vol_level);
1558 if (ret)
1559 goto out;
1560 } else if (vreg->is_enabled && vreg->is_always_on) {
1561 if (vreg->lpm_sup) {
1562 /* Put always_on regulator in LPM (low power mode) */
1563 ret = sdhci_msm_vreg_set_optimum_mode(vreg,
1564 vreg->lpm_uA);
1565 if (ret < 0)
1566 goto out;
1567 }
1568 }
1569out:
1570 return ret;
1571}
1572
1573static int sdhci_msm_setup_vreg(struct sdhci_msm_pltfm_data *pdata,
1574 bool enable, bool is_init)
1575{
1576 int ret = 0, i;
1577 struct sdhci_msm_slot_reg_data *curr_slot;
1578 struct sdhci_msm_reg_data *vreg_table[2];
1579
1580 curr_slot = pdata->vreg_data;
1581 if (!curr_slot) {
1582 pr_debug("%s: vreg info unavailable,assuming the slot is powered by always on domain\n",
1583 __func__);
1584 goto out;
1585 }
1586
1587 vreg_table[0] = curr_slot->vdd_data;
1588 vreg_table[1] = curr_slot->vdd_io_data;
1589
1590 for (i = 0; i < ARRAY_SIZE(vreg_table); i++) {
1591 if (vreg_table[i]) {
1592 if (enable)
1593 ret = sdhci_msm_vreg_enable(vreg_table[i]);
1594 else
1595 ret = sdhci_msm_vreg_disable(vreg_table[i]);
1596 if (ret)
1597 goto out;
1598 }
1599 }
1600out:
1601 return ret;
1602}
1603
1604/*
1605 * Reset vreg by ensuring it is off during probe. A call
1606 * to enable vreg is needed to balance disable vreg
1607 */
1608static int sdhci_msm_vreg_reset(struct sdhci_msm_pltfm_data *pdata)
1609{
1610 int ret;
1611
1612 ret = sdhci_msm_setup_vreg(pdata, 1, true);
1613 if (ret)
1614 return ret;
1615 ret = sdhci_msm_setup_vreg(pdata, 0, true);
1616 return ret;
1617}
1618
1619/* This init function should be called only once for each SDHC slot */
1620static int sdhci_msm_vreg_init(struct device *dev,
1621 struct sdhci_msm_pltfm_data *pdata,
1622 bool is_init)
1623{
1624 int ret = 0;
1625 struct sdhci_msm_slot_reg_data *curr_slot;
1626 struct sdhci_msm_reg_data *curr_vdd_reg, *curr_vdd_io_reg;
1627
1628 curr_slot = pdata->vreg_data;
1629 if (!curr_slot)
1630 goto out;
1631
1632 curr_vdd_reg = curr_slot->vdd_data;
1633 curr_vdd_io_reg = curr_slot->vdd_io_data;
1634
1635 if (!is_init)
1636 /* Deregister all regulators from regulator framework */
1637 goto vdd_io_reg_deinit;
1638
1639 /*
1640 * Get the regulator handle from voltage regulator framework
1641 * and then try to set the voltage level for the regulator
1642 */
1643 if (curr_vdd_reg) {
1644 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_reg);
1645 if (ret)
1646 goto out;
1647 }
1648 if (curr_vdd_io_reg) {
1649 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_io_reg);
1650 if (ret)
1651 goto vdd_reg_deinit;
1652 }
1653 ret = sdhci_msm_vreg_reset(pdata);
1654 if (ret)
1655 dev_err(dev, "vreg reset failed (%d)\n", ret);
1656 goto out;
1657
1658vdd_io_reg_deinit:
1659 if (curr_vdd_io_reg)
1660 sdhci_msm_vreg_deinit_reg(curr_vdd_io_reg);
1661vdd_reg_deinit:
1662 if (curr_vdd_reg)
1663 sdhci_msm_vreg_deinit_reg(curr_vdd_reg);
1664out:
1665 return ret;
1666}
1667
1668
1669static int sdhci_msm_set_vdd_io_vol(struct sdhci_msm_pltfm_data *pdata,
1670 enum vdd_io_level level,
1671 unsigned int voltage_level)
1672{
1673 int ret = 0;
1674 int set_level;
1675 struct sdhci_msm_reg_data *vdd_io_reg;
1676
1677 if (!pdata->vreg_data)
1678 return ret;
1679
1680 vdd_io_reg = pdata->vreg_data->vdd_io_data;
1681 if (vdd_io_reg && vdd_io_reg->is_enabled) {
1682 switch (level) {
1683 case VDD_IO_LOW:
1684 set_level = vdd_io_reg->low_vol_level;
1685 break;
1686 case VDD_IO_HIGH:
1687 set_level = vdd_io_reg->high_vol_level;
1688 break;
1689 case VDD_IO_SET_LEVEL:
1690 set_level = voltage_level;
1691 break;
1692 default:
1693 pr_err("%s: invalid argument level = %d",
1694 __func__, level);
1695 ret = -EINVAL;
1696 return ret;
1697 }
1698 ret = sdhci_msm_vreg_set_voltage(vdd_io_reg, set_level,
1699 set_level);
1700 }
1701 return ret;
1702}
1703
1704static irqreturn_t sdhci_msm_pwr_irq(int irq, void *data)
1705{
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07001706 struct sdhci_host *host = (struct sdhci_host *)data;
1707 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1708 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301709 u8 irq_status = 0;
1710 u8 irq_ack = 0;
1711 int ret = 0;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301712 int pwr_state = 0, io_level = 0;
1713 unsigned long flags;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301714
1715 irq_status = readb_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
1716 pr_debug("%s: Received IRQ(%d), status=0x%x\n",
1717 mmc_hostname(msm_host->mmc), irq, irq_status);
1718
1719 /* Clear the interrupt */
1720 writeb_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
1721 /*
1722 * SDHC has core_mem and hc_mem device memory and these memory
1723 * addresses do not fall within 1KB region. Hence, any update to
1724 * core_mem address space would require an mb() to ensure this gets
1725 * completed before its next update to registers within hc_mem.
1726 */
1727 mb();
1728
1729 /* Handle BUS ON/OFF*/
1730 if (irq_status & CORE_PWRCTL_BUS_ON) {
1731 ret = sdhci_msm_setup_vreg(msm_host->pdata, true, false);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301732 if (!ret) {
Asutosh Das33a4ff52012-12-18 16:14:02 +05301733 ret = sdhci_msm_setup_pins(msm_host->pdata, true);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301734 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
1735 VDD_IO_HIGH, 0);
1736 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301737 if (ret)
1738 irq_ack |= CORE_PWRCTL_BUS_FAIL;
1739 else
1740 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301741
1742 pwr_state = REQ_BUS_ON;
1743 io_level = REQ_IO_HIGH;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301744 }
1745 if (irq_status & CORE_PWRCTL_BUS_OFF) {
1746 ret = sdhci_msm_setup_vreg(msm_host->pdata, false, false);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301747 if (!ret) {
Asutosh Das33a4ff52012-12-18 16:14:02 +05301748 ret = sdhci_msm_setup_pins(msm_host->pdata, false);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301749 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
1750 VDD_IO_LOW, 0);
1751 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301752 if (ret)
1753 irq_ack |= CORE_PWRCTL_BUS_FAIL;
1754 else
1755 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301756
1757 pwr_state = REQ_BUS_OFF;
1758 io_level = REQ_IO_LOW;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301759 }
1760 /* Handle IO LOW/HIGH */
1761 if (irq_status & CORE_PWRCTL_IO_LOW) {
1762 /* Switch voltage Low */
1763 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_LOW, 0);
1764 if (ret)
1765 irq_ack |= CORE_PWRCTL_IO_FAIL;
1766 else
1767 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301768
1769 io_level = REQ_IO_LOW;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301770 }
1771 if (irq_status & CORE_PWRCTL_IO_HIGH) {
1772 /* Switch voltage High */
1773 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_HIGH, 0);
1774 if (ret)
1775 irq_ack |= CORE_PWRCTL_IO_FAIL;
1776 else
1777 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301778
1779 io_level = REQ_IO_HIGH;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301780 }
1781
1782 /* ACK status to the core */
1783 writeb_relaxed(irq_ack, (msm_host->core_mem + CORE_PWRCTL_CTL));
1784 /*
1785 * SDHC has core_mem and hc_mem device memory and these memory
1786 * addresses do not fall within 1KB region. Hence, any update to
1787 * core_mem address space would require an mb() to ensure this gets
1788 * completed before its next update to registers within hc_mem.
1789 */
1790 mb();
1791
Sahitya Tummala179e7382013-03-20 19:24:01 +05301792 if (io_level & REQ_IO_HIGH)
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07001793 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
1794 ~CORE_IO_PAD_PWR_SWITCH),
1795 host->ioaddr + CORE_VENDOR_SPEC);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301796 else if (io_level & REQ_IO_LOW)
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07001797 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
1798 CORE_IO_PAD_PWR_SWITCH),
1799 host->ioaddr + CORE_VENDOR_SPEC);
1800 mb();
1801
Asutosh Das33a4ff52012-12-18 16:14:02 +05301802 pr_debug("%s: Handled IRQ(%d), ret=%d, ack=0x%x\n",
1803 mmc_hostname(msm_host->mmc), irq, ret, irq_ack);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301804 spin_lock_irqsave(&host->lock, flags);
1805 if (pwr_state)
1806 msm_host->curr_pwr_state = pwr_state;
1807 if (io_level)
1808 msm_host->curr_io_level = io_level;
1809 complete(&msm_host->pwr_irq_completion);
1810 spin_unlock_irqrestore(&host->lock, flags);
1811
Asutosh Das33a4ff52012-12-18 16:14:02 +05301812 return IRQ_HANDLED;
1813}
1814
1815/* This function returns the max. current supported by VDD rail in mA */
1816static unsigned int sdhci_msm_get_vreg_vdd_max_current(struct sdhci_msm_host
1817 *host)
1818{
1819 struct sdhci_msm_slot_reg_data *curr_slot = host->pdata->vreg_data;
1820 if (!curr_slot)
1821 return 0;
1822 if (curr_slot->vdd_data)
1823 return curr_slot->vdd_data->hpm_uA / 1000;
1824 else
1825 return 0;
1826}
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301827static ssize_t
1828show_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
1829 char *buf)
1830{
1831 struct sdhci_host *host = dev_get_drvdata(dev);
1832 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1833 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1834
1835 return snprintf(buf, PAGE_SIZE, "%u\n",
1836 msm_host->msm_bus_vote.is_max_bw_needed);
1837}
1838
1839static ssize_t
1840store_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
1841 const char *buf, size_t count)
1842{
1843 struct sdhci_host *host = dev_get_drvdata(dev);
1844 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1845 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1846 uint32_t value;
1847 unsigned long flags;
1848
1849 if (!kstrtou32(buf, 0, &value)) {
1850 spin_lock_irqsave(&host->lock, flags);
1851 msm_host->msm_bus_vote.is_max_bw_needed = !!value;
1852 spin_unlock_irqrestore(&host->lock, flags);
1853 }
1854 return count;
1855}
Asutosh Das33a4ff52012-12-18 16:14:02 +05301856
Sahitya Tummala179e7382013-03-20 19:24:01 +05301857static void sdhci_msm_check_power_status(struct sdhci_host *host, u32 req_type)
Asutosh Das33a4ff52012-12-18 16:14:02 +05301858{
1859 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1860 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301861 unsigned long flags;
1862 bool done = false;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301863
Sahitya Tummala179e7382013-03-20 19:24:01 +05301864 spin_lock_irqsave(&host->lock, flags);
1865 pr_debug("%s: %s: request %d curr_pwr_state %x curr_io_level %x\n",
1866 mmc_hostname(host->mmc), __func__, req_type,
1867 msm_host->curr_pwr_state, msm_host->curr_io_level);
1868 if ((req_type & msm_host->curr_pwr_state) ||
1869 (req_type & msm_host->curr_io_level))
1870 done = true;
1871 spin_unlock_irqrestore(&host->lock, flags);
Asutosh Das33a4ff52012-12-18 16:14:02 +05301872
Sahitya Tummala179e7382013-03-20 19:24:01 +05301873 /*
1874 * This is needed here to hanlde a case where IRQ gets
1875 * triggered even before this function is called so that
1876 * x->done counter of completion gets reset. Otherwise,
1877 * next call to wait_for_completion returns immediately
1878 * without actually waiting for the IRQ to be handled.
1879 */
1880 if (done)
1881 init_completion(&msm_host->pwr_irq_completion);
1882 else
1883 wait_for_completion(&msm_host->pwr_irq_completion);
1884
1885 pr_debug("%s: %s: request %d done\n", mmc_hostname(host->mmc),
1886 __func__, req_type);
Asutosh Das33a4ff52012-12-18 16:14:02 +05301887}
1888
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07001889static void sdhci_msm_toggle_cdr(struct sdhci_host *host, bool enable)
1890{
1891 if (enable)
1892 writel_relaxed((readl_relaxed(host->ioaddr +
1893 CORE_DLL_CONFIG) | CORE_CDR_EN),
1894 host->ioaddr + CORE_DLL_CONFIG);
1895 else
1896 writel_relaxed((readl_relaxed(host->ioaddr +
1897 CORE_DLL_CONFIG) & ~CORE_CDR_EN),
1898 host->ioaddr + CORE_DLL_CONFIG);
1899}
1900
Asutosh Das3781bd82013-01-10 21:11:04 +05301901static unsigned int sdhci_msm_max_segs(void)
1902{
1903 return SDHCI_MSM_MAX_SEGMENTS;
1904}
1905
Sahitya Tummala00240122013-02-28 19:50:51 +05301906static unsigned int sdhci_msm_get_min_clock(struct sdhci_host *host)
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301907{
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301908 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1909 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301910
Sahitya Tummala00240122013-02-28 19:50:51 +05301911 return msm_host->pdata->sup_clk_table[0];
1912}
1913
1914static unsigned int sdhci_msm_get_max_clock(struct sdhci_host *host)
1915{
1916 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1917 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1918 int max_clk_index = msm_host->pdata->sup_clk_cnt;
1919
1920 return msm_host->pdata->sup_clk_table[max_clk_index - 1];
1921}
1922
1923static unsigned int sdhci_msm_get_sup_clk_rate(struct sdhci_host *host,
1924 u32 req_clk)
1925{
1926 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1927 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1928 unsigned int sel_clk = -1;
1929 unsigned char cnt;
1930
1931 if (req_clk < sdhci_msm_get_min_clock(host)) {
1932 sel_clk = sdhci_msm_get_min_clock(host);
1933 return sel_clk;
1934 }
1935
1936 for (cnt = 0; cnt < msm_host->pdata->sup_clk_cnt; cnt++) {
1937 if (msm_host->pdata->sup_clk_table[cnt] > req_clk) {
1938 break;
1939 } else if (msm_host->pdata->sup_clk_table[cnt] == req_clk) {
1940 sel_clk = msm_host->pdata->sup_clk_table[cnt];
1941 break;
1942 } else {
1943 sel_clk = msm_host->pdata->sup_clk_table[cnt];
1944 }
1945 }
1946 return sel_clk;
1947}
1948
1949static int sdhci_msm_prepare_clocks(struct sdhci_host *host, bool enable)
1950{
1951 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1952 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1953 int rc = 0;
1954
1955 if (enable && !atomic_read(&msm_host->clks_on)) {
1956 pr_debug("%s: request to enable clocks\n",
1957 mmc_hostname(host->mmc));
Sahitya Tummalac45ae732013-05-23 15:59:22 +05301958
1959 sdhci_msm_bus_voting(host, 1);
1960
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301961 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
1962 rc = clk_prepare_enable(msm_host->bus_clk);
1963 if (rc) {
1964 pr_err("%s: %s: failed to enable the bus-clock with error %d\n",
1965 mmc_hostname(host->mmc), __func__, rc);
Sahitya Tummalac45ae732013-05-23 15:59:22 +05301966 goto remove_vote;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301967 }
1968 }
1969 if (!IS_ERR(msm_host->pclk)) {
1970 rc = clk_prepare_enable(msm_host->pclk);
1971 if (rc) {
1972 pr_err("%s: %s: failed to enable the pclk with error %d\n",
1973 mmc_hostname(host->mmc), __func__, rc);
1974 goto disable_bus_clk;
1975 }
1976 }
1977 rc = clk_prepare_enable(msm_host->clk);
1978 if (rc) {
1979 pr_err("%s: %s: failed to enable the host-clk with error %d\n",
1980 mmc_hostname(host->mmc), __func__, rc);
1981 goto disable_pclk;
1982 }
1983 mb();
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301984
Sahitya Tummala00240122013-02-28 19:50:51 +05301985 } else if (!enable && atomic_read(&msm_host->clks_on)) {
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301986 pr_debug("%s: request to disable clocks\n",
1987 mmc_hostname(host->mmc));
1988 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
1989 mb();
1990 clk_disable_unprepare(msm_host->clk);
1991 if (!IS_ERR(msm_host->pclk))
1992 clk_disable_unprepare(msm_host->pclk);
1993 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
1994 clk_disable_unprepare(msm_host->bus_clk);
Sahitya Tummalac45ae732013-05-23 15:59:22 +05301995
1996 sdhci_msm_bus_voting(host, 0);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301997 }
Sahitya Tummala00240122013-02-28 19:50:51 +05301998 atomic_set(&msm_host->clks_on, enable);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301999 goto out;
2000disable_pclk:
2001 if (!IS_ERR_OR_NULL(msm_host->pclk))
2002 clk_disable_unprepare(msm_host->pclk);
2003disable_bus_clk:
2004 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2005 clk_disable_unprepare(msm_host->bus_clk);
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302006remove_vote:
2007 if (msm_host->msm_bus_vote.client_handle)
2008 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302009out:
Sahitya Tummala00240122013-02-28 19:50:51 +05302010 return rc;
2011}
2012
2013static void sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock)
2014{
2015 int rc;
2016 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2017 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2018 struct mmc_ios curr_ios = host->mmc->ios;
2019 u32 sup_clock, ddr_clock;
2020
2021 if (!clock) {
2022 sdhci_msm_prepare_clocks(host, false);
2023 host->clock = clock;
2024 return;
2025 }
2026
2027 rc = sdhci_msm_prepare_clocks(host, true);
2028 if (rc)
2029 return;
2030
2031 sup_clock = sdhci_msm_get_sup_clk_rate(host, clock);
2032 if (curr_ios.timing == MMC_TIMING_UHS_DDR50) {
2033 /*
2034 * The SDHC requires internal clock frequency to be double the
2035 * actual clock that will be set for DDR mode. The controller
2036 * uses the faster clock(100MHz) for some of its parts and send
2037 * the actual required clock (50MHz) to the card.
2038 */
2039 ddr_clock = clock * 2;
2040 sup_clock = sdhci_msm_get_sup_clk_rate(host,
2041 ddr_clock);
2042 }
2043 if (sup_clock != msm_host->clk_rate) {
2044 pr_debug("%s: %s: setting clk rate to %u\n",
2045 mmc_hostname(host->mmc), __func__, sup_clock);
2046 rc = clk_set_rate(msm_host->clk, sup_clock);
2047 if (rc) {
2048 pr_err("%s: %s: Failed to set rate %u for host-clk : %d\n",
2049 mmc_hostname(host->mmc), __func__,
2050 sup_clock, rc);
2051 return;
2052 }
2053 msm_host->clk_rate = sup_clock;
2054 host->clock = clock;
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302055 /*
2056 * Update the bus vote in case of frequency change due to
2057 * clock scaling.
2058 */
2059 sdhci_msm_bus_voting(host, 1);
Sahitya Tummala00240122013-02-28 19:50:51 +05302060 }
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302061}
2062
Sahitya Tummala5e4f9642013-03-21 11:13:25 +05302063static int sdhci_msm_set_uhs_signaling(struct sdhci_host *host,
2064 unsigned int uhs)
2065{
2066 u16 ctrl_2;
2067
2068 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2069 /* Select Bus Speed Mode for host */
2070 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
2071 if (uhs == MMC_TIMING_MMC_HS200)
2072 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2073 else if (uhs == MMC_TIMING_UHS_SDR12)
2074 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
2075 else if (uhs == MMC_TIMING_UHS_SDR25)
2076 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
2077 else if (uhs == MMC_TIMING_UHS_SDR50)
2078 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
2079 else if (uhs == MMC_TIMING_UHS_SDR104)
2080 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2081 else if (uhs == MMC_TIMING_UHS_DDR50)
2082 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Sahitya Tummala00240122013-02-28 19:50:51 +05302083 /*
2084 * When clock frquency is less than 100MHz, the feedback clock must be
2085 * provided and DLL must not be used so that tuning can be skipped. To
2086 * provide feedback clock, the mode selection can be any value less
2087 * than 3'b011 in bits [2:0] of HOST CONTROL2 register.
2088 */
2089 if (host->clock <= (100 * 1000 * 1000) &&
2090 (uhs == MMC_TIMING_MMC_HS200 ||
2091 uhs == MMC_TIMING_UHS_SDR104))
2092 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
2093
Sahitya Tummala5e4f9642013-03-21 11:13:25 +05302094 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
2095
2096 return 0;
2097}
2098
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002099/*
2100 * sdhci_msm_disable_data_xfer - disable undergoing AHB bus data transfer
2101 *
2102 * Write 0 to bit 0 in MCI_DATA_CTL (offset 0x2C) - clearing TxActive bit by
2103 * access to legacy registers. It will stop current burst and prevent start of
2104 * the next on.
2105 *
2106 * Polling CORE_AHB_DATA_DELAY_US timeout, by reading bit 13:12 until they are 0
2107 * in CORE_SDCC_DEBUG_REG (offset 0x124) will validate that AHB burst was
2108 * completed and a new one didn't start.
2109 *
2110 * Waiting for 4us while AHB finishes descriptors fetch.
2111 */
2112static void sdhci_msm_disable_data_xfer(struct sdhci_host *host)
2113{
2114 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2115 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2116 u32 value;
2117 int ret;
2118
2119 value = readl_relaxed(msm_host->core_mem + CORE_MCI_DATA_CTRL);
2120 value &= ~(u32)CORE_MCI_DPSM_ENABLE;
2121 writel_relaxed(value, msm_host->core_mem + CORE_MCI_DATA_CTRL);
2122
2123 /* Enable the test bus for device slot */
2124 writel_relaxed(CORE_TESTBUS_ENA | CORE_TESTBUS_SEL2,
2125 msm_host->core_mem + CORE_TESTBUS_CONFIG);
2126
2127 ret = readl_poll_timeout_noirq(msm_host->core_mem
2128 + CORE_SDCC_DEBUG_REG, value,
2129 !(value & CORE_DEBUG_REG_AHB_HTRANS),
2130 CORE_AHB_DATA_DELAY_US, 1);
2131 if (ret) {
2132 pr_err("%s: %s: can't stop ongoing AHB bus access by ADMA\n",
2133 mmc_hostname(host->mmc), __func__);
2134 BUG();
2135 }
2136 /* Disable the test bus for device slot */
2137 value = readl_relaxed(msm_host->core_mem + CORE_TESTBUS_CONFIG);
2138 value &= ~CORE_TESTBUS_ENA;
2139 writel_relaxed(value, msm_host->core_mem + CORE_TESTBUS_CONFIG);
2140
2141 udelay(CORE_AHB_DESC_DELAY_US);
2142}
2143
Asutosh Das33a4ff52012-12-18 16:14:02 +05302144static struct sdhci_ops sdhci_msm_ops = {
Sahitya Tummala5e4f9642013-03-21 11:13:25 +05302145 .set_uhs_signaling = sdhci_msm_set_uhs_signaling,
Asutosh Das33a4ff52012-12-18 16:14:02 +05302146 .check_power_status = sdhci_msm_check_power_status,
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07002147 .execute_tuning = sdhci_msm_execute_tuning,
2148 .toggle_cdr = sdhci_msm_toggle_cdr,
Asutosh Das3781bd82013-01-10 21:11:04 +05302149 .get_max_segments = sdhci_msm_max_segs,
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302150 .set_clock = sdhci_msm_set_clock,
Sahitya Tummala00240122013-02-28 19:50:51 +05302151 .get_min_clock = sdhci_msm_get_min_clock,
2152 .get_max_clock = sdhci_msm_get_max_clock,
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002153 .disable_data_xfer = sdhci_msm_disable_data_xfer,
Asutosh Das33a4ff52012-12-18 16:14:02 +05302154};
2155
2156static int __devinit sdhci_msm_probe(struct platform_device *pdev)
2157{
2158 struct sdhci_host *host;
2159 struct sdhci_pltfm_host *pltfm_host;
2160 struct sdhci_msm_host *msm_host;
2161 struct resource *core_memres = NULL;
Asutosh Dasbbc84782013-02-11 15:31:35 +05302162 int ret = 0, dead = 0;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302163 u32 vdd_max_current;
Stephen Boyd3edbd8f2013-04-24 14:19:46 -07002164 u16 host_version;
Subhash Jadavanic08d2062013-05-14 17:46:43 +05302165 u32 pwr, irq_status, irq_ctl;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302166
2167 pr_debug("%s: Enter %s\n", dev_name(&pdev->dev), __func__);
2168 msm_host = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_msm_host),
2169 GFP_KERNEL);
2170 if (!msm_host) {
2171 ret = -ENOMEM;
2172 goto out;
2173 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05302174
2175 msm_host->sdhci_msm_pdata.ops = &sdhci_msm_ops;
2176 host = sdhci_pltfm_init(pdev, &msm_host->sdhci_msm_pdata);
2177 if (IS_ERR(host)) {
2178 ret = PTR_ERR(host);
2179 goto out;
2180 }
2181
2182 pltfm_host = sdhci_priv(host);
2183 pltfm_host->priv = msm_host;
2184 msm_host->mmc = host->mmc;
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302185 msm_host->pdev = pdev;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302186
2187 /* Extract platform data */
2188 if (pdev->dev.of_node) {
Venkat Gopalakrishnanc61ab7e2013-03-11 12:17:57 -07002189 ret = of_alias_get_id(pdev->dev.of_node, "sdhc");
2190 if (ret < 0) {
2191 dev_err(&pdev->dev, "Failed to get slot index %d\n",
2192 ret);
2193 goto pltfm_free;
2194 }
2195 if (disable_slots & (1 << (ret - 1))) {
2196 dev_info(&pdev->dev, "%s: Slot %d disabled\n", __func__,
2197 ret);
2198 ret = -ENODEV;
2199 goto pltfm_free;
2200 }
2201
Asutosh Das33a4ff52012-12-18 16:14:02 +05302202 msm_host->pdata = sdhci_msm_populate_pdata(&pdev->dev);
2203 if (!msm_host->pdata) {
2204 dev_err(&pdev->dev, "DT parsing error\n");
2205 goto pltfm_free;
2206 }
2207 } else {
2208 dev_err(&pdev->dev, "No device tree node\n");
2209 goto pltfm_free;
2210 }
2211
2212 /* Setup Clocks */
2213
2214 /* Setup SDCC bus voter clock. */
2215 msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus_clk");
2216 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
2217 /* Vote for max. clk rate for max. performance */
2218 ret = clk_set_rate(msm_host->bus_clk, INT_MAX);
2219 if (ret)
2220 goto pltfm_free;
2221 ret = clk_prepare_enable(msm_host->bus_clk);
2222 if (ret)
2223 goto pltfm_free;
2224 }
2225
2226 /* Setup main peripheral bus clock */
2227 msm_host->pclk = devm_clk_get(&pdev->dev, "iface_clk");
2228 if (!IS_ERR(msm_host->pclk)) {
2229 ret = clk_prepare_enable(msm_host->pclk);
2230 if (ret)
2231 goto bus_clk_disable;
2232 }
2233
2234 /* Setup SDC MMC clock */
2235 msm_host->clk = devm_clk_get(&pdev->dev, "core_clk");
2236 if (IS_ERR(msm_host->clk)) {
2237 ret = PTR_ERR(msm_host->clk);
2238 goto pclk_disable;
2239 }
2240
2241 ret = clk_prepare_enable(msm_host->clk);
2242 if (ret)
2243 goto pclk_disable;
2244
Sahitya Tummala00240122013-02-28 19:50:51 +05302245 /* Set to the minimum supported clock frequency */
2246 ret = clk_set_rate(msm_host->clk, sdhci_msm_get_min_clock(host));
2247 if (ret) {
2248 dev_err(&pdev->dev, "MClk rate set failed (%d)\n", ret);
2249 goto clk_disable;
2250 }
2251 msm_host->clk_rate = sdhci_msm_get_min_clock(host);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302252 atomic_set(&msm_host->clks_on, 1);
Sahitya Tummala00240122013-02-28 19:50:51 +05302253
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302254 ret = sdhci_msm_bus_register(msm_host, pdev);
2255 if (ret)
2256 goto clk_disable;
2257
2258 if (msm_host->msm_bus_vote.client_handle)
2259 INIT_DELAYED_WORK(&msm_host->msm_bus_vote.vote_work,
2260 sdhci_msm_bus_work);
2261 sdhci_msm_bus_voting(host, 1);
2262
Asutosh Das33a4ff52012-12-18 16:14:02 +05302263 /* Setup regulators */
2264 ret = sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, true);
2265 if (ret) {
2266 dev_err(&pdev->dev, "Regulator setup failed (%d)\n", ret);
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302267 goto bus_unregister;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302268 }
2269
2270 /* Reset the core and Enable SDHC mode */
2271 core_memres = platform_get_resource_byname(pdev,
2272 IORESOURCE_MEM, "core_mem");
2273 msm_host->core_mem = devm_ioremap(&pdev->dev, core_memres->start,
2274 resource_size(core_memres));
2275
2276 if (!msm_host->core_mem) {
2277 dev_err(&pdev->dev, "Failed to remap registers\n");
2278 ret = -ENOMEM;
2279 goto vreg_deinit;
2280 }
2281
2282 /* Set SW_RST bit in POWER register (Offset 0x0) */
Sahitya Tummalad5d76e72013-04-25 11:50:56 +05302283 writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_POWER) |
2284 CORE_SW_RST, msm_host->core_mem + CORE_POWER);
2285 /*
2286 * SW reset can take upto 10HCLK + 15MCLK cycles.
2287 * Calculating based on min clk rates (hclk = 27MHz,
2288 * mclk = 400KHz) it comes to ~40us. Let's poll for
2289 * max. 1ms for reset completion.
2290 */
2291 ret = readl_poll_timeout(msm_host->core_mem + CORE_POWER,
2292 pwr, !(pwr & CORE_SW_RST), 100, 10);
2293
2294 if (ret) {
2295 dev_err(&pdev->dev, "reset failed (%d)\n", ret);
2296 goto vreg_deinit;
2297 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05302298 /* Set HC_MODE_EN bit in HC_MODE register */
2299 writel_relaxed(HC_MODE_EN, (msm_host->core_mem + CORE_HC_MODE));
2300
2301 /*
Subhash Jadavanic08d2062013-05-14 17:46:43 +05302302 * CORE_SW_RST above may trigger power irq if previous status of PWRCTL
2303 * was either BUS_ON or IO_HIGH_V. So before we enable the power irq
2304 * interrupt in GIC (by registering the interrupt handler), we need to
2305 * ensure that any pending power irq interrupt status is acknowledged
2306 * otherwise power irq interrupt handler would be fired prematurely.
2307 */
2308 irq_status = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
2309 writel_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
2310 irq_ctl = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_CTL);
2311 if (irq_status & (CORE_PWRCTL_BUS_ON | CORE_PWRCTL_BUS_OFF))
2312 irq_ctl |= CORE_PWRCTL_BUS_SUCCESS;
2313 if (irq_status & (CORE_PWRCTL_IO_HIGH | CORE_PWRCTL_IO_LOW))
2314 irq_ctl |= CORE_PWRCTL_IO_SUCCESS;
2315 writel_relaxed(irq_ctl, (msm_host->core_mem + CORE_PWRCTL_CTL));
2316 /*
2317 * Ensure that above writes are propogated before interrupt enablement
2318 * in GIC.
2319 */
2320 mb();
2321
2322 /*
Asutosh Das33a4ff52012-12-18 16:14:02 +05302323 * Following are the deviations from SDHC spec v3.0 -
2324 * 1. Card detection is handled using separate GPIO.
2325 * 2. Bus power control is handled by interacting with PMIC.
2326 */
2327 host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
2328 host->quirks |= SDHCI_QUIRK_SINGLE_POWER_WRITE;
Sahitya Tummala00240122013-02-28 19:50:51 +05302329 host->quirks |= SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN;
2330 host->quirks2 |= SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK;
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302331 host->quirks2 |= SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING;
Krishna Kondaa20d3362013-04-01 21:01:59 -07002332 host->quirks2 |= SDHCI_QUIRK2_USE_MAX_DISCARD_SIZE;
Sahitya Tummalad2ae8832013-04-12 11:49:11 +05302333 host->quirks2 |= SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD;
Sahitya Tummalae6886bd2013-04-12 12:11:20 +05302334 host->quirks2 |= SDHCI_QUIRK2_BROKEN_PRESET_VALUE;
Sahitya Tummala4d12d0b2013-04-12 11:59:25 +05302335 host->quirks2 |= SDHCI_QUIRK2_USE_RESERVED_MAX_TIMEOUT;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302336
Stephen Boyd3edbd8f2013-04-24 14:19:46 -07002337 host_version = readw_relaxed((host->ioaddr + SDHCI_HOST_VERSION));
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002338 dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n",
2339 host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >>
2340 SDHCI_VENDOR_VER_SHIFT));
2341 if (((host_version & SDHCI_VENDOR_VER_MASK) >>
2342 SDHCI_VENDOR_VER_SHIFT) == SDHCI_VER_100) {
2343 /*
2344 * Add 40us delay in interrupt handler when
2345 * operating at initialization frequency(400KHz).
2346 */
2347 host->quirks2 |= SDHCI_QUIRK2_SLOW_INT_CLR;
2348 /*
2349 * Set Software Reset for DAT line in Software
2350 * Reset Register (Bit 2).
2351 */
2352 host->quirks2 |= SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT;
2353 }
2354
2355 /* Setup PWRCTL irq */
Asutosh Dasbbc84782013-02-11 15:31:35 +05302356 msm_host->pwr_irq = platform_get_irq_byname(pdev, "pwr_irq");
2357 if (msm_host->pwr_irq < 0) {
Asutosh Das33a4ff52012-12-18 16:14:02 +05302358 dev_err(&pdev->dev, "Failed to get pwr_irq by name (%d)\n",
Asutosh Dasbbc84782013-02-11 15:31:35 +05302359 msm_host->pwr_irq);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302360 goto vreg_deinit;
2361 }
Asutosh Dasbbc84782013-02-11 15:31:35 +05302362 ret = devm_request_threaded_irq(&pdev->dev, msm_host->pwr_irq, NULL,
Asutosh Das33a4ff52012-12-18 16:14:02 +05302363 sdhci_msm_pwr_irq, IRQF_ONESHOT,
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07002364 dev_name(&pdev->dev), host);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302365 if (ret) {
2366 dev_err(&pdev->dev, "Request threaded irq(%d) failed (%d)\n",
Asutosh Dasbbc84782013-02-11 15:31:35 +05302367 msm_host->pwr_irq, ret);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302368 goto vreg_deinit;
2369 }
2370
2371 /* Enable pwr irq interrupts */
2372 writel_relaxed(INT_MASK, (msm_host->core_mem + CORE_PWRCTL_MASK));
2373
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302374 /* Set clock gating delay to be used when CONFIG_MMC_CLKGATE is set */
2375 msm_host->mmc->clkgate_delay = SDHCI_MSM_MMC_CLK_GATE_DELAY;
2376
Asutosh Das33a4ff52012-12-18 16:14:02 +05302377 /* Set host capabilities */
2378 msm_host->mmc->caps |= msm_host->pdata->mmc_bus_width;
2379 msm_host->mmc->caps |= msm_host->pdata->caps;
2380
2381 vdd_max_current = sdhci_msm_get_vreg_vdd_max_current(msm_host);
2382 if (vdd_max_current >= 800)
2383 msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_800;
2384 else if (vdd_max_current >= 600)
2385 msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_600;
2386 else if (vdd_max_current >= 400)
2387 msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_400;
2388 else
2389 msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_200;
2390
2391 if (vdd_max_current > 150)
2392 msm_host->mmc->caps |= MMC_CAP_SET_XPC_180 |
2393 MMC_CAP_SET_XPC_300|
2394 MMC_CAP_SET_XPC_330;
2395
Sahitya Tummalacbe1b7a2013-02-28 12:21:58 +05302396 msm_host->mmc->caps |= MMC_CAP_HW_RESET;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302397 msm_host->mmc->caps2 |= msm_host->pdata->caps2;
Asutosh Dasbbc84782013-02-11 15:31:35 +05302398 msm_host->mmc->caps2 |= MMC_CAP2_CORE_RUNTIME_PM;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302399 msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR;
2400 msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR_CONTROL;
2401 msm_host->mmc->caps2 |= (MMC_CAP2_BOOTPART_NOACC |
2402 MMC_CAP2_DETECT_ON_ERR);
2403 msm_host->mmc->caps2 |= MMC_CAP2_SANITIZE;
2404 msm_host->mmc->caps2 |= MMC_CAP2_CACHE_CTRL;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302405 msm_host->mmc->caps2 |= MMC_CAP2_POWEROFF_NOTIFY;
Sahitya Tummala00240122013-02-28 19:50:51 +05302406 msm_host->mmc->caps2 |= MMC_CAP2_CLK_SCALE;
Konstantin Dorfmanfa436d52013-04-17 16:26:11 +03002407 msm_host->mmc->caps2 |= MMC_CAP2_STOP_REQUEST;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302408
2409 if (msm_host->pdata->nonremovable)
2410 msm_host->mmc->caps |= MMC_CAP_NONREMOVABLE;
2411
Sahitya Tummalab4e84042013-03-10 07:03:17 +05302412 host->cpu_dma_latency_us = msm_host->pdata->cpu_dma_latency_us;
2413
Sahitya Tummala179e7382013-03-20 19:24:01 +05302414 init_completion(&msm_host->pwr_irq_completion);
2415
Sahitya Tummala62448d92013-03-12 14:57:46 +05302416 if (gpio_is_valid(msm_host->pdata->status_gpio)) {
2417 ret = mmc_cd_gpio_request(msm_host->mmc,
2418 msm_host->pdata->status_gpio);
2419 if (ret) {
2420 dev_err(&pdev->dev, "%s: Failed to request card detection IRQ %d\n",
2421 __func__, ret);
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302422 goto vreg_deinit;
Sahitya Tummala62448d92013-03-12 14:57:46 +05302423 }
2424 }
2425
Sahitya Tummala2fa7eb12013-03-20 19:34:59 +05302426 if (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(32))) {
2427 host->dma_mask = DMA_BIT_MASK(32);
2428 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2429 } else {
2430 dev_err(&pdev->dev, "%s: Failed to set dma mask\n", __func__);
2431 }
2432
Asutosh Das33a4ff52012-12-18 16:14:02 +05302433 ret = sdhci_add_host(host);
2434 if (ret) {
2435 dev_err(&pdev->dev, "Add host failed (%d)\n", ret);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302436 goto free_cd_gpio;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302437 }
2438
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302439 msm_host->msm_bus_vote.max_bus_bw.show = show_sdhci_max_bus_bw;
2440 msm_host->msm_bus_vote.max_bus_bw.store = store_sdhci_max_bus_bw;
2441 sysfs_attr_init(&msm_host->msm_bus_vote.max_bus_bw.attr);
2442 msm_host->msm_bus_vote.max_bus_bw.attr.name = "max_bus_bw";
2443 msm_host->msm_bus_vote.max_bus_bw.attr.mode = S_IRUGO | S_IWUSR;
2444 ret = device_create_file(&pdev->dev,
2445 &msm_host->msm_bus_vote.max_bus_bw);
2446 if (ret)
2447 goto remove_host;
2448
Asutosh Dasbbc84782013-02-11 15:31:35 +05302449 ret = pm_runtime_set_active(&pdev->dev);
2450 if (ret)
2451 pr_err("%s: %s: pm_runtime_set_active failed: err: %d\n",
2452 mmc_hostname(host->mmc), __func__, ret);
2453 else
2454 pm_runtime_enable(&pdev->dev);
2455
Asutosh Das33a4ff52012-12-18 16:14:02 +05302456 /* Successful initialization */
2457 goto out;
2458
2459remove_host:
2460 dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
2461 sdhci_remove_host(host, dead);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302462free_cd_gpio:
2463 if (gpio_is_valid(msm_host->pdata->status_gpio))
2464 mmc_cd_gpio_free(msm_host->mmc);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302465vreg_deinit:
2466 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302467bus_unregister:
2468 if (msm_host->msm_bus_vote.client_handle)
2469 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2470 sdhci_msm_bus_unregister(msm_host);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302471clk_disable:
2472 if (!IS_ERR(msm_host->clk))
2473 clk_disable_unprepare(msm_host->clk);
2474pclk_disable:
2475 if (!IS_ERR(msm_host->pclk))
2476 clk_disable_unprepare(msm_host->pclk);
2477bus_clk_disable:
2478 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2479 clk_disable_unprepare(msm_host->bus_clk);
2480pltfm_free:
2481 sdhci_pltfm_free(pdev);
2482out:
2483 pr_debug("%s: Exit %s\n", dev_name(&pdev->dev), __func__);
2484 return ret;
2485}
2486
2487static int __devexit sdhci_msm_remove(struct platform_device *pdev)
2488{
2489 struct sdhci_host *host = platform_get_drvdata(pdev);
2490 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2491 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2492 struct sdhci_msm_pltfm_data *pdata = msm_host->pdata;
2493 int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) ==
2494 0xffffffff);
2495
2496 pr_debug("%s: %s\n", dev_name(&pdev->dev), __func__);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302497 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302498 sdhci_remove_host(host, dead);
Asutosh Dasbbc84782013-02-11 15:31:35 +05302499 pm_runtime_disable(&pdev->dev);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302500 sdhci_pltfm_free(pdev);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302501
2502 if (gpio_is_valid(msm_host->pdata->status_gpio))
2503 mmc_cd_gpio_free(msm_host->mmc);
2504
Asutosh Das33a4ff52012-12-18 16:14:02 +05302505 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302506
Asutosh Das33a4ff52012-12-18 16:14:02 +05302507 if (pdata->pin_data)
Asutosh Das390519d2012-12-21 12:21:42 +05302508 sdhci_msm_setup_pins(pdata, false);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302509
2510 if (msm_host->msm_bus_vote.client_handle) {
2511 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2512 sdhci_msm_bus_unregister(msm_host);
2513 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05302514 return 0;
2515}
2516
Asutosh Dasbbc84782013-02-11 15:31:35 +05302517static int sdhci_msm_runtime_suspend(struct device *dev)
2518{
2519 struct sdhci_host *host = dev_get_drvdata(dev);
2520 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2521 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2522
2523 disable_irq(host->irq);
2524 disable_irq(msm_host->pwr_irq);
2525
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302526 /*
2527 * Remove the vote immediately only if clocks are off in which
2528 * case we might have queued work to remove vote but it may not
2529 * be completed before runtime suspend or system suspend.
2530 */
2531 if (!atomic_read(&msm_host->clks_on)) {
2532 if (msm_host->msm_bus_vote.client_handle)
2533 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2534 }
2535
Asutosh Dasbbc84782013-02-11 15:31:35 +05302536 return 0;
2537}
2538
2539static int sdhci_msm_runtime_resume(struct device *dev)
2540{
2541 struct sdhci_host *host = dev_get_drvdata(dev);
2542 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2543 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2544
2545 enable_irq(msm_host->pwr_irq);
2546 enable_irq(host->irq);
2547
2548 return 0;
2549}
2550
2551#ifdef CONFIG_PM_SLEEP
2552
2553static int sdhci_msm_suspend(struct device *dev)
2554{
2555 struct sdhci_host *host = dev_get_drvdata(dev);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302556 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2557 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Dasbbc84782013-02-11 15:31:35 +05302558 int ret = 0;
2559
Sahitya Tummala62448d92013-03-12 14:57:46 +05302560 if (gpio_is_valid(msm_host->pdata->status_gpio))
2561 mmc_cd_gpio_free(msm_host->mmc);
2562
Asutosh Dasbbc84782013-02-11 15:31:35 +05302563 if (pm_runtime_suspended(dev)) {
2564 pr_debug("%s: %s: already runtime suspended\n",
2565 mmc_hostname(host->mmc), __func__);
2566 goto out;
2567 }
2568
2569 return sdhci_msm_runtime_suspend(dev);
2570out:
2571 return ret;
2572}
2573
2574static int sdhci_msm_resume(struct device *dev)
2575{
2576 struct sdhci_host *host = dev_get_drvdata(dev);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302577 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2578 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Dasbbc84782013-02-11 15:31:35 +05302579 int ret = 0;
2580
Sahitya Tummala62448d92013-03-12 14:57:46 +05302581 if (gpio_is_valid(msm_host->pdata->status_gpio)) {
2582 ret = mmc_cd_gpio_request(msm_host->mmc,
2583 msm_host->pdata->status_gpio);
2584 if (ret)
2585 pr_err("%s: %s: Failed to request card detection IRQ %d\n",
2586 mmc_hostname(host->mmc), __func__, ret);
2587 }
2588
Asutosh Dasbbc84782013-02-11 15:31:35 +05302589 if (pm_runtime_suspended(dev)) {
2590 pr_debug("%s: %s: runtime suspended, defer system resume\n",
2591 mmc_hostname(host->mmc), __func__);
2592 goto out;
2593 }
2594
2595 return sdhci_msm_runtime_resume(dev);
2596out:
2597 return ret;
2598}
2599#endif
2600
2601#ifdef CONFIG_PM
2602static const struct dev_pm_ops sdhci_msm_pmops = {
2603 SET_SYSTEM_SLEEP_PM_OPS(sdhci_msm_suspend, sdhci_msm_resume)
2604 SET_RUNTIME_PM_OPS(sdhci_msm_runtime_suspend, sdhci_msm_runtime_resume,
2605 NULL)
2606};
2607
2608#define SDHCI_MSM_PMOPS (&sdhci_msm_pmops)
2609
2610#else
2611#define SDHCI_PM_OPS NULL
2612#endif
Asutosh Das33a4ff52012-12-18 16:14:02 +05302613static const struct of_device_id sdhci_msm_dt_match[] = {
2614 {.compatible = "qcom,sdhci-msm"},
2615};
2616MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match);
2617
2618static struct platform_driver sdhci_msm_driver = {
2619 .probe = sdhci_msm_probe,
2620 .remove = __devexit_p(sdhci_msm_remove),
2621 .driver = {
2622 .name = "sdhci_msm",
2623 .owner = THIS_MODULE,
2624 .of_match_table = sdhci_msm_dt_match,
Asutosh Dasbbc84782013-02-11 15:31:35 +05302625 .pm = SDHCI_MSM_PMOPS,
Asutosh Das33a4ff52012-12-18 16:14:02 +05302626 },
2627};
2628
2629module_platform_driver(sdhci_msm_driver);
2630
2631MODULE_DESCRIPTION("Qualcomm Secure Digital Host Controller Interface driver");
2632MODULE_LICENSE("GPL v2");