blob: 928f3c703adeb1b338e58c964de995a37a65dc10 [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>
Asutosh Das0ef24812012-12-18 16:14:02 +053035
36#include "sdhci-pltfm.h"
37
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -070038#define SDHCI_VER_100 0x2B
Asutosh Das0ef24812012-12-18 16:14:02 +053039#define CORE_HC_MODE 0x78
40#define HC_MODE_EN 0x1
41
42#define CORE_POWER 0x0
43#define CORE_SW_RST (1 << 7)
44
45#define CORE_PWRCTL_STATUS 0xDC
46#define CORE_PWRCTL_MASK 0xE0
47#define CORE_PWRCTL_CLEAR 0xE4
48#define CORE_PWRCTL_CTL 0xE8
49
50#define CORE_PWRCTL_BUS_OFF 0x01
51#define CORE_PWRCTL_BUS_ON (1 << 1)
52#define CORE_PWRCTL_IO_LOW (1 << 2)
53#define CORE_PWRCTL_IO_HIGH (1 << 3)
54
55#define CORE_PWRCTL_BUS_SUCCESS 0x01
56#define CORE_PWRCTL_BUS_FAIL (1 << 1)
57#define CORE_PWRCTL_IO_SUCCESS (1 << 2)
58#define CORE_PWRCTL_IO_FAIL (1 << 3)
59
60#define INT_MASK 0xF
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -070061#define MAX_PHASES 16
62
63#define CORE_DLL_LOCK (1 << 7)
64#define CORE_DLL_EN (1 << 16)
65#define CORE_CDR_EN (1 << 17)
66#define CORE_CK_OUT_EN (1 << 18)
67#define CORE_CDR_EXT_EN (1 << 19)
68#define CORE_DLL_PDN (1 << 29)
69#define CORE_DLL_RST (1 << 30)
70#define CORE_DLL_CONFIG 0x100
71#define CORE_DLL_TEST_CTL 0x104
72#define CORE_DLL_STATUS 0x108
73
74#define CORE_VENDOR_SPEC 0x10C
75#define CORE_CLK_PWRSAVE (1 << 1)
76#define CORE_IO_PAD_PWR_SWITCH (1 << 16)
77
78static const u32 tuning_block_64[] = {
79 0x00FF0FFF, 0xCCC3CCFF, 0xFFCC3CC3, 0xEFFEFFFE,
80 0xDDFFDFFF, 0xFBFFFBFF, 0xFF7FFFBF, 0xEFBDF777,
81 0xF0FFF0FF, 0x3CCCFC0F, 0xCFCC33CC, 0xEEFFEFFF,
82 0xFDFFFDFF, 0xFFBFFFDF, 0xFFF7FFBB, 0xDE7B7FF7
83};
84
85static const u32 tuning_block_128[] = {
86 0xFF00FFFF, 0x0000FFFF, 0xCCCCFFFF, 0xCCCC33CC,
87 0xCC3333CC, 0xFFFFCCCC, 0xFFFFEEFF, 0xFFEEEEFF,
88 0xFFDDFFFF, 0xDDDDFFFF, 0xBBFFFFFF, 0xBBFFFFFF,
89 0xFFFFFFBB, 0xFFFFFF77, 0x77FF7777, 0xFFEEDDBB,
90 0x00FFFFFF, 0x00FFFFFF, 0xCCFFFF00, 0xCC33CCCC,
91 0x3333CCCC, 0xFFCCCCCC, 0xFFEEFFFF, 0xEEEEFFFF,
92 0xDDFFFFFF, 0xDDFFFFFF, 0xFFFFFFDD, 0xFFFFFFBB,
93 0xFFFFBBBB, 0xFFFF77FF, 0xFF7777FF, 0xEEDDBB77
94};
Asutosh Das0ef24812012-12-18 16:14:02 +053095
96/* This structure keeps information per regulator */
97struct sdhci_msm_reg_data {
98 /* voltage regulator handle */
99 struct regulator *reg;
100 /* regulator name */
101 const char *name;
102 /* voltage level to be set */
103 u32 low_vol_level;
104 u32 high_vol_level;
105 /* Load values for low power and high power mode */
106 u32 lpm_uA;
107 u32 hpm_uA;
108
109 /* is this regulator enabled? */
110 bool is_enabled;
111 /* is this regulator needs to be always on? */
112 bool is_always_on;
113 /* is low power mode setting required for this regulator? */
114 bool lpm_sup;
115 bool set_voltage_sup;
116};
117
118/*
119 * This structure keeps information for all the
120 * regulators required for a SDCC slot.
121 */
122struct sdhci_msm_slot_reg_data {
123 /* keeps VDD/VCC regulator info */
124 struct sdhci_msm_reg_data *vdd_data;
125 /* keeps VDD IO regulator info */
126 struct sdhci_msm_reg_data *vdd_io_data;
127};
128
129struct sdhci_msm_gpio {
130 u32 no;
131 const char *name;
132 bool is_enabled;
133};
134
135struct sdhci_msm_gpio_data {
136 struct sdhci_msm_gpio *gpio;
137 u8 size;
138};
139
140struct sdhci_msm_pin_data {
141 /*
142 * = 1 if controller pins are using gpios
143 * = 0 if controller has dedicated MSM pads
144 */
145 bool cfg_sts;
146 struct sdhci_msm_gpio_data *gpio_data;
147};
148
149struct sdhci_msm_pltfm_data {
150 /* Supported UHS-I Modes */
151 u32 caps;
152
153 /* More capabilities */
154 u32 caps2;
155
156 unsigned long mmc_bus_width;
157 u32 max_clk;
158 struct sdhci_msm_slot_reg_data *vreg_data;
159 bool nonremovable;
160 struct sdhci_msm_pin_data *pin_data;
161};
162
163struct sdhci_msm_host {
164 void __iomem *core_mem; /* MSM SDCC mapped address */
165 struct clk *clk; /* main SD/MMC bus clock */
166 struct clk *pclk; /* SDHC peripheral bus clock */
167 struct clk *bus_clk; /* SDHC bus voter clock */
168 struct sdhci_msm_pltfm_data *pdata;
169 struct mmc_host *mmc;
170 struct sdhci_pltfm_data sdhci_msm_pdata;
171 wait_queue_head_t pwr_irq_wait;
172};
173
174enum vdd_io_level {
175 /* set vdd_io_data->low_vol_level */
176 VDD_IO_LOW,
177 /* set vdd_io_data->high_vol_level */
178 VDD_IO_HIGH,
179 /*
180 * set whatever there in voltage_level (third argument) of
181 * sdhci_msm_set_vdd_io_vol() function.
182 */
183 VDD_IO_SET_LEVEL,
184};
185
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700186/* MSM platform specific tuning */
187static inline int msm_dll_poll_ck_out_en(struct sdhci_host *host,
188 u8 poll)
189{
190 int rc = 0;
191 u32 wait_cnt = 50;
192 u8 ck_out_en = 0;
193 struct mmc_host *mmc = host->mmc;
194
195 /* poll for CK_OUT_EN bit. max. poll time = 50us */
196 ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) &
197 CORE_CK_OUT_EN);
198
199 while (ck_out_en != poll) {
200 if (--wait_cnt == 0) {
201 pr_err("%s: %s: CK_OUT_EN bit is not %d\n",
202 mmc_hostname(mmc), __func__, poll);
203 rc = -ETIMEDOUT;
204 goto out;
205 }
206 udelay(1);
207
208 ck_out_en = !!(readl_relaxed(host->ioaddr +
209 CORE_DLL_CONFIG) & CORE_CK_OUT_EN);
210 }
211out:
212 return rc;
213}
214
215static int msm_config_cm_dll_phase(struct sdhci_host *host, u8 phase)
216{
217 int rc = 0;
218 u8 grey_coded_phase_table[] = {0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4,
219 0xC, 0xD, 0xF, 0xE, 0xA, 0xB, 0x9,
220 0x8};
221 unsigned long flags;
222 u32 config;
223 struct mmc_host *mmc = host->mmc;
224
225 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
226 spin_lock_irqsave(&host->lock, flags);
227
228 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
229 config &= ~(CORE_CDR_EN | CORE_CK_OUT_EN);
230 config |= (CORE_CDR_EXT_EN | CORE_DLL_EN);
231 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
232
233 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '0' */
234 rc = msm_dll_poll_ck_out_en(host, 0);
235 if (rc)
236 goto err_out;
237
238 /*
239 * Write the selected DLL clock output phase (0 ... 15)
240 * to CDR_SELEXT bit field of DLL_CONFIG register.
241 */
242 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
243 & ~(0xF << 20))
244 | (grey_coded_phase_table[phase] << 20)),
245 host->ioaddr + CORE_DLL_CONFIG);
246
247 /* Set CK_OUT_EN bit of DLL_CONFIG register to 1. */
248 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
249 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
250
251 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '1' */
252 rc = msm_dll_poll_ck_out_en(host, 1);
253 if (rc)
254 goto err_out;
255
256 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
257 config |= CORE_CDR_EN;
258 config &= ~CORE_CDR_EXT_EN;
259 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
260 goto out;
261
262err_out:
263 pr_err("%s: %s: Failed to set DLL phase: %d\n",
264 mmc_hostname(mmc), __func__, phase);
265out:
266 spin_unlock_irqrestore(&host->lock, flags);
267 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
268 return rc;
269}
270
271/*
272 * Find out the greatest range of consecuitive selected
273 * DLL clock output phases that can be used as sampling
274 * setting for SD3.0 UHS-I card read operation (in SDR104
275 * timing mode) or for eMMC4.5 card read operation (in HS200
276 * timing mode).
277 * Select the 3/4 of the range and configure the DLL with the
278 * selected DLL clock output phase.
279 */
280
281static int msm_find_most_appropriate_phase(struct sdhci_host *host,
282 u8 *phase_table, u8 total_phases)
283{
284 int ret;
285 u8 ranges[MAX_PHASES][MAX_PHASES] = { {0}, {0} };
286 u8 phases_per_row[MAX_PHASES] = {0};
287 int row_index = 0, col_index = 0, selected_row_index = 0, curr_max = 0;
288 int i, cnt, phase_0_raw_index = 0, phase_15_raw_index = 0;
289 bool phase_0_found = false, phase_15_found = false;
290 struct mmc_host *mmc = host->mmc;
291
292 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
293 if (!total_phases || (total_phases > MAX_PHASES)) {
294 pr_err("%s: %s: invalid argument: total_phases=%d\n",
295 mmc_hostname(mmc), __func__, total_phases);
296 return -EINVAL;
297 }
298
299 for (cnt = 0; cnt < total_phases; cnt++) {
300 ranges[row_index][col_index] = phase_table[cnt];
301 phases_per_row[row_index] += 1;
302 col_index++;
303
304 if ((cnt + 1) == total_phases) {
305 continue;
306 /* check if next phase in phase_table is consecutive or not */
307 } else if ((phase_table[cnt] + 1) != phase_table[cnt + 1]) {
308 row_index++;
309 col_index = 0;
310 }
311 }
312
313 if (row_index >= MAX_PHASES)
314 return -EINVAL;
315
316 /* Check if phase-0 is present in first valid window? */
317 if (!ranges[0][0]) {
318 phase_0_found = true;
319 phase_0_raw_index = 0;
320 /* Check if cycle exist between 2 valid windows */
321 for (cnt = 1; cnt <= row_index; cnt++) {
322 if (phases_per_row[cnt]) {
323 for (i = 0; i < phases_per_row[cnt]; i++) {
324 if (ranges[cnt][i] == 15) {
325 phase_15_found = true;
326 phase_15_raw_index = cnt;
327 break;
328 }
329 }
330 }
331 }
332 }
333
334 /* If 2 valid windows form cycle then merge them as single window */
335 if (phase_0_found && phase_15_found) {
336 /* number of phases in raw where phase 0 is present */
337 u8 phases_0 = phases_per_row[phase_0_raw_index];
338 /* number of phases in raw where phase 15 is present */
339 u8 phases_15 = phases_per_row[phase_15_raw_index];
340
341 if (phases_0 + phases_15 >= MAX_PHASES)
342 /*
343 * If there are more than 1 phase windows then total
344 * number of phases in both the windows should not be
345 * more than or equal to MAX_PHASES.
346 */
347 return -EINVAL;
348
349 /* Merge 2 cyclic windows */
350 i = phases_15;
351 for (cnt = 0; cnt < phases_0; cnt++) {
352 ranges[phase_15_raw_index][i] =
353 ranges[phase_0_raw_index][cnt];
354 if (++i >= MAX_PHASES)
355 break;
356 }
357
358 phases_per_row[phase_0_raw_index] = 0;
359 phases_per_row[phase_15_raw_index] = phases_15 + phases_0;
360 }
361
362 for (cnt = 0; cnt <= row_index; cnt++) {
363 if (phases_per_row[cnt] > curr_max) {
364 curr_max = phases_per_row[cnt];
365 selected_row_index = cnt;
366 }
367 }
368
369 i = ((curr_max * 3) / 4);
370 if (i)
371 i--;
372
373 ret = (int)ranges[selected_row_index][i];
374
375 if (ret >= MAX_PHASES) {
376 ret = -EINVAL;
377 pr_err("%s: %s: invalid phase selected=%d\n",
378 mmc_hostname(mmc), __func__, ret);
379 }
380
381 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
382 return ret;
383}
384
385static inline void msm_cm_dll_set_freq(struct sdhci_host *host)
386{
387 u32 mclk_freq = 0;
388
389 /* Program the MCLK value to MCLK_FREQ bit field */
390 if (host->clock <= 112000000)
391 mclk_freq = 0;
392 else if (host->clock <= 125000000)
393 mclk_freq = 1;
394 else if (host->clock <= 137000000)
395 mclk_freq = 2;
396 else if (host->clock <= 150000000)
397 mclk_freq = 3;
398 else if (host->clock <= 162000000)
399 mclk_freq = 4;
400 else if (host->clock <= 175000000)
401 mclk_freq = 5;
402 else if (host->clock <= 187000000)
403 mclk_freq = 6;
404 else if (host->clock <= 200000000)
405 mclk_freq = 7;
406
407 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
408 & ~(7 << 24)) | (mclk_freq << 24)),
409 host->ioaddr + CORE_DLL_CONFIG);
410}
411
412/* Initialize the DLL (Programmable Delay Line ) */
413static int msm_init_cm_dll(struct sdhci_host *host)
414{
415 struct mmc_host *mmc = host->mmc;
416 int rc = 0;
417 unsigned long flags;
418 u32 wait_cnt;
419
420 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
421 spin_lock_irqsave(&host->lock, flags);
422
423 /*
424 * Make sure that clock is always enabled when DLL
425 * tuning is in progress. Keeping PWRSAVE ON may
426 * turn off the clock. So let's disable the PWRSAVE
427 * here and re-enable it once tuning is completed.
428 */
429 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
430 & ~CORE_CLK_PWRSAVE),
431 host->ioaddr + CORE_VENDOR_SPEC);
432
433 /* Write 1 to DLL_RST bit of DLL_CONFIG register */
434 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
435 | CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
436
437 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
438 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
439 | CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
440 msm_cm_dll_set_freq(host);
441
442 /* Write 0 to DLL_RST bit of DLL_CONFIG register */
443 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
444 & ~CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
445
446 /* Write 0 to DLL_PDN bit of DLL_CONFIG register */
447 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
448 & ~CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
449
450 /* Set DLL_EN bit to 1. */
451 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
452 | CORE_DLL_EN), host->ioaddr + CORE_DLL_CONFIG);
453
454 /* Set CK_OUT_EN bit to 1. */
455 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
456 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
457
458 wait_cnt = 50;
459 /* Wait until DLL_LOCK bit of DLL_STATUS register becomes '1' */
460 while (!(readl_relaxed(host->ioaddr + CORE_DLL_STATUS) &
461 CORE_DLL_LOCK)) {
462 /* max. wait for 50us sec for LOCK bit to be set */
463 if (--wait_cnt == 0) {
464 pr_err("%s: %s: DLL failed to LOCK\n",
465 mmc_hostname(mmc), __func__);
466 rc = -ETIMEDOUT;
467 goto out;
468 }
469 /* wait for 1us before polling again */
470 udelay(1);
471 }
472
473out:
474 /* re-enable PWRSAVE */
475 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
476 CORE_CLK_PWRSAVE),
477 host->ioaddr + CORE_VENDOR_SPEC);
478 spin_unlock_irqrestore(&host->lock, flags);
479 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
480 return rc;
481}
482
483int sdhci_msm_execute_tuning(struct sdhci_host *host, u32 opcode)
484{
485 unsigned long flags;
486 u8 phase, *data_buf, tuned_phases[16], tuned_phase_cnt = 0;
487 const u32 *tuning_block_pattern = tuning_block_64;
488 int size = sizeof(tuning_block_64); /* Tuning pattern size in bytes */
489 int rc;
490 struct mmc_host *mmc = host->mmc;
491
492 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
493 /* Tuning is only required for SDR104 modes */
494 spin_lock_irqsave(&host->lock, flags);
495
496 if ((opcode == MMC_SEND_TUNING_BLOCK_HS200) &&
497 (mmc->ios.bus_width == MMC_BUS_WIDTH_8)) {
498 tuning_block_pattern = tuning_block_128;
499 size = sizeof(tuning_block_128);
500 }
501 spin_unlock_irqrestore(&host->lock, flags);
502
503 /* first of all reset the tuning block */
504 rc = msm_init_cm_dll(host);
505 if (rc)
506 goto out;
507
508 data_buf = kmalloc(size, GFP_KERNEL);
509 if (!data_buf) {
510 rc = -ENOMEM;
511 goto out;
512 }
513
514 phase = 0;
515 do {
516 struct mmc_command cmd = {0};
517 struct mmc_data data = {0};
518 struct mmc_request mrq = {
519 .cmd = &cmd,
520 .data = &data
521 };
522 struct scatterlist sg;
523
524 /* set the phase in delay line hw block */
525 rc = msm_config_cm_dll_phase(host, phase);
526 if (rc)
527 goto kfree;
528
529 cmd.opcode = opcode;
530 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
531
532 data.blksz = size;
533 data.blocks = 1;
534 data.flags = MMC_DATA_READ;
535 data.timeout_ns = 1000 * 1000 * 1000; /* 1 sec */
536
537 data.sg = &sg;
538 data.sg_len = 1;
539 sg_init_one(&sg, data_buf, size);
540 memset(data_buf, 0, size);
541 mmc_wait_for_req(mmc, &mrq);
542
543 if (!cmd.error && !data.error &&
544 !memcmp(data_buf, tuning_block_pattern, size)) {
545 /* tuning is successful at this tuning point */
546 tuned_phases[tuned_phase_cnt++] = phase;
547 pr_debug("%s: %s: found good phase = %d\n",
548 mmc_hostname(mmc), __func__, phase);
549 }
550 } while (++phase < 16);
551
552 if (tuned_phase_cnt) {
553 rc = msm_find_most_appropriate_phase(host, tuned_phases,
554 tuned_phase_cnt);
555 if (rc < 0)
556 goto kfree;
557 else
558 phase = (u8)rc;
559
560 /*
561 * Finally set the selected phase in delay
562 * line hw block.
563 */
564 rc = msm_config_cm_dll_phase(host, phase);
565 if (rc)
566 goto kfree;
567 pr_debug("%s: %s: finally setting the tuning phase to %d\n",
568 mmc_hostname(mmc), __func__, phase);
569 } else {
570 /* tuning failed */
571 pr_err("%s: %s: no tuning point found\n",
572 mmc_hostname(mmc), __func__);
573 rc = -EAGAIN;
574 }
575
576kfree:
577 kfree(data_buf);
578out:
579 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
580 return rc;
581}
582
Asutosh Das0ef24812012-12-18 16:14:02 +0530583static int sdhci_msm_setup_gpio(struct sdhci_msm_pltfm_data *pdata, bool enable)
584{
585 struct sdhci_msm_gpio_data *curr;
586 int i, ret = 0;
587
588 curr = pdata->pin_data->gpio_data;
589 for (i = 0; i < curr->size; i++) {
590 if (!gpio_is_valid(curr->gpio[i].no)) {
591 ret = -EINVAL;
592 pr_err("%s: Invalid gpio = %d\n", __func__,
593 curr->gpio[i].no);
594 goto free_gpios;
595 }
596 if (enable) {
597 ret = gpio_request(curr->gpio[i].no,
598 curr->gpio[i].name);
599 if (ret) {
600 pr_err("%s: gpio_request(%d, %s) failed %d\n",
601 __func__, curr->gpio[i].no,
602 curr->gpio[i].name, ret);
603 goto free_gpios;
604 }
605 curr->gpio[i].is_enabled = true;
606 } else {
607 gpio_free(curr->gpio[i].no);
608 curr->gpio[i].is_enabled = false;
609 }
610 }
611 return ret;
612
613free_gpios:
614 for (i--; i >= 0; i--) {
615 gpio_free(curr->gpio[i].no);
616 curr->gpio[i].is_enabled = false;
617 }
618 return ret;
619}
620
621static int sdhci_msm_setup_pins(struct sdhci_msm_pltfm_data *pdata, bool enable)
622{
623 int ret = 0;
624
625 if (!pdata->pin_data || (pdata->pin_data->cfg_sts == enable))
626 return 0;
627
628 ret = sdhci_msm_setup_gpio(pdata, enable);
629 if (!ret)
630 pdata->pin_data->cfg_sts = enable;
631
632 return ret;
633}
634
635#define MAX_PROP_SIZE 32
636static int sdhci_msm_dt_parse_vreg_info(struct device *dev,
637 struct sdhci_msm_reg_data **vreg_data, const char *vreg_name)
638{
639 int len, ret = 0;
640 const __be32 *prop;
641 char prop_name[MAX_PROP_SIZE];
642 struct sdhci_msm_reg_data *vreg;
643 struct device_node *np = dev->of_node;
644
645 snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", vreg_name);
646 if (!of_parse_phandle(np, prop_name, 0)) {
647 dev_err(dev, "No vreg data found for %s\n", vreg_name);
648 ret = -EINVAL;
649 return ret;
650 }
651
652 vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
653 if (!vreg) {
654 dev_err(dev, "No memory for vreg: %s\n", vreg_name);
655 ret = -ENOMEM;
656 return ret;
657 }
658
659 vreg->name = vreg_name;
660
661 snprintf(prop_name, MAX_PROP_SIZE,
662 "qcom,%s-always-on", vreg_name);
663 if (of_get_property(np, prop_name, NULL))
664 vreg->is_always_on = true;
665
666 snprintf(prop_name, MAX_PROP_SIZE,
667 "qcom,%s-lpm-sup", vreg_name);
668 if (of_get_property(np, prop_name, NULL))
669 vreg->lpm_sup = true;
670
671 snprintf(prop_name, MAX_PROP_SIZE,
672 "qcom,%s-voltage-level", vreg_name);
673 prop = of_get_property(np, prop_name, &len);
674 if (!prop || (len != (2 * sizeof(__be32)))) {
675 dev_warn(dev, "%s %s property\n",
676 prop ? "invalid format" : "no", prop_name);
677 } else {
678 vreg->low_vol_level = be32_to_cpup(&prop[0]);
679 vreg->high_vol_level = be32_to_cpup(&prop[1]);
680 }
681
682 snprintf(prop_name, MAX_PROP_SIZE,
683 "qcom,%s-current-level", vreg_name);
684 prop = of_get_property(np, prop_name, &len);
685 if (!prop || (len != (2 * sizeof(__be32)))) {
686 dev_warn(dev, "%s %s property\n",
687 prop ? "invalid format" : "no", prop_name);
688 } else {
689 vreg->lpm_uA = be32_to_cpup(&prop[0]);
690 vreg->hpm_uA = be32_to_cpup(&prop[1]);
691 }
692
693 *vreg_data = vreg;
694 dev_dbg(dev, "%s: %s %s vol=[%d %d]uV, curr=[%d %d]uA\n",
695 vreg->name, vreg->is_always_on ? "always_on," : "",
696 vreg->lpm_sup ? "lpm_sup," : "", vreg->low_vol_level,
697 vreg->high_vol_level, vreg->lpm_uA, vreg->hpm_uA);
698
699 return ret;
700}
701
702#define GPIO_NAME_MAX_LEN 32
703static int sdhci_msm_dt_parse_gpio_info(struct device *dev,
704 struct sdhci_msm_pltfm_data *pdata)
705{
706 int ret = 0, cnt, i;
707 struct sdhci_msm_pin_data *pin_data;
708 struct device_node *np = dev->of_node;
709
710 pin_data = devm_kzalloc(dev, sizeof(*pin_data), GFP_KERNEL);
711 if (!pin_data) {
712 dev_err(dev, "No memory for pin_data\n");
713 ret = -ENOMEM;
714 goto out;
715 }
716
717 cnt = of_gpio_count(np);
718 if (cnt > 0) {
719 pin_data->gpio_data = devm_kzalloc(dev,
720 sizeof(struct sdhci_msm_gpio_data), GFP_KERNEL);
721 if (!pin_data->gpio_data) {
722 dev_err(dev, "No memory for gpio_data\n");
723 ret = -ENOMEM;
724 goto out;
725 }
726 pin_data->gpio_data->size = cnt;
727 pin_data->gpio_data->gpio = devm_kzalloc(dev, cnt *
728 sizeof(struct sdhci_msm_gpio), GFP_KERNEL);
729
730 if (!pin_data->gpio_data->gpio) {
731 dev_err(dev, "No memory for gpio\n");
732 ret = -ENOMEM;
733 goto out;
734 }
735
736 for (i = 0; i < cnt; i++) {
737 const char *name = NULL;
738 char result[GPIO_NAME_MAX_LEN];
739 pin_data->gpio_data->gpio[i].no = of_get_gpio(np, i);
740 of_property_read_string_index(np,
741 "qcom,gpio-names", i, &name);
742
743 snprintf(result, GPIO_NAME_MAX_LEN, "%s-%s",
744 dev_name(dev), name ? name : "?");
745 pin_data->gpio_data->gpio[i].name = result;
746 dev_dbg(dev, "%s: gpio[%s] = %d\n", __func__,
747 pin_data->gpio_data->gpio[i].name,
748 pin_data->gpio_data->gpio[i].no);
749 pdata->pin_data = pin_data;
750 }
751 }
752
753out:
754 if (ret)
755 dev_err(dev, "%s failed with err %d\n", __func__, ret);
756 return ret;
757}
758
759/* Parse platform data */
760static struct sdhci_msm_pltfm_data *sdhci_msm_populate_pdata(struct device *dev)
761{
762 struct sdhci_msm_pltfm_data *pdata = NULL;
763 struct device_node *np = dev->of_node;
764 u32 bus_width = 0;
765 int len, i;
766
767 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
768 if (!pdata) {
769 dev_err(dev, "failed to allocate memory for platform data\n");
770 goto out;
771 }
772
773 of_property_read_u32(np, "qcom,bus-width", &bus_width);
774 if (bus_width == 8)
775 pdata->mmc_bus_width = MMC_CAP_8_BIT_DATA;
776 else if (bus_width == 4)
777 pdata->mmc_bus_width = MMC_CAP_4_BIT_DATA;
778 else {
779 dev_notice(dev, "invalid bus-width, default to 1-bit mode\n");
780 pdata->mmc_bus_width = 0;
781 }
782
783 pdata->vreg_data = devm_kzalloc(dev, sizeof(struct
784 sdhci_msm_slot_reg_data),
785 GFP_KERNEL);
786 if (!pdata->vreg_data) {
787 dev_err(dev, "failed to allocate memory for vreg data\n");
788 goto out;
789 }
790
791 if (sdhci_msm_dt_parse_vreg_info(dev, &pdata->vreg_data->vdd_data,
792 "vdd")) {
793 dev_err(dev, "failed parsing vdd data\n");
794 goto out;
795 }
796 if (sdhci_msm_dt_parse_vreg_info(dev,
797 &pdata->vreg_data->vdd_io_data,
798 "vdd-io")) {
799 dev_err(dev, "failed parsing vdd-io data\n");
800 goto out;
801 }
802
803 if (sdhci_msm_dt_parse_gpio_info(dev, pdata)) {
804 dev_err(dev, "failed parsing gpio data\n");
805 goto out;
806 }
807
808 of_property_read_u32(np, "qcom,max-clk-rate", &pdata->max_clk);
809
810 len = of_property_count_strings(np, "qcom,bus-speed-mode");
811
812 for (i = 0; i < len; i++) {
813 const char *name = NULL;
814
815 of_property_read_string_index(np,
816 "qcom,bus-speed-mode", i, &name);
817 if (!name)
818 continue;
819
820 if (!strncmp(name, "HS200_1p8v", sizeof("HS200_1p8v")))
821 pdata->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
822 else if (!strncmp(name, "HS200_1p2v", sizeof("HS200_1p2v")))
823 pdata->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
824 else if (!strncmp(name, "DDR_1p8v", sizeof("DDR_1p8v")))
825 pdata->caps |= MMC_CAP_1_8V_DDR
826 | MMC_CAP_UHS_DDR50;
827 else if (!strncmp(name, "DDR_1p2v", sizeof("DDR_1p2v")))
828 pdata->caps |= MMC_CAP_1_2V_DDR
829 | MMC_CAP_UHS_DDR50;
830 }
831
832 if (of_get_property(np, "qcom,nonremovable", NULL))
833 pdata->nonremovable = true;
834
835 return pdata;
836out:
837 return NULL;
838}
839
840/* Regulator utility functions */
841static int sdhci_msm_vreg_init_reg(struct device *dev,
842 struct sdhci_msm_reg_data *vreg)
843{
844 int ret = 0;
845
846 /* check if regulator is already initialized? */
847 if (vreg->reg)
848 goto out;
849
850 /* Get the regulator handle */
851 vreg->reg = devm_regulator_get(dev, vreg->name);
852 if (IS_ERR(vreg->reg)) {
853 ret = PTR_ERR(vreg->reg);
854 pr_err("%s: devm_regulator_get(%s) failed. ret=%d\n",
855 __func__, vreg->name, ret);
856 goto out;
857 }
858
859 /* sanity check */
860 if (!vreg->high_vol_level || !vreg->hpm_uA) {
861 pr_err("%s: %s invalid constraints specified\n",
862 __func__, vreg->name);
863 ret = -EINVAL;
864 }
865
866out:
867 return ret;
868}
869
870static void sdhci_msm_vreg_deinit_reg(struct sdhci_msm_reg_data *vreg)
871{
872 if (vreg->reg)
873 devm_regulator_put(vreg->reg);
874}
875
876static int sdhci_msm_vreg_set_optimum_mode(struct sdhci_msm_reg_data
877 *vreg, int uA_load)
878{
879 int ret = 0;
880
881 /*
882 * regulators that do not support regulator_set_voltage also
883 * do not support regulator_set_optimum_mode
884 */
885 if (vreg->set_voltage_sup) {
886 ret = regulator_set_load(vreg->reg, uA_load);
887 if (ret < 0)
888 pr_err("%s: regulator_set_load(reg=%s,uA_load=%d) failed. ret=%d\n",
889 __func__, vreg->name, uA_load, ret);
890 else
891 /*
892 * regulator_set_load() can return non zero
893 * value even for success case.
894 */
895 ret = 0;
896 }
897 return ret;
898}
899
900static int sdhci_msm_vreg_set_voltage(struct sdhci_msm_reg_data *vreg,
901 int min_uV, int max_uV)
902{
903 int ret = 0;
904
905 ret = regulator_set_voltage(vreg->reg, min_uV, max_uV);
906 if (ret) {
907 pr_err("%s: regulator_set_voltage(%s)failed. min_uV=%d,max_uV=%d,ret=%d\n",
908 __func__, vreg->name, min_uV, max_uV, ret);
909 }
910
911 return ret;
912}
913
914static int sdhci_msm_vreg_enable(struct sdhci_msm_reg_data *vreg)
915{
916 int ret = 0;
917
918 /* Put regulator in HPM (high power mode) */
919 ret = sdhci_msm_vreg_set_optimum_mode(vreg, vreg->hpm_uA);
920 if (ret < 0)
921 return ret;
922
923 if (!vreg->is_enabled) {
924 /* Set voltage level */
925 ret = sdhci_msm_vreg_set_voltage(vreg, vreg->high_vol_level,
926 vreg->high_vol_level);
927 if (ret)
928 return ret;
929 }
930 ret = regulator_enable(vreg->reg);
931 if (ret) {
932 pr_err("%s: regulator_enable(%s) failed. ret=%d\n",
933 __func__, vreg->name, ret);
934 return ret;
935 }
936 vreg->is_enabled = true;
937 return ret;
938}
939
940static int sdhci_msm_vreg_disable(struct sdhci_msm_reg_data *vreg)
941{
942 int ret = 0;
943
944 /* Never disable regulator marked as always_on */
945 if (vreg->is_enabled && !vreg->is_always_on) {
946 ret = regulator_disable(vreg->reg);
947 if (ret) {
948 pr_err("%s: regulator_disable(%s) failed. ret=%d\n",
949 __func__, vreg->name, ret);
950 goto out;
951 }
952 vreg->is_enabled = false;
953
954 ret = sdhci_msm_vreg_set_optimum_mode(vreg, 0);
955 if (ret < 0)
956 goto out;
957
958 /* Set min. voltage level to 0 */
959 ret = sdhci_msm_vreg_set_voltage(vreg, 0, vreg->high_vol_level);
960 if (ret)
961 goto out;
962 } else if (vreg->is_enabled && vreg->is_always_on) {
963 if (vreg->lpm_sup) {
964 /* Put always_on regulator in LPM (low power mode) */
965 ret = sdhci_msm_vreg_set_optimum_mode(vreg,
966 vreg->lpm_uA);
967 if (ret < 0)
968 goto out;
969 }
970 }
971out:
972 return ret;
973}
974
975static int sdhci_msm_setup_vreg(struct sdhci_msm_pltfm_data *pdata,
976 bool enable, bool is_init)
977{
978 int ret = 0, i;
979 struct sdhci_msm_slot_reg_data *curr_slot;
980 struct sdhci_msm_reg_data *vreg_table[2];
981
982 curr_slot = pdata->vreg_data;
983 if (!curr_slot) {
984 pr_debug("%s: vreg info unavailable,assuming the slot is powered by always on domain\n",
985 __func__);
986 goto out;
987 }
988
989 vreg_table[0] = curr_slot->vdd_data;
990 vreg_table[1] = curr_slot->vdd_io_data;
991
992 for (i = 0; i < ARRAY_SIZE(vreg_table); i++) {
993 if (vreg_table[i]) {
994 if (enable)
995 ret = sdhci_msm_vreg_enable(vreg_table[i]);
996 else
997 ret = sdhci_msm_vreg_disable(vreg_table[i]);
998 if (ret)
999 goto out;
1000 }
1001 }
1002out:
1003 return ret;
1004}
1005
1006/*
1007 * Reset vreg by ensuring it is off during probe. A call
1008 * to enable vreg is needed to balance disable vreg
1009 */
1010static int sdhci_msm_vreg_reset(struct sdhci_msm_pltfm_data *pdata)
1011{
1012 int ret;
1013
1014 ret = sdhci_msm_setup_vreg(pdata, 1, true);
1015 if (ret)
1016 return ret;
1017 ret = sdhci_msm_setup_vreg(pdata, 0, true);
1018 return ret;
1019}
1020
1021/* This init function should be called only once for each SDHC slot */
1022static int sdhci_msm_vreg_init(struct device *dev,
1023 struct sdhci_msm_pltfm_data *pdata,
1024 bool is_init)
1025{
1026 int ret = 0;
1027 struct sdhci_msm_slot_reg_data *curr_slot;
1028 struct sdhci_msm_reg_data *curr_vdd_reg, *curr_vdd_io_reg;
1029
1030 curr_slot = pdata->vreg_data;
1031 if (!curr_slot)
1032 goto out;
1033
1034 curr_vdd_reg = curr_slot->vdd_data;
1035 curr_vdd_io_reg = curr_slot->vdd_io_data;
1036
1037 if (!is_init)
1038 /* Deregister all regulators from regulator framework */
1039 goto vdd_io_reg_deinit;
1040
1041 /*
1042 * Get the regulator handle from voltage regulator framework
1043 * and then try to set the voltage level for the regulator
1044 */
1045 if (curr_vdd_reg) {
1046 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_reg);
1047 if (ret)
1048 goto out;
1049 }
1050 if (curr_vdd_io_reg) {
1051 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_io_reg);
1052 if (ret)
1053 goto vdd_reg_deinit;
1054 }
1055 ret = sdhci_msm_vreg_reset(pdata);
1056 if (ret)
1057 dev_err(dev, "vreg reset failed (%d)\n", ret);
1058 goto out;
1059
1060vdd_io_reg_deinit:
1061 if (curr_vdd_io_reg)
1062 sdhci_msm_vreg_deinit_reg(curr_vdd_io_reg);
1063vdd_reg_deinit:
1064 if (curr_vdd_reg)
1065 sdhci_msm_vreg_deinit_reg(curr_vdd_reg);
1066out:
1067 return ret;
1068}
1069
1070
1071static int sdhci_msm_set_vdd_io_vol(struct sdhci_msm_pltfm_data *pdata,
1072 enum vdd_io_level level,
1073 unsigned int voltage_level)
1074{
1075 int ret = 0;
1076 int set_level;
1077 struct sdhci_msm_reg_data *vdd_io_reg;
1078
1079 if (!pdata->vreg_data)
1080 return ret;
1081
1082 vdd_io_reg = pdata->vreg_data->vdd_io_data;
1083 if (vdd_io_reg && vdd_io_reg->is_enabled) {
1084 switch (level) {
1085 case VDD_IO_LOW:
1086 set_level = vdd_io_reg->low_vol_level;
1087 break;
1088 case VDD_IO_HIGH:
1089 set_level = vdd_io_reg->high_vol_level;
1090 break;
1091 case VDD_IO_SET_LEVEL:
1092 set_level = voltage_level;
1093 break;
1094 default:
1095 pr_err("%s: invalid argument level = %d",
1096 __func__, level);
1097 ret = -EINVAL;
1098 return ret;
1099 }
1100 ret = sdhci_msm_vreg_set_voltage(vdd_io_reg, set_level,
1101 set_level);
1102 }
1103 return ret;
1104}
1105
1106static irqreturn_t sdhci_msm_pwr_irq(int irq, void *data)
1107{
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001108 struct sdhci_host *host = (struct sdhci_host *)data;
1109 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1110 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Das0ef24812012-12-18 16:14:02 +05301111 u8 irq_status = 0;
1112 u8 irq_ack = 0;
1113 int ret = 0;
1114
1115 irq_status = readb_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
1116 pr_debug("%s: Received IRQ(%d), status=0x%x\n",
1117 mmc_hostname(msm_host->mmc), irq, irq_status);
1118
1119 /* Clear the interrupt */
1120 writeb_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
1121 /*
1122 * SDHC has core_mem and hc_mem device memory and these memory
1123 * addresses do not fall within 1KB region. Hence, any update to
1124 * core_mem address space would require an mb() to ensure this gets
1125 * completed before its next update to registers within hc_mem.
1126 */
1127 mb();
1128
1129 /* Handle BUS ON/OFF*/
1130 if (irq_status & CORE_PWRCTL_BUS_ON) {
1131 ret = sdhci_msm_setup_vreg(msm_host->pdata, true, false);
1132 if (!ret)
1133 ret = sdhci_msm_setup_pins(msm_host->pdata, true);
1134 if (ret)
1135 irq_ack |= CORE_PWRCTL_BUS_FAIL;
1136 else
1137 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
1138 }
1139 if (irq_status & CORE_PWRCTL_BUS_OFF) {
1140 ret = sdhci_msm_setup_vreg(msm_host->pdata, false, false);
1141 if (!ret)
1142 ret = sdhci_msm_setup_pins(msm_host->pdata, false);
1143 if (ret)
1144 irq_ack |= CORE_PWRCTL_BUS_FAIL;
1145 else
1146 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
1147 }
1148 /* Handle IO LOW/HIGH */
1149 if (irq_status & CORE_PWRCTL_IO_LOW) {
1150 /* Switch voltage Low */
1151 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_LOW, 0);
1152 if (ret)
1153 irq_ack |= CORE_PWRCTL_IO_FAIL;
1154 else
1155 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
1156 }
1157 if (irq_status & CORE_PWRCTL_IO_HIGH) {
1158 /* Switch voltage High */
1159 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_HIGH, 0);
1160 if (ret)
1161 irq_ack |= CORE_PWRCTL_IO_FAIL;
1162 else
1163 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
1164 }
1165
1166 /* ACK status to the core */
1167 writeb_relaxed(irq_ack, (msm_host->core_mem + CORE_PWRCTL_CTL));
1168 /*
1169 * SDHC has core_mem and hc_mem device memory and these memory
1170 * addresses do not fall within 1KB region. Hence, any update to
1171 * core_mem address space would require an mb() to ensure this gets
1172 * completed before its next update to registers within hc_mem.
1173 */
1174 mb();
1175
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001176 if (irq_status & CORE_PWRCTL_IO_HIGH)
1177 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
1178 ~CORE_IO_PAD_PWR_SWITCH),
1179 host->ioaddr + CORE_VENDOR_SPEC);
1180 if (irq_status & CORE_PWRCTL_IO_LOW)
1181 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
1182 CORE_IO_PAD_PWR_SWITCH),
1183 host->ioaddr + CORE_VENDOR_SPEC);
1184 mb();
1185
Asutosh Das0ef24812012-12-18 16:14:02 +05301186 pr_debug("%s: Handled IRQ(%d), ret=%d, ack=0x%x\n",
1187 mmc_hostname(msm_host->mmc), irq, ret, irq_ack);
1188 wake_up_interruptible(&msm_host->pwr_irq_wait);
1189 return IRQ_HANDLED;
1190}
1191
1192static void sdhci_msm_check_power_status(struct sdhci_host *host)
1193{
1194 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1195 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1196 int ret = 0;
1197
1198 pr_debug("%s: %s: power status before waiting 0x%x\n",
1199 mmc_hostname(host->mmc), __func__,
1200 readb_relaxed(msm_host->core_mem + CORE_PWRCTL_CTL));
1201
1202 ret = wait_event_interruptible(msm_host->pwr_irq_wait,
1203 (readb_relaxed(msm_host->core_mem +
1204 CORE_PWRCTL_CTL)) != 0x0);
1205 if (ret)
1206 pr_warning("%s: %s: returned due to error %d\n",
1207 mmc_hostname(host->mmc), __func__, ret);
1208 pr_debug("%s: %s: ret %d power status after handling power IRQ 0x%x\n",
1209 mmc_hostname(host->mmc), __func__, ret,
1210 readb_relaxed(msm_host->core_mem + CORE_PWRCTL_CTL));
1211}
1212
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001213static void sdhci_msm_toggle_cdr(struct sdhci_host *host, bool enable)
1214{
1215 if (enable)
1216 writel_relaxed((readl_relaxed(host->ioaddr +
1217 CORE_DLL_CONFIG) | CORE_CDR_EN),
1218 host->ioaddr + CORE_DLL_CONFIG);
1219 else
1220 writel_relaxed((readl_relaxed(host->ioaddr +
1221 CORE_DLL_CONFIG) & ~CORE_CDR_EN),
1222 host->ioaddr + CORE_DLL_CONFIG);
1223}
1224
Asutosh Das0ef24812012-12-18 16:14:02 +05301225static struct sdhci_ops sdhci_msm_ops = {
1226 .check_power_status = sdhci_msm_check_power_status,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001227 .platform_execute_tuning = sdhci_msm_execute_tuning,
1228 .toggle_cdr = sdhci_msm_toggle_cdr,
Asutosh Das0ef24812012-12-18 16:14:02 +05301229};
1230
1231static int sdhci_msm_probe(struct platform_device *pdev)
1232{
1233 struct sdhci_host *host;
1234 struct sdhci_pltfm_host *pltfm_host;
1235 struct sdhci_msm_host *msm_host;
1236 struct resource *core_memres = NULL;
1237 int ret = 0, pwr_irq = 0, dead = 0;
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07001238 u32 host_version;
Asutosh Das0ef24812012-12-18 16:14:02 +05301239
1240 pr_debug("%s: Enter %s\n", dev_name(&pdev->dev), __func__);
1241 msm_host = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_msm_host),
1242 GFP_KERNEL);
1243 if (!msm_host) {
1244 ret = -ENOMEM;
1245 goto out;
1246 }
1247 init_waitqueue_head(&msm_host->pwr_irq_wait);
1248
1249 msm_host->sdhci_msm_pdata.ops = &sdhci_msm_ops;
1250 host = sdhci_pltfm_init(pdev, &msm_host->sdhci_msm_pdata, 0);
1251 if (IS_ERR(host)) {
1252 ret = PTR_ERR(host);
1253 goto out;
1254 }
1255
1256 pltfm_host = sdhci_priv(host);
1257 pltfm_host->priv = msm_host;
1258 msm_host->mmc = host->mmc;
1259
1260 /* Extract platform data */
1261 if (pdev->dev.of_node) {
1262 msm_host->pdata = sdhci_msm_populate_pdata(&pdev->dev);
1263 if (!msm_host->pdata) {
1264 dev_err(&pdev->dev, "DT parsing error\n");
1265 goto pltfm_free;
1266 }
1267 } else {
1268 dev_err(&pdev->dev, "No device tree node\n");
1269 goto pltfm_free;
1270 }
1271
1272 /* Setup Clocks */
1273
1274 /* Setup SDCC bus voter clock. */
1275 msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus_clk");
1276 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
1277 /* Vote for max. clk rate for max. performance */
1278 ret = clk_set_rate(msm_host->bus_clk, INT_MAX);
1279 if (ret)
1280 goto pltfm_free;
1281 ret = clk_prepare_enable(msm_host->bus_clk);
1282 if (ret)
1283 goto pltfm_free;
1284 }
1285
1286 /* Setup main peripheral bus clock */
1287 msm_host->pclk = devm_clk_get(&pdev->dev, "iface_clk");
1288 if (!IS_ERR(msm_host->pclk)) {
1289 ret = clk_prepare_enable(msm_host->pclk);
1290 if (ret)
1291 goto bus_clk_disable;
1292 }
1293
1294 /* Setup SDC MMC clock */
1295 msm_host->clk = devm_clk_get(&pdev->dev, "core_clk");
1296 if (IS_ERR(msm_host->clk)) {
1297 ret = PTR_ERR(msm_host->clk);
1298 goto pclk_disable;
1299 }
1300
1301 ret = clk_prepare_enable(msm_host->clk);
1302 if (ret)
1303 goto pclk_disable;
1304
1305 /* Setup regulators */
1306 ret = sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, true);
1307 if (ret) {
1308 dev_err(&pdev->dev, "Regulator setup failed (%d)\n", ret);
1309 goto clk_disable;
1310 }
1311
1312 /* Reset the core and Enable SDHC mode */
1313 core_memres = platform_get_resource_byname(pdev,
1314 IORESOURCE_MEM, "core_mem");
1315 msm_host->core_mem = devm_ioremap(&pdev->dev, core_memres->start,
1316 resource_size(core_memres));
1317
1318 if (!msm_host->core_mem) {
1319 dev_err(&pdev->dev, "Failed to remap registers\n");
1320 ret = -ENOMEM;
1321 goto vreg_deinit;
1322 }
1323
1324 /* Set SW_RST bit in POWER register (Offset 0x0) */
1325 writel_relaxed(CORE_SW_RST, msm_host->core_mem + CORE_POWER);
1326 /* Set HC_MODE_EN bit in HC_MODE register */
1327 writel_relaxed(HC_MODE_EN, (msm_host->core_mem + CORE_HC_MODE));
1328
1329 /*
1330 * Following are the deviations from SDHC spec v3.0 -
1331 * 1. Card detection is handled using separate GPIO.
1332 * 2. Bus power control is handled by interacting with PMIC.
1333 */
1334 host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
1335 host->quirks |= SDHCI_QUIRK_SINGLE_POWER_WRITE;
1336
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07001337 host_version = readl_relaxed((host->ioaddr + SDHCI_HOST_VERSION));
1338 dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n",
1339 host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >>
1340 SDHCI_VENDOR_VER_SHIFT));
1341 if (((host_version & SDHCI_VENDOR_VER_MASK) >>
1342 SDHCI_VENDOR_VER_SHIFT) == SDHCI_VER_100) {
1343 /*
1344 * Add 40us delay in interrupt handler when
1345 * operating at initialization frequency(400KHz).
1346 */
1347 host->quirks2 |= SDHCI_QUIRK2_SLOW_INT_CLR;
1348 /*
1349 * Set Software Reset for DAT line in Software
1350 * Reset Register (Bit 2).
1351 */
1352 host->quirks2 |= SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT;
1353 }
1354
1355 /* Setup PWRCTL irq */
Asutosh Das0ef24812012-12-18 16:14:02 +05301356 pwr_irq = platform_get_irq_byname(pdev, "pwr_irq");
1357 if (pwr_irq < 0) {
1358 dev_err(&pdev->dev, "Failed to get pwr_irq by name (%d)\n",
1359 pwr_irq);
1360 goto vreg_deinit;
1361 }
1362 ret = devm_request_threaded_irq(&pdev->dev, pwr_irq, NULL,
1363 sdhci_msm_pwr_irq, IRQF_ONESHOT,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001364 dev_name(&pdev->dev), host);
Asutosh Das0ef24812012-12-18 16:14:02 +05301365 if (ret) {
1366 dev_err(&pdev->dev, "Request threaded irq(%d) failed (%d)\n",
1367 pwr_irq, ret);
1368 goto vreg_deinit;
1369 }
1370
1371 /* Enable pwr irq interrupts */
1372 writel_relaxed(INT_MASK, (msm_host->core_mem + CORE_PWRCTL_MASK));
1373
1374 /* Set host capabilities */
1375 msm_host->mmc->caps |= msm_host->pdata->mmc_bus_width;
1376 msm_host->mmc->caps |= msm_host->pdata->caps;
1377
1378 msm_host->mmc->caps2 |= msm_host->pdata->caps2;
1379 msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR;
1380 msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR_CONTROL;
1381
1382 if (msm_host->pdata->nonremovable)
1383 msm_host->mmc->caps |= MMC_CAP_NONREMOVABLE;
1384
1385 ret = sdhci_add_host(host);
1386 if (ret) {
1387 dev_err(&pdev->dev, "Add host failed (%d)\n", ret);
1388 goto vreg_deinit;
1389 }
1390
1391 /* Set core clk rate, optionally override from dts */
1392 if (msm_host->pdata->max_clk)
1393 host->max_clk = msm_host->pdata->max_clk;
1394 ret = clk_set_rate(msm_host->clk, host->max_clk);
1395 if (ret) {
1396 dev_err(&pdev->dev, "MClk rate set failed (%d)\n", ret);
1397 goto remove_host;
1398 }
1399
1400 /* Successful initialization */
1401 goto out;
1402
1403remove_host:
1404 dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
1405 sdhci_remove_host(host, dead);
1406vreg_deinit:
1407 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
1408clk_disable:
1409 if (!IS_ERR(msm_host->clk))
1410 clk_disable_unprepare(msm_host->clk);
1411pclk_disable:
1412 if (!IS_ERR(msm_host->pclk))
1413 clk_disable_unprepare(msm_host->pclk);
1414bus_clk_disable:
1415 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
1416 clk_disable_unprepare(msm_host->bus_clk);
1417pltfm_free:
1418 sdhci_pltfm_free(pdev);
1419out:
1420 pr_debug("%s: Exit %s\n", dev_name(&pdev->dev), __func__);
1421 return ret;
1422}
1423
1424static int sdhci_msm_remove(struct platform_device *pdev)
1425{
1426 struct sdhci_host *host = platform_get_drvdata(pdev);
1427 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1428 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1429 struct sdhci_msm_pltfm_data *pdata = msm_host->pdata;
1430 int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) ==
1431 0xffffffff);
1432
1433 pr_debug("%s: %s\n", dev_name(&pdev->dev), __func__);
1434 sdhci_remove_host(host, dead);
1435 sdhci_pltfm_free(pdev);
1436 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
1437 if (!IS_ERR(msm_host->clk))
1438 clk_disable_unprepare(msm_host->clk);
1439 if (!IS_ERR(msm_host->pclk))
1440 clk_disable_unprepare(msm_host->pclk);
1441 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
1442 clk_disable_unprepare(msm_host->bus_clk);
1443 if (pdata->pin_data)
1444 sdhci_msm_setup_gpio(pdata, false);
1445 return 0;
1446}
1447
1448static const struct of_device_id sdhci_msm_dt_match[] = {
1449 {.compatible = "qcom,sdhci-msm"},
1450};
1451MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match);
1452
1453static struct platform_driver sdhci_msm_driver = {
1454 .probe = sdhci_msm_probe,
1455 .remove = sdhci_msm_remove,
1456 .driver = {
1457 .name = "sdhci_msm",
1458 .owner = THIS_MODULE,
1459 .of_match_table = sdhci_msm_dt_match,
1460 },
1461};
1462
1463module_platform_driver(sdhci_msm_driver);
1464
1465MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Secure Digital Host Controller Interface driver");
1466MODULE_LICENSE("GPL v2");