blob: e9981aece98a9782a2c65eab344d001a6f4fa76f [file] [log] [blame]
Asutosh Das0ef24812012-12-18 16:14:02 +05301/*
2 * drivers/mmc/host/sdhci-msm.c - Qualcomm Technologies, Inc. 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 Gopalakrishnan7944a372012-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>
Sahitya Tummala581df132013-03-12 14:57:46 +053035#include <linux/mmc/slot-gpio.h>
Sahitya Tummalaeaa21862013-03-20 19:34:59 +053036#include <linux/dma-mapping.h>
Sahitya Tummala8a3e8182013-03-10 14:12:52 +053037#include <linux/msm-bus.h>
Asutosh Das0ef24812012-12-18 16:14:02 +053038
39#include "sdhci-pltfm.h"
40
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -070041#define SDHCI_VER_100 0x2B
Asutosh Das0ef24812012-12-18 16:14:02 +053042#define CORE_HC_MODE 0x78
43#define HC_MODE_EN 0x1
44
45#define CORE_POWER 0x0
46#define CORE_SW_RST (1 << 7)
47
48#define CORE_PWRCTL_STATUS 0xDC
49#define CORE_PWRCTL_MASK 0xE0
50#define CORE_PWRCTL_CLEAR 0xE4
51#define CORE_PWRCTL_CTL 0xE8
52
53#define CORE_PWRCTL_BUS_OFF 0x01
54#define CORE_PWRCTL_BUS_ON (1 << 1)
55#define CORE_PWRCTL_IO_LOW (1 << 2)
56#define CORE_PWRCTL_IO_HIGH (1 << 3)
57
58#define CORE_PWRCTL_BUS_SUCCESS 0x01
59#define CORE_PWRCTL_BUS_FAIL (1 << 1)
60#define CORE_PWRCTL_IO_SUCCESS (1 << 2)
61#define CORE_PWRCTL_IO_FAIL (1 << 3)
62
63#define INT_MASK 0xF
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -070064#define MAX_PHASES 16
65
66#define CORE_DLL_LOCK (1 << 7)
67#define CORE_DLL_EN (1 << 16)
68#define CORE_CDR_EN (1 << 17)
69#define CORE_CK_OUT_EN (1 << 18)
70#define CORE_CDR_EXT_EN (1 << 19)
71#define CORE_DLL_PDN (1 << 29)
72#define CORE_DLL_RST (1 << 30)
73#define CORE_DLL_CONFIG 0x100
74#define CORE_DLL_TEST_CTL 0x104
75#define CORE_DLL_STATUS 0x108
76
77#define CORE_VENDOR_SPEC 0x10C
78#define CORE_CLK_PWRSAVE (1 << 1)
79#define CORE_IO_PAD_PWR_SWITCH (1 << 16)
80
Asutosh Das648f9d12013-01-10 21:11:04 +053081/* 8KB descriptors */
82#define SDHCI_MSM_MAX_SEGMENTS (1 << 13)
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +053083#define SDHCI_MSM_MMC_CLK_GATE_DELAY 200 /* msecs */
Asutosh Das648f9d12013-01-10 21:11:04 +053084
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -070085static const u32 tuning_block_64[] = {
86 0x00FF0FFF, 0xCCC3CCFF, 0xFFCC3CC3, 0xEFFEFFFE,
87 0xDDFFDFFF, 0xFBFFFBFF, 0xFF7FFFBF, 0xEFBDF777,
88 0xF0FFF0FF, 0x3CCCFC0F, 0xCFCC33CC, 0xEEFFEFFF,
89 0xFDFFFDFF, 0xFFBFFFDF, 0xFFF7FFBB, 0xDE7B7FF7
90};
91
92static const u32 tuning_block_128[] = {
93 0xFF00FFFF, 0x0000FFFF, 0xCCCCFFFF, 0xCCCC33CC,
94 0xCC3333CC, 0xFFFFCCCC, 0xFFFFEEFF, 0xFFEEEEFF,
95 0xFFDDFFFF, 0xDDDDFFFF, 0xBBFFFFFF, 0xBBFFFFFF,
96 0xFFFFFFBB, 0xFFFFFF77, 0x77FF7777, 0xFFEEDDBB,
97 0x00FFFFFF, 0x00FFFFFF, 0xCCFFFF00, 0xCC33CCCC,
98 0x3333CCCC, 0xFFCCCCCC, 0xFFEEFFFF, 0xEEEEFFFF,
99 0xDDFFFFFF, 0xDDFFFFFF, 0xFFFFFFDD, 0xFFFFFFBB,
100 0xFFFFBBBB, 0xFFFF77FF, 0xFF7777FF, 0xEEDDBB77
101};
Asutosh Das0ef24812012-12-18 16:14:02 +0530102
103/* This structure keeps information per regulator */
104struct sdhci_msm_reg_data {
105 /* voltage regulator handle */
106 struct regulator *reg;
107 /* regulator name */
108 const char *name;
109 /* voltage level to be set */
110 u32 low_vol_level;
111 u32 high_vol_level;
112 /* Load values for low power and high power mode */
113 u32 lpm_uA;
114 u32 hpm_uA;
115
116 /* is this regulator enabled? */
117 bool is_enabled;
118 /* is this regulator needs to be always on? */
119 bool is_always_on;
120 /* is low power mode setting required for this regulator? */
121 bool lpm_sup;
122 bool set_voltage_sup;
123};
124
125/*
126 * This structure keeps information for all the
127 * regulators required for a SDCC slot.
128 */
129struct sdhci_msm_slot_reg_data {
130 /* keeps VDD/VCC regulator info */
131 struct sdhci_msm_reg_data *vdd_data;
132 /* keeps VDD IO regulator info */
133 struct sdhci_msm_reg_data *vdd_io_data;
134};
135
136struct sdhci_msm_gpio {
137 u32 no;
138 const char *name;
139 bool is_enabled;
140};
141
142struct sdhci_msm_gpio_data {
143 struct sdhci_msm_gpio *gpio;
144 u8 size;
145};
146
147struct sdhci_msm_pin_data {
148 /*
149 * = 1 if controller pins are using gpios
150 * = 0 if controller has dedicated MSM pads
151 */
152 bool cfg_sts;
153 struct sdhci_msm_gpio_data *gpio_data;
154};
155
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530156struct sdhci_msm_bus_voting_data {
157 struct msm_bus_scale_pdata *bus_pdata;
158 unsigned int *bw_vecs;
159 unsigned int bw_vecs_size;
160};
161
Asutosh Das0ef24812012-12-18 16:14:02 +0530162struct sdhci_msm_pltfm_data {
163 /* Supported UHS-I Modes */
164 u32 caps;
165
166 /* More capabilities */
167 u32 caps2;
168
169 unsigned long mmc_bus_width;
170 u32 max_clk;
171 struct sdhci_msm_slot_reg_data *vreg_data;
172 bool nonremovable;
173 struct sdhci_msm_pin_data *pin_data;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +0530174 u32 cpu_dma_latency_us;
Sahitya Tummala581df132013-03-12 14:57:46 +0530175 int status_gpio; /* card detection GPIO that is configured as IRQ */
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530176 struct sdhci_msm_bus_voting_data *voting_data;
177};
178
179struct sdhci_msm_bus_vote {
180 uint32_t client_handle;
181 uint32_t curr_vote;
182 int min_bw_vote;
183 int max_bw_vote;
184 bool is_max_bw_needed;
185 struct delayed_work vote_work;
186 struct device_attribute max_bus_bw;
Asutosh Das0ef24812012-12-18 16:14:02 +0530187};
188
189struct sdhci_msm_host {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530190 struct platform_device *pdev;
Asutosh Das0ef24812012-12-18 16:14:02 +0530191 void __iomem *core_mem; /* MSM SDCC mapped address */
192 struct clk *clk; /* main SD/MMC bus clock */
193 struct clk *pclk; /* SDHC peripheral bus clock */
194 struct clk *bus_clk; /* SDHC bus voter clock */
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +0530195 atomic_t clks_on; /* Set if clocks are enabled */
Asutosh Das0ef24812012-12-18 16:14:02 +0530196 struct sdhci_msm_pltfm_data *pdata;
197 struct mmc_host *mmc;
198 struct sdhci_pltfm_data sdhci_msm_pdata;
199 wait_queue_head_t pwr_irq_wait;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530200 struct sdhci_msm_bus_vote msm_bus_vote;
Asutosh Das0ef24812012-12-18 16:14:02 +0530201};
202
203enum vdd_io_level {
204 /* set vdd_io_data->low_vol_level */
205 VDD_IO_LOW,
206 /* set vdd_io_data->high_vol_level */
207 VDD_IO_HIGH,
208 /*
209 * set whatever there in voltage_level (third argument) of
210 * sdhci_msm_set_vdd_io_vol() function.
211 */
212 VDD_IO_SET_LEVEL,
213};
214
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700215/* MSM platform specific tuning */
216static inline int msm_dll_poll_ck_out_en(struct sdhci_host *host,
217 u8 poll)
218{
219 int rc = 0;
220 u32 wait_cnt = 50;
221 u8 ck_out_en = 0;
222 struct mmc_host *mmc = host->mmc;
223
224 /* poll for CK_OUT_EN bit. max. poll time = 50us */
225 ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) &
226 CORE_CK_OUT_EN);
227
228 while (ck_out_en != poll) {
229 if (--wait_cnt == 0) {
230 pr_err("%s: %s: CK_OUT_EN bit is not %d\n",
231 mmc_hostname(mmc), __func__, poll);
232 rc = -ETIMEDOUT;
233 goto out;
234 }
235 udelay(1);
236
237 ck_out_en = !!(readl_relaxed(host->ioaddr +
238 CORE_DLL_CONFIG) & CORE_CK_OUT_EN);
239 }
240out:
241 return rc;
242}
243
244static int msm_config_cm_dll_phase(struct sdhci_host *host, u8 phase)
245{
246 int rc = 0;
247 u8 grey_coded_phase_table[] = {0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4,
248 0xC, 0xD, 0xF, 0xE, 0xA, 0xB, 0x9,
249 0x8};
250 unsigned long flags;
251 u32 config;
252 struct mmc_host *mmc = host->mmc;
253
254 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
255 spin_lock_irqsave(&host->lock, flags);
256
257 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
258 config &= ~(CORE_CDR_EN | CORE_CK_OUT_EN);
259 config |= (CORE_CDR_EXT_EN | CORE_DLL_EN);
260 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
261
262 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '0' */
263 rc = msm_dll_poll_ck_out_en(host, 0);
264 if (rc)
265 goto err_out;
266
267 /*
268 * Write the selected DLL clock output phase (0 ... 15)
269 * to CDR_SELEXT bit field of DLL_CONFIG register.
270 */
271 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
272 & ~(0xF << 20))
273 | (grey_coded_phase_table[phase] << 20)),
274 host->ioaddr + CORE_DLL_CONFIG);
275
276 /* Set CK_OUT_EN bit of DLL_CONFIG register to 1. */
277 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
278 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
279
280 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '1' */
281 rc = msm_dll_poll_ck_out_en(host, 1);
282 if (rc)
283 goto err_out;
284
285 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
286 config |= CORE_CDR_EN;
287 config &= ~CORE_CDR_EXT_EN;
288 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
289 goto out;
290
291err_out:
292 pr_err("%s: %s: Failed to set DLL phase: %d\n",
293 mmc_hostname(mmc), __func__, phase);
294out:
295 spin_unlock_irqrestore(&host->lock, flags);
296 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
297 return rc;
298}
299
300/*
301 * Find out the greatest range of consecuitive selected
302 * DLL clock output phases that can be used as sampling
303 * setting for SD3.0 UHS-I card read operation (in SDR104
304 * timing mode) or for eMMC4.5 card read operation (in HS200
305 * timing mode).
306 * Select the 3/4 of the range and configure the DLL with the
307 * selected DLL clock output phase.
308 */
309
310static int msm_find_most_appropriate_phase(struct sdhci_host *host,
311 u8 *phase_table, u8 total_phases)
312{
313 int ret;
314 u8 ranges[MAX_PHASES][MAX_PHASES] = { {0}, {0} };
315 u8 phases_per_row[MAX_PHASES] = {0};
316 int row_index = 0, col_index = 0, selected_row_index = 0, curr_max = 0;
317 int i, cnt, phase_0_raw_index = 0, phase_15_raw_index = 0;
318 bool phase_0_found = false, phase_15_found = false;
319 struct mmc_host *mmc = host->mmc;
320
321 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
322 if (!total_phases || (total_phases > MAX_PHASES)) {
323 pr_err("%s: %s: invalid argument: total_phases=%d\n",
324 mmc_hostname(mmc), __func__, total_phases);
325 return -EINVAL;
326 }
327
328 for (cnt = 0; cnt < total_phases; cnt++) {
329 ranges[row_index][col_index] = phase_table[cnt];
330 phases_per_row[row_index] += 1;
331 col_index++;
332
333 if ((cnt + 1) == total_phases) {
334 continue;
335 /* check if next phase in phase_table is consecutive or not */
336 } else if ((phase_table[cnt] + 1) != phase_table[cnt + 1]) {
337 row_index++;
338 col_index = 0;
339 }
340 }
341
342 if (row_index >= MAX_PHASES)
343 return -EINVAL;
344
345 /* Check if phase-0 is present in first valid window? */
346 if (!ranges[0][0]) {
347 phase_0_found = true;
348 phase_0_raw_index = 0;
349 /* Check if cycle exist between 2 valid windows */
350 for (cnt = 1; cnt <= row_index; cnt++) {
351 if (phases_per_row[cnt]) {
352 for (i = 0; i < phases_per_row[cnt]; i++) {
353 if (ranges[cnt][i] == 15) {
354 phase_15_found = true;
355 phase_15_raw_index = cnt;
356 break;
357 }
358 }
359 }
360 }
361 }
362
363 /* If 2 valid windows form cycle then merge them as single window */
364 if (phase_0_found && phase_15_found) {
365 /* number of phases in raw where phase 0 is present */
366 u8 phases_0 = phases_per_row[phase_0_raw_index];
367 /* number of phases in raw where phase 15 is present */
368 u8 phases_15 = phases_per_row[phase_15_raw_index];
369
370 if (phases_0 + phases_15 >= MAX_PHASES)
371 /*
372 * If there are more than 1 phase windows then total
373 * number of phases in both the windows should not be
374 * more than or equal to MAX_PHASES.
375 */
376 return -EINVAL;
377
378 /* Merge 2 cyclic windows */
379 i = phases_15;
380 for (cnt = 0; cnt < phases_0; cnt++) {
381 ranges[phase_15_raw_index][i] =
382 ranges[phase_0_raw_index][cnt];
383 if (++i >= MAX_PHASES)
384 break;
385 }
386
387 phases_per_row[phase_0_raw_index] = 0;
388 phases_per_row[phase_15_raw_index] = phases_15 + phases_0;
389 }
390
391 for (cnt = 0; cnt <= row_index; cnt++) {
392 if (phases_per_row[cnt] > curr_max) {
393 curr_max = phases_per_row[cnt];
394 selected_row_index = cnt;
395 }
396 }
397
398 i = ((curr_max * 3) / 4);
399 if (i)
400 i--;
401
402 ret = (int)ranges[selected_row_index][i];
403
404 if (ret >= MAX_PHASES) {
405 ret = -EINVAL;
406 pr_err("%s: %s: invalid phase selected=%d\n",
407 mmc_hostname(mmc), __func__, ret);
408 }
409
410 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
411 return ret;
412}
413
414static inline void msm_cm_dll_set_freq(struct sdhci_host *host)
415{
416 u32 mclk_freq = 0;
417
418 /* Program the MCLK value to MCLK_FREQ bit field */
419 if (host->clock <= 112000000)
420 mclk_freq = 0;
421 else if (host->clock <= 125000000)
422 mclk_freq = 1;
423 else if (host->clock <= 137000000)
424 mclk_freq = 2;
425 else if (host->clock <= 150000000)
426 mclk_freq = 3;
427 else if (host->clock <= 162000000)
428 mclk_freq = 4;
429 else if (host->clock <= 175000000)
430 mclk_freq = 5;
431 else if (host->clock <= 187000000)
432 mclk_freq = 6;
433 else if (host->clock <= 200000000)
434 mclk_freq = 7;
435
436 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
437 & ~(7 << 24)) | (mclk_freq << 24)),
438 host->ioaddr + CORE_DLL_CONFIG);
439}
440
441/* Initialize the DLL (Programmable Delay Line ) */
442static int msm_init_cm_dll(struct sdhci_host *host)
443{
444 struct mmc_host *mmc = host->mmc;
445 int rc = 0;
446 unsigned long flags;
447 u32 wait_cnt;
448
449 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
450 spin_lock_irqsave(&host->lock, flags);
451
452 /*
453 * Make sure that clock is always enabled when DLL
454 * tuning is in progress. Keeping PWRSAVE ON may
455 * turn off the clock. So let's disable the PWRSAVE
456 * here and re-enable it once tuning is completed.
457 */
458 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
459 & ~CORE_CLK_PWRSAVE),
460 host->ioaddr + CORE_VENDOR_SPEC);
461
462 /* Write 1 to DLL_RST bit of DLL_CONFIG register */
463 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
464 | CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
465
466 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
467 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
468 | CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
469 msm_cm_dll_set_freq(host);
470
471 /* Write 0 to DLL_RST bit of DLL_CONFIG register */
472 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
473 & ~CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
474
475 /* Write 0 to DLL_PDN bit of DLL_CONFIG register */
476 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
477 & ~CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
478
479 /* Set DLL_EN bit to 1. */
480 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
481 | CORE_DLL_EN), host->ioaddr + CORE_DLL_CONFIG);
482
483 /* Set CK_OUT_EN bit to 1. */
484 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
485 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
486
487 wait_cnt = 50;
488 /* Wait until DLL_LOCK bit of DLL_STATUS register becomes '1' */
489 while (!(readl_relaxed(host->ioaddr + CORE_DLL_STATUS) &
490 CORE_DLL_LOCK)) {
491 /* max. wait for 50us sec for LOCK bit to be set */
492 if (--wait_cnt == 0) {
493 pr_err("%s: %s: DLL failed to LOCK\n",
494 mmc_hostname(mmc), __func__);
495 rc = -ETIMEDOUT;
496 goto out;
497 }
498 /* wait for 1us before polling again */
499 udelay(1);
500 }
501
502out:
503 /* re-enable PWRSAVE */
504 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
505 CORE_CLK_PWRSAVE),
506 host->ioaddr + CORE_VENDOR_SPEC);
507 spin_unlock_irqrestore(&host->lock, flags);
508 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
509 return rc;
510}
511
512int sdhci_msm_execute_tuning(struct sdhci_host *host, u32 opcode)
513{
514 unsigned long flags;
515 u8 phase, *data_buf, tuned_phases[16], tuned_phase_cnt = 0;
516 const u32 *tuning_block_pattern = tuning_block_64;
517 int size = sizeof(tuning_block_64); /* Tuning pattern size in bytes */
518 int rc;
519 struct mmc_host *mmc = host->mmc;
520
521 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
522 /* Tuning is only required for SDR104 modes */
523 spin_lock_irqsave(&host->lock, flags);
524
525 if ((opcode == MMC_SEND_TUNING_BLOCK_HS200) &&
526 (mmc->ios.bus_width == MMC_BUS_WIDTH_8)) {
527 tuning_block_pattern = tuning_block_128;
528 size = sizeof(tuning_block_128);
529 }
530 spin_unlock_irqrestore(&host->lock, flags);
531
532 /* first of all reset the tuning block */
533 rc = msm_init_cm_dll(host);
534 if (rc)
535 goto out;
536
537 data_buf = kmalloc(size, GFP_KERNEL);
538 if (!data_buf) {
539 rc = -ENOMEM;
540 goto out;
541 }
542
543 phase = 0;
544 do {
545 struct mmc_command cmd = {0};
546 struct mmc_data data = {0};
547 struct mmc_request mrq = {
548 .cmd = &cmd,
549 .data = &data
550 };
551 struct scatterlist sg;
552
553 /* set the phase in delay line hw block */
554 rc = msm_config_cm_dll_phase(host, phase);
555 if (rc)
556 goto kfree;
557
558 cmd.opcode = opcode;
559 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
560
561 data.blksz = size;
562 data.blocks = 1;
563 data.flags = MMC_DATA_READ;
564 data.timeout_ns = 1000 * 1000 * 1000; /* 1 sec */
565
566 data.sg = &sg;
567 data.sg_len = 1;
568 sg_init_one(&sg, data_buf, size);
569 memset(data_buf, 0, size);
570 mmc_wait_for_req(mmc, &mrq);
571
572 if (!cmd.error && !data.error &&
573 !memcmp(data_buf, tuning_block_pattern, size)) {
574 /* tuning is successful at this tuning point */
575 tuned_phases[tuned_phase_cnt++] = phase;
576 pr_debug("%s: %s: found good phase = %d\n",
577 mmc_hostname(mmc), __func__, phase);
578 }
579 } while (++phase < 16);
580
581 if (tuned_phase_cnt) {
582 rc = msm_find_most_appropriate_phase(host, tuned_phases,
583 tuned_phase_cnt);
584 if (rc < 0)
585 goto kfree;
586 else
587 phase = (u8)rc;
588
589 /*
590 * Finally set the selected phase in delay
591 * line hw block.
592 */
593 rc = msm_config_cm_dll_phase(host, phase);
594 if (rc)
595 goto kfree;
596 pr_debug("%s: %s: finally setting the tuning phase to %d\n",
597 mmc_hostname(mmc), __func__, phase);
598 } else {
599 /* tuning failed */
600 pr_err("%s: %s: no tuning point found\n",
601 mmc_hostname(mmc), __func__);
602 rc = -EAGAIN;
603 }
604
605kfree:
606 kfree(data_buf);
607out:
608 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
609 return rc;
610}
611
Asutosh Das0ef24812012-12-18 16:14:02 +0530612static int sdhci_msm_setup_gpio(struct sdhci_msm_pltfm_data *pdata, bool enable)
613{
614 struct sdhci_msm_gpio_data *curr;
615 int i, ret = 0;
616
617 curr = pdata->pin_data->gpio_data;
618 for (i = 0; i < curr->size; i++) {
619 if (!gpio_is_valid(curr->gpio[i].no)) {
620 ret = -EINVAL;
621 pr_err("%s: Invalid gpio = %d\n", __func__,
622 curr->gpio[i].no);
623 goto free_gpios;
624 }
625 if (enable) {
626 ret = gpio_request(curr->gpio[i].no,
627 curr->gpio[i].name);
628 if (ret) {
629 pr_err("%s: gpio_request(%d, %s) failed %d\n",
630 __func__, curr->gpio[i].no,
631 curr->gpio[i].name, ret);
632 goto free_gpios;
633 }
634 curr->gpio[i].is_enabled = true;
635 } else {
636 gpio_free(curr->gpio[i].no);
637 curr->gpio[i].is_enabled = false;
638 }
639 }
640 return ret;
641
642free_gpios:
643 for (i--; i >= 0; i--) {
644 gpio_free(curr->gpio[i].no);
645 curr->gpio[i].is_enabled = false;
646 }
647 return ret;
648}
649
650static int sdhci_msm_setup_pins(struct sdhci_msm_pltfm_data *pdata, bool enable)
651{
652 int ret = 0;
653
654 if (!pdata->pin_data || (pdata->pin_data->cfg_sts == enable))
655 return 0;
656
657 ret = sdhci_msm_setup_gpio(pdata, enable);
658 if (!ret)
659 pdata->pin_data->cfg_sts = enable;
660
661 return ret;
662}
663
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530664static int sdhci_msm_dt_get_array(struct device *dev, const char *prop_name,
665 u32 **out, int *len, u32 size)
666{
667 int ret = 0;
668 struct device_node *np = dev->of_node;
669 size_t sz;
670 u32 *arr = NULL;
671
672 if (!of_get_property(np, prop_name, len)) {
673 ret = -EINVAL;
674 goto out;
675 }
676 sz = *len = *len / sizeof(*arr);
677 if (sz <= 0 || (size > 0 && (sz != size))) {
678 dev_err(dev, "%s invalid size\n", prop_name);
679 ret = -EINVAL;
680 goto out;
681 }
682
683 arr = devm_kzalloc(dev, sz * sizeof(*arr), GFP_KERNEL);
684 if (!arr) {
685 dev_err(dev, "%s failed allocating memory\n", prop_name);
686 ret = -ENOMEM;
687 goto out;
688 }
689
690 ret = of_property_read_u32_array(np, prop_name, arr, sz);
691 if (ret < 0) {
692 dev_err(dev, "%s failed reading array %d\n", prop_name, ret);
693 goto out;
694 }
695 *out = arr;
696out:
697 if (ret)
698 *len = 0;
699 return ret;
700}
701
Asutosh Das0ef24812012-12-18 16:14:02 +0530702#define MAX_PROP_SIZE 32
703static int sdhci_msm_dt_parse_vreg_info(struct device *dev,
704 struct sdhci_msm_reg_data **vreg_data, const char *vreg_name)
705{
706 int len, ret = 0;
707 const __be32 *prop;
708 char prop_name[MAX_PROP_SIZE];
709 struct sdhci_msm_reg_data *vreg;
710 struct device_node *np = dev->of_node;
711
712 snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", vreg_name);
713 if (!of_parse_phandle(np, prop_name, 0)) {
714 dev_err(dev, "No vreg data found for %s\n", vreg_name);
715 ret = -EINVAL;
716 return ret;
717 }
718
719 vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
720 if (!vreg) {
721 dev_err(dev, "No memory for vreg: %s\n", vreg_name);
722 ret = -ENOMEM;
723 return ret;
724 }
725
726 vreg->name = vreg_name;
727
728 snprintf(prop_name, MAX_PROP_SIZE,
729 "qcom,%s-always-on", vreg_name);
730 if (of_get_property(np, prop_name, NULL))
731 vreg->is_always_on = true;
732
733 snprintf(prop_name, MAX_PROP_SIZE,
734 "qcom,%s-lpm-sup", vreg_name);
735 if (of_get_property(np, prop_name, NULL))
736 vreg->lpm_sup = true;
737
738 snprintf(prop_name, MAX_PROP_SIZE,
739 "qcom,%s-voltage-level", vreg_name);
740 prop = of_get_property(np, prop_name, &len);
741 if (!prop || (len != (2 * sizeof(__be32)))) {
742 dev_warn(dev, "%s %s property\n",
743 prop ? "invalid format" : "no", prop_name);
744 } else {
745 vreg->low_vol_level = be32_to_cpup(&prop[0]);
746 vreg->high_vol_level = be32_to_cpup(&prop[1]);
747 }
748
749 snprintf(prop_name, MAX_PROP_SIZE,
750 "qcom,%s-current-level", vreg_name);
751 prop = of_get_property(np, prop_name, &len);
752 if (!prop || (len != (2 * sizeof(__be32)))) {
753 dev_warn(dev, "%s %s property\n",
754 prop ? "invalid format" : "no", prop_name);
755 } else {
756 vreg->lpm_uA = be32_to_cpup(&prop[0]);
757 vreg->hpm_uA = be32_to_cpup(&prop[1]);
758 }
759
760 *vreg_data = vreg;
761 dev_dbg(dev, "%s: %s %s vol=[%d %d]uV, curr=[%d %d]uA\n",
762 vreg->name, vreg->is_always_on ? "always_on," : "",
763 vreg->lpm_sup ? "lpm_sup," : "", vreg->low_vol_level,
764 vreg->high_vol_level, vreg->lpm_uA, vreg->hpm_uA);
765
766 return ret;
767}
768
769#define GPIO_NAME_MAX_LEN 32
770static int sdhci_msm_dt_parse_gpio_info(struct device *dev,
771 struct sdhci_msm_pltfm_data *pdata)
772{
773 int ret = 0, cnt, i;
774 struct sdhci_msm_pin_data *pin_data;
775 struct device_node *np = dev->of_node;
776
777 pin_data = devm_kzalloc(dev, sizeof(*pin_data), GFP_KERNEL);
778 if (!pin_data) {
779 dev_err(dev, "No memory for pin_data\n");
780 ret = -ENOMEM;
781 goto out;
782 }
783
784 cnt = of_gpio_count(np);
785 if (cnt > 0) {
786 pin_data->gpio_data = devm_kzalloc(dev,
787 sizeof(struct sdhci_msm_gpio_data), GFP_KERNEL);
788 if (!pin_data->gpio_data) {
789 dev_err(dev, "No memory for gpio_data\n");
790 ret = -ENOMEM;
791 goto out;
792 }
793 pin_data->gpio_data->size = cnt;
794 pin_data->gpio_data->gpio = devm_kzalloc(dev, cnt *
795 sizeof(struct sdhci_msm_gpio), GFP_KERNEL);
796
797 if (!pin_data->gpio_data->gpio) {
798 dev_err(dev, "No memory for gpio\n");
799 ret = -ENOMEM;
800 goto out;
801 }
802
803 for (i = 0; i < cnt; i++) {
804 const char *name = NULL;
805 char result[GPIO_NAME_MAX_LEN];
806 pin_data->gpio_data->gpio[i].no = of_get_gpio(np, i);
807 of_property_read_string_index(np,
808 "qcom,gpio-names", i, &name);
809
810 snprintf(result, GPIO_NAME_MAX_LEN, "%s-%s",
811 dev_name(dev), name ? name : "?");
812 pin_data->gpio_data->gpio[i].name = result;
813 dev_dbg(dev, "%s: gpio[%s] = %d\n", __func__,
814 pin_data->gpio_data->gpio[i].name,
815 pin_data->gpio_data->gpio[i].no);
816 pdata->pin_data = pin_data;
817 }
818 }
819
820out:
821 if (ret)
822 dev_err(dev, "%s failed with err %d\n", __func__, ret);
823 return ret;
824}
825
826/* Parse platform data */
827static struct sdhci_msm_pltfm_data *sdhci_msm_populate_pdata(struct device *dev)
828{
829 struct sdhci_msm_pltfm_data *pdata = NULL;
830 struct device_node *np = dev->of_node;
831 u32 bus_width = 0;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +0530832 u32 cpu_dma_latency;
Asutosh Das0ef24812012-12-18 16:14:02 +0530833 int len, i;
834
835 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
836 if (!pdata) {
837 dev_err(dev, "failed to allocate memory for platform data\n");
838 goto out;
839 }
840
Sahitya Tummala581df132013-03-12 14:57:46 +0530841 pdata->status_gpio = of_get_named_gpio_flags(np, "cd-gpios", 0, 0);
842
Asutosh Das0ef24812012-12-18 16:14:02 +0530843 of_property_read_u32(np, "qcom,bus-width", &bus_width);
844 if (bus_width == 8)
845 pdata->mmc_bus_width = MMC_CAP_8_BIT_DATA;
846 else if (bus_width == 4)
847 pdata->mmc_bus_width = MMC_CAP_4_BIT_DATA;
848 else {
849 dev_notice(dev, "invalid bus-width, default to 1-bit mode\n");
850 pdata->mmc_bus_width = 0;
851 }
852
Sahitya Tummalac6f48d42013-03-10 07:03:17 +0530853 if (!of_property_read_u32(np, "qcom,cpu-dma-latency-us",
854 &cpu_dma_latency))
855 pdata->cpu_dma_latency_us = cpu_dma_latency;
856
Asutosh Das0ef24812012-12-18 16:14:02 +0530857 pdata->vreg_data = devm_kzalloc(dev, sizeof(struct
858 sdhci_msm_slot_reg_data),
859 GFP_KERNEL);
860 if (!pdata->vreg_data) {
861 dev_err(dev, "failed to allocate memory for vreg data\n");
862 goto out;
863 }
864
865 if (sdhci_msm_dt_parse_vreg_info(dev, &pdata->vreg_data->vdd_data,
866 "vdd")) {
867 dev_err(dev, "failed parsing vdd data\n");
868 goto out;
869 }
870 if (sdhci_msm_dt_parse_vreg_info(dev,
871 &pdata->vreg_data->vdd_io_data,
872 "vdd-io")) {
873 dev_err(dev, "failed parsing vdd-io data\n");
874 goto out;
875 }
876
877 if (sdhci_msm_dt_parse_gpio_info(dev, pdata)) {
878 dev_err(dev, "failed parsing gpio data\n");
879 goto out;
880 }
881
882 of_property_read_u32(np, "qcom,max-clk-rate", &pdata->max_clk);
883
884 len = of_property_count_strings(np, "qcom,bus-speed-mode");
885
886 for (i = 0; i < len; i++) {
887 const char *name = NULL;
888
889 of_property_read_string_index(np,
890 "qcom,bus-speed-mode", i, &name);
891 if (!name)
892 continue;
893
894 if (!strncmp(name, "HS200_1p8v", sizeof("HS200_1p8v")))
895 pdata->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
896 else if (!strncmp(name, "HS200_1p2v", sizeof("HS200_1p2v")))
897 pdata->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
898 else if (!strncmp(name, "DDR_1p8v", sizeof("DDR_1p8v")))
899 pdata->caps |= MMC_CAP_1_8V_DDR
900 | MMC_CAP_UHS_DDR50;
901 else if (!strncmp(name, "DDR_1p2v", sizeof("DDR_1p2v")))
902 pdata->caps |= MMC_CAP_1_2V_DDR
903 | MMC_CAP_UHS_DDR50;
904 }
905
906 if (of_get_property(np, "qcom,nonremovable", NULL))
907 pdata->nonremovable = true;
908
909 return pdata;
910out:
911 return NULL;
912}
913
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530914/* Returns required bandwidth in Bytes per Sec */
915static unsigned int sdhci_get_bw_required(struct sdhci_host *host,
916 struct mmc_ios *ios)
917{
918 unsigned int bw;
919
920 bw = host->clock;
921 /*
922 * For DDR mode, SDCC controller clock will be at
923 * the double rate than the actual clock that goes to card.
924 */
925 if (ios->bus_width == MMC_BUS_WIDTH_4)
926 bw /= 2;
927 else if (ios->bus_width == MMC_BUS_WIDTH_1)
928 bw /= 8;
929
930 return bw;
931}
932
933static int sdhci_msm_bus_get_vote_for_bw(struct sdhci_msm_host *host,
934 unsigned int bw)
935{
936 unsigned int *table = host->pdata->voting_data->bw_vecs;
937 unsigned int size = host->pdata->voting_data->bw_vecs_size;
938 int i;
939
940 if (host->msm_bus_vote.is_max_bw_needed && bw)
941 return host->msm_bus_vote.max_bw_vote;
942
943 for (i = 0; i < size; i++) {
944 if (bw <= table[i])
945 break;
946 }
947
948 if (i && (i == size))
949 i--;
950
951 return i;
952}
953
954/*
955 * This function must be called with host lock acquired.
956 * Caller of this function should also ensure that msm bus client
957 * handle is not null.
958 */
959static inline int sdhci_msm_bus_set_vote(struct sdhci_msm_host *msm_host,
960 int vote,
961 unsigned long flags)
962{
963 struct sdhci_host *host = platform_get_drvdata(msm_host->pdev);
964 int rc = 0;
965
966 if (vote != msm_host->msm_bus_vote.curr_vote) {
967 spin_unlock_irqrestore(&host->lock, flags);
968 rc = msm_bus_scale_client_update_request(
969 msm_host->msm_bus_vote.client_handle, vote);
970 spin_lock_irqsave(&host->lock, flags);
971 if (rc) {
972 pr_err("%s: msm_bus_scale_client_update_request() failed: bus_client_handle=0x%x, vote=%d, err=%d\n",
973 mmc_hostname(host->mmc),
974 msm_host->msm_bus_vote.client_handle, vote, rc);
975 goto out;
976 }
977 msm_host->msm_bus_vote.curr_vote = vote;
978 }
979out:
980 return rc;
981}
982
983/*
984 * Internal work. Work to set 0 bandwidth for msm bus.
985 */
986static void sdhci_msm_bus_work(struct work_struct *work)
987{
988 struct sdhci_msm_host *msm_host;
989 struct sdhci_host *host;
990 unsigned long flags;
991
992 msm_host = container_of(work, struct sdhci_msm_host,
993 msm_bus_vote.vote_work.work);
994 host = platform_get_drvdata(msm_host->pdev);
995
996 if (!msm_host->msm_bus_vote.client_handle)
997 return;
998
999 spin_lock_irqsave(&host->lock, flags);
1000 /* don't vote for 0 bandwidth if any request is in progress */
1001 if (!host->mrq) {
1002 sdhci_msm_bus_set_vote(msm_host,
1003 msm_host->msm_bus_vote.min_bw_vote, flags);
1004 } else
1005 pr_warning("%s: %s: Transfer in progress. skipping bus voting to 0 bandwidth\n",
1006 mmc_hostname(host->mmc), __func__);
1007 spin_unlock_irqrestore(&host->lock, flags);
1008}
1009
1010/*
1011 * This function cancels any scheduled delayed work and sets the bus
1012 * vote based on bw (bandwidth) argument.
1013 */
1014static void sdhci_msm_bus_cancel_work_and_set_vote(struct sdhci_host *host,
1015 unsigned int bw)
1016{
1017 int vote;
1018 unsigned long flags;
1019 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1020 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1021
1022 cancel_delayed_work_sync(&msm_host->msm_bus_vote.vote_work);
1023 spin_lock_irqsave(&host->lock, flags);
1024 vote = sdhci_msm_bus_get_vote_for_bw(msm_host, bw);
1025 sdhci_msm_bus_set_vote(msm_host, vote, flags);
1026 spin_unlock_irqrestore(&host->lock, flags);
1027}
1028
1029#define MSM_MMC_BUS_VOTING_DELAY 200 /* msecs */
1030
1031/* This function queues a work which will set the bandwidth requiement to 0 */
1032static void sdhci_msm_bus_queue_work(struct sdhci_host *host)
1033{
1034 unsigned long flags;
1035 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1036 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1037
1038 spin_lock_irqsave(&host->lock, flags);
1039 if (msm_host->msm_bus_vote.min_bw_vote !=
1040 msm_host->msm_bus_vote.curr_vote)
1041 queue_delayed_work(system_wq,
1042 &msm_host->msm_bus_vote.vote_work,
1043 msecs_to_jiffies(MSM_MMC_BUS_VOTING_DELAY));
1044 spin_unlock_irqrestore(&host->lock, flags);
1045}
1046
1047static int sdhci_msm_bus_register(struct sdhci_msm_host *host,
1048 struct platform_device *pdev)
1049{
1050 int rc = 0;
1051 struct msm_bus_scale_pdata *bus_pdata;
1052
1053 struct sdhci_msm_bus_voting_data *data;
1054 struct device *dev = &pdev->dev;
1055
1056 data = devm_kzalloc(dev,
1057 sizeof(struct sdhci_msm_bus_voting_data), GFP_KERNEL);
1058 if (!data) {
1059 dev_err(&pdev->dev,
1060 "%s: failed to allocate memory\n", __func__);
1061 rc = -ENOMEM;
1062 goto out;
1063 }
1064 data->bus_pdata = msm_bus_cl_get_pdata(pdev);
1065 if (data->bus_pdata) {
1066 rc = sdhci_msm_dt_get_array(dev, "qcom,bus-bw-vectors-bps",
1067 &data->bw_vecs, &data->bw_vecs_size, 0);
1068 if (rc) {
1069 dev_err(&pdev->dev,
1070 "%s: Failed to get bus-bw-vectors-bps\n",
1071 __func__);
1072 goto out;
1073 }
1074 host->pdata->voting_data = data;
1075 }
1076 if (host->pdata->voting_data &&
1077 host->pdata->voting_data->bus_pdata &&
1078 host->pdata->voting_data->bw_vecs &&
1079 host->pdata->voting_data->bw_vecs_size) {
1080
1081 bus_pdata = host->pdata->voting_data->bus_pdata;
1082 host->msm_bus_vote.client_handle =
1083 msm_bus_scale_register_client(bus_pdata);
1084 if (!host->msm_bus_vote.client_handle) {
1085 dev_err(&pdev->dev, "msm_bus_scale_register_client()\n");
1086 rc = -EFAULT;
1087 goto out;
1088 }
1089 /* cache the vote index for minimum and maximum bandwidth */
1090 host->msm_bus_vote.min_bw_vote =
1091 sdhci_msm_bus_get_vote_for_bw(host, 0);
1092 host->msm_bus_vote.max_bw_vote =
1093 sdhci_msm_bus_get_vote_for_bw(host, UINT_MAX);
1094 } else {
1095 devm_kfree(dev, data);
1096 }
1097
1098out:
1099 return rc;
1100}
1101
1102static void sdhci_msm_bus_unregister(struct sdhci_msm_host *host)
1103{
1104 if (host->msm_bus_vote.client_handle)
1105 msm_bus_scale_unregister_client(
1106 host->msm_bus_vote.client_handle);
1107}
1108
1109static void sdhci_msm_bus_voting(struct sdhci_host *host, u32 enable)
1110{
1111 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1112 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1113 struct mmc_ios *ios = &host->mmc->ios;
1114 unsigned int bw;
1115
1116 if (!msm_host->msm_bus_vote.client_handle)
1117 return;
1118
1119 bw = sdhci_get_bw_required(host, ios);
1120 if (enable)
1121 sdhci_msm_bus_cancel_work_and_set_vote(host, bw);
1122 else
1123 sdhci_msm_bus_queue_work(host);
1124}
1125
Asutosh Das0ef24812012-12-18 16:14:02 +05301126/* Regulator utility functions */
1127static int sdhci_msm_vreg_init_reg(struct device *dev,
1128 struct sdhci_msm_reg_data *vreg)
1129{
1130 int ret = 0;
1131
1132 /* check if regulator is already initialized? */
1133 if (vreg->reg)
1134 goto out;
1135
1136 /* Get the regulator handle */
1137 vreg->reg = devm_regulator_get(dev, vreg->name);
1138 if (IS_ERR(vreg->reg)) {
1139 ret = PTR_ERR(vreg->reg);
1140 pr_err("%s: devm_regulator_get(%s) failed. ret=%d\n",
1141 __func__, vreg->name, ret);
1142 goto out;
1143 }
1144
1145 /* sanity check */
1146 if (!vreg->high_vol_level || !vreg->hpm_uA) {
1147 pr_err("%s: %s invalid constraints specified\n",
1148 __func__, vreg->name);
1149 ret = -EINVAL;
1150 }
1151
1152out:
1153 return ret;
1154}
1155
1156static void sdhci_msm_vreg_deinit_reg(struct sdhci_msm_reg_data *vreg)
1157{
1158 if (vreg->reg)
1159 devm_regulator_put(vreg->reg);
1160}
1161
1162static int sdhci_msm_vreg_set_optimum_mode(struct sdhci_msm_reg_data
1163 *vreg, int uA_load)
1164{
1165 int ret = 0;
1166
1167 /*
1168 * regulators that do not support regulator_set_voltage also
1169 * do not support regulator_set_optimum_mode
1170 */
1171 if (vreg->set_voltage_sup) {
1172 ret = regulator_set_load(vreg->reg, uA_load);
1173 if (ret < 0)
1174 pr_err("%s: regulator_set_load(reg=%s,uA_load=%d) failed. ret=%d\n",
1175 __func__, vreg->name, uA_load, ret);
1176 else
1177 /*
1178 * regulator_set_load() can return non zero
1179 * value even for success case.
1180 */
1181 ret = 0;
1182 }
1183 return ret;
1184}
1185
1186static int sdhci_msm_vreg_set_voltage(struct sdhci_msm_reg_data *vreg,
1187 int min_uV, int max_uV)
1188{
1189 int ret = 0;
1190
1191 ret = regulator_set_voltage(vreg->reg, min_uV, max_uV);
1192 if (ret) {
1193 pr_err("%s: regulator_set_voltage(%s)failed. min_uV=%d,max_uV=%d,ret=%d\n",
1194 __func__, vreg->name, min_uV, max_uV, ret);
1195 }
1196
1197 return ret;
1198}
1199
1200static int sdhci_msm_vreg_enable(struct sdhci_msm_reg_data *vreg)
1201{
1202 int ret = 0;
1203
1204 /* Put regulator in HPM (high power mode) */
1205 ret = sdhci_msm_vreg_set_optimum_mode(vreg, vreg->hpm_uA);
1206 if (ret < 0)
1207 return ret;
1208
1209 if (!vreg->is_enabled) {
1210 /* Set voltage level */
1211 ret = sdhci_msm_vreg_set_voltage(vreg, vreg->high_vol_level,
1212 vreg->high_vol_level);
1213 if (ret)
1214 return ret;
1215 }
1216 ret = regulator_enable(vreg->reg);
1217 if (ret) {
1218 pr_err("%s: regulator_enable(%s) failed. ret=%d\n",
1219 __func__, vreg->name, ret);
1220 return ret;
1221 }
1222 vreg->is_enabled = true;
1223 return ret;
1224}
1225
1226static int sdhci_msm_vreg_disable(struct sdhci_msm_reg_data *vreg)
1227{
1228 int ret = 0;
1229
1230 /* Never disable regulator marked as always_on */
1231 if (vreg->is_enabled && !vreg->is_always_on) {
1232 ret = regulator_disable(vreg->reg);
1233 if (ret) {
1234 pr_err("%s: regulator_disable(%s) failed. ret=%d\n",
1235 __func__, vreg->name, ret);
1236 goto out;
1237 }
1238 vreg->is_enabled = false;
1239
1240 ret = sdhci_msm_vreg_set_optimum_mode(vreg, 0);
1241 if (ret < 0)
1242 goto out;
1243
1244 /* Set min. voltage level to 0 */
1245 ret = sdhci_msm_vreg_set_voltage(vreg, 0, vreg->high_vol_level);
1246 if (ret)
1247 goto out;
1248 } else if (vreg->is_enabled && vreg->is_always_on) {
1249 if (vreg->lpm_sup) {
1250 /* Put always_on regulator in LPM (low power mode) */
1251 ret = sdhci_msm_vreg_set_optimum_mode(vreg,
1252 vreg->lpm_uA);
1253 if (ret < 0)
1254 goto out;
1255 }
1256 }
1257out:
1258 return ret;
1259}
1260
1261static int sdhci_msm_setup_vreg(struct sdhci_msm_pltfm_data *pdata,
1262 bool enable, bool is_init)
1263{
1264 int ret = 0, i;
1265 struct sdhci_msm_slot_reg_data *curr_slot;
1266 struct sdhci_msm_reg_data *vreg_table[2];
1267
1268 curr_slot = pdata->vreg_data;
1269 if (!curr_slot) {
1270 pr_debug("%s: vreg info unavailable,assuming the slot is powered by always on domain\n",
1271 __func__);
1272 goto out;
1273 }
1274
1275 vreg_table[0] = curr_slot->vdd_data;
1276 vreg_table[1] = curr_slot->vdd_io_data;
1277
1278 for (i = 0; i < ARRAY_SIZE(vreg_table); i++) {
1279 if (vreg_table[i]) {
1280 if (enable)
1281 ret = sdhci_msm_vreg_enable(vreg_table[i]);
1282 else
1283 ret = sdhci_msm_vreg_disable(vreg_table[i]);
1284 if (ret)
1285 goto out;
1286 }
1287 }
1288out:
1289 return ret;
1290}
1291
1292/*
1293 * Reset vreg by ensuring it is off during probe. A call
1294 * to enable vreg is needed to balance disable vreg
1295 */
1296static int sdhci_msm_vreg_reset(struct sdhci_msm_pltfm_data *pdata)
1297{
1298 int ret;
1299
1300 ret = sdhci_msm_setup_vreg(pdata, 1, true);
1301 if (ret)
1302 return ret;
1303 ret = sdhci_msm_setup_vreg(pdata, 0, true);
1304 return ret;
1305}
1306
1307/* This init function should be called only once for each SDHC slot */
1308static int sdhci_msm_vreg_init(struct device *dev,
1309 struct sdhci_msm_pltfm_data *pdata,
1310 bool is_init)
1311{
1312 int ret = 0;
1313 struct sdhci_msm_slot_reg_data *curr_slot;
1314 struct sdhci_msm_reg_data *curr_vdd_reg, *curr_vdd_io_reg;
1315
1316 curr_slot = pdata->vreg_data;
1317 if (!curr_slot)
1318 goto out;
1319
1320 curr_vdd_reg = curr_slot->vdd_data;
1321 curr_vdd_io_reg = curr_slot->vdd_io_data;
1322
1323 if (!is_init)
1324 /* Deregister all regulators from regulator framework */
1325 goto vdd_io_reg_deinit;
1326
1327 /*
1328 * Get the regulator handle from voltage regulator framework
1329 * and then try to set the voltage level for the regulator
1330 */
1331 if (curr_vdd_reg) {
1332 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_reg);
1333 if (ret)
1334 goto out;
1335 }
1336 if (curr_vdd_io_reg) {
1337 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_io_reg);
1338 if (ret)
1339 goto vdd_reg_deinit;
1340 }
1341 ret = sdhci_msm_vreg_reset(pdata);
1342 if (ret)
1343 dev_err(dev, "vreg reset failed (%d)\n", ret);
1344 goto out;
1345
1346vdd_io_reg_deinit:
1347 if (curr_vdd_io_reg)
1348 sdhci_msm_vreg_deinit_reg(curr_vdd_io_reg);
1349vdd_reg_deinit:
1350 if (curr_vdd_reg)
1351 sdhci_msm_vreg_deinit_reg(curr_vdd_reg);
1352out:
1353 return ret;
1354}
1355
1356
1357static int sdhci_msm_set_vdd_io_vol(struct sdhci_msm_pltfm_data *pdata,
1358 enum vdd_io_level level,
1359 unsigned int voltage_level)
1360{
1361 int ret = 0;
1362 int set_level;
1363 struct sdhci_msm_reg_data *vdd_io_reg;
1364
1365 if (!pdata->vreg_data)
1366 return ret;
1367
1368 vdd_io_reg = pdata->vreg_data->vdd_io_data;
1369 if (vdd_io_reg && vdd_io_reg->is_enabled) {
1370 switch (level) {
1371 case VDD_IO_LOW:
1372 set_level = vdd_io_reg->low_vol_level;
1373 break;
1374 case VDD_IO_HIGH:
1375 set_level = vdd_io_reg->high_vol_level;
1376 break;
1377 case VDD_IO_SET_LEVEL:
1378 set_level = voltage_level;
1379 break;
1380 default:
1381 pr_err("%s: invalid argument level = %d",
1382 __func__, level);
1383 ret = -EINVAL;
1384 return ret;
1385 }
1386 ret = sdhci_msm_vreg_set_voltage(vdd_io_reg, set_level,
1387 set_level);
1388 }
1389 return ret;
1390}
1391
1392static irqreturn_t sdhci_msm_pwr_irq(int irq, void *data)
1393{
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001394 struct sdhci_host *host = (struct sdhci_host *)data;
1395 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1396 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Das0ef24812012-12-18 16:14:02 +05301397 u8 irq_status = 0;
1398 u8 irq_ack = 0;
1399 int ret = 0;
1400
1401 irq_status = readb_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
1402 pr_debug("%s: Received IRQ(%d), status=0x%x\n",
1403 mmc_hostname(msm_host->mmc), irq, irq_status);
1404
1405 /* Clear the interrupt */
1406 writeb_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
1407 /*
1408 * SDHC has core_mem and hc_mem device memory and these memory
1409 * addresses do not fall within 1KB region. Hence, any update to
1410 * core_mem address space would require an mb() to ensure this gets
1411 * completed before its next update to registers within hc_mem.
1412 */
1413 mb();
1414
1415 /* Handle BUS ON/OFF*/
1416 if (irq_status & CORE_PWRCTL_BUS_ON) {
1417 ret = sdhci_msm_setup_vreg(msm_host->pdata, true, false);
1418 if (!ret)
1419 ret = sdhci_msm_setup_pins(msm_host->pdata, true);
1420 if (ret)
1421 irq_ack |= CORE_PWRCTL_BUS_FAIL;
1422 else
1423 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
1424 }
1425 if (irq_status & CORE_PWRCTL_BUS_OFF) {
1426 ret = sdhci_msm_setup_vreg(msm_host->pdata, false, false);
1427 if (!ret)
1428 ret = sdhci_msm_setup_pins(msm_host->pdata, false);
1429 if (ret)
1430 irq_ack |= CORE_PWRCTL_BUS_FAIL;
1431 else
1432 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
1433 }
1434 /* Handle IO LOW/HIGH */
1435 if (irq_status & CORE_PWRCTL_IO_LOW) {
1436 /* Switch voltage Low */
1437 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_LOW, 0);
1438 if (ret)
1439 irq_ack |= CORE_PWRCTL_IO_FAIL;
1440 else
1441 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
1442 }
1443 if (irq_status & CORE_PWRCTL_IO_HIGH) {
1444 /* Switch voltage High */
1445 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_HIGH, 0);
1446 if (ret)
1447 irq_ack |= CORE_PWRCTL_IO_FAIL;
1448 else
1449 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
1450 }
1451
1452 /* ACK status to the core */
1453 writeb_relaxed(irq_ack, (msm_host->core_mem + CORE_PWRCTL_CTL));
1454 /*
1455 * SDHC has core_mem and hc_mem device memory and these memory
1456 * addresses do not fall within 1KB region. Hence, any update to
1457 * core_mem address space would require an mb() to ensure this gets
1458 * completed before its next update to registers within hc_mem.
1459 */
1460 mb();
1461
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001462 if (irq_status & CORE_PWRCTL_IO_HIGH)
1463 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
1464 ~CORE_IO_PAD_PWR_SWITCH),
1465 host->ioaddr + CORE_VENDOR_SPEC);
1466 if (irq_status & CORE_PWRCTL_IO_LOW)
1467 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
1468 CORE_IO_PAD_PWR_SWITCH),
1469 host->ioaddr + CORE_VENDOR_SPEC);
1470 mb();
1471
Asutosh Das0ef24812012-12-18 16:14:02 +05301472 pr_debug("%s: Handled IRQ(%d), ret=%d, ack=0x%x\n",
1473 mmc_hostname(msm_host->mmc), irq, ret, irq_ack);
1474 wake_up_interruptible(&msm_host->pwr_irq_wait);
1475 return IRQ_HANDLED;
1476}
1477
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301478static ssize_t
1479show_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
1480 char *buf)
1481{
1482 struct sdhci_host *host = dev_get_drvdata(dev);
1483 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1484 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1485
1486 return snprintf(buf, PAGE_SIZE, "%u\n",
1487 msm_host->msm_bus_vote.is_max_bw_needed);
1488}
1489
1490static ssize_t
1491store_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
1492 const char *buf, size_t count)
1493{
1494 struct sdhci_host *host = dev_get_drvdata(dev);
1495 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1496 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1497 uint32_t value;
1498 unsigned long flags;
1499
1500 if (!kstrtou32(buf, 0, &value)) {
1501 spin_lock_irqsave(&host->lock, flags);
1502 msm_host->msm_bus_vote.is_max_bw_needed = !!value;
1503 spin_unlock_irqrestore(&host->lock, flags);
1504 }
1505 return count;
1506}
1507
Asutosh Das0ef24812012-12-18 16:14:02 +05301508static void sdhci_msm_check_power_status(struct sdhci_host *host)
1509{
1510 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1511 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1512 int ret = 0;
1513
1514 pr_debug("%s: %s: power status before waiting 0x%x\n",
1515 mmc_hostname(host->mmc), __func__,
1516 readb_relaxed(msm_host->core_mem + CORE_PWRCTL_CTL));
1517
1518 ret = wait_event_interruptible(msm_host->pwr_irq_wait,
1519 (readb_relaxed(msm_host->core_mem +
1520 CORE_PWRCTL_CTL)) != 0x0);
1521 if (ret)
1522 pr_warning("%s: %s: returned due to error %d\n",
1523 mmc_hostname(host->mmc), __func__, ret);
1524 pr_debug("%s: %s: ret %d power status after handling power IRQ 0x%x\n",
1525 mmc_hostname(host->mmc), __func__, ret,
1526 readb_relaxed(msm_host->core_mem + CORE_PWRCTL_CTL));
1527}
1528
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001529static void sdhci_msm_toggle_cdr(struct sdhci_host *host, bool enable)
1530{
1531 if (enable)
1532 writel_relaxed((readl_relaxed(host->ioaddr +
1533 CORE_DLL_CONFIG) | CORE_CDR_EN),
1534 host->ioaddr + CORE_DLL_CONFIG);
1535 else
1536 writel_relaxed((readl_relaxed(host->ioaddr +
1537 CORE_DLL_CONFIG) & ~CORE_CDR_EN),
1538 host->ioaddr + CORE_DLL_CONFIG);
1539}
1540
Asutosh Das648f9d12013-01-10 21:11:04 +05301541static unsigned int sdhci_msm_max_segs(void)
1542{
1543 return SDHCI_MSM_MAX_SEGMENTS;
1544}
1545
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301546void sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock)
1547{
1548 int rc;
1549 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1550 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1551 unsigned long flags;
1552
1553 if (clock && !atomic_read(&msm_host->clks_on)) {
1554 pr_debug("%s: request to enable clock at rate %u\n",
1555 mmc_hostname(host->mmc), clock);
1556 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
1557 rc = clk_prepare_enable(msm_host->bus_clk);
1558 if (rc) {
1559 pr_err("%s: %s: failed to enable the bus-clock with error %d\n",
1560 mmc_hostname(host->mmc), __func__, rc);
1561 goto out;
1562 }
1563 }
1564 if (!IS_ERR(msm_host->pclk)) {
1565 rc = clk_prepare_enable(msm_host->pclk);
1566 if (rc) {
1567 pr_err("%s: %s: failed to enable the pclk with error %d\n",
1568 mmc_hostname(host->mmc), __func__, rc);
1569 goto disable_bus_clk;
1570 }
1571 }
1572 rc = clk_prepare_enable(msm_host->clk);
1573 if (rc) {
1574 pr_err("%s: %s: failed to enable the host-clk with error %d\n",
1575 mmc_hostname(host->mmc), __func__, rc);
1576 goto disable_pclk;
1577 }
1578 mb();
1579 atomic_set(&msm_host->clks_on, 1);
1580
1581 } else if (!clock && atomic_read(&msm_host->clks_on)) {
1582 pr_debug("%s: request to disable clocks\n",
1583 mmc_hostname(host->mmc));
1584 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
1585 mb();
1586 clk_disable_unprepare(msm_host->clk);
1587 if (!IS_ERR(msm_host->pclk))
1588 clk_disable_unprepare(msm_host->pclk);
1589 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
1590 clk_disable_unprepare(msm_host->bus_clk);
1591 atomic_set(&msm_host->clks_on, 0);
1592 }
1593 spin_lock_irqsave(&host->lock, flags);
1594 host->clock = clock;
1595 spin_unlock_irqrestore(&host->lock, flags);
1596 goto out;
1597disable_pclk:
1598 if (!IS_ERR_OR_NULL(msm_host->pclk))
1599 clk_disable_unprepare(msm_host->pclk);
1600disable_bus_clk:
1601 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
1602 clk_disable_unprepare(msm_host->bus_clk);
1603out:
1604 return;
1605}
1606
Sahitya Tummala14613432013-03-21 11:13:25 +05301607static void sdhci_msm_set_uhs_signaling(struct sdhci_host *host,
1608 unsigned int uhs)
1609{
1610 u16 ctrl_2;
1611
1612 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1613 /* Select Bus Speed Mode for host */
1614 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1615 if (uhs == MMC_TIMING_MMC_HS200)
1616 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1617 else if (uhs == MMC_TIMING_UHS_SDR12)
1618 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1619 else if (uhs == MMC_TIMING_UHS_SDR25)
1620 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1621 else if (uhs == MMC_TIMING_UHS_SDR50)
1622 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1623 else if (uhs == MMC_TIMING_UHS_SDR104)
1624 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1625 else if (uhs == MMC_TIMING_UHS_DDR50)
1626 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1627 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1628
1629}
1630
Asutosh Das0ef24812012-12-18 16:14:02 +05301631static struct sdhci_ops sdhci_msm_ops = {
Sahitya Tummala14613432013-03-21 11:13:25 +05301632 .set_uhs_signaling = sdhci_msm_set_uhs_signaling,
Asutosh Das0ef24812012-12-18 16:14:02 +05301633 .check_power_status = sdhci_msm_check_power_status,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001634 .platform_execute_tuning = sdhci_msm_execute_tuning,
1635 .toggle_cdr = sdhci_msm_toggle_cdr,
Asutosh Das648f9d12013-01-10 21:11:04 +05301636 .get_max_segments = sdhci_msm_max_segs,
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301637 .set_clock = sdhci_msm_set_clock,
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301638 .platform_bus_voting = sdhci_msm_bus_voting,
Asutosh Das0ef24812012-12-18 16:14:02 +05301639};
1640
1641static int sdhci_msm_probe(struct platform_device *pdev)
1642{
1643 struct sdhci_host *host;
1644 struct sdhci_pltfm_host *pltfm_host;
1645 struct sdhci_msm_host *msm_host;
1646 struct resource *core_memres = NULL;
1647 int ret = 0, pwr_irq = 0, dead = 0;
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07001648 u32 host_version;
Asutosh Das0ef24812012-12-18 16:14:02 +05301649
1650 pr_debug("%s: Enter %s\n", dev_name(&pdev->dev), __func__);
1651 msm_host = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_msm_host),
1652 GFP_KERNEL);
1653 if (!msm_host) {
1654 ret = -ENOMEM;
1655 goto out;
1656 }
1657 init_waitqueue_head(&msm_host->pwr_irq_wait);
1658
1659 msm_host->sdhci_msm_pdata.ops = &sdhci_msm_ops;
1660 host = sdhci_pltfm_init(pdev, &msm_host->sdhci_msm_pdata, 0);
1661 if (IS_ERR(host)) {
1662 ret = PTR_ERR(host);
1663 goto out;
1664 }
1665
1666 pltfm_host = sdhci_priv(host);
1667 pltfm_host->priv = msm_host;
1668 msm_host->mmc = host->mmc;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301669 msm_host->pdev = pdev;
Asutosh Das0ef24812012-12-18 16:14:02 +05301670
1671 /* Extract platform data */
1672 if (pdev->dev.of_node) {
1673 msm_host->pdata = sdhci_msm_populate_pdata(&pdev->dev);
1674 if (!msm_host->pdata) {
1675 dev_err(&pdev->dev, "DT parsing error\n");
1676 goto pltfm_free;
1677 }
1678 } else {
1679 dev_err(&pdev->dev, "No device tree node\n");
1680 goto pltfm_free;
1681 }
1682
1683 /* Setup Clocks */
1684
1685 /* Setup SDCC bus voter clock. */
1686 msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus_clk");
1687 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
1688 /* Vote for max. clk rate for max. performance */
1689 ret = clk_set_rate(msm_host->bus_clk, INT_MAX);
1690 if (ret)
1691 goto pltfm_free;
1692 ret = clk_prepare_enable(msm_host->bus_clk);
1693 if (ret)
1694 goto pltfm_free;
1695 }
1696
1697 /* Setup main peripheral bus clock */
1698 msm_host->pclk = devm_clk_get(&pdev->dev, "iface_clk");
1699 if (!IS_ERR(msm_host->pclk)) {
1700 ret = clk_prepare_enable(msm_host->pclk);
1701 if (ret)
1702 goto bus_clk_disable;
1703 }
1704
1705 /* Setup SDC MMC clock */
1706 msm_host->clk = devm_clk_get(&pdev->dev, "core_clk");
1707 if (IS_ERR(msm_host->clk)) {
1708 ret = PTR_ERR(msm_host->clk);
1709 goto pclk_disable;
1710 }
1711
1712 ret = clk_prepare_enable(msm_host->clk);
1713 if (ret)
1714 goto pclk_disable;
1715
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301716 atomic_set(&msm_host->clks_on, 1);
Asutosh Das0ef24812012-12-18 16:14:02 +05301717 /* Setup regulators */
1718 ret = sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, true);
1719 if (ret) {
1720 dev_err(&pdev->dev, "Regulator setup failed (%d)\n", ret);
1721 goto clk_disable;
1722 }
1723
1724 /* Reset the core and Enable SDHC mode */
1725 core_memres = platform_get_resource_byname(pdev,
1726 IORESOURCE_MEM, "core_mem");
1727 msm_host->core_mem = devm_ioremap(&pdev->dev, core_memres->start,
1728 resource_size(core_memres));
1729
1730 if (!msm_host->core_mem) {
1731 dev_err(&pdev->dev, "Failed to remap registers\n");
1732 ret = -ENOMEM;
1733 goto vreg_deinit;
1734 }
1735
1736 /* Set SW_RST bit in POWER register (Offset 0x0) */
1737 writel_relaxed(CORE_SW_RST, msm_host->core_mem + CORE_POWER);
1738 /* Set HC_MODE_EN bit in HC_MODE register */
1739 writel_relaxed(HC_MODE_EN, (msm_host->core_mem + CORE_HC_MODE));
1740
1741 /*
1742 * Following are the deviations from SDHC spec v3.0 -
1743 * 1. Card detection is handled using separate GPIO.
1744 * 2. Bus power control is handled by interacting with PMIC.
1745 */
1746 host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
1747 host->quirks |= SDHCI_QUIRK_SINGLE_POWER_WRITE;
1748
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07001749 host_version = readl_relaxed((host->ioaddr + SDHCI_HOST_VERSION));
1750 dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n",
1751 host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >>
1752 SDHCI_VENDOR_VER_SHIFT));
1753 if (((host_version & SDHCI_VENDOR_VER_MASK) >>
1754 SDHCI_VENDOR_VER_SHIFT) == SDHCI_VER_100) {
1755 /*
1756 * Add 40us delay in interrupt handler when
1757 * operating at initialization frequency(400KHz).
1758 */
1759 host->quirks2 |= SDHCI_QUIRK2_SLOW_INT_CLR;
1760 /*
1761 * Set Software Reset for DAT line in Software
1762 * Reset Register (Bit 2).
1763 */
1764 host->quirks2 |= SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT;
1765 }
1766
1767 /* Setup PWRCTL irq */
Asutosh Das0ef24812012-12-18 16:14:02 +05301768 pwr_irq = platform_get_irq_byname(pdev, "pwr_irq");
1769 if (pwr_irq < 0) {
1770 dev_err(&pdev->dev, "Failed to get pwr_irq by name (%d)\n",
1771 pwr_irq);
1772 goto vreg_deinit;
1773 }
1774 ret = devm_request_threaded_irq(&pdev->dev, pwr_irq, NULL,
1775 sdhci_msm_pwr_irq, IRQF_ONESHOT,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001776 dev_name(&pdev->dev), host);
Asutosh Das0ef24812012-12-18 16:14:02 +05301777 if (ret) {
1778 dev_err(&pdev->dev, "Request threaded irq(%d) failed (%d)\n",
1779 pwr_irq, ret);
1780 goto vreg_deinit;
1781 }
1782
1783 /* Enable pwr irq interrupts */
1784 writel_relaxed(INT_MASK, (msm_host->core_mem + CORE_PWRCTL_MASK));
1785
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301786#ifdef CONFIG_MMC_CLKGATE
1787 /* Set clock gating delay to be used when CONFIG_MMC_CLKGATE is set */
1788 msm_host->mmc->clkgate_delay = SDHCI_MSM_MMC_CLK_GATE_DELAY;
1789#endif
1790
Asutosh Das0ef24812012-12-18 16:14:02 +05301791 /* Set host capabilities */
1792 msm_host->mmc->caps |= msm_host->pdata->mmc_bus_width;
1793 msm_host->mmc->caps |= msm_host->pdata->caps;
Sahitya Tummala7bd99062013-02-28 12:21:58 +05301794 msm_host->mmc->caps |= MMC_CAP_HW_RESET;
Asutosh Das0ef24812012-12-18 16:14:02 +05301795 msm_host->mmc->caps2 |= msm_host->pdata->caps2;
1796 msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR;
1797 msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR_CONTROL;
1798
1799 if (msm_host->pdata->nonremovable)
1800 msm_host->mmc->caps |= MMC_CAP_NONREMOVABLE;
1801
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05301802 host->cpu_dma_latency_us = msm_host->pdata->cpu_dma_latency_us;
1803
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301804 ret = sdhci_msm_bus_register(msm_host, pdev);
1805 if (ret)
1806 goto vreg_deinit;
1807
1808 if (msm_host->msm_bus_vote.client_handle)
1809 INIT_DELAYED_WORK(&msm_host->msm_bus_vote.vote_work,
1810 sdhci_msm_bus_work);
1811
Sahitya Tummala581df132013-03-12 14:57:46 +05301812 if (gpio_is_valid(msm_host->pdata->status_gpio)) {
1813 ret = mmc_gpio_request_cd(msm_host->mmc,
1814 msm_host->pdata->status_gpio, 0);
1815 if (ret) {
1816 dev_err(&pdev->dev, "%s: Failed to request card detection IRQ %d\n",
1817 __func__, ret);
1818 goto bus_unregister;
1819 }
1820 }
1821
Sahitya Tummalaeaa21862013-03-20 19:34:59 +05301822 if (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(32))) {
1823 host->dma_mask = DMA_BIT_MASK(32);
1824 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
1825 } else {
1826 dev_err(&pdev->dev, "%s: Failed to set dma mask\n", __func__);
1827 }
1828
Asutosh Das0ef24812012-12-18 16:14:02 +05301829 ret = sdhci_add_host(host);
1830 if (ret) {
1831 dev_err(&pdev->dev, "Add host failed (%d)\n", ret);
Sahitya Tummala581df132013-03-12 14:57:46 +05301832 goto vreg_deinit;
Asutosh Das0ef24812012-12-18 16:14:02 +05301833 }
1834
1835 /* Set core clk rate, optionally override from dts */
1836 if (msm_host->pdata->max_clk)
1837 host->max_clk = msm_host->pdata->max_clk;
1838 ret = clk_set_rate(msm_host->clk, host->max_clk);
1839 if (ret) {
1840 dev_err(&pdev->dev, "MClk rate set failed (%d)\n", ret);
1841 goto remove_host;
1842 }
1843
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301844 msm_host->msm_bus_vote.max_bus_bw.show = show_sdhci_max_bus_bw;
1845 msm_host->msm_bus_vote.max_bus_bw.store = store_sdhci_max_bus_bw;
1846 sysfs_attr_init(&msm_host->msm_bus_vote.max_bus_bw.attr);
1847 msm_host->msm_bus_vote.max_bus_bw.attr.name = "max_bus_bw";
1848 msm_host->msm_bus_vote.max_bus_bw.attr.mode = S_IRUGO | S_IWUSR;
1849 ret = device_create_file(&pdev->dev,
1850 &msm_host->msm_bus_vote.max_bus_bw);
1851 if (ret)
1852 goto remove_host;
1853
Asutosh Das0ef24812012-12-18 16:14:02 +05301854 /* Successful initialization */
1855 goto out;
1856
1857remove_host:
1858 dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
1859 sdhci_remove_host(host, dead);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301860bus_unregister:
1861 sdhci_msm_bus_unregister(msm_host);
Asutosh Das0ef24812012-12-18 16:14:02 +05301862vreg_deinit:
1863 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
1864clk_disable:
1865 if (!IS_ERR(msm_host->clk))
1866 clk_disable_unprepare(msm_host->clk);
1867pclk_disable:
1868 if (!IS_ERR(msm_host->pclk))
1869 clk_disable_unprepare(msm_host->pclk);
1870bus_clk_disable:
1871 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
1872 clk_disable_unprepare(msm_host->bus_clk);
1873pltfm_free:
1874 sdhci_pltfm_free(pdev);
1875out:
1876 pr_debug("%s: Exit %s\n", dev_name(&pdev->dev), __func__);
1877 return ret;
1878}
1879
1880static int sdhci_msm_remove(struct platform_device *pdev)
1881{
1882 struct sdhci_host *host = platform_get_drvdata(pdev);
1883 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1884 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1885 struct sdhci_msm_pltfm_data *pdata = msm_host->pdata;
1886 int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) ==
1887 0xffffffff);
1888
1889 pr_debug("%s: %s\n", dev_name(&pdev->dev), __func__);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301890 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Asutosh Das0ef24812012-12-18 16:14:02 +05301891 sdhci_remove_host(host, dead);
1892 sdhci_pltfm_free(pdev);
Sahitya Tummala581df132013-03-12 14:57:46 +05301893
Asutosh Das0ef24812012-12-18 16:14:02 +05301894 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301895
Asutosh Das0ef24812012-12-18 16:14:02 +05301896 if (pdata->pin_data)
1897 sdhci_msm_setup_gpio(pdata, false);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301898
1899 if (msm_host->msm_bus_vote.client_handle) {
1900 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
1901 sdhci_msm_bus_unregister(msm_host);
1902 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301903 return 0;
1904}
1905
1906static const struct of_device_id sdhci_msm_dt_match[] = {
1907 {.compatible = "qcom,sdhci-msm"},
1908};
1909MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match);
1910
1911static struct platform_driver sdhci_msm_driver = {
1912 .probe = sdhci_msm_probe,
1913 .remove = sdhci_msm_remove,
1914 .driver = {
1915 .name = "sdhci_msm",
1916 .owner = THIS_MODULE,
1917 .of_match_table = sdhci_msm_dt_match,
1918 },
1919};
1920
1921module_platform_driver(sdhci_msm_driver);
1922
1923MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Secure Digital Host Controller Interface driver");
1924MODULE_LICENSE("GPL v2");