blob: b1996a5a4363a9f3997cca8b3de47f67133abed5 [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 Tummala66b0fe32013-04-25 11:50:56 +053037#include <linux/iopoll.h>
Sahitya Tummala8a3e8182013-03-10 14:12:52 +053038#include <linux/msm-bus.h>
Asutosh Das0ef24812012-12-18 16:14:02 +053039
40#include "sdhci-pltfm.h"
41
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -070042#define SDHCI_VER_100 0x2B
Asutosh Das0ef24812012-12-18 16:14:02 +053043#define CORE_HC_MODE 0x78
44#define HC_MODE_EN 0x1
45
46#define CORE_POWER 0x0
47#define CORE_SW_RST (1 << 7)
48
49#define CORE_PWRCTL_STATUS 0xDC
50#define CORE_PWRCTL_MASK 0xE0
51#define CORE_PWRCTL_CLEAR 0xE4
52#define CORE_PWRCTL_CTL 0xE8
53
54#define CORE_PWRCTL_BUS_OFF 0x01
55#define CORE_PWRCTL_BUS_ON (1 << 1)
56#define CORE_PWRCTL_IO_LOW (1 << 2)
57#define CORE_PWRCTL_IO_HIGH (1 << 3)
58
59#define CORE_PWRCTL_BUS_SUCCESS 0x01
60#define CORE_PWRCTL_BUS_FAIL (1 << 1)
61#define CORE_PWRCTL_IO_SUCCESS (1 << 2)
62#define CORE_PWRCTL_IO_FAIL (1 << 3)
63
64#define INT_MASK 0xF
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -070065#define MAX_PHASES 16
66
67#define CORE_DLL_LOCK (1 << 7)
68#define CORE_DLL_EN (1 << 16)
69#define CORE_CDR_EN (1 << 17)
70#define CORE_CK_OUT_EN (1 << 18)
71#define CORE_CDR_EXT_EN (1 << 19)
72#define CORE_DLL_PDN (1 << 29)
73#define CORE_DLL_RST (1 << 30)
74#define CORE_DLL_CONFIG 0x100
75#define CORE_DLL_TEST_CTL 0x104
76#define CORE_DLL_STATUS 0x108
77
78#define CORE_VENDOR_SPEC 0x10C
79#define CORE_CLK_PWRSAVE (1 << 1)
80#define CORE_IO_PAD_PWR_SWITCH (1 << 16)
81
Asutosh Das648f9d12013-01-10 21:11:04 +053082/* 8KB descriptors */
83#define SDHCI_MSM_MAX_SEGMENTS (1 << 13)
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +053084#define SDHCI_MSM_MMC_CLK_GATE_DELAY 200 /* msecs */
Asutosh Das648f9d12013-01-10 21:11:04 +053085
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -070086static const u32 tuning_block_64[] = {
87 0x00FF0FFF, 0xCCC3CCFF, 0xFFCC3CC3, 0xEFFEFFFE,
88 0xDDFFDFFF, 0xFBFFFBFF, 0xFF7FFFBF, 0xEFBDF777,
89 0xF0FFF0FF, 0x3CCCFC0F, 0xCFCC33CC, 0xEEFFEFFF,
90 0xFDFFFDFF, 0xFFBFFFDF, 0xFFF7FFBB, 0xDE7B7FF7
91};
92
93static const u32 tuning_block_128[] = {
94 0xFF00FFFF, 0x0000FFFF, 0xCCCCFFFF, 0xCCCC33CC,
95 0xCC3333CC, 0xFFFFCCCC, 0xFFFFEEFF, 0xFFEEEEFF,
96 0xFFDDFFFF, 0xDDDDFFFF, 0xBBFFFFFF, 0xBBFFFFFF,
97 0xFFFFFFBB, 0xFFFFFF77, 0x77FF7777, 0xFFEEDDBB,
98 0x00FFFFFF, 0x00FFFFFF, 0xCCFFFF00, 0xCC33CCCC,
99 0x3333CCCC, 0xFFCCCCCC, 0xFFEEFFFF, 0xEEEEFFFF,
100 0xDDFFFFFF, 0xDDFFFFFF, 0xFFFFFFDD, 0xFFFFFFBB,
101 0xFFFFBBBB, 0xFFFF77FF, 0xFF7777FF, 0xEEDDBB77
102};
Asutosh Das0ef24812012-12-18 16:14:02 +0530103
Venkat Gopalakrishnan270580a2013-03-11 12:17:57 -0700104static int disable_slots;
105/* root can write, others read */
106module_param(disable_slots, int, S_IRUGO|S_IWUSR);
107
Asutosh Das0ef24812012-12-18 16:14:02 +0530108/* This structure keeps information per regulator */
109struct sdhci_msm_reg_data {
110 /* voltage regulator handle */
111 struct regulator *reg;
112 /* regulator name */
113 const char *name;
114 /* voltage level to be set */
115 u32 low_vol_level;
116 u32 high_vol_level;
117 /* Load values for low power and high power mode */
118 u32 lpm_uA;
119 u32 hpm_uA;
120
121 /* is this regulator enabled? */
122 bool is_enabled;
123 /* is this regulator needs to be always on? */
124 bool is_always_on;
125 /* is low power mode setting required for this regulator? */
126 bool lpm_sup;
127 bool set_voltage_sup;
128};
129
130/*
131 * This structure keeps information for all the
132 * regulators required for a SDCC slot.
133 */
134struct sdhci_msm_slot_reg_data {
135 /* keeps VDD/VCC regulator info */
136 struct sdhci_msm_reg_data *vdd_data;
137 /* keeps VDD IO regulator info */
138 struct sdhci_msm_reg_data *vdd_io_data;
139};
140
141struct sdhci_msm_gpio {
142 u32 no;
143 const char *name;
144 bool is_enabled;
145};
146
147struct sdhci_msm_gpio_data {
148 struct sdhci_msm_gpio *gpio;
149 u8 size;
150};
151
152struct sdhci_msm_pin_data {
153 /*
154 * = 1 if controller pins are using gpios
155 * = 0 if controller has dedicated MSM pads
156 */
157 bool cfg_sts;
158 struct sdhci_msm_gpio_data *gpio_data;
159};
160
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530161struct sdhci_msm_bus_voting_data {
162 struct msm_bus_scale_pdata *bus_pdata;
163 unsigned int *bw_vecs;
164 unsigned int bw_vecs_size;
165};
166
Asutosh Das0ef24812012-12-18 16:14:02 +0530167struct sdhci_msm_pltfm_data {
168 /* Supported UHS-I Modes */
169 u32 caps;
170
171 /* More capabilities */
172 u32 caps2;
173
174 unsigned long mmc_bus_width;
Asutosh Das0ef24812012-12-18 16:14:02 +0530175 struct sdhci_msm_slot_reg_data *vreg_data;
176 bool nonremovable;
177 struct sdhci_msm_pin_data *pin_data;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +0530178 u32 cpu_dma_latency_us;
Sahitya Tummala581df132013-03-12 14:57:46 +0530179 int status_gpio; /* card detection GPIO that is configured as IRQ */
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530180 struct sdhci_msm_bus_voting_data *voting_data;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530181 u32 *sup_clk_table;
182 unsigned char sup_clk_cnt;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530183};
184
185struct sdhci_msm_bus_vote {
186 uint32_t client_handle;
187 uint32_t curr_vote;
188 int min_bw_vote;
189 int max_bw_vote;
190 bool is_max_bw_needed;
191 struct delayed_work vote_work;
192 struct device_attribute max_bus_bw;
Asutosh Das0ef24812012-12-18 16:14:02 +0530193};
194
195struct sdhci_msm_host {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530196 struct platform_device *pdev;
Asutosh Das0ef24812012-12-18 16:14:02 +0530197 void __iomem *core_mem; /* MSM SDCC mapped address */
198 struct clk *clk; /* main SD/MMC bus clock */
199 struct clk *pclk; /* SDHC peripheral bus clock */
200 struct clk *bus_clk; /* SDHC bus voter clock */
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +0530201 atomic_t clks_on; /* Set if clocks are enabled */
Asutosh Das0ef24812012-12-18 16:14:02 +0530202 struct sdhci_msm_pltfm_data *pdata;
203 struct mmc_host *mmc;
204 struct sdhci_pltfm_data sdhci_msm_pdata;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +0530205 u32 curr_pwr_state;
206 u32 curr_io_level;
207 struct completion pwr_irq_completion;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530208 struct sdhci_msm_bus_vote msm_bus_vote;
Sahitya Tummala5c55b932013-06-20 14:00:18 +0530209 struct device_attribute polling;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530210 u32 clk_rate; /* Keeps track of current clock rate that is set */
Asutosh Das0ef24812012-12-18 16:14:02 +0530211};
212
213enum vdd_io_level {
214 /* set vdd_io_data->low_vol_level */
215 VDD_IO_LOW,
216 /* set vdd_io_data->high_vol_level */
217 VDD_IO_HIGH,
218 /*
219 * set whatever there in voltage_level (third argument) of
220 * sdhci_msm_set_vdd_io_vol() function.
221 */
222 VDD_IO_SET_LEVEL,
223};
224
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700225/* MSM platform specific tuning */
226static inline int msm_dll_poll_ck_out_en(struct sdhci_host *host,
227 u8 poll)
228{
229 int rc = 0;
230 u32 wait_cnt = 50;
231 u8 ck_out_en = 0;
232 struct mmc_host *mmc = host->mmc;
233
234 /* poll for CK_OUT_EN bit. max. poll time = 50us */
235 ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) &
236 CORE_CK_OUT_EN);
237
238 while (ck_out_en != poll) {
239 if (--wait_cnt == 0) {
240 pr_err("%s: %s: CK_OUT_EN bit is not %d\n",
241 mmc_hostname(mmc), __func__, poll);
242 rc = -ETIMEDOUT;
243 goto out;
244 }
245 udelay(1);
246
247 ck_out_en = !!(readl_relaxed(host->ioaddr +
248 CORE_DLL_CONFIG) & CORE_CK_OUT_EN);
249 }
250out:
251 return rc;
252}
253
254static int msm_config_cm_dll_phase(struct sdhci_host *host, u8 phase)
255{
256 int rc = 0;
257 u8 grey_coded_phase_table[] = {0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4,
258 0xC, 0xD, 0xF, 0xE, 0xA, 0xB, 0x9,
259 0x8};
260 unsigned long flags;
261 u32 config;
262 struct mmc_host *mmc = host->mmc;
263
264 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
265 spin_lock_irqsave(&host->lock, flags);
266
267 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
268 config &= ~(CORE_CDR_EN | CORE_CK_OUT_EN);
269 config |= (CORE_CDR_EXT_EN | CORE_DLL_EN);
270 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
271
272 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '0' */
273 rc = msm_dll_poll_ck_out_en(host, 0);
274 if (rc)
275 goto err_out;
276
277 /*
278 * Write the selected DLL clock output phase (0 ... 15)
279 * to CDR_SELEXT bit field of DLL_CONFIG register.
280 */
281 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
282 & ~(0xF << 20))
283 | (grey_coded_phase_table[phase] << 20)),
284 host->ioaddr + CORE_DLL_CONFIG);
285
286 /* Set CK_OUT_EN bit of DLL_CONFIG register to 1. */
287 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
288 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
289
290 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '1' */
291 rc = msm_dll_poll_ck_out_en(host, 1);
292 if (rc)
293 goto err_out;
294
295 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
296 config |= CORE_CDR_EN;
297 config &= ~CORE_CDR_EXT_EN;
298 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
299 goto out;
300
301err_out:
302 pr_err("%s: %s: Failed to set DLL phase: %d\n",
303 mmc_hostname(mmc), __func__, phase);
304out:
305 spin_unlock_irqrestore(&host->lock, flags);
306 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
307 return rc;
308}
309
310/*
311 * Find out the greatest range of consecuitive selected
312 * DLL clock output phases that can be used as sampling
313 * setting for SD3.0 UHS-I card read operation (in SDR104
314 * timing mode) or for eMMC4.5 card read operation (in HS200
315 * timing mode).
316 * Select the 3/4 of the range and configure the DLL with the
317 * selected DLL clock output phase.
318 */
319
320static int msm_find_most_appropriate_phase(struct sdhci_host *host,
321 u8 *phase_table, u8 total_phases)
322{
323 int ret;
324 u8 ranges[MAX_PHASES][MAX_PHASES] = { {0}, {0} };
325 u8 phases_per_row[MAX_PHASES] = {0};
326 int row_index = 0, col_index = 0, selected_row_index = 0, curr_max = 0;
327 int i, cnt, phase_0_raw_index = 0, phase_15_raw_index = 0;
328 bool phase_0_found = false, phase_15_found = false;
329 struct mmc_host *mmc = host->mmc;
330
331 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
332 if (!total_phases || (total_phases > MAX_PHASES)) {
333 pr_err("%s: %s: invalid argument: total_phases=%d\n",
334 mmc_hostname(mmc), __func__, total_phases);
335 return -EINVAL;
336 }
337
338 for (cnt = 0; cnt < total_phases; cnt++) {
339 ranges[row_index][col_index] = phase_table[cnt];
340 phases_per_row[row_index] += 1;
341 col_index++;
342
343 if ((cnt + 1) == total_phases) {
344 continue;
345 /* check if next phase in phase_table is consecutive or not */
346 } else if ((phase_table[cnt] + 1) != phase_table[cnt + 1]) {
347 row_index++;
348 col_index = 0;
349 }
350 }
351
352 if (row_index >= MAX_PHASES)
353 return -EINVAL;
354
355 /* Check if phase-0 is present in first valid window? */
356 if (!ranges[0][0]) {
357 phase_0_found = true;
358 phase_0_raw_index = 0;
359 /* Check if cycle exist between 2 valid windows */
360 for (cnt = 1; cnt <= row_index; cnt++) {
361 if (phases_per_row[cnt]) {
362 for (i = 0; i < phases_per_row[cnt]; i++) {
363 if (ranges[cnt][i] == 15) {
364 phase_15_found = true;
365 phase_15_raw_index = cnt;
366 break;
367 }
368 }
369 }
370 }
371 }
372
373 /* If 2 valid windows form cycle then merge them as single window */
374 if (phase_0_found && phase_15_found) {
375 /* number of phases in raw where phase 0 is present */
376 u8 phases_0 = phases_per_row[phase_0_raw_index];
377 /* number of phases in raw where phase 15 is present */
378 u8 phases_15 = phases_per_row[phase_15_raw_index];
379
380 if (phases_0 + phases_15 >= MAX_PHASES)
381 /*
382 * If there are more than 1 phase windows then total
383 * number of phases in both the windows should not be
384 * more than or equal to MAX_PHASES.
385 */
386 return -EINVAL;
387
388 /* Merge 2 cyclic windows */
389 i = phases_15;
390 for (cnt = 0; cnt < phases_0; cnt++) {
391 ranges[phase_15_raw_index][i] =
392 ranges[phase_0_raw_index][cnt];
393 if (++i >= MAX_PHASES)
394 break;
395 }
396
397 phases_per_row[phase_0_raw_index] = 0;
398 phases_per_row[phase_15_raw_index] = phases_15 + phases_0;
399 }
400
401 for (cnt = 0; cnt <= row_index; cnt++) {
402 if (phases_per_row[cnt] > curr_max) {
403 curr_max = phases_per_row[cnt];
404 selected_row_index = cnt;
405 }
406 }
407
408 i = ((curr_max * 3) / 4);
409 if (i)
410 i--;
411
412 ret = (int)ranges[selected_row_index][i];
413
414 if (ret >= MAX_PHASES) {
415 ret = -EINVAL;
416 pr_err("%s: %s: invalid phase selected=%d\n",
417 mmc_hostname(mmc), __func__, ret);
418 }
419
420 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
421 return ret;
422}
423
424static inline void msm_cm_dll_set_freq(struct sdhci_host *host)
425{
426 u32 mclk_freq = 0;
427
428 /* Program the MCLK value to MCLK_FREQ bit field */
429 if (host->clock <= 112000000)
430 mclk_freq = 0;
431 else if (host->clock <= 125000000)
432 mclk_freq = 1;
433 else if (host->clock <= 137000000)
434 mclk_freq = 2;
435 else if (host->clock <= 150000000)
436 mclk_freq = 3;
437 else if (host->clock <= 162000000)
438 mclk_freq = 4;
439 else if (host->clock <= 175000000)
440 mclk_freq = 5;
441 else if (host->clock <= 187000000)
442 mclk_freq = 6;
443 else if (host->clock <= 200000000)
444 mclk_freq = 7;
445
446 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
447 & ~(7 << 24)) | (mclk_freq << 24)),
448 host->ioaddr + CORE_DLL_CONFIG);
449}
450
451/* Initialize the DLL (Programmable Delay Line ) */
452static int msm_init_cm_dll(struct sdhci_host *host)
453{
454 struct mmc_host *mmc = host->mmc;
455 int rc = 0;
456 unsigned long flags;
457 u32 wait_cnt;
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530458 bool prev_pwrsave, curr_pwrsave;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700459
460 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
461 spin_lock_irqsave(&host->lock, flags);
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530462 prev_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
463 CORE_CLK_PWRSAVE);
464 curr_pwrsave = prev_pwrsave;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700465 /*
466 * Make sure that clock is always enabled when DLL
467 * tuning is in progress. Keeping PWRSAVE ON may
468 * turn off the clock. So let's disable the PWRSAVE
469 * here and re-enable it once tuning is completed.
470 */
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530471 if (prev_pwrsave) {
472 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
473 & ~CORE_CLK_PWRSAVE),
474 host->ioaddr + CORE_VENDOR_SPEC);
475 curr_pwrsave = false;
476 }
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700477
478 /* Write 1 to DLL_RST bit of DLL_CONFIG register */
479 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
480 | CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
481
482 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
483 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
484 | CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
485 msm_cm_dll_set_freq(host);
486
487 /* Write 0 to DLL_RST bit of DLL_CONFIG register */
488 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
489 & ~CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
490
491 /* Write 0 to DLL_PDN bit of DLL_CONFIG register */
492 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
493 & ~CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
494
495 /* Set DLL_EN bit to 1. */
496 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
497 | CORE_DLL_EN), host->ioaddr + CORE_DLL_CONFIG);
498
499 /* Set CK_OUT_EN bit to 1. */
500 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
501 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
502
503 wait_cnt = 50;
504 /* Wait until DLL_LOCK bit of DLL_STATUS register becomes '1' */
505 while (!(readl_relaxed(host->ioaddr + CORE_DLL_STATUS) &
506 CORE_DLL_LOCK)) {
507 /* max. wait for 50us sec for LOCK bit to be set */
508 if (--wait_cnt == 0) {
509 pr_err("%s: %s: DLL failed to LOCK\n",
510 mmc_hostname(mmc), __func__);
511 rc = -ETIMEDOUT;
512 goto out;
513 }
514 /* wait for 1us before polling again */
515 udelay(1);
516 }
517
518out:
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530519 /* Restore the correct PWRSAVE state */
520 if (prev_pwrsave ^ curr_pwrsave) {
521 u32 reg = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC);
522
523 if (prev_pwrsave)
524 reg |= CORE_CLK_PWRSAVE;
525 else
526 reg &= ~CORE_CLK_PWRSAVE;
527
528 writel_relaxed(reg, host->ioaddr + CORE_VENDOR_SPEC);
529 }
530
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700531 spin_unlock_irqrestore(&host->lock, flags);
532 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
533 return rc;
534}
535
536int sdhci_msm_execute_tuning(struct sdhci_host *host, u32 opcode)
537{
538 unsigned long flags;
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530539 int tuning_seq_cnt = 3;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700540 u8 phase, *data_buf, tuned_phases[16], tuned_phase_cnt = 0;
541 const u32 *tuning_block_pattern = tuning_block_64;
542 int size = sizeof(tuning_block_64); /* Tuning pattern size in bytes */
543 int rc;
544 struct mmc_host *mmc = host->mmc;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530545 struct mmc_ios ios = host->mmc->ios;
546
547 /*
548 * Tuning is required for SDR104 and HS200 cards and if clock frequency
549 * is greater than 100MHz in these modes.
550 */
551 if (host->clock <= (100 * 1000 * 1000) ||
552 !(ios.timing == MMC_TIMING_MMC_HS200 ||
553 ios.timing == MMC_TIMING_UHS_SDR104))
554 return 0;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700555
556 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700557 spin_lock_irqsave(&host->lock, flags);
558
559 if ((opcode == MMC_SEND_TUNING_BLOCK_HS200) &&
560 (mmc->ios.bus_width == MMC_BUS_WIDTH_8)) {
561 tuning_block_pattern = tuning_block_128;
562 size = sizeof(tuning_block_128);
563 }
564 spin_unlock_irqrestore(&host->lock, flags);
565
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700566 data_buf = kmalloc(size, GFP_KERNEL);
567 if (!data_buf) {
568 rc = -ENOMEM;
569 goto out;
570 }
571
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530572retry:
573 /* first of all reset the tuning block */
574 rc = msm_init_cm_dll(host);
575 if (rc)
576 goto kfree;
577
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700578 phase = 0;
579 do {
580 struct mmc_command cmd = {0};
581 struct mmc_data data = {0};
582 struct mmc_request mrq = {
583 .cmd = &cmd,
584 .data = &data
585 };
586 struct scatterlist sg;
587
588 /* set the phase in delay line hw block */
589 rc = msm_config_cm_dll_phase(host, phase);
590 if (rc)
591 goto kfree;
592
593 cmd.opcode = opcode;
594 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
595
596 data.blksz = size;
597 data.blocks = 1;
598 data.flags = MMC_DATA_READ;
599 data.timeout_ns = 1000 * 1000 * 1000; /* 1 sec */
600
601 data.sg = &sg;
602 data.sg_len = 1;
603 sg_init_one(&sg, data_buf, size);
604 memset(data_buf, 0, size);
605 mmc_wait_for_req(mmc, &mrq);
606
607 if (!cmd.error && !data.error &&
608 !memcmp(data_buf, tuning_block_pattern, size)) {
609 /* tuning is successful at this tuning point */
610 tuned_phases[tuned_phase_cnt++] = phase;
611 pr_debug("%s: %s: found good phase = %d\n",
612 mmc_hostname(mmc), __func__, phase);
613 }
614 } while (++phase < 16);
615
616 if (tuned_phase_cnt) {
617 rc = msm_find_most_appropriate_phase(host, tuned_phases,
618 tuned_phase_cnt);
619 if (rc < 0)
620 goto kfree;
621 else
622 phase = (u8)rc;
623
624 /*
625 * Finally set the selected phase in delay
626 * line hw block.
627 */
628 rc = msm_config_cm_dll_phase(host, phase);
629 if (rc)
630 goto kfree;
631 pr_debug("%s: %s: finally setting the tuning phase to %d\n",
632 mmc_hostname(mmc), __func__, phase);
633 } else {
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530634 if (--tuning_seq_cnt)
635 goto retry;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700636 /* tuning failed */
637 pr_err("%s: %s: no tuning point found\n",
638 mmc_hostname(mmc), __func__);
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530639 rc = -EIO;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700640 }
641
642kfree:
643 kfree(data_buf);
644out:
645 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
646 return rc;
647}
648
Asutosh Das0ef24812012-12-18 16:14:02 +0530649static int sdhci_msm_setup_gpio(struct sdhci_msm_pltfm_data *pdata, bool enable)
650{
651 struct sdhci_msm_gpio_data *curr;
652 int i, ret = 0;
653
654 curr = pdata->pin_data->gpio_data;
655 for (i = 0; i < curr->size; i++) {
656 if (!gpio_is_valid(curr->gpio[i].no)) {
657 ret = -EINVAL;
658 pr_err("%s: Invalid gpio = %d\n", __func__,
659 curr->gpio[i].no);
660 goto free_gpios;
661 }
662 if (enable) {
663 ret = gpio_request(curr->gpio[i].no,
664 curr->gpio[i].name);
665 if (ret) {
666 pr_err("%s: gpio_request(%d, %s) failed %d\n",
667 __func__, curr->gpio[i].no,
668 curr->gpio[i].name, ret);
669 goto free_gpios;
670 }
671 curr->gpio[i].is_enabled = true;
672 } else {
673 gpio_free(curr->gpio[i].no);
674 curr->gpio[i].is_enabled = false;
675 }
676 }
677 return ret;
678
679free_gpios:
680 for (i--; i >= 0; i--) {
681 gpio_free(curr->gpio[i].no);
682 curr->gpio[i].is_enabled = false;
683 }
684 return ret;
685}
686
687static int sdhci_msm_setup_pins(struct sdhci_msm_pltfm_data *pdata, bool enable)
688{
689 int ret = 0;
690
691 if (!pdata->pin_data || (pdata->pin_data->cfg_sts == enable))
692 return 0;
693
694 ret = sdhci_msm_setup_gpio(pdata, enable);
695 if (!ret)
696 pdata->pin_data->cfg_sts = enable;
697
698 return ret;
699}
700
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530701static int sdhci_msm_dt_get_array(struct device *dev, const char *prop_name,
702 u32 **out, int *len, u32 size)
703{
704 int ret = 0;
705 struct device_node *np = dev->of_node;
706 size_t sz;
707 u32 *arr = NULL;
708
709 if (!of_get_property(np, prop_name, len)) {
710 ret = -EINVAL;
711 goto out;
712 }
713 sz = *len = *len / sizeof(*arr);
714 if (sz <= 0 || (size > 0 && (sz != size))) {
715 dev_err(dev, "%s invalid size\n", prop_name);
716 ret = -EINVAL;
717 goto out;
718 }
719
720 arr = devm_kzalloc(dev, sz * sizeof(*arr), GFP_KERNEL);
721 if (!arr) {
722 dev_err(dev, "%s failed allocating memory\n", prop_name);
723 ret = -ENOMEM;
724 goto out;
725 }
726
727 ret = of_property_read_u32_array(np, prop_name, arr, sz);
728 if (ret < 0) {
729 dev_err(dev, "%s failed reading array %d\n", prop_name, ret);
730 goto out;
731 }
732 *out = arr;
733out:
734 if (ret)
735 *len = 0;
736 return ret;
737}
738
Asutosh Das0ef24812012-12-18 16:14:02 +0530739#define MAX_PROP_SIZE 32
740static int sdhci_msm_dt_parse_vreg_info(struct device *dev,
741 struct sdhci_msm_reg_data **vreg_data, const char *vreg_name)
742{
743 int len, ret = 0;
744 const __be32 *prop;
745 char prop_name[MAX_PROP_SIZE];
746 struct sdhci_msm_reg_data *vreg;
747 struct device_node *np = dev->of_node;
748
749 snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", vreg_name);
750 if (!of_parse_phandle(np, prop_name, 0)) {
751 dev_err(dev, "No vreg data found for %s\n", vreg_name);
752 ret = -EINVAL;
753 return ret;
754 }
755
756 vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
757 if (!vreg) {
758 dev_err(dev, "No memory for vreg: %s\n", vreg_name);
759 ret = -ENOMEM;
760 return ret;
761 }
762
763 vreg->name = vreg_name;
764
765 snprintf(prop_name, MAX_PROP_SIZE,
766 "qcom,%s-always-on", vreg_name);
767 if (of_get_property(np, prop_name, NULL))
768 vreg->is_always_on = true;
769
770 snprintf(prop_name, MAX_PROP_SIZE,
771 "qcom,%s-lpm-sup", vreg_name);
772 if (of_get_property(np, prop_name, NULL))
773 vreg->lpm_sup = true;
774
775 snprintf(prop_name, MAX_PROP_SIZE,
776 "qcom,%s-voltage-level", vreg_name);
777 prop = of_get_property(np, prop_name, &len);
778 if (!prop || (len != (2 * sizeof(__be32)))) {
779 dev_warn(dev, "%s %s property\n",
780 prop ? "invalid format" : "no", prop_name);
781 } else {
782 vreg->low_vol_level = be32_to_cpup(&prop[0]);
783 vreg->high_vol_level = be32_to_cpup(&prop[1]);
784 }
785
786 snprintf(prop_name, MAX_PROP_SIZE,
787 "qcom,%s-current-level", vreg_name);
788 prop = of_get_property(np, prop_name, &len);
789 if (!prop || (len != (2 * sizeof(__be32)))) {
790 dev_warn(dev, "%s %s property\n",
791 prop ? "invalid format" : "no", prop_name);
792 } else {
793 vreg->lpm_uA = be32_to_cpup(&prop[0]);
794 vreg->hpm_uA = be32_to_cpup(&prop[1]);
795 }
796
797 *vreg_data = vreg;
798 dev_dbg(dev, "%s: %s %s vol=[%d %d]uV, curr=[%d %d]uA\n",
799 vreg->name, vreg->is_always_on ? "always_on," : "",
800 vreg->lpm_sup ? "lpm_sup," : "", vreg->low_vol_level,
801 vreg->high_vol_level, vreg->lpm_uA, vreg->hpm_uA);
802
803 return ret;
804}
805
806#define GPIO_NAME_MAX_LEN 32
807static int sdhci_msm_dt_parse_gpio_info(struct device *dev,
808 struct sdhci_msm_pltfm_data *pdata)
809{
810 int ret = 0, cnt, i;
811 struct sdhci_msm_pin_data *pin_data;
812 struct device_node *np = dev->of_node;
813
814 pin_data = devm_kzalloc(dev, sizeof(*pin_data), GFP_KERNEL);
815 if (!pin_data) {
816 dev_err(dev, "No memory for pin_data\n");
817 ret = -ENOMEM;
818 goto out;
819 }
820
821 cnt = of_gpio_count(np);
822 if (cnt > 0) {
823 pin_data->gpio_data = devm_kzalloc(dev,
824 sizeof(struct sdhci_msm_gpio_data), GFP_KERNEL);
825 if (!pin_data->gpio_data) {
826 dev_err(dev, "No memory for gpio_data\n");
827 ret = -ENOMEM;
828 goto out;
829 }
830 pin_data->gpio_data->size = cnt;
831 pin_data->gpio_data->gpio = devm_kzalloc(dev, cnt *
832 sizeof(struct sdhci_msm_gpio), GFP_KERNEL);
833
834 if (!pin_data->gpio_data->gpio) {
835 dev_err(dev, "No memory for gpio\n");
836 ret = -ENOMEM;
837 goto out;
838 }
839
840 for (i = 0; i < cnt; i++) {
841 const char *name = NULL;
842 char result[GPIO_NAME_MAX_LEN];
843 pin_data->gpio_data->gpio[i].no = of_get_gpio(np, i);
844 of_property_read_string_index(np,
845 "qcom,gpio-names", i, &name);
846
847 snprintf(result, GPIO_NAME_MAX_LEN, "%s-%s",
848 dev_name(dev), name ? name : "?");
849 pin_data->gpio_data->gpio[i].name = result;
850 dev_dbg(dev, "%s: gpio[%s] = %d\n", __func__,
851 pin_data->gpio_data->gpio[i].name,
852 pin_data->gpio_data->gpio[i].no);
853 pdata->pin_data = pin_data;
854 }
855 }
856
857out:
858 if (ret)
859 dev_err(dev, "%s failed with err %d\n", __func__, ret);
860 return ret;
861}
862
863/* Parse platform data */
864static struct sdhci_msm_pltfm_data *sdhci_msm_populate_pdata(struct device *dev)
865{
866 struct sdhci_msm_pltfm_data *pdata = NULL;
867 struct device_node *np = dev->of_node;
868 u32 bus_width = 0;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +0530869 u32 cpu_dma_latency;
Asutosh Das0ef24812012-12-18 16:14:02 +0530870 int len, i;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530871 int clk_table_len;
872 u32 *clk_table = NULL;
Sujit Reddy Thumma1958d3d2013-06-03 09:54:32 +0530873 enum of_gpio_flags flags = OF_GPIO_ACTIVE_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +0530874
875 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
876 if (!pdata) {
877 dev_err(dev, "failed to allocate memory for platform data\n");
878 goto out;
879 }
880
Sujit Reddy Thumma1958d3d2013-06-03 09:54:32 +0530881 pdata->status_gpio = of_get_named_gpio_flags(np, "cd-gpios", 0, &flags);
882 if (gpio_is_valid(pdata->status_gpio) & !(flags & OF_GPIO_ACTIVE_LOW))
883 pdata->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
Sahitya Tummala581df132013-03-12 14:57:46 +0530884
Asutosh Das0ef24812012-12-18 16:14:02 +0530885 of_property_read_u32(np, "qcom,bus-width", &bus_width);
886 if (bus_width == 8)
887 pdata->mmc_bus_width = MMC_CAP_8_BIT_DATA;
888 else if (bus_width == 4)
889 pdata->mmc_bus_width = MMC_CAP_4_BIT_DATA;
890 else {
891 dev_notice(dev, "invalid bus-width, default to 1-bit mode\n");
892 pdata->mmc_bus_width = 0;
893 }
894
Sahitya Tummalac6f48d42013-03-10 07:03:17 +0530895 if (!of_property_read_u32(np, "qcom,cpu-dma-latency-us",
896 &cpu_dma_latency))
897 pdata->cpu_dma_latency_us = cpu_dma_latency;
898
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530899 if (sdhci_msm_dt_get_array(dev, "qcom,clk-rates",
900 &clk_table, &clk_table_len, 0)) {
901 dev_err(dev, "failed parsing supported clock rates\n");
902 goto out;
903 }
904 if (!clk_table || !clk_table_len) {
905 dev_err(dev, "Invalid clock table\n");
906 goto out;
907 }
908 pdata->sup_clk_table = clk_table;
909 pdata->sup_clk_cnt = clk_table_len;
910
Asutosh Das0ef24812012-12-18 16:14:02 +0530911 pdata->vreg_data = devm_kzalloc(dev, sizeof(struct
912 sdhci_msm_slot_reg_data),
913 GFP_KERNEL);
914 if (!pdata->vreg_data) {
915 dev_err(dev, "failed to allocate memory for vreg data\n");
916 goto out;
917 }
918
919 if (sdhci_msm_dt_parse_vreg_info(dev, &pdata->vreg_data->vdd_data,
920 "vdd")) {
921 dev_err(dev, "failed parsing vdd data\n");
922 goto out;
923 }
924 if (sdhci_msm_dt_parse_vreg_info(dev,
925 &pdata->vreg_data->vdd_io_data,
926 "vdd-io")) {
927 dev_err(dev, "failed parsing vdd-io data\n");
928 goto out;
929 }
930
931 if (sdhci_msm_dt_parse_gpio_info(dev, pdata)) {
932 dev_err(dev, "failed parsing gpio data\n");
933 goto out;
934 }
935
Asutosh Das0ef24812012-12-18 16:14:02 +0530936 len = of_property_count_strings(np, "qcom,bus-speed-mode");
937
938 for (i = 0; i < len; i++) {
939 const char *name = NULL;
940
941 of_property_read_string_index(np,
942 "qcom,bus-speed-mode", i, &name);
943 if (!name)
944 continue;
945
946 if (!strncmp(name, "HS200_1p8v", sizeof("HS200_1p8v")))
947 pdata->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
948 else if (!strncmp(name, "HS200_1p2v", sizeof("HS200_1p2v")))
949 pdata->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
950 else if (!strncmp(name, "DDR_1p8v", sizeof("DDR_1p8v")))
951 pdata->caps |= MMC_CAP_1_8V_DDR
952 | MMC_CAP_UHS_DDR50;
953 else if (!strncmp(name, "DDR_1p2v", sizeof("DDR_1p2v")))
954 pdata->caps |= MMC_CAP_1_2V_DDR
955 | MMC_CAP_UHS_DDR50;
956 }
957
958 if (of_get_property(np, "qcom,nonremovable", NULL))
959 pdata->nonremovable = true;
960
961 return pdata;
962out:
963 return NULL;
964}
965
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530966/* Returns required bandwidth in Bytes per Sec */
967static unsigned int sdhci_get_bw_required(struct sdhci_host *host,
968 struct mmc_ios *ios)
969{
Sahitya Tummala2886c922013-04-03 18:03:31 +0530970 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
971 struct sdhci_msm_host *msm_host = pltfm_host->priv;
972
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530973 unsigned int bw;
974
Sahitya Tummala2886c922013-04-03 18:03:31 +0530975 bw = msm_host->clk_rate;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530976 /*
977 * For DDR mode, SDCC controller clock will be at
978 * the double rate than the actual clock that goes to card.
979 */
980 if (ios->bus_width == MMC_BUS_WIDTH_4)
981 bw /= 2;
982 else if (ios->bus_width == MMC_BUS_WIDTH_1)
983 bw /= 8;
984
985 return bw;
986}
987
988static int sdhci_msm_bus_get_vote_for_bw(struct sdhci_msm_host *host,
989 unsigned int bw)
990{
991 unsigned int *table = host->pdata->voting_data->bw_vecs;
992 unsigned int size = host->pdata->voting_data->bw_vecs_size;
993 int i;
994
995 if (host->msm_bus_vote.is_max_bw_needed && bw)
996 return host->msm_bus_vote.max_bw_vote;
997
998 for (i = 0; i < size; i++) {
999 if (bw <= table[i])
1000 break;
1001 }
1002
1003 if (i && (i == size))
1004 i--;
1005
1006 return i;
1007}
1008
1009/*
1010 * This function must be called with host lock acquired.
1011 * Caller of this function should also ensure that msm bus client
1012 * handle is not null.
1013 */
1014static inline int sdhci_msm_bus_set_vote(struct sdhci_msm_host *msm_host,
1015 int vote,
1016 unsigned long flags)
1017{
1018 struct sdhci_host *host = platform_get_drvdata(msm_host->pdev);
1019 int rc = 0;
1020
1021 if (vote != msm_host->msm_bus_vote.curr_vote) {
1022 spin_unlock_irqrestore(&host->lock, flags);
1023 rc = msm_bus_scale_client_update_request(
1024 msm_host->msm_bus_vote.client_handle, vote);
1025 spin_lock_irqsave(&host->lock, flags);
1026 if (rc) {
1027 pr_err("%s: msm_bus_scale_client_update_request() failed: bus_client_handle=0x%x, vote=%d, err=%d\n",
1028 mmc_hostname(host->mmc),
1029 msm_host->msm_bus_vote.client_handle, vote, rc);
1030 goto out;
1031 }
1032 msm_host->msm_bus_vote.curr_vote = vote;
1033 }
1034out:
1035 return rc;
1036}
1037
1038/*
1039 * Internal work. Work to set 0 bandwidth for msm bus.
1040 */
1041static void sdhci_msm_bus_work(struct work_struct *work)
1042{
1043 struct sdhci_msm_host *msm_host;
1044 struct sdhci_host *host;
1045 unsigned long flags;
1046
1047 msm_host = container_of(work, struct sdhci_msm_host,
1048 msm_bus_vote.vote_work.work);
1049 host = platform_get_drvdata(msm_host->pdev);
1050
1051 if (!msm_host->msm_bus_vote.client_handle)
1052 return;
1053
1054 spin_lock_irqsave(&host->lock, flags);
1055 /* don't vote for 0 bandwidth if any request is in progress */
1056 if (!host->mrq) {
1057 sdhci_msm_bus_set_vote(msm_host,
1058 msm_host->msm_bus_vote.min_bw_vote, flags);
1059 } else
1060 pr_warning("%s: %s: Transfer in progress. skipping bus voting to 0 bandwidth\n",
1061 mmc_hostname(host->mmc), __func__);
1062 spin_unlock_irqrestore(&host->lock, flags);
1063}
1064
1065/*
1066 * This function cancels any scheduled delayed work and sets the bus
1067 * vote based on bw (bandwidth) argument.
1068 */
1069static void sdhci_msm_bus_cancel_work_and_set_vote(struct sdhci_host *host,
1070 unsigned int bw)
1071{
1072 int vote;
1073 unsigned long flags;
1074 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1075 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1076
1077 cancel_delayed_work_sync(&msm_host->msm_bus_vote.vote_work);
1078 spin_lock_irqsave(&host->lock, flags);
1079 vote = sdhci_msm_bus_get_vote_for_bw(msm_host, bw);
1080 sdhci_msm_bus_set_vote(msm_host, vote, flags);
1081 spin_unlock_irqrestore(&host->lock, flags);
1082}
1083
1084#define MSM_MMC_BUS_VOTING_DELAY 200 /* msecs */
1085
1086/* This function queues a work which will set the bandwidth requiement to 0 */
1087static void sdhci_msm_bus_queue_work(struct sdhci_host *host)
1088{
1089 unsigned long flags;
1090 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1091 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1092
1093 spin_lock_irqsave(&host->lock, flags);
1094 if (msm_host->msm_bus_vote.min_bw_vote !=
1095 msm_host->msm_bus_vote.curr_vote)
1096 queue_delayed_work(system_wq,
1097 &msm_host->msm_bus_vote.vote_work,
1098 msecs_to_jiffies(MSM_MMC_BUS_VOTING_DELAY));
1099 spin_unlock_irqrestore(&host->lock, flags);
1100}
1101
1102static int sdhci_msm_bus_register(struct sdhci_msm_host *host,
1103 struct platform_device *pdev)
1104{
1105 int rc = 0;
1106 struct msm_bus_scale_pdata *bus_pdata;
1107
1108 struct sdhci_msm_bus_voting_data *data;
1109 struct device *dev = &pdev->dev;
1110
1111 data = devm_kzalloc(dev,
1112 sizeof(struct sdhci_msm_bus_voting_data), GFP_KERNEL);
1113 if (!data) {
1114 dev_err(&pdev->dev,
1115 "%s: failed to allocate memory\n", __func__);
1116 rc = -ENOMEM;
1117 goto out;
1118 }
1119 data->bus_pdata = msm_bus_cl_get_pdata(pdev);
1120 if (data->bus_pdata) {
1121 rc = sdhci_msm_dt_get_array(dev, "qcom,bus-bw-vectors-bps",
1122 &data->bw_vecs, &data->bw_vecs_size, 0);
1123 if (rc) {
1124 dev_err(&pdev->dev,
1125 "%s: Failed to get bus-bw-vectors-bps\n",
1126 __func__);
1127 goto out;
1128 }
1129 host->pdata->voting_data = data;
1130 }
1131 if (host->pdata->voting_data &&
1132 host->pdata->voting_data->bus_pdata &&
1133 host->pdata->voting_data->bw_vecs &&
1134 host->pdata->voting_data->bw_vecs_size) {
1135
1136 bus_pdata = host->pdata->voting_data->bus_pdata;
1137 host->msm_bus_vote.client_handle =
1138 msm_bus_scale_register_client(bus_pdata);
1139 if (!host->msm_bus_vote.client_handle) {
1140 dev_err(&pdev->dev, "msm_bus_scale_register_client()\n");
1141 rc = -EFAULT;
1142 goto out;
1143 }
1144 /* cache the vote index for minimum and maximum bandwidth */
1145 host->msm_bus_vote.min_bw_vote =
1146 sdhci_msm_bus_get_vote_for_bw(host, 0);
1147 host->msm_bus_vote.max_bw_vote =
1148 sdhci_msm_bus_get_vote_for_bw(host, UINT_MAX);
1149 } else {
1150 devm_kfree(dev, data);
1151 }
1152
1153out:
1154 return rc;
1155}
1156
1157static void sdhci_msm_bus_unregister(struct sdhci_msm_host *host)
1158{
1159 if (host->msm_bus_vote.client_handle)
1160 msm_bus_scale_unregister_client(
1161 host->msm_bus_vote.client_handle);
1162}
1163
1164static void sdhci_msm_bus_voting(struct sdhci_host *host, u32 enable)
1165{
1166 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1167 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1168 struct mmc_ios *ios = &host->mmc->ios;
1169 unsigned int bw;
1170
1171 if (!msm_host->msm_bus_vote.client_handle)
1172 return;
1173
1174 bw = sdhci_get_bw_required(host, ios);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301175 if (enable) {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301176 sdhci_msm_bus_cancel_work_and_set_vote(host, bw);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301177 } else {
1178 /*
1179 * If clock gating is enabled, then remove the vote
1180 * immediately because clocks will be disabled only
1181 * after SDHCI_MSM_MMC_CLK_GATE_DELAY and thus no
1182 * additional delay is required to remove the bus vote.
1183 */
1184#ifdef CONFIG_MMC_CLKGATE
1185 if (host->mmc->clkgate_delay)
1186 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
1187 else
1188#endif
1189 sdhci_msm_bus_queue_work(host);
1190 }
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301191}
1192
Asutosh Das0ef24812012-12-18 16:14:02 +05301193/* Regulator utility functions */
1194static int sdhci_msm_vreg_init_reg(struct device *dev,
1195 struct sdhci_msm_reg_data *vreg)
1196{
1197 int ret = 0;
1198
1199 /* check if regulator is already initialized? */
1200 if (vreg->reg)
1201 goto out;
1202
1203 /* Get the regulator handle */
1204 vreg->reg = devm_regulator_get(dev, vreg->name);
1205 if (IS_ERR(vreg->reg)) {
1206 ret = PTR_ERR(vreg->reg);
1207 pr_err("%s: devm_regulator_get(%s) failed. ret=%d\n",
1208 __func__, vreg->name, ret);
1209 goto out;
1210 }
1211
1212 /* sanity check */
1213 if (!vreg->high_vol_level || !vreg->hpm_uA) {
1214 pr_err("%s: %s invalid constraints specified\n",
1215 __func__, vreg->name);
1216 ret = -EINVAL;
1217 }
1218
1219out:
1220 return ret;
1221}
1222
1223static void sdhci_msm_vreg_deinit_reg(struct sdhci_msm_reg_data *vreg)
1224{
1225 if (vreg->reg)
1226 devm_regulator_put(vreg->reg);
1227}
1228
1229static int sdhci_msm_vreg_set_optimum_mode(struct sdhci_msm_reg_data
1230 *vreg, int uA_load)
1231{
1232 int ret = 0;
1233
1234 /*
1235 * regulators that do not support regulator_set_voltage also
1236 * do not support regulator_set_optimum_mode
1237 */
1238 if (vreg->set_voltage_sup) {
1239 ret = regulator_set_load(vreg->reg, uA_load);
1240 if (ret < 0)
1241 pr_err("%s: regulator_set_load(reg=%s,uA_load=%d) failed. ret=%d\n",
1242 __func__, vreg->name, uA_load, ret);
1243 else
1244 /*
1245 * regulator_set_load() can return non zero
1246 * value even for success case.
1247 */
1248 ret = 0;
1249 }
1250 return ret;
1251}
1252
1253static int sdhci_msm_vreg_set_voltage(struct sdhci_msm_reg_data *vreg,
1254 int min_uV, int max_uV)
1255{
1256 int ret = 0;
1257
1258 ret = regulator_set_voltage(vreg->reg, min_uV, max_uV);
1259 if (ret) {
1260 pr_err("%s: regulator_set_voltage(%s)failed. min_uV=%d,max_uV=%d,ret=%d\n",
1261 __func__, vreg->name, min_uV, max_uV, ret);
1262 }
1263
1264 return ret;
1265}
1266
1267static int sdhci_msm_vreg_enable(struct sdhci_msm_reg_data *vreg)
1268{
1269 int ret = 0;
1270
1271 /* Put regulator in HPM (high power mode) */
1272 ret = sdhci_msm_vreg_set_optimum_mode(vreg, vreg->hpm_uA);
1273 if (ret < 0)
1274 return ret;
1275
1276 if (!vreg->is_enabled) {
1277 /* Set voltage level */
1278 ret = sdhci_msm_vreg_set_voltage(vreg, vreg->high_vol_level,
1279 vreg->high_vol_level);
1280 if (ret)
1281 return ret;
1282 }
1283 ret = regulator_enable(vreg->reg);
1284 if (ret) {
1285 pr_err("%s: regulator_enable(%s) failed. ret=%d\n",
1286 __func__, vreg->name, ret);
1287 return ret;
1288 }
1289 vreg->is_enabled = true;
1290 return ret;
1291}
1292
1293static int sdhci_msm_vreg_disable(struct sdhci_msm_reg_data *vreg)
1294{
1295 int ret = 0;
1296
1297 /* Never disable regulator marked as always_on */
1298 if (vreg->is_enabled && !vreg->is_always_on) {
1299 ret = regulator_disable(vreg->reg);
1300 if (ret) {
1301 pr_err("%s: regulator_disable(%s) failed. ret=%d\n",
1302 __func__, vreg->name, ret);
1303 goto out;
1304 }
1305 vreg->is_enabled = false;
1306
1307 ret = sdhci_msm_vreg_set_optimum_mode(vreg, 0);
1308 if (ret < 0)
1309 goto out;
1310
1311 /* Set min. voltage level to 0 */
1312 ret = sdhci_msm_vreg_set_voltage(vreg, 0, vreg->high_vol_level);
1313 if (ret)
1314 goto out;
1315 } else if (vreg->is_enabled && vreg->is_always_on) {
1316 if (vreg->lpm_sup) {
1317 /* Put always_on regulator in LPM (low power mode) */
1318 ret = sdhci_msm_vreg_set_optimum_mode(vreg,
1319 vreg->lpm_uA);
1320 if (ret < 0)
1321 goto out;
1322 }
1323 }
1324out:
1325 return ret;
1326}
1327
1328static int sdhci_msm_setup_vreg(struct sdhci_msm_pltfm_data *pdata,
1329 bool enable, bool is_init)
1330{
1331 int ret = 0, i;
1332 struct sdhci_msm_slot_reg_data *curr_slot;
1333 struct sdhci_msm_reg_data *vreg_table[2];
1334
1335 curr_slot = pdata->vreg_data;
1336 if (!curr_slot) {
1337 pr_debug("%s: vreg info unavailable,assuming the slot is powered by always on domain\n",
1338 __func__);
1339 goto out;
1340 }
1341
1342 vreg_table[0] = curr_slot->vdd_data;
1343 vreg_table[1] = curr_slot->vdd_io_data;
1344
1345 for (i = 0; i < ARRAY_SIZE(vreg_table); i++) {
1346 if (vreg_table[i]) {
1347 if (enable)
1348 ret = sdhci_msm_vreg_enable(vreg_table[i]);
1349 else
1350 ret = sdhci_msm_vreg_disable(vreg_table[i]);
1351 if (ret)
1352 goto out;
1353 }
1354 }
1355out:
1356 return ret;
1357}
1358
1359/*
1360 * Reset vreg by ensuring it is off during probe. A call
1361 * to enable vreg is needed to balance disable vreg
1362 */
1363static int sdhci_msm_vreg_reset(struct sdhci_msm_pltfm_data *pdata)
1364{
1365 int ret;
1366
1367 ret = sdhci_msm_setup_vreg(pdata, 1, true);
1368 if (ret)
1369 return ret;
1370 ret = sdhci_msm_setup_vreg(pdata, 0, true);
1371 return ret;
1372}
1373
1374/* This init function should be called only once for each SDHC slot */
1375static int sdhci_msm_vreg_init(struct device *dev,
1376 struct sdhci_msm_pltfm_data *pdata,
1377 bool is_init)
1378{
1379 int ret = 0;
1380 struct sdhci_msm_slot_reg_data *curr_slot;
1381 struct sdhci_msm_reg_data *curr_vdd_reg, *curr_vdd_io_reg;
1382
1383 curr_slot = pdata->vreg_data;
1384 if (!curr_slot)
1385 goto out;
1386
1387 curr_vdd_reg = curr_slot->vdd_data;
1388 curr_vdd_io_reg = curr_slot->vdd_io_data;
1389
1390 if (!is_init)
1391 /* Deregister all regulators from regulator framework */
1392 goto vdd_io_reg_deinit;
1393
1394 /*
1395 * Get the regulator handle from voltage regulator framework
1396 * and then try to set the voltage level for the regulator
1397 */
1398 if (curr_vdd_reg) {
1399 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_reg);
1400 if (ret)
1401 goto out;
1402 }
1403 if (curr_vdd_io_reg) {
1404 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_io_reg);
1405 if (ret)
1406 goto vdd_reg_deinit;
1407 }
1408 ret = sdhci_msm_vreg_reset(pdata);
1409 if (ret)
1410 dev_err(dev, "vreg reset failed (%d)\n", ret);
1411 goto out;
1412
1413vdd_io_reg_deinit:
1414 if (curr_vdd_io_reg)
1415 sdhci_msm_vreg_deinit_reg(curr_vdd_io_reg);
1416vdd_reg_deinit:
1417 if (curr_vdd_reg)
1418 sdhci_msm_vreg_deinit_reg(curr_vdd_reg);
1419out:
1420 return ret;
1421}
1422
1423
1424static int sdhci_msm_set_vdd_io_vol(struct sdhci_msm_pltfm_data *pdata,
1425 enum vdd_io_level level,
1426 unsigned int voltage_level)
1427{
1428 int ret = 0;
1429 int set_level;
1430 struct sdhci_msm_reg_data *vdd_io_reg;
1431
1432 if (!pdata->vreg_data)
1433 return ret;
1434
1435 vdd_io_reg = pdata->vreg_data->vdd_io_data;
1436 if (vdd_io_reg && vdd_io_reg->is_enabled) {
1437 switch (level) {
1438 case VDD_IO_LOW:
1439 set_level = vdd_io_reg->low_vol_level;
1440 break;
1441 case VDD_IO_HIGH:
1442 set_level = vdd_io_reg->high_vol_level;
1443 break;
1444 case VDD_IO_SET_LEVEL:
1445 set_level = voltage_level;
1446 break;
1447 default:
1448 pr_err("%s: invalid argument level = %d",
1449 __func__, level);
1450 ret = -EINVAL;
1451 return ret;
1452 }
1453 ret = sdhci_msm_vreg_set_voltage(vdd_io_reg, set_level,
1454 set_level);
1455 }
1456 return ret;
1457}
1458
1459static irqreturn_t sdhci_msm_pwr_irq(int irq, void *data)
1460{
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001461 struct sdhci_host *host = (struct sdhci_host *)data;
1462 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1463 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Das0ef24812012-12-18 16:14:02 +05301464 u8 irq_status = 0;
1465 u8 irq_ack = 0;
1466 int ret = 0;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301467 int pwr_state = 0, io_level = 0;
1468 unsigned long flags;
Asutosh Das0ef24812012-12-18 16:14:02 +05301469
1470 irq_status = readb_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
1471 pr_debug("%s: Received IRQ(%d), status=0x%x\n",
1472 mmc_hostname(msm_host->mmc), irq, irq_status);
1473
1474 /* Clear the interrupt */
1475 writeb_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
1476 /*
1477 * SDHC has core_mem and hc_mem device memory and these memory
1478 * addresses do not fall within 1KB region. Hence, any update to
1479 * core_mem address space would require an mb() to ensure this gets
1480 * completed before its next update to registers within hc_mem.
1481 */
1482 mb();
1483
1484 /* Handle BUS ON/OFF*/
1485 if (irq_status & CORE_PWRCTL_BUS_ON) {
1486 ret = sdhci_msm_setup_vreg(msm_host->pdata, true, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301487 if (!ret) {
Asutosh Das0ef24812012-12-18 16:14:02 +05301488 ret = sdhci_msm_setup_pins(msm_host->pdata, true);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301489 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
1490 VDD_IO_HIGH, 0);
1491 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301492 if (ret)
1493 irq_ack |= CORE_PWRCTL_BUS_FAIL;
1494 else
1495 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301496
1497 pwr_state = REQ_BUS_ON;
1498 io_level = REQ_IO_HIGH;
Asutosh Das0ef24812012-12-18 16:14:02 +05301499 }
1500 if (irq_status & CORE_PWRCTL_BUS_OFF) {
1501 ret = sdhci_msm_setup_vreg(msm_host->pdata, false, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301502 if (!ret) {
Asutosh Das0ef24812012-12-18 16:14:02 +05301503 ret = sdhci_msm_setup_pins(msm_host->pdata, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301504 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
1505 VDD_IO_LOW, 0);
1506 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301507 if (ret)
1508 irq_ack |= CORE_PWRCTL_BUS_FAIL;
1509 else
1510 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301511
1512 pwr_state = REQ_BUS_OFF;
1513 io_level = REQ_IO_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05301514 }
1515 /* Handle IO LOW/HIGH */
1516 if (irq_status & CORE_PWRCTL_IO_LOW) {
1517 /* Switch voltage Low */
1518 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_LOW, 0);
1519 if (ret)
1520 irq_ack |= CORE_PWRCTL_IO_FAIL;
1521 else
1522 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301523
1524 io_level = REQ_IO_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05301525 }
1526 if (irq_status & CORE_PWRCTL_IO_HIGH) {
1527 /* Switch voltage High */
1528 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_HIGH, 0);
1529 if (ret)
1530 irq_ack |= CORE_PWRCTL_IO_FAIL;
1531 else
1532 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301533
1534 io_level = REQ_IO_HIGH;
Asutosh Das0ef24812012-12-18 16:14:02 +05301535 }
1536
1537 /* ACK status to the core */
1538 writeb_relaxed(irq_ack, (msm_host->core_mem + CORE_PWRCTL_CTL));
1539 /*
1540 * SDHC has core_mem and hc_mem device memory and these memory
1541 * addresses do not fall within 1KB region. Hence, any update to
1542 * core_mem address space would require an mb() to ensure this gets
1543 * completed before its next update to registers within hc_mem.
1544 */
1545 mb();
1546
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301547 if (io_level & REQ_IO_HIGH)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001548 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
1549 ~CORE_IO_PAD_PWR_SWITCH),
1550 host->ioaddr + CORE_VENDOR_SPEC);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301551 else if (io_level & REQ_IO_LOW)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001552 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
1553 CORE_IO_PAD_PWR_SWITCH),
1554 host->ioaddr + CORE_VENDOR_SPEC);
1555 mb();
1556
Asutosh Das0ef24812012-12-18 16:14:02 +05301557 pr_debug("%s: Handled IRQ(%d), ret=%d, ack=0x%x\n",
1558 mmc_hostname(msm_host->mmc), irq, ret, irq_ack);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301559 spin_lock_irqsave(&host->lock, flags);
1560 if (pwr_state)
1561 msm_host->curr_pwr_state = pwr_state;
1562 if (io_level)
1563 msm_host->curr_io_level = io_level;
1564 complete(&msm_host->pwr_irq_completion);
1565 spin_unlock_irqrestore(&host->lock, flags);
1566
Asutosh Das0ef24812012-12-18 16:14:02 +05301567 return IRQ_HANDLED;
1568}
1569
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301570static ssize_t
Sahitya Tummala5c55b932013-06-20 14:00:18 +05301571show_polling(struct device *dev, struct device_attribute *attr, char *buf)
1572{
1573 struct sdhci_host *host = dev_get_drvdata(dev);
1574 int poll;
1575 unsigned long flags;
1576
1577 spin_lock_irqsave(&host->lock, flags);
1578 poll = !!(host->mmc->caps & MMC_CAP_NEEDS_POLL);
1579 spin_unlock_irqrestore(&host->lock, flags);
1580
1581 return snprintf(buf, PAGE_SIZE, "%d\n", poll);
1582}
1583
1584static ssize_t
1585store_polling(struct device *dev, struct device_attribute *attr,
1586 const char *buf, size_t count)
1587{
1588 struct sdhci_host *host = dev_get_drvdata(dev);
1589 int value;
1590 unsigned long flags;
1591
1592 if (!kstrtou32(buf, 0, &value)) {
1593 spin_lock_irqsave(&host->lock, flags);
1594 if (value) {
1595 host->mmc->caps |= MMC_CAP_NEEDS_POLL;
1596 mmc_detect_change(host->mmc, 0);
1597 } else {
1598 host->mmc->caps &= ~MMC_CAP_NEEDS_POLL;
1599 }
1600 spin_unlock_irqrestore(&host->lock, flags);
1601 }
1602 return count;
1603}
1604
1605static ssize_t
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301606show_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
1607 char *buf)
1608{
1609 struct sdhci_host *host = dev_get_drvdata(dev);
1610 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1611 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1612
1613 return snprintf(buf, PAGE_SIZE, "%u\n",
1614 msm_host->msm_bus_vote.is_max_bw_needed);
1615}
1616
1617static ssize_t
1618store_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
1619 const char *buf, size_t count)
1620{
1621 struct sdhci_host *host = dev_get_drvdata(dev);
1622 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1623 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1624 uint32_t value;
1625 unsigned long flags;
1626
1627 if (!kstrtou32(buf, 0, &value)) {
1628 spin_lock_irqsave(&host->lock, flags);
1629 msm_host->msm_bus_vote.is_max_bw_needed = !!value;
1630 spin_unlock_irqrestore(&host->lock, flags);
1631 }
1632 return count;
1633}
1634
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301635static void sdhci_msm_check_power_status(struct sdhci_host *host, u32 req_type)
Asutosh Das0ef24812012-12-18 16:14:02 +05301636{
1637 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1638 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301639 unsigned long flags;
1640 bool done = false;
Asutosh Das0ef24812012-12-18 16:14:02 +05301641
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301642 spin_lock_irqsave(&host->lock, flags);
1643 pr_debug("%s: %s: request %d curr_pwr_state %x curr_io_level %x\n",
1644 mmc_hostname(host->mmc), __func__, req_type,
1645 msm_host->curr_pwr_state, msm_host->curr_io_level);
1646 if ((req_type & msm_host->curr_pwr_state) ||
1647 (req_type & msm_host->curr_io_level))
1648 done = true;
1649 spin_unlock_irqrestore(&host->lock, flags);
Asutosh Das0ef24812012-12-18 16:14:02 +05301650
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301651 /*
1652 * This is needed here to hanlde a case where IRQ gets
1653 * triggered even before this function is called so that
1654 * x->done counter of completion gets reset. Otherwise,
1655 * next call to wait_for_completion returns immediately
1656 * without actually waiting for the IRQ to be handled.
1657 */
1658 if (done)
1659 init_completion(&msm_host->pwr_irq_completion);
1660 else
1661 wait_for_completion(&msm_host->pwr_irq_completion);
1662
1663 pr_debug("%s: %s: request %d done\n", mmc_hostname(host->mmc),
1664 __func__, req_type);
Asutosh Das0ef24812012-12-18 16:14:02 +05301665}
1666
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001667static void sdhci_msm_toggle_cdr(struct sdhci_host *host, bool enable)
1668{
1669 if (enable)
1670 writel_relaxed((readl_relaxed(host->ioaddr +
1671 CORE_DLL_CONFIG) | CORE_CDR_EN),
1672 host->ioaddr + CORE_DLL_CONFIG);
1673 else
1674 writel_relaxed((readl_relaxed(host->ioaddr +
1675 CORE_DLL_CONFIG) & ~CORE_CDR_EN),
1676 host->ioaddr + CORE_DLL_CONFIG);
1677}
1678
Asutosh Das648f9d12013-01-10 21:11:04 +05301679static unsigned int sdhci_msm_max_segs(void)
1680{
1681 return SDHCI_MSM_MAX_SEGMENTS;
1682}
1683
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301684static unsigned int sdhci_msm_get_min_clock(struct sdhci_host *host)
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301685{
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301686 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1687 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301688
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301689 return msm_host->pdata->sup_clk_table[0];
1690}
1691
1692static unsigned int sdhci_msm_get_max_clock(struct sdhci_host *host)
1693{
1694 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1695 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1696 int max_clk_index = msm_host->pdata->sup_clk_cnt;
1697
1698 return msm_host->pdata->sup_clk_table[max_clk_index - 1];
1699}
1700
1701static unsigned int sdhci_msm_get_sup_clk_rate(struct sdhci_host *host,
1702 u32 req_clk)
1703{
1704 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1705 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1706 unsigned int sel_clk = -1;
1707 unsigned char cnt;
1708
1709 if (req_clk < sdhci_msm_get_min_clock(host)) {
1710 sel_clk = sdhci_msm_get_min_clock(host);
1711 return sel_clk;
1712 }
1713
1714 for (cnt = 0; cnt < msm_host->pdata->sup_clk_cnt; cnt++) {
1715 if (msm_host->pdata->sup_clk_table[cnt] > req_clk) {
1716 break;
1717 } else if (msm_host->pdata->sup_clk_table[cnt] == req_clk) {
1718 sel_clk = msm_host->pdata->sup_clk_table[cnt];
1719 break;
1720 } else {
1721 sel_clk = msm_host->pdata->sup_clk_table[cnt];
1722 }
1723 }
1724 return sel_clk;
1725}
1726
1727static int sdhci_msm_prepare_clocks(struct sdhci_host *host, bool enable)
1728{
1729 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1730 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1731 int rc = 0;
1732
1733 if (enable && !atomic_read(&msm_host->clks_on)) {
1734 pr_debug("%s: request to enable clocks\n",
1735 mmc_hostname(host->mmc));
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301736
1737 sdhci_msm_bus_voting(host, 1);
1738
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301739 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
1740 rc = clk_prepare_enable(msm_host->bus_clk);
1741 if (rc) {
1742 pr_err("%s: %s: failed to enable the bus-clock with error %d\n",
1743 mmc_hostname(host->mmc), __func__, rc);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301744 goto remove_vote;
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301745 }
1746 }
1747 if (!IS_ERR(msm_host->pclk)) {
1748 rc = clk_prepare_enable(msm_host->pclk);
1749 if (rc) {
1750 pr_err("%s: %s: failed to enable the pclk with error %d\n",
1751 mmc_hostname(host->mmc), __func__, rc);
1752 goto disable_bus_clk;
1753 }
1754 }
1755 rc = clk_prepare_enable(msm_host->clk);
1756 if (rc) {
1757 pr_err("%s: %s: failed to enable the host-clk with error %d\n",
1758 mmc_hostname(host->mmc), __func__, rc);
1759 goto disable_pclk;
1760 }
1761 mb();
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301762
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301763 } else if (!enable && atomic_read(&msm_host->clks_on)) {
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301764 pr_debug("%s: request to disable clocks\n",
1765 mmc_hostname(host->mmc));
1766 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
1767 mb();
1768 clk_disable_unprepare(msm_host->clk);
1769 if (!IS_ERR(msm_host->pclk))
1770 clk_disable_unprepare(msm_host->pclk);
1771 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
1772 clk_disable_unprepare(msm_host->bus_clk);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301773
1774 sdhci_msm_bus_voting(host, 0);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301775 }
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301776 atomic_set(&msm_host->clks_on, enable);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301777 goto out;
1778disable_pclk:
1779 if (!IS_ERR_OR_NULL(msm_host->pclk))
1780 clk_disable_unprepare(msm_host->pclk);
1781disable_bus_clk:
1782 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
1783 clk_disable_unprepare(msm_host->bus_clk);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301784remove_vote:
1785 if (msm_host->msm_bus_vote.client_handle)
1786 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301787out:
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301788 return rc;
1789}
1790
1791static void sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock)
1792{
1793 int rc;
1794 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1795 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1796 struct mmc_ios curr_ios = host->mmc->ios;
1797 u32 sup_clock, ddr_clock;
1798
1799 if (!clock) {
1800 sdhci_msm_prepare_clocks(host, false);
1801 host->clock = clock;
1802 goto out;
1803 }
1804
1805 rc = sdhci_msm_prepare_clocks(host, true);
1806 if (rc)
1807 goto out;
1808
1809 sup_clock = sdhci_msm_get_sup_clk_rate(host, clock);
1810 if (curr_ios.timing == MMC_TIMING_UHS_DDR50) {
1811 /*
1812 * The SDHC requires internal clock frequency to be double the
1813 * actual clock that will be set for DDR mode. The controller
1814 * uses the faster clock(100MHz) for some of its parts and send
1815 * the actual required clock (50MHz) to the card.
1816 */
1817 ddr_clock = clock * 2;
1818 sup_clock = sdhci_msm_get_sup_clk_rate(host,
1819 ddr_clock);
1820 }
1821 if (sup_clock != msm_host->clk_rate) {
1822 pr_debug("%s: %s: setting clk rate to %u\n",
1823 mmc_hostname(host->mmc), __func__, sup_clock);
1824 rc = clk_set_rate(msm_host->clk, sup_clock);
1825 if (rc) {
1826 pr_err("%s: %s: Failed to set rate %u for host-clk : %d\n",
1827 mmc_hostname(host->mmc), __func__,
1828 sup_clock, rc);
1829 goto out;
1830 }
1831 msm_host->clk_rate = sup_clock;
1832 host->clock = clock;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301833 /*
1834 * Update the bus vote in case of frequency change due to
1835 * clock scaling.
1836 */
1837 sdhci_msm_bus_voting(host, 1);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301838 }
1839out:
1840 sdhci_set_clock(host, clock);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301841}
1842
Sahitya Tummala14613432013-03-21 11:13:25 +05301843static void sdhci_msm_set_uhs_signaling(struct sdhci_host *host,
1844 unsigned int uhs)
1845{
1846 u16 ctrl_2;
1847
1848 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1849 /* Select Bus Speed Mode for host */
1850 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1851 if (uhs == MMC_TIMING_MMC_HS200)
1852 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1853 else if (uhs == MMC_TIMING_UHS_SDR12)
1854 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1855 else if (uhs == MMC_TIMING_UHS_SDR25)
1856 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1857 else if (uhs == MMC_TIMING_UHS_SDR50)
1858 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1859 else if (uhs == MMC_TIMING_UHS_SDR104)
1860 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1861 else if (uhs == MMC_TIMING_UHS_DDR50)
1862 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301863 /*
1864 * When clock frquency is less than 100MHz, the feedback clock must be
1865 * provided and DLL must not be used so that tuning can be skipped. To
1866 * provide feedback clock, the mode selection can be any value less
1867 * than 3'b011 in bits [2:0] of HOST CONTROL2 register.
1868 */
1869 if (host->clock <= (100 * 1000 * 1000) &&
1870 (uhs == MMC_TIMING_MMC_HS200 ||
1871 uhs == MMC_TIMING_UHS_SDR104))
1872 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1873
Sahitya Tummala14613432013-03-21 11:13:25 +05301874 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1875
1876}
1877
Asutosh Das0ef24812012-12-18 16:14:02 +05301878static struct sdhci_ops sdhci_msm_ops = {
Sahitya Tummala14613432013-03-21 11:13:25 +05301879 .set_uhs_signaling = sdhci_msm_set_uhs_signaling,
Asutosh Das0ef24812012-12-18 16:14:02 +05301880 .check_power_status = sdhci_msm_check_power_status,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001881 .platform_execute_tuning = sdhci_msm_execute_tuning,
1882 .toggle_cdr = sdhci_msm_toggle_cdr,
Asutosh Das648f9d12013-01-10 21:11:04 +05301883 .get_max_segments = sdhci_msm_max_segs,
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301884 .set_clock = sdhci_msm_set_clock,
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301885 .get_min_clock = sdhci_msm_get_min_clock,
1886 .get_max_clock = sdhci_msm_get_max_clock,
Asutosh Das0ef24812012-12-18 16:14:02 +05301887};
1888
1889static int sdhci_msm_probe(struct platform_device *pdev)
1890{
1891 struct sdhci_host *host;
1892 struct sdhci_pltfm_host *pltfm_host;
1893 struct sdhci_msm_host *msm_host;
1894 struct resource *core_memres = NULL;
1895 int ret = 0, pwr_irq = 0, dead = 0;
Stephen Boyd8dce5c62013-04-24 14:19:46 -07001896 u16 host_version;
Subhash Jadavani28137342013-05-14 17:46:43 +05301897 u32 pwr, irq_status, irq_ctl;
Asutosh Das0ef24812012-12-18 16:14:02 +05301898
1899 pr_debug("%s: Enter %s\n", dev_name(&pdev->dev), __func__);
1900 msm_host = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_msm_host),
1901 GFP_KERNEL);
1902 if (!msm_host) {
1903 ret = -ENOMEM;
1904 goto out;
1905 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301906
1907 msm_host->sdhci_msm_pdata.ops = &sdhci_msm_ops;
1908 host = sdhci_pltfm_init(pdev, &msm_host->sdhci_msm_pdata, 0);
1909 if (IS_ERR(host)) {
1910 ret = PTR_ERR(host);
1911 goto out;
1912 }
1913
1914 pltfm_host = sdhci_priv(host);
1915 pltfm_host->priv = msm_host;
1916 msm_host->mmc = host->mmc;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301917 msm_host->pdev = pdev;
Asutosh Das0ef24812012-12-18 16:14:02 +05301918
1919 /* Extract platform data */
1920 if (pdev->dev.of_node) {
Venkat Gopalakrishnan270580a2013-03-11 12:17:57 -07001921 ret = of_alias_get_id(pdev->dev.of_node, "sdhc");
1922 if (ret < 0) {
1923 dev_err(&pdev->dev, "Failed to get slot index %d\n",
1924 ret);
1925 goto pltfm_free;
1926 }
1927 if (disable_slots & (1 << (ret - 1))) {
1928 dev_info(&pdev->dev, "%s: Slot %d disabled\n", __func__,
1929 ret);
1930 ret = -ENODEV;
1931 goto pltfm_free;
1932 }
1933
Asutosh Das0ef24812012-12-18 16:14:02 +05301934 msm_host->pdata = sdhci_msm_populate_pdata(&pdev->dev);
1935 if (!msm_host->pdata) {
1936 dev_err(&pdev->dev, "DT parsing error\n");
1937 goto pltfm_free;
1938 }
1939 } else {
1940 dev_err(&pdev->dev, "No device tree node\n");
1941 goto pltfm_free;
1942 }
1943
1944 /* Setup Clocks */
1945
1946 /* Setup SDCC bus voter clock. */
1947 msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus_clk");
1948 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
1949 /* Vote for max. clk rate for max. performance */
1950 ret = clk_set_rate(msm_host->bus_clk, INT_MAX);
1951 if (ret)
1952 goto pltfm_free;
1953 ret = clk_prepare_enable(msm_host->bus_clk);
1954 if (ret)
1955 goto pltfm_free;
1956 }
1957
1958 /* Setup main peripheral bus clock */
1959 msm_host->pclk = devm_clk_get(&pdev->dev, "iface_clk");
1960 if (!IS_ERR(msm_host->pclk)) {
1961 ret = clk_prepare_enable(msm_host->pclk);
1962 if (ret)
1963 goto bus_clk_disable;
1964 }
1965
1966 /* Setup SDC MMC clock */
1967 msm_host->clk = devm_clk_get(&pdev->dev, "core_clk");
1968 if (IS_ERR(msm_host->clk)) {
1969 ret = PTR_ERR(msm_host->clk);
1970 goto pclk_disable;
1971 }
1972
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301973 /* Set to the minimum supported clock frequency */
1974 ret = clk_set_rate(msm_host->clk, sdhci_msm_get_min_clock(host));
1975 if (ret) {
1976 dev_err(&pdev->dev, "MClk rate set failed (%d)\n", ret);
Sahitya Tummala020ede0d2013-06-07 13:03:07 +05301977 goto pclk_disable;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301978 }
Sahitya Tummala020ede0d2013-06-07 13:03:07 +05301979 ret = clk_prepare_enable(msm_host->clk);
1980 if (ret)
1981 goto pclk_disable;
1982
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301983 msm_host->clk_rate = sdhci_msm_get_min_clock(host);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301984 atomic_set(&msm_host->clks_on, 1);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301985
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301986 ret = sdhci_msm_bus_register(msm_host, pdev);
1987 if (ret)
1988 goto clk_disable;
1989
1990 if (msm_host->msm_bus_vote.client_handle)
1991 INIT_DELAYED_WORK(&msm_host->msm_bus_vote.vote_work,
1992 sdhci_msm_bus_work);
1993 sdhci_msm_bus_voting(host, 1);
1994
Asutosh Das0ef24812012-12-18 16:14:02 +05301995 /* Setup regulators */
1996 ret = sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, true);
1997 if (ret) {
1998 dev_err(&pdev->dev, "Regulator setup failed (%d)\n", ret);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301999 goto bus_unregister;
Asutosh Das0ef24812012-12-18 16:14:02 +05302000 }
2001
2002 /* Reset the core and Enable SDHC mode */
2003 core_memres = platform_get_resource_byname(pdev,
2004 IORESOURCE_MEM, "core_mem");
2005 msm_host->core_mem = devm_ioremap(&pdev->dev, core_memres->start,
2006 resource_size(core_memres));
2007
2008 if (!msm_host->core_mem) {
2009 dev_err(&pdev->dev, "Failed to remap registers\n");
2010 ret = -ENOMEM;
2011 goto vreg_deinit;
2012 }
2013
2014 /* Set SW_RST bit in POWER register (Offset 0x0) */
Sahitya Tummala66b0fe32013-04-25 11:50:56 +05302015 writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_POWER) |
2016 CORE_SW_RST, msm_host->core_mem + CORE_POWER);
2017 /*
2018 * SW reset can take upto 10HCLK + 15MCLK cycles.
2019 * Calculating based on min clk rates (hclk = 27MHz,
2020 * mclk = 400KHz) it comes to ~40us. Let's poll for
2021 * max. 1ms for reset completion.
2022 */
2023 ret = readl_poll_timeout(msm_host->core_mem + CORE_POWER,
2024 pwr, !(pwr & CORE_SW_RST), 100, 10);
2025
2026 if (ret) {
2027 dev_err(&pdev->dev, "reset failed (%d)\n", ret);
2028 goto vreg_deinit;
2029 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302030 /* Set HC_MODE_EN bit in HC_MODE register */
2031 writel_relaxed(HC_MODE_EN, (msm_host->core_mem + CORE_HC_MODE));
2032
2033 /*
Subhash Jadavani28137342013-05-14 17:46:43 +05302034 * CORE_SW_RST above may trigger power irq if previous status of PWRCTL
2035 * was either BUS_ON or IO_HIGH_V. So before we enable the power irq
2036 * interrupt in GIC (by registering the interrupt handler), we need to
2037 * ensure that any pending power irq interrupt status is acknowledged
2038 * otherwise power irq interrupt handler would be fired prematurely.
2039 */
2040 irq_status = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
2041 writel_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
2042 irq_ctl = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_CTL);
2043 if (irq_status & (CORE_PWRCTL_BUS_ON | CORE_PWRCTL_BUS_OFF))
2044 irq_ctl |= CORE_PWRCTL_BUS_SUCCESS;
2045 if (irq_status & (CORE_PWRCTL_IO_HIGH | CORE_PWRCTL_IO_LOW))
2046 irq_ctl |= CORE_PWRCTL_IO_SUCCESS;
2047 writel_relaxed(irq_ctl, (msm_host->core_mem + CORE_PWRCTL_CTL));
2048 /*
2049 * Ensure that above writes are propogated before interrupt enablement
2050 * in GIC.
2051 */
2052 mb();
2053
2054 /*
Asutosh Das0ef24812012-12-18 16:14:02 +05302055 * Following are the deviations from SDHC spec v3.0 -
2056 * 1. Card detection is handled using separate GPIO.
2057 * 2. Bus power control is handled by interacting with PMIC.
2058 */
2059 host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
2060 host->quirks |= SDHCI_QUIRK_SINGLE_POWER_WRITE;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302061 host->quirks |= SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN;
2062 host->quirks2 |= SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK;
Sahitya Tummala87d439442013-04-12 11:49:11 +05302063 host->quirks2 |= SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD;
Sahitya Tummala314162c2013-04-12 12:11:20 +05302064 host->quirks2 |= SDHCI_QUIRK2_BROKEN_PRESET_VALUE;
Sahitya Tummala7c9780d2013-04-12 11:59:25 +05302065 host->quirks2 |= SDHCI_QUIRK2_USE_RESERVED_MAX_TIMEOUT;
Asutosh Das0ef24812012-12-18 16:14:02 +05302066
Sahitya Tummalaa5733ab52013-06-10 16:32:51 +05302067 if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK)
2068 host->quirks2 |= SDHCI_QUIRK2_DIVIDE_TOUT_BY_4;
2069
Stephen Boyd8dce5c62013-04-24 14:19:46 -07002070 host_version = readw_relaxed((host->ioaddr + SDHCI_HOST_VERSION));
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07002071 dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n",
2072 host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >>
2073 SDHCI_VENDOR_VER_SHIFT));
2074 if (((host_version & SDHCI_VENDOR_VER_MASK) >>
2075 SDHCI_VENDOR_VER_SHIFT) == SDHCI_VER_100) {
2076 /*
2077 * Add 40us delay in interrupt handler when
2078 * operating at initialization frequency(400KHz).
2079 */
2080 host->quirks2 |= SDHCI_QUIRK2_SLOW_INT_CLR;
2081 /*
2082 * Set Software Reset for DAT line in Software
2083 * Reset Register (Bit 2).
2084 */
2085 host->quirks2 |= SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT;
2086 }
2087
2088 /* Setup PWRCTL irq */
Asutosh Das0ef24812012-12-18 16:14:02 +05302089 pwr_irq = platform_get_irq_byname(pdev, "pwr_irq");
2090 if (pwr_irq < 0) {
2091 dev_err(&pdev->dev, "Failed to get pwr_irq by name (%d)\n",
2092 pwr_irq);
2093 goto vreg_deinit;
2094 }
2095 ret = devm_request_threaded_irq(&pdev->dev, pwr_irq, NULL,
2096 sdhci_msm_pwr_irq, IRQF_ONESHOT,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002097 dev_name(&pdev->dev), host);
Asutosh Das0ef24812012-12-18 16:14:02 +05302098 if (ret) {
2099 dev_err(&pdev->dev, "Request threaded irq(%d) failed (%d)\n",
2100 pwr_irq, ret);
2101 goto vreg_deinit;
2102 }
2103
2104 /* Enable pwr irq interrupts */
2105 writel_relaxed(INT_MASK, (msm_host->core_mem + CORE_PWRCTL_MASK));
2106
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302107#ifdef CONFIG_MMC_CLKGATE
2108 /* Set clock gating delay to be used when CONFIG_MMC_CLKGATE is set */
2109 msm_host->mmc->clkgate_delay = SDHCI_MSM_MMC_CLK_GATE_DELAY;
2110#endif
2111
Asutosh Das0ef24812012-12-18 16:14:02 +05302112 /* Set host capabilities */
2113 msm_host->mmc->caps |= msm_host->pdata->mmc_bus_width;
2114 msm_host->mmc->caps |= msm_host->pdata->caps;
Asutosh Das0ef24812012-12-18 16:14:02 +05302115 msm_host->mmc->caps2 |= msm_host->pdata->caps2;
2116 msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR;
2117 msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR_CONTROL;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302118 msm_host->mmc->caps2 |= MMC_CAP2_CLK_SCALE;
Subhash Jadavani6d472b22013-05-29 15:52:10 +05302119 msm_host->mmc->caps2 |= MMC_CAP2_ASYNC_SDIO_IRQ_4BIT_MODE;
Asutosh Dasbea115d2013-06-24 18:20:45 +05302120 msm_host->mmc->pm_caps |= MMC_PM_KEEP_POWER;
Asutosh Das0ef24812012-12-18 16:14:02 +05302121
2122 if (msm_host->pdata->nonremovable)
2123 msm_host->mmc->caps |= MMC_CAP_NONREMOVABLE;
2124
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05302125 host->cpu_dma_latency_us = msm_host->pdata->cpu_dma_latency_us;
2126
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302127 init_completion(&msm_host->pwr_irq_completion);
2128
Sahitya Tummala581df132013-03-12 14:57:46 +05302129 if (gpio_is_valid(msm_host->pdata->status_gpio)) {
2130 ret = mmc_gpio_request_cd(msm_host->mmc,
2131 msm_host->pdata->status_gpio, 0);
2132 if (ret) {
2133 dev_err(&pdev->dev, "%s: Failed to request card detection IRQ %d\n",
2134 __func__, ret);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302135 goto vreg_deinit;
Sahitya Tummala581df132013-03-12 14:57:46 +05302136 }
2137 }
2138
Sahitya Tummalaeaa21862013-03-20 19:34:59 +05302139 if (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(32))) {
2140 host->dma_mask = DMA_BIT_MASK(32);
2141 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2142 } else {
2143 dev_err(&pdev->dev, "%s: Failed to set dma mask\n", __func__);
2144 }
2145
Asutosh Das0ef24812012-12-18 16:14:02 +05302146 ret = sdhci_add_host(host);
2147 if (ret) {
2148 dev_err(&pdev->dev, "Add host failed (%d)\n", ret);
Sahitya Tummala581df132013-03-12 14:57:46 +05302149 goto vreg_deinit;
Asutosh Das0ef24812012-12-18 16:14:02 +05302150 }
2151
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302152 msm_host->msm_bus_vote.max_bus_bw.show = show_sdhci_max_bus_bw;
2153 msm_host->msm_bus_vote.max_bus_bw.store = store_sdhci_max_bus_bw;
2154 sysfs_attr_init(&msm_host->msm_bus_vote.max_bus_bw.attr);
2155 msm_host->msm_bus_vote.max_bus_bw.attr.name = "max_bus_bw";
2156 msm_host->msm_bus_vote.max_bus_bw.attr.mode = S_IRUGO | S_IWUSR;
2157 ret = device_create_file(&pdev->dev,
2158 &msm_host->msm_bus_vote.max_bus_bw);
2159 if (ret)
2160 goto remove_host;
2161
Sahitya Tummala5c55b932013-06-20 14:00:18 +05302162 if (!gpio_is_valid(msm_host->pdata->status_gpio)) {
2163 msm_host->polling.show = show_polling;
2164 msm_host->polling.store = store_polling;
2165 sysfs_attr_init(&msm_host->polling.attr);
2166 msm_host->polling.attr.name = "polling";
2167 msm_host->polling.attr.mode = S_IRUGO | S_IWUSR;
2168 ret = device_create_file(&pdev->dev, &msm_host->polling);
2169 if (ret)
2170 goto remove_max_bus_bw_file;
2171 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302172 /* Successful initialization */
2173 goto out;
2174
Sahitya Tummala5c55b932013-06-20 14:00:18 +05302175remove_max_bus_bw_file:
2176 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Asutosh Das0ef24812012-12-18 16:14:02 +05302177remove_host:
2178 dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
2179 sdhci_remove_host(host, dead);
2180vreg_deinit:
2181 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302182bus_unregister:
2183 if (msm_host->msm_bus_vote.client_handle)
2184 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2185 sdhci_msm_bus_unregister(msm_host);
Asutosh Das0ef24812012-12-18 16:14:02 +05302186clk_disable:
2187 if (!IS_ERR(msm_host->clk))
2188 clk_disable_unprepare(msm_host->clk);
2189pclk_disable:
2190 if (!IS_ERR(msm_host->pclk))
2191 clk_disable_unprepare(msm_host->pclk);
2192bus_clk_disable:
2193 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2194 clk_disable_unprepare(msm_host->bus_clk);
2195pltfm_free:
2196 sdhci_pltfm_free(pdev);
2197out:
2198 pr_debug("%s: Exit %s\n", dev_name(&pdev->dev), __func__);
2199 return ret;
2200}
2201
2202static int sdhci_msm_remove(struct platform_device *pdev)
2203{
2204 struct sdhci_host *host = platform_get_drvdata(pdev);
2205 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2206 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2207 struct sdhci_msm_pltfm_data *pdata = msm_host->pdata;
2208 int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) ==
2209 0xffffffff);
2210
2211 pr_debug("%s: %s\n", dev_name(&pdev->dev), __func__);
Sahitya Tummala5c55b932013-06-20 14:00:18 +05302212 if (!gpio_is_valid(msm_host->pdata->status_gpio))
2213 device_remove_file(&pdev->dev, &msm_host->polling);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302214 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Asutosh Das0ef24812012-12-18 16:14:02 +05302215 sdhci_remove_host(host, dead);
2216 sdhci_pltfm_free(pdev);
Sahitya Tummala581df132013-03-12 14:57:46 +05302217
Asutosh Das0ef24812012-12-18 16:14:02 +05302218 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302219
Asutosh Das0ef24812012-12-18 16:14:02 +05302220 if (pdata->pin_data)
2221 sdhci_msm_setup_gpio(pdata, false);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302222
2223 if (msm_host->msm_bus_vote.client_handle) {
2224 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2225 sdhci_msm_bus_unregister(msm_host);
2226 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302227 return 0;
2228}
2229
2230static const struct of_device_id sdhci_msm_dt_match[] = {
2231 {.compatible = "qcom,sdhci-msm"},
2232};
2233MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match);
2234
2235static struct platform_driver sdhci_msm_driver = {
2236 .probe = sdhci_msm_probe,
2237 .remove = sdhci_msm_remove,
2238 .driver = {
2239 .name = "sdhci_msm",
2240 .owner = THIS_MODULE,
2241 .of_match_table = sdhci_msm_dt_match,
2242 },
2243};
2244
2245module_platform_driver(sdhci_msm_driver);
2246
2247MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Secure Digital Host Controller Interface driver");
2248MODULE_LICENSE("GPL v2");