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