blob: 429f4c8557888c16b8d660796131eb50bdaabdc3 [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
108#define CORE_CLK_PWRSAVE (1 << 1)
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700109#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)
Asutosh Dase5e9ca62013-07-30 19:08:36 +0530112#define CORE_HC_AUTO_CMD21_EN (1 << 6)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700113#define CORE_IO_PAD_PWR_SWITCH (1 << 16)
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700114#define CORE_HC_SELECT_IN_EN (1 << 18)
115#define CORE_HC_SELECT_IN_HS400 (6 << 19)
116#define CORE_HC_SELECT_IN_MASK (7 << 19)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700117
Venkat Gopalakrishnan0a92d532014-12-16 17:31:00 -0800118#define CORE_VENDOR_SPEC_ADMA_ERR_ADDR0 0x114
119#define CORE_VENDOR_SPEC_ADMA_ERR_ADDR1 0x118
120
Krishna Konda7feab352013-09-17 23:55:40 -0700121#define CORE_VENDOR_SPEC_CAPABILITIES0 0x11C
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +0530122#define CORE_8_BIT_SUPPORT (1 << 18)
123#define CORE_3_3V_SUPPORT (1 << 24)
124#define CORE_3_0V_SUPPORT (1 << 25)
125#define CORE_1_8V_SUPPORT (1 << 26)
Gilad Broner2a10ca02014-10-02 17:20:35 +0300126#define CORE_SYS_BUS_SUPPORT_64_BIT BIT(28)
Krishna Konda7feab352013-09-17 23:55:40 -0700127
Venkat Gopalakrishnan0a92d532014-12-16 17:31:00 -0800128#define CORE_SDCC_DEBUG_REG 0x124
Sahitya Tummala67717bc2013-08-02 09:21:37 +0530129
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700130#define CORE_CSR_CDC_CTLR_CFG0 0x130
131#define CORE_SW_TRIG_FULL_CALIB (1 << 16)
132#define CORE_HW_AUTOCAL_ENA (1 << 17)
133
134#define CORE_CSR_CDC_CTLR_CFG1 0x134
135#define CORE_CSR_CDC_CAL_TIMER_CFG0 0x138
136#define CORE_TIMER_ENA (1 << 16)
137
138#define CORE_CSR_CDC_CAL_TIMER_CFG1 0x13C
139#define CORE_CSR_CDC_REFCOUNT_CFG 0x140
140#define CORE_CSR_CDC_COARSE_CAL_CFG 0x144
141#define CORE_CDC_OFFSET_CFG 0x14C
142#define CORE_CSR_CDC_DELAY_CFG 0x150
143#define CORE_CDC_SLAVE_DDA_CFG 0x160
144#define CORE_CSR_CDC_STATUS0 0x164
145#define CORE_CALIBRATION_DONE (1 << 0)
146
147#define CORE_CDC_ERROR_CODE_MASK 0x7000000
148
149#define CORE_CSR_CDC_GEN_CFG 0x178
150#define CORE_CDC_SWITCH_BYPASS_OFF (1 << 0)
151#define CORE_CDC_SWITCH_RC_EN (1 << 1)
152
153#define CORE_DDR_200_CFG 0x184
154#define CORE_CDC_T4_DLY_SEL (1 << 0)
155#define CORE_START_CDC_TRAFFIC (1 << 6)
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700156#define CORE_VENDOR_SPEC3 0x1B0
157#define CORE_PWRSAVE_DLL (1 << 3)
158
159#define CORE_DLL_CONFIG_2 0x1B4
160#define CORE_DDR_CAL_EN (1 << 0)
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800161#define CORE_FLL_CYCLE_CNT (1 << 18)
162#define CORE_DLL_CLOCK_DISABLE (1 << 21)
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700163
164#define CORE_DDR_CONFIG 0x1B8
165#define DDR_CONFIG_POR_VAL 0x80040853
166
Venkat Gopalakrishnan450745e2014-07-24 20:39:34 -0700167/* 512 descriptors */
168#define SDHCI_MSM_MAX_SEGMENTS (1 << 9)
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +0530169#define SDHCI_MSM_MMC_CLK_GATE_DELAY 200 /* msecs */
Asutosh Das648f9d12013-01-10 21:11:04 +0530170
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700171#define CORE_FREQ_100MHZ (100 * 1000 * 1000)
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800172#define TCXO_FREQ 19200000
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700173
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700174#define INVALID_TUNING_PHASE -1
175
Krishna Konda96e6b112013-10-28 15:25:03 -0700176#define NUM_TUNING_PHASES 16
177#define MAX_DRV_TYPES_SUPPORTED_HS200 3
Konstantin Dorfman98377d32015-02-25 10:09:41 +0200178#define MSM_AUTOSUSPEND_DELAY_MS 100
Krishna Konda96e6b112013-10-28 15:25:03 -0700179
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700180static const u32 tuning_block_64[] = {
181 0x00FF0FFF, 0xCCC3CCFF, 0xFFCC3CC3, 0xEFFEFFFE,
182 0xDDFFDFFF, 0xFBFFFBFF, 0xFF7FFFBF, 0xEFBDF777,
183 0xF0FFF0FF, 0x3CCCFC0F, 0xCFCC33CC, 0xEEFFEFFF,
184 0xFDFFFDFF, 0xFFBFFFDF, 0xFFF7FFBB, 0xDE7B7FF7
185};
186
187static const u32 tuning_block_128[] = {
188 0xFF00FFFF, 0x0000FFFF, 0xCCCCFFFF, 0xCCCC33CC,
189 0xCC3333CC, 0xFFFFCCCC, 0xFFFFEEFF, 0xFFEEEEFF,
190 0xFFDDFFFF, 0xDDDDFFFF, 0xBBFFFFFF, 0xBBFFFFFF,
191 0xFFFFFFBB, 0xFFFFFF77, 0x77FF7777, 0xFFEEDDBB,
192 0x00FFFFFF, 0x00FFFFFF, 0xCCFFFF00, 0xCC33CCCC,
193 0x3333CCCC, 0xFFCCCCCC, 0xFFEEFFFF, 0xEEEEFFFF,
194 0xDDFFFFFF, 0xDDFFFFFF, 0xFFFFFFDD, 0xFFFFFFBB,
195 0xFFFFBBBB, 0xFFFF77FF, 0xFF7777FF, 0xEEDDBB77
196};
Asutosh Das0ef24812012-12-18 16:14:02 +0530197
Venkat Gopalakrishnan270580a2013-03-11 12:17:57 -0700198static int disable_slots;
199/* root can write, others read */
200module_param(disable_slots, int, S_IRUGO|S_IWUSR);
201
Asutosh Das0ef24812012-12-18 16:14:02 +0530202/* This structure keeps information per regulator */
203struct sdhci_msm_reg_data {
204 /* voltage regulator handle */
205 struct regulator *reg;
206 /* regulator name */
207 const char *name;
208 /* voltage level to be set */
209 u32 low_vol_level;
210 u32 high_vol_level;
211 /* Load values for low power and high power mode */
212 u32 lpm_uA;
213 u32 hpm_uA;
214
215 /* is this regulator enabled? */
216 bool is_enabled;
217 /* is this regulator needs to be always on? */
218 bool is_always_on;
219 /* is low power mode setting required for this regulator? */
220 bool lpm_sup;
221 bool set_voltage_sup;
222};
223
Asutosh Das598a4d42014-02-05 16:38:23 +0530224#define MSM_MMC_DEFAULT_CPU_DMA_LATENCY 200 /* usecs */
Asutosh Das0ef24812012-12-18 16:14:02 +0530225/*
226 * This structure keeps information for all the
227 * regulators required for a SDCC slot.
228 */
229struct sdhci_msm_slot_reg_data {
230 /* keeps VDD/VCC regulator info */
231 struct sdhci_msm_reg_data *vdd_data;
232 /* keeps VDD IO regulator info */
233 struct sdhci_msm_reg_data *vdd_io_data;
234};
235
236struct sdhci_msm_gpio {
237 u32 no;
238 const char *name;
239 bool is_enabled;
240};
241
242struct sdhci_msm_gpio_data {
243 struct sdhci_msm_gpio *gpio;
244 u8 size;
245};
246
247struct sdhci_msm_pin_data {
248 /*
249 * = 1 if controller pins are using gpios
250 * = 0 if controller has dedicated MSM pads
251 */
Sahitya Tummala1cd7e072014-02-14 13:19:01 +0530252 u8 is_gpio;
Asutosh Das0ef24812012-12-18 16:14:02 +0530253 struct sdhci_msm_gpio_data *gpio_data;
254};
255
Pratibhasagar V9acf2642013-11-21 21:07:21 +0530256struct sdhci_pinctrl_data {
257 struct pinctrl *pctrl;
258 struct pinctrl_state *pins_active;
259 struct pinctrl_state *pins_sleep;
260};
261
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530262struct sdhci_msm_bus_voting_data {
263 struct msm_bus_scale_pdata *bus_pdata;
264 unsigned int *bw_vecs;
265 unsigned int bw_vecs_size;
266};
267
Asutosh Das0ef24812012-12-18 16:14:02 +0530268struct sdhci_msm_pltfm_data {
269 /* Supported UHS-I Modes */
270 u32 caps;
271
272 /* More capabilities */
273 u32 caps2;
274
275 unsigned long mmc_bus_width;
Asutosh Das0ef24812012-12-18 16:14:02 +0530276 struct sdhci_msm_slot_reg_data *vreg_data;
277 bool nonremovable;
Guoping Yuf7c91332014-08-20 16:56:18 +0800278 bool nonhotplug;
Pratibhasagar V9acf2642013-11-21 21:07:21 +0530279 bool pin_cfg_sts;
Asutosh Das0ef24812012-12-18 16:14:02 +0530280 struct sdhci_msm_pin_data *pin_data;
Pratibhasagar V9acf2642013-11-21 21:07:21 +0530281 struct sdhci_pinctrl_data *pctrl_data;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +0530282 u32 cpu_dma_latency_us;
Sahitya Tummala581df132013-03-12 14:57:46 +0530283 int status_gpio; /* card detection GPIO that is configured as IRQ */
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530284 struct sdhci_msm_bus_voting_data *voting_data;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530285 u32 *sup_clk_table;
286 unsigned char sup_clk_cnt;
Maya Erez994cf2f2014-10-21 20:22:04 +0300287 enum pm_qos_req_type cpu_affinity_type;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530288};
289
290struct sdhci_msm_bus_vote {
291 uint32_t client_handle;
292 uint32_t curr_vote;
293 int min_bw_vote;
294 int max_bw_vote;
295 bool is_max_bw_needed;
296 struct delayed_work vote_work;
297 struct device_attribute max_bus_bw;
Asutosh Das0ef24812012-12-18 16:14:02 +0530298};
299
300struct sdhci_msm_host {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530301 struct platform_device *pdev;
Asutosh Das0ef24812012-12-18 16:14:02 +0530302 void __iomem *core_mem; /* MSM SDCC mapped address */
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +0200303 int pwr_irq; /* power irq */
Asutosh Das0ef24812012-12-18 16:14:02 +0530304 struct clk *clk; /* main SD/MMC bus clock */
305 struct clk *pclk; /* SDHC peripheral bus clock */
306 struct clk *bus_clk; /* SDHC bus voter clock */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700307 struct clk *ff_clk; /* CDC calibration fixed feedback clock */
308 struct clk *sleep_clk; /* CDC calibration sleep clock */
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +0530309 atomic_t clks_on; /* Set if clocks are enabled */
Asutosh Das0ef24812012-12-18 16:14:02 +0530310 struct sdhci_msm_pltfm_data *pdata;
311 struct mmc_host *mmc;
312 struct sdhci_pltfm_data sdhci_msm_pdata;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +0530313 u32 curr_pwr_state;
314 u32 curr_io_level;
315 struct completion pwr_irq_completion;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530316 struct sdhci_msm_bus_vote msm_bus_vote;
Sahitya Tummala5c55b932013-06-20 14:00:18 +0530317 struct device_attribute polling;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530318 u32 clk_rate; /* Keeps track of current clock rate that is set */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700319 bool tuning_done;
320 bool calibration_done;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700321 u8 saved_tuning_phase;
Asutosh Dase5e9ca62013-07-30 19:08:36 +0530322 bool en_auto_cmd21;
323 struct device_attribute auto_cmd21_attr;
Asutosh Das9d7ee2f2013-11-08 12:33:47 +0530324 atomic_t controller_clock;
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700325 bool use_cdclp533;
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800326 bool use_updated_dll_reset;
Asutosh Das0ef24812012-12-18 16:14:02 +0530327};
328
329enum vdd_io_level {
330 /* set vdd_io_data->low_vol_level */
331 VDD_IO_LOW,
332 /* set vdd_io_data->high_vol_level */
333 VDD_IO_HIGH,
334 /*
335 * set whatever there in voltage_level (third argument) of
336 * sdhci_msm_set_vdd_io_vol() function.
337 */
338 VDD_IO_SET_LEVEL,
339};
340
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700341/* MSM platform specific tuning */
342static inline int msm_dll_poll_ck_out_en(struct sdhci_host *host,
343 u8 poll)
344{
345 int rc = 0;
346 u32 wait_cnt = 50;
347 u8 ck_out_en = 0;
348 struct mmc_host *mmc = host->mmc;
349
350 /* poll for CK_OUT_EN bit. max. poll time = 50us */
351 ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) &
352 CORE_CK_OUT_EN);
353
354 while (ck_out_en != poll) {
355 if (--wait_cnt == 0) {
356 pr_err("%s: %s: CK_OUT_EN bit is not %d\n",
357 mmc_hostname(mmc), __func__, poll);
358 rc = -ETIMEDOUT;
359 goto out;
360 }
361 udelay(1);
362
363 ck_out_en = !!(readl_relaxed(host->ioaddr +
364 CORE_DLL_CONFIG) & CORE_CK_OUT_EN);
365 }
366out:
367 return rc;
368}
369
Asutosh Dase5e9ca62013-07-30 19:08:36 +0530370/*
371 * Enable CDR to track changes of DAT lines and adjust sampling
372 * point according to voltage/temperature variations
373 */
374static int msm_enable_cdr_cm_sdc4_dll(struct sdhci_host *host)
375{
376 int rc = 0;
377 u32 config;
378
379 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
380 config |= CORE_CDR_EN;
381 config &= ~(CORE_CDR_EXT_EN | CORE_CK_OUT_EN);
382 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
383
384 rc = msm_dll_poll_ck_out_en(host, 0);
385 if (rc)
386 goto err;
387
388 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) |
389 CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
390
391 rc = msm_dll_poll_ck_out_en(host, 1);
392 if (rc)
393 goto err;
394 goto out;
395err:
396 pr_err("%s: %s: failed\n", mmc_hostname(host->mmc), __func__);
397out:
398 return rc;
399}
400
401static ssize_t store_auto_cmd21(struct device *dev, struct device_attribute
402 *attr, const char *buf, size_t count)
403{
404 struct sdhci_host *host = dev_get_drvdata(dev);
405 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
406 struct sdhci_msm_host *msm_host = pltfm_host->priv;
407 u32 tmp;
408 unsigned long flags;
409
410 if (!kstrtou32(buf, 0, &tmp)) {
411 spin_lock_irqsave(&host->lock, flags);
412 msm_host->en_auto_cmd21 = !!tmp;
413 spin_unlock_irqrestore(&host->lock, flags);
414 }
415 return count;
416}
417
418static ssize_t show_auto_cmd21(struct device *dev,
419 struct device_attribute *attr, char *buf)
420{
421 struct sdhci_host *host = dev_get_drvdata(dev);
422 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
423 struct sdhci_msm_host *msm_host = pltfm_host->priv;
424
425 return snprintf(buf, PAGE_SIZE, "%d\n", msm_host->en_auto_cmd21);
426}
427
428/* MSM auto-tuning handler */
429static int sdhci_msm_config_auto_tuning_cmd(struct sdhci_host *host,
430 bool enable,
431 u32 type)
432{
433 int rc = 0;
434 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
435 struct sdhci_msm_host *msm_host = pltfm_host->priv;
436 u32 val = 0;
437
438 if (!msm_host->en_auto_cmd21)
439 return 0;
440
441 if (type == MMC_SEND_TUNING_BLOCK_HS200)
442 val = CORE_HC_AUTO_CMD21_EN;
443 else
444 return 0;
445
446 if (enable) {
447 rc = msm_enable_cdr_cm_sdc4_dll(host);
448 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
449 val, host->ioaddr + CORE_VENDOR_SPEC);
450 } else {
451 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
452 ~val, host->ioaddr + CORE_VENDOR_SPEC);
453 }
454 return rc;
455}
456
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700457static int msm_config_cm_dll_phase(struct sdhci_host *host, u8 phase)
458{
459 int rc = 0;
460 u8 grey_coded_phase_table[] = {0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4,
461 0xC, 0xD, 0xF, 0xE, 0xA, 0xB, 0x9,
462 0x8};
463 unsigned long flags;
464 u32 config;
465 struct mmc_host *mmc = host->mmc;
466
467 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
468 spin_lock_irqsave(&host->lock, flags);
469
470 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
471 config &= ~(CORE_CDR_EN | CORE_CK_OUT_EN);
472 config |= (CORE_CDR_EXT_EN | CORE_DLL_EN);
473 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
474
475 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '0' */
476 rc = msm_dll_poll_ck_out_en(host, 0);
477 if (rc)
478 goto err_out;
479
480 /*
481 * Write the selected DLL clock output phase (0 ... 15)
482 * to CDR_SELEXT bit field of DLL_CONFIG register.
483 */
484 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
485 & ~(0xF << 20))
486 | (grey_coded_phase_table[phase] << 20)),
487 host->ioaddr + CORE_DLL_CONFIG);
488
489 /* Set CK_OUT_EN bit of DLL_CONFIG register to 1. */
490 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
491 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
492
493 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '1' */
494 rc = msm_dll_poll_ck_out_en(host, 1);
495 if (rc)
496 goto err_out;
497
498 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
499 config |= CORE_CDR_EN;
500 config &= ~CORE_CDR_EXT_EN;
501 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
502 goto out;
503
504err_out:
505 pr_err("%s: %s: Failed to set DLL phase: %d\n",
506 mmc_hostname(mmc), __func__, phase);
507out:
508 spin_unlock_irqrestore(&host->lock, flags);
509 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
510 return rc;
511}
512
513/*
514 * Find out the greatest range of consecuitive selected
515 * DLL clock output phases that can be used as sampling
516 * setting for SD3.0 UHS-I card read operation (in SDR104
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700517 * timing mode) or for eMMC4.5 card read operation (in
518 * HS400/HS200 timing mode).
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700519 * Select the 3/4 of the range and configure the DLL with the
520 * selected DLL clock output phase.
521 */
522
523static int msm_find_most_appropriate_phase(struct sdhci_host *host,
524 u8 *phase_table, u8 total_phases)
525{
526 int ret;
527 u8 ranges[MAX_PHASES][MAX_PHASES] = { {0}, {0} };
528 u8 phases_per_row[MAX_PHASES] = {0};
529 int row_index = 0, col_index = 0, selected_row_index = 0, curr_max = 0;
530 int i, cnt, phase_0_raw_index = 0, phase_15_raw_index = 0;
531 bool phase_0_found = false, phase_15_found = false;
532 struct mmc_host *mmc = host->mmc;
533
534 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
535 if (!total_phases || (total_phases > MAX_PHASES)) {
536 pr_err("%s: %s: invalid argument: total_phases=%d\n",
537 mmc_hostname(mmc), __func__, total_phases);
538 return -EINVAL;
539 }
540
541 for (cnt = 0; cnt < total_phases; cnt++) {
542 ranges[row_index][col_index] = phase_table[cnt];
543 phases_per_row[row_index] += 1;
544 col_index++;
545
546 if ((cnt + 1) == total_phases) {
547 continue;
548 /* check if next phase in phase_table is consecutive or not */
549 } else if ((phase_table[cnt] + 1) != phase_table[cnt + 1]) {
550 row_index++;
551 col_index = 0;
552 }
553 }
554
555 if (row_index >= MAX_PHASES)
556 return -EINVAL;
557
558 /* Check if phase-0 is present in first valid window? */
559 if (!ranges[0][0]) {
560 phase_0_found = true;
561 phase_0_raw_index = 0;
562 /* Check if cycle exist between 2 valid windows */
563 for (cnt = 1; cnt <= row_index; cnt++) {
564 if (phases_per_row[cnt]) {
565 for (i = 0; i < phases_per_row[cnt]; i++) {
566 if (ranges[cnt][i] == 15) {
567 phase_15_found = true;
568 phase_15_raw_index = cnt;
569 break;
570 }
571 }
572 }
573 }
574 }
575
576 /* If 2 valid windows form cycle then merge them as single window */
577 if (phase_0_found && phase_15_found) {
578 /* number of phases in raw where phase 0 is present */
579 u8 phases_0 = phases_per_row[phase_0_raw_index];
580 /* number of phases in raw where phase 15 is present */
581 u8 phases_15 = phases_per_row[phase_15_raw_index];
582
583 if (phases_0 + phases_15 >= MAX_PHASES)
584 /*
585 * If there are more than 1 phase windows then total
586 * number of phases in both the windows should not be
587 * more than or equal to MAX_PHASES.
588 */
589 return -EINVAL;
590
591 /* Merge 2 cyclic windows */
592 i = phases_15;
593 for (cnt = 0; cnt < phases_0; cnt++) {
594 ranges[phase_15_raw_index][i] =
595 ranges[phase_0_raw_index][cnt];
596 if (++i >= MAX_PHASES)
597 break;
598 }
599
600 phases_per_row[phase_0_raw_index] = 0;
601 phases_per_row[phase_15_raw_index] = phases_15 + phases_0;
602 }
603
604 for (cnt = 0; cnt <= row_index; cnt++) {
605 if (phases_per_row[cnt] > curr_max) {
606 curr_max = phases_per_row[cnt];
607 selected_row_index = cnt;
608 }
609 }
610
611 i = ((curr_max * 3) / 4);
612 if (i)
613 i--;
614
615 ret = (int)ranges[selected_row_index][i];
616
617 if (ret >= MAX_PHASES) {
618 ret = -EINVAL;
619 pr_err("%s: %s: invalid phase selected=%d\n",
620 mmc_hostname(mmc), __func__, ret);
621 }
622
623 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
624 return ret;
625}
626
627static inline void msm_cm_dll_set_freq(struct sdhci_host *host)
628{
629 u32 mclk_freq = 0;
630
631 /* Program the MCLK value to MCLK_FREQ bit field */
632 if (host->clock <= 112000000)
633 mclk_freq = 0;
634 else if (host->clock <= 125000000)
635 mclk_freq = 1;
636 else if (host->clock <= 137000000)
637 mclk_freq = 2;
638 else if (host->clock <= 150000000)
639 mclk_freq = 3;
640 else if (host->clock <= 162000000)
641 mclk_freq = 4;
642 else if (host->clock <= 175000000)
643 mclk_freq = 5;
644 else if (host->clock <= 187000000)
645 mclk_freq = 6;
646 else if (host->clock <= 200000000)
647 mclk_freq = 7;
648
649 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
650 & ~(7 << 24)) | (mclk_freq << 24)),
651 host->ioaddr + CORE_DLL_CONFIG);
652}
653
654/* Initialize the DLL (Programmable Delay Line ) */
655static int msm_init_cm_dll(struct sdhci_host *host)
656{
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800657 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
658 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700659 struct mmc_host *mmc = host->mmc;
660 int rc = 0;
661 unsigned long flags;
662 u32 wait_cnt;
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530663 bool prev_pwrsave, curr_pwrsave;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700664
665 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
666 spin_lock_irqsave(&host->lock, flags);
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530667 prev_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
668 CORE_CLK_PWRSAVE);
669 curr_pwrsave = prev_pwrsave;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700670 /*
671 * Make sure that clock is always enabled when DLL
672 * tuning is in progress. Keeping PWRSAVE ON may
673 * turn off the clock. So let's disable the PWRSAVE
674 * here and re-enable it once tuning is completed.
675 */
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530676 if (prev_pwrsave) {
677 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
678 & ~CORE_CLK_PWRSAVE),
679 host->ioaddr + CORE_VENDOR_SPEC);
680 curr_pwrsave = false;
681 }
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700682
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800683 if (msm_host->use_updated_dll_reset) {
684 /* Disable the DLL clock */
685 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
686 & ~CORE_CK_OUT_EN),
687 host->ioaddr + CORE_DLL_CONFIG);
688
689 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
690 | CORE_DLL_CLOCK_DISABLE),
691 host->ioaddr + CORE_DLL_CONFIG_2);
692 }
693
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700694 /* Write 1 to DLL_RST bit of DLL_CONFIG register */
695 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
696 | CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
697
698 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
699 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
700 | CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
701 msm_cm_dll_set_freq(host);
702
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800703 if (msm_host->use_updated_dll_reset) {
704 u32 mclk_freq = 0;
705
706 if ((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
707 & CORE_FLL_CYCLE_CNT))
708 mclk_freq = (u32) ((host->clock / TCXO_FREQ) * 8);
709 else
710 mclk_freq = (u32) ((host->clock / TCXO_FREQ) * 4);
711
712 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
713 & ~(0xFF << 10)) | (mclk_freq << 10)),
714 host->ioaddr + CORE_DLL_CONFIG_2);
715 /* wait for 5us before enabling DLL clock */
716 udelay(5);
717 }
718
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700719 /* Write 0 to DLL_RST bit of DLL_CONFIG register */
720 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
721 & ~CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
722
723 /* Write 0 to DLL_PDN bit of DLL_CONFIG register */
724 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
725 & ~CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
726
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800727 if (msm_host->use_updated_dll_reset) {
728 msm_cm_dll_set_freq(host);
729 /* Enable the DLL clock */
730 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
731 & ~CORE_DLL_CLOCK_DISABLE),
732 host->ioaddr + CORE_DLL_CONFIG_2);
733 }
734
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700735 /* Set DLL_EN bit to 1. */
736 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
737 | CORE_DLL_EN), host->ioaddr + CORE_DLL_CONFIG);
738
739 /* Set CK_OUT_EN bit to 1. */
740 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
741 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
742
743 wait_cnt = 50;
744 /* Wait until DLL_LOCK bit of DLL_STATUS register becomes '1' */
745 while (!(readl_relaxed(host->ioaddr + CORE_DLL_STATUS) &
746 CORE_DLL_LOCK)) {
747 /* max. wait for 50us sec for LOCK bit to be set */
748 if (--wait_cnt == 0) {
749 pr_err("%s: %s: DLL failed to LOCK\n",
750 mmc_hostname(mmc), __func__);
751 rc = -ETIMEDOUT;
752 goto out;
753 }
754 /* wait for 1us before polling again */
755 udelay(1);
756 }
757
758out:
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530759 /* Restore the correct PWRSAVE state */
760 if (prev_pwrsave ^ curr_pwrsave) {
761 u32 reg = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC);
762
763 if (prev_pwrsave)
764 reg |= CORE_CLK_PWRSAVE;
765 else
766 reg &= ~CORE_CLK_PWRSAVE;
767
768 writel_relaxed(reg, host->ioaddr + CORE_VENDOR_SPEC);
769 }
770
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700771 spin_unlock_irqrestore(&host->lock, flags);
772 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
773 return rc;
774}
775
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700776static int sdhci_msm_cdclp533_calibration(struct sdhci_host *host)
777{
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700778 u32 calib_done;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700779 int ret = 0;
780 int cdc_err = 0;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700781
782 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
783
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700784 /* Write 0 to CDC_T4_DLY_SEL field in VENDOR_SPEC_DDR200_CFG */
785 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
786 & ~CORE_CDC_T4_DLY_SEL),
787 host->ioaddr + CORE_DDR_200_CFG);
788
789 /* Write 0 to CDC_SWITCH_BYPASS_OFF field in CORE_CSR_CDC_GEN_CFG */
790 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG)
791 & ~CORE_CDC_SWITCH_BYPASS_OFF),
792 host->ioaddr + CORE_CSR_CDC_GEN_CFG);
793
794 /* Write 1 to CDC_SWITCH_RC_EN field in CORE_CSR_CDC_GEN_CFG */
795 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG)
796 | CORE_CDC_SWITCH_RC_EN),
797 host->ioaddr + CORE_CSR_CDC_GEN_CFG);
798
799 /* Write 0 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
800 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
801 & ~CORE_START_CDC_TRAFFIC),
802 host->ioaddr + CORE_DDR_200_CFG);
803
804 /*
805 * Perform CDC Register Initialization Sequence
806 *
807 * CORE_CSR_CDC_CTLR_CFG0 0x11800EC
808 * CORE_CSR_CDC_CTLR_CFG1 0x3011111
809 * CORE_CSR_CDC_CAL_TIMER_CFG0 0x1201000
810 * CORE_CSR_CDC_CAL_TIMER_CFG1 0x4
811 * CORE_CSR_CDC_REFCOUNT_CFG 0xCB732020
812 * CORE_CSR_CDC_COARSE_CAL_CFG 0xB19
813 * CORE_CSR_CDC_DELAY_CFG 0x3AC
814 * CORE_CDC_OFFSET_CFG 0x0
815 * CORE_CDC_SLAVE_DDA_CFG 0x16334
816 */
817
818 writel_relaxed(0x11800EC, host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
819 writel_relaxed(0x3011111, host->ioaddr + CORE_CSR_CDC_CTLR_CFG1);
820 writel_relaxed(0x1201000, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
821 writel_relaxed(0x4, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG1);
822 writel_relaxed(0xCB732020, host->ioaddr + CORE_CSR_CDC_REFCOUNT_CFG);
823 writel_relaxed(0xB19, host->ioaddr + CORE_CSR_CDC_COARSE_CAL_CFG);
Subhash Jadavanibe406d92014-06-17 16:47:48 -0700824 writel_relaxed(0x4E2, host->ioaddr + CORE_CSR_CDC_DELAY_CFG);
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700825 writel_relaxed(0x0, host->ioaddr + CORE_CDC_OFFSET_CFG);
826 writel_relaxed(0x16334, host->ioaddr + CORE_CDC_SLAVE_DDA_CFG);
827
828 /* CDC HW Calibration */
829
830 /* Write 1 to SW_TRIG_FULL_CALIB field in CORE_CSR_CDC_CTLR_CFG0 */
831 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
832 | CORE_SW_TRIG_FULL_CALIB),
833 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
834
835 /* Write 0 to SW_TRIG_FULL_CALIB field in CORE_CSR_CDC_CTLR_CFG0 */
836 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
837 & ~CORE_SW_TRIG_FULL_CALIB),
838 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
839
840 /* Write 1 to HW_AUTOCAL_ENA field in CORE_CSR_CDC_CTLR_CFG0 */
841 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
842 | CORE_HW_AUTOCAL_ENA),
843 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
844
845 /* Write 1 to TIMER_ENA field in CORE_CSR_CDC_CAL_TIMER_CFG0 */
846 writel_relaxed((readl_relaxed(host->ioaddr +
847 CORE_CSR_CDC_CAL_TIMER_CFG0) | CORE_TIMER_ENA),
848 host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
849
850 mb();
851
852 /* Poll on CALIBRATION_DONE field in CORE_CSR_CDC_STATUS0 to be 1 */
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700853 ret = readl_poll_timeout(host->ioaddr + CORE_CSR_CDC_STATUS0,
854 calib_done, (calib_done & CORE_CALIBRATION_DONE), 1, 50);
855
856 if (ret == -ETIMEDOUT) {
857 pr_err("%s: %s: CDC Calibration was not completed\n",
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700858 mmc_hostname(host->mmc), __func__);
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700859 goto out;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700860 }
861
862 /* Verify CDC_ERROR_CODE field in CORE_CSR_CDC_STATUS0 is 0 */
863 cdc_err = readl_relaxed(host->ioaddr + CORE_CSR_CDC_STATUS0)
864 & CORE_CDC_ERROR_CODE_MASK;
865 if (cdc_err) {
866 pr_err("%s: %s: CDC Error Code %d\n",
867 mmc_hostname(host->mmc), __func__, cdc_err);
868 ret = -EINVAL;
869 goto out;
870 }
871
872 /* Write 1 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
873 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
874 | CORE_START_CDC_TRAFFIC),
875 host->ioaddr + CORE_DDR_200_CFG);
876out:
877 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
878 __func__, ret);
879 return ret;
880}
881
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700882static int sdhci_msm_cm_dll_sdc4_calibration(struct sdhci_host *host)
883{
884 u32 dll_status;
885 int ret = 0;
886
887 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
888
889 /*
890 * Currently the CORE_DDR_CONFIG register defaults to desired
891 * configuration on reset. Currently reprogramming the power on
892 * reset (POR) value in case it might have been modified by
893 * bootloaders. In the future, if this changes, then the desired
894 * values will need to be programmed appropriately.
895 */
896 writel_relaxed(DDR_CONFIG_POR_VAL, host->ioaddr + CORE_DDR_CONFIG);
897
898 /* Write 1 to DDR_CAL_EN field in CORE_DLL_CONFIG_2 */
899 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
900 | CORE_DDR_CAL_EN),
901 host->ioaddr + CORE_DLL_CONFIG_2);
902
903 /* Poll on DDR_DLL_LOCK bit in CORE_DLL_STATUS to be set */
904 ret = readl_poll_timeout(host->ioaddr + CORE_DLL_STATUS,
905 dll_status, (dll_status & CORE_DDR_DLL_LOCK), 10, 1000);
906
907 if (ret == -ETIMEDOUT) {
908 pr_err("%s: %s: CM_DLL_SDC4 Calibration was not completed\n",
909 mmc_hostname(host->mmc), __func__);
910 goto out;
911 }
912
913 /* set CORE_PWRSAVE_DLL bit in CORE_VENDOR_SPEC3 */
914 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC3)
915 | CORE_PWRSAVE_DLL),
916 host->ioaddr + CORE_VENDOR_SPEC3);
917 mb();
918out:
919 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
920 __func__, ret);
921 return ret;
922}
923
924static int sdhci_msm_hs400_dll_calibration(struct sdhci_host *host)
925{
926 int ret = 0;
927 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
928 struct sdhci_msm_host *msm_host = pltfm_host->priv;
929
930 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
931
932 /*
933 * Retuning in HS400 (DDR mode) will fail, just reset the
934 * tuning block and restore the saved tuning phase.
935 */
936 ret = msm_init_cm_dll(host);
937 if (ret)
938 goto out;
939
940 /* Set the selected phase in delay line hw block */
941 ret = msm_config_cm_dll_phase(host, msm_host->saved_tuning_phase);
942 if (ret)
943 goto out;
944
Krishna Konda0e8efba2014-06-23 14:50:38 -0700945 /* Write 1 to CMD_DAT_TRACK_SEL field in DLL_CONFIG */
946 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
947 | CORE_CMD_DAT_TRACK_SEL),
948 host->ioaddr + CORE_DLL_CONFIG);
949
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700950 if (msm_host->use_cdclp533)
951 /* Calibrate CDCLP533 DLL HW */
952 ret = sdhci_msm_cdclp533_calibration(host);
953 else
954 /* Calibrate CM_DLL_SDC4 HW */
955 ret = sdhci_msm_cm_dll_sdc4_calibration(host);
956out:
957 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
958 __func__, ret);
959 return ret;
960}
961
Krishna Konda96e6b112013-10-28 15:25:03 -0700962static void sdhci_msm_set_mmc_drv_type(struct sdhci_host *host, u32 opcode,
963 u8 drv_type)
964{
965 struct mmc_command cmd = {0};
966 struct mmc_request mrq = {NULL};
967 struct mmc_host *mmc = host->mmc;
968 u8 val = ((drv_type << 4) | 2);
969
970 cmd.opcode = MMC_SWITCH;
971 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
972 (EXT_CSD_HS_TIMING << 16) |
973 (val << 8) |
974 EXT_CSD_CMD_SET_NORMAL;
975 cmd.flags = MMC_CMD_AC | MMC_RSP_R1B;
976 /* 1 sec */
977 cmd.busy_timeout = 1000 * 1000;
978
979 memset(cmd.resp, 0, sizeof(cmd.resp));
980 cmd.retries = 3;
981
982 mrq.cmd = &cmd;
983 cmd.data = NULL;
984
985 mmc_wait_for_req(mmc, &mrq);
986 pr_debug("%s: %s: set card drive type to %d\n",
987 mmc_hostname(mmc), __func__,
988 drv_type);
989}
990
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700991int sdhci_msm_execute_tuning(struct sdhci_host *host, u32 opcode)
992{
993 unsigned long flags;
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530994 int tuning_seq_cnt = 3;
Krishna Konda96e6b112013-10-28 15:25:03 -0700995 u8 phase, *data_buf, tuned_phases[NUM_TUNING_PHASES], tuned_phase_cnt;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700996 const u32 *tuning_block_pattern = tuning_block_64;
997 int size = sizeof(tuning_block_64); /* Tuning pattern size in bytes */
998 int rc;
999 struct mmc_host *mmc = host->mmc;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301000 struct mmc_ios ios = host->mmc->ios;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07001001 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1002 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Krishna Konda96e6b112013-10-28 15:25:03 -07001003 u8 drv_type = 0;
1004 bool drv_type_changed = false;
1005 struct mmc_card *card = host->mmc->card;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301006
1007 /*
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07001008 * Tuning is required for SDR104, HS200 and HS400 cards and
1009 * if clock frequency is greater than 100MHz in these modes.
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301010 */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07001011 if (host->clock <= CORE_FREQ_100MHZ ||
1012 !((ios.timing == MMC_TIMING_MMC_HS400) ||
1013 (ios.timing == MMC_TIMING_MMC_HS200) ||
1014 (ios.timing == MMC_TIMING_UHS_SDR104)))
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301015 return 0;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001016
1017 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07001018
Krishna Konda2faa7bb2014-06-04 01:25:16 -07001019 /* CDC/SDC4 DLL HW calibration is only required for HS400 mode*/
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07001020 if (msm_host->tuning_done && !msm_host->calibration_done &&
1021 (mmc->ios.timing == MMC_TIMING_MMC_HS400)) {
Krishna Konda2faa7bb2014-06-04 01:25:16 -07001022 rc = sdhci_msm_hs400_dll_calibration(host);
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07001023 spin_lock_irqsave(&host->lock, flags);
1024 if (!rc)
1025 msm_host->calibration_done = true;
1026 spin_unlock_irqrestore(&host->lock, flags);
1027 goto out;
1028 }
1029
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001030 spin_lock_irqsave(&host->lock, flags);
1031
1032 if ((opcode == MMC_SEND_TUNING_BLOCK_HS200) &&
1033 (mmc->ios.bus_width == MMC_BUS_WIDTH_8)) {
1034 tuning_block_pattern = tuning_block_128;
1035 size = sizeof(tuning_block_128);
1036 }
1037 spin_unlock_irqrestore(&host->lock, flags);
1038
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001039 data_buf = kmalloc(size, GFP_KERNEL);
1040 if (!data_buf) {
1041 rc = -ENOMEM;
1042 goto out;
1043 }
1044
Sahitya Tummala9fe16532013-06-13 10:36:57 +05301045retry:
Krishna Konda96e6b112013-10-28 15:25:03 -07001046 tuned_phase_cnt = 0;
1047
Sahitya Tummala9fe16532013-06-13 10:36:57 +05301048 /* first of all reset the tuning block */
1049 rc = msm_init_cm_dll(host);
1050 if (rc)
1051 goto kfree;
1052
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001053 phase = 0;
1054 do {
1055 struct mmc_command cmd = {0};
1056 struct mmc_data data = {0};
1057 struct mmc_request mrq = {
1058 .cmd = &cmd,
1059 .data = &data
1060 };
1061 struct scatterlist sg;
1062
1063 /* set the phase in delay line hw block */
1064 rc = msm_config_cm_dll_phase(host, phase);
1065 if (rc)
1066 goto kfree;
1067
1068 cmd.opcode = opcode;
1069 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1070
1071 data.blksz = size;
1072 data.blocks = 1;
1073 data.flags = MMC_DATA_READ;
1074 data.timeout_ns = 1000 * 1000 * 1000; /* 1 sec */
1075
1076 data.sg = &sg;
1077 data.sg_len = 1;
1078 sg_init_one(&sg, data_buf, size);
1079 memset(data_buf, 0, size);
1080 mmc_wait_for_req(mmc, &mrq);
1081
1082 if (!cmd.error && !data.error &&
1083 !memcmp(data_buf, tuning_block_pattern, size)) {
1084 /* tuning is successful at this tuning point */
1085 tuned_phases[tuned_phase_cnt++] = phase;
Krishna Konda96e6b112013-10-28 15:25:03 -07001086 pr_debug("%s: %s: found *** good *** phase = %d\n",
1087 mmc_hostname(mmc), __func__, phase);
1088 } else {
1089 pr_debug("%s: %s: found ## bad ## phase = %d\n",
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001090 mmc_hostname(mmc), __func__, phase);
1091 }
1092 } while (++phase < 16);
1093
Sahitya Tummaladfdb4af2014-04-01 14:29:13 +05301094 if ((tuned_phase_cnt == NUM_TUNING_PHASES) &&
1095 card && mmc_card_mmc(card)) {
Krishna Konda96e6b112013-10-28 15:25:03 -07001096 /*
1097 * If all phases pass then its a problem. So change the card's
1098 * drive type to a different value, if supported and repeat
1099 * tuning until at least one phase fails. Then set the original
1100 * drive type back.
1101 *
1102 * If all the phases still pass after trying all possible
1103 * drive types, then one of those 16 phases will be picked.
1104 * This is no different from what was going on before the
1105 * modification to change drive type and retune.
1106 */
1107 pr_debug("%s: tuned phases count: %d\n", mmc_hostname(mmc),
1108 tuned_phase_cnt);
1109
1110 /* set drive type to other value . default setting is 0x0 */
1111 while (++drv_type <= MAX_DRV_TYPES_SUPPORTED_HS200) {
1112 if (card->ext_csd.raw_driver_strength &
1113 (1 << drv_type)) {
1114 sdhci_msm_set_mmc_drv_type(host, opcode,
1115 drv_type);
1116 if (!drv_type_changed)
1117 drv_type_changed = true;
1118 goto retry;
1119 }
1120 }
1121 }
1122
1123 /* reset drive type to default (50 ohm) if changed */
1124 if (drv_type_changed)
1125 sdhci_msm_set_mmc_drv_type(host, opcode, 0);
1126
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001127 if (tuned_phase_cnt) {
1128 rc = msm_find_most_appropriate_phase(host, tuned_phases,
1129 tuned_phase_cnt);
1130 if (rc < 0)
1131 goto kfree;
1132 else
1133 phase = (u8)rc;
1134
1135 /*
1136 * Finally set the selected phase in delay
1137 * line hw block.
1138 */
1139 rc = msm_config_cm_dll_phase(host, phase);
1140 if (rc)
1141 goto kfree;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07001142 msm_host->saved_tuning_phase = phase;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001143 pr_debug("%s: %s: finally setting the tuning phase to %d\n",
1144 mmc_hostname(mmc), __func__, phase);
1145 } else {
Sahitya Tummala9fe16532013-06-13 10:36:57 +05301146 if (--tuning_seq_cnt)
1147 goto retry;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001148 /* tuning failed */
1149 pr_err("%s: %s: no tuning point found\n",
1150 mmc_hostname(mmc), __func__);
Sahitya Tummala9fe16532013-06-13 10:36:57 +05301151 rc = -EIO;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001152 }
1153
1154kfree:
1155 kfree(data_buf);
1156out:
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07001157 spin_lock_irqsave(&host->lock, flags);
1158 if (!rc)
1159 msm_host->tuning_done = true;
1160 spin_unlock_irqrestore(&host->lock, flags);
1161 pr_debug("%s: Exit %s, err(%d)\n", mmc_hostname(mmc), __func__, rc);
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001162 return rc;
1163}
1164
Asutosh Das0ef24812012-12-18 16:14:02 +05301165static int sdhci_msm_setup_gpio(struct sdhci_msm_pltfm_data *pdata, bool enable)
1166{
1167 struct sdhci_msm_gpio_data *curr;
1168 int i, ret = 0;
1169
1170 curr = pdata->pin_data->gpio_data;
1171 for (i = 0; i < curr->size; i++) {
1172 if (!gpio_is_valid(curr->gpio[i].no)) {
1173 ret = -EINVAL;
1174 pr_err("%s: Invalid gpio = %d\n", __func__,
1175 curr->gpio[i].no);
1176 goto free_gpios;
1177 }
1178 if (enable) {
1179 ret = gpio_request(curr->gpio[i].no,
1180 curr->gpio[i].name);
1181 if (ret) {
1182 pr_err("%s: gpio_request(%d, %s) failed %d\n",
1183 __func__, curr->gpio[i].no,
1184 curr->gpio[i].name, ret);
1185 goto free_gpios;
1186 }
1187 curr->gpio[i].is_enabled = true;
1188 } else {
1189 gpio_free(curr->gpio[i].no);
1190 curr->gpio[i].is_enabled = false;
1191 }
1192 }
1193 return ret;
1194
1195free_gpios:
1196 for (i--; i >= 0; i--) {
1197 gpio_free(curr->gpio[i].no);
1198 curr->gpio[i].is_enabled = false;
1199 }
1200 return ret;
1201}
1202
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301203static int sdhci_msm_setup_pinctrl(struct sdhci_msm_pltfm_data *pdata,
1204 bool enable)
1205{
1206 int ret = 0;
1207
1208 if (enable)
1209 ret = pinctrl_select_state(pdata->pctrl_data->pctrl,
1210 pdata->pctrl_data->pins_active);
1211 else
1212 ret = pinctrl_select_state(pdata->pctrl_data->pctrl,
1213 pdata->pctrl_data->pins_sleep);
1214
1215 if (ret < 0)
1216 pr_err("%s state for pinctrl failed with %d\n",
1217 enable ? "Enabling" : "Disabling", ret);
1218
1219 return ret;
1220}
1221
Asutosh Das0ef24812012-12-18 16:14:02 +05301222static int sdhci_msm_setup_pins(struct sdhci_msm_pltfm_data *pdata, bool enable)
1223{
1224 int ret = 0;
1225
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301226 if (pdata->pin_cfg_sts == enable) {
Asutosh Das0ef24812012-12-18 16:14:02 +05301227 return 0;
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301228 } else if (pdata->pctrl_data) {
1229 ret = sdhci_msm_setup_pinctrl(pdata, enable);
1230 goto out;
1231 } else if (!pdata->pin_data) {
1232 return 0;
1233 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301234
Sahitya Tummala1cd7e072014-02-14 13:19:01 +05301235 if (pdata->pin_data->is_gpio)
1236 ret = sdhci_msm_setup_gpio(pdata, enable);
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301237out:
Asutosh Das0ef24812012-12-18 16:14:02 +05301238 if (!ret)
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301239 pdata->pin_cfg_sts = enable;
Asutosh Das0ef24812012-12-18 16:14:02 +05301240
1241 return ret;
1242}
1243
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301244static int sdhci_msm_dt_get_array(struct device *dev, const char *prop_name,
1245 u32 **out, int *len, u32 size)
1246{
1247 int ret = 0;
1248 struct device_node *np = dev->of_node;
1249 size_t sz;
1250 u32 *arr = NULL;
1251
1252 if (!of_get_property(np, prop_name, len)) {
1253 ret = -EINVAL;
1254 goto out;
1255 }
1256 sz = *len = *len / sizeof(*arr);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07001257 if (sz <= 0 || (size > 0 && (sz > size))) {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301258 dev_err(dev, "%s invalid size\n", prop_name);
1259 ret = -EINVAL;
1260 goto out;
1261 }
1262
1263 arr = devm_kzalloc(dev, sz * sizeof(*arr), GFP_KERNEL);
1264 if (!arr) {
1265 dev_err(dev, "%s failed allocating memory\n", prop_name);
1266 ret = -ENOMEM;
1267 goto out;
1268 }
1269
1270 ret = of_property_read_u32_array(np, prop_name, arr, sz);
1271 if (ret < 0) {
1272 dev_err(dev, "%s failed reading array %d\n", prop_name, ret);
1273 goto out;
1274 }
1275 *out = arr;
1276out:
1277 if (ret)
1278 *len = 0;
1279 return ret;
1280}
1281
Asutosh Das0ef24812012-12-18 16:14:02 +05301282#define MAX_PROP_SIZE 32
1283static int sdhci_msm_dt_parse_vreg_info(struct device *dev,
1284 struct sdhci_msm_reg_data **vreg_data, const char *vreg_name)
1285{
1286 int len, ret = 0;
1287 const __be32 *prop;
1288 char prop_name[MAX_PROP_SIZE];
1289 struct sdhci_msm_reg_data *vreg;
1290 struct device_node *np = dev->of_node;
1291
1292 snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", vreg_name);
1293 if (!of_parse_phandle(np, prop_name, 0)) {
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301294 dev_info(dev, "No vreg data found for %s\n", vreg_name);
Asutosh Das0ef24812012-12-18 16:14:02 +05301295 return ret;
1296 }
1297
1298 vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
1299 if (!vreg) {
1300 dev_err(dev, "No memory for vreg: %s\n", vreg_name);
1301 ret = -ENOMEM;
1302 return ret;
1303 }
1304
1305 vreg->name = vreg_name;
1306
1307 snprintf(prop_name, MAX_PROP_SIZE,
1308 "qcom,%s-always-on", vreg_name);
1309 if (of_get_property(np, prop_name, NULL))
1310 vreg->is_always_on = true;
1311
1312 snprintf(prop_name, MAX_PROP_SIZE,
1313 "qcom,%s-lpm-sup", vreg_name);
1314 if (of_get_property(np, prop_name, NULL))
1315 vreg->lpm_sup = true;
1316
1317 snprintf(prop_name, MAX_PROP_SIZE,
1318 "qcom,%s-voltage-level", vreg_name);
1319 prop = of_get_property(np, prop_name, &len);
1320 if (!prop || (len != (2 * sizeof(__be32)))) {
1321 dev_warn(dev, "%s %s property\n",
1322 prop ? "invalid format" : "no", prop_name);
1323 } else {
1324 vreg->low_vol_level = be32_to_cpup(&prop[0]);
1325 vreg->high_vol_level = be32_to_cpup(&prop[1]);
1326 }
1327
1328 snprintf(prop_name, MAX_PROP_SIZE,
1329 "qcom,%s-current-level", vreg_name);
1330 prop = of_get_property(np, prop_name, &len);
1331 if (!prop || (len != (2 * sizeof(__be32)))) {
1332 dev_warn(dev, "%s %s property\n",
1333 prop ? "invalid format" : "no", prop_name);
1334 } else {
1335 vreg->lpm_uA = be32_to_cpup(&prop[0]);
1336 vreg->hpm_uA = be32_to_cpup(&prop[1]);
1337 }
1338
1339 *vreg_data = vreg;
1340 dev_dbg(dev, "%s: %s %s vol=[%d %d]uV, curr=[%d %d]uA\n",
1341 vreg->name, vreg->is_always_on ? "always_on," : "",
1342 vreg->lpm_sup ? "lpm_sup," : "", vreg->low_vol_level,
1343 vreg->high_vol_level, vreg->lpm_uA, vreg->hpm_uA);
1344
1345 return ret;
1346}
1347
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301348static int sdhci_msm_parse_pinctrl_info(struct device *dev,
1349 struct sdhci_msm_pltfm_data *pdata)
1350{
1351 struct sdhci_pinctrl_data *pctrl_data;
1352 struct pinctrl *pctrl;
1353 int ret = 0;
1354
1355 /* Try to obtain pinctrl handle */
1356 pctrl = devm_pinctrl_get(dev);
1357 if (IS_ERR(pctrl)) {
1358 ret = PTR_ERR(pctrl);
1359 goto out;
1360 }
1361 pctrl_data = devm_kzalloc(dev, sizeof(*pctrl_data), GFP_KERNEL);
1362 if (!pctrl_data) {
1363 dev_err(dev, "No memory for sdhci_pinctrl_data\n");
1364 ret = -ENOMEM;
1365 goto out;
1366 }
1367 pctrl_data->pctrl = pctrl;
1368 /* Look-up and keep the states handy to be used later */
1369 pctrl_data->pins_active = pinctrl_lookup_state(
1370 pctrl_data->pctrl, "active");
1371 if (IS_ERR(pctrl_data->pins_active)) {
1372 ret = PTR_ERR(pctrl_data->pins_active);
1373 dev_err(dev, "Could not get active pinstates, err:%d\n", ret);
1374 goto out;
1375 }
1376 pctrl_data->pins_sleep = pinctrl_lookup_state(
1377 pctrl_data->pctrl, "sleep");
1378 if (IS_ERR(pctrl_data->pins_sleep)) {
1379 ret = PTR_ERR(pctrl_data->pins_sleep);
1380 dev_err(dev, "Could not get sleep pinstates, err:%d\n", ret);
1381 goto out;
1382 }
1383 pdata->pctrl_data = pctrl_data;
1384out:
1385 return ret;
1386}
1387
Asutosh Das0ef24812012-12-18 16:14:02 +05301388#define GPIO_NAME_MAX_LEN 32
1389static int sdhci_msm_dt_parse_gpio_info(struct device *dev,
1390 struct sdhci_msm_pltfm_data *pdata)
1391{
1392 int ret = 0, cnt, i;
1393 struct sdhci_msm_pin_data *pin_data;
1394 struct device_node *np = dev->of_node;
1395
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301396 ret = sdhci_msm_parse_pinctrl_info(dev, pdata);
1397 if (!ret) {
1398 goto out;
1399 } else if (ret == -EPROBE_DEFER) {
1400 dev_err(dev, "Pinctrl framework not registered, err:%d\n", ret);
1401 goto out;
1402 } else {
1403 dev_err(dev, "Parsing Pinctrl failed with %d, falling back on GPIO lib\n",
1404 ret);
Sahitya Tummala1cd7e072014-02-14 13:19:01 +05301405 ret = 0;
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301406 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301407 pin_data = devm_kzalloc(dev, sizeof(*pin_data), GFP_KERNEL);
1408 if (!pin_data) {
1409 dev_err(dev, "No memory for pin_data\n");
1410 ret = -ENOMEM;
1411 goto out;
1412 }
1413
1414 cnt = of_gpio_count(np);
1415 if (cnt > 0) {
1416 pin_data->gpio_data = devm_kzalloc(dev,
1417 sizeof(struct sdhci_msm_gpio_data), GFP_KERNEL);
1418 if (!pin_data->gpio_data) {
1419 dev_err(dev, "No memory for gpio_data\n");
1420 ret = -ENOMEM;
1421 goto out;
1422 }
1423 pin_data->gpio_data->size = cnt;
1424 pin_data->gpio_data->gpio = devm_kzalloc(dev, cnt *
1425 sizeof(struct sdhci_msm_gpio), GFP_KERNEL);
1426
1427 if (!pin_data->gpio_data->gpio) {
1428 dev_err(dev, "No memory for gpio\n");
1429 ret = -ENOMEM;
1430 goto out;
1431 }
1432
1433 for (i = 0; i < cnt; i++) {
1434 const char *name = NULL;
1435 char result[GPIO_NAME_MAX_LEN];
1436 pin_data->gpio_data->gpio[i].no = of_get_gpio(np, i);
1437 of_property_read_string_index(np,
1438 "qcom,gpio-names", i, &name);
1439
1440 snprintf(result, GPIO_NAME_MAX_LEN, "%s-%s",
1441 dev_name(dev), name ? name : "?");
1442 pin_data->gpio_data->gpio[i].name = result;
1443 dev_dbg(dev, "%s: gpio[%s] = %d\n", __func__,
1444 pin_data->gpio_data->gpio[i].name,
1445 pin_data->gpio_data->gpio[i].no);
Asutosh Das0ef24812012-12-18 16:14:02 +05301446 }
1447 }
Sahitya Tummala1cd7e072014-02-14 13:19:01 +05301448 pdata->pin_data = pin_data;
Asutosh Das0ef24812012-12-18 16:14:02 +05301449out:
1450 if (ret)
1451 dev_err(dev, "%s failed with err %d\n", __func__, ret);
1452 return ret;
1453}
1454
Maya Erez994cf2f2014-10-21 20:22:04 +03001455#ifdef CONFIG_SMP
1456static void sdhci_msm_populate_affinity_type(struct sdhci_msm_pltfm_data *pdata,
1457 struct device_node *np)
1458{
1459 const char *cpu_affinity = NULL;
1460
1461 pdata->cpu_affinity_type = PM_QOS_REQ_AFFINE_IRQ;
1462 if (!of_property_read_string(np, "qcom,cpu-affinity",
1463 &cpu_affinity)) {
1464 if (!strcmp(cpu_affinity, "all_cores"))
1465 pdata->cpu_affinity_type = PM_QOS_REQ_ALL_CORES;
1466 else if (!strcmp(cpu_affinity, "affine_cores"))
1467 pdata->cpu_affinity_type = PM_QOS_REQ_AFFINE_CORES;
1468 else if (!strcmp(cpu_affinity, "affine_irq"))
1469 pdata->cpu_affinity_type = PM_QOS_REQ_AFFINE_IRQ;
1470 }
1471}
1472#else
1473static void sdhci_msm_populate_affinity_type(struct sdhci_msm_pltfm_data *pdata,
1474 struct device_node *np)
1475{
1476}
1477#endif
1478
Asutosh Das0ef24812012-12-18 16:14:02 +05301479/* Parse platform data */
1480static struct sdhci_msm_pltfm_data *sdhci_msm_populate_pdata(struct device *dev)
1481{
1482 struct sdhci_msm_pltfm_data *pdata = NULL;
1483 struct device_node *np = dev->of_node;
1484 u32 bus_width = 0;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05301485 u32 cpu_dma_latency;
Asutosh Das0ef24812012-12-18 16:14:02 +05301486 int len, i;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301487 int clk_table_len;
1488 u32 *clk_table = NULL;
Sujit Reddy Thumma1958d3d2013-06-03 09:54:32 +05301489 enum of_gpio_flags flags = OF_GPIO_ACTIVE_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05301490
1491 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1492 if (!pdata) {
1493 dev_err(dev, "failed to allocate memory for platform data\n");
1494 goto out;
1495 }
1496
Sujit Reddy Thumma1958d3d2013-06-03 09:54:32 +05301497 pdata->status_gpio = of_get_named_gpio_flags(np, "cd-gpios", 0, &flags);
1498 if (gpio_is_valid(pdata->status_gpio) & !(flags & OF_GPIO_ACTIVE_LOW))
1499 pdata->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
Sahitya Tummala581df132013-03-12 14:57:46 +05301500
Asutosh Das0ef24812012-12-18 16:14:02 +05301501 of_property_read_u32(np, "qcom,bus-width", &bus_width);
1502 if (bus_width == 8)
1503 pdata->mmc_bus_width = MMC_CAP_8_BIT_DATA;
1504 else if (bus_width == 4)
1505 pdata->mmc_bus_width = MMC_CAP_4_BIT_DATA;
1506 else {
1507 dev_notice(dev, "invalid bus-width, default to 1-bit mode\n");
1508 pdata->mmc_bus_width = 0;
1509 }
1510
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05301511 if (!of_property_read_u32(np, "qcom,cpu-dma-latency-us",
1512 &cpu_dma_latency))
1513 pdata->cpu_dma_latency_us = cpu_dma_latency;
Asutosh Das598a4d42014-02-05 16:38:23 +05301514 else
1515 pdata->cpu_dma_latency_us = MSM_MMC_DEFAULT_CPU_DMA_LATENCY;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301516 if (sdhci_msm_dt_get_array(dev, "qcom,clk-rates",
1517 &clk_table, &clk_table_len, 0)) {
1518 dev_err(dev, "failed parsing supported clock rates\n");
1519 goto out;
1520 }
1521 if (!clk_table || !clk_table_len) {
1522 dev_err(dev, "Invalid clock table\n");
1523 goto out;
1524 }
1525 pdata->sup_clk_table = clk_table;
1526 pdata->sup_clk_cnt = clk_table_len;
1527
Asutosh Das0ef24812012-12-18 16:14:02 +05301528 pdata->vreg_data = devm_kzalloc(dev, sizeof(struct
1529 sdhci_msm_slot_reg_data),
1530 GFP_KERNEL);
1531 if (!pdata->vreg_data) {
1532 dev_err(dev, "failed to allocate memory for vreg data\n");
1533 goto out;
1534 }
1535
1536 if (sdhci_msm_dt_parse_vreg_info(dev, &pdata->vreg_data->vdd_data,
1537 "vdd")) {
1538 dev_err(dev, "failed parsing vdd data\n");
1539 goto out;
1540 }
1541 if (sdhci_msm_dt_parse_vreg_info(dev,
1542 &pdata->vreg_data->vdd_io_data,
1543 "vdd-io")) {
1544 dev_err(dev, "failed parsing vdd-io data\n");
1545 goto out;
1546 }
1547
1548 if (sdhci_msm_dt_parse_gpio_info(dev, pdata)) {
1549 dev_err(dev, "failed parsing gpio data\n");
1550 goto out;
1551 }
1552
Asutosh Das0ef24812012-12-18 16:14:02 +05301553 len = of_property_count_strings(np, "qcom,bus-speed-mode");
1554
1555 for (i = 0; i < len; i++) {
1556 const char *name = NULL;
1557
1558 of_property_read_string_index(np,
1559 "qcom,bus-speed-mode", i, &name);
1560 if (!name)
1561 continue;
1562
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07001563 if (!strncmp(name, "HS400_1p8v", sizeof("HS400_1p8v")))
1564 pdata->caps2 |= MMC_CAP2_HS400_1_8V;
1565 else if (!strncmp(name, "HS400_1p2v", sizeof("HS400_1p2v")))
1566 pdata->caps2 |= MMC_CAP2_HS400_1_2V;
1567 else if (!strncmp(name, "HS200_1p8v", sizeof("HS200_1p8v")))
Asutosh Das0ef24812012-12-18 16:14:02 +05301568 pdata->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
1569 else if (!strncmp(name, "HS200_1p2v", sizeof("HS200_1p2v")))
1570 pdata->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
1571 else if (!strncmp(name, "DDR_1p8v", sizeof("DDR_1p8v")))
1572 pdata->caps |= MMC_CAP_1_8V_DDR
1573 | MMC_CAP_UHS_DDR50;
1574 else if (!strncmp(name, "DDR_1p2v", sizeof("DDR_1p2v")))
1575 pdata->caps |= MMC_CAP_1_2V_DDR
1576 | MMC_CAP_UHS_DDR50;
1577 }
1578
1579 if (of_get_property(np, "qcom,nonremovable", NULL))
1580 pdata->nonremovable = true;
1581
Guoping Yuf7c91332014-08-20 16:56:18 +08001582 if (of_get_property(np, "qcom,nonhotplug", NULL))
1583 pdata->nonhotplug = true;
1584
Maya Erez994cf2f2014-10-21 20:22:04 +03001585 sdhci_msm_populate_affinity_type(pdata, np);
1586
Asutosh Das0ef24812012-12-18 16:14:02 +05301587 return pdata;
1588out:
1589 return NULL;
1590}
1591
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301592/* Returns required bandwidth in Bytes per Sec */
1593static unsigned int sdhci_get_bw_required(struct sdhci_host *host,
1594 struct mmc_ios *ios)
1595{
Sahitya Tummala2886c922013-04-03 18:03:31 +05301596 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1597 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1598
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301599 unsigned int bw;
1600
Sahitya Tummala2886c922013-04-03 18:03:31 +05301601 bw = msm_host->clk_rate;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301602 /*
1603 * For DDR mode, SDCC controller clock will be at
1604 * the double rate than the actual clock that goes to card.
1605 */
1606 if (ios->bus_width == MMC_BUS_WIDTH_4)
1607 bw /= 2;
1608 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1609 bw /= 8;
1610
1611 return bw;
1612}
1613
1614static int sdhci_msm_bus_get_vote_for_bw(struct sdhci_msm_host *host,
1615 unsigned int bw)
1616{
1617 unsigned int *table = host->pdata->voting_data->bw_vecs;
1618 unsigned int size = host->pdata->voting_data->bw_vecs_size;
1619 int i;
1620
1621 if (host->msm_bus_vote.is_max_bw_needed && bw)
1622 return host->msm_bus_vote.max_bw_vote;
1623
1624 for (i = 0; i < size; i++) {
1625 if (bw <= table[i])
1626 break;
1627 }
1628
1629 if (i && (i == size))
1630 i--;
1631
1632 return i;
1633}
1634
1635/*
1636 * This function must be called with host lock acquired.
1637 * Caller of this function should also ensure that msm bus client
1638 * handle is not null.
1639 */
1640static inline int sdhci_msm_bus_set_vote(struct sdhci_msm_host *msm_host,
1641 int vote,
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301642 unsigned long *flags)
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301643{
1644 struct sdhci_host *host = platform_get_drvdata(msm_host->pdev);
1645 int rc = 0;
1646
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301647 BUG_ON(!flags);
1648
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301649 if (vote != msm_host->msm_bus_vote.curr_vote) {
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301650 spin_unlock_irqrestore(&host->lock, *flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301651 rc = msm_bus_scale_client_update_request(
1652 msm_host->msm_bus_vote.client_handle, vote);
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301653 spin_lock_irqsave(&host->lock, *flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301654 if (rc) {
1655 pr_err("%s: msm_bus_scale_client_update_request() failed: bus_client_handle=0x%x, vote=%d, err=%d\n",
1656 mmc_hostname(host->mmc),
1657 msm_host->msm_bus_vote.client_handle, vote, rc);
1658 goto out;
1659 }
1660 msm_host->msm_bus_vote.curr_vote = vote;
1661 }
1662out:
1663 return rc;
1664}
1665
1666/*
1667 * Internal work. Work to set 0 bandwidth for msm bus.
1668 */
1669static void sdhci_msm_bus_work(struct work_struct *work)
1670{
1671 struct sdhci_msm_host *msm_host;
1672 struct sdhci_host *host;
1673 unsigned long flags;
1674
1675 msm_host = container_of(work, struct sdhci_msm_host,
1676 msm_bus_vote.vote_work.work);
1677 host = platform_get_drvdata(msm_host->pdev);
1678
1679 if (!msm_host->msm_bus_vote.client_handle)
1680 return;
1681
1682 spin_lock_irqsave(&host->lock, flags);
1683 /* don't vote for 0 bandwidth if any request is in progress */
1684 if (!host->mrq) {
1685 sdhci_msm_bus_set_vote(msm_host,
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301686 msm_host->msm_bus_vote.min_bw_vote, &flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301687 } else
1688 pr_warning("%s: %s: Transfer in progress. skipping bus voting to 0 bandwidth\n",
1689 mmc_hostname(host->mmc), __func__);
1690 spin_unlock_irqrestore(&host->lock, flags);
1691}
1692
1693/*
1694 * This function cancels any scheduled delayed work and sets the bus
1695 * vote based on bw (bandwidth) argument.
1696 */
1697static void sdhci_msm_bus_cancel_work_and_set_vote(struct sdhci_host *host,
1698 unsigned int bw)
1699{
1700 int vote;
1701 unsigned long flags;
1702 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1703 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1704
1705 cancel_delayed_work_sync(&msm_host->msm_bus_vote.vote_work);
1706 spin_lock_irqsave(&host->lock, flags);
1707 vote = sdhci_msm_bus_get_vote_for_bw(msm_host, bw);
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301708 sdhci_msm_bus_set_vote(msm_host, vote, &flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301709 spin_unlock_irqrestore(&host->lock, flags);
1710}
1711
1712#define MSM_MMC_BUS_VOTING_DELAY 200 /* msecs */
1713
1714/* This function queues a work which will set the bandwidth requiement to 0 */
1715static void sdhci_msm_bus_queue_work(struct sdhci_host *host)
1716{
1717 unsigned long flags;
1718 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1719 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1720
1721 spin_lock_irqsave(&host->lock, flags);
1722 if (msm_host->msm_bus_vote.min_bw_vote !=
1723 msm_host->msm_bus_vote.curr_vote)
1724 queue_delayed_work(system_wq,
1725 &msm_host->msm_bus_vote.vote_work,
1726 msecs_to_jiffies(MSM_MMC_BUS_VOTING_DELAY));
1727 spin_unlock_irqrestore(&host->lock, flags);
1728}
1729
1730static int sdhci_msm_bus_register(struct sdhci_msm_host *host,
1731 struct platform_device *pdev)
1732{
1733 int rc = 0;
1734 struct msm_bus_scale_pdata *bus_pdata;
1735
1736 struct sdhci_msm_bus_voting_data *data;
1737 struct device *dev = &pdev->dev;
1738
1739 data = devm_kzalloc(dev,
1740 sizeof(struct sdhci_msm_bus_voting_data), GFP_KERNEL);
1741 if (!data) {
1742 dev_err(&pdev->dev,
1743 "%s: failed to allocate memory\n", __func__);
1744 rc = -ENOMEM;
1745 goto out;
1746 }
1747 data->bus_pdata = msm_bus_cl_get_pdata(pdev);
1748 if (data->bus_pdata) {
1749 rc = sdhci_msm_dt_get_array(dev, "qcom,bus-bw-vectors-bps",
1750 &data->bw_vecs, &data->bw_vecs_size, 0);
1751 if (rc) {
1752 dev_err(&pdev->dev,
1753 "%s: Failed to get bus-bw-vectors-bps\n",
1754 __func__);
1755 goto out;
1756 }
1757 host->pdata->voting_data = data;
1758 }
1759 if (host->pdata->voting_data &&
1760 host->pdata->voting_data->bus_pdata &&
1761 host->pdata->voting_data->bw_vecs &&
1762 host->pdata->voting_data->bw_vecs_size) {
1763
1764 bus_pdata = host->pdata->voting_data->bus_pdata;
1765 host->msm_bus_vote.client_handle =
1766 msm_bus_scale_register_client(bus_pdata);
1767 if (!host->msm_bus_vote.client_handle) {
1768 dev_err(&pdev->dev, "msm_bus_scale_register_client()\n");
1769 rc = -EFAULT;
1770 goto out;
1771 }
1772 /* cache the vote index for minimum and maximum bandwidth */
1773 host->msm_bus_vote.min_bw_vote =
1774 sdhci_msm_bus_get_vote_for_bw(host, 0);
1775 host->msm_bus_vote.max_bw_vote =
1776 sdhci_msm_bus_get_vote_for_bw(host, UINT_MAX);
1777 } else {
1778 devm_kfree(dev, data);
1779 }
1780
1781out:
1782 return rc;
1783}
1784
1785static void sdhci_msm_bus_unregister(struct sdhci_msm_host *host)
1786{
1787 if (host->msm_bus_vote.client_handle)
1788 msm_bus_scale_unregister_client(
1789 host->msm_bus_vote.client_handle);
1790}
1791
1792static void sdhci_msm_bus_voting(struct sdhci_host *host, u32 enable)
1793{
1794 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1795 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1796 struct mmc_ios *ios = &host->mmc->ios;
1797 unsigned int bw;
1798
1799 if (!msm_host->msm_bus_vote.client_handle)
1800 return;
1801
1802 bw = sdhci_get_bw_required(host, ios);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301803 if (enable) {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301804 sdhci_msm_bus_cancel_work_and_set_vote(host, bw);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301805 } else {
1806 /*
1807 * If clock gating is enabled, then remove the vote
1808 * immediately because clocks will be disabled only
1809 * after SDHCI_MSM_MMC_CLK_GATE_DELAY and thus no
1810 * additional delay is required to remove the bus vote.
1811 */
1812#ifdef CONFIG_MMC_CLKGATE
1813 if (host->mmc->clkgate_delay)
1814 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
1815 else
1816#endif
1817 sdhci_msm_bus_queue_work(host);
1818 }
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301819}
1820
Asutosh Das0ef24812012-12-18 16:14:02 +05301821/* Regulator utility functions */
1822static int sdhci_msm_vreg_init_reg(struct device *dev,
1823 struct sdhci_msm_reg_data *vreg)
1824{
1825 int ret = 0;
1826
1827 /* check if regulator is already initialized? */
1828 if (vreg->reg)
1829 goto out;
1830
1831 /* Get the regulator handle */
1832 vreg->reg = devm_regulator_get(dev, vreg->name);
1833 if (IS_ERR(vreg->reg)) {
1834 ret = PTR_ERR(vreg->reg);
1835 pr_err("%s: devm_regulator_get(%s) failed. ret=%d\n",
1836 __func__, vreg->name, ret);
1837 goto out;
1838 }
1839
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301840 if (regulator_count_voltages(vreg->reg) > 0) {
1841 vreg->set_voltage_sup = true;
1842 /* sanity check */
1843 if (!vreg->high_vol_level || !vreg->hpm_uA) {
1844 pr_err("%s: %s invalid constraints specified\n",
1845 __func__, vreg->name);
1846 ret = -EINVAL;
1847 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301848 }
1849
1850out:
1851 return ret;
1852}
1853
1854static void sdhci_msm_vreg_deinit_reg(struct sdhci_msm_reg_data *vreg)
1855{
1856 if (vreg->reg)
1857 devm_regulator_put(vreg->reg);
1858}
1859
1860static int sdhci_msm_vreg_set_optimum_mode(struct sdhci_msm_reg_data
1861 *vreg, int uA_load)
1862{
1863 int ret = 0;
1864
1865 /*
1866 * regulators that do not support regulator_set_voltage also
1867 * do not support regulator_set_optimum_mode
1868 */
1869 if (vreg->set_voltage_sup) {
1870 ret = regulator_set_load(vreg->reg, uA_load);
1871 if (ret < 0)
1872 pr_err("%s: regulator_set_load(reg=%s,uA_load=%d) failed. ret=%d\n",
1873 __func__, vreg->name, uA_load, ret);
1874 else
1875 /*
1876 * regulator_set_load() can return non zero
1877 * value even for success case.
1878 */
1879 ret = 0;
1880 }
1881 return ret;
1882}
1883
1884static int sdhci_msm_vreg_set_voltage(struct sdhci_msm_reg_data *vreg,
1885 int min_uV, int max_uV)
1886{
1887 int ret = 0;
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301888 if (vreg->set_voltage_sup) {
1889 ret = regulator_set_voltage(vreg->reg, min_uV, max_uV);
1890 if (ret) {
1891 pr_err("%s: regulator_set_voltage(%s)failed. min_uV=%d,max_uV=%d,ret=%d\n",
Asutosh Das0ef24812012-12-18 16:14:02 +05301892 __func__, vreg->name, min_uV, max_uV, ret);
1893 }
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301894 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301895
1896 return ret;
1897}
1898
1899static int sdhci_msm_vreg_enable(struct sdhci_msm_reg_data *vreg)
1900{
1901 int ret = 0;
1902
1903 /* Put regulator in HPM (high power mode) */
1904 ret = sdhci_msm_vreg_set_optimum_mode(vreg, vreg->hpm_uA);
1905 if (ret < 0)
1906 return ret;
1907
1908 if (!vreg->is_enabled) {
1909 /* Set voltage level */
1910 ret = sdhci_msm_vreg_set_voltage(vreg, vreg->high_vol_level,
1911 vreg->high_vol_level);
1912 if (ret)
1913 return ret;
1914 }
1915 ret = regulator_enable(vreg->reg);
1916 if (ret) {
1917 pr_err("%s: regulator_enable(%s) failed. ret=%d\n",
1918 __func__, vreg->name, ret);
1919 return ret;
1920 }
1921 vreg->is_enabled = true;
1922 return ret;
1923}
1924
1925static int sdhci_msm_vreg_disable(struct sdhci_msm_reg_data *vreg)
1926{
1927 int ret = 0;
1928
1929 /* Never disable regulator marked as always_on */
1930 if (vreg->is_enabled && !vreg->is_always_on) {
1931 ret = regulator_disable(vreg->reg);
1932 if (ret) {
1933 pr_err("%s: regulator_disable(%s) failed. ret=%d\n",
1934 __func__, vreg->name, ret);
1935 goto out;
1936 }
1937 vreg->is_enabled = false;
1938
1939 ret = sdhci_msm_vreg_set_optimum_mode(vreg, 0);
1940 if (ret < 0)
1941 goto out;
1942
1943 /* Set min. voltage level to 0 */
1944 ret = sdhci_msm_vreg_set_voltage(vreg, 0, vreg->high_vol_level);
1945 if (ret)
1946 goto out;
1947 } else if (vreg->is_enabled && vreg->is_always_on) {
1948 if (vreg->lpm_sup) {
1949 /* Put always_on regulator in LPM (low power mode) */
1950 ret = sdhci_msm_vreg_set_optimum_mode(vreg,
1951 vreg->lpm_uA);
1952 if (ret < 0)
1953 goto out;
1954 }
1955 }
1956out:
1957 return ret;
1958}
1959
1960static int sdhci_msm_setup_vreg(struct sdhci_msm_pltfm_data *pdata,
1961 bool enable, bool is_init)
1962{
1963 int ret = 0, i;
1964 struct sdhci_msm_slot_reg_data *curr_slot;
1965 struct sdhci_msm_reg_data *vreg_table[2];
1966
1967 curr_slot = pdata->vreg_data;
1968 if (!curr_slot) {
1969 pr_debug("%s: vreg info unavailable,assuming the slot is powered by always on domain\n",
1970 __func__);
1971 goto out;
1972 }
1973
1974 vreg_table[0] = curr_slot->vdd_data;
1975 vreg_table[1] = curr_slot->vdd_io_data;
1976
1977 for (i = 0; i < ARRAY_SIZE(vreg_table); i++) {
1978 if (vreg_table[i]) {
1979 if (enable)
1980 ret = sdhci_msm_vreg_enable(vreg_table[i]);
1981 else
1982 ret = sdhci_msm_vreg_disable(vreg_table[i]);
1983 if (ret)
1984 goto out;
1985 }
1986 }
1987out:
1988 return ret;
1989}
1990
1991/*
1992 * Reset vreg by ensuring it is off during probe. A call
1993 * to enable vreg is needed to balance disable vreg
1994 */
1995static int sdhci_msm_vreg_reset(struct sdhci_msm_pltfm_data *pdata)
1996{
1997 int ret;
1998
1999 ret = sdhci_msm_setup_vreg(pdata, 1, true);
2000 if (ret)
2001 return ret;
2002 ret = sdhci_msm_setup_vreg(pdata, 0, true);
2003 return ret;
2004}
2005
2006/* This init function should be called only once for each SDHC slot */
2007static int sdhci_msm_vreg_init(struct device *dev,
2008 struct sdhci_msm_pltfm_data *pdata,
2009 bool is_init)
2010{
2011 int ret = 0;
2012 struct sdhci_msm_slot_reg_data *curr_slot;
2013 struct sdhci_msm_reg_data *curr_vdd_reg, *curr_vdd_io_reg;
2014
2015 curr_slot = pdata->vreg_data;
2016 if (!curr_slot)
2017 goto out;
2018
2019 curr_vdd_reg = curr_slot->vdd_data;
2020 curr_vdd_io_reg = curr_slot->vdd_io_data;
2021
2022 if (!is_init)
2023 /* Deregister all regulators from regulator framework */
2024 goto vdd_io_reg_deinit;
2025
2026 /*
2027 * Get the regulator handle from voltage regulator framework
2028 * and then try to set the voltage level for the regulator
2029 */
2030 if (curr_vdd_reg) {
2031 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_reg);
2032 if (ret)
2033 goto out;
2034 }
2035 if (curr_vdd_io_reg) {
2036 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_io_reg);
2037 if (ret)
2038 goto vdd_reg_deinit;
2039 }
2040 ret = sdhci_msm_vreg_reset(pdata);
2041 if (ret)
2042 dev_err(dev, "vreg reset failed (%d)\n", ret);
2043 goto out;
2044
2045vdd_io_reg_deinit:
2046 if (curr_vdd_io_reg)
2047 sdhci_msm_vreg_deinit_reg(curr_vdd_io_reg);
2048vdd_reg_deinit:
2049 if (curr_vdd_reg)
2050 sdhci_msm_vreg_deinit_reg(curr_vdd_reg);
2051out:
2052 return ret;
2053}
2054
2055
2056static int sdhci_msm_set_vdd_io_vol(struct sdhci_msm_pltfm_data *pdata,
2057 enum vdd_io_level level,
2058 unsigned int voltage_level)
2059{
2060 int ret = 0;
2061 int set_level;
2062 struct sdhci_msm_reg_data *vdd_io_reg;
2063
2064 if (!pdata->vreg_data)
2065 return ret;
2066
2067 vdd_io_reg = pdata->vreg_data->vdd_io_data;
2068 if (vdd_io_reg && vdd_io_reg->is_enabled) {
2069 switch (level) {
2070 case VDD_IO_LOW:
2071 set_level = vdd_io_reg->low_vol_level;
2072 break;
2073 case VDD_IO_HIGH:
2074 set_level = vdd_io_reg->high_vol_level;
2075 break;
2076 case VDD_IO_SET_LEVEL:
2077 set_level = voltage_level;
2078 break;
2079 default:
2080 pr_err("%s: invalid argument level = %d",
2081 __func__, level);
2082 ret = -EINVAL;
2083 return ret;
2084 }
2085 ret = sdhci_msm_vreg_set_voltage(vdd_io_reg, set_level,
2086 set_level);
2087 }
2088 return ret;
2089}
2090
2091static irqreturn_t sdhci_msm_pwr_irq(int irq, void *data)
2092{
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002093 struct sdhci_host *host = (struct sdhci_host *)data;
2094 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2095 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Das0ef24812012-12-18 16:14:02 +05302096 u8 irq_status = 0;
2097 u8 irq_ack = 0;
2098 int ret = 0;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302099 int pwr_state = 0, io_level = 0;
2100 unsigned long flags;
Asutosh Das0ef24812012-12-18 16:14:02 +05302101
2102 irq_status = readb_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
2103 pr_debug("%s: Received IRQ(%d), status=0x%x\n",
2104 mmc_hostname(msm_host->mmc), irq, irq_status);
2105
2106 /* Clear the interrupt */
2107 writeb_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
2108 /*
2109 * SDHC has core_mem and hc_mem device memory and these memory
2110 * addresses do not fall within 1KB region. Hence, any update to
2111 * core_mem address space would require an mb() to ensure this gets
2112 * completed before its next update to registers within hc_mem.
2113 */
2114 mb();
2115
2116 /* Handle BUS ON/OFF*/
2117 if (irq_status & CORE_PWRCTL_BUS_ON) {
2118 ret = sdhci_msm_setup_vreg(msm_host->pdata, true, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302119 if (!ret) {
Asutosh Das0ef24812012-12-18 16:14:02 +05302120 ret = sdhci_msm_setup_pins(msm_host->pdata, true);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302121 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
2122 VDD_IO_HIGH, 0);
2123 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302124 if (ret)
2125 irq_ack |= CORE_PWRCTL_BUS_FAIL;
2126 else
2127 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302128
2129 pwr_state = REQ_BUS_ON;
2130 io_level = REQ_IO_HIGH;
Asutosh Das0ef24812012-12-18 16:14:02 +05302131 }
2132 if (irq_status & CORE_PWRCTL_BUS_OFF) {
2133 ret = sdhci_msm_setup_vreg(msm_host->pdata, false, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302134 if (!ret) {
Asutosh Das0ef24812012-12-18 16:14:02 +05302135 ret = sdhci_msm_setup_pins(msm_host->pdata, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302136 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
2137 VDD_IO_LOW, 0);
2138 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302139 if (ret)
2140 irq_ack |= CORE_PWRCTL_BUS_FAIL;
2141 else
2142 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302143
2144 pwr_state = REQ_BUS_OFF;
2145 io_level = REQ_IO_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05302146 }
2147 /* Handle IO LOW/HIGH */
2148 if (irq_status & CORE_PWRCTL_IO_LOW) {
2149 /* Switch voltage Low */
2150 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_LOW, 0);
2151 if (ret)
2152 irq_ack |= CORE_PWRCTL_IO_FAIL;
2153 else
2154 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302155
2156 io_level = REQ_IO_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05302157 }
2158 if (irq_status & CORE_PWRCTL_IO_HIGH) {
2159 /* Switch voltage High */
2160 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_HIGH, 0);
2161 if (ret)
2162 irq_ack |= CORE_PWRCTL_IO_FAIL;
2163 else
2164 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302165
2166 io_level = REQ_IO_HIGH;
Asutosh Das0ef24812012-12-18 16:14:02 +05302167 }
2168
2169 /* ACK status to the core */
2170 writeb_relaxed(irq_ack, (msm_host->core_mem + CORE_PWRCTL_CTL));
2171 /*
2172 * SDHC has core_mem and hc_mem device memory and these memory
2173 * addresses do not fall within 1KB region. Hence, any update to
2174 * core_mem address space would require an mb() to ensure this gets
2175 * completed before its next update to registers within hc_mem.
2176 */
2177 mb();
2178
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302179 if (io_level & REQ_IO_HIGH)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002180 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2181 ~CORE_IO_PAD_PWR_SWITCH),
2182 host->ioaddr + CORE_VENDOR_SPEC);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302183 else if (io_level & REQ_IO_LOW)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002184 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
2185 CORE_IO_PAD_PWR_SWITCH),
2186 host->ioaddr + CORE_VENDOR_SPEC);
2187 mb();
2188
Asutosh Das0ef24812012-12-18 16:14:02 +05302189 pr_debug("%s: Handled IRQ(%d), ret=%d, ack=0x%x\n",
2190 mmc_hostname(msm_host->mmc), irq, ret, irq_ack);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302191 spin_lock_irqsave(&host->lock, flags);
2192 if (pwr_state)
2193 msm_host->curr_pwr_state = pwr_state;
2194 if (io_level)
2195 msm_host->curr_io_level = io_level;
2196 complete(&msm_host->pwr_irq_completion);
2197 spin_unlock_irqrestore(&host->lock, flags);
2198
Asutosh Das0ef24812012-12-18 16:14:02 +05302199 return IRQ_HANDLED;
2200}
2201
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302202static ssize_t
Sahitya Tummala5c55b932013-06-20 14:00:18 +05302203show_polling(struct device *dev, struct device_attribute *attr, char *buf)
2204{
2205 struct sdhci_host *host = dev_get_drvdata(dev);
2206 int poll;
2207 unsigned long flags;
2208
2209 spin_lock_irqsave(&host->lock, flags);
2210 poll = !!(host->mmc->caps & MMC_CAP_NEEDS_POLL);
2211 spin_unlock_irqrestore(&host->lock, flags);
2212
2213 return snprintf(buf, PAGE_SIZE, "%d\n", poll);
2214}
2215
2216static ssize_t
2217store_polling(struct device *dev, struct device_attribute *attr,
2218 const char *buf, size_t count)
2219{
2220 struct sdhci_host *host = dev_get_drvdata(dev);
2221 int value;
2222 unsigned long flags;
2223
2224 if (!kstrtou32(buf, 0, &value)) {
2225 spin_lock_irqsave(&host->lock, flags);
2226 if (value) {
2227 host->mmc->caps |= MMC_CAP_NEEDS_POLL;
2228 mmc_detect_change(host->mmc, 0);
2229 } else {
2230 host->mmc->caps &= ~MMC_CAP_NEEDS_POLL;
2231 }
2232 spin_unlock_irqrestore(&host->lock, flags);
2233 }
2234 return count;
2235}
2236
2237static ssize_t
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302238show_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
2239 char *buf)
2240{
2241 struct sdhci_host *host = dev_get_drvdata(dev);
2242 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2243 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2244
2245 return snprintf(buf, PAGE_SIZE, "%u\n",
2246 msm_host->msm_bus_vote.is_max_bw_needed);
2247}
2248
2249static ssize_t
2250store_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
2251 const char *buf, size_t count)
2252{
2253 struct sdhci_host *host = dev_get_drvdata(dev);
2254 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2255 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2256 uint32_t value;
2257 unsigned long flags;
2258
2259 if (!kstrtou32(buf, 0, &value)) {
2260 spin_lock_irqsave(&host->lock, flags);
2261 msm_host->msm_bus_vote.is_max_bw_needed = !!value;
2262 spin_unlock_irqrestore(&host->lock, flags);
2263 }
2264 return count;
2265}
2266
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302267static void sdhci_msm_check_power_status(struct sdhci_host *host, u32 req_type)
Asutosh Das0ef24812012-12-18 16:14:02 +05302268{
2269 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2270 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302271 unsigned long flags;
2272 bool done = false;
Sahitya Tummala481fbb02013-08-06 15:22:28 +05302273 u32 io_sig_sts;
Asutosh Das0ef24812012-12-18 16:14:02 +05302274
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302275 spin_lock_irqsave(&host->lock, flags);
2276 pr_debug("%s: %s: request %d curr_pwr_state %x curr_io_level %x\n",
2277 mmc_hostname(host->mmc), __func__, req_type,
2278 msm_host->curr_pwr_state, msm_host->curr_io_level);
Sahitya Tummala481fbb02013-08-06 15:22:28 +05302279 io_sig_sts = readl_relaxed(msm_host->core_mem + CORE_GENERICS);
2280 /*
2281 * The IRQ for request type IO High/Low will be generated when -
2282 * 1. SWITCHABLE_SIGNALLING_VOL is enabled in HW.
2283 * 2. If 1 is true and when there is a state change in 1.8V enable
2284 * bit (bit 3) of SDHCI_HOST_CONTROL2 register. The reset state of
2285 * that bit is 0 which indicates 3.3V IO voltage. So, when MMC core
2286 * layer tries to set it to 3.3V before card detection happens, the
2287 * IRQ doesn't get triggered as there is no state change in this bit.
2288 * The driver already handles this case by changing the IO voltage
2289 * level to high as part of controller power up sequence. Hence, check
2290 * for host->pwr to handle a case where IO voltage high request is
2291 * issued even before controller power up.
2292 */
2293 if (req_type & (REQ_IO_HIGH | REQ_IO_LOW)) {
2294 if (!(io_sig_sts & SWITCHABLE_SIGNALLING_VOL) ||
2295 ((req_type & REQ_IO_HIGH) && !host->pwr)) {
2296 pr_debug("%s: do not wait for power IRQ that never comes\n",
2297 mmc_hostname(host->mmc));
2298 spin_unlock_irqrestore(&host->lock, flags);
2299 return;
2300 }
2301 }
2302
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302303 if ((req_type & msm_host->curr_pwr_state) ||
2304 (req_type & msm_host->curr_io_level))
2305 done = true;
2306 spin_unlock_irqrestore(&host->lock, flags);
Asutosh Das0ef24812012-12-18 16:14:02 +05302307
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302308 /*
2309 * This is needed here to hanlde a case where IRQ gets
2310 * triggered even before this function is called so that
2311 * x->done counter of completion gets reset. Otherwise,
2312 * next call to wait_for_completion returns immediately
2313 * without actually waiting for the IRQ to be handled.
2314 */
2315 if (done)
2316 init_completion(&msm_host->pwr_irq_completion);
2317 else
2318 wait_for_completion(&msm_host->pwr_irq_completion);
2319
2320 pr_debug("%s: %s: request %d done\n", mmc_hostname(host->mmc),
2321 __func__, req_type);
Asutosh Das0ef24812012-12-18 16:14:02 +05302322}
2323
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002324static void sdhci_msm_toggle_cdr(struct sdhci_host *host, bool enable)
2325{
2326 if (enable)
2327 writel_relaxed((readl_relaxed(host->ioaddr +
2328 CORE_DLL_CONFIG) | CORE_CDR_EN),
2329 host->ioaddr + CORE_DLL_CONFIG);
2330 else
2331 writel_relaxed((readl_relaxed(host->ioaddr +
2332 CORE_DLL_CONFIG) & ~CORE_CDR_EN),
2333 host->ioaddr + CORE_DLL_CONFIG);
2334}
2335
Asutosh Das648f9d12013-01-10 21:11:04 +05302336static unsigned int sdhci_msm_max_segs(void)
2337{
2338 return SDHCI_MSM_MAX_SEGMENTS;
2339}
2340
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302341static unsigned int sdhci_msm_get_min_clock(struct sdhci_host *host)
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302342{
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302343 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2344 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302345
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302346 return msm_host->pdata->sup_clk_table[0];
2347}
2348
2349static unsigned int sdhci_msm_get_max_clock(struct sdhci_host *host)
2350{
2351 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2352 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2353 int max_clk_index = msm_host->pdata->sup_clk_cnt;
2354
2355 return msm_host->pdata->sup_clk_table[max_clk_index - 1];
2356}
2357
2358static unsigned int sdhci_msm_get_sup_clk_rate(struct sdhci_host *host,
2359 u32 req_clk)
2360{
2361 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2362 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2363 unsigned int sel_clk = -1;
2364 unsigned char cnt;
2365
2366 if (req_clk < sdhci_msm_get_min_clock(host)) {
2367 sel_clk = sdhci_msm_get_min_clock(host);
2368 return sel_clk;
2369 }
2370
2371 for (cnt = 0; cnt < msm_host->pdata->sup_clk_cnt; cnt++) {
2372 if (msm_host->pdata->sup_clk_table[cnt] > req_clk) {
2373 break;
2374 } else if (msm_host->pdata->sup_clk_table[cnt] == req_clk) {
2375 sel_clk = msm_host->pdata->sup_clk_table[cnt];
2376 break;
2377 } else {
2378 sel_clk = msm_host->pdata->sup_clk_table[cnt];
2379 }
2380 }
2381 return sel_clk;
2382}
2383
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302384static int sdhci_msm_enable_controller_clock(struct sdhci_host *host)
2385{
2386 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2387 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2388 int rc = 0;
2389
2390 if (atomic_read(&msm_host->controller_clock))
2391 return 0;
2392
2393 sdhci_msm_bus_voting(host, 1);
2394
2395 if (!IS_ERR(msm_host->pclk)) {
2396 rc = clk_prepare_enable(msm_host->pclk);
2397 if (rc) {
2398 pr_err("%s: %s: failed to enable the pclk with error %d\n",
2399 mmc_hostname(host->mmc), __func__, rc);
2400 goto remove_vote;
2401 }
2402 }
2403
2404 rc = clk_prepare_enable(msm_host->clk);
2405 if (rc) {
2406 pr_err("%s: %s: failed to enable the host-clk with error %d\n",
2407 mmc_hostname(host->mmc), __func__, rc);
2408 goto disable_pclk;
2409 }
2410
2411 atomic_set(&msm_host->controller_clock, 1);
2412 pr_debug("%s: %s: enabled controller clock\n",
2413 mmc_hostname(host->mmc), __func__);
2414 goto out;
2415
2416disable_pclk:
2417 if (!IS_ERR(msm_host->pclk))
2418 clk_disable_unprepare(msm_host->pclk);
2419remove_vote:
2420 if (msm_host->msm_bus_vote.client_handle)
2421 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2422out:
2423 return rc;
2424}
2425
2426
2427
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302428static int sdhci_msm_prepare_clocks(struct sdhci_host *host, bool enable)
2429{
2430 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2431 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2432 int rc = 0;
2433
2434 if (enable && !atomic_read(&msm_host->clks_on)) {
2435 pr_debug("%s: request to enable clocks\n",
2436 mmc_hostname(host->mmc));
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302437
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302438 /*
2439 * The bus-width or the clock rate might have changed
2440 * after controller clocks are enbaled, update bus vote
2441 * in such case.
2442 */
2443 if (atomic_read(&msm_host->controller_clock))
2444 sdhci_msm_bus_voting(host, 1);
2445
2446 rc = sdhci_msm_enable_controller_clock(host);
2447 if (rc)
2448 goto remove_vote;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302449
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302450 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
2451 rc = clk_prepare_enable(msm_host->bus_clk);
2452 if (rc) {
2453 pr_err("%s: %s: failed to enable the bus-clock with error %d\n",
2454 mmc_hostname(host->mmc), __func__, rc);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302455 goto disable_controller_clk;
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302456 }
2457 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002458 if (!IS_ERR(msm_host->ff_clk)) {
2459 rc = clk_prepare_enable(msm_host->ff_clk);
2460 if (rc) {
2461 pr_err("%s: %s: failed to enable the ff_clk with error %d\n",
2462 mmc_hostname(host->mmc), __func__, rc);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302463 goto disable_bus_clk;
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002464 }
2465 }
2466 if (!IS_ERR(msm_host->sleep_clk)) {
2467 rc = clk_prepare_enable(msm_host->sleep_clk);
2468 if (rc) {
2469 pr_err("%s: %s: failed to enable the sleep_clk with error %d\n",
2470 mmc_hostname(host->mmc), __func__, rc);
2471 goto disable_ff_clk;
2472 }
2473 }
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302474 mb();
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302475
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302476 } else if (!enable && atomic_read(&msm_host->clks_on)) {
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302477 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
2478 mb();
Sahitya Tummaladc182982013-08-20 15:32:09 +05302479 /*
2480 * During 1.8V signal switching the clock source must
2481 * still be ON as it requires accessing SDHC
2482 * registers (SDHCi host control2 register bit 3 must
2483 * be written and polled after stopping the SDCLK).
2484 */
2485 if (host->mmc->card_clock_off)
2486 return 0;
2487 pr_debug("%s: request to disable clocks\n",
2488 mmc_hostname(host->mmc));
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002489 if (!IS_ERR_OR_NULL(msm_host->sleep_clk))
2490 clk_disable_unprepare(msm_host->sleep_clk);
2491 if (!IS_ERR_OR_NULL(msm_host->ff_clk))
2492 clk_disable_unprepare(msm_host->ff_clk);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302493 clk_disable_unprepare(msm_host->clk);
2494 if (!IS_ERR(msm_host->pclk))
2495 clk_disable_unprepare(msm_host->pclk);
2496 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2497 clk_disable_unprepare(msm_host->bus_clk);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302498
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302499 atomic_set(&msm_host->controller_clock, 0);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302500 sdhci_msm_bus_voting(host, 0);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302501 }
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302502 atomic_set(&msm_host->clks_on, enable);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302503 goto out;
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002504disable_ff_clk:
2505 if (!IS_ERR_OR_NULL(msm_host->ff_clk))
2506 clk_disable_unprepare(msm_host->ff_clk);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302507disable_bus_clk:
2508 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2509 clk_disable_unprepare(msm_host->bus_clk);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302510disable_controller_clk:
2511 if (!IS_ERR_OR_NULL(msm_host->clk))
2512 clk_disable_unprepare(msm_host->clk);
2513 if (!IS_ERR_OR_NULL(msm_host->pclk))
2514 clk_disable_unprepare(msm_host->pclk);
2515 atomic_set(&msm_host->controller_clock, 0);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302516remove_vote:
2517 if (msm_host->msm_bus_vote.client_handle)
2518 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302519out:
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302520 return rc;
2521}
2522
2523static void sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock)
2524{
2525 int rc;
2526 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2527 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2528 struct mmc_ios curr_ios = host->mmc->ios;
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002529 u32 sup_clock, ddr_clock, dll_lock;
Sahitya Tummala043744a2013-06-24 09:55:33 +05302530 bool curr_pwrsave;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302531
2532 if (!clock) {
Sujit Reddy Thummabf1aecc2014-01-10 10:58:54 +05302533 /*
2534 * disable pwrsave to ensure clock is not auto-gated until
2535 * the rate is >400KHz (initialization complete).
2536 */
2537 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2538 ~CORE_CLK_PWRSAVE, host->ioaddr + CORE_VENDOR_SPEC);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302539 sdhci_msm_prepare_clocks(host, false);
2540 host->clock = clock;
2541 goto out;
2542 }
2543
2544 rc = sdhci_msm_prepare_clocks(host, true);
2545 if (rc)
2546 goto out;
2547
Sahitya Tummala043744a2013-06-24 09:55:33 +05302548 curr_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2549 CORE_CLK_PWRSAVE);
Sahitya Tummalae000b242013-08-29 16:21:08 +05302550 if ((clock > 400000) &&
Venkat Gopalakrishnane6659b92015-01-21 15:51:37 -08002551 !curr_pwrsave)
Sahitya Tummala043744a2013-06-24 09:55:33 +05302552 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2553 | CORE_CLK_PWRSAVE,
2554 host->ioaddr + CORE_VENDOR_SPEC);
2555 /*
2556 * Disable pwrsave for a newly added card if doesn't allow clock
2557 * gating.
2558 */
Venkat Gopalakrishnane6659b92015-01-21 15:51:37 -08002559 else if (curr_pwrsave)
Sahitya Tummala043744a2013-06-24 09:55:33 +05302560 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2561 & ~CORE_CLK_PWRSAVE,
2562 host->ioaddr + CORE_VENDOR_SPEC);
2563
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302564 sup_clock = sdhci_msm_get_sup_clk_rate(host, clock);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002565 if ((curr_ios.timing == MMC_TIMING_UHS_DDR50) ||
Venkat Gopalakrishnan0a29da92015-01-09 12:19:16 -08002566 (curr_ios.timing == MMC_TIMING_MMC_DDR52) ||
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002567 (curr_ios.timing == MMC_TIMING_MMC_HS400)) {
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302568 /*
2569 * The SDHC requires internal clock frequency to be double the
2570 * actual clock that will be set for DDR mode. The controller
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002571 * uses the faster clock(100/400MHz) for some of its parts and
2572 * send the actual required clock (50/200MHz) to the card.
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302573 */
2574 ddr_clock = clock * 2;
2575 sup_clock = sdhci_msm_get_sup_clk_rate(host,
2576 ddr_clock);
2577 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002578
2579 /*
2580 * In general all timing modes are controlled via UHS mode select in
2581 * Host Control2 register. eMMC specific HS200/HS400 doesn't have
2582 * their respective modes defined here, hence we use these values.
2583 *
2584 * HS200 - SDR104 (Since they both are equivalent in functionality)
2585 * HS400 - This involves multiple configurations
2586 * Initially SDR104 - when tuning is required as HS200
2587 * Then when switching to DDR @ 400MHz (HS400) we use
2588 * the vendor specific HC_SELECT_IN to control the mode.
2589 *
2590 * In addition to controlling the modes we also need to select the
2591 * correct input clock for DLL depending on the mode.
2592 *
2593 * HS400 - divided clock (free running MCLK/2)
2594 * All other modes - default (free running MCLK)
2595 */
2596 if (curr_ios.timing == MMC_TIMING_MMC_HS400) {
2597 /* Select the divided clock (free running MCLK/2) */
2598 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2599 & ~CORE_HC_MCLK_SEL_MASK)
2600 | CORE_HC_MCLK_SEL_HS400),
2601 host->ioaddr + CORE_VENDOR_SPEC);
2602 /*
2603 * Select HS400 mode using the HC_SELECT_IN from VENDOR SPEC
2604 * register
2605 */
2606 if (msm_host->tuning_done && !msm_host->calibration_done) {
2607 /*
2608 * Write 0x6 to HC_SELECT_IN and 1 to HC_SELECT_IN_EN
2609 * field in VENDOR_SPEC_FUNC
2610 */
2611 writel_relaxed((readl_relaxed(host->ioaddr + \
2612 CORE_VENDOR_SPEC)
2613 | CORE_HC_SELECT_IN_HS400
2614 | CORE_HC_SELECT_IN_EN),
2615 host->ioaddr + CORE_VENDOR_SPEC);
2616 }
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002617 if (!host->mmc->ios.old_rate && !msm_host->use_cdclp533) {
2618 /*
2619 * Poll on DLL_LOCK and DDR_DLL_LOCK bits in
2620 * CORE_DLL_STATUS to be set. This should get set
2621 * with in 15 us at 200 MHz.
2622 */
2623 rc = readl_poll_timeout(host->ioaddr + CORE_DLL_STATUS,
2624 dll_lock, (dll_lock & (CORE_DLL_LOCK |
2625 CORE_DDR_DLL_LOCK)), 10, 1000);
2626 if (rc == -ETIMEDOUT)
2627 pr_err("%s: Unable to get DLL_LOCK/DDR_DLL_LOCK, dll_status: 0x%08x\n",
2628 mmc_hostname(host->mmc),
2629 dll_lock);
2630 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002631 } else {
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002632 if (!msm_host->use_cdclp533)
2633 /* set CORE_PWRSAVE_DLL bit in CORE_VENDOR_SPEC3 */
2634 writel_relaxed((readl_relaxed(host->ioaddr +
2635 CORE_VENDOR_SPEC3) & ~CORE_PWRSAVE_DLL),
2636 host->ioaddr + CORE_VENDOR_SPEC3);
2637
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002638 /* Select the default clock (free running MCLK) */
2639 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2640 & ~CORE_HC_MCLK_SEL_MASK)
2641 | CORE_HC_MCLK_SEL_DFLT),
2642 host->ioaddr + CORE_VENDOR_SPEC);
2643
2644 /*
2645 * Disable HC_SELECT_IN to be able to use the UHS mode select
2646 * configuration from Host Control2 register for all other
2647 * modes.
2648 *
2649 * Write 0 to HC_SELECT_IN and HC_SELECT_IN_EN field
2650 * in VENDOR_SPEC_FUNC
2651 */
2652 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2653 & ~CORE_HC_SELECT_IN_EN
2654 & ~CORE_HC_SELECT_IN_MASK),
2655 host->ioaddr + CORE_VENDOR_SPEC);
2656 }
2657 mb();
2658
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302659 if (sup_clock != msm_host->clk_rate) {
2660 pr_debug("%s: %s: setting clk rate to %u\n",
2661 mmc_hostname(host->mmc), __func__, sup_clock);
2662 rc = clk_set_rate(msm_host->clk, sup_clock);
2663 if (rc) {
2664 pr_err("%s: %s: Failed to set rate %u for host-clk : %d\n",
2665 mmc_hostname(host->mmc), __func__,
2666 sup_clock, rc);
2667 goto out;
2668 }
2669 msm_host->clk_rate = sup_clock;
2670 host->clock = clock;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302671 /*
2672 * Update the bus vote in case of frequency change due to
2673 * clock scaling.
2674 */
2675 sdhci_msm_bus_voting(host, 1);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302676 }
2677out:
2678 sdhci_set_clock(host, clock);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302679}
2680
Sahitya Tummala14613432013-03-21 11:13:25 +05302681static void sdhci_msm_set_uhs_signaling(struct sdhci_host *host,
2682 unsigned int uhs)
2683{
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002684 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2685 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala14613432013-03-21 11:13:25 +05302686 u16 ctrl_2;
2687
2688 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2689 /* Select Bus Speed Mode for host */
2690 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Venkat Gopalakrishnan0a29da92015-01-09 12:19:16 -08002691 if ((uhs == MMC_TIMING_MMC_HS400) ||
2692 (uhs == MMC_TIMING_MMC_HS200) ||
2693 (uhs == MMC_TIMING_UHS_SDR104))
Sahitya Tummala14613432013-03-21 11:13:25 +05302694 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2695 else if (uhs == MMC_TIMING_UHS_SDR12)
2696 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
2697 else if (uhs == MMC_TIMING_UHS_SDR25)
2698 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
2699 else if (uhs == MMC_TIMING_UHS_SDR50)
2700 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
Venkat Gopalakrishnan0a29da92015-01-09 12:19:16 -08002701 else if ((uhs == MMC_TIMING_UHS_DDR50) ||
2702 (uhs == MMC_TIMING_MMC_DDR52))
Sahitya Tummala14613432013-03-21 11:13:25 +05302703 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302704 /*
2705 * When clock frquency is less than 100MHz, the feedback clock must be
2706 * provided and DLL must not be used so that tuning can be skipped. To
2707 * provide feedback clock, the mode selection can be any value less
2708 * than 3'b011 in bits [2:0] of HOST CONTROL2 register.
2709 */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002710 if (host->clock <= CORE_FREQ_100MHZ) {
2711 if ((uhs == MMC_TIMING_MMC_HS400) ||
2712 (uhs == MMC_TIMING_MMC_HS200) ||
2713 (uhs == MMC_TIMING_UHS_SDR104))
2714 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302715
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002716 /*
2717 * Make sure DLL is disabled when not required
2718 *
2719 * Write 1 to DLL_RST bit of DLL_CONFIG register
2720 */
2721 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
2722 | CORE_DLL_RST),
2723 host->ioaddr + CORE_DLL_CONFIG);
2724
2725 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
2726 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
2727 | CORE_DLL_PDN),
2728 host->ioaddr + CORE_DLL_CONFIG);
2729 mb();
2730
2731 /*
2732 * The DLL needs to be restored and CDCLP533 recalibrated
2733 * when the clock frequency is set back to 400MHz.
2734 */
2735 msm_host->calibration_done = false;
2736 }
2737
2738 pr_debug("%s: %s-clock:%u uhs mode:%u ctrl_2:0x%x\n",
2739 mmc_hostname(host->mmc), __func__, host->clock, uhs, ctrl_2);
Sahitya Tummala14613432013-03-21 11:13:25 +05302740 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
2741
2742}
2743
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302744#define MAX_TEST_BUS 20
2745
2746void sdhci_msm_dump_vendor_regs(struct sdhci_host *host)
2747{
2748 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2749 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2750 int tbsel, tbsel2;
2751 int i, index = 0;
2752 u32 test_bus_val = 0;
2753 u32 debug_reg[MAX_TEST_BUS] = {0};
2754
2755 pr_info("----------- VENDOR REGISTER DUMP -----------\n");
2756 pr_info("Data cnt: 0x%08x | Fifo cnt: 0x%08x | Int sts: 0x%08x\n",
2757 readl_relaxed(msm_host->core_mem + CORE_MCI_DATA_CNT),
2758 readl_relaxed(msm_host->core_mem + CORE_MCI_FIFO_CNT),
2759 readl_relaxed(msm_host->core_mem + CORE_MCI_STATUS));
2760 pr_info("DLL cfg: 0x%08x | DLL sts: 0x%08x | SDCC ver: 0x%08x\n",
2761 readl_relaxed(host->ioaddr + CORE_DLL_CONFIG),
2762 readl_relaxed(host->ioaddr + CORE_DLL_STATUS),
2763 readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION));
2764 pr_info("Vndr func: 0x%08x | Vndr adma err : addr0: 0x%08x addr1: 0x%08x\n",
2765 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC),
2766 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_ADMA_ERR_ADDR0),
2767 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_ADMA_ERR_ADDR1));
2768
2769 /*
2770 * tbsel indicates [2:0] bits and tbsel2 indicates [7:4] bits
2771 * of CORE_TESTBUS_CONFIG register.
2772 *
2773 * To select test bus 0 to 7 use tbsel and to select any test bus
2774 * above 7 use (tbsel2 | tbsel) to get the test bus number. For eg,
2775 * to select test bus 14, write 0x1E to CORE_TESTBUS_CONFIG register
2776 * i.e., tbsel2[7:4] = 0001, tbsel[2:0] = 110.
2777 */
2778 for (tbsel2 = 0; tbsel2 < 3; tbsel2++) {
2779 for (tbsel = 0; tbsel < 8; tbsel++) {
2780 if (index >= MAX_TEST_BUS)
2781 break;
2782 test_bus_val = (tbsel2 << CORE_TESTBUS_SEL2_BIT) |
2783 tbsel | CORE_TESTBUS_ENA;
2784 writel_relaxed(test_bus_val,
2785 msm_host->core_mem + CORE_TESTBUS_CONFIG);
2786 debug_reg[index++] = readl_relaxed(msm_host->core_mem +
2787 CORE_SDCC_DEBUG_REG);
2788 }
2789 }
2790 for (i = 0; i < MAX_TEST_BUS; i = i + 4)
2791 pr_info(" Test bus[%d to %d]: 0x%08x 0x%08x 0x%08x 0x%08x\n",
2792 i, i + 3, debug_reg[i], debug_reg[i+1],
2793 debug_reg[i+2], debug_reg[i+3]);
2794 /* Disable test bus */
2795 writel_relaxed(~CORE_TESTBUS_ENA, msm_host->core_mem +
2796 CORE_TESTBUS_CONFIG);
2797}
2798
Asutosh Das0ef24812012-12-18 16:14:02 +05302799static struct sdhci_ops sdhci_msm_ops = {
Sahitya Tummala14613432013-03-21 11:13:25 +05302800 .set_uhs_signaling = sdhci_msm_set_uhs_signaling,
Asutosh Das0ef24812012-12-18 16:14:02 +05302801 .check_power_status = sdhci_msm_check_power_status,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002802 .platform_execute_tuning = sdhci_msm_execute_tuning,
2803 .toggle_cdr = sdhci_msm_toggle_cdr,
Asutosh Das648f9d12013-01-10 21:11:04 +05302804 .get_max_segments = sdhci_msm_max_segs,
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302805 .set_clock = sdhci_msm_set_clock,
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302806 .get_min_clock = sdhci_msm_get_min_clock,
2807 .get_max_clock = sdhci_msm_get_max_clock,
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302808 .dump_vendor_regs = sdhci_msm_dump_vendor_regs,
Asutosh Dase5e9ca62013-07-30 19:08:36 +05302809 .config_auto_tuning_cmd = sdhci_msm_config_auto_tuning_cmd,
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302810 .enable_controller_clock = sdhci_msm_enable_controller_clock,
Venkat Gopalakrishnanb8cb7072015-01-09 11:04:34 -08002811 .set_bus_width = sdhci_set_bus_width,
Venkat Gopalakrishnan411df072015-01-09 11:09:44 -08002812 .reset = sdhci_reset,
Asutosh Das0ef24812012-12-18 16:14:02 +05302813};
2814
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302815static void sdhci_set_default_hw_caps(struct sdhci_msm_host *msm_host,
2816 struct sdhci_host *host)
2817{
2818 u32 version, caps;
2819 u16 minor;
2820 u8 major;
2821
2822 version = readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION);
2823 major = (version & CORE_VERSION_MAJOR_MASK) >>
2824 CORE_VERSION_MAJOR_SHIFT;
2825 minor = version & CORE_VERSION_TARGET_MASK;
2826
2827 /*
2828 * Starting with SDCC 5 controller (core major version = 1)
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002829 * controller won't advertise 3.0v, 1.8v and 8-bit features
2830 * except for some targets.
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302831 */
2832 if (major >= 1 && minor != 0x11 && minor != 0x12) {
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002833 struct sdhci_msm_reg_data *vdd_io_reg;
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302834 caps = CORE_3_0V_SUPPORT;
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002835 /*
2836 * Enable 1.8V support capability on controllers that
2837 * support dual voltage
2838 */
2839 vdd_io_reg = msm_host->pdata->vreg_data->vdd_io_data;
2840 if (vdd_io_reg &&
2841 (vdd_io_reg->low_vol_level != vdd_io_reg->high_vol_level))
2842 caps |= CORE_1_8V_SUPPORT;
Pratibhasagar Vada47992013-12-09 20:42:32 +05302843 if (msm_host->pdata->mmc_bus_width == MMC_CAP_8_BIT_DATA)
2844 caps |= CORE_8_BIT_SUPPORT;
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302845 writel_relaxed(
2846 (readl_relaxed(host->ioaddr + SDHCI_CAPABILITIES) |
2847 caps), host->ioaddr + CORE_VENDOR_SPEC_CAPABILITIES0);
2848 }
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002849
2850 /*
2851 * SDCC 5 controller with major version 1, minor version 0x34 and later
2852 * with HS 400 mode support will use CM DLL instead of CDC LP 533 DLL.
2853 */
2854 if ((major == 1) && (minor < 0x34))
2855 msm_host->use_cdclp533 = true;
Gilad Broner2a10ca02014-10-02 17:20:35 +03002856
2857 /*
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -08002858 * SDCC 5 controller with major version 1, minor version 0x42 and later
2859 * will require additional steps when resetting DLL.
2860 */
2861 if ((major == 1) && (minor >= 0x42))
2862 msm_host->use_updated_dll_reset = true;
2863
2864 /*
Gilad Broner2a10ca02014-10-02 17:20:35 +03002865 * Mask 64-bit support for controller with 32-bit address bus so that
2866 * smaller descriptor size will be used and improve memory consumption.
2867 * In case bus addressing ever changes, controller version should be
2868 * used in order to decide whether or not to mask 64-bit support.
2869 */
2870 caps = readl_relaxed(host->ioaddr + SDHCI_CAPABILITIES);
2871 caps &= ~CORE_SYS_BUS_SUPPORT_64_BIT;
2872 writel_relaxed(caps, host->ioaddr + CORE_VENDOR_SPEC_CAPABILITIES0);
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302873}
2874
Asutosh Das0ef24812012-12-18 16:14:02 +05302875static int sdhci_msm_probe(struct platform_device *pdev)
2876{
2877 struct sdhci_host *host;
2878 struct sdhci_pltfm_host *pltfm_host;
2879 struct sdhci_msm_host *msm_host;
2880 struct resource *core_memres = NULL;
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02002881 int ret = 0, dead = 0;
Stephen Boyd8dce5c62013-04-24 14:19:46 -07002882 u16 host_version;
Subhash Jadavani28137342013-05-14 17:46:43 +05302883 u32 pwr, irq_status, irq_ctl;
Asutosh Das0ef24812012-12-18 16:14:02 +05302884
2885 pr_debug("%s: Enter %s\n", dev_name(&pdev->dev), __func__);
2886 msm_host = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_msm_host),
2887 GFP_KERNEL);
2888 if (!msm_host) {
2889 ret = -ENOMEM;
2890 goto out;
2891 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302892
2893 msm_host->sdhci_msm_pdata.ops = &sdhci_msm_ops;
2894 host = sdhci_pltfm_init(pdev, &msm_host->sdhci_msm_pdata, 0);
2895 if (IS_ERR(host)) {
2896 ret = PTR_ERR(host);
2897 goto out;
2898 }
2899
2900 pltfm_host = sdhci_priv(host);
2901 pltfm_host->priv = msm_host;
2902 msm_host->mmc = host->mmc;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302903 msm_host->pdev = pdev;
Asutosh Das0ef24812012-12-18 16:14:02 +05302904
2905 /* Extract platform data */
2906 if (pdev->dev.of_node) {
Venkat Gopalakrishnan270580a2013-03-11 12:17:57 -07002907 ret = of_alias_get_id(pdev->dev.of_node, "sdhc");
2908 if (ret < 0) {
2909 dev_err(&pdev->dev, "Failed to get slot index %d\n",
2910 ret);
2911 goto pltfm_free;
2912 }
2913 if (disable_slots & (1 << (ret - 1))) {
2914 dev_info(&pdev->dev, "%s: Slot %d disabled\n", __func__,
2915 ret);
2916 ret = -ENODEV;
2917 goto pltfm_free;
2918 }
2919
Asutosh Das0ef24812012-12-18 16:14:02 +05302920 msm_host->pdata = sdhci_msm_populate_pdata(&pdev->dev);
2921 if (!msm_host->pdata) {
2922 dev_err(&pdev->dev, "DT parsing error\n");
2923 goto pltfm_free;
2924 }
2925 } else {
2926 dev_err(&pdev->dev, "No device tree node\n");
2927 goto pltfm_free;
2928 }
2929
2930 /* Setup Clocks */
2931
2932 /* Setup SDCC bus voter clock. */
2933 msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus_clk");
2934 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
2935 /* Vote for max. clk rate for max. performance */
2936 ret = clk_set_rate(msm_host->bus_clk, INT_MAX);
2937 if (ret)
2938 goto pltfm_free;
2939 ret = clk_prepare_enable(msm_host->bus_clk);
2940 if (ret)
2941 goto pltfm_free;
2942 }
2943
2944 /* Setup main peripheral bus clock */
2945 msm_host->pclk = devm_clk_get(&pdev->dev, "iface_clk");
2946 if (!IS_ERR(msm_host->pclk)) {
2947 ret = clk_prepare_enable(msm_host->pclk);
2948 if (ret)
2949 goto bus_clk_disable;
2950 }
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302951 atomic_set(&msm_host->controller_clock, 1);
Asutosh Das0ef24812012-12-18 16:14:02 +05302952
2953 /* Setup SDC MMC clock */
2954 msm_host->clk = devm_clk_get(&pdev->dev, "core_clk");
2955 if (IS_ERR(msm_host->clk)) {
2956 ret = PTR_ERR(msm_host->clk);
2957 goto pclk_disable;
2958 }
2959
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302960 /* Set to the minimum supported clock frequency */
2961 ret = clk_set_rate(msm_host->clk, sdhci_msm_get_min_clock(host));
2962 if (ret) {
2963 dev_err(&pdev->dev, "MClk rate set failed (%d)\n", ret);
Sahitya Tummala020ede0d2013-06-07 13:03:07 +05302964 goto pclk_disable;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302965 }
Sahitya Tummala020ede0d2013-06-07 13:03:07 +05302966 ret = clk_prepare_enable(msm_host->clk);
2967 if (ret)
2968 goto pclk_disable;
2969
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302970 msm_host->clk_rate = sdhci_msm_get_min_clock(host);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302971 atomic_set(&msm_host->clks_on, 1);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302972
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002973 /* Setup CDC calibration fixed feedback clock */
2974 msm_host->ff_clk = devm_clk_get(&pdev->dev, "cal_clk");
2975 if (!IS_ERR(msm_host->ff_clk)) {
2976 ret = clk_prepare_enable(msm_host->ff_clk);
2977 if (ret)
2978 goto clk_disable;
2979 }
2980
2981 /* Setup CDC calibration sleep clock */
2982 msm_host->sleep_clk = devm_clk_get(&pdev->dev, "sleep_clk");
2983 if (!IS_ERR(msm_host->sleep_clk)) {
2984 ret = clk_prepare_enable(msm_host->sleep_clk);
2985 if (ret)
2986 goto ff_clk_disable;
2987 }
2988
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07002989 msm_host->saved_tuning_phase = INVALID_TUNING_PHASE;
2990
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302991 ret = sdhci_msm_bus_register(msm_host, pdev);
2992 if (ret)
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002993 goto sleep_clk_disable;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302994
2995 if (msm_host->msm_bus_vote.client_handle)
2996 INIT_DELAYED_WORK(&msm_host->msm_bus_vote.vote_work,
2997 sdhci_msm_bus_work);
2998 sdhci_msm_bus_voting(host, 1);
2999
Asutosh Das0ef24812012-12-18 16:14:02 +05303000 /* Setup regulators */
3001 ret = sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, true);
3002 if (ret) {
3003 dev_err(&pdev->dev, "Regulator setup failed (%d)\n", ret);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05303004 goto bus_unregister;
Asutosh Das0ef24812012-12-18 16:14:02 +05303005 }
3006
3007 /* Reset the core and Enable SDHC mode */
3008 core_memres = platform_get_resource_byname(pdev,
3009 IORESOURCE_MEM, "core_mem");
Asutosh Das890bdee2014-08-08 23:01:42 +05303010 if (!core_memres) {
3011 dev_err(&pdev->dev, "Failed to get iomem resource\n");
3012 goto vreg_deinit;
3013 }
Asutosh Das0ef24812012-12-18 16:14:02 +05303014 msm_host->core_mem = devm_ioremap(&pdev->dev, core_memres->start,
3015 resource_size(core_memres));
3016
3017 if (!msm_host->core_mem) {
3018 dev_err(&pdev->dev, "Failed to remap registers\n");
3019 ret = -ENOMEM;
3020 goto vreg_deinit;
3021 }
3022
Stepan Moskovchenkoa4d0fa72013-09-13 22:19:33 -07003023 /* Unset HC_MODE_EN bit in HC_MODE register */
3024 writel_relaxed(0, (msm_host->core_mem + CORE_HC_MODE));
3025
Asutosh Das0ef24812012-12-18 16:14:02 +05303026 /* Set SW_RST bit in POWER register (Offset 0x0) */
Sahitya Tummala66b0fe32013-04-25 11:50:56 +05303027 writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_POWER) |
3028 CORE_SW_RST, msm_host->core_mem + CORE_POWER);
3029 /*
3030 * SW reset can take upto 10HCLK + 15MCLK cycles.
3031 * Calculating based on min clk rates (hclk = 27MHz,
3032 * mclk = 400KHz) it comes to ~40us. Let's poll for
3033 * max. 1ms for reset completion.
3034 */
3035 ret = readl_poll_timeout(msm_host->core_mem + CORE_POWER,
Matt Wagantallc7097a22014-04-29 15:48:15 -07003036 pwr, !(pwr & CORE_SW_RST), 10, 1000);
Sahitya Tummala66b0fe32013-04-25 11:50:56 +05303037
3038 if (ret) {
3039 dev_err(&pdev->dev, "reset failed (%d)\n", ret);
3040 goto vreg_deinit;
3041 }
Asutosh Das0ef24812012-12-18 16:14:02 +05303042 /* Set HC_MODE_EN bit in HC_MODE register */
3043 writel_relaxed(HC_MODE_EN, (msm_host->core_mem + CORE_HC_MODE));
3044
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07003045 /* Set FF_CLK_SW_RST_DIS bit in HC_MODE register */
3046 writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_HC_MODE) |
3047 FF_CLK_SW_RST_DIS, msm_host->core_mem + CORE_HC_MODE);
3048
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05303049 sdhci_set_default_hw_caps(msm_host, host);
Asutosh Das0ef24812012-12-18 16:14:02 +05303050 /*
Subhash Jadavani28137342013-05-14 17:46:43 +05303051 * CORE_SW_RST above may trigger power irq if previous status of PWRCTL
3052 * was either BUS_ON or IO_HIGH_V. So before we enable the power irq
3053 * interrupt in GIC (by registering the interrupt handler), we need to
3054 * ensure that any pending power irq interrupt status is acknowledged
3055 * otherwise power irq interrupt handler would be fired prematurely.
3056 */
3057 irq_status = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
3058 writel_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
3059 irq_ctl = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_CTL);
3060 if (irq_status & (CORE_PWRCTL_BUS_ON | CORE_PWRCTL_BUS_OFF))
3061 irq_ctl |= CORE_PWRCTL_BUS_SUCCESS;
3062 if (irq_status & (CORE_PWRCTL_IO_HIGH | CORE_PWRCTL_IO_LOW))
3063 irq_ctl |= CORE_PWRCTL_IO_SUCCESS;
3064 writel_relaxed(irq_ctl, (msm_host->core_mem + CORE_PWRCTL_CTL));
3065 /*
3066 * Ensure that above writes are propogated before interrupt enablement
3067 * in GIC.
3068 */
3069 mb();
3070
3071 /*
Asutosh Das0ef24812012-12-18 16:14:02 +05303072 * Following are the deviations from SDHC spec v3.0 -
3073 * 1. Card detection is handled using separate GPIO.
3074 * 2. Bus power control is handled by interacting with PMIC.
3075 */
3076 host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
3077 host->quirks |= SDHCI_QUIRK_SINGLE_POWER_WRITE;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05303078 host->quirks |= SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN;
3079 host->quirks2 |= SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK;
Sahitya Tummala87d43942013-04-12 11:49:11 +05303080 host->quirks2 |= SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD;
Sahitya Tummala314162c2013-04-12 12:11:20 +05303081 host->quirks2 |= SDHCI_QUIRK2_BROKEN_PRESET_VALUE;
Sahitya Tummala7c9780d2013-04-12 11:59:25 +05303082 host->quirks2 |= SDHCI_QUIRK2_USE_RESERVED_MAX_TIMEOUT;
Asutosh Das0ef24812012-12-18 16:14:02 +05303083
Sahitya Tummalaa5733ab52013-06-10 16:32:51 +05303084 if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK)
3085 host->quirks2 |= SDHCI_QUIRK2_DIVIDE_TOUT_BY_4;
3086
Stephen Boyd8dce5c62013-04-24 14:19:46 -07003087 host_version = readw_relaxed((host->ioaddr + SDHCI_HOST_VERSION));
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07003088 dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n",
3089 host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >>
3090 SDHCI_VENDOR_VER_SHIFT));
3091 if (((host_version & SDHCI_VENDOR_VER_MASK) >>
3092 SDHCI_VENDOR_VER_SHIFT) == SDHCI_VER_100) {
3093 /*
3094 * Add 40us delay in interrupt handler when
3095 * operating at initialization frequency(400KHz).
3096 */
3097 host->quirks2 |= SDHCI_QUIRK2_SLOW_INT_CLR;
3098 /*
3099 * Set Software Reset for DAT line in Software
3100 * Reset Register (Bit 2).
3101 */
3102 host->quirks2 |= SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT;
3103 }
3104
Asutosh Das214b9662013-06-13 14:27:42 +05303105 host->quirks2 |= SDHCI_QUIRK2_IGN_DATA_END_BIT_ERROR;
3106
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07003107 /* Setup PWRCTL irq */
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02003108 msm_host->pwr_irq = platform_get_irq_byname(pdev, "pwr_irq");
3109 if (msm_host->pwr_irq < 0) {
Asutosh Das0ef24812012-12-18 16:14:02 +05303110 dev_err(&pdev->dev, "Failed to get pwr_irq by name (%d)\n",
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02003111 msm_host->pwr_irq);
Asutosh Das0ef24812012-12-18 16:14:02 +05303112 goto vreg_deinit;
3113 }
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02003114 ret = devm_request_threaded_irq(&pdev->dev, msm_host->pwr_irq, NULL,
Asutosh Das0ef24812012-12-18 16:14:02 +05303115 sdhci_msm_pwr_irq, IRQF_ONESHOT,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07003116 dev_name(&pdev->dev), host);
Asutosh Das0ef24812012-12-18 16:14:02 +05303117 if (ret) {
3118 dev_err(&pdev->dev, "Request threaded irq(%d) failed (%d)\n",
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02003119 msm_host->pwr_irq, ret);
Asutosh Das0ef24812012-12-18 16:14:02 +05303120 goto vreg_deinit;
3121 }
3122
3123 /* Enable pwr irq interrupts */
3124 writel_relaxed(INT_MASK, (msm_host->core_mem + CORE_PWRCTL_MASK));
3125
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05303126#ifdef CONFIG_MMC_CLKGATE
3127 /* Set clock gating delay to be used when CONFIG_MMC_CLKGATE is set */
3128 msm_host->mmc->clkgate_delay = SDHCI_MSM_MMC_CLK_GATE_DELAY;
3129#endif
3130
Asutosh Das0ef24812012-12-18 16:14:02 +05303131 /* Set host capabilities */
3132 msm_host->mmc->caps |= msm_host->pdata->mmc_bus_width;
3133 msm_host->mmc->caps |= msm_host->pdata->caps;
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003134 msm_host->mmc->caps |= MMC_CAP_AGGRESSIVE_PM;
Asutosh Das0ef24812012-12-18 16:14:02 +05303135 msm_host->mmc->caps2 |= msm_host->pdata->caps2;
Venkat Gopalakrishnan97c16112014-12-16 18:26:25 -08003136 msm_host->mmc->caps2 |= MMC_CAP2_BOOTPART_NOACC;
3137 msm_host->mmc->caps2 |= MMC_CAP2_FULL_PWR_CYCLE;
Subhash Jadavani6d472b22013-05-29 15:52:10 +05303138 msm_host->mmc->caps2 |= MMC_CAP2_ASYNC_SDIO_IRQ_4BIT_MODE;
Venkat Gopalakrishnan97c16112014-12-16 18:26:25 -08003139 msm_host->mmc->caps2 |= MMC_CAP2_HS400_POST_TUNING;
Asutosh Dasbea115d2013-06-24 18:20:45 +05303140 msm_host->mmc->pm_caps |= MMC_PM_KEEP_POWER;
Asutosh Das0ef24812012-12-18 16:14:02 +05303141
3142 if (msm_host->pdata->nonremovable)
3143 msm_host->mmc->caps |= MMC_CAP_NONREMOVABLE;
3144
Guoping Yuf7c91332014-08-20 16:56:18 +08003145 if (msm_host->pdata->nonhotplug)
3146 msm_host->mmc->caps2 |= MMC_CAP2_NONHOTPLUG;
3147
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05303148 host->cpu_dma_latency_us = msm_host->pdata->cpu_dma_latency_us;
Maya Erez994cf2f2014-10-21 20:22:04 +03003149 host->pm_qos_req_dma.type = msm_host->pdata->cpu_affinity_type;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05303150
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05303151 init_completion(&msm_host->pwr_irq_completion);
3152
Sahitya Tummala581df132013-03-12 14:57:46 +05303153 if (gpio_is_valid(msm_host->pdata->status_gpio)) {
Sahitya Tummala6ddabb42014-06-05 13:26:55 +05303154 /*
3155 * Set up the card detect GPIO in active configuration before
3156 * configuring it as an IRQ. Otherwise, it can be in some
3157 * weird/inconsistent state resulting in flood of interrupts.
3158 */
3159 sdhci_msm_setup_pins(msm_host->pdata, true);
3160
Sahitya Tummala581df132013-03-12 14:57:46 +05303161 ret = mmc_gpio_request_cd(msm_host->mmc,
3162 msm_host->pdata->status_gpio, 0);
3163 if (ret) {
3164 dev_err(&pdev->dev, "%s: Failed to request card detection IRQ %d\n",
3165 __func__, ret);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05303166 goto vreg_deinit;
Sahitya Tummala581df132013-03-12 14:57:46 +05303167 }
3168 }
3169
Krishna Konda7feab352013-09-17 23:55:40 -07003170 if ((sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT) &&
3171 (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(64)))) {
3172 host->dma_mask = DMA_BIT_MASK(64);
3173 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
3174 } else if (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(32))) {
Sahitya Tummalaeaa21862013-03-20 19:34:59 +05303175 host->dma_mask = DMA_BIT_MASK(32);
3176 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
3177 } else {
3178 dev_err(&pdev->dev, "%s: Failed to set dma mask\n", __func__);
3179 }
3180
Asutosh Das0ef24812012-12-18 16:14:02 +05303181 ret = sdhci_add_host(host);
3182 if (ret) {
3183 dev_err(&pdev->dev, "Add host failed (%d)\n", ret);
Sahitya Tummala581df132013-03-12 14:57:46 +05303184 goto vreg_deinit;
Asutosh Das0ef24812012-12-18 16:14:02 +05303185 }
3186
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003187 pm_runtime_set_active(&pdev->dev);
3188 pm_runtime_enable(&pdev->dev);
3189 pm_runtime_set_autosuspend_delay(&pdev->dev, MSM_AUTOSUSPEND_DELAY_MS);
3190 pm_runtime_use_autosuspend(&pdev->dev);
3191
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05303192 msm_host->msm_bus_vote.max_bus_bw.show = show_sdhci_max_bus_bw;
3193 msm_host->msm_bus_vote.max_bus_bw.store = store_sdhci_max_bus_bw;
3194 sysfs_attr_init(&msm_host->msm_bus_vote.max_bus_bw.attr);
3195 msm_host->msm_bus_vote.max_bus_bw.attr.name = "max_bus_bw";
3196 msm_host->msm_bus_vote.max_bus_bw.attr.mode = S_IRUGO | S_IWUSR;
3197 ret = device_create_file(&pdev->dev,
3198 &msm_host->msm_bus_vote.max_bus_bw);
3199 if (ret)
3200 goto remove_host;
3201
Sahitya Tummala5c55b932013-06-20 14:00:18 +05303202 if (!gpio_is_valid(msm_host->pdata->status_gpio)) {
3203 msm_host->polling.show = show_polling;
3204 msm_host->polling.store = store_polling;
3205 sysfs_attr_init(&msm_host->polling.attr);
3206 msm_host->polling.attr.name = "polling";
3207 msm_host->polling.attr.mode = S_IRUGO | S_IWUSR;
3208 ret = device_create_file(&pdev->dev, &msm_host->polling);
3209 if (ret)
3210 goto remove_max_bus_bw_file;
3211 }
Asutosh Dase5e9ca62013-07-30 19:08:36 +05303212
3213 msm_host->auto_cmd21_attr.show = show_auto_cmd21;
3214 msm_host->auto_cmd21_attr.store = store_auto_cmd21;
3215 sysfs_attr_init(&msm_host->auto_cmd21_attr.attr);
3216 msm_host->auto_cmd21_attr.attr.name = "enable_auto_cmd21";
3217 msm_host->auto_cmd21_attr.attr.mode = S_IRUGO | S_IWUSR;
3218 ret = device_create_file(&pdev->dev, &msm_host->auto_cmd21_attr);
3219 if (ret) {
3220 pr_err("%s: %s: failed creating auto-cmd21 attr: %d\n",
3221 mmc_hostname(host->mmc), __func__, ret);
3222 device_remove_file(&pdev->dev, &msm_host->auto_cmd21_attr);
3223 }
Asutosh Das0ef24812012-12-18 16:14:02 +05303224 /* Successful initialization */
3225 goto out;
3226
Sahitya Tummala5c55b932013-06-20 14:00:18 +05303227remove_max_bus_bw_file:
3228 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Asutosh Das0ef24812012-12-18 16:14:02 +05303229remove_host:
3230 dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003231 pm_runtime_disable(&pdev->dev);
Asutosh Das0ef24812012-12-18 16:14:02 +05303232 sdhci_remove_host(host, dead);
3233vreg_deinit:
3234 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05303235bus_unregister:
3236 if (msm_host->msm_bus_vote.client_handle)
3237 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
3238 sdhci_msm_bus_unregister(msm_host);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07003239sleep_clk_disable:
3240 if (!IS_ERR(msm_host->sleep_clk))
3241 clk_disable_unprepare(msm_host->sleep_clk);
3242ff_clk_disable:
3243 if (!IS_ERR(msm_host->ff_clk))
3244 clk_disable_unprepare(msm_host->ff_clk);
Asutosh Das0ef24812012-12-18 16:14:02 +05303245clk_disable:
3246 if (!IS_ERR(msm_host->clk))
3247 clk_disable_unprepare(msm_host->clk);
3248pclk_disable:
3249 if (!IS_ERR(msm_host->pclk))
3250 clk_disable_unprepare(msm_host->pclk);
3251bus_clk_disable:
3252 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
3253 clk_disable_unprepare(msm_host->bus_clk);
3254pltfm_free:
3255 sdhci_pltfm_free(pdev);
3256out:
3257 pr_debug("%s: Exit %s\n", dev_name(&pdev->dev), __func__);
3258 return ret;
3259}
3260
3261static int sdhci_msm_remove(struct platform_device *pdev)
3262{
3263 struct sdhci_host *host = platform_get_drvdata(pdev);
3264 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3265 struct sdhci_msm_host *msm_host = pltfm_host->priv;
3266 struct sdhci_msm_pltfm_data *pdata = msm_host->pdata;
3267 int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) ==
3268 0xffffffff);
3269
3270 pr_debug("%s: %s\n", dev_name(&pdev->dev), __func__);
Sahitya Tummala5c55b932013-06-20 14:00:18 +05303271 if (!gpio_is_valid(msm_host->pdata->status_gpio))
3272 device_remove_file(&pdev->dev, &msm_host->polling);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05303273 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003274 pm_runtime_disable(&pdev->dev);
Asutosh Das0ef24812012-12-18 16:14:02 +05303275 sdhci_remove_host(host, dead);
3276 sdhci_pltfm_free(pdev);
Sahitya Tummala581df132013-03-12 14:57:46 +05303277
Asutosh Das0ef24812012-12-18 16:14:02 +05303278 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05303279
Pratibhasagar V9acf2642013-11-21 21:07:21 +05303280 sdhci_msm_setup_pins(pdata, true);
3281 sdhci_msm_setup_pins(pdata, false);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05303282
3283 if (msm_host->msm_bus_vote.client_handle) {
3284 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
3285 sdhci_msm_bus_unregister(msm_host);
3286 }
Asutosh Das0ef24812012-12-18 16:14:02 +05303287 return 0;
3288}
3289
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003290#ifdef CONFIG_PM
3291static int sdhci_msm_runtime_suspend(struct device *dev)
3292{
3293 struct sdhci_host *host = dev_get_drvdata(dev);
3294 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3295 struct sdhci_msm_host *msm_host = pltfm_host->priv;
3296
3297 disable_irq(host->irq);
3298 disable_irq(msm_host->pwr_irq);
3299
3300 /*
3301 * Remove the vote immediately only if clocks are off in which
3302 * case we might have queued work to remove vote but it may not
3303 * be completed before runtime suspend or system suspend.
3304 */
3305 if (!atomic_read(&msm_host->clks_on)) {
3306 if (msm_host->msm_bus_vote.client_handle)
3307 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
3308 }
3309
3310 return 0;
3311}
3312
3313static int sdhci_msm_runtime_resume(struct device *dev)
3314{
3315 struct sdhci_host *host = dev_get_drvdata(dev);
3316 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3317 struct sdhci_msm_host *msm_host = pltfm_host->priv;
3318
3319 enable_irq(msm_host->pwr_irq);
3320 enable_irq(host->irq);
3321
3322 return 0;
3323}
3324
3325static int sdhci_msm_suspend(struct device *dev)
3326{
3327 struct sdhci_host *host = dev_get_drvdata(dev);
3328 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3329 struct sdhci_msm_host *msm_host = pltfm_host->priv;
3330
3331 if (gpio_is_valid(msm_host->pdata->status_gpio) &&
3332 (msm_host->mmc->slot.cd_irq >= 0))
3333 disable_irq(msm_host->mmc->slot.cd_irq);
3334
3335 if (pm_runtime_suspended(dev)) {
3336 pr_debug("%s: %s: already runtime suspended\n",
3337 mmc_hostname(host->mmc), __func__);
3338 goto out;
3339 }
3340 return sdhci_msm_runtime_suspend(dev);
3341out:
3342 return 0;
3343}
3344
3345static int sdhci_msm_resume(struct device *dev)
3346{
3347 struct sdhci_host *host = dev_get_drvdata(dev);
3348 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3349 struct sdhci_msm_host *msm_host = pltfm_host->priv;
3350 int ret = 0;
3351
3352 if (gpio_is_valid(msm_host->pdata->status_gpio) &&
3353 (msm_host->mmc->slot.cd_irq >= 0))
3354 enable_irq(msm_host->mmc->slot.cd_irq);
3355
3356 if (pm_runtime_suspended(dev)) {
3357 pr_debug("%s: %s: runtime suspended, defer system resume\n",
3358 mmc_hostname(host->mmc), __func__);
3359 goto out;
3360 }
3361
3362 return sdhci_msm_runtime_resume(dev);
3363out:
3364 return ret;
3365}
3366
3367static const struct dev_pm_ops sdhci_msm_pmops = {
3368 SET_SYSTEM_SLEEP_PM_OPS(sdhci_msm_suspend, sdhci_msm_resume)
3369 SET_RUNTIME_PM_OPS(sdhci_msm_runtime_suspend, sdhci_msm_runtime_resume,
3370 NULL)
3371};
3372
3373#define SDHCI_MSM_PMOPS (&sdhci_msm_pmops)
3374
3375#else
3376#define SDHCI_MSM_PMOPS NULL
3377#endif
Asutosh Das0ef24812012-12-18 16:14:02 +05303378static const struct of_device_id sdhci_msm_dt_match[] = {
3379 {.compatible = "qcom,sdhci-msm"},
3380};
3381MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match);
3382
3383static struct platform_driver sdhci_msm_driver = {
3384 .probe = sdhci_msm_probe,
3385 .remove = sdhci_msm_remove,
3386 .driver = {
3387 .name = "sdhci_msm",
3388 .owner = THIS_MODULE,
3389 .of_match_table = sdhci_msm_dt_match,
Konstantin Dorfman98377d32015-02-25 10:09:41 +02003390 .pm = SDHCI_MSM_PMOPS,
Asutosh Das0ef24812012-12-18 16:14:02 +05303391 },
3392};
3393
3394module_platform_driver(sdhci_msm_driver);
3395
3396MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Secure Digital Host Controller Interface driver");
3397MODULE_LICENSE("GPL v2");