blob: 2769709e470dd797e85716790692e4efb75c11d8 [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
Venkat Gopalakrishnanc61ab7e2013-03-11 12:17:57 -0700106static int disable_slots;
107/* root can write, others read */
108module_param(disable_slots, int, S_IRUGO|S_IWUSR);
109
Asutosh Das33a4ff52012-12-18 16:14:02 +0530110/* This structure keeps information per regulator */
111struct sdhci_msm_reg_data {
112 /* voltage regulator handle */
113 struct regulator *reg;
114 /* regulator name */
115 const char *name;
116 /* voltage level to be set */
117 u32 low_vol_level;
118 u32 high_vol_level;
119 /* Load values for low power and high power mode */
120 u32 lpm_uA;
121 u32 hpm_uA;
122
123 /* is this regulator enabled? */
124 bool is_enabled;
125 /* is this regulator needs to be always on? */
126 bool is_always_on;
127 /* is low power mode setting required for this regulator? */
128 bool lpm_sup;
129};
130
131/*
132 * This structure keeps information for all the
133 * regulators required for a SDCC slot.
134 */
135struct sdhci_msm_slot_reg_data {
136 /* keeps VDD/VCC regulator info */
137 struct sdhci_msm_reg_data *vdd_data;
138 /* keeps VDD IO regulator info */
139 struct sdhci_msm_reg_data *vdd_io_data;
140};
141
142struct sdhci_msm_gpio {
143 u32 no;
144 const char *name;
145 bool is_enabled;
146};
147
148struct sdhci_msm_gpio_data {
149 struct sdhci_msm_gpio *gpio;
150 u8 size;
151};
152
Asutosh Das390519d2012-12-21 12:21:42 +0530153struct sdhci_msm_pad_pull {
154 enum msm_tlmm_pull_tgt no;
155 u32 val;
156};
157
158struct sdhci_msm_pad_pull_data {
159 struct sdhci_msm_pad_pull *on;
160 struct sdhci_msm_pad_pull *off;
161 u8 size;
162};
163
164struct sdhci_msm_pad_drv {
165 enum msm_tlmm_hdrive_tgt no;
166 u32 val;
167};
168
169struct sdhci_msm_pad_drv_data {
170 struct sdhci_msm_pad_drv *on;
171 struct sdhci_msm_pad_drv *off;
172 u8 size;
173};
174
175struct sdhci_msm_pad_data {
176 struct sdhci_msm_pad_pull_data *pull;
177 struct sdhci_msm_pad_drv_data *drv;
178};
179
180
Asutosh Das33a4ff52012-12-18 16:14:02 +0530181struct sdhci_msm_pin_data {
182 /*
183 * = 1 if controller pins are using gpios
184 * = 0 if controller has dedicated MSM pads
185 */
Asutosh Das390519d2012-12-21 12:21:42 +0530186 u8 is_gpio;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530187 bool cfg_sts;
188 struct sdhci_msm_gpio_data *gpio_data;
Asutosh Das390519d2012-12-21 12:21:42 +0530189 struct sdhci_msm_pad_data *pad_data;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530190};
191
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +0530192struct sdhci_msm_bus_voting_data {
193 struct msm_bus_scale_pdata *bus_pdata;
194 unsigned int *bw_vecs;
195 unsigned int bw_vecs_size;
196};
197
Asutosh Das33a4ff52012-12-18 16:14:02 +0530198struct sdhci_msm_pltfm_data {
199 /* Supported UHS-I Modes */
200 u32 caps;
201
202 /* More capabilities */
203 u32 caps2;
204
205 unsigned long mmc_bus_width;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530206 struct sdhci_msm_slot_reg_data *vreg_data;
207 bool nonremovable;
208 struct sdhci_msm_pin_data *pin_data;
Sahitya Tummalab4e84042013-03-10 07:03:17 +0530209 u32 cpu_dma_latency_us;
Sahitya Tummala62448d92013-03-12 14:57:46 +0530210 int status_gpio; /* card detection GPIO that is configured as IRQ */
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +0530211 struct sdhci_msm_bus_voting_data *voting_data;
Sahitya Tummala00240122013-02-28 19:50:51 +0530212 u32 *sup_clk_table;
213 unsigned char sup_clk_cnt;
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +0530214};
215
216struct sdhci_msm_bus_vote {
217 uint32_t client_handle;
218 uint32_t curr_vote;
219 int min_bw_vote;
220 int max_bw_vote;
221 bool is_max_bw_needed;
222 struct delayed_work vote_work;
223 struct device_attribute max_bus_bw;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530224};
225
226struct sdhci_msm_host {
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +0530227 struct platform_device *pdev;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530228 void __iomem *core_mem; /* MSM SDCC mapped address */
Asutosh Dasbbc84782013-02-11 15:31:35 +0530229 int pwr_irq; /* power irq */
Asutosh Das33a4ff52012-12-18 16:14:02 +0530230 struct clk *clk; /* main SD/MMC bus clock */
231 struct clk *pclk; /* SDHC peripheral bus clock */
232 struct clk *bus_clk; /* SDHC bus voter clock */
Sahitya Tummala04c3a462013-01-11 11:30:45 +0530233 atomic_t clks_on; /* Set if clocks are enabled */
Asutosh Das33a4ff52012-12-18 16:14:02 +0530234 struct sdhci_msm_pltfm_data *pdata;
235 struct mmc_host *mmc;
236 struct sdhci_pltfm_data sdhci_msm_pdata;
Sahitya Tummala179e7382013-03-20 19:24:01 +0530237 u32 curr_pwr_state;
238 u32 curr_io_level;
239 struct completion pwr_irq_completion;
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +0530240 struct sdhci_msm_bus_vote msm_bus_vote;
Sahitya Tummala00240122013-02-28 19:50:51 +0530241 u32 clk_rate; /* Keeps track of current clock rate that is set */
Asutosh Das33a4ff52012-12-18 16:14:02 +0530242};
243
244enum vdd_io_level {
245 /* set vdd_io_data->low_vol_level */
246 VDD_IO_LOW,
247 /* set vdd_io_data->high_vol_level */
248 VDD_IO_HIGH,
249 /*
250 * set whatever there in voltage_level (third argument) of
251 * sdhci_msm_set_vdd_io_vol() function.
252 */
253 VDD_IO_SET_LEVEL,
254};
255
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700256/* MSM platform specific tuning */
257static inline int msm_dll_poll_ck_out_en(struct sdhci_host *host,
258 u8 poll)
259{
260 int rc = 0;
261 u32 wait_cnt = 50;
262 u8 ck_out_en = 0;
263 struct mmc_host *mmc = host->mmc;
264
265 /* poll for CK_OUT_EN bit. max. poll time = 50us */
266 ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) &
267 CORE_CK_OUT_EN);
268
269 while (ck_out_en != poll) {
270 if (--wait_cnt == 0) {
271 pr_err("%s: %s: CK_OUT_EN bit is not %d\n",
272 mmc_hostname(mmc), __func__, poll);
273 rc = -ETIMEDOUT;
274 goto out;
275 }
276 udelay(1);
277
278 ck_out_en = !!(readl_relaxed(host->ioaddr +
279 CORE_DLL_CONFIG) & CORE_CK_OUT_EN);
280 }
281out:
282 return rc;
283}
284
285static int msm_config_cm_dll_phase(struct sdhci_host *host, u8 phase)
286{
287 int rc = 0;
288 u8 grey_coded_phase_table[] = {0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4,
289 0xC, 0xD, 0xF, 0xE, 0xA, 0xB, 0x9,
290 0x8};
291 unsigned long flags;
292 u32 config;
293 struct mmc_host *mmc = host->mmc;
294
295 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
296 spin_lock_irqsave(&host->lock, flags);
297
298 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
299 config &= ~(CORE_CDR_EN | CORE_CK_OUT_EN);
300 config |= (CORE_CDR_EXT_EN | CORE_DLL_EN);
301 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
302
303 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '0' */
304 rc = msm_dll_poll_ck_out_en(host, 0);
305 if (rc)
306 goto err_out;
307
308 /*
309 * Write the selected DLL clock output phase (0 ... 15)
310 * to CDR_SELEXT bit field of DLL_CONFIG register.
311 */
312 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
313 & ~(0xF << 20))
314 | (grey_coded_phase_table[phase] << 20)),
315 host->ioaddr + CORE_DLL_CONFIG);
316
317 /* Set CK_OUT_EN bit of DLL_CONFIG register to 1. */
318 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
319 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
320
321 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '1' */
322 rc = msm_dll_poll_ck_out_en(host, 1);
323 if (rc)
324 goto err_out;
325
326 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
327 config |= CORE_CDR_EN;
328 config &= ~CORE_CDR_EXT_EN;
329 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
330 goto out;
331
332err_out:
333 pr_err("%s: %s: Failed to set DLL phase: %d\n",
334 mmc_hostname(mmc), __func__, phase);
335out:
336 spin_unlock_irqrestore(&host->lock, flags);
337 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
338 return rc;
339}
340
341/*
342 * Find out the greatest range of consecuitive selected
343 * DLL clock output phases that can be used as sampling
344 * setting for SD3.0 UHS-I card read operation (in SDR104
345 * timing mode) or for eMMC4.5 card read operation (in HS200
346 * timing mode).
347 * Select the 3/4 of the range and configure the DLL with the
348 * selected DLL clock output phase.
349 */
350
351static int msm_find_most_appropriate_phase(struct sdhci_host *host,
352 u8 *phase_table, u8 total_phases)
353{
354 int ret;
355 u8 ranges[MAX_PHASES][MAX_PHASES] = { {0}, {0} };
356 u8 phases_per_row[MAX_PHASES] = {0};
357 int row_index = 0, col_index = 0, selected_row_index = 0, curr_max = 0;
358 int i, cnt, phase_0_raw_index = 0, phase_15_raw_index = 0;
359 bool phase_0_found = false, phase_15_found = false;
360 struct mmc_host *mmc = host->mmc;
361
362 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
363 if (!total_phases || (total_phases > MAX_PHASES)) {
364 pr_err("%s: %s: invalid argument: total_phases=%d\n",
365 mmc_hostname(mmc), __func__, total_phases);
366 return -EINVAL;
367 }
368
369 for (cnt = 0; cnt < total_phases; cnt++) {
370 ranges[row_index][col_index] = phase_table[cnt];
371 phases_per_row[row_index] += 1;
372 col_index++;
373
374 if ((cnt + 1) == total_phases) {
375 continue;
376 /* check if next phase in phase_table is consecutive or not */
377 } else if ((phase_table[cnt] + 1) != phase_table[cnt + 1]) {
378 row_index++;
379 col_index = 0;
380 }
381 }
382
383 if (row_index >= MAX_PHASES)
384 return -EINVAL;
385
386 /* Check if phase-0 is present in first valid window? */
387 if (!ranges[0][0]) {
388 phase_0_found = true;
389 phase_0_raw_index = 0;
390 /* Check if cycle exist between 2 valid windows */
391 for (cnt = 1; cnt <= row_index; cnt++) {
392 if (phases_per_row[cnt]) {
393 for (i = 0; i < phases_per_row[cnt]; i++) {
394 if (ranges[cnt][i] == 15) {
395 phase_15_found = true;
396 phase_15_raw_index = cnt;
397 break;
398 }
399 }
400 }
401 }
402 }
403
404 /* If 2 valid windows form cycle then merge them as single window */
405 if (phase_0_found && phase_15_found) {
406 /* number of phases in raw where phase 0 is present */
407 u8 phases_0 = phases_per_row[phase_0_raw_index];
408 /* number of phases in raw where phase 15 is present */
409 u8 phases_15 = phases_per_row[phase_15_raw_index];
410
411 if (phases_0 + phases_15 >= MAX_PHASES)
412 /*
413 * If there are more than 1 phase windows then total
414 * number of phases in both the windows should not be
415 * more than or equal to MAX_PHASES.
416 */
417 return -EINVAL;
418
419 /* Merge 2 cyclic windows */
420 i = phases_15;
421 for (cnt = 0; cnt < phases_0; cnt++) {
422 ranges[phase_15_raw_index][i] =
423 ranges[phase_0_raw_index][cnt];
424 if (++i >= MAX_PHASES)
425 break;
426 }
427
428 phases_per_row[phase_0_raw_index] = 0;
429 phases_per_row[phase_15_raw_index] = phases_15 + phases_0;
430 }
431
432 for (cnt = 0; cnt <= row_index; cnt++) {
433 if (phases_per_row[cnt] > curr_max) {
434 curr_max = phases_per_row[cnt];
435 selected_row_index = cnt;
436 }
437 }
438
439 i = ((curr_max * 3) / 4);
440 if (i)
441 i--;
442
443 ret = (int)ranges[selected_row_index][i];
444
445 if (ret >= MAX_PHASES) {
446 ret = -EINVAL;
447 pr_err("%s: %s: invalid phase selected=%d\n",
448 mmc_hostname(mmc), __func__, ret);
449 }
450
451 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
452 return ret;
453}
454
455static inline void msm_cm_dll_set_freq(struct sdhci_host *host)
456{
457 u32 mclk_freq = 0;
458
459 /* Program the MCLK value to MCLK_FREQ bit field */
460 if (host->clock <= 112000000)
461 mclk_freq = 0;
462 else if (host->clock <= 125000000)
463 mclk_freq = 1;
464 else if (host->clock <= 137000000)
465 mclk_freq = 2;
466 else if (host->clock <= 150000000)
467 mclk_freq = 3;
468 else if (host->clock <= 162000000)
469 mclk_freq = 4;
470 else if (host->clock <= 175000000)
471 mclk_freq = 5;
472 else if (host->clock <= 187000000)
473 mclk_freq = 6;
474 else if (host->clock <= 200000000)
475 mclk_freq = 7;
476
477 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
478 & ~(7 << 24)) | (mclk_freq << 24)),
479 host->ioaddr + CORE_DLL_CONFIG);
480}
481
482/* Initialize the DLL (Programmable Delay Line ) */
483static int msm_init_cm_dll(struct sdhci_host *host)
484{
485 struct mmc_host *mmc = host->mmc;
486 int rc = 0;
487 unsigned long flags;
488 u32 wait_cnt;
489
490 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
491 spin_lock_irqsave(&host->lock, flags);
492
493 /*
494 * Make sure that clock is always enabled when DLL
495 * tuning is in progress. Keeping PWRSAVE ON may
496 * turn off the clock. So let's disable the PWRSAVE
497 * here and re-enable it once tuning is completed.
498 */
499 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
500 & ~CORE_CLK_PWRSAVE),
501 host->ioaddr + CORE_VENDOR_SPEC);
502
503 /* Write 1 to DLL_RST bit of DLL_CONFIG register */
504 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
505 | CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
506
507 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
508 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
509 | CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
510 msm_cm_dll_set_freq(host);
511
512 /* Write 0 to DLL_RST bit of DLL_CONFIG register */
513 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
514 & ~CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
515
516 /* Write 0 to DLL_PDN bit of DLL_CONFIG register */
517 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
518 & ~CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
519
520 /* Set DLL_EN bit to 1. */
521 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
522 | CORE_DLL_EN), host->ioaddr + CORE_DLL_CONFIG);
523
524 /* Set CK_OUT_EN bit to 1. */
525 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
526 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
527
528 wait_cnt = 50;
529 /* Wait until DLL_LOCK bit of DLL_STATUS register becomes '1' */
530 while (!(readl_relaxed(host->ioaddr + CORE_DLL_STATUS) &
531 CORE_DLL_LOCK)) {
532 /* max. wait for 50us sec for LOCK bit to be set */
533 if (--wait_cnt == 0) {
534 pr_err("%s: %s: DLL failed to LOCK\n",
535 mmc_hostname(mmc), __func__);
536 rc = -ETIMEDOUT;
537 goto out;
538 }
539 /* wait for 1us before polling again */
540 udelay(1);
541 }
542
543out:
544 /* re-enable PWRSAVE */
545 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
546 CORE_CLK_PWRSAVE),
547 host->ioaddr + CORE_VENDOR_SPEC);
548 spin_unlock_irqrestore(&host->lock, flags);
549 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
550 return rc;
551}
552
553int sdhci_msm_execute_tuning(struct sdhci_host *host, u32 opcode)
554{
555 unsigned long flags;
556 u8 phase, *data_buf, tuned_phases[16], tuned_phase_cnt = 0;
557 const u32 *tuning_block_pattern = tuning_block_64;
558 int size = sizeof(tuning_block_64); /* Tuning pattern size in bytes */
559 int rc;
560 struct mmc_host *mmc = host->mmc;
Sahitya Tummala00240122013-02-28 19:50:51 +0530561 struct mmc_ios ios = host->mmc->ios;
562
563 /*
564 * Tuning is required for SDR104 and HS200 cards and if clock frequency
565 * is greater than 100MHz in these modes.
566 */
567 if (host->clock <= (100 * 1000 * 1000) ||
568 !(ios.timing == MMC_TIMING_MMC_HS200 ||
569 ios.timing == MMC_TIMING_UHS_SDR104))
570 return 0;
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700571
572 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700573 spin_lock_irqsave(&host->lock, flags);
574
575 if ((opcode == MMC_SEND_TUNING_BLOCK_HS200) &&
576 (mmc->ios.bus_width == MMC_BUS_WIDTH_8)) {
577 tuning_block_pattern = tuning_block_128;
578 size = sizeof(tuning_block_128);
579 }
580 spin_unlock_irqrestore(&host->lock, flags);
581
582 /* first of all reset the tuning block */
583 rc = msm_init_cm_dll(host);
584 if (rc)
585 goto out;
586
587 data_buf = kmalloc(size, GFP_KERNEL);
588 if (!data_buf) {
589 rc = -ENOMEM;
590 goto out;
591 }
592
593 phase = 0;
594 do {
595 struct mmc_command cmd = {0};
596 struct mmc_data data = {0};
597 struct mmc_request mrq = {
598 .cmd = &cmd,
599 .data = &data
600 };
601 struct scatterlist sg;
602
603 /* set the phase in delay line hw block */
604 rc = msm_config_cm_dll_phase(host, phase);
605 if (rc)
606 goto kfree;
607
608 cmd.opcode = opcode;
609 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
610
611 data.blksz = size;
612 data.blocks = 1;
613 data.flags = MMC_DATA_READ;
614 data.timeout_ns = 1000 * 1000 * 1000; /* 1 sec */
615
616 data.sg = &sg;
617 data.sg_len = 1;
618 sg_init_one(&sg, data_buf, size);
619 memset(data_buf, 0, size);
620 mmc_wait_for_req(mmc, &mrq);
621
622 if (!cmd.error && !data.error &&
623 !memcmp(data_buf, tuning_block_pattern, size)) {
624 /* tuning is successful at this tuning point */
625 tuned_phases[tuned_phase_cnt++] = phase;
626 pr_debug("%s: %s: found good phase = %d\n",
627 mmc_hostname(mmc), __func__, phase);
628 }
629 } while (++phase < 16);
630
631 if (tuned_phase_cnt) {
632 rc = msm_find_most_appropriate_phase(host, tuned_phases,
633 tuned_phase_cnt);
634 if (rc < 0)
635 goto kfree;
636 else
637 phase = (u8)rc;
638
639 /*
640 * Finally set the selected phase in delay
641 * line hw block.
642 */
643 rc = msm_config_cm_dll_phase(host, phase);
644 if (rc)
645 goto kfree;
646 pr_debug("%s: %s: finally setting the tuning phase to %d\n",
647 mmc_hostname(mmc), __func__, phase);
648 } else {
649 /* tuning failed */
650 pr_err("%s: %s: no tuning point found\n",
651 mmc_hostname(mmc), __func__);
652 rc = -EAGAIN;
653 }
654
655kfree:
656 kfree(data_buf);
657out:
658 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
659 return rc;
660}
661
Asutosh Das33a4ff52012-12-18 16:14:02 +0530662static int sdhci_msm_setup_gpio(struct sdhci_msm_pltfm_data *pdata, bool enable)
663{
664 struct sdhci_msm_gpio_data *curr;
665 int i, ret = 0;
666
667 curr = pdata->pin_data->gpio_data;
668 for (i = 0; i < curr->size; i++) {
669 if (!gpio_is_valid(curr->gpio[i].no)) {
670 ret = -EINVAL;
671 pr_err("%s: Invalid gpio = %d\n", __func__,
672 curr->gpio[i].no);
673 goto free_gpios;
674 }
675 if (enable) {
676 ret = gpio_request(curr->gpio[i].no,
677 curr->gpio[i].name);
678 if (ret) {
679 pr_err("%s: gpio_request(%d, %s) failed %d\n",
680 __func__, curr->gpio[i].no,
681 curr->gpio[i].name, ret);
682 goto free_gpios;
683 }
684 curr->gpio[i].is_enabled = true;
685 } else {
686 gpio_free(curr->gpio[i].no);
687 curr->gpio[i].is_enabled = false;
688 }
689 }
690 return ret;
691
692free_gpios:
693 for (i--; i >= 0; i--) {
694 gpio_free(curr->gpio[i].no);
695 curr->gpio[i].is_enabled = false;
696 }
697 return ret;
698}
699
Asutosh Das390519d2012-12-21 12:21:42 +0530700static int sdhci_msm_setup_pad(struct sdhci_msm_pltfm_data *pdata, bool enable)
701{
702 struct sdhci_msm_pad_data *curr;
703 int i;
704
705 curr = pdata->pin_data->pad_data;
706 for (i = 0; i < curr->drv->size; i++) {
707 if (enable)
708 msm_tlmm_set_hdrive(curr->drv->on[i].no,
709 curr->drv->on[i].val);
710 else
711 msm_tlmm_set_hdrive(curr->drv->off[i].no,
712 curr->drv->off[i].val);
713 }
714
715 for (i = 0; i < curr->pull->size; i++) {
716 if (enable)
717 msm_tlmm_set_pull(curr->pull->on[i].no,
718 curr->pull->on[i].val);
719 else
720 msm_tlmm_set_pull(curr->pull->off[i].no,
721 curr->pull->off[i].val);
722 }
723
724 return 0;
725}
726
Asutosh Das33a4ff52012-12-18 16:14:02 +0530727static int sdhci_msm_setup_pins(struct sdhci_msm_pltfm_data *pdata, bool enable)
728{
729 int ret = 0;
730
731 if (!pdata->pin_data || (pdata->pin_data->cfg_sts == enable))
732 return 0;
Asutosh Das390519d2012-12-21 12:21:42 +0530733 if (pdata->pin_data->is_gpio)
734 ret = sdhci_msm_setup_gpio(pdata, enable);
735 else
736 ret = sdhci_msm_setup_pad(pdata, enable);
Asutosh Das33a4ff52012-12-18 16:14:02 +0530737
Asutosh Das33a4ff52012-12-18 16:14:02 +0530738 if (!ret)
739 pdata->pin_data->cfg_sts = enable;
740
741 return ret;
742}
743
Asutosh Das390519d2012-12-21 12:21:42 +0530744static int sdhci_msm_dt_get_array(struct device *dev, const char *prop_name,
745 u32 **out, int *len, u32 size)
746{
747 int ret = 0;
748 struct device_node *np = dev->of_node;
749 size_t sz;
750 u32 *arr = NULL;
751
752 if (!of_get_property(np, prop_name, len)) {
753 ret = -EINVAL;
754 goto out;
755 }
756 sz = *len = *len / sizeof(*arr);
757 if (sz <= 0 || (size > 0 && (sz != size))) {
758 dev_err(dev, "%s invalid size\n", prop_name);
759 ret = -EINVAL;
760 goto out;
761 }
762
763 arr = devm_kzalloc(dev, sz * sizeof(*arr), GFP_KERNEL);
764 if (!arr) {
765 dev_err(dev, "%s failed allocating memory\n", prop_name);
766 ret = -ENOMEM;
767 goto out;
768 }
769
770 ret = of_property_read_u32_array(np, prop_name, arr, sz);
771 if (ret < 0) {
772 dev_err(dev, "%s failed reading array %d\n", prop_name, ret);
773 goto out;
774 }
775 *out = arr;
776out:
777 if (ret)
778 *len = 0;
779 return ret;
780}
781
Asutosh Das33a4ff52012-12-18 16:14:02 +0530782#define MAX_PROP_SIZE 32
783static int sdhci_msm_dt_parse_vreg_info(struct device *dev,
784 struct sdhci_msm_reg_data **vreg_data, const char *vreg_name)
785{
786 int len, ret = 0;
787 const __be32 *prop;
788 char prop_name[MAX_PROP_SIZE];
789 struct sdhci_msm_reg_data *vreg;
790 struct device_node *np = dev->of_node;
791
792 snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", vreg_name);
793 if (!of_parse_phandle(np, prop_name, 0)) {
794 dev_err(dev, "No vreg data found for %s\n", vreg_name);
795 ret = -EINVAL;
796 return ret;
797 }
798
799 vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
800 if (!vreg) {
801 dev_err(dev, "No memory for vreg: %s\n", vreg_name);
802 ret = -ENOMEM;
803 return ret;
804 }
805
806 vreg->name = vreg_name;
807
808 snprintf(prop_name, MAX_PROP_SIZE,
809 "qcom,%s-always-on", vreg_name);
810 if (of_get_property(np, prop_name, NULL))
811 vreg->is_always_on = true;
812
813 snprintf(prop_name, MAX_PROP_SIZE,
814 "qcom,%s-lpm-sup", vreg_name);
815 if (of_get_property(np, prop_name, NULL))
816 vreg->lpm_sup = true;
817
818 snprintf(prop_name, MAX_PROP_SIZE,
819 "qcom,%s-voltage-level", vreg_name);
820 prop = of_get_property(np, prop_name, &len);
821 if (!prop || (len != (2 * sizeof(__be32)))) {
822 dev_warn(dev, "%s %s property\n",
823 prop ? "invalid format" : "no", prop_name);
824 } else {
825 vreg->low_vol_level = be32_to_cpup(&prop[0]);
826 vreg->high_vol_level = be32_to_cpup(&prop[1]);
827 }
828
829 snprintf(prop_name, MAX_PROP_SIZE,
830 "qcom,%s-current-level", vreg_name);
831 prop = of_get_property(np, prop_name, &len);
832 if (!prop || (len != (2 * sizeof(__be32)))) {
833 dev_warn(dev, "%s %s property\n",
834 prop ? "invalid format" : "no", prop_name);
835 } else {
836 vreg->lpm_uA = be32_to_cpup(&prop[0]);
837 vreg->hpm_uA = be32_to_cpup(&prop[1]);
838 }
839
840 *vreg_data = vreg;
841 dev_dbg(dev, "%s: %s %s vol=[%d %d]uV, curr=[%d %d]uA\n",
842 vreg->name, vreg->is_always_on ? "always_on," : "",
843 vreg->lpm_sup ? "lpm_sup," : "", vreg->low_vol_level,
844 vreg->high_vol_level, vreg->lpm_uA, vreg->hpm_uA);
845
846 return ret;
847}
848
Asutosh Das390519d2012-12-21 12:21:42 +0530849/* GPIO/Pad data extraction */
850static int sdhci_msm_dt_get_pad_pull_info(struct device *dev, int id,
851 struct sdhci_msm_pad_pull_data **pad_pull_data)
852{
853 int ret = 0, base = 0, len, i;
854 u32 *tmp;
855 struct sdhci_msm_pad_pull_data *pull_data;
856 struct sdhci_msm_pad_pull *pull;
857
858 switch (id) {
859 case 1:
860 base = TLMM_PULL_SDC1_CLK;
861 break;
862 case 2:
863 base = TLMM_PULL_SDC2_CLK;
864 break;
865 case 3:
866 base = TLMM_PULL_SDC3_CLK;
867 break;
868 case 4:
869 base = TLMM_PULL_SDC4_CLK;
870 break;
871 default:
872 dev_err(dev, "%s: Invalid slot id\n", __func__);
873 ret = -EINVAL;
874 goto out;
875 }
876
877 pull_data = devm_kzalloc(dev, sizeof(struct sdhci_msm_pad_pull_data),
878 GFP_KERNEL);
879 if (!pull_data) {
880 dev_err(dev, "No memory for msm_mmc_pad_pull_data\n");
881 ret = -ENOMEM;
882 goto out;
883 }
884 pull_data->size = 3; /* array size for clk, cmd, data */
885
886 /* Allocate on, off configs for clk, cmd, data */
887 pull = devm_kzalloc(dev, 2 * pull_data->size *\
888 sizeof(struct sdhci_msm_pad_pull), GFP_KERNEL);
889 if (!pull) {
890 dev_err(dev, "No memory for msm_mmc_pad_pull\n");
891 ret = -ENOMEM;
892 goto out;
893 }
894 pull_data->on = pull;
895 pull_data->off = pull + pull_data->size;
896
897 ret = sdhci_msm_dt_get_array(dev, "qcom,pad-pull-on",
898 &tmp, &len, pull_data->size);
899 if (ret)
900 goto out;
901
902 for (i = 0; i < len; i++) {
903 pull_data->on[i].no = base + i;
904 pull_data->on[i].val = tmp[i];
905 dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
906 i, pull_data->on[i].val);
907 }
908
909 ret = sdhci_msm_dt_get_array(dev, "qcom,pad-pull-off",
910 &tmp, &len, pull_data->size);
911 if (ret)
912 goto out;
913
914 for (i = 0; i < len; i++) {
915 pull_data->off[i].no = base + i;
916 pull_data->off[i].val = tmp[i];
917 dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
918 i, pull_data->off[i].val);
919 }
920
921 *pad_pull_data = pull_data;
922out:
923 return ret;
924}
925
926static int sdhci_msm_dt_get_pad_drv_info(struct device *dev, int id,
927 struct sdhci_msm_pad_drv_data **pad_drv_data)
928{
929 int ret = 0, base = 0, len, i;
930 u32 *tmp;
931 struct sdhci_msm_pad_drv_data *drv_data;
932 struct sdhci_msm_pad_drv *drv;
933
934 switch (id) {
935 case 1:
936 base = TLMM_HDRV_SDC1_CLK;
937 break;
938 case 2:
939 base = TLMM_HDRV_SDC2_CLK;
940 break;
941 case 3:
942 base = TLMM_HDRV_SDC3_CLK;
943 break;
944 case 4:
945 base = TLMM_HDRV_SDC4_CLK;
946 break;
947 default:
948 dev_err(dev, "%s: Invalid slot id\n", __func__);
949 ret = -EINVAL;
950 goto out;
951 }
952
953 drv_data = devm_kzalloc(dev, sizeof(struct sdhci_msm_pad_drv_data),
954 GFP_KERNEL);
955 if (!drv_data) {
956 dev_err(dev, "No memory for msm_mmc_pad_drv_data\n");
957 ret = -ENOMEM;
958 goto out;
959 }
960 drv_data->size = 3; /* array size for clk, cmd, data */
961
962 /* Allocate on, off configs for clk, cmd, data */
963 drv = devm_kzalloc(dev, 2 * drv_data->size *\
964 sizeof(struct sdhci_msm_pad_drv), GFP_KERNEL);
965 if (!drv) {
966 dev_err(dev, "No memory msm_mmc_pad_drv\n");
967 ret = -ENOMEM;
968 goto out;
969 }
970 drv_data->on = drv;
971 drv_data->off = drv + drv_data->size;
972
973 ret = sdhci_msm_dt_get_array(dev, "qcom,pad-drv-on",
974 &tmp, &len, drv_data->size);
975 if (ret)
976 goto out;
977
978 for (i = 0; i < len; i++) {
979 drv_data->on[i].no = base + i;
980 drv_data->on[i].val = tmp[i];
981 dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
982 i, drv_data->on[i].val);
983 }
984
985 ret = sdhci_msm_dt_get_array(dev, "qcom,pad-drv-off",
986 &tmp, &len, drv_data->size);
987 if (ret)
988 goto out;
989
990 for (i = 0; i < len; i++) {
991 drv_data->off[i].no = base + i;
992 drv_data->off[i].val = tmp[i];
993 dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
994 i, drv_data->off[i].val);
995 }
996
997 *pad_drv_data = drv_data;
998out:
999 return ret;
1000}
1001
Asutosh Das33a4ff52012-12-18 16:14:02 +05301002#define GPIO_NAME_MAX_LEN 32
1003static int sdhci_msm_dt_parse_gpio_info(struct device *dev,
1004 struct sdhci_msm_pltfm_data *pdata)
1005{
Asutosh Das390519d2012-12-21 12:21:42 +05301006 int ret = 0, id = 0, cnt, i;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301007 struct sdhci_msm_pin_data *pin_data;
1008 struct device_node *np = dev->of_node;
1009
1010 pin_data = devm_kzalloc(dev, sizeof(*pin_data), GFP_KERNEL);
1011 if (!pin_data) {
1012 dev_err(dev, "No memory for pin_data\n");
1013 ret = -ENOMEM;
1014 goto out;
1015 }
1016
1017 cnt = of_gpio_count(np);
1018 if (cnt > 0) {
Asutosh Das390519d2012-12-21 12:21:42 +05301019 pin_data->is_gpio = true;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301020 pin_data->gpio_data = devm_kzalloc(dev,
1021 sizeof(struct sdhci_msm_gpio_data), GFP_KERNEL);
1022 if (!pin_data->gpio_data) {
1023 dev_err(dev, "No memory for gpio_data\n");
1024 ret = -ENOMEM;
1025 goto out;
1026 }
1027 pin_data->gpio_data->size = cnt;
1028 pin_data->gpio_data->gpio = devm_kzalloc(dev, cnt *
1029 sizeof(struct sdhci_msm_gpio), GFP_KERNEL);
1030
1031 if (!pin_data->gpio_data->gpio) {
1032 dev_err(dev, "No memory for gpio\n");
1033 ret = -ENOMEM;
1034 goto out;
1035 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301036 for (i = 0; i < cnt; i++) {
1037 const char *name = NULL;
1038 char result[GPIO_NAME_MAX_LEN];
1039 pin_data->gpio_data->gpio[i].no = of_get_gpio(np, i);
1040 of_property_read_string_index(np,
1041 "qcom,gpio-names", i, &name);
1042
1043 snprintf(result, GPIO_NAME_MAX_LEN, "%s-%s",
1044 dev_name(dev), name ? name : "?");
1045 pin_data->gpio_data->gpio[i].name = result;
1046 dev_dbg(dev, "%s: gpio[%s] = %d\n", __func__,
Asutosh Das390519d2012-12-21 12:21:42 +05301047 pin_data->gpio_data->gpio[i].name,
1048 pin_data->gpio_data->gpio[i].no);
Asutosh Das33a4ff52012-12-18 16:14:02 +05301049 }
Asutosh Das390519d2012-12-21 12:21:42 +05301050 } else {
1051 pin_data->pad_data =
1052 devm_kzalloc(dev,
1053 sizeof(struct sdhci_msm_pad_data),
1054 GFP_KERNEL);
1055 if (!pin_data->pad_data) {
1056 dev_err(dev,
1057 "No memory for pin_data->pad_data\n");
1058 ret = -ENOMEM;
1059 goto out;
1060 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301061
Asutosh Das390519d2012-12-21 12:21:42 +05301062 ret = of_alias_get_id(np, "sdhc");
1063 if (ret < 0) {
1064 dev_err(dev, "Failed to get slot index %d\n", ret);
1065 goto out;
1066 }
1067 id = ret;
1068
1069 ret = sdhci_msm_dt_get_pad_pull_info(
1070 dev, id, &pin_data->pad_data->pull);
1071 if (ret)
1072 goto out;
1073 ret = sdhci_msm_dt_get_pad_drv_info(
1074 dev, id, &pin_data->pad_data->drv);
1075 if (ret)
1076 goto out;
1077
1078 }
1079 pdata->pin_data = pin_data;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301080out:
1081 if (ret)
1082 dev_err(dev, "%s failed with err %d\n", __func__, ret);
1083 return ret;
1084}
1085
1086/* Parse platform data */
1087static struct sdhci_msm_pltfm_data *sdhci_msm_populate_pdata(struct device *dev)
1088{
1089 struct sdhci_msm_pltfm_data *pdata = NULL;
1090 struct device_node *np = dev->of_node;
1091 u32 bus_width = 0;
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301092 u32 cpu_dma_latency;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301093 int len, i;
Sahitya Tummala00240122013-02-28 19:50:51 +05301094 int clk_table_len;
1095 u32 *clk_table = NULL;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301096
1097 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1098 if (!pdata) {
1099 dev_err(dev, "failed to allocate memory for platform data\n");
1100 goto out;
1101 }
1102
Sahitya Tummala62448d92013-03-12 14:57:46 +05301103 pdata->status_gpio = of_get_named_gpio_flags(np, "cd-gpios", 0, 0);
1104
Asutosh Das33a4ff52012-12-18 16:14:02 +05301105 of_property_read_u32(np, "qcom,bus-width", &bus_width);
1106 if (bus_width == 8)
1107 pdata->mmc_bus_width = MMC_CAP_8_BIT_DATA;
1108 else if (bus_width == 4)
1109 pdata->mmc_bus_width = MMC_CAP_4_BIT_DATA;
1110 else {
1111 dev_notice(dev, "invalid bus-width, default to 1-bit mode\n");
1112 pdata->mmc_bus_width = 0;
1113 }
1114
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301115 if (!of_property_read_u32(np, "qcom,cpu-dma-latency-us",
1116 &cpu_dma_latency))
1117 pdata->cpu_dma_latency_us = cpu_dma_latency;
1118
Sahitya Tummala00240122013-02-28 19:50:51 +05301119 if (sdhci_msm_dt_get_array(dev, "qcom,clk-rates",
1120 &clk_table, &clk_table_len, 0)) {
1121 dev_err(dev, "failed parsing supported clock rates\n");
1122 goto out;
1123 }
1124 if (!clk_table || !clk_table_len) {
1125 dev_err(dev, "Invalid clock table\n");
1126 goto out;
1127 }
1128 pdata->sup_clk_table = clk_table;
1129 pdata->sup_clk_cnt = clk_table_len;
1130
Asutosh Das33a4ff52012-12-18 16:14:02 +05301131 pdata->vreg_data = devm_kzalloc(dev, sizeof(struct
1132 sdhci_msm_slot_reg_data),
1133 GFP_KERNEL);
1134 if (!pdata->vreg_data) {
1135 dev_err(dev, "failed to allocate memory for vreg data\n");
1136 goto out;
1137 }
1138
1139 if (sdhci_msm_dt_parse_vreg_info(dev, &pdata->vreg_data->vdd_data,
1140 "vdd")) {
1141 dev_err(dev, "failed parsing vdd data\n");
1142 goto out;
1143 }
1144 if (sdhci_msm_dt_parse_vreg_info(dev,
1145 &pdata->vreg_data->vdd_io_data,
1146 "vdd-io")) {
1147 dev_err(dev, "failed parsing vdd-io data\n");
1148 goto out;
1149 }
1150
1151 if (sdhci_msm_dt_parse_gpio_info(dev, pdata)) {
1152 dev_err(dev, "failed parsing gpio data\n");
1153 goto out;
1154 }
1155
Asutosh Das33a4ff52012-12-18 16:14:02 +05301156 len = of_property_count_strings(np, "qcom,bus-speed-mode");
1157
1158 for (i = 0; i < len; i++) {
1159 const char *name = NULL;
1160
1161 of_property_read_string_index(np,
1162 "qcom,bus-speed-mode", i, &name);
1163 if (!name)
1164 continue;
1165
1166 if (!strncmp(name, "HS200_1p8v", sizeof("HS200_1p8v")))
1167 pdata->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
1168 else if (!strncmp(name, "HS200_1p2v", sizeof("HS200_1p2v")))
1169 pdata->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
1170 else if (!strncmp(name, "DDR_1p8v", sizeof("DDR_1p8v")))
1171 pdata->caps |= MMC_CAP_1_8V_DDR
1172 | MMC_CAP_UHS_DDR50;
1173 else if (!strncmp(name, "DDR_1p2v", sizeof("DDR_1p2v")))
1174 pdata->caps |= MMC_CAP_1_2V_DDR
1175 | MMC_CAP_UHS_DDR50;
1176 }
1177
1178 if (of_get_property(np, "qcom,nonremovable", NULL))
1179 pdata->nonremovable = true;
1180
1181 return pdata;
1182out:
1183 return NULL;
1184}
1185
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301186/* Returns required bandwidth in Bytes per Sec */
1187static unsigned int sdhci_get_bw_required(struct sdhci_host *host,
1188 struct mmc_ios *ios)
1189{
1190 unsigned int bw;
1191
1192 bw = host->clock;
1193 /*
1194 * For DDR mode, SDCC controller clock will be at
1195 * the double rate than the actual clock that goes to card.
1196 */
1197 if (ios->bus_width == MMC_BUS_WIDTH_4)
1198 bw /= 2;
1199 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1200 bw /= 8;
1201
1202 return bw;
1203}
1204
1205static int sdhci_msm_bus_get_vote_for_bw(struct sdhci_msm_host *host,
1206 unsigned int bw)
1207{
1208 unsigned int *table = host->pdata->voting_data->bw_vecs;
1209 unsigned int size = host->pdata->voting_data->bw_vecs_size;
1210 int i;
1211
1212 if (host->msm_bus_vote.is_max_bw_needed && bw)
1213 return host->msm_bus_vote.max_bw_vote;
1214
1215 for (i = 0; i < size; i++) {
1216 if (bw <= table[i])
1217 break;
1218 }
1219
1220 if (i && (i == size))
1221 i--;
1222
1223 return i;
1224}
1225
1226/*
1227 * This function must be called with host lock acquired.
1228 * Caller of this function should also ensure that msm bus client
1229 * handle is not null.
1230 */
1231static inline int sdhci_msm_bus_set_vote(struct sdhci_msm_host *msm_host,
1232 int vote,
1233 unsigned long flags)
1234{
1235 struct sdhci_host *host = platform_get_drvdata(msm_host->pdev);
1236 int rc = 0;
1237
1238 if (vote != msm_host->msm_bus_vote.curr_vote) {
1239 spin_unlock_irqrestore(&host->lock, flags);
1240 rc = msm_bus_scale_client_update_request(
1241 msm_host->msm_bus_vote.client_handle, vote);
1242 spin_lock_irqsave(&host->lock, flags);
1243 if (rc) {
1244 pr_err("%s: msm_bus_scale_client_update_request() failed: bus_client_handle=0x%x, vote=%d, err=%d\n",
1245 mmc_hostname(host->mmc),
1246 msm_host->msm_bus_vote.client_handle, vote, rc);
1247 goto out;
1248 }
1249 msm_host->msm_bus_vote.curr_vote = vote;
1250 }
1251out:
1252 return rc;
1253}
1254
1255/*
1256 * Internal work. Work to set 0 bandwidth for msm bus.
1257 */
1258static void sdhci_msm_bus_work(struct work_struct *work)
1259{
1260 struct sdhci_msm_host *msm_host;
1261 struct sdhci_host *host;
1262 unsigned long flags;
1263
1264 msm_host = container_of(work, struct sdhci_msm_host,
1265 msm_bus_vote.vote_work.work);
1266 host = platform_get_drvdata(msm_host->pdev);
1267
1268 if (!msm_host->msm_bus_vote.client_handle)
1269 return;
1270
1271 spin_lock_irqsave(&host->lock, flags);
1272 /* don't vote for 0 bandwidth if any request is in progress */
1273 if (!host->mrq) {
1274 sdhci_msm_bus_set_vote(msm_host,
1275 msm_host->msm_bus_vote.min_bw_vote, flags);
1276 } else
1277 pr_warning("%s: %s: Transfer in progress. skipping bus voting to 0 bandwidth\n",
1278 mmc_hostname(host->mmc), __func__);
1279 spin_unlock_irqrestore(&host->lock, flags);
1280}
1281
1282/*
1283 * This function cancels any scheduled delayed work and sets the bus
1284 * vote based on bw (bandwidth) argument.
1285 */
1286static void sdhci_msm_bus_cancel_work_and_set_vote(struct sdhci_host *host,
1287 unsigned int bw)
1288{
1289 int vote;
1290 unsigned long flags;
1291 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1292 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1293
1294 cancel_delayed_work_sync(&msm_host->msm_bus_vote.vote_work);
1295 spin_lock_irqsave(&host->lock, flags);
1296 vote = sdhci_msm_bus_get_vote_for_bw(msm_host, bw);
1297 sdhci_msm_bus_set_vote(msm_host, vote, flags);
1298 spin_unlock_irqrestore(&host->lock, flags);
1299}
1300
1301#define MSM_MMC_BUS_VOTING_DELAY 200 /* msecs */
1302
1303/* This function queues a work which will set the bandwidth requiement to 0 */
1304static void sdhci_msm_bus_queue_work(struct sdhci_host *host)
1305{
1306 unsigned long flags;
1307 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1308 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1309
1310 spin_lock_irqsave(&host->lock, flags);
1311 if (msm_host->msm_bus_vote.min_bw_vote !=
1312 msm_host->msm_bus_vote.curr_vote)
1313 queue_delayed_work(system_nrt_wq,
1314 &msm_host->msm_bus_vote.vote_work,
1315 msecs_to_jiffies(MSM_MMC_BUS_VOTING_DELAY));
1316 spin_unlock_irqrestore(&host->lock, flags);
1317}
1318
1319static int sdhci_msm_bus_register(struct sdhci_msm_host *host,
1320 struct platform_device *pdev)
1321{
1322 int rc = 0;
1323 struct msm_bus_scale_pdata *bus_pdata;
1324
1325 struct sdhci_msm_bus_voting_data *data;
1326 struct device *dev = &pdev->dev;
1327
1328 data = devm_kzalloc(dev,
1329 sizeof(struct sdhci_msm_bus_voting_data), GFP_KERNEL);
1330 if (!data) {
1331 dev_err(&pdev->dev,
1332 "%s: failed to allocate memory\n", __func__);
1333 rc = -ENOMEM;
1334 goto out;
1335 }
1336 data->bus_pdata = msm_bus_cl_get_pdata(pdev);
1337 if (data->bus_pdata) {
1338 rc = sdhci_msm_dt_get_array(dev, "qcom,bus-bw-vectors-bps",
1339 &data->bw_vecs, &data->bw_vecs_size, 0);
1340 if (rc) {
1341 dev_err(&pdev->dev,
1342 "%s: Failed to get bus-bw-vectors-bps\n",
1343 __func__);
1344 goto out;
1345 }
1346 host->pdata->voting_data = data;
1347 }
1348 if (host->pdata->voting_data &&
1349 host->pdata->voting_data->bus_pdata &&
1350 host->pdata->voting_data->bw_vecs &&
1351 host->pdata->voting_data->bw_vecs_size) {
1352
1353 bus_pdata = host->pdata->voting_data->bus_pdata;
1354 host->msm_bus_vote.client_handle =
1355 msm_bus_scale_register_client(bus_pdata);
1356 if (!host->msm_bus_vote.client_handle) {
1357 dev_err(&pdev->dev, "msm_bus_scale_register_client()\n");
1358 rc = -EFAULT;
1359 goto out;
1360 }
1361 /* cache the vote index for minimum and maximum bandwidth */
1362 host->msm_bus_vote.min_bw_vote =
1363 sdhci_msm_bus_get_vote_for_bw(host, 0);
1364 host->msm_bus_vote.max_bw_vote =
1365 sdhci_msm_bus_get_vote_for_bw(host, UINT_MAX);
1366 } else {
1367 devm_kfree(dev, data);
1368 }
1369
1370out:
1371 return rc;
1372}
1373
1374static void sdhci_msm_bus_unregister(struct sdhci_msm_host *host)
1375{
1376 if (host->msm_bus_vote.client_handle)
1377 msm_bus_scale_unregister_client(
1378 host->msm_bus_vote.client_handle);
1379}
1380
1381static void sdhci_msm_bus_voting(struct sdhci_host *host, u32 enable)
1382{
1383 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1384 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1385 struct mmc_ios *ios = &host->mmc->ios;
1386 unsigned int bw;
1387
1388 if (!msm_host->msm_bus_vote.client_handle)
1389 return;
1390
1391 bw = sdhci_get_bw_required(host, ios);
1392 if (enable)
1393 sdhci_msm_bus_cancel_work_and_set_vote(host, bw);
1394 else
1395 sdhci_msm_bus_queue_work(host);
1396}
1397
Asutosh Das33a4ff52012-12-18 16:14:02 +05301398/* Regulator utility functions */
1399static int sdhci_msm_vreg_init_reg(struct device *dev,
1400 struct sdhci_msm_reg_data *vreg)
1401{
1402 int ret = 0;
1403
1404 /* check if regulator is already initialized? */
1405 if (vreg->reg)
1406 goto out;
1407
1408 /* Get the regulator handle */
1409 vreg->reg = devm_regulator_get(dev, vreg->name);
1410 if (IS_ERR(vreg->reg)) {
1411 ret = PTR_ERR(vreg->reg);
1412 pr_err("%s: devm_regulator_get(%s) failed. ret=%d\n",
1413 __func__, vreg->name, ret);
1414 goto out;
1415 }
1416
1417 /* sanity check */
1418 if (!vreg->high_vol_level || !vreg->hpm_uA) {
1419 pr_err("%s: %s invalid constraints specified\n",
1420 __func__, vreg->name);
1421 ret = -EINVAL;
1422 }
1423
1424out:
1425 return ret;
1426}
1427
1428static void sdhci_msm_vreg_deinit_reg(struct sdhci_msm_reg_data *vreg)
1429{
1430 if (vreg->reg)
1431 devm_regulator_put(vreg->reg);
1432}
1433
1434static int sdhci_msm_vreg_set_optimum_mode(struct sdhci_msm_reg_data
1435 *vreg, int uA_load)
1436{
1437 int ret = 0;
1438
1439 /*
1440 * regulators that do not support regulator_set_voltage also
1441 * do not support regulator_set_optimum_mode
1442 */
1443 ret = regulator_set_optimum_mode(vreg->reg, uA_load);
1444 if (ret < 0)
1445 pr_err("%s: regulator_set_optimum_mode(reg=%s,uA_load=%d) failed. ret=%d\n",
1446 __func__, vreg->name, uA_load, ret);
1447 else
1448 /*
1449 * regulator_set_optimum_mode() can return non zero
1450 * value even for success case.
1451 */
1452 ret = 0;
1453 return ret;
1454}
1455
1456static int sdhci_msm_vreg_set_voltage(struct sdhci_msm_reg_data *vreg,
1457 int min_uV, int max_uV)
1458{
1459 int ret = 0;
1460
1461 ret = regulator_set_voltage(vreg->reg, min_uV, max_uV);
1462 if (ret) {
1463 pr_err("%s: regulator_set_voltage(%s)failed. min_uV=%d,max_uV=%d,ret=%d\n",
1464 __func__, vreg->name, min_uV, max_uV, ret);
1465 }
1466
1467 return ret;
1468}
1469
1470static int sdhci_msm_vreg_enable(struct sdhci_msm_reg_data *vreg)
1471{
1472 int ret = 0;
1473
1474 /* Put regulator in HPM (high power mode) */
1475 ret = sdhci_msm_vreg_set_optimum_mode(vreg, vreg->hpm_uA);
1476 if (ret < 0)
1477 return ret;
1478
1479 if (!vreg->is_enabled) {
1480 /* Set voltage level */
1481 ret = sdhci_msm_vreg_set_voltage(vreg, vreg->high_vol_level,
1482 vreg->high_vol_level);
1483 if (ret)
1484 return ret;
1485 }
1486 ret = regulator_enable(vreg->reg);
1487 if (ret) {
1488 pr_err("%s: regulator_enable(%s) failed. ret=%d\n",
1489 __func__, vreg->name, ret);
1490 return ret;
1491 }
1492 vreg->is_enabled = true;
1493 return ret;
1494}
1495
1496static int sdhci_msm_vreg_disable(struct sdhci_msm_reg_data *vreg)
1497{
1498 int ret = 0;
1499
1500 /* Never disable regulator marked as always_on */
1501 if (vreg->is_enabled && !vreg->is_always_on) {
1502 ret = regulator_disable(vreg->reg);
1503 if (ret) {
1504 pr_err("%s: regulator_disable(%s) failed. ret=%d\n",
1505 __func__, vreg->name, ret);
1506 goto out;
1507 }
1508 vreg->is_enabled = false;
1509
1510 ret = sdhci_msm_vreg_set_optimum_mode(vreg, 0);
1511 if (ret < 0)
1512 goto out;
1513
1514 /* Set min. voltage level to 0 */
1515 ret = sdhci_msm_vreg_set_voltage(vreg, 0, vreg->high_vol_level);
1516 if (ret)
1517 goto out;
1518 } else if (vreg->is_enabled && vreg->is_always_on) {
1519 if (vreg->lpm_sup) {
1520 /* Put always_on regulator in LPM (low power mode) */
1521 ret = sdhci_msm_vreg_set_optimum_mode(vreg,
1522 vreg->lpm_uA);
1523 if (ret < 0)
1524 goto out;
1525 }
1526 }
1527out:
1528 return ret;
1529}
1530
1531static int sdhci_msm_setup_vreg(struct sdhci_msm_pltfm_data *pdata,
1532 bool enable, bool is_init)
1533{
1534 int ret = 0, i;
1535 struct sdhci_msm_slot_reg_data *curr_slot;
1536 struct sdhci_msm_reg_data *vreg_table[2];
1537
1538 curr_slot = pdata->vreg_data;
1539 if (!curr_slot) {
1540 pr_debug("%s: vreg info unavailable,assuming the slot is powered by always on domain\n",
1541 __func__);
1542 goto out;
1543 }
1544
1545 vreg_table[0] = curr_slot->vdd_data;
1546 vreg_table[1] = curr_slot->vdd_io_data;
1547
1548 for (i = 0; i < ARRAY_SIZE(vreg_table); i++) {
1549 if (vreg_table[i]) {
1550 if (enable)
1551 ret = sdhci_msm_vreg_enable(vreg_table[i]);
1552 else
1553 ret = sdhci_msm_vreg_disable(vreg_table[i]);
1554 if (ret)
1555 goto out;
1556 }
1557 }
1558out:
1559 return ret;
1560}
1561
1562/*
1563 * Reset vreg by ensuring it is off during probe. A call
1564 * to enable vreg is needed to balance disable vreg
1565 */
1566static int sdhci_msm_vreg_reset(struct sdhci_msm_pltfm_data *pdata)
1567{
1568 int ret;
1569
1570 ret = sdhci_msm_setup_vreg(pdata, 1, true);
1571 if (ret)
1572 return ret;
1573 ret = sdhci_msm_setup_vreg(pdata, 0, true);
1574 return ret;
1575}
1576
1577/* This init function should be called only once for each SDHC slot */
1578static int sdhci_msm_vreg_init(struct device *dev,
1579 struct sdhci_msm_pltfm_data *pdata,
1580 bool is_init)
1581{
1582 int ret = 0;
1583 struct sdhci_msm_slot_reg_data *curr_slot;
1584 struct sdhci_msm_reg_data *curr_vdd_reg, *curr_vdd_io_reg;
1585
1586 curr_slot = pdata->vreg_data;
1587 if (!curr_slot)
1588 goto out;
1589
1590 curr_vdd_reg = curr_slot->vdd_data;
1591 curr_vdd_io_reg = curr_slot->vdd_io_data;
1592
1593 if (!is_init)
1594 /* Deregister all regulators from regulator framework */
1595 goto vdd_io_reg_deinit;
1596
1597 /*
1598 * Get the regulator handle from voltage regulator framework
1599 * and then try to set the voltage level for the regulator
1600 */
1601 if (curr_vdd_reg) {
1602 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_reg);
1603 if (ret)
1604 goto out;
1605 }
1606 if (curr_vdd_io_reg) {
1607 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_io_reg);
1608 if (ret)
1609 goto vdd_reg_deinit;
1610 }
1611 ret = sdhci_msm_vreg_reset(pdata);
1612 if (ret)
1613 dev_err(dev, "vreg reset failed (%d)\n", ret);
1614 goto out;
1615
1616vdd_io_reg_deinit:
1617 if (curr_vdd_io_reg)
1618 sdhci_msm_vreg_deinit_reg(curr_vdd_io_reg);
1619vdd_reg_deinit:
1620 if (curr_vdd_reg)
1621 sdhci_msm_vreg_deinit_reg(curr_vdd_reg);
1622out:
1623 return ret;
1624}
1625
1626
1627static int sdhci_msm_set_vdd_io_vol(struct sdhci_msm_pltfm_data *pdata,
1628 enum vdd_io_level level,
1629 unsigned int voltage_level)
1630{
1631 int ret = 0;
1632 int set_level;
1633 struct sdhci_msm_reg_data *vdd_io_reg;
1634
1635 if (!pdata->vreg_data)
1636 return ret;
1637
1638 vdd_io_reg = pdata->vreg_data->vdd_io_data;
1639 if (vdd_io_reg && vdd_io_reg->is_enabled) {
1640 switch (level) {
1641 case VDD_IO_LOW:
1642 set_level = vdd_io_reg->low_vol_level;
1643 break;
1644 case VDD_IO_HIGH:
1645 set_level = vdd_io_reg->high_vol_level;
1646 break;
1647 case VDD_IO_SET_LEVEL:
1648 set_level = voltage_level;
1649 break;
1650 default:
1651 pr_err("%s: invalid argument level = %d",
1652 __func__, level);
1653 ret = -EINVAL;
1654 return ret;
1655 }
1656 ret = sdhci_msm_vreg_set_voltage(vdd_io_reg, set_level,
1657 set_level);
1658 }
1659 return ret;
1660}
1661
1662static irqreturn_t sdhci_msm_pwr_irq(int irq, void *data)
1663{
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07001664 struct sdhci_host *host = (struct sdhci_host *)data;
1665 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1666 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301667 u8 irq_status = 0;
1668 u8 irq_ack = 0;
1669 int ret = 0;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301670 int pwr_state = 0, io_level = 0;
1671 unsigned long flags;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301672
1673 irq_status = readb_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
1674 pr_debug("%s: Received IRQ(%d), status=0x%x\n",
1675 mmc_hostname(msm_host->mmc), irq, irq_status);
1676
1677 /* Clear the interrupt */
1678 writeb_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
1679 /*
1680 * SDHC has core_mem and hc_mem device memory and these memory
1681 * addresses do not fall within 1KB region. Hence, any update to
1682 * core_mem address space would require an mb() to ensure this gets
1683 * completed before its next update to registers within hc_mem.
1684 */
1685 mb();
1686
1687 /* Handle BUS ON/OFF*/
1688 if (irq_status & CORE_PWRCTL_BUS_ON) {
1689 ret = sdhci_msm_setup_vreg(msm_host->pdata, true, false);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301690 if (!ret) {
Asutosh Das33a4ff52012-12-18 16:14:02 +05301691 ret = sdhci_msm_setup_pins(msm_host->pdata, true);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301692 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
1693 VDD_IO_HIGH, 0);
1694 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301695 if (ret)
1696 irq_ack |= CORE_PWRCTL_BUS_FAIL;
1697 else
1698 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301699
1700 pwr_state = REQ_BUS_ON;
1701 io_level = REQ_IO_HIGH;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301702 }
1703 if (irq_status & CORE_PWRCTL_BUS_OFF) {
1704 ret = sdhci_msm_setup_vreg(msm_host->pdata, false, false);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301705 if (!ret) {
Asutosh Das33a4ff52012-12-18 16:14:02 +05301706 ret = sdhci_msm_setup_pins(msm_host->pdata, false);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301707 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
1708 VDD_IO_LOW, 0);
1709 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301710 if (ret)
1711 irq_ack |= CORE_PWRCTL_BUS_FAIL;
1712 else
1713 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301714
1715 pwr_state = REQ_BUS_OFF;
1716 io_level = REQ_IO_LOW;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301717 }
1718 /* Handle IO LOW/HIGH */
1719 if (irq_status & CORE_PWRCTL_IO_LOW) {
1720 /* Switch voltage Low */
1721 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_LOW, 0);
1722 if (ret)
1723 irq_ack |= CORE_PWRCTL_IO_FAIL;
1724 else
1725 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301726
1727 io_level = REQ_IO_LOW;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301728 }
1729 if (irq_status & CORE_PWRCTL_IO_HIGH) {
1730 /* Switch voltage High */
1731 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_HIGH, 0);
1732 if (ret)
1733 irq_ack |= CORE_PWRCTL_IO_FAIL;
1734 else
1735 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301736
1737 io_level = REQ_IO_HIGH;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301738 }
1739
1740 /* ACK status to the core */
1741 writeb_relaxed(irq_ack, (msm_host->core_mem + CORE_PWRCTL_CTL));
1742 /*
1743 * SDHC has core_mem and hc_mem device memory and these memory
1744 * addresses do not fall within 1KB region. Hence, any update to
1745 * core_mem address space would require an mb() to ensure this gets
1746 * completed before its next update to registers within hc_mem.
1747 */
1748 mb();
1749
Sahitya Tummala179e7382013-03-20 19:24:01 +05301750 if (io_level & REQ_IO_HIGH)
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);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301754 else if (io_level & REQ_IO_LOW)
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07001755 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
1756 CORE_IO_PAD_PWR_SWITCH),
1757 host->ioaddr + CORE_VENDOR_SPEC);
1758 mb();
1759
Asutosh Das33a4ff52012-12-18 16:14:02 +05301760 pr_debug("%s: Handled IRQ(%d), ret=%d, ack=0x%x\n",
1761 mmc_hostname(msm_host->mmc), irq, ret, irq_ack);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301762 spin_lock_irqsave(&host->lock, flags);
1763 if (pwr_state)
1764 msm_host->curr_pwr_state = pwr_state;
1765 if (io_level)
1766 msm_host->curr_io_level = io_level;
1767 complete(&msm_host->pwr_irq_completion);
1768 spin_unlock_irqrestore(&host->lock, flags);
1769
Asutosh Das33a4ff52012-12-18 16:14:02 +05301770 return IRQ_HANDLED;
1771}
1772
1773/* This function returns the max. current supported by VDD rail in mA */
1774static unsigned int sdhci_msm_get_vreg_vdd_max_current(struct sdhci_msm_host
1775 *host)
1776{
1777 struct sdhci_msm_slot_reg_data *curr_slot = host->pdata->vreg_data;
1778 if (!curr_slot)
1779 return 0;
1780 if (curr_slot->vdd_data)
1781 return curr_slot->vdd_data->hpm_uA / 1000;
1782 else
1783 return 0;
1784}
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301785static ssize_t
1786show_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
1787 char *buf)
1788{
1789 struct sdhci_host *host = dev_get_drvdata(dev);
1790 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1791 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1792
1793 return snprintf(buf, PAGE_SIZE, "%u\n",
1794 msm_host->msm_bus_vote.is_max_bw_needed);
1795}
1796
1797static ssize_t
1798store_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
1799 const char *buf, size_t count)
1800{
1801 struct sdhci_host *host = dev_get_drvdata(dev);
1802 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1803 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1804 uint32_t value;
1805 unsigned long flags;
1806
1807 if (!kstrtou32(buf, 0, &value)) {
1808 spin_lock_irqsave(&host->lock, flags);
1809 msm_host->msm_bus_vote.is_max_bw_needed = !!value;
1810 spin_unlock_irqrestore(&host->lock, flags);
1811 }
1812 return count;
1813}
Asutosh Das33a4ff52012-12-18 16:14:02 +05301814
Sahitya Tummala179e7382013-03-20 19:24:01 +05301815static void sdhci_msm_check_power_status(struct sdhci_host *host, u32 req_type)
Asutosh Das33a4ff52012-12-18 16:14:02 +05301816{
1817 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1818 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301819 unsigned long flags;
1820 bool done = false;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301821
Sahitya Tummala179e7382013-03-20 19:24:01 +05301822 spin_lock_irqsave(&host->lock, flags);
1823 pr_debug("%s: %s: request %d curr_pwr_state %x curr_io_level %x\n",
1824 mmc_hostname(host->mmc), __func__, req_type,
1825 msm_host->curr_pwr_state, msm_host->curr_io_level);
1826 if ((req_type & msm_host->curr_pwr_state) ||
1827 (req_type & msm_host->curr_io_level))
1828 done = true;
1829 spin_unlock_irqrestore(&host->lock, flags);
Asutosh Das33a4ff52012-12-18 16:14:02 +05301830
Sahitya Tummala179e7382013-03-20 19:24:01 +05301831 /*
1832 * This is needed here to hanlde a case where IRQ gets
1833 * triggered even before this function is called so that
1834 * x->done counter of completion gets reset. Otherwise,
1835 * next call to wait_for_completion returns immediately
1836 * without actually waiting for the IRQ to be handled.
1837 */
1838 if (done)
1839 init_completion(&msm_host->pwr_irq_completion);
1840 else
1841 wait_for_completion(&msm_host->pwr_irq_completion);
1842
1843 pr_debug("%s: %s: request %d done\n", mmc_hostname(host->mmc),
1844 __func__, req_type);
Asutosh Das33a4ff52012-12-18 16:14:02 +05301845}
1846
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07001847static void sdhci_msm_toggle_cdr(struct sdhci_host *host, bool enable)
1848{
1849 if (enable)
1850 writel_relaxed((readl_relaxed(host->ioaddr +
1851 CORE_DLL_CONFIG) | CORE_CDR_EN),
1852 host->ioaddr + CORE_DLL_CONFIG);
1853 else
1854 writel_relaxed((readl_relaxed(host->ioaddr +
1855 CORE_DLL_CONFIG) & ~CORE_CDR_EN),
1856 host->ioaddr + CORE_DLL_CONFIG);
1857}
1858
Asutosh Das3781bd82013-01-10 21:11:04 +05301859static unsigned int sdhci_msm_max_segs(void)
1860{
1861 return SDHCI_MSM_MAX_SEGMENTS;
1862}
1863
Sahitya Tummala00240122013-02-28 19:50:51 +05301864static unsigned int sdhci_msm_get_min_clock(struct sdhci_host *host)
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301865{
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301866 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1867 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301868
Sahitya Tummala00240122013-02-28 19:50:51 +05301869 return msm_host->pdata->sup_clk_table[0];
1870}
1871
1872static unsigned int sdhci_msm_get_max_clock(struct sdhci_host *host)
1873{
1874 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1875 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1876 int max_clk_index = msm_host->pdata->sup_clk_cnt;
1877
1878 return msm_host->pdata->sup_clk_table[max_clk_index - 1];
1879}
1880
1881static unsigned int sdhci_msm_get_sup_clk_rate(struct sdhci_host *host,
1882 u32 req_clk)
1883{
1884 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1885 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1886 unsigned int sel_clk = -1;
1887 unsigned char cnt;
1888
1889 if (req_clk < sdhci_msm_get_min_clock(host)) {
1890 sel_clk = sdhci_msm_get_min_clock(host);
1891 return sel_clk;
1892 }
1893
1894 for (cnt = 0; cnt < msm_host->pdata->sup_clk_cnt; cnt++) {
1895 if (msm_host->pdata->sup_clk_table[cnt] > req_clk) {
1896 break;
1897 } else if (msm_host->pdata->sup_clk_table[cnt] == req_clk) {
1898 sel_clk = msm_host->pdata->sup_clk_table[cnt];
1899 break;
1900 } else {
1901 sel_clk = msm_host->pdata->sup_clk_table[cnt];
1902 }
1903 }
1904 return sel_clk;
1905}
1906
1907static int sdhci_msm_prepare_clocks(struct sdhci_host *host, bool enable)
1908{
1909 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1910 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1911 int rc = 0;
1912
1913 if (enable && !atomic_read(&msm_host->clks_on)) {
1914 pr_debug("%s: request to enable clocks\n",
1915 mmc_hostname(host->mmc));
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301916 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
1917 rc = clk_prepare_enable(msm_host->bus_clk);
1918 if (rc) {
1919 pr_err("%s: %s: failed to enable the bus-clock with error %d\n",
1920 mmc_hostname(host->mmc), __func__, rc);
1921 goto out;
1922 }
1923 }
1924 if (!IS_ERR(msm_host->pclk)) {
1925 rc = clk_prepare_enable(msm_host->pclk);
1926 if (rc) {
1927 pr_err("%s: %s: failed to enable the pclk with error %d\n",
1928 mmc_hostname(host->mmc), __func__, rc);
1929 goto disable_bus_clk;
1930 }
1931 }
1932 rc = clk_prepare_enable(msm_host->clk);
1933 if (rc) {
1934 pr_err("%s: %s: failed to enable the host-clk with error %d\n",
1935 mmc_hostname(host->mmc), __func__, rc);
1936 goto disable_pclk;
1937 }
1938 mb();
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301939
Sahitya Tummala00240122013-02-28 19:50:51 +05301940 } else if (!enable && atomic_read(&msm_host->clks_on)) {
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301941 pr_debug("%s: request to disable clocks\n",
1942 mmc_hostname(host->mmc));
1943 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
1944 mb();
1945 clk_disable_unprepare(msm_host->clk);
1946 if (!IS_ERR(msm_host->pclk))
1947 clk_disable_unprepare(msm_host->pclk);
1948 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
1949 clk_disable_unprepare(msm_host->bus_clk);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301950 }
Sahitya Tummala00240122013-02-28 19:50:51 +05301951 atomic_set(&msm_host->clks_on, enable);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301952 goto out;
1953disable_pclk:
1954 if (!IS_ERR_OR_NULL(msm_host->pclk))
1955 clk_disable_unprepare(msm_host->pclk);
1956disable_bus_clk:
1957 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
1958 clk_disable_unprepare(msm_host->bus_clk);
1959out:
Sahitya Tummala00240122013-02-28 19:50:51 +05301960 return rc;
1961}
1962
1963static void sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock)
1964{
1965 int rc;
1966 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1967 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1968 struct mmc_ios curr_ios = host->mmc->ios;
1969 u32 sup_clock, ddr_clock;
1970
1971 if (!clock) {
1972 sdhci_msm_prepare_clocks(host, false);
1973 host->clock = clock;
1974 return;
1975 }
1976
1977 rc = sdhci_msm_prepare_clocks(host, true);
1978 if (rc)
1979 return;
1980
1981 sup_clock = sdhci_msm_get_sup_clk_rate(host, clock);
1982 if (curr_ios.timing == MMC_TIMING_UHS_DDR50) {
1983 /*
1984 * The SDHC requires internal clock frequency to be double the
1985 * actual clock that will be set for DDR mode. The controller
1986 * uses the faster clock(100MHz) for some of its parts and send
1987 * the actual required clock (50MHz) to the card.
1988 */
1989 ddr_clock = clock * 2;
1990 sup_clock = sdhci_msm_get_sup_clk_rate(host,
1991 ddr_clock);
1992 }
1993 if (sup_clock != msm_host->clk_rate) {
1994 pr_debug("%s: %s: setting clk rate to %u\n",
1995 mmc_hostname(host->mmc), __func__, sup_clock);
1996 rc = clk_set_rate(msm_host->clk, sup_clock);
1997 if (rc) {
1998 pr_err("%s: %s: Failed to set rate %u for host-clk : %d\n",
1999 mmc_hostname(host->mmc), __func__,
2000 sup_clock, rc);
2001 return;
2002 }
2003 msm_host->clk_rate = sup_clock;
2004 host->clock = clock;
2005 }
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302006}
2007
Sahitya Tummala5e4f9642013-03-21 11:13:25 +05302008static int sdhci_msm_set_uhs_signaling(struct sdhci_host *host,
2009 unsigned int uhs)
2010{
2011 u16 ctrl_2;
2012
2013 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2014 /* Select Bus Speed Mode for host */
2015 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
2016 if (uhs == MMC_TIMING_MMC_HS200)
2017 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2018 else if (uhs == MMC_TIMING_UHS_SDR12)
2019 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
2020 else if (uhs == MMC_TIMING_UHS_SDR25)
2021 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
2022 else if (uhs == MMC_TIMING_UHS_SDR50)
2023 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
2024 else if (uhs == MMC_TIMING_UHS_SDR104)
2025 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2026 else if (uhs == MMC_TIMING_UHS_DDR50)
2027 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Sahitya Tummala00240122013-02-28 19:50:51 +05302028 /*
2029 * When clock frquency is less than 100MHz, the feedback clock must be
2030 * provided and DLL must not be used so that tuning can be skipped. To
2031 * provide feedback clock, the mode selection can be any value less
2032 * than 3'b011 in bits [2:0] of HOST CONTROL2 register.
2033 */
2034 if (host->clock <= (100 * 1000 * 1000) &&
2035 (uhs == MMC_TIMING_MMC_HS200 ||
2036 uhs == MMC_TIMING_UHS_SDR104))
2037 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
2038
Sahitya Tummala5e4f9642013-03-21 11:13:25 +05302039 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
2040
2041 return 0;
2042}
2043
Asutosh Das33a4ff52012-12-18 16:14:02 +05302044static struct sdhci_ops sdhci_msm_ops = {
Sahitya Tummala5e4f9642013-03-21 11:13:25 +05302045 .set_uhs_signaling = sdhci_msm_set_uhs_signaling,
Asutosh Das33a4ff52012-12-18 16:14:02 +05302046 .check_power_status = sdhci_msm_check_power_status,
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07002047 .execute_tuning = sdhci_msm_execute_tuning,
2048 .toggle_cdr = sdhci_msm_toggle_cdr,
Asutosh Das3781bd82013-01-10 21:11:04 +05302049 .get_max_segments = sdhci_msm_max_segs,
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302050 .set_clock = sdhci_msm_set_clock,
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302051 .platform_bus_voting = sdhci_msm_bus_voting,
Sahitya Tummala00240122013-02-28 19:50:51 +05302052 .get_min_clock = sdhci_msm_get_min_clock,
2053 .get_max_clock = sdhci_msm_get_max_clock,
Asutosh Das33a4ff52012-12-18 16:14:02 +05302054};
2055
2056static int __devinit sdhci_msm_probe(struct platform_device *pdev)
2057{
2058 struct sdhci_host *host;
2059 struct sdhci_pltfm_host *pltfm_host;
2060 struct sdhci_msm_host *msm_host;
2061 struct resource *core_memres = NULL;
Asutosh Dasbbc84782013-02-11 15:31:35 +05302062 int ret = 0, dead = 0;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302063 u32 vdd_max_current;
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002064 u32 host_version;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302065
2066 pr_debug("%s: Enter %s\n", dev_name(&pdev->dev), __func__);
2067 msm_host = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_msm_host),
2068 GFP_KERNEL);
2069 if (!msm_host) {
2070 ret = -ENOMEM;
2071 goto out;
2072 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05302073
2074 msm_host->sdhci_msm_pdata.ops = &sdhci_msm_ops;
2075 host = sdhci_pltfm_init(pdev, &msm_host->sdhci_msm_pdata);
2076 if (IS_ERR(host)) {
2077 ret = PTR_ERR(host);
2078 goto out;
2079 }
2080
2081 pltfm_host = sdhci_priv(host);
2082 pltfm_host->priv = msm_host;
2083 msm_host->mmc = host->mmc;
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302084 msm_host->pdev = pdev;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302085
2086 /* Extract platform data */
2087 if (pdev->dev.of_node) {
Venkat Gopalakrishnanc61ab7e2013-03-11 12:17:57 -07002088 ret = of_alias_get_id(pdev->dev.of_node, "sdhc");
2089 if (ret < 0) {
2090 dev_err(&pdev->dev, "Failed to get slot index %d\n",
2091 ret);
2092 goto pltfm_free;
2093 }
2094 if (disable_slots & (1 << (ret - 1))) {
2095 dev_info(&pdev->dev, "%s: Slot %d disabled\n", __func__,
2096 ret);
2097 ret = -ENODEV;
2098 goto pltfm_free;
2099 }
2100
Asutosh Das33a4ff52012-12-18 16:14:02 +05302101 msm_host->pdata = sdhci_msm_populate_pdata(&pdev->dev);
2102 if (!msm_host->pdata) {
2103 dev_err(&pdev->dev, "DT parsing error\n");
2104 goto pltfm_free;
2105 }
2106 } else {
2107 dev_err(&pdev->dev, "No device tree node\n");
2108 goto pltfm_free;
2109 }
2110
2111 /* Setup Clocks */
2112
2113 /* Setup SDCC bus voter clock. */
2114 msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus_clk");
2115 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
2116 /* Vote for max. clk rate for max. performance */
2117 ret = clk_set_rate(msm_host->bus_clk, INT_MAX);
2118 if (ret)
2119 goto pltfm_free;
2120 ret = clk_prepare_enable(msm_host->bus_clk);
2121 if (ret)
2122 goto pltfm_free;
2123 }
2124
2125 /* Setup main peripheral bus clock */
2126 msm_host->pclk = devm_clk_get(&pdev->dev, "iface_clk");
2127 if (!IS_ERR(msm_host->pclk)) {
2128 ret = clk_prepare_enable(msm_host->pclk);
2129 if (ret)
2130 goto bus_clk_disable;
2131 }
2132
2133 /* Setup SDC MMC clock */
2134 msm_host->clk = devm_clk_get(&pdev->dev, "core_clk");
2135 if (IS_ERR(msm_host->clk)) {
2136 ret = PTR_ERR(msm_host->clk);
2137 goto pclk_disable;
2138 }
2139
2140 ret = clk_prepare_enable(msm_host->clk);
2141 if (ret)
2142 goto pclk_disable;
2143
Sahitya Tummala00240122013-02-28 19:50:51 +05302144 /* Set to the minimum supported clock frequency */
2145 ret = clk_set_rate(msm_host->clk, sdhci_msm_get_min_clock(host));
2146 if (ret) {
2147 dev_err(&pdev->dev, "MClk rate set failed (%d)\n", ret);
2148 goto clk_disable;
2149 }
2150 msm_host->clk_rate = sdhci_msm_get_min_clock(host);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302151 atomic_set(&msm_host->clks_on, 1);
Sahitya Tummala00240122013-02-28 19:50:51 +05302152
Asutosh Das33a4ff52012-12-18 16:14:02 +05302153 /* Setup regulators */
2154 ret = sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, true);
2155 if (ret) {
2156 dev_err(&pdev->dev, "Regulator setup failed (%d)\n", ret);
2157 goto clk_disable;
2158 }
2159
2160 /* Reset the core and Enable SDHC mode */
2161 core_memres = platform_get_resource_byname(pdev,
2162 IORESOURCE_MEM, "core_mem");
2163 msm_host->core_mem = devm_ioremap(&pdev->dev, core_memres->start,
2164 resource_size(core_memres));
2165
2166 if (!msm_host->core_mem) {
2167 dev_err(&pdev->dev, "Failed to remap registers\n");
2168 ret = -ENOMEM;
2169 goto vreg_deinit;
2170 }
2171
2172 /* Set SW_RST bit in POWER register (Offset 0x0) */
2173 writel_relaxed(CORE_SW_RST, msm_host->core_mem + CORE_POWER);
2174 /* Set HC_MODE_EN bit in HC_MODE register */
2175 writel_relaxed(HC_MODE_EN, (msm_host->core_mem + CORE_HC_MODE));
2176
2177 /*
2178 * Following are the deviations from SDHC spec v3.0 -
2179 * 1. Card detection is handled using separate GPIO.
2180 * 2. Bus power control is handled by interacting with PMIC.
2181 */
2182 host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
2183 host->quirks |= SDHCI_QUIRK_SINGLE_POWER_WRITE;
Sahitya Tummala00240122013-02-28 19:50:51 +05302184 host->quirks |= SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN;
2185 host->quirks2 |= SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK;
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302186 host->quirks2 |= SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302187
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002188 host_version = readl_relaxed((host->ioaddr + SDHCI_HOST_VERSION));
2189 dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n",
2190 host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >>
2191 SDHCI_VENDOR_VER_SHIFT));
2192 if (((host_version & SDHCI_VENDOR_VER_MASK) >>
2193 SDHCI_VENDOR_VER_SHIFT) == SDHCI_VER_100) {
2194 /*
2195 * Add 40us delay in interrupt handler when
2196 * operating at initialization frequency(400KHz).
2197 */
2198 host->quirks2 |= SDHCI_QUIRK2_SLOW_INT_CLR;
2199 /*
2200 * Set Software Reset for DAT line in Software
2201 * Reset Register (Bit 2).
2202 */
2203 host->quirks2 |= SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT;
2204 }
2205
2206 /* Setup PWRCTL irq */
Asutosh Dasbbc84782013-02-11 15:31:35 +05302207 msm_host->pwr_irq = platform_get_irq_byname(pdev, "pwr_irq");
2208 if (msm_host->pwr_irq < 0) {
Asutosh Das33a4ff52012-12-18 16:14:02 +05302209 dev_err(&pdev->dev, "Failed to get pwr_irq by name (%d)\n",
Asutosh Dasbbc84782013-02-11 15:31:35 +05302210 msm_host->pwr_irq);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302211 goto vreg_deinit;
2212 }
Asutosh Dasbbc84782013-02-11 15:31:35 +05302213 ret = devm_request_threaded_irq(&pdev->dev, msm_host->pwr_irq, NULL,
Asutosh Das33a4ff52012-12-18 16:14:02 +05302214 sdhci_msm_pwr_irq, IRQF_ONESHOT,
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07002215 dev_name(&pdev->dev), host);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302216 if (ret) {
2217 dev_err(&pdev->dev, "Request threaded irq(%d) failed (%d)\n",
Asutosh Dasbbc84782013-02-11 15:31:35 +05302218 msm_host->pwr_irq, ret);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302219 goto vreg_deinit;
2220 }
2221
2222 /* Enable pwr irq interrupts */
2223 writel_relaxed(INT_MASK, (msm_host->core_mem + CORE_PWRCTL_MASK));
2224
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302225 /* Set clock gating delay to be used when CONFIG_MMC_CLKGATE is set */
2226 msm_host->mmc->clkgate_delay = SDHCI_MSM_MMC_CLK_GATE_DELAY;
2227
Asutosh Das33a4ff52012-12-18 16:14:02 +05302228 /* Set host capabilities */
2229 msm_host->mmc->caps |= msm_host->pdata->mmc_bus_width;
2230 msm_host->mmc->caps |= msm_host->pdata->caps;
2231
2232 vdd_max_current = sdhci_msm_get_vreg_vdd_max_current(msm_host);
2233 if (vdd_max_current >= 800)
2234 msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_800;
2235 else if (vdd_max_current >= 600)
2236 msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_600;
2237 else if (vdd_max_current >= 400)
2238 msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_400;
2239 else
2240 msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_200;
2241
2242 if (vdd_max_current > 150)
2243 msm_host->mmc->caps |= MMC_CAP_SET_XPC_180 |
2244 MMC_CAP_SET_XPC_300|
2245 MMC_CAP_SET_XPC_330;
2246
Sahitya Tummalacbe1b7a2013-02-28 12:21:58 +05302247 msm_host->mmc->caps |= MMC_CAP_HW_RESET;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302248 msm_host->mmc->caps2 |= msm_host->pdata->caps2;
Asutosh Dasbbc84782013-02-11 15:31:35 +05302249 msm_host->mmc->caps2 |= MMC_CAP2_CORE_RUNTIME_PM;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302250 msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR;
2251 msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR_CONTROL;
2252 msm_host->mmc->caps2 |= (MMC_CAP2_BOOTPART_NOACC |
2253 MMC_CAP2_DETECT_ON_ERR);
2254 msm_host->mmc->caps2 |= MMC_CAP2_SANITIZE;
2255 msm_host->mmc->caps2 |= MMC_CAP2_CACHE_CTRL;
2256 msm_host->mmc->caps2 |= MMC_CAP2_INIT_BKOPS;
2257 msm_host->mmc->caps2 |= MMC_CAP2_POWEROFF_NOTIFY;
Sahitya Tummala00240122013-02-28 19:50:51 +05302258 msm_host->mmc->caps2 |= MMC_CAP2_CLK_SCALE;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302259
2260 if (msm_host->pdata->nonremovable)
2261 msm_host->mmc->caps |= MMC_CAP_NONREMOVABLE;
2262
Sahitya Tummalab4e84042013-03-10 07:03:17 +05302263 host->cpu_dma_latency_us = msm_host->pdata->cpu_dma_latency_us;
2264
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302265 ret = sdhci_msm_bus_register(msm_host, pdev);
2266 if (ret)
2267 goto vreg_deinit;
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
Sahitya Tummala179e7382013-03-20 19:24:01 +05302273 init_completion(&msm_host->pwr_irq_completion);
2274
Sahitya Tummala62448d92013-03-12 14:57:46 +05302275 if (gpio_is_valid(msm_host->pdata->status_gpio)) {
2276 ret = mmc_cd_gpio_request(msm_host->mmc,
2277 msm_host->pdata->status_gpio);
2278 if (ret) {
2279 dev_err(&pdev->dev, "%s: Failed to request card detection IRQ %d\n",
2280 __func__, ret);
2281 goto bus_unregister;
2282 }
2283 }
2284
Sahitya Tummala2fa7eb12013-03-20 19:34:59 +05302285 if (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(32))) {
2286 host->dma_mask = DMA_BIT_MASK(32);
2287 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2288 } else {
2289 dev_err(&pdev->dev, "%s: Failed to set dma mask\n", __func__);
2290 }
2291
Asutosh Das33a4ff52012-12-18 16:14:02 +05302292 ret = sdhci_add_host(host);
2293 if (ret) {
2294 dev_err(&pdev->dev, "Add host failed (%d)\n", ret);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302295 goto free_cd_gpio;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302296 }
2297
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302298 msm_host->msm_bus_vote.max_bus_bw.show = show_sdhci_max_bus_bw;
2299 msm_host->msm_bus_vote.max_bus_bw.store = store_sdhci_max_bus_bw;
2300 sysfs_attr_init(&msm_host->msm_bus_vote.max_bus_bw.attr);
2301 msm_host->msm_bus_vote.max_bus_bw.attr.name = "max_bus_bw";
2302 msm_host->msm_bus_vote.max_bus_bw.attr.mode = S_IRUGO | S_IWUSR;
2303 ret = device_create_file(&pdev->dev,
2304 &msm_host->msm_bus_vote.max_bus_bw);
2305 if (ret)
2306 goto remove_host;
2307
Asutosh Dasbbc84782013-02-11 15:31:35 +05302308 ret = pm_runtime_set_active(&pdev->dev);
2309 if (ret)
2310 pr_err("%s: %s: pm_runtime_set_active failed: err: %d\n",
2311 mmc_hostname(host->mmc), __func__, ret);
2312 else
2313 pm_runtime_enable(&pdev->dev);
2314
Asutosh Das33a4ff52012-12-18 16:14:02 +05302315 /* Successful initialization */
2316 goto out;
2317
2318remove_host:
2319 dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
2320 sdhci_remove_host(host, dead);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302321free_cd_gpio:
2322 if (gpio_is_valid(msm_host->pdata->status_gpio))
2323 mmc_cd_gpio_free(msm_host->mmc);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302324bus_unregister:
2325 sdhci_msm_bus_unregister(msm_host);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302326vreg_deinit:
2327 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
2328clk_disable:
2329 if (!IS_ERR(msm_host->clk))
2330 clk_disable_unprepare(msm_host->clk);
2331pclk_disable:
2332 if (!IS_ERR(msm_host->pclk))
2333 clk_disable_unprepare(msm_host->pclk);
2334bus_clk_disable:
2335 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2336 clk_disable_unprepare(msm_host->bus_clk);
2337pltfm_free:
2338 sdhci_pltfm_free(pdev);
2339out:
2340 pr_debug("%s: Exit %s\n", dev_name(&pdev->dev), __func__);
2341 return ret;
2342}
2343
2344static int __devexit sdhci_msm_remove(struct platform_device *pdev)
2345{
2346 struct sdhci_host *host = platform_get_drvdata(pdev);
2347 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2348 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2349 struct sdhci_msm_pltfm_data *pdata = msm_host->pdata;
2350 int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) ==
2351 0xffffffff);
2352
2353 pr_debug("%s: %s\n", dev_name(&pdev->dev), __func__);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302354 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302355 sdhci_remove_host(host, dead);
Asutosh Dasbbc84782013-02-11 15:31:35 +05302356 pm_runtime_disable(&pdev->dev);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302357 sdhci_pltfm_free(pdev);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302358
2359 if (gpio_is_valid(msm_host->pdata->status_gpio))
2360 mmc_cd_gpio_free(msm_host->mmc);
2361
Asutosh Das33a4ff52012-12-18 16:14:02 +05302362 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302363
Asutosh Das33a4ff52012-12-18 16:14:02 +05302364 if (pdata->pin_data)
Asutosh Das390519d2012-12-21 12:21:42 +05302365 sdhci_msm_setup_pins(pdata, false);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302366
2367 if (msm_host->msm_bus_vote.client_handle) {
2368 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2369 sdhci_msm_bus_unregister(msm_host);
2370 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05302371 return 0;
2372}
2373
Asutosh Dasbbc84782013-02-11 15:31:35 +05302374static int sdhci_msm_runtime_suspend(struct device *dev)
2375{
2376 struct sdhci_host *host = dev_get_drvdata(dev);
2377 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2378 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2379
2380 disable_irq(host->irq);
2381 disable_irq(msm_host->pwr_irq);
2382
2383 return 0;
2384}
2385
2386static int sdhci_msm_runtime_resume(struct device *dev)
2387{
2388 struct sdhci_host *host = dev_get_drvdata(dev);
2389 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2390 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2391
2392 enable_irq(msm_host->pwr_irq);
2393 enable_irq(host->irq);
2394
2395 return 0;
2396}
2397
2398#ifdef CONFIG_PM_SLEEP
2399
2400static int sdhci_msm_suspend(struct device *dev)
2401{
2402 struct sdhci_host *host = dev_get_drvdata(dev);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302403 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2404 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Dasbbc84782013-02-11 15:31:35 +05302405 int ret = 0;
2406
Sahitya Tummala62448d92013-03-12 14:57:46 +05302407 if (gpio_is_valid(msm_host->pdata->status_gpio))
2408 mmc_cd_gpio_free(msm_host->mmc);
2409
Asutosh Dasbbc84782013-02-11 15:31:35 +05302410 if (pm_runtime_suspended(dev)) {
2411 pr_debug("%s: %s: already runtime suspended\n",
2412 mmc_hostname(host->mmc), __func__);
2413 goto out;
2414 }
2415
2416 return sdhci_msm_runtime_suspend(dev);
2417out:
2418 return ret;
2419}
2420
2421static int sdhci_msm_resume(struct device *dev)
2422{
2423 struct sdhci_host *host = dev_get_drvdata(dev);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302424 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2425 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Dasbbc84782013-02-11 15:31:35 +05302426 int ret = 0;
2427
Sahitya Tummala62448d92013-03-12 14:57:46 +05302428 if (gpio_is_valid(msm_host->pdata->status_gpio)) {
2429 ret = mmc_cd_gpio_request(msm_host->mmc,
2430 msm_host->pdata->status_gpio);
2431 if (ret)
2432 pr_err("%s: %s: Failed to request card detection IRQ %d\n",
2433 mmc_hostname(host->mmc), __func__, ret);
2434 }
2435
Asutosh Dasbbc84782013-02-11 15:31:35 +05302436 if (pm_runtime_suspended(dev)) {
2437 pr_debug("%s: %s: runtime suspended, defer system resume\n",
2438 mmc_hostname(host->mmc), __func__);
2439 goto out;
2440 }
2441
2442 return sdhci_msm_runtime_resume(dev);
2443out:
2444 return ret;
2445}
2446#endif
2447
2448#ifdef CONFIG_PM
2449static const struct dev_pm_ops sdhci_msm_pmops = {
2450 SET_SYSTEM_SLEEP_PM_OPS(sdhci_msm_suspend, sdhci_msm_resume)
2451 SET_RUNTIME_PM_OPS(sdhci_msm_runtime_suspend, sdhci_msm_runtime_resume,
2452 NULL)
2453};
2454
2455#define SDHCI_MSM_PMOPS (&sdhci_msm_pmops)
2456
2457#else
2458#define SDHCI_PM_OPS NULL
2459#endif
Asutosh Das33a4ff52012-12-18 16:14:02 +05302460static const struct of_device_id sdhci_msm_dt_match[] = {
2461 {.compatible = "qcom,sdhci-msm"},
2462};
2463MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match);
2464
2465static struct platform_driver sdhci_msm_driver = {
2466 .probe = sdhci_msm_probe,
2467 .remove = __devexit_p(sdhci_msm_remove),
2468 .driver = {
2469 .name = "sdhci_msm",
2470 .owner = THIS_MODULE,
2471 .of_match_table = sdhci_msm_dt_match,
Asutosh Dasbbc84782013-02-11 15:31:35 +05302472 .pm = SDHCI_MSM_PMOPS,
Asutosh Das33a4ff52012-12-18 16:14:02 +05302473 },
2474};
2475
2476module_platform_driver(sdhci_msm_driver);
2477
2478MODULE_DESCRIPTION("Qualcomm Secure Digital Host Controller Interface driver");
2479MODULE_LICENSE("GPL v2");