blob: 15e36a890e4cbf46904933a75381f0b965caea09 [file] [log] [blame]
Asutosh Das33a4ff52012-12-18 16:14:02 +05301/*
2 * drivers/mmc/host/sdhci-msm.c - Qualcomm 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 Gopalakrishnan8609a432012-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>
Asutosh Dasbbc84782013-02-11 15:31:35 +053035#include <linux/pm.h>
36#include <linux/pm_runtime.h>
Sahitya Tummala62448d92013-03-12 14:57:46 +053037#include <linux/mmc/cd-gpio.h>
Sahitya Tummala2fa7eb12013-03-20 19:34:59 +053038#include <linux/dma-mapping.h>
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -070039#include <mach/gpio.h>
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +053040#include <mach/msm_bus.h>
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +030041#include <linux/iopoll.h>
Asutosh Das33a4ff52012-12-18 16:14:02 +053042
43#include "sdhci-pltfm.h"
44
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -070045#define SDHCI_VER_100 0x2B
Asutosh Das33a4ff52012-12-18 16:14:02 +053046#define CORE_HC_MODE 0x78
47#define HC_MODE_EN 0x1
48
49#define CORE_POWER 0x0
50#define CORE_SW_RST (1 << 7)
51
52#define CORE_PWRCTL_STATUS 0xDC
53#define CORE_PWRCTL_MASK 0xE0
54#define CORE_PWRCTL_CLEAR 0xE4
55#define CORE_PWRCTL_CTL 0xE8
56
57#define CORE_PWRCTL_BUS_OFF 0x01
58#define CORE_PWRCTL_BUS_ON (1 << 1)
59#define CORE_PWRCTL_IO_LOW (1 << 2)
60#define CORE_PWRCTL_IO_HIGH (1 << 3)
61
62#define CORE_PWRCTL_BUS_SUCCESS 0x01
63#define CORE_PWRCTL_BUS_FAIL (1 << 1)
64#define CORE_PWRCTL_IO_SUCCESS (1 << 2)
65#define CORE_PWRCTL_IO_FAIL (1 << 3)
66
67#define INT_MASK 0xF
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -070068#define MAX_PHASES 16
69
70#define CORE_DLL_LOCK (1 << 7)
71#define CORE_DLL_EN (1 << 16)
72#define CORE_CDR_EN (1 << 17)
73#define CORE_CK_OUT_EN (1 << 18)
74#define CORE_CDR_EXT_EN (1 << 19)
75#define CORE_DLL_PDN (1 << 29)
76#define CORE_DLL_RST (1 << 30)
77#define CORE_DLL_CONFIG 0x100
78#define CORE_DLL_TEST_CTL 0x104
79#define CORE_DLL_STATUS 0x108
80
81#define CORE_VENDOR_SPEC 0x10C
82#define CORE_CLK_PWRSAVE (1 << 1)
83#define CORE_IO_PAD_PWR_SWITCH (1 << 16)
84
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +030085#define CORE_MCI_DATA_CTRL 0x2C
86#define CORE_MCI_DPSM_ENABLE (1 << 0)
87
88#define CORE_TESTBUS_CONFIG 0x0CC
89#define CORE_TESTBUS_ENA (1 << 3)
90#define CORE_TESTBUS_SEL2 (1 << 4)
91
92/*
93 * Waiting until end of potential AHB access for data:
94 * 16 AHB cycles (160ns for 100MHz and 320ns for 50MHz) +
95 * delay on AHB (2us) = maximum 2.32us
96 * Taking x10 times margin
97 */
98#define CORE_AHB_DATA_DELAY_US 23
99/* Waiting until end of potential AHB access for descriptor:
100 * Single (1 AHB cycle) + delay on AHB bus = max 2us
101 * INCR4 (4 AHB cycles) + delay on AHB bus = max 2us
102 * Single (1 AHB cycle) + delay on AHB bus = max 2us
103 * Total 8 us delay with margin
104 */
105#define CORE_AHB_DESC_DELAY_US 8
106
107#define CORE_SDCC_DEBUG_REG 0x124
108#define CORE_DEBUG_REG_AHB_HTRANS (3 << 12)
109
Asutosh Das3781bd82013-01-10 21:11:04 +0530110/* 8KB descriptors */
111#define SDHCI_MSM_MAX_SEGMENTS (1 << 13)
Sahitya Tummala04c3a462013-01-11 11:30:45 +0530112#define SDHCI_MSM_MMC_CLK_GATE_DELAY 200 /* msecs */
Asutosh Das3781bd82013-01-10 21:11:04 +0530113
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700114static const u32 tuning_block_64[] = {
115 0x00FF0FFF, 0xCCC3CCFF, 0xFFCC3CC3, 0xEFFEFFFE,
116 0xDDFFDFFF, 0xFBFFFBFF, 0xFF7FFFBF, 0xEFBDF777,
117 0xF0FFF0FF, 0x3CCCFC0F, 0xCFCC33CC, 0xEEFFEFFF,
118 0xFDFFFDFF, 0xFFBFFFDF, 0xFFF7FFBB, 0xDE7B7FF7
119};
120
121static const u32 tuning_block_128[] = {
122 0xFF00FFFF, 0x0000FFFF, 0xCCCCFFFF, 0xCCCC33CC,
123 0xCC3333CC, 0xFFFFCCCC, 0xFFFFEEFF, 0xFFEEEEFF,
124 0xFFDDFFFF, 0xDDDDFFFF, 0xBBFFFFFF, 0xBBFFFFFF,
125 0xFFFFFFBB, 0xFFFFFF77, 0x77FF7777, 0xFFEEDDBB,
126 0x00FFFFFF, 0x00FFFFFF, 0xCCFFFF00, 0xCC33CCCC,
127 0x3333CCCC, 0xFFCCCCCC, 0xFFEEFFFF, 0xEEEEFFFF,
128 0xDDFFFFFF, 0xDDFFFFFF, 0xFFFFFFDD, 0xFFFFFFBB,
129 0xFFFFBBBB, 0xFFFF77FF, 0xFF7777FF, 0xEEDDBB77
130};
Asutosh Das33a4ff52012-12-18 16:14:02 +0530131
Venkat Gopalakrishnanc61ab7e2013-03-11 12:17:57 -0700132static int disable_slots;
133/* root can write, others read */
134module_param(disable_slots, int, S_IRUGO|S_IWUSR);
135
Asutosh Das33a4ff52012-12-18 16:14:02 +0530136/* This structure keeps information per regulator */
137struct sdhci_msm_reg_data {
138 /* voltage regulator handle */
139 struct regulator *reg;
140 /* regulator name */
141 const char *name;
142 /* voltage level to be set */
143 u32 low_vol_level;
144 u32 high_vol_level;
145 /* Load values for low power and high power mode */
146 u32 lpm_uA;
147 u32 hpm_uA;
148
149 /* is this regulator enabled? */
150 bool is_enabled;
151 /* is this regulator needs to be always on? */
152 bool is_always_on;
153 /* is low power mode setting required for this regulator? */
154 bool lpm_sup;
155};
156
157/*
158 * This structure keeps information for all the
159 * regulators required for a SDCC slot.
160 */
161struct sdhci_msm_slot_reg_data {
162 /* keeps VDD/VCC regulator info */
163 struct sdhci_msm_reg_data *vdd_data;
164 /* keeps VDD IO regulator info */
165 struct sdhci_msm_reg_data *vdd_io_data;
166};
167
168struct sdhci_msm_gpio {
169 u32 no;
170 const char *name;
171 bool is_enabled;
172};
173
174struct sdhci_msm_gpio_data {
175 struct sdhci_msm_gpio *gpio;
176 u8 size;
177};
178
Asutosh Das390519d2012-12-21 12:21:42 +0530179struct sdhci_msm_pad_pull {
180 enum msm_tlmm_pull_tgt no;
181 u32 val;
182};
183
184struct sdhci_msm_pad_pull_data {
185 struct sdhci_msm_pad_pull *on;
186 struct sdhci_msm_pad_pull *off;
187 u8 size;
188};
189
190struct sdhci_msm_pad_drv {
191 enum msm_tlmm_hdrive_tgt no;
192 u32 val;
193};
194
195struct sdhci_msm_pad_drv_data {
196 struct sdhci_msm_pad_drv *on;
197 struct sdhci_msm_pad_drv *off;
198 u8 size;
199};
200
201struct sdhci_msm_pad_data {
202 struct sdhci_msm_pad_pull_data *pull;
203 struct sdhci_msm_pad_drv_data *drv;
204};
205
206
Asutosh Das33a4ff52012-12-18 16:14:02 +0530207struct sdhci_msm_pin_data {
208 /*
209 * = 1 if controller pins are using gpios
210 * = 0 if controller has dedicated MSM pads
211 */
Asutosh Das390519d2012-12-21 12:21:42 +0530212 u8 is_gpio;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530213 bool cfg_sts;
214 struct sdhci_msm_gpio_data *gpio_data;
Asutosh Das390519d2012-12-21 12:21:42 +0530215 struct sdhci_msm_pad_data *pad_data;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530216};
217
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +0530218struct sdhci_msm_bus_voting_data {
219 struct msm_bus_scale_pdata *bus_pdata;
220 unsigned int *bw_vecs;
221 unsigned int bw_vecs_size;
222};
223
Asutosh Das33a4ff52012-12-18 16:14:02 +0530224struct sdhci_msm_pltfm_data {
225 /* Supported UHS-I Modes */
226 u32 caps;
227
228 /* More capabilities */
229 u32 caps2;
230
231 unsigned long mmc_bus_width;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530232 struct sdhci_msm_slot_reg_data *vreg_data;
233 bool nonremovable;
234 struct sdhci_msm_pin_data *pin_data;
Sahitya Tummalab4e84042013-03-10 07:03:17 +0530235 u32 cpu_dma_latency_us;
Sahitya Tummala62448d92013-03-12 14:57:46 +0530236 int status_gpio; /* card detection GPIO that is configured as IRQ */
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +0530237 struct sdhci_msm_bus_voting_data *voting_data;
Sahitya Tummala00240122013-02-28 19:50:51 +0530238 u32 *sup_clk_table;
239 unsigned char sup_clk_cnt;
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +0530240};
241
242struct sdhci_msm_bus_vote {
243 uint32_t client_handle;
244 uint32_t curr_vote;
245 int min_bw_vote;
246 int max_bw_vote;
247 bool is_max_bw_needed;
248 struct delayed_work vote_work;
249 struct device_attribute max_bus_bw;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530250};
251
252struct sdhci_msm_host {
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +0530253 struct platform_device *pdev;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530254 void __iomem *core_mem; /* MSM SDCC mapped address */
Asutosh Dasbbc84782013-02-11 15:31:35 +0530255 int pwr_irq; /* power irq */
Asutosh Das33a4ff52012-12-18 16:14:02 +0530256 struct clk *clk; /* main SD/MMC bus clock */
257 struct clk *pclk; /* SDHC peripheral bus clock */
258 struct clk *bus_clk; /* SDHC bus voter clock */
Sahitya Tummala04c3a462013-01-11 11:30:45 +0530259 atomic_t clks_on; /* Set if clocks are enabled */
Asutosh Das33a4ff52012-12-18 16:14:02 +0530260 struct sdhci_msm_pltfm_data *pdata;
261 struct mmc_host *mmc;
262 struct sdhci_pltfm_data sdhci_msm_pdata;
Sahitya Tummala179e7382013-03-20 19:24:01 +0530263 u32 curr_pwr_state;
264 u32 curr_io_level;
265 struct completion pwr_irq_completion;
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +0530266 struct sdhci_msm_bus_vote msm_bus_vote;
Sahitya Tummala00240122013-02-28 19:50:51 +0530267 u32 clk_rate; /* Keeps track of current clock rate that is set */
Asutosh Das33a4ff52012-12-18 16:14:02 +0530268};
269
270enum vdd_io_level {
271 /* set vdd_io_data->low_vol_level */
272 VDD_IO_LOW,
273 /* set vdd_io_data->high_vol_level */
274 VDD_IO_HIGH,
275 /*
276 * set whatever there in voltage_level (third argument) of
277 * sdhci_msm_set_vdd_io_vol() function.
278 */
279 VDD_IO_SET_LEVEL,
280};
281
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700282/* MSM platform specific tuning */
283static inline int msm_dll_poll_ck_out_en(struct sdhci_host *host,
284 u8 poll)
285{
286 int rc = 0;
287 u32 wait_cnt = 50;
288 u8 ck_out_en = 0;
289 struct mmc_host *mmc = host->mmc;
290
291 /* poll for CK_OUT_EN bit. max. poll time = 50us */
292 ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) &
293 CORE_CK_OUT_EN);
294
295 while (ck_out_en != poll) {
296 if (--wait_cnt == 0) {
297 pr_err("%s: %s: CK_OUT_EN bit is not %d\n",
298 mmc_hostname(mmc), __func__, poll);
299 rc = -ETIMEDOUT;
300 goto out;
301 }
302 udelay(1);
303
304 ck_out_en = !!(readl_relaxed(host->ioaddr +
305 CORE_DLL_CONFIG) & CORE_CK_OUT_EN);
306 }
307out:
308 return rc;
309}
310
311static int msm_config_cm_dll_phase(struct sdhci_host *host, u8 phase)
312{
313 int rc = 0;
314 u8 grey_coded_phase_table[] = {0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4,
315 0xC, 0xD, 0xF, 0xE, 0xA, 0xB, 0x9,
316 0x8};
317 unsigned long flags;
318 u32 config;
319 struct mmc_host *mmc = host->mmc;
320
321 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
322 spin_lock_irqsave(&host->lock, flags);
323
324 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
325 config &= ~(CORE_CDR_EN | CORE_CK_OUT_EN);
326 config |= (CORE_CDR_EXT_EN | CORE_DLL_EN);
327 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
328
329 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '0' */
330 rc = msm_dll_poll_ck_out_en(host, 0);
331 if (rc)
332 goto err_out;
333
334 /*
335 * Write the selected DLL clock output phase (0 ... 15)
336 * to CDR_SELEXT bit field of DLL_CONFIG register.
337 */
338 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
339 & ~(0xF << 20))
340 | (grey_coded_phase_table[phase] << 20)),
341 host->ioaddr + CORE_DLL_CONFIG);
342
343 /* Set CK_OUT_EN bit of DLL_CONFIG register to 1. */
344 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
345 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
346
347 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '1' */
348 rc = msm_dll_poll_ck_out_en(host, 1);
349 if (rc)
350 goto err_out;
351
352 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
353 config |= CORE_CDR_EN;
354 config &= ~CORE_CDR_EXT_EN;
355 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
356 goto out;
357
358err_out:
359 pr_err("%s: %s: Failed to set DLL phase: %d\n",
360 mmc_hostname(mmc), __func__, phase);
361out:
362 spin_unlock_irqrestore(&host->lock, flags);
363 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
364 return rc;
365}
366
367/*
368 * Find out the greatest range of consecuitive selected
369 * DLL clock output phases that can be used as sampling
370 * setting for SD3.0 UHS-I card read operation (in SDR104
371 * timing mode) or for eMMC4.5 card read operation (in HS200
372 * timing mode).
373 * Select the 3/4 of the range and configure the DLL with the
374 * selected DLL clock output phase.
375 */
376
377static int msm_find_most_appropriate_phase(struct sdhci_host *host,
378 u8 *phase_table, u8 total_phases)
379{
380 int ret;
381 u8 ranges[MAX_PHASES][MAX_PHASES] = { {0}, {0} };
382 u8 phases_per_row[MAX_PHASES] = {0};
383 int row_index = 0, col_index = 0, selected_row_index = 0, curr_max = 0;
384 int i, cnt, phase_0_raw_index = 0, phase_15_raw_index = 0;
385 bool phase_0_found = false, phase_15_found = false;
386 struct mmc_host *mmc = host->mmc;
387
388 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
389 if (!total_phases || (total_phases > MAX_PHASES)) {
390 pr_err("%s: %s: invalid argument: total_phases=%d\n",
391 mmc_hostname(mmc), __func__, total_phases);
392 return -EINVAL;
393 }
394
395 for (cnt = 0; cnt < total_phases; cnt++) {
396 ranges[row_index][col_index] = phase_table[cnt];
397 phases_per_row[row_index] += 1;
398 col_index++;
399
400 if ((cnt + 1) == total_phases) {
401 continue;
402 /* check if next phase in phase_table is consecutive or not */
403 } else if ((phase_table[cnt] + 1) != phase_table[cnt + 1]) {
404 row_index++;
405 col_index = 0;
406 }
407 }
408
409 if (row_index >= MAX_PHASES)
410 return -EINVAL;
411
412 /* Check if phase-0 is present in first valid window? */
413 if (!ranges[0][0]) {
414 phase_0_found = true;
415 phase_0_raw_index = 0;
416 /* Check if cycle exist between 2 valid windows */
417 for (cnt = 1; cnt <= row_index; cnt++) {
418 if (phases_per_row[cnt]) {
419 for (i = 0; i < phases_per_row[cnt]; i++) {
420 if (ranges[cnt][i] == 15) {
421 phase_15_found = true;
422 phase_15_raw_index = cnt;
423 break;
424 }
425 }
426 }
427 }
428 }
429
430 /* If 2 valid windows form cycle then merge them as single window */
431 if (phase_0_found && phase_15_found) {
432 /* number of phases in raw where phase 0 is present */
433 u8 phases_0 = phases_per_row[phase_0_raw_index];
434 /* number of phases in raw where phase 15 is present */
435 u8 phases_15 = phases_per_row[phase_15_raw_index];
436
437 if (phases_0 + phases_15 >= MAX_PHASES)
438 /*
439 * If there are more than 1 phase windows then total
440 * number of phases in both the windows should not be
441 * more than or equal to MAX_PHASES.
442 */
443 return -EINVAL;
444
445 /* Merge 2 cyclic windows */
446 i = phases_15;
447 for (cnt = 0; cnt < phases_0; cnt++) {
448 ranges[phase_15_raw_index][i] =
449 ranges[phase_0_raw_index][cnt];
450 if (++i >= MAX_PHASES)
451 break;
452 }
453
454 phases_per_row[phase_0_raw_index] = 0;
455 phases_per_row[phase_15_raw_index] = phases_15 + phases_0;
456 }
457
458 for (cnt = 0; cnt <= row_index; cnt++) {
459 if (phases_per_row[cnt] > curr_max) {
460 curr_max = phases_per_row[cnt];
461 selected_row_index = cnt;
462 }
463 }
464
465 i = ((curr_max * 3) / 4);
466 if (i)
467 i--;
468
469 ret = (int)ranges[selected_row_index][i];
470
471 if (ret >= MAX_PHASES) {
472 ret = -EINVAL;
473 pr_err("%s: %s: invalid phase selected=%d\n",
474 mmc_hostname(mmc), __func__, ret);
475 }
476
477 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
478 return ret;
479}
480
481static inline void msm_cm_dll_set_freq(struct sdhci_host *host)
482{
483 u32 mclk_freq = 0;
484
485 /* Program the MCLK value to MCLK_FREQ bit field */
486 if (host->clock <= 112000000)
487 mclk_freq = 0;
488 else if (host->clock <= 125000000)
489 mclk_freq = 1;
490 else if (host->clock <= 137000000)
491 mclk_freq = 2;
492 else if (host->clock <= 150000000)
493 mclk_freq = 3;
494 else if (host->clock <= 162000000)
495 mclk_freq = 4;
496 else if (host->clock <= 175000000)
497 mclk_freq = 5;
498 else if (host->clock <= 187000000)
499 mclk_freq = 6;
500 else if (host->clock <= 200000000)
501 mclk_freq = 7;
502
503 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
504 & ~(7 << 24)) | (mclk_freq << 24)),
505 host->ioaddr + CORE_DLL_CONFIG);
506}
507
508/* Initialize the DLL (Programmable Delay Line ) */
509static int msm_init_cm_dll(struct sdhci_host *host)
510{
511 struct mmc_host *mmc = host->mmc;
512 int rc = 0;
513 unsigned long flags;
514 u32 wait_cnt;
515
516 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
517 spin_lock_irqsave(&host->lock, flags);
518
519 /*
520 * Make sure that clock is always enabled when DLL
521 * tuning is in progress. Keeping PWRSAVE ON may
522 * turn off the clock. So let's disable the PWRSAVE
523 * here and re-enable it once tuning is completed.
524 */
525 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
526 & ~CORE_CLK_PWRSAVE),
527 host->ioaddr + CORE_VENDOR_SPEC);
528
529 /* Write 1 to DLL_RST bit of DLL_CONFIG register */
530 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
531 | CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
532
533 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
534 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
535 | CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
536 msm_cm_dll_set_freq(host);
537
538 /* Write 0 to DLL_RST bit of DLL_CONFIG register */
539 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
540 & ~CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
541
542 /* Write 0 to DLL_PDN bit of DLL_CONFIG register */
543 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
544 & ~CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
545
546 /* Set DLL_EN bit to 1. */
547 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
548 | CORE_DLL_EN), host->ioaddr + CORE_DLL_CONFIG);
549
550 /* Set CK_OUT_EN bit to 1. */
551 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
552 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
553
554 wait_cnt = 50;
555 /* Wait until DLL_LOCK bit of DLL_STATUS register becomes '1' */
556 while (!(readl_relaxed(host->ioaddr + CORE_DLL_STATUS) &
557 CORE_DLL_LOCK)) {
558 /* max. wait for 50us sec for LOCK bit to be set */
559 if (--wait_cnt == 0) {
560 pr_err("%s: %s: DLL failed to LOCK\n",
561 mmc_hostname(mmc), __func__);
562 rc = -ETIMEDOUT;
563 goto out;
564 }
565 /* wait for 1us before polling again */
566 udelay(1);
567 }
568
569out:
570 /* re-enable PWRSAVE */
571 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
572 CORE_CLK_PWRSAVE),
573 host->ioaddr + CORE_VENDOR_SPEC);
574 spin_unlock_irqrestore(&host->lock, flags);
575 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
576 return rc;
577}
578
579int sdhci_msm_execute_tuning(struct sdhci_host *host, u32 opcode)
580{
581 unsigned long flags;
582 u8 phase, *data_buf, tuned_phases[16], tuned_phase_cnt = 0;
583 const u32 *tuning_block_pattern = tuning_block_64;
584 int size = sizeof(tuning_block_64); /* Tuning pattern size in bytes */
585 int rc;
586 struct mmc_host *mmc = host->mmc;
Sahitya Tummala00240122013-02-28 19:50:51 +0530587 struct mmc_ios ios = host->mmc->ios;
588
589 /*
590 * Tuning is required for SDR104 and HS200 cards and if clock frequency
591 * is greater than 100MHz in these modes.
592 */
593 if (host->clock <= (100 * 1000 * 1000) ||
594 !(ios.timing == MMC_TIMING_MMC_HS200 ||
595 ios.timing == MMC_TIMING_UHS_SDR104))
596 return 0;
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700597
598 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700599 spin_lock_irqsave(&host->lock, flags);
600
601 if ((opcode == MMC_SEND_TUNING_BLOCK_HS200) &&
602 (mmc->ios.bus_width == MMC_BUS_WIDTH_8)) {
603 tuning_block_pattern = tuning_block_128;
604 size = sizeof(tuning_block_128);
605 }
606 spin_unlock_irqrestore(&host->lock, flags);
607
608 /* first of all reset the tuning block */
609 rc = msm_init_cm_dll(host);
610 if (rc)
611 goto out;
612
613 data_buf = kmalloc(size, GFP_KERNEL);
614 if (!data_buf) {
615 rc = -ENOMEM;
616 goto out;
617 }
618
619 phase = 0;
620 do {
621 struct mmc_command cmd = {0};
622 struct mmc_data data = {0};
623 struct mmc_request mrq = {
624 .cmd = &cmd,
625 .data = &data
626 };
627 struct scatterlist sg;
628
629 /* set the phase in delay line hw block */
630 rc = msm_config_cm_dll_phase(host, phase);
631 if (rc)
632 goto kfree;
633
634 cmd.opcode = opcode;
635 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
636
637 data.blksz = size;
638 data.blocks = 1;
639 data.flags = MMC_DATA_READ;
640 data.timeout_ns = 1000 * 1000 * 1000; /* 1 sec */
641
642 data.sg = &sg;
643 data.sg_len = 1;
644 sg_init_one(&sg, data_buf, size);
645 memset(data_buf, 0, size);
646 mmc_wait_for_req(mmc, &mrq);
647
648 if (!cmd.error && !data.error &&
649 !memcmp(data_buf, tuning_block_pattern, size)) {
650 /* tuning is successful at this tuning point */
651 tuned_phases[tuned_phase_cnt++] = phase;
652 pr_debug("%s: %s: found good phase = %d\n",
653 mmc_hostname(mmc), __func__, phase);
654 }
655 } while (++phase < 16);
656
657 if (tuned_phase_cnt) {
658 rc = msm_find_most_appropriate_phase(host, tuned_phases,
659 tuned_phase_cnt);
660 if (rc < 0)
661 goto kfree;
662 else
663 phase = (u8)rc;
664
665 /*
666 * Finally set the selected phase in delay
667 * line hw block.
668 */
669 rc = msm_config_cm_dll_phase(host, phase);
670 if (rc)
671 goto kfree;
672 pr_debug("%s: %s: finally setting the tuning phase to %d\n",
673 mmc_hostname(mmc), __func__, phase);
674 } else {
675 /* tuning failed */
676 pr_err("%s: %s: no tuning point found\n",
677 mmc_hostname(mmc), __func__);
678 rc = -EAGAIN;
679 }
680
681kfree:
682 kfree(data_buf);
683out:
684 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
685 return rc;
686}
687
Asutosh Das33a4ff52012-12-18 16:14:02 +0530688static int sdhci_msm_setup_gpio(struct sdhci_msm_pltfm_data *pdata, bool enable)
689{
690 struct sdhci_msm_gpio_data *curr;
691 int i, ret = 0;
692
693 curr = pdata->pin_data->gpio_data;
694 for (i = 0; i < curr->size; i++) {
695 if (!gpio_is_valid(curr->gpio[i].no)) {
696 ret = -EINVAL;
697 pr_err("%s: Invalid gpio = %d\n", __func__,
698 curr->gpio[i].no);
699 goto free_gpios;
700 }
701 if (enable) {
702 ret = gpio_request(curr->gpio[i].no,
703 curr->gpio[i].name);
704 if (ret) {
705 pr_err("%s: gpio_request(%d, %s) failed %d\n",
706 __func__, curr->gpio[i].no,
707 curr->gpio[i].name, ret);
708 goto free_gpios;
709 }
710 curr->gpio[i].is_enabled = true;
711 } else {
712 gpio_free(curr->gpio[i].no);
713 curr->gpio[i].is_enabled = false;
714 }
715 }
716 return ret;
717
718free_gpios:
719 for (i--; i >= 0; i--) {
720 gpio_free(curr->gpio[i].no);
721 curr->gpio[i].is_enabled = false;
722 }
723 return ret;
724}
725
Asutosh Das390519d2012-12-21 12:21:42 +0530726static int sdhci_msm_setup_pad(struct sdhci_msm_pltfm_data *pdata, bool enable)
727{
728 struct sdhci_msm_pad_data *curr;
729 int i;
730
731 curr = pdata->pin_data->pad_data;
732 for (i = 0; i < curr->drv->size; i++) {
733 if (enable)
734 msm_tlmm_set_hdrive(curr->drv->on[i].no,
735 curr->drv->on[i].val);
736 else
737 msm_tlmm_set_hdrive(curr->drv->off[i].no,
738 curr->drv->off[i].val);
739 }
740
741 for (i = 0; i < curr->pull->size; i++) {
742 if (enable)
743 msm_tlmm_set_pull(curr->pull->on[i].no,
744 curr->pull->on[i].val);
745 else
746 msm_tlmm_set_pull(curr->pull->off[i].no,
747 curr->pull->off[i].val);
748 }
749
750 return 0;
751}
752
Asutosh Das33a4ff52012-12-18 16:14:02 +0530753static int sdhci_msm_setup_pins(struct sdhci_msm_pltfm_data *pdata, bool enable)
754{
755 int ret = 0;
756
757 if (!pdata->pin_data || (pdata->pin_data->cfg_sts == enable))
758 return 0;
Asutosh Das390519d2012-12-21 12:21:42 +0530759 if (pdata->pin_data->is_gpio)
760 ret = sdhci_msm_setup_gpio(pdata, enable);
761 else
762 ret = sdhci_msm_setup_pad(pdata, enable);
Asutosh Das33a4ff52012-12-18 16:14:02 +0530763
Asutosh Das33a4ff52012-12-18 16:14:02 +0530764 if (!ret)
765 pdata->pin_data->cfg_sts = enable;
766
767 return ret;
768}
769
Asutosh Das390519d2012-12-21 12:21:42 +0530770static int sdhci_msm_dt_get_array(struct device *dev, const char *prop_name,
771 u32 **out, int *len, u32 size)
772{
773 int ret = 0;
774 struct device_node *np = dev->of_node;
775 size_t sz;
776 u32 *arr = NULL;
777
778 if (!of_get_property(np, prop_name, len)) {
779 ret = -EINVAL;
780 goto out;
781 }
782 sz = *len = *len / sizeof(*arr);
783 if (sz <= 0 || (size > 0 && (sz != size))) {
784 dev_err(dev, "%s invalid size\n", prop_name);
785 ret = -EINVAL;
786 goto out;
787 }
788
789 arr = devm_kzalloc(dev, sz * sizeof(*arr), GFP_KERNEL);
790 if (!arr) {
791 dev_err(dev, "%s failed allocating memory\n", prop_name);
792 ret = -ENOMEM;
793 goto out;
794 }
795
796 ret = of_property_read_u32_array(np, prop_name, arr, sz);
797 if (ret < 0) {
798 dev_err(dev, "%s failed reading array %d\n", prop_name, ret);
799 goto out;
800 }
801 *out = arr;
802out:
803 if (ret)
804 *len = 0;
805 return ret;
806}
807
Asutosh Das33a4ff52012-12-18 16:14:02 +0530808#define MAX_PROP_SIZE 32
809static int sdhci_msm_dt_parse_vreg_info(struct device *dev,
810 struct sdhci_msm_reg_data **vreg_data, const char *vreg_name)
811{
812 int len, ret = 0;
813 const __be32 *prop;
814 char prop_name[MAX_PROP_SIZE];
815 struct sdhci_msm_reg_data *vreg;
816 struct device_node *np = dev->of_node;
817
818 snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", vreg_name);
819 if (!of_parse_phandle(np, prop_name, 0)) {
820 dev_err(dev, "No vreg data found for %s\n", vreg_name);
821 ret = -EINVAL;
822 return ret;
823 }
824
825 vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
826 if (!vreg) {
827 dev_err(dev, "No memory for vreg: %s\n", vreg_name);
828 ret = -ENOMEM;
829 return ret;
830 }
831
832 vreg->name = vreg_name;
833
834 snprintf(prop_name, MAX_PROP_SIZE,
835 "qcom,%s-always-on", vreg_name);
836 if (of_get_property(np, prop_name, NULL))
837 vreg->is_always_on = true;
838
839 snprintf(prop_name, MAX_PROP_SIZE,
840 "qcom,%s-lpm-sup", vreg_name);
841 if (of_get_property(np, prop_name, NULL))
842 vreg->lpm_sup = true;
843
844 snprintf(prop_name, MAX_PROP_SIZE,
845 "qcom,%s-voltage-level", vreg_name);
846 prop = of_get_property(np, prop_name, &len);
847 if (!prop || (len != (2 * sizeof(__be32)))) {
848 dev_warn(dev, "%s %s property\n",
849 prop ? "invalid format" : "no", prop_name);
850 } else {
851 vreg->low_vol_level = be32_to_cpup(&prop[0]);
852 vreg->high_vol_level = be32_to_cpup(&prop[1]);
853 }
854
855 snprintf(prop_name, MAX_PROP_SIZE,
856 "qcom,%s-current-level", vreg_name);
857 prop = of_get_property(np, prop_name, &len);
858 if (!prop || (len != (2 * sizeof(__be32)))) {
859 dev_warn(dev, "%s %s property\n",
860 prop ? "invalid format" : "no", prop_name);
861 } else {
862 vreg->lpm_uA = be32_to_cpup(&prop[0]);
863 vreg->hpm_uA = be32_to_cpup(&prop[1]);
864 }
865
866 *vreg_data = vreg;
867 dev_dbg(dev, "%s: %s %s vol=[%d %d]uV, curr=[%d %d]uA\n",
868 vreg->name, vreg->is_always_on ? "always_on," : "",
869 vreg->lpm_sup ? "lpm_sup," : "", vreg->low_vol_level,
870 vreg->high_vol_level, vreg->lpm_uA, vreg->hpm_uA);
871
872 return ret;
873}
874
Asutosh Das390519d2012-12-21 12:21:42 +0530875/* GPIO/Pad data extraction */
876static int sdhci_msm_dt_get_pad_pull_info(struct device *dev, int id,
877 struct sdhci_msm_pad_pull_data **pad_pull_data)
878{
879 int ret = 0, base = 0, len, i;
880 u32 *tmp;
881 struct sdhci_msm_pad_pull_data *pull_data;
882 struct sdhci_msm_pad_pull *pull;
883
884 switch (id) {
885 case 1:
886 base = TLMM_PULL_SDC1_CLK;
887 break;
888 case 2:
889 base = TLMM_PULL_SDC2_CLK;
890 break;
891 case 3:
892 base = TLMM_PULL_SDC3_CLK;
893 break;
894 case 4:
895 base = TLMM_PULL_SDC4_CLK;
896 break;
897 default:
898 dev_err(dev, "%s: Invalid slot id\n", __func__);
899 ret = -EINVAL;
900 goto out;
901 }
902
903 pull_data = devm_kzalloc(dev, sizeof(struct sdhci_msm_pad_pull_data),
904 GFP_KERNEL);
905 if (!pull_data) {
906 dev_err(dev, "No memory for msm_mmc_pad_pull_data\n");
907 ret = -ENOMEM;
908 goto out;
909 }
910 pull_data->size = 3; /* array size for clk, cmd, data */
911
912 /* Allocate on, off configs for clk, cmd, data */
913 pull = devm_kzalloc(dev, 2 * pull_data->size *\
914 sizeof(struct sdhci_msm_pad_pull), GFP_KERNEL);
915 if (!pull) {
916 dev_err(dev, "No memory for msm_mmc_pad_pull\n");
917 ret = -ENOMEM;
918 goto out;
919 }
920 pull_data->on = pull;
921 pull_data->off = pull + pull_data->size;
922
923 ret = sdhci_msm_dt_get_array(dev, "qcom,pad-pull-on",
924 &tmp, &len, pull_data->size);
925 if (ret)
926 goto out;
927
928 for (i = 0; i < len; i++) {
929 pull_data->on[i].no = base + i;
930 pull_data->on[i].val = tmp[i];
931 dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
932 i, pull_data->on[i].val);
933 }
934
935 ret = sdhci_msm_dt_get_array(dev, "qcom,pad-pull-off",
936 &tmp, &len, pull_data->size);
937 if (ret)
938 goto out;
939
940 for (i = 0; i < len; i++) {
941 pull_data->off[i].no = base + i;
942 pull_data->off[i].val = tmp[i];
943 dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
944 i, pull_data->off[i].val);
945 }
946
947 *pad_pull_data = pull_data;
948out:
949 return ret;
950}
951
952static int sdhci_msm_dt_get_pad_drv_info(struct device *dev, int id,
953 struct sdhci_msm_pad_drv_data **pad_drv_data)
954{
955 int ret = 0, base = 0, len, i;
956 u32 *tmp;
957 struct sdhci_msm_pad_drv_data *drv_data;
958 struct sdhci_msm_pad_drv *drv;
959
960 switch (id) {
961 case 1:
962 base = TLMM_HDRV_SDC1_CLK;
963 break;
964 case 2:
965 base = TLMM_HDRV_SDC2_CLK;
966 break;
967 case 3:
968 base = TLMM_HDRV_SDC3_CLK;
969 break;
970 case 4:
971 base = TLMM_HDRV_SDC4_CLK;
972 break;
973 default:
974 dev_err(dev, "%s: Invalid slot id\n", __func__);
975 ret = -EINVAL;
976 goto out;
977 }
978
979 drv_data = devm_kzalloc(dev, sizeof(struct sdhci_msm_pad_drv_data),
980 GFP_KERNEL);
981 if (!drv_data) {
982 dev_err(dev, "No memory for msm_mmc_pad_drv_data\n");
983 ret = -ENOMEM;
984 goto out;
985 }
986 drv_data->size = 3; /* array size for clk, cmd, data */
987
988 /* Allocate on, off configs for clk, cmd, data */
989 drv = devm_kzalloc(dev, 2 * drv_data->size *\
990 sizeof(struct sdhci_msm_pad_drv), GFP_KERNEL);
991 if (!drv) {
992 dev_err(dev, "No memory msm_mmc_pad_drv\n");
993 ret = -ENOMEM;
994 goto out;
995 }
996 drv_data->on = drv;
997 drv_data->off = drv + drv_data->size;
998
999 ret = sdhci_msm_dt_get_array(dev, "qcom,pad-drv-on",
1000 &tmp, &len, drv_data->size);
1001 if (ret)
1002 goto out;
1003
1004 for (i = 0; i < len; i++) {
1005 drv_data->on[i].no = base + i;
1006 drv_data->on[i].val = tmp[i];
1007 dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
1008 i, drv_data->on[i].val);
1009 }
1010
1011 ret = sdhci_msm_dt_get_array(dev, "qcom,pad-drv-off",
1012 &tmp, &len, drv_data->size);
1013 if (ret)
1014 goto out;
1015
1016 for (i = 0; i < len; i++) {
1017 drv_data->off[i].no = base + i;
1018 drv_data->off[i].val = tmp[i];
1019 dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
1020 i, drv_data->off[i].val);
1021 }
1022
1023 *pad_drv_data = drv_data;
1024out:
1025 return ret;
1026}
1027
Asutosh Das33a4ff52012-12-18 16:14:02 +05301028#define GPIO_NAME_MAX_LEN 32
1029static int sdhci_msm_dt_parse_gpio_info(struct device *dev,
1030 struct sdhci_msm_pltfm_data *pdata)
1031{
Asutosh Das390519d2012-12-21 12:21:42 +05301032 int ret = 0, id = 0, cnt, i;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301033 struct sdhci_msm_pin_data *pin_data;
1034 struct device_node *np = dev->of_node;
1035
1036 pin_data = devm_kzalloc(dev, sizeof(*pin_data), GFP_KERNEL);
1037 if (!pin_data) {
1038 dev_err(dev, "No memory for pin_data\n");
1039 ret = -ENOMEM;
1040 goto out;
1041 }
1042
1043 cnt = of_gpio_count(np);
1044 if (cnt > 0) {
Asutosh Das390519d2012-12-21 12:21:42 +05301045 pin_data->is_gpio = true;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301046 pin_data->gpio_data = devm_kzalloc(dev,
1047 sizeof(struct sdhci_msm_gpio_data), GFP_KERNEL);
1048 if (!pin_data->gpio_data) {
1049 dev_err(dev, "No memory for gpio_data\n");
1050 ret = -ENOMEM;
1051 goto out;
1052 }
1053 pin_data->gpio_data->size = cnt;
1054 pin_data->gpio_data->gpio = devm_kzalloc(dev, cnt *
1055 sizeof(struct sdhci_msm_gpio), GFP_KERNEL);
1056
1057 if (!pin_data->gpio_data->gpio) {
1058 dev_err(dev, "No memory for gpio\n");
1059 ret = -ENOMEM;
1060 goto out;
1061 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301062 for (i = 0; i < cnt; i++) {
1063 const char *name = NULL;
1064 char result[GPIO_NAME_MAX_LEN];
1065 pin_data->gpio_data->gpio[i].no = of_get_gpio(np, i);
1066 of_property_read_string_index(np,
1067 "qcom,gpio-names", i, &name);
1068
1069 snprintf(result, GPIO_NAME_MAX_LEN, "%s-%s",
1070 dev_name(dev), name ? name : "?");
1071 pin_data->gpio_data->gpio[i].name = result;
1072 dev_dbg(dev, "%s: gpio[%s] = %d\n", __func__,
Asutosh Das390519d2012-12-21 12:21:42 +05301073 pin_data->gpio_data->gpio[i].name,
1074 pin_data->gpio_data->gpio[i].no);
Asutosh Das33a4ff52012-12-18 16:14:02 +05301075 }
Asutosh Das390519d2012-12-21 12:21:42 +05301076 } else {
1077 pin_data->pad_data =
1078 devm_kzalloc(dev,
1079 sizeof(struct sdhci_msm_pad_data),
1080 GFP_KERNEL);
1081 if (!pin_data->pad_data) {
1082 dev_err(dev,
1083 "No memory for pin_data->pad_data\n");
1084 ret = -ENOMEM;
1085 goto out;
1086 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301087
Asutosh Das390519d2012-12-21 12:21:42 +05301088 ret = of_alias_get_id(np, "sdhc");
1089 if (ret < 0) {
1090 dev_err(dev, "Failed to get slot index %d\n", ret);
1091 goto out;
1092 }
1093 id = ret;
1094
1095 ret = sdhci_msm_dt_get_pad_pull_info(
1096 dev, id, &pin_data->pad_data->pull);
1097 if (ret)
1098 goto out;
1099 ret = sdhci_msm_dt_get_pad_drv_info(
1100 dev, id, &pin_data->pad_data->drv);
1101 if (ret)
1102 goto out;
1103
1104 }
1105 pdata->pin_data = pin_data;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301106out:
1107 if (ret)
1108 dev_err(dev, "%s failed with err %d\n", __func__, ret);
1109 return ret;
1110}
1111
1112/* Parse platform data */
1113static struct sdhci_msm_pltfm_data *sdhci_msm_populate_pdata(struct device *dev)
1114{
1115 struct sdhci_msm_pltfm_data *pdata = NULL;
1116 struct device_node *np = dev->of_node;
1117 u32 bus_width = 0;
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301118 u32 cpu_dma_latency;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301119 int len, i;
Sahitya Tummala00240122013-02-28 19:50:51 +05301120 int clk_table_len;
1121 u32 *clk_table = NULL;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301122
1123 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1124 if (!pdata) {
1125 dev_err(dev, "failed to allocate memory for platform data\n");
1126 goto out;
1127 }
1128
Sahitya Tummala62448d92013-03-12 14:57:46 +05301129 pdata->status_gpio = of_get_named_gpio_flags(np, "cd-gpios", 0, 0);
1130
Asutosh Das33a4ff52012-12-18 16:14:02 +05301131 of_property_read_u32(np, "qcom,bus-width", &bus_width);
1132 if (bus_width == 8)
1133 pdata->mmc_bus_width = MMC_CAP_8_BIT_DATA;
1134 else if (bus_width == 4)
1135 pdata->mmc_bus_width = MMC_CAP_4_BIT_DATA;
1136 else {
1137 dev_notice(dev, "invalid bus-width, default to 1-bit mode\n");
1138 pdata->mmc_bus_width = 0;
1139 }
1140
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301141 if (!of_property_read_u32(np, "qcom,cpu-dma-latency-us",
1142 &cpu_dma_latency))
1143 pdata->cpu_dma_latency_us = cpu_dma_latency;
1144
Sahitya Tummala00240122013-02-28 19:50:51 +05301145 if (sdhci_msm_dt_get_array(dev, "qcom,clk-rates",
1146 &clk_table, &clk_table_len, 0)) {
1147 dev_err(dev, "failed parsing supported clock rates\n");
1148 goto out;
1149 }
1150 if (!clk_table || !clk_table_len) {
1151 dev_err(dev, "Invalid clock table\n");
1152 goto out;
1153 }
1154 pdata->sup_clk_table = clk_table;
1155 pdata->sup_clk_cnt = clk_table_len;
1156
Asutosh Das33a4ff52012-12-18 16:14:02 +05301157 pdata->vreg_data = devm_kzalloc(dev, sizeof(struct
1158 sdhci_msm_slot_reg_data),
1159 GFP_KERNEL);
1160 if (!pdata->vreg_data) {
1161 dev_err(dev, "failed to allocate memory for vreg data\n");
1162 goto out;
1163 }
1164
1165 if (sdhci_msm_dt_parse_vreg_info(dev, &pdata->vreg_data->vdd_data,
1166 "vdd")) {
1167 dev_err(dev, "failed parsing vdd data\n");
1168 goto out;
1169 }
1170 if (sdhci_msm_dt_parse_vreg_info(dev,
1171 &pdata->vreg_data->vdd_io_data,
1172 "vdd-io")) {
1173 dev_err(dev, "failed parsing vdd-io data\n");
1174 goto out;
1175 }
1176
1177 if (sdhci_msm_dt_parse_gpio_info(dev, pdata)) {
1178 dev_err(dev, "failed parsing gpio data\n");
1179 goto out;
1180 }
1181
Asutosh Das33a4ff52012-12-18 16:14:02 +05301182 len = of_property_count_strings(np, "qcom,bus-speed-mode");
1183
1184 for (i = 0; i < len; i++) {
1185 const char *name = NULL;
1186
1187 of_property_read_string_index(np,
1188 "qcom,bus-speed-mode", i, &name);
1189 if (!name)
1190 continue;
1191
1192 if (!strncmp(name, "HS200_1p8v", sizeof("HS200_1p8v")))
1193 pdata->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
1194 else if (!strncmp(name, "HS200_1p2v", sizeof("HS200_1p2v")))
1195 pdata->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
1196 else if (!strncmp(name, "DDR_1p8v", sizeof("DDR_1p8v")))
1197 pdata->caps |= MMC_CAP_1_8V_DDR
1198 | MMC_CAP_UHS_DDR50;
1199 else if (!strncmp(name, "DDR_1p2v", sizeof("DDR_1p2v")))
1200 pdata->caps |= MMC_CAP_1_2V_DDR
1201 | MMC_CAP_UHS_DDR50;
1202 }
1203
1204 if (of_get_property(np, "qcom,nonremovable", NULL))
1205 pdata->nonremovable = true;
1206
1207 return pdata;
1208out:
1209 return NULL;
1210}
1211
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301212/* Returns required bandwidth in Bytes per Sec */
1213static unsigned int sdhci_get_bw_required(struct sdhci_host *host,
1214 struct mmc_ios *ios)
1215{
Sahitya Tummala53aff982013-04-03 18:03:31 +05301216 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1217 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1218
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301219 unsigned int bw;
1220
Sahitya Tummala53aff982013-04-03 18:03:31 +05301221 bw = msm_host->clk_rate;
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301222 /*
1223 * For DDR mode, SDCC controller clock will be at
1224 * the double rate than the actual clock that goes to card.
1225 */
1226 if (ios->bus_width == MMC_BUS_WIDTH_4)
1227 bw /= 2;
1228 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1229 bw /= 8;
1230
1231 return bw;
1232}
1233
1234static int sdhci_msm_bus_get_vote_for_bw(struct sdhci_msm_host *host,
1235 unsigned int bw)
1236{
1237 unsigned int *table = host->pdata->voting_data->bw_vecs;
1238 unsigned int size = host->pdata->voting_data->bw_vecs_size;
1239 int i;
1240
1241 if (host->msm_bus_vote.is_max_bw_needed && bw)
1242 return host->msm_bus_vote.max_bw_vote;
1243
1244 for (i = 0; i < size; i++) {
1245 if (bw <= table[i])
1246 break;
1247 }
1248
1249 if (i && (i == size))
1250 i--;
1251
1252 return i;
1253}
1254
1255/*
1256 * This function must be called with host lock acquired.
1257 * Caller of this function should also ensure that msm bus client
1258 * handle is not null.
1259 */
1260static inline int sdhci_msm_bus_set_vote(struct sdhci_msm_host *msm_host,
1261 int vote,
1262 unsigned long flags)
1263{
1264 struct sdhci_host *host = platform_get_drvdata(msm_host->pdev);
1265 int rc = 0;
1266
1267 if (vote != msm_host->msm_bus_vote.curr_vote) {
1268 spin_unlock_irqrestore(&host->lock, flags);
1269 rc = msm_bus_scale_client_update_request(
1270 msm_host->msm_bus_vote.client_handle, vote);
1271 spin_lock_irqsave(&host->lock, flags);
1272 if (rc) {
1273 pr_err("%s: msm_bus_scale_client_update_request() failed: bus_client_handle=0x%x, vote=%d, err=%d\n",
1274 mmc_hostname(host->mmc),
1275 msm_host->msm_bus_vote.client_handle, vote, rc);
1276 goto out;
1277 }
1278 msm_host->msm_bus_vote.curr_vote = vote;
1279 }
1280out:
1281 return rc;
1282}
1283
1284/*
1285 * Internal work. Work to set 0 bandwidth for msm bus.
1286 */
1287static void sdhci_msm_bus_work(struct work_struct *work)
1288{
1289 struct sdhci_msm_host *msm_host;
1290 struct sdhci_host *host;
1291 unsigned long flags;
1292
1293 msm_host = container_of(work, struct sdhci_msm_host,
1294 msm_bus_vote.vote_work.work);
1295 host = platform_get_drvdata(msm_host->pdev);
1296
1297 if (!msm_host->msm_bus_vote.client_handle)
1298 return;
1299
1300 spin_lock_irqsave(&host->lock, flags);
1301 /* don't vote for 0 bandwidth if any request is in progress */
1302 if (!host->mrq) {
1303 sdhci_msm_bus_set_vote(msm_host,
1304 msm_host->msm_bus_vote.min_bw_vote, flags);
1305 } else
1306 pr_warning("%s: %s: Transfer in progress. skipping bus voting to 0 bandwidth\n",
1307 mmc_hostname(host->mmc), __func__);
1308 spin_unlock_irqrestore(&host->lock, flags);
1309}
1310
1311/*
1312 * This function cancels any scheduled delayed work and sets the bus
1313 * vote based on bw (bandwidth) argument.
1314 */
1315static void sdhci_msm_bus_cancel_work_and_set_vote(struct sdhci_host *host,
1316 unsigned int bw)
1317{
1318 int vote;
1319 unsigned long flags;
1320 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1321 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1322
1323 cancel_delayed_work_sync(&msm_host->msm_bus_vote.vote_work);
1324 spin_lock_irqsave(&host->lock, flags);
1325 vote = sdhci_msm_bus_get_vote_for_bw(msm_host, bw);
1326 sdhci_msm_bus_set_vote(msm_host, vote, flags);
1327 spin_unlock_irqrestore(&host->lock, flags);
1328}
1329
1330#define MSM_MMC_BUS_VOTING_DELAY 200 /* msecs */
1331
1332/* This function queues a work which will set the bandwidth requiement to 0 */
1333static void sdhci_msm_bus_queue_work(struct sdhci_host *host)
1334{
1335 unsigned long flags;
1336 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1337 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1338
1339 spin_lock_irqsave(&host->lock, flags);
1340 if (msm_host->msm_bus_vote.min_bw_vote !=
1341 msm_host->msm_bus_vote.curr_vote)
1342 queue_delayed_work(system_nrt_wq,
1343 &msm_host->msm_bus_vote.vote_work,
1344 msecs_to_jiffies(MSM_MMC_BUS_VOTING_DELAY));
1345 spin_unlock_irqrestore(&host->lock, flags);
1346}
1347
1348static int sdhci_msm_bus_register(struct sdhci_msm_host *host,
1349 struct platform_device *pdev)
1350{
1351 int rc = 0;
1352 struct msm_bus_scale_pdata *bus_pdata;
1353
1354 struct sdhci_msm_bus_voting_data *data;
1355 struct device *dev = &pdev->dev;
1356
1357 data = devm_kzalloc(dev,
1358 sizeof(struct sdhci_msm_bus_voting_data), GFP_KERNEL);
1359 if (!data) {
1360 dev_err(&pdev->dev,
1361 "%s: failed to allocate memory\n", __func__);
1362 rc = -ENOMEM;
1363 goto out;
1364 }
1365 data->bus_pdata = msm_bus_cl_get_pdata(pdev);
1366 if (data->bus_pdata) {
1367 rc = sdhci_msm_dt_get_array(dev, "qcom,bus-bw-vectors-bps",
1368 &data->bw_vecs, &data->bw_vecs_size, 0);
1369 if (rc) {
1370 dev_err(&pdev->dev,
1371 "%s: Failed to get bus-bw-vectors-bps\n",
1372 __func__);
1373 goto out;
1374 }
1375 host->pdata->voting_data = data;
1376 }
1377 if (host->pdata->voting_data &&
1378 host->pdata->voting_data->bus_pdata &&
1379 host->pdata->voting_data->bw_vecs &&
1380 host->pdata->voting_data->bw_vecs_size) {
1381
1382 bus_pdata = host->pdata->voting_data->bus_pdata;
1383 host->msm_bus_vote.client_handle =
1384 msm_bus_scale_register_client(bus_pdata);
1385 if (!host->msm_bus_vote.client_handle) {
1386 dev_err(&pdev->dev, "msm_bus_scale_register_client()\n");
1387 rc = -EFAULT;
1388 goto out;
1389 }
1390 /* cache the vote index for minimum and maximum bandwidth */
1391 host->msm_bus_vote.min_bw_vote =
1392 sdhci_msm_bus_get_vote_for_bw(host, 0);
1393 host->msm_bus_vote.max_bw_vote =
1394 sdhci_msm_bus_get_vote_for_bw(host, UINT_MAX);
1395 } else {
1396 devm_kfree(dev, data);
1397 }
1398
1399out:
1400 return rc;
1401}
1402
1403static void sdhci_msm_bus_unregister(struct sdhci_msm_host *host)
1404{
1405 if (host->msm_bus_vote.client_handle)
1406 msm_bus_scale_unregister_client(
1407 host->msm_bus_vote.client_handle);
1408}
1409
1410static void sdhci_msm_bus_voting(struct sdhci_host *host, u32 enable)
1411{
1412 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1413 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1414 struct mmc_ios *ios = &host->mmc->ios;
1415 unsigned int bw;
1416
1417 if (!msm_host->msm_bus_vote.client_handle)
1418 return;
1419
1420 bw = sdhci_get_bw_required(host, ios);
1421 if (enable)
1422 sdhci_msm_bus_cancel_work_and_set_vote(host, bw);
1423 else
1424 sdhci_msm_bus_queue_work(host);
1425}
1426
Asutosh Das33a4ff52012-12-18 16:14:02 +05301427/* Regulator utility functions */
1428static int sdhci_msm_vreg_init_reg(struct device *dev,
1429 struct sdhci_msm_reg_data *vreg)
1430{
1431 int ret = 0;
1432
1433 /* check if regulator is already initialized? */
1434 if (vreg->reg)
1435 goto out;
1436
1437 /* Get the regulator handle */
1438 vreg->reg = devm_regulator_get(dev, vreg->name);
1439 if (IS_ERR(vreg->reg)) {
1440 ret = PTR_ERR(vreg->reg);
1441 pr_err("%s: devm_regulator_get(%s) failed. ret=%d\n",
1442 __func__, vreg->name, ret);
1443 goto out;
1444 }
1445
1446 /* sanity check */
1447 if (!vreg->high_vol_level || !vreg->hpm_uA) {
1448 pr_err("%s: %s invalid constraints specified\n",
1449 __func__, vreg->name);
1450 ret = -EINVAL;
1451 }
1452
1453out:
1454 return ret;
1455}
1456
1457static void sdhci_msm_vreg_deinit_reg(struct sdhci_msm_reg_data *vreg)
1458{
1459 if (vreg->reg)
1460 devm_regulator_put(vreg->reg);
1461}
1462
1463static int sdhci_msm_vreg_set_optimum_mode(struct sdhci_msm_reg_data
1464 *vreg, int uA_load)
1465{
1466 int ret = 0;
1467
1468 /*
1469 * regulators that do not support regulator_set_voltage also
1470 * do not support regulator_set_optimum_mode
1471 */
1472 ret = regulator_set_optimum_mode(vreg->reg, uA_load);
1473 if (ret < 0)
1474 pr_err("%s: regulator_set_optimum_mode(reg=%s,uA_load=%d) failed. ret=%d\n",
1475 __func__, vreg->name, uA_load, ret);
1476 else
1477 /*
1478 * regulator_set_optimum_mode() can return non zero
1479 * value even for success case.
1480 */
1481 ret = 0;
1482 return ret;
1483}
1484
1485static int sdhci_msm_vreg_set_voltage(struct sdhci_msm_reg_data *vreg,
1486 int min_uV, int max_uV)
1487{
1488 int ret = 0;
1489
1490 ret = regulator_set_voltage(vreg->reg, min_uV, max_uV);
1491 if (ret) {
1492 pr_err("%s: regulator_set_voltage(%s)failed. min_uV=%d,max_uV=%d,ret=%d\n",
1493 __func__, vreg->name, min_uV, max_uV, ret);
1494 }
1495
1496 return ret;
1497}
1498
1499static int sdhci_msm_vreg_enable(struct sdhci_msm_reg_data *vreg)
1500{
1501 int ret = 0;
1502
1503 /* Put regulator in HPM (high power mode) */
1504 ret = sdhci_msm_vreg_set_optimum_mode(vreg, vreg->hpm_uA);
1505 if (ret < 0)
1506 return ret;
1507
1508 if (!vreg->is_enabled) {
1509 /* Set voltage level */
1510 ret = sdhci_msm_vreg_set_voltage(vreg, vreg->high_vol_level,
1511 vreg->high_vol_level);
1512 if (ret)
1513 return ret;
1514 }
1515 ret = regulator_enable(vreg->reg);
1516 if (ret) {
1517 pr_err("%s: regulator_enable(%s) failed. ret=%d\n",
1518 __func__, vreg->name, ret);
1519 return ret;
1520 }
1521 vreg->is_enabled = true;
1522 return ret;
1523}
1524
1525static int sdhci_msm_vreg_disable(struct sdhci_msm_reg_data *vreg)
1526{
1527 int ret = 0;
1528
1529 /* Never disable regulator marked as always_on */
1530 if (vreg->is_enabled && !vreg->is_always_on) {
1531 ret = regulator_disable(vreg->reg);
1532 if (ret) {
1533 pr_err("%s: regulator_disable(%s) failed. ret=%d\n",
1534 __func__, vreg->name, ret);
1535 goto out;
1536 }
1537 vreg->is_enabled = false;
1538
1539 ret = sdhci_msm_vreg_set_optimum_mode(vreg, 0);
1540 if (ret < 0)
1541 goto out;
1542
1543 /* Set min. voltage level to 0 */
1544 ret = sdhci_msm_vreg_set_voltage(vreg, 0, vreg->high_vol_level);
1545 if (ret)
1546 goto out;
1547 } else if (vreg->is_enabled && vreg->is_always_on) {
1548 if (vreg->lpm_sup) {
1549 /* Put always_on regulator in LPM (low power mode) */
1550 ret = sdhci_msm_vreg_set_optimum_mode(vreg,
1551 vreg->lpm_uA);
1552 if (ret < 0)
1553 goto out;
1554 }
1555 }
1556out:
1557 return ret;
1558}
1559
1560static int sdhci_msm_setup_vreg(struct sdhci_msm_pltfm_data *pdata,
1561 bool enable, bool is_init)
1562{
1563 int ret = 0, i;
1564 struct sdhci_msm_slot_reg_data *curr_slot;
1565 struct sdhci_msm_reg_data *vreg_table[2];
1566
1567 curr_slot = pdata->vreg_data;
1568 if (!curr_slot) {
1569 pr_debug("%s: vreg info unavailable,assuming the slot is powered by always on domain\n",
1570 __func__);
1571 goto out;
1572 }
1573
1574 vreg_table[0] = curr_slot->vdd_data;
1575 vreg_table[1] = curr_slot->vdd_io_data;
1576
1577 for (i = 0; i < ARRAY_SIZE(vreg_table); i++) {
1578 if (vreg_table[i]) {
1579 if (enable)
1580 ret = sdhci_msm_vreg_enable(vreg_table[i]);
1581 else
1582 ret = sdhci_msm_vreg_disable(vreg_table[i]);
1583 if (ret)
1584 goto out;
1585 }
1586 }
1587out:
1588 return ret;
1589}
1590
1591/*
1592 * Reset vreg by ensuring it is off during probe. A call
1593 * to enable vreg is needed to balance disable vreg
1594 */
1595static int sdhci_msm_vreg_reset(struct sdhci_msm_pltfm_data *pdata)
1596{
1597 int ret;
1598
1599 ret = sdhci_msm_setup_vreg(pdata, 1, true);
1600 if (ret)
1601 return ret;
1602 ret = sdhci_msm_setup_vreg(pdata, 0, true);
1603 return ret;
1604}
1605
1606/* This init function should be called only once for each SDHC slot */
1607static int sdhci_msm_vreg_init(struct device *dev,
1608 struct sdhci_msm_pltfm_data *pdata,
1609 bool is_init)
1610{
1611 int ret = 0;
1612 struct sdhci_msm_slot_reg_data *curr_slot;
1613 struct sdhci_msm_reg_data *curr_vdd_reg, *curr_vdd_io_reg;
1614
1615 curr_slot = pdata->vreg_data;
1616 if (!curr_slot)
1617 goto out;
1618
1619 curr_vdd_reg = curr_slot->vdd_data;
1620 curr_vdd_io_reg = curr_slot->vdd_io_data;
1621
1622 if (!is_init)
1623 /* Deregister all regulators from regulator framework */
1624 goto vdd_io_reg_deinit;
1625
1626 /*
1627 * Get the regulator handle from voltage regulator framework
1628 * and then try to set the voltage level for the regulator
1629 */
1630 if (curr_vdd_reg) {
1631 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_reg);
1632 if (ret)
1633 goto out;
1634 }
1635 if (curr_vdd_io_reg) {
1636 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_io_reg);
1637 if (ret)
1638 goto vdd_reg_deinit;
1639 }
1640 ret = sdhci_msm_vreg_reset(pdata);
1641 if (ret)
1642 dev_err(dev, "vreg reset failed (%d)\n", ret);
1643 goto out;
1644
1645vdd_io_reg_deinit:
1646 if (curr_vdd_io_reg)
1647 sdhci_msm_vreg_deinit_reg(curr_vdd_io_reg);
1648vdd_reg_deinit:
1649 if (curr_vdd_reg)
1650 sdhci_msm_vreg_deinit_reg(curr_vdd_reg);
1651out:
1652 return ret;
1653}
1654
1655
1656static int sdhci_msm_set_vdd_io_vol(struct sdhci_msm_pltfm_data *pdata,
1657 enum vdd_io_level level,
1658 unsigned int voltage_level)
1659{
1660 int ret = 0;
1661 int set_level;
1662 struct sdhci_msm_reg_data *vdd_io_reg;
1663
1664 if (!pdata->vreg_data)
1665 return ret;
1666
1667 vdd_io_reg = pdata->vreg_data->vdd_io_data;
1668 if (vdd_io_reg && vdd_io_reg->is_enabled) {
1669 switch (level) {
1670 case VDD_IO_LOW:
1671 set_level = vdd_io_reg->low_vol_level;
1672 break;
1673 case VDD_IO_HIGH:
1674 set_level = vdd_io_reg->high_vol_level;
1675 break;
1676 case VDD_IO_SET_LEVEL:
1677 set_level = voltage_level;
1678 break;
1679 default:
1680 pr_err("%s: invalid argument level = %d",
1681 __func__, level);
1682 ret = -EINVAL;
1683 return ret;
1684 }
1685 ret = sdhci_msm_vreg_set_voltage(vdd_io_reg, set_level,
1686 set_level);
1687 }
1688 return ret;
1689}
1690
1691static irqreturn_t sdhci_msm_pwr_irq(int irq, void *data)
1692{
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07001693 struct sdhci_host *host = (struct sdhci_host *)data;
1694 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1695 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301696 u8 irq_status = 0;
1697 u8 irq_ack = 0;
1698 int ret = 0;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301699 int pwr_state = 0, io_level = 0;
1700 unsigned long flags;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301701
1702 irq_status = readb_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
1703 pr_debug("%s: Received IRQ(%d), status=0x%x\n",
1704 mmc_hostname(msm_host->mmc), irq, irq_status);
1705
1706 /* Clear the interrupt */
1707 writeb_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
1708 /*
1709 * SDHC has core_mem and hc_mem device memory and these memory
1710 * addresses do not fall within 1KB region. Hence, any update to
1711 * core_mem address space would require an mb() to ensure this gets
1712 * completed before its next update to registers within hc_mem.
1713 */
1714 mb();
1715
1716 /* Handle BUS ON/OFF*/
1717 if (irq_status & CORE_PWRCTL_BUS_ON) {
1718 ret = sdhci_msm_setup_vreg(msm_host->pdata, true, false);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301719 if (!ret) {
Asutosh Das33a4ff52012-12-18 16:14:02 +05301720 ret = sdhci_msm_setup_pins(msm_host->pdata, true);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301721 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
1722 VDD_IO_HIGH, 0);
1723 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301724 if (ret)
1725 irq_ack |= CORE_PWRCTL_BUS_FAIL;
1726 else
1727 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301728
1729 pwr_state = REQ_BUS_ON;
1730 io_level = REQ_IO_HIGH;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301731 }
1732 if (irq_status & CORE_PWRCTL_BUS_OFF) {
1733 ret = sdhci_msm_setup_vreg(msm_host->pdata, false, false);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301734 if (!ret) {
Asutosh Das33a4ff52012-12-18 16:14:02 +05301735 ret = sdhci_msm_setup_pins(msm_host->pdata, false);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301736 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
1737 VDD_IO_LOW, 0);
1738 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301739 if (ret)
1740 irq_ack |= CORE_PWRCTL_BUS_FAIL;
1741 else
1742 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301743
1744 pwr_state = REQ_BUS_OFF;
1745 io_level = REQ_IO_LOW;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301746 }
1747 /* Handle IO LOW/HIGH */
1748 if (irq_status & CORE_PWRCTL_IO_LOW) {
1749 /* Switch voltage Low */
1750 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_LOW, 0);
1751 if (ret)
1752 irq_ack |= CORE_PWRCTL_IO_FAIL;
1753 else
1754 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301755
1756 io_level = REQ_IO_LOW;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301757 }
1758 if (irq_status & CORE_PWRCTL_IO_HIGH) {
1759 /* Switch voltage High */
1760 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_HIGH, 0);
1761 if (ret)
1762 irq_ack |= CORE_PWRCTL_IO_FAIL;
1763 else
1764 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301765
1766 io_level = REQ_IO_HIGH;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301767 }
1768
1769 /* ACK status to the core */
1770 writeb_relaxed(irq_ack, (msm_host->core_mem + CORE_PWRCTL_CTL));
1771 /*
1772 * SDHC has core_mem and hc_mem device memory and these memory
1773 * addresses do not fall within 1KB region. Hence, any update to
1774 * core_mem address space would require an mb() to ensure this gets
1775 * completed before its next update to registers within hc_mem.
1776 */
1777 mb();
1778
Sahitya Tummala179e7382013-03-20 19:24:01 +05301779 if (io_level & REQ_IO_HIGH)
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07001780 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
1781 ~CORE_IO_PAD_PWR_SWITCH),
1782 host->ioaddr + CORE_VENDOR_SPEC);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301783 else if (io_level & REQ_IO_LOW)
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07001784 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
1785 CORE_IO_PAD_PWR_SWITCH),
1786 host->ioaddr + CORE_VENDOR_SPEC);
1787 mb();
1788
Asutosh Das33a4ff52012-12-18 16:14:02 +05301789 pr_debug("%s: Handled IRQ(%d), ret=%d, ack=0x%x\n",
1790 mmc_hostname(msm_host->mmc), irq, ret, irq_ack);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301791 spin_lock_irqsave(&host->lock, flags);
1792 if (pwr_state)
1793 msm_host->curr_pwr_state = pwr_state;
1794 if (io_level)
1795 msm_host->curr_io_level = io_level;
1796 complete(&msm_host->pwr_irq_completion);
1797 spin_unlock_irqrestore(&host->lock, flags);
1798
Asutosh Das33a4ff52012-12-18 16:14:02 +05301799 return IRQ_HANDLED;
1800}
1801
1802/* This function returns the max. current supported by VDD rail in mA */
1803static unsigned int sdhci_msm_get_vreg_vdd_max_current(struct sdhci_msm_host
1804 *host)
1805{
1806 struct sdhci_msm_slot_reg_data *curr_slot = host->pdata->vreg_data;
1807 if (!curr_slot)
1808 return 0;
1809 if (curr_slot->vdd_data)
1810 return curr_slot->vdd_data->hpm_uA / 1000;
1811 else
1812 return 0;
1813}
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301814static ssize_t
1815show_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
1816 char *buf)
1817{
1818 struct sdhci_host *host = dev_get_drvdata(dev);
1819 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1820 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1821
1822 return snprintf(buf, PAGE_SIZE, "%u\n",
1823 msm_host->msm_bus_vote.is_max_bw_needed);
1824}
1825
1826static ssize_t
1827store_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
1828 const char *buf, size_t count)
1829{
1830 struct sdhci_host *host = dev_get_drvdata(dev);
1831 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1832 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1833 uint32_t value;
1834 unsigned long flags;
1835
1836 if (!kstrtou32(buf, 0, &value)) {
1837 spin_lock_irqsave(&host->lock, flags);
1838 msm_host->msm_bus_vote.is_max_bw_needed = !!value;
1839 spin_unlock_irqrestore(&host->lock, flags);
1840 }
1841 return count;
1842}
Asutosh Das33a4ff52012-12-18 16:14:02 +05301843
Sahitya Tummala179e7382013-03-20 19:24:01 +05301844static void sdhci_msm_check_power_status(struct sdhci_host *host, u32 req_type)
Asutosh Das33a4ff52012-12-18 16:14:02 +05301845{
1846 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1847 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301848 unsigned long flags;
1849 bool done = false;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301850
Sahitya Tummala179e7382013-03-20 19:24:01 +05301851 spin_lock_irqsave(&host->lock, flags);
1852 pr_debug("%s: %s: request %d curr_pwr_state %x curr_io_level %x\n",
1853 mmc_hostname(host->mmc), __func__, req_type,
1854 msm_host->curr_pwr_state, msm_host->curr_io_level);
1855 if ((req_type & msm_host->curr_pwr_state) ||
1856 (req_type & msm_host->curr_io_level))
1857 done = true;
1858 spin_unlock_irqrestore(&host->lock, flags);
Asutosh Das33a4ff52012-12-18 16:14:02 +05301859
Sahitya Tummala179e7382013-03-20 19:24:01 +05301860 /*
1861 * This is needed here to hanlde a case where IRQ gets
1862 * triggered even before this function is called so that
1863 * x->done counter of completion gets reset. Otherwise,
1864 * next call to wait_for_completion returns immediately
1865 * without actually waiting for the IRQ to be handled.
1866 */
1867 if (done)
1868 init_completion(&msm_host->pwr_irq_completion);
1869 else
1870 wait_for_completion(&msm_host->pwr_irq_completion);
1871
1872 pr_debug("%s: %s: request %d done\n", mmc_hostname(host->mmc),
1873 __func__, req_type);
Asutosh Das33a4ff52012-12-18 16:14:02 +05301874}
1875
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07001876static void sdhci_msm_toggle_cdr(struct sdhci_host *host, bool enable)
1877{
1878 if (enable)
1879 writel_relaxed((readl_relaxed(host->ioaddr +
1880 CORE_DLL_CONFIG) | CORE_CDR_EN),
1881 host->ioaddr + CORE_DLL_CONFIG);
1882 else
1883 writel_relaxed((readl_relaxed(host->ioaddr +
1884 CORE_DLL_CONFIG) & ~CORE_CDR_EN),
1885 host->ioaddr + CORE_DLL_CONFIG);
1886}
1887
Asutosh Das3781bd82013-01-10 21:11:04 +05301888static unsigned int sdhci_msm_max_segs(void)
1889{
1890 return SDHCI_MSM_MAX_SEGMENTS;
1891}
1892
Sahitya Tummala00240122013-02-28 19:50:51 +05301893static unsigned int sdhci_msm_get_min_clock(struct sdhci_host *host)
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301894{
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301895 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1896 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301897
Sahitya Tummala00240122013-02-28 19:50:51 +05301898 return msm_host->pdata->sup_clk_table[0];
1899}
1900
1901static unsigned int sdhci_msm_get_max_clock(struct sdhci_host *host)
1902{
1903 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1904 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1905 int max_clk_index = msm_host->pdata->sup_clk_cnt;
1906
1907 return msm_host->pdata->sup_clk_table[max_clk_index - 1];
1908}
1909
1910static unsigned int sdhci_msm_get_sup_clk_rate(struct sdhci_host *host,
1911 u32 req_clk)
1912{
1913 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1914 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1915 unsigned int sel_clk = -1;
1916 unsigned char cnt;
1917
1918 if (req_clk < sdhci_msm_get_min_clock(host)) {
1919 sel_clk = sdhci_msm_get_min_clock(host);
1920 return sel_clk;
1921 }
1922
1923 for (cnt = 0; cnt < msm_host->pdata->sup_clk_cnt; cnt++) {
1924 if (msm_host->pdata->sup_clk_table[cnt] > req_clk) {
1925 break;
1926 } else if (msm_host->pdata->sup_clk_table[cnt] == req_clk) {
1927 sel_clk = msm_host->pdata->sup_clk_table[cnt];
1928 break;
1929 } else {
1930 sel_clk = msm_host->pdata->sup_clk_table[cnt];
1931 }
1932 }
1933 return sel_clk;
1934}
1935
1936static int sdhci_msm_prepare_clocks(struct sdhci_host *host, bool enable)
1937{
1938 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1939 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1940 int rc = 0;
1941
1942 if (enable && !atomic_read(&msm_host->clks_on)) {
1943 pr_debug("%s: request to enable clocks\n",
1944 mmc_hostname(host->mmc));
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301945 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
1946 rc = clk_prepare_enable(msm_host->bus_clk);
1947 if (rc) {
1948 pr_err("%s: %s: failed to enable the bus-clock with error %d\n",
1949 mmc_hostname(host->mmc), __func__, rc);
1950 goto out;
1951 }
1952 }
1953 if (!IS_ERR(msm_host->pclk)) {
1954 rc = clk_prepare_enable(msm_host->pclk);
1955 if (rc) {
1956 pr_err("%s: %s: failed to enable the pclk with error %d\n",
1957 mmc_hostname(host->mmc), __func__, rc);
1958 goto disable_bus_clk;
1959 }
1960 }
1961 rc = clk_prepare_enable(msm_host->clk);
1962 if (rc) {
1963 pr_err("%s: %s: failed to enable the host-clk with error %d\n",
1964 mmc_hostname(host->mmc), __func__, rc);
1965 goto disable_pclk;
1966 }
1967 mb();
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301968
Sahitya Tummala00240122013-02-28 19:50:51 +05301969 } else if (!enable && atomic_read(&msm_host->clks_on)) {
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301970 pr_debug("%s: request to disable clocks\n",
1971 mmc_hostname(host->mmc));
1972 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
1973 mb();
1974 clk_disable_unprepare(msm_host->clk);
1975 if (!IS_ERR(msm_host->pclk))
1976 clk_disable_unprepare(msm_host->pclk);
1977 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
1978 clk_disable_unprepare(msm_host->bus_clk);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301979 }
Sahitya Tummala00240122013-02-28 19:50:51 +05301980 atomic_set(&msm_host->clks_on, enable);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301981 goto out;
1982disable_pclk:
1983 if (!IS_ERR_OR_NULL(msm_host->pclk))
1984 clk_disable_unprepare(msm_host->pclk);
1985disable_bus_clk:
1986 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
1987 clk_disable_unprepare(msm_host->bus_clk);
1988out:
Sahitya Tummala00240122013-02-28 19:50:51 +05301989 return rc;
1990}
1991
1992static void sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock)
1993{
1994 int rc;
1995 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1996 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1997 struct mmc_ios curr_ios = host->mmc->ios;
1998 u32 sup_clock, ddr_clock;
1999
2000 if (!clock) {
2001 sdhci_msm_prepare_clocks(host, false);
2002 host->clock = clock;
2003 return;
2004 }
2005
2006 rc = sdhci_msm_prepare_clocks(host, true);
2007 if (rc)
2008 return;
2009
2010 sup_clock = sdhci_msm_get_sup_clk_rate(host, clock);
2011 if (curr_ios.timing == MMC_TIMING_UHS_DDR50) {
2012 /*
2013 * The SDHC requires internal clock frequency to be double the
2014 * actual clock that will be set for DDR mode. The controller
2015 * uses the faster clock(100MHz) for some of its parts and send
2016 * the actual required clock (50MHz) to the card.
2017 */
2018 ddr_clock = clock * 2;
2019 sup_clock = sdhci_msm_get_sup_clk_rate(host,
2020 ddr_clock);
2021 }
2022 if (sup_clock != msm_host->clk_rate) {
2023 pr_debug("%s: %s: setting clk rate to %u\n",
2024 mmc_hostname(host->mmc), __func__, sup_clock);
2025 rc = clk_set_rate(msm_host->clk, sup_clock);
2026 if (rc) {
2027 pr_err("%s: %s: Failed to set rate %u for host-clk : %d\n",
2028 mmc_hostname(host->mmc), __func__,
2029 sup_clock, rc);
2030 return;
2031 }
2032 msm_host->clk_rate = sup_clock;
2033 host->clock = clock;
2034 }
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302035}
2036
Sahitya Tummala5e4f9642013-03-21 11:13:25 +05302037static int sdhci_msm_set_uhs_signaling(struct sdhci_host *host,
2038 unsigned int uhs)
2039{
2040 u16 ctrl_2;
2041
2042 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2043 /* Select Bus Speed Mode for host */
2044 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
2045 if (uhs == MMC_TIMING_MMC_HS200)
2046 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2047 else if (uhs == MMC_TIMING_UHS_SDR12)
2048 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
2049 else if (uhs == MMC_TIMING_UHS_SDR25)
2050 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
2051 else if (uhs == MMC_TIMING_UHS_SDR50)
2052 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
2053 else if (uhs == MMC_TIMING_UHS_SDR104)
2054 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2055 else if (uhs == MMC_TIMING_UHS_DDR50)
2056 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Sahitya Tummala00240122013-02-28 19:50:51 +05302057 /*
2058 * When clock frquency is less than 100MHz, the feedback clock must be
2059 * provided and DLL must not be used so that tuning can be skipped. To
2060 * provide feedback clock, the mode selection can be any value less
2061 * than 3'b011 in bits [2:0] of HOST CONTROL2 register.
2062 */
2063 if (host->clock <= (100 * 1000 * 1000) &&
2064 (uhs == MMC_TIMING_MMC_HS200 ||
2065 uhs == MMC_TIMING_UHS_SDR104))
2066 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
2067
Sahitya Tummala5e4f9642013-03-21 11:13:25 +05302068 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
2069
2070 return 0;
2071}
2072
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002073/*
2074 * sdhci_msm_disable_data_xfer - disable undergoing AHB bus data transfer
2075 *
2076 * Write 0 to bit 0 in MCI_DATA_CTL (offset 0x2C) - clearing TxActive bit by
2077 * access to legacy registers. It will stop current burst and prevent start of
2078 * the next on.
2079 *
2080 * Polling CORE_AHB_DATA_DELAY_US timeout, by reading bit 13:12 until they are 0
2081 * in CORE_SDCC_DEBUG_REG (offset 0x124) will validate that AHB burst was
2082 * completed and a new one didn't start.
2083 *
2084 * Waiting for 4us while AHB finishes descriptors fetch.
2085 */
2086static void sdhci_msm_disable_data_xfer(struct sdhci_host *host)
2087{
2088 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2089 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2090 u32 value;
2091 int ret;
2092
2093 value = readl_relaxed(msm_host->core_mem + CORE_MCI_DATA_CTRL);
2094 value &= ~(u32)CORE_MCI_DPSM_ENABLE;
2095 writel_relaxed(value, msm_host->core_mem + CORE_MCI_DATA_CTRL);
2096
2097 /* Enable the test bus for device slot */
2098 writel_relaxed(CORE_TESTBUS_ENA | CORE_TESTBUS_SEL2,
2099 msm_host->core_mem + CORE_TESTBUS_CONFIG);
2100
2101 ret = readl_poll_timeout_noirq(msm_host->core_mem
2102 + CORE_SDCC_DEBUG_REG, value,
2103 !(value & CORE_DEBUG_REG_AHB_HTRANS),
2104 CORE_AHB_DATA_DELAY_US, 1);
2105 if (ret) {
2106 pr_err("%s: %s: can't stop ongoing AHB bus access by ADMA\n",
2107 mmc_hostname(host->mmc), __func__);
2108 BUG();
2109 }
2110 /* Disable the test bus for device slot */
2111 value = readl_relaxed(msm_host->core_mem + CORE_TESTBUS_CONFIG);
2112 value &= ~CORE_TESTBUS_ENA;
2113 writel_relaxed(value, msm_host->core_mem + CORE_TESTBUS_CONFIG);
2114
2115 udelay(CORE_AHB_DESC_DELAY_US);
2116}
2117
Asutosh Das33a4ff52012-12-18 16:14:02 +05302118static struct sdhci_ops sdhci_msm_ops = {
Sahitya Tummala5e4f9642013-03-21 11:13:25 +05302119 .set_uhs_signaling = sdhci_msm_set_uhs_signaling,
Asutosh Das33a4ff52012-12-18 16:14:02 +05302120 .check_power_status = sdhci_msm_check_power_status,
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07002121 .execute_tuning = sdhci_msm_execute_tuning,
2122 .toggle_cdr = sdhci_msm_toggle_cdr,
Asutosh Das3781bd82013-01-10 21:11:04 +05302123 .get_max_segments = sdhci_msm_max_segs,
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302124 .set_clock = sdhci_msm_set_clock,
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302125 .platform_bus_voting = sdhci_msm_bus_voting,
Sahitya Tummala00240122013-02-28 19:50:51 +05302126 .get_min_clock = sdhci_msm_get_min_clock,
2127 .get_max_clock = sdhci_msm_get_max_clock,
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002128 .disable_data_xfer = sdhci_msm_disable_data_xfer,
Asutosh Das33a4ff52012-12-18 16:14:02 +05302129};
2130
2131static int __devinit sdhci_msm_probe(struct platform_device *pdev)
2132{
2133 struct sdhci_host *host;
2134 struct sdhci_pltfm_host *pltfm_host;
2135 struct sdhci_msm_host *msm_host;
2136 struct resource *core_memres = NULL;
Asutosh Dasbbc84782013-02-11 15:31:35 +05302137 int ret = 0, dead = 0;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302138 u32 vdd_max_current;
Stephen Boyd3edbd8f2013-04-24 14:19:46 -07002139 u16 host_version;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302140
2141 pr_debug("%s: Enter %s\n", dev_name(&pdev->dev), __func__);
2142 msm_host = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_msm_host),
2143 GFP_KERNEL);
2144 if (!msm_host) {
2145 ret = -ENOMEM;
2146 goto out;
2147 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05302148
2149 msm_host->sdhci_msm_pdata.ops = &sdhci_msm_ops;
2150 host = sdhci_pltfm_init(pdev, &msm_host->sdhci_msm_pdata);
2151 if (IS_ERR(host)) {
2152 ret = PTR_ERR(host);
2153 goto out;
2154 }
2155
2156 pltfm_host = sdhci_priv(host);
2157 pltfm_host->priv = msm_host;
2158 msm_host->mmc = host->mmc;
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302159 msm_host->pdev = pdev;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302160
2161 /* Extract platform data */
2162 if (pdev->dev.of_node) {
Venkat Gopalakrishnanc61ab7e2013-03-11 12:17:57 -07002163 ret = of_alias_get_id(pdev->dev.of_node, "sdhc");
2164 if (ret < 0) {
2165 dev_err(&pdev->dev, "Failed to get slot index %d\n",
2166 ret);
2167 goto pltfm_free;
2168 }
2169 if (disable_slots & (1 << (ret - 1))) {
2170 dev_info(&pdev->dev, "%s: Slot %d disabled\n", __func__,
2171 ret);
2172 ret = -ENODEV;
2173 goto pltfm_free;
2174 }
2175
Asutosh Das33a4ff52012-12-18 16:14:02 +05302176 msm_host->pdata = sdhci_msm_populate_pdata(&pdev->dev);
2177 if (!msm_host->pdata) {
2178 dev_err(&pdev->dev, "DT parsing error\n");
2179 goto pltfm_free;
2180 }
2181 } else {
2182 dev_err(&pdev->dev, "No device tree node\n");
2183 goto pltfm_free;
2184 }
2185
2186 /* Setup Clocks */
2187
2188 /* Setup SDCC bus voter clock. */
2189 msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus_clk");
2190 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
2191 /* Vote for max. clk rate for max. performance */
2192 ret = clk_set_rate(msm_host->bus_clk, INT_MAX);
2193 if (ret)
2194 goto pltfm_free;
2195 ret = clk_prepare_enable(msm_host->bus_clk);
2196 if (ret)
2197 goto pltfm_free;
2198 }
2199
2200 /* Setup main peripheral bus clock */
2201 msm_host->pclk = devm_clk_get(&pdev->dev, "iface_clk");
2202 if (!IS_ERR(msm_host->pclk)) {
2203 ret = clk_prepare_enable(msm_host->pclk);
2204 if (ret)
2205 goto bus_clk_disable;
2206 }
2207
2208 /* Setup SDC MMC clock */
2209 msm_host->clk = devm_clk_get(&pdev->dev, "core_clk");
2210 if (IS_ERR(msm_host->clk)) {
2211 ret = PTR_ERR(msm_host->clk);
2212 goto pclk_disable;
2213 }
2214
2215 ret = clk_prepare_enable(msm_host->clk);
2216 if (ret)
2217 goto pclk_disable;
2218
Sahitya Tummala00240122013-02-28 19:50:51 +05302219 /* Set to the minimum supported clock frequency */
2220 ret = clk_set_rate(msm_host->clk, sdhci_msm_get_min_clock(host));
2221 if (ret) {
2222 dev_err(&pdev->dev, "MClk rate set failed (%d)\n", ret);
2223 goto clk_disable;
2224 }
2225 msm_host->clk_rate = sdhci_msm_get_min_clock(host);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302226 atomic_set(&msm_host->clks_on, 1);
Sahitya Tummala00240122013-02-28 19:50:51 +05302227
Asutosh Das33a4ff52012-12-18 16:14:02 +05302228 /* Setup regulators */
2229 ret = sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, true);
2230 if (ret) {
2231 dev_err(&pdev->dev, "Regulator setup failed (%d)\n", ret);
2232 goto clk_disable;
2233 }
2234
2235 /* Reset the core and Enable SDHC mode */
2236 core_memres = platform_get_resource_byname(pdev,
2237 IORESOURCE_MEM, "core_mem");
2238 msm_host->core_mem = devm_ioremap(&pdev->dev, core_memres->start,
2239 resource_size(core_memres));
2240
2241 if (!msm_host->core_mem) {
2242 dev_err(&pdev->dev, "Failed to remap registers\n");
2243 ret = -ENOMEM;
2244 goto vreg_deinit;
2245 }
2246
2247 /* Set SW_RST bit in POWER register (Offset 0x0) */
2248 writel_relaxed(CORE_SW_RST, msm_host->core_mem + CORE_POWER);
2249 /* Set HC_MODE_EN bit in HC_MODE register */
2250 writel_relaxed(HC_MODE_EN, (msm_host->core_mem + CORE_HC_MODE));
2251
2252 /*
2253 * Following are the deviations from SDHC spec v3.0 -
2254 * 1. Card detection is handled using separate GPIO.
2255 * 2. Bus power control is handled by interacting with PMIC.
2256 */
2257 host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
2258 host->quirks |= SDHCI_QUIRK_SINGLE_POWER_WRITE;
Sahitya Tummala00240122013-02-28 19:50:51 +05302259 host->quirks |= SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN;
2260 host->quirks2 |= SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK;
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302261 host->quirks2 |= SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING;
Krishna Kondaa20d3362013-04-01 21:01:59 -07002262 host->quirks2 |= SDHCI_QUIRK2_USE_MAX_DISCARD_SIZE;
Sahitya Tummalad2ae8832013-04-12 11:49:11 +05302263 host->quirks2 |= SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD;
Sahitya Tummalae6886bd2013-04-12 12:11:20 +05302264 host->quirks2 |= SDHCI_QUIRK2_BROKEN_PRESET_VALUE;
Sahitya Tummala4d12d0b2013-04-12 11:59:25 +05302265 host->quirks2 |= SDHCI_QUIRK2_USE_RESERVED_MAX_TIMEOUT;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302266
Stephen Boyd3edbd8f2013-04-24 14:19:46 -07002267 host_version = readw_relaxed((host->ioaddr + SDHCI_HOST_VERSION));
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002268 dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n",
2269 host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >>
2270 SDHCI_VENDOR_VER_SHIFT));
2271 if (((host_version & SDHCI_VENDOR_VER_MASK) >>
2272 SDHCI_VENDOR_VER_SHIFT) == SDHCI_VER_100) {
2273 /*
2274 * Add 40us delay in interrupt handler when
2275 * operating at initialization frequency(400KHz).
2276 */
2277 host->quirks2 |= SDHCI_QUIRK2_SLOW_INT_CLR;
2278 /*
2279 * Set Software Reset for DAT line in Software
2280 * Reset Register (Bit 2).
2281 */
2282 host->quirks2 |= SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT;
2283 }
2284
2285 /* Setup PWRCTL irq */
Asutosh Dasbbc84782013-02-11 15:31:35 +05302286 msm_host->pwr_irq = platform_get_irq_byname(pdev, "pwr_irq");
2287 if (msm_host->pwr_irq < 0) {
Asutosh Das33a4ff52012-12-18 16:14:02 +05302288 dev_err(&pdev->dev, "Failed to get pwr_irq by name (%d)\n",
Asutosh Dasbbc84782013-02-11 15:31:35 +05302289 msm_host->pwr_irq);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302290 goto vreg_deinit;
2291 }
Asutosh Dasbbc84782013-02-11 15:31:35 +05302292 ret = devm_request_threaded_irq(&pdev->dev, msm_host->pwr_irq, NULL,
Asutosh Das33a4ff52012-12-18 16:14:02 +05302293 sdhci_msm_pwr_irq, IRQF_ONESHOT,
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07002294 dev_name(&pdev->dev), host);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302295 if (ret) {
2296 dev_err(&pdev->dev, "Request threaded irq(%d) failed (%d)\n",
Asutosh Dasbbc84782013-02-11 15:31:35 +05302297 msm_host->pwr_irq, ret);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302298 goto vreg_deinit;
2299 }
2300
2301 /* Enable pwr irq interrupts */
2302 writel_relaxed(INT_MASK, (msm_host->core_mem + CORE_PWRCTL_MASK));
2303
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302304 /* Set clock gating delay to be used when CONFIG_MMC_CLKGATE is set */
2305 msm_host->mmc->clkgate_delay = SDHCI_MSM_MMC_CLK_GATE_DELAY;
2306
Asutosh Das33a4ff52012-12-18 16:14:02 +05302307 /* Set host capabilities */
2308 msm_host->mmc->caps |= msm_host->pdata->mmc_bus_width;
2309 msm_host->mmc->caps |= msm_host->pdata->caps;
2310
2311 vdd_max_current = sdhci_msm_get_vreg_vdd_max_current(msm_host);
2312 if (vdd_max_current >= 800)
2313 msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_800;
2314 else if (vdd_max_current >= 600)
2315 msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_600;
2316 else if (vdd_max_current >= 400)
2317 msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_400;
2318 else
2319 msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_200;
2320
2321 if (vdd_max_current > 150)
2322 msm_host->mmc->caps |= MMC_CAP_SET_XPC_180 |
2323 MMC_CAP_SET_XPC_300|
2324 MMC_CAP_SET_XPC_330;
2325
Sahitya Tummalacbe1b7a2013-02-28 12:21:58 +05302326 msm_host->mmc->caps |= MMC_CAP_HW_RESET;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302327 msm_host->mmc->caps2 |= msm_host->pdata->caps2;
Asutosh Dasbbc84782013-02-11 15:31:35 +05302328 msm_host->mmc->caps2 |= MMC_CAP2_CORE_RUNTIME_PM;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302329 msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR;
2330 msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR_CONTROL;
2331 msm_host->mmc->caps2 |= (MMC_CAP2_BOOTPART_NOACC |
2332 MMC_CAP2_DETECT_ON_ERR);
2333 msm_host->mmc->caps2 |= MMC_CAP2_SANITIZE;
2334 msm_host->mmc->caps2 |= MMC_CAP2_CACHE_CTRL;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302335 msm_host->mmc->caps2 |= MMC_CAP2_POWEROFF_NOTIFY;
Sahitya Tummala00240122013-02-28 19:50:51 +05302336 msm_host->mmc->caps2 |= MMC_CAP2_CLK_SCALE;
Konstantin Dorfmanfa436d52013-04-17 16:26:11 +03002337 msm_host->mmc->caps2 |= MMC_CAP2_STOP_REQUEST;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302338
2339 if (msm_host->pdata->nonremovable)
2340 msm_host->mmc->caps |= MMC_CAP_NONREMOVABLE;
2341
Sahitya Tummalab4e84042013-03-10 07:03:17 +05302342 host->cpu_dma_latency_us = msm_host->pdata->cpu_dma_latency_us;
2343
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302344 ret = sdhci_msm_bus_register(msm_host, pdev);
2345 if (ret)
2346 goto vreg_deinit;
2347
2348 if (msm_host->msm_bus_vote.client_handle)
2349 INIT_DELAYED_WORK(&msm_host->msm_bus_vote.vote_work,
2350 sdhci_msm_bus_work);
2351
Sahitya Tummala179e7382013-03-20 19:24:01 +05302352 init_completion(&msm_host->pwr_irq_completion);
2353
Sahitya Tummala62448d92013-03-12 14:57:46 +05302354 if (gpio_is_valid(msm_host->pdata->status_gpio)) {
2355 ret = mmc_cd_gpio_request(msm_host->mmc,
2356 msm_host->pdata->status_gpio);
2357 if (ret) {
2358 dev_err(&pdev->dev, "%s: Failed to request card detection IRQ %d\n",
2359 __func__, ret);
2360 goto bus_unregister;
2361 }
2362 }
2363
Sahitya Tummala2fa7eb12013-03-20 19:34:59 +05302364 if (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(32))) {
2365 host->dma_mask = DMA_BIT_MASK(32);
2366 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2367 } else {
2368 dev_err(&pdev->dev, "%s: Failed to set dma mask\n", __func__);
2369 }
2370
Asutosh Das33a4ff52012-12-18 16:14:02 +05302371 ret = sdhci_add_host(host);
2372 if (ret) {
2373 dev_err(&pdev->dev, "Add host failed (%d)\n", ret);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302374 goto free_cd_gpio;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302375 }
2376
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302377 msm_host->msm_bus_vote.max_bus_bw.show = show_sdhci_max_bus_bw;
2378 msm_host->msm_bus_vote.max_bus_bw.store = store_sdhci_max_bus_bw;
2379 sysfs_attr_init(&msm_host->msm_bus_vote.max_bus_bw.attr);
2380 msm_host->msm_bus_vote.max_bus_bw.attr.name = "max_bus_bw";
2381 msm_host->msm_bus_vote.max_bus_bw.attr.mode = S_IRUGO | S_IWUSR;
2382 ret = device_create_file(&pdev->dev,
2383 &msm_host->msm_bus_vote.max_bus_bw);
2384 if (ret)
2385 goto remove_host;
2386
Asutosh Dasbbc84782013-02-11 15:31:35 +05302387 ret = pm_runtime_set_active(&pdev->dev);
2388 if (ret)
2389 pr_err("%s: %s: pm_runtime_set_active failed: err: %d\n",
2390 mmc_hostname(host->mmc), __func__, ret);
2391 else
2392 pm_runtime_enable(&pdev->dev);
2393
Asutosh Das33a4ff52012-12-18 16:14:02 +05302394 /* Successful initialization */
2395 goto out;
2396
2397remove_host:
2398 dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
2399 sdhci_remove_host(host, dead);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302400free_cd_gpio:
2401 if (gpio_is_valid(msm_host->pdata->status_gpio))
2402 mmc_cd_gpio_free(msm_host->mmc);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302403bus_unregister:
2404 sdhci_msm_bus_unregister(msm_host);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302405vreg_deinit:
2406 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
2407clk_disable:
2408 if (!IS_ERR(msm_host->clk))
2409 clk_disable_unprepare(msm_host->clk);
2410pclk_disable:
2411 if (!IS_ERR(msm_host->pclk))
2412 clk_disable_unprepare(msm_host->pclk);
2413bus_clk_disable:
2414 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2415 clk_disable_unprepare(msm_host->bus_clk);
2416pltfm_free:
2417 sdhci_pltfm_free(pdev);
2418out:
2419 pr_debug("%s: Exit %s\n", dev_name(&pdev->dev), __func__);
2420 return ret;
2421}
2422
2423static int __devexit sdhci_msm_remove(struct platform_device *pdev)
2424{
2425 struct sdhci_host *host = platform_get_drvdata(pdev);
2426 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2427 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2428 struct sdhci_msm_pltfm_data *pdata = msm_host->pdata;
2429 int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) ==
2430 0xffffffff);
2431
2432 pr_debug("%s: %s\n", dev_name(&pdev->dev), __func__);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302433 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302434 sdhci_remove_host(host, dead);
Asutosh Dasbbc84782013-02-11 15:31:35 +05302435 pm_runtime_disable(&pdev->dev);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302436 sdhci_pltfm_free(pdev);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302437
2438 if (gpio_is_valid(msm_host->pdata->status_gpio))
2439 mmc_cd_gpio_free(msm_host->mmc);
2440
Asutosh Das33a4ff52012-12-18 16:14:02 +05302441 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302442
Asutosh Das33a4ff52012-12-18 16:14:02 +05302443 if (pdata->pin_data)
Asutosh Das390519d2012-12-21 12:21:42 +05302444 sdhci_msm_setup_pins(pdata, false);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302445
2446 if (msm_host->msm_bus_vote.client_handle) {
2447 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2448 sdhci_msm_bus_unregister(msm_host);
2449 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05302450 return 0;
2451}
2452
Asutosh Dasbbc84782013-02-11 15:31:35 +05302453static int sdhci_msm_runtime_suspend(struct device *dev)
2454{
2455 struct sdhci_host *host = dev_get_drvdata(dev);
2456 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2457 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2458
2459 disable_irq(host->irq);
2460 disable_irq(msm_host->pwr_irq);
2461
2462 return 0;
2463}
2464
2465static int sdhci_msm_runtime_resume(struct device *dev)
2466{
2467 struct sdhci_host *host = dev_get_drvdata(dev);
2468 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2469 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2470
2471 enable_irq(msm_host->pwr_irq);
2472 enable_irq(host->irq);
2473
2474 return 0;
2475}
2476
2477#ifdef CONFIG_PM_SLEEP
2478
2479static int sdhci_msm_suspend(struct device *dev)
2480{
2481 struct sdhci_host *host = dev_get_drvdata(dev);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302482 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2483 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Dasbbc84782013-02-11 15:31:35 +05302484 int ret = 0;
2485
Sahitya Tummala62448d92013-03-12 14:57:46 +05302486 if (gpio_is_valid(msm_host->pdata->status_gpio))
2487 mmc_cd_gpio_free(msm_host->mmc);
2488
Asutosh Dasbbc84782013-02-11 15:31:35 +05302489 if (pm_runtime_suspended(dev)) {
2490 pr_debug("%s: %s: already runtime suspended\n",
2491 mmc_hostname(host->mmc), __func__);
2492 goto out;
2493 }
2494
Sahitya Tummala8fcd7d92013-04-12 14:19:25 +05302495 if (msm_host->msm_bus_vote.client_handle)
2496 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2497
Asutosh Dasbbc84782013-02-11 15:31:35 +05302498 return sdhci_msm_runtime_suspend(dev);
2499out:
2500 return ret;
2501}
2502
2503static int sdhci_msm_resume(struct device *dev)
2504{
2505 struct sdhci_host *host = dev_get_drvdata(dev);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302506 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2507 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Dasbbc84782013-02-11 15:31:35 +05302508 int ret = 0;
2509
Sahitya Tummala62448d92013-03-12 14:57:46 +05302510 if (gpio_is_valid(msm_host->pdata->status_gpio)) {
2511 ret = mmc_cd_gpio_request(msm_host->mmc,
2512 msm_host->pdata->status_gpio);
2513 if (ret)
2514 pr_err("%s: %s: Failed to request card detection IRQ %d\n",
2515 mmc_hostname(host->mmc), __func__, ret);
2516 }
2517
Asutosh Dasbbc84782013-02-11 15:31:35 +05302518 if (pm_runtime_suspended(dev)) {
2519 pr_debug("%s: %s: runtime suspended, defer system resume\n",
2520 mmc_hostname(host->mmc), __func__);
2521 goto out;
2522 }
2523
2524 return sdhci_msm_runtime_resume(dev);
2525out:
2526 return ret;
2527}
2528#endif
2529
2530#ifdef CONFIG_PM
2531static const struct dev_pm_ops sdhci_msm_pmops = {
2532 SET_SYSTEM_SLEEP_PM_OPS(sdhci_msm_suspend, sdhci_msm_resume)
2533 SET_RUNTIME_PM_OPS(sdhci_msm_runtime_suspend, sdhci_msm_runtime_resume,
2534 NULL)
2535};
2536
2537#define SDHCI_MSM_PMOPS (&sdhci_msm_pmops)
2538
2539#else
2540#define SDHCI_PM_OPS NULL
2541#endif
Asutosh Das33a4ff52012-12-18 16:14:02 +05302542static const struct of_device_id sdhci_msm_dt_match[] = {
2543 {.compatible = "qcom,sdhci-msm"},
2544};
2545MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match);
2546
2547static struct platform_driver sdhci_msm_driver = {
2548 .probe = sdhci_msm_probe,
2549 .remove = __devexit_p(sdhci_msm_remove),
2550 .driver = {
2551 .name = "sdhci_msm",
2552 .owner = THIS_MODULE,
2553 .of_match_table = sdhci_msm_dt_match,
Asutosh Dasbbc84782013-02-11 15:31:35 +05302554 .pm = SDHCI_MSM_PMOPS,
Asutosh Das33a4ff52012-12-18 16:14:02 +05302555 },
2556};
2557
2558module_platform_driver(sdhci_msm_driver);
2559
2560MODULE_DESCRIPTION("Qualcomm Secure Digital Host Controller Interface driver");
2561MODULE_LICENSE("GPL v2");