blob: ecceca57b72a01c910b69898fff3a54bfeba1760 [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
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -070045#define FF_CLK_SW_RST_DIS (1 << 13)
Asutosh Das0ef24812012-12-18 16:14:02 +053046
Sahitya Tummala481fbb02013-08-06 15:22:28 +053047#define CORE_GENERICS 0x70
48#define SWITCHABLE_SIGNALLING_VOL (1 << 29)
49
Asutosh Das0ef24812012-12-18 16:14:02 +053050#define CORE_POWER 0x0
51#define CORE_SW_RST (1 << 7)
52
Sahitya Tummala67717bc2013-08-02 09:21:37 +053053#define CORE_MCI_VERSION 0x050
54#define CORE_TESTBUS_CONFIG 0x0CC
55#define CORE_TESTBUS_ENA (1 << 3)
56#define CORE_SDCC_DEBUG_REG 0x124
57
Asutosh Das0ef24812012-12-18 16:14:02 +053058#define CORE_PWRCTL_STATUS 0xDC
59#define CORE_PWRCTL_MASK 0xE0
60#define CORE_PWRCTL_CLEAR 0xE4
61#define CORE_PWRCTL_CTL 0xE8
62
63#define CORE_PWRCTL_BUS_OFF 0x01
64#define CORE_PWRCTL_BUS_ON (1 << 1)
65#define CORE_PWRCTL_IO_LOW (1 << 2)
66#define CORE_PWRCTL_IO_HIGH (1 << 3)
67
68#define CORE_PWRCTL_BUS_SUCCESS 0x01
69#define CORE_PWRCTL_BUS_FAIL (1 << 1)
70#define CORE_PWRCTL_IO_SUCCESS (1 << 2)
71#define CORE_PWRCTL_IO_FAIL (1 << 3)
72
73#define INT_MASK 0xF
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -070074#define MAX_PHASES 16
75
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -070076#define CORE_DLL_CONFIG 0x100
77#define CORE_CMD_DAT_TRACK_SEL (1 << 0)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -070078#define CORE_DLL_EN (1 << 16)
79#define CORE_CDR_EN (1 << 17)
80#define CORE_CK_OUT_EN (1 << 18)
81#define CORE_CDR_EXT_EN (1 << 19)
82#define CORE_DLL_PDN (1 << 29)
83#define CORE_DLL_RST (1 << 30)
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -070084
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -070085#define CORE_DLL_STATUS 0x108
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -070086#define CORE_DLL_LOCK (1 << 7)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -070087
88#define CORE_VENDOR_SPEC 0x10C
89#define CORE_CLK_PWRSAVE (1 << 1)
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -070090#define CORE_HC_MCLK_SEL_DFLT (2 << 8)
91#define CORE_HC_MCLK_SEL_HS400 (3 << 8)
92#define CORE_HC_MCLK_SEL_MASK (3 << 8)
Asutosh Dase5e9ca62013-07-30 19:08:36 +053093#define CORE_HC_AUTO_CMD21_EN (1 << 6)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -070094#define CORE_IO_PAD_PWR_SWITCH (1 << 16)
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -070095#define CORE_HC_SELECT_IN_EN (1 << 18)
96#define CORE_HC_SELECT_IN_HS400 (6 << 19)
97#define CORE_HC_SELECT_IN_MASK (7 << 19)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -070098
Sahitya Tummala67717bc2013-08-02 09:21:37 +053099#define CORE_VENDOR_SPEC_ADMA_ERR_ADDR0 0x114
100#define CORE_VENDOR_SPEC_ADMA_ERR_ADDR1 0x118
101
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700102#define CORE_CSR_CDC_CTLR_CFG0 0x130
103#define CORE_SW_TRIG_FULL_CALIB (1 << 16)
104#define CORE_HW_AUTOCAL_ENA (1 << 17)
105
106#define CORE_CSR_CDC_CTLR_CFG1 0x134
107#define CORE_CSR_CDC_CAL_TIMER_CFG0 0x138
108#define CORE_TIMER_ENA (1 << 16)
109
110#define CORE_CSR_CDC_CAL_TIMER_CFG1 0x13C
111#define CORE_CSR_CDC_REFCOUNT_CFG 0x140
112#define CORE_CSR_CDC_COARSE_CAL_CFG 0x144
113#define CORE_CDC_OFFSET_CFG 0x14C
114#define CORE_CSR_CDC_DELAY_CFG 0x150
115#define CORE_CDC_SLAVE_DDA_CFG 0x160
116#define CORE_CSR_CDC_STATUS0 0x164
117#define CORE_CALIBRATION_DONE (1 << 0)
118
119#define CORE_CDC_ERROR_CODE_MASK 0x7000000
120
121#define CORE_CSR_CDC_GEN_CFG 0x178
122#define CORE_CDC_SWITCH_BYPASS_OFF (1 << 0)
123#define CORE_CDC_SWITCH_RC_EN (1 << 1)
124
125#define CORE_DDR_200_CFG 0x184
126#define CORE_CDC_T4_DLY_SEL (1 << 0)
127#define CORE_START_CDC_TRAFFIC (1 << 6)
128
Sahitya Tummala67717bc2013-08-02 09:21:37 +0530129#define CORE_MCI_DATA_CNT 0x30
130#define CORE_MCI_STATUS 0x34
131#define CORE_MCI_FIFO_CNT 0x44
132
133#define CORE_TESTBUS_SEL2_BIT 4
134#define CORE_TESTBUS_SEL2 (1 << CORE_TESTBUS_SEL2_BIT)
135
Asutosh Das648f9d12013-01-10 21:11:04 +0530136/* 8KB descriptors */
137#define SDHCI_MSM_MAX_SEGMENTS (1 << 13)
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +0530138#define SDHCI_MSM_MMC_CLK_GATE_DELAY 200 /* msecs */
Asutosh Das648f9d12013-01-10 21:11:04 +0530139
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700140#define CORE_FREQ_100MHZ (100 * 1000 * 1000)
141
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700142#define INVALID_TUNING_PHASE -1
143
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700144static const u32 tuning_block_64[] = {
145 0x00FF0FFF, 0xCCC3CCFF, 0xFFCC3CC3, 0xEFFEFFFE,
146 0xDDFFDFFF, 0xFBFFFBFF, 0xFF7FFFBF, 0xEFBDF777,
147 0xF0FFF0FF, 0x3CCCFC0F, 0xCFCC33CC, 0xEEFFEFFF,
148 0xFDFFFDFF, 0xFFBFFFDF, 0xFFF7FFBB, 0xDE7B7FF7
149};
150
151static const u32 tuning_block_128[] = {
152 0xFF00FFFF, 0x0000FFFF, 0xCCCCFFFF, 0xCCCC33CC,
153 0xCC3333CC, 0xFFFFCCCC, 0xFFFFEEFF, 0xFFEEEEFF,
154 0xFFDDFFFF, 0xDDDDFFFF, 0xBBFFFFFF, 0xBBFFFFFF,
155 0xFFFFFFBB, 0xFFFFFF77, 0x77FF7777, 0xFFEEDDBB,
156 0x00FFFFFF, 0x00FFFFFF, 0xCCFFFF00, 0xCC33CCCC,
157 0x3333CCCC, 0xFFCCCCCC, 0xFFEEFFFF, 0xEEEEFFFF,
158 0xDDFFFFFF, 0xDDFFFFFF, 0xFFFFFFDD, 0xFFFFFFBB,
159 0xFFFFBBBB, 0xFFFF77FF, 0xFF7777FF, 0xEEDDBB77
160};
Asutosh Das0ef24812012-12-18 16:14:02 +0530161
Venkat Gopalakrishnan270580a2013-03-11 12:17:57 -0700162static int disable_slots;
163/* root can write, others read */
164module_param(disable_slots, int, S_IRUGO|S_IWUSR);
165
Asutosh Das0ef24812012-12-18 16:14:02 +0530166/* This structure keeps information per regulator */
167struct sdhci_msm_reg_data {
168 /* voltage regulator handle */
169 struct regulator *reg;
170 /* regulator name */
171 const char *name;
172 /* voltage level to be set */
173 u32 low_vol_level;
174 u32 high_vol_level;
175 /* Load values for low power and high power mode */
176 u32 lpm_uA;
177 u32 hpm_uA;
178
179 /* is this regulator enabled? */
180 bool is_enabled;
181 /* is this regulator needs to be always on? */
182 bool is_always_on;
183 /* is low power mode setting required for this regulator? */
184 bool lpm_sup;
185 bool set_voltage_sup;
186};
187
188/*
189 * This structure keeps information for all the
190 * regulators required for a SDCC slot.
191 */
192struct sdhci_msm_slot_reg_data {
193 /* keeps VDD/VCC regulator info */
194 struct sdhci_msm_reg_data *vdd_data;
195 /* keeps VDD IO regulator info */
196 struct sdhci_msm_reg_data *vdd_io_data;
197};
198
199struct sdhci_msm_gpio {
200 u32 no;
201 const char *name;
202 bool is_enabled;
203};
204
205struct sdhci_msm_gpio_data {
206 struct sdhci_msm_gpio *gpio;
207 u8 size;
208};
209
210struct sdhci_msm_pin_data {
211 /*
212 * = 1 if controller pins are using gpios
213 * = 0 if controller has dedicated MSM pads
214 */
215 bool cfg_sts;
216 struct sdhci_msm_gpio_data *gpio_data;
217};
218
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530219struct sdhci_msm_bus_voting_data {
220 struct msm_bus_scale_pdata *bus_pdata;
221 unsigned int *bw_vecs;
222 unsigned int bw_vecs_size;
223};
224
Asutosh Das0ef24812012-12-18 16:14:02 +0530225struct sdhci_msm_pltfm_data {
226 /* Supported UHS-I Modes */
227 u32 caps;
228
229 /* More capabilities */
230 u32 caps2;
231
232 unsigned long mmc_bus_width;
Asutosh Das0ef24812012-12-18 16:14:02 +0530233 struct sdhci_msm_slot_reg_data *vreg_data;
234 bool nonremovable;
235 struct sdhci_msm_pin_data *pin_data;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +0530236 u32 cpu_dma_latency_us;
Sahitya Tummala581df132013-03-12 14:57:46 +0530237 int status_gpio; /* card detection GPIO that is configured as IRQ */
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530238 struct sdhci_msm_bus_voting_data *voting_data;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530239 u32 *sup_clk_table;
240 unsigned char sup_clk_cnt;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530241};
242
243struct sdhci_msm_bus_vote {
244 uint32_t client_handle;
245 uint32_t curr_vote;
246 int min_bw_vote;
247 int max_bw_vote;
248 bool is_max_bw_needed;
249 struct delayed_work vote_work;
250 struct device_attribute max_bus_bw;
Asutosh Das0ef24812012-12-18 16:14:02 +0530251};
252
253struct sdhci_msm_host {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530254 struct platform_device *pdev;
Asutosh Das0ef24812012-12-18 16:14:02 +0530255 void __iomem *core_mem; /* MSM SDCC mapped address */
256 struct clk *clk; /* main SD/MMC bus clock */
257 struct clk *pclk; /* SDHC peripheral bus clock */
258 struct clk *bus_clk; /* SDHC bus voter clock */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700259 struct clk *ff_clk; /* CDC calibration fixed feedback clock */
260 struct clk *sleep_clk; /* CDC calibration sleep clock */
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +0530261 atomic_t clks_on; /* Set if clocks are enabled */
Asutosh Das0ef24812012-12-18 16:14:02 +0530262 struct sdhci_msm_pltfm_data *pdata;
263 struct mmc_host *mmc;
264 struct sdhci_pltfm_data sdhci_msm_pdata;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +0530265 u32 curr_pwr_state;
266 u32 curr_io_level;
267 struct completion pwr_irq_completion;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530268 struct sdhci_msm_bus_vote msm_bus_vote;
Sahitya Tummala5c55b932013-06-20 14:00:18 +0530269 struct device_attribute polling;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530270 u32 clk_rate; /* Keeps track of current clock rate that is set */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700271 bool tuning_done;
272 bool calibration_done;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700273 u8 saved_tuning_phase;
Asutosh Dase5e9ca62013-07-30 19:08:36 +0530274 bool en_auto_cmd21;
275 struct device_attribute auto_cmd21_attr;
Asutosh Das0ef24812012-12-18 16:14:02 +0530276};
277
278enum vdd_io_level {
279 /* set vdd_io_data->low_vol_level */
280 VDD_IO_LOW,
281 /* set vdd_io_data->high_vol_level */
282 VDD_IO_HIGH,
283 /*
284 * set whatever there in voltage_level (third argument) of
285 * sdhci_msm_set_vdd_io_vol() function.
286 */
287 VDD_IO_SET_LEVEL,
288};
289
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700290/* MSM platform specific tuning */
291static inline int msm_dll_poll_ck_out_en(struct sdhci_host *host,
292 u8 poll)
293{
294 int rc = 0;
295 u32 wait_cnt = 50;
296 u8 ck_out_en = 0;
297 struct mmc_host *mmc = host->mmc;
298
299 /* poll for CK_OUT_EN bit. max. poll time = 50us */
300 ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) &
301 CORE_CK_OUT_EN);
302
303 while (ck_out_en != poll) {
304 if (--wait_cnt == 0) {
305 pr_err("%s: %s: CK_OUT_EN bit is not %d\n",
306 mmc_hostname(mmc), __func__, poll);
307 rc = -ETIMEDOUT;
308 goto out;
309 }
310 udelay(1);
311
312 ck_out_en = !!(readl_relaxed(host->ioaddr +
313 CORE_DLL_CONFIG) & CORE_CK_OUT_EN);
314 }
315out:
316 return rc;
317}
318
Asutosh Dase5e9ca62013-07-30 19:08:36 +0530319/*
320 * Enable CDR to track changes of DAT lines and adjust sampling
321 * point according to voltage/temperature variations
322 */
323static int msm_enable_cdr_cm_sdc4_dll(struct sdhci_host *host)
324{
325 int rc = 0;
326 u32 config;
327
328 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
329 config |= CORE_CDR_EN;
330 config &= ~(CORE_CDR_EXT_EN | CORE_CK_OUT_EN);
331 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
332
333 rc = msm_dll_poll_ck_out_en(host, 0);
334 if (rc)
335 goto err;
336
337 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) |
338 CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
339
340 rc = msm_dll_poll_ck_out_en(host, 1);
341 if (rc)
342 goto err;
343 goto out;
344err:
345 pr_err("%s: %s: failed\n", mmc_hostname(host->mmc), __func__);
346out:
347 return rc;
348}
349
350static ssize_t store_auto_cmd21(struct device *dev, struct device_attribute
351 *attr, const char *buf, size_t count)
352{
353 struct sdhci_host *host = dev_get_drvdata(dev);
354 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
355 struct sdhci_msm_host *msm_host = pltfm_host->priv;
356 u32 tmp;
357 unsigned long flags;
358
359 if (!kstrtou32(buf, 0, &tmp)) {
360 spin_lock_irqsave(&host->lock, flags);
361 msm_host->en_auto_cmd21 = !!tmp;
362 spin_unlock_irqrestore(&host->lock, flags);
363 }
364 return count;
365}
366
367static ssize_t show_auto_cmd21(struct device *dev,
368 struct device_attribute *attr, char *buf)
369{
370 struct sdhci_host *host = dev_get_drvdata(dev);
371 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
372 struct sdhci_msm_host *msm_host = pltfm_host->priv;
373
374 return snprintf(buf, PAGE_SIZE, "%d\n", msm_host->en_auto_cmd21);
375}
376
377/* MSM auto-tuning handler */
378static int sdhci_msm_config_auto_tuning_cmd(struct sdhci_host *host,
379 bool enable,
380 u32 type)
381{
382 int rc = 0;
383 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
384 struct sdhci_msm_host *msm_host = pltfm_host->priv;
385 u32 val = 0;
386
387 if (!msm_host->en_auto_cmd21)
388 return 0;
389
390 if (type == MMC_SEND_TUNING_BLOCK_HS200)
391 val = CORE_HC_AUTO_CMD21_EN;
392 else
393 return 0;
394
395 if (enable) {
396 rc = msm_enable_cdr_cm_sdc4_dll(host);
397 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
398 val, host->ioaddr + CORE_VENDOR_SPEC);
399 } else {
400 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
401 ~val, host->ioaddr + CORE_VENDOR_SPEC);
402 }
403 return rc;
404}
405
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700406static int msm_config_cm_dll_phase(struct sdhci_host *host, u8 phase)
407{
408 int rc = 0;
409 u8 grey_coded_phase_table[] = {0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4,
410 0xC, 0xD, 0xF, 0xE, 0xA, 0xB, 0x9,
411 0x8};
412 unsigned long flags;
413 u32 config;
414 struct mmc_host *mmc = host->mmc;
415
416 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
417 spin_lock_irqsave(&host->lock, flags);
418
419 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
420 config &= ~(CORE_CDR_EN | CORE_CK_OUT_EN);
421 config |= (CORE_CDR_EXT_EN | CORE_DLL_EN);
422 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
423
424 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '0' */
425 rc = msm_dll_poll_ck_out_en(host, 0);
426 if (rc)
427 goto err_out;
428
429 /*
430 * Write the selected DLL clock output phase (0 ... 15)
431 * to CDR_SELEXT bit field of DLL_CONFIG register.
432 */
433 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
434 & ~(0xF << 20))
435 | (grey_coded_phase_table[phase] << 20)),
436 host->ioaddr + CORE_DLL_CONFIG);
437
438 /* Set CK_OUT_EN bit of DLL_CONFIG register to 1. */
439 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
440 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
441
442 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '1' */
443 rc = msm_dll_poll_ck_out_en(host, 1);
444 if (rc)
445 goto err_out;
446
447 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
448 config |= CORE_CDR_EN;
449 config &= ~CORE_CDR_EXT_EN;
450 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
451 goto out;
452
453err_out:
454 pr_err("%s: %s: Failed to set DLL phase: %d\n",
455 mmc_hostname(mmc), __func__, phase);
456out:
457 spin_unlock_irqrestore(&host->lock, flags);
458 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
459 return rc;
460}
461
462/*
463 * Find out the greatest range of consecuitive selected
464 * DLL clock output phases that can be used as sampling
465 * setting for SD3.0 UHS-I card read operation (in SDR104
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700466 * timing mode) or for eMMC4.5 card read operation (in
467 * HS400/HS200 timing mode).
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700468 * Select the 3/4 of the range and configure the DLL with the
469 * selected DLL clock output phase.
470 */
471
472static int msm_find_most_appropriate_phase(struct sdhci_host *host,
473 u8 *phase_table, u8 total_phases)
474{
475 int ret;
476 u8 ranges[MAX_PHASES][MAX_PHASES] = { {0}, {0} };
477 u8 phases_per_row[MAX_PHASES] = {0};
478 int row_index = 0, col_index = 0, selected_row_index = 0, curr_max = 0;
479 int i, cnt, phase_0_raw_index = 0, phase_15_raw_index = 0;
480 bool phase_0_found = false, phase_15_found = false;
481 struct mmc_host *mmc = host->mmc;
482
483 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
484 if (!total_phases || (total_phases > MAX_PHASES)) {
485 pr_err("%s: %s: invalid argument: total_phases=%d\n",
486 mmc_hostname(mmc), __func__, total_phases);
487 return -EINVAL;
488 }
489
490 for (cnt = 0; cnt < total_phases; cnt++) {
491 ranges[row_index][col_index] = phase_table[cnt];
492 phases_per_row[row_index] += 1;
493 col_index++;
494
495 if ((cnt + 1) == total_phases) {
496 continue;
497 /* check if next phase in phase_table is consecutive or not */
498 } else if ((phase_table[cnt] + 1) != phase_table[cnt + 1]) {
499 row_index++;
500 col_index = 0;
501 }
502 }
503
504 if (row_index >= MAX_PHASES)
505 return -EINVAL;
506
507 /* Check if phase-0 is present in first valid window? */
508 if (!ranges[0][0]) {
509 phase_0_found = true;
510 phase_0_raw_index = 0;
511 /* Check if cycle exist between 2 valid windows */
512 for (cnt = 1; cnt <= row_index; cnt++) {
513 if (phases_per_row[cnt]) {
514 for (i = 0; i < phases_per_row[cnt]; i++) {
515 if (ranges[cnt][i] == 15) {
516 phase_15_found = true;
517 phase_15_raw_index = cnt;
518 break;
519 }
520 }
521 }
522 }
523 }
524
525 /* If 2 valid windows form cycle then merge them as single window */
526 if (phase_0_found && phase_15_found) {
527 /* number of phases in raw where phase 0 is present */
528 u8 phases_0 = phases_per_row[phase_0_raw_index];
529 /* number of phases in raw where phase 15 is present */
530 u8 phases_15 = phases_per_row[phase_15_raw_index];
531
532 if (phases_0 + phases_15 >= MAX_PHASES)
533 /*
534 * If there are more than 1 phase windows then total
535 * number of phases in both the windows should not be
536 * more than or equal to MAX_PHASES.
537 */
538 return -EINVAL;
539
540 /* Merge 2 cyclic windows */
541 i = phases_15;
542 for (cnt = 0; cnt < phases_0; cnt++) {
543 ranges[phase_15_raw_index][i] =
544 ranges[phase_0_raw_index][cnt];
545 if (++i >= MAX_PHASES)
546 break;
547 }
548
549 phases_per_row[phase_0_raw_index] = 0;
550 phases_per_row[phase_15_raw_index] = phases_15 + phases_0;
551 }
552
553 for (cnt = 0; cnt <= row_index; cnt++) {
554 if (phases_per_row[cnt] > curr_max) {
555 curr_max = phases_per_row[cnt];
556 selected_row_index = cnt;
557 }
558 }
559
560 i = ((curr_max * 3) / 4);
561 if (i)
562 i--;
563
564 ret = (int)ranges[selected_row_index][i];
565
566 if (ret >= MAX_PHASES) {
567 ret = -EINVAL;
568 pr_err("%s: %s: invalid phase selected=%d\n",
569 mmc_hostname(mmc), __func__, ret);
570 }
571
572 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
573 return ret;
574}
575
576static inline void msm_cm_dll_set_freq(struct sdhci_host *host)
577{
578 u32 mclk_freq = 0;
579
580 /* Program the MCLK value to MCLK_FREQ bit field */
581 if (host->clock <= 112000000)
582 mclk_freq = 0;
583 else if (host->clock <= 125000000)
584 mclk_freq = 1;
585 else if (host->clock <= 137000000)
586 mclk_freq = 2;
587 else if (host->clock <= 150000000)
588 mclk_freq = 3;
589 else if (host->clock <= 162000000)
590 mclk_freq = 4;
591 else if (host->clock <= 175000000)
592 mclk_freq = 5;
593 else if (host->clock <= 187000000)
594 mclk_freq = 6;
595 else if (host->clock <= 200000000)
596 mclk_freq = 7;
597
598 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
599 & ~(7 << 24)) | (mclk_freq << 24)),
600 host->ioaddr + CORE_DLL_CONFIG);
601}
602
603/* Initialize the DLL (Programmable Delay Line ) */
604static int msm_init_cm_dll(struct sdhci_host *host)
605{
606 struct mmc_host *mmc = host->mmc;
607 int rc = 0;
608 unsigned long flags;
609 u32 wait_cnt;
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530610 bool prev_pwrsave, curr_pwrsave;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700611
612 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
613 spin_lock_irqsave(&host->lock, flags);
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530614 prev_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
615 CORE_CLK_PWRSAVE);
616 curr_pwrsave = prev_pwrsave;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700617 /*
618 * Make sure that clock is always enabled when DLL
619 * tuning is in progress. Keeping PWRSAVE ON may
620 * turn off the clock. So let's disable the PWRSAVE
621 * here and re-enable it once tuning is completed.
622 */
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530623 if (prev_pwrsave) {
624 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
625 & ~CORE_CLK_PWRSAVE),
626 host->ioaddr + CORE_VENDOR_SPEC);
627 curr_pwrsave = false;
628 }
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700629
630 /* Write 1 to DLL_RST bit of DLL_CONFIG register */
631 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
632 | CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
633
634 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
635 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
636 | CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
637 msm_cm_dll_set_freq(host);
638
639 /* Write 0 to DLL_RST bit of DLL_CONFIG register */
640 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
641 & ~CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
642
643 /* Write 0 to DLL_PDN bit of DLL_CONFIG register */
644 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
645 & ~CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
646
647 /* Set DLL_EN bit to 1. */
648 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
649 | CORE_DLL_EN), host->ioaddr + CORE_DLL_CONFIG);
650
651 /* Set CK_OUT_EN bit to 1. */
652 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
653 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
654
655 wait_cnt = 50;
656 /* Wait until DLL_LOCK bit of DLL_STATUS register becomes '1' */
657 while (!(readl_relaxed(host->ioaddr + CORE_DLL_STATUS) &
658 CORE_DLL_LOCK)) {
659 /* max. wait for 50us sec for LOCK bit to be set */
660 if (--wait_cnt == 0) {
661 pr_err("%s: %s: DLL failed to LOCK\n",
662 mmc_hostname(mmc), __func__);
663 rc = -ETIMEDOUT;
664 goto out;
665 }
666 /* wait for 1us before polling again */
667 udelay(1);
668 }
669
670out:
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530671 /* Restore the correct PWRSAVE state */
672 if (prev_pwrsave ^ curr_pwrsave) {
673 u32 reg = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC);
674
675 if (prev_pwrsave)
676 reg |= CORE_CLK_PWRSAVE;
677 else
678 reg &= ~CORE_CLK_PWRSAVE;
679
680 writel_relaxed(reg, host->ioaddr + CORE_VENDOR_SPEC);
681 }
682
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700683 spin_unlock_irqrestore(&host->lock, flags);
684 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
685 return rc;
686}
687
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700688static int sdhci_msm_cdclp533_calibration(struct sdhci_host *host)
689{
690 u32 wait_cnt;
691 int ret = 0;
692 int cdc_err = 0;
693 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
694 struct sdhci_msm_host *msm_host = pltfm_host->priv;
695
696 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
697
698 /*
699 * Retuning in HS400 (DDR mode) will fail, just reset the
700 * tuning block and restore the saved tuning phase.
701 */
702 ret = msm_init_cm_dll(host);
703 if (ret)
704 goto out;
705
706 /* Set the selected phase in delay line hw block */
707 ret = msm_config_cm_dll_phase(host, msm_host->saved_tuning_phase);
708 if (ret)
709 goto out;
710
711 /* Write 1 to CMD_DAT_TRACK_SEL field in DLL_CONFIG */
712 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
713 | CORE_CMD_DAT_TRACK_SEL),
714 host->ioaddr + CORE_DLL_CONFIG);
715
716 /* Write 0 to CDC_T4_DLY_SEL field in VENDOR_SPEC_DDR200_CFG */
717 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
718 & ~CORE_CDC_T4_DLY_SEL),
719 host->ioaddr + CORE_DDR_200_CFG);
720
721 /* Write 0 to CDC_SWITCH_BYPASS_OFF field in CORE_CSR_CDC_GEN_CFG */
722 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG)
723 & ~CORE_CDC_SWITCH_BYPASS_OFF),
724 host->ioaddr + CORE_CSR_CDC_GEN_CFG);
725
726 /* Write 1 to CDC_SWITCH_RC_EN field in CORE_CSR_CDC_GEN_CFG */
727 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG)
728 | CORE_CDC_SWITCH_RC_EN),
729 host->ioaddr + CORE_CSR_CDC_GEN_CFG);
730
731 /* Write 0 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
732 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
733 & ~CORE_START_CDC_TRAFFIC),
734 host->ioaddr + CORE_DDR_200_CFG);
735
736 /*
737 * Perform CDC Register Initialization Sequence
738 *
739 * CORE_CSR_CDC_CTLR_CFG0 0x11800EC
740 * CORE_CSR_CDC_CTLR_CFG1 0x3011111
741 * CORE_CSR_CDC_CAL_TIMER_CFG0 0x1201000
742 * CORE_CSR_CDC_CAL_TIMER_CFG1 0x4
743 * CORE_CSR_CDC_REFCOUNT_CFG 0xCB732020
744 * CORE_CSR_CDC_COARSE_CAL_CFG 0xB19
745 * CORE_CSR_CDC_DELAY_CFG 0x3AC
746 * CORE_CDC_OFFSET_CFG 0x0
747 * CORE_CDC_SLAVE_DDA_CFG 0x16334
748 */
749
750 writel_relaxed(0x11800EC, host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
751 writel_relaxed(0x3011111, host->ioaddr + CORE_CSR_CDC_CTLR_CFG1);
752 writel_relaxed(0x1201000, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
753 writel_relaxed(0x4, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG1);
754 writel_relaxed(0xCB732020, host->ioaddr + CORE_CSR_CDC_REFCOUNT_CFG);
755 writel_relaxed(0xB19, host->ioaddr + CORE_CSR_CDC_COARSE_CAL_CFG);
756 writel_relaxed(0x3AC, host->ioaddr + CORE_CSR_CDC_DELAY_CFG);
757 writel_relaxed(0x0, host->ioaddr + CORE_CDC_OFFSET_CFG);
758 writel_relaxed(0x16334, host->ioaddr + CORE_CDC_SLAVE_DDA_CFG);
759
760 /* CDC HW Calibration */
761
762 /* Write 1 to SW_TRIG_FULL_CALIB field in CORE_CSR_CDC_CTLR_CFG0 */
763 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
764 | CORE_SW_TRIG_FULL_CALIB),
765 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
766
767 /* Write 0 to SW_TRIG_FULL_CALIB field in CORE_CSR_CDC_CTLR_CFG0 */
768 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
769 & ~CORE_SW_TRIG_FULL_CALIB),
770 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
771
772 /* Write 1 to HW_AUTOCAL_ENA field in CORE_CSR_CDC_CTLR_CFG0 */
773 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
774 | CORE_HW_AUTOCAL_ENA),
775 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
776
777 /* Write 1 to TIMER_ENA field in CORE_CSR_CDC_CAL_TIMER_CFG0 */
778 writel_relaxed((readl_relaxed(host->ioaddr +
779 CORE_CSR_CDC_CAL_TIMER_CFG0) | CORE_TIMER_ENA),
780 host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
781
782 mb();
783
784 /* Poll on CALIBRATION_DONE field in CORE_CSR_CDC_STATUS0 to be 1 */
785 wait_cnt = 50;
786 while (!(readl_relaxed(host->ioaddr + CORE_CSR_CDC_STATUS0)
787 & CORE_CALIBRATION_DONE)) {
788 /* max. wait for 50us sec for CALIBRATION_DONE bit to be set */
789 if (--wait_cnt == 0) {
790 pr_err("%s: %s: CDC Calibration was not completed\n",
791 mmc_hostname(host->mmc), __func__);
792 ret = -ETIMEDOUT;
793 goto out;
794 }
795 /* wait for 1us before polling again */
796 udelay(1);
797 }
798
799 /* Verify CDC_ERROR_CODE field in CORE_CSR_CDC_STATUS0 is 0 */
800 cdc_err = readl_relaxed(host->ioaddr + CORE_CSR_CDC_STATUS0)
801 & CORE_CDC_ERROR_CODE_MASK;
802 if (cdc_err) {
803 pr_err("%s: %s: CDC Error Code %d\n",
804 mmc_hostname(host->mmc), __func__, cdc_err);
805 ret = -EINVAL;
806 goto out;
807 }
808
809 /* Write 1 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
810 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
811 | CORE_START_CDC_TRAFFIC),
812 host->ioaddr + CORE_DDR_200_CFG);
813out:
814 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
815 __func__, ret);
816 return ret;
817}
818
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700819int sdhci_msm_execute_tuning(struct sdhci_host *host, u32 opcode)
820{
821 unsigned long flags;
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530822 int tuning_seq_cnt = 3;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700823 u8 phase, *data_buf, tuned_phases[16], tuned_phase_cnt = 0;
824 const u32 *tuning_block_pattern = tuning_block_64;
825 int size = sizeof(tuning_block_64); /* Tuning pattern size in bytes */
826 int rc;
827 struct mmc_host *mmc = host->mmc;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530828 struct mmc_ios ios = host->mmc->ios;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700829 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
830 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530831
832 /*
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700833 * Tuning is required for SDR104, HS200 and HS400 cards and
834 * if clock frequency is greater than 100MHz in these modes.
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530835 */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700836 if (host->clock <= CORE_FREQ_100MHZ ||
837 !((ios.timing == MMC_TIMING_MMC_HS400) ||
838 (ios.timing == MMC_TIMING_MMC_HS200) ||
839 (ios.timing == MMC_TIMING_UHS_SDR104)))
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530840 return 0;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700841
842 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700843
844 /* CDCLP533 HW calibration is only required for HS400 mode*/
845 if (msm_host->tuning_done && !msm_host->calibration_done &&
846 (mmc->ios.timing == MMC_TIMING_MMC_HS400)) {
847 rc = sdhci_msm_cdclp533_calibration(host);
848 spin_lock_irqsave(&host->lock, flags);
849 if (!rc)
850 msm_host->calibration_done = true;
851 spin_unlock_irqrestore(&host->lock, flags);
852 goto out;
853 }
854
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700855 spin_lock_irqsave(&host->lock, flags);
856
857 if ((opcode == MMC_SEND_TUNING_BLOCK_HS200) &&
858 (mmc->ios.bus_width == MMC_BUS_WIDTH_8)) {
859 tuning_block_pattern = tuning_block_128;
860 size = sizeof(tuning_block_128);
861 }
862 spin_unlock_irqrestore(&host->lock, flags);
863
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700864 data_buf = kmalloc(size, GFP_KERNEL);
865 if (!data_buf) {
866 rc = -ENOMEM;
867 goto out;
868 }
869
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530870retry:
871 /* first of all reset the tuning block */
872 rc = msm_init_cm_dll(host);
873 if (rc)
874 goto kfree;
875
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700876 phase = 0;
877 do {
878 struct mmc_command cmd = {0};
879 struct mmc_data data = {0};
880 struct mmc_request mrq = {
881 .cmd = &cmd,
882 .data = &data
883 };
884 struct scatterlist sg;
885
886 /* set the phase in delay line hw block */
887 rc = msm_config_cm_dll_phase(host, phase);
888 if (rc)
889 goto kfree;
890
891 cmd.opcode = opcode;
892 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
893
894 data.blksz = size;
895 data.blocks = 1;
896 data.flags = MMC_DATA_READ;
897 data.timeout_ns = 1000 * 1000 * 1000; /* 1 sec */
898
899 data.sg = &sg;
900 data.sg_len = 1;
901 sg_init_one(&sg, data_buf, size);
902 memset(data_buf, 0, size);
903 mmc_wait_for_req(mmc, &mrq);
904
905 if (!cmd.error && !data.error &&
906 !memcmp(data_buf, tuning_block_pattern, size)) {
907 /* tuning is successful at this tuning point */
908 tuned_phases[tuned_phase_cnt++] = phase;
909 pr_debug("%s: %s: found good phase = %d\n",
910 mmc_hostname(mmc), __func__, phase);
911 }
912 } while (++phase < 16);
913
914 if (tuned_phase_cnt) {
915 rc = msm_find_most_appropriate_phase(host, tuned_phases,
916 tuned_phase_cnt);
917 if (rc < 0)
918 goto kfree;
919 else
920 phase = (u8)rc;
921
922 /*
923 * Finally set the selected phase in delay
924 * line hw block.
925 */
926 rc = msm_config_cm_dll_phase(host, phase);
927 if (rc)
928 goto kfree;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700929 msm_host->saved_tuning_phase = phase;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700930 pr_debug("%s: %s: finally setting the tuning phase to %d\n",
931 mmc_hostname(mmc), __func__, phase);
932 } else {
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530933 if (--tuning_seq_cnt)
934 goto retry;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700935 /* tuning failed */
936 pr_err("%s: %s: no tuning point found\n",
937 mmc_hostname(mmc), __func__);
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530938 rc = -EIO;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700939 }
940
941kfree:
942 kfree(data_buf);
943out:
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700944 spin_lock_irqsave(&host->lock, flags);
945 if (!rc)
946 msm_host->tuning_done = true;
947 spin_unlock_irqrestore(&host->lock, flags);
948 pr_debug("%s: Exit %s, err(%d)\n", mmc_hostname(mmc), __func__, rc);
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700949 return rc;
950}
951
Asutosh Das0ef24812012-12-18 16:14:02 +0530952static int sdhci_msm_setup_gpio(struct sdhci_msm_pltfm_data *pdata, bool enable)
953{
954 struct sdhci_msm_gpio_data *curr;
955 int i, ret = 0;
956
957 curr = pdata->pin_data->gpio_data;
958 for (i = 0; i < curr->size; i++) {
959 if (!gpio_is_valid(curr->gpio[i].no)) {
960 ret = -EINVAL;
961 pr_err("%s: Invalid gpio = %d\n", __func__,
962 curr->gpio[i].no);
963 goto free_gpios;
964 }
965 if (enable) {
966 ret = gpio_request(curr->gpio[i].no,
967 curr->gpio[i].name);
968 if (ret) {
969 pr_err("%s: gpio_request(%d, %s) failed %d\n",
970 __func__, curr->gpio[i].no,
971 curr->gpio[i].name, ret);
972 goto free_gpios;
973 }
974 curr->gpio[i].is_enabled = true;
975 } else {
976 gpio_free(curr->gpio[i].no);
977 curr->gpio[i].is_enabled = false;
978 }
979 }
980 return ret;
981
982free_gpios:
983 for (i--; i >= 0; i--) {
984 gpio_free(curr->gpio[i].no);
985 curr->gpio[i].is_enabled = false;
986 }
987 return ret;
988}
989
990static int sdhci_msm_setup_pins(struct sdhci_msm_pltfm_data *pdata, bool enable)
991{
992 int ret = 0;
993
994 if (!pdata->pin_data || (pdata->pin_data->cfg_sts == enable))
995 return 0;
996
997 ret = sdhci_msm_setup_gpio(pdata, enable);
998 if (!ret)
999 pdata->pin_data->cfg_sts = enable;
1000
1001 return ret;
1002}
1003
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301004static int sdhci_msm_dt_get_array(struct device *dev, const char *prop_name,
1005 u32 **out, int *len, u32 size)
1006{
1007 int ret = 0;
1008 struct device_node *np = dev->of_node;
1009 size_t sz;
1010 u32 *arr = NULL;
1011
1012 if (!of_get_property(np, prop_name, len)) {
1013 ret = -EINVAL;
1014 goto out;
1015 }
1016 sz = *len = *len / sizeof(*arr);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07001017 if (sz <= 0 || (size > 0 && (sz > size))) {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301018 dev_err(dev, "%s invalid size\n", prop_name);
1019 ret = -EINVAL;
1020 goto out;
1021 }
1022
1023 arr = devm_kzalloc(dev, sz * sizeof(*arr), GFP_KERNEL);
1024 if (!arr) {
1025 dev_err(dev, "%s failed allocating memory\n", prop_name);
1026 ret = -ENOMEM;
1027 goto out;
1028 }
1029
1030 ret = of_property_read_u32_array(np, prop_name, arr, sz);
1031 if (ret < 0) {
1032 dev_err(dev, "%s failed reading array %d\n", prop_name, ret);
1033 goto out;
1034 }
1035 *out = arr;
1036out:
1037 if (ret)
1038 *len = 0;
1039 return ret;
1040}
1041
Asutosh Das0ef24812012-12-18 16:14:02 +05301042#define MAX_PROP_SIZE 32
1043static int sdhci_msm_dt_parse_vreg_info(struct device *dev,
1044 struct sdhci_msm_reg_data **vreg_data, const char *vreg_name)
1045{
1046 int len, ret = 0;
1047 const __be32 *prop;
1048 char prop_name[MAX_PROP_SIZE];
1049 struct sdhci_msm_reg_data *vreg;
1050 struct device_node *np = dev->of_node;
1051
1052 snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", vreg_name);
1053 if (!of_parse_phandle(np, prop_name, 0)) {
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301054 dev_info(dev, "No vreg data found for %s\n", vreg_name);
Asutosh Das0ef24812012-12-18 16:14:02 +05301055 return ret;
1056 }
1057
1058 vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
1059 if (!vreg) {
1060 dev_err(dev, "No memory for vreg: %s\n", vreg_name);
1061 ret = -ENOMEM;
1062 return ret;
1063 }
1064
1065 vreg->name = vreg_name;
1066
1067 snprintf(prop_name, MAX_PROP_SIZE,
1068 "qcom,%s-always-on", vreg_name);
1069 if (of_get_property(np, prop_name, NULL))
1070 vreg->is_always_on = true;
1071
1072 snprintf(prop_name, MAX_PROP_SIZE,
1073 "qcom,%s-lpm-sup", vreg_name);
1074 if (of_get_property(np, prop_name, NULL))
1075 vreg->lpm_sup = true;
1076
1077 snprintf(prop_name, MAX_PROP_SIZE,
1078 "qcom,%s-voltage-level", vreg_name);
1079 prop = of_get_property(np, prop_name, &len);
1080 if (!prop || (len != (2 * sizeof(__be32)))) {
1081 dev_warn(dev, "%s %s property\n",
1082 prop ? "invalid format" : "no", prop_name);
1083 } else {
1084 vreg->low_vol_level = be32_to_cpup(&prop[0]);
1085 vreg->high_vol_level = be32_to_cpup(&prop[1]);
1086 }
1087
1088 snprintf(prop_name, MAX_PROP_SIZE,
1089 "qcom,%s-current-level", vreg_name);
1090 prop = of_get_property(np, prop_name, &len);
1091 if (!prop || (len != (2 * sizeof(__be32)))) {
1092 dev_warn(dev, "%s %s property\n",
1093 prop ? "invalid format" : "no", prop_name);
1094 } else {
1095 vreg->lpm_uA = be32_to_cpup(&prop[0]);
1096 vreg->hpm_uA = be32_to_cpup(&prop[1]);
1097 }
1098
1099 *vreg_data = vreg;
1100 dev_dbg(dev, "%s: %s %s vol=[%d %d]uV, curr=[%d %d]uA\n",
1101 vreg->name, vreg->is_always_on ? "always_on," : "",
1102 vreg->lpm_sup ? "lpm_sup," : "", vreg->low_vol_level,
1103 vreg->high_vol_level, vreg->lpm_uA, vreg->hpm_uA);
1104
1105 return ret;
1106}
1107
1108#define GPIO_NAME_MAX_LEN 32
1109static int sdhci_msm_dt_parse_gpio_info(struct device *dev,
1110 struct sdhci_msm_pltfm_data *pdata)
1111{
1112 int ret = 0, cnt, i;
1113 struct sdhci_msm_pin_data *pin_data;
1114 struct device_node *np = dev->of_node;
1115
1116 pin_data = devm_kzalloc(dev, sizeof(*pin_data), GFP_KERNEL);
1117 if (!pin_data) {
1118 dev_err(dev, "No memory for pin_data\n");
1119 ret = -ENOMEM;
1120 goto out;
1121 }
1122
1123 cnt = of_gpio_count(np);
1124 if (cnt > 0) {
1125 pin_data->gpio_data = devm_kzalloc(dev,
1126 sizeof(struct sdhci_msm_gpio_data), GFP_KERNEL);
1127 if (!pin_data->gpio_data) {
1128 dev_err(dev, "No memory for gpio_data\n");
1129 ret = -ENOMEM;
1130 goto out;
1131 }
1132 pin_data->gpio_data->size = cnt;
1133 pin_data->gpio_data->gpio = devm_kzalloc(dev, cnt *
1134 sizeof(struct sdhci_msm_gpio), GFP_KERNEL);
1135
1136 if (!pin_data->gpio_data->gpio) {
1137 dev_err(dev, "No memory for gpio\n");
1138 ret = -ENOMEM;
1139 goto out;
1140 }
1141
1142 for (i = 0; i < cnt; i++) {
1143 const char *name = NULL;
1144 char result[GPIO_NAME_MAX_LEN];
1145 pin_data->gpio_data->gpio[i].no = of_get_gpio(np, i);
1146 of_property_read_string_index(np,
1147 "qcom,gpio-names", i, &name);
1148
1149 snprintf(result, GPIO_NAME_MAX_LEN, "%s-%s",
1150 dev_name(dev), name ? name : "?");
1151 pin_data->gpio_data->gpio[i].name = result;
1152 dev_dbg(dev, "%s: gpio[%s] = %d\n", __func__,
1153 pin_data->gpio_data->gpio[i].name,
1154 pin_data->gpio_data->gpio[i].no);
1155 pdata->pin_data = pin_data;
1156 }
1157 }
1158
1159out:
1160 if (ret)
1161 dev_err(dev, "%s failed with err %d\n", __func__, ret);
1162 return ret;
1163}
1164
1165/* Parse platform data */
1166static struct sdhci_msm_pltfm_data *sdhci_msm_populate_pdata(struct device *dev)
1167{
1168 struct sdhci_msm_pltfm_data *pdata = NULL;
1169 struct device_node *np = dev->of_node;
1170 u32 bus_width = 0;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05301171 u32 cpu_dma_latency;
Asutosh Das0ef24812012-12-18 16:14:02 +05301172 int len, i;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301173 int clk_table_len;
1174 u32 *clk_table = NULL;
Sujit Reddy Thumma1958d3d2013-06-03 09:54:32 +05301175 enum of_gpio_flags flags = OF_GPIO_ACTIVE_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05301176
1177 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1178 if (!pdata) {
1179 dev_err(dev, "failed to allocate memory for platform data\n");
1180 goto out;
1181 }
1182
Sujit Reddy Thumma1958d3d2013-06-03 09:54:32 +05301183 pdata->status_gpio = of_get_named_gpio_flags(np, "cd-gpios", 0, &flags);
1184 if (gpio_is_valid(pdata->status_gpio) & !(flags & OF_GPIO_ACTIVE_LOW))
1185 pdata->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
Sahitya Tummala581df132013-03-12 14:57:46 +05301186
Asutosh Das0ef24812012-12-18 16:14:02 +05301187 of_property_read_u32(np, "qcom,bus-width", &bus_width);
1188 if (bus_width == 8)
1189 pdata->mmc_bus_width = MMC_CAP_8_BIT_DATA;
1190 else if (bus_width == 4)
1191 pdata->mmc_bus_width = MMC_CAP_4_BIT_DATA;
1192 else {
1193 dev_notice(dev, "invalid bus-width, default to 1-bit mode\n");
1194 pdata->mmc_bus_width = 0;
1195 }
1196
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05301197 if (!of_property_read_u32(np, "qcom,cpu-dma-latency-us",
1198 &cpu_dma_latency))
1199 pdata->cpu_dma_latency_us = cpu_dma_latency;
1200
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301201 if (sdhci_msm_dt_get_array(dev, "qcom,clk-rates",
1202 &clk_table, &clk_table_len, 0)) {
1203 dev_err(dev, "failed parsing supported clock rates\n");
1204 goto out;
1205 }
1206 if (!clk_table || !clk_table_len) {
1207 dev_err(dev, "Invalid clock table\n");
1208 goto out;
1209 }
1210 pdata->sup_clk_table = clk_table;
1211 pdata->sup_clk_cnt = clk_table_len;
1212
Asutosh Das0ef24812012-12-18 16:14:02 +05301213 pdata->vreg_data = devm_kzalloc(dev, sizeof(struct
1214 sdhci_msm_slot_reg_data),
1215 GFP_KERNEL);
1216 if (!pdata->vreg_data) {
1217 dev_err(dev, "failed to allocate memory for vreg data\n");
1218 goto out;
1219 }
1220
1221 if (sdhci_msm_dt_parse_vreg_info(dev, &pdata->vreg_data->vdd_data,
1222 "vdd")) {
1223 dev_err(dev, "failed parsing vdd data\n");
1224 goto out;
1225 }
1226 if (sdhci_msm_dt_parse_vreg_info(dev,
1227 &pdata->vreg_data->vdd_io_data,
1228 "vdd-io")) {
1229 dev_err(dev, "failed parsing vdd-io data\n");
1230 goto out;
1231 }
1232
1233 if (sdhci_msm_dt_parse_gpio_info(dev, pdata)) {
1234 dev_err(dev, "failed parsing gpio data\n");
1235 goto out;
1236 }
1237
Asutosh Das0ef24812012-12-18 16:14:02 +05301238 len = of_property_count_strings(np, "qcom,bus-speed-mode");
1239
1240 for (i = 0; i < len; i++) {
1241 const char *name = NULL;
1242
1243 of_property_read_string_index(np,
1244 "qcom,bus-speed-mode", i, &name);
1245 if (!name)
1246 continue;
1247
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07001248 if (!strncmp(name, "HS400_1p8v", sizeof("HS400_1p8v")))
1249 pdata->caps2 |= MMC_CAP2_HS400_1_8V;
1250 else if (!strncmp(name, "HS400_1p2v", sizeof("HS400_1p2v")))
1251 pdata->caps2 |= MMC_CAP2_HS400_1_2V;
1252 else if (!strncmp(name, "HS200_1p8v", sizeof("HS200_1p8v")))
Asutosh Das0ef24812012-12-18 16:14:02 +05301253 pdata->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
1254 else if (!strncmp(name, "HS200_1p2v", sizeof("HS200_1p2v")))
1255 pdata->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
1256 else if (!strncmp(name, "DDR_1p8v", sizeof("DDR_1p8v")))
1257 pdata->caps |= MMC_CAP_1_8V_DDR
1258 | MMC_CAP_UHS_DDR50;
1259 else if (!strncmp(name, "DDR_1p2v", sizeof("DDR_1p2v")))
1260 pdata->caps |= MMC_CAP_1_2V_DDR
1261 | MMC_CAP_UHS_DDR50;
1262 }
1263
1264 if (of_get_property(np, "qcom,nonremovable", NULL))
1265 pdata->nonremovable = true;
1266
1267 return pdata;
1268out:
1269 return NULL;
1270}
1271
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301272/* Returns required bandwidth in Bytes per Sec */
1273static unsigned int sdhci_get_bw_required(struct sdhci_host *host,
1274 struct mmc_ios *ios)
1275{
Sahitya Tummala2886c922013-04-03 18:03:31 +05301276 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1277 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1278
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301279 unsigned int bw;
1280
Sahitya Tummala2886c922013-04-03 18:03:31 +05301281 bw = msm_host->clk_rate;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301282 /*
1283 * For DDR mode, SDCC controller clock will be at
1284 * the double rate than the actual clock that goes to card.
1285 */
1286 if (ios->bus_width == MMC_BUS_WIDTH_4)
1287 bw /= 2;
1288 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1289 bw /= 8;
1290
1291 return bw;
1292}
1293
1294static int sdhci_msm_bus_get_vote_for_bw(struct sdhci_msm_host *host,
1295 unsigned int bw)
1296{
1297 unsigned int *table = host->pdata->voting_data->bw_vecs;
1298 unsigned int size = host->pdata->voting_data->bw_vecs_size;
1299 int i;
1300
1301 if (host->msm_bus_vote.is_max_bw_needed && bw)
1302 return host->msm_bus_vote.max_bw_vote;
1303
1304 for (i = 0; i < size; i++) {
1305 if (bw <= table[i])
1306 break;
1307 }
1308
1309 if (i && (i == size))
1310 i--;
1311
1312 return i;
1313}
1314
1315/*
1316 * This function must be called with host lock acquired.
1317 * Caller of this function should also ensure that msm bus client
1318 * handle is not null.
1319 */
1320static inline int sdhci_msm_bus_set_vote(struct sdhci_msm_host *msm_host,
1321 int vote,
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301322 unsigned long *flags)
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301323{
1324 struct sdhci_host *host = platform_get_drvdata(msm_host->pdev);
1325 int rc = 0;
1326
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301327 BUG_ON(!flags);
1328
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301329 if (vote != msm_host->msm_bus_vote.curr_vote) {
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301330 spin_unlock_irqrestore(&host->lock, *flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301331 rc = msm_bus_scale_client_update_request(
1332 msm_host->msm_bus_vote.client_handle, vote);
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301333 spin_lock_irqsave(&host->lock, *flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301334 if (rc) {
1335 pr_err("%s: msm_bus_scale_client_update_request() failed: bus_client_handle=0x%x, vote=%d, err=%d\n",
1336 mmc_hostname(host->mmc),
1337 msm_host->msm_bus_vote.client_handle, vote, rc);
1338 goto out;
1339 }
1340 msm_host->msm_bus_vote.curr_vote = vote;
1341 }
1342out:
1343 return rc;
1344}
1345
1346/*
1347 * Internal work. Work to set 0 bandwidth for msm bus.
1348 */
1349static void sdhci_msm_bus_work(struct work_struct *work)
1350{
1351 struct sdhci_msm_host *msm_host;
1352 struct sdhci_host *host;
1353 unsigned long flags;
1354
1355 msm_host = container_of(work, struct sdhci_msm_host,
1356 msm_bus_vote.vote_work.work);
1357 host = platform_get_drvdata(msm_host->pdev);
1358
1359 if (!msm_host->msm_bus_vote.client_handle)
1360 return;
1361
1362 spin_lock_irqsave(&host->lock, flags);
1363 /* don't vote for 0 bandwidth if any request is in progress */
1364 if (!host->mrq) {
1365 sdhci_msm_bus_set_vote(msm_host,
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301366 msm_host->msm_bus_vote.min_bw_vote, &flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301367 } else
1368 pr_warning("%s: %s: Transfer in progress. skipping bus voting to 0 bandwidth\n",
1369 mmc_hostname(host->mmc), __func__);
1370 spin_unlock_irqrestore(&host->lock, flags);
1371}
1372
1373/*
1374 * This function cancels any scheduled delayed work and sets the bus
1375 * vote based on bw (bandwidth) argument.
1376 */
1377static void sdhci_msm_bus_cancel_work_and_set_vote(struct sdhci_host *host,
1378 unsigned int bw)
1379{
1380 int vote;
1381 unsigned long flags;
1382 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1383 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1384
1385 cancel_delayed_work_sync(&msm_host->msm_bus_vote.vote_work);
1386 spin_lock_irqsave(&host->lock, flags);
1387 vote = sdhci_msm_bus_get_vote_for_bw(msm_host, bw);
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301388 sdhci_msm_bus_set_vote(msm_host, vote, &flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301389 spin_unlock_irqrestore(&host->lock, flags);
1390}
1391
1392#define MSM_MMC_BUS_VOTING_DELAY 200 /* msecs */
1393
1394/* This function queues a work which will set the bandwidth requiement to 0 */
1395static void sdhci_msm_bus_queue_work(struct sdhci_host *host)
1396{
1397 unsigned long flags;
1398 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1399 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1400
1401 spin_lock_irqsave(&host->lock, flags);
1402 if (msm_host->msm_bus_vote.min_bw_vote !=
1403 msm_host->msm_bus_vote.curr_vote)
1404 queue_delayed_work(system_wq,
1405 &msm_host->msm_bus_vote.vote_work,
1406 msecs_to_jiffies(MSM_MMC_BUS_VOTING_DELAY));
1407 spin_unlock_irqrestore(&host->lock, flags);
1408}
1409
1410static int sdhci_msm_bus_register(struct sdhci_msm_host *host,
1411 struct platform_device *pdev)
1412{
1413 int rc = 0;
1414 struct msm_bus_scale_pdata *bus_pdata;
1415
1416 struct sdhci_msm_bus_voting_data *data;
1417 struct device *dev = &pdev->dev;
1418
1419 data = devm_kzalloc(dev,
1420 sizeof(struct sdhci_msm_bus_voting_data), GFP_KERNEL);
1421 if (!data) {
1422 dev_err(&pdev->dev,
1423 "%s: failed to allocate memory\n", __func__);
1424 rc = -ENOMEM;
1425 goto out;
1426 }
1427 data->bus_pdata = msm_bus_cl_get_pdata(pdev);
1428 if (data->bus_pdata) {
1429 rc = sdhci_msm_dt_get_array(dev, "qcom,bus-bw-vectors-bps",
1430 &data->bw_vecs, &data->bw_vecs_size, 0);
1431 if (rc) {
1432 dev_err(&pdev->dev,
1433 "%s: Failed to get bus-bw-vectors-bps\n",
1434 __func__);
1435 goto out;
1436 }
1437 host->pdata->voting_data = data;
1438 }
1439 if (host->pdata->voting_data &&
1440 host->pdata->voting_data->bus_pdata &&
1441 host->pdata->voting_data->bw_vecs &&
1442 host->pdata->voting_data->bw_vecs_size) {
1443
1444 bus_pdata = host->pdata->voting_data->bus_pdata;
1445 host->msm_bus_vote.client_handle =
1446 msm_bus_scale_register_client(bus_pdata);
1447 if (!host->msm_bus_vote.client_handle) {
1448 dev_err(&pdev->dev, "msm_bus_scale_register_client()\n");
1449 rc = -EFAULT;
1450 goto out;
1451 }
1452 /* cache the vote index for minimum and maximum bandwidth */
1453 host->msm_bus_vote.min_bw_vote =
1454 sdhci_msm_bus_get_vote_for_bw(host, 0);
1455 host->msm_bus_vote.max_bw_vote =
1456 sdhci_msm_bus_get_vote_for_bw(host, UINT_MAX);
1457 } else {
1458 devm_kfree(dev, data);
1459 }
1460
1461out:
1462 return rc;
1463}
1464
1465static void sdhci_msm_bus_unregister(struct sdhci_msm_host *host)
1466{
1467 if (host->msm_bus_vote.client_handle)
1468 msm_bus_scale_unregister_client(
1469 host->msm_bus_vote.client_handle);
1470}
1471
1472static void sdhci_msm_bus_voting(struct sdhci_host *host, u32 enable)
1473{
1474 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1475 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1476 struct mmc_ios *ios = &host->mmc->ios;
1477 unsigned int bw;
1478
1479 if (!msm_host->msm_bus_vote.client_handle)
1480 return;
1481
1482 bw = sdhci_get_bw_required(host, ios);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301483 if (enable) {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301484 sdhci_msm_bus_cancel_work_and_set_vote(host, bw);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301485 } else {
1486 /*
1487 * If clock gating is enabled, then remove the vote
1488 * immediately because clocks will be disabled only
1489 * after SDHCI_MSM_MMC_CLK_GATE_DELAY and thus no
1490 * additional delay is required to remove the bus vote.
1491 */
1492#ifdef CONFIG_MMC_CLKGATE
1493 if (host->mmc->clkgate_delay)
1494 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
1495 else
1496#endif
1497 sdhci_msm_bus_queue_work(host);
1498 }
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301499}
1500
Asutosh Das0ef24812012-12-18 16:14:02 +05301501/* Regulator utility functions */
1502static int sdhci_msm_vreg_init_reg(struct device *dev,
1503 struct sdhci_msm_reg_data *vreg)
1504{
1505 int ret = 0;
1506
1507 /* check if regulator is already initialized? */
1508 if (vreg->reg)
1509 goto out;
1510
1511 /* Get the regulator handle */
1512 vreg->reg = devm_regulator_get(dev, vreg->name);
1513 if (IS_ERR(vreg->reg)) {
1514 ret = PTR_ERR(vreg->reg);
1515 pr_err("%s: devm_regulator_get(%s) failed. ret=%d\n",
1516 __func__, vreg->name, ret);
1517 goto out;
1518 }
1519
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301520 if (regulator_count_voltages(vreg->reg) > 0) {
1521 vreg->set_voltage_sup = true;
1522 /* sanity check */
1523 if (!vreg->high_vol_level || !vreg->hpm_uA) {
1524 pr_err("%s: %s invalid constraints specified\n",
1525 __func__, vreg->name);
1526 ret = -EINVAL;
1527 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301528 }
1529
1530out:
1531 return ret;
1532}
1533
1534static void sdhci_msm_vreg_deinit_reg(struct sdhci_msm_reg_data *vreg)
1535{
1536 if (vreg->reg)
1537 devm_regulator_put(vreg->reg);
1538}
1539
1540static int sdhci_msm_vreg_set_optimum_mode(struct sdhci_msm_reg_data
1541 *vreg, int uA_load)
1542{
1543 int ret = 0;
1544
1545 /*
1546 * regulators that do not support regulator_set_voltage also
1547 * do not support regulator_set_optimum_mode
1548 */
1549 if (vreg->set_voltage_sup) {
1550 ret = regulator_set_load(vreg->reg, uA_load);
1551 if (ret < 0)
1552 pr_err("%s: regulator_set_load(reg=%s,uA_load=%d) failed. ret=%d\n",
1553 __func__, vreg->name, uA_load, ret);
1554 else
1555 /*
1556 * regulator_set_load() can return non zero
1557 * value even for success case.
1558 */
1559 ret = 0;
1560 }
1561 return ret;
1562}
1563
1564static int sdhci_msm_vreg_set_voltage(struct sdhci_msm_reg_data *vreg,
1565 int min_uV, int max_uV)
1566{
1567 int ret = 0;
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301568 if (vreg->set_voltage_sup) {
1569 ret = regulator_set_voltage(vreg->reg, min_uV, max_uV);
1570 if (ret) {
1571 pr_err("%s: regulator_set_voltage(%s)failed. min_uV=%d,max_uV=%d,ret=%d\n",
Asutosh Das0ef24812012-12-18 16:14:02 +05301572 __func__, vreg->name, min_uV, max_uV, ret);
1573 }
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301574 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301575
1576 return ret;
1577}
1578
1579static int sdhci_msm_vreg_enable(struct sdhci_msm_reg_data *vreg)
1580{
1581 int ret = 0;
1582
1583 /* Put regulator in HPM (high power mode) */
1584 ret = sdhci_msm_vreg_set_optimum_mode(vreg, vreg->hpm_uA);
1585 if (ret < 0)
1586 return ret;
1587
1588 if (!vreg->is_enabled) {
1589 /* Set voltage level */
1590 ret = sdhci_msm_vreg_set_voltage(vreg, vreg->high_vol_level,
1591 vreg->high_vol_level);
1592 if (ret)
1593 return ret;
1594 }
1595 ret = regulator_enable(vreg->reg);
1596 if (ret) {
1597 pr_err("%s: regulator_enable(%s) failed. ret=%d\n",
1598 __func__, vreg->name, ret);
1599 return ret;
1600 }
1601 vreg->is_enabled = true;
1602 return ret;
1603}
1604
1605static int sdhci_msm_vreg_disable(struct sdhci_msm_reg_data *vreg)
1606{
1607 int ret = 0;
1608
1609 /* Never disable regulator marked as always_on */
1610 if (vreg->is_enabled && !vreg->is_always_on) {
1611 ret = regulator_disable(vreg->reg);
1612 if (ret) {
1613 pr_err("%s: regulator_disable(%s) failed. ret=%d\n",
1614 __func__, vreg->name, ret);
1615 goto out;
1616 }
1617 vreg->is_enabled = false;
1618
1619 ret = sdhci_msm_vreg_set_optimum_mode(vreg, 0);
1620 if (ret < 0)
1621 goto out;
1622
1623 /* Set min. voltage level to 0 */
1624 ret = sdhci_msm_vreg_set_voltage(vreg, 0, vreg->high_vol_level);
1625 if (ret)
1626 goto out;
1627 } else if (vreg->is_enabled && vreg->is_always_on) {
1628 if (vreg->lpm_sup) {
1629 /* Put always_on regulator in LPM (low power mode) */
1630 ret = sdhci_msm_vreg_set_optimum_mode(vreg,
1631 vreg->lpm_uA);
1632 if (ret < 0)
1633 goto out;
1634 }
1635 }
1636out:
1637 return ret;
1638}
1639
1640static int sdhci_msm_setup_vreg(struct sdhci_msm_pltfm_data *pdata,
1641 bool enable, bool is_init)
1642{
1643 int ret = 0, i;
1644 struct sdhci_msm_slot_reg_data *curr_slot;
1645 struct sdhci_msm_reg_data *vreg_table[2];
1646
1647 curr_slot = pdata->vreg_data;
1648 if (!curr_slot) {
1649 pr_debug("%s: vreg info unavailable,assuming the slot is powered by always on domain\n",
1650 __func__);
1651 goto out;
1652 }
1653
1654 vreg_table[0] = curr_slot->vdd_data;
1655 vreg_table[1] = curr_slot->vdd_io_data;
1656
1657 for (i = 0; i < ARRAY_SIZE(vreg_table); i++) {
1658 if (vreg_table[i]) {
1659 if (enable)
1660 ret = sdhci_msm_vreg_enable(vreg_table[i]);
1661 else
1662 ret = sdhci_msm_vreg_disable(vreg_table[i]);
1663 if (ret)
1664 goto out;
1665 }
1666 }
1667out:
1668 return ret;
1669}
1670
1671/*
1672 * Reset vreg by ensuring it is off during probe. A call
1673 * to enable vreg is needed to balance disable vreg
1674 */
1675static int sdhci_msm_vreg_reset(struct sdhci_msm_pltfm_data *pdata)
1676{
1677 int ret;
1678
1679 ret = sdhci_msm_setup_vreg(pdata, 1, true);
1680 if (ret)
1681 return ret;
1682 ret = sdhci_msm_setup_vreg(pdata, 0, true);
1683 return ret;
1684}
1685
1686/* This init function should be called only once for each SDHC slot */
1687static int sdhci_msm_vreg_init(struct device *dev,
1688 struct sdhci_msm_pltfm_data *pdata,
1689 bool is_init)
1690{
1691 int ret = 0;
1692 struct sdhci_msm_slot_reg_data *curr_slot;
1693 struct sdhci_msm_reg_data *curr_vdd_reg, *curr_vdd_io_reg;
1694
1695 curr_slot = pdata->vreg_data;
1696 if (!curr_slot)
1697 goto out;
1698
1699 curr_vdd_reg = curr_slot->vdd_data;
1700 curr_vdd_io_reg = curr_slot->vdd_io_data;
1701
1702 if (!is_init)
1703 /* Deregister all regulators from regulator framework */
1704 goto vdd_io_reg_deinit;
1705
1706 /*
1707 * Get the regulator handle from voltage regulator framework
1708 * and then try to set the voltage level for the regulator
1709 */
1710 if (curr_vdd_reg) {
1711 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_reg);
1712 if (ret)
1713 goto out;
1714 }
1715 if (curr_vdd_io_reg) {
1716 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_io_reg);
1717 if (ret)
1718 goto vdd_reg_deinit;
1719 }
1720 ret = sdhci_msm_vreg_reset(pdata);
1721 if (ret)
1722 dev_err(dev, "vreg reset failed (%d)\n", ret);
1723 goto out;
1724
1725vdd_io_reg_deinit:
1726 if (curr_vdd_io_reg)
1727 sdhci_msm_vreg_deinit_reg(curr_vdd_io_reg);
1728vdd_reg_deinit:
1729 if (curr_vdd_reg)
1730 sdhci_msm_vreg_deinit_reg(curr_vdd_reg);
1731out:
1732 return ret;
1733}
1734
1735
1736static int sdhci_msm_set_vdd_io_vol(struct sdhci_msm_pltfm_data *pdata,
1737 enum vdd_io_level level,
1738 unsigned int voltage_level)
1739{
1740 int ret = 0;
1741 int set_level;
1742 struct sdhci_msm_reg_data *vdd_io_reg;
1743
1744 if (!pdata->vreg_data)
1745 return ret;
1746
1747 vdd_io_reg = pdata->vreg_data->vdd_io_data;
1748 if (vdd_io_reg && vdd_io_reg->is_enabled) {
1749 switch (level) {
1750 case VDD_IO_LOW:
1751 set_level = vdd_io_reg->low_vol_level;
1752 break;
1753 case VDD_IO_HIGH:
1754 set_level = vdd_io_reg->high_vol_level;
1755 break;
1756 case VDD_IO_SET_LEVEL:
1757 set_level = voltage_level;
1758 break;
1759 default:
1760 pr_err("%s: invalid argument level = %d",
1761 __func__, level);
1762 ret = -EINVAL;
1763 return ret;
1764 }
1765 ret = sdhci_msm_vreg_set_voltage(vdd_io_reg, set_level,
1766 set_level);
1767 }
1768 return ret;
1769}
1770
1771static irqreturn_t sdhci_msm_pwr_irq(int irq, void *data)
1772{
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001773 struct sdhci_host *host = (struct sdhci_host *)data;
1774 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1775 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Das0ef24812012-12-18 16:14:02 +05301776 u8 irq_status = 0;
1777 u8 irq_ack = 0;
1778 int ret = 0;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301779 int pwr_state = 0, io_level = 0;
1780 unsigned long flags;
Asutosh Das0ef24812012-12-18 16:14:02 +05301781
1782 irq_status = readb_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
1783 pr_debug("%s: Received IRQ(%d), status=0x%x\n",
1784 mmc_hostname(msm_host->mmc), irq, irq_status);
1785
1786 /* Clear the interrupt */
1787 writeb_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
1788 /*
1789 * SDHC has core_mem and hc_mem device memory and these memory
1790 * addresses do not fall within 1KB region. Hence, any update to
1791 * core_mem address space would require an mb() to ensure this gets
1792 * completed before its next update to registers within hc_mem.
1793 */
1794 mb();
1795
1796 /* Handle BUS ON/OFF*/
1797 if (irq_status & CORE_PWRCTL_BUS_ON) {
1798 ret = sdhci_msm_setup_vreg(msm_host->pdata, true, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301799 if (!ret) {
Asutosh Das0ef24812012-12-18 16:14:02 +05301800 ret = sdhci_msm_setup_pins(msm_host->pdata, true);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301801 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
1802 VDD_IO_HIGH, 0);
1803 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301804 if (ret)
1805 irq_ack |= CORE_PWRCTL_BUS_FAIL;
1806 else
1807 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301808
1809 pwr_state = REQ_BUS_ON;
1810 io_level = REQ_IO_HIGH;
Asutosh Das0ef24812012-12-18 16:14:02 +05301811 }
1812 if (irq_status & CORE_PWRCTL_BUS_OFF) {
1813 ret = sdhci_msm_setup_vreg(msm_host->pdata, false, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301814 if (!ret) {
Asutosh Das0ef24812012-12-18 16:14:02 +05301815 ret = sdhci_msm_setup_pins(msm_host->pdata, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301816 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
1817 VDD_IO_LOW, 0);
1818 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301819 if (ret)
1820 irq_ack |= CORE_PWRCTL_BUS_FAIL;
1821 else
1822 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301823
1824 pwr_state = REQ_BUS_OFF;
1825 io_level = REQ_IO_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05301826 }
1827 /* Handle IO LOW/HIGH */
1828 if (irq_status & CORE_PWRCTL_IO_LOW) {
1829 /* Switch voltage Low */
1830 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_LOW, 0);
1831 if (ret)
1832 irq_ack |= CORE_PWRCTL_IO_FAIL;
1833 else
1834 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301835
1836 io_level = REQ_IO_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05301837 }
1838 if (irq_status & CORE_PWRCTL_IO_HIGH) {
1839 /* Switch voltage High */
1840 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_HIGH, 0);
1841 if (ret)
1842 irq_ack |= CORE_PWRCTL_IO_FAIL;
1843 else
1844 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301845
1846 io_level = REQ_IO_HIGH;
Asutosh Das0ef24812012-12-18 16:14:02 +05301847 }
1848
1849 /* ACK status to the core */
1850 writeb_relaxed(irq_ack, (msm_host->core_mem + CORE_PWRCTL_CTL));
1851 /*
1852 * SDHC has core_mem and hc_mem device memory and these memory
1853 * addresses do not fall within 1KB region. Hence, any update to
1854 * core_mem address space would require an mb() to ensure this gets
1855 * completed before its next update to registers within hc_mem.
1856 */
1857 mb();
1858
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301859 if (io_level & REQ_IO_HIGH)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001860 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
1861 ~CORE_IO_PAD_PWR_SWITCH),
1862 host->ioaddr + CORE_VENDOR_SPEC);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301863 else if (io_level & REQ_IO_LOW)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001864 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
1865 CORE_IO_PAD_PWR_SWITCH),
1866 host->ioaddr + CORE_VENDOR_SPEC);
1867 mb();
1868
Asutosh Das0ef24812012-12-18 16:14:02 +05301869 pr_debug("%s: Handled IRQ(%d), ret=%d, ack=0x%x\n",
1870 mmc_hostname(msm_host->mmc), irq, ret, irq_ack);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301871 spin_lock_irqsave(&host->lock, flags);
1872 if (pwr_state)
1873 msm_host->curr_pwr_state = pwr_state;
1874 if (io_level)
1875 msm_host->curr_io_level = io_level;
1876 complete(&msm_host->pwr_irq_completion);
1877 spin_unlock_irqrestore(&host->lock, flags);
1878
Asutosh Das0ef24812012-12-18 16:14:02 +05301879 return IRQ_HANDLED;
1880}
1881
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301882static ssize_t
Sahitya Tummala5c55b932013-06-20 14:00:18 +05301883show_polling(struct device *dev, struct device_attribute *attr, char *buf)
1884{
1885 struct sdhci_host *host = dev_get_drvdata(dev);
1886 int poll;
1887 unsigned long flags;
1888
1889 spin_lock_irqsave(&host->lock, flags);
1890 poll = !!(host->mmc->caps & MMC_CAP_NEEDS_POLL);
1891 spin_unlock_irqrestore(&host->lock, flags);
1892
1893 return snprintf(buf, PAGE_SIZE, "%d\n", poll);
1894}
1895
1896static ssize_t
1897store_polling(struct device *dev, struct device_attribute *attr,
1898 const char *buf, size_t count)
1899{
1900 struct sdhci_host *host = dev_get_drvdata(dev);
1901 int value;
1902 unsigned long flags;
1903
1904 if (!kstrtou32(buf, 0, &value)) {
1905 spin_lock_irqsave(&host->lock, flags);
1906 if (value) {
1907 host->mmc->caps |= MMC_CAP_NEEDS_POLL;
1908 mmc_detect_change(host->mmc, 0);
1909 } else {
1910 host->mmc->caps &= ~MMC_CAP_NEEDS_POLL;
1911 }
1912 spin_unlock_irqrestore(&host->lock, flags);
1913 }
1914 return count;
1915}
1916
1917static ssize_t
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301918show_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
1919 char *buf)
1920{
1921 struct sdhci_host *host = dev_get_drvdata(dev);
1922 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1923 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1924
1925 return snprintf(buf, PAGE_SIZE, "%u\n",
1926 msm_host->msm_bus_vote.is_max_bw_needed);
1927}
1928
1929static ssize_t
1930store_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
1931 const char *buf, size_t count)
1932{
1933 struct sdhci_host *host = dev_get_drvdata(dev);
1934 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1935 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1936 uint32_t value;
1937 unsigned long flags;
1938
1939 if (!kstrtou32(buf, 0, &value)) {
1940 spin_lock_irqsave(&host->lock, flags);
1941 msm_host->msm_bus_vote.is_max_bw_needed = !!value;
1942 spin_unlock_irqrestore(&host->lock, flags);
1943 }
1944 return count;
1945}
1946
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301947static void sdhci_msm_check_power_status(struct sdhci_host *host, u32 req_type)
Asutosh Das0ef24812012-12-18 16:14:02 +05301948{
1949 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1950 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301951 unsigned long flags;
1952 bool done = false;
Sahitya Tummala481fbb02013-08-06 15:22:28 +05301953 u32 io_sig_sts;
Asutosh Das0ef24812012-12-18 16:14:02 +05301954
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301955 spin_lock_irqsave(&host->lock, flags);
1956 pr_debug("%s: %s: request %d curr_pwr_state %x curr_io_level %x\n",
1957 mmc_hostname(host->mmc), __func__, req_type,
1958 msm_host->curr_pwr_state, msm_host->curr_io_level);
Sahitya Tummala481fbb02013-08-06 15:22:28 +05301959 io_sig_sts = readl_relaxed(msm_host->core_mem + CORE_GENERICS);
1960 /*
1961 * The IRQ for request type IO High/Low will be generated when -
1962 * 1. SWITCHABLE_SIGNALLING_VOL is enabled in HW.
1963 * 2. If 1 is true and when there is a state change in 1.8V enable
1964 * bit (bit 3) of SDHCI_HOST_CONTROL2 register. The reset state of
1965 * that bit is 0 which indicates 3.3V IO voltage. So, when MMC core
1966 * layer tries to set it to 3.3V before card detection happens, the
1967 * IRQ doesn't get triggered as there is no state change in this bit.
1968 * The driver already handles this case by changing the IO voltage
1969 * level to high as part of controller power up sequence. Hence, check
1970 * for host->pwr to handle a case where IO voltage high request is
1971 * issued even before controller power up.
1972 */
1973 if (req_type & (REQ_IO_HIGH | REQ_IO_LOW)) {
1974 if (!(io_sig_sts & SWITCHABLE_SIGNALLING_VOL) ||
1975 ((req_type & REQ_IO_HIGH) && !host->pwr)) {
1976 pr_debug("%s: do not wait for power IRQ that never comes\n",
1977 mmc_hostname(host->mmc));
1978 spin_unlock_irqrestore(&host->lock, flags);
1979 return;
1980 }
1981 }
1982
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301983 if ((req_type & msm_host->curr_pwr_state) ||
1984 (req_type & msm_host->curr_io_level))
1985 done = true;
1986 spin_unlock_irqrestore(&host->lock, flags);
Asutosh Das0ef24812012-12-18 16:14:02 +05301987
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301988 /*
1989 * This is needed here to hanlde a case where IRQ gets
1990 * triggered even before this function is called so that
1991 * x->done counter of completion gets reset. Otherwise,
1992 * next call to wait_for_completion returns immediately
1993 * without actually waiting for the IRQ to be handled.
1994 */
1995 if (done)
1996 init_completion(&msm_host->pwr_irq_completion);
1997 else
1998 wait_for_completion(&msm_host->pwr_irq_completion);
1999
2000 pr_debug("%s: %s: request %d done\n", mmc_hostname(host->mmc),
2001 __func__, req_type);
Asutosh Das0ef24812012-12-18 16:14:02 +05302002}
2003
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002004static void sdhci_msm_toggle_cdr(struct sdhci_host *host, bool enable)
2005{
2006 if (enable)
2007 writel_relaxed((readl_relaxed(host->ioaddr +
2008 CORE_DLL_CONFIG) | CORE_CDR_EN),
2009 host->ioaddr + CORE_DLL_CONFIG);
2010 else
2011 writel_relaxed((readl_relaxed(host->ioaddr +
2012 CORE_DLL_CONFIG) & ~CORE_CDR_EN),
2013 host->ioaddr + CORE_DLL_CONFIG);
2014}
2015
Asutosh Das648f9d12013-01-10 21:11:04 +05302016static unsigned int sdhci_msm_max_segs(void)
2017{
2018 return SDHCI_MSM_MAX_SEGMENTS;
2019}
2020
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302021static unsigned int sdhci_msm_get_min_clock(struct sdhci_host *host)
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302022{
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302023 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2024 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302025
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302026 return msm_host->pdata->sup_clk_table[0];
2027}
2028
2029static unsigned int sdhci_msm_get_max_clock(struct sdhci_host *host)
2030{
2031 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2032 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2033 int max_clk_index = msm_host->pdata->sup_clk_cnt;
2034
2035 return msm_host->pdata->sup_clk_table[max_clk_index - 1];
2036}
2037
2038static unsigned int sdhci_msm_get_sup_clk_rate(struct sdhci_host *host,
2039 u32 req_clk)
2040{
2041 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2042 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2043 unsigned int sel_clk = -1;
2044 unsigned char cnt;
2045
2046 if (req_clk < sdhci_msm_get_min_clock(host)) {
2047 sel_clk = sdhci_msm_get_min_clock(host);
2048 return sel_clk;
2049 }
2050
2051 for (cnt = 0; cnt < msm_host->pdata->sup_clk_cnt; cnt++) {
2052 if (msm_host->pdata->sup_clk_table[cnt] > req_clk) {
2053 break;
2054 } else if (msm_host->pdata->sup_clk_table[cnt] == req_clk) {
2055 sel_clk = msm_host->pdata->sup_clk_table[cnt];
2056 break;
2057 } else {
2058 sel_clk = msm_host->pdata->sup_clk_table[cnt];
2059 }
2060 }
2061 return sel_clk;
2062}
2063
2064static int sdhci_msm_prepare_clocks(struct sdhci_host *host, bool enable)
2065{
2066 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2067 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2068 int rc = 0;
2069
2070 if (enable && !atomic_read(&msm_host->clks_on)) {
2071 pr_debug("%s: request to enable clocks\n",
2072 mmc_hostname(host->mmc));
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302073
2074 sdhci_msm_bus_voting(host, 1);
2075
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302076 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
2077 rc = clk_prepare_enable(msm_host->bus_clk);
2078 if (rc) {
2079 pr_err("%s: %s: failed to enable the bus-clock with error %d\n",
2080 mmc_hostname(host->mmc), __func__, rc);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302081 goto remove_vote;
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302082 }
2083 }
2084 if (!IS_ERR(msm_host->pclk)) {
2085 rc = clk_prepare_enable(msm_host->pclk);
2086 if (rc) {
2087 pr_err("%s: %s: failed to enable the pclk with error %d\n",
2088 mmc_hostname(host->mmc), __func__, rc);
2089 goto disable_bus_clk;
2090 }
2091 }
2092 rc = clk_prepare_enable(msm_host->clk);
2093 if (rc) {
2094 pr_err("%s: %s: failed to enable the host-clk with error %d\n",
2095 mmc_hostname(host->mmc), __func__, rc);
2096 goto disable_pclk;
2097 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002098 if (!IS_ERR(msm_host->ff_clk)) {
2099 rc = clk_prepare_enable(msm_host->ff_clk);
2100 if (rc) {
2101 pr_err("%s: %s: failed to enable the ff_clk with error %d\n",
2102 mmc_hostname(host->mmc), __func__, rc);
2103 goto disable_clk;
2104 }
2105 }
2106 if (!IS_ERR(msm_host->sleep_clk)) {
2107 rc = clk_prepare_enable(msm_host->sleep_clk);
2108 if (rc) {
2109 pr_err("%s: %s: failed to enable the sleep_clk with error %d\n",
2110 mmc_hostname(host->mmc), __func__, rc);
2111 goto disable_ff_clk;
2112 }
2113 }
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302114 mb();
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302115
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302116 } else if (!enable && atomic_read(&msm_host->clks_on)) {
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302117 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
2118 mb();
Sahitya Tummaladc182982013-08-20 15:32:09 +05302119 /*
2120 * During 1.8V signal switching the clock source must
2121 * still be ON as it requires accessing SDHC
2122 * registers (SDHCi host control2 register bit 3 must
2123 * be written and polled after stopping the SDCLK).
2124 */
2125 if (host->mmc->card_clock_off)
2126 return 0;
2127 pr_debug("%s: request to disable clocks\n",
2128 mmc_hostname(host->mmc));
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002129 if (!IS_ERR_OR_NULL(msm_host->sleep_clk))
2130 clk_disable_unprepare(msm_host->sleep_clk);
2131 if (!IS_ERR_OR_NULL(msm_host->ff_clk))
2132 clk_disable_unprepare(msm_host->ff_clk);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302133 clk_disable_unprepare(msm_host->clk);
2134 if (!IS_ERR(msm_host->pclk))
2135 clk_disable_unprepare(msm_host->pclk);
2136 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2137 clk_disable_unprepare(msm_host->bus_clk);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302138
2139 sdhci_msm_bus_voting(host, 0);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302140 }
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302141 atomic_set(&msm_host->clks_on, enable);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302142 goto out;
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002143disable_ff_clk:
2144 if (!IS_ERR_OR_NULL(msm_host->ff_clk))
2145 clk_disable_unprepare(msm_host->ff_clk);
2146disable_clk:
2147 if (!IS_ERR_OR_NULL(msm_host->clk))
2148 clk_disable_unprepare(msm_host->clk);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302149disable_pclk:
2150 if (!IS_ERR_OR_NULL(msm_host->pclk))
2151 clk_disable_unprepare(msm_host->pclk);
2152disable_bus_clk:
2153 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2154 clk_disable_unprepare(msm_host->bus_clk);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302155remove_vote:
2156 if (msm_host->msm_bus_vote.client_handle)
2157 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302158out:
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302159 return rc;
2160}
2161
2162static void sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock)
2163{
2164 int rc;
2165 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2166 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2167 struct mmc_ios curr_ios = host->mmc->ios;
2168 u32 sup_clock, ddr_clock;
Sahitya Tummala043744a2013-06-24 09:55:33 +05302169 bool curr_pwrsave;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302170
2171 if (!clock) {
2172 sdhci_msm_prepare_clocks(host, false);
2173 host->clock = clock;
2174 goto out;
2175 }
2176
2177 rc = sdhci_msm_prepare_clocks(host, true);
2178 if (rc)
2179 goto out;
2180
Sahitya Tummala043744a2013-06-24 09:55:33 +05302181 curr_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2182 CORE_CLK_PWRSAVE);
Sahitya Tummalae000b242013-08-29 16:21:08 +05302183 if ((clock > 400000) &&
Sahitya Tummala043744a2013-06-24 09:55:33 +05302184 !curr_pwrsave && mmc_host_may_gate_card(host->mmc->card))
2185 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2186 | CORE_CLK_PWRSAVE,
2187 host->ioaddr + CORE_VENDOR_SPEC);
2188 /*
2189 * Disable pwrsave for a newly added card if doesn't allow clock
2190 * gating.
2191 */
2192 else if (curr_pwrsave && !mmc_host_may_gate_card(host->mmc->card))
2193 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2194 & ~CORE_CLK_PWRSAVE,
2195 host->ioaddr + CORE_VENDOR_SPEC);
2196
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302197 sup_clock = sdhci_msm_get_sup_clk_rate(host, clock);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002198 if ((curr_ios.timing == MMC_TIMING_UHS_DDR50) ||
2199 (curr_ios.timing == MMC_TIMING_MMC_HS400)) {
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302200 /*
2201 * The SDHC requires internal clock frequency to be double the
2202 * actual clock that will be set for DDR mode. The controller
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002203 * uses the faster clock(100/400MHz) for some of its parts and
2204 * send the actual required clock (50/200MHz) to the card.
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302205 */
2206 ddr_clock = clock * 2;
2207 sup_clock = sdhci_msm_get_sup_clk_rate(host,
2208 ddr_clock);
2209 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002210
2211 /*
2212 * In general all timing modes are controlled via UHS mode select in
2213 * Host Control2 register. eMMC specific HS200/HS400 doesn't have
2214 * their respective modes defined here, hence we use these values.
2215 *
2216 * HS200 - SDR104 (Since they both are equivalent in functionality)
2217 * HS400 - This involves multiple configurations
2218 * Initially SDR104 - when tuning is required as HS200
2219 * Then when switching to DDR @ 400MHz (HS400) we use
2220 * the vendor specific HC_SELECT_IN to control the mode.
2221 *
2222 * In addition to controlling the modes we also need to select the
2223 * correct input clock for DLL depending on the mode.
2224 *
2225 * HS400 - divided clock (free running MCLK/2)
2226 * All other modes - default (free running MCLK)
2227 */
2228 if (curr_ios.timing == MMC_TIMING_MMC_HS400) {
2229 /* Select the divided clock (free running MCLK/2) */
2230 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2231 & ~CORE_HC_MCLK_SEL_MASK)
2232 | CORE_HC_MCLK_SEL_HS400),
2233 host->ioaddr + CORE_VENDOR_SPEC);
2234 /*
2235 * Select HS400 mode using the HC_SELECT_IN from VENDOR SPEC
2236 * register
2237 */
2238 if (msm_host->tuning_done && !msm_host->calibration_done) {
2239 /*
2240 * Write 0x6 to HC_SELECT_IN and 1 to HC_SELECT_IN_EN
2241 * field in VENDOR_SPEC_FUNC
2242 */
2243 writel_relaxed((readl_relaxed(host->ioaddr + \
2244 CORE_VENDOR_SPEC)
2245 | CORE_HC_SELECT_IN_HS400
2246 | CORE_HC_SELECT_IN_EN),
2247 host->ioaddr + CORE_VENDOR_SPEC);
2248 }
2249 } else {
2250 /* Select the default clock (free running MCLK) */
2251 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2252 & ~CORE_HC_MCLK_SEL_MASK)
2253 | CORE_HC_MCLK_SEL_DFLT),
2254 host->ioaddr + CORE_VENDOR_SPEC);
2255
2256 /*
2257 * Disable HC_SELECT_IN to be able to use the UHS mode select
2258 * configuration from Host Control2 register for all other
2259 * modes.
2260 *
2261 * Write 0 to HC_SELECT_IN and HC_SELECT_IN_EN field
2262 * in VENDOR_SPEC_FUNC
2263 */
2264 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2265 & ~CORE_HC_SELECT_IN_EN
2266 & ~CORE_HC_SELECT_IN_MASK),
2267 host->ioaddr + CORE_VENDOR_SPEC);
2268 }
2269 mb();
2270
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302271 if (sup_clock != msm_host->clk_rate) {
2272 pr_debug("%s: %s: setting clk rate to %u\n",
2273 mmc_hostname(host->mmc), __func__, sup_clock);
2274 rc = clk_set_rate(msm_host->clk, sup_clock);
2275 if (rc) {
2276 pr_err("%s: %s: Failed to set rate %u for host-clk : %d\n",
2277 mmc_hostname(host->mmc), __func__,
2278 sup_clock, rc);
2279 goto out;
2280 }
2281 msm_host->clk_rate = sup_clock;
2282 host->clock = clock;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302283 /*
2284 * Update the bus vote in case of frequency change due to
2285 * clock scaling.
2286 */
2287 sdhci_msm_bus_voting(host, 1);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302288 }
2289out:
2290 sdhci_set_clock(host, clock);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302291}
2292
Sahitya Tummala14613432013-03-21 11:13:25 +05302293static void sdhci_msm_set_uhs_signaling(struct sdhci_host *host,
2294 unsigned int uhs)
2295{
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002296 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2297 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala14613432013-03-21 11:13:25 +05302298 u16 ctrl_2;
2299
2300 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2301 /* Select Bus Speed Mode for host */
2302 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002303 if (uhs == MMC_TIMING_MMC_HS400)
2304 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2305 else if (uhs == MMC_TIMING_MMC_HS200)
Sahitya Tummala14613432013-03-21 11:13:25 +05302306 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2307 else if (uhs == MMC_TIMING_UHS_SDR12)
2308 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
2309 else if (uhs == MMC_TIMING_UHS_SDR25)
2310 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
2311 else if (uhs == MMC_TIMING_UHS_SDR50)
2312 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
2313 else if (uhs == MMC_TIMING_UHS_SDR104)
2314 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2315 else if (uhs == MMC_TIMING_UHS_DDR50)
2316 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302317 /*
2318 * When clock frquency is less than 100MHz, the feedback clock must be
2319 * provided and DLL must not be used so that tuning can be skipped. To
2320 * provide feedback clock, the mode selection can be any value less
2321 * than 3'b011 in bits [2:0] of HOST CONTROL2 register.
2322 */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002323 if (host->clock <= CORE_FREQ_100MHZ) {
2324 if ((uhs == MMC_TIMING_MMC_HS400) ||
2325 (uhs == MMC_TIMING_MMC_HS200) ||
2326 (uhs == MMC_TIMING_UHS_SDR104))
2327 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302328
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002329 /*
2330 * Make sure DLL is disabled when not required
2331 *
2332 * Write 1 to DLL_RST bit of DLL_CONFIG register
2333 */
2334 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
2335 | CORE_DLL_RST),
2336 host->ioaddr + CORE_DLL_CONFIG);
2337
2338 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
2339 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
2340 | CORE_DLL_PDN),
2341 host->ioaddr + CORE_DLL_CONFIG);
2342 mb();
2343
2344 /*
2345 * The DLL needs to be restored and CDCLP533 recalibrated
2346 * when the clock frequency is set back to 400MHz.
2347 */
2348 msm_host->calibration_done = false;
2349 }
2350
2351 pr_debug("%s: %s-clock:%u uhs mode:%u ctrl_2:0x%x\n",
2352 mmc_hostname(host->mmc), __func__, host->clock, uhs, ctrl_2);
Sahitya Tummala14613432013-03-21 11:13:25 +05302353 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
2354
2355}
2356
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302357#define MAX_TEST_BUS 20
2358
2359void sdhci_msm_dump_vendor_regs(struct sdhci_host *host)
2360{
2361 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2362 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2363 int tbsel, tbsel2;
2364 int i, index = 0;
2365 u32 test_bus_val = 0;
2366 u32 debug_reg[MAX_TEST_BUS] = {0};
2367
2368 pr_info("----------- VENDOR REGISTER DUMP -----------\n");
2369 pr_info("Data cnt: 0x%08x | Fifo cnt: 0x%08x | Int sts: 0x%08x\n",
2370 readl_relaxed(msm_host->core_mem + CORE_MCI_DATA_CNT),
2371 readl_relaxed(msm_host->core_mem + CORE_MCI_FIFO_CNT),
2372 readl_relaxed(msm_host->core_mem + CORE_MCI_STATUS));
2373 pr_info("DLL cfg: 0x%08x | DLL sts: 0x%08x | SDCC ver: 0x%08x\n",
2374 readl_relaxed(host->ioaddr + CORE_DLL_CONFIG),
2375 readl_relaxed(host->ioaddr + CORE_DLL_STATUS),
2376 readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION));
2377 pr_info("Vndr func: 0x%08x | Vndr adma err : addr0: 0x%08x addr1: 0x%08x\n",
2378 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC),
2379 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_ADMA_ERR_ADDR0),
2380 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_ADMA_ERR_ADDR1));
2381
2382 /*
2383 * tbsel indicates [2:0] bits and tbsel2 indicates [7:4] bits
2384 * of CORE_TESTBUS_CONFIG register.
2385 *
2386 * To select test bus 0 to 7 use tbsel and to select any test bus
2387 * above 7 use (tbsel2 | tbsel) to get the test bus number. For eg,
2388 * to select test bus 14, write 0x1E to CORE_TESTBUS_CONFIG register
2389 * i.e., tbsel2[7:4] = 0001, tbsel[2:0] = 110.
2390 */
2391 for (tbsel2 = 0; tbsel2 < 3; tbsel2++) {
2392 for (tbsel = 0; tbsel < 8; tbsel++) {
2393 if (index >= MAX_TEST_BUS)
2394 break;
2395 test_bus_val = (tbsel2 << CORE_TESTBUS_SEL2_BIT) |
2396 tbsel | CORE_TESTBUS_ENA;
2397 writel_relaxed(test_bus_val,
2398 msm_host->core_mem + CORE_TESTBUS_CONFIG);
2399 debug_reg[index++] = readl_relaxed(msm_host->core_mem +
2400 CORE_SDCC_DEBUG_REG);
2401 }
2402 }
2403 for (i = 0; i < MAX_TEST_BUS; i = i + 4)
2404 pr_info(" Test bus[%d to %d]: 0x%08x 0x%08x 0x%08x 0x%08x\n",
2405 i, i + 3, debug_reg[i], debug_reg[i+1],
2406 debug_reg[i+2], debug_reg[i+3]);
2407 /* Disable test bus */
2408 writel_relaxed(~CORE_TESTBUS_ENA, msm_host->core_mem +
2409 CORE_TESTBUS_CONFIG);
2410}
2411
Asutosh Das0ef24812012-12-18 16:14:02 +05302412static struct sdhci_ops sdhci_msm_ops = {
Sahitya Tummala14613432013-03-21 11:13:25 +05302413 .set_uhs_signaling = sdhci_msm_set_uhs_signaling,
Asutosh Das0ef24812012-12-18 16:14:02 +05302414 .check_power_status = sdhci_msm_check_power_status,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002415 .platform_execute_tuning = sdhci_msm_execute_tuning,
2416 .toggle_cdr = sdhci_msm_toggle_cdr,
Asutosh Das648f9d12013-01-10 21:11:04 +05302417 .get_max_segments = sdhci_msm_max_segs,
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302418 .set_clock = sdhci_msm_set_clock,
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302419 .get_min_clock = sdhci_msm_get_min_clock,
2420 .get_max_clock = sdhci_msm_get_max_clock,
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302421 .dump_vendor_regs = sdhci_msm_dump_vendor_regs,
Asutosh Dase5e9ca62013-07-30 19:08:36 +05302422 .config_auto_tuning_cmd = sdhci_msm_config_auto_tuning_cmd,
Asutosh Das0ef24812012-12-18 16:14:02 +05302423};
2424
2425static int sdhci_msm_probe(struct platform_device *pdev)
2426{
2427 struct sdhci_host *host;
2428 struct sdhci_pltfm_host *pltfm_host;
2429 struct sdhci_msm_host *msm_host;
2430 struct resource *core_memres = NULL;
2431 int ret = 0, pwr_irq = 0, dead = 0;
Stephen Boyd8dce5c62013-04-24 14:19:46 -07002432 u16 host_version;
Subhash Jadavani28137342013-05-14 17:46:43 +05302433 u32 pwr, irq_status, irq_ctl;
Asutosh Das0ef24812012-12-18 16:14:02 +05302434
2435 pr_debug("%s: Enter %s\n", dev_name(&pdev->dev), __func__);
2436 msm_host = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_msm_host),
2437 GFP_KERNEL);
2438 if (!msm_host) {
2439 ret = -ENOMEM;
2440 goto out;
2441 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302442
2443 msm_host->sdhci_msm_pdata.ops = &sdhci_msm_ops;
2444 host = sdhci_pltfm_init(pdev, &msm_host->sdhci_msm_pdata, 0);
2445 if (IS_ERR(host)) {
2446 ret = PTR_ERR(host);
2447 goto out;
2448 }
2449
2450 pltfm_host = sdhci_priv(host);
2451 pltfm_host->priv = msm_host;
2452 msm_host->mmc = host->mmc;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302453 msm_host->pdev = pdev;
Asutosh Das0ef24812012-12-18 16:14:02 +05302454
2455 /* Extract platform data */
2456 if (pdev->dev.of_node) {
Venkat Gopalakrishnan270580a2013-03-11 12:17:57 -07002457 ret = of_alias_get_id(pdev->dev.of_node, "sdhc");
2458 if (ret < 0) {
2459 dev_err(&pdev->dev, "Failed to get slot index %d\n",
2460 ret);
2461 goto pltfm_free;
2462 }
2463 if (disable_slots & (1 << (ret - 1))) {
2464 dev_info(&pdev->dev, "%s: Slot %d disabled\n", __func__,
2465 ret);
2466 ret = -ENODEV;
2467 goto pltfm_free;
2468 }
2469
Asutosh Das0ef24812012-12-18 16:14:02 +05302470 msm_host->pdata = sdhci_msm_populate_pdata(&pdev->dev);
2471 if (!msm_host->pdata) {
2472 dev_err(&pdev->dev, "DT parsing error\n");
2473 goto pltfm_free;
2474 }
2475 } else {
2476 dev_err(&pdev->dev, "No device tree node\n");
2477 goto pltfm_free;
2478 }
2479
2480 /* Setup Clocks */
2481
2482 /* Setup SDCC bus voter clock. */
2483 msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus_clk");
2484 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
2485 /* Vote for max. clk rate for max. performance */
2486 ret = clk_set_rate(msm_host->bus_clk, INT_MAX);
2487 if (ret)
2488 goto pltfm_free;
2489 ret = clk_prepare_enable(msm_host->bus_clk);
2490 if (ret)
2491 goto pltfm_free;
2492 }
2493
2494 /* Setup main peripheral bus clock */
2495 msm_host->pclk = devm_clk_get(&pdev->dev, "iface_clk");
2496 if (!IS_ERR(msm_host->pclk)) {
2497 ret = clk_prepare_enable(msm_host->pclk);
2498 if (ret)
2499 goto bus_clk_disable;
2500 }
2501
2502 /* Setup SDC MMC clock */
2503 msm_host->clk = devm_clk_get(&pdev->dev, "core_clk");
2504 if (IS_ERR(msm_host->clk)) {
2505 ret = PTR_ERR(msm_host->clk);
2506 goto pclk_disable;
2507 }
2508
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302509 /* Set to the minimum supported clock frequency */
2510 ret = clk_set_rate(msm_host->clk, sdhci_msm_get_min_clock(host));
2511 if (ret) {
2512 dev_err(&pdev->dev, "MClk rate set failed (%d)\n", ret);
Sahitya Tummala020ede0d2013-06-07 13:03:07 +05302513 goto pclk_disable;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302514 }
Sahitya Tummala020ede0d2013-06-07 13:03:07 +05302515 ret = clk_prepare_enable(msm_host->clk);
2516 if (ret)
2517 goto pclk_disable;
2518
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302519 msm_host->clk_rate = sdhci_msm_get_min_clock(host);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302520 atomic_set(&msm_host->clks_on, 1);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302521
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002522 /* Setup CDC calibration fixed feedback clock */
2523 msm_host->ff_clk = devm_clk_get(&pdev->dev, "cal_clk");
2524 if (!IS_ERR(msm_host->ff_clk)) {
2525 ret = clk_prepare_enable(msm_host->ff_clk);
2526 if (ret)
2527 goto clk_disable;
2528 }
2529
2530 /* Setup CDC calibration sleep clock */
2531 msm_host->sleep_clk = devm_clk_get(&pdev->dev, "sleep_clk");
2532 if (!IS_ERR(msm_host->sleep_clk)) {
2533 ret = clk_prepare_enable(msm_host->sleep_clk);
2534 if (ret)
2535 goto ff_clk_disable;
2536 }
2537
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07002538 msm_host->saved_tuning_phase = INVALID_TUNING_PHASE;
2539
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302540 ret = sdhci_msm_bus_register(msm_host, pdev);
2541 if (ret)
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002542 goto sleep_clk_disable;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302543
2544 if (msm_host->msm_bus_vote.client_handle)
2545 INIT_DELAYED_WORK(&msm_host->msm_bus_vote.vote_work,
2546 sdhci_msm_bus_work);
2547 sdhci_msm_bus_voting(host, 1);
2548
Asutosh Das0ef24812012-12-18 16:14:02 +05302549 /* Setup regulators */
2550 ret = sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, true);
2551 if (ret) {
2552 dev_err(&pdev->dev, "Regulator setup failed (%d)\n", ret);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302553 goto bus_unregister;
Asutosh Das0ef24812012-12-18 16:14:02 +05302554 }
2555
2556 /* Reset the core and Enable SDHC mode */
2557 core_memres = platform_get_resource_byname(pdev,
2558 IORESOURCE_MEM, "core_mem");
2559 msm_host->core_mem = devm_ioremap(&pdev->dev, core_memres->start,
2560 resource_size(core_memres));
2561
2562 if (!msm_host->core_mem) {
2563 dev_err(&pdev->dev, "Failed to remap registers\n");
2564 ret = -ENOMEM;
2565 goto vreg_deinit;
2566 }
2567
Stepan Moskovchenkoa4d0fa72013-09-13 22:19:33 -07002568 /* Unset HC_MODE_EN bit in HC_MODE register */
2569 writel_relaxed(0, (msm_host->core_mem + CORE_HC_MODE));
2570
Asutosh Das0ef24812012-12-18 16:14:02 +05302571 /* Set SW_RST bit in POWER register (Offset 0x0) */
Sahitya Tummala66b0fe32013-04-25 11:50:56 +05302572 writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_POWER) |
2573 CORE_SW_RST, msm_host->core_mem + CORE_POWER);
2574 /*
2575 * SW reset can take upto 10HCLK + 15MCLK cycles.
2576 * Calculating based on min clk rates (hclk = 27MHz,
2577 * mclk = 400KHz) it comes to ~40us. Let's poll for
2578 * max. 1ms for reset completion.
2579 */
2580 ret = readl_poll_timeout(msm_host->core_mem + CORE_POWER,
2581 pwr, !(pwr & CORE_SW_RST), 100, 10);
2582
2583 if (ret) {
2584 dev_err(&pdev->dev, "reset failed (%d)\n", ret);
2585 goto vreg_deinit;
2586 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302587 /* Set HC_MODE_EN bit in HC_MODE register */
2588 writel_relaxed(HC_MODE_EN, (msm_host->core_mem + CORE_HC_MODE));
2589
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002590 /* Set FF_CLK_SW_RST_DIS bit in HC_MODE register */
2591 writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_HC_MODE) |
2592 FF_CLK_SW_RST_DIS, msm_host->core_mem + CORE_HC_MODE);
2593
Asutosh Das0ef24812012-12-18 16:14:02 +05302594 /*
Subhash Jadavani28137342013-05-14 17:46:43 +05302595 * CORE_SW_RST above may trigger power irq if previous status of PWRCTL
2596 * was either BUS_ON or IO_HIGH_V. So before we enable the power irq
2597 * interrupt in GIC (by registering the interrupt handler), we need to
2598 * ensure that any pending power irq interrupt status is acknowledged
2599 * otherwise power irq interrupt handler would be fired prematurely.
2600 */
2601 irq_status = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
2602 writel_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
2603 irq_ctl = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_CTL);
2604 if (irq_status & (CORE_PWRCTL_BUS_ON | CORE_PWRCTL_BUS_OFF))
2605 irq_ctl |= CORE_PWRCTL_BUS_SUCCESS;
2606 if (irq_status & (CORE_PWRCTL_IO_HIGH | CORE_PWRCTL_IO_LOW))
2607 irq_ctl |= CORE_PWRCTL_IO_SUCCESS;
2608 writel_relaxed(irq_ctl, (msm_host->core_mem + CORE_PWRCTL_CTL));
2609 /*
2610 * Ensure that above writes are propogated before interrupt enablement
2611 * in GIC.
2612 */
2613 mb();
2614
2615 /*
Asutosh Das0ef24812012-12-18 16:14:02 +05302616 * Following are the deviations from SDHC spec v3.0 -
2617 * 1. Card detection is handled using separate GPIO.
2618 * 2. Bus power control is handled by interacting with PMIC.
2619 */
2620 host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
2621 host->quirks |= SDHCI_QUIRK_SINGLE_POWER_WRITE;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302622 host->quirks |= SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN;
2623 host->quirks2 |= SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK;
Sahitya Tummala87d43942013-04-12 11:49:11 +05302624 host->quirks2 |= SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD;
Sahitya Tummala314162c2013-04-12 12:11:20 +05302625 host->quirks2 |= SDHCI_QUIRK2_BROKEN_PRESET_VALUE;
Sahitya Tummala7c9780d2013-04-12 11:59:25 +05302626 host->quirks2 |= SDHCI_QUIRK2_USE_RESERVED_MAX_TIMEOUT;
Asutosh Das0ef24812012-12-18 16:14:02 +05302627
Sahitya Tummalaa5733ab52013-06-10 16:32:51 +05302628 if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK)
2629 host->quirks2 |= SDHCI_QUIRK2_DIVIDE_TOUT_BY_4;
2630
Stephen Boyd8dce5c62013-04-24 14:19:46 -07002631 host_version = readw_relaxed((host->ioaddr + SDHCI_HOST_VERSION));
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07002632 dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n",
2633 host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >>
2634 SDHCI_VENDOR_VER_SHIFT));
2635 if (((host_version & SDHCI_VENDOR_VER_MASK) >>
2636 SDHCI_VENDOR_VER_SHIFT) == SDHCI_VER_100) {
2637 /*
2638 * Add 40us delay in interrupt handler when
2639 * operating at initialization frequency(400KHz).
2640 */
2641 host->quirks2 |= SDHCI_QUIRK2_SLOW_INT_CLR;
2642 /*
2643 * Set Software Reset for DAT line in Software
2644 * Reset Register (Bit 2).
2645 */
2646 host->quirks2 |= SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT;
2647 }
2648
Asutosh Das214b9662013-06-13 14:27:42 +05302649 host->quirks2 |= SDHCI_QUIRK2_IGN_DATA_END_BIT_ERROR;
2650
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07002651 /* Setup PWRCTL irq */
Asutosh Das0ef24812012-12-18 16:14:02 +05302652 pwr_irq = platform_get_irq_byname(pdev, "pwr_irq");
2653 if (pwr_irq < 0) {
2654 dev_err(&pdev->dev, "Failed to get pwr_irq by name (%d)\n",
2655 pwr_irq);
2656 goto vreg_deinit;
2657 }
2658 ret = devm_request_threaded_irq(&pdev->dev, pwr_irq, NULL,
2659 sdhci_msm_pwr_irq, IRQF_ONESHOT,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002660 dev_name(&pdev->dev), host);
Asutosh Das0ef24812012-12-18 16:14:02 +05302661 if (ret) {
2662 dev_err(&pdev->dev, "Request threaded irq(%d) failed (%d)\n",
2663 pwr_irq, ret);
2664 goto vreg_deinit;
2665 }
2666
2667 /* Enable pwr irq interrupts */
2668 writel_relaxed(INT_MASK, (msm_host->core_mem + CORE_PWRCTL_MASK));
2669
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302670#ifdef CONFIG_MMC_CLKGATE
2671 /* Set clock gating delay to be used when CONFIG_MMC_CLKGATE is set */
2672 msm_host->mmc->clkgate_delay = SDHCI_MSM_MMC_CLK_GATE_DELAY;
2673#endif
2674
Asutosh Das0ef24812012-12-18 16:14:02 +05302675 /* Set host capabilities */
2676 msm_host->mmc->caps |= msm_host->pdata->mmc_bus_width;
2677 msm_host->mmc->caps |= msm_host->pdata->caps;
Asutosh Das0ef24812012-12-18 16:14:02 +05302678 msm_host->mmc->caps2 |= msm_host->pdata->caps2;
2679 msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR;
2680 msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR_CONTROL;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302681 msm_host->mmc->caps2 |= MMC_CAP2_CLK_SCALE;
Subhash Jadavani6d472b22013-05-29 15:52:10 +05302682 msm_host->mmc->caps2 |= MMC_CAP2_ASYNC_SDIO_IRQ_4BIT_MODE;
Asutosh Dasbea115d2013-06-24 18:20:45 +05302683 msm_host->mmc->pm_caps |= MMC_PM_KEEP_POWER;
Asutosh Das0ef24812012-12-18 16:14:02 +05302684
2685 if (msm_host->pdata->nonremovable)
2686 msm_host->mmc->caps |= MMC_CAP_NONREMOVABLE;
2687
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05302688 host->cpu_dma_latency_us = msm_host->pdata->cpu_dma_latency_us;
2689
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302690 init_completion(&msm_host->pwr_irq_completion);
2691
Sahitya Tummala581df132013-03-12 14:57:46 +05302692 if (gpio_is_valid(msm_host->pdata->status_gpio)) {
2693 ret = mmc_gpio_request_cd(msm_host->mmc,
2694 msm_host->pdata->status_gpio, 0);
2695 if (ret) {
2696 dev_err(&pdev->dev, "%s: Failed to request card detection IRQ %d\n",
2697 __func__, ret);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302698 goto vreg_deinit;
Sahitya Tummala581df132013-03-12 14:57:46 +05302699 }
2700 }
2701
Sahitya Tummalaeaa21862013-03-20 19:34:59 +05302702 if (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(32))) {
2703 host->dma_mask = DMA_BIT_MASK(32);
2704 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2705 } else {
2706 dev_err(&pdev->dev, "%s: Failed to set dma mask\n", __func__);
2707 }
2708
Asutosh Das0ef24812012-12-18 16:14:02 +05302709 ret = sdhci_add_host(host);
2710 if (ret) {
2711 dev_err(&pdev->dev, "Add host failed (%d)\n", ret);
Sahitya Tummala581df132013-03-12 14:57:46 +05302712 goto vreg_deinit;
Asutosh Das0ef24812012-12-18 16:14:02 +05302713 }
2714
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302715 msm_host->msm_bus_vote.max_bus_bw.show = show_sdhci_max_bus_bw;
2716 msm_host->msm_bus_vote.max_bus_bw.store = store_sdhci_max_bus_bw;
2717 sysfs_attr_init(&msm_host->msm_bus_vote.max_bus_bw.attr);
2718 msm_host->msm_bus_vote.max_bus_bw.attr.name = "max_bus_bw";
2719 msm_host->msm_bus_vote.max_bus_bw.attr.mode = S_IRUGO | S_IWUSR;
2720 ret = device_create_file(&pdev->dev,
2721 &msm_host->msm_bus_vote.max_bus_bw);
2722 if (ret)
2723 goto remove_host;
2724
Sahitya Tummala5c55b932013-06-20 14:00:18 +05302725 if (!gpio_is_valid(msm_host->pdata->status_gpio)) {
2726 msm_host->polling.show = show_polling;
2727 msm_host->polling.store = store_polling;
2728 sysfs_attr_init(&msm_host->polling.attr);
2729 msm_host->polling.attr.name = "polling";
2730 msm_host->polling.attr.mode = S_IRUGO | S_IWUSR;
2731 ret = device_create_file(&pdev->dev, &msm_host->polling);
2732 if (ret)
2733 goto remove_max_bus_bw_file;
2734 }
Asutosh Dase5e9ca62013-07-30 19:08:36 +05302735
2736 msm_host->auto_cmd21_attr.show = show_auto_cmd21;
2737 msm_host->auto_cmd21_attr.store = store_auto_cmd21;
2738 sysfs_attr_init(&msm_host->auto_cmd21_attr.attr);
2739 msm_host->auto_cmd21_attr.attr.name = "enable_auto_cmd21";
2740 msm_host->auto_cmd21_attr.attr.mode = S_IRUGO | S_IWUSR;
2741 ret = device_create_file(&pdev->dev, &msm_host->auto_cmd21_attr);
2742 if (ret) {
2743 pr_err("%s: %s: failed creating auto-cmd21 attr: %d\n",
2744 mmc_hostname(host->mmc), __func__, ret);
2745 device_remove_file(&pdev->dev, &msm_host->auto_cmd21_attr);
2746 }
2747
Asutosh Das0ef24812012-12-18 16:14:02 +05302748 /* Successful initialization */
2749 goto out;
2750
Sahitya Tummala5c55b932013-06-20 14:00:18 +05302751remove_max_bus_bw_file:
2752 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Asutosh Das0ef24812012-12-18 16:14:02 +05302753remove_host:
2754 dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
2755 sdhci_remove_host(host, dead);
2756vreg_deinit:
2757 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302758bus_unregister:
2759 if (msm_host->msm_bus_vote.client_handle)
2760 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2761 sdhci_msm_bus_unregister(msm_host);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002762sleep_clk_disable:
2763 if (!IS_ERR(msm_host->sleep_clk))
2764 clk_disable_unprepare(msm_host->sleep_clk);
2765ff_clk_disable:
2766 if (!IS_ERR(msm_host->ff_clk))
2767 clk_disable_unprepare(msm_host->ff_clk);
Asutosh Das0ef24812012-12-18 16:14:02 +05302768clk_disable:
2769 if (!IS_ERR(msm_host->clk))
2770 clk_disable_unprepare(msm_host->clk);
2771pclk_disable:
2772 if (!IS_ERR(msm_host->pclk))
2773 clk_disable_unprepare(msm_host->pclk);
2774bus_clk_disable:
2775 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2776 clk_disable_unprepare(msm_host->bus_clk);
2777pltfm_free:
2778 sdhci_pltfm_free(pdev);
2779out:
2780 pr_debug("%s: Exit %s\n", dev_name(&pdev->dev), __func__);
2781 return ret;
2782}
2783
2784static int sdhci_msm_remove(struct platform_device *pdev)
2785{
2786 struct sdhci_host *host = platform_get_drvdata(pdev);
2787 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2788 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2789 struct sdhci_msm_pltfm_data *pdata = msm_host->pdata;
2790 int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) ==
2791 0xffffffff);
2792
2793 pr_debug("%s: %s\n", dev_name(&pdev->dev), __func__);
Sahitya Tummala5c55b932013-06-20 14:00:18 +05302794 if (!gpio_is_valid(msm_host->pdata->status_gpio))
2795 device_remove_file(&pdev->dev, &msm_host->polling);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302796 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Asutosh Das0ef24812012-12-18 16:14:02 +05302797 sdhci_remove_host(host, dead);
2798 sdhci_pltfm_free(pdev);
Sahitya Tummala581df132013-03-12 14:57:46 +05302799
Asutosh Das0ef24812012-12-18 16:14:02 +05302800 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302801
Asutosh Das0ef24812012-12-18 16:14:02 +05302802 if (pdata->pin_data)
2803 sdhci_msm_setup_gpio(pdata, false);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302804
2805 if (msm_host->msm_bus_vote.client_handle) {
2806 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2807 sdhci_msm_bus_unregister(msm_host);
2808 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302809 return 0;
2810}
2811
2812static const struct of_device_id sdhci_msm_dt_match[] = {
2813 {.compatible = "qcom,sdhci-msm"},
2814};
2815MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match);
2816
2817static struct platform_driver sdhci_msm_driver = {
2818 .probe = sdhci_msm_probe,
2819 .remove = sdhci_msm_remove,
2820 .driver = {
2821 .name = "sdhci_msm",
2822 .owner = THIS_MODULE,
2823 .of_match_table = sdhci_msm_dt_match,
2824 },
2825};
2826
2827module_platform_driver(sdhci_msm_driver);
2828
2829MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Secure Digital Host Controller Interface driver");
2830MODULE_LICENSE("GPL v2");