blob: c7c8acb2a7e4f4810093fc846d3f9cfa40a75615 [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;
Subhash Jadavaniefb5f622013-05-28 18:21:57 +0530515 bool prev_pwrsave, curr_pwrsave;
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700516
517 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
518 spin_lock_irqsave(&host->lock, flags);
Subhash Jadavaniefb5f622013-05-28 18:21:57 +0530519 prev_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
520 CORE_CLK_PWRSAVE);
521 curr_pwrsave = prev_pwrsave;
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700522 /*
523 * Make sure that clock is always enabled when DLL
524 * tuning is in progress. Keeping PWRSAVE ON may
525 * turn off the clock. So let's disable the PWRSAVE
526 * here and re-enable it once tuning is completed.
527 */
Subhash Jadavaniefb5f622013-05-28 18:21:57 +0530528 if (prev_pwrsave) {
529 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
530 & ~CORE_CLK_PWRSAVE),
531 host->ioaddr + CORE_VENDOR_SPEC);
532 curr_pwrsave = false;
533 }
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700534
535 /* Write 1 to DLL_RST bit of DLL_CONFIG register */
536 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
537 | CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
538
539 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
540 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
541 | CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
542 msm_cm_dll_set_freq(host);
543
544 /* Write 0 to DLL_RST bit of DLL_CONFIG register */
545 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
546 & ~CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
547
548 /* Write 0 to DLL_PDN bit of DLL_CONFIG register */
549 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
550 & ~CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
551
552 /* Set DLL_EN bit to 1. */
553 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
554 | CORE_DLL_EN), host->ioaddr + CORE_DLL_CONFIG);
555
556 /* Set CK_OUT_EN bit to 1. */
557 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
558 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
559
560 wait_cnt = 50;
561 /* Wait until DLL_LOCK bit of DLL_STATUS register becomes '1' */
562 while (!(readl_relaxed(host->ioaddr + CORE_DLL_STATUS) &
563 CORE_DLL_LOCK)) {
564 /* max. wait for 50us sec for LOCK bit to be set */
565 if (--wait_cnt == 0) {
566 pr_err("%s: %s: DLL failed to LOCK\n",
567 mmc_hostname(mmc), __func__);
568 rc = -ETIMEDOUT;
569 goto out;
570 }
571 /* wait for 1us before polling again */
572 udelay(1);
573 }
574
575out:
Subhash Jadavaniefb5f622013-05-28 18:21:57 +0530576 /* Restore the correct PWRSAVE state */
577 if (prev_pwrsave ^ curr_pwrsave) {
578 u32 reg = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC);
579
580 if (prev_pwrsave)
581 reg |= CORE_CLK_PWRSAVE;
582 else
583 reg &= ~CORE_CLK_PWRSAVE;
584
585 writel_relaxed(reg, host->ioaddr + CORE_VENDOR_SPEC);
586 }
587
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700588 spin_unlock_irqrestore(&host->lock, flags);
589 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
590 return rc;
591}
592
593int sdhci_msm_execute_tuning(struct sdhci_host *host, u32 opcode)
594{
595 unsigned long flags;
596 u8 phase, *data_buf, tuned_phases[16], tuned_phase_cnt = 0;
597 const u32 *tuning_block_pattern = tuning_block_64;
598 int size = sizeof(tuning_block_64); /* Tuning pattern size in bytes */
599 int rc;
600 struct mmc_host *mmc = host->mmc;
Sahitya Tummala00240122013-02-28 19:50:51 +0530601 struct mmc_ios ios = host->mmc->ios;
602
603 /*
604 * Tuning is required for SDR104 and HS200 cards and if clock frequency
605 * is greater than 100MHz in these modes.
606 */
607 if (host->clock <= (100 * 1000 * 1000) ||
608 !(ios.timing == MMC_TIMING_MMC_HS200 ||
609 ios.timing == MMC_TIMING_UHS_SDR104))
610 return 0;
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700611
612 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700613 spin_lock_irqsave(&host->lock, flags);
614
615 if ((opcode == MMC_SEND_TUNING_BLOCK_HS200) &&
616 (mmc->ios.bus_width == MMC_BUS_WIDTH_8)) {
617 tuning_block_pattern = tuning_block_128;
618 size = sizeof(tuning_block_128);
619 }
620 spin_unlock_irqrestore(&host->lock, flags);
621
622 /* first of all reset the tuning block */
623 rc = msm_init_cm_dll(host);
624 if (rc)
625 goto out;
626
627 data_buf = kmalloc(size, GFP_KERNEL);
628 if (!data_buf) {
629 rc = -ENOMEM;
630 goto out;
631 }
632
633 phase = 0;
634 do {
635 struct mmc_command cmd = {0};
636 struct mmc_data data = {0};
637 struct mmc_request mrq = {
638 .cmd = &cmd,
639 .data = &data
640 };
641 struct scatterlist sg;
642
643 /* set the phase in delay line hw block */
644 rc = msm_config_cm_dll_phase(host, phase);
645 if (rc)
646 goto kfree;
647
648 cmd.opcode = opcode;
649 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
650
651 data.blksz = size;
652 data.blocks = 1;
653 data.flags = MMC_DATA_READ;
654 data.timeout_ns = 1000 * 1000 * 1000; /* 1 sec */
655
656 data.sg = &sg;
657 data.sg_len = 1;
658 sg_init_one(&sg, data_buf, size);
659 memset(data_buf, 0, size);
660 mmc_wait_for_req(mmc, &mrq);
661
662 if (!cmd.error && !data.error &&
663 !memcmp(data_buf, tuning_block_pattern, size)) {
664 /* tuning is successful at this tuning point */
665 tuned_phases[tuned_phase_cnt++] = phase;
666 pr_debug("%s: %s: found good phase = %d\n",
667 mmc_hostname(mmc), __func__, phase);
668 }
669 } while (++phase < 16);
670
671 if (tuned_phase_cnt) {
672 rc = msm_find_most_appropriate_phase(host, tuned_phases,
673 tuned_phase_cnt);
674 if (rc < 0)
675 goto kfree;
676 else
677 phase = (u8)rc;
678
679 /*
680 * Finally set the selected phase in delay
681 * line hw block.
682 */
683 rc = msm_config_cm_dll_phase(host, phase);
684 if (rc)
685 goto kfree;
686 pr_debug("%s: %s: finally setting the tuning phase to %d\n",
687 mmc_hostname(mmc), __func__, phase);
688 } else {
689 /* tuning failed */
690 pr_err("%s: %s: no tuning point found\n",
691 mmc_hostname(mmc), __func__);
692 rc = -EAGAIN;
693 }
694
695kfree:
696 kfree(data_buf);
697out:
698 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
699 return rc;
700}
701
Asutosh Das33a4ff52012-12-18 16:14:02 +0530702static int sdhci_msm_setup_gpio(struct sdhci_msm_pltfm_data *pdata, bool enable)
703{
704 struct sdhci_msm_gpio_data *curr;
705 int i, ret = 0;
706
707 curr = pdata->pin_data->gpio_data;
708 for (i = 0; i < curr->size; i++) {
709 if (!gpio_is_valid(curr->gpio[i].no)) {
710 ret = -EINVAL;
711 pr_err("%s: Invalid gpio = %d\n", __func__,
712 curr->gpio[i].no);
713 goto free_gpios;
714 }
715 if (enable) {
716 ret = gpio_request(curr->gpio[i].no,
717 curr->gpio[i].name);
718 if (ret) {
719 pr_err("%s: gpio_request(%d, %s) failed %d\n",
720 __func__, curr->gpio[i].no,
721 curr->gpio[i].name, ret);
722 goto free_gpios;
723 }
724 curr->gpio[i].is_enabled = true;
725 } else {
726 gpio_free(curr->gpio[i].no);
727 curr->gpio[i].is_enabled = false;
728 }
729 }
730 return ret;
731
732free_gpios:
733 for (i--; i >= 0; i--) {
734 gpio_free(curr->gpio[i].no);
735 curr->gpio[i].is_enabled = false;
736 }
737 return ret;
738}
739
Asutosh Das390519d2012-12-21 12:21:42 +0530740static int sdhci_msm_setup_pad(struct sdhci_msm_pltfm_data *pdata, bool enable)
741{
742 struct sdhci_msm_pad_data *curr;
743 int i;
744
745 curr = pdata->pin_data->pad_data;
746 for (i = 0; i < curr->drv->size; i++) {
747 if (enable)
748 msm_tlmm_set_hdrive(curr->drv->on[i].no,
749 curr->drv->on[i].val);
750 else
751 msm_tlmm_set_hdrive(curr->drv->off[i].no,
752 curr->drv->off[i].val);
753 }
754
755 for (i = 0; i < curr->pull->size; i++) {
756 if (enable)
757 msm_tlmm_set_pull(curr->pull->on[i].no,
758 curr->pull->on[i].val);
759 else
760 msm_tlmm_set_pull(curr->pull->off[i].no,
761 curr->pull->off[i].val);
762 }
763
764 return 0;
765}
766
Asutosh Das33a4ff52012-12-18 16:14:02 +0530767static int sdhci_msm_setup_pins(struct sdhci_msm_pltfm_data *pdata, bool enable)
768{
769 int ret = 0;
770
771 if (!pdata->pin_data || (pdata->pin_data->cfg_sts == enable))
772 return 0;
Asutosh Das390519d2012-12-21 12:21:42 +0530773 if (pdata->pin_data->is_gpio)
774 ret = sdhci_msm_setup_gpio(pdata, enable);
775 else
776 ret = sdhci_msm_setup_pad(pdata, enable);
Asutosh Das33a4ff52012-12-18 16:14:02 +0530777
Asutosh Das33a4ff52012-12-18 16:14:02 +0530778 if (!ret)
779 pdata->pin_data->cfg_sts = enable;
780
781 return ret;
782}
783
Asutosh Das390519d2012-12-21 12:21:42 +0530784static int sdhci_msm_dt_get_array(struct device *dev, const char *prop_name,
785 u32 **out, int *len, u32 size)
786{
787 int ret = 0;
788 struct device_node *np = dev->of_node;
789 size_t sz;
790 u32 *arr = NULL;
791
792 if (!of_get_property(np, prop_name, len)) {
793 ret = -EINVAL;
794 goto out;
795 }
796 sz = *len = *len / sizeof(*arr);
797 if (sz <= 0 || (size > 0 && (sz != size))) {
798 dev_err(dev, "%s invalid size\n", prop_name);
799 ret = -EINVAL;
800 goto out;
801 }
802
803 arr = devm_kzalloc(dev, sz * sizeof(*arr), GFP_KERNEL);
804 if (!arr) {
805 dev_err(dev, "%s failed allocating memory\n", prop_name);
806 ret = -ENOMEM;
807 goto out;
808 }
809
810 ret = of_property_read_u32_array(np, prop_name, arr, sz);
811 if (ret < 0) {
812 dev_err(dev, "%s failed reading array %d\n", prop_name, ret);
813 goto out;
814 }
815 *out = arr;
816out:
817 if (ret)
818 *len = 0;
819 return ret;
820}
821
Asutosh Das33a4ff52012-12-18 16:14:02 +0530822#define MAX_PROP_SIZE 32
823static int sdhci_msm_dt_parse_vreg_info(struct device *dev,
824 struct sdhci_msm_reg_data **vreg_data, const char *vreg_name)
825{
826 int len, ret = 0;
827 const __be32 *prop;
828 char prop_name[MAX_PROP_SIZE];
829 struct sdhci_msm_reg_data *vreg;
830 struct device_node *np = dev->of_node;
831
832 snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", vreg_name);
833 if (!of_parse_phandle(np, prop_name, 0)) {
834 dev_err(dev, "No vreg data found for %s\n", vreg_name);
835 ret = -EINVAL;
836 return ret;
837 }
838
839 vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
840 if (!vreg) {
841 dev_err(dev, "No memory for vreg: %s\n", vreg_name);
842 ret = -ENOMEM;
843 return ret;
844 }
845
846 vreg->name = vreg_name;
847
848 snprintf(prop_name, MAX_PROP_SIZE,
849 "qcom,%s-always-on", vreg_name);
850 if (of_get_property(np, prop_name, NULL))
851 vreg->is_always_on = true;
852
853 snprintf(prop_name, MAX_PROP_SIZE,
854 "qcom,%s-lpm-sup", vreg_name);
855 if (of_get_property(np, prop_name, NULL))
856 vreg->lpm_sup = true;
857
858 snprintf(prop_name, MAX_PROP_SIZE,
859 "qcom,%s-voltage-level", vreg_name);
860 prop = of_get_property(np, prop_name, &len);
861 if (!prop || (len != (2 * sizeof(__be32)))) {
862 dev_warn(dev, "%s %s property\n",
863 prop ? "invalid format" : "no", prop_name);
864 } else {
865 vreg->low_vol_level = be32_to_cpup(&prop[0]);
866 vreg->high_vol_level = be32_to_cpup(&prop[1]);
867 }
868
869 snprintf(prop_name, MAX_PROP_SIZE,
870 "qcom,%s-current-level", vreg_name);
871 prop = of_get_property(np, prop_name, &len);
872 if (!prop || (len != (2 * sizeof(__be32)))) {
873 dev_warn(dev, "%s %s property\n",
874 prop ? "invalid format" : "no", prop_name);
875 } else {
876 vreg->lpm_uA = be32_to_cpup(&prop[0]);
877 vreg->hpm_uA = be32_to_cpup(&prop[1]);
878 }
879
880 *vreg_data = vreg;
881 dev_dbg(dev, "%s: %s %s vol=[%d %d]uV, curr=[%d %d]uA\n",
882 vreg->name, vreg->is_always_on ? "always_on," : "",
883 vreg->lpm_sup ? "lpm_sup," : "", vreg->low_vol_level,
884 vreg->high_vol_level, vreg->lpm_uA, vreg->hpm_uA);
885
886 return ret;
887}
888
Asutosh Das390519d2012-12-21 12:21:42 +0530889/* GPIO/Pad data extraction */
890static int sdhci_msm_dt_get_pad_pull_info(struct device *dev, int id,
891 struct sdhci_msm_pad_pull_data **pad_pull_data)
892{
893 int ret = 0, base = 0, len, i;
894 u32 *tmp;
895 struct sdhci_msm_pad_pull_data *pull_data;
896 struct sdhci_msm_pad_pull *pull;
897
898 switch (id) {
899 case 1:
900 base = TLMM_PULL_SDC1_CLK;
901 break;
902 case 2:
903 base = TLMM_PULL_SDC2_CLK;
904 break;
905 case 3:
906 base = TLMM_PULL_SDC3_CLK;
907 break;
908 case 4:
909 base = TLMM_PULL_SDC4_CLK;
910 break;
911 default:
912 dev_err(dev, "%s: Invalid slot id\n", __func__);
913 ret = -EINVAL;
914 goto out;
915 }
916
917 pull_data = devm_kzalloc(dev, sizeof(struct sdhci_msm_pad_pull_data),
918 GFP_KERNEL);
919 if (!pull_data) {
920 dev_err(dev, "No memory for msm_mmc_pad_pull_data\n");
921 ret = -ENOMEM;
922 goto out;
923 }
924 pull_data->size = 3; /* array size for clk, cmd, data */
925
926 /* Allocate on, off configs for clk, cmd, data */
927 pull = devm_kzalloc(dev, 2 * pull_data->size *\
928 sizeof(struct sdhci_msm_pad_pull), GFP_KERNEL);
929 if (!pull) {
930 dev_err(dev, "No memory for msm_mmc_pad_pull\n");
931 ret = -ENOMEM;
932 goto out;
933 }
934 pull_data->on = pull;
935 pull_data->off = pull + pull_data->size;
936
937 ret = sdhci_msm_dt_get_array(dev, "qcom,pad-pull-on",
938 &tmp, &len, pull_data->size);
939 if (ret)
940 goto out;
941
942 for (i = 0; i < len; i++) {
943 pull_data->on[i].no = base + i;
944 pull_data->on[i].val = tmp[i];
945 dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
946 i, pull_data->on[i].val);
947 }
948
949 ret = sdhci_msm_dt_get_array(dev, "qcom,pad-pull-off",
950 &tmp, &len, pull_data->size);
951 if (ret)
952 goto out;
953
954 for (i = 0; i < len; i++) {
955 pull_data->off[i].no = base + i;
956 pull_data->off[i].val = tmp[i];
957 dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
958 i, pull_data->off[i].val);
959 }
960
961 *pad_pull_data = pull_data;
962out:
963 return ret;
964}
965
966static int sdhci_msm_dt_get_pad_drv_info(struct device *dev, int id,
967 struct sdhci_msm_pad_drv_data **pad_drv_data)
968{
969 int ret = 0, base = 0, len, i;
970 u32 *tmp;
971 struct sdhci_msm_pad_drv_data *drv_data;
972 struct sdhci_msm_pad_drv *drv;
973
974 switch (id) {
975 case 1:
976 base = TLMM_HDRV_SDC1_CLK;
977 break;
978 case 2:
979 base = TLMM_HDRV_SDC2_CLK;
980 break;
981 case 3:
982 base = TLMM_HDRV_SDC3_CLK;
983 break;
984 case 4:
985 base = TLMM_HDRV_SDC4_CLK;
986 break;
987 default:
988 dev_err(dev, "%s: Invalid slot id\n", __func__);
989 ret = -EINVAL;
990 goto out;
991 }
992
993 drv_data = devm_kzalloc(dev, sizeof(struct sdhci_msm_pad_drv_data),
994 GFP_KERNEL);
995 if (!drv_data) {
996 dev_err(dev, "No memory for msm_mmc_pad_drv_data\n");
997 ret = -ENOMEM;
998 goto out;
999 }
1000 drv_data->size = 3; /* array size for clk, cmd, data */
1001
1002 /* Allocate on, off configs for clk, cmd, data */
1003 drv = devm_kzalloc(dev, 2 * drv_data->size *\
1004 sizeof(struct sdhci_msm_pad_drv), GFP_KERNEL);
1005 if (!drv) {
1006 dev_err(dev, "No memory msm_mmc_pad_drv\n");
1007 ret = -ENOMEM;
1008 goto out;
1009 }
1010 drv_data->on = drv;
1011 drv_data->off = drv + drv_data->size;
1012
1013 ret = sdhci_msm_dt_get_array(dev, "qcom,pad-drv-on",
1014 &tmp, &len, drv_data->size);
1015 if (ret)
1016 goto out;
1017
1018 for (i = 0; i < len; i++) {
1019 drv_data->on[i].no = base + i;
1020 drv_data->on[i].val = tmp[i];
1021 dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
1022 i, drv_data->on[i].val);
1023 }
1024
1025 ret = sdhci_msm_dt_get_array(dev, "qcom,pad-drv-off",
1026 &tmp, &len, drv_data->size);
1027 if (ret)
1028 goto out;
1029
1030 for (i = 0; i < len; i++) {
1031 drv_data->off[i].no = base + i;
1032 drv_data->off[i].val = tmp[i];
1033 dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
1034 i, drv_data->off[i].val);
1035 }
1036
1037 *pad_drv_data = drv_data;
1038out:
1039 return ret;
1040}
1041
Asutosh Das33a4ff52012-12-18 16:14:02 +05301042#define GPIO_NAME_MAX_LEN 32
1043static int sdhci_msm_dt_parse_gpio_info(struct device *dev,
1044 struct sdhci_msm_pltfm_data *pdata)
1045{
Asutosh Das390519d2012-12-21 12:21:42 +05301046 int ret = 0, id = 0, cnt, i;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301047 struct sdhci_msm_pin_data *pin_data;
1048 struct device_node *np = dev->of_node;
1049
1050 pin_data = devm_kzalloc(dev, sizeof(*pin_data), GFP_KERNEL);
1051 if (!pin_data) {
1052 dev_err(dev, "No memory for pin_data\n");
1053 ret = -ENOMEM;
1054 goto out;
1055 }
1056
1057 cnt = of_gpio_count(np);
1058 if (cnt > 0) {
Asutosh Das390519d2012-12-21 12:21:42 +05301059 pin_data->is_gpio = true;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301060 pin_data->gpio_data = devm_kzalloc(dev,
1061 sizeof(struct sdhci_msm_gpio_data), GFP_KERNEL);
1062 if (!pin_data->gpio_data) {
1063 dev_err(dev, "No memory for gpio_data\n");
1064 ret = -ENOMEM;
1065 goto out;
1066 }
1067 pin_data->gpio_data->size = cnt;
1068 pin_data->gpio_data->gpio = devm_kzalloc(dev, cnt *
1069 sizeof(struct sdhci_msm_gpio), GFP_KERNEL);
1070
1071 if (!pin_data->gpio_data->gpio) {
1072 dev_err(dev, "No memory for gpio\n");
1073 ret = -ENOMEM;
1074 goto out;
1075 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301076 for (i = 0; i < cnt; i++) {
1077 const char *name = NULL;
1078 char result[GPIO_NAME_MAX_LEN];
1079 pin_data->gpio_data->gpio[i].no = of_get_gpio(np, i);
1080 of_property_read_string_index(np,
1081 "qcom,gpio-names", i, &name);
1082
1083 snprintf(result, GPIO_NAME_MAX_LEN, "%s-%s",
1084 dev_name(dev), name ? name : "?");
1085 pin_data->gpio_data->gpio[i].name = result;
1086 dev_dbg(dev, "%s: gpio[%s] = %d\n", __func__,
Asutosh Das390519d2012-12-21 12:21:42 +05301087 pin_data->gpio_data->gpio[i].name,
1088 pin_data->gpio_data->gpio[i].no);
Asutosh Das33a4ff52012-12-18 16:14:02 +05301089 }
Asutosh Das390519d2012-12-21 12:21:42 +05301090 } else {
1091 pin_data->pad_data =
1092 devm_kzalloc(dev,
1093 sizeof(struct sdhci_msm_pad_data),
1094 GFP_KERNEL);
1095 if (!pin_data->pad_data) {
1096 dev_err(dev,
1097 "No memory for pin_data->pad_data\n");
1098 ret = -ENOMEM;
1099 goto out;
1100 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301101
Asutosh Das390519d2012-12-21 12:21:42 +05301102 ret = of_alias_get_id(np, "sdhc");
1103 if (ret < 0) {
1104 dev_err(dev, "Failed to get slot index %d\n", ret);
1105 goto out;
1106 }
1107 id = ret;
1108
1109 ret = sdhci_msm_dt_get_pad_pull_info(
1110 dev, id, &pin_data->pad_data->pull);
1111 if (ret)
1112 goto out;
1113 ret = sdhci_msm_dt_get_pad_drv_info(
1114 dev, id, &pin_data->pad_data->drv);
1115 if (ret)
1116 goto out;
1117
1118 }
1119 pdata->pin_data = pin_data;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301120out:
1121 if (ret)
1122 dev_err(dev, "%s failed with err %d\n", __func__, ret);
1123 return ret;
1124}
1125
1126/* Parse platform data */
1127static struct sdhci_msm_pltfm_data *sdhci_msm_populate_pdata(struct device *dev)
1128{
1129 struct sdhci_msm_pltfm_data *pdata = NULL;
1130 struct device_node *np = dev->of_node;
1131 u32 bus_width = 0;
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301132 u32 cpu_dma_latency;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301133 int len, i;
Sahitya Tummala00240122013-02-28 19:50:51 +05301134 int clk_table_len;
1135 u32 *clk_table = NULL;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301136
1137 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1138 if (!pdata) {
1139 dev_err(dev, "failed to allocate memory for platform data\n");
1140 goto out;
1141 }
1142
Sahitya Tummala62448d92013-03-12 14:57:46 +05301143 pdata->status_gpio = of_get_named_gpio_flags(np, "cd-gpios", 0, 0);
1144
Asutosh Das33a4ff52012-12-18 16:14:02 +05301145 of_property_read_u32(np, "qcom,bus-width", &bus_width);
1146 if (bus_width == 8)
1147 pdata->mmc_bus_width = MMC_CAP_8_BIT_DATA;
1148 else if (bus_width == 4)
1149 pdata->mmc_bus_width = MMC_CAP_4_BIT_DATA;
1150 else {
1151 dev_notice(dev, "invalid bus-width, default to 1-bit mode\n");
1152 pdata->mmc_bus_width = 0;
1153 }
1154
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301155 if (!of_property_read_u32(np, "qcom,cpu-dma-latency-us",
1156 &cpu_dma_latency))
1157 pdata->cpu_dma_latency_us = cpu_dma_latency;
1158
Sahitya Tummala00240122013-02-28 19:50:51 +05301159 if (sdhci_msm_dt_get_array(dev, "qcom,clk-rates",
1160 &clk_table, &clk_table_len, 0)) {
1161 dev_err(dev, "failed parsing supported clock rates\n");
1162 goto out;
1163 }
1164 if (!clk_table || !clk_table_len) {
1165 dev_err(dev, "Invalid clock table\n");
1166 goto out;
1167 }
1168 pdata->sup_clk_table = clk_table;
1169 pdata->sup_clk_cnt = clk_table_len;
1170
Asutosh Das33a4ff52012-12-18 16:14:02 +05301171 pdata->vreg_data = devm_kzalloc(dev, sizeof(struct
1172 sdhci_msm_slot_reg_data),
1173 GFP_KERNEL);
1174 if (!pdata->vreg_data) {
1175 dev_err(dev, "failed to allocate memory for vreg data\n");
1176 goto out;
1177 }
1178
1179 if (sdhci_msm_dt_parse_vreg_info(dev, &pdata->vreg_data->vdd_data,
1180 "vdd")) {
1181 dev_err(dev, "failed parsing vdd data\n");
1182 goto out;
1183 }
1184 if (sdhci_msm_dt_parse_vreg_info(dev,
1185 &pdata->vreg_data->vdd_io_data,
1186 "vdd-io")) {
1187 dev_err(dev, "failed parsing vdd-io data\n");
1188 goto out;
1189 }
1190
1191 if (sdhci_msm_dt_parse_gpio_info(dev, pdata)) {
1192 dev_err(dev, "failed parsing gpio data\n");
1193 goto out;
1194 }
1195
Asutosh Das33a4ff52012-12-18 16:14:02 +05301196 len = of_property_count_strings(np, "qcom,bus-speed-mode");
1197
1198 for (i = 0; i < len; i++) {
1199 const char *name = NULL;
1200
1201 of_property_read_string_index(np,
1202 "qcom,bus-speed-mode", i, &name);
1203 if (!name)
1204 continue;
1205
1206 if (!strncmp(name, "HS200_1p8v", sizeof("HS200_1p8v")))
1207 pdata->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
1208 else if (!strncmp(name, "HS200_1p2v", sizeof("HS200_1p2v")))
1209 pdata->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
1210 else if (!strncmp(name, "DDR_1p8v", sizeof("DDR_1p8v")))
1211 pdata->caps |= MMC_CAP_1_8V_DDR
1212 | MMC_CAP_UHS_DDR50;
1213 else if (!strncmp(name, "DDR_1p2v", sizeof("DDR_1p2v")))
1214 pdata->caps |= MMC_CAP_1_2V_DDR
1215 | MMC_CAP_UHS_DDR50;
1216 }
1217
1218 if (of_get_property(np, "qcom,nonremovable", NULL))
1219 pdata->nonremovable = true;
1220
1221 return pdata;
1222out:
1223 return NULL;
1224}
1225
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301226/* Returns required bandwidth in Bytes per Sec */
1227static unsigned int sdhci_get_bw_required(struct sdhci_host *host,
1228 struct mmc_ios *ios)
1229{
Sahitya Tummala53aff982013-04-03 18:03:31 +05301230 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1231 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1232
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301233 unsigned int bw;
1234
Sahitya Tummala53aff982013-04-03 18:03:31 +05301235 bw = msm_host->clk_rate;
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301236 /*
1237 * For DDR mode, SDCC controller clock will be at
1238 * the double rate than the actual clock that goes to card.
1239 */
1240 if (ios->bus_width == MMC_BUS_WIDTH_4)
1241 bw /= 2;
1242 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1243 bw /= 8;
1244
1245 return bw;
1246}
1247
1248static int sdhci_msm_bus_get_vote_for_bw(struct sdhci_msm_host *host,
1249 unsigned int bw)
1250{
1251 unsigned int *table = host->pdata->voting_data->bw_vecs;
1252 unsigned int size = host->pdata->voting_data->bw_vecs_size;
1253 int i;
1254
1255 if (host->msm_bus_vote.is_max_bw_needed && bw)
1256 return host->msm_bus_vote.max_bw_vote;
1257
1258 for (i = 0; i < size; i++) {
1259 if (bw <= table[i])
1260 break;
1261 }
1262
1263 if (i && (i == size))
1264 i--;
1265
1266 return i;
1267}
1268
1269/*
1270 * This function must be called with host lock acquired.
1271 * Caller of this function should also ensure that msm bus client
1272 * handle is not null.
1273 */
1274static inline int sdhci_msm_bus_set_vote(struct sdhci_msm_host *msm_host,
1275 int vote,
1276 unsigned long flags)
1277{
1278 struct sdhci_host *host = platform_get_drvdata(msm_host->pdev);
1279 int rc = 0;
1280
1281 if (vote != msm_host->msm_bus_vote.curr_vote) {
1282 spin_unlock_irqrestore(&host->lock, flags);
1283 rc = msm_bus_scale_client_update_request(
1284 msm_host->msm_bus_vote.client_handle, vote);
1285 spin_lock_irqsave(&host->lock, flags);
1286 if (rc) {
1287 pr_err("%s: msm_bus_scale_client_update_request() failed: bus_client_handle=0x%x, vote=%d, err=%d\n",
1288 mmc_hostname(host->mmc),
1289 msm_host->msm_bus_vote.client_handle, vote, rc);
1290 goto out;
1291 }
1292 msm_host->msm_bus_vote.curr_vote = vote;
1293 }
1294out:
1295 return rc;
1296}
1297
1298/*
1299 * Internal work. Work to set 0 bandwidth for msm bus.
1300 */
1301static void sdhci_msm_bus_work(struct work_struct *work)
1302{
1303 struct sdhci_msm_host *msm_host;
1304 struct sdhci_host *host;
1305 unsigned long flags;
1306
1307 msm_host = container_of(work, struct sdhci_msm_host,
1308 msm_bus_vote.vote_work.work);
1309 host = platform_get_drvdata(msm_host->pdev);
1310
1311 if (!msm_host->msm_bus_vote.client_handle)
1312 return;
1313
1314 spin_lock_irqsave(&host->lock, flags);
1315 /* don't vote for 0 bandwidth if any request is in progress */
1316 if (!host->mrq) {
1317 sdhci_msm_bus_set_vote(msm_host,
1318 msm_host->msm_bus_vote.min_bw_vote, flags);
1319 } else
1320 pr_warning("%s: %s: Transfer in progress. skipping bus voting to 0 bandwidth\n",
1321 mmc_hostname(host->mmc), __func__);
1322 spin_unlock_irqrestore(&host->lock, flags);
1323}
1324
1325/*
1326 * This function cancels any scheduled delayed work and sets the bus
1327 * vote based on bw (bandwidth) argument.
1328 */
1329static void sdhci_msm_bus_cancel_work_and_set_vote(struct sdhci_host *host,
1330 unsigned int bw)
1331{
1332 int vote;
1333 unsigned long flags;
1334 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1335 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1336
1337 cancel_delayed_work_sync(&msm_host->msm_bus_vote.vote_work);
1338 spin_lock_irqsave(&host->lock, flags);
1339 vote = sdhci_msm_bus_get_vote_for_bw(msm_host, bw);
1340 sdhci_msm_bus_set_vote(msm_host, vote, flags);
1341 spin_unlock_irqrestore(&host->lock, flags);
1342}
1343
1344#define MSM_MMC_BUS_VOTING_DELAY 200 /* msecs */
1345
1346/* This function queues a work which will set the bandwidth requiement to 0 */
1347static void sdhci_msm_bus_queue_work(struct sdhci_host *host)
1348{
1349 unsigned long flags;
1350 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1351 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1352
1353 spin_lock_irqsave(&host->lock, flags);
1354 if (msm_host->msm_bus_vote.min_bw_vote !=
1355 msm_host->msm_bus_vote.curr_vote)
1356 queue_delayed_work(system_nrt_wq,
1357 &msm_host->msm_bus_vote.vote_work,
1358 msecs_to_jiffies(MSM_MMC_BUS_VOTING_DELAY));
1359 spin_unlock_irqrestore(&host->lock, flags);
1360}
1361
1362static int sdhci_msm_bus_register(struct sdhci_msm_host *host,
1363 struct platform_device *pdev)
1364{
1365 int rc = 0;
1366 struct msm_bus_scale_pdata *bus_pdata;
1367
1368 struct sdhci_msm_bus_voting_data *data;
1369 struct device *dev = &pdev->dev;
1370
1371 data = devm_kzalloc(dev,
1372 sizeof(struct sdhci_msm_bus_voting_data), GFP_KERNEL);
1373 if (!data) {
1374 dev_err(&pdev->dev,
1375 "%s: failed to allocate memory\n", __func__);
1376 rc = -ENOMEM;
1377 goto out;
1378 }
1379 data->bus_pdata = msm_bus_cl_get_pdata(pdev);
1380 if (data->bus_pdata) {
1381 rc = sdhci_msm_dt_get_array(dev, "qcom,bus-bw-vectors-bps",
1382 &data->bw_vecs, &data->bw_vecs_size, 0);
1383 if (rc) {
1384 dev_err(&pdev->dev,
1385 "%s: Failed to get bus-bw-vectors-bps\n",
1386 __func__);
1387 goto out;
1388 }
1389 host->pdata->voting_data = data;
1390 }
1391 if (host->pdata->voting_data &&
1392 host->pdata->voting_data->bus_pdata &&
1393 host->pdata->voting_data->bw_vecs &&
1394 host->pdata->voting_data->bw_vecs_size) {
1395
1396 bus_pdata = host->pdata->voting_data->bus_pdata;
1397 host->msm_bus_vote.client_handle =
1398 msm_bus_scale_register_client(bus_pdata);
1399 if (!host->msm_bus_vote.client_handle) {
1400 dev_err(&pdev->dev, "msm_bus_scale_register_client()\n");
1401 rc = -EFAULT;
1402 goto out;
1403 }
1404 /* cache the vote index for minimum and maximum bandwidth */
1405 host->msm_bus_vote.min_bw_vote =
1406 sdhci_msm_bus_get_vote_for_bw(host, 0);
1407 host->msm_bus_vote.max_bw_vote =
1408 sdhci_msm_bus_get_vote_for_bw(host, UINT_MAX);
1409 } else {
1410 devm_kfree(dev, data);
1411 }
1412
1413out:
1414 return rc;
1415}
1416
1417static void sdhci_msm_bus_unregister(struct sdhci_msm_host *host)
1418{
1419 if (host->msm_bus_vote.client_handle)
1420 msm_bus_scale_unregister_client(
1421 host->msm_bus_vote.client_handle);
1422}
1423
1424static void sdhci_msm_bus_voting(struct sdhci_host *host, u32 enable)
1425{
1426 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1427 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1428 struct mmc_ios *ios = &host->mmc->ios;
1429 unsigned int bw;
1430
1431 if (!msm_host->msm_bus_vote.client_handle)
1432 return;
1433
1434 bw = sdhci_get_bw_required(host, ios);
Sahitya Tummalac45ae732013-05-23 15:59:22 +05301435 if (enable) {
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301436 sdhci_msm_bus_cancel_work_and_set_vote(host, bw);
Sahitya Tummalac45ae732013-05-23 15:59:22 +05301437 } else {
1438 /*
1439 * If clock gating is enabled, then remove the vote
1440 * immediately because clocks will be disabled only
1441 * after SDHCI_MSM_MMC_CLK_GATE_DELAY and thus no
1442 * additional delay is required to remove the bus vote.
1443 */
1444 if (host->mmc->clkgate_delay)
1445 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
1446 else
1447 sdhci_msm_bus_queue_work(host);
1448 }
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301449}
1450
Asutosh Das33a4ff52012-12-18 16:14:02 +05301451/* Regulator utility functions */
1452static int sdhci_msm_vreg_init_reg(struct device *dev,
1453 struct sdhci_msm_reg_data *vreg)
1454{
1455 int ret = 0;
1456
1457 /* check if regulator is already initialized? */
1458 if (vreg->reg)
1459 goto out;
1460
1461 /* Get the regulator handle */
1462 vreg->reg = devm_regulator_get(dev, vreg->name);
1463 if (IS_ERR(vreg->reg)) {
1464 ret = PTR_ERR(vreg->reg);
1465 pr_err("%s: devm_regulator_get(%s) failed. ret=%d\n",
1466 __func__, vreg->name, ret);
1467 goto out;
1468 }
1469
1470 /* sanity check */
1471 if (!vreg->high_vol_level || !vreg->hpm_uA) {
1472 pr_err("%s: %s invalid constraints specified\n",
1473 __func__, vreg->name);
1474 ret = -EINVAL;
1475 }
1476
1477out:
1478 return ret;
1479}
1480
1481static void sdhci_msm_vreg_deinit_reg(struct sdhci_msm_reg_data *vreg)
1482{
1483 if (vreg->reg)
1484 devm_regulator_put(vreg->reg);
1485}
1486
1487static int sdhci_msm_vreg_set_optimum_mode(struct sdhci_msm_reg_data
1488 *vreg, int uA_load)
1489{
1490 int ret = 0;
1491
1492 /*
1493 * regulators that do not support regulator_set_voltage also
1494 * do not support regulator_set_optimum_mode
1495 */
1496 ret = regulator_set_optimum_mode(vreg->reg, uA_load);
1497 if (ret < 0)
1498 pr_err("%s: regulator_set_optimum_mode(reg=%s,uA_load=%d) failed. ret=%d\n",
1499 __func__, vreg->name, uA_load, ret);
1500 else
1501 /*
1502 * regulator_set_optimum_mode() can return non zero
1503 * value even for success case.
1504 */
1505 ret = 0;
1506 return ret;
1507}
1508
1509static int sdhci_msm_vreg_set_voltage(struct sdhci_msm_reg_data *vreg,
1510 int min_uV, int max_uV)
1511{
1512 int ret = 0;
1513
1514 ret = regulator_set_voltage(vreg->reg, min_uV, max_uV);
1515 if (ret) {
1516 pr_err("%s: regulator_set_voltage(%s)failed. min_uV=%d,max_uV=%d,ret=%d\n",
1517 __func__, vreg->name, min_uV, max_uV, ret);
1518 }
1519
1520 return ret;
1521}
1522
1523static int sdhci_msm_vreg_enable(struct sdhci_msm_reg_data *vreg)
1524{
1525 int ret = 0;
1526
1527 /* Put regulator in HPM (high power mode) */
1528 ret = sdhci_msm_vreg_set_optimum_mode(vreg, vreg->hpm_uA);
1529 if (ret < 0)
1530 return ret;
1531
1532 if (!vreg->is_enabled) {
1533 /* Set voltage level */
1534 ret = sdhci_msm_vreg_set_voltage(vreg, vreg->high_vol_level,
1535 vreg->high_vol_level);
1536 if (ret)
1537 return ret;
1538 }
1539 ret = regulator_enable(vreg->reg);
1540 if (ret) {
1541 pr_err("%s: regulator_enable(%s) failed. ret=%d\n",
1542 __func__, vreg->name, ret);
1543 return ret;
1544 }
1545 vreg->is_enabled = true;
1546 return ret;
1547}
1548
1549static int sdhci_msm_vreg_disable(struct sdhci_msm_reg_data *vreg)
1550{
1551 int ret = 0;
1552
1553 /* Never disable regulator marked as always_on */
1554 if (vreg->is_enabled && !vreg->is_always_on) {
1555 ret = regulator_disable(vreg->reg);
1556 if (ret) {
1557 pr_err("%s: regulator_disable(%s) failed. ret=%d\n",
1558 __func__, vreg->name, ret);
1559 goto out;
1560 }
1561 vreg->is_enabled = false;
1562
1563 ret = sdhci_msm_vreg_set_optimum_mode(vreg, 0);
1564 if (ret < 0)
1565 goto out;
1566
1567 /* Set min. voltage level to 0 */
1568 ret = sdhci_msm_vreg_set_voltage(vreg, 0, vreg->high_vol_level);
1569 if (ret)
1570 goto out;
1571 } else if (vreg->is_enabled && vreg->is_always_on) {
1572 if (vreg->lpm_sup) {
1573 /* Put always_on regulator in LPM (low power mode) */
1574 ret = sdhci_msm_vreg_set_optimum_mode(vreg,
1575 vreg->lpm_uA);
1576 if (ret < 0)
1577 goto out;
1578 }
1579 }
1580out:
1581 return ret;
1582}
1583
1584static int sdhci_msm_setup_vreg(struct sdhci_msm_pltfm_data *pdata,
1585 bool enable, bool is_init)
1586{
1587 int ret = 0, i;
1588 struct sdhci_msm_slot_reg_data *curr_slot;
1589 struct sdhci_msm_reg_data *vreg_table[2];
1590
1591 curr_slot = pdata->vreg_data;
1592 if (!curr_slot) {
1593 pr_debug("%s: vreg info unavailable,assuming the slot is powered by always on domain\n",
1594 __func__);
1595 goto out;
1596 }
1597
1598 vreg_table[0] = curr_slot->vdd_data;
1599 vreg_table[1] = curr_slot->vdd_io_data;
1600
1601 for (i = 0; i < ARRAY_SIZE(vreg_table); i++) {
1602 if (vreg_table[i]) {
1603 if (enable)
1604 ret = sdhci_msm_vreg_enable(vreg_table[i]);
1605 else
1606 ret = sdhci_msm_vreg_disable(vreg_table[i]);
1607 if (ret)
1608 goto out;
1609 }
1610 }
1611out:
1612 return ret;
1613}
1614
1615/*
1616 * Reset vreg by ensuring it is off during probe. A call
1617 * to enable vreg is needed to balance disable vreg
1618 */
1619static int sdhci_msm_vreg_reset(struct sdhci_msm_pltfm_data *pdata)
1620{
1621 int ret;
1622
1623 ret = sdhci_msm_setup_vreg(pdata, 1, true);
1624 if (ret)
1625 return ret;
1626 ret = sdhci_msm_setup_vreg(pdata, 0, true);
1627 return ret;
1628}
1629
1630/* This init function should be called only once for each SDHC slot */
1631static int sdhci_msm_vreg_init(struct device *dev,
1632 struct sdhci_msm_pltfm_data *pdata,
1633 bool is_init)
1634{
1635 int ret = 0;
1636 struct sdhci_msm_slot_reg_data *curr_slot;
1637 struct sdhci_msm_reg_data *curr_vdd_reg, *curr_vdd_io_reg;
1638
1639 curr_slot = pdata->vreg_data;
1640 if (!curr_slot)
1641 goto out;
1642
1643 curr_vdd_reg = curr_slot->vdd_data;
1644 curr_vdd_io_reg = curr_slot->vdd_io_data;
1645
1646 if (!is_init)
1647 /* Deregister all regulators from regulator framework */
1648 goto vdd_io_reg_deinit;
1649
1650 /*
1651 * Get the regulator handle from voltage regulator framework
1652 * and then try to set the voltage level for the regulator
1653 */
1654 if (curr_vdd_reg) {
1655 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_reg);
1656 if (ret)
1657 goto out;
1658 }
1659 if (curr_vdd_io_reg) {
1660 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_io_reg);
1661 if (ret)
1662 goto vdd_reg_deinit;
1663 }
1664 ret = sdhci_msm_vreg_reset(pdata);
1665 if (ret)
1666 dev_err(dev, "vreg reset failed (%d)\n", ret);
1667 goto out;
1668
1669vdd_io_reg_deinit:
1670 if (curr_vdd_io_reg)
1671 sdhci_msm_vreg_deinit_reg(curr_vdd_io_reg);
1672vdd_reg_deinit:
1673 if (curr_vdd_reg)
1674 sdhci_msm_vreg_deinit_reg(curr_vdd_reg);
1675out:
1676 return ret;
1677}
1678
1679
1680static int sdhci_msm_set_vdd_io_vol(struct sdhci_msm_pltfm_data *pdata,
1681 enum vdd_io_level level,
1682 unsigned int voltage_level)
1683{
1684 int ret = 0;
1685 int set_level;
1686 struct sdhci_msm_reg_data *vdd_io_reg;
1687
1688 if (!pdata->vreg_data)
1689 return ret;
1690
1691 vdd_io_reg = pdata->vreg_data->vdd_io_data;
1692 if (vdd_io_reg && vdd_io_reg->is_enabled) {
1693 switch (level) {
1694 case VDD_IO_LOW:
1695 set_level = vdd_io_reg->low_vol_level;
1696 break;
1697 case VDD_IO_HIGH:
1698 set_level = vdd_io_reg->high_vol_level;
1699 break;
1700 case VDD_IO_SET_LEVEL:
1701 set_level = voltage_level;
1702 break;
1703 default:
1704 pr_err("%s: invalid argument level = %d",
1705 __func__, level);
1706 ret = -EINVAL;
1707 return ret;
1708 }
1709 ret = sdhci_msm_vreg_set_voltage(vdd_io_reg, set_level,
1710 set_level);
1711 }
1712 return ret;
1713}
1714
1715static irqreturn_t sdhci_msm_pwr_irq(int irq, void *data)
1716{
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07001717 struct sdhci_host *host = (struct sdhci_host *)data;
1718 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1719 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301720 u8 irq_status = 0;
1721 u8 irq_ack = 0;
1722 int ret = 0;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301723 int pwr_state = 0, io_level = 0;
1724 unsigned long flags;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301725
1726 irq_status = readb_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
1727 pr_debug("%s: Received IRQ(%d), status=0x%x\n",
1728 mmc_hostname(msm_host->mmc), irq, irq_status);
1729
1730 /* Clear the interrupt */
1731 writeb_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
1732 /*
1733 * SDHC has core_mem and hc_mem device memory and these memory
1734 * addresses do not fall within 1KB region. Hence, any update to
1735 * core_mem address space would require an mb() to ensure this gets
1736 * completed before its next update to registers within hc_mem.
1737 */
1738 mb();
1739
1740 /* Handle BUS ON/OFF*/
1741 if (irq_status & CORE_PWRCTL_BUS_ON) {
1742 ret = sdhci_msm_setup_vreg(msm_host->pdata, true, false);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301743 if (!ret) {
Asutosh Das33a4ff52012-12-18 16:14:02 +05301744 ret = sdhci_msm_setup_pins(msm_host->pdata, true);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301745 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
1746 VDD_IO_HIGH, 0);
1747 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301748 if (ret)
1749 irq_ack |= CORE_PWRCTL_BUS_FAIL;
1750 else
1751 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301752
1753 pwr_state = REQ_BUS_ON;
1754 io_level = REQ_IO_HIGH;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301755 }
1756 if (irq_status & CORE_PWRCTL_BUS_OFF) {
1757 ret = sdhci_msm_setup_vreg(msm_host->pdata, false, false);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301758 if (!ret) {
Asutosh Das33a4ff52012-12-18 16:14:02 +05301759 ret = sdhci_msm_setup_pins(msm_host->pdata, false);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301760 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
1761 VDD_IO_LOW, 0);
1762 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301763 if (ret)
1764 irq_ack |= CORE_PWRCTL_BUS_FAIL;
1765 else
1766 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301767
1768 pwr_state = REQ_BUS_OFF;
1769 io_level = REQ_IO_LOW;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301770 }
1771 /* Handle IO LOW/HIGH */
1772 if (irq_status & CORE_PWRCTL_IO_LOW) {
1773 /* Switch voltage Low */
1774 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_LOW, 0);
1775 if (ret)
1776 irq_ack |= CORE_PWRCTL_IO_FAIL;
1777 else
1778 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301779
1780 io_level = REQ_IO_LOW;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301781 }
1782 if (irq_status & CORE_PWRCTL_IO_HIGH) {
1783 /* Switch voltage High */
1784 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_HIGH, 0);
1785 if (ret)
1786 irq_ack |= CORE_PWRCTL_IO_FAIL;
1787 else
1788 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301789
1790 io_level = REQ_IO_HIGH;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301791 }
1792
1793 /* ACK status to the core */
1794 writeb_relaxed(irq_ack, (msm_host->core_mem + CORE_PWRCTL_CTL));
1795 /*
1796 * SDHC has core_mem and hc_mem device memory and these memory
1797 * addresses do not fall within 1KB region. Hence, any update to
1798 * core_mem address space would require an mb() to ensure this gets
1799 * completed before its next update to registers within hc_mem.
1800 */
1801 mb();
1802
Sahitya Tummala179e7382013-03-20 19:24:01 +05301803 if (io_level & REQ_IO_HIGH)
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07001804 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
1805 ~CORE_IO_PAD_PWR_SWITCH),
1806 host->ioaddr + CORE_VENDOR_SPEC);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301807 else if (io_level & REQ_IO_LOW)
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07001808 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
1809 CORE_IO_PAD_PWR_SWITCH),
1810 host->ioaddr + CORE_VENDOR_SPEC);
1811 mb();
1812
Asutosh Das33a4ff52012-12-18 16:14:02 +05301813 pr_debug("%s: Handled IRQ(%d), ret=%d, ack=0x%x\n",
1814 mmc_hostname(msm_host->mmc), irq, ret, irq_ack);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301815 spin_lock_irqsave(&host->lock, flags);
1816 if (pwr_state)
1817 msm_host->curr_pwr_state = pwr_state;
1818 if (io_level)
1819 msm_host->curr_io_level = io_level;
1820 complete(&msm_host->pwr_irq_completion);
1821 spin_unlock_irqrestore(&host->lock, flags);
1822
Asutosh Das33a4ff52012-12-18 16:14:02 +05301823 return IRQ_HANDLED;
1824}
1825
1826/* This function returns the max. current supported by VDD rail in mA */
1827static unsigned int sdhci_msm_get_vreg_vdd_max_current(struct sdhci_msm_host
1828 *host)
1829{
1830 struct sdhci_msm_slot_reg_data *curr_slot = host->pdata->vreg_data;
1831 if (!curr_slot)
1832 return 0;
1833 if (curr_slot->vdd_data)
1834 return curr_slot->vdd_data->hpm_uA / 1000;
1835 else
1836 return 0;
1837}
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301838static ssize_t
1839show_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
1840 char *buf)
1841{
1842 struct sdhci_host *host = dev_get_drvdata(dev);
1843 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1844 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1845
1846 return snprintf(buf, PAGE_SIZE, "%u\n",
1847 msm_host->msm_bus_vote.is_max_bw_needed);
1848}
1849
1850static ssize_t
1851store_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
1852 const char *buf, size_t count)
1853{
1854 struct sdhci_host *host = dev_get_drvdata(dev);
1855 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1856 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1857 uint32_t value;
1858 unsigned long flags;
1859
1860 if (!kstrtou32(buf, 0, &value)) {
1861 spin_lock_irqsave(&host->lock, flags);
1862 msm_host->msm_bus_vote.is_max_bw_needed = !!value;
1863 spin_unlock_irqrestore(&host->lock, flags);
1864 }
1865 return count;
1866}
Asutosh Das33a4ff52012-12-18 16:14:02 +05301867
Sahitya Tummala179e7382013-03-20 19:24:01 +05301868static void sdhci_msm_check_power_status(struct sdhci_host *host, u32 req_type)
Asutosh Das33a4ff52012-12-18 16:14:02 +05301869{
1870 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1871 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301872 unsigned long flags;
1873 bool done = false;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301874
Sahitya Tummala179e7382013-03-20 19:24:01 +05301875 spin_lock_irqsave(&host->lock, flags);
1876 pr_debug("%s: %s: request %d curr_pwr_state %x curr_io_level %x\n",
1877 mmc_hostname(host->mmc), __func__, req_type,
1878 msm_host->curr_pwr_state, msm_host->curr_io_level);
1879 if ((req_type & msm_host->curr_pwr_state) ||
1880 (req_type & msm_host->curr_io_level))
1881 done = true;
1882 spin_unlock_irqrestore(&host->lock, flags);
Asutosh Das33a4ff52012-12-18 16:14:02 +05301883
Sahitya Tummala179e7382013-03-20 19:24:01 +05301884 /*
1885 * This is needed here to hanlde a case where IRQ gets
1886 * triggered even before this function is called so that
1887 * x->done counter of completion gets reset. Otherwise,
1888 * next call to wait_for_completion returns immediately
1889 * without actually waiting for the IRQ to be handled.
1890 */
1891 if (done)
1892 init_completion(&msm_host->pwr_irq_completion);
1893 else
1894 wait_for_completion(&msm_host->pwr_irq_completion);
1895
1896 pr_debug("%s: %s: request %d done\n", mmc_hostname(host->mmc),
1897 __func__, req_type);
Asutosh Das33a4ff52012-12-18 16:14:02 +05301898}
1899
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07001900static void sdhci_msm_toggle_cdr(struct sdhci_host *host, bool enable)
1901{
1902 if (enable)
1903 writel_relaxed((readl_relaxed(host->ioaddr +
1904 CORE_DLL_CONFIG) | CORE_CDR_EN),
1905 host->ioaddr + CORE_DLL_CONFIG);
1906 else
1907 writel_relaxed((readl_relaxed(host->ioaddr +
1908 CORE_DLL_CONFIG) & ~CORE_CDR_EN),
1909 host->ioaddr + CORE_DLL_CONFIG);
1910}
1911
Asutosh Das3781bd82013-01-10 21:11:04 +05301912static unsigned int sdhci_msm_max_segs(void)
1913{
1914 return SDHCI_MSM_MAX_SEGMENTS;
1915}
1916
Sahitya Tummala00240122013-02-28 19:50:51 +05301917static unsigned int sdhci_msm_get_min_clock(struct sdhci_host *host)
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301918{
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301919 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1920 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301921
Sahitya Tummala00240122013-02-28 19:50:51 +05301922 return msm_host->pdata->sup_clk_table[0];
1923}
1924
1925static unsigned int sdhci_msm_get_max_clock(struct sdhci_host *host)
1926{
1927 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1928 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1929 int max_clk_index = msm_host->pdata->sup_clk_cnt;
1930
1931 return msm_host->pdata->sup_clk_table[max_clk_index - 1];
1932}
1933
1934static unsigned int sdhci_msm_get_sup_clk_rate(struct sdhci_host *host,
1935 u32 req_clk)
1936{
1937 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1938 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1939 unsigned int sel_clk = -1;
1940 unsigned char cnt;
1941
1942 if (req_clk < sdhci_msm_get_min_clock(host)) {
1943 sel_clk = sdhci_msm_get_min_clock(host);
1944 return sel_clk;
1945 }
1946
1947 for (cnt = 0; cnt < msm_host->pdata->sup_clk_cnt; cnt++) {
1948 if (msm_host->pdata->sup_clk_table[cnt] > req_clk) {
1949 break;
1950 } else if (msm_host->pdata->sup_clk_table[cnt] == req_clk) {
1951 sel_clk = msm_host->pdata->sup_clk_table[cnt];
1952 break;
1953 } else {
1954 sel_clk = msm_host->pdata->sup_clk_table[cnt];
1955 }
1956 }
1957 return sel_clk;
1958}
1959
1960static int sdhci_msm_prepare_clocks(struct sdhci_host *host, bool enable)
1961{
1962 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1963 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1964 int rc = 0;
1965
1966 if (enable && !atomic_read(&msm_host->clks_on)) {
1967 pr_debug("%s: request to enable clocks\n",
1968 mmc_hostname(host->mmc));
Sahitya Tummalac45ae732013-05-23 15:59:22 +05301969
1970 sdhci_msm_bus_voting(host, 1);
1971
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301972 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
1973 rc = clk_prepare_enable(msm_host->bus_clk);
1974 if (rc) {
1975 pr_err("%s: %s: failed to enable the bus-clock with error %d\n",
1976 mmc_hostname(host->mmc), __func__, rc);
Sahitya Tummalac45ae732013-05-23 15:59:22 +05301977 goto remove_vote;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301978 }
1979 }
1980 if (!IS_ERR(msm_host->pclk)) {
1981 rc = clk_prepare_enable(msm_host->pclk);
1982 if (rc) {
1983 pr_err("%s: %s: failed to enable the pclk with error %d\n",
1984 mmc_hostname(host->mmc), __func__, rc);
1985 goto disable_bus_clk;
1986 }
1987 }
1988 rc = clk_prepare_enable(msm_host->clk);
1989 if (rc) {
1990 pr_err("%s: %s: failed to enable the host-clk with error %d\n",
1991 mmc_hostname(host->mmc), __func__, rc);
1992 goto disable_pclk;
1993 }
1994 mb();
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301995
Sahitya Tummala00240122013-02-28 19:50:51 +05301996 } else if (!enable && atomic_read(&msm_host->clks_on)) {
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301997 pr_debug("%s: request to disable clocks\n",
1998 mmc_hostname(host->mmc));
1999 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
2000 mb();
2001 clk_disable_unprepare(msm_host->clk);
2002 if (!IS_ERR(msm_host->pclk))
2003 clk_disable_unprepare(msm_host->pclk);
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 +05302006
2007 sdhci_msm_bus_voting(host, 0);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302008 }
Sahitya Tummala00240122013-02-28 19:50:51 +05302009 atomic_set(&msm_host->clks_on, enable);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302010 goto out;
2011disable_pclk:
2012 if (!IS_ERR_OR_NULL(msm_host->pclk))
2013 clk_disable_unprepare(msm_host->pclk);
2014disable_bus_clk:
2015 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2016 clk_disable_unprepare(msm_host->bus_clk);
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302017remove_vote:
2018 if (msm_host->msm_bus_vote.client_handle)
2019 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302020out:
Sahitya Tummala00240122013-02-28 19:50:51 +05302021 return rc;
2022}
2023
2024static void sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock)
2025{
2026 int rc;
2027 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2028 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2029 struct mmc_ios curr_ios = host->mmc->ios;
2030 u32 sup_clock, ddr_clock;
2031
2032 if (!clock) {
2033 sdhci_msm_prepare_clocks(host, false);
2034 host->clock = clock;
2035 return;
2036 }
2037
2038 rc = sdhci_msm_prepare_clocks(host, true);
2039 if (rc)
2040 return;
2041
2042 sup_clock = sdhci_msm_get_sup_clk_rate(host, clock);
2043 if (curr_ios.timing == MMC_TIMING_UHS_DDR50) {
2044 /*
2045 * The SDHC requires internal clock frequency to be double the
2046 * actual clock that will be set for DDR mode. The controller
2047 * uses the faster clock(100MHz) for some of its parts and send
2048 * the actual required clock (50MHz) to the card.
2049 */
2050 ddr_clock = clock * 2;
2051 sup_clock = sdhci_msm_get_sup_clk_rate(host,
2052 ddr_clock);
2053 }
2054 if (sup_clock != msm_host->clk_rate) {
2055 pr_debug("%s: %s: setting clk rate to %u\n",
2056 mmc_hostname(host->mmc), __func__, sup_clock);
2057 rc = clk_set_rate(msm_host->clk, sup_clock);
2058 if (rc) {
2059 pr_err("%s: %s: Failed to set rate %u for host-clk : %d\n",
2060 mmc_hostname(host->mmc), __func__,
2061 sup_clock, rc);
2062 return;
2063 }
2064 msm_host->clk_rate = sup_clock;
2065 host->clock = clock;
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302066 /*
2067 * Update the bus vote in case of frequency change due to
2068 * clock scaling.
2069 */
2070 sdhci_msm_bus_voting(host, 1);
Sahitya Tummala00240122013-02-28 19:50:51 +05302071 }
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302072}
2073
Sahitya Tummala5e4f9642013-03-21 11:13:25 +05302074static int sdhci_msm_set_uhs_signaling(struct sdhci_host *host,
2075 unsigned int uhs)
2076{
2077 u16 ctrl_2;
2078
2079 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2080 /* Select Bus Speed Mode for host */
2081 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
2082 if (uhs == MMC_TIMING_MMC_HS200)
2083 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2084 else if (uhs == MMC_TIMING_UHS_SDR12)
2085 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
2086 else if (uhs == MMC_TIMING_UHS_SDR25)
2087 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
2088 else if (uhs == MMC_TIMING_UHS_SDR50)
2089 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
2090 else if (uhs == MMC_TIMING_UHS_SDR104)
2091 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2092 else if (uhs == MMC_TIMING_UHS_DDR50)
2093 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Sahitya Tummala00240122013-02-28 19:50:51 +05302094 /*
2095 * When clock frquency is less than 100MHz, the feedback clock must be
2096 * provided and DLL must not be used so that tuning can be skipped. To
2097 * provide feedback clock, the mode selection can be any value less
2098 * than 3'b011 in bits [2:0] of HOST CONTROL2 register.
2099 */
2100 if (host->clock <= (100 * 1000 * 1000) &&
2101 (uhs == MMC_TIMING_MMC_HS200 ||
2102 uhs == MMC_TIMING_UHS_SDR104))
2103 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
2104
Sahitya Tummala5e4f9642013-03-21 11:13:25 +05302105 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
2106
2107 return 0;
2108}
2109
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002110/*
2111 * sdhci_msm_disable_data_xfer - disable undergoing AHB bus data transfer
2112 *
2113 * Write 0 to bit 0 in MCI_DATA_CTL (offset 0x2C) - clearing TxActive bit by
2114 * access to legacy registers. It will stop current burst and prevent start of
2115 * the next on.
2116 *
2117 * Polling CORE_AHB_DATA_DELAY_US timeout, by reading bit 13:12 until they are 0
2118 * in CORE_SDCC_DEBUG_REG (offset 0x124) will validate that AHB burst was
2119 * completed and a new one didn't start.
2120 *
2121 * Waiting for 4us while AHB finishes descriptors fetch.
2122 */
2123static void sdhci_msm_disable_data_xfer(struct sdhci_host *host)
2124{
2125 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2126 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2127 u32 value;
2128 int ret;
2129
2130 value = readl_relaxed(msm_host->core_mem + CORE_MCI_DATA_CTRL);
2131 value &= ~(u32)CORE_MCI_DPSM_ENABLE;
2132 writel_relaxed(value, msm_host->core_mem + CORE_MCI_DATA_CTRL);
2133
2134 /* Enable the test bus for device slot */
2135 writel_relaxed(CORE_TESTBUS_ENA | CORE_TESTBUS_SEL2,
2136 msm_host->core_mem + CORE_TESTBUS_CONFIG);
2137
2138 ret = readl_poll_timeout_noirq(msm_host->core_mem
2139 + CORE_SDCC_DEBUG_REG, value,
2140 !(value & CORE_DEBUG_REG_AHB_HTRANS),
2141 CORE_AHB_DATA_DELAY_US, 1);
2142 if (ret) {
2143 pr_err("%s: %s: can't stop ongoing AHB bus access by ADMA\n",
2144 mmc_hostname(host->mmc), __func__);
2145 BUG();
2146 }
2147 /* Disable the test bus for device slot */
2148 value = readl_relaxed(msm_host->core_mem + CORE_TESTBUS_CONFIG);
2149 value &= ~CORE_TESTBUS_ENA;
2150 writel_relaxed(value, msm_host->core_mem + CORE_TESTBUS_CONFIG);
2151
2152 udelay(CORE_AHB_DESC_DELAY_US);
2153}
2154
Asutosh Das33a4ff52012-12-18 16:14:02 +05302155static struct sdhci_ops sdhci_msm_ops = {
Sahitya Tummala5e4f9642013-03-21 11:13:25 +05302156 .set_uhs_signaling = sdhci_msm_set_uhs_signaling,
Asutosh Das33a4ff52012-12-18 16:14:02 +05302157 .check_power_status = sdhci_msm_check_power_status,
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07002158 .execute_tuning = sdhci_msm_execute_tuning,
2159 .toggle_cdr = sdhci_msm_toggle_cdr,
Asutosh Das3781bd82013-01-10 21:11:04 +05302160 .get_max_segments = sdhci_msm_max_segs,
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302161 .set_clock = sdhci_msm_set_clock,
Sahitya Tummala00240122013-02-28 19:50:51 +05302162 .get_min_clock = sdhci_msm_get_min_clock,
2163 .get_max_clock = sdhci_msm_get_max_clock,
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002164 .disable_data_xfer = sdhci_msm_disable_data_xfer,
Asutosh Das33a4ff52012-12-18 16:14:02 +05302165};
2166
2167static int __devinit sdhci_msm_probe(struct platform_device *pdev)
2168{
2169 struct sdhci_host *host;
2170 struct sdhci_pltfm_host *pltfm_host;
2171 struct sdhci_msm_host *msm_host;
2172 struct resource *core_memres = NULL;
Asutosh Dasbbc84782013-02-11 15:31:35 +05302173 int ret = 0, dead = 0;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302174 u32 vdd_max_current;
Stephen Boyd3edbd8f2013-04-24 14:19:46 -07002175 u16 host_version;
Subhash Jadavanic08d2062013-05-14 17:46:43 +05302176 u32 pwr, irq_status, irq_ctl;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302177
2178 pr_debug("%s: Enter %s\n", dev_name(&pdev->dev), __func__);
2179 msm_host = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_msm_host),
2180 GFP_KERNEL);
2181 if (!msm_host) {
2182 ret = -ENOMEM;
2183 goto out;
2184 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05302185
2186 msm_host->sdhci_msm_pdata.ops = &sdhci_msm_ops;
2187 host = sdhci_pltfm_init(pdev, &msm_host->sdhci_msm_pdata);
2188 if (IS_ERR(host)) {
2189 ret = PTR_ERR(host);
2190 goto out;
2191 }
2192
2193 pltfm_host = sdhci_priv(host);
2194 pltfm_host->priv = msm_host;
2195 msm_host->mmc = host->mmc;
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302196 msm_host->pdev = pdev;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302197
2198 /* Extract platform data */
2199 if (pdev->dev.of_node) {
Venkat Gopalakrishnanc61ab7e2013-03-11 12:17:57 -07002200 ret = of_alias_get_id(pdev->dev.of_node, "sdhc");
2201 if (ret < 0) {
2202 dev_err(&pdev->dev, "Failed to get slot index %d\n",
2203 ret);
2204 goto pltfm_free;
2205 }
2206 if (disable_slots & (1 << (ret - 1))) {
2207 dev_info(&pdev->dev, "%s: Slot %d disabled\n", __func__,
2208 ret);
2209 ret = -ENODEV;
2210 goto pltfm_free;
2211 }
2212
Asutosh Das33a4ff52012-12-18 16:14:02 +05302213 msm_host->pdata = sdhci_msm_populate_pdata(&pdev->dev);
2214 if (!msm_host->pdata) {
2215 dev_err(&pdev->dev, "DT parsing error\n");
2216 goto pltfm_free;
2217 }
2218 } else {
2219 dev_err(&pdev->dev, "No device tree node\n");
2220 goto pltfm_free;
2221 }
2222
2223 /* Setup Clocks */
2224
2225 /* Setup SDCC bus voter clock. */
2226 msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus_clk");
2227 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
2228 /* Vote for max. clk rate for max. performance */
2229 ret = clk_set_rate(msm_host->bus_clk, INT_MAX);
2230 if (ret)
2231 goto pltfm_free;
2232 ret = clk_prepare_enable(msm_host->bus_clk);
2233 if (ret)
2234 goto pltfm_free;
2235 }
2236
2237 /* Setup main peripheral bus clock */
2238 msm_host->pclk = devm_clk_get(&pdev->dev, "iface_clk");
2239 if (!IS_ERR(msm_host->pclk)) {
2240 ret = clk_prepare_enable(msm_host->pclk);
2241 if (ret)
2242 goto bus_clk_disable;
2243 }
2244
2245 /* Setup SDC MMC clock */
2246 msm_host->clk = devm_clk_get(&pdev->dev, "core_clk");
2247 if (IS_ERR(msm_host->clk)) {
2248 ret = PTR_ERR(msm_host->clk);
2249 goto pclk_disable;
2250 }
2251
2252 ret = clk_prepare_enable(msm_host->clk);
2253 if (ret)
2254 goto pclk_disable;
2255
Sahitya Tummala00240122013-02-28 19:50:51 +05302256 /* Set to the minimum supported clock frequency */
2257 ret = clk_set_rate(msm_host->clk, sdhci_msm_get_min_clock(host));
2258 if (ret) {
2259 dev_err(&pdev->dev, "MClk rate set failed (%d)\n", ret);
2260 goto clk_disable;
2261 }
2262 msm_host->clk_rate = sdhci_msm_get_min_clock(host);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302263 atomic_set(&msm_host->clks_on, 1);
Sahitya Tummala00240122013-02-28 19:50:51 +05302264
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302265 ret = sdhci_msm_bus_register(msm_host, pdev);
2266 if (ret)
2267 goto clk_disable;
2268
2269 if (msm_host->msm_bus_vote.client_handle)
2270 INIT_DELAYED_WORK(&msm_host->msm_bus_vote.vote_work,
2271 sdhci_msm_bus_work);
2272 sdhci_msm_bus_voting(host, 1);
2273
Asutosh Das33a4ff52012-12-18 16:14:02 +05302274 /* Setup regulators */
2275 ret = sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, true);
2276 if (ret) {
2277 dev_err(&pdev->dev, "Regulator setup failed (%d)\n", ret);
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302278 goto bus_unregister;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302279 }
2280
2281 /* Reset the core and Enable SDHC mode */
2282 core_memres = platform_get_resource_byname(pdev,
2283 IORESOURCE_MEM, "core_mem");
2284 msm_host->core_mem = devm_ioremap(&pdev->dev, core_memres->start,
2285 resource_size(core_memres));
2286
2287 if (!msm_host->core_mem) {
2288 dev_err(&pdev->dev, "Failed to remap registers\n");
2289 ret = -ENOMEM;
2290 goto vreg_deinit;
2291 }
2292
2293 /* Set SW_RST bit in POWER register (Offset 0x0) */
Sahitya Tummalad5d76e72013-04-25 11:50:56 +05302294 writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_POWER) |
2295 CORE_SW_RST, msm_host->core_mem + CORE_POWER);
2296 /*
2297 * SW reset can take upto 10HCLK + 15MCLK cycles.
2298 * Calculating based on min clk rates (hclk = 27MHz,
2299 * mclk = 400KHz) it comes to ~40us. Let's poll for
2300 * max. 1ms for reset completion.
2301 */
2302 ret = readl_poll_timeout(msm_host->core_mem + CORE_POWER,
2303 pwr, !(pwr & CORE_SW_RST), 100, 10);
2304
2305 if (ret) {
2306 dev_err(&pdev->dev, "reset failed (%d)\n", ret);
2307 goto vreg_deinit;
2308 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05302309 /* Set HC_MODE_EN bit in HC_MODE register */
2310 writel_relaxed(HC_MODE_EN, (msm_host->core_mem + CORE_HC_MODE));
2311
2312 /*
Subhash Jadavanic08d2062013-05-14 17:46:43 +05302313 * CORE_SW_RST above may trigger power irq if previous status of PWRCTL
2314 * was either BUS_ON or IO_HIGH_V. So before we enable the power irq
2315 * interrupt in GIC (by registering the interrupt handler), we need to
2316 * ensure that any pending power irq interrupt status is acknowledged
2317 * otherwise power irq interrupt handler would be fired prematurely.
2318 */
2319 irq_status = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
2320 writel_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
2321 irq_ctl = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_CTL);
2322 if (irq_status & (CORE_PWRCTL_BUS_ON | CORE_PWRCTL_BUS_OFF))
2323 irq_ctl |= CORE_PWRCTL_BUS_SUCCESS;
2324 if (irq_status & (CORE_PWRCTL_IO_HIGH | CORE_PWRCTL_IO_LOW))
2325 irq_ctl |= CORE_PWRCTL_IO_SUCCESS;
2326 writel_relaxed(irq_ctl, (msm_host->core_mem + CORE_PWRCTL_CTL));
2327 /*
2328 * Ensure that above writes are propogated before interrupt enablement
2329 * in GIC.
2330 */
2331 mb();
2332
2333 /*
Asutosh Das33a4ff52012-12-18 16:14:02 +05302334 * Following are the deviations from SDHC spec v3.0 -
2335 * 1. Card detection is handled using separate GPIO.
2336 * 2. Bus power control is handled by interacting with PMIC.
2337 */
2338 host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
2339 host->quirks |= SDHCI_QUIRK_SINGLE_POWER_WRITE;
Sahitya Tummala00240122013-02-28 19:50:51 +05302340 host->quirks |= SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN;
2341 host->quirks2 |= SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK;
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302342 host->quirks2 |= SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING;
Krishna Kondaa20d3362013-04-01 21:01:59 -07002343 host->quirks2 |= SDHCI_QUIRK2_USE_MAX_DISCARD_SIZE;
Sahitya Tummalad2ae8832013-04-12 11:49:11 +05302344 host->quirks2 |= SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD;
Sahitya Tummalae6886bd2013-04-12 12:11:20 +05302345 host->quirks2 |= SDHCI_QUIRK2_BROKEN_PRESET_VALUE;
Sahitya Tummala4d12d0b2013-04-12 11:59:25 +05302346 host->quirks2 |= SDHCI_QUIRK2_USE_RESERVED_MAX_TIMEOUT;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302347
Stephen Boyd3edbd8f2013-04-24 14:19:46 -07002348 host_version = readw_relaxed((host->ioaddr + SDHCI_HOST_VERSION));
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002349 dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n",
2350 host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >>
2351 SDHCI_VENDOR_VER_SHIFT));
2352 if (((host_version & SDHCI_VENDOR_VER_MASK) >>
2353 SDHCI_VENDOR_VER_SHIFT) == SDHCI_VER_100) {
2354 /*
2355 * Add 40us delay in interrupt handler when
2356 * operating at initialization frequency(400KHz).
2357 */
2358 host->quirks2 |= SDHCI_QUIRK2_SLOW_INT_CLR;
2359 /*
2360 * Set Software Reset for DAT line in Software
2361 * Reset Register (Bit 2).
2362 */
2363 host->quirks2 |= SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT;
2364 }
2365
2366 /* Setup PWRCTL irq */
Asutosh Dasbbc84782013-02-11 15:31:35 +05302367 msm_host->pwr_irq = platform_get_irq_byname(pdev, "pwr_irq");
2368 if (msm_host->pwr_irq < 0) {
Asutosh Das33a4ff52012-12-18 16:14:02 +05302369 dev_err(&pdev->dev, "Failed to get pwr_irq by name (%d)\n",
Asutosh Dasbbc84782013-02-11 15:31:35 +05302370 msm_host->pwr_irq);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302371 goto vreg_deinit;
2372 }
Asutosh Dasbbc84782013-02-11 15:31:35 +05302373 ret = devm_request_threaded_irq(&pdev->dev, msm_host->pwr_irq, NULL,
Asutosh Das33a4ff52012-12-18 16:14:02 +05302374 sdhci_msm_pwr_irq, IRQF_ONESHOT,
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07002375 dev_name(&pdev->dev), host);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302376 if (ret) {
2377 dev_err(&pdev->dev, "Request threaded irq(%d) failed (%d)\n",
Asutosh Dasbbc84782013-02-11 15:31:35 +05302378 msm_host->pwr_irq, ret);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302379 goto vreg_deinit;
2380 }
2381
2382 /* Enable pwr irq interrupts */
2383 writel_relaxed(INT_MASK, (msm_host->core_mem + CORE_PWRCTL_MASK));
2384
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302385 /* Set clock gating delay to be used when CONFIG_MMC_CLKGATE is set */
2386 msm_host->mmc->clkgate_delay = SDHCI_MSM_MMC_CLK_GATE_DELAY;
2387
Asutosh Das33a4ff52012-12-18 16:14:02 +05302388 /* Set host capabilities */
2389 msm_host->mmc->caps |= msm_host->pdata->mmc_bus_width;
2390 msm_host->mmc->caps |= msm_host->pdata->caps;
2391
2392 vdd_max_current = sdhci_msm_get_vreg_vdd_max_current(msm_host);
2393 if (vdd_max_current >= 800)
2394 msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_800;
2395 else if (vdd_max_current >= 600)
2396 msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_600;
2397 else if (vdd_max_current >= 400)
2398 msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_400;
2399 else
2400 msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_200;
2401
2402 if (vdd_max_current > 150)
2403 msm_host->mmc->caps |= MMC_CAP_SET_XPC_180 |
2404 MMC_CAP_SET_XPC_300|
2405 MMC_CAP_SET_XPC_330;
2406
Sahitya Tummalacbe1b7a2013-02-28 12:21:58 +05302407 msm_host->mmc->caps |= MMC_CAP_HW_RESET;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302408 msm_host->mmc->caps2 |= msm_host->pdata->caps2;
Asutosh Dasbbc84782013-02-11 15:31:35 +05302409 msm_host->mmc->caps2 |= MMC_CAP2_CORE_RUNTIME_PM;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302410 msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR;
2411 msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR_CONTROL;
2412 msm_host->mmc->caps2 |= (MMC_CAP2_BOOTPART_NOACC |
2413 MMC_CAP2_DETECT_ON_ERR);
2414 msm_host->mmc->caps2 |= MMC_CAP2_SANITIZE;
2415 msm_host->mmc->caps2 |= MMC_CAP2_CACHE_CTRL;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302416 msm_host->mmc->caps2 |= MMC_CAP2_POWEROFF_NOTIFY;
Sahitya Tummala00240122013-02-28 19:50:51 +05302417 msm_host->mmc->caps2 |= MMC_CAP2_CLK_SCALE;
Konstantin Dorfmanfa436d52013-04-17 16:26:11 +03002418 msm_host->mmc->caps2 |= MMC_CAP2_STOP_REQUEST;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302419
2420 if (msm_host->pdata->nonremovable)
2421 msm_host->mmc->caps |= MMC_CAP_NONREMOVABLE;
2422
Sahitya Tummalab4e84042013-03-10 07:03:17 +05302423 host->cpu_dma_latency_us = msm_host->pdata->cpu_dma_latency_us;
2424
Sahitya Tummala179e7382013-03-20 19:24:01 +05302425 init_completion(&msm_host->pwr_irq_completion);
2426
Sahitya Tummala62448d92013-03-12 14:57:46 +05302427 if (gpio_is_valid(msm_host->pdata->status_gpio)) {
2428 ret = mmc_cd_gpio_request(msm_host->mmc,
2429 msm_host->pdata->status_gpio);
2430 if (ret) {
2431 dev_err(&pdev->dev, "%s: Failed to request card detection IRQ %d\n",
2432 __func__, ret);
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302433 goto vreg_deinit;
Sahitya Tummala62448d92013-03-12 14:57:46 +05302434 }
2435 }
2436
Sahitya Tummala2fa7eb12013-03-20 19:34:59 +05302437 if (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(32))) {
2438 host->dma_mask = DMA_BIT_MASK(32);
2439 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2440 } else {
2441 dev_err(&pdev->dev, "%s: Failed to set dma mask\n", __func__);
2442 }
2443
Asutosh Das33a4ff52012-12-18 16:14:02 +05302444 ret = sdhci_add_host(host);
2445 if (ret) {
2446 dev_err(&pdev->dev, "Add host failed (%d)\n", ret);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302447 goto free_cd_gpio;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302448 }
2449
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302450 msm_host->msm_bus_vote.max_bus_bw.show = show_sdhci_max_bus_bw;
2451 msm_host->msm_bus_vote.max_bus_bw.store = store_sdhci_max_bus_bw;
2452 sysfs_attr_init(&msm_host->msm_bus_vote.max_bus_bw.attr);
2453 msm_host->msm_bus_vote.max_bus_bw.attr.name = "max_bus_bw";
2454 msm_host->msm_bus_vote.max_bus_bw.attr.mode = S_IRUGO | S_IWUSR;
2455 ret = device_create_file(&pdev->dev,
2456 &msm_host->msm_bus_vote.max_bus_bw);
2457 if (ret)
2458 goto remove_host;
2459
Asutosh Dasbbc84782013-02-11 15:31:35 +05302460 ret = pm_runtime_set_active(&pdev->dev);
2461 if (ret)
2462 pr_err("%s: %s: pm_runtime_set_active failed: err: %d\n",
2463 mmc_hostname(host->mmc), __func__, ret);
2464 else
2465 pm_runtime_enable(&pdev->dev);
2466
Asutosh Das33a4ff52012-12-18 16:14:02 +05302467 /* Successful initialization */
2468 goto out;
2469
2470remove_host:
2471 dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
2472 sdhci_remove_host(host, dead);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302473free_cd_gpio:
2474 if (gpio_is_valid(msm_host->pdata->status_gpio))
2475 mmc_cd_gpio_free(msm_host->mmc);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302476vreg_deinit:
2477 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302478bus_unregister:
2479 if (msm_host->msm_bus_vote.client_handle)
2480 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2481 sdhci_msm_bus_unregister(msm_host);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302482clk_disable:
2483 if (!IS_ERR(msm_host->clk))
2484 clk_disable_unprepare(msm_host->clk);
2485pclk_disable:
2486 if (!IS_ERR(msm_host->pclk))
2487 clk_disable_unprepare(msm_host->pclk);
2488bus_clk_disable:
2489 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2490 clk_disable_unprepare(msm_host->bus_clk);
2491pltfm_free:
2492 sdhci_pltfm_free(pdev);
2493out:
2494 pr_debug("%s: Exit %s\n", dev_name(&pdev->dev), __func__);
2495 return ret;
2496}
2497
2498static int __devexit sdhci_msm_remove(struct platform_device *pdev)
2499{
2500 struct sdhci_host *host = platform_get_drvdata(pdev);
2501 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2502 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2503 struct sdhci_msm_pltfm_data *pdata = msm_host->pdata;
2504 int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) ==
2505 0xffffffff);
2506
2507 pr_debug("%s: %s\n", dev_name(&pdev->dev), __func__);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302508 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302509 sdhci_remove_host(host, dead);
Asutosh Dasbbc84782013-02-11 15:31:35 +05302510 pm_runtime_disable(&pdev->dev);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302511 sdhci_pltfm_free(pdev);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302512
2513 if (gpio_is_valid(msm_host->pdata->status_gpio))
2514 mmc_cd_gpio_free(msm_host->mmc);
2515
Asutosh Das33a4ff52012-12-18 16:14:02 +05302516 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302517
Asutosh Das33a4ff52012-12-18 16:14:02 +05302518 if (pdata->pin_data)
Asutosh Das390519d2012-12-21 12:21:42 +05302519 sdhci_msm_setup_pins(pdata, false);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302520
2521 if (msm_host->msm_bus_vote.client_handle) {
2522 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2523 sdhci_msm_bus_unregister(msm_host);
2524 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05302525 return 0;
2526}
2527
Asutosh Dasbbc84782013-02-11 15:31:35 +05302528static int sdhci_msm_runtime_suspend(struct device *dev)
2529{
2530 struct sdhci_host *host = dev_get_drvdata(dev);
2531 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2532 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2533
2534 disable_irq(host->irq);
2535 disable_irq(msm_host->pwr_irq);
2536
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302537 /*
2538 * Remove the vote immediately only if clocks are off in which
2539 * case we might have queued work to remove vote but it may not
2540 * be completed before runtime suspend or system suspend.
2541 */
2542 if (!atomic_read(&msm_host->clks_on)) {
2543 if (msm_host->msm_bus_vote.client_handle)
2544 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2545 }
2546
Asutosh Dasbbc84782013-02-11 15:31:35 +05302547 return 0;
2548}
2549
2550static int sdhci_msm_runtime_resume(struct device *dev)
2551{
2552 struct sdhci_host *host = dev_get_drvdata(dev);
2553 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2554 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2555
2556 enable_irq(msm_host->pwr_irq);
2557 enable_irq(host->irq);
2558
2559 return 0;
2560}
2561
2562#ifdef CONFIG_PM_SLEEP
2563
2564static int sdhci_msm_suspend(struct device *dev)
2565{
2566 struct sdhci_host *host = dev_get_drvdata(dev);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302567 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2568 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Dasbbc84782013-02-11 15:31:35 +05302569 int ret = 0;
2570
Sahitya Tummala62448d92013-03-12 14:57:46 +05302571 if (gpio_is_valid(msm_host->pdata->status_gpio))
2572 mmc_cd_gpio_free(msm_host->mmc);
2573
Asutosh Dasbbc84782013-02-11 15:31:35 +05302574 if (pm_runtime_suspended(dev)) {
2575 pr_debug("%s: %s: already runtime suspended\n",
2576 mmc_hostname(host->mmc), __func__);
2577 goto out;
2578 }
2579
2580 return sdhci_msm_runtime_suspend(dev);
2581out:
2582 return ret;
2583}
2584
2585static int sdhci_msm_resume(struct device *dev)
2586{
2587 struct sdhci_host *host = dev_get_drvdata(dev);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302588 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2589 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Dasbbc84782013-02-11 15:31:35 +05302590 int ret = 0;
2591
Sahitya Tummala62448d92013-03-12 14:57:46 +05302592 if (gpio_is_valid(msm_host->pdata->status_gpio)) {
2593 ret = mmc_cd_gpio_request(msm_host->mmc,
2594 msm_host->pdata->status_gpio);
2595 if (ret)
2596 pr_err("%s: %s: Failed to request card detection IRQ %d\n",
2597 mmc_hostname(host->mmc), __func__, ret);
2598 }
2599
Asutosh Dasbbc84782013-02-11 15:31:35 +05302600 if (pm_runtime_suspended(dev)) {
2601 pr_debug("%s: %s: runtime suspended, defer system resume\n",
2602 mmc_hostname(host->mmc), __func__);
2603 goto out;
2604 }
2605
2606 return sdhci_msm_runtime_resume(dev);
2607out:
2608 return ret;
2609}
2610#endif
2611
2612#ifdef CONFIG_PM
2613static const struct dev_pm_ops sdhci_msm_pmops = {
2614 SET_SYSTEM_SLEEP_PM_OPS(sdhci_msm_suspend, sdhci_msm_resume)
2615 SET_RUNTIME_PM_OPS(sdhci_msm_runtime_suspend, sdhci_msm_runtime_resume,
2616 NULL)
2617};
2618
2619#define SDHCI_MSM_PMOPS (&sdhci_msm_pmops)
2620
2621#else
2622#define SDHCI_PM_OPS NULL
2623#endif
Asutosh Das33a4ff52012-12-18 16:14:02 +05302624static const struct of_device_id sdhci_msm_dt_match[] = {
2625 {.compatible = "qcom,sdhci-msm"},
2626};
2627MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match);
2628
2629static struct platform_driver sdhci_msm_driver = {
2630 .probe = sdhci_msm_probe,
2631 .remove = __devexit_p(sdhci_msm_remove),
2632 .driver = {
2633 .name = "sdhci_msm",
2634 .owner = THIS_MODULE,
2635 .of_match_table = sdhci_msm_dt_match,
Asutosh Dasbbc84782013-02-11 15:31:35 +05302636 .pm = SDHCI_MSM_PMOPS,
Asutosh Das33a4ff52012-12-18 16:14:02 +05302637 },
2638};
2639
2640module_platform_driver(sdhci_msm_driver);
2641
2642MODULE_DESCRIPTION("Qualcomm Secure Digital Host Controller Interface driver");
2643MODULE_LICENSE("GPL v2");