blob: 444b627abe1b5349a6a9fc54ef6fcff31c25c66d [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>
Asutosh Das33a4ff52012-12-18 16:14:02 +053041
42#include "sdhci-pltfm.h"
43
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -070044#define SDHCI_VER_100 0x2B
Asutosh Das33a4ff52012-12-18 16:14:02 +053045#define CORE_HC_MODE 0x78
46#define HC_MODE_EN 0x1
47
48#define CORE_POWER 0x0
49#define CORE_SW_RST (1 << 7)
50
51#define CORE_PWRCTL_STATUS 0xDC
52#define CORE_PWRCTL_MASK 0xE0
53#define CORE_PWRCTL_CLEAR 0xE4
54#define CORE_PWRCTL_CTL 0xE8
55
56#define CORE_PWRCTL_BUS_OFF 0x01
57#define CORE_PWRCTL_BUS_ON (1 << 1)
58#define CORE_PWRCTL_IO_LOW (1 << 2)
59#define CORE_PWRCTL_IO_HIGH (1 << 3)
60
61#define CORE_PWRCTL_BUS_SUCCESS 0x01
62#define CORE_PWRCTL_BUS_FAIL (1 << 1)
63#define CORE_PWRCTL_IO_SUCCESS (1 << 2)
64#define CORE_PWRCTL_IO_FAIL (1 << 3)
65
66#define INT_MASK 0xF
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -070067#define MAX_PHASES 16
68
69#define CORE_DLL_LOCK (1 << 7)
70#define CORE_DLL_EN (1 << 16)
71#define CORE_CDR_EN (1 << 17)
72#define CORE_CK_OUT_EN (1 << 18)
73#define CORE_CDR_EXT_EN (1 << 19)
74#define CORE_DLL_PDN (1 << 29)
75#define CORE_DLL_RST (1 << 30)
76#define CORE_DLL_CONFIG 0x100
77#define CORE_DLL_TEST_CTL 0x104
78#define CORE_DLL_STATUS 0x108
79
80#define CORE_VENDOR_SPEC 0x10C
81#define CORE_CLK_PWRSAVE (1 << 1)
82#define CORE_IO_PAD_PWR_SWITCH (1 << 16)
83
Asutosh Das3781bd82013-01-10 21:11:04 +053084/* 8KB descriptors */
85#define SDHCI_MSM_MAX_SEGMENTS (1 << 13)
Sahitya Tummala04c3a462013-01-11 11:30:45 +053086#define SDHCI_MSM_MMC_CLK_GATE_DELAY 200 /* msecs */
Asutosh Das3781bd82013-01-10 21:11:04 +053087
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -070088static const u32 tuning_block_64[] = {
89 0x00FF0FFF, 0xCCC3CCFF, 0xFFCC3CC3, 0xEFFEFFFE,
90 0xDDFFDFFF, 0xFBFFFBFF, 0xFF7FFFBF, 0xEFBDF777,
91 0xF0FFF0FF, 0x3CCCFC0F, 0xCFCC33CC, 0xEEFFEFFF,
92 0xFDFFFDFF, 0xFFBFFFDF, 0xFFF7FFBB, 0xDE7B7FF7
93};
94
95static const u32 tuning_block_128[] = {
96 0xFF00FFFF, 0x0000FFFF, 0xCCCCFFFF, 0xCCCC33CC,
97 0xCC3333CC, 0xFFFFCCCC, 0xFFFFEEFF, 0xFFEEEEFF,
98 0xFFDDFFFF, 0xDDDDFFFF, 0xBBFFFFFF, 0xBBFFFFFF,
99 0xFFFFFFBB, 0xFFFFFF77, 0x77FF7777, 0xFFEEDDBB,
100 0x00FFFFFF, 0x00FFFFFF, 0xCCFFFF00, 0xCC33CCCC,
101 0x3333CCCC, 0xFFCCCCCC, 0xFFEEFFFF, 0xEEEEFFFF,
102 0xDDFFFFFF, 0xDDFFFFFF, 0xFFFFFFDD, 0xFFFFFFBB,
103 0xFFFFBBBB, 0xFFFF77FF, 0xFF7777FF, 0xEEDDBB77
104};
Asutosh Das33a4ff52012-12-18 16:14:02 +0530105
106/* This structure keeps information per regulator */
107struct sdhci_msm_reg_data {
108 /* voltage regulator handle */
109 struct regulator *reg;
110 /* regulator name */
111 const char *name;
112 /* voltage level to be set */
113 u32 low_vol_level;
114 u32 high_vol_level;
115 /* Load values for low power and high power mode */
116 u32 lpm_uA;
117 u32 hpm_uA;
118
119 /* is this regulator enabled? */
120 bool is_enabled;
121 /* is this regulator needs to be always on? */
122 bool is_always_on;
123 /* is low power mode setting required for this regulator? */
124 bool lpm_sup;
125};
126
127/*
128 * This structure keeps information for all the
129 * regulators required for a SDCC slot.
130 */
131struct sdhci_msm_slot_reg_data {
132 /* keeps VDD/VCC regulator info */
133 struct sdhci_msm_reg_data *vdd_data;
134 /* keeps VDD IO regulator info */
135 struct sdhci_msm_reg_data *vdd_io_data;
136};
137
138struct sdhci_msm_gpio {
139 u32 no;
140 const char *name;
141 bool is_enabled;
142};
143
144struct sdhci_msm_gpio_data {
145 struct sdhci_msm_gpio *gpio;
146 u8 size;
147};
148
Asutosh Das390519d2012-12-21 12:21:42 +0530149struct sdhci_msm_pad_pull {
150 enum msm_tlmm_pull_tgt no;
151 u32 val;
152};
153
154struct sdhci_msm_pad_pull_data {
155 struct sdhci_msm_pad_pull *on;
156 struct sdhci_msm_pad_pull *off;
157 u8 size;
158};
159
160struct sdhci_msm_pad_drv {
161 enum msm_tlmm_hdrive_tgt no;
162 u32 val;
163};
164
165struct sdhci_msm_pad_drv_data {
166 struct sdhci_msm_pad_drv *on;
167 struct sdhci_msm_pad_drv *off;
168 u8 size;
169};
170
171struct sdhci_msm_pad_data {
172 struct sdhci_msm_pad_pull_data *pull;
173 struct sdhci_msm_pad_drv_data *drv;
174};
175
176
Asutosh Das33a4ff52012-12-18 16:14:02 +0530177struct sdhci_msm_pin_data {
178 /*
179 * = 1 if controller pins are using gpios
180 * = 0 if controller has dedicated MSM pads
181 */
Asutosh Das390519d2012-12-21 12:21:42 +0530182 u8 is_gpio;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530183 bool cfg_sts;
184 struct sdhci_msm_gpio_data *gpio_data;
Asutosh Das390519d2012-12-21 12:21:42 +0530185 struct sdhci_msm_pad_data *pad_data;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530186};
187
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +0530188struct sdhci_msm_bus_voting_data {
189 struct msm_bus_scale_pdata *bus_pdata;
190 unsigned int *bw_vecs;
191 unsigned int bw_vecs_size;
192};
193
Asutosh Das33a4ff52012-12-18 16:14:02 +0530194struct sdhci_msm_pltfm_data {
195 /* Supported UHS-I Modes */
196 u32 caps;
197
198 /* More capabilities */
199 u32 caps2;
200
201 unsigned long mmc_bus_width;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530202 struct sdhci_msm_slot_reg_data *vreg_data;
203 bool nonremovable;
204 struct sdhci_msm_pin_data *pin_data;
Sahitya Tummalab4e84042013-03-10 07:03:17 +0530205 u32 cpu_dma_latency_us;
Sahitya Tummala62448d92013-03-12 14:57:46 +0530206 int status_gpio; /* card detection GPIO that is configured as IRQ */
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +0530207 struct sdhci_msm_bus_voting_data *voting_data;
Sahitya Tummala00240122013-02-28 19:50:51 +0530208 u32 *sup_clk_table;
209 unsigned char sup_clk_cnt;
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +0530210};
211
212struct sdhci_msm_bus_vote {
213 uint32_t client_handle;
214 uint32_t curr_vote;
215 int min_bw_vote;
216 int max_bw_vote;
217 bool is_max_bw_needed;
218 struct delayed_work vote_work;
219 struct device_attribute max_bus_bw;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530220};
221
222struct sdhci_msm_host {
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +0530223 struct platform_device *pdev;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530224 void __iomem *core_mem; /* MSM SDCC mapped address */
Asutosh Dasbbc84782013-02-11 15:31:35 +0530225 int pwr_irq; /* power irq */
Asutosh Das33a4ff52012-12-18 16:14:02 +0530226 struct clk *clk; /* main SD/MMC bus clock */
227 struct clk *pclk; /* SDHC peripheral bus clock */
228 struct clk *bus_clk; /* SDHC bus voter clock */
Sahitya Tummala04c3a462013-01-11 11:30:45 +0530229 atomic_t clks_on; /* Set if clocks are enabled */
Asutosh Das33a4ff52012-12-18 16:14:02 +0530230 struct sdhci_msm_pltfm_data *pdata;
231 struct mmc_host *mmc;
232 struct sdhci_pltfm_data sdhci_msm_pdata;
Sahitya Tummala179e7382013-03-20 19:24:01 +0530233 u32 curr_pwr_state;
234 u32 curr_io_level;
235 struct completion pwr_irq_completion;
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +0530236 struct sdhci_msm_bus_vote msm_bus_vote;
Sahitya Tummala00240122013-02-28 19:50:51 +0530237 u32 clk_rate; /* Keeps track of current clock rate that is set */
Asutosh Das33a4ff52012-12-18 16:14:02 +0530238};
239
240enum vdd_io_level {
241 /* set vdd_io_data->low_vol_level */
242 VDD_IO_LOW,
243 /* set vdd_io_data->high_vol_level */
244 VDD_IO_HIGH,
245 /*
246 * set whatever there in voltage_level (third argument) of
247 * sdhci_msm_set_vdd_io_vol() function.
248 */
249 VDD_IO_SET_LEVEL,
250};
251
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700252/* MSM platform specific tuning */
253static inline int msm_dll_poll_ck_out_en(struct sdhci_host *host,
254 u8 poll)
255{
256 int rc = 0;
257 u32 wait_cnt = 50;
258 u8 ck_out_en = 0;
259 struct mmc_host *mmc = host->mmc;
260
261 /* poll for CK_OUT_EN bit. max. poll time = 50us */
262 ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) &
263 CORE_CK_OUT_EN);
264
265 while (ck_out_en != poll) {
266 if (--wait_cnt == 0) {
267 pr_err("%s: %s: CK_OUT_EN bit is not %d\n",
268 mmc_hostname(mmc), __func__, poll);
269 rc = -ETIMEDOUT;
270 goto out;
271 }
272 udelay(1);
273
274 ck_out_en = !!(readl_relaxed(host->ioaddr +
275 CORE_DLL_CONFIG) & CORE_CK_OUT_EN);
276 }
277out:
278 return rc;
279}
280
281static int msm_config_cm_dll_phase(struct sdhci_host *host, u8 phase)
282{
283 int rc = 0;
284 u8 grey_coded_phase_table[] = {0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4,
285 0xC, 0xD, 0xF, 0xE, 0xA, 0xB, 0x9,
286 0x8};
287 unsigned long flags;
288 u32 config;
289 struct mmc_host *mmc = host->mmc;
290
291 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
292 spin_lock_irqsave(&host->lock, flags);
293
294 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
295 config &= ~(CORE_CDR_EN | CORE_CK_OUT_EN);
296 config |= (CORE_CDR_EXT_EN | CORE_DLL_EN);
297 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
298
299 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '0' */
300 rc = msm_dll_poll_ck_out_en(host, 0);
301 if (rc)
302 goto err_out;
303
304 /*
305 * Write the selected DLL clock output phase (0 ... 15)
306 * to CDR_SELEXT bit field of DLL_CONFIG register.
307 */
308 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
309 & ~(0xF << 20))
310 | (grey_coded_phase_table[phase] << 20)),
311 host->ioaddr + CORE_DLL_CONFIG);
312
313 /* Set CK_OUT_EN bit of DLL_CONFIG register to 1. */
314 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
315 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
316
317 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '1' */
318 rc = msm_dll_poll_ck_out_en(host, 1);
319 if (rc)
320 goto err_out;
321
322 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
323 config |= CORE_CDR_EN;
324 config &= ~CORE_CDR_EXT_EN;
325 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
326 goto out;
327
328err_out:
329 pr_err("%s: %s: Failed to set DLL phase: %d\n",
330 mmc_hostname(mmc), __func__, phase);
331out:
332 spin_unlock_irqrestore(&host->lock, flags);
333 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
334 return rc;
335}
336
337/*
338 * Find out the greatest range of consecuitive selected
339 * DLL clock output phases that can be used as sampling
340 * setting for SD3.0 UHS-I card read operation (in SDR104
341 * timing mode) or for eMMC4.5 card read operation (in HS200
342 * timing mode).
343 * Select the 3/4 of the range and configure the DLL with the
344 * selected DLL clock output phase.
345 */
346
347static int msm_find_most_appropriate_phase(struct sdhci_host *host,
348 u8 *phase_table, u8 total_phases)
349{
350 int ret;
351 u8 ranges[MAX_PHASES][MAX_PHASES] = { {0}, {0} };
352 u8 phases_per_row[MAX_PHASES] = {0};
353 int row_index = 0, col_index = 0, selected_row_index = 0, curr_max = 0;
354 int i, cnt, phase_0_raw_index = 0, phase_15_raw_index = 0;
355 bool phase_0_found = false, phase_15_found = false;
356 struct mmc_host *mmc = host->mmc;
357
358 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
359 if (!total_phases || (total_phases > MAX_PHASES)) {
360 pr_err("%s: %s: invalid argument: total_phases=%d\n",
361 mmc_hostname(mmc), __func__, total_phases);
362 return -EINVAL;
363 }
364
365 for (cnt = 0; cnt < total_phases; cnt++) {
366 ranges[row_index][col_index] = phase_table[cnt];
367 phases_per_row[row_index] += 1;
368 col_index++;
369
370 if ((cnt + 1) == total_phases) {
371 continue;
372 /* check if next phase in phase_table is consecutive or not */
373 } else if ((phase_table[cnt] + 1) != phase_table[cnt + 1]) {
374 row_index++;
375 col_index = 0;
376 }
377 }
378
379 if (row_index >= MAX_PHASES)
380 return -EINVAL;
381
382 /* Check if phase-0 is present in first valid window? */
383 if (!ranges[0][0]) {
384 phase_0_found = true;
385 phase_0_raw_index = 0;
386 /* Check if cycle exist between 2 valid windows */
387 for (cnt = 1; cnt <= row_index; cnt++) {
388 if (phases_per_row[cnt]) {
389 for (i = 0; i < phases_per_row[cnt]; i++) {
390 if (ranges[cnt][i] == 15) {
391 phase_15_found = true;
392 phase_15_raw_index = cnt;
393 break;
394 }
395 }
396 }
397 }
398 }
399
400 /* If 2 valid windows form cycle then merge them as single window */
401 if (phase_0_found && phase_15_found) {
402 /* number of phases in raw where phase 0 is present */
403 u8 phases_0 = phases_per_row[phase_0_raw_index];
404 /* number of phases in raw where phase 15 is present */
405 u8 phases_15 = phases_per_row[phase_15_raw_index];
406
407 if (phases_0 + phases_15 >= MAX_PHASES)
408 /*
409 * If there are more than 1 phase windows then total
410 * number of phases in both the windows should not be
411 * more than or equal to MAX_PHASES.
412 */
413 return -EINVAL;
414
415 /* Merge 2 cyclic windows */
416 i = phases_15;
417 for (cnt = 0; cnt < phases_0; cnt++) {
418 ranges[phase_15_raw_index][i] =
419 ranges[phase_0_raw_index][cnt];
420 if (++i >= MAX_PHASES)
421 break;
422 }
423
424 phases_per_row[phase_0_raw_index] = 0;
425 phases_per_row[phase_15_raw_index] = phases_15 + phases_0;
426 }
427
428 for (cnt = 0; cnt <= row_index; cnt++) {
429 if (phases_per_row[cnt] > curr_max) {
430 curr_max = phases_per_row[cnt];
431 selected_row_index = cnt;
432 }
433 }
434
435 i = ((curr_max * 3) / 4);
436 if (i)
437 i--;
438
439 ret = (int)ranges[selected_row_index][i];
440
441 if (ret >= MAX_PHASES) {
442 ret = -EINVAL;
443 pr_err("%s: %s: invalid phase selected=%d\n",
444 mmc_hostname(mmc), __func__, ret);
445 }
446
447 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
448 return ret;
449}
450
451static inline void msm_cm_dll_set_freq(struct sdhci_host *host)
452{
453 u32 mclk_freq = 0;
454
455 /* Program the MCLK value to MCLK_FREQ bit field */
456 if (host->clock <= 112000000)
457 mclk_freq = 0;
458 else if (host->clock <= 125000000)
459 mclk_freq = 1;
460 else if (host->clock <= 137000000)
461 mclk_freq = 2;
462 else if (host->clock <= 150000000)
463 mclk_freq = 3;
464 else if (host->clock <= 162000000)
465 mclk_freq = 4;
466 else if (host->clock <= 175000000)
467 mclk_freq = 5;
468 else if (host->clock <= 187000000)
469 mclk_freq = 6;
470 else if (host->clock <= 200000000)
471 mclk_freq = 7;
472
473 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
474 & ~(7 << 24)) | (mclk_freq << 24)),
475 host->ioaddr + CORE_DLL_CONFIG);
476}
477
478/* Initialize the DLL (Programmable Delay Line ) */
479static int msm_init_cm_dll(struct sdhci_host *host)
480{
481 struct mmc_host *mmc = host->mmc;
482 int rc = 0;
483 unsigned long flags;
484 u32 wait_cnt;
485
486 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
487 spin_lock_irqsave(&host->lock, flags);
488
489 /*
490 * Make sure that clock is always enabled when DLL
491 * tuning is in progress. Keeping PWRSAVE ON may
492 * turn off the clock. So let's disable the PWRSAVE
493 * here and re-enable it once tuning is completed.
494 */
495 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
496 & ~CORE_CLK_PWRSAVE),
497 host->ioaddr + CORE_VENDOR_SPEC);
498
499 /* Write 1 to DLL_RST bit of DLL_CONFIG register */
500 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
501 | CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
502
503 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
504 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
505 | CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
506 msm_cm_dll_set_freq(host);
507
508 /* Write 0 to DLL_RST bit of DLL_CONFIG register */
509 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
510 & ~CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
511
512 /* Write 0 to DLL_PDN bit of DLL_CONFIG register */
513 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
514 & ~CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
515
516 /* Set DLL_EN bit to 1. */
517 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
518 | CORE_DLL_EN), host->ioaddr + CORE_DLL_CONFIG);
519
520 /* Set CK_OUT_EN bit to 1. */
521 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
522 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
523
524 wait_cnt = 50;
525 /* Wait until DLL_LOCK bit of DLL_STATUS register becomes '1' */
526 while (!(readl_relaxed(host->ioaddr + CORE_DLL_STATUS) &
527 CORE_DLL_LOCK)) {
528 /* max. wait for 50us sec for LOCK bit to be set */
529 if (--wait_cnt == 0) {
530 pr_err("%s: %s: DLL failed to LOCK\n",
531 mmc_hostname(mmc), __func__);
532 rc = -ETIMEDOUT;
533 goto out;
534 }
535 /* wait for 1us before polling again */
536 udelay(1);
537 }
538
539out:
540 /* re-enable PWRSAVE */
541 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
542 CORE_CLK_PWRSAVE),
543 host->ioaddr + CORE_VENDOR_SPEC);
544 spin_unlock_irqrestore(&host->lock, flags);
545 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
546 return rc;
547}
548
549int sdhci_msm_execute_tuning(struct sdhci_host *host, u32 opcode)
550{
551 unsigned long flags;
552 u8 phase, *data_buf, tuned_phases[16], tuned_phase_cnt = 0;
553 const u32 *tuning_block_pattern = tuning_block_64;
554 int size = sizeof(tuning_block_64); /* Tuning pattern size in bytes */
555 int rc;
556 struct mmc_host *mmc = host->mmc;
Sahitya Tummala00240122013-02-28 19:50:51 +0530557 struct mmc_ios ios = host->mmc->ios;
558
559 /*
560 * Tuning is required for SDR104 and HS200 cards and if clock frequency
561 * is greater than 100MHz in these modes.
562 */
563 if (host->clock <= (100 * 1000 * 1000) ||
564 !(ios.timing == MMC_TIMING_MMC_HS200 ||
565 ios.timing == MMC_TIMING_UHS_SDR104))
566 return 0;
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700567
568 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700569 spin_lock_irqsave(&host->lock, flags);
570
571 if ((opcode == MMC_SEND_TUNING_BLOCK_HS200) &&
572 (mmc->ios.bus_width == MMC_BUS_WIDTH_8)) {
573 tuning_block_pattern = tuning_block_128;
574 size = sizeof(tuning_block_128);
575 }
576 spin_unlock_irqrestore(&host->lock, flags);
577
578 /* first of all reset the tuning block */
579 rc = msm_init_cm_dll(host);
580 if (rc)
581 goto out;
582
583 data_buf = kmalloc(size, GFP_KERNEL);
584 if (!data_buf) {
585 rc = -ENOMEM;
586 goto out;
587 }
588
589 phase = 0;
590 do {
591 struct mmc_command cmd = {0};
592 struct mmc_data data = {0};
593 struct mmc_request mrq = {
594 .cmd = &cmd,
595 .data = &data
596 };
597 struct scatterlist sg;
598
599 /* set the phase in delay line hw block */
600 rc = msm_config_cm_dll_phase(host, phase);
601 if (rc)
602 goto kfree;
603
604 cmd.opcode = opcode;
605 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
606
607 data.blksz = size;
608 data.blocks = 1;
609 data.flags = MMC_DATA_READ;
610 data.timeout_ns = 1000 * 1000 * 1000; /* 1 sec */
611
612 data.sg = &sg;
613 data.sg_len = 1;
614 sg_init_one(&sg, data_buf, size);
615 memset(data_buf, 0, size);
616 mmc_wait_for_req(mmc, &mrq);
617
618 if (!cmd.error && !data.error &&
619 !memcmp(data_buf, tuning_block_pattern, size)) {
620 /* tuning is successful at this tuning point */
621 tuned_phases[tuned_phase_cnt++] = phase;
622 pr_debug("%s: %s: found good phase = %d\n",
623 mmc_hostname(mmc), __func__, phase);
624 }
625 } while (++phase < 16);
626
627 if (tuned_phase_cnt) {
628 rc = msm_find_most_appropriate_phase(host, tuned_phases,
629 tuned_phase_cnt);
630 if (rc < 0)
631 goto kfree;
632 else
633 phase = (u8)rc;
634
635 /*
636 * Finally set the selected phase in delay
637 * line hw block.
638 */
639 rc = msm_config_cm_dll_phase(host, phase);
640 if (rc)
641 goto kfree;
642 pr_debug("%s: %s: finally setting the tuning phase to %d\n",
643 mmc_hostname(mmc), __func__, phase);
644 } else {
645 /* tuning failed */
646 pr_err("%s: %s: no tuning point found\n",
647 mmc_hostname(mmc), __func__);
648 rc = -EAGAIN;
649 }
650
651kfree:
652 kfree(data_buf);
653out:
654 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
655 return rc;
656}
657
Asutosh Das33a4ff52012-12-18 16:14:02 +0530658static int sdhci_msm_setup_gpio(struct sdhci_msm_pltfm_data *pdata, bool enable)
659{
660 struct sdhci_msm_gpio_data *curr;
661 int i, ret = 0;
662
663 curr = pdata->pin_data->gpio_data;
664 for (i = 0; i < curr->size; i++) {
665 if (!gpio_is_valid(curr->gpio[i].no)) {
666 ret = -EINVAL;
667 pr_err("%s: Invalid gpio = %d\n", __func__,
668 curr->gpio[i].no);
669 goto free_gpios;
670 }
671 if (enable) {
672 ret = gpio_request(curr->gpio[i].no,
673 curr->gpio[i].name);
674 if (ret) {
675 pr_err("%s: gpio_request(%d, %s) failed %d\n",
676 __func__, curr->gpio[i].no,
677 curr->gpio[i].name, ret);
678 goto free_gpios;
679 }
680 curr->gpio[i].is_enabled = true;
681 } else {
682 gpio_free(curr->gpio[i].no);
683 curr->gpio[i].is_enabled = false;
684 }
685 }
686 return ret;
687
688free_gpios:
689 for (i--; i >= 0; i--) {
690 gpio_free(curr->gpio[i].no);
691 curr->gpio[i].is_enabled = false;
692 }
693 return ret;
694}
695
Asutosh Das390519d2012-12-21 12:21:42 +0530696static int sdhci_msm_setup_pad(struct sdhci_msm_pltfm_data *pdata, bool enable)
697{
698 struct sdhci_msm_pad_data *curr;
699 int i;
700
701 curr = pdata->pin_data->pad_data;
702 for (i = 0; i < curr->drv->size; i++) {
703 if (enable)
704 msm_tlmm_set_hdrive(curr->drv->on[i].no,
705 curr->drv->on[i].val);
706 else
707 msm_tlmm_set_hdrive(curr->drv->off[i].no,
708 curr->drv->off[i].val);
709 }
710
711 for (i = 0; i < curr->pull->size; i++) {
712 if (enable)
713 msm_tlmm_set_pull(curr->pull->on[i].no,
714 curr->pull->on[i].val);
715 else
716 msm_tlmm_set_pull(curr->pull->off[i].no,
717 curr->pull->off[i].val);
718 }
719
720 return 0;
721}
722
Asutosh Das33a4ff52012-12-18 16:14:02 +0530723static int sdhci_msm_setup_pins(struct sdhci_msm_pltfm_data *pdata, bool enable)
724{
725 int ret = 0;
726
727 if (!pdata->pin_data || (pdata->pin_data->cfg_sts == enable))
728 return 0;
Asutosh Das390519d2012-12-21 12:21:42 +0530729 if (pdata->pin_data->is_gpio)
730 ret = sdhci_msm_setup_gpio(pdata, enable);
731 else
732 ret = sdhci_msm_setup_pad(pdata, enable);
Asutosh Das33a4ff52012-12-18 16:14:02 +0530733
Asutosh Das33a4ff52012-12-18 16:14:02 +0530734 if (!ret)
735 pdata->pin_data->cfg_sts = enable;
736
737 return ret;
738}
739
Asutosh Das390519d2012-12-21 12:21:42 +0530740static int sdhci_msm_dt_get_array(struct device *dev, const char *prop_name,
741 u32 **out, int *len, u32 size)
742{
743 int ret = 0;
744 struct device_node *np = dev->of_node;
745 size_t sz;
746 u32 *arr = NULL;
747
748 if (!of_get_property(np, prop_name, len)) {
749 ret = -EINVAL;
750 goto out;
751 }
752 sz = *len = *len / sizeof(*arr);
753 if (sz <= 0 || (size > 0 && (sz != size))) {
754 dev_err(dev, "%s invalid size\n", prop_name);
755 ret = -EINVAL;
756 goto out;
757 }
758
759 arr = devm_kzalloc(dev, sz * sizeof(*arr), GFP_KERNEL);
760 if (!arr) {
761 dev_err(dev, "%s failed allocating memory\n", prop_name);
762 ret = -ENOMEM;
763 goto out;
764 }
765
766 ret = of_property_read_u32_array(np, prop_name, arr, sz);
767 if (ret < 0) {
768 dev_err(dev, "%s failed reading array %d\n", prop_name, ret);
769 goto out;
770 }
771 *out = arr;
772out:
773 if (ret)
774 *len = 0;
775 return ret;
776}
777
Asutosh Das33a4ff52012-12-18 16:14:02 +0530778#define MAX_PROP_SIZE 32
779static int sdhci_msm_dt_parse_vreg_info(struct device *dev,
780 struct sdhci_msm_reg_data **vreg_data, const char *vreg_name)
781{
782 int len, ret = 0;
783 const __be32 *prop;
784 char prop_name[MAX_PROP_SIZE];
785 struct sdhci_msm_reg_data *vreg;
786 struct device_node *np = dev->of_node;
787
788 snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", vreg_name);
789 if (!of_parse_phandle(np, prop_name, 0)) {
790 dev_err(dev, "No vreg data found for %s\n", vreg_name);
791 ret = -EINVAL;
792 return ret;
793 }
794
795 vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
796 if (!vreg) {
797 dev_err(dev, "No memory for vreg: %s\n", vreg_name);
798 ret = -ENOMEM;
799 return ret;
800 }
801
802 vreg->name = vreg_name;
803
804 snprintf(prop_name, MAX_PROP_SIZE,
805 "qcom,%s-always-on", vreg_name);
806 if (of_get_property(np, prop_name, NULL))
807 vreg->is_always_on = true;
808
809 snprintf(prop_name, MAX_PROP_SIZE,
810 "qcom,%s-lpm-sup", vreg_name);
811 if (of_get_property(np, prop_name, NULL))
812 vreg->lpm_sup = true;
813
814 snprintf(prop_name, MAX_PROP_SIZE,
815 "qcom,%s-voltage-level", vreg_name);
816 prop = of_get_property(np, prop_name, &len);
817 if (!prop || (len != (2 * sizeof(__be32)))) {
818 dev_warn(dev, "%s %s property\n",
819 prop ? "invalid format" : "no", prop_name);
820 } else {
821 vreg->low_vol_level = be32_to_cpup(&prop[0]);
822 vreg->high_vol_level = be32_to_cpup(&prop[1]);
823 }
824
825 snprintf(prop_name, MAX_PROP_SIZE,
826 "qcom,%s-current-level", vreg_name);
827 prop = of_get_property(np, prop_name, &len);
828 if (!prop || (len != (2 * sizeof(__be32)))) {
829 dev_warn(dev, "%s %s property\n",
830 prop ? "invalid format" : "no", prop_name);
831 } else {
832 vreg->lpm_uA = be32_to_cpup(&prop[0]);
833 vreg->hpm_uA = be32_to_cpup(&prop[1]);
834 }
835
836 *vreg_data = vreg;
837 dev_dbg(dev, "%s: %s %s vol=[%d %d]uV, curr=[%d %d]uA\n",
838 vreg->name, vreg->is_always_on ? "always_on," : "",
839 vreg->lpm_sup ? "lpm_sup," : "", vreg->low_vol_level,
840 vreg->high_vol_level, vreg->lpm_uA, vreg->hpm_uA);
841
842 return ret;
843}
844
Asutosh Das390519d2012-12-21 12:21:42 +0530845/* GPIO/Pad data extraction */
846static int sdhci_msm_dt_get_pad_pull_info(struct device *dev, int id,
847 struct sdhci_msm_pad_pull_data **pad_pull_data)
848{
849 int ret = 0, base = 0, len, i;
850 u32 *tmp;
851 struct sdhci_msm_pad_pull_data *pull_data;
852 struct sdhci_msm_pad_pull *pull;
853
854 switch (id) {
855 case 1:
856 base = TLMM_PULL_SDC1_CLK;
857 break;
858 case 2:
859 base = TLMM_PULL_SDC2_CLK;
860 break;
861 case 3:
862 base = TLMM_PULL_SDC3_CLK;
863 break;
864 case 4:
865 base = TLMM_PULL_SDC4_CLK;
866 break;
867 default:
868 dev_err(dev, "%s: Invalid slot id\n", __func__);
869 ret = -EINVAL;
870 goto out;
871 }
872
873 pull_data = devm_kzalloc(dev, sizeof(struct sdhci_msm_pad_pull_data),
874 GFP_KERNEL);
875 if (!pull_data) {
876 dev_err(dev, "No memory for msm_mmc_pad_pull_data\n");
877 ret = -ENOMEM;
878 goto out;
879 }
880 pull_data->size = 3; /* array size for clk, cmd, data */
881
882 /* Allocate on, off configs for clk, cmd, data */
883 pull = devm_kzalloc(dev, 2 * pull_data->size *\
884 sizeof(struct sdhci_msm_pad_pull), GFP_KERNEL);
885 if (!pull) {
886 dev_err(dev, "No memory for msm_mmc_pad_pull\n");
887 ret = -ENOMEM;
888 goto out;
889 }
890 pull_data->on = pull;
891 pull_data->off = pull + pull_data->size;
892
893 ret = sdhci_msm_dt_get_array(dev, "qcom,pad-pull-on",
894 &tmp, &len, pull_data->size);
895 if (ret)
896 goto out;
897
898 for (i = 0; i < len; i++) {
899 pull_data->on[i].no = base + i;
900 pull_data->on[i].val = tmp[i];
901 dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
902 i, pull_data->on[i].val);
903 }
904
905 ret = sdhci_msm_dt_get_array(dev, "qcom,pad-pull-off",
906 &tmp, &len, pull_data->size);
907 if (ret)
908 goto out;
909
910 for (i = 0; i < len; i++) {
911 pull_data->off[i].no = base + i;
912 pull_data->off[i].val = tmp[i];
913 dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
914 i, pull_data->off[i].val);
915 }
916
917 *pad_pull_data = pull_data;
918out:
919 return ret;
920}
921
922static int sdhci_msm_dt_get_pad_drv_info(struct device *dev, int id,
923 struct sdhci_msm_pad_drv_data **pad_drv_data)
924{
925 int ret = 0, base = 0, len, i;
926 u32 *tmp;
927 struct sdhci_msm_pad_drv_data *drv_data;
928 struct sdhci_msm_pad_drv *drv;
929
930 switch (id) {
931 case 1:
932 base = TLMM_HDRV_SDC1_CLK;
933 break;
934 case 2:
935 base = TLMM_HDRV_SDC2_CLK;
936 break;
937 case 3:
938 base = TLMM_HDRV_SDC3_CLK;
939 break;
940 case 4:
941 base = TLMM_HDRV_SDC4_CLK;
942 break;
943 default:
944 dev_err(dev, "%s: Invalid slot id\n", __func__);
945 ret = -EINVAL;
946 goto out;
947 }
948
949 drv_data = devm_kzalloc(dev, sizeof(struct sdhci_msm_pad_drv_data),
950 GFP_KERNEL);
951 if (!drv_data) {
952 dev_err(dev, "No memory for msm_mmc_pad_drv_data\n");
953 ret = -ENOMEM;
954 goto out;
955 }
956 drv_data->size = 3; /* array size for clk, cmd, data */
957
958 /* Allocate on, off configs for clk, cmd, data */
959 drv = devm_kzalloc(dev, 2 * drv_data->size *\
960 sizeof(struct sdhci_msm_pad_drv), GFP_KERNEL);
961 if (!drv) {
962 dev_err(dev, "No memory msm_mmc_pad_drv\n");
963 ret = -ENOMEM;
964 goto out;
965 }
966 drv_data->on = drv;
967 drv_data->off = drv + drv_data->size;
968
969 ret = sdhci_msm_dt_get_array(dev, "qcom,pad-drv-on",
970 &tmp, &len, drv_data->size);
971 if (ret)
972 goto out;
973
974 for (i = 0; i < len; i++) {
975 drv_data->on[i].no = base + i;
976 drv_data->on[i].val = tmp[i];
977 dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
978 i, drv_data->on[i].val);
979 }
980
981 ret = sdhci_msm_dt_get_array(dev, "qcom,pad-drv-off",
982 &tmp, &len, drv_data->size);
983 if (ret)
984 goto out;
985
986 for (i = 0; i < len; i++) {
987 drv_data->off[i].no = base + i;
988 drv_data->off[i].val = tmp[i];
989 dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
990 i, drv_data->off[i].val);
991 }
992
993 *pad_drv_data = drv_data;
994out:
995 return ret;
996}
997
Asutosh Das33a4ff52012-12-18 16:14:02 +0530998#define GPIO_NAME_MAX_LEN 32
999static int sdhci_msm_dt_parse_gpio_info(struct device *dev,
1000 struct sdhci_msm_pltfm_data *pdata)
1001{
Asutosh Das390519d2012-12-21 12:21:42 +05301002 int ret = 0, id = 0, cnt, i;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301003 struct sdhci_msm_pin_data *pin_data;
1004 struct device_node *np = dev->of_node;
1005
1006 pin_data = devm_kzalloc(dev, sizeof(*pin_data), GFP_KERNEL);
1007 if (!pin_data) {
1008 dev_err(dev, "No memory for pin_data\n");
1009 ret = -ENOMEM;
1010 goto out;
1011 }
1012
1013 cnt = of_gpio_count(np);
1014 if (cnt > 0) {
Asutosh Das390519d2012-12-21 12:21:42 +05301015 pin_data->is_gpio = true;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301016 pin_data->gpio_data = devm_kzalloc(dev,
1017 sizeof(struct sdhci_msm_gpio_data), GFP_KERNEL);
1018 if (!pin_data->gpio_data) {
1019 dev_err(dev, "No memory for gpio_data\n");
1020 ret = -ENOMEM;
1021 goto out;
1022 }
1023 pin_data->gpio_data->size = cnt;
1024 pin_data->gpio_data->gpio = devm_kzalloc(dev, cnt *
1025 sizeof(struct sdhci_msm_gpio), GFP_KERNEL);
1026
1027 if (!pin_data->gpio_data->gpio) {
1028 dev_err(dev, "No memory for gpio\n");
1029 ret = -ENOMEM;
1030 goto out;
1031 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301032 for (i = 0; i < cnt; i++) {
1033 const char *name = NULL;
1034 char result[GPIO_NAME_MAX_LEN];
1035 pin_data->gpio_data->gpio[i].no = of_get_gpio(np, i);
1036 of_property_read_string_index(np,
1037 "qcom,gpio-names", i, &name);
1038
1039 snprintf(result, GPIO_NAME_MAX_LEN, "%s-%s",
1040 dev_name(dev), name ? name : "?");
1041 pin_data->gpio_data->gpio[i].name = result;
1042 dev_dbg(dev, "%s: gpio[%s] = %d\n", __func__,
Asutosh Das390519d2012-12-21 12:21:42 +05301043 pin_data->gpio_data->gpio[i].name,
1044 pin_data->gpio_data->gpio[i].no);
Asutosh Das33a4ff52012-12-18 16:14:02 +05301045 }
Asutosh Das390519d2012-12-21 12:21:42 +05301046 } else {
1047 pin_data->pad_data =
1048 devm_kzalloc(dev,
1049 sizeof(struct sdhci_msm_pad_data),
1050 GFP_KERNEL);
1051 if (!pin_data->pad_data) {
1052 dev_err(dev,
1053 "No memory for pin_data->pad_data\n");
1054 ret = -ENOMEM;
1055 goto out;
1056 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301057
Asutosh Das390519d2012-12-21 12:21:42 +05301058 ret = of_alias_get_id(np, "sdhc");
1059 if (ret < 0) {
1060 dev_err(dev, "Failed to get slot index %d\n", ret);
1061 goto out;
1062 }
1063 id = ret;
1064
1065 ret = sdhci_msm_dt_get_pad_pull_info(
1066 dev, id, &pin_data->pad_data->pull);
1067 if (ret)
1068 goto out;
1069 ret = sdhci_msm_dt_get_pad_drv_info(
1070 dev, id, &pin_data->pad_data->drv);
1071 if (ret)
1072 goto out;
1073
1074 }
1075 pdata->pin_data = pin_data;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301076out:
1077 if (ret)
1078 dev_err(dev, "%s failed with err %d\n", __func__, ret);
1079 return ret;
1080}
1081
1082/* Parse platform data */
1083static struct sdhci_msm_pltfm_data *sdhci_msm_populate_pdata(struct device *dev)
1084{
1085 struct sdhci_msm_pltfm_data *pdata = NULL;
1086 struct device_node *np = dev->of_node;
1087 u32 bus_width = 0;
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301088 u32 cpu_dma_latency;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301089 int len, i;
Sahitya Tummala00240122013-02-28 19:50:51 +05301090 int clk_table_len;
1091 u32 *clk_table = NULL;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301092
1093 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1094 if (!pdata) {
1095 dev_err(dev, "failed to allocate memory for platform data\n");
1096 goto out;
1097 }
1098
Sahitya Tummala62448d92013-03-12 14:57:46 +05301099 pdata->status_gpio = of_get_named_gpio_flags(np, "cd-gpios", 0, 0);
1100
Asutosh Das33a4ff52012-12-18 16:14:02 +05301101 of_property_read_u32(np, "qcom,bus-width", &bus_width);
1102 if (bus_width == 8)
1103 pdata->mmc_bus_width = MMC_CAP_8_BIT_DATA;
1104 else if (bus_width == 4)
1105 pdata->mmc_bus_width = MMC_CAP_4_BIT_DATA;
1106 else {
1107 dev_notice(dev, "invalid bus-width, default to 1-bit mode\n");
1108 pdata->mmc_bus_width = 0;
1109 }
1110
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301111 if (!of_property_read_u32(np, "qcom,cpu-dma-latency-us",
1112 &cpu_dma_latency))
1113 pdata->cpu_dma_latency_us = cpu_dma_latency;
1114
Sahitya Tummala00240122013-02-28 19:50:51 +05301115 if (sdhci_msm_dt_get_array(dev, "qcom,clk-rates",
1116 &clk_table, &clk_table_len, 0)) {
1117 dev_err(dev, "failed parsing supported clock rates\n");
1118 goto out;
1119 }
1120 if (!clk_table || !clk_table_len) {
1121 dev_err(dev, "Invalid clock table\n");
1122 goto out;
1123 }
1124 pdata->sup_clk_table = clk_table;
1125 pdata->sup_clk_cnt = clk_table_len;
1126
Asutosh Das33a4ff52012-12-18 16:14:02 +05301127 pdata->vreg_data = devm_kzalloc(dev, sizeof(struct
1128 sdhci_msm_slot_reg_data),
1129 GFP_KERNEL);
1130 if (!pdata->vreg_data) {
1131 dev_err(dev, "failed to allocate memory for vreg data\n");
1132 goto out;
1133 }
1134
1135 if (sdhci_msm_dt_parse_vreg_info(dev, &pdata->vreg_data->vdd_data,
1136 "vdd")) {
1137 dev_err(dev, "failed parsing vdd data\n");
1138 goto out;
1139 }
1140 if (sdhci_msm_dt_parse_vreg_info(dev,
1141 &pdata->vreg_data->vdd_io_data,
1142 "vdd-io")) {
1143 dev_err(dev, "failed parsing vdd-io data\n");
1144 goto out;
1145 }
1146
1147 if (sdhci_msm_dt_parse_gpio_info(dev, pdata)) {
1148 dev_err(dev, "failed parsing gpio data\n");
1149 goto out;
1150 }
1151
Asutosh Das33a4ff52012-12-18 16:14:02 +05301152 len = of_property_count_strings(np, "qcom,bus-speed-mode");
1153
1154 for (i = 0; i < len; i++) {
1155 const char *name = NULL;
1156
1157 of_property_read_string_index(np,
1158 "qcom,bus-speed-mode", i, &name);
1159 if (!name)
1160 continue;
1161
1162 if (!strncmp(name, "HS200_1p8v", sizeof("HS200_1p8v")))
1163 pdata->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
1164 else if (!strncmp(name, "HS200_1p2v", sizeof("HS200_1p2v")))
1165 pdata->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
1166 else if (!strncmp(name, "DDR_1p8v", sizeof("DDR_1p8v")))
1167 pdata->caps |= MMC_CAP_1_8V_DDR
1168 | MMC_CAP_UHS_DDR50;
1169 else if (!strncmp(name, "DDR_1p2v", sizeof("DDR_1p2v")))
1170 pdata->caps |= MMC_CAP_1_2V_DDR
1171 | MMC_CAP_UHS_DDR50;
1172 }
1173
1174 if (of_get_property(np, "qcom,nonremovable", NULL))
1175 pdata->nonremovable = true;
1176
1177 return pdata;
1178out:
1179 return NULL;
1180}
1181
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301182/* Returns required bandwidth in Bytes per Sec */
1183static unsigned int sdhci_get_bw_required(struct sdhci_host *host,
1184 struct mmc_ios *ios)
1185{
1186 unsigned int bw;
1187
1188 bw = host->clock;
1189 /*
1190 * For DDR mode, SDCC controller clock will be at
1191 * the double rate than the actual clock that goes to card.
1192 */
1193 if (ios->bus_width == MMC_BUS_WIDTH_4)
1194 bw /= 2;
1195 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1196 bw /= 8;
1197
1198 return bw;
1199}
1200
1201static int sdhci_msm_bus_get_vote_for_bw(struct sdhci_msm_host *host,
1202 unsigned int bw)
1203{
1204 unsigned int *table = host->pdata->voting_data->bw_vecs;
1205 unsigned int size = host->pdata->voting_data->bw_vecs_size;
1206 int i;
1207
1208 if (host->msm_bus_vote.is_max_bw_needed && bw)
1209 return host->msm_bus_vote.max_bw_vote;
1210
1211 for (i = 0; i < size; i++) {
1212 if (bw <= table[i])
1213 break;
1214 }
1215
1216 if (i && (i == size))
1217 i--;
1218
1219 return i;
1220}
1221
1222/*
1223 * This function must be called with host lock acquired.
1224 * Caller of this function should also ensure that msm bus client
1225 * handle is not null.
1226 */
1227static inline int sdhci_msm_bus_set_vote(struct sdhci_msm_host *msm_host,
1228 int vote,
1229 unsigned long flags)
1230{
1231 struct sdhci_host *host = platform_get_drvdata(msm_host->pdev);
1232 int rc = 0;
1233
1234 if (vote != msm_host->msm_bus_vote.curr_vote) {
1235 spin_unlock_irqrestore(&host->lock, flags);
1236 rc = msm_bus_scale_client_update_request(
1237 msm_host->msm_bus_vote.client_handle, vote);
1238 spin_lock_irqsave(&host->lock, flags);
1239 if (rc) {
1240 pr_err("%s: msm_bus_scale_client_update_request() failed: bus_client_handle=0x%x, vote=%d, err=%d\n",
1241 mmc_hostname(host->mmc),
1242 msm_host->msm_bus_vote.client_handle, vote, rc);
1243 goto out;
1244 }
1245 msm_host->msm_bus_vote.curr_vote = vote;
1246 }
1247out:
1248 return rc;
1249}
1250
1251/*
1252 * Internal work. Work to set 0 bandwidth for msm bus.
1253 */
1254static void sdhci_msm_bus_work(struct work_struct *work)
1255{
1256 struct sdhci_msm_host *msm_host;
1257 struct sdhci_host *host;
1258 unsigned long flags;
1259
1260 msm_host = container_of(work, struct sdhci_msm_host,
1261 msm_bus_vote.vote_work.work);
1262 host = platform_get_drvdata(msm_host->pdev);
1263
1264 if (!msm_host->msm_bus_vote.client_handle)
1265 return;
1266
1267 spin_lock_irqsave(&host->lock, flags);
1268 /* don't vote for 0 bandwidth if any request is in progress */
1269 if (!host->mrq) {
1270 sdhci_msm_bus_set_vote(msm_host,
1271 msm_host->msm_bus_vote.min_bw_vote, flags);
1272 } else
1273 pr_warning("%s: %s: Transfer in progress. skipping bus voting to 0 bandwidth\n",
1274 mmc_hostname(host->mmc), __func__);
1275 spin_unlock_irqrestore(&host->lock, flags);
1276}
1277
1278/*
1279 * This function cancels any scheduled delayed work and sets the bus
1280 * vote based on bw (bandwidth) argument.
1281 */
1282static void sdhci_msm_bus_cancel_work_and_set_vote(struct sdhci_host *host,
1283 unsigned int bw)
1284{
1285 int vote;
1286 unsigned long flags;
1287 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1288 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1289
1290 cancel_delayed_work_sync(&msm_host->msm_bus_vote.vote_work);
1291 spin_lock_irqsave(&host->lock, flags);
1292 vote = sdhci_msm_bus_get_vote_for_bw(msm_host, bw);
1293 sdhci_msm_bus_set_vote(msm_host, vote, flags);
1294 spin_unlock_irqrestore(&host->lock, flags);
1295}
1296
1297#define MSM_MMC_BUS_VOTING_DELAY 200 /* msecs */
1298
1299/* This function queues a work which will set the bandwidth requiement to 0 */
1300static void sdhci_msm_bus_queue_work(struct sdhci_host *host)
1301{
1302 unsigned long flags;
1303 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1304 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1305
1306 spin_lock_irqsave(&host->lock, flags);
1307 if (msm_host->msm_bus_vote.min_bw_vote !=
1308 msm_host->msm_bus_vote.curr_vote)
1309 queue_delayed_work(system_nrt_wq,
1310 &msm_host->msm_bus_vote.vote_work,
1311 msecs_to_jiffies(MSM_MMC_BUS_VOTING_DELAY));
1312 spin_unlock_irqrestore(&host->lock, flags);
1313}
1314
1315static int sdhci_msm_bus_register(struct sdhci_msm_host *host,
1316 struct platform_device *pdev)
1317{
1318 int rc = 0;
1319 struct msm_bus_scale_pdata *bus_pdata;
1320
1321 struct sdhci_msm_bus_voting_data *data;
1322 struct device *dev = &pdev->dev;
1323
1324 data = devm_kzalloc(dev,
1325 sizeof(struct sdhci_msm_bus_voting_data), GFP_KERNEL);
1326 if (!data) {
1327 dev_err(&pdev->dev,
1328 "%s: failed to allocate memory\n", __func__);
1329 rc = -ENOMEM;
1330 goto out;
1331 }
1332 data->bus_pdata = msm_bus_cl_get_pdata(pdev);
1333 if (data->bus_pdata) {
1334 rc = sdhci_msm_dt_get_array(dev, "qcom,bus-bw-vectors-bps",
1335 &data->bw_vecs, &data->bw_vecs_size, 0);
1336 if (rc) {
1337 dev_err(&pdev->dev,
1338 "%s: Failed to get bus-bw-vectors-bps\n",
1339 __func__);
1340 goto out;
1341 }
1342 host->pdata->voting_data = data;
1343 }
1344 if (host->pdata->voting_data &&
1345 host->pdata->voting_data->bus_pdata &&
1346 host->pdata->voting_data->bw_vecs &&
1347 host->pdata->voting_data->bw_vecs_size) {
1348
1349 bus_pdata = host->pdata->voting_data->bus_pdata;
1350 host->msm_bus_vote.client_handle =
1351 msm_bus_scale_register_client(bus_pdata);
1352 if (!host->msm_bus_vote.client_handle) {
1353 dev_err(&pdev->dev, "msm_bus_scale_register_client()\n");
1354 rc = -EFAULT;
1355 goto out;
1356 }
1357 /* cache the vote index for minimum and maximum bandwidth */
1358 host->msm_bus_vote.min_bw_vote =
1359 sdhci_msm_bus_get_vote_for_bw(host, 0);
1360 host->msm_bus_vote.max_bw_vote =
1361 sdhci_msm_bus_get_vote_for_bw(host, UINT_MAX);
1362 } else {
1363 devm_kfree(dev, data);
1364 }
1365
1366out:
1367 return rc;
1368}
1369
1370static void sdhci_msm_bus_unregister(struct sdhci_msm_host *host)
1371{
1372 if (host->msm_bus_vote.client_handle)
1373 msm_bus_scale_unregister_client(
1374 host->msm_bus_vote.client_handle);
1375}
1376
1377static void sdhci_msm_bus_voting(struct sdhci_host *host, u32 enable)
1378{
1379 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1380 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1381 struct mmc_ios *ios = &host->mmc->ios;
1382 unsigned int bw;
1383
1384 if (!msm_host->msm_bus_vote.client_handle)
1385 return;
1386
1387 bw = sdhci_get_bw_required(host, ios);
1388 if (enable)
1389 sdhci_msm_bus_cancel_work_and_set_vote(host, bw);
1390 else
1391 sdhci_msm_bus_queue_work(host);
1392}
1393
Asutosh Das33a4ff52012-12-18 16:14:02 +05301394/* Regulator utility functions */
1395static int sdhci_msm_vreg_init_reg(struct device *dev,
1396 struct sdhci_msm_reg_data *vreg)
1397{
1398 int ret = 0;
1399
1400 /* check if regulator is already initialized? */
1401 if (vreg->reg)
1402 goto out;
1403
1404 /* Get the regulator handle */
1405 vreg->reg = devm_regulator_get(dev, vreg->name);
1406 if (IS_ERR(vreg->reg)) {
1407 ret = PTR_ERR(vreg->reg);
1408 pr_err("%s: devm_regulator_get(%s) failed. ret=%d\n",
1409 __func__, vreg->name, ret);
1410 goto out;
1411 }
1412
1413 /* sanity check */
1414 if (!vreg->high_vol_level || !vreg->hpm_uA) {
1415 pr_err("%s: %s invalid constraints specified\n",
1416 __func__, vreg->name);
1417 ret = -EINVAL;
1418 }
1419
1420out:
1421 return ret;
1422}
1423
1424static void sdhci_msm_vreg_deinit_reg(struct sdhci_msm_reg_data *vreg)
1425{
1426 if (vreg->reg)
1427 devm_regulator_put(vreg->reg);
1428}
1429
1430static int sdhci_msm_vreg_set_optimum_mode(struct sdhci_msm_reg_data
1431 *vreg, int uA_load)
1432{
1433 int ret = 0;
1434
1435 /*
1436 * regulators that do not support regulator_set_voltage also
1437 * do not support regulator_set_optimum_mode
1438 */
1439 ret = regulator_set_optimum_mode(vreg->reg, uA_load);
1440 if (ret < 0)
1441 pr_err("%s: regulator_set_optimum_mode(reg=%s,uA_load=%d) failed. ret=%d\n",
1442 __func__, vreg->name, uA_load, ret);
1443 else
1444 /*
1445 * regulator_set_optimum_mode() can return non zero
1446 * value even for success case.
1447 */
1448 ret = 0;
1449 return ret;
1450}
1451
1452static int sdhci_msm_vreg_set_voltage(struct sdhci_msm_reg_data *vreg,
1453 int min_uV, int max_uV)
1454{
1455 int ret = 0;
1456
1457 ret = regulator_set_voltage(vreg->reg, min_uV, max_uV);
1458 if (ret) {
1459 pr_err("%s: regulator_set_voltage(%s)failed. min_uV=%d,max_uV=%d,ret=%d\n",
1460 __func__, vreg->name, min_uV, max_uV, ret);
1461 }
1462
1463 return ret;
1464}
1465
1466static int sdhci_msm_vreg_enable(struct sdhci_msm_reg_data *vreg)
1467{
1468 int ret = 0;
1469
1470 /* Put regulator in HPM (high power mode) */
1471 ret = sdhci_msm_vreg_set_optimum_mode(vreg, vreg->hpm_uA);
1472 if (ret < 0)
1473 return ret;
1474
1475 if (!vreg->is_enabled) {
1476 /* Set voltage level */
1477 ret = sdhci_msm_vreg_set_voltage(vreg, vreg->high_vol_level,
1478 vreg->high_vol_level);
1479 if (ret)
1480 return ret;
1481 }
1482 ret = regulator_enable(vreg->reg);
1483 if (ret) {
1484 pr_err("%s: regulator_enable(%s) failed. ret=%d\n",
1485 __func__, vreg->name, ret);
1486 return ret;
1487 }
1488 vreg->is_enabled = true;
1489 return ret;
1490}
1491
1492static int sdhci_msm_vreg_disable(struct sdhci_msm_reg_data *vreg)
1493{
1494 int ret = 0;
1495
1496 /* Never disable regulator marked as always_on */
1497 if (vreg->is_enabled && !vreg->is_always_on) {
1498 ret = regulator_disable(vreg->reg);
1499 if (ret) {
1500 pr_err("%s: regulator_disable(%s) failed. ret=%d\n",
1501 __func__, vreg->name, ret);
1502 goto out;
1503 }
1504 vreg->is_enabled = false;
1505
1506 ret = sdhci_msm_vreg_set_optimum_mode(vreg, 0);
1507 if (ret < 0)
1508 goto out;
1509
1510 /* Set min. voltage level to 0 */
1511 ret = sdhci_msm_vreg_set_voltage(vreg, 0, vreg->high_vol_level);
1512 if (ret)
1513 goto out;
1514 } else if (vreg->is_enabled && vreg->is_always_on) {
1515 if (vreg->lpm_sup) {
1516 /* Put always_on regulator in LPM (low power mode) */
1517 ret = sdhci_msm_vreg_set_optimum_mode(vreg,
1518 vreg->lpm_uA);
1519 if (ret < 0)
1520 goto out;
1521 }
1522 }
1523out:
1524 return ret;
1525}
1526
1527static int sdhci_msm_setup_vreg(struct sdhci_msm_pltfm_data *pdata,
1528 bool enable, bool is_init)
1529{
1530 int ret = 0, i;
1531 struct sdhci_msm_slot_reg_data *curr_slot;
1532 struct sdhci_msm_reg_data *vreg_table[2];
1533
1534 curr_slot = pdata->vreg_data;
1535 if (!curr_slot) {
1536 pr_debug("%s: vreg info unavailable,assuming the slot is powered by always on domain\n",
1537 __func__);
1538 goto out;
1539 }
1540
1541 vreg_table[0] = curr_slot->vdd_data;
1542 vreg_table[1] = curr_slot->vdd_io_data;
1543
1544 for (i = 0; i < ARRAY_SIZE(vreg_table); i++) {
1545 if (vreg_table[i]) {
1546 if (enable)
1547 ret = sdhci_msm_vreg_enable(vreg_table[i]);
1548 else
1549 ret = sdhci_msm_vreg_disable(vreg_table[i]);
1550 if (ret)
1551 goto out;
1552 }
1553 }
1554out:
1555 return ret;
1556}
1557
1558/*
1559 * Reset vreg by ensuring it is off during probe. A call
1560 * to enable vreg is needed to balance disable vreg
1561 */
1562static int sdhci_msm_vreg_reset(struct sdhci_msm_pltfm_data *pdata)
1563{
1564 int ret;
1565
1566 ret = sdhci_msm_setup_vreg(pdata, 1, true);
1567 if (ret)
1568 return ret;
1569 ret = sdhci_msm_setup_vreg(pdata, 0, true);
1570 return ret;
1571}
1572
1573/* This init function should be called only once for each SDHC slot */
1574static int sdhci_msm_vreg_init(struct device *dev,
1575 struct sdhci_msm_pltfm_data *pdata,
1576 bool is_init)
1577{
1578 int ret = 0;
1579 struct sdhci_msm_slot_reg_data *curr_slot;
1580 struct sdhci_msm_reg_data *curr_vdd_reg, *curr_vdd_io_reg;
1581
1582 curr_slot = pdata->vreg_data;
1583 if (!curr_slot)
1584 goto out;
1585
1586 curr_vdd_reg = curr_slot->vdd_data;
1587 curr_vdd_io_reg = curr_slot->vdd_io_data;
1588
1589 if (!is_init)
1590 /* Deregister all regulators from regulator framework */
1591 goto vdd_io_reg_deinit;
1592
1593 /*
1594 * Get the regulator handle from voltage regulator framework
1595 * and then try to set the voltage level for the regulator
1596 */
1597 if (curr_vdd_reg) {
1598 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_reg);
1599 if (ret)
1600 goto out;
1601 }
1602 if (curr_vdd_io_reg) {
1603 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_io_reg);
1604 if (ret)
1605 goto vdd_reg_deinit;
1606 }
1607 ret = sdhci_msm_vreg_reset(pdata);
1608 if (ret)
1609 dev_err(dev, "vreg reset failed (%d)\n", ret);
1610 goto out;
1611
1612vdd_io_reg_deinit:
1613 if (curr_vdd_io_reg)
1614 sdhci_msm_vreg_deinit_reg(curr_vdd_io_reg);
1615vdd_reg_deinit:
1616 if (curr_vdd_reg)
1617 sdhci_msm_vreg_deinit_reg(curr_vdd_reg);
1618out:
1619 return ret;
1620}
1621
1622
1623static int sdhci_msm_set_vdd_io_vol(struct sdhci_msm_pltfm_data *pdata,
1624 enum vdd_io_level level,
1625 unsigned int voltage_level)
1626{
1627 int ret = 0;
1628 int set_level;
1629 struct sdhci_msm_reg_data *vdd_io_reg;
1630
1631 if (!pdata->vreg_data)
1632 return ret;
1633
1634 vdd_io_reg = pdata->vreg_data->vdd_io_data;
1635 if (vdd_io_reg && vdd_io_reg->is_enabled) {
1636 switch (level) {
1637 case VDD_IO_LOW:
1638 set_level = vdd_io_reg->low_vol_level;
1639 break;
1640 case VDD_IO_HIGH:
1641 set_level = vdd_io_reg->high_vol_level;
1642 break;
1643 case VDD_IO_SET_LEVEL:
1644 set_level = voltage_level;
1645 break;
1646 default:
1647 pr_err("%s: invalid argument level = %d",
1648 __func__, level);
1649 ret = -EINVAL;
1650 return ret;
1651 }
1652 ret = sdhci_msm_vreg_set_voltage(vdd_io_reg, set_level,
1653 set_level);
1654 }
1655 return ret;
1656}
1657
1658static irqreturn_t sdhci_msm_pwr_irq(int irq, void *data)
1659{
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07001660 struct sdhci_host *host = (struct sdhci_host *)data;
1661 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1662 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301663 u8 irq_status = 0;
1664 u8 irq_ack = 0;
1665 int ret = 0;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301666 int pwr_state = 0, io_level = 0;
1667 unsigned long flags;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301668
1669 irq_status = readb_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
1670 pr_debug("%s: Received IRQ(%d), status=0x%x\n",
1671 mmc_hostname(msm_host->mmc), irq, irq_status);
1672
1673 /* Clear the interrupt */
1674 writeb_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
1675 /*
1676 * SDHC has core_mem and hc_mem device memory and these memory
1677 * addresses do not fall within 1KB region. Hence, any update to
1678 * core_mem address space would require an mb() to ensure this gets
1679 * completed before its next update to registers within hc_mem.
1680 */
1681 mb();
1682
1683 /* Handle BUS ON/OFF*/
1684 if (irq_status & CORE_PWRCTL_BUS_ON) {
1685 ret = sdhci_msm_setup_vreg(msm_host->pdata, true, false);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301686 if (!ret) {
Asutosh Das33a4ff52012-12-18 16:14:02 +05301687 ret = sdhci_msm_setup_pins(msm_host->pdata, true);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301688 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
1689 VDD_IO_HIGH, 0);
1690 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301691 if (ret)
1692 irq_ack |= CORE_PWRCTL_BUS_FAIL;
1693 else
1694 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301695
1696 pwr_state = REQ_BUS_ON;
1697 io_level = REQ_IO_HIGH;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301698 }
1699 if (irq_status & CORE_PWRCTL_BUS_OFF) {
1700 ret = sdhci_msm_setup_vreg(msm_host->pdata, false, false);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301701 if (!ret) {
Asutosh Das33a4ff52012-12-18 16:14:02 +05301702 ret = sdhci_msm_setup_pins(msm_host->pdata, false);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301703 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
1704 VDD_IO_LOW, 0);
1705 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301706 if (ret)
1707 irq_ack |= CORE_PWRCTL_BUS_FAIL;
1708 else
1709 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301710
1711 pwr_state = REQ_BUS_OFF;
1712 io_level = REQ_IO_LOW;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301713 }
1714 /* Handle IO LOW/HIGH */
1715 if (irq_status & CORE_PWRCTL_IO_LOW) {
1716 /* Switch voltage Low */
1717 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_LOW, 0);
1718 if (ret)
1719 irq_ack |= CORE_PWRCTL_IO_FAIL;
1720 else
1721 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301722
1723 io_level = REQ_IO_LOW;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301724 }
1725 if (irq_status & CORE_PWRCTL_IO_HIGH) {
1726 /* Switch voltage High */
1727 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_HIGH, 0);
1728 if (ret)
1729 irq_ack |= CORE_PWRCTL_IO_FAIL;
1730 else
1731 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301732
1733 io_level = REQ_IO_HIGH;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301734 }
1735
1736 /* ACK status to the core */
1737 writeb_relaxed(irq_ack, (msm_host->core_mem + CORE_PWRCTL_CTL));
1738 /*
1739 * SDHC has core_mem and hc_mem device memory and these memory
1740 * addresses do not fall within 1KB region. Hence, any update to
1741 * core_mem address space would require an mb() to ensure this gets
1742 * completed before its next update to registers within hc_mem.
1743 */
1744 mb();
1745
Sahitya Tummala179e7382013-03-20 19:24:01 +05301746 if (io_level & REQ_IO_HIGH)
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07001747 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
1748 ~CORE_IO_PAD_PWR_SWITCH),
1749 host->ioaddr + CORE_VENDOR_SPEC);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301750 else if (io_level & REQ_IO_LOW)
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07001751 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
1752 CORE_IO_PAD_PWR_SWITCH),
1753 host->ioaddr + CORE_VENDOR_SPEC);
1754 mb();
1755
Asutosh Das33a4ff52012-12-18 16:14:02 +05301756 pr_debug("%s: Handled IRQ(%d), ret=%d, ack=0x%x\n",
1757 mmc_hostname(msm_host->mmc), irq, ret, irq_ack);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301758 spin_lock_irqsave(&host->lock, flags);
1759 if (pwr_state)
1760 msm_host->curr_pwr_state = pwr_state;
1761 if (io_level)
1762 msm_host->curr_io_level = io_level;
1763 complete(&msm_host->pwr_irq_completion);
1764 spin_unlock_irqrestore(&host->lock, flags);
1765
Asutosh Das33a4ff52012-12-18 16:14:02 +05301766 return IRQ_HANDLED;
1767}
1768
1769/* This function returns the max. current supported by VDD rail in mA */
1770static unsigned int sdhci_msm_get_vreg_vdd_max_current(struct sdhci_msm_host
1771 *host)
1772{
1773 struct sdhci_msm_slot_reg_data *curr_slot = host->pdata->vreg_data;
1774 if (!curr_slot)
1775 return 0;
1776 if (curr_slot->vdd_data)
1777 return curr_slot->vdd_data->hpm_uA / 1000;
1778 else
1779 return 0;
1780}
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301781static ssize_t
1782show_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
1783 char *buf)
1784{
1785 struct sdhci_host *host = dev_get_drvdata(dev);
1786 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1787 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1788
1789 return snprintf(buf, PAGE_SIZE, "%u\n",
1790 msm_host->msm_bus_vote.is_max_bw_needed);
1791}
1792
1793static ssize_t
1794store_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
1795 const char *buf, size_t count)
1796{
1797 struct sdhci_host *host = dev_get_drvdata(dev);
1798 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1799 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1800 uint32_t value;
1801 unsigned long flags;
1802
1803 if (!kstrtou32(buf, 0, &value)) {
1804 spin_lock_irqsave(&host->lock, flags);
1805 msm_host->msm_bus_vote.is_max_bw_needed = !!value;
1806 spin_unlock_irqrestore(&host->lock, flags);
1807 }
1808 return count;
1809}
Asutosh Das33a4ff52012-12-18 16:14:02 +05301810
Sahitya Tummala179e7382013-03-20 19:24:01 +05301811static void sdhci_msm_check_power_status(struct sdhci_host *host, u32 req_type)
Asutosh Das33a4ff52012-12-18 16:14:02 +05301812{
1813 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1814 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301815 unsigned long flags;
1816 bool done = false;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301817
Sahitya Tummala179e7382013-03-20 19:24:01 +05301818 spin_lock_irqsave(&host->lock, flags);
1819 pr_debug("%s: %s: request %d curr_pwr_state %x curr_io_level %x\n",
1820 mmc_hostname(host->mmc), __func__, req_type,
1821 msm_host->curr_pwr_state, msm_host->curr_io_level);
1822 if ((req_type & msm_host->curr_pwr_state) ||
1823 (req_type & msm_host->curr_io_level))
1824 done = true;
1825 spin_unlock_irqrestore(&host->lock, flags);
Asutosh Das33a4ff52012-12-18 16:14:02 +05301826
Sahitya Tummala179e7382013-03-20 19:24:01 +05301827 /*
1828 * This is needed here to hanlde a case where IRQ gets
1829 * triggered even before this function is called so that
1830 * x->done counter of completion gets reset. Otherwise,
1831 * next call to wait_for_completion returns immediately
1832 * without actually waiting for the IRQ to be handled.
1833 */
1834 if (done)
1835 init_completion(&msm_host->pwr_irq_completion);
1836 else
1837 wait_for_completion(&msm_host->pwr_irq_completion);
1838
1839 pr_debug("%s: %s: request %d done\n", mmc_hostname(host->mmc),
1840 __func__, req_type);
Asutosh Das33a4ff52012-12-18 16:14:02 +05301841}
1842
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07001843static void sdhci_msm_toggle_cdr(struct sdhci_host *host, bool enable)
1844{
1845 if (enable)
1846 writel_relaxed((readl_relaxed(host->ioaddr +
1847 CORE_DLL_CONFIG) | CORE_CDR_EN),
1848 host->ioaddr + CORE_DLL_CONFIG);
1849 else
1850 writel_relaxed((readl_relaxed(host->ioaddr +
1851 CORE_DLL_CONFIG) & ~CORE_CDR_EN),
1852 host->ioaddr + CORE_DLL_CONFIG);
1853}
1854
Asutosh Das3781bd82013-01-10 21:11:04 +05301855static unsigned int sdhci_msm_max_segs(void)
1856{
1857 return SDHCI_MSM_MAX_SEGMENTS;
1858}
1859
Sahitya Tummala00240122013-02-28 19:50:51 +05301860static unsigned int sdhci_msm_get_min_clock(struct sdhci_host *host)
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301861{
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301862 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1863 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301864
Sahitya Tummala00240122013-02-28 19:50:51 +05301865 return msm_host->pdata->sup_clk_table[0];
1866}
1867
1868static unsigned int sdhci_msm_get_max_clock(struct sdhci_host *host)
1869{
1870 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1871 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1872 int max_clk_index = msm_host->pdata->sup_clk_cnt;
1873
1874 return msm_host->pdata->sup_clk_table[max_clk_index - 1];
1875}
1876
1877static unsigned int sdhci_msm_get_sup_clk_rate(struct sdhci_host *host,
1878 u32 req_clk)
1879{
1880 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1881 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1882 unsigned int sel_clk = -1;
1883 unsigned char cnt;
1884
1885 if (req_clk < sdhci_msm_get_min_clock(host)) {
1886 sel_clk = sdhci_msm_get_min_clock(host);
1887 return sel_clk;
1888 }
1889
1890 for (cnt = 0; cnt < msm_host->pdata->sup_clk_cnt; cnt++) {
1891 if (msm_host->pdata->sup_clk_table[cnt] > req_clk) {
1892 break;
1893 } else if (msm_host->pdata->sup_clk_table[cnt] == req_clk) {
1894 sel_clk = msm_host->pdata->sup_clk_table[cnt];
1895 break;
1896 } else {
1897 sel_clk = msm_host->pdata->sup_clk_table[cnt];
1898 }
1899 }
1900 return sel_clk;
1901}
1902
1903static int sdhci_msm_prepare_clocks(struct sdhci_host *host, bool enable)
1904{
1905 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1906 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1907 int rc = 0;
1908
1909 if (enable && !atomic_read(&msm_host->clks_on)) {
1910 pr_debug("%s: request to enable clocks\n",
1911 mmc_hostname(host->mmc));
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301912 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
1913 rc = clk_prepare_enable(msm_host->bus_clk);
1914 if (rc) {
1915 pr_err("%s: %s: failed to enable the bus-clock with error %d\n",
1916 mmc_hostname(host->mmc), __func__, rc);
1917 goto out;
1918 }
1919 }
1920 if (!IS_ERR(msm_host->pclk)) {
1921 rc = clk_prepare_enable(msm_host->pclk);
1922 if (rc) {
1923 pr_err("%s: %s: failed to enable the pclk with error %d\n",
1924 mmc_hostname(host->mmc), __func__, rc);
1925 goto disable_bus_clk;
1926 }
1927 }
1928 rc = clk_prepare_enable(msm_host->clk);
1929 if (rc) {
1930 pr_err("%s: %s: failed to enable the host-clk with error %d\n",
1931 mmc_hostname(host->mmc), __func__, rc);
1932 goto disable_pclk;
1933 }
1934 mb();
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301935
Sahitya Tummala00240122013-02-28 19:50:51 +05301936 } else if (!enable && atomic_read(&msm_host->clks_on)) {
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301937 pr_debug("%s: request to disable clocks\n",
1938 mmc_hostname(host->mmc));
1939 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
1940 mb();
1941 clk_disable_unprepare(msm_host->clk);
1942 if (!IS_ERR(msm_host->pclk))
1943 clk_disable_unprepare(msm_host->pclk);
1944 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
1945 clk_disable_unprepare(msm_host->bus_clk);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301946 }
Sahitya Tummala00240122013-02-28 19:50:51 +05301947 atomic_set(&msm_host->clks_on, enable);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301948 goto out;
1949disable_pclk:
1950 if (!IS_ERR_OR_NULL(msm_host->pclk))
1951 clk_disable_unprepare(msm_host->pclk);
1952disable_bus_clk:
1953 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
1954 clk_disable_unprepare(msm_host->bus_clk);
1955out:
Sahitya Tummala00240122013-02-28 19:50:51 +05301956 return rc;
1957}
1958
1959static void sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock)
1960{
1961 int rc;
1962 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1963 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1964 struct mmc_ios curr_ios = host->mmc->ios;
1965 u32 sup_clock, ddr_clock;
1966
1967 if (!clock) {
1968 sdhci_msm_prepare_clocks(host, false);
1969 host->clock = clock;
1970 return;
1971 }
1972
1973 rc = sdhci_msm_prepare_clocks(host, true);
1974 if (rc)
1975 return;
1976
1977 sup_clock = sdhci_msm_get_sup_clk_rate(host, clock);
1978 if (curr_ios.timing == MMC_TIMING_UHS_DDR50) {
1979 /*
1980 * The SDHC requires internal clock frequency to be double the
1981 * actual clock that will be set for DDR mode. The controller
1982 * uses the faster clock(100MHz) for some of its parts and send
1983 * the actual required clock (50MHz) to the card.
1984 */
1985 ddr_clock = clock * 2;
1986 sup_clock = sdhci_msm_get_sup_clk_rate(host,
1987 ddr_clock);
1988 }
1989 if (sup_clock != msm_host->clk_rate) {
1990 pr_debug("%s: %s: setting clk rate to %u\n",
1991 mmc_hostname(host->mmc), __func__, sup_clock);
1992 rc = clk_set_rate(msm_host->clk, sup_clock);
1993 if (rc) {
1994 pr_err("%s: %s: Failed to set rate %u for host-clk : %d\n",
1995 mmc_hostname(host->mmc), __func__,
1996 sup_clock, rc);
1997 return;
1998 }
1999 msm_host->clk_rate = sup_clock;
2000 host->clock = clock;
2001 }
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302002}
2003
Sahitya Tummala5e4f9642013-03-21 11:13:25 +05302004static int sdhci_msm_set_uhs_signaling(struct sdhci_host *host,
2005 unsigned int uhs)
2006{
2007 u16 ctrl_2;
2008
2009 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2010 /* Select Bus Speed Mode for host */
2011 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
2012 if (uhs == MMC_TIMING_MMC_HS200)
2013 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2014 else if (uhs == MMC_TIMING_UHS_SDR12)
2015 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
2016 else if (uhs == MMC_TIMING_UHS_SDR25)
2017 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
2018 else if (uhs == MMC_TIMING_UHS_SDR50)
2019 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
2020 else if (uhs == MMC_TIMING_UHS_SDR104)
2021 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2022 else if (uhs == MMC_TIMING_UHS_DDR50)
2023 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Sahitya Tummala00240122013-02-28 19:50:51 +05302024 /*
2025 * When clock frquency is less than 100MHz, the feedback clock must be
2026 * provided and DLL must not be used so that tuning can be skipped. To
2027 * provide feedback clock, the mode selection can be any value less
2028 * than 3'b011 in bits [2:0] of HOST CONTROL2 register.
2029 */
2030 if (host->clock <= (100 * 1000 * 1000) &&
2031 (uhs == MMC_TIMING_MMC_HS200 ||
2032 uhs == MMC_TIMING_UHS_SDR104))
2033 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
2034
Sahitya Tummala5e4f9642013-03-21 11:13:25 +05302035 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
2036
2037 return 0;
2038}
2039
Asutosh Das33a4ff52012-12-18 16:14:02 +05302040static struct sdhci_ops sdhci_msm_ops = {
Sahitya Tummala5e4f9642013-03-21 11:13:25 +05302041 .set_uhs_signaling = sdhci_msm_set_uhs_signaling,
Asutosh Das33a4ff52012-12-18 16:14:02 +05302042 .check_power_status = sdhci_msm_check_power_status,
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07002043 .execute_tuning = sdhci_msm_execute_tuning,
2044 .toggle_cdr = sdhci_msm_toggle_cdr,
Asutosh Das3781bd82013-01-10 21:11:04 +05302045 .get_max_segments = sdhci_msm_max_segs,
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302046 .set_clock = sdhci_msm_set_clock,
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302047 .platform_bus_voting = sdhci_msm_bus_voting,
Sahitya Tummala00240122013-02-28 19:50:51 +05302048 .get_min_clock = sdhci_msm_get_min_clock,
2049 .get_max_clock = sdhci_msm_get_max_clock,
Asutosh Das33a4ff52012-12-18 16:14:02 +05302050};
2051
2052static int __devinit sdhci_msm_probe(struct platform_device *pdev)
2053{
2054 struct sdhci_host *host;
2055 struct sdhci_pltfm_host *pltfm_host;
2056 struct sdhci_msm_host *msm_host;
2057 struct resource *core_memres = NULL;
Asutosh Dasbbc84782013-02-11 15:31:35 +05302058 int ret = 0, dead = 0;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302059 u32 vdd_max_current;
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002060 u32 host_version;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302061
2062 pr_debug("%s: Enter %s\n", dev_name(&pdev->dev), __func__);
2063 msm_host = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_msm_host),
2064 GFP_KERNEL);
2065 if (!msm_host) {
2066 ret = -ENOMEM;
2067 goto out;
2068 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05302069
2070 msm_host->sdhci_msm_pdata.ops = &sdhci_msm_ops;
2071 host = sdhci_pltfm_init(pdev, &msm_host->sdhci_msm_pdata);
2072 if (IS_ERR(host)) {
2073 ret = PTR_ERR(host);
2074 goto out;
2075 }
2076
2077 pltfm_host = sdhci_priv(host);
2078 pltfm_host->priv = msm_host;
2079 msm_host->mmc = host->mmc;
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302080 msm_host->pdev = pdev;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302081
2082 /* Extract platform data */
2083 if (pdev->dev.of_node) {
2084 msm_host->pdata = sdhci_msm_populate_pdata(&pdev->dev);
2085 if (!msm_host->pdata) {
2086 dev_err(&pdev->dev, "DT parsing error\n");
2087 goto pltfm_free;
2088 }
2089 } else {
2090 dev_err(&pdev->dev, "No device tree node\n");
2091 goto pltfm_free;
2092 }
2093
2094 /* Setup Clocks */
2095
2096 /* Setup SDCC bus voter clock. */
2097 msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus_clk");
2098 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
2099 /* Vote for max. clk rate for max. performance */
2100 ret = clk_set_rate(msm_host->bus_clk, INT_MAX);
2101 if (ret)
2102 goto pltfm_free;
2103 ret = clk_prepare_enable(msm_host->bus_clk);
2104 if (ret)
2105 goto pltfm_free;
2106 }
2107
2108 /* Setup main peripheral bus clock */
2109 msm_host->pclk = devm_clk_get(&pdev->dev, "iface_clk");
2110 if (!IS_ERR(msm_host->pclk)) {
2111 ret = clk_prepare_enable(msm_host->pclk);
2112 if (ret)
2113 goto bus_clk_disable;
2114 }
2115
2116 /* Setup SDC MMC clock */
2117 msm_host->clk = devm_clk_get(&pdev->dev, "core_clk");
2118 if (IS_ERR(msm_host->clk)) {
2119 ret = PTR_ERR(msm_host->clk);
2120 goto pclk_disable;
2121 }
2122
2123 ret = clk_prepare_enable(msm_host->clk);
2124 if (ret)
2125 goto pclk_disable;
2126
Sahitya Tummala00240122013-02-28 19:50:51 +05302127 /* Set to the minimum supported clock frequency */
2128 ret = clk_set_rate(msm_host->clk, sdhci_msm_get_min_clock(host));
2129 if (ret) {
2130 dev_err(&pdev->dev, "MClk rate set failed (%d)\n", ret);
2131 goto clk_disable;
2132 }
2133 msm_host->clk_rate = sdhci_msm_get_min_clock(host);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302134 atomic_set(&msm_host->clks_on, 1);
Sahitya Tummala00240122013-02-28 19:50:51 +05302135
Asutosh Das33a4ff52012-12-18 16:14:02 +05302136 /* Setup regulators */
2137 ret = sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, true);
2138 if (ret) {
2139 dev_err(&pdev->dev, "Regulator setup failed (%d)\n", ret);
2140 goto clk_disable;
2141 }
2142
2143 /* Reset the core and Enable SDHC mode */
2144 core_memres = platform_get_resource_byname(pdev,
2145 IORESOURCE_MEM, "core_mem");
2146 msm_host->core_mem = devm_ioremap(&pdev->dev, core_memres->start,
2147 resource_size(core_memres));
2148
2149 if (!msm_host->core_mem) {
2150 dev_err(&pdev->dev, "Failed to remap registers\n");
2151 ret = -ENOMEM;
2152 goto vreg_deinit;
2153 }
2154
2155 /* Set SW_RST bit in POWER register (Offset 0x0) */
2156 writel_relaxed(CORE_SW_RST, msm_host->core_mem + CORE_POWER);
2157 /* Set HC_MODE_EN bit in HC_MODE register */
2158 writel_relaxed(HC_MODE_EN, (msm_host->core_mem + CORE_HC_MODE));
2159
2160 /*
2161 * Following are the deviations from SDHC spec v3.0 -
2162 * 1. Card detection is handled using separate GPIO.
2163 * 2. Bus power control is handled by interacting with PMIC.
2164 */
2165 host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
2166 host->quirks |= SDHCI_QUIRK_SINGLE_POWER_WRITE;
Sahitya Tummala00240122013-02-28 19:50:51 +05302167 host->quirks |= SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN;
2168 host->quirks2 |= SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK;
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302169 host->quirks2 |= SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302170
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002171 host_version = readl_relaxed((host->ioaddr + SDHCI_HOST_VERSION));
2172 dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n",
2173 host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >>
2174 SDHCI_VENDOR_VER_SHIFT));
2175 if (((host_version & SDHCI_VENDOR_VER_MASK) >>
2176 SDHCI_VENDOR_VER_SHIFT) == SDHCI_VER_100) {
2177 /*
2178 * Add 40us delay in interrupt handler when
2179 * operating at initialization frequency(400KHz).
2180 */
2181 host->quirks2 |= SDHCI_QUIRK2_SLOW_INT_CLR;
2182 /*
2183 * Set Software Reset for DAT line in Software
2184 * Reset Register (Bit 2).
2185 */
2186 host->quirks2 |= SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT;
2187 }
2188
2189 /* Setup PWRCTL irq */
Asutosh Dasbbc84782013-02-11 15:31:35 +05302190 msm_host->pwr_irq = platform_get_irq_byname(pdev, "pwr_irq");
2191 if (msm_host->pwr_irq < 0) {
Asutosh Das33a4ff52012-12-18 16:14:02 +05302192 dev_err(&pdev->dev, "Failed to get pwr_irq by name (%d)\n",
Asutosh Dasbbc84782013-02-11 15:31:35 +05302193 msm_host->pwr_irq);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302194 goto vreg_deinit;
2195 }
Asutosh Dasbbc84782013-02-11 15:31:35 +05302196 ret = devm_request_threaded_irq(&pdev->dev, msm_host->pwr_irq, NULL,
Asutosh Das33a4ff52012-12-18 16:14:02 +05302197 sdhci_msm_pwr_irq, IRQF_ONESHOT,
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07002198 dev_name(&pdev->dev), host);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302199 if (ret) {
2200 dev_err(&pdev->dev, "Request threaded irq(%d) failed (%d)\n",
Asutosh Dasbbc84782013-02-11 15:31:35 +05302201 msm_host->pwr_irq, ret);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302202 goto vreg_deinit;
2203 }
2204
2205 /* Enable pwr irq interrupts */
2206 writel_relaxed(INT_MASK, (msm_host->core_mem + CORE_PWRCTL_MASK));
2207
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302208 /* Set clock gating delay to be used when CONFIG_MMC_CLKGATE is set */
2209 msm_host->mmc->clkgate_delay = SDHCI_MSM_MMC_CLK_GATE_DELAY;
2210
Asutosh Das33a4ff52012-12-18 16:14:02 +05302211 /* Set host capabilities */
2212 msm_host->mmc->caps |= msm_host->pdata->mmc_bus_width;
2213 msm_host->mmc->caps |= msm_host->pdata->caps;
2214
2215 vdd_max_current = sdhci_msm_get_vreg_vdd_max_current(msm_host);
2216 if (vdd_max_current >= 800)
2217 msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_800;
2218 else if (vdd_max_current >= 600)
2219 msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_600;
2220 else if (vdd_max_current >= 400)
2221 msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_400;
2222 else
2223 msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_200;
2224
2225 if (vdd_max_current > 150)
2226 msm_host->mmc->caps |= MMC_CAP_SET_XPC_180 |
2227 MMC_CAP_SET_XPC_300|
2228 MMC_CAP_SET_XPC_330;
2229
Sahitya Tummalacbe1b7a2013-02-28 12:21:58 +05302230 msm_host->mmc->caps |= MMC_CAP_HW_RESET;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302231 msm_host->mmc->caps2 |= msm_host->pdata->caps2;
Asutosh Dasbbc84782013-02-11 15:31:35 +05302232 msm_host->mmc->caps2 |= MMC_CAP2_CORE_RUNTIME_PM;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302233 msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR;
2234 msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR_CONTROL;
2235 msm_host->mmc->caps2 |= (MMC_CAP2_BOOTPART_NOACC |
2236 MMC_CAP2_DETECT_ON_ERR);
2237 msm_host->mmc->caps2 |= MMC_CAP2_SANITIZE;
2238 msm_host->mmc->caps2 |= MMC_CAP2_CACHE_CTRL;
2239 msm_host->mmc->caps2 |= MMC_CAP2_INIT_BKOPS;
2240 msm_host->mmc->caps2 |= MMC_CAP2_POWEROFF_NOTIFY;
Sahitya Tummala00240122013-02-28 19:50:51 +05302241 msm_host->mmc->caps2 |= MMC_CAP2_CLK_SCALE;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302242
2243 if (msm_host->pdata->nonremovable)
2244 msm_host->mmc->caps |= MMC_CAP_NONREMOVABLE;
2245
Sahitya Tummalab4e84042013-03-10 07:03:17 +05302246 host->cpu_dma_latency_us = msm_host->pdata->cpu_dma_latency_us;
2247
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302248 ret = sdhci_msm_bus_register(msm_host, pdev);
2249 if (ret)
2250 goto vreg_deinit;
2251
2252 if (msm_host->msm_bus_vote.client_handle)
2253 INIT_DELAYED_WORK(&msm_host->msm_bus_vote.vote_work,
2254 sdhci_msm_bus_work);
2255
Sahitya Tummala179e7382013-03-20 19:24:01 +05302256 init_completion(&msm_host->pwr_irq_completion);
2257
Sahitya Tummala62448d92013-03-12 14:57:46 +05302258 if (gpio_is_valid(msm_host->pdata->status_gpio)) {
2259 ret = mmc_cd_gpio_request(msm_host->mmc,
2260 msm_host->pdata->status_gpio);
2261 if (ret) {
2262 dev_err(&pdev->dev, "%s: Failed to request card detection IRQ %d\n",
2263 __func__, ret);
2264 goto bus_unregister;
2265 }
2266 }
2267
Sahitya Tummala2fa7eb12013-03-20 19:34:59 +05302268 if (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(32))) {
2269 host->dma_mask = DMA_BIT_MASK(32);
2270 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2271 } else {
2272 dev_err(&pdev->dev, "%s: Failed to set dma mask\n", __func__);
2273 }
2274
Asutosh Das33a4ff52012-12-18 16:14:02 +05302275 ret = sdhci_add_host(host);
2276 if (ret) {
2277 dev_err(&pdev->dev, "Add host failed (%d)\n", ret);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302278 goto free_cd_gpio;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302279 }
2280
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302281 msm_host->msm_bus_vote.max_bus_bw.show = show_sdhci_max_bus_bw;
2282 msm_host->msm_bus_vote.max_bus_bw.store = store_sdhci_max_bus_bw;
2283 sysfs_attr_init(&msm_host->msm_bus_vote.max_bus_bw.attr);
2284 msm_host->msm_bus_vote.max_bus_bw.attr.name = "max_bus_bw";
2285 msm_host->msm_bus_vote.max_bus_bw.attr.mode = S_IRUGO | S_IWUSR;
2286 ret = device_create_file(&pdev->dev,
2287 &msm_host->msm_bus_vote.max_bus_bw);
2288 if (ret)
2289 goto remove_host;
2290
Asutosh Dasbbc84782013-02-11 15:31:35 +05302291 ret = pm_runtime_set_active(&pdev->dev);
2292 if (ret)
2293 pr_err("%s: %s: pm_runtime_set_active failed: err: %d\n",
2294 mmc_hostname(host->mmc), __func__, ret);
2295 else
2296 pm_runtime_enable(&pdev->dev);
2297
Asutosh Das33a4ff52012-12-18 16:14:02 +05302298 /* Successful initialization */
2299 goto out;
2300
2301remove_host:
2302 dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
2303 sdhci_remove_host(host, dead);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302304free_cd_gpio:
2305 if (gpio_is_valid(msm_host->pdata->status_gpio))
2306 mmc_cd_gpio_free(msm_host->mmc);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302307bus_unregister:
2308 sdhci_msm_bus_unregister(msm_host);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302309vreg_deinit:
2310 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
2311clk_disable:
2312 if (!IS_ERR(msm_host->clk))
2313 clk_disable_unprepare(msm_host->clk);
2314pclk_disable:
2315 if (!IS_ERR(msm_host->pclk))
2316 clk_disable_unprepare(msm_host->pclk);
2317bus_clk_disable:
2318 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2319 clk_disable_unprepare(msm_host->bus_clk);
2320pltfm_free:
2321 sdhci_pltfm_free(pdev);
2322out:
2323 pr_debug("%s: Exit %s\n", dev_name(&pdev->dev), __func__);
2324 return ret;
2325}
2326
2327static int __devexit sdhci_msm_remove(struct platform_device *pdev)
2328{
2329 struct sdhci_host *host = platform_get_drvdata(pdev);
2330 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2331 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2332 struct sdhci_msm_pltfm_data *pdata = msm_host->pdata;
2333 int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) ==
2334 0xffffffff);
2335
2336 pr_debug("%s: %s\n", dev_name(&pdev->dev), __func__);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302337 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302338 sdhci_remove_host(host, dead);
Asutosh Dasbbc84782013-02-11 15:31:35 +05302339 pm_runtime_disable(&pdev->dev);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302340 sdhci_pltfm_free(pdev);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302341
2342 if (gpio_is_valid(msm_host->pdata->status_gpio))
2343 mmc_cd_gpio_free(msm_host->mmc);
2344
Asutosh Das33a4ff52012-12-18 16:14:02 +05302345 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302346
Asutosh Das33a4ff52012-12-18 16:14:02 +05302347 if (pdata->pin_data)
Asutosh Das390519d2012-12-21 12:21:42 +05302348 sdhci_msm_setup_pins(pdata, false);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302349
2350 if (msm_host->msm_bus_vote.client_handle) {
2351 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2352 sdhci_msm_bus_unregister(msm_host);
2353 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05302354 return 0;
2355}
2356
Asutosh Dasbbc84782013-02-11 15:31:35 +05302357static int sdhci_msm_runtime_suspend(struct device *dev)
2358{
2359 struct sdhci_host *host = dev_get_drvdata(dev);
2360 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2361 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2362
2363 disable_irq(host->irq);
2364 disable_irq(msm_host->pwr_irq);
2365
2366 return 0;
2367}
2368
2369static int sdhci_msm_runtime_resume(struct device *dev)
2370{
2371 struct sdhci_host *host = dev_get_drvdata(dev);
2372 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2373 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2374
2375 enable_irq(msm_host->pwr_irq);
2376 enable_irq(host->irq);
2377
2378 return 0;
2379}
2380
2381#ifdef CONFIG_PM_SLEEP
2382
2383static int sdhci_msm_suspend(struct device *dev)
2384{
2385 struct sdhci_host *host = dev_get_drvdata(dev);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302386 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2387 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Dasbbc84782013-02-11 15:31:35 +05302388 int ret = 0;
2389
Sahitya Tummala62448d92013-03-12 14:57:46 +05302390 if (gpio_is_valid(msm_host->pdata->status_gpio))
2391 mmc_cd_gpio_free(msm_host->mmc);
2392
Asutosh Dasbbc84782013-02-11 15:31:35 +05302393 if (pm_runtime_suspended(dev)) {
2394 pr_debug("%s: %s: already runtime suspended\n",
2395 mmc_hostname(host->mmc), __func__);
2396 goto out;
2397 }
2398
2399 return sdhci_msm_runtime_suspend(dev);
2400out:
2401 return ret;
2402}
2403
2404static int sdhci_msm_resume(struct device *dev)
2405{
2406 struct sdhci_host *host = dev_get_drvdata(dev);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302407 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2408 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Dasbbc84782013-02-11 15:31:35 +05302409 int ret = 0;
2410
Sahitya Tummala62448d92013-03-12 14:57:46 +05302411 if (gpio_is_valid(msm_host->pdata->status_gpio)) {
2412 ret = mmc_cd_gpio_request(msm_host->mmc,
2413 msm_host->pdata->status_gpio);
2414 if (ret)
2415 pr_err("%s: %s: Failed to request card detection IRQ %d\n",
2416 mmc_hostname(host->mmc), __func__, ret);
2417 }
2418
Asutosh Dasbbc84782013-02-11 15:31:35 +05302419 if (pm_runtime_suspended(dev)) {
2420 pr_debug("%s: %s: runtime suspended, defer system resume\n",
2421 mmc_hostname(host->mmc), __func__);
2422 goto out;
2423 }
2424
2425 return sdhci_msm_runtime_resume(dev);
2426out:
2427 return ret;
2428}
2429#endif
2430
2431#ifdef CONFIG_PM
2432static const struct dev_pm_ops sdhci_msm_pmops = {
2433 SET_SYSTEM_SLEEP_PM_OPS(sdhci_msm_suspend, sdhci_msm_resume)
2434 SET_RUNTIME_PM_OPS(sdhci_msm_runtime_suspend, sdhci_msm_runtime_resume,
2435 NULL)
2436};
2437
2438#define SDHCI_MSM_PMOPS (&sdhci_msm_pmops)
2439
2440#else
2441#define SDHCI_PM_OPS NULL
2442#endif
Asutosh Das33a4ff52012-12-18 16:14:02 +05302443static const struct of_device_id sdhci_msm_dt_match[] = {
2444 {.compatible = "qcom,sdhci-msm"},
2445};
2446MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match);
2447
2448static struct platform_driver sdhci_msm_driver = {
2449 .probe = sdhci_msm_probe,
2450 .remove = __devexit_p(sdhci_msm_remove),
2451 .driver = {
2452 .name = "sdhci_msm",
2453 .owner = THIS_MODULE,
2454 .of_match_table = sdhci_msm_dt_match,
Asutosh Dasbbc84782013-02-11 15:31:35 +05302455 .pm = SDHCI_MSM_PMOPS,
Asutosh Das33a4ff52012-12-18 16:14:02 +05302456 },
2457};
2458
2459module_platform_driver(sdhci_msm_driver);
2460
2461MODULE_DESCRIPTION("Qualcomm Secure Digital Host Controller Interface driver");
2462MODULE_LICENSE("GPL v2");