blob: 0421c857590ec4eccf1317304aab9848847af18c [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
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +053043
44#define CORE_VERSION_MAJOR_MASK 0xF0000000
45#define CORE_VERSION_MAJOR_SHIFT 28
46
Asutosh Das0ef24812012-12-18 16:14:02 +053047#define CORE_HC_MODE 0x78
48#define HC_MODE_EN 0x1
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -070049#define FF_CLK_SW_RST_DIS (1 << 13)
Asutosh Das0ef24812012-12-18 16:14:02 +053050
Sahitya Tummala481fbb02013-08-06 15:22:28 +053051#define CORE_GENERICS 0x70
52#define SWITCHABLE_SIGNALLING_VOL (1 << 29)
53
Asutosh Das0ef24812012-12-18 16:14:02 +053054#define CORE_POWER 0x0
55#define CORE_SW_RST (1 << 7)
56
Sahitya Tummala67717bc2013-08-02 09:21:37 +053057#define CORE_MCI_VERSION 0x050
58#define CORE_TESTBUS_CONFIG 0x0CC
59#define CORE_TESTBUS_ENA (1 << 3)
60#define CORE_SDCC_DEBUG_REG 0x124
61
Asutosh Das0ef24812012-12-18 16:14:02 +053062#define CORE_PWRCTL_STATUS 0xDC
63#define CORE_PWRCTL_MASK 0xE0
64#define CORE_PWRCTL_CLEAR 0xE4
65#define CORE_PWRCTL_CTL 0xE8
66
67#define CORE_PWRCTL_BUS_OFF 0x01
68#define CORE_PWRCTL_BUS_ON (1 << 1)
69#define CORE_PWRCTL_IO_LOW (1 << 2)
70#define CORE_PWRCTL_IO_HIGH (1 << 3)
71
72#define CORE_PWRCTL_BUS_SUCCESS 0x01
73#define CORE_PWRCTL_BUS_FAIL (1 << 1)
74#define CORE_PWRCTL_IO_SUCCESS (1 << 2)
75#define CORE_PWRCTL_IO_FAIL (1 << 3)
76
77#define INT_MASK 0xF
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -070078#define MAX_PHASES 16
79
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -070080#define CORE_DLL_CONFIG 0x100
81#define CORE_CMD_DAT_TRACK_SEL (1 << 0)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -070082#define CORE_DLL_EN (1 << 16)
83#define CORE_CDR_EN (1 << 17)
84#define CORE_CK_OUT_EN (1 << 18)
85#define CORE_CDR_EXT_EN (1 << 19)
86#define CORE_DLL_PDN (1 << 29)
87#define CORE_DLL_RST (1 << 30)
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -070088
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -070089#define CORE_DLL_STATUS 0x108
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -070090#define CORE_DLL_LOCK (1 << 7)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -070091
92#define CORE_VENDOR_SPEC 0x10C
93#define CORE_CLK_PWRSAVE (1 << 1)
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -070094#define CORE_HC_MCLK_SEL_DFLT (2 << 8)
95#define CORE_HC_MCLK_SEL_HS400 (3 << 8)
96#define CORE_HC_MCLK_SEL_MASK (3 << 8)
Asutosh Dase5e9ca62013-07-30 19:08:36 +053097#define CORE_HC_AUTO_CMD21_EN (1 << 6)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -070098#define CORE_IO_PAD_PWR_SWITCH (1 << 16)
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -070099#define CORE_HC_SELECT_IN_EN (1 << 18)
100#define CORE_HC_SELECT_IN_HS400 (6 << 19)
101#define CORE_HC_SELECT_IN_MASK (7 << 19)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700102
Krishna Konda7feab352013-09-17 23:55:40 -0700103#define CORE_VENDOR_SPEC_CAPABILITIES0 0x11C
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +0530104#define CORE_8_BIT_SUPPORT (1 << 18)
105#define CORE_3_3V_SUPPORT (1 << 24)
106#define CORE_3_0V_SUPPORT (1 << 25)
107#define CORE_1_8V_SUPPORT (1 << 26)
Krishna Konda7feab352013-09-17 23:55:40 -0700108#define CORE_SYS_BUS_SUPPORT_64_BIT 28
109
Sahitya Tummala67717bc2013-08-02 09:21:37 +0530110#define CORE_VENDOR_SPEC_ADMA_ERR_ADDR0 0x114
111#define CORE_VENDOR_SPEC_ADMA_ERR_ADDR1 0x118
112
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700113#define CORE_CSR_CDC_CTLR_CFG0 0x130
114#define CORE_SW_TRIG_FULL_CALIB (1 << 16)
115#define CORE_HW_AUTOCAL_ENA (1 << 17)
116
117#define CORE_CSR_CDC_CTLR_CFG1 0x134
118#define CORE_CSR_CDC_CAL_TIMER_CFG0 0x138
119#define CORE_TIMER_ENA (1 << 16)
120
121#define CORE_CSR_CDC_CAL_TIMER_CFG1 0x13C
122#define CORE_CSR_CDC_REFCOUNT_CFG 0x140
123#define CORE_CSR_CDC_COARSE_CAL_CFG 0x144
124#define CORE_CDC_OFFSET_CFG 0x14C
125#define CORE_CSR_CDC_DELAY_CFG 0x150
126#define CORE_CDC_SLAVE_DDA_CFG 0x160
127#define CORE_CSR_CDC_STATUS0 0x164
128#define CORE_CALIBRATION_DONE (1 << 0)
129
130#define CORE_CDC_ERROR_CODE_MASK 0x7000000
131
132#define CORE_CSR_CDC_GEN_CFG 0x178
133#define CORE_CDC_SWITCH_BYPASS_OFF (1 << 0)
134#define CORE_CDC_SWITCH_RC_EN (1 << 1)
135
136#define CORE_DDR_200_CFG 0x184
137#define CORE_CDC_T4_DLY_SEL (1 << 0)
138#define CORE_START_CDC_TRAFFIC (1 << 6)
139
Sahitya Tummala67717bc2013-08-02 09:21:37 +0530140#define CORE_MCI_DATA_CNT 0x30
141#define CORE_MCI_STATUS 0x34
142#define CORE_MCI_FIFO_CNT 0x44
143
144#define CORE_TESTBUS_SEL2_BIT 4
145#define CORE_TESTBUS_SEL2 (1 << CORE_TESTBUS_SEL2_BIT)
146
Asutosh Das648f9d12013-01-10 21:11:04 +0530147/* 8KB descriptors */
148#define SDHCI_MSM_MAX_SEGMENTS (1 << 13)
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +0530149#define SDHCI_MSM_MMC_CLK_GATE_DELAY 200 /* msecs */
Asutosh Das648f9d12013-01-10 21:11:04 +0530150
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700151#define CORE_FREQ_100MHZ (100 * 1000 * 1000)
152
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700153#define INVALID_TUNING_PHASE -1
154
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +0530155#define CORE_VERSION_TARGET_MASK 0x000000FF
156
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700157static const u32 tuning_block_64[] = {
158 0x00FF0FFF, 0xCCC3CCFF, 0xFFCC3CC3, 0xEFFEFFFE,
159 0xDDFFDFFF, 0xFBFFFBFF, 0xFF7FFFBF, 0xEFBDF777,
160 0xF0FFF0FF, 0x3CCCFC0F, 0xCFCC33CC, 0xEEFFEFFF,
161 0xFDFFFDFF, 0xFFBFFFDF, 0xFFF7FFBB, 0xDE7B7FF7
162};
163
164static const u32 tuning_block_128[] = {
165 0xFF00FFFF, 0x0000FFFF, 0xCCCCFFFF, 0xCCCC33CC,
166 0xCC3333CC, 0xFFFFCCCC, 0xFFFFEEFF, 0xFFEEEEFF,
167 0xFFDDFFFF, 0xDDDDFFFF, 0xBBFFFFFF, 0xBBFFFFFF,
168 0xFFFFFFBB, 0xFFFFFF77, 0x77FF7777, 0xFFEEDDBB,
169 0x00FFFFFF, 0x00FFFFFF, 0xCCFFFF00, 0xCC33CCCC,
170 0x3333CCCC, 0xFFCCCCCC, 0xFFEEFFFF, 0xEEEEFFFF,
171 0xDDFFFFFF, 0xDDFFFFFF, 0xFFFFFFDD, 0xFFFFFFBB,
172 0xFFFFBBBB, 0xFFFF77FF, 0xFF7777FF, 0xEEDDBB77
173};
Asutosh Das0ef24812012-12-18 16:14:02 +0530174
Venkat Gopalakrishnan270580a2013-03-11 12:17:57 -0700175static int disable_slots;
176/* root can write, others read */
177module_param(disable_slots, int, S_IRUGO|S_IWUSR);
178
Asutosh Das0ef24812012-12-18 16:14:02 +0530179/* This structure keeps information per regulator */
180struct sdhci_msm_reg_data {
181 /* voltage regulator handle */
182 struct regulator *reg;
183 /* regulator name */
184 const char *name;
185 /* voltage level to be set */
186 u32 low_vol_level;
187 u32 high_vol_level;
188 /* Load values for low power and high power mode */
189 u32 lpm_uA;
190 u32 hpm_uA;
191
192 /* is this regulator enabled? */
193 bool is_enabled;
194 /* is this regulator needs to be always on? */
195 bool is_always_on;
196 /* is low power mode setting required for this regulator? */
197 bool lpm_sup;
198 bool set_voltage_sup;
199};
200
201/*
202 * This structure keeps information for all the
203 * regulators required for a SDCC slot.
204 */
205struct sdhci_msm_slot_reg_data {
206 /* keeps VDD/VCC regulator info */
207 struct sdhci_msm_reg_data *vdd_data;
208 /* keeps VDD IO regulator info */
209 struct sdhci_msm_reg_data *vdd_io_data;
210};
211
212struct sdhci_msm_gpio {
213 u32 no;
214 const char *name;
215 bool is_enabled;
216};
217
218struct sdhci_msm_gpio_data {
219 struct sdhci_msm_gpio *gpio;
220 u8 size;
221};
222
223struct sdhci_msm_pin_data {
224 /*
225 * = 1 if controller pins are using gpios
226 * = 0 if controller has dedicated MSM pads
227 */
228 bool cfg_sts;
229 struct sdhci_msm_gpio_data *gpio_data;
230};
231
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530232struct sdhci_msm_bus_voting_data {
233 struct msm_bus_scale_pdata *bus_pdata;
234 unsigned int *bw_vecs;
235 unsigned int bw_vecs_size;
236};
237
Asutosh Das0ef24812012-12-18 16:14:02 +0530238struct sdhci_msm_pltfm_data {
239 /* Supported UHS-I Modes */
240 u32 caps;
241
242 /* More capabilities */
243 u32 caps2;
244
245 unsigned long mmc_bus_width;
Asutosh Das0ef24812012-12-18 16:14:02 +0530246 struct sdhci_msm_slot_reg_data *vreg_data;
247 bool nonremovable;
248 struct sdhci_msm_pin_data *pin_data;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +0530249 u32 cpu_dma_latency_us;
Sahitya Tummala581df132013-03-12 14:57:46 +0530250 int status_gpio; /* card detection GPIO that is configured as IRQ */
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530251 struct sdhci_msm_bus_voting_data *voting_data;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530252 u32 *sup_clk_table;
253 unsigned char sup_clk_cnt;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530254};
255
256struct sdhci_msm_bus_vote {
257 uint32_t client_handle;
258 uint32_t curr_vote;
259 int min_bw_vote;
260 int max_bw_vote;
261 bool is_max_bw_needed;
262 struct delayed_work vote_work;
263 struct device_attribute max_bus_bw;
Asutosh Das0ef24812012-12-18 16:14:02 +0530264};
265
266struct sdhci_msm_host {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530267 struct platform_device *pdev;
Asutosh Das0ef24812012-12-18 16:14:02 +0530268 void __iomem *core_mem; /* MSM SDCC mapped address */
269 struct clk *clk; /* main SD/MMC bus clock */
270 struct clk *pclk; /* SDHC peripheral bus clock */
271 struct clk *bus_clk; /* SDHC bus voter clock */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700272 struct clk *ff_clk; /* CDC calibration fixed feedback clock */
273 struct clk *sleep_clk; /* CDC calibration sleep clock */
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +0530274 atomic_t clks_on; /* Set if clocks are enabled */
Asutosh Das0ef24812012-12-18 16:14:02 +0530275 struct sdhci_msm_pltfm_data *pdata;
276 struct mmc_host *mmc;
277 struct sdhci_pltfm_data sdhci_msm_pdata;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +0530278 u32 curr_pwr_state;
279 u32 curr_io_level;
280 struct completion pwr_irq_completion;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530281 struct sdhci_msm_bus_vote msm_bus_vote;
Sahitya Tummala5c55b932013-06-20 14:00:18 +0530282 struct device_attribute polling;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530283 u32 clk_rate; /* Keeps track of current clock rate that is set */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700284 bool tuning_done;
285 bool calibration_done;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700286 u8 saved_tuning_phase;
Asutosh Dase5e9ca62013-07-30 19:08:36 +0530287 bool en_auto_cmd21;
288 struct device_attribute auto_cmd21_attr;
Asutosh Das9d7ee2f2013-11-08 12:33:47 +0530289 atomic_t controller_clock;
Asutosh Das0ef24812012-12-18 16:14:02 +0530290};
291
292enum vdd_io_level {
293 /* set vdd_io_data->low_vol_level */
294 VDD_IO_LOW,
295 /* set vdd_io_data->high_vol_level */
296 VDD_IO_HIGH,
297 /*
298 * set whatever there in voltage_level (third argument) of
299 * sdhci_msm_set_vdd_io_vol() function.
300 */
301 VDD_IO_SET_LEVEL,
302};
303
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700304/* MSM platform specific tuning */
305static inline int msm_dll_poll_ck_out_en(struct sdhci_host *host,
306 u8 poll)
307{
308 int rc = 0;
309 u32 wait_cnt = 50;
310 u8 ck_out_en = 0;
311 struct mmc_host *mmc = host->mmc;
312
313 /* poll for CK_OUT_EN bit. max. poll time = 50us */
314 ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) &
315 CORE_CK_OUT_EN);
316
317 while (ck_out_en != poll) {
318 if (--wait_cnt == 0) {
319 pr_err("%s: %s: CK_OUT_EN bit is not %d\n",
320 mmc_hostname(mmc), __func__, poll);
321 rc = -ETIMEDOUT;
322 goto out;
323 }
324 udelay(1);
325
326 ck_out_en = !!(readl_relaxed(host->ioaddr +
327 CORE_DLL_CONFIG) & CORE_CK_OUT_EN);
328 }
329out:
330 return rc;
331}
332
Asutosh Dase5e9ca62013-07-30 19:08:36 +0530333/*
334 * Enable CDR to track changes of DAT lines and adjust sampling
335 * point according to voltage/temperature variations
336 */
337static int msm_enable_cdr_cm_sdc4_dll(struct sdhci_host *host)
338{
339 int rc = 0;
340 u32 config;
341
342 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
343 config |= CORE_CDR_EN;
344 config &= ~(CORE_CDR_EXT_EN | CORE_CK_OUT_EN);
345 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
346
347 rc = msm_dll_poll_ck_out_en(host, 0);
348 if (rc)
349 goto err;
350
351 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) |
352 CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
353
354 rc = msm_dll_poll_ck_out_en(host, 1);
355 if (rc)
356 goto err;
357 goto out;
358err:
359 pr_err("%s: %s: failed\n", mmc_hostname(host->mmc), __func__);
360out:
361 return rc;
362}
363
364static ssize_t store_auto_cmd21(struct device *dev, struct device_attribute
365 *attr, const char *buf, size_t count)
366{
367 struct sdhci_host *host = dev_get_drvdata(dev);
368 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
369 struct sdhci_msm_host *msm_host = pltfm_host->priv;
370 u32 tmp;
371 unsigned long flags;
372
373 if (!kstrtou32(buf, 0, &tmp)) {
374 spin_lock_irqsave(&host->lock, flags);
375 msm_host->en_auto_cmd21 = !!tmp;
376 spin_unlock_irqrestore(&host->lock, flags);
377 }
378 return count;
379}
380
381static ssize_t show_auto_cmd21(struct device *dev,
382 struct device_attribute *attr, char *buf)
383{
384 struct sdhci_host *host = dev_get_drvdata(dev);
385 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
386 struct sdhci_msm_host *msm_host = pltfm_host->priv;
387
388 return snprintf(buf, PAGE_SIZE, "%d\n", msm_host->en_auto_cmd21);
389}
390
391/* MSM auto-tuning handler */
392static int sdhci_msm_config_auto_tuning_cmd(struct sdhci_host *host,
393 bool enable,
394 u32 type)
395{
396 int rc = 0;
397 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
398 struct sdhci_msm_host *msm_host = pltfm_host->priv;
399 u32 val = 0;
400
401 if (!msm_host->en_auto_cmd21)
402 return 0;
403
404 if (type == MMC_SEND_TUNING_BLOCK_HS200)
405 val = CORE_HC_AUTO_CMD21_EN;
406 else
407 return 0;
408
409 if (enable) {
410 rc = msm_enable_cdr_cm_sdc4_dll(host);
411 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
412 val, host->ioaddr + CORE_VENDOR_SPEC);
413 } else {
414 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
415 ~val, host->ioaddr + CORE_VENDOR_SPEC);
416 }
417 return rc;
418}
419
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700420static int msm_config_cm_dll_phase(struct sdhci_host *host, u8 phase)
421{
422 int rc = 0;
423 u8 grey_coded_phase_table[] = {0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4,
424 0xC, 0xD, 0xF, 0xE, 0xA, 0xB, 0x9,
425 0x8};
426 unsigned long flags;
427 u32 config;
428 struct mmc_host *mmc = host->mmc;
429
430 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
431 spin_lock_irqsave(&host->lock, flags);
432
433 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
434 config &= ~(CORE_CDR_EN | CORE_CK_OUT_EN);
435 config |= (CORE_CDR_EXT_EN | CORE_DLL_EN);
436 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
437
438 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '0' */
439 rc = msm_dll_poll_ck_out_en(host, 0);
440 if (rc)
441 goto err_out;
442
443 /*
444 * Write the selected DLL clock output phase (0 ... 15)
445 * to CDR_SELEXT bit field of DLL_CONFIG register.
446 */
447 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
448 & ~(0xF << 20))
449 | (grey_coded_phase_table[phase] << 20)),
450 host->ioaddr + CORE_DLL_CONFIG);
451
452 /* Set CK_OUT_EN bit of DLL_CONFIG register to 1. */
453 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
454 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
455
456 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '1' */
457 rc = msm_dll_poll_ck_out_en(host, 1);
458 if (rc)
459 goto err_out;
460
461 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
462 config |= CORE_CDR_EN;
463 config &= ~CORE_CDR_EXT_EN;
464 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
465 goto out;
466
467err_out:
468 pr_err("%s: %s: Failed to set DLL phase: %d\n",
469 mmc_hostname(mmc), __func__, phase);
470out:
471 spin_unlock_irqrestore(&host->lock, flags);
472 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
473 return rc;
474}
475
476/*
477 * Find out the greatest range of consecuitive selected
478 * DLL clock output phases that can be used as sampling
479 * setting for SD3.0 UHS-I card read operation (in SDR104
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700480 * timing mode) or for eMMC4.5 card read operation (in
481 * HS400/HS200 timing mode).
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700482 * Select the 3/4 of the range and configure the DLL with the
483 * selected DLL clock output phase.
484 */
485
486static int msm_find_most_appropriate_phase(struct sdhci_host *host,
487 u8 *phase_table, u8 total_phases)
488{
489 int ret;
490 u8 ranges[MAX_PHASES][MAX_PHASES] = { {0}, {0} };
491 u8 phases_per_row[MAX_PHASES] = {0};
492 int row_index = 0, col_index = 0, selected_row_index = 0, curr_max = 0;
493 int i, cnt, phase_0_raw_index = 0, phase_15_raw_index = 0;
494 bool phase_0_found = false, phase_15_found = false;
495 struct mmc_host *mmc = host->mmc;
496
497 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
498 if (!total_phases || (total_phases > MAX_PHASES)) {
499 pr_err("%s: %s: invalid argument: total_phases=%d\n",
500 mmc_hostname(mmc), __func__, total_phases);
501 return -EINVAL;
502 }
503
504 for (cnt = 0; cnt < total_phases; cnt++) {
505 ranges[row_index][col_index] = phase_table[cnt];
506 phases_per_row[row_index] += 1;
507 col_index++;
508
509 if ((cnt + 1) == total_phases) {
510 continue;
511 /* check if next phase in phase_table is consecutive or not */
512 } else if ((phase_table[cnt] + 1) != phase_table[cnt + 1]) {
513 row_index++;
514 col_index = 0;
515 }
516 }
517
518 if (row_index >= MAX_PHASES)
519 return -EINVAL;
520
521 /* Check if phase-0 is present in first valid window? */
522 if (!ranges[0][0]) {
523 phase_0_found = true;
524 phase_0_raw_index = 0;
525 /* Check if cycle exist between 2 valid windows */
526 for (cnt = 1; cnt <= row_index; cnt++) {
527 if (phases_per_row[cnt]) {
528 for (i = 0; i < phases_per_row[cnt]; i++) {
529 if (ranges[cnt][i] == 15) {
530 phase_15_found = true;
531 phase_15_raw_index = cnt;
532 break;
533 }
534 }
535 }
536 }
537 }
538
539 /* If 2 valid windows form cycle then merge them as single window */
540 if (phase_0_found && phase_15_found) {
541 /* number of phases in raw where phase 0 is present */
542 u8 phases_0 = phases_per_row[phase_0_raw_index];
543 /* number of phases in raw where phase 15 is present */
544 u8 phases_15 = phases_per_row[phase_15_raw_index];
545
546 if (phases_0 + phases_15 >= MAX_PHASES)
547 /*
548 * If there are more than 1 phase windows then total
549 * number of phases in both the windows should not be
550 * more than or equal to MAX_PHASES.
551 */
552 return -EINVAL;
553
554 /* Merge 2 cyclic windows */
555 i = phases_15;
556 for (cnt = 0; cnt < phases_0; cnt++) {
557 ranges[phase_15_raw_index][i] =
558 ranges[phase_0_raw_index][cnt];
559 if (++i >= MAX_PHASES)
560 break;
561 }
562
563 phases_per_row[phase_0_raw_index] = 0;
564 phases_per_row[phase_15_raw_index] = phases_15 + phases_0;
565 }
566
567 for (cnt = 0; cnt <= row_index; cnt++) {
568 if (phases_per_row[cnt] > curr_max) {
569 curr_max = phases_per_row[cnt];
570 selected_row_index = cnt;
571 }
572 }
573
574 i = ((curr_max * 3) / 4);
575 if (i)
576 i--;
577
578 ret = (int)ranges[selected_row_index][i];
579
580 if (ret >= MAX_PHASES) {
581 ret = -EINVAL;
582 pr_err("%s: %s: invalid phase selected=%d\n",
583 mmc_hostname(mmc), __func__, ret);
584 }
585
586 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
587 return ret;
588}
589
590static inline void msm_cm_dll_set_freq(struct sdhci_host *host)
591{
592 u32 mclk_freq = 0;
593
594 /* Program the MCLK value to MCLK_FREQ bit field */
595 if (host->clock <= 112000000)
596 mclk_freq = 0;
597 else if (host->clock <= 125000000)
598 mclk_freq = 1;
599 else if (host->clock <= 137000000)
600 mclk_freq = 2;
601 else if (host->clock <= 150000000)
602 mclk_freq = 3;
603 else if (host->clock <= 162000000)
604 mclk_freq = 4;
605 else if (host->clock <= 175000000)
606 mclk_freq = 5;
607 else if (host->clock <= 187000000)
608 mclk_freq = 6;
609 else if (host->clock <= 200000000)
610 mclk_freq = 7;
611
612 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
613 & ~(7 << 24)) | (mclk_freq << 24)),
614 host->ioaddr + CORE_DLL_CONFIG);
615}
616
617/* Initialize the DLL (Programmable Delay Line ) */
618static int msm_init_cm_dll(struct sdhci_host *host)
619{
620 struct mmc_host *mmc = host->mmc;
621 int rc = 0;
622 unsigned long flags;
623 u32 wait_cnt;
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530624 bool prev_pwrsave, curr_pwrsave;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700625
626 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
627 spin_lock_irqsave(&host->lock, flags);
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530628 prev_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
629 CORE_CLK_PWRSAVE);
630 curr_pwrsave = prev_pwrsave;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700631 /*
632 * Make sure that clock is always enabled when DLL
633 * tuning is in progress. Keeping PWRSAVE ON may
634 * turn off the clock. So let's disable the PWRSAVE
635 * here and re-enable it once tuning is completed.
636 */
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530637 if (prev_pwrsave) {
638 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
639 & ~CORE_CLK_PWRSAVE),
640 host->ioaddr + CORE_VENDOR_SPEC);
641 curr_pwrsave = false;
642 }
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700643
644 /* Write 1 to DLL_RST bit of DLL_CONFIG register */
645 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
646 | CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
647
648 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
649 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
650 | CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
651 msm_cm_dll_set_freq(host);
652
653 /* Write 0 to DLL_RST bit of DLL_CONFIG register */
654 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
655 & ~CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
656
657 /* Write 0 to DLL_PDN bit of DLL_CONFIG register */
658 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
659 & ~CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
660
661 /* Set DLL_EN bit to 1. */
662 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
663 | CORE_DLL_EN), host->ioaddr + CORE_DLL_CONFIG);
664
665 /* Set CK_OUT_EN bit to 1. */
666 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
667 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
668
669 wait_cnt = 50;
670 /* Wait until DLL_LOCK bit of DLL_STATUS register becomes '1' */
671 while (!(readl_relaxed(host->ioaddr + CORE_DLL_STATUS) &
672 CORE_DLL_LOCK)) {
673 /* max. wait for 50us sec for LOCK bit to be set */
674 if (--wait_cnt == 0) {
675 pr_err("%s: %s: DLL failed to LOCK\n",
676 mmc_hostname(mmc), __func__);
677 rc = -ETIMEDOUT;
678 goto out;
679 }
680 /* wait for 1us before polling again */
681 udelay(1);
682 }
683
684out:
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530685 /* Restore the correct PWRSAVE state */
686 if (prev_pwrsave ^ curr_pwrsave) {
687 u32 reg = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC);
688
689 if (prev_pwrsave)
690 reg |= CORE_CLK_PWRSAVE;
691 else
692 reg &= ~CORE_CLK_PWRSAVE;
693
694 writel_relaxed(reg, host->ioaddr + CORE_VENDOR_SPEC);
695 }
696
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700697 spin_unlock_irqrestore(&host->lock, flags);
698 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
699 return rc;
700}
701
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700702static int sdhci_msm_cdclp533_calibration(struct sdhci_host *host)
703{
704 u32 wait_cnt;
705 int ret = 0;
706 int cdc_err = 0;
707 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
708 struct sdhci_msm_host *msm_host = pltfm_host->priv;
709
710 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
711
712 /*
713 * Retuning in HS400 (DDR mode) will fail, just reset the
714 * tuning block and restore the saved tuning phase.
715 */
716 ret = msm_init_cm_dll(host);
717 if (ret)
718 goto out;
719
720 /* Set the selected phase in delay line hw block */
721 ret = msm_config_cm_dll_phase(host, msm_host->saved_tuning_phase);
722 if (ret)
723 goto out;
724
725 /* Write 1 to CMD_DAT_TRACK_SEL field in DLL_CONFIG */
726 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
727 | CORE_CMD_DAT_TRACK_SEL),
728 host->ioaddr + CORE_DLL_CONFIG);
729
730 /* Write 0 to CDC_T4_DLY_SEL field in VENDOR_SPEC_DDR200_CFG */
731 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
732 & ~CORE_CDC_T4_DLY_SEL),
733 host->ioaddr + CORE_DDR_200_CFG);
734
735 /* Write 0 to CDC_SWITCH_BYPASS_OFF field in CORE_CSR_CDC_GEN_CFG */
736 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG)
737 & ~CORE_CDC_SWITCH_BYPASS_OFF),
738 host->ioaddr + CORE_CSR_CDC_GEN_CFG);
739
740 /* Write 1 to CDC_SWITCH_RC_EN field in CORE_CSR_CDC_GEN_CFG */
741 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG)
742 | CORE_CDC_SWITCH_RC_EN),
743 host->ioaddr + CORE_CSR_CDC_GEN_CFG);
744
745 /* Write 0 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
746 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
747 & ~CORE_START_CDC_TRAFFIC),
748 host->ioaddr + CORE_DDR_200_CFG);
749
750 /*
751 * Perform CDC Register Initialization Sequence
752 *
753 * CORE_CSR_CDC_CTLR_CFG0 0x11800EC
754 * CORE_CSR_CDC_CTLR_CFG1 0x3011111
755 * CORE_CSR_CDC_CAL_TIMER_CFG0 0x1201000
756 * CORE_CSR_CDC_CAL_TIMER_CFG1 0x4
757 * CORE_CSR_CDC_REFCOUNT_CFG 0xCB732020
758 * CORE_CSR_CDC_COARSE_CAL_CFG 0xB19
759 * CORE_CSR_CDC_DELAY_CFG 0x3AC
760 * CORE_CDC_OFFSET_CFG 0x0
761 * CORE_CDC_SLAVE_DDA_CFG 0x16334
762 */
763
764 writel_relaxed(0x11800EC, host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
765 writel_relaxed(0x3011111, host->ioaddr + CORE_CSR_CDC_CTLR_CFG1);
766 writel_relaxed(0x1201000, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
767 writel_relaxed(0x4, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG1);
768 writel_relaxed(0xCB732020, host->ioaddr + CORE_CSR_CDC_REFCOUNT_CFG);
769 writel_relaxed(0xB19, host->ioaddr + CORE_CSR_CDC_COARSE_CAL_CFG);
770 writel_relaxed(0x3AC, host->ioaddr + CORE_CSR_CDC_DELAY_CFG);
771 writel_relaxed(0x0, host->ioaddr + CORE_CDC_OFFSET_CFG);
772 writel_relaxed(0x16334, host->ioaddr + CORE_CDC_SLAVE_DDA_CFG);
773
774 /* CDC HW Calibration */
775
776 /* Write 1 to SW_TRIG_FULL_CALIB field in CORE_CSR_CDC_CTLR_CFG0 */
777 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
778 | CORE_SW_TRIG_FULL_CALIB),
779 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
780
781 /* Write 0 to SW_TRIG_FULL_CALIB field in CORE_CSR_CDC_CTLR_CFG0 */
782 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
783 & ~CORE_SW_TRIG_FULL_CALIB),
784 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
785
786 /* Write 1 to HW_AUTOCAL_ENA field in CORE_CSR_CDC_CTLR_CFG0 */
787 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
788 | CORE_HW_AUTOCAL_ENA),
789 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
790
791 /* Write 1 to TIMER_ENA field in CORE_CSR_CDC_CAL_TIMER_CFG0 */
792 writel_relaxed((readl_relaxed(host->ioaddr +
793 CORE_CSR_CDC_CAL_TIMER_CFG0) | CORE_TIMER_ENA),
794 host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
795
796 mb();
797
798 /* Poll on CALIBRATION_DONE field in CORE_CSR_CDC_STATUS0 to be 1 */
799 wait_cnt = 50;
800 while (!(readl_relaxed(host->ioaddr + CORE_CSR_CDC_STATUS0)
801 & CORE_CALIBRATION_DONE)) {
802 /* max. wait for 50us sec for CALIBRATION_DONE bit to be set */
803 if (--wait_cnt == 0) {
804 pr_err("%s: %s: CDC Calibration was not completed\n",
805 mmc_hostname(host->mmc), __func__);
806 ret = -ETIMEDOUT;
807 goto out;
808 }
809 /* wait for 1us before polling again */
810 udelay(1);
811 }
812
813 /* Verify CDC_ERROR_CODE field in CORE_CSR_CDC_STATUS0 is 0 */
814 cdc_err = readl_relaxed(host->ioaddr + CORE_CSR_CDC_STATUS0)
815 & CORE_CDC_ERROR_CODE_MASK;
816 if (cdc_err) {
817 pr_err("%s: %s: CDC Error Code %d\n",
818 mmc_hostname(host->mmc), __func__, cdc_err);
819 ret = -EINVAL;
820 goto out;
821 }
822
823 /* Write 1 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
824 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
825 | CORE_START_CDC_TRAFFIC),
826 host->ioaddr + CORE_DDR_200_CFG);
827out:
828 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
829 __func__, ret);
830 return ret;
831}
832
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700833int sdhci_msm_execute_tuning(struct sdhci_host *host, u32 opcode)
834{
835 unsigned long flags;
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530836 int tuning_seq_cnt = 3;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700837 u8 phase, *data_buf, tuned_phases[16], tuned_phase_cnt = 0;
838 const u32 *tuning_block_pattern = tuning_block_64;
839 int size = sizeof(tuning_block_64); /* Tuning pattern size in bytes */
840 int rc;
841 struct mmc_host *mmc = host->mmc;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530842 struct mmc_ios ios = host->mmc->ios;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700843 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
844 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530845
846 /*
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700847 * Tuning is required for SDR104, HS200 and HS400 cards and
848 * if clock frequency is greater than 100MHz in these modes.
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530849 */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700850 if (host->clock <= CORE_FREQ_100MHZ ||
851 !((ios.timing == MMC_TIMING_MMC_HS400) ||
852 (ios.timing == MMC_TIMING_MMC_HS200) ||
853 (ios.timing == MMC_TIMING_UHS_SDR104)))
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530854 return 0;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700855
856 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700857
858 /* CDCLP533 HW calibration is only required for HS400 mode*/
859 if (msm_host->tuning_done && !msm_host->calibration_done &&
860 (mmc->ios.timing == MMC_TIMING_MMC_HS400)) {
861 rc = sdhci_msm_cdclp533_calibration(host);
862 spin_lock_irqsave(&host->lock, flags);
863 if (!rc)
864 msm_host->calibration_done = true;
865 spin_unlock_irqrestore(&host->lock, flags);
866 goto out;
867 }
868
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700869 spin_lock_irqsave(&host->lock, flags);
870
871 if ((opcode == MMC_SEND_TUNING_BLOCK_HS200) &&
872 (mmc->ios.bus_width == MMC_BUS_WIDTH_8)) {
873 tuning_block_pattern = tuning_block_128;
874 size = sizeof(tuning_block_128);
875 }
876 spin_unlock_irqrestore(&host->lock, flags);
877
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700878 data_buf = kmalloc(size, GFP_KERNEL);
879 if (!data_buf) {
880 rc = -ENOMEM;
881 goto out;
882 }
883
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530884retry:
885 /* first of all reset the tuning block */
886 rc = msm_init_cm_dll(host);
887 if (rc)
888 goto kfree;
889
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700890 phase = 0;
891 do {
892 struct mmc_command cmd = {0};
893 struct mmc_data data = {0};
894 struct mmc_request mrq = {
895 .cmd = &cmd,
896 .data = &data
897 };
898 struct scatterlist sg;
899
900 /* set the phase in delay line hw block */
901 rc = msm_config_cm_dll_phase(host, phase);
902 if (rc)
903 goto kfree;
904
905 cmd.opcode = opcode;
906 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
907
908 data.blksz = size;
909 data.blocks = 1;
910 data.flags = MMC_DATA_READ;
911 data.timeout_ns = 1000 * 1000 * 1000; /* 1 sec */
912
913 data.sg = &sg;
914 data.sg_len = 1;
915 sg_init_one(&sg, data_buf, size);
916 memset(data_buf, 0, size);
917 mmc_wait_for_req(mmc, &mrq);
918
919 if (!cmd.error && !data.error &&
920 !memcmp(data_buf, tuning_block_pattern, size)) {
921 /* tuning is successful at this tuning point */
922 tuned_phases[tuned_phase_cnt++] = phase;
923 pr_debug("%s: %s: found good phase = %d\n",
924 mmc_hostname(mmc), __func__, phase);
925 }
926 } while (++phase < 16);
927
928 if (tuned_phase_cnt) {
929 rc = msm_find_most_appropriate_phase(host, tuned_phases,
930 tuned_phase_cnt);
931 if (rc < 0)
932 goto kfree;
933 else
934 phase = (u8)rc;
935
936 /*
937 * Finally set the selected phase in delay
938 * line hw block.
939 */
940 rc = msm_config_cm_dll_phase(host, phase);
941 if (rc)
942 goto kfree;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700943 msm_host->saved_tuning_phase = phase;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700944 pr_debug("%s: %s: finally setting the tuning phase to %d\n",
945 mmc_hostname(mmc), __func__, phase);
946 } else {
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530947 if (--tuning_seq_cnt)
948 goto retry;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700949 /* tuning failed */
950 pr_err("%s: %s: no tuning point found\n",
951 mmc_hostname(mmc), __func__);
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530952 rc = -EIO;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700953 }
954
955kfree:
956 kfree(data_buf);
957out:
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700958 spin_lock_irqsave(&host->lock, flags);
959 if (!rc)
960 msm_host->tuning_done = true;
961 spin_unlock_irqrestore(&host->lock, flags);
962 pr_debug("%s: Exit %s, err(%d)\n", mmc_hostname(mmc), __func__, rc);
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700963 return rc;
964}
965
Asutosh Das0ef24812012-12-18 16:14:02 +0530966static int sdhci_msm_setup_gpio(struct sdhci_msm_pltfm_data *pdata, bool enable)
967{
968 struct sdhci_msm_gpio_data *curr;
969 int i, ret = 0;
970
971 curr = pdata->pin_data->gpio_data;
972 for (i = 0; i < curr->size; i++) {
973 if (!gpio_is_valid(curr->gpio[i].no)) {
974 ret = -EINVAL;
975 pr_err("%s: Invalid gpio = %d\n", __func__,
976 curr->gpio[i].no);
977 goto free_gpios;
978 }
979 if (enable) {
980 ret = gpio_request(curr->gpio[i].no,
981 curr->gpio[i].name);
982 if (ret) {
983 pr_err("%s: gpio_request(%d, %s) failed %d\n",
984 __func__, curr->gpio[i].no,
985 curr->gpio[i].name, ret);
986 goto free_gpios;
987 }
988 curr->gpio[i].is_enabled = true;
989 } else {
990 gpio_free(curr->gpio[i].no);
991 curr->gpio[i].is_enabled = false;
992 }
993 }
994 return ret;
995
996free_gpios:
997 for (i--; i >= 0; i--) {
998 gpio_free(curr->gpio[i].no);
999 curr->gpio[i].is_enabled = false;
1000 }
1001 return ret;
1002}
1003
1004static int sdhci_msm_setup_pins(struct sdhci_msm_pltfm_data *pdata, bool enable)
1005{
1006 int ret = 0;
1007
1008 if (!pdata->pin_data || (pdata->pin_data->cfg_sts == enable))
1009 return 0;
1010
1011 ret = sdhci_msm_setup_gpio(pdata, enable);
1012 if (!ret)
1013 pdata->pin_data->cfg_sts = enable;
1014
1015 return ret;
1016}
1017
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301018static int sdhci_msm_dt_get_array(struct device *dev, const char *prop_name,
1019 u32 **out, int *len, u32 size)
1020{
1021 int ret = 0;
1022 struct device_node *np = dev->of_node;
1023 size_t sz;
1024 u32 *arr = NULL;
1025
1026 if (!of_get_property(np, prop_name, len)) {
1027 ret = -EINVAL;
1028 goto out;
1029 }
1030 sz = *len = *len / sizeof(*arr);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07001031 if (sz <= 0 || (size > 0 && (sz > size))) {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301032 dev_err(dev, "%s invalid size\n", prop_name);
1033 ret = -EINVAL;
1034 goto out;
1035 }
1036
1037 arr = devm_kzalloc(dev, sz * sizeof(*arr), GFP_KERNEL);
1038 if (!arr) {
1039 dev_err(dev, "%s failed allocating memory\n", prop_name);
1040 ret = -ENOMEM;
1041 goto out;
1042 }
1043
1044 ret = of_property_read_u32_array(np, prop_name, arr, sz);
1045 if (ret < 0) {
1046 dev_err(dev, "%s failed reading array %d\n", prop_name, ret);
1047 goto out;
1048 }
1049 *out = arr;
1050out:
1051 if (ret)
1052 *len = 0;
1053 return ret;
1054}
1055
Asutosh Das0ef24812012-12-18 16:14:02 +05301056#define MAX_PROP_SIZE 32
1057static int sdhci_msm_dt_parse_vreg_info(struct device *dev,
1058 struct sdhci_msm_reg_data **vreg_data, const char *vreg_name)
1059{
1060 int len, ret = 0;
1061 const __be32 *prop;
1062 char prop_name[MAX_PROP_SIZE];
1063 struct sdhci_msm_reg_data *vreg;
1064 struct device_node *np = dev->of_node;
1065
1066 snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", vreg_name);
1067 if (!of_parse_phandle(np, prop_name, 0)) {
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301068 dev_info(dev, "No vreg data found for %s\n", vreg_name);
Asutosh Das0ef24812012-12-18 16:14:02 +05301069 return ret;
1070 }
1071
1072 vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
1073 if (!vreg) {
1074 dev_err(dev, "No memory for vreg: %s\n", vreg_name);
1075 ret = -ENOMEM;
1076 return ret;
1077 }
1078
1079 vreg->name = vreg_name;
1080
1081 snprintf(prop_name, MAX_PROP_SIZE,
1082 "qcom,%s-always-on", vreg_name);
1083 if (of_get_property(np, prop_name, NULL))
1084 vreg->is_always_on = true;
1085
1086 snprintf(prop_name, MAX_PROP_SIZE,
1087 "qcom,%s-lpm-sup", vreg_name);
1088 if (of_get_property(np, prop_name, NULL))
1089 vreg->lpm_sup = true;
1090
1091 snprintf(prop_name, MAX_PROP_SIZE,
1092 "qcom,%s-voltage-level", vreg_name);
1093 prop = of_get_property(np, prop_name, &len);
1094 if (!prop || (len != (2 * sizeof(__be32)))) {
1095 dev_warn(dev, "%s %s property\n",
1096 prop ? "invalid format" : "no", prop_name);
1097 } else {
1098 vreg->low_vol_level = be32_to_cpup(&prop[0]);
1099 vreg->high_vol_level = be32_to_cpup(&prop[1]);
1100 }
1101
1102 snprintf(prop_name, MAX_PROP_SIZE,
1103 "qcom,%s-current-level", vreg_name);
1104 prop = of_get_property(np, prop_name, &len);
1105 if (!prop || (len != (2 * sizeof(__be32)))) {
1106 dev_warn(dev, "%s %s property\n",
1107 prop ? "invalid format" : "no", prop_name);
1108 } else {
1109 vreg->lpm_uA = be32_to_cpup(&prop[0]);
1110 vreg->hpm_uA = be32_to_cpup(&prop[1]);
1111 }
1112
1113 *vreg_data = vreg;
1114 dev_dbg(dev, "%s: %s %s vol=[%d %d]uV, curr=[%d %d]uA\n",
1115 vreg->name, vreg->is_always_on ? "always_on," : "",
1116 vreg->lpm_sup ? "lpm_sup," : "", vreg->low_vol_level,
1117 vreg->high_vol_level, vreg->lpm_uA, vreg->hpm_uA);
1118
1119 return ret;
1120}
1121
1122#define GPIO_NAME_MAX_LEN 32
1123static int sdhci_msm_dt_parse_gpio_info(struct device *dev,
1124 struct sdhci_msm_pltfm_data *pdata)
1125{
1126 int ret = 0, cnt, i;
1127 struct sdhci_msm_pin_data *pin_data;
1128 struct device_node *np = dev->of_node;
1129
1130 pin_data = devm_kzalloc(dev, sizeof(*pin_data), GFP_KERNEL);
1131 if (!pin_data) {
1132 dev_err(dev, "No memory for pin_data\n");
1133 ret = -ENOMEM;
1134 goto out;
1135 }
1136
1137 cnt = of_gpio_count(np);
1138 if (cnt > 0) {
1139 pin_data->gpio_data = devm_kzalloc(dev,
1140 sizeof(struct sdhci_msm_gpio_data), GFP_KERNEL);
1141 if (!pin_data->gpio_data) {
1142 dev_err(dev, "No memory for gpio_data\n");
1143 ret = -ENOMEM;
1144 goto out;
1145 }
1146 pin_data->gpio_data->size = cnt;
1147 pin_data->gpio_data->gpio = devm_kzalloc(dev, cnt *
1148 sizeof(struct sdhci_msm_gpio), GFP_KERNEL);
1149
1150 if (!pin_data->gpio_data->gpio) {
1151 dev_err(dev, "No memory for gpio\n");
1152 ret = -ENOMEM;
1153 goto out;
1154 }
1155
1156 for (i = 0; i < cnt; i++) {
1157 const char *name = NULL;
1158 char result[GPIO_NAME_MAX_LEN];
1159 pin_data->gpio_data->gpio[i].no = of_get_gpio(np, i);
1160 of_property_read_string_index(np,
1161 "qcom,gpio-names", i, &name);
1162
1163 snprintf(result, GPIO_NAME_MAX_LEN, "%s-%s",
1164 dev_name(dev), name ? name : "?");
1165 pin_data->gpio_data->gpio[i].name = result;
1166 dev_dbg(dev, "%s: gpio[%s] = %d\n", __func__,
1167 pin_data->gpio_data->gpio[i].name,
1168 pin_data->gpio_data->gpio[i].no);
1169 pdata->pin_data = pin_data;
1170 }
1171 }
1172
1173out:
1174 if (ret)
1175 dev_err(dev, "%s failed with err %d\n", __func__, ret);
1176 return ret;
1177}
1178
1179/* Parse platform data */
1180static struct sdhci_msm_pltfm_data *sdhci_msm_populate_pdata(struct device *dev)
1181{
1182 struct sdhci_msm_pltfm_data *pdata = NULL;
1183 struct device_node *np = dev->of_node;
1184 u32 bus_width = 0;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05301185 u32 cpu_dma_latency;
Asutosh Das0ef24812012-12-18 16:14:02 +05301186 int len, i;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301187 int clk_table_len;
1188 u32 *clk_table = NULL;
Sujit Reddy Thumma1958d3d2013-06-03 09:54:32 +05301189 enum of_gpio_flags flags = OF_GPIO_ACTIVE_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05301190
1191 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1192 if (!pdata) {
1193 dev_err(dev, "failed to allocate memory for platform data\n");
1194 goto out;
1195 }
1196
Sujit Reddy Thumma1958d3d2013-06-03 09:54:32 +05301197 pdata->status_gpio = of_get_named_gpio_flags(np, "cd-gpios", 0, &flags);
1198 if (gpio_is_valid(pdata->status_gpio) & !(flags & OF_GPIO_ACTIVE_LOW))
1199 pdata->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
Sahitya Tummala581df132013-03-12 14:57:46 +05301200
Asutosh Das0ef24812012-12-18 16:14:02 +05301201 of_property_read_u32(np, "qcom,bus-width", &bus_width);
1202 if (bus_width == 8)
1203 pdata->mmc_bus_width = MMC_CAP_8_BIT_DATA;
1204 else if (bus_width == 4)
1205 pdata->mmc_bus_width = MMC_CAP_4_BIT_DATA;
1206 else {
1207 dev_notice(dev, "invalid bus-width, default to 1-bit mode\n");
1208 pdata->mmc_bus_width = 0;
1209 }
1210
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05301211 if (!of_property_read_u32(np, "qcom,cpu-dma-latency-us",
1212 &cpu_dma_latency))
1213 pdata->cpu_dma_latency_us = cpu_dma_latency;
1214
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301215 if (sdhci_msm_dt_get_array(dev, "qcom,clk-rates",
1216 &clk_table, &clk_table_len, 0)) {
1217 dev_err(dev, "failed parsing supported clock rates\n");
1218 goto out;
1219 }
1220 if (!clk_table || !clk_table_len) {
1221 dev_err(dev, "Invalid clock table\n");
1222 goto out;
1223 }
1224 pdata->sup_clk_table = clk_table;
1225 pdata->sup_clk_cnt = clk_table_len;
1226
Asutosh Das0ef24812012-12-18 16:14:02 +05301227 pdata->vreg_data = devm_kzalloc(dev, sizeof(struct
1228 sdhci_msm_slot_reg_data),
1229 GFP_KERNEL);
1230 if (!pdata->vreg_data) {
1231 dev_err(dev, "failed to allocate memory for vreg data\n");
1232 goto out;
1233 }
1234
1235 if (sdhci_msm_dt_parse_vreg_info(dev, &pdata->vreg_data->vdd_data,
1236 "vdd")) {
1237 dev_err(dev, "failed parsing vdd data\n");
1238 goto out;
1239 }
1240 if (sdhci_msm_dt_parse_vreg_info(dev,
1241 &pdata->vreg_data->vdd_io_data,
1242 "vdd-io")) {
1243 dev_err(dev, "failed parsing vdd-io data\n");
1244 goto out;
1245 }
1246
1247 if (sdhci_msm_dt_parse_gpio_info(dev, pdata)) {
1248 dev_err(dev, "failed parsing gpio data\n");
1249 goto out;
1250 }
1251
Asutosh Das0ef24812012-12-18 16:14:02 +05301252 len = of_property_count_strings(np, "qcom,bus-speed-mode");
1253
1254 for (i = 0; i < len; i++) {
1255 const char *name = NULL;
1256
1257 of_property_read_string_index(np,
1258 "qcom,bus-speed-mode", i, &name);
1259 if (!name)
1260 continue;
1261
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07001262 if (!strncmp(name, "HS400_1p8v", sizeof("HS400_1p8v")))
1263 pdata->caps2 |= MMC_CAP2_HS400_1_8V;
1264 else if (!strncmp(name, "HS400_1p2v", sizeof("HS400_1p2v")))
1265 pdata->caps2 |= MMC_CAP2_HS400_1_2V;
1266 else if (!strncmp(name, "HS200_1p8v", sizeof("HS200_1p8v")))
Asutosh Das0ef24812012-12-18 16:14:02 +05301267 pdata->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
1268 else if (!strncmp(name, "HS200_1p2v", sizeof("HS200_1p2v")))
1269 pdata->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
1270 else if (!strncmp(name, "DDR_1p8v", sizeof("DDR_1p8v")))
1271 pdata->caps |= MMC_CAP_1_8V_DDR
1272 | MMC_CAP_UHS_DDR50;
1273 else if (!strncmp(name, "DDR_1p2v", sizeof("DDR_1p2v")))
1274 pdata->caps |= MMC_CAP_1_2V_DDR
1275 | MMC_CAP_UHS_DDR50;
1276 }
1277
1278 if (of_get_property(np, "qcom,nonremovable", NULL))
1279 pdata->nonremovable = true;
1280
1281 return pdata;
1282out:
1283 return NULL;
1284}
1285
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301286/* Returns required bandwidth in Bytes per Sec */
1287static unsigned int sdhci_get_bw_required(struct sdhci_host *host,
1288 struct mmc_ios *ios)
1289{
Sahitya Tummala2886c922013-04-03 18:03:31 +05301290 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1291 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1292
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301293 unsigned int bw;
1294
Sahitya Tummala2886c922013-04-03 18:03:31 +05301295 bw = msm_host->clk_rate;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301296 /*
1297 * For DDR mode, SDCC controller clock will be at
1298 * the double rate than the actual clock that goes to card.
1299 */
1300 if (ios->bus_width == MMC_BUS_WIDTH_4)
1301 bw /= 2;
1302 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1303 bw /= 8;
1304
1305 return bw;
1306}
1307
1308static int sdhci_msm_bus_get_vote_for_bw(struct sdhci_msm_host *host,
1309 unsigned int bw)
1310{
1311 unsigned int *table = host->pdata->voting_data->bw_vecs;
1312 unsigned int size = host->pdata->voting_data->bw_vecs_size;
1313 int i;
1314
1315 if (host->msm_bus_vote.is_max_bw_needed && bw)
1316 return host->msm_bus_vote.max_bw_vote;
1317
1318 for (i = 0; i < size; i++) {
1319 if (bw <= table[i])
1320 break;
1321 }
1322
1323 if (i && (i == size))
1324 i--;
1325
1326 return i;
1327}
1328
1329/*
1330 * This function must be called with host lock acquired.
1331 * Caller of this function should also ensure that msm bus client
1332 * handle is not null.
1333 */
1334static inline int sdhci_msm_bus_set_vote(struct sdhci_msm_host *msm_host,
1335 int vote,
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301336 unsigned long *flags)
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301337{
1338 struct sdhci_host *host = platform_get_drvdata(msm_host->pdev);
1339 int rc = 0;
1340
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301341 BUG_ON(!flags);
1342
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301343 if (vote != msm_host->msm_bus_vote.curr_vote) {
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301344 spin_unlock_irqrestore(&host->lock, *flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301345 rc = msm_bus_scale_client_update_request(
1346 msm_host->msm_bus_vote.client_handle, vote);
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301347 spin_lock_irqsave(&host->lock, *flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301348 if (rc) {
1349 pr_err("%s: msm_bus_scale_client_update_request() failed: bus_client_handle=0x%x, vote=%d, err=%d\n",
1350 mmc_hostname(host->mmc),
1351 msm_host->msm_bus_vote.client_handle, vote, rc);
1352 goto out;
1353 }
1354 msm_host->msm_bus_vote.curr_vote = vote;
1355 }
1356out:
1357 return rc;
1358}
1359
1360/*
1361 * Internal work. Work to set 0 bandwidth for msm bus.
1362 */
1363static void sdhci_msm_bus_work(struct work_struct *work)
1364{
1365 struct sdhci_msm_host *msm_host;
1366 struct sdhci_host *host;
1367 unsigned long flags;
1368
1369 msm_host = container_of(work, struct sdhci_msm_host,
1370 msm_bus_vote.vote_work.work);
1371 host = platform_get_drvdata(msm_host->pdev);
1372
1373 if (!msm_host->msm_bus_vote.client_handle)
1374 return;
1375
1376 spin_lock_irqsave(&host->lock, flags);
1377 /* don't vote for 0 bandwidth if any request is in progress */
1378 if (!host->mrq) {
1379 sdhci_msm_bus_set_vote(msm_host,
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301380 msm_host->msm_bus_vote.min_bw_vote, &flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301381 } else
1382 pr_warning("%s: %s: Transfer in progress. skipping bus voting to 0 bandwidth\n",
1383 mmc_hostname(host->mmc), __func__);
1384 spin_unlock_irqrestore(&host->lock, flags);
1385}
1386
1387/*
1388 * This function cancels any scheduled delayed work and sets the bus
1389 * vote based on bw (bandwidth) argument.
1390 */
1391static void sdhci_msm_bus_cancel_work_and_set_vote(struct sdhci_host *host,
1392 unsigned int bw)
1393{
1394 int vote;
1395 unsigned long flags;
1396 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1397 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1398
1399 cancel_delayed_work_sync(&msm_host->msm_bus_vote.vote_work);
1400 spin_lock_irqsave(&host->lock, flags);
1401 vote = sdhci_msm_bus_get_vote_for_bw(msm_host, bw);
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301402 sdhci_msm_bus_set_vote(msm_host, vote, &flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301403 spin_unlock_irqrestore(&host->lock, flags);
1404}
1405
1406#define MSM_MMC_BUS_VOTING_DELAY 200 /* msecs */
1407
1408/* This function queues a work which will set the bandwidth requiement to 0 */
1409static void sdhci_msm_bus_queue_work(struct sdhci_host *host)
1410{
1411 unsigned long flags;
1412 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1413 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1414
1415 spin_lock_irqsave(&host->lock, flags);
1416 if (msm_host->msm_bus_vote.min_bw_vote !=
1417 msm_host->msm_bus_vote.curr_vote)
1418 queue_delayed_work(system_wq,
1419 &msm_host->msm_bus_vote.vote_work,
1420 msecs_to_jiffies(MSM_MMC_BUS_VOTING_DELAY));
1421 spin_unlock_irqrestore(&host->lock, flags);
1422}
1423
1424static int sdhci_msm_bus_register(struct sdhci_msm_host *host,
1425 struct platform_device *pdev)
1426{
1427 int rc = 0;
1428 struct msm_bus_scale_pdata *bus_pdata;
1429
1430 struct sdhci_msm_bus_voting_data *data;
1431 struct device *dev = &pdev->dev;
1432
1433 data = devm_kzalloc(dev,
1434 sizeof(struct sdhci_msm_bus_voting_data), GFP_KERNEL);
1435 if (!data) {
1436 dev_err(&pdev->dev,
1437 "%s: failed to allocate memory\n", __func__);
1438 rc = -ENOMEM;
1439 goto out;
1440 }
1441 data->bus_pdata = msm_bus_cl_get_pdata(pdev);
1442 if (data->bus_pdata) {
1443 rc = sdhci_msm_dt_get_array(dev, "qcom,bus-bw-vectors-bps",
1444 &data->bw_vecs, &data->bw_vecs_size, 0);
1445 if (rc) {
1446 dev_err(&pdev->dev,
1447 "%s: Failed to get bus-bw-vectors-bps\n",
1448 __func__);
1449 goto out;
1450 }
1451 host->pdata->voting_data = data;
1452 }
1453 if (host->pdata->voting_data &&
1454 host->pdata->voting_data->bus_pdata &&
1455 host->pdata->voting_data->bw_vecs &&
1456 host->pdata->voting_data->bw_vecs_size) {
1457
1458 bus_pdata = host->pdata->voting_data->bus_pdata;
1459 host->msm_bus_vote.client_handle =
1460 msm_bus_scale_register_client(bus_pdata);
1461 if (!host->msm_bus_vote.client_handle) {
1462 dev_err(&pdev->dev, "msm_bus_scale_register_client()\n");
1463 rc = -EFAULT;
1464 goto out;
1465 }
1466 /* cache the vote index for minimum and maximum bandwidth */
1467 host->msm_bus_vote.min_bw_vote =
1468 sdhci_msm_bus_get_vote_for_bw(host, 0);
1469 host->msm_bus_vote.max_bw_vote =
1470 sdhci_msm_bus_get_vote_for_bw(host, UINT_MAX);
1471 } else {
1472 devm_kfree(dev, data);
1473 }
1474
1475out:
1476 return rc;
1477}
1478
1479static void sdhci_msm_bus_unregister(struct sdhci_msm_host *host)
1480{
1481 if (host->msm_bus_vote.client_handle)
1482 msm_bus_scale_unregister_client(
1483 host->msm_bus_vote.client_handle);
1484}
1485
1486static void sdhci_msm_bus_voting(struct sdhci_host *host, u32 enable)
1487{
1488 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1489 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1490 struct mmc_ios *ios = &host->mmc->ios;
1491 unsigned int bw;
1492
1493 if (!msm_host->msm_bus_vote.client_handle)
1494 return;
1495
1496 bw = sdhci_get_bw_required(host, ios);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301497 if (enable) {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301498 sdhci_msm_bus_cancel_work_and_set_vote(host, bw);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301499 } else {
1500 /*
1501 * If clock gating is enabled, then remove the vote
1502 * immediately because clocks will be disabled only
1503 * after SDHCI_MSM_MMC_CLK_GATE_DELAY and thus no
1504 * additional delay is required to remove the bus vote.
1505 */
1506#ifdef CONFIG_MMC_CLKGATE
1507 if (host->mmc->clkgate_delay)
1508 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
1509 else
1510#endif
1511 sdhci_msm_bus_queue_work(host);
1512 }
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301513}
1514
Asutosh Das0ef24812012-12-18 16:14:02 +05301515/* Regulator utility functions */
1516static int sdhci_msm_vreg_init_reg(struct device *dev,
1517 struct sdhci_msm_reg_data *vreg)
1518{
1519 int ret = 0;
1520
1521 /* check if regulator is already initialized? */
1522 if (vreg->reg)
1523 goto out;
1524
1525 /* Get the regulator handle */
1526 vreg->reg = devm_regulator_get(dev, vreg->name);
1527 if (IS_ERR(vreg->reg)) {
1528 ret = PTR_ERR(vreg->reg);
1529 pr_err("%s: devm_regulator_get(%s) failed. ret=%d\n",
1530 __func__, vreg->name, ret);
1531 goto out;
1532 }
1533
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301534 if (regulator_count_voltages(vreg->reg) > 0) {
1535 vreg->set_voltage_sup = true;
1536 /* sanity check */
1537 if (!vreg->high_vol_level || !vreg->hpm_uA) {
1538 pr_err("%s: %s invalid constraints specified\n",
1539 __func__, vreg->name);
1540 ret = -EINVAL;
1541 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301542 }
1543
1544out:
1545 return ret;
1546}
1547
1548static void sdhci_msm_vreg_deinit_reg(struct sdhci_msm_reg_data *vreg)
1549{
1550 if (vreg->reg)
1551 devm_regulator_put(vreg->reg);
1552}
1553
1554static int sdhci_msm_vreg_set_optimum_mode(struct sdhci_msm_reg_data
1555 *vreg, int uA_load)
1556{
1557 int ret = 0;
1558
1559 /*
1560 * regulators that do not support regulator_set_voltage also
1561 * do not support regulator_set_optimum_mode
1562 */
1563 if (vreg->set_voltage_sup) {
1564 ret = regulator_set_load(vreg->reg, uA_load);
1565 if (ret < 0)
1566 pr_err("%s: regulator_set_load(reg=%s,uA_load=%d) failed. ret=%d\n",
1567 __func__, vreg->name, uA_load, ret);
1568 else
1569 /*
1570 * regulator_set_load() can return non zero
1571 * value even for success case.
1572 */
1573 ret = 0;
1574 }
1575 return ret;
1576}
1577
1578static int sdhci_msm_vreg_set_voltage(struct sdhci_msm_reg_data *vreg,
1579 int min_uV, int max_uV)
1580{
1581 int ret = 0;
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301582 if (vreg->set_voltage_sup) {
1583 ret = regulator_set_voltage(vreg->reg, min_uV, max_uV);
1584 if (ret) {
1585 pr_err("%s: regulator_set_voltage(%s)failed. min_uV=%d,max_uV=%d,ret=%d\n",
Asutosh Das0ef24812012-12-18 16:14:02 +05301586 __func__, vreg->name, min_uV, max_uV, ret);
1587 }
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301588 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301589
1590 return ret;
1591}
1592
1593static int sdhci_msm_vreg_enable(struct sdhci_msm_reg_data *vreg)
1594{
1595 int ret = 0;
1596
1597 /* Put regulator in HPM (high power mode) */
1598 ret = sdhci_msm_vreg_set_optimum_mode(vreg, vreg->hpm_uA);
1599 if (ret < 0)
1600 return ret;
1601
1602 if (!vreg->is_enabled) {
1603 /* Set voltage level */
1604 ret = sdhci_msm_vreg_set_voltage(vreg, vreg->high_vol_level,
1605 vreg->high_vol_level);
1606 if (ret)
1607 return ret;
1608 }
1609 ret = regulator_enable(vreg->reg);
1610 if (ret) {
1611 pr_err("%s: regulator_enable(%s) failed. ret=%d\n",
1612 __func__, vreg->name, ret);
1613 return ret;
1614 }
1615 vreg->is_enabled = true;
1616 return ret;
1617}
1618
1619static int sdhci_msm_vreg_disable(struct sdhci_msm_reg_data *vreg)
1620{
1621 int ret = 0;
1622
1623 /* Never disable regulator marked as always_on */
1624 if (vreg->is_enabled && !vreg->is_always_on) {
1625 ret = regulator_disable(vreg->reg);
1626 if (ret) {
1627 pr_err("%s: regulator_disable(%s) failed. ret=%d\n",
1628 __func__, vreg->name, ret);
1629 goto out;
1630 }
1631 vreg->is_enabled = false;
1632
1633 ret = sdhci_msm_vreg_set_optimum_mode(vreg, 0);
1634 if (ret < 0)
1635 goto out;
1636
1637 /* Set min. voltage level to 0 */
1638 ret = sdhci_msm_vreg_set_voltage(vreg, 0, vreg->high_vol_level);
1639 if (ret)
1640 goto out;
1641 } else if (vreg->is_enabled && vreg->is_always_on) {
1642 if (vreg->lpm_sup) {
1643 /* Put always_on regulator in LPM (low power mode) */
1644 ret = sdhci_msm_vreg_set_optimum_mode(vreg,
1645 vreg->lpm_uA);
1646 if (ret < 0)
1647 goto out;
1648 }
1649 }
1650out:
1651 return ret;
1652}
1653
1654static int sdhci_msm_setup_vreg(struct sdhci_msm_pltfm_data *pdata,
1655 bool enable, bool is_init)
1656{
1657 int ret = 0, i;
1658 struct sdhci_msm_slot_reg_data *curr_slot;
1659 struct sdhci_msm_reg_data *vreg_table[2];
1660
1661 curr_slot = pdata->vreg_data;
1662 if (!curr_slot) {
1663 pr_debug("%s: vreg info unavailable,assuming the slot is powered by always on domain\n",
1664 __func__);
1665 goto out;
1666 }
1667
1668 vreg_table[0] = curr_slot->vdd_data;
1669 vreg_table[1] = curr_slot->vdd_io_data;
1670
1671 for (i = 0; i < ARRAY_SIZE(vreg_table); i++) {
1672 if (vreg_table[i]) {
1673 if (enable)
1674 ret = sdhci_msm_vreg_enable(vreg_table[i]);
1675 else
1676 ret = sdhci_msm_vreg_disable(vreg_table[i]);
1677 if (ret)
1678 goto out;
1679 }
1680 }
1681out:
1682 return ret;
1683}
1684
1685/*
1686 * Reset vreg by ensuring it is off during probe. A call
1687 * to enable vreg is needed to balance disable vreg
1688 */
1689static int sdhci_msm_vreg_reset(struct sdhci_msm_pltfm_data *pdata)
1690{
1691 int ret;
1692
1693 ret = sdhci_msm_setup_vreg(pdata, 1, true);
1694 if (ret)
1695 return ret;
1696 ret = sdhci_msm_setup_vreg(pdata, 0, true);
1697 return ret;
1698}
1699
1700/* This init function should be called only once for each SDHC slot */
1701static int sdhci_msm_vreg_init(struct device *dev,
1702 struct sdhci_msm_pltfm_data *pdata,
1703 bool is_init)
1704{
1705 int ret = 0;
1706 struct sdhci_msm_slot_reg_data *curr_slot;
1707 struct sdhci_msm_reg_data *curr_vdd_reg, *curr_vdd_io_reg;
1708
1709 curr_slot = pdata->vreg_data;
1710 if (!curr_slot)
1711 goto out;
1712
1713 curr_vdd_reg = curr_slot->vdd_data;
1714 curr_vdd_io_reg = curr_slot->vdd_io_data;
1715
1716 if (!is_init)
1717 /* Deregister all regulators from regulator framework */
1718 goto vdd_io_reg_deinit;
1719
1720 /*
1721 * Get the regulator handle from voltage regulator framework
1722 * and then try to set the voltage level for the regulator
1723 */
1724 if (curr_vdd_reg) {
1725 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_reg);
1726 if (ret)
1727 goto out;
1728 }
1729 if (curr_vdd_io_reg) {
1730 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_io_reg);
1731 if (ret)
1732 goto vdd_reg_deinit;
1733 }
1734 ret = sdhci_msm_vreg_reset(pdata);
1735 if (ret)
1736 dev_err(dev, "vreg reset failed (%d)\n", ret);
1737 goto out;
1738
1739vdd_io_reg_deinit:
1740 if (curr_vdd_io_reg)
1741 sdhci_msm_vreg_deinit_reg(curr_vdd_io_reg);
1742vdd_reg_deinit:
1743 if (curr_vdd_reg)
1744 sdhci_msm_vreg_deinit_reg(curr_vdd_reg);
1745out:
1746 return ret;
1747}
1748
1749
1750static int sdhci_msm_set_vdd_io_vol(struct sdhci_msm_pltfm_data *pdata,
1751 enum vdd_io_level level,
1752 unsigned int voltage_level)
1753{
1754 int ret = 0;
1755 int set_level;
1756 struct sdhci_msm_reg_data *vdd_io_reg;
1757
1758 if (!pdata->vreg_data)
1759 return ret;
1760
1761 vdd_io_reg = pdata->vreg_data->vdd_io_data;
1762 if (vdd_io_reg && vdd_io_reg->is_enabled) {
1763 switch (level) {
1764 case VDD_IO_LOW:
1765 set_level = vdd_io_reg->low_vol_level;
1766 break;
1767 case VDD_IO_HIGH:
1768 set_level = vdd_io_reg->high_vol_level;
1769 break;
1770 case VDD_IO_SET_LEVEL:
1771 set_level = voltage_level;
1772 break;
1773 default:
1774 pr_err("%s: invalid argument level = %d",
1775 __func__, level);
1776 ret = -EINVAL;
1777 return ret;
1778 }
1779 ret = sdhci_msm_vreg_set_voltage(vdd_io_reg, set_level,
1780 set_level);
1781 }
1782 return ret;
1783}
1784
1785static irqreturn_t sdhci_msm_pwr_irq(int irq, void *data)
1786{
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001787 struct sdhci_host *host = (struct sdhci_host *)data;
1788 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1789 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Das0ef24812012-12-18 16:14:02 +05301790 u8 irq_status = 0;
1791 u8 irq_ack = 0;
1792 int ret = 0;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301793 int pwr_state = 0, io_level = 0;
1794 unsigned long flags;
Asutosh Das0ef24812012-12-18 16:14:02 +05301795
1796 irq_status = readb_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
1797 pr_debug("%s: Received IRQ(%d), status=0x%x\n",
1798 mmc_hostname(msm_host->mmc), irq, irq_status);
1799
1800 /* Clear the interrupt */
1801 writeb_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
1802 /*
1803 * SDHC has core_mem and hc_mem device memory and these memory
1804 * addresses do not fall within 1KB region. Hence, any update to
1805 * core_mem address space would require an mb() to ensure this gets
1806 * completed before its next update to registers within hc_mem.
1807 */
1808 mb();
1809
1810 /* Handle BUS ON/OFF*/
1811 if (irq_status & CORE_PWRCTL_BUS_ON) {
1812 ret = sdhci_msm_setup_vreg(msm_host->pdata, true, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301813 if (!ret) {
Asutosh Das0ef24812012-12-18 16:14:02 +05301814 ret = sdhci_msm_setup_pins(msm_host->pdata, true);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301815 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
1816 VDD_IO_HIGH, 0);
1817 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301818 if (ret)
1819 irq_ack |= CORE_PWRCTL_BUS_FAIL;
1820 else
1821 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301822
1823 pwr_state = REQ_BUS_ON;
1824 io_level = REQ_IO_HIGH;
Asutosh Das0ef24812012-12-18 16:14:02 +05301825 }
1826 if (irq_status & CORE_PWRCTL_BUS_OFF) {
1827 ret = sdhci_msm_setup_vreg(msm_host->pdata, false, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301828 if (!ret) {
Asutosh Das0ef24812012-12-18 16:14:02 +05301829 ret = sdhci_msm_setup_pins(msm_host->pdata, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301830 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
1831 VDD_IO_LOW, 0);
1832 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301833 if (ret)
1834 irq_ack |= CORE_PWRCTL_BUS_FAIL;
1835 else
1836 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301837
1838 pwr_state = REQ_BUS_OFF;
1839 io_level = REQ_IO_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05301840 }
1841 /* Handle IO LOW/HIGH */
1842 if (irq_status & CORE_PWRCTL_IO_LOW) {
1843 /* Switch voltage Low */
1844 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_LOW, 0);
1845 if (ret)
1846 irq_ack |= CORE_PWRCTL_IO_FAIL;
1847 else
1848 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301849
1850 io_level = REQ_IO_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05301851 }
1852 if (irq_status & CORE_PWRCTL_IO_HIGH) {
1853 /* Switch voltage High */
1854 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_HIGH, 0);
1855 if (ret)
1856 irq_ack |= CORE_PWRCTL_IO_FAIL;
1857 else
1858 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301859
1860 io_level = REQ_IO_HIGH;
Asutosh Das0ef24812012-12-18 16:14:02 +05301861 }
1862
1863 /* ACK status to the core */
1864 writeb_relaxed(irq_ack, (msm_host->core_mem + CORE_PWRCTL_CTL));
1865 /*
1866 * SDHC has core_mem and hc_mem device memory and these memory
1867 * addresses do not fall within 1KB region. Hence, any update to
1868 * core_mem address space would require an mb() to ensure this gets
1869 * completed before its next update to registers within hc_mem.
1870 */
1871 mb();
1872
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301873 if (io_level & REQ_IO_HIGH)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001874 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
1875 ~CORE_IO_PAD_PWR_SWITCH),
1876 host->ioaddr + CORE_VENDOR_SPEC);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301877 else if (io_level & REQ_IO_LOW)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001878 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
1879 CORE_IO_PAD_PWR_SWITCH),
1880 host->ioaddr + CORE_VENDOR_SPEC);
1881 mb();
1882
Asutosh Das0ef24812012-12-18 16:14:02 +05301883 pr_debug("%s: Handled IRQ(%d), ret=%d, ack=0x%x\n",
1884 mmc_hostname(msm_host->mmc), irq, ret, irq_ack);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301885 spin_lock_irqsave(&host->lock, flags);
1886 if (pwr_state)
1887 msm_host->curr_pwr_state = pwr_state;
1888 if (io_level)
1889 msm_host->curr_io_level = io_level;
1890 complete(&msm_host->pwr_irq_completion);
1891 spin_unlock_irqrestore(&host->lock, flags);
1892
Asutosh Das0ef24812012-12-18 16:14:02 +05301893 return IRQ_HANDLED;
1894}
1895
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301896static ssize_t
Sahitya Tummala5c55b932013-06-20 14:00:18 +05301897show_polling(struct device *dev, struct device_attribute *attr, char *buf)
1898{
1899 struct sdhci_host *host = dev_get_drvdata(dev);
1900 int poll;
1901 unsigned long flags;
1902
1903 spin_lock_irqsave(&host->lock, flags);
1904 poll = !!(host->mmc->caps & MMC_CAP_NEEDS_POLL);
1905 spin_unlock_irqrestore(&host->lock, flags);
1906
1907 return snprintf(buf, PAGE_SIZE, "%d\n", poll);
1908}
1909
1910static ssize_t
1911store_polling(struct device *dev, struct device_attribute *attr,
1912 const char *buf, size_t count)
1913{
1914 struct sdhci_host *host = dev_get_drvdata(dev);
1915 int value;
1916 unsigned long flags;
1917
1918 if (!kstrtou32(buf, 0, &value)) {
1919 spin_lock_irqsave(&host->lock, flags);
1920 if (value) {
1921 host->mmc->caps |= MMC_CAP_NEEDS_POLL;
1922 mmc_detect_change(host->mmc, 0);
1923 } else {
1924 host->mmc->caps &= ~MMC_CAP_NEEDS_POLL;
1925 }
1926 spin_unlock_irqrestore(&host->lock, flags);
1927 }
1928 return count;
1929}
1930
1931static ssize_t
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301932show_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
1933 char *buf)
1934{
1935 struct sdhci_host *host = dev_get_drvdata(dev);
1936 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1937 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1938
1939 return snprintf(buf, PAGE_SIZE, "%u\n",
1940 msm_host->msm_bus_vote.is_max_bw_needed);
1941}
1942
1943static ssize_t
1944store_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
1945 const char *buf, size_t count)
1946{
1947 struct sdhci_host *host = dev_get_drvdata(dev);
1948 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1949 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1950 uint32_t value;
1951 unsigned long flags;
1952
1953 if (!kstrtou32(buf, 0, &value)) {
1954 spin_lock_irqsave(&host->lock, flags);
1955 msm_host->msm_bus_vote.is_max_bw_needed = !!value;
1956 spin_unlock_irqrestore(&host->lock, flags);
1957 }
1958 return count;
1959}
1960
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301961static void sdhci_msm_check_power_status(struct sdhci_host *host, u32 req_type)
Asutosh Das0ef24812012-12-18 16:14:02 +05301962{
1963 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1964 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301965 unsigned long flags;
1966 bool done = false;
Sahitya Tummala481fbb02013-08-06 15:22:28 +05301967 u32 io_sig_sts;
Asutosh Das0ef24812012-12-18 16:14:02 +05301968
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301969 spin_lock_irqsave(&host->lock, flags);
1970 pr_debug("%s: %s: request %d curr_pwr_state %x curr_io_level %x\n",
1971 mmc_hostname(host->mmc), __func__, req_type,
1972 msm_host->curr_pwr_state, msm_host->curr_io_level);
Sahitya Tummala481fbb02013-08-06 15:22:28 +05301973 io_sig_sts = readl_relaxed(msm_host->core_mem + CORE_GENERICS);
1974 /*
1975 * The IRQ for request type IO High/Low will be generated when -
1976 * 1. SWITCHABLE_SIGNALLING_VOL is enabled in HW.
1977 * 2. If 1 is true and when there is a state change in 1.8V enable
1978 * bit (bit 3) of SDHCI_HOST_CONTROL2 register. The reset state of
1979 * that bit is 0 which indicates 3.3V IO voltage. So, when MMC core
1980 * layer tries to set it to 3.3V before card detection happens, the
1981 * IRQ doesn't get triggered as there is no state change in this bit.
1982 * The driver already handles this case by changing the IO voltage
1983 * level to high as part of controller power up sequence. Hence, check
1984 * for host->pwr to handle a case where IO voltage high request is
1985 * issued even before controller power up.
1986 */
1987 if (req_type & (REQ_IO_HIGH | REQ_IO_LOW)) {
1988 if (!(io_sig_sts & SWITCHABLE_SIGNALLING_VOL) ||
1989 ((req_type & REQ_IO_HIGH) && !host->pwr)) {
1990 pr_debug("%s: do not wait for power IRQ that never comes\n",
1991 mmc_hostname(host->mmc));
1992 spin_unlock_irqrestore(&host->lock, flags);
1993 return;
1994 }
1995 }
1996
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301997 if ((req_type & msm_host->curr_pwr_state) ||
1998 (req_type & msm_host->curr_io_level))
1999 done = true;
2000 spin_unlock_irqrestore(&host->lock, flags);
Asutosh Das0ef24812012-12-18 16:14:02 +05302001
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302002 /*
2003 * This is needed here to hanlde a case where IRQ gets
2004 * triggered even before this function is called so that
2005 * x->done counter of completion gets reset. Otherwise,
2006 * next call to wait_for_completion returns immediately
2007 * without actually waiting for the IRQ to be handled.
2008 */
2009 if (done)
2010 init_completion(&msm_host->pwr_irq_completion);
2011 else
2012 wait_for_completion(&msm_host->pwr_irq_completion);
2013
2014 pr_debug("%s: %s: request %d done\n", mmc_hostname(host->mmc),
2015 __func__, req_type);
Asutosh Das0ef24812012-12-18 16:14:02 +05302016}
2017
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002018static void sdhci_msm_toggle_cdr(struct sdhci_host *host, bool enable)
2019{
2020 if (enable)
2021 writel_relaxed((readl_relaxed(host->ioaddr +
2022 CORE_DLL_CONFIG) | CORE_CDR_EN),
2023 host->ioaddr + CORE_DLL_CONFIG);
2024 else
2025 writel_relaxed((readl_relaxed(host->ioaddr +
2026 CORE_DLL_CONFIG) & ~CORE_CDR_EN),
2027 host->ioaddr + CORE_DLL_CONFIG);
2028}
2029
Asutosh Das648f9d12013-01-10 21:11:04 +05302030static unsigned int sdhci_msm_max_segs(void)
2031{
2032 return SDHCI_MSM_MAX_SEGMENTS;
2033}
2034
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302035static unsigned int sdhci_msm_get_min_clock(struct sdhci_host *host)
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302036{
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302037 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2038 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302039
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302040 return msm_host->pdata->sup_clk_table[0];
2041}
2042
2043static unsigned int sdhci_msm_get_max_clock(struct sdhci_host *host)
2044{
2045 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2046 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2047 int max_clk_index = msm_host->pdata->sup_clk_cnt;
2048
2049 return msm_host->pdata->sup_clk_table[max_clk_index - 1];
2050}
2051
2052static unsigned int sdhci_msm_get_sup_clk_rate(struct sdhci_host *host,
2053 u32 req_clk)
2054{
2055 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2056 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2057 unsigned int sel_clk = -1;
2058 unsigned char cnt;
2059
2060 if (req_clk < sdhci_msm_get_min_clock(host)) {
2061 sel_clk = sdhci_msm_get_min_clock(host);
2062 return sel_clk;
2063 }
2064
2065 for (cnt = 0; cnt < msm_host->pdata->sup_clk_cnt; cnt++) {
2066 if (msm_host->pdata->sup_clk_table[cnt] > req_clk) {
2067 break;
2068 } else if (msm_host->pdata->sup_clk_table[cnt] == req_clk) {
2069 sel_clk = msm_host->pdata->sup_clk_table[cnt];
2070 break;
2071 } else {
2072 sel_clk = msm_host->pdata->sup_clk_table[cnt];
2073 }
2074 }
2075 return sel_clk;
2076}
2077
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302078static int sdhci_msm_enable_controller_clock(struct sdhci_host *host)
2079{
2080 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2081 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2082 int rc = 0;
2083
2084 if (atomic_read(&msm_host->controller_clock))
2085 return 0;
2086
2087 sdhci_msm_bus_voting(host, 1);
2088
2089 if (!IS_ERR(msm_host->pclk)) {
2090 rc = clk_prepare_enable(msm_host->pclk);
2091 if (rc) {
2092 pr_err("%s: %s: failed to enable the pclk with error %d\n",
2093 mmc_hostname(host->mmc), __func__, rc);
2094 goto remove_vote;
2095 }
2096 }
2097
2098 rc = clk_prepare_enable(msm_host->clk);
2099 if (rc) {
2100 pr_err("%s: %s: failed to enable the host-clk with error %d\n",
2101 mmc_hostname(host->mmc), __func__, rc);
2102 goto disable_pclk;
2103 }
2104
2105 atomic_set(&msm_host->controller_clock, 1);
2106 pr_debug("%s: %s: enabled controller clock\n",
2107 mmc_hostname(host->mmc), __func__);
2108 goto out;
2109
2110disable_pclk:
2111 if (!IS_ERR(msm_host->pclk))
2112 clk_disable_unprepare(msm_host->pclk);
2113remove_vote:
2114 if (msm_host->msm_bus_vote.client_handle)
2115 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2116out:
2117 return rc;
2118}
2119
2120
2121
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302122static int sdhci_msm_prepare_clocks(struct sdhci_host *host, bool enable)
2123{
2124 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2125 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2126 int rc = 0;
2127
2128 if (enable && !atomic_read(&msm_host->clks_on)) {
2129 pr_debug("%s: request to enable clocks\n",
2130 mmc_hostname(host->mmc));
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302131
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302132 /*
2133 * The bus-width or the clock rate might have changed
2134 * after controller clocks are enbaled, update bus vote
2135 * in such case.
2136 */
2137 if (atomic_read(&msm_host->controller_clock))
2138 sdhci_msm_bus_voting(host, 1);
2139
2140 rc = sdhci_msm_enable_controller_clock(host);
2141 if (rc)
2142 goto remove_vote;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302143
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302144 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
2145 rc = clk_prepare_enable(msm_host->bus_clk);
2146 if (rc) {
2147 pr_err("%s: %s: failed to enable the bus-clock with error %d\n",
2148 mmc_hostname(host->mmc), __func__, rc);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302149 goto disable_controller_clk;
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302150 }
2151 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002152 if (!IS_ERR(msm_host->ff_clk)) {
2153 rc = clk_prepare_enable(msm_host->ff_clk);
2154 if (rc) {
2155 pr_err("%s: %s: failed to enable the ff_clk with error %d\n",
2156 mmc_hostname(host->mmc), __func__, rc);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302157 goto disable_bus_clk;
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002158 }
2159 }
2160 if (!IS_ERR(msm_host->sleep_clk)) {
2161 rc = clk_prepare_enable(msm_host->sleep_clk);
2162 if (rc) {
2163 pr_err("%s: %s: failed to enable the sleep_clk with error %d\n",
2164 mmc_hostname(host->mmc), __func__, rc);
2165 goto disable_ff_clk;
2166 }
2167 }
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302168 mb();
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302169
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302170 } else if (!enable && atomic_read(&msm_host->clks_on)) {
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302171 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
2172 mb();
Sahitya Tummaladc182982013-08-20 15:32:09 +05302173 /*
2174 * During 1.8V signal switching the clock source must
2175 * still be ON as it requires accessing SDHC
2176 * registers (SDHCi host control2 register bit 3 must
2177 * be written and polled after stopping the SDCLK).
2178 */
2179 if (host->mmc->card_clock_off)
2180 return 0;
2181 pr_debug("%s: request to disable clocks\n",
2182 mmc_hostname(host->mmc));
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002183 if (!IS_ERR_OR_NULL(msm_host->sleep_clk))
2184 clk_disable_unprepare(msm_host->sleep_clk);
2185 if (!IS_ERR_OR_NULL(msm_host->ff_clk))
2186 clk_disable_unprepare(msm_host->ff_clk);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302187 clk_disable_unprepare(msm_host->clk);
2188 if (!IS_ERR(msm_host->pclk))
2189 clk_disable_unprepare(msm_host->pclk);
2190 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2191 clk_disable_unprepare(msm_host->bus_clk);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302192
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302193 atomic_set(&msm_host->controller_clock, 0);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302194 sdhci_msm_bus_voting(host, 0);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302195 }
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302196 atomic_set(&msm_host->clks_on, enable);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302197 goto out;
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002198disable_ff_clk:
2199 if (!IS_ERR_OR_NULL(msm_host->ff_clk))
2200 clk_disable_unprepare(msm_host->ff_clk);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302201disable_bus_clk:
2202 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2203 clk_disable_unprepare(msm_host->bus_clk);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302204disable_controller_clk:
2205 if (!IS_ERR_OR_NULL(msm_host->clk))
2206 clk_disable_unprepare(msm_host->clk);
2207 if (!IS_ERR_OR_NULL(msm_host->pclk))
2208 clk_disable_unprepare(msm_host->pclk);
2209 atomic_set(&msm_host->controller_clock, 0);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302210remove_vote:
2211 if (msm_host->msm_bus_vote.client_handle)
2212 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302213out:
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302214 return rc;
2215}
2216
2217static void sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock)
2218{
2219 int rc;
2220 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2221 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2222 struct mmc_ios curr_ios = host->mmc->ios;
2223 u32 sup_clock, ddr_clock;
Sahitya Tummala043744a2013-06-24 09:55:33 +05302224 bool curr_pwrsave;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302225
2226 if (!clock) {
2227 sdhci_msm_prepare_clocks(host, false);
2228 host->clock = clock;
2229 goto out;
2230 }
2231
2232 rc = sdhci_msm_prepare_clocks(host, true);
2233 if (rc)
2234 goto out;
2235
Sahitya Tummala043744a2013-06-24 09:55:33 +05302236 curr_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2237 CORE_CLK_PWRSAVE);
Sahitya Tummalae000b242013-08-29 16:21:08 +05302238 if ((clock > 400000) &&
Sahitya Tummala043744a2013-06-24 09:55:33 +05302239 !curr_pwrsave && mmc_host_may_gate_card(host->mmc->card))
2240 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2241 | CORE_CLK_PWRSAVE,
2242 host->ioaddr + CORE_VENDOR_SPEC);
2243 /*
2244 * Disable pwrsave for a newly added card if doesn't allow clock
2245 * gating.
2246 */
2247 else if (curr_pwrsave && !mmc_host_may_gate_card(host->mmc->card))
2248 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2249 & ~CORE_CLK_PWRSAVE,
2250 host->ioaddr + CORE_VENDOR_SPEC);
2251
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302252 sup_clock = sdhci_msm_get_sup_clk_rate(host, clock);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002253 if ((curr_ios.timing == MMC_TIMING_UHS_DDR50) ||
2254 (curr_ios.timing == MMC_TIMING_MMC_HS400)) {
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302255 /*
2256 * The SDHC requires internal clock frequency to be double the
2257 * actual clock that will be set for DDR mode. The controller
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002258 * uses the faster clock(100/400MHz) for some of its parts and
2259 * send the actual required clock (50/200MHz) to the card.
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302260 */
2261 ddr_clock = clock * 2;
2262 sup_clock = sdhci_msm_get_sup_clk_rate(host,
2263 ddr_clock);
2264 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002265
2266 /*
2267 * In general all timing modes are controlled via UHS mode select in
2268 * Host Control2 register. eMMC specific HS200/HS400 doesn't have
2269 * their respective modes defined here, hence we use these values.
2270 *
2271 * HS200 - SDR104 (Since they both are equivalent in functionality)
2272 * HS400 - This involves multiple configurations
2273 * Initially SDR104 - when tuning is required as HS200
2274 * Then when switching to DDR @ 400MHz (HS400) we use
2275 * the vendor specific HC_SELECT_IN to control the mode.
2276 *
2277 * In addition to controlling the modes we also need to select the
2278 * correct input clock for DLL depending on the mode.
2279 *
2280 * HS400 - divided clock (free running MCLK/2)
2281 * All other modes - default (free running MCLK)
2282 */
2283 if (curr_ios.timing == MMC_TIMING_MMC_HS400) {
2284 /* Select the divided clock (free running MCLK/2) */
2285 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2286 & ~CORE_HC_MCLK_SEL_MASK)
2287 | CORE_HC_MCLK_SEL_HS400),
2288 host->ioaddr + CORE_VENDOR_SPEC);
2289 /*
2290 * Select HS400 mode using the HC_SELECT_IN from VENDOR SPEC
2291 * register
2292 */
2293 if (msm_host->tuning_done && !msm_host->calibration_done) {
2294 /*
2295 * Write 0x6 to HC_SELECT_IN and 1 to HC_SELECT_IN_EN
2296 * field in VENDOR_SPEC_FUNC
2297 */
2298 writel_relaxed((readl_relaxed(host->ioaddr + \
2299 CORE_VENDOR_SPEC)
2300 | CORE_HC_SELECT_IN_HS400
2301 | CORE_HC_SELECT_IN_EN),
2302 host->ioaddr + CORE_VENDOR_SPEC);
2303 }
2304 } else {
2305 /* Select the default clock (free running MCLK) */
2306 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2307 & ~CORE_HC_MCLK_SEL_MASK)
2308 | CORE_HC_MCLK_SEL_DFLT),
2309 host->ioaddr + CORE_VENDOR_SPEC);
2310
2311 /*
2312 * Disable HC_SELECT_IN to be able to use the UHS mode select
2313 * configuration from Host Control2 register for all other
2314 * modes.
2315 *
2316 * Write 0 to HC_SELECT_IN and HC_SELECT_IN_EN field
2317 * in VENDOR_SPEC_FUNC
2318 */
2319 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2320 & ~CORE_HC_SELECT_IN_EN
2321 & ~CORE_HC_SELECT_IN_MASK),
2322 host->ioaddr + CORE_VENDOR_SPEC);
2323 }
2324 mb();
2325
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302326 if (sup_clock != msm_host->clk_rate) {
2327 pr_debug("%s: %s: setting clk rate to %u\n",
2328 mmc_hostname(host->mmc), __func__, sup_clock);
2329 rc = clk_set_rate(msm_host->clk, sup_clock);
2330 if (rc) {
2331 pr_err("%s: %s: Failed to set rate %u for host-clk : %d\n",
2332 mmc_hostname(host->mmc), __func__,
2333 sup_clock, rc);
2334 goto out;
2335 }
2336 msm_host->clk_rate = sup_clock;
2337 host->clock = clock;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302338 /*
2339 * Update the bus vote in case of frequency change due to
2340 * clock scaling.
2341 */
2342 sdhci_msm_bus_voting(host, 1);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302343 }
2344out:
2345 sdhci_set_clock(host, clock);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302346}
2347
Sahitya Tummala14613432013-03-21 11:13:25 +05302348static void sdhci_msm_set_uhs_signaling(struct sdhci_host *host,
2349 unsigned int uhs)
2350{
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002351 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2352 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala14613432013-03-21 11:13:25 +05302353 u16 ctrl_2;
2354
2355 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2356 /* Select Bus Speed Mode for host */
2357 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002358 if (uhs == MMC_TIMING_MMC_HS400)
2359 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2360 else if (uhs == MMC_TIMING_MMC_HS200)
Sahitya Tummala14613432013-03-21 11:13:25 +05302361 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2362 else if (uhs == MMC_TIMING_UHS_SDR12)
2363 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
2364 else if (uhs == MMC_TIMING_UHS_SDR25)
2365 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
2366 else if (uhs == MMC_TIMING_UHS_SDR50)
2367 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
2368 else if (uhs == MMC_TIMING_UHS_SDR104)
2369 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2370 else if (uhs == MMC_TIMING_UHS_DDR50)
2371 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302372 /*
2373 * When clock frquency is less than 100MHz, the feedback clock must be
2374 * provided and DLL must not be used so that tuning can be skipped. To
2375 * provide feedback clock, the mode selection can be any value less
2376 * than 3'b011 in bits [2:0] of HOST CONTROL2 register.
2377 */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002378 if (host->clock <= CORE_FREQ_100MHZ) {
2379 if ((uhs == MMC_TIMING_MMC_HS400) ||
2380 (uhs == MMC_TIMING_MMC_HS200) ||
2381 (uhs == MMC_TIMING_UHS_SDR104))
2382 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302383
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002384 /*
2385 * Make sure DLL is disabled when not required
2386 *
2387 * Write 1 to DLL_RST bit of DLL_CONFIG register
2388 */
2389 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
2390 | CORE_DLL_RST),
2391 host->ioaddr + CORE_DLL_CONFIG);
2392
2393 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
2394 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
2395 | CORE_DLL_PDN),
2396 host->ioaddr + CORE_DLL_CONFIG);
2397 mb();
2398
2399 /*
2400 * The DLL needs to be restored and CDCLP533 recalibrated
2401 * when the clock frequency is set back to 400MHz.
2402 */
2403 msm_host->calibration_done = false;
2404 }
2405
2406 pr_debug("%s: %s-clock:%u uhs mode:%u ctrl_2:0x%x\n",
2407 mmc_hostname(host->mmc), __func__, host->clock, uhs, ctrl_2);
Sahitya Tummala14613432013-03-21 11:13:25 +05302408 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
2409
2410}
2411
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302412#define MAX_TEST_BUS 20
2413
2414void sdhci_msm_dump_vendor_regs(struct sdhci_host *host)
2415{
2416 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2417 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2418 int tbsel, tbsel2;
2419 int i, index = 0;
2420 u32 test_bus_val = 0;
2421 u32 debug_reg[MAX_TEST_BUS] = {0};
2422
2423 pr_info("----------- VENDOR REGISTER DUMP -----------\n");
2424 pr_info("Data cnt: 0x%08x | Fifo cnt: 0x%08x | Int sts: 0x%08x\n",
2425 readl_relaxed(msm_host->core_mem + CORE_MCI_DATA_CNT),
2426 readl_relaxed(msm_host->core_mem + CORE_MCI_FIFO_CNT),
2427 readl_relaxed(msm_host->core_mem + CORE_MCI_STATUS));
2428 pr_info("DLL cfg: 0x%08x | DLL sts: 0x%08x | SDCC ver: 0x%08x\n",
2429 readl_relaxed(host->ioaddr + CORE_DLL_CONFIG),
2430 readl_relaxed(host->ioaddr + CORE_DLL_STATUS),
2431 readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION));
2432 pr_info("Vndr func: 0x%08x | Vndr adma err : addr0: 0x%08x addr1: 0x%08x\n",
2433 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC),
2434 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_ADMA_ERR_ADDR0),
2435 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_ADMA_ERR_ADDR1));
2436
2437 /*
2438 * tbsel indicates [2:0] bits and tbsel2 indicates [7:4] bits
2439 * of CORE_TESTBUS_CONFIG register.
2440 *
2441 * To select test bus 0 to 7 use tbsel and to select any test bus
2442 * above 7 use (tbsel2 | tbsel) to get the test bus number. For eg,
2443 * to select test bus 14, write 0x1E to CORE_TESTBUS_CONFIG register
2444 * i.e., tbsel2[7:4] = 0001, tbsel[2:0] = 110.
2445 */
2446 for (tbsel2 = 0; tbsel2 < 3; tbsel2++) {
2447 for (tbsel = 0; tbsel < 8; tbsel++) {
2448 if (index >= MAX_TEST_BUS)
2449 break;
2450 test_bus_val = (tbsel2 << CORE_TESTBUS_SEL2_BIT) |
2451 tbsel | CORE_TESTBUS_ENA;
2452 writel_relaxed(test_bus_val,
2453 msm_host->core_mem + CORE_TESTBUS_CONFIG);
2454 debug_reg[index++] = readl_relaxed(msm_host->core_mem +
2455 CORE_SDCC_DEBUG_REG);
2456 }
2457 }
2458 for (i = 0; i < MAX_TEST_BUS; i = i + 4)
2459 pr_info(" Test bus[%d to %d]: 0x%08x 0x%08x 0x%08x 0x%08x\n",
2460 i, i + 3, debug_reg[i], debug_reg[i+1],
2461 debug_reg[i+2], debug_reg[i+3]);
2462 /* Disable test bus */
2463 writel_relaxed(~CORE_TESTBUS_ENA, msm_host->core_mem +
2464 CORE_TESTBUS_CONFIG);
2465}
2466
Asutosh Das0ef24812012-12-18 16:14:02 +05302467static struct sdhci_ops sdhci_msm_ops = {
Sahitya Tummala14613432013-03-21 11:13:25 +05302468 .set_uhs_signaling = sdhci_msm_set_uhs_signaling,
Asutosh Das0ef24812012-12-18 16:14:02 +05302469 .check_power_status = sdhci_msm_check_power_status,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002470 .platform_execute_tuning = sdhci_msm_execute_tuning,
2471 .toggle_cdr = sdhci_msm_toggle_cdr,
Asutosh Das648f9d12013-01-10 21:11:04 +05302472 .get_max_segments = sdhci_msm_max_segs,
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302473 .set_clock = sdhci_msm_set_clock,
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302474 .get_min_clock = sdhci_msm_get_min_clock,
2475 .get_max_clock = sdhci_msm_get_max_clock,
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302476 .dump_vendor_regs = sdhci_msm_dump_vendor_regs,
Asutosh Dase5e9ca62013-07-30 19:08:36 +05302477 .config_auto_tuning_cmd = sdhci_msm_config_auto_tuning_cmd,
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302478 .enable_controller_clock = sdhci_msm_enable_controller_clock,
Asutosh Das0ef24812012-12-18 16:14:02 +05302479};
2480
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302481static void sdhci_set_default_hw_caps(struct sdhci_msm_host *msm_host,
2482 struct sdhci_host *host)
2483{
2484 u32 version, caps;
2485 u16 minor;
2486 u8 major;
2487
2488 version = readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION);
2489 major = (version & CORE_VERSION_MAJOR_MASK) >>
2490 CORE_VERSION_MAJOR_SHIFT;
2491 minor = version & CORE_VERSION_TARGET_MASK;
2492
2493 /*
2494 * Starting with SDCC 5 controller (core major version = 1)
Pratibhasagar Vada47992013-12-09 20:42:32 +05302495 * controller won't advertise 3.0v and 8-bit features except for
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302496 * some targets.
2497 */
2498 if (major >= 1 && minor != 0x11 && minor != 0x12) {
2499 caps = CORE_3_0V_SUPPORT;
Pratibhasagar Vada47992013-12-09 20:42:32 +05302500 if (msm_host->pdata->mmc_bus_width == MMC_CAP_8_BIT_DATA)
2501 caps |= CORE_8_BIT_SUPPORT;
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302502 writel_relaxed(
2503 (readl_relaxed(host->ioaddr + SDHCI_CAPABILITIES) |
2504 caps), host->ioaddr + CORE_VENDOR_SPEC_CAPABILITIES0);
2505 }
2506}
2507
Asutosh Das0ef24812012-12-18 16:14:02 +05302508static int sdhci_msm_probe(struct platform_device *pdev)
2509{
2510 struct sdhci_host *host;
2511 struct sdhci_pltfm_host *pltfm_host;
2512 struct sdhci_msm_host *msm_host;
2513 struct resource *core_memres = NULL;
2514 int ret = 0, pwr_irq = 0, dead = 0;
Stephen Boyd8dce5c62013-04-24 14:19:46 -07002515 u16 host_version;
Subhash Jadavani28137342013-05-14 17:46:43 +05302516 u32 pwr, irq_status, irq_ctl;
Asutosh Das0ef24812012-12-18 16:14:02 +05302517
2518 pr_debug("%s: Enter %s\n", dev_name(&pdev->dev), __func__);
2519 msm_host = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_msm_host),
2520 GFP_KERNEL);
2521 if (!msm_host) {
2522 ret = -ENOMEM;
2523 goto out;
2524 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302525
2526 msm_host->sdhci_msm_pdata.ops = &sdhci_msm_ops;
2527 host = sdhci_pltfm_init(pdev, &msm_host->sdhci_msm_pdata, 0);
2528 if (IS_ERR(host)) {
2529 ret = PTR_ERR(host);
2530 goto out;
2531 }
2532
2533 pltfm_host = sdhci_priv(host);
2534 pltfm_host->priv = msm_host;
2535 msm_host->mmc = host->mmc;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302536 msm_host->pdev = pdev;
Asutosh Das0ef24812012-12-18 16:14:02 +05302537
2538 /* Extract platform data */
2539 if (pdev->dev.of_node) {
Venkat Gopalakrishnan270580a2013-03-11 12:17:57 -07002540 ret = of_alias_get_id(pdev->dev.of_node, "sdhc");
2541 if (ret < 0) {
2542 dev_err(&pdev->dev, "Failed to get slot index %d\n",
2543 ret);
2544 goto pltfm_free;
2545 }
2546 if (disable_slots & (1 << (ret - 1))) {
2547 dev_info(&pdev->dev, "%s: Slot %d disabled\n", __func__,
2548 ret);
2549 ret = -ENODEV;
2550 goto pltfm_free;
2551 }
2552
Asutosh Das0ef24812012-12-18 16:14:02 +05302553 msm_host->pdata = sdhci_msm_populate_pdata(&pdev->dev);
2554 if (!msm_host->pdata) {
2555 dev_err(&pdev->dev, "DT parsing error\n");
2556 goto pltfm_free;
2557 }
2558 } else {
2559 dev_err(&pdev->dev, "No device tree node\n");
2560 goto pltfm_free;
2561 }
2562
2563 /* Setup Clocks */
2564
2565 /* Setup SDCC bus voter clock. */
2566 msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus_clk");
2567 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
2568 /* Vote for max. clk rate for max. performance */
2569 ret = clk_set_rate(msm_host->bus_clk, INT_MAX);
2570 if (ret)
2571 goto pltfm_free;
2572 ret = clk_prepare_enable(msm_host->bus_clk);
2573 if (ret)
2574 goto pltfm_free;
2575 }
2576
2577 /* Setup main peripheral bus clock */
2578 msm_host->pclk = devm_clk_get(&pdev->dev, "iface_clk");
2579 if (!IS_ERR(msm_host->pclk)) {
2580 ret = clk_prepare_enable(msm_host->pclk);
2581 if (ret)
2582 goto bus_clk_disable;
2583 }
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302584 atomic_set(&msm_host->controller_clock, 1);
Asutosh Das0ef24812012-12-18 16:14:02 +05302585
2586 /* Setup SDC MMC clock */
2587 msm_host->clk = devm_clk_get(&pdev->dev, "core_clk");
2588 if (IS_ERR(msm_host->clk)) {
2589 ret = PTR_ERR(msm_host->clk);
2590 goto pclk_disable;
2591 }
2592
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302593 /* Set to the minimum supported clock frequency */
2594 ret = clk_set_rate(msm_host->clk, sdhci_msm_get_min_clock(host));
2595 if (ret) {
2596 dev_err(&pdev->dev, "MClk rate set failed (%d)\n", ret);
Sahitya Tummala020ede0d2013-06-07 13:03:07 +05302597 goto pclk_disable;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302598 }
Sahitya Tummala020ede0d2013-06-07 13:03:07 +05302599 ret = clk_prepare_enable(msm_host->clk);
2600 if (ret)
2601 goto pclk_disable;
2602
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302603 msm_host->clk_rate = sdhci_msm_get_min_clock(host);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302604 atomic_set(&msm_host->clks_on, 1);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302605
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002606 /* Setup CDC calibration fixed feedback clock */
2607 msm_host->ff_clk = devm_clk_get(&pdev->dev, "cal_clk");
2608 if (!IS_ERR(msm_host->ff_clk)) {
2609 ret = clk_prepare_enable(msm_host->ff_clk);
2610 if (ret)
2611 goto clk_disable;
2612 }
2613
2614 /* Setup CDC calibration sleep clock */
2615 msm_host->sleep_clk = devm_clk_get(&pdev->dev, "sleep_clk");
2616 if (!IS_ERR(msm_host->sleep_clk)) {
2617 ret = clk_prepare_enable(msm_host->sleep_clk);
2618 if (ret)
2619 goto ff_clk_disable;
2620 }
2621
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07002622 msm_host->saved_tuning_phase = INVALID_TUNING_PHASE;
2623
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302624 ret = sdhci_msm_bus_register(msm_host, pdev);
2625 if (ret)
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002626 goto sleep_clk_disable;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302627
2628 if (msm_host->msm_bus_vote.client_handle)
2629 INIT_DELAYED_WORK(&msm_host->msm_bus_vote.vote_work,
2630 sdhci_msm_bus_work);
2631 sdhci_msm_bus_voting(host, 1);
2632
Asutosh Das0ef24812012-12-18 16:14:02 +05302633 /* Setup regulators */
2634 ret = sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, true);
2635 if (ret) {
2636 dev_err(&pdev->dev, "Regulator setup failed (%d)\n", ret);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302637 goto bus_unregister;
Asutosh Das0ef24812012-12-18 16:14:02 +05302638 }
2639
2640 /* Reset the core and Enable SDHC mode */
2641 core_memres = platform_get_resource_byname(pdev,
2642 IORESOURCE_MEM, "core_mem");
2643 msm_host->core_mem = devm_ioremap(&pdev->dev, core_memres->start,
2644 resource_size(core_memres));
2645
2646 if (!msm_host->core_mem) {
2647 dev_err(&pdev->dev, "Failed to remap registers\n");
2648 ret = -ENOMEM;
2649 goto vreg_deinit;
2650 }
2651
Stepan Moskovchenkoa4d0fa72013-09-13 22:19:33 -07002652 /* Unset HC_MODE_EN bit in HC_MODE register */
2653 writel_relaxed(0, (msm_host->core_mem + CORE_HC_MODE));
2654
Asutosh Das0ef24812012-12-18 16:14:02 +05302655 /* Set SW_RST bit in POWER register (Offset 0x0) */
Sahitya Tummala66b0fe32013-04-25 11:50:56 +05302656 writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_POWER) |
2657 CORE_SW_RST, msm_host->core_mem + CORE_POWER);
2658 /*
2659 * SW reset can take upto 10HCLK + 15MCLK cycles.
2660 * Calculating based on min clk rates (hclk = 27MHz,
2661 * mclk = 400KHz) it comes to ~40us. Let's poll for
2662 * max. 1ms for reset completion.
2663 */
2664 ret = readl_poll_timeout(msm_host->core_mem + CORE_POWER,
2665 pwr, !(pwr & CORE_SW_RST), 100, 10);
2666
2667 if (ret) {
2668 dev_err(&pdev->dev, "reset failed (%d)\n", ret);
2669 goto vreg_deinit;
2670 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302671 /* Set HC_MODE_EN bit in HC_MODE register */
2672 writel_relaxed(HC_MODE_EN, (msm_host->core_mem + CORE_HC_MODE));
2673
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002674 /* Set FF_CLK_SW_RST_DIS bit in HC_MODE register */
2675 writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_HC_MODE) |
2676 FF_CLK_SW_RST_DIS, msm_host->core_mem + CORE_HC_MODE);
2677
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302678 sdhci_set_default_hw_caps(msm_host, host);
Asutosh Das0ef24812012-12-18 16:14:02 +05302679 /*
Subhash Jadavani28137342013-05-14 17:46:43 +05302680 * CORE_SW_RST above may trigger power irq if previous status of PWRCTL
2681 * was either BUS_ON or IO_HIGH_V. So before we enable the power irq
2682 * interrupt in GIC (by registering the interrupt handler), we need to
2683 * ensure that any pending power irq interrupt status is acknowledged
2684 * otherwise power irq interrupt handler would be fired prematurely.
2685 */
2686 irq_status = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
2687 writel_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
2688 irq_ctl = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_CTL);
2689 if (irq_status & (CORE_PWRCTL_BUS_ON | CORE_PWRCTL_BUS_OFF))
2690 irq_ctl |= CORE_PWRCTL_BUS_SUCCESS;
2691 if (irq_status & (CORE_PWRCTL_IO_HIGH | CORE_PWRCTL_IO_LOW))
2692 irq_ctl |= CORE_PWRCTL_IO_SUCCESS;
2693 writel_relaxed(irq_ctl, (msm_host->core_mem + CORE_PWRCTL_CTL));
2694 /*
2695 * Ensure that above writes are propogated before interrupt enablement
2696 * in GIC.
2697 */
2698 mb();
2699
2700 /*
Asutosh Das0ef24812012-12-18 16:14:02 +05302701 * Following are the deviations from SDHC spec v3.0 -
2702 * 1. Card detection is handled using separate GPIO.
2703 * 2. Bus power control is handled by interacting with PMIC.
2704 */
2705 host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
2706 host->quirks |= SDHCI_QUIRK_SINGLE_POWER_WRITE;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302707 host->quirks |= SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN;
2708 host->quirks2 |= SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK;
Sahitya Tummala87d43942013-04-12 11:49:11 +05302709 host->quirks2 |= SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD;
Sahitya Tummala314162c2013-04-12 12:11:20 +05302710 host->quirks2 |= SDHCI_QUIRK2_BROKEN_PRESET_VALUE;
Sahitya Tummala7c9780d2013-04-12 11:59:25 +05302711 host->quirks2 |= SDHCI_QUIRK2_USE_RESERVED_MAX_TIMEOUT;
Asutosh Das0ef24812012-12-18 16:14:02 +05302712
Sahitya Tummalaa5733ab52013-06-10 16:32:51 +05302713 if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK)
2714 host->quirks2 |= SDHCI_QUIRK2_DIVIDE_TOUT_BY_4;
2715
Stephen Boyd8dce5c62013-04-24 14:19:46 -07002716 host_version = readw_relaxed((host->ioaddr + SDHCI_HOST_VERSION));
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07002717 dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n",
2718 host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >>
2719 SDHCI_VENDOR_VER_SHIFT));
2720 if (((host_version & SDHCI_VENDOR_VER_MASK) >>
2721 SDHCI_VENDOR_VER_SHIFT) == SDHCI_VER_100) {
2722 /*
2723 * Add 40us delay in interrupt handler when
2724 * operating at initialization frequency(400KHz).
2725 */
2726 host->quirks2 |= SDHCI_QUIRK2_SLOW_INT_CLR;
2727 /*
2728 * Set Software Reset for DAT line in Software
2729 * Reset Register (Bit 2).
2730 */
2731 host->quirks2 |= SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT;
2732 }
2733
Asutosh Das214b9662013-06-13 14:27:42 +05302734 host->quirks2 |= SDHCI_QUIRK2_IGN_DATA_END_BIT_ERROR;
2735
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07002736 /* Setup PWRCTL irq */
Asutosh Das0ef24812012-12-18 16:14:02 +05302737 pwr_irq = platform_get_irq_byname(pdev, "pwr_irq");
2738 if (pwr_irq < 0) {
2739 dev_err(&pdev->dev, "Failed to get pwr_irq by name (%d)\n",
2740 pwr_irq);
2741 goto vreg_deinit;
2742 }
2743 ret = devm_request_threaded_irq(&pdev->dev, pwr_irq, NULL,
2744 sdhci_msm_pwr_irq, IRQF_ONESHOT,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002745 dev_name(&pdev->dev), host);
Asutosh Das0ef24812012-12-18 16:14:02 +05302746 if (ret) {
2747 dev_err(&pdev->dev, "Request threaded irq(%d) failed (%d)\n",
2748 pwr_irq, ret);
2749 goto vreg_deinit;
2750 }
2751
2752 /* Enable pwr irq interrupts */
2753 writel_relaxed(INT_MASK, (msm_host->core_mem + CORE_PWRCTL_MASK));
2754
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302755#ifdef CONFIG_MMC_CLKGATE
2756 /* Set clock gating delay to be used when CONFIG_MMC_CLKGATE is set */
2757 msm_host->mmc->clkgate_delay = SDHCI_MSM_MMC_CLK_GATE_DELAY;
2758#endif
2759
Asutosh Das0ef24812012-12-18 16:14:02 +05302760 /* Set host capabilities */
2761 msm_host->mmc->caps |= msm_host->pdata->mmc_bus_width;
2762 msm_host->mmc->caps |= msm_host->pdata->caps;
Asutosh Das0ef24812012-12-18 16:14:02 +05302763 msm_host->mmc->caps2 |= msm_host->pdata->caps2;
2764 msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR;
2765 msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR_CONTROL;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302766 msm_host->mmc->caps2 |= MMC_CAP2_CLK_SCALE;
Subhash Jadavani6d472b22013-05-29 15:52:10 +05302767 msm_host->mmc->caps2 |= MMC_CAP2_ASYNC_SDIO_IRQ_4BIT_MODE;
Asutosh Dasbea115d2013-06-24 18:20:45 +05302768 msm_host->mmc->pm_caps |= MMC_PM_KEEP_POWER;
Asutosh Das0ef24812012-12-18 16:14:02 +05302769
2770 if (msm_host->pdata->nonremovable)
2771 msm_host->mmc->caps |= MMC_CAP_NONREMOVABLE;
2772
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05302773 host->cpu_dma_latency_us = msm_host->pdata->cpu_dma_latency_us;
2774
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302775 init_completion(&msm_host->pwr_irq_completion);
2776
Sahitya Tummala581df132013-03-12 14:57:46 +05302777 if (gpio_is_valid(msm_host->pdata->status_gpio)) {
2778 ret = mmc_gpio_request_cd(msm_host->mmc,
2779 msm_host->pdata->status_gpio, 0);
2780 if (ret) {
2781 dev_err(&pdev->dev, "%s: Failed to request card detection IRQ %d\n",
2782 __func__, ret);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302783 goto vreg_deinit;
Sahitya Tummala581df132013-03-12 14:57:46 +05302784 }
2785 }
2786
Krishna Konda7feab352013-09-17 23:55:40 -07002787 if ((sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT) &&
2788 (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(64)))) {
2789 host->dma_mask = DMA_BIT_MASK(64);
2790 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2791 } else if (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(32))) {
Sahitya Tummalaeaa21862013-03-20 19:34:59 +05302792 host->dma_mask = DMA_BIT_MASK(32);
2793 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2794 } else {
2795 dev_err(&pdev->dev, "%s: Failed to set dma mask\n", __func__);
2796 }
2797
Asutosh Das0ef24812012-12-18 16:14:02 +05302798 ret = sdhci_add_host(host);
2799 if (ret) {
2800 dev_err(&pdev->dev, "Add host failed (%d)\n", ret);
Sahitya Tummala581df132013-03-12 14:57:46 +05302801 goto vreg_deinit;
Asutosh Das0ef24812012-12-18 16:14:02 +05302802 }
2803
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302804 msm_host->msm_bus_vote.max_bus_bw.show = show_sdhci_max_bus_bw;
2805 msm_host->msm_bus_vote.max_bus_bw.store = store_sdhci_max_bus_bw;
2806 sysfs_attr_init(&msm_host->msm_bus_vote.max_bus_bw.attr);
2807 msm_host->msm_bus_vote.max_bus_bw.attr.name = "max_bus_bw";
2808 msm_host->msm_bus_vote.max_bus_bw.attr.mode = S_IRUGO | S_IWUSR;
2809 ret = device_create_file(&pdev->dev,
2810 &msm_host->msm_bus_vote.max_bus_bw);
2811 if (ret)
2812 goto remove_host;
2813
Sahitya Tummala5c55b932013-06-20 14:00:18 +05302814 if (!gpio_is_valid(msm_host->pdata->status_gpio)) {
2815 msm_host->polling.show = show_polling;
2816 msm_host->polling.store = store_polling;
2817 sysfs_attr_init(&msm_host->polling.attr);
2818 msm_host->polling.attr.name = "polling";
2819 msm_host->polling.attr.mode = S_IRUGO | S_IWUSR;
2820 ret = device_create_file(&pdev->dev, &msm_host->polling);
2821 if (ret)
2822 goto remove_max_bus_bw_file;
2823 }
Asutosh Dase5e9ca62013-07-30 19:08:36 +05302824
2825 msm_host->auto_cmd21_attr.show = show_auto_cmd21;
2826 msm_host->auto_cmd21_attr.store = store_auto_cmd21;
2827 sysfs_attr_init(&msm_host->auto_cmd21_attr.attr);
2828 msm_host->auto_cmd21_attr.attr.name = "enable_auto_cmd21";
2829 msm_host->auto_cmd21_attr.attr.mode = S_IRUGO | S_IWUSR;
2830 ret = device_create_file(&pdev->dev, &msm_host->auto_cmd21_attr);
2831 if (ret) {
2832 pr_err("%s: %s: failed creating auto-cmd21 attr: %d\n",
2833 mmc_hostname(host->mmc), __func__, ret);
2834 device_remove_file(&pdev->dev, &msm_host->auto_cmd21_attr);
2835 }
2836
Asutosh Das0ef24812012-12-18 16:14:02 +05302837 /* Successful initialization */
2838 goto out;
2839
Sahitya Tummala5c55b932013-06-20 14:00:18 +05302840remove_max_bus_bw_file:
2841 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Asutosh Das0ef24812012-12-18 16:14:02 +05302842remove_host:
2843 dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
2844 sdhci_remove_host(host, dead);
2845vreg_deinit:
2846 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302847bus_unregister:
2848 if (msm_host->msm_bus_vote.client_handle)
2849 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2850 sdhci_msm_bus_unregister(msm_host);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002851sleep_clk_disable:
2852 if (!IS_ERR(msm_host->sleep_clk))
2853 clk_disable_unprepare(msm_host->sleep_clk);
2854ff_clk_disable:
2855 if (!IS_ERR(msm_host->ff_clk))
2856 clk_disable_unprepare(msm_host->ff_clk);
Asutosh Das0ef24812012-12-18 16:14:02 +05302857clk_disable:
2858 if (!IS_ERR(msm_host->clk))
2859 clk_disable_unprepare(msm_host->clk);
2860pclk_disable:
2861 if (!IS_ERR(msm_host->pclk))
2862 clk_disable_unprepare(msm_host->pclk);
2863bus_clk_disable:
2864 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2865 clk_disable_unprepare(msm_host->bus_clk);
2866pltfm_free:
2867 sdhci_pltfm_free(pdev);
2868out:
2869 pr_debug("%s: Exit %s\n", dev_name(&pdev->dev), __func__);
2870 return ret;
2871}
2872
2873static int sdhci_msm_remove(struct platform_device *pdev)
2874{
2875 struct sdhci_host *host = platform_get_drvdata(pdev);
2876 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2877 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2878 struct sdhci_msm_pltfm_data *pdata = msm_host->pdata;
2879 int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) ==
2880 0xffffffff);
2881
2882 pr_debug("%s: %s\n", dev_name(&pdev->dev), __func__);
Sahitya Tummala5c55b932013-06-20 14:00:18 +05302883 if (!gpio_is_valid(msm_host->pdata->status_gpio))
2884 device_remove_file(&pdev->dev, &msm_host->polling);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302885 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Asutosh Das0ef24812012-12-18 16:14:02 +05302886 sdhci_remove_host(host, dead);
2887 sdhci_pltfm_free(pdev);
Sahitya Tummala581df132013-03-12 14:57:46 +05302888
Asutosh Das0ef24812012-12-18 16:14:02 +05302889 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302890
Asutosh Das0ef24812012-12-18 16:14:02 +05302891 if (pdata->pin_data)
2892 sdhci_msm_setup_gpio(pdata, false);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302893
2894 if (msm_host->msm_bus_vote.client_handle) {
2895 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2896 sdhci_msm_bus_unregister(msm_host);
2897 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302898 return 0;
2899}
2900
2901static const struct of_device_id sdhci_msm_dt_match[] = {
2902 {.compatible = "qcom,sdhci-msm"},
2903};
2904MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match);
2905
2906static struct platform_driver sdhci_msm_driver = {
2907 .probe = sdhci_msm_probe,
2908 .remove = sdhci_msm_remove,
2909 .driver = {
2910 .name = "sdhci_msm",
2911 .owner = THIS_MODULE,
2912 .of_match_table = sdhci_msm_dt_match,
2913 },
2914};
2915
2916module_platform_driver(sdhci_msm_driver);
2917
2918MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Secure Digital Host Controller Interface driver");
2919MODULE_LICENSE("GPL v2");