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