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