blob: ee82df3771dc5be6e1ccb5f6083e284db3591a97 [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{
947 unsigned int bw;
948
949 bw = host->clock;
950 /*
951 * For DDR mode, SDCC controller clock will be at
952 * the double rate than the actual clock that goes to card.
953 */
954 if (ios->bus_width == MMC_BUS_WIDTH_4)
955 bw /= 2;
956 else if (ios->bus_width == MMC_BUS_WIDTH_1)
957 bw /= 8;
958
959 return bw;
960}
961
962static int sdhci_msm_bus_get_vote_for_bw(struct sdhci_msm_host *host,
963 unsigned int bw)
964{
965 unsigned int *table = host->pdata->voting_data->bw_vecs;
966 unsigned int size = host->pdata->voting_data->bw_vecs_size;
967 int i;
968
969 if (host->msm_bus_vote.is_max_bw_needed && bw)
970 return host->msm_bus_vote.max_bw_vote;
971
972 for (i = 0; i < size; i++) {
973 if (bw <= table[i])
974 break;
975 }
976
977 if (i && (i == size))
978 i--;
979
980 return i;
981}
982
983/*
984 * This function must be called with host lock acquired.
985 * Caller of this function should also ensure that msm bus client
986 * handle is not null.
987 */
988static inline int sdhci_msm_bus_set_vote(struct sdhci_msm_host *msm_host,
989 int vote,
990 unsigned long flags)
991{
992 struct sdhci_host *host = platform_get_drvdata(msm_host->pdev);
993 int rc = 0;
994
995 if (vote != msm_host->msm_bus_vote.curr_vote) {
996 spin_unlock_irqrestore(&host->lock, flags);
997 rc = msm_bus_scale_client_update_request(
998 msm_host->msm_bus_vote.client_handle, vote);
999 spin_lock_irqsave(&host->lock, flags);
1000 if (rc) {
1001 pr_err("%s: msm_bus_scale_client_update_request() failed: bus_client_handle=0x%x, vote=%d, err=%d\n",
1002 mmc_hostname(host->mmc),
1003 msm_host->msm_bus_vote.client_handle, vote, rc);
1004 goto out;
1005 }
1006 msm_host->msm_bus_vote.curr_vote = vote;
1007 }
1008out:
1009 return rc;
1010}
1011
1012/*
1013 * Internal work. Work to set 0 bandwidth for msm bus.
1014 */
1015static void sdhci_msm_bus_work(struct work_struct *work)
1016{
1017 struct sdhci_msm_host *msm_host;
1018 struct sdhci_host *host;
1019 unsigned long flags;
1020
1021 msm_host = container_of(work, struct sdhci_msm_host,
1022 msm_bus_vote.vote_work.work);
1023 host = platform_get_drvdata(msm_host->pdev);
1024
1025 if (!msm_host->msm_bus_vote.client_handle)
1026 return;
1027
1028 spin_lock_irqsave(&host->lock, flags);
1029 /* don't vote for 0 bandwidth if any request is in progress */
1030 if (!host->mrq) {
1031 sdhci_msm_bus_set_vote(msm_host,
1032 msm_host->msm_bus_vote.min_bw_vote, flags);
1033 } else
1034 pr_warning("%s: %s: Transfer in progress. skipping bus voting to 0 bandwidth\n",
1035 mmc_hostname(host->mmc), __func__);
1036 spin_unlock_irqrestore(&host->lock, flags);
1037}
1038
1039/*
1040 * This function cancels any scheduled delayed work and sets the bus
1041 * vote based on bw (bandwidth) argument.
1042 */
1043static void sdhci_msm_bus_cancel_work_and_set_vote(struct sdhci_host *host,
1044 unsigned int bw)
1045{
1046 int vote;
1047 unsigned long flags;
1048 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1049 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1050
1051 cancel_delayed_work_sync(&msm_host->msm_bus_vote.vote_work);
1052 spin_lock_irqsave(&host->lock, flags);
1053 vote = sdhci_msm_bus_get_vote_for_bw(msm_host, bw);
1054 sdhci_msm_bus_set_vote(msm_host, vote, flags);
1055 spin_unlock_irqrestore(&host->lock, flags);
1056}
1057
1058#define MSM_MMC_BUS_VOTING_DELAY 200 /* msecs */
1059
1060/* This function queues a work which will set the bandwidth requiement to 0 */
1061static void sdhci_msm_bus_queue_work(struct sdhci_host *host)
1062{
1063 unsigned long flags;
1064 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1065 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1066
1067 spin_lock_irqsave(&host->lock, flags);
1068 if (msm_host->msm_bus_vote.min_bw_vote !=
1069 msm_host->msm_bus_vote.curr_vote)
1070 queue_delayed_work(system_wq,
1071 &msm_host->msm_bus_vote.vote_work,
1072 msecs_to_jiffies(MSM_MMC_BUS_VOTING_DELAY));
1073 spin_unlock_irqrestore(&host->lock, flags);
1074}
1075
1076static int sdhci_msm_bus_register(struct sdhci_msm_host *host,
1077 struct platform_device *pdev)
1078{
1079 int rc = 0;
1080 struct msm_bus_scale_pdata *bus_pdata;
1081
1082 struct sdhci_msm_bus_voting_data *data;
1083 struct device *dev = &pdev->dev;
1084
1085 data = devm_kzalloc(dev,
1086 sizeof(struct sdhci_msm_bus_voting_data), GFP_KERNEL);
1087 if (!data) {
1088 dev_err(&pdev->dev,
1089 "%s: failed to allocate memory\n", __func__);
1090 rc = -ENOMEM;
1091 goto out;
1092 }
1093 data->bus_pdata = msm_bus_cl_get_pdata(pdev);
1094 if (data->bus_pdata) {
1095 rc = sdhci_msm_dt_get_array(dev, "qcom,bus-bw-vectors-bps",
1096 &data->bw_vecs, &data->bw_vecs_size, 0);
1097 if (rc) {
1098 dev_err(&pdev->dev,
1099 "%s: Failed to get bus-bw-vectors-bps\n",
1100 __func__);
1101 goto out;
1102 }
1103 host->pdata->voting_data = data;
1104 }
1105 if (host->pdata->voting_data &&
1106 host->pdata->voting_data->bus_pdata &&
1107 host->pdata->voting_data->bw_vecs &&
1108 host->pdata->voting_data->bw_vecs_size) {
1109
1110 bus_pdata = host->pdata->voting_data->bus_pdata;
1111 host->msm_bus_vote.client_handle =
1112 msm_bus_scale_register_client(bus_pdata);
1113 if (!host->msm_bus_vote.client_handle) {
1114 dev_err(&pdev->dev, "msm_bus_scale_register_client()\n");
1115 rc = -EFAULT;
1116 goto out;
1117 }
1118 /* cache the vote index for minimum and maximum bandwidth */
1119 host->msm_bus_vote.min_bw_vote =
1120 sdhci_msm_bus_get_vote_for_bw(host, 0);
1121 host->msm_bus_vote.max_bw_vote =
1122 sdhci_msm_bus_get_vote_for_bw(host, UINT_MAX);
1123 } else {
1124 devm_kfree(dev, data);
1125 }
1126
1127out:
1128 return rc;
1129}
1130
1131static void sdhci_msm_bus_unregister(struct sdhci_msm_host *host)
1132{
1133 if (host->msm_bus_vote.client_handle)
1134 msm_bus_scale_unregister_client(
1135 host->msm_bus_vote.client_handle);
1136}
1137
1138static void sdhci_msm_bus_voting(struct sdhci_host *host, u32 enable)
1139{
1140 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1141 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1142 struct mmc_ios *ios = &host->mmc->ios;
1143 unsigned int bw;
1144
1145 if (!msm_host->msm_bus_vote.client_handle)
1146 return;
1147
1148 bw = sdhci_get_bw_required(host, ios);
1149 if (enable)
1150 sdhci_msm_bus_cancel_work_and_set_vote(host, bw);
1151 else
1152 sdhci_msm_bus_queue_work(host);
1153}
1154
Asutosh Das0ef24812012-12-18 16:14:02 +05301155/* Regulator utility functions */
1156static int sdhci_msm_vreg_init_reg(struct device *dev,
1157 struct sdhci_msm_reg_data *vreg)
1158{
1159 int ret = 0;
1160
1161 /* check if regulator is already initialized? */
1162 if (vreg->reg)
1163 goto out;
1164
1165 /* Get the regulator handle */
1166 vreg->reg = devm_regulator_get(dev, vreg->name);
1167 if (IS_ERR(vreg->reg)) {
1168 ret = PTR_ERR(vreg->reg);
1169 pr_err("%s: devm_regulator_get(%s) failed. ret=%d\n",
1170 __func__, vreg->name, ret);
1171 goto out;
1172 }
1173
1174 /* sanity check */
1175 if (!vreg->high_vol_level || !vreg->hpm_uA) {
1176 pr_err("%s: %s invalid constraints specified\n",
1177 __func__, vreg->name);
1178 ret = -EINVAL;
1179 }
1180
1181out:
1182 return ret;
1183}
1184
1185static void sdhci_msm_vreg_deinit_reg(struct sdhci_msm_reg_data *vreg)
1186{
1187 if (vreg->reg)
1188 devm_regulator_put(vreg->reg);
1189}
1190
1191static int sdhci_msm_vreg_set_optimum_mode(struct sdhci_msm_reg_data
1192 *vreg, int uA_load)
1193{
1194 int ret = 0;
1195
1196 /*
1197 * regulators that do not support regulator_set_voltage also
1198 * do not support regulator_set_optimum_mode
1199 */
1200 if (vreg->set_voltage_sup) {
1201 ret = regulator_set_load(vreg->reg, uA_load);
1202 if (ret < 0)
1203 pr_err("%s: regulator_set_load(reg=%s,uA_load=%d) failed. ret=%d\n",
1204 __func__, vreg->name, uA_load, ret);
1205 else
1206 /*
1207 * regulator_set_load() can return non zero
1208 * value even for success case.
1209 */
1210 ret = 0;
1211 }
1212 return ret;
1213}
1214
1215static int sdhci_msm_vreg_set_voltage(struct sdhci_msm_reg_data *vreg,
1216 int min_uV, int max_uV)
1217{
1218 int ret = 0;
1219
1220 ret = regulator_set_voltage(vreg->reg, min_uV, max_uV);
1221 if (ret) {
1222 pr_err("%s: regulator_set_voltage(%s)failed. min_uV=%d,max_uV=%d,ret=%d\n",
1223 __func__, vreg->name, min_uV, max_uV, ret);
1224 }
1225
1226 return ret;
1227}
1228
1229static int sdhci_msm_vreg_enable(struct sdhci_msm_reg_data *vreg)
1230{
1231 int ret = 0;
1232
1233 /* Put regulator in HPM (high power mode) */
1234 ret = sdhci_msm_vreg_set_optimum_mode(vreg, vreg->hpm_uA);
1235 if (ret < 0)
1236 return ret;
1237
1238 if (!vreg->is_enabled) {
1239 /* Set voltage level */
1240 ret = sdhci_msm_vreg_set_voltage(vreg, vreg->high_vol_level,
1241 vreg->high_vol_level);
1242 if (ret)
1243 return ret;
1244 }
1245 ret = regulator_enable(vreg->reg);
1246 if (ret) {
1247 pr_err("%s: regulator_enable(%s) failed. ret=%d\n",
1248 __func__, vreg->name, ret);
1249 return ret;
1250 }
1251 vreg->is_enabled = true;
1252 return ret;
1253}
1254
1255static int sdhci_msm_vreg_disable(struct sdhci_msm_reg_data *vreg)
1256{
1257 int ret = 0;
1258
1259 /* Never disable regulator marked as always_on */
1260 if (vreg->is_enabled && !vreg->is_always_on) {
1261 ret = regulator_disable(vreg->reg);
1262 if (ret) {
1263 pr_err("%s: regulator_disable(%s) failed. ret=%d\n",
1264 __func__, vreg->name, ret);
1265 goto out;
1266 }
1267 vreg->is_enabled = false;
1268
1269 ret = sdhci_msm_vreg_set_optimum_mode(vreg, 0);
1270 if (ret < 0)
1271 goto out;
1272
1273 /* Set min. voltage level to 0 */
1274 ret = sdhci_msm_vreg_set_voltage(vreg, 0, vreg->high_vol_level);
1275 if (ret)
1276 goto out;
1277 } else if (vreg->is_enabled && vreg->is_always_on) {
1278 if (vreg->lpm_sup) {
1279 /* Put always_on regulator in LPM (low power mode) */
1280 ret = sdhci_msm_vreg_set_optimum_mode(vreg,
1281 vreg->lpm_uA);
1282 if (ret < 0)
1283 goto out;
1284 }
1285 }
1286out:
1287 return ret;
1288}
1289
1290static int sdhci_msm_setup_vreg(struct sdhci_msm_pltfm_data *pdata,
1291 bool enable, bool is_init)
1292{
1293 int ret = 0, i;
1294 struct sdhci_msm_slot_reg_data *curr_slot;
1295 struct sdhci_msm_reg_data *vreg_table[2];
1296
1297 curr_slot = pdata->vreg_data;
1298 if (!curr_slot) {
1299 pr_debug("%s: vreg info unavailable,assuming the slot is powered by always on domain\n",
1300 __func__);
1301 goto out;
1302 }
1303
1304 vreg_table[0] = curr_slot->vdd_data;
1305 vreg_table[1] = curr_slot->vdd_io_data;
1306
1307 for (i = 0; i < ARRAY_SIZE(vreg_table); i++) {
1308 if (vreg_table[i]) {
1309 if (enable)
1310 ret = sdhci_msm_vreg_enable(vreg_table[i]);
1311 else
1312 ret = sdhci_msm_vreg_disable(vreg_table[i]);
1313 if (ret)
1314 goto out;
1315 }
1316 }
1317out:
1318 return ret;
1319}
1320
1321/*
1322 * Reset vreg by ensuring it is off during probe. A call
1323 * to enable vreg is needed to balance disable vreg
1324 */
1325static int sdhci_msm_vreg_reset(struct sdhci_msm_pltfm_data *pdata)
1326{
1327 int ret;
1328
1329 ret = sdhci_msm_setup_vreg(pdata, 1, true);
1330 if (ret)
1331 return ret;
1332 ret = sdhci_msm_setup_vreg(pdata, 0, true);
1333 return ret;
1334}
1335
1336/* This init function should be called only once for each SDHC slot */
1337static int sdhci_msm_vreg_init(struct device *dev,
1338 struct sdhci_msm_pltfm_data *pdata,
1339 bool is_init)
1340{
1341 int ret = 0;
1342 struct sdhci_msm_slot_reg_data *curr_slot;
1343 struct sdhci_msm_reg_data *curr_vdd_reg, *curr_vdd_io_reg;
1344
1345 curr_slot = pdata->vreg_data;
1346 if (!curr_slot)
1347 goto out;
1348
1349 curr_vdd_reg = curr_slot->vdd_data;
1350 curr_vdd_io_reg = curr_slot->vdd_io_data;
1351
1352 if (!is_init)
1353 /* Deregister all regulators from regulator framework */
1354 goto vdd_io_reg_deinit;
1355
1356 /*
1357 * Get the regulator handle from voltage regulator framework
1358 * and then try to set the voltage level for the regulator
1359 */
1360 if (curr_vdd_reg) {
1361 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_reg);
1362 if (ret)
1363 goto out;
1364 }
1365 if (curr_vdd_io_reg) {
1366 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_io_reg);
1367 if (ret)
1368 goto vdd_reg_deinit;
1369 }
1370 ret = sdhci_msm_vreg_reset(pdata);
1371 if (ret)
1372 dev_err(dev, "vreg reset failed (%d)\n", ret);
1373 goto out;
1374
1375vdd_io_reg_deinit:
1376 if (curr_vdd_io_reg)
1377 sdhci_msm_vreg_deinit_reg(curr_vdd_io_reg);
1378vdd_reg_deinit:
1379 if (curr_vdd_reg)
1380 sdhci_msm_vreg_deinit_reg(curr_vdd_reg);
1381out:
1382 return ret;
1383}
1384
1385
1386static int sdhci_msm_set_vdd_io_vol(struct sdhci_msm_pltfm_data *pdata,
1387 enum vdd_io_level level,
1388 unsigned int voltage_level)
1389{
1390 int ret = 0;
1391 int set_level;
1392 struct sdhci_msm_reg_data *vdd_io_reg;
1393
1394 if (!pdata->vreg_data)
1395 return ret;
1396
1397 vdd_io_reg = pdata->vreg_data->vdd_io_data;
1398 if (vdd_io_reg && vdd_io_reg->is_enabled) {
1399 switch (level) {
1400 case VDD_IO_LOW:
1401 set_level = vdd_io_reg->low_vol_level;
1402 break;
1403 case VDD_IO_HIGH:
1404 set_level = vdd_io_reg->high_vol_level;
1405 break;
1406 case VDD_IO_SET_LEVEL:
1407 set_level = voltage_level;
1408 break;
1409 default:
1410 pr_err("%s: invalid argument level = %d",
1411 __func__, level);
1412 ret = -EINVAL;
1413 return ret;
1414 }
1415 ret = sdhci_msm_vreg_set_voltage(vdd_io_reg, set_level,
1416 set_level);
1417 }
1418 return ret;
1419}
1420
1421static irqreturn_t sdhci_msm_pwr_irq(int irq, void *data)
1422{
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001423 struct sdhci_host *host = (struct sdhci_host *)data;
1424 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1425 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Das0ef24812012-12-18 16:14:02 +05301426 u8 irq_status = 0;
1427 u8 irq_ack = 0;
1428 int ret = 0;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301429 int pwr_state = 0, io_level = 0;
1430 unsigned long flags;
Asutosh Das0ef24812012-12-18 16:14:02 +05301431
1432 irq_status = readb_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
1433 pr_debug("%s: Received IRQ(%d), status=0x%x\n",
1434 mmc_hostname(msm_host->mmc), irq, irq_status);
1435
1436 /* Clear the interrupt */
1437 writeb_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
1438 /*
1439 * SDHC has core_mem and hc_mem device memory and these memory
1440 * addresses do not fall within 1KB region. Hence, any update to
1441 * core_mem address space would require an mb() to ensure this gets
1442 * completed before its next update to registers within hc_mem.
1443 */
1444 mb();
1445
1446 /* Handle BUS ON/OFF*/
1447 if (irq_status & CORE_PWRCTL_BUS_ON) {
1448 ret = sdhci_msm_setup_vreg(msm_host->pdata, true, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301449 if (!ret) {
Asutosh Das0ef24812012-12-18 16:14:02 +05301450 ret = sdhci_msm_setup_pins(msm_host->pdata, true);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301451 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
1452 VDD_IO_HIGH, 0);
1453 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301454 if (ret)
1455 irq_ack |= CORE_PWRCTL_BUS_FAIL;
1456 else
1457 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301458
1459 pwr_state = REQ_BUS_ON;
1460 io_level = REQ_IO_HIGH;
Asutosh Das0ef24812012-12-18 16:14:02 +05301461 }
1462 if (irq_status & CORE_PWRCTL_BUS_OFF) {
1463 ret = sdhci_msm_setup_vreg(msm_host->pdata, false, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301464 if (!ret) {
Asutosh Das0ef24812012-12-18 16:14:02 +05301465 ret = sdhci_msm_setup_pins(msm_host->pdata, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301466 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
1467 VDD_IO_LOW, 0);
1468 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301469 if (ret)
1470 irq_ack |= CORE_PWRCTL_BUS_FAIL;
1471 else
1472 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301473
1474 pwr_state = REQ_BUS_OFF;
1475 io_level = REQ_IO_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05301476 }
1477 /* Handle IO LOW/HIGH */
1478 if (irq_status & CORE_PWRCTL_IO_LOW) {
1479 /* Switch voltage Low */
1480 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_LOW, 0);
1481 if (ret)
1482 irq_ack |= CORE_PWRCTL_IO_FAIL;
1483 else
1484 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301485
1486 io_level = REQ_IO_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05301487 }
1488 if (irq_status & CORE_PWRCTL_IO_HIGH) {
1489 /* Switch voltage High */
1490 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_HIGH, 0);
1491 if (ret)
1492 irq_ack |= CORE_PWRCTL_IO_FAIL;
1493 else
1494 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301495
1496 io_level = REQ_IO_HIGH;
Asutosh Das0ef24812012-12-18 16:14:02 +05301497 }
1498
1499 /* ACK status to the core */
1500 writeb_relaxed(irq_ack, (msm_host->core_mem + CORE_PWRCTL_CTL));
1501 /*
1502 * SDHC has core_mem and hc_mem device memory and these memory
1503 * addresses do not fall within 1KB region. Hence, any update to
1504 * core_mem address space would require an mb() to ensure this gets
1505 * completed before its next update to registers within hc_mem.
1506 */
1507 mb();
1508
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301509 if (io_level & REQ_IO_HIGH)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001510 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
1511 ~CORE_IO_PAD_PWR_SWITCH),
1512 host->ioaddr + CORE_VENDOR_SPEC);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301513 else if (io_level & REQ_IO_LOW)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001514 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
1515 CORE_IO_PAD_PWR_SWITCH),
1516 host->ioaddr + CORE_VENDOR_SPEC);
1517 mb();
1518
Asutosh Das0ef24812012-12-18 16:14:02 +05301519 pr_debug("%s: Handled IRQ(%d), ret=%d, ack=0x%x\n",
1520 mmc_hostname(msm_host->mmc), irq, ret, irq_ack);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301521 spin_lock_irqsave(&host->lock, flags);
1522 if (pwr_state)
1523 msm_host->curr_pwr_state = pwr_state;
1524 if (io_level)
1525 msm_host->curr_io_level = io_level;
1526 complete(&msm_host->pwr_irq_completion);
1527 spin_unlock_irqrestore(&host->lock, flags);
1528
Asutosh Das0ef24812012-12-18 16:14:02 +05301529 return IRQ_HANDLED;
1530}
1531
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301532static ssize_t
1533show_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
1534 char *buf)
1535{
1536 struct sdhci_host *host = dev_get_drvdata(dev);
1537 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1538 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1539
1540 return snprintf(buf, PAGE_SIZE, "%u\n",
1541 msm_host->msm_bus_vote.is_max_bw_needed);
1542}
1543
1544static ssize_t
1545store_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
1546 const char *buf, size_t count)
1547{
1548 struct sdhci_host *host = dev_get_drvdata(dev);
1549 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1550 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1551 uint32_t value;
1552 unsigned long flags;
1553
1554 if (!kstrtou32(buf, 0, &value)) {
1555 spin_lock_irqsave(&host->lock, flags);
1556 msm_host->msm_bus_vote.is_max_bw_needed = !!value;
1557 spin_unlock_irqrestore(&host->lock, flags);
1558 }
1559 return count;
1560}
1561
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301562static void sdhci_msm_check_power_status(struct sdhci_host *host, u32 req_type)
Asutosh Das0ef24812012-12-18 16:14:02 +05301563{
1564 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1565 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301566 unsigned long flags;
1567 bool done = false;
Asutosh Das0ef24812012-12-18 16:14:02 +05301568
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301569 spin_lock_irqsave(&host->lock, flags);
1570 pr_debug("%s: %s: request %d curr_pwr_state %x curr_io_level %x\n",
1571 mmc_hostname(host->mmc), __func__, req_type,
1572 msm_host->curr_pwr_state, msm_host->curr_io_level);
1573 if ((req_type & msm_host->curr_pwr_state) ||
1574 (req_type & msm_host->curr_io_level))
1575 done = true;
1576 spin_unlock_irqrestore(&host->lock, flags);
Asutosh Das0ef24812012-12-18 16:14:02 +05301577
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301578 /*
1579 * This is needed here to hanlde a case where IRQ gets
1580 * triggered even before this function is called so that
1581 * x->done counter of completion gets reset. Otherwise,
1582 * next call to wait_for_completion returns immediately
1583 * without actually waiting for the IRQ to be handled.
1584 */
1585 if (done)
1586 init_completion(&msm_host->pwr_irq_completion);
1587 else
1588 wait_for_completion(&msm_host->pwr_irq_completion);
1589
1590 pr_debug("%s: %s: request %d done\n", mmc_hostname(host->mmc),
1591 __func__, req_type);
Asutosh Das0ef24812012-12-18 16:14:02 +05301592}
1593
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001594static void sdhci_msm_toggle_cdr(struct sdhci_host *host, bool enable)
1595{
1596 if (enable)
1597 writel_relaxed((readl_relaxed(host->ioaddr +
1598 CORE_DLL_CONFIG) | CORE_CDR_EN),
1599 host->ioaddr + CORE_DLL_CONFIG);
1600 else
1601 writel_relaxed((readl_relaxed(host->ioaddr +
1602 CORE_DLL_CONFIG) & ~CORE_CDR_EN),
1603 host->ioaddr + CORE_DLL_CONFIG);
1604}
1605
Asutosh Das648f9d12013-01-10 21:11:04 +05301606static unsigned int sdhci_msm_max_segs(void)
1607{
1608 return SDHCI_MSM_MAX_SEGMENTS;
1609}
1610
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301611static unsigned int sdhci_msm_get_min_clock(struct sdhci_host *host)
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301612{
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301613 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1614 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301615
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301616 return msm_host->pdata->sup_clk_table[0];
1617}
1618
1619static unsigned int sdhci_msm_get_max_clock(struct sdhci_host *host)
1620{
1621 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1622 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1623 int max_clk_index = msm_host->pdata->sup_clk_cnt;
1624
1625 return msm_host->pdata->sup_clk_table[max_clk_index - 1];
1626}
1627
1628static unsigned int sdhci_msm_get_sup_clk_rate(struct sdhci_host *host,
1629 u32 req_clk)
1630{
1631 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1632 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1633 unsigned int sel_clk = -1;
1634 unsigned char cnt;
1635
1636 if (req_clk < sdhci_msm_get_min_clock(host)) {
1637 sel_clk = sdhci_msm_get_min_clock(host);
1638 return sel_clk;
1639 }
1640
1641 for (cnt = 0; cnt < msm_host->pdata->sup_clk_cnt; cnt++) {
1642 if (msm_host->pdata->sup_clk_table[cnt] > req_clk) {
1643 break;
1644 } else if (msm_host->pdata->sup_clk_table[cnt] == req_clk) {
1645 sel_clk = msm_host->pdata->sup_clk_table[cnt];
1646 break;
1647 } else {
1648 sel_clk = msm_host->pdata->sup_clk_table[cnt];
1649 }
1650 }
1651 return sel_clk;
1652}
1653
1654static int sdhci_msm_prepare_clocks(struct sdhci_host *host, bool enable)
1655{
1656 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1657 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1658 int rc = 0;
1659
1660 if (enable && !atomic_read(&msm_host->clks_on)) {
1661 pr_debug("%s: request to enable clocks\n",
1662 mmc_hostname(host->mmc));
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301663 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
1664 rc = clk_prepare_enable(msm_host->bus_clk);
1665 if (rc) {
1666 pr_err("%s: %s: failed to enable the bus-clock with error %d\n",
1667 mmc_hostname(host->mmc), __func__, rc);
1668 goto out;
1669 }
1670 }
1671 if (!IS_ERR(msm_host->pclk)) {
1672 rc = clk_prepare_enable(msm_host->pclk);
1673 if (rc) {
1674 pr_err("%s: %s: failed to enable the pclk with error %d\n",
1675 mmc_hostname(host->mmc), __func__, rc);
1676 goto disable_bus_clk;
1677 }
1678 }
1679 rc = clk_prepare_enable(msm_host->clk);
1680 if (rc) {
1681 pr_err("%s: %s: failed to enable the host-clk with error %d\n",
1682 mmc_hostname(host->mmc), __func__, rc);
1683 goto disable_pclk;
1684 }
1685 mb();
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301686
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301687 } else if (!enable && atomic_read(&msm_host->clks_on)) {
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301688 pr_debug("%s: request to disable clocks\n",
1689 mmc_hostname(host->mmc));
1690 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
1691 mb();
1692 clk_disable_unprepare(msm_host->clk);
1693 if (!IS_ERR(msm_host->pclk))
1694 clk_disable_unprepare(msm_host->pclk);
1695 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
1696 clk_disable_unprepare(msm_host->bus_clk);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301697 }
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301698 atomic_set(&msm_host->clks_on, enable);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301699 goto out;
1700disable_pclk:
1701 if (!IS_ERR_OR_NULL(msm_host->pclk))
1702 clk_disable_unprepare(msm_host->pclk);
1703disable_bus_clk:
1704 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
1705 clk_disable_unprepare(msm_host->bus_clk);
1706out:
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301707 return rc;
1708}
1709
1710static void sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock)
1711{
1712 int rc;
1713 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1714 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1715 struct mmc_ios curr_ios = host->mmc->ios;
1716 u32 sup_clock, ddr_clock;
1717
1718 if (!clock) {
1719 sdhci_msm_prepare_clocks(host, false);
1720 host->clock = clock;
1721 goto out;
1722 }
1723
1724 rc = sdhci_msm_prepare_clocks(host, true);
1725 if (rc)
1726 goto out;
1727
1728 sup_clock = sdhci_msm_get_sup_clk_rate(host, clock);
1729 if (curr_ios.timing == MMC_TIMING_UHS_DDR50) {
1730 /*
1731 * The SDHC requires internal clock frequency to be double the
1732 * actual clock that will be set for DDR mode. The controller
1733 * uses the faster clock(100MHz) for some of its parts and send
1734 * the actual required clock (50MHz) to the card.
1735 */
1736 ddr_clock = clock * 2;
1737 sup_clock = sdhci_msm_get_sup_clk_rate(host,
1738 ddr_clock);
1739 }
1740 if (sup_clock != msm_host->clk_rate) {
1741 pr_debug("%s: %s: setting clk rate to %u\n",
1742 mmc_hostname(host->mmc), __func__, sup_clock);
1743 rc = clk_set_rate(msm_host->clk, sup_clock);
1744 if (rc) {
1745 pr_err("%s: %s: Failed to set rate %u for host-clk : %d\n",
1746 mmc_hostname(host->mmc), __func__,
1747 sup_clock, rc);
1748 goto out;
1749 }
1750 msm_host->clk_rate = sup_clock;
1751 host->clock = clock;
1752 }
1753out:
1754 sdhci_set_clock(host, clock);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301755}
1756
Sahitya Tummala14613432013-03-21 11:13:25 +05301757static void sdhci_msm_set_uhs_signaling(struct sdhci_host *host,
1758 unsigned int uhs)
1759{
1760 u16 ctrl_2;
1761
1762 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1763 /* Select Bus Speed Mode for host */
1764 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1765 if (uhs == MMC_TIMING_MMC_HS200)
1766 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1767 else if (uhs == MMC_TIMING_UHS_SDR12)
1768 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1769 else if (uhs == MMC_TIMING_UHS_SDR25)
1770 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1771 else if (uhs == MMC_TIMING_UHS_SDR50)
1772 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1773 else if (uhs == MMC_TIMING_UHS_SDR104)
1774 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1775 else if (uhs == MMC_TIMING_UHS_DDR50)
1776 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301777 /*
1778 * When clock frquency is less than 100MHz, the feedback clock must be
1779 * provided and DLL must not be used so that tuning can be skipped. To
1780 * provide feedback clock, the mode selection can be any value less
1781 * than 3'b011 in bits [2:0] of HOST CONTROL2 register.
1782 */
1783 if (host->clock <= (100 * 1000 * 1000) &&
1784 (uhs == MMC_TIMING_MMC_HS200 ||
1785 uhs == MMC_TIMING_UHS_SDR104))
1786 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1787
Sahitya Tummala14613432013-03-21 11:13:25 +05301788 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1789
1790}
1791
Asutosh Das0ef24812012-12-18 16:14:02 +05301792static struct sdhci_ops sdhci_msm_ops = {
Sahitya Tummala14613432013-03-21 11:13:25 +05301793 .set_uhs_signaling = sdhci_msm_set_uhs_signaling,
Asutosh Das0ef24812012-12-18 16:14:02 +05301794 .check_power_status = sdhci_msm_check_power_status,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001795 .platform_execute_tuning = sdhci_msm_execute_tuning,
1796 .toggle_cdr = sdhci_msm_toggle_cdr,
Asutosh Das648f9d12013-01-10 21:11:04 +05301797 .get_max_segments = sdhci_msm_max_segs,
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301798 .set_clock = sdhci_msm_set_clock,
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301799 .platform_bus_voting = sdhci_msm_bus_voting,
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301800 .get_min_clock = sdhci_msm_get_min_clock,
1801 .get_max_clock = sdhci_msm_get_max_clock,
Asutosh Das0ef24812012-12-18 16:14:02 +05301802};
1803
1804static int sdhci_msm_probe(struct platform_device *pdev)
1805{
1806 struct sdhci_host *host;
1807 struct sdhci_pltfm_host *pltfm_host;
1808 struct sdhci_msm_host *msm_host;
1809 struct resource *core_memres = NULL;
1810 int ret = 0, pwr_irq = 0, dead = 0;
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07001811 u32 host_version;
Asutosh Das0ef24812012-12-18 16:14:02 +05301812
1813 pr_debug("%s: Enter %s\n", dev_name(&pdev->dev), __func__);
1814 msm_host = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_msm_host),
1815 GFP_KERNEL);
1816 if (!msm_host) {
1817 ret = -ENOMEM;
1818 goto out;
1819 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301820
1821 msm_host->sdhci_msm_pdata.ops = &sdhci_msm_ops;
1822 host = sdhci_pltfm_init(pdev, &msm_host->sdhci_msm_pdata, 0);
1823 if (IS_ERR(host)) {
1824 ret = PTR_ERR(host);
1825 goto out;
1826 }
1827
1828 pltfm_host = sdhci_priv(host);
1829 pltfm_host->priv = msm_host;
1830 msm_host->mmc = host->mmc;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301831 msm_host->pdev = pdev;
Asutosh Das0ef24812012-12-18 16:14:02 +05301832
1833 /* Extract platform data */
1834 if (pdev->dev.of_node) {
Venkat Gopalakrishnan270580a2013-03-11 12:17:57 -07001835 ret = of_alias_get_id(pdev->dev.of_node, "sdhc");
1836 if (ret < 0) {
1837 dev_err(&pdev->dev, "Failed to get slot index %d\n",
1838 ret);
1839 goto pltfm_free;
1840 }
1841 if (disable_slots & (1 << (ret - 1))) {
1842 dev_info(&pdev->dev, "%s: Slot %d disabled\n", __func__,
1843 ret);
1844 ret = -ENODEV;
1845 goto pltfm_free;
1846 }
1847
Asutosh Das0ef24812012-12-18 16:14:02 +05301848 msm_host->pdata = sdhci_msm_populate_pdata(&pdev->dev);
1849 if (!msm_host->pdata) {
1850 dev_err(&pdev->dev, "DT parsing error\n");
1851 goto pltfm_free;
1852 }
1853 } else {
1854 dev_err(&pdev->dev, "No device tree node\n");
1855 goto pltfm_free;
1856 }
1857
1858 /* Setup Clocks */
1859
1860 /* Setup SDCC bus voter clock. */
1861 msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus_clk");
1862 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
1863 /* Vote for max. clk rate for max. performance */
1864 ret = clk_set_rate(msm_host->bus_clk, INT_MAX);
1865 if (ret)
1866 goto pltfm_free;
1867 ret = clk_prepare_enable(msm_host->bus_clk);
1868 if (ret)
1869 goto pltfm_free;
1870 }
1871
1872 /* Setup main peripheral bus clock */
1873 msm_host->pclk = devm_clk_get(&pdev->dev, "iface_clk");
1874 if (!IS_ERR(msm_host->pclk)) {
1875 ret = clk_prepare_enable(msm_host->pclk);
1876 if (ret)
1877 goto bus_clk_disable;
1878 }
1879
1880 /* Setup SDC MMC clock */
1881 msm_host->clk = devm_clk_get(&pdev->dev, "core_clk");
1882 if (IS_ERR(msm_host->clk)) {
1883 ret = PTR_ERR(msm_host->clk);
1884 goto pclk_disable;
1885 }
1886
1887 ret = clk_prepare_enable(msm_host->clk);
1888 if (ret)
1889 goto pclk_disable;
1890
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301891 /* Set to the minimum supported clock frequency */
1892 ret = clk_set_rate(msm_host->clk, sdhci_msm_get_min_clock(host));
1893 if (ret) {
1894 dev_err(&pdev->dev, "MClk rate set failed (%d)\n", ret);
1895 goto clk_disable;
1896 }
1897 msm_host->clk_rate = sdhci_msm_get_min_clock(host);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301898 atomic_set(&msm_host->clks_on, 1);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301899
Asutosh Das0ef24812012-12-18 16:14:02 +05301900 /* Setup regulators */
1901 ret = sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, true);
1902 if (ret) {
1903 dev_err(&pdev->dev, "Regulator setup failed (%d)\n", ret);
1904 goto clk_disable;
1905 }
1906
1907 /* Reset the core and Enable SDHC mode */
1908 core_memres = platform_get_resource_byname(pdev,
1909 IORESOURCE_MEM, "core_mem");
1910 msm_host->core_mem = devm_ioremap(&pdev->dev, core_memres->start,
1911 resource_size(core_memres));
1912
1913 if (!msm_host->core_mem) {
1914 dev_err(&pdev->dev, "Failed to remap registers\n");
1915 ret = -ENOMEM;
1916 goto vreg_deinit;
1917 }
1918
1919 /* Set SW_RST bit in POWER register (Offset 0x0) */
1920 writel_relaxed(CORE_SW_RST, msm_host->core_mem + CORE_POWER);
1921 /* Set HC_MODE_EN bit in HC_MODE register */
1922 writel_relaxed(HC_MODE_EN, (msm_host->core_mem + CORE_HC_MODE));
1923
1924 /*
1925 * Following are the deviations from SDHC spec v3.0 -
1926 * 1. Card detection is handled using separate GPIO.
1927 * 2. Bus power control is handled by interacting with PMIC.
1928 */
1929 host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
1930 host->quirks |= SDHCI_QUIRK_SINGLE_POWER_WRITE;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301931 host->quirks |= SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN;
1932 host->quirks2 |= SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK;
Asutosh Das0ef24812012-12-18 16:14:02 +05301933
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07001934 host_version = readl_relaxed((host->ioaddr + SDHCI_HOST_VERSION));
1935 dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n",
1936 host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >>
1937 SDHCI_VENDOR_VER_SHIFT));
1938 if (((host_version & SDHCI_VENDOR_VER_MASK) >>
1939 SDHCI_VENDOR_VER_SHIFT) == SDHCI_VER_100) {
1940 /*
1941 * Add 40us delay in interrupt handler when
1942 * operating at initialization frequency(400KHz).
1943 */
1944 host->quirks2 |= SDHCI_QUIRK2_SLOW_INT_CLR;
1945 /*
1946 * Set Software Reset for DAT line in Software
1947 * Reset Register (Bit 2).
1948 */
1949 host->quirks2 |= SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT;
1950 }
1951
1952 /* Setup PWRCTL irq */
Asutosh Das0ef24812012-12-18 16:14:02 +05301953 pwr_irq = platform_get_irq_byname(pdev, "pwr_irq");
1954 if (pwr_irq < 0) {
1955 dev_err(&pdev->dev, "Failed to get pwr_irq by name (%d)\n",
1956 pwr_irq);
1957 goto vreg_deinit;
1958 }
1959 ret = devm_request_threaded_irq(&pdev->dev, pwr_irq, NULL,
1960 sdhci_msm_pwr_irq, IRQF_ONESHOT,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001961 dev_name(&pdev->dev), host);
Asutosh Das0ef24812012-12-18 16:14:02 +05301962 if (ret) {
1963 dev_err(&pdev->dev, "Request threaded irq(%d) failed (%d)\n",
1964 pwr_irq, ret);
1965 goto vreg_deinit;
1966 }
1967
1968 /* Enable pwr irq interrupts */
1969 writel_relaxed(INT_MASK, (msm_host->core_mem + CORE_PWRCTL_MASK));
1970
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301971#ifdef CONFIG_MMC_CLKGATE
1972 /* Set clock gating delay to be used when CONFIG_MMC_CLKGATE is set */
1973 msm_host->mmc->clkgate_delay = SDHCI_MSM_MMC_CLK_GATE_DELAY;
1974#endif
1975
Asutosh Das0ef24812012-12-18 16:14:02 +05301976 /* Set host capabilities */
1977 msm_host->mmc->caps |= msm_host->pdata->mmc_bus_width;
1978 msm_host->mmc->caps |= msm_host->pdata->caps;
Sahitya Tummala7bd99062013-02-28 12:21:58 +05301979 msm_host->mmc->caps |= MMC_CAP_HW_RESET;
Asutosh Das0ef24812012-12-18 16:14:02 +05301980 msm_host->mmc->caps2 |= msm_host->pdata->caps2;
1981 msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR;
1982 msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR_CONTROL;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301983 msm_host->mmc->caps2 |= MMC_CAP2_CLK_SCALE;
Asutosh Das0ef24812012-12-18 16:14:02 +05301984
1985 if (msm_host->pdata->nonremovable)
1986 msm_host->mmc->caps |= MMC_CAP_NONREMOVABLE;
1987
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05301988 host->cpu_dma_latency_us = msm_host->pdata->cpu_dma_latency_us;
1989
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301990 ret = sdhci_msm_bus_register(msm_host, pdev);
1991 if (ret)
1992 goto vreg_deinit;
1993
1994 if (msm_host->msm_bus_vote.client_handle)
1995 INIT_DELAYED_WORK(&msm_host->msm_bus_vote.vote_work,
1996 sdhci_msm_bus_work);
1997
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301998 init_completion(&msm_host->pwr_irq_completion);
1999
Sahitya Tummala581df132013-03-12 14:57:46 +05302000 if (gpio_is_valid(msm_host->pdata->status_gpio)) {
2001 ret = mmc_gpio_request_cd(msm_host->mmc,
2002 msm_host->pdata->status_gpio, 0);
2003 if (ret) {
2004 dev_err(&pdev->dev, "%s: Failed to request card detection IRQ %d\n",
2005 __func__, ret);
2006 goto bus_unregister;
2007 }
2008 }
2009
Sahitya Tummalaeaa21862013-03-20 19:34:59 +05302010 if (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(32))) {
2011 host->dma_mask = DMA_BIT_MASK(32);
2012 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2013 } else {
2014 dev_err(&pdev->dev, "%s: Failed to set dma mask\n", __func__);
2015 }
2016
Asutosh Das0ef24812012-12-18 16:14:02 +05302017 ret = sdhci_add_host(host);
2018 if (ret) {
2019 dev_err(&pdev->dev, "Add host failed (%d)\n", ret);
Sahitya Tummala581df132013-03-12 14:57:46 +05302020 goto vreg_deinit;
Asutosh Das0ef24812012-12-18 16:14:02 +05302021 }
2022
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302023 msm_host->msm_bus_vote.max_bus_bw.show = show_sdhci_max_bus_bw;
2024 msm_host->msm_bus_vote.max_bus_bw.store = store_sdhci_max_bus_bw;
2025 sysfs_attr_init(&msm_host->msm_bus_vote.max_bus_bw.attr);
2026 msm_host->msm_bus_vote.max_bus_bw.attr.name = "max_bus_bw";
2027 msm_host->msm_bus_vote.max_bus_bw.attr.mode = S_IRUGO | S_IWUSR;
2028 ret = device_create_file(&pdev->dev,
2029 &msm_host->msm_bus_vote.max_bus_bw);
2030 if (ret)
2031 goto remove_host;
2032
Asutosh Das0ef24812012-12-18 16:14:02 +05302033 /* Successful initialization */
2034 goto out;
2035
2036remove_host:
2037 dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
2038 sdhci_remove_host(host, dead);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302039bus_unregister:
2040 sdhci_msm_bus_unregister(msm_host);
Asutosh Das0ef24812012-12-18 16:14:02 +05302041vreg_deinit:
2042 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
2043clk_disable:
2044 if (!IS_ERR(msm_host->clk))
2045 clk_disable_unprepare(msm_host->clk);
2046pclk_disable:
2047 if (!IS_ERR(msm_host->pclk))
2048 clk_disable_unprepare(msm_host->pclk);
2049bus_clk_disable:
2050 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2051 clk_disable_unprepare(msm_host->bus_clk);
2052pltfm_free:
2053 sdhci_pltfm_free(pdev);
2054out:
2055 pr_debug("%s: Exit %s\n", dev_name(&pdev->dev), __func__);
2056 return ret;
2057}
2058
2059static int sdhci_msm_remove(struct platform_device *pdev)
2060{
2061 struct sdhci_host *host = platform_get_drvdata(pdev);
2062 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2063 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2064 struct sdhci_msm_pltfm_data *pdata = msm_host->pdata;
2065 int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) ==
2066 0xffffffff);
2067
2068 pr_debug("%s: %s\n", dev_name(&pdev->dev), __func__);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302069 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Asutosh Das0ef24812012-12-18 16:14:02 +05302070 sdhci_remove_host(host, dead);
2071 sdhci_pltfm_free(pdev);
Sahitya Tummala581df132013-03-12 14:57:46 +05302072
Asutosh Das0ef24812012-12-18 16:14:02 +05302073 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302074
Asutosh Das0ef24812012-12-18 16:14:02 +05302075 if (pdata->pin_data)
2076 sdhci_msm_setup_gpio(pdata, false);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302077
2078 if (msm_host->msm_bus_vote.client_handle) {
2079 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2080 sdhci_msm_bus_unregister(msm_host);
2081 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302082 return 0;
2083}
2084
2085static const struct of_device_id sdhci_msm_dt_match[] = {
2086 {.compatible = "qcom,sdhci-msm"},
2087};
2088MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match);
2089
2090static struct platform_driver sdhci_msm_driver = {
2091 .probe = sdhci_msm_probe,
2092 .remove = sdhci_msm_remove,
2093 .driver = {
2094 .name = "sdhci_msm",
2095 .owner = THIS_MODULE,
2096 .of_match_table = sdhci_msm_dt_match,
2097 },
2098};
2099
2100module_platform_driver(sdhci_msm_driver);
2101
2102MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Secure Digital Host Controller Interface driver");
2103MODULE_LICENSE("GPL v2");