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