blob: 33dfad2caa8ebefe84285d6b5ae3586ae757c5f5 [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
Venkat Gopalakrishnan0a179c82013-06-26 17:56:11 -070092#define CORE_MCI_VERSION 0x050
93#define CORE_VERSION_310 0x10000011
94
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +030095/*
96 * Waiting until end of potential AHB access for data:
97 * 16 AHB cycles (160ns for 100MHz and 320ns for 50MHz) +
98 * delay on AHB (2us) = maximum 2.32us
99 * Taking x10 times margin
100 */
101#define CORE_AHB_DATA_DELAY_US 23
102/* Waiting until end of potential AHB access for descriptor:
103 * Single (1 AHB cycle) + delay on AHB bus = max 2us
104 * INCR4 (4 AHB cycles) + delay on AHB bus = max 2us
105 * Single (1 AHB cycle) + delay on AHB bus = max 2us
106 * Total 8 us delay with margin
107 */
108#define CORE_AHB_DESC_DELAY_US 8
109
110#define CORE_SDCC_DEBUG_REG 0x124
111#define CORE_DEBUG_REG_AHB_HTRANS (3 << 12)
112
Asutosh Das3781bd82013-01-10 21:11:04 +0530113/* 8KB descriptors */
114#define SDHCI_MSM_MAX_SEGMENTS (1 << 13)
Sahitya Tummala04c3a462013-01-11 11:30:45 +0530115#define SDHCI_MSM_MMC_CLK_GATE_DELAY 200 /* msecs */
Asutosh Das3781bd82013-01-10 21:11:04 +0530116
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700117static const u32 tuning_block_64[] = {
118 0x00FF0FFF, 0xCCC3CCFF, 0xFFCC3CC3, 0xEFFEFFFE,
119 0xDDFFDFFF, 0xFBFFFBFF, 0xFF7FFFBF, 0xEFBDF777,
120 0xF0FFF0FF, 0x3CCCFC0F, 0xCFCC33CC, 0xEEFFEFFF,
121 0xFDFFFDFF, 0xFFBFFFDF, 0xFFF7FFBB, 0xDE7B7FF7
122};
123
124static const u32 tuning_block_128[] = {
125 0xFF00FFFF, 0x0000FFFF, 0xCCCCFFFF, 0xCCCC33CC,
126 0xCC3333CC, 0xFFFFCCCC, 0xFFFFEEFF, 0xFFEEEEFF,
127 0xFFDDFFFF, 0xDDDDFFFF, 0xBBFFFFFF, 0xBBFFFFFF,
128 0xFFFFFFBB, 0xFFFFFF77, 0x77FF7777, 0xFFEEDDBB,
129 0x00FFFFFF, 0x00FFFFFF, 0xCCFFFF00, 0xCC33CCCC,
130 0x3333CCCC, 0xFFCCCCCC, 0xFFEEFFFF, 0xEEEEFFFF,
131 0xDDFFFFFF, 0xDDFFFFFF, 0xFFFFFFDD, 0xFFFFFFBB,
132 0xFFFFBBBB, 0xFFFF77FF, 0xFF7777FF, 0xEEDDBB77
133};
Asutosh Das33a4ff52012-12-18 16:14:02 +0530134
Venkat Gopalakrishnanc61ab7e2013-03-11 12:17:57 -0700135static int disable_slots;
136/* root can write, others read */
137module_param(disable_slots, int, S_IRUGO|S_IWUSR);
138
Asutosh Das33a4ff52012-12-18 16:14:02 +0530139/* This structure keeps information per regulator */
140struct sdhci_msm_reg_data {
141 /* voltage regulator handle */
142 struct regulator *reg;
143 /* regulator name */
144 const char *name;
145 /* voltage level to be set */
146 u32 low_vol_level;
147 u32 high_vol_level;
148 /* Load values for low power and high power mode */
149 u32 lpm_uA;
150 u32 hpm_uA;
151
152 /* is this regulator enabled? */
153 bool is_enabled;
154 /* is this regulator needs to be always on? */
155 bool is_always_on;
156 /* is low power mode setting required for this regulator? */
157 bool lpm_sup;
Asutosh Das95afcad2013-06-28 15:03:44 +0530158 bool set_voltage_sup;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530159};
160
161/*
162 * This structure keeps information for all the
163 * regulators required for a SDCC slot.
164 */
165struct sdhci_msm_slot_reg_data {
166 /* keeps VDD/VCC regulator info */
167 struct sdhci_msm_reg_data *vdd_data;
168 /* keeps VDD IO regulator info */
169 struct sdhci_msm_reg_data *vdd_io_data;
170};
171
172struct sdhci_msm_gpio {
173 u32 no;
174 const char *name;
175 bool is_enabled;
176};
177
178struct sdhci_msm_gpio_data {
179 struct sdhci_msm_gpio *gpio;
180 u8 size;
181};
182
Asutosh Das390519d2012-12-21 12:21:42 +0530183struct sdhci_msm_pad_pull {
184 enum msm_tlmm_pull_tgt no;
185 u32 val;
186};
187
188struct sdhci_msm_pad_pull_data {
189 struct sdhci_msm_pad_pull *on;
190 struct sdhci_msm_pad_pull *off;
191 u8 size;
192};
193
194struct sdhci_msm_pad_drv {
195 enum msm_tlmm_hdrive_tgt no;
196 u32 val;
197};
198
199struct sdhci_msm_pad_drv_data {
200 struct sdhci_msm_pad_drv *on;
201 struct sdhci_msm_pad_drv *off;
202 u8 size;
203};
204
205struct sdhci_msm_pad_data {
206 struct sdhci_msm_pad_pull_data *pull;
207 struct sdhci_msm_pad_drv_data *drv;
208};
209
210
Asutosh Das33a4ff52012-12-18 16:14:02 +0530211struct sdhci_msm_pin_data {
212 /*
213 * = 1 if controller pins are using gpios
214 * = 0 if controller has dedicated MSM pads
215 */
Asutosh Das390519d2012-12-21 12:21:42 +0530216 u8 is_gpio;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530217 bool cfg_sts;
218 struct sdhci_msm_gpio_data *gpio_data;
Asutosh Das390519d2012-12-21 12:21:42 +0530219 struct sdhci_msm_pad_data *pad_data;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530220};
221
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +0530222struct sdhci_msm_bus_voting_data {
223 struct msm_bus_scale_pdata *bus_pdata;
224 unsigned int *bw_vecs;
225 unsigned int bw_vecs_size;
226};
227
Asutosh Das33a4ff52012-12-18 16:14:02 +0530228struct sdhci_msm_pltfm_data {
229 /* Supported UHS-I Modes */
230 u32 caps;
231
232 /* More capabilities */
233 u32 caps2;
234
235 unsigned long mmc_bus_width;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530236 struct sdhci_msm_slot_reg_data *vreg_data;
237 bool nonremovable;
238 struct sdhci_msm_pin_data *pin_data;
Sahitya Tummalab4e84042013-03-10 07:03:17 +0530239 u32 cpu_dma_latency_us;
Sahitya Tummala62448d92013-03-12 14:57:46 +0530240 int status_gpio; /* card detection GPIO that is configured as IRQ */
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +0530241 struct sdhci_msm_bus_voting_data *voting_data;
Sahitya Tummala00240122013-02-28 19:50:51 +0530242 u32 *sup_clk_table;
243 unsigned char sup_clk_cnt;
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +0530244};
245
246struct sdhci_msm_bus_vote {
247 uint32_t client_handle;
248 uint32_t curr_vote;
249 int min_bw_vote;
250 int max_bw_vote;
251 bool is_max_bw_needed;
252 struct delayed_work vote_work;
253 struct device_attribute max_bus_bw;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530254};
255
256struct sdhci_msm_host {
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +0530257 struct platform_device *pdev;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530258 void __iomem *core_mem; /* MSM SDCC mapped address */
Asutosh Dasbbc84782013-02-11 15:31:35 +0530259 int pwr_irq; /* power irq */
Asutosh Das33a4ff52012-12-18 16:14:02 +0530260 struct clk *clk; /* main SD/MMC bus clock */
261 struct clk *pclk; /* SDHC peripheral bus clock */
262 struct clk *bus_clk; /* SDHC bus voter clock */
Sahitya Tummala04c3a462013-01-11 11:30:45 +0530263 atomic_t clks_on; /* Set if clocks are enabled */
Asutosh Das33a4ff52012-12-18 16:14:02 +0530264 struct sdhci_msm_pltfm_data *pdata;
265 struct mmc_host *mmc;
266 struct sdhci_pltfm_data sdhci_msm_pdata;
Sahitya Tummala179e7382013-03-20 19:24:01 +0530267 u32 curr_pwr_state;
268 u32 curr_io_level;
269 struct completion pwr_irq_completion;
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +0530270 struct sdhci_msm_bus_vote msm_bus_vote;
Sahitya Tummala3b292c32013-06-20 14:00:18 +0530271 struct device_attribute polling;
Sahitya Tummala00240122013-02-28 19:50:51 +0530272 u32 clk_rate; /* Keeps track of current clock rate that is set */
Asutosh Das33a4ff52012-12-18 16:14:02 +0530273};
274
275enum vdd_io_level {
276 /* set vdd_io_data->low_vol_level */
277 VDD_IO_LOW,
278 /* set vdd_io_data->high_vol_level */
279 VDD_IO_HIGH,
280 /*
281 * set whatever there in voltage_level (third argument) of
282 * sdhci_msm_set_vdd_io_vol() function.
283 */
284 VDD_IO_SET_LEVEL,
285};
286
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700287/* MSM platform specific tuning */
288static inline int msm_dll_poll_ck_out_en(struct sdhci_host *host,
289 u8 poll)
290{
291 int rc = 0;
292 u32 wait_cnt = 50;
293 u8 ck_out_en = 0;
294 struct mmc_host *mmc = host->mmc;
295
296 /* poll for CK_OUT_EN bit. max. poll time = 50us */
297 ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) &
298 CORE_CK_OUT_EN);
299
300 while (ck_out_en != poll) {
301 if (--wait_cnt == 0) {
302 pr_err("%s: %s: CK_OUT_EN bit is not %d\n",
303 mmc_hostname(mmc), __func__, poll);
304 rc = -ETIMEDOUT;
305 goto out;
306 }
307 udelay(1);
308
309 ck_out_en = !!(readl_relaxed(host->ioaddr +
310 CORE_DLL_CONFIG) & CORE_CK_OUT_EN);
311 }
312out:
313 return rc;
314}
315
316static int msm_config_cm_dll_phase(struct sdhci_host *host, u8 phase)
317{
318 int rc = 0;
319 u8 grey_coded_phase_table[] = {0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4,
320 0xC, 0xD, 0xF, 0xE, 0xA, 0xB, 0x9,
321 0x8};
322 unsigned long flags;
323 u32 config;
324 struct mmc_host *mmc = host->mmc;
325
326 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
327 spin_lock_irqsave(&host->lock, flags);
328
329 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
330 config &= ~(CORE_CDR_EN | CORE_CK_OUT_EN);
331 config |= (CORE_CDR_EXT_EN | CORE_DLL_EN);
332 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
333
334 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '0' */
335 rc = msm_dll_poll_ck_out_en(host, 0);
336 if (rc)
337 goto err_out;
338
339 /*
340 * Write the selected DLL clock output phase (0 ... 15)
341 * to CDR_SELEXT bit field of DLL_CONFIG register.
342 */
343 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
344 & ~(0xF << 20))
345 | (grey_coded_phase_table[phase] << 20)),
346 host->ioaddr + CORE_DLL_CONFIG);
347
348 /* Set CK_OUT_EN bit of DLL_CONFIG register to 1. */
349 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
350 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
351
352 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '1' */
353 rc = msm_dll_poll_ck_out_en(host, 1);
354 if (rc)
355 goto err_out;
356
357 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
358 config |= CORE_CDR_EN;
359 config &= ~CORE_CDR_EXT_EN;
360 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
361 goto out;
362
363err_out:
364 pr_err("%s: %s: Failed to set DLL phase: %d\n",
365 mmc_hostname(mmc), __func__, phase);
366out:
367 spin_unlock_irqrestore(&host->lock, flags);
368 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
369 return rc;
370}
371
372/*
373 * Find out the greatest range of consecuitive selected
374 * DLL clock output phases that can be used as sampling
375 * setting for SD3.0 UHS-I card read operation (in SDR104
376 * timing mode) or for eMMC4.5 card read operation (in HS200
377 * timing mode).
378 * Select the 3/4 of the range and configure the DLL with the
379 * selected DLL clock output phase.
380 */
381
382static int msm_find_most_appropriate_phase(struct sdhci_host *host,
383 u8 *phase_table, u8 total_phases)
384{
385 int ret;
386 u8 ranges[MAX_PHASES][MAX_PHASES] = { {0}, {0} };
387 u8 phases_per_row[MAX_PHASES] = {0};
388 int row_index = 0, col_index = 0, selected_row_index = 0, curr_max = 0;
389 int i, cnt, phase_0_raw_index = 0, phase_15_raw_index = 0;
390 bool phase_0_found = false, phase_15_found = false;
391 struct mmc_host *mmc = host->mmc;
392
393 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
394 if (!total_phases || (total_phases > MAX_PHASES)) {
395 pr_err("%s: %s: invalid argument: total_phases=%d\n",
396 mmc_hostname(mmc), __func__, total_phases);
397 return -EINVAL;
398 }
399
400 for (cnt = 0; cnt < total_phases; cnt++) {
401 ranges[row_index][col_index] = phase_table[cnt];
402 phases_per_row[row_index] += 1;
403 col_index++;
404
405 if ((cnt + 1) == total_phases) {
406 continue;
407 /* check if next phase in phase_table is consecutive or not */
408 } else if ((phase_table[cnt] + 1) != phase_table[cnt + 1]) {
409 row_index++;
410 col_index = 0;
411 }
412 }
413
414 if (row_index >= MAX_PHASES)
415 return -EINVAL;
416
417 /* Check if phase-0 is present in first valid window? */
418 if (!ranges[0][0]) {
419 phase_0_found = true;
420 phase_0_raw_index = 0;
421 /* Check if cycle exist between 2 valid windows */
422 for (cnt = 1; cnt <= row_index; cnt++) {
423 if (phases_per_row[cnt]) {
424 for (i = 0; i < phases_per_row[cnt]; i++) {
425 if (ranges[cnt][i] == 15) {
426 phase_15_found = true;
427 phase_15_raw_index = cnt;
428 break;
429 }
430 }
431 }
432 }
433 }
434
435 /* If 2 valid windows form cycle then merge them as single window */
436 if (phase_0_found && phase_15_found) {
437 /* number of phases in raw where phase 0 is present */
438 u8 phases_0 = phases_per_row[phase_0_raw_index];
439 /* number of phases in raw where phase 15 is present */
440 u8 phases_15 = phases_per_row[phase_15_raw_index];
441
442 if (phases_0 + phases_15 >= MAX_PHASES)
443 /*
444 * If there are more than 1 phase windows then total
445 * number of phases in both the windows should not be
446 * more than or equal to MAX_PHASES.
447 */
448 return -EINVAL;
449
450 /* Merge 2 cyclic windows */
451 i = phases_15;
452 for (cnt = 0; cnt < phases_0; cnt++) {
453 ranges[phase_15_raw_index][i] =
454 ranges[phase_0_raw_index][cnt];
455 if (++i >= MAX_PHASES)
456 break;
457 }
458
459 phases_per_row[phase_0_raw_index] = 0;
460 phases_per_row[phase_15_raw_index] = phases_15 + phases_0;
461 }
462
463 for (cnt = 0; cnt <= row_index; cnt++) {
464 if (phases_per_row[cnt] > curr_max) {
465 curr_max = phases_per_row[cnt];
466 selected_row_index = cnt;
467 }
468 }
469
470 i = ((curr_max * 3) / 4);
471 if (i)
472 i--;
473
474 ret = (int)ranges[selected_row_index][i];
475
476 if (ret >= MAX_PHASES) {
477 ret = -EINVAL;
478 pr_err("%s: %s: invalid phase selected=%d\n",
479 mmc_hostname(mmc), __func__, ret);
480 }
481
482 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
483 return ret;
484}
485
486static inline void msm_cm_dll_set_freq(struct sdhci_host *host)
487{
488 u32 mclk_freq = 0;
489
490 /* Program the MCLK value to MCLK_FREQ bit field */
491 if (host->clock <= 112000000)
492 mclk_freq = 0;
493 else if (host->clock <= 125000000)
494 mclk_freq = 1;
495 else if (host->clock <= 137000000)
496 mclk_freq = 2;
497 else if (host->clock <= 150000000)
498 mclk_freq = 3;
499 else if (host->clock <= 162000000)
500 mclk_freq = 4;
501 else if (host->clock <= 175000000)
502 mclk_freq = 5;
503 else if (host->clock <= 187000000)
504 mclk_freq = 6;
505 else if (host->clock <= 200000000)
506 mclk_freq = 7;
507
508 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
509 & ~(7 << 24)) | (mclk_freq << 24)),
510 host->ioaddr + CORE_DLL_CONFIG);
511}
512
513/* Initialize the DLL (Programmable Delay Line ) */
514static int msm_init_cm_dll(struct sdhci_host *host)
515{
516 struct mmc_host *mmc = host->mmc;
517 int rc = 0;
518 unsigned long flags;
519 u32 wait_cnt;
Subhash Jadavaniefb5f622013-05-28 18:21:57 +0530520 bool prev_pwrsave, curr_pwrsave;
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700521
522 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
523 spin_lock_irqsave(&host->lock, flags);
Subhash Jadavaniefb5f622013-05-28 18:21:57 +0530524 prev_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
525 CORE_CLK_PWRSAVE);
526 curr_pwrsave = prev_pwrsave;
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700527 /*
528 * Make sure that clock is always enabled when DLL
529 * tuning is in progress. Keeping PWRSAVE ON may
530 * turn off the clock. So let's disable the PWRSAVE
531 * here and re-enable it once tuning is completed.
532 */
Subhash Jadavaniefb5f622013-05-28 18:21:57 +0530533 if (prev_pwrsave) {
534 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
535 & ~CORE_CLK_PWRSAVE),
536 host->ioaddr + CORE_VENDOR_SPEC);
537 curr_pwrsave = false;
538 }
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700539
540 /* Write 1 to DLL_RST bit of DLL_CONFIG register */
541 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
542 | CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
543
544 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
545 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
546 | CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
547 msm_cm_dll_set_freq(host);
548
549 /* Write 0 to DLL_RST bit of DLL_CONFIG register */
550 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
551 & ~CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
552
553 /* Write 0 to DLL_PDN bit of DLL_CONFIG register */
554 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
555 & ~CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
556
557 /* Set DLL_EN bit to 1. */
558 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
559 | CORE_DLL_EN), host->ioaddr + CORE_DLL_CONFIG);
560
561 /* Set CK_OUT_EN bit to 1. */
562 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
563 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
564
565 wait_cnt = 50;
566 /* Wait until DLL_LOCK bit of DLL_STATUS register becomes '1' */
567 while (!(readl_relaxed(host->ioaddr + CORE_DLL_STATUS) &
568 CORE_DLL_LOCK)) {
569 /* max. wait for 50us sec for LOCK bit to be set */
570 if (--wait_cnt == 0) {
571 pr_err("%s: %s: DLL failed to LOCK\n",
572 mmc_hostname(mmc), __func__);
573 rc = -ETIMEDOUT;
574 goto out;
575 }
576 /* wait for 1us before polling again */
577 udelay(1);
578 }
579
580out:
Subhash Jadavaniefb5f622013-05-28 18:21:57 +0530581 /* Restore the correct PWRSAVE state */
582 if (prev_pwrsave ^ curr_pwrsave) {
583 u32 reg = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC);
584
585 if (prev_pwrsave)
586 reg |= CORE_CLK_PWRSAVE;
587 else
588 reg &= ~CORE_CLK_PWRSAVE;
589
590 writel_relaxed(reg, host->ioaddr + CORE_VENDOR_SPEC);
591 }
592
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700593 spin_unlock_irqrestore(&host->lock, flags);
594 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
595 return rc;
596}
597
598int sdhci_msm_execute_tuning(struct sdhci_host *host, u32 opcode)
599{
600 unsigned long flags;
Sahitya Tummala714e9642013-06-13 10:36:57 +0530601 int tuning_seq_cnt = 3;
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700602 u8 phase, *data_buf, tuned_phases[16], tuned_phase_cnt = 0;
603 const u32 *tuning_block_pattern = tuning_block_64;
604 int size = sizeof(tuning_block_64); /* Tuning pattern size in bytes */
605 int rc;
606 struct mmc_host *mmc = host->mmc;
Sahitya Tummala00240122013-02-28 19:50:51 +0530607 struct mmc_ios ios = host->mmc->ios;
608
609 /*
610 * Tuning is required for SDR104 and HS200 cards and if clock frequency
611 * is greater than 100MHz in these modes.
612 */
613 if (host->clock <= (100 * 1000 * 1000) ||
614 !(ios.timing == MMC_TIMING_MMC_HS200 ||
615 ios.timing == MMC_TIMING_UHS_SDR104))
616 return 0;
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700617
618 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700619 spin_lock_irqsave(&host->lock, flags);
620
621 if ((opcode == MMC_SEND_TUNING_BLOCK_HS200) &&
622 (mmc->ios.bus_width == MMC_BUS_WIDTH_8)) {
623 tuning_block_pattern = tuning_block_128;
624 size = sizeof(tuning_block_128);
625 }
626 spin_unlock_irqrestore(&host->lock, flags);
627
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700628 data_buf = kmalloc(size, GFP_KERNEL);
629 if (!data_buf) {
630 rc = -ENOMEM;
631 goto out;
632 }
633
Sahitya Tummala714e9642013-06-13 10:36:57 +0530634retry:
635 /* first of all reset the tuning block */
636 rc = msm_init_cm_dll(host);
637 if (rc)
638 goto kfree;
639
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700640 phase = 0;
641 do {
642 struct mmc_command cmd = {0};
643 struct mmc_data data = {0};
644 struct mmc_request mrq = {
645 .cmd = &cmd,
646 .data = &data
647 };
648 struct scatterlist sg;
649
650 /* set the phase in delay line hw block */
651 rc = msm_config_cm_dll_phase(host, phase);
652 if (rc)
653 goto kfree;
654
655 cmd.opcode = opcode;
656 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
657
658 data.blksz = size;
659 data.blocks = 1;
660 data.flags = MMC_DATA_READ;
661 data.timeout_ns = 1000 * 1000 * 1000; /* 1 sec */
662
663 data.sg = &sg;
664 data.sg_len = 1;
665 sg_init_one(&sg, data_buf, size);
666 memset(data_buf, 0, size);
667 mmc_wait_for_req(mmc, &mrq);
668
669 if (!cmd.error && !data.error &&
670 !memcmp(data_buf, tuning_block_pattern, size)) {
671 /* tuning is successful at this tuning point */
672 tuned_phases[tuned_phase_cnt++] = phase;
673 pr_debug("%s: %s: found good phase = %d\n",
674 mmc_hostname(mmc), __func__, phase);
675 }
676 } while (++phase < 16);
677
678 if (tuned_phase_cnt) {
679 rc = msm_find_most_appropriate_phase(host, tuned_phases,
680 tuned_phase_cnt);
681 if (rc < 0)
682 goto kfree;
683 else
684 phase = (u8)rc;
685
686 /*
687 * Finally set the selected phase in delay
688 * line hw block.
689 */
690 rc = msm_config_cm_dll_phase(host, phase);
691 if (rc)
692 goto kfree;
693 pr_debug("%s: %s: finally setting the tuning phase to %d\n",
694 mmc_hostname(mmc), __func__, phase);
695 } else {
Sahitya Tummala714e9642013-06-13 10:36:57 +0530696 if (--tuning_seq_cnt)
697 goto retry;
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700698 /* tuning failed */
699 pr_err("%s: %s: no tuning point found\n",
700 mmc_hostname(mmc), __func__);
Sahitya Tummala714e9642013-06-13 10:36:57 +0530701 rc = -EIO;
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700702 }
703
704kfree:
705 kfree(data_buf);
706out:
707 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
708 return rc;
709}
710
Asutosh Das33a4ff52012-12-18 16:14:02 +0530711static int sdhci_msm_setup_gpio(struct sdhci_msm_pltfm_data *pdata, bool enable)
712{
713 struct sdhci_msm_gpio_data *curr;
714 int i, ret = 0;
715
716 curr = pdata->pin_data->gpio_data;
717 for (i = 0; i < curr->size; i++) {
718 if (!gpio_is_valid(curr->gpio[i].no)) {
719 ret = -EINVAL;
720 pr_err("%s: Invalid gpio = %d\n", __func__,
721 curr->gpio[i].no);
722 goto free_gpios;
723 }
724 if (enable) {
725 ret = gpio_request(curr->gpio[i].no,
726 curr->gpio[i].name);
727 if (ret) {
728 pr_err("%s: gpio_request(%d, %s) failed %d\n",
729 __func__, curr->gpio[i].no,
730 curr->gpio[i].name, ret);
731 goto free_gpios;
732 }
733 curr->gpio[i].is_enabled = true;
734 } else {
735 gpio_free(curr->gpio[i].no);
736 curr->gpio[i].is_enabled = false;
737 }
738 }
739 return ret;
740
741free_gpios:
742 for (i--; i >= 0; i--) {
743 gpio_free(curr->gpio[i].no);
744 curr->gpio[i].is_enabled = false;
745 }
746 return ret;
747}
748
Asutosh Das390519d2012-12-21 12:21:42 +0530749static int sdhci_msm_setup_pad(struct sdhci_msm_pltfm_data *pdata, bool enable)
750{
751 struct sdhci_msm_pad_data *curr;
752 int i;
753
754 curr = pdata->pin_data->pad_data;
755 for (i = 0; i < curr->drv->size; i++) {
756 if (enable)
757 msm_tlmm_set_hdrive(curr->drv->on[i].no,
758 curr->drv->on[i].val);
759 else
760 msm_tlmm_set_hdrive(curr->drv->off[i].no,
761 curr->drv->off[i].val);
762 }
763
764 for (i = 0; i < curr->pull->size; i++) {
765 if (enable)
766 msm_tlmm_set_pull(curr->pull->on[i].no,
767 curr->pull->on[i].val);
768 else
769 msm_tlmm_set_pull(curr->pull->off[i].no,
770 curr->pull->off[i].val);
771 }
772
773 return 0;
774}
775
Asutosh Das33a4ff52012-12-18 16:14:02 +0530776static int sdhci_msm_setup_pins(struct sdhci_msm_pltfm_data *pdata, bool enable)
777{
778 int ret = 0;
779
780 if (!pdata->pin_data || (pdata->pin_data->cfg_sts == enable))
781 return 0;
Asutosh Das390519d2012-12-21 12:21:42 +0530782 if (pdata->pin_data->is_gpio)
783 ret = sdhci_msm_setup_gpio(pdata, enable);
784 else
785 ret = sdhci_msm_setup_pad(pdata, enable);
Asutosh Das33a4ff52012-12-18 16:14:02 +0530786
Asutosh Das33a4ff52012-12-18 16:14:02 +0530787 if (!ret)
788 pdata->pin_data->cfg_sts = enable;
789
790 return ret;
791}
792
Asutosh Das390519d2012-12-21 12:21:42 +0530793static int sdhci_msm_dt_get_array(struct device *dev, const char *prop_name,
794 u32 **out, int *len, u32 size)
795{
796 int ret = 0;
797 struct device_node *np = dev->of_node;
798 size_t sz;
799 u32 *arr = NULL;
800
801 if (!of_get_property(np, prop_name, len)) {
802 ret = -EINVAL;
803 goto out;
804 }
805 sz = *len = *len / sizeof(*arr);
806 if (sz <= 0 || (size > 0 && (sz != size))) {
807 dev_err(dev, "%s invalid size\n", prop_name);
808 ret = -EINVAL;
809 goto out;
810 }
811
812 arr = devm_kzalloc(dev, sz * sizeof(*arr), GFP_KERNEL);
813 if (!arr) {
814 dev_err(dev, "%s failed allocating memory\n", prop_name);
815 ret = -ENOMEM;
816 goto out;
817 }
818
819 ret = of_property_read_u32_array(np, prop_name, arr, sz);
820 if (ret < 0) {
821 dev_err(dev, "%s failed reading array %d\n", prop_name, ret);
822 goto out;
823 }
824 *out = arr;
825out:
826 if (ret)
827 *len = 0;
828 return ret;
829}
830
Asutosh Das33a4ff52012-12-18 16:14:02 +0530831#define MAX_PROP_SIZE 32
832static int sdhci_msm_dt_parse_vreg_info(struct device *dev,
833 struct sdhci_msm_reg_data **vreg_data, const char *vreg_name)
834{
835 int len, ret = 0;
836 const __be32 *prop;
837 char prop_name[MAX_PROP_SIZE];
838 struct sdhci_msm_reg_data *vreg;
839 struct device_node *np = dev->of_node;
840
841 snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", vreg_name);
842 if (!of_parse_phandle(np, prop_name, 0)) {
Asutosh Das95afcad2013-06-28 15:03:44 +0530843 dev_info(dev, "No vreg data found for %s\n", vreg_name);
Asutosh Das33a4ff52012-12-18 16:14:02 +0530844 return ret;
845 }
846
847 vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
848 if (!vreg) {
849 dev_err(dev, "No memory for vreg: %s\n", vreg_name);
850 ret = -ENOMEM;
851 return ret;
852 }
853
854 vreg->name = vreg_name;
855
856 snprintf(prop_name, MAX_PROP_SIZE,
857 "qcom,%s-always-on", vreg_name);
858 if (of_get_property(np, prop_name, NULL))
859 vreg->is_always_on = true;
860
861 snprintf(prop_name, MAX_PROP_SIZE,
862 "qcom,%s-lpm-sup", vreg_name);
863 if (of_get_property(np, prop_name, NULL))
864 vreg->lpm_sup = true;
865
866 snprintf(prop_name, MAX_PROP_SIZE,
867 "qcom,%s-voltage-level", vreg_name);
868 prop = of_get_property(np, prop_name, &len);
869 if (!prop || (len != (2 * sizeof(__be32)))) {
870 dev_warn(dev, "%s %s property\n",
871 prop ? "invalid format" : "no", prop_name);
872 } else {
873 vreg->low_vol_level = be32_to_cpup(&prop[0]);
874 vreg->high_vol_level = be32_to_cpup(&prop[1]);
875 }
876
877 snprintf(prop_name, MAX_PROP_SIZE,
878 "qcom,%s-current-level", vreg_name);
879 prop = of_get_property(np, prop_name, &len);
880 if (!prop || (len != (2 * sizeof(__be32)))) {
881 dev_warn(dev, "%s %s property\n",
882 prop ? "invalid format" : "no", prop_name);
883 } else {
884 vreg->lpm_uA = be32_to_cpup(&prop[0]);
885 vreg->hpm_uA = be32_to_cpup(&prop[1]);
886 }
887
888 *vreg_data = vreg;
889 dev_dbg(dev, "%s: %s %s vol=[%d %d]uV, curr=[%d %d]uA\n",
890 vreg->name, vreg->is_always_on ? "always_on," : "",
891 vreg->lpm_sup ? "lpm_sup," : "", vreg->low_vol_level,
892 vreg->high_vol_level, vreg->lpm_uA, vreg->hpm_uA);
893
894 return ret;
895}
896
Asutosh Das390519d2012-12-21 12:21:42 +0530897/* GPIO/Pad data extraction */
898static int sdhci_msm_dt_get_pad_pull_info(struct device *dev, int id,
899 struct sdhci_msm_pad_pull_data **pad_pull_data)
900{
901 int ret = 0, base = 0, len, i;
902 u32 *tmp;
903 struct sdhci_msm_pad_pull_data *pull_data;
904 struct sdhci_msm_pad_pull *pull;
905
906 switch (id) {
907 case 1:
908 base = TLMM_PULL_SDC1_CLK;
909 break;
910 case 2:
911 base = TLMM_PULL_SDC2_CLK;
912 break;
913 case 3:
914 base = TLMM_PULL_SDC3_CLK;
915 break;
916 case 4:
917 base = TLMM_PULL_SDC4_CLK;
918 break;
919 default:
920 dev_err(dev, "%s: Invalid slot id\n", __func__);
921 ret = -EINVAL;
922 goto out;
923 }
924
925 pull_data = devm_kzalloc(dev, sizeof(struct sdhci_msm_pad_pull_data),
926 GFP_KERNEL);
927 if (!pull_data) {
928 dev_err(dev, "No memory for msm_mmc_pad_pull_data\n");
929 ret = -ENOMEM;
930 goto out;
931 }
932 pull_data->size = 3; /* array size for clk, cmd, data */
933
934 /* Allocate on, off configs for clk, cmd, data */
935 pull = devm_kzalloc(dev, 2 * pull_data->size *\
936 sizeof(struct sdhci_msm_pad_pull), GFP_KERNEL);
937 if (!pull) {
938 dev_err(dev, "No memory for msm_mmc_pad_pull\n");
939 ret = -ENOMEM;
940 goto out;
941 }
942 pull_data->on = pull;
943 pull_data->off = pull + pull_data->size;
944
945 ret = sdhci_msm_dt_get_array(dev, "qcom,pad-pull-on",
946 &tmp, &len, pull_data->size);
947 if (ret)
948 goto out;
949
950 for (i = 0; i < len; i++) {
951 pull_data->on[i].no = base + i;
952 pull_data->on[i].val = tmp[i];
953 dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
954 i, pull_data->on[i].val);
955 }
956
957 ret = sdhci_msm_dt_get_array(dev, "qcom,pad-pull-off",
958 &tmp, &len, pull_data->size);
959 if (ret)
960 goto out;
961
962 for (i = 0; i < len; i++) {
963 pull_data->off[i].no = base + i;
964 pull_data->off[i].val = tmp[i];
965 dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
966 i, pull_data->off[i].val);
967 }
968
969 *pad_pull_data = pull_data;
970out:
971 return ret;
972}
973
974static int sdhci_msm_dt_get_pad_drv_info(struct device *dev, int id,
975 struct sdhci_msm_pad_drv_data **pad_drv_data)
976{
977 int ret = 0, base = 0, len, i;
978 u32 *tmp;
979 struct sdhci_msm_pad_drv_data *drv_data;
980 struct sdhci_msm_pad_drv *drv;
981
982 switch (id) {
983 case 1:
984 base = TLMM_HDRV_SDC1_CLK;
985 break;
986 case 2:
987 base = TLMM_HDRV_SDC2_CLK;
988 break;
989 case 3:
990 base = TLMM_HDRV_SDC3_CLK;
991 break;
992 case 4:
993 base = TLMM_HDRV_SDC4_CLK;
994 break;
995 default:
996 dev_err(dev, "%s: Invalid slot id\n", __func__);
997 ret = -EINVAL;
998 goto out;
999 }
1000
1001 drv_data = devm_kzalloc(dev, sizeof(struct sdhci_msm_pad_drv_data),
1002 GFP_KERNEL);
1003 if (!drv_data) {
1004 dev_err(dev, "No memory for msm_mmc_pad_drv_data\n");
1005 ret = -ENOMEM;
1006 goto out;
1007 }
1008 drv_data->size = 3; /* array size for clk, cmd, data */
1009
1010 /* Allocate on, off configs for clk, cmd, data */
1011 drv = devm_kzalloc(dev, 2 * drv_data->size *\
1012 sizeof(struct sdhci_msm_pad_drv), GFP_KERNEL);
1013 if (!drv) {
1014 dev_err(dev, "No memory msm_mmc_pad_drv\n");
1015 ret = -ENOMEM;
1016 goto out;
1017 }
1018 drv_data->on = drv;
1019 drv_data->off = drv + drv_data->size;
1020
1021 ret = sdhci_msm_dt_get_array(dev, "qcom,pad-drv-on",
1022 &tmp, &len, drv_data->size);
1023 if (ret)
1024 goto out;
1025
1026 for (i = 0; i < len; i++) {
1027 drv_data->on[i].no = base + i;
1028 drv_data->on[i].val = tmp[i];
1029 dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
1030 i, drv_data->on[i].val);
1031 }
1032
1033 ret = sdhci_msm_dt_get_array(dev, "qcom,pad-drv-off",
1034 &tmp, &len, drv_data->size);
1035 if (ret)
1036 goto out;
1037
1038 for (i = 0; i < len; i++) {
1039 drv_data->off[i].no = base + i;
1040 drv_data->off[i].val = tmp[i];
1041 dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
1042 i, drv_data->off[i].val);
1043 }
1044
1045 *pad_drv_data = drv_data;
1046out:
1047 return ret;
1048}
1049
Asutosh Das33a4ff52012-12-18 16:14:02 +05301050#define GPIO_NAME_MAX_LEN 32
1051static int sdhci_msm_dt_parse_gpio_info(struct device *dev,
1052 struct sdhci_msm_pltfm_data *pdata)
1053{
Asutosh Das390519d2012-12-21 12:21:42 +05301054 int ret = 0, id = 0, cnt, i;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301055 struct sdhci_msm_pin_data *pin_data;
1056 struct device_node *np = dev->of_node;
1057
1058 pin_data = devm_kzalloc(dev, sizeof(*pin_data), GFP_KERNEL);
1059 if (!pin_data) {
1060 dev_err(dev, "No memory for pin_data\n");
1061 ret = -ENOMEM;
1062 goto out;
1063 }
1064
1065 cnt = of_gpio_count(np);
1066 if (cnt > 0) {
Asutosh Das390519d2012-12-21 12:21:42 +05301067 pin_data->is_gpio = true;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301068 pin_data->gpio_data = devm_kzalloc(dev,
1069 sizeof(struct sdhci_msm_gpio_data), GFP_KERNEL);
1070 if (!pin_data->gpio_data) {
1071 dev_err(dev, "No memory for gpio_data\n");
1072 ret = -ENOMEM;
1073 goto out;
1074 }
1075 pin_data->gpio_data->size = cnt;
1076 pin_data->gpio_data->gpio = devm_kzalloc(dev, cnt *
1077 sizeof(struct sdhci_msm_gpio), GFP_KERNEL);
1078
1079 if (!pin_data->gpio_data->gpio) {
1080 dev_err(dev, "No memory for gpio\n");
1081 ret = -ENOMEM;
1082 goto out;
1083 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301084 for (i = 0; i < cnt; i++) {
1085 const char *name = NULL;
1086 char result[GPIO_NAME_MAX_LEN];
1087 pin_data->gpio_data->gpio[i].no = of_get_gpio(np, i);
1088 of_property_read_string_index(np,
1089 "qcom,gpio-names", i, &name);
1090
1091 snprintf(result, GPIO_NAME_MAX_LEN, "%s-%s",
1092 dev_name(dev), name ? name : "?");
1093 pin_data->gpio_data->gpio[i].name = result;
1094 dev_dbg(dev, "%s: gpio[%s] = %d\n", __func__,
Asutosh Das390519d2012-12-21 12:21:42 +05301095 pin_data->gpio_data->gpio[i].name,
1096 pin_data->gpio_data->gpio[i].no);
Asutosh Das33a4ff52012-12-18 16:14:02 +05301097 }
Asutosh Das390519d2012-12-21 12:21:42 +05301098 } else {
1099 pin_data->pad_data =
1100 devm_kzalloc(dev,
1101 sizeof(struct sdhci_msm_pad_data),
1102 GFP_KERNEL);
1103 if (!pin_data->pad_data) {
1104 dev_err(dev,
1105 "No memory for pin_data->pad_data\n");
1106 ret = -ENOMEM;
1107 goto out;
1108 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301109
Asutosh Das390519d2012-12-21 12:21:42 +05301110 ret = of_alias_get_id(np, "sdhc");
1111 if (ret < 0) {
1112 dev_err(dev, "Failed to get slot index %d\n", ret);
1113 goto out;
1114 }
1115 id = ret;
1116
1117 ret = sdhci_msm_dt_get_pad_pull_info(
1118 dev, id, &pin_data->pad_data->pull);
1119 if (ret)
1120 goto out;
1121 ret = sdhci_msm_dt_get_pad_drv_info(
1122 dev, id, &pin_data->pad_data->drv);
1123 if (ret)
1124 goto out;
1125
1126 }
1127 pdata->pin_data = pin_data;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301128out:
1129 if (ret)
1130 dev_err(dev, "%s failed with err %d\n", __func__, ret);
1131 return ret;
1132}
1133
1134/* Parse platform data */
1135static struct sdhci_msm_pltfm_data *sdhci_msm_populate_pdata(struct device *dev)
1136{
1137 struct sdhci_msm_pltfm_data *pdata = NULL;
1138 struct device_node *np = dev->of_node;
1139 u32 bus_width = 0;
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301140 u32 cpu_dma_latency;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301141 int len, i;
Sahitya Tummala00240122013-02-28 19:50:51 +05301142 int clk_table_len;
1143 u32 *clk_table = NULL;
Sujit Reddy Thumma4ddff322013-06-03 09:54:32 +05301144 enum of_gpio_flags flags = OF_GPIO_ACTIVE_LOW;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301145
1146 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1147 if (!pdata) {
1148 dev_err(dev, "failed to allocate memory for platform data\n");
1149 goto out;
1150 }
1151
Sujit Reddy Thumma4ddff322013-06-03 09:54:32 +05301152 pdata->status_gpio = of_get_named_gpio_flags(np, "cd-gpios", 0, &flags);
1153 if (gpio_is_valid(pdata->status_gpio) & !(flags & OF_GPIO_ACTIVE_LOW))
1154 pdata->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
Sahitya Tummala62448d92013-03-12 14:57:46 +05301155
Asutosh Das33a4ff52012-12-18 16:14:02 +05301156 of_property_read_u32(np, "qcom,bus-width", &bus_width);
1157 if (bus_width == 8)
1158 pdata->mmc_bus_width = MMC_CAP_8_BIT_DATA;
1159 else if (bus_width == 4)
1160 pdata->mmc_bus_width = MMC_CAP_4_BIT_DATA;
1161 else {
1162 dev_notice(dev, "invalid bus-width, default to 1-bit mode\n");
1163 pdata->mmc_bus_width = 0;
1164 }
1165
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301166 if (!of_property_read_u32(np, "qcom,cpu-dma-latency-us",
1167 &cpu_dma_latency))
1168 pdata->cpu_dma_latency_us = cpu_dma_latency;
1169
Sahitya Tummala00240122013-02-28 19:50:51 +05301170 if (sdhci_msm_dt_get_array(dev, "qcom,clk-rates",
1171 &clk_table, &clk_table_len, 0)) {
1172 dev_err(dev, "failed parsing supported clock rates\n");
1173 goto out;
1174 }
1175 if (!clk_table || !clk_table_len) {
1176 dev_err(dev, "Invalid clock table\n");
1177 goto out;
1178 }
1179 pdata->sup_clk_table = clk_table;
1180 pdata->sup_clk_cnt = clk_table_len;
1181
Asutosh Das33a4ff52012-12-18 16:14:02 +05301182 pdata->vreg_data = devm_kzalloc(dev, sizeof(struct
1183 sdhci_msm_slot_reg_data),
1184 GFP_KERNEL);
1185 if (!pdata->vreg_data) {
1186 dev_err(dev, "failed to allocate memory for vreg data\n");
1187 goto out;
1188 }
1189
1190 if (sdhci_msm_dt_parse_vreg_info(dev, &pdata->vreg_data->vdd_data,
1191 "vdd")) {
1192 dev_err(dev, "failed parsing vdd data\n");
1193 goto out;
1194 }
1195 if (sdhci_msm_dt_parse_vreg_info(dev,
1196 &pdata->vreg_data->vdd_io_data,
1197 "vdd-io")) {
1198 dev_err(dev, "failed parsing vdd-io data\n");
1199 goto out;
1200 }
1201
1202 if (sdhci_msm_dt_parse_gpio_info(dev, pdata)) {
1203 dev_err(dev, "failed parsing gpio data\n");
1204 goto out;
1205 }
1206
Asutosh Das33a4ff52012-12-18 16:14:02 +05301207 len = of_property_count_strings(np, "qcom,bus-speed-mode");
1208
1209 for (i = 0; i < len; i++) {
1210 const char *name = NULL;
1211
1212 of_property_read_string_index(np,
1213 "qcom,bus-speed-mode", i, &name);
1214 if (!name)
1215 continue;
1216
1217 if (!strncmp(name, "HS200_1p8v", sizeof("HS200_1p8v")))
1218 pdata->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
1219 else if (!strncmp(name, "HS200_1p2v", sizeof("HS200_1p2v")))
1220 pdata->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
1221 else if (!strncmp(name, "DDR_1p8v", sizeof("DDR_1p8v")))
1222 pdata->caps |= MMC_CAP_1_8V_DDR
1223 | MMC_CAP_UHS_DDR50;
1224 else if (!strncmp(name, "DDR_1p2v", sizeof("DDR_1p2v")))
1225 pdata->caps |= MMC_CAP_1_2V_DDR
1226 | MMC_CAP_UHS_DDR50;
1227 }
1228
1229 if (of_get_property(np, "qcom,nonremovable", NULL))
1230 pdata->nonremovable = true;
1231
1232 return pdata;
1233out:
1234 return NULL;
1235}
1236
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301237/* Returns required bandwidth in Bytes per Sec */
1238static unsigned int sdhci_get_bw_required(struct sdhci_host *host,
1239 struct mmc_ios *ios)
1240{
Sahitya Tummala53aff982013-04-03 18:03:31 +05301241 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1242 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1243
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301244 unsigned int bw;
1245
Sahitya Tummala53aff982013-04-03 18:03:31 +05301246 bw = msm_host->clk_rate;
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301247 /*
1248 * For DDR mode, SDCC controller clock will be at
1249 * the double rate than the actual clock that goes to card.
1250 */
1251 if (ios->bus_width == MMC_BUS_WIDTH_4)
1252 bw /= 2;
1253 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1254 bw /= 8;
1255
1256 return bw;
1257}
1258
1259static int sdhci_msm_bus_get_vote_for_bw(struct sdhci_msm_host *host,
1260 unsigned int bw)
1261{
1262 unsigned int *table = host->pdata->voting_data->bw_vecs;
1263 unsigned int size = host->pdata->voting_data->bw_vecs_size;
1264 int i;
1265
1266 if (host->msm_bus_vote.is_max_bw_needed && bw)
1267 return host->msm_bus_vote.max_bw_vote;
1268
1269 for (i = 0; i < size; i++) {
1270 if (bw <= table[i])
1271 break;
1272 }
1273
1274 if (i && (i == size))
1275 i--;
1276
1277 return i;
1278}
1279
1280/*
1281 * This function must be called with host lock acquired.
1282 * Caller of this function should also ensure that msm bus client
1283 * handle is not null.
1284 */
1285static inline int sdhci_msm_bus_set_vote(struct sdhci_msm_host *msm_host,
1286 int vote,
1287 unsigned long flags)
1288{
1289 struct sdhci_host *host = platform_get_drvdata(msm_host->pdev);
1290 int rc = 0;
1291
1292 if (vote != msm_host->msm_bus_vote.curr_vote) {
1293 spin_unlock_irqrestore(&host->lock, flags);
1294 rc = msm_bus_scale_client_update_request(
1295 msm_host->msm_bus_vote.client_handle, vote);
1296 spin_lock_irqsave(&host->lock, flags);
1297 if (rc) {
1298 pr_err("%s: msm_bus_scale_client_update_request() failed: bus_client_handle=0x%x, vote=%d, err=%d\n",
1299 mmc_hostname(host->mmc),
1300 msm_host->msm_bus_vote.client_handle, vote, rc);
1301 goto out;
1302 }
1303 msm_host->msm_bus_vote.curr_vote = vote;
1304 }
1305out:
1306 return rc;
1307}
1308
1309/*
1310 * Internal work. Work to set 0 bandwidth for msm bus.
1311 */
1312static void sdhci_msm_bus_work(struct work_struct *work)
1313{
1314 struct sdhci_msm_host *msm_host;
1315 struct sdhci_host *host;
1316 unsigned long flags;
1317
1318 msm_host = container_of(work, struct sdhci_msm_host,
1319 msm_bus_vote.vote_work.work);
1320 host = platform_get_drvdata(msm_host->pdev);
1321
1322 if (!msm_host->msm_bus_vote.client_handle)
1323 return;
1324
1325 spin_lock_irqsave(&host->lock, flags);
1326 /* don't vote for 0 bandwidth if any request is in progress */
1327 if (!host->mrq) {
1328 sdhci_msm_bus_set_vote(msm_host,
1329 msm_host->msm_bus_vote.min_bw_vote, flags);
1330 } else
1331 pr_warning("%s: %s: Transfer in progress. skipping bus voting to 0 bandwidth\n",
1332 mmc_hostname(host->mmc), __func__);
1333 spin_unlock_irqrestore(&host->lock, flags);
1334}
1335
1336/*
1337 * This function cancels any scheduled delayed work and sets the bus
1338 * vote based on bw (bandwidth) argument.
1339 */
1340static void sdhci_msm_bus_cancel_work_and_set_vote(struct sdhci_host *host,
1341 unsigned int bw)
1342{
1343 int vote;
1344 unsigned long flags;
1345 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1346 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1347
1348 cancel_delayed_work_sync(&msm_host->msm_bus_vote.vote_work);
1349 spin_lock_irqsave(&host->lock, flags);
1350 vote = sdhci_msm_bus_get_vote_for_bw(msm_host, bw);
1351 sdhci_msm_bus_set_vote(msm_host, vote, flags);
1352 spin_unlock_irqrestore(&host->lock, flags);
1353}
1354
1355#define MSM_MMC_BUS_VOTING_DELAY 200 /* msecs */
1356
1357/* This function queues a work which will set the bandwidth requiement to 0 */
1358static void sdhci_msm_bus_queue_work(struct sdhci_host *host)
1359{
1360 unsigned long flags;
1361 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1362 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1363
1364 spin_lock_irqsave(&host->lock, flags);
1365 if (msm_host->msm_bus_vote.min_bw_vote !=
1366 msm_host->msm_bus_vote.curr_vote)
1367 queue_delayed_work(system_nrt_wq,
1368 &msm_host->msm_bus_vote.vote_work,
1369 msecs_to_jiffies(MSM_MMC_BUS_VOTING_DELAY));
1370 spin_unlock_irqrestore(&host->lock, flags);
1371}
1372
1373static int sdhci_msm_bus_register(struct sdhci_msm_host *host,
1374 struct platform_device *pdev)
1375{
1376 int rc = 0;
1377 struct msm_bus_scale_pdata *bus_pdata;
1378
1379 struct sdhci_msm_bus_voting_data *data;
1380 struct device *dev = &pdev->dev;
1381
1382 data = devm_kzalloc(dev,
1383 sizeof(struct sdhci_msm_bus_voting_data), GFP_KERNEL);
1384 if (!data) {
1385 dev_err(&pdev->dev,
1386 "%s: failed to allocate memory\n", __func__);
1387 rc = -ENOMEM;
1388 goto out;
1389 }
1390 data->bus_pdata = msm_bus_cl_get_pdata(pdev);
1391 if (data->bus_pdata) {
1392 rc = sdhci_msm_dt_get_array(dev, "qcom,bus-bw-vectors-bps",
1393 &data->bw_vecs, &data->bw_vecs_size, 0);
1394 if (rc) {
1395 dev_err(&pdev->dev,
1396 "%s: Failed to get bus-bw-vectors-bps\n",
1397 __func__);
1398 goto out;
1399 }
1400 host->pdata->voting_data = data;
1401 }
1402 if (host->pdata->voting_data &&
1403 host->pdata->voting_data->bus_pdata &&
1404 host->pdata->voting_data->bw_vecs &&
1405 host->pdata->voting_data->bw_vecs_size) {
1406
1407 bus_pdata = host->pdata->voting_data->bus_pdata;
1408 host->msm_bus_vote.client_handle =
1409 msm_bus_scale_register_client(bus_pdata);
1410 if (!host->msm_bus_vote.client_handle) {
1411 dev_err(&pdev->dev, "msm_bus_scale_register_client()\n");
1412 rc = -EFAULT;
1413 goto out;
1414 }
1415 /* cache the vote index for minimum and maximum bandwidth */
1416 host->msm_bus_vote.min_bw_vote =
1417 sdhci_msm_bus_get_vote_for_bw(host, 0);
1418 host->msm_bus_vote.max_bw_vote =
1419 sdhci_msm_bus_get_vote_for_bw(host, UINT_MAX);
1420 } else {
1421 devm_kfree(dev, data);
1422 }
1423
1424out:
1425 return rc;
1426}
1427
1428static void sdhci_msm_bus_unregister(struct sdhci_msm_host *host)
1429{
1430 if (host->msm_bus_vote.client_handle)
1431 msm_bus_scale_unregister_client(
1432 host->msm_bus_vote.client_handle);
1433}
1434
1435static void sdhci_msm_bus_voting(struct sdhci_host *host, u32 enable)
1436{
1437 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1438 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1439 struct mmc_ios *ios = &host->mmc->ios;
1440 unsigned int bw;
1441
1442 if (!msm_host->msm_bus_vote.client_handle)
1443 return;
1444
1445 bw = sdhci_get_bw_required(host, ios);
Sahitya Tummalac45ae732013-05-23 15:59:22 +05301446 if (enable) {
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301447 sdhci_msm_bus_cancel_work_and_set_vote(host, bw);
Sahitya Tummalac45ae732013-05-23 15:59:22 +05301448 } else {
1449 /*
1450 * If clock gating is enabled, then remove the vote
1451 * immediately because clocks will be disabled only
1452 * after SDHCI_MSM_MMC_CLK_GATE_DELAY and thus no
1453 * additional delay is required to remove the bus vote.
1454 */
1455 if (host->mmc->clkgate_delay)
1456 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
1457 else
1458 sdhci_msm_bus_queue_work(host);
1459 }
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301460}
1461
Asutosh Das33a4ff52012-12-18 16:14:02 +05301462/* Regulator utility functions */
1463static int sdhci_msm_vreg_init_reg(struct device *dev,
1464 struct sdhci_msm_reg_data *vreg)
1465{
1466 int ret = 0;
1467
1468 /* check if regulator is already initialized? */
1469 if (vreg->reg)
1470 goto out;
1471
1472 /* Get the regulator handle */
1473 vreg->reg = devm_regulator_get(dev, vreg->name);
1474 if (IS_ERR(vreg->reg)) {
1475 ret = PTR_ERR(vreg->reg);
1476 pr_err("%s: devm_regulator_get(%s) failed. ret=%d\n",
1477 __func__, vreg->name, ret);
1478 goto out;
1479 }
1480
Asutosh Das95afcad2013-06-28 15:03:44 +05301481 if (regulator_count_voltages(vreg->reg) > 0) {
1482 vreg->set_voltage_sup = true;
1483 /* sanity check */
1484 if (!vreg->high_vol_level || !vreg->hpm_uA) {
1485 pr_err("%s: %s invalid constraints specified\n",
1486 __func__, vreg->name);
1487 ret = -EINVAL;
1488 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301489 }
1490
1491out:
1492 return ret;
1493}
1494
1495static void sdhci_msm_vreg_deinit_reg(struct sdhci_msm_reg_data *vreg)
1496{
1497 if (vreg->reg)
1498 devm_regulator_put(vreg->reg);
1499}
1500
1501static int sdhci_msm_vreg_set_optimum_mode(struct sdhci_msm_reg_data
1502 *vreg, int uA_load)
1503{
1504 int ret = 0;
1505
1506 /*
1507 * regulators that do not support regulator_set_voltage also
1508 * do not support regulator_set_optimum_mode
1509 */
Asutosh Das95afcad2013-06-28 15:03:44 +05301510 if (vreg->set_voltage_sup) {
1511 ret = regulator_set_optimum_mode(vreg->reg, uA_load);
1512 if (ret < 0)
1513 pr_err("%s: regulator_set_optimum_mode(reg=%s,uA_load=%d) failed. ret=%d\n",
Asutosh Das33a4ff52012-12-18 16:14:02 +05301514 __func__, vreg->name, uA_load, ret);
1515 else
1516 /*
1517 * regulator_set_optimum_mode() can return non zero
1518 * value even for success case.
1519 */
1520 ret = 0;
Asutosh Das95afcad2013-06-28 15:03:44 +05301521 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301522 return ret;
1523}
1524
1525static int sdhci_msm_vreg_set_voltage(struct sdhci_msm_reg_data *vreg,
1526 int min_uV, int max_uV)
1527{
1528 int ret = 0;
Asutosh Das95afcad2013-06-28 15:03:44 +05301529 if (vreg->set_voltage_sup) {
1530 ret = regulator_set_voltage(vreg->reg, min_uV, max_uV);
1531 if (ret) {
1532 pr_err("%s: regulator_set_voltage(%s)failed. min_uV=%d,max_uV=%d,ret=%d\n",
Asutosh Das33a4ff52012-12-18 16:14:02 +05301533 __func__, vreg->name, min_uV, max_uV, ret);
1534 }
Asutosh Das95afcad2013-06-28 15:03:44 +05301535 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301536
1537 return ret;
1538}
1539
1540static int sdhci_msm_vreg_enable(struct sdhci_msm_reg_data *vreg)
1541{
1542 int ret = 0;
1543
1544 /* Put regulator in HPM (high power mode) */
1545 ret = sdhci_msm_vreg_set_optimum_mode(vreg, vreg->hpm_uA);
1546 if (ret < 0)
1547 return ret;
1548
1549 if (!vreg->is_enabled) {
1550 /* Set voltage level */
1551 ret = sdhci_msm_vreg_set_voltage(vreg, vreg->high_vol_level,
1552 vreg->high_vol_level);
1553 if (ret)
1554 return ret;
1555 }
1556 ret = regulator_enable(vreg->reg);
1557 if (ret) {
1558 pr_err("%s: regulator_enable(%s) failed. ret=%d\n",
1559 __func__, vreg->name, ret);
1560 return ret;
1561 }
1562 vreg->is_enabled = true;
1563 return ret;
1564}
1565
1566static int sdhci_msm_vreg_disable(struct sdhci_msm_reg_data *vreg)
1567{
1568 int ret = 0;
1569
1570 /* Never disable regulator marked as always_on */
1571 if (vreg->is_enabled && !vreg->is_always_on) {
1572 ret = regulator_disable(vreg->reg);
1573 if (ret) {
1574 pr_err("%s: regulator_disable(%s) failed. ret=%d\n",
1575 __func__, vreg->name, ret);
1576 goto out;
1577 }
1578 vreg->is_enabled = false;
1579
1580 ret = sdhci_msm_vreg_set_optimum_mode(vreg, 0);
1581 if (ret < 0)
1582 goto out;
1583
1584 /* Set min. voltage level to 0 */
1585 ret = sdhci_msm_vreg_set_voltage(vreg, 0, vreg->high_vol_level);
1586 if (ret)
1587 goto out;
1588 } else if (vreg->is_enabled && vreg->is_always_on) {
1589 if (vreg->lpm_sup) {
1590 /* Put always_on regulator in LPM (low power mode) */
1591 ret = sdhci_msm_vreg_set_optimum_mode(vreg,
1592 vreg->lpm_uA);
1593 if (ret < 0)
1594 goto out;
1595 }
1596 }
1597out:
1598 return ret;
1599}
1600
1601static int sdhci_msm_setup_vreg(struct sdhci_msm_pltfm_data *pdata,
1602 bool enable, bool is_init)
1603{
1604 int ret = 0, i;
1605 struct sdhci_msm_slot_reg_data *curr_slot;
1606 struct sdhci_msm_reg_data *vreg_table[2];
1607
1608 curr_slot = pdata->vreg_data;
1609 if (!curr_slot) {
1610 pr_debug("%s: vreg info unavailable,assuming the slot is powered by always on domain\n",
1611 __func__);
1612 goto out;
1613 }
1614
1615 vreg_table[0] = curr_slot->vdd_data;
1616 vreg_table[1] = curr_slot->vdd_io_data;
1617
1618 for (i = 0; i < ARRAY_SIZE(vreg_table); i++) {
1619 if (vreg_table[i]) {
1620 if (enable)
1621 ret = sdhci_msm_vreg_enable(vreg_table[i]);
1622 else
1623 ret = sdhci_msm_vreg_disable(vreg_table[i]);
1624 if (ret)
1625 goto out;
1626 }
1627 }
1628out:
1629 return ret;
1630}
1631
1632/*
1633 * Reset vreg by ensuring it is off during probe. A call
1634 * to enable vreg is needed to balance disable vreg
1635 */
1636static int sdhci_msm_vreg_reset(struct sdhci_msm_pltfm_data *pdata)
1637{
1638 int ret;
1639
1640 ret = sdhci_msm_setup_vreg(pdata, 1, true);
1641 if (ret)
1642 return ret;
1643 ret = sdhci_msm_setup_vreg(pdata, 0, true);
1644 return ret;
1645}
1646
1647/* This init function should be called only once for each SDHC slot */
1648static int sdhci_msm_vreg_init(struct device *dev,
1649 struct sdhci_msm_pltfm_data *pdata,
1650 bool is_init)
1651{
1652 int ret = 0;
1653 struct sdhci_msm_slot_reg_data *curr_slot;
1654 struct sdhci_msm_reg_data *curr_vdd_reg, *curr_vdd_io_reg;
1655
1656 curr_slot = pdata->vreg_data;
1657 if (!curr_slot)
1658 goto out;
1659
1660 curr_vdd_reg = curr_slot->vdd_data;
1661 curr_vdd_io_reg = curr_slot->vdd_io_data;
1662
1663 if (!is_init)
1664 /* Deregister all regulators from regulator framework */
1665 goto vdd_io_reg_deinit;
1666
1667 /*
1668 * Get the regulator handle from voltage regulator framework
1669 * and then try to set the voltage level for the regulator
1670 */
1671 if (curr_vdd_reg) {
1672 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_reg);
1673 if (ret)
1674 goto out;
1675 }
1676 if (curr_vdd_io_reg) {
1677 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_io_reg);
1678 if (ret)
1679 goto vdd_reg_deinit;
1680 }
1681 ret = sdhci_msm_vreg_reset(pdata);
1682 if (ret)
1683 dev_err(dev, "vreg reset failed (%d)\n", ret);
1684 goto out;
1685
1686vdd_io_reg_deinit:
1687 if (curr_vdd_io_reg)
1688 sdhci_msm_vreg_deinit_reg(curr_vdd_io_reg);
1689vdd_reg_deinit:
1690 if (curr_vdd_reg)
1691 sdhci_msm_vreg_deinit_reg(curr_vdd_reg);
1692out:
1693 return ret;
1694}
1695
1696
1697static int sdhci_msm_set_vdd_io_vol(struct sdhci_msm_pltfm_data *pdata,
1698 enum vdd_io_level level,
1699 unsigned int voltage_level)
1700{
1701 int ret = 0;
1702 int set_level;
1703 struct sdhci_msm_reg_data *vdd_io_reg;
1704
1705 if (!pdata->vreg_data)
1706 return ret;
1707
1708 vdd_io_reg = pdata->vreg_data->vdd_io_data;
1709 if (vdd_io_reg && vdd_io_reg->is_enabled) {
1710 switch (level) {
1711 case VDD_IO_LOW:
1712 set_level = vdd_io_reg->low_vol_level;
1713 break;
1714 case VDD_IO_HIGH:
1715 set_level = vdd_io_reg->high_vol_level;
1716 break;
1717 case VDD_IO_SET_LEVEL:
1718 set_level = voltage_level;
1719 break;
1720 default:
1721 pr_err("%s: invalid argument level = %d",
1722 __func__, level);
1723 ret = -EINVAL;
1724 return ret;
1725 }
1726 ret = sdhci_msm_vreg_set_voltage(vdd_io_reg, set_level,
1727 set_level);
1728 }
1729 return ret;
1730}
1731
1732static irqreturn_t sdhci_msm_pwr_irq(int irq, void *data)
1733{
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07001734 struct sdhci_host *host = (struct sdhci_host *)data;
1735 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1736 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301737 u8 irq_status = 0;
1738 u8 irq_ack = 0;
1739 int ret = 0;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301740 int pwr_state = 0, io_level = 0;
1741 unsigned long flags;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301742
1743 irq_status = readb_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
1744 pr_debug("%s: Received IRQ(%d), status=0x%x\n",
1745 mmc_hostname(msm_host->mmc), irq, irq_status);
1746
1747 /* Clear the interrupt */
1748 writeb_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
1749 /*
1750 * SDHC has core_mem and hc_mem device memory and these memory
1751 * addresses do not fall within 1KB region. Hence, any update to
1752 * core_mem address space would require an mb() to ensure this gets
1753 * completed before its next update to registers within hc_mem.
1754 */
1755 mb();
1756
1757 /* Handle BUS ON/OFF*/
1758 if (irq_status & CORE_PWRCTL_BUS_ON) {
1759 ret = sdhci_msm_setup_vreg(msm_host->pdata, true, false);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301760 if (!ret) {
Asutosh Das33a4ff52012-12-18 16:14:02 +05301761 ret = sdhci_msm_setup_pins(msm_host->pdata, true);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301762 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
1763 VDD_IO_HIGH, 0);
1764 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301765 if (ret)
1766 irq_ack |= CORE_PWRCTL_BUS_FAIL;
1767 else
1768 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301769
1770 pwr_state = REQ_BUS_ON;
1771 io_level = REQ_IO_HIGH;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301772 }
1773 if (irq_status & CORE_PWRCTL_BUS_OFF) {
1774 ret = sdhci_msm_setup_vreg(msm_host->pdata, false, false);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301775 if (!ret) {
Asutosh Das33a4ff52012-12-18 16:14:02 +05301776 ret = sdhci_msm_setup_pins(msm_host->pdata, false);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301777 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
1778 VDD_IO_LOW, 0);
1779 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301780 if (ret)
1781 irq_ack |= CORE_PWRCTL_BUS_FAIL;
1782 else
1783 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301784
1785 pwr_state = REQ_BUS_OFF;
1786 io_level = REQ_IO_LOW;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301787 }
1788 /* Handle IO LOW/HIGH */
1789 if (irq_status & CORE_PWRCTL_IO_LOW) {
1790 /* Switch voltage Low */
1791 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_LOW, 0);
1792 if (ret)
1793 irq_ack |= CORE_PWRCTL_IO_FAIL;
1794 else
1795 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301796
1797 io_level = REQ_IO_LOW;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301798 }
1799 if (irq_status & CORE_PWRCTL_IO_HIGH) {
1800 /* Switch voltage High */
1801 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_HIGH, 0);
1802 if (ret)
1803 irq_ack |= CORE_PWRCTL_IO_FAIL;
1804 else
1805 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301806
1807 io_level = REQ_IO_HIGH;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301808 }
1809
1810 /* ACK status to the core */
1811 writeb_relaxed(irq_ack, (msm_host->core_mem + CORE_PWRCTL_CTL));
1812 /*
1813 * SDHC has core_mem and hc_mem device memory and these memory
1814 * addresses do not fall within 1KB region. Hence, any update to
1815 * core_mem address space would require an mb() to ensure this gets
1816 * completed before its next update to registers within hc_mem.
1817 */
1818 mb();
1819
Sahitya Tummala179e7382013-03-20 19:24:01 +05301820 if (io_level & REQ_IO_HIGH)
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07001821 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
1822 ~CORE_IO_PAD_PWR_SWITCH),
1823 host->ioaddr + CORE_VENDOR_SPEC);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301824 else if (io_level & REQ_IO_LOW)
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07001825 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
1826 CORE_IO_PAD_PWR_SWITCH),
1827 host->ioaddr + CORE_VENDOR_SPEC);
1828 mb();
1829
Asutosh Das33a4ff52012-12-18 16:14:02 +05301830 pr_debug("%s: Handled IRQ(%d), ret=%d, ack=0x%x\n",
1831 mmc_hostname(msm_host->mmc), irq, ret, irq_ack);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301832 spin_lock_irqsave(&host->lock, flags);
1833 if (pwr_state)
1834 msm_host->curr_pwr_state = pwr_state;
1835 if (io_level)
1836 msm_host->curr_io_level = io_level;
1837 complete(&msm_host->pwr_irq_completion);
1838 spin_unlock_irqrestore(&host->lock, flags);
1839
Asutosh Das33a4ff52012-12-18 16:14:02 +05301840 return IRQ_HANDLED;
1841}
1842
1843/* This function returns the max. current supported by VDD rail in mA */
1844static unsigned int sdhci_msm_get_vreg_vdd_max_current(struct sdhci_msm_host
1845 *host)
1846{
1847 struct sdhci_msm_slot_reg_data *curr_slot = host->pdata->vreg_data;
1848 if (!curr_slot)
1849 return 0;
1850 if (curr_slot->vdd_data)
1851 return curr_slot->vdd_data->hpm_uA / 1000;
1852 else
1853 return 0;
1854}
Sahitya Tummala3b292c32013-06-20 14:00:18 +05301855
1856static ssize_t
1857show_polling(struct device *dev, struct device_attribute *attr, char *buf)
1858{
1859 struct sdhci_host *host = dev_get_drvdata(dev);
1860 int poll;
1861 unsigned long flags;
1862
1863 spin_lock_irqsave(&host->lock, flags);
1864 poll = !!(host->mmc->caps & MMC_CAP_NEEDS_POLL);
1865 spin_unlock_irqrestore(&host->lock, flags);
1866
1867 return snprintf(buf, PAGE_SIZE, "%d\n", poll);
1868}
1869
1870static ssize_t
1871store_polling(struct device *dev, struct device_attribute *attr,
1872 const char *buf, size_t count)
1873{
1874 struct sdhci_host *host = dev_get_drvdata(dev);
1875 int value;
1876 unsigned long flags;
1877
1878 if (!kstrtou32(buf, 0, &value)) {
1879 spin_lock_irqsave(&host->lock, flags);
1880 if (value) {
1881 host->mmc->caps |= MMC_CAP_NEEDS_POLL;
1882 mmc_detect_change(host->mmc, 0);
1883 } else {
1884 host->mmc->caps &= ~MMC_CAP_NEEDS_POLL;
1885 }
1886 spin_unlock_irqrestore(&host->lock, flags);
1887 }
1888 return count;
1889}
1890
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301891static ssize_t
1892show_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
1893 char *buf)
1894{
1895 struct sdhci_host *host = dev_get_drvdata(dev);
1896 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1897 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1898
1899 return snprintf(buf, PAGE_SIZE, "%u\n",
1900 msm_host->msm_bus_vote.is_max_bw_needed);
1901}
1902
1903static ssize_t
1904store_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
1905 const char *buf, size_t count)
1906{
1907 struct sdhci_host *host = dev_get_drvdata(dev);
1908 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1909 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1910 uint32_t value;
1911 unsigned long flags;
1912
1913 if (!kstrtou32(buf, 0, &value)) {
1914 spin_lock_irqsave(&host->lock, flags);
1915 msm_host->msm_bus_vote.is_max_bw_needed = !!value;
1916 spin_unlock_irqrestore(&host->lock, flags);
1917 }
1918 return count;
1919}
Asutosh Das33a4ff52012-12-18 16:14:02 +05301920
Sahitya Tummala179e7382013-03-20 19:24:01 +05301921static void sdhci_msm_check_power_status(struct sdhci_host *host, u32 req_type)
Asutosh Das33a4ff52012-12-18 16:14:02 +05301922{
1923 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1924 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301925 unsigned long flags;
1926 bool done = false;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301927
Sahitya Tummala179e7382013-03-20 19:24:01 +05301928 spin_lock_irqsave(&host->lock, flags);
1929 pr_debug("%s: %s: request %d curr_pwr_state %x curr_io_level %x\n",
1930 mmc_hostname(host->mmc), __func__, req_type,
1931 msm_host->curr_pwr_state, msm_host->curr_io_level);
1932 if ((req_type & msm_host->curr_pwr_state) ||
1933 (req_type & msm_host->curr_io_level))
1934 done = true;
1935 spin_unlock_irqrestore(&host->lock, flags);
Asutosh Das33a4ff52012-12-18 16:14:02 +05301936
Sahitya Tummala179e7382013-03-20 19:24:01 +05301937 /*
1938 * This is needed here to hanlde a case where IRQ gets
1939 * triggered even before this function is called so that
1940 * x->done counter of completion gets reset. Otherwise,
1941 * next call to wait_for_completion returns immediately
1942 * without actually waiting for the IRQ to be handled.
1943 */
1944 if (done)
1945 init_completion(&msm_host->pwr_irq_completion);
1946 else
1947 wait_for_completion(&msm_host->pwr_irq_completion);
1948
1949 pr_debug("%s: %s: request %d done\n", mmc_hostname(host->mmc),
1950 __func__, req_type);
Asutosh Das33a4ff52012-12-18 16:14:02 +05301951}
1952
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07001953static void sdhci_msm_toggle_cdr(struct sdhci_host *host, bool enable)
1954{
1955 if (enable)
1956 writel_relaxed((readl_relaxed(host->ioaddr +
1957 CORE_DLL_CONFIG) | CORE_CDR_EN),
1958 host->ioaddr + CORE_DLL_CONFIG);
1959 else
1960 writel_relaxed((readl_relaxed(host->ioaddr +
1961 CORE_DLL_CONFIG) & ~CORE_CDR_EN),
1962 host->ioaddr + CORE_DLL_CONFIG);
1963}
1964
Asutosh Das3781bd82013-01-10 21:11:04 +05301965static unsigned int sdhci_msm_max_segs(void)
1966{
1967 return SDHCI_MSM_MAX_SEGMENTS;
1968}
1969
Sahitya Tummala00240122013-02-28 19:50:51 +05301970static unsigned int sdhci_msm_get_min_clock(struct sdhci_host *host)
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301971{
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301972 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1973 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301974
Sahitya Tummala00240122013-02-28 19:50:51 +05301975 return msm_host->pdata->sup_clk_table[0];
1976}
1977
1978static unsigned int sdhci_msm_get_max_clock(struct sdhci_host *host)
1979{
1980 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1981 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1982 int max_clk_index = msm_host->pdata->sup_clk_cnt;
1983
1984 return msm_host->pdata->sup_clk_table[max_clk_index - 1];
1985}
1986
1987static unsigned int sdhci_msm_get_sup_clk_rate(struct sdhci_host *host,
1988 u32 req_clk)
1989{
1990 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1991 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1992 unsigned int sel_clk = -1;
1993 unsigned char cnt;
1994
1995 if (req_clk < sdhci_msm_get_min_clock(host)) {
1996 sel_clk = sdhci_msm_get_min_clock(host);
1997 return sel_clk;
1998 }
1999
2000 for (cnt = 0; cnt < msm_host->pdata->sup_clk_cnt; cnt++) {
2001 if (msm_host->pdata->sup_clk_table[cnt] > req_clk) {
2002 break;
2003 } else if (msm_host->pdata->sup_clk_table[cnt] == req_clk) {
2004 sel_clk = msm_host->pdata->sup_clk_table[cnt];
2005 break;
2006 } else {
2007 sel_clk = msm_host->pdata->sup_clk_table[cnt];
2008 }
2009 }
2010 return sel_clk;
2011}
2012
2013static int sdhci_msm_prepare_clocks(struct sdhci_host *host, bool enable)
2014{
2015 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2016 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2017 int rc = 0;
2018
2019 if (enable && !atomic_read(&msm_host->clks_on)) {
2020 pr_debug("%s: request to enable clocks\n",
2021 mmc_hostname(host->mmc));
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302022
2023 sdhci_msm_bus_voting(host, 1);
2024
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302025 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
2026 rc = clk_prepare_enable(msm_host->bus_clk);
2027 if (rc) {
2028 pr_err("%s: %s: failed to enable the bus-clock with error %d\n",
2029 mmc_hostname(host->mmc), __func__, rc);
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302030 goto remove_vote;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302031 }
2032 }
2033 if (!IS_ERR(msm_host->pclk)) {
2034 rc = clk_prepare_enable(msm_host->pclk);
2035 if (rc) {
2036 pr_err("%s: %s: failed to enable the pclk with error %d\n",
2037 mmc_hostname(host->mmc), __func__, rc);
2038 goto disable_bus_clk;
2039 }
2040 }
2041 rc = clk_prepare_enable(msm_host->clk);
2042 if (rc) {
2043 pr_err("%s: %s: failed to enable the host-clk with error %d\n",
2044 mmc_hostname(host->mmc), __func__, rc);
2045 goto disable_pclk;
2046 }
2047 mb();
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302048
Sahitya Tummala00240122013-02-28 19:50:51 +05302049 } else if (!enable && atomic_read(&msm_host->clks_on)) {
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302050 pr_debug("%s: request to disable clocks\n",
2051 mmc_hostname(host->mmc));
2052 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
2053 mb();
2054 clk_disable_unprepare(msm_host->clk);
2055 if (!IS_ERR(msm_host->pclk))
2056 clk_disable_unprepare(msm_host->pclk);
2057 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2058 clk_disable_unprepare(msm_host->bus_clk);
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302059
2060 sdhci_msm_bus_voting(host, 0);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302061 }
Sahitya Tummala00240122013-02-28 19:50:51 +05302062 atomic_set(&msm_host->clks_on, enable);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302063 goto out;
2064disable_pclk:
2065 if (!IS_ERR_OR_NULL(msm_host->pclk))
2066 clk_disable_unprepare(msm_host->pclk);
2067disable_bus_clk:
2068 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2069 clk_disable_unprepare(msm_host->bus_clk);
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302070remove_vote:
2071 if (msm_host->msm_bus_vote.client_handle)
2072 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302073out:
Sahitya Tummala00240122013-02-28 19:50:51 +05302074 return rc;
2075}
2076
2077static void sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock)
2078{
2079 int rc;
2080 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2081 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2082 struct mmc_ios curr_ios = host->mmc->ios;
2083 u32 sup_clock, ddr_clock;
2084
2085 if (!clock) {
2086 sdhci_msm_prepare_clocks(host, false);
2087 host->clock = clock;
2088 return;
2089 }
2090
2091 rc = sdhci_msm_prepare_clocks(host, true);
2092 if (rc)
2093 return;
2094
2095 sup_clock = sdhci_msm_get_sup_clk_rate(host, clock);
2096 if (curr_ios.timing == MMC_TIMING_UHS_DDR50) {
2097 /*
2098 * The SDHC requires internal clock frequency to be double the
2099 * actual clock that will be set for DDR mode. The controller
2100 * uses the faster clock(100MHz) for some of its parts and send
2101 * the actual required clock (50MHz) to the card.
2102 */
2103 ddr_clock = clock * 2;
2104 sup_clock = sdhci_msm_get_sup_clk_rate(host,
2105 ddr_clock);
2106 }
2107 if (sup_clock != msm_host->clk_rate) {
2108 pr_debug("%s: %s: setting clk rate to %u\n",
2109 mmc_hostname(host->mmc), __func__, sup_clock);
2110 rc = clk_set_rate(msm_host->clk, sup_clock);
2111 if (rc) {
2112 pr_err("%s: %s: Failed to set rate %u for host-clk : %d\n",
2113 mmc_hostname(host->mmc), __func__,
2114 sup_clock, rc);
2115 return;
2116 }
2117 msm_host->clk_rate = sup_clock;
2118 host->clock = clock;
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302119 /*
2120 * Update the bus vote in case of frequency change due to
2121 * clock scaling.
2122 */
2123 sdhci_msm_bus_voting(host, 1);
Sahitya Tummala00240122013-02-28 19:50:51 +05302124 }
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302125}
2126
Sahitya Tummala5e4f9642013-03-21 11:13:25 +05302127static int sdhci_msm_set_uhs_signaling(struct sdhci_host *host,
2128 unsigned int uhs)
2129{
2130 u16 ctrl_2;
2131
2132 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2133 /* Select Bus Speed Mode for host */
2134 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
2135 if (uhs == MMC_TIMING_MMC_HS200)
2136 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2137 else if (uhs == MMC_TIMING_UHS_SDR12)
2138 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
2139 else if (uhs == MMC_TIMING_UHS_SDR25)
2140 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
2141 else if (uhs == MMC_TIMING_UHS_SDR50)
2142 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
2143 else if (uhs == MMC_TIMING_UHS_SDR104)
2144 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2145 else if (uhs == MMC_TIMING_UHS_DDR50)
2146 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Sahitya Tummala00240122013-02-28 19:50:51 +05302147 /*
2148 * When clock frquency is less than 100MHz, the feedback clock must be
2149 * provided and DLL must not be used so that tuning can be skipped. To
2150 * provide feedback clock, the mode selection can be any value less
2151 * than 3'b011 in bits [2:0] of HOST CONTROL2 register.
2152 */
2153 if (host->clock <= (100 * 1000 * 1000) &&
2154 (uhs == MMC_TIMING_MMC_HS200 ||
2155 uhs == MMC_TIMING_UHS_SDR104))
2156 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
2157
Sahitya Tummala5e4f9642013-03-21 11:13:25 +05302158 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
2159
2160 return 0;
2161}
2162
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002163/*
2164 * sdhci_msm_disable_data_xfer - disable undergoing AHB bus data transfer
2165 *
2166 * Write 0 to bit 0 in MCI_DATA_CTL (offset 0x2C) - clearing TxActive bit by
2167 * access to legacy registers. It will stop current burst and prevent start of
2168 * the next on.
2169 *
2170 * Polling CORE_AHB_DATA_DELAY_US timeout, by reading bit 13:12 until they are 0
2171 * in CORE_SDCC_DEBUG_REG (offset 0x124) will validate that AHB burst was
2172 * completed and a new one didn't start.
2173 *
2174 * Waiting for 4us while AHB finishes descriptors fetch.
2175 */
2176static void sdhci_msm_disable_data_xfer(struct sdhci_host *host)
2177{
2178 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2179 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2180 u32 value;
2181 int ret;
Venkat Gopalakrishnan0a179c82013-06-26 17:56:11 -07002182 u32 version;
2183
2184 version = readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION);
2185 /* Core version 3.1.0 doesn't need this workaround */
2186 if (version == CORE_VERSION_310)
2187 return;
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002188
2189 value = readl_relaxed(msm_host->core_mem + CORE_MCI_DATA_CTRL);
2190 value &= ~(u32)CORE_MCI_DPSM_ENABLE;
2191 writel_relaxed(value, msm_host->core_mem + CORE_MCI_DATA_CTRL);
2192
2193 /* Enable the test bus for device slot */
2194 writel_relaxed(CORE_TESTBUS_ENA | CORE_TESTBUS_SEL2,
2195 msm_host->core_mem + CORE_TESTBUS_CONFIG);
2196
2197 ret = readl_poll_timeout_noirq(msm_host->core_mem
2198 + CORE_SDCC_DEBUG_REG, value,
2199 !(value & CORE_DEBUG_REG_AHB_HTRANS),
2200 CORE_AHB_DATA_DELAY_US, 1);
2201 if (ret) {
2202 pr_err("%s: %s: can't stop ongoing AHB bus access by ADMA\n",
2203 mmc_hostname(host->mmc), __func__);
2204 BUG();
2205 }
2206 /* Disable the test bus for device slot */
2207 value = readl_relaxed(msm_host->core_mem + CORE_TESTBUS_CONFIG);
2208 value &= ~CORE_TESTBUS_ENA;
2209 writel_relaxed(value, msm_host->core_mem + CORE_TESTBUS_CONFIG);
2210
2211 udelay(CORE_AHB_DESC_DELAY_US);
2212}
2213
Asutosh Das33a4ff52012-12-18 16:14:02 +05302214static struct sdhci_ops sdhci_msm_ops = {
Sahitya Tummala5e4f9642013-03-21 11:13:25 +05302215 .set_uhs_signaling = sdhci_msm_set_uhs_signaling,
Asutosh Das33a4ff52012-12-18 16:14:02 +05302216 .check_power_status = sdhci_msm_check_power_status,
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07002217 .execute_tuning = sdhci_msm_execute_tuning,
2218 .toggle_cdr = sdhci_msm_toggle_cdr,
Asutosh Das3781bd82013-01-10 21:11:04 +05302219 .get_max_segments = sdhci_msm_max_segs,
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302220 .set_clock = sdhci_msm_set_clock,
Sahitya Tummala00240122013-02-28 19:50:51 +05302221 .get_min_clock = sdhci_msm_get_min_clock,
2222 .get_max_clock = sdhci_msm_get_max_clock,
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002223 .disable_data_xfer = sdhci_msm_disable_data_xfer,
Asutosh Das33a4ff52012-12-18 16:14:02 +05302224};
2225
2226static int __devinit sdhci_msm_probe(struct platform_device *pdev)
2227{
2228 struct sdhci_host *host;
2229 struct sdhci_pltfm_host *pltfm_host;
2230 struct sdhci_msm_host *msm_host;
2231 struct resource *core_memres = NULL;
Asutosh Dasbbc84782013-02-11 15:31:35 +05302232 int ret = 0, dead = 0;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302233 u32 vdd_max_current;
Stephen Boyd3edbd8f2013-04-24 14:19:46 -07002234 u16 host_version;
Subhash Jadavanic08d2062013-05-14 17:46:43 +05302235 u32 pwr, irq_status, irq_ctl;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302236
2237 pr_debug("%s: Enter %s\n", dev_name(&pdev->dev), __func__);
2238 msm_host = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_msm_host),
2239 GFP_KERNEL);
2240 if (!msm_host) {
2241 ret = -ENOMEM;
2242 goto out;
2243 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05302244
2245 msm_host->sdhci_msm_pdata.ops = &sdhci_msm_ops;
2246 host = sdhci_pltfm_init(pdev, &msm_host->sdhci_msm_pdata);
2247 if (IS_ERR(host)) {
2248 ret = PTR_ERR(host);
2249 goto out;
2250 }
2251
2252 pltfm_host = sdhci_priv(host);
2253 pltfm_host->priv = msm_host;
2254 msm_host->mmc = host->mmc;
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302255 msm_host->pdev = pdev;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302256
2257 /* Extract platform data */
2258 if (pdev->dev.of_node) {
Venkat Gopalakrishnanc61ab7e2013-03-11 12:17:57 -07002259 ret = of_alias_get_id(pdev->dev.of_node, "sdhc");
2260 if (ret < 0) {
2261 dev_err(&pdev->dev, "Failed to get slot index %d\n",
2262 ret);
2263 goto pltfm_free;
2264 }
2265 if (disable_slots & (1 << (ret - 1))) {
2266 dev_info(&pdev->dev, "%s: Slot %d disabled\n", __func__,
2267 ret);
2268 ret = -ENODEV;
2269 goto pltfm_free;
2270 }
2271
Asutosh Das33a4ff52012-12-18 16:14:02 +05302272 msm_host->pdata = sdhci_msm_populate_pdata(&pdev->dev);
2273 if (!msm_host->pdata) {
2274 dev_err(&pdev->dev, "DT parsing error\n");
2275 goto pltfm_free;
2276 }
2277 } else {
2278 dev_err(&pdev->dev, "No device tree node\n");
2279 goto pltfm_free;
2280 }
2281
2282 /* Setup Clocks */
2283
2284 /* Setup SDCC bus voter clock. */
2285 msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus_clk");
2286 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
2287 /* Vote for max. clk rate for max. performance */
2288 ret = clk_set_rate(msm_host->bus_clk, INT_MAX);
2289 if (ret)
2290 goto pltfm_free;
2291 ret = clk_prepare_enable(msm_host->bus_clk);
2292 if (ret)
2293 goto pltfm_free;
2294 }
2295
2296 /* Setup main peripheral bus clock */
2297 msm_host->pclk = devm_clk_get(&pdev->dev, "iface_clk");
2298 if (!IS_ERR(msm_host->pclk)) {
2299 ret = clk_prepare_enable(msm_host->pclk);
2300 if (ret)
2301 goto bus_clk_disable;
2302 }
2303
2304 /* Setup SDC MMC clock */
2305 msm_host->clk = devm_clk_get(&pdev->dev, "core_clk");
2306 if (IS_ERR(msm_host->clk)) {
2307 ret = PTR_ERR(msm_host->clk);
2308 goto pclk_disable;
2309 }
2310
Sahitya Tummala00240122013-02-28 19:50:51 +05302311 /* Set to the minimum supported clock frequency */
2312 ret = clk_set_rate(msm_host->clk, sdhci_msm_get_min_clock(host));
2313 if (ret) {
2314 dev_err(&pdev->dev, "MClk rate set failed (%d)\n", ret);
Sahitya Tummalac954ab02013-06-07 13:03:07 +05302315 goto pclk_disable;
Sahitya Tummala00240122013-02-28 19:50:51 +05302316 }
Sahitya Tummalac954ab02013-06-07 13:03:07 +05302317 ret = clk_prepare_enable(msm_host->clk);
2318 if (ret)
2319 goto pclk_disable;
2320
Sahitya Tummala00240122013-02-28 19:50:51 +05302321 msm_host->clk_rate = sdhci_msm_get_min_clock(host);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302322 atomic_set(&msm_host->clks_on, 1);
Sahitya Tummala00240122013-02-28 19:50:51 +05302323
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302324 ret = sdhci_msm_bus_register(msm_host, pdev);
2325 if (ret)
2326 goto clk_disable;
2327
2328 if (msm_host->msm_bus_vote.client_handle)
2329 INIT_DELAYED_WORK(&msm_host->msm_bus_vote.vote_work,
2330 sdhci_msm_bus_work);
2331 sdhci_msm_bus_voting(host, 1);
2332
Asutosh Das33a4ff52012-12-18 16:14:02 +05302333 /* Setup regulators */
2334 ret = sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, true);
2335 if (ret) {
2336 dev_err(&pdev->dev, "Regulator setup failed (%d)\n", ret);
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302337 goto bus_unregister;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302338 }
2339
2340 /* Reset the core and Enable SDHC mode */
2341 core_memres = platform_get_resource_byname(pdev,
2342 IORESOURCE_MEM, "core_mem");
2343 msm_host->core_mem = devm_ioremap(&pdev->dev, core_memres->start,
2344 resource_size(core_memres));
2345
2346 if (!msm_host->core_mem) {
2347 dev_err(&pdev->dev, "Failed to remap registers\n");
2348 ret = -ENOMEM;
2349 goto vreg_deinit;
2350 }
2351
2352 /* Set SW_RST bit in POWER register (Offset 0x0) */
Sahitya Tummalad5d76e72013-04-25 11:50:56 +05302353 writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_POWER) |
2354 CORE_SW_RST, msm_host->core_mem + CORE_POWER);
2355 /*
2356 * SW reset can take upto 10HCLK + 15MCLK cycles.
2357 * Calculating based on min clk rates (hclk = 27MHz,
2358 * mclk = 400KHz) it comes to ~40us. Let's poll for
2359 * max. 1ms for reset completion.
2360 */
2361 ret = readl_poll_timeout(msm_host->core_mem + CORE_POWER,
2362 pwr, !(pwr & CORE_SW_RST), 100, 10);
2363
2364 if (ret) {
2365 dev_err(&pdev->dev, "reset failed (%d)\n", ret);
2366 goto vreg_deinit;
2367 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05302368 /* Set HC_MODE_EN bit in HC_MODE register */
2369 writel_relaxed(HC_MODE_EN, (msm_host->core_mem + CORE_HC_MODE));
2370
2371 /*
Subhash Jadavanic08d2062013-05-14 17:46:43 +05302372 * CORE_SW_RST above may trigger power irq if previous status of PWRCTL
2373 * was either BUS_ON or IO_HIGH_V. So before we enable the power irq
2374 * interrupt in GIC (by registering the interrupt handler), we need to
2375 * ensure that any pending power irq interrupt status is acknowledged
2376 * otherwise power irq interrupt handler would be fired prematurely.
2377 */
2378 irq_status = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
2379 writel_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
2380 irq_ctl = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_CTL);
2381 if (irq_status & (CORE_PWRCTL_BUS_ON | CORE_PWRCTL_BUS_OFF))
2382 irq_ctl |= CORE_PWRCTL_BUS_SUCCESS;
2383 if (irq_status & (CORE_PWRCTL_IO_HIGH | CORE_PWRCTL_IO_LOW))
2384 irq_ctl |= CORE_PWRCTL_IO_SUCCESS;
2385 writel_relaxed(irq_ctl, (msm_host->core_mem + CORE_PWRCTL_CTL));
2386 /*
2387 * Ensure that above writes are propogated before interrupt enablement
2388 * in GIC.
2389 */
2390 mb();
2391
2392 /*
Asutosh Das33a4ff52012-12-18 16:14:02 +05302393 * Following are the deviations from SDHC spec v3.0 -
2394 * 1. Card detection is handled using separate GPIO.
2395 * 2. Bus power control is handled by interacting with PMIC.
2396 */
2397 host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
2398 host->quirks |= SDHCI_QUIRK_SINGLE_POWER_WRITE;
Sahitya Tummala00240122013-02-28 19:50:51 +05302399 host->quirks |= SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN;
2400 host->quirks2 |= SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK;
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302401 host->quirks2 |= SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING;
Krishna Kondaa20d3362013-04-01 21:01:59 -07002402 host->quirks2 |= SDHCI_QUIRK2_USE_MAX_DISCARD_SIZE;
Sahitya Tummalad2ae8832013-04-12 11:49:11 +05302403 host->quirks2 |= SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD;
Sahitya Tummalae6886bd2013-04-12 12:11:20 +05302404 host->quirks2 |= SDHCI_QUIRK2_BROKEN_PRESET_VALUE;
Sahitya Tummala4d12d0b2013-04-12 11:59:25 +05302405 host->quirks2 |= SDHCI_QUIRK2_USE_RESERVED_MAX_TIMEOUT;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302406
Sahitya Tummalaf667cc12013-06-10 16:32:51 +05302407 if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK)
2408 host->quirks2 |= SDHCI_QUIRK2_DIVIDE_TOUT_BY_4;
2409
Stephen Boyd3edbd8f2013-04-24 14:19:46 -07002410 host_version = readw_relaxed((host->ioaddr + SDHCI_HOST_VERSION));
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002411 dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n",
2412 host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >>
2413 SDHCI_VENDOR_VER_SHIFT));
2414 if (((host_version & SDHCI_VENDOR_VER_MASK) >>
2415 SDHCI_VENDOR_VER_SHIFT) == SDHCI_VER_100) {
2416 /*
2417 * Add 40us delay in interrupt handler when
2418 * operating at initialization frequency(400KHz).
2419 */
2420 host->quirks2 |= SDHCI_QUIRK2_SLOW_INT_CLR;
2421 /*
2422 * Set Software Reset for DAT line in Software
2423 * Reset Register (Bit 2).
2424 */
2425 host->quirks2 |= SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT;
2426 }
2427
2428 /* Setup PWRCTL irq */
Asutosh Dasbbc84782013-02-11 15:31:35 +05302429 msm_host->pwr_irq = platform_get_irq_byname(pdev, "pwr_irq");
2430 if (msm_host->pwr_irq < 0) {
Asutosh Das33a4ff52012-12-18 16:14:02 +05302431 dev_err(&pdev->dev, "Failed to get pwr_irq by name (%d)\n",
Asutosh Dasbbc84782013-02-11 15:31:35 +05302432 msm_host->pwr_irq);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302433 goto vreg_deinit;
2434 }
Asutosh Dasbbc84782013-02-11 15:31:35 +05302435 ret = devm_request_threaded_irq(&pdev->dev, msm_host->pwr_irq, NULL,
Asutosh Das33a4ff52012-12-18 16:14:02 +05302436 sdhci_msm_pwr_irq, IRQF_ONESHOT,
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07002437 dev_name(&pdev->dev), host);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302438 if (ret) {
2439 dev_err(&pdev->dev, "Request threaded irq(%d) failed (%d)\n",
Asutosh Dasbbc84782013-02-11 15:31:35 +05302440 msm_host->pwr_irq, ret);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302441 goto vreg_deinit;
2442 }
2443
2444 /* Enable pwr irq interrupts */
2445 writel_relaxed(INT_MASK, (msm_host->core_mem + CORE_PWRCTL_MASK));
2446
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302447 /* Set clock gating delay to be used when CONFIG_MMC_CLKGATE is set */
2448 msm_host->mmc->clkgate_delay = SDHCI_MSM_MMC_CLK_GATE_DELAY;
2449
Asutosh Das33a4ff52012-12-18 16:14:02 +05302450 /* Set host capabilities */
2451 msm_host->mmc->caps |= msm_host->pdata->mmc_bus_width;
2452 msm_host->mmc->caps |= msm_host->pdata->caps;
2453
2454 vdd_max_current = sdhci_msm_get_vreg_vdd_max_current(msm_host);
2455 if (vdd_max_current >= 800)
2456 msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_800;
2457 else if (vdd_max_current >= 600)
2458 msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_600;
2459 else if (vdd_max_current >= 400)
2460 msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_400;
2461 else
2462 msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_200;
2463
2464 if (vdd_max_current > 150)
2465 msm_host->mmc->caps |= MMC_CAP_SET_XPC_180 |
2466 MMC_CAP_SET_XPC_300|
2467 MMC_CAP_SET_XPC_330;
2468
2469 msm_host->mmc->caps2 |= msm_host->pdata->caps2;
Asutosh Dasbbc84782013-02-11 15:31:35 +05302470 msm_host->mmc->caps2 |= MMC_CAP2_CORE_RUNTIME_PM;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302471 msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR;
2472 msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR_CONTROL;
2473 msm_host->mmc->caps2 |= (MMC_CAP2_BOOTPART_NOACC |
2474 MMC_CAP2_DETECT_ON_ERR);
2475 msm_host->mmc->caps2 |= MMC_CAP2_SANITIZE;
2476 msm_host->mmc->caps2 |= MMC_CAP2_CACHE_CTRL;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302477 msm_host->mmc->caps2 |= MMC_CAP2_POWEROFF_NOTIFY;
Sahitya Tummala00240122013-02-28 19:50:51 +05302478 msm_host->mmc->caps2 |= MMC_CAP2_CLK_SCALE;
Konstantin Dorfmanfa436d52013-04-17 16:26:11 +03002479 msm_host->mmc->caps2 |= MMC_CAP2_STOP_REQUEST;
Subhash Jadavani61a52c92013-05-29 15:52:10 +05302480 msm_host->mmc->caps2 |= MMC_CAP2_ASYNC_SDIO_IRQ_4BIT_MODE;
Asutosh Das4dc60412013-06-24 18:20:45 +05302481 msm_host->mmc->pm_caps |= MMC_PM_KEEP_POWER;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302482
2483 if (msm_host->pdata->nonremovable)
2484 msm_host->mmc->caps |= MMC_CAP_NONREMOVABLE;
2485
Sahitya Tummalab4e84042013-03-10 07:03:17 +05302486 host->cpu_dma_latency_us = msm_host->pdata->cpu_dma_latency_us;
2487
Sahitya Tummala179e7382013-03-20 19:24:01 +05302488 init_completion(&msm_host->pwr_irq_completion);
2489
Sahitya Tummala62448d92013-03-12 14:57:46 +05302490 if (gpio_is_valid(msm_host->pdata->status_gpio)) {
2491 ret = mmc_cd_gpio_request(msm_host->mmc,
2492 msm_host->pdata->status_gpio);
2493 if (ret) {
2494 dev_err(&pdev->dev, "%s: Failed to request card detection IRQ %d\n",
2495 __func__, ret);
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302496 goto vreg_deinit;
Sahitya Tummala62448d92013-03-12 14:57:46 +05302497 }
2498 }
2499
Sahitya Tummala2fa7eb12013-03-20 19:34:59 +05302500 if (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(32))) {
2501 host->dma_mask = DMA_BIT_MASK(32);
2502 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2503 } else {
2504 dev_err(&pdev->dev, "%s: Failed to set dma mask\n", __func__);
2505 }
2506
Asutosh Das33a4ff52012-12-18 16:14:02 +05302507 ret = sdhci_add_host(host);
2508 if (ret) {
2509 dev_err(&pdev->dev, "Add host failed (%d)\n", ret);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302510 goto free_cd_gpio;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302511 }
2512
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302513 msm_host->msm_bus_vote.max_bus_bw.show = show_sdhci_max_bus_bw;
2514 msm_host->msm_bus_vote.max_bus_bw.store = store_sdhci_max_bus_bw;
2515 sysfs_attr_init(&msm_host->msm_bus_vote.max_bus_bw.attr);
2516 msm_host->msm_bus_vote.max_bus_bw.attr.name = "max_bus_bw";
2517 msm_host->msm_bus_vote.max_bus_bw.attr.mode = S_IRUGO | S_IWUSR;
2518 ret = device_create_file(&pdev->dev,
2519 &msm_host->msm_bus_vote.max_bus_bw);
2520 if (ret)
2521 goto remove_host;
2522
Sahitya Tummala3b292c32013-06-20 14:00:18 +05302523 if (!gpio_is_valid(msm_host->pdata->status_gpio)) {
2524 msm_host->polling.show = show_polling;
2525 msm_host->polling.store = store_polling;
2526 sysfs_attr_init(&msm_host->polling.attr);
2527 msm_host->polling.attr.name = "polling";
2528 msm_host->polling.attr.mode = S_IRUGO | S_IWUSR;
2529 ret = device_create_file(&pdev->dev, &msm_host->polling);
2530 if (ret)
2531 goto remove_max_bus_bw_file;
2532 }
Asutosh Dasbbc84782013-02-11 15:31:35 +05302533 ret = pm_runtime_set_active(&pdev->dev);
2534 if (ret)
2535 pr_err("%s: %s: pm_runtime_set_active failed: err: %d\n",
2536 mmc_hostname(host->mmc), __func__, ret);
2537 else
2538 pm_runtime_enable(&pdev->dev);
2539
Asutosh Das33a4ff52012-12-18 16:14:02 +05302540 /* Successful initialization */
2541 goto out;
2542
Sahitya Tummala3b292c32013-06-20 14:00:18 +05302543remove_max_bus_bw_file:
2544 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302545remove_host:
2546 dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
2547 sdhci_remove_host(host, dead);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302548free_cd_gpio:
2549 if (gpio_is_valid(msm_host->pdata->status_gpio))
2550 mmc_cd_gpio_free(msm_host->mmc);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302551vreg_deinit:
2552 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302553bus_unregister:
2554 if (msm_host->msm_bus_vote.client_handle)
2555 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2556 sdhci_msm_bus_unregister(msm_host);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302557clk_disable:
2558 if (!IS_ERR(msm_host->clk))
2559 clk_disable_unprepare(msm_host->clk);
2560pclk_disable:
2561 if (!IS_ERR(msm_host->pclk))
2562 clk_disable_unprepare(msm_host->pclk);
2563bus_clk_disable:
2564 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2565 clk_disable_unprepare(msm_host->bus_clk);
2566pltfm_free:
2567 sdhci_pltfm_free(pdev);
2568out:
2569 pr_debug("%s: Exit %s\n", dev_name(&pdev->dev), __func__);
2570 return ret;
2571}
2572
2573static int __devexit sdhci_msm_remove(struct platform_device *pdev)
2574{
2575 struct sdhci_host *host = platform_get_drvdata(pdev);
2576 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2577 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2578 struct sdhci_msm_pltfm_data *pdata = msm_host->pdata;
2579 int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) ==
2580 0xffffffff);
2581
2582 pr_debug("%s: %s\n", dev_name(&pdev->dev), __func__);
Sahitya Tummala3b292c32013-06-20 14:00:18 +05302583 if (!gpio_is_valid(msm_host->pdata->status_gpio))
2584 device_remove_file(&pdev->dev, &msm_host->polling);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302585 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302586 sdhci_remove_host(host, dead);
Asutosh Dasbbc84782013-02-11 15:31:35 +05302587 pm_runtime_disable(&pdev->dev);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302588 sdhci_pltfm_free(pdev);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302589
2590 if (gpio_is_valid(msm_host->pdata->status_gpio))
2591 mmc_cd_gpio_free(msm_host->mmc);
2592
Asutosh Das33a4ff52012-12-18 16:14:02 +05302593 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302594
Asutosh Das33a4ff52012-12-18 16:14:02 +05302595 if (pdata->pin_data)
Asutosh Das390519d2012-12-21 12:21:42 +05302596 sdhci_msm_setup_pins(pdata, false);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302597
2598 if (msm_host->msm_bus_vote.client_handle) {
2599 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2600 sdhci_msm_bus_unregister(msm_host);
2601 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05302602 return 0;
2603}
2604
Asutosh Dasbbc84782013-02-11 15:31:35 +05302605static int sdhci_msm_runtime_suspend(struct device *dev)
2606{
2607 struct sdhci_host *host = dev_get_drvdata(dev);
2608 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2609 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2610
2611 disable_irq(host->irq);
2612 disable_irq(msm_host->pwr_irq);
2613
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302614 /*
2615 * Remove the vote immediately only if clocks are off in which
2616 * case we might have queued work to remove vote but it may not
2617 * be completed before runtime suspend or system suspend.
2618 */
2619 if (!atomic_read(&msm_host->clks_on)) {
2620 if (msm_host->msm_bus_vote.client_handle)
2621 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2622 }
2623
Asutosh Dasbbc84782013-02-11 15:31:35 +05302624 return 0;
2625}
2626
2627static int sdhci_msm_runtime_resume(struct device *dev)
2628{
2629 struct sdhci_host *host = dev_get_drvdata(dev);
2630 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2631 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2632
2633 enable_irq(msm_host->pwr_irq);
2634 enable_irq(host->irq);
2635
2636 return 0;
2637}
2638
2639#ifdef CONFIG_PM_SLEEP
2640
2641static int sdhci_msm_suspend(struct device *dev)
2642{
2643 struct sdhci_host *host = dev_get_drvdata(dev);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302644 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2645 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Dasbbc84782013-02-11 15:31:35 +05302646 int ret = 0;
2647
Sahitya Tummala62448d92013-03-12 14:57:46 +05302648 if (gpio_is_valid(msm_host->pdata->status_gpio))
2649 mmc_cd_gpio_free(msm_host->mmc);
2650
Asutosh Dasbbc84782013-02-11 15:31:35 +05302651 if (pm_runtime_suspended(dev)) {
2652 pr_debug("%s: %s: already runtime suspended\n",
2653 mmc_hostname(host->mmc), __func__);
2654 goto out;
2655 }
2656
2657 return sdhci_msm_runtime_suspend(dev);
2658out:
2659 return ret;
2660}
2661
2662static int sdhci_msm_resume(struct device *dev)
2663{
2664 struct sdhci_host *host = dev_get_drvdata(dev);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302665 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2666 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Dasbbc84782013-02-11 15:31:35 +05302667 int ret = 0;
2668
Sahitya Tummala62448d92013-03-12 14:57:46 +05302669 if (gpio_is_valid(msm_host->pdata->status_gpio)) {
2670 ret = mmc_cd_gpio_request(msm_host->mmc,
2671 msm_host->pdata->status_gpio);
2672 if (ret)
2673 pr_err("%s: %s: Failed to request card detection IRQ %d\n",
2674 mmc_hostname(host->mmc), __func__, ret);
2675 }
2676
Asutosh Dasbbc84782013-02-11 15:31:35 +05302677 if (pm_runtime_suspended(dev)) {
2678 pr_debug("%s: %s: runtime suspended, defer system resume\n",
2679 mmc_hostname(host->mmc), __func__);
2680 goto out;
2681 }
2682
2683 return sdhci_msm_runtime_resume(dev);
2684out:
2685 return ret;
2686}
2687#endif
2688
2689#ifdef CONFIG_PM
2690static const struct dev_pm_ops sdhci_msm_pmops = {
2691 SET_SYSTEM_SLEEP_PM_OPS(sdhci_msm_suspend, sdhci_msm_resume)
2692 SET_RUNTIME_PM_OPS(sdhci_msm_runtime_suspend, sdhci_msm_runtime_resume,
2693 NULL)
2694};
2695
2696#define SDHCI_MSM_PMOPS (&sdhci_msm_pmops)
2697
2698#else
2699#define SDHCI_PM_OPS NULL
2700#endif
Asutosh Das33a4ff52012-12-18 16:14:02 +05302701static const struct of_device_id sdhci_msm_dt_match[] = {
2702 {.compatible = "qcom,sdhci-msm"},
2703};
2704MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match);
2705
2706static struct platform_driver sdhci_msm_driver = {
2707 .probe = sdhci_msm_probe,
2708 .remove = __devexit_p(sdhci_msm_remove),
2709 .driver = {
2710 .name = "sdhci_msm",
2711 .owner = THIS_MODULE,
2712 .of_match_table = sdhci_msm_dt_match,
Asutosh Dasbbc84782013-02-11 15:31:35 +05302713 .pm = SDHCI_MSM_PMOPS,
Asutosh Das33a4ff52012-12-18 16:14:02 +05302714 },
2715};
2716
2717module_platform_driver(sdhci_msm_driver);
2718
2719MODULE_DESCRIPTION("Qualcomm Secure Digital Host Controller Interface driver");
2720MODULE_LICENSE("GPL v2");