blob: f7feaeb751ab8df9d90d6525598d88a2025a12b5 [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 */
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +0200301 int pwr_irq; /* power irq */
Asutosh Das0ef24812012-12-18 16:14:02 +0530302 struct clk *clk; /* main SD/MMC bus clock */
303 struct clk *pclk; /* SDHC peripheral bus clock */
304 struct clk *bus_clk; /* SDHC bus voter clock */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700305 struct clk *ff_clk; /* CDC calibration fixed feedback clock */
306 struct clk *sleep_clk; /* CDC calibration sleep clock */
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +0530307 atomic_t clks_on; /* Set if clocks are enabled */
Asutosh Das0ef24812012-12-18 16:14:02 +0530308 struct sdhci_msm_pltfm_data *pdata;
309 struct mmc_host *mmc;
310 struct sdhci_pltfm_data sdhci_msm_pdata;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +0530311 u32 curr_pwr_state;
312 u32 curr_io_level;
313 struct completion pwr_irq_completion;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530314 struct sdhci_msm_bus_vote msm_bus_vote;
Sahitya Tummala5c55b932013-06-20 14:00:18 +0530315 struct device_attribute polling;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530316 u32 clk_rate; /* Keeps track of current clock rate that is set */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700317 bool tuning_done;
318 bool calibration_done;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700319 u8 saved_tuning_phase;
Asutosh Dase5e9ca62013-07-30 19:08:36 +0530320 bool en_auto_cmd21;
321 struct device_attribute auto_cmd21_attr;
Asutosh Das9d7ee2f2013-11-08 12:33:47 +0530322 atomic_t controller_clock;
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700323 bool use_cdclp533;
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800324 bool use_updated_dll_reset;
Asutosh Das0ef24812012-12-18 16:14:02 +0530325};
326
327enum vdd_io_level {
328 /* set vdd_io_data->low_vol_level */
329 VDD_IO_LOW,
330 /* set vdd_io_data->high_vol_level */
331 VDD_IO_HIGH,
332 /*
333 * set whatever there in voltage_level (third argument) of
334 * sdhci_msm_set_vdd_io_vol() function.
335 */
336 VDD_IO_SET_LEVEL,
337};
338
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700339/* MSM platform specific tuning */
340static inline int msm_dll_poll_ck_out_en(struct sdhci_host *host,
341 u8 poll)
342{
343 int rc = 0;
344 u32 wait_cnt = 50;
345 u8 ck_out_en = 0;
346 struct mmc_host *mmc = host->mmc;
347
348 /* poll for CK_OUT_EN bit. max. poll time = 50us */
349 ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) &
350 CORE_CK_OUT_EN);
351
352 while (ck_out_en != poll) {
353 if (--wait_cnt == 0) {
354 pr_err("%s: %s: CK_OUT_EN bit is not %d\n",
355 mmc_hostname(mmc), __func__, poll);
356 rc = -ETIMEDOUT;
357 goto out;
358 }
359 udelay(1);
360
361 ck_out_en = !!(readl_relaxed(host->ioaddr +
362 CORE_DLL_CONFIG) & CORE_CK_OUT_EN);
363 }
364out:
365 return rc;
366}
367
Asutosh Dase5e9ca62013-07-30 19:08:36 +0530368/*
369 * Enable CDR to track changes of DAT lines and adjust sampling
370 * point according to voltage/temperature variations
371 */
372static int msm_enable_cdr_cm_sdc4_dll(struct sdhci_host *host)
373{
374 int rc = 0;
375 u32 config;
376
377 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
378 config |= CORE_CDR_EN;
379 config &= ~(CORE_CDR_EXT_EN | CORE_CK_OUT_EN);
380 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
381
382 rc = msm_dll_poll_ck_out_en(host, 0);
383 if (rc)
384 goto err;
385
386 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) |
387 CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
388
389 rc = msm_dll_poll_ck_out_en(host, 1);
390 if (rc)
391 goto err;
392 goto out;
393err:
394 pr_err("%s: %s: failed\n", mmc_hostname(host->mmc), __func__);
395out:
396 return rc;
397}
398
399static ssize_t store_auto_cmd21(struct device *dev, struct device_attribute
400 *attr, const char *buf, size_t count)
401{
402 struct sdhci_host *host = dev_get_drvdata(dev);
403 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
404 struct sdhci_msm_host *msm_host = pltfm_host->priv;
405 u32 tmp;
406 unsigned long flags;
407
408 if (!kstrtou32(buf, 0, &tmp)) {
409 spin_lock_irqsave(&host->lock, flags);
410 msm_host->en_auto_cmd21 = !!tmp;
411 spin_unlock_irqrestore(&host->lock, flags);
412 }
413 return count;
414}
415
416static ssize_t show_auto_cmd21(struct device *dev,
417 struct device_attribute *attr, char *buf)
418{
419 struct sdhci_host *host = dev_get_drvdata(dev);
420 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
421 struct sdhci_msm_host *msm_host = pltfm_host->priv;
422
423 return snprintf(buf, PAGE_SIZE, "%d\n", msm_host->en_auto_cmd21);
424}
425
426/* MSM auto-tuning handler */
427static int sdhci_msm_config_auto_tuning_cmd(struct sdhci_host *host,
428 bool enable,
429 u32 type)
430{
431 int rc = 0;
432 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
433 struct sdhci_msm_host *msm_host = pltfm_host->priv;
434 u32 val = 0;
435
436 if (!msm_host->en_auto_cmd21)
437 return 0;
438
439 if (type == MMC_SEND_TUNING_BLOCK_HS200)
440 val = CORE_HC_AUTO_CMD21_EN;
441 else
442 return 0;
443
444 if (enable) {
445 rc = msm_enable_cdr_cm_sdc4_dll(host);
446 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
447 val, host->ioaddr + CORE_VENDOR_SPEC);
448 } else {
449 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
450 ~val, host->ioaddr + CORE_VENDOR_SPEC);
451 }
452 return rc;
453}
454
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700455static int msm_config_cm_dll_phase(struct sdhci_host *host, u8 phase)
456{
457 int rc = 0;
458 u8 grey_coded_phase_table[] = {0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4,
459 0xC, 0xD, 0xF, 0xE, 0xA, 0xB, 0x9,
460 0x8};
461 unsigned long flags;
462 u32 config;
463 struct mmc_host *mmc = host->mmc;
464
465 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
466 spin_lock_irqsave(&host->lock, flags);
467
468 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
469 config &= ~(CORE_CDR_EN | CORE_CK_OUT_EN);
470 config |= (CORE_CDR_EXT_EN | CORE_DLL_EN);
471 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
472
473 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '0' */
474 rc = msm_dll_poll_ck_out_en(host, 0);
475 if (rc)
476 goto err_out;
477
478 /*
479 * Write the selected DLL clock output phase (0 ... 15)
480 * to CDR_SELEXT bit field of DLL_CONFIG register.
481 */
482 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
483 & ~(0xF << 20))
484 | (grey_coded_phase_table[phase] << 20)),
485 host->ioaddr + CORE_DLL_CONFIG);
486
487 /* Set CK_OUT_EN bit of DLL_CONFIG register to 1. */
488 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
489 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
490
491 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '1' */
492 rc = msm_dll_poll_ck_out_en(host, 1);
493 if (rc)
494 goto err_out;
495
496 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
497 config |= CORE_CDR_EN;
498 config &= ~CORE_CDR_EXT_EN;
499 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
500 goto out;
501
502err_out:
503 pr_err("%s: %s: Failed to set DLL phase: %d\n",
504 mmc_hostname(mmc), __func__, phase);
505out:
506 spin_unlock_irqrestore(&host->lock, flags);
507 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
508 return rc;
509}
510
511/*
512 * Find out the greatest range of consecuitive selected
513 * DLL clock output phases that can be used as sampling
514 * setting for SD3.0 UHS-I card read operation (in SDR104
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700515 * timing mode) or for eMMC4.5 card read operation (in
516 * HS400/HS200 timing mode).
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700517 * Select the 3/4 of the range and configure the DLL with the
518 * selected DLL clock output phase.
519 */
520
521static int msm_find_most_appropriate_phase(struct sdhci_host *host,
522 u8 *phase_table, u8 total_phases)
523{
524 int ret;
525 u8 ranges[MAX_PHASES][MAX_PHASES] = { {0}, {0} };
526 u8 phases_per_row[MAX_PHASES] = {0};
527 int row_index = 0, col_index = 0, selected_row_index = 0, curr_max = 0;
528 int i, cnt, phase_0_raw_index = 0, phase_15_raw_index = 0;
529 bool phase_0_found = false, phase_15_found = false;
530 struct mmc_host *mmc = host->mmc;
531
532 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
533 if (!total_phases || (total_phases > MAX_PHASES)) {
534 pr_err("%s: %s: invalid argument: total_phases=%d\n",
535 mmc_hostname(mmc), __func__, total_phases);
536 return -EINVAL;
537 }
538
539 for (cnt = 0; cnt < total_phases; cnt++) {
540 ranges[row_index][col_index] = phase_table[cnt];
541 phases_per_row[row_index] += 1;
542 col_index++;
543
544 if ((cnt + 1) == total_phases) {
545 continue;
546 /* check if next phase in phase_table is consecutive or not */
547 } else if ((phase_table[cnt] + 1) != phase_table[cnt + 1]) {
548 row_index++;
549 col_index = 0;
550 }
551 }
552
553 if (row_index >= MAX_PHASES)
554 return -EINVAL;
555
556 /* Check if phase-0 is present in first valid window? */
557 if (!ranges[0][0]) {
558 phase_0_found = true;
559 phase_0_raw_index = 0;
560 /* Check if cycle exist between 2 valid windows */
561 for (cnt = 1; cnt <= row_index; cnt++) {
562 if (phases_per_row[cnt]) {
563 for (i = 0; i < phases_per_row[cnt]; i++) {
564 if (ranges[cnt][i] == 15) {
565 phase_15_found = true;
566 phase_15_raw_index = cnt;
567 break;
568 }
569 }
570 }
571 }
572 }
573
574 /* If 2 valid windows form cycle then merge them as single window */
575 if (phase_0_found && phase_15_found) {
576 /* number of phases in raw where phase 0 is present */
577 u8 phases_0 = phases_per_row[phase_0_raw_index];
578 /* number of phases in raw where phase 15 is present */
579 u8 phases_15 = phases_per_row[phase_15_raw_index];
580
581 if (phases_0 + phases_15 >= MAX_PHASES)
582 /*
583 * If there are more than 1 phase windows then total
584 * number of phases in both the windows should not be
585 * more than or equal to MAX_PHASES.
586 */
587 return -EINVAL;
588
589 /* Merge 2 cyclic windows */
590 i = phases_15;
591 for (cnt = 0; cnt < phases_0; cnt++) {
592 ranges[phase_15_raw_index][i] =
593 ranges[phase_0_raw_index][cnt];
594 if (++i >= MAX_PHASES)
595 break;
596 }
597
598 phases_per_row[phase_0_raw_index] = 0;
599 phases_per_row[phase_15_raw_index] = phases_15 + phases_0;
600 }
601
602 for (cnt = 0; cnt <= row_index; cnt++) {
603 if (phases_per_row[cnt] > curr_max) {
604 curr_max = phases_per_row[cnt];
605 selected_row_index = cnt;
606 }
607 }
608
609 i = ((curr_max * 3) / 4);
610 if (i)
611 i--;
612
613 ret = (int)ranges[selected_row_index][i];
614
615 if (ret >= MAX_PHASES) {
616 ret = -EINVAL;
617 pr_err("%s: %s: invalid phase selected=%d\n",
618 mmc_hostname(mmc), __func__, ret);
619 }
620
621 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
622 return ret;
623}
624
625static inline void msm_cm_dll_set_freq(struct sdhci_host *host)
626{
627 u32 mclk_freq = 0;
628
629 /* Program the MCLK value to MCLK_FREQ bit field */
630 if (host->clock <= 112000000)
631 mclk_freq = 0;
632 else if (host->clock <= 125000000)
633 mclk_freq = 1;
634 else if (host->clock <= 137000000)
635 mclk_freq = 2;
636 else if (host->clock <= 150000000)
637 mclk_freq = 3;
638 else if (host->clock <= 162000000)
639 mclk_freq = 4;
640 else if (host->clock <= 175000000)
641 mclk_freq = 5;
642 else if (host->clock <= 187000000)
643 mclk_freq = 6;
644 else if (host->clock <= 200000000)
645 mclk_freq = 7;
646
647 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
648 & ~(7 << 24)) | (mclk_freq << 24)),
649 host->ioaddr + CORE_DLL_CONFIG);
650}
651
652/* Initialize the DLL (Programmable Delay Line ) */
653static int msm_init_cm_dll(struct sdhci_host *host)
654{
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800655 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
656 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700657 struct mmc_host *mmc = host->mmc;
658 int rc = 0;
659 unsigned long flags;
660 u32 wait_cnt;
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530661 bool prev_pwrsave, curr_pwrsave;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700662
663 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
664 spin_lock_irqsave(&host->lock, flags);
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530665 prev_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
666 CORE_CLK_PWRSAVE);
667 curr_pwrsave = prev_pwrsave;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700668 /*
669 * Make sure that clock is always enabled when DLL
670 * tuning is in progress. Keeping PWRSAVE ON may
671 * turn off the clock. So let's disable the PWRSAVE
672 * here and re-enable it once tuning is completed.
673 */
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530674 if (prev_pwrsave) {
675 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
676 & ~CORE_CLK_PWRSAVE),
677 host->ioaddr + CORE_VENDOR_SPEC);
678 curr_pwrsave = false;
679 }
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700680
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800681 if (msm_host->use_updated_dll_reset) {
682 /* Disable the DLL clock */
683 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
684 & ~CORE_CK_OUT_EN),
685 host->ioaddr + CORE_DLL_CONFIG);
686
687 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
688 | CORE_DLL_CLOCK_DISABLE),
689 host->ioaddr + CORE_DLL_CONFIG_2);
690 }
691
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700692 /* Write 1 to DLL_RST bit of DLL_CONFIG register */
693 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
694 | CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
695
696 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
697 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
698 | CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
699 msm_cm_dll_set_freq(host);
700
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800701 if (msm_host->use_updated_dll_reset) {
702 u32 mclk_freq = 0;
703
704 if ((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
705 & CORE_FLL_CYCLE_CNT))
706 mclk_freq = (u32) ((host->clock / TCXO_FREQ) * 8);
707 else
708 mclk_freq = (u32) ((host->clock / TCXO_FREQ) * 4);
709
710 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
711 & ~(0xFF << 10)) | (mclk_freq << 10)),
712 host->ioaddr + CORE_DLL_CONFIG_2);
713 /* wait for 5us before enabling DLL clock */
714 udelay(5);
715 }
716
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700717 /* Write 0 to DLL_RST bit of DLL_CONFIG register */
718 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
719 & ~CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
720
721 /* Write 0 to DLL_PDN bit of DLL_CONFIG register */
722 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
723 & ~CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
724
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -0800725 if (msm_host->use_updated_dll_reset) {
726 msm_cm_dll_set_freq(host);
727 /* Enable the DLL clock */
728 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
729 & ~CORE_DLL_CLOCK_DISABLE),
730 host->ioaddr + CORE_DLL_CONFIG_2);
731 }
732
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700733 /* Set DLL_EN bit to 1. */
734 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
735 | CORE_DLL_EN), host->ioaddr + CORE_DLL_CONFIG);
736
737 /* Set CK_OUT_EN bit to 1. */
738 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
739 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
740
741 wait_cnt = 50;
742 /* Wait until DLL_LOCK bit of DLL_STATUS register becomes '1' */
743 while (!(readl_relaxed(host->ioaddr + CORE_DLL_STATUS) &
744 CORE_DLL_LOCK)) {
745 /* max. wait for 50us sec for LOCK bit to be set */
746 if (--wait_cnt == 0) {
747 pr_err("%s: %s: DLL failed to LOCK\n",
748 mmc_hostname(mmc), __func__);
749 rc = -ETIMEDOUT;
750 goto out;
751 }
752 /* wait for 1us before polling again */
753 udelay(1);
754 }
755
756out:
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530757 /* Restore the correct PWRSAVE state */
758 if (prev_pwrsave ^ curr_pwrsave) {
759 u32 reg = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC);
760
761 if (prev_pwrsave)
762 reg |= CORE_CLK_PWRSAVE;
763 else
764 reg &= ~CORE_CLK_PWRSAVE;
765
766 writel_relaxed(reg, host->ioaddr + CORE_VENDOR_SPEC);
767 }
768
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700769 spin_unlock_irqrestore(&host->lock, flags);
770 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
771 return rc;
772}
773
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700774static int sdhci_msm_cdclp533_calibration(struct sdhci_host *host)
775{
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700776 u32 calib_done;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700777 int ret = 0;
778 int cdc_err = 0;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700779
780 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
781
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700782 /* Write 0 to CDC_T4_DLY_SEL field in VENDOR_SPEC_DDR200_CFG */
783 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
784 & ~CORE_CDC_T4_DLY_SEL),
785 host->ioaddr + CORE_DDR_200_CFG);
786
787 /* Write 0 to CDC_SWITCH_BYPASS_OFF field in CORE_CSR_CDC_GEN_CFG */
788 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG)
789 & ~CORE_CDC_SWITCH_BYPASS_OFF),
790 host->ioaddr + CORE_CSR_CDC_GEN_CFG);
791
792 /* Write 1 to CDC_SWITCH_RC_EN field in CORE_CSR_CDC_GEN_CFG */
793 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG)
794 | CORE_CDC_SWITCH_RC_EN),
795 host->ioaddr + CORE_CSR_CDC_GEN_CFG);
796
797 /* Write 0 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
798 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
799 & ~CORE_START_CDC_TRAFFIC),
800 host->ioaddr + CORE_DDR_200_CFG);
801
802 /*
803 * Perform CDC Register Initialization Sequence
804 *
805 * CORE_CSR_CDC_CTLR_CFG0 0x11800EC
806 * CORE_CSR_CDC_CTLR_CFG1 0x3011111
807 * CORE_CSR_CDC_CAL_TIMER_CFG0 0x1201000
808 * CORE_CSR_CDC_CAL_TIMER_CFG1 0x4
809 * CORE_CSR_CDC_REFCOUNT_CFG 0xCB732020
810 * CORE_CSR_CDC_COARSE_CAL_CFG 0xB19
811 * CORE_CSR_CDC_DELAY_CFG 0x3AC
812 * CORE_CDC_OFFSET_CFG 0x0
813 * CORE_CDC_SLAVE_DDA_CFG 0x16334
814 */
815
816 writel_relaxed(0x11800EC, host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
817 writel_relaxed(0x3011111, host->ioaddr + CORE_CSR_CDC_CTLR_CFG1);
818 writel_relaxed(0x1201000, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
819 writel_relaxed(0x4, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG1);
820 writel_relaxed(0xCB732020, host->ioaddr + CORE_CSR_CDC_REFCOUNT_CFG);
821 writel_relaxed(0xB19, host->ioaddr + CORE_CSR_CDC_COARSE_CAL_CFG);
Subhash Jadavanibe406d92014-06-17 16:47:48 -0700822 writel_relaxed(0x4E2, host->ioaddr + CORE_CSR_CDC_DELAY_CFG);
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700823 writel_relaxed(0x0, host->ioaddr + CORE_CDC_OFFSET_CFG);
824 writel_relaxed(0x16334, host->ioaddr + CORE_CDC_SLAVE_DDA_CFG);
825
826 /* CDC HW Calibration */
827
828 /* Write 1 to SW_TRIG_FULL_CALIB field in CORE_CSR_CDC_CTLR_CFG0 */
829 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
830 | CORE_SW_TRIG_FULL_CALIB),
831 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
832
833 /* Write 0 to SW_TRIG_FULL_CALIB field in CORE_CSR_CDC_CTLR_CFG0 */
834 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
835 & ~CORE_SW_TRIG_FULL_CALIB),
836 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
837
838 /* Write 1 to HW_AUTOCAL_ENA field in CORE_CSR_CDC_CTLR_CFG0 */
839 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
840 | CORE_HW_AUTOCAL_ENA),
841 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
842
843 /* Write 1 to TIMER_ENA field in CORE_CSR_CDC_CAL_TIMER_CFG0 */
844 writel_relaxed((readl_relaxed(host->ioaddr +
845 CORE_CSR_CDC_CAL_TIMER_CFG0) | CORE_TIMER_ENA),
846 host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
847
848 mb();
849
850 /* Poll on CALIBRATION_DONE field in CORE_CSR_CDC_STATUS0 to be 1 */
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700851 ret = readl_poll_timeout(host->ioaddr + CORE_CSR_CDC_STATUS0,
852 calib_done, (calib_done & CORE_CALIBRATION_DONE), 1, 50);
853
854 if (ret == -ETIMEDOUT) {
855 pr_err("%s: %s: CDC Calibration was not completed\n",
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700856 mmc_hostname(host->mmc), __func__);
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700857 goto out;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700858 }
859
860 /* Verify CDC_ERROR_CODE field in CORE_CSR_CDC_STATUS0 is 0 */
861 cdc_err = readl_relaxed(host->ioaddr + CORE_CSR_CDC_STATUS0)
862 & CORE_CDC_ERROR_CODE_MASK;
863 if (cdc_err) {
864 pr_err("%s: %s: CDC Error Code %d\n",
865 mmc_hostname(host->mmc), __func__, cdc_err);
866 ret = -EINVAL;
867 goto out;
868 }
869
870 /* Write 1 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
871 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
872 | CORE_START_CDC_TRAFFIC),
873 host->ioaddr + CORE_DDR_200_CFG);
874out:
875 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
876 __func__, ret);
877 return ret;
878}
879
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700880static int sdhci_msm_cm_dll_sdc4_calibration(struct sdhci_host *host)
881{
882 u32 dll_status;
883 int ret = 0;
884
885 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
886
887 /*
888 * Currently the CORE_DDR_CONFIG register defaults to desired
889 * configuration on reset. Currently reprogramming the power on
890 * reset (POR) value in case it might have been modified by
891 * bootloaders. In the future, if this changes, then the desired
892 * values will need to be programmed appropriately.
893 */
894 writel_relaxed(DDR_CONFIG_POR_VAL, host->ioaddr + CORE_DDR_CONFIG);
895
896 /* Write 1 to DDR_CAL_EN field in CORE_DLL_CONFIG_2 */
897 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2)
898 | CORE_DDR_CAL_EN),
899 host->ioaddr + CORE_DLL_CONFIG_2);
900
901 /* Poll on DDR_DLL_LOCK bit in CORE_DLL_STATUS to be set */
902 ret = readl_poll_timeout(host->ioaddr + CORE_DLL_STATUS,
903 dll_status, (dll_status & CORE_DDR_DLL_LOCK), 10, 1000);
904
905 if (ret == -ETIMEDOUT) {
906 pr_err("%s: %s: CM_DLL_SDC4 Calibration was not completed\n",
907 mmc_hostname(host->mmc), __func__);
908 goto out;
909 }
910
911 /* set CORE_PWRSAVE_DLL bit in CORE_VENDOR_SPEC3 */
912 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC3)
913 | CORE_PWRSAVE_DLL),
914 host->ioaddr + CORE_VENDOR_SPEC3);
915 mb();
916out:
917 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
918 __func__, ret);
919 return ret;
920}
921
922static int sdhci_msm_hs400_dll_calibration(struct sdhci_host *host)
923{
924 int ret = 0;
925 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
926 struct sdhci_msm_host *msm_host = pltfm_host->priv;
927
928 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
929
930 /*
931 * Retuning in HS400 (DDR mode) will fail, just reset the
932 * tuning block and restore the saved tuning phase.
933 */
934 ret = msm_init_cm_dll(host);
935 if (ret)
936 goto out;
937
938 /* Set the selected phase in delay line hw block */
939 ret = msm_config_cm_dll_phase(host, msm_host->saved_tuning_phase);
940 if (ret)
941 goto out;
942
Krishna Konda0e8efba2014-06-23 14:50:38 -0700943 /* Write 1 to CMD_DAT_TRACK_SEL field in DLL_CONFIG */
944 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
945 | CORE_CMD_DAT_TRACK_SEL),
946 host->ioaddr + CORE_DLL_CONFIG);
947
Krishna Konda2faa7bb2014-06-04 01:25:16 -0700948 if (msm_host->use_cdclp533)
949 /* Calibrate CDCLP533 DLL HW */
950 ret = sdhci_msm_cdclp533_calibration(host);
951 else
952 /* Calibrate CM_DLL_SDC4 HW */
953 ret = sdhci_msm_cm_dll_sdc4_calibration(host);
954out:
955 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
956 __func__, ret);
957 return ret;
958}
959
Krishna Konda96e6b112013-10-28 15:25:03 -0700960static void sdhci_msm_set_mmc_drv_type(struct sdhci_host *host, u32 opcode,
961 u8 drv_type)
962{
963 struct mmc_command cmd = {0};
964 struct mmc_request mrq = {NULL};
965 struct mmc_host *mmc = host->mmc;
966 u8 val = ((drv_type << 4) | 2);
967
968 cmd.opcode = MMC_SWITCH;
969 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
970 (EXT_CSD_HS_TIMING << 16) |
971 (val << 8) |
972 EXT_CSD_CMD_SET_NORMAL;
973 cmd.flags = MMC_CMD_AC | MMC_RSP_R1B;
974 /* 1 sec */
975 cmd.busy_timeout = 1000 * 1000;
976
977 memset(cmd.resp, 0, sizeof(cmd.resp));
978 cmd.retries = 3;
979
980 mrq.cmd = &cmd;
981 cmd.data = NULL;
982
983 mmc_wait_for_req(mmc, &mrq);
984 pr_debug("%s: %s: set card drive type to %d\n",
985 mmc_hostname(mmc), __func__,
986 drv_type);
987}
988
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700989int sdhci_msm_execute_tuning(struct sdhci_host *host, u32 opcode)
990{
991 unsigned long flags;
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530992 int tuning_seq_cnt = 3;
Krishna Konda96e6b112013-10-28 15:25:03 -0700993 u8 phase, *data_buf, tuned_phases[NUM_TUNING_PHASES], tuned_phase_cnt;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700994 const u32 *tuning_block_pattern = tuning_block_64;
995 int size = sizeof(tuning_block_64); /* Tuning pattern size in bytes */
996 int rc;
997 struct mmc_host *mmc = host->mmc;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530998 struct mmc_ios ios = host->mmc->ios;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700999 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1000 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Krishna Konda96e6b112013-10-28 15:25:03 -07001001 u8 drv_type = 0;
1002 bool drv_type_changed = false;
1003 struct mmc_card *card = host->mmc->card;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301004
1005 /*
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07001006 * Tuning is required for SDR104, HS200 and HS400 cards and
1007 * if clock frequency is greater than 100MHz in these modes.
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301008 */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07001009 if (host->clock <= CORE_FREQ_100MHZ ||
1010 !((ios.timing == MMC_TIMING_MMC_HS400) ||
1011 (ios.timing == MMC_TIMING_MMC_HS200) ||
1012 (ios.timing == MMC_TIMING_UHS_SDR104)))
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301013 return 0;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001014
1015 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07001016
Krishna Konda2faa7bb2014-06-04 01:25:16 -07001017 /* CDC/SDC4 DLL HW calibration is only required for HS400 mode*/
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07001018 if (msm_host->tuning_done && !msm_host->calibration_done &&
1019 (mmc->ios.timing == MMC_TIMING_MMC_HS400)) {
Krishna Konda2faa7bb2014-06-04 01:25:16 -07001020 rc = sdhci_msm_hs400_dll_calibration(host);
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07001021 spin_lock_irqsave(&host->lock, flags);
1022 if (!rc)
1023 msm_host->calibration_done = true;
1024 spin_unlock_irqrestore(&host->lock, flags);
1025 goto out;
1026 }
1027
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001028 spin_lock_irqsave(&host->lock, flags);
1029
1030 if ((opcode == MMC_SEND_TUNING_BLOCK_HS200) &&
1031 (mmc->ios.bus_width == MMC_BUS_WIDTH_8)) {
1032 tuning_block_pattern = tuning_block_128;
1033 size = sizeof(tuning_block_128);
1034 }
1035 spin_unlock_irqrestore(&host->lock, flags);
1036
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001037 data_buf = kmalloc(size, GFP_KERNEL);
1038 if (!data_buf) {
1039 rc = -ENOMEM;
1040 goto out;
1041 }
1042
Sahitya Tummala9fe16532013-06-13 10:36:57 +05301043retry:
Krishna Konda96e6b112013-10-28 15:25:03 -07001044 tuned_phase_cnt = 0;
1045
Sahitya Tummala9fe16532013-06-13 10:36:57 +05301046 /* first of all reset the tuning block */
1047 rc = msm_init_cm_dll(host);
1048 if (rc)
1049 goto kfree;
1050
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001051 phase = 0;
1052 do {
1053 struct mmc_command cmd = {0};
1054 struct mmc_data data = {0};
1055 struct mmc_request mrq = {
1056 .cmd = &cmd,
1057 .data = &data
1058 };
1059 struct scatterlist sg;
1060
1061 /* set the phase in delay line hw block */
1062 rc = msm_config_cm_dll_phase(host, phase);
1063 if (rc)
1064 goto kfree;
1065
1066 cmd.opcode = opcode;
1067 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1068
1069 data.blksz = size;
1070 data.blocks = 1;
1071 data.flags = MMC_DATA_READ;
1072 data.timeout_ns = 1000 * 1000 * 1000; /* 1 sec */
1073
1074 data.sg = &sg;
1075 data.sg_len = 1;
1076 sg_init_one(&sg, data_buf, size);
1077 memset(data_buf, 0, size);
1078 mmc_wait_for_req(mmc, &mrq);
1079
1080 if (!cmd.error && !data.error &&
1081 !memcmp(data_buf, tuning_block_pattern, size)) {
1082 /* tuning is successful at this tuning point */
1083 tuned_phases[tuned_phase_cnt++] = phase;
Krishna Konda96e6b112013-10-28 15:25:03 -07001084 pr_debug("%s: %s: found *** good *** phase = %d\n",
1085 mmc_hostname(mmc), __func__, phase);
1086 } else {
1087 pr_debug("%s: %s: found ## bad ## phase = %d\n",
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001088 mmc_hostname(mmc), __func__, phase);
1089 }
1090 } while (++phase < 16);
1091
Sahitya Tummaladfdb4af2014-04-01 14:29:13 +05301092 if ((tuned_phase_cnt == NUM_TUNING_PHASES) &&
1093 card && mmc_card_mmc(card)) {
Krishna Konda96e6b112013-10-28 15:25:03 -07001094 /*
1095 * If all phases pass then its a problem. So change the card's
1096 * drive type to a different value, if supported and repeat
1097 * tuning until at least one phase fails. Then set the original
1098 * drive type back.
1099 *
1100 * If all the phases still pass after trying all possible
1101 * drive types, then one of those 16 phases will be picked.
1102 * This is no different from what was going on before the
1103 * modification to change drive type and retune.
1104 */
1105 pr_debug("%s: tuned phases count: %d\n", mmc_hostname(mmc),
1106 tuned_phase_cnt);
1107
1108 /* set drive type to other value . default setting is 0x0 */
1109 while (++drv_type <= MAX_DRV_TYPES_SUPPORTED_HS200) {
1110 if (card->ext_csd.raw_driver_strength &
1111 (1 << drv_type)) {
1112 sdhci_msm_set_mmc_drv_type(host, opcode,
1113 drv_type);
1114 if (!drv_type_changed)
1115 drv_type_changed = true;
1116 goto retry;
1117 }
1118 }
1119 }
1120
1121 /* reset drive type to default (50 ohm) if changed */
1122 if (drv_type_changed)
1123 sdhci_msm_set_mmc_drv_type(host, opcode, 0);
1124
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001125 if (tuned_phase_cnt) {
1126 rc = msm_find_most_appropriate_phase(host, tuned_phases,
1127 tuned_phase_cnt);
1128 if (rc < 0)
1129 goto kfree;
1130 else
1131 phase = (u8)rc;
1132
1133 /*
1134 * Finally set the selected phase in delay
1135 * line hw block.
1136 */
1137 rc = msm_config_cm_dll_phase(host, phase);
1138 if (rc)
1139 goto kfree;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07001140 msm_host->saved_tuning_phase = phase;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001141 pr_debug("%s: %s: finally setting the tuning phase to %d\n",
1142 mmc_hostname(mmc), __func__, phase);
1143 } else {
Sahitya Tummala9fe16532013-06-13 10:36:57 +05301144 if (--tuning_seq_cnt)
1145 goto retry;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001146 /* tuning failed */
1147 pr_err("%s: %s: no tuning point found\n",
1148 mmc_hostname(mmc), __func__);
Sahitya Tummala9fe16532013-06-13 10:36:57 +05301149 rc = -EIO;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001150 }
1151
1152kfree:
1153 kfree(data_buf);
1154out:
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07001155 spin_lock_irqsave(&host->lock, flags);
1156 if (!rc)
1157 msm_host->tuning_done = true;
1158 spin_unlock_irqrestore(&host->lock, flags);
1159 pr_debug("%s: Exit %s, err(%d)\n", mmc_hostname(mmc), __func__, rc);
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001160 return rc;
1161}
1162
Asutosh Das0ef24812012-12-18 16:14:02 +05301163static int sdhci_msm_setup_gpio(struct sdhci_msm_pltfm_data *pdata, bool enable)
1164{
1165 struct sdhci_msm_gpio_data *curr;
1166 int i, ret = 0;
1167
1168 curr = pdata->pin_data->gpio_data;
1169 for (i = 0; i < curr->size; i++) {
1170 if (!gpio_is_valid(curr->gpio[i].no)) {
1171 ret = -EINVAL;
1172 pr_err("%s: Invalid gpio = %d\n", __func__,
1173 curr->gpio[i].no);
1174 goto free_gpios;
1175 }
1176 if (enable) {
1177 ret = gpio_request(curr->gpio[i].no,
1178 curr->gpio[i].name);
1179 if (ret) {
1180 pr_err("%s: gpio_request(%d, %s) failed %d\n",
1181 __func__, curr->gpio[i].no,
1182 curr->gpio[i].name, ret);
1183 goto free_gpios;
1184 }
1185 curr->gpio[i].is_enabled = true;
1186 } else {
1187 gpio_free(curr->gpio[i].no);
1188 curr->gpio[i].is_enabled = false;
1189 }
1190 }
1191 return ret;
1192
1193free_gpios:
1194 for (i--; i >= 0; i--) {
1195 gpio_free(curr->gpio[i].no);
1196 curr->gpio[i].is_enabled = false;
1197 }
1198 return ret;
1199}
1200
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301201static int sdhci_msm_setup_pinctrl(struct sdhci_msm_pltfm_data *pdata,
1202 bool enable)
1203{
1204 int ret = 0;
1205
1206 if (enable)
1207 ret = pinctrl_select_state(pdata->pctrl_data->pctrl,
1208 pdata->pctrl_data->pins_active);
1209 else
1210 ret = pinctrl_select_state(pdata->pctrl_data->pctrl,
1211 pdata->pctrl_data->pins_sleep);
1212
1213 if (ret < 0)
1214 pr_err("%s state for pinctrl failed with %d\n",
1215 enable ? "Enabling" : "Disabling", ret);
1216
1217 return ret;
1218}
1219
Asutosh Das0ef24812012-12-18 16:14:02 +05301220static int sdhci_msm_setup_pins(struct sdhci_msm_pltfm_data *pdata, bool enable)
1221{
1222 int ret = 0;
1223
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301224 if (pdata->pin_cfg_sts == enable) {
Asutosh Das0ef24812012-12-18 16:14:02 +05301225 return 0;
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301226 } else if (pdata->pctrl_data) {
1227 ret = sdhci_msm_setup_pinctrl(pdata, enable);
1228 goto out;
1229 } else if (!pdata->pin_data) {
1230 return 0;
1231 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301232
Sahitya Tummala1cd7e072014-02-14 13:19:01 +05301233 if (pdata->pin_data->is_gpio)
1234 ret = sdhci_msm_setup_gpio(pdata, enable);
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301235out:
Asutosh Das0ef24812012-12-18 16:14:02 +05301236 if (!ret)
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301237 pdata->pin_cfg_sts = enable;
Asutosh Das0ef24812012-12-18 16:14:02 +05301238
1239 return ret;
1240}
1241
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301242static int sdhci_msm_dt_get_array(struct device *dev, const char *prop_name,
1243 u32 **out, int *len, u32 size)
1244{
1245 int ret = 0;
1246 struct device_node *np = dev->of_node;
1247 size_t sz;
1248 u32 *arr = NULL;
1249
1250 if (!of_get_property(np, prop_name, len)) {
1251 ret = -EINVAL;
1252 goto out;
1253 }
1254 sz = *len = *len / sizeof(*arr);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07001255 if (sz <= 0 || (size > 0 && (sz > size))) {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301256 dev_err(dev, "%s invalid size\n", prop_name);
1257 ret = -EINVAL;
1258 goto out;
1259 }
1260
1261 arr = devm_kzalloc(dev, sz * sizeof(*arr), GFP_KERNEL);
1262 if (!arr) {
1263 dev_err(dev, "%s failed allocating memory\n", prop_name);
1264 ret = -ENOMEM;
1265 goto out;
1266 }
1267
1268 ret = of_property_read_u32_array(np, prop_name, arr, sz);
1269 if (ret < 0) {
1270 dev_err(dev, "%s failed reading array %d\n", prop_name, ret);
1271 goto out;
1272 }
1273 *out = arr;
1274out:
1275 if (ret)
1276 *len = 0;
1277 return ret;
1278}
1279
Asutosh Das0ef24812012-12-18 16:14:02 +05301280#define MAX_PROP_SIZE 32
1281static int sdhci_msm_dt_parse_vreg_info(struct device *dev,
1282 struct sdhci_msm_reg_data **vreg_data, const char *vreg_name)
1283{
1284 int len, ret = 0;
1285 const __be32 *prop;
1286 char prop_name[MAX_PROP_SIZE];
1287 struct sdhci_msm_reg_data *vreg;
1288 struct device_node *np = dev->of_node;
1289
1290 snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", vreg_name);
1291 if (!of_parse_phandle(np, prop_name, 0)) {
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301292 dev_info(dev, "No vreg data found for %s\n", vreg_name);
Asutosh Das0ef24812012-12-18 16:14:02 +05301293 return ret;
1294 }
1295
1296 vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
1297 if (!vreg) {
1298 dev_err(dev, "No memory for vreg: %s\n", vreg_name);
1299 ret = -ENOMEM;
1300 return ret;
1301 }
1302
1303 vreg->name = vreg_name;
1304
1305 snprintf(prop_name, MAX_PROP_SIZE,
1306 "qcom,%s-always-on", vreg_name);
1307 if (of_get_property(np, prop_name, NULL))
1308 vreg->is_always_on = true;
1309
1310 snprintf(prop_name, MAX_PROP_SIZE,
1311 "qcom,%s-lpm-sup", vreg_name);
1312 if (of_get_property(np, prop_name, NULL))
1313 vreg->lpm_sup = true;
1314
1315 snprintf(prop_name, MAX_PROP_SIZE,
1316 "qcom,%s-voltage-level", vreg_name);
1317 prop = of_get_property(np, prop_name, &len);
1318 if (!prop || (len != (2 * sizeof(__be32)))) {
1319 dev_warn(dev, "%s %s property\n",
1320 prop ? "invalid format" : "no", prop_name);
1321 } else {
1322 vreg->low_vol_level = be32_to_cpup(&prop[0]);
1323 vreg->high_vol_level = be32_to_cpup(&prop[1]);
1324 }
1325
1326 snprintf(prop_name, MAX_PROP_SIZE,
1327 "qcom,%s-current-level", vreg_name);
1328 prop = of_get_property(np, prop_name, &len);
1329 if (!prop || (len != (2 * sizeof(__be32)))) {
1330 dev_warn(dev, "%s %s property\n",
1331 prop ? "invalid format" : "no", prop_name);
1332 } else {
1333 vreg->lpm_uA = be32_to_cpup(&prop[0]);
1334 vreg->hpm_uA = be32_to_cpup(&prop[1]);
1335 }
1336
1337 *vreg_data = vreg;
1338 dev_dbg(dev, "%s: %s %s vol=[%d %d]uV, curr=[%d %d]uA\n",
1339 vreg->name, vreg->is_always_on ? "always_on," : "",
1340 vreg->lpm_sup ? "lpm_sup," : "", vreg->low_vol_level,
1341 vreg->high_vol_level, vreg->lpm_uA, vreg->hpm_uA);
1342
1343 return ret;
1344}
1345
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301346static int sdhci_msm_parse_pinctrl_info(struct device *dev,
1347 struct sdhci_msm_pltfm_data *pdata)
1348{
1349 struct sdhci_pinctrl_data *pctrl_data;
1350 struct pinctrl *pctrl;
1351 int ret = 0;
1352
1353 /* Try to obtain pinctrl handle */
1354 pctrl = devm_pinctrl_get(dev);
1355 if (IS_ERR(pctrl)) {
1356 ret = PTR_ERR(pctrl);
1357 goto out;
1358 }
1359 pctrl_data = devm_kzalloc(dev, sizeof(*pctrl_data), GFP_KERNEL);
1360 if (!pctrl_data) {
1361 dev_err(dev, "No memory for sdhci_pinctrl_data\n");
1362 ret = -ENOMEM;
1363 goto out;
1364 }
1365 pctrl_data->pctrl = pctrl;
1366 /* Look-up and keep the states handy to be used later */
1367 pctrl_data->pins_active = pinctrl_lookup_state(
1368 pctrl_data->pctrl, "active");
1369 if (IS_ERR(pctrl_data->pins_active)) {
1370 ret = PTR_ERR(pctrl_data->pins_active);
1371 dev_err(dev, "Could not get active pinstates, err:%d\n", ret);
1372 goto out;
1373 }
1374 pctrl_data->pins_sleep = pinctrl_lookup_state(
1375 pctrl_data->pctrl, "sleep");
1376 if (IS_ERR(pctrl_data->pins_sleep)) {
1377 ret = PTR_ERR(pctrl_data->pins_sleep);
1378 dev_err(dev, "Could not get sleep pinstates, err:%d\n", ret);
1379 goto out;
1380 }
1381 pdata->pctrl_data = pctrl_data;
1382out:
1383 return ret;
1384}
1385
Asutosh Das0ef24812012-12-18 16:14:02 +05301386#define GPIO_NAME_MAX_LEN 32
1387static int sdhci_msm_dt_parse_gpio_info(struct device *dev,
1388 struct sdhci_msm_pltfm_data *pdata)
1389{
1390 int ret = 0, cnt, i;
1391 struct sdhci_msm_pin_data *pin_data;
1392 struct device_node *np = dev->of_node;
1393
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301394 ret = sdhci_msm_parse_pinctrl_info(dev, pdata);
1395 if (!ret) {
1396 goto out;
1397 } else if (ret == -EPROBE_DEFER) {
1398 dev_err(dev, "Pinctrl framework not registered, err:%d\n", ret);
1399 goto out;
1400 } else {
1401 dev_err(dev, "Parsing Pinctrl failed with %d, falling back on GPIO lib\n",
1402 ret);
Sahitya Tummala1cd7e072014-02-14 13:19:01 +05301403 ret = 0;
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301404 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301405 pin_data = devm_kzalloc(dev, sizeof(*pin_data), GFP_KERNEL);
1406 if (!pin_data) {
1407 dev_err(dev, "No memory for pin_data\n");
1408 ret = -ENOMEM;
1409 goto out;
1410 }
1411
1412 cnt = of_gpio_count(np);
1413 if (cnt > 0) {
1414 pin_data->gpio_data = devm_kzalloc(dev,
1415 sizeof(struct sdhci_msm_gpio_data), GFP_KERNEL);
1416 if (!pin_data->gpio_data) {
1417 dev_err(dev, "No memory for gpio_data\n");
1418 ret = -ENOMEM;
1419 goto out;
1420 }
1421 pin_data->gpio_data->size = cnt;
1422 pin_data->gpio_data->gpio = devm_kzalloc(dev, cnt *
1423 sizeof(struct sdhci_msm_gpio), GFP_KERNEL);
1424
1425 if (!pin_data->gpio_data->gpio) {
1426 dev_err(dev, "No memory for gpio\n");
1427 ret = -ENOMEM;
1428 goto out;
1429 }
1430
1431 for (i = 0; i < cnt; i++) {
1432 const char *name = NULL;
1433 char result[GPIO_NAME_MAX_LEN];
1434 pin_data->gpio_data->gpio[i].no = of_get_gpio(np, i);
1435 of_property_read_string_index(np,
1436 "qcom,gpio-names", i, &name);
1437
1438 snprintf(result, GPIO_NAME_MAX_LEN, "%s-%s",
1439 dev_name(dev), name ? name : "?");
1440 pin_data->gpio_data->gpio[i].name = result;
1441 dev_dbg(dev, "%s: gpio[%s] = %d\n", __func__,
1442 pin_data->gpio_data->gpio[i].name,
1443 pin_data->gpio_data->gpio[i].no);
Asutosh Das0ef24812012-12-18 16:14:02 +05301444 }
1445 }
Sahitya Tummala1cd7e072014-02-14 13:19:01 +05301446 pdata->pin_data = pin_data;
Asutosh Das0ef24812012-12-18 16:14:02 +05301447out:
1448 if (ret)
1449 dev_err(dev, "%s failed with err %d\n", __func__, ret);
1450 return ret;
1451}
1452
Maya Erez994cf2f2014-10-21 20:22:04 +03001453#ifdef CONFIG_SMP
1454static void sdhci_msm_populate_affinity_type(struct sdhci_msm_pltfm_data *pdata,
1455 struct device_node *np)
1456{
1457 const char *cpu_affinity = NULL;
1458
1459 pdata->cpu_affinity_type = PM_QOS_REQ_AFFINE_IRQ;
1460 if (!of_property_read_string(np, "qcom,cpu-affinity",
1461 &cpu_affinity)) {
1462 if (!strcmp(cpu_affinity, "all_cores"))
1463 pdata->cpu_affinity_type = PM_QOS_REQ_ALL_CORES;
1464 else if (!strcmp(cpu_affinity, "affine_cores"))
1465 pdata->cpu_affinity_type = PM_QOS_REQ_AFFINE_CORES;
1466 else if (!strcmp(cpu_affinity, "affine_irq"))
1467 pdata->cpu_affinity_type = PM_QOS_REQ_AFFINE_IRQ;
1468 }
1469}
1470#else
1471static void sdhci_msm_populate_affinity_type(struct sdhci_msm_pltfm_data *pdata,
1472 struct device_node *np)
1473{
1474}
1475#endif
1476
Asutosh Das0ef24812012-12-18 16:14:02 +05301477/* Parse platform data */
1478static struct sdhci_msm_pltfm_data *sdhci_msm_populate_pdata(struct device *dev)
1479{
1480 struct sdhci_msm_pltfm_data *pdata = NULL;
1481 struct device_node *np = dev->of_node;
1482 u32 bus_width = 0;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05301483 u32 cpu_dma_latency;
Asutosh Das0ef24812012-12-18 16:14:02 +05301484 int len, i;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301485 int clk_table_len;
1486 u32 *clk_table = NULL;
Sujit Reddy Thumma1958d3d2013-06-03 09:54:32 +05301487 enum of_gpio_flags flags = OF_GPIO_ACTIVE_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05301488
1489 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1490 if (!pdata) {
1491 dev_err(dev, "failed to allocate memory for platform data\n");
1492 goto out;
1493 }
1494
Sujit Reddy Thumma1958d3d2013-06-03 09:54:32 +05301495 pdata->status_gpio = of_get_named_gpio_flags(np, "cd-gpios", 0, &flags);
1496 if (gpio_is_valid(pdata->status_gpio) & !(flags & OF_GPIO_ACTIVE_LOW))
1497 pdata->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
Sahitya Tummala581df132013-03-12 14:57:46 +05301498
Asutosh Das0ef24812012-12-18 16:14:02 +05301499 of_property_read_u32(np, "qcom,bus-width", &bus_width);
1500 if (bus_width == 8)
1501 pdata->mmc_bus_width = MMC_CAP_8_BIT_DATA;
1502 else if (bus_width == 4)
1503 pdata->mmc_bus_width = MMC_CAP_4_BIT_DATA;
1504 else {
1505 dev_notice(dev, "invalid bus-width, default to 1-bit mode\n");
1506 pdata->mmc_bus_width = 0;
1507 }
1508
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05301509 if (!of_property_read_u32(np, "qcom,cpu-dma-latency-us",
1510 &cpu_dma_latency))
1511 pdata->cpu_dma_latency_us = cpu_dma_latency;
Asutosh Das598a4d42014-02-05 16:38:23 +05301512 else
1513 pdata->cpu_dma_latency_us = MSM_MMC_DEFAULT_CPU_DMA_LATENCY;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301514 if (sdhci_msm_dt_get_array(dev, "qcom,clk-rates",
1515 &clk_table, &clk_table_len, 0)) {
1516 dev_err(dev, "failed parsing supported clock rates\n");
1517 goto out;
1518 }
1519 if (!clk_table || !clk_table_len) {
1520 dev_err(dev, "Invalid clock table\n");
1521 goto out;
1522 }
1523 pdata->sup_clk_table = clk_table;
1524 pdata->sup_clk_cnt = clk_table_len;
1525
Asutosh Das0ef24812012-12-18 16:14:02 +05301526 pdata->vreg_data = devm_kzalloc(dev, sizeof(struct
1527 sdhci_msm_slot_reg_data),
1528 GFP_KERNEL);
1529 if (!pdata->vreg_data) {
1530 dev_err(dev, "failed to allocate memory for vreg data\n");
1531 goto out;
1532 }
1533
1534 if (sdhci_msm_dt_parse_vreg_info(dev, &pdata->vreg_data->vdd_data,
1535 "vdd")) {
1536 dev_err(dev, "failed parsing vdd data\n");
1537 goto out;
1538 }
1539 if (sdhci_msm_dt_parse_vreg_info(dev,
1540 &pdata->vreg_data->vdd_io_data,
1541 "vdd-io")) {
1542 dev_err(dev, "failed parsing vdd-io data\n");
1543 goto out;
1544 }
1545
1546 if (sdhci_msm_dt_parse_gpio_info(dev, pdata)) {
1547 dev_err(dev, "failed parsing gpio data\n");
1548 goto out;
1549 }
1550
Asutosh Das0ef24812012-12-18 16:14:02 +05301551 len = of_property_count_strings(np, "qcom,bus-speed-mode");
1552
1553 for (i = 0; i < len; i++) {
1554 const char *name = NULL;
1555
1556 of_property_read_string_index(np,
1557 "qcom,bus-speed-mode", i, &name);
1558 if (!name)
1559 continue;
1560
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07001561 if (!strncmp(name, "HS400_1p8v", sizeof("HS400_1p8v")))
1562 pdata->caps2 |= MMC_CAP2_HS400_1_8V;
1563 else if (!strncmp(name, "HS400_1p2v", sizeof("HS400_1p2v")))
1564 pdata->caps2 |= MMC_CAP2_HS400_1_2V;
1565 else if (!strncmp(name, "HS200_1p8v", sizeof("HS200_1p8v")))
Asutosh Das0ef24812012-12-18 16:14:02 +05301566 pdata->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
1567 else if (!strncmp(name, "HS200_1p2v", sizeof("HS200_1p2v")))
1568 pdata->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
1569 else if (!strncmp(name, "DDR_1p8v", sizeof("DDR_1p8v")))
1570 pdata->caps |= MMC_CAP_1_8V_DDR
1571 | MMC_CAP_UHS_DDR50;
1572 else if (!strncmp(name, "DDR_1p2v", sizeof("DDR_1p2v")))
1573 pdata->caps |= MMC_CAP_1_2V_DDR
1574 | MMC_CAP_UHS_DDR50;
1575 }
1576
1577 if (of_get_property(np, "qcom,nonremovable", NULL))
1578 pdata->nonremovable = true;
1579
Guoping Yuf7c91332014-08-20 16:56:18 +08001580 if (of_get_property(np, "qcom,nonhotplug", NULL))
1581 pdata->nonhotplug = true;
1582
Maya Erez994cf2f2014-10-21 20:22:04 +03001583 sdhci_msm_populate_affinity_type(pdata, np);
1584
Asutosh Das0ef24812012-12-18 16:14:02 +05301585 return pdata;
1586out:
1587 return NULL;
1588}
1589
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301590/* Returns required bandwidth in Bytes per Sec */
1591static unsigned int sdhci_get_bw_required(struct sdhci_host *host,
1592 struct mmc_ios *ios)
1593{
Sahitya Tummala2886c922013-04-03 18:03:31 +05301594 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1595 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1596
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301597 unsigned int bw;
1598
Sahitya Tummala2886c922013-04-03 18:03:31 +05301599 bw = msm_host->clk_rate;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301600 /*
1601 * For DDR mode, SDCC controller clock will be at
1602 * the double rate than the actual clock that goes to card.
1603 */
1604 if (ios->bus_width == MMC_BUS_WIDTH_4)
1605 bw /= 2;
1606 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1607 bw /= 8;
1608
1609 return bw;
1610}
1611
1612static int sdhci_msm_bus_get_vote_for_bw(struct sdhci_msm_host *host,
1613 unsigned int bw)
1614{
1615 unsigned int *table = host->pdata->voting_data->bw_vecs;
1616 unsigned int size = host->pdata->voting_data->bw_vecs_size;
1617 int i;
1618
1619 if (host->msm_bus_vote.is_max_bw_needed && bw)
1620 return host->msm_bus_vote.max_bw_vote;
1621
1622 for (i = 0; i < size; i++) {
1623 if (bw <= table[i])
1624 break;
1625 }
1626
1627 if (i && (i == size))
1628 i--;
1629
1630 return i;
1631}
1632
1633/*
1634 * This function must be called with host lock acquired.
1635 * Caller of this function should also ensure that msm bus client
1636 * handle is not null.
1637 */
1638static inline int sdhci_msm_bus_set_vote(struct sdhci_msm_host *msm_host,
1639 int vote,
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301640 unsigned long *flags)
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301641{
1642 struct sdhci_host *host = platform_get_drvdata(msm_host->pdev);
1643 int rc = 0;
1644
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301645 BUG_ON(!flags);
1646
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301647 if (vote != msm_host->msm_bus_vote.curr_vote) {
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301648 spin_unlock_irqrestore(&host->lock, *flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301649 rc = msm_bus_scale_client_update_request(
1650 msm_host->msm_bus_vote.client_handle, vote);
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301651 spin_lock_irqsave(&host->lock, *flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301652 if (rc) {
1653 pr_err("%s: msm_bus_scale_client_update_request() failed: bus_client_handle=0x%x, vote=%d, err=%d\n",
1654 mmc_hostname(host->mmc),
1655 msm_host->msm_bus_vote.client_handle, vote, rc);
1656 goto out;
1657 }
1658 msm_host->msm_bus_vote.curr_vote = vote;
1659 }
1660out:
1661 return rc;
1662}
1663
1664/*
1665 * Internal work. Work to set 0 bandwidth for msm bus.
1666 */
1667static void sdhci_msm_bus_work(struct work_struct *work)
1668{
1669 struct sdhci_msm_host *msm_host;
1670 struct sdhci_host *host;
1671 unsigned long flags;
1672
1673 msm_host = container_of(work, struct sdhci_msm_host,
1674 msm_bus_vote.vote_work.work);
1675 host = platform_get_drvdata(msm_host->pdev);
1676
1677 if (!msm_host->msm_bus_vote.client_handle)
1678 return;
1679
1680 spin_lock_irqsave(&host->lock, flags);
1681 /* don't vote for 0 bandwidth if any request is in progress */
1682 if (!host->mrq) {
1683 sdhci_msm_bus_set_vote(msm_host,
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301684 msm_host->msm_bus_vote.min_bw_vote, &flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301685 } else
1686 pr_warning("%s: %s: Transfer in progress. skipping bus voting to 0 bandwidth\n",
1687 mmc_hostname(host->mmc), __func__);
1688 spin_unlock_irqrestore(&host->lock, flags);
1689}
1690
1691/*
1692 * This function cancels any scheduled delayed work and sets the bus
1693 * vote based on bw (bandwidth) argument.
1694 */
1695static void sdhci_msm_bus_cancel_work_and_set_vote(struct sdhci_host *host,
1696 unsigned int bw)
1697{
1698 int vote;
1699 unsigned long flags;
1700 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1701 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1702
1703 cancel_delayed_work_sync(&msm_host->msm_bus_vote.vote_work);
1704 spin_lock_irqsave(&host->lock, flags);
1705 vote = sdhci_msm_bus_get_vote_for_bw(msm_host, bw);
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301706 sdhci_msm_bus_set_vote(msm_host, vote, &flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301707 spin_unlock_irqrestore(&host->lock, flags);
1708}
1709
1710#define MSM_MMC_BUS_VOTING_DELAY 200 /* msecs */
1711
1712/* This function queues a work which will set the bandwidth requiement to 0 */
1713static void sdhci_msm_bus_queue_work(struct sdhci_host *host)
1714{
1715 unsigned long flags;
1716 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1717 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1718
1719 spin_lock_irqsave(&host->lock, flags);
1720 if (msm_host->msm_bus_vote.min_bw_vote !=
1721 msm_host->msm_bus_vote.curr_vote)
1722 queue_delayed_work(system_wq,
1723 &msm_host->msm_bus_vote.vote_work,
1724 msecs_to_jiffies(MSM_MMC_BUS_VOTING_DELAY));
1725 spin_unlock_irqrestore(&host->lock, flags);
1726}
1727
1728static int sdhci_msm_bus_register(struct sdhci_msm_host *host,
1729 struct platform_device *pdev)
1730{
1731 int rc = 0;
1732 struct msm_bus_scale_pdata *bus_pdata;
1733
1734 struct sdhci_msm_bus_voting_data *data;
1735 struct device *dev = &pdev->dev;
1736
1737 data = devm_kzalloc(dev,
1738 sizeof(struct sdhci_msm_bus_voting_data), GFP_KERNEL);
1739 if (!data) {
1740 dev_err(&pdev->dev,
1741 "%s: failed to allocate memory\n", __func__);
1742 rc = -ENOMEM;
1743 goto out;
1744 }
1745 data->bus_pdata = msm_bus_cl_get_pdata(pdev);
1746 if (data->bus_pdata) {
1747 rc = sdhci_msm_dt_get_array(dev, "qcom,bus-bw-vectors-bps",
1748 &data->bw_vecs, &data->bw_vecs_size, 0);
1749 if (rc) {
1750 dev_err(&pdev->dev,
1751 "%s: Failed to get bus-bw-vectors-bps\n",
1752 __func__);
1753 goto out;
1754 }
1755 host->pdata->voting_data = data;
1756 }
1757 if (host->pdata->voting_data &&
1758 host->pdata->voting_data->bus_pdata &&
1759 host->pdata->voting_data->bw_vecs &&
1760 host->pdata->voting_data->bw_vecs_size) {
1761
1762 bus_pdata = host->pdata->voting_data->bus_pdata;
1763 host->msm_bus_vote.client_handle =
1764 msm_bus_scale_register_client(bus_pdata);
1765 if (!host->msm_bus_vote.client_handle) {
1766 dev_err(&pdev->dev, "msm_bus_scale_register_client()\n");
1767 rc = -EFAULT;
1768 goto out;
1769 }
1770 /* cache the vote index for minimum and maximum bandwidth */
1771 host->msm_bus_vote.min_bw_vote =
1772 sdhci_msm_bus_get_vote_for_bw(host, 0);
1773 host->msm_bus_vote.max_bw_vote =
1774 sdhci_msm_bus_get_vote_for_bw(host, UINT_MAX);
1775 } else {
1776 devm_kfree(dev, data);
1777 }
1778
1779out:
1780 return rc;
1781}
1782
1783static void sdhci_msm_bus_unregister(struct sdhci_msm_host *host)
1784{
1785 if (host->msm_bus_vote.client_handle)
1786 msm_bus_scale_unregister_client(
1787 host->msm_bus_vote.client_handle);
1788}
1789
1790static void sdhci_msm_bus_voting(struct sdhci_host *host, u32 enable)
1791{
1792 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1793 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1794 struct mmc_ios *ios = &host->mmc->ios;
1795 unsigned int bw;
1796
1797 if (!msm_host->msm_bus_vote.client_handle)
1798 return;
1799
1800 bw = sdhci_get_bw_required(host, ios);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301801 if (enable) {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301802 sdhci_msm_bus_cancel_work_and_set_vote(host, bw);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301803 } else {
1804 /*
1805 * If clock gating is enabled, then remove the vote
1806 * immediately because clocks will be disabled only
1807 * after SDHCI_MSM_MMC_CLK_GATE_DELAY and thus no
1808 * additional delay is required to remove the bus vote.
1809 */
1810#ifdef CONFIG_MMC_CLKGATE
1811 if (host->mmc->clkgate_delay)
1812 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
1813 else
1814#endif
1815 sdhci_msm_bus_queue_work(host);
1816 }
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301817}
1818
Asutosh Das0ef24812012-12-18 16:14:02 +05301819/* Regulator utility functions */
1820static int sdhci_msm_vreg_init_reg(struct device *dev,
1821 struct sdhci_msm_reg_data *vreg)
1822{
1823 int ret = 0;
1824
1825 /* check if regulator is already initialized? */
1826 if (vreg->reg)
1827 goto out;
1828
1829 /* Get the regulator handle */
1830 vreg->reg = devm_regulator_get(dev, vreg->name);
1831 if (IS_ERR(vreg->reg)) {
1832 ret = PTR_ERR(vreg->reg);
1833 pr_err("%s: devm_regulator_get(%s) failed. ret=%d\n",
1834 __func__, vreg->name, ret);
1835 goto out;
1836 }
1837
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301838 if (regulator_count_voltages(vreg->reg) > 0) {
1839 vreg->set_voltage_sup = true;
1840 /* sanity check */
1841 if (!vreg->high_vol_level || !vreg->hpm_uA) {
1842 pr_err("%s: %s invalid constraints specified\n",
1843 __func__, vreg->name);
1844 ret = -EINVAL;
1845 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301846 }
1847
1848out:
1849 return ret;
1850}
1851
1852static void sdhci_msm_vreg_deinit_reg(struct sdhci_msm_reg_data *vreg)
1853{
1854 if (vreg->reg)
1855 devm_regulator_put(vreg->reg);
1856}
1857
1858static int sdhci_msm_vreg_set_optimum_mode(struct sdhci_msm_reg_data
1859 *vreg, int uA_load)
1860{
1861 int ret = 0;
1862
1863 /*
1864 * regulators that do not support regulator_set_voltage also
1865 * do not support regulator_set_optimum_mode
1866 */
1867 if (vreg->set_voltage_sup) {
1868 ret = regulator_set_load(vreg->reg, uA_load);
1869 if (ret < 0)
1870 pr_err("%s: regulator_set_load(reg=%s,uA_load=%d) failed. ret=%d\n",
1871 __func__, vreg->name, uA_load, ret);
1872 else
1873 /*
1874 * regulator_set_load() can return non zero
1875 * value even for success case.
1876 */
1877 ret = 0;
1878 }
1879 return ret;
1880}
1881
1882static int sdhci_msm_vreg_set_voltage(struct sdhci_msm_reg_data *vreg,
1883 int min_uV, int max_uV)
1884{
1885 int ret = 0;
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301886 if (vreg->set_voltage_sup) {
1887 ret = regulator_set_voltage(vreg->reg, min_uV, max_uV);
1888 if (ret) {
1889 pr_err("%s: regulator_set_voltage(%s)failed. min_uV=%d,max_uV=%d,ret=%d\n",
Asutosh Das0ef24812012-12-18 16:14:02 +05301890 __func__, vreg->name, min_uV, max_uV, ret);
1891 }
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301892 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301893
1894 return ret;
1895}
1896
1897static int sdhci_msm_vreg_enable(struct sdhci_msm_reg_data *vreg)
1898{
1899 int ret = 0;
1900
1901 /* Put regulator in HPM (high power mode) */
1902 ret = sdhci_msm_vreg_set_optimum_mode(vreg, vreg->hpm_uA);
1903 if (ret < 0)
1904 return ret;
1905
1906 if (!vreg->is_enabled) {
1907 /* Set voltage level */
1908 ret = sdhci_msm_vreg_set_voltage(vreg, vreg->high_vol_level,
1909 vreg->high_vol_level);
1910 if (ret)
1911 return ret;
1912 }
1913 ret = regulator_enable(vreg->reg);
1914 if (ret) {
1915 pr_err("%s: regulator_enable(%s) failed. ret=%d\n",
1916 __func__, vreg->name, ret);
1917 return ret;
1918 }
1919 vreg->is_enabled = true;
1920 return ret;
1921}
1922
1923static int sdhci_msm_vreg_disable(struct sdhci_msm_reg_data *vreg)
1924{
1925 int ret = 0;
1926
1927 /* Never disable regulator marked as always_on */
1928 if (vreg->is_enabled && !vreg->is_always_on) {
1929 ret = regulator_disable(vreg->reg);
1930 if (ret) {
1931 pr_err("%s: regulator_disable(%s) failed. ret=%d\n",
1932 __func__, vreg->name, ret);
1933 goto out;
1934 }
1935 vreg->is_enabled = false;
1936
1937 ret = sdhci_msm_vreg_set_optimum_mode(vreg, 0);
1938 if (ret < 0)
1939 goto out;
1940
1941 /* Set min. voltage level to 0 */
1942 ret = sdhci_msm_vreg_set_voltage(vreg, 0, vreg->high_vol_level);
1943 if (ret)
1944 goto out;
1945 } else if (vreg->is_enabled && vreg->is_always_on) {
1946 if (vreg->lpm_sup) {
1947 /* Put always_on regulator in LPM (low power mode) */
1948 ret = sdhci_msm_vreg_set_optimum_mode(vreg,
1949 vreg->lpm_uA);
1950 if (ret < 0)
1951 goto out;
1952 }
1953 }
1954out:
1955 return ret;
1956}
1957
1958static int sdhci_msm_setup_vreg(struct sdhci_msm_pltfm_data *pdata,
1959 bool enable, bool is_init)
1960{
1961 int ret = 0, i;
1962 struct sdhci_msm_slot_reg_data *curr_slot;
1963 struct sdhci_msm_reg_data *vreg_table[2];
1964
1965 curr_slot = pdata->vreg_data;
1966 if (!curr_slot) {
1967 pr_debug("%s: vreg info unavailable,assuming the slot is powered by always on domain\n",
1968 __func__);
1969 goto out;
1970 }
1971
1972 vreg_table[0] = curr_slot->vdd_data;
1973 vreg_table[1] = curr_slot->vdd_io_data;
1974
1975 for (i = 0; i < ARRAY_SIZE(vreg_table); i++) {
1976 if (vreg_table[i]) {
1977 if (enable)
1978 ret = sdhci_msm_vreg_enable(vreg_table[i]);
1979 else
1980 ret = sdhci_msm_vreg_disable(vreg_table[i]);
1981 if (ret)
1982 goto out;
1983 }
1984 }
1985out:
1986 return ret;
1987}
1988
1989/*
1990 * Reset vreg by ensuring it is off during probe. A call
1991 * to enable vreg is needed to balance disable vreg
1992 */
1993static int sdhci_msm_vreg_reset(struct sdhci_msm_pltfm_data *pdata)
1994{
1995 int ret;
1996
1997 ret = sdhci_msm_setup_vreg(pdata, 1, true);
1998 if (ret)
1999 return ret;
2000 ret = sdhci_msm_setup_vreg(pdata, 0, true);
2001 return ret;
2002}
2003
2004/* This init function should be called only once for each SDHC slot */
2005static int sdhci_msm_vreg_init(struct device *dev,
2006 struct sdhci_msm_pltfm_data *pdata,
2007 bool is_init)
2008{
2009 int ret = 0;
2010 struct sdhci_msm_slot_reg_data *curr_slot;
2011 struct sdhci_msm_reg_data *curr_vdd_reg, *curr_vdd_io_reg;
2012
2013 curr_slot = pdata->vreg_data;
2014 if (!curr_slot)
2015 goto out;
2016
2017 curr_vdd_reg = curr_slot->vdd_data;
2018 curr_vdd_io_reg = curr_slot->vdd_io_data;
2019
2020 if (!is_init)
2021 /* Deregister all regulators from regulator framework */
2022 goto vdd_io_reg_deinit;
2023
2024 /*
2025 * Get the regulator handle from voltage regulator framework
2026 * and then try to set the voltage level for the regulator
2027 */
2028 if (curr_vdd_reg) {
2029 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_reg);
2030 if (ret)
2031 goto out;
2032 }
2033 if (curr_vdd_io_reg) {
2034 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_io_reg);
2035 if (ret)
2036 goto vdd_reg_deinit;
2037 }
2038 ret = sdhci_msm_vreg_reset(pdata);
2039 if (ret)
2040 dev_err(dev, "vreg reset failed (%d)\n", ret);
2041 goto out;
2042
2043vdd_io_reg_deinit:
2044 if (curr_vdd_io_reg)
2045 sdhci_msm_vreg_deinit_reg(curr_vdd_io_reg);
2046vdd_reg_deinit:
2047 if (curr_vdd_reg)
2048 sdhci_msm_vreg_deinit_reg(curr_vdd_reg);
2049out:
2050 return ret;
2051}
2052
2053
2054static int sdhci_msm_set_vdd_io_vol(struct sdhci_msm_pltfm_data *pdata,
2055 enum vdd_io_level level,
2056 unsigned int voltage_level)
2057{
2058 int ret = 0;
2059 int set_level;
2060 struct sdhci_msm_reg_data *vdd_io_reg;
2061
2062 if (!pdata->vreg_data)
2063 return ret;
2064
2065 vdd_io_reg = pdata->vreg_data->vdd_io_data;
2066 if (vdd_io_reg && vdd_io_reg->is_enabled) {
2067 switch (level) {
2068 case VDD_IO_LOW:
2069 set_level = vdd_io_reg->low_vol_level;
2070 break;
2071 case VDD_IO_HIGH:
2072 set_level = vdd_io_reg->high_vol_level;
2073 break;
2074 case VDD_IO_SET_LEVEL:
2075 set_level = voltage_level;
2076 break;
2077 default:
2078 pr_err("%s: invalid argument level = %d",
2079 __func__, level);
2080 ret = -EINVAL;
2081 return ret;
2082 }
2083 ret = sdhci_msm_vreg_set_voltage(vdd_io_reg, set_level,
2084 set_level);
2085 }
2086 return ret;
2087}
2088
2089static irqreturn_t sdhci_msm_pwr_irq(int irq, void *data)
2090{
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002091 struct sdhci_host *host = (struct sdhci_host *)data;
2092 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2093 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Das0ef24812012-12-18 16:14:02 +05302094 u8 irq_status = 0;
2095 u8 irq_ack = 0;
2096 int ret = 0;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302097 int pwr_state = 0, io_level = 0;
2098 unsigned long flags;
Asutosh Das0ef24812012-12-18 16:14:02 +05302099
2100 irq_status = readb_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
2101 pr_debug("%s: Received IRQ(%d), status=0x%x\n",
2102 mmc_hostname(msm_host->mmc), irq, irq_status);
2103
2104 /* Clear the interrupt */
2105 writeb_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
2106 /*
2107 * SDHC has core_mem and hc_mem device memory and these memory
2108 * addresses do not fall within 1KB region. Hence, any update to
2109 * core_mem address space would require an mb() to ensure this gets
2110 * completed before its next update to registers within hc_mem.
2111 */
2112 mb();
2113
2114 /* Handle BUS ON/OFF*/
2115 if (irq_status & CORE_PWRCTL_BUS_ON) {
2116 ret = sdhci_msm_setup_vreg(msm_host->pdata, true, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302117 if (!ret) {
Asutosh Das0ef24812012-12-18 16:14:02 +05302118 ret = sdhci_msm_setup_pins(msm_host->pdata, true);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302119 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
2120 VDD_IO_HIGH, 0);
2121 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302122 if (ret)
2123 irq_ack |= CORE_PWRCTL_BUS_FAIL;
2124 else
2125 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302126
2127 pwr_state = REQ_BUS_ON;
2128 io_level = REQ_IO_HIGH;
Asutosh Das0ef24812012-12-18 16:14:02 +05302129 }
2130 if (irq_status & CORE_PWRCTL_BUS_OFF) {
2131 ret = sdhci_msm_setup_vreg(msm_host->pdata, false, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302132 if (!ret) {
Asutosh Das0ef24812012-12-18 16:14:02 +05302133 ret = sdhci_msm_setup_pins(msm_host->pdata, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302134 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
2135 VDD_IO_LOW, 0);
2136 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302137 if (ret)
2138 irq_ack |= CORE_PWRCTL_BUS_FAIL;
2139 else
2140 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302141
2142 pwr_state = REQ_BUS_OFF;
2143 io_level = REQ_IO_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05302144 }
2145 /* Handle IO LOW/HIGH */
2146 if (irq_status & CORE_PWRCTL_IO_LOW) {
2147 /* Switch voltage Low */
2148 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_LOW, 0);
2149 if (ret)
2150 irq_ack |= CORE_PWRCTL_IO_FAIL;
2151 else
2152 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302153
2154 io_level = REQ_IO_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05302155 }
2156 if (irq_status & CORE_PWRCTL_IO_HIGH) {
2157 /* Switch voltage High */
2158 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_HIGH, 0);
2159 if (ret)
2160 irq_ack |= CORE_PWRCTL_IO_FAIL;
2161 else
2162 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302163
2164 io_level = REQ_IO_HIGH;
Asutosh Das0ef24812012-12-18 16:14:02 +05302165 }
2166
2167 /* ACK status to the core */
2168 writeb_relaxed(irq_ack, (msm_host->core_mem + CORE_PWRCTL_CTL));
2169 /*
2170 * SDHC has core_mem and hc_mem device memory and these memory
2171 * addresses do not fall within 1KB region. Hence, any update to
2172 * core_mem address space would require an mb() to ensure this gets
2173 * completed before its next update to registers within hc_mem.
2174 */
2175 mb();
2176
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302177 if (io_level & REQ_IO_HIGH)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002178 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2179 ~CORE_IO_PAD_PWR_SWITCH),
2180 host->ioaddr + CORE_VENDOR_SPEC);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302181 else if (io_level & REQ_IO_LOW)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002182 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
2183 CORE_IO_PAD_PWR_SWITCH),
2184 host->ioaddr + CORE_VENDOR_SPEC);
2185 mb();
2186
Asutosh Das0ef24812012-12-18 16:14:02 +05302187 pr_debug("%s: Handled IRQ(%d), ret=%d, ack=0x%x\n",
2188 mmc_hostname(msm_host->mmc), irq, ret, irq_ack);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302189 spin_lock_irqsave(&host->lock, flags);
2190 if (pwr_state)
2191 msm_host->curr_pwr_state = pwr_state;
2192 if (io_level)
2193 msm_host->curr_io_level = io_level;
2194 complete(&msm_host->pwr_irq_completion);
2195 spin_unlock_irqrestore(&host->lock, flags);
2196
Asutosh Das0ef24812012-12-18 16:14:02 +05302197 return IRQ_HANDLED;
2198}
2199
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302200static ssize_t
Sahitya Tummala5c55b932013-06-20 14:00:18 +05302201show_polling(struct device *dev, struct device_attribute *attr, char *buf)
2202{
2203 struct sdhci_host *host = dev_get_drvdata(dev);
2204 int poll;
2205 unsigned long flags;
2206
2207 spin_lock_irqsave(&host->lock, flags);
2208 poll = !!(host->mmc->caps & MMC_CAP_NEEDS_POLL);
2209 spin_unlock_irqrestore(&host->lock, flags);
2210
2211 return snprintf(buf, PAGE_SIZE, "%d\n", poll);
2212}
2213
2214static ssize_t
2215store_polling(struct device *dev, struct device_attribute *attr,
2216 const char *buf, size_t count)
2217{
2218 struct sdhci_host *host = dev_get_drvdata(dev);
2219 int value;
2220 unsigned long flags;
2221
2222 if (!kstrtou32(buf, 0, &value)) {
2223 spin_lock_irqsave(&host->lock, flags);
2224 if (value) {
2225 host->mmc->caps |= MMC_CAP_NEEDS_POLL;
2226 mmc_detect_change(host->mmc, 0);
2227 } else {
2228 host->mmc->caps &= ~MMC_CAP_NEEDS_POLL;
2229 }
2230 spin_unlock_irqrestore(&host->lock, flags);
2231 }
2232 return count;
2233}
2234
2235static ssize_t
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302236show_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
2237 char *buf)
2238{
2239 struct sdhci_host *host = dev_get_drvdata(dev);
2240 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2241 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2242
2243 return snprintf(buf, PAGE_SIZE, "%u\n",
2244 msm_host->msm_bus_vote.is_max_bw_needed);
2245}
2246
2247static ssize_t
2248store_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
2249 const char *buf, size_t count)
2250{
2251 struct sdhci_host *host = dev_get_drvdata(dev);
2252 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2253 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2254 uint32_t value;
2255 unsigned long flags;
2256
2257 if (!kstrtou32(buf, 0, &value)) {
2258 spin_lock_irqsave(&host->lock, flags);
2259 msm_host->msm_bus_vote.is_max_bw_needed = !!value;
2260 spin_unlock_irqrestore(&host->lock, flags);
2261 }
2262 return count;
2263}
2264
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302265static void sdhci_msm_check_power_status(struct sdhci_host *host, u32 req_type)
Asutosh Das0ef24812012-12-18 16:14:02 +05302266{
2267 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2268 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302269 unsigned long flags;
2270 bool done = false;
Sahitya Tummala481fbb02013-08-06 15:22:28 +05302271 u32 io_sig_sts;
Asutosh Das0ef24812012-12-18 16:14:02 +05302272
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302273 spin_lock_irqsave(&host->lock, flags);
2274 pr_debug("%s: %s: request %d curr_pwr_state %x curr_io_level %x\n",
2275 mmc_hostname(host->mmc), __func__, req_type,
2276 msm_host->curr_pwr_state, msm_host->curr_io_level);
Sahitya Tummala481fbb02013-08-06 15:22:28 +05302277 io_sig_sts = readl_relaxed(msm_host->core_mem + CORE_GENERICS);
2278 /*
2279 * The IRQ for request type IO High/Low will be generated when -
2280 * 1. SWITCHABLE_SIGNALLING_VOL is enabled in HW.
2281 * 2. If 1 is true and when there is a state change in 1.8V enable
2282 * bit (bit 3) of SDHCI_HOST_CONTROL2 register. The reset state of
2283 * that bit is 0 which indicates 3.3V IO voltage. So, when MMC core
2284 * layer tries to set it to 3.3V before card detection happens, the
2285 * IRQ doesn't get triggered as there is no state change in this bit.
2286 * The driver already handles this case by changing the IO voltage
2287 * level to high as part of controller power up sequence. Hence, check
2288 * for host->pwr to handle a case where IO voltage high request is
2289 * issued even before controller power up.
2290 */
2291 if (req_type & (REQ_IO_HIGH | REQ_IO_LOW)) {
2292 if (!(io_sig_sts & SWITCHABLE_SIGNALLING_VOL) ||
2293 ((req_type & REQ_IO_HIGH) && !host->pwr)) {
2294 pr_debug("%s: do not wait for power IRQ that never comes\n",
2295 mmc_hostname(host->mmc));
2296 spin_unlock_irqrestore(&host->lock, flags);
2297 return;
2298 }
2299 }
2300
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302301 if ((req_type & msm_host->curr_pwr_state) ||
2302 (req_type & msm_host->curr_io_level))
2303 done = true;
2304 spin_unlock_irqrestore(&host->lock, flags);
Asutosh Das0ef24812012-12-18 16:14:02 +05302305
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302306 /*
2307 * This is needed here to hanlde a case where IRQ gets
2308 * triggered even before this function is called so that
2309 * x->done counter of completion gets reset. Otherwise,
2310 * next call to wait_for_completion returns immediately
2311 * without actually waiting for the IRQ to be handled.
2312 */
2313 if (done)
2314 init_completion(&msm_host->pwr_irq_completion);
2315 else
2316 wait_for_completion(&msm_host->pwr_irq_completion);
2317
2318 pr_debug("%s: %s: request %d done\n", mmc_hostname(host->mmc),
2319 __func__, req_type);
Asutosh Das0ef24812012-12-18 16:14:02 +05302320}
2321
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002322static void sdhci_msm_toggle_cdr(struct sdhci_host *host, bool enable)
2323{
2324 if (enable)
2325 writel_relaxed((readl_relaxed(host->ioaddr +
2326 CORE_DLL_CONFIG) | CORE_CDR_EN),
2327 host->ioaddr + CORE_DLL_CONFIG);
2328 else
2329 writel_relaxed((readl_relaxed(host->ioaddr +
2330 CORE_DLL_CONFIG) & ~CORE_CDR_EN),
2331 host->ioaddr + CORE_DLL_CONFIG);
2332}
2333
Asutosh Das648f9d12013-01-10 21:11:04 +05302334static unsigned int sdhci_msm_max_segs(void)
2335{
2336 return SDHCI_MSM_MAX_SEGMENTS;
2337}
2338
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302339static unsigned int sdhci_msm_get_min_clock(struct sdhci_host *host)
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302340{
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302341 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2342 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302343
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302344 return msm_host->pdata->sup_clk_table[0];
2345}
2346
2347static unsigned int sdhci_msm_get_max_clock(struct sdhci_host *host)
2348{
2349 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2350 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2351 int max_clk_index = msm_host->pdata->sup_clk_cnt;
2352
2353 return msm_host->pdata->sup_clk_table[max_clk_index - 1];
2354}
2355
2356static unsigned int sdhci_msm_get_sup_clk_rate(struct sdhci_host *host,
2357 u32 req_clk)
2358{
2359 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2360 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2361 unsigned int sel_clk = -1;
2362 unsigned char cnt;
2363
2364 if (req_clk < sdhci_msm_get_min_clock(host)) {
2365 sel_clk = sdhci_msm_get_min_clock(host);
2366 return sel_clk;
2367 }
2368
2369 for (cnt = 0; cnt < msm_host->pdata->sup_clk_cnt; cnt++) {
2370 if (msm_host->pdata->sup_clk_table[cnt] > req_clk) {
2371 break;
2372 } else if (msm_host->pdata->sup_clk_table[cnt] == req_clk) {
2373 sel_clk = msm_host->pdata->sup_clk_table[cnt];
2374 break;
2375 } else {
2376 sel_clk = msm_host->pdata->sup_clk_table[cnt];
2377 }
2378 }
2379 return sel_clk;
2380}
2381
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302382static int sdhci_msm_enable_controller_clock(struct sdhci_host *host)
2383{
2384 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2385 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2386 int rc = 0;
2387
2388 if (atomic_read(&msm_host->controller_clock))
2389 return 0;
2390
2391 sdhci_msm_bus_voting(host, 1);
2392
2393 if (!IS_ERR(msm_host->pclk)) {
2394 rc = clk_prepare_enable(msm_host->pclk);
2395 if (rc) {
2396 pr_err("%s: %s: failed to enable the pclk with error %d\n",
2397 mmc_hostname(host->mmc), __func__, rc);
2398 goto remove_vote;
2399 }
2400 }
2401
2402 rc = clk_prepare_enable(msm_host->clk);
2403 if (rc) {
2404 pr_err("%s: %s: failed to enable the host-clk with error %d\n",
2405 mmc_hostname(host->mmc), __func__, rc);
2406 goto disable_pclk;
2407 }
2408
2409 atomic_set(&msm_host->controller_clock, 1);
2410 pr_debug("%s: %s: enabled controller clock\n",
2411 mmc_hostname(host->mmc), __func__);
2412 goto out;
2413
2414disable_pclk:
2415 if (!IS_ERR(msm_host->pclk))
2416 clk_disable_unprepare(msm_host->pclk);
2417remove_vote:
2418 if (msm_host->msm_bus_vote.client_handle)
2419 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2420out:
2421 return rc;
2422}
2423
2424
2425
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302426static int sdhci_msm_prepare_clocks(struct sdhci_host *host, bool enable)
2427{
2428 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2429 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2430 int rc = 0;
2431
2432 if (enable && !atomic_read(&msm_host->clks_on)) {
2433 pr_debug("%s: request to enable clocks\n",
2434 mmc_hostname(host->mmc));
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302435
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302436 /*
2437 * The bus-width or the clock rate might have changed
2438 * after controller clocks are enbaled, update bus vote
2439 * in such case.
2440 */
2441 if (atomic_read(&msm_host->controller_clock))
2442 sdhci_msm_bus_voting(host, 1);
2443
2444 rc = sdhci_msm_enable_controller_clock(host);
2445 if (rc)
2446 goto remove_vote;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302447
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302448 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
2449 rc = clk_prepare_enable(msm_host->bus_clk);
2450 if (rc) {
2451 pr_err("%s: %s: failed to enable the bus-clock with error %d\n",
2452 mmc_hostname(host->mmc), __func__, rc);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302453 goto disable_controller_clk;
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302454 }
2455 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002456 if (!IS_ERR(msm_host->ff_clk)) {
2457 rc = clk_prepare_enable(msm_host->ff_clk);
2458 if (rc) {
2459 pr_err("%s: %s: failed to enable the ff_clk with error %d\n",
2460 mmc_hostname(host->mmc), __func__, rc);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302461 goto disable_bus_clk;
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002462 }
2463 }
2464 if (!IS_ERR(msm_host->sleep_clk)) {
2465 rc = clk_prepare_enable(msm_host->sleep_clk);
2466 if (rc) {
2467 pr_err("%s: %s: failed to enable the sleep_clk with error %d\n",
2468 mmc_hostname(host->mmc), __func__, rc);
2469 goto disable_ff_clk;
2470 }
2471 }
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302472 mb();
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302473
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302474 } else if (!enable && atomic_read(&msm_host->clks_on)) {
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302475 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
2476 mb();
Sahitya Tummaladc182982013-08-20 15:32:09 +05302477 /*
2478 * During 1.8V signal switching the clock source must
2479 * still be ON as it requires accessing SDHC
2480 * registers (SDHCi host control2 register bit 3 must
2481 * be written and polled after stopping the SDCLK).
2482 */
2483 if (host->mmc->card_clock_off)
2484 return 0;
2485 pr_debug("%s: request to disable clocks\n",
2486 mmc_hostname(host->mmc));
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002487 if (!IS_ERR_OR_NULL(msm_host->sleep_clk))
2488 clk_disable_unprepare(msm_host->sleep_clk);
2489 if (!IS_ERR_OR_NULL(msm_host->ff_clk))
2490 clk_disable_unprepare(msm_host->ff_clk);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302491 clk_disable_unprepare(msm_host->clk);
2492 if (!IS_ERR(msm_host->pclk))
2493 clk_disable_unprepare(msm_host->pclk);
2494 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2495 clk_disable_unprepare(msm_host->bus_clk);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302496
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302497 atomic_set(&msm_host->controller_clock, 0);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302498 sdhci_msm_bus_voting(host, 0);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302499 }
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302500 atomic_set(&msm_host->clks_on, enable);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302501 goto out;
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002502disable_ff_clk:
2503 if (!IS_ERR_OR_NULL(msm_host->ff_clk))
2504 clk_disable_unprepare(msm_host->ff_clk);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302505disable_bus_clk:
2506 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2507 clk_disable_unprepare(msm_host->bus_clk);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302508disable_controller_clk:
2509 if (!IS_ERR_OR_NULL(msm_host->clk))
2510 clk_disable_unprepare(msm_host->clk);
2511 if (!IS_ERR_OR_NULL(msm_host->pclk))
2512 clk_disable_unprepare(msm_host->pclk);
2513 atomic_set(&msm_host->controller_clock, 0);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302514remove_vote:
2515 if (msm_host->msm_bus_vote.client_handle)
2516 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302517out:
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302518 return rc;
2519}
2520
2521static void sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock)
2522{
2523 int rc;
2524 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2525 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2526 struct mmc_ios curr_ios = host->mmc->ios;
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002527 u32 sup_clock, ddr_clock, dll_lock;
Sahitya Tummala043744a2013-06-24 09:55:33 +05302528 bool curr_pwrsave;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302529
2530 if (!clock) {
Sujit Reddy Thummabf1aecc2014-01-10 10:58:54 +05302531 /*
2532 * disable pwrsave to ensure clock is not auto-gated until
2533 * the rate is >400KHz (initialization complete).
2534 */
2535 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2536 ~CORE_CLK_PWRSAVE, host->ioaddr + CORE_VENDOR_SPEC);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302537 sdhci_msm_prepare_clocks(host, false);
2538 host->clock = clock;
2539 goto out;
2540 }
2541
2542 rc = sdhci_msm_prepare_clocks(host, true);
2543 if (rc)
2544 goto out;
2545
Sahitya Tummala043744a2013-06-24 09:55:33 +05302546 curr_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2547 CORE_CLK_PWRSAVE);
Sahitya Tummalae000b242013-08-29 16:21:08 +05302548 if ((clock > 400000) &&
Venkat Gopalakrishnane6659b92015-01-21 15:51:37 -08002549 !curr_pwrsave)
Sahitya Tummala043744a2013-06-24 09:55:33 +05302550 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2551 | CORE_CLK_PWRSAVE,
2552 host->ioaddr + CORE_VENDOR_SPEC);
2553 /*
2554 * Disable pwrsave for a newly added card if doesn't allow clock
2555 * gating.
2556 */
Venkat Gopalakrishnane6659b92015-01-21 15:51:37 -08002557 else if (curr_pwrsave)
Sahitya Tummala043744a2013-06-24 09:55:33 +05302558 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2559 & ~CORE_CLK_PWRSAVE,
2560 host->ioaddr + CORE_VENDOR_SPEC);
2561
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302562 sup_clock = sdhci_msm_get_sup_clk_rate(host, clock);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002563 if ((curr_ios.timing == MMC_TIMING_UHS_DDR50) ||
Venkat Gopalakrishnan0a29da92015-01-09 12:19:16 -08002564 (curr_ios.timing == MMC_TIMING_MMC_DDR52) ||
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002565 (curr_ios.timing == MMC_TIMING_MMC_HS400)) {
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302566 /*
2567 * The SDHC requires internal clock frequency to be double the
2568 * actual clock that will be set for DDR mode. The controller
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002569 * uses the faster clock(100/400MHz) for some of its parts and
2570 * send the actual required clock (50/200MHz) to the card.
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302571 */
2572 ddr_clock = clock * 2;
2573 sup_clock = sdhci_msm_get_sup_clk_rate(host,
2574 ddr_clock);
2575 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002576
2577 /*
2578 * In general all timing modes are controlled via UHS mode select in
2579 * Host Control2 register. eMMC specific HS200/HS400 doesn't have
2580 * their respective modes defined here, hence we use these values.
2581 *
2582 * HS200 - SDR104 (Since they both are equivalent in functionality)
2583 * HS400 - This involves multiple configurations
2584 * Initially SDR104 - when tuning is required as HS200
2585 * Then when switching to DDR @ 400MHz (HS400) we use
2586 * the vendor specific HC_SELECT_IN to control the mode.
2587 *
2588 * In addition to controlling the modes we also need to select the
2589 * correct input clock for DLL depending on the mode.
2590 *
2591 * HS400 - divided clock (free running MCLK/2)
2592 * All other modes - default (free running MCLK)
2593 */
2594 if (curr_ios.timing == MMC_TIMING_MMC_HS400) {
2595 /* Select the divided clock (free running MCLK/2) */
2596 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2597 & ~CORE_HC_MCLK_SEL_MASK)
2598 | CORE_HC_MCLK_SEL_HS400),
2599 host->ioaddr + CORE_VENDOR_SPEC);
2600 /*
2601 * Select HS400 mode using the HC_SELECT_IN from VENDOR SPEC
2602 * register
2603 */
2604 if (msm_host->tuning_done && !msm_host->calibration_done) {
2605 /*
2606 * Write 0x6 to HC_SELECT_IN and 1 to HC_SELECT_IN_EN
2607 * field in VENDOR_SPEC_FUNC
2608 */
2609 writel_relaxed((readl_relaxed(host->ioaddr + \
2610 CORE_VENDOR_SPEC)
2611 | CORE_HC_SELECT_IN_HS400
2612 | CORE_HC_SELECT_IN_EN),
2613 host->ioaddr + CORE_VENDOR_SPEC);
2614 }
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002615 if (!host->mmc->ios.old_rate && !msm_host->use_cdclp533) {
2616 /*
2617 * Poll on DLL_LOCK and DDR_DLL_LOCK bits in
2618 * CORE_DLL_STATUS to be set. This should get set
2619 * with in 15 us at 200 MHz.
2620 */
2621 rc = readl_poll_timeout(host->ioaddr + CORE_DLL_STATUS,
2622 dll_lock, (dll_lock & (CORE_DLL_LOCK |
2623 CORE_DDR_DLL_LOCK)), 10, 1000);
2624 if (rc == -ETIMEDOUT)
2625 pr_err("%s: Unable to get DLL_LOCK/DDR_DLL_LOCK, dll_status: 0x%08x\n",
2626 mmc_hostname(host->mmc),
2627 dll_lock);
2628 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002629 } else {
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002630 if (!msm_host->use_cdclp533)
2631 /* set CORE_PWRSAVE_DLL bit in CORE_VENDOR_SPEC3 */
2632 writel_relaxed((readl_relaxed(host->ioaddr +
2633 CORE_VENDOR_SPEC3) & ~CORE_PWRSAVE_DLL),
2634 host->ioaddr + CORE_VENDOR_SPEC3);
2635
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002636 /* Select the default clock (free running MCLK) */
2637 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2638 & ~CORE_HC_MCLK_SEL_MASK)
2639 | CORE_HC_MCLK_SEL_DFLT),
2640 host->ioaddr + CORE_VENDOR_SPEC);
2641
2642 /*
2643 * Disable HC_SELECT_IN to be able to use the UHS mode select
2644 * configuration from Host Control2 register for all other
2645 * modes.
2646 *
2647 * Write 0 to HC_SELECT_IN and HC_SELECT_IN_EN field
2648 * in VENDOR_SPEC_FUNC
2649 */
2650 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2651 & ~CORE_HC_SELECT_IN_EN
2652 & ~CORE_HC_SELECT_IN_MASK),
2653 host->ioaddr + CORE_VENDOR_SPEC);
2654 }
2655 mb();
2656
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302657 if (sup_clock != msm_host->clk_rate) {
2658 pr_debug("%s: %s: setting clk rate to %u\n",
2659 mmc_hostname(host->mmc), __func__, sup_clock);
2660 rc = clk_set_rate(msm_host->clk, sup_clock);
2661 if (rc) {
2662 pr_err("%s: %s: Failed to set rate %u for host-clk : %d\n",
2663 mmc_hostname(host->mmc), __func__,
2664 sup_clock, rc);
2665 goto out;
2666 }
2667 msm_host->clk_rate = sup_clock;
2668 host->clock = clock;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302669 /*
2670 * Update the bus vote in case of frequency change due to
2671 * clock scaling.
2672 */
2673 sdhci_msm_bus_voting(host, 1);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302674 }
2675out:
2676 sdhci_set_clock(host, clock);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302677}
2678
Sahitya Tummala14613432013-03-21 11:13:25 +05302679static void sdhci_msm_set_uhs_signaling(struct sdhci_host *host,
2680 unsigned int uhs)
2681{
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002682 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2683 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala14613432013-03-21 11:13:25 +05302684 u16 ctrl_2;
2685
2686 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2687 /* Select Bus Speed Mode for host */
2688 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Venkat Gopalakrishnan0a29da92015-01-09 12:19:16 -08002689 if ((uhs == MMC_TIMING_MMC_HS400) ||
2690 (uhs == MMC_TIMING_MMC_HS200) ||
2691 (uhs == MMC_TIMING_UHS_SDR104))
Sahitya Tummala14613432013-03-21 11:13:25 +05302692 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2693 else if (uhs == MMC_TIMING_UHS_SDR12)
2694 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
2695 else if (uhs == MMC_TIMING_UHS_SDR25)
2696 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
2697 else if (uhs == MMC_TIMING_UHS_SDR50)
2698 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
Venkat Gopalakrishnan0a29da92015-01-09 12:19:16 -08002699 else if ((uhs == MMC_TIMING_UHS_DDR50) ||
2700 (uhs == MMC_TIMING_MMC_DDR52))
Sahitya Tummala14613432013-03-21 11:13:25 +05302701 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302702 /*
2703 * When clock frquency is less than 100MHz, the feedback clock must be
2704 * provided and DLL must not be used so that tuning can be skipped. To
2705 * provide feedback clock, the mode selection can be any value less
2706 * than 3'b011 in bits [2:0] of HOST CONTROL2 register.
2707 */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002708 if (host->clock <= CORE_FREQ_100MHZ) {
2709 if ((uhs == MMC_TIMING_MMC_HS400) ||
2710 (uhs == MMC_TIMING_MMC_HS200) ||
2711 (uhs == MMC_TIMING_UHS_SDR104))
2712 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302713
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002714 /*
2715 * Make sure DLL is disabled when not required
2716 *
2717 * Write 1 to DLL_RST bit of DLL_CONFIG register
2718 */
2719 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
2720 | CORE_DLL_RST),
2721 host->ioaddr + CORE_DLL_CONFIG);
2722
2723 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
2724 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
2725 | CORE_DLL_PDN),
2726 host->ioaddr + CORE_DLL_CONFIG);
2727 mb();
2728
2729 /*
2730 * The DLL needs to be restored and CDCLP533 recalibrated
2731 * when the clock frequency is set back to 400MHz.
2732 */
2733 msm_host->calibration_done = false;
2734 }
2735
2736 pr_debug("%s: %s-clock:%u uhs mode:%u ctrl_2:0x%x\n",
2737 mmc_hostname(host->mmc), __func__, host->clock, uhs, ctrl_2);
Sahitya Tummala14613432013-03-21 11:13:25 +05302738 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
2739
2740}
2741
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302742#define MAX_TEST_BUS 20
2743
2744void sdhci_msm_dump_vendor_regs(struct sdhci_host *host)
2745{
2746 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2747 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2748 int tbsel, tbsel2;
2749 int i, index = 0;
2750 u32 test_bus_val = 0;
2751 u32 debug_reg[MAX_TEST_BUS] = {0};
2752
2753 pr_info("----------- VENDOR REGISTER DUMP -----------\n");
2754 pr_info("Data cnt: 0x%08x | Fifo cnt: 0x%08x | Int sts: 0x%08x\n",
2755 readl_relaxed(msm_host->core_mem + CORE_MCI_DATA_CNT),
2756 readl_relaxed(msm_host->core_mem + CORE_MCI_FIFO_CNT),
2757 readl_relaxed(msm_host->core_mem + CORE_MCI_STATUS));
2758 pr_info("DLL cfg: 0x%08x | DLL sts: 0x%08x | SDCC ver: 0x%08x\n",
2759 readl_relaxed(host->ioaddr + CORE_DLL_CONFIG),
2760 readl_relaxed(host->ioaddr + CORE_DLL_STATUS),
2761 readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION));
2762 pr_info("Vndr func: 0x%08x | Vndr adma err : addr0: 0x%08x addr1: 0x%08x\n",
2763 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC),
2764 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_ADMA_ERR_ADDR0),
2765 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_ADMA_ERR_ADDR1));
2766
2767 /*
2768 * tbsel indicates [2:0] bits and tbsel2 indicates [7:4] bits
2769 * of CORE_TESTBUS_CONFIG register.
2770 *
2771 * To select test bus 0 to 7 use tbsel and to select any test bus
2772 * above 7 use (tbsel2 | tbsel) to get the test bus number. For eg,
2773 * to select test bus 14, write 0x1E to CORE_TESTBUS_CONFIG register
2774 * i.e., tbsel2[7:4] = 0001, tbsel[2:0] = 110.
2775 */
2776 for (tbsel2 = 0; tbsel2 < 3; tbsel2++) {
2777 for (tbsel = 0; tbsel < 8; tbsel++) {
2778 if (index >= MAX_TEST_BUS)
2779 break;
2780 test_bus_val = (tbsel2 << CORE_TESTBUS_SEL2_BIT) |
2781 tbsel | CORE_TESTBUS_ENA;
2782 writel_relaxed(test_bus_val,
2783 msm_host->core_mem + CORE_TESTBUS_CONFIG);
2784 debug_reg[index++] = readl_relaxed(msm_host->core_mem +
2785 CORE_SDCC_DEBUG_REG);
2786 }
2787 }
2788 for (i = 0; i < MAX_TEST_BUS; i = i + 4)
2789 pr_info(" Test bus[%d to %d]: 0x%08x 0x%08x 0x%08x 0x%08x\n",
2790 i, i + 3, debug_reg[i], debug_reg[i+1],
2791 debug_reg[i+2], debug_reg[i+3]);
2792 /* Disable test bus */
2793 writel_relaxed(~CORE_TESTBUS_ENA, msm_host->core_mem +
2794 CORE_TESTBUS_CONFIG);
2795}
2796
Asutosh Das0ef24812012-12-18 16:14:02 +05302797static struct sdhci_ops sdhci_msm_ops = {
Sahitya Tummala14613432013-03-21 11:13:25 +05302798 .set_uhs_signaling = sdhci_msm_set_uhs_signaling,
Asutosh Das0ef24812012-12-18 16:14:02 +05302799 .check_power_status = sdhci_msm_check_power_status,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002800 .platform_execute_tuning = sdhci_msm_execute_tuning,
2801 .toggle_cdr = sdhci_msm_toggle_cdr,
Asutosh Das648f9d12013-01-10 21:11:04 +05302802 .get_max_segments = sdhci_msm_max_segs,
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302803 .set_clock = sdhci_msm_set_clock,
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302804 .get_min_clock = sdhci_msm_get_min_clock,
2805 .get_max_clock = sdhci_msm_get_max_clock,
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302806 .dump_vendor_regs = sdhci_msm_dump_vendor_regs,
Asutosh Dase5e9ca62013-07-30 19:08:36 +05302807 .config_auto_tuning_cmd = sdhci_msm_config_auto_tuning_cmd,
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302808 .enable_controller_clock = sdhci_msm_enable_controller_clock,
Venkat Gopalakrishnanb8cb7072015-01-09 11:04:34 -08002809 .set_bus_width = sdhci_set_bus_width,
Venkat Gopalakrishnan411df072015-01-09 11:09:44 -08002810 .reset = sdhci_reset,
Asutosh Das0ef24812012-12-18 16:14:02 +05302811};
2812
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302813static void sdhci_set_default_hw_caps(struct sdhci_msm_host *msm_host,
2814 struct sdhci_host *host)
2815{
2816 u32 version, caps;
2817 u16 minor;
2818 u8 major;
2819
2820 version = readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION);
2821 major = (version & CORE_VERSION_MAJOR_MASK) >>
2822 CORE_VERSION_MAJOR_SHIFT;
2823 minor = version & CORE_VERSION_TARGET_MASK;
2824
2825 /*
2826 * Starting with SDCC 5 controller (core major version = 1)
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002827 * controller won't advertise 3.0v, 1.8v and 8-bit features
2828 * except for some targets.
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302829 */
2830 if (major >= 1 && minor != 0x11 && minor != 0x12) {
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002831 struct sdhci_msm_reg_data *vdd_io_reg;
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302832 caps = CORE_3_0V_SUPPORT;
Venkat Gopalakrishnan3ac8fd32014-08-28 18:15:45 -07002833 /*
2834 * Enable 1.8V support capability on controllers that
2835 * support dual voltage
2836 */
2837 vdd_io_reg = msm_host->pdata->vreg_data->vdd_io_data;
2838 if (vdd_io_reg &&
2839 (vdd_io_reg->low_vol_level != vdd_io_reg->high_vol_level))
2840 caps |= CORE_1_8V_SUPPORT;
Pratibhasagar Vada47992013-12-09 20:42:32 +05302841 if (msm_host->pdata->mmc_bus_width == MMC_CAP_8_BIT_DATA)
2842 caps |= CORE_8_BIT_SUPPORT;
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302843 writel_relaxed(
2844 (readl_relaxed(host->ioaddr + SDHCI_CAPABILITIES) |
2845 caps), host->ioaddr + CORE_VENDOR_SPEC_CAPABILITIES0);
2846 }
Krishna Konda2faa7bb2014-06-04 01:25:16 -07002847
2848 /*
2849 * SDCC 5 controller with major version 1, minor version 0x34 and later
2850 * with HS 400 mode support will use CM DLL instead of CDC LP 533 DLL.
2851 */
2852 if ((major == 1) && (minor < 0x34))
2853 msm_host->use_cdclp533 = true;
Gilad Broner2a10ca02014-10-02 17:20:35 +03002854
2855 /*
Venkat Gopalakrishnanc0a5c3a2015-02-03 16:10:07 -08002856 * SDCC 5 controller with major version 1, minor version 0x42 and later
2857 * will require additional steps when resetting DLL.
2858 */
2859 if ((major == 1) && (minor >= 0x42))
2860 msm_host->use_updated_dll_reset = true;
2861
2862 /*
Gilad Broner2a10ca02014-10-02 17:20:35 +03002863 * Mask 64-bit support for controller with 32-bit address bus so that
2864 * smaller descriptor size will be used and improve memory consumption.
2865 * In case bus addressing ever changes, controller version should be
2866 * used in order to decide whether or not to mask 64-bit support.
2867 */
2868 caps = readl_relaxed(host->ioaddr + SDHCI_CAPABILITIES);
2869 caps &= ~CORE_SYS_BUS_SUPPORT_64_BIT;
2870 writel_relaxed(caps, host->ioaddr + CORE_VENDOR_SPEC_CAPABILITIES0);
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302871}
2872
Asutosh Das0ef24812012-12-18 16:14:02 +05302873static int sdhci_msm_probe(struct platform_device *pdev)
2874{
2875 struct sdhci_host *host;
2876 struct sdhci_pltfm_host *pltfm_host;
2877 struct sdhci_msm_host *msm_host;
2878 struct resource *core_memres = NULL;
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02002879 int ret = 0, dead = 0;
Stephen Boyd8dce5c62013-04-24 14:19:46 -07002880 u16 host_version;
Subhash Jadavani28137342013-05-14 17:46:43 +05302881 u32 pwr, irq_status, irq_ctl;
Asutosh Das0ef24812012-12-18 16:14:02 +05302882
2883 pr_debug("%s: Enter %s\n", dev_name(&pdev->dev), __func__);
2884 msm_host = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_msm_host),
2885 GFP_KERNEL);
2886 if (!msm_host) {
2887 ret = -ENOMEM;
2888 goto out;
2889 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302890
2891 msm_host->sdhci_msm_pdata.ops = &sdhci_msm_ops;
2892 host = sdhci_pltfm_init(pdev, &msm_host->sdhci_msm_pdata, 0);
2893 if (IS_ERR(host)) {
2894 ret = PTR_ERR(host);
2895 goto out;
2896 }
2897
2898 pltfm_host = sdhci_priv(host);
2899 pltfm_host->priv = msm_host;
2900 msm_host->mmc = host->mmc;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302901 msm_host->pdev = pdev;
Asutosh Das0ef24812012-12-18 16:14:02 +05302902
2903 /* Extract platform data */
2904 if (pdev->dev.of_node) {
Venkat Gopalakrishnan270580a2013-03-11 12:17:57 -07002905 ret = of_alias_get_id(pdev->dev.of_node, "sdhc");
2906 if (ret < 0) {
2907 dev_err(&pdev->dev, "Failed to get slot index %d\n",
2908 ret);
2909 goto pltfm_free;
2910 }
2911 if (disable_slots & (1 << (ret - 1))) {
2912 dev_info(&pdev->dev, "%s: Slot %d disabled\n", __func__,
2913 ret);
2914 ret = -ENODEV;
2915 goto pltfm_free;
2916 }
2917
Asutosh Das0ef24812012-12-18 16:14:02 +05302918 msm_host->pdata = sdhci_msm_populate_pdata(&pdev->dev);
2919 if (!msm_host->pdata) {
2920 dev_err(&pdev->dev, "DT parsing error\n");
2921 goto pltfm_free;
2922 }
2923 } else {
2924 dev_err(&pdev->dev, "No device tree node\n");
2925 goto pltfm_free;
2926 }
2927
2928 /* Setup Clocks */
2929
2930 /* Setup SDCC bus voter clock. */
2931 msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus_clk");
2932 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
2933 /* Vote for max. clk rate for max. performance */
2934 ret = clk_set_rate(msm_host->bus_clk, INT_MAX);
2935 if (ret)
2936 goto pltfm_free;
2937 ret = clk_prepare_enable(msm_host->bus_clk);
2938 if (ret)
2939 goto pltfm_free;
2940 }
2941
2942 /* Setup main peripheral bus clock */
2943 msm_host->pclk = devm_clk_get(&pdev->dev, "iface_clk");
2944 if (!IS_ERR(msm_host->pclk)) {
2945 ret = clk_prepare_enable(msm_host->pclk);
2946 if (ret)
2947 goto bus_clk_disable;
2948 }
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302949 atomic_set(&msm_host->controller_clock, 1);
Asutosh Das0ef24812012-12-18 16:14:02 +05302950
2951 /* Setup SDC MMC clock */
2952 msm_host->clk = devm_clk_get(&pdev->dev, "core_clk");
2953 if (IS_ERR(msm_host->clk)) {
2954 ret = PTR_ERR(msm_host->clk);
2955 goto pclk_disable;
2956 }
2957
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302958 /* Set to the minimum supported clock frequency */
2959 ret = clk_set_rate(msm_host->clk, sdhci_msm_get_min_clock(host));
2960 if (ret) {
2961 dev_err(&pdev->dev, "MClk rate set failed (%d)\n", ret);
Sahitya Tummala020ede0d2013-06-07 13:03:07 +05302962 goto pclk_disable;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302963 }
Sahitya Tummala020ede0d2013-06-07 13:03:07 +05302964 ret = clk_prepare_enable(msm_host->clk);
2965 if (ret)
2966 goto pclk_disable;
2967
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302968 msm_host->clk_rate = sdhci_msm_get_min_clock(host);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302969 atomic_set(&msm_host->clks_on, 1);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302970
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002971 /* Setup CDC calibration fixed feedback clock */
2972 msm_host->ff_clk = devm_clk_get(&pdev->dev, "cal_clk");
2973 if (!IS_ERR(msm_host->ff_clk)) {
2974 ret = clk_prepare_enable(msm_host->ff_clk);
2975 if (ret)
2976 goto clk_disable;
2977 }
2978
2979 /* Setup CDC calibration sleep clock */
2980 msm_host->sleep_clk = devm_clk_get(&pdev->dev, "sleep_clk");
2981 if (!IS_ERR(msm_host->sleep_clk)) {
2982 ret = clk_prepare_enable(msm_host->sleep_clk);
2983 if (ret)
2984 goto ff_clk_disable;
2985 }
2986
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07002987 msm_host->saved_tuning_phase = INVALID_TUNING_PHASE;
2988
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302989 ret = sdhci_msm_bus_register(msm_host, pdev);
2990 if (ret)
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002991 goto sleep_clk_disable;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302992
2993 if (msm_host->msm_bus_vote.client_handle)
2994 INIT_DELAYED_WORK(&msm_host->msm_bus_vote.vote_work,
2995 sdhci_msm_bus_work);
2996 sdhci_msm_bus_voting(host, 1);
2997
Asutosh Das0ef24812012-12-18 16:14:02 +05302998 /* Setup regulators */
2999 ret = sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, true);
3000 if (ret) {
3001 dev_err(&pdev->dev, "Regulator setup failed (%d)\n", ret);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05303002 goto bus_unregister;
Asutosh Das0ef24812012-12-18 16:14:02 +05303003 }
3004
3005 /* Reset the core and Enable SDHC mode */
3006 core_memres = platform_get_resource_byname(pdev,
3007 IORESOURCE_MEM, "core_mem");
Asutosh Das890bdee2014-08-08 23:01:42 +05303008 if (!core_memres) {
3009 dev_err(&pdev->dev, "Failed to get iomem resource\n");
3010 goto vreg_deinit;
3011 }
Asutosh Das0ef24812012-12-18 16:14:02 +05303012 msm_host->core_mem = devm_ioremap(&pdev->dev, core_memres->start,
3013 resource_size(core_memres));
3014
3015 if (!msm_host->core_mem) {
3016 dev_err(&pdev->dev, "Failed to remap registers\n");
3017 ret = -ENOMEM;
3018 goto vreg_deinit;
3019 }
3020
Stepan Moskovchenkoa4d0fa72013-09-13 22:19:33 -07003021 /* Unset HC_MODE_EN bit in HC_MODE register */
3022 writel_relaxed(0, (msm_host->core_mem + CORE_HC_MODE));
3023
Asutosh Das0ef24812012-12-18 16:14:02 +05303024 /* Set SW_RST bit in POWER register (Offset 0x0) */
Sahitya Tummala66b0fe32013-04-25 11:50:56 +05303025 writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_POWER) |
3026 CORE_SW_RST, msm_host->core_mem + CORE_POWER);
3027 /*
3028 * SW reset can take upto 10HCLK + 15MCLK cycles.
3029 * Calculating based on min clk rates (hclk = 27MHz,
3030 * mclk = 400KHz) it comes to ~40us. Let's poll for
3031 * max. 1ms for reset completion.
3032 */
3033 ret = readl_poll_timeout(msm_host->core_mem + CORE_POWER,
Matt Wagantallc7097a22014-04-29 15:48:15 -07003034 pwr, !(pwr & CORE_SW_RST), 10, 1000);
Sahitya Tummala66b0fe32013-04-25 11:50:56 +05303035
3036 if (ret) {
3037 dev_err(&pdev->dev, "reset failed (%d)\n", ret);
3038 goto vreg_deinit;
3039 }
Asutosh Das0ef24812012-12-18 16:14:02 +05303040 /* Set HC_MODE_EN bit in HC_MODE register */
3041 writel_relaxed(HC_MODE_EN, (msm_host->core_mem + CORE_HC_MODE));
3042
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07003043 /* Set FF_CLK_SW_RST_DIS bit in HC_MODE register */
3044 writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_HC_MODE) |
3045 FF_CLK_SW_RST_DIS, msm_host->core_mem + CORE_HC_MODE);
3046
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05303047 sdhci_set_default_hw_caps(msm_host, host);
Asutosh Das0ef24812012-12-18 16:14:02 +05303048 /*
Subhash Jadavani28137342013-05-14 17:46:43 +05303049 * CORE_SW_RST above may trigger power irq if previous status of PWRCTL
3050 * was either BUS_ON or IO_HIGH_V. So before we enable the power irq
3051 * interrupt in GIC (by registering the interrupt handler), we need to
3052 * ensure that any pending power irq interrupt status is acknowledged
3053 * otherwise power irq interrupt handler would be fired prematurely.
3054 */
3055 irq_status = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
3056 writel_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
3057 irq_ctl = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_CTL);
3058 if (irq_status & (CORE_PWRCTL_BUS_ON | CORE_PWRCTL_BUS_OFF))
3059 irq_ctl |= CORE_PWRCTL_BUS_SUCCESS;
3060 if (irq_status & (CORE_PWRCTL_IO_HIGH | CORE_PWRCTL_IO_LOW))
3061 irq_ctl |= CORE_PWRCTL_IO_SUCCESS;
3062 writel_relaxed(irq_ctl, (msm_host->core_mem + CORE_PWRCTL_CTL));
3063 /*
3064 * Ensure that above writes are propogated before interrupt enablement
3065 * in GIC.
3066 */
3067 mb();
3068
3069 /*
Asutosh Das0ef24812012-12-18 16:14:02 +05303070 * Following are the deviations from SDHC spec v3.0 -
3071 * 1. Card detection is handled using separate GPIO.
3072 * 2. Bus power control is handled by interacting with PMIC.
3073 */
3074 host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
3075 host->quirks |= SDHCI_QUIRK_SINGLE_POWER_WRITE;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05303076 host->quirks |= SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN;
3077 host->quirks2 |= SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK;
Sahitya Tummala87d439442013-04-12 11:49:11 +05303078 host->quirks2 |= SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD;
Sahitya Tummala314162c2013-04-12 12:11:20 +05303079 host->quirks2 |= SDHCI_QUIRK2_BROKEN_PRESET_VALUE;
Sahitya Tummala7c9780d2013-04-12 11:59:25 +05303080 host->quirks2 |= SDHCI_QUIRK2_USE_RESERVED_MAX_TIMEOUT;
Asutosh Das0ef24812012-12-18 16:14:02 +05303081
Sahitya Tummalaa5733ab52013-06-10 16:32:51 +05303082 if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK)
3083 host->quirks2 |= SDHCI_QUIRK2_DIVIDE_TOUT_BY_4;
3084
Stephen Boyd8dce5c62013-04-24 14:19:46 -07003085 host_version = readw_relaxed((host->ioaddr + SDHCI_HOST_VERSION));
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07003086 dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n",
3087 host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >>
3088 SDHCI_VENDOR_VER_SHIFT));
3089 if (((host_version & SDHCI_VENDOR_VER_MASK) >>
3090 SDHCI_VENDOR_VER_SHIFT) == SDHCI_VER_100) {
3091 /*
3092 * Add 40us delay in interrupt handler when
3093 * operating at initialization frequency(400KHz).
3094 */
3095 host->quirks2 |= SDHCI_QUIRK2_SLOW_INT_CLR;
3096 /*
3097 * Set Software Reset for DAT line in Software
3098 * Reset Register (Bit 2).
3099 */
3100 host->quirks2 |= SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT;
3101 }
3102
Asutosh Das214b9662013-06-13 14:27:42 +05303103 host->quirks2 |= SDHCI_QUIRK2_IGN_DATA_END_BIT_ERROR;
3104
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07003105 /* Setup PWRCTL irq */
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02003106 msm_host->pwr_irq = platform_get_irq_byname(pdev, "pwr_irq");
3107 if (msm_host->pwr_irq < 0) {
Asutosh Das0ef24812012-12-18 16:14:02 +05303108 dev_err(&pdev->dev, "Failed to get pwr_irq by name (%d)\n",
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02003109 msm_host->pwr_irq);
Asutosh Das0ef24812012-12-18 16:14:02 +05303110 goto vreg_deinit;
3111 }
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02003112 ret = devm_request_threaded_irq(&pdev->dev, msm_host->pwr_irq, NULL,
Asutosh Das0ef24812012-12-18 16:14:02 +05303113 sdhci_msm_pwr_irq, IRQF_ONESHOT,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07003114 dev_name(&pdev->dev), host);
Asutosh Das0ef24812012-12-18 16:14:02 +05303115 if (ret) {
3116 dev_err(&pdev->dev, "Request threaded irq(%d) failed (%d)\n",
Konstantein Dorfmane0be02d2015-01-27 12:06:02 +02003117 msm_host->pwr_irq, ret);
Asutosh Das0ef24812012-12-18 16:14:02 +05303118 goto vreg_deinit;
3119 }
3120
3121 /* Enable pwr irq interrupts */
3122 writel_relaxed(INT_MASK, (msm_host->core_mem + CORE_PWRCTL_MASK));
3123
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05303124#ifdef CONFIG_MMC_CLKGATE
3125 /* Set clock gating delay to be used when CONFIG_MMC_CLKGATE is set */
3126 msm_host->mmc->clkgate_delay = SDHCI_MSM_MMC_CLK_GATE_DELAY;
3127#endif
3128
Asutosh Das0ef24812012-12-18 16:14:02 +05303129 /* Set host capabilities */
3130 msm_host->mmc->caps |= msm_host->pdata->mmc_bus_width;
3131 msm_host->mmc->caps |= msm_host->pdata->caps;
Asutosh Das0ef24812012-12-18 16:14:02 +05303132 msm_host->mmc->caps2 |= msm_host->pdata->caps2;
Venkat Gopalakrishnan97c16112014-12-16 18:26:25 -08003133 msm_host->mmc->caps2 |= MMC_CAP2_BOOTPART_NOACC;
3134 msm_host->mmc->caps2 |= MMC_CAP2_FULL_PWR_CYCLE;
Subhash Jadavani6d472b22013-05-29 15:52:10 +05303135 msm_host->mmc->caps2 |= MMC_CAP2_ASYNC_SDIO_IRQ_4BIT_MODE;
Venkat Gopalakrishnan97c16112014-12-16 18:26:25 -08003136 msm_host->mmc->caps2 |= MMC_CAP2_HS400_POST_TUNING;
Asutosh Dasbea115d2013-06-24 18:20:45 +05303137 msm_host->mmc->pm_caps |= MMC_PM_KEEP_POWER;
Asutosh Das0ef24812012-12-18 16:14:02 +05303138
3139 if (msm_host->pdata->nonremovable)
3140 msm_host->mmc->caps |= MMC_CAP_NONREMOVABLE;
3141
Guoping Yuf7c91332014-08-20 16:56:18 +08003142 if (msm_host->pdata->nonhotplug)
3143 msm_host->mmc->caps2 |= MMC_CAP2_NONHOTPLUG;
3144
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05303145 host->cpu_dma_latency_us = msm_host->pdata->cpu_dma_latency_us;
Maya Erez994cf2f2014-10-21 20:22:04 +03003146 host->pm_qos_req_dma.type = msm_host->pdata->cpu_affinity_type;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05303147
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05303148 init_completion(&msm_host->pwr_irq_completion);
3149
Sahitya Tummala581df132013-03-12 14:57:46 +05303150 if (gpio_is_valid(msm_host->pdata->status_gpio)) {
Sahitya Tummala6ddabb42014-06-05 13:26:55 +05303151 /*
3152 * Set up the card detect GPIO in active configuration before
3153 * configuring it as an IRQ. Otherwise, it can be in some
3154 * weird/inconsistent state resulting in flood of interrupts.
3155 */
3156 sdhci_msm_setup_pins(msm_host->pdata, true);
3157
Sahitya Tummala581df132013-03-12 14:57:46 +05303158 ret = mmc_gpio_request_cd(msm_host->mmc,
3159 msm_host->pdata->status_gpio, 0);
3160 if (ret) {
3161 dev_err(&pdev->dev, "%s: Failed to request card detection IRQ %d\n",
3162 __func__, ret);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05303163 goto vreg_deinit;
Sahitya Tummala581df132013-03-12 14:57:46 +05303164 }
3165 }
3166
Krishna Konda7feab352013-09-17 23:55:40 -07003167 if ((sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT) &&
3168 (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(64)))) {
3169 host->dma_mask = DMA_BIT_MASK(64);
3170 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
3171 } else if (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(32))) {
Sahitya Tummalaeaa21862013-03-20 19:34:59 +05303172 host->dma_mask = DMA_BIT_MASK(32);
3173 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
3174 } else {
3175 dev_err(&pdev->dev, "%s: Failed to set dma mask\n", __func__);
3176 }
3177
Asutosh Das0ef24812012-12-18 16:14:02 +05303178 ret = sdhci_add_host(host);
3179 if (ret) {
3180 dev_err(&pdev->dev, "Add host failed (%d)\n", ret);
Sahitya Tummala581df132013-03-12 14:57:46 +05303181 goto vreg_deinit;
Asutosh Das0ef24812012-12-18 16:14:02 +05303182 }
3183
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05303184 msm_host->msm_bus_vote.max_bus_bw.show = show_sdhci_max_bus_bw;
3185 msm_host->msm_bus_vote.max_bus_bw.store = store_sdhci_max_bus_bw;
3186 sysfs_attr_init(&msm_host->msm_bus_vote.max_bus_bw.attr);
3187 msm_host->msm_bus_vote.max_bus_bw.attr.name = "max_bus_bw";
3188 msm_host->msm_bus_vote.max_bus_bw.attr.mode = S_IRUGO | S_IWUSR;
3189 ret = device_create_file(&pdev->dev,
3190 &msm_host->msm_bus_vote.max_bus_bw);
3191 if (ret)
3192 goto remove_host;
3193
Sahitya Tummala5c55b932013-06-20 14:00:18 +05303194 if (!gpio_is_valid(msm_host->pdata->status_gpio)) {
3195 msm_host->polling.show = show_polling;
3196 msm_host->polling.store = store_polling;
3197 sysfs_attr_init(&msm_host->polling.attr);
3198 msm_host->polling.attr.name = "polling";
3199 msm_host->polling.attr.mode = S_IRUGO | S_IWUSR;
3200 ret = device_create_file(&pdev->dev, &msm_host->polling);
3201 if (ret)
3202 goto remove_max_bus_bw_file;
3203 }
Asutosh Dase5e9ca62013-07-30 19:08:36 +05303204
3205 msm_host->auto_cmd21_attr.show = show_auto_cmd21;
3206 msm_host->auto_cmd21_attr.store = store_auto_cmd21;
3207 sysfs_attr_init(&msm_host->auto_cmd21_attr.attr);
3208 msm_host->auto_cmd21_attr.attr.name = "enable_auto_cmd21";
3209 msm_host->auto_cmd21_attr.attr.mode = S_IRUGO | S_IWUSR;
3210 ret = device_create_file(&pdev->dev, &msm_host->auto_cmd21_attr);
3211 if (ret) {
3212 pr_err("%s: %s: failed creating auto-cmd21 attr: %d\n",
3213 mmc_hostname(host->mmc), __func__, ret);
3214 device_remove_file(&pdev->dev, &msm_host->auto_cmd21_attr);
3215 }
3216
Asutosh Das0ef24812012-12-18 16:14:02 +05303217 /* Successful initialization */
3218 goto out;
3219
Sahitya Tummala5c55b932013-06-20 14:00:18 +05303220remove_max_bus_bw_file:
3221 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Asutosh Das0ef24812012-12-18 16:14:02 +05303222remove_host:
3223 dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
3224 sdhci_remove_host(host, dead);
3225vreg_deinit:
3226 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05303227bus_unregister:
3228 if (msm_host->msm_bus_vote.client_handle)
3229 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
3230 sdhci_msm_bus_unregister(msm_host);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07003231sleep_clk_disable:
3232 if (!IS_ERR(msm_host->sleep_clk))
3233 clk_disable_unprepare(msm_host->sleep_clk);
3234ff_clk_disable:
3235 if (!IS_ERR(msm_host->ff_clk))
3236 clk_disable_unprepare(msm_host->ff_clk);
Asutosh Das0ef24812012-12-18 16:14:02 +05303237clk_disable:
3238 if (!IS_ERR(msm_host->clk))
3239 clk_disable_unprepare(msm_host->clk);
3240pclk_disable:
3241 if (!IS_ERR(msm_host->pclk))
3242 clk_disable_unprepare(msm_host->pclk);
3243bus_clk_disable:
3244 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
3245 clk_disable_unprepare(msm_host->bus_clk);
3246pltfm_free:
3247 sdhci_pltfm_free(pdev);
3248out:
3249 pr_debug("%s: Exit %s\n", dev_name(&pdev->dev), __func__);
3250 return ret;
3251}
3252
3253static int sdhci_msm_remove(struct platform_device *pdev)
3254{
3255 struct sdhci_host *host = platform_get_drvdata(pdev);
3256 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
3257 struct sdhci_msm_host *msm_host = pltfm_host->priv;
3258 struct sdhci_msm_pltfm_data *pdata = msm_host->pdata;
3259 int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) ==
3260 0xffffffff);
3261
3262 pr_debug("%s: %s\n", dev_name(&pdev->dev), __func__);
Sahitya Tummala5c55b932013-06-20 14:00:18 +05303263 if (!gpio_is_valid(msm_host->pdata->status_gpio))
3264 device_remove_file(&pdev->dev, &msm_host->polling);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05303265 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Asutosh Das0ef24812012-12-18 16:14:02 +05303266 sdhci_remove_host(host, dead);
3267 sdhci_pltfm_free(pdev);
Sahitya Tummala581df132013-03-12 14:57:46 +05303268
Asutosh Das0ef24812012-12-18 16:14:02 +05303269 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05303270
Pratibhasagar V9acf2642013-11-21 21:07:21 +05303271 sdhci_msm_setup_pins(pdata, true);
3272 sdhci_msm_setup_pins(pdata, false);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05303273
3274 if (msm_host->msm_bus_vote.client_handle) {
3275 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
3276 sdhci_msm_bus_unregister(msm_host);
3277 }
Asutosh Das0ef24812012-12-18 16:14:02 +05303278 return 0;
3279}
3280
3281static const struct of_device_id sdhci_msm_dt_match[] = {
3282 {.compatible = "qcom,sdhci-msm"},
3283};
3284MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match);
3285
3286static struct platform_driver sdhci_msm_driver = {
3287 .probe = sdhci_msm_probe,
3288 .remove = sdhci_msm_remove,
3289 .driver = {
3290 .name = "sdhci_msm",
3291 .owner = THIS_MODULE,
3292 .of_match_table = sdhci_msm_dt_match,
3293 },
3294};
3295
3296module_platform_driver(sdhci_msm_driver);
3297
3298MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Secure Digital Host Controller Interface driver");
3299MODULE_LICENSE("GPL v2");