blob: 34aa3a1d24ef5d84c638a5fe0486df65805349cb [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
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -070048#define FF_CLK_SW_RST_DIS (1 << 13)
Asutosh Das33a4ff52012-12-18 16:14:02 +053049
50#define CORE_POWER 0x0
51#define CORE_SW_RST (1 << 7)
52
53#define CORE_PWRCTL_STATUS 0xDC
54#define CORE_PWRCTL_MASK 0xE0
55#define CORE_PWRCTL_CLEAR 0xE4
56#define CORE_PWRCTL_CTL 0xE8
57
58#define CORE_PWRCTL_BUS_OFF 0x01
59#define CORE_PWRCTL_BUS_ON (1 << 1)
60#define CORE_PWRCTL_IO_LOW (1 << 2)
61#define CORE_PWRCTL_IO_HIGH (1 << 3)
62
63#define CORE_PWRCTL_BUS_SUCCESS 0x01
64#define CORE_PWRCTL_BUS_FAIL (1 << 1)
65#define CORE_PWRCTL_IO_SUCCESS (1 << 2)
66#define CORE_PWRCTL_IO_FAIL (1 << 3)
67
68#define INT_MASK 0xF
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -070069#define MAX_PHASES 16
70
71#define CORE_DLL_LOCK (1 << 7)
72#define CORE_DLL_EN (1 << 16)
73#define CORE_CDR_EN (1 << 17)
74#define CORE_CK_OUT_EN (1 << 18)
75#define CORE_CDR_EXT_EN (1 << 19)
76#define CORE_DLL_PDN (1 << 29)
77#define CORE_DLL_RST (1 << 30)
78#define CORE_DLL_CONFIG 0x100
79#define CORE_DLL_TEST_CTL 0x104
80#define CORE_DLL_STATUS 0x108
81
82#define CORE_VENDOR_SPEC 0x10C
83#define CORE_CLK_PWRSAVE (1 << 1)
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -070084#define CORE_HC_MCLK_SEL_DFLT (2 << 8)
85#define CORE_HC_MCLK_SEL_HS400 (3 << 8)
86#define CORE_HC_MCLK_SEL_MASK (3 << 8)
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -070087#define CORE_IO_PAD_PWR_SWITCH (1 << 16)
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -070088#define CORE_HC_SELECT_IN_EN (1 << 18)
89#define CORE_HC_SELECT_IN_HS400 (6 << 19)
90#define CORE_HC_SELECT_IN_MASK (7 << 19)
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -070091
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +030092#define CORE_MCI_DATA_CTRL 0x2C
93#define CORE_MCI_DPSM_ENABLE (1 << 0)
94
95#define CORE_TESTBUS_CONFIG 0x0CC
96#define CORE_TESTBUS_ENA (1 << 3)
97#define CORE_TESTBUS_SEL2 (1 << 4)
98
Venkat Gopalakrishnan0a179c82013-06-26 17:56:11 -070099#define CORE_MCI_VERSION 0x050
100#define CORE_VERSION_310 0x10000011
101
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +0300102/*
103 * Waiting until end of potential AHB access for data:
104 * 16 AHB cycles (160ns for 100MHz and 320ns for 50MHz) +
105 * delay on AHB (2us) = maximum 2.32us
106 * Taking x10 times margin
107 */
108#define CORE_AHB_DATA_DELAY_US 23
109/* Waiting until end of potential AHB access for descriptor:
110 * Single (1 AHB cycle) + delay on AHB bus = max 2us
111 * INCR4 (4 AHB cycles) + delay on AHB bus = max 2us
112 * Single (1 AHB cycle) + delay on AHB bus = max 2us
113 * Total 8 us delay with margin
114 */
115#define CORE_AHB_DESC_DELAY_US 8
116
117#define CORE_SDCC_DEBUG_REG 0x124
118#define CORE_DEBUG_REG_AHB_HTRANS (3 << 12)
119
Asutosh Das3781bd82013-01-10 21:11:04 +0530120/* 8KB descriptors */
121#define SDHCI_MSM_MAX_SEGMENTS (1 << 13)
Sahitya Tummala04c3a462013-01-11 11:30:45 +0530122#define SDHCI_MSM_MMC_CLK_GATE_DELAY 200 /* msecs */
Asutosh Das3781bd82013-01-10 21:11:04 +0530123
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -0700124#define CORE_FREQ_100MHZ (100 * 1000 * 1000)
125
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700126static const u32 tuning_block_64[] = {
127 0x00FF0FFF, 0xCCC3CCFF, 0xFFCC3CC3, 0xEFFEFFFE,
128 0xDDFFDFFF, 0xFBFFFBFF, 0xFF7FFFBF, 0xEFBDF777,
129 0xF0FFF0FF, 0x3CCCFC0F, 0xCFCC33CC, 0xEEFFEFFF,
130 0xFDFFFDFF, 0xFFBFFFDF, 0xFFF7FFBB, 0xDE7B7FF7
131};
132
133static const u32 tuning_block_128[] = {
134 0xFF00FFFF, 0x0000FFFF, 0xCCCCFFFF, 0xCCCC33CC,
135 0xCC3333CC, 0xFFFFCCCC, 0xFFFFEEFF, 0xFFEEEEFF,
136 0xFFDDFFFF, 0xDDDDFFFF, 0xBBFFFFFF, 0xBBFFFFFF,
137 0xFFFFFFBB, 0xFFFFFF77, 0x77FF7777, 0xFFEEDDBB,
138 0x00FFFFFF, 0x00FFFFFF, 0xCCFFFF00, 0xCC33CCCC,
139 0x3333CCCC, 0xFFCCCCCC, 0xFFEEFFFF, 0xEEEEFFFF,
140 0xDDFFFFFF, 0xDDFFFFFF, 0xFFFFFFDD, 0xFFFFFFBB,
141 0xFFFFBBBB, 0xFFFF77FF, 0xFF7777FF, 0xEEDDBB77
142};
Asutosh Das33a4ff52012-12-18 16:14:02 +0530143
Venkat Gopalakrishnanc61ab7e2013-03-11 12:17:57 -0700144static int disable_slots;
145/* root can write, others read */
146module_param(disable_slots, int, S_IRUGO|S_IWUSR);
147
Asutosh Das33a4ff52012-12-18 16:14:02 +0530148/* This structure keeps information per regulator */
149struct sdhci_msm_reg_data {
150 /* voltage regulator handle */
151 struct regulator *reg;
152 /* regulator name */
153 const char *name;
154 /* voltage level to be set */
155 u32 low_vol_level;
156 u32 high_vol_level;
157 /* Load values for low power and high power mode */
158 u32 lpm_uA;
159 u32 hpm_uA;
160
161 /* is this regulator enabled? */
162 bool is_enabled;
163 /* is this regulator needs to be always on? */
164 bool is_always_on;
165 /* is low power mode setting required for this regulator? */
166 bool lpm_sup;
Asutosh Das95afcad2013-06-28 15:03:44 +0530167 bool set_voltage_sup;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530168};
169
170/*
171 * This structure keeps information for all the
172 * regulators required for a SDCC slot.
173 */
174struct sdhci_msm_slot_reg_data {
175 /* keeps VDD/VCC regulator info */
176 struct sdhci_msm_reg_data *vdd_data;
177 /* keeps VDD IO regulator info */
178 struct sdhci_msm_reg_data *vdd_io_data;
179};
180
181struct sdhci_msm_gpio {
182 u32 no;
183 const char *name;
184 bool is_enabled;
185};
186
187struct sdhci_msm_gpio_data {
188 struct sdhci_msm_gpio *gpio;
189 u8 size;
190};
191
Asutosh Das390519d2012-12-21 12:21:42 +0530192struct sdhci_msm_pad_pull {
193 enum msm_tlmm_pull_tgt no;
194 u32 val;
195};
196
197struct sdhci_msm_pad_pull_data {
198 struct sdhci_msm_pad_pull *on;
199 struct sdhci_msm_pad_pull *off;
200 u8 size;
201};
202
203struct sdhci_msm_pad_drv {
204 enum msm_tlmm_hdrive_tgt no;
205 u32 val;
206};
207
208struct sdhci_msm_pad_drv_data {
209 struct sdhci_msm_pad_drv *on;
210 struct sdhci_msm_pad_drv *off;
211 u8 size;
212};
213
214struct sdhci_msm_pad_data {
215 struct sdhci_msm_pad_pull_data *pull;
216 struct sdhci_msm_pad_drv_data *drv;
217};
218
219
Asutosh Das33a4ff52012-12-18 16:14:02 +0530220struct sdhci_msm_pin_data {
221 /*
222 * = 1 if controller pins are using gpios
223 * = 0 if controller has dedicated MSM pads
224 */
Asutosh Das390519d2012-12-21 12:21:42 +0530225 u8 is_gpio;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530226 bool cfg_sts;
227 struct sdhci_msm_gpio_data *gpio_data;
Asutosh Das390519d2012-12-21 12:21:42 +0530228 struct sdhci_msm_pad_data *pad_data;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530229};
230
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +0530231struct sdhci_msm_bus_voting_data {
232 struct msm_bus_scale_pdata *bus_pdata;
233 unsigned int *bw_vecs;
234 unsigned int bw_vecs_size;
235};
236
Asutosh Das33a4ff52012-12-18 16:14:02 +0530237struct sdhci_msm_pltfm_data {
238 /* Supported UHS-I Modes */
239 u32 caps;
240
241 /* More capabilities */
242 u32 caps2;
243
244 unsigned long mmc_bus_width;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530245 struct sdhci_msm_slot_reg_data *vreg_data;
246 bool nonremovable;
247 struct sdhci_msm_pin_data *pin_data;
Sahitya Tummalab4e84042013-03-10 07:03:17 +0530248 u32 cpu_dma_latency_us;
Sahitya Tummala62448d92013-03-12 14:57:46 +0530249 int status_gpio; /* card detection GPIO that is configured as IRQ */
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +0530250 struct sdhci_msm_bus_voting_data *voting_data;
Sahitya Tummala00240122013-02-28 19:50:51 +0530251 u32 *sup_clk_table;
252 unsigned char sup_clk_cnt;
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +0530253};
254
255struct sdhci_msm_bus_vote {
256 uint32_t client_handle;
257 uint32_t curr_vote;
258 int min_bw_vote;
259 int max_bw_vote;
260 bool is_max_bw_needed;
261 struct delayed_work vote_work;
262 struct device_attribute max_bus_bw;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530263};
264
265struct sdhci_msm_host {
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +0530266 struct platform_device *pdev;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530267 void __iomem *core_mem; /* MSM SDCC mapped address */
Asutosh Dasbbc84782013-02-11 15:31:35 +0530268 int pwr_irq; /* power irq */
Asutosh Das33a4ff52012-12-18 16:14:02 +0530269 struct clk *clk; /* main SD/MMC bus clock */
270 struct clk *pclk; /* SDHC peripheral bus clock */
271 struct clk *bus_clk; /* SDHC bus voter clock */
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -0700272 struct clk *ff_clk; /* CDC calibration fixed feedback clock */
273 struct clk *sleep_clk; /* CDC calibration sleep clock */
Sahitya Tummala04c3a462013-01-11 11:30:45 +0530274 atomic_t clks_on; /* Set if clocks are enabled */
Asutosh Das33a4ff52012-12-18 16:14:02 +0530275 struct sdhci_msm_pltfm_data *pdata;
276 struct mmc_host *mmc;
277 struct sdhci_pltfm_data sdhci_msm_pdata;
Sahitya Tummala179e7382013-03-20 19:24:01 +0530278 u32 curr_pwr_state;
279 u32 curr_io_level;
280 struct completion pwr_irq_completion;
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +0530281 struct sdhci_msm_bus_vote msm_bus_vote;
Sahitya Tummala3b292c32013-06-20 14:00:18 +0530282 struct device_attribute polling;
Sahitya Tummala00240122013-02-28 19:50:51 +0530283 u32 clk_rate; /* Keeps track of current clock rate that is set */
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -0700284 bool tuning_done;
285 bool calibration_done;
Asutosh Das33a4ff52012-12-18 16:14:02 +0530286};
287
288enum vdd_io_level {
289 /* set vdd_io_data->low_vol_level */
290 VDD_IO_LOW,
291 /* set vdd_io_data->high_vol_level */
292 VDD_IO_HIGH,
293 /*
294 * set whatever there in voltage_level (third argument) of
295 * sdhci_msm_set_vdd_io_vol() function.
296 */
297 VDD_IO_SET_LEVEL,
298};
299
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700300/* MSM platform specific tuning */
301static inline int msm_dll_poll_ck_out_en(struct sdhci_host *host,
302 u8 poll)
303{
304 int rc = 0;
305 u32 wait_cnt = 50;
306 u8 ck_out_en = 0;
307 struct mmc_host *mmc = host->mmc;
308
309 /* poll for CK_OUT_EN bit. max. poll time = 50us */
310 ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) &
311 CORE_CK_OUT_EN);
312
313 while (ck_out_en != poll) {
314 if (--wait_cnt == 0) {
315 pr_err("%s: %s: CK_OUT_EN bit is not %d\n",
316 mmc_hostname(mmc), __func__, poll);
317 rc = -ETIMEDOUT;
318 goto out;
319 }
320 udelay(1);
321
322 ck_out_en = !!(readl_relaxed(host->ioaddr +
323 CORE_DLL_CONFIG) & CORE_CK_OUT_EN);
324 }
325out:
326 return rc;
327}
328
329static int msm_config_cm_dll_phase(struct sdhci_host *host, u8 phase)
330{
331 int rc = 0;
332 u8 grey_coded_phase_table[] = {0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4,
333 0xC, 0xD, 0xF, 0xE, 0xA, 0xB, 0x9,
334 0x8};
335 unsigned long flags;
336 u32 config;
337 struct mmc_host *mmc = host->mmc;
338
339 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
340 spin_lock_irqsave(&host->lock, flags);
341
342 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
343 config &= ~(CORE_CDR_EN | CORE_CK_OUT_EN);
344 config |= (CORE_CDR_EXT_EN | CORE_DLL_EN);
345 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
346
347 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '0' */
348 rc = msm_dll_poll_ck_out_en(host, 0);
349 if (rc)
350 goto err_out;
351
352 /*
353 * Write the selected DLL clock output phase (0 ... 15)
354 * to CDR_SELEXT bit field of DLL_CONFIG register.
355 */
356 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
357 & ~(0xF << 20))
358 | (grey_coded_phase_table[phase] << 20)),
359 host->ioaddr + CORE_DLL_CONFIG);
360
361 /* Set CK_OUT_EN bit of DLL_CONFIG register to 1. */
362 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
363 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
364
365 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '1' */
366 rc = msm_dll_poll_ck_out_en(host, 1);
367 if (rc)
368 goto err_out;
369
370 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
371 config |= CORE_CDR_EN;
372 config &= ~CORE_CDR_EXT_EN;
373 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
374 goto out;
375
376err_out:
377 pr_err("%s: %s: Failed to set DLL phase: %d\n",
378 mmc_hostname(mmc), __func__, phase);
379out:
380 spin_unlock_irqrestore(&host->lock, flags);
381 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
382 return rc;
383}
384
385/*
386 * Find out the greatest range of consecuitive selected
387 * DLL clock output phases that can be used as sampling
388 * setting for SD3.0 UHS-I card read operation (in SDR104
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -0700389 * timing mode) or for eMMC4.5 card read operation (in
390 * HS400/HS200 timing mode).
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700391 * Select the 3/4 of the range and configure the DLL with the
392 * selected DLL clock output phase.
393 */
394
395static int msm_find_most_appropriate_phase(struct sdhci_host *host,
396 u8 *phase_table, u8 total_phases)
397{
398 int ret;
399 u8 ranges[MAX_PHASES][MAX_PHASES] = { {0}, {0} };
400 u8 phases_per_row[MAX_PHASES] = {0};
401 int row_index = 0, col_index = 0, selected_row_index = 0, curr_max = 0;
402 int i, cnt, phase_0_raw_index = 0, phase_15_raw_index = 0;
403 bool phase_0_found = false, phase_15_found = false;
404 struct mmc_host *mmc = host->mmc;
405
406 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
407 if (!total_phases || (total_phases > MAX_PHASES)) {
408 pr_err("%s: %s: invalid argument: total_phases=%d\n",
409 mmc_hostname(mmc), __func__, total_phases);
410 return -EINVAL;
411 }
412
413 for (cnt = 0; cnt < total_phases; cnt++) {
414 ranges[row_index][col_index] = phase_table[cnt];
415 phases_per_row[row_index] += 1;
416 col_index++;
417
418 if ((cnt + 1) == total_phases) {
419 continue;
420 /* check if next phase in phase_table is consecutive or not */
421 } else if ((phase_table[cnt] + 1) != phase_table[cnt + 1]) {
422 row_index++;
423 col_index = 0;
424 }
425 }
426
427 if (row_index >= MAX_PHASES)
428 return -EINVAL;
429
430 /* Check if phase-0 is present in first valid window? */
431 if (!ranges[0][0]) {
432 phase_0_found = true;
433 phase_0_raw_index = 0;
434 /* Check if cycle exist between 2 valid windows */
435 for (cnt = 1; cnt <= row_index; cnt++) {
436 if (phases_per_row[cnt]) {
437 for (i = 0; i < phases_per_row[cnt]; i++) {
438 if (ranges[cnt][i] == 15) {
439 phase_15_found = true;
440 phase_15_raw_index = cnt;
441 break;
442 }
443 }
444 }
445 }
446 }
447
448 /* If 2 valid windows form cycle then merge them as single window */
449 if (phase_0_found && phase_15_found) {
450 /* number of phases in raw where phase 0 is present */
451 u8 phases_0 = phases_per_row[phase_0_raw_index];
452 /* number of phases in raw where phase 15 is present */
453 u8 phases_15 = phases_per_row[phase_15_raw_index];
454
455 if (phases_0 + phases_15 >= MAX_PHASES)
456 /*
457 * If there are more than 1 phase windows then total
458 * number of phases in both the windows should not be
459 * more than or equal to MAX_PHASES.
460 */
461 return -EINVAL;
462
463 /* Merge 2 cyclic windows */
464 i = phases_15;
465 for (cnt = 0; cnt < phases_0; cnt++) {
466 ranges[phase_15_raw_index][i] =
467 ranges[phase_0_raw_index][cnt];
468 if (++i >= MAX_PHASES)
469 break;
470 }
471
472 phases_per_row[phase_0_raw_index] = 0;
473 phases_per_row[phase_15_raw_index] = phases_15 + phases_0;
474 }
475
476 for (cnt = 0; cnt <= row_index; cnt++) {
477 if (phases_per_row[cnt] > curr_max) {
478 curr_max = phases_per_row[cnt];
479 selected_row_index = cnt;
480 }
481 }
482
483 i = ((curr_max * 3) / 4);
484 if (i)
485 i--;
486
487 ret = (int)ranges[selected_row_index][i];
488
489 if (ret >= MAX_PHASES) {
490 ret = -EINVAL;
491 pr_err("%s: %s: invalid phase selected=%d\n",
492 mmc_hostname(mmc), __func__, ret);
493 }
494
495 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
496 return ret;
497}
498
499static inline void msm_cm_dll_set_freq(struct sdhci_host *host)
500{
501 u32 mclk_freq = 0;
502
503 /* Program the MCLK value to MCLK_FREQ bit field */
504 if (host->clock <= 112000000)
505 mclk_freq = 0;
506 else if (host->clock <= 125000000)
507 mclk_freq = 1;
508 else if (host->clock <= 137000000)
509 mclk_freq = 2;
510 else if (host->clock <= 150000000)
511 mclk_freq = 3;
512 else if (host->clock <= 162000000)
513 mclk_freq = 4;
514 else if (host->clock <= 175000000)
515 mclk_freq = 5;
516 else if (host->clock <= 187000000)
517 mclk_freq = 6;
518 else if (host->clock <= 200000000)
519 mclk_freq = 7;
520
521 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
522 & ~(7 << 24)) | (mclk_freq << 24)),
523 host->ioaddr + CORE_DLL_CONFIG);
524}
525
526/* Initialize the DLL (Programmable Delay Line ) */
527static int msm_init_cm_dll(struct sdhci_host *host)
528{
529 struct mmc_host *mmc = host->mmc;
530 int rc = 0;
531 unsigned long flags;
532 u32 wait_cnt;
Subhash Jadavaniefb5f622013-05-28 18:21:57 +0530533 bool prev_pwrsave, curr_pwrsave;
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700534
535 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
536 spin_lock_irqsave(&host->lock, flags);
Subhash Jadavaniefb5f622013-05-28 18:21:57 +0530537 prev_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
538 CORE_CLK_PWRSAVE);
539 curr_pwrsave = prev_pwrsave;
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700540 /*
541 * Make sure that clock is always enabled when DLL
542 * tuning is in progress. Keeping PWRSAVE ON may
543 * turn off the clock. So let's disable the PWRSAVE
544 * here and re-enable it once tuning is completed.
545 */
Subhash Jadavaniefb5f622013-05-28 18:21:57 +0530546 if (prev_pwrsave) {
547 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
548 & ~CORE_CLK_PWRSAVE),
549 host->ioaddr + CORE_VENDOR_SPEC);
550 curr_pwrsave = false;
551 }
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700552
553 /* Write 1 to DLL_RST bit of DLL_CONFIG register */
554 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
555 | CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
556
557 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
558 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
559 | CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
560 msm_cm_dll_set_freq(host);
561
562 /* Write 0 to DLL_RST bit of DLL_CONFIG register */
563 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
564 & ~CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
565
566 /* Write 0 to DLL_PDN bit of DLL_CONFIG register */
567 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
568 & ~CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
569
570 /* Set DLL_EN bit to 1. */
571 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
572 | CORE_DLL_EN), host->ioaddr + CORE_DLL_CONFIG);
573
574 /* Set CK_OUT_EN bit to 1. */
575 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
576 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
577
578 wait_cnt = 50;
579 /* Wait until DLL_LOCK bit of DLL_STATUS register becomes '1' */
580 while (!(readl_relaxed(host->ioaddr + CORE_DLL_STATUS) &
581 CORE_DLL_LOCK)) {
582 /* max. wait for 50us sec for LOCK bit to be set */
583 if (--wait_cnt == 0) {
584 pr_err("%s: %s: DLL failed to LOCK\n",
585 mmc_hostname(mmc), __func__);
586 rc = -ETIMEDOUT;
587 goto out;
588 }
589 /* wait for 1us before polling again */
590 udelay(1);
591 }
592
593out:
Subhash Jadavaniefb5f622013-05-28 18:21:57 +0530594 /* Restore the correct PWRSAVE state */
595 if (prev_pwrsave ^ curr_pwrsave) {
596 u32 reg = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC);
597
598 if (prev_pwrsave)
599 reg |= CORE_CLK_PWRSAVE;
600 else
601 reg &= ~CORE_CLK_PWRSAVE;
602
603 writel_relaxed(reg, host->ioaddr + CORE_VENDOR_SPEC);
604 }
605
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700606 spin_unlock_irqrestore(&host->lock, flags);
607 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
608 return rc;
609}
610
611int sdhci_msm_execute_tuning(struct sdhci_host *host, u32 opcode)
612{
613 unsigned long flags;
Sahitya Tummala714e9642013-06-13 10:36:57 +0530614 int tuning_seq_cnt = 3;
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700615 u8 phase, *data_buf, tuned_phases[16], tuned_phase_cnt = 0;
616 const u32 *tuning_block_pattern = tuning_block_64;
617 int size = sizeof(tuning_block_64); /* Tuning pattern size in bytes */
618 int rc;
619 struct mmc_host *mmc = host->mmc;
Sahitya Tummala00240122013-02-28 19:50:51 +0530620 struct mmc_ios ios = host->mmc->ios;
621
622 /*
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -0700623 * Tuning is required for SDR104, HS200 and HS400 cards and
624 * if clock frequency is greater than 100MHz in these modes.
Sahitya Tummala00240122013-02-28 19:50:51 +0530625 */
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -0700626 if (host->clock <= CORE_FREQ_100MHZ ||
627 !((ios.timing == MMC_TIMING_MMC_HS400) ||
628 (ios.timing == MMC_TIMING_MMC_HS200) ||
629 (ios.timing == MMC_TIMING_UHS_SDR104)))
Sahitya Tummala00240122013-02-28 19:50:51 +0530630 return 0;
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700631
632 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700633 spin_lock_irqsave(&host->lock, flags);
634
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -0700635 if (((opcode == MMC_SEND_TUNING_BLOCK_HS400) ||
636 (opcode == MMC_SEND_TUNING_BLOCK_HS200)) &&
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700637 (mmc->ios.bus_width == MMC_BUS_WIDTH_8)) {
638 tuning_block_pattern = tuning_block_128;
639 size = sizeof(tuning_block_128);
640 }
641 spin_unlock_irqrestore(&host->lock, flags);
642
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700643 data_buf = kmalloc(size, GFP_KERNEL);
644 if (!data_buf) {
645 rc = -ENOMEM;
646 goto out;
647 }
648
Sahitya Tummala714e9642013-06-13 10:36:57 +0530649retry:
650 /* first of all reset the tuning block */
651 rc = msm_init_cm_dll(host);
652 if (rc)
653 goto kfree;
654
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700655 phase = 0;
656 do {
657 struct mmc_command cmd = {0};
658 struct mmc_data data = {0};
659 struct mmc_request mrq = {
660 .cmd = &cmd,
661 .data = &data
662 };
663 struct scatterlist sg;
664
665 /* set the phase in delay line hw block */
666 rc = msm_config_cm_dll_phase(host, phase);
667 if (rc)
668 goto kfree;
669
670 cmd.opcode = opcode;
671 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
672
673 data.blksz = size;
674 data.blocks = 1;
675 data.flags = MMC_DATA_READ;
676 data.timeout_ns = 1000 * 1000 * 1000; /* 1 sec */
677
678 data.sg = &sg;
679 data.sg_len = 1;
680 sg_init_one(&sg, data_buf, size);
681 memset(data_buf, 0, size);
682 mmc_wait_for_req(mmc, &mrq);
683
684 if (!cmd.error && !data.error &&
685 !memcmp(data_buf, tuning_block_pattern, size)) {
686 /* tuning is successful at this tuning point */
687 tuned_phases[tuned_phase_cnt++] = phase;
688 pr_debug("%s: %s: found good phase = %d\n",
689 mmc_hostname(mmc), __func__, phase);
690 }
691 } while (++phase < 16);
692
693 if (tuned_phase_cnt) {
694 rc = msm_find_most_appropriate_phase(host, tuned_phases,
695 tuned_phase_cnt);
696 if (rc < 0)
697 goto kfree;
698 else
699 phase = (u8)rc;
700
701 /*
702 * Finally set the selected phase in delay
703 * line hw block.
704 */
705 rc = msm_config_cm_dll_phase(host, phase);
706 if (rc)
707 goto kfree;
708 pr_debug("%s: %s: finally setting the tuning phase to %d\n",
709 mmc_hostname(mmc), __func__, phase);
710 } else {
Sahitya Tummala714e9642013-06-13 10:36:57 +0530711 if (--tuning_seq_cnt)
712 goto retry;
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700713 /* tuning failed */
714 pr_err("%s: %s: no tuning point found\n",
715 mmc_hostname(mmc), __func__);
Sahitya Tummala714e9642013-06-13 10:36:57 +0530716 rc = -EIO;
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -0700717 }
718
719kfree:
720 kfree(data_buf);
721out:
722 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
723 return rc;
724}
725
Asutosh Das33a4ff52012-12-18 16:14:02 +0530726static int sdhci_msm_setup_gpio(struct sdhci_msm_pltfm_data *pdata, bool enable)
727{
728 struct sdhci_msm_gpio_data *curr;
729 int i, ret = 0;
730
731 curr = pdata->pin_data->gpio_data;
732 for (i = 0; i < curr->size; i++) {
733 if (!gpio_is_valid(curr->gpio[i].no)) {
734 ret = -EINVAL;
735 pr_err("%s: Invalid gpio = %d\n", __func__,
736 curr->gpio[i].no);
737 goto free_gpios;
738 }
739 if (enable) {
740 ret = gpio_request(curr->gpio[i].no,
741 curr->gpio[i].name);
742 if (ret) {
743 pr_err("%s: gpio_request(%d, %s) failed %d\n",
744 __func__, curr->gpio[i].no,
745 curr->gpio[i].name, ret);
746 goto free_gpios;
747 }
748 curr->gpio[i].is_enabled = true;
749 } else {
750 gpio_free(curr->gpio[i].no);
751 curr->gpio[i].is_enabled = false;
752 }
753 }
754 return ret;
755
756free_gpios:
757 for (i--; i >= 0; i--) {
758 gpio_free(curr->gpio[i].no);
759 curr->gpio[i].is_enabled = false;
760 }
761 return ret;
762}
763
Asutosh Das390519d2012-12-21 12:21:42 +0530764static int sdhci_msm_setup_pad(struct sdhci_msm_pltfm_data *pdata, bool enable)
765{
766 struct sdhci_msm_pad_data *curr;
767 int i;
768
769 curr = pdata->pin_data->pad_data;
770 for (i = 0; i < curr->drv->size; i++) {
771 if (enable)
772 msm_tlmm_set_hdrive(curr->drv->on[i].no,
773 curr->drv->on[i].val);
774 else
775 msm_tlmm_set_hdrive(curr->drv->off[i].no,
776 curr->drv->off[i].val);
777 }
778
779 for (i = 0; i < curr->pull->size; i++) {
780 if (enable)
781 msm_tlmm_set_pull(curr->pull->on[i].no,
782 curr->pull->on[i].val);
783 else
784 msm_tlmm_set_pull(curr->pull->off[i].no,
785 curr->pull->off[i].val);
786 }
787
788 return 0;
789}
790
Asutosh Das33a4ff52012-12-18 16:14:02 +0530791static int sdhci_msm_setup_pins(struct sdhci_msm_pltfm_data *pdata, bool enable)
792{
793 int ret = 0;
794
795 if (!pdata->pin_data || (pdata->pin_data->cfg_sts == enable))
796 return 0;
Asutosh Das390519d2012-12-21 12:21:42 +0530797 if (pdata->pin_data->is_gpio)
798 ret = sdhci_msm_setup_gpio(pdata, enable);
799 else
800 ret = sdhci_msm_setup_pad(pdata, enable);
Asutosh Das33a4ff52012-12-18 16:14:02 +0530801
Asutosh Das33a4ff52012-12-18 16:14:02 +0530802 if (!ret)
803 pdata->pin_data->cfg_sts = enable;
804
805 return ret;
806}
807
Asutosh Das390519d2012-12-21 12:21:42 +0530808static int sdhci_msm_dt_get_array(struct device *dev, const char *prop_name,
809 u32 **out, int *len, u32 size)
810{
811 int ret = 0;
812 struct device_node *np = dev->of_node;
813 size_t sz;
814 u32 *arr = NULL;
815
816 if (!of_get_property(np, prop_name, len)) {
817 ret = -EINVAL;
818 goto out;
819 }
820 sz = *len = *len / sizeof(*arr);
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -0700821 if (sz <= 0 || (size > 0 && (sz > size))) {
Asutosh Das390519d2012-12-21 12:21:42 +0530822 dev_err(dev, "%s invalid size\n", prop_name);
823 ret = -EINVAL;
824 goto out;
825 }
826
827 arr = devm_kzalloc(dev, sz * sizeof(*arr), GFP_KERNEL);
828 if (!arr) {
829 dev_err(dev, "%s failed allocating memory\n", prop_name);
830 ret = -ENOMEM;
831 goto out;
832 }
833
834 ret = of_property_read_u32_array(np, prop_name, arr, sz);
835 if (ret < 0) {
836 dev_err(dev, "%s failed reading array %d\n", prop_name, ret);
837 goto out;
838 }
839 *out = arr;
840out:
841 if (ret)
842 *len = 0;
843 return ret;
844}
845
Asutosh Das33a4ff52012-12-18 16:14:02 +0530846#define MAX_PROP_SIZE 32
847static int sdhci_msm_dt_parse_vreg_info(struct device *dev,
848 struct sdhci_msm_reg_data **vreg_data, const char *vreg_name)
849{
850 int len, ret = 0;
851 const __be32 *prop;
852 char prop_name[MAX_PROP_SIZE];
853 struct sdhci_msm_reg_data *vreg;
854 struct device_node *np = dev->of_node;
855
856 snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", vreg_name);
857 if (!of_parse_phandle(np, prop_name, 0)) {
Asutosh Das95afcad2013-06-28 15:03:44 +0530858 dev_info(dev, "No vreg data found for %s\n", vreg_name);
Asutosh Das33a4ff52012-12-18 16:14:02 +0530859 return ret;
860 }
861
862 vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
863 if (!vreg) {
864 dev_err(dev, "No memory for vreg: %s\n", vreg_name);
865 ret = -ENOMEM;
866 return ret;
867 }
868
869 vreg->name = vreg_name;
870
871 snprintf(prop_name, MAX_PROP_SIZE,
872 "qcom,%s-always-on", vreg_name);
873 if (of_get_property(np, prop_name, NULL))
874 vreg->is_always_on = true;
875
876 snprintf(prop_name, MAX_PROP_SIZE,
877 "qcom,%s-lpm-sup", vreg_name);
878 if (of_get_property(np, prop_name, NULL))
879 vreg->lpm_sup = true;
880
881 snprintf(prop_name, MAX_PROP_SIZE,
882 "qcom,%s-voltage-level", vreg_name);
883 prop = of_get_property(np, prop_name, &len);
884 if (!prop || (len != (2 * sizeof(__be32)))) {
885 dev_warn(dev, "%s %s property\n",
886 prop ? "invalid format" : "no", prop_name);
887 } else {
888 vreg->low_vol_level = be32_to_cpup(&prop[0]);
889 vreg->high_vol_level = be32_to_cpup(&prop[1]);
890 }
891
892 snprintf(prop_name, MAX_PROP_SIZE,
893 "qcom,%s-current-level", vreg_name);
894 prop = of_get_property(np, prop_name, &len);
895 if (!prop || (len != (2 * sizeof(__be32)))) {
896 dev_warn(dev, "%s %s property\n",
897 prop ? "invalid format" : "no", prop_name);
898 } else {
899 vreg->lpm_uA = be32_to_cpup(&prop[0]);
900 vreg->hpm_uA = be32_to_cpup(&prop[1]);
901 }
902
903 *vreg_data = vreg;
904 dev_dbg(dev, "%s: %s %s vol=[%d %d]uV, curr=[%d %d]uA\n",
905 vreg->name, vreg->is_always_on ? "always_on," : "",
906 vreg->lpm_sup ? "lpm_sup," : "", vreg->low_vol_level,
907 vreg->high_vol_level, vreg->lpm_uA, vreg->hpm_uA);
908
909 return ret;
910}
911
Asutosh Das390519d2012-12-21 12:21:42 +0530912/* GPIO/Pad data extraction */
913static int sdhci_msm_dt_get_pad_pull_info(struct device *dev, int id,
914 struct sdhci_msm_pad_pull_data **pad_pull_data)
915{
916 int ret = 0, base = 0, len, i;
917 u32 *tmp;
918 struct sdhci_msm_pad_pull_data *pull_data;
919 struct sdhci_msm_pad_pull *pull;
920
921 switch (id) {
922 case 1:
923 base = TLMM_PULL_SDC1_CLK;
924 break;
925 case 2:
926 base = TLMM_PULL_SDC2_CLK;
927 break;
928 case 3:
929 base = TLMM_PULL_SDC3_CLK;
930 break;
931 case 4:
932 base = TLMM_PULL_SDC4_CLK;
933 break;
934 default:
935 dev_err(dev, "%s: Invalid slot id\n", __func__);
936 ret = -EINVAL;
937 goto out;
938 }
939
940 pull_data = devm_kzalloc(dev, sizeof(struct sdhci_msm_pad_pull_data),
941 GFP_KERNEL);
942 if (!pull_data) {
943 dev_err(dev, "No memory for msm_mmc_pad_pull_data\n");
944 ret = -ENOMEM;
945 goto out;
946 }
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -0700947 pull_data->size = 4; /* array size for clk, cmd, data and rclk */
Asutosh Das390519d2012-12-21 12:21:42 +0530948
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -0700949 /* Allocate on, off configs for clk, cmd, data and rclk */
Asutosh Das390519d2012-12-21 12:21:42 +0530950 pull = devm_kzalloc(dev, 2 * pull_data->size *\
951 sizeof(struct sdhci_msm_pad_pull), GFP_KERNEL);
952 if (!pull) {
953 dev_err(dev, "No memory for msm_mmc_pad_pull\n");
954 ret = -ENOMEM;
955 goto out;
956 }
957 pull_data->on = pull;
958 pull_data->off = pull + pull_data->size;
959
960 ret = sdhci_msm_dt_get_array(dev, "qcom,pad-pull-on",
961 &tmp, &len, pull_data->size);
962 if (ret)
963 goto out;
964
965 for (i = 0; i < len; i++) {
966 pull_data->on[i].no = base + i;
967 pull_data->on[i].val = tmp[i];
968 dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
969 i, pull_data->on[i].val);
970 }
971
972 ret = sdhci_msm_dt_get_array(dev, "qcom,pad-pull-off",
973 &tmp, &len, pull_data->size);
974 if (ret)
975 goto out;
976
977 for (i = 0; i < len; i++) {
978 pull_data->off[i].no = base + i;
979 pull_data->off[i].val = tmp[i];
980 dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
981 i, pull_data->off[i].val);
982 }
983
984 *pad_pull_data = pull_data;
985out:
986 return ret;
987}
988
989static int sdhci_msm_dt_get_pad_drv_info(struct device *dev, int id,
990 struct sdhci_msm_pad_drv_data **pad_drv_data)
991{
992 int ret = 0, base = 0, len, i;
993 u32 *tmp;
994 struct sdhci_msm_pad_drv_data *drv_data;
995 struct sdhci_msm_pad_drv *drv;
996
997 switch (id) {
998 case 1:
999 base = TLMM_HDRV_SDC1_CLK;
1000 break;
1001 case 2:
1002 base = TLMM_HDRV_SDC2_CLK;
1003 break;
1004 case 3:
1005 base = TLMM_HDRV_SDC3_CLK;
1006 break;
1007 case 4:
1008 base = TLMM_HDRV_SDC4_CLK;
1009 break;
1010 default:
1011 dev_err(dev, "%s: Invalid slot id\n", __func__);
1012 ret = -EINVAL;
1013 goto out;
1014 }
1015
1016 drv_data = devm_kzalloc(dev, sizeof(struct sdhci_msm_pad_drv_data),
1017 GFP_KERNEL);
1018 if (!drv_data) {
1019 dev_err(dev, "No memory for msm_mmc_pad_drv_data\n");
1020 ret = -ENOMEM;
1021 goto out;
1022 }
1023 drv_data->size = 3; /* array size for clk, cmd, data */
1024
1025 /* Allocate on, off configs for clk, cmd, data */
1026 drv = devm_kzalloc(dev, 2 * drv_data->size *\
1027 sizeof(struct sdhci_msm_pad_drv), GFP_KERNEL);
1028 if (!drv) {
1029 dev_err(dev, "No memory msm_mmc_pad_drv\n");
1030 ret = -ENOMEM;
1031 goto out;
1032 }
1033 drv_data->on = drv;
1034 drv_data->off = drv + drv_data->size;
1035
1036 ret = sdhci_msm_dt_get_array(dev, "qcom,pad-drv-on",
1037 &tmp, &len, drv_data->size);
1038 if (ret)
1039 goto out;
1040
1041 for (i = 0; i < len; i++) {
1042 drv_data->on[i].no = base + i;
1043 drv_data->on[i].val = tmp[i];
1044 dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
1045 i, drv_data->on[i].val);
1046 }
1047
1048 ret = sdhci_msm_dt_get_array(dev, "qcom,pad-drv-off",
1049 &tmp, &len, drv_data->size);
1050 if (ret)
1051 goto out;
1052
1053 for (i = 0; i < len; i++) {
1054 drv_data->off[i].no = base + i;
1055 drv_data->off[i].val = tmp[i];
1056 dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
1057 i, drv_data->off[i].val);
1058 }
1059
1060 *pad_drv_data = drv_data;
1061out:
1062 return ret;
1063}
1064
Asutosh Das33a4ff52012-12-18 16:14:02 +05301065#define GPIO_NAME_MAX_LEN 32
1066static int sdhci_msm_dt_parse_gpio_info(struct device *dev,
1067 struct sdhci_msm_pltfm_data *pdata)
1068{
Asutosh Das390519d2012-12-21 12:21:42 +05301069 int ret = 0, id = 0, cnt, i;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301070 struct sdhci_msm_pin_data *pin_data;
1071 struct device_node *np = dev->of_node;
1072
1073 pin_data = devm_kzalloc(dev, sizeof(*pin_data), GFP_KERNEL);
1074 if (!pin_data) {
1075 dev_err(dev, "No memory for pin_data\n");
1076 ret = -ENOMEM;
1077 goto out;
1078 }
1079
1080 cnt = of_gpio_count(np);
1081 if (cnt > 0) {
Asutosh Das390519d2012-12-21 12:21:42 +05301082 pin_data->is_gpio = true;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301083 pin_data->gpio_data = devm_kzalloc(dev,
1084 sizeof(struct sdhci_msm_gpio_data), GFP_KERNEL);
1085 if (!pin_data->gpio_data) {
1086 dev_err(dev, "No memory for gpio_data\n");
1087 ret = -ENOMEM;
1088 goto out;
1089 }
1090 pin_data->gpio_data->size = cnt;
1091 pin_data->gpio_data->gpio = devm_kzalloc(dev, cnt *
1092 sizeof(struct sdhci_msm_gpio), GFP_KERNEL);
1093
1094 if (!pin_data->gpio_data->gpio) {
1095 dev_err(dev, "No memory for gpio\n");
1096 ret = -ENOMEM;
1097 goto out;
1098 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301099 for (i = 0; i < cnt; i++) {
1100 const char *name = NULL;
1101 char result[GPIO_NAME_MAX_LEN];
1102 pin_data->gpio_data->gpio[i].no = of_get_gpio(np, i);
1103 of_property_read_string_index(np,
1104 "qcom,gpio-names", i, &name);
1105
1106 snprintf(result, GPIO_NAME_MAX_LEN, "%s-%s",
1107 dev_name(dev), name ? name : "?");
1108 pin_data->gpio_data->gpio[i].name = result;
1109 dev_dbg(dev, "%s: gpio[%s] = %d\n", __func__,
Asutosh Das390519d2012-12-21 12:21:42 +05301110 pin_data->gpio_data->gpio[i].name,
1111 pin_data->gpio_data->gpio[i].no);
Asutosh Das33a4ff52012-12-18 16:14:02 +05301112 }
Asutosh Das390519d2012-12-21 12:21:42 +05301113 } else {
1114 pin_data->pad_data =
1115 devm_kzalloc(dev,
1116 sizeof(struct sdhci_msm_pad_data),
1117 GFP_KERNEL);
1118 if (!pin_data->pad_data) {
1119 dev_err(dev,
1120 "No memory for pin_data->pad_data\n");
1121 ret = -ENOMEM;
1122 goto out;
1123 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301124
Asutosh Das390519d2012-12-21 12:21:42 +05301125 ret = of_alias_get_id(np, "sdhc");
1126 if (ret < 0) {
1127 dev_err(dev, "Failed to get slot index %d\n", ret);
1128 goto out;
1129 }
1130 id = ret;
1131
1132 ret = sdhci_msm_dt_get_pad_pull_info(
1133 dev, id, &pin_data->pad_data->pull);
1134 if (ret)
1135 goto out;
1136 ret = sdhci_msm_dt_get_pad_drv_info(
1137 dev, id, &pin_data->pad_data->drv);
1138 if (ret)
1139 goto out;
1140
1141 }
1142 pdata->pin_data = pin_data;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301143out:
1144 if (ret)
1145 dev_err(dev, "%s failed with err %d\n", __func__, ret);
1146 return ret;
1147}
1148
1149/* Parse platform data */
1150static struct sdhci_msm_pltfm_data *sdhci_msm_populate_pdata(struct device *dev)
1151{
1152 struct sdhci_msm_pltfm_data *pdata = NULL;
1153 struct device_node *np = dev->of_node;
1154 u32 bus_width = 0;
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301155 u32 cpu_dma_latency;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301156 int len, i;
Sahitya Tummala00240122013-02-28 19:50:51 +05301157 int clk_table_len;
1158 u32 *clk_table = NULL;
Sujit Reddy Thumma4ddff322013-06-03 09:54:32 +05301159 enum of_gpio_flags flags = OF_GPIO_ACTIVE_LOW;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301160
1161 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1162 if (!pdata) {
1163 dev_err(dev, "failed to allocate memory for platform data\n");
1164 goto out;
1165 }
1166
Sujit Reddy Thumma4ddff322013-06-03 09:54:32 +05301167 pdata->status_gpio = of_get_named_gpio_flags(np, "cd-gpios", 0, &flags);
1168 if (gpio_is_valid(pdata->status_gpio) & !(flags & OF_GPIO_ACTIVE_LOW))
1169 pdata->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
Sahitya Tummala62448d92013-03-12 14:57:46 +05301170
Asutosh Das33a4ff52012-12-18 16:14:02 +05301171 of_property_read_u32(np, "qcom,bus-width", &bus_width);
1172 if (bus_width == 8)
1173 pdata->mmc_bus_width = MMC_CAP_8_BIT_DATA;
1174 else if (bus_width == 4)
1175 pdata->mmc_bus_width = MMC_CAP_4_BIT_DATA;
1176 else {
1177 dev_notice(dev, "invalid bus-width, default to 1-bit mode\n");
1178 pdata->mmc_bus_width = 0;
1179 }
1180
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301181 if (!of_property_read_u32(np, "qcom,cpu-dma-latency-us",
1182 &cpu_dma_latency))
1183 pdata->cpu_dma_latency_us = cpu_dma_latency;
1184
Sahitya Tummala00240122013-02-28 19:50:51 +05301185 if (sdhci_msm_dt_get_array(dev, "qcom,clk-rates",
1186 &clk_table, &clk_table_len, 0)) {
1187 dev_err(dev, "failed parsing supported clock rates\n");
1188 goto out;
1189 }
1190 if (!clk_table || !clk_table_len) {
1191 dev_err(dev, "Invalid clock table\n");
1192 goto out;
1193 }
1194 pdata->sup_clk_table = clk_table;
1195 pdata->sup_clk_cnt = clk_table_len;
1196
Asutosh Das33a4ff52012-12-18 16:14:02 +05301197 pdata->vreg_data = devm_kzalloc(dev, sizeof(struct
1198 sdhci_msm_slot_reg_data),
1199 GFP_KERNEL);
1200 if (!pdata->vreg_data) {
1201 dev_err(dev, "failed to allocate memory for vreg data\n");
1202 goto out;
1203 }
1204
1205 if (sdhci_msm_dt_parse_vreg_info(dev, &pdata->vreg_data->vdd_data,
1206 "vdd")) {
1207 dev_err(dev, "failed parsing vdd data\n");
1208 goto out;
1209 }
1210 if (sdhci_msm_dt_parse_vreg_info(dev,
1211 &pdata->vreg_data->vdd_io_data,
1212 "vdd-io")) {
1213 dev_err(dev, "failed parsing vdd-io data\n");
1214 goto out;
1215 }
1216
1217 if (sdhci_msm_dt_parse_gpio_info(dev, pdata)) {
1218 dev_err(dev, "failed parsing gpio data\n");
1219 goto out;
1220 }
1221
Asutosh Das33a4ff52012-12-18 16:14:02 +05301222 len = of_property_count_strings(np, "qcom,bus-speed-mode");
1223
1224 for (i = 0; i < len; i++) {
1225 const char *name = NULL;
1226
1227 of_property_read_string_index(np,
1228 "qcom,bus-speed-mode", i, &name);
1229 if (!name)
1230 continue;
1231
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -07001232 if (!strncmp(name, "HS400_1p8v", sizeof("HS400_1p8v")))
1233 pdata->caps2 |= MMC_CAP2_HS400_1_8V;
1234 else if (!strncmp(name, "HS400_1p2v", sizeof("HS400_1p2v")))
1235 pdata->caps2 |= MMC_CAP2_HS400_1_2V;
1236 else if (!strncmp(name, "HS200_1p8v", sizeof("HS200_1p8v")))
Asutosh Das33a4ff52012-12-18 16:14:02 +05301237 pdata->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
1238 else if (!strncmp(name, "HS200_1p2v", sizeof("HS200_1p2v")))
1239 pdata->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
1240 else if (!strncmp(name, "DDR_1p8v", sizeof("DDR_1p8v")))
1241 pdata->caps |= MMC_CAP_1_8V_DDR
1242 | MMC_CAP_UHS_DDR50;
1243 else if (!strncmp(name, "DDR_1p2v", sizeof("DDR_1p2v")))
1244 pdata->caps |= MMC_CAP_1_2V_DDR
1245 | MMC_CAP_UHS_DDR50;
1246 }
1247
1248 if (of_get_property(np, "qcom,nonremovable", NULL))
1249 pdata->nonremovable = true;
1250
1251 return pdata;
1252out:
1253 return NULL;
1254}
1255
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301256/* Returns required bandwidth in Bytes per Sec */
1257static unsigned int sdhci_get_bw_required(struct sdhci_host *host,
1258 struct mmc_ios *ios)
1259{
Sahitya Tummala53aff982013-04-03 18:03:31 +05301260 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1261 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1262
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301263 unsigned int bw;
1264
Sahitya Tummala53aff982013-04-03 18:03:31 +05301265 bw = msm_host->clk_rate;
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301266 /*
1267 * For DDR mode, SDCC controller clock will be at
1268 * the double rate than the actual clock that goes to card.
1269 */
1270 if (ios->bus_width == MMC_BUS_WIDTH_4)
1271 bw /= 2;
1272 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1273 bw /= 8;
1274
1275 return bw;
1276}
1277
1278static int sdhci_msm_bus_get_vote_for_bw(struct sdhci_msm_host *host,
1279 unsigned int bw)
1280{
1281 unsigned int *table = host->pdata->voting_data->bw_vecs;
1282 unsigned int size = host->pdata->voting_data->bw_vecs_size;
1283 int i;
1284
1285 if (host->msm_bus_vote.is_max_bw_needed && bw)
1286 return host->msm_bus_vote.max_bw_vote;
1287
1288 for (i = 0; i < size; i++) {
1289 if (bw <= table[i])
1290 break;
1291 }
1292
1293 if (i && (i == size))
1294 i--;
1295
1296 return i;
1297}
1298
1299/*
1300 * This function must be called with host lock acquired.
1301 * Caller of this function should also ensure that msm bus client
1302 * handle is not null.
1303 */
1304static inline int sdhci_msm_bus_set_vote(struct sdhci_msm_host *msm_host,
1305 int vote,
1306 unsigned long flags)
1307{
1308 struct sdhci_host *host = platform_get_drvdata(msm_host->pdev);
1309 int rc = 0;
1310
1311 if (vote != msm_host->msm_bus_vote.curr_vote) {
1312 spin_unlock_irqrestore(&host->lock, flags);
1313 rc = msm_bus_scale_client_update_request(
1314 msm_host->msm_bus_vote.client_handle, vote);
1315 spin_lock_irqsave(&host->lock, flags);
1316 if (rc) {
1317 pr_err("%s: msm_bus_scale_client_update_request() failed: bus_client_handle=0x%x, vote=%d, err=%d\n",
1318 mmc_hostname(host->mmc),
1319 msm_host->msm_bus_vote.client_handle, vote, rc);
1320 goto out;
1321 }
1322 msm_host->msm_bus_vote.curr_vote = vote;
1323 }
1324out:
1325 return rc;
1326}
1327
1328/*
1329 * Internal work. Work to set 0 bandwidth for msm bus.
1330 */
1331static void sdhci_msm_bus_work(struct work_struct *work)
1332{
1333 struct sdhci_msm_host *msm_host;
1334 struct sdhci_host *host;
1335 unsigned long flags;
1336
1337 msm_host = container_of(work, struct sdhci_msm_host,
1338 msm_bus_vote.vote_work.work);
1339 host = platform_get_drvdata(msm_host->pdev);
1340
1341 if (!msm_host->msm_bus_vote.client_handle)
1342 return;
1343
1344 spin_lock_irqsave(&host->lock, flags);
1345 /* don't vote for 0 bandwidth if any request is in progress */
1346 if (!host->mrq) {
1347 sdhci_msm_bus_set_vote(msm_host,
1348 msm_host->msm_bus_vote.min_bw_vote, flags);
1349 } else
1350 pr_warning("%s: %s: Transfer in progress. skipping bus voting to 0 bandwidth\n",
1351 mmc_hostname(host->mmc), __func__);
1352 spin_unlock_irqrestore(&host->lock, flags);
1353}
1354
1355/*
1356 * This function cancels any scheduled delayed work and sets the bus
1357 * vote based on bw (bandwidth) argument.
1358 */
1359static void sdhci_msm_bus_cancel_work_and_set_vote(struct sdhci_host *host,
1360 unsigned int bw)
1361{
1362 int vote;
1363 unsigned long flags;
1364 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1365 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1366
1367 cancel_delayed_work_sync(&msm_host->msm_bus_vote.vote_work);
1368 spin_lock_irqsave(&host->lock, flags);
1369 vote = sdhci_msm_bus_get_vote_for_bw(msm_host, bw);
1370 sdhci_msm_bus_set_vote(msm_host, vote, flags);
1371 spin_unlock_irqrestore(&host->lock, flags);
1372}
1373
1374#define MSM_MMC_BUS_VOTING_DELAY 200 /* msecs */
1375
1376/* This function queues a work which will set the bandwidth requiement to 0 */
1377static void sdhci_msm_bus_queue_work(struct sdhci_host *host)
1378{
1379 unsigned long flags;
1380 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1381 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1382
1383 spin_lock_irqsave(&host->lock, flags);
1384 if (msm_host->msm_bus_vote.min_bw_vote !=
1385 msm_host->msm_bus_vote.curr_vote)
1386 queue_delayed_work(system_nrt_wq,
1387 &msm_host->msm_bus_vote.vote_work,
1388 msecs_to_jiffies(MSM_MMC_BUS_VOTING_DELAY));
1389 spin_unlock_irqrestore(&host->lock, flags);
1390}
1391
1392static int sdhci_msm_bus_register(struct sdhci_msm_host *host,
1393 struct platform_device *pdev)
1394{
1395 int rc = 0;
1396 struct msm_bus_scale_pdata *bus_pdata;
1397
1398 struct sdhci_msm_bus_voting_data *data;
1399 struct device *dev = &pdev->dev;
1400
1401 data = devm_kzalloc(dev,
1402 sizeof(struct sdhci_msm_bus_voting_data), GFP_KERNEL);
1403 if (!data) {
1404 dev_err(&pdev->dev,
1405 "%s: failed to allocate memory\n", __func__);
1406 rc = -ENOMEM;
1407 goto out;
1408 }
1409 data->bus_pdata = msm_bus_cl_get_pdata(pdev);
1410 if (data->bus_pdata) {
1411 rc = sdhci_msm_dt_get_array(dev, "qcom,bus-bw-vectors-bps",
1412 &data->bw_vecs, &data->bw_vecs_size, 0);
1413 if (rc) {
1414 dev_err(&pdev->dev,
1415 "%s: Failed to get bus-bw-vectors-bps\n",
1416 __func__);
1417 goto out;
1418 }
1419 host->pdata->voting_data = data;
1420 }
1421 if (host->pdata->voting_data &&
1422 host->pdata->voting_data->bus_pdata &&
1423 host->pdata->voting_data->bw_vecs &&
1424 host->pdata->voting_data->bw_vecs_size) {
1425
1426 bus_pdata = host->pdata->voting_data->bus_pdata;
1427 host->msm_bus_vote.client_handle =
1428 msm_bus_scale_register_client(bus_pdata);
1429 if (!host->msm_bus_vote.client_handle) {
1430 dev_err(&pdev->dev, "msm_bus_scale_register_client()\n");
1431 rc = -EFAULT;
1432 goto out;
1433 }
1434 /* cache the vote index for minimum and maximum bandwidth */
1435 host->msm_bus_vote.min_bw_vote =
1436 sdhci_msm_bus_get_vote_for_bw(host, 0);
1437 host->msm_bus_vote.max_bw_vote =
1438 sdhci_msm_bus_get_vote_for_bw(host, UINT_MAX);
1439 } else {
1440 devm_kfree(dev, data);
1441 }
1442
1443out:
1444 return rc;
1445}
1446
1447static void sdhci_msm_bus_unregister(struct sdhci_msm_host *host)
1448{
1449 if (host->msm_bus_vote.client_handle)
1450 msm_bus_scale_unregister_client(
1451 host->msm_bus_vote.client_handle);
1452}
1453
1454static void sdhci_msm_bus_voting(struct sdhci_host *host, u32 enable)
1455{
1456 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1457 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1458 struct mmc_ios *ios = &host->mmc->ios;
1459 unsigned int bw;
1460
1461 if (!msm_host->msm_bus_vote.client_handle)
1462 return;
1463
1464 bw = sdhci_get_bw_required(host, ios);
Sahitya Tummalac45ae732013-05-23 15:59:22 +05301465 if (enable) {
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301466 sdhci_msm_bus_cancel_work_and_set_vote(host, bw);
Sahitya Tummalac45ae732013-05-23 15:59:22 +05301467 } else {
1468 /*
1469 * If clock gating is enabled, then remove the vote
1470 * immediately because clocks will be disabled only
1471 * after SDHCI_MSM_MMC_CLK_GATE_DELAY and thus no
1472 * additional delay is required to remove the bus vote.
1473 */
1474 if (host->mmc->clkgate_delay)
1475 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
1476 else
1477 sdhci_msm_bus_queue_work(host);
1478 }
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301479}
1480
Asutosh Das33a4ff52012-12-18 16:14:02 +05301481/* Regulator utility functions */
1482static int sdhci_msm_vreg_init_reg(struct device *dev,
1483 struct sdhci_msm_reg_data *vreg)
1484{
1485 int ret = 0;
1486
1487 /* check if regulator is already initialized? */
1488 if (vreg->reg)
1489 goto out;
1490
1491 /* Get the regulator handle */
1492 vreg->reg = devm_regulator_get(dev, vreg->name);
1493 if (IS_ERR(vreg->reg)) {
1494 ret = PTR_ERR(vreg->reg);
1495 pr_err("%s: devm_regulator_get(%s) failed. ret=%d\n",
1496 __func__, vreg->name, ret);
1497 goto out;
1498 }
1499
Asutosh Das95afcad2013-06-28 15:03:44 +05301500 if (regulator_count_voltages(vreg->reg) > 0) {
1501 vreg->set_voltage_sup = true;
1502 /* sanity check */
1503 if (!vreg->high_vol_level || !vreg->hpm_uA) {
1504 pr_err("%s: %s invalid constraints specified\n",
1505 __func__, vreg->name);
1506 ret = -EINVAL;
1507 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301508 }
1509
1510out:
1511 return ret;
1512}
1513
1514static void sdhci_msm_vreg_deinit_reg(struct sdhci_msm_reg_data *vreg)
1515{
1516 if (vreg->reg)
1517 devm_regulator_put(vreg->reg);
1518}
1519
1520static int sdhci_msm_vreg_set_optimum_mode(struct sdhci_msm_reg_data
1521 *vreg, int uA_load)
1522{
1523 int ret = 0;
1524
1525 /*
1526 * regulators that do not support regulator_set_voltage also
1527 * do not support regulator_set_optimum_mode
1528 */
Asutosh Das95afcad2013-06-28 15:03:44 +05301529 if (vreg->set_voltage_sup) {
1530 ret = regulator_set_optimum_mode(vreg->reg, uA_load);
1531 if (ret < 0)
1532 pr_err("%s: regulator_set_optimum_mode(reg=%s,uA_load=%d) failed. ret=%d\n",
Asutosh Das33a4ff52012-12-18 16:14:02 +05301533 __func__, vreg->name, uA_load, ret);
1534 else
1535 /*
1536 * regulator_set_optimum_mode() can return non zero
1537 * value even for success case.
1538 */
1539 ret = 0;
Asutosh Das95afcad2013-06-28 15:03:44 +05301540 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301541 return ret;
1542}
1543
1544static int sdhci_msm_vreg_set_voltage(struct sdhci_msm_reg_data *vreg,
1545 int min_uV, int max_uV)
1546{
1547 int ret = 0;
Asutosh Das95afcad2013-06-28 15:03:44 +05301548 if (vreg->set_voltage_sup) {
1549 ret = regulator_set_voltage(vreg->reg, min_uV, max_uV);
1550 if (ret) {
1551 pr_err("%s: regulator_set_voltage(%s)failed. min_uV=%d,max_uV=%d,ret=%d\n",
Asutosh Das33a4ff52012-12-18 16:14:02 +05301552 __func__, vreg->name, min_uV, max_uV, ret);
1553 }
Asutosh Das95afcad2013-06-28 15:03:44 +05301554 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301555
1556 return ret;
1557}
1558
1559static int sdhci_msm_vreg_enable(struct sdhci_msm_reg_data *vreg)
1560{
1561 int ret = 0;
1562
1563 /* Put regulator in HPM (high power mode) */
1564 ret = sdhci_msm_vreg_set_optimum_mode(vreg, vreg->hpm_uA);
1565 if (ret < 0)
1566 return ret;
1567
1568 if (!vreg->is_enabled) {
1569 /* Set voltage level */
1570 ret = sdhci_msm_vreg_set_voltage(vreg, vreg->high_vol_level,
1571 vreg->high_vol_level);
1572 if (ret)
1573 return ret;
1574 }
1575 ret = regulator_enable(vreg->reg);
1576 if (ret) {
1577 pr_err("%s: regulator_enable(%s) failed. ret=%d\n",
1578 __func__, vreg->name, ret);
1579 return ret;
1580 }
1581 vreg->is_enabled = true;
1582 return ret;
1583}
1584
1585static int sdhci_msm_vreg_disable(struct sdhci_msm_reg_data *vreg)
1586{
1587 int ret = 0;
1588
1589 /* Never disable regulator marked as always_on */
1590 if (vreg->is_enabled && !vreg->is_always_on) {
1591 ret = regulator_disable(vreg->reg);
1592 if (ret) {
1593 pr_err("%s: regulator_disable(%s) failed. ret=%d\n",
1594 __func__, vreg->name, ret);
1595 goto out;
1596 }
1597 vreg->is_enabled = false;
1598
1599 ret = sdhci_msm_vreg_set_optimum_mode(vreg, 0);
1600 if (ret < 0)
1601 goto out;
1602
1603 /* Set min. voltage level to 0 */
1604 ret = sdhci_msm_vreg_set_voltage(vreg, 0, vreg->high_vol_level);
1605 if (ret)
1606 goto out;
1607 } else if (vreg->is_enabled && vreg->is_always_on) {
1608 if (vreg->lpm_sup) {
1609 /* Put always_on regulator in LPM (low power mode) */
1610 ret = sdhci_msm_vreg_set_optimum_mode(vreg,
1611 vreg->lpm_uA);
1612 if (ret < 0)
1613 goto out;
1614 }
1615 }
1616out:
1617 return ret;
1618}
1619
1620static int sdhci_msm_setup_vreg(struct sdhci_msm_pltfm_data *pdata,
1621 bool enable, bool is_init)
1622{
1623 int ret = 0, i;
1624 struct sdhci_msm_slot_reg_data *curr_slot;
1625 struct sdhci_msm_reg_data *vreg_table[2];
1626
1627 curr_slot = pdata->vreg_data;
1628 if (!curr_slot) {
1629 pr_debug("%s: vreg info unavailable,assuming the slot is powered by always on domain\n",
1630 __func__);
1631 goto out;
1632 }
1633
1634 vreg_table[0] = curr_slot->vdd_data;
1635 vreg_table[1] = curr_slot->vdd_io_data;
1636
1637 for (i = 0; i < ARRAY_SIZE(vreg_table); i++) {
1638 if (vreg_table[i]) {
1639 if (enable)
1640 ret = sdhci_msm_vreg_enable(vreg_table[i]);
1641 else
1642 ret = sdhci_msm_vreg_disable(vreg_table[i]);
1643 if (ret)
1644 goto out;
1645 }
1646 }
1647out:
1648 return ret;
1649}
1650
1651/*
1652 * Reset vreg by ensuring it is off during probe. A call
1653 * to enable vreg is needed to balance disable vreg
1654 */
1655static int sdhci_msm_vreg_reset(struct sdhci_msm_pltfm_data *pdata)
1656{
1657 int ret;
1658
1659 ret = sdhci_msm_setup_vreg(pdata, 1, true);
1660 if (ret)
1661 return ret;
1662 ret = sdhci_msm_setup_vreg(pdata, 0, true);
1663 return ret;
1664}
1665
1666/* This init function should be called only once for each SDHC slot */
1667static int sdhci_msm_vreg_init(struct device *dev,
1668 struct sdhci_msm_pltfm_data *pdata,
1669 bool is_init)
1670{
1671 int ret = 0;
1672 struct sdhci_msm_slot_reg_data *curr_slot;
1673 struct sdhci_msm_reg_data *curr_vdd_reg, *curr_vdd_io_reg;
1674
1675 curr_slot = pdata->vreg_data;
1676 if (!curr_slot)
1677 goto out;
1678
1679 curr_vdd_reg = curr_slot->vdd_data;
1680 curr_vdd_io_reg = curr_slot->vdd_io_data;
1681
1682 if (!is_init)
1683 /* Deregister all regulators from regulator framework */
1684 goto vdd_io_reg_deinit;
1685
1686 /*
1687 * Get the regulator handle from voltage regulator framework
1688 * and then try to set the voltage level for the regulator
1689 */
1690 if (curr_vdd_reg) {
1691 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_reg);
1692 if (ret)
1693 goto out;
1694 }
1695 if (curr_vdd_io_reg) {
1696 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_io_reg);
1697 if (ret)
1698 goto vdd_reg_deinit;
1699 }
1700 ret = sdhci_msm_vreg_reset(pdata);
1701 if (ret)
1702 dev_err(dev, "vreg reset failed (%d)\n", ret);
1703 goto out;
1704
1705vdd_io_reg_deinit:
1706 if (curr_vdd_io_reg)
1707 sdhci_msm_vreg_deinit_reg(curr_vdd_io_reg);
1708vdd_reg_deinit:
1709 if (curr_vdd_reg)
1710 sdhci_msm_vreg_deinit_reg(curr_vdd_reg);
1711out:
1712 return ret;
1713}
1714
1715
1716static int sdhci_msm_set_vdd_io_vol(struct sdhci_msm_pltfm_data *pdata,
1717 enum vdd_io_level level,
1718 unsigned int voltage_level)
1719{
1720 int ret = 0;
1721 int set_level;
1722 struct sdhci_msm_reg_data *vdd_io_reg;
1723
1724 if (!pdata->vreg_data)
1725 return ret;
1726
1727 vdd_io_reg = pdata->vreg_data->vdd_io_data;
1728 if (vdd_io_reg && vdd_io_reg->is_enabled) {
1729 switch (level) {
1730 case VDD_IO_LOW:
1731 set_level = vdd_io_reg->low_vol_level;
1732 break;
1733 case VDD_IO_HIGH:
1734 set_level = vdd_io_reg->high_vol_level;
1735 break;
1736 case VDD_IO_SET_LEVEL:
1737 set_level = voltage_level;
1738 break;
1739 default:
1740 pr_err("%s: invalid argument level = %d",
1741 __func__, level);
1742 ret = -EINVAL;
1743 return ret;
1744 }
1745 ret = sdhci_msm_vreg_set_voltage(vdd_io_reg, set_level,
1746 set_level);
1747 }
1748 return ret;
1749}
1750
1751static irqreturn_t sdhci_msm_pwr_irq(int irq, void *data)
1752{
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07001753 struct sdhci_host *host = (struct sdhci_host *)data;
1754 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1755 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301756 u8 irq_status = 0;
1757 u8 irq_ack = 0;
1758 int ret = 0;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301759 int pwr_state = 0, io_level = 0;
1760 unsigned long flags;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301761
1762 irq_status = readb_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
1763 pr_debug("%s: Received IRQ(%d), status=0x%x\n",
1764 mmc_hostname(msm_host->mmc), irq, irq_status);
1765
1766 /* Clear the interrupt */
1767 writeb_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
1768 /*
1769 * SDHC has core_mem and hc_mem device memory and these memory
1770 * addresses do not fall within 1KB region. Hence, any update to
1771 * core_mem address space would require an mb() to ensure this gets
1772 * completed before its next update to registers within hc_mem.
1773 */
1774 mb();
1775
1776 /* Handle BUS ON/OFF*/
1777 if (irq_status & CORE_PWRCTL_BUS_ON) {
1778 ret = sdhci_msm_setup_vreg(msm_host->pdata, true, false);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301779 if (!ret) {
Asutosh Das33a4ff52012-12-18 16:14:02 +05301780 ret = sdhci_msm_setup_pins(msm_host->pdata, true);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301781 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
1782 VDD_IO_HIGH, 0);
1783 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301784 if (ret)
1785 irq_ack |= CORE_PWRCTL_BUS_FAIL;
1786 else
1787 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301788
1789 pwr_state = REQ_BUS_ON;
1790 io_level = REQ_IO_HIGH;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301791 }
1792 if (irq_status & CORE_PWRCTL_BUS_OFF) {
1793 ret = sdhci_msm_setup_vreg(msm_host->pdata, false, false);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301794 if (!ret) {
Asutosh Das33a4ff52012-12-18 16:14:02 +05301795 ret = sdhci_msm_setup_pins(msm_host->pdata, false);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301796 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
1797 VDD_IO_LOW, 0);
1798 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05301799 if (ret)
1800 irq_ack |= CORE_PWRCTL_BUS_FAIL;
1801 else
1802 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301803
1804 pwr_state = REQ_BUS_OFF;
1805 io_level = REQ_IO_LOW;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301806 }
1807 /* Handle IO LOW/HIGH */
1808 if (irq_status & CORE_PWRCTL_IO_LOW) {
1809 /* Switch voltage Low */
1810 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_LOW, 0);
1811 if (ret)
1812 irq_ack |= CORE_PWRCTL_IO_FAIL;
1813 else
1814 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301815
1816 io_level = REQ_IO_LOW;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301817 }
1818 if (irq_status & CORE_PWRCTL_IO_HIGH) {
1819 /* Switch voltage High */
1820 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_HIGH, 0);
1821 if (ret)
1822 irq_ack |= CORE_PWRCTL_IO_FAIL;
1823 else
1824 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301825
1826 io_level = REQ_IO_HIGH;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301827 }
1828
1829 /* ACK status to the core */
1830 writeb_relaxed(irq_ack, (msm_host->core_mem + CORE_PWRCTL_CTL));
1831 /*
1832 * SDHC has core_mem and hc_mem device memory and these memory
1833 * addresses do not fall within 1KB region. Hence, any update to
1834 * core_mem address space would require an mb() to ensure this gets
1835 * completed before its next update to registers within hc_mem.
1836 */
1837 mb();
1838
Sahitya Tummala179e7382013-03-20 19:24:01 +05301839 if (io_level & REQ_IO_HIGH)
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07001840 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
1841 ~CORE_IO_PAD_PWR_SWITCH),
1842 host->ioaddr + CORE_VENDOR_SPEC);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301843 else if (io_level & REQ_IO_LOW)
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07001844 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
1845 CORE_IO_PAD_PWR_SWITCH),
1846 host->ioaddr + CORE_VENDOR_SPEC);
1847 mb();
1848
Asutosh Das33a4ff52012-12-18 16:14:02 +05301849 pr_debug("%s: Handled IRQ(%d), ret=%d, ack=0x%x\n",
1850 mmc_hostname(msm_host->mmc), irq, ret, irq_ack);
Sahitya Tummala179e7382013-03-20 19:24:01 +05301851 spin_lock_irqsave(&host->lock, flags);
1852 if (pwr_state)
1853 msm_host->curr_pwr_state = pwr_state;
1854 if (io_level)
1855 msm_host->curr_io_level = io_level;
1856 complete(&msm_host->pwr_irq_completion);
1857 spin_unlock_irqrestore(&host->lock, flags);
1858
Asutosh Das33a4ff52012-12-18 16:14:02 +05301859 return IRQ_HANDLED;
1860}
1861
1862/* This function returns the max. current supported by VDD rail in mA */
1863static unsigned int sdhci_msm_get_vreg_vdd_max_current(struct sdhci_msm_host
1864 *host)
1865{
1866 struct sdhci_msm_slot_reg_data *curr_slot = host->pdata->vreg_data;
1867 if (!curr_slot)
1868 return 0;
1869 if (curr_slot->vdd_data)
1870 return curr_slot->vdd_data->hpm_uA / 1000;
1871 else
1872 return 0;
1873}
Sahitya Tummala3b292c32013-06-20 14:00:18 +05301874
1875static ssize_t
1876show_polling(struct device *dev, struct device_attribute *attr, char *buf)
1877{
1878 struct sdhci_host *host = dev_get_drvdata(dev);
1879 int poll;
1880 unsigned long flags;
1881
1882 spin_lock_irqsave(&host->lock, flags);
1883 poll = !!(host->mmc->caps & MMC_CAP_NEEDS_POLL);
1884 spin_unlock_irqrestore(&host->lock, flags);
1885
1886 return snprintf(buf, PAGE_SIZE, "%d\n", poll);
1887}
1888
1889static ssize_t
1890store_polling(struct device *dev, struct device_attribute *attr,
1891 const char *buf, size_t count)
1892{
1893 struct sdhci_host *host = dev_get_drvdata(dev);
1894 int value;
1895 unsigned long flags;
1896
1897 if (!kstrtou32(buf, 0, &value)) {
1898 spin_lock_irqsave(&host->lock, flags);
1899 if (value) {
1900 host->mmc->caps |= MMC_CAP_NEEDS_POLL;
1901 mmc_detect_change(host->mmc, 0);
1902 } else {
1903 host->mmc->caps &= ~MMC_CAP_NEEDS_POLL;
1904 }
1905 spin_unlock_irqrestore(&host->lock, flags);
1906 }
1907 return count;
1908}
1909
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301910static ssize_t
1911show_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
1912 char *buf)
1913{
1914 struct sdhci_host *host = dev_get_drvdata(dev);
1915 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1916 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1917
1918 return snprintf(buf, PAGE_SIZE, "%u\n",
1919 msm_host->msm_bus_vote.is_max_bw_needed);
1920}
1921
1922static ssize_t
1923store_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
1924 const char *buf, size_t count)
1925{
1926 struct sdhci_host *host = dev_get_drvdata(dev);
1927 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1928 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1929 uint32_t value;
1930 unsigned long flags;
1931
1932 if (!kstrtou32(buf, 0, &value)) {
1933 spin_lock_irqsave(&host->lock, flags);
1934 msm_host->msm_bus_vote.is_max_bw_needed = !!value;
1935 spin_unlock_irqrestore(&host->lock, flags);
1936 }
1937 return count;
1938}
Asutosh Das33a4ff52012-12-18 16:14:02 +05301939
Sahitya Tummala179e7382013-03-20 19:24:01 +05301940static void sdhci_msm_check_power_status(struct sdhci_host *host, u32 req_type)
Asutosh Das33a4ff52012-12-18 16:14:02 +05301941{
1942 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1943 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala179e7382013-03-20 19:24:01 +05301944 unsigned long flags;
1945 bool done = false;
Asutosh Das33a4ff52012-12-18 16:14:02 +05301946
Sahitya Tummala179e7382013-03-20 19:24:01 +05301947 spin_lock_irqsave(&host->lock, flags);
1948 pr_debug("%s: %s: request %d curr_pwr_state %x curr_io_level %x\n",
1949 mmc_hostname(host->mmc), __func__, req_type,
1950 msm_host->curr_pwr_state, msm_host->curr_io_level);
1951 if ((req_type & msm_host->curr_pwr_state) ||
1952 (req_type & msm_host->curr_io_level))
1953 done = true;
1954 spin_unlock_irqrestore(&host->lock, flags);
Asutosh Das33a4ff52012-12-18 16:14:02 +05301955
Sahitya Tummala179e7382013-03-20 19:24:01 +05301956 /*
1957 * This is needed here to hanlde a case where IRQ gets
1958 * triggered even before this function is called so that
1959 * x->done counter of completion gets reset. Otherwise,
1960 * next call to wait_for_completion returns immediately
1961 * without actually waiting for the IRQ to be handled.
1962 */
1963 if (done)
1964 init_completion(&msm_host->pwr_irq_completion);
1965 else
1966 wait_for_completion(&msm_host->pwr_irq_completion);
1967
1968 pr_debug("%s: %s: request %d done\n", mmc_hostname(host->mmc),
1969 __func__, req_type);
Asutosh Das33a4ff52012-12-18 16:14:02 +05301970}
1971
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07001972static void sdhci_msm_toggle_cdr(struct sdhci_host *host, bool enable)
1973{
1974 if (enable)
1975 writel_relaxed((readl_relaxed(host->ioaddr +
1976 CORE_DLL_CONFIG) | CORE_CDR_EN),
1977 host->ioaddr + CORE_DLL_CONFIG);
1978 else
1979 writel_relaxed((readl_relaxed(host->ioaddr +
1980 CORE_DLL_CONFIG) & ~CORE_CDR_EN),
1981 host->ioaddr + CORE_DLL_CONFIG);
1982}
1983
Asutosh Das3781bd82013-01-10 21:11:04 +05301984static unsigned int sdhci_msm_max_segs(void)
1985{
1986 return SDHCI_MSM_MAX_SEGMENTS;
1987}
1988
Sahitya Tummala00240122013-02-28 19:50:51 +05301989static unsigned int sdhci_msm_get_min_clock(struct sdhci_host *host)
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301990{
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301991 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1992 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301993
Sahitya Tummala00240122013-02-28 19:50:51 +05301994 return msm_host->pdata->sup_clk_table[0];
1995}
1996
1997static unsigned int sdhci_msm_get_max_clock(struct sdhci_host *host)
1998{
1999 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2000 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2001 int max_clk_index = msm_host->pdata->sup_clk_cnt;
2002
2003 return msm_host->pdata->sup_clk_table[max_clk_index - 1];
2004}
2005
2006static unsigned int sdhci_msm_get_sup_clk_rate(struct sdhci_host *host,
2007 u32 req_clk)
2008{
2009 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2010 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2011 unsigned int sel_clk = -1;
2012 unsigned char cnt;
2013
2014 if (req_clk < sdhci_msm_get_min_clock(host)) {
2015 sel_clk = sdhci_msm_get_min_clock(host);
2016 return sel_clk;
2017 }
2018
2019 for (cnt = 0; cnt < msm_host->pdata->sup_clk_cnt; cnt++) {
2020 if (msm_host->pdata->sup_clk_table[cnt] > req_clk) {
2021 break;
2022 } else if (msm_host->pdata->sup_clk_table[cnt] == req_clk) {
2023 sel_clk = msm_host->pdata->sup_clk_table[cnt];
2024 break;
2025 } else {
2026 sel_clk = msm_host->pdata->sup_clk_table[cnt];
2027 }
2028 }
2029 return sel_clk;
2030}
2031
2032static int sdhci_msm_prepare_clocks(struct sdhci_host *host, bool enable)
2033{
2034 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2035 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2036 int rc = 0;
2037
2038 if (enable && !atomic_read(&msm_host->clks_on)) {
2039 pr_debug("%s: request to enable clocks\n",
2040 mmc_hostname(host->mmc));
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302041
2042 sdhci_msm_bus_voting(host, 1);
2043
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302044 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
2045 rc = clk_prepare_enable(msm_host->bus_clk);
2046 if (rc) {
2047 pr_err("%s: %s: failed to enable the bus-clock with error %d\n",
2048 mmc_hostname(host->mmc), __func__, rc);
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302049 goto remove_vote;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302050 }
2051 }
2052 if (!IS_ERR(msm_host->pclk)) {
2053 rc = clk_prepare_enable(msm_host->pclk);
2054 if (rc) {
2055 pr_err("%s: %s: failed to enable the pclk with error %d\n",
2056 mmc_hostname(host->mmc), __func__, rc);
2057 goto disable_bus_clk;
2058 }
2059 }
2060 rc = clk_prepare_enable(msm_host->clk);
2061 if (rc) {
2062 pr_err("%s: %s: failed to enable the host-clk with error %d\n",
2063 mmc_hostname(host->mmc), __func__, rc);
2064 goto disable_pclk;
2065 }
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -07002066 if (!IS_ERR(msm_host->ff_clk)) {
2067 rc = clk_prepare_enable(msm_host->ff_clk);
2068 if (rc) {
2069 pr_err("%s: %s: failed to enable the ff_clk with error %d\n",
2070 mmc_hostname(host->mmc), __func__, rc);
2071 goto disable_clk;
2072 }
2073 }
2074 if (!IS_ERR(msm_host->sleep_clk)) {
2075 rc = clk_prepare_enable(msm_host->sleep_clk);
2076 if (rc) {
2077 pr_err("%s: %s: failed to enable the sleep_clk with error %d\n",
2078 mmc_hostname(host->mmc), __func__, rc);
2079 goto disable_ff_clk;
2080 }
2081 }
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302082 mb();
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302083
Sahitya Tummala00240122013-02-28 19:50:51 +05302084 } else if (!enable && atomic_read(&msm_host->clks_on)) {
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302085 pr_debug("%s: request to disable clocks\n",
2086 mmc_hostname(host->mmc));
2087 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
2088 mb();
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -07002089 if (!IS_ERR_OR_NULL(msm_host->sleep_clk))
2090 clk_disable_unprepare(msm_host->sleep_clk);
2091 if (!IS_ERR_OR_NULL(msm_host->ff_clk))
2092 clk_disable_unprepare(msm_host->ff_clk);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302093 clk_disable_unprepare(msm_host->clk);
2094 if (!IS_ERR(msm_host->pclk))
2095 clk_disable_unprepare(msm_host->pclk);
2096 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2097 clk_disable_unprepare(msm_host->bus_clk);
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302098
2099 sdhci_msm_bus_voting(host, 0);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302100 }
Sahitya Tummala00240122013-02-28 19:50:51 +05302101 atomic_set(&msm_host->clks_on, enable);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302102 goto out;
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -07002103disable_ff_clk:
2104 if (!IS_ERR_OR_NULL(msm_host->ff_clk))
2105 clk_disable_unprepare(msm_host->ff_clk);
2106disable_clk:
2107 if (!IS_ERR_OR_NULL(msm_host->clk))
2108 clk_disable_unprepare(msm_host->clk);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302109disable_pclk:
2110 if (!IS_ERR_OR_NULL(msm_host->pclk))
2111 clk_disable_unprepare(msm_host->pclk);
2112disable_bus_clk:
2113 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2114 clk_disable_unprepare(msm_host->bus_clk);
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302115remove_vote:
2116 if (msm_host->msm_bus_vote.client_handle)
2117 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302118out:
Sahitya Tummala00240122013-02-28 19:50:51 +05302119 return rc;
2120}
2121
2122static void sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock)
2123{
2124 int rc;
2125 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2126 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2127 struct mmc_ios curr_ios = host->mmc->ios;
2128 u32 sup_clock, ddr_clock;
2129
2130 if (!clock) {
2131 sdhci_msm_prepare_clocks(host, false);
2132 host->clock = clock;
2133 return;
2134 }
2135
2136 rc = sdhci_msm_prepare_clocks(host, true);
2137 if (rc)
2138 return;
2139
2140 sup_clock = sdhci_msm_get_sup_clk_rate(host, clock);
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -07002141 if ((curr_ios.timing == MMC_TIMING_UHS_DDR50) ||
2142 (curr_ios.timing == MMC_TIMING_MMC_HS400)) {
Sahitya Tummala00240122013-02-28 19:50:51 +05302143 /*
2144 * The SDHC requires internal clock frequency to be double the
2145 * actual clock that will be set for DDR mode. The controller
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -07002146 * uses the faster clock(100/400MHz) for some of its parts and
2147 * send the actual required clock (50/200MHz) to the card.
Sahitya Tummala00240122013-02-28 19:50:51 +05302148 */
2149 ddr_clock = clock * 2;
2150 sup_clock = sdhci_msm_get_sup_clk_rate(host,
2151 ddr_clock);
2152 }
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -07002153
2154 /*
2155 * In general all timing modes are controlled via UHS mode select in
2156 * Host Control2 register. eMMC specific HS200/HS400 doesn't have
2157 * their respective modes defined here, hence we use these values.
2158 *
2159 * HS200 - SDR104 (Since they both are equivalent in functionality)
2160 * HS400 - This involves multiple configurations
2161 * Initially SDR104 - when tuning is required as HS200
2162 * Then when switching to DDR @ 400MHz (HS400) we use
2163 * the vendor specific HC_SELECT_IN to control the mode.
2164 *
2165 * In addition to controlling the modes we also need to select the
2166 * correct input clock for DLL depending on the mode.
2167 *
2168 * HS400 - divided clock (free running MCLK/2)
2169 * All other modes - default (free running MCLK)
2170 */
2171 if (curr_ios.timing == MMC_TIMING_MMC_HS400) {
2172 /* Select the divided clock (free running MCLK/2) */
2173 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2174 & ~CORE_HC_MCLK_SEL_MASK)
2175 | CORE_HC_MCLK_SEL_HS400),
2176 host->ioaddr + CORE_VENDOR_SPEC);
2177 /*
2178 * Select HS400 mode using the HC_SELECT_IN from VENDOR SPEC
2179 * register
2180 */
2181 if (msm_host->tuning_done && !msm_host->calibration_done) {
2182 /*
2183 * Write 0x6 to HC_SELECT_IN and 1 to HC_SELECT_IN_EN
2184 * field in VENDOR_SPEC_FUNC
2185 */
2186 writel_relaxed((readl_relaxed(host->ioaddr + \
2187 CORE_VENDOR_SPEC)
2188 | CORE_HC_SELECT_IN_HS400
2189 | CORE_HC_SELECT_IN_EN),
2190 host->ioaddr + CORE_VENDOR_SPEC);
2191 }
2192 } else {
2193 /* Select the default clock (free running MCLK) */
2194 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2195 & ~CORE_HC_MCLK_SEL_MASK)
2196 | CORE_HC_MCLK_SEL_DFLT),
2197 host->ioaddr + CORE_VENDOR_SPEC);
2198
2199 /*
2200 * Disable HC_SELECT_IN to be able to use the UHS mode select
2201 * configuration from Host Control2 register for all other
2202 * modes.
2203 *
2204 * Write 0 to HC_SELECT_IN and HC_SELECT_IN_EN field
2205 * in VENDOR_SPEC_FUNC
2206 */
2207 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2208 & ~CORE_HC_SELECT_IN_EN
2209 & ~CORE_HC_SELECT_IN_MASK),
2210 host->ioaddr + CORE_VENDOR_SPEC);
2211 }
2212 mb();
2213
Sahitya Tummala00240122013-02-28 19:50:51 +05302214 if (sup_clock != msm_host->clk_rate) {
2215 pr_debug("%s: %s: setting clk rate to %u\n",
2216 mmc_hostname(host->mmc), __func__, sup_clock);
2217 rc = clk_set_rate(msm_host->clk, sup_clock);
2218 if (rc) {
2219 pr_err("%s: %s: Failed to set rate %u for host-clk : %d\n",
2220 mmc_hostname(host->mmc), __func__,
2221 sup_clock, rc);
2222 return;
2223 }
2224 msm_host->clk_rate = sup_clock;
2225 host->clock = clock;
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302226 /*
2227 * Update the bus vote in case of frequency change due to
2228 * clock scaling.
2229 */
2230 sdhci_msm_bus_voting(host, 1);
Sahitya Tummala00240122013-02-28 19:50:51 +05302231 }
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302232}
2233
Sahitya Tummala5e4f9642013-03-21 11:13:25 +05302234static int sdhci_msm_set_uhs_signaling(struct sdhci_host *host,
2235 unsigned int uhs)
2236{
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -07002237 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2238 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala5e4f9642013-03-21 11:13:25 +05302239 u16 ctrl_2;
2240
2241 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2242 /* Select Bus Speed Mode for host */
2243 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -07002244 if (uhs == MMC_TIMING_MMC_HS400)
2245 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2246 else if (uhs == MMC_TIMING_MMC_HS200)
Sahitya Tummala5e4f9642013-03-21 11:13:25 +05302247 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2248 else if (uhs == MMC_TIMING_UHS_SDR12)
2249 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
2250 else if (uhs == MMC_TIMING_UHS_SDR25)
2251 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
2252 else if (uhs == MMC_TIMING_UHS_SDR50)
2253 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
2254 else if (uhs == MMC_TIMING_UHS_SDR104)
2255 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2256 else if (uhs == MMC_TIMING_UHS_DDR50)
2257 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Sahitya Tummala00240122013-02-28 19:50:51 +05302258 /*
2259 * When clock frquency is less than 100MHz, the feedback clock must be
2260 * provided and DLL must not be used so that tuning can be skipped. To
2261 * provide feedback clock, the mode selection can be any value less
2262 * than 3'b011 in bits [2:0] of HOST CONTROL2 register.
2263 */
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -07002264 if (host->clock <= CORE_FREQ_100MHZ) {
2265 if ((uhs == MMC_TIMING_MMC_HS400) ||
2266 (uhs == MMC_TIMING_MMC_HS200) ||
2267 (uhs == MMC_TIMING_UHS_SDR104))
2268 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Sahitya Tummala00240122013-02-28 19:50:51 +05302269
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -07002270 /*
2271 * Make sure DLL is disabled when not required
2272 *
2273 * Write 1 to DLL_RST bit of DLL_CONFIG register
2274 */
2275 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
2276 | CORE_DLL_RST),
2277 host->ioaddr + CORE_DLL_CONFIG);
2278
2279 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
2280 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
2281 | CORE_DLL_PDN),
2282 host->ioaddr + CORE_DLL_CONFIG);
2283 mb();
2284
2285 /*
2286 * The DLL needs to be restored and CDCLP533 recalibrated
2287 * when the clock frequency is set back to 400MHz.
2288 */
2289 msm_host->calibration_done = false;
2290 }
2291
2292 pr_debug("%s: %s-clock:%u uhs mode:%u ctrl_2:0x%x\n",
2293 mmc_hostname(host->mmc), __func__, host->clock, uhs, ctrl_2);
Sahitya Tummala5e4f9642013-03-21 11:13:25 +05302294 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
2295
2296 return 0;
2297}
2298
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002299/*
2300 * sdhci_msm_disable_data_xfer - disable undergoing AHB bus data transfer
2301 *
2302 * Write 0 to bit 0 in MCI_DATA_CTL (offset 0x2C) - clearing TxActive bit by
2303 * access to legacy registers. It will stop current burst and prevent start of
2304 * the next on.
2305 *
2306 * Polling CORE_AHB_DATA_DELAY_US timeout, by reading bit 13:12 until they are 0
2307 * in CORE_SDCC_DEBUG_REG (offset 0x124) will validate that AHB burst was
2308 * completed and a new one didn't start.
2309 *
2310 * Waiting for 4us while AHB finishes descriptors fetch.
2311 */
2312static void sdhci_msm_disable_data_xfer(struct sdhci_host *host)
2313{
2314 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2315 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2316 u32 value;
2317 int ret;
Venkat Gopalakrishnan0a179c82013-06-26 17:56:11 -07002318 u32 version;
2319
2320 version = readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION);
2321 /* Core version 3.1.0 doesn't need this workaround */
2322 if (version == CORE_VERSION_310)
2323 return;
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002324
2325 value = readl_relaxed(msm_host->core_mem + CORE_MCI_DATA_CTRL);
2326 value &= ~(u32)CORE_MCI_DPSM_ENABLE;
2327 writel_relaxed(value, msm_host->core_mem + CORE_MCI_DATA_CTRL);
2328
2329 /* Enable the test bus for device slot */
2330 writel_relaxed(CORE_TESTBUS_ENA | CORE_TESTBUS_SEL2,
2331 msm_host->core_mem + CORE_TESTBUS_CONFIG);
2332
2333 ret = readl_poll_timeout_noirq(msm_host->core_mem
2334 + CORE_SDCC_DEBUG_REG, value,
2335 !(value & CORE_DEBUG_REG_AHB_HTRANS),
2336 CORE_AHB_DATA_DELAY_US, 1);
2337 if (ret) {
2338 pr_err("%s: %s: can't stop ongoing AHB bus access by ADMA\n",
2339 mmc_hostname(host->mmc), __func__);
2340 BUG();
2341 }
2342 /* Disable the test bus for device slot */
2343 value = readl_relaxed(msm_host->core_mem + CORE_TESTBUS_CONFIG);
2344 value &= ~CORE_TESTBUS_ENA;
2345 writel_relaxed(value, msm_host->core_mem + CORE_TESTBUS_CONFIG);
2346
2347 udelay(CORE_AHB_DESC_DELAY_US);
2348}
2349
Asutosh Das33a4ff52012-12-18 16:14:02 +05302350static struct sdhci_ops sdhci_msm_ops = {
Sahitya Tummala5e4f9642013-03-21 11:13:25 +05302351 .set_uhs_signaling = sdhci_msm_set_uhs_signaling,
Asutosh Das33a4ff52012-12-18 16:14:02 +05302352 .check_power_status = sdhci_msm_check_power_status,
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07002353 .execute_tuning = sdhci_msm_execute_tuning,
2354 .toggle_cdr = sdhci_msm_toggle_cdr,
Asutosh Das3781bd82013-01-10 21:11:04 +05302355 .get_max_segments = sdhci_msm_max_segs,
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302356 .set_clock = sdhci_msm_set_clock,
Sahitya Tummala00240122013-02-28 19:50:51 +05302357 .get_min_clock = sdhci_msm_get_min_clock,
2358 .get_max_clock = sdhci_msm_get_max_clock,
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002359 .disable_data_xfer = sdhci_msm_disable_data_xfer,
Asutosh Das33a4ff52012-12-18 16:14:02 +05302360};
2361
2362static int __devinit sdhci_msm_probe(struct platform_device *pdev)
2363{
2364 struct sdhci_host *host;
2365 struct sdhci_pltfm_host *pltfm_host;
2366 struct sdhci_msm_host *msm_host;
2367 struct resource *core_memres = NULL;
Asutosh Dasbbc84782013-02-11 15:31:35 +05302368 int ret = 0, dead = 0;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302369 u32 vdd_max_current;
Stephen Boyd3edbd8f2013-04-24 14:19:46 -07002370 u16 host_version;
Subhash Jadavanic08d2062013-05-14 17:46:43 +05302371 u32 pwr, irq_status, irq_ctl;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302372
2373 pr_debug("%s: Enter %s\n", dev_name(&pdev->dev), __func__);
2374 msm_host = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_msm_host),
2375 GFP_KERNEL);
2376 if (!msm_host) {
2377 ret = -ENOMEM;
2378 goto out;
2379 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05302380
2381 msm_host->sdhci_msm_pdata.ops = &sdhci_msm_ops;
2382 host = sdhci_pltfm_init(pdev, &msm_host->sdhci_msm_pdata);
2383 if (IS_ERR(host)) {
2384 ret = PTR_ERR(host);
2385 goto out;
2386 }
2387
2388 pltfm_host = sdhci_priv(host);
2389 pltfm_host->priv = msm_host;
2390 msm_host->mmc = host->mmc;
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302391 msm_host->pdev = pdev;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302392
2393 /* Extract platform data */
2394 if (pdev->dev.of_node) {
Venkat Gopalakrishnanc61ab7e2013-03-11 12:17:57 -07002395 ret = of_alias_get_id(pdev->dev.of_node, "sdhc");
2396 if (ret < 0) {
2397 dev_err(&pdev->dev, "Failed to get slot index %d\n",
2398 ret);
2399 goto pltfm_free;
2400 }
2401 if (disable_slots & (1 << (ret - 1))) {
2402 dev_info(&pdev->dev, "%s: Slot %d disabled\n", __func__,
2403 ret);
2404 ret = -ENODEV;
2405 goto pltfm_free;
2406 }
2407
Asutosh Das33a4ff52012-12-18 16:14:02 +05302408 msm_host->pdata = sdhci_msm_populate_pdata(&pdev->dev);
2409 if (!msm_host->pdata) {
2410 dev_err(&pdev->dev, "DT parsing error\n");
2411 goto pltfm_free;
2412 }
2413 } else {
2414 dev_err(&pdev->dev, "No device tree node\n");
2415 goto pltfm_free;
2416 }
2417
2418 /* Setup Clocks */
2419
2420 /* Setup SDCC bus voter clock. */
2421 msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus_clk");
2422 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
2423 /* Vote for max. clk rate for max. performance */
2424 ret = clk_set_rate(msm_host->bus_clk, INT_MAX);
2425 if (ret)
2426 goto pltfm_free;
2427 ret = clk_prepare_enable(msm_host->bus_clk);
2428 if (ret)
2429 goto pltfm_free;
2430 }
2431
2432 /* Setup main peripheral bus clock */
2433 msm_host->pclk = devm_clk_get(&pdev->dev, "iface_clk");
2434 if (!IS_ERR(msm_host->pclk)) {
2435 ret = clk_prepare_enable(msm_host->pclk);
2436 if (ret)
2437 goto bus_clk_disable;
2438 }
2439
2440 /* Setup SDC MMC clock */
2441 msm_host->clk = devm_clk_get(&pdev->dev, "core_clk");
2442 if (IS_ERR(msm_host->clk)) {
2443 ret = PTR_ERR(msm_host->clk);
2444 goto pclk_disable;
2445 }
2446
Sahitya Tummala00240122013-02-28 19:50:51 +05302447 /* Set to the minimum supported clock frequency */
2448 ret = clk_set_rate(msm_host->clk, sdhci_msm_get_min_clock(host));
2449 if (ret) {
2450 dev_err(&pdev->dev, "MClk rate set failed (%d)\n", ret);
Sahitya Tummalac954ab02013-06-07 13:03:07 +05302451 goto pclk_disable;
Sahitya Tummala00240122013-02-28 19:50:51 +05302452 }
Sahitya Tummalac954ab02013-06-07 13:03:07 +05302453 ret = clk_prepare_enable(msm_host->clk);
2454 if (ret)
2455 goto pclk_disable;
2456
Sahitya Tummala00240122013-02-28 19:50:51 +05302457 msm_host->clk_rate = sdhci_msm_get_min_clock(host);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302458 atomic_set(&msm_host->clks_on, 1);
Sahitya Tummala00240122013-02-28 19:50:51 +05302459
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -07002460 /* Setup CDC calibration fixed feedback clock */
2461 msm_host->ff_clk = devm_clk_get(&pdev->dev, "cal_clk");
2462 if (!IS_ERR(msm_host->ff_clk)) {
2463 ret = clk_prepare_enable(msm_host->ff_clk);
2464 if (ret)
2465 goto clk_disable;
2466 }
2467
2468 /* Setup CDC calibration sleep clock */
2469 msm_host->sleep_clk = devm_clk_get(&pdev->dev, "sleep_clk");
2470 if (!IS_ERR(msm_host->sleep_clk)) {
2471 ret = clk_prepare_enable(msm_host->sleep_clk);
2472 if (ret)
2473 goto ff_clk_disable;
2474 }
2475
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302476 ret = sdhci_msm_bus_register(msm_host, pdev);
2477 if (ret)
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -07002478 goto sleep_clk_disable;
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302479
2480 if (msm_host->msm_bus_vote.client_handle)
2481 INIT_DELAYED_WORK(&msm_host->msm_bus_vote.vote_work,
2482 sdhci_msm_bus_work);
2483 sdhci_msm_bus_voting(host, 1);
2484
Asutosh Das33a4ff52012-12-18 16:14:02 +05302485 /* Setup regulators */
2486 ret = sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, true);
2487 if (ret) {
2488 dev_err(&pdev->dev, "Regulator setup failed (%d)\n", ret);
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302489 goto bus_unregister;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302490 }
2491
2492 /* Reset the core and Enable SDHC mode */
2493 core_memres = platform_get_resource_byname(pdev,
2494 IORESOURCE_MEM, "core_mem");
2495 msm_host->core_mem = devm_ioremap(&pdev->dev, core_memres->start,
2496 resource_size(core_memres));
2497
2498 if (!msm_host->core_mem) {
2499 dev_err(&pdev->dev, "Failed to remap registers\n");
2500 ret = -ENOMEM;
2501 goto vreg_deinit;
2502 }
2503
2504 /* Set SW_RST bit in POWER register (Offset 0x0) */
Sahitya Tummalad5d76e72013-04-25 11:50:56 +05302505 writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_POWER) |
2506 CORE_SW_RST, msm_host->core_mem + CORE_POWER);
2507 /*
2508 * SW reset can take upto 10HCLK + 15MCLK cycles.
2509 * Calculating based on min clk rates (hclk = 27MHz,
2510 * mclk = 400KHz) it comes to ~40us. Let's poll for
2511 * max. 1ms for reset completion.
2512 */
2513 ret = readl_poll_timeout(msm_host->core_mem + CORE_POWER,
2514 pwr, !(pwr & CORE_SW_RST), 100, 10);
2515
2516 if (ret) {
2517 dev_err(&pdev->dev, "reset failed (%d)\n", ret);
2518 goto vreg_deinit;
2519 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05302520 /* Set HC_MODE_EN bit in HC_MODE register */
2521 writel_relaxed(HC_MODE_EN, (msm_host->core_mem + CORE_HC_MODE));
2522
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -07002523 /* Set FF_CLK_SW_RST_DIS bit in HC_MODE register */
2524 writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_HC_MODE) |
2525 FF_CLK_SW_RST_DIS, msm_host->core_mem + CORE_HC_MODE);
2526
Asutosh Das33a4ff52012-12-18 16:14:02 +05302527 /*
Subhash Jadavanic08d2062013-05-14 17:46:43 +05302528 * CORE_SW_RST above may trigger power irq if previous status of PWRCTL
2529 * was either BUS_ON or IO_HIGH_V. So before we enable the power irq
2530 * interrupt in GIC (by registering the interrupt handler), we need to
2531 * ensure that any pending power irq interrupt status is acknowledged
2532 * otherwise power irq interrupt handler would be fired prematurely.
2533 */
2534 irq_status = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
2535 writel_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
2536 irq_ctl = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_CTL);
2537 if (irq_status & (CORE_PWRCTL_BUS_ON | CORE_PWRCTL_BUS_OFF))
2538 irq_ctl |= CORE_PWRCTL_BUS_SUCCESS;
2539 if (irq_status & (CORE_PWRCTL_IO_HIGH | CORE_PWRCTL_IO_LOW))
2540 irq_ctl |= CORE_PWRCTL_IO_SUCCESS;
2541 writel_relaxed(irq_ctl, (msm_host->core_mem + CORE_PWRCTL_CTL));
2542 /*
2543 * Ensure that above writes are propogated before interrupt enablement
2544 * in GIC.
2545 */
2546 mb();
2547
2548 /*
Asutosh Das33a4ff52012-12-18 16:14:02 +05302549 * Following are the deviations from SDHC spec v3.0 -
2550 * 1. Card detection is handled using separate GPIO.
2551 * 2. Bus power control is handled by interacting with PMIC.
2552 */
2553 host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
2554 host->quirks |= SDHCI_QUIRK_SINGLE_POWER_WRITE;
Sahitya Tummala00240122013-02-28 19:50:51 +05302555 host->quirks |= SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN;
2556 host->quirks2 |= SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK;
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302557 host->quirks2 |= SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING;
Krishna Kondaa20d3362013-04-01 21:01:59 -07002558 host->quirks2 |= SDHCI_QUIRK2_USE_MAX_DISCARD_SIZE;
Sahitya Tummalad2ae8832013-04-12 11:49:11 +05302559 host->quirks2 |= SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD;
Sahitya Tummalae6886bd2013-04-12 12:11:20 +05302560 host->quirks2 |= SDHCI_QUIRK2_BROKEN_PRESET_VALUE;
Sahitya Tummala4d12d0b2013-04-12 11:59:25 +05302561 host->quirks2 |= SDHCI_QUIRK2_USE_RESERVED_MAX_TIMEOUT;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302562
Sahitya Tummalaf667cc12013-06-10 16:32:51 +05302563 if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK)
2564 host->quirks2 |= SDHCI_QUIRK2_DIVIDE_TOUT_BY_4;
2565
Stephen Boyd3edbd8f2013-04-24 14:19:46 -07002566 host_version = readw_relaxed((host->ioaddr + SDHCI_HOST_VERSION));
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002567 dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n",
2568 host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >>
2569 SDHCI_VENDOR_VER_SHIFT));
2570 if (((host_version & SDHCI_VENDOR_VER_MASK) >>
2571 SDHCI_VENDOR_VER_SHIFT) == SDHCI_VER_100) {
2572 /*
2573 * Add 40us delay in interrupt handler when
2574 * operating at initialization frequency(400KHz).
2575 */
2576 host->quirks2 |= SDHCI_QUIRK2_SLOW_INT_CLR;
2577 /*
2578 * Set Software Reset for DAT line in Software
2579 * Reset Register (Bit 2).
2580 */
2581 host->quirks2 |= SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT;
2582 }
2583
2584 /* Setup PWRCTL irq */
Asutosh Dasbbc84782013-02-11 15:31:35 +05302585 msm_host->pwr_irq = platform_get_irq_byname(pdev, "pwr_irq");
2586 if (msm_host->pwr_irq < 0) {
Asutosh Das33a4ff52012-12-18 16:14:02 +05302587 dev_err(&pdev->dev, "Failed to get pwr_irq by name (%d)\n",
Asutosh Dasbbc84782013-02-11 15:31:35 +05302588 msm_host->pwr_irq);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302589 goto vreg_deinit;
2590 }
Asutosh Dasbbc84782013-02-11 15:31:35 +05302591 ret = devm_request_threaded_irq(&pdev->dev, msm_host->pwr_irq, NULL,
Asutosh Das33a4ff52012-12-18 16:14:02 +05302592 sdhci_msm_pwr_irq, IRQF_ONESHOT,
Venkat Gopalakrishnan8609a432012-09-11 16:13:31 -07002593 dev_name(&pdev->dev), host);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302594 if (ret) {
2595 dev_err(&pdev->dev, "Request threaded irq(%d) failed (%d)\n",
Asutosh Dasbbc84782013-02-11 15:31:35 +05302596 msm_host->pwr_irq, ret);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302597 goto vreg_deinit;
2598 }
2599
2600 /* Enable pwr irq interrupts */
2601 writel_relaxed(INT_MASK, (msm_host->core_mem + CORE_PWRCTL_MASK));
2602
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302603 /* Set clock gating delay to be used when CONFIG_MMC_CLKGATE is set */
2604 msm_host->mmc->clkgate_delay = SDHCI_MSM_MMC_CLK_GATE_DELAY;
2605
Asutosh Das33a4ff52012-12-18 16:14:02 +05302606 /* Set host capabilities */
2607 msm_host->mmc->caps |= msm_host->pdata->mmc_bus_width;
2608 msm_host->mmc->caps |= msm_host->pdata->caps;
2609
2610 vdd_max_current = sdhci_msm_get_vreg_vdd_max_current(msm_host);
2611 if (vdd_max_current >= 800)
2612 msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_800;
2613 else if (vdd_max_current >= 600)
2614 msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_600;
2615 else if (vdd_max_current >= 400)
2616 msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_400;
2617 else
2618 msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_200;
2619
2620 if (vdd_max_current > 150)
2621 msm_host->mmc->caps |= MMC_CAP_SET_XPC_180 |
2622 MMC_CAP_SET_XPC_300|
2623 MMC_CAP_SET_XPC_330;
2624
2625 msm_host->mmc->caps2 |= msm_host->pdata->caps2;
Asutosh Dasbbc84782013-02-11 15:31:35 +05302626 msm_host->mmc->caps2 |= MMC_CAP2_CORE_RUNTIME_PM;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302627 msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR;
2628 msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR_CONTROL;
2629 msm_host->mmc->caps2 |= (MMC_CAP2_BOOTPART_NOACC |
2630 MMC_CAP2_DETECT_ON_ERR);
2631 msm_host->mmc->caps2 |= MMC_CAP2_SANITIZE;
2632 msm_host->mmc->caps2 |= MMC_CAP2_CACHE_CTRL;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302633 msm_host->mmc->caps2 |= MMC_CAP2_POWEROFF_NOTIFY;
Sahitya Tummala00240122013-02-28 19:50:51 +05302634 msm_host->mmc->caps2 |= MMC_CAP2_CLK_SCALE;
Konstantin Dorfmanfa436d52013-04-17 16:26:11 +03002635 msm_host->mmc->caps2 |= MMC_CAP2_STOP_REQUEST;
Subhash Jadavani61a52c92013-05-29 15:52:10 +05302636 msm_host->mmc->caps2 |= MMC_CAP2_ASYNC_SDIO_IRQ_4BIT_MODE;
Asutosh Das4dc60412013-06-24 18:20:45 +05302637 msm_host->mmc->pm_caps |= MMC_PM_KEEP_POWER;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302638
2639 if (msm_host->pdata->nonremovable)
2640 msm_host->mmc->caps |= MMC_CAP_NONREMOVABLE;
2641
Sahitya Tummalab4e84042013-03-10 07:03:17 +05302642 host->cpu_dma_latency_us = msm_host->pdata->cpu_dma_latency_us;
2643
Sahitya Tummala179e7382013-03-20 19:24:01 +05302644 init_completion(&msm_host->pwr_irq_completion);
2645
Sahitya Tummala62448d92013-03-12 14:57:46 +05302646 if (gpio_is_valid(msm_host->pdata->status_gpio)) {
2647 ret = mmc_cd_gpio_request(msm_host->mmc,
2648 msm_host->pdata->status_gpio);
2649 if (ret) {
2650 dev_err(&pdev->dev, "%s: Failed to request card detection IRQ %d\n",
2651 __func__, ret);
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302652 goto vreg_deinit;
Sahitya Tummala62448d92013-03-12 14:57:46 +05302653 }
2654 }
2655
Sahitya Tummala2fa7eb12013-03-20 19:34:59 +05302656 if (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(32))) {
2657 host->dma_mask = DMA_BIT_MASK(32);
2658 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2659 } else {
2660 dev_err(&pdev->dev, "%s: Failed to set dma mask\n", __func__);
2661 }
2662
Asutosh Das33a4ff52012-12-18 16:14:02 +05302663 ret = sdhci_add_host(host);
2664 if (ret) {
2665 dev_err(&pdev->dev, "Add host failed (%d)\n", ret);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302666 goto free_cd_gpio;
Asutosh Das33a4ff52012-12-18 16:14:02 +05302667 }
2668
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302669 msm_host->msm_bus_vote.max_bus_bw.show = show_sdhci_max_bus_bw;
2670 msm_host->msm_bus_vote.max_bus_bw.store = store_sdhci_max_bus_bw;
2671 sysfs_attr_init(&msm_host->msm_bus_vote.max_bus_bw.attr);
2672 msm_host->msm_bus_vote.max_bus_bw.attr.name = "max_bus_bw";
2673 msm_host->msm_bus_vote.max_bus_bw.attr.mode = S_IRUGO | S_IWUSR;
2674 ret = device_create_file(&pdev->dev,
2675 &msm_host->msm_bus_vote.max_bus_bw);
2676 if (ret)
2677 goto remove_host;
2678
Sahitya Tummala3b292c32013-06-20 14:00:18 +05302679 if (!gpio_is_valid(msm_host->pdata->status_gpio)) {
2680 msm_host->polling.show = show_polling;
2681 msm_host->polling.store = store_polling;
2682 sysfs_attr_init(&msm_host->polling.attr);
2683 msm_host->polling.attr.name = "polling";
2684 msm_host->polling.attr.mode = S_IRUGO | S_IWUSR;
2685 ret = device_create_file(&pdev->dev, &msm_host->polling);
2686 if (ret)
2687 goto remove_max_bus_bw_file;
2688 }
Asutosh Dasbbc84782013-02-11 15:31:35 +05302689 ret = pm_runtime_set_active(&pdev->dev);
2690 if (ret)
2691 pr_err("%s: %s: pm_runtime_set_active failed: err: %d\n",
2692 mmc_hostname(host->mmc), __func__, ret);
2693 else
2694 pm_runtime_enable(&pdev->dev);
2695
Asutosh Das33a4ff52012-12-18 16:14:02 +05302696 /* Successful initialization */
2697 goto out;
2698
Sahitya Tummala3b292c32013-06-20 14:00:18 +05302699remove_max_bus_bw_file:
2700 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302701remove_host:
2702 dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
2703 sdhci_remove_host(host, dead);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302704free_cd_gpio:
2705 if (gpio_is_valid(msm_host->pdata->status_gpio))
2706 mmc_cd_gpio_free(msm_host->mmc);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302707vreg_deinit:
2708 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302709bus_unregister:
2710 if (msm_host->msm_bus_vote.client_handle)
2711 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2712 sdhci_msm_bus_unregister(msm_host);
Venkat Gopalakrishnan2d397062013-06-23 17:36:46 -07002713sleep_clk_disable:
2714 if (!IS_ERR(msm_host->sleep_clk))
2715 clk_disable_unprepare(msm_host->sleep_clk);
2716ff_clk_disable:
2717 if (!IS_ERR(msm_host->ff_clk))
2718 clk_disable_unprepare(msm_host->ff_clk);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302719clk_disable:
2720 if (!IS_ERR(msm_host->clk))
2721 clk_disable_unprepare(msm_host->clk);
2722pclk_disable:
2723 if (!IS_ERR(msm_host->pclk))
2724 clk_disable_unprepare(msm_host->pclk);
2725bus_clk_disable:
2726 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2727 clk_disable_unprepare(msm_host->bus_clk);
2728pltfm_free:
2729 sdhci_pltfm_free(pdev);
2730out:
2731 pr_debug("%s: Exit %s\n", dev_name(&pdev->dev), __func__);
2732 return ret;
2733}
2734
2735static int __devexit sdhci_msm_remove(struct platform_device *pdev)
2736{
2737 struct sdhci_host *host = platform_get_drvdata(pdev);
2738 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2739 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2740 struct sdhci_msm_pltfm_data *pdata = msm_host->pdata;
2741 int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) ==
2742 0xffffffff);
2743
2744 pr_debug("%s: %s\n", dev_name(&pdev->dev), __func__);
Sahitya Tummala3b292c32013-06-20 14:00:18 +05302745 if (!gpio_is_valid(msm_host->pdata->status_gpio))
2746 device_remove_file(&pdev->dev, &msm_host->polling);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302747 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302748 sdhci_remove_host(host, dead);
Asutosh Dasbbc84782013-02-11 15:31:35 +05302749 pm_runtime_disable(&pdev->dev);
Asutosh Das33a4ff52012-12-18 16:14:02 +05302750 sdhci_pltfm_free(pdev);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302751
2752 if (gpio_is_valid(msm_host->pdata->status_gpio))
2753 mmc_cd_gpio_free(msm_host->mmc);
2754
Asutosh Das33a4ff52012-12-18 16:14:02 +05302755 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302756
Asutosh Das33a4ff52012-12-18 16:14:02 +05302757 if (pdata->pin_data)
Asutosh Das390519d2012-12-21 12:21:42 +05302758 sdhci_msm_setup_pins(pdata, false);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05302759
2760 if (msm_host->msm_bus_vote.client_handle) {
2761 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2762 sdhci_msm_bus_unregister(msm_host);
2763 }
Asutosh Das33a4ff52012-12-18 16:14:02 +05302764 return 0;
2765}
2766
Asutosh Dasbbc84782013-02-11 15:31:35 +05302767static int sdhci_msm_runtime_suspend(struct device *dev)
2768{
2769 struct sdhci_host *host = dev_get_drvdata(dev);
2770 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2771 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2772
2773 disable_irq(host->irq);
2774 disable_irq(msm_host->pwr_irq);
2775
Sahitya Tummalac45ae732013-05-23 15:59:22 +05302776 /*
2777 * Remove the vote immediately only if clocks are off in which
2778 * case we might have queued work to remove vote but it may not
2779 * be completed before runtime suspend or system suspend.
2780 */
2781 if (!atomic_read(&msm_host->clks_on)) {
2782 if (msm_host->msm_bus_vote.client_handle)
2783 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2784 }
2785
Asutosh Dasbbc84782013-02-11 15:31:35 +05302786 return 0;
2787}
2788
2789static int sdhci_msm_runtime_resume(struct device *dev)
2790{
2791 struct sdhci_host *host = dev_get_drvdata(dev);
2792 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2793 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2794
2795 enable_irq(msm_host->pwr_irq);
2796 enable_irq(host->irq);
2797
2798 return 0;
2799}
2800
2801#ifdef CONFIG_PM_SLEEP
2802
2803static int sdhci_msm_suspend(struct device *dev)
2804{
2805 struct sdhci_host *host = dev_get_drvdata(dev);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302806 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2807 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Dasbbc84782013-02-11 15:31:35 +05302808 int ret = 0;
2809
Sahitya Tummala62448d92013-03-12 14:57:46 +05302810 if (gpio_is_valid(msm_host->pdata->status_gpio))
2811 mmc_cd_gpio_free(msm_host->mmc);
2812
Asutosh Dasbbc84782013-02-11 15:31:35 +05302813 if (pm_runtime_suspended(dev)) {
2814 pr_debug("%s: %s: already runtime suspended\n",
2815 mmc_hostname(host->mmc), __func__);
2816 goto out;
2817 }
2818
2819 return sdhci_msm_runtime_suspend(dev);
2820out:
2821 return ret;
2822}
2823
2824static int sdhci_msm_resume(struct device *dev)
2825{
2826 struct sdhci_host *host = dev_get_drvdata(dev);
Sahitya Tummala62448d92013-03-12 14:57:46 +05302827 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2828 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Dasbbc84782013-02-11 15:31:35 +05302829 int ret = 0;
2830
Sahitya Tummala62448d92013-03-12 14:57:46 +05302831 if (gpio_is_valid(msm_host->pdata->status_gpio)) {
2832 ret = mmc_cd_gpio_request(msm_host->mmc,
2833 msm_host->pdata->status_gpio);
2834 if (ret)
2835 pr_err("%s: %s: Failed to request card detection IRQ %d\n",
2836 mmc_hostname(host->mmc), __func__, ret);
2837 }
2838
Asutosh Dasbbc84782013-02-11 15:31:35 +05302839 if (pm_runtime_suspended(dev)) {
2840 pr_debug("%s: %s: runtime suspended, defer system resume\n",
2841 mmc_hostname(host->mmc), __func__);
2842 goto out;
2843 }
2844
2845 return sdhci_msm_runtime_resume(dev);
2846out:
2847 return ret;
2848}
2849#endif
2850
2851#ifdef CONFIG_PM
2852static const struct dev_pm_ops sdhci_msm_pmops = {
2853 SET_SYSTEM_SLEEP_PM_OPS(sdhci_msm_suspend, sdhci_msm_resume)
2854 SET_RUNTIME_PM_OPS(sdhci_msm_runtime_suspend, sdhci_msm_runtime_resume,
2855 NULL)
2856};
2857
2858#define SDHCI_MSM_PMOPS (&sdhci_msm_pmops)
2859
2860#else
2861#define SDHCI_PM_OPS NULL
2862#endif
Asutosh Das33a4ff52012-12-18 16:14:02 +05302863static const struct of_device_id sdhci_msm_dt_match[] = {
2864 {.compatible = "qcom,sdhci-msm"},
2865};
2866MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match);
2867
2868static struct platform_driver sdhci_msm_driver = {
2869 .probe = sdhci_msm_probe,
2870 .remove = __devexit_p(sdhci_msm_remove),
2871 .driver = {
2872 .name = "sdhci_msm",
2873 .owner = THIS_MODULE,
2874 .of_match_table = sdhci_msm_dt_match,
Asutosh Dasbbc84782013-02-11 15:31:35 +05302875 .pm = SDHCI_MSM_PMOPS,
Asutosh Das33a4ff52012-12-18 16:14:02 +05302876 },
2877};
2878
2879module_platform_driver(sdhci_msm_driver);
2880
2881MODULE_DESCRIPTION("Qualcomm Secure Digital Host Controller Interface driver");
2882MODULE_LICENSE("GPL v2");