blob: 2e27559d98daa2f41407aad0776348fc7d5854a0 [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 Tummala66b0fe32013-04-25 11:50:56 +053037#include <linux/iopoll.h>
Sahitya Tummala8a3e8182013-03-10 14:12:52 +053038#include <linux/msm-bus.h>
Asutosh Das0ef24812012-12-18 16:14:02 +053039
40#include "sdhci-pltfm.h"
41
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -070042#define SDHCI_VER_100 0x2B
Asutosh Das0ef24812012-12-18 16:14:02 +053043#define CORE_HC_MODE 0x78
44#define HC_MODE_EN 0x1
45
46#define CORE_POWER 0x0
47#define CORE_SW_RST (1 << 7)
48
49#define CORE_PWRCTL_STATUS 0xDC
50#define CORE_PWRCTL_MASK 0xE0
51#define CORE_PWRCTL_CLEAR 0xE4
52#define CORE_PWRCTL_CTL 0xE8
53
54#define CORE_PWRCTL_BUS_OFF 0x01
55#define CORE_PWRCTL_BUS_ON (1 << 1)
56#define CORE_PWRCTL_IO_LOW (1 << 2)
57#define CORE_PWRCTL_IO_HIGH (1 << 3)
58
59#define CORE_PWRCTL_BUS_SUCCESS 0x01
60#define CORE_PWRCTL_BUS_FAIL (1 << 1)
61#define CORE_PWRCTL_IO_SUCCESS (1 << 2)
62#define CORE_PWRCTL_IO_FAIL (1 << 3)
63
64#define INT_MASK 0xF
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -070065#define MAX_PHASES 16
66
67#define CORE_DLL_LOCK (1 << 7)
68#define CORE_DLL_EN (1 << 16)
69#define CORE_CDR_EN (1 << 17)
70#define CORE_CK_OUT_EN (1 << 18)
71#define CORE_CDR_EXT_EN (1 << 19)
72#define CORE_DLL_PDN (1 << 29)
73#define CORE_DLL_RST (1 << 30)
74#define CORE_DLL_CONFIG 0x100
75#define CORE_DLL_TEST_CTL 0x104
76#define CORE_DLL_STATUS 0x108
77
78#define CORE_VENDOR_SPEC 0x10C
79#define CORE_CLK_PWRSAVE (1 << 1)
80#define CORE_IO_PAD_PWR_SWITCH (1 << 16)
81
Asutosh Das648f9d12013-01-10 21:11:04 +053082/* 8KB descriptors */
83#define SDHCI_MSM_MAX_SEGMENTS (1 << 13)
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +053084#define SDHCI_MSM_MMC_CLK_GATE_DELAY 200 /* msecs */
Asutosh Das648f9d12013-01-10 21:11:04 +053085
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -070086static const u32 tuning_block_64[] = {
87 0x00FF0FFF, 0xCCC3CCFF, 0xFFCC3CC3, 0xEFFEFFFE,
88 0xDDFFDFFF, 0xFBFFFBFF, 0xFF7FFFBF, 0xEFBDF777,
89 0xF0FFF0FF, 0x3CCCFC0F, 0xCFCC33CC, 0xEEFFEFFF,
90 0xFDFFFDFF, 0xFFBFFFDF, 0xFFF7FFBB, 0xDE7B7FF7
91};
92
93static const u32 tuning_block_128[] = {
94 0xFF00FFFF, 0x0000FFFF, 0xCCCCFFFF, 0xCCCC33CC,
95 0xCC3333CC, 0xFFFFCCCC, 0xFFFFEEFF, 0xFFEEEEFF,
96 0xFFDDFFFF, 0xDDDDFFFF, 0xBBFFFFFF, 0xBBFFFFFF,
97 0xFFFFFFBB, 0xFFFFFF77, 0x77FF7777, 0xFFEEDDBB,
98 0x00FFFFFF, 0x00FFFFFF, 0xCCFFFF00, 0xCC33CCCC,
99 0x3333CCCC, 0xFFCCCCCC, 0xFFEEFFFF, 0xEEEEFFFF,
100 0xDDFFFFFF, 0xDDFFFFFF, 0xFFFFFFDD, 0xFFFFFFBB,
101 0xFFFFBBBB, 0xFFFF77FF, 0xFF7777FF, 0xEEDDBB77
102};
Asutosh Das0ef24812012-12-18 16:14:02 +0530103
Venkat Gopalakrishnan270580a2013-03-11 12:17:57 -0700104static int disable_slots;
105/* root can write, others read */
106module_param(disable_slots, int, S_IRUGO|S_IWUSR);
107
Asutosh Das0ef24812012-12-18 16:14:02 +0530108/* This structure keeps information per regulator */
109struct sdhci_msm_reg_data {
110 /* voltage regulator handle */
111 struct regulator *reg;
112 /* regulator name */
113 const char *name;
114 /* voltage level to be set */
115 u32 low_vol_level;
116 u32 high_vol_level;
117 /* Load values for low power and high power mode */
118 u32 lpm_uA;
119 u32 hpm_uA;
120
121 /* is this regulator enabled? */
122 bool is_enabled;
123 /* is this regulator needs to be always on? */
124 bool is_always_on;
125 /* is low power mode setting required for this regulator? */
126 bool lpm_sup;
127 bool set_voltage_sup;
128};
129
130/*
131 * This structure keeps information for all the
132 * regulators required for a SDCC slot.
133 */
134struct sdhci_msm_slot_reg_data {
135 /* keeps VDD/VCC regulator info */
136 struct sdhci_msm_reg_data *vdd_data;
137 /* keeps VDD IO regulator info */
138 struct sdhci_msm_reg_data *vdd_io_data;
139};
140
141struct sdhci_msm_gpio {
142 u32 no;
143 const char *name;
144 bool is_enabled;
145};
146
147struct sdhci_msm_gpio_data {
148 struct sdhci_msm_gpio *gpio;
149 u8 size;
150};
151
152struct sdhci_msm_pin_data {
153 /*
154 * = 1 if controller pins are using gpios
155 * = 0 if controller has dedicated MSM pads
156 */
157 bool cfg_sts;
158 struct sdhci_msm_gpio_data *gpio_data;
159};
160
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530161struct sdhci_msm_bus_voting_data {
162 struct msm_bus_scale_pdata *bus_pdata;
163 unsigned int *bw_vecs;
164 unsigned int bw_vecs_size;
165};
166
Asutosh Das0ef24812012-12-18 16:14:02 +0530167struct sdhci_msm_pltfm_data {
168 /* Supported UHS-I Modes */
169 u32 caps;
170
171 /* More capabilities */
172 u32 caps2;
173
174 unsigned long mmc_bus_width;
Asutosh Das0ef24812012-12-18 16:14:02 +0530175 struct sdhci_msm_slot_reg_data *vreg_data;
176 bool nonremovable;
177 struct sdhci_msm_pin_data *pin_data;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +0530178 u32 cpu_dma_latency_us;
Sahitya Tummala581df132013-03-12 14:57:46 +0530179 int status_gpio; /* card detection GPIO that is configured as IRQ */
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530180 struct sdhci_msm_bus_voting_data *voting_data;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530181 u32 *sup_clk_table;
182 unsigned char sup_clk_cnt;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530183};
184
185struct sdhci_msm_bus_vote {
186 uint32_t client_handle;
187 uint32_t curr_vote;
188 int min_bw_vote;
189 int max_bw_vote;
190 bool is_max_bw_needed;
191 struct delayed_work vote_work;
192 struct device_attribute max_bus_bw;
Asutosh Das0ef24812012-12-18 16:14:02 +0530193};
194
195struct sdhci_msm_host {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530196 struct platform_device *pdev;
Asutosh Das0ef24812012-12-18 16:14:02 +0530197 void __iomem *core_mem; /* MSM SDCC mapped address */
198 struct clk *clk; /* main SD/MMC bus clock */
199 struct clk *pclk; /* SDHC peripheral bus clock */
200 struct clk *bus_clk; /* SDHC bus voter clock */
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +0530201 atomic_t clks_on; /* Set if clocks are enabled */
Asutosh Das0ef24812012-12-18 16:14:02 +0530202 struct sdhci_msm_pltfm_data *pdata;
203 struct mmc_host *mmc;
204 struct sdhci_pltfm_data sdhci_msm_pdata;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +0530205 u32 curr_pwr_state;
206 u32 curr_io_level;
207 struct completion pwr_irq_completion;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530208 struct sdhci_msm_bus_vote msm_bus_vote;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530209 u32 clk_rate; /* Keeps track of current clock rate that is set */
Asutosh Das0ef24812012-12-18 16:14:02 +0530210};
211
212enum vdd_io_level {
213 /* set vdd_io_data->low_vol_level */
214 VDD_IO_LOW,
215 /* set vdd_io_data->high_vol_level */
216 VDD_IO_HIGH,
217 /*
218 * set whatever there in voltage_level (third argument) of
219 * sdhci_msm_set_vdd_io_vol() function.
220 */
221 VDD_IO_SET_LEVEL,
222};
223
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700224/* MSM platform specific tuning */
225static inline int msm_dll_poll_ck_out_en(struct sdhci_host *host,
226 u8 poll)
227{
228 int rc = 0;
229 u32 wait_cnt = 50;
230 u8 ck_out_en = 0;
231 struct mmc_host *mmc = host->mmc;
232
233 /* poll for CK_OUT_EN bit. max. poll time = 50us */
234 ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) &
235 CORE_CK_OUT_EN);
236
237 while (ck_out_en != poll) {
238 if (--wait_cnt == 0) {
239 pr_err("%s: %s: CK_OUT_EN bit is not %d\n",
240 mmc_hostname(mmc), __func__, poll);
241 rc = -ETIMEDOUT;
242 goto out;
243 }
244 udelay(1);
245
246 ck_out_en = !!(readl_relaxed(host->ioaddr +
247 CORE_DLL_CONFIG) & CORE_CK_OUT_EN);
248 }
249out:
250 return rc;
251}
252
253static int msm_config_cm_dll_phase(struct sdhci_host *host, u8 phase)
254{
255 int rc = 0;
256 u8 grey_coded_phase_table[] = {0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4,
257 0xC, 0xD, 0xF, 0xE, 0xA, 0xB, 0x9,
258 0x8};
259 unsigned long flags;
260 u32 config;
261 struct mmc_host *mmc = host->mmc;
262
263 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
264 spin_lock_irqsave(&host->lock, flags);
265
266 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
267 config &= ~(CORE_CDR_EN | CORE_CK_OUT_EN);
268 config |= (CORE_CDR_EXT_EN | CORE_DLL_EN);
269 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
270
271 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '0' */
272 rc = msm_dll_poll_ck_out_en(host, 0);
273 if (rc)
274 goto err_out;
275
276 /*
277 * Write the selected DLL clock output phase (0 ... 15)
278 * to CDR_SELEXT bit field of DLL_CONFIG register.
279 */
280 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
281 & ~(0xF << 20))
282 | (grey_coded_phase_table[phase] << 20)),
283 host->ioaddr + CORE_DLL_CONFIG);
284
285 /* Set CK_OUT_EN bit of DLL_CONFIG register to 1. */
286 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
287 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
288
289 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '1' */
290 rc = msm_dll_poll_ck_out_en(host, 1);
291 if (rc)
292 goto err_out;
293
294 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
295 config |= CORE_CDR_EN;
296 config &= ~CORE_CDR_EXT_EN;
297 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
298 goto out;
299
300err_out:
301 pr_err("%s: %s: Failed to set DLL phase: %d\n",
302 mmc_hostname(mmc), __func__, phase);
303out:
304 spin_unlock_irqrestore(&host->lock, flags);
305 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
306 return rc;
307}
308
309/*
310 * Find out the greatest range of consecuitive selected
311 * DLL clock output phases that can be used as sampling
312 * setting for SD3.0 UHS-I card read operation (in SDR104
313 * timing mode) or for eMMC4.5 card read operation (in HS200
314 * timing mode).
315 * Select the 3/4 of the range and configure the DLL with the
316 * selected DLL clock output phase.
317 */
318
319static int msm_find_most_appropriate_phase(struct sdhci_host *host,
320 u8 *phase_table, u8 total_phases)
321{
322 int ret;
323 u8 ranges[MAX_PHASES][MAX_PHASES] = { {0}, {0} };
324 u8 phases_per_row[MAX_PHASES] = {0};
325 int row_index = 0, col_index = 0, selected_row_index = 0, curr_max = 0;
326 int i, cnt, phase_0_raw_index = 0, phase_15_raw_index = 0;
327 bool phase_0_found = false, phase_15_found = false;
328 struct mmc_host *mmc = host->mmc;
329
330 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
331 if (!total_phases || (total_phases > MAX_PHASES)) {
332 pr_err("%s: %s: invalid argument: total_phases=%d\n",
333 mmc_hostname(mmc), __func__, total_phases);
334 return -EINVAL;
335 }
336
337 for (cnt = 0; cnt < total_phases; cnt++) {
338 ranges[row_index][col_index] = phase_table[cnt];
339 phases_per_row[row_index] += 1;
340 col_index++;
341
342 if ((cnt + 1) == total_phases) {
343 continue;
344 /* check if next phase in phase_table is consecutive or not */
345 } else if ((phase_table[cnt] + 1) != phase_table[cnt + 1]) {
346 row_index++;
347 col_index = 0;
348 }
349 }
350
351 if (row_index >= MAX_PHASES)
352 return -EINVAL;
353
354 /* Check if phase-0 is present in first valid window? */
355 if (!ranges[0][0]) {
356 phase_0_found = true;
357 phase_0_raw_index = 0;
358 /* Check if cycle exist between 2 valid windows */
359 for (cnt = 1; cnt <= row_index; cnt++) {
360 if (phases_per_row[cnt]) {
361 for (i = 0; i < phases_per_row[cnt]; i++) {
362 if (ranges[cnt][i] == 15) {
363 phase_15_found = true;
364 phase_15_raw_index = cnt;
365 break;
366 }
367 }
368 }
369 }
370 }
371
372 /* If 2 valid windows form cycle then merge them as single window */
373 if (phase_0_found && phase_15_found) {
374 /* number of phases in raw where phase 0 is present */
375 u8 phases_0 = phases_per_row[phase_0_raw_index];
376 /* number of phases in raw where phase 15 is present */
377 u8 phases_15 = phases_per_row[phase_15_raw_index];
378
379 if (phases_0 + phases_15 >= MAX_PHASES)
380 /*
381 * If there are more than 1 phase windows then total
382 * number of phases in both the windows should not be
383 * more than or equal to MAX_PHASES.
384 */
385 return -EINVAL;
386
387 /* Merge 2 cyclic windows */
388 i = phases_15;
389 for (cnt = 0; cnt < phases_0; cnt++) {
390 ranges[phase_15_raw_index][i] =
391 ranges[phase_0_raw_index][cnt];
392 if (++i >= MAX_PHASES)
393 break;
394 }
395
396 phases_per_row[phase_0_raw_index] = 0;
397 phases_per_row[phase_15_raw_index] = phases_15 + phases_0;
398 }
399
400 for (cnt = 0; cnt <= row_index; cnt++) {
401 if (phases_per_row[cnt] > curr_max) {
402 curr_max = phases_per_row[cnt];
403 selected_row_index = cnt;
404 }
405 }
406
407 i = ((curr_max * 3) / 4);
408 if (i)
409 i--;
410
411 ret = (int)ranges[selected_row_index][i];
412
413 if (ret >= MAX_PHASES) {
414 ret = -EINVAL;
415 pr_err("%s: %s: invalid phase selected=%d\n",
416 mmc_hostname(mmc), __func__, ret);
417 }
418
419 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
420 return ret;
421}
422
423static inline void msm_cm_dll_set_freq(struct sdhci_host *host)
424{
425 u32 mclk_freq = 0;
426
427 /* Program the MCLK value to MCLK_FREQ bit field */
428 if (host->clock <= 112000000)
429 mclk_freq = 0;
430 else if (host->clock <= 125000000)
431 mclk_freq = 1;
432 else if (host->clock <= 137000000)
433 mclk_freq = 2;
434 else if (host->clock <= 150000000)
435 mclk_freq = 3;
436 else if (host->clock <= 162000000)
437 mclk_freq = 4;
438 else if (host->clock <= 175000000)
439 mclk_freq = 5;
440 else if (host->clock <= 187000000)
441 mclk_freq = 6;
442 else if (host->clock <= 200000000)
443 mclk_freq = 7;
444
445 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
446 & ~(7 << 24)) | (mclk_freq << 24)),
447 host->ioaddr + CORE_DLL_CONFIG);
448}
449
450/* Initialize the DLL (Programmable Delay Line ) */
451static int msm_init_cm_dll(struct sdhci_host *host)
452{
453 struct mmc_host *mmc = host->mmc;
454 int rc = 0;
455 unsigned long flags;
456 u32 wait_cnt;
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530457 bool prev_pwrsave, curr_pwrsave;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700458
459 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
460 spin_lock_irqsave(&host->lock, flags);
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530461 prev_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
462 CORE_CLK_PWRSAVE);
463 curr_pwrsave = prev_pwrsave;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700464 /*
465 * Make sure that clock is always enabled when DLL
466 * tuning is in progress. Keeping PWRSAVE ON may
467 * turn off the clock. So let's disable the PWRSAVE
468 * here and re-enable it once tuning is completed.
469 */
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530470 if (prev_pwrsave) {
471 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
472 & ~CORE_CLK_PWRSAVE),
473 host->ioaddr + CORE_VENDOR_SPEC);
474 curr_pwrsave = false;
475 }
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700476
477 /* Write 1 to DLL_RST bit of DLL_CONFIG register */
478 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
479 | CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
480
481 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
482 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
483 | CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
484 msm_cm_dll_set_freq(host);
485
486 /* Write 0 to DLL_RST bit of DLL_CONFIG register */
487 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
488 & ~CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
489
490 /* Write 0 to DLL_PDN bit of DLL_CONFIG register */
491 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
492 & ~CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
493
494 /* Set DLL_EN bit to 1. */
495 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
496 | CORE_DLL_EN), host->ioaddr + CORE_DLL_CONFIG);
497
498 /* Set CK_OUT_EN bit to 1. */
499 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
500 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
501
502 wait_cnt = 50;
503 /* Wait until DLL_LOCK bit of DLL_STATUS register becomes '1' */
504 while (!(readl_relaxed(host->ioaddr + CORE_DLL_STATUS) &
505 CORE_DLL_LOCK)) {
506 /* max. wait for 50us sec for LOCK bit to be set */
507 if (--wait_cnt == 0) {
508 pr_err("%s: %s: DLL failed to LOCK\n",
509 mmc_hostname(mmc), __func__);
510 rc = -ETIMEDOUT;
511 goto out;
512 }
513 /* wait for 1us before polling again */
514 udelay(1);
515 }
516
517out:
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530518 /* Restore the correct PWRSAVE state */
519 if (prev_pwrsave ^ curr_pwrsave) {
520 u32 reg = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC);
521
522 if (prev_pwrsave)
523 reg |= CORE_CLK_PWRSAVE;
524 else
525 reg &= ~CORE_CLK_PWRSAVE;
526
527 writel_relaxed(reg, host->ioaddr + CORE_VENDOR_SPEC);
528 }
529
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700530 spin_unlock_irqrestore(&host->lock, flags);
531 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
532 return rc;
533}
534
535int sdhci_msm_execute_tuning(struct sdhci_host *host, u32 opcode)
536{
537 unsigned long flags;
538 u8 phase, *data_buf, tuned_phases[16], tuned_phase_cnt = 0;
539 const u32 *tuning_block_pattern = tuning_block_64;
540 int size = sizeof(tuning_block_64); /* Tuning pattern size in bytes */
541 int rc;
542 struct mmc_host *mmc = host->mmc;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530543 struct mmc_ios ios = host->mmc->ios;
544
545 /*
546 * Tuning is required for SDR104 and HS200 cards and if clock frequency
547 * is greater than 100MHz in these modes.
548 */
549 if (host->clock <= (100 * 1000 * 1000) ||
550 !(ios.timing == MMC_TIMING_MMC_HS200 ||
551 ios.timing == MMC_TIMING_UHS_SDR104))
552 return 0;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700553
554 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700555 spin_lock_irqsave(&host->lock, flags);
556
557 if ((opcode == MMC_SEND_TUNING_BLOCK_HS200) &&
558 (mmc->ios.bus_width == MMC_BUS_WIDTH_8)) {
559 tuning_block_pattern = tuning_block_128;
560 size = sizeof(tuning_block_128);
561 }
562 spin_unlock_irqrestore(&host->lock, flags);
563
564 /* first of all reset the tuning block */
565 rc = msm_init_cm_dll(host);
566 if (rc)
567 goto out;
568
569 data_buf = kmalloc(size, GFP_KERNEL);
570 if (!data_buf) {
571 rc = -ENOMEM;
572 goto out;
573 }
574
575 phase = 0;
576 do {
577 struct mmc_command cmd = {0};
578 struct mmc_data data = {0};
579 struct mmc_request mrq = {
580 .cmd = &cmd,
581 .data = &data
582 };
583 struct scatterlist sg;
584
585 /* set the phase in delay line hw block */
586 rc = msm_config_cm_dll_phase(host, phase);
587 if (rc)
588 goto kfree;
589
590 cmd.opcode = opcode;
591 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
592
593 data.blksz = size;
594 data.blocks = 1;
595 data.flags = MMC_DATA_READ;
596 data.timeout_ns = 1000 * 1000 * 1000; /* 1 sec */
597
598 data.sg = &sg;
599 data.sg_len = 1;
600 sg_init_one(&sg, data_buf, size);
601 memset(data_buf, 0, size);
602 mmc_wait_for_req(mmc, &mrq);
603
604 if (!cmd.error && !data.error &&
605 !memcmp(data_buf, tuning_block_pattern, size)) {
606 /* tuning is successful at this tuning point */
607 tuned_phases[tuned_phase_cnt++] = phase;
608 pr_debug("%s: %s: found good phase = %d\n",
609 mmc_hostname(mmc), __func__, phase);
610 }
611 } while (++phase < 16);
612
613 if (tuned_phase_cnt) {
614 rc = msm_find_most_appropriate_phase(host, tuned_phases,
615 tuned_phase_cnt);
616 if (rc < 0)
617 goto kfree;
618 else
619 phase = (u8)rc;
620
621 /*
622 * Finally set the selected phase in delay
623 * line hw block.
624 */
625 rc = msm_config_cm_dll_phase(host, phase);
626 if (rc)
627 goto kfree;
628 pr_debug("%s: %s: finally setting the tuning phase to %d\n",
629 mmc_hostname(mmc), __func__, phase);
630 } else {
631 /* tuning failed */
632 pr_err("%s: %s: no tuning point found\n",
633 mmc_hostname(mmc), __func__);
634 rc = -EAGAIN;
635 }
636
637kfree:
638 kfree(data_buf);
639out:
640 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
641 return rc;
642}
643
Asutosh Das0ef24812012-12-18 16:14:02 +0530644static int sdhci_msm_setup_gpio(struct sdhci_msm_pltfm_data *pdata, bool enable)
645{
646 struct sdhci_msm_gpio_data *curr;
647 int i, ret = 0;
648
649 curr = pdata->pin_data->gpio_data;
650 for (i = 0; i < curr->size; i++) {
651 if (!gpio_is_valid(curr->gpio[i].no)) {
652 ret = -EINVAL;
653 pr_err("%s: Invalid gpio = %d\n", __func__,
654 curr->gpio[i].no);
655 goto free_gpios;
656 }
657 if (enable) {
658 ret = gpio_request(curr->gpio[i].no,
659 curr->gpio[i].name);
660 if (ret) {
661 pr_err("%s: gpio_request(%d, %s) failed %d\n",
662 __func__, curr->gpio[i].no,
663 curr->gpio[i].name, ret);
664 goto free_gpios;
665 }
666 curr->gpio[i].is_enabled = true;
667 } else {
668 gpio_free(curr->gpio[i].no);
669 curr->gpio[i].is_enabled = false;
670 }
671 }
672 return ret;
673
674free_gpios:
675 for (i--; i >= 0; i--) {
676 gpio_free(curr->gpio[i].no);
677 curr->gpio[i].is_enabled = false;
678 }
679 return ret;
680}
681
682static int sdhci_msm_setup_pins(struct sdhci_msm_pltfm_data *pdata, bool enable)
683{
684 int ret = 0;
685
686 if (!pdata->pin_data || (pdata->pin_data->cfg_sts == enable))
687 return 0;
688
689 ret = sdhci_msm_setup_gpio(pdata, enable);
690 if (!ret)
691 pdata->pin_data->cfg_sts = enable;
692
693 return ret;
694}
695
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530696static int sdhci_msm_dt_get_array(struct device *dev, const char *prop_name,
697 u32 **out, int *len, u32 size)
698{
699 int ret = 0;
700 struct device_node *np = dev->of_node;
701 size_t sz;
702 u32 *arr = NULL;
703
704 if (!of_get_property(np, prop_name, len)) {
705 ret = -EINVAL;
706 goto out;
707 }
708 sz = *len = *len / sizeof(*arr);
709 if (sz <= 0 || (size > 0 && (sz != size))) {
710 dev_err(dev, "%s invalid size\n", prop_name);
711 ret = -EINVAL;
712 goto out;
713 }
714
715 arr = devm_kzalloc(dev, sz * sizeof(*arr), GFP_KERNEL);
716 if (!arr) {
717 dev_err(dev, "%s failed allocating memory\n", prop_name);
718 ret = -ENOMEM;
719 goto out;
720 }
721
722 ret = of_property_read_u32_array(np, prop_name, arr, sz);
723 if (ret < 0) {
724 dev_err(dev, "%s failed reading array %d\n", prop_name, ret);
725 goto out;
726 }
727 *out = arr;
728out:
729 if (ret)
730 *len = 0;
731 return ret;
732}
733
Asutosh Das0ef24812012-12-18 16:14:02 +0530734#define MAX_PROP_SIZE 32
735static int sdhci_msm_dt_parse_vreg_info(struct device *dev,
736 struct sdhci_msm_reg_data **vreg_data, const char *vreg_name)
737{
738 int len, ret = 0;
739 const __be32 *prop;
740 char prop_name[MAX_PROP_SIZE];
741 struct sdhci_msm_reg_data *vreg;
742 struct device_node *np = dev->of_node;
743
744 snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", vreg_name);
745 if (!of_parse_phandle(np, prop_name, 0)) {
746 dev_err(dev, "No vreg data found for %s\n", vreg_name);
747 ret = -EINVAL;
748 return ret;
749 }
750
751 vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
752 if (!vreg) {
753 dev_err(dev, "No memory for vreg: %s\n", vreg_name);
754 ret = -ENOMEM;
755 return ret;
756 }
757
758 vreg->name = vreg_name;
759
760 snprintf(prop_name, MAX_PROP_SIZE,
761 "qcom,%s-always-on", vreg_name);
762 if (of_get_property(np, prop_name, NULL))
763 vreg->is_always_on = true;
764
765 snprintf(prop_name, MAX_PROP_SIZE,
766 "qcom,%s-lpm-sup", vreg_name);
767 if (of_get_property(np, prop_name, NULL))
768 vreg->lpm_sup = true;
769
770 snprintf(prop_name, MAX_PROP_SIZE,
771 "qcom,%s-voltage-level", vreg_name);
772 prop = of_get_property(np, prop_name, &len);
773 if (!prop || (len != (2 * sizeof(__be32)))) {
774 dev_warn(dev, "%s %s property\n",
775 prop ? "invalid format" : "no", prop_name);
776 } else {
777 vreg->low_vol_level = be32_to_cpup(&prop[0]);
778 vreg->high_vol_level = be32_to_cpup(&prop[1]);
779 }
780
781 snprintf(prop_name, MAX_PROP_SIZE,
782 "qcom,%s-current-level", vreg_name);
783 prop = of_get_property(np, prop_name, &len);
784 if (!prop || (len != (2 * sizeof(__be32)))) {
785 dev_warn(dev, "%s %s property\n",
786 prop ? "invalid format" : "no", prop_name);
787 } else {
788 vreg->lpm_uA = be32_to_cpup(&prop[0]);
789 vreg->hpm_uA = be32_to_cpup(&prop[1]);
790 }
791
792 *vreg_data = vreg;
793 dev_dbg(dev, "%s: %s %s vol=[%d %d]uV, curr=[%d %d]uA\n",
794 vreg->name, vreg->is_always_on ? "always_on," : "",
795 vreg->lpm_sup ? "lpm_sup," : "", vreg->low_vol_level,
796 vreg->high_vol_level, vreg->lpm_uA, vreg->hpm_uA);
797
798 return ret;
799}
800
801#define GPIO_NAME_MAX_LEN 32
802static int sdhci_msm_dt_parse_gpio_info(struct device *dev,
803 struct sdhci_msm_pltfm_data *pdata)
804{
805 int ret = 0, cnt, i;
806 struct sdhci_msm_pin_data *pin_data;
807 struct device_node *np = dev->of_node;
808
809 pin_data = devm_kzalloc(dev, sizeof(*pin_data), GFP_KERNEL);
810 if (!pin_data) {
811 dev_err(dev, "No memory for pin_data\n");
812 ret = -ENOMEM;
813 goto out;
814 }
815
816 cnt = of_gpio_count(np);
817 if (cnt > 0) {
818 pin_data->gpio_data = devm_kzalloc(dev,
819 sizeof(struct sdhci_msm_gpio_data), GFP_KERNEL);
820 if (!pin_data->gpio_data) {
821 dev_err(dev, "No memory for gpio_data\n");
822 ret = -ENOMEM;
823 goto out;
824 }
825 pin_data->gpio_data->size = cnt;
826 pin_data->gpio_data->gpio = devm_kzalloc(dev, cnt *
827 sizeof(struct sdhci_msm_gpio), GFP_KERNEL);
828
829 if (!pin_data->gpio_data->gpio) {
830 dev_err(dev, "No memory for gpio\n");
831 ret = -ENOMEM;
832 goto out;
833 }
834
835 for (i = 0; i < cnt; i++) {
836 const char *name = NULL;
837 char result[GPIO_NAME_MAX_LEN];
838 pin_data->gpio_data->gpio[i].no = of_get_gpio(np, i);
839 of_property_read_string_index(np,
840 "qcom,gpio-names", i, &name);
841
842 snprintf(result, GPIO_NAME_MAX_LEN, "%s-%s",
843 dev_name(dev), name ? name : "?");
844 pin_data->gpio_data->gpio[i].name = result;
845 dev_dbg(dev, "%s: gpio[%s] = %d\n", __func__,
846 pin_data->gpio_data->gpio[i].name,
847 pin_data->gpio_data->gpio[i].no);
848 pdata->pin_data = pin_data;
849 }
850 }
851
852out:
853 if (ret)
854 dev_err(dev, "%s failed with err %d\n", __func__, ret);
855 return ret;
856}
857
858/* Parse platform data */
859static struct sdhci_msm_pltfm_data *sdhci_msm_populate_pdata(struct device *dev)
860{
861 struct sdhci_msm_pltfm_data *pdata = NULL;
862 struct device_node *np = dev->of_node;
863 u32 bus_width = 0;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +0530864 u32 cpu_dma_latency;
Asutosh Das0ef24812012-12-18 16:14:02 +0530865 int len, i;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530866 int clk_table_len;
867 u32 *clk_table = NULL;
Sujit Reddy Thumma1958d3d2013-06-03 09:54:32 +0530868 enum of_gpio_flags flags = OF_GPIO_ACTIVE_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +0530869
870 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
871 if (!pdata) {
872 dev_err(dev, "failed to allocate memory for platform data\n");
873 goto out;
874 }
875
Sujit Reddy Thumma1958d3d2013-06-03 09:54:32 +0530876 pdata->status_gpio = of_get_named_gpio_flags(np, "cd-gpios", 0, &flags);
877 if (gpio_is_valid(pdata->status_gpio) & !(flags & OF_GPIO_ACTIVE_LOW))
878 pdata->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
Sahitya Tummala581df132013-03-12 14:57:46 +0530879
Asutosh Das0ef24812012-12-18 16:14:02 +0530880 of_property_read_u32(np, "qcom,bus-width", &bus_width);
881 if (bus_width == 8)
882 pdata->mmc_bus_width = MMC_CAP_8_BIT_DATA;
883 else if (bus_width == 4)
884 pdata->mmc_bus_width = MMC_CAP_4_BIT_DATA;
885 else {
886 dev_notice(dev, "invalid bus-width, default to 1-bit mode\n");
887 pdata->mmc_bus_width = 0;
888 }
889
Sahitya Tummalac6f48d42013-03-10 07:03:17 +0530890 if (!of_property_read_u32(np, "qcom,cpu-dma-latency-us",
891 &cpu_dma_latency))
892 pdata->cpu_dma_latency_us = cpu_dma_latency;
893
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530894 if (sdhci_msm_dt_get_array(dev, "qcom,clk-rates",
895 &clk_table, &clk_table_len, 0)) {
896 dev_err(dev, "failed parsing supported clock rates\n");
897 goto out;
898 }
899 if (!clk_table || !clk_table_len) {
900 dev_err(dev, "Invalid clock table\n");
901 goto out;
902 }
903 pdata->sup_clk_table = clk_table;
904 pdata->sup_clk_cnt = clk_table_len;
905
Asutosh Das0ef24812012-12-18 16:14:02 +0530906 pdata->vreg_data = devm_kzalloc(dev, sizeof(struct
907 sdhci_msm_slot_reg_data),
908 GFP_KERNEL);
909 if (!pdata->vreg_data) {
910 dev_err(dev, "failed to allocate memory for vreg data\n");
911 goto out;
912 }
913
914 if (sdhci_msm_dt_parse_vreg_info(dev, &pdata->vreg_data->vdd_data,
915 "vdd")) {
916 dev_err(dev, "failed parsing vdd data\n");
917 goto out;
918 }
919 if (sdhci_msm_dt_parse_vreg_info(dev,
920 &pdata->vreg_data->vdd_io_data,
921 "vdd-io")) {
922 dev_err(dev, "failed parsing vdd-io data\n");
923 goto out;
924 }
925
926 if (sdhci_msm_dt_parse_gpio_info(dev, pdata)) {
927 dev_err(dev, "failed parsing gpio data\n");
928 goto out;
929 }
930
Asutosh Das0ef24812012-12-18 16:14:02 +0530931 len = of_property_count_strings(np, "qcom,bus-speed-mode");
932
933 for (i = 0; i < len; i++) {
934 const char *name = NULL;
935
936 of_property_read_string_index(np,
937 "qcom,bus-speed-mode", i, &name);
938 if (!name)
939 continue;
940
941 if (!strncmp(name, "HS200_1p8v", sizeof("HS200_1p8v")))
942 pdata->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
943 else if (!strncmp(name, "HS200_1p2v", sizeof("HS200_1p2v")))
944 pdata->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
945 else if (!strncmp(name, "DDR_1p8v", sizeof("DDR_1p8v")))
946 pdata->caps |= MMC_CAP_1_8V_DDR
947 | MMC_CAP_UHS_DDR50;
948 else if (!strncmp(name, "DDR_1p2v", sizeof("DDR_1p2v")))
949 pdata->caps |= MMC_CAP_1_2V_DDR
950 | MMC_CAP_UHS_DDR50;
951 }
952
953 if (of_get_property(np, "qcom,nonremovable", NULL))
954 pdata->nonremovable = true;
955
956 return pdata;
957out:
958 return NULL;
959}
960
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530961/* Returns required bandwidth in Bytes per Sec */
962static unsigned int sdhci_get_bw_required(struct sdhci_host *host,
963 struct mmc_ios *ios)
964{
Sahitya Tummala2886c922013-04-03 18:03:31 +0530965 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
966 struct sdhci_msm_host *msm_host = pltfm_host->priv;
967
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530968 unsigned int bw;
969
Sahitya Tummala2886c922013-04-03 18:03:31 +0530970 bw = msm_host->clk_rate;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530971 /*
972 * For DDR mode, SDCC controller clock will be at
973 * the double rate than the actual clock that goes to card.
974 */
975 if (ios->bus_width == MMC_BUS_WIDTH_4)
976 bw /= 2;
977 else if (ios->bus_width == MMC_BUS_WIDTH_1)
978 bw /= 8;
979
980 return bw;
981}
982
983static int sdhci_msm_bus_get_vote_for_bw(struct sdhci_msm_host *host,
984 unsigned int bw)
985{
986 unsigned int *table = host->pdata->voting_data->bw_vecs;
987 unsigned int size = host->pdata->voting_data->bw_vecs_size;
988 int i;
989
990 if (host->msm_bus_vote.is_max_bw_needed && bw)
991 return host->msm_bus_vote.max_bw_vote;
992
993 for (i = 0; i < size; i++) {
994 if (bw <= table[i])
995 break;
996 }
997
998 if (i && (i == size))
999 i--;
1000
1001 return i;
1002}
1003
1004/*
1005 * This function must be called with host lock acquired.
1006 * Caller of this function should also ensure that msm bus client
1007 * handle is not null.
1008 */
1009static inline int sdhci_msm_bus_set_vote(struct sdhci_msm_host *msm_host,
1010 int vote,
1011 unsigned long flags)
1012{
1013 struct sdhci_host *host = platform_get_drvdata(msm_host->pdev);
1014 int rc = 0;
1015
1016 if (vote != msm_host->msm_bus_vote.curr_vote) {
1017 spin_unlock_irqrestore(&host->lock, flags);
1018 rc = msm_bus_scale_client_update_request(
1019 msm_host->msm_bus_vote.client_handle, vote);
1020 spin_lock_irqsave(&host->lock, flags);
1021 if (rc) {
1022 pr_err("%s: msm_bus_scale_client_update_request() failed: bus_client_handle=0x%x, vote=%d, err=%d\n",
1023 mmc_hostname(host->mmc),
1024 msm_host->msm_bus_vote.client_handle, vote, rc);
1025 goto out;
1026 }
1027 msm_host->msm_bus_vote.curr_vote = vote;
1028 }
1029out:
1030 return rc;
1031}
1032
1033/*
1034 * Internal work. Work to set 0 bandwidth for msm bus.
1035 */
1036static void sdhci_msm_bus_work(struct work_struct *work)
1037{
1038 struct sdhci_msm_host *msm_host;
1039 struct sdhci_host *host;
1040 unsigned long flags;
1041
1042 msm_host = container_of(work, struct sdhci_msm_host,
1043 msm_bus_vote.vote_work.work);
1044 host = platform_get_drvdata(msm_host->pdev);
1045
1046 if (!msm_host->msm_bus_vote.client_handle)
1047 return;
1048
1049 spin_lock_irqsave(&host->lock, flags);
1050 /* don't vote for 0 bandwidth if any request is in progress */
1051 if (!host->mrq) {
1052 sdhci_msm_bus_set_vote(msm_host,
1053 msm_host->msm_bus_vote.min_bw_vote, flags);
1054 } else
1055 pr_warning("%s: %s: Transfer in progress. skipping bus voting to 0 bandwidth\n",
1056 mmc_hostname(host->mmc), __func__);
1057 spin_unlock_irqrestore(&host->lock, flags);
1058}
1059
1060/*
1061 * This function cancels any scheduled delayed work and sets the bus
1062 * vote based on bw (bandwidth) argument.
1063 */
1064static void sdhci_msm_bus_cancel_work_and_set_vote(struct sdhci_host *host,
1065 unsigned int bw)
1066{
1067 int vote;
1068 unsigned long flags;
1069 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1070 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1071
1072 cancel_delayed_work_sync(&msm_host->msm_bus_vote.vote_work);
1073 spin_lock_irqsave(&host->lock, flags);
1074 vote = sdhci_msm_bus_get_vote_for_bw(msm_host, bw);
1075 sdhci_msm_bus_set_vote(msm_host, vote, flags);
1076 spin_unlock_irqrestore(&host->lock, flags);
1077}
1078
1079#define MSM_MMC_BUS_VOTING_DELAY 200 /* msecs */
1080
1081/* This function queues a work which will set the bandwidth requiement to 0 */
1082static void sdhci_msm_bus_queue_work(struct sdhci_host *host)
1083{
1084 unsigned long flags;
1085 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1086 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1087
1088 spin_lock_irqsave(&host->lock, flags);
1089 if (msm_host->msm_bus_vote.min_bw_vote !=
1090 msm_host->msm_bus_vote.curr_vote)
1091 queue_delayed_work(system_wq,
1092 &msm_host->msm_bus_vote.vote_work,
1093 msecs_to_jiffies(MSM_MMC_BUS_VOTING_DELAY));
1094 spin_unlock_irqrestore(&host->lock, flags);
1095}
1096
1097static int sdhci_msm_bus_register(struct sdhci_msm_host *host,
1098 struct platform_device *pdev)
1099{
1100 int rc = 0;
1101 struct msm_bus_scale_pdata *bus_pdata;
1102
1103 struct sdhci_msm_bus_voting_data *data;
1104 struct device *dev = &pdev->dev;
1105
1106 data = devm_kzalloc(dev,
1107 sizeof(struct sdhci_msm_bus_voting_data), GFP_KERNEL);
1108 if (!data) {
1109 dev_err(&pdev->dev,
1110 "%s: failed to allocate memory\n", __func__);
1111 rc = -ENOMEM;
1112 goto out;
1113 }
1114 data->bus_pdata = msm_bus_cl_get_pdata(pdev);
1115 if (data->bus_pdata) {
1116 rc = sdhci_msm_dt_get_array(dev, "qcom,bus-bw-vectors-bps",
1117 &data->bw_vecs, &data->bw_vecs_size, 0);
1118 if (rc) {
1119 dev_err(&pdev->dev,
1120 "%s: Failed to get bus-bw-vectors-bps\n",
1121 __func__);
1122 goto out;
1123 }
1124 host->pdata->voting_data = data;
1125 }
1126 if (host->pdata->voting_data &&
1127 host->pdata->voting_data->bus_pdata &&
1128 host->pdata->voting_data->bw_vecs &&
1129 host->pdata->voting_data->bw_vecs_size) {
1130
1131 bus_pdata = host->pdata->voting_data->bus_pdata;
1132 host->msm_bus_vote.client_handle =
1133 msm_bus_scale_register_client(bus_pdata);
1134 if (!host->msm_bus_vote.client_handle) {
1135 dev_err(&pdev->dev, "msm_bus_scale_register_client()\n");
1136 rc = -EFAULT;
1137 goto out;
1138 }
1139 /* cache the vote index for minimum and maximum bandwidth */
1140 host->msm_bus_vote.min_bw_vote =
1141 sdhci_msm_bus_get_vote_for_bw(host, 0);
1142 host->msm_bus_vote.max_bw_vote =
1143 sdhci_msm_bus_get_vote_for_bw(host, UINT_MAX);
1144 } else {
1145 devm_kfree(dev, data);
1146 }
1147
1148out:
1149 return rc;
1150}
1151
1152static void sdhci_msm_bus_unregister(struct sdhci_msm_host *host)
1153{
1154 if (host->msm_bus_vote.client_handle)
1155 msm_bus_scale_unregister_client(
1156 host->msm_bus_vote.client_handle);
1157}
1158
1159static void sdhci_msm_bus_voting(struct sdhci_host *host, u32 enable)
1160{
1161 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1162 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1163 struct mmc_ios *ios = &host->mmc->ios;
1164 unsigned int bw;
1165
1166 if (!msm_host->msm_bus_vote.client_handle)
1167 return;
1168
1169 bw = sdhci_get_bw_required(host, ios);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301170 if (enable) {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301171 sdhci_msm_bus_cancel_work_and_set_vote(host, bw);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301172 } else {
1173 /*
1174 * If clock gating is enabled, then remove the vote
1175 * immediately because clocks will be disabled only
1176 * after SDHCI_MSM_MMC_CLK_GATE_DELAY and thus no
1177 * additional delay is required to remove the bus vote.
1178 */
1179#ifdef CONFIG_MMC_CLKGATE
1180 if (host->mmc->clkgate_delay)
1181 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
1182 else
1183#endif
1184 sdhci_msm_bus_queue_work(host);
1185 }
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301186}
1187
Asutosh Das0ef24812012-12-18 16:14:02 +05301188/* Regulator utility functions */
1189static int sdhci_msm_vreg_init_reg(struct device *dev,
1190 struct sdhci_msm_reg_data *vreg)
1191{
1192 int ret = 0;
1193
1194 /* check if regulator is already initialized? */
1195 if (vreg->reg)
1196 goto out;
1197
1198 /* Get the regulator handle */
1199 vreg->reg = devm_regulator_get(dev, vreg->name);
1200 if (IS_ERR(vreg->reg)) {
1201 ret = PTR_ERR(vreg->reg);
1202 pr_err("%s: devm_regulator_get(%s) failed. ret=%d\n",
1203 __func__, vreg->name, ret);
1204 goto out;
1205 }
1206
1207 /* sanity check */
1208 if (!vreg->high_vol_level || !vreg->hpm_uA) {
1209 pr_err("%s: %s invalid constraints specified\n",
1210 __func__, vreg->name);
1211 ret = -EINVAL;
1212 }
1213
1214out:
1215 return ret;
1216}
1217
1218static void sdhci_msm_vreg_deinit_reg(struct sdhci_msm_reg_data *vreg)
1219{
1220 if (vreg->reg)
1221 devm_regulator_put(vreg->reg);
1222}
1223
1224static int sdhci_msm_vreg_set_optimum_mode(struct sdhci_msm_reg_data
1225 *vreg, int uA_load)
1226{
1227 int ret = 0;
1228
1229 /*
1230 * regulators that do not support regulator_set_voltage also
1231 * do not support regulator_set_optimum_mode
1232 */
1233 if (vreg->set_voltage_sup) {
1234 ret = regulator_set_load(vreg->reg, uA_load);
1235 if (ret < 0)
1236 pr_err("%s: regulator_set_load(reg=%s,uA_load=%d) failed. ret=%d\n",
1237 __func__, vreg->name, uA_load, ret);
1238 else
1239 /*
1240 * regulator_set_load() can return non zero
1241 * value even for success case.
1242 */
1243 ret = 0;
1244 }
1245 return ret;
1246}
1247
1248static int sdhci_msm_vreg_set_voltage(struct sdhci_msm_reg_data *vreg,
1249 int min_uV, int max_uV)
1250{
1251 int ret = 0;
1252
1253 ret = regulator_set_voltage(vreg->reg, min_uV, max_uV);
1254 if (ret) {
1255 pr_err("%s: regulator_set_voltage(%s)failed. min_uV=%d,max_uV=%d,ret=%d\n",
1256 __func__, vreg->name, min_uV, max_uV, ret);
1257 }
1258
1259 return ret;
1260}
1261
1262static int sdhci_msm_vreg_enable(struct sdhci_msm_reg_data *vreg)
1263{
1264 int ret = 0;
1265
1266 /* Put regulator in HPM (high power mode) */
1267 ret = sdhci_msm_vreg_set_optimum_mode(vreg, vreg->hpm_uA);
1268 if (ret < 0)
1269 return ret;
1270
1271 if (!vreg->is_enabled) {
1272 /* Set voltage level */
1273 ret = sdhci_msm_vreg_set_voltage(vreg, vreg->high_vol_level,
1274 vreg->high_vol_level);
1275 if (ret)
1276 return ret;
1277 }
1278 ret = regulator_enable(vreg->reg);
1279 if (ret) {
1280 pr_err("%s: regulator_enable(%s) failed. ret=%d\n",
1281 __func__, vreg->name, ret);
1282 return ret;
1283 }
1284 vreg->is_enabled = true;
1285 return ret;
1286}
1287
1288static int sdhci_msm_vreg_disable(struct sdhci_msm_reg_data *vreg)
1289{
1290 int ret = 0;
1291
1292 /* Never disable regulator marked as always_on */
1293 if (vreg->is_enabled && !vreg->is_always_on) {
1294 ret = regulator_disable(vreg->reg);
1295 if (ret) {
1296 pr_err("%s: regulator_disable(%s) failed. ret=%d\n",
1297 __func__, vreg->name, ret);
1298 goto out;
1299 }
1300 vreg->is_enabled = false;
1301
1302 ret = sdhci_msm_vreg_set_optimum_mode(vreg, 0);
1303 if (ret < 0)
1304 goto out;
1305
1306 /* Set min. voltage level to 0 */
1307 ret = sdhci_msm_vreg_set_voltage(vreg, 0, vreg->high_vol_level);
1308 if (ret)
1309 goto out;
1310 } else if (vreg->is_enabled && vreg->is_always_on) {
1311 if (vreg->lpm_sup) {
1312 /* Put always_on regulator in LPM (low power mode) */
1313 ret = sdhci_msm_vreg_set_optimum_mode(vreg,
1314 vreg->lpm_uA);
1315 if (ret < 0)
1316 goto out;
1317 }
1318 }
1319out:
1320 return ret;
1321}
1322
1323static int sdhci_msm_setup_vreg(struct sdhci_msm_pltfm_data *pdata,
1324 bool enable, bool is_init)
1325{
1326 int ret = 0, i;
1327 struct sdhci_msm_slot_reg_data *curr_slot;
1328 struct sdhci_msm_reg_data *vreg_table[2];
1329
1330 curr_slot = pdata->vreg_data;
1331 if (!curr_slot) {
1332 pr_debug("%s: vreg info unavailable,assuming the slot is powered by always on domain\n",
1333 __func__);
1334 goto out;
1335 }
1336
1337 vreg_table[0] = curr_slot->vdd_data;
1338 vreg_table[1] = curr_slot->vdd_io_data;
1339
1340 for (i = 0; i < ARRAY_SIZE(vreg_table); i++) {
1341 if (vreg_table[i]) {
1342 if (enable)
1343 ret = sdhci_msm_vreg_enable(vreg_table[i]);
1344 else
1345 ret = sdhci_msm_vreg_disable(vreg_table[i]);
1346 if (ret)
1347 goto out;
1348 }
1349 }
1350out:
1351 return ret;
1352}
1353
1354/*
1355 * Reset vreg by ensuring it is off during probe. A call
1356 * to enable vreg is needed to balance disable vreg
1357 */
1358static int sdhci_msm_vreg_reset(struct sdhci_msm_pltfm_data *pdata)
1359{
1360 int ret;
1361
1362 ret = sdhci_msm_setup_vreg(pdata, 1, true);
1363 if (ret)
1364 return ret;
1365 ret = sdhci_msm_setup_vreg(pdata, 0, true);
1366 return ret;
1367}
1368
1369/* This init function should be called only once for each SDHC slot */
1370static int sdhci_msm_vreg_init(struct device *dev,
1371 struct sdhci_msm_pltfm_data *pdata,
1372 bool is_init)
1373{
1374 int ret = 0;
1375 struct sdhci_msm_slot_reg_data *curr_slot;
1376 struct sdhci_msm_reg_data *curr_vdd_reg, *curr_vdd_io_reg;
1377
1378 curr_slot = pdata->vreg_data;
1379 if (!curr_slot)
1380 goto out;
1381
1382 curr_vdd_reg = curr_slot->vdd_data;
1383 curr_vdd_io_reg = curr_slot->vdd_io_data;
1384
1385 if (!is_init)
1386 /* Deregister all regulators from regulator framework */
1387 goto vdd_io_reg_deinit;
1388
1389 /*
1390 * Get the regulator handle from voltage regulator framework
1391 * and then try to set the voltage level for the regulator
1392 */
1393 if (curr_vdd_reg) {
1394 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_reg);
1395 if (ret)
1396 goto out;
1397 }
1398 if (curr_vdd_io_reg) {
1399 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_io_reg);
1400 if (ret)
1401 goto vdd_reg_deinit;
1402 }
1403 ret = sdhci_msm_vreg_reset(pdata);
1404 if (ret)
1405 dev_err(dev, "vreg reset failed (%d)\n", ret);
1406 goto out;
1407
1408vdd_io_reg_deinit:
1409 if (curr_vdd_io_reg)
1410 sdhci_msm_vreg_deinit_reg(curr_vdd_io_reg);
1411vdd_reg_deinit:
1412 if (curr_vdd_reg)
1413 sdhci_msm_vreg_deinit_reg(curr_vdd_reg);
1414out:
1415 return ret;
1416}
1417
1418
1419static int sdhci_msm_set_vdd_io_vol(struct sdhci_msm_pltfm_data *pdata,
1420 enum vdd_io_level level,
1421 unsigned int voltage_level)
1422{
1423 int ret = 0;
1424 int set_level;
1425 struct sdhci_msm_reg_data *vdd_io_reg;
1426
1427 if (!pdata->vreg_data)
1428 return ret;
1429
1430 vdd_io_reg = pdata->vreg_data->vdd_io_data;
1431 if (vdd_io_reg && vdd_io_reg->is_enabled) {
1432 switch (level) {
1433 case VDD_IO_LOW:
1434 set_level = vdd_io_reg->low_vol_level;
1435 break;
1436 case VDD_IO_HIGH:
1437 set_level = vdd_io_reg->high_vol_level;
1438 break;
1439 case VDD_IO_SET_LEVEL:
1440 set_level = voltage_level;
1441 break;
1442 default:
1443 pr_err("%s: invalid argument level = %d",
1444 __func__, level);
1445 ret = -EINVAL;
1446 return ret;
1447 }
1448 ret = sdhci_msm_vreg_set_voltage(vdd_io_reg, set_level,
1449 set_level);
1450 }
1451 return ret;
1452}
1453
1454static irqreturn_t sdhci_msm_pwr_irq(int irq, void *data)
1455{
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001456 struct sdhci_host *host = (struct sdhci_host *)data;
1457 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1458 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Das0ef24812012-12-18 16:14:02 +05301459 u8 irq_status = 0;
1460 u8 irq_ack = 0;
1461 int ret = 0;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301462 int pwr_state = 0, io_level = 0;
1463 unsigned long flags;
Asutosh Das0ef24812012-12-18 16:14:02 +05301464
1465 irq_status = readb_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
1466 pr_debug("%s: Received IRQ(%d), status=0x%x\n",
1467 mmc_hostname(msm_host->mmc), irq, irq_status);
1468
1469 /* Clear the interrupt */
1470 writeb_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
1471 /*
1472 * SDHC has core_mem and hc_mem device memory and these memory
1473 * addresses do not fall within 1KB region. Hence, any update to
1474 * core_mem address space would require an mb() to ensure this gets
1475 * completed before its next update to registers within hc_mem.
1476 */
1477 mb();
1478
1479 /* Handle BUS ON/OFF*/
1480 if (irq_status & CORE_PWRCTL_BUS_ON) {
1481 ret = sdhci_msm_setup_vreg(msm_host->pdata, true, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301482 if (!ret) {
Asutosh Das0ef24812012-12-18 16:14:02 +05301483 ret = sdhci_msm_setup_pins(msm_host->pdata, true);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301484 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
1485 VDD_IO_HIGH, 0);
1486 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301487 if (ret)
1488 irq_ack |= CORE_PWRCTL_BUS_FAIL;
1489 else
1490 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301491
1492 pwr_state = REQ_BUS_ON;
1493 io_level = REQ_IO_HIGH;
Asutosh Das0ef24812012-12-18 16:14:02 +05301494 }
1495 if (irq_status & CORE_PWRCTL_BUS_OFF) {
1496 ret = sdhci_msm_setup_vreg(msm_host->pdata, false, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301497 if (!ret) {
Asutosh Das0ef24812012-12-18 16:14:02 +05301498 ret = sdhci_msm_setup_pins(msm_host->pdata, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301499 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
1500 VDD_IO_LOW, 0);
1501 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301502 if (ret)
1503 irq_ack |= CORE_PWRCTL_BUS_FAIL;
1504 else
1505 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301506
1507 pwr_state = REQ_BUS_OFF;
1508 io_level = REQ_IO_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05301509 }
1510 /* Handle IO LOW/HIGH */
1511 if (irq_status & CORE_PWRCTL_IO_LOW) {
1512 /* Switch voltage Low */
1513 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_LOW, 0);
1514 if (ret)
1515 irq_ack |= CORE_PWRCTL_IO_FAIL;
1516 else
1517 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301518
1519 io_level = REQ_IO_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05301520 }
1521 if (irq_status & CORE_PWRCTL_IO_HIGH) {
1522 /* Switch voltage High */
1523 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_HIGH, 0);
1524 if (ret)
1525 irq_ack |= CORE_PWRCTL_IO_FAIL;
1526 else
1527 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301528
1529 io_level = REQ_IO_HIGH;
Asutosh Das0ef24812012-12-18 16:14:02 +05301530 }
1531
1532 /* ACK status to the core */
1533 writeb_relaxed(irq_ack, (msm_host->core_mem + CORE_PWRCTL_CTL));
1534 /*
1535 * SDHC has core_mem and hc_mem device memory and these memory
1536 * addresses do not fall within 1KB region. Hence, any update to
1537 * core_mem address space would require an mb() to ensure this gets
1538 * completed before its next update to registers within hc_mem.
1539 */
1540 mb();
1541
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301542 if (io_level & REQ_IO_HIGH)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001543 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
1544 ~CORE_IO_PAD_PWR_SWITCH),
1545 host->ioaddr + CORE_VENDOR_SPEC);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301546 else if (io_level & REQ_IO_LOW)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001547 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
1548 CORE_IO_PAD_PWR_SWITCH),
1549 host->ioaddr + CORE_VENDOR_SPEC);
1550 mb();
1551
Asutosh Das0ef24812012-12-18 16:14:02 +05301552 pr_debug("%s: Handled IRQ(%d), ret=%d, ack=0x%x\n",
1553 mmc_hostname(msm_host->mmc), irq, ret, irq_ack);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301554 spin_lock_irqsave(&host->lock, flags);
1555 if (pwr_state)
1556 msm_host->curr_pwr_state = pwr_state;
1557 if (io_level)
1558 msm_host->curr_io_level = io_level;
1559 complete(&msm_host->pwr_irq_completion);
1560 spin_unlock_irqrestore(&host->lock, flags);
1561
Asutosh Das0ef24812012-12-18 16:14:02 +05301562 return IRQ_HANDLED;
1563}
1564
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301565static ssize_t
1566show_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
1567 char *buf)
1568{
1569 struct sdhci_host *host = dev_get_drvdata(dev);
1570 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1571 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1572
1573 return snprintf(buf, PAGE_SIZE, "%u\n",
1574 msm_host->msm_bus_vote.is_max_bw_needed);
1575}
1576
1577static ssize_t
1578store_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
1579 const char *buf, size_t count)
1580{
1581 struct sdhci_host *host = dev_get_drvdata(dev);
1582 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1583 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1584 uint32_t value;
1585 unsigned long flags;
1586
1587 if (!kstrtou32(buf, 0, &value)) {
1588 spin_lock_irqsave(&host->lock, flags);
1589 msm_host->msm_bus_vote.is_max_bw_needed = !!value;
1590 spin_unlock_irqrestore(&host->lock, flags);
1591 }
1592 return count;
1593}
1594
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301595static void sdhci_msm_check_power_status(struct sdhci_host *host, u32 req_type)
Asutosh Das0ef24812012-12-18 16:14:02 +05301596{
1597 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1598 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301599 unsigned long flags;
1600 bool done = false;
Asutosh Das0ef24812012-12-18 16:14:02 +05301601
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301602 spin_lock_irqsave(&host->lock, flags);
1603 pr_debug("%s: %s: request %d curr_pwr_state %x curr_io_level %x\n",
1604 mmc_hostname(host->mmc), __func__, req_type,
1605 msm_host->curr_pwr_state, msm_host->curr_io_level);
1606 if ((req_type & msm_host->curr_pwr_state) ||
1607 (req_type & msm_host->curr_io_level))
1608 done = true;
1609 spin_unlock_irqrestore(&host->lock, flags);
Asutosh Das0ef24812012-12-18 16:14:02 +05301610
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301611 /*
1612 * This is needed here to hanlde a case where IRQ gets
1613 * triggered even before this function is called so that
1614 * x->done counter of completion gets reset. Otherwise,
1615 * next call to wait_for_completion returns immediately
1616 * without actually waiting for the IRQ to be handled.
1617 */
1618 if (done)
1619 init_completion(&msm_host->pwr_irq_completion);
1620 else
1621 wait_for_completion(&msm_host->pwr_irq_completion);
1622
1623 pr_debug("%s: %s: request %d done\n", mmc_hostname(host->mmc),
1624 __func__, req_type);
Asutosh Das0ef24812012-12-18 16:14:02 +05301625}
1626
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001627static void sdhci_msm_toggle_cdr(struct sdhci_host *host, bool enable)
1628{
1629 if (enable)
1630 writel_relaxed((readl_relaxed(host->ioaddr +
1631 CORE_DLL_CONFIG) | CORE_CDR_EN),
1632 host->ioaddr + CORE_DLL_CONFIG);
1633 else
1634 writel_relaxed((readl_relaxed(host->ioaddr +
1635 CORE_DLL_CONFIG) & ~CORE_CDR_EN),
1636 host->ioaddr + CORE_DLL_CONFIG);
1637}
1638
Asutosh Das648f9d12013-01-10 21:11:04 +05301639static unsigned int sdhci_msm_max_segs(void)
1640{
1641 return SDHCI_MSM_MAX_SEGMENTS;
1642}
1643
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301644static unsigned int sdhci_msm_get_min_clock(struct sdhci_host *host)
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301645{
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301646 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1647 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301648
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301649 return msm_host->pdata->sup_clk_table[0];
1650}
1651
1652static unsigned int sdhci_msm_get_max_clock(struct sdhci_host *host)
1653{
1654 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1655 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1656 int max_clk_index = msm_host->pdata->sup_clk_cnt;
1657
1658 return msm_host->pdata->sup_clk_table[max_clk_index - 1];
1659}
1660
1661static unsigned int sdhci_msm_get_sup_clk_rate(struct sdhci_host *host,
1662 u32 req_clk)
1663{
1664 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1665 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1666 unsigned int sel_clk = -1;
1667 unsigned char cnt;
1668
1669 if (req_clk < sdhci_msm_get_min_clock(host)) {
1670 sel_clk = sdhci_msm_get_min_clock(host);
1671 return sel_clk;
1672 }
1673
1674 for (cnt = 0; cnt < msm_host->pdata->sup_clk_cnt; cnt++) {
1675 if (msm_host->pdata->sup_clk_table[cnt] > req_clk) {
1676 break;
1677 } else if (msm_host->pdata->sup_clk_table[cnt] == req_clk) {
1678 sel_clk = msm_host->pdata->sup_clk_table[cnt];
1679 break;
1680 } else {
1681 sel_clk = msm_host->pdata->sup_clk_table[cnt];
1682 }
1683 }
1684 return sel_clk;
1685}
1686
1687static int sdhci_msm_prepare_clocks(struct sdhci_host *host, bool enable)
1688{
1689 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1690 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1691 int rc = 0;
1692
1693 if (enable && !atomic_read(&msm_host->clks_on)) {
1694 pr_debug("%s: request to enable clocks\n",
1695 mmc_hostname(host->mmc));
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301696
1697 sdhci_msm_bus_voting(host, 1);
1698
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301699 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
1700 rc = clk_prepare_enable(msm_host->bus_clk);
1701 if (rc) {
1702 pr_err("%s: %s: failed to enable the bus-clock with error %d\n",
1703 mmc_hostname(host->mmc), __func__, rc);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301704 goto remove_vote;
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301705 }
1706 }
1707 if (!IS_ERR(msm_host->pclk)) {
1708 rc = clk_prepare_enable(msm_host->pclk);
1709 if (rc) {
1710 pr_err("%s: %s: failed to enable the pclk with error %d\n",
1711 mmc_hostname(host->mmc), __func__, rc);
1712 goto disable_bus_clk;
1713 }
1714 }
1715 rc = clk_prepare_enable(msm_host->clk);
1716 if (rc) {
1717 pr_err("%s: %s: failed to enable the host-clk with error %d\n",
1718 mmc_hostname(host->mmc), __func__, rc);
1719 goto disable_pclk;
1720 }
1721 mb();
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301722
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301723 } else if (!enable && atomic_read(&msm_host->clks_on)) {
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301724 pr_debug("%s: request to disable clocks\n",
1725 mmc_hostname(host->mmc));
1726 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
1727 mb();
1728 clk_disable_unprepare(msm_host->clk);
1729 if (!IS_ERR(msm_host->pclk))
1730 clk_disable_unprepare(msm_host->pclk);
1731 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
1732 clk_disable_unprepare(msm_host->bus_clk);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301733
1734 sdhci_msm_bus_voting(host, 0);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301735 }
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301736 atomic_set(&msm_host->clks_on, enable);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301737 goto out;
1738disable_pclk:
1739 if (!IS_ERR_OR_NULL(msm_host->pclk))
1740 clk_disable_unprepare(msm_host->pclk);
1741disable_bus_clk:
1742 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
1743 clk_disable_unprepare(msm_host->bus_clk);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301744remove_vote:
1745 if (msm_host->msm_bus_vote.client_handle)
1746 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301747out:
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301748 return rc;
1749}
1750
1751static void sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock)
1752{
1753 int rc;
1754 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1755 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1756 struct mmc_ios curr_ios = host->mmc->ios;
1757 u32 sup_clock, ddr_clock;
1758
1759 if (!clock) {
1760 sdhci_msm_prepare_clocks(host, false);
1761 host->clock = clock;
1762 goto out;
1763 }
1764
1765 rc = sdhci_msm_prepare_clocks(host, true);
1766 if (rc)
1767 goto out;
1768
1769 sup_clock = sdhci_msm_get_sup_clk_rate(host, clock);
1770 if (curr_ios.timing == MMC_TIMING_UHS_DDR50) {
1771 /*
1772 * The SDHC requires internal clock frequency to be double the
1773 * actual clock that will be set for DDR mode. The controller
1774 * uses the faster clock(100MHz) for some of its parts and send
1775 * the actual required clock (50MHz) to the card.
1776 */
1777 ddr_clock = clock * 2;
1778 sup_clock = sdhci_msm_get_sup_clk_rate(host,
1779 ddr_clock);
1780 }
1781 if (sup_clock != msm_host->clk_rate) {
1782 pr_debug("%s: %s: setting clk rate to %u\n",
1783 mmc_hostname(host->mmc), __func__, sup_clock);
1784 rc = clk_set_rate(msm_host->clk, sup_clock);
1785 if (rc) {
1786 pr_err("%s: %s: Failed to set rate %u for host-clk : %d\n",
1787 mmc_hostname(host->mmc), __func__,
1788 sup_clock, rc);
1789 goto out;
1790 }
1791 msm_host->clk_rate = sup_clock;
1792 host->clock = clock;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301793 /*
1794 * Update the bus vote in case of frequency change due to
1795 * clock scaling.
1796 */
1797 sdhci_msm_bus_voting(host, 1);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301798 }
1799out:
1800 sdhci_set_clock(host, clock);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301801}
1802
Sahitya Tummala14613432013-03-21 11:13:25 +05301803static void sdhci_msm_set_uhs_signaling(struct sdhci_host *host,
1804 unsigned int uhs)
1805{
1806 u16 ctrl_2;
1807
1808 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1809 /* Select Bus Speed Mode for host */
1810 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1811 if (uhs == MMC_TIMING_MMC_HS200)
1812 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1813 else if (uhs == MMC_TIMING_UHS_SDR12)
1814 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1815 else if (uhs == MMC_TIMING_UHS_SDR25)
1816 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1817 else if (uhs == MMC_TIMING_UHS_SDR50)
1818 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1819 else if (uhs == MMC_TIMING_UHS_SDR104)
1820 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1821 else if (uhs == MMC_TIMING_UHS_DDR50)
1822 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301823 /*
1824 * When clock frquency is less than 100MHz, the feedback clock must be
1825 * provided and DLL must not be used so that tuning can be skipped. To
1826 * provide feedback clock, the mode selection can be any value less
1827 * than 3'b011 in bits [2:0] of HOST CONTROL2 register.
1828 */
1829 if (host->clock <= (100 * 1000 * 1000) &&
1830 (uhs == MMC_TIMING_MMC_HS200 ||
1831 uhs == MMC_TIMING_UHS_SDR104))
1832 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1833
Sahitya Tummala14613432013-03-21 11:13:25 +05301834 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1835
1836}
1837
Asutosh Das0ef24812012-12-18 16:14:02 +05301838static struct sdhci_ops sdhci_msm_ops = {
Sahitya Tummala14613432013-03-21 11:13:25 +05301839 .set_uhs_signaling = sdhci_msm_set_uhs_signaling,
Asutosh Das0ef24812012-12-18 16:14:02 +05301840 .check_power_status = sdhci_msm_check_power_status,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001841 .platform_execute_tuning = sdhci_msm_execute_tuning,
1842 .toggle_cdr = sdhci_msm_toggle_cdr,
Asutosh Das648f9d12013-01-10 21:11:04 +05301843 .get_max_segments = sdhci_msm_max_segs,
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301844 .set_clock = sdhci_msm_set_clock,
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301845 .get_min_clock = sdhci_msm_get_min_clock,
1846 .get_max_clock = sdhci_msm_get_max_clock,
Asutosh Das0ef24812012-12-18 16:14:02 +05301847};
1848
1849static int sdhci_msm_probe(struct platform_device *pdev)
1850{
1851 struct sdhci_host *host;
1852 struct sdhci_pltfm_host *pltfm_host;
1853 struct sdhci_msm_host *msm_host;
1854 struct resource *core_memres = NULL;
1855 int ret = 0, pwr_irq = 0, dead = 0;
Stephen Boyd8dce5c62013-04-24 14:19:46 -07001856 u16 host_version;
Subhash Jadavani28137342013-05-14 17:46:43 +05301857 u32 pwr, irq_status, irq_ctl;
Asutosh Das0ef24812012-12-18 16:14:02 +05301858
1859 pr_debug("%s: Enter %s\n", dev_name(&pdev->dev), __func__);
1860 msm_host = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_msm_host),
1861 GFP_KERNEL);
1862 if (!msm_host) {
1863 ret = -ENOMEM;
1864 goto out;
1865 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301866
1867 msm_host->sdhci_msm_pdata.ops = &sdhci_msm_ops;
1868 host = sdhci_pltfm_init(pdev, &msm_host->sdhci_msm_pdata, 0);
1869 if (IS_ERR(host)) {
1870 ret = PTR_ERR(host);
1871 goto out;
1872 }
1873
1874 pltfm_host = sdhci_priv(host);
1875 pltfm_host->priv = msm_host;
1876 msm_host->mmc = host->mmc;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301877 msm_host->pdev = pdev;
Asutosh Das0ef24812012-12-18 16:14:02 +05301878
1879 /* Extract platform data */
1880 if (pdev->dev.of_node) {
Venkat Gopalakrishnan270580a2013-03-11 12:17:57 -07001881 ret = of_alias_get_id(pdev->dev.of_node, "sdhc");
1882 if (ret < 0) {
1883 dev_err(&pdev->dev, "Failed to get slot index %d\n",
1884 ret);
1885 goto pltfm_free;
1886 }
1887 if (disable_slots & (1 << (ret - 1))) {
1888 dev_info(&pdev->dev, "%s: Slot %d disabled\n", __func__,
1889 ret);
1890 ret = -ENODEV;
1891 goto pltfm_free;
1892 }
1893
Asutosh Das0ef24812012-12-18 16:14:02 +05301894 msm_host->pdata = sdhci_msm_populate_pdata(&pdev->dev);
1895 if (!msm_host->pdata) {
1896 dev_err(&pdev->dev, "DT parsing error\n");
1897 goto pltfm_free;
1898 }
1899 } else {
1900 dev_err(&pdev->dev, "No device tree node\n");
1901 goto pltfm_free;
1902 }
1903
1904 /* Setup Clocks */
1905
1906 /* Setup SDCC bus voter clock. */
1907 msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus_clk");
1908 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
1909 /* Vote for max. clk rate for max. performance */
1910 ret = clk_set_rate(msm_host->bus_clk, INT_MAX);
1911 if (ret)
1912 goto pltfm_free;
1913 ret = clk_prepare_enable(msm_host->bus_clk);
1914 if (ret)
1915 goto pltfm_free;
1916 }
1917
1918 /* Setup main peripheral bus clock */
1919 msm_host->pclk = devm_clk_get(&pdev->dev, "iface_clk");
1920 if (!IS_ERR(msm_host->pclk)) {
1921 ret = clk_prepare_enable(msm_host->pclk);
1922 if (ret)
1923 goto bus_clk_disable;
1924 }
1925
1926 /* Setup SDC MMC clock */
1927 msm_host->clk = devm_clk_get(&pdev->dev, "core_clk");
1928 if (IS_ERR(msm_host->clk)) {
1929 ret = PTR_ERR(msm_host->clk);
1930 goto pclk_disable;
1931 }
1932
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301933 /* Set to the minimum supported clock frequency */
1934 ret = clk_set_rate(msm_host->clk, sdhci_msm_get_min_clock(host));
1935 if (ret) {
1936 dev_err(&pdev->dev, "MClk rate set failed (%d)\n", ret);
Sahitya Tummala020ede0d2013-06-07 13:03:07 +05301937 goto pclk_disable;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301938 }
Sahitya Tummala020ede0d2013-06-07 13:03:07 +05301939 ret = clk_prepare_enable(msm_host->clk);
1940 if (ret)
1941 goto pclk_disable;
1942
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301943 msm_host->clk_rate = sdhci_msm_get_min_clock(host);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301944 atomic_set(&msm_host->clks_on, 1);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301945
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301946 ret = sdhci_msm_bus_register(msm_host, pdev);
1947 if (ret)
1948 goto clk_disable;
1949
1950 if (msm_host->msm_bus_vote.client_handle)
1951 INIT_DELAYED_WORK(&msm_host->msm_bus_vote.vote_work,
1952 sdhci_msm_bus_work);
1953 sdhci_msm_bus_voting(host, 1);
1954
Asutosh Das0ef24812012-12-18 16:14:02 +05301955 /* Setup regulators */
1956 ret = sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, true);
1957 if (ret) {
1958 dev_err(&pdev->dev, "Regulator setup failed (%d)\n", ret);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301959 goto bus_unregister;
Asutosh Das0ef24812012-12-18 16:14:02 +05301960 }
1961
1962 /* Reset the core and Enable SDHC mode */
1963 core_memres = platform_get_resource_byname(pdev,
1964 IORESOURCE_MEM, "core_mem");
1965 msm_host->core_mem = devm_ioremap(&pdev->dev, core_memres->start,
1966 resource_size(core_memres));
1967
1968 if (!msm_host->core_mem) {
1969 dev_err(&pdev->dev, "Failed to remap registers\n");
1970 ret = -ENOMEM;
1971 goto vreg_deinit;
1972 }
1973
1974 /* Set SW_RST bit in POWER register (Offset 0x0) */
Sahitya Tummala66b0fe32013-04-25 11:50:56 +05301975 writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_POWER) |
1976 CORE_SW_RST, msm_host->core_mem + CORE_POWER);
1977 /*
1978 * SW reset can take upto 10HCLK + 15MCLK cycles.
1979 * Calculating based on min clk rates (hclk = 27MHz,
1980 * mclk = 400KHz) it comes to ~40us. Let's poll for
1981 * max. 1ms for reset completion.
1982 */
1983 ret = readl_poll_timeout(msm_host->core_mem + CORE_POWER,
1984 pwr, !(pwr & CORE_SW_RST), 100, 10);
1985
1986 if (ret) {
1987 dev_err(&pdev->dev, "reset failed (%d)\n", ret);
1988 goto vreg_deinit;
1989 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301990 /* Set HC_MODE_EN bit in HC_MODE register */
1991 writel_relaxed(HC_MODE_EN, (msm_host->core_mem + CORE_HC_MODE));
1992
1993 /*
Subhash Jadavani28137342013-05-14 17:46:43 +05301994 * CORE_SW_RST above may trigger power irq if previous status of PWRCTL
1995 * was either BUS_ON or IO_HIGH_V. So before we enable the power irq
1996 * interrupt in GIC (by registering the interrupt handler), we need to
1997 * ensure that any pending power irq interrupt status is acknowledged
1998 * otherwise power irq interrupt handler would be fired prematurely.
1999 */
2000 irq_status = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
2001 writel_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
2002 irq_ctl = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_CTL);
2003 if (irq_status & (CORE_PWRCTL_BUS_ON | CORE_PWRCTL_BUS_OFF))
2004 irq_ctl |= CORE_PWRCTL_BUS_SUCCESS;
2005 if (irq_status & (CORE_PWRCTL_IO_HIGH | CORE_PWRCTL_IO_LOW))
2006 irq_ctl |= CORE_PWRCTL_IO_SUCCESS;
2007 writel_relaxed(irq_ctl, (msm_host->core_mem + CORE_PWRCTL_CTL));
2008 /*
2009 * Ensure that above writes are propogated before interrupt enablement
2010 * in GIC.
2011 */
2012 mb();
2013
2014 /*
Asutosh Das0ef24812012-12-18 16:14:02 +05302015 * Following are the deviations from SDHC spec v3.0 -
2016 * 1. Card detection is handled using separate GPIO.
2017 * 2. Bus power control is handled by interacting with PMIC.
2018 */
2019 host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
2020 host->quirks |= SDHCI_QUIRK_SINGLE_POWER_WRITE;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302021 host->quirks |= SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN;
2022 host->quirks2 |= SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK;
Sahitya Tummala87d43942013-04-12 11:49:11 +05302023 host->quirks2 |= SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD;
Sahitya Tummala314162c2013-04-12 12:11:20 +05302024 host->quirks2 |= SDHCI_QUIRK2_BROKEN_PRESET_VALUE;
Sahitya Tummala7c9780d2013-04-12 11:59:25 +05302025 host->quirks2 |= SDHCI_QUIRK2_USE_RESERVED_MAX_TIMEOUT;
Asutosh Das0ef24812012-12-18 16:14:02 +05302026
Stephen Boyd8dce5c62013-04-24 14:19:46 -07002027 host_version = readw_relaxed((host->ioaddr + SDHCI_HOST_VERSION));
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07002028 dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n",
2029 host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >>
2030 SDHCI_VENDOR_VER_SHIFT));
2031 if (((host_version & SDHCI_VENDOR_VER_MASK) >>
2032 SDHCI_VENDOR_VER_SHIFT) == SDHCI_VER_100) {
2033 /*
2034 * Add 40us delay in interrupt handler when
2035 * operating at initialization frequency(400KHz).
2036 */
2037 host->quirks2 |= SDHCI_QUIRK2_SLOW_INT_CLR;
2038 /*
2039 * Set Software Reset for DAT line in Software
2040 * Reset Register (Bit 2).
2041 */
2042 host->quirks2 |= SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT;
2043 }
2044
2045 /* Setup PWRCTL irq */
Asutosh Das0ef24812012-12-18 16:14:02 +05302046 pwr_irq = platform_get_irq_byname(pdev, "pwr_irq");
2047 if (pwr_irq < 0) {
2048 dev_err(&pdev->dev, "Failed to get pwr_irq by name (%d)\n",
2049 pwr_irq);
2050 goto vreg_deinit;
2051 }
2052 ret = devm_request_threaded_irq(&pdev->dev, pwr_irq, NULL,
2053 sdhci_msm_pwr_irq, IRQF_ONESHOT,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002054 dev_name(&pdev->dev), host);
Asutosh Das0ef24812012-12-18 16:14:02 +05302055 if (ret) {
2056 dev_err(&pdev->dev, "Request threaded irq(%d) failed (%d)\n",
2057 pwr_irq, ret);
2058 goto vreg_deinit;
2059 }
2060
2061 /* Enable pwr irq interrupts */
2062 writel_relaxed(INT_MASK, (msm_host->core_mem + CORE_PWRCTL_MASK));
2063
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302064#ifdef CONFIG_MMC_CLKGATE
2065 /* Set clock gating delay to be used when CONFIG_MMC_CLKGATE is set */
2066 msm_host->mmc->clkgate_delay = SDHCI_MSM_MMC_CLK_GATE_DELAY;
2067#endif
2068
Asutosh Das0ef24812012-12-18 16:14:02 +05302069 /* Set host capabilities */
2070 msm_host->mmc->caps |= msm_host->pdata->mmc_bus_width;
2071 msm_host->mmc->caps |= msm_host->pdata->caps;
Sahitya Tummala7bd99062013-02-28 12:21:58 +05302072 msm_host->mmc->caps |= MMC_CAP_HW_RESET;
Asutosh Das0ef24812012-12-18 16:14:02 +05302073 msm_host->mmc->caps2 |= msm_host->pdata->caps2;
2074 msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR;
2075 msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR_CONTROL;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302076 msm_host->mmc->caps2 |= MMC_CAP2_CLK_SCALE;
Subhash Jadavani6d472b22013-05-29 15:52:10 +05302077 msm_host->mmc->caps2 |= MMC_CAP2_ASYNC_SDIO_IRQ_4BIT_MODE;
Asutosh Das0ef24812012-12-18 16:14:02 +05302078
2079 if (msm_host->pdata->nonremovable)
2080 msm_host->mmc->caps |= MMC_CAP_NONREMOVABLE;
2081
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05302082 host->cpu_dma_latency_us = msm_host->pdata->cpu_dma_latency_us;
2083
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302084 init_completion(&msm_host->pwr_irq_completion);
2085
Sahitya Tummala581df132013-03-12 14:57:46 +05302086 if (gpio_is_valid(msm_host->pdata->status_gpio)) {
2087 ret = mmc_gpio_request_cd(msm_host->mmc,
2088 msm_host->pdata->status_gpio, 0);
2089 if (ret) {
2090 dev_err(&pdev->dev, "%s: Failed to request card detection IRQ %d\n",
2091 __func__, ret);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302092 goto vreg_deinit;
Sahitya Tummala581df132013-03-12 14:57:46 +05302093 }
2094 }
2095
Sahitya Tummalaeaa21862013-03-20 19:34:59 +05302096 if (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(32))) {
2097 host->dma_mask = DMA_BIT_MASK(32);
2098 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2099 } else {
2100 dev_err(&pdev->dev, "%s: Failed to set dma mask\n", __func__);
2101 }
2102
Asutosh Das0ef24812012-12-18 16:14:02 +05302103 ret = sdhci_add_host(host);
2104 if (ret) {
2105 dev_err(&pdev->dev, "Add host failed (%d)\n", ret);
Sahitya Tummala581df132013-03-12 14:57:46 +05302106 goto vreg_deinit;
Asutosh Das0ef24812012-12-18 16:14:02 +05302107 }
2108
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302109 msm_host->msm_bus_vote.max_bus_bw.show = show_sdhci_max_bus_bw;
2110 msm_host->msm_bus_vote.max_bus_bw.store = store_sdhci_max_bus_bw;
2111 sysfs_attr_init(&msm_host->msm_bus_vote.max_bus_bw.attr);
2112 msm_host->msm_bus_vote.max_bus_bw.attr.name = "max_bus_bw";
2113 msm_host->msm_bus_vote.max_bus_bw.attr.mode = S_IRUGO | S_IWUSR;
2114 ret = device_create_file(&pdev->dev,
2115 &msm_host->msm_bus_vote.max_bus_bw);
2116 if (ret)
2117 goto remove_host;
2118
Asutosh Das0ef24812012-12-18 16:14:02 +05302119 /* Successful initialization */
2120 goto out;
2121
2122remove_host:
2123 dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
2124 sdhci_remove_host(host, dead);
2125vreg_deinit:
2126 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302127bus_unregister:
2128 if (msm_host->msm_bus_vote.client_handle)
2129 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2130 sdhci_msm_bus_unregister(msm_host);
Asutosh Das0ef24812012-12-18 16:14:02 +05302131clk_disable:
2132 if (!IS_ERR(msm_host->clk))
2133 clk_disable_unprepare(msm_host->clk);
2134pclk_disable:
2135 if (!IS_ERR(msm_host->pclk))
2136 clk_disable_unprepare(msm_host->pclk);
2137bus_clk_disable:
2138 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2139 clk_disable_unprepare(msm_host->bus_clk);
2140pltfm_free:
2141 sdhci_pltfm_free(pdev);
2142out:
2143 pr_debug("%s: Exit %s\n", dev_name(&pdev->dev), __func__);
2144 return ret;
2145}
2146
2147static int sdhci_msm_remove(struct platform_device *pdev)
2148{
2149 struct sdhci_host *host = platform_get_drvdata(pdev);
2150 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2151 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2152 struct sdhci_msm_pltfm_data *pdata = msm_host->pdata;
2153 int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) ==
2154 0xffffffff);
2155
2156 pr_debug("%s: %s\n", dev_name(&pdev->dev), __func__);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302157 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Asutosh Das0ef24812012-12-18 16:14:02 +05302158 sdhci_remove_host(host, dead);
2159 sdhci_pltfm_free(pdev);
Sahitya Tummala581df132013-03-12 14:57:46 +05302160
Asutosh Das0ef24812012-12-18 16:14:02 +05302161 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302162
Asutosh Das0ef24812012-12-18 16:14:02 +05302163 if (pdata->pin_data)
2164 sdhci_msm_setup_gpio(pdata, false);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302165
2166 if (msm_host->msm_bus_vote.client_handle) {
2167 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2168 sdhci_msm_bus_unregister(msm_host);
2169 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302170 return 0;
2171}
2172
2173static const struct of_device_id sdhci_msm_dt_match[] = {
2174 {.compatible = "qcom,sdhci-msm"},
2175};
2176MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match);
2177
2178static struct platform_driver sdhci_msm_driver = {
2179 .probe = sdhci_msm_probe,
2180 .remove = sdhci_msm_remove,
2181 .driver = {
2182 .name = "sdhci_msm",
2183 .owner = THIS_MODULE,
2184 .of_match_table = sdhci_msm_dt_match,
2185 },
2186};
2187
2188module_platform_driver(sdhci_msm_driver);
2189
2190MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Secure Digital Host Controller Interface driver");
2191MODULE_LICENSE("GPL v2");