blob: 555aab082a7647bbf8127a010c39dc5fbdce3b83 [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 *
Sujit Reddy Thummabf1aecc2014-01-10 10:58:54 +05305 * Copyright (c) 2012-2014, The Linux Foundation. All rights reserved.
Asutosh Das0ef24812012-12-18 16:14:02 +05306 *
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>
Pratibhasagar V9acf2642013-11-21 21:07:21 +053038#include <linux/pinctrl/consumer.h>
39#include <linux/iopoll.h>
Sahitya Tummala8a3e8182013-03-10 14:12:52 +053040#include <linux/msm-bus.h>
Asutosh Das0ef24812012-12-18 16:14:02 +053041
42#include "sdhci-pltfm.h"
43
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -070044#define SDHCI_VER_100 0x2B
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +053045
46#define CORE_VERSION_MAJOR_MASK 0xF0000000
47#define CORE_VERSION_MAJOR_SHIFT 28
48
Asutosh Das0ef24812012-12-18 16:14:02 +053049#define CORE_HC_MODE 0x78
50#define HC_MODE_EN 0x1
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -070051#define FF_CLK_SW_RST_DIS (1 << 13)
Asutosh Das0ef24812012-12-18 16:14:02 +053052
Sahitya Tummala481fbb02013-08-06 15:22:28 +053053#define CORE_GENERICS 0x70
54#define SWITCHABLE_SIGNALLING_VOL (1 << 29)
55
Asutosh Das0ef24812012-12-18 16:14:02 +053056#define CORE_POWER 0x0
57#define CORE_SW_RST (1 << 7)
58
Sahitya Tummala67717bc2013-08-02 09:21:37 +053059#define CORE_MCI_VERSION 0x050
60#define CORE_TESTBUS_CONFIG 0x0CC
61#define CORE_TESTBUS_ENA (1 << 3)
62#define CORE_SDCC_DEBUG_REG 0x124
63
Asutosh Das0ef24812012-12-18 16:14:02 +053064#define CORE_PWRCTL_STATUS 0xDC
65#define CORE_PWRCTL_MASK 0xE0
66#define CORE_PWRCTL_CLEAR 0xE4
67#define CORE_PWRCTL_CTL 0xE8
68
69#define CORE_PWRCTL_BUS_OFF 0x01
70#define CORE_PWRCTL_BUS_ON (1 << 1)
71#define CORE_PWRCTL_IO_LOW (1 << 2)
72#define CORE_PWRCTL_IO_HIGH (1 << 3)
73
74#define CORE_PWRCTL_BUS_SUCCESS 0x01
75#define CORE_PWRCTL_BUS_FAIL (1 << 1)
76#define CORE_PWRCTL_IO_SUCCESS (1 << 2)
77#define CORE_PWRCTL_IO_FAIL (1 << 3)
78
79#define INT_MASK 0xF
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -070080#define MAX_PHASES 16
81
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -070082#define CORE_DLL_CONFIG 0x100
83#define CORE_CMD_DAT_TRACK_SEL (1 << 0)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -070084#define CORE_DLL_EN (1 << 16)
85#define CORE_CDR_EN (1 << 17)
86#define CORE_CK_OUT_EN (1 << 18)
87#define CORE_CDR_EXT_EN (1 << 19)
88#define CORE_DLL_PDN (1 << 29)
89#define CORE_DLL_RST (1 << 30)
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -070090
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -070091#define CORE_DLL_STATUS 0x108
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -070092#define CORE_DLL_LOCK (1 << 7)
Krishna Konda2faa7bb2014-06-04 01:25:16 -070093#define CORE_DDR_DLL_LOCK (1 << 11)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -070094
95#define CORE_VENDOR_SPEC 0x10C
96#define CORE_CLK_PWRSAVE (1 << 1)
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -070097#define CORE_HC_MCLK_SEL_DFLT (2 << 8)
98#define CORE_HC_MCLK_SEL_HS400 (3 << 8)
99#define CORE_HC_MCLK_SEL_MASK (3 << 8)
Asutosh Dase5e9ca62013-07-30 19:08:36 +0530100#define CORE_HC_AUTO_CMD21_EN (1 << 6)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700101#define CORE_IO_PAD_PWR_SWITCH (1 << 16)
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700102#define CORE_HC_SELECT_IN_EN (1 << 18)
103#define CORE_HC_SELECT_IN_HS400 (6 << 19)
104#define CORE_HC_SELECT_IN_MASK (7 << 19)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700105
Krishna Konda7feab352013-09-17 23:55:40 -0700106#define CORE_VENDOR_SPEC_CAPABILITIES0 0x11C
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +0530107#define CORE_8_BIT_SUPPORT (1 << 18)
108#define CORE_3_3V_SUPPORT (1 << 24)
109#define CORE_3_0V_SUPPORT (1 << 25)
110#define CORE_1_8V_SUPPORT (1 << 26)
Krishna Konda7feab352013-09-17 23:55:40 -0700111#define CORE_SYS_BUS_SUPPORT_64_BIT 28
112
Sahitya Tummala67717bc2013-08-02 09:21:37 +0530113#define CORE_VENDOR_SPEC_ADMA_ERR_ADDR0 0x114
114#define CORE_VENDOR_SPEC_ADMA_ERR_ADDR1 0x118
115
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700116#define CORE_CSR_CDC_CTLR_CFG0 0x130
117#define CORE_SW_TRIG_FULL_CALIB (1 << 16)
118#define CORE_HW_AUTOCAL_ENA (1 << 17)
119
120#define CORE_CSR_CDC_CTLR_CFG1 0x134
121#define CORE_CSR_CDC_CAL_TIMER_CFG0 0x138
122#define CORE_TIMER_ENA (1 << 16)
123
124#define CORE_CSR_CDC_CAL_TIMER_CFG1 0x13C
125#define CORE_CSR_CDC_REFCOUNT_CFG 0x140
126#define CORE_CSR_CDC_COARSE_CAL_CFG 0x144
127#define CORE_CDC_OFFSET_CFG 0x14C
128#define CORE_CSR_CDC_DELAY_CFG 0x150
129#define CORE_CDC_SLAVE_DDA_CFG 0x160
130#define CORE_CSR_CDC_STATUS0 0x164
131#define CORE_CALIBRATION_DONE (1 << 0)
132
133#define CORE_CDC_ERROR_CODE_MASK 0x7000000
134
135#define CORE_CSR_CDC_GEN_CFG 0x178
136#define CORE_CDC_SWITCH_BYPASS_OFF (1 << 0)
137#define CORE_CDC_SWITCH_RC_EN (1 << 1)
138
139#define CORE_DDR_200_CFG 0x184
140#define CORE_CDC_T4_DLY_SEL (1 << 0)
141#define CORE_START_CDC_TRAFFIC (1 << 6)
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700142#define CORE_VENDOR_SPEC3 0x1B0
143#define CORE_PWRSAVE_DLL (1 << 3)
144
145#define CORE_DLL_CONFIG_2 0x1B4
146#define CORE_DDR_CAL_EN (1 << 0)
147
148#define CORE_DDR_CONFIG 0x1B8
149#define DDR_CONFIG_POR_VAL 0x80040853
150
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700151
Sahitya Tummala67717bc2013-08-02 09:21:37 +0530152#define CORE_MCI_DATA_CNT 0x30
153#define CORE_MCI_STATUS 0x34
154#define CORE_MCI_FIFO_CNT 0x44
155
156#define CORE_TESTBUS_SEL2_BIT 4
157#define CORE_TESTBUS_SEL2 (1 << CORE_TESTBUS_SEL2_BIT)
158
Venkat Gopalakrishnan450745e2014-07-24 20:39:34 -0700159/* 512 descriptors */
160#define SDHCI_MSM_MAX_SEGMENTS (1 << 9)
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +0530161#define SDHCI_MSM_MMC_CLK_GATE_DELAY 200 /* msecs */
Asutosh Das648f9d12013-01-10 21:11:04 +0530162
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700163#define CORE_FREQ_100MHZ (100 * 1000 * 1000)
164
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700165#define INVALID_TUNING_PHASE -1
166
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +0530167#define CORE_VERSION_TARGET_MASK 0x000000FF
168
Krishna Konda96e6b112013-10-28 15:25:03 -0700169#define NUM_TUNING_PHASES 16
170#define MAX_DRV_TYPES_SUPPORTED_HS200 3
171
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700172static const u32 tuning_block_64[] = {
173 0x00FF0FFF, 0xCCC3CCFF, 0xFFCC3CC3, 0xEFFEFFFE,
174 0xDDFFDFFF, 0xFBFFFBFF, 0xFF7FFFBF, 0xEFBDF777,
175 0xF0FFF0FF, 0x3CCCFC0F, 0xCFCC33CC, 0xEEFFEFFF,
176 0xFDFFFDFF, 0xFFBFFFDF, 0xFFF7FFBB, 0xDE7B7FF7
177};
178
179static const u32 tuning_block_128[] = {
180 0xFF00FFFF, 0x0000FFFF, 0xCCCCFFFF, 0xCCCC33CC,
181 0xCC3333CC, 0xFFFFCCCC, 0xFFFFEEFF, 0xFFEEEEFF,
182 0xFFDDFFFF, 0xDDDDFFFF, 0xBBFFFFFF, 0xBBFFFFFF,
183 0xFFFFFFBB, 0xFFFFFF77, 0x77FF7777, 0xFFEEDDBB,
184 0x00FFFFFF, 0x00FFFFFF, 0xCCFFFF00, 0xCC33CCCC,
185 0x3333CCCC, 0xFFCCCCCC, 0xFFEEFFFF, 0xEEEEFFFF,
186 0xDDFFFFFF, 0xDDFFFFFF, 0xFFFFFFDD, 0xFFFFFFBB,
187 0xFFFFBBBB, 0xFFFF77FF, 0xFF7777FF, 0xEEDDBB77
188};
Asutosh Das0ef24812012-12-18 16:14:02 +0530189
Venkat Gopalakrishnan270580a2013-03-11 12:17:57 -0700190static int disable_slots;
191/* root can write, others read */
192module_param(disable_slots, int, S_IRUGO|S_IWUSR);
193
Asutosh Das0ef24812012-12-18 16:14:02 +0530194/* This structure keeps information per regulator */
195struct sdhci_msm_reg_data {
196 /* voltage regulator handle */
197 struct regulator *reg;
198 /* regulator name */
199 const char *name;
200 /* voltage level to be set */
201 u32 low_vol_level;
202 u32 high_vol_level;
203 /* Load values for low power and high power mode */
204 u32 lpm_uA;
205 u32 hpm_uA;
206
207 /* is this regulator enabled? */
208 bool is_enabled;
209 /* is this regulator needs to be always on? */
210 bool is_always_on;
211 /* is low power mode setting required for this regulator? */
212 bool lpm_sup;
213 bool set_voltage_sup;
214};
215
Asutosh Das598a4d42014-02-05 16:38:23 +0530216#define MSM_MMC_DEFAULT_CPU_DMA_LATENCY 200 /* usecs */
Asutosh Das0ef24812012-12-18 16:14:02 +0530217/*
218 * This structure keeps information for all the
219 * regulators required for a SDCC slot.
220 */
221struct sdhci_msm_slot_reg_data {
222 /* keeps VDD/VCC regulator info */
223 struct sdhci_msm_reg_data *vdd_data;
224 /* keeps VDD IO regulator info */
225 struct sdhci_msm_reg_data *vdd_io_data;
226};
227
228struct sdhci_msm_gpio {
229 u32 no;
230 const char *name;
231 bool is_enabled;
232};
233
234struct sdhci_msm_gpio_data {
235 struct sdhci_msm_gpio *gpio;
236 u8 size;
237};
238
239struct sdhci_msm_pin_data {
240 /*
241 * = 1 if controller pins are using gpios
242 * = 0 if controller has dedicated MSM pads
243 */
Sahitya Tummala1cd7e072014-02-14 13:19:01 +0530244 u8 is_gpio;
Asutosh Das0ef24812012-12-18 16:14:02 +0530245 struct sdhci_msm_gpio_data *gpio_data;
246};
247
Pratibhasagar V9acf2642013-11-21 21:07:21 +0530248struct sdhci_pinctrl_data {
249 struct pinctrl *pctrl;
250 struct pinctrl_state *pins_active;
251 struct pinctrl_state *pins_sleep;
252};
253
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530254struct sdhci_msm_bus_voting_data {
255 struct msm_bus_scale_pdata *bus_pdata;
256 unsigned int *bw_vecs;
257 unsigned int bw_vecs_size;
258};
259
Asutosh Das0ef24812012-12-18 16:14:02 +0530260struct sdhci_msm_pltfm_data {
261 /* Supported UHS-I Modes */
262 u32 caps;
263
264 /* More capabilities */
265 u32 caps2;
266
267 unsigned long mmc_bus_width;
Asutosh Das0ef24812012-12-18 16:14:02 +0530268 struct sdhci_msm_slot_reg_data *vreg_data;
269 bool nonremovable;
Guoping Yuf7c91332014-08-20 16:56:18 +0800270 bool nonhotplug;
Pratibhasagar V9acf2642013-11-21 21:07:21 +0530271 bool pin_cfg_sts;
Asutosh Das0ef24812012-12-18 16:14:02 +0530272 struct sdhci_msm_pin_data *pin_data;
Pratibhasagar V9acf2642013-11-21 21:07:21 +0530273 struct sdhci_pinctrl_data *pctrl_data;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +0530274 u32 cpu_dma_latency_us;
Sahitya Tummala581df132013-03-12 14:57:46 +0530275 int status_gpio; /* card detection GPIO that is configured as IRQ */
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530276 struct sdhci_msm_bus_voting_data *voting_data;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530277 u32 *sup_clk_table;
278 unsigned char sup_clk_cnt;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530279};
280
281struct sdhci_msm_bus_vote {
282 uint32_t client_handle;
283 uint32_t curr_vote;
284 int min_bw_vote;
285 int max_bw_vote;
286 bool is_max_bw_needed;
287 struct delayed_work vote_work;
288 struct device_attribute max_bus_bw;
Asutosh Das0ef24812012-12-18 16:14:02 +0530289};
290
291struct sdhci_msm_host {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530292 struct platform_device *pdev;
Asutosh Das0ef24812012-12-18 16:14:02 +0530293 void __iomem *core_mem; /* MSM SDCC mapped address */
294 struct clk *clk; /* main SD/MMC bus clock */
295 struct clk *pclk; /* SDHC peripheral bus clock */
296 struct clk *bus_clk; /* SDHC bus voter clock */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700297 struct clk *ff_clk; /* CDC calibration fixed feedback clock */
298 struct clk *sleep_clk; /* CDC calibration sleep clock */
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +0530299 atomic_t clks_on; /* Set if clocks are enabled */
Asutosh Das0ef24812012-12-18 16:14:02 +0530300 struct sdhci_msm_pltfm_data *pdata;
301 struct mmc_host *mmc;
302 struct sdhci_pltfm_data sdhci_msm_pdata;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +0530303 u32 curr_pwr_state;
304 u32 curr_io_level;
305 struct completion pwr_irq_completion;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530306 struct sdhci_msm_bus_vote msm_bus_vote;
Sahitya Tummala5c55b932013-06-20 14:00:18 +0530307 struct device_attribute polling;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530308 u32 clk_rate; /* Keeps track of current clock rate that is set */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700309 bool tuning_done;
310 bool calibration_done;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700311 u8 saved_tuning_phase;
Asutosh Dase5e9ca62013-07-30 19:08:36 +0530312 bool en_auto_cmd21;
313 struct device_attribute auto_cmd21_attr;
Asutosh Das9d7ee2f2013-11-08 12:33:47 +0530314 atomic_t controller_clock;
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700315 bool use_cdclp533;
Asutosh Das0ef24812012-12-18 16:14:02 +0530316};
317
318enum vdd_io_level {
319 /* set vdd_io_data->low_vol_level */
320 VDD_IO_LOW,
321 /* set vdd_io_data->high_vol_level */
322 VDD_IO_HIGH,
323 /*
324 * set whatever there in voltage_level (third argument) of
325 * sdhci_msm_set_vdd_io_vol() function.
326 */
327 VDD_IO_SET_LEVEL,
328};
329
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700330/* MSM platform specific tuning */
331static inline int msm_dll_poll_ck_out_en(struct sdhci_host *host,
332 u8 poll)
333{
334 int rc = 0;
335 u32 wait_cnt = 50;
336 u8 ck_out_en = 0;
337 struct mmc_host *mmc = host->mmc;
338
339 /* poll for CK_OUT_EN bit. max. poll time = 50us */
340 ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) &
341 CORE_CK_OUT_EN);
342
343 while (ck_out_en != poll) {
344 if (--wait_cnt == 0) {
345 pr_err("%s: %s: CK_OUT_EN bit is not %d\n",
346 mmc_hostname(mmc), __func__, poll);
347 rc = -ETIMEDOUT;
348 goto out;
349 }
350 udelay(1);
351
352 ck_out_en = !!(readl_relaxed(host->ioaddr +
353 CORE_DLL_CONFIG) & CORE_CK_OUT_EN);
354 }
355out:
356 return rc;
357}
358
Asutosh Dase5e9ca62013-07-30 19:08:36 +0530359/*
360 * Enable CDR to track changes of DAT lines and adjust sampling
361 * point according to voltage/temperature variations
362 */
363static int msm_enable_cdr_cm_sdc4_dll(struct sdhci_host *host)
364{
365 int rc = 0;
366 u32 config;
367
368 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
369 config |= CORE_CDR_EN;
370 config &= ~(CORE_CDR_EXT_EN | CORE_CK_OUT_EN);
371 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
372
373 rc = msm_dll_poll_ck_out_en(host, 0);
374 if (rc)
375 goto err;
376
377 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) |
378 CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
379
380 rc = msm_dll_poll_ck_out_en(host, 1);
381 if (rc)
382 goto err;
383 goto out;
384err:
385 pr_err("%s: %s: failed\n", mmc_hostname(host->mmc), __func__);
386out:
387 return rc;
388}
389
390static ssize_t store_auto_cmd21(struct device *dev, struct device_attribute
391 *attr, const char *buf, size_t count)
392{
393 struct sdhci_host *host = dev_get_drvdata(dev);
394 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
395 struct sdhci_msm_host *msm_host = pltfm_host->priv;
396 u32 tmp;
397 unsigned long flags;
398
399 if (!kstrtou32(buf, 0, &tmp)) {
400 spin_lock_irqsave(&host->lock, flags);
401 msm_host->en_auto_cmd21 = !!tmp;
402 spin_unlock_irqrestore(&host->lock, flags);
403 }
404 return count;
405}
406
407static ssize_t show_auto_cmd21(struct device *dev,
408 struct device_attribute *attr, char *buf)
409{
410 struct sdhci_host *host = dev_get_drvdata(dev);
411 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
412 struct sdhci_msm_host *msm_host = pltfm_host->priv;
413
414 return snprintf(buf, PAGE_SIZE, "%d\n", msm_host->en_auto_cmd21);
415}
416
417/* MSM auto-tuning handler */
418static int sdhci_msm_config_auto_tuning_cmd(struct sdhci_host *host,
419 bool enable,
420 u32 type)
421{
422 int rc = 0;
423 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
424 struct sdhci_msm_host *msm_host = pltfm_host->priv;
425 u32 val = 0;
426
427 if (!msm_host->en_auto_cmd21)
428 return 0;
429
430 if (type == MMC_SEND_TUNING_BLOCK_HS200)
431 val = CORE_HC_AUTO_CMD21_EN;
432 else
433 return 0;
434
435 if (enable) {
436 rc = msm_enable_cdr_cm_sdc4_dll(host);
437 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
438 val, host->ioaddr + CORE_VENDOR_SPEC);
439 } else {
440 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
441 ~val, host->ioaddr + CORE_VENDOR_SPEC);
442 }
443 return rc;
444}
445
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700446static int msm_config_cm_dll_phase(struct sdhci_host *host, u8 phase)
447{
448 int rc = 0;
449 u8 grey_coded_phase_table[] = {0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4,
450 0xC, 0xD, 0xF, 0xE, 0xA, 0xB, 0x9,
451 0x8};
452 unsigned long flags;
453 u32 config;
454 struct mmc_host *mmc = host->mmc;
455
456 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
457 spin_lock_irqsave(&host->lock, flags);
458
459 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
460 config &= ~(CORE_CDR_EN | CORE_CK_OUT_EN);
461 config |= (CORE_CDR_EXT_EN | CORE_DLL_EN);
462 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
463
464 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '0' */
465 rc = msm_dll_poll_ck_out_en(host, 0);
466 if (rc)
467 goto err_out;
468
469 /*
470 * Write the selected DLL clock output phase (0 ... 15)
471 * to CDR_SELEXT bit field of DLL_CONFIG register.
472 */
473 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
474 & ~(0xF << 20))
475 | (grey_coded_phase_table[phase] << 20)),
476 host->ioaddr + CORE_DLL_CONFIG);
477
478 /* Set CK_OUT_EN bit of DLL_CONFIG register to 1. */
479 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
480 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
481
482 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '1' */
483 rc = msm_dll_poll_ck_out_en(host, 1);
484 if (rc)
485 goto err_out;
486
487 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
488 config |= CORE_CDR_EN;
489 config &= ~CORE_CDR_EXT_EN;
490 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
491 goto out;
492
493err_out:
494 pr_err("%s: %s: Failed to set DLL phase: %d\n",
495 mmc_hostname(mmc), __func__, phase);
496out:
497 spin_unlock_irqrestore(&host->lock, flags);
498 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
499 return rc;
500}
501
502/*
503 * Find out the greatest range of consecuitive selected
504 * DLL clock output phases that can be used as sampling
505 * setting for SD3.0 UHS-I card read operation (in SDR104
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700506 * timing mode) or for eMMC4.5 card read operation (in
507 * HS400/HS200 timing mode).
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700508 * Select the 3/4 of the range and configure the DLL with the
509 * selected DLL clock output phase.
510 */
511
512static int msm_find_most_appropriate_phase(struct sdhci_host *host,
513 u8 *phase_table, u8 total_phases)
514{
515 int ret;
516 u8 ranges[MAX_PHASES][MAX_PHASES] = { {0}, {0} };
517 u8 phases_per_row[MAX_PHASES] = {0};
518 int row_index = 0, col_index = 0, selected_row_index = 0, curr_max = 0;
519 int i, cnt, phase_0_raw_index = 0, phase_15_raw_index = 0;
520 bool phase_0_found = false, phase_15_found = false;
521 struct mmc_host *mmc = host->mmc;
522
523 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
524 if (!total_phases || (total_phases > MAX_PHASES)) {
525 pr_err("%s: %s: invalid argument: total_phases=%d\n",
526 mmc_hostname(mmc), __func__, total_phases);
527 return -EINVAL;
528 }
529
530 for (cnt = 0; cnt < total_phases; cnt++) {
531 ranges[row_index][col_index] = phase_table[cnt];
532 phases_per_row[row_index] += 1;
533 col_index++;
534
535 if ((cnt + 1) == total_phases) {
536 continue;
537 /* check if next phase in phase_table is consecutive or not */
538 } else if ((phase_table[cnt] + 1) != phase_table[cnt + 1]) {
539 row_index++;
540 col_index = 0;
541 }
542 }
543
544 if (row_index >= MAX_PHASES)
545 return -EINVAL;
546
547 /* Check if phase-0 is present in first valid window? */
548 if (!ranges[0][0]) {
549 phase_0_found = true;
550 phase_0_raw_index = 0;
551 /* Check if cycle exist between 2 valid windows */
552 for (cnt = 1; cnt <= row_index; cnt++) {
553 if (phases_per_row[cnt]) {
554 for (i = 0; i < phases_per_row[cnt]; i++) {
555 if (ranges[cnt][i] == 15) {
556 phase_15_found = true;
557 phase_15_raw_index = cnt;
558 break;
559 }
560 }
561 }
562 }
563 }
564
565 /* If 2 valid windows form cycle then merge them as single window */
566 if (phase_0_found && phase_15_found) {
567 /* number of phases in raw where phase 0 is present */
568 u8 phases_0 = phases_per_row[phase_0_raw_index];
569 /* number of phases in raw where phase 15 is present */
570 u8 phases_15 = phases_per_row[phase_15_raw_index];
571
572 if (phases_0 + phases_15 >= MAX_PHASES)
573 /*
574 * If there are more than 1 phase windows then total
575 * number of phases in both the windows should not be
576 * more than or equal to MAX_PHASES.
577 */
578 return -EINVAL;
579
580 /* Merge 2 cyclic windows */
581 i = phases_15;
582 for (cnt = 0; cnt < phases_0; cnt++) {
583 ranges[phase_15_raw_index][i] =
584 ranges[phase_0_raw_index][cnt];
585 if (++i >= MAX_PHASES)
586 break;
587 }
588
589 phases_per_row[phase_0_raw_index] = 0;
590 phases_per_row[phase_15_raw_index] = phases_15 + phases_0;
591 }
592
593 for (cnt = 0; cnt <= row_index; cnt++) {
594 if (phases_per_row[cnt] > curr_max) {
595 curr_max = phases_per_row[cnt];
596 selected_row_index = cnt;
597 }
598 }
599
600 i = ((curr_max * 3) / 4);
601 if (i)
602 i--;
603
604 ret = (int)ranges[selected_row_index][i];
605
606 if (ret >= MAX_PHASES) {
607 ret = -EINVAL;
608 pr_err("%s: %s: invalid phase selected=%d\n",
609 mmc_hostname(mmc), __func__, ret);
610 }
611
612 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
613 return ret;
614}
615
616static inline void msm_cm_dll_set_freq(struct sdhci_host *host)
617{
618 u32 mclk_freq = 0;
619
620 /* Program the MCLK value to MCLK_FREQ bit field */
621 if (host->clock <= 112000000)
622 mclk_freq = 0;
623 else if (host->clock <= 125000000)
624 mclk_freq = 1;
625 else if (host->clock <= 137000000)
626 mclk_freq = 2;
627 else if (host->clock <= 150000000)
628 mclk_freq = 3;
629 else if (host->clock <= 162000000)
630 mclk_freq = 4;
631 else if (host->clock <= 175000000)
632 mclk_freq = 5;
633 else if (host->clock <= 187000000)
634 mclk_freq = 6;
635 else if (host->clock <= 200000000)
636 mclk_freq = 7;
637
638 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
639 & ~(7 << 24)) | (mclk_freq << 24)),
640 host->ioaddr + CORE_DLL_CONFIG);
641}
642
643/* Initialize the DLL (Programmable Delay Line ) */
644static int msm_init_cm_dll(struct sdhci_host *host)
645{
646 struct mmc_host *mmc = host->mmc;
647 int rc = 0;
648 unsigned long flags;
649 u32 wait_cnt;
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530650 bool prev_pwrsave, curr_pwrsave;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700651
652 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
653 spin_lock_irqsave(&host->lock, flags);
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530654 prev_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
655 CORE_CLK_PWRSAVE);
656 curr_pwrsave = prev_pwrsave;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700657 /*
658 * Make sure that clock is always enabled when DLL
659 * tuning is in progress. Keeping PWRSAVE ON may
660 * turn off the clock. So let's disable the PWRSAVE
661 * here and re-enable it once tuning is completed.
662 */
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530663 if (prev_pwrsave) {
664 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
665 & ~CORE_CLK_PWRSAVE),
666 host->ioaddr + CORE_VENDOR_SPEC);
667 curr_pwrsave = false;
668 }
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700669
670 /* Write 1 to DLL_RST bit of DLL_CONFIG register */
671 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
672 | CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
673
674 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
675 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
676 | CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
677 msm_cm_dll_set_freq(host);
678
679 /* Write 0 to DLL_RST bit of DLL_CONFIG register */
680 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
681 & ~CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
682
683 /* Write 0 to DLL_PDN bit of DLL_CONFIG register */
684 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
685 & ~CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
686
687 /* Set DLL_EN bit to 1. */
688 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
689 | CORE_DLL_EN), host->ioaddr + CORE_DLL_CONFIG);
690
691 /* Set CK_OUT_EN bit to 1. */
692 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
693 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
694
695 wait_cnt = 50;
696 /* Wait until DLL_LOCK bit of DLL_STATUS register becomes '1' */
697 while (!(readl_relaxed(host->ioaddr + CORE_DLL_STATUS) &
698 CORE_DLL_LOCK)) {
699 /* max. wait for 50us sec for LOCK bit to be set */
700 if (--wait_cnt == 0) {
701 pr_err("%s: %s: DLL failed to LOCK\n",
702 mmc_hostname(mmc), __func__);
703 rc = -ETIMEDOUT;
704 goto out;
705 }
706 /* wait for 1us before polling again */
707 udelay(1);
708 }
709
710out:
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530711 /* Restore the correct PWRSAVE state */
712 if (prev_pwrsave ^ curr_pwrsave) {
713 u32 reg = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC);
714
715 if (prev_pwrsave)
716 reg |= CORE_CLK_PWRSAVE;
717 else
718 reg &= ~CORE_CLK_PWRSAVE;
719
720 writel_relaxed(reg, host->ioaddr + CORE_VENDOR_SPEC);
721 }
722
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700723 spin_unlock_irqrestore(&host->lock, flags);
724 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
725 return rc;
726}
727
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700728static int sdhci_msm_cdclp533_calibration(struct sdhci_host *host)
729{
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700730 u32 calib_done;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700731 int ret = 0;
732 int cdc_err = 0;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700733
734 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
735
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700736 /* Write 0 to CDC_T4_DLY_SEL field in VENDOR_SPEC_DDR200_CFG */
737 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
738 & ~CORE_CDC_T4_DLY_SEL),
739 host->ioaddr + CORE_DDR_200_CFG);
740
741 /* Write 0 to CDC_SWITCH_BYPASS_OFF field in CORE_CSR_CDC_GEN_CFG */
742 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG)
743 & ~CORE_CDC_SWITCH_BYPASS_OFF),
744 host->ioaddr + CORE_CSR_CDC_GEN_CFG);
745
746 /* Write 1 to CDC_SWITCH_RC_EN field in CORE_CSR_CDC_GEN_CFG */
747 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG)
748 | CORE_CDC_SWITCH_RC_EN),
749 host->ioaddr + CORE_CSR_CDC_GEN_CFG);
750
751 /* Write 0 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
752 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
753 & ~CORE_START_CDC_TRAFFIC),
754 host->ioaddr + CORE_DDR_200_CFG);
755
756 /*
757 * Perform CDC Register Initialization Sequence
758 *
759 * CORE_CSR_CDC_CTLR_CFG0 0x11800EC
760 * CORE_CSR_CDC_CTLR_CFG1 0x3011111
761 * CORE_CSR_CDC_CAL_TIMER_CFG0 0x1201000
762 * CORE_CSR_CDC_CAL_TIMER_CFG1 0x4
763 * CORE_CSR_CDC_REFCOUNT_CFG 0xCB732020
764 * CORE_CSR_CDC_COARSE_CAL_CFG 0xB19
765 * CORE_CSR_CDC_DELAY_CFG 0x3AC
766 * CORE_CDC_OFFSET_CFG 0x0
767 * CORE_CDC_SLAVE_DDA_CFG 0x16334
768 */
769
770 writel_relaxed(0x11800EC, host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
771 writel_relaxed(0x3011111, host->ioaddr + CORE_CSR_CDC_CTLR_CFG1);
772 writel_relaxed(0x1201000, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
773 writel_relaxed(0x4, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG1);
774 writel_relaxed(0xCB732020, host->ioaddr + CORE_CSR_CDC_REFCOUNT_CFG);
775 writel_relaxed(0xB19, host->ioaddr + CORE_CSR_CDC_COARSE_CAL_CFG);
Subhash Jadavanibe406d92014-06-17 16:47:48 -0700776 writel_relaxed(0x4E2, host->ioaddr + CORE_CSR_CDC_DELAY_CFG);
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700777 writel_relaxed(0x0, host->ioaddr + CORE_CDC_OFFSET_CFG);
778 writel_relaxed(0x16334, host->ioaddr + CORE_CDC_SLAVE_DDA_CFG);
779
780 /* CDC HW Calibration */
781
782 /* Write 1 to SW_TRIG_FULL_CALIB field in CORE_CSR_CDC_CTLR_CFG0 */
783 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
784 | CORE_SW_TRIG_FULL_CALIB),
785 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
786
787 /* Write 0 to SW_TRIG_FULL_CALIB field in CORE_CSR_CDC_CTLR_CFG0 */
788 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
789 & ~CORE_SW_TRIG_FULL_CALIB),
790 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
791
792 /* Write 1 to HW_AUTOCAL_ENA field in CORE_CSR_CDC_CTLR_CFG0 */
793 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
794 | CORE_HW_AUTOCAL_ENA),
795 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
796
797 /* Write 1 to TIMER_ENA field in CORE_CSR_CDC_CAL_TIMER_CFG0 */
798 writel_relaxed((readl_relaxed(host->ioaddr +
799 CORE_CSR_CDC_CAL_TIMER_CFG0) | CORE_TIMER_ENA),
800 host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
801
802 mb();
803
804 /* Poll on CALIBRATION_DONE field in CORE_CSR_CDC_STATUS0 to be 1 */
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700805 ret = readl_poll_timeout(host->ioaddr + CORE_CSR_CDC_STATUS0,
806 calib_done, (calib_done & CORE_CALIBRATION_DONE), 1, 50);
807
808 if (ret == -ETIMEDOUT) {
809 pr_err("%s: %s: CDC Calibration was not completed\n",
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700810 mmc_hostname(host->mmc), __func__);
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700811 goto out;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700812 }
813
814 /* Verify CDC_ERROR_CODE field in CORE_CSR_CDC_STATUS0 is 0 */
815 cdc_err = readl_relaxed(host->ioaddr + CORE_CSR_CDC_STATUS0)
816 & CORE_CDC_ERROR_CODE_MASK;
817 if (cdc_err) {
818 pr_err("%s: %s: CDC Error Code %d\n",
819 mmc_hostname(host->mmc), __func__, cdc_err);
820 ret = -EINVAL;
821 goto out;
822 }
823
824 /* Write 1 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
825 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
826 | CORE_START_CDC_TRAFFIC),
827 host->ioaddr + CORE_DDR_200_CFG);
828out:
829 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
830 __func__, ret);
831 return ret;
832}
833
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700834static int sdhci_msm_cm_dll_sdc4_calibration(struct sdhci_host *host)
835{
836 u32 dll_status;
837 int ret = 0;
838
839 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
840
841 /*
842 * Currently the CORE_DDR_CONFIG register defaults to desired
843 * configuration on reset. Currently reprogramming the power on
844 * reset (POR) value in case it might have been modified by
845 * bootloaders. In the future, if this changes, then the desired
846 * values will need to be programmed appropriately.
847 */
848 writel_relaxed(DDR_CONFIG_POR_VAL, host->ioaddr + CORE_DDR_CONFIG);
849
850 /* Write 1 to DDR_CAL_EN field in CORE_DLL_CONFIG_2 */
851 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
852 | CORE_DDR_CAL_EN),
853 host->ioaddr + CORE_DLL_CONFIG_2);
854
855 /* Poll on DDR_DLL_LOCK bit in CORE_DLL_STATUS to be set */
856 ret = readl_poll_timeout(host->ioaddr + CORE_DLL_STATUS,
857 dll_status, (dll_status & CORE_DDR_DLL_LOCK), 10, 1000);
858
859 if (ret == -ETIMEDOUT) {
860 pr_err("%s: %s: CM_DLL_SDC4 Calibration was not completed\n",
861 mmc_hostname(host->mmc), __func__);
862 goto out;
863 }
864
865 /* set CORE_PWRSAVE_DLL bit in CORE_VENDOR_SPEC3 */
866 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC3)
867 | CORE_PWRSAVE_DLL),
868 host->ioaddr + CORE_VENDOR_SPEC3);
869 mb();
870out:
871 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
872 __func__, ret);
873 return ret;
874}
875
876static int sdhci_msm_hs400_dll_calibration(struct sdhci_host *host)
877{
878 int ret = 0;
879 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
880 struct sdhci_msm_host *msm_host = pltfm_host->priv;
881
882 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
883
884 /*
885 * Retuning in HS400 (DDR mode) will fail, just reset the
886 * tuning block and restore the saved tuning phase.
887 */
888 ret = msm_init_cm_dll(host);
889 if (ret)
890 goto out;
891
892 /* Set the selected phase in delay line hw block */
893 ret = msm_config_cm_dll_phase(host, msm_host->saved_tuning_phase);
894 if (ret)
895 goto out;
896
Krishna Konda0e8efba2014-06-23 14:50:38 -0700897 /* Write 1 to CMD_DAT_TRACK_SEL field in DLL_CONFIG */
898 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
899 | CORE_CMD_DAT_TRACK_SEL),
900 host->ioaddr + CORE_DLL_CONFIG);
901
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700902 if (msm_host->use_cdclp533)
903 /* Calibrate CDCLP533 DLL HW */
904 ret = sdhci_msm_cdclp533_calibration(host);
905 else
906 /* Calibrate CM_DLL_SDC4 HW */
907 ret = sdhci_msm_cm_dll_sdc4_calibration(host);
908out:
909 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
910 __func__, ret);
911 return ret;
912}
913
Krishna Konda96e6b112013-10-28 15:25:03 -0700914static void sdhci_msm_set_mmc_drv_type(struct sdhci_host *host, u32 opcode,
915 u8 drv_type)
916{
917 struct mmc_command cmd = {0};
918 struct mmc_request mrq = {NULL};
919 struct mmc_host *mmc = host->mmc;
920 u8 val = ((drv_type << 4) | 2);
921
922 cmd.opcode = MMC_SWITCH;
923 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
924 (EXT_CSD_HS_TIMING << 16) |
925 (val << 8) |
926 EXT_CSD_CMD_SET_NORMAL;
927 cmd.flags = MMC_CMD_AC | MMC_RSP_R1B;
928 /* 1 sec */
929 cmd.busy_timeout = 1000 * 1000;
930
931 memset(cmd.resp, 0, sizeof(cmd.resp));
932 cmd.retries = 3;
933
934 mrq.cmd = &cmd;
935 cmd.data = NULL;
936
937 mmc_wait_for_req(mmc, &mrq);
938 pr_debug("%s: %s: set card drive type to %d\n",
939 mmc_hostname(mmc), __func__,
940 drv_type);
941}
942
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700943int sdhci_msm_execute_tuning(struct sdhci_host *host, u32 opcode)
944{
945 unsigned long flags;
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530946 int tuning_seq_cnt = 3;
Krishna Konda96e6b112013-10-28 15:25:03 -0700947 u8 phase, *data_buf, tuned_phases[NUM_TUNING_PHASES], tuned_phase_cnt;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700948 const u32 *tuning_block_pattern = tuning_block_64;
949 int size = sizeof(tuning_block_64); /* Tuning pattern size in bytes */
950 int rc;
951 struct mmc_host *mmc = host->mmc;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530952 struct mmc_ios ios = host->mmc->ios;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700953 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
954 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Krishna Konda96e6b112013-10-28 15:25:03 -0700955 u8 drv_type = 0;
956 bool drv_type_changed = false;
957 struct mmc_card *card = host->mmc->card;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530958
959 /*
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700960 * Tuning is required for SDR104, HS200 and HS400 cards and
961 * if clock frequency is greater than 100MHz in these modes.
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530962 */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700963 if (host->clock <= CORE_FREQ_100MHZ ||
964 !((ios.timing == MMC_TIMING_MMC_HS400) ||
965 (ios.timing == MMC_TIMING_MMC_HS200) ||
966 (ios.timing == MMC_TIMING_UHS_SDR104)))
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530967 return 0;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700968
969 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700970
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700971 /* CDC/SDC4 DLL HW calibration is only required for HS400 mode*/
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700972 if (msm_host->tuning_done && !msm_host->calibration_done &&
973 (mmc->ios.timing == MMC_TIMING_MMC_HS400)) {
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700974 rc = sdhci_msm_hs400_dll_calibration(host);
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700975 spin_lock_irqsave(&host->lock, flags);
976 if (!rc)
977 msm_host->calibration_done = true;
978 spin_unlock_irqrestore(&host->lock, flags);
979 goto out;
980 }
981
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700982 spin_lock_irqsave(&host->lock, flags);
983
984 if ((opcode == MMC_SEND_TUNING_BLOCK_HS200) &&
985 (mmc->ios.bus_width == MMC_BUS_WIDTH_8)) {
986 tuning_block_pattern = tuning_block_128;
987 size = sizeof(tuning_block_128);
988 }
989 spin_unlock_irqrestore(&host->lock, flags);
990
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700991 data_buf = kmalloc(size, GFP_KERNEL);
992 if (!data_buf) {
993 rc = -ENOMEM;
994 goto out;
995 }
996
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530997retry:
Krishna Konda96e6b112013-10-28 15:25:03 -0700998 tuned_phase_cnt = 0;
999
Sahitya Tummala9fe16532013-06-13 10:36:57 +05301000 /* first of all reset the tuning block */
1001 rc = msm_init_cm_dll(host);
1002 if (rc)
1003 goto kfree;
1004
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001005 phase = 0;
1006 do {
1007 struct mmc_command cmd = {0};
1008 struct mmc_data data = {0};
1009 struct mmc_request mrq = {
1010 .cmd = &cmd,
1011 .data = &data
1012 };
1013 struct scatterlist sg;
1014
1015 /* set the phase in delay line hw block */
1016 rc = msm_config_cm_dll_phase(host, phase);
1017 if (rc)
1018 goto kfree;
1019
1020 cmd.opcode = opcode;
1021 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1022
1023 data.blksz = size;
1024 data.blocks = 1;
1025 data.flags = MMC_DATA_READ;
1026 data.timeout_ns = 1000 * 1000 * 1000; /* 1 sec */
1027
1028 data.sg = &sg;
1029 data.sg_len = 1;
1030 sg_init_one(&sg, data_buf, size);
1031 memset(data_buf, 0, size);
1032 mmc_wait_for_req(mmc, &mrq);
1033
1034 if (!cmd.error && !data.error &&
1035 !memcmp(data_buf, tuning_block_pattern, size)) {
1036 /* tuning is successful at this tuning point */
1037 tuned_phases[tuned_phase_cnt++] = phase;
Krishna Konda96e6b112013-10-28 15:25:03 -07001038 pr_debug("%s: %s: found *** good *** phase = %d\n",
1039 mmc_hostname(mmc), __func__, phase);
1040 } else {
1041 pr_debug("%s: %s: found ## bad ## phase = %d\n",
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001042 mmc_hostname(mmc), __func__, phase);
1043 }
1044 } while (++phase < 16);
1045
Sahitya Tummaladfdb4af2014-04-01 14:29:13 +05301046 if ((tuned_phase_cnt == NUM_TUNING_PHASES) &&
1047 card && mmc_card_mmc(card)) {
Krishna Konda96e6b112013-10-28 15:25:03 -07001048 /*
1049 * If all phases pass then its a problem. So change the card's
1050 * drive type to a different value, if supported and repeat
1051 * tuning until at least one phase fails. Then set the original
1052 * drive type back.
1053 *
1054 * If all the phases still pass after trying all possible
1055 * drive types, then one of those 16 phases will be picked.
1056 * This is no different from what was going on before the
1057 * modification to change drive type and retune.
1058 */
1059 pr_debug("%s: tuned phases count: %d\n", mmc_hostname(mmc),
1060 tuned_phase_cnt);
1061
1062 /* set drive type to other value . default setting is 0x0 */
1063 while (++drv_type <= MAX_DRV_TYPES_SUPPORTED_HS200) {
1064 if (card->ext_csd.raw_driver_strength &
1065 (1 << drv_type)) {
1066 sdhci_msm_set_mmc_drv_type(host, opcode,
1067 drv_type);
1068 if (!drv_type_changed)
1069 drv_type_changed = true;
1070 goto retry;
1071 }
1072 }
1073 }
1074
1075 /* reset drive type to default (50 ohm) if changed */
1076 if (drv_type_changed)
1077 sdhci_msm_set_mmc_drv_type(host, opcode, 0);
1078
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001079 if (tuned_phase_cnt) {
1080 rc = msm_find_most_appropriate_phase(host, tuned_phases,
1081 tuned_phase_cnt);
1082 if (rc < 0)
1083 goto kfree;
1084 else
1085 phase = (u8)rc;
1086
1087 /*
1088 * Finally set the selected phase in delay
1089 * line hw block.
1090 */
1091 rc = msm_config_cm_dll_phase(host, phase);
1092 if (rc)
1093 goto kfree;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07001094 msm_host->saved_tuning_phase = phase;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001095 pr_debug("%s: %s: finally setting the tuning phase to %d\n",
1096 mmc_hostname(mmc), __func__, phase);
1097 } else {
Sahitya Tummala9fe16532013-06-13 10:36:57 +05301098 if (--tuning_seq_cnt)
1099 goto retry;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001100 /* tuning failed */
1101 pr_err("%s: %s: no tuning point found\n",
1102 mmc_hostname(mmc), __func__);
Sahitya Tummala9fe16532013-06-13 10:36:57 +05301103 rc = -EIO;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001104 }
1105
1106kfree:
1107 kfree(data_buf);
1108out:
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07001109 spin_lock_irqsave(&host->lock, flags);
1110 if (!rc)
1111 msm_host->tuning_done = true;
1112 spin_unlock_irqrestore(&host->lock, flags);
1113 pr_debug("%s: Exit %s, err(%d)\n", mmc_hostname(mmc), __func__, rc);
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001114 return rc;
1115}
1116
Asutosh Das0ef24812012-12-18 16:14:02 +05301117static int sdhci_msm_setup_gpio(struct sdhci_msm_pltfm_data *pdata, bool enable)
1118{
1119 struct sdhci_msm_gpio_data *curr;
1120 int i, ret = 0;
1121
1122 curr = pdata->pin_data->gpio_data;
1123 for (i = 0; i < curr->size; i++) {
1124 if (!gpio_is_valid(curr->gpio[i].no)) {
1125 ret = -EINVAL;
1126 pr_err("%s: Invalid gpio = %d\n", __func__,
1127 curr->gpio[i].no);
1128 goto free_gpios;
1129 }
1130 if (enable) {
1131 ret = gpio_request(curr->gpio[i].no,
1132 curr->gpio[i].name);
1133 if (ret) {
1134 pr_err("%s: gpio_request(%d, %s) failed %d\n",
1135 __func__, curr->gpio[i].no,
1136 curr->gpio[i].name, ret);
1137 goto free_gpios;
1138 }
1139 curr->gpio[i].is_enabled = true;
1140 } else {
1141 gpio_free(curr->gpio[i].no);
1142 curr->gpio[i].is_enabled = false;
1143 }
1144 }
1145 return ret;
1146
1147free_gpios:
1148 for (i--; i >= 0; i--) {
1149 gpio_free(curr->gpio[i].no);
1150 curr->gpio[i].is_enabled = false;
1151 }
1152 return ret;
1153}
1154
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301155static int sdhci_msm_setup_pinctrl(struct sdhci_msm_pltfm_data *pdata,
1156 bool enable)
1157{
1158 int ret = 0;
1159
1160 if (enable)
1161 ret = pinctrl_select_state(pdata->pctrl_data->pctrl,
1162 pdata->pctrl_data->pins_active);
1163 else
1164 ret = pinctrl_select_state(pdata->pctrl_data->pctrl,
1165 pdata->pctrl_data->pins_sleep);
1166
1167 if (ret < 0)
1168 pr_err("%s state for pinctrl failed with %d\n",
1169 enable ? "Enabling" : "Disabling", ret);
1170
1171 return ret;
1172}
1173
Asutosh Das0ef24812012-12-18 16:14:02 +05301174static int sdhci_msm_setup_pins(struct sdhci_msm_pltfm_data *pdata, bool enable)
1175{
1176 int ret = 0;
1177
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301178 if (pdata->pin_cfg_sts == enable) {
Asutosh Das0ef24812012-12-18 16:14:02 +05301179 return 0;
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301180 } else if (pdata->pctrl_data) {
1181 ret = sdhci_msm_setup_pinctrl(pdata, enable);
1182 goto out;
1183 } else if (!pdata->pin_data) {
1184 return 0;
1185 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301186
Sahitya Tummala1cd7e072014-02-14 13:19:01 +05301187 if (pdata->pin_data->is_gpio)
1188 ret = sdhci_msm_setup_gpio(pdata, enable);
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301189out:
Asutosh Das0ef24812012-12-18 16:14:02 +05301190 if (!ret)
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301191 pdata->pin_cfg_sts = enable;
Asutosh Das0ef24812012-12-18 16:14:02 +05301192
1193 return ret;
1194}
1195
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301196static int sdhci_msm_dt_get_array(struct device *dev, const char *prop_name,
1197 u32 **out, int *len, u32 size)
1198{
1199 int ret = 0;
1200 struct device_node *np = dev->of_node;
1201 size_t sz;
1202 u32 *arr = NULL;
1203
1204 if (!of_get_property(np, prop_name, len)) {
1205 ret = -EINVAL;
1206 goto out;
1207 }
1208 sz = *len = *len / sizeof(*arr);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07001209 if (sz <= 0 || (size > 0 && (sz > size))) {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301210 dev_err(dev, "%s invalid size\n", prop_name);
1211 ret = -EINVAL;
1212 goto out;
1213 }
1214
1215 arr = devm_kzalloc(dev, sz * sizeof(*arr), GFP_KERNEL);
1216 if (!arr) {
1217 dev_err(dev, "%s failed allocating memory\n", prop_name);
1218 ret = -ENOMEM;
1219 goto out;
1220 }
1221
1222 ret = of_property_read_u32_array(np, prop_name, arr, sz);
1223 if (ret < 0) {
1224 dev_err(dev, "%s failed reading array %d\n", prop_name, ret);
1225 goto out;
1226 }
1227 *out = arr;
1228out:
1229 if (ret)
1230 *len = 0;
1231 return ret;
1232}
1233
Asutosh Das0ef24812012-12-18 16:14:02 +05301234#define MAX_PROP_SIZE 32
1235static int sdhci_msm_dt_parse_vreg_info(struct device *dev,
1236 struct sdhci_msm_reg_data **vreg_data, const char *vreg_name)
1237{
1238 int len, ret = 0;
1239 const __be32 *prop;
1240 char prop_name[MAX_PROP_SIZE];
1241 struct sdhci_msm_reg_data *vreg;
1242 struct device_node *np = dev->of_node;
1243
1244 snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", vreg_name);
1245 if (!of_parse_phandle(np, prop_name, 0)) {
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301246 dev_info(dev, "No vreg data found for %s\n", vreg_name);
Asutosh Das0ef24812012-12-18 16:14:02 +05301247 return ret;
1248 }
1249
1250 vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
1251 if (!vreg) {
1252 dev_err(dev, "No memory for vreg: %s\n", vreg_name);
1253 ret = -ENOMEM;
1254 return ret;
1255 }
1256
1257 vreg->name = vreg_name;
1258
1259 snprintf(prop_name, MAX_PROP_SIZE,
1260 "qcom,%s-always-on", vreg_name);
1261 if (of_get_property(np, prop_name, NULL))
1262 vreg->is_always_on = true;
1263
1264 snprintf(prop_name, MAX_PROP_SIZE,
1265 "qcom,%s-lpm-sup", vreg_name);
1266 if (of_get_property(np, prop_name, NULL))
1267 vreg->lpm_sup = true;
1268
1269 snprintf(prop_name, MAX_PROP_SIZE,
1270 "qcom,%s-voltage-level", vreg_name);
1271 prop = of_get_property(np, prop_name, &len);
1272 if (!prop || (len != (2 * sizeof(__be32)))) {
1273 dev_warn(dev, "%s %s property\n",
1274 prop ? "invalid format" : "no", prop_name);
1275 } else {
1276 vreg->low_vol_level = be32_to_cpup(&prop[0]);
1277 vreg->high_vol_level = be32_to_cpup(&prop[1]);
1278 }
1279
1280 snprintf(prop_name, MAX_PROP_SIZE,
1281 "qcom,%s-current-level", vreg_name);
1282 prop = of_get_property(np, prop_name, &len);
1283 if (!prop || (len != (2 * sizeof(__be32)))) {
1284 dev_warn(dev, "%s %s property\n",
1285 prop ? "invalid format" : "no", prop_name);
1286 } else {
1287 vreg->lpm_uA = be32_to_cpup(&prop[0]);
1288 vreg->hpm_uA = be32_to_cpup(&prop[1]);
1289 }
1290
1291 *vreg_data = vreg;
1292 dev_dbg(dev, "%s: %s %s vol=[%d %d]uV, curr=[%d %d]uA\n",
1293 vreg->name, vreg->is_always_on ? "always_on," : "",
1294 vreg->lpm_sup ? "lpm_sup," : "", vreg->low_vol_level,
1295 vreg->high_vol_level, vreg->lpm_uA, vreg->hpm_uA);
1296
1297 return ret;
1298}
1299
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301300static int sdhci_msm_parse_pinctrl_info(struct device *dev,
1301 struct sdhci_msm_pltfm_data *pdata)
1302{
1303 struct sdhci_pinctrl_data *pctrl_data;
1304 struct pinctrl *pctrl;
1305 int ret = 0;
1306
1307 /* Try to obtain pinctrl handle */
1308 pctrl = devm_pinctrl_get(dev);
1309 if (IS_ERR(pctrl)) {
1310 ret = PTR_ERR(pctrl);
1311 goto out;
1312 }
1313 pctrl_data = devm_kzalloc(dev, sizeof(*pctrl_data), GFP_KERNEL);
1314 if (!pctrl_data) {
1315 dev_err(dev, "No memory for sdhci_pinctrl_data\n");
1316 ret = -ENOMEM;
1317 goto out;
1318 }
1319 pctrl_data->pctrl = pctrl;
1320 /* Look-up and keep the states handy to be used later */
1321 pctrl_data->pins_active = pinctrl_lookup_state(
1322 pctrl_data->pctrl, "active");
1323 if (IS_ERR(pctrl_data->pins_active)) {
1324 ret = PTR_ERR(pctrl_data->pins_active);
1325 dev_err(dev, "Could not get active pinstates, err:%d\n", ret);
1326 goto out;
1327 }
1328 pctrl_data->pins_sleep = pinctrl_lookup_state(
1329 pctrl_data->pctrl, "sleep");
1330 if (IS_ERR(pctrl_data->pins_sleep)) {
1331 ret = PTR_ERR(pctrl_data->pins_sleep);
1332 dev_err(dev, "Could not get sleep pinstates, err:%d\n", ret);
1333 goto out;
1334 }
1335 pdata->pctrl_data = pctrl_data;
1336out:
1337 return ret;
1338}
1339
Asutosh Das0ef24812012-12-18 16:14:02 +05301340#define GPIO_NAME_MAX_LEN 32
1341static int sdhci_msm_dt_parse_gpio_info(struct device *dev,
1342 struct sdhci_msm_pltfm_data *pdata)
1343{
1344 int ret = 0, cnt, i;
1345 struct sdhci_msm_pin_data *pin_data;
1346 struct device_node *np = dev->of_node;
1347
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301348 ret = sdhci_msm_parse_pinctrl_info(dev, pdata);
1349 if (!ret) {
1350 goto out;
1351 } else if (ret == -EPROBE_DEFER) {
1352 dev_err(dev, "Pinctrl framework not registered, err:%d\n", ret);
1353 goto out;
1354 } else {
1355 dev_err(dev, "Parsing Pinctrl failed with %d, falling back on GPIO lib\n",
1356 ret);
Sahitya Tummala1cd7e072014-02-14 13:19:01 +05301357 ret = 0;
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301358 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301359 pin_data = devm_kzalloc(dev, sizeof(*pin_data), GFP_KERNEL);
1360 if (!pin_data) {
1361 dev_err(dev, "No memory for pin_data\n");
1362 ret = -ENOMEM;
1363 goto out;
1364 }
1365
1366 cnt = of_gpio_count(np);
1367 if (cnt > 0) {
1368 pin_data->gpio_data = devm_kzalloc(dev,
1369 sizeof(struct sdhci_msm_gpio_data), GFP_KERNEL);
1370 if (!pin_data->gpio_data) {
1371 dev_err(dev, "No memory for gpio_data\n");
1372 ret = -ENOMEM;
1373 goto out;
1374 }
1375 pin_data->gpio_data->size = cnt;
1376 pin_data->gpio_data->gpio = devm_kzalloc(dev, cnt *
1377 sizeof(struct sdhci_msm_gpio), GFP_KERNEL);
1378
1379 if (!pin_data->gpio_data->gpio) {
1380 dev_err(dev, "No memory for gpio\n");
1381 ret = -ENOMEM;
1382 goto out;
1383 }
1384
1385 for (i = 0; i < cnt; i++) {
1386 const char *name = NULL;
1387 char result[GPIO_NAME_MAX_LEN];
1388 pin_data->gpio_data->gpio[i].no = of_get_gpio(np, i);
1389 of_property_read_string_index(np,
1390 "qcom,gpio-names", i, &name);
1391
1392 snprintf(result, GPIO_NAME_MAX_LEN, "%s-%s",
1393 dev_name(dev), name ? name : "?");
1394 pin_data->gpio_data->gpio[i].name = result;
1395 dev_dbg(dev, "%s: gpio[%s] = %d\n", __func__,
1396 pin_data->gpio_data->gpio[i].name,
1397 pin_data->gpio_data->gpio[i].no);
Asutosh Das0ef24812012-12-18 16:14:02 +05301398 }
1399 }
Sahitya Tummala1cd7e072014-02-14 13:19:01 +05301400 pdata->pin_data = pin_data;
Asutosh Das0ef24812012-12-18 16:14:02 +05301401out:
1402 if (ret)
1403 dev_err(dev, "%s failed with err %d\n", __func__, ret);
1404 return ret;
1405}
1406
1407/* Parse platform data */
1408static struct sdhci_msm_pltfm_data *sdhci_msm_populate_pdata(struct device *dev)
1409{
1410 struct sdhci_msm_pltfm_data *pdata = NULL;
1411 struct device_node *np = dev->of_node;
1412 u32 bus_width = 0;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05301413 u32 cpu_dma_latency;
Asutosh Das0ef24812012-12-18 16:14:02 +05301414 int len, i;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301415 int clk_table_len;
1416 u32 *clk_table = NULL;
Sujit Reddy Thumma1958d3d2013-06-03 09:54:32 +05301417 enum of_gpio_flags flags = OF_GPIO_ACTIVE_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05301418
1419 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1420 if (!pdata) {
1421 dev_err(dev, "failed to allocate memory for platform data\n");
1422 goto out;
1423 }
1424
Sujit Reddy Thumma1958d3d2013-06-03 09:54:32 +05301425 pdata->status_gpio = of_get_named_gpio_flags(np, "cd-gpios", 0, &flags);
1426 if (gpio_is_valid(pdata->status_gpio) & !(flags & OF_GPIO_ACTIVE_LOW))
1427 pdata->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
Sahitya Tummala581df132013-03-12 14:57:46 +05301428
Asutosh Das0ef24812012-12-18 16:14:02 +05301429 of_property_read_u32(np, "qcom,bus-width", &bus_width);
1430 if (bus_width == 8)
1431 pdata->mmc_bus_width = MMC_CAP_8_BIT_DATA;
1432 else if (bus_width == 4)
1433 pdata->mmc_bus_width = MMC_CAP_4_BIT_DATA;
1434 else {
1435 dev_notice(dev, "invalid bus-width, default to 1-bit mode\n");
1436 pdata->mmc_bus_width = 0;
1437 }
1438
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05301439 if (!of_property_read_u32(np, "qcom,cpu-dma-latency-us",
1440 &cpu_dma_latency))
1441 pdata->cpu_dma_latency_us = cpu_dma_latency;
Asutosh Das598a4d42014-02-05 16:38:23 +05301442 else
1443 pdata->cpu_dma_latency_us = MSM_MMC_DEFAULT_CPU_DMA_LATENCY;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301444 if (sdhci_msm_dt_get_array(dev, "qcom,clk-rates",
1445 &clk_table, &clk_table_len, 0)) {
1446 dev_err(dev, "failed parsing supported clock rates\n");
1447 goto out;
1448 }
1449 if (!clk_table || !clk_table_len) {
1450 dev_err(dev, "Invalid clock table\n");
1451 goto out;
1452 }
1453 pdata->sup_clk_table = clk_table;
1454 pdata->sup_clk_cnt = clk_table_len;
1455
Asutosh Das0ef24812012-12-18 16:14:02 +05301456 pdata->vreg_data = devm_kzalloc(dev, sizeof(struct
1457 sdhci_msm_slot_reg_data),
1458 GFP_KERNEL);
1459 if (!pdata->vreg_data) {
1460 dev_err(dev, "failed to allocate memory for vreg data\n");
1461 goto out;
1462 }
1463
1464 if (sdhci_msm_dt_parse_vreg_info(dev, &pdata->vreg_data->vdd_data,
1465 "vdd")) {
1466 dev_err(dev, "failed parsing vdd data\n");
1467 goto out;
1468 }
1469 if (sdhci_msm_dt_parse_vreg_info(dev,
1470 &pdata->vreg_data->vdd_io_data,
1471 "vdd-io")) {
1472 dev_err(dev, "failed parsing vdd-io data\n");
1473 goto out;
1474 }
1475
1476 if (sdhci_msm_dt_parse_gpio_info(dev, pdata)) {
1477 dev_err(dev, "failed parsing gpio data\n");
1478 goto out;
1479 }
1480
Asutosh Das0ef24812012-12-18 16:14:02 +05301481 len = of_property_count_strings(np, "qcom,bus-speed-mode");
1482
1483 for (i = 0; i < len; i++) {
1484 const char *name = NULL;
1485
1486 of_property_read_string_index(np,
1487 "qcom,bus-speed-mode", i, &name);
1488 if (!name)
1489 continue;
1490
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07001491 if (!strncmp(name, "HS400_1p8v", sizeof("HS400_1p8v")))
1492 pdata->caps2 |= MMC_CAP2_HS400_1_8V;
1493 else if (!strncmp(name, "HS400_1p2v", sizeof("HS400_1p2v")))
1494 pdata->caps2 |= MMC_CAP2_HS400_1_2V;
1495 else if (!strncmp(name, "HS200_1p8v", sizeof("HS200_1p8v")))
Asutosh Das0ef24812012-12-18 16:14:02 +05301496 pdata->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
1497 else if (!strncmp(name, "HS200_1p2v", sizeof("HS200_1p2v")))
1498 pdata->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
1499 else if (!strncmp(name, "DDR_1p8v", sizeof("DDR_1p8v")))
1500 pdata->caps |= MMC_CAP_1_8V_DDR
1501 | MMC_CAP_UHS_DDR50;
1502 else if (!strncmp(name, "DDR_1p2v", sizeof("DDR_1p2v")))
1503 pdata->caps |= MMC_CAP_1_2V_DDR
1504 | MMC_CAP_UHS_DDR50;
1505 }
1506
1507 if (of_get_property(np, "qcom,nonremovable", NULL))
1508 pdata->nonremovable = true;
1509
Guoping Yuf7c91332014-08-20 16:56:18 +08001510 if (of_get_property(np, "qcom,nonhotplug", NULL))
1511 pdata->nonhotplug = true;
1512
Asutosh Das0ef24812012-12-18 16:14:02 +05301513 return pdata;
1514out:
1515 return NULL;
1516}
1517
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301518/* Returns required bandwidth in Bytes per Sec */
1519static unsigned int sdhci_get_bw_required(struct sdhci_host *host,
1520 struct mmc_ios *ios)
1521{
Sahitya Tummala2886c922013-04-03 18:03:31 +05301522 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1523 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1524
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301525 unsigned int bw;
1526
Sahitya Tummala2886c922013-04-03 18:03:31 +05301527 bw = msm_host->clk_rate;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301528 /*
1529 * For DDR mode, SDCC controller clock will be at
1530 * the double rate than the actual clock that goes to card.
1531 */
1532 if (ios->bus_width == MMC_BUS_WIDTH_4)
1533 bw /= 2;
1534 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1535 bw /= 8;
1536
1537 return bw;
1538}
1539
1540static int sdhci_msm_bus_get_vote_for_bw(struct sdhci_msm_host *host,
1541 unsigned int bw)
1542{
1543 unsigned int *table = host->pdata->voting_data->bw_vecs;
1544 unsigned int size = host->pdata->voting_data->bw_vecs_size;
1545 int i;
1546
1547 if (host->msm_bus_vote.is_max_bw_needed && bw)
1548 return host->msm_bus_vote.max_bw_vote;
1549
1550 for (i = 0; i < size; i++) {
1551 if (bw <= table[i])
1552 break;
1553 }
1554
1555 if (i && (i == size))
1556 i--;
1557
1558 return i;
1559}
1560
1561/*
1562 * This function must be called with host lock acquired.
1563 * Caller of this function should also ensure that msm bus client
1564 * handle is not null.
1565 */
1566static inline int sdhci_msm_bus_set_vote(struct sdhci_msm_host *msm_host,
1567 int vote,
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301568 unsigned long *flags)
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301569{
1570 struct sdhci_host *host = platform_get_drvdata(msm_host->pdev);
1571 int rc = 0;
1572
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301573 BUG_ON(!flags);
1574
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301575 if (vote != msm_host->msm_bus_vote.curr_vote) {
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301576 spin_unlock_irqrestore(&host->lock, *flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301577 rc = msm_bus_scale_client_update_request(
1578 msm_host->msm_bus_vote.client_handle, vote);
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301579 spin_lock_irqsave(&host->lock, *flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301580 if (rc) {
1581 pr_err("%s: msm_bus_scale_client_update_request() failed: bus_client_handle=0x%x, vote=%d, err=%d\n",
1582 mmc_hostname(host->mmc),
1583 msm_host->msm_bus_vote.client_handle, vote, rc);
1584 goto out;
1585 }
1586 msm_host->msm_bus_vote.curr_vote = vote;
1587 }
1588out:
1589 return rc;
1590}
1591
1592/*
1593 * Internal work. Work to set 0 bandwidth for msm bus.
1594 */
1595static void sdhci_msm_bus_work(struct work_struct *work)
1596{
1597 struct sdhci_msm_host *msm_host;
1598 struct sdhci_host *host;
1599 unsigned long flags;
1600
1601 msm_host = container_of(work, struct sdhci_msm_host,
1602 msm_bus_vote.vote_work.work);
1603 host = platform_get_drvdata(msm_host->pdev);
1604
1605 if (!msm_host->msm_bus_vote.client_handle)
1606 return;
1607
1608 spin_lock_irqsave(&host->lock, flags);
1609 /* don't vote for 0 bandwidth if any request is in progress */
1610 if (!host->mrq) {
1611 sdhci_msm_bus_set_vote(msm_host,
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301612 msm_host->msm_bus_vote.min_bw_vote, &flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301613 } else
1614 pr_warning("%s: %s: Transfer in progress. skipping bus voting to 0 bandwidth\n",
1615 mmc_hostname(host->mmc), __func__);
1616 spin_unlock_irqrestore(&host->lock, flags);
1617}
1618
1619/*
1620 * This function cancels any scheduled delayed work and sets the bus
1621 * vote based on bw (bandwidth) argument.
1622 */
1623static void sdhci_msm_bus_cancel_work_and_set_vote(struct sdhci_host *host,
1624 unsigned int bw)
1625{
1626 int vote;
1627 unsigned long flags;
1628 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1629 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1630
1631 cancel_delayed_work_sync(&msm_host->msm_bus_vote.vote_work);
1632 spin_lock_irqsave(&host->lock, flags);
1633 vote = sdhci_msm_bus_get_vote_for_bw(msm_host, bw);
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301634 sdhci_msm_bus_set_vote(msm_host, vote, &flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301635 spin_unlock_irqrestore(&host->lock, flags);
1636}
1637
1638#define MSM_MMC_BUS_VOTING_DELAY 200 /* msecs */
1639
1640/* This function queues a work which will set the bandwidth requiement to 0 */
1641static void sdhci_msm_bus_queue_work(struct sdhci_host *host)
1642{
1643 unsigned long flags;
1644 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1645 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1646
1647 spin_lock_irqsave(&host->lock, flags);
1648 if (msm_host->msm_bus_vote.min_bw_vote !=
1649 msm_host->msm_bus_vote.curr_vote)
1650 queue_delayed_work(system_wq,
1651 &msm_host->msm_bus_vote.vote_work,
1652 msecs_to_jiffies(MSM_MMC_BUS_VOTING_DELAY));
1653 spin_unlock_irqrestore(&host->lock, flags);
1654}
1655
1656static int sdhci_msm_bus_register(struct sdhci_msm_host *host,
1657 struct platform_device *pdev)
1658{
1659 int rc = 0;
1660 struct msm_bus_scale_pdata *bus_pdata;
1661
1662 struct sdhci_msm_bus_voting_data *data;
1663 struct device *dev = &pdev->dev;
1664
1665 data = devm_kzalloc(dev,
1666 sizeof(struct sdhci_msm_bus_voting_data), GFP_KERNEL);
1667 if (!data) {
1668 dev_err(&pdev->dev,
1669 "%s: failed to allocate memory\n", __func__);
1670 rc = -ENOMEM;
1671 goto out;
1672 }
1673 data->bus_pdata = msm_bus_cl_get_pdata(pdev);
1674 if (data->bus_pdata) {
1675 rc = sdhci_msm_dt_get_array(dev, "qcom,bus-bw-vectors-bps",
1676 &data->bw_vecs, &data->bw_vecs_size, 0);
1677 if (rc) {
1678 dev_err(&pdev->dev,
1679 "%s: Failed to get bus-bw-vectors-bps\n",
1680 __func__);
1681 goto out;
1682 }
1683 host->pdata->voting_data = data;
1684 }
1685 if (host->pdata->voting_data &&
1686 host->pdata->voting_data->bus_pdata &&
1687 host->pdata->voting_data->bw_vecs &&
1688 host->pdata->voting_data->bw_vecs_size) {
1689
1690 bus_pdata = host->pdata->voting_data->bus_pdata;
1691 host->msm_bus_vote.client_handle =
1692 msm_bus_scale_register_client(bus_pdata);
1693 if (!host->msm_bus_vote.client_handle) {
1694 dev_err(&pdev->dev, "msm_bus_scale_register_client()\n");
1695 rc = -EFAULT;
1696 goto out;
1697 }
1698 /* cache the vote index for minimum and maximum bandwidth */
1699 host->msm_bus_vote.min_bw_vote =
1700 sdhci_msm_bus_get_vote_for_bw(host, 0);
1701 host->msm_bus_vote.max_bw_vote =
1702 sdhci_msm_bus_get_vote_for_bw(host, UINT_MAX);
1703 } else {
1704 devm_kfree(dev, data);
1705 }
1706
1707out:
1708 return rc;
1709}
1710
1711static void sdhci_msm_bus_unregister(struct sdhci_msm_host *host)
1712{
1713 if (host->msm_bus_vote.client_handle)
1714 msm_bus_scale_unregister_client(
1715 host->msm_bus_vote.client_handle);
1716}
1717
1718static void sdhci_msm_bus_voting(struct sdhci_host *host, u32 enable)
1719{
1720 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1721 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1722 struct mmc_ios *ios = &host->mmc->ios;
1723 unsigned int bw;
1724
1725 if (!msm_host->msm_bus_vote.client_handle)
1726 return;
1727
1728 bw = sdhci_get_bw_required(host, ios);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301729 if (enable) {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301730 sdhci_msm_bus_cancel_work_and_set_vote(host, bw);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301731 } else {
1732 /*
1733 * If clock gating is enabled, then remove the vote
1734 * immediately because clocks will be disabled only
1735 * after SDHCI_MSM_MMC_CLK_GATE_DELAY and thus no
1736 * additional delay is required to remove the bus vote.
1737 */
1738#ifdef CONFIG_MMC_CLKGATE
1739 if (host->mmc->clkgate_delay)
1740 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
1741 else
1742#endif
1743 sdhci_msm_bus_queue_work(host);
1744 }
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301745}
1746
Asutosh Das0ef24812012-12-18 16:14:02 +05301747/* Regulator utility functions */
1748static int sdhci_msm_vreg_init_reg(struct device *dev,
1749 struct sdhci_msm_reg_data *vreg)
1750{
1751 int ret = 0;
1752
1753 /* check if regulator is already initialized? */
1754 if (vreg->reg)
1755 goto out;
1756
1757 /* Get the regulator handle */
1758 vreg->reg = devm_regulator_get(dev, vreg->name);
1759 if (IS_ERR(vreg->reg)) {
1760 ret = PTR_ERR(vreg->reg);
1761 pr_err("%s: devm_regulator_get(%s) failed. ret=%d\n",
1762 __func__, vreg->name, ret);
1763 goto out;
1764 }
1765
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301766 if (regulator_count_voltages(vreg->reg) > 0) {
1767 vreg->set_voltage_sup = true;
1768 /* sanity check */
1769 if (!vreg->high_vol_level || !vreg->hpm_uA) {
1770 pr_err("%s: %s invalid constraints specified\n",
1771 __func__, vreg->name);
1772 ret = -EINVAL;
1773 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301774 }
1775
1776out:
1777 return ret;
1778}
1779
1780static void sdhci_msm_vreg_deinit_reg(struct sdhci_msm_reg_data *vreg)
1781{
1782 if (vreg->reg)
1783 devm_regulator_put(vreg->reg);
1784}
1785
1786static int sdhci_msm_vreg_set_optimum_mode(struct sdhci_msm_reg_data
1787 *vreg, int uA_load)
1788{
1789 int ret = 0;
1790
1791 /*
1792 * regulators that do not support regulator_set_voltage also
1793 * do not support regulator_set_optimum_mode
1794 */
1795 if (vreg->set_voltage_sup) {
1796 ret = regulator_set_load(vreg->reg, uA_load);
1797 if (ret < 0)
1798 pr_err("%s: regulator_set_load(reg=%s,uA_load=%d) failed. ret=%d\n",
1799 __func__, vreg->name, uA_load, ret);
1800 else
1801 /*
1802 * regulator_set_load() can return non zero
1803 * value even for success case.
1804 */
1805 ret = 0;
1806 }
1807 return ret;
1808}
1809
1810static int sdhci_msm_vreg_set_voltage(struct sdhci_msm_reg_data *vreg,
1811 int min_uV, int max_uV)
1812{
1813 int ret = 0;
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301814 if (vreg->set_voltage_sup) {
1815 ret = regulator_set_voltage(vreg->reg, min_uV, max_uV);
1816 if (ret) {
1817 pr_err("%s: regulator_set_voltage(%s)failed. min_uV=%d,max_uV=%d,ret=%d\n",
Asutosh Das0ef24812012-12-18 16:14:02 +05301818 __func__, vreg->name, min_uV, max_uV, ret);
1819 }
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301820 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301821
1822 return ret;
1823}
1824
1825static int sdhci_msm_vreg_enable(struct sdhci_msm_reg_data *vreg)
1826{
1827 int ret = 0;
1828
1829 /* Put regulator in HPM (high power mode) */
1830 ret = sdhci_msm_vreg_set_optimum_mode(vreg, vreg->hpm_uA);
1831 if (ret < 0)
1832 return ret;
1833
1834 if (!vreg->is_enabled) {
1835 /* Set voltage level */
1836 ret = sdhci_msm_vreg_set_voltage(vreg, vreg->high_vol_level,
1837 vreg->high_vol_level);
1838 if (ret)
1839 return ret;
1840 }
1841 ret = regulator_enable(vreg->reg);
1842 if (ret) {
1843 pr_err("%s: regulator_enable(%s) failed. ret=%d\n",
1844 __func__, vreg->name, ret);
1845 return ret;
1846 }
1847 vreg->is_enabled = true;
1848 return ret;
1849}
1850
1851static int sdhci_msm_vreg_disable(struct sdhci_msm_reg_data *vreg)
1852{
1853 int ret = 0;
1854
1855 /* Never disable regulator marked as always_on */
1856 if (vreg->is_enabled && !vreg->is_always_on) {
1857 ret = regulator_disable(vreg->reg);
1858 if (ret) {
1859 pr_err("%s: regulator_disable(%s) failed. ret=%d\n",
1860 __func__, vreg->name, ret);
1861 goto out;
1862 }
1863 vreg->is_enabled = false;
1864
1865 ret = sdhci_msm_vreg_set_optimum_mode(vreg, 0);
1866 if (ret < 0)
1867 goto out;
1868
1869 /* Set min. voltage level to 0 */
1870 ret = sdhci_msm_vreg_set_voltage(vreg, 0, vreg->high_vol_level);
1871 if (ret)
1872 goto out;
1873 } else if (vreg->is_enabled && vreg->is_always_on) {
1874 if (vreg->lpm_sup) {
1875 /* Put always_on regulator in LPM (low power mode) */
1876 ret = sdhci_msm_vreg_set_optimum_mode(vreg,
1877 vreg->lpm_uA);
1878 if (ret < 0)
1879 goto out;
1880 }
1881 }
1882out:
1883 return ret;
1884}
1885
1886static int sdhci_msm_setup_vreg(struct sdhci_msm_pltfm_data *pdata,
1887 bool enable, bool is_init)
1888{
1889 int ret = 0, i;
1890 struct sdhci_msm_slot_reg_data *curr_slot;
1891 struct sdhci_msm_reg_data *vreg_table[2];
1892
1893 curr_slot = pdata->vreg_data;
1894 if (!curr_slot) {
1895 pr_debug("%s: vreg info unavailable,assuming the slot is powered by always on domain\n",
1896 __func__);
1897 goto out;
1898 }
1899
1900 vreg_table[0] = curr_slot->vdd_data;
1901 vreg_table[1] = curr_slot->vdd_io_data;
1902
1903 for (i = 0; i < ARRAY_SIZE(vreg_table); i++) {
1904 if (vreg_table[i]) {
1905 if (enable)
1906 ret = sdhci_msm_vreg_enable(vreg_table[i]);
1907 else
1908 ret = sdhci_msm_vreg_disable(vreg_table[i]);
1909 if (ret)
1910 goto out;
1911 }
1912 }
1913out:
1914 return ret;
1915}
1916
1917/*
1918 * Reset vreg by ensuring it is off during probe. A call
1919 * to enable vreg is needed to balance disable vreg
1920 */
1921static int sdhci_msm_vreg_reset(struct sdhci_msm_pltfm_data *pdata)
1922{
1923 int ret;
1924
1925 ret = sdhci_msm_setup_vreg(pdata, 1, true);
1926 if (ret)
1927 return ret;
1928 ret = sdhci_msm_setup_vreg(pdata, 0, true);
1929 return ret;
1930}
1931
1932/* This init function should be called only once for each SDHC slot */
1933static int sdhci_msm_vreg_init(struct device *dev,
1934 struct sdhci_msm_pltfm_data *pdata,
1935 bool is_init)
1936{
1937 int ret = 0;
1938 struct sdhci_msm_slot_reg_data *curr_slot;
1939 struct sdhci_msm_reg_data *curr_vdd_reg, *curr_vdd_io_reg;
1940
1941 curr_slot = pdata->vreg_data;
1942 if (!curr_slot)
1943 goto out;
1944
1945 curr_vdd_reg = curr_slot->vdd_data;
1946 curr_vdd_io_reg = curr_slot->vdd_io_data;
1947
1948 if (!is_init)
1949 /* Deregister all regulators from regulator framework */
1950 goto vdd_io_reg_deinit;
1951
1952 /*
1953 * Get the regulator handle from voltage regulator framework
1954 * and then try to set the voltage level for the regulator
1955 */
1956 if (curr_vdd_reg) {
1957 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_reg);
1958 if (ret)
1959 goto out;
1960 }
1961 if (curr_vdd_io_reg) {
1962 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_io_reg);
1963 if (ret)
1964 goto vdd_reg_deinit;
1965 }
1966 ret = sdhci_msm_vreg_reset(pdata);
1967 if (ret)
1968 dev_err(dev, "vreg reset failed (%d)\n", ret);
1969 goto out;
1970
1971vdd_io_reg_deinit:
1972 if (curr_vdd_io_reg)
1973 sdhci_msm_vreg_deinit_reg(curr_vdd_io_reg);
1974vdd_reg_deinit:
1975 if (curr_vdd_reg)
1976 sdhci_msm_vreg_deinit_reg(curr_vdd_reg);
1977out:
1978 return ret;
1979}
1980
1981
1982static int sdhci_msm_set_vdd_io_vol(struct sdhci_msm_pltfm_data *pdata,
1983 enum vdd_io_level level,
1984 unsigned int voltage_level)
1985{
1986 int ret = 0;
1987 int set_level;
1988 struct sdhci_msm_reg_data *vdd_io_reg;
1989
1990 if (!pdata->vreg_data)
1991 return ret;
1992
1993 vdd_io_reg = pdata->vreg_data->vdd_io_data;
1994 if (vdd_io_reg && vdd_io_reg->is_enabled) {
1995 switch (level) {
1996 case VDD_IO_LOW:
1997 set_level = vdd_io_reg->low_vol_level;
1998 break;
1999 case VDD_IO_HIGH:
2000 set_level = vdd_io_reg->high_vol_level;
2001 break;
2002 case VDD_IO_SET_LEVEL:
2003 set_level = voltage_level;
2004 break;
2005 default:
2006 pr_err("%s: invalid argument level = %d",
2007 __func__, level);
2008 ret = -EINVAL;
2009 return ret;
2010 }
2011 ret = sdhci_msm_vreg_set_voltage(vdd_io_reg, set_level,
2012 set_level);
2013 }
2014 return ret;
2015}
2016
2017static irqreturn_t sdhci_msm_pwr_irq(int irq, void *data)
2018{
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002019 struct sdhci_host *host = (struct sdhci_host *)data;
2020 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2021 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Das0ef24812012-12-18 16:14:02 +05302022 u8 irq_status = 0;
2023 u8 irq_ack = 0;
2024 int ret = 0;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302025 int pwr_state = 0, io_level = 0;
2026 unsigned long flags;
Asutosh Das0ef24812012-12-18 16:14:02 +05302027
2028 irq_status = readb_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
2029 pr_debug("%s: Received IRQ(%d), status=0x%x\n",
2030 mmc_hostname(msm_host->mmc), irq, irq_status);
2031
2032 /* Clear the interrupt */
2033 writeb_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
2034 /*
2035 * SDHC has core_mem and hc_mem device memory and these memory
2036 * addresses do not fall within 1KB region. Hence, any update to
2037 * core_mem address space would require an mb() to ensure this gets
2038 * completed before its next update to registers within hc_mem.
2039 */
2040 mb();
2041
2042 /* Handle BUS ON/OFF*/
2043 if (irq_status & CORE_PWRCTL_BUS_ON) {
2044 ret = sdhci_msm_setup_vreg(msm_host->pdata, true, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302045 if (!ret) {
Asutosh Das0ef24812012-12-18 16:14:02 +05302046 ret = sdhci_msm_setup_pins(msm_host->pdata, true);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302047 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
2048 VDD_IO_HIGH, 0);
2049 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302050 if (ret)
2051 irq_ack |= CORE_PWRCTL_BUS_FAIL;
2052 else
2053 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302054
2055 pwr_state = REQ_BUS_ON;
2056 io_level = REQ_IO_HIGH;
Asutosh Das0ef24812012-12-18 16:14:02 +05302057 }
2058 if (irq_status & CORE_PWRCTL_BUS_OFF) {
2059 ret = sdhci_msm_setup_vreg(msm_host->pdata, false, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302060 if (!ret) {
Asutosh Das0ef24812012-12-18 16:14:02 +05302061 ret = sdhci_msm_setup_pins(msm_host->pdata, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302062 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
2063 VDD_IO_LOW, 0);
2064 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302065 if (ret)
2066 irq_ack |= CORE_PWRCTL_BUS_FAIL;
2067 else
2068 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302069
2070 pwr_state = REQ_BUS_OFF;
2071 io_level = REQ_IO_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05302072 }
2073 /* Handle IO LOW/HIGH */
2074 if (irq_status & CORE_PWRCTL_IO_LOW) {
2075 /* Switch voltage Low */
2076 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_LOW, 0);
2077 if (ret)
2078 irq_ack |= CORE_PWRCTL_IO_FAIL;
2079 else
2080 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302081
2082 io_level = REQ_IO_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05302083 }
2084 if (irq_status & CORE_PWRCTL_IO_HIGH) {
2085 /* Switch voltage High */
2086 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_HIGH, 0);
2087 if (ret)
2088 irq_ack |= CORE_PWRCTL_IO_FAIL;
2089 else
2090 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302091
2092 io_level = REQ_IO_HIGH;
Asutosh Das0ef24812012-12-18 16:14:02 +05302093 }
2094
2095 /* ACK status to the core */
2096 writeb_relaxed(irq_ack, (msm_host->core_mem + CORE_PWRCTL_CTL));
2097 /*
2098 * SDHC has core_mem and hc_mem device memory and these memory
2099 * addresses do not fall within 1KB region. Hence, any update to
2100 * core_mem address space would require an mb() to ensure this gets
2101 * completed before its next update to registers within hc_mem.
2102 */
2103 mb();
2104
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302105 if (io_level & REQ_IO_HIGH)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002106 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2107 ~CORE_IO_PAD_PWR_SWITCH),
2108 host->ioaddr + CORE_VENDOR_SPEC);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302109 else if (io_level & REQ_IO_LOW)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002110 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
2111 CORE_IO_PAD_PWR_SWITCH),
2112 host->ioaddr + CORE_VENDOR_SPEC);
2113 mb();
2114
Asutosh Das0ef24812012-12-18 16:14:02 +05302115 pr_debug("%s: Handled IRQ(%d), ret=%d, ack=0x%x\n",
2116 mmc_hostname(msm_host->mmc), irq, ret, irq_ack);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302117 spin_lock_irqsave(&host->lock, flags);
2118 if (pwr_state)
2119 msm_host->curr_pwr_state = pwr_state;
2120 if (io_level)
2121 msm_host->curr_io_level = io_level;
2122 complete(&msm_host->pwr_irq_completion);
2123 spin_unlock_irqrestore(&host->lock, flags);
2124
Asutosh Das0ef24812012-12-18 16:14:02 +05302125 return IRQ_HANDLED;
2126}
2127
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302128static ssize_t
Sahitya Tummala5c55b932013-06-20 14:00:18 +05302129show_polling(struct device *dev, struct device_attribute *attr, char *buf)
2130{
2131 struct sdhci_host *host = dev_get_drvdata(dev);
2132 int poll;
2133 unsigned long flags;
2134
2135 spin_lock_irqsave(&host->lock, flags);
2136 poll = !!(host->mmc->caps & MMC_CAP_NEEDS_POLL);
2137 spin_unlock_irqrestore(&host->lock, flags);
2138
2139 return snprintf(buf, PAGE_SIZE, "%d\n", poll);
2140}
2141
2142static ssize_t
2143store_polling(struct device *dev, struct device_attribute *attr,
2144 const char *buf, size_t count)
2145{
2146 struct sdhci_host *host = dev_get_drvdata(dev);
2147 int value;
2148 unsigned long flags;
2149
2150 if (!kstrtou32(buf, 0, &value)) {
2151 spin_lock_irqsave(&host->lock, flags);
2152 if (value) {
2153 host->mmc->caps |= MMC_CAP_NEEDS_POLL;
2154 mmc_detect_change(host->mmc, 0);
2155 } else {
2156 host->mmc->caps &= ~MMC_CAP_NEEDS_POLL;
2157 }
2158 spin_unlock_irqrestore(&host->lock, flags);
2159 }
2160 return count;
2161}
2162
2163static ssize_t
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302164show_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
2165 char *buf)
2166{
2167 struct sdhci_host *host = dev_get_drvdata(dev);
2168 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2169 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2170
2171 return snprintf(buf, PAGE_SIZE, "%u\n",
2172 msm_host->msm_bus_vote.is_max_bw_needed);
2173}
2174
2175static ssize_t
2176store_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
2177 const char *buf, size_t count)
2178{
2179 struct sdhci_host *host = dev_get_drvdata(dev);
2180 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2181 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2182 uint32_t value;
2183 unsigned long flags;
2184
2185 if (!kstrtou32(buf, 0, &value)) {
2186 spin_lock_irqsave(&host->lock, flags);
2187 msm_host->msm_bus_vote.is_max_bw_needed = !!value;
2188 spin_unlock_irqrestore(&host->lock, flags);
2189 }
2190 return count;
2191}
2192
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302193static void sdhci_msm_check_power_status(struct sdhci_host *host, u32 req_type)
Asutosh Das0ef24812012-12-18 16:14:02 +05302194{
2195 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2196 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302197 unsigned long flags;
2198 bool done = false;
Sahitya Tummala481fbb02013-08-06 15:22:28 +05302199 u32 io_sig_sts;
Asutosh Das0ef24812012-12-18 16:14:02 +05302200
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302201 spin_lock_irqsave(&host->lock, flags);
2202 pr_debug("%s: %s: request %d curr_pwr_state %x curr_io_level %x\n",
2203 mmc_hostname(host->mmc), __func__, req_type,
2204 msm_host->curr_pwr_state, msm_host->curr_io_level);
Sahitya Tummala481fbb02013-08-06 15:22:28 +05302205 io_sig_sts = readl_relaxed(msm_host->core_mem + CORE_GENERICS);
2206 /*
2207 * The IRQ for request type IO High/Low will be generated when -
2208 * 1. SWITCHABLE_SIGNALLING_VOL is enabled in HW.
2209 * 2. If 1 is true and when there is a state change in 1.8V enable
2210 * bit (bit 3) of SDHCI_HOST_CONTROL2 register. The reset state of
2211 * that bit is 0 which indicates 3.3V IO voltage. So, when MMC core
2212 * layer tries to set it to 3.3V before card detection happens, the
2213 * IRQ doesn't get triggered as there is no state change in this bit.
2214 * The driver already handles this case by changing the IO voltage
2215 * level to high as part of controller power up sequence. Hence, check
2216 * for host->pwr to handle a case where IO voltage high request is
2217 * issued even before controller power up.
2218 */
2219 if (req_type & (REQ_IO_HIGH | REQ_IO_LOW)) {
2220 if (!(io_sig_sts & SWITCHABLE_SIGNALLING_VOL) ||
2221 ((req_type & REQ_IO_HIGH) && !host->pwr)) {
2222 pr_debug("%s: do not wait for power IRQ that never comes\n",
2223 mmc_hostname(host->mmc));
2224 spin_unlock_irqrestore(&host->lock, flags);
2225 return;
2226 }
2227 }
2228
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302229 if ((req_type & msm_host->curr_pwr_state) ||
2230 (req_type & msm_host->curr_io_level))
2231 done = true;
2232 spin_unlock_irqrestore(&host->lock, flags);
Asutosh Das0ef24812012-12-18 16:14:02 +05302233
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302234 /*
2235 * This is needed here to hanlde a case where IRQ gets
2236 * triggered even before this function is called so that
2237 * x->done counter of completion gets reset. Otherwise,
2238 * next call to wait_for_completion returns immediately
2239 * without actually waiting for the IRQ to be handled.
2240 */
2241 if (done)
2242 init_completion(&msm_host->pwr_irq_completion);
2243 else
2244 wait_for_completion(&msm_host->pwr_irq_completion);
2245
2246 pr_debug("%s: %s: request %d done\n", mmc_hostname(host->mmc),
2247 __func__, req_type);
Asutosh Das0ef24812012-12-18 16:14:02 +05302248}
2249
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002250static void sdhci_msm_toggle_cdr(struct sdhci_host *host, bool enable)
2251{
2252 if (enable)
2253 writel_relaxed((readl_relaxed(host->ioaddr +
2254 CORE_DLL_CONFIG) | CORE_CDR_EN),
2255 host->ioaddr + CORE_DLL_CONFIG);
2256 else
2257 writel_relaxed((readl_relaxed(host->ioaddr +
2258 CORE_DLL_CONFIG) & ~CORE_CDR_EN),
2259 host->ioaddr + CORE_DLL_CONFIG);
2260}
2261
Asutosh Das648f9d12013-01-10 21:11:04 +05302262static unsigned int sdhci_msm_max_segs(void)
2263{
2264 return SDHCI_MSM_MAX_SEGMENTS;
2265}
2266
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302267static unsigned int sdhci_msm_get_min_clock(struct sdhci_host *host)
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302268{
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302269 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2270 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302271
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302272 return msm_host->pdata->sup_clk_table[0];
2273}
2274
2275static unsigned int sdhci_msm_get_max_clock(struct sdhci_host *host)
2276{
2277 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2278 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2279 int max_clk_index = msm_host->pdata->sup_clk_cnt;
2280
2281 return msm_host->pdata->sup_clk_table[max_clk_index - 1];
2282}
2283
2284static unsigned int sdhci_msm_get_sup_clk_rate(struct sdhci_host *host,
2285 u32 req_clk)
2286{
2287 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2288 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2289 unsigned int sel_clk = -1;
2290 unsigned char cnt;
2291
2292 if (req_clk < sdhci_msm_get_min_clock(host)) {
2293 sel_clk = sdhci_msm_get_min_clock(host);
2294 return sel_clk;
2295 }
2296
2297 for (cnt = 0; cnt < msm_host->pdata->sup_clk_cnt; cnt++) {
2298 if (msm_host->pdata->sup_clk_table[cnt] > req_clk) {
2299 break;
2300 } else if (msm_host->pdata->sup_clk_table[cnt] == req_clk) {
2301 sel_clk = msm_host->pdata->sup_clk_table[cnt];
2302 break;
2303 } else {
2304 sel_clk = msm_host->pdata->sup_clk_table[cnt];
2305 }
2306 }
2307 return sel_clk;
2308}
2309
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302310static int sdhci_msm_enable_controller_clock(struct sdhci_host *host)
2311{
2312 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2313 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2314 int rc = 0;
2315
2316 if (atomic_read(&msm_host->controller_clock))
2317 return 0;
2318
2319 sdhci_msm_bus_voting(host, 1);
2320
2321 if (!IS_ERR(msm_host->pclk)) {
2322 rc = clk_prepare_enable(msm_host->pclk);
2323 if (rc) {
2324 pr_err("%s: %s: failed to enable the pclk with error %d\n",
2325 mmc_hostname(host->mmc), __func__, rc);
2326 goto remove_vote;
2327 }
2328 }
2329
2330 rc = clk_prepare_enable(msm_host->clk);
2331 if (rc) {
2332 pr_err("%s: %s: failed to enable the host-clk with error %d\n",
2333 mmc_hostname(host->mmc), __func__, rc);
2334 goto disable_pclk;
2335 }
2336
2337 atomic_set(&msm_host->controller_clock, 1);
2338 pr_debug("%s: %s: enabled controller clock\n",
2339 mmc_hostname(host->mmc), __func__);
2340 goto out;
2341
2342disable_pclk:
2343 if (!IS_ERR(msm_host->pclk))
2344 clk_disable_unprepare(msm_host->pclk);
2345remove_vote:
2346 if (msm_host->msm_bus_vote.client_handle)
2347 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2348out:
2349 return rc;
2350}
2351
2352
2353
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302354static int sdhci_msm_prepare_clocks(struct sdhci_host *host, bool enable)
2355{
2356 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2357 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2358 int rc = 0;
2359
2360 if (enable && !atomic_read(&msm_host->clks_on)) {
2361 pr_debug("%s: request to enable clocks\n",
2362 mmc_hostname(host->mmc));
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302363
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302364 /*
2365 * The bus-width or the clock rate might have changed
2366 * after controller clocks are enbaled, update bus vote
2367 * in such case.
2368 */
2369 if (atomic_read(&msm_host->controller_clock))
2370 sdhci_msm_bus_voting(host, 1);
2371
2372 rc = sdhci_msm_enable_controller_clock(host);
2373 if (rc)
2374 goto remove_vote;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302375
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302376 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
2377 rc = clk_prepare_enable(msm_host->bus_clk);
2378 if (rc) {
2379 pr_err("%s: %s: failed to enable the bus-clock with error %d\n",
2380 mmc_hostname(host->mmc), __func__, rc);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302381 goto disable_controller_clk;
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302382 }
2383 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002384 if (!IS_ERR(msm_host->ff_clk)) {
2385 rc = clk_prepare_enable(msm_host->ff_clk);
2386 if (rc) {
2387 pr_err("%s: %s: failed to enable the ff_clk with error %d\n",
2388 mmc_hostname(host->mmc), __func__, rc);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302389 goto disable_bus_clk;
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002390 }
2391 }
2392 if (!IS_ERR(msm_host->sleep_clk)) {
2393 rc = clk_prepare_enable(msm_host->sleep_clk);
2394 if (rc) {
2395 pr_err("%s: %s: failed to enable the sleep_clk with error %d\n",
2396 mmc_hostname(host->mmc), __func__, rc);
2397 goto disable_ff_clk;
2398 }
2399 }
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302400 mb();
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302401
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302402 } else if (!enable && atomic_read(&msm_host->clks_on)) {
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302403 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
2404 mb();
Sahitya Tummaladc182982013-08-20 15:32:09 +05302405 /*
2406 * During 1.8V signal switching the clock source must
2407 * still be ON as it requires accessing SDHC
2408 * registers (SDHCi host control2 register bit 3 must
2409 * be written and polled after stopping the SDCLK).
2410 */
2411 if (host->mmc->card_clock_off)
2412 return 0;
2413 pr_debug("%s: request to disable clocks\n",
2414 mmc_hostname(host->mmc));
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002415 if (!IS_ERR_OR_NULL(msm_host->sleep_clk))
2416 clk_disable_unprepare(msm_host->sleep_clk);
2417 if (!IS_ERR_OR_NULL(msm_host->ff_clk))
2418 clk_disable_unprepare(msm_host->ff_clk);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302419 clk_disable_unprepare(msm_host->clk);
2420 if (!IS_ERR(msm_host->pclk))
2421 clk_disable_unprepare(msm_host->pclk);
2422 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2423 clk_disable_unprepare(msm_host->bus_clk);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302424
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302425 atomic_set(&msm_host->controller_clock, 0);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302426 sdhci_msm_bus_voting(host, 0);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302427 }
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302428 atomic_set(&msm_host->clks_on, enable);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302429 goto out;
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002430disable_ff_clk:
2431 if (!IS_ERR_OR_NULL(msm_host->ff_clk))
2432 clk_disable_unprepare(msm_host->ff_clk);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302433disable_bus_clk:
2434 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2435 clk_disable_unprepare(msm_host->bus_clk);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302436disable_controller_clk:
2437 if (!IS_ERR_OR_NULL(msm_host->clk))
2438 clk_disable_unprepare(msm_host->clk);
2439 if (!IS_ERR_OR_NULL(msm_host->pclk))
2440 clk_disable_unprepare(msm_host->pclk);
2441 atomic_set(&msm_host->controller_clock, 0);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302442remove_vote:
2443 if (msm_host->msm_bus_vote.client_handle)
2444 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302445out:
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302446 return rc;
2447}
2448
2449static void sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock)
2450{
2451 int rc;
2452 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2453 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2454 struct mmc_ios curr_ios = host->mmc->ios;
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002455 u32 sup_clock, ddr_clock, dll_lock;
Sahitya Tummala043744a2013-06-24 09:55:33 +05302456 bool curr_pwrsave;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302457
2458 if (!clock) {
Sujit Reddy Thummabf1aecc2014-01-10 10:58:54 +05302459 /*
2460 * disable pwrsave to ensure clock is not auto-gated until
2461 * the rate is >400KHz (initialization complete).
2462 */
2463 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2464 ~CORE_CLK_PWRSAVE, host->ioaddr + CORE_VENDOR_SPEC);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302465 sdhci_msm_prepare_clocks(host, false);
2466 host->clock = clock;
2467 goto out;
2468 }
2469
2470 rc = sdhci_msm_prepare_clocks(host, true);
2471 if (rc)
2472 goto out;
2473
Sahitya Tummala043744a2013-06-24 09:55:33 +05302474 curr_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2475 CORE_CLK_PWRSAVE);
Sahitya Tummalae000b242013-08-29 16:21:08 +05302476 if ((clock > 400000) &&
Sahitya Tummala043744a2013-06-24 09:55:33 +05302477 !curr_pwrsave && mmc_host_may_gate_card(host->mmc->card))
2478 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2479 | CORE_CLK_PWRSAVE,
2480 host->ioaddr + CORE_VENDOR_SPEC);
2481 /*
2482 * Disable pwrsave for a newly added card if doesn't allow clock
2483 * gating.
2484 */
2485 else if (curr_pwrsave && !mmc_host_may_gate_card(host->mmc->card))
2486 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2487 & ~CORE_CLK_PWRSAVE,
2488 host->ioaddr + CORE_VENDOR_SPEC);
2489
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302490 sup_clock = sdhci_msm_get_sup_clk_rate(host, clock);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002491 if ((curr_ios.timing == MMC_TIMING_UHS_DDR50) ||
2492 (curr_ios.timing == MMC_TIMING_MMC_HS400)) {
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302493 /*
2494 * The SDHC requires internal clock frequency to be double the
2495 * actual clock that will be set for DDR mode. The controller
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002496 * uses the faster clock(100/400MHz) for some of its parts and
2497 * send the actual required clock (50/200MHz) to the card.
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302498 */
2499 ddr_clock = clock * 2;
2500 sup_clock = sdhci_msm_get_sup_clk_rate(host,
2501 ddr_clock);
2502 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002503
2504 /*
2505 * In general all timing modes are controlled via UHS mode select in
2506 * Host Control2 register. eMMC specific HS200/HS400 doesn't have
2507 * their respective modes defined here, hence we use these values.
2508 *
2509 * HS200 - SDR104 (Since they both are equivalent in functionality)
2510 * HS400 - This involves multiple configurations
2511 * Initially SDR104 - when tuning is required as HS200
2512 * Then when switching to DDR @ 400MHz (HS400) we use
2513 * the vendor specific HC_SELECT_IN to control the mode.
2514 *
2515 * In addition to controlling the modes we also need to select the
2516 * correct input clock for DLL depending on the mode.
2517 *
2518 * HS400 - divided clock (free running MCLK/2)
2519 * All other modes - default (free running MCLK)
2520 */
2521 if (curr_ios.timing == MMC_TIMING_MMC_HS400) {
2522 /* Select the divided clock (free running MCLK/2) */
2523 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2524 & ~CORE_HC_MCLK_SEL_MASK)
2525 | CORE_HC_MCLK_SEL_HS400),
2526 host->ioaddr + CORE_VENDOR_SPEC);
2527 /*
2528 * Select HS400 mode using the HC_SELECT_IN from VENDOR SPEC
2529 * register
2530 */
2531 if (msm_host->tuning_done && !msm_host->calibration_done) {
2532 /*
2533 * Write 0x6 to HC_SELECT_IN and 1 to HC_SELECT_IN_EN
2534 * field in VENDOR_SPEC_FUNC
2535 */
2536 writel_relaxed((readl_relaxed(host->ioaddr + \
2537 CORE_VENDOR_SPEC)
2538 | CORE_HC_SELECT_IN_HS400
2539 | CORE_HC_SELECT_IN_EN),
2540 host->ioaddr + CORE_VENDOR_SPEC);
2541 }
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002542 if (!host->mmc->ios.old_rate && !msm_host->use_cdclp533) {
2543 /*
2544 * Poll on DLL_LOCK and DDR_DLL_LOCK bits in
2545 * CORE_DLL_STATUS to be set. This should get set
2546 * with in 15 us at 200 MHz.
2547 */
2548 rc = readl_poll_timeout(host->ioaddr + CORE_DLL_STATUS,
2549 dll_lock, (dll_lock & (CORE_DLL_LOCK |
2550 CORE_DDR_DLL_LOCK)), 10, 1000);
2551 if (rc == -ETIMEDOUT)
2552 pr_err("%s: Unable to get DLL_LOCK/DDR_DLL_LOCK, dll_status: 0x%08x\n",
2553 mmc_hostname(host->mmc),
2554 dll_lock);
2555 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002556 } else {
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002557 if (!msm_host->use_cdclp533)
2558 /* set CORE_PWRSAVE_DLL bit in CORE_VENDOR_SPEC3 */
2559 writel_relaxed((readl_relaxed(host->ioaddr +
2560 CORE_VENDOR_SPEC3) & ~CORE_PWRSAVE_DLL),
2561 host->ioaddr + CORE_VENDOR_SPEC3);
2562
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002563 /* Select the default clock (free running MCLK) */
2564 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2565 & ~CORE_HC_MCLK_SEL_MASK)
2566 | CORE_HC_MCLK_SEL_DFLT),
2567 host->ioaddr + CORE_VENDOR_SPEC);
2568
2569 /*
2570 * Disable HC_SELECT_IN to be able to use the UHS mode select
2571 * configuration from Host Control2 register for all other
2572 * modes.
2573 *
2574 * Write 0 to HC_SELECT_IN and HC_SELECT_IN_EN field
2575 * in VENDOR_SPEC_FUNC
2576 */
2577 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2578 & ~CORE_HC_SELECT_IN_EN
2579 & ~CORE_HC_SELECT_IN_MASK),
2580 host->ioaddr + CORE_VENDOR_SPEC);
2581 }
2582 mb();
2583
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302584 if (sup_clock != msm_host->clk_rate) {
2585 pr_debug("%s: %s: setting clk rate to %u\n",
2586 mmc_hostname(host->mmc), __func__, sup_clock);
2587 rc = clk_set_rate(msm_host->clk, sup_clock);
2588 if (rc) {
2589 pr_err("%s: %s: Failed to set rate %u for host-clk : %d\n",
2590 mmc_hostname(host->mmc), __func__,
2591 sup_clock, rc);
2592 goto out;
2593 }
2594 msm_host->clk_rate = sup_clock;
2595 host->clock = clock;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302596 /*
2597 * Update the bus vote in case of frequency change due to
2598 * clock scaling.
2599 */
2600 sdhci_msm_bus_voting(host, 1);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302601 }
2602out:
2603 sdhci_set_clock(host, clock);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302604}
2605
Sahitya Tummala14613432013-03-21 11:13:25 +05302606static void sdhci_msm_set_uhs_signaling(struct sdhci_host *host,
2607 unsigned int uhs)
2608{
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002609 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2610 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala14613432013-03-21 11:13:25 +05302611 u16 ctrl_2;
2612
2613 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2614 /* Select Bus Speed Mode for host */
2615 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002616 if (uhs == MMC_TIMING_MMC_HS400)
2617 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2618 else if (uhs == MMC_TIMING_MMC_HS200)
Sahitya Tummala14613432013-03-21 11:13:25 +05302619 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2620 else if (uhs == MMC_TIMING_UHS_SDR12)
2621 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
2622 else if (uhs == MMC_TIMING_UHS_SDR25)
2623 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
2624 else if (uhs == MMC_TIMING_UHS_SDR50)
2625 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
2626 else if (uhs == MMC_TIMING_UHS_SDR104)
2627 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2628 else if (uhs == MMC_TIMING_UHS_DDR50)
2629 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302630 /*
2631 * When clock frquency is less than 100MHz, the feedback clock must be
2632 * provided and DLL must not be used so that tuning can be skipped. To
2633 * provide feedback clock, the mode selection can be any value less
2634 * than 3'b011 in bits [2:0] of HOST CONTROL2 register.
2635 */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002636 if (host->clock <= CORE_FREQ_100MHZ) {
2637 if ((uhs == MMC_TIMING_MMC_HS400) ||
2638 (uhs == MMC_TIMING_MMC_HS200) ||
2639 (uhs == MMC_TIMING_UHS_SDR104))
2640 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302641
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002642 /*
2643 * Make sure DLL is disabled when not required
2644 *
2645 * Write 1 to DLL_RST bit of DLL_CONFIG register
2646 */
2647 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
2648 | CORE_DLL_RST),
2649 host->ioaddr + CORE_DLL_CONFIG);
2650
2651 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
2652 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
2653 | CORE_DLL_PDN),
2654 host->ioaddr + CORE_DLL_CONFIG);
2655 mb();
2656
2657 /*
2658 * The DLL needs to be restored and CDCLP533 recalibrated
2659 * when the clock frequency is set back to 400MHz.
2660 */
2661 msm_host->calibration_done = false;
2662 }
2663
2664 pr_debug("%s: %s-clock:%u uhs mode:%u ctrl_2:0x%x\n",
2665 mmc_hostname(host->mmc), __func__, host->clock, uhs, ctrl_2);
Sahitya Tummala14613432013-03-21 11:13:25 +05302666 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
2667
2668}
2669
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302670#define MAX_TEST_BUS 20
2671
2672void sdhci_msm_dump_vendor_regs(struct sdhci_host *host)
2673{
2674 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2675 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2676 int tbsel, tbsel2;
2677 int i, index = 0;
2678 u32 test_bus_val = 0;
2679 u32 debug_reg[MAX_TEST_BUS] = {0};
2680
2681 pr_info("----------- VENDOR REGISTER DUMP -----------\n");
2682 pr_info("Data cnt: 0x%08x | Fifo cnt: 0x%08x | Int sts: 0x%08x\n",
2683 readl_relaxed(msm_host->core_mem + CORE_MCI_DATA_CNT),
2684 readl_relaxed(msm_host->core_mem + CORE_MCI_FIFO_CNT),
2685 readl_relaxed(msm_host->core_mem + CORE_MCI_STATUS));
2686 pr_info("DLL cfg: 0x%08x | DLL sts: 0x%08x | SDCC ver: 0x%08x\n",
2687 readl_relaxed(host->ioaddr + CORE_DLL_CONFIG),
2688 readl_relaxed(host->ioaddr + CORE_DLL_STATUS),
2689 readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION));
2690 pr_info("Vndr func: 0x%08x | Vndr adma err : addr0: 0x%08x addr1: 0x%08x\n",
2691 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC),
2692 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_ADMA_ERR_ADDR0),
2693 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_ADMA_ERR_ADDR1));
2694
2695 /*
2696 * tbsel indicates [2:0] bits and tbsel2 indicates [7:4] bits
2697 * of CORE_TESTBUS_CONFIG register.
2698 *
2699 * To select test bus 0 to 7 use tbsel and to select any test bus
2700 * above 7 use (tbsel2 | tbsel) to get the test bus number. For eg,
2701 * to select test bus 14, write 0x1E to CORE_TESTBUS_CONFIG register
2702 * i.e., tbsel2[7:4] = 0001, tbsel[2:0] = 110.
2703 */
2704 for (tbsel2 = 0; tbsel2 < 3; tbsel2++) {
2705 for (tbsel = 0; tbsel < 8; tbsel++) {
2706 if (index >= MAX_TEST_BUS)
2707 break;
2708 test_bus_val = (tbsel2 << CORE_TESTBUS_SEL2_BIT) |
2709 tbsel | CORE_TESTBUS_ENA;
2710 writel_relaxed(test_bus_val,
2711 msm_host->core_mem + CORE_TESTBUS_CONFIG);
2712 debug_reg[index++] = readl_relaxed(msm_host->core_mem +
2713 CORE_SDCC_DEBUG_REG);
2714 }
2715 }
2716 for (i = 0; i < MAX_TEST_BUS; i = i + 4)
2717 pr_info(" Test bus[%d to %d]: 0x%08x 0x%08x 0x%08x 0x%08x\n",
2718 i, i + 3, debug_reg[i], debug_reg[i+1],
2719 debug_reg[i+2], debug_reg[i+3]);
2720 /* Disable test bus */
2721 writel_relaxed(~CORE_TESTBUS_ENA, msm_host->core_mem +
2722 CORE_TESTBUS_CONFIG);
2723}
2724
Asutosh Das0ef24812012-12-18 16:14:02 +05302725static struct sdhci_ops sdhci_msm_ops = {
Sahitya Tummala14613432013-03-21 11:13:25 +05302726 .set_uhs_signaling = sdhci_msm_set_uhs_signaling,
Asutosh Das0ef24812012-12-18 16:14:02 +05302727 .check_power_status = sdhci_msm_check_power_status,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002728 .platform_execute_tuning = sdhci_msm_execute_tuning,
2729 .toggle_cdr = sdhci_msm_toggle_cdr,
Asutosh Das648f9d12013-01-10 21:11:04 +05302730 .get_max_segments = sdhci_msm_max_segs,
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302731 .set_clock = sdhci_msm_set_clock,
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302732 .get_min_clock = sdhci_msm_get_min_clock,
2733 .get_max_clock = sdhci_msm_get_max_clock,
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302734 .dump_vendor_regs = sdhci_msm_dump_vendor_regs,
Asutosh Dase5e9ca62013-07-30 19:08:36 +05302735 .config_auto_tuning_cmd = sdhci_msm_config_auto_tuning_cmd,
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302736 .enable_controller_clock = sdhci_msm_enable_controller_clock,
Asutosh Das0ef24812012-12-18 16:14:02 +05302737};
2738
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302739static void sdhci_set_default_hw_caps(struct sdhci_msm_host *msm_host,
2740 struct sdhci_host *host)
2741{
2742 u32 version, caps;
2743 u16 minor;
2744 u8 major;
2745
2746 version = readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION);
2747 major = (version & CORE_VERSION_MAJOR_MASK) >>
2748 CORE_VERSION_MAJOR_SHIFT;
2749 minor = version & CORE_VERSION_TARGET_MASK;
2750
2751 /*
2752 * Starting with SDCC 5 controller (core major version = 1)
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002753 * controller won't advertise 3.0v, 1.8v and 8-bit features
2754 * except for some targets.
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302755 */
2756 if (major >= 1 && minor != 0x11 && minor != 0x12) {
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002757 struct sdhci_msm_reg_data *vdd_io_reg;
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302758 caps = CORE_3_0V_SUPPORT;
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002759 /*
2760 * Enable 1.8V support capability on controllers that
2761 * support dual voltage
2762 */
2763 vdd_io_reg = msm_host->pdata->vreg_data->vdd_io_data;
2764 if (vdd_io_reg &&
2765 (vdd_io_reg->low_vol_level != vdd_io_reg->high_vol_level))
2766 caps |= CORE_1_8V_SUPPORT;
Pratibhasagar Vada47992013-12-09 20:42:32 +05302767 if (msm_host->pdata->mmc_bus_width == MMC_CAP_8_BIT_DATA)
2768 caps |= CORE_8_BIT_SUPPORT;
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302769 writel_relaxed(
2770 (readl_relaxed(host->ioaddr + SDHCI_CAPABILITIES) |
2771 caps), host->ioaddr + CORE_VENDOR_SPEC_CAPABILITIES0);
2772 }
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002773
2774 /*
2775 * SDCC 5 controller with major version 1, minor version 0x34 and later
2776 * with HS 400 mode support will use CM DLL instead of CDC LP 533 DLL.
2777 */
2778 if ((major == 1) && (minor < 0x34))
2779 msm_host->use_cdclp533 = true;
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302780}
2781
Asutosh Das0ef24812012-12-18 16:14:02 +05302782static int sdhci_msm_probe(struct platform_device *pdev)
2783{
2784 struct sdhci_host *host;
2785 struct sdhci_pltfm_host *pltfm_host;
2786 struct sdhci_msm_host *msm_host;
2787 struct resource *core_memres = NULL;
2788 int ret = 0, pwr_irq = 0, dead = 0;
Stephen Boyd8dce5c62013-04-24 14:19:46 -07002789 u16 host_version;
Subhash Jadavani28137342013-05-14 17:46:43 +05302790 u32 pwr, irq_status, irq_ctl;
Asutosh Das0ef24812012-12-18 16:14:02 +05302791
2792 pr_debug("%s: Enter %s\n", dev_name(&pdev->dev), __func__);
2793 msm_host = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_msm_host),
2794 GFP_KERNEL);
2795 if (!msm_host) {
2796 ret = -ENOMEM;
2797 goto out;
2798 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302799
2800 msm_host->sdhci_msm_pdata.ops = &sdhci_msm_ops;
2801 host = sdhci_pltfm_init(pdev, &msm_host->sdhci_msm_pdata, 0);
2802 if (IS_ERR(host)) {
2803 ret = PTR_ERR(host);
2804 goto out;
2805 }
2806
2807 pltfm_host = sdhci_priv(host);
2808 pltfm_host->priv = msm_host;
2809 msm_host->mmc = host->mmc;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302810 msm_host->pdev = pdev;
Asutosh Das0ef24812012-12-18 16:14:02 +05302811
2812 /* Extract platform data */
2813 if (pdev->dev.of_node) {
Venkat Gopalakrishnan270580a2013-03-11 12:17:57 -07002814 ret = of_alias_get_id(pdev->dev.of_node, "sdhc");
2815 if (ret < 0) {
2816 dev_err(&pdev->dev, "Failed to get slot index %d\n",
2817 ret);
2818 goto pltfm_free;
2819 }
2820 if (disable_slots & (1 << (ret - 1))) {
2821 dev_info(&pdev->dev, "%s: Slot %d disabled\n", __func__,
2822 ret);
2823 ret = -ENODEV;
2824 goto pltfm_free;
2825 }
2826
Asutosh Das0ef24812012-12-18 16:14:02 +05302827 msm_host->pdata = sdhci_msm_populate_pdata(&pdev->dev);
2828 if (!msm_host->pdata) {
2829 dev_err(&pdev->dev, "DT parsing error\n");
2830 goto pltfm_free;
2831 }
2832 } else {
2833 dev_err(&pdev->dev, "No device tree node\n");
2834 goto pltfm_free;
2835 }
2836
2837 /* Setup Clocks */
2838
2839 /* Setup SDCC bus voter clock. */
2840 msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus_clk");
2841 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
2842 /* Vote for max. clk rate for max. performance */
2843 ret = clk_set_rate(msm_host->bus_clk, INT_MAX);
2844 if (ret)
2845 goto pltfm_free;
2846 ret = clk_prepare_enable(msm_host->bus_clk);
2847 if (ret)
2848 goto pltfm_free;
2849 }
2850
2851 /* Setup main peripheral bus clock */
2852 msm_host->pclk = devm_clk_get(&pdev->dev, "iface_clk");
2853 if (!IS_ERR(msm_host->pclk)) {
2854 ret = clk_prepare_enable(msm_host->pclk);
2855 if (ret)
2856 goto bus_clk_disable;
2857 }
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302858 atomic_set(&msm_host->controller_clock, 1);
Asutosh Das0ef24812012-12-18 16:14:02 +05302859
2860 /* Setup SDC MMC clock */
2861 msm_host->clk = devm_clk_get(&pdev->dev, "core_clk");
2862 if (IS_ERR(msm_host->clk)) {
2863 ret = PTR_ERR(msm_host->clk);
2864 goto pclk_disable;
2865 }
2866
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302867 /* Set to the minimum supported clock frequency */
2868 ret = clk_set_rate(msm_host->clk, sdhci_msm_get_min_clock(host));
2869 if (ret) {
2870 dev_err(&pdev->dev, "MClk rate set failed (%d)\n", ret);
Sahitya Tummala020ede0d2013-06-07 13:03:07 +05302871 goto pclk_disable;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302872 }
Sahitya Tummala020ede0d2013-06-07 13:03:07 +05302873 ret = clk_prepare_enable(msm_host->clk);
2874 if (ret)
2875 goto pclk_disable;
2876
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302877 msm_host->clk_rate = sdhci_msm_get_min_clock(host);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302878 atomic_set(&msm_host->clks_on, 1);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302879
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002880 /* Setup CDC calibration fixed feedback clock */
2881 msm_host->ff_clk = devm_clk_get(&pdev->dev, "cal_clk");
2882 if (!IS_ERR(msm_host->ff_clk)) {
2883 ret = clk_prepare_enable(msm_host->ff_clk);
2884 if (ret)
2885 goto clk_disable;
2886 }
2887
2888 /* Setup CDC calibration sleep clock */
2889 msm_host->sleep_clk = devm_clk_get(&pdev->dev, "sleep_clk");
2890 if (!IS_ERR(msm_host->sleep_clk)) {
2891 ret = clk_prepare_enable(msm_host->sleep_clk);
2892 if (ret)
2893 goto ff_clk_disable;
2894 }
2895
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07002896 msm_host->saved_tuning_phase = INVALID_TUNING_PHASE;
2897
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302898 ret = sdhci_msm_bus_register(msm_host, pdev);
2899 if (ret)
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002900 goto sleep_clk_disable;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302901
2902 if (msm_host->msm_bus_vote.client_handle)
2903 INIT_DELAYED_WORK(&msm_host->msm_bus_vote.vote_work,
2904 sdhci_msm_bus_work);
2905 sdhci_msm_bus_voting(host, 1);
2906
Asutosh Das0ef24812012-12-18 16:14:02 +05302907 /* Setup regulators */
2908 ret = sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, true);
2909 if (ret) {
2910 dev_err(&pdev->dev, "Regulator setup failed (%d)\n", ret);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302911 goto bus_unregister;
Asutosh Das0ef24812012-12-18 16:14:02 +05302912 }
2913
2914 /* Reset the core and Enable SDHC mode */
2915 core_memres = platform_get_resource_byname(pdev,
2916 IORESOURCE_MEM, "core_mem");
Asutosh Das890bdee2014-08-08 23:01:42 +05302917 if (!core_memres) {
2918 dev_err(&pdev->dev, "Failed to get iomem resource\n");
2919 goto vreg_deinit;
2920 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302921 msm_host->core_mem = devm_ioremap(&pdev->dev, core_memres->start,
2922 resource_size(core_memres));
2923
2924 if (!msm_host->core_mem) {
2925 dev_err(&pdev->dev, "Failed to remap registers\n");
2926 ret = -ENOMEM;
2927 goto vreg_deinit;
2928 }
2929
Stepan Moskovchenkoa4d0fa72013-09-13 22:19:33 -07002930 /* Unset HC_MODE_EN bit in HC_MODE register */
2931 writel_relaxed(0, (msm_host->core_mem + CORE_HC_MODE));
2932
Asutosh Das0ef24812012-12-18 16:14:02 +05302933 /* Set SW_RST bit in POWER register (Offset 0x0) */
Sahitya Tummala66b0fe32013-04-25 11:50:56 +05302934 writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_POWER) |
2935 CORE_SW_RST, msm_host->core_mem + CORE_POWER);
2936 /*
2937 * SW reset can take upto 10HCLK + 15MCLK cycles.
2938 * Calculating based on min clk rates (hclk = 27MHz,
2939 * mclk = 400KHz) it comes to ~40us. Let's poll for
2940 * max. 1ms for reset completion.
2941 */
2942 ret = readl_poll_timeout(msm_host->core_mem + CORE_POWER,
Matt Wagantallc7097a22014-04-29 15:48:15 -07002943 pwr, !(pwr & CORE_SW_RST), 10, 1000);
Sahitya Tummala66b0fe32013-04-25 11:50:56 +05302944
2945 if (ret) {
2946 dev_err(&pdev->dev, "reset failed (%d)\n", ret);
2947 goto vreg_deinit;
2948 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302949 /* Set HC_MODE_EN bit in HC_MODE register */
2950 writel_relaxed(HC_MODE_EN, (msm_host->core_mem + CORE_HC_MODE));
2951
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002952 /* Set FF_CLK_SW_RST_DIS bit in HC_MODE register */
2953 writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_HC_MODE) |
2954 FF_CLK_SW_RST_DIS, msm_host->core_mem + CORE_HC_MODE);
2955
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302956 sdhci_set_default_hw_caps(msm_host, host);
Asutosh Das0ef24812012-12-18 16:14:02 +05302957 /*
Subhash Jadavani28137342013-05-14 17:46:43 +05302958 * CORE_SW_RST above may trigger power irq if previous status of PWRCTL
2959 * was either BUS_ON or IO_HIGH_V. So before we enable the power irq
2960 * interrupt in GIC (by registering the interrupt handler), we need to
2961 * ensure that any pending power irq interrupt status is acknowledged
2962 * otherwise power irq interrupt handler would be fired prematurely.
2963 */
2964 irq_status = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
2965 writel_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
2966 irq_ctl = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_CTL);
2967 if (irq_status & (CORE_PWRCTL_BUS_ON | CORE_PWRCTL_BUS_OFF))
2968 irq_ctl |= CORE_PWRCTL_BUS_SUCCESS;
2969 if (irq_status & (CORE_PWRCTL_IO_HIGH | CORE_PWRCTL_IO_LOW))
2970 irq_ctl |= CORE_PWRCTL_IO_SUCCESS;
2971 writel_relaxed(irq_ctl, (msm_host->core_mem + CORE_PWRCTL_CTL));
2972 /*
2973 * Ensure that above writes are propogated before interrupt enablement
2974 * in GIC.
2975 */
2976 mb();
2977
2978 /*
Asutosh Das0ef24812012-12-18 16:14:02 +05302979 * Following are the deviations from SDHC spec v3.0 -
2980 * 1. Card detection is handled using separate GPIO.
2981 * 2. Bus power control is handled by interacting with PMIC.
2982 */
2983 host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
2984 host->quirks |= SDHCI_QUIRK_SINGLE_POWER_WRITE;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302985 host->quirks |= SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN;
2986 host->quirks2 |= SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK;
Sahitya Tummala87d43942013-04-12 11:49:11 +05302987 host->quirks2 |= SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD;
Sahitya Tummala314162c2013-04-12 12:11:20 +05302988 host->quirks2 |= SDHCI_QUIRK2_BROKEN_PRESET_VALUE;
Sahitya Tummala7c9780d2013-04-12 11:59:25 +05302989 host->quirks2 |= SDHCI_QUIRK2_USE_RESERVED_MAX_TIMEOUT;
Asutosh Das0ef24812012-12-18 16:14:02 +05302990
Sahitya Tummalaa5733ab52013-06-10 16:32:51 +05302991 if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK)
2992 host->quirks2 |= SDHCI_QUIRK2_DIVIDE_TOUT_BY_4;
2993
Stephen Boyd8dce5c62013-04-24 14:19:46 -07002994 host_version = readw_relaxed((host->ioaddr + SDHCI_HOST_VERSION));
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07002995 dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n",
2996 host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >>
2997 SDHCI_VENDOR_VER_SHIFT));
2998 if (((host_version & SDHCI_VENDOR_VER_MASK) >>
2999 SDHCI_VENDOR_VER_SHIFT) == SDHCI_VER_100) {
3000 /*
3001 * Add 40us delay in interrupt handler when
3002 * operating at initialization frequency(400KHz).
3003 */
3004 host->quirks2 |= SDHCI_QUIRK2_SLOW_INT_CLR;
3005 /*
3006 * Set Software Reset for DAT line in Software
3007 * Reset Register (Bit 2).
3008 */
3009 host->quirks2 |= SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT;
3010 }
3011
Asutosh Das214b9662013-06-13 14:27:42 +05303012 host->quirks2 |= SDHCI_QUIRK2_IGN_DATA_END_BIT_ERROR;
3013
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07003014 /* Setup PWRCTL irq */
Asutosh Das0ef24812012-12-18 16:14:02 +05303015 pwr_irq = platform_get_irq_byname(pdev, "pwr_irq");
3016 if (pwr_irq < 0) {
3017 dev_err(&pdev->dev, "Failed to get pwr_irq by name (%d)\n",
3018 pwr_irq);
3019 goto vreg_deinit;
3020 }
3021 ret = devm_request_threaded_irq(&pdev->dev, pwr_irq, NULL,
3022 sdhci_msm_pwr_irq, IRQF_ONESHOT,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07003023 dev_name(&pdev->dev), host);
Asutosh Das0ef24812012-12-18 16:14:02 +05303024 if (ret) {
3025 dev_err(&pdev->dev, "Request threaded irq(%d) failed (%d)\n",
3026 pwr_irq, ret);
3027 goto vreg_deinit;
3028 }
3029
3030 /* Enable pwr irq interrupts */
3031 writel_relaxed(INT_MASK, (msm_host->core_mem + CORE_PWRCTL_MASK));
3032
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05303033#ifdef CONFIG_MMC_CLKGATE
3034 /* Set clock gating delay to be used when CONFIG_MMC_CLKGATE is set */
3035 msm_host->mmc->clkgate_delay = SDHCI_MSM_MMC_CLK_GATE_DELAY;
3036#endif
3037
Asutosh Das0ef24812012-12-18 16:14:02 +05303038 /* Set host capabilities */
3039 msm_host->mmc->caps |= msm_host->pdata->mmc_bus_width;
3040 msm_host->mmc->caps |= msm_host->pdata->caps;
Asutosh Das0ef24812012-12-18 16:14:02 +05303041 msm_host->mmc->caps2 |= msm_host->pdata->caps2;
3042 msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR;
3043 msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR_CONTROL;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05303044 msm_host->mmc->caps2 |= MMC_CAP2_CLK_SCALE;
Subhash Jadavani6d472b22013-05-29 15:52:10 +05303045 msm_host->mmc->caps2 |= MMC_CAP2_ASYNC_SDIO_IRQ_4BIT_MODE;
Asutosh Dasbea115d2013-06-24 18:20:45 +05303046 msm_host->mmc->pm_caps |= MMC_PM_KEEP_POWER;
Asutosh Das0ef24812012-12-18 16:14:02 +05303047
3048 if (msm_host->pdata->nonremovable)
3049 msm_host->mmc->caps |= MMC_CAP_NONREMOVABLE;
3050
Guoping Yuf7c91332014-08-20 16:56:18 +08003051 if (msm_host->pdata->nonhotplug)
3052 msm_host->mmc->caps2 |= MMC_CAP2_NONHOTPLUG;
3053
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05303054 host->cpu_dma_latency_us = msm_host->pdata->cpu_dma_latency_us;
3055
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05303056 init_completion(&msm_host->pwr_irq_completion);
3057
Sahitya Tummala581df132013-03-12 14:57:46 +05303058 if (gpio_is_valid(msm_host->pdata->status_gpio)) {
Sahitya Tummala6ddabb42014-06-05 13:26:55 +05303059 /*
3060 * Set up the card detect GPIO in active configuration before
3061 * configuring it as an IRQ. Otherwise, it can be in some
3062 * weird/inconsistent state resulting in flood of interrupts.
3063 */
3064 sdhci_msm_setup_pins(msm_host->pdata, true);
3065
Sahitya Tummala581df132013-03-12 14:57:46 +05303066 ret = mmc_gpio_request_cd(msm_host->mmc,
3067 msm_host->pdata->status_gpio, 0);
3068 if (ret) {
3069 dev_err(&pdev->dev, "%s: Failed to request card detection IRQ %d\n",
3070 __func__, ret);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05303071 goto vreg_deinit;
Sahitya Tummala581df132013-03-12 14:57:46 +05303072 }
3073 }
3074
Krishna Konda7feab352013-09-17 23:55:40 -07003075 if ((sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT) &&
3076 (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(64)))) {
3077 host->dma_mask = DMA_BIT_MASK(64);
3078 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
3079 } else if (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(32))) {
Sahitya Tummalaeaa21862013-03-20 19:34:59 +05303080 host->dma_mask = DMA_BIT_MASK(32);
3081 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
3082 } else {
3083 dev_err(&pdev->dev, "%s: Failed to set dma mask\n", __func__);
3084 }
3085
Asutosh Das0ef24812012-12-18 16:14:02 +05303086 ret = sdhci_add_host(host);
3087 if (ret) {
3088 dev_err(&pdev->dev, "Add host failed (%d)\n", ret);
Sahitya Tummala581df132013-03-12 14:57:46 +05303089 goto vreg_deinit;
Asutosh Das0ef24812012-12-18 16:14:02 +05303090 }
3091
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05303092 msm_host->msm_bus_vote.max_bus_bw.show = show_sdhci_max_bus_bw;
3093 msm_host->msm_bus_vote.max_bus_bw.store = store_sdhci_max_bus_bw;
3094 sysfs_attr_init(&msm_host->msm_bus_vote.max_bus_bw.attr);
3095 msm_host->msm_bus_vote.max_bus_bw.attr.name = "max_bus_bw";
3096 msm_host->msm_bus_vote.max_bus_bw.attr.mode = S_IRUGO | S_IWUSR;
3097 ret = device_create_file(&pdev->dev,
3098 &msm_host->msm_bus_vote.max_bus_bw);
3099 if (ret)
3100 goto remove_host;
3101
Sahitya Tummala5c55b932013-06-20 14:00:18 +05303102 if (!gpio_is_valid(msm_host->pdata->status_gpio)) {
3103 msm_host->polling.show = show_polling;
3104 msm_host->polling.store = store_polling;
3105 sysfs_attr_init(&msm_host->polling.attr);
3106 msm_host->polling.attr.name = "polling";
3107 msm_host->polling.attr.mode = S_IRUGO | S_IWUSR;
3108 ret = device_create_file(&pdev->dev, &msm_host->polling);
3109 if (ret)
3110 goto remove_max_bus_bw_file;
3111 }
Asutosh Dase5e9ca62013-07-30 19:08:36 +05303112
3113 msm_host->auto_cmd21_attr.show = show_auto_cmd21;
3114 msm_host->auto_cmd21_attr.store = store_auto_cmd21;
3115 sysfs_attr_init(&msm_host->auto_cmd21_attr.attr);
3116 msm_host->auto_cmd21_attr.attr.name = "enable_auto_cmd21";
3117 msm_host->auto_cmd21_attr.attr.mode = S_IRUGO | S_IWUSR;
3118 ret = device_create_file(&pdev->dev, &msm_host->auto_cmd21_attr);
3119 if (ret) {
3120 pr_err("%s: %s: failed creating auto-cmd21 attr: %d\n",
3121 mmc_hostname(host->mmc), __func__, ret);
3122 device_remove_file(&pdev->dev, &msm_host->auto_cmd21_attr);
3123 }
3124
Asutosh Das0ef24812012-12-18 16:14:02 +05303125 /* Successful initialization */
3126 goto out;
3127
Sahitya Tummala5c55b932013-06-20 14:00:18 +05303128remove_max_bus_bw_file:
3129 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Asutosh Das0ef24812012-12-18 16:14:02 +05303130remove_host:
3131 dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
3132 sdhci_remove_host(host, dead);
3133vreg_deinit:
3134 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05303135bus_unregister:
3136 if (msm_host->msm_bus_vote.client_handle)
3137 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
3138 sdhci_msm_bus_unregister(msm_host);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07003139sleep_clk_disable:
3140 if (!IS_ERR(msm_host->sleep_clk))
3141 clk_disable_unprepare(msm_host->sleep_clk);
3142ff_clk_disable:
3143 if (!IS_ERR(msm_host->ff_clk))
3144 clk_disable_unprepare(msm_host->ff_clk);
Asutosh Das0ef24812012-12-18 16:14:02 +05303145clk_disable:
3146 if (!IS_ERR(msm_host->clk))
3147 clk_disable_unprepare(msm_host->clk);
3148pclk_disable:
3149 if (!IS_ERR(msm_host->pclk))
3150 clk_disable_unprepare(msm_host->pclk);
3151bus_clk_disable:
3152 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
3153 clk_disable_unprepare(msm_host->bus_clk);
3154pltfm_free:
3155 sdhci_pltfm_free(pdev);
3156out:
3157 pr_debug("%s: Exit %s\n", dev_name(&pdev->dev), __func__);
3158 return ret;
3159}
3160
3161static int sdhci_msm_remove(struct platform_device *pdev)
3162{
3163 struct sdhci_host *host = platform_get_drvdata(pdev);
3164 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3165 struct sdhci_msm_host *msm_host = pltfm_host->priv;
3166 struct sdhci_msm_pltfm_data *pdata = msm_host->pdata;
3167 int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) ==
3168 0xffffffff);
3169
3170 pr_debug("%s: %s\n", dev_name(&pdev->dev), __func__);
Sahitya Tummala5c55b932013-06-20 14:00:18 +05303171 if (!gpio_is_valid(msm_host->pdata->status_gpio))
3172 device_remove_file(&pdev->dev, &msm_host->polling);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05303173 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Asutosh Das0ef24812012-12-18 16:14:02 +05303174 sdhci_remove_host(host, dead);
3175 sdhci_pltfm_free(pdev);
Sahitya Tummala581df132013-03-12 14:57:46 +05303176
Asutosh Das0ef24812012-12-18 16:14:02 +05303177 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05303178
Pratibhasagar V9acf2642013-11-21 21:07:21 +05303179 sdhci_msm_setup_pins(pdata, true);
3180 sdhci_msm_setup_pins(pdata, false);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05303181
3182 if (msm_host->msm_bus_vote.client_handle) {
3183 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
3184 sdhci_msm_bus_unregister(msm_host);
3185 }
Asutosh Das0ef24812012-12-18 16:14:02 +05303186 return 0;
3187}
3188
3189static const struct of_device_id sdhci_msm_dt_match[] = {
3190 {.compatible = "qcom,sdhci-msm"},
3191};
3192MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match);
3193
3194static struct platform_driver sdhci_msm_driver = {
3195 .probe = sdhci_msm_probe,
3196 .remove = sdhci_msm_remove,
3197 .driver = {
3198 .name = "sdhci_msm",
3199 .owner = THIS_MODULE,
3200 .of_match_table = sdhci_msm_dt_match,
3201 },
3202};
3203
3204module_platform_driver(sdhci_msm_driver);
3205
3206MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Secure Digital Host Controller Interface driver");
3207MODULE_LICENSE("GPL v2");