blob: 0d86fe324236b7817a4348927745bb52e5d877b9 [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 *
Venkat Gopalakrishnan0a92d532014-12-16 17:31:00 -08005 * Copyright (c) 2012-2015, 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>
Konstantin Dorfman98377d32015-02-25 10:09:41 +020041#include <linux/pm_runtime.h>
Asutosh Das0ef24812012-12-18 16:14:02 +053042
43#include "sdhci-pltfm.h"
44
Venkat Gopalakrishnan0a92d532014-12-16 17:31:00 -080045#define CORE_POWER 0x0
46#define CORE_SW_RST (1 << 7)
47
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -070048#define SDHCI_VER_100 0x2B
Venkat Gopalakrishnan0a92d532014-12-16 17:31:00 -080049#define CORE_MCI_DATA_CNT 0x30
50#define CORE_MCI_STATUS 0x34
51#define CORE_MCI_FIFO_CNT 0x44
52
53#define CORE_VERSION_STEP_MASK 0x0000FFFF
54#define CORE_VERSION_MINOR_MASK 0x0FFF0000
55#define CORE_VERSION_MINOR_SHIFT 16
56#define CORE_VERSION_MAJOR_MASK 0xF0000000
57#define CORE_VERSION_MAJOR_SHIFT 28
58#define CORE_VERSION_TARGET_MASK 0x000000FF
59
60#define CORE_GENERICS 0x70
61#define SWITCHABLE_SIGNALLING_VOL (1 << 29)
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +053062
63#define CORE_VERSION_MAJOR_MASK 0xF0000000
64#define CORE_VERSION_MAJOR_SHIFT 28
65
Asutosh Das0ef24812012-12-18 16:14:02 +053066#define CORE_HC_MODE 0x78
67#define HC_MODE_EN 0x1
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -070068#define FF_CLK_SW_RST_DIS (1 << 13)
Asutosh Das0ef24812012-12-18 16:14:02 +053069
Sahitya Tummala67717bc2013-08-02 09:21:37 +053070#define CORE_MCI_VERSION 0x050
71#define CORE_TESTBUS_CONFIG 0x0CC
72#define CORE_TESTBUS_ENA (1 << 3)
Venkat Gopalakrishnan0a92d532014-12-16 17:31:00 -080073#define CORE_TESTBUS_SEL2_BIT 4
74#define CORE_TESTBUS_SEL2 (1 << CORE_TESTBUS_SEL2_BIT)
Sahitya Tummala67717bc2013-08-02 09:21:37 +053075
Asutosh Das0ef24812012-12-18 16:14:02 +053076#define CORE_PWRCTL_STATUS 0xDC
77#define CORE_PWRCTL_MASK 0xE0
78#define CORE_PWRCTL_CLEAR 0xE4
79#define CORE_PWRCTL_CTL 0xE8
80
81#define CORE_PWRCTL_BUS_OFF 0x01
82#define CORE_PWRCTL_BUS_ON (1 << 1)
83#define CORE_PWRCTL_IO_LOW (1 << 2)
84#define CORE_PWRCTL_IO_HIGH (1 << 3)
85
86#define CORE_PWRCTL_BUS_SUCCESS 0x01
87#define CORE_PWRCTL_BUS_FAIL (1 << 1)
88#define CORE_PWRCTL_IO_SUCCESS (1 << 2)
89#define CORE_PWRCTL_IO_FAIL (1 << 3)
90
91#define INT_MASK 0xF
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -070092#define MAX_PHASES 16
93
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -070094#define CORE_DLL_CONFIG 0x100
95#define CORE_CMD_DAT_TRACK_SEL (1 << 0)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -070096#define CORE_DLL_EN (1 << 16)
97#define CORE_CDR_EN (1 << 17)
98#define CORE_CK_OUT_EN (1 << 18)
99#define CORE_CDR_EXT_EN (1 << 19)
100#define CORE_DLL_PDN (1 << 29)
101#define CORE_DLL_RST (1 << 30)
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700102
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700103#define CORE_DLL_STATUS 0x108
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700104#define CORE_DLL_LOCK (1 << 7)
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700105#define CORE_DDR_DLL_LOCK (1 << 11)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700106
107#define CORE_VENDOR_SPEC 0x10C
Krishna Konda46fd1432014-10-30 21:13:27 -0700108#define CORE_CLK_PWRSAVE (1 << 1)
109#define CORE_HC_MCLK_SEL_DFLT (2 << 8)
110#define CORE_HC_MCLK_SEL_HS400 (3 << 8)
111#define CORE_HC_MCLK_SEL_MASK (3 << 8)
112#define CORE_HC_AUTO_CMD21_EN (1 << 6)
113#define CORE_IO_PAD_PWR_SWITCH_EN (1 << 15)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700114#define CORE_IO_PAD_PWR_SWITCH (1 << 16)
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700115#define CORE_HC_SELECT_IN_EN (1 << 18)
116#define CORE_HC_SELECT_IN_HS400 (6 << 19)
117#define CORE_HC_SELECT_IN_MASK (7 << 19)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700118
Venkat Gopalakrishnan0a92d532014-12-16 17:31:00 -0800119#define CORE_VENDOR_SPEC_ADMA_ERR_ADDR0 0x114
120#define CORE_VENDOR_SPEC_ADMA_ERR_ADDR1 0x118
121
Krishna Konda7feab352013-09-17 23:55:40 -0700122#define CORE_VENDOR_SPEC_CAPABILITIES0 0x11C
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +0530123#define CORE_8_BIT_SUPPORT (1 << 18)
124#define CORE_3_3V_SUPPORT (1 << 24)
125#define CORE_3_0V_SUPPORT (1 << 25)
126#define CORE_1_8V_SUPPORT (1 << 26)
Gilad Broner2a10ca02014-10-02 17:20:35 +0300127#define CORE_SYS_BUS_SUPPORT_64_BIT BIT(28)
Krishna Konda7feab352013-09-17 23:55:40 -0700128
Venkat Gopalakrishnan0a92d532014-12-16 17:31:00 -0800129#define CORE_SDCC_DEBUG_REG 0x124
Sahitya Tummala67717bc2013-08-02 09:21:37 +0530130
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700131#define CORE_CSR_CDC_CTLR_CFG0 0x130
132#define CORE_SW_TRIG_FULL_CALIB (1 << 16)
133#define CORE_HW_AUTOCAL_ENA (1 << 17)
134
135#define CORE_CSR_CDC_CTLR_CFG1 0x134
136#define CORE_CSR_CDC_CAL_TIMER_CFG0 0x138
137#define CORE_TIMER_ENA (1 << 16)
138
139#define CORE_CSR_CDC_CAL_TIMER_CFG1 0x13C
140#define CORE_CSR_CDC_REFCOUNT_CFG 0x140
141#define CORE_CSR_CDC_COARSE_CAL_CFG 0x144
142#define CORE_CDC_OFFSET_CFG 0x14C
143#define CORE_CSR_CDC_DELAY_CFG 0x150
144#define CORE_CDC_SLAVE_DDA_CFG 0x160
145#define CORE_CSR_CDC_STATUS0 0x164
146#define CORE_CALIBRATION_DONE (1 << 0)
147
148#define CORE_CDC_ERROR_CODE_MASK 0x7000000
149
150#define CORE_CSR_CDC_GEN_CFG 0x178
151#define CORE_CDC_SWITCH_BYPASS_OFF (1 << 0)
152#define CORE_CDC_SWITCH_RC_EN (1 << 1)
153
154#define CORE_DDR_200_CFG 0x184
155#define CORE_CDC_T4_DLY_SEL (1 << 0)
156#define CORE_START_CDC_TRAFFIC (1 << 6)
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700157#define CORE_VENDOR_SPEC3 0x1B0
158#define CORE_PWRSAVE_DLL (1 << 3)
159
160#define CORE_DLL_CONFIG_2 0x1B4
161#define CORE_DDR_CAL_EN (1 << 0)
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800162#define CORE_FLL_CYCLE_CNT (1 << 18)
163#define CORE_DLL_CLOCK_DISABLE (1 << 21)
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700164
165#define CORE_DDR_CONFIG 0x1B8
166#define DDR_CONFIG_POR_VAL 0x80040853
167
Venkat Gopalakrishnan450745e2014-07-24 20:39:34 -0700168/* 512 descriptors */
169#define SDHCI_MSM_MAX_SEGMENTS (1 << 9)
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +0530170#define SDHCI_MSM_MMC_CLK_GATE_DELAY 200 /* msecs */
Asutosh Das648f9d12013-01-10 21:11:04 +0530171
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700172#define CORE_FREQ_100MHZ (100 * 1000 * 1000)
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800173#define TCXO_FREQ 19200000
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700174
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700175#define INVALID_TUNING_PHASE -1
176
Krishna Konda96e6b112013-10-28 15:25:03 -0700177#define NUM_TUNING_PHASES 16
178#define MAX_DRV_TYPES_SUPPORTED_HS200 3
Konstantin Dorfman98377d32015-02-25 10:09:41 +0200179#define MSM_AUTOSUSPEND_DELAY_MS 100
Krishna Konda96e6b112013-10-28 15:25:03 -0700180
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700181static const u32 tuning_block_64[] = {
182 0x00FF0FFF, 0xCCC3CCFF, 0xFFCC3CC3, 0xEFFEFFFE,
183 0xDDFFDFFF, 0xFBFFFBFF, 0xFF7FFFBF, 0xEFBDF777,
184 0xF0FFF0FF, 0x3CCCFC0F, 0xCFCC33CC, 0xEEFFEFFF,
185 0xFDFFFDFF, 0xFFBFFFDF, 0xFFF7FFBB, 0xDE7B7FF7
186};
187
188static const u32 tuning_block_128[] = {
189 0xFF00FFFF, 0x0000FFFF, 0xCCCCFFFF, 0xCCCC33CC,
190 0xCC3333CC, 0xFFFFCCCC, 0xFFFFEEFF, 0xFFEEEEFF,
191 0xFFDDFFFF, 0xDDDDFFFF, 0xBBFFFFFF, 0xBBFFFFFF,
192 0xFFFFFFBB, 0xFFFFFF77, 0x77FF7777, 0xFFEEDDBB,
193 0x00FFFFFF, 0x00FFFFFF, 0xCCFFFF00, 0xCC33CCCC,
194 0x3333CCCC, 0xFFCCCCCC, 0xFFEEFFFF, 0xEEEEFFFF,
195 0xDDFFFFFF, 0xDDFFFFFF, 0xFFFFFFDD, 0xFFFFFFBB,
196 0xFFFFBBBB, 0xFFFF77FF, 0xFF7777FF, 0xEEDDBB77
197};
Asutosh Das0ef24812012-12-18 16:14:02 +0530198
Venkat Gopalakrishnan270580a2013-03-11 12:17:57 -0700199static int disable_slots;
200/* root can write, others read */
201module_param(disable_slots, int, S_IRUGO|S_IWUSR);
202
Asutosh Das0ef24812012-12-18 16:14:02 +0530203/* This structure keeps information per regulator */
204struct sdhci_msm_reg_data {
205 /* voltage regulator handle */
206 struct regulator *reg;
207 /* regulator name */
208 const char *name;
209 /* voltage level to be set */
210 u32 low_vol_level;
211 u32 high_vol_level;
212 /* Load values for low power and high power mode */
213 u32 lpm_uA;
214 u32 hpm_uA;
215
216 /* is this regulator enabled? */
217 bool is_enabled;
218 /* is this regulator needs to be always on? */
219 bool is_always_on;
220 /* is low power mode setting required for this regulator? */
221 bool lpm_sup;
222 bool set_voltage_sup;
223};
224
Asutosh Das598a4d42014-02-05 16:38:23 +0530225#define MSM_MMC_DEFAULT_CPU_DMA_LATENCY 200 /* usecs */
Asutosh Das0ef24812012-12-18 16:14:02 +0530226/*
227 * This structure keeps information for all the
228 * regulators required for a SDCC slot.
229 */
230struct sdhci_msm_slot_reg_data {
231 /* keeps VDD/VCC regulator info */
232 struct sdhci_msm_reg_data *vdd_data;
233 /* keeps VDD IO regulator info */
234 struct sdhci_msm_reg_data *vdd_io_data;
235};
236
237struct sdhci_msm_gpio {
238 u32 no;
239 const char *name;
240 bool is_enabled;
241};
242
243struct sdhci_msm_gpio_data {
244 struct sdhci_msm_gpio *gpio;
245 u8 size;
246};
247
248struct sdhci_msm_pin_data {
249 /*
250 * = 1 if controller pins are using gpios
251 * = 0 if controller has dedicated MSM pads
252 */
Sahitya Tummala1cd7e072014-02-14 13:19:01 +0530253 u8 is_gpio;
Asutosh Das0ef24812012-12-18 16:14:02 +0530254 struct sdhci_msm_gpio_data *gpio_data;
255};
256
Pratibhasagar V9acf2642013-11-21 21:07:21 +0530257struct sdhci_pinctrl_data {
258 struct pinctrl *pctrl;
259 struct pinctrl_state *pins_active;
260 struct pinctrl_state *pins_sleep;
261};
262
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530263struct sdhci_msm_bus_voting_data {
264 struct msm_bus_scale_pdata *bus_pdata;
265 unsigned int *bw_vecs;
266 unsigned int bw_vecs_size;
267};
268
Asutosh Das0ef24812012-12-18 16:14:02 +0530269struct sdhci_msm_pltfm_data {
270 /* Supported UHS-I Modes */
271 u32 caps;
272
273 /* More capabilities */
274 u32 caps2;
275
276 unsigned long mmc_bus_width;
Asutosh Das0ef24812012-12-18 16:14:02 +0530277 struct sdhci_msm_slot_reg_data *vreg_data;
278 bool nonremovable;
Guoping Yuf7c91332014-08-20 16:56:18 +0800279 bool nonhotplug;
Pratibhasagar V9acf2642013-11-21 21:07:21 +0530280 bool pin_cfg_sts;
Asutosh Das0ef24812012-12-18 16:14:02 +0530281 struct sdhci_msm_pin_data *pin_data;
Pratibhasagar V9acf2642013-11-21 21:07:21 +0530282 struct sdhci_pinctrl_data *pctrl_data;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +0530283 u32 cpu_dma_latency_us;
Sahitya Tummala581df132013-03-12 14:57:46 +0530284 int status_gpio; /* card detection GPIO that is configured as IRQ */
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530285 struct sdhci_msm_bus_voting_data *voting_data;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530286 u32 *sup_clk_table;
287 unsigned char sup_clk_cnt;
Maya Erez994cf2f2014-10-21 20:22:04 +0300288 enum pm_qos_req_type cpu_affinity_type;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530289};
290
291struct sdhci_msm_bus_vote {
292 uint32_t client_handle;
293 uint32_t curr_vote;
294 int min_bw_vote;
295 int max_bw_vote;
296 bool is_max_bw_needed;
297 struct delayed_work vote_work;
298 struct device_attribute max_bus_bw;
Asutosh Das0ef24812012-12-18 16:14:02 +0530299};
300
301struct sdhci_msm_host {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530302 struct platform_device *pdev;
Asutosh Das0ef24812012-12-18 16:14:02 +0530303 void __iomem *core_mem; /* MSM SDCC mapped address */
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +0200304 int pwr_irq; /* power irq */
Asutosh Das0ef24812012-12-18 16:14:02 +0530305 struct clk *clk; /* main SD/MMC bus clock */
306 struct clk *pclk; /* SDHC peripheral bus clock */
307 struct clk *bus_clk; /* SDHC bus voter clock */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700308 struct clk *ff_clk; /* CDC calibration fixed feedback clock */
309 struct clk *sleep_clk; /* CDC calibration sleep clock */
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +0530310 atomic_t clks_on; /* Set if clocks are enabled */
Asutosh Das0ef24812012-12-18 16:14:02 +0530311 struct sdhci_msm_pltfm_data *pdata;
312 struct mmc_host *mmc;
313 struct sdhci_pltfm_data sdhci_msm_pdata;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +0530314 u32 curr_pwr_state;
315 u32 curr_io_level;
316 struct completion pwr_irq_completion;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530317 struct sdhci_msm_bus_vote msm_bus_vote;
Sahitya Tummala5c55b932013-06-20 14:00:18 +0530318 struct device_attribute polling;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530319 u32 clk_rate; /* Keeps track of current clock rate that is set */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700320 bool tuning_done;
321 bool calibration_done;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700322 u8 saved_tuning_phase;
Asutosh Dase5e9ca62013-07-30 19:08:36 +0530323 bool en_auto_cmd21;
324 struct device_attribute auto_cmd21_attr;
Asutosh Das9d7ee2f2013-11-08 12:33:47 +0530325 atomic_t controller_clock;
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700326 bool use_cdclp533;
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800327 bool use_updated_dll_reset;
Krishna Konda46fd1432014-10-30 21:13:27 -0700328 u32 caps_0;
Asutosh Das0ef24812012-12-18 16:14:02 +0530329};
330
331enum vdd_io_level {
332 /* set vdd_io_data->low_vol_level */
333 VDD_IO_LOW,
334 /* set vdd_io_data->high_vol_level */
335 VDD_IO_HIGH,
336 /*
337 * set whatever there in voltage_level (third argument) of
338 * sdhci_msm_set_vdd_io_vol() function.
339 */
340 VDD_IO_SET_LEVEL,
341};
342
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700343/* MSM platform specific tuning */
344static inline int msm_dll_poll_ck_out_en(struct sdhci_host *host,
345 u8 poll)
346{
347 int rc = 0;
348 u32 wait_cnt = 50;
349 u8 ck_out_en = 0;
350 struct mmc_host *mmc = host->mmc;
351
352 /* poll for CK_OUT_EN bit. max. poll time = 50us */
353 ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) &
354 CORE_CK_OUT_EN);
355
356 while (ck_out_en != poll) {
357 if (--wait_cnt == 0) {
358 pr_err("%s: %s: CK_OUT_EN bit is not %d\n",
359 mmc_hostname(mmc), __func__, poll);
360 rc = -ETIMEDOUT;
361 goto out;
362 }
363 udelay(1);
364
365 ck_out_en = !!(readl_relaxed(host->ioaddr +
366 CORE_DLL_CONFIG) & CORE_CK_OUT_EN);
367 }
368out:
369 return rc;
370}
371
Asutosh Dase5e9ca62013-07-30 19:08:36 +0530372/*
373 * Enable CDR to track changes of DAT lines and adjust sampling
374 * point according to voltage/temperature variations
375 */
376static int msm_enable_cdr_cm_sdc4_dll(struct sdhci_host *host)
377{
378 int rc = 0;
379 u32 config;
380
381 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
382 config |= CORE_CDR_EN;
383 config &= ~(CORE_CDR_EXT_EN | CORE_CK_OUT_EN);
384 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
385
386 rc = msm_dll_poll_ck_out_en(host, 0);
387 if (rc)
388 goto err;
389
390 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) |
391 CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
392
393 rc = msm_dll_poll_ck_out_en(host, 1);
394 if (rc)
395 goto err;
396 goto out;
397err:
398 pr_err("%s: %s: failed\n", mmc_hostname(host->mmc), __func__);
399out:
400 return rc;
401}
402
403static ssize_t store_auto_cmd21(struct device *dev, struct device_attribute
404 *attr, const char *buf, size_t count)
405{
406 struct sdhci_host *host = dev_get_drvdata(dev);
407 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
408 struct sdhci_msm_host *msm_host = pltfm_host->priv;
409 u32 tmp;
410 unsigned long flags;
411
412 if (!kstrtou32(buf, 0, &tmp)) {
413 spin_lock_irqsave(&host->lock, flags);
414 msm_host->en_auto_cmd21 = !!tmp;
415 spin_unlock_irqrestore(&host->lock, flags);
416 }
417 return count;
418}
419
420static ssize_t show_auto_cmd21(struct device *dev,
421 struct device_attribute *attr, char *buf)
422{
423 struct sdhci_host *host = dev_get_drvdata(dev);
424 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
425 struct sdhci_msm_host *msm_host = pltfm_host->priv;
426
427 return snprintf(buf, PAGE_SIZE, "%d\n", msm_host->en_auto_cmd21);
428}
429
430/* MSM auto-tuning handler */
431static int sdhci_msm_config_auto_tuning_cmd(struct sdhci_host *host,
432 bool enable,
433 u32 type)
434{
435 int rc = 0;
436 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
437 struct sdhci_msm_host *msm_host = pltfm_host->priv;
438 u32 val = 0;
439
440 if (!msm_host->en_auto_cmd21)
441 return 0;
442
443 if (type == MMC_SEND_TUNING_BLOCK_HS200)
444 val = CORE_HC_AUTO_CMD21_EN;
445 else
446 return 0;
447
448 if (enable) {
449 rc = msm_enable_cdr_cm_sdc4_dll(host);
450 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
451 val, host->ioaddr + CORE_VENDOR_SPEC);
452 } else {
453 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
454 ~val, host->ioaddr + CORE_VENDOR_SPEC);
455 }
456 return rc;
457}
458
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700459static int msm_config_cm_dll_phase(struct sdhci_host *host, u8 phase)
460{
461 int rc = 0;
462 u8 grey_coded_phase_table[] = {0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4,
463 0xC, 0xD, 0xF, 0xE, 0xA, 0xB, 0x9,
464 0x8};
465 unsigned long flags;
466 u32 config;
467 struct mmc_host *mmc = host->mmc;
468
469 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
470 spin_lock_irqsave(&host->lock, flags);
471
472 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
473 config &= ~(CORE_CDR_EN | CORE_CK_OUT_EN);
474 config |= (CORE_CDR_EXT_EN | CORE_DLL_EN);
475 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
476
477 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '0' */
478 rc = msm_dll_poll_ck_out_en(host, 0);
479 if (rc)
480 goto err_out;
481
482 /*
483 * Write the selected DLL clock output phase (0 ... 15)
484 * to CDR_SELEXT bit field of DLL_CONFIG register.
485 */
486 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
487 & ~(0xF << 20))
488 | (grey_coded_phase_table[phase] << 20)),
489 host->ioaddr + CORE_DLL_CONFIG);
490
491 /* Set CK_OUT_EN bit of DLL_CONFIG register to 1. */
492 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
493 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
494
495 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '1' */
496 rc = msm_dll_poll_ck_out_en(host, 1);
497 if (rc)
498 goto err_out;
499
500 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
501 config |= CORE_CDR_EN;
502 config &= ~CORE_CDR_EXT_EN;
503 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
504 goto out;
505
506err_out:
507 pr_err("%s: %s: Failed to set DLL phase: %d\n",
508 mmc_hostname(mmc), __func__, phase);
509out:
510 spin_unlock_irqrestore(&host->lock, flags);
511 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
512 return rc;
513}
514
515/*
516 * Find out the greatest range of consecuitive selected
517 * DLL clock output phases that can be used as sampling
518 * setting for SD3.0 UHS-I card read operation (in SDR104
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700519 * timing mode) or for eMMC4.5 card read operation (in
520 * HS400/HS200 timing mode).
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700521 * Select the 3/4 of the range and configure the DLL with the
522 * selected DLL clock output phase.
523 */
524
525static int msm_find_most_appropriate_phase(struct sdhci_host *host,
526 u8 *phase_table, u8 total_phases)
527{
528 int ret;
529 u8 ranges[MAX_PHASES][MAX_PHASES] = { {0}, {0} };
530 u8 phases_per_row[MAX_PHASES] = {0};
531 int row_index = 0, col_index = 0, selected_row_index = 0, curr_max = 0;
532 int i, cnt, phase_0_raw_index = 0, phase_15_raw_index = 0;
533 bool phase_0_found = false, phase_15_found = false;
534 struct mmc_host *mmc = host->mmc;
535
536 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
537 if (!total_phases || (total_phases > MAX_PHASES)) {
538 pr_err("%s: %s: invalid argument: total_phases=%d\n",
539 mmc_hostname(mmc), __func__, total_phases);
540 return -EINVAL;
541 }
542
543 for (cnt = 0; cnt < total_phases; cnt++) {
544 ranges[row_index][col_index] = phase_table[cnt];
545 phases_per_row[row_index] += 1;
546 col_index++;
547
548 if ((cnt + 1) == total_phases) {
549 continue;
550 /* check if next phase in phase_table is consecutive or not */
551 } else if ((phase_table[cnt] + 1) != phase_table[cnt + 1]) {
552 row_index++;
553 col_index = 0;
554 }
555 }
556
557 if (row_index >= MAX_PHASES)
558 return -EINVAL;
559
560 /* Check if phase-0 is present in first valid window? */
561 if (!ranges[0][0]) {
562 phase_0_found = true;
563 phase_0_raw_index = 0;
564 /* Check if cycle exist between 2 valid windows */
565 for (cnt = 1; cnt <= row_index; cnt++) {
566 if (phases_per_row[cnt]) {
567 for (i = 0; i < phases_per_row[cnt]; i++) {
568 if (ranges[cnt][i] == 15) {
569 phase_15_found = true;
570 phase_15_raw_index = cnt;
571 break;
572 }
573 }
574 }
575 }
576 }
577
578 /* If 2 valid windows form cycle then merge them as single window */
579 if (phase_0_found && phase_15_found) {
580 /* number of phases in raw where phase 0 is present */
581 u8 phases_0 = phases_per_row[phase_0_raw_index];
582 /* number of phases in raw where phase 15 is present */
583 u8 phases_15 = phases_per_row[phase_15_raw_index];
584
585 if (phases_0 + phases_15 >= MAX_PHASES)
586 /*
587 * If there are more than 1 phase windows then total
588 * number of phases in both the windows should not be
589 * more than or equal to MAX_PHASES.
590 */
591 return -EINVAL;
592
593 /* Merge 2 cyclic windows */
594 i = phases_15;
595 for (cnt = 0; cnt < phases_0; cnt++) {
596 ranges[phase_15_raw_index][i] =
597 ranges[phase_0_raw_index][cnt];
598 if (++i >= MAX_PHASES)
599 break;
600 }
601
602 phases_per_row[phase_0_raw_index] = 0;
603 phases_per_row[phase_15_raw_index] = phases_15 + phases_0;
604 }
605
606 for (cnt = 0; cnt <= row_index; cnt++) {
607 if (phases_per_row[cnt] > curr_max) {
608 curr_max = phases_per_row[cnt];
609 selected_row_index = cnt;
610 }
611 }
612
613 i = ((curr_max * 3) / 4);
614 if (i)
615 i--;
616
617 ret = (int)ranges[selected_row_index][i];
618
619 if (ret >= MAX_PHASES) {
620 ret = -EINVAL;
621 pr_err("%s: %s: invalid phase selected=%d\n",
622 mmc_hostname(mmc), __func__, ret);
623 }
624
625 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
626 return ret;
627}
628
629static inline void msm_cm_dll_set_freq(struct sdhci_host *host)
630{
631 u32 mclk_freq = 0;
632
633 /* Program the MCLK value to MCLK_FREQ bit field */
634 if (host->clock <= 112000000)
635 mclk_freq = 0;
636 else if (host->clock <= 125000000)
637 mclk_freq = 1;
638 else if (host->clock <= 137000000)
639 mclk_freq = 2;
640 else if (host->clock <= 150000000)
641 mclk_freq = 3;
642 else if (host->clock <= 162000000)
643 mclk_freq = 4;
644 else if (host->clock <= 175000000)
645 mclk_freq = 5;
646 else if (host->clock <= 187000000)
647 mclk_freq = 6;
648 else if (host->clock <= 200000000)
649 mclk_freq = 7;
650
651 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
652 & ~(7 << 24)) | (mclk_freq << 24)),
653 host->ioaddr + CORE_DLL_CONFIG);
654}
655
656/* Initialize the DLL (Programmable Delay Line ) */
657static int msm_init_cm_dll(struct sdhci_host *host)
658{
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800659 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
660 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700661 struct mmc_host *mmc = host->mmc;
662 int rc = 0;
663 unsigned long flags;
664 u32 wait_cnt;
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530665 bool prev_pwrsave, curr_pwrsave;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700666
667 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
668 spin_lock_irqsave(&host->lock, flags);
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530669 prev_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
670 CORE_CLK_PWRSAVE);
671 curr_pwrsave = prev_pwrsave;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700672 /*
673 * Make sure that clock is always enabled when DLL
674 * tuning is in progress. Keeping PWRSAVE ON may
675 * turn off the clock. So let's disable the PWRSAVE
676 * here and re-enable it once tuning is completed.
677 */
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530678 if (prev_pwrsave) {
679 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
680 & ~CORE_CLK_PWRSAVE),
681 host->ioaddr + CORE_VENDOR_SPEC);
682 curr_pwrsave = false;
683 }
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700684
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800685 if (msm_host->use_updated_dll_reset) {
686 /* Disable the DLL clock */
687 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
688 & ~CORE_CK_OUT_EN),
689 host->ioaddr + CORE_DLL_CONFIG);
690
691 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
692 | CORE_DLL_CLOCK_DISABLE),
693 host->ioaddr + CORE_DLL_CONFIG_2);
694 }
695
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700696 /* Write 1 to DLL_RST bit of DLL_CONFIG register */
697 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
698 | CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
699
700 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
701 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
702 | CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
703 msm_cm_dll_set_freq(host);
704
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800705 if (msm_host->use_updated_dll_reset) {
706 u32 mclk_freq = 0;
707
708 if ((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
709 & CORE_FLL_CYCLE_CNT))
710 mclk_freq = (u32) ((host->clock / TCXO_FREQ) * 8);
711 else
712 mclk_freq = (u32) ((host->clock / TCXO_FREQ) * 4);
713
714 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
715 & ~(0xFF << 10)) | (mclk_freq << 10)),
716 host->ioaddr + CORE_DLL_CONFIG_2);
717 /* wait for 5us before enabling DLL clock */
718 udelay(5);
719 }
720
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700721 /* Write 0 to DLL_RST bit of DLL_CONFIG register */
722 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
723 & ~CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
724
725 /* Write 0 to DLL_PDN bit of DLL_CONFIG register */
726 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
727 & ~CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
728
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800729 if (msm_host->use_updated_dll_reset) {
730 msm_cm_dll_set_freq(host);
731 /* Enable the DLL clock */
732 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
733 & ~CORE_DLL_CLOCK_DISABLE),
734 host->ioaddr + CORE_DLL_CONFIG_2);
735 }
736
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700737 /* Set DLL_EN bit to 1. */
738 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
739 | CORE_DLL_EN), host->ioaddr + CORE_DLL_CONFIG);
740
741 /* Set CK_OUT_EN bit to 1. */
742 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
743 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
744
745 wait_cnt = 50;
746 /* Wait until DLL_LOCK bit of DLL_STATUS register becomes '1' */
747 while (!(readl_relaxed(host->ioaddr + CORE_DLL_STATUS) &
748 CORE_DLL_LOCK)) {
749 /* max. wait for 50us sec for LOCK bit to be set */
750 if (--wait_cnt == 0) {
751 pr_err("%s: %s: DLL failed to LOCK\n",
752 mmc_hostname(mmc), __func__);
753 rc = -ETIMEDOUT;
754 goto out;
755 }
756 /* wait for 1us before polling again */
757 udelay(1);
758 }
759
760out:
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530761 /* Restore the correct PWRSAVE state */
762 if (prev_pwrsave ^ curr_pwrsave) {
763 u32 reg = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC);
764
765 if (prev_pwrsave)
766 reg |= CORE_CLK_PWRSAVE;
767 else
768 reg &= ~CORE_CLK_PWRSAVE;
769
770 writel_relaxed(reg, host->ioaddr + CORE_VENDOR_SPEC);
771 }
772
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700773 spin_unlock_irqrestore(&host->lock, flags);
774 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
775 return rc;
776}
777
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700778static int sdhci_msm_cdclp533_calibration(struct sdhci_host *host)
779{
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700780 u32 calib_done;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700781 int ret = 0;
782 int cdc_err = 0;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700783
784 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
785
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700786 /* Write 0 to CDC_T4_DLY_SEL field in VENDOR_SPEC_DDR200_CFG */
787 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
788 & ~CORE_CDC_T4_DLY_SEL),
789 host->ioaddr + CORE_DDR_200_CFG);
790
791 /* Write 0 to CDC_SWITCH_BYPASS_OFF field in CORE_CSR_CDC_GEN_CFG */
792 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG)
793 & ~CORE_CDC_SWITCH_BYPASS_OFF),
794 host->ioaddr + CORE_CSR_CDC_GEN_CFG);
795
796 /* Write 1 to CDC_SWITCH_RC_EN field in CORE_CSR_CDC_GEN_CFG */
797 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG)
798 | CORE_CDC_SWITCH_RC_EN),
799 host->ioaddr + CORE_CSR_CDC_GEN_CFG);
800
801 /* Write 0 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
802 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
803 & ~CORE_START_CDC_TRAFFIC),
804 host->ioaddr + CORE_DDR_200_CFG);
805
806 /*
807 * Perform CDC Register Initialization Sequence
808 *
809 * CORE_CSR_CDC_CTLR_CFG0 0x11800EC
810 * CORE_CSR_CDC_CTLR_CFG1 0x3011111
811 * CORE_CSR_CDC_CAL_TIMER_CFG0 0x1201000
812 * CORE_CSR_CDC_CAL_TIMER_CFG1 0x4
813 * CORE_CSR_CDC_REFCOUNT_CFG 0xCB732020
814 * CORE_CSR_CDC_COARSE_CAL_CFG 0xB19
815 * CORE_CSR_CDC_DELAY_CFG 0x3AC
816 * CORE_CDC_OFFSET_CFG 0x0
817 * CORE_CDC_SLAVE_DDA_CFG 0x16334
818 */
819
820 writel_relaxed(0x11800EC, host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
821 writel_relaxed(0x3011111, host->ioaddr + CORE_CSR_CDC_CTLR_CFG1);
822 writel_relaxed(0x1201000, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
823 writel_relaxed(0x4, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG1);
824 writel_relaxed(0xCB732020, host->ioaddr + CORE_CSR_CDC_REFCOUNT_CFG);
825 writel_relaxed(0xB19, host->ioaddr + CORE_CSR_CDC_COARSE_CAL_CFG);
Subhash Jadavanibe406d92014-06-17 16:47:48 -0700826 writel_relaxed(0x4E2, host->ioaddr + CORE_CSR_CDC_DELAY_CFG);
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700827 writel_relaxed(0x0, host->ioaddr + CORE_CDC_OFFSET_CFG);
828 writel_relaxed(0x16334, host->ioaddr + CORE_CDC_SLAVE_DDA_CFG);
829
830 /* CDC HW Calibration */
831
832 /* Write 1 to SW_TRIG_FULL_CALIB field in CORE_CSR_CDC_CTLR_CFG0 */
833 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
834 | CORE_SW_TRIG_FULL_CALIB),
835 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
836
837 /* Write 0 to SW_TRIG_FULL_CALIB field in CORE_CSR_CDC_CTLR_CFG0 */
838 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
839 & ~CORE_SW_TRIG_FULL_CALIB),
840 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
841
842 /* Write 1 to HW_AUTOCAL_ENA field in CORE_CSR_CDC_CTLR_CFG0 */
843 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
844 | CORE_HW_AUTOCAL_ENA),
845 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
846
847 /* Write 1 to TIMER_ENA field in CORE_CSR_CDC_CAL_TIMER_CFG0 */
848 writel_relaxed((readl_relaxed(host->ioaddr +
849 CORE_CSR_CDC_CAL_TIMER_CFG0) | CORE_TIMER_ENA),
850 host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
851
852 mb();
853
854 /* Poll on CALIBRATION_DONE field in CORE_CSR_CDC_STATUS0 to be 1 */
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700855 ret = readl_poll_timeout(host->ioaddr + CORE_CSR_CDC_STATUS0,
856 calib_done, (calib_done & CORE_CALIBRATION_DONE), 1, 50);
857
858 if (ret == -ETIMEDOUT) {
859 pr_err("%s: %s: CDC Calibration was not completed\n",
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700860 mmc_hostname(host->mmc), __func__);
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700861 goto out;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700862 }
863
864 /* Verify CDC_ERROR_CODE field in CORE_CSR_CDC_STATUS0 is 0 */
865 cdc_err = readl_relaxed(host->ioaddr + CORE_CSR_CDC_STATUS0)
866 & CORE_CDC_ERROR_CODE_MASK;
867 if (cdc_err) {
868 pr_err("%s: %s: CDC Error Code %d\n",
869 mmc_hostname(host->mmc), __func__, cdc_err);
870 ret = -EINVAL;
871 goto out;
872 }
873
874 /* Write 1 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
875 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
876 | CORE_START_CDC_TRAFFIC),
877 host->ioaddr + CORE_DDR_200_CFG);
878out:
879 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
880 __func__, ret);
881 return ret;
882}
883
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700884static int sdhci_msm_cm_dll_sdc4_calibration(struct sdhci_host *host)
885{
886 u32 dll_status;
887 int ret = 0;
888
889 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
890
891 /*
892 * Currently the CORE_DDR_CONFIG register defaults to desired
893 * configuration on reset. Currently reprogramming the power on
894 * reset (POR) value in case it might have been modified by
895 * bootloaders. In the future, if this changes, then the desired
896 * values will need to be programmed appropriately.
897 */
898 writel_relaxed(DDR_CONFIG_POR_VAL, host->ioaddr + CORE_DDR_CONFIG);
899
900 /* Write 1 to DDR_CAL_EN field in CORE_DLL_CONFIG_2 */
901 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
902 | CORE_DDR_CAL_EN),
903 host->ioaddr + CORE_DLL_CONFIG_2);
904
905 /* Poll on DDR_DLL_LOCK bit in CORE_DLL_STATUS to be set */
906 ret = readl_poll_timeout(host->ioaddr + CORE_DLL_STATUS,
907 dll_status, (dll_status & CORE_DDR_DLL_LOCK), 10, 1000);
908
909 if (ret == -ETIMEDOUT) {
910 pr_err("%s: %s: CM_DLL_SDC4 Calibration was not completed\n",
911 mmc_hostname(host->mmc), __func__);
912 goto out;
913 }
914
915 /* set CORE_PWRSAVE_DLL bit in CORE_VENDOR_SPEC3 */
916 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC3)
917 | CORE_PWRSAVE_DLL),
918 host->ioaddr + CORE_VENDOR_SPEC3);
919 mb();
920out:
921 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
922 __func__, ret);
923 return ret;
924}
925
926static int sdhci_msm_hs400_dll_calibration(struct sdhci_host *host)
927{
928 int ret = 0;
929 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
930 struct sdhci_msm_host *msm_host = pltfm_host->priv;
931
932 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
933
934 /*
935 * Retuning in HS400 (DDR mode) will fail, just reset the
936 * tuning block and restore the saved tuning phase.
937 */
938 ret = msm_init_cm_dll(host);
939 if (ret)
940 goto out;
941
942 /* Set the selected phase in delay line hw block */
943 ret = msm_config_cm_dll_phase(host, msm_host->saved_tuning_phase);
944 if (ret)
945 goto out;
946
Krishna Konda0e8efba2014-06-23 14:50:38 -0700947 /* Write 1 to CMD_DAT_TRACK_SEL field in DLL_CONFIG */
948 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
949 | CORE_CMD_DAT_TRACK_SEL),
950 host->ioaddr + CORE_DLL_CONFIG);
951
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700952 if (msm_host->use_cdclp533)
953 /* Calibrate CDCLP533 DLL HW */
954 ret = sdhci_msm_cdclp533_calibration(host);
955 else
956 /* Calibrate CM_DLL_SDC4 HW */
957 ret = sdhci_msm_cm_dll_sdc4_calibration(host);
958out:
959 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
960 __func__, ret);
961 return ret;
962}
963
Krishna Konda96e6b112013-10-28 15:25:03 -0700964static void sdhci_msm_set_mmc_drv_type(struct sdhci_host *host, u32 opcode,
965 u8 drv_type)
966{
967 struct mmc_command cmd = {0};
968 struct mmc_request mrq = {NULL};
969 struct mmc_host *mmc = host->mmc;
970 u8 val = ((drv_type << 4) | 2);
971
972 cmd.opcode = MMC_SWITCH;
973 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
974 (EXT_CSD_HS_TIMING << 16) |
975 (val << 8) |
976 EXT_CSD_CMD_SET_NORMAL;
977 cmd.flags = MMC_CMD_AC | MMC_RSP_R1B;
978 /* 1 sec */
979 cmd.busy_timeout = 1000 * 1000;
980
981 memset(cmd.resp, 0, sizeof(cmd.resp));
982 cmd.retries = 3;
983
984 mrq.cmd = &cmd;
985 cmd.data = NULL;
986
987 mmc_wait_for_req(mmc, &mrq);
988 pr_debug("%s: %s: set card drive type to %d\n",
989 mmc_hostname(mmc), __func__,
990 drv_type);
991}
992
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700993int sdhci_msm_execute_tuning(struct sdhci_host *host, u32 opcode)
994{
995 unsigned long flags;
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530996 int tuning_seq_cnt = 3;
Krishna Konda96e6b112013-10-28 15:25:03 -0700997 u8 phase, *data_buf, tuned_phases[NUM_TUNING_PHASES], tuned_phase_cnt;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700998 const u32 *tuning_block_pattern = tuning_block_64;
999 int size = sizeof(tuning_block_64); /* Tuning pattern size in bytes */
1000 int rc;
1001 struct mmc_host *mmc = host->mmc;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301002 struct mmc_ios ios = host->mmc->ios;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07001003 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1004 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Krishna Konda96e6b112013-10-28 15:25:03 -07001005 u8 drv_type = 0;
1006 bool drv_type_changed = false;
1007 struct mmc_card *card = host->mmc->card;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301008
1009 /*
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07001010 * Tuning is required for SDR104, HS200 and HS400 cards and
1011 * if clock frequency is greater than 100MHz in these modes.
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301012 */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07001013 if (host->clock <= CORE_FREQ_100MHZ ||
1014 !((ios.timing == MMC_TIMING_MMC_HS400) ||
1015 (ios.timing == MMC_TIMING_MMC_HS200) ||
1016 (ios.timing == MMC_TIMING_UHS_SDR104)))
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301017 return 0;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001018
1019 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07001020
Krishna Konda2faa7bb2014-06-04 01:25:16 -07001021 /* CDC/SDC4 DLL HW calibration is only required for HS400 mode*/
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07001022 if (msm_host->tuning_done && !msm_host->calibration_done &&
1023 (mmc->ios.timing == MMC_TIMING_MMC_HS400)) {
Krishna Konda2faa7bb2014-06-04 01:25:16 -07001024 rc = sdhci_msm_hs400_dll_calibration(host);
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07001025 spin_lock_irqsave(&host->lock, flags);
1026 if (!rc)
1027 msm_host->calibration_done = true;
1028 spin_unlock_irqrestore(&host->lock, flags);
1029 goto out;
1030 }
1031
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001032 spin_lock_irqsave(&host->lock, flags);
1033
1034 if ((opcode == MMC_SEND_TUNING_BLOCK_HS200) &&
1035 (mmc->ios.bus_width == MMC_BUS_WIDTH_8)) {
1036 tuning_block_pattern = tuning_block_128;
1037 size = sizeof(tuning_block_128);
1038 }
1039 spin_unlock_irqrestore(&host->lock, flags);
1040
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001041 data_buf = kmalloc(size, GFP_KERNEL);
1042 if (!data_buf) {
1043 rc = -ENOMEM;
1044 goto out;
1045 }
1046
Sahitya Tummala9fe16532013-06-13 10:36:57 +05301047retry:
Krishna Konda96e6b112013-10-28 15:25:03 -07001048 tuned_phase_cnt = 0;
1049
Sahitya Tummala9fe16532013-06-13 10:36:57 +05301050 /* first of all reset the tuning block */
1051 rc = msm_init_cm_dll(host);
1052 if (rc)
1053 goto kfree;
1054
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001055 phase = 0;
1056 do {
1057 struct mmc_command cmd = {0};
1058 struct mmc_data data = {0};
1059 struct mmc_request mrq = {
1060 .cmd = &cmd,
1061 .data = &data
1062 };
1063 struct scatterlist sg;
1064
1065 /* set the phase in delay line hw block */
1066 rc = msm_config_cm_dll_phase(host, phase);
1067 if (rc)
1068 goto kfree;
1069
1070 cmd.opcode = opcode;
1071 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1072
1073 data.blksz = size;
1074 data.blocks = 1;
1075 data.flags = MMC_DATA_READ;
1076 data.timeout_ns = 1000 * 1000 * 1000; /* 1 sec */
1077
1078 data.sg = &sg;
1079 data.sg_len = 1;
1080 sg_init_one(&sg, data_buf, size);
1081 memset(data_buf, 0, size);
1082 mmc_wait_for_req(mmc, &mrq);
1083
1084 if (!cmd.error && !data.error &&
1085 !memcmp(data_buf, tuning_block_pattern, size)) {
1086 /* tuning is successful at this tuning point */
1087 tuned_phases[tuned_phase_cnt++] = phase;
Krishna Konda96e6b112013-10-28 15:25:03 -07001088 pr_debug("%s: %s: found *** good *** phase = %d\n",
1089 mmc_hostname(mmc), __func__, phase);
1090 } else {
1091 pr_debug("%s: %s: found ## bad ## phase = %d\n",
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001092 mmc_hostname(mmc), __func__, phase);
1093 }
1094 } while (++phase < 16);
1095
Sahitya Tummaladfdb4af2014-04-01 14:29:13 +05301096 if ((tuned_phase_cnt == NUM_TUNING_PHASES) &&
1097 card && mmc_card_mmc(card)) {
Krishna Konda96e6b112013-10-28 15:25:03 -07001098 /*
1099 * If all phases pass then its a problem. So change the card's
1100 * drive type to a different value, if supported and repeat
1101 * tuning until at least one phase fails. Then set the original
1102 * drive type back.
1103 *
1104 * If all the phases still pass after trying all possible
1105 * drive types, then one of those 16 phases will be picked.
1106 * This is no different from what was going on before the
1107 * modification to change drive type and retune.
1108 */
1109 pr_debug("%s: tuned phases count: %d\n", mmc_hostname(mmc),
1110 tuned_phase_cnt);
1111
1112 /* set drive type to other value . default setting is 0x0 */
1113 while (++drv_type <= MAX_DRV_TYPES_SUPPORTED_HS200) {
1114 if (card->ext_csd.raw_driver_strength &
1115 (1 << drv_type)) {
1116 sdhci_msm_set_mmc_drv_type(host, opcode,
1117 drv_type);
1118 if (!drv_type_changed)
1119 drv_type_changed = true;
1120 goto retry;
1121 }
1122 }
1123 }
1124
1125 /* reset drive type to default (50 ohm) if changed */
1126 if (drv_type_changed)
1127 sdhci_msm_set_mmc_drv_type(host, opcode, 0);
1128
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001129 if (tuned_phase_cnt) {
1130 rc = msm_find_most_appropriate_phase(host, tuned_phases,
1131 tuned_phase_cnt);
1132 if (rc < 0)
1133 goto kfree;
1134 else
1135 phase = (u8)rc;
1136
1137 /*
1138 * Finally set the selected phase in delay
1139 * line hw block.
1140 */
1141 rc = msm_config_cm_dll_phase(host, phase);
1142 if (rc)
1143 goto kfree;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07001144 msm_host->saved_tuning_phase = phase;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001145 pr_debug("%s: %s: finally setting the tuning phase to %d\n",
1146 mmc_hostname(mmc), __func__, phase);
1147 } else {
Sahitya Tummala9fe16532013-06-13 10:36:57 +05301148 if (--tuning_seq_cnt)
1149 goto retry;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001150 /* tuning failed */
1151 pr_err("%s: %s: no tuning point found\n",
1152 mmc_hostname(mmc), __func__);
Sahitya Tummala9fe16532013-06-13 10:36:57 +05301153 rc = -EIO;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001154 }
1155
1156kfree:
1157 kfree(data_buf);
1158out:
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07001159 spin_lock_irqsave(&host->lock, flags);
1160 if (!rc)
1161 msm_host->tuning_done = true;
1162 spin_unlock_irqrestore(&host->lock, flags);
1163 pr_debug("%s: Exit %s, err(%d)\n", mmc_hostname(mmc), __func__, rc);
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001164 return rc;
1165}
1166
Asutosh Das0ef24812012-12-18 16:14:02 +05301167static int sdhci_msm_setup_gpio(struct sdhci_msm_pltfm_data *pdata, bool enable)
1168{
1169 struct sdhci_msm_gpio_data *curr;
1170 int i, ret = 0;
1171
1172 curr = pdata->pin_data->gpio_data;
1173 for (i = 0; i < curr->size; i++) {
1174 if (!gpio_is_valid(curr->gpio[i].no)) {
1175 ret = -EINVAL;
1176 pr_err("%s: Invalid gpio = %d\n", __func__,
1177 curr->gpio[i].no);
1178 goto free_gpios;
1179 }
1180 if (enable) {
1181 ret = gpio_request(curr->gpio[i].no,
1182 curr->gpio[i].name);
1183 if (ret) {
1184 pr_err("%s: gpio_request(%d, %s) failed %d\n",
1185 __func__, curr->gpio[i].no,
1186 curr->gpio[i].name, ret);
1187 goto free_gpios;
1188 }
1189 curr->gpio[i].is_enabled = true;
1190 } else {
1191 gpio_free(curr->gpio[i].no);
1192 curr->gpio[i].is_enabled = false;
1193 }
1194 }
1195 return ret;
1196
1197free_gpios:
1198 for (i--; i >= 0; i--) {
1199 gpio_free(curr->gpio[i].no);
1200 curr->gpio[i].is_enabled = false;
1201 }
1202 return ret;
1203}
1204
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301205static int sdhci_msm_setup_pinctrl(struct sdhci_msm_pltfm_data *pdata,
1206 bool enable)
1207{
1208 int ret = 0;
1209
1210 if (enable)
1211 ret = pinctrl_select_state(pdata->pctrl_data->pctrl,
1212 pdata->pctrl_data->pins_active);
1213 else
1214 ret = pinctrl_select_state(pdata->pctrl_data->pctrl,
1215 pdata->pctrl_data->pins_sleep);
1216
1217 if (ret < 0)
1218 pr_err("%s state for pinctrl failed with %d\n",
1219 enable ? "Enabling" : "Disabling", ret);
1220
1221 return ret;
1222}
1223
Asutosh Das0ef24812012-12-18 16:14:02 +05301224static int sdhci_msm_setup_pins(struct sdhci_msm_pltfm_data *pdata, bool enable)
1225{
1226 int ret = 0;
1227
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301228 if (pdata->pin_cfg_sts == enable) {
Asutosh Das0ef24812012-12-18 16:14:02 +05301229 return 0;
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301230 } else if (pdata->pctrl_data) {
1231 ret = sdhci_msm_setup_pinctrl(pdata, enable);
1232 goto out;
1233 } else if (!pdata->pin_data) {
1234 return 0;
1235 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301236
Sahitya Tummala1cd7e072014-02-14 13:19:01 +05301237 if (pdata->pin_data->is_gpio)
1238 ret = sdhci_msm_setup_gpio(pdata, enable);
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301239out:
Asutosh Das0ef24812012-12-18 16:14:02 +05301240 if (!ret)
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301241 pdata->pin_cfg_sts = enable;
Asutosh Das0ef24812012-12-18 16:14:02 +05301242
1243 return ret;
1244}
1245
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301246static int sdhci_msm_dt_get_array(struct device *dev, const char *prop_name,
1247 u32 **out, int *len, u32 size)
1248{
1249 int ret = 0;
1250 struct device_node *np = dev->of_node;
1251 size_t sz;
1252 u32 *arr = NULL;
1253
1254 if (!of_get_property(np, prop_name, len)) {
1255 ret = -EINVAL;
1256 goto out;
1257 }
1258 sz = *len = *len / sizeof(*arr);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07001259 if (sz <= 0 || (size > 0 && (sz > size))) {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301260 dev_err(dev, "%s invalid size\n", prop_name);
1261 ret = -EINVAL;
1262 goto out;
1263 }
1264
1265 arr = devm_kzalloc(dev, sz * sizeof(*arr), GFP_KERNEL);
1266 if (!arr) {
1267 dev_err(dev, "%s failed allocating memory\n", prop_name);
1268 ret = -ENOMEM;
1269 goto out;
1270 }
1271
1272 ret = of_property_read_u32_array(np, prop_name, arr, sz);
1273 if (ret < 0) {
1274 dev_err(dev, "%s failed reading array %d\n", prop_name, ret);
1275 goto out;
1276 }
1277 *out = arr;
1278out:
1279 if (ret)
1280 *len = 0;
1281 return ret;
1282}
1283
Asutosh Das0ef24812012-12-18 16:14:02 +05301284#define MAX_PROP_SIZE 32
1285static int sdhci_msm_dt_parse_vreg_info(struct device *dev,
1286 struct sdhci_msm_reg_data **vreg_data, const char *vreg_name)
1287{
1288 int len, ret = 0;
1289 const __be32 *prop;
1290 char prop_name[MAX_PROP_SIZE];
1291 struct sdhci_msm_reg_data *vreg;
1292 struct device_node *np = dev->of_node;
1293
1294 snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", vreg_name);
1295 if (!of_parse_phandle(np, prop_name, 0)) {
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301296 dev_info(dev, "No vreg data found for %s\n", vreg_name);
Asutosh Das0ef24812012-12-18 16:14:02 +05301297 return ret;
1298 }
1299
1300 vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
1301 if (!vreg) {
1302 dev_err(dev, "No memory for vreg: %s\n", vreg_name);
1303 ret = -ENOMEM;
1304 return ret;
1305 }
1306
1307 vreg->name = vreg_name;
1308
1309 snprintf(prop_name, MAX_PROP_SIZE,
1310 "qcom,%s-always-on", vreg_name);
1311 if (of_get_property(np, prop_name, NULL))
1312 vreg->is_always_on = true;
1313
1314 snprintf(prop_name, MAX_PROP_SIZE,
1315 "qcom,%s-lpm-sup", vreg_name);
1316 if (of_get_property(np, prop_name, NULL))
1317 vreg->lpm_sup = true;
1318
1319 snprintf(prop_name, MAX_PROP_SIZE,
1320 "qcom,%s-voltage-level", vreg_name);
1321 prop = of_get_property(np, prop_name, &len);
1322 if (!prop || (len != (2 * sizeof(__be32)))) {
1323 dev_warn(dev, "%s %s property\n",
1324 prop ? "invalid format" : "no", prop_name);
1325 } else {
1326 vreg->low_vol_level = be32_to_cpup(&prop[0]);
1327 vreg->high_vol_level = be32_to_cpup(&prop[1]);
1328 }
1329
1330 snprintf(prop_name, MAX_PROP_SIZE,
1331 "qcom,%s-current-level", vreg_name);
1332 prop = of_get_property(np, prop_name, &len);
1333 if (!prop || (len != (2 * sizeof(__be32)))) {
1334 dev_warn(dev, "%s %s property\n",
1335 prop ? "invalid format" : "no", prop_name);
1336 } else {
1337 vreg->lpm_uA = be32_to_cpup(&prop[0]);
1338 vreg->hpm_uA = be32_to_cpup(&prop[1]);
1339 }
1340
1341 *vreg_data = vreg;
1342 dev_dbg(dev, "%s: %s %s vol=[%d %d]uV, curr=[%d %d]uA\n",
1343 vreg->name, vreg->is_always_on ? "always_on," : "",
1344 vreg->lpm_sup ? "lpm_sup," : "", vreg->low_vol_level,
1345 vreg->high_vol_level, vreg->lpm_uA, vreg->hpm_uA);
1346
1347 return ret;
1348}
1349
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301350static int sdhci_msm_parse_pinctrl_info(struct device *dev,
1351 struct sdhci_msm_pltfm_data *pdata)
1352{
1353 struct sdhci_pinctrl_data *pctrl_data;
1354 struct pinctrl *pctrl;
1355 int ret = 0;
1356
1357 /* Try to obtain pinctrl handle */
1358 pctrl = devm_pinctrl_get(dev);
1359 if (IS_ERR(pctrl)) {
1360 ret = PTR_ERR(pctrl);
1361 goto out;
1362 }
1363 pctrl_data = devm_kzalloc(dev, sizeof(*pctrl_data), GFP_KERNEL);
1364 if (!pctrl_data) {
1365 dev_err(dev, "No memory for sdhci_pinctrl_data\n");
1366 ret = -ENOMEM;
1367 goto out;
1368 }
1369 pctrl_data->pctrl = pctrl;
1370 /* Look-up and keep the states handy to be used later */
1371 pctrl_data->pins_active = pinctrl_lookup_state(
1372 pctrl_data->pctrl, "active");
1373 if (IS_ERR(pctrl_data->pins_active)) {
1374 ret = PTR_ERR(pctrl_data->pins_active);
1375 dev_err(dev, "Could not get active pinstates, err:%d\n", ret);
1376 goto out;
1377 }
1378 pctrl_data->pins_sleep = pinctrl_lookup_state(
1379 pctrl_data->pctrl, "sleep");
1380 if (IS_ERR(pctrl_data->pins_sleep)) {
1381 ret = PTR_ERR(pctrl_data->pins_sleep);
1382 dev_err(dev, "Could not get sleep pinstates, err:%d\n", ret);
1383 goto out;
1384 }
1385 pdata->pctrl_data = pctrl_data;
1386out:
1387 return ret;
1388}
1389
Asutosh Das0ef24812012-12-18 16:14:02 +05301390#define GPIO_NAME_MAX_LEN 32
1391static int sdhci_msm_dt_parse_gpio_info(struct device *dev,
1392 struct sdhci_msm_pltfm_data *pdata)
1393{
1394 int ret = 0, cnt, i;
1395 struct sdhci_msm_pin_data *pin_data;
1396 struct device_node *np = dev->of_node;
1397
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301398 ret = sdhci_msm_parse_pinctrl_info(dev, pdata);
1399 if (!ret) {
1400 goto out;
1401 } else if (ret == -EPROBE_DEFER) {
1402 dev_err(dev, "Pinctrl framework not registered, err:%d\n", ret);
1403 goto out;
1404 } else {
1405 dev_err(dev, "Parsing Pinctrl failed with %d, falling back on GPIO lib\n",
1406 ret);
Sahitya Tummala1cd7e072014-02-14 13:19:01 +05301407 ret = 0;
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301408 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301409 pin_data = devm_kzalloc(dev, sizeof(*pin_data), GFP_KERNEL);
1410 if (!pin_data) {
1411 dev_err(dev, "No memory for pin_data\n");
1412 ret = -ENOMEM;
1413 goto out;
1414 }
1415
1416 cnt = of_gpio_count(np);
1417 if (cnt > 0) {
1418 pin_data->gpio_data = devm_kzalloc(dev,
1419 sizeof(struct sdhci_msm_gpio_data), GFP_KERNEL);
1420 if (!pin_data->gpio_data) {
1421 dev_err(dev, "No memory for gpio_data\n");
1422 ret = -ENOMEM;
1423 goto out;
1424 }
1425 pin_data->gpio_data->size = cnt;
1426 pin_data->gpio_data->gpio = devm_kzalloc(dev, cnt *
1427 sizeof(struct sdhci_msm_gpio), GFP_KERNEL);
1428
1429 if (!pin_data->gpio_data->gpio) {
1430 dev_err(dev, "No memory for gpio\n");
1431 ret = -ENOMEM;
1432 goto out;
1433 }
1434
1435 for (i = 0; i < cnt; i++) {
1436 const char *name = NULL;
1437 char result[GPIO_NAME_MAX_LEN];
1438 pin_data->gpio_data->gpio[i].no = of_get_gpio(np, i);
1439 of_property_read_string_index(np,
1440 "qcom,gpio-names", i, &name);
1441
1442 snprintf(result, GPIO_NAME_MAX_LEN, "%s-%s",
1443 dev_name(dev), name ? name : "?");
1444 pin_data->gpio_data->gpio[i].name = result;
1445 dev_dbg(dev, "%s: gpio[%s] = %d\n", __func__,
1446 pin_data->gpio_data->gpio[i].name,
1447 pin_data->gpio_data->gpio[i].no);
Asutosh Das0ef24812012-12-18 16:14:02 +05301448 }
1449 }
Sahitya Tummala1cd7e072014-02-14 13:19:01 +05301450 pdata->pin_data = pin_data;
Asutosh Das0ef24812012-12-18 16:14:02 +05301451out:
1452 if (ret)
1453 dev_err(dev, "%s failed with err %d\n", __func__, ret);
1454 return ret;
1455}
1456
Maya Erez994cf2f2014-10-21 20:22:04 +03001457#ifdef CONFIG_SMP
1458static void sdhci_msm_populate_affinity_type(struct sdhci_msm_pltfm_data *pdata,
1459 struct device_node *np)
1460{
1461 const char *cpu_affinity = NULL;
1462
1463 pdata->cpu_affinity_type = PM_QOS_REQ_AFFINE_IRQ;
1464 if (!of_property_read_string(np, "qcom,cpu-affinity",
1465 &cpu_affinity)) {
1466 if (!strcmp(cpu_affinity, "all_cores"))
1467 pdata->cpu_affinity_type = PM_QOS_REQ_ALL_CORES;
1468 else if (!strcmp(cpu_affinity, "affine_cores"))
1469 pdata->cpu_affinity_type = PM_QOS_REQ_AFFINE_CORES;
1470 else if (!strcmp(cpu_affinity, "affine_irq"))
1471 pdata->cpu_affinity_type = PM_QOS_REQ_AFFINE_IRQ;
1472 }
1473}
1474#else
1475static void sdhci_msm_populate_affinity_type(struct sdhci_msm_pltfm_data *pdata,
1476 struct device_node *np)
1477{
1478}
1479#endif
1480
Asutosh Das0ef24812012-12-18 16:14:02 +05301481/* Parse platform data */
1482static struct sdhci_msm_pltfm_data *sdhci_msm_populate_pdata(struct device *dev)
1483{
1484 struct sdhci_msm_pltfm_data *pdata = NULL;
1485 struct device_node *np = dev->of_node;
1486 u32 bus_width = 0;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05301487 u32 cpu_dma_latency;
Asutosh Das0ef24812012-12-18 16:14:02 +05301488 int len, i;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301489 int clk_table_len;
1490 u32 *clk_table = NULL;
Sujit Reddy Thumma1958d3d2013-06-03 09:54:32 +05301491 enum of_gpio_flags flags = OF_GPIO_ACTIVE_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05301492
1493 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1494 if (!pdata) {
1495 dev_err(dev, "failed to allocate memory for platform data\n");
1496 goto out;
1497 }
1498
Sujit Reddy Thumma1958d3d2013-06-03 09:54:32 +05301499 pdata->status_gpio = of_get_named_gpio_flags(np, "cd-gpios", 0, &flags);
1500 if (gpio_is_valid(pdata->status_gpio) & !(flags & OF_GPIO_ACTIVE_LOW))
1501 pdata->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
Sahitya Tummala581df132013-03-12 14:57:46 +05301502
Asutosh Das0ef24812012-12-18 16:14:02 +05301503 of_property_read_u32(np, "qcom,bus-width", &bus_width);
1504 if (bus_width == 8)
1505 pdata->mmc_bus_width = MMC_CAP_8_BIT_DATA;
1506 else if (bus_width == 4)
1507 pdata->mmc_bus_width = MMC_CAP_4_BIT_DATA;
1508 else {
1509 dev_notice(dev, "invalid bus-width, default to 1-bit mode\n");
1510 pdata->mmc_bus_width = 0;
1511 }
1512
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05301513 if (!of_property_read_u32(np, "qcom,cpu-dma-latency-us",
1514 &cpu_dma_latency))
1515 pdata->cpu_dma_latency_us = cpu_dma_latency;
Asutosh Das598a4d42014-02-05 16:38:23 +05301516 else
1517 pdata->cpu_dma_latency_us = MSM_MMC_DEFAULT_CPU_DMA_LATENCY;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301518 if (sdhci_msm_dt_get_array(dev, "qcom,clk-rates",
1519 &clk_table, &clk_table_len, 0)) {
1520 dev_err(dev, "failed parsing supported clock rates\n");
1521 goto out;
1522 }
1523 if (!clk_table || !clk_table_len) {
1524 dev_err(dev, "Invalid clock table\n");
1525 goto out;
1526 }
1527 pdata->sup_clk_table = clk_table;
1528 pdata->sup_clk_cnt = clk_table_len;
1529
Asutosh Das0ef24812012-12-18 16:14:02 +05301530 pdata->vreg_data = devm_kzalloc(dev, sizeof(struct
1531 sdhci_msm_slot_reg_data),
1532 GFP_KERNEL);
1533 if (!pdata->vreg_data) {
1534 dev_err(dev, "failed to allocate memory for vreg data\n");
1535 goto out;
1536 }
1537
1538 if (sdhci_msm_dt_parse_vreg_info(dev, &pdata->vreg_data->vdd_data,
1539 "vdd")) {
1540 dev_err(dev, "failed parsing vdd data\n");
1541 goto out;
1542 }
1543 if (sdhci_msm_dt_parse_vreg_info(dev,
1544 &pdata->vreg_data->vdd_io_data,
1545 "vdd-io")) {
1546 dev_err(dev, "failed parsing vdd-io data\n");
1547 goto out;
1548 }
1549
1550 if (sdhci_msm_dt_parse_gpio_info(dev, pdata)) {
1551 dev_err(dev, "failed parsing gpio data\n");
1552 goto out;
1553 }
1554
Asutosh Das0ef24812012-12-18 16:14:02 +05301555 len = of_property_count_strings(np, "qcom,bus-speed-mode");
1556
1557 for (i = 0; i < len; i++) {
1558 const char *name = NULL;
1559
1560 of_property_read_string_index(np,
1561 "qcom,bus-speed-mode", i, &name);
1562 if (!name)
1563 continue;
1564
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07001565 if (!strncmp(name, "HS400_1p8v", sizeof("HS400_1p8v")))
1566 pdata->caps2 |= MMC_CAP2_HS400_1_8V;
1567 else if (!strncmp(name, "HS400_1p2v", sizeof("HS400_1p2v")))
1568 pdata->caps2 |= MMC_CAP2_HS400_1_2V;
1569 else if (!strncmp(name, "HS200_1p8v", sizeof("HS200_1p8v")))
Asutosh Das0ef24812012-12-18 16:14:02 +05301570 pdata->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
1571 else if (!strncmp(name, "HS200_1p2v", sizeof("HS200_1p2v")))
1572 pdata->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
1573 else if (!strncmp(name, "DDR_1p8v", sizeof("DDR_1p8v")))
1574 pdata->caps |= MMC_CAP_1_8V_DDR
1575 | MMC_CAP_UHS_DDR50;
1576 else if (!strncmp(name, "DDR_1p2v", sizeof("DDR_1p2v")))
1577 pdata->caps |= MMC_CAP_1_2V_DDR
1578 | MMC_CAP_UHS_DDR50;
1579 }
1580
1581 if (of_get_property(np, "qcom,nonremovable", NULL))
1582 pdata->nonremovable = true;
1583
Guoping Yuf7c91332014-08-20 16:56:18 +08001584 if (of_get_property(np, "qcom,nonhotplug", NULL))
1585 pdata->nonhotplug = true;
1586
Maya Erez994cf2f2014-10-21 20:22:04 +03001587 sdhci_msm_populate_affinity_type(pdata, np);
1588
Asutosh Das0ef24812012-12-18 16:14:02 +05301589 return pdata;
1590out:
1591 return NULL;
1592}
1593
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301594/* Returns required bandwidth in Bytes per Sec */
1595static unsigned int sdhci_get_bw_required(struct sdhci_host *host,
1596 struct mmc_ios *ios)
1597{
Sahitya Tummala2886c922013-04-03 18:03:31 +05301598 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1599 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1600
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301601 unsigned int bw;
1602
Sahitya Tummala2886c922013-04-03 18:03:31 +05301603 bw = msm_host->clk_rate;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301604 /*
1605 * For DDR mode, SDCC controller clock will be at
1606 * the double rate than the actual clock that goes to card.
1607 */
1608 if (ios->bus_width == MMC_BUS_WIDTH_4)
1609 bw /= 2;
1610 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1611 bw /= 8;
1612
1613 return bw;
1614}
1615
1616static int sdhci_msm_bus_get_vote_for_bw(struct sdhci_msm_host *host,
1617 unsigned int bw)
1618{
1619 unsigned int *table = host->pdata->voting_data->bw_vecs;
1620 unsigned int size = host->pdata->voting_data->bw_vecs_size;
1621 int i;
1622
1623 if (host->msm_bus_vote.is_max_bw_needed && bw)
1624 return host->msm_bus_vote.max_bw_vote;
1625
1626 for (i = 0; i < size; i++) {
1627 if (bw <= table[i])
1628 break;
1629 }
1630
1631 if (i && (i == size))
1632 i--;
1633
1634 return i;
1635}
1636
1637/*
1638 * This function must be called with host lock acquired.
1639 * Caller of this function should also ensure that msm bus client
1640 * handle is not null.
1641 */
1642static inline int sdhci_msm_bus_set_vote(struct sdhci_msm_host *msm_host,
1643 int vote,
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301644 unsigned long *flags)
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301645{
1646 struct sdhci_host *host = platform_get_drvdata(msm_host->pdev);
1647 int rc = 0;
1648
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301649 BUG_ON(!flags);
1650
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301651 if (vote != msm_host->msm_bus_vote.curr_vote) {
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301652 spin_unlock_irqrestore(&host->lock, *flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301653 rc = msm_bus_scale_client_update_request(
1654 msm_host->msm_bus_vote.client_handle, vote);
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301655 spin_lock_irqsave(&host->lock, *flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301656 if (rc) {
1657 pr_err("%s: msm_bus_scale_client_update_request() failed: bus_client_handle=0x%x, vote=%d, err=%d\n",
1658 mmc_hostname(host->mmc),
1659 msm_host->msm_bus_vote.client_handle, vote, rc);
1660 goto out;
1661 }
1662 msm_host->msm_bus_vote.curr_vote = vote;
1663 }
1664out:
1665 return rc;
1666}
1667
1668/*
1669 * Internal work. Work to set 0 bandwidth for msm bus.
1670 */
1671static void sdhci_msm_bus_work(struct work_struct *work)
1672{
1673 struct sdhci_msm_host *msm_host;
1674 struct sdhci_host *host;
1675 unsigned long flags;
1676
1677 msm_host = container_of(work, struct sdhci_msm_host,
1678 msm_bus_vote.vote_work.work);
1679 host = platform_get_drvdata(msm_host->pdev);
1680
1681 if (!msm_host->msm_bus_vote.client_handle)
1682 return;
1683
1684 spin_lock_irqsave(&host->lock, flags);
1685 /* don't vote for 0 bandwidth if any request is in progress */
1686 if (!host->mrq) {
1687 sdhci_msm_bus_set_vote(msm_host,
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301688 msm_host->msm_bus_vote.min_bw_vote, &flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301689 } else
1690 pr_warning("%s: %s: Transfer in progress. skipping bus voting to 0 bandwidth\n",
1691 mmc_hostname(host->mmc), __func__);
1692 spin_unlock_irqrestore(&host->lock, flags);
1693}
1694
1695/*
1696 * This function cancels any scheduled delayed work and sets the bus
1697 * vote based on bw (bandwidth) argument.
1698 */
1699static void sdhci_msm_bus_cancel_work_and_set_vote(struct sdhci_host *host,
1700 unsigned int bw)
1701{
1702 int vote;
1703 unsigned long flags;
1704 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1705 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1706
1707 cancel_delayed_work_sync(&msm_host->msm_bus_vote.vote_work);
1708 spin_lock_irqsave(&host->lock, flags);
1709 vote = sdhci_msm_bus_get_vote_for_bw(msm_host, bw);
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301710 sdhci_msm_bus_set_vote(msm_host, vote, &flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301711 spin_unlock_irqrestore(&host->lock, flags);
1712}
1713
1714#define MSM_MMC_BUS_VOTING_DELAY 200 /* msecs */
1715
1716/* This function queues a work which will set the bandwidth requiement to 0 */
1717static void sdhci_msm_bus_queue_work(struct sdhci_host *host)
1718{
1719 unsigned long flags;
1720 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1721 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1722
1723 spin_lock_irqsave(&host->lock, flags);
1724 if (msm_host->msm_bus_vote.min_bw_vote !=
1725 msm_host->msm_bus_vote.curr_vote)
1726 queue_delayed_work(system_wq,
1727 &msm_host->msm_bus_vote.vote_work,
1728 msecs_to_jiffies(MSM_MMC_BUS_VOTING_DELAY));
1729 spin_unlock_irqrestore(&host->lock, flags);
1730}
1731
1732static int sdhci_msm_bus_register(struct sdhci_msm_host *host,
1733 struct platform_device *pdev)
1734{
1735 int rc = 0;
1736 struct msm_bus_scale_pdata *bus_pdata;
1737
1738 struct sdhci_msm_bus_voting_data *data;
1739 struct device *dev = &pdev->dev;
1740
1741 data = devm_kzalloc(dev,
1742 sizeof(struct sdhci_msm_bus_voting_data), GFP_KERNEL);
1743 if (!data) {
1744 dev_err(&pdev->dev,
1745 "%s: failed to allocate memory\n", __func__);
1746 rc = -ENOMEM;
1747 goto out;
1748 }
1749 data->bus_pdata = msm_bus_cl_get_pdata(pdev);
1750 if (data->bus_pdata) {
1751 rc = sdhci_msm_dt_get_array(dev, "qcom,bus-bw-vectors-bps",
1752 &data->bw_vecs, &data->bw_vecs_size, 0);
1753 if (rc) {
1754 dev_err(&pdev->dev,
1755 "%s: Failed to get bus-bw-vectors-bps\n",
1756 __func__);
1757 goto out;
1758 }
1759 host->pdata->voting_data = data;
1760 }
1761 if (host->pdata->voting_data &&
1762 host->pdata->voting_data->bus_pdata &&
1763 host->pdata->voting_data->bw_vecs &&
1764 host->pdata->voting_data->bw_vecs_size) {
1765
1766 bus_pdata = host->pdata->voting_data->bus_pdata;
1767 host->msm_bus_vote.client_handle =
1768 msm_bus_scale_register_client(bus_pdata);
1769 if (!host->msm_bus_vote.client_handle) {
1770 dev_err(&pdev->dev, "msm_bus_scale_register_client()\n");
1771 rc = -EFAULT;
1772 goto out;
1773 }
1774 /* cache the vote index for minimum and maximum bandwidth */
1775 host->msm_bus_vote.min_bw_vote =
1776 sdhci_msm_bus_get_vote_for_bw(host, 0);
1777 host->msm_bus_vote.max_bw_vote =
1778 sdhci_msm_bus_get_vote_for_bw(host, UINT_MAX);
1779 } else {
1780 devm_kfree(dev, data);
1781 }
1782
1783out:
1784 return rc;
1785}
1786
1787static void sdhci_msm_bus_unregister(struct sdhci_msm_host *host)
1788{
1789 if (host->msm_bus_vote.client_handle)
1790 msm_bus_scale_unregister_client(
1791 host->msm_bus_vote.client_handle);
1792}
1793
1794static void sdhci_msm_bus_voting(struct sdhci_host *host, u32 enable)
1795{
1796 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1797 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1798 struct mmc_ios *ios = &host->mmc->ios;
1799 unsigned int bw;
1800
1801 if (!msm_host->msm_bus_vote.client_handle)
1802 return;
1803
1804 bw = sdhci_get_bw_required(host, ios);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301805 if (enable) {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301806 sdhci_msm_bus_cancel_work_and_set_vote(host, bw);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301807 } else {
1808 /*
1809 * If clock gating is enabled, then remove the vote
1810 * immediately because clocks will be disabled only
1811 * after SDHCI_MSM_MMC_CLK_GATE_DELAY and thus no
1812 * additional delay is required to remove the bus vote.
1813 */
1814#ifdef CONFIG_MMC_CLKGATE
1815 if (host->mmc->clkgate_delay)
1816 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
1817 else
1818#endif
1819 sdhci_msm_bus_queue_work(host);
1820 }
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301821}
1822
Asutosh Das0ef24812012-12-18 16:14:02 +05301823/* Regulator utility functions */
1824static int sdhci_msm_vreg_init_reg(struct device *dev,
1825 struct sdhci_msm_reg_data *vreg)
1826{
1827 int ret = 0;
1828
1829 /* check if regulator is already initialized? */
1830 if (vreg->reg)
1831 goto out;
1832
1833 /* Get the regulator handle */
1834 vreg->reg = devm_regulator_get(dev, vreg->name);
1835 if (IS_ERR(vreg->reg)) {
1836 ret = PTR_ERR(vreg->reg);
1837 pr_err("%s: devm_regulator_get(%s) failed. ret=%d\n",
1838 __func__, vreg->name, ret);
1839 goto out;
1840 }
1841
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301842 if (regulator_count_voltages(vreg->reg) > 0) {
1843 vreg->set_voltage_sup = true;
1844 /* sanity check */
1845 if (!vreg->high_vol_level || !vreg->hpm_uA) {
1846 pr_err("%s: %s invalid constraints specified\n",
1847 __func__, vreg->name);
1848 ret = -EINVAL;
1849 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301850 }
1851
1852out:
1853 return ret;
1854}
1855
1856static void sdhci_msm_vreg_deinit_reg(struct sdhci_msm_reg_data *vreg)
1857{
1858 if (vreg->reg)
1859 devm_regulator_put(vreg->reg);
1860}
1861
1862static int sdhci_msm_vreg_set_optimum_mode(struct sdhci_msm_reg_data
1863 *vreg, int uA_load)
1864{
1865 int ret = 0;
1866
1867 /*
1868 * regulators that do not support regulator_set_voltage also
1869 * do not support regulator_set_optimum_mode
1870 */
1871 if (vreg->set_voltage_sup) {
1872 ret = regulator_set_load(vreg->reg, uA_load);
1873 if (ret < 0)
1874 pr_err("%s: regulator_set_load(reg=%s,uA_load=%d) failed. ret=%d\n",
1875 __func__, vreg->name, uA_load, ret);
1876 else
1877 /*
1878 * regulator_set_load() can return non zero
1879 * value even for success case.
1880 */
1881 ret = 0;
1882 }
1883 return ret;
1884}
1885
1886static int sdhci_msm_vreg_set_voltage(struct sdhci_msm_reg_data *vreg,
1887 int min_uV, int max_uV)
1888{
1889 int ret = 0;
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301890 if (vreg->set_voltage_sup) {
1891 ret = regulator_set_voltage(vreg->reg, min_uV, max_uV);
1892 if (ret) {
1893 pr_err("%s: regulator_set_voltage(%s)failed. min_uV=%d,max_uV=%d,ret=%d\n",
Asutosh Das0ef24812012-12-18 16:14:02 +05301894 __func__, vreg->name, min_uV, max_uV, ret);
1895 }
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301896 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301897
1898 return ret;
1899}
1900
1901static int sdhci_msm_vreg_enable(struct sdhci_msm_reg_data *vreg)
1902{
1903 int ret = 0;
1904
1905 /* Put regulator in HPM (high power mode) */
1906 ret = sdhci_msm_vreg_set_optimum_mode(vreg, vreg->hpm_uA);
1907 if (ret < 0)
1908 return ret;
1909
1910 if (!vreg->is_enabled) {
1911 /* Set voltage level */
1912 ret = sdhci_msm_vreg_set_voltage(vreg, vreg->high_vol_level,
1913 vreg->high_vol_level);
1914 if (ret)
1915 return ret;
1916 }
1917 ret = regulator_enable(vreg->reg);
1918 if (ret) {
1919 pr_err("%s: regulator_enable(%s) failed. ret=%d\n",
1920 __func__, vreg->name, ret);
1921 return ret;
1922 }
1923 vreg->is_enabled = true;
1924 return ret;
1925}
1926
1927static int sdhci_msm_vreg_disable(struct sdhci_msm_reg_data *vreg)
1928{
1929 int ret = 0;
1930
1931 /* Never disable regulator marked as always_on */
1932 if (vreg->is_enabled && !vreg->is_always_on) {
1933 ret = regulator_disable(vreg->reg);
1934 if (ret) {
1935 pr_err("%s: regulator_disable(%s) failed. ret=%d\n",
1936 __func__, vreg->name, ret);
1937 goto out;
1938 }
1939 vreg->is_enabled = false;
1940
1941 ret = sdhci_msm_vreg_set_optimum_mode(vreg, 0);
1942 if (ret < 0)
1943 goto out;
1944
1945 /* Set min. voltage level to 0 */
1946 ret = sdhci_msm_vreg_set_voltage(vreg, 0, vreg->high_vol_level);
1947 if (ret)
1948 goto out;
1949 } else if (vreg->is_enabled && vreg->is_always_on) {
1950 if (vreg->lpm_sup) {
1951 /* Put always_on regulator in LPM (low power mode) */
1952 ret = sdhci_msm_vreg_set_optimum_mode(vreg,
1953 vreg->lpm_uA);
1954 if (ret < 0)
1955 goto out;
1956 }
1957 }
1958out:
1959 return ret;
1960}
1961
1962static int sdhci_msm_setup_vreg(struct sdhci_msm_pltfm_data *pdata,
1963 bool enable, bool is_init)
1964{
1965 int ret = 0, i;
1966 struct sdhci_msm_slot_reg_data *curr_slot;
1967 struct sdhci_msm_reg_data *vreg_table[2];
1968
1969 curr_slot = pdata->vreg_data;
1970 if (!curr_slot) {
1971 pr_debug("%s: vreg info unavailable,assuming the slot is powered by always on domain\n",
1972 __func__);
1973 goto out;
1974 }
1975
1976 vreg_table[0] = curr_slot->vdd_data;
1977 vreg_table[1] = curr_slot->vdd_io_data;
1978
1979 for (i = 0; i < ARRAY_SIZE(vreg_table); i++) {
1980 if (vreg_table[i]) {
1981 if (enable)
1982 ret = sdhci_msm_vreg_enable(vreg_table[i]);
1983 else
1984 ret = sdhci_msm_vreg_disable(vreg_table[i]);
1985 if (ret)
1986 goto out;
1987 }
1988 }
1989out:
1990 return ret;
1991}
1992
1993/*
1994 * Reset vreg by ensuring it is off during probe. A call
1995 * to enable vreg is needed to balance disable vreg
1996 */
1997static int sdhci_msm_vreg_reset(struct sdhci_msm_pltfm_data *pdata)
1998{
1999 int ret;
2000
2001 ret = sdhci_msm_setup_vreg(pdata, 1, true);
2002 if (ret)
2003 return ret;
2004 ret = sdhci_msm_setup_vreg(pdata, 0, true);
2005 return ret;
2006}
2007
2008/* This init function should be called only once for each SDHC slot */
2009static int sdhci_msm_vreg_init(struct device *dev,
2010 struct sdhci_msm_pltfm_data *pdata,
2011 bool is_init)
2012{
2013 int ret = 0;
2014 struct sdhci_msm_slot_reg_data *curr_slot;
2015 struct sdhci_msm_reg_data *curr_vdd_reg, *curr_vdd_io_reg;
2016
2017 curr_slot = pdata->vreg_data;
2018 if (!curr_slot)
2019 goto out;
2020
2021 curr_vdd_reg = curr_slot->vdd_data;
2022 curr_vdd_io_reg = curr_slot->vdd_io_data;
2023
2024 if (!is_init)
2025 /* Deregister all regulators from regulator framework */
2026 goto vdd_io_reg_deinit;
2027
2028 /*
2029 * Get the regulator handle from voltage regulator framework
2030 * and then try to set the voltage level for the regulator
2031 */
2032 if (curr_vdd_reg) {
2033 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_reg);
2034 if (ret)
2035 goto out;
2036 }
2037 if (curr_vdd_io_reg) {
2038 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_io_reg);
2039 if (ret)
2040 goto vdd_reg_deinit;
2041 }
2042 ret = sdhci_msm_vreg_reset(pdata);
2043 if (ret)
2044 dev_err(dev, "vreg reset failed (%d)\n", ret);
2045 goto out;
2046
2047vdd_io_reg_deinit:
2048 if (curr_vdd_io_reg)
2049 sdhci_msm_vreg_deinit_reg(curr_vdd_io_reg);
2050vdd_reg_deinit:
2051 if (curr_vdd_reg)
2052 sdhci_msm_vreg_deinit_reg(curr_vdd_reg);
2053out:
2054 return ret;
2055}
2056
2057
2058static int sdhci_msm_set_vdd_io_vol(struct sdhci_msm_pltfm_data *pdata,
2059 enum vdd_io_level level,
2060 unsigned int voltage_level)
2061{
2062 int ret = 0;
2063 int set_level;
2064 struct sdhci_msm_reg_data *vdd_io_reg;
2065
2066 if (!pdata->vreg_data)
2067 return ret;
2068
2069 vdd_io_reg = pdata->vreg_data->vdd_io_data;
2070 if (vdd_io_reg && vdd_io_reg->is_enabled) {
2071 switch (level) {
2072 case VDD_IO_LOW:
2073 set_level = vdd_io_reg->low_vol_level;
2074 break;
2075 case VDD_IO_HIGH:
2076 set_level = vdd_io_reg->high_vol_level;
2077 break;
2078 case VDD_IO_SET_LEVEL:
2079 set_level = voltage_level;
2080 break;
2081 default:
2082 pr_err("%s: invalid argument level = %d",
2083 __func__, level);
2084 ret = -EINVAL;
2085 return ret;
2086 }
2087 ret = sdhci_msm_vreg_set_voltage(vdd_io_reg, set_level,
2088 set_level);
2089 }
2090 return ret;
2091}
2092
2093static irqreturn_t sdhci_msm_pwr_irq(int irq, void *data)
2094{
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002095 struct sdhci_host *host = (struct sdhci_host *)data;
2096 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2097 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Das0ef24812012-12-18 16:14:02 +05302098 u8 irq_status = 0;
2099 u8 irq_ack = 0;
2100 int ret = 0;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302101 int pwr_state = 0, io_level = 0;
2102 unsigned long flags;
Asutosh Das0ef24812012-12-18 16:14:02 +05302103
2104 irq_status = readb_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
2105 pr_debug("%s: Received IRQ(%d), status=0x%x\n",
2106 mmc_hostname(msm_host->mmc), irq, irq_status);
2107
2108 /* Clear the interrupt */
2109 writeb_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
2110 /*
2111 * SDHC has core_mem and hc_mem device memory and these memory
2112 * addresses do not fall within 1KB region. Hence, any update to
2113 * core_mem address space would require an mb() to ensure this gets
2114 * completed before its next update to registers within hc_mem.
2115 */
2116 mb();
2117
2118 /* Handle BUS ON/OFF*/
2119 if (irq_status & CORE_PWRCTL_BUS_ON) {
2120 ret = sdhci_msm_setup_vreg(msm_host->pdata, true, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302121 if (!ret) {
Asutosh Das0ef24812012-12-18 16:14:02 +05302122 ret = sdhci_msm_setup_pins(msm_host->pdata, true);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302123 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
2124 VDD_IO_HIGH, 0);
2125 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302126 if (ret)
2127 irq_ack |= CORE_PWRCTL_BUS_FAIL;
2128 else
2129 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302130
2131 pwr_state = REQ_BUS_ON;
2132 io_level = REQ_IO_HIGH;
Asutosh Das0ef24812012-12-18 16:14:02 +05302133 }
2134 if (irq_status & CORE_PWRCTL_BUS_OFF) {
2135 ret = sdhci_msm_setup_vreg(msm_host->pdata, false, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302136 if (!ret) {
Asutosh Das0ef24812012-12-18 16:14:02 +05302137 ret = sdhci_msm_setup_pins(msm_host->pdata, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302138 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
2139 VDD_IO_LOW, 0);
2140 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302141 if (ret)
2142 irq_ack |= CORE_PWRCTL_BUS_FAIL;
2143 else
2144 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302145
2146 pwr_state = REQ_BUS_OFF;
2147 io_level = REQ_IO_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05302148 }
2149 /* Handle IO LOW/HIGH */
2150 if (irq_status & CORE_PWRCTL_IO_LOW) {
2151 /* Switch voltage Low */
2152 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_LOW, 0);
2153 if (ret)
2154 irq_ack |= CORE_PWRCTL_IO_FAIL;
2155 else
2156 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302157
2158 io_level = REQ_IO_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05302159 }
2160 if (irq_status & CORE_PWRCTL_IO_HIGH) {
2161 /* Switch voltage High */
2162 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_HIGH, 0);
2163 if (ret)
2164 irq_ack |= CORE_PWRCTL_IO_FAIL;
2165 else
2166 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302167
2168 io_level = REQ_IO_HIGH;
Asutosh Das0ef24812012-12-18 16:14:02 +05302169 }
2170
2171 /* ACK status to the core */
2172 writeb_relaxed(irq_ack, (msm_host->core_mem + CORE_PWRCTL_CTL));
2173 /*
2174 * SDHC has core_mem and hc_mem device memory and these memory
2175 * addresses do not fall within 1KB region. Hence, any update to
2176 * core_mem address space would require an mb() to ensure this gets
2177 * completed before its next update to registers within hc_mem.
2178 */
2179 mb();
2180
Krishna Konda46fd1432014-10-30 21:13:27 -07002181 if ((io_level & REQ_IO_HIGH) && (msm_host->caps_0 & CORE_3_0V_SUPPORT))
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002182 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2183 ~CORE_IO_PAD_PWR_SWITCH),
2184 host->ioaddr + CORE_VENDOR_SPEC);
Krishna Konda46fd1432014-10-30 21:13:27 -07002185 else if ((io_level & REQ_IO_LOW) ||
2186 (msm_host->caps_0 & CORE_1_8V_SUPPORT))
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002187 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
2188 CORE_IO_PAD_PWR_SWITCH),
2189 host->ioaddr + CORE_VENDOR_SPEC);
2190 mb();
2191
Asutosh Das0ef24812012-12-18 16:14:02 +05302192 pr_debug("%s: Handled IRQ(%d), ret=%d, ack=0x%x\n",
2193 mmc_hostname(msm_host->mmc), irq, ret, irq_ack);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302194 spin_lock_irqsave(&host->lock, flags);
2195 if (pwr_state)
2196 msm_host->curr_pwr_state = pwr_state;
2197 if (io_level)
2198 msm_host->curr_io_level = io_level;
2199 complete(&msm_host->pwr_irq_completion);
2200 spin_unlock_irqrestore(&host->lock, flags);
2201
Asutosh Das0ef24812012-12-18 16:14:02 +05302202 return IRQ_HANDLED;
2203}
2204
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302205static ssize_t
Sahitya Tummala5c55b932013-06-20 14:00:18 +05302206show_polling(struct device *dev, struct device_attribute *attr, char *buf)
2207{
2208 struct sdhci_host *host = dev_get_drvdata(dev);
2209 int poll;
2210 unsigned long flags;
2211
2212 spin_lock_irqsave(&host->lock, flags);
2213 poll = !!(host->mmc->caps & MMC_CAP_NEEDS_POLL);
2214 spin_unlock_irqrestore(&host->lock, flags);
2215
2216 return snprintf(buf, PAGE_SIZE, "%d\n", poll);
2217}
2218
2219static ssize_t
2220store_polling(struct device *dev, struct device_attribute *attr,
2221 const char *buf, size_t count)
2222{
2223 struct sdhci_host *host = dev_get_drvdata(dev);
2224 int value;
2225 unsigned long flags;
2226
2227 if (!kstrtou32(buf, 0, &value)) {
2228 spin_lock_irqsave(&host->lock, flags);
2229 if (value) {
2230 host->mmc->caps |= MMC_CAP_NEEDS_POLL;
2231 mmc_detect_change(host->mmc, 0);
2232 } else {
2233 host->mmc->caps &= ~MMC_CAP_NEEDS_POLL;
2234 }
2235 spin_unlock_irqrestore(&host->lock, flags);
2236 }
2237 return count;
2238}
2239
2240static ssize_t
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302241show_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
2242 char *buf)
2243{
2244 struct sdhci_host *host = dev_get_drvdata(dev);
2245 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2246 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2247
2248 return snprintf(buf, PAGE_SIZE, "%u\n",
2249 msm_host->msm_bus_vote.is_max_bw_needed);
2250}
2251
2252static ssize_t
2253store_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
2254 const char *buf, size_t count)
2255{
2256 struct sdhci_host *host = dev_get_drvdata(dev);
2257 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2258 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2259 uint32_t value;
2260 unsigned long flags;
2261
2262 if (!kstrtou32(buf, 0, &value)) {
2263 spin_lock_irqsave(&host->lock, flags);
2264 msm_host->msm_bus_vote.is_max_bw_needed = !!value;
2265 spin_unlock_irqrestore(&host->lock, flags);
2266 }
2267 return count;
2268}
2269
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302270static void sdhci_msm_check_power_status(struct sdhci_host *host, u32 req_type)
Asutosh Das0ef24812012-12-18 16:14:02 +05302271{
2272 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2273 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302274 unsigned long flags;
2275 bool done = false;
Sahitya Tummala481fbb02013-08-06 15:22:28 +05302276 u32 io_sig_sts;
Asutosh Das0ef24812012-12-18 16:14:02 +05302277
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302278 spin_lock_irqsave(&host->lock, flags);
2279 pr_debug("%s: %s: request %d curr_pwr_state %x curr_io_level %x\n",
2280 mmc_hostname(host->mmc), __func__, req_type,
2281 msm_host->curr_pwr_state, msm_host->curr_io_level);
Sahitya Tummala481fbb02013-08-06 15:22:28 +05302282 io_sig_sts = readl_relaxed(msm_host->core_mem + CORE_GENERICS);
2283 /*
2284 * The IRQ for request type IO High/Low will be generated when -
2285 * 1. SWITCHABLE_SIGNALLING_VOL is enabled in HW.
2286 * 2. If 1 is true and when there is a state change in 1.8V enable
2287 * bit (bit 3) of SDHCI_HOST_CONTROL2 register. The reset state of
2288 * that bit is 0 which indicates 3.3V IO voltage. So, when MMC core
2289 * layer tries to set it to 3.3V before card detection happens, the
2290 * IRQ doesn't get triggered as there is no state change in this bit.
2291 * The driver already handles this case by changing the IO voltage
2292 * level to high as part of controller power up sequence. Hence, check
2293 * for host->pwr to handle a case where IO voltage high request is
2294 * issued even before controller power up.
2295 */
2296 if (req_type & (REQ_IO_HIGH | REQ_IO_LOW)) {
2297 if (!(io_sig_sts & SWITCHABLE_SIGNALLING_VOL) ||
2298 ((req_type & REQ_IO_HIGH) && !host->pwr)) {
2299 pr_debug("%s: do not wait for power IRQ that never comes\n",
2300 mmc_hostname(host->mmc));
2301 spin_unlock_irqrestore(&host->lock, flags);
2302 return;
2303 }
2304 }
2305
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302306 if ((req_type & msm_host->curr_pwr_state) ||
2307 (req_type & msm_host->curr_io_level))
2308 done = true;
2309 spin_unlock_irqrestore(&host->lock, flags);
Asutosh Das0ef24812012-12-18 16:14:02 +05302310
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302311 /*
2312 * This is needed here to hanlde a case where IRQ gets
2313 * triggered even before this function is called so that
2314 * x->done counter of completion gets reset. Otherwise,
2315 * next call to wait_for_completion returns immediately
2316 * without actually waiting for the IRQ to be handled.
2317 */
2318 if (done)
2319 init_completion(&msm_host->pwr_irq_completion);
2320 else
2321 wait_for_completion(&msm_host->pwr_irq_completion);
2322
2323 pr_debug("%s: %s: request %d done\n", mmc_hostname(host->mmc),
2324 __func__, req_type);
Asutosh Das0ef24812012-12-18 16:14:02 +05302325}
2326
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002327static void sdhci_msm_toggle_cdr(struct sdhci_host *host, bool enable)
2328{
2329 if (enable)
2330 writel_relaxed((readl_relaxed(host->ioaddr +
2331 CORE_DLL_CONFIG) | CORE_CDR_EN),
2332 host->ioaddr + CORE_DLL_CONFIG);
2333 else
2334 writel_relaxed((readl_relaxed(host->ioaddr +
2335 CORE_DLL_CONFIG) & ~CORE_CDR_EN),
2336 host->ioaddr + CORE_DLL_CONFIG);
2337}
2338
Asutosh Das648f9d12013-01-10 21:11:04 +05302339static unsigned int sdhci_msm_max_segs(void)
2340{
2341 return SDHCI_MSM_MAX_SEGMENTS;
2342}
2343
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302344static unsigned int sdhci_msm_get_min_clock(struct sdhci_host *host)
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302345{
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302346 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2347 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302348
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302349 return msm_host->pdata->sup_clk_table[0];
2350}
2351
2352static unsigned int sdhci_msm_get_max_clock(struct sdhci_host *host)
2353{
2354 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2355 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2356 int max_clk_index = msm_host->pdata->sup_clk_cnt;
2357
2358 return msm_host->pdata->sup_clk_table[max_clk_index - 1];
2359}
2360
2361static unsigned int sdhci_msm_get_sup_clk_rate(struct sdhci_host *host,
2362 u32 req_clk)
2363{
2364 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2365 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2366 unsigned int sel_clk = -1;
2367 unsigned char cnt;
2368
2369 if (req_clk < sdhci_msm_get_min_clock(host)) {
2370 sel_clk = sdhci_msm_get_min_clock(host);
2371 return sel_clk;
2372 }
2373
2374 for (cnt = 0; cnt < msm_host->pdata->sup_clk_cnt; cnt++) {
2375 if (msm_host->pdata->sup_clk_table[cnt] > req_clk) {
2376 break;
2377 } else if (msm_host->pdata->sup_clk_table[cnt] == req_clk) {
2378 sel_clk = msm_host->pdata->sup_clk_table[cnt];
2379 break;
2380 } else {
2381 sel_clk = msm_host->pdata->sup_clk_table[cnt];
2382 }
2383 }
2384 return sel_clk;
2385}
2386
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302387static int sdhci_msm_enable_controller_clock(struct sdhci_host *host)
2388{
2389 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2390 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2391 int rc = 0;
2392
2393 if (atomic_read(&msm_host->controller_clock))
2394 return 0;
2395
2396 sdhci_msm_bus_voting(host, 1);
2397
2398 if (!IS_ERR(msm_host->pclk)) {
2399 rc = clk_prepare_enable(msm_host->pclk);
2400 if (rc) {
2401 pr_err("%s: %s: failed to enable the pclk with error %d\n",
2402 mmc_hostname(host->mmc), __func__, rc);
2403 goto remove_vote;
2404 }
2405 }
2406
2407 rc = clk_prepare_enable(msm_host->clk);
2408 if (rc) {
2409 pr_err("%s: %s: failed to enable the host-clk with error %d\n",
2410 mmc_hostname(host->mmc), __func__, rc);
2411 goto disable_pclk;
2412 }
2413
2414 atomic_set(&msm_host->controller_clock, 1);
2415 pr_debug("%s: %s: enabled controller clock\n",
2416 mmc_hostname(host->mmc), __func__);
2417 goto out;
2418
2419disable_pclk:
2420 if (!IS_ERR(msm_host->pclk))
2421 clk_disable_unprepare(msm_host->pclk);
2422remove_vote:
2423 if (msm_host->msm_bus_vote.client_handle)
2424 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2425out:
2426 return rc;
2427}
2428
2429
2430
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302431static int sdhci_msm_prepare_clocks(struct sdhci_host *host, bool enable)
2432{
2433 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2434 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2435 int rc = 0;
2436
2437 if (enable && !atomic_read(&msm_host->clks_on)) {
2438 pr_debug("%s: request to enable clocks\n",
2439 mmc_hostname(host->mmc));
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302440
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302441 /*
2442 * The bus-width or the clock rate might have changed
2443 * after controller clocks are enbaled, update bus vote
2444 * in such case.
2445 */
2446 if (atomic_read(&msm_host->controller_clock))
2447 sdhci_msm_bus_voting(host, 1);
2448
2449 rc = sdhci_msm_enable_controller_clock(host);
2450 if (rc)
2451 goto remove_vote;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302452
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302453 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
2454 rc = clk_prepare_enable(msm_host->bus_clk);
2455 if (rc) {
2456 pr_err("%s: %s: failed to enable the bus-clock with error %d\n",
2457 mmc_hostname(host->mmc), __func__, rc);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302458 goto disable_controller_clk;
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302459 }
2460 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002461 if (!IS_ERR(msm_host->ff_clk)) {
2462 rc = clk_prepare_enable(msm_host->ff_clk);
2463 if (rc) {
2464 pr_err("%s: %s: failed to enable the ff_clk with error %d\n",
2465 mmc_hostname(host->mmc), __func__, rc);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302466 goto disable_bus_clk;
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002467 }
2468 }
2469 if (!IS_ERR(msm_host->sleep_clk)) {
2470 rc = clk_prepare_enable(msm_host->sleep_clk);
2471 if (rc) {
2472 pr_err("%s: %s: failed to enable the sleep_clk with error %d\n",
2473 mmc_hostname(host->mmc), __func__, rc);
2474 goto disable_ff_clk;
2475 }
2476 }
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302477 mb();
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302478
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302479 } else if (!enable && atomic_read(&msm_host->clks_on)) {
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302480 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
2481 mb();
Sahitya Tummaladc182982013-08-20 15:32:09 +05302482 /*
2483 * During 1.8V signal switching the clock source must
2484 * still be ON as it requires accessing SDHC
2485 * registers (SDHCi host control2 register bit 3 must
2486 * be written and polled after stopping the SDCLK).
2487 */
2488 if (host->mmc->card_clock_off)
2489 return 0;
2490 pr_debug("%s: request to disable clocks\n",
2491 mmc_hostname(host->mmc));
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002492 if (!IS_ERR_OR_NULL(msm_host->sleep_clk))
2493 clk_disable_unprepare(msm_host->sleep_clk);
2494 if (!IS_ERR_OR_NULL(msm_host->ff_clk))
2495 clk_disable_unprepare(msm_host->ff_clk);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302496 clk_disable_unprepare(msm_host->clk);
2497 if (!IS_ERR(msm_host->pclk))
2498 clk_disable_unprepare(msm_host->pclk);
2499 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2500 clk_disable_unprepare(msm_host->bus_clk);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302501
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302502 atomic_set(&msm_host->controller_clock, 0);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302503 sdhci_msm_bus_voting(host, 0);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302504 }
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302505 atomic_set(&msm_host->clks_on, enable);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302506 goto out;
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002507disable_ff_clk:
2508 if (!IS_ERR_OR_NULL(msm_host->ff_clk))
2509 clk_disable_unprepare(msm_host->ff_clk);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302510disable_bus_clk:
2511 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2512 clk_disable_unprepare(msm_host->bus_clk);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302513disable_controller_clk:
2514 if (!IS_ERR_OR_NULL(msm_host->clk))
2515 clk_disable_unprepare(msm_host->clk);
2516 if (!IS_ERR_OR_NULL(msm_host->pclk))
2517 clk_disable_unprepare(msm_host->pclk);
2518 atomic_set(&msm_host->controller_clock, 0);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302519remove_vote:
2520 if (msm_host->msm_bus_vote.client_handle)
2521 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302522out:
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302523 return rc;
2524}
2525
2526static void sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock)
2527{
2528 int rc;
2529 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2530 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2531 struct mmc_ios curr_ios = host->mmc->ios;
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002532 u32 sup_clock, ddr_clock, dll_lock;
Sahitya Tummala043744a2013-06-24 09:55:33 +05302533 bool curr_pwrsave;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302534
2535 if (!clock) {
Sujit Reddy Thummabf1aecc2014-01-10 10:58:54 +05302536 /*
2537 * disable pwrsave to ensure clock is not auto-gated until
2538 * the rate is >400KHz (initialization complete).
2539 */
2540 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2541 ~CORE_CLK_PWRSAVE, host->ioaddr + CORE_VENDOR_SPEC);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302542 sdhci_msm_prepare_clocks(host, false);
2543 host->clock = clock;
2544 goto out;
2545 }
2546
2547 rc = sdhci_msm_prepare_clocks(host, true);
2548 if (rc)
2549 goto out;
2550
Sahitya Tummala043744a2013-06-24 09:55:33 +05302551 curr_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2552 CORE_CLK_PWRSAVE);
Sahitya Tummalae000b242013-08-29 16:21:08 +05302553 if ((clock > 400000) &&
Venkat Gopalakrishnane6659b92015-01-21 15:51:37 -08002554 !curr_pwrsave)
Sahitya Tummala043744a2013-06-24 09:55:33 +05302555 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2556 | CORE_CLK_PWRSAVE,
2557 host->ioaddr + CORE_VENDOR_SPEC);
2558 /*
2559 * Disable pwrsave for a newly added card if doesn't allow clock
2560 * gating.
2561 */
Venkat Gopalakrishnane6659b92015-01-21 15:51:37 -08002562 else if (curr_pwrsave)
Sahitya Tummala043744a2013-06-24 09:55:33 +05302563 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2564 & ~CORE_CLK_PWRSAVE,
2565 host->ioaddr + CORE_VENDOR_SPEC);
2566
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302567 sup_clock = sdhci_msm_get_sup_clk_rate(host, clock);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002568 if ((curr_ios.timing == MMC_TIMING_UHS_DDR50) ||
Venkat Gopalakrishnan0a29da92015-01-09 12:19:16 -08002569 (curr_ios.timing == MMC_TIMING_MMC_DDR52) ||
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002570 (curr_ios.timing == MMC_TIMING_MMC_HS400)) {
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302571 /*
2572 * The SDHC requires internal clock frequency to be double the
2573 * actual clock that will be set for DDR mode. The controller
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002574 * uses the faster clock(100/400MHz) for some of its parts and
2575 * send the actual required clock (50/200MHz) to the card.
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302576 */
2577 ddr_clock = clock * 2;
2578 sup_clock = sdhci_msm_get_sup_clk_rate(host,
2579 ddr_clock);
2580 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002581
2582 /*
2583 * In general all timing modes are controlled via UHS mode select in
2584 * Host Control2 register. eMMC specific HS200/HS400 doesn't have
2585 * their respective modes defined here, hence we use these values.
2586 *
2587 * HS200 - SDR104 (Since they both are equivalent in functionality)
2588 * HS400 - This involves multiple configurations
2589 * Initially SDR104 - when tuning is required as HS200
2590 * Then when switching to DDR @ 400MHz (HS400) we use
2591 * the vendor specific HC_SELECT_IN to control the mode.
2592 *
2593 * In addition to controlling the modes we also need to select the
2594 * correct input clock for DLL depending on the mode.
2595 *
2596 * HS400 - divided clock (free running MCLK/2)
2597 * All other modes - default (free running MCLK)
2598 */
2599 if (curr_ios.timing == MMC_TIMING_MMC_HS400) {
2600 /* Select the divided clock (free running MCLK/2) */
2601 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2602 & ~CORE_HC_MCLK_SEL_MASK)
2603 | CORE_HC_MCLK_SEL_HS400),
2604 host->ioaddr + CORE_VENDOR_SPEC);
2605 /*
2606 * Select HS400 mode using the HC_SELECT_IN from VENDOR SPEC
2607 * register
2608 */
2609 if (msm_host->tuning_done && !msm_host->calibration_done) {
2610 /*
2611 * Write 0x6 to HC_SELECT_IN and 1 to HC_SELECT_IN_EN
2612 * field in VENDOR_SPEC_FUNC
2613 */
2614 writel_relaxed((readl_relaxed(host->ioaddr + \
2615 CORE_VENDOR_SPEC)
2616 | CORE_HC_SELECT_IN_HS400
2617 | CORE_HC_SELECT_IN_EN),
2618 host->ioaddr + CORE_VENDOR_SPEC);
2619 }
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002620 if (!host->mmc->ios.old_rate && !msm_host->use_cdclp533) {
2621 /*
2622 * Poll on DLL_LOCK and DDR_DLL_LOCK bits in
2623 * CORE_DLL_STATUS to be set. This should get set
2624 * with in 15 us at 200 MHz.
2625 */
2626 rc = readl_poll_timeout(host->ioaddr + CORE_DLL_STATUS,
2627 dll_lock, (dll_lock & (CORE_DLL_LOCK |
2628 CORE_DDR_DLL_LOCK)), 10, 1000);
2629 if (rc == -ETIMEDOUT)
2630 pr_err("%s: Unable to get DLL_LOCK/DDR_DLL_LOCK, dll_status: 0x%08x\n",
2631 mmc_hostname(host->mmc),
2632 dll_lock);
2633 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002634 } else {
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002635 if (!msm_host->use_cdclp533)
2636 /* set CORE_PWRSAVE_DLL bit in CORE_VENDOR_SPEC3 */
2637 writel_relaxed((readl_relaxed(host->ioaddr +
2638 CORE_VENDOR_SPEC3) & ~CORE_PWRSAVE_DLL),
2639 host->ioaddr + CORE_VENDOR_SPEC3);
2640
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002641 /* Select the default clock (free running MCLK) */
2642 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2643 & ~CORE_HC_MCLK_SEL_MASK)
2644 | CORE_HC_MCLK_SEL_DFLT),
2645 host->ioaddr + CORE_VENDOR_SPEC);
2646
2647 /*
2648 * Disable HC_SELECT_IN to be able to use the UHS mode select
2649 * configuration from Host Control2 register for all other
2650 * modes.
2651 *
2652 * Write 0 to HC_SELECT_IN and HC_SELECT_IN_EN field
2653 * in VENDOR_SPEC_FUNC
2654 */
2655 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2656 & ~CORE_HC_SELECT_IN_EN
2657 & ~CORE_HC_SELECT_IN_MASK),
2658 host->ioaddr + CORE_VENDOR_SPEC);
2659 }
2660 mb();
2661
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302662 if (sup_clock != msm_host->clk_rate) {
2663 pr_debug("%s: %s: setting clk rate to %u\n",
2664 mmc_hostname(host->mmc), __func__, sup_clock);
2665 rc = clk_set_rate(msm_host->clk, sup_clock);
2666 if (rc) {
2667 pr_err("%s: %s: Failed to set rate %u for host-clk : %d\n",
2668 mmc_hostname(host->mmc), __func__,
2669 sup_clock, rc);
2670 goto out;
2671 }
2672 msm_host->clk_rate = sup_clock;
2673 host->clock = clock;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302674 /*
2675 * Update the bus vote in case of frequency change due to
2676 * clock scaling.
2677 */
2678 sdhci_msm_bus_voting(host, 1);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302679 }
2680out:
2681 sdhci_set_clock(host, clock);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302682}
2683
Sahitya Tummala14613432013-03-21 11:13:25 +05302684static void sdhci_msm_set_uhs_signaling(struct sdhci_host *host,
2685 unsigned int uhs)
2686{
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002687 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2688 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala14613432013-03-21 11:13:25 +05302689 u16 ctrl_2;
2690
2691 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2692 /* Select Bus Speed Mode for host */
2693 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Venkat Gopalakrishnan0a29da92015-01-09 12:19:16 -08002694 if ((uhs == MMC_TIMING_MMC_HS400) ||
2695 (uhs == MMC_TIMING_MMC_HS200) ||
2696 (uhs == MMC_TIMING_UHS_SDR104))
Sahitya Tummala14613432013-03-21 11:13:25 +05302697 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2698 else if (uhs == MMC_TIMING_UHS_SDR12)
2699 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
2700 else if (uhs == MMC_TIMING_UHS_SDR25)
2701 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
2702 else if (uhs == MMC_TIMING_UHS_SDR50)
2703 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
Venkat Gopalakrishnan0a29da92015-01-09 12:19:16 -08002704 else if ((uhs == MMC_TIMING_UHS_DDR50) ||
2705 (uhs == MMC_TIMING_MMC_DDR52))
Sahitya Tummala14613432013-03-21 11:13:25 +05302706 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302707 /*
2708 * When clock frquency is less than 100MHz, the feedback clock must be
2709 * provided and DLL must not be used so that tuning can be skipped. To
2710 * provide feedback clock, the mode selection can be any value less
2711 * than 3'b011 in bits [2:0] of HOST CONTROL2 register.
2712 */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002713 if (host->clock <= CORE_FREQ_100MHZ) {
2714 if ((uhs == MMC_TIMING_MMC_HS400) ||
2715 (uhs == MMC_TIMING_MMC_HS200) ||
2716 (uhs == MMC_TIMING_UHS_SDR104))
2717 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302718
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002719 /*
2720 * Make sure DLL is disabled when not required
2721 *
2722 * Write 1 to DLL_RST bit of DLL_CONFIG register
2723 */
2724 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
2725 | CORE_DLL_RST),
2726 host->ioaddr + CORE_DLL_CONFIG);
2727
2728 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
2729 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
2730 | CORE_DLL_PDN),
2731 host->ioaddr + CORE_DLL_CONFIG);
2732 mb();
2733
2734 /*
2735 * The DLL needs to be restored and CDCLP533 recalibrated
2736 * when the clock frequency is set back to 400MHz.
2737 */
2738 msm_host->calibration_done = false;
2739 }
2740
2741 pr_debug("%s: %s-clock:%u uhs mode:%u ctrl_2:0x%x\n",
2742 mmc_hostname(host->mmc), __func__, host->clock, uhs, ctrl_2);
Sahitya Tummala14613432013-03-21 11:13:25 +05302743 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
2744
2745}
2746
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302747#define MAX_TEST_BUS 20
2748
2749void sdhci_msm_dump_vendor_regs(struct sdhci_host *host)
2750{
2751 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2752 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2753 int tbsel, tbsel2;
2754 int i, index = 0;
2755 u32 test_bus_val = 0;
2756 u32 debug_reg[MAX_TEST_BUS] = {0};
2757
2758 pr_info("----------- VENDOR REGISTER DUMP -----------\n");
2759 pr_info("Data cnt: 0x%08x | Fifo cnt: 0x%08x | Int sts: 0x%08x\n",
2760 readl_relaxed(msm_host->core_mem + CORE_MCI_DATA_CNT),
2761 readl_relaxed(msm_host->core_mem + CORE_MCI_FIFO_CNT),
2762 readl_relaxed(msm_host->core_mem + CORE_MCI_STATUS));
2763 pr_info("DLL cfg: 0x%08x | DLL sts: 0x%08x | SDCC ver: 0x%08x\n",
2764 readl_relaxed(host->ioaddr + CORE_DLL_CONFIG),
2765 readl_relaxed(host->ioaddr + CORE_DLL_STATUS),
2766 readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION));
2767 pr_info("Vndr func: 0x%08x | Vndr adma err : addr0: 0x%08x addr1: 0x%08x\n",
2768 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC),
2769 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_ADMA_ERR_ADDR0),
2770 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_ADMA_ERR_ADDR1));
2771
2772 /*
2773 * tbsel indicates [2:0] bits and tbsel2 indicates [7:4] bits
2774 * of CORE_TESTBUS_CONFIG register.
2775 *
2776 * To select test bus 0 to 7 use tbsel and to select any test bus
2777 * above 7 use (tbsel2 | tbsel) to get the test bus number. For eg,
2778 * to select test bus 14, write 0x1E to CORE_TESTBUS_CONFIG register
2779 * i.e., tbsel2[7:4] = 0001, tbsel[2:0] = 110.
2780 */
2781 for (tbsel2 = 0; tbsel2 < 3; tbsel2++) {
2782 for (tbsel = 0; tbsel < 8; tbsel++) {
2783 if (index >= MAX_TEST_BUS)
2784 break;
2785 test_bus_val = (tbsel2 << CORE_TESTBUS_SEL2_BIT) |
2786 tbsel | CORE_TESTBUS_ENA;
2787 writel_relaxed(test_bus_val,
2788 msm_host->core_mem + CORE_TESTBUS_CONFIG);
2789 debug_reg[index++] = readl_relaxed(msm_host->core_mem +
2790 CORE_SDCC_DEBUG_REG);
2791 }
2792 }
2793 for (i = 0; i < MAX_TEST_BUS; i = i + 4)
2794 pr_info(" Test bus[%d to %d]: 0x%08x 0x%08x 0x%08x 0x%08x\n",
2795 i, i + 3, debug_reg[i], debug_reg[i+1],
2796 debug_reg[i+2], debug_reg[i+3]);
2797 /* Disable test bus */
2798 writel_relaxed(~CORE_TESTBUS_ENA, msm_host->core_mem +
2799 CORE_TESTBUS_CONFIG);
2800}
2801
Asutosh Das0ef24812012-12-18 16:14:02 +05302802static struct sdhci_ops sdhci_msm_ops = {
Sahitya Tummala14613432013-03-21 11:13:25 +05302803 .set_uhs_signaling = sdhci_msm_set_uhs_signaling,
Asutosh Das0ef24812012-12-18 16:14:02 +05302804 .check_power_status = sdhci_msm_check_power_status,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002805 .platform_execute_tuning = sdhci_msm_execute_tuning,
2806 .toggle_cdr = sdhci_msm_toggle_cdr,
Asutosh Das648f9d12013-01-10 21:11:04 +05302807 .get_max_segments = sdhci_msm_max_segs,
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302808 .set_clock = sdhci_msm_set_clock,
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302809 .get_min_clock = sdhci_msm_get_min_clock,
2810 .get_max_clock = sdhci_msm_get_max_clock,
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302811 .dump_vendor_regs = sdhci_msm_dump_vendor_regs,
Asutosh Dase5e9ca62013-07-30 19:08:36 +05302812 .config_auto_tuning_cmd = sdhci_msm_config_auto_tuning_cmd,
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302813 .enable_controller_clock = sdhci_msm_enable_controller_clock,
Venkat Gopalakrishnanb8cb7072015-01-09 11:04:34 -08002814 .set_bus_width = sdhci_set_bus_width,
Venkat Gopalakrishnan411df072015-01-09 11:09:44 -08002815 .reset = sdhci_reset,
Asutosh Das0ef24812012-12-18 16:14:02 +05302816};
2817
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302818static void sdhci_set_default_hw_caps(struct sdhci_msm_host *msm_host,
2819 struct sdhci_host *host)
2820{
Krishna Konda46fd1432014-10-30 21:13:27 -07002821 u32 version, caps = 0;
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302822 u16 minor;
2823 u8 major;
2824
2825 version = readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION);
2826 major = (version & CORE_VERSION_MAJOR_MASK) >>
2827 CORE_VERSION_MAJOR_SHIFT;
2828 minor = version & CORE_VERSION_TARGET_MASK;
2829
Krishna Konda46fd1432014-10-30 21:13:27 -07002830 caps = readl_relaxed(host->ioaddr + SDHCI_CAPABILITIES);
2831
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302832 /*
2833 * Starting with SDCC 5 controller (core major version = 1)
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002834 * controller won't advertise 3.0v, 1.8v and 8-bit features
2835 * except for some targets.
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302836 */
2837 if (major >= 1 && minor != 0x11 && minor != 0x12) {
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002838 struct sdhci_msm_reg_data *vdd_io_reg;
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002839 /*
2840 * Enable 1.8V support capability on controllers that
2841 * support dual voltage
2842 */
2843 vdd_io_reg = msm_host->pdata->vreg_data->vdd_io_data;
Krishna Konda46fd1432014-10-30 21:13:27 -07002844 if (vdd_io_reg && (vdd_io_reg->high_vol_level > 2700000))
2845 caps |= CORE_3_0V_SUPPORT;
2846 if (vdd_io_reg && (vdd_io_reg->low_vol_level < 1950000))
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002847 caps |= CORE_1_8V_SUPPORT;
Pratibhasagar Vada47992013-12-09 20:42:32 +05302848 if (msm_host->pdata->mmc_bus_width == MMC_CAP_8_BIT_DATA)
2849 caps |= CORE_8_BIT_SUPPORT;
Krishna Konda46fd1432014-10-30 21:13:27 -07002850 writel_relaxed(caps, host->ioaddr +
2851 CORE_VENDOR_SPEC_CAPABILITIES0);
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302852 }
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002853
2854 /*
2855 * SDCC 5 controller with major version 1, minor version 0x34 and later
2856 * with HS 400 mode support will use CM DLL instead of CDC LP 533 DLL.
2857 */
2858 if ((major == 1) && (minor < 0x34))
2859 msm_host->use_cdclp533 = true;
Gilad Broner2a10ca02014-10-02 17:20:35 +03002860
2861 /*
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -08002862 * SDCC 5 controller with major version 1, minor version 0x42 and later
2863 * will require additional steps when resetting DLL.
2864 */
2865 if ((major == 1) && (minor >= 0x42))
2866 msm_host->use_updated_dll_reset = true;
2867
2868 /*
Gilad Broner2a10ca02014-10-02 17:20:35 +03002869 * Mask 64-bit support for controller with 32-bit address bus so that
2870 * smaller descriptor size will be used and improve memory consumption.
2871 * In case bus addressing ever changes, controller version should be
2872 * used in order to decide whether or not to mask 64-bit support.
2873 */
Gilad Broner2a10ca02014-10-02 17:20:35 +03002874 caps &= ~CORE_SYS_BUS_SUPPORT_64_BIT;
2875 writel_relaxed(caps, host->ioaddr + CORE_VENDOR_SPEC_CAPABILITIES0);
Krishna Konda46fd1432014-10-30 21:13:27 -07002876 /* keep track of the value in SDHCI_CAPABILITIES */
2877 msm_host->caps_0 = caps;
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302878}
2879
Asutosh Das0ef24812012-12-18 16:14:02 +05302880static int sdhci_msm_probe(struct platform_device *pdev)
2881{
2882 struct sdhci_host *host;
2883 struct sdhci_pltfm_host *pltfm_host;
2884 struct sdhci_msm_host *msm_host;
2885 struct resource *core_memres = NULL;
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02002886 int ret = 0, dead = 0;
Stephen Boyd8dce5c62013-04-24 14:19:46 -07002887 u16 host_version;
Subhash Jadavani28137342013-05-14 17:46:43 +05302888 u32 pwr, irq_status, irq_ctl;
Asutosh Das0ef24812012-12-18 16:14:02 +05302889
2890 pr_debug("%s: Enter %s\n", dev_name(&pdev->dev), __func__);
2891 msm_host = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_msm_host),
2892 GFP_KERNEL);
2893 if (!msm_host) {
2894 ret = -ENOMEM;
2895 goto out;
2896 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302897
2898 msm_host->sdhci_msm_pdata.ops = &sdhci_msm_ops;
2899 host = sdhci_pltfm_init(pdev, &msm_host->sdhci_msm_pdata, 0);
2900 if (IS_ERR(host)) {
2901 ret = PTR_ERR(host);
2902 goto out;
2903 }
2904
2905 pltfm_host = sdhci_priv(host);
2906 pltfm_host->priv = msm_host;
2907 msm_host->mmc = host->mmc;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302908 msm_host->pdev = pdev;
Asutosh Das0ef24812012-12-18 16:14:02 +05302909
2910 /* Extract platform data */
2911 if (pdev->dev.of_node) {
Venkat Gopalakrishnan270580a2013-03-11 12:17:57 -07002912 ret = of_alias_get_id(pdev->dev.of_node, "sdhc");
2913 if (ret < 0) {
2914 dev_err(&pdev->dev, "Failed to get slot index %d\n",
2915 ret);
2916 goto pltfm_free;
2917 }
2918 if (disable_slots & (1 << (ret - 1))) {
2919 dev_info(&pdev->dev, "%s: Slot %d disabled\n", __func__,
2920 ret);
2921 ret = -ENODEV;
2922 goto pltfm_free;
2923 }
2924
Asutosh Das0ef24812012-12-18 16:14:02 +05302925 msm_host->pdata = sdhci_msm_populate_pdata(&pdev->dev);
2926 if (!msm_host->pdata) {
2927 dev_err(&pdev->dev, "DT parsing error\n");
2928 goto pltfm_free;
2929 }
2930 } else {
2931 dev_err(&pdev->dev, "No device tree node\n");
2932 goto pltfm_free;
2933 }
2934
2935 /* Setup Clocks */
2936
2937 /* Setup SDCC bus voter clock. */
2938 msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus_clk");
2939 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
2940 /* Vote for max. clk rate for max. performance */
2941 ret = clk_set_rate(msm_host->bus_clk, INT_MAX);
2942 if (ret)
2943 goto pltfm_free;
2944 ret = clk_prepare_enable(msm_host->bus_clk);
2945 if (ret)
2946 goto pltfm_free;
2947 }
2948
2949 /* Setup main peripheral bus clock */
2950 msm_host->pclk = devm_clk_get(&pdev->dev, "iface_clk");
2951 if (!IS_ERR(msm_host->pclk)) {
2952 ret = clk_prepare_enable(msm_host->pclk);
2953 if (ret)
2954 goto bus_clk_disable;
2955 }
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302956 atomic_set(&msm_host->controller_clock, 1);
Asutosh Das0ef24812012-12-18 16:14:02 +05302957
2958 /* Setup SDC MMC clock */
2959 msm_host->clk = devm_clk_get(&pdev->dev, "core_clk");
2960 if (IS_ERR(msm_host->clk)) {
2961 ret = PTR_ERR(msm_host->clk);
2962 goto pclk_disable;
2963 }
2964
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302965 /* Set to the minimum supported clock frequency */
2966 ret = clk_set_rate(msm_host->clk, sdhci_msm_get_min_clock(host));
2967 if (ret) {
2968 dev_err(&pdev->dev, "MClk rate set failed (%d)\n", ret);
Sahitya Tummala020ede0d2013-06-07 13:03:07 +05302969 goto pclk_disable;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302970 }
Sahitya Tummala020ede0d2013-06-07 13:03:07 +05302971 ret = clk_prepare_enable(msm_host->clk);
2972 if (ret)
2973 goto pclk_disable;
2974
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302975 msm_host->clk_rate = sdhci_msm_get_min_clock(host);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302976 atomic_set(&msm_host->clks_on, 1);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302977
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002978 /* Setup CDC calibration fixed feedback clock */
2979 msm_host->ff_clk = devm_clk_get(&pdev->dev, "cal_clk");
2980 if (!IS_ERR(msm_host->ff_clk)) {
2981 ret = clk_prepare_enable(msm_host->ff_clk);
2982 if (ret)
2983 goto clk_disable;
2984 }
2985
2986 /* Setup CDC calibration sleep clock */
2987 msm_host->sleep_clk = devm_clk_get(&pdev->dev, "sleep_clk");
2988 if (!IS_ERR(msm_host->sleep_clk)) {
2989 ret = clk_prepare_enable(msm_host->sleep_clk);
2990 if (ret)
2991 goto ff_clk_disable;
2992 }
2993
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07002994 msm_host->saved_tuning_phase = INVALID_TUNING_PHASE;
2995
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302996 ret = sdhci_msm_bus_register(msm_host, pdev);
2997 if (ret)
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002998 goto sleep_clk_disable;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302999
3000 if (msm_host->msm_bus_vote.client_handle)
3001 INIT_DELAYED_WORK(&msm_host->msm_bus_vote.vote_work,
3002 sdhci_msm_bus_work);
3003 sdhci_msm_bus_voting(host, 1);
3004
Asutosh Das0ef24812012-12-18 16:14:02 +05303005 /* Setup regulators */
3006 ret = sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, true);
3007 if (ret) {
3008 dev_err(&pdev->dev, "Regulator setup failed (%d)\n", ret);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05303009 goto bus_unregister;
Asutosh Das0ef24812012-12-18 16:14:02 +05303010 }
3011
3012 /* Reset the core and Enable SDHC mode */
3013 core_memres = platform_get_resource_byname(pdev,
3014 IORESOURCE_MEM, "core_mem");
Asutosh Das890bdee2014-08-08 23:01:42 +05303015 if (!core_memres) {
3016 dev_err(&pdev->dev, "Failed to get iomem resource\n");
3017 goto vreg_deinit;
3018 }
Asutosh Das0ef24812012-12-18 16:14:02 +05303019 msm_host->core_mem = devm_ioremap(&pdev->dev, core_memres->start,
3020 resource_size(core_memres));
3021
3022 if (!msm_host->core_mem) {
3023 dev_err(&pdev->dev, "Failed to remap registers\n");
3024 ret = -ENOMEM;
3025 goto vreg_deinit;
3026 }
3027
Stepan Moskovchenkoa4d0fa72013-09-13 22:19:33 -07003028 /* Unset HC_MODE_EN bit in HC_MODE register */
3029 writel_relaxed(0, (msm_host->core_mem + CORE_HC_MODE));
3030
Asutosh Das0ef24812012-12-18 16:14:02 +05303031 /* Set SW_RST bit in POWER register (Offset 0x0) */
Sahitya Tummala66b0fe32013-04-25 11:50:56 +05303032 writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_POWER) |
3033 CORE_SW_RST, msm_host->core_mem + CORE_POWER);
3034 /*
3035 * SW reset can take upto 10HCLK + 15MCLK cycles.
3036 * Calculating based on min clk rates (hclk = 27MHz,
3037 * mclk = 400KHz) it comes to ~40us. Let's poll for
3038 * max. 1ms for reset completion.
3039 */
3040 ret = readl_poll_timeout(msm_host->core_mem + CORE_POWER,
Matt Wagantallc7097a22014-04-29 15:48:15 -07003041 pwr, !(pwr & CORE_SW_RST), 10, 1000);
Sahitya Tummala66b0fe32013-04-25 11:50:56 +05303042
3043 if (ret) {
3044 dev_err(&pdev->dev, "reset failed (%d)\n", ret);
3045 goto vreg_deinit;
3046 }
Asutosh Das0ef24812012-12-18 16:14:02 +05303047 /* Set HC_MODE_EN bit in HC_MODE register */
3048 writel_relaxed(HC_MODE_EN, (msm_host->core_mem + CORE_HC_MODE));
3049
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07003050 /* Set FF_CLK_SW_RST_DIS bit in HC_MODE register */
3051 writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_HC_MODE) |
3052 FF_CLK_SW_RST_DIS, msm_host->core_mem + CORE_HC_MODE);
3053
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05303054 sdhci_set_default_hw_caps(msm_host, host);
Krishna Konda46fd1432014-10-30 21:13:27 -07003055
3056 /*
3057 * Set the PAD_PWR_SWTICH_EN bit so that the PAD_PWR_SWITCH bit can
3058 * be used as required later on.
3059 */
3060 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
3061 CORE_IO_PAD_PWR_SWITCH_EN),
3062 host->ioaddr + CORE_VENDOR_SPEC);
Asutosh Das0ef24812012-12-18 16:14:02 +05303063 /*
Subhash Jadavani28137342013-05-14 17:46:43 +05303064 * CORE_SW_RST above may trigger power irq if previous status of PWRCTL
3065 * was either BUS_ON or IO_HIGH_V. So before we enable the power irq
3066 * interrupt in GIC (by registering the interrupt handler), we need to
3067 * ensure that any pending power irq interrupt status is acknowledged
3068 * otherwise power irq interrupt handler would be fired prematurely.
3069 */
3070 irq_status = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
3071 writel_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
3072 irq_ctl = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_CTL);
3073 if (irq_status & (CORE_PWRCTL_BUS_ON | CORE_PWRCTL_BUS_OFF))
3074 irq_ctl |= CORE_PWRCTL_BUS_SUCCESS;
3075 if (irq_status & (CORE_PWRCTL_IO_HIGH | CORE_PWRCTL_IO_LOW))
3076 irq_ctl |= CORE_PWRCTL_IO_SUCCESS;
3077 writel_relaxed(irq_ctl, (msm_host->core_mem + CORE_PWRCTL_CTL));
Krishna Konda46fd1432014-10-30 21:13:27 -07003078
Subhash Jadavani28137342013-05-14 17:46:43 +05303079 /*
3080 * Ensure that above writes are propogated before interrupt enablement
3081 * in GIC.
3082 */
3083 mb();
3084
3085 /*
Asutosh Das0ef24812012-12-18 16:14:02 +05303086 * Following are the deviations from SDHC spec v3.0 -
3087 * 1. Card detection is handled using separate GPIO.
3088 * 2. Bus power control is handled by interacting with PMIC.
3089 */
3090 host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
3091 host->quirks |= SDHCI_QUIRK_SINGLE_POWER_WRITE;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05303092 host->quirks |= SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN;
3093 host->quirks2 |= SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK;
Sahitya Tummala87d43942013-04-12 11:49:11 +05303094 host->quirks2 |= SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD;
Sahitya Tummala314162c2013-04-12 12:11:20 +05303095 host->quirks2 |= SDHCI_QUIRK2_BROKEN_PRESET_VALUE;
Sahitya Tummala7c9780d2013-04-12 11:59:25 +05303096 host->quirks2 |= SDHCI_QUIRK2_USE_RESERVED_MAX_TIMEOUT;
Asutosh Das0ef24812012-12-18 16:14:02 +05303097
Sahitya Tummalaa5733ab52013-06-10 16:32:51 +05303098 if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK)
3099 host->quirks2 |= SDHCI_QUIRK2_DIVIDE_TOUT_BY_4;
3100
Stephen Boyd8dce5c62013-04-24 14:19:46 -07003101 host_version = readw_relaxed((host->ioaddr + SDHCI_HOST_VERSION));
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07003102 dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n",
3103 host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >>
3104 SDHCI_VENDOR_VER_SHIFT));
3105 if (((host_version & SDHCI_VENDOR_VER_MASK) >>
3106 SDHCI_VENDOR_VER_SHIFT) == SDHCI_VER_100) {
3107 /*
3108 * Add 40us delay in interrupt handler when
3109 * operating at initialization frequency(400KHz).
3110 */
3111 host->quirks2 |= SDHCI_QUIRK2_SLOW_INT_CLR;
3112 /*
3113 * Set Software Reset for DAT line in Software
3114 * Reset Register (Bit 2).
3115 */
3116 host->quirks2 |= SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT;
3117 }
3118
Asutosh Das214b9662013-06-13 14:27:42 +05303119 host->quirks2 |= SDHCI_QUIRK2_IGN_DATA_END_BIT_ERROR;
3120
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07003121 /* Setup PWRCTL irq */
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02003122 msm_host->pwr_irq = platform_get_irq_byname(pdev, "pwr_irq");
3123 if (msm_host->pwr_irq < 0) {
Asutosh Das0ef24812012-12-18 16:14:02 +05303124 dev_err(&pdev->dev, "Failed to get pwr_irq by name (%d)\n",
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02003125 msm_host->pwr_irq);
Asutosh Das0ef24812012-12-18 16:14:02 +05303126 goto vreg_deinit;
3127 }
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02003128 ret = devm_request_threaded_irq(&pdev->dev, msm_host->pwr_irq, NULL,
Asutosh Das0ef24812012-12-18 16:14:02 +05303129 sdhci_msm_pwr_irq, IRQF_ONESHOT,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07003130 dev_name(&pdev->dev), host);
Asutosh Das0ef24812012-12-18 16:14:02 +05303131 if (ret) {
3132 dev_err(&pdev->dev, "Request threaded irq(%d) failed (%d)\n",
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02003133 msm_host->pwr_irq, ret);
Asutosh Das0ef24812012-12-18 16:14:02 +05303134 goto vreg_deinit;
3135 }
3136
3137 /* Enable pwr irq interrupts */
3138 writel_relaxed(INT_MASK, (msm_host->core_mem + CORE_PWRCTL_MASK));
3139
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05303140#ifdef CONFIG_MMC_CLKGATE
3141 /* Set clock gating delay to be used when CONFIG_MMC_CLKGATE is set */
3142 msm_host->mmc->clkgate_delay = SDHCI_MSM_MMC_CLK_GATE_DELAY;
3143#endif
3144
Asutosh Das0ef24812012-12-18 16:14:02 +05303145 /* Set host capabilities */
3146 msm_host->mmc->caps |= msm_host->pdata->mmc_bus_width;
3147 msm_host->mmc->caps |= msm_host->pdata->caps;
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003148 msm_host->mmc->caps |= MMC_CAP_AGGRESSIVE_PM;
Asutosh Das0ef24812012-12-18 16:14:02 +05303149 msm_host->mmc->caps2 |= msm_host->pdata->caps2;
Venkat Gopalakrishnan97c16112014-12-16 18:26:25 -08003150 msm_host->mmc->caps2 |= MMC_CAP2_BOOTPART_NOACC;
3151 msm_host->mmc->caps2 |= MMC_CAP2_FULL_PWR_CYCLE;
Subhash Jadavani6d472b22013-05-29 15:52:10 +05303152 msm_host->mmc->caps2 |= MMC_CAP2_ASYNC_SDIO_IRQ_4BIT_MODE;
Venkat Gopalakrishnan97c16112014-12-16 18:26:25 -08003153 msm_host->mmc->caps2 |= MMC_CAP2_HS400_POST_TUNING;
Asutosh Dasbea115d2013-06-24 18:20:45 +05303154 msm_host->mmc->pm_caps |= MMC_PM_KEEP_POWER;
Asutosh Das0ef24812012-12-18 16:14:02 +05303155
3156 if (msm_host->pdata->nonremovable)
3157 msm_host->mmc->caps |= MMC_CAP_NONREMOVABLE;
3158
Guoping Yuf7c91332014-08-20 16:56:18 +08003159 if (msm_host->pdata->nonhotplug)
3160 msm_host->mmc->caps2 |= MMC_CAP2_NONHOTPLUG;
3161
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05303162 host->cpu_dma_latency_us = msm_host->pdata->cpu_dma_latency_us;
Maya Erez994cf2f2014-10-21 20:22:04 +03003163 host->pm_qos_req_dma.type = msm_host->pdata->cpu_affinity_type;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05303164
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05303165 init_completion(&msm_host->pwr_irq_completion);
3166
Sahitya Tummala581df132013-03-12 14:57:46 +05303167 if (gpio_is_valid(msm_host->pdata->status_gpio)) {
Sahitya Tummala6ddabb42014-06-05 13:26:55 +05303168 /*
3169 * Set up the card detect GPIO in active configuration before
3170 * configuring it as an IRQ. Otherwise, it can be in some
3171 * weird/inconsistent state resulting in flood of interrupts.
3172 */
3173 sdhci_msm_setup_pins(msm_host->pdata, true);
3174
Sahitya Tummala581df132013-03-12 14:57:46 +05303175 ret = mmc_gpio_request_cd(msm_host->mmc,
3176 msm_host->pdata->status_gpio, 0);
3177 if (ret) {
3178 dev_err(&pdev->dev, "%s: Failed to request card detection IRQ %d\n",
3179 __func__, ret);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05303180 goto vreg_deinit;
Sahitya Tummala581df132013-03-12 14:57:46 +05303181 }
3182 }
3183
Krishna Konda7feab352013-09-17 23:55:40 -07003184 if ((sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT) &&
3185 (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(64)))) {
3186 host->dma_mask = DMA_BIT_MASK(64);
3187 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
3188 } else if (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(32))) {
Sahitya Tummalaeaa21862013-03-20 19:34:59 +05303189 host->dma_mask = DMA_BIT_MASK(32);
3190 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
3191 } else {
3192 dev_err(&pdev->dev, "%s: Failed to set dma mask\n", __func__);
3193 }
3194
Asutosh Das0ef24812012-12-18 16:14:02 +05303195 ret = sdhci_add_host(host);
3196 if (ret) {
3197 dev_err(&pdev->dev, "Add host failed (%d)\n", ret);
Sahitya Tummala581df132013-03-12 14:57:46 +05303198 goto vreg_deinit;
Asutosh Das0ef24812012-12-18 16:14:02 +05303199 }
3200
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003201 pm_runtime_set_active(&pdev->dev);
3202 pm_runtime_enable(&pdev->dev);
3203 pm_runtime_set_autosuspend_delay(&pdev->dev, MSM_AUTOSUSPEND_DELAY_MS);
3204 pm_runtime_use_autosuspend(&pdev->dev);
3205
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05303206 msm_host->msm_bus_vote.max_bus_bw.show = show_sdhci_max_bus_bw;
3207 msm_host->msm_bus_vote.max_bus_bw.store = store_sdhci_max_bus_bw;
3208 sysfs_attr_init(&msm_host->msm_bus_vote.max_bus_bw.attr);
3209 msm_host->msm_bus_vote.max_bus_bw.attr.name = "max_bus_bw";
3210 msm_host->msm_bus_vote.max_bus_bw.attr.mode = S_IRUGO | S_IWUSR;
3211 ret = device_create_file(&pdev->dev,
3212 &msm_host->msm_bus_vote.max_bus_bw);
3213 if (ret)
3214 goto remove_host;
3215
Sahitya Tummala5c55b932013-06-20 14:00:18 +05303216 if (!gpio_is_valid(msm_host->pdata->status_gpio)) {
3217 msm_host->polling.show = show_polling;
3218 msm_host->polling.store = store_polling;
3219 sysfs_attr_init(&msm_host->polling.attr);
3220 msm_host->polling.attr.name = "polling";
3221 msm_host->polling.attr.mode = S_IRUGO | S_IWUSR;
3222 ret = device_create_file(&pdev->dev, &msm_host->polling);
3223 if (ret)
3224 goto remove_max_bus_bw_file;
3225 }
Asutosh Dase5e9ca62013-07-30 19:08:36 +05303226
3227 msm_host->auto_cmd21_attr.show = show_auto_cmd21;
3228 msm_host->auto_cmd21_attr.store = store_auto_cmd21;
3229 sysfs_attr_init(&msm_host->auto_cmd21_attr.attr);
3230 msm_host->auto_cmd21_attr.attr.name = "enable_auto_cmd21";
3231 msm_host->auto_cmd21_attr.attr.mode = S_IRUGO | S_IWUSR;
3232 ret = device_create_file(&pdev->dev, &msm_host->auto_cmd21_attr);
3233 if (ret) {
3234 pr_err("%s: %s: failed creating auto-cmd21 attr: %d\n",
3235 mmc_hostname(host->mmc), __func__, ret);
3236 device_remove_file(&pdev->dev, &msm_host->auto_cmd21_attr);
3237 }
Asutosh Das0ef24812012-12-18 16:14:02 +05303238 /* Successful initialization */
3239 goto out;
3240
Sahitya Tummala5c55b932013-06-20 14:00:18 +05303241remove_max_bus_bw_file:
3242 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Asutosh Das0ef24812012-12-18 16:14:02 +05303243remove_host:
3244 dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003245 pm_runtime_disable(&pdev->dev);
Asutosh Das0ef24812012-12-18 16:14:02 +05303246 sdhci_remove_host(host, dead);
3247vreg_deinit:
3248 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05303249bus_unregister:
3250 if (msm_host->msm_bus_vote.client_handle)
3251 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
3252 sdhci_msm_bus_unregister(msm_host);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07003253sleep_clk_disable:
3254 if (!IS_ERR(msm_host->sleep_clk))
3255 clk_disable_unprepare(msm_host->sleep_clk);
3256ff_clk_disable:
3257 if (!IS_ERR(msm_host->ff_clk))
3258 clk_disable_unprepare(msm_host->ff_clk);
Asutosh Das0ef24812012-12-18 16:14:02 +05303259clk_disable:
3260 if (!IS_ERR(msm_host->clk))
3261 clk_disable_unprepare(msm_host->clk);
3262pclk_disable:
3263 if (!IS_ERR(msm_host->pclk))
3264 clk_disable_unprepare(msm_host->pclk);
3265bus_clk_disable:
3266 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
3267 clk_disable_unprepare(msm_host->bus_clk);
3268pltfm_free:
3269 sdhci_pltfm_free(pdev);
3270out:
3271 pr_debug("%s: Exit %s\n", dev_name(&pdev->dev), __func__);
3272 return ret;
3273}
3274
3275static int sdhci_msm_remove(struct platform_device *pdev)
3276{
3277 struct sdhci_host *host = platform_get_drvdata(pdev);
3278 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3279 struct sdhci_msm_host *msm_host = pltfm_host->priv;
3280 struct sdhci_msm_pltfm_data *pdata = msm_host->pdata;
3281 int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) ==
3282 0xffffffff);
3283
3284 pr_debug("%s: %s\n", dev_name(&pdev->dev), __func__);
Sahitya Tummala5c55b932013-06-20 14:00:18 +05303285 if (!gpio_is_valid(msm_host->pdata->status_gpio))
3286 device_remove_file(&pdev->dev, &msm_host->polling);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05303287 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003288 pm_runtime_disable(&pdev->dev);
Asutosh Das0ef24812012-12-18 16:14:02 +05303289 sdhci_remove_host(host, dead);
3290 sdhci_pltfm_free(pdev);
Sahitya Tummala581df132013-03-12 14:57:46 +05303291
Asutosh Das0ef24812012-12-18 16:14:02 +05303292 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05303293
Pratibhasagar V9acf2642013-11-21 21:07:21 +05303294 sdhci_msm_setup_pins(pdata, true);
3295 sdhci_msm_setup_pins(pdata, false);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05303296
3297 if (msm_host->msm_bus_vote.client_handle) {
3298 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
3299 sdhci_msm_bus_unregister(msm_host);
3300 }
Asutosh Das0ef24812012-12-18 16:14:02 +05303301 return 0;
3302}
3303
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003304#ifdef CONFIG_PM
3305static int sdhci_msm_runtime_suspend(struct device *dev)
3306{
3307 struct sdhci_host *host = dev_get_drvdata(dev);
3308 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3309 struct sdhci_msm_host *msm_host = pltfm_host->priv;
3310
3311 disable_irq(host->irq);
3312 disable_irq(msm_host->pwr_irq);
3313
3314 /*
3315 * Remove the vote immediately only if clocks are off in which
3316 * case we might have queued work to remove vote but it may not
3317 * be completed before runtime suspend or system suspend.
3318 */
3319 if (!atomic_read(&msm_host->clks_on)) {
3320 if (msm_host->msm_bus_vote.client_handle)
3321 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
3322 }
3323
3324 return 0;
3325}
3326
3327static int sdhci_msm_runtime_resume(struct device *dev)
3328{
3329 struct sdhci_host *host = dev_get_drvdata(dev);
3330 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3331 struct sdhci_msm_host *msm_host = pltfm_host->priv;
3332
3333 enable_irq(msm_host->pwr_irq);
3334 enable_irq(host->irq);
3335
3336 return 0;
3337}
3338
3339static int sdhci_msm_suspend(struct device *dev)
3340{
3341 struct sdhci_host *host = dev_get_drvdata(dev);
3342 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3343 struct sdhci_msm_host *msm_host = pltfm_host->priv;
3344
3345 if (gpio_is_valid(msm_host->pdata->status_gpio) &&
3346 (msm_host->mmc->slot.cd_irq >= 0))
3347 disable_irq(msm_host->mmc->slot.cd_irq);
3348
3349 if (pm_runtime_suspended(dev)) {
3350 pr_debug("%s: %s: already runtime suspended\n",
3351 mmc_hostname(host->mmc), __func__);
3352 goto out;
3353 }
3354 return sdhci_msm_runtime_suspend(dev);
3355out:
3356 return 0;
3357}
3358
3359static int sdhci_msm_resume(struct device *dev)
3360{
3361 struct sdhci_host *host = dev_get_drvdata(dev);
3362 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3363 struct sdhci_msm_host *msm_host = pltfm_host->priv;
3364 int ret = 0;
3365
3366 if (gpio_is_valid(msm_host->pdata->status_gpio) &&
3367 (msm_host->mmc->slot.cd_irq >= 0))
3368 enable_irq(msm_host->mmc->slot.cd_irq);
3369
3370 if (pm_runtime_suspended(dev)) {
3371 pr_debug("%s: %s: runtime suspended, defer system resume\n",
3372 mmc_hostname(host->mmc), __func__);
3373 goto out;
3374 }
3375
3376 return sdhci_msm_runtime_resume(dev);
3377out:
3378 return ret;
3379}
3380
3381static const struct dev_pm_ops sdhci_msm_pmops = {
3382 SET_SYSTEM_SLEEP_PM_OPS(sdhci_msm_suspend, sdhci_msm_resume)
3383 SET_RUNTIME_PM_OPS(sdhci_msm_runtime_suspend, sdhci_msm_runtime_resume,
3384 NULL)
3385};
3386
3387#define SDHCI_MSM_PMOPS (&sdhci_msm_pmops)
3388
3389#else
3390#define SDHCI_MSM_PMOPS NULL
3391#endif
Asutosh Das0ef24812012-12-18 16:14:02 +05303392static const struct of_device_id sdhci_msm_dt_match[] = {
3393 {.compatible = "qcom,sdhci-msm"},
3394};
3395MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match);
3396
3397static struct platform_driver sdhci_msm_driver = {
3398 .probe = sdhci_msm_probe,
3399 .remove = sdhci_msm_remove,
3400 .driver = {
3401 .name = "sdhci_msm",
3402 .owner = THIS_MODULE,
3403 .of_match_table = sdhci_msm_dt_match,
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003404 .pm = SDHCI_MSM_PMOPS,
Asutosh Das0ef24812012-12-18 16:14:02 +05303405 },
3406};
3407
3408module_platform_driver(sdhci_msm_driver);
3409
3410MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Secure Digital Host Controller Interface driver");
3411MODULE_LICENSE("GPL v2");