blob: 1c355753c45b0d51298b60d6f941a5a094d59bb8 [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>
Asutosh Das0ef24812012-12-18 16:14:02 +053041
42#include "sdhci-pltfm.h"
43
Venkat Gopalakrishnan0a92d532014-12-16 17:31:00 -080044#define CORE_POWER 0x0
45#define CORE_SW_RST (1 << 7)
46
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -070047#define SDHCI_VER_100 0x2B
Venkat Gopalakrishnan0a92d532014-12-16 17:31:00 -080048#define CORE_MCI_DATA_CNT 0x30
49#define CORE_MCI_STATUS 0x34
50#define CORE_MCI_FIFO_CNT 0x44
51
52#define CORE_VERSION_STEP_MASK 0x0000FFFF
53#define CORE_VERSION_MINOR_MASK 0x0FFF0000
54#define CORE_VERSION_MINOR_SHIFT 16
55#define CORE_VERSION_MAJOR_MASK 0xF0000000
56#define CORE_VERSION_MAJOR_SHIFT 28
57#define CORE_VERSION_TARGET_MASK 0x000000FF
58
59#define CORE_GENERICS 0x70
60#define SWITCHABLE_SIGNALLING_VOL (1 << 29)
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +053061
62#define CORE_VERSION_MAJOR_MASK 0xF0000000
63#define CORE_VERSION_MAJOR_SHIFT 28
64
Asutosh Das0ef24812012-12-18 16:14:02 +053065#define CORE_HC_MODE 0x78
66#define HC_MODE_EN 0x1
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -070067#define FF_CLK_SW_RST_DIS (1 << 13)
Asutosh Das0ef24812012-12-18 16:14:02 +053068
Sahitya Tummala67717bc2013-08-02 09:21:37 +053069#define CORE_MCI_VERSION 0x050
70#define CORE_TESTBUS_CONFIG 0x0CC
71#define CORE_TESTBUS_ENA (1 << 3)
Venkat Gopalakrishnan0a92d532014-12-16 17:31:00 -080072#define CORE_TESTBUS_SEL2_BIT 4
73#define CORE_TESTBUS_SEL2 (1 << CORE_TESTBUS_SEL2_BIT)
Sahitya Tummala67717bc2013-08-02 09:21:37 +053074
Asutosh Das0ef24812012-12-18 16:14:02 +053075#define CORE_PWRCTL_STATUS 0xDC
76#define CORE_PWRCTL_MASK 0xE0
77#define CORE_PWRCTL_CLEAR 0xE4
78#define CORE_PWRCTL_CTL 0xE8
79
80#define CORE_PWRCTL_BUS_OFF 0x01
81#define CORE_PWRCTL_BUS_ON (1 << 1)
82#define CORE_PWRCTL_IO_LOW (1 << 2)
83#define CORE_PWRCTL_IO_HIGH (1 << 3)
84
85#define CORE_PWRCTL_BUS_SUCCESS 0x01
86#define CORE_PWRCTL_BUS_FAIL (1 << 1)
87#define CORE_PWRCTL_IO_SUCCESS (1 << 2)
88#define CORE_PWRCTL_IO_FAIL (1 << 3)
89
90#define INT_MASK 0xF
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -070091#define MAX_PHASES 16
92
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -070093#define CORE_DLL_CONFIG 0x100
94#define CORE_CMD_DAT_TRACK_SEL (1 << 0)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -070095#define CORE_DLL_EN (1 << 16)
96#define CORE_CDR_EN (1 << 17)
97#define CORE_CK_OUT_EN (1 << 18)
98#define CORE_CDR_EXT_EN (1 << 19)
99#define CORE_DLL_PDN (1 << 29)
100#define CORE_DLL_RST (1 << 30)
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700101
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700102#define CORE_DLL_STATUS 0x108
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700103#define CORE_DLL_LOCK (1 << 7)
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700104#define CORE_DDR_DLL_LOCK (1 << 11)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700105
106#define CORE_VENDOR_SPEC 0x10C
107#define CORE_CLK_PWRSAVE (1 << 1)
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700108#define CORE_HC_MCLK_SEL_DFLT (2 << 8)
109#define CORE_HC_MCLK_SEL_HS400 (3 << 8)
110#define CORE_HC_MCLK_SEL_MASK (3 << 8)
Asutosh Dase5e9ca62013-07-30 19:08:36 +0530111#define CORE_HC_AUTO_CMD21_EN (1 << 6)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700112#define CORE_IO_PAD_PWR_SWITCH (1 << 16)
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700113#define CORE_HC_SELECT_IN_EN (1 << 18)
114#define CORE_HC_SELECT_IN_HS400 (6 << 19)
115#define CORE_HC_SELECT_IN_MASK (7 << 19)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700116
Venkat Gopalakrishnan0a92d532014-12-16 17:31:00 -0800117#define CORE_VENDOR_SPEC_ADMA_ERR_ADDR0 0x114
118#define CORE_VENDOR_SPEC_ADMA_ERR_ADDR1 0x118
119
Krishna Konda7feab352013-09-17 23:55:40 -0700120#define CORE_VENDOR_SPEC_CAPABILITIES0 0x11C
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +0530121#define CORE_8_BIT_SUPPORT (1 << 18)
122#define CORE_3_3V_SUPPORT (1 << 24)
123#define CORE_3_0V_SUPPORT (1 << 25)
124#define CORE_1_8V_SUPPORT (1 << 26)
Gilad Broner2a10ca02014-10-02 17:20:35 +0300125#define CORE_SYS_BUS_SUPPORT_64_BIT BIT(28)
Krishna Konda7feab352013-09-17 23:55:40 -0700126
Venkat Gopalakrishnan0a92d532014-12-16 17:31:00 -0800127#define CORE_SDCC_DEBUG_REG 0x124
Sahitya Tummala67717bc2013-08-02 09:21:37 +0530128
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700129#define CORE_CSR_CDC_CTLR_CFG0 0x130
130#define CORE_SW_TRIG_FULL_CALIB (1 << 16)
131#define CORE_HW_AUTOCAL_ENA (1 << 17)
132
133#define CORE_CSR_CDC_CTLR_CFG1 0x134
134#define CORE_CSR_CDC_CAL_TIMER_CFG0 0x138
135#define CORE_TIMER_ENA (1 << 16)
136
137#define CORE_CSR_CDC_CAL_TIMER_CFG1 0x13C
138#define CORE_CSR_CDC_REFCOUNT_CFG 0x140
139#define CORE_CSR_CDC_COARSE_CAL_CFG 0x144
140#define CORE_CDC_OFFSET_CFG 0x14C
141#define CORE_CSR_CDC_DELAY_CFG 0x150
142#define CORE_CDC_SLAVE_DDA_CFG 0x160
143#define CORE_CSR_CDC_STATUS0 0x164
144#define CORE_CALIBRATION_DONE (1 << 0)
145
146#define CORE_CDC_ERROR_CODE_MASK 0x7000000
147
148#define CORE_CSR_CDC_GEN_CFG 0x178
149#define CORE_CDC_SWITCH_BYPASS_OFF (1 << 0)
150#define CORE_CDC_SWITCH_RC_EN (1 << 1)
151
152#define CORE_DDR_200_CFG 0x184
153#define CORE_CDC_T4_DLY_SEL (1 << 0)
154#define CORE_START_CDC_TRAFFIC (1 << 6)
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700155#define CORE_VENDOR_SPEC3 0x1B0
156#define CORE_PWRSAVE_DLL (1 << 3)
157
158#define CORE_DLL_CONFIG_2 0x1B4
159#define CORE_DDR_CAL_EN (1 << 0)
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800160#define CORE_FLL_CYCLE_CNT (1 << 18)
161#define CORE_DLL_CLOCK_DISABLE (1 << 21)
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700162
163#define CORE_DDR_CONFIG 0x1B8
164#define DDR_CONFIG_POR_VAL 0x80040853
165
Venkat Gopalakrishnan450745e2014-07-24 20:39:34 -0700166/* 512 descriptors */
167#define SDHCI_MSM_MAX_SEGMENTS (1 << 9)
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +0530168#define SDHCI_MSM_MMC_CLK_GATE_DELAY 200 /* msecs */
Asutosh Das648f9d12013-01-10 21:11:04 +0530169
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700170#define CORE_FREQ_100MHZ (100 * 1000 * 1000)
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800171#define TCXO_FREQ 19200000
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700172
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700173#define INVALID_TUNING_PHASE -1
174
Krishna Konda96e6b112013-10-28 15:25:03 -0700175#define NUM_TUNING_PHASES 16
176#define MAX_DRV_TYPES_SUPPORTED_HS200 3
177
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700178static const u32 tuning_block_64[] = {
179 0x00FF0FFF, 0xCCC3CCFF, 0xFFCC3CC3, 0xEFFEFFFE,
180 0xDDFFDFFF, 0xFBFFFBFF, 0xFF7FFFBF, 0xEFBDF777,
181 0xF0FFF0FF, 0x3CCCFC0F, 0xCFCC33CC, 0xEEFFEFFF,
182 0xFDFFFDFF, 0xFFBFFFDF, 0xFFF7FFBB, 0xDE7B7FF7
183};
184
185static const u32 tuning_block_128[] = {
186 0xFF00FFFF, 0x0000FFFF, 0xCCCCFFFF, 0xCCCC33CC,
187 0xCC3333CC, 0xFFFFCCCC, 0xFFFFEEFF, 0xFFEEEEFF,
188 0xFFDDFFFF, 0xDDDDFFFF, 0xBBFFFFFF, 0xBBFFFFFF,
189 0xFFFFFFBB, 0xFFFFFF77, 0x77FF7777, 0xFFEEDDBB,
190 0x00FFFFFF, 0x00FFFFFF, 0xCCFFFF00, 0xCC33CCCC,
191 0x3333CCCC, 0xFFCCCCCC, 0xFFEEFFFF, 0xEEEEFFFF,
192 0xDDFFFFFF, 0xDDFFFFFF, 0xFFFFFFDD, 0xFFFFFFBB,
193 0xFFFFBBBB, 0xFFFF77FF, 0xFF7777FF, 0xEEDDBB77
194};
Asutosh Das0ef24812012-12-18 16:14:02 +0530195
Venkat Gopalakrishnan270580a2013-03-11 12:17:57 -0700196static int disable_slots;
197/* root can write, others read */
198module_param(disable_slots, int, S_IRUGO|S_IWUSR);
199
Asutosh Das0ef24812012-12-18 16:14:02 +0530200/* This structure keeps information per regulator */
201struct sdhci_msm_reg_data {
202 /* voltage regulator handle */
203 struct regulator *reg;
204 /* regulator name */
205 const char *name;
206 /* voltage level to be set */
207 u32 low_vol_level;
208 u32 high_vol_level;
209 /* Load values for low power and high power mode */
210 u32 lpm_uA;
211 u32 hpm_uA;
212
213 /* is this regulator enabled? */
214 bool is_enabled;
215 /* is this regulator needs to be always on? */
216 bool is_always_on;
217 /* is low power mode setting required for this regulator? */
218 bool lpm_sup;
219 bool set_voltage_sup;
220};
221
Asutosh Das598a4d42014-02-05 16:38:23 +0530222#define MSM_MMC_DEFAULT_CPU_DMA_LATENCY 200 /* usecs */
Asutosh Das0ef24812012-12-18 16:14:02 +0530223/*
224 * This structure keeps information for all the
225 * regulators required for a SDCC slot.
226 */
227struct sdhci_msm_slot_reg_data {
228 /* keeps VDD/VCC regulator info */
229 struct sdhci_msm_reg_data *vdd_data;
230 /* keeps VDD IO regulator info */
231 struct sdhci_msm_reg_data *vdd_io_data;
232};
233
234struct sdhci_msm_gpio {
235 u32 no;
236 const char *name;
237 bool is_enabled;
238};
239
240struct sdhci_msm_gpio_data {
241 struct sdhci_msm_gpio *gpio;
242 u8 size;
243};
244
245struct sdhci_msm_pin_data {
246 /*
247 * = 1 if controller pins are using gpios
248 * = 0 if controller has dedicated MSM pads
249 */
Sahitya Tummala1cd7e072014-02-14 13:19:01 +0530250 u8 is_gpio;
Asutosh Das0ef24812012-12-18 16:14:02 +0530251 struct sdhci_msm_gpio_data *gpio_data;
252};
253
Pratibhasagar V9acf2642013-11-21 21:07:21 +0530254struct sdhci_pinctrl_data {
255 struct pinctrl *pctrl;
256 struct pinctrl_state *pins_active;
257 struct pinctrl_state *pins_sleep;
258};
259
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530260struct sdhci_msm_bus_voting_data {
261 struct msm_bus_scale_pdata *bus_pdata;
262 unsigned int *bw_vecs;
263 unsigned int bw_vecs_size;
264};
265
Asutosh Das0ef24812012-12-18 16:14:02 +0530266struct sdhci_msm_pltfm_data {
267 /* Supported UHS-I Modes */
268 u32 caps;
269
270 /* More capabilities */
271 u32 caps2;
272
273 unsigned long mmc_bus_width;
Asutosh Das0ef24812012-12-18 16:14:02 +0530274 struct sdhci_msm_slot_reg_data *vreg_data;
275 bool nonremovable;
Guoping Yuf7c91332014-08-20 16:56:18 +0800276 bool nonhotplug;
Pratibhasagar V9acf2642013-11-21 21:07:21 +0530277 bool pin_cfg_sts;
Asutosh Das0ef24812012-12-18 16:14:02 +0530278 struct sdhci_msm_pin_data *pin_data;
Pratibhasagar V9acf2642013-11-21 21:07:21 +0530279 struct sdhci_pinctrl_data *pctrl_data;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +0530280 u32 cpu_dma_latency_us;
Sahitya Tummala581df132013-03-12 14:57:46 +0530281 int status_gpio; /* card detection GPIO that is configured as IRQ */
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530282 struct sdhci_msm_bus_voting_data *voting_data;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530283 u32 *sup_clk_table;
284 unsigned char sup_clk_cnt;
Maya Erez994cf2f2014-10-21 20:22:04 +0300285 enum pm_qos_req_type cpu_affinity_type;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530286};
287
288struct sdhci_msm_bus_vote {
289 uint32_t client_handle;
290 uint32_t curr_vote;
291 int min_bw_vote;
292 int max_bw_vote;
293 bool is_max_bw_needed;
294 struct delayed_work vote_work;
295 struct device_attribute max_bus_bw;
Asutosh Das0ef24812012-12-18 16:14:02 +0530296};
297
298struct sdhci_msm_host {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530299 struct platform_device *pdev;
Asutosh Das0ef24812012-12-18 16:14:02 +0530300 void __iomem *core_mem; /* MSM SDCC mapped address */
301 struct clk *clk; /* main SD/MMC bus clock */
302 struct clk *pclk; /* SDHC peripheral bus clock */
303 struct clk *bus_clk; /* SDHC bus voter clock */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700304 struct clk *ff_clk; /* CDC calibration fixed feedback clock */
305 struct clk *sleep_clk; /* CDC calibration sleep clock */
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +0530306 atomic_t clks_on; /* Set if clocks are enabled */
Asutosh Das0ef24812012-12-18 16:14:02 +0530307 struct sdhci_msm_pltfm_data *pdata;
308 struct mmc_host *mmc;
309 struct sdhci_pltfm_data sdhci_msm_pdata;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +0530310 u32 curr_pwr_state;
311 u32 curr_io_level;
312 struct completion pwr_irq_completion;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530313 struct sdhci_msm_bus_vote msm_bus_vote;
Sahitya Tummala5c55b932013-06-20 14:00:18 +0530314 struct device_attribute polling;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530315 u32 clk_rate; /* Keeps track of current clock rate that is set */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700316 bool tuning_done;
317 bool calibration_done;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700318 u8 saved_tuning_phase;
Asutosh Dase5e9ca62013-07-30 19:08:36 +0530319 bool en_auto_cmd21;
320 struct device_attribute auto_cmd21_attr;
Asutosh Das9d7ee2f2013-11-08 12:33:47 +0530321 atomic_t controller_clock;
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700322 bool use_cdclp533;
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800323 bool use_updated_dll_reset;
Asutosh Das0ef24812012-12-18 16:14:02 +0530324};
325
326enum vdd_io_level {
327 /* set vdd_io_data->low_vol_level */
328 VDD_IO_LOW,
329 /* set vdd_io_data->high_vol_level */
330 VDD_IO_HIGH,
331 /*
332 * set whatever there in voltage_level (third argument) of
333 * sdhci_msm_set_vdd_io_vol() function.
334 */
335 VDD_IO_SET_LEVEL,
336};
337
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700338/* MSM platform specific tuning */
339static inline int msm_dll_poll_ck_out_en(struct sdhci_host *host,
340 u8 poll)
341{
342 int rc = 0;
343 u32 wait_cnt = 50;
344 u8 ck_out_en = 0;
345 struct mmc_host *mmc = host->mmc;
346
347 /* poll for CK_OUT_EN bit. max. poll time = 50us */
348 ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) &
349 CORE_CK_OUT_EN);
350
351 while (ck_out_en != poll) {
352 if (--wait_cnt == 0) {
353 pr_err("%s: %s: CK_OUT_EN bit is not %d\n",
354 mmc_hostname(mmc), __func__, poll);
355 rc = -ETIMEDOUT;
356 goto out;
357 }
358 udelay(1);
359
360 ck_out_en = !!(readl_relaxed(host->ioaddr +
361 CORE_DLL_CONFIG) & CORE_CK_OUT_EN);
362 }
363out:
364 return rc;
365}
366
Asutosh Dase5e9ca62013-07-30 19:08:36 +0530367/*
368 * Enable CDR to track changes of DAT lines and adjust sampling
369 * point according to voltage/temperature variations
370 */
371static int msm_enable_cdr_cm_sdc4_dll(struct sdhci_host *host)
372{
373 int rc = 0;
374 u32 config;
375
376 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
377 config |= CORE_CDR_EN;
378 config &= ~(CORE_CDR_EXT_EN | CORE_CK_OUT_EN);
379 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
380
381 rc = msm_dll_poll_ck_out_en(host, 0);
382 if (rc)
383 goto err;
384
385 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) |
386 CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
387
388 rc = msm_dll_poll_ck_out_en(host, 1);
389 if (rc)
390 goto err;
391 goto out;
392err:
393 pr_err("%s: %s: failed\n", mmc_hostname(host->mmc), __func__);
394out:
395 return rc;
396}
397
398static ssize_t store_auto_cmd21(struct device *dev, struct device_attribute
399 *attr, const char *buf, size_t count)
400{
401 struct sdhci_host *host = dev_get_drvdata(dev);
402 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
403 struct sdhci_msm_host *msm_host = pltfm_host->priv;
404 u32 tmp;
405 unsigned long flags;
406
407 if (!kstrtou32(buf, 0, &tmp)) {
408 spin_lock_irqsave(&host->lock, flags);
409 msm_host->en_auto_cmd21 = !!tmp;
410 spin_unlock_irqrestore(&host->lock, flags);
411 }
412 return count;
413}
414
415static ssize_t show_auto_cmd21(struct device *dev,
416 struct device_attribute *attr, char *buf)
417{
418 struct sdhci_host *host = dev_get_drvdata(dev);
419 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
420 struct sdhci_msm_host *msm_host = pltfm_host->priv;
421
422 return snprintf(buf, PAGE_SIZE, "%d\n", msm_host->en_auto_cmd21);
423}
424
425/* MSM auto-tuning handler */
426static int sdhci_msm_config_auto_tuning_cmd(struct sdhci_host *host,
427 bool enable,
428 u32 type)
429{
430 int rc = 0;
431 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
432 struct sdhci_msm_host *msm_host = pltfm_host->priv;
433 u32 val = 0;
434
435 if (!msm_host->en_auto_cmd21)
436 return 0;
437
438 if (type == MMC_SEND_TUNING_BLOCK_HS200)
439 val = CORE_HC_AUTO_CMD21_EN;
440 else
441 return 0;
442
443 if (enable) {
444 rc = msm_enable_cdr_cm_sdc4_dll(host);
445 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
446 val, host->ioaddr + CORE_VENDOR_SPEC);
447 } else {
448 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
449 ~val, host->ioaddr + CORE_VENDOR_SPEC);
450 }
451 return rc;
452}
453
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700454static int msm_config_cm_dll_phase(struct sdhci_host *host, u8 phase)
455{
456 int rc = 0;
457 u8 grey_coded_phase_table[] = {0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4,
458 0xC, 0xD, 0xF, 0xE, 0xA, 0xB, 0x9,
459 0x8};
460 unsigned long flags;
461 u32 config;
462 struct mmc_host *mmc = host->mmc;
463
464 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
465 spin_lock_irqsave(&host->lock, flags);
466
467 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
468 config &= ~(CORE_CDR_EN | CORE_CK_OUT_EN);
469 config |= (CORE_CDR_EXT_EN | CORE_DLL_EN);
470 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
471
472 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '0' */
473 rc = msm_dll_poll_ck_out_en(host, 0);
474 if (rc)
475 goto err_out;
476
477 /*
478 * Write the selected DLL clock output phase (0 ... 15)
479 * to CDR_SELEXT bit field of DLL_CONFIG register.
480 */
481 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
482 & ~(0xF << 20))
483 | (grey_coded_phase_table[phase] << 20)),
484 host->ioaddr + CORE_DLL_CONFIG);
485
486 /* Set CK_OUT_EN bit of DLL_CONFIG register to 1. */
487 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
488 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
489
490 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '1' */
491 rc = msm_dll_poll_ck_out_en(host, 1);
492 if (rc)
493 goto err_out;
494
495 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
496 config |= CORE_CDR_EN;
497 config &= ~CORE_CDR_EXT_EN;
498 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
499 goto out;
500
501err_out:
502 pr_err("%s: %s: Failed to set DLL phase: %d\n",
503 mmc_hostname(mmc), __func__, phase);
504out:
505 spin_unlock_irqrestore(&host->lock, flags);
506 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
507 return rc;
508}
509
510/*
511 * Find out the greatest range of consecuitive selected
512 * DLL clock output phases that can be used as sampling
513 * setting for SD3.0 UHS-I card read operation (in SDR104
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700514 * timing mode) or for eMMC4.5 card read operation (in
515 * HS400/HS200 timing mode).
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700516 * Select the 3/4 of the range and configure the DLL with the
517 * selected DLL clock output phase.
518 */
519
520static int msm_find_most_appropriate_phase(struct sdhci_host *host,
521 u8 *phase_table, u8 total_phases)
522{
523 int ret;
524 u8 ranges[MAX_PHASES][MAX_PHASES] = { {0}, {0} };
525 u8 phases_per_row[MAX_PHASES] = {0};
526 int row_index = 0, col_index = 0, selected_row_index = 0, curr_max = 0;
527 int i, cnt, phase_0_raw_index = 0, phase_15_raw_index = 0;
528 bool phase_0_found = false, phase_15_found = false;
529 struct mmc_host *mmc = host->mmc;
530
531 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
532 if (!total_phases || (total_phases > MAX_PHASES)) {
533 pr_err("%s: %s: invalid argument: total_phases=%d\n",
534 mmc_hostname(mmc), __func__, total_phases);
535 return -EINVAL;
536 }
537
538 for (cnt = 0; cnt < total_phases; cnt++) {
539 ranges[row_index][col_index] = phase_table[cnt];
540 phases_per_row[row_index] += 1;
541 col_index++;
542
543 if ((cnt + 1) == total_phases) {
544 continue;
545 /* check if next phase in phase_table is consecutive or not */
546 } else if ((phase_table[cnt] + 1) != phase_table[cnt + 1]) {
547 row_index++;
548 col_index = 0;
549 }
550 }
551
552 if (row_index >= MAX_PHASES)
553 return -EINVAL;
554
555 /* Check if phase-0 is present in first valid window? */
556 if (!ranges[0][0]) {
557 phase_0_found = true;
558 phase_0_raw_index = 0;
559 /* Check if cycle exist between 2 valid windows */
560 for (cnt = 1; cnt <= row_index; cnt++) {
561 if (phases_per_row[cnt]) {
562 for (i = 0; i < phases_per_row[cnt]; i++) {
563 if (ranges[cnt][i] == 15) {
564 phase_15_found = true;
565 phase_15_raw_index = cnt;
566 break;
567 }
568 }
569 }
570 }
571 }
572
573 /* If 2 valid windows form cycle then merge them as single window */
574 if (phase_0_found && phase_15_found) {
575 /* number of phases in raw where phase 0 is present */
576 u8 phases_0 = phases_per_row[phase_0_raw_index];
577 /* number of phases in raw where phase 15 is present */
578 u8 phases_15 = phases_per_row[phase_15_raw_index];
579
580 if (phases_0 + phases_15 >= MAX_PHASES)
581 /*
582 * If there are more than 1 phase windows then total
583 * number of phases in both the windows should not be
584 * more than or equal to MAX_PHASES.
585 */
586 return -EINVAL;
587
588 /* Merge 2 cyclic windows */
589 i = phases_15;
590 for (cnt = 0; cnt < phases_0; cnt++) {
591 ranges[phase_15_raw_index][i] =
592 ranges[phase_0_raw_index][cnt];
593 if (++i >= MAX_PHASES)
594 break;
595 }
596
597 phases_per_row[phase_0_raw_index] = 0;
598 phases_per_row[phase_15_raw_index] = phases_15 + phases_0;
599 }
600
601 for (cnt = 0; cnt <= row_index; cnt++) {
602 if (phases_per_row[cnt] > curr_max) {
603 curr_max = phases_per_row[cnt];
604 selected_row_index = cnt;
605 }
606 }
607
608 i = ((curr_max * 3) / 4);
609 if (i)
610 i--;
611
612 ret = (int)ranges[selected_row_index][i];
613
614 if (ret >= MAX_PHASES) {
615 ret = -EINVAL;
616 pr_err("%s: %s: invalid phase selected=%d\n",
617 mmc_hostname(mmc), __func__, ret);
618 }
619
620 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
621 return ret;
622}
623
624static inline void msm_cm_dll_set_freq(struct sdhci_host *host)
625{
626 u32 mclk_freq = 0;
627
628 /* Program the MCLK value to MCLK_FREQ bit field */
629 if (host->clock <= 112000000)
630 mclk_freq = 0;
631 else if (host->clock <= 125000000)
632 mclk_freq = 1;
633 else if (host->clock <= 137000000)
634 mclk_freq = 2;
635 else if (host->clock <= 150000000)
636 mclk_freq = 3;
637 else if (host->clock <= 162000000)
638 mclk_freq = 4;
639 else if (host->clock <= 175000000)
640 mclk_freq = 5;
641 else if (host->clock <= 187000000)
642 mclk_freq = 6;
643 else if (host->clock <= 200000000)
644 mclk_freq = 7;
645
646 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
647 & ~(7 << 24)) | (mclk_freq << 24)),
648 host->ioaddr + CORE_DLL_CONFIG);
649}
650
651/* Initialize the DLL (Programmable Delay Line ) */
652static int msm_init_cm_dll(struct sdhci_host *host)
653{
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800654 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
655 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700656 struct mmc_host *mmc = host->mmc;
657 int rc = 0;
658 unsigned long flags;
659 u32 wait_cnt;
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530660 bool prev_pwrsave, curr_pwrsave;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700661
662 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
663 spin_lock_irqsave(&host->lock, flags);
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530664 prev_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
665 CORE_CLK_PWRSAVE);
666 curr_pwrsave = prev_pwrsave;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700667 /*
668 * Make sure that clock is always enabled when DLL
669 * tuning is in progress. Keeping PWRSAVE ON may
670 * turn off the clock. So let's disable the PWRSAVE
671 * here and re-enable it once tuning is completed.
672 */
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530673 if (prev_pwrsave) {
674 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
675 & ~CORE_CLK_PWRSAVE),
676 host->ioaddr + CORE_VENDOR_SPEC);
677 curr_pwrsave = false;
678 }
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700679
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800680 if (msm_host->use_updated_dll_reset) {
681 /* Disable the DLL clock */
682 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
683 & ~CORE_CK_OUT_EN),
684 host->ioaddr + CORE_DLL_CONFIG);
685
686 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
687 | CORE_DLL_CLOCK_DISABLE),
688 host->ioaddr + CORE_DLL_CONFIG_2);
689 }
690
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700691 /* Write 1 to DLL_RST bit of DLL_CONFIG register */
692 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
693 | CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
694
695 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
696 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
697 | CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
698 msm_cm_dll_set_freq(host);
699
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800700 if (msm_host->use_updated_dll_reset) {
701 u32 mclk_freq = 0;
702
703 if ((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
704 & CORE_FLL_CYCLE_CNT))
705 mclk_freq = (u32) ((host->clock / TCXO_FREQ) * 8);
706 else
707 mclk_freq = (u32) ((host->clock / TCXO_FREQ) * 4);
708
709 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
710 & ~(0xFF << 10)) | (mclk_freq << 10)),
711 host->ioaddr + CORE_DLL_CONFIG_2);
712 /* wait for 5us before enabling DLL clock */
713 udelay(5);
714 }
715
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700716 /* Write 0 to DLL_RST bit of DLL_CONFIG register */
717 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
718 & ~CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
719
720 /* Write 0 to DLL_PDN bit of DLL_CONFIG register */
721 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
722 & ~CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
723
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800724 if (msm_host->use_updated_dll_reset) {
725 msm_cm_dll_set_freq(host);
726 /* Enable the DLL clock */
727 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
728 & ~CORE_DLL_CLOCK_DISABLE),
729 host->ioaddr + CORE_DLL_CONFIG_2);
730 }
731
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700732 /* Set DLL_EN bit to 1. */
733 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
734 | CORE_DLL_EN), host->ioaddr + CORE_DLL_CONFIG);
735
736 /* Set CK_OUT_EN bit to 1. */
737 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
738 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
739
740 wait_cnt = 50;
741 /* Wait until DLL_LOCK bit of DLL_STATUS register becomes '1' */
742 while (!(readl_relaxed(host->ioaddr + CORE_DLL_STATUS) &
743 CORE_DLL_LOCK)) {
744 /* max. wait for 50us sec for LOCK bit to be set */
745 if (--wait_cnt == 0) {
746 pr_err("%s: %s: DLL failed to LOCK\n",
747 mmc_hostname(mmc), __func__);
748 rc = -ETIMEDOUT;
749 goto out;
750 }
751 /* wait for 1us before polling again */
752 udelay(1);
753 }
754
755out:
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530756 /* Restore the correct PWRSAVE state */
757 if (prev_pwrsave ^ curr_pwrsave) {
758 u32 reg = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC);
759
760 if (prev_pwrsave)
761 reg |= CORE_CLK_PWRSAVE;
762 else
763 reg &= ~CORE_CLK_PWRSAVE;
764
765 writel_relaxed(reg, host->ioaddr + CORE_VENDOR_SPEC);
766 }
767
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700768 spin_unlock_irqrestore(&host->lock, flags);
769 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
770 return rc;
771}
772
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700773static int sdhci_msm_cdclp533_calibration(struct sdhci_host *host)
774{
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700775 u32 calib_done;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700776 int ret = 0;
777 int cdc_err = 0;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700778
779 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
780
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700781 /* Write 0 to CDC_T4_DLY_SEL field in VENDOR_SPEC_DDR200_CFG */
782 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
783 & ~CORE_CDC_T4_DLY_SEL),
784 host->ioaddr + CORE_DDR_200_CFG);
785
786 /* Write 0 to CDC_SWITCH_BYPASS_OFF field in CORE_CSR_CDC_GEN_CFG */
787 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG)
788 & ~CORE_CDC_SWITCH_BYPASS_OFF),
789 host->ioaddr + CORE_CSR_CDC_GEN_CFG);
790
791 /* Write 1 to CDC_SWITCH_RC_EN field in CORE_CSR_CDC_GEN_CFG */
792 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG)
793 | CORE_CDC_SWITCH_RC_EN),
794 host->ioaddr + CORE_CSR_CDC_GEN_CFG);
795
796 /* Write 0 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
797 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
798 & ~CORE_START_CDC_TRAFFIC),
799 host->ioaddr + CORE_DDR_200_CFG);
800
801 /*
802 * Perform CDC Register Initialization Sequence
803 *
804 * CORE_CSR_CDC_CTLR_CFG0 0x11800EC
805 * CORE_CSR_CDC_CTLR_CFG1 0x3011111
806 * CORE_CSR_CDC_CAL_TIMER_CFG0 0x1201000
807 * CORE_CSR_CDC_CAL_TIMER_CFG1 0x4
808 * CORE_CSR_CDC_REFCOUNT_CFG 0xCB732020
809 * CORE_CSR_CDC_COARSE_CAL_CFG 0xB19
810 * CORE_CSR_CDC_DELAY_CFG 0x3AC
811 * CORE_CDC_OFFSET_CFG 0x0
812 * CORE_CDC_SLAVE_DDA_CFG 0x16334
813 */
814
815 writel_relaxed(0x11800EC, host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
816 writel_relaxed(0x3011111, host->ioaddr + CORE_CSR_CDC_CTLR_CFG1);
817 writel_relaxed(0x1201000, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
818 writel_relaxed(0x4, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG1);
819 writel_relaxed(0xCB732020, host->ioaddr + CORE_CSR_CDC_REFCOUNT_CFG);
820 writel_relaxed(0xB19, host->ioaddr + CORE_CSR_CDC_COARSE_CAL_CFG);
Subhash Jadavanibe406d92014-06-17 16:47:48 -0700821 writel_relaxed(0x4E2, host->ioaddr + CORE_CSR_CDC_DELAY_CFG);
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700822 writel_relaxed(0x0, host->ioaddr + CORE_CDC_OFFSET_CFG);
823 writel_relaxed(0x16334, host->ioaddr + CORE_CDC_SLAVE_DDA_CFG);
824
825 /* CDC HW Calibration */
826
827 /* Write 1 to SW_TRIG_FULL_CALIB field in CORE_CSR_CDC_CTLR_CFG0 */
828 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
829 | CORE_SW_TRIG_FULL_CALIB),
830 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
831
832 /* Write 0 to SW_TRIG_FULL_CALIB field in CORE_CSR_CDC_CTLR_CFG0 */
833 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
834 & ~CORE_SW_TRIG_FULL_CALIB),
835 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
836
837 /* Write 1 to HW_AUTOCAL_ENA field in CORE_CSR_CDC_CTLR_CFG0 */
838 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
839 | CORE_HW_AUTOCAL_ENA),
840 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
841
842 /* Write 1 to TIMER_ENA field in CORE_CSR_CDC_CAL_TIMER_CFG0 */
843 writel_relaxed((readl_relaxed(host->ioaddr +
844 CORE_CSR_CDC_CAL_TIMER_CFG0) | CORE_TIMER_ENA),
845 host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
846
847 mb();
848
849 /* Poll on CALIBRATION_DONE field in CORE_CSR_CDC_STATUS0 to be 1 */
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700850 ret = readl_poll_timeout(host->ioaddr + CORE_CSR_CDC_STATUS0,
851 calib_done, (calib_done & CORE_CALIBRATION_DONE), 1, 50);
852
853 if (ret == -ETIMEDOUT) {
854 pr_err("%s: %s: CDC Calibration was not completed\n",
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700855 mmc_hostname(host->mmc), __func__);
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700856 goto out;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700857 }
858
859 /* Verify CDC_ERROR_CODE field in CORE_CSR_CDC_STATUS0 is 0 */
860 cdc_err = readl_relaxed(host->ioaddr + CORE_CSR_CDC_STATUS0)
861 & CORE_CDC_ERROR_CODE_MASK;
862 if (cdc_err) {
863 pr_err("%s: %s: CDC Error Code %d\n",
864 mmc_hostname(host->mmc), __func__, cdc_err);
865 ret = -EINVAL;
866 goto out;
867 }
868
869 /* Write 1 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
870 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
871 | CORE_START_CDC_TRAFFIC),
872 host->ioaddr + CORE_DDR_200_CFG);
873out:
874 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
875 __func__, ret);
876 return ret;
877}
878
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700879static int sdhci_msm_cm_dll_sdc4_calibration(struct sdhci_host *host)
880{
881 u32 dll_status;
882 int ret = 0;
883
884 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
885
886 /*
887 * Currently the CORE_DDR_CONFIG register defaults to desired
888 * configuration on reset. Currently reprogramming the power on
889 * reset (POR) value in case it might have been modified by
890 * bootloaders. In the future, if this changes, then the desired
891 * values will need to be programmed appropriately.
892 */
893 writel_relaxed(DDR_CONFIG_POR_VAL, host->ioaddr + CORE_DDR_CONFIG);
894
895 /* Write 1 to DDR_CAL_EN field in CORE_DLL_CONFIG_2 */
896 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
897 | CORE_DDR_CAL_EN),
898 host->ioaddr + CORE_DLL_CONFIG_2);
899
900 /* Poll on DDR_DLL_LOCK bit in CORE_DLL_STATUS to be set */
901 ret = readl_poll_timeout(host->ioaddr + CORE_DLL_STATUS,
902 dll_status, (dll_status & CORE_DDR_DLL_LOCK), 10, 1000);
903
904 if (ret == -ETIMEDOUT) {
905 pr_err("%s: %s: CM_DLL_SDC4 Calibration was not completed\n",
906 mmc_hostname(host->mmc), __func__);
907 goto out;
908 }
909
910 /* set CORE_PWRSAVE_DLL bit in CORE_VENDOR_SPEC3 */
911 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC3)
912 | CORE_PWRSAVE_DLL),
913 host->ioaddr + CORE_VENDOR_SPEC3);
914 mb();
915out:
916 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
917 __func__, ret);
918 return ret;
919}
920
921static int sdhci_msm_hs400_dll_calibration(struct sdhci_host *host)
922{
923 int ret = 0;
924 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
925 struct sdhci_msm_host *msm_host = pltfm_host->priv;
926
927 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
928
929 /*
930 * Retuning in HS400 (DDR mode) will fail, just reset the
931 * tuning block and restore the saved tuning phase.
932 */
933 ret = msm_init_cm_dll(host);
934 if (ret)
935 goto out;
936
937 /* Set the selected phase in delay line hw block */
938 ret = msm_config_cm_dll_phase(host, msm_host->saved_tuning_phase);
939 if (ret)
940 goto out;
941
Krishna Konda0e8efba2014-06-23 14:50:38 -0700942 /* Write 1 to CMD_DAT_TRACK_SEL field in DLL_CONFIG */
943 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
944 | CORE_CMD_DAT_TRACK_SEL),
945 host->ioaddr + CORE_DLL_CONFIG);
946
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700947 if (msm_host->use_cdclp533)
948 /* Calibrate CDCLP533 DLL HW */
949 ret = sdhci_msm_cdclp533_calibration(host);
950 else
951 /* Calibrate CM_DLL_SDC4 HW */
952 ret = sdhci_msm_cm_dll_sdc4_calibration(host);
953out:
954 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
955 __func__, ret);
956 return ret;
957}
958
Krishna Konda96e6b112013-10-28 15:25:03 -0700959static void sdhci_msm_set_mmc_drv_type(struct sdhci_host *host, u32 opcode,
960 u8 drv_type)
961{
962 struct mmc_command cmd = {0};
963 struct mmc_request mrq = {NULL};
964 struct mmc_host *mmc = host->mmc;
965 u8 val = ((drv_type << 4) | 2);
966
967 cmd.opcode = MMC_SWITCH;
968 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
969 (EXT_CSD_HS_TIMING << 16) |
970 (val << 8) |
971 EXT_CSD_CMD_SET_NORMAL;
972 cmd.flags = MMC_CMD_AC | MMC_RSP_R1B;
973 /* 1 sec */
974 cmd.busy_timeout = 1000 * 1000;
975
976 memset(cmd.resp, 0, sizeof(cmd.resp));
977 cmd.retries = 3;
978
979 mrq.cmd = &cmd;
980 cmd.data = NULL;
981
982 mmc_wait_for_req(mmc, &mrq);
983 pr_debug("%s: %s: set card drive type to %d\n",
984 mmc_hostname(mmc), __func__,
985 drv_type);
986}
987
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700988int sdhci_msm_execute_tuning(struct sdhci_host *host, u32 opcode)
989{
990 unsigned long flags;
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530991 int tuning_seq_cnt = 3;
Krishna Konda96e6b112013-10-28 15:25:03 -0700992 u8 phase, *data_buf, tuned_phases[NUM_TUNING_PHASES], tuned_phase_cnt;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700993 const u32 *tuning_block_pattern = tuning_block_64;
994 int size = sizeof(tuning_block_64); /* Tuning pattern size in bytes */
995 int rc;
996 struct mmc_host *mmc = host->mmc;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530997 struct mmc_ios ios = host->mmc->ios;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700998 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
999 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Krishna Konda96e6b112013-10-28 15:25:03 -07001000 u8 drv_type = 0;
1001 bool drv_type_changed = false;
1002 struct mmc_card *card = host->mmc->card;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301003
1004 /*
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07001005 * Tuning is required for SDR104, HS200 and HS400 cards and
1006 * if clock frequency is greater than 100MHz in these modes.
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301007 */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07001008 if (host->clock <= CORE_FREQ_100MHZ ||
1009 !((ios.timing == MMC_TIMING_MMC_HS400) ||
1010 (ios.timing == MMC_TIMING_MMC_HS200) ||
1011 (ios.timing == MMC_TIMING_UHS_SDR104)))
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301012 return 0;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001013
1014 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07001015
Krishna Konda2faa7bb2014-06-04 01:25:16 -07001016 /* CDC/SDC4 DLL HW calibration is only required for HS400 mode*/
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07001017 if (msm_host->tuning_done && !msm_host->calibration_done &&
1018 (mmc->ios.timing == MMC_TIMING_MMC_HS400)) {
Krishna Konda2faa7bb2014-06-04 01:25:16 -07001019 rc = sdhci_msm_hs400_dll_calibration(host);
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07001020 spin_lock_irqsave(&host->lock, flags);
1021 if (!rc)
1022 msm_host->calibration_done = true;
1023 spin_unlock_irqrestore(&host->lock, flags);
1024 goto out;
1025 }
1026
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001027 spin_lock_irqsave(&host->lock, flags);
1028
1029 if ((opcode == MMC_SEND_TUNING_BLOCK_HS200) &&
1030 (mmc->ios.bus_width == MMC_BUS_WIDTH_8)) {
1031 tuning_block_pattern = tuning_block_128;
1032 size = sizeof(tuning_block_128);
1033 }
1034 spin_unlock_irqrestore(&host->lock, flags);
1035
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001036 data_buf = kmalloc(size, GFP_KERNEL);
1037 if (!data_buf) {
1038 rc = -ENOMEM;
1039 goto out;
1040 }
1041
Sahitya Tummala9fe16532013-06-13 10:36:57 +05301042retry:
Krishna Konda96e6b112013-10-28 15:25:03 -07001043 tuned_phase_cnt = 0;
1044
Sahitya Tummala9fe16532013-06-13 10:36:57 +05301045 /* first of all reset the tuning block */
1046 rc = msm_init_cm_dll(host);
1047 if (rc)
1048 goto kfree;
1049
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001050 phase = 0;
1051 do {
1052 struct mmc_command cmd = {0};
1053 struct mmc_data data = {0};
1054 struct mmc_request mrq = {
1055 .cmd = &cmd,
1056 .data = &data
1057 };
1058 struct scatterlist sg;
1059
1060 /* set the phase in delay line hw block */
1061 rc = msm_config_cm_dll_phase(host, phase);
1062 if (rc)
1063 goto kfree;
1064
1065 cmd.opcode = opcode;
1066 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1067
1068 data.blksz = size;
1069 data.blocks = 1;
1070 data.flags = MMC_DATA_READ;
1071 data.timeout_ns = 1000 * 1000 * 1000; /* 1 sec */
1072
1073 data.sg = &sg;
1074 data.sg_len = 1;
1075 sg_init_one(&sg, data_buf, size);
1076 memset(data_buf, 0, size);
1077 mmc_wait_for_req(mmc, &mrq);
1078
1079 if (!cmd.error && !data.error &&
1080 !memcmp(data_buf, tuning_block_pattern, size)) {
1081 /* tuning is successful at this tuning point */
1082 tuned_phases[tuned_phase_cnt++] = phase;
Krishna Konda96e6b112013-10-28 15:25:03 -07001083 pr_debug("%s: %s: found *** good *** phase = %d\n",
1084 mmc_hostname(mmc), __func__, phase);
1085 } else {
1086 pr_debug("%s: %s: found ## bad ## phase = %d\n",
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001087 mmc_hostname(mmc), __func__, phase);
1088 }
1089 } while (++phase < 16);
1090
Sahitya Tummaladfdb4af2014-04-01 14:29:13 +05301091 if ((tuned_phase_cnt == NUM_TUNING_PHASES) &&
1092 card && mmc_card_mmc(card)) {
Krishna Konda96e6b112013-10-28 15:25:03 -07001093 /*
1094 * If all phases pass then its a problem. So change the card's
1095 * drive type to a different value, if supported and repeat
1096 * tuning until at least one phase fails. Then set the original
1097 * drive type back.
1098 *
1099 * If all the phases still pass after trying all possible
1100 * drive types, then one of those 16 phases will be picked.
1101 * This is no different from what was going on before the
1102 * modification to change drive type and retune.
1103 */
1104 pr_debug("%s: tuned phases count: %d\n", mmc_hostname(mmc),
1105 tuned_phase_cnt);
1106
1107 /* set drive type to other value . default setting is 0x0 */
1108 while (++drv_type <= MAX_DRV_TYPES_SUPPORTED_HS200) {
1109 if (card->ext_csd.raw_driver_strength &
1110 (1 << drv_type)) {
1111 sdhci_msm_set_mmc_drv_type(host, opcode,
1112 drv_type);
1113 if (!drv_type_changed)
1114 drv_type_changed = true;
1115 goto retry;
1116 }
1117 }
1118 }
1119
1120 /* reset drive type to default (50 ohm) if changed */
1121 if (drv_type_changed)
1122 sdhci_msm_set_mmc_drv_type(host, opcode, 0);
1123
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001124 if (tuned_phase_cnt) {
1125 rc = msm_find_most_appropriate_phase(host, tuned_phases,
1126 tuned_phase_cnt);
1127 if (rc < 0)
1128 goto kfree;
1129 else
1130 phase = (u8)rc;
1131
1132 /*
1133 * Finally set the selected phase in delay
1134 * line hw block.
1135 */
1136 rc = msm_config_cm_dll_phase(host, phase);
1137 if (rc)
1138 goto kfree;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07001139 msm_host->saved_tuning_phase = phase;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001140 pr_debug("%s: %s: finally setting the tuning phase to %d\n",
1141 mmc_hostname(mmc), __func__, phase);
1142 } else {
Sahitya Tummala9fe16532013-06-13 10:36:57 +05301143 if (--tuning_seq_cnt)
1144 goto retry;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001145 /* tuning failed */
1146 pr_err("%s: %s: no tuning point found\n",
1147 mmc_hostname(mmc), __func__);
Sahitya Tummala9fe16532013-06-13 10:36:57 +05301148 rc = -EIO;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001149 }
1150
1151kfree:
1152 kfree(data_buf);
1153out:
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07001154 spin_lock_irqsave(&host->lock, flags);
1155 if (!rc)
1156 msm_host->tuning_done = true;
1157 spin_unlock_irqrestore(&host->lock, flags);
1158 pr_debug("%s: Exit %s, err(%d)\n", mmc_hostname(mmc), __func__, rc);
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001159 return rc;
1160}
1161
Asutosh Das0ef24812012-12-18 16:14:02 +05301162static int sdhci_msm_setup_gpio(struct sdhci_msm_pltfm_data *pdata, bool enable)
1163{
1164 struct sdhci_msm_gpio_data *curr;
1165 int i, ret = 0;
1166
1167 curr = pdata->pin_data->gpio_data;
1168 for (i = 0; i < curr->size; i++) {
1169 if (!gpio_is_valid(curr->gpio[i].no)) {
1170 ret = -EINVAL;
1171 pr_err("%s: Invalid gpio = %d\n", __func__,
1172 curr->gpio[i].no);
1173 goto free_gpios;
1174 }
1175 if (enable) {
1176 ret = gpio_request(curr->gpio[i].no,
1177 curr->gpio[i].name);
1178 if (ret) {
1179 pr_err("%s: gpio_request(%d, %s) failed %d\n",
1180 __func__, curr->gpio[i].no,
1181 curr->gpio[i].name, ret);
1182 goto free_gpios;
1183 }
1184 curr->gpio[i].is_enabled = true;
1185 } else {
1186 gpio_free(curr->gpio[i].no);
1187 curr->gpio[i].is_enabled = false;
1188 }
1189 }
1190 return ret;
1191
1192free_gpios:
1193 for (i--; i >= 0; i--) {
1194 gpio_free(curr->gpio[i].no);
1195 curr->gpio[i].is_enabled = false;
1196 }
1197 return ret;
1198}
1199
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301200static int sdhci_msm_setup_pinctrl(struct sdhci_msm_pltfm_data *pdata,
1201 bool enable)
1202{
1203 int ret = 0;
1204
1205 if (enable)
1206 ret = pinctrl_select_state(pdata->pctrl_data->pctrl,
1207 pdata->pctrl_data->pins_active);
1208 else
1209 ret = pinctrl_select_state(pdata->pctrl_data->pctrl,
1210 pdata->pctrl_data->pins_sleep);
1211
1212 if (ret < 0)
1213 pr_err("%s state for pinctrl failed with %d\n",
1214 enable ? "Enabling" : "Disabling", ret);
1215
1216 return ret;
1217}
1218
Asutosh Das0ef24812012-12-18 16:14:02 +05301219static int sdhci_msm_setup_pins(struct sdhci_msm_pltfm_data *pdata, bool enable)
1220{
1221 int ret = 0;
1222
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301223 if (pdata->pin_cfg_sts == enable) {
Asutosh Das0ef24812012-12-18 16:14:02 +05301224 return 0;
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301225 } else if (pdata->pctrl_data) {
1226 ret = sdhci_msm_setup_pinctrl(pdata, enable);
1227 goto out;
1228 } else if (!pdata->pin_data) {
1229 return 0;
1230 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301231
Sahitya Tummala1cd7e072014-02-14 13:19:01 +05301232 if (pdata->pin_data->is_gpio)
1233 ret = sdhci_msm_setup_gpio(pdata, enable);
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301234out:
Asutosh Das0ef24812012-12-18 16:14:02 +05301235 if (!ret)
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301236 pdata->pin_cfg_sts = enable;
Asutosh Das0ef24812012-12-18 16:14:02 +05301237
1238 return ret;
1239}
1240
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301241static int sdhci_msm_dt_get_array(struct device *dev, const char *prop_name,
1242 u32 **out, int *len, u32 size)
1243{
1244 int ret = 0;
1245 struct device_node *np = dev->of_node;
1246 size_t sz;
1247 u32 *arr = NULL;
1248
1249 if (!of_get_property(np, prop_name, len)) {
1250 ret = -EINVAL;
1251 goto out;
1252 }
1253 sz = *len = *len / sizeof(*arr);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07001254 if (sz <= 0 || (size > 0 && (sz > size))) {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301255 dev_err(dev, "%s invalid size\n", prop_name);
1256 ret = -EINVAL;
1257 goto out;
1258 }
1259
1260 arr = devm_kzalloc(dev, sz * sizeof(*arr), GFP_KERNEL);
1261 if (!arr) {
1262 dev_err(dev, "%s failed allocating memory\n", prop_name);
1263 ret = -ENOMEM;
1264 goto out;
1265 }
1266
1267 ret = of_property_read_u32_array(np, prop_name, arr, sz);
1268 if (ret < 0) {
1269 dev_err(dev, "%s failed reading array %d\n", prop_name, ret);
1270 goto out;
1271 }
1272 *out = arr;
1273out:
1274 if (ret)
1275 *len = 0;
1276 return ret;
1277}
1278
Asutosh Das0ef24812012-12-18 16:14:02 +05301279#define MAX_PROP_SIZE 32
1280static int sdhci_msm_dt_parse_vreg_info(struct device *dev,
1281 struct sdhci_msm_reg_data **vreg_data, const char *vreg_name)
1282{
1283 int len, ret = 0;
1284 const __be32 *prop;
1285 char prop_name[MAX_PROP_SIZE];
1286 struct sdhci_msm_reg_data *vreg;
1287 struct device_node *np = dev->of_node;
1288
1289 snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", vreg_name);
1290 if (!of_parse_phandle(np, prop_name, 0)) {
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301291 dev_info(dev, "No vreg data found for %s\n", vreg_name);
Asutosh Das0ef24812012-12-18 16:14:02 +05301292 return ret;
1293 }
1294
1295 vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
1296 if (!vreg) {
1297 dev_err(dev, "No memory for vreg: %s\n", vreg_name);
1298 ret = -ENOMEM;
1299 return ret;
1300 }
1301
1302 vreg->name = vreg_name;
1303
1304 snprintf(prop_name, MAX_PROP_SIZE,
1305 "qcom,%s-always-on", vreg_name);
1306 if (of_get_property(np, prop_name, NULL))
1307 vreg->is_always_on = true;
1308
1309 snprintf(prop_name, MAX_PROP_SIZE,
1310 "qcom,%s-lpm-sup", vreg_name);
1311 if (of_get_property(np, prop_name, NULL))
1312 vreg->lpm_sup = true;
1313
1314 snprintf(prop_name, MAX_PROP_SIZE,
1315 "qcom,%s-voltage-level", vreg_name);
1316 prop = of_get_property(np, prop_name, &len);
1317 if (!prop || (len != (2 * sizeof(__be32)))) {
1318 dev_warn(dev, "%s %s property\n",
1319 prop ? "invalid format" : "no", prop_name);
1320 } else {
1321 vreg->low_vol_level = be32_to_cpup(&prop[0]);
1322 vreg->high_vol_level = be32_to_cpup(&prop[1]);
1323 }
1324
1325 snprintf(prop_name, MAX_PROP_SIZE,
1326 "qcom,%s-current-level", vreg_name);
1327 prop = of_get_property(np, prop_name, &len);
1328 if (!prop || (len != (2 * sizeof(__be32)))) {
1329 dev_warn(dev, "%s %s property\n",
1330 prop ? "invalid format" : "no", prop_name);
1331 } else {
1332 vreg->lpm_uA = be32_to_cpup(&prop[0]);
1333 vreg->hpm_uA = be32_to_cpup(&prop[1]);
1334 }
1335
1336 *vreg_data = vreg;
1337 dev_dbg(dev, "%s: %s %s vol=[%d %d]uV, curr=[%d %d]uA\n",
1338 vreg->name, vreg->is_always_on ? "always_on," : "",
1339 vreg->lpm_sup ? "lpm_sup," : "", vreg->low_vol_level,
1340 vreg->high_vol_level, vreg->lpm_uA, vreg->hpm_uA);
1341
1342 return ret;
1343}
1344
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301345static int sdhci_msm_parse_pinctrl_info(struct device *dev,
1346 struct sdhci_msm_pltfm_data *pdata)
1347{
1348 struct sdhci_pinctrl_data *pctrl_data;
1349 struct pinctrl *pctrl;
1350 int ret = 0;
1351
1352 /* Try to obtain pinctrl handle */
1353 pctrl = devm_pinctrl_get(dev);
1354 if (IS_ERR(pctrl)) {
1355 ret = PTR_ERR(pctrl);
1356 goto out;
1357 }
1358 pctrl_data = devm_kzalloc(dev, sizeof(*pctrl_data), GFP_KERNEL);
1359 if (!pctrl_data) {
1360 dev_err(dev, "No memory for sdhci_pinctrl_data\n");
1361 ret = -ENOMEM;
1362 goto out;
1363 }
1364 pctrl_data->pctrl = pctrl;
1365 /* Look-up and keep the states handy to be used later */
1366 pctrl_data->pins_active = pinctrl_lookup_state(
1367 pctrl_data->pctrl, "active");
1368 if (IS_ERR(pctrl_data->pins_active)) {
1369 ret = PTR_ERR(pctrl_data->pins_active);
1370 dev_err(dev, "Could not get active pinstates, err:%d\n", ret);
1371 goto out;
1372 }
1373 pctrl_data->pins_sleep = pinctrl_lookup_state(
1374 pctrl_data->pctrl, "sleep");
1375 if (IS_ERR(pctrl_data->pins_sleep)) {
1376 ret = PTR_ERR(pctrl_data->pins_sleep);
1377 dev_err(dev, "Could not get sleep pinstates, err:%d\n", ret);
1378 goto out;
1379 }
1380 pdata->pctrl_data = pctrl_data;
1381out:
1382 return ret;
1383}
1384
Asutosh Das0ef24812012-12-18 16:14:02 +05301385#define GPIO_NAME_MAX_LEN 32
1386static int sdhci_msm_dt_parse_gpio_info(struct device *dev,
1387 struct sdhci_msm_pltfm_data *pdata)
1388{
1389 int ret = 0, cnt, i;
1390 struct sdhci_msm_pin_data *pin_data;
1391 struct device_node *np = dev->of_node;
1392
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301393 ret = sdhci_msm_parse_pinctrl_info(dev, pdata);
1394 if (!ret) {
1395 goto out;
1396 } else if (ret == -EPROBE_DEFER) {
1397 dev_err(dev, "Pinctrl framework not registered, err:%d\n", ret);
1398 goto out;
1399 } else {
1400 dev_err(dev, "Parsing Pinctrl failed with %d, falling back on GPIO lib\n",
1401 ret);
Sahitya Tummala1cd7e072014-02-14 13:19:01 +05301402 ret = 0;
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301403 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301404 pin_data = devm_kzalloc(dev, sizeof(*pin_data), GFP_KERNEL);
1405 if (!pin_data) {
1406 dev_err(dev, "No memory for pin_data\n");
1407 ret = -ENOMEM;
1408 goto out;
1409 }
1410
1411 cnt = of_gpio_count(np);
1412 if (cnt > 0) {
1413 pin_data->gpio_data = devm_kzalloc(dev,
1414 sizeof(struct sdhci_msm_gpio_data), GFP_KERNEL);
1415 if (!pin_data->gpio_data) {
1416 dev_err(dev, "No memory for gpio_data\n");
1417 ret = -ENOMEM;
1418 goto out;
1419 }
1420 pin_data->gpio_data->size = cnt;
1421 pin_data->gpio_data->gpio = devm_kzalloc(dev, cnt *
1422 sizeof(struct sdhci_msm_gpio), GFP_KERNEL);
1423
1424 if (!pin_data->gpio_data->gpio) {
1425 dev_err(dev, "No memory for gpio\n");
1426 ret = -ENOMEM;
1427 goto out;
1428 }
1429
1430 for (i = 0; i < cnt; i++) {
1431 const char *name = NULL;
1432 char result[GPIO_NAME_MAX_LEN];
1433 pin_data->gpio_data->gpio[i].no = of_get_gpio(np, i);
1434 of_property_read_string_index(np,
1435 "qcom,gpio-names", i, &name);
1436
1437 snprintf(result, GPIO_NAME_MAX_LEN, "%s-%s",
1438 dev_name(dev), name ? name : "?");
1439 pin_data->gpio_data->gpio[i].name = result;
1440 dev_dbg(dev, "%s: gpio[%s] = %d\n", __func__,
1441 pin_data->gpio_data->gpio[i].name,
1442 pin_data->gpio_data->gpio[i].no);
Asutosh Das0ef24812012-12-18 16:14:02 +05301443 }
1444 }
Sahitya Tummala1cd7e072014-02-14 13:19:01 +05301445 pdata->pin_data = pin_data;
Asutosh Das0ef24812012-12-18 16:14:02 +05301446out:
1447 if (ret)
1448 dev_err(dev, "%s failed with err %d\n", __func__, ret);
1449 return ret;
1450}
1451
Maya Erez994cf2f2014-10-21 20:22:04 +03001452#ifdef CONFIG_SMP
1453static void sdhci_msm_populate_affinity_type(struct sdhci_msm_pltfm_data *pdata,
1454 struct device_node *np)
1455{
1456 const char *cpu_affinity = NULL;
1457
1458 pdata->cpu_affinity_type = PM_QOS_REQ_AFFINE_IRQ;
1459 if (!of_property_read_string(np, "qcom,cpu-affinity",
1460 &cpu_affinity)) {
1461 if (!strcmp(cpu_affinity, "all_cores"))
1462 pdata->cpu_affinity_type = PM_QOS_REQ_ALL_CORES;
1463 else if (!strcmp(cpu_affinity, "affine_cores"))
1464 pdata->cpu_affinity_type = PM_QOS_REQ_AFFINE_CORES;
1465 else if (!strcmp(cpu_affinity, "affine_irq"))
1466 pdata->cpu_affinity_type = PM_QOS_REQ_AFFINE_IRQ;
1467 }
1468}
1469#else
1470static void sdhci_msm_populate_affinity_type(struct sdhci_msm_pltfm_data *pdata,
1471 struct device_node *np)
1472{
1473}
1474#endif
1475
Asutosh Das0ef24812012-12-18 16:14:02 +05301476/* Parse platform data */
1477static struct sdhci_msm_pltfm_data *sdhci_msm_populate_pdata(struct device *dev)
1478{
1479 struct sdhci_msm_pltfm_data *pdata = NULL;
1480 struct device_node *np = dev->of_node;
1481 u32 bus_width = 0;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05301482 u32 cpu_dma_latency;
Asutosh Das0ef24812012-12-18 16:14:02 +05301483 int len, i;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301484 int clk_table_len;
1485 u32 *clk_table = NULL;
Sujit Reddy Thumma1958d3d2013-06-03 09:54:32 +05301486 enum of_gpio_flags flags = OF_GPIO_ACTIVE_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05301487
1488 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1489 if (!pdata) {
1490 dev_err(dev, "failed to allocate memory for platform data\n");
1491 goto out;
1492 }
1493
Sujit Reddy Thumma1958d3d2013-06-03 09:54:32 +05301494 pdata->status_gpio = of_get_named_gpio_flags(np, "cd-gpios", 0, &flags);
1495 if (gpio_is_valid(pdata->status_gpio) & !(flags & OF_GPIO_ACTIVE_LOW))
1496 pdata->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
Sahitya Tummala581df132013-03-12 14:57:46 +05301497
Asutosh Das0ef24812012-12-18 16:14:02 +05301498 of_property_read_u32(np, "qcom,bus-width", &bus_width);
1499 if (bus_width == 8)
1500 pdata->mmc_bus_width = MMC_CAP_8_BIT_DATA;
1501 else if (bus_width == 4)
1502 pdata->mmc_bus_width = MMC_CAP_4_BIT_DATA;
1503 else {
1504 dev_notice(dev, "invalid bus-width, default to 1-bit mode\n");
1505 pdata->mmc_bus_width = 0;
1506 }
1507
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05301508 if (!of_property_read_u32(np, "qcom,cpu-dma-latency-us",
1509 &cpu_dma_latency))
1510 pdata->cpu_dma_latency_us = cpu_dma_latency;
Asutosh Das598a4d42014-02-05 16:38:23 +05301511 else
1512 pdata->cpu_dma_latency_us = MSM_MMC_DEFAULT_CPU_DMA_LATENCY;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301513 if (sdhci_msm_dt_get_array(dev, "qcom,clk-rates",
1514 &clk_table, &clk_table_len, 0)) {
1515 dev_err(dev, "failed parsing supported clock rates\n");
1516 goto out;
1517 }
1518 if (!clk_table || !clk_table_len) {
1519 dev_err(dev, "Invalid clock table\n");
1520 goto out;
1521 }
1522 pdata->sup_clk_table = clk_table;
1523 pdata->sup_clk_cnt = clk_table_len;
1524
Asutosh Das0ef24812012-12-18 16:14:02 +05301525 pdata->vreg_data = devm_kzalloc(dev, sizeof(struct
1526 sdhci_msm_slot_reg_data),
1527 GFP_KERNEL);
1528 if (!pdata->vreg_data) {
1529 dev_err(dev, "failed to allocate memory for vreg data\n");
1530 goto out;
1531 }
1532
1533 if (sdhci_msm_dt_parse_vreg_info(dev, &pdata->vreg_data->vdd_data,
1534 "vdd")) {
1535 dev_err(dev, "failed parsing vdd data\n");
1536 goto out;
1537 }
1538 if (sdhci_msm_dt_parse_vreg_info(dev,
1539 &pdata->vreg_data->vdd_io_data,
1540 "vdd-io")) {
1541 dev_err(dev, "failed parsing vdd-io data\n");
1542 goto out;
1543 }
1544
1545 if (sdhci_msm_dt_parse_gpio_info(dev, pdata)) {
1546 dev_err(dev, "failed parsing gpio data\n");
1547 goto out;
1548 }
1549
Asutosh Das0ef24812012-12-18 16:14:02 +05301550 len = of_property_count_strings(np, "qcom,bus-speed-mode");
1551
1552 for (i = 0; i < len; i++) {
1553 const char *name = NULL;
1554
1555 of_property_read_string_index(np,
1556 "qcom,bus-speed-mode", i, &name);
1557 if (!name)
1558 continue;
1559
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07001560 if (!strncmp(name, "HS400_1p8v", sizeof("HS400_1p8v")))
1561 pdata->caps2 |= MMC_CAP2_HS400_1_8V;
1562 else if (!strncmp(name, "HS400_1p2v", sizeof("HS400_1p2v")))
1563 pdata->caps2 |= MMC_CAP2_HS400_1_2V;
1564 else if (!strncmp(name, "HS200_1p8v", sizeof("HS200_1p8v")))
Asutosh Das0ef24812012-12-18 16:14:02 +05301565 pdata->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
1566 else if (!strncmp(name, "HS200_1p2v", sizeof("HS200_1p2v")))
1567 pdata->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
1568 else if (!strncmp(name, "DDR_1p8v", sizeof("DDR_1p8v")))
1569 pdata->caps |= MMC_CAP_1_8V_DDR
1570 | MMC_CAP_UHS_DDR50;
1571 else if (!strncmp(name, "DDR_1p2v", sizeof("DDR_1p2v")))
1572 pdata->caps |= MMC_CAP_1_2V_DDR
1573 | MMC_CAP_UHS_DDR50;
1574 }
1575
1576 if (of_get_property(np, "qcom,nonremovable", NULL))
1577 pdata->nonremovable = true;
1578
Guoping Yuf7c91332014-08-20 16:56:18 +08001579 if (of_get_property(np, "qcom,nonhotplug", NULL))
1580 pdata->nonhotplug = true;
1581
Maya Erez994cf2f2014-10-21 20:22:04 +03001582 sdhci_msm_populate_affinity_type(pdata, np);
1583
Asutosh Das0ef24812012-12-18 16:14:02 +05301584 return pdata;
1585out:
1586 return NULL;
1587}
1588
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301589/* Returns required bandwidth in Bytes per Sec */
1590static unsigned int sdhci_get_bw_required(struct sdhci_host *host,
1591 struct mmc_ios *ios)
1592{
Sahitya Tummala2886c922013-04-03 18:03:31 +05301593 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1594 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1595
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301596 unsigned int bw;
1597
Sahitya Tummala2886c922013-04-03 18:03:31 +05301598 bw = msm_host->clk_rate;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301599 /*
1600 * For DDR mode, SDCC controller clock will be at
1601 * the double rate than the actual clock that goes to card.
1602 */
1603 if (ios->bus_width == MMC_BUS_WIDTH_4)
1604 bw /= 2;
1605 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1606 bw /= 8;
1607
1608 return bw;
1609}
1610
1611static int sdhci_msm_bus_get_vote_for_bw(struct sdhci_msm_host *host,
1612 unsigned int bw)
1613{
1614 unsigned int *table = host->pdata->voting_data->bw_vecs;
1615 unsigned int size = host->pdata->voting_data->bw_vecs_size;
1616 int i;
1617
1618 if (host->msm_bus_vote.is_max_bw_needed && bw)
1619 return host->msm_bus_vote.max_bw_vote;
1620
1621 for (i = 0; i < size; i++) {
1622 if (bw <= table[i])
1623 break;
1624 }
1625
1626 if (i && (i == size))
1627 i--;
1628
1629 return i;
1630}
1631
1632/*
1633 * This function must be called with host lock acquired.
1634 * Caller of this function should also ensure that msm bus client
1635 * handle is not null.
1636 */
1637static inline int sdhci_msm_bus_set_vote(struct sdhci_msm_host *msm_host,
1638 int vote,
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301639 unsigned long *flags)
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301640{
1641 struct sdhci_host *host = platform_get_drvdata(msm_host->pdev);
1642 int rc = 0;
1643
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301644 BUG_ON(!flags);
1645
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301646 if (vote != msm_host->msm_bus_vote.curr_vote) {
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301647 spin_unlock_irqrestore(&host->lock, *flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301648 rc = msm_bus_scale_client_update_request(
1649 msm_host->msm_bus_vote.client_handle, vote);
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301650 spin_lock_irqsave(&host->lock, *flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301651 if (rc) {
1652 pr_err("%s: msm_bus_scale_client_update_request() failed: bus_client_handle=0x%x, vote=%d, err=%d\n",
1653 mmc_hostname(host->mmc),
1654 msm_host->msm_bus_vote.client_handle, vote, rc);
1655 goto out;
1656 }
1657 msm_host->msm_bus_vote.curr_vote = vote;
1658 }
1659out:
1660 return rc;
1661}
1662
1663/*
1664 * Internal work. Work to set 0 bandwidth for msm bus.
1665 */
1666static void sdhci_msm_bus_work(struct work_struct *work)
1667{
1668 struct sdhci_msm_host *msm_host;
1669 struct sdhci_host *host;
1670 unsigned long flags;
1671
1672 msm_host = container_of(work, struct sdhci_msm_host,
1673 msm_bus_vote.vote_work.work);
1674 host = platform_get_drvdata(msm_host->pdev);
1675
1676 if (!msm_host->msm_bus_vote.client_handle)
1677 return;
1678
1679 spin_lock_irqsave(&host->lock, flags);
1680 /* don't vote for 0 bandwidth if any request is in progress */
1681 if (!host->mrq) {
1682 sdhci_msm_bus_set_vote(msm_host,
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301683 msm_host->msm_bus_vote.min_bw_vote, &flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301684 } else
1685 pr_warning("%s: %s: Transfer in progress. skipping bus voting to 0 bandwidth\n",
1686 mmc_hostname(host->mmc), __func__);
1687 spin_unlock_irqrestore(&host->lock, flags);
1688}
1689
1690/*
1691 * This function cancels any scheduled delayed work and sets the bus
1692 * vote based on bw (bandwidth) argument.
1693 */
1694static void sdhci_msm_bus_cancel_work_and_set_vote(struct sdhci_host *host,
1695 unsigned int bw)
1696{
1697 int vote;
1698 unsigned long flags;
1699 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1700 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1701
1702 cancel_delayed_work_sync(&msm_host->msm_bus_vote.vote_work);
1703 spin_lock_irqsave(&host->lock, flags);
1704 vote = sdhci_msm_bus_get_vote_for_bw(msm_host, bw);
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301705 sdhci_msm_bus_set_vote(msm_host, vote, &flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301706 spin_unlock_irqrestore(&host->lock, flags);
1707}
1708
1709#define MSM_MMC_BUS_VOTING_DELAY 200 /* msecs */
1710
1711/* This function queues a work which will set the bandwidth requiement to 0 */
1712static void sdhci_msm_bus_queue_work(struct sdhci_host *host)
1713{
1714 unsigned long flags;
1715 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1716 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1717
1718 spin_lock_irqsave(&host->lock, flags);
1719 if (msm_host->msm_bus_vote.min_bw_vote !=
1720 msm_host->msm_bus_vote.curr_vote)
1721 queue_delayed_work(system_wq,
1722 &msm_host->msm_bus_vote.vote_work,
1723 msecs_to_jiffies(MSM_MMC_BUS_VOTING_DELAY));
1724 spin_unlock_irqrestore(&host->lock, flags);
1725}
1726
1727static int sdhci_msm_bus_register(struct sdhci_msm_host *host,
1728 struct platform_device *pdev)
1729{
1730 int rc = 0;
1731 struct msm_bus_scale_pdata *bus_pdata;
1732
1733 struct sdhci_msm_bus_voting_data *data;
1734 struct device *dev = &pdev->dev;
1735
1736 data = devm_kzalloc(dev,
1737 sizeof(struct sdhci_msm_bus_voting_data), GFP_KERNEL);
1738 if (!data) {
1739 dev_err(&pdev->dev,
1740 "%s: failed to allocate memory\n", __func__);
1741 rc = -ENOMEM;
1742 goto out;
1743 }
1744 data->bus_pdata = msm_bus_cl_get_pdata(pdev);
1745 if (data->bus_pdata) {
1746 rc = sdhci_msm_dt_get_array(dev, "qcom,bus-bw-vectors-bps",
1747 &data->bw_vecs, &data->bw_vecs_size, 0);
1748 if (rc) {
1749 dev_err(&pdev->dev,
1750 "%s: Failed to get bus-bw-vectors-bps\n",
1751 __func__);
1752 goto out;
1753 }
1754 host->pdata->voting_data = data;
1755 }
1756 if (host->pdata->voting_data &&
1757 host->pdata->voting_data->bus_pdata &&
1758 host->pdata->voting_data->bw_vecs &&
1759 host->pdata->voting_data->bw_vecs_size) {
1760
1761 bus_pdata = host->pdata->voting_data->bus_pdata;
1762 host->msm_bus_vote.client_handle =
1763 msm_bus_scale_register_client(bus_pdata);
1764 if (!host->msm_bus_vote.client_handle) {
1765 dev_err(&pdev->dev, "msm_bus_scale_register_client()\n");
1766 rc = -EFAULT;
1767 goto out;
1768 }
1769 /* cache the vote index for minimum and maximum bandwidth */
1770 host->msm_bus_vote.min_bw_vote =
1771 sdhci_msm_bus_get_vote_for_bw(host, 0);
1772 host->msm_bus_vote.max_bw_vote =
1773 sdhci_msm_bus_get_vote_for_bw(host, UINT_MAX);
1774 } else {
1775 devm_kfree(dev, data);
1776 }
1777
1778out:
1779 return rc;
1780}
1781
1782static void sdhci_msm_bus_unregister(struct sdhci_msm_host *host)
1783{
1784 if (host->msm_bus_vote.client_handle)
1785 msm_bus_scale_unregister_client(
1786 host->msm_bus_vote.client_handle);
1787}
1788
1789static void sdhci_msm_bus_voting(struct sdhci_host *host, u32 enable)
1790{
1791 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1792 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1793 struct mmc_ios *ios = &host->mmc->ios;
1794 unsigned int bw;
1795
1796 if (!msm_host->msm_bus_vote.client_handle)
1797 return;
1798
1799 bw = sdhci_get_bw_required(host, ios);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301800 if (enable) {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301801 sdhci_msm_bus_cancel_work_and_set_vote(host, bw);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301802 } else {
1803 /*
1804 * If clock gating is enabled, then remove the vote
1805 * immediately because clocks will be disabled only
1806 * after SDHCI_MSM_MMC_CLK_GATE_DELAY and thus no
1807 * additional delay is required to remove the bus vote.
1808 */
1809#ifdef CONFIG_MMC_CLKGATE
1810 if (host->mmc->clkgate_delay)
1811 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
1812 else
1813#endif
1814 sdhci_msm_bus_queue_work(host);
1815 }
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301816}
1817
Asutosh Das0ef24812012-12-18 16:14:02 +05301818/* Regulator utility functions */
1819static int sdhci_msm_vreg_init_reg(struct device *dev,
1820 struct sdhci_msm_reg_data *vreg)
1821{
1822 int ret = 0;
1823
1824 /* check if regulator is already initialized? */
1825 if (vreg->reg)
1826 goto out;
1827
1828 /* Get the regulator handle */
1829 vreg->reg = devm_regulator_get(dev, vreg->name);
1830 if (IS_ERR(vreg->reg)) {
1831 ret = PTR_ERR(vreg->reg);
1832 pr_err("%s: devm_regulator_get(%s) failed. ret=%d\n",
1833 __func__, vreg->name, ret);
1834 goto out;
1835 }
1836
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301837 if (regulator_count_voltages(vreg->reg) > 0) {
1838 vreg->set_voltage_sup = true;
1839 /* sanity check */
1840 if (!vreg->high_vol_level || !vreg->hpm_uA) {
1841 pr_err("%s: %s invalid constraints specified\n",
1842 __func__, vreg->name);
1843 ret = -EINVAL;
1844 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301845 }
1846
1847out:
1848 return ret;
1849}
1850
1851static void sdhci_msm_vreg_deinit_reg(struct sdhci_msm_reg_data *vreg)
1852{
1853 if (vreg->reg)
1854 devm_regulator_put(vreg->reg);
1855}
1856
1857static int sdhci_msm_vreg_set_optimum_mode(struct sdhci_msm_reg_data
1858 *vreg, int uA_load)
1859{
1860 int ret = 0;
1861
1862 /*
1863 * regulators that do not support regulator_set_voltage also
1864 * do not support regulator_set_optimum_mode
1865 */
1866 if (vreg->set_voltage_sup) {
1867 ret = regulator_set_load(vreg->reg, uA_load);
1868 if (ret < 0)
1869 pr_err("%s: regulator_set_load(reg=%s,uA_load=%d) failed. ret=%d\n",
1870 __func__, vreg->name, uA_load, ret);
1871 else
1872 /*
1873 * regulator_set_load() can return non zero
1874 * value even for success case.
1875 */
1876 ret = 0;
1877 }
1878 return ret;
1879}
1880
1881static int sdhci_msm_vreg_set_voltage(struct sdhci_msm_reg_data *vreg,
1882 int min_uV, int max_uV)
1883{
1884 int ret = 0;
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301885 if (vreg->set_voltage_sup) {
1886 ret = regulator_set_voltage(vreg->reg, min_uV, max_uV);
1887 if (ret) {
1888 pr_err("%s: regulator_set_voltage(%s)failed. min_uV=%d,max_uV=%d,ret=%d\n",
Asutosh Das0ef24812012-12-18 16:14:02 +05301889 __func__, vreg->name, min_uV, max_uV, ret);
1890 }
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301891 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301892
1893 return ret;
1894}
1895
1896static int sdhci_msm_vreg_enable(struct sdhci_msm_reg_data *vreg)
1897{
1898 int ret = 0;
1899
1900 /* Put regulator in HPM (high power mode) */
1901 ret = sdhci_msm_vreg_set_optimum_mode(vreg, vreg->hpm_uA);
1902 if (ret < 0)
1903 return ret;
1904
1905 if (!vreg->is_enabled) {
1906 /* Set voltage level */
1907 ret = sdhci_msm_vreg_set_voltage(vreg, vreg->high_vol_level,
1908 vreg->high_vol_level);
1909 if (ret)
1910 return ret;
1911 }
1912 ret = regulator_enable(vreg->reg);
1913 if (ret) {
1914 pr_err("%s: regulator_enable(%s) failed. ret=%d\n",
1915 __func__, vreg->name, ret);
1916 return ret;
1917 }
1918 vreg->is_enabled = true;
1919 return ret;
1920}
1921
1922static int sdhci_msm_vreg_disable(struct sdhci_msm_reg_data *vreg)
1923{
1924 int ret = 0;
1925
1926 /* Never disable regulator marked as always_on */
1927 if (vreg->is_enabled && !vreg->is_always_on) {
1928 ret = regulator_disable(vreg->reg);
1929 if (ret) {
1930 pr_err("%s: regulator_disable(%s) failed. ret=%d\n",
1931 __func__, vreg->name, ret);
1932 goto out;
1933 }
1934 vreg->is_enabled = false;
1935
1936 ret = sdhci_msm_vreg_set_optimum_mode(vreg, 0);
1937 if (ret < 0)
1938 goto out;
1939
1940 /* Set min. voltage level to 0 */
1941 ret = sdhci_msm_vreg_set_voltage(vreg, 0, vreg->high_vol_level);
1942 if (ret)
1943 goto out;
1944 } else if (vreg->is_enabled && vreg->is_always_on) {
1945 if (vreg->lpm_sup) {
1946 /* Put always_on regulator in LPM (low power mode) */
1947 ret = sdhci_msm_vreg_set_optimum_mode(vreg,
1948 vreg->lpm_uA);
1949 if (ret < 0)
1950 goto out;
1951 }
1952 }
1953out:
1954 return ret;
1955}
1956
1957static int sdhci_msm_setup_vreg(struct sdhci_msm_pltfm_data *pdata,
1958 bool enable, bool is_init)
1959{
1960 int ret = 0, i;
1961 struct sdhci_msm_slot_reg_data *curr_slot;
1962 struct sdhci_msm_reg_data *vreg_table[2];
1963
1964 curr_slot = pdata->vreg_data;
1965 if (!curr_slot) {
1966 pr_debug("%s: vreg info unavailable,assuming the slot is powered by always on domain\n",
1967 __func__);
1968 goto out;
1969 }
1970
1971 vreg_table[0] = curr_slot->vdd_data;
1972 vreg_table[1] = curr_slot->vdd_io_data;
1973
1974 for (i = 0; i < ARRAY_SIZE(vreg_table); i++) {
1975 if (vreg_table[i]) {
1976 if (enable)
1977 ret = sdhci_msm_vreg_enable(vreg_table[i]);
1978 else
1979 ret = sdhci_msm_vreg_disable(vreg_table[i]);
1980 if (ret)
1981 goto out;
1982 }
1983 }
1984out:
1985 return ret;
1986}
1987
1988/*
1989 * Reset vreg by ensuring it is off during probe. A call
1990 * to enable vreg is needed to balance disable vreg
1991 */
1992static int sdhci_msm_vreg_reset(struct sdhci_msm_pltfm_data *pdata)
1993{
1994 int ret;
1995
1996 ret = sdhci_msm_setup_vreg(pdata, 1, true);
1997 if (ret)
1998 return ret;
1999 ret = sdhci_msm_setup_vreg(pdata, 0, true);
2000 return ret;
2001}
2002
2003/* This init function should be called only once for each SDHC slot */
2004static int sdhci_msm_vreg_init(struct device *dev,
2005 struct sdhci_msm_pltfm_data *pdata,
2006 bool is_init)
2007{
2008 int ret = 0;
2009 struct sdhci_msm_slot_reg_data *curr_slot;
2010 struct sdhci_msm_reg_data *curr_vdd_reg, *curr_vdd_io_reg;
2011
2012 curr_slot = pdata->vreg_data;
2013 if (!curr_slot)
2014 goto out;
2015
2016 curr_vdd_reg = curr_slot->vdd_data;
2017 curr_vdd_io_reg = curr_slot->vdd_io_data;
2018
2019 if (!is_init)
2020 /* Deregister all regulators from regulator framework */
2021 goto vdd_io_reg_deinit;
2022
2023 /*
2024 * Get the regulator handle from voltage regulator framework
2025 * and then try to set the voltage level for the regulator
2026 */
2027 if (curr_vdd_reg) {
2028 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_reg);
2029 if (ret)
2030 goto out;
2031 }
2032 if (curr_vdd_io_reg) {
2033 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_io_reg);
2034 if (ret)
2035 goto vdd_reg_deinit;
2036 }
2037 ret = sdhci_msm_vreg_reset(pdata);
2038 if (ret)
2039 dev_err(dev, "vreg reset failed (%d)\n", ret);
2040 goto out;
2041
2042vdd_io_reg_deinit:
2043 if (curr_vdd_io_reg)
2044 sdhci_msm_vreg_deinit_reg(curr_vdd_io_reg);
2045vdd_reg_deinit:
2046 if (curr_vdd_reg)
2047 sdhci_msm_vreg_deinit_reg(curr_vdd_reg);
2048out:
2049 return ret;
2050}
2051
2052
2053static int sdhci_msm_set_vdd_io_vol(struct sdhci_msm_pltfm_data *pdata,
2054 enum vdd_io_level level,
2055 unsigned int voltage_level)
2056{
2057 int ret = 0;
2058 int set_level;
2059 struct sdhci_msm_reg_data *vdd_io_reg;
2060
2061 if (!pdata->vreg_data)
2062 return ret;
2063
2064 vdd_io_reg = pdata->vreg_data->vdd_io_data;
2065 if (vdd_io_reg && vdd_io_reg->is_enabled) {
2066 switch (level) {
2067 case VDD_IO_LOW:
2068 set_level = vdd_io_reg->low_vol_level;
2069 break;
2070 case VDD_IO_HIGH:
2071 set_level = vdd_io_reg->high_vol_level;
2072 break;
2073 case VDD_IO_SET_LEVEL:
2074 set_level = voltage_level;
2075 break;
2076 default:
2077 pr_err("%s: invalid argument level = %d",
2078 __func__, level);
2079 ret = -EINVAL;
2080 return ret;
2081 }
2082 ret = sdhci_msm_vreg_set_voltage(vdd_io_reg, set_level,
2083 set_level);
2084 }
2085 return ret;
2086}
2087
2088static irqreturn_t sdhci_msm_pwr_irq(int irq, void *data)
2089{
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002090 struct sdhci_host *host = (struct sdhci_host *)data;
2091 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2092 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Das0ef24812012-12-18 16:14:02 +05302093 u8 irq_status = 0;
2094 u8 irq_ack = 0;
2095 int ret = 0;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302096 int pwr_state = 0, io_level = 0;
2097 unsigned long flags;
Asutosh Das0ef24812012-12-18 16:14:02 +05302098
2099 irq_status = readb_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
2100 pr_debug("%s: Received IRQ(%d), status=0x%x\n",
2101 mmc_hostname(msm_host->mmc), irq, irq_status);
2102
2103 /* Clear the interrupt */
2104 writeb_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
2105 /*
2106 * SDHC has core_mem and hc_mem device memory and these memory
2107 * addresses do not fall within 1KB region. Hence, any update to
2108 * core_mem address space would require an mb() to ensure this gets
2109 * completed before its next update to registers within hc_mem.
2110 */
2111 mb();
2112
2113 /* Handle BUS ON/OFF*/
2114 if (irq_status & CORE_PWRCTL_BUS_ON) {
2115 ret = sdhci_msm_setup_vreg(msm_host->pdata, true, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302116 if (!ret) {
Asutosh Das0ef24812012-12-18 16:14:02 +05302117 ret = sdhci_msm_setup_pins(msm_host->pdata, true);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302118 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
2119 VDD_IO_HIGH, 0);
2120 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302121 if (ret)
2122 irq_ack |= CORE_PWRCTL_BUS_FAIL;
2123 else
2124 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302125
2126 pwr_state = REQ_BUS_ON;
2127 io_level = REQ_IO_HIGH;
Asutosh Das0ef24812012-12-18 16:14:02 +05302128 }
2129 if (irq_status & CORE_PWRCTL_BUS_OFF) {
2130 ret = sdhci_msm_setup_vreg(msm_host->pdata, false, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302131 if (!ret) {
Asutosh Das0ef24812012-12-18 16:14:02 +05302132 ret = sdhci_msm_setup_pins(msm_host->pdata, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302133 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
2134 VDD_IO_LOW, 0);
2135 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302136 if (ret)
2137 irq_ack |= CORE_PWRCTL_BUS_FAIL;
2138 else
2139 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302140
2141 pwr_state = REQ_BUS_OFF;
2142 io_level = REQ_IO_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05302143 }
2144 /* Handle IO LOW/HIGH */
2145 if (irq_status & CORE_PWRCTL_IO_LOW) {
2146 /* Switch voltage Low */
2147 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_LOW, 0);
2148 if (ret)
2149 irq_ack |= CORE_PWRCTL_IO_FAIL;
2150 else
2151 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302152
2153 io_level = REQ_IO_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05302154 }
2155 if (irq_status & CORE_PWRCTL_IO_HIGH) {
2156 /* Switch voltage High */
2157 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_HIGH, 0);
2158 if (ret)
2159 irq_ack |= CORE_PWRCTL_IO_FAIL;
2160 else
2161 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302162
2163 io_level = REQ_IO_HIGH;
Asutosh Das0ef24812012-12-18 16:14:02 +05302164 }
2165
2166 /* ACK status to the core */
2167 writeb_relaxed(irq_ack, (msm_host->core_mem + CORE_PWRCTL_CTL));
2168 /*
2169 * SDHC has core_mem and hc_mem device memory and these memory
2170 * addresses do not fall within 1KB region. Hence, any update to
2171 * core_mem address space would require an mb() to ensure this gets
2172 * completed before its next update to registers within hc_mem.
2173 */
2174 mb();
2175
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302176 if (io_level & REQ_IO_HIGH)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002177 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2178 ~CORE_IO_PAD_PWR_SWITCH),
2179 host->ioaddr + CORE_VENDOR_SPEC);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302180 else if (io_level & REQ_IO_LOW)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002181 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
2182 CORE_IO_PAD_PWR_SWITCH),
2183 host->ioaddr + CORE_VENDOR_SPEC);
2184 mb();
2185
Asutosh Das0ef24812012-12-18 16:14:02 +05302186 pr_debug("%s: Handled IRQ(%d), ret=%d, ack=0x%x\n",
2187 mmc_hostname(msm_host->mmc), irq, ret, irq_ack);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302188 spin_lock_irqsave(&host->lock, flags);
2189 if (pwr_state)
2190 msm_host->curr_pwr_state = pwr_state;
2191 if (io_level)
2192 msm_host->curr_io_level = io_level;
2193 complete(&msm_host->pwr_irq_completion);
2194 spin_unlock_irqrestore(&host->lock, flags);
2195
Asutosh Das0ef24812012-12-18 16:14:02 +05302196 return IRQ_HANDLED;
2197}
2198
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302199static ssize_t
Sahitya Tummala5c55b932013-06-20 14:00:18 +05302200show_polling(struct device *dev, struct device_attribute *attr, char *buf)
2201{
2202 struct sdhci_host *host = dev_get_drvdata(dev);
2203 int poll;
2204 unsigned long flags;
2205
2206 spin_lock_irqsave(&host->lock, flags);
2207 poll = !!(host->mmc->caps & MMC_CAP_NEEDS_POLL);
2208 spin_unlock_irqrestore(&host->lock, flags);
2209
2210 return snprintf(buf, PAGE_SIZE, "%d\n", poll);
2211}
2212
2213static ssize_t
2214store_polling(struct device *dev, struct device_attribute *attr,
2215 const char *buf, size_t count)
2216{
2217 struct sdhci_host *host = dev_get_drvdata(dev);
2218 int value;
2219 unsigned long flags;
2220
2221 if (!kstrtou32(buf, 0, &value)) {
2222 spin_lock_irqsave(&host->lock, flags);
2223 if (value) {
2224 host->mmc->caps |= MMC_CAP_NEEDS_POLL;
2225 mmc_detect_change(host->mmc, 0);
2226 } else {
2227 host->mmc->caps &= ~MMC_CAP_NEEDS_POLL;
2228 }
2229 spin_unlock_irqrestore(&host->lock, flags);
2230 }
2231 return count;
2232}
2233
2234static ssize_t
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302235show_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
2236 char *buf)
2237{
2238 struct sdhci_host *host = dev_get_drvdata(dev);
2239 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2240 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2241
2242 return snprintf(buf, PAGE_SIZE, "%u\n",
2243 msm_host->msm_bus_vote.is_max_bw_needed);
2244}
2245
2246static ssize_t
2247store_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
2248 const char *buf, size_t count)
2249{
2250 struct sdhci_host *host = dev_get_drvdata(dev);
2251 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2252 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2253 uint32_t value;
2254 unsigned long flags;
2255
2256 if (!kstrtou32(buf, 0, &value)) {
2257 spin_lock_irqsave(&host->lock, flags);
2258 msm_host->msm_bus_vote.is_max_bw_needed = !!value;
2259 spin_unlock_irqrestore(&host->lock, flags);
2260 }
2261 return count;
2262}
2263
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302264static void sdhci_msm_check_power_status(struct sdhci_host *host, u32 req_type)
Asutosh Das0ef24812012-12-18 16:14:02 +05302265{
2266 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2267 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302268 unsigned long flags;
2269 bool done = false;
Sahitya Tummala481fbb02013-08-06 15:22:28 +05302270 u32 io_sig_sts;
Asutosh Das0ef24812012-12-18 16:14:02 +05302271
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302272 spin_lock_irqsave(&host->lock, flags);
2273 pr_debug("%s: %s: request %d curr_pwr_state %x curr_io_level %x\n",
2274 mmc_hostname(host->mmc), __func__, req_type,
2275 msm_host->curr_pwr_state, msm_host->curr_io_level);
Sahitya Tummala481fbb02013-08-06 15:22:28 +05302276 io_sig_sts = readl_relaxed(msm_host->core_mem + CORE_GENERICS);
2277 /*
2278 * The IRQ for request type IO High/Low will be generated when -
2279 * 1. SWITCHABLE_SIGNALLING_VOL is enabled in HW.
2280 * 2. If 1 is true and when there is a state change in 1.8V enable
2281 * bit (bit 3) of SDHCI_HOST_CONTROL2 register. The reset state of
2282 * that bit is 0 which indicates 3.3V IO voltage. So, when MMC core
2283 * layer tries to set it to 3.3V before card detection happens, the
2284 * IRQ doesn't get triggered as there is no state change in this bit.
2285 * The driver already handles this case by changing the IO voltage
2286 * level to high as part of controller power up sequence. Hence, check
2287 * for host->pwr to handle a case where IO voltage high request is
2288 * issued even before controller power up.
2289 */
2290 if (req_type & (REQ_IO_HIGH | REQ_IO_LOW)) {
2291 if (!(io_sig_sts & SWITCHABLE_SIGNALLING_VOL) ||
2292 ((req_type & REQ_IO_HIGH) && !host->pwr)) {
2293 pr_debug("%s: do not wait for power IRQ that never comes\n",
2294 mmc_hostname(host->mmc));
2295 spin_unlock_irqrestore(&host->lock, flags);
2296 return;
2297 }
2298 }
2299
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302300 if ((req_type & msm_host->curr_pwr_state) ||
2301 (req_type & msm_host->curr_io_level))
2302 done = true;
2303 spin_unlock_irqrestore(&host->lock, flags);
Asutosh Das0ef24812012-12-18 16:14:02 +05302304
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302305 /*
2306 * This is needed here to hanlde a case where IRQ gets
2307 * triggered even before this function is called so that
2308 * x->done counter of completion gets reset. Otherwise,
2309 * next call to wait_for_completion returns immediately
2310 * without actually waiting for the IRQ to be handled.
2311 */
2312 if (done)
2313 init_completion(&msm_host->pwr_irq_completion);
2314 else
2315 wait_for_completion(&msm_host->pwr_irq_completion);
2316
2317 pr_debug("%s: %s: request %d done\n", mmc_hostname(host->mmc),
2318 __func__, req_type);
Asutosh Das0ef24812012-12-18 16:14:02 +05302319}
2320
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002321static void sdhci_msm_toggle_cdr(struct sdhci_host *host, bool enable)
2322{
2323 if (enable)
2324 writel_relaxed((readl_relaxed(host->ioaddr +
2325 CORE_DLL_CONFIG) | CORE_CDR_EN),
2326 host->ioaddr + CORE_DLL_CONFIG);
2327 else
2328 writel_relaxed((readl_relaxed(host->ioaddr +
2329 CORE_DLL_CONFIG) & ~CORE_CDR_EN),
2330 host->ioaddr + CORE_DLL_CONFIG);
2331}
2332
Asutosh Das648f9d12013-01-10 21:11:04 +05302333static unsigned int sdhci_msm_max_segs(void)
2334{
2335 return SDHCI_MSM_MAX_SEGMENTS;
2336}
2337
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302338static unsigned int sdhci_msm_get_min_clock(struct sdhci_host *host)
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302339{
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302340 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2341 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302342
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302343 return msm_host->pdata->sup_clk_table[0];
2344}
2345
2346static unsigned int sdhci_msm_get_max_clock(struct sdhci_host *host)
2347{
2348 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2349 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2350 int max_clk_index = msm_host->pdata->sup_clk_cnt;
2351
2352 return msm_host->pdata->sup_clk_table[max_clk_index - 1];
2353}
2354
2355static unsigned int sdhci_msm_get_sup_clk_rate(struct sdhci_host *host,
2356 u32 req_clk)
2357{
2358 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2359 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2360 unsigned int sel_clk = -1;
2361 unsigned char cnt;
2362
2363 if (req_clk < sdhci_msm_get_min_clock(host)) {
2364 sel_clk = sdhci_msm_get_min_clock(host);
2365 return sel_clk;
2366 }
2367
2368 for (cnt = 0; cnt < msm_host->pdata->sup_clk_cnt; cnt++) {
2369 if (msm_host->pdata->sup_clk_table[cnt] > req_clk) {
2370 break;
2371 } else if (msm_host->pdata->sup_clk_table[cnt] == req_clk) {
2372 sel_clk = msm_host->pdata->sup_clk_table[cnt];
2373 break;
2374 } else {
2375 sel_clk = msm_host->pdata->sup_clk_table[cnt];
2376 }
2377 }
2378 return sel_clk;
2379}
2380
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302381static int sdhci_msm_enable_controller_clock(struct sdhci_host *host)
2382{
2383 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2384 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2385 int rc = 0;
2386
2387 if (atomic_read(&msm_host->controller_clock))
2388 return 0;
2389
2390 sdhci_msm_bus_voting(host, 1);
2391
2392 if (!IS_ERR(msm_host->pclk)) {
2393 rc = clk_prepare_enable(msm_host->pclk);
2394 if (rc) {
2395 pr_err("%s: %s: failed to enable the pclk with error %d\n",
2396 mmc_hostname(host->mmc), __func__, rc);
2397 goto remove_vote;
2398 }
2399 }
2400
2401 rc = clk_prepare_enable(msm_host->clk);
2402 if (rc) {
2403 pr_err("%s: %s: failed to enable the host-clk with error %d\n",
2404 mmc_hostname(host->mmc), __func__, rc);
2405 goto disable_pclk;
2406 }
2407
2408 atomic_set(&msm_host->controller_clock, 1);
2409 pr_debug("%s: %s: enabled controller clock\n",
2410 mmc_hostname(host->mmc), __func__);
2411 goto out;
2412
2413disable_pclk:
2414 if (!IS_ERR(msm_host->pclk))
2415 clk_disable_unprepare(msm_host->pclk);
2416remove_vote:
2417 if (msm_host->msm_bus_vote.client_handle)
2418 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2419out:
2420 return rc;
2421}
2422
2423
2424
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302425static int sdhci_msm_prepare_clocks(struct sdhci_host *host, bool enable)
2426{
2427 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2428 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2429 int rc = 0;
2430
2431 if (enable && !atomic_read(&msm_host->clks_on)) {
2432 pr_debug("%s: request to enable clocks\n",
2433 mmc_hostname(host->mmc));
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302434
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302435 /*
2436 * The bus-width or the clock rate might have changed
2437 * after controller clocks are enbaled, update bus vote
2438 * in such case.
2439 */
2440 if (atomic_read(&msm_host->controller_clock))
2441 sdhci_msm_bus_voting(host, 1);
2442
2443 rc = sdhci_msm_enable_controller_clock(host);
2444 if (rc)
2445 goto remove_vote;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302446
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302447 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
2448 rc = clk_prepare_enable(msm_host->bus_clk);
2449 if (rc) {
2450 pr_err("%s: %s: failed to enable the bus-clock with error %d\n",
2451 mmc_hostname(host->mmc), __func__, rc);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302452 goto disable_controller_clk;
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302453 }
2454 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002455 if (!IS_ERR(msm_host->ff_clk)) {
2456 rc = clk_prepare_enable(msm_host->ff_clk);
2457 if (rc) {
2458 pr_err("%s: %s: failed to enable the ff_clk with error %d\n",
2459 mmc_hostname(host->mmc), __func__, rc);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302460 goto disable_bus_clk;
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002461 }
2462 }
2463 if (!IS_ERR(msm_host->sleep_clk)) {
2464 rc = clk_prepare_enable(msm_host->sleep_clk);
2465 if (rc) {
2466 pr_err("%s: %s: failed to enable the sleep_clk with error %d\n",
2467 mmc_hostname(host->mmc), __func__, rc);
2468 goto disable_ff_clk;
2469 }
2470 }
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302471 mb();
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302472
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302473 } else if (!enable && atomic_read(&msm_host->clks_on)) {
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302474 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
2475 mb();
Sahitya Tummaladc182982013-08-20 15:32:09 +05302476 /*
2477 * During 1.8V signal switching the clock source must
2478 * still be ON as it requires accessing SDHC
2479 * registers (SDHCi host control2 register bit 3 must
2480 * be written and polled after stopping the SDCLK).
2481 */
2482 if (host->mmc->card_clock_off)
2483 return 0;
2484 pr_debug("%s: request to disable clocks\n",
2485 mmc_hostname(host->mmc));
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002486 if (!IS_ERR_OR_NULL(msm_host->sleep_clk))
2487 clk_disable_unprepare(msm_host->sleep_clk);
2488 if (!IS_ERR_OR_NULL(msm_host->ff_clk))
2489 clk_disable_unprepare(msm_host->ff_clk);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302490 clk_disable_unprepare(msm_host->clk);
2491 if (!IS_ERR(msm_host->pclk))
2492 clk_disable_unprepare(msm_host->pclk);
2493 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2494 clk_disable_unprepare(msm_host->bus_clk);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302495
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302496 atomic_set(&msm_host->controller_clock, 0);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302497 sdhci_msm_bus_voting(host, 0);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302498 }
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302499 atomic_set(&msm_host->clks_on, enable);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302500 goto out;
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002501disable_ff_clk:
2502 if (!IS_ERR_OR_NULL(msm_host->ff_clk))
2503 clk_disable_unprepare(msm_host->ff_clk);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302504disable_bus_clk:
2505 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2506 clk_disable_unprepare(msm_host->bus_clk);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302507disable_controller_clk:
2508 if (!IS_ERR_OR_NULL(msm_host->clk))
2509 clk_disable_unprepare(msm_host->clk);
2510 if (!IS_ERR_OR_NULL(msm_host->pclk))
2511 clk_disable_unprepare(msm_host->pclk);
2512 atomic_set(&msm_host->controller_clock, 0);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302513remove_vote:
2514 if (msm_host->msm_bus_vote.client_handle)
2515 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302516out:
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302517 return rc;
2518}
2519
2520static void sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock)
2521{
2522 int rc;
2523 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2524 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2525 struct mmc_ios curr_ios = host->mmc->ios;
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002526 u32 sup_clock, ddr_clock, dll_lock;
Sahitya Tummala043744a2013-06-24 09:55:33 +05302527 bool curr_pwrsave;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302528
2529 if (!clock) {
Sujit Reddy Thummabf1aecc2014-01-10 10:58:54 +05302530 /*
2531 * disable pwrsave to ensure clock is not auto-gated until
2532 * the rate is >400KHz (initialization complete).
2533 */
2534 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2535 ~CORE_CLK_PWRSAVE, host->ioaddr + CORE_VENDOR_SPEC);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302536 sdhci_msm_prepare_clocks(host, false);
2537 host->clock = clock;
2538 goto out;
2539 }
2540
2541 rc = sdhci_msm_prepare_clocks(host, true);
2542 if (rc)
2543 goto out;
2544
Sahitya Tummala043744a2013-06-24 09:55:33 +05302545 curr_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2546 CORE_CLK_PWRSAVE);
Sahitya Tummalae000b242013-08-29 16:21:08 +05302547 if ((clock > 400000) &&
Venkat Gopalakrishnane6659b92015-01-21 15:51:37 -08002548 !curr_pwrsave)
Sahitya Tummala043744a2013-06-24 09:55:33 +05302549 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2550 | CORE_CLK_PWRSAVE,
2551 host->ioaddr + CORE_VENDOR_SPEC);
2552 /*
2553 * Disable pwrsave for a newly added card if doesn't allow clock
2554 * gating.
2555 */
Venkat Gopalakrishnane6659b92015-01-21 15:51:37 -08002556 else if (curr_pwrsave)
Sahitya Tummala043744a2013-06-24 09:55:33 +05302557 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2558 & ~CORE_CLK_PWRSAVE,
2559 host->ioaddr + CORE_VENDOR_SPEC);
2560
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302561 sup_clock = sdhci_msm_get_sup_clk_rate(host, clock);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002562 if ((curr_ios.timing == MMC_TIMING_UHS_DDR50) ||
Venkat Gopalakrishnan0a29da92015-01-09 12:19:16 -08002563 (curr_ios.timing == MMC_TIMING_MMC_DDR52) ||
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002564 (curr_ios.timing == MMC_TIMING_MMC_HS400)) {
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302565 /*
2566 * The SDHC requires internal clock frequency to be double the
2567 * actual clock that will be set for DDR mode. The controller
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002568 * uses the faster clock(100/400MHz) for some of its parts and
2569 * send the actual required clock (50/200MHz) to the card.
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302570 */
2571 ddr_clock = clock * 2;
2572 sup_clock = sdhci_msm_get_sup_clk_rate(host,
2573 ddr_clock);
2574 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002575
2576 /*
2577 * In general all timing modes are controlled via UHS mode select in
2578 * Host Control2 register. eMMC specific HS200/HS400 doesn't have
2579 * their respective modes defined here, hence we use these values.
2580 *
2581 * HS200 - SDR104 (Since they both are equivalent in functionality)
2582 * HS400 - This involves multiple configurations
2583 * Initially SDR104 - when tuning is required as HS200
2584 * Then when switching to DDR @ 400MHz (HS400) we use
2585 * the vendor specific HC_SELECT_IN to control the mode.
2586 *
2587 * In addition to controlling the modes we also need to select the
2588 * correct input clock for DLL depending on the mode.
2589 *
2590 * HS400 - divided clock (free running MCLK/2)
2591 * All other modes - default (free running MCLK)
2592 */
2593 if (curr_ios.timing == MMC_TIMING_MMC_HS400) {
2594 /* Select the divided clock (free running MCLK/2) */
2595 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2596 & ~CORE_HC_MCLK_SEL_MASK)
2597 | CORE_HC_MCLK_SEL_HS400),
2598 host->ioaddr + CORE_VENDOR_SPEC);
2599 /*
2600 * Select HS400 mode using the HC_SELECT_IN from VENDOR SPEC
2601 * register
2602 */
2603 if (msm_host->tuning_done && !msm_host->calibration_done) {
2604 /*
2605 * Write 0x6 to HC_SELECT_IN and 1 to HC_SELECT_IN_EN
2606 * field in VENDOR_SPEC_FUNC
2607 */
2608 writel_relaxed((readl_relaxed(host->ioaddr + \
2609 CORE_VENDOR_SPEC)
2610 | CORE_HC_SELECT_IN_HS400
2611 | CORE_HC_SELECT_IN_EN),
2612 host->ioaddr + CORE_VENDOR_SPEC);
2613 }
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002614 if (!host->mmc->ios.old_rate && !msm_host->use_cdclp533) {
2615 /*
2616 * Poll on DLL_LOCK and DDR_DLL_LOCK bits in
2617 * CORE_DLL_STATUS to be set. This should get set
2618 * with in 15 us at 200 MHz.
2619 */
2620 rc = readl_poll_timeout(host->ioaddr + CORE_DLL_STATUS,
2621 dll_lock, (dll_lock & (CORE_DLL_LOCK |
2622 CORE_DDR_DLL_LOCK)), 10, 1000);
2623 if (rc == -ETIMEDOUT)
2624 pr_err("%s: Unable to get DLL_LOCK/DDR_DLL_LOCK, dll_status: 0x%08x\n",
2625 mmc_hostname(host->mmc),
2626 dll_lock);
2627 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002628 } else {
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002629 if (!msm_host->use_cdclp533)
2630 /* set CORE_PWRSAVE_DLL bit in CORE_VENDOR_SPEC3 */
2631 writel_relaxed((readl_relaxed(host->ioaddr +
2632 CORE_VENDOR_SPEC3) & ~CORE_PWRSAVE_DLL),
2633 host->ioaddr + CORE_VENDOR_SPEC3);
2634
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002635 /* Select the default clock (free running MCLK) */
2636 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2637 & ~CORE_HC_MCLK_SEL_MASK)
2638 | CORE_HC_MCLK_SEL_DFLT),
2639 host->ioaddr + CORE_VENDOR_SPEC);
2640
2641 /*
2642 * Disable HC_SELECT_IN to be able to use the UHS mode select
2643 * configuration from Host Control2 register for all other
2644 * modes.
2645 *
2646 * Write 0 to HC_SELECT_IN and HC_SELECT_IN_EN field
2647 * in VENDOR_SPEC_FUNC
2648 */
2649 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2650 & ~CORE_HC_SELECT_IN_EN
2651 & ~CORE_HC_SELECT_IN_MASK),
2652 host->ioaddr + CORE_VENDOR_SPEC);
2653 }
2654 mb();
2655
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302656 if (sup_clock != msm_host->clk_rate) {
2657 pr_debug("%s: %s: setting clk rate to %u\n",
2658 mmc_hostname(host->mmc), __func__, sup_clock);
2659 rc = clk_set_rate(msm_host->clk, sup_clock);
2660 if (rc) {
2661 pr_err("%s: %s: Failed to set rate %u for host-clk : %d\n",
2662 mmc_hostname(host->mmc), __func__,
2663 sup_clock, rc);
2664 goto out;
2665 }
2666 msm_host->clk_rate = sup_clock;
2667 host->clock = clock;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302668 /*
2669 * Update the bus vote in case of frequency change due to
2670 * clock scaling.
2671 */
2672 sdhci_msm_bus_voting(host, 1);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302673 }
2674out:
2675 sdhci_set_clock(host, clock);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302676}
2677
Sahitya Tummala14613432013-03-21 11:13:25 +05302678static void sdhci_msm_set_uhs_signaling(struct sdhci_host *host,
2679 unsigned int uhs)
2680{
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002681 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2682 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala14613432013-03-21 11:13:25 +05302683 u16 ctrl_2;
2684
2685 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2686 /* Select Bus Speed Mode for host */
2687 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Venkat Gopalakrishnan0a29da92015-01-09 12:19:16 -08002688 if ((uhs == MMC_TIMING_MMC_HS400) ||
2689 (uhs == MMC_TIMING_MMC_HS200) ||
2690 (uhs == MMC_TIMING_UHS_SDR104))
Sahitya Tummala14613432013-03-21 11:13:25 +05302691 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2692 else if (uhs == MMC_TIMING_UHS_SDR12)
2693 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
2694 else if (uhs == MMC_TIMING_UHS_SDR25)
2695 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
2696 else if (uhs == MMC_TIMING_UHS_SDR50)
2697 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
Venkat Gopalakrishnan0a29da92015-01-09 12:19:16 -08002698 else if ((uhs == MMC_TIMING_UHS_DDR50) ||
2699 (uhs == MMC_TIMING_MMC_DDR52))
Sahitya Tummala14613432013-03-21 11:13:25 +05302700 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302701 /*
2702 * When clock frquency is less than 100MHz, the feedback clock must be
2703 * provided and DLL must not be used so that tuning can be skipped. To
2704 * provide feedback clock, the mode selection can be any value less
2705 * than 3'b011 in bits [2:0] of HOST CONTROL2 register.
2706 */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002707 if (host->clock <= CORE_FREQ_100MHZ) {
2708 if ((uhs == MMC_TIMING_MMC_HS400) ||
2709 (uhs == MMC_TIMING_MMC_HS200) ||
2710 (uhs == MMC_TIMING_UHS_SDR104))
2711 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302712
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002713 /*
2714 * Make sure DLL is disabled when not required
2715 *
2716 * Write 1 to DLL_RST bit of DLL_CONFIG register
2717 */
2718 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
2719 | CORE_DLL_RST),
2720 host->ioaddr + CORE_DLL_CONFIG);
2721
2722 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
2723 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
2724 | CORE_DLL_PDN),
2725 host->ioaddr + CORE_DLL_CONFIG);
2726 mb();
2727
2728 /*
2729 * The DLL needs to be restored and CDCLP533 recalibrated
2730 * when the clock frequency is set back to 400MHz.
2731 */
2732 msm_host->calibration_done = false;
2733 }
2734
2735 pr_debug("%s: %s-clock:%u uhs mode:%u ctrl_2:0x%x\n",
2736 mmc_hostname(host->mmc), __func__, host->clock, uhs, ctrl_2);
Sahitya Tummala14613432013-03-21 11:13:25 +05302737 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
2738
2739}
2740
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302741#define MAX_TEST_BUS 20
2742
2743void sdhci_msm_dump_vendor_regs(struct sdhci_host *host)
2744{
2745 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2746 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2747 int tbsel, tbsel2;
2748 int i, index = 0;
2749 u32 test_bus_val = 0;
2750 u32 debug_reg[MAX_TEST_BUS] = {0};
2751
2752 pr_info("----------- VENDOR REGISTER DUMP -----------\n");
2753 pr_info("Data cnt: 0x%08x | Fifo cnt: 0x%08x | Int sts: 0x%08x\n",
2754 readl_relaxed(msm_host->core_mem + CORE_MCI_DATA_CNT),
2755 readl_relaxed(msm_host->core_mem + CORE_MCI_FIFO_CNT),
2756 readl_relaxed(msm_host->core_mem + CORE_MCI_STATUS));
2757 pr_info("DLL cfg: 0x%08x | DLL sts: 0x%08x | SDCC ver: 0x%08x\n",
2758 readl_relaxed(host->ioaddr + CORE_DLL_CONFIG),
2759 readl_relaxed(host->ioaddr + CORE_DLL_STATUS),
2760 readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION));
2761 pr_info("Vndr func: 0x%08x | Vndr adma err : addr0: 0x%08x addr1: 0x%08x\n",
2762 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC),
2763 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_ADMA_ERR_ADDR0),
2764 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_ADMA_ERR_ADDR1));
2765
2766 /*
2767 * tbsel indicates [2:0] bits and tbsel2 indicates [7:4] bits
2768 * of CORE_TESTBUS_CONFIG register.
2769 *
2770 * To select test bus 0 to 7 use tbsel and to select any test bus
2771 * above 7 use (tbsel2 | tbsel) to get the test bus number. For eg,
2772 * to select test bus 14, write 0x1E to CORE_TESTBUS_CONFIG register
2773 * i.e., tbsel2[7:4] = 0001, tbsel[2:0] = 110.
2774 */
2775 for (tbsel2 = 0; tbsel2 < 3; tbsel2++) {
2776 for (tbsel = 0; tbsel < 8; tbsel++) {
2777 if (index >= MAX_TEST_BUS)
2778 break;
2779 test_bus_val = (tbsel2 << CORE_TESTBUS_SEL2_BIT) |
2780 tbsel | CORE_TESTBUS_ENA;
2781 writel_relaxed(test_bus_val,
2782 msm_host->core_mem + CORE_TESTBUS_CONFIG);
2783 debug_reg[index++] = readl_relaxed(msm_host->core_mem +
2784 CORE_SDCC_DEBUG_REG);
2785 }
2786 }
2787 for (i = 0; i < MAX_TEST_BUS; i = i + 4)
2788 pr_info(" Test bus[%d to %d]: 0x%08x 0x%08x 0x%08x 0x%08x\n",
2789 i, i + 3, debug_reg[i], debug_reg[i+1],
2790 debug_reg[i+2], debug_reg[i+3]);
2791 /* Disable test bus */
2792 writel_relaxed(~CORE_TESTBUS_ENA, msm_host->core_mem +
2793 CORE_TESTBUS_CONFIG);
2794}
2795
Asutosh Das0ef24812012-12-18 16:14:02 +05302796static struct sdhci_ops sdhci_msm_ops = {
Sahitya Tummala14613432013-03-21 11:13:25 +05302797 .set_uhs_signaling = sdhci_msm_set_uhs_signaling,
Asutosh Das0ef24812012-12-18 16:14:02 +05302798 .check_power_status = sdhci_msm_check_power_status,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002799 .platform_execute_tuning = sdhci_msm_execute_tuning,
2800 .toggle_cdr = sdhci_msm_toggle_cdr,
Asutosh Das648f9d12013-01-10 21:11:04 +05302801 .get_max_segments = sdhci_msm_max_segs,
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302802 .set_clock = sdhci_msm_set_clock,
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302803 .get_min_clock = sdhci_msm_get_min_clock,
2804 .get_max_clock = sdhci_msm_get_max_clock,
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302805 .dump_vendor_regs = sdhci_msm_dump_vendor_regs,
Asutosh Dase5e9ca62013-07-30 19:08:36 +05302806 .config_auto_tuning_cmd = sdhci_msm_config_auto_tuning_cmd,
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302807 .enable_controller_clock = sdhci_msm_enable_controller_clock,
Venkat Gopalakrishnanb8cb7072015-01-09 11:04:34 -08002808 .set_bus_width = sdhci_set_bus_width,
Venkat Gopalakrishnan411df072015-01-09 11:09:44 -08002809 .reset = sdhci_reset,
Asutosh Das0ef24812012-12-18 16:14:02 +05302810};
2811
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302812static void sdhci_set_default_hw_caps(struct sdhci_msm_host *msm_host,
2813 struct sdhci_host *host)
2814{
2815 u32 version, caps;
2816 u16 minor;
2817 u8 major;
2818
2819 version = readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION);
2820 major = (version & CORE_VERSION_MAJOR_MASK) >>
2821 CORE_VERSION_MAJOR_SHIFT;
2822 minor = version & CORE_VERSION_TARGET_MASK;
2823
2824 /*
2825 * Starting with SDCC 5 controller (core major version = 1)
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002826 * controller won't advertise 3.0v, 1.8v and 8-bit features
2827 * except for some targets.
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302828 */
2829 if (major >= 1 && minor != 0x11 && minor != 0x12) {
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002830 struct sdhci_msm_reg_data *vdd_io_reg;
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302831 caps = CORE_3_0V_SUPPORT;
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002832 /*
2833 * Enable 1.8V support capability on controllers that
2834 * support dual voltage
2835 */
2836 vdd_io_reg = msm_host->pdata->vreg_data->vdd_io_data;
2837 if (vdd_io_reg &&
2838 (vdd_io_reg->low_vol_level != vdd_io_reg->high_vol_level))
2839 caps |= CORE_1_8V_SUPPORT;
Pratibhasagar Vada47992013-12-09 20:42:32 +05302840 if (msm_host->pdata->mmc_bus_width == MMC_CAP_8_BIT_DATA)
2841 caps |= CORE_8_BIT_SUPPORT;
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302842 writel_relaxed(
2843 (readl_relaxed(host->ioaddr + SDHCI_CAPABILITIES) |
2844 caps), host->ioaddr + CORE_VENDOR_SPEC_CAPABILITIES0);
2845 }
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002846
2847 /*
2848 * SDCC 5 controller with major version 1, minor version 0x34 and later
2849 * with HS 400 mode support will use CM DLL instead of CDC LP 533 DLL.
2850 */
2851 if ((major == 1) && (minor < 0x34))
2852 msm_host->use_cdclp533 = true;
Gilad Broner2a10ca02014-10-02 17:20:35 +03002853
2854 /*
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -08002855 * SDCC 5 controller with major version 1, minor version 0x42 and later
2856 * will require additional steps when resetting DLL.
2857 */
2858 if ((major == 1) && (minor >= 0x42))
2859 msm_host->use_updated_dll_reset = true;
2860
2861 /*
Gilad Broner2a10ca02014-10-02 17:20:35 +03002862 * Mask 64-bit support for controller with 32-bit address bus so that
2863 * smaller descriptor size will be used and improve memory consumption.
2864 * In case bus addressing ever changes, controller version should be
2865 * used in order to decide whether or not to mask 64-bit support.
2866 */
2867 caps = readl_relaxed(host->ioaddr + SDHCI_CAPABILITIES);
2868 caps &= ~CORE_SYS_BUS_SUPPORT_64_BIT;
2869 writel_relaxed(caps, host->ioaddr + CORE_VENDOR_SPEC_CAPABILITIES0);
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302870}
2871
Asutosh Das0ef24812012-12-18 16:14:02 +05302872static int sdhci_msm_probe(struct platform_device *pdev)
2873{
2874 struct sdhci_host *host;
2875 struct sdhci_pltfm_host *pltfm_host;
2876 struct sdhci_msm_host *msm_host;
2877 struct resource *core_memres = NULL;
2878 int ret = 0, pwr_irq = 0, dead = 0;
Stephen Boyd8dce5c62013-04-24 14:19:46 -07002879 u16 host_version;
Subhash Jadavani28137342013-05-14 17:46:43 +05302880 u32 pwr, irq_status, irq_ctl;
Asutosh Das0ef24812012-12-18 16:14:02 +05302881
2882 pr_debug("%s: Enter %s\n", dev_name(&pdev->dev), __func__);
2883 msm_host = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_msm_host),
2884 GFP_KERNEL);
2885 if (!msm_host) {
2886 ret = -ENOMEM;
2887 goto out;
2888 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302889
2890 msm_host->sdhci_msm_pdata.ops = &sdhci_msm_ops;
2891 host = sdhci_pltfm_init(pdev, &msm_host->sdhci_msm_pdata, 0);
2892 if (IS_ERR(host)) {
2893 ret = PTR_ERR(host);
2894 goto out;
2895 }
2896
2897 pltfm_host = sdhci_priv(host);
2898 pltfm_host->priv = msm_host;
2899 msm_host->mmc = host->mmc;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302900 msm_host->pdev = pdev;
Asutosh Das0ef24812012-12-18 16:14:02 +05302901
2902 /* Extract platform data */
2903 if (pdev->dev.of_node) {
Venkat Gopalakrishnan270580a2013-03-11 12:17:57 -07002904 ret = of_alias_get_id(pdev->dev.of_node, "sdhc");
2905 if (ret < 0) {
2906 dev_err(&pdev->dev, "Failed to get slot index %d\n",
2907 ret);
2908 goto pltfm_free;
2909 }
2910 if (disable_slots & (1 << (ret - 1))) {
2911 dev_info(&pdev->dev, "%s: Slot %d disabled\n", __func__,
2912 ret);
2913 ret = -ENODEV;
2914 goto pltfm_free;
2915 }
2916
Asutosh Das0ef24812012-12-18 16:14:02 +05302917 msm_host->pdata = sdhci_msm_populate_pdata(&pdev->dev);
2918 if (!msm_host->pdata) {
2919 dev_err(&pdev->dev, "DT parsing error\n");
2920 goto pltfm_free;
2921 }
2922 } else {
2923 dev_err(&pdev->dev, "No device tree node\n");
2924 goto pltfm_free;
2925 }
2926
2927 /* Setup Clocks */
2928
2929 /* Setup SDCC bus voter clock. */
2930 msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus_clk");
2931 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
2932 /* Vote for max. clk rate for max. performance */
2933 ret = clk_set_rate(msm_host->bus_clk, INT_MAX);
2934 if (ret)
2935 goto pltfm_free;
2936 ret = clk_prepare_enable(msm_host->bus_clk);
2937 if (ret)
2938 goto pltfm_free;
2939 }
2940
2941 /* Setup main peripheral bus clock */
2942 msm_host->pclk = devm_clk_get(&pdev->dev, "iface_clk");
2943 if (!IS_ERR(msm_host->pclk)) {
2944 ret = clk_prepare_enable(msm_host->pclk);
2945 if (ret)
2946 goto bus_clk_disable;
2947 }
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302948 atomic_set(&msm_host->controller_clock, 1);
Asutosh Das0ef24812012-12-18 16:14:02 +05302949
2950 /* Setup SDC MMC clock */
2951 msm_host->clk = devm_clk_get(&pdev->dev, "core_clk");
2952 if (IS_ERR(msm_host->clk)) {
2953 ret = PTR_ERR(msm_host->clk);
2954 goto pclk_disable;
2955 }
2956
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302957 /* Set to the minimum supported clock frequency */
2958 ret = clk_set_rate(msm_host->clk, sdhci_msm_get_min_clock(host));
2959 if (ret) {
2960 dev_err(&pdev->dev, "MClk rate set failed (%d)\n", ret);
Sahitya Tummala020ede0d2013-06-07 13:03:07 +05302961 goto pclk_disable;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302962 }
Sahitya Tummala020ede0d2013-06-07 13:03:07 +05302963 ret = clk_prepare_enable(msm_host->clk);
2964 if (ret)
2965 goto pclk_disable;
2966
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302967 msm_host->clk_rate = sdhci_msm_get_min_clock(host);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302968 atomic_set(&msm_host->clks_on, 1);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302969
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002970 /* Setup CDC calibration fixed feedback clock */
2971 msm_host->ff_clk = devm_clk_get(&pdev->dev, "cal_clk");
2972 if (!IS_ERR(msm_host->ff_clk)) {
2973 ret = clk_prepare_enable(msm_host->ff_clk);
2974 if (ret)
2975 goto clk_disable;
2976 }
2977
2978 /* Setup CDC calibration sleep clock */
2979 msm_host->sleep_clk = devm_clk_get(&pdev->dev, "sleep_clk");
2980 if (!IS_ERR(msm_host->sleep_clk)) {
2981 ret = clk_prepare_enable(msm_host->sleep_clk);
2982 if (ret)
2983 goto ff_clk_disable;
2984 }
2985
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07002986 msm_host->saved_tuning_phase = INVALID_TUNING_PHASE;
2987
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302988 ret = sdhci_msm_bus_register(msm_host, pdev);
2989 if (ret)
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002990 goto sleep_clk_disable;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302991
2992 if (msm_host->msm_bus_vote.client_handle)
2993 INIT_DELAYED_WORK(&msm_host->msm_bus_vote.vote_work,
2994 sdhci_msm_bus_work);
2995 sdhci_msm_bus_voting(host, 1);
2996
Asutosh Das0ef24812012-12-18 16:14:02 +05302997 /* Setup regulators */
2998 ret = sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, true);
2999 if (ret) {
3000 dev_err(&pdev->dev, "Regulator setup failed (%d)\n", ret);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05303001 goto bus_unregister;
Asutosh Das0ef24812012-12-18 16:14:02 +05303002 }
3003
3004 /* Reset the core and Enable SDHC mode */
3005 core_memres = platform_get_resource_byname(pdev,
3006 IORESOURCE_MEM, "core_mem");
Asutosh Das890bdee2014-08-08 23:01:42 +05303007 if (!core_memres) {
3008 dev_err(&pdev->dev, "Failed to get iomem resource\n");
3009 goto vreg_deinit;
3010 }
Asutosh Das0ef24812012-12-18 16:14:02 +05303011 msm_host->core_mem = devm_ioremap(&pdev->dev, core_memres->start,
3012 resource_size(core_memres));
3013
3014 if (!msm_host->core_mem) {
3015 dev_err(&pdev->dev, "Failed to remap registers\n");
3016 ret = -ENOMEM;
3017 goto vreg_deinit;
3018 }
3019
Stepan Moskovchenkoa4d0fa72013-09-13 22:19:33 -07003020 /* Unset HC_MODE_EN bit in HC_MODE register */
3021 writel_relaxed(0, (msm_host->core_mem + CORE_HC_MODE));
3022
Asutosh Das0ef24812012-12-18 16:14:02 +05303023 /* Set SW_RST bit in POWER register (Offset 0x0) */
Sahitya Tummala66b0fe32013-04-25 11:50:56 +05303024 writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_POWER) |
3025 CORE_SW_RST, msm_host->core_mem + CORE_POWER);
3026 /*
3027 * SW reset can take upto 10HCLK + 15MCLK cycles.
3028 * Calculating based on min clk rates (hclk = 27MHz,
3029 * mclk = 400KHz) it comes to ~40us. Let's poll for
3030 * max. 1ms for reset completion.
3031 */
3032 ret = readl_poll_timeout(msm_host->core_mem + CORE_POWER,
Matt Wagantallc7097a22014-04-29 15:48:15 -07003033 pwr, !(pwr & CORE_SW_RST), 10, 1000);
Sahitya Tummala66b0fe32013-04-25 11:50:56 +05303034
3035 if (ret) {
3036 dev_err(&pdev->dev, "reset failed (%d)\n", ret);
3037 goto vreg_deinit;
3038 }
Asutosh Das0ef24812012-12-18 16:14:02 +05303039 /* Set HC_MODE_EN bit in HC_MODE register */
3040 writel_relaxed(HC_MODE_EN, (msm_host->core_mem + CORE_HC_MODE));
3041
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07003042 /* Set FF_CLK_SW_RST_DIS bit in HC_MODE register */
3043 writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_HC_MODE) |
3044 FF_CLK_SW_RST_DIS, msm_host->core_mem + CORE_HC_MODE);
3045
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05303046 sdhci_set_default_hw_caps(msm_host, host);
Asutosh Das0ef24812012-12-18 16:14:02 +05303047 /*
Subhash Jadavani28137342013-05-14 17:46:43 +05303048 * CORE_SW_RST above may trigger power irq if previous status of PWRCTL
3049 * was either BUS_ON or IO_HIGH_V. So before we enable the power irq
3050 * interrupt in GIC (by registering the interrupt handler), we need to
3051 * ensure that any pending power irq interrupt status is acknowledged
3052 * otherwise power irq interrupt handler would be fired prematurely.
3053 */
3054 irq_status = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
3055 writel_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
3056 irq_ctl = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_CTL);
3057 if (irq_status & (CORE_PWRCTL_BUS_ON | CORE_PWRCTL_BUS_OFF))
3058 irq_ctl |= CORE_PWRCTL_BUS_SUCCESS;
3059 if (irq_status & (CORE_PWRCTL_IO_HIGH | CORE_PWRCTL_IO_LOW))
3060 irq_ctl |= CORE_PWRCTL_IO_SUCCESS;
3061 writel_relaxed(irq_ctl, (msm_host->core_mem + CORE_PWRCTL_CTL));
3062 /*
3063 * Ensure that above writes are propogated before interrupt enablement
3064 * in GIC.
3065 */
3066 mb();
3067
3068 /*
Asutosh Das0ef24812012-12-18 16:14:02 +05303069 * Following are the deviations from SDHC spec v3.0 -
3070 * 1. Card detection is handled using separate GPIO.
3071 * 2. Bus power control is handled by interacting with PMIC.
3072 */
3073 host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
3074 host->quirks |= SDHCI_QUIRK_SINGLE_POWER_WRITE;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05303075 host->quirks |= SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN;
3076 host->quirks2 |= SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK;
Sahitya Tummala87d439442013-04-12 11:49:11 +05303077 host->quirks2 |= SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD;
Sahitya Tummala314162c2013-04-12 12:11:20 +05303078 host->quirks2 |= SDHCI_QUIRK2_BROKEN_PRESET_VALUE;
Sahitya Tummala7c9780d2013-04-12 11:59:25 +05303079 host->quirks2 |= SDHCI_QUIRK2_USE_RESERVED_MAX_TIMEOUT;
Asutosh Das0ef24812012-12-18 16:14:02 +05303080
Sahitya Tummalaa5733ab52013-06-10 16:32:51 +05303081 if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK)
3082 host->quirks2 |= SDHCI_QUIRK2_DIVIDE_TOUT_BY_4;
3083
Stephen Boyd8dce5c62013-04-24 14:19:46 -07003084 host_version = readw_relaxed((host->ioaddr + SDHCI_HOST_VERSION));
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07003085 dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n",
3086 host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >>
3087 SDHCI_VENDOR_VER_SHIFT));
3088 if (((host_version & SDHCI_VENDOR_VER_MASK) >>
3089 SDHCI_VENDOR_VER_SHIFT) == SDHCI_VER_100) {
3090 /*
3091 * Add 40us delay in interrupt handler when
3092 * operating at initialization frequency(400KHz).
3093 */
3094 host->quirks2 |= SDHCI_QUIRK2_SLOW_INT_CLR;
3095 /*
3096 * Set Software Reset for DAT line in Software
3097 * Reset Register (Bit 2).
3098 */
3099 host->quirks2 |= SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT;
3100 }
3101
Asutosh Das214b9662013-06-13 14:27:42 +05303102 host->quirks2 |= SDHCI_QUIRK2_IGN_DATA_END_BIT_ERROR;
3103
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07003104 /* Setup PWRCTL irq */
Asutosh Das0ef24812012-12-18 16:14:02 +05303105 pwr_irq = platform_get_irq_byname(pdev, "pwr_irq");
3106 if (pwr_irq < 0) {
3107 dev_err(&pdev->dev, "Failed to get pwr_irq by name (%d)\n",
3108 pwr_irq);
3109 goto vreg_deinit;
3110 }
3111 ret = devm_request_threaded_irq(&pdev->dev, pwr_irq, NULL,
3112 sdhci_msm_pwr_irq, IRQF_ONESHOT,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07003113 dev_name(&pdev->dev), host);
Asutosh Das0ef24812012-12-18 16:14:02 +05303114 if (ret) {
3115 dev_err(&pdev->dev, "Request threaded irq(%d) failed (%d)\n",
3116 pwr_irq, ret);
3117 goto vreg_deinit;
3118 }
3119
3120 /* Enable pwr irq interrupts */
3121 writel_relaxed(INT_MASK, (msm_host->core_mem + CORE_PWRCTL_MASK));
3122
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05303123#ifdef CONFIG_MMC_CLKGATE
3124 /* Set clock gating delay to be used when CONFIG_MMC_CLKGATE is set */
3125 msm_host->mmc->clkgate_delay = SDHCI_MSM_MMC_CLK_GATE_DELAY;
3126#endif
3127
Asutosh Das0ef24812012-12-18 16:14:02 +05303128 /* Set host capabilities */
3129 msm_host->mmc->caps |= msm_host->pdata->mmc_bus_width;
3130 msm_host->mmc->caps |= msm_host->pdata->caps;
Asutosh Das0ef24812012-12-18 16:14:02 +05303131 msm_host->mmc->caps2 |= msm_host->pdata->caps2;
Venkat Gopalakrishnan97c16112014-12-16 18:26:25 -08003132 msm_host->mmc->caps2 |= MMC_CAP2_BOOTPART_NOACC;
3133 msm_host->mmc->caps2 |= MMC_CAP2_FULL_PWR_CYCLE;
Subhash Jadavani6d472b22013-05-29 15:52:10 +05303134 msm_host->mmc->caps2 |= MMC_CAP2_ASYNC_SDIO_IRQ_4BIT_MODE;
Venkat Gopalakrishnan97c16112014-12-16 18:26:25 -08003135 msm_host->mmc->caps2 |= MMC_CAP2_HS400_POST_TUNING;
Asutosh Dasbea115d2013-06-24 18:20:45 +05303136 msm_host->mmc->pm_caps |= MMC_PM_KEEP_POWER;
Asutosh Das0ef24812012-12-18 16:14:02 +05303137
3138 if (msm_host->pdata->nonremovable)
3139 msm_host->mmc->caps |= MMC_CAP_NONREMOVABLE;
3140
Guoping Yuf7c91332014-08-20 16:56:18 +08003141 if (msm_host->pdata->nonhotplug)
3142 msm_host->mmc->caps2 |= MMC_CAP2_NONHOTPLUG;
3143
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05303144 host->cpu_dma_latency_us = msm_host->pdata->cpu_dma_latency_us;
Maya Erez994cf2f2014-10-21 20:22:04 +03003145 host->pm_qos_req_dma.type = msm_host->pdata->cpu_affinity_type;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05303146
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05303147 init_completion(&msm_host->pwr_irq_completion);
3148
Sahitya Tummala581df132013-03-12 14:57:46 +05303149 if (gpio_is_valid(msm_host->pdata->status_gpio)) {
Sahitya Tummala6ddabb42014-06-05 13:26:55 +05303150 /*
3151 * Set up the card detect GPIO in active configuration before
3152 * configuring it as an IRQ. Otherwise, it can be in some
3153 * weird/inconsistent state resulting in flood of interrupts.
3154 */
3155 sdhci_msm_setup_pins(msm_host->pdata, true);
3156
Sahitya Tummala581df132013-03-12 14:57:46 +05303157 ret = mmc_gpio_request_cd(msm_host->mmc,
3158 msm_host->pdata->status_gpio, 0);
3159 if (ret) {
3160 dev_err(&pdev->dev, "%s: Failed to request card detection IRQ %d\n",
3161 __func__, ret);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05303162 goto vreg_deinit;
Sahitya Tummala581df132013-03-12 14:57:46 +05303163 }
3164 }
3165
Krishna Konda7feab352013-09-17 23:55:40 -07003166 if ((sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT) &&
3167 (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(64)))) {
3168 host->dma_mask = DMA_BIT_MASK(64);
3169 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
3170 } else if (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(32))) {
Sahitya Tummalaeaa21862013-03-20 19:34:59 +05303171 host->dma_mask = DMA_BIT_MASK(32);
3172 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
3173 } else {
3174 dev_err(&pdev->dev, "%s: Failed to set dma mask\n", __func__);
3175 }
3176
Asutosh Das0ef24812012-12-18 16:14:02 +05303177 ret = sdhci_add_host(host);
3178 if (ret) {
3179 dev_err(&pdev->dev, "Add host failed (%d)\n", ret);
Sahitya Tummala581df132013-03-12 14:57:46 +05303180 goto vreg_deinit;
Asutosh Das0ef24812012-12-18 16:14:02 +05303181 }
3182
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05303183 msm_host->msm_bus_vote.max_bus_bw.show = show_sdhci_max_bus_bw;
3184 msm_host->msm_bus_vote.max_bus_bw.store = store_sdhci_max_bus_bw;
3185 sysfs_attr_init(&msm_host->msm_bus_vote.max_bus_bw.attr);
3186 msm_host->msm_bus_vote.max_bus_bw.attr.name = "max_bus_bw";
3187 msm_host->msm_bus_vote.max_bus_bw.attr.mode = S_IRUGO | S_IWUSR;
3188 ret = device_create_file(&pdev->dev,
3189 &msm_host->msm_bus_vote.max_bus_bw);
3190 if (ret)
3191 goto remove_host;
3192
Sahitya Tummala5c55b932013-06-20 14:00:18 +05303193 if (!gpio_is_valid(msm_host->pdata->status_gpio)) {
3194 msm_host->polling.show = show_polling;
3195 msm_host->polling.store = store_polling;
3196 sysfs_attr_init(&msm_host->polling.attr);
3197 msm_host->polling.attr.name = "polling";
3198 msm_host->polling.attr.mode = S_IRUGO | S_IWUSR;
3199 ret = device_create_file(&pdev->dev, &msm_host->polling);
3200 if (ret)
3201 goto remove_max_bus_bw_file;
3202 }
Asutosh Dase5e9ca62013-07-30 19:08:36 +05303203
3204 msm_host->auto_cmd21_attr.show = show_auto_cmd21;
3205 msm_host->auto_cmd21_attr.store = store_auto_cmd21;
3206 sysfs_attr_init(&msm_host->auto_cmd21_attr.attr);
3207 msm_host->auto_cmd21_attr.attr.name = "enable_auto_cmd21";
3208 msm_host->auto_cmd21_attr.attr.mode = S_IRUGO | S_IWUSR;
3209 ret = device_create_file(&pdev->dev, &msm_host->auto_cmd21_attr);
3210 if (ret) {
3211 pr_err("%s: %s: failed creating auto-cmd21 attr: %d\n",
3212 mmc_hostname(host->mmc), __func__, ret);
3213 device_remove_file(&pdev->dev, &msm_host->auto_cmd21_attr);
3214 }
3215
Asutosh Das0ef24812012-12-18 16:14:02 +05303216 /* Successful initialization */
3217 goto out;
3218
Sahitya Tummala5c55b932013-06-20 14:00:18 +05303219remove_max_bus_bw_file:
3220 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Asutosh Das0ef24812012-12-18 16:14:02 +05303221remove_host:
3222 dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
3223 sdhci_remove_host(host, dead);
3224vreg_deinit:
3225 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05303226bus_unregister:
3227 if (msm_host->msm_bus_vote.client_handle)
3228 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
3229 sdhci_msm_bus_unregister(msm_host);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07003230sleep_clk_disable:
3231 if (!IS_ERR(msm_host->sleep_clk))
3232 clk_disable_unprepare(msm_host->sleep_clk);
3233ff_clk_disable:
3234 if (!IS_ERR(msm_host->ff_clk))
3235 clk_disable_unprepare(msm_host->ff_clk);
Asutosh Das0ef24812012-12-18 16:14:02 +05303236clk_disable:
3237 if (!IS_ERR(msm_host->clk))
3238 clk_disable_unprepare(msm_host->clk);
3239pclk_disable:
3240 if (!IS_ERR(msm_host->pclk))
3241 clk_disable_unprepare(msm_host->pclk);
3242bus_clk_disable:
3243 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
3244 clk_disable_unprepare(msm_host->bus_clk);
3245pltfm_free:
3246 sdhci_pltfm_free(pdev);
3247out:
3248 pr_debug("%s: Exit %s\n", dev_name(&pdev->dev), __func__);
3249 return ret;
3250}
3251
3252static int sdhci_msm_remove(struct platform_device *pdev)
3253{
3254 struct sdhci_host *host = platform_get_drvdata(pdev);
3255 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3256 struct sdhci_msm_host *msm_host = pltfm_host->priv;
3257 struct sdhci_msm_pltfm_data *pdata = msm_host->pdata;
3258 int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) ==
3259 0xffffffff);
3260
3261 pr_debug("%s: %s\n", dev_name(&pdev->dev), __func__);
Sahitya Tummala5c55b932013-06-20 14:00:18 +05303262 if (!gpio_is_valid(msm_host->pdata->status_gpio))
3263 device_remove_file(&pdev->dev, &msm_host->polling);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05303264 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Asutosh Das0ef24812012-12-18 16:14:02 +05303265 sdhci_remove_host(host, dead);
3266 sdhci_pltfm_free(pdev);
Sahitya Tummala581df132013-03-12 14:57:46 +05303267
Asutosh Das0ef24812012-12-18 16:14:02 +05303268 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05303269
Pratibhasagar V9acf2642013-11-21 21:07:21 +05303270 sdhci_msm_setup_pins(pdata, true);
3271 sdhci_msm_setup_pins(pdata, false);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05303272
3273 if (msm_host->msm_bus_vote.client_handle) {
3274 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
3275 sdhci_msm_bus_unregister(msm_host);
3276 }
Asutosh Das0ef24812012-12-18 16:14:02 +05303277 return 0;
3278}
3279
3280static const struct of_device_id sdhci_msm_dt_match[] = {
3281 {.compatible = "qcom,sdhci-msm"},
3282};
3283MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match);
3284
3285static struct platform_driver sdhci_msm_driver = {
3286 .probe = sdhci_msm_probe,
3287 .remove = sdhci_msm_remove,
3288 .driver = {
3289 .name = "sdhci_msm",
3290 .owner = THIS_MODULE,
3291 .of_match_table = sdhci_msm_dt_match,
3292 },
3293};
3294
3295module_platform_driver(sdhci_msm_driver);
3296
3297MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Secure Digital Host Controller Interface driver");
3298MODULE_LICENSE("GPL v2");